Skip to content

Commit eb6c992

Browse files
theihoranakryiko
authored andcommitted
selftests/bpf: Add cmp_map_pointer_with_const test
Add a test for CONST_PTR_TO_MAP comparison with a non-0 constant. A BPF program with this code must not pass verification in unpriv. Signed-off-by: Ihor Solodrai <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5534e58 commit eb6c992

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ __retval(0)
624624
__naked void cmp_map_pointer_with_zero(void)
625625
{
626626
asm volatile (" \
627-
r1 = 0; \
628627
r1 = %[map_hash_8b] ll; \
629628
if r1 == 0 goto l0_%=; \
630629
l0_%=: r0 = 0; \
@@ -634,6 +633,22 @@ l0_%=: r0 = 0; \
634633
: __clobber_all);
635634
}
636635

636+
SEC("socket")
637+
__description("unpriv: cmp map pointer with const")
638+
__success __failure_unpriv __msg_unpriv("R1 pointer comparison prohibited")
639+
__retval(0)
640+
__naked void cmp_map_pointer_with_const(void)
641+
{
642+
asm volatile (" \
643+
r1 = %[map_hash_8b] ll; \
644+
if r1 == 0x0000beef goto l0_%=; \
645+
l0_%=: r0 = 0; \
646+
exit; \
647+
" :
648+
: __imm_addr(map_hash_8b)
649+
: __clobber_all);
650+
}
651+
637652
SEC("socket")
638653
__description("unpriv: write into frame pointer")
639654
__failure __msg("frame pointer is read only")

0 commit comments

Comments
 (0)