Skip to content

Commit 81f6d05

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
bpf: check changes_pkt_data property for extension programs
When processing calls to global sub-programs, verifier decides whether to invalidate all packet pointers in current state depending on the changes_pkt_data property of the global sub-program. Because of this, an extension program replacing a global sub-program must be compatible with changes_pkt_data property of the sub-program being replaced. This commit: - adds changes_pkt_data flag to struct bpf_prog_aux: - this flag is set in check_cfg() for main sub-program; - in jit_subprogs() for other sub-programs; - modifies bpf_check_attach_btf_id() to check changes_pkt_data flag; - moves call to check_attach_btf_id() after the call to check_cfg(), because it needs changes_pkt_data flag to be set: bpf_check: ... ... - check_attach_btf_id resolve_pseudo_ldimm64 resolve_pseudo_ldimm64 --> bpf_prog_is_offloaded bpf_prog_is_offloaded check_cfg check_cfg + check_attach_btf_id ... ... The following fields are set by check_attach_btf_id(): - env->ops - prog->aux->attach_btf_trace - prog->aux->attach_func_name - prog->aux->attach_func_proto - prog->aux->dst_trampoline - prog->aux->mod - prog->aux->saved_dst_attach_type - prog->aux->saved_dst_prog_type - prog->expected_attach_type Neither of these fields are used by resolve_pseudo_ldimm64() or bpf_prog_offload_verifier_prep() (for netronome and netdevsim drivers), so the reordering is safe. Suggested-by: Alexei Starovoitov <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 3f23ee5 commit 81f6d05

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,7 @@ struct bpf_prog_aux {
15271527
bool is_extended; /* true if extended by freplace program */
15281528
bool jits_use_priv_stack;
15291529
bool priv_stack_requested;
1530+
bool changes_pkt_data;
15301531
u64 prog_array_member_cnt; /* counts how many times as member of prog_array */
15311532
struct mutex ext_mutex; /* mutex for is_extended and prog_array_member_cnt */
15321533
struct bpf_arena *arena;

kernel/bpf/verifier.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16872,6 +16872,7 @@ static int check_cfg(struct bpf_verifier_env *env)
1687216872
}
1687316873
}
1687416874
ret = 0; /* cfg looks good */
16875+
env->prog->aux->changes_pkt_data = env->subprog_info[0].changes_pkt_data;
1687516876

1687616877
err_free:
1687716878
kvfree(insn_state);
@@ -20361,6 +20362,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
2036120362
func[i]->aux->num_exentries = num_exentries;
2036220363
func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable;
2036320364
func[i]->aux->exception_cb = env->subprog_info[i].is_exception_cb;
20365+
func[i]->aux->changes_pkt_data = env->subprog_info[i].changes_pkt_data;
2036420366
if (!i)
2036520367
func[i]->aux->exception_boundary = env->seen_exception;
2036620368
func[i] = bpf_int_jit_compile(func[i]);
@@ -22225,6 +22227,12 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
2222522227
"Extension programs should be JITed\n");
2222622228
return -EINVAL;
2222722229
}
22230+
if (prog->aux->changes_pkt_data &&
22231+
!aux->func[subprog]->aux->changes_pkt_data) {
22232+
bpf_log(log,
22233+
"Extension program changes packet data, while original does not\n");
22234+
return -EINVAL;
22235+
}
2222822236
}
2222922237
if (!tgt_prog->jited) {
2223022238
bpf_log(log, "Can attach to only JITed progs\n");
@@ -22690,10 +22698,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
2269022698
if (ret < 0)
2269122699
goto skip_full_check;
2269222700

22693-
ret = check_attach_btf_id(env);
22694-
if (ret)
22695-
goto skip_full_check;
22696-
2269722701
ret = resolve_pseudo_ldimm64(env);
2269822702
if (ret < 0)
2269922703
goto skip_full_check;
@@ -22708,6 +22712,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
2270822712
if (ret < 0)
2270922713
goto skip_full_check;
2271022714

22715+
ret = check_attach_btf_id(env);
22716+
if (ret)
22717+
goto skip_full_check;
22718+
2271122719
ret = mark_fastcall_patterns(env);
2271222720
if (ret < 0)
2271322721
goto skip_full_check;

0 commit comments

Comments
 (0)