Skip to content

Commit e28bd35

Browse files
andreimateianakryiko
authored andcommitted
bpf: Add verifier regression test for previous patch
Add a regression test for var-off zero-sized reads. Signed-off-by: Andrei Matei <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent a833a17 commit e28bd35

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,35 @@ __naked void access_max_out_of_bound(void)
224224
: __clobber_all);
225225
}
226226

227+
/* Similar to the test above, but this time check the special case of a
228+
* zero-sized stack access. We used to have a bug causing crashes for zero-sized
229+
* out-of-bounds accesses.
230+
*/
231+
SEC("socket")
232+
__description("indirect variable-offset stack access, zero-sized, max out of bound")
233+
__failure __msg("invalid variable-offset indirect access to stack R1")
234+
__naked void zero_sized_access_max_out_of_bound(void)
235+
{
236+
asm volatile (" \
237+
r0 = 0; \
238+
/* Fill some stack */ \
239+
*(u64*)(r10 - 16) = r0; \
240+
*(u64*)(r10 - 8) = r0; \
241+
/* Get an unknown value */ \
242+
r1 = *(u32*)(r1 + 0); \
243+
r1 &= 63; \
244+
r1 += -16; \
245+
/* r1 is now anywhere in [-16,48) */ \
246+
r1 += r10; \
247+
r2 = 0; \
248+
r3 = 0; \
249+
call %[bpf_probe_read_kernel]; \
250+
exit; \
251+
" :
252+
: __imm(bpf_probe_read_kernel)
253+
: __clobber_all);
254+
}
255+
227256
SEC("lwt_in")
228257
__description("indirect variable-offset stack access, min out of bound")
229258
__failure __msg("invalid variable-offset indirect access to stack R2")

0 commit comments

Comments
 (0)