Skip to content

Commit 180a46b

Browse files
pchaignoborkmann
authored andcommitted
selftests/bpf: Test accesses to ctx padding
This patch adds tests covering the various paddings in ctx structures. In case of sk_lookup BPF programs, the behavior is a bit different because accesses to the padding are explicitly allowed. Other cases result in a clear reject from the verifier. Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/3dc5f025e350aeb2bb1c257b87c577518e574aeb.1758094761.git.paul.chaignon@gmail.com
1 parent 7c60f6e commit 180a46b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,34 @@ narrow_load("sockops", bpf_sock_ops, skb_hwtstamp);
262262
unaligned_access("flow_dissector", __sk_buff, data);
263263
unaligned_access("netfilter", bpf_nf_ctx, skb);
264264

265+
#define padding_access(type, ctx, prev_field, sz) \
266+
SEC(type) \
267+
__description("access on " #ctx " padding after " #prev_field) \
268+
__naked void padding_ctx_access_##ctx(void) \
269+
{ \
270+
asm volatile (" \
271+
r1 = *(u%[size] *)(r1 + %[off]); \
272+
r0 = 0; \
273+
exit;" \
274+
: \
275+
: __imm_const(size, sz * 8), \
276+
__imm_const(off, offsetofend(struct ctx, prev_field)) \
277+
: __clobber_all); \
278+
}
279+
280+
__failure __msg("invalid bpf_context access")
281+
padding_access("cgroup/bind4", bpf_sock_addr, msg_src_ip6[3], 4);
282+
283+
__success
284+
padding_access("sk_lookup", bpf_sk_lookup, remote_port, 2);
285+
286+
__failure __msg("invalid bpf_context access")
287+
padding_access("tc", __sk_buff, tstamp_type, 2);
288+
289+
__failure __msg("invalid bpf_context access")
290+
padding_access("cgroup/post_bind4", bpf_sock, dst_port, 2);
291+
292+
__failure __msg("invalid bpf_context access")
293+
padding_access("sk_reuseport", sk_reuseport_md, hash, 4);
294+
265295
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)