• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 /* BPF testing macros. */
7 #define EXPECT_EQ_BLOCK(_block, _code, _k, _jt, _jf)	\
8 do {	\
9 	EXPECT_EQ((_block)->code, _code);		\
10 	EXPECT_EQ((_block)->k, (unsigned int)(_k));	\
11 	EXPECT_EQ((_block)->jt, _jt);			\
12 	EXPECT_EQ((_block)->jf, _jf);			\
13 } while (0)
14 
15 #define EXPECT_EQ_STMT(_block, _code, _k) \
16 	EXPECT_EQ_BLOCK(_block, _code, _k, 0, 0)
17 
18 #define EXPECT_COMP(_block) \
19 do {	\
20 	EXPECT_EQ((_block)->len, BPF_ARG_COMP_LEN + 1);			\
21 	EXPECT_EQ((_block)->instrs->code, BPF_LD+BPF_W+BPF_ABS);	\
22 } while (0)
23 
24 #define EXPECT_GT_GE_COMP(_block) \
25 do {	\
26 	EXPECT_EQ((_block)->len, BPF_ARG_GT_GE_COMP_LEN + 1);		\
27 	EXPECT_EQ((_block)->instrs->code, BPF_LD+BPF_W+BPF_ABS);	\
28 } while (0)
29 
30 #define EXPECT_SHORT_GT_GE_COMP(_block) \
31 do {	\
32 	EXPECT_EQ((_block)->len, BPF_ARG_SHORT_GT_GE_COMP_LEN + 1);	\
33 	EXPECT_EQ((_block)->instrs->code, BPF_LD+BPF_W+BPF_ABS);	\
34 } while (0)
35 
36 #define EXPECT_LBL(_block) \
37 do {	\
38 	EXPECT_TRUE((_block)->code == (BPF_JMP+BPF_JA));	\
39 	EXPECT_TRUE((_block)->jt == LABEL_JT);			\
40 	EXPECT_TRUE((_block)->jf == LABEL_JF);			\
41 } while (0)
42 
43 #define EXPECT_ACTUAL_LBL(_block, _id) \
44 do {	\
45 	EXPECT_TRUE((_block)->code == (BPF_JMP+BPF_JA));	\
46 	EXPECT_TRUE((_block)->k == (_id));			\
47 	EXPECT_TRUE((_block)->jt == LABEL_JT);			\
48 	EXPECT_TRUE((_block)->jf == LABEL_JF);			\
49 } while (0)
50 
51 #define EXPECT_JUMP_LBL(_block) \
52 do {	\
53 	EXPECT_EQ((_block)->code, BPF_JMP+BPF_JA);	\
54 	EXPECT_EQ((_block)->jt, JUMP_JT);		\
55 	EXPECT_EQ((_block)->jf, JUMP_JF);		\
56 } while (0)
57 
58 #define EXPECT_GROUP_END(_block) \
59 do {	\
60 	EXPECT_EQ((_block)->len, 2U);			\
61 	EXPECT_JUMP_LBL(&(_block)->instrs[0]);		\
62 	EXPECT_LBL(&(_block)->instrs[1]);		\
63 } while (0)
64 
65 #define EXPECT_KILL(_block) \
66 do {	\
67 	EXPECT_EQ((_block)->len, 1U);				\
68 	EXPECT_EQ_STMT((_block)->instrs,			\
69 			BPF_RET+BPF_K, SECCOMP_RET_KILL);	\
70 } while (0)
71 
72 #define EXPECT_TRAP(_block) \
73 do {	\
74 	EXPECT_EQ((_block)->len, 1U);				\
75 	EXPECT_EQ_STMT((_block)->instrs,			\
76 			BPF_RET+BPF_K, SECCOMP_RET_TRAP);	\
77 } while (0)
78 
79 #define EXPECT_ALLOW(_block) \
80 do {	\
81 	EXPECT_EQ((_block)->len, 2U);				\
82 	EXPECT_LBL(&(_block)->instrs[0]);			\
83 	EXPECT_EQ_STMT(&(_block)->instrs[1],			\
84 			BPF_RET+BPF_K, SECCOMP_RET_ALLOW);	\
85 } while (0)
86 
87 #define EXPECT_ARCH_VALIDATION(_filter) \
88 do {	\
89 	EXPECT_EQ_STMT(&(_filter)[0], BPF_LD+BPF_W+BPF_ABS, arch_nr);	\
90 	EXPECT_EQ_BLOCK(&(_filter)[1],					\
91 			BPF_JMP+BPF_JEQ+BPF_K, ARCH_NR, SKIP, NEXT);	\
92 	EXPECT_EQ_STMT(&(_filter)[2], BPF_RET+BPF_K, SECCOMP_RET_KILL);	\
93 } while (0)
94 
95 #define EXPECT_ALLOW_SYSCALL(_filter, _nr) \
96 do {	\
97 	EXPECT_EQ_BLOCK(&(_filter)[0],					\
98 			BPF_JMP+BPF_JEQ+BPF_K, (_nr), NEXT, SKIP);	\
99 	EXPECT_EQ_STMT(&(_filter)[1],					\
100 			BPF_RET+BPF_K, SECCOMP_RET_ALLOW);		\
101 } while (0)
102 
103 #define EXPECT_ALLOW_SYSCALL_ARGS(_filter, _nr, _id, _jt, _jf) \
104 do {	\
105 	EXPECT_EQ_BLOCK(&(_filter)[0],					\
106 			BPF_JMP+BPF_JEQ+BPF_K, (_nr), NEXT, SKIP);	\
107 	EXPECT_EQ_BLOCK(&(_filter)[1],					\
108 			BPF_JMP+BPF_JA, (_id), (_jt), (_jf));		\
109 } while (0)
110