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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_experimental.h"
#include "bpf_misc.h"
/*
* Variable-offset access into arrays within a BTF access chain, e.g. a[i].b,
* where 'i' is a register with a varying offset. The register's step has to
* match the stride of one of the arrays crossed while walking the access
* chain, and the maximal possible offset has to stay within that array.
*
* A program-allocated object (bpf_obj_new) is used as the source of a
* PTR_TO_BTF_ID with a shape we fully control.
*/
struct inner {
int a;
int b;
};
struct outer {
struct inner arr[8]; /* off 0, stride 8, size 64 */
char bytes[32]; /* off 64, stride 1, size 32 */
long longs[8]; /* off 96, stride 8, size 64 */
int grid[4][4]; /* off 160, 16 ints, size 64 */
};
/* arr[i].b, i in [0, 7], 4-byte read: stays within arr. */
SEC("syscall")
__success
int arr_field_in_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 7; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 4); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/* arr[i].b, i in [0, 15]: max offset runs past the end of arr. */
SEC("syscall")
__failure
__msg("invalid variable offset access into struct outer")
int arr_field_out_of_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 15; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 4); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/* longs[i], i in [0, 7], 8-byte read: scalar array with stride 8. */
SEC("syscall")
__success
int long_array_in_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
long val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 7; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u64 *)(r1 + 96); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* bytes[i], i in [0, 31], read as 4 bytes: min_off is valid within the array,
* but the 4-byte read at max_off runs past the array end. Exercises the
* size-aware bound in btf_struct_access().
*/
SEC("syscall")
__failure
__flag(BPF_F_ANY_ALIGNMENT)
__msg("invalid variable offset access into struct outer")
int byte_array_size_spanning(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 31; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 64); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/* bytes[i], i in [0, 31], 1-byte read: step 1 matches the char array stride. */
SEC("syscall")
__success
int byte_array_in_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 31; \
r1 += r2; \
%[val] = *(u8 *)(r1 + 64); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* arr[] accessed with step 4 (half of sizeof(struct inner)): the step is not
* a whole number of elements, so no crossed array has a matching stride.
*/
SEC("syscall")
__failure
__msg("invalid variable offset access into struct outer")
int arr_unaligned_step(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 7; \
r2 *= 4; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 0); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* grid[i][0], i in [0, 3]: steps along the outer dimension of a 2D array
* (stride 16). __btf_resolve_size() linearizes the array; the step is a
* multiple of the innermost element size (4).
*/
SEC("syscall")
__success
int grid_outer_dim(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 3; \
r2 *= 16; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 160); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* grid[0][i], i in [0, 3]: steps along the inner dimension of a 2D array
* (stride 4, the innermost element size).
*/
SEC("syscall")
__success
int grid_inner_dim(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct outer *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 3; \
r2 *= 4; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 160); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* An array of pointers within a program-allocated object. Reading a pointer
* member of a local object yields a SCALAR_VALUE, but the varying offset still
* has to be validated against the array bounds.
*/
struct with_ptrs {
struct inner *parr[8]; /* off 0, stride 8 (sizeof ptr), size 64 */
};
/* parr[i], i in [0, 7], 8-byte read: stays within parr. */
SEC("syscall")
__success
int ptr_array_in_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct with_ptrs *o;
long val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 7; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u64 *)(r1 + 0); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* parr[i], i in [0, 15]: max offset runs past the end of parr. Without the
* variable-offset check on the WALK_PTR path this would be wrongly accepted.
*/
SEC("syscall")
__failure
__msg("invalid variable offset access into struct with_ptrs")
int ptr_array_out_of_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct with_ptrs *o;
long val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 15; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u64 *)(r1 + 0); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* The cases above all use bpf_obj_new() (PTR_TO_BTF_ID | MEM_ALLOC). Exercise
* the trusted kernel-BTF path too, using a task_struct from
* bpf_get_current_task_btf() and its embedded char comm[] array. The comm
* offset is folded into the pointer as a constant before the varying index.
*/
/* task->comm[i], i in [0, 15], 1-byte read: stays within comm. */
SEC("syscall")
__success
int kernel_btf_array_in_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct task_struct *task;
int val = 0;
task = bpf_get_current_task_btf();
asm volatile (" \
r1 = %[task]; \
r1 += %[off]; \
r2 = %[i]; \
r2 &= 15; \
r1 += r2; \
%[val] = *(u8 *)(r1 + 0); \
" : [val] "=r"(val)
: [task] "r"(task), [i] "r"(i),
[off] "i"(offsetof(struct task_struct, comm))
: "r1", "r2");
return val;
}
/* task->comm[i], i in [0, 31]: max offset runs past comm. */
SEC("syscall")
__failure
__msg("invalid variable offset access into struct task_struct")
int kernel_btf_array_out_of_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct task_struct *task;
int val = 0;
task = bpf_get_current_task_btf();
asm volatile (" \
r1 = %[task]; \
r1 += %[off]; \
r2 = %[i]; \
r2 &= 31; \
r1 += r2; \
%[val] = *(u8 *)(r1 + 0); \
" : [val] "=r"(val)
: [task] "r"(task), [i] "r"(i),
[off] "i"(offsetof(struct task_struct, comm))
: "r1", "r2");
return val;
}
/*
* Array nested below a struct member at a non-zero offset. The walk descends
* through 'm' (resetting the running offset) before reaching 'arr', exercising
* the array_start = min_off - arrays[i].off telescoping across a WALK_STRUCT
* dive.
*/
struct mid {
struct inner arr[8]; /* stride 8, size 64 */
};
struct nest {
long pad; /* off 0 */
struct mid m; /* off 8 */
};
/* m.arr[i].b, i in [0, 7], 4-byte read: stays within m.arr. */
SEC("syscall")
__success
int nested_struct_in_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct nest *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 7; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 12); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/* m.arr[i].b, i in [0, 15]: max offset runs past the end of m.arr. */
SEC("syscall")
__failure
__msg("invalid variable offset access into struct nest")
int nested_struct_out_of_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct nest *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 15; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 12); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
/*
* A trailing flexible array member makes the struct tail an unbounded region,
* so a varying access into it has no upper bound to exceed (mirrors how
* unix_address.name[] is accessed via sun_path[i]).
*
* A custom (program) BTF struct can only be reached as a PTR_TO_BTF_ID through
* bpf_obj_new(), which does not allow flexible array members. So use a kernel
* type via an untrusted PTR_TO_BTF_ID (bpf_core_cast()). struct vring_used ends
* with a flexible array 'ring[]' of struct vring_used_elem { __virtio32 id, len; },
* mirroring a 'struct foo { int a; int b; }' flexible array.
*/
/* ring[i].len, i in [0, 63], 4-byte read: the flexible array has no upper bound. */
SEC("syscall")
__success
int flex_array_field(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct vring_used *o;
int val = 0;
o = bpf_core_cast(bpf_get_current_task_btf(), struct vring_used);
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 63; \
r2 *= 8; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 8); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
return val;
}
/*
* ring[] accessed with step 4 (half of sizeof(struct vring_used_elem)): the
* step is not a whole number of elements, so the flexible array stride does
* not match.
*/
SEC("syscall")
__failure
__msg("invalid variable offset access into struct vring_used")
int flex_array_unaligned_step(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct vring_used *o;
int val = 0;
o = bpf_core_cast(bpf_get_current_task_btf(), struct vring_used);
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 7; \
r2 *= 4; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 4); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i)
: "r1", "r2");
return val;
}
/*
* Access either name[0].sun_path[0] or one byte past sun_path.
* Step 108 does not match sizeof(struct sockaddr_un), which is 110.
* It matches the inner char array's stride, but exceeds its bounds.
* The flexible-tail exemption must not apply to that fixed array.
*/
SEC("syscall")
__failure
__msg("invalid variable offset access into struct unix_address")
int flex_array_inner_array_out_of_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct unix_address *o;
int val = 0;
o = bpf_core_cast(bpf_get_current_task_btf(), struct unix_address);
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 1; \
r2 *= %[len]; \
r1 += r2; \
%[val] = *(u8 *)(r1 + %[off]); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i),
__imm_const(len, sizeof_field(struct sockaddr_un, sun_path)),
__imm_const(off, offsetof(struct unix_address, name) +
offsetof(struct sockaddr_un, sun_path))
: "r1", "r2");
return val;
}
typedef int array_element_t;
typedef array_element_t array_type_t[4];
struct typedef_array {
array_type_t values;
};
/*
* Resolving values' size crosses both the array and element typedefs.
* Record the array ID, not the int ID left by element-type resolution.
* values[i], i in [0, 3], must pass the variable-offset array check.
*/
SEC("syscall")
__success
int typedef_array_in_bounds(void *ctx)
{
unsigned long i = bpf_get_prandom_u32();
struct typedef_array *o;
int val = 0;
o = bpf_obj_new(typeof(*o));
if (!o)
return 0;
asm volatile (" \
r1 = %[o]; \
r2 = %[i]; \
r2 &= 3; \
r2 *= %[elem_size]; \
r1 += r2; \
%[val] = *(u32 *)(r1 + 0); \
" : [val] "=r"(val)
: [o] "r"(o), [i] "r"(i),
__imm_const(elem_size, sizeof(array_element_t))
: "r1", "r2");
bpf_obj_drop(o);
return val;
}
char _license[] SEC("license") = "GPL";
|