Skip to content

Commit a315028

Browse files
pchaignoKernel Patches Daemon
authored andcommitted
selftest/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 <[email protected]> Acked-by: Eduard Zingerman <[email protected]>
1 parent b337031 commit a315028

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,28 @@ 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+
265289
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)