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 */ | |
145 | int imm; | |
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 | ||
205 | /* verbose verifier prints what it's seeing | |
206 | * bpf_check() is called under lock, so no race to access these global vars | |
207 | */ | |
208 | static u32 log_level, log_size, log_len; | |
209 | static char *log_buf; | |
210 | ||
211 | static DEFINE_MUTEX(bpf_verifier_lock); | |
212 | ||
213 | /* log_level controls verbosity level of eBPF verifier. | |
214 | * verbose() is used to dump the verification trace to the log, so the user | |
215 | * can figure out what's wrong with the program | |
216 | */ | |
1d056d9c | 217 | static __printf(1, 2) void verbose(const char *fmt, ...) |
cbd35700 AS |
218 | { |
219 | va_list args; | |
220 | ||
221 | if (log_level == 0 || log_len >= log_size - 1) | |
222 | return; | |
223 | ||
224 | va_start(args, fmt); | |
225 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); | |
226 | va_end(args); | |
227 | } | |
228 | ||
17a52670 AS |
229 | /* string representation of 'enum bpf_reg_type' */ |
230 | static const char * const reg_type_str[] = { | |
231 | [NOT_INIT] = "?", | |
232 | [UNKNOWN_VALUE] = "inv", | |
233 | [PTR_TO_CTX] = "ctx", | |
234 | [CONST_PTR_TO_MAP] = "map_ptr", | |
235 | [PTR_TO_MAP_VALUE] = "map_value", | |
236 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
237 | [FRAME_PTR] = "fp", | |
238 | [PTR_TO_STACK] = "fp", | |
239 | [CONST_IMM] = "imm", | |
240 | }; | |
241 | ||
35578d79 KX |
242 | static const struct { |
243 | int map_type; | |
244 | int func_id; | |
245 | } func_limit[] = { | |
246 | {BPF_MAP_TYPE_PROG_ARRAY, BPF_FUNC_tail_call}, | |
247 | {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_read}, | |
a43eec30 | 248 | {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_output}, |
d5a3b1f6 | 249 | {BPF_MAP_TYPE_STACK_TRACE, BPF_FUNC_get_stackid}, |
35578d79 KX |
250 | }; |
251 | ||
17a52670 AS |
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) | |
263 | verbose("%d", env->cur_state.regs[i].imm); | |
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++; | |
457 | if (env->stack_size > 1024) { | |
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; | |
480 | regs[i].map_ptr = NULL; | |
481 | } | |
482 | ||
483 | /* frame pointer */ | |
484 | regs[BPF_REG_FP].type = FRAME_PTR; | |
485 | ||
486 | /* 1st arg to a function */ | |
487 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
488 | } | |
489 | ||
490 | static void mark_reg_unknown_value(struct reg_state *regs, u32 regno) | |
491 | { | |
492 | BUG_ON(regno >= MAX_BPF_REG); | |
493 | regs[regno].type = UNKNOWN_VALUE; | |
494 | regs[regno].imm = 0; | |
495 | regs[regno].map_ptr = NULL; | |
496 | } | |
497 | ||
498 | enum reg_arg_type { | |
499 | SRC_OP, /* register is used as source operand */ | |
500 | DST_OP, /* register is used as destination operand */ | |
501 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
502 | }; | |
503 | ||
504 | static int check_reg_arg(struct reg_state *regs, u32 regno, | |
505 | enum reg_arg_type t) | |
506 | { | |
507 | if (regno >= MAX_BPF_REG) { | |
508 | verbose("R%d is invalid\n", regno); | |
509 | return -EINVAL; | |
510 | } | |
511 | ||
512 | if (t == SRC_OP) { | |
513 | /* check whether register used as source operand can be read */ | |
514 | if (regs[regno].type == NOT_INIT) { | |
515 | verbose("R%d !read_ok\n", regno); | |
516 | return -EACCES; | |
517 | } | |
518 | } else { | |
519 | /* check whether register used as dest operand can be written to */ | |
520 | if (regno == BPF_REG_FP) { | |
521 | verbose("frame pointer is read only\n"); | |
522 | return -EACCES; | |
523 | } | |
524 | if (t == DST_OP) | |
525 | mark_reg_unknown_value(regs, regno); | |
526 | } | |
527 | return 0; | |
528 | } | |
529 | ||
530 | static int bpf_size_to_bytes(int bpf_size) | |
531 | { | |
532 | if (bpf_size == BPF_W) | |
533 | return 4; | |
534 | else if (bpf_size == BPF_H) | |
535 | return 2; | |
536 | else if (bpf_size == BPF_B) | |
537 | return 1; | |
538 | else if (bpf_size == BPF_DW) | |
539 | return 8; | |
540 | else | |
541 | return -EINVAL; | |
542 | } | |
543 | ||
1be7f75d AS |
544 | static bool is_spillable_regtype(enum bpf_reg_type type) |
545 | { | |
546 | switch (type) { | |
547 | case PTR_TO_MAP_VALUE: | |
548 | case PTR_TO_MAP_VALUE_OR_NULL: | |
549 | case PTR_TO_STACK: | |
550 | case PTR_TO_CTX: | |
551 | case FRAME_PTR: | |
552 | case CONST_PTR_TO_MAP: | |
553 | return true; | |
554 | default: | |
555 | return false; | |
556 | } | |
557 | } | |
558 | ||
17a52670 AS |
559 | /* check_stack_read/write functions track spill/fill of registers, |
560 | * stack boundary and alignment are checked in check_mem_access() | |
561 | */ | |
562 | static int check_stack_write(struct verifier_state *state, int off, int size, | |
563 | int value_regno) | |
564 | { | |
17a52670 | 565 | int i; |
9c399760 AS |
566 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
567 | * so it's aligned access and [off, off + size) are within stack limits | |
568 | */ | |
17a52670 AS |
569 | |
570 | if (value_regno >= 0 && | |
1be7f75d | 571 | is_spillable_regtype(state->regs[value_regno].type)) { |
17a52670 AS |
572 | |
573 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 574 | if (size != BPF_REG_SIZE) { |
17a52670 AS |
575 | verbose("invalid size of register spill\n"); |
576 | return -EACCES; | |
577 | } | |
578 | ||
17a52670 | 579 | /* save register state */ |
9c399760 AS |
580 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
581 | state->regs[value_regno]; | |
17a52670 | 582 | |
9c399760 AS |
583 | for (i = 0; i < BPF_REG_SIZE; i++) |
584 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; | |
585 | } else { | |
17a52670 | 586 | /* regular write of data into stack */ |
9c399760 AS |
587 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
588 | (struct reg_state) {}; | |
589 | ||
590 | for (i = 0; i < size; i++) | |
591 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; | |
17a52670 AS |
592 | } |
593 | return 0; | |
594 | } | |
595 | ||
596 | static int check_stack_read(struct verifier_state *state, int off, int size, | |
597 | int value_regno) | |
598 | { | |
9c399760 | 599 | u8 *slot_type; |
17a52670 | 600 | int i; |
17a52670 | 601 | |
9c399760 | 602 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
17a52670 | 603 | |
9c399760 AS |
604 | if (slot_type[0] == STACK_SPILL) { |
605 | if (size != BPF_REG_SIZE) { | |
17a52670 AS |
606 | verbose("invalid size of register spill\n"); |
607 | return -EACCES; | |
608 | } | |
9c399760 AS |
609 | for (i = 1; i < BPF_REG_SIZE; i++) { |
610 | if (slot_type[i] != STACK_SPILL) { | |
17a52670 AS |
611 | verbose("corrupted spill memory\n"); |
612 | return -EACCES; | |
613 | } | |
614 | } | |
615 | ||
616 | if (value_regno >= 0) | |
617 | /* restore register state from stack */ | |
9c399760 AS |
618 | state->regs[value_regno] = |
619 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; | |
17a52670 AS |
620 | return 0; |
621 | } else { | |
622 | for (i = 0; i < size; i++) { | |
9c399760 | 623 | if (slot_type[i] != STACK_MISC) { |
17a52670 AS |
624 | verbose("invalid read from stack off %d+%d size %d\n", |
625 | off, i, size); | |
626 | return -EACCES; | |
627 | } | |
628 | } | |
629 | if (value_regno >= 0) | |
630 | /* have read misc data from the stack */ | |
631 | mark_reg_unknown_value(state->regs, value_regno); | |
632 | return 0; | |
633 | } | |
634 | } | |
635 | ||
636 | /* check read/write into map element returned by bpf_map_lookup_elem() */ | |
637 | static int check_map_access(struct verifier_env *env, u32 regno, int off, | |
638 | int size) | |
639 | { | |
640 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; | |
641 | ||
642 | if (off < 0 || off + size > map->value_size) { | |
643 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", | |
644 | map->value_size, off, size); | |
645 | return -EACCES; | |
646 | } | |
647 | return 0; | |
648 | } | |
649 | ||
650 | /* check access to 'struct bpf_context' fields */ | |
651 | static int check_ctx_access(struct verifier_env *env, int off, int size, | |
652 | enum bpf_access_type t) | |
653 | { | |
654 | if (env->prog->aux->ops->is_valid_access && | |
655 | env->prog->aux->ops->is_valid_access(off, size, t)) | |
656 | return 0; | |
657 | ||
658 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); | |
659 | return -EACCES; | |
660 | } | |
661 | ||
1be7f75d AS |
662 | static bool is_pointer_value(struct verifier_env *env, int regno) |
663 | { | |
664 | if (env->allow_ptr_leaks) | |
665 | return false; | |
666 | ||
667 | switch (env->cur_state.regs[regno].type) { | |
668 | case UNKNOWN_VALUE: | |
669 | case CONST_IMM: | |
670 | return false; | |
671 | default: | |
672 | return true; | |
673 | } | |
674 | } | |
675 | ||
17a52670 AS |
676 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
677 | * if t==write, value_regno is a register which value is stored into memory | |
678 | * if t==read, value_regno is a register which will receive the value from memory | |
679 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
680 | * if t==read && value_regno==-1, don't care what we read from memory | |
681 | */ | |
682 | static int check_mem_access(struct verifier_env *env, u32 regno, int off, | |
683 | int bpf_size, enum bpf_access_type t, | |
684 | int value_regno) | |
685 | { | |
686 | struct verifier_state *state = &env->cur_state; | |
687 | int size, err = 0; | |
688 | ||
24b4d2ab AG |
689 | if (state->regs[regno].type == PTR_TO_STACK) |
690 | off += state->regs[regno].imm; | |
691 | ||
17a52670 AS |
692 | size = bpf_size_to_bytes(bpf_size); |
693 | if (size < 0) | |
694 | return size; | |
695 | ||
696 | if (off % size != 0) { | |
697 | verbose("misaligned access off %d size %d\n", off, size); | |
698 | return -EACCES; | |
699 | } | |
700 | ||
701 | if (state->regs[regno].type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
702 | if (t == BPF_WRITE && value_regno >= 0 && |
703 | is_pointer_value(env, value_regno)) { | |
704 | verbose("R%d leaks addr into map\n", value_regno); | |
705 | return -EACCES; | |
706 | } | |
17a52670 AS |
707 | err = check_map_access(env, regno, off, size); |
708 | if (!err && t == BPF_READ && value_regno >= 0) | |
709 | mark_reg_unknown_value(state->regs, value_regno); | |
710 | ||
711 | } else if (state->regs[regno].type == PTR_TO_CTX) { | |
1be7f75d AS |
712 | if (t == BPF_WRITE && value_regno >= 0 && |
713 | is_pointer_value(env, value_regno)) { | |
714 | verbose("R%d leaks addr into ctx\n", value_regno); | |
715 | return -EACCES; | |
716 | } | |
17a52670 AS |
717 | err = check_ctx_access(env, off, size, t); |
718 | if (!err && t == BPF_READ && value_regno >= 0) | |
719 | mark_reg_unknown_value(state->regs, value_regno); | |
720 | ||
24b4d2ab AG |
721 | } else if (state->regs[regno].type == FRAME_PTR || |
722 | state->regs[regno].type == PTR_TO_STACK) { | |
17a52670 AS |
723 | if (off >= 0 || off < -MAX_BPF_STACK) { |
724 | verbose("invalid stack off=%d size=%d\n", off, size); | |
725 | return -EACCES; | |
726 | } | |
1be7f75d AS |
727 | if (t == BPF_WRITE) { |
728 | if (!env->allow_ptr_leaks && | |
729 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && | |
730 | size != BPF_REG_SIZE) { | |
731 | verbose("attempt to corrupt spilled pointer on stack\n"); | |
732 | return -EACCES; | |
733 | } | |
17a52670 | 734 | err = check_stack_write(state, off, size, value_regno); |
1be7f75d | 735 | } else { |
17a52670 | 736 | err = check_stack_read(state, off, size, value_regno); |
1be7f75d | 737 | } |
17a52670 AS |
738 | } else { |
739 | verbose("R%d invalid mem access '%s'\n", | |
740 | regno, reg_type_str[state->regs[regno].type]); | |
741 | return -EACCES; | |
742 | } | |
743 | return err; | |
744 | } | |
745 | ||
746 | static int check_xadd(struct verifier_env *env, struct bpf_insn *insn) | |
747 | { | |
748 | struct reg_state *regs = env->cur_state.regs; | |
749 | int err; | |
750 | ||
751 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
752 | insn->imm != 0) { | |
753 | verbose("BPF_XADD uses reserved fields\n"); | |
754 | return -EINVAL; | |
755 | } | |
756 | ||
757 | /* check src1 operand */ | |
758 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
759 | if (err) | |
760 | return err; | |
761 | ||
762 | /* check src2 operand */ | |
763 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
764 | if (err) | |
765 | return err; | |
766 | ||
767 | /* check whether atomic_add can read the memory */ | |
768 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
769 | BPF_SIZE(insn->code), BPF_READ, -1); | |
770 | if (err) | |
771 | return err; | |
772 | ||
773 | /* check whether atomic_add can write into the same memory */ | |
774 | return check_mem_access(env, insn->dst_reg, insn->off, | |
775 | BPF_SIZE(insn->code), BPF_WRITE, -1); | |
776 | } | |
777 | ||
778 | /* when register 'regno' is passed into function that will read 'access_size' | |
779 | * bytes from that pointer, make sure that it's within stack boundary | |
780 | * and all elements of stack are initialized | |
781 | */ | |
8e2fe1d9 DB |
782 | static int check_stack_boundary(struct verifier_env *env, int regno, |
783 | int access_size, bool zero_size_allowed) | |
17a52670 AS |
784 | { |
785 | struct verifier_state *state = &env->cur_state; | |
786 | struct reg_state *regs = state->regs; | |
787 | int off, i; | |
788 | ||
8e2fe1d9 DB |
789 | if (regs[regno].type != PTR_TO_STACK) { |
790 | if (zero_size_allowed && access_size == 0 && | |
791 | regs[regno].type == CONST_IMM && | |
792 | regs[regno].imm == 0) | |
793 | return 0; | |
794 | ||
795 | verbose("R%d type=%s expected=%s\n", regno, | |
796 | reg_type_str[regs[regno].type], | |
797 | reg_type_str[PTR_TO_STACK]); | |
17a52670 | 798 | return -EACCES; |
8e2fe1d9 | 799 | } |
17a52670 AS |
800 | |
801 | off = regs[regno].imm; | |
802 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
803 | access_size <= 0) { | |
804 | verbose("invalid stack type R%d off=%d access_size=%d\n", | |
805 | regno, off, access_size); | |
806 | return -EACCES; | |
807 | } | |
808 | ||
809 | for (i = 0; i < access_size; i++) { | |
9c399760 | 810 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
17a52670 AS |
811 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
812 | off, i, access_size); | |
813 | return -EACCES; | |
814 | } | |
815 | } | |
816 | return 0; | |
817 | } | |
818 | ||
819 | static int check_func_arg(struct verifier_env *env, u32 regno, | |
820 | enum bpf_arg_type arg_type, struct bpf_map **mapp) | |
821 | { | |
822 | struct reg_state *reg = env->cur_state.regs + regno; | |
823 | enum bpf_reg_type expected_type; | |
824 | int err = 0; | |
825 | ||
80f1d68c | 826 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
827 | return 0; |
828 | ||
829 | if (reg->type == NOT_INIT) { | |
830 | verbose("R%d !read_ok\n", regno); | |
831 | return -EACCES; | |
832 | } | |
833 | ||
1be7f75d AS |
834 | if (arg_type == ARG_ANYTHING) { |
835 | if (is_pointer_value(env, regno)) { | |
836 | verbose("R%d leaks addr into helper function\n", regno); | |
837 | return -EACCES; | |
838 | } | |
80f1d68c | 839 | return 0; |
1be7f75d | 840 | } |
80f1d68c | 841 | |
8e2fe1d9 | 842 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
17a52670 AS |
843 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
844 | expected_type = PTR_TO_STACK; | |
8e2fe1d9 DB |
845 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
846 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { | |
17a52670 AS |
847 | expected_type = CONST_IMM; |
848 | } else if (arg_type == ARG_CONST_MAP_PTR) { | |
849 | expected_type = CONST_PTR_TO_MAP; | |
608cd71a AS |
850 | } else if (arg_type == ARG_PTR_TO_CTX) { |
851 | expected_type = PTR_TO_CTX; | |
8e2fe1d9 DB |
852 | } else if (arg_type == ARG_PTR_TO_STACK) { |
853 | expected_type = PTR_TO_STACK; | |
854 | /* One exception here. In case function allows for NULL to be | |
855 | * passed in as argument, it's a CONST_IMM type. Final test | |
856 | * happens during stack boundary checking. | |
857 | */ | |
858 | if (reg->type == CONST_IMM && reg->imm == 0) | |
859 | expected_type = CONST_IMM; | |
17a52670 AS |
860 | } else { |
861 | verbose("unsupported arg_type %d\n", arg_type); | |
862 | return -EFAULT; | |
863 | } | |
864 | ||
865 | if (reg->type != expected_type) { | |
866 | verbose("R%d type=%s expected=%s\n", regno, | |
867 | reg_type_str[reg->type], reg_type_str[expected_type]); | |
868 | return -EACCES; | |
869 | } | |
870 | ||
871 | if (arg_type == ARG_CONST_MAP_PTR) { | |
872 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
873 | *mapp = reg->map_ptr; | |
874 | ||
875 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { | |
876 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
877 | * check that [key, key + map->key_size) are within | |
878 | * stack limits and initialized | |
879 | */ | |
880 | if (!*mapp) { | |
881 | /* in function declaration map_ptr must come before | |
882 | * map_key, so that it's verified and known before | |
883 | * we have to check map_key here. Otherwise it means | |
884 | * that kernel subsystem misconfigured verifier | |
885 | */ | |
886 | verbose("invalid map_ptr to access map->key\n"); | |
887 | return -EACCES; | |
888 | } | |
8e2fe1d9 DB |
889 | err = check_stack_boundary(env, regno, (*mapp)->key_size, |
890 | false); | |
17a52670 AS |
891 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
892 | /* bpf_map_xxx(..., map_ptr, ..., value) call: | |
893 | * check [value, value + map->value_size) validity | |
894 | */ | |
895 | if (!*mapp) { | |
896 | /* kernel subsystem misconfigured verifier */ | |
897 | verbose("invalid map_ptr to access map->value\n"); | |
898 | return -EACCES; | |
899 | } | |
8e2fe1d9 DB |
900 | err = check_stack_boundary(env, regno, (*mapp)->value_size, |
901 | false); | |
902 | } else if (arg_type == ARG_CONST_STACK_SIZE || | |
903 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { | |
904 | bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO); | |
17a52670 | 905 | |
17a52670 AS |
906 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
907 | * from stack pointer 'buf'. Check it | |
908 | * note: regno == len, regno - 1 == buf | |
909 | */ | |
910 | if (regno == 0) { | |
911 | /* kernel subsystem misconfigured verifier */ | |
912 | verbose("ARG_CONST_STACK_SIZE cannot be first argument\n"); | |
913 | return -EACCES; | |
914 | } | |
8e2fe1d9 DB |
915 | err = check_stack_boundary(env, regno - 1, reg->imm, |
916 | zero_size_allowed); | |
17a52670 AS |
917 | } |
918 | ||
919 | return err; | |
920 | } | |
921 | ||
35578d79 KX |
922 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
923 | { | |
924 | bool bool_map, bool_func; | |
925 | int i; | |
926 | ||
927 | if (!map) | |
928 | return 0; | |
929 | ||
140d8b33 | 930 | for (i = 0; i < ARRAY_SIZE(func_limit); i++) { |
35578d79 KX |
931 | bool_map = (map->map_type == func_limit[i].map_type); |
932 | bool_func = (func_id == func_limit[i].func_id); | |
933 | /* only when map & func pair match it can continue. | |
934 | * don't allow any other map type to be passed into | |
935 | * the special func; | |
936 | */ | |
d5a3b1f6 AS |
937 | if (bool_func && bool_map != bool_func) { |
938 | verbose("cannot pass map_type %d into func %d\n", | |
939 | map->map_type, func_id); | |
35578d79 | 940 | return -EINVAL; |
d5a3b1f6 | 941 | } |
35578d79 KX |
942 | } |
943 | ||
944 | return 0; | |
945 | } | |
946 | ||
17a52670 AS |
947 | static int check_call(struct verifier_env *env, int func_id) |
948 | { | |
949 | struct verifier_state *state = &env->cur_state; | |
950 | const struct bpf_func_proto *fn = NULL; | |
951 | struct reg_state *regs = state->regs; | |
952 | struct bpf_map *map = NULL; | |
953 | struct reg_state *reg; | |
954 | int i, err; | |
955 | ||
956 | /* find function prototype */ | |
957 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
958 | verbose("invalid func %d\n", func_id); | |
959 | return -EINVAL; | |
960 | } | |
961 | ||
962 | if (env->prog->aux->ops->get_func_proto) | |
963 | fn = env->prog->aux->ops->get_func_proto(func_id); | |
964 | ||
965 | if (!fn) { | |
966 | verbose("unknown func %d\n", func_id); | |
967 | return -EINVAL; | |
968 | } | |
969 | ||
970 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 971 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
17a52670 AS |
972 | verbose("cannot call GPL only function from proprietary program\n"); |
973 | return -EINVAL; | |
974 | } | |
975 | ||
976 | /* check args */ | |
977 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &map); | |
978 | if (err) | |
979 | return err; | |
980 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &map); | |
981 | if (err) | |
982 | return err; | |
983 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &map); | |
984 | if (err) | |
985 | return err; | |
986 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &map); | |
987 | if (err) | |
988 | return err; | |
989 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &map); | |
990 | if (err) | |
991 | return err; | |
992 | ||
993 | /* reset caller saved regs */ | |
994 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
995 | reg = regs + caller_saved[i]; | |
996 | reg->type = NOT_INIT; | |
997 | reg->imm = 0; | |
998 | } | |
999 | ||
1000 | /* update return register */ | |
1001 | if (fn->ret_type == RET_INTEGER) { | |
1002 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
1003 | } else if (fn->ret_type == RET_VOID) { | |
1004 | regs[BPF_REG_0].type = NOT_INIT; | |
1005 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { | |
1006 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
1007 | /* remember map_ptr, so that check_map_access() | |
1008 | * can check 'value_size' boundary of memory access | |
1009 | * to map element returned from bpf_map_lookup_elem() | |
1010 | */ | |
1011 | if (map == NULL) { | |
1012 | verbose("kernel subsystem misconfigured verifier\n"); | |
1013 | return -EINVAL; | |
1014 | } | |
1015 | regs[BPF_REG_0].map_ptr = map; | |
1016 | } else { | |
1017 | verbose("unknown return type %d of func %d\n", | |
1018 | fn->ret_type, func_id); | |
1019 | return -EINVAL; | |
1020 | } | |
04fd61ab | 1021 | |
35578d79 KX |
1022 | err = check_map_func_compatibility(map, func_id); |
1023 | if (err) | |
1024 | return err; | |
04fd61ab | 1025 | |
17a52670 AS |
1026 | return 0; |
1027 | } | |
1028 | ||
1029 | /* check validity of 32-bit and 64-bit arithmetic operations */ | |
1be7f75d | 1030 | static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn) |
17a52670 | 1031 | { |
1be7f75d | 1032 | struct reg_state *regs = env->cur_state.regs; |
17a52670 AS |
1033 | u8 opcode = BPF_OP(insn->code); |
1034 | int err; | |
1035 | ||
1036 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
1037 | if (opcode == BPF_NEG) { | |
1038 | if (BPF_SRC(insn->code) != 0 || | |
1039 | insn->src_reg != BPF_REG_0 || | |
1040 | insn->off != 0 || insn->imm != 0) { | |
1041 | verbose("BPF_NEG uses reserved fields\n"); | |
1042 | return -EINVAL; | |
1043 | } | |
1044 | } else { | |
1045 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
1046 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) { | |
1047 | verbose("BPF_END uses reserved fields\n"); | |
1048 | return -EINVAL; | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | /* check src operand */ | |
1053 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1054 | if (err) | |
1055 | return err; | |
1056 | ||
1be7f75d AS |
1057 | if (is_pointer_value(env, insn->dst_reg)) { |
1058 | verbose("R%d pointer arithmetic prohibited\n", | |
1059 | insn->dst_reg); | |
1060 | return -EACCES; | |
1061 | } | |
1062 | ||
17a52670 AS |
1063 | /* check dest operand */ |
1064 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1065 | if (err) | |
1066 | return err; | |
1067 | ||
1068 | } else if (opcode == BPF_MOV) { | |
1069 | ||
1070 | if (BPF_SRC(insn->code) == BPF_X) { | |
1071 | if (insn->imm != 0 || insn->off != 0) { | |
1072 | verbose("BPF_MOV uses reserved fields\n"); | |
1073 | return -EINVAL; | |
1074 | } | |
1075 | ||
1076 | /* check src operand */ | |
1077 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1078 | if (err) | |
1079 | return err; | |
1080 | } else { | |
1081 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
1082 | verbose("BPF_MOV uses reserved fields\n"); | |
1083 | return -EINVAL; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | /* check dest operand */ | |
1088 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1089 | if (err) | |
1090 | return err; | |
1091 | ||
1092 | if (BPF_SRC(insn->code) == BPF_X) { | |
1093 | if (BPF_CLASS(insn->code) == BPF_ALU64) { | |
1094 | /* case: R1 = R2 | |
1095 | * copy register state to dest reg | |
1096 | */ | |
1097 | regs[insn->dst_reg] = regs[insn->src_reg]; | |
1098 | } else { | |
1be7f75d AS |
1099 | if (is_pointer_value(env, insn->src_reg)) { |
1100 | verbose("R%d partial copy of pointer\n", | |
1101 | insn->src_reg); | |
1102 | return -EACCES; | |
1103 | } | |
17a52670 AS |
1104 | regs[insn->dst_reg].type = UNKNOWN_VALUE; |
1105 | regs[insn->dst_reg].map_ptr = NULL; | |
1106 | } | |
1107 | } else { | |
1108 | /* case: R = imm | |
1109 | * remember the value we stored into this reg | |
1110 | */ | |
1111 | regs[insn->dst_reg].type = CONST_IMM; | |
1112 | regs[insn->dst_reg].imm = insn->imm; | |
1113 | } | |
1114 | ||
1115 | } else if (opcode > BPF_END) { | |
1116 | verbose("invalid BPF_ALU opcode %x\n", opcode); | |
1117 | return -EINVAL; | |
1118 | ||
1119 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
1120 | ||
1121 | bool stack_relative = false; | |
1122 | ||
1123 | if (BPF_SRC(insn->code) == BPF_X) { | |
1124 | if (insn->imm != 0 || insn->off != 0) { | |
1125 | verbose("BPF_ALU uses reserved fields\n"); | |
1126 | return -EINVAL; | |
1127 | } | |
1128 | /* check src1 operand */ | |
1129 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1130 | if (err) | |
1131 | return err; | |
1132 | } else { | |
1133 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
1134 | verbose("BPF_ALU uses reserved fields\n"); | |
1135 | return -EINVAL; | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | /* check src2 operand */ | |
1140 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1141 | if (err) | |
1142 | return err; | |
1143 | ||
1144 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
1145 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
1146 | verbose("div by zero\n"); | |
1147 | return -EINVAL; | |
1148 | } | |
1149 | ||
229394e8 RV |
1150 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
1151 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
1152 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
1153 | ||
1154 | if (insn->imm < 0 || insn->imm >= size) { | |
1155 | verbose("invalid shift %d\n", insn->imm); | |
1156 | return -EINVAL; | |
1157 | } | |
1158 | } | |
1159 | ||
17a52670 AS |
1160 | /* pattern match 'bpf_add Rx, imm' instruction */ |
1161 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && | |
1162 | regs[insn->dst_reg].type == FRAME_PTR && | |
1be7f75d | 1163 | BPF_SRC(insn->code) == BPF_K) { |
17a52670 | 1164 | stack_relative = true; |
1be7f75d AS |
1165 | } else if (is_pointer_value(env, insn->dst_reg)) { |
1166 | verbose("R%d pointer arithmetic prohibited\n", | |
1167 | insn->dst_reg); | |
1168 | return -EACCES; | |
1169 | } else if (BPF_SRC(insn->code) == BPF_X && | |
1170 | is_pointer_value(env, insn->src_reg)) { | |
1171 | verbose("R%d pointer arithmetic prohibited\n", | |
1172 | insn->src_reg); | |
1173 | return -EACCES; | |
1174 | } | |
17a52670 AS |
1175 | |
1176 | /* check dest operand */ | |
1177 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1178 | if (err) | |
1179 | return err; | |
1180 | ||
1181 | if (stack_relative) { | |
1182 | regs[insn->dst_reg].type = PTR_TO_STACK; | |
1183 | regs[insn->dst_reg].imm = insn->imm; | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | return 0; | |
1188 | } | |
1189 | ||
1190 | static int check_cond_jmp_op(struct verifier_env *env, | |
1191 | struct bpf_insn *insn, int *insn_idx) | |
1192 | { | |
1193 | struct reg_state *regs = env->cur_state.regs; | |
1194 | struct verifier_state *other_branch; | |
1195 | u8 opcode = BPF_OP(insn->code); | |
1196 | int err; | |
1197 | ||
1198 | if (opcode > BPF_EXIT) { | |
1199 | verbose("invalid BPF_JMP opcode %x\n", opcode); | |
1200 | return -EINVAL; | |
1201 | } | |
1202 | ||
1203 | if (BPF_SRC(insn->code) == BPF_X) { | |
1204 | if (insn->imm != 0) { | |
1205 | verbose("BPF_JMP uses reserved fields\n"); | |
1206 | return -EINVAL; | |
1207 | } | |
1208 | ||
1209 | /* check src1 operand */ | |
1210 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1211 | if (err) | |
1212 | return err; | |
1be7f75d AS |
1213 | |
1214 | if (is_pointer_value(env, insn->src_reg)) { | |
1215 | verbose("R%d pointer comparison prohibited\n", | |
1216 | insn->src_reg); | |
1217 | return -EACCES; | |
1218 | } | |
17a52670 AS |
1219 | } else { |
1220 | if (insn->src_reg != BPF_REG_0) { | |
1221 | verbose("BPF_JMP uses reserved fields\n"); | |
1222 | return -EINVAL; | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | /* check src2 operand */ | |
1227 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1228 | if (err) | |
1229 | return err; | |
1230 | ||
1231 | /* detect if R == 0 where R was initialized to zero earlier */ | |
1232 | if (BPF_SRC(insn->code) == BPF_K && | |
1233 | (opcode == BPF_JEQ || opcode == BPF_JNE) && | |
1234 | regs[insn->dst_reg].type == CONST_IMM && | |
1235 | regs[insn->dst_reg].imm == insn->imm) { | |
1236 | if (opcode == BPF_JEQ) { | |
1237 | /* if (imm == imm) goto pc+off; | |
1238 | * only follow the goto, ignore fall-through | |
1239 | */ | |
1240 | *insn_idx += insn->off; | |
1241 | return 0; | |
1242 | } else { | |
1243 | /* if (imm != imm) goto pc+off; | |
1244 | * only follow fall-through branch, since | |
1245 | * that's where the program will go | |
1246 | */ | |
1247 | return 0; | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); | |
1252 | if (!other_branch) | |
1253 | return -EFAULT; | |
1254 | ||
1255 | /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */ | |
1256 | if (BPF_SRC(insn->code) == BPF_K && | |
1257 | insn->imm == 0 && (opcode == BPF_JEQ || | |
1258 | opcode == BPF_JNE) && | |
1259 | regs[insn->dst_reg].type == PTR_TO_MAP_VALUE_OR_NULL) { | |
1260 | if (opcode == BPF_JEQ) { | |
1261 | /* next fallthrough insn can access memory via | |
1262 | * this register | |
1263 | */ | |
1264 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
1265 | /* branch targer cannot access it, since reg == 0 */ | |
1266 | other_branch->regs[insn->dst_reg].type = CONST_IMM; | |
1267 | other_branch->regs[insn->dst_reg].imm = 0; | |
1268 | } else { | |
1269 | other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
1270 | regs[insn->dst_reg].type = CONST_IMM; | |
1271 | regs[insn->dst_reg].imm = 0; | |
1272 | } | |
1be7f75d AS |
1273 | } else if (is_pointer_value(env, insn->dst_reg)) { |
1274 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); | |
1275 | return -EACCES; | |
17a52670 AS |
1276 | } else if (BPF_SRC(insn->code) == BPF_K && |
1277 | (opcode == BPF_JEQ || opcode == BPF_JNE)) { | |
1278 | ||
1279 | if (opcode == BPF_JEQ) { | |
1280 | /* detect if (R == imm) goto | |
1281 | * and in the target state recognize that R = imm | |
1282 | */ | |
1283 | other_branch->regs[insn->dst_reg].type = CONST_IMM; | |
1284 | other_branch->regs[insn->dst_reg].imm = insn->imm; | |
1285 | } else { | |
1286 | /* detect if (R != imm) goto | |
1287 | * and in the fall-through state recognize that R = imm | |
1288 | */ | |
1289 | regs[insn->dst_reg].type = CONST_IMM; | |
1290 | regs[insn->dst_reg].imm = insn->imm; | |
1291 | } | |
1292 | } | |
1293 | if (log_level) | |
1294 | print_verifier_state(env); | |
1295 | return 0; | |
1296 | } | |
1297 | ||
0246e64d AS |
1298 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
1299 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) | |
1300 | { | |
1301 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; | |
1302 | ||
1303 | return (struct bpf_map *) (unsigned long) imm64; | |
1304 | } | |
1305 | ||
17a52670 AS |
1306 | /* verify BPF_LD_IMM64 instruction */ |
1307 | static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn) | |
1308 | { | |
1309 | struct reg_state *regs = env->cur_state.regs; | |
1310 | int err; | |
1311 | ||
1312 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
1313 | verbose("invalid BPF_LD_IMM insn\n"); | |
1314 | return -EINVAL; | |
1315 | } | |
1316 | if (insn->off != 0) { | |
1317 | verbose("BPF_LD_IMM64 uses reserved fields\n"); | |
1318 | return -EINVAL; | |
1319 | } | |
1320 | ||
1321 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1322 | if (err) | |
1323 | return err; | |
1324 | ||
1325 | if (insn->src_reg == 0) | |
1326 | /* generic move 64-bit immediate into a register */ | |
1327 | return 0; | |
1328 | ||
1329 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ | |
1330 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); | |
1331 | ||
1332 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
1333 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); | |
1334 | return 0; | |
1335 | } | |
1336 | ||
96be4325 DB |
1337 | static bool may_access_skb(enum bpf_prog_type type) |
1338 | { | |
1339 | switch (type) { | |
1340 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
1341 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 1342 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
1343 | return true; |
1344 | default: | |
1345 | return false; | |
1346 | } | |
1347 | } | |
1348 | ||
ddd872bc AS |
1349 | /* verify safety of LD_ABS|LD_IND instructions: |
1350 | * - they can only appear in the programs where ctx == skb | |
1351 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
1352 | * preserve R6-R9, and store return value into R0 | |
1353 | * | |
1354 | * Implicit input: | |
1355 | * ctx == skb == R6 == CTX | |
1356 | * | |
1357 | * Explicit input: | |
1358 | * SRC == any register | |
1359 | * IMM == 32-bit immediate | |
1360 | * | |
1361 | * Output: | |
1362 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
1363 | */ | |
1364 | static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn) | |
1365 | { | |
1366 | struct reg_state *regs = env->cur_state.regs; | |
1367 | u8 mode = BPF_MODE(insn->code); | |
1368 | struct reg_state *reg; | |
1369 | int i, err; | |
1370 | ||
24701ece | 1371 | if (!may_access_skb(env->prog->type)) { |
96be4325 | 1372 | verbose("BPF_LD_ABS|IND instructions not allowed for this program type\n"); |
ddd872bc AS |
1373 | return -EINVAL; |
1374 | } | |
1375 | ||
1376 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || | |
d82bccc6 | 1377 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc AS |
1378 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
1379 | verbose("BPF_LD_ABS uses reserved fields\n"); | |
1380 | return -EINVAL; | |
1381 | } | |
1382 | ||
1383 | /* check whether implicit source operand (register R6) is readable */ | |
1384 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); | |
1385 | if (err) | |
1386 | return err; | |
1387 | ||
1388 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { | |
1389 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
1390 | return -EINVAL; | |
1391 | } | |
1392 | ||
1393 | if (mode == BPF_IND) { | |
1394 | /* check explicit source operand */ | |
1395 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1396 | if (err) | |
1397 | return err; | |
1398 | } | |
1399 | ||
1400 | /* reset caller saved regs to unreadable */ | |
1401 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
1402 | reg = regs + caller_saved[i]; | |
1403 | reg->type = NOT_INIT; | |
1404 | reg->imm = 0; | |
1405 | } | |
1406 | ||
1407 | /* mark destination R0 register as readable, since it contains | |
1408 | * the value fetched from the packet | |
1409 | */ | |
1410 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
1411 | return 0; | |
1412 | } | |
1413 | ||
475fb78f AS |
1414 | /* non-recursive DFS pseudo code |
1415 | * 1 procedure DFS-iterative(G,v): | |
1416 | * 2 label v as discovered | |
1417 | * 3 let S be a stack | |
1418 | * 4 S.push(v) | |
1419 | * 5 while S is not empty | |
1420 | * 6 t <- S.pop() | |
1421 | * 7 if t is what we're looking for: | |
1422 | * 8 return t | |
1423 | * 9 for all edges e in G.adjacentEdges(t) do | |
1424 | * 10 if edge e is already labelled | |
1425 | * 11 continue with the next edge | |
1426 | * 12 w <- G.adjacentVertex(t,e) | |
1427 | * 13 if vertex w is not discovered and not explored | |
1428 | * 14 label e as tree-edge | |
1429 | * 15 label w as discovered | |
1430 | * 16 S.push(w) | |
1431 | * 17 continue at 5 | |
1432 | * 18 else if vertex w is discovered | |
1433 | * 19 label e as back-edge | |
1434 | * 20 else | |
1435 | * 21 // vertex w is explored | |
1436 | * 22 label e as forward- or cross-edge | |
1437 | * 23 label t as explored | |
1438 | * 24 S.pop() | |
1439 | * | |
1440 | * convention: | |
1441 | * 0x10 - discovered | |
1442 | * 0x11 - discovered and fall-through edge labelled | |
1443 | * 0x12 - discovered and fall-through and branch edges labelled | |
1444 | * 0x20 - explored | |
1445 | */ | |
1446 | ||
1447 | enum { | |
1448 | DISCOVERED = 0x10, | |
1449 | EXPLORED = 0x20, | |
1450 | FALLTHROUGH = 1, | |
1451 | BRANCH = 2, | |
1452 | }; | |
1453 | ||
f1bca824 AS |
1454 | #define STATE_LIST_MARK ((struct verifier_state_list *) -1L) |
1455 | ||
475fb78f AS |
1456 | static int *insn_stack; /* stack of insns to process */ |
1457 | static int cur_stack; /* current stack index */ | |
1458 | static int *insn_state; | |
1459 | ||
1460 | /* t, w, e - match pseudo-code above: | |
1461 | * t - index of current instruction | |
1462 | * w - next instruction | |
1463 | * e - edge | |
1464 | */ | |
1465 | static int push_insn(int t, int w, int e, struct verifier_env *env) | |
1466 | { | |
1467 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) | |
1468 | return 0; | |
1469 | ||
1470 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
1471 | return 0; | |
1472 | ||
1473 | if (w < 0 || w >= env->prog->len) { | |
1474 | verbose("jump out of range from insn %d to %d\n", t, w); | |
1475 | return -EINVAL; | |
1476 | } | |
1477 | ||
f1bca824 AS |
1478 | if (e == BRANCH) |
1479 | /* mark branch target for state pruning */ | |
1480 | env->explored_states[w] = STATE_LIST_MARK; | |
1481 | ||
475fb78f AS |
1482 | if (insn_state[w] == 0) { |
1483 | /* tree-edge */ | |
1484 | insn_state[t] = DISCOVERED | e; | |
1485 | insn_state[w] = DISCOVERED; | |
1486 | if (cur_stack >= env->prog->len) | |
1487 | return -E2BIG; | |
1488 | insn_stack[cur_stack++] = w; | |
1489 | return 1; | |
1490 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
1491 | verbose("back-edge from insn %d to %d\n", t, w); | |
1492 | return -EINVAL; | |
1493 | } else if (insn_state[w] == EXPLORED) { | |
1494 | /* forward- or cross-edge */ | |
1495 | insn_state[t] = DISCOVERED | e; | |
1496 | } else { | |
1497 | verbose("insn state internal bug\n"); | |
1498 | return -EFAULT; | |
1499 | } | |
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | /* non-recursive depth-first-search to detect loops in BPF program | |
1504 | * loop == back-edge in directed graph | |
1505 | */ | |
1506 | static int check_cfg(struct verifier_env *env) | |
1507 | { | |
1508 | struct bpf_insn *insns = env->prog->insnsi; | |
1509 | int insn_cnt = env->prog->len; | |
1510 | int ret = 0; | |
1511 | int i, t; | |
1512 | ||
1513 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
1514 | if (!insn_state) | |
1515 | return -ENOMEM; | |
1516 | ||
1517 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
1518 | if (!insn_stack) { | |
1519 | kfree(insn_state); | |
1520 | return -ENOMEM; | |
1521 | } | |
1522 | ||
1523 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
1524 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
1525 | cur_stack = 1; | |
1526 | ||
1527 | peek_stack: | |
1528 | if (cur_stack == 0) | |
1529 | goto check_state; | |
1530 | t = insn_stack[cur_stack - 1]; | |
1531 | ||
1532 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { | |
1533 | u8 opcode = BPF_OP(insns[t].code); | |
1534 | ||
1535 | if (opcode == BPF_EXIT) { | |
1536 | goto mark_explored; | |
1537 | } else if (opcode == BPF_CALL) { | |
1538 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
1539 | if (ret == 1) | |
1540 | goto peek_stack; | |
1541 | else if (ret < 0) | |
1542 | goto err_free; | |
1543 | } else if (opcode == BPF_JA) { | |
1544 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
1545 | ret = -EINVAL; | |
1546 | goto err_free; | |
1547 | } | |
1548 | /* unconditional jump with single edge */ | |
1549 | ret = push_insn(t, t + insns[t].off + 1, | |
1550 | FALLTHROUGH, env); | |
1551 | if (ret == 1) | |
1552 | goto peek_stack; | |
1553 | else if (ret < 0) | |
1554 | goto err_free; | |
f1bca824 AS |
1555 | /* tell verifier to check for equivalent states |
1556 | * after every call and jump | |
1557 | */ | |
c3de6317 AS |
1558 | if (t + 1 < insn_cnt) |
1559 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
1560 | } else { |
1561 | /* conditional jump with two edges */ | |
1562 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
1563 | if (ret == 1) | |
1564 | goto peek_stack; | |
1565 | else if (ret < 0) | |
1566 | goto err_free; | |
1567 | ||
1568 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
1569 | if (ret == 1) | |
1570 | goto peek_stack; | |
1571 | else if (ret < 0) | |
1572 | goto err_free; | |
1573 | } | |
1574 | } else { | |
1575 | /* all other non-branch instructions with single | |
1576 | * fall-through edge | |
1577 | */ | |
1578 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
1579 | if (ret == 1) | |
1580 | goto peek_stack; | |
1581 | else if (ret < 0) | |
1582 | goto err_free; | |
1583 | } | |
1584 | ||
1585 | mark_explored: | |
1586 | insn_state[t] = EXPLORED; | |
1587 | if (cur_stack-- <= 0) { | |
1588 | verbose("pop stack internal bug\n"); | |
1589 | ret = -EFAULT; | |
1590 | goto err_free; | |
1591 | } | |
1592 | goto peek_stack; | |
1593 | ||
1594 | check_state: | |
1595 | for (i = 0; i < insn_cnt; i++) { | |
1596 | if (insn_state[i] != EXPLORED) { | |
1597 | verbose("unreachable insn %d\n", i); | |
1598 | ret = -EINVAL; | |
1599 | goto err_free; | |
1600 | } | |
1601 | } | |
1602 | ret = 0; /* cfg looks good */ | |
1603 | ||
1604 | err_free: | |
1605 | kfree(insn_state); | |
1606 | kfree(insn_stack); | |
1607 | return ret; | |
1608 | } | |
1609 | ||
f1bca824 AS |
1610 | /* compare two verifier states |
1611 | * | |
1612 | * all states stored in state_list are known to be valid, since | |
1613 | * verifier reached 'bpf_exit' instruction through them | |
1614 | * | |
1615 | * this function is called when verifier exploring different branches of | |
1616 | * execution popped from the state stack. If it sees an old state that has | |
1617 | * more strict register state and more strict stack state then this execution | |
1618 | * branch doesn't need to be explored further, since verifier already | |
1619 | * concluded that more strict state leads to valid finish. | |
1620 | * | |
1621 | * Therefore two states are equivalent if register state is more conservative | |
1622 | * and explored stack state is more conservative than the current one. | |
1623 | * Example: | |
1624 | * explored current | |
1625 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
1626 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
1627 | * | |
1628 | * In other words if current stack state (one being explored) has more | |
1629 | * valid slots than old one that already passed validation, it means | |
1630 | * the verifier can stop exploring and conclude that current state is valid too | |
1631 | * | |
1632 | * Similarly with registers. If explored state has register type as invalid | |
1633 | * whereas register type in current state is meaningful, it means that | |
1634 | * the current state will reach 'bpf_exit' instruction safely | |
1635 | */ | |
1636 | static bool states_equal(struct verifier_state *old, struct verifier_state *cur) | |
1637 | { | |
1638 | int i; | |
1639 | ||
1640 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1641 | if (memcmp(&old->regs[i], &cur->regs[i], | |
1642 | sizeof(old->regs[0])) != 0) { | |
1643 | if (old->regs[i].type == NOT_INIT || | |
32bf08a6 AS |
1644 | (old->regs[i].type == UNKNOWN_VALUE && |
1645 | cur->regs[i].type != NOT_INIT)) | |
f1bca824 AS |
1646 | continue; |
1647 | return false; | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | for (i = 0; i < MAX_BPF_STACK; i++) { | |
9c399760 AS |
1652 | if (old->stack_slot_type[i] == STACK_INVALID) |
1653 | continue; | |
1654 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) | |
1655 | /* Ex: old explored (safe) state has STACK_SPILL in | |
1656 | * this stack slot, but current has has STACK_MISC -> | |
1657 | * this verifier states are not equivalent, | |
1658 | * return false to continue verification of this path | |
1659 | */ | |
f1bca824 | 1660 | return false; |
9c399760 AS |
1661 | if (i % BPF_REG_SIZE) |
1662 | continue; | |
1663 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], | |
1664 | &cur->spilled_regs[i / BPF_REG_SIZE], | |
1665 | sizeof(old->spilled_regs[0]))) | |
1666 | /* when explored and current stack slot types are | |
1667 | * the same, check that stored pointers types | |
1668 | * are the same as well. | |
1669 | * Ex: explored safe path could have stored | |
1670 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -8} | |
1671 | * but current path has stored: | |
1672 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -16} | |
1673 | * such verifier states are not equivalent. | |
1674 | * return false to continue verification of this path | |
1675 | */ | |
1676 | return false; | |
1677 | else | |
1678 | continue; | |
f1bca824 AS |
1679 | } |
1680 | return true; | |
1681 | } | |
1682 | ||
1683 | static int is_state_visited(struct verifier_env *env, int insn_idx) | |
1684 | { | |
1685 | struct verifier_state_list *new_sl; | |
1686 | struct verifier_state_list *sl; | |
1687 | ||
1688 | sl = env->explored_states[insn_idx]; | |
1689 | if (!sl) | |
1690 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
1691 | * be doing state search here | |
1692 | */ | |
1693 | return 0; | |
1694 | ||
1695 | while (sl != STATE_LIST_MARK) { | |
1696 | if (states_equal(&sl->state, &env->cur_state)) | |
1697 | /* reached equivalent register/stack state, | |
1698 | * prune the search | |
1699 | */ | |
1700 | return 1; | |
1701 | sl = sl->next; | |
1702 | } | |
1703 | ||
1704 | /* there were no equivalent states, remember current one. | |
1705 | * technically the current state is not proven to be safe yet, | |
1706 | * but it will either reach bpf_exit (which means it's safe) or | |
1707 | * it will be rejected. Since there are no loops, we won't be | |
1708 | * seeing this 'insn_idx' instruction again on the way to bpf_exit | |
1709 | */ | |
1710 | new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER); | |
1711 | if (!new_sl) | |
1712 | return -ENOMEM; | |
1713 | ||
1714 | /* add new state to the head of linked list */ | |
1715 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); | |
1716 | new_sl->next = env->explored_states[insn_idx]; | |
1717 | env->explored_states[insn_idx] = new_sl; | |
1718 | return 0; | |
1719 | } | |
1720 | ||
17a52670 AS |
1721 | static int do_check(struct verifier_env *env) |
1722 | { | |
1723 | struct verifier_state *state = &env->cur_state; | |
1724 | struct bpf_insn *insns = env->prog->insnsi; | |
1725 | struct reg_state *regs = state->regs; | |
1726 | int insn_cnt = env->prog->len; | |
1727 | int insn_idx, prev_insn_idx = 0; | |
1728 | int insn_processed = 0; | |
1729 | bool do_print_state = false; | |
1730 | ||
1731 | init_reg_state(regs); | |
1732 | insn_idx = 0; | |
1733 | for (;;) { | |
1734 | struct bpf_insn *insn; | |
1735 | u8 class; | |
1736 | int err; | |
1737 | ||
1738 | if (insn_idx >= insn_cnt) { | |
1739 | verbose("invalid insn idx %d insn_cnt %d\n", | |
1740 | insn_idx, insn_cnt); | |
1741 | return -EFAULT; | |
1742 | } | |
1743 | ||
1744 | insn = &insns[insn_idx]; | |
1745 | class = BPF_CLASS(insn->code); | |
1746 | ||
1747 | if (++insn_processed > 32768) { | |
1748 | verbose("BPF program is too large. Proccessed %d insn\n", | |
1749 | insn_processed); | |
1750 | return -E2BIG; | |
1751 | } | |
1752 | ||
f1bca824 AS |
1753 | err = is_state_visited(env, insn_idx); |
1754 | if (err < 0) | |
1755 | return err; | |
1756 | if (err == 1) { | |
1757 | /* found equivalent state, can prune the search */ | |
1758 | if (log_level) { | |
1759 | if (do_print_state) | |
1760 | verbose("\nfrom %d to %d: safe\n", | |
1761 | prev_insn_idx, insn_idx); | |
1762 | else | |
1763 | verbose("%d: safe\n", insn_idx); | |
1764 | } | |
1765 | goto process_bpf_exit; | |
1766 | } | |
1767 | ||
17a52670 AS |
1768 | if (log_level && do_print_state) { |
1769 | verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx); | |
1770 | print_verifier_state(env); | |
1771 | do_print_state = false; | |
1772 | } | |
1773 | ||
1774 | if (log_level) { | |
1775 | verbose("%d: ", insn_idx); | |
1776 | print_bpf_insn(insn); | |
1777 | } | |
1778 | ||
1779 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1be7f75d | 1780 | err = check_alu_op(env, insn); |
17a52670 AS |
1781 | if (err) |
1782 | return err; | |
1783 | ||
1784 | } else if (class == BPF_LDX) { | |
9bac3d6d AS |
1785 | enum bpf_reg_type src_reg_type; |
1786 | ||
1787 | /* check for reserved fields is already done */ | |
1788 | ||
17a52670 AS |
1789 | /* check src operand */ |
1790 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1791 | if (err) | |
1792 | return err; | |
1793 | ||
1794 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); | |
1795 | if (err) | |
1796 | return err; | |
1797 | ||
725f9dcd AS |
1798 | src_reg_type = regs[insn->src_reg].type; |
1799 | ||
17a52670 AS |
1800 | /* check that memory (src_reg + off) is readable, |
1801 | * the state of dst_reg will be updated by this func | |
1802 | */ | |
1803 | err = check_mem_access(env, insn->src_reg, insn->off, | |
1804 | BPF_SIZE(insn->code), BPF_READ, | |
1805 | insn->dst_reg); | |
1806 | if (err) | |
1807 | return err; | |
1808 | ||
725f9dcd AS |
1809 | if (BPF_SIZE(insn->code) != BPF_W) { |
1810 | insn_idx++; | |
1811 | continue; | |
1812 | } | |
9bac3d6d | 1813 | |
725f9dcd | 1814 | if (insn->imm == 0) { |
9bac3d6d AS |
1815 | /* saw a valid insn |
1816 | * dst_reg = *(u32 *)(src_reg + off) | |
1817 | * use reserved 'imm' field to mark this insn | |
1818 | */ | |
1819 | insn->imm = src_reg_type; | |
1820 | ||
1821 | } else if (src_reg_type != insn->imm && | |
1822 | (src_reg_type == PTR_TO_CTX || | |
1823 | insn->imm == PTR_TO_CTX)) { | |
1824 | /* ABuser program is trying to use the same insn | |
1825 | * dst_reg = *(u32*) (src_reg + off) | |
1826 | * with different pointer types: | |
1827 | * src_reg == ctx in one branch and | |
1828 | * src_reg == stack|map in some other branch. | |
1829 | * Reject it. | |
1830 | */ | |
1831 | verbose("same insn cannot be used with different pointers\n"); | |
1832 | return -EINVAL; | |
1833 | } | |
1834 | ||
17a52670 | 1835 | } else if (class == BPF_STX) { |
d691f9e8 AS |
1836 | enum bpf_reg_type dst_reg_type; |
1837 | ||
17a52670 AS |
1838 | if (BPF_MODE(insn->code) == BPF_XADD) { |
1839 | err = check_xadd(env, insn); | |
1840 | if (err) | |
1841 | return err; | |
1842 | insn_idx++; | |
1843 | continue; | |
1844 | } | |
1845 | ||
17a52670 AS |
1846 | /* check src1 operand */ |
1847 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1848 | if (err) | |
1849 | return err; | |
1850 | /* check src2 operand */ | |
1851 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1852 | if (err) | |
1853 | return err; | |
1854 | ||
d691f9e8 AS |
1855 | dst_reg_type = regs[insn->dst_reg].type; |
1856 | ||
17a52670 AS |
1857 | /* check that memory (dst_reg + off) is writeable */ |
1858 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
1859 | BPF_SIZE(insn->code), BPF_WRITE, | |
1860 | insn->src_reg); | |
1861 | if (err) | |
1862 | return err; | |
1863 | ||
d691f9e8 AS |
1864 | if (insn->imm == 0) { |
1865 | insn->imm = dst_reg_type; | |
1866 | } else if (dst_reg_type != insn->imm && | |
1867 | (dst_reg_type == PTR_TO_CTX || | |
1868 | insn->imm == PTR_TO_CTX)) { | |
1869 | verbose("same insn cannot be used with different pointers\n"); | |
1870 | return -EINVAL; | |
1871 | } | |
1872 | ||
17a52670 AS |
1873 | } else if (class == BPF_ST) { |
1874 | if (BPF_MODE(insn->code) != BPF_MEM || | |
1875 | insn->src_reg != BPF_REG_0) { | |
1876 | verbose("BPF_ST uses reserved fields\n"); | |
1877 | return -EINVAL; | |
1878 | } | |
1879 | /* check src operand */ | |
1880 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1881 | if (err) | |
1882 | return err; | |
1883 | ||
1884 | /* check that memory (dst_reg + off) is writeable */ | |
1885 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
1886 | BPF_SIZE(insn->code), BPF_WRITE, | |
1887 | -1); | |
1888 | if (err) | |
1889 | return err; | |
1890 | ||
1891 | } else if (class == BPF_JMP) { | |
1892 | u8 opcode = BPF_OP(insn->code); | |
1893 | ||
1894 | if (opcode == BPF_CALL) { | |
1895 | if (BPF_SRC(insn->code) != BPF_K || | |
1896 | insn->off != 0 || | |
1897 | insn->src_reg != BPF_REG_0 || | |
1898 | insn->dst_reg != BPF_REG_0) { | |
1899 | verbose("BPF_CALL uses reserved fields\n"); | |
1900 | return -EINVAL; | |
1901 | } | |
1902 | ||
1903 | err = check_call(env, insn->imm); | |
1904 | if (err) | |
1905 | return err; | |
1906 | ||
1907 | } else if (opcode == BPF_JA) { | |
1908 | if (BPF_SRC(insn->code) != BPF_K || | |
1909 | insn->imm != 0 || | |
1910 | insn->src_reg != BPF_REG_0 || | |
1911 | insn->dst_reg != BPF_REG_0) { | |
1912 | verbose("BPF_JA uses reserved fields\n"); | |
1913 | return -EINVAL; | |
1914 | } | |
1915 | ||
1916 | insn_idx += insn->off + 1; | |
1917 | continue; | |
1918 | ||
1919 | } else if (opcode == BPF_EXIT) { | |
1920 | if (BPF_SRC(insn->code) != BPF_K || | |
1921 | insn->imm != 0 || | |
1922 | insn->src_reg != BPF_REG_0 || | |
1923 | insn->dst_reg != BPF_REG_0) { | |
1924 | verbose("BPF_EXIT uses reserved fields\n"); | |
1925 | return -EINVAL; | |
1926 | } | |
1927 | ||
1928 | /* eBPF calling convetion is such that R0 is used | |
1929 | * to return the value from eBPF program. | |
1930 | * Make sure that it's readable at this time | |
1931 | * of bpf_exit, which means that program wrote | |
1932 | * something into it earlier | |
1933 | */ | |
1934 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); | |
1935 | if (err) | |
1936 | return err; | |
1937 | ||
1be7f75d AS |
1938 | if (is_pointer_value(env, BPF_REG_0)) { |
1939 | verbose("R0 leaks addr as return value\n"); | |
1940 | return -EACCES; | |
1941 | } | |
1942 | ||
f1bca824 | 1943 | process_bpf_exit: |
17a52670 AS |
1944 | insn_idx = pop_stack(env, &prev_insn_idx); |
1945 | if (insn_idx < 0) { | |
1946 | break; | |
1947 | } else { | |
1948 | do_print_state = true; | |
1949 | continue; | |
1950 | } | |
1951 | } else { | |
1952 | err = check_cond_jmp_op(env, insn, &insn_idx); | |
1953 | if (err) | |
1954 | return err; | |
1955 | } | |
1956 | } else if (class == BPF_LD) { | |
1957 | u8 mode = BPF_MODE(insn->code); | |
1958 | ||
1959 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
1960 | err = check_ld_abs(env, insn); |
1961 | if (err) | |
1962 | return err; | |
1963 | ||
17a52670 AS |
1964 | } else if (mode == BPF_IMM) { |
1965 | err = check_ld_imm(env, insn); | |
1966 | if (err) | |
1967 | return err; | |
1968 | ||
1969 | insn_idx++; | |
1970 | } else { | |
1971 | verbose("invalid BPF_LD mode\n"); | |
1972 | return -EINVAL; | |
1973 | } | |
1974 | } else { | |
1975 | verbose("unknown insn class %d\n", class); | |
1976 | return -EINVAL; | |
1977 | } | |
1978 | ||
1979 | insn_idx++; | |
1980 | } | |
1981 | ||
1982 | return 0; | |
1983 | } | |
1984 | ||
0246e64d AS |
1985 | /* look for pseudo eBPF instructions that access map FDs and |
1986 | * replace them with actual map pointers | |
1987 | */ | |
1988 | static int replace_map_fd_with_map_ptr(struct verifier_env *env) | |
1989 | { | |
1990 | struct bpf_insn *insn = env->prog->insnsi; | |
1991 | int insn_cnt = env->prog->len; | |
1992 | int i, j; | |
1993 | ||
1994 | for (i = 0; i < insn_cnt; i++, insn++) { | |
9bac3d6d | 1995 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 1996 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
9bac3d6d AS |
1997 | verbose("BPF_LDX uses reserved fields\n"); |
1998 | return -EINVAL; | |
1999 | } | |
2000 | ||
d691f9e8 AS |
2001 | if (BPF_CLASS(insn->code) == BPF_STX && |
2002 | ((BPF_MODE(insn->code) != BPF_MEM && | |
2003 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
2004 | verbose("BPF_STX uses reserved fields\n"); | |
2005 | return -EINVAL; | |
2006 | } | |
2007 | ||
0246e64d AS |
2008 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
2009 | struct bpf_map *map; | |
2010 | struct fd f; | |
2011 | ||
2012 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
2013 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
2014 | insn[1].off != 0) { | |
2015 | verbose("invalid bpf_ld_imm64 insn\n"); | |
2016 | return -EINVAL; | |
2017 | } | |
2018 | ||
2019 | if (insn->src_reg == 0) | |
2020 | /* valid generic load 64-bit imm */ | |
2021 | goto next_insn; | |
2022 | ||
2023 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { | |
2024 | verbose("unrecognized bpf_ld_imm64 insn\n"); | |
2025 | return -EINVAL; | |
2026 | } | |
2027 | ||
2028 | f = fdget(insn->imm); | |
c2101297 | 2029 | map = __bpf_map_get(f); |
0246e64d AS |
2030 | if (IS_ERR(map)) { |
2031 | verbose("fd %d is not pointing to valid bpf_map\n", | |
2032 | insn->imm); | |
0246e64d AS |
2033 | return PTR_ERR(map); |
2034 | } | |
2035 | ||
2036 | /* store map pointer inside BPF_LD_IMM64 instruction */ | |
2037 | insn[0].imm = (u32) (unsigned long) map; | |
2038 | insn[1].imm = ((u64) (unsigned long) map) >> 32; | |
2039 | ||
2040 | /* check whether we recorded this map already */ | |
2041 | for (j = 0; j < env->used_map_cnt; j++) | |
2042 | if (env->used_maps[j] == map) { | |
2043 | fdput(f); | |
2044 | goto next_insn; | |
2045 | } | |
2046 | ||
2047 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
2048 | fdput(f); | |
2049 | return -E2BIG; | |
2050 | } | |
2051 | ||
0246e64d AS |
2052 | /* hold the map. If the program is rejected by verifier, |
2053 | * the map will be released by release_maps() or it | |
2054 | * will be used by the valid program until it's unloaded | |
2055 | * and all maps are released in free_bpf_prog_info() | |
2056 | */ | |
92117d84 AS |
2057 | map = bpf_map_inc(map, false); |
2058 | if (IS_ERR(map)) { | |
2059 | fdput(f); | |
2060 | return PTR_ERR(map); | |
2061 | } | |
2062 | env->used_maps[env->used_map_cnt++] = map; | |
2063 | ||
0246e64d AS |
2064 | fdput(f); |
2065 | next_insn: | |
2066 | insn++; | |
2067 | i++; | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
2072 | * 'struct bpf_map *' into a register instead of user map_fd. | |
2073 | * These pointers will be used later by verifier to validate map access. | |
2074 | */ | |
2075 | return 0; | |
2076 | } | |
2077 | ||
2078 | /* drop refcnt of maps used by the rejected program */ | |
2079 | static void release_maps(struct verifier_env *env) | |
2080 | { | |
2081 | int i; | |
2082 | ||
2083 | for (i = 0; i < env->used_map_cnt; i++) | |
2084 | bpf_map_put(env->used_maps[i]); | |
2085 | } | |
2086 | ||
2087 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
2088 | static void convert_pseudo_ld_imm64(struct verifier_env *env) | |
2089 | { | |
2090 | struct bpf_insn *insn = env->prog->insnsi; | |
2091 | int insn_cnt = env->prog->len; | |
2092 | int i; | |
2093 | ||
2094 | for (i = 0; i < insn_cnt; i++, insn++) | |
2095 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
2096 | insn->src_reg = 0; | |
2097 | } | |
2098 | ||
9bac3d6d AS |
2099 | static void adjust_branches(struct bpf_prog *prog, int pos, int delta) |
2100 | { | |
2101 | struct bpf_insn *insn = prog->insnsi; | |
2102 | int insn_cnt = prog->len; | |
2103 | int i; | |
2104 | ||
2105 | for (i = 0; i < insn_cnt; i++, insn++) { | |
2106 | if (BPF_CLASS(insn->code) != BPF_JMP || | |
2107 | BPF_OP(insn->code) == BPF_CALL || | |
2108 | BPF_OP(insn->code) == BPF_EXIT) | |
2109 | continue; | |
2110 | ||
2111 | /* adjust offset of jmps if necessary */ | |
2112 | if (i < pos && i + insn->off + 1 > pos) | |
2113 | insn->off += delta; | |
a1b14d27 | 2114 | else if (i > pos + delta && i + insn->off + 1 <= pos + delta) |
9bac3d6d AS |
2115 | insn->off -= delta; |
2116 | } | |
2117 | } | |
2118 | ||
2119 | /* convert load instructions that access fields of 'struct __sk_buff' | |
2120 | * into sequence of instructions that access fields of 'struct sk_buff' | |
2121 | */ | |
2122 | static int convert_ctx_accesses(struct verifier_env *env) | |
2123 | { | |
2124 | struct bpf_insn *insn = env->prog->insnsi; | |
2125 | int insn_cnt = env->prog->len; | |
2126 | struct bpf_insn insn_buf[16]; | |
2127 | struct bpf_prog *new_prog; | |
2128 | u32 cnt; | |
2129 | int i; | |
d691f9e8 | 2130 | enum bpf_access_type type; |
9bac3d6d AS |
2131 | |
2132 | if (!env->prog->aux->ops->convert_ctx_access) | |
2133 | return 0; | |
2134 | ||
2135 | for (i = 0; i < insn_cnt; i++, insn++) { | |
d691f9e8 AS |
2136 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_W)) |
2137 | type = BPF_READ; | |
2138 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_W)) | |
2139 | type = BPF_WRITE; | |
2140 | else | |
9bac3d6d AS |
2141 | continue; |
2142 | ||
2143 | if (insn->imm != PTR_TO_CTX) { | |
2144 | /* clear internal mark */ | |
2145 | insn->imm = 0; | |
2146 | continue; | |
2147 | } | |
2148 | ||
2149 | cnt = env->prog->aux->ops-> | |
d691f9e8 | 2150 | convert_ctx_access(type, insn->dst_reg, insn->src_reg, |
ff936a04 | 2151 | insn->off, insn_buf, env->prog); |
9bac3d6d AS |
2152 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
2153 | verbose("bpf verifier is misconfigured\n"); | |
2154 | return -EINVAL; | |
2155 | } | |
2156 | ||
2157 | if (cnt == 1) { | |
2158 | memcpy(insn, insn_buf, sizeof(*insn)); | |
2159 | continue; | |
2160 | } | |
2161 | ||
2162 | /* several new insns need to be inserted. Make room for them */ | |
2163 | insn_cnt += cnt - 1; | |
2164 | new_prog = bpf_prog_realloc(env->prog, | |
2165 | bpf_prog_size(insn_cnt), | |
2166 | GFP_USER); | |
2167 | if (!new_prog) | |
2168 | return -ENOMEM; | |
2169 | ||
2170 | new_prog->len = insn_cnt; | |
2171 | ||
2172 | memmove(new_prog->insnsi + i + cnt, new_prog->insns + i + 1, | |
2173 | sizeof(*insn) * (insn_cnt - i - cnt)); | |
2174 | ||
2175 | /* copy substitute insns in place of load instruction */ | |
2176 | memcpy(new_prog->insnsi + i, insn_buf, sizeof(*insn) * cnt); | |
2177 | ||
2178 | /* adjust branches in the whole program */ | |
2179 | adjust_branches(new_prog, i, cnt - 1); | |
2180 | ||
2181 | /* keep walking new program and skip insns we just inserted */ | |
2182 | env->prog = new_prog; | |
2183 | insn = new_prog->insnsi + i + cnt - 1; | |
2184 | i += cnt - 1; | |
2185 | } | |
2186 | ||
2187 | return 0; | |
2188 | } | |
2189 | ||
f1bca824 AS |
2190 | static void free_states(struct verifier_env *env) |
2191 | { | |
2192 | struct verifier_state_list *sl, *sln; | |
2193 | int i; | |
2194 | ||
2195 | if (!env->explored_states) | |
2196 | return; | |
2197 | ||
2198 | for (i = 0; i < env->prog->len; i++) { | |
2199 | sl = env->explored_states[i]; | |
2200 | ||
2201 | if (sl) | |
2202 | while (sl != STATE_LIST_MARK) { | |
2203 | sln = sl->next; | |
2204 | kfree(sl); | |
2205 | sl = sln; | |
2206 | } | |
2207 | } | |
2208 | ||
2209 | kfree(env->explored_states); | |
2210 | } | |
2211 | ||
9bac3d6d | 2212 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
51580e79 | 2213 | { |
cbd35700 AS |
2214 | char __user *log_ubuf = NULL; |
2215 | struct verifier_env *env; | |
51580e79 AS |
2216 | int ret = -EINVAL; |
2217 | ||
9bac3d6d | 2218 | if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS) |
cbd35700 AS |
2219 | return -E2BIG; |
2220 | ||
2221 | /* 'struct verifier_env' can be global, but since it's not small, | |
2222 | * allocate/free it every time bpf_check() is called | |
2223 | */ | |
2224 | env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL); | |
2225 | if (!env) | |
2226 | return -ENOMEM; | |
2227 | ||
9bac3d6d | 2228 | env->prog = *prog; |
0246e64d | 2229 | |
cbd35700 AS |
2230 | /* grab the mutex to protect few globals used by verifier */ |
2231 | mutex_lock(&bpf_verifier_lock); | |
2232 | ||
2233 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
2234 | /* user requested verbose verifier output | |
2235 | * and supplied buffer to store the verification trace | |
2236 | */ | |
2237 | log_level = attr->log_level; | |
2238 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; | |
2239 | log_size = attr->log_size; | |
2240 | log_len = 0; | |
2241 | ||
2242 | ret = -EINVAL; | |
2243 | /* log_* values have to be sane */ | |
2244 | if (log_size < 128 || log_size > UINT_MAX >> 8 || | |
2245 | log_level == 0 || log_ubuf == NULL) | |
2246 | goto free_env; | |
2247 | ||
2248 | ret = -ENOMEM; | |
2249 | log_buf = vmalloc(log_size); | |
2250 | if (!log_buf) | |
2251 | goto free_env; | |
2252 | } else { | |
2253 | log_level = 0; | |
2254 | } | |
2255 | ||
0246e64d AS |
2256 | ret = replace_map_fd_with_map_ptr(env); |
2257 | if (ret < 0) | |
2258 | goto skip_full_check; | |
2259 | ||
9bac3d6d | 2260 | env->explored_states = kcalloc(env->prog->len, |
f1bca824 AS |
2261 | sizeof(struct verifier_state_list *), |
2262 | GFP_USER); | |
2263 | ret = -ENOMEM; | |
2264 | if (!env->explored_states) | |
2265 | goto skip_full_check; | |
2266 | ||
475fb78f AS |
2267 | ret = check_cfg(env); |
2268 | if (ret < 0) | |
2269 | goto skip_full_check; | |
2270 | ||
1be7f75d AS |
2271 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
2272 | ||
17a52670 | 2273 | ret = do_check(env); |
cbd35700 | 2274 | |
0246e64d | 2275 | skip_full_check: |
17a52670 | 2276 | while (pop_stack(env, NULL) >= 0); |
f1bca824 | 2277 | free_states(env); |
0246e64d | 2278 | |
9bac3d6d AS |
2279 | if (ret == 0) |
2280 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
2281 | ret = convert_ctx_accesses(env); | |
2282 | ||
cbd35700 AS |
2283 | if (log_level && log_len >= log_size - 1) { |
2284 | BUG_ON(log_len >= log_size); | |
2285 | /* verifier log exceeded user supplied buffer */ | |
2286 | ret = -ENOSPC; | |
2287 | /* fall through to return what was recorded */ | |
2288 | } | |
2289 | ||
2290 | /* copy verifier log back to user space including trailing zero */ | |
2291 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { | |
2292 | ret = -EFAULT; | |
2293 | goto free_log_buf; | |
2294 | } | |
2295 | ||
0246e64d AS |
2296 | if (ret == 0 && env->used_map_cnt) { |
2297 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
2298 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
2299 | sizeof(env->used_maps[0]), | |
2300 | GFP_KERNEL); | |
0246e64d | 2301 | |
9bac3d6d | 2302 | if (!env->prog->aux->used_maps) { |
0246e64d AS |
2303 | ret = -ENOMEM; |
2304 | goto free_log_buf; | |
2305 | } | |
2306 | ||
9bac3d6d | 2307 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 2308 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 2309 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
2310 | |
2311 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
2312 | * bpf_ld_imm64 instructions | |
2313 | */ | |
2314 | convert_pseudo_ld_imm64(env); | |
2315 | } | |
cbd35700 AS |
2316 | |
2317 | free_log_buf: | |
2318 | if (log_level) | |
2319 | vfree(log_buf); | |
2320 | free_env: | |
9bac3d6d | 2321 | if (!env->prog->aux->used_maps) |
0246e64d AS |
2322 | /* if we didn't copy map pointers into bpf_prog_info, release |
2323 | * them now. Otherwise free_bpf_prog_info() will release them. | |
2324 | */ | |
2325 | release_maps(env); | |
9bac3d6d | 2326 | *prog = env->prog; |
cbd35700 AS |
2327 | kfree(env); |
2328 | mutex_unlock(&bpf_verifier_lock); | |
51580e79 AS |
2329 | return ret; |
2330 | } |