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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// SPDX-License-Identifier: GPL-2.0
/* Converted from tools/testing/selftests/bpf/verifier/ld_ind.c */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "../../../include/linux/filter.h"
#include "bpf_misc.h"
SEC("socket")
__description("ld_ind: check calling conv, r1")
__failure __msg("R1 !read_ok")
__failure_unpriv
__naked void ind_check_calling_conv_r1(void)
{
asm volatile (" \
r6 = r1; \
r1 = 1; \
.8byte %[ld_ind]; \
r0 = r1; \
exit; \
" :
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_1, -0x200000))
: __clobber_all);
}
SEC("socket")
__description("ld_ind: check calling conv, r2")
__failure __msg("R2 !read_ok")
__failure_unpriv
__naked void ind_check_calling_conv_r2(void)
{
asm volatile (" \
r6 = r1; \
r2 = 1; \
.8byte %[ld_ind]; \
r0 = r2; \
exit; \
" :
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_2, -0x200000))
: __clobber_all);
}
SEC("socket")
__description("ld_ind: check calling conv, r3")
__failure __msg("R3 !read_ok")
__failure_unpriv
__naked void ind_check_calling_conv_r3(void)
{
asm volatile (" \
r6 = r1; \
r3 = 1; \
.8byte %[ld_ind]; \
r0 = r3; \
exit; \
" :
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_3, -0x200000))
: __clobber_all);
}
SEC("socket")
__description("ld_ind: check calling conv, r4")
__failure __msg("R4 !read_ok")
__failure_unpriv
__naked void ind_check_calling_conv_r4(void)
{
asm volatile (" \
r6 = r1; \
r4 = 1; \
.8byte %[ld_ind]; \
r0 = r4; \
exit; \
" :
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_4, -0x200000))
: __clobber_all);
}
SEC("socket")
__description("ld_ind: check calling conv, r5")
__failure __msg("R5 !read_ok")
__failure_unpriv
__naked void ind_check_calling_conv_r5(void)
{
asm volatile (" \
r6 = r1; \
r5 = 1; \
.8byte %[ld_ind]; \
r0 = r5; \
exit; \
" :
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_5, -0x200000))
: __clobber_all);
}
SEC("socket")
__description("ld_ind: check calling conv, r7")
__success __success_unpriv __retval(1)
__naked void ind_check_calling_conv_r7(void)
{
asm volatile (" \
r6 = r1; \
r7 = 1; \
.8byte %[ld_ind]; \
r0 = r7; \
exit; \
" :
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_7, -0x200000))
: __clobber_all);
}
/*
* ld_{abs,ind} subprog that always sets r0=1 on the success path.
* bpf_gen_ld_abs() emits a hidden exit with r0=0 when the load helper
* fails. The verifier must model this failure return so that callers
* account for r0=0 as a possible return value.
*/
__naked __noinline __used
static int ldabs_subprog(void)
{
asm volatile (
"r6 = r1;"
".8byte %[ld_abs];"
"r0 = 1;"
"exit;"
:
: __imm_insn(ld_abs, BPF_LD_ABS(BPF_W, 0))
: __clobber_all);
}
__naked __noinline __used
static int ldind_subprog(void)
{
asm volatile (
"r6 = r1;"
"r7 = 0;"
".8byte %[ld_ind];"
"r0 = 1;"
"exit;"
:
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_7, 0))
: __clobber_all);
}
SEC("socket")
__description("ld_abs: subprog early exit on ld_abs failure")
__failure __msg("R9 !read_ok")
__naked void ld_abs_subprog_early_exit(void)
{
asm volatile (
"call ldabs_subprog;"
"if r0 != 0 goto l_exit_%=;"
"r0 = r9;"
"l_exit_%=:"
"r0 = 0;"
"exit;"
::: __clobber_all);
}
SEC("socket")
__description("ld_ind: subprog early exit on ld_ind failure")
__failure __msg("R9 !read_ok")
__naked void ld_ind_subprog_early_exit(void)
{
asm volatile (
"call ldind_subprog;"
"if r0 != 0 goto l_exit_%=;"
"r0 = r9;"
"l_exit_%=:"
"r0 = 0;"
"exit;"
::: __clobber_all);
}
SEC("socket")
__description("ld_abs: subprog with both paths safe")
__success
__naked void ld_abs_subprog_both_paths_safe(void)
{
asm volatile (
"call ldabs_subprog;"
"r0 = 0;"
"exit;"
::: __clobber_all);
}
SEC("socket")
__description("ld_ind: subprog with both paths safe")
__success
__naked void ld_ind_subprog_both_paths_safe(void)
{
asm volatile (
"call ldind_subprog;"
"r0 = 0;"
"exit;"
::: __clobber_all);
}
__naked __noinline __used
static int ld_abs_callback(void)
{
asm volatile (
"r6 = *(u64 *)(r2 + 0);"
".8byte %[ld_abs];"
"r0 = 0;"
"exit;"
:
: __imm_insn(ld_abs, BPF_LD_ABS(BPF_W, 0))
: __clobber_all);
}
SEC("socket")
__description("ld_abs: reject in callback")
__failure __msg("cannot use BPF_LD_[ABS|IND] within callback")
int ld_abs_callback_reject(struct __sk_buff *skb)
{
bpf_loop(1, ld_abs_callback, &skb, 0);
return 0;
}
__naked __noinline __used
static int ld_ind_callback_subprog(void)
{
asm volatile (
"r6 = r1;"
"r7 = 0;"
".8byte %[ld_ind];"
"r0 = 0;"
"exit;"
:
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_7, 0))
: __clobber_all);
}
__naked __noinline __used
static int ld_ind_callback(void)
{
asm volatile (
"r1 = *(u64 *)(r2 + 0);"
"call ld_ind_callback_subprog;"
"exit;"
::: __clobber_all);
}
SEC("socket")
__description("ld_ind: reject in callback subprog")
__failure __msg("cannot use BPF_LD_[ABS|IND] within callback")
int ld_ind_callback_subprog_reject(struct __sk_buff *skb)
{
bpf_loop(1, ld_ind_callback, &skb, 0);
return 0;
}
static __noinline int ld_ind_global_static(struct __sk_buff *skb)
{
asm volatile (
"r6 = %[skb];"
"r7 = 0;"
".8byte %[ld_ind];"
:
: [skb] "r"(skb),
__imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_7, 0))
: __clobber_common, "r6", "r7");
return skb->mark;
}
__noinline int ld_ind_global(struct __sk_buff *skb)
{
return ld_ind_global_static(skb);
}
static int ld_ind_global_callback(__u32 index, struct __sk_buff **ctx)
{
ld_ind_global(*ctx);
return 0;
}
SEC("socket")
__description("ld_ind: reject in callback global subprog")
__failure __msg("cannot use BPF_LD_[ABS|IND] within callback")
int ld_ind_global_callback_reject(struct __sk_buff *skb)
{
bpf_loop(1, ld_ind_global_callback, &skb, 0);
return 0;
}
SEC("socket")
__description("ld_ind: allow in non-callback global subprog")
__success
int ld_ind_global_subprog_ok(struct __sk_buff *skb)
{
return ld_ind_global(skb);
}
/*
* ld_{abs,ind} in subprogs require scalar (int) return type in BTF.
* A test with void return must be rejected.
*/
__naked __noinline __used
static void ldabs_void_subprog(void)
{
asm volatile (
"r6 = r1;"
".8byte %[ld_abs];"
"r0 = 1;"
"exit;"
:
: __imm_insn(ld_abs, BPF_LD_ABS(BPF_W, 0))
: __clobber_all);
}
SEC("socket")
__description("ld_abs: reject void return subprog")
__failure __msg("LD_ABS is only allowed in functions that return 'int'")
__naked void ld_abs_void_subprog_reject(void)
{
asm volatile (
"call ldabs_void_subprog;"
"r0 = 0;"
"exit;"
::: __clobber_all);
}
__naked __noinline __used
static void ldind_void_subprog(void)
{
asm volatile (
"r6 = r1;"
"r7 = 0;"
".8byte %[ld_ind];"
"r0 = 1;"
"exit;"
:
: __imm_insn(ld_ind, BPF_LD_IND(BPF_W, BPF_REG_7, 0))
: __clobber_all);
}
SEC("socket")
__description("ld_ind: reject void return subprog")
__failure __msg("LD_ABS is only allowed in functions that return 'int'")
__naked void ld_ind_void_subprog_reject(void)
{
asm volatile (
"call ldind_void_subprog;"
"r0 = 0;"
"exit;"
::: __clobber_all);
}
char _license[] SEC("license") = "GPL";
|