Skip to content

Commit

Permalink
bpf: teach verifier to track stack depth
Browse files Browse the repository at this point in the history
teach verifier to track bpf program stack depth

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Alexei Starovoitov authored and David S. Miller committed May 31, 2017
1 parent f696b8f commit 8726679
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ struct bpf_prog_aux {
atomic_t refcnt;
u32 used_map_cnt;
u32 max_ctx_offset;
u32 stack_depth;
struct latch_tree_node ksym_tnode;
struct list_head ksym_lnode;
const struct bpf_verifier_ops *ops;
Expand Down
10 changes: 9 additions & 1 deletion kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,10 @@ static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
verbose("invalid stack off=%d size=%d\n", off, size);
return -EACCES;
}

if (env->prog->aux->stack_depth < -off)
env->prog->aux->stack_depth = -off;

if (t == BPF_WRITE) {
if (!env->allow_ptr_leaks &&
state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
Expand Down Expand Up @@ -1032,6 +1036,9 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
return -EACCES;
}

if (env->prog->aux->stack_depth < -off)
env->prog->aux->stack_depth = -off;

if (meta && meta->raw_mode) {
meta->access_size = access_size;
meta->regno = regno;
Expand Down Expand Up @@ -3167,7 +3174,8 @@ static int do_check(struct bpf_verifier_env *env)
insn_idx++;
}

verbose("processed %d insns\n", insn_processed);
verbose("processed %d insns, stack depth %d\n",
insn_processed, env->prog->aux->stack_depth);
return 0;
}

Expand Down

0 comments on commit 8726679

Please sign in to comment.