xref: /linux/tools/testing/selftests/bpf/verifier/wide_access.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 #define BPF_SOCK_ADDR_STORE(field, off, res, err, flgs)	\
2 { \
3 	"wide store to bpf_sock_addr." #field "[" #off "]", \
4 	.insns = { \
5 	BPF_MOV64_IMM(BPF_REG_0, 1), \
6 	BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \
7 		    offsetof(struct bpf_sock_addr, field[off])), \
8 	BPF_EXIT_INSN(), \
9 	}, \
10 	.result = res, \
11 	.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
12 	.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
13 	.errstr = err, \
14 	.flags = flgs, \
15 }
16 
17 /* user_ip6[0] is u64 aligned */
18 BPF_SOCK_ADDR_STORE(user_ip6, 0, ACCEPT,
19 		    NULL, 0),
20 BPF_SOCK_ADDR_STORE(user_ip6, 1, REJECT,
21 		    "invalid bpf_context access off=12 size=8",
22 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
23 BPF_SOCK_ADDR_STORE(user_ip6, 2, ACCEPT,
24 		    NULL, 0),
25 BPF_SOCK_ADDR_STORE(user_ip6, 3, REJECT,
26 		    "invalid bpf_context access off=20 size=8",
27 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
28 
29 /* msg_src_ip6[0] is _not_ u64 aligned */
30 BPF_SOCK_ADDR_STORE(msg_src_ip6, 0, REJECT,
31 		    "invalid bpf_context access off=44 size=8",
32 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
33 BPF_SOCK_ADDR_STORE(msg_src_ip6, 1, ACCEPT,
34 		    NULL, 0),
35 BPF_SOCK_ADDR_STORE(msg_src_ip6, 2, REJECT,
36 		    "invalid bpf_context access off=52 size=8",
37 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
38 BPF_SOCK_ADDR_STORE(msg_src_ip6, 3, REJECT,
39 		    "invalid bpf_context access off=56 size=8", 0),
40 
41 #undef BPF_SOCK_ADDR_STORE
42 
43 #define BPF_SOCK_ADDR_LOAD(field, off, res, err, flgs)	\
44 { \
45 	"wide load from bpf_sock_addr." #field "[" #off "]", \
46 	.insns = { \
47 	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, \
48 		    offsetof(struct bpf_sock_addr, field[off])), \
49 	BPF_MOV64_IMM(BPF_REG_0, 1), \
50 	BPF_EXIT_INSN(), \
51 	}, \
52 	.result = res, \
53 	.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
54 	.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
55 	.errstr = err, \
56 	.flags = flgs, \
57 }
58 
59 /* user_ip6[0] is u64 aligned */
60 BPF_SOCK_ADDR_LOAD(user_ip6, 0, ACCEPT,
61 		   NULL, 0),
62 BPF_SOCK_ADDR_LOAD(user_ip6, 1, REJECT,
63 		   "invalid bpf_context access off=12 size=8",
64 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
65 BPF_SOCK_ADDR_LOAD(user_ip6, 2, ACCEPT,
66 		   NULL, 0),
67 BPF_SOCK_ADDR_LOAD(user_ip6, 3, REJECT,
68 		   "invalid bpf_context access off=20 size=8",
69 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
70 
71 /* msg_src_ip6[0] is _not_ u64 aligned */
72 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 0, REJECT,
73 		   "invalid bpf_context access off=44 size=8",
74 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
75 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 1, ACCEPT,
76 		   NULL, 0),
77 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 2, REJECT,
78 		   "invalid bpf_context access off=52 size=8",
79 		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
80 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 3, REJECT,
81 		   "invalid bpf_context access off=56 size=8", 0),
82 
83 #undef BPF_SOCK_ADDR_LOAD
84