summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/verifier_ctx_sk_msg.c
blob: 65edc89799f99dcc07fed1c4fefcf4a4a33a744a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// SPDX-License-Identifier: GPL-2.0
/* Converted from tools/testing/selftests/bpf/verifier/ctx_sk_msg.c */

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"

SEC("sk_msg")
__description("valid access family in SK_MSG")
__success
__naked void access_family_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_family]);		\
	exit;						\
"	:
	: __imm_const(sk_msg_md_family, offsetof(struct sk_msg_md, family))
	: __clobber_all);
}

SEC("sk_msg")
__description("valid access remote_ip4 in SK_MSG")
__success
__naked void remote_ip4_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_remote_ip4]);	\
	exit;						\
"	:
	: __imm_const(sk_msg_md_remote_ip4, offsetof(struct sk_msg_md, remote_ip4))
	: __clobber_all);
}

SEC("sk_msg")
__description("valid access local_ip4 in SK_MSG")
__success
__naked void local_ip4_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_local_ip4]);	\
	exit;						\
"	:
	: __imm_const(sk_msg_md_local_ip4, offsetof(struct sk_msg_md, local_ip4))
	: __clobber_all);
}

SEC("sk_msg")
__description("valid access remote_port in SK_MSG")
__success
__naked void remote_port_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_remote_port]);	\
	exit;						\
"	:
	: __imm_const(sk_msg_md_remote_port, offsetof(struct sk_msg_md, remote_port))
	: __clobber_all);
}

SEC("sk_msg")
__description("valid access local_port in SK_MSG")
__success
__naked void local_port_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_local_port]);	\
	exit;						\
"	:
	: __imm_const(sk_msg_md_local_port, offsetof(struct sk_msg_md, local_port))
	: __clobber_all);
}

SEC("sk_skb")
__description("valid access remote_ip6 in SK_MSG")
__success
__naked void remote_ip6_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_remote_ip6_0]);	\
	r0 = *(u32*)(r1 + %[sk_msg_md_remote_ip6_1]);	\
	r0 = *(u32*)(r1 + %[sk_msg_md_remote_ip6_2]);	\
	r0 = *(u32*)(r1 + %[sk_msg_md_remote_ip6_3]);	\
	exit;						\
"	:
	: __imm_const(sk_msg_md_remote_ip6_0, offsetof(struct sk_msg_md, remote_ip6[0])),
	  __imm_const(sk_msg_md_remote_ip6_1, offsetof(struct sk_msg_md, remote_ip6[1])),
	  __imm_const(sk_msg_md_remote_ip6_2, offsetof(struct sk_msg_md, remote_ip6[2])),
	  __imm_const(sk_msg_md_remote_ip6_3, offsetof(struct sk_msg_md, remote_ip6[3]))
	: __clobber_all);
}

SEC("sk_skb")
__description("valid access local_ip6 in SK_MSG")
__success
__naked void local_ip6_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_local_ip6_0]);	\
	r0 = *(u32*)(r1 + %[sk_msg_md_local_ip6_1]);	\
	r0 = *(u32*)(r1 + %[sk_msg_md_local_ip6_2]);	\
	r0 = *(u32*)(r1 + %[sk_msg_md_local_ip6_3]);	\
	exit;						\
"	:
	: __imm_const(sk_msg_md_local_ip6_0, offsetof(struct sk_msg_md, local_ip6[0])),
	  __imm_const(sk_msg_md_local_ip6_1, offsetof(struct sk_msg_md, local_ip6[1])),
	  __imm_const(sk_msg_md_local_ip6_2, offsetof(struct sk_msg_md, local_ip6[2])),
	  __imm_const(sk_msg_md_local_ip6_3, offsetof(struct sk_msg_md, local_ip6[3]))
	: __clobber_all);
}

SEC("sk_msg")
__description("valid access size in SK_MSG")
__success
__naked void access_size_in_sk_msg(void)
{
	asm volatile ("					\
	r0 = *(u32*)(r1 + %[sk_msg_md_size]);		\
	exit;						\
"	:
	: __imm_const(sk_msg_md_size, offsetof(struct sk_msg_md, size))
	: __clobber_all);
}

SEC("sk_msg")
__description("invalid 64B read of size in SK_MSG")
__failure __msg("invalid bpf_context access")
__flag(BPF_F_ANY_ALIGNMENT)
__naked void of_size_in_sk_msg(void)
{
	asm volatile ("					\
	r2 = *(u64*)(r1 + %[sk_msg_md_size]);		\
	exit;						\
"	:
	: __imm_const(sk_msg_md_size, offsetof(struct sk_msg_md, size))
	: __clobber_all);
}

SEC("sk_msg")
__description("invalid read past end of SK_MSG")
__failure __msg("invalid bpf_context access")
__naked void past_end_of_sk_msg(void)
{
	asm volatile ("					\
	r2 = *(u32*)(r1 + %[__imm_0]);			\
	exit;						\
"	:
	: __imm_const(__imm_0, offsetof(struct sk_msg_md, size) + 4)
	: __clobber_all);
}

SEC("sk_msg")
__description("invalid read offset in SK_MSG")
__failure __msg("invalid bpf_context access")
__flag(BPF_F_ANY_ALIGNMENT)
__naked void read_offset_in_sk_msg(void)
{
	asm volatile ("					\
	r2 = *(u32*)(r1 + %[__imm_0]);			\
	exit;						\
"	:
	: __imm_const(__imm_0, offsetof(struct sk_msg_md, family) + 1)
	: __clobber_all);
}

SEC("sk_msg")
__description("direct packet read for SK_MSG")
__success
__naked void packet_read_for_sk_msg(void)
{
	asm volatile ("					\
	r2 = *(u64*)(r1 + %[sk_msg_md_data]);		\
	r3 = *(u64*)(r1 + %[sk_msg_md_data_end]);	\
	r0 = r2;					\
	r0 += 8;					\
	if r0 > r3 goto l0_%=;				\
	r0 = *(u8*)(r2 + 0);				\
l0_%=:	r0 = 0;						\
	exit;						\
"	:
	: __imm_const(sk_msg_md_data, offsetof(struct sk_msg_md, data)),
	  __imm_const(sk_msg_md_data_end, offsetof(struct sk_msg_md, data_end))
	: __clobber_all);
}

SEC("sk_msg")
__description("direct packet write for SK_MSG")
__success
__naked void packet_write_for_sk_msg(void)
{
	asm volatile ("					\
	r2 = *(u64*)(r1 + %[sk_msg_md_data]);		\
	r3 = *(u64*)(r1 + %[sk_msg_md_data_end]);	\
	r0 = r2;					\
	r0 += 8;					\
	if r0 > r3 goto l0_%=;				\
	*(u8*)(r2 + 0) = r2;				\
l0_%=:	r0 = 0;						\
	exit;						\
"	:
	: __imm_const(sk_msg_md_data, offsetof(struct sk_msg_md, data)),
	  __imm_const(sk_msg_md_data_end, offsetof(struct sk_msg_md, data_end))
	: __clobber_all);
}

SEC("sk_msg")
__description("overlapping checks for direct packet access SK_MSG")
__success
__naked void direct_packet_access_sk_msg(void)
{
	asm volatile ("					\
	r2 = *(u64*)(r1 + %[sk_msg_md_data]);		\
	r3 = *(u64*)(r1 + %[sk_msg_md_data_end]);	\
	r0 = r2;					\
	r0 += 8;					\
	if r0 > r3 goto l0_%=;				\
	r1 = r2;					\
	r1 += 6;					\
	if r1 > r3 goto l0_%=;				\
	r0 = *(u16*)(r2 + 6);				\
l0_%=:	r0 = 0;						\
	exit;						\
"	:
	: __imm_const(sk_msg_md_data, offsetof(struct sk_msg_md, data)),
	  __imm_const(sk_msg_md_data_end, offsetof(struct sk_msg_md, data_end))
	: __clobber_all);
}

char _license[] SEC("license") = "GPL";