Skip to content

Commit 8db6daa

Browse files
pchaignoKernel Patches Daemon
authored andcommitted
selftests/bpf: Test invalid narrower ctx load
This patch adds two selftests to cover invalid narrower loads on the context. These used to cause kernel warning before the previous patch. To trigger the warning, the load had to be aligned, to read an affected pointer field (ex., skb->sk), and not starting at the beginning of the pointer field. The new selftests show two such loads of 1B and 4B sizes. Signed-off-by: Paul Chaignon <[email protected]>
1 parent 88789da commit 8db6daa

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,31 @@ __naked void null_check_8_null_bind(void)
218218
: __clobber_all);
219219
}
220220

221+
SEC("tc")
222+
__description("invalid narrow skb->sk load")
223+
__failure __msg("invalid bpf_context access")
224+
__naked void invalid_narrow_skb_sk_load(void)
225+
{
226+
asm volatile (" \
227+
r0 = *(u8 *)(r1 + %[__sk_buff_sk]); \
228+
exit; \
229+
" :
230+
: __imm_const(__sk_buff_sk, offsetof(struct __sk_buff, sk) + 1)
231+
: __clobber_all);
232+
}
233+
234+
SEC("sockops")
235+
__description("invalid narrow skops->sk_data load")
236+
__failure __msg("invalid bpf_context access")
237+
__naked void invalid_narrow_skops_sk_data_load(void)
238+
{
239+
asm volatile (" \
240+
r1 = *(u32 *)(r1 + %[sk_data]); \
241+
r0 = 0; \
242+
exit; \
243+
" :
244+
: __imm_const(sk_data, offsetof(struct bpf_sock_ops, skb_data) + 4)
245+
: __clobber_all);
246+
}
247+
221248
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)