]>
Commit | Line | Data |
---|---|---|
51580e79 | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
969bf05e | 2 | * Copyright (c) 2016 Facebook |
51580e79 AS |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | */ | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/bpf.h> | |
58e2af8b | 17 | #include <linux/bpf_verifier.h> |
51580e79 AS |
18 | #include <linux/filter.h> |
19 | #include <net/netlink.h> | |
20 | #include <linux/file.h> | |
21 | #include <linux/vmalloc.h> | |
ebb676da | 22 | #include <linux/stringify.h> |
51580e79 AS |
23 | |
24 | /* bpf_check() is a static code analyzer that walks eBPF program | |
25 | * instruction by instruction and updates register/stack state. | |
26 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
27 | * | |
28 | * The first pass is depth-first-search to check that the program is a DAG. | |
29 | * It rejects the following programs: | |
30 | * - larger than BPF_MAXINSNS insns | |
31 | * - if loop is present (detected via back-edge) | |
32 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
33 | * - out of bounds or malformed jumps | |
34 | * The second pass is all possible path descent from the 1st insn. | |
35 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 36 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
37 | * insn is less then 4K, but there are too many branches that change stack/regs. |
38 | * Number of 'branches to be analyzed' is limited to 1k | |
39 | * | |
40 | * On entry to each instruction, each register has a type, and the instruction | |
41 | * changes the types of the registers depending on instruction semantics. | |
42 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
43 | * copied to R1. | |
44 | * | |
45 | * All registers are 64-bit. | |
46 | * R0 - return register | |
47 | * R1-R5 argument passing registers | |
48 | * R6-R9 callee saved registers | |
49 | * R10 - frame pointer read-only | |
50 | * | |
51 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
52 | * and has type PTR_TO_CTX. | |
53 | * | |
54 | * Verifier tracks arithmetic operations on pointers in case: | |
55 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
56 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
57 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
58 | * and 2nd arithmetic instruction is pattern matched to recognize | |
59 | * that it wants to construct a pointer to some element within stack. | |
60 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
61 | * (and -20 constant is saved for further stack bounds checking). | |
62 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
63 | * | |
64 | * Most of the time the registers have UNKNOWN_VALUE type, which | |
65 | * means the register has some value, but it's not a valid pointer. | |
66 | * (like pointer plus pointer becomes UNKNOWN_VALUE type) | |
67 | * | |
68 | * When verifier sees load or store instructions the type of base register | |
69 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer | |
70 | * types recognized by check_mem_access() function. | |
71 | * | |
72 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
73 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
74 | * | |
75 | * registers used to pass values to function calls are checked against | |
76 | * function argument constraints. | |
77 | * | |
78 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
79 | * It means that the register type passed to this function must be | |
80 | * PTR_TO_STACK and it will be used inside the function as | |
81 | * 'pointer to map element key' | |
82 | * | |
83 | * For example the argument constraints for bpf_map_lookup_elem(): | |
84 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
85 | * .arg1_type = ARG_CONST_MAP_PTR, | |
86 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
87 | * | |
88 | * ret_type says that this function returns 'pointer to map elem value or null' | |
89 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
90 | * 2nd argument should be a pointer to stack, which will be used inside | |
91 | * the helper function as a pointer to map element key. | |
92 | * | |
93 | * On the kernel side the helper function looks like: | |
94 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
95 | * { | |
96 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
97 | * void *key = (void *) (unsigned long) r2; | |
98 | * void *value; | |
99 | * | |
100 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
101 | * [key, key + map->key_size) bytes are valid and were initialized on | |
102 | * the stack of eBPF program. | |
103 | * } | |
104 | * | |
105 | * Corresponding eBPF program may look like: | |
106 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
107 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
108 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
109 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
110 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
111 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
112 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
113 | * | |
114 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
115 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
116 | * and were initialized prior to this call. | |
117 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
118 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
119 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
120 | * returns ether pointer to map value or NULL. | |
121 | * | |
122 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
123 | * insn, the register holding that pointer in the true branch changes state to | |
124 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
125 | * branch. See check_cond_jmp_op(). | |
126 | * | |
127 | * After the call R0 is set to return type of the function and registers R1-R5 | |
128 | * are set to NOT_INIT to indicate that they are no longer readable. | |
129 | */ | |
130 | ||
17a52670 | 131 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 132 | struct bpf_verifier_stack_elem { |
17a52670 AS |
133 | /* verifer state is 'st' |
134 | * before processing instruction 'insn_idx' | |
135 | * and after processing instruction 'prev_insn_idx' | |
136 | */ | |
58e2af8b | 137 | struct bpf_verifier_state st; |
17a52670 AS |
138 | int insn_idx; |
139 | int prev_insn_idx; | |
58e2af8b | 140 | struct bpf_verifier_stack_elem *next; |
cbd35700 AS |
141 | }; |
142 | ||
3c2ce60b | 143 | #define BPF_COMPLEXITY_LIMIT_INSNS 98304 |
07016151 DB |
144 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
145 | ||
fad73a1a MKL |
146 | #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA) |
147 | ||
33ff9823 DB |
148 | struct bpf_call_arg_meta { |
149 | struct bpf_map *map_ptr; | |
435faee1 | 150 | bool raw_mode; |
36bbef52 | 151 | bool pkt_access; |
435faee1 DB |
152 | int regno; |
153 | int access_size; | |
33ff9823 DB |
154 | }; |
155 | ||
cbd35700 AS |
156 | /* verbose verifier prints what it's seeing |
157 | * bpf_check() is called under lock, so no race to access these global vars | |
158 | */ | |
159 | static u32 log_level, log_size, log_len; | |
160 | static char *log_buf; | |
161 | ||
162 | static DEFINE_MUTEX(bpf_verifier_lock); | |
163 | ||
164 | /* log_level controls verbosity level of eBPF verifier. | |
165 | * verbose() is used to dump the verification trace to the log, so the user | |
166 | * can figure out what's wrong with the program | |
167 | */ | |
1d056d9c | 168 | static __printf(1, 2) void verbose(const char *fmt, ...) |
cbd35700 AS |
169 | { |
170 | va_list args; | |
171 | ||
172 | if (log_level == 0 || log_len >= log_size - 1) | |
173 | return; | |
174 | ||
175 | va_start(args, fmt); | |
176 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); | |
177 | va_end(args); | |
178 | } | |
179 | ||
17a52670 AS |
180 | /* string representation of 'enum bpf_reg_type' */ |
181 | static const char * const reg_type_str[] = { | |
182 | [NOT_INIT] = "?", | |
183 | [UNKNOWN_VALUE] = "inv", | |
184 | [PTR_TO_CTX] = "ctx", | |
185 | [CONST_PTR_TO_MAP] = "map_ptr", | |
186 | [PTR_TO_MAP_VALUE] = "map_value", | |
187 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
48461135 | 188 | [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj", |
17a52670 AS |
189 | [FRAME_PTR] = "fp", |
190 | [PTR_TO_STACK] = "fp", | |
191 | [CONST_IMM] = "imm", | |
969bf05e AS |
192 | [PTR_TO_PACKET] = "pkt", |
193 | [PTR_TO_PACKET_END] = "pkt_end", | |
17a52670 AS |
194 | }; |
195 | ||
ebb676da TG |
196 | #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x) |
197 | static const char * const func_id_str[] = { | |
198 | __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN) | |
199 | }; | |
200 | #undef __BPF_FUNC_STR_FN | |
201 | ||
202 | static const char *func_id_name(int id) | |
203 | { | |
204 | BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID); | |
205 | ||
206 | if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id]) | |
207 | return func_id_str[id]; | |
208 | else | |
209 | return "unknown"; | |
210 | } | |
211 | ||
58e2af8b | 212 | static void print_verifier_state(struct bpf_verifier_state *state) |
17a52670 | 213 | { |
58e2af8b | 214 | struct bpf_reg_state *reg; |
17a52670 AS |
215 | enum bpf_reg_type t; |
216 | int i; | |
217 | ||
218 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1a0dc1ac AS |
219 | reg = &state->regs[i]; |
220 | t = reg->type; | |
17a52670 AS |
221 | if (t == NOT_INIT) |
222 | continue; | |
223 | verbose(" R%d=%s", i, reg_type_str[t]); | |
224 | if (t == CONST_IMM || t == PTR_TO_STACK) | |
969bf05e AS |
225 | verbose("%lld", reg->imm); |
226 | else if (t == PTR_TO_PACKET) | |
227 | verbose("(id=%d,off=%d,r=%d)", | |
228 | reg->id, reg->off, reg->range); | |
229 | else if (t == UNKNOWN_VALUE && reg->imm) | |
230 | verbose("%lld", reg->imm); | |
17a52670 | 231 | else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || |
48461135 JB |
232 | t == PTR_TO_MAP_VALUE_OR_NULL || |
233 | t == PTR_TO_MAP_VALUE_ADJ) | |
57a09bf0 | 234 | verbose("(ks=%d,vs=%d,id=%u)", |
1a0dc1ac | 235 | reg->map_ptr->key_size, |
57a09bf0 TG |
236 | reg->map_ptr->value_size, |
237 | reg->id); | |
48461135 | 238 | if (reg->min_value != BPF_REGISTER_MIN_RANGE) |
f23cc643 JB |
239 | verbose(",min_value=%lld", |
240 | (long long)reg->min_value); | |
48461135 JB |
241 | if (reg->max_value != BPF_REGISTER_MAX_RANGE) |
242 | verbose(",max_value=%llu", | |
243 | (unsigned long long)reg->max_value); | |
d1174416 DM |
244 | if (reg->min_align) |
245 | verbose(",min_align=%u", reg->min_align); | |
246 | if (reg->aux_off) | |
247 | verbose(",aux_off=%u", reg->aux_off); | |
248 | if (reg->aux_off_align) | |
249 | verbose(",aux_off_align=%u", reg->aux_off_align); | |
17a52670 | 250 | } |
9c399760 | 251 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
1a0dc1ac | 252 | if (state->stack_slot_type[i] == STACK_SPILL) |
17a52670 | 253 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
1a0dc1ac | 254 | reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]); |
17a52670 AS |
255 | } |
256 | verbose("\n"); | |
257 | } | |
258 | ||
cbd35700 AS |
259 | static const char *const bpf_class_string[] = { |
260 | [BPF_LD] = "ld", | |
261 | [BPF_LDX] = "ldx", | |
262 | [BPF_ST] = "st", | |
263 | [BPF_STX] = "stx", | |
264 | [BPF_ALU] = "alu", | |
265 | [BPF_JMP] = "jmp", | |
266 | [BPF_RET] = "BUG", | |
267 | [BPF_ALU64] = "alu64", | |
268 | }; | |
269 | ||
687f0715 | 270 | static const char *const bpf_alu_string[16] = { |
cbd35700 AS |
271 | [BPF_ADD >> 4] = "+=", |
272 | [BPF_SUB >> 4] = "-=", | |
273 | [BPF_MUL >> 4] = "*=", | |
274 | [BPF_DIV >> 4] = "/=", | |
275 | [BPF_OR >> 4] = "|=", | |
276 | [BPF_AND >> 4] = "&=", | |
277 | [BPF_LSH >> 4] = "<<=", | |
278 | [BPF_RSH >> 4] = ">>=", | |
279 | [BPF_NEG >> 4] = "neg", | |
280 | [BPF_MOD >> 4] = "%=", | |
281 | [BPF_XOR >> 4] = "^=", | |
282 | [BPF_MOV >> 4] = "=", | |
283 | [BPF_ARSH >> 4] = "s>>=", | |
284 | [BPF_END >> 4] = "endian", | |
285 | }; | |
286 | ||
287 | static const char *const bpf_ldst_string[] = { | |
288 | [BPF_W >> 3] = "u32", | |
289 | [BPF_H >> 3] = "u16", | |
290 | [BPF_B >> 3] = "u8", | |
291 | [BPF_DW >> 3] = "u64", | |
292 | }; | |
293 | ||
687f0715 | 294 | static const char *const bpf_jmp_string[16] = { |
cbd35700 AS |
295 | [BPF_JA >> 4] = "jmp", |
296 | [BPF_JEQ >> 4] = "==", | |
297 | [BPF_JGT >> 4] = ">", | |
298 | [BPF_JGE >> 4] = ">=", | |
299 | [BPF_JSET >> 4] = "&", | |
300 | [BPF_JNE >> 4] = "!=", | |
301 | [BPF_JSGT >> 4] = "s>", | |
302 | [BPF_JSGE >> 4] = "s>=", | |
303 | [BPF_CALL >> 4] = "call", | |
304 | [BPF_EXIT >> 4] = "exit", | |
305 | }; | |
306 | ||
0d0e5769 DB |
307 | static void print_bpf_insn(const struct bpf_verifier_env *env, |
308 | const struct bpf_insn *insn) | |
cbd35700 AS |
309 | { |
310 | u8 class = BPF_CLASS(insn->code); | |
311 | ||
312 | if (class == BPF_ALU || class == BPF_ALU64) { | |
313 | if (BPF_SRC(insn->code) == BPF_X) | |
314 | verbose("(%02x) %sr%d %s %sr%d\n", | |
315 | insn->code, class == BPF_ALU ? "(u32) " : "", | |
316 | insn->dst_reg, | |
317 | bpf_alu_string[BPF_OP(insn->code) >> 4], | |
318 | class == BPF_ALU ? "(u32) " : "", | |
319 | insn->src_reg); | |
320 | else | |
321 | verbose("(%02x) %sr%d %s %s%d\n", | |
322 | insn->code, class == BPF_ALU ? "(u32) " : "", | |
323 | insn->dst_reg, | |
324 | bpf_alu_string[BPF_OP(insn->code) >> 4], | |
325 | class == BPF_ALU ? "(u32) " : "", | |
326 | insn->imm); | |
327 | } else if (class == BPF_STX) { | |
328 | if (BPF_MODE(insn->code) == BPF_MEM) | |
329 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", | |
330 | insn->code, | |
331 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
332 | insn->dst_reg, | |
333 | insn->off, insn->src_reg); | |
334 | else if (BPF_MODE(insn->code) == BPF_XADD) | |
335 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", | |
336 | insn->code, | |
337 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
338 | insn->dst_reg, insn->off, | |
339 | insn->src_reg); | |
340 | else | |
341 | verbose("BUG_%02x\n", insn->code); | |
342 | } else if (class == BPF_ST) { | |
343 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
344 | verbose("BUG_st_%02x\n", insn->code); | |
345 | return; | |
346 | } | |
347 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", | |
348 | insn->code, | |
349 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
350 | insn->dst_reg, | |
351 | insn->off, insn->imm); | |
352 | } else if (class == BPF_LDX) { | |
353 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
354 | verbose("BUG_ldx_%02x\n", insn->code); | |
355 | return; | |
356 | } | |
357 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", | |
358 | insn->code, insn->dst_reg, | |
359 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
360 | insn->src_reg, insn->off); | |
361 | } else if (class == BPF_LD) { | |
362 | if (BPF_MODE(insn->code) == BPF_ABS) { | |
363 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", | |
364 | insn->code, | |
365 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
366 | insn->imm); | |
367 | } else if (BPF_MODE(insn->code) == BPF_IND) { | |
368 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", | |
369 | insn->code, | |
370 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
371 | insn->src_reg, insn->imm); | |
0d0e5769 DB |
372 | } else if (BPF_MODE(insn->code) == BPF_IMM && |
373 | BPF_SIZE(insn->code) == BPF_DW) { | |
374 | /* At this point, we already made sure that the second | |
375 | * part of the ldimm64 insn is accessible. | |
376 | */ | |
377 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; | |
378 | bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD; | |
379 | ||
380 | if (map_ptr && !env->allow_ptr_leaks) | |
381 | imm = 0; | |
382 | ||
383 | verbose("(%02x) r%d = 0x%llx\n", insn->code, | |
384 | insn->dst_reg, (unsigned long long)imm); | |
cbd35700 AS |
385 | } else { |
386 | verbose("BUG_ld_%02x\n", insn->code); | |
387 | return; | |
388 | } | |
389 | } else if (class == BPF_JMP) { | |
390 | u8 opcode = BPF_OP(insn->code); | |
391 | ||
392 | if (opcode == BPF_CALL) { | |
ebb676da TG |
393 | verbose("(%02x) call %s#%d\n", insn->code, |
394 | func_id_name(insn->imm), insn->imm); | |
cbd35700 AS |
395 | } else if (insn->code == (BPF_JMP | BPF_JA)) { |
396 | verbose("(%02x) goto pc%+d\n", | |
397 | insn->code, insn->off); | |
398 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { | |
399 | verbose("(%02x) exit\n", insn->code); | |
400 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
401 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", | |
402 | insn->code, insn->dst_reg, | |
403 | bpf_jmp_string[BPF_OP(insn->code) >> 4], | |
404 | insn->src_reg, insn->off); | |
405 | } else { | |
406 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", | |
407 | insn->code, insn->dst_reg, | |
408 | bpf_jmp_string[BPF_OP(insn->code) >> 4], | |
409 | insn->imm, insn->off); | |
410 | } | |
411 | } else { | |
412 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); | |
413 | } | |
414 | } | |
415 | ||
58e2af8b | 416 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx) |
17a52670 | 417 | { |
58e2af8b | 418 | struct bpf_verifier_stack_elem *elem; |
17a52670 AS |
419 | int insn_idx; |
420 | ||
421 | if (env->head == NULL) | |
422 | return -1; | |
423 | ||
424 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); | |
425 | insn_idx = env->head->insn_idx; | |
426 | if (prev_insn_idx) | |
427 | *prev_insn_idx = env->head->prev_insn_idx; | |
428 | elem = env->head->next; | |
429 | kfree(env->head); | |
430 | env->head = elem; | |
431 | env->stack_size--; | |
432 | return insn_idx; | |
433 | } | |
434 | ||
58e2af8b JK |
435 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
436 | int insn_idx, int prev_insn_idx) | |
17a52670 | 437 | { |
58e2af8b | 438 | struct bpf_verifier_stack_elem *elem; |
17a52670 | 439 | |
58e2af8b | 440 | elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
441 | if (!elem) |
442 | goto err; | |
443 | ||
444 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); | |
445 | elem->insn_idx = insn_idx; | |
446 | elem->prev_insn_idx = prev_insn_idx; | |
447 | elem->next = env->head; | |
448 | env->head = elem; | |
449 | env->stack_size++; | |
07016151 | 450 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
17a52670 AS |
451 | verbose("BPF program is too complex\n"); |
452 | goto err; | |
453 | } | |
454 | return &elem->st; | |
455 | err: | |
456 | /* pop all elements and return */ | |
457 | while (pop_stack(env, NULL) >= 0); | |
458 | return NULL; | |
459 | } | |
460 | ||
461 | #define CALLER_SAVED_REGS 6 | |
462 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
463 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
464 | }; | |
465 | ||
a9789ef9 DB |
466 | static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno) |
467 | { | |
468 | BUG_ON(regno >= MAX_BPF_REG); | |
469 | ||
470 | memset(®s[regno], 0, sizeof(regs[regno])); | |
471 | regs[regno].type = NOT_INIT; | |
472 | regs[regno].min_value = BPF_REGISTER_MIN_RANGE; | |
473 | regs[regno].max_value = BPF_REGISTER_MAX_RANGE; | |
474 | } | |
475 | ||
58e2af8b | 476 | static void init_reg_state(struct bpf_reg_state *regs) |
17a52670 AS |
477 | { |
478 | int i; | |
479 | ||
a9789ef9 DB |
480 | for (i = 0; i < MAX_BPF_REG; i++) |
481 | mark_reg_not_init(regs, i); | |
17a52670 AS |
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 | ||
6760bf2d | 490 | static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
17a52670 | 491 | { |
17a52670 | 492 | regs[regno].type = UNKNOWN_VALUE; |
57a09bf0 | 493 | regs[regno].id = 0; |
17a52670 | 494 | regs[regno].imm = 0; |
17a52670 AS |
495 | } |
496 | ||
6760bf2d DB |
497 | static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
498 | { | |
499 | BUG_ON(regno >= MAX_BPF_REG); | |
500 | __mark_reg_unknown_value(regs, regno); | |
501 | } | |
502 | ||
48461135 JB |
503 | static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno) |
504 | { | |
505 | regs[regno].min_value = BPF_REGISTER_MIN_RANGE; | |
506 | regs[regno].max_value = BPF_REGISTER_MAX_RANGE; | |
4cabc5b1 | 507 | regs[regno].value_from_signed = false; |
d1174416 | 508 | regs[regno].min_align = 0; |
48461135 JB |
509 | } |
510 | ||
f0318d01 GB |
511 | static void mark_reg_unknown_value_and_range(struct bpf_reg_state *regs, |
512 | u32 regno) | |
513 | { | |
514 | mark_reg_unknown_value(regs, regno); | |
515 | reset_reg_range_values(regs, regno); | |
516 | } | |
517 | ||
17a52670 AS |
518 | enum reg_arg_type { |
519 | SRC_OP, /* register is used as source operand */ | |
520 | DST_OP, /* register is used as destination operand */ | |
521 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
522 | }; | |
523 | ||
58e2af8b | 524 | static int check_reg_arg(struct bpf_reg_state *regs, u32 regno, |
17a52670 AS |
525 | enum reg_arg_type t) |
526 | { | |
527 | if (regno >= MAX_BPF_REG) { | |
528 | verbose("R%d is invalid\n", regno); | |
529 | return -EINVAL; | |
530 | } | |
531 | ||
532 | if (t == SRC_OP) { | |
533 | /* check whether register used as source operand can be read */ | |
534 | if (regs[regno].type == NOT_INIT) { | |
535 | verbose("R%d !read_ok\n", regno); | |
536 | return -EACCES; | |
537 | } | |
538 | } else { | |
539 | /* check whether register used as dest operand can be written to */ | |
540 | if (regno == BPF_REG_FP) { | |
541 | verbose("frame pointer is read only\n"); | |
542 | return -EACCES; | |
543 | } | |
544 | if (t == DST_OP) | |
545 | mark_reg_unknown_value(regs, regno); | |
546 | } | |
547 | return 0; | |
548 | } | |
549 | ||
1be7f75d AS |
550 | static bool is_spillable_regtype(enum bpf_reg_type type) |
551 | { | |
552 | switch (type) { | |
553 | case PTR_TO_MAP_VALUE: | |
554 | case PTR_TO_MAP_VALUE_OR_NULL: | |
f0318d01 | 555 | case PTR_TO_MAP_VALUE_ADJ: |
1be7f75d AS |
556 | case PTR_TO_STACK: |
557 | case PTR_TO_CTX: | |
969bf05e AS |
558 | case PTR_TO_PACKET: |
559 | case PTR_TO_PACKET_END: | |
1be7f75d AS |
560 | case FRAME_PTR: |
561 | case CONST_PTR_TO_MAP: | |
562 | return true; | |
563 | default: | |
564 | return false; | |
565 | } | |
566 | } | |
567 | ||
17a52670 AS |
568 | /* check_stack_read/write functions track spill/fill of registers, |
569 | * stack boundary and alignment are checked in check_mem_access() | |
570 | */ | |
674d8995 AS |
571 | static int check_stack_write(struct bpf_verifier_env *env, |
572 | struct bpf_verifier_state *state, int off, | |
573 | int size, int value_regno, int insn_idx) | |
17a52670 | 574 | { |
17a52670 | 575 | int i; |
9c399760 AS |
576 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
577 | * so it's aligned access and [off, off + size) are within stack limits | |
578 | */ | |
17a52670 AS |
579 | |
580 | if (value_regno >= 0 && | |
1be7f75d | 581 | is_spillable_regtype(state->regs[value_regno].type)) { |
17a52670 AS |
582 | |
583 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 584 | if (size != BPF_REG_SIZE) { |
17a52670 AS |
585 | verbose("invalid size of register spill\n"); |
586 | return -EACCES; | |
587 | } | |
588 | ||
17a52670 | 589 | /* save register state */ |
9c399760 AS |
590 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
591 | state->regs[value_regno]; | |
17a52670 | 592 | |
674d8995 AS |
593 | for (i = 0; i < BPF_REG_SIZE; i++) { |
594 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] == STACK_MISC && | |
595 | !env->allow_ptr_leaks) { | |
596 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; | |
597 | int soff = off; | |
598 | ||
599 | /* detected reuse of integer stack slot with a pointer | |
600 | * which means either llvm is reusing stack slot or | |
601 | * an attacker is trying to exploit CVE-2018-3639 | |
602 | * (speculative store bypass) | |
603 | * Have to sanitize that slot with preemptive | |
604 | * store of zero. | |
605 | */ | |
606 | if (*poff && *poff != soff) { | |
607 | /* disallow programs where single insn stores | |
608 | * into two different stack slots, since verifier | |
609 | * cannot sanitize them | |
610 | */ | |
611 | verbose("insn %d cannot access two stack slots fp%d and fp%d", | |
612 | insn_idx, *poff, soff); | |
613 | return -EINVAL; | |
614 | } | |
615 | *poff = soff; | |
616 | } | |
9c399760 | 617 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; |
674d8995 | 618 | } |
9c399760 | 619 | } else { |
17a52670 | 620 | /* regular write of data into stack */ |
9c399760 | 621 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
58e2af8b | 622 | (struct bpf_reg_state) {}; |
9c399760 AS |
623 | |
624 | for (i = 0; i < size; i++) | |
625 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; | |
17a52670 AS |
626 | } |
627 | return 0; | |
628 | } | |
629 | ||
58e2af8b | 630 | static int check_stack_read(struct bpf_verifier_state *state, int off, int size, |
17a52670 AS |
631 | int value_regno) |
632 | { | |
9c399760 | 633 | u8 *slot_type; |
17a52670 | 634 | int i; |
17a52670 | 635 | |
9c399760 | 636 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
17a52670 | 637 | |
9c399760 AS |
638 | if (slot_type[0] == STACK_SPILL) { |
639 | if (size != BPF_REG_SIZE) { | |
17a52670 AS |
640 | verbose("invalid size of register spill\n"); |
641 | return -EACCES; | |
642 | } | |
9c399760 AS |
643 | for (i = 1; i < BPF_REG_SIZE; i++) { |
644 | if (slot_type[i] != STACK_SPILL) { | |
17a52670 AS |
645 | verbose("corrupted spill memory\n"); |
646 | return -EACCES; | |
647 | } | |
648 | } | |
649 | ||
650 | if (value_regno >= 0) | |
651 | /* restore register state from stack */ | |
9c399760 AS |
652 | state->regs[value_regno] = |
653 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; | |
17a52670 AS |
654 | return 0; |
655 | } else { | |
656 | for (i = 0; i < size; i++) { | |
9c399760 | 657 | if (slot_type[i] != STACK_MISC) { |
17a52670 AS |
658 | verbose("invalid read from stack off %d+%d size %d\n", |
659 | off, i, size); | |
660 | return -EACCES; | |
661 | } | |
662 | } | |
663 | if (value_regno >= 0) | |
664 | /* have read misc data from the stack */ | |
f0318d01 GB |
665 | mark_reg_unknown_value_and_range(state->regs, |
666 | value_regno); | |
17a52670 AS |
667 | return 0; |
668 | } | |
669 | } | |
670 | ||
671 | /* check read/write into map element returned by bpf_map_lookup_elem() */ | |
58e2af8b | 672 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
17a52670 AS |
673 | int size) |
674 | { | |
675 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; | |
676 | ||
5722569b | 677 | if (off < 0 || size <= 0 || off + size > map->value_size) { |
17a52670 AS |
678 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", |
679 | map->value_size, off, size); | |
680 | return -EACCES; | |
681 | } | |
682 | return 0; | |
683 | } | |
684 | ||
dbcfe5f7 GB |
685 | /* check read/write into an adjusted map element */ |
686 | static int check_map_access_adj(struct bpf_verifier_env *env, u32 regno, | |
687 | int off, int size) | |
688 | { | |
689 | struct bpf_verifier_state *state = &env->cur_state; | |
690 | struct bpf_reg_state *reg = &state->regs[regno]; | |
691 | int err; | |
692 | ||
693 | /* We adjusted the register to this map value, so we | |
694 | * need to change off and size to min_value and max_value | |
695 | * respectively to make sure our theoretical access will be | |
696 | * safe. | |
697 | */ | |
698 | if (log_level) | |
699 | print_verifier_state(state); | |
700 | env->varlen_map_value_access = true; | |
701 | /* The minimum value is only important with signed | |
702 | * comparisons where we can't assume the floor of a | |
703 | * value is 0. If we are using signed variables for our | |
704 | * index'es we need to make sure that whatever we use | |
705 | * will have a set floor within our range. | |
706 | */ | |
707 | if (reg->min_value < 0) { | |
708 | verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
709 | regno); | |
710 | return -EACCES; | |
711 | } | |
712 | err = check_map_access(env, regno, reg->min_value + off, size); | |
713 | if (err) { | |
714 | verbose("R%d min value is outside of the array range\n", | |
715 | regno); | |
716 | return err; | |
717 | } | |
718 | ||
719 | /* If we haven't set a max value then we need to bail | |
720 | * since we can't be sure we won't do bad things. | |
721 | */ | |
722 | if (reg->max_value == BPF_REGISTER_MAX_RANGE) { | |
723 | verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n", | |
724 | regno); | |
725 | return -EACCES; | |
726 | } | |
727 | return check_map_access(env, regno, reg->max_value + off, size); | |
728 | } | |
729 | ||
969bf05e AS |
730 | #define MAX_PACKET_OFF 0xffff |
731 | ||
58e2af8b | 732 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
733 | const struct bpf_call_arg_meta *meta, |
734 | enum bpf_access_type t) | |
4acf6c0b | 735 | { |
36bbef52 | 736 | switch (env->prog->type) { |
3a0af8fd TG |
737 | case BPF_PROG_TYPE_LWT_IN: |
738 | case BPF_PROG_TYPE_LWT_OUT: | |
739 | /* dst_input() and dst_output() can't write for now */ | |
740 | if (t == BPF_WRITE) | |
741 | return false; | |
7e57fbb2 | 742 | /* fallthrough */ |
36bbef52 DB |
743 | case BPF_PROG_TYPE_SCHED_CLS: |
744 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 745 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 746 | case BPF_PROG_TYPE_LWT_XMIT: |
36bbef52 DB |
747 | if (meta) |
748 | return meta->pkt_access; | |
749 | ||
750 | env->seen_direct_write = true; | |
4acf6c0b BB |
751 | return true; |
752 | default: | |
753 | return false; | |
754 | } | |
755 | } | |
756 | ||
58e2af8b | 757 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
969bf05e AS |
758 | int size) |
759 | { | |
58e2af8b JK |
760 | struct bpf_reg_state *regs = env->cur_state.regs; |
761 | struct bpf_reg_state *reg = ®s[regno]; | |
969bf05e | 762 | |
d91b28ed | 763 | off += reg->off; |
b399cf64 | 764 | if (off < 0 || size <= 0 || off + size > reg->range) { |
d91b28ed AS |
765 | verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
766 | off, size, regno, reg->id, reg->off, reg->range); | |
969bf05e AS |
767 | return -EACCES; |
768 | } | |
769 | return 0; | |
770 | } | |
771 | ||
17a52670 | 772 | /* check access to 'struct bpf_context' fields */ |
31fd8581 | 773 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
19de99f7 | 774 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 775 | { |
f96da094 DB |
776 | struct bpf_insn_access_aux info = { |
777 | .reg_type = *reg_type, | |
778 | }; | |
31fd8581 | 779 | |
13a27dfc JK |
780 | /* for analyzer ctx accesses are already validated and converted */ |
781 | if (env->analyzer_ops) | |
782 | return 0; | |
783 | ||
17a52670 | 784 | if (env->prog->aux->ops->is_valid_access && |
23994631 | 785 | env->prog->aux->ops->is_valid_access(off, size, t, &info)) { |
f96da094 DB |
786 | /* A non zero info.ctx_field_size indicates that this field is a |
787 | * candidate for later verifier transformation to load the whole | |
788 | * field and then apply a mask when accessed with a narrower | |
789 | * access than actual ctx access size. A zero info.ctx_field_size | |
790 | * will only allow for whole field access and rejects any other | |
791 | * type of narrower access. | |
31fd8581 | 792 | */ |
f96da094 | 793 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
23994631 | 794 | *reg_type = info.reg_type; |
31fd8581 | 795 | |
32bbe007 AS |
796 | /* remember the offset of last byte accessed in ctx */ |
797 | if (env->prog->aux->max_ctx_offset < off + size) | |
798 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 799 | return 0; |
32bbe007 | 800 | } |
17a52670 AS |
801 | |
802 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); | |
803 | return -EACCES; | |
804 | } | |
805 | ||
4cabc5b1 DB |
806 | static bool __is_pointer_value(bool allow_ptr_leaks, |
807 | const struct bpf_reg_state *reg) | |
1be7f75d | 808 | { |
4cabc5b1 | 809 | if (allow_ptr_leaks) |
1be7f75d AS |
810 | return false; |
811 | ||
4cabc5b1 | 812 | switch (reg->type) { |
1be7f75d AS |
813 | case UNKNOWN_VALUE: |
814 | case CONST_IMM: | |
815 | return false; | |
816 | default: | |
817 | return true; | |
818 | } | |
819 | } | |
820 | ||
4cabc5b1 DB |
821 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
822 | { | |
823 | return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]); | |
824 | } | |
825 | ||
79adffcd | 826 | static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg, |
d1174416 | 827 | int off, int size, bool strict) |
969bf05e | 828 | { |
e07b98d9 | 829 | int ip_align; |
d1174416 DM |
830 | int reg_off; |
831 | ||
832 | /* Byte size accesses are always allowed. */ | |
833 | if (!strict || size == 1) | |
834 | return 0; | |
835 | ||
836 | reg_off = reg->off; | |
837 | if (reg->id) { | |
838 | if (reg->aux_off_align % size) { | |
839 | verbose("Packet access is only %u byte aligned, %d byte access not allowed\n", | |
840 | reg->aux_off_align, size); | |
841 | return -EACCES; | |
842 | } | |
843 | reg_off += reg->aux_off; | |
969bf05e AS |
844 | } |
845 | ||
e4eda884 DM |
846 | /* For platforms that do not have a Kconfig enabling |
847 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
848 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
849 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
850 | * to this code only in strict mode where we want to emulate | |
851 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
852 | * unconditional IP align value of '2'. | |
e07b98d9 | 853 | */ |
e4eda884 | 854 | ip_align = 2; |
e07b98d9 | 855 | if ((ip_align + reg_off + off) % size != 0) { |
969bf05e | 856 | verbose("misaligned packet access off %d+%d+%d size %d\n", |
e07b98d9 | 857 | ip_align, reg_off, off, size); |
969bf05e AS |
858 | return -EACCES; |
859 | } | |
79adffcd | 860 | |
969bf05e AS |
861 | return 0; |
862 | } | |
863 | ||
79adffcd | 864 | static int check_val_ptr_alignment(const struct bpf_reg_state *reg, |
d1174416 | 865 | int size, bool strict) |
79adffcd | 866 | { |
d1174416 | 867 | if (strict && size != 1) { |
79adffcd DB |
868 | verbose("Unknown alignment. Only byte-sized access allowed in value access.\n"); |
869 | return -EACCES; | |
870 | } | |
871 | ||
969bf05e AS |
872 | return 0; |
873 | } | |
874 | ||
e07b98d9 DM |
875 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
876 | const struct bpf_reg_state *reg, | |
79adffcd DB |
877 | int off, int size) |
878 | { | |
e07b98d9 | 879 | bool strict = env->strict_alignment; |
d1174416 | 880 | |
79adffcd DB |
881 | switch (reg->type) { |
882 | case PTR_TO_PACKET: | |
d1174416 | 883 | return check_pkt_ptr_alignment(reg, off, size, strict); |
79adffcd | 884 | case PTR_TO_MAP_VALUE_ADJ: |
d1174416 | 885 | return check_val_ptr_alignment(reg, size, strict); |
79adffcd DB |
886 | default: |
887 | if (off % size != 0) { | |
888 | verbose("misaligned access off %d size %d\n", | |
889 | off, size); | |
890 | return -EACCES; | |
891 | } | |
892 | ||
893 | return 0; | |
894 | } | |
895 | } | |
896 | ||
17a52670 AS |
897 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
898 | * if t==write, value_regno is a register which value is stored into memory | |
899 | * if t==read, value_regno is a register which will receive the value from memory | |
900 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
901 | * if t==read && value_regno==-1, don't care what we read from memory | |
902 | */ | |
31fd8581 | 903 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off, |
17a52670 AS |
904 | int bpf_size, enum bpf_access_type t, |
905 | int value_regno) | |
906 | { | |
58e2af8b JK |
907 | struct bpf_verifier_state *state = &env->cur_state; |
908 | struct bpf_reg_state *reg = &state->regs[regno]; | |
17a52670 AS |
909 | int size, err = 0; |
910 | ||
1a0dc1ac AS |
911 | if (reg->type == PTR_TO_STACK) |
912 | off += reg->imm; | |
24b4d2ab | 913 | |
17a52670 AS |
914 | size = bpf_size_to_bytes(bpf_size); |
915 | if (size < 0) | |
916 | return size; | |
917 | ||
e07b98d9 | 918 | err = check_ptr_alignment(env, reg, off, size); |
969bf05e AS |
919 | if (err) |
920 | return err; | |
17a52670 | 921 | |
48461135 JB |
922 | if (reg->type == PTR_TO_MAP_VALUE || |
923 | reg->type == PTR_TO_MAP_VALUE_ADJ) { | |
1be7f75d AS |
924 | if (t == BPF_WRITE && value_regno >= 0 && |
925 | is_pointer_value(env, value_regno)) { | |
926 | verbose("R%d leaks addr into map\n", value_regno); | |
927 | return -EACCES; | |
928 | } | |
48461135 | 929 | |
dbcfe5f7 GB |
930 | if (reg->type == PTR_TO_MAP_VALUE_ADJ) |
931 | err = check_map_access_adj(env, regno, off, size); | |
932 | else | |
933 | err = check_map_access(env, regno, off, size); | |
17a52670 | 934 | if (!err && t == BPF_READ && value_regno >= 0) |
f0318d01 GB |
935 | mark_reg_unknown_value_and_range(state->regs, |
936 | value_regno); | |
17a52670 | 937 | |
1a0dc1ac | 938 | } else if (reg->type == PTR_TO_CTX) { |
19de99f7 AS |
939 | enum bpf_reg_type reg_type = UNKNOWN_VALUE; |
940 | ||
1be7f75d AS |
941 | if (t == BPF_WRITE && value_regno >= 0 && |
942 | is_pointer_value(env, value_regno)) { | |
943 | verbose("R%d leaks addr into ctx\n", value_regno); | |
944 | return -EACCES; | |
945 | } | |
31fd8581 | 946 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
969bf05e | 947 | if (!err && t == BPF_READ && value_regno >= 0) { |
f0318d01 GB |
948 | mark_reg_unknown_value_and_range(state->regs, |
949 | value_regno); | |
1955351d MS |
950 | /* note that reg.[id|off|range] == 0 */ |
951 | state->regs[value_regno].type = reg_type; | |
d1174416 DM |
952 | state->regs[value_regno].aux_off = 0; |
953 | state->regs[value_regno].aux_off_align = 0; | |
969bf05e | 954 | } |
17a52670 | 955 | |
1a0dc1ac | 956 | } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) { |
17a52670 AS |
957 | if (off >= 0 || off < -MAX_BPF_STACK) { |
958 | verbose("invalid stack off=%d size=%d\n", off, size); | |
959 | return -EACCES; | |
960 | } | |
8726679a AS |
961 | |
962 | if (env->prog->aux->stack_depth < -off) | |
963 | env->prog->aux->stack_depth = -off; | |
964 | ||
1be7f75d AS |
965 | if (t == BPF_WRITE) { |
966 | if (!env->allow_ptr_leaks && | |
967 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && | |
968 | size != BPF_REG_SIZE) { | |
969 | verbose("attempt to corrupt spilled pointer on stack\n"); | |
970 | return -EACCES; | |
971 | } | |
674d8995 AS |
972 | err = check_stack_write(env, state, off, size, |
973 | value_regno, insn_idx); | |
1be7f75d | 974 | } else { |
17a52670 | 975 | err = check_stack_read(state, off, size, value_regno); |
1be7f75d | 976 | } |
969bf05e | 977 | } else if (state->regs[regno].type == PTR_TO_PACKET) { |
3a0af8fd | 978 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
969bf05e AS |
979 | verbose("cannot write into packet\n"); |
980 | return -EACCES; | |
981 | } | |
4acf6c0b BB |
982 | if (t == BPF_WRITE && value_regno >= 0 && |
983 | is_pointer_value(env, value_regno)) { | |
984 | verbose("R%d leaks addr into packet\n", value_regno); | |
985 | return -EACCES; | |
986 | } | |
969bf05e AS |
987 | err = check_packet_access(env, regno, off, size); |
988 | if (!err && t == BPF_READ && value_regno >= 0) | |
f0318d01 GB |
989 | mark_reg_unknown_value_and_range(state->regs, |
990 | value_regno); | |
17a52670 AS |
991 | } else { |
992 | verbose("R%d invalid mem access '%s'\n", | |
1a0dc1ac | 993 | regno, reg_type_str[reg->type]); |
17a52670 AS |
994 | return -EACCES; |
995 | } | |
969bf05e AS |
996 | |
997 | if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks && | |
998 | state->regs[value_regno].type == UNKNOWN_VALUE) { | |
999 | /* 1 or 2 byte load zero-extends, determine the number of | |
1000 | * zero upper bits. Not doing it fo 4 byte load, since | |
1001 | * such values cannot be added to ptr_to_packet anyway. | |
1002 | */ | |
1003 | state->regs[value_regno].imm = 64 - size * 8; | |
1004 | } | |
17a52670 AS |
1005 | return err; |
1006 | } | |
1007 | ||
31fd8581 | 1008 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 1009 | { |
58e2af8b | 1010 | struct bpf_reg_state *regs = env->cur_state.regs; |
17a52670 AS |
1011 | int err; |
1012 | ||
1013 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
1014 | insn->imm != 0) { | |
1015 | verbose("BPF_XADD uses reserved fields\n"); | |
1016 | return -EINVAL; | |
1017 | } | |
1018 | ||
1019 | /* check src1 operand */ | |
1020 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1021 | if (err) | |
1022 | return err; | |
1023 | ||
1024 | /* check src2 operand */ | |
1025 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1026 | if (err) | |
1027 | return err; | |
1028 | ||
6bdf6abc DB |
1029 | if (is_pointer_value(env, insn->src_reg)) { |
1030 | verbose("R%d leaks addr into mem\n", insn->src_reg); | |
1031 | return -EACCES; | |
1032 | } | |
1033 | ||
17a52670 | 1034 | /* check whether atomic_add can read the memory */ |
31fd8581 | 1035 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
1036 | BPF_SIZE(insn->code), BPF_READ, -1); |
1037 | if (err) | |
1038 | return err; | |
1039 | ||
1040 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 1041 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
1042 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
1043 | } | |
1044 | ||
1045 | /* when register 'regno' is passed into function that will read 'access_size' | |
1046 | * bytes from that pointer, make sure that it's within stack boundary | |
1047 | * and all elements of stack are initialized | |
1048 | */ | |
58e2af8b | 1049 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
1050 | int access_size, bool zero_size_allowed, |
1051 | struct bpf_call_arg_meta *meta) | |
17a52670 | 1052 | { |
58e2af8b JK |
1053 | struct bpf_verifier_state *state = &env->cur_state; |
1054 | struct bpf_reg_state *regs = state->regs; | |
17a52670 AS |
1055 | int off, i; |
1056 | ||
8e2fe1d9 DB |
1057 | if (regs[regno].type != PTR_TO_STACK) { |
1058 | if (zero_size_allowed && access_size == 0 && | |
1059 | regs[regno].type == CONST_IMM && | |
1060 | regs[regno].imm == 0) | |
1061 | return 0; | |
1062 | ||
1063 | verbose("R%d type=%s expected=%s\n", regno, | |
1064 | reg_type_str[regs[regno].type], | |
1065 | reg_type_str[PTR_TO_STACK]); | |
17a52670 | 1066 | return -EACCES; |
8e2fe1d9 | 1067 | } |
17a52670 AS |
1068 | |
1069 | off = regs[regno].imm; | |
1070 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
1071 | access_size <= 0) { | |
1072 | verbose("invalid stack type R%d off=%d access_size=%d\n", | |
1073 | regno, off, access_size); | |
1074 | return -EACCES; | |
1075 | } | |
1076 | ||
8726679a AS |
1077 | if (env->prog->aux->stack_depth < -off) |
1078 | env->prog->aux->stack_depth = -off; | |
1079 | ||
435faee1 DB |
1080 | if (meta && meta->raw_mode) { |
1081 | meta->access_size = access_size; | |
1082 | meta->regno = regno; | |
1083 | return 0; | |
1084 | } | |
1085 | ||
17a52670 | 1086 | for (i = 0; i < access_size; i++) { |
9c399760 | 1087 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
17a52670 AS |
1088 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
1089 | off, i, access_size); | |
1090 | return -EACCES; | |
1091 | } | |
1092 | } | |
1093 | return 0; | |
1094 | } | |
1095 | ||
06c1c049 GB |
1096 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
1097 | int access_size, bool zero_size_allowed, | |
1098 | struct bpf_call_arg_meta *meta) | |
1099 | { | |
1100 | struct bpf_reg_state *regs = env->cur_state.regs; | |
1101 | ||
1102 | switch (regs[regno].type) { | |
1103 | case PTR_TO_PACKET: | |
1104 | return check_packet_access(env, regno, 0, access_size); | |
1105 | case PTR_TO_MAP_VALUE: | |
1106 | return check_map_access(env, regno, 0, access_size); | |
1107 | case PTR_TO_MAP_VALUE_ADJ: | |
1108 | return check_map_access_adj(env, regno, 0, access_size); | |
1109 | default: /* const_imm|ptr_to_stack or invalid ptr */ | |
1110 | return check_stack_boundary(env, regno, access_size, | |
1111 | zero_size_allowed, meta); | |
1112 | } | |
1113 | } | |
1114 | ||
58e2af8b | 1115 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
1116 | enum bpf_arg_type arg_type, |
1117 | struct bpf_call_arg_meta *meta) | |
17a52670 | 1118 | { |
58e2af8b | 1119 | struct bpf_reg_state *regs = env->cur_state.regs, *reg = ®s[regno]; |
6841de8b | 1120 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
1121 | int err = 0; |
1122 | ||
80f1d68c | 1123 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
1124 | return 0; |
1125 | ||
6841de8b | 1126 | if (type == NOT_INIT) { |
17a52670 AS |
1127 | verbose("R%d !read_ok\n", regno); |
1128 | return -EACCES; | |
1129 | } | |
1130 | ||
1be7f75d AS |
1131 | if (arg_type == ARG_ANYTHING) { |
1132 | if (is_pointer_value(env, regno)) { | |
1133 | verbose("R%d leaks addr into helper function\n", regno); | |
1134 | return -EACCES; | |
1135 | } | |
80f1d68c | 1136 | return 0; |
1be7f75d | 1137 | } |
80f1d68c | 1138 | |
3a0af8fd TG |
1139 | if (type == PTR_TO_PACKET && |
1140 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { | |
36bbef52 | 1141 | verbose("helper access to the packet is not allowed\n"); |
6841de8b AS |
1142 | return -EACCES; |
1143 | } | |
1144 | ||
8e2fe1d9 | 1145 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
17a52670 AS |
1146 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
1147 | expected_type = PTR_TO_STACK; | |
6841de8b AS |
1148 | if (type != PTR_TO_PACKET && type != expected_type) |
1149 | goto err_type; | |
39f19ebb AS |
1150 | } else if (arg_type == ARG_CONST_SIZE || |
1151 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
17a52670 | 1152 | expected_type = CONST_IMM; |
06c1c049 GB |
1153 | /* One exception. Allow UNKNOWN_VALUE registers when the |
1154 | * boundaries are known and don't cause unsafe memory accesses | |
1155 | */ | |
1156 | if (type != UNKNOWN_VALUE && type != expected_type) | |
6841de8b | 1157 | goto err_type; |
17a52670 AS |
1158 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
1159 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
1160 | if (type != expected_type) |
1161 | goto err_type; | |
608cd71a AS |
1162 | } else if (arg_type == ARG_PTR_TO_CTX) { |
1163 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
1164 | if (type != expected_type) |
1165 | goto err_type; | |
39f19ebb AS |
1166 | } else if (arg_type == ARG_PTR_TO_MEM || |
1167 | arg_type == ARG_PTR_TO_UNINIT_MEM) { | |
8e2fe1d9 DB |
1168 | expected_type = PTR_TO_STACK; |
1169 | /* One exception here. In case function allows for NULL to be | |
1170 | * passed in as argument, it's a CONST_IMM type. Final test | |
1171 | * happens during stack boundary checking. | |
1172 | */ | |
6841de8b AS |
1173 | if (type == CONST_IMM && reg->imm == 0) |
1174 | /* final test in check_stack_boundary() */; | |
5722569b GB |
1175 | else if (type != PTR_TO_PACKET && type != PTR_TO_MAP_VALUE && |
1176 | type != PTR_TO_MAP_VALUE_ADJ && type != expected_type) | |
6841de8b | 1177 | goto err_type; |
39f19ebb | 1178 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
17a52670 AS |
1179 | } else { |
1180 | verbose("unsupported arg_type %d\n", arg_type); | |
1181 | return -EFAULT; | |
1182 | } | |
1183 | ||
17a52670 AS |
1184 | if (arg_type == ARG_CONST_MAP_PTR) { |
1185 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 1186 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
1187 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
1188 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
1189 | * check that [key, key + map->key_size) are within | |
1190 | * stack limits and initialized | |
1191 | */ | |
33ff9823 | 1192 | if (!meta->map_ptr) { |
17a52670 AS |
1193 | /* in function declaration map_ptr must come before |
1194 | * map_key, so that it's verified and known before | |
1195 | * we have to check map_key here. Otherwise it means | |
1196 | * that kernel subsystem misconfigured verifier | |
1197 | */ | |
1198 | verbose("invalid map_ptr to access map->key\n"); | |
1199 | return -EACCES; | |
1200 | } | |
6841de8b AS |
1201 | if (type == PTR_TO_PACKET) |
1202 | err = check_packet_access(env, regno, 0, | |
1203 | meta->map_ptr->key_size); | |
1204 | else | |
1205 | err = check_stack_boundary(env, regno, | |
1206 | meta->map_ptr->key_size, | |
1207 | false, NULL); | |
17a52670 AS |
1208 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
1209 | /* bpf_map_xxx(..., map_ptr, ..., value) call: | |
1210 | * check [value, value + map->value_size) validity | |
1211 | */ | |
33ff9823 | 1212 | if (!meta->map_ptr) { |
17a52670 AS |
1213 | /* kernel subsystem misconfigured verifier */ |
1214 | verbose("invalid map_ptr to access map->value\n"); | |
1215 | return -EACCES; | |
1216 | } | |
6841de8b AS |
1217 | if (type == PTR_TO_PACKET) |
1218 | err = check_packet_access(env, regno, 0, | |
1219 | meta->map_ptr->value_size); | |
1220 | else | |
1221 | err = check_stack_boundary(env, regno, | |
1222 | meta->map_ptr->value_size, | |
1223 | false, NULL); | |
39f19ebb AS |
1224 | } else if (arg_type == ARG_CONST_SIZE || |
1225 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
1226 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); | |
17a52670 | 1227 | |
17a52670 AS |
1228 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
1229 | * from stack pointer 'buf'. Check it | |
1230 | * note: regno == len, regno - 1 == buf | |
1231 | */ | |
1232 | if (regno == 0) { | |
1233 | /* kernel subsystem misconfigured verifier */ | |
39f19ebb | 1234 | verbose("ARG_CONST_SIZE cannot be first argument\n"); |
17a52670 AS |
1235 | return -EACCES; |
1236 | } | |
06c1c049 GB |
1237 | |
1238 | /* If the register is UNKNOWN_VALUE, the access check happens | |
1239 | * using its boundaries. Otherwise, just use its imm | |
1240 | */ | |
1241 | if (type == UNKNOWN_VALUE) { | |
1242 | /* For unprivileged variable accesses, disable raw | |
1243 | * mode so that the program is required to | |
1244 | * initialize all the memory that the helper could | |
1245 | * just partially fill up. | |
1246 | */ | |
1247 | meta = NULL; | |
1248 | ||
1249 | if (reg->min_value < 0) { | |
1250 | verbose("R%d min value is negative, either use unsigned or 'var &= const'\n", | |
1251 | regno); | |
1252 | return -EACCES; | |
1253 | } | |
1254 | ||
1255 | if (reg->min_value == 0) { | |
1256 | err = check_helper_mem_access(env, regno - 1, 0, | |
1257 | zero_size_allowed, | |
1258 | meta); | |
1259 | if (err) | |
1260 | return err; | |
1261 | } | |
1262 | ||
1263 | if (reg->max_value == BPF_REGISTER_MAX_RANGE) { | |
1264 | verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
1265 | regno); | |
1266 | return -EACCES; | |
1267 | } | |
1268 | err = check_helper_mem_access(env, regno - 1, | |
1269 | reg->max_value, | |
1270 | zero_size_allowed, meta); | |
1271 | if (err) | |
1272 | return err; | |
1273 | } else { | |
1274 | /* register is CONST_IMM */ | |
1275 | err = check_helper_mem_access(env, regno - 1, reg->imm, | |
1276 | zero_size_allowed, meta); | |
1277 | } | |
17a52670 AS |
1278 | } |
1279 | ||
1280 | return err; | |
6841de8b AS |
1281 | err_type: |
1282 | verbose("R%d type=%s expected=%s\n", regno, | |
1283 | reg_type_str[type], reg_type_str[expected_type]); | |
1284 | return -EACCES; | |
17a52670 AS |
1285 | } |
1286 | ||
35578d79 KX |
1287 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
1288 | { | |
35578d79 KX |
1289 | if (!map) |
1290 | return 0; | |
1291 | ||
6aff67c8 AS |
1292 | /* We need a two way check, first is from map perspective ... */ |
1293 | switch (map->map_type) { | |
1294 | case BPF_MAP_TYPE_PROG_ARRAY: | |
1295 | if (func_id != BPF_FUNC_tail_call) | |
1296 | goto error; | |
1297 | break; | |
1298 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
1299 | if (func_id != BPF_FUNC_perf_event_read && | |
1300 | func_id != BPF_FUNC_perf_event_output) | |
1301 | goto error; | |
1302 | break; | |
1303 | case BPF_MAP_TYPE_STACK_TRACE: | |
1304 | if (func_id != BPF_FUNC_get_stackid) | |
1305 | goto error; | |
1306 | break; | |
4ed8ec52 | 1307 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 1308 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 1309 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
1310 | goto error; |
1311 | break; | |
56f668df | 1312 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 1313 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
1314 | if (func_id != BPF_FUNC_map_lookup_elem) |
1315 | goto error; | |
6aff67c8 AS |
1316 | default: |
1317 | break; | |
1318 | } | |
1319 | ||
1320 | /* ... and second from the function itself. */ | |
1321 | switch (func_id) { | |
1322 | case BPF_FUNC_tail_call: | |
1323 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
1324 | goto error; | |
1325 | break; | |
1326 | case BPF_FUNC_perf_event_read: | |
1327 | case BPF_FUNC_perf_event_output: | |
1328 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) | |
1329 | goto error; | |
1330 | break; | |
1331 | case BPF_FUNC_get_stackid: | |
1332 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
1333 | goto error; | |
1334 | break; | |
60d20f91 | 1335 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 1336 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
1337 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
1338 | goto error; | |
1339 | break; | |
6aff67c8 AS |
1340 | default: |
1341 | break; | |
35578d79 KX |
1342 | } |
1343 | ||
1344 | return 0; | |
6aff67c8 | 1345 | error: |
ebb676da TG |
1346 | verbose("cannot pass map_type %d into func %s#%d\n", |
1347 | map->map_type, func_id_name(func_id), func_id); | |
6aff67c8 | 1348 | return -EINVAL; |
35578d79 KX |
1349 | } |
1350 | ||
435faee1 DB |
1351 | static int check_raw_mode(const struct bpf_func_proto *fn) |
1352 | { | |
1353 | int count = 0; | |
1354 | ||
39f19ebb | 1355 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1356 | count++; |
39f19ebb | 1357 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1358 | count++; |
39f19ebb | 1359 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1360 | count++; |
39f19ebb | 1361 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1362 | count++; |
39f19ebb | 1363 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
1364 | count++; |
1365 | ||
1366 | return count > 1 ? -EINVAL : 0; | |
1367 | } | |
1368 | ||
58e2af8b | 1369 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 1370 | { |
58e2af8b JK |
1371 | struct bpf_verifier_state *state = &env->cur_state; |
1372 | struct bpf_reg_state *regs = state->regs, *reg; | |
969bf05e AS |
1373 | int i; |
1374 | ||
1375 | for (i = 0; i < MAX_BPF_REG; i++) | |
1376 | if (regs[i].type == PTR_TO_PACKET || | |
1377 | regs[i].type == PTR_TO_PACKET_END) | |
1378 | mark_reg_unknown_value(regs, i); | |
1379 | ||
1380 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { | |
1381 | if (state->stack_slot_type[i] != STACK_SPILL) | |
1382 | continue; | |
1383 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; | |
1384 | if (reg->type != PTR_TO_PACKET && | |
1385 | reg->type != PTR_TO_PACKET_END) | |
1386 | continue; | |
36e24c00 DB |
1387 | __mark_reg_unknown_value(state->spilled_regs, |
1388 | i / BPF_REG_SIZE); | |
969bf05e AS |
1389 | } |
1390 | } | |
1391 | ||
81ed18ab | 1392 | static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 1393 | { |
58e2af8b | 1394 | struct bpf_verifier_state *state = &env->cur_state; |
17a52670 | 1395 | const struct bpf_func_proto *fn = NULL; |
58e2af8b | 1396 | struct bpf_reg_state *regs = state->regs; |
33ff9823 | 1397 | struct bpf_call_arg_meta meta; |
969bf05e | 1398 | bool changes_data; |
17a52670 AS |
1399 | int i, err; |
1400 | ||
1401 | /* find function prototype */ | |
1402 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
ebb676da | 1403 | verbose("invalid func %s#%d\n", func_id_name(func_id), func_id); |
17a52670 AS |
1404 | return -EINVAL; |
1405 | } | |
1406 | ||
1407 | if (env->prog->aux->ops->get_func_proto) | |
1408 | fn = env->prog->aux->ops->get_func_proto(func_id); | |
1409 | ||
1410 | if (!fn) { | |
ebb676da | 1411 | verbose("unknown func %s#%d\n", func_id_name(func_id), func_id); |
17a52670 AS |
1412 | return -EINVAL; |
1413 | } | |
1414 | ||
1415 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 1416 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
17a52670 AS |
1417 | verbose("cannot call GPL only function from proprietary program\n"); |
1418 | return -EINVAL; | |
1419 | } | |
1420 | ||
17bedab2 | 1421 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
969bf05e | 1422 | |
33ff9823 | 1423 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 1424 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 1425 | |
435faee1 DB |
1426 | /* We only support one arg being in raw mode at the moment, which |
1427 | * is sufficient for the helper functions we have right now. | |
1428 | */ | |
1429 | err = check_raw_mode(fn); | |
1430 | if (err) { | |
ebb676da TG |
1431 | verbose("kernel subsystem misconfigured func %s#%d\n", |
1432 | func_id_name(func_id), func_id); | |
435faee1 DB |
1433 | return err; |
1434 | } | |
1435 | ||
17a52670 | 1436 | /* check args */ |
33ff9823 | 1437 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
1438 | if (err) |
1439 | return err; | |
33ff9823 | 1440 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
1441 | if (err) |
1442 | return err; | |
33ff9823 | 1443 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
1444 | if (err) |
1445 | return err; | |
33ff9823 | 1446 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
1447 | if (err) |
1448 | return err; | |
33ff9823 | 1449 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
1450 | if (err) |
1451 | return err; | |
1452 | ||
435faee1 DB |
1453 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
1454 | * is inferred from register state. | |
1455 | */ | |
1456 | for (i = 0; i < meta.access_size; i++) { | |
31fd8581 | 1457 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1); |
435faee1 DB |
1458 | if (err) |
1459 | return err; | |
1460 | } | |
1461 | ||
17a52670 | 1462 | /* reset caller saved regs */ |
a9789ef9 DB |
1463 | for (i = 0; i < CALLER_SAVED_REGS; i++) |
1464 | mark_reg_not_init(regs, caller_saved[i]); | |
17a52670 AS |
1465 | |
1466 | /* update return register */ | |
1467 | if (fn->ret_type == RET_INTEGER) { | |
1468 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
1469 | } else if (fn->ret_type == RET_VOID) { | |
1470 | regs[BPF_REG_0].type = NOT_INIT; | |
1471 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { | |
fad73a1a MKL |
1472 | struct bpf_insn_aux_data *insn_aux; |
1473 | ||
17a52670 | 1474 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
48461135 | 1475 | regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0; |
17a52670 AS |
1476 | /* remember map_ptr, so that check_map_access() |
1477 | * can check 'value_size' boundary of memory access | |
1478 | * to map element returned from bpf_map_lookup_elem() | |
1479 | */ | |
33ff9823 | 1480 | if (meta.map_ptr == NULL) { |
17a52670 AS |
1481 | verbose("kernel subsystem misconfigured verifier\n"); |
1482 | return -EINVAL; | |
1483 | } | |
33ff9823 | 1484 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
57a09bf0 | 1485 | regs[BPF_REG_0].id = ++env->id_gen; |
fad73a1a MKL |
1486 | insn_aux = &env->insn_aux_data[insn_idx]; |
1487 | if (!insn_aux->map_ptr) | |
1488 | insn_aux->map_ptr = meta.map_ptr; | |
1489 | else if (insn_aux->map_ptr != meta.map_ptr) | |
1490 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; | |
17a52670 | 1491 | } else { |
ebb676da TG |
1492 | verbose("unknown return type %d of func %s#%d\n", |
1493 | fn->ret_type, func_id_name(func_id), func_id); | |
17a52670 AS |
1494 | return -EINVAL; |
1495 | } | |
04fd61ab | 1496 | |
33ff9823 | 1497 | err = check_map_func_compatibility(meta.map_ptr, func_id); |
35578d79 KX |
1498 | if (err) |
1499 | return err; | |
04fd61ab | 1500 | |
969bf05e AS |
1501 | if (changes_data) |
1502 | clear_all_pkt_pointers(env); | |
1503 | return 0; | |
1504 | } | |
1505 | ||
58e2af8b JK |
1506 | static int check_packet_ptr_add(struct bpf_verifier_env *env, |
1507 | struct bpf_insn *insn) | |
969bf05e | 1508 | { |
58e2af8b JK |
1509 | struct bpf_reg_state *regs = env->cur_state.regs; |
1510 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; | |
1511 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; | |
1512 | struct bpf_reg_state tmp_reg; | |
969bf05e AS |
1513 | s32 imm; |
1514 | ||
1515 | if (BPF_SRC(insn->code) == BPF_K) { | |
1516 | /* pkt_ptr += imm */ | |
1517 | imm = insn->imm; | |
1518 | ||
1519 | add_imm: | |
63dfef75 | 1520 | if (imm < 0) { |
969bf05e AS |
1521 | verbose("addition of negative constant to packet pointer is not allowed\n"); |
1522 | return -EACCES; | |
1523 | } | |
1524 | if (imm >= MAX_PACKET_OFF || | |
1525 | imm + dst_reg->off >= MAX_PACKET_OFF) { | |
1526 | verbose("constant %d is too large to add to packet pointer\n", | |
1527 | imm); | |
1528 | return -EACCES; | |
1529 | } | |
1530 | /* a constant was added to pkt_ptr. | |
1531 | * Remember it while keeping the same 'id' | |
1532 | */ | |
1533 | dst_reg->off += imm; | |
1534 | } else { | |
d1174416 DM |
1535 | bool had_id; |
1536 | ||
1b9b69ec AS |
1537 | if (src_reg->type == PTR_TO_PACKET) { |
1538 | /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */ | |
1539 | tmp_reg = *dst_reg; /* save r7 state */ | |
1540 | *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */ | |
1541 | src_reg = &tmp_reg; /* pretend it's src_reg state */ | |
1542 | /* if the checks below reject it, the copy won't matter, | |
1543 | * since we're rejecting the whole program. If all ok, | |
1544 | * then imm22 state will be added to r7 | |
1545 | * and r7 will be pkt(id=0,off=22,r=62) while | |
1546 | * r6 will stay as pkt(id=0,off=0,r=62) | |
1547 | */ | |
1548 | } | |
1549 | ||
969bf05e AS |
1550 | if (src_reg->type == CONST_IMM) { |
1551 | /* pkt_ptr += reg where reg is known constant */ | |
1552 | imm = src_reg->imm; | |
1553 | goto add_imm; | |
1554 | } | |
1555 | /* disallow pkt_ptr += reg | |
1556 | * if reg is not uknown_value with guaranteed zero upper bits | |
1557 | * otherwise pkt_ptr may overflow and addition will become | |
1558 | * subtraction which is not allowed | |
1559 | */ | |
1560 | if (src_reg->type != UNKNOWN_VALUE) { | |
1561 | verbose("cannot add '%s' to ptr_to_packet\n", | |
1562 | reg_type_str[src_reg->type]); | |
1563 | return -EACCES; | |
1564 | } | |
1565 | if (src_reg->imm < 48) { | |
1566 | verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n", | |
1567 | src_reg->imm); | |
1568 | return -EACCES; | |
1569 | } | |
d1174416 DM |
1570 | |
1571 | had_id = (dst_reg->id != 0); | |
1572 | ||
969bf05e AS |
1573 | /* dst_reg stays as pkt_ptr type and since some positive |
1574 | * integer value was added to the pointer, increment its 'id' | |
1575 | */ | |
1f415a74 | 1576 | dst_reg->id = ++env->id_gen; |
969bf05e | 1577 | |
d1174416 | 1578 | /* something was added to pkt_ptr, set range to zero */ |
6832a333 | 1579 | dst_reg->aux_off += dst_reg->off; |
969bf05e AS |
1580 | dst_reg->off = 0; |
1581 | dst_reg->range = 0; | |
d1174416 DM |
1582 | if (had_id) |
1583 | dst_reg->aux_off_align = min(dst_reg->aux_off_align, | |
1584 | src_reg->min_align); | |
1585 | else | |
1586 | dst_reg->aux_off_align = src_reg->min_align; | |
969bf05e AS |
1587 | } |
1588 | return 0; | |
1589 | } | |
1590 | ||
58e2af8b | 1591 | static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn) |
969bf05e | 1592 | { |
58e2af8b JK |
1593 | struct bpf_reg_state *regs = env->cur_state.regs; |
1594 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; | |
969bf05e AS |
1595 | u8 opcode = BPF_OP(insn->code); |
1596 | s64 imm_log2; | |
1597 | ||
1598 | /* for type == UNKNOWN_VALUE: | |
1599 | * imm > 0 -> number of zero upper bits | |
1600 | * imm == 0 -> don't track which is the same as all bits can be non-zero | |
1601 | */ | |
1602 | ||
1603 | if (BPF_SRC(insn->code) == BPF_X) { | |
58e2af8b | 1604 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
969bf05e AS |
1605 | |
1606 | if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 && | |
1607 | dst_reg->imm && opcode == BPF_ADD) { | |
1608 | /* dreg += sreg | |
1609 | * where both have zero upper bits. Adding them | |
1610 | * can only result making one more bit non-zero | |
1611 | * in the larger value. | |
1612 | * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47) | |
1613 | * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47) | |
1614 | */ | |
1615 | dst_reg->imm = min(dst_reg->imm, src_reg->imm); | |
1616 | dst_reg->imm--; | |
1617 | return 0; | |
1618 | } | |
1619 | if (src_reg->type == CONST_IMM && src_reg->imm > 0 && | |
1620 | dst_reg->imm && opcode == BPF_ADD) { | |
1621 | /* dreg += sreg | |
1622 | * where dreg has zero upper bits and sreg is const. | |
1623 | * Adding them can only result making one more bit | |
1624 | * non-zero in the larger value. | |
1625 | */ | |
1626 | imm_log2 = __ilog2_u64((long long)src_reg->imm); | |
1627 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); | |
1628 | dst_reg->imm--; | |
1629 | return 0; | |
1630 | } | |
1631 | /* all other cases non supported yet, just mark dst_reg */ | |
1632 | dst_reg->imm = 0; | |
1633 | return 0; | |
1634 | } | |
1635 | ||
1636 | /* sign extend 32-bit imm into 64-bit to make sure that | |
1637 | * negative values occupy bit 63. Note ilog2() would have | |
1638 | * been incorrect, since sizeof(insn->imm) == 4 | |
1639 | */ | |
1640 | imm_log2 = __ilog2_u64((long long)insn->imm); | |
1641 | ||
1642 | if (dst_reg->imm && opcode == BPF_LSH) { | |
1643 | /* reg <<= imm | |
1644 | * if reg was a result of 2 byte load, then its imm == 48 | |
1645 | * which means that upper 48 bits are zero and shifting this reg | |
1646 | * left by 4 would mean that upper 44 bits are still zero | |
1647 | */ | |
1648 | dst_reg->imm -= insn->imm; | |
1649 | } else if (dst_reg->imm && opcode == BPF_MUL) { | |
1650 | /* reg *= imm | |
1651 | * if multiplying by 14 subtract 4 | |
1652 | * This is conservative calculation of upper zero bits. | |
1653 | * It's not trying to special case insn->imm == 1 or 0 cases | |
1654 | */ | |
1655 | dst_reg->imm -= imm_log2 + 1; | |
1656 | } else if (opcode == BPF_AND) { | |
1657 | /* reg &= imm */ | |
1658 | dst_reg->imm = 63 - imm_log2; | |
1659 | } else if (dst_reg->imm && opcode == BPF_ADD) { | |
1660 | /* reg += imm */ | |
1661 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); | |
1662 | dst_reg->imm--; | |
1663 | } else if (opcode == BPF_RSH) { | |
1664 | /* reg >>= imm | |
1665 | * which means that after right shift, upper bits will be zero | |
1666 | * note that verifier already checked that | |
1667 | * 0 <= imm < 64 for shift insn | |
1668 | */ | |
1669 | dst_reg->imm += insn->imm; | |
1670 | if (unlikely(dst_reg->imm > 64)) | |
1671 | /* some dumb code did: | |
1672 | * r2 = *(u32 *)mem; | |
1673 | * r2 >>= 32; | |
1674 | * and all bits are zero now */ | |
1675 | dst_reg->imm = 64; | |
1676 | } else { | |
1677 | /* all other alu ops, means that we don't know what will | |
1678 | * happen to the value, mark it with unknown number of zero bits | |
1679 | */ | |
1680 | dst_reg->imm = 0; | |
1681 | } | |
1682 | ||
1683 | if (dst_reg->imm < 0) { | |
1684 | /* all 64 bits of the register can contain non-zero bits | |
1685 | * and such value cannot be added to ptr_to_packet, since it | |
1686 | * may overflow, mark it as unknown to avoid further eval | |
1687 | */ | |
1688 | dst_reg->imm = 0; | |
1689 | } | |
1690 | return 0; | |
1691 | } | |
1692 | ||
43188702 JF |
1693 | static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env *env, |
1694 | struct bpf_insn *insn) | |
1695 | { | |
1696 | struct bpf_reg_state *regs = env->cur_state.regs; | |
1697 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; | |
1698 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; | |
1699 | u8 opcode = BPF_OP(insn->code); | |
1700 | s64 imm_log2 = __ilog2_u64((long long)dst_reg->imm); | |
1701 | ||
1702 | /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */ | |
1703 | if (src_reg->imm > 0 && dst_reg->imm) { | |
1704 | switch (opcode) { | |
1705 | case BPF_ADD: | |
1706 | /* dreg += sreg | |
1707 | * where both have zero upper bits. Adding them | |
1708 | * can only result making one more bit non-zero | |
1709 | * in the larger value. | |
1710 | * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47) | |
1711 | * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47) | |
1712 | */ | |
1713 | dst_reg->imm = min(src_reg->imm, 63 - imm_log2); | |
1714 | dst_reg->imm--; | |
1715 | break; | |
1716 | case BPF_AND: | |
1717 | /* dreg &= sreg | |
1718 | * AND can not extend zero bits only shrink | |
1719 | * Ex. 0x00..00ffffff | |
1720 | * & 0x0f..ffffffff | |
1721 | * ---------------- | |
1722 | * 0x00..00ffffff | |
1723 | */ | |
1724 | dst_reg->imm = max(src_reg->imm, 63 - imm_log2); | |
1725 | break; | |
1726 | case BPF_OR: | |
1727 | /* dreg |= sreg | |
1728 | * OR can only extend zero bits | |
1729 | * Ex. 0x00..00ffffff | |
1730 | * | 0x0f..ffffffff | |
1731 | * ---------------- | |
1732 | * 0x0f..00ffffff | |
1733 | */ | |
1734 | dst_reg->imm = min(src_reg->imm, 63 - imm_log2); | |
1735 | break; | |
1736 | case BPF_SUB: | |
1737 | case BPF_MUL: | |
1738 | case BPF_RSH: | |
1739 | case BPF_LSH: | |
1740 | /* These may be flushed out later */ | |
1741 | default: | |
1742 | mark_reg_unknown_value(regs, insn->dst_reg); | |
1743 | } | |
1744 | } else { | |
1745 | mark_reg_unknown_value(regs, insn->dst_reg); | |
1746 | } | |
1747 | ||
1748 | dst_reg->type = UNKNOWN_VALUE; | |
1749 | return 0; | |
1750 | } | |
1751 | ||
58e2af8b JK |
1752 | static int evaluate_reg_imm_alu(struct bpf_verifier_env *env, |
1753 | struct bpf_insn *insn) | |
969bf05e | 1754 | { |
58e2af8b JK |
1755 | struct bpf_reg_state *regs = env->cur_state.regs; |
1756 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; | |
1757 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; | |
969bf05e | 1758 | u8 opcode = BPF_OP(insn->code); |
3fadc801 | 1759 | u64 dst_imm = dst_reg->imm; |
969bf05e | 1760 | |
43188702 JF |
1761 | if (BPF_SRC(insn->code) == BPF_X && src_reg->type == UNKNOWN_VALUE) |
1762 | return evaluate_reg_imm_alu_unknown(env, insn); | |
1763 | ||
3fadc801 DB |
1764 | /* dst_reg->type == CONST_IMM here. Simulate execution of insns |
1765 | * containing ALU ops. Don't care about overflow or negative | |
1766 | * values, just add/sub/... them; registers are in u64. | |
969bf05e | 1767 | */ |
3fadc801 DB |
1768 | if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) { |
1769 | dst_imm += insn->imm; | |
1770 | } else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X && | |
1771 | src_reg->type == CONST_IMM) { | |
1772 | dst_imm += src_reg->imm; | |
1773 | } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_K) { | |
1774 | dst_imm -= insn->imm; | |
1775 | } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_X && | |
1776 | src_reg->type == CONST_IMM) { | |
1777 | dst_imm -= src_reg->imm; | |
1778 | } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_K) { | |
1779 | dst_imm *= insn->imm; | |
1780 | } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_X && | |
1781 | src_reg->type == CONST_IMM) { | |
1782 | dst_imm *= src_reg->imm; | |
1783 | } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_K) { | |
1784 | dst_imm |= insn->imm; | |
1785 | } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_X && | |
1786 | src_reg->type == CONST_IMM) { | |
1787 | dst_imm |= src_reg->imm; | |
1788 | } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_K) { | |
1789 | dst_imm &= insn->imm; | |
1790 | } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_X && | |
1791 | src_reg->type == CONST_IMM) { | |
1792 | dst_imm &= src_reg->imm; | |
1793 | } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_K) { | |
1794 | dst_imm >>= insn->imm; | |
1795 | } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_X && | |
1796 | src_reg->type == CONST_IMM) { | |
1797 | dst_imm >>= src_reg->imm; | |
1798 | } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_K) { | |
1799 | dst_imm <<= insn->imm; | |
1800 | } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_X && | |
1801 | src_reg->type == CONST_IMM) { | |
1802 | dst_imm <<= src_reg->imm; | |
1803 | } else { | |
969bf05e | 1804 | mark_reg_unknown_value(regs, insn->dst_reg); |
3fadc801 DB |
1805 | goto out; |
1806 | } | |
1807 | ||
1808 | dst_reg->imm = dst_imm; | |
1809 | out: | |
17a52670 AS |
1810 | return 0; |
1811 | } | |
1812 | ||
48461135 JB |
1813 | static void check_reg_overflow(struct bpf_reg_state *reg) |
1814 | { | |
1815 | if (reg->max_value > BPF_REGISTER_MAX_RANGE) | |
1816 | reg->max_value = BPF_REGISTER_MAX_RANGE; | |
f23cc643 JB |
1817 | if (reg->min_value < BPF_REGISTER_MIN_RANGE || |
1818 | reg->min_value > BPF_REGISTER_MAX_RANGE) | |
48461135 JB |
1819 | reg->min_value = BPF_REGISTER_MIN_RANGE; |
1820 | } | |
1821 | ||
d1174416 DM |
1822 | static u32 calc_align(u32 imm) |
1823 | { | |
1824 | if (!imm) | |
1825 | return 1U << 31; | |
1826 | return imm - ((imm - 1) & imm); | |
1827 | } | |
1828 | ||
48461135 JB |
1829 | static void adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
1830 | struct bpf_insn *insn) | |
1831 | { | |
1832 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; | |
f23cc643 JB |
1833 | s64 min_val = BPF_REGISTER_MIN_RANGE; |
1834 | u64 max_val = BPF_REGISTER_MAX_RANGE; | |
48461135 | 1835 | u8 opcode = BPF_OP(insn->code); |
d1174416 | 1836 | u32 dst_align, src_align; |
48461135 JB |
1837 | |
1838 | dst_reg = ®s[insn->dst_reg]; | |
d1174416 | 1839 | src_align = 0; |
48461135 JB |
1840 | if (BPF_SRC(insn->code) == BPF_X) { |
1841 | check_reg_overflow(®s[insn->src_reg]); | |
1842 | min_val = regs[insn->src_reg].min_value; | |
1843 | max_val = regs[insn->src_reg].max_value; | |
1844 | ||
1845 | /* If the source register is a random pointer then the | |
1846 | * min_value/max_value values represent the range of the known | |
1847 | * accesses into that value, not the actual min/max value of the | |
1848 | * register itself. In this case we have to reset the reg range | |
1849 | * values so we know it is not safe to look at. | |
1850 | */ | |
1851 | if (regs[insn->src_reg].type != CONST_IMM && | |
1852 | regs[insn->src_reg].type != UNKNOWN_VALUE) { | |
1853 | min_val = BPF_REGISTER_MIN_RANGE; | |
1854 | max_val = BPF_REGISTER_MAX_RANGE; | |
d1174416 DM |
1855 | src_align = 0; |
1856 | } else { | |
1857 | src_align = regs[insn->src_reg].min_align; | |
48461135 JB |
1858 | } |
1859 | } else if (insn->imm < BPF_REGISTER_MAX_RANGE && | |
1860 | (s64)insn->imm > BPF_REGISTER_MIN_RANGE) { | |
1861 | min_val = max_val = insn->imm; | |
d1174416 | 1862 | src_align = calc_align(insn->imm); |
48461135 JB |
1863 | } |
1864 | ||
d1174416 DM |
1865 | dst_align = dst_reg->min_align; |
1866 | ||
48461135 | 1867 | /* We don't know anything about what was done to this register, mark it |
4cabc5b1 DB |
1868 | * as unknown. Also, if both derived bounds came from signed/unsigned |
1869 | * mixed compares and one side is unbounded, we cannot really do anything | |
1870 | * with them as boundaries cannot be trusted. Thus, arithmetic of two | |
1871 | * regs of such kind will get invalidated bounds on the dst side. | |
48461135 | 1872 | */ |
4cabc5b1 DB |
1873 | if ((min_val == BPF_REGISTER_MIN_RANGE && |
1874 | max_val == BPF_REGISTER_MAX_RANGE) || | |
1875 | (BPF_SRC(insn->code) == BPF_X && | |
1876 | ((min_val != BPF_REGISTER_MIN_RANGE && | |
1877 | max_val == BPF_REGISTER_MAX_RANGE) || | |
1878 | (min_val == BPF_REGISTER_MIN_RANGE && | |
1879 | max_val != BPF_REGISTER_MAX_RANGE) || | |
1880 | (dst_reg->min_value != BPF_REGISTER_MIN_RANGE && | |
1881 | dst_reg->max_value == BPF_REGISTER_MAX_RANGE) || | |
1882 | (dst_reg->min_value == BPF_REGISTER_MIN_RANGE && | |
1883 | dst_reg->max_value != BPF_REGISTER_MAX_RANGE)) && | |
1884 | regs[insn->dst_reg].value_from_signed != | |
1885 | regs[insn->src_reg].value_from_signed)) { | |
48461135 JB |
1886 | reset_reg_range_values(regs, insn->dst_reg); |
1887 | return; | |
1888 | } | |
1889 | ||
f23cc643 JB |
1890 | /* If one of our values was at the end of our ranges then we can't just |
1891 | * do our normal operations to the register, we need to set the values | |
1892 | * to the min/max since they are undefined. | |
1893 | */ | |
9305706c EC |
1894 | if (opcode != BPF_SUB) { |
1895 | if (min_val == BPF_REGISTER_MIN_RANGE) | |
1896 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; | |
1897 | if (max_val == BPF_REGISTER_MAX_RANGE) | |
1898 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; | |
1899 | } | |
f23cc643 | 1900 | |
48461135 JB |
1901 | switch (opcode) { |
1902 | case BPF_ADD: | |
f23cc643 JB |
1903 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
1904 | dst_reg->min_value += min_val; | |
1905 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) | |
1906 | dst_reg->max_value += max_val; | |
d1174416 | 1907 | dst_reg->min_align = min(src_align, dst_align); |
48461135 JB |
1908 | break; |
1909 | case BPF_SUB: | |
9305706c EC |
1910 | /* If one of our values was at the end of our ranges, then the |
1911 | * _opposite_ value in the dst_reg goes to the end of our range. | |
1912 | */ | |
1913 | if (min_val == BPF_REGISTER_MIN_RANGE) | |
1914 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; | |
1915 | if (max_val == BPF_REGISTER_MAX_RANGE) | |
1916 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; | |
f23cc643 | 1917 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
9305706c | 1918 | dst_reg->min_value -= max_val; |
f23cc643 | 1919 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
9305706c | 1920 | dst_reg->max_value -= min_val; |
d1174416 | 1921 | dst_reg->min_align = min(src_align, dst_align); |
48461135 JB |
1922 | break; |
1923 | case BPF_MUL: | |
f23cc643 JB |
1924 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
1925 | dst_reg->min_value *= min_val; | |
1926 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) | |
1927 | dst_reg->max_value *= max_val; | |
d1174416 | 1928 | dst_reg->min_align = max(src_align, dst_align); |
48461135 JB |
1929 | break; |
1930 | case BPF_AND: | |
f23cc643 JB |
1931 | /* Disallow AND'ing of negative numbers, ain't nobody got time |
1932 | * for that. Otherwise the minimum is 0 and the max is the max | |
1933 | * value we could AND against. | |
1934 | */ | |
1935 | if (min_val < 0) | |
1936 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; | |
1937 | else | |
1938 | dst_reg->min_value = 0; | |
48461135 | 1939 | dst_reg->max_value = max_val; |
d1174416 | 1940 | dst_reg->min_align = max(src_align, dst_align); |
48461135 JB |
1941 | break; |
1942 | case BPF_LSH: | |
1943 | /* Gotta have special overflow logic here, if we're shifting | |
1944 | * more than MAX_RANGE then just assume we have an invalid | |
1945 | * range. | |
1946 | */ | |
d1174416 | 1947 | if (min_val > ilog2(BPF_REGISTER_MAX_RANGE)) { |
48461135 | 1948 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
d1174416 DM |
1949 | dst_reg->min_align = 1; |
1950 | } else { | |
1951 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) | |
1952 | dst_reg->min_value <<= min_val; | |
1953 | if (!dst_reg->min_align) | |
1954 | dst_reg->min_align = 1; | |
1955 | dst_reg->min_align <<= min_val; | |
1956 | } | |
48461135 JB |
1957 | if (max_val > ilog2(BPF_REGISTER_MAX_RANGE)) |
1958 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; | |
f23cc643 | 1959 | else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
48461135 JB |
1960 | dst_reg->max_value <<= max_val; |
1961 | break; | |
1962 | case BPF_RSH: | |
f23cc643 JB |
1963 | /* RSH by a negative number is undefined, and the BPF_RSH is an |
1964 | * unsigned shift, so make the appropriate casts. | |
48461135 | 1965 | */ |
d1174416 | 1966 | if (min_val < 0 || dst_reg->min_value < 0) { |
f23cc643 | 1967 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
d1174416 | 1968 | } else { |
f23cc643 JB |
1969 | dst_reg->min_value = |
1970 | (u64)(dst_reg->min_value) >> min_val; | |
d1174416 DM |
1971 | } |
1972 | if (min_val < 0) { | |
1973 | dst_reg->min_align = 1; | |
1974 | } else { | |
1975 | dst_reg->min_align >>= (u64) min_val; | |
1976 | if (!dst_reg->min_align) | |
1977 | dst_reg->min_align = 1; | |
1978 | } | |
f23cc643 JB |
1979 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
1980 | dst_reg->max_value >>= max_val; | |
48461135 JB |
1981 | break; |
1982 | default: | |
1983 | reset_reg_range_values(regs, insn->dst_reg); | |
1984 | break; | |
1985 | } | |
1986 | ||
1987 | check_reg_overflow(dst_reg); | |
1988 | } | |
1989 | ||
17a52670 | 1990 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 1991 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 1992 | { |
58e2af8b | 1993 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; |
17a52670 AS |
1994 | u8 opcode = BPF_OP(insn->code); |
1995 | int err; | |
1996 | ||
1997 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
1998 | if (opcode == BPF_NEG) { | |
1999 | if (BPF_SRC(insn->code) != 0 || | |
2000 | insn->src_reg != BPF_REG_0 || | |
2001 | insn->off != 0 || insn->imm != 0) { | |
2002 | verbose("BPF_NEG uses reserved fields\n"); | |
2003 | return -EINVAL; | |
2004 | } | |
2005 | } else { | |
2006 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
ef0fb511 EC |
2007 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
2008 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
17a52670 AS |
2009 | verbose("BPF_END uses reserved fields\n"); |
2010 | return -EINVAL; | |
2011 | } | |
2012 | } | |
2013 | ||
2014 | /* check src operand */ | |
2015 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
2016 | if (err) | |
2017 | return err; | |
2018 | ||
1be7f75d AS |
2019 | if (is_pointer_value(env, insn->dst_reg)) { |
2020 | verbose("R%d pointer arithmetic prohibited\n", | |
2021 | insn->dst_reg); | |
2022 | return -EACCES; | |
2023 | } | |
2024 | ||
17a52670 AS |
2025 | /* check dest operand */ |
2026 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
2027 | if (err) | |
2028 | return err; | |
2029 | ||
2030 | } else if (opcode == BPF_MOV) { | |
2031 | ||
2032 | if (BPF_SRC(insn->code) == BPF_X) { | |
2033 | if (insn->imm != 0 || insn->off != 0) { | |
2034 | verbose("BPF_MOV uses reserved fields\n"); | |
2035 | return -EINVAL; | |
2036 | } | |
2037 | ||
2038 | /* check src operand */ | |
2039 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2040 | if (err) | |
2041 | return err; | |
2042 | } else { | |
2043 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
2044 | verbose("BPF_MOV uses reserved fields\n"); | |
2045 | return -EINVAL; | |
2046 | } | |
2047 | } | |
2048 | ||
2049 | /* check dest operand */ | |
2050 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
2051 | if (err) | |
2052 | return err; | |
2053 | ||
48461135 JB |
2054 | /* we are setting our register to something new, we need to |
2055 | * reset its range values. | |
2056 | */ | |
2057 | reset_reg_range_values(regs, insn->dst_reg); | |
2058 | ||
17a52670 AS |
2059 | if (BPF_SRC(insn->code) == BPF_X) { |
2060 | if (BPF_CLASS(insn->code) == BPF_ALU64) { | |
2061 | /* case: R1 = R2 | |
2062 | * copy register state to dest reg | |
2063 | */ | |
2064 | regs[insn->dst_reg] = regs[insn->src_reg]; | |
2065 | } else { | |
1be7f75d AS |
2066 | if (is_pointer_value(env, insn->src_reg)) { |
2067 | verbose("R%d partial copy of pointer\n", | |
2068 | insn->src_reg); | |
2069 | return -EACCES; | |
2070 | } | |
57a09bf0 | 2071 | mark_reg_unknown_value(regs, insn->dst_reg); |
17a52670 AS |
2072 | } |
2073 | } else { | |
2074 | /* case: R = imm | |
2075 | * remember the value we stored into this reg | |
2076 | */ | |
868c8812 JH |
2077 | u64 imm; |
2078 | ||
2079 | if (BPF_CLASS(insn->code) == BPF_ALU64) | |
2080 | imm = insn->imm; | |
2081 | else | |
2082 | imm = (u32)insn->imm; | |
2083 | ||
17a52670 | 2084 | regs[insn->dst_reg].type = CONST_IMM; |
868c8812 | 2085 | regs[insn->dst_reg].imm = imm; |
4a2ff55a | 2086 | regs[insn->dst_reg].id = 0; |
868c8812 JH |
2087 | regs[insn->dst_reg].max_value = imm; |
2088 | regs[insn->dst_reg].min_value = imm; | |
2089 | regs[insn->dst_reg].min_align = calc_align(imm); | |
4cabc5b1 | 2090 | regs[insn->dst_reg].value_from_signed = false; |
17a52670 AS |
2091 | } |
2092 | ||
2093 | } else if (opcode > BPF_END) { | |
2094 | verbose("invalid BPF_ALU opcode %x\n", opcode); | |
2095 | return -EINVAL; | |
2096 | ||
2097 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
2098 | ||
17a52670 AS |
2099 | if (BPF_SRC(insn->code) == BPF_X) { |
2100 | if (insn->imm != 0 || insn->off != 0) { | |
2101 | verbose("BPF_ALU uses reserved fields\n"); | |
2102 | return -EINVAL; | |
2103 | } | |
2104 | /* check src1 operand */ | |
2105 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2106 | if (err) | |
2107 | return err; | |
2108 | } else { | |
2109 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
2110 | verbose("BPF_ALU uses reserved fields\n"); | |
2111 | return -EINVAL; | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | /* check src2 operand */ | |
2116 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
2117 | if (err) | |
2118 | return err; | |
2119 | ||
2120 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
2121 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
2122 | verbose("div by zero\n"); | |
2123 | return -EINVAL; | |
2124 | } | |
2125 | ||
229394e8 RV |
2126 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
2127 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
2128 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
2129 | ||
2130 | if (insn->imm < 0 || insn->imm >= size) { | |
2131 | verbose("invalid shift %d\n", insn->imm); | |
2132 | return -EINVAL; | |
2133 | } | |
2134 | } | |
2135 | ||
1a0dc1ac AS |
2136 | /* check dest operand */ |
2137 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); | |
2138 | if (err) | |
2139 | return err; | |
2140 | ||
2141 | dst_reg = ®s[insn->dst_reg]; | |
2142 | ||
48461135 JB |
2143 | /* first we want to adjust our ranges. */ |
2144 | adjust_reg_min_max_vals(env, insn); | |
2145 | ||
17a52670 AS |
2146 | /* pattern match 'bpf_add Rx, imm' instruction */ |
2147 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && | |
1a0dc1ac AS |
2148 | dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) { |
2149 | dst_reg->type = PTR_TO_STACK; | |
2150 | dst_reg->imm = insn->imm; | |
2151 | return 0; | |
332270fd YS |
2152 | } else if (opcode == BPF_ADD && |
2153 | BPF_CLASS(insn->code) == BPF_ALU64 && | |
2154 | dst_reg->type == PTR_TO_STACK && | |
2155 | ((BPF_SRC(insn->code) == BPF_X && | |
2156 | regs[insn->src_reg].type == CONST_IMM) || | |
2157 | BPF_SRC(insn->code) == BPF_K)) { | |
1c26ffd0 JH |
2158 | if (BPF_SRC(insn->code) == BPF_X) { |
2159 | /* check in case the register contains a big | |
2160 | * 64-bit value | |
2161 | */ | |
2162 | if (regs[insn->src_reg].imm < -MAX_BPF_STACK || | |
2163 | regs[insn->src_reg].imm > MAX_BPF_STACK) { | |
2164 | verbose("R%d value too big in R%d pointer arithmetic\n", | |
2165 | insn->src_reg, insn->dst_reg); | |
2166 | return -EACCES; | |
2167 | } | |
332270fd | 2168 | dst_reg->imm += regs[insn->src_reg].imm; |
1c26ffd0 JH |
2169 | } else { |
2170 | /* safe against overflow: addition of 32-bit | |
2171 | * numbers in 64-bit representation | |
2172 | */ | |
332270fd | 2173 | dst_reg->imm += insn->imm; |
1c26ffd0 JH |
2174 | } |
2175 | if (dst_reg->imm > 0 || dst_reg->imm < -MAX_BPF_STACK) { | |
2176 | verbose("R%d out-of-bounds pointer arithmetic\n", | |
2177 | insn->dst_reg); | |
2178 | return -EACCES; | |
2179 | } | |
332270fd | 2180 | return 0; |
969bf05e AS |
2181 | } else if (opcode == BPF_ADD && |
2182 | BPF_CLASS(insn->code) == BPF_ALU64 && | |
1b9b69ec AS |
2183 | (dst_reg->type == PTR_TO_PACKET || |
2184 | (BPF_SRC(insn->code) == BPF_X && | |
2185 | regs[insn->src_reg].type == PTR_TO_PACKET))) { | |
969bf05e AS |
2186 | /* ptr_to_packet += K|X */ |
2187 | return check_packet_ptr_add(env, insn); | |
2188 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && | |
2189 | dst_reg->type == UNKNOWN_VALUE && | |
2190 | env->allow_ptr_leaks) { | |
2191 | /* unknown += K|X */ | |
2192 | return evaluate_reg_alu(env, insn); | |
2193 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && | |
2194 | dst_reg->type == CONST_IMM && | |
2195 | env->allow_ptr_leaks) { | |
2196 | /* reg_imm += K|X */ | |
2197 | return evaluate_reg_imm_alu(env, insn); | |
1be7f75d AS |
2198 | } else if (is_pointer_value(env, insn->dst_reg)) { |
2199 | verbose("R%d pointer arithmetic prohibited\n", | |
2200 | insn->dst_reg); | |
2201 | return -EACCES; | |
2202 | } else if (BPF_SRC(insn->code) == BPF_X && | |
2203 | is_pointer_value(env, insn->src_reg)) { | |
2204 | verbose("R%d pointer arithmetic prohibited\n", | |
2205 | insn->src_reg); | |
2206 | return -EACCES; | |
2207 | } | |
17a52670 | 2208 | |
48461135 JB |
2209 | /* If we did pointer math on a map value then just set it to our |
2210 | * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or | |
2211 | * loads to this register appropriately, otherwise just mark the | |
2212 | * register as unknown. | |
2213 | */ | |
2214 | if (env->allow_ptr_leaks && | |
fce366a9 | 2215 | BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD && |
48461135 JB |
2216 | (dst_reg->type == PTR_TO_MAP_VALUE || |
2217 | dst_reg->type == PTR_TO_MAP_VALUE_ADJ)) | |
2218 | dst_reg->type = PTR_TO_MAP_VALUE_ADJ; | |
2219 | else | |
2220 | mark_reg_unknown_value(regs, insn->dst_reg); | |
17a52670 AS |
2221 | } |
2222 | ||
2223 | return 0; | |
2224 | } | |
2225 | ||
58e2af8b JK |
2226 | static void find_good_pkt_pointers(struct bpf_verifier_state *state, |
2227 | struct bpf_reg_state *dst_reg) | |
969bf05e | 2228 | { |
58e2af8b | 2229 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e | 2230 | int i; |
2d2be8ca DB |
2231 | |
2232 | /* LLVM can generate two kind of checks: | |
2233 | * | |
2234 | * Type 1: | |
2235 | * | |
2236 | * r2 = r3; | |
2237 | * r2 += 8; | |
2238 | * if (r2 > pkt_end) goto <handle exception> | |
2239 | * <access okay> | |
2240 | * | |
2241 | * Where: | |
2242 | * r2 == dst_reg, pkt_end == src_reg | |
2243 | * r2=pkt(id=n,off=8,r=0) | |
2244 | * r3=pkt(id=n,off=0,r=0) | |
2245 | * | |
2246 | * Type 2: | |
2247 | * | |
2248 | * r2 = r3; | |
2249 | * r2 += 8; | |
2250 | * if (pkt_end >= r2) goto <access okay> | |
2251 | * <handle exception> | |
2252 | * | |
2253 | * Where: | |
2254 | * pkt_end == dst_reg, r2 == src_reg | |
2255 | * r2=pkt(id=n,off=8,r=0) | |
2256 | * r3=pkt(id=n,off=0,r=0) | |
2257 | * | |
2258 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
2259 | * so that range of bytes [r3, r3 + 8) is safe to access. | |
969bf05e | 2260 | */ |
2d2be8ca | 2261 | |
969bf05e AS |
2262 | for (i = 0; i < MAX_BPF_REG; i++) |
2263 | if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id) | |
b1977682 AS |
2264 | /* keep the maximum range already checked */ |
2265 | regs[i].range = max(regs[i].range, dst_reg->off); | |
969bf05e AS |
2266 | |
2267 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { | |
2268 | if (state->stack_slot_type[i] != STACK_SPILL) | |
2269 | continue; | |
2270 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; | |
2271 | if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id) | |
b1977682 | 2272 | reg->range = max(reg->range, dst_reg->off); |
969bf05e AS |
2273 | } |
2274 | } | |
2275 | ||
48461135 JB |
2276 | /* Adjusts the register min/max values in the case that the dst_reg is the |
2277 | * variable register that we are working on, and src_reg is a constant or we're | |
2278 | * simply doing a BPF_K check. | |
2279 | */ | |
2280 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
2281 | struct bpf_reg_state *false_reg, u64 val, | |
2282 | u8 opcode) | |
2283 | { | |
4cabc5b1 DB |
2284 | bool value_from_signed = true; |
2285 | bool is_range = true; | |
2286 | ||
48461135 JB |
2287 | switch (opcode) { |
2288 | case BPF_JEQ: | |
2289 | /* If this is false then we know nothing Jon Snow, but if it is | |
2290 | * true then we know for sure. | |
2291 | */ | |
2292 | true_reg->max_value = true_reg->min_value = val; | |
4cabc5b1 | 2293 | is_range = false; |
48461135 JB |
2294 | break; |
2295 | case BPF_JNE: | |
2296 | /* If this is true we know nothing Jon Snow, but if it is false | |
2297 | * we know the value for sure; | |
2298 | */ | |
2299 | false_reg->max_value = false_reg->min_value = val; | |
4cabc5b1 | 2300 | is_range = false; |
48461135 JB |
2301 | break; |
2302 | case BPF_JGT: | |
4cabc5b1 | 2303 | value_from_signed = false; |
7e57fbb2 | 2304 | /* fallthrough */ |
48461135 | 2305 | case BPF_JSGT: |
4cabc5b1 DB |
2306 | if (true_reg->value_from_signed != value_from_signed) |
2307 | reset_reg_range_values(true_reg, 0); | |
2308 | if (false_reg->value_from_signed != value_from_signed) | |
2309 | reset_reg_range_values(false_reg, 0); | |
2310 | if (opcode == BPF_JGT) { | |
2311 | /* Unsigned comparison, the minimum value is 0. */ | |
2312 | false_reg->min_value = 0; | |
2313 | } | |
48461135 JB |
2314 | /* If this is false then we know the maximum val is val, |
2315 | * otherwise we know the min val is val+1. | |
2316 | */ | |
2317 | false_reg->max_value = val; | |
4cabc5b1 | 2318 | false_reg->value_from_signed = value_from_signed; |
48461135 | 2319 | true_reg->min_value = val + 1; |
4cabc5b1 | 2320 | true_reg->value_from_signed = value_from_signed; |
48461135 JB |
2321 | break; |
2322 | case BPF_JGE: | |
4cabc5b1 | 2323 | value_from_signed = false; |
7e57fbb2 | 2324 | /* fallthrough */ |
48461135 | 2325 | case BPF_JSGE: |
4cabc5b1 DB |
2326 | if (true_reg->value_from_signed != value_from_signed) |
2327 | reset_reg_range_values(true_reg, 0); | |
2328 | if (false_reg->value_from_signed != value_from_signed) | |
2329 | reset_reg_range_values(false_reg, 0); | |
2330 | if (opcode == BPF_JGE) { | |
2331 | /* Unsigned comparison, the minimum value is 0. */ | |
2332 | false_reg->min_value = 0; | |
2333 | } | |
48461135 JB |
2334 | /* If this is false then we know the maximum value is val - 1, |
2335 | * otherwise we know the mimimum value is val. | |
2336 | */ | |
2337 | false_reg->max_value = val - 1; | |
4cabc5b1 | 2338 | false_reg->value_from_signed = value_from_signed; |
48461135 | 2339 | true_reg->min_value = val; |
4cabc5b1 | 2340 | true_reg->value_from_signed = value_from_signed; |
48461135 JB |
2341 | break; |
2342 | default: | |
2343 | break; | |
2344 | } | |
2345 | ||
2346 | check_reg_overflow(false_reg); | |
2347 | check_reg_overflow(true_reg); | |
4cabc5b1 DB |
2348 | if (is_range) { |
2349 | if (__is_pointer_value(false, false_reg)) | |
2350 | reset_reg_range_values(false_reg, 0); | |
2351 | if (__is_pointer_value(false, true_reg)) | |
2352 | reset_reg_range_values(true_reg, 0); | |
2353 | } | |
48461135 JB |
2354 | } |
2355 | ||
2356 | /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg | |
2357 | * is the variable reg. | |
2358 | */ | |
2359 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
2360 | struct bpf_reg_state *false_reg, u64 val, | |
2361 | u8 opcode) | |
2362 | { | |
4cabc5b1 DB |
2363 | bool value_from_signed = true; |
2364 | bool is_range = true; | |
2365 | ||
48461135 JB |
2366 | switch (opcode) { |
2367 | case BPF_JEQ: | |
2368 | /* If this is false then we know nothing Jon Snow, but if it is | |
2369 | * true then we know for sure. | |
2370 | */ | |
2371 | true_reg->max_value = true_reg->min_value = val; | |
4cabc5b1 | 2372 | is_range = false; |
48461135 JB |
2373 | break; |
2374 | case BPF_JNE: | |
2375 | /* If this is true we know nothing Jon Snow, but if it is false | |
2376 | * we know the value for sure; | |
2377 | */ | |
2378 | false_reg->max_value = false_reg->min_value = val; | |
4cabc5b1 | 2379 | is_range = false; |
48461135 JB |
2380 | break; |
2381 | case BPF_JGT: | |
4cabc5b1 | 2382 | value_from_signed = false; |
7e57fbb2 | 2383 | /* fallthrough */ |
48461135 | 2384 | case BPF_JSGT: |
4cabc5b1 DB |
2385 | if (true_reg->value_from_signed != value_from_signed) |
2386 | reset_reg_range_values(true_reg, 0); | |
2387 | if (false_reg->value_from_signed != value_from_signed) | |
2388 | reset_reg_range_values(false_reg, 0); | |
2389 | if (opcode == BPF_JGT) { | |
2390 | /* Unsigned comparison, the minimum value is 0. */ | |
2391 | true_reg->min_value = 0; | |
2392 | } | |
48461135 JB |
2393 | /* |
2394 | * If this is false, then the val is <= the register, if it is | |
2395 | * true the register <= to the val. | |
2396 | */ | |
2397 | false_reg->min_value = val; | |
4cabc5b1 | 2398 | false_reg->value_from_signed = value_from_signed; |
48461135 | 2399 | true_reg->max_value = val - 1; |
4cabc5b1 | 2400 | true_reg->value_from_signed = value_from_signed; |
48461135 JB |
2401 | break; |
2402 | case BPF_JGE: | |
4cabc5b1 | 2403 | value_from_signed = false; |
7e57fbb2 | 2404 | /* fallthrough */ |
48461135 | 2405 | case BPF_JSGE: |
4cabc5b1 DB |
2406 | if (true_reg->value_from_signed != value_from_signed) |
2407 | reset_reg_range_values(true_reg, 0); | |
2408 | if (false_reg->value_from_signed != value_from_signed) | |
2409 | reset_reg_range_values(false_reg, 0); | |
2410 | if (opcode == BPF_JGE) { | |
2411 | /* Unsigned comparison, the minimum value is 0. */ | |
2412 | true_reg->min_value = 0; | |
2413 | } | |
48461135 JB |
2414 | /* If this is false then constant < register, if it is true then |
2415 | * the register < constant. | |
2416 | */ | |
2417 | false_reg->min_value = val + 1; | |
4cabc5b1 | 2418 | false_reg->value_from_signed = value_from_signed; |
48461135 | 2419 | true_reg->max_value = val; |
4cabc5b1 | 2420 | true_reg->value_from_signed = value_from_signed; |
48461135 JB |
2421 | break; |
2422 | default: | |
2423 | break; | |
2424 | } | |
2425 | ||
2426 | check_reg_overflow(false_reg); | |
2427 | check_reg_overflow(true_reg); | |
4cabc5b1 DB |
2428 | if (is_range) { |
2429 | if (__is_pointer_value(false, false_reg)) | |
2430 | reset_reg_range_values(false_reg, 0); | |
2431 | if (__is_pointer_value(false, true_reg)) | |
2432 | reset_reg_range_values(true_reg, 0); | |
2433 | } | |
48461135 JB |
2434 | } |
2435 | ||
57a09bf0 TG |
2436 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
2437 | enum bpf_reg_type type) | |
2438 | { | |
2439 | struct bpf_reg_state *reg = ®s[regno]; | |
2440 | ||
2441 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { | |
56f668df MKL |
2442 | if (type == UNKNOWN_VALUE) { |
2443 | __mark_reg_unknown_value(regs, regno); | |
2444 | } else if (reg->map_ptr->inner_map_meta) { | |
2445 | reg->type = CONST_PTR_TO_MAP; | |
2446 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
2447 | } else { | |
2448 | reg->type = type; | |
2449 | } | |
a08dd0da DB |
2450 | /* We don't need id from this point onwards anymore, thus we |
2451 | * should better reset it, so that state pruning has chances | |
2452 | * to take effect. | |
2453 | */ | |
2454 | reg->id = 0; | |
57a09bf0 TG |
2455 | } |
2456 | } | |
2457 | ||
2458 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
2459 | * be folded together at some point. | |
2460 | */ | |
2461 | static void mark_map_regs(struct bpf_verifier_state *state, u32 regno, | |
2462 | enum bpf_reg_type type) | |
2463 | { | |
2464 | struct bpf_reg_state *regs = state->regs; | |
a08dd0da | 2465 | u32 id = regs[regno].id; |
57a09bf0 TG |
2466 | int i; |
2467 | ||
2468 | for (i = 0; i < MAX_BPF_REG; i++) | |
a08dd0da | 2469 | mark_map_reg(regs, i, id, type); |
57a09bf0 TG |
2470 | |
2471 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { | |
2472 | if (state->stack_slot_type[i] != STACK_SPILL) | |
2473 | continue; | |
a08dd0da | 2474 | mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type); |
57a09bf0 TG |
2475 | } |
2476 | } | |
2477 | ||
58e2af8b | 2478 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
2479 | struct bpf_insn *insn, int *insn_idx) |
2480 | { | |
58e2af8b JK |
2481 | struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state; |
2482 | struct bpf_reg_state *regs = this_branch->regs, *dst_reg; | |
17a52670 AS |
2483 | u8 opcode = BPF_OP(insn->code); |
2484 | int err; | |
2485 | ||
2486 | if (opcode > BPF_EXIT) { | |
2487 | verbose("invalid BPF_JMP opcode %x\n", opcode); | |
2488 | return -EINVAL; | |
2489 | } | |
2490 | ||
2491 | if (BPF_SRC(insn->code) == BPF_X) { | |
2492 | if (insn->imm != 0) { | |
2493 | verbose("BPF_JMP uses reserved fields\n"); | |
2494 | return -EINVAL; | |
2495 | } | |
2496 | ||
2497 | /* check src1 operand */ | |
2498 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2499 | if (err) | |
2500 | return err; | |
1be7f75d AS |
2501 | |
2502 | if (is_pointer_value(env, insn->src_reg)) { | |
2503 | verbose("R%d pointer comparison prohibited\n", | |
2504 | insn->src_reg); | |
2505 | return -EACCES; | |
2506 | } | |
17a52670 AS |
2507 | } else { |
2508 | if (insn->src_reg != BPF_REG_0) { | |
2509 | verbose("BPF_JMP uses reserved fields\n"); | |
2510 | return -EINVAL; | |
2511 | } | |
2512 | } | |
2513 | ||
2514 | /* check src2 operand */ | |
2515 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
2516 | if (err) | |
2517 | return err; | |
2518 | ||
1a0dc1ac AS |
2519 | dst_reg = ®s[insn->dst_reg]; |
2520 | ||
17a52670 AS |
2521 | /* detect if R == 0 where R was initialized to zero earlier */ |
2522 | if (BPF_SRC(insn->code) == BPF_K && | |
2523 | (opcode == BPF_JEQ || opcode == BPF_JNE) && | |
1a0dc1ac | 2524 | dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) { |
17a52670 AS |
2525 | if (opcode == BPF_JEQ) { |
2526 | /* if (imm == imm) goto pc+off; | |
2527 | * only follow the goto, ignore fall-through | |
2528 | */ | |
2529 | *insn_idx += insn->off; | |
2530 | return 0; | |
2531 | } else { | |
2532 | /* if (imm != imm) goto pc+off; | |
2533 | * only follow fall-through branch, since | |
2534 | * that's where the program will go | |
2535 | */ | |
2536 | return 0; | |
2537 | } | |
2538 | } | |
2539 | ||
2540 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); | |
2541 | if (!other_branch) | |
2542 | return -EFAULT; | |
2543 | ||
48461135 JB |
2544 | /* detect if we are comparing against a constant value so we can adjust |
2545 | * our min/max values for our dst register. | |
2546 | */ | |
2547 | if (BPF_SRC(insn->code) == BPF_X) { | |
2548 | if (regs[insn->src_reg].type == CONST_IMM) | |
2549 | reg_set_min_max(&other_branch->regs[insn->dst_reg], | |
2550 | dst_reg, regs[insn->src_reg].imm, | |
2551 | opcode); | |
2552 | else if (dst_reg->type == CONST_IMM) | |
2553 | reg_set_min_max_inv(&other_branch->regs[insn->src_reg], | |
2554 | ®s[insn->src_reg], dst_reg->imm, | |
2555 | opcode); | |
2556 | } else { | |
2557 | reg_set_min_max(&other_branch->regs[insn->dst_reg], | |
2558 | dst_reg, insn->imm, opcode); | |
2559 | } | |
2560 | ||
58e2af8b | 2561 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */ |
17a52670 | 2562 | if (BPF_SRC(insn->code) == BPF_K && |
1a0dc1ac AS |
2563 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
2564 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { | |
57a09bf0 TG |
2565 | /* Mark all identical map registers in each branch as either |
2566 | * safe or unknown depending R == 0 or R != 0 conditional. | |
2567 | */ | |
2568 | mark_map_regs(this_branch, insn->dst_reg, | |
2569 | opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE); | |
2570 | mark_map_regs(other_branch, insn->dst_reg, | |
2571 | opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE); | |
969bf05e AS |
2572 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && |
2573 | dst_reg->type == PTR_TO_PACKET && | |
2574 | regs[insn->src_reg].type == PTR_TO_PACKET_END) { | |
2d2be8ca DB |
2575 | find_good_pkt_pointers(this_branch, dst_reg); |
2576 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE && | |
2577 | dst_reg->type == PTR_TO_PACKET_END && | |
2578 | regs[insn->src_reg].type == PTR_TO_PACKET) { | |
2579 | find_good_pkt_pointers(other_branch, ®s[insn->src_reg]); | |
1be7f75d AS |
2580 | } else if (is_pointer_value(env, insn->dst_reg)) { |
2581 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); | |
2582 | return -EACCES; | |
17a52670 AS |
2583 | } |
2584 | if (log_level) | |
2d2be8ca | 2585 | print_verifier_state(this_branch); |
17a52670 AS |
2586 | return 0; |
2587 | } | |
2588 | ||
0246e64d AS |
2589 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
2590 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) | |
2591 | { | |
2592 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; | |
2593 | ||
2594 | return (struct bpf_map *) (unsigned long) imm64; | |
2595 | } | |
2596 | ||
17a52670 | 2597 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 2598 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 2599 | { |
58e2af8b | 2600 | struct bpf_reg_state *regs = env->cur_state.regs; |
17a52670 AS |
2601 | int err; |
2602 | ||
2603 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
2604 | verbose("invalid BPF_LD_IMM insn\n"); | |
2605 | return -EINVAL; | |
2606 | } | |
2607 | if (insn->off != 0) { | |
2608 | verbose("BPF_LD_IMM64 uses reserved fields\n"); | |
2609 | return -EINVAL; | |
2610 | } | |
2611 | ||
2612 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
2613 | if (err) | |
2614 | return err; | |
2615 | ||
6b173873 | 2616 | if (insn->src_reg == 0) { |
6b173873 JK |
2617 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
2618 | ||
6b173873 JK |
2619 | regs[insn->dst_reg].type = CONST_IMM; |
2620 | regs[insn->dst_reg].imm = imm; | |
4a2ff55a | 2621 | regs[insn->dst_reg].id = 0; |
17a52670 | 2622 | return 0; |
6b173873 | 2623 | } |
17a52670 AS |
2624 | |
2625 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ | |
2626 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); | |
2627 | ||
2628 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
2629 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); | |
2630 | return 0; | |
2631 | } | |
2632 | ||
96be4325 DB |
2633 | static bool may_access_skb(enum bpf_prog_type type) |
2634 | { | |
2635 | switch (type) { | |
2636 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
2637 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 2638 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
2639 | return true; |
2640 | default: | |
2641 | return false; | |
2642 | } | |
2643 | } | |
2644 | ||
ddd872bc AS |
2645 | /* verify safety of LD_ABS|LD_IND instructions: |
2646 | * - they can only appear in the programs where ctx == skb | |
2647 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
2648 | * preserve R6-R9, and store return value into R0 | |
2649 | * | |
2650 | * Implicit input: | |
2651 | * ctx == skb == R6 == CTX | |
2652 | * | |
2653 | * Explicit input: | |
2654 | * SRC == any register | |
2655 | * IMM == 32-bit immediate | |
2656 | * | |
2657 | * Output: | |
2658 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
2659 | */ | |
58e2af8b | 2660 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 2661 | { |
58e2af8b | 2662 | struct bpf_reg_state *regs = env->cur_state.regs; |
ddd872bc | 2663 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
2664 | int i, err; |
2665 | ||
24701ece | 2666 | if (!may_access_skb(env->prog->type)) { |
1a0dc1ac | 2667 | verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
2668 | return -EINVAL; |
2669 | } | |
2670 | ||
2671 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || | |
d82bccc6 | 2672 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 2673 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
1a0dc1ac | 2674 | verbose("BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
2675 | return -EINVAL; |
2676 | } | |
2677 | ||
2678 | /* check whether implicit source operand (register R6) is readable */ | |
2679 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); | |
2680 | if (err) | |
2681 | return err; | |
2682 | ||
2683 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { | |
2684 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
2685 | return -EINVAL; | |
2686 | } | |
2687 | ||
2688 | if (mode == BPF_IND) { | |
2689 | /* check explicit source operand */ | |
2690 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2691 | if (err) | |
2692 | return err; | |
2693 | } | |
2694 | ||
2695 | /* reset caller saved regs to unreadable */ | |
a9789ef9 DB |
2696 | for (i = 0; i < CALLER_SAVED_REGS; i++) |
2697 | mark_reg_not_init(regs, caller_saved[i]); | |
ddd872bc AS |
2698 | |
2699 | /* mark destination R0 register as readable, since it contains | |
2700 | * the value fetched from the packet | |
2701 | */ | |
2702 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
2703 | return 0; | |
2704 | } | |
2705 | ||
475fb78f AS |
2706 | /* non-recursive DFS pseudo code |
2707 | * 1 procedure DFS-iterative(G,v): | |
2708 | * 2 label v as discovered | |
2709 | * 3 let S be a stack | |
2710 | * 4 S.push(v) | |
2711 | * 5 while S is not empty | |
2712 | * 6 t <- S.pop() | |
2713 | * 7 if t is what we're looking for: | |
2714 | * 8 return t | |
2715 | * 9 for all edges e in G.adjacentEdges(t) do | |
2716 | * 10 if edge e is already labelled | |
2717 | * 11 continue with the next edge | |
2718 | * 12 w <- G.adjacentVertex(t,e) | |
2719 | * 13 if vertex w is not discovered and not explored | |
2720 | * 14 label e as tree-edge | |
2721 | * 15 label w as discovered | |
2722 | * 16 S.push(w) | |
2723 | * 17 continue at 5 | |
2724 | * 18 else if vertex w is discovered | |
2725 | * 19 label e as back-edge | |
2726 | * 20 else | |
2727 | * 21 // vertex w is explored | |
2728 | * 22 label e as forward- or cross-edge | |
2729 | * 23 label t as explored | |
2730 | * 24 S.pop() | |
2731 | * | |
2732 | * convention: | |
2733 | * 0x10 - discovered | |
2734 | * 0x11 - discovered and fall-through edge labelled | |
2735 | * 0x12 - discovered and fall-through and branch edges labelled | |
2736 | * 0x20 - explored | |
2737 | */ | |
2738 | ||
2739 | enum { | |
2740 | DISCOVERED = 0x10, | |
2741 | EXPLORED = 0x20, | |
2742 | FALLTHROUGH = 1, | |
2743 | BRANCH = 2, | |
2744 | }; | |
2745 | ||
58e2af8b | 2746 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
f1bca824 | 2747 | |
475fb78f AS |
2748 | static int *insn_stack; /* stack of insns to process */ |
2749 | static int cur_stack; /* current stack index */ | |
2750 | static int *insn_state; | |
2751 | ||
2752 | /* t, w, e - match pseudo-code above: | |
2753 | * t - index of current instruction | |
2754 | * w - next instruction | |
2755 | * e - edge | |
2756 | */ | |
58e2af8b | 2757 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
475fb78f AS |
2758 | { |
2759 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) | |
2760 | return 0; | |
2761 | ||
2762 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
2763 | return 0; | |
2764 | ||
2765 | if (w < 0 || w >= env->prog->len) { | |
2766 | verbose("jump out of range from insn %d to %d\n", t, w); | |
2767 | return -EINVAL; | |
2768 | } | |
2769 | ||
f1bca824 AS |
2770 | if (e == BRANCH) |
2771 | /* mark branch target for state pruning */ | |
2772 | env->explored_states[w] = STATE_LIST_MARK; | |
2773 | ||
475fb78f AS |
2774 | if (insn_state[w] == 0) { |
2775 | /* tree-edge */ | |
2776 | insn_state[t] = DISCOVERED | e; | |
2777 | insn_state[w] = DISCOVERED; | |
2778 | if (cur_stack >= env->prog->len) | |
2779 | return -E2BIG; | |
2780 | insn_stack[cur_stack++] = w; | |
2781 | return 1; | |
2782 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2783 | verbose("back-edge from insn %d to %d\n", t, w); | |
2784 | return -EINVAL; | |
2785 | } else if (insn_state[w] == EXPLORED) { | |
2786 | /* forward- or cross-edge */ | |
2787 | insn_state[t] = DISCOVERED | e; | |
2788 | } else { | |
2789 | verbose("insn state internal bug\n"); | |
2790 | return -EFAULT; | |
2791 | } | |
2792 | return 0; | |
2793 | } | |
2794 | ||
2795 | /* non-recursive depth-first-search to detect loops in BPF program | |
2796 | * loop == back-edge in directed graph | |
2797 | */ | |
58e2af8b | 2798 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
2799 | { |
2800 | struct bpf_insn *insns = env->prog->insnsi; | |
2801 | int insn_cnt = env->prog->len; | |
2802 | int ret = 0; | |
2803 | int i, t; | |
2804 | ||
2805 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
2806 | if (!insn_state) | |
2807 | return -ENOMEM; | |
2808 | ||
2809 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
2810 | if (!insn_stack) { | |
2811 | kfree(insn_state); | |
2812 | return -ENOMEM; | |
2813 | } | |
2814 | ||
2815 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
2816 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
2817 | cur_stack = 1; | |
2818 | ||
2819 | peek_stack: | |
2820 | if (cur_stack == 0) | |
2821 | goto check_state; | |
2822 | t = insn_stack[cur_stack - 1]; | |
2823 | ||
2824 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { | |
2825 | u8 opcode = BPF_OP(insns[t].code); | |
2826 | ||
2827 | if (opcode == BPF_EXIT) { | |
2828 | goto mark_explored; | |
2829 | } else if (opcode == BPF_CALL) { | |
2830 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
2831 | if (ret == 1) | |
2832 | goto peek_stack; | |
2833 | else if (ret < 0) | |
2834 | goto err_free; | |
07016151 DB |
2835 | if (t + 1 < insn_cnt) |
2836 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
2837 | } else if (opcode == BPF_JA) { |
2838 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
2839 | ret = -EINVAL; | |
2840 | goto err_free; | |
2841 | } | |
2842 | /* unconditional jump with single edge */ | |
2843 | ret = push_insn(t, t + insns[t].off + 1, | |
2844 | FALLTHROUGH, env); | |
2845 | if (ret == 1) | |
2846 | goto peek_stack; | |
2847 | else if (ret < 0) | |
2848 | goto err_free; | |
f1bca824 AS |
2849 | /* tell verifier to check for equivalent states |
2850 | * after every call and jump | |
2851 | */ | |
c3de6317 AS |
2852 | if (t + 1 < insn_cnt) |
2853 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
2854 | } else { |
2855 | /* conditional jump with two edges */ | |
3c2ce60b | 2856 | env->explored_states[t] = STATE_LIST_MARK; |
475fb78f AS |
2857 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
2858 | if (ret == 1) | |
2859 | goto peek_stack; | |
2860 | else if (ret < 0) | |
2861 | goto err_free; | |
2862 | ||
2863 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
2864 | if (ret == 1) | |
2865 | goto peek_stack; | |
2866 | else if (ret < 0) | |
2867 | goto err_free; | |
2868 | } | |
2869 | } else { | |
2870 | /* all other non-branch instructions with single | |
2871 | * fall-through edge | |
2872 | */ | |
2873 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
2874 | if (ret == 1) | |
2875 | goto peek_stack; | |
2876 | else if (ret < 0) | |
2877 | goto err_free; | |
2878 | } | |
2879 | ||
2880 | mark_explored: | |
2881 | insn_state[t] = EXPLORED; | |
2882 | if (cur_stack-- <= 0) { | |
2883 | verbose("pop stack internal bug\n"); | |
2884 | ret = -EFAULT; | |
2885 | goto err_free; | |
2886 | } | |
2887 | goto peek_stack; | |
2888 | ||
2889 | check_state: | |
2890 | for (i = 0; i < insn_cnt; i++) { | |
2891 | if (insn_state[i] != EXPLORED) { | |
2892 | verbose("unreachable insn %d\n", i); | |
2893 | ret = -EINVAL; | |
2894 | goto err_free; | |
2895 | } | |
2896 | } | |
2897 | ret = 0; /* cfg looks good */ | |
2898 | ||
2899 | err_free: | |
2900 | kfree(insn_state); | |
2901 | kfree(insn_stack); | |
2902 | return ret; | |
2903 | } | |
2904 | ||
969bf05e AS |
2905 | /* the following conditions reduce the number of explored insns |
2906 | * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet | |
2907 | */ | |
1ad2f583 DB |
2908 | static bool compare_ptrs_to_packet(struct bpf_verifier_env *env, |
2909 | struct bpf_reg_state *old, | |
58e2af8b | 2910 | struct bpf_reg_state *cur) |
969bf05e AS |
2911 | { |
2912 | if (old->id != cur->id) | |
2913 | return false; | |
2914 | ||
2915 | /* old ptr_to_packet is more conservative, since it allows smaller | |
2916 | * range. Ex: | |
2917 | * old(off=0,r=10) is equal to cur(off=0,r=20), because | |
2918 | * old(off=0,r=10) means that with range=10 the verifier proceeded | |
2919 | * further and found no issues with the program. Now we're in the same | |
2920 | * spot with cur(off=0,r=20), so we're safe too, since anything further | |
2921 | * will only be looking at most 10 bytes after this pointer. | |
2922 | */ | |
2923 | if (old->off == cur->off && old->range < cur->range) | |
2924 | return true; | |
2925 | ||
2926 | /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0) | |
2927 | * since both cannot be used for packet access and safe(old) | |
2928 | * pointer has smaller off that could be used for further | |
2929 | * 'if (ptr > data_end)' check | |
2930 | * Ex: | |
2931 | * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean | |
2932 | * that we cannot access the packet. | |
2933 | * The safe range is: | |
2934 | * [ptr, ptr + range - off) | |
2935 | * so whenever off >=range, it means no safe bytes from this pointer. | |
2936 | * When comparing old->off <= cur->off, it means that older code | |
2937 | * went with smaller offset and that offset was later | |
2938 | * used to figure out the safe range after 'if (ptr > data_end)' check | |
2939 | * Say, 'old' state was explored like: | |
2940 | * ... R3(off=0, r=0) | |
2941 | * R4 = R3 + 20 | |
2942 | * ... now R4(off=20,r=0) <-- here | |
2943 | * if (R4 > data_end) | |
2944 | * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access. | |
2945 | * ... the code further went all the way to bpf_exit. | |
2946 | * Now the 'cur' state at the mark 'here' has R4(off=30,r=0). | |
2947 | * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier | |
2948 | * goes further, such cur_R4 will give larger safe packet range after | |
2949 | * 'if (R4 > data_end)' and all further insn were already good with r=20, | |
2950 | * so they will be good with r=30 and we can prune the search. | |
2951 | */ | |
1ad2f583 | 2952 | if (!env->strict_alignment && old->off <= cur->off && |
969bf05e AS |
2953 | old->off >= old->range && cur->off >= cur->range) |
2954 | return true; | |
2955 | ||
2956 | return false; | |
2957 | } | |
2958 | ||
f1bca824 AS |
2959 | /* compare two verifier states |
2960 | * | |
2961 | * all states stored in state_list are known to be valid, since | |
2962 | * verifier reached 'bpf_exit' instruction through them | |
2963 | * | |
2964 | * this function is called when verifier exploring different branches of | |
2965 | * execution popped from the state stack. If it sees an old state that has | |
2966 | * more strict register state and more strict stack state then this execution | |
2967 | * branch doesn't need to be explored further, since verifier already | |
2968 | * concluded that more strict state leads to valid finish. | |
2969 | * | |
2970 | * Therefore two states are equivalent if register state is more conservative | |
2971 | * and explored stack state is more conservative than the current one. | |
2972 | * Example: | |
2973 | * explored current | |
2974 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
2975 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
2976 | * | |
2977 | * In other words if current stack state (one being explored) has more | |
2978 | * valid slots than old one that already passed validation, it means | |
2979 | * the verifier can stop exploring and conclude that current state is valid too | |
2980 | * | |
2981 | * Similarly with registers. If explored state has register type as invalid | |
2982 | * whereas register type in current state is meaningful, it means that | |
2983 | * the current state will reach 'bpf_exit' instruction safely | |
2984 | */ | |
48461135 JB |
2985 | static bool states_equal(struct bpf_verifier_env *env, |
2986 | struct bpf_verifier_state *old, | |
58e2af8b | 2987 | struct bpf_verifier_state *cur) |
f1bca824 | 2988 | { |
e2d2afe1 | 2989 | bool varlen_map_access = env->varlen_map_value_access; |
58e2af8b | 2990 | struct bpf_reg_state *rold, *rcur; |
f1bca824 AS |
2991 | int i; |
2992 | ||
2993 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1a0dc1ac AS |
2994 | rold = &old->regs[i]; |
2995 | rcur = &cur->regs[i]; | |
2996 | ||
2997 | if (memcmp(rold, rcur, sizeof(*rold)) == 0) | |
2998 | continue; | |
2999 | ||
48461135 JB |
3000 | /* If the ranges were not the same, but everything else was and |
3001 | * we didn't do a variable access into a map then we are a-ok. | |
3002 | */ | |
e2d2afe1 | 3003 | if (!varlen_map_access && |
d2a4dd37 | 3004 | memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0) |
48461135 JB |
3005 | continue; |
3006 | ||
e2d2afe1 JB |
3007 | /* If we didn't map access then again we don't care about the |
3008 | * mismatched range values and it's ok if our old type was | |
3fb43780 | 3009 | * UNKNOWN and we didn't go to a NOT_INIT'ed or pointer reg. |
e2d2afe1 | 3010 | */ |
1a0dc1ac | 3011 | if (rold->type == NOT_INIT || |
e2d2afe1 | 3012 | (!varlen_map_access && rold->type == UNKNOWN_VALUE && |
3fb43780 BH |
3013 | rcur->type != NOT_INIT && |
3014 | !__is_pointer_value(env->allow_ptr_leaks, rcur))) | |
1a0dc1ac AS |
3015 | continue; |
3016 | ||
3c2ce60b DB |
3017 | /* Don't care about the reg->id in this case. */ |
3018 | if (rold->type == PTR_TO_MAP_VALUE_OR_NULL && | |
3019 | rcur->type == PTR_TO_MAP_VALUE_OR_NULL && | |
3020 | rold->map_ptr == rcur->map_ptr) | |
3021 | continue; | |
3022 | ||
969bf05e | 3023 | if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET && |
1ad2f583 | 3024 | compare_ptrs_to_packet(env, rold, rcur)) |
969bf05e AS |
3025 | continue; |
3026 | ||
1a0dc1ac | 3027 | return false; |
f1bca824 AS |
3028 | } |
3029 | ||
3030 | for (i = 0; i < MAX_BPF_STACK; i++) { | |
9c399760 AS |
3031 | if (old->stack_slot_type[i] == STACK_INVALID) |
3032 | continue; | |
3033 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) | |
3034 | /* Ex: old explored (safe) state has STACK_SPILL in | |
3035 | * this stack slot, but current has has STACK_MISC -> | |
3036 | * this verifier states are not equivalent, | |
3037 | * return false to continue verification of this path | |
3038 | */ | |
f1bca824 | 3039 | return false; |
9c399760 AS |
3040 | if (i % BPF_REG_SIZE) |
3041 | continue; | |
d25da6ca DB |
3042 | if (old->stack_slot_type[i] != STACK_SPILL) |
3043 | continue; | |
9c399760 AS |
3044 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], |
3045 | &cur->spilled_regs[i / BPF_REG_SIZE], | |
3046 | sizeof(old->spilled_regs[0]))) | |
3047 | /* when explored and current stack slot types are | |
3048 | * the same, check that stored pointers types | |
3049 | * are the same as well. | |
3050 | * Ex: explored safe path could have stored | |
58e2af8b | 3051 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8} |
9c399760 | 3052 | * but current path has stored: |
58e2af8b | 3053 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16} |
9c399760 AS |
3054 | * such verifier states are not equivalent. |
3055 | * return false to continue verification of this path | |
3056 | */ | |
3057 | return false; | |
3058 | else | |
3059 | continue; | |
f1bca824 AS |
3060 | } |
3061 | return true; | |
3062 | } | |
3063 | ||
58e2af8b | 3064 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 3065 | { |
58e2af8b JK |
3066 | struct bpf_verifier_state_list *new_sl; |
3067 | struct bpf_verifier_state_list *sl; | |
f1bca824 AS |
3068 | |
3069 | sl = env->explored_states[insn_idx]; | |
3070 | if (!sl) | |
3071 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
3072 | * be doing state search here | |
3073 | */ | |
3074 | return 0; | |
3075 | ||
3076 | while (sl != STATE_LIST_MARK) { | |
48461135 | 3077 | if (states_equal(env, &sl->state, &env->cur_state)) |
f1bca824 AS |
3078 | /* reached equivalent register/stack state, |
3079 | * prune the search | |
3080 | */ | |
3081 | return 1; | |
3082 | sl = sl->next; | |
3083 | } | |
3084 | ||
3085 | /* there were no equivalent states, remember current one. | |
3086 | * technically the current state is not proven to be safe yet, | |
3087 | * but it will either reach bpf_exit (which means it's safe) or | |
3088 | * it will be rejected. Since there are no loops, we won't be | |
3089 | * seeing this 'insn_idx' instruction again on the way to bpf_exit | |
3090 | */ | |
58e2af8b | 3091 | new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER); |
f1bca824 AS |
3092 | if (!new_sl) |
3093 | return -ENOMEM; | |
3094 | ||
3095 | /* add new state to the head of linked list */ | |
3096 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); | |
3097 | new_sl->next = env->explored_states[insn_idx]; | |
3098 | env->explored_states[insn_idx] = new_sl; | |
3099 | return 0; | |
3100 | } | |
3101 | ||
13a27dfc JK |
3102 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
3103 | int insn_idx, int prev_insn_idx) | |
3104 | { | |
3105 | if (!env->analyzer_ops || !env->analyzer_ops->insn_hook) | |
3106 | return 0; | |
3107 | ||
3108 | return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx); | |
3109 | } | |
3110 | ||
58e2af8b | 3111 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 3112 | { |
58e2af8b | 3113 | struct bpf_verifier_state *state = &env->cur_state; |
17a52670 | 3114 | struct bpf_insn *insns = env->prog->insnsi; |
58e2af8b | 3115 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
3116 | int insn_cnt = env->prog->len; |
3117 | int insn_idx, prev_insn_idx = 0; | |
3118 | int insn_processed = 0; | |
3119 | bool do_print_state = false; | |
3120 | ||
3121 | init_reg_state(regs); | |
3122 | insn_idx = 0; | |
48461135 | 3123 | env->varlen_map_value_access = false; |
17a52670 AS |
3124 | for (;;) { |
3125 | struct bpf_insn *insn; | |
3126 | u8 class; | |
3127 | int err; | |
3128 | ||
3129 | if (insn_idx >= insn_cnt) { | |
3130 | verbose("invalid insn idx %d insn_cnt %d\n", | |
3131 | insn_idx, insn_cnt); | |
3132 | return -EFAULT; | |
3133 | } | |
3134 | ||
3135 | insn = &insns[insn_idx]; | |
3136 | class = BPF_CLASS(insn->code); | |
3137 | ||
07016151 | 3138 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
bc1750f3 | 3139 | verbose("BPF program is too large. Processed %d insn\n", |
17a52670 AS |
3140 | insn_processed); |
3141 | return -E2BIG; | |
3142 | } | |
3143 | ||
f1bca824 AS |
3144 | err = is_state_visited(env, insn_idx); |
3145 | if (err < 0) | |
3146 | return err; | |
3147 | if (err == 1) { | |
3148 | /* found equivalent state, can prune the search */ | |
3149 | if (log_level) { | |
3150 | if (do_print_state) | |
3151 | verbose("\nfrom %d to %d: safe\n", | |
3152 | prev_insn_idx, insn_idx); | |
3153 | else | |
3154 | verbose("%d: safe\n", insn_idx); | |
3155 | } | |
3156 | goto process_bpf_exit; | |
3157 | } | |
3158 | ||
3c2ce60b DB |
3159 | if (need_resched()) |
3160 | cond_resched(); | |
3161 | ||
c5fc9692 DM |
3162 | if (log_level > 1 || (log_level && do_print_state)) { |
3163 | if (log_level > 1) | |
3164 | verbose("%d:", insn_idx); | |
3165 | else | |
3166 | verbose("\nfrom %d to %d:", | |
3167 | prev_insn_idx, insn_idx); | |
1a0dc1ac | 3168 | print_verifier_state(&env->cur_state); |
17a52670 AS |
3169 | do_print_state = false; |
3170 | } | |
3171 | ||
3172 | if (log_level) { | |
3173 | verbose("%d: ", insn_idx); | |
0d0e5769 | 3174 | print_bpf_insn(env, insn); |
17a52670 AS |
3175 | } |
3176 | ||
13a27dfc JK |
3177 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
3178 | if (err) | |
3179 | return err; | |
3180 | ||
2df70878 | 3181 | env->insn_aux_data[insn_idx].seen = true; |
17a52670 | 3182 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 3183 | err = check_alu_op(env, insn); |
17a52670 AS |
3184 | if (err) |
3185 | return err; | |
3186 | ||
3187 | } else if (class == BPF_LDX) { | |
3df126f3 | 3188 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
3189 | |
3190 | /* check for reserved fields is already done */ | |
3191 | ||
17a52670 AS |
3192 | /* check src operand */ |
3193 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
3194 | if (err) | |
3195 | return err; | |
3196 | ||
3197 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); | |
3198 | if (err) | |
3199 | return err; | |
3200 | ||
725f9dcd AS |
3201 | src_reg_type = regs[insn->src_reg].type; |
3202 | ||
17a52670 AS |
3203 | /* check that memory (src_reg + off) is readable, |
3204 | * the state of dst_reg will be updated by this func | |
3205 | */ | |
31fd8581 | 3206 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
17a52670 AS |
3207 | BPF_SIZE(insn->code), BPF_READ, |
3208 | insn->dst_reg); | |
3209 | if (err) | |
3210 | return err; | |
3211 | ||
3df126f3 JK |
3212 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
3213 | ||
3214 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
3215 | /* saw a valid insn |
3216 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 3217 | * save type to validate intersecting paths |
9bac3d6d | 3218 | */ |
3df126f3 | 3219 | *prev_src_type = src_reg_type; |
9bac3d6d | 3220 | |
3df126f3 | 3221 | } else if (src_reg_type != *prev_src_type && |
9bac3d6d | 3222 | (src_reg_type == PTR_TO_CTX || |
3df126f3 | 3223 | *prev_src_type == PTR_TO_CTX)) { |
9bac3d6d AS |
3224 | /* ABuser program is trying to use the same insn |
3225 | * dst_reg = *(u32*) (src_reg + off) | |
3226 | * with different pointer types: | |
3227 | * src_reg == ctx in one branch and | |
3228 | * src_reg == stack|map in some other branch. | |
3229 | * Reject it. | |
3230 | */ | |
3231 | verbose("same insn cannot be used with different pointers\n"); | |
3232 | return -EINVAL; | |
3233 | } | |
3234 | ||
17a52670 | 3235 | } else if (class == BPF_STX) { |
3df126f3 | 3236 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 3237 | |
17a52670 | 3238 | if (BPF_MODE(insn->code) == BPF_XADD) { |
31fd8581 | 3239 | err = check_xadd(env, insn_idx, insn); |
17a52670 AS |
3240 | if (err) |
3241 | return err; | |
3242 | insn_idx++; | |
3243 | continue; | |
3244 | } | |
3245 | ||
17a52670 AS |
3246 | /* check src1 operand */ |
3247 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
3248 | if (err) | |
3249 | return err; | |
3250 | /* check src2 operand */ | |
3251 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
3252 | if (err) | |
3253 | return err; | |
3254 | ||
d691f9e8 AS |
3255 | dst_reg_type = regs[insn->dst_reg].type; |
3256 | ||
17a52670 | 3257 | /* check that memory (dst_reg + off) is writeable */ |
31fd8581 | 3258 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
3259 | BPF_SIZE(insn->code), BPF_WRITE, |
3260 | insn->src_reg); | |
3261 | if (err) | |
3262 | return err; | |
3263 | ||
3df126f3 JK |
3264 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
3265 | ||
3266 | if (*prev_dst_type == NOT_INIT) { | |
3267 | *prev_dst_type = dst_reg_type; | |
3268 | } else if (dst_reg_type != *prev_dst_type && | |
d691f9e8 | 3269 | (dst_reg_type == PTR_TO_CTX || |
3df126f3 | 3270 | *prev_dst_type == PTR_TO_CTX)) { |
d691f9e8 AS |
3271 | verbose("same insn cannot be used with different pointers\n"); |
3272 | return -EINVAL; | |
3273 | } | |
3274 | ||
17a52670 AS |
3275 | } else if (class == BPF_ST) { |
3276 | if (BPF_MODE(insn->code) != BPF_MEM || | |
3277 | insn->src_reg != BPF_REG_0) { | |
3278 | verbose("BPF_ST uses reserved fields\n"); | |
3279 | return -EINVAL; | |
3280 | } | |
3281 | /* check src operand */ | |
3282 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
3283 | if (err) | |
3284 | return err; | |
3285 | ||
3286 | /* check that memory (dst_reg + off) is writeable */ | |
31fd8581 | 3287 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
3288 | BPF_SIZE(insn->code), BPF_WRITE, |
3289 | -1); | |
3290 | if (err) | |
3291 | return err; | |
3292 | ||
3293 | } else if (class == BPF_JMP) { | |
3294 | u8 opcode = BPF_OP(insn->code); | |
3295 | ||
3296 | if (opcode == BPF_CALL) { | |
3297 | if (BPF_SRC(insn->code) != BPF_K || | |
3298 | insn->off != 0 || | |
3299 | insn->src_reg != BPF_REG_0 || | |
3300 | insn->dst_reg != BPF_REG_0) { | |
3301 | verbose("BPF_CALL uses reserved fields\n"); | |
3302 | return -EINVAL; | |
3303 | } | |
3304 | ||
81ed18ab | 3305 | err = check_call(env, insn->imm, insn_idx); |
17a52670 AS |
3306 | if (err) |
3307 | return err; | |
3308 | ||
3309 | } else if (opcode == BPF_JA) { | |
3310 | if (BPF_SRC(insn->code) != BPF_K || | |
3311 | insn->imm != 0 || | |
3312 | insn->src_reg != BPF_REG_0 || | |
3313 | insn->dst_reg != BPF_REG_0) { | |
3314 | verbose("BPF_JA uses reserved fields\n"); | |
3315 | return -EINVAL; | |
3316 | } | |
3317 | ||
3318 | insn_idx += insn->off + 1; | |
3319 | continue; | |
3320 | ||
3321 | } else if (opcode == BPF_EXIT) { | |
3322 | if (BPF_SRC(insn->code) != BPF_K || | |
3323 | insn->imm != 0 || | |
3324 | insn->src_reg != BPF_REG_0 || | |
3325 | insn->dst_reg != BPF_REG_0) { | |
3326 | verbose("BPF_EXIT uses reserved fields\n"); | |
3327 | return -EINVAL; | |
3328 | } | |
3329 | ||
3330 | /* eBPF calling convetion is such that R0 is used | |
3331 | * to return the value from eBPF program. | |
3332 | * Make sure that it's readable at this time | |
3333 | * of bpf_exit, which means that program wrote | |
3334 | * something into it earlier | |
3335 | */ | |
3336 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); | |
3337 | if (err) | |
3338 | return err; | |
3339 | ||
1be7f75d AS |
3340 | if (is_pointer_value(env, BPF_REG_0)) { |
3341 | verbose("R0 leaks addr as return value\n"); | |
3342 | return -EACCES; | |
3343 | } | |
3344 | ||
f1bca824 | 3345 | process_bpf_exit: |
17a52670 AS |
3346 | insn_idx = pop_stack(env, &prev_insn_idx); |
3347 | if (insn_idx < 0) { | |
3348 | break; | |
3349 | } else { | |
3350 | do_print_state = true; | |
3351 | continue; | |
3352 | } | |
3353 | } else { | |
3354 | err = check_cond_jmp_op(env, insn, &insn_idx); | |
3355 | if (err) | |
3356 | return err; | |
3357 | } | |
3358 | } else if (class == BPF_LD) { | |
3359 | u8 mode = BPF_MODE(insn->code); | |
3360 | ||
3361 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
3362 | err = check_ld_abs(env, insn); |
3363 | if (err) | |
3364 | return err; | |
3365 | ||
17a52670 AS |
3366 | } else if (mode == BPF_IMM) { |
3367 | err = check_ld_imm(env, insn); | |
3368 | if (err) | |
3369 | return err; | |
3370 | ||
3371 | insn_idx++; | |
2df70878 | 3372 | env->insn_aux_data[insn_idx].seen = true; |
17a52670 AS |
3373 | } else { |
3374 | verbose("invalid BPF_LD mode\n"); | |
3375 | return -EINVAL; | |
3376 | } | |
48461135 | 3377 | reset_reg_range_values(regs, insn->dst_reg); |
17a52670 AS |
3378 | } else { |
3379 | verbose("unknown insn class %d\n", class); | |
3380 | return -EINVAL; | |
3381 | } | |
3382 | ||
3383 | insn_idx++; | |
3384 | } | |
3385 | ||
8726679a AS |
3386 | verbose("processed %d insns, stack depth %d\n", |
3387 | insn_processed, env->prog->aux->stack_depth); | |
17a52670 AS |
3388 | return 0; |
3389 | } | |
3390 | ||
56f668df MKL |
3391 | static int check_map_prealloc(struct bpf_map *map) |
3392 | { | |
3393 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
3394 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
3395 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
3396 | !(map->map_flags & BPF_F_NO_PREALLOC); |
3397 | } | |
3398 | ||
fdc15d38 AS |
3399 | static int check_map_prog_compatibility(struct bpf_map *map, |
3400 | struct bpf_prog *prog) | |
3401 | ||
3402 | { | |
56f668df MKL |
3403 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
3404 | * preallocated hash maps, since doing memory allocation | |
3405 | * in overflow_handler can crash depending on where nmi got | |
3406 | * triggered. | |
3407 | */ | |
3408 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
3409 | if (!check_map_prealloc(map)) { | |
3410 | verbose("perf_event programs can only use preallocated hash map\n"); | |
3411 | return -EINVAL; | |
3412 | } | |
3413 | if (map->inner_map_meta && | |
3414 | !check_map_prealloc(map->inner_map_meta)) { | |
3415 | verbose("perf_event programs can only use preallocated inner hash map\n"); | |
3416 | return -EINVAL; | |
3417 | } | |
fdc15d38 AS |
3418 | } |
3419 | return 0; | |
3420 | } | |
3421 | ||
0246e64d AS |
3422 | /* look for pseudo eBPF instructions that access map FDs and |
3423 | * replace them with actual map pointers | |
3424 | */ | |
58e2af8b | 3425 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
3426 | { |
3427 | struct bpf_insn *insn = env->prog->insnsi; | |
3428 | int insn_cnt = env->prog->len; | |
fdc15d38 | 3429 | int i, j, err; |
0246e64d | 3430 | |
f1f7714e | 3431 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
3432 | if (err) |
3433 | return err; | |
3434 | ||
0246e64d | 3435 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 3436 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 3437 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
9bac3d6d AS |
3438 | verbose("BPF_LDX uses reserved fields\n"); |
3439 | return -EINVAL; | |
3440 | } | |
3441 | ||
d691f9e8 AS |
3442 | if (BPF_CLASS(insn->code) == BPF_STX && |
3443 | ((BPF_MODE(insn->code) != BPF_MEM && | |
3444 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
3445 | verbose("BPF_STX uses reserved fields\n"); | |
3446 | return -EINVAL; | |
3447 | } | |
3448 | ||
0246e64d AS |
3449 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
3450 | struct bpf_map *map; | |
3451 | struct fd f; | |
3452 | ||
3453 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
3454 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
3455 | insn[1].off != 0) { | |
3456 | verbose("invalid bpf_ld_imm64 insn\n"); | |
3457 | return -EINVAL; | |
3458 | } | |
3459 | ||
3460 | if (insn->src_reg == 0) | |
3461 | /* valid generic load 64-bit imm */ | |
3462 | goto next_insn; | |
3463 | ||
3464 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { | |
3465 | verbose("unrecognized bpf_ld_imm64 insn\n"); | |
3466 | return -EINVAL; | |
3467 | } | |
3468 | ||
3469 | f = fdget(insn->imm); | |
c2101297 | 3470 | map = __bpf_map_get(f); |
0246e64d AS |
3471 | if (IS_ERR(map)) { |
3472 | verbose("fd %d is not pointing to valid bpf_map\n", | |
3473 | insn->imm); | |
0246e64d AS |
3474 | return PTR_ERR(map); |
3475 | } | |
3476 | ||
fdc15d38 AS |
3477 | err = check_map_prog_compatibility(map, env->prog); |
3478 | if (err) { | |
3479 | fdput(f); | |
3480 | return err; | |
3481 | } | |
3482 | ||
0246e64d AS |
3483 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
3484 | insn[0].imm = (u32) (unsigned long) map; | |
3485 | insn[1].imm = ((u64) (unsigned long) map) >> 32; | |
3486 | ||
3487 | /* check whether we recorded this map already */ | |
3488 | for (j = 0; j < env->used_map_cnt; j++) | |
3489 | if (env->used_maps[j] == map) { | |
3490 | fdput(f); | |
3491 | goto next_insn; | |
3492 | } | |
3493 | ||
3494 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
3495 | fdput(f); | |
3496 | return -E2BIG; | |
3497 | } | |
3498 | ||
0246e64d AS |
3499 | /* hold the map. If the program is rejected by verifier, |
3500 | * the map will be released by release_maps() or it | |
3501 | * will be used by the valid program until it's unloaded | |
3502 | * and all maps are released in free_bpf_prog_info() | |
3503 | */ | |
92117d84 AS |
3504 | map = bpf_map_inc(map, false); |
3505 | if (IS_ERR(map)) { | |
3506 | fdput(f); | |
3507 | return PTR_ERR(map); | |
3508 | } | |
3509 | env->used_maps[env->used_map_cnt++] = map; | |
3510 | ||
0246e64d AS |
3511 | fdput(f); |
3512 | next_insn: | |
3513 | insn++; | |
3514 | i++; | |
3515 | } | |
3516 | } | |
3517 | ||
3518 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
3519 | * 'struct bpf_map *' into a register instead of user map_fd. | |
3520 | * These pointers will be used later by verifier to validate map access. | |
3521 | */ | |
3522 | return 0; | |
3523 | } | |
3524 | ||
3525 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 3526 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d AS |
3527 | { |
3528 | int i; | |
3529 | ||
3530 | for (i = 0; i < env->used_map_cnt; i++) | |
3531 | bpf_map_put(env->used_maps[i]); | |
3532 | } | |
3533 | ||
3534 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 3535 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
3536 | { |
3537 | struct bpf_insn *insn = env->prog->insnsi; | |
3538 | int insn_cnt = env->prog->len; | |
3539 | int i; | |
3540 | ||
3541 | for (i = 0; i < insn_cnt; i++, insn++) | |
3542 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
3543 | insn->src_reg = 0; | |
3544 | } | |
3545 | ||
8041902d AS |
3546 | /* single env->prog->insni[off] instruction was replaced with the range |
3547 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
3548 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
3549 | */ | |
3550 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, | |
3551 | u32 off, u32 cnt) | |
3552 | { | |
3553 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
2df70878 | 3554 | int i; |
8041902d AS |
3555 | |
3556 | if (cnt == 1) | |
3557 | return 0; | |
3558 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); | |
3559 | if (!new_data) | |
3560 | return -ENOMEM; | |
3561 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
3562 | memcpy(new_data + off + cnt - 1, old_data + off, | |
3563 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
2df70878 AS |
3564 | for (i = off; i < off + cnt - 1; i++) |
3565 | new_data[i].seen = true; | |
8041902d AS |
3566 | env->insn_aux_data = new_data; |
3567 | vfree(old_data); | |
3568 | return 0; | |
3569 | } | |
3570 | ||
3571 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, | |
3572 | const struct bpf_insn *patch, u32 len) | |
3573 | { | |
3574 | struct bpf_prog *new_prog; | |
3575 | ||
3576 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
3577 | if (!new_prog) | |
3578 | return NULL; | |
3579 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) | |
3580 | return NULL; | |
3581 | return new_prog; | |
3582 | } | |
3583 | ||
2df70878 AS |
3584 | /* The verifier does more data flow analysis than llvm and will not explore |
3585 | * branches that are dead at run time. Malicious programs can have dead code | |
3586 | * too. Therefore replace all dead at-run-time code with nops. | |
3587 | */ | |
3588 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
3589 | { | |
3590 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
3591 | struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0); | |
3592 | struct bpf_insn *insn = env->prog->insnsi; | |
3593 | const int insn_cnt = env->prog->len; | |
3594 | int i; | |
3595 | ||
3596 | for (i = 0; i < insn_cnt; i++) { | |
3597 | if (aux_data[i].seen) | |
3598 | continue; | |
3599 | memcpy(insn + i, &nop, sizeof(nop)); | |
3600 | } | |
3601 | } | |
3602 | ||
9bac3d6d AS |
3603 | /* convert load instructions that access fields of 'struct __sk_buff' |
3604 | * into sequence of instructions that access fields of 'struct sk_buff' | |
3605 | */ | |
58e2af8b | 3606 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 3607 | { |
36bbef52 | 3608 | const struct bpf_verifier_ops *ops = env->prog->aux->ops; |
f96da094 | 3609 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 3610 | const int insn_cnt = env->prog->len; |
36bbef52 | 3611 | struct bpf_insn insn_buf[16], *insn; |
9bac3d6d | 3612 | struct bpf_prog *new_prog; |
d691f9e8 | 3613 | enum bpf_access_type type; |
f96da094 DB |
3614 | bool is_narrower_load; |
3615 | u32 target_size; | |
9bac3d6d | 3616 | |
36bbef52 DB |
3617 | if (ops->gen_prologue) { |
3618 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, | |
3619 | env->prog); | |
3620 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
3621 | verbose("bpf verifier is misconfigured\n"); | |
3622 | return -EINVAL; | |
3623 | } else if (cnt) { | |
8041902d | 3624 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
3625 | if (!new_prog) |
3626 | return -ENOMEM; | |
8041902d | 3627 | |
36bbef52 | 3628 | env->prog = new_prog; |
3df126f3 | 3629 | delta += cnt - 1; |
36bbef52 DB |
3630 | } |
3631 | } | |
3632 | ||
3633 | if (!ops->convert_ctx_access) | |
9bac3d6d AS |
3634 | return 0; |
3635 | ||
3df126f3 | 3636 | insn = env->prog->insnsi + delta; |
36bbef52 | 3637 | |
9bac3d6d | 3638 | for (i = 0; i < insn_cnt; i++, insn++) { |
62c7989b DB |
3639 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
3640 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
3641 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 3642 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 3643 | type = BPF_READ; |
62c7989b DB |
3644 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
3645 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
3646 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 3647 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
3648 | type = BPF_WRITE; |
3649 | else | |
9bac3d6d AS |
3650 | continue; |
3651 | ||
674d8995 AS |
3652 | if (type == BPF_WRITE && |
3653 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
3654 | struct bpf_insn patch[] = { | |
3655 | /* Sanitize suspicious stack slot with zero. | |
3656 | * There are no memory dependencies for this store, | |
3657 | * since it's only using frame pointer and immediate | |
3658 | * constant of zero | |
3659 | */ | |
3660 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
3661 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
3662 | 0), | |
3663 | /* the original STX instruction will immediately | |
3664 | * overwrite the same stack slot with appropriate value | |
3665 | */ | |
3666 | *insn, | |
3667 | }; | |
3668 | ||
3669 | cnt = ARRAY_SIZE(patch); | |
3670 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
3671 | if (!new_prog) | |
3672 | return -ENOMEM; | |
3673 | ||
3674 | delta += cnt - 1; | |
3675 | env->prog = new_prog; | |
3676 | insn = new_prog->insnsi + i + delta; | |
3677 | continue; | |
3678 | } | |
3679 | ||
8041902d | 3680 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
9bac3d6d | 3681 | continue; |
9bac3d6d | 3682 | |
31fd8581 | 3683 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 3684 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
3685 | |
3686 | /* If the read access is a narrower load of the field, | |
3687 | * convert to a 4/8-byte load, to minimum program type specific | |
3688 | * convert_ctx_access changes. If conversion is successful, | |
3689 | * we will apply proper mask to the result. | |
3690 | */ | |
f96da094 | 3691 | is_narrower_load = size < ctx_field_size; |
31fd8581 | 3692 | if (is_narrower_load) { |
f96da094 DB |
3693 | u32 off = insn->off; |
3694 | u8 size_code; | |
3695 | ||
3696 | if (type == BPF_WRITE) { | |
3697 | verbose("bpf verifier narrow ctx access misconfigured\n"); | |
3698 | return -EINVAL; | |
3699 | } | |
31fd8581 | 3700 | |
f96da094 | 3701 | size_code = BPF_H; |
31fd8581 YS |
3702 | if (ctx_field_size == 4) |
3703 | size_code = BPF_W; | |
3704 | else if (ctx_field_size == 8) | |
3705 | size_code = BPF_DW; | |
f96da094 | 3706 | |
31fd8581 YS |
3707 | insn->off = off & ~(ctx_field_size - 1); |
3708 | insn->code = BPF_LDX | BPF_MEM | size_code; | |
3709 | } | |
f96da094 DB |
3710 | |
3711 | target_size = 0; | |
3712 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, | |
3713 | &target_size); | |
3714 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || | |
3715 | (ctx_field_size && !target_size)) { | |
9bac3d6d AS |
3716 | verbose("bpf verifier is misconfigured\n"); |
3717 | return -EINVAL; | |
3718 | } | |
f96da094 DB |
3719 | |
3720 | if (is_narrower_load && size < target_size) { | |
31fd8581 YS |
3721 | if (ctx_field_size <= 4) |
3722 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 3723 | (1 << size * 8) - 1); |
31fd8581 YS |
3724 | else |
3725 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 3726 | (1 << size * 8) - 1); |
31fd8581 | 3727 | } |
9bac3d6d | 3728 | |
8041902d | 3729 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
3730 | if (!new_prog) |
3731 | return -ENOMEM; | |
3732 | ||
3df126f3 | 3733 | delta += cnt - 1; |
9bac3d6d AS |
3734 | |
3735 | /* keep walking new program and skip insns we just inserted */ | |
3736 | env->prog = new_prog; | |
3df126f3 | 3737 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
3738 | } |
3739 | ||
3740 | return 0; | |
3741 | } | |
3742 | ||
79741b3b | 3743 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 3744 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
3745 | * |
3746 | * this function is called after eBPF program passed verification | |
3747 | */ | |
79741b3b | 3748 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 3749 | { |
79741b3b AS |
3750 | struct bpf_prog *prog = env->prog; |
3751 | struct bpf_insn *insn = prog->insnsi; | |
e245c5c6 | 3752 | const struct bpf_func_proto *fn; |
79741b3b | 3753 | const int insn_cnt = prog->len; |
81ed18ab AS |
3754 | struct bpf_insn insn_buf[16]; |
3755 | struct bpf_prog *new_prog; | |
3756 | struct bpf_map *map_ptr; | |
3757 | int i, cnt, delta = 0; | |
e245c5c6 | 3758 | |
79741b3b AS |
3759 | for (i = 0; i < insn_cnt; i++, insn++) { |
3760 | if (insn->code != (BPF_JMP | BPF_CALL)) | |
3761 | continue; | |
e245c5c6 | 3762 | |
79741b3b AS |
3763 | if (insn->imm == BPF_FUNC_get_route_realm) |
3764 | prog->dst_needed = 1; | |
3765 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
3766 | bpf_user_rnd_init_once(); | |
79741b3b | 3767 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
3768 | /* If we tail call into other programs, we |
3769 | * cannot make any assumptions since they can | |
3770 | * be replaced dynamically during runtime in | |
3771 | * the program array. | |
3772 | */ | |
3773 | prog->cb_access = 1; | |
80a58d02 | 3774 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
7b9f6da1 | 3775 | |
79741b3b AS |
3776 | /* mark bpf_tail_call as different opcode to avoid |
3777 | * conditional branch in the interpeter for every normal | |
3778 | * call and to prevent accidental JITing by JIT compiler | |
3779 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 3780 | */ |
79741b3b | 3781 | insn->imm = 0; |
71189fa9 | 3782 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
79741b3b AS |
3783 | continue; |
3784 | } | |
e245c5c6 | 3785 | |
81ed18ab AS |
3786 | if (ebpf_jit_enabled() && insn->imm == BPF_FUNC_map_lookup_elem) { |
3787 | map_ptr = env->insn_aux_data[i + delta].map_ptr; | |
fad73a1a MKL |
3788 | if (map_ptr == BPF_MAP_PTR_POISON || |
3789 | !map_ptr->ops->map_gen_lookup) | |
81ed18ab AS |
3790 | goto patch_call_imm; |
3791 | ||
3792 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); | |
3793 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
3794 | verbose("bpf verifier is misconfigured\n"); | |
3795 | return -EINVAL; | |
3796 | } | |
3797 | ||
3798 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
3799 | cnt); | |
3800 | if (!new_prog) | |
3801 | return -ENOMEM; | |
3802 | ||
3803 | delta += cnt - 1; | |
3804 | ||
3805 | /* keep walking new program and skip insns we just inserted */ | |
3806 | env->prog = prog = new_prog; | |
3807 | insn = new_prog->insnsi + i + delta; | |
3808 | continue; | |
3809 | } | |
3810 | ||
3811 | patch_call_imm: | |
79741b3b AS |
3812 | fn = prog->aux->ops->get_func_proto(insn->imm); |
3813 | /* all functions that have prototype and verifier allowed | |
3814 | * programs to call them, must be real in-kernel functions | |
3815 | */ | |
3816 | if (!fn->func) { | |
3817 | verbose("kernel subsystem misconfigured func %s#%d\n", | |
3818 | func_id_name(insn->imm), insn->imm); | |
3819 | return -EFAULT; | |
e245c5c6 | 3820 | } |
79741b3b | 3821 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 3822 | } |
e245c5c6 | 3823 | |
79741b3b AS |
3824 | return 0; |
3825 | } | |
e245c5c6 | 3826 | |
58e2af8b | 3827 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 3828 | { |
58e2af8b | 3829 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
3830 | int i; |
3831 | ||
3832 | if (!env->explored_states) | |
3833 | return; | |
3834 | ||
3835 | for (i = 0; i < env->prog->len; i++) { | |
3836 | sl = env->explored_states[i]; | |
3837 | ||
3838 | if (sl) | |
3839 | while (sl != STATE_LIST_MARK) { | |
3840 | sln = sl->next; | |
3841 | kfree(sl); | |
3842 | sl = sln; | |
3843 | } | |
3844 | } | |
3845 | ||
3846 | kfree(env->explored_states); | |
3847 | } | |
3848 | ||
9bac3d6d | 3849 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
51580e79 | 3850 | { |
cbd35700 | 3851 | char __user *log_ubuf = NULL; |
58e2af8b | 3852 | struct bpf_verifier_env *env; |
51580e79 AS |
3853 | int ret = -EINVAL; |
3854 | ||
58e2af8b | 3855 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
3856 | * allocate/free it every time bpf_check() is called |
3857 | */ | |
58e2af8b | 3858 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
3859 | if (!env) |
3860 | return -ENOMEM; | |
3861 | ||
3df126f3 JK |
3862 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
3863 | (*prog)->len); | |
3864 | ret = -ENOMEM; | |
3865 | if (!env->insn_aux_data) | |
3866 | goto err_free_env; | |
9bac3d6d | 3867 | env->prog = *prog; |
0246e64d | 3868 | |
cbd35700 AS |
3869 | /* grab the mutex to protect few globals used by verifier */ |
3870 | mutex_lock(&bpf_verifier_lock); | |
3871 | ||
3872 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
3873 | /* user requested verbose verifier output | |
3874 | * and supplied buffer to store the verification trace | |
3875 | */ | |
3876 | log_level = attr->log_level; | |
3877 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; | |
3878 | log_size = attr->log_size; | |
3879 | log_len = 0; | |
3880 | ||
3881 | ret = -EINVAL; | |
3882 | /* log_* values have to be sane */ | |
3883 | if (log_size < 128 || log_size > UINT_MAX >> 8 || | |
3884 | log_level == 0 || log_ubuf == NULL) | |
3df126f3 | 3885 | goto err_unlock; |
cbd35700 AS |
3886 | |
3887 | ret = -ENOMEM; | |
3888 | log_buf = vmalloc(log_size); | |
3889 | if (!log_buf) | |
3df126f3 | 3890 | goto err_unlock; |
cbd35700 AS |
3891 | } else { |
3892 | log_level = 0; | |
3893 | } | |
1ad2f583 DB |
3894 | |
3895 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); | |
3896 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 3897 | env->strict_alignment = true; |
cbd35700 | 3898 | |
0246e64d AS |
3899 | ret = replace_map_fd_with_map_ptr(env); |
3900 | if (ret < 0) | |
3901 | goto skip_full_check; | |
3902 | ||
9bac3d6d | 3903 | env->explored_states = kcalloc(env->prog->len, |
58e2af8b | 3904 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
3905 | GFP_USER); |
3906 | ret = -ENOMEM; | |
3907 | if (!env->explored_states) | |
3908 | goto skip_full_check; | |
3909 | ||
475fb78f AS |
3910 | ret = check_cfg(env); |
3911 | if (ret < 0) | |
3912 | goto skip_full_check; | |
3913 | ||
1be7f75d AS |
3914 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
3915 | ||
17a52670 | 3916 | ret = do_check(env); |
cbd35700 | 3917 | |
0246e64d | 3918 | skip_full_check: |
17a52670 | 3919 | while (pop_stack(env, NULL) >= 0); |
f1bca824 | 3920 | free_states(env); |
0246e64d | 3921 | |
2df70878 AS |
3922 | if (ret == 0) |
3923 | sanitize_dead_code(env); | |
3924 | ||
9bac3d6d AS |
3925 | if (ret == 0) |
3926 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
3927 | ret = convert_ctx_accesses(env); | |
3928 | ||
e245c5c6 | 3929 | if (ret == 0) |
79741b3b | 3930 | ret = fixup_bpf_calls(env); |
e245c5c6 | 3931 | |
cbd35700 AS |
3932 | if (log_level && log_len >= log_size - 1) { |
3933 | BUG_ON(log_len >= log_size); | |
3934 | /* verifier log exceeded user supplied buffer */ | |
3935 | ret = -ENOSPC; | |
3936 | /* fall through to return what was recorded */ | |
3937 | } | |
3938 | ||
3939 | /* copy verifier log back to user space including trailing zero */ | |
3940 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { | |
3941 | ret = -EFAULT; | |
3942 | goto free_log_buf; | |
3943 | } | |
3944 | ||
0246e64d AS |
3945 | if (ret == 0 && env->used_map_cnt) { |
3946 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
3947 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
3948 | sizeof(env->used_maps[0]), | |
3949 | GFP_KERNEL); | |
0246e64d | 3950 | |
9bac3d6d | 3951 | if (!env->prog->aux->used_maps) { |
0246e64d AS |
3952 | ret = -ENOMEM; |
3953 | goto free_log_buf; | |
3954 | } | |
3955 | ||
9bac3d6d | 3956 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 3957 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 3958 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
3959 | |
3960 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
3961 | * bpf_ld_imm64 instructions | |
3962 | */ | |
3963 | convert_pseudo_ld_imm64(env); | |
3964 | } | |
cbd35700 AS |
3965 | |
3966 | free_log_buf: | |
3967 | if (log_level) | |
3968 | vfree(log_buf); | |
9bac3d6d | 3969 | if (!env->prog->aux->used_maps) |
0246e64d AS |
3970 | /* if we didn't copy map pointers into bpf_prog_info, release |
3971 | * them now. Otherwise free_bpf_prog_info() will release them. | |
3972 | */ | |
3973 | release_maps(env); | |
9bac3d6d | 3974 | *prog = env->prog; |
3df126f3 | 3975 | err_unlock: |
cbd35700 | 3976 | mutex_unlock(&bpf_verifier_lock); |
3df126f3 JK |
3977 | vfree(env->insn_aux_data); |
3978 | err_free_env: | |
3979 | kfree(env); | |
51580e79 AS |
3980 | return ret; |
3981 | } | |
13a27dfc JK |
3982 | |
3983 | int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops, | |
3984 | void *priv) | |
3985 | { | |
3986 | struct bpf_verifier_env *env; | |
3987 | int ret; | |
3988 | ||
3989 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); | |
3990 | if (!env) | |
3991 | return -ENOMEM; | |
3992 | ||
3993 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * | |
3994 | prog->len); | |
3995 | ret = -ENOMEM; | |
3996 | if (!env->insn_aux_data) | |
3997 | goto err_free_env; | |
3998 | env->prog = prog; | |
3999 | env->analyzer_ops = ops; | |
4000 | env->analyzer_priv = priv; | |
4001 | ||
4002 | /* grab the mutex to protect few globals used by verifier */ | |
4003 | mutex_lock(&bpf_verifier_lock); | |
4004 | ||
4005 | log_level = 0; | |
1ad2f583 | 4006 | |
e07b98d9 | 4007 | env->strict_alignment = false; |
1ad2f583 DB |
4008 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
4009 | env->strict_alignment = true; | |
13a27dfc JK |
4010 | |
4011 | env->explored_states = kcalloc(env->prog->len, | |
4012 | sizeof(struct bpf_verifier_state_list *), | |
4013 | GFP_KERNEL); | |
4014 | ret = -ENOMEM; | |
4015 | if (!env->explored_states) | |
4016 | goto skip_full_check; | |
4017 | ||
4018 | ret = check_cfg(env); | |
4019 | if (ret < 0) | |
4020 | goto skip_full_check; | |
4021 | ||
4022 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); | |
4023 | ||
4024 | ret = do_check(env); | |
4025 | ||
4026 | skip_full_check: | |
4027 | while (pop_stack(env, NULL) >= 0); | |
4028 | free_states(env); | |
4029 | ||
4030 | mutex_unlock(&bpf_verifier_lock); | |
4031 | vfree(env->insn_aux_data); | |
4032 | err_free_env: | |
4033 | kfree(env); | |
4034 | return ret; | |
4035 | } | |
4036 | EXPORT_SYMBOL_GPL(bpf_analyzer); |