Commit | Line | Data |
---|---|---|
51580e79 AS |
1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
2 | * | |
3 | * This program is free software; you can redistribute it and/or | |
4 | * modify it under the terms of version 2 of the GNU General Public | |
5 | * License as published by the Free Software Foundation. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, but | |
8 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * General Public License for more details. | |
11 | */ | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/types.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/bpf.h> | |
16 | #include <linux/filter.h> | |
17 | #include <net/netlink.h> | |
18 | #include <linux/file.h> | |
19 | #include <linux/vmalloc.h> | |
20 | ||
21 | /* bpf_check() is a static code analyzer that walks eBPF program | |
22 | * instruction by instruction and updates register/stack state. | |
23 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
24 | * | |
25 | * The first pass is depth-first-search to check that the program is a DAG. | |
26 | * It rejects the following programs: | |
27 | * - larger than BPF_MAXINSNS insns | |
28 | * - if loop is present (detected via back-edge) | |
29 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
30 | * - out of bounds or malformed jumps | |
31 | * The second pass is all possible path descent from the 1st insn. | |
32 | * Since it's analyzing all pathes through the program, the length of the | |
33 | * analysis is limited to 32k insn, which may be hit even if total number of | |
34 | * insn is less then 4K, but there are too many branches that change stack/regs. | |
35 | * Number of 'branches to be analyzed' is limited to 1k | |
36 | * | |
37 | * On entry to each instruction, each register has a type, and the instruction | |
38 | * changes the types of the registers depending on instruction semantics. | |
39 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
40 | * copied to R1. | |
41 | * | |
42 | * All registers are 64-bit. | |
43 | * R0 - return register | |
44 | * R1-R5 argument passing registers | |
45 | * R6-R9 callee saved registers | |
46 | * R10 - frame pointer read-only | |
47 | * | |
48 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
49 | * and has type PTR_TO_CTX. | |
50 | * | |
51 | * Verifier tracks arithmetic operations on pointers in case: | |
52 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
53 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
54 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
55 | * and 2nd arithmetic instruction is pattern matched to recognize | |
56 | * that it wants to construct a pointer to some element within stack. | |
57 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
58 | * (and -20 constant is saved for further stack bounds checking). | |
59 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
60 | * | |
61 | * Most of the time the registers have UNKNOWN_VALUE type, which | |
62 | * means the register has some value, but it's not a valid pointer. | |
63 | * (like pointer plus pointer becomes UNKNOWN_VALUE type) | |
64 | * | |
65 | * When verifier sees load or store instructions the type of base register | |
66 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer | |
67 | * types recognized by check_mem_access() function. | |
68 | * | |
69 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
70 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
71 | * | |
72 | * registers used to pass values to function calls are checked against | |
73 | * function argument constraints. | |
74 | * | |
75 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
76 | * It means that the register type passed to this function must be | |
77 | * PTR_TO_STACK and it will be used inside the function as | |
78 | * 'pointer to map element key' | |
79 | * | |
80 | * For example the argument constraints for bpf_map_lookup_elem(): | |
81 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
82 | * .arg1_type = ARG_CONST_MAP_PTR, | |
83 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
84 | * | |
85 | * ret_type says that this function returns 'pointer to map elem value or null' | |
86 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
87 | * 2nd argument should be a pointer to stack, which will be used inside | |
88 | * the helper function as a pointer to map element key. | |
89 | * | |
90 | * On the kernel side the helper function looks like: | |
91 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
92 | * { | |
93 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
94 | * void *key = (void *) (unsigned long) r2; | |
95 | * void *value; | |
96 | * | |
97 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
98 | * [key, key + map->key_size) bytes are valid and were initialized on | |
99 | * the stack of eBPF program. | |
100 | * } | |
101 | * | |
102 | * Corresponding eBPF program may look like: | |
103 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
104 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
105 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
106 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
107 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
108 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
109 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
110 | * | |
111 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
112 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
113 | * and were initialized prior to this call. | |
114 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
115 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
116 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
117 | * returns ether pointer to map value or NULL. | |
118 | * | |
119 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
120 | * insn, the register holding that pointer in the true branch changes state to | |
121 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
122 | * branch. See check_cond_jmp_op(). | |
123 | * | |
124 | * After the call R0 is set to return type of the function and registers R1-R5 | |
125 | * are set to NOT_INIT to indicate that they are no longer readable. | |
126 | */ | |
127 | ||
17a52670 AS |
128 | /* types of values stored in eBPF registers */ |
129 | enum bpf_reg_type { | |
130 | NOT_INIT = 0, /* nothing was written into register */ | |
131 | UNKNOWN_VALUE, /* reg doesn't contain a valid pointer */ | |
132 | PTR_TO_CTX, /* reg points to bpf_context */ | |
133 | CONST_PTR_TO_MAP, /* reg points to struct bpf_map */ | |
134 | PTR_TO_MAP_VALUE, /* reg points to map element value */ | |
135 | PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */ | |
136 | FRAME_PTR, /* reg == frame_pointer */ | |
137 | PTR_TO_STACK, /* reg == frame_pointer + imm */ | |
138 | CONST_IMM, /* constant integer value */ | |
139 | }; | |
140 | ||
141 | struct reg_state { | |
142 | enum bpf_reg_type type; | |
143 | union { | |
144 | /* valid when type == CONST_IMM | PTR_TO_STACK */ | |
4923ec0b | 145 | long imm; |
17a52670 AS |
146 | |
147 | /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | | |
148 | * PTR_TO_MAP_VALUE_OR_NULL | |
149 | */ | |
150 | struct bpf_map *map_ptr; | |
151 | }; | |
152 | }; | |
153 | ||
154 | enum bpf_stack_slot_type { | |
155 | STACK_INVALID, /* nothing was stored in this stack slot */ | |
9c399760 | 156 | STACK_SPILL, /* register spilled into stack */ |
17a52670 AS |
157 | STACK_MISC /* BPF program wrote some data into this slot */ |
158 | }; | |
159 | ||
9c399760 | 160 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ |
17a52670 AS |
161 | |
162 | /* state of the program: | |
163 | * type of all registers and stack info | |
164 | */ | |
165 | struct verifier_state { | |
166 | struct reg_state regs[MAX_BPF_REG]; | |
9c399760 AS |
167 | u8 stack_slot_type[MAX_BPF_STACK]; |
168 | struct reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE]; | |
17a52670 AS |
169 | }; |
170 | ||
171 | /* linked list of verifier states used to prune search */ | |
172 | struct verifier_state_list { | |
173 | struct verifier_state state; | |
174 | struct verifier_state_list *next; | |
175 | }; | |
176 | ||
177 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ | |
178 | struct verifier_stack_elem { | |
179 | /* verifer state is 'st' | |
180 | * before processing instruction 'insn_idx' | |
181 | * and after processing instruction 'prev_insn_idx' | |
182 | */ | |
183 | struct verifier_state st; | |
184 | int insn_idx; | |
185 | int prev_insn_idx; | |
186 | struct verifier_stack_elem *next; | |
187 | }; | |
188 | ||
0246e64d AS |
189 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ |
190 | ||
cbd35700 AS |
191 | /* single container for all structs |
192 | * one verifier_env per bpf_check() call | |
193 | */ | |
194 | struct verifier_env { | |
0246e64d | 195 | struct bpf_prog *prog; /* eBPF program being verified */ |
17a52670 AS |
196 | struct verifier_stack_elem *head; /* stack of verifier states to be processed */ |
197 | int stack_size; /* number of states to be processed */ | |
198 | struct verifier_state cur_state; /* current verifier state */ | |
f1bca824 | 199 | struct verifier_state_list **explored_states; /* search pruning optimization */ |
0246e64d AS |
200 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
201 | u32 used_map_cnt; /* number of used maps */ | |
1be7f75d | 202 | bool allow_ptr_leaks; |
cbd35700 AS |
203 | }; |
204 | ||
07016151 DB |
205 | #define BPF_COMPLEXITY_LIMIT_INSNS 65536 |
206 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 | |
207 | ||
33ff9823 DB |
208 | struct bpf_call_arg_meta { |
209 | struct bpf_map *map_ptr; | |
435faee1 DB |
210 | bool raw_mode; |
211 | int regno; | |
212 | int access_size; | |
33ff9823 DB |
213 | }; |
214 | ||
cbd35700 AS |
215 | /* verbose verifier prints what it's seeing |
216 | * bpf_check() is called under lock, so no race to access these global vars | |
217 | */ | |
218 | static u32 log_level, log_size, log_len; | |
219 | static char *log_buf; | |
220 | ||
221 | static DEFINE_MUTEX(bpf_verifier_lock); | |
222 | ||
223 | /* log_level controls verbosity level of eBPF verifier. | |
224 | * verbose() is used to dump the verification trace to the log, so the user | |
225 | * can figure out what's wrong with the program | |
226 | */ | |
1d056d9c | 227 | static __printf(1, 2) void verbose(const char *fmt, ...) |
cbd35700 AS |
228 | { |
229 | va_list args; | |
230 | ||
231 | if (log_level == 0 || log_len >= log_size - 1) | |
232 | return; | |
233 | ||
234 | va_start(args, fmt); | |
235 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); | |
236 | va_end(args); | |
237 | } | |
238 | ||
17a52670 AS |
239 | /* string representation of 'enum bpf_reg_type' */ |
240 | static const char * const reg_type_str[] = { | |
241 | [NOT_INIT] = "?", | |
242 | [UNKNOWN_VALUE] = "inv", | |
243 | [PTR_TO_CTX] = "ctx", | |
244 | [CONST_PTR_TO_MAP] = "map_ptr", | |
245 | [PTR_TO_MAP_VALUE] = "map_value", | |
246 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
247 | [FRAME_PTR] = "fp", | |
248 | [PTR_TO_STACK] = "fp", | |
249 | [CONST_IMM] = "imm", | |
250 | }; | |
251 | ||
252 | static void print_verifier_state(struct verifier_env *env) | |
253 | { | |
254 | enum bpf_reg_type t; | |
255 | int i; | |
256 | ||
257 | for (i = 0; i < MAX_BPF_REG; i++) { | |
258 | t = env->cur_state.regs[i].type; | |
259 | if (t == NOT_INIT) | |
260 | continue; | |
261 | verbose(" R%d=%s", i, reg_type_str[t]); | |
262 | if (t == CONST_IMM || t == PTR_TO_STACK) | |
4923ec0b | 263 | verbose("%ld", env->cur_state.regs[i].imm); |
17a52670 AS |
264 | else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || |
265 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
266 | verbose("(ks=%d,vs=%d)", | |
267 | env->cur_state.regs[i].map_ptr->key_size, | |
268 | env->cur_state.regs[i].map_ptr->value_size); | |
269 | } | |
9c399760 AS |
270 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
271 | if (env->cur_state.stack_slot_type[i] == STACK_SPILL) | |
17a52670 | 272 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
9c399760 | 273 | reg_type_str[env->cur_state.spilled_regs[i / BPF_REG_SIZE].type]); |
17a52670 AS |
274 | } |
275 | verbose("\n"); | |
276 | } | |
277 | ||
cbd35700 AS |
278 | static const char *const bpf_class_string[] = { |
279 | [BPF_LD] = "ld", | |
280 | [BPF_LDX] = "ldx", | |
281 | [BPF_ST] = "st", | |
282 | [BPF_STX] = "stx", | |
283 | [BPF_ALU] = "alu", | |
284 | [BPF_JMP] = "jmp", | |
285 | [BPF_RET] = "BUG", | |
286 | [BPF_ALU64] = "alu64", | |
287 | }; | |
288 | ||
687f0715 | 289 | static const char *const bpf_alu_string[16] = { |
cbd35700 AS |
290 | [BPF_ADD >> 4] = "+=", |
291 | [BPF_SUB >> 4] = "-=", | |
292 | [BPF_MUL >> 4] = "*=", | |
293 | [BPF_DIV >> 4] = "/=", | |
294 | [BPF_OR >> 4] = "|=", | |
295 | [BPF_AND >> 4] = "&=", | |
296 | [BPF_LSH >> 4] = "<<=", | |
297 | [BPF_RSH >> 4] = ">>=", | |
298 | [BPF_NEG >> 4] = "neg", | |
299 | [BPF_MOD >> 4] = "%=", | |
300 | [BPF_XOR >> 4] = "^=", | |
301 | [BPF_MOV >> 4] = "=", | |
302 | [BPF_ARSH >> 4] = "s>>=", | |
303 | [BPF_END >> 4] = "endian", | |
304 | }; | |
305 | ||
306 | static const char *const bpf_ldst_string[] = { | |
307 | [BPF_W >> 3] = "u32", | |
308 | [BPF_H >> 3] = "u16", | |
309 | [BPF_B >> 3] = "u8", | |
310 | [BPF_DW >> 3] = "u64", | |
311 | }; | |
312 | ||
687f0715 | 313 | static const char *const bpf_jmp_string[16] = { |
cbd35700 AS |
314 | [BPF_JA >> 4] = "jmp", |
315 | [BPF_JEQ >> 4] = "==", | |
316 | [BPF_JGT >> 4] = ">", | |
317 | [BPF_JGE >> 4] = ">=", | |
318 | [BPF_JSET >> 4] = "&", | |
319 | [BPF_JNE >> 4] = "!=", | |
320 | [BPF_JSGT >> 4] = "s>", | |
321 | [BPF_JSGE >> 4] = "s>=", | |
322 | [BPF_CALL >> 4] = "call", | |
323 | [BPF_EXIT >> 4] = "exit", | |
324 | }; | |
325 | ||
326 | static void print_bpf_insn(struct bpf_insn *insn) | |
327 | { | |
328 | u8 class = BPF_CLASS(insn->code); | |
329 | ||
330 | if (class == BPF_ALU || class == BPF_ALU64) { | |
331 | if (BPF_SRC(insn->code) == BPF_X) | |
332 | verbose("(%02x) %sr%d %s %sr%d\n", | |
333 | insn->code, class == BPF_ALU ? "(u32) " : "", | |
334 | insn->dst_reg, | |
335 | bpf_alu_string[BPF_OP(insn->code) >> 4], | |
336 | class == BPF_ALU ? "(u32) " : "", | |
337 | insn->src_reg); | |
338 | else | |
339 | verbose("(%02x) %sr%d %s %s%d\n", | |
340 | insn->code, class == BPF_ALU ? "(u32) " : "", | |
341 | insn->dst_reg, | |
342 | bpf_alu_string[BPF_OP(insn->code) >> 4], | |
343 | class == BPF_ALU ? "(u32) " : "", | |
344 | insn->imm); | |
345 | } else if (class == BPF_STX) { | |
346 | if (BPF_MODE(insn->code) == BPF_MEM) | |
347 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", | |
348 | insn->code, | |
349 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
350 | insn->dst_reg, | |
351 | insn->off, insn->src_reg); | |
352 | else if (BPF_MODE(insn->code) == BPF_XADD) | |
353 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", | |
354 | insn->code, | |
355 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
356 | insn->dst_reg, insn->off, | |
357 | insn->src_reg); | |
358 | else | |
359 | verbose("BUG_%02x\n", insn->code); | |
360 | } else if (class == BPF_ST) { | |
361 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
362 | verbose("BUG_st_%02x\n", insn->code); | |
363 | return; | |
364 | } | |
365 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", | |
366 | insn->code, | |
367 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
368 | insn->dst_reg, | |
369 | insn->off, insn->imm); | |
370 | } else if (class == BPF_LDX) { | |
371 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
372 | verbose("BUG_ldx_%02x\n", insn->code); | |
373 | return; | |
374 | } | |
375 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", | |
376 | insn->code, insn->dst_reg, | |
377 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
378 | insn->src_reg, insn->off); | |
379 | } else if (class == BPF_LD) { | |
380 | if (BPF_MODE(insn->code) == BPF_ABS) { | |
381 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", | |
382 | insn->code, | |
383 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
384 | insn->imm); | |
385 | } else if (BPF_MODE(insn->code) == BPF_IND) { | |
386 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", | |
387 | insn->code, | |
388 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
389 | insn->src_reg, insn->imm); | |
390 | } else if (BPF_MODE(insn->code) == BPF_IMM) { | |
391 | verbose("(%02x) r%d = 0x%x\n", | |
392 | insn->code, insn->dst_reg, insn->imm); | |
393 | } else { | |
394 | verbose("BUG_ld_%02x\n", insn->code); | |
395 | return; | |
396 | } | |
397 | } else if (class == BPF_JMP) { | |
398 | u8 opcode = BPF_OP(insn->code); | |
399 | ||
400 | if (opcode == BPF_CALL) { | |
401 | verbose("(%02x) call %d\n", insn->code, insn->imm); | |
402 | } else if (insn->code == (BPF_JMP | BPF_JA)) { | |
403 | verbose("(%02x) goto pc%+d\n", | |
404 | insn->code, insn->off); | |
405 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { | |
406 | verbose("(%02x) exit\n", insn->code); | |
407 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
408 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", | |
409 | insn->code, insn->dst_reg, | |
410 | bpf_jmp_string[BPF_OP(insn->code) >> 4], | |
411 | insn->src_reg, insn->off); | |
412 | } else { | |
413 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", | |
414 | insn->code, insn->dst_reg, | |
415 | bpf_jmp_string[BPF_OP(insn->code) >> 4], | |
416 | insn->imm, insn->off); | |
417 | } | |
418 | } else { | |
419 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); | |
420 | } | |
421 | } | |
422 | ||
17a52670 AS |
423 | static int pop_stack(struct verifier_env *env, int *prev_insn_idx) |
424 | { | |
425 | struct verifier_stack_elem *elem; | |
426 | int insn_idx; | |
427 | ||
428 | if (env->head == NULL) | |
429 | return -1; | |
430 | ||
431 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); | |
432 | insn_idx = env->head->insn_idx; | |
433 | if (prev_insn_idx) | |
434 | *prev_insn_idx = env->head->prev_insn_idx; | |
435 | elem = env->head->next; | |
436 | kfree(env->head); | |
437 | env->head = elem; | |
438 | env->stack_size--; | |
439 | return insn_idx; | |
440 | } | |
441 | ||
442 | static struct verifier_state *push_stack(struct verifier_env *env, int insn_idx, | |
443 | int prev_insn_idx) | |
444 | { | |
445 | struct verifier_stack_elem *elem; | |
446 | ||
447 | elem = kmalloc(sizeof(struct verifier_stack_elem), GFP_KERNEL); | |
448 | if (!elem) | |
449 | goto err; | |
450 | ||
451 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); | |
452 | elem->insn_idx = insn_idx; | |
453 | elem->prev_insn_idx = prev_insn_idx; | |
454 | elem->next = env->head; | |
455 | env->head = elem; | |
456 | env->stack_size++; | |
07016151 | 457 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
17a52670 AS |
458 | verbose("BPF program is too complex\n"); |
459 | goto err; | |
460 | } | |
461 | return &elem->st; | |
462 | err: | |
463 | /* pop all elements and return */ | |
464 | while (pop_stack(env, NULL) >= 0); | |
465 | return NULL; | |
466 | } | |
467 | ||
468 | #define CALLER_SAVED_REGS 6 | |
469 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
470 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
471 | }; | |
472 | ||
473 | static void init_reg_state(struct reg_state *regs) | |
474 | { | |
475 | int i; | |
476 | ||
477 | for (i = 0; i < MAX_BPF_REG; i++) { | |
478 | regs[i].type = NOT_INIT; | |
479 | regs[i].imm = 0; | |
17a52670 AS |
480 | } |
481 | ||
482 | /* frame pointer */ | |
483 | regs[BPF_REG_FP].type = FRAME_PTR; | |
484 | ||
485 | /* 1st arg to a function */ | |
486 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
487 | } | |
488 | ||
489 | static void mark_reg_unknown_value(struct reg_state *regs, u32 regno) | |
490 | { | |
491 | BUG_ON(regno >= MAX_BPF_REG); | |
492 | regs[regno].type = UNKNOWN_VALUE; | |
493 | regs[regno].imm = 0; | |
17a52670 AS |
494 | } |
495 | ||
496 | enum reg_arg_type { | |
497 | SRC_OP, /* register is used as source operand */ | |
498 | DST_OP, /* register is used as destination operand */ | |
499 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
500 | }; | |
501 | ||
502 | static int check_reg_arg(struct reg_state *regs, u32 regno, | |
503 | enum reg_arg_type t) | |
504 | { | |
505 | if (regno >= MAX_BPF_REG) { | |
506 | verbose("R%d is invalid\n", regno); | |
507 | return -EINVAL; | |
508 | } | |
509 | ||
510 | if (t == SRC_OP) { | |
511 | /* check whether register used as source operand can be read */ | |
512 | if (regs[regno].type == NOT_INIT) { | |
513 | verbose("R%d !read_ok\n", regno); | |
514 | return -EACCES; | |
515 | } | |
516 | } else { | |
517 | /* check whether register used as dest operand can be written to */ | |
518 | if (regno == BPF_REG_FP) { | |
519 | verbose("frame pointer is read only\n"); | |
520 | return -EACCES; | |
521 | } | |
522 | if (t == DST_OP) | |
523 | mark_reg_unknown_value(regs, regno); | |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
528 | static int bpf_size_to_bytes(int bpf_size) | |
529 | { | |
530 | if (bpf_size == BPF_W) | |
531 | return 4; | |
532 | else if (bpf_size == BPF_H) | |
533 | return 2; | |
534 | else if (bpf_size == BPF_B) | |
535 | return 1; | |
536 | else if (bpf_size == BPF_DW) | |
537 | return 8; | |
538 | else | |
539 | return -EINVAL; | |
540 | } | |
541 | ||
1be7f75d AS |
542 | static bool is_spillable_regtype(enum bpf_reg_type type) |
543 | { | |
544 | switch (type) { | |
545 | case PTR_TO_MAP_VALUE: | |
546 | case PTR_TO_MAP_VALUE_OR_NULL: | |
547 | case PTR_TO_STACK: | |
548 | case PTR_TO_CTX: | |
549 | case FRAME_PTR: | |
550 | case CONST_PTR_TO_MAP: | |
551 | return true; | |
552 | default: | |
553 | return false; | |
554 | } | |
555 | } | |
556 | ||
17a52670 AS |
557 | /* check_stack_read/write functions track spill/fill of registers, |
558 | * stack boundary and alignment are checked in check_mem_access() | |
559 | */ | |
560 | static int check_stack_write(struct verifier_state *state, int off, int size, | |
561 | int value_regno) | |
562 | { | |
17a52670 | 563 | int i; |
9c399760 AS |
564 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
565 | * so it's aligned access and [off, off + size) are within stack limits | |
566 | */ | |
17a52670 AS |
567 | |
568 | if (value_regno >= 0 && | |
1be7f75d | 569 | is_spillable_regtype(state->regs[value_regno].type)) { |
17a52670 AS |
570 | |
571 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 572 | if (size != BPF_REG_SIZE) { |
17a52670 AS |
573 | verbose("invalid size of register spill\n"); |
574 | return -EACCES; | |
575 | } | |
576 | ||
17a52670 | 577 | /* save register state */ |
9c399760 AS |
578 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
579 | state->regs[value_regno]; | |
17a52670 | 580 | |
9c399760 AS |
581 | for (i = 0; i < BPF_REG_SIZE; i++) |
582 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; | |
583 | } else { | |
17a52670 | 584 | /* regular write of data into stack */ |
9c399760 AS |
585 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
586 | (struct reg_state) {}; | |
587 | ||
588 | for (i = 0; i < size; i++) | |
589 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; | |
17a52670 AS |
590 | } |
591 | return 0; | |
592 | } | |
593 | ||
594 | static int check_stack_read(struct verifier_state *state, int off, int size, | |
595 | int value_regno) | |
596 | { | |
9c399760 | 597 | u8 *slot_type; |
17a52670 | 598 | int i; |
17a52670 | 599 | |
9c399760 | 600 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
17a52670 | 601 | |
9c399760 AS |
602 | if (slot_type[0] == STACK_SPILL) { |
603 | if (size != BPF_REG_SIZE) { | |
17a52670 AS |
604 | verbose("invalid size of register spill\n"); |
605 | return -EACCES; | |
606 | } | |
9c399760 AS |
607 | for (i = 1; i < BPF_REG_SIZE; i++) { |
608 | if (slot_type[i] != STACK_SPILL) { | |
17a52670 AS |
609 | verbose("corrupted spill memory\n"); |
610 | return -EACCES; | |
611 | } | |
612 | } | |
613 | ||
614 | if (value_regno >= 0) | |
615 | /* restore register state from stack */ | |
9c399760 AS |
616 | state->regs[value_regno] = |
617 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; | |
17a52670 AS |
618 | return 0; |
619 | } else { | |
620 | for (i = 0; i < size; i++) { | |
9c399760 | 621 | if (slot_type[i] != STACK_MISC) { |
17a52670 AS |
622 | verbose("invalid read from stack off %d+%d size %d\n", |
623 | off, i, size); | |
624 | return -EACCES; | |
625 | } | |
626 | } | |
627 | if (value_regno >= 0) | |
628 | /* have read misc data from the stack */ | |
629 | mark_reg_unknown_value(state->regs, value_regno); | |
630 | return 0; | |
631 | } | |
632 | } | |
633 | ||
634 | /* check read/write into map element returned by bpf_map_lookup_elem() */ | |
635 | static int check_map_access(struct verifier_env *env, u32 regno, int off, | |
636 | int size) | |
637 | { | |
638 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; | |
639 | ||
640 | if (off < 0 || off + size > map->value_size) { | |
641 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", | |
642 | map->value_size, off, size); | |
643 | return -EACCES; | |
644 | } | |
645 | return 0; | |
646 | } | |
647 | ||
648 | /* check access to 'struct bpf_context' fields */ | |
649 | static int check_ctx_access(struct verifier_env *env, int off, int size, | |
650 | enum bpf_access_type t) | |
651 | { | |
652 | if (env->prog->aux->ops->is_valid_access && | |
32bbe007 AS |
653 | env->prog->aux->ops->is_valid_access(off, size, t)) { |
654 | /* remember the offset of last byte accessed in ctx */ | |
655 | if (env->prog->aux->max_ctx_offset < off + size) | |
656 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 657 | return 0; |
32bbe007 | 658 | } |
17a52670 AS |
659 | |
660 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); | |
661 | return -EACCES; | |
662 | } | |
663 | ||
1be7f75d AS |
664 | static bool is_pointer_value(struct verifier_env *env, int regno) |
665 | { | |
666 | if (env->allow_ptr_leaks) | |
667 | return false; | |
668 | ||
669 | switch (env->cur_state.regs[regno].type) { | |
670 | case UNKNOWN_VALUE: | |
671 | case CONST_IMM: | |
672 | return false; | |
673 | default: | |
674 | return true; | |
675 | } | |
676 | } | |
677 | ||
17a52670 AS |
678 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
679 | * if t==write, value_regno is a register which value is stored into memory | |
680 | * if t==read, value_regno is a register which will receive the value from memory | |
681 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
682 | * if t==read && value_regno==-1, don't care what we read from memory | |
683 | */ | |
684 | static int check_mem_access(struct verifier_env *env, u32 regno, int off, | |
685 | int bpf_size, enum bpf_access_type t, | |
686 | int value_regno) | |
687 | { | |
688 | struct verifier_state *state = &env->cur_state; | |
689 | int size, err = 0; | |
690 | ||
24b4d2ab AG |
691 | if (state->regs[regno].type == PTR_TO_STACK) |
692 | off += state->regs[regno].imm; | |
693 | ||
17a52670 AS |
694 | size = bpf_size_to_bytes(bpf_size); |
695 | if (size < 0) | |
696 | return size; | |
697 | ||
698 | if (off % size != 0) { | |
699 | verbose("misaligned access off %d size %d\n", off, size); | |
700 | return -EACCES; | |
701 | } | |
702 | ||
703 | if (state->regs[regno].type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
704 | if (t == BPF_WRITE && value_regno >= 0 && |
705 | is_pointer_value(env, value_regno)) { | |
706 | verbose("R%d leaks addr into map\n", value_regno); | |
707 | return -EACCES; | |
708 | } | |
17a52670 AS |
709 | err = check_map_access(env, regno, off, size); |
710 | if (!err && t == BPF_READ && value_regno >= 0) | |
711 | mark_reg_unknown_value(state->regs, value_regno); | |
712 | ||
713 | } else if (state->regs[regno].type == PTR_TO_CTX) { | |
1be7f75d AS |
714 | if (t == BPF_WRITE && value_regno >= 0 && |
715 | is_pointer_value(env, value_regno)) { | |
716 | verbose("R%d leaks addr into ctx\n", value_regno); | |
717 | return -EACCES; | |
718 | } | |
17a52670 AS |
719 | err = check_ctx_access(env, off, size, t); |
720 | if (!err && t == BPF_READ && value_regno >= 0) | |
721 | mark_reg_unknown_value(state->regs, value_regno); | |
722 | ||
24b4d2ab AG |
723 | } else if (state->regs[regno].type == FRAME_PTR || |
724 | state->regs[regno].type == PTR_TO_STACK) { | |
17a52670 AS |
725 | if (off >= 0 || off < -MAX_BPF_STACK) { |
726 | verbose("invalid stack off=%d size=%d\n", off, size); | |
727 | return -EACCES; | |
728 | } | |
1be7f75d AS |
729 | if (t == BPF_WRITE) { |
730 | if (!env->allow_ptr_leaks && | |
731 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && | |
732 | size != BPF_REG_SIZE) { | |
733 | verbose("attempt to corrupt spilled pointer on stack\n"); | |
734 | return -EACCES; | |
735 | } | |
17a52670 | 736 | err = check_stack_write(state, off, size, value_regno); |
1be7f75d | 737 | } else { |
17a52670 | 738 | err = check_stack_read(state, off, size, value_regno); |
1be7f75d | 739 | } |
17a52670 AS |
740 | } else { |
741 | verbose("R%d invalid mem access '%s'\n", | |
742 | regno, reg_type_str[state->regs[regno].type]); | |
743 | return -EACCES; | |
744 | } | |
745 | return err; | |
746 | } | |
747 | ||
748 | static int check_xadd(struct verifier_env *env, struct bpf_insn *insn) | |
749 | { | |
750 | struct reg_state *regs = env->cur_state.regs; | |
751 | int err; | |
752 | ||
753 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
754 | insn->imm != 0) { | |
755 | verbose("BPF_XADD uses reserved fields\n"); | |
756 | return -EINVAL; | |
757 | } | |
758 | ||
759 | /* check src1 operand */ | |
760 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
761 | if (err) | |
762 | return err; | |
763 | ||
764 | /* check src2 operand */ | |
765 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
766 | if (err) | |
767 | return err; | |
768 | ||
769 | /* check whether atomic_add can read the memory */ | |
770 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
771 | BPF_SIZE(insn->code), BPF_READ, -1); | |
772 | if (err) | |
773 | return err; | |
774 | ||
775 | /* check whether atomic_add can write into the same memory */ | |
776 | return check_mem_access(env, insn->dst_reg, insn->off, | |
777 | BPF_SIZE(insn->code), BPF_WRITE, -1); | |
778 | } | |
779 | ||
780 | /* when register 'regno' is passed into function that will read 'access_size' | |
781 | * bytes from that pointer, make sure that it's within stack boundary | |
782 | * and all elements of stack are initialized | |
783 | */ | |
8e2fe1d9 | 784 | static int check_stack_boundary(struct verifier_env *env, int regno, |
435faee1 DB |
785 | int access_size, bool zero_size_allowed, |
786 | struct bpf_call_arg_meta *meta) | |
17a52670 AS |
787 | { |
788 | struct verifier_state *state = &env->cur_state; | |
789 | struct reg_state *regs = state->regs; | |
790 | int off, i; | |
791 | ||
8e2fe1d9 DB |
792 | if (regs[regno].type != PTR_TO_STACK) { |
793 | if (zero_size_allowed && access_size == 0 && | |
794 | regs[regno].type == CONST_IMM && | |
795 | regs[regno].imm == 0) | |
796 | return 0; | |
797 | ||
798 | verbose("R%d type=%s expected=%s\n", regno, | |
799 | reg_type_str[regs[regno].type], | |
800 | reg_type_str[PTR_TO_STACK]); | |
17a52670 | 801 | return -EACCES; |
8e2fe1d9 | 802 | } |
17a52670 AS |
803 | |
804 | off = regs[regno].imm; | |
805 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
806 | access_size <= 0) { | |
807 | verbose("invalid stack type R%d off=%d access_size=%d\n", | |
808 | regno, off, access_size); | |
809 | return -EACCES; | |
810 | } | |
811 | ||
435faee1 DB |
812 | if (meta && meta->raw_mode) { |
813 | meta->access_size = access_size; | |
814 | meta->regno = regno; | |
815 | return 0; | |
816 | } | |
817 | ||
17a52670 | 818 | for (i = 0; i < access_size; i++) { |
9c399760 | 819 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
17a52670 AS |
820 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
821 | off, i, access_size); | |
822 | return -EACCES; | |
823 | } | |
824 | } | |
825 | return 0; | |
826 | } | |
827 | ||
828 | static int check_func_arg(struct verifier_env *env, u32 regno, | |
33ff9823 DB |
829 | enum bpf_arg_type arg_type, |
830 | struct bpf_call_arg_meta *meta) | |
17a52670 AS |
831 | { |
832 | struct reg_state *reg = env->cur_state.regs + regno; | |
833 | enum bpf_reg_type expected_type; | |
834 | int err = 0; | |
835 | ||
80f1d68c | 836 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
837 | return 0; |
838 | ||
839 | if (reg->type == NOT_INIT) { | |
840 | verbose("R%d !read_ok\n", regno); | |
841 | return -EACCES; | |
842 | } | |
843 | ||
1be7f75d AS |
844 | if (arg_type == ARG_ANYTHING) { |
845 | if (is_pointer_value(env, regno)) { | |
846 | verbose("R%d leaks addr into helper function\n", regno); | |
847 | return -EACCES; | |
848 | } | |
80f1d68c | 849 | return 0; |
1be7f75d | 850 | } |
80f1d68c | 851 | |
8e2fe1d9 | 852 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
17a52670 AS |
853 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
854 | expected_type = PTR_TO_STACK; | |
8e2fe1d9 DB |
855 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
856 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { | |
17a52670 AS |
857 | expected_type = CONST_IMM; |
858 | } else if (arg_type == ARG_CONST_MAP_PTR) { | |
859 | expected_type = CONST_PTR_TO_MAP; | |
608cd71a AS |
860 | } else if (arg_type == ARG_PTR_TO_CTX) { |
861 | expected_type = PTR_TO_CTX; | |
435faee1 DB |
862 | } else if (arg_type == ARG_PTR_TO_STACK || |
863 | arg_type == ARG_PTR_TO_RAW_STACK) { | |
8e2fe1d9 DB |
864 | expected_type = PTR_TO_STACK; |
865 | /* One exception here. In case function allows for NULL to be | |
866 | * passed in as argument, it's a CONST_IMM type. Final test | |
867 | * happens during stack boundary checking. | |
868 | */ | |
869 | if (reg->type == CONST_IMM && reg->imm == 0) | |
870 | expected_type = CONST_IMM; | |
435faee1 | 871 | meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK; |
17a52670 AS |
872 | } else { |
873 | verbose("unsupported arg_type %d\n", arg_type); | |
874 | return -EFAULT; | |
875 | } | |
876 | ||
877 | if (reg->type != expected_type) { | |
878 | verbose("R%d type=%s expected=%s\n", regno, | |
879 | reg_type_str[reg->type], reg_type_str[expected_type]); | |
880 | return -EACCES; | |
881 | } | |
882 | ||
883 | if (arg_type == ARG_CONST_MAP_PTR) { | |
884 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 885 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
886 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
887 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
888 | * check that [key, key + map->key_size) are within | |
889 | * stack limits and initialized | |
890 | */ | |
33ff9823 | 891 | if (!meta->map_ptr) { |
17a52670 AS |
892 | /* in function declaration map_ptr must come before |
893 | * map_key, so that it's verified and known before | |
894 | * we have to check map_key here. Otherwise it means | |
895 | * that kernel subsystem misconfigured verifier | |
896 | */ | |
897 | verbose("invalid map_ptr to access map->key\n"); | |
898 | return -EACCES; | |
899 | } | |
33ff9823 | 900 | err = check_stack_boundary(env, regno, meta->map_ptr->key_size, |
435faee1 | 901 | false, NULL); |
17a52670 AS |
902 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
903 | /* bpf_map_xxx(..., map_ptr, ..., value) call: | |
904 | * check [value, value + map->value_size) validity | |
905 | */ | |
33ff9823 | 906 | if (!meta->map_ptr) { |
17a52670 AS |
907 | /* kernel subsystem misconfigured verifier */ |
908 | verbose("invalid map_ptr to access map->value\n"); | |
909 | return -EACCES; | |
910 | } | |
33ff9823 | 911 | err = check_stack_boundary(env, regno, |
435faee1 DB |
912 | meta->map_ptr->value_size, |
913 | false, NULL); | |
8e2fe1d9 DB |
914 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
915 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { | |
916 | bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO); | |
17a52670 | 917 | |
17a52670 AS |
918 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
919 | * from stack pointer 'buf'. Check it | |
920 | * note: regno == len, regno - 1 == buf | |
921 | */ | |
922 | if (regno == 0) { | |
923 | /* kernel subsystem misconfigured verifier */ | |
924 | verbose("ARG_CONST_STACK_SIZE cannot be first argument\n"); | |
925 | return -EACCES; | |
926 | } | |
8e2fe1d9 | 927 | err = check_stack_boundary(env, regno - 1, reg->imm, |
435faee1 | 928 | zero_size_allowed, meta); |
17a52670 AS |
929 | } |
930 | ||
931 | return err; | |
932 | } | |
933 | ||
35578d79 KX |
934 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
935 | { | |
35578d79 KX |
936 | if (!map) |
937 | return 0; | |
938 | ||
6aff67c8 AS |
939 | /* We need a two way check, first is from map perspective ... */ |
940 | switch (map->map_type) { | |
941 | case BPF_MAP_TYPE_PROG_ARRAY: | |
942 | if (func_id != BPF_FUNC_tail_call) | |
943 | goto error; | |
944 | break; | |
945 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
946 | if (func_id != BPF_FUNC_perf_event_read && | |
947 | func_id != BPF_FUNC_perf_event_output) | |
948 | goto error; | |
949 | break; | |
950 | case BPF_MAP_TYPE_STACK_TRACE: | |
951 | if (func_id != BPF_FUNC_get_stackid) | |
952 | goto error; | |
953 | break; | |
954 | default: | |
955 | break; | |
956 | } | |
957 | ||
958 | /* ... and second from the function itself. */ | |
959 | switch (func_id) { | |
960 | case BPF_FUNC_tail_call: | |
961 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
962 | goto error; | |
963 | break; | |
964 | case BPF_FUNC_perf_event_read: | |
965 | case BPF_FUNC_perf_event_output: | |
966 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) | |
967 | goto error; | |
968 | break; | |
969 | case BPF_FUNC_get_stackid: | |
970 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
971 | goto error; | |
972 | break; | |
973 | default: | |
974 | break; | |
35578d79 KX |
975 | } |
976 | ||
977 | return 0; | |
6aff67c8 AS |
978 | error: |
979 | verbose("cannot pass map_type %d into func %d\n", | |
980 | map->map_type, func_id); | |
981 | return -EINVAL; | |
35578d79 KX |
982 | } |
983 | ||
435faee1 DB |
984 | static int check_raw_mode(const struct bpf_func_proto *fn) |
985 | { | |
986 | int count = 0; | |
987 | ||
988 | if (fn->arg1_type == ARG_PTR_TO_RAW_STACK) | |
989 | count++; | |
990 | if (fn->arg2_type == ARG_PTR_TO_RAW_STACK) | |
991 | count++; | |
992 | if (fn->arg3_type == ARG_PTR_TO_RAW_STACK) | |
993 | count++; | |
994 | if (fn->arg4_type == ARG_PTR_TO_RAW_STACK) | |
995 | count++; | |
996 | if (fn->arg5_type == ARG_PTR_TO_RAW_STACK) | |
997 | count++; | |
998 | ||
999 | return count > 1 ? -EINVAL : 0; | |
1000 | } | |
1001 | ||
17a52670 AS |
1002 | static int check_call(struct verifier_env *env, int func_id) |
1003 | { | |
1004 | struct verifier_state *state = &env->cur_state; | |
1005 | const struct bpf_func_proto *fn = NULL; | |
1006 | struct reg_state *regs = state->regs; | |
17a52670 | 1007 | struct reg_state *reg; |
33ff9823 | 1008 | struct bpf_call_arg_meta meta; |
17a52670 AS |
1009 | int i, err; |
1010 | ||
1011 | /* find function prototype */ | |
1012 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
1013 | verbose("invalid func %d\n", func_id); | |
1014 | return -EINVAL; | |
1015 | } | |
1016 | ||
1017 | if (env->prog->aux->ops->get_func_proto) | |
1018 | fn = env->prog->aux->ops->get_func_proto(func_id); | |
1019 | ||
1020 | if (!fn) { | |
1021 | verbose("unknown func %d\n", func_id); | |
1022 | return -EINVAL; | |
1023 | } | |
1024 | ||
1025 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 1026 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
17a52670 AS |
1027 | verbose("cannot call GPL only function from proprietary program\n"); |
1028 | return -EINVAL; | |
1029 | } | |
1030 | ||
33ff9823 DB |
1031 | memset(&meta, 0, sizeof(meta)); |
1032 | ||
435faee1 DB |
1033 | /* We only support one arg being in raw mode at the moment, which |
1034 | * is sufficient for the helper functions we have right now. | |
1035 | */ | |
1036 | err = check_raw_mode(fn); | |
1037 | if (err) { | |
1038 | verbose("kernel subsystem misconfigured func %d\n", func_id); | |
1039 | return err; | |
1040 | } | |
1041 | ||
17a52670 | 1042 | /* check args */ |
33ff9823 | 1043 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
1044 | if (err) |
1045 | return err; | |
33ff9823 | 1046 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
1047 | if (err) |
1048 | return err; | |
33ff9823 | 1049 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
1050 | if (err) |
1051 | return err; | |
33ff9823 | 1052 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
1053 | if (err) |
1054 | return err; | |
33ff9823 | 1055 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
1056 | if (err) |
1057 | return err; | |
1058 | ||
435faee1 DB |
1059 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
1060 | * is inferred from register state. | |
1061 | */ | |
1062 | for (i = 0; i < meta.access_size; i++) { | |
1063 | err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1); | |
1064 | if (err) | |
1065 | return err; | |
1066 | } | |
1067 | ||
17a52670 AS |
1068 | /* reset caller saved regs */ |
1069 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
1070 | reg = regs + caller_saved[i]; | |
1071 | reg->type = NOT_INIT; | |
1072 | reg->imm = 0; | |
1073 | } | |
1074 | ||
1075 | /* update return register */ | |
1076 | if (fn->ret_type == RET_INTEGER) { | |
1077 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
1078 | } else if (fn->ret_type == RET_VOID) { | |
1079 | regs[BPF_REG_0].type = NOT_INIT; | |
1080 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { | |
1081 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
1082 | /* remember map_ptr, so that check_map_access() | |
1083 | * can check 'value_size' boundary of memory access | |
1084 | * to map element returned from bpf_map_lookup_elem() | |
1085 | */ | |
33ff9823 | 1086 | if (meta.map_ptr == NULL) { |
17a52670 AS |
1087 | verbose("kernel subsystem misconfigured verifier\n"); |
1088 | return -EINVAL; | |
1089 | } | |
33ff9823 | 1090 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
17a52670 AS |
1091 | } else { |
1092 | verbose("unknown return type %d of func %d\n", | |
1093 | fn->ret_type, func_id); | |
1094 | return -EINVAL; | |
1095 | } | |
04fd61ab | 1096 | |
33ff9823 | 1097 | err = check_map_func_compatibility(meta.map_ptr, func_id); |
35578d79 KX |
1098 | if (err) |
1099 | return err; | |
04fd61ab | 1100 | |
17a52670 AS |
1101 | return 0; |
1102 | } | |
1103 | ||
1104 | /* check validity of 32-bit and 64-bit arithmetic operations */ | |
1be7f75d | 1105 | static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn) |
17a52670 | 1106 | { |
1be7f75d | 1107 | struct reg_state *regs = env->cur_state.regs; |
17a52670 AS |
1108 | u8 opcode = BPF_OP(insn->code); |
1109 | int err; | |
1110 | ||
1111 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
1112 | if (opcode == BPF_NEG) { | |
1113 | if (BPF_SRC(insn->code) != 0 || | |
1114 | insn->src_reg != BPF_REG_0 || | |
1115 | insn->off != 0 || insn->imm != 0) { | |
1116 | verbose("BPF_NEG uses reserved fields\n"); | |
1117 | return -EINVAL; | |
1118 | } | |
1119 | } else { | |
1120 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
1121 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) { | |
1122 | verbose("BPF_END uses reserved fields\n"); | |
1123 | return -EINVAL; | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | /* check src operand */ | |
1128 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1129 | if (err) | |
1130 | return err; | |
1131 | ||
1be7f75d AS |
1132 | if (is_pointer_value(env, insn->dst_reg)) { |
1133 | verbose("R%d pointer arithmetic prohibited\n", | |
1134 | insn->dst_reg); | |
1135 | return -EACCES; | |
1136 | } | |
1137 | ||
17a52670 AS |
1138 | /* check dest operand */ |
1139 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1140 | if (err) | |
1141 | return err; | |
1142 | ||
1143 | } else if (opcode == BPF_MOV) { | |
1144 | ||
1145 | if (BPF_SRC(insn->code) == BPF_X) { | |
1146 | if (insn->imm != 0 || insn->off != 0) { | |
1147 | verbose("BPF_MOV uses reserved fields\n"); | |
1148 | return -EINVAL; | |
1149 | } | |
1150 | ||
1151 | /* check src operand */ | |
1152 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1153 | if (err) | |
1154 | return err; | |
1155 | } else { | |
1156 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
1157 | verbose("BPF_MOV uses reserved fields\n"); | |
1158 | return -EINVAL; | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | /* check dest operand */ | |
1163 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1164 | if (err) | |
1165 | return err; | |
1166 | ||
1167 | if (BPF_SRC(insn->code) == BPF_X) { | |
1168 | if (BPF_CLASS(insn->code) == BPF_ALU64) { | |
1169 | /* case: R1 = R2 | |
1170 | * copy register state to dest reg | |
1171 | */ | |
1172 | regs[insn->dst_reg] = regs[insn->src_reg]; | |
1173 | } else { | |
1be7f75d AS |
1174 | if (is_pointer_value(env, insn->src_reg)) { |
1175 | verbose("R%d partial copy of pointer\n", | |
1176 | insn->src_reg); | |
1177 | return -EACCES; | |
1178 | } | |
17a52670 AS |
1179 | regs[insn->dst_reg].type = UNKNOWN_VALUE; |
1180 | regs[insn->dst_reg].map_ptr = NULL; | |
1181 | } | |
1182 | } else { | |
1183 | /* case: R = imm | |
1184 | * remember the value we stored into this reg | |
1185 | */ | |
1186 | regs[insn->dst_reg].type = CONST_IMM; | |
1187 | regs[insn->dst_reg].imm = insn->imm; | |
1188 | } | |
1189 | ||
1190 | } else if (opcode > BPF_END) { | |
1191 | verbose("invalid BPF_ALU opcode %x\n", opcode); | |
1192 | return -EINVAL; | |
1193 | ||
1194 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
1195 | ||
1196 | bool stack_relative = false; | |
1197 | ||
1198 | if (BPF_SRC(insn->code) == BPF_X) { | |
1199 | if (insn->imm != 0 || insn->off != 0) { | |
1200 | verbose("BPF_ALU uses reserved fields\n"); | |
1201 | return -EINVAL; | |
1202 | } | |
1203 | /* check src1 operand */ | |
1204 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1205 | if (err) | |
1206 | return err; | |
1207 | } else { | |
1208 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
1209 | verbose("BPF_ALU uses reserved fields\n"); | |
1210 | return -EINVAL; | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | /* check src2 operand */ | |
1215 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1216 | if (err) | |
1217 | return err; | |
1218 | ||
1219 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
1220 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
1221 | verbose("div by zero\n"); | |
1222 | return -EINVAL; | |
1223 | } | |
1224 | ||
229394e8 RV |
1225 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
1226 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
1227 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
1228 | ||
1229 | if (insn->imm < 0 || insn->imm >= size) { | |
1230 | verbose("invalid shift %d\n", insn->imm); | |
1231 | return -EINVAL; | |
1232 | } | |
1233 | } | |
1234 | ||
17a52670 AS |
1235 | /* pattern match 'bpf_add Rx, imm' instruction */ |
1236 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && | |
1237 | regs[insn->dst_reg].type == FRAME_PTR && | |
1be7f75d | 1238 | BPF_SRC(insn->code) == BPF_K) { |
17a52670 | 1239 | stack_relative = true; |
1be7f75d AS |
1240 | } else if (is_pointer_value(env, insn->dst_reg)) { |
1241 | verbose("R%d pointer arithmetic prohibited\n", | |
1242 | insn->dst_reg); | |
1243 | return -EACCES; | |
1244 | } else if (BPF_SRC(insn->code) == BPF_X && | |
1245 | is_pointer_value(env, insn->src_reg)) { | |
1246 | verbose("R%d pointer arithmetic prohibited\n", | |
1247 | insn->src_reg); | |
1248 | return -EACCES; | |
1249 | } | |
17a52670 AS |
1250 | |
1251 | /* check dest operand */ | |
1252 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1253 | if (err) | |
1254 | return err; | |
1255 | ||
1256 | if (stack_relative) { | |
1257 | regs[insn->dst_reg].type = PTR_TO_STACK; | |
1258 | regs[insn->dst_reg].imm = insn->imm; | |
1259 | } | |
1260 | } | |
1261 | ||
1262 | return 0; | |
1263 | } | |
1264 | ||
1265 | static int check_cond_jmp_op(struct verifier_env *env, | |
1266 | struct bpf_insn *insn, int *insn_idx) | |
1267 | { | |
1268 | struct reg_state *regs = env->cur_state.regs; | |
1269 | struct verifier_state *other_branch; | |
1270 | u8 opcode = BPF_OP(insn->code); | |
1271 | int err; | |
1272 | ||
1273 | if (opcode > BPF_EXIT) { | |
1274 | verbose("invalid BPF_JMP opcode %x\n", opcode); | |
1275 | return -EINVAL; | |
1276 | } | |
1277 | ||
1278 | if (BPF_SRC(insn->code) == BPF_X) { | |
1279 | if (insn->imm != 0) { | |
1280 | verbose("BPF_JMP uses reserved fields\n"); | |
1281 | return -EINVAL; | |
1282 | } | |
1283 | ||
1284 | /* check src1 operand */ | |
1285 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1286 | if (err) | |
1287 | return err; | |
1be7f75d AS |
1288 | |
1289 | if (is_pointer_value(env, insn->src_reg)) { | |
1290 | verbose("R%d pointer comparison prohibited\n", | |
1291 | insn->src_reg); | |
1292 | return -EACCES; | |
1293 | } | |
17a52670 AS |
1294 | } else { |
1295 | if (insn->src_reg != BPF_REG_0) { | |
1296 | verbose("BPF_JMP uses reserved fields\n"); | |
1297 | return -EINVAL; | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | /* check src2 operand */ | |
1302 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1303 | if (err) | |
1304 | return err; | |
1305 | ||
1306 | /* detect if R == 0 where R was initialized to zero earlier */ | |
1307 | if (BPF_SRC(insn->code) == BPF_K && | |
1308 | (opcode == BPF_JEQ || opcode == BPF_JNE) && | |
1309 | regs[insn->dst_reg].type == CONST_IMM && | |
1310 | regs[insn->dst_reg].imm == insn->imm) { | |
1311 | if (opcode == BPF_JEQ) { | |
1312 | /* if (imm == imm) goto pc+off; | |
1313 | * only follow the goto, ignore fall-through | |
1314 | */ | |
1315 | *insn_idx += insn->off; | |
1316 | return 0; | |
1317 | } else { | |
1318 | /* if (imm != imm) goto pc+off; | |
1319 | * only follow fall-through branch, since | |
1320 | * that's where the program will go | |
1321 | */ | |
1322 | return 0; | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); | |
1327 | if (!other_branch) | |
1328 | return -EFAULT; | |
1329 | ||
1330 | /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */ | |
1331 | if (BPF_SRC(insn->code) == BPF_K && | |
1332 | insn->imm == 0 && (opcode == BPF_JEQ || | |
1333 | opcode == BPF_JNE) && | |
1334 | regs[insn->dst_reg].type == PTR_TO_MAP_VALUE_OR_NULL) { | |
1335 | if (opcode == BPF_JEQ) { | |
1336 | /* next fallthrough insn can access memory via | |
1337 | * this register | |
1338 | */ | |
1339 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
1340 | /* branch targer cannot access it, since reg == 0 */ | |
1341 | other_branch->regs[insn->dst_reg].type = CONST_IMM; | |
1342 | other_branch->regs[insn->dst_reg].imm = 0; | |
1343 | } else { | |
1344 | other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
1345 | regs[insn->dst_reg].type = CONST_IMM; | |
1346 | regs[insn->dst_reg].imm = 0; | |
1347 | } | |
1be7f75d AS |
1348 | } else if (is_pointer_value(env, insn->dst_reg)) { |
1349 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); | |
1350 | return -EACCES; | |
17a52670 AS |
1351 | } else if (BPF_SRC(insn->code) == BPF_K && |
1352 | (opcode == BPF_JEQ || opcode == BPF_JNE)) { | |
1353 | ||
1354 | if (opcode == BPF_JEQ) { | |
1355 | /* detect if (R == imm) goto | |
1356 | * and in the target state recognize that R = imm | |
1357 | */ | |
1358 | other_branch->regs[insn->dst_reg].type = CONST_IMM; | |
1359 | other_branch->regs[insn->dst_reg].imm = insn->imm; | |
1360 | } else { | |
1361 | /* detect if (R != imm) goto | |
1362 | * and in the fall-through state recognize that R = imm | |
1363 | */ | |
1364 | regs[insn->dst_reg].type = CONST_IMM; | |
1365 | regs[insn->dst_reg].imm = insn->imm; | |
1366 | } | |
1367 | } | |
1368 | if (log_level) | |
1369 | print_verifier_state(env); | |
1370 | return 0; | |
1371 | } | |
1372 | ||
0246e64d AS |
1373 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
1374 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) | |
1375 | { | |
1376 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; | |
1377 | ||
1378 | return (struct bpf_map *) (unsigned long) imm64; | |
1379 | } | |
1380 | ||
17a52670 AS |
1381 | /* verify BPF_LD_IMM64 instruction */ |
1382 | static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn) | |
1383 | { | |
1384 | struct reg_state *regs = env->cur_state.regs; | |
1385 | int err; | |
1386 | ||
1387 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
1388 | verbose("invalid BPF_LD_IMM insn\n"); | |
1389 | return -EINVAL; | |
1390 | } | |
1391 | if (insn->off != 0) { | |
1392 | verbose("BPF_LD_IMM64 uses reserved fields\n"); | |
1393 | return -EINVAL; | |
1394 | } | |
1395 | ||
1396 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1397 | if (err) | |
1398 | return err; | |
1399 | ||
1400 | if (insn->src_reg == 0) | |
1401 | /* generic move 64-bit immediate into a register */ | |
1402 | return 0; | |
1403 | ||
1404 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ | |
1405 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); | |
1406 | ||
1407 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
1408 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); | |
1409 | return 0; | |
1410 | } | |
1411 | ||
96be4325 DB |
1412 | static bool may_access_skb(enum bpf_prog_type type) |
1413 | { | |
1414 | switch (type) { | |
1415 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
1416 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 1417 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
1418 | return true; |
1419 | default: | |
1420 | return false; | |
1421 | } | |
1422 | } | |
1423 | ||
ddd872bc AS |
1424 | /* verify safety of LD_ABS|LD_IND instructions: |
1425 | * - they can only appear in the programs where ctx == skb | |
1426 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
1427 | * preserve R6-R9, and store return value into R0 | |
1428 | * | |
1429 | * Implicit input: | |
1430 | * ctx == skb == R6 == CTX | |
1431 | * | |
1432 | * Explicit input: | |
1433 | * SRC == any register | |
1434 | * IMM == 32-bit immediate | |
1435 | * | |
1436 | * Output: | |
1437 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
1438 | */ | |
1439 | static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn) | |
1440 | { | |
1441 | struct reg_state *regs = env->cur_state.regs; | |
1442 | u8 mode = BPF_MODE(insn->code); | |
1443 | struct reg_state *reg; | |
1444 | int i, err; | |
1445 | ||
24701ece | 1446 | if (!may_access_skb(env->prog->type)) { |
96be4325 | 1447 | verbose("BPF_LD_ABS|IND instructions not allowed for this program type\n"); |
ddd872bc AS |
1448 | return -EINVAL; |
1449 | } | |
1450 | ||
1451 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || | |
d82bccc6 | 1452 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc AS |
1453 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
1454 | verbose("BPF_LD_ABS uses reserved fields\n"); | |
1455 | return -EINVAL; | |
1456 | } | |
1457 | ||
1458 | /* check whether implicit source operand (register R6) is readable */ | |
1459 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); | |
1460 | if (err) | |
1461 | return err; | |
1462 | ||
1463 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { | |
1464 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
1465 | return -EINVAL; | |
1466 | } | |
1467 | ||
1468 | if (mode == BPF_IND) { | |
1469 | /* check explicit source operand */ | |
1470 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1471 | if (err) | |
1472 | return err; | |
1473 | } | |
1474 | ||
1475 | /* reset caller saved regs to unreadable */ | |
1476 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
1477 | reg = regs + caller_saved[i]; | |
1478 | reg->type = NOT_INIT; | |
1479 | reg->imm = 0; | |
1480 | } | |
1481 | ||
1482 | /* mark destination R0 register as readable, since it contains | |
1483 | * the value fetched from the packet | |
1484 | */ | |
1485 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
1486 | return 0; | |
1487 | } | |
1488 | ||
475fb78f AS |
1489 | /* non-recursive DFS pseudo code |
1490 | * 1 procedure DFS-iterative(G,v): | |
1491 | * 2 label v as discovered | |
1492 | * 3 let S be a stack | |
1493 | * 4 S.push(v) | |
1494 | * 5 while S is not empty | |
1495 | * 6 t <- S.pop() | |
1496 | * 7 if t is what we're looking for: | |
1497 | * 8 return t | |
1498 | * 9 for all edges e in G.adjacentEdges(t) do | |
1499 | * 10 if edge e is already labelled | |
1500 | * 11 continue with the next edge | |
1501 | * 12 w <- G.adjacentVertex(t,e) | |
1502 | * 13 if vertex w is not discovered and not explored | |
1503 | * 14 label e as tree-edge | |
1504 | * 15 label w as discovered | |
1505 | * 16 S.push(w) | |
1506 | * 17 continue at 5 | |
1507 | * 18 else if vertex w is discovered | |
1508 | * 19 label e as back-edge | |
1509 | * 20 else | |
1510 | * 21 // vertex w is explored | |
1511 | * 22 label e as forward- or cross-edge | |
1512 | * 23 label t as explored | |
1513 | * 24 S.pop() | |
1514 | * | |
1515 | * convention: | |
1516 | * 0x10 - discovered | |
1517 | * 0x11 - discovered and fall-through edge labelled | |
1518 | * 0x12 - discovered and fall-through and branch edges labelled | |
1519 | * 0x20 - explored | |
1520 | */ | |
1521 | ||
1522 | enum { | |
1523 | DISCOVERED = 0x10, | |
1524 | EXPLORED = 0x20, | |
1525 | FALLTHROUGH = 1, | |
1526 | BRANCH = 2, | |
1527 | }; | |
1528 | ||
f1bca824 AS |
1529 | #define STATE_LIST_MARK ((struct verifier_state_list *) -1L) |
1530 | ||
475fb78f AS |
1531 | static int *insn_stack; /* stack of insns to process */ |
1532 | static int cur_stack; /* current stack index */ | |
1533 | static int *insn_state; | |
1534 | ||
1535 | /* t, w, e - match pseudo-code above: | |
1536 | * t - index of current instruction | |
1537 | * w - next instruction | |
1538 | * e - edge | |
1539 | */ | |
1540 | static int push_insn(int t, int w, int e, struct verifier_env *env) | |
1541 | { | |
1542 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) | |
1543 | return 0; | |
1544 | ||
1545 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
1546 | return 0; | |
1547 | ||
1548 | if (w < 0 || w >= env->prog->len) { | |
1549 | verbose("jump out of range from insn %d to %d\n", t, w); | |
1550 | return -EINVAL; | |
1551 | } | |
1552 | ||
f1bca824 AS |
1553 | if (e == BRANCH) |
1554 | /* mark branch target for state pruning */ | |
1555 | env->explored_states[w] = STATE_LIST_MARK; | |
1556 | ||
475fb78f AS |
1557 | if (insn_state[w] == 0) { |
1558 | /* tree-edge */ | |
1559 | insn_state[t] = DISCOVERED | e; | |
1560 | insn_state[w] = DISCOVERED; | |
1561 | if (cur_stack >= env->prog->len) | |
1562 | return -E2BIG; | |
1563 | insn_stack[cur_stack++] = w; | |
1564 | return 1; | |
1565 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
1566 | verbose("back-edge from insn %d to %d\n", t, w); | |
1567 | return -EINVAL; | |
1568 | } else if (insn_state[w] == EXPLORED) { | |
1569 | /* forward- or cross-edge */ | |
1570 | insn_state[t] = DISCOVERED | e; | |
1571 | } else { | |
1572 | verbose("insn state internal bug\n"); | |
1573 | return -EFAULT; | |
1574 | } | |
1575 | return 0; | |
1576 | } | |
1577 | ||
1578 | /* non-recursive depth-first-search to detect loops in BPF program | |
1579 | * loop == back-edge in directed graph | |
1580 | */ | |
1581 | static int check_cfg(struct verifier_env *env) | |
1582 | { | |
1583 | struct bpf_insn *insns = env->prog->insnsi; | |
1584 | int insn_cnt = env->prog->len; | |
1585 | int ret = 0; | |
1586 | int i, t; | |
1587 | ||
1588 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
1589 | if (!insn_state) | |
1590 | return -ENOMEM; | |
1591 | ||
1592 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
1593 | if (!insn_stack) { | |
1594 | kfree(insn_state); | |
1595 | return -ENOMEM; | |
1596 | } | |
1597 | ||
1598 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
1599 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
1600 | cur_stack = 1; | |
1601 | ||
1602 | peek_stack: | |
1603 | if (cur_stack == 0) | |
1604 | goto check_state; | |
1605 | t = insn_stack[cur_stack - 1]; | |
1606 | ||
1607 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { | |
1608 | u8 opcode = BPF_OP(insns[t].code); | |
1609 | ||
1610 | if (opcode == BPF_EXIT) { | |
1611 | goto mark_explored; | |
1612 | } else if (opcode == BPF_CALL) { | |
1613 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
1614 | if (ret == 1) | |
1615 | goto peek_stack; | |
1616 | else if (ret < 0) | |
1617 | goto err_free; | |
07016151 DB |
1618 | if (t + 1 < insn_cnt) |
1619 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
1620 | } else if (opcode == BPF_JA) { |
1621 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
1622 | ret = -EINVAL; | |
1623 | goto err_free; | |
1624 | } | |
1625 | /* unconditional jump with single edge */ | |
1626 | ret = push_insn(t, t + insns[t].off + 1, | |
1627 | FALLTHROUGH, env); | |
1628 | if (ret == 1) | |
1629 | goto peek_stack; | |
1630 | else if (ret < 0) | |
1631 | goto err_free; | |
f1bca824 AS |
1632 | /* tell verifier to check for equivalent states |
1633 | * after every call and jump | |
1634 | */ | |
c3de6317 AS |
1635 | if (t + 1 < insn_cnt) |
1636 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
1637 | } else { |
1638 | /* conditional jump with two edges */ | |
1639 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
1640 | if (ret == 1) | |
1641 | goto peek_stack; | |
1642 | else if (ret < 0) | |
1643 | goto err_free; | |
1644 | ||
1645 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
1646 | if (ret == 1) | |
1647 | goto peek_stack; | |
1648 | else if (ret < 0) | |
1649 | goto err_free; | |
1650 | } | |
1651 | } else { | |
1652 | /* all other non-branch instructions with single | |
1653 | * fall-through edge | |
1654 | */ | |
1655 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
1656 | if (ret == 1) | |
1657 | goto peek_stack; | |
1658 | else if (ret < 0) | |
1659 | goto err_free; | |
1660 | } | |
1661 | ||
1662 | mark_explored: | |
1663 | insn_state[t] = EXPLORED; | |
1664 | if (cur_stack-- <= 0) { | |
1665 | verbose("pop stack internal bug\n"); | |
1666 | ret = -EFAULT; | |
1667 | goto err_free; | |
1668 | } | |
1669 | goto peek_stack; | |
1670 | ||
1671 | check_state: | |
1672 | for (i = 0; i < insn_cnt; i++) { | |
1673 | if (insn_state[i] != EXPLORED) { | |
1674 | verbose("unreachable insn %d\n", i); | |
1675 | ret = -EINVAL; | |
1676 | goto err_free; | |
1677 | } | |
1678 | } | |
1679 | ret = 0; /* cfg looks good */ | |
1680 | ||
1681 | err_free: | |
1682 | kfree(insn_state); | |
1683 | kfree(insn_stack); | |
1684 | return ret; | |
1685 | } | |
1686 | ||
f1bca824 AS |
1687 | /* compare two verifier states |
1688 | * | |
1689 | * all states stored in state_list are known to be valid, since | |
1690 | * verifier reached 'bpf_exit' instruction through them | |
1691 | * | |
1692 | * this function is called when verifier exploring different branches of | |
1693 | * execution popped from the state stack. If it sees an old state that has | |
1694 | * more strict register state and more strict stack state then this execution | |
1695 | * branch doesn't need to be explored further, since verifier already | |
1696 | * concluded that more strict state leads to valid finish. | |
1697 | * | |
1698 | * Therefore two states are equivalent if register state is more conservative | |
1699 | * and explored stack state is more conservative than the current one. | |
1700 | * Example: | |
1701 | * explored current | |
1702 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
1703 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
1704 | * | |
1705 | * In other words if current stack state (one being explored) has more | |
1706 | * valid slots than old one that already passed validation, it means | |
1707 | * the verifier can stop exploring and conclude that current state is valid too | |
1708 | * | |
1709 | * Similarly with registers. If explored state has register type as invalid | |
1710 | * whereas register type in current state is meaningful, it means that | |
1711 | * the current state will reach 'bpf_exit' instruction safely | |
1712 | */ | |
1713 | static bool states_equal(struct verifier_state *old, struct verifier_state *cur) | |
1714 | { | |
1715 | int i; | |
1716 | ||
1717 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1718 | if (memcmp(&old->regs[i], &cur->regs[i], | |
1719 | sizeof(old->regs[0])) != 0) { | |
1720 | if (old->regs[i].type == NOT_INIT || | |
32bf08a6 AS |
1721 | (old->regs[i].type == UNKNOWN_VALUE && |
1722 | cur->regs[i].type != NOT_INIT)) | |
f1bca824 AS |
1723 | continue; |
1724 | return false; | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | for (i = 0; i < MAX_BPF_STACK; i++) { | |
9c399760 AS |
1729 | if (old->stack_slot_type[i] == STACK_INVALID) |
1730 | continue; | |
1731 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) | |
1732 | /* Ex: old explored (safe) state has STACK_SPILL in | |
1733 | * this stack slot, but current has has STACK_MISC -> | |
1734 | * this verifier states are not equivalent, | |
1735 | * return false to continue verification of this path | |
1736 | */ | |
f1bca824 | 1737 | return false; |
9c399760 AS |
1738 | if (i % BPF_REG_SIZE) |
1739 | continue; | |
1740 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], | |
1741 | &cur->spilled_regs[i / BPF_REG_SIZE], | |
1742 | sizeof(old->spilled_regs[0]))) | |
1743 | /* when explored and current stack slot types are | |
1744 | * the same, check that stored pointers types | |
1745 | * are the same as well. | |
1746 | * Ex: explored safe path could have stored | |
1747 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -8} | |
1748 | * but current path has stored: | |
1749 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -16} | |
1750 | * such verifier states are not equivalent. | |
1751 | * return false to continue verification of this path | |
1752 | */ | |
1753 | return false; | |
1754 | else | |
1755 | continue; | |
f1bca824 AS |
1756 | } |
1757 | return true; | |
1758 | } | |
1759 | ||
1760 | static int is_state_visited(struct verifier_env *env, int insn_idx) | |
1761 | { | |
1762 | struct verifier_state_list *new_sl; | |
1763 | struct verifier_state_list *sl; | |
1764 | ||
1765 | sl = env->explored_states[insn_idx]; | |
1766 | if (!sl) | |
1767 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
1768 | * be doing state search here | |
1769 | */ | |
1770 | return 0; | |
1771 | ||
1772 | while (sl != STATE_LIST_MARK) { | |
1773 | if (states_equal(&sl->state, &env->cur_state)) | |
1774 | /* reached equivalent register/stack state, | |
1775 | * prune the search | |
1776 | */ | |
1777 | return 1; | |
1778 | sl = sl->next; | |
1779 | } | |
1780 | ||
1781 | /* there were no equivalent states, remember current one. | |
1782 | * technically the current state is not proven to be safe yet, | |
1783 | * but it will either reach bpf_exit (which means it's safe) or | |
1784 | * it will be rejected. Since there are no loops, we won't be | |
1785 | * seeing this 'insn_idx' instruction again on the way to bpf_exit | |
1786 | */ | |
1787 | new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER); | |
1788 | if (!new_sl) | |
1789 | return -ENOMEM; | |
1790 | ||
1791 | /* add new state to the head of linked list */ | |
1792 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); | |
1793 | new_sl->next = env->explored_states[insn_idx]; | |
1794 | env->explored_states[insn_idx] = new_sl; | |
1795 | return 0; | |
1796 | } | |
1797 | ||
17a52670 AS |
1798 | static int do_check(struct verifier_env *env) |
1799 | { | |
1800 | struct verifier_state *state = &env->cur_state; | |
1801 | struct bpf_insn *insns = env->prog->insnsi; | |
1802 | struct reg_state *regs = state->regs; | |
1803 | int insn_cnt = env->prog->len; | |
1804 | int insn_idx, prev_insn_idx = 0; | |
1805 | int insn_processed = 0; | |
1806 | bool do_print_state = false; | |
1807 | ||
1808 | init_reg_state(regs); | |
1809 | insn_idx = 0; | |
1810 | for (;;) { | |
1811 | struct bpf_insn *insn; | |
1812 | u8 class; | |
1813 | int err; | |
1814 | ||
1815 | if (insn_idx >= insn_cnt) { | |
1816 | verbose("invalid insn idx %d insn_cnt %d\n", | |
1817 | insn_idx, insn_cnt); | |
1818 | return -EFAULT; | |
1819 | } | |
1820 | ||
1821 | insn = &insns[insn_idx]; | |
1822 | class = BPF_CLASS(insn->code); | |
1823 | ||
07016151 | 1824 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
17a52670 AS |
1825 | verbose("BPF program is too large. Proccessed %d insn\n", |
1826 | insn_processed); | |
1827 | return -E2BIG; | |
1828 | } | |
1829 | ||
f1bca824 AS |
1830 | err = is_state_visited(env, insn_idx); |
1831 | if (err < 0) | |
1832 | return err; | |
1833 | if (err == 1) { | |
1834 | /* found equivalent state, can prune the search */ | |
1835 | if (log_level) { | |
1836 | if (do_print_state) | |
1837 | verbose("\nfrom %d to %d: safe\n", | |
1838 | prev_insn_idx, insn_idx); | |
1839 | else | |
1840 | verbose("%d: safe\n", insn_idx); | |
1841 | } | |
1842 | goto process_bpf_exit; | |
1843 | } | |
1844 | ||
17a52670 AS |
1845 | if (log_level && do_print_state) { |
1846 | verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx); | |
1847 | print_verifier_state(env); | |
1848 | do_print_state = false; | |
1849 | } | |
1850 | ||
1851 | if (log_level) { | |
1852 | verbose("%d: ", insn_idx); | |
1853 | print_bpf_insn(insn); | |
1854 | } | |
1855 | ||
1856 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1be7f75d | 1857 | err = check_alu_op(env, insn); |
17a52670 AS |
1858 | if (err) |
1859 | return err; | |
1860 | ||
1861 | } else if (class == BPF_LDX) { | |
9bac3d6d AS |
1862 | enum bpf_reg_type src_reg_type; |
1863 | ||
1864 | /* check for reserved fields is already done */ | |
1865 | ||
17a52670 AS |
1866 | /* check src operand */ |
1867 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1868 | if (err) | |
1869 | return err; | |
1870 | ||
1871 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); | |
1872 | if (err) | |
1873 | return err; | |
1874 | ||
725f9dcd AS |
1875 | src_reg_type = regs[insn->src_reg].type; |
1876 | ||
17a52670 AS |
1877 | /* check that memory (src_reg + off) is readable, |
1878 | * the state of dst_reg will be updated by this func | |
1879 | */ | |
1880 | err = check_mem_access(env, insn->src_reg, insn->off, | |
1881 | BPF_SIZE(insn->code), BPF_READ, | |
1882 | insn->dst_reg); | |
1883 | if (err) | |
1884 | return err; | |
1885 | ||
725f9dcd AS |
1886 | if (BPF_SIZE(insn->code) != BPF_W) { |
1887 | insn_idx++; | |
1888 | continue; | |
1889 | } | |
9bac3d6d | 1890 | |
725f9dcd | 1891 | if (insn->imm == 0) { |
9bac3d6d AS |
1892 | /* saw a valid insn |
1893 | * dst_reg = *(u32 *)(src_reg + off) | |
1894 | * use reserved 'imm' field to mark this insn | |
1895 | */ | |
1896 | insn->imm = src_reg_type; | |
1897 | ||
1898 | } else if (src_reg_type != insn->imm && | |
1899 | (src_reg_type == PTR_TO_CTX || | |
1900 | insn->imm == PTR_TO_CTX)) { | |
1901 | /* ABuser program is trying to use the same insn | |
1902 | * dst_reg = *(u32*) (src_reg + off) | |
1903 | * with different pointer types: | |
1904 | * src_reg == ctx in one branch and | |
1905 | * src_reg == stack|map in some other branch. | |
1906 | * Reject it. | |
1907 | */ | |
1908 | verbose("same insn cannot be used with different pointers\n"); | |
1909 | return -EINVAL; | |
1910 | } | |
1911 | ||
17a52670 | 1912 | } else if (class == BPF_STX) { |
d691f9e8 AS |
1913 | enum bpf_reg_type dst_reg_type; |
1914 | ||
17a52670 AS |
1915 | if (BPF_MODE(insn->code) == BPF_XADD) { |
1916 | err = check_xadd(env, insn); | |
1917 | if (err) | |
1918 | return err; | |
1919 | insn_idx++; | |
1920 | continue; | |
1921 | } | |
1922 | ||
17a52670 AS |
1923 | /* check src1 operand */ |
1924 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1925 | if (err) | |
1926 | return err; | |
1927 | /* check src2 operand */ | |
1928 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1929 | if (err) | |
1930 | return err; | |
1931 | ||
d691f9e8 AS |
1932 | dst_reg_type = regs[insn->dst_reg].type; |
1933 | ||
17a52670 AS |
1934 | /* check that memory (dst_reg + off) is writeable */ |
1935 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
1936 | BPF_SIZE(insn->code), BPF_WRITE, | |
1937 | insn->src_reg); | |
1938 | if (err) | |
1939 | return err; | |
1940 | ||
d691f9e8 AS |
1941 | if (insn->imm == 0) { |
1942 | insn->imm = dst_reg_type; | |
1943 | } else if (dst_reg_type != insn->imm && | |
1944 | (dst_reg_type == PTR_TO_CTX || | |
1945 | insn->imm == PTR_TO_CTX)) { | |
1946 | verbose("same insn cannot be used with different pointers\n"); | |
1947 | return -EINVAL; | |
1948 | } | |
1949 | ||
17a52670 AS |
1950 | } else if (class == BPF_ST) { |
1951 | if (BPF_MODE(insn->code) != BPF_MEM || | |
1952 | insn->src_reg != BPF_REG_0) { | |
1953 | verbose("BPF_ST uses reserved fields\n"); | |
1954 | return -EINVAL; | |
1955 | } | |
1956 | /* check src operand */ | |
1957 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1958 | if (err) | |
1959 | return err; | |
1960 | ||
1961 | /* check that memory (dst_reg + off) is writeable */ | |
1962 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
1963 | BPF_SIZE(insn->code), BPF_WRITE, | |
1964 | -1); | |
1965 | if (err) | |
1966 | return err; | |
1967 | ||
1968 | } else if (class == BPF_JMP) { | |
1969 | u8 opcode = BPF_OP(insn->code); | |
1970 | ||
1971 | if (opcode == BPF_CALL) { | |
1972 | if (BPF_SRC(insn->code) != BPF_K || | |
1973 | insn->off != 0 || | |
1974 | insn->src_reg != BPF_REG_0 || | |
1975 | insn->dst_reg != BPF_REG_0) { | |
1976 | verbose("BPF_CALL uses reserved fields\n"); | |
1977 | return -EINVAL; | |
1978 | } | |
1979 | ||
1980 | err = check_call(env, insn->imm); | |
1981 | if (err) | |
1982 | return err; | |
1983 | ||
1984 | } else if (opcode == BPF_JA) { | |
1985 | if (BPF_SRC(insn->code) != BPF_K || | |
1986 | insn->imm != 0 || | |
1987 | insn->src_reg != BPF_REG_0 || | |
1988 | insn->dst_reg != BPF_REG_0) { | |
1989 | verbose("BPF_JA uses reserved fields\n"); | |
1990 | return -EINVAL; | |
1991 | } | |
1992 | ||
1993 | insn_idx += insn->off + 1; | |
1994 | continue; | |
1995 | ||
1996 | } else if (opcode == BPF_EXIT) { | |
1997 | if (BPF_SRC(insn->code) != BPF_K || | |
1998 | insn->imm != 0 || | |
1999 | insn->src_reg != BPF_REG_0 || | |
2000 | insn->dst_reg != BPF_REG_0) { | |
2001 | verbose("BPF_EXIT uses reserved fields\n"); | |
2002 | return -EINVAL; | |
2003 | } | |
2004 | ||
2005 | /* eBPF calling convetion is such that R0 is used | |
2006 | * to return the value from eBPF program. | |
2007 | * Make sure that it's readable at this time | |
2008 | * of bpf_exit, which means that program wrote | |
2009 | * something into it earlier | |
2010 | */ | |
2011 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); | |
2012 | if (err) | |
2013 | return err; | |
2014 | ||
1be7f75d AS |
2015 | if (is_pointer_value(env, BPF_REG_0)) { |
2016 | verbose("R0 leaks addr as return value\n"); | |
2017 | return -EACCES; | |
2018 | } | |
2019 | ||
f1bca824 | 2020 | process_bpf_exit: |
17a52670 AS |
2021 | insn_idx = pop_stack(env, &prev_insn_idx); |
2022 | if (insn_idx < 0) { | |
2023 | break; | |
2024 | } else { | |
2025 | do_print_state = true; | |
2026 | continue; | |
2027 | } | |
2028 | } else { | |
2029 | err = check_cond_jmp_op(env, insn, &insn_idx); | |
2030 | if (err) | |
2031 | return err; | |
2032 | } | |
2033 | } else if (class == BPF_LD) { | |
2034 | u8 mode = BPF_MODE(insn->code); | |
2035 | ||
2036 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
2037 | err = check_ld_abs(env, insn); |
2038 | if (err) | |
2039 | return err; | |
2040 | ||
17a52670 AS |
2041 | } else if (mode == BPF_IMM) { |
2042 | err = check_ld_imm(env, insn); | |
2043 | if (err) | |
2044 | return err; | |
2045 | ||
2046 | insn_idx++; | |
2047 | } else { | |
2048 | verbose("invalid BPF_LD mode\n"); | |
2049 | return -EINVAL; | |
2050 | } | |
2051 | } else { | |
2052 | verbose("unknown insn class %d\n", class); | |
2053 | return -EINVAL; | |
2054 | } | |
2055 | ||
2056 | insn_idx++; | |
2057 | } | |
2058 | ||
2059 | return 0; | |
2060 | } | |
2061 | ||
0246e64d AS |
2062 | /* look for pseudo eBPF instructions that access map FDs and |
2063 | * replace them with actual map pointers | |
2064 | */ | |
2065 | static int replace_map_fd_with_map_ptr(struct verifier_env *env) | |
2066 | { | |
2067 | struct bpf_insn *insn = env->prog->insnsi; | |
2068 | int insn_cnt = env->prog->len; | |
2069 | int i, j; | |
2070 | ||
2071 | for (i = 0; i < insn_cnt; i++, insn++) { | |
9bac3d6d | 2072 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 2073 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
9bac3d6d AS |
2074 | verbose("BPF_LDX uses reserved fields\n"); |
2075 | return -EINVAL; | |
2076 | } | |
2077 | ||
d691f9e8 AS |
2078 | if (BPF_CLASS(insn->code) == BPF_STX && |
2079 | ((BPF_MODE(insn->code) != BPF_MEM && | |
2080 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
2081 | verbose("BPF_STX uses reserved fields\n"); | |
2082 | return -EINVAL; | |
2083 | } | |
2084 | ||
0246e64d AS |
2085 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
2086 | struct bpf_map *map; | |
2087 | struct fd f; | |
2088 | ||
2089 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
2090 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
2091 | insn[1].off != 0) { | |
2092 | verbose("invalid bpf_ld_imm64 insn\n"); | |
2093 | return -EINVAL; | |
2094 | } | |
2095 | ||
2096 | if (insn->src_reg == 0) | |
2097 | /* valid generic load 64-bit imm */ | |
2098 | goto next_insn; | |
2099 | ||
2100 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { | |
2101 | verbose("unrecognized bpf_ld_imm64 insn\n"); | |
2102 | return -EINVAL; | |
2103 | } | |
2104 | ||
2105 | f = fdget(insn->imm); | |
c2101297 | 2106 | map = __bpf_map_get(f); |
0246e64d AS |
2107 | if (IS_ERR(map)) { |
2108 | verbose("fd %d is not pointing to valid bpf_map\n", | |
2109 | insn->imm); | |
0246e64d AS |
2110 | return PTR_ERR(map); |
2111 | } | |
2112 | ||
2113 | /* store map pointer inside BPF_LD_IMM64 instruction */ | |
2114 | insn[0].imm = (u32) (unsigned long) map; | |
2115 | insn[1].imm = ((u64) (unsigned long) map) >> 32; | |
2116 | ||
2117 | /* check whether we recorded this map already */ | |
2118 | for (j = 0; j < env->used_map_cnt; j++) | |
2119 | if (env->used_maps[j] == map) { | |
2120 | fdput(f); | |
2121 | goto next_insn; | |
2122 | } | |
2123 | ||
2124 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
2125 | fdput(f); | |
2126 | return -E2BIG; | |
2127 | } | |
2128 | ||
0246e64d AS |
2129 | /* hold the map. If the program is rejected by verifier, |
2130 | * the map will be released by release_maps() or it | |
2131 | * will be used by the valid program until it's unloaded | |
2132 | * and all maps are released in free_bpf_prog_info() | |
2133 | */ | |
92117d84 AS |
2134 | map = bpf_map_inc(map, false); |
2135 | if (IS_ERR(map)) { | |
2136 | fdput(f); | |
2137 | return PTR_ERR(map); | |
2138 | } | |
2139 | env->used_maps[env->used_map_cnt++] = map; | |
2140 | ||
0246e64d AS |
2141 | fdput(f); |
2142 | next_insn: | |
2143 | insn++; | |
2144 | i++; | |
2145 | } | |
2146 | } | |
2147 | ||
2148 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
2149 | * 'struct bpf_map *' into a register instead of user map_fd. | |
2150 | * These pointers will be used later by verifier to validate map access. | |
2151 | */ | |
2152 | return 0; | |
2153 | } | |
2154 | ||
2155 | /* drop refcnt of maps used by the rejected program */ | |
2156 | static void release_maps(struct verifier_env *env) | |
2157 | { | |
2158 | int i; | |
2159 | ||
2160 | for (i = 0; i < env->used_map_cnt; i++) | |
2161 | bpf_map_put(env->used_maps[i]); | |
2162 | } | |
2163 | ||
2164 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
2165 | static void convert_pseudo_ld_imm64(struct verifier_env *env) | |
2166 | { | |
2167 | struct bpf_insn *insn = env->prog->insnsi; | |
2168 | int insn_cnt = env->prog->len; | |
2169 | int i; | |
2170 | ||
2171 | for (i = 0; i < insn_cnt; i++, insn++) | |
2172 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
2173 | insn->src_reg = 0; | |
2174 | } | |
2175 | ||
9bac3d6d AS |
2176 | static void adjust_branches(struct bpf_prog *prog, int pos, int delta) |
2177 | { | |
2178 | struct bpf_insn *insn = prog->insnsi; | |
2179 | int insn_cnt = prog->len; | |
2180 | int i; | |
2181 | ||
2182 | for (i = 0; i < insn_cnt; i++, insn++) { | |
2183 | if (BPF_CLASS(insn->code) != BPF_JMP || | |
2184 | BPF_OP(insn->code) == BPF_CALL || | |
2185 | BPF_OP(insn->code) == BPF_EXIT) | |
2186 | continue; | |
2187 | ||
2188 | /* adjust offset of jmps if necessary */ | |
2189 | if (i < pos && i + insn->off + 1 > pos) | |
2190 | insn->off += delta; | |
a1b14d27 | 2191 | else if (i > pos + delta && i + insn->off + 1 <= pos + delta) |
9bac3d6d AS |
2192 | insn->off -= delta; |
2193 | } | |
2194 | } | |
2195 | ||
2196 | /* convert load instructions that access fields of 'struct __sk_buff' | |
2197 | * into sequence of instructions that access fields of 'struct sk_buff' | |
2198 | */ | |
2199 | static int convert_ctx_accesses(struct verifier_env *env) | |
2200 | { | |
2201 | struct bpf_insn *insn = env->prog->insnsi; | |
2202 | int insn_cnt = env->prog->len; | |
2203 | struct bpf_insn insn_buf[16]; | |
2204 | struct bpf_prog *new_prog; | |
2205 | u32 cnt; | |
2206 | int i; | |
d691f9e8 | 2207 | enum bpf_access_type type; |
9bac3d6d AS |
2208 | |
2209 | if (!env->prog->aux->ops->convert_ctx_access) | |
2210 | return 0; | |
2211 | ||
2212 | for (i = 0; i < insn_cnt; i++, insn++) { | |
d691f9e8 AS |
2213 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_W)) |
2214 | type = BPF_READ; | |
2215 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_W)) | |
2216 | type = BPF_WRITE; | |
2217 | else | |
9bac3d6d AS |
2218 | continue; |
2219 | ||
2220 | if (insn->imm != PTR_TO_CTX) { | |
2221 | /* clear internal mark */ | |
2222 | insn->imm = 0; | |
2223 | continue; | |
2224 | } | |
2225 | ||
2226 | cnt = env->prog->aux->ops-> | |
d691f9e8 | 2227 | convert_ctx_access(type, insn->dst_reg, insn->src_reg, |
ff936a04 | 2228 | insn->off, insn_buf, env->prog); |
9bac3d6d AS |
2229 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
2230 | verbose("bpf verifier is misconfigured\n"); | |
2231 | return -EINVAL; | |
2232 | } | |
2233 | ||
2234 | if (cnt == 1) { | |
2235 | memcpy(insn, insn_buf, sizeof(*insn)); | |
2236 | continue; | |
2237 | } | |
2238 | ||
2239 | /* several new insns need to be inserted. Make room for them */ | |
2240 | insn_cnt += cnt - 1; | |
2241 | new_prog = bpf_prog_realloc(env->prog, | |
2242 | bpf_prog_size(insn_cnt), | |
2243 | GFP_USER); | |
2244 | if (!new_prog) | |
2245 | return -ENOMEM; | |
2246 | ||
2247 | new_prog->len = insn_cnt; | |
2248 | ||
2249 | memmove(new_prog->insnsi + i + cnt, new_prog->insns + i + 1, | |
2250 | sizeof(*insn) * (insn_cnt - i - cnt)); | |
2251 | ||
2252 | /* copy substitute insns in place of load instruction */ | |
2253 | memcpy(new_prog->insnsi + i, insn_buf, sizeof(*insn) * cnt); | |
2254 | ||
2255 | /* adjust branches in the whole program */ | |
2256 | adjust_branches(new_prog, i, cnt - 1); | |
2257 | ||
2258 | /* keep walking new program and skip insns we just inserted */ | |
2259 | env->prog = new_prog; | |
2260 | insn = new_prog->insnsi + i + cnt - 1; | |
2261 | i += cnt - 1; | |
2262 | } | |
2263 | ||
2264 | return 0; | |
2265 | } | |
2266 | ||
f1bca824 AS |
2267 | static void free_states(struct verifier_env *env) |
2268 | { | |
2269 | struct verifier_state_list *sl, *sln; | |
2270 | int i; | |
2271 | ||
2272 | if (!env->explored_states) | |
2273 | return; | |
2274 | ||
2275 | for (i = 0; i < env->prog->len; i++) { | |
2276 | sl = env->explored_states[i]; | |
2277 | ||
2278 | if (sl) | |
2279 | while (sl != STATE_LIST_MARK) { | |
2280 | sln = sl->next; | |
2281 | kfree(sl); | |
2282 | sl = sln; | |
2283 | } | |
2284 | } | |
2285 | ||
2286 | kfree(env->explored_states); | |
2287 | } | |
2288 | ||
9bac3d6d | 2289 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
51580e79 | 2290 | { |
cbd35700 AS |
2291 | char __user *log_ubuf = NULL; |
2292 | struct verifier_env *env; | |
51580e79 AS |
2293 | int ret = -EINVAL; |
2294 | ||
9bac3d6d | 2295 | if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS) |
cbd35700 AS |
2296 | return -E2BIG; |
2297 | ||
2298 | /* 'struct verifier_env' can be global, but since it's not small, | |
2299 | * allocate/free it every time bpf_check() is called | |
2300 | */ | |
2301 | env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL); | |
2302 | if (!env) | |
2303 | return -ENOMEM; | |
2304 | ||
9bac3d6d | 2305 | env->prog = *prog; |
0246e64d | 2306 | |
cbd35700 AS |
2307 | /* grab the mutex to protect few globals used by verifier */ |
2308 | mutex_lock(&bpf_verifier_lock); | |
2309 | ||
2310 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
2311 | /* user requested verbose verifier output | |
2312 | * and supplied buffer to store the verification trace | |
2313 | */ | |
2314 | log_level = attr->log_level; | |
2315 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; | |
2316 | log_size = attr->log_size; | |
2317 | log_len = 0; | |
2318 | ||
2319 | ret = -EINVAL; | |
2320 | /* log_* values have to be sane */ | |
2321 | if (log_size < 128 || log_size > UINT_MAX >> 8 || | |
2322 | log_level == 0 || log_ubuf == NULL) | |
2323 | goto free_env; | |
2324 | ||
2325 | ret = -ENOMEM; | |
2326 | log_buf = vmalloc(log_size); | |
2327 | if (!log_buf) | |
2328 | goto free_env; | |
2329 | } else { | |
2330 | log_level = 0; | |
2331 | } | |
2332 | ||
0246e64d AS |
2333 | ret = replace_map_fd_with_map_ptr(env); |
2334 | if (ret < 0) | |
2335 | goto skip_full_check; | |
2336 | ||
9bac3d6d | 2337 | env->explored_states = kcalloc(env->prog->len, |
f1bca824 AS |
2338 | sizeof(struct verifier_state_list *), |
2339 | GFP_USER); | |
2340 | ret = -ENOMEM; | |
2341 | if (!env->explored_states) | |
2342 | goto skip_full_check; | |
2343 | ||
475fb78f AS |
2344 | ret = check_cfg(env); |
2345 | if (ret < 0) | |
2346 | goto skip_full_check; | |
2347 | ||
1be7f75d AS |
2348 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
2349 | ||
17a52670 | 2350 | ret = do_check(env); |
cbd35700 | 2351 | |
0246e64d | 2352 | skip_full_check: |
17a52670 | 2353 | while (pop_stack(env, NULL) >= 0); |
f1bca824 | 2354 | free_states(env); |
0246e64d | 2355 | |
9bac3d6d AS |
2356 | if (ret == 0) |
2357 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
2358 | ret = convert_ctx_accesses(env); | |
2359 | ||
cbd35700 AS |
2360 | if (log_level && log_len >= log_size - 1) { |
2361 | BUG_ON(log_len >= log_size); | |
2362 | /* verifier log exceeded user supplied buffer */ | |
2363 | ret = -ENOSPC; | |
2364 | /* fall through to return what was recorded */ | |
2365 | } | |
2366 | ||
2367 | /* copy verifier log back to user space including trailing zero */ | |
2368 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { | |
2369 | ret = -EFAULT; | |
2370 | goto free_log_buf; | |
2371 | } | |
2372 | ||
0246e64d AS |
2373 | if (ret == 0 && env->used_map_cnt) { |
2374 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
2375 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
2376 | sizeof(env->used_maps[0]), | |
2377 | GFP_KERNEL); | |
0246e64d | 2378 | |
9bac3d6d | 2379 | if (!env->prog->aux->used_maps) { |
0246e64d AS |
2380 | ret = -ENOMEM; |
2381 | goto free_log_buf; | |
2382 | } | |
2383 | ||
9bac3d6d | 2384 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 2385 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 2386 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
2387 | |
2388 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
2389 | * bpf_ld_imm64 instructions | |
2390 | */ | |
2391 | convert_pseudo_ld_imm64(env); | |
2392 | } | |
cbd35700 AS |
2393 | |
2394 | free_log_buf: | |
2395 | if (log_level) | |
2396 | vfree(log_buf); | |
2397 | free_env: | |
9bac3d6d | 2398 | if (!env->prog->aux->used_maps) |
0246e64d AS |
2399 | /* if we didn't copy map pointers into bpf_prog_info, release |
2400 | * them now. Otherwise free_bpf_prog_info() will release them. | |
2401 | */ | |
2402 | release_maps(env); | |
9bac3d6d | 2403 | *prog = env->prog; |
cbd35700 AS |
2404 | kfree(env); |
2405 | mutex_unlock(&bpf_verifier_lock); | |
51580e79 AS |
2406 | return ret; |
2407 | } |