Skip to content

Commit 7877816

Browse files
eddyz87gregkh
authored andcommitted
selftests/bpf: test refining u32/s32 bounds when ranges cross min/max boundary
[ Upstream commit f81fdfd ] 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> Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ded1ea2 commit 7877816

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
@@ -1110,7 +1110,7 @@ l0_%=: r0 = 0; \
11101110
SEC("xdp")
11111111
__description("bound check with JMP32_JSLT for crossing 32-bit signed boundary")
11121112
__success __retval(0)
1113-
__flag(!BPF_F_TEST_REG_INVARIANTS) /* known invariants violation */
1113+
__flag(BPF_F_TEST_REG_INVARIANTS)
11141114
__naked void crossing_32_bit_signed_boundary_2(void)
11151115
{
11161116
asm volatile (" \
@@ -1318,4 +1318,41 @@ l0_%=: r0 = 0; \
13181318
: __clobber_all);
13191319
}
13201320

1321+
SEC("socket")
1322+
__success
1323+
__flag(BPF_F_TEST_REG_INVARIANTS)
1324+
__naked void signed_unsigned_intersection32_case1(void *ctx)
1325+
{
1326+
asm volatile(" \
1327+
call %[bpf_get_prandom_u32]; \
1328+
w0 &= 0xffffffff; \
1329+
if w0 < 0x3 goto 1f; /* on fall-through u32 range [3..U32_MAX] */ \
1330+
if w0 s> 0x1 goto 1f; /* on fall-through s32 range [S32_MIN..1] */ \
1331+
if w0 s< 0x0 goto 1f; /* range can be narrowed to [S32_MIN..-1] */ \
1332+
r10 = 0; /* thus predicting the jump. */ \
1333+
1: exit; \
1334+
" :
1335+
: __imm(bpf_get_prandom_u32)
1336+
: __clobber_all);
1337+
}
1338+
1339+
SEC("socket")
1340+
__success
1341+
__flag(BPF_F_TEST_REG_INVARIANTS)
1342+
__naked void signed_unsigned_intersection32_case2(void *ctx)
1343+
{
1344+
asm volatile(" \
1345+
call %[bpf_get_prandom_u32]; \
1346+
w0 &= 0xffffffff; \
1347+
if w0 > 0x80000003 goto 1f; /* on fall-through u32 range [0..S32_MIN+3] */ \
1348+
if w0 s< -3 goto 1f; /* on fall-through s32 range [-3..S32_MAX] */ \
1349+
if w0 s> 5 goto 1f; /* on fall-through s32 range [-3..5] */ \
1350+
if w0 <= 5 goto 1f; /* range can be narrowed to [0..5] */ \
1351+
r10 = 0; /* thus predicting the jump */ \
1352+
1: exit; \
1353+
" :
1354+
: __imm(bpf_get_prandom_u32)
1355+
: __clobber_all);
1356+
}
1357+
13211358
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)