Skip to content

Commit 8726679

Browse files
4astdavem330
authored andcommitted
bpf: teach verifier to track stack depth
teach verifier to track bpf program stack depth Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f696b8f commit 8726679

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ struct bpf_prog_aux {
171171
atomic_t refcnt;
172172
u32 used_map_cnt;
173173
u32 max_ctx_offset;
174+
u32 stack_depth;
174175
struct latch_tree_node ksym_tnode;
175176
struct list_head ksym_lnode;
176177
const struct bpf_verifier_ops *ops;

kernel/bpf/verifier.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,10 @@ static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
926926
verbose("invalid stack off=%d size=%d\n", off, size);
927927
return -EACCES;
928928
}
929+
930+
if (env->prog->aux->stack_depth < -off)
931+
env->prog->aux->stack_depth = -off;
932+
929933
if (t == BPF_WRITE) {
930934
if (!env->allow_ptr_leaks &&
931935
state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
@@ -1032,6 +1036,9 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
10321036
return -EACCES;
10331037
}
10341038

1039+
if (env->prog->aux->stack_depth < -off)
1040+
env->prog->aux->stack_depth = -off;
1041+
10351042
if (meta && meta->raw_mode) {
10361043
meta->access_size = access_size;
10371044
meta->regno = regno;
@@ -3167,7 +3174,8 @@ static int do_check(struct bpf_verifier_env *env)
31673174
insn_idx++;
31683175
}
31693176

3170-
verbose("processed %d insns\n", insn_processed);
3177+
verbose("processed %d insns, stack depth %d\n",
3178+
insn_processed, env->prog->aux->stack_depth);
31713179
return 0;
31723180
}
31733181

0 commit comments

Comments
 (0)