summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/verifier/bounds.c
blob: 8a1caf46ffbc37800612d6e4a447733cd473b380 (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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
{
	"subtraction bounds (map value) variant 1",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
	BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
	BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 0xff, 7),
	BPF_LDX_MEM(BPF_B, BPF_REG_3, BPF_REG_0, 1),
	BPF_JMP_IMM(BPF_JGT, BPF_REG_3, 0xff, 5),
	BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_3),
	BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 56),
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "R0 max value is outside of the allowed memory range",
	.result = REJECT,
},
{
	"subtraction bounds (map value) variant 2",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
	BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
	BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 0xff, 6),
	BPF_LDX_MEM(BPF_B, BPF_REG_3, BPF_REG_0, 1),
	BPF_JMP_IMM(BPF_JGT, BPF_REG_3, 0xff, 4),
	BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_3),
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "R0 min value is negative, either use unsigned index or do a if (index >=0) check.",
	.errstr_unpriv = "R1 has unknown scalar with mixed signed bounds",
	.result = REJECT,
},
{
	"check subtraction on pointers for unpriv",
	.insns = {
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_LD_MAP_FD(BPF_REG_ARG1, 0),
	BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_FP),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG2, -8),
	BPF_ST_MEM(BPF_DW, BPF_REG_ARG2, 0, 9),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_MOV64_REG(BPF_REG_9, BPF_REG_FP),
	BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_0),
	BPF_LD_MAP_FD(BPF_REG_ARG1, 0),
	BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_FP),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG2, -8),
	BPF_ST_MEM(BPF_DW, BPF_REG_ARG2, 0, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_9, 0),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 1, 9 },
	.result = ACCEPT,
	.result_unpriv = REJECT,
	.errstr_unpriv = "R9 pointer -= pointer prohibited",
},
{
	"bounds check based on zero-extended MOV",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
	/* r2 = 0x0000'0000'ffff'ffff */
	BPF_MOV32_IMM(BPF_REG_2, 0xffffffff),
	/* r2 = 0 */
	BPF_ALU64_IMM(BPF_RSH, BPF_REG_2, 32),
	/* no-op */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
	/* access at offset 0 */
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT
},
{
	"bounds check based on sign-extended MOV. test1",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
	/* r2 = 0xffff'ffff'ffff'ffff */
	BPF_MOV64_IMM(BPF_REG_2, 0xffffffff),
	/* r2 = 0xffff'ffff */
	BPF_ALU64_IMM(BPF_RSH, BPF_REG_2, 32),
	/* r0 = <oob pointer> */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
	/* access to OOB pointer */
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "map_value pointer and 4294967295",
	.result = REJECT
},
{
	"bounds check based on sign-extended MOV. test2",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
	/* r2 = 0xffff'ffff'ffff'ffff */
	BPF_MOV64_IMM(BPF_REG_2, 0xffffffff),
	/* r2 = 0xfff'ffff */
	BPF_ALU64_IMM(BPF_RSH, BPF_REG_2, 36),
	/* r0 = <oob pointer> */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
	/* access to OOB pointer */
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "R0 min value is outside of the allowed memory range",
	.result = REJECT
},
{
	"bounds check based on reg_off + var_off + insn_off. test1",
	.insns = {
	BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
		    offsetof(struct __sk_buff, mark)),
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
	BPF_ALU64_IMM(BPF_AND, BPF_REG_6, 1),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, (1 << 29) - 1),
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_6),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, (1 << 29) - 1),
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 3),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 4 },
	.errstr = "value_size=8 off=1073741825",
	.result = REJECT,
	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
},
{
	"bounds check based on reg_off + var_off + insn_off. test2",
	.insns = {
	BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
		    offsetof(struct __sk_buff, mark)),
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
	BPF_ALU64_IMM(BPF_AND, BPF_REG_6, 1),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, (1 << 30) - 1),
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_6),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, (1 << 29) - 1),
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 3),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 4 },
	.errstr = "value 1073741823",
	.result = REJECT,
	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
},
{
	"bounds check after truncation of non-boundary-crossing range",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
	/* r1 = [0x00, 0xff] */
	BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
	BPF_MOV64_IMM(BPF_REG_2, 1),
	/* r2 = 0x10'0000'0000 */
	BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 36),
	/* r1 = [0x10'0000'0000, 0x10'0000'00ff] */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2),
	/* r1 = [0x10'7fff'ffff, 0x10'8000'00fe] */
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x7fffffff),
	/* r1 = [0x00, 0xff] */
	BPF_ALU32_IMM(BPF_SUB, BPF_REG_1, 0x7fffffff),
	/* r1 = 0 */
	BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
	/* no-op */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	/* access at offset 0 */
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT
},
{
	"bounds check after truncation of boundary-crossing range (1)",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
	/* r1 = [0x00, 0xff] */
	BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
	/* r1 = [0xffff'ff80, 0x1'0000'007f] */
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
	/* r1 = [0xffff'ff80, 0xffff'ffff] or
	 *      [0x0000'0000, 0x0000'007f]
	 */
	BPF_ALU32_IMM(BPF_ADD, BPF_REG_1, 0),
	BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
	/* r1 = [0x00, 0xff] or
	 *      [0xffff'ffff'0000'0080, 0xffff'ffff'ffff'ffff]
	 */
	BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
	/* error on OOB pointer computation */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	/* not actually fully unbounded, but the bound is very high */
	.errstr = "value -4294967168 makes map_value pointer be out of bounds",
	.result = REJECT,
},
{
	"bounds check after truncation of boundary-crossing range (2)",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
	/* r1 = [0x00, 0xff] */
	BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
	/* r1 = [0xffff'ff80, 0x1'0000'007f] */
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
	/* r1 = [0xffff'ff80, 0xffff'ffff] or
	 *      [0x0000'0000, 0x0000'007f]
	 * difference to previous test: truncation via MOV32
	 * instead of ALU32.
	 */
	BPF_MOV32_REG(BPF_REG_1, BPF_REG_1),
	BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
	/* r1 = [0x00, 0xff] or
	 *      [0xffff'ffff'0000'0080, 0xffff'ffff'ffff'ffff]
	 */
	BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
	/* error on OOB pointer computation */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "value -4294967168 makes map_value pointer be out of bounds",
	.result = REJECT,
},
{
	"bounds check after wrapping 32-bit addition",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
	/* r1 = 0x7fff'ffff */
	BPF_MOV64_IMM(BPF_REG_1, 0x7fffffff),
	/* r1 = 0xffff'fffe */
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x7fffffff),
	/* r1 = 0 */
	BPF_ALU32_IMM(BPF_ADD, BPF_REG_1, 2),
	/* no-op */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	/* access at offset 0 */
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT
},
{
	"bounds check after shift with oversized count operand",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
	BPF_MOV64_IMM(BPF_REG_2, 32),
	BPF_MOV64_IMM(BPF_REG_1, 1),
	/* r1 = (u32)1 << (u32)32 = ? */
	BPF_ALU32_REG(BPF_LSH, BPF_REG_1, BPF_REG_2),
	/* r1 = [0x0000, 0xffff] */
	BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0xffff),
	/* computes unknown pointer, potentially OOB */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	/* potentially OOB access */
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "R0 max value is outside of the allowed memory range",
	.result = REJECT
},
{
	"bounds check after right shift of maybe-negative number",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
	/* r1 = [0x00, 0xff] */
	BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
	/* r1 = [-0x01, 0xfe] */
	BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 1),
	/* r1 = 0 or 0xff'ffff'ffff'ffff */
	BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
	/* r1 = 0 or 0xffff'ffff'ffff */
	BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
	/* computes unknown pointer, potentially OOB */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	/* potentially OOB access */
	BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "R0 unbounded memory access",
	.result = REJECT
},
{
	"bounds check after 32-bit right shift with 64-bit input",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
	/* r1 = 2 */
	BPF_MOV64_IMM(BPF_REG_1, 2),
	/* r1 = 1<<32 */
	BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 31),
	/* r1 = 0 (NOT 2!) */
	BPF_ALU32_IMM(BPF_RSH, BPF_REG_1, 31),
	/* r1 = 0xffff'fffe (NOT 0!) */
	BPF_ALU32_IMM(BPF_SUB, BPF_REG_1, 2),
	/* error on computing OOB pointer */
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	/* exit */
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "math between map_value pointer and 4294967294 is not allowed",
	.result = REJECT,
},
{
	"bounds check map access with off+size signed 32bit overflow. test1",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x7ffffffe),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
	BPF_JMP_A(0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "map_value pointer and 2147483646",
	.result = REJECT
},
{
	"bounds check map access with off+size signed 32bit overflow. test2",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x1fffffff),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x1fffffff),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x1fffffff),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
	BPF_JMP_A(0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "pointer offset 1073741822",
	.errstr_unpriv = "R0 pointer arithmetic of map value goes out of range",
	.result = REJECT
},
{
	"bounds check map access with off+size signed 32bit overflow. test3",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 0x1fffffff),
	BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 0x1fffffff),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 2),
	BPF_JMP_A(0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "pointer offset -1073741822",
	.errstr_unpriv = "R0 pointer arithmetic of map value goes out of range",
	.result = REJECT
},
{
	"bounds check map access with off+size signed 32bit overflow. test4",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_MOV64_IMM(BPF_REG_1, 1000000),
	BPF_ALU64_IMM(BPF_MUL, BPF_REG_1, 1000000),
	BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 2),
	BPF_JMP_A(0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr = "map_value pointer and 1000000000000",
	.result = REJECT
},
{
	"bounds check mixed 32bit and 64bit arithmetic. test1",
	.insns = {
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_MOV64_IMM(BPF_REG_1, -1),
	BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 32),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
	/* r1 = 0xffffFFFF00000001 */
	BPF_JMP32_IMM(BPF_JGT, BPF_REG_1, 1, 3),
	/* check ALU64 op keeps 32bit bounds */
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
	BPF_JMP32_IMM(BPF_JGT, BPF_REG_1, 2, 1),
	BPF_JMP_A(1),
	/* invalid ldx if bounds are lost above */
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, -1),
	BPF_EXIT_INSN(),
	},
	.result = ACCEPT
},
{
	"bounds check mixed 32bit and 64bit arithmetic. test2",
	.insns = {
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_MOV64_IMM(BPF_REG_1, -1),
	BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 32),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
	/* r1 = 0xffffFFFF00000001 */
	BPF_MOV64_IMM(BPF_REG_2, 3),
	/* r1 = 0x2 */
	BPF_ALU32_IMM(BPF_ADD, BPF_REG_1, 1),
	/* check ALU32 op zero extends 64bit bounds */
	BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, 1),
	BPF_JMP_A(1),
	/* invalid ldx if bounds are lost above */
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, -1),
	BPF_EXIT_INSN(),
	},
	.result = ACCEPT
},
{
	"assigning 32bit bounds to 64bit for wA = 0, wB = wA",
	.insns = {
	BPF_LDX_MEM(BPF_W, BPF_REG_8, BPF_REG_1,
		    offsetof(struct __sk_buff, data_end)),
	BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
		    offsetof(struct __sk_buff, data)),
	BPF_MOV32_IMM(BPF_REG_9, 0),
	BPF_MOV32_REG(BPF_REG_2, BPF_REG_9),
	BPF_MOV64_REG(BPF_REG_6, BPF_REG_7),
	BPF_ALU64_REG(BPF_ADD, BPF_REG_6, BPF_REG_2),
	BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 8),
	BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_8, 1),
	BPF_LDX_MEM(BPF_W, BPF_REG_5, BPF_REG_6, 0),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
	.result = ACCEPT,
	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
},
{
	"bounds check for reg = 0, reg xor 1",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_MOV64_IMM(BPF_REG_1, 0),
	BPF_ALU64_IMM(BPF_XOR, BPF_REG_1, 1),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT,
},
{
	"bounds check for reg32 = 0, reg32 xor 1",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_MOV32_IMM(BPF_REG_1, 0),
	BPF_ALU32_IMM(BPF_XOR, BPF_REG_1, 1),
	BPF_JMP32_IMM(BPF_JNE, BPF_REG_1, 0, 1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT,
},
{
	"bounds check for reg = 2, reg xor 3",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_MOV64_IMM(BPF_REG_1, 2),
	BPF_ALU64_IMM(BPF_XOR, BPF_REG_1, 3),
	BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 0, 1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT,
},
{
	"bounds check for reg = any, reg xor 3",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
	BPF_ALU64_IMM(BPF_XOR, BPF_REG_1, 3),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = REJECT,
	.errstr = "invalid access to map value",
	.errstr_unpriv = "invalid access to map value",
},
{
	"bounds check for reg32 = any, reg32 xor 3",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
	BPF_ALU32_IMM(BPF_XOR, BPF_REG_1, 3),
	BPF_JMP32_IMM(BPF_JNE, BPF_REG_1, 0, 1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = REJECT,
	.errstr = "invalid access to map value",
	.errstr_unpriv = "invalid access to map value",
},
{
	"bounds check for reg > 0, reg xor 3",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
	BPF_JMP_IMM(BPF_JLE, BPF_REG_1, 0, 3),
	BPF_ALU64_IMM(BPF_XOR, BPF_REG_1, 3),
	BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT,
},
{
	"bounds check for reg32 > 0, reg32 xor 3",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
	BPF_EXIT_INSN(),
	BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
	BPF_JMP32_IMM(BPF_JLE, BPF_REG_1, 0, 3),
	BPF_ALU32_IMM(BPF_XOR, BPF_REG_1, 3),
	BPF_JMP32_IMM(BPF_JGE, BPF_REG_1, 0, 1),
	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.result = ACCEPT,
},
{
	"bounds checks after 32-bit truncation. test 1",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
	BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
	/* This used to reduce the max bound to 0x7fffffff */
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
	BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 0x7fffffff, 1),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr_unpriv = "R0 leaks addr",
	.result_unpriv = REJECT,
	.result = ACCEPT,
},
{
	"bounds checks after 32-bit truncation. test 2",
	.insns = {
	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
	BPF_LD_MAP_FD(BPF_REG_1, 0),
	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
	BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
	BPF_JMP_IMM(BPF_JSLT, BPF_REG_1, 1, 1),
	BPF_JMP32_IMM(BPF_JSLT, BPF_REG_1, 0, 1),
	BPF_MOV64_IMM(BPF_REG_0, 0),
	BPF_EXIT_INSN(),
	},
	.fixup_map_hash_8b = { 3 },
	.errstr_unpriv = "R0 leaks addr",
	.result_unpriv = REJECT,
	.result = ACCEPT,
},