Skip to content

Commit 8709c16

Browse files
mannkafaiAlexei Starovoitov
authored andcommitted
selftests/bpf: Add test for BPF_NEG alu on CONST_PTR_TO_MAP
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>
1 parent 3490458 commit 8709c16

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ l0_%=: exit; \
146146
: __clobber_all);
147147
}
148148

149+
SEC("socket")
150+
__description("map_ptr illegal alu op, map_ptr = -map_ptr")
151+
__failure __msg("R0 invalid mem access 'scalar'")
152+
__failure_unpriv __msg_unpriv("R0 pointer arithmetic prohibited")
153+
__flag(BPF_F_ANY_ALIGNMENT)
154+
__naked void map_ptr_illegal_alu_op(void)
155+
{
156+
asm volatile (" \
157+
r0 = %[map_hash_48b] ll; \
158+
r0 = -r0; \
159+
r1 = 22; \
160+
*(u64*)(r0 + 0) = r1; \
161+
exit; \
162+
" :
163+
: __imm_addr(map_hash_48b)
164+
: __clobber_all);
165+
}
166+
149167
SEC("flow_dissector")
150168
__description("flow_keys illegal alu op with variable offset")
151169
__failure __msg("R7 pointer arithmetic on flow_keys prohibited")

0 commit comments

Comments
 (0)