Commit eb6c9927 authored by Ihor Solodrai's avatar Ihor Solodrai Committed by Andrii Nakryiko
Browse files

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: default avatarIhor Solodrai <isolodrai@meta.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250609183024.359974-3-isolodrai@meta.com
parent 5534e58f
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -624,7 +624,6 @@ __retval(0)
__naked void cmp_map_pointer_with_zero(void)
{
	asm volatile ("					\
	r1 = 0;						\
	r1 = %[map_hash_8b] ll;				\
	if r1 == 0 goto l0_%=;				\
l0_%=:	r0 = 0;						\
@@ -634,6 +633,22 @@ l0_%=: r0 = 0; \
	: __clobber_all);
}

SEC("socket")
__description("unpriv: cmp map pointer with const")
__success __failure_unpriv __msg_unpriv("R1 pointer comparison prohibited")
__retval(0)
__naked void cmp_map_pointer_with_const(void)
{
	asm volatile ("					\
	r1 = %[map_hash_8b] ll;				\
	if r1 == 0x0000beef goto l0_%=;			\
l0_%=:	r0 = 0;						\
	exit;						\
"	:
	: __imm_addr(map_hash_8b)
	: __clobber_all);
}

SEC("socket")
__description("unpriv: write into frame pointer")
__failure __msg("frame pointer is read only")