Skip to content

Commit ba578b8

Browse files
pchaignoMartin KaFai Lau
authored andcommitted
selftests/bpf: Test invalid narrower ctx load
This patch adds selftests to cover invalid narrower loads on the context. These used to cause kernel warnings before the previous patch. To trigger the warning, the load had to be aligned, to read an affected context field (ex., skb->sk), and not starting at the beginning of the field. The nine new cases all fail without the previous patch. Suggested-by: Eduard Zingerman <[email protected]> Signed-off-by: Paul Chaignon <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://patch.msgid.link/44cd83ea9c6868079943f0a436c6efa850528cc1.1753194596.git.paul.chaignon@gmail.com
1 parent e092992 commit ba578b8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

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

221+
#define narrow_load(type, ctx, field) \
222+
SEC(type) \
223+
__description("narrow load on field " #field " of " #ctx) \
224+
__failure __msg("invalid bpf_context access") \
225+
__naked void invalid_narrow_load##ctx##field(void) \
226+
{ \
227+
asm volatile (" \
228+
r1 = *(u32 *)(r1 + %[off]); \
229+
r0 = 0; \
230+
exit;" \
231+
: \
232+
: __imm_const(off, offsetof(struct ctx, field) + 4) \
233+
: __clobber_all); \
234+
}
235+
236+
narrow_load("cgroup/getsockopt", bpf_sockopt, sk);
237+
narrow_load("cgroup/getsockopt", bpf_sockopt, optval);
238+
narrow_load("cgroup/getsockopt", bpf_sockopt, optval_end);
239+
narrow_load("tc", __sk_buff, sk);
240+
narrow_load("cgroup/bind4", bpf_sock_addr, sk);
241+
narrow_load("sockops", bpf_sock_ops, sk);
242+
narrow_load("sockops", bpf_sock_ops, skb_data);
243+
narrow_load("sockops", bpf_sock_ops, skb_data_end);
244+
narrow_load("sockops", bpf_sock_ops, skb_hwtstamp);
245+
221246
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)