Skip to content

Commit 0ad1734

Browse files
Rat5akAlexei Starovoitov
authored andcommitted
selftests/bpf: Add tests for maybe_fork_scalars() OR vs AND handling
Add three test cases to verifier_bounds.c to verify that maybe_fork_scalars() correctly tracks register values for BPF_OR operations with constant source operands: 1. or_scalar_fork_rejects_oob: After ARSH 63 + OR 8, the pushed path should have dst = 8. With value_size = 8, accessing map_value + 8 is out of bounds and must be rejected. 2. and_scalar_fork_still_works: Regression test ensuring AND forking continues to work. ARSH 63 + AND 4 produces pushed dst = 0 and current dst = 4, both within value_size = 8. 3. or_scalar_fork_allows_inbounds: After ARSH 63 + OR 4, the pushed path has dst = 4, which is within value_size = 8 and should be accepted. These tests exercise the fix in the previous patch, which makes the pushed path re-execute the ALU instruction so it computes the correct result for BPF_OR. Signed-off-by: Daniel Wade <danjwade95@gmail.com> Reviewed-by: Amery Hung <ameryhung@gmail.com> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20260314021521.128361-3-danjwade95@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent c845894 commit 0ad1734

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,4 +2037,98 @@ __naked void signed_unsigned_intersection32_case2(void *ctx)
20372037
: __clobber_all);
20382038
}
20392039

2040+
SEC("socket")
2041+
__description("maybe_fork_scalars: OR with constant rejects OOB")
2042+
__failure __msg("invalid access to map value")
2043+
__naked void or_scalar_fork_rejects_oob(void)
2044+
{
2045+
asm volatile (" \
2046+
r1 = 0; \
2047+
*(u64*)(r10 - 8) = r1; \
2048+
r2 = r10; \
2049+
r2 += -8; \
2050+
r1 = %[map_hash_8b] ll; \
2051+
call %[bpf_map_lookup_elem]; \
2052+
if r0 == 0 goto l0_%=; \
2053+
r9 = r0; \
2054+
r6 = *(u64*)(r9 + 0); \
2055+
r6 s>>= 63; \
2056+
r6 |= 8; \
2057+
/* r6 is -1 (current) or 8 (pushed) */ \
2058+
if r6 s< 0 goto l0_%=; \
2059+
/* pushed path: r6 = 8, OOB for value_size=8 */ \
2060+
r9 += r6; \
2061+
r0 = *(u8*)(r9 + 0); \
2062+
l0_%=: r0 = 0; \
2063+
exit; \
2064+
" :
2065+
: __imm(bpf_map_lookup_elem),
2066+
__imm_addr(map_hash_8b)
2067+
: __clobber_all);
2068+
}
2069+
2070+
SEC("socket")
2071+
__description("maybe_fork_scalars: AND with constant still works")
2072+
__success __retval(0)
2073+
__naked void and_scalar_fork_still_works(void)
2074+
{
2075+
asm volatile (" \
2076+
r1 = 0; \
2077+
*(u64*)(r10 - 8) = r1; \
2078+
r2 = r10; \
2079+
r2 += -8; \
2080+
r1 = %[map_hash_8b] ll; \
2081+
call %[bpf_map_lookup_elem]; \
2082+
if r0 == 0 goto l0_%=; \
2083+
r9 = r0; \
2084+
r6 = *(u64*)(r9 + 0); \
2085+
r6 s>>= 63; \
2086+
r6 &= 4; \
2087+
/* \
2088+
* r6 is 0 (pushed, 0&4==0) or 4 (current) \
2089+
* both within value_size=8 \
2090+
*/ \
2091+
if r6 s< 0 goto l0_%=; \
2092+
r9 += r6; \
2093+
r0 = *(u8*)(r9 + 0); \
2094+
l0_%=: r0 = 0; \
2095+
exit; \
2096+
" :
2097+
: __imm(bpf_map_lookup_elem),
2098+
__imm_addr(map_hash_8b)
2099+
: __clobber_all);
2100+
}
2101+
2102+
SEC("socket")
2103+
__description("maybe_fork_scalars: OR with constant allows in-bounds")
2104+
__success __retval(0)
2105+
__naked void or_scalar_fork_allows_inbounds(void)
2106+
{
2107+
asm volatile (" \
2108+
r1 = 0; \
2109+
*(u64*)(r10 - 8) = r1; \
2110+
r2 = r10; \
2111+
r2 += -8; \
2112+
r1 = %[map_hash_8b] ll; \
2113+
call %[bpf_map_lookup_elem]; \
2114+
if r0 == 0 goto l0_%=; \
2115+
r9 = r0; \
2116+
r6 = *(u64*)(r9 + 0); \
2117+
r6 s>>= 63; \
2118+
r6 |= 4; \
2119+
/* \
2120+
* r6 is -1 (current) or 4 (pushed) \
2121+
* pushed path: r6 = 4, within value_size=8 \
2122+
*/ \
2123+
if r6 s< 0 goto l0_%=; \
2124+
r9 += r6; \
2125+
r0 = *(u8*)(r9 + 0); \
2126+
l0_%=: r0 = 0; \
2127+
exit; \
2128+
" :
2129+
: __imm(bpf_map_lookup_elem),
2130+
__imm_addr(map_hash_8b)
2131+
: __clobber_all);
2132+
}
2133+
20402134
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)