]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/bpf: Add test for BPF_NEG alu on CONST_PTR_TO_MAP
authorKaFai Wan <kafai.wan@linux.dev>
Wed, 1 Oct 2025 19:17:39 +0000 (00:47 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 1 Oct 2025 20:55:00 +0000 (13:55 -0700)
Add a test case for BPF_NEG operation on CONST_PTR_TO_MAP. Tests if
BPF_NEG operation on map_ptr is rejected in unprivileged mode and is a
scalar value and do not trigger Oops in privileged mode.

Signed-off-by: KaFai Wan <kafai.wan@linux.dev>
Signed-off-by: Brahmajit Das <listout@listout.xyz>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20251001191739.2323644-3-listout@listout.xyz
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c

index a9ab37d3b9e2df323d702f44705b1117bc443780..dcaab61a11a09574b12e2792fc35e4913d3ba851 100644 (file)
@@ -146,6 +146,24 @@ l0_%=:     exit;                                           \
        : __clobber_all);
 }
 
+SEC("socket")
+__description("map_ptr illegal alu op, map_ptr = -map_ptr")
+__failure __msg("R0 invalid mem access 'scalar'")
+__failure_unpriv __msg_unpriv("R0 pointer arithmetic prohibited")
+__flag(BPF_F_ANY_ALIGNMENT)
+__naked void map_ptr_illegal_alu_op(void)
+{
+       asm volatile ("                                 \
+       r0 = %[map_hash_48b] ll;                        \
+       r0 = -r0;                                       \
+       r1 = 22;                                        \
+       *(u64*)(r0 + 0) = r1;                           \
+       exit;                                           \
+"      :
+       : __imm_addr(map_hash_48b)
+       : __clobber_all);
+}
+
 SEC("flow_dissector")
 __description("flow_keys illegal alu op with variable offset")
 __failure __msg("R7 pointer arithmetic on flow_keys prohibited")