Skip to content

Commit 03f87c0

Browse files
tohojoAlexei Starovoitov
authored andcommitted
bpf: Propagate expected_attach_type when verifying freplace programs
For some program types, the verifier relies on the expected_attach_type of the program being verified in the verification process. However, for freplace programs, the attach type was not propagated along with the verifier ops, so the expected_attach_type would always be zero for freplace programs. This in turn caused the verifier to sometimes make the wrong call for freplace programs. For all existing uses of expected_attach_type for this purpose, the result of this was only false negatives (i.e., freplace functions would be rejected by the verifier even though they were valid programs for the target they were replacing). However, should a false positive be introduced, this can lead to out-of-bounds accesses and/or crashes. The fix introduced in this patch is to propagate the expected_attach_type to the freplace program during verification, and reset it after that is done. Fixes: be8704f ("bpf: Introduce dynamic program extensions") Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/158773526726.293902.13257293296560360508.stgit@toke.dk
1 parent 4adb7a4 commit 03f87c0

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10497,6 +10497,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
1049710497
return -EINVAL;
1049810498
}
1049910499
env->ops = bpf_verifier_ops[tgt_prog->type];
10500+
prog->expected_attach_type = tgt_prog->expected_attach_type;
1050010501
}
1050110502
if (!tgt_prog->jited) {
1050210503
verbose(env, "Can attach to only JITed progs\n");
@@ -10841,6 +10842,13 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
1084110842
* them now. Otherwise free_used_maps() will release them.
1084210843
*/
1084310844
release_maps(env);
10845+
10846+
/* extension progs temporarily inherit the attach_type of their targets
10847+
for verification purposes, so set it back to zero before returning
10848+
*/
10849+
if (env->prog->type == BPF_PROG_TYPE_EXT)
10850+
env->prog->expected_attach_type = 0;
10851+
1084410852
*prog = env->prog;
1084510853
err_unlock:
1084610854
if (!is_priv)

0 commit comments

Comments
 (0)