Skip to content

Commit a3f1a00

Browse files
dkanalievKernel Patches Daemon
authored andcommitted
selftests/bpf: Add verifier bounds checks for sign extension
This patch adds a new test cases to validate the improved register bounds tracking logic. We perform the sequence: call bpf_get_prandom_u32; r1 &= 0x100; r1 = (s8)r1; After the bitwise AND, `r1` is either 0 or 256 (0x100). If 0: The lower 8 bits are 0. If 256: The bit at index 8 is set, but the lower 8 bits are 0. Since the cast to s8 only considers bits 0-7, the set bit at index 8 is truncated. In both cases, the sign bit (bit 7) is 0, so the result is exactly 0. With the coercion logic before this series: 1: (bf) r1 = r0 ; R0=scalar(id=1) R1=scalar(id=1) 2: (57) r1 &= 256 ; R1=scalar(...,var_off=(0x0; 0x100)) 3: (bf) r1 = (s8)r1 ; R1=scalar(smin=smin32=-128,smax=smax32=127) With our changes: 1: (bf) r1 = r0 ; R0=scalar(id=1) R1=scalar(id=1) 2: (57) r1 &= 256 ; R1=scalar(...,var_off=(0x0; 0x100)) 3: (bf) r1 = (s8)r1 ; R1=0 Signed-off-by: Dimitar Kanaliev <[email protected]>
1 parent 3160cb7 commit a3f1a00

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,25 @@ label_%=: \
339339
: __clobber_all);
340340
}
341341

342+
SEC("socket")
343+
__description("MOV64SX, S8, upper bits truncation")
344+
__log_level(2)
345+
__msg("R1={{P?}}0")
346+
__success __success_unpriv __retval(0)
347+
__naked void mov64sx_s8_truncated_range(void)
348+
{
349+
asm volatile (" \
350+
call %[bpf_get_prandom_u32]; \
351+
r1 = r0; \
352+
r1 &= 0x100; \
353+
r1 = (s8)r1; \
354+
r0 = 0; \
355+
exit; \
356+
" :
357+
: __imm(bpf_get_prandom_u32)
358+
: __clobber_all);
359+
}
360+
342361
#else
343362

344363
SEC("socket")

0 commit comments

Comments
 (0)