Skip to content

Commit 51081a3

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
bpf: track changes_pkt_data property for global functions
When processing calls to certain helpers, verifier invalidates all packet pointers in a current state. For example, consider the following program: __attribute__((__noinline__)) long skb_pull_data(struct __sk_buff *sk, __u32 len) { return bpf_skb_pull_data(sk, len); } SEC("tc") int test_invalidate_checks(struct __sk_buff *sk) { int *p = (void *)(long)sk->data; if ((void *)(p + 1) > (void *)(long)sk->data_end) return TCX_DROP; skb_pull_data(sk, 0); *p = 42; return TCX_PASS; } After a call to bpf_skb_pull_data() the pointer 'p' can't be used safely. See function filter.c:bpf_helper_changes_pkt_data() for a list of such helpers. At the moment verifier invalidates packet pointers when processing helper function calls, and does not traverse global sub-programs when processing calls to global sub-programs. This means that calls to helpers done from global sub-programs do not invalidate pointers in the caller state. E.g. the program above is unsafe, but is not rejected by verifier. This commit fixes the omission by computing field bpf_subprog_info->changes_pkt_data for each sub-program before main verification pass. changes_pkt_data should be set if: - subprogram calls helper for which bpf_helper_changes_pkt_data returns true; - subprogram calls a global function, for which bpf_subprog_info->changes_pkt_data should be set. The verifier.c:check_cfg() pass is modified to compute this information. The commit relies on depth first instruction traversal done by check_cfg() and absence of recursive function calls: - check_cfg() would eventually visit every call to subprogram S in a state when S is fully explored; - when S is fully explored: - every direct helper call within S is explored (and thus changes_pkt_data is set if needed); - every call to subprogram S1 called by S was visited with S1 fully explored (and thus S inherits changes_pkt_data from S1). The downside of such approach is that dead code elimination is not taken into account: if a helper call inside global function is dead because of current configuration, verifier would conservatively assume that the call occurs for the purpose of the changes_pkt_data computation. Reported-by: Nick Zavaritsky <mejedi@gmail.com> Closes: https://lore.kernel.org/bpf/0498CA22-5779-4767-9C0C-A9515CEA711F@gmail.com/ Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20241210041100.1898468-4-eddyz87@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent b238e18 commit 51081a3

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ struct bpf_subprog_info {
659659
bool args_cached: 1;
660660
/* true if bpf_fastcall stack region is used by functions that can't be inlined */
661661
bool keep_fastcall_stack: 1;
662+
bool changes_pkt_data: 1;
662663

663664
enum priv_stack_mode priv_stack_mode;
664665
u8 arg_cnt;

kernel/bpf/verifier.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10042,6 +10042,8 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1004210042

1004310043
verbose(env, "Func#%d ('%s') is global and assumed valid.\n",
1004410044
subprog, sub_name);
10045+
if (env->subprog_info[subprog].changes_pkt_data)
10046+
clear_all_pkt_pointers(env);
1004510047
/* mark global subprog for verifying after main prog */
1004610048
subprog_aux(env, subprog)->called = true;
1004710049
clear_caller_saved_regs(env, caller->regs);
@@ -16246,6 +16248,29 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
1624616248
return 0;
1624716249
}
1624816250

16251+
static void mark_subprog_changes_pkt_data(struct bpf_verifier_env *env, int off)
16252+
{
16253+
struct bpf_subprog_info *subprog;
16254+
16255+
subprog = find_containing_subprog(env, off);
16256+
subprog->changes_pkt_data = true;
16257+
}
16258+
16259+
/* 't' is an index of a call-site.
16260+
* 'w' is a callee entry point.
16261+
* Eventually this function would be called when env->cfg.insn_state[w] == EXPLORED.
16262+
* Rely on DFS traversal order and absence of recursive calls to guarantee that
16263+
* callee's change_pkt_data marks would be correct at that moment.
16264+
*/
16265+
static void merge_callee_effects(struct bpf_verifier_env *env, int t, int w)
16266+
{
16267+
struct bpf_subprog_info *caller, *callee;
16268+
16269+
caller = find_containing_subprog(env, t);
16270+
callee = find_containing_subprog(env, w);
16271+
caller->changes_pkt_data |= callee->changes_pkt_data;
16272+
}
16273+
1624916274
/* non-recursive DFS pseudo code
1625016275
* 1 procedure DFS-iterative(G,v):
1625116276
* 2 label v as discovered
@@ -16379,6 +16404,7 @@ static int visit_func_call_insn(int t, struct bpf_insn *insns,
1637916404
bool visit_callee)
1638016405
{
1638116406
int ret, insn_sz;
16407+
int w;
1638216408

1638316409
insn_sz = bpf_is_ldimm64(&insns[t]) ? 2 : 1;
1638416410
ret = push_insn(t, t + insn_sz, FALLTHROUGH, env);
@@ -16390,8 +16416,10 @@ static int visit_func_call_insn(int t, struct bpf_insn *insns,
1639016416
mark_jmp_point(env, t + insn_sz);
1639116417

1639216418
if (visit_callee) {
16419+
w = t + insns[t].imm + 1;
1639316420
mark_prune_point(env, t);
16394-
ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
16421+
merge_callee_effects(env, t, w);
16422+
ret = push_insn(t, w, BRANCH, env);
1639516423
}
1639616424
return ret;
1639716425
}
@@ -16708,6 +16736,8 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
1670816736
mark_prune_point(env, t);
1670916737
mark_jmp_point(env, t);
1671016738
}
16739+
if (bpf_helper_call(insn) && bpf_helper_changes_pkt_data(insn->imm))
16740+
mark_subprog_changes_pkt_data(env, t);
1671116741
if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
1671216742
struct bpf_kfunc_call_arg_meta meta;
1671316743

0 commit comments

Comments
 (0)