]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bpf: Make reg_not_null() true for CONST_PTR_TO_MAP
authorIhor Solodrai <isolodrai@meta.com>
Mon, 9 Jun 2025 18:30:22 +0000 (11:30 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 9 Jun 2025 23:42:04 +0000 (16:42 -0700)
When reg->type is CONST_PTR_TO_MAP, it can not be null. However the
verifier explores the branches under rX == 0 in check_cond_jmp_op()
even if reg->type is CONST_PTR_TO_MAP, because it was not checked for
in reg_not_null().

Fix this by adding CONST_PTR_TO_MAP to the set of types that are
considered non nullable in reg_not_null().

An old "unpriv: cmp map pointer with zero" selftest fails with this
change, because now early out correctly triggers in
check_cond_jmp_op(), making the verification to pass.

In practice verifier may allow pointer to null comparison in unpriv,
since in many cases the relevant branch and comparison op are removed
as dead code. So change the expected test result to __success_unpriv.

Signed-off-by: Ihor Solodrai <isolodrai@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250609183024.359974-2-isolodrai@meta.com
kernel/bpf/verifier.c
tools/testing/selftests/bpf/progs/verifier_unpriv.c

index e31f6b0ccb30e6a80293c1e5620ffc737590e31e..48a3241cda0ffb33e5e1206350fceab88a671b69 100644 (file)
@@ -405,7 +405,8 @@ static bool reg_not_null(const struct bpf_reg_state *reg)
                type == PTR_TO_MAP_KEY ||
                type == PTR_TO_SOCK_COMMON ||
                (type == PTR_TO_BTF_ID && is_trusted_reg(reg)) ||
-               type == PTR_TO_MEM;
+               type == PTR_TO_MEM ||
+               type == CONST_PTR_TO_MAP;
 }
 
 static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg)
index a4a5e2071604047cd4711da0497f1fd1772f9c4f..28200f068ce53c28c13bdd5d30673c8c74e255d6 100644 (file)
@@ -619,7 +619,7 @@ __naked void pass_pointer_to_tail_call(void)
 
 SEC("socket")
 __description("unpriv: cmp map pointer with zero")
-__success __failure_unpriv __msg_unpriv("R1 pointer comparison")
+__success __success_unpriv
 __retval(0)
 __naked void cmp_map_pointer_with_zero(void)
 {