-
Notifications
You must be signed in to change notification settings - Fork 30
/
syscall_filter_unittest_macros.h
125 lines (106 loc) · 6.08 KB
/
syscall_filter_unittest_macros.h
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
/* Copyright 2016 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SYSCALL_FILTER_UNITTEST_MACROS_H
#define SYSCALL_FILTER_UNITTEST_MACROS_H
#include "bpf.h"
/* BPF testing macros. */
#define EXPECT_EQ_BLOCK(_block, _code, _k, _jt, _jf) \
do { \
EXPECT_EQ((_block)->code, _code); \
EXPECT_EQ((_block)->k, (unsigned int)(_k)); \
EXPECT_EQ((_block)->jt, _jt); \
EXPECT_EQ((_block)->jf, _jf); \
} while (0)
#define EXPECT_EQ_STMT(_block, _code, _k) \
EXPECT_EQ_BLOCK(_block, _code, _k, 0, 0)
#define EXPECT_COMP(_block) \
do { \
EXPECT_EQ((_block)->len, BPF_ARG_COMP_LEN + 1); \
EXPECT_EQ((_block)->instrs->code, BPF_LD + BPF_W + BPF_ABS); \
} while (0)
#define EXPECT_GT_GE_COMP(_block) \
do { \
EXPECT_EQ((_block)->len, BPF_ARG_GT_GE_COMP_LEN + 1); \
EXPECT_EQ((_block)->instrs->code, BPF_LD + BPF_W + BPF_ABS); \
} while (0)
#define EXPECT_SHORT_GT_GE_COMP(_block) \
do { \
EXPECT_EQ((_block)->len, BPF_ARG_SHORT_GT_GE_COMP_LEN + 1); \
EXPECT_EQ((_block)->instrs->code, BPF_LD + BPF_W + BPF_ABS); \
} while (0)
#define EXPECT_LBL(_block) \
do { \
EXPECT_TRUE((_block)->code == (BPF_JMP + BPF_JA)); \
EXPECT_TRUE((_block)->jt == LABEL_JT); \
EXPECT_TRUE((_block)->jf == LABEL_JF); \
} while (0)
#define EXPECT_ACTUAL_LBL(_block, _id) \
do { \
EXPECT_TRUE((_block)->code == (BPF_JMP + BPF_JA)); \
EXPECT_TRUE((_block)->k == (_id)); \
EXPECT_TRUE((_block)->jt == LABEL_JT); \
EXPECT_TRUE((_block)->jf == LABEL_JF); \
} while (0)
#define EXPECT_JUMP_LBL(_block) \
do { \
EXPECT_EQ((_block)->code, BPF_JMP + BPF_JA); \
EXPECT_EQ((_block)->jt, JUMP_JT); \
EXPECT_EQ((_block)->jf, JUMP_JF); \
} while (0)
#define EXPECT_GROUP_END(_block) \
do { \
EXPECT_EQ((_block)->len, 2U); \
EXPECT_JUMP_LBL(&(_block)->instrs[0]); \
EXPECT_LBL(&(_block)->instrs[1]); \
} while (0)
#define EXPECT_KILL(_block) \
do { \
EXPECT_EQ((_block)->len, 1U); \
EXPECT_EQ_STMT((_block)->instrs, BPF_RET + BPF_K, \
SECCOMP_RET_KILL); \
} while (0)
#define EXPECT_TRAP(_block) \
do { \
EXPECT_EQ((_block)->len, 1U); \
EXPECT_EQ_STMT((_block)->instrs, BPF_RET + BPF_K, \
SECCOMP_RET_TRAP); \
} while (0)
#define EXPECT_LOG(_block) \
do { \
EXPECT_EQ((_block)->len, 1U); \
EXPECT_EQ_STMT((_block)->instrs, BPF_RET + BPF_K, \
SECCOMP_RET_LOG); \
} while (0)
#define EXPECT_ALLOW(_block) \
do { \
EXPECT_EQ((_block)->len, 2U); \
EXPECT_LBL(&(_block)->instrs[0]); \
EXPECT_EQ_STMT(&(_block)->instrs[1], BPF_RET + BPF_K, \
SECCOMP_RET_ALLOW); \
} while (0)
#define EXPECT_ARCH_VALIDATION(_filter) \
do { \
EXPECT_EQ_STMT(&(_filter)[0], BPF_LD + BPF_W + BPF_ABS, \
arch_nr); \
EXPECT_EQ_BLOCK(&(_filter)[1], BPF_JMP + BPF_JEQ + BPF_K, \
MINIJAIL_ARCH_NR, SKIP, NEXT); \
EXPECT_EQ_STMT(&(_filter)[2], BPF_RET + BPF_K, \
SECCOMP_RET_KILL); \
} while (0)
#define EXPECT_ALLOW_SYSCALL(_filter, _nr) \
do { \
EXPECT_EQ_BLOCK(&(_filter)[0], BPF_JMP + BPF_JEQ + BPF_K, \
(_nr), NEXT, SKIP); \
EXPECT_EQ_STMT(&(_filter)[1], BPF_RET + BPF_K, \
SECCOMP_RET_ALLOW); \
} while (0)
#define EXPECT_ALLOW_SYSCALL_ARGS(_filter, _nr, _id, _jt, _jf) \
do { \
EXPECT_EQ_BLOCK(&(_filter)[0], BPF_JMP + BPF_JEQ + BPF_K, \
(_nr), NEXT, SKIP); \
EXPECT_EQ_BLOCK(&(_filter)[1], BPF_JMP + BPF_JA, (_id), (_jt), \
(_jf)); \
} while (0)
#endif // SYSCALL_FILTER_UNITTEST_MACROS_H