• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2016 The Android Open Source Project
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define USE_RET_KILL 0
17 #define USE_RET_TRAP 1
18 
19 #define NO_LOGGING  0
20 #define USE_LOGGING 1
21 
22 /* BPF testing macros. */
23 #define EXPECT_EQ_BLOCK(_block, _code, _k, _jt, _jf)	\
24 do {	\
25 	EXPECT_EQ((_block)->code, _code);		\
26 	EXPECT_EQ((_block)->k, (unsigned int)(_k));	\
27 	EXPECT_EQ((_block)->jt, _jt);			\
28 	EXPECT_EQ((_block)->jf, _jf);			\
29 } while (0)
30 
31 #define EXPECT_EQ_STMT(_block, _code, _k) \
32 	EXPECT_EQ_BLOCK(_block, _code, _k, 0, 0)
33 
34 #define EXPECT_COMP(_block) \
35 do {	\
36 	EXPECT_EQ((_block)->len, BPF_ARG_COMP_LEN + 1);			\
37 	EXPECT_EQ((_block)->instrs->code, BPF_LD+BPF_W+BPF_ABS);	\
38 } while (0)
39 
40 #define EXPECT_LBL(_block) \
41 do {	\
42 	EXPECT_TRUE((_block)->code == (BPF_JMP+BPF_JA));	\
43 	EXPECT_TRUE((_block)->jt == LABEL_JT);			\
44 	EXPECT_TRUE((_block)->jf == LABEL_JF);			\
45 } while (0)
46 
47 #define EXPECT_ACTUAL_LBL(_block, _id) \
48 do {	\
49 	EXPECT_TRUE((_block)->code == (BPF_JMP+BPF_JA));	\
50 	EXPECT_TRUE((_block)->k == (_id));			\
51 	EXPECT_TRUE((_block)->jt == LABEL_JT);			\
52 	EXPECT_TRUE((_block)->jf == LABEL_JF);			\
53 } while (0)
54 
55 #define EXPECT_JUMP_LBL(_block) \
56 do {	\
57 	EXPECT_EQ((_block)->code, BPF_JMP+BPF_JA);	\
58 	EXPECT_EQ((_block)->jt, JUMP_JT);		\
59 	EXPECT_EQ((_block)->jf, JUMP_JF);		\
60 } while (0)
61 
62 #define EXPECT_GROUP_END(_block) \
63 do {	\
64 	EXPECT_EQ((_block)->len, 2U);			\
65 	EXPECT_JUMP_LBL(&(_block)->instrs[0]);		\
66 	EXPECT_LBL(&(_block)->instrs[1]);		\
67 } while (0)
68 
69 #define EXPECT_KILL(_block) \
70 do {	\
71 	EXPECT_EQ((_block)->len, 1U);				\
72 	EXPECT_EQ_STMT((_block)->instrs,			\
73 			BPF_RET+BPF_K, SECCOMP_RET_KILL);	\
74 } while (0)
75 
76 #define EXPECT_TRAP(_block) \
77 do {	\
78 	EXPECT_EQ((_block)->len, 1U);				\
79 	EXPECT_EQ_STMT((_block)->instrs,			\
80 			BPF_RET+BPF_K, SECCOMP_RET_TRAP);	\
81 } while (0)
82 
83 #define EXPECT_ALLOW(_block) \
84 do {	\
85 	EXPECT_EQ((_block)->len, 2U);				\
86 	EXPECT_LBL(&(_block)->instrs[0]);			\
87 	EXPECT_EQ_STMT(&(_block)->instrs[1],			\
88 			BPF_RET+BPF_K, SECCOMP_RET_ALLOW);	\
89 } while (0)
90 
91 #define EXPECT_ARCH_VALIDATION(_filter) \
92 do {	\
93 	EXPECT_EQ_STMT(&(_filter)[0], BPF_LD+BPF_W+BPF_ABS, arch_nr);	\
94 	EXPECT_EQ_BLOCK(&(_filter)[1],					\
95 			BPF_JMP+BPF_JEQ+BPF_K, ARCH_NR, SKIP, NEXT);	\
96 	EXPECT_EQ_STMT(&(_filter)[2], BPF_RET+BPF_K, SECCOMP_RET_KILL);	\
97 } while (0)
98 
99 #define EXPECT_ALLOW_SYSCALL(_filter, _nr) \
100 do {	\
101 	EXPECT_EQ_BLOCK(&(_filter)[0],					\
102 			BPF_JMP+BPF_JEQ+BPF_K, (_nr), NEXT, SKIP);	\
103 	EXPECT_EQ_STMT(&(_filter)[1],					\
104 			BPF_RET+BPF_K, SECCOMP_RET_ALLOW);		\
105 } while (0)
106 
107 #define EXPECT_ALLOW_SYSCALL_ARGS(_filter, _nr, _id, _jt, _jf) \
108 do {	\
109 	EXPECT_EQ_BLOCK(&(_filter)[0],					\
110 			BPF_JMP+BPF_JEQ+BPF_K, (_nr), NEXT, SKIP);	\
111 	EXPECT_EQ_BLOCK(&(_filter)[1],					\
112 			BPF_JMP+BPF_JA, (_id), (_jt), (_jf));		\
113 } while (0)
114