Skip to content

Commit f81fdfd

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: test refining u32/s32 bounds when ranges cross min/max boundary
Two test cases for signed/unsigned 32-bit bounds refinement when s32 range crosses the sign boundary: - s32 range [S32_MIN..1] overlapping with u32 range [3..U32_MAX], s32 range tail before sign boundary overlaps with u32 range. - s32 range [-3..5] overlapping with u32 range [0..S32_MIN+3], s32 range head after the sign boundary overlaps with u32 range. This covers both branches added in the __reg32_deduce_bounds(). Also, crossing_32_bit_signed_boundary_2() no longer triggers invariant violations. Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com> Reviewed-by: Paul Chaignon <paul.chaignon@gmail.com> Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20260306-bpf-32-bit-range-overflow-v3-2-f7f67e060a6b@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent fbc7aef commit f81fdfd

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ l0_%=: r0 = 0; \
11481148
SEC("xdp")
11491149
__description("bound check with JMP32_JSLT for crossing 32-bit signed boundary")
11501150
__success __retval(0)
1151-
__flag(!BPF_F_TEST_REG_INVARIANTS) /* known invariants violation */
1151+
__flag(BPF_F_TEST_REG_INVARIANTS)
11521152
__naked void crossing_32_bit_signed_boundary_2(void)
11531153
{
11541154
asm volatile (" \
@@ -2000,4 +2000,41 @@ __naked void bounds_refinement_multiple_overlaps(void *ctx)
20002000
: __clobber_all);
20012001
}
20022002

2003+
SEC("socket")
2004+
__success
2005+
__flag(BPF_F_TEST_REG_INVARIANTS)
2006+
__naked void signed_unsigned_intersection32_case1(void *ctx)
2007+
{
2008+
asm volatile(" \
2009+
call %[bpf_get_prandom_u32]; \
2010+
w0 &= 0xffffffff; \
2011+
if w0 < 0x3 goto 1f; /* on fall-through u32 range [3..U32_MAX] */ \
2012+
if w0 s> 0x1 goto 1f; /* on fall-through s32 range [S32_MIN..1] */ \
2013+
if w0 s< 0x0 goto 1f; /* range can be narrowed to [S32_MIN..-1] */ \
2014+
r10 = 0; /* thus predicting the jump. */ \
2015+
1: exit; \
2016+
" :
2017+
: __imm(bpf_get_prandom_u32)
2018+
: __clobber_all);
2019+
}
2020+
2021+
SEC("socket")
2022+
__success
2023+
__flag(BPF_F_TEST_REG_INVARIANTS)
2024+
__naked void signed_unsigned_intersection32_case2(void *ctx)
2025+
{
2026+
asm volatile(" \
2027+
call %[bpf_get_prandom_u32]; \
2028+
w0 &= 0xffffffff; \
2029+
if w0 > 0x80000003 goto 1f; /* on fall-through u32 range [0..S32_MIN+3] */ \
2030+
if w0 s< -3 goto 1f; /* on fall-through s32 range [-3..S32_MAX] */ \
2031+
if w0 s> 5 goto 1f; /* on fall-through s32 range [-3..5] */ \
2032+
if w0 <= 5 goto 1f; /* range can be narrowed to [0..5] */ \
2033+
r10 = 0; /* thus predicting the jump */ \
2034+
1: exit; \
2035+
" :
2036+
: __imm(bpf_get_prandom_u32)
2037+
: __clobber_all);
2038+
}
2039+
20032040
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)