/src/crosvm/third_party/minijail/bpf.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* bpf.h |
2 | | * Copyright 2012 The ChromiumOS Authors |
3 | | * Use of this source code is governed by a BSD-style license that can be |
4 | | * found in the LICENSE file. |
5 | | * |
6 | | * Berkeley Packet Filter functions. |
7 | | */ |
8 | | |
9 | | #ifndef BPF_H |
10 | | #define BPF_H |
11 | | |
12 | | #include <asm/bitsperlong.h> /* for __BITS_PER_LONG */ |
13 | | #include <endian.h> |
14 | | #include <linux/audit.h> |
15 | | #include <linux/filter.h> |
16 | | #include <stddef.h> |
17 | | #include <sys/user.h> |
18 | | |
19 | | #ifdef __cplusplus |
20 | | extern "C" { |
21 | | #endif |
22 | | |
23 | | #include "arch.h" |
24 | | |
25 | | #if __BITS_PER_LONG == 32 || defined(__ILP32__) |
26 | | #define BITS32 |
27 | | #elif __BITS_PER_LONG == 64 |
28 | | #define BITS64 |
29 | | #endif |
30 | | |
31 | | /* Constants for comparison operators. */ |
32 | 0 | #define MIN_OPERATOR 128 |
33 | | enum { |
34 | | EQ = MIN_OPERATOR, |
35 | | NE, |
36 | | LT, |
37 | | LE, |
38 | | GT, |
39 | | GE, |
40 | | SET, |
41 | | IN |
42 | | }; |
43 | | |
44 | | /* |
45 | | * BPF return values and data structures, |
46 | | * since they're not yet in the kernel. |
47 | | * TODO(crbug.com/1147037): Replace this with an #include. |
48 | | */ |
49 | | |
50 | | #define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the entire process */ |
51 | | #define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread */ |
52 | | #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD |
53 | | #define SECCOMP_RET_TRAP 0x00030000U /* return SIGSYS */ |
54 | | #define SECCOMP_RET_ERRNO 0x00050000U /* return -1 and set errno */ |
55 | | #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */ |
56 | | #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */ |
57 | | |
58 | | #define SECCOMP_RET_DATA 0x0000ffffU /* mask for return value */ |
59 | | |
60 | | struct seccomp_data { |
61 | | int nr; |
62 | | __u32 arch; |
63 | | __u64 instruction_pointer; |
64 | | __u64 args[6]; |
65 | | }; |
66 | | |
67 | | #define syscall_nr (offsetof(struct seccomp_data, nr)) |
68 | | #define arch_nr (offsetof(struct seccomp_data, arch)) |
69 | | |
70 | | /* Size-dependent defines. */ |
71 | | #if defined(BITS32) |
72 | | /* |
73 | | * On 32 bits, comparisons take 2 instructions: 1 for loading the argument, |
74 | | * 1 for the actual comparison. |
75 | | */ |
76 | | #define BPF_LOAD_ARG_LEN 1U |
77 | | #define BPF_COMP_LEN 1U |
78 | | #define BPF_SHORT_GT_GE_COMP_LEN 1U |
79 | | #define BPF_GT_GE_COMP_LEN 1U |
80 | | #define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN) |
81 | | #define BPF_ARG_SHORT_GT_GE_COMP_LEN \ |
82 | | (BPF_LOAD_ARG_LEN + BPF_SHORT_GT_GE_COMP_LEN) |
83 | | #define BPF_ARG_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_GT_GE_COMP_LEN) |
84 | | |
85 | | #define bpf_comp_jeq bpf_comp_jeq32 |
86 | | #define bpf_comp_jgt bpf_comp_jgt32 |
87 | | #define bpf_comp_jge bpf_comp_jge32 |
88 | | #define bpf_comp_jset bpf_comp_jset32 |
89 | | |
90 | | #define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) |
91 | | |
92 | | #elif defined(BITS64) |
93 | | /* |
94 | | * On 64 bits, comparisons take 7-8 instructions: 4 for loading the argument, |
95 | | * and 3-4 for the actual comparison. |
96 | | */ |
97 | 0 | #define BPF_LOAD_ARG_LEN 4U |
98 | 0 | #define BPF_COMP_LEN 3U |
99 | 0 | #define BPF_SHORT_GT_GE_COMP_LEN 3U |
100 | 0 | #define BPF_GT_GE_COMP_LEN 4U |
101 | 0 | #define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN) |
102 | | #define BPF_ARG_SHORT_GT_GE_COMP_LEN \ |
103 | 0 | (BPF_LOAD_ARG_LEN + BPF_SHORT_GT_GE_COMP_LEN) |
104 | 0 | #define BPF_ARG_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_GT_GE_COMP_LEN) |
105 | | |
106 | 0 | #define bpf_comp_jeq bpf_comp_jeq64 |
107 | 0 | #define bpf_comp_jgt bpf_comp_jgt64 |
108 | 0 | #define bpf_comp_jge bpf_comp_jge64 |
109 | 0 | #define bpf_comp_jset bpf_comp_jset64 |
110 | | |
111 | | /* Ensure that we load the logically correct offset. */ |
112 | | #if defined(__LITTLE_ENDIAN__) || __BYTE_ORDER == __LITTLE_ENDIAN |
113 | | #define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) |
114 | | #define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32) |
115 | | #else |
116 | | #error "Unsupported endianness" |
117 | | #endif |
118 | | |
119 | | #else |
120 | | #error "Unknown bit width" |
121 | | |
122 | | #endif |
123 | | |
124 | | /* Common jump targets. */ |
125 | 0 | #define NEXT 0 |
126 | 0 | #define SKIP 1 |
127 | 0 | #define SKIPN(_n) (_n) |
128 | | |
129 | | /* Support for labels in BPF programs. */ |
130 | 0 | #define JUMP_JT 0xff |
131 | 0 | #define JUMP_JF 0xff |
132 | 0 | #define LABEL_JT 0xfe |
133 | 0 | #define LABEL_JF 0xfe |
134 | | |
135 | 0 | #define MAX_BPF_LABEL_LEN 32 |
136 | | |
137 | 0 | #define BPF_LABELS_MAX 512U /* Each syscall could have an argument block. */ |
138 | | struct bpf_labels { |
139 | | size_t count; |
140 | | struct __bpf_label { |
141 | | const char *label; |
142 | | unsigned int location; |
143 | | } labels[BPF_LABELS_MAX]; |
144 | | }; |
145 | | |
146 | | /* BPF instruction manipulation functions and macros. */ |
147 | | static inline size_t set_bpf_instr(struct sock_filter *instr, |
148 | | unsigned short code, unsigned int k, |
149 | | unsigned char jt, unsigned char jf) |
150 | 0 | { |
151 | 0 | instr->code = code; |
152 | 0 | instr->k = k; |
153 | 0 | instr->jt = jt; |
154 | 0 | instr->jf = jf; |
155 | 0 | return 1U; |
156 | 0 | } Unexecuted instantiation: syscall_filter.c:set_bpf_instr Unexecuted instantiation: bpf.c:set_bpf_instr |
157 | | |
158 | | #define set_bpf_stmt(_block, _code, _k) \ |
159 | 0 | set_bpf_instr((_block), (_code), (_k), 0, 0) |
160 | | |
161 | | #define set_bpf_jump(_block, _code, _k, _jt, _jf) \ |
162 | 0 | set_bpf_instr((_block), (_code), (_k), (_jt), (_jf)) |
163 | | |
164 | | #define set_bpf_lbl(_block, _lbl_id) \ |
165 | 0 | set_bpf_jump((_block), BPF_JMP + BPF_JA, (_lbl_id), LABEL_JT, LABEL_JF) |
166 | | |
167 | | #define set_bpf_jump_lbl(_block, _lbl_id) \ |
168 | 0 | set_bpf_jump((_block), BPF_JMP + BPF_JA, (_lbl_id), JUMP_JT, JUMP_JF) |
169 | | |
170 | | #define set_bpf_ret_kill(_block) \ |
171 | 0 | set_bpf_stmt((_block), BPF_RET + BPF_K, SECCOMP_RET_KILL) |
172 | | |
173 | | #define set_bpf_ret_kill_process(_block) \ |
174 | 0 | set_bpf_stmt((_block), BPF_RET + BPF_K, SECCOMP_RET_KILL_PROCESS) |
175 | | |
176 | | #define set_bpf_ret_trap(_block) \ |
177 | 0 | set_bpf_stmt((_block), BPF_RET + BPF_K, SECCOMP_RET_TRAP) |
178 | | |
179 | | #define set_bpf_ret_errno(_block, _errno) \ |
180 | 0 | set_bpf_stmt((_block), BPF_RET + BPF_K, \ |
181 | 0 | SECCOMP_RET_ERRNO | ((_errno) & SECCOMP_RET_DATA)) |
182 | | |
183 | | #define set_bpf_ret_log(_block) \ |
184 | 0 | set_bpf_stmt((_block), BPF_RET + BPF_K, SECCOMP_RET_LOG) |
185 | | |
186 | | #define set_bpf_ret_allow(_block) \ |
187 | 0 | set_bpf_stmt((_block), BPF_RET + BPF_K, SECCOMP_RET_ALLOW) |
188 | | |
189 | | #define bpf_load_syscall_nr(_filter) \ |
190 | 0 | set_bpf_stmt((_filter), BPF_LD + BPF_W + BPF_ABS, syscall_nr) |
191 | | |
192 | | /* BPF label functions. */ |
193 | | int bpf_resolve_jumps(struct bpf_labels *labels, struct sock_filter *filter, |
194 | | size_t count); |
195 | | int bpf_label_id(struct bpf_labels *labels, const char *label); |
196 | | void free_label_strings(struct bpf_labels *labels); |
197 | | |
198 | | /* BPF helper functions. */ |
199 | | size_t bpf_load_arg(struct sock_filter *filter, int argidx); |
200 | | size_t bpf_comp_jeq(struct sock_filter *filter, unsigned long c, |
201 | | unsigned char jt, unsigned char jf); |
202 | | size_t bpf_comp_jgt(struct sock_filter *filter, unsigned long c, |
203 | | unsigned char jt, unsigned char jf); |
204 | | size_t bpf_comp_jge(struct sock_filter *filter, unsigned long c, |
205 | | unsigned char jt, unsigned char jf); |
206 | | size_t bpf_comp_jset(struct sock_filter *filter, unsigned long mask, |
207 | | unsigned char jt, unsigned char jf); |
208 | | size_t bpf_comp_jin(struct sock_filter *filter, unsigned long mask, |
209 | | unsigned char jt, unsigned char jf); |
210 | | |
211 | | /* Functions called by syscall_filter.c */ |
212 | 0 | #define ARCH_VALIDATION_LEN 3U |
213 | 0 | #define ALLOW_SYSCALL_LEN 2U |
214 | | |
215 | | size_t bpf_arg_comp(struct sock_filter **pfilter, int op, int argidx, |
216 | | unsigned long c, unsigned int label_id); |
217 | | size_t bpf_validate_arch(struct sock_filter *filter); |
218 | | size_t bpf_allow_syscall(struct sock_filter *filter, int nr); |
219 | | size_t bpf_allow_syscall_args(struct sock_filter *filter, int nr, |
220 | | unsigned int id); |
221 | | |
222 | | #ifdef __cplusplus |
223 | | }; /* extern "C" */ |
224 | | #endif |
225 | | |
226 | | #endif /* BPF_H */ |