|
1 | 1 | // SPDX-License-Identifier: GPL-2.0
|
2 | 2 | /* Converted from tools/testing/selftests/bpf/verifier/ctx.c */
|
3 | 3 |
|
4 |
| -#include <linux/bpf.h> |
| 4 | +#include "vmlinux.h" |
5 | 5 | #include <bpf/bpf_helpers.h>
|
6 | 6 | #include "bpf_misc.h"
|
7 | 7 |
|
| 8 | +#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) |
| 9 | + |
8 | 10 | SEC("tc")
|
9 | 11 | __description("context stores via BPF_ATOMIC")
|
10 | 12 | __failure __msg("BPF_ATOMIC stores into R1 ctx is not allowed")
|
@@ -243,4 +245,23 @@ narrow_load("sockops", bpf_sock_ops, skb_data);
|
243 | 245 | narrow_load("sockops", bpf_sock_ops, skb_data_end);
|
244 | 246 | narrow_load("sockops", bpf_sock_ops, skb_hwtstamp);
|
245 | 247 |
|
| 248 | +#define unaligned_access(type, ctx, field) \ |
| 249 | + SEC(type) \ |
| 250 | + __description("unaligned access on field " #field " of " #ctx) \ |
| 251 | + __failure __msg("invalid bpf_context access") \ |
| 252 | + __naked void unaligned_ctx_access_##ctx##field(void) \ |
| 253 | + { \ |
| 254 | + asm volatile (" \ |
| 255 | + r1 = *(u%[size] *)(r1 + %[off]); \ |
| 256 | + r0 = 0; \ |
| 257 | + exit;" \ |
| 258 | + : \ |
| 259 | + : __imm_const(size, sizeof_field(struct ctx, field) * 8), \ |
| 260 | + __imm_const(off, offsetof(struct ctx, field) + 1) \ |
| 261 | + : __clobber_all); \ |
| 262 | + } |
| 263 | + |
| 264 | +unaligned_access("flow_dissector", __sk_buff, data); |
| 265 | +unaligned_access("netfilter", bpf_nf_ctx, skb); |
| 266 | + |
246 | 267 | char _license[] SEC("license") = "GPL";
|
0 commit comments