-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
executable file
·1563 lines (1441 loc) · 34.5 KB
/
tests.py
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import re
import io
import sys
import rewrite
import logging
import unittest
import subprocess
from tree_sitter_matching import *
from cfg import build_cfg, compute_live_regs, cfg_to_dot, cfg_to_text
from rewrite import convert_string, Options, convert_insn_list, parse_c_string
class Tests(unittest.TestCase):
def _aux(self, input, expected_output, options=Options()):
output = convert_string(input, options)
self.maxDiff = None
self.assertMultiLineEqual(expected_output.lstrip(), output,)
def test_simple(self):
self._aux('''
{
/* some
* comment
*/
"invalid and of 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 + 2),
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, 1),
BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
BPF_ALU64_IMM(BPF_AND, BPF_REG_1, -4),
BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2),
BPF_ALU64_IMM(BPF_MOD, BPF_REG_1, 2),
BPF_ALU64_IMM(BPF_OR, BPF_REG_1, 2),
BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
BPF_ALU64_REG(BPF_NEG, BPF_REG_1, BPF_REG_1),
BPF_ALU32_REG(BPF_NEG, BPF_REG_1, BPF_REG_1),
BPF_ALU64_IMM(BPF_NEG, BPF_REG_2, 0),
BPF_ALU32_IMM(BPF_NEG, BPF_REG_2, 0),
// invalid LD_MAP_FD (it is not patched)
BPF_LD_MAP_FD(BPF_REG_7, 32),
BPF_LD_MAP_FD(BPF_REG_8, 42),
// comment
BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, offsetof(struct test_val,
foo)),
BPF_CALL_REL(1),
BPF_EXIT_INSN(),
//
BPF_ENDIAN(BPF_TO_LE, BPF_REG_0, 16),
BPF_ENDIAN(BPF_TO_LE, BPF_REG_1, 32),
BPF_ENDIAN(BPF_TO_LE, BPF_REG_2, 64),
//
BPF_ENDIAN(BPF_TO_BE, BPF_REG_0, 16),
BPF_ENDIAN(BPF_TO_BE, BPF_REG_1, 32),
BPF_ENDIAN(BPF_TO_BE, BPF_REG_2, 64),
//
BPF_ENDIAN(BPF_FROM_LE, BPF_REG_0, 16),
BPF_ENDIAN(BPF_FROM_LE, BPF_REG_1, 32),
BPF_ENDIAN(BPF_FROM_LE, BPF_REG_2, 64),
//
BPF_ENDIAN(BPF_FROM_BE, BPF_REG_0, 16),
BPF_ENDIAN(BPF_FROM_BE, BPF_REG_1, 32),
BPF_ENDIAN(BPF_FROM_BE, BPF_REG_2, 64),
//
BPF_EXIT_INSN(),
},
.fixup_map_hash_48b = { 3 },
.errstr = "R0 max value is outside of the allowed memory range",
.errstr_unpriv = "abra-cadabra",
.result = REJECT,
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "../../../include/linux/filter.h"
#include "bpf_misc.h"
#define MAX_ENTRIES 11
struct test_val {
unsigned int index;
int foo[MAX_ENTRIES];
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, long long);
__type(value, struct test_val);
} map_hash_48b SEC(".maps");
/* some
* comment
*/
SEC("socket")
__description("invalid and of negative number")
__failure __msg("R0 max value is outside of the allowed memory range")
__msg_unpriv("abra-cadabra")
__flag(BPF_F_ANY_ALIGNMENT)
__naked void invalid_and_of_negative_number(void)
{
asm volatile (" \\
*(u64*)(r10 - 8) = 0; \\
r2 = r10; \\
r2 += %[__imm_0]; \\
r1 = %[map_hash_48b] ll; \\
call %[bpf_map_lookup_elem]; \\
if r0 == 0 goto l0_%=; \\
r1 = *(u8*)(r0 + 0); \\
l0_%=: r1 &= -4; \\
r1 <<= 2; \\
r1 %%= 2; \\
r1 |= 2; \\
r0 += r1; \\
r1 = -r1; \\
w1 = -w1; \\
r2 = -r2; \\
w2 = -w2; \\
/* invalid LD_MAP_FD (it is not patched) */ \\
.8byte %[ld_map_fd]; \\
.8byte 0; \\
.8byte %[ld_map_fd_1]; \\
.8byte 0; \\
/* comment */ \\
*(u64*)(r0 + 0) = %[test_val_foo]; \\
call invalid_and_of_negative_number__1; \\
exit; \\
" :
: __imm(bpf_map_lookup_elem),
__imm_addr(map_hash_48b),
__imm_const(__imm_0, -8 + 2),
__imm_const(test_val_foo, offsetof(struct test_val, foo)),
__imm_insn(ld_map_fd, BPF_RAW_INSN(BPF_LD | BPF_DW | BPF_IMM, BPF_REG_7, BPF_PSEUDO_MAP_FD, 0, 32)),
__imm_insn(ld_map_fd_1, BPF_RAW_INSN(BPF_LD | BPF_DW | BPF_IMM, BPF_REG_8, BPF_PSEUDO_MAP_FD, 0, 42))
: __clobber_all);
}
static __naked __noinline __attribute__((used))
void invalid_and_of_negative_number__1(void)
{
asm volatile (" \\
/* */ \\
r0 = le16 r0; \\
r1 = le32 r1; \\
r2 = le64 r2; \\
/* */ \\
r0 = be16 r0; \\
r1 = be32 r1; \\
r2 = be64 r2; \\
/* */ \\
r0 = le16 r0; \\
r1 = le32 r1; \\
r2 = le64 r2; \\
/* */ \\
r0 = be16 r0; \\
r1 = be32 r1; \\
r2 = be64 r2; \\
/* */ \\
exit; \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_double_size_insn(self):
self._aux('''
{
"dsize",
.insns = {
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_EXIT_INSN(),
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_EXIT_INSN(),
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_EXIT_INSN(),
},
.fixup_map_hash_8b = { 0, 3, 6 },
.result = ACCEPT,
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, long long);
__type(value, long long);
} map_hash_8b SEC(".maps");
SEC("socket")
__description("dsize")
__success __success_unpriv __retval(0)
__naked void dsize(void)
{
asm volatile (" \\
r1 = %[map_hash_8b] ll; \\
exit; \\
r1 = %[map_hash_8b] ll; \\
exit; \\
r1 = %[map_hash_8b] ll; \\
exit; \\
" :
: __imm_addr(map_hash_8b)
: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_double_size_insn2(self):
self._aux('''
{
"dsize2",
.insns = {
BPF_LD_IMM64(BPF_REG_1, 0),
BPF_JMP_A(-3),
BPF_LD_IMM64(BPF_REG_1, 0),
BPF_JMP_A(-6),
},
.result = ACCEPT,
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("dsize2")
__success __success_unpriv __retval(0)
__naked void dsize2(void)
{
asm volatile (" \\
l0_%=: r1 = 0 ll; \\
goto l0_%=; \\
r1 = 0 ll; \\
goto l0_%=; \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_long_label(self):
self._aux('''
{
"dsize2",
.insns = {
BPF_MOV64_REG(BPF_REG_0, BPF_REG_0),
BPF_JMP_A(-1),
},
.result = ACCEPT,
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("dsize2")
__success __success_unpriv __retval(0)
__naked void dsize2(void)
{
asm volatile (" \\
r0 = r0; \\
l100_%=: \\
goto l100_%=; \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''', options=Options(label_start=100))
def test_atomic(self):
self._aux('''
{
"atomic",
.insns = {
BPF_ATOMIC_OP(BPF_DW, BPF_ADD | BPF_FETCH, BPF_REG_10, BPF_REG_1, -8),
BPF_ATOMIC_OP(BPF_DW, BPF_AND | BPF_FETCH, BPF_REG_10, BPF_REG_2, -8),
//
BPF_ATOMIC_OP(BPF_W, BPF_FETCH | BPF_OR , BPF_REG_10, BPF_REG_3, -8),
BPF_ATOMIC_OP(BPF_W, BPF_FETCH | BPF_XOR, BPF_REG_10, BPF_REG_4, -8),
//
BPF_ATOMIC_OP(BPF_W, BPF_ADD, BPF_REG_10, BPF_REG_1, -16),
BPF_ATOMIC_OP(BPF_W, BPF_AND, BPF_REG_10, BPF_REG_2, -16),
//
BPF_ATOMIC_OP(BPF_DW, BPF_OR , BPF_REG_10, BPF_REG_3, -16),
BPF_ATOMIC_OP(BPF_DW, BPF_XOR, BPF_REG_10, BPF_REG_4, -16),
//
BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_10, BPF_REG_1, -8),
BPF_ATOMIC_OP(BPF_W, BPF_XCHG, BPF_REG_10, BPF_REG_1, -4),
//
BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_10, BPF_REG_1, -8),
BPF_ATOMIC_OP(BPF_W, BPF_CMPXCHG, BPF_REG_10, BPF_REG_1, -4),
//
BPF_RAW_INSN(BPF_STX | BPF_ATOMIC | BPF_DW, BPF_REG_10, BPF_REG_0, -8, BPF_ADD),
},
.result = ACCEPT,
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("atomic")
__success __success_unpriv __retval(0)
__naked void atomic(void)
{
asm volatile (" \\
r1 = atomic_fetch_add((u64 *)(r10 - 8), r1); \\
r2 = atomic_fetch_and((u64 *)(r10 - 8), r2); \\
/* */ \\
w3 = atomic_fetch_or((u32 *)(r10 - 8), w3); \\
w4 = atomic_fetch_xor((u32 *)(r10 - 8), w4); \\
/* */ \\
lock *(u32 *)(r10 - 16) += w1; \\
lock *(u32 *)(r10 - 16) &= w2; \\
/* */ \\
lock *(u64 *)(r10 - 16) |= r3; \\
lock *(u64 *)(r10 - 16) ^= r4; \\
/* */ \\
r1 = xchg_64(r10 - 8, r1); \\
w1 = xchg32_32(w10 - 4, w1); \\
/* */ \\
r0 = cmpxchg_64(r10 - 8, r0, r1); \\
w0 = cmpxchg32_32(r10 - 4, w0, w1); \\
/* */ \\
lock *(u64 *)(r10 - 8) += r0; \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_comments(self):
self._aux('''
{
/* 1a */ /* 2a */
/* 3a */
"atomic",
/* 1b */ /* 2b */
/* 3b */
.insns = {
/* 1c */ /* 2c */
/* 3c
* 4c
*/
BPF_ST_MEM(BPF_DW, BPF_REG_1, -16, 77),
/* 1d */ /* 2d */
/* 3d */
},
/* 1e */ /* 2e */
/* 3e */
.result = REJECT,
/* 1f */ /* 2f */
/* 3f */
.result_unpriv = REJECT,
/* 1g */ /* 2g */
/* 3g */
.errstr = 'foo',
/* 1h */ /* 2h */
/* 3h */
.errstr_unpriv = 'bar',
/* 1i */ /* 2i */
/* 3i */
.retval = 1,
/* 1j */ /* 2j */
/* 3j */
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
/* 1a */ /* 2a */
/* 3a */
SEC("socket")
__description("atomic")
/* 1e */ /* 2e */
/* 3e */
__failure
/* 1g */ /* 2g */
/* 3g */
__msg("foo")
/* 1f */ /* 2f */
/* 3f */
__failure_unpriv
/* 1h */ /* 2h */
/* 3h */
__msg_unpriv("bar")
/* 1i */ /* 2i */
/* 3i */
__retval(1)
/* 1j */ /* 2j */
/* 3j */
__flag(BPF_F_ANY_ALIGNMENT)
__naked void atomic(void)
{
/* 1b */ /* 2b */
/* 3b */
asm volatile (" \\
/* 1c */ /* 2c */ \\
/* 3c \\
* 4c \\
*/ \\
r0 = 77; \\
*(u64*)(r1 - 16) = r0; \\
/* 1d */ /* 2d */ \\
/* 3d */ \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''', Options(replace_st_mem=True))
def test_rewrap_comments(self):
self._aux('''
{
"atomic",
.insns = {
/* a */
// b
// c
/* d */
BPF_ST_MEM(BPF_DW, BPF_REG_1, -16, 77),
// a
// b
/* c */
},
.result = ACCEPT,
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("atomic")
__success __success_unpriv __retval(0)
__naked void atomic(void)
{
asm volatile (" \\
/* a */ \\
/* b \\
* c \\
*/ \\
/* d */ \\
*(u64*)(r1 - 16) = 77; \\
/* a \\
* b \\
*/ \\
/* c */ \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_dummy_comments_removal(self):
self._aux('''
{
"comments test",
.insns = {
/* r1 = 42 */
BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 42),
/* w1 = 42; */
BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 42),
/* r1 = 42, */
BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 42),
/* r1 = 142 */
BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 42),
},
.result = ACCEPT
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("comments test")
__success __success_unpriv __retval(0)
__naked void comments_test(void)
{
asm volatile (" \\
r1 = 42; \\
r1 = 42; \\
r1 = 42; \\
/* r1 = 142 */ \\
r1 = 42; \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_off(self):
self._aux('''
{
"imm",
.insns = {
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, offsetof(struct foo, bar)),
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -offsetof(struct foo, bar)),
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, offsetof(struct foo, buz[7])),
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 42),
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -7),
BPF_ST_MEM(BPF_DW, BPF_REG_0, -foo, -8),
BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -foo),
BPF_ATOMIC_OP(BPF_DW, BPF_OR , BPF_REG_10, BPF_REG_3, -foo),
BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_10, BPF_REG_1, -foo),
BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_10, BPF_REG_1, -foo),
BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, sizeof(struct foo)),
},
.result = REJECT,
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("imm")
__failure __failure_unpriv
__naked void imm(void)
{
asm volatile (" \\
r0 = *(u64*)(r1 + %[foo_bar]); \\
r0 = *(u64*)(r1 - %[foo_bar]); \\
r0 = *(u64*)(r1 + %[foo_buz_7]); \\
r0 = *(u64*)(r1 + 42); \\
r0 = *(u64*)(r1 - 7); \\
*(u64*)(r0 - %[foo]) = -8; \\
*(u64*)(r0 - %[foo]) = r1; \\
lock *(u64 *)(r10 - %[foo]) |= r3; \\
r1 = xchg_64(r10 - %[foo], r1); \\
r0 = cmpxchg_64(r10 - %[foo], r0, r1); \\
r0 = %[sizeof_foo]; \\
" :
: __imm(foo),
__imm_const(foo_bar, offsetof(struct foo, bar)),
__imm_const(foo_buz_7, offsetof(struct foo, buz[7])),
__imm_const(sizeof_foo, sizeof(struct foo))
: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_result_attrs(self):
self._aux('''
{ "t1", },
{ "t2", .result = ACCEPT, },
{ "t2.1", .result = ACCEPT, .runs = -1 },
{ "t3", .result = VERBOSE_ACCEPT, },
{ "t4", .result = REJECT, },
{ "t5", .result = ACCEPT, .errstr = "x" },
{ "t6", .result = ACCEPT, .errstr = "x", .result_unpriv = REJECT, .errstr_unpriv = "y" },
{ "t7", .result = ACCEPT, .prog_type = BPF_PROG_TYPE_SOCKET_FILTER },
{ "t8", .result = ACCEPT, .prog_type = BPF_PROG_TYPE_CGROUP_SKB },
{ "t9", .result = ACCEPT, .prog_type = BPF_PROG_TYPE_LSM },
{ "t10", .result = ACCEPT, .prog_type = BPF_PROG_TYPE_LSM,
.errstr = "foo\\
bar" },
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("t1")
__failure __failure_unpriv
__naked void t1(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("socket")
__description("t2")
__success __success_unpriv __retval(0)
__naked void t2(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("socket")
__description("t2.1")
__success __success_unpriv
__naked void t2_1(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("socket")
__description("t3")
__success __success_unpriv __log_level(2) __retval(0)
__naked void t3(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("socket")
__description("t4")
__failure __failure_unpriv
__naked void t4(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("socket")
__description("t5")
__success __msg("x")
__success_unpriv
__retval(0)
__naked void t5(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("socket")
__description("t6")
__success __msg("x")
__failure_unpriv __msg_unpriv("y")
__retval(0)
__naked void t6(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("socket")
__description("t7")
__success __success_unpriv __retval(0)
__naked void t7(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("cgroup/skb")
__description("t8")
__success __success_unpriv __retval(0)
__naked void t8(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("lsm")
__description("t9")
__success
__naked void t9(void)
{
asm volatile ("" ::: __clobber_all);
}
SEC("lsm")
__description("t10")
__success
__msg("foo")
__msg("bar")
__naked void t10(void)
{
asm volatile ("" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_bad_insns(self):
self._aux('''
{
"imm",
.insns = {
BPF_ALU64_IMM(12),
BPF_ALU64_REG(CAPIBARA, BPF_REG_1, BPF_REG_2),
BPF_LD_IMM64(),
},
.result = ACCEPT,
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "../../../include/linux/filter.h"
#include "bpf_misc.h"
SEC("socket")
__description("imm")
__success __success_unpriv __retval(0)
__naked void imm(void)
{
asm volatile (" \\
.8byte %[alu64_imm]; \\
.8byte %[alu64_reg]; \\
NOT CONVERTED: BPF_LD_IMM64() \\
" :
: __imm_insn(alu64_imm, BPF_ALU64_IMM(12)),
__imm_insn(alu64_reg, BPF_ALU64_REG(CAPIBARA, BPF_REG_1, BPF_REG_2))
: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_kfunc(self):
self._aux('''
{
"kfunc",
.insns = {
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_EXIT_INSN(),
},
.result = ACCEPT,
.fixup_kfunc_btf_id = {
{ "bpf_kfunc_call_test_acquire", 0 },
{ "bpf_kfunc_call_test_release", 1 },
},
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
struct prog_test_ref_kfunc {} __attribute__((preserve_access_index));
extern struct prog_test_ref_kfunc *
bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr) __ksym;
extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
/* BTF FUNC records are not generated for kfuncs referenced
* from inline assembly. These records are necessary for
* libbpf to link the program. The function below is a hack
* to ensure that BTF FUNC records are generated.
*/
void __kfunc_btf_root()
{
bpf_kfunc_call_test_acquire(0);
bpf_kfunc_call_test_release(0);
}
SEC("socket")
__description("kfunc")
__success __success_unpriv __retval(0)
__naked void kfunc(void)
{
asm volatile (" \\
call %[bpf_kfunc_call_test_acquire]; \\
call %[bpf_kfunc_call_test_release]; \\
exit; \\
" :
: __imm(bpf_kfunc_call_test_acquire),
__imm(bpf_kfunc_call_test_release)
: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_macro1(self):
self._aux('''
{
"macro",
.insns = {
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
offsetofend(struct __sk_buff, gso_size)),
},
.result = ACCEPT,
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
#define offsetofend(TYPE, MEMBER) \\
(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
SEC("socket")
__description("macro")
__success __success_unpriv __retval(0)
__naked void macro(void)
{
asm volatile (" \\
r0 = *(u32*)(r1 + %[__sk_buff_gso_size__end]); \\
" :
: __imm_const(__sk_buff_gso_size__end, offsetofend(struct __sk_buff, gso_size))
: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_string_per_insn(self):
self._aux('''
{
"string_per_insn",
.insns = {
/* Comment line #1
* Comment line #2
*/
BPF_MOV64_IMM(BPF_REG_0, 0),
/* Another comment */
BPF_EXIT_INSN()
},
.result = ACCEPT
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("string_per_insn")
__success __success_unpriv __retval(0)
__naked void string_per_insn(void)
{
asm volatile (
/* Comment line #1
* Comment line #2
*/
"r0 = 0;"
/* Another comment */
"exit;"
::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''', Options(string_per_insn=True))
def test_pseudocall1(self):
self._aux('''
{
"pseudocall",
.insns = {
/* c1 */
BPF_CALL_REL(1),
/* c2 */
BPF_EXIT_INSN(),
/* c3 */
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -3),
/* c4 */
},
.result = ACCEPT
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("pseudocall")
__success __success_unpriv __retval(0)
__naked void pseudocall(void)
{
asm volatile (" \\
/* c1 */ \\
call pseudocall__1; \\
/* c2 */ \\
exit; \\
" ::: __clobber_all);
}
static __naked __noinline __attribute__((used))
void pseudocall__1(void)
{
asm volatile (" \\
/* c3 */ \\
call pseudocall; \\
/* c4 */ \\
" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_pseudocall2(self):
self._aux('''
{
"pseudocall",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, bar),
BPF_CALL_REL(1),
BPF_EXIT_INSN(),
BPF_MOV64_IMM(BPF_REG_0, foo),
BPF_EXIT_INSN(),
},
.result = ACCEPT
},
''',
'''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("socket")
__description("pseudocall")
__success __success_unpriv __retval(0)
__naked void pseudocall(void)
{
asm volatile (" \\
r0 = %[bar]; \\
call pseudocall__1; \\
exit; \\
" :
: __imm(bar)
: __clobber_all);
}
static __naked __noinline __attribute__((used))
void pseudocall__1(void)
{
asm volatile (" \\
r0 = %[foo]; \\
exit; \\
" :
: __imm(foo)
: __clobber_all);
}
char _license[] SEC("license") = "GPL";''')
def test_prog_with_attach_type_1(self):
self._aux('''
{
"pseudocall",
.insns = {},
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
.expected_attach_type = BPF_CGROUP_INET4_CONNECT,
.result = ACCEPT
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("cgroup/connect4")
__description("pseudocall")
__success
__naked void pseudocall(void)
{
asm volatile ("" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''',
Options(string_per_insn=True))
def test_prog_with_attach_type_2(self):
self._aux('''
{
"pseudocall",
.insns = {},
.prog_type = BPF_PROG_TYPE_LSM,
.expected_attach_type = BPF_LSM_MAC,
.kfunc = "barumba",
.result = ACCEPT
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("lsm/barumba")
__description("pseudocall")
__success
__naked void pseudocall(void)
{
asm volatile ("" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''',
Options(string_per_insn=True))
def test_prog_with_attach_type_3(self):
self._aux('''
{
"pseudocall",
.insns = {},
.prog_type = BPF_PROG_TYPE_LSM,
.expected_attach_type = BPF_LSM_MAC,
.kfunc = "barumba",
.flags = BPF_F_SLEEPABLE,
.result = ACCEPT
},
''', '''
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
SEC("lsm.s/barumba")
__description("pseudocall")
__success
__naked void pseudocall(void)
{
asm volatile ("" ::: __clobber_all);
}
char _license[] SEC("license") = "GPL";''',
Options(string_per_insn=True))