]>
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 | |
36 | * analysis is limited to 32k insn, which may be hit even if total number of | |
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 | ||
07016151 DB |
143 | #define BPF_COMPLEXITY_LIMIT_INSNS 65536 |
144 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 | |
145 | ||
33ff9823 DB |
146 | struct bpf_call_arg_meta { |
147 | struct bpf_map *map_ptr; | |
435faee1 | 148 | bool raw_mode; |
36bbef52 | 149 | bool pkt_access; |
435faee1 DB |
150 | int regno; |
151 | int access_size; | |
33ff9823 DB |
152 | }; |
153 | ||
cbd35700 AS |
154 | /* verbose verifier prints what it's seeing |
155 | * bpf_check() is called under lock, so no race to access these global vars | |
156 | */ | |
157 | static u32 log_level, log_size, log_len; | |
158 | static char *log_buf; | |
159 | ||
160 | static DEFINE_MUTEX(bpf_verifier_lock); | |
161 | ||
162 | /* log_level controls verbosity level of eBPF verifier. | |
163 | * verbose() is used to dump the verification trace to the log, so the user | |
164 | * can figure out what's wrong with the program | |
165 | */ | |
1d056d9c | 166 | static __printf(1, 2) void verbose(const char *fmt, ...) |
cbd35700 AS |
167 | { |
168 | va_list args; | |
169 | ||
170 | if (log_level == 0 || log_len >= log_size - 1) | |
171 | return; | |
172 | ||
173 | va_start(args, fmt); | |
174 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); | |
175 | va_end(args); | |
176 | } | |
177 | ||
17a52670 AS |
178 | /* string representation of 'enum bpf_reg_type' */ |
179 | static const char * const reg_type_str[] = { | |
180 | [NOT_INIT] = "?", | |
181 | [UNKNOWN_VALUE] = "inv", | |
182 | [PTR_TO_CTX] = "ctx", | |
183 | [CONST_PTR_TO_MAP] = "map_ptr", | |
184 | [PTR_TO_MAP_VALUE] = "map_value", | |
185 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
48461135 | 186 | [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj", |
17a52670 AS |
187 | [FRAME_PTR] = "fp", |
188 | [PTR_TO_STACK] = "fp", | |
189 | [CONST_IMM] = "imm", | |
969bf05e AS |
190 | [PTR_TO_PACKET] = "pkt", |
191 | [PTR_TO_PACKET_END] = "pkt_end", | |
17a52670 AS |
192 | }; |
193 | ||
ebb676da TG |
194 | #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x) |
195 | static const char * const func_id_str[] = { | |
196 | __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN) | |
197 | }; | |
198 | #undef __BPF_FUNC_STR_FN | |
199 | ||
200 | static const char *func_id_name(int id) | |
201 | { | |
202 | BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID); | |
203 | ||
204 | if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id]) | |
205 | return func_id_str[id]; | |
206 | else | |
207 | return "unknown"; | |
208 | } | |
209 | ||
58e2af8b | 210 | static void print_verifier_state(struct bpf_verifier_state *state) |
17a52670 | 211 | { |
58e2af8b | 212 | struct bpf_reg_state *reg; |
17a52670 AS |
213 | enum bpf_reg_type t; |
214 | int i; | |
215 | ||
216 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1a0dc1ac AS |
217 | reg = &state->regs[i]; |
218 | t = reg->type; | |
17a52670 AS |
219 | if (t == NOT_INIT) |
220 | continue; | |
221 | verbose(" R%d=%s", i, reg_type_str[t]); | |
222 | if (t == CONST_IMM || t == PTR_TO_STACK) | |
969bf05e AS |
223 | verbose("%lld", reg->imm); |
224 | else if (t == PTR_TO_PACKET) | |
225 | verbose("(id=%d,off=%d,r=%d)", | |
226 | reg->id, reg->off, reg->range); | |
227 | else if (t == UNKNOWN_VALUE && reg->imm) | |
228 | verbose("%lld", reg->imm); | |
17a52670 | 229 | else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || |
48461135 JB |
230 | t == PTR_TO_MAP_VALUE_OR_NULL || |
231 | t == PTR_TO_MAP_VALUE_ADJ) | |
57a09bf0 | 232 | verbose("(ks=%d,vs=%d,id=%u)", |
1a0dc1ac | 233 | reg->map_ptr->key_size, |
57a09bf0 TG |
234 | reg->map_ptr->value_size, |
235 | reg->id); | |
48461135 | 236 | if (reg->min_value != BPF_REGISTER_MIN_RANGE) |
f23cc643 JB |
237 | verbose(",min_value=%lld", |
238 | (long long)reg->min_value); | |
48461135 JB |
239 | if (reg->max_value != BPF_REGISTER_MAX_RANGE) |
240 | verbose(",max_value=%llu", | |
241 | (unsigned long long)reg->max_value); | |
17a52670 | 242 | } |
9c399760 | 243 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
1a0dc1ac | 244 | if (state->stack_slot_type[i] == STACK_SPILL) |
17a52670 | 245 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
1a0dc1ac | 246 | reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]); |
17a52670 AS |
247 | } |
248 | verbose("\n"); | |
249 | } | |
250 | ||
cbd35700 AS |
251 | static const char *const bpf_class_string[] = { |
252 | [BPF_LD] = "ld", | |
253 | [BPF_LDX] = "ldx", | |
254 | [BPF_ST] = "st", | |
255 | [BPF_STX] = "stx", | |
256 | [BPF_ALU] = "alu", | |
257 | [BPF_JMP] = "jmp", | |
258 | [BPF_RET] = "BUG", | |
259 | [BPF_ALU64] = "alu64", | |
260 | }; | |
261 | ||
687f0715 | 262 | static const char *const bpf_alu_string[16] = { |
cbd35700 AS |
263 | [BPF_ADD >> 4] = "+=", |
264 | [BPF_SUB >> 4] = "-=", | |
265 | [BPF_MUL >> 4] = "*=", | |
266 | [BPF_DIV >> 4] = "/=", | |
267 | [BPF_OR >> 4] = "|=", | |
268 | [BPF_AND >> 4] = "&=", | |
269 | [BPF_LSH >> 4] = "<<=", | |
270 | [BPF_RSH >> 4] = ">>=", | |
271 | [BPF_NEG >> 4] = "neg", | |
272 | [BPF_MOD >> 4] = "%=", | |
273 | [BPF_XOR >> 4] = "^=", | |
274 | [BPF_MOV >> 4] = "=", | |
275 | [BPF_ARSH >> 4] = "s>>=", | |
276 | [BPF_END >> 4] = "endian", | |
277 | }; | |
278 | ||
279 | static const char *const bpf_ldst_string[] = { | |
280 | [BPF_W >> 3] = "u32", | |
281 | [BPF_H >> 3] = "u16", | |
282 | [BPF_B >> 3] = "u8", | |
283 | [BPF_DW >> 3] = "u64", | |
284 | }; | |
285 | ||
687f0715 | 286 | static const char *const bpf_jmp_string[16] = { |
cbd35700 AS |
287 | [BPF_JA >> 4] = "jmp", |
288 | [BPF_JEQ >> 4] = "==", | |
289 | [BPF_JGT >> 4] = ">", | |
290 | [BPF_JGE >> 4] = ">=", | |
291 | [BPF_JSET >> 4] = "&", | |
292 | [BPF_JNE >> 4] = "!=", | |
293 | [BPF_JSGT >> 4] = "s>", | |
294 | [BPF_JSGE >> 4] = "s>=", | |
295 | [BPF_CALL >> 4] = "call", | |
296 | [BPF_EXIT >> 4] = "exit", | |
297 | }; | |
298 | ||
badebe9c DB |
299 | static void print_bpf_insn(const struct bpf_verifier_env *env, |
300 | const struct bpf_insn *insn) | |
cbd35700 AS |
301 | { |
302 | u8 class = BPF_CLASS(insn->code); | |
303 | ||
304 | if (class == BPF_ALU || class == BPF_ALU64) { | |
305 | if (BPF_SRC(insn->code) == BPF_X) | |
306 | verbose("(%02x) %sr%d %s %sr%d\n", | |
307 | insn->code, class == BPF_ALU ? "(u32) " : "", | |
308 | insn->dst_reg, | |
309 | bpf_alu_string[BPF_OP(insn->code) >> 4], | |
310 | class == BPF_ALU ? "(u32) " : "", | |
311 | insn->src_reg); | |
312 | else | |
313 | verbose("(%02x) %sr%d %s %s%d\n", | |
314 | insn->code, class == BPF_ALU ? "(u32) " : "", | |
315 | insn->dst_reg, | |
316 | bpf_alu_string[BPF_OP(insn->code) >> 4], | |
317 | class == BPF_ALU ? "(u32) " : "", | |
318 | insn->imm); | |
319 | } else if (class == BPF_STX) { | |
320 | if (BPF_MODE(insn->code) == BPF_MEM) | |
321 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", | |
322 | insn->code, | |
323 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
324 | insn->dst_reg, | |
325 | insn->off, insn->src_reg); | |
326 | else if (BPF_MODE(insn->code) == BPF_XADD) | |
327 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", | |
328 | insn->code, | |
329 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
330 | insn->dst_reg, insn->off, | |
331 | insn->src_reg); | |
332 | else | |
333 | verbose("BUG_%02x\n", insn->code); | |
334 | } else if (class == BPF_ST) { | |
335 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
336 | verbose("BUG_st_%02x\n", insn->code); | |
337 | return; | |
338 | } | |
339 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", | |
340 | insn->code, | |
341 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
342 | insn->dst_reg, | |
343 | insn->off, insn->imm); | |
344 | } else if (class == BPF_LDX) { | |
345 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
346 | verbose("BUG_ldx_%02x\n", insn->code); | |
347 | return; | |
348 | } | |
349 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", | |
350 | insn->code, insn->dst_reg, | |
351 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
352 | insn->src_reg, insn->off); | |
353 | } else if (class == BPF_LD) { | |
354 | if (BPF_MODE(insn->code) == BPF_ABS) { | |
355 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", | |
356 | insn->code, | |
357 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
358 | insn->imm); | |
359 | } else if (BPF_MODE(insn->code) == BPF_IND) { | |
360 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", | |
361 | insn->code, | |
362 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], | |
363 | insn->src_reg, insn->imm); | |
badebe9c DB |
364 | } else if (BPF_MODE(insn->code) == BPF_IMM && |
365 | BPF_SIZE(insn->code) == BPF_DW) { | |
366 | /* At this point, we already made sure that the second | |
367 | * part of the ldimm64 insn is accessible. | |
368 | */ | |
369 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; | |
370 | bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD; | |
371 | ||
372 | if (map_ptr && !env->allow_ptr_leaks) | |
373 | imm = 0; | |
374 | ||
375 | verbose("(%02x) r%d = 0x%llx\n", insn->code, | |
376 | insn->dst_reg, (unsigned long long)imm); | |
cbd35700 AS |
377 | } else { |
378 | verbose("BUG_ld_%02x\n", insn->code); | |
379 | return; | |
380 | } | |
381 | } else if (class == BPF_JMP) { | |
382 | u8 opcode = BPF_OP(insn->code); | |
383 | ||
384 | if (opcode == BPF_CALL) { | |
ebb676da TG |
385 | verbose("(%02x) call %s#%d\n", insn->code, |
386 | func_id_name(insn->imm), insn->imm); | |
cbd35700 AS |
387 | } else if (insn->code == (BPF_JMP | BPF_JA)) { |
388 | verbose("(%02x) goto pc%+d\n", | |
389 | insn->code, insn->off); | |
390 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { | |
391 | verbose("(%02x) exit\n", insn->code); | |
392 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
393 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", | |
394 | insn->code, insn->dst_reg, | |
395 | bpf_jmp_string[BPF_OP(insn->code) >> 4], | |
396 | insn->src_reg, insn->off); | |
397 | } else { | |
398 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", | |
399 | insn->code, insn->dst_reg, | |
400 | bpf_jmp_string[BPF_OP(insn->code) >> 4], | |
401 | insn->imm, insn->off); | |
402 | } | |
403 | } else { | |
404 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); | |
405 | } | |
406 | } | |
407 | ||
58e2af8b | 408 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx) |
17a52670 | 409 | { |
58e2af8b | 410 | struct bpf_verifier_stack_elem *elem; |
17a52670 AS |
411 | int insn_idx; |
412 | ||
413 | if (env->head == NULL) | |
414 | return -1; | |
415 | ||
416 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); | |
417 | insn_idx = env->head->insn_idx; | |
418 | if (prev_insn_idx) | |
419 | *prev_insn_idx = env->head->prev_insn_idx; | |
420 | elem = env->head->next; | |
421 | kfree(env->head); | |
422 | env->head = elem; | |
423 | env->stack_size--; | |
424 | return insn_idx; | |
425 | } | |
426 | ||
58e2af8b JK |
427 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
428 | int insn_idx, int prev_insn_idx) | |
17a52670 | 429 | { |
58e2af8b | 430 | struct bpf_verifier_stack_elem *elem; |
17a52670 | 431 | |
58e2af8b | 432 | elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
433 | if (!elem) |
434 | goto err; | |
435 | ||
436 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); | |
437 | elem->insn_idx = insn_idx; | |
438 | elem->prev_insn_idx = prev_insn_idx; | |
439 | elem->next = env->head; | |
440 | env->head = elem; | |
441 | env->stack_size++; | |
07016151 | 442 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
17a52670 AS |
443 | verbose("BPF program is too complex\n"); |
444 | goto err; | |
445 | } | |
446 | return &elem->st; | |
447 | err: | |
448 | /* pop all elements and return */ | |
449 | while (pop_stack(env, NULL) >= 0); | |
450 | return NULL; | |
451 | } | |
452 | ||
453 | #define CALLER_SAVED_REGS 6 | |
454 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
455 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
456 | }; | |
457 | ||
58e2af8b | 458 | static void init_reg_state(struct bpf_reg_state *regs) |
17a52670 AS |
459 | { |
460 | int i; | |
461 | ||
462 | for (i = 0; i < MAX_BPF_REG; i++) { | |
463 | regs[i].type = NOT_INIT; | |
464 | regs[i].imm = 0; | |
48461135 JB |
465 | regs[i].min_value = BPF_REGISTER_MIN_RANGE; |
466 | regs[i].max_value = BPF_REGISTER_MAX_RANGE; | |
17a52670 AS |
467 | } |
468 | ||
469 | /* frame pointer */ | |
470 | regs[BPF_REG_FP].type = FRAME_PTR; | |
471 | ||
472 | /* 1st arg to a function */ | |
473 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
474 | } | |
475 | ||
6760bf2d | 476 | static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
17a52670 | 477 | { |
17a52670 | 478 | regs[regno].type = UNKNOWN_VALUE; |
57a09bf0 | 479 | regs[regno].id = 0; |
17a52670 | 480 | regs[regno].imm = 0; |
17a52670 AS |
481 | } |
482 | ||
6760bf2d DB |
483 | static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
484 | { | |
485 | BUG_ON(regno >= MAX_BPF_REG); | |
486 | __mark_reg_unknown_value(regs, regno); | |
487 | } | |
488 | ||
48461135 JB |
489 | static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno) |
490 | { | |
491 | regs[regno].min_value = BPF_REGISTER_MIN_RANGE; | |
492 | regs[regno].max_value = BPF_REGISTER_MAX_RANGE; | |
493 | } | |
494 | ||
17a52670 AS |
495 | enum reg_arg_type { |
496 | SRC_OP, /* register is used as source operand */ | |
497 | DST_OP, /* register is used as destination operand */ | |
498 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
499 | }; | |
500 | ||
58e2af8b | 501 | static int check_reg_arg(struct bpf_reg_state *regs, u32 regno, |
17a52670 AS |
502 | enum reg_arg_type t) |
503 | { | |
504 | if (regno >= MAX_BPF_REG) { | |
505 | verbose("R%d is invalid\n", regno); | |
506 | return -EINVAL; | |
507 | } | |
508 | ||
509 | if (t == SRC_OP) { | |
510 | /* check whether register used as source operand can be read */ | |
511 | if (regs[regno].type == NOT_INIT) { | |
512 | verbose("R%d !read_ok\n", regno); | |
513 | return -EACCES; | |
514 | } | |
515 | } else { | |
516 | /* check whether register used as dest operand can be written to */ | |
517 | if (regno == BPF_REG_FP) { | |
518 | verbose("frame pointer is read only\n"); | |
519 | return -EACCES; | |
520 | } | |
521 | if (t == DST_OP) | |
522 | mark_reg_unknown_value(regs, regno); | |
523 | } | |
524 | return 0; | |
525 | } | |
526 | ||
527 | static int bpf_size_to_bytes(int bpf_size) | |
528 | { | |
529 | if (bpf_size == BPF_W) | |
530 | return 4; | |
531 | else if (bpf_size == BPF_H) | |
532 | return 2; | |
533 | else if (bpf_size == BPF_B) | |
534 | return 1; | |
535 | else if (bpf_size == BPF_DW) | |
536 | return 8; | |
537 | else | |
538 | return -EINVAL; | |
539 | } | |
540 | ||
1be7f75d AS |
541 | static bool is_spillable_regtype(enum bpf_reg_type type) |
542 | { | |
543 | switch (type) { | |
544 | case PTR_TO_MAP_VALUE: | |
545 | case PTR_TO_MAP_VALUE_OR_NULL: | |
546 | case PTR_TO_STACK: | |
547 | case PTR_TO_CTX: | |
969bf05e AS |
548 | case PTR_TO_PACKET: |
549 | case PTR_TO_PACKET_END: | |
1be7f75d AS |
550 | case FRAME_PTR: |
551 | case CONST_PTR_TO_MAP: | |
552 | return true; | |
553 | default: | |
554 | return false; | |
555 | } | |
556 | } | |
557 | ||
17a52670 AS |
558 | /* check_stack_read/write functions track spill/fill of registers, |
559 | * stack boundary and alignment are checked in check_mem_access() | |
560 | */ | |
58e2af8b JK |
561 | static int check_stack_write(struct bpf_verifier_state *state, int off, |
562 | int size, int value_regno) | |
17a52670 | 563 | { |
17a52670 | 564 | int i; |
9c399760 AS |
565 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
566 | * so it's aligned access and [off, off + size) are within stack limits | |
567 | */ | |
17a52670 AS |
568 | |
569 | if (value_regno >= 0 && | |
1be7f75d | 570 | is_spillable_regtype(state->regs[value_regno].type)) { |
17a52670 AS |
571 | |
572 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 573 | if (size != BPF_REG_SIZE) { |
17a52670 AS |
574 | verbose("invalid size of register spill\n"); |
575 | return -EACCES; | |
576 | } | |
577 | ||
17a52670 | 578 | /* save register state */ |
9c399760 AS |
579 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
580 | state->regs[value_regno]; | |
17a52670 | 581 | |
9c399760 AS |
582 | for (i = 0; i < BPF_REG_SIZE; i++) |
583 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; | |
584 | } else { | |
17a52670 | 585 | /* regular write of data into stack */ |
9c399760 | 586 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
58e2af8b | 587 | (struct bpf_reg_state) {}; |
9c399760 AS |
588 | |
589 | for (i = 0; i < size; i++) | |
590 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; | |
17a52670 AS |
591 | } |
592 | return 0; | |
593 | } | |
594 | ||
58e2af8b | 595 | static int check_stack_read(struct bpf_verifier_state *state, int off, int size, |
17a52670 AS |
596 | int value_regno) |
597 | { | |
9c399760 | 598 | u8 *slot_type; |
17a52670 | 599 | int i; |
17a52670 | 600 | |
9c399760 | 601 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
17a52670 | 602 | |
9c399760 AS |
603 | if (slot_type[0] == STACK_SPILL) { |
604 | if (size != BPF_REG_SIZE) { | |
17a52670 AS |
605 | verbose("invalid size of register spill\n"); |
606 | return -EACCES; | |
607 | } | |
9c399760 AS |
608 | for (i = 1; i < BPF_REG_SIZE; i++) { |
609 | if (slot_type[i] != STACK_SPILL) { | |
17a52670 AS |
610 | verbose("corrupted spill memory\n"); |
611 | return -EACCES; | |
612 | } | |
613 | } | |
614 | ||
615 | if (value_regno >= 0) | |
616 | /* restore register state from stack */ | |
9c399760 AS |
617 | state->regs[value_regno] = |
618 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; | |
17a52670 AS |
619 | return 0; |
620 | } else { | |
621 | for (i = 0; i < size; i++) { | |
9c399760 | 622 | if (slot_type[i] != STACK_MISC) { |
17a52670 AS |
623 | verbose("invalid read from stack off %d+%d size %d\n", |
624 | off, i, size); | |
625 | return -EACCES; | |
626 | } | |
627 | } | |
628 | if (value_regno >= 0) | |
629 | /* have read misc data from the stack */ | |
630 | mark_reg_unknown_value(state->regs, value_regno); | |
631 | return 0; | |
632 | } | |
633 | } | |
634 | ||
635 | /* check read/write into map element returned by bpf_map_lookup_elem() */ | |
58e2af8b | 636 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
17a52670 AS |
637 | int size) |
638 | { | |
639 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; | |
640 | ||
641 | if (off < 0 || off + size > map->value_size) { | |
642 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", | |
643 | map->value_size, off, size); | |
644 | return -EACCES; | |
645 | } | |
646 | return 0; | |
647 | } | |
648 | ||
969bf05e AS |
649 | #define MAX_PACKET_OFF 0xffff |
650 | ||
58e2af8b | 651 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
652 | const struct bpf_call_arg_meta *meta, |
653 | enum bpf_access_type t) | |
4acf6c0b | 654 | { |
36bbef52 | 655 | switch (env->prog->type) { |
3a0af8fd TG |
656 | case BPF_PROG_TYPE_LWT_IN: |
657 | case BPF_PROG_TYPE_LWT_OUT: | |
658 | /* dst_input() and dst_output() can't write for now */ | |
659 | if (t == BPF_WRITE) | |
660 | return false; | |
36bbef52 DB |
661 | case BPF_PROG_TYPE_SCHED_CLS: |
662 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 663 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 664 | case BPF_PROG_TYPE_LWT_XMIT: |
36bbef52 DB |
665 | if (meta) |
666 | return meta->pkt_access; | |
667 | ||
668 | env->seen_direct_write = true; | |
4acf6c0b BB |
669 | return true; |
670 | default: | |
671 | return false; | |
672 | } | |
673 | } | |
674 | ||
58e2af8b | 675 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
969bf05e AS |
676 | int size) |
677 | { | |
58e2af8b JK |
678 | struct bpf_reg_state *regs = env->cur_state.regs; |
679 | struct bpf_reg_state *reg = ®s[regno]; | |
969bf05e | 680 | |
d91b28ed | 681 | off += reg->off; |
b399cf64 | 682 | if (off < 0 || size <= 0 || off + size > reg->range) { |
d91b28ed AS |
683 | verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
684 | off, size, regno, reg->id, reg->off, reg->range); | |
969bf05e AS |
685 | return -EACCES; |
686 | } | |
687 | return 0; | |
688 | } | |
689 | ||
17a52670 | 690 | /* check access to 'struct bpf_context' fields */ |
58e2af8b | 691 | static int check_ctx_access(struct bpf_verifier_env *env, int off, int size, |
19de99f7 | 692 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 693 | { |
13a27dfc JK |
694 | /* for analyzer ctx accesses are already validated and converted */ |
695 | if (env->analyzer_ops) | |
696 | return 0; | |
697 | ||
17a52670 | 698 | if (env->prog->aux->ops->is_valid_access && |
19de99f7 | 699 | env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) { |
32bbe007 AS |
700 | /* remember the offset of last byte accessed in ctx */ |
701 | if (env->prog->aux->max_ctx_offset < off + size) | |
702 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 703 | return 0; |
32bbe007 | 704 | } |
17a52670 AS |
705 | |
706 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); | |
707 | return -EACCES; | |
708 | } | |
709 | ||
58e2af8b | 710 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
1be7f75d AS |
711 | { |
712 | if (env->allow_ptr_leaks) | |
713 | return false; | |
714 | ||
715 | switch (env->cur_state.regs[regno].type) { | |
716 | case UNKNOWN_VALUE: | |
717 | case CONST_IMM: | |
718 | return false; | |
719 | default: | |
720 | return true; | |
721 | } | |
722 | } | |
723 | ||
58e2af8b JK |
724 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
725 | struct bpf_reg_state *reg, int off, int size) | |
969bf05e | 726 | { |
48461135 | 727 | if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) { |
969bf05e | 728 | if (off % size != 0) { |
58e2af8b JK |
729 | verbose("misaligned access off %d size %d\n", |
730 | off, size); | |
969bf05e AS |
731 | return -EACCES; |
732 | } else { | |
733 | return 0; | |
734 | } | |
735 | } | |
736 | ||
969bf05e AS |
737 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
738 | /* misaligned access to packet is ok on x86,arm,arm64 */ | |
739 | return 0; | |
740 | ||
741 | if (reg->id && size != 1) { | |
742 | verbose("Unknown packet alignment. Only byte-sized access allowed\n"); | |
743 | return -EACCES; | |
744 | } | |
745 | ||
746 | /* skb->data is NET_IP_ALIGN-ed */ | |
48461135 JB |
747 | if (reg->type == PTR_TO_PACKET && |
748 | (NET_IP_ALIGN + reg->off + off) % size != 0) { | |
969bf05e AS |
749 | verbose("misaligned packet access off %d+%d+%d size %d\n", |
750 | NET_IP_ALIGN, reg->off, off, size); | |
751 | return -EACCES; | |
752 | } | |
753 | return 0; | |
754 | } | |
755 | ||
17a52670 AS |
756 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
757 | * if t==write, value_regno is a register which value is stored into memory | |
758 | * if t==read, value_regno is a register which will receive the value from memory | |
759 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
760 | * if t==read && value_regno==-1, don't care what we read from memory | |
761 | */ | |
58e2af8b | 762 | static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off, |
17a52670 AS |
763 | int bpf_size, enum bpf_access_type t, |
764 | int value_regno) | |
765 | { | |
58e2af8b JK |
766 | struct bpf_verifier_state *state = &env->cur_state; |
767 | struct bpf_reg_state *reg = &state->regs[regno]; | |
17a52670 AS |
768 | int size, err = 0; |
769 | ||
1a0dc1ac AS |
770 | if (reg->type == PTR_TO_STACK) |
771 | off += reg->imm; | |
24b4d2ab | 772 | |
17a52670 AS |
773 | size = bpf_size_to_bytes(bpf_size); |
774 | if (size < 0) | |
775 | return size; | |
776 | ||
969bf05e AS |
777 | err = check_ptr_alignment(env, reg, off, size); |
778 | if (err) | |
779 | return err; | |
17a52670 | 780 | |
48461135 JB |
781 | if (reg->type == PTR_TO_MAP_VALUE || |
782 | reg->type == PTR_TO_MAP_VALUE_ADJ) { | |
1be7f75d AS |
783 | if (t == BPF_WRITE && value_regno >= 0 && |
784 | is_pointer_value(env, value_regno)) { | |
785 | verbose("R%d leaks addr into map\n", value_regno); | |
786 | return -EACCES; | |
787 | } | |
48461135 JB |
788 | |
789 | /* If we adjusted the register to this map value at all then we | |
790 | * need to change off and size to min_value and max_value | |
791 | * respectively to make sure our theoretical access will be | |
792 | * safe. | |
793 | */ | |
794 | if (reg->type == PTR_TO_MAP_VALUE_ADJ) { | |
795 | if (log_level) | |
796 | print_verifier_state(state); | |
797 | env->varlen_map_value_access = true; | |
798 | /* The minimum value is only important with signed | |
799 | * comparisons where we can't assume the floor of a | |
800 | * value is 0. If we are using signed variables for our | |
801 | * index'es we need to make sure that whatever we use | |
802 | * will have a set floor within our range. | |
803 | */ | |
f23cc643 | 804 | if (reg->min_value < 0) { |
48461135 JB |
805 | verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
806 | regno); | |
807 | return -EACCES; | |
808 | } | |
809 | err = check_map_access(env, regno, reg->min_value + off, | |
810 | size); | |
811 | if (err) { | |
812 | verbose("R%d min value is outside of the array range\n", | |
813 | regno); | |
814 | return err; | |
815 | } | |
816 | ||
817 | /* If we haven't set a max value then we need to bail | |
818 | * since we can't be sure we won't do bad things. | |
819 | */ | |
820 | if (reg->max_value == BPF_REGISTER_MAX_RANGE) { | |
821 | verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n", | |
822 | regno); | |
823 | return -EACCES; | |
824 | } | |
825 | off += reg->max_value; | |
826 | } | |
17a52670 AS |
827 | err = check_map_access(env, regno, off, size); |
828 | if (!err && t == BPF_READ && value_regno >= 0) | |
829 | mark_reg_unknown_value(state->regs, value_regno); | |
830 | ||
1a0dc1ac | 831 | } else if (reg->type == PTR_TO_CTX) { |
19de99f7 AS |
832 | enum bpf_reg_type reg_type = UNKNOWN_VALUE; |
833 | ||
1be7f75d AS |
834 | if (t == BPF_WRITE && value_regno >= 0 && |
835 | is_pointer_value(env, value_regno)) { | |
836 | verbose("R%d leaks addr into ctx\n", value_regno); | |
837 | return -EACCES; | |
838 | } | |
19de99f7 | 839 | err = check_ctx_access(env, off, size, t, ®_type); |
969bf05e | 840 | if (!err && t == BPF_READ && value_regno >= 0) { |
17a52670 | 841 | mark_reg_unknown_value(state->regs, value_regno); |
1955351d MS |
842 | /* note that reg.[id|off|range] == 0 */ |
843 | state->regs[value_regno].type = reg_type; | |
969bf05e | 844 | } |
17a52670 | 845 | |
1a0dc1ac | 846 | } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) { |
17a52670 AS |
847 | if (off >= 0 || off < -MAX_BPF_STACK) { |
848 | verbose("invalid stack off=%d size=%d\n", off, size); | |
849 | return -EACCES; | |
850 | } | |
1be7f75d AS |
851 | if (t == BPF_WRITE) { |
852 | if (!env->allow_ptr_leaks && | |
853 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && | |
854 | size != BPF_REG_SIZE) { | |
855 | verbose("attempt to corrupt spilled pointer on stack\n"); | |
856 | return -EACCES; | |
857 | } | |
17a52670 | 858 | err = check_stack_write(state, off, size, value_regno); |
1be7f75d | 859 | } else { |
17a52670 | 860 | err = check_stack_read(state, off, size, value_regno); |
1be7f75d | 861 | } |
969bf05e | 862 | } else if (state->regs[regno].type == PTR_TO_PACKET) { |
3a0af8fd | 863 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
969bf05e AS |
864 | verbose("cannot write into packet\n"); |
865 | return -EACCES; | |
866 | } | |
4acf6c0b BB |
867 | if (t == BPF_WRITE && value_regno >= 0 && |
868 | is_pointer_value(env, value_regno)) { | |
869 | verbose("R%d leaks addr into packet\n", value_regno); | |
870 | return -EACCES; | |
871 | } | |
969bf05e AS |
872 | err = check_packet_access(env, regno, off, size); |
873 | if (!err && t == BPF_READ && value_regno >= 0) | |
874 | mark_reg_unknown_value(state->regs, value_regno); | |
17a52670 AS |
875 | } else { |
876 | verbose("R%d invalid mem access '%s'\n", | |
1a0dc1ac | 877 | regno, reg_type_str[reg->type]); |
17a52670 AS |
878 | return -EACCES; |
879 | } | |
969bf05e AS |
880 | |
881 | if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks && | |
882 | state->regs[value_regno].type == UNKNOWN_VALUE) { | |
883 | /* 1 or 2 byte load zero-extends, determine the number of | |
884 | * zero upper bits. Not doing it fo 4 byte load, since | |
885 | * such values cannot be added to ptr_to_packet anyway. | |
886 | */ | |
887 | state->regs[value_regno].imm = 64 - size * 8; | |
888 | } | |
17a52670 AS |
889 | return err; |
890 | } | |
891 | ||
58e2af8b | 892 | static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 893 | { |
58e2af8b | 894 | struct bpf_reg_state *regs = env->cur_state.regs; |
17a52670 AS |
895 | int err; |
896 | ||
897 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
898 | insn->imm != 0) { | |
899 | verbose("BPF_XADD uses reserved fields\n"); | |
900 | return -EINVAL; | |
901 | } | |
902 | ||
903 | /* check src1 operand */ | |
904 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
905 | if (err) | |
906 | return err; | |
907 | ||
908 | /* check src2 operand */ | |
909 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
910 | if (err) | |
911 | return err; | |
912 | ||
913 | /* check whether atomic_add can read the memory */ | |
914 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
915 | BPF_SIZE(insn->code), BPF_READ, -1); | |
916 | if (err) | |
917 | return err; | |
918 | ||
919 | /* check whether atomic_add can write into the same memory */ | |
920 | return check_mem_access(env, insn->dst_reg, insn->off, | |
921 | BPF_SIZE(insn->code), BPF_WRITE, -1); | |
922 | } | |
923 | ||
924 | /* when register 'regno' is passed into function that will read 'access_size' | |
925 | * bytes from that pointer, make sure that it's within stack boundary | |
926 | * and all elements of stack are initialized | |
927 | */ | |
58e2af8b | 928 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
929 | int access_size, bool zero_size_allowed, |
930 | struct bpf_call_arg_meta *meta) | |
17a52670 | 931 | { |
58e2af8b JK |
932 | struct bpf_verifier_state *state = &env->cur_state; |
933 | struct bpf_reg_state *regs = state->regs; | |
17a52670 AS |
934 | int off, i; |
935 | ||
8e2fe1d9 DB |
936 | if (regs[regno].type != PTR_TO_STACK) { |
937 | if (zero_size_allowed && access_size == 0 && | |
938 | regs[regno].type == CONST_IMM && | |
939 | regs[regno].imm == 0) | |
940 | return 0; | |
941 | ||
942 | verbose("R%d type=%s expected=%s\n", regno, | |
943 | reg_type_str[regs[regno].type], | |
944 | reg_type_str[PTR_TO_STACK]); | |
17a52670 | 945 | return -EACCES; |
8e2fe1d9 | 946 | } |
17a52670 AS |
947 | |
948 | off = regs[regno].imm; | |
949 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
950 | access_size <= 0) { | |
951 | verbose("invalid stack type R%d off=%d access_size=%d\n", | |
952 | regno, off, access_size); | |
953 | return -EACCES; | |
954 | } | |
955 | ||
435faee1 DB |
956 | if (meta && meta->raw_mode) { |
957 | meta->access_size = access_size; | |
958 | meta->regno = regno; | |
959 | return 0; | |
960 | } | |
961 | ||
17a52670 | 962 | for (i = 0; i < access_size; i++) { |
9c399760 | 963 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
17a52670 AS |
964 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
965 | off, i, access_size); | |
966 | return -EACCES; | |
967 | } | |
968 | } | |
969 | return 0; | |
970 | } | |
971 | ||
58e2af8b | 972 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
973 | enum bpf_arg_type arg_type, |
974 | struct bpf_call_arg_meta *meta) | |
17a52670 | 975 | { |
58e2af8b | 976 | struct bpf_reg_state *regs = env->cur_state.regs, *reg = ®s[regno]; |
6841de8b | 977 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
978 | int err = 0; |
979 | ||
80f1d68c | 980 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
981 | return 0; |
982 | ||
6841de8b | 983 | if (type == NOT_INIT) { |
17a52670 AS |
984 | verbose("R%d !read_ok\n", regno); |
985 | return -EACCES; | |
986 | } | |
987 | ||
1be7f75d AS |
988 | if (arg_type == ARG_ANYTHING) { |
989 | if (is_pointer_value(env, regno)) { | |
990 | verbose("R%d leaks addr into helper function\n", regno); | |
991 | return -EACCES; | |
992 | } | |
80f1d68c | 993 | return 0; |
1be7f75d | 994 | } |
80f1d68c | 995 | |
3a0af8fd TG |
996 | if (type == PTR_TO_PACKET && |
997 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { | |
36bbef52 | 998 | verbose("helper access to the packet is not allowed\n"); |
6841de8b AS |
999 | return -EACCES; |
1000 | } | |
1001 | ||
8e2fe1d9 | 1002 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
17a52670 AS |
1003 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
1004 | expected_type = PTR_TO_STACK; | |
6841de8b AS |
1005 | if (type != PTR_TO_PACKET && type != expected_type) |
1006 | goto err_type; | |
8e2fe1d9 DB |
1007 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
1008 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { | |
17a52670 | 1009 | expected_type = CONST_IMM; |
6841de8b AS |
1010 | if (type != expected_type) |
1011 | goto err_type; | |
17a52670 AS |
1012 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
1013 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
1014 | if (type != expected_type) |
1015 | goto err_type; | |
608cd71a AS |
1016 | } else if (arg_type == ARG_PTR_TO_CTX) { |
1017 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
1018 | if (type != expected_type) |
1019 | goto err_type; | |
435faee1 DB |
1020 | } else if (arg_type == ARG_PTR_TO_STACK || |
1021 | arg_type == ARG_PTR_TO_RAW_STACK) { | |
8e2fe1d9 DB |
1022 | expected_type = PTR_TO_STACK; |
1023 | /* One exception here. In case function allows for NULL to be | |
1024 | * passed in as argument, it's a CONST_IMM type. Final test | |
1025 | * happens during stack boundary checking. | |
1026 | */ | |
6841de8b AS |
1027 | if (type == CONST_IMM && reg->imm == 0) |
1028 | /* final test in check_stack_boundary() */; | |
1029 | else if (type != PTR_TO_PACKET && type != expected_type) | |
1030 | goto err_type; | |
435faee1 | 1031 | meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK; |
17a52670 AS |
1032 | } else { |
1033 | verbose("unsupported arg_type %d\n", arg_type); | |
1034 | return -EFAULT; | |
1035 | } | |
1036 | ||
17a52670 AS |
1037 | if (arg_type == ARG_CONST_MAP_PTR) { |
1038 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 1039 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
1040 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
1041 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
1042 | * check that [key, key + map->key_size) are within | |
1043 | * stack limits and initialized | |
1044 | */ | |
33ff9823 | 1045 | if (!meta->map_ptr) { |
17a52670 AS |
1046 | /* in function declaration map_ptr must come before |
1047 | * map_key, so that it's verified and known before | |
1048 | * we have to check map_key here. Otherwise it means | |
1049 | * that kernel subsystem misconfigured verifier | |
1050 | */ | |
1051 | verbose("invalid map_ptr to access map->key\n"); | |
1052 | return -EACCES; | |
1053 | } | |
6841de8b AS |
1054 | if (type == PTR_TO_PACKET) |
1055 | err = check_packet_access(env, regno, 0, | |
1056 | meta->map_ptr->key_size); | |
1057 | else | |
1058 | err = check_stack_boundary(env, regno, | |
1059 | meta->map_ptr->key_size, | |
1060 | false, NULL); | |
17a52670 AS |
1061 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
1062 | /* bpf_map_xxx(..., map_ptr, ..., value) call: | |
1063 | * check [value, value + map->value_size) validity | |
1064 | */ | |
33ff9823 | 1065 | if (!meta->map_ptr) { |
17a52670 AS |
1066 | /* kernel subsystem misconfigured verifier */ |
1067 | verbose("invalid map_ptr to access map->value\n"); | |
1068 | return -EACCES; | |
1069 | } | |
6841de8b AS |
1070 | if (type == PTR_TO_PACKET) |
1071 | err = check_packet_access(env, regno, 0, | |
1072 | meta->map_ptr->value_size); | |
1073 | else | |
1074 | err = check_stack_boundary(env, regno, | |
1075 | meta->map_ptr->value_size, | |
1076 | false, NULL); | |
8e2fe1d9 DB |
1077 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
1078 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { | |
1079 | bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO); | |
17a52670 | 1080 | |
17a52670 AS |
1081 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
1082 | * from stack pointer 'buf'. Check it | |
1083 | * note: regno == len, regno - 1 == buf | |
1084 | */ | |
1085 | if (regno == 0) { | |
1086 | /* kernel subsystem misconfigured verifier */ | |
1087 | verbose("ARG_CONST_STACK_SIZE cannot be first argument\n"); | |
1088 | return -EACCES; | |
1089 | } | |
6841de8b AS |
1090 | if (regs[regno - 1].type == PTR_TO_PACKET) |
1091 | err = check_packet_access(env, regno - 1, 0, reg->imm); | |
1092 | else | |
1093 | err = check_stack_boundary(env, regno - 1, reg->imm, | |
1094 | zero_size_allowed, meta); | |
17a52670 AS |
1095 | } |
1096 | ||
1097 | return err; | |
6841de8b AS |
1098 | err_type: |
1099 | verbose("R%d type=%s expected=%s\n", regno, | |
1100 | reg_type_str[type], reg_type_str[expected_type]); | |
1101 | return -EACCES; | |
17a52670 AS |
1102 | } |
1103 | ||
35578d79 KX |
1104 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
1105 | { | |
35578d79 KX |
1106 | if (!map) |
1107 | return 0; | |
1108 | ||
6aff67c8 AS |
1109 | /* We need a two way check, first is from map perspective ... */ |
1110 | switch (map->map_type) { | |
1111 | case BPF_MAP_TYPE_PROG_ARRAY: | |
1112 | if (func_id != BPF_FUNC_tail_call) | |
1113 | goto error; | |
1114 | break; | |
1115 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
1116 | if (func_id != BPF_FUNC_perf_event_read && | |
1117 | func_id != BPF_FUNC_perf_event_output) | |
1118 | goto error; | |
1119 | break; | |
1120 | case BPF_MAP_TYPE_STACK_TRACE: | |
1121 | if (func_id != BPF_FUNC_get_stackid) | |
1122 | goto error; | |
1123 | break; | |
4ed8ec52 | 1124 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 1125 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 1126 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
1127 | goto error; |
1128 | break; | |
6aff67c8 AS |
1129 | default: |
1130 | break; | |
1131 | } | |
1132 | ||
1133 | /* ... and second from the function itself. */ | |
1134 | switch (func_id) { | |
1135 | case BPF_FUNC_tail_call: | |
1136 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
1137 | goto error; | |
1138 | break; | |
1139 | case BPF_FUNC_perf_event_read: | |
1140 | case BPF_FUNC_perf_event_output: | |
1141 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) | |
1142 | goto error; | |
1143 | break; | |
1144 | case BPF_FUNC_get_stackid: | |
1145 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
1146 | goto error; | |
1147 | break; | |
60d20f91 | 1148 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 1149 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
1150 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
1151 | goto error; | |
1152 | break; | |
6aff67c8 AS |
1153 | default: |
1154 | break; | |
35578d79 KX |
1155 | } |
1156 | ||
1157 | return 0; | |
6aff67c8 | 1158 | error: |
ebb676da TG |
1159 | verbose("cannot pass map_type %d into func %s#%d\n", |
1160 | map->map_type, func_id_name(func_id), func_id); | |
6aff67c8 | 1161 | return -EINVAL; |
35578d79 KX |
1162 | } |
1163 | ||
435faee1 DB |
1164 | static int check_raw_mode(const struct bpf_func_proto *fn) |
1165 | { | |
1166 | int count = 0; | |
1167 | ||
1168 | if (fn->arg1_type == ARG_PTR_TO_RAW_STACK) | |
1169 | count++; | |
1170 | if (fn->arg2_type == ARG_PTR_TO_RAW_STACK) | |
1171 | count++; | |
1172 | if (fn->arg3_type == ARG_PTR_TO_RAW_STACK) | |
1173 | count++; | |
1174 | if (fn->arg4_type == ARG_PTR_TO_RAW_STACK) | |
1175 | count++; | |
1176 | if (fn->arg5_type == ARG_PTR_TO_RAW_STACK) | |
1177 | count++; | |
1178 | ||
1179 | return count > 1 ? -EINVAL : 0; | |
1180 | } | |
1181 | ||
58e2af8b | 1182 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 1183 | { |
58e2af8b JK |
1184 | struct bpf_verifier_state *state = &env->cur_state; |
1185 | struct bpf_reg_state *regs = state->regs, *reg; | |
969bf05e AS |
1186 | int i; |
1187 | ||
1188 | for (i = 0; i < MAX_BPF_REG; i++) | |
1189 | if (regs[i].type == PTR_TO_PACKET || | |
1190 | regs[i].type == PTR_TO_PACKET_END) | |
1191 | mark_reg_unknown_value(regs, i); | |
1192 | ||
1193 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { | |
1194 | if (state->stack_slot_type[i] != STACK_SPILL) | |
1195 | continue; | |
1196 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; | |
1197 | if (reg->type != PTR_TO_PACKET && | |
1198 | reg->type != PTR_TO_PACKET_END) | |
1199 | continue; | |
1200 | reg->type = UNKNOWN_VALUE; | |
1201 | reg->imm = 0; | |
1202 | } | |
1203 | } | |
1204 | ||
58e2af8b | 1205 | static int check_call(struct bpf_verifier_env *env, int func_id) |
17a52670 | 1206 | { |
58e2af8b | 1207 | struct bpf_verifier_state *state = &env->cur_state; |
17a52670 | 1208 | const struct bpf_func_proto *fn = NULL; |
58e2af8b JK |
1209 | struct bpf_reg_state *regs = state->regs; |
1210 | struct bpf_reg_state *reg; | |
33ff9823 | 1211 | struct bpf_call_arg_meta meta; |
969bf05e | 1212 | bool changes_data; |
17a52670 AS |
1213 | int i, err; |
1214 | ||
1215 | /* find function prototype */ | |
1216 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
ebb676da | 1217 | verbose("invalid func %s#%d\n", func_id_name(func_id), func_id); |
17a52670 AS |
1218 | return -EINVAL; |
1219 | } | |
1220 | ||
1221 | if (env->prog->aux->ops->get_func_proto) | |
1222 | fn = env->prog->aux->ops->get_func_proto(func_id); | |
1223 | ||
1224 | if (!fn) { | |
ebb676da | 1225 | verbose("unknown func %s#%d\n", func_id_name(func_id), func_id); |
17a52670 AS |
1226 | return -EINVAL; |
1227 | } | |
1228 | ||
1229 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 1230 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
17a52670 AS |
1231 | verbose("cannot call GPL only function from proprietary program\n"); |
1232 | return -EINVAL; | |
1233 | } | |
1234 | ||
17bedab2 | 1235 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
969bf05e | 1236 | |
33ff9823 | 1237 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 1238 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 1239 | |
435faee1 DB |
1240 | /* We only support one arg being in raw mode at the moment, which |
1241 | * is sufficient for the helper functions we have right now. | |
1242 | */ | |
1243 | err = check_raw_mode(fn); | |
1244 | if (err) { | |
ebb676da TG |
1245 | verbose("kernel subsystem misconfigured func %s#%d\n", |
1246 | func_id_name(func_id), func_id); | |
435faee1 DB |
1247 | return err; |
1248 | } | |
1249 | ||
17a52670 | 1250 | /* check args */ |
33ff9823 | 1251 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
1252 | if (err) |
1253 | return err; | |
33ff9823 | 1254 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
1255 | if (err) |
1256 | return err; | |
33ff9823 | 1257 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
1258 | if (err) |
1259 | return err; | |
33ff9823 | 1260 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
1261 | if (err) |
1262 | return err; | |
33ff9823 | 1263 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
1264 | if (err) |
1265 | return err; | |
1266 | ||
435faee1 DB |
1267 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
1268 | * is inferred from register state. | |
1269 | */ | |
1270 | for (i = 0; i < meta.access_size; i++) { | |
1271 | err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1); | |
1272 | if (err) | |
1273 | return err; | |
1274 | } | |
1275 | ||
17a52670 AS |
1276 | /* reset caller saved regs */ |
1277 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
1278 | reg = regs + caller_saved[i]; | |
1279 | reg->type = NOT_INIT; | |
1280 | reg->imm = 0; | |
1281 | } | |
1282 | ||
1283 | /* update return register */ | |
1284 | if (fn->ret_type == RET_INTEGER) { | |
1285 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
1286 | } else if (fn->ret_type == RET_VOID) { | |
1287 | regs[BPF_REG_0].type = NOT_INIT; | |
1288 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { | |
1289 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
48461135 | 1290 | regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0; |
17a52670 AS |
1291 | /* remember map_ptr, so that check_map_access() |
1292 | * can check 'value_size' boundary of memory access | |
1293 | * to map element returned from bpf_map_lookup_elem() | |
1294 | */ | |
33ff9823 | 1295 | if (meta.map_ptr == NULL) { |
17a52670 AS |
1296 | verbose("kernel subsystem misconfigured verifier\n"); |
1297 | return -EINVAL; | |
1298 | } | |
33ff9823 | 1299 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
57a09bf0 | 1300 | regs[BPF_REG_0].id = ++env->id_gen; |
17a52670 | 1301 | } else { |
ebb676da TG |
1302 | verbose("unknown return type %d of func %s#%d\n", |
1303 | fn->ret_type, func_id_name(func_id), func_id); | |
17a52670 AS |
1304 | return -EINVAL; |
1305 | } | |
04fd61ab | 1306 | |
33ff9823 | 1307 | err = check_map_func_compatibility(meta.map_ptr, func_id); |
35578d79 KX |
1308 | if (err) |
1309 | return err; | |
04fd61ab | 1310 | |
969bf05e AS |
1311 | if (changes_data) |
1312 | clear_all_pkt_pointers(env); | |
1313 | return 0; | |
1314 | } | |
1315 | ||
58e2af8b JK |
1316 | static int check_packet_ptr_add(struct bpf_verifier_env *env, |
1317 | struct bpf_insn *insn) | |
969bf05e | 1318 | { |
58e2af8b JK |
1319 | struct bpf_reg_state *regs = env->cur_state.regs; |
1320 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; | |
1321 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; | |
1322 | struct bpf_reg_state tmp_reg; | |
969bf05e AS |
1323 | s32 imm; |
1324 | ||
1325 | if (BPF_SRC(insn->code) == BPF_K) { | |
1326 | /* pkt_ptr += imm */ | |
1327 | imm = insn->imm; | |
1328 | ||
1329 | add_imm: | |
1330 | if (imm <= 0) { | |
1331 | verbose("addition of negative constant to packet pointer is not allowed\n"); | |
1332 | return -EACCES; | |
1333 | } | |
1334 | if (imm >= MAX_PACKET_OFF || | |
1335 | imm + dst_reg->off >= MAX_PACKET_OFF) { | |
1336 | verbose("constant %d is too large to add to packet pointer\n", | |
1337 | imm); | |
1338 | return -EACCES; | |
1339 | } | |
1340 | /* a constant was added to pkt_ptr. | |
1341 | * Remember it while keeping the same 'id' | |
1342 | */ | |
1343 | dst_reg->off += imm; | |
1344 | } else { | |
1b9b69ec AS |
1345 | if (src_reg->type == PTR_TO_PACKET) { |
1346 | /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */ | |
1347 | tmp_reg = *dst_reg; /* save r7 state */ | |
1348 | *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */ | |
1349 | src_reg = &tmp_reg; /* pretend it's src_reg state */ | |
1350 | /* if the checks below reject it, the copy won't matter, | |
1351 | * since we're rejecting the whole program. If all ok, | |
1352 | * then imm22 state will be added to r7 | |
1353 | * and r7 will be pkt(id=0,off=22,r=62) while | |
1354 | * r6 will stay as pkt(id=0,off=0,r=62) | |
1355 | */ | |
1356 | } | |
1357 | ||
969bf05e AS |
1358 | if (src_reg->type == CONST_IMM) { |
1359 | /* pkt_ptr += reg where reg is known constant */ | |
1360 | imm = src_reg->imm; | |
1361 | goto add_imm; | |
1362 | } | |
1363 | /* disallow pkt_ptr += reg | |
1364 | * if reg is not uknown_value with guaranteed zero upper bits | |
1365 | * otherwise pkt_ptr may overflow and addition will become | |
1366 | * subtraction which is not allowed | |
1367 | */ | |
1368 | if (src_reg->type != UNKNOWN_VALUE) { | |
1369 | verbose("cannot add '%s' to ptr_to_packet\n", | |
1370 | reg_type_str[src_reg->type]); | |
1371 | return -EACCES; | |
1372 | } | |
1373 | if (src_reg->imm < 48) { | |
1374 | verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n", | |
1375 | src_reg->imm); | |
1376 | return -EACCES; | |
1377 | } | |
1378 | /* dst_reg stays as pkt_ptr type and since some positive | |
1379 | * integer value was added to the pointer, increment its 'id' | |
1380 | */ | |
1f415a74 | 1381 | dst_reg->id = ++env->id_gen; |
969bf05e AS |
1382 | |
1383 | /* something was added to pkt_ptr, set range and off to zero */ | |
1384 | dst_reg->off = 0; | |
1385 | dst_reg->range = 0; | |
1386 | } | |
1387 | return 0; | |
1388 | } | |
1389 | ||
58e2af8b | 1390 | static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn) |
969bf05e | 1391 | { |
58e2af8b JK |
1392 | struct bpf_reg_state *regs = env->cur_state.regs; |
1393 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; | |
969bf05e AS |
1394 | u8 opcode = BPF_OP(insn->code); |
1395 | s64 imm_log2; | |
1396 | ||
1397 | /* for type == UNKNOWN_VALUE: | |
1398 | * imm > 0 -> number of zero upper bits | |
1399 | * imm == 0 -> don't track which is the same as all bits can be non-zero | |
1400 | */ | |
1401 | ||
1402 | if (BPF_SRC(insn->code) == BPF_X) { | |
58e2af8b | 1403 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
969bf05e AS |
1404 | |
1405 | if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 && | |
1406 | dst_reg->imm && opcode == BPF_ADD) { | |
1407 | /* dreg += sreg | |
1408 | * where both have zero upper bits. Adding them | |
1409 | * can only result making one more bit non-zero | |
1410 | * in the larger value. | |
1411 | * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47) | |
1412 | * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47) | |
1413 | */ | |
1414 | dst_reg->imm = min(dst_reg->imm, src_reg->imm); | |
1415 | dst_reg->imm--; | |
1416 | return 0; | |
1417 | } | |
1418 | if (src_reg->type == CONST_IMM && src_reg->imm > 0 && | |
1419 | dst_reg->imm && opcode == BPF_ADD) { | |
1420 | /* dreg += sreg | |
1421 | * where dreg has zero upper bits and sreg is const. | |
1422 | * Adding them can only result making one more bit | |
1423 | * non-zero in the larger value. | |
1424 | */ | |
1425 | imm_log2 = __ilog2_u64((long long)src_reg->imm); | |
1426 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); | |
1427 | dst_reg->imm--; | |
1428 | return 0; | |
1429 | } | |
1430 | /* all other cases non supported yet, just mark dst_reg */ | |
1431 | dst_reg->imm = 0; | |
1432 | return 0; | |
1433 | } | |
1434 | ||
1435 | /* sign extend 32-bit imm into 64-bit to make sure that | |
1436 | * negative values occupy bit 63. Note ilog2() would have | |
1437 | * been incorrect, since sizeof(insn->imm) == 4 | |
1438 | */ | |
1439 | imm_log2 = __ilog2_u64((long long)insn->imm); | |
1440 | ||
1441 | if (dst_reg->imm && opcode == BPF_LSH) { | |
1442 | /* reg <<= imm | |
1443 | * if reg was a result of 2 byte load, then its imm == 48 | |
1444 | * which means that upper 48 bits are zero and shifting this reg | |
1445 | * left by 4 would mean that upper 44 bits are still zero | |
1446 | */ | |
1447 | dst_reg->imm -= insn->imm; | |
1448 | } else if (dst_reg->imm && opcode == BPF_MUL) { | |
1449 | /* reg *= imm | |
1450 | * if multiplying by 14 subtract 4 | |
1451 | * This is conservative calculation of upper zero bits. | |
1452 | * It's not trying to special case insn->imm == 1 or 0 cases | |
1453 | */ | |
1454 | dst_reg->imm -= imm_log2 + 1; | |
1455 | } else if (opcode == BPF_AND) { | |
1456 | /* reg &= imm */ | |
1457 | dst_reg->imm = 63 - imm_log2; | |
1458 | } else if (dst_reg->imm && opcode == BPF_ADD) { | |
1459 | /* reg += imm */ | |
1460 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); | |
1461 | dst_reg->imm--; | |
1462 | } else if (opcode == BPF_RSH) { | |
1463 | /* reg >>= imm | |
1464 | * which means that after right shift, upper bits will be zero | |
1465 | * note that verifier already checked that | |
1466 | * 0 <= imm < 64 for shift insn | |
1467 | */ | |
1468 | dst_reg->imm += insn->imm; | |
1469 | if (unlikely(dst_reg->imm > 64)) | |
1470 | /* some dumb code did: | |
1471 | * r2 = *(u32 *)mem; | |
1472 | * r2 >>= 32; | |
1473 | * and all bits are zero now */ | |
1474 | dst_reg->imm = 64; | |
1475 | } else { | |
1476 | /* all other alu ops, means that we don't know what will | |
1477 | * happen to the value, mark it with unknown number of zero bits | |
1478 | */ | |
1479 | dst_reg->imm = 0; | |
1480 | } | |
1481 | ||
1482 | if (dst_reg->imm < 0) { | |
1483 | /* all 64 bits of the register can contain non-zero bits | |
1484 | * and such value cannot be added to ptr_to_packet, since it | |
1485 | * may overflow, mark it as unknown to avoid further eval | |
1486 | */ | |
1487 | dst_reg->imm = 0; | |
1488 | } | |
1489 | return 0; | |
1490 | } | |
1491 | ||
58e2af8b JK |
1492 | static int evaluate_reg_imm_alu(struct bpf_verifier_env *env, |
1493 | struct bpf_insn *insn) | |
969bf05e | 1494 | { |
58e2af8b JK |
1495 | struct bpf_reg_state *regs = env->cur_state.regs; |
1496 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; | |
1497 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; | |
969bf05e AS |
1498 | u8 opcode = BPF_OP(insn->code); |
1499 | ||
3c839744 GB |
1500 | /* dst_reg->type == CONST_IMM here, simulate execution of 'add'/'or' |
1501 | * insn. Don't care about overflow or negative values, just add them | |
969bf05e AS |
1502 | */ |
1503 | if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) | |
1504 | dst_reg->imm += insn->imm; | |
1505 | else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X && | |
1506 | src_reg->type == CONST_IMM) | |
1507 | dst_reg->imm += src_reg->imm; | |
3c839744 GB |
1508 | else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_K) |
1509 | dst_reg->imm |= insn->imm; | |
1510 | else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_X && | |
1511 | src_reg->type == CONST_IMM) | |
1512 | dst_reg->imm |= src_reg->imm; | |
969bf05e AS |
1513 | else |
1514 | mark_reg_unknown_value(regs, insn->dst_reg); | |
17a52670 AS |
1515 | return 0; |
1516 | } | |
1517 | ||
48461135 JB |
1518 | static void check_reg_overflow(struct bpf_reg_state *reg) |
1519 | { | |
1520 | if (reg->max_value > BPF_REGISTER_MAX_RANGE) | |
1521 | reg->max_value = BPF_REGISTER_MAX_RANGE; | |
f23cc643 JB |
1522 | if (reg->min_value < BPF_REGISTER_MIN_RANGE || |
1523 | reg->min_value > BPF_REGISTER_MAX_RANGE) | |
48461135 JB |
1524 | reg->min_value = BPF_REGISTER_MIN_RANGE; |
1525 | } | |
1526 | ||
1527 | static void adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
1528 | struct bpf_insn *insn) | |
1529 | { | |
1530 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; | |
f23cc643 JB |
1531 | s64 min_val = BPF_REGISTER_MIN_RANGE; |
1532 | u64 max_val = BPF_REGISTER_MAX_RANGE; | |
48461135 JB |
1533 | u8 opcode = BPF_OP(insn->code); |
1534 | ||
1535 | dst_reg = ®s[insn->dst_reg]; | |
1536 | if (BPF_SRC(insn->code) == BPF_X) { | |
1537 | check_reg_overflow(®s[insn->src_reg]); | |
1538 | min_val = regs[insn->src_reg].min_value; | |
1539 | max_val = regs[insn->src_reg].max_value; | |
1540 | ||
1541 | /* If the source register is a random pointer then the | |
1542 | * min_value/max_value values represent the range of the known | |
1543 | * accesses into that value, not the actual min/max value of the | |
1544 | * register itself. In this case we have to reset the reg range | |
1545 | * values so we know it is not safe to look at. | |
1546 | */ | |
1547 | if (regs[insn->src_reg].type != CONST_IMM && | |
1548 | regs[insn->src_reg].type != UNKNOWN_VALUE) { | |
1549 | min_val = BPF_REGISTER_MIN_RANGE; | |
1550 | max_val = BPF_REGISTER_MAX_RANGE; | |
1551 | } | |
1552 | } else if (insn->imm < BPF_REGISTER_MAX_RANGE && | |
1553 | (s64)insn->imm > BPF_REGISTER_MIN_RANGE) { | |
1554 | min_val = max_val = insn->imm; | |
48461135 JB |
1555 | } |
1556 | ||
1557 | /* We don't know anything about what was done to this register, mark it | |
1558 | * as unknown. | |
1559 | */ | |
1560 | if (min_val == BPF_REGISTER_MIN_RANGE && | |
1561 | max_val == BPF_REGISTER_MAX_RANGE) { | |
1562 | reset_reg_range_values(regs, insn->dst_reg); | |
1563 | return; | |
1564 | } | |
1565 | ||
f23cc643 JB |
1566 | /* If one of our values was at the end of our ranges then we can't just |
1567 | * do our normal operations to the register, we need to set the values | |
1568 | * to the min/max since they are undefined. | |
1569 | */ | |
1570 | if (min_val == BPF_REGISTER_MIN_RANGE) | |
1571 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; | |
1572 | if (max_val == BPF_REGISTER_MAX_RANGE) | |
1573 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; | |
1574 | ||
48461135 JB |
1575 | switch (opcode) { |
1576 | case BPF_ADD: | |
f23cc643 JB |
1577 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
1578 | dst_reg->min_value += min_val; | |
1579 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) | |
1580 | dst_reg->max_value += max_val; | |
48461135 JB |
1581 | break; |
1582 | case BPF_SUB: | |
f23cc643 JB |
1583 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
1584 | dst_reg->min_value -= min_val; | |
1585 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) | |
1586 | dst_reg->max_value -= max_val; | |
48461135 JB |
1587 | break; |
1588 | case BPF_MUL: | |
f23cc643 JB |
1589 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
1590 | dst_reg->min_value *= min_val; | |
1591 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) | |
1592 | dst_reg->max_value *= max_val; | |
48461135 JB |
1593 | break; |
1594 | case BPF_AND: | |
f23cc643 JB |
1595 | /* Disallow AND'ing of negative numbers, ain't nobody got time |
1596 | * for that. Otherwise the minimum is 0 and the max is the max | |
1597 | * value we could AND against. | |
1598 | */ | |
1599 | if (min_val < 0) | |
1600 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; | |
1601 | else | |
1602 | dst_reg->min_value = 0; | |
48461135 JB |
1603 | dst_reg->max_value = max_val; |
1604 | break; | |
1605 | case BPF_LSH: | |
1606 | /* Gotta have special overflow logic here, if we're shifting | |
1607 | * more than MAX_RANGE then just assume we have an invalid | |
1608 | * range. | |
1609 | */ | |
1610 | if (min_val > ilog2(BPF_REGISTER_MAX_RANGE)) | |
1611 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; | |
f23cc643 | 1612 | else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
48461135 JB |
1613 | dst_reg->min_value <<= min_val; |
1614 | ||
1615 | if (max_val > ilog2(BPF_REGISTER_MAX_RANGE)) | |
1616 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; | |
f23cc643 | 1617 | else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
48461135 JB |
1618 | dst_reg->max_value <<= max_val; |
1619 | break; | |
1620 | case BPF_RSH: | |
f23cc643 JB |
1621 | /* RSH by a negative number is undefined, and the BPF_RSH is an |
1622 | * unsigned shift, so make the appropriate casts. | |
48461135 | 1623 | */ |
f23cc643 JB |
1624 | if (min_val < 0 || dst_reg->min_value < 0) |
1625 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; | |
1626 | else | |
1627 | dst_reg->min_value = | |
1628 | (u64)(dst_reg->min_value) >> min_val; | |
1629 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) | |
1630 | dst_reg->max_value >>= max_val; | |
48461135 JB |
1631 | break; |
1632 | default: | |
1633 | reset_reg_range_values(regs, insn->dst_reg); | |
1634 | break; | |
1635 | } | |
1636 | ||
1637 | check_reg_overflow(dst_reg); | |
1638 | } | |
1639 | ||
17a52670 | 1640 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 1641 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 1642 | { |
58e2af8b | 1643 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; |
17a52670 AS |
1644 | u8 opcode = BPF_OP(insn->code); |
1645 | int err; | |
1646 | ||
1647 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
1648 | if (opcode == BPF_NEG) { | |
1649 | if (BPF_SRC(insn->code) != 0 || | |
1650 | insn->src_reg != BPF_REG_0 || | |
1651 | insn->off != 0 || insn->imm != 0) { | |
1652 | verbose("BPF_NEG uses reserved fields\n"); | |
1653 | return -EINVAL; | |
1654 | } | |
1655 | } else { | |
1656 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
1657 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) { | |
1658 | verbose("BPF_END uses reserved fields\n"); | |
1659 | return -EINVAL; | |
1660 | } | |
1661 | } | |
1662 | ||
1663 | /* check src operand */ | |
1664 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1665 | if (err) | |
1666 | return err; | |
1667 | ||
1be7f75d AS |
1668 | if (is_pointer_value(env, insn->dst_reg)) { |
1669 | verbose("R%d pointer arithmetic prohibited\n", | |
1670 | insn->dst_reg); | |
1671 | return -EACCES; | |
1672 | } | |
1673 | ||
17a52670 AS |
1674 | /* check dest operand */ |
1675 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1676 | if (err) | |
1677 | return err; | |
1678 | ||
1679 | } else if (opcode == BPF_MOV) { | |
1680 | ||
1681 | if (BPF_SRC(insn->code) == BPF_X) { | |
1682 | if (insn->imm != 0 || insn->off != 0) { | |
1683 | verbose("BPF_MOV uses reserved fields\n"); | |
1684 | return -EINVAL; | |
1685 | } | |
1686 | ||
1687 | /* check src operand */ | |
1688 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1689 | if (err) | |
1690 | return err; | |
1691 | } else { | |
1692 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
1693 | verbose("BPF_MOV uses reserved fields\n"); | |
1694 | return -EINVAL; | |
1695 | } | |
1696 | } | |
1697 | ||
1698 | /* check dest operand */ | |
1699 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
1700 | if (err) | |
1701 | return err; | |
1702 | ||
48461135 JB |
1703 | /* we are setting our register to something new, we need to |
1704 | * reset its range values. | |
1705 | */ | |
1706 | reset_reg_range_values(regs, insn->dst_reg); | |
1707 | ||
17a52670 AS |
1708 | if (BPF_SRC(insn->code) == BPF_X) { |
1709 | if (BPF_CLASS(insn->code) == BPF_ALU64) { | |
1710 | /* case: R1 = R2 | |
1711 | * copy register state to dest reg | |
1712 | */ | |
1713 | regs[insn->dst_reg] = regs[insn->src_reg]; | |
1714 | } else { | |
1be7f75d AS |
1715 | if (is_pointer_value(env, insn->src_reg)) { |
1716 | verbose("R%d partial copy of pointer\n", | |
1717 | insn->src_reg); | |
1718 | return -EACCES; | |
1719 | } | |
57a09bf0 | 1720 | mark_reg_unknown_value(regs, insn->dst_reg); |
17a52670 AS |
1721 | } |
1722 | } else { | |
1723 | /* case: R = imm | |
1724 | * remember the value we stored into this reg | |
1725 | */ | |
1726 | regs[insn->dst_reg].type = CONST_IMM; | |
1727 | regs[insn->dst_reg].imm = insn->imm; | |
48461135 JB |
1728 | regs[insn->dst_reg].max_value = insn->imm; |
1729 | regs[insn->dst_reg].min_value = insn->imm; | |
17a52670 AS |
1730 | } |
1731 | ||
1732 | } else if (opcode > BPF_END) { | |
1733 | verbose("invalid BPF_ALU opcode %x\n", opcode); | |
1734 | return -EINVAL; | |
1735 | ||
1736 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
1737 | ||
17a52670 AS |
1738 | if (BPF_SRC(insn->code) == BPF_X) { |
1739 | if (insn->imm != 0 || insn->off != 0) { | |
1740 | verbose("BPF_ALU uses reserved fields\n"); | |
1741 | return -EINVAL; | |
1742 | } | |
1743 | /* check src1 operand */ | |
1744 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
1745 | if (err) | |
1746 | return err; | |
1747 | } else { | |
1748 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
1749 | verbose("BPF_ALU uses reserved fields\n"); | |
1750 | return -EINVAL; | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | /* check src2 operand */ | |
1755 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
1756 | if (err) | |
1757 | return err; | |
1758 | ||
1759 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
1760 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
1761 | verbose("div by zero\n"); | |
1762 | return -EINVAL; | |
1763 | } | |
1764 | ||
229394e8 RV |
1765 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
1766 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
1767 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
1768 | ||
1769 | if (insn->imm < 0 || insn->imm >= size) { | |
1770 | verbose("invalid shift %d\n", insn->imm); | |
1771 | return -EINVAL; | |
1772 | } | |
1773 | } | |
1774 | ||
1a0dc1ac AS |
1775 | /* check dest operand */ |
1776 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); | |
1777 | if (err) | |
1778 | return err; | |
1779 | ||
1780 | dst_reg = ®s[insn->dst_reg]; | |
1781 | ||
48461135 JB |
1782 | /* first we want to adjust our ranges. */ |
1783 | adjust_reg_min_max_vals(env, insn); | |
1784 | ||
17a52670 AS |
1785 | /* pattern match 'bpf_add Rx, imm' instruction */ |
1786 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && | |
1a0dc1ac AS |
1787 | dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) { |
1788 | dst_reg->type = PTR_TO_STACK; | |
1789 | dst_reg->imm = insn->imm; | |
1790 | return 0; | |
969bf05e AS |
1791 | } else if (opcode == BPF_ADD && |
1792 | BPF_CLASS(insn->code) == BPF_ALU64 && | |
fe390aa5 YS |
1793 | dst_reg->type == PTR_TO_STACK && |
1794 | ((BPF_SRC(insn->code) == BPF_X && | |
1795 | regs[insn->src_reg].type == CONST_IMM) || | |
1796 | BPF_SRC(insn->code) == BPF_K)) { | |
1797 | if (BPF_SRC(insn->code) == BPF_X) | |
1798 | dst_reg->imm += regs[insn->src_reg].imm; | |
1799 | else | |
1800 | dst_reg->imm += insn->imm; | |
1801 | return 0; | |
1802 | } else if (opcode == BPF_ADD && | |
1803 | BPF_CLASS(insn->code) == BPF_ALU64 && | |
1b9b69ec AS |
1804 | (dst_reg->type == PTR_TO_PACKET || |
1805 | (BPF_SRC(insn->code) == BPF_X && | |
1806 | regs[insn->src_reg].type == PTR_TO_PACKET))) { | |
969bf05e AS |
1807 | /* ptr_to_packet += K|X */ |
1808 | return check_packet_ptr_add(env, insn); | |
1809 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && | |
1810 | dst_reg->type == UNKNOWN_VALUE && | |
1811 | env->allow_ptr_leaks) { | |
1812 | /* unknown += K|X */ | |
1813 | return evaluate_reg_alu(env, insn); | |
1814 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && | |
1815 | dst_reg->type == CONST_IMM && | |
1816 | env->allow_ptr_leaks) { | |
1817 | /* reg_imm += K|X */ | |
1818 | return evaluate_reg_imm_alu(env, insn); | |
1be7f75d AS |
1819 | } else if (is_pointer_value(env, insn->dst_reg)) { |
1820 | verbose("R%d pointer arithmetic prohibited\n", | |
1821 | insn->dst_reg); | |
1822 | return -EACCES; | |
1823 | } else if (BPF_SRC(insn->code) == BPF_X && | |
1824 | is_pointer_value(env, insn->src_reg)) { | |
1825 | verbose("R%d pointer arithmetic prohibited\n", | |
1826 | insn->src_reg); | |
1827 | return -EACCES; | |
1828 | } | |
17a52670 | 1829 | |
48461135 JB |
1830 | /* If we did pointer math on a map value then just set it to our |
1831 | * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or | |
1832 | * loads to this register appropriately, otherwise just mark the | |
1833 | * register as unknown. | |
1834 | */ | |
1835 | if (env->allow_ptr_leaks && | |
1836 | (dst_reg->type == PTR_TO_MAP_VALUE || | |
1837 | dst_reg->type == PTR_TO_MAP_VALUE_ADJ)) | |
1838 | dst_reg->type = PTR_TO_MAP_VALUE_ADJ; | |
1839 | else | |
1840 | mark_reg_unknown_value(regs, insn->dst_reg); | |
17a52670 AS |
1841 | } |
1842 | ||
1843 | return 0; | |
1844 | } | |
1845 | ||
58e2af8b JK |
1846 | static void find_good_pkt_pointers(struct bpf_verifier_state *state, |
1847 | struct bpf_reg_state *dst_reg) | |
969bf05e | 1848 | { |
58e2af8b | 1849 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e | 1850 | int i; |
2d2be8ca DB |
1851 | |
1852 | /* LLVM can generate two kind of checks: | |
1853 | * | |
1854 | * Type 1: | |
1855 | * | |
1856 | * r2 = r3; | |
1857 | * r2 += 8; | |
1858 | * if (r2 > pkt_end) goto <handle exception> | |
1859 | * <access okay> | |
1860 | * | |
1861 | * Where: | |
1862 | * r2 == dst_reg, pkt_end == src_reg | |
1863 | * r2=pkt(id=n,off=8,r=0) | |
1864 | * r3=pkt(id=n,off=0,r=0) | |
1865 | * | |
1866 | * Type 2: | |
1867 | * | |
1868 | * r2 = r3; | |
1869 | * r2 += 8; | |
1870 | * if (pkt_end >= r2) goto <access okay> | |
1871 | * <handle exception> | |
1872 | * | |
1873 | * Where: | |
1874 | * pkt_end == dst_reg, r2 == src_reg | |
1875 | * r2=pkt(id=n,off=8,r=0) | |
1876 | * r3=pkt(id=n,off=0,r=0) | |
1877 | * | |
1878 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
1879 | * so that range of bytes [r3, r3 + 8) is safe to access. | |
969bf05e | 1880 | */ |
2d2be8ca | 1881 | |
969bf05e AS |
1882 | for (i = 0; i < MAX_BPF_REG; i++) |
1883 | if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id) | |
ff6caf60 AS |
1884 | /* keep the maximum range already checked */ |
1885 | regs[i].range = max(regs[i].range, dst_reg->off); | |
969bf05e AS |
1886 | |
1887 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { | |
1888 | if (state->stack_slot_type[i] != STACK_SPILL) | |
1889 | continue; | |
1890 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; | |
1891 | if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id) | |
ff6caf60 | 1892 | reg->range = max(reg->range, dst_reg->off); |
969bf05e AS |
1893 | } |
1894 | } | |
1895 | ||
48461135 JB |
1896 | /* Adjusts the register min/max values in the case that the dst_reg is the |
1897 | * variable register that we are working on, and src_reg is a constant or we're | |
1898 | * simply doing a BPF_K check. | |
1899 | */ | |
1900 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
1901 | struct bpf_reg_state *false_reg, u64 val, | |
1902 | u8 opcode) | |
1903 | { | |
1904 | switch (opcode) { | |
1905 | case BPF_JEQ: | |
1906 | /* If this is false then we know nothing Jon Snow, but if it is | |
1907 | * true then we know for sure. | |
1908 | */ | |
1909 | true_reg->max_value = true_reg->min_value = val; | |
1910 | break; | |
1911 | case BPF_JNE: | |
1912 | /* If this is true we know nothing Jon Snow, but if it is false | |
1913 | * we know the value for sure; | |
1914 | */ | |
1915 | false_reg->max_value = false_reg->min_value = val; | |
1916 | break; | |
1917 | case BPF_JGT: | |
1918 | /* Unsigned comparison, the minimum value is 0. */ | |
1919 | false_reg->min_value = 0; | |
1920 | case BPF_JSGT: | |
1921 | /* If this is false then we know the maximum val is val, | |
1922 | * otherwise we know the min val is val+1. | |
1923 | */ | |
1924 | false_reg->max_value = val; | |
1925 | true_reg->min_value = val + 1; | |
1926 | break; | |
1927 | case BPF_JGE: | |
1928 | /* Unsigned comparison, the minimum value is 0. */ | |
1929 | false_reg->min_value = 0; | |
1930 | case BPF_JSGE: | |
1931 | /* If this is false then we know the maximum value is val - 1, | |
1932 | * otherwise we know the mimimum value is val. | |
1933 | */ | |
1934 | false_reg->max_value = val - 1; | |
1935 | true_reg->min_value = val; | |
1936 | break; | |
1937 | default: | |
1938 | break; | |
1939 | } | |
1940 | ||
1941 | check_reg_overflow(false_reg); | |
1942 | check_reg_overflow(true_reg); | |
1943 | } | |
1944 | ||
1945 | /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg | |
1946 | * is the variable reg. | |
1947 | */ | |
1948 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
1949 | struct bpf_reg_state *false_reg, u64 val, | |
1950 | u8 opcode) | |
1951 | { | |
1952 | switch (opcode) { | |
1953 | case BPF_JEQ: | |
1954 | /* If this is false then we know nothing Jon Snow, but if it is | |
1955 | * true then we know for sure. | |
1956 | */ | |
1957 | true_reg->max_value = true_reg->min_value = val; | |
1958 | break; | |
1959 | case BPF_JNE: | |
1960 | /* If this is true we know nothing Jon Snow, but if it is false | |
1961 | * we know the value for sure; | |
1962 | */ | |
1963 | false_reg->max_value = false_reg->min_value = val; | |
1964 | break; | |
1965 | case BPF_JGT: | |
1966 | /* Unsigned comparison, the minimum value is 0. */ | |
1967 | true_reg->min_value = 0; | |
1968 | case BPF_JSGT: | |
1969 | /* | |
1970 | * If this is false, then the val is <= the register, if it is | |
1971 | * true the register <= to the val. | |
1972 | */ | |
1973 | false_reg->min_value = val; | |
1974 | true_reg->max_value = val - 1; | |
1975 | break; | |
1976 | case BPF_JGE: | |
1977 | /* Unsigned comparison, the minimum value is 0. */ | |
1978 | true_reg->min_value = 0; | |
1979 | case BPF_JSGE: | |
1980 | /* If this is false then constant < register, if it is true then | |
1981 | * the register < constant. | |
1982 | */ | |
1983 | false_reg->min_value = val + 1; | |
1984 | true_reg->max_value = val; | |
1985 | break; | |
1986 | default: | |
1987 | break; | |
1988 | } | |
1989 | ||
1990 | check_reg_overflow(false_reg); | |
1991 | check_reg_overflow(true_reg); | |
1992 | } | |
1993 | ||
57a09bf0 TG |
1994 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
1995 | enum bpf_reg_type type) | |
1996 | { | |
1997 | struct bpf_reg_state *reg = ®s[regno]; | |
1998 | ||
1999 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { | |
2000 | reg->type = type; | |
a08dd0da DB |
2001 | /* We don't need id from this point onwards anymore, thus we |
2002 | * should better reset it, so that state pruning has chances | |
2003 | * to take effect. | |
2004 | */ | |
2005 | reg->id = 0; | |
57a09bf0 | 2006 | if (type == UNKNOWN_VALUE) |
6760bf2d | 2007 | __mark_reg_unknown_value(regs, regno); |
57a09bf0 TG |
2008 | } |
2009 | } | |
2010 | ||
2011 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
2012 | * be folded together at some point. | |
2013 | */ | |
2014 | static void mark_map_regs(struct bpf_verifier_state *state, u32 regno, | |
2015 | enum bpf_reg_type type) | |
2016 | { | |
2017 | struct bpf_reg_state *regs = state->regs; | |
a08dd0da | 2018 | u32 id = regs[regno].id; |
57a09bf0 TG |
2019 | int i; |
2020 | ||
2021 | for (i = 0; i < MAX_BPF_REG; i++) | |
a08dd0da | 2022 | mark_map_reg(regs, i, id, type); |
57a09bf0 TG |
2023 | |
2024 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { | |
2025 | if (state->stack_slot_type[i] != STACK_SPILL) | |
2026 | continue; | |
a08dd0da | 2027 | mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type); |
57a09bf0 TG |
2028 | } |
2029 | } | |
2030 | ||
58e2af8b | 2031 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
2032 | struct bpf_insn *insn, int *insn_idx) |
2033 | { | |
58e2af8b JK |
2034 | struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state; |
2035 | struct bpf_reg_state *regs = this_branch->regs, *dst_reg; | |
17a52670 AS |
2036 | u8 opcode = BPF_OP(insn->code); |
2037 | int err; | |
2038 | ||
2039 | if (opcode > BPF_EXIT) { | |
2040 | verbose("invalid BPF_JMP opcode %x\n", opcode); | |
2041 | return -EINVAL; | |
2042 | } | |
2043 | ||
2044 | if (BPF_SRC(insn->code) == BPF_X) { | |
2045 | if (insn->imm != 0) { | |
2046 | verbose("BPF_JMP uses reserved fields\n"); | |
2047 | return -EINVAL; | |
2048 | } | |
2049 | ||
2050 | /* check src1 operand */ | |
2051 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2052 | if (err) | |
2053 | return err; | |
1be7f75d AS |
2054 | |
2055 | if (is_pointer_value(env, insn->src_reg)) { | |
2056 | verbose("R%d pointer comparison prohibited\n", | |
2057 | insn->src_reg); | |
2058 | return -EACCES; | |
2059 | } | |
17a52670 AS |
2060 | } else { |
2061 | if (insn->src_reg != BPF_REG_0) { | |
2062 | verbose("BPF_JMP uses reserved fields\n"); | |
2063 | return -EINVAL; | |
2064 | } | |
2065 | } | |
2066 | ||
2067 | /* check src2 operand */ | |
2068 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
2069 | if (err) | |
2070 | return err; | |
2071 | ||
1a0dc1ac AS |
2072 | dst_reg = ®s[insn->dst_reg]; |
2073 | ||
17a52670 AS |
2074 | /* detect if R == 0 where R was initialized to zero earlier */ |
2075 | if (BPF_SRC(insn->code) == BPF_K && | |
2076 | (opcode == BPF_JEQ || opcode == BPF_JNE) && | |
1a0dc1ac | 2077 | dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) { |
17a52670 AS |
2078 | if (opcode == BPF_JEQ) { |
2079 | /* if (imm == imm) goto pc+off; | |
2080 | * only follow the goto, ignore fall-through | |
2081 | */ | |
2082 | *insn_idx += insn->off; | |
2083 | return 0; | |
2084 | } else { | |
2085 | /* if (imm != imm) goto pc+off; | |
2086 | * only follow fall-through branch, since | |
2087 | * that's where the program will go | |
2088 | */ | |
2089 | return 0; | |
2090 | } | |
2091 | } | |
2092 | ||
2093 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); | |
2094 | if (!other_branch) | |
2095 | return -EFAULT; | |
2096 | ||
48461135 JB |
2097 | /* detect if we are comparing against a constant value so we can adjust |
2098 | * our min/max values for our dst register. | |
2099 | */ | |
2100 | if (BPF_SRC(insn->code) == BPF_X) { | |
2101 | if (regs[insn->src_reg].type == CONST_IMM) | |
2102 | reg_set_min_max(&other_branch->regs[insn->dst_reg], | |
2103 | dst_reg, regs[insn->src_reg].imm, | |
2104 | opcode); | |
2105 | else if (dst_reg->type == CONST_IMM) | |
2106 | reg_set_min_max_inv(&other_branch->regs[insn->src_reg], | |
2107 | ®s[insn->src_reg], dst_reg->imm, | |
2108 | opcode); | |
2109 | } else { | |
2110 | reg_set_min_max(&other_branch->regs[insn->dst_reg], | |
2111 | dst_reg, insn->imm, opcode); | |
2112 | } | |
2113 | ||
58e2af8b | 2114 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */ |
17a52670 | 2115 | if (BPF_SRC(insn->code) == BPF_K && |
1a0dc1ac AS |
2116 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
2117 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { | |
57a09bf0 TG |
2118 | /* Mark all identical map registers in each branch as either |
2119 | * safe or unknown depending R == 0 or R != 0 conditional. | |
2120 | */ | |
2121 | mark_map_regs(this_branch, insn->dst_reg, | |
2122 | opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE); | |
2123 | mark_map_regs(other_branch, insn->dst_reg, | |
2124 | opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE); | |
969bf05e AS |
2125 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && |
2126 | dst_reg->type == PTR_TO_PACKET && | |
2127 | regs[insn->src_reg].type == PTR_TO_PACKET_END) { | |
2d2be8ca DB |
2128 | find_good_pkt_pointers(this_branch, dst_reg); |
2129 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE && | |
2130 | dst_reg->type == PTR_TO_PACKET_END && | |
2131 | regs[insn->src_reg].type == PTR_TO_PACKET) { | |
2132 | find_good_pkt_pointers(other_branch, ®s[insn->src_reg]); | |
1be7f75d AS |
2133 | } else if (is_pointer_value(env, insn->dst_reg)) { |
2134 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); | |
2135 | return -EACCES; | |
17a52670 AS |
2136 | } |
2137 | if (log_level) | |
2d2be8ca | 2138 | print_verifier_state(this_branch); |
17a52670 AS |
2139 | return 0; |
2140 | } | |
2141 | ||
0246e64d AS |
2142 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
2143 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) | |
2144 | { | |
2145 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; | |
2146 | ||
2147 | return (struct bpf_map *) (unsigned long) imm64; | |
2148 | } | |
2149 | ||
17a52670 | 2150 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 2151 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 2152 | { |
58e2af8b | 2153 | struct bpf_reg_state *regs = env->cur_state.regs; |
17a52670 AS |
2154 | int err; |
2155 | ||
2156 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
2157 | verbose("invalid BPF_LD_IMM insn\n"); | |
2158 | return -EINVAL; | |
2159 | } | |
2160 | if (insn->off != 0) { | |
2161 | verbose("BPF_LD_IMM64 uses reserved fields\n"); | |
2162 | return -EINVAL; | |
2163 | } | |
2164 | ||
2165 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); | |
2166 | if (err) | |
2167 | return err; | |
2168 | ||
6b173873 JK |
2169 | if (insn->src_reg == 0) { |
2170 | /* generic move 64-bit immediate into a register, | |
2171 | * only analyzer needs to collect the ld_imm value. | |
2172 | */ | |
2173 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; | |
2174 | ||
2175 | if (!env->analyzer_ops) | |
2176 | return 0; | |
2177 | ||
2178 | regs[insn->dst_reg].type = CONST_IMM; | |
2179 | regs[insn->dst_reg].imm = imm; | |
17a52670 | 2180 | return 0; |
6b173873 | 2181 | } |
17a52670 AS |
2182 | |
2183 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ | |
2184 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); | |
2185 | ||
2186 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
2187 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); | |
2188 | return 0; | |
2189 | } | |
2190 | ||
96be4325 DB |
2191 | static bool may_access_skb(enum bpf_prog_type type) |
2192 | { | |
2193 | switch (type) { | |
2194 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
2195 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 2196 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
2197 | return true; |
2198 | default: | |
2199 | return false; | |
2200 | } | |
2201 | } | |
2202 | ||
ddd872bc AS |
2203 | /* verify safety of LD_ABS|LD_IND instructions: |
2204 | * - they can only appear in the programs where ctx == skb | |
2205 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
2206 | * preserve R6-R9, and store return value into R0 | |
2207 | * | |
2208 | * Implicit input: | |
2209 | * ctx == skb == R6 == CTX | |
2210 | * | |
2211 | * Explicit input: | |
2212 | * SRC == any register | |
2213 | * IMM == 32-bit immediate | |
2214 | * | |
2215 | * Output: | |
2216 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
2217 | */ | |
58e2af8b | 2218 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 2219 | { |
58e2af8b | 2220 | struct bpf_reg_state *regs = env->cur_state.regs; |
ddd872bc | 2221 | u8 mode = BPF_MODE(insn->code); |
58e2af8b | 2222 | struct bpf_reg_state *reg; |
ddd872bc AS |
2223 | int i, err; |
2224 | ||
24701ece | 2225 | if (!may_access_skb(env->prog->type)) { |
1a0dc1ac | 2226 | verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
2227 | return -EINVAL; |
2228 | } | |
2229 | ||
2230 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || | |
d82bccc6 | 2231 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 2232 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
1a0dc1ac | 2233 | verbose("BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
2234 | return -EINVAL; |
2235 | } | |
2236 | ||
2237 | /* check whether implicit source operand (register R6) is readable */ | |
2238 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); | |
2239 | if (err) | |
2240 | return err; | |
2241 | ||
2242 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { | |
2243 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
2244 | return -EINVAL; | |
2245 | } | |
2246 | ||
2247 | if (mode == BPF_IND) { | |
2248 | /* check explicit source operand */ | |
2249 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2250 | if (err) | |
2251 | return err; | |
2252 | } | |
2253 | ||
2254 | /* reset caller saved regs to unreadable */ | |
2255 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
2256 | reg = regs + caller_saved[i]; | |
2257 | reg->type = NOT_INIT; | |
2258 | reg->imm = 0; | |
2259 | } | |
2260 | ||
2261 | /* mark destination R0 register as readable, since it contains | |
2262 | * the value fetched from the packet | |
2263 | */ | |
2264 | regs[BPF_REG_0].type = UNKNOWN_VALUE; | |
2265 | return 0; | |
2266 | } | |
2267 | ||
475fb78f AS |
2268 | /* non-recursive DFS pseudo code |
2269 | * 1 procedure DFS-iterative(G,v): | |
2270 | * 2 label v as discovered | |
2271 | * 3 let S be a stack | |
2272 | * 4 S.push(v) | |
2273 | * 5 while S is not empty | |
2274 | * 6 t <- S.pop() | |
2275 | * 7 if t is what we're looking for: | |
2276 | * 8 return t | |
2277 | * 9 for all edges e in G.adjacentEdges(t) do | |
2278 | * 10 if edge e is already labelled | |
2279 | * 11 continue with the next edge | |
2280 | * 12 w <- G.adjacentVertex(t,e) | |
2281 | * 13 if vertex w is not discovered and not explored | |
2282 | * 14 label e as tree-edge | |
2283 | * 15 label w as discovered | |
2284 | * 16 S.push(w) | |
2285 | * 17 continue at 5 | |
2286 | * 18 else if vertex w is discovered | |
2287 | * 19 label e as back-edge | |
2288 | * 20 else | |
2289 | * 21 // vertex w is explored | |
2290 | * 22 label e as forward- or cross-edge | |
2291 | * 23 label t as explored | |
2292 | * 24 S.pop() | |
2293 | * | |
2294 | * convention: | |
2295 | * 0x10 - discovered | |
2296 | * 0x11 - discovered and fall-through edge labelled | |
2297 | * 0x12 - discovered and fall-through and branch edges labelled | |
2298 | * 0x20 - explored | |
2299 | */ | |
2300 | ||
2301 | enum { | |
2302 | DISCOVERED = 0x10, | |
2303 | EXPLORED = 0x20, | |
2304 | FALLTHROUGH = 1, | |
2305 | BRANCH = 2, | |
2306 | }; | |
2307 | ||
58e2af8b | 2308 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
f1bca824 | 2309 | |
475fb78f AS |
2310 | static int *insn_stack; /* stack of insns to process */ |
2311 | static int cur_stack; /* current stack index */ | |
2312 | static int *insn_state; | |
2313 | ||
2314 | /* t, w, e - match pseudo-code above: | |
2315 | * t - index of current instruction | |
2316 | * w - next instruction | |
2317 | * e - edge | |
2318 | */ | |
58e2af8b | 2319 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
475fb78f AS |
2320 | { |
2321 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) | |
2322 | return 0; | |
2323 | ||
2324 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
2325 | return 0; | |
2326 | ||
2327 | if (w < 0 || w >= env->prog->len) { | |
2328 | verbose("jump out of range from insn %d to %d\n", t, w); | |
2329 | return -EINVAL; | |
2330 | } | |
2331 | ||
f1bca824 AS |
2332 | if (e == BRANCH) |
2333 | /* mark branch target for state pruning */ | |
2334 | env->explored_states[w] = STATE_LIST_MARK; | |
2335 | ||
475fb78f AS |
2336 | if (insn_state[w] == 0) { |
2337 | /* tree-edge */ | |
2338 | insn_state[t] = DISCOVERED | e; | |
2339 | insn_state[w] = DISCOVERED; | |
2340 | if (cur_stack >= env->prog->len) | |
2341 | return -E2BIG; | |
2342 | insn_stack[cur_stack++] = w; | |
2343 | return 1; | |
2344 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2345 | verbose("back-edge from insn %d to %d\n", t, w); | |
2346 | return -EINVAL; | |
2347 | } else if (insn_state[w] == EXPLORED) { | |
2348 | /* forward- or cross-edge */ | |
2349 | insn_state[t] = DISCOVERED | e; | |
2350 | } else { | |
2351 | verbose("insn state internal bug\n"); | |
2352 | return -EFAULT; | |
2353 | } | |
2354 | return 0; | |
2355 | } | |
2356 | ||
2357 | /* non-recursive depth-first-search to detect loops in BPF program | |
2358 | * loop == back-edge in directed graph | |
2359 | */ | |
58e2af8b | 2360 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
2361 | { |
2362 | struct bpf_insn *insns = env->prog->insnsi; | |
2363 | int insn_cnt = env->prog->len; | |
2364 | int ret = 0; | |
2365 | int i, t; | |
2366 | ||
2367 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
2368 | if (!insn_state) | |
2369 | return -ENOMEM; | |
2370 | ||
2371 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
2372 | if (!insn_stack) { | |
2373 | kfree(insn_state); | |
2374 | return -ENOMEM; | |
2375 | } | |
2376 | ||
2377 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
2378 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
2379 | cur_stack = 1; | |
2380 | ||
2381 | peek_stack: | |
2382 | if (cur_stack == 0) | |
2383 | goto check_state; | |
2384 | t = insn_stack[cur_stack - 1]; | |
2385 | ||
2386 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { | |
2387 | u8 opcode = BPF_OP(insns[t].code); | |
2388 | ||
2389 | if (opcode == BPF_EXIT) { | |
2390 | goto mark_explored; | |
2391 | } else if (opcode == BPF_CALL) { | |
2392 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
2393 | if (ret == 1) | |
2394 | goto peek_stack; | |
2395 | else if (ret < 0) | |
2396 | goto err_free; | |
07016151 DB |
2397 | if (t + 1 < insn_cnt) |
2398 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
2399 | } else if (opcode == BPF_JA) { |
2400 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
2401 | ret = -EINVAL; | |
2402 | goto err_free; | |
2403 | } | |
2404 | /* unconditional jump with single edge */ | |
2405 | ret = push_insn(t, t + insns[t].off + 1, | |
2406 | FALLTHROUGH, env); | |
2407 | if (ret == 1) | |
2408 | goto peek_stack; | |
2409 | else if (ret < 0) | |
2410 | goto err_free; | |
f1bca824 AS |
2411 | /* tell verifier to check for equivalent states |
2412 | * after every call and jump | |
2413 | */ | |
c3de6317 AS |
2414 | if (t + 1 < insn_cnt) |
2415 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
2416 | } else { |
2417 | /* conditional jump with two edges */ | |
2418 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
2419 | if (ret == 1) | |
2420 | goto peek_stack; | |
2421 | else if (ret < 0) | |
2422 | goto err_free; | |
2423 | ||
2424 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
2425 | if (ret == 1) | |
2426 | goto peek_stack; | |
2427 | else if (ret < 0) | |
2428 | goto err_free; | |
2429 | } | |
2430 | } else { | |
2431 | /* all other non-branch instructions with single | |
2432 | * fall-through edge | |
2433 | */ | |
2434 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
2435 | if (ret == 1) | |
2436 | goto peek_stack; | |
2437 | else if (ret < 0) | |
2438 | goto err_free; | |
2439 | } | |
2440 | ||
2441 | mark_explored: | |
2442 | insn_state[t] = EXPLORED; | |
2443 | if (cur_stack-- <= 0) { | |
2444 | verbose("pop stack internal bug\n"); | |
2445 | ret = -EFAULT; | |
2446 | goto err_free; | |
2447 | } | |
2448 | goto peek_stack; | |
2449 | ||
2450 | check_state: | |
2451 | for (i = 0; i < insn_cnt; i++) { | |
2452 | if (insn_state[i] != EXPLORED) { | |
2453 | verbose("unreachable insn %d\n", i); | |
2454 | ret = -EINVAL; | |
2455 | goto err_free; | |
2456 | } | |
2457 | } | |
2458 | ret = 0; /* cfg looks good */ | |
2459 | ||
2460 | err_free: | |
2461 | kfree(insn_state); | |
2462 | kfree(insn_stack); | |
2463 | return ret; | |
2464 | } | |
2465 | ||
969bf05e AS |
2466 | /* the following conditions reduce the number of explored insns |
2467 | * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet | |
2468 | */ | |
58e2af8b JK |
2469 | static bool compare_ptrs_to_packet(struct bpf_reg_state *old, |
2470 | struct bpf_reg_state *cur) | |
969bf05e AS |
2471 | { |
2472 | if (old->id != cur->id) | |
2473 | return false; | |
2474 | ||
2475 | /* old ptr_to_packet is more conservative, since it allows smaller | |
2476 | * range. Ex: | |
2477 | * old(off=0,r=10) is equal to cur(off=0,r=20), because | |
2478 | * old(off=0,r=10) means that with range=10 the verifier proceeded | |
2479 | * further and found no issues with the program. Now we're in the same | |
2480 | * spot with cur(off=0,r=20), so we're safe too, since anything further | |
2481 | * will only be looking at most 10 bytes after this pointer. | |
2482 | */ | |
2483 | if (old->off == cur->off && old->range < cur->range) | |
2484 | return true; | |
2485 | ||
2486 | /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0) | |
2487 | * since both cannot be used for packet access and safe(old) | |
2488 | * pointer has smaller off that could be used for further | |
2489 | * 'if (ptr > data_end)' check | |
2490 | * Ex: | |
2491 | * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean | |
2492 | * that we cannot access the packet. | |
2493 | * The safe range is: | |
2494 | * [ptr, ptr + range - off) | |
2495 | * so whenever off >=range, it means no safe bytes from this pointer. | |
2496 | * When comparing old->off <= cur->off, it means that older code | |
2497 | * went with smaller offset and that offset was later | |
2498 | * used to figure out the safe range after 'if (ptr > data_end)' check | |
2499 | * Say, 'old' state was explored like: | |
2500 | * ... R3(off=0, r=0) | |
2501 | * R4 = R3 + 20 | |
2502 | * ... now R4(off=20,r=0) <-- here | |
2503 | * if (R4 > data_end) | |
2504 | * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access. | |
2505 | * ... the code further went all the way to bpf_exit. | |
2506 | * Now the 'cur' state at the mark 'here' has R4(off=30,r=0). | |
2507 | * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier | |
2508 | * goes further, such cur_R4 will give larger safe packet range after | |
2509 | * 'if (R4 > data_end)' and all further insn were already good with r=20, | |
2510 | * so they will be good with r=30 and we can prune the search. | |
2511 | */ | |
2512 | if (old->off <= cur->off && | |
2513 | old->off >= old->range && cur->off >= cur->range) | |
2514 | return true; | |
2515 | ||
2516 | return false; | |
2517 | } | |
2518 | ||
f1bca824 AS |
2519 | /* compare two verifier states |
2520 | * | |
2521 | * all states stored in state_list are known to be valid, since | |
2522 | * verifier reached 'bpf_exit' instruction through them | |
2523 | * | |
2524 | * this function is called when verifier exploring different branches of | |
2525 | * execution popped from the state stack. If it sees an old state that has | |
2526 | * more strict register state and more strict stack state then this execution | |
2527 | * branch doesn't need to be explored further, since verifier already | |
2528 | * concluded that more strict state leads to valid finish. | |
2529 | * | |
2530 | * Therefore two states are equivalent if register state is more conservative | |
2531 | * and explored stack state is more conservative than the current one. | |
2532 | * Example: | |
2533 | * explored current | |
2534 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
2535 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
2536 | * | |
2537 | * In other words if current stack state (one being explored) has more | |
2538 | * valid slots than old one that already passed validation, it means | |
2539 | * the verifier can stop exploring and conclude that current state is valid too | |
2540 | * | |
2541 | * Similarly with registers. If explored state has register type as invalid | |
2542 | * whereas register type in current state is meaningful, it means that | |
2543 | * the current state will reach 'bpf_exit' instruction safely | |
2544 | */ | |
48461135 JB |
2545 | static bool states_equal(struct bpf_verifier_env *env, |
2546 | struct bpf_verifier_state *old, | |
58e2af8b | 2547 | struct bpf_verifier_state *cur) |
f1bca824 | 2548 | { |
e2d2afe1 | 2549 | bool varlen_map_access = env->varlen_map_value_access; |
58e2af8b | 2550 | struct bpf_reg_state *rold, *rcur; |
f1bca824 AS |
2551 | int i; |
2552 | ||
2553 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1a0dc1ac AS |
2554 | rold = &old->regs[i]; |
2555 | rcur = &cur->regs[i]; | |
2556 | ||
2557 | if (memcmp(rold, rcur, sizeof(*rold)) == 0) | |
2558 | continue; | |
2559 | ||
48461135 JB |
2560 | /* If the ranges were not the same, but everything else was and |
2561 | * we didn't do a variable access into a map then we are a-ok. | |
2562 | */ | |
e2d2afe1 | 2563 | if (!varlen_map_access && |
d2a4dd37 | 2564 | memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0) |
48461135 JB |
2565 | continue; |
2566 | ||
e2d2afe1 JB |
2567 | /* If we didn't map access then again we don't care about the |
2568 | * mismatched range values and it's ok if our old type was | |
2569 | * UNKNOWN and we didn't go to a NOT_INIT'ed reg. | |
2570 | */ | |
1a0dc1ac | 2571 | if (rold->type == NOT_INIT || |
e2d2afe1 JB |
2572 | (!varlen_map_access && rold->type == UNKNOWN_VALUE && |
2573 | rcur->type != NOT_INIT)) | |
1a0dc1ac AS |
2574 | continue; |
2575 | ||
969bf05e AS |
2576 | if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET && |
2577 | compare_ptrs_to_packet(rold, rcur)) | |
2578 | continue; | |
2579 | ||
1a0dc1ac | 2580 | return false; |
f1bca824 AS |
2581 | } |
2582 | ||
2583 | for (i = 0; i < MAX_BPF_STACK; i++) { | |
9c399760 AS |
2584 | if (old->stack_slot_type[i] == STACK_INVALID) |
2585 | continue; | |
2586 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) | |
2587 | /* Ex: old explored (safe) state has STACK_SPILL in | |
2588 | * this stack slot, but current has has STACK_MISC -> | |
2589 | * this verifier states are not equivalent, | |
2590 | * return false to continue verification of this path | |
2591 | */ | |
f1bca824 | 2592 | return false; |
9c399760 AS |
2593 | if (i % BPF_REG_SIZE) |
2594 | continue; | |
2595 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], | |
2596 | &cur->spilled_regs[i / BPF_REG_SIZE], | |
2597 | sizeof(old->spilled_regs[0]))) | |
2598 | /* when explored and current stack slot types are | |
2599 | * the same, check that stored pointers types | |
2600 | * are the same as well. | |
2601 | * Ex: explored safe path could have stored | |
58e2af8b | 2602 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8} |
9c399760 | 2603 | * but current path has stored: |
58e2af8b | 2604 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16} |
9c399760 AS |
2605 | * such verifier states are not equivalent. |
2606 | * return false to continue verification of this path | |
2607 | */ | |
2608 | return false; | |
2609 | else | |
2610 | continue; | |
f1bca824 AS |
2611 | } |
2612 | return true; | |
2613 | } | |
2614 | ||
58e2af8b | 2615 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 2616 | { |
58e2af8b JK |
2617 | struct bpf_verifier_state_list *new_sl; |
2618 | struct bpf_verifier_state_list *sl; | |
f1bca824 AS |
2619 | |
2620 | sl = env->explored_states[insn_idx]; | |
2621 | if (!sl) | |
2622 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
2623 | * be doing state search here | |
2624 | */ | |
2625 | return 0; | |
2626 | ||
2627 | while (sl != STATE_LIST_MARK) { | |
48461135 | 2628 | if (states_equal(env, &sl->state, &env->cur_state)) |
f1bca824 AS |
2629 | /* reached equivalent register/stack state, |
2630 | * prune the search | |
2631 | */ | |
2632 | return 1; | |
2633 | sl = sl->next; | |
2634 | } | |
2635 | ||
2636 | /* there were no equivalent states, remember current one. | |
2637 | * technically the current state is not proven to be safe yet, | |
2638 | * but it will either reach bpf_exit (which means it's safe) or | |
2639 | * it will be rejected. Since there are no loops, we won't be | |
2640 | * seeing this 'insn_idx' instruction again on the way to bpf_exit | |
2641 | */ | |
58e2af8b | 2642 | new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER); |
f1bca824 AS |
2643 | if (!new_sl) |
2644 | return -ENOMEM; | |
2645 | ||
2646 | /* add new state to the head of linked list */ | |
2647 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); | |
2648 | new_sl->next = env->explored_states[insn_idx]; | |
2649 | env->explored_states[insn_idx] = new_sl; | |
2650 | return 0; | |
2651 | } | |
2652 | ||
13a27dfc JK |
2653 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
2654 | int insn_idx, int prev_insn_idx) | |
2655 | { | |
2656 | if (!env->analyzer_ops || !env->analyzer_ops->insn_hook) | |
2657 | return 0; | |
2658 | ||
2659 | return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx); | |
2660 | } | |
2661 | ||
58e2af8b | 2662 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 2663 | { |
58e2af8b | 2664 | struct bpf_verifier_state *state = &env->cur_state; |
17a52670 | 2665 | struct bpf_insn *insns = env->prog->insnsi; |
58e2af8b | 2666 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
2667 | int insn_cnt = env->prog->len; |
2668 | int insn_idx, prev_insn_idx = 0; | |
2669 | int insn_processed = 0; | |
2670 | bool do_print_state = false; | |
2671 | ||
2672 | init_reg_state(regs); | |
2673 | insn_idx = 0; | |
48461135 | 2674 | env->varlen_map_value_access = false; |
17a52670 AS |
2675 | for (;;) { |
2676 | struct bpf_insn *insn; | |
2677 | u8 class; | |
2678 | int err; | |
2679 | ||
2680 | if (insn_idx >= insn_cnt) { | |
2681 | verbose("invalid insn idx %d insn_cnt %d\n", | |
2682 | insn_idx, insn_cnt); | |
2683 | return -EFAULT; | |
2684 | } | |
2685 | ||
2686 | insn = &insns[insn_idx]; | |
2687 | class = BPF_CLASS(insn->code); | |
2688 | ||
07016151 | 2689 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
17a52670 AS |
2690 | verbose("BPF program is too large. Proccessed %d insn\n", |
2691 | insn_processed); | |
2692 | return -E2BIG; | |
2693 | } | |
2694 | ||
f1bca824 AS |
2695 | err = is_state_visited(env, insn_idx); |
2696 | if (err < 0) | |
2697 | return err; | |
2698 | if (err == 1) { | |
2699 | /* found equivalent state, can prune the search */ | |
2700 | if (log_level) { | |
2701 | if (do_print_state) | |
2702 | verbose("\nfrom %d to %d: safe\n", | |
2703 | prev_insn_idx, insn_idx); | |
2704 | else | |
2705 | verbose("%d: safe\n", insn_idx); | |
2706 | } | |
2707 | goto process_bpf_exit; | |
2708 | } | |
2709 | ||
17a52670 AS |
2710 | if (log_level && do_print_state) { |
2711 | verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx); | |
1a0dc1ac | 2712 | print_verifier_state(&env->cur_state); |
17a52670 AS |
2713 | do_print_state = false; |
2714 | } | |
2715 | ||
2716 | if (log_level) { | |
2717 | verbose("%d: ", insn_idx); | |
badebe9c | 2718 | print_bpf_insn(env, insn); |
17a52670 AS |
2719 | } |
2720 | ||
13a27dfc JK |
2721 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
2722 | if (err) | |
2723 | return err; | |
2724 | ||
17a52670 | 2725 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 2726 | err = check_alu_op(env, insn); |
17a52670 AS |
2727 | if (err) |
2728 | return err; | |
2729 | ||
2730 | } else if (class == BPF_LDX) { | |
3df126f3 | 2731 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
2732 | |
2733 | /* check for reserved fields is already done */ | |
2734 | ||
17a52670 AS |
2735 | /* check src operand */ |
2736 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2737 | if (err) | |
2738 | return err; | |
2739 | ||
2740 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); | |
2741 | if (err) | |
2742 | return err; | |
2743 | ||
725f9dcd AS |
2744 | src_reg_type = regs[insn->src_reg].type; |
2745 | ||
17a52670 AS |
2746 | /* check that memory (src_reg + off) is readable, |
2747 | * the state of dst_reg will be updated by this func | |
2748 | */ | |
2749 | err = check_mem_access(env, insn->src_reg, insn->off, | |
2750 | BPF_SIZE(insn->code), BPF_READ, | |
2751 | insn->dst_reg); | |
2752 | if (err) | |
2753 | return err; | |
2754 | ||
48461135 | 2755 | reset_reg_range_values(regs, insn->dst_reg); |
ea2e7ce5 AS |
2756 | if (BPF_SIZE(insn->code) != BPF_W && |
2757 | BPF_SIZE(insn->code) != BPF_DW) { | |
725f9dcd AS |
2758 | insn_idx++; |
2759 | continue; | |
2760 | } | |
9bac3d6d | 2761 | |
3df126f3 JK |
2762 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
2763 | ||
2764 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
2765 | /* saw a valid insn |
2766 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 2767 | * save type to validate intersecting paths |
9bac3d6d | 2768 | */ |
3df126f3 | 2769 | *prev_src_type = src_reg_type; |
9bac3d6d | 2770 | |
3df126f3 | 2771 | } else if (src_reg_type != *prev_src_type && |
9bac3d6d | 2772 | (src_reg_type == PTR_TO_CTX || |
3df126f3 | 2773 | *prev_src_type == PTR_TO_CTX)) { |
9bac3d6d AS |
2774 | /* ABuser program is trying to use the same insn |
2775 | * dst_reg = *(u32*) (src_reg + off) | |
2776 | * with different pointer types: | |
2777 | * src_reg == ctx in one branch and | |
2778 | * src_reg == stack|map in some other branch. | |
2779 | * Reject it. | |
2780 | */ | |
2781 | verbose("same insn cannot be used with different pointers\n"); | |
2782 | return -EINVAL; | |
2783 | } | |
2784 | ||
17a52670 | 2785 | } else if (class == BPF_STX) { |
3df126f3 | 2786 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 2787 | |
17a52670 AS |
2788 | if (BPF_MODE(insn->code) == BPF_XADD) { |
2789 | err = check_xadd(env, insn); | |
2790 | if (err) | |
2791 | return err; | |
2792 | insn_idx++; | |
2793 | continue; | |
2794 | } | |
2795 | ||
17a52670 AS |
2796 | /* check src1 operand */ |
2797 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); | |
2798 | if (err) | |
2799 | return err; | |
2800 | /* check src2 operand */ | |
2801 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
2802 | if (err) | |
2803 | return err; | |
2804 | ||
d691f9e8 AS |
2805 | dst_reg_type = regs[insn->dst_reg].type; |
2806 | ||
17a52670 AS |
2807 | /* check that memory (dst_reg + off) is writeable */ |
2808 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
2809 | BPF_SIZE(insn->code), BPF_WRITE, | |
2810 | insn->src_reg); | |
2811 | if (err) | |
2812 | return err; | |
2813 | ||
3df126f3 JK |
2814 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
2815 | ||
2816 | if (*prev_dst_type == NOT_INIT) { | |
2817 | *prev_dst_type = dst_reg_type; | |
2818 | } else if (dst_reg_type != *prev_dst_type && | |
d691f9e8 | 2819 | (dst_reg_type == PTR_TO_CTX || |
3df126f3 | 2820 | *prev_dst_type == PTR_TO_CTX)) { |
d691f9e8 AS |
2821 | verbose("same insn cannot be used with different pointers\n"); |
2822 | return -EINVAL; | |
2823 | } | |
2824 | ||
17a52670 AS |
2825 | } else if (class == BPF_ST) { |
2826 | if (BPF_MODE(insn->code) != BPF_MEM || | |
2827 | insn->src_reg != BPF_REG_0) { | |
2828 | verbose("BPF_ST uses reserved fields\n"); | |
2829 | return -EINVAL; | |
2830 | } | |
2831 | /* check src operand */ | |
2832 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); | |
2833 | if (err) | |
2834 | return err; | |
2835 | ||
2836 | /* check that memory (dst_reg + off) is writeable */ | |
2837 | err = check_mem_access(env, insn->dst_reg, insn->off, | |
2838 | BPF_SIZE(insn->code), BPF_WRITE, | |
2839 | -1); | |
2840 | if (err) | |
2841 | return err; | |
2842 | ||
2843 | } else if (class == BPF_JMP) { | |
2844 | u8 opcode = BPF_OP(insn->code); | |
2845 | ||
2846 | if (opcode == BPF_CALL) { | |
2847 | if (BPF_SRC(insn->code) != BPF_K || | |
2848 | insn->off != 0 || | |
2849 | insn->src_reg != BPF_REG_0 || | |
2850 | insn->dst_reg != BPF_REG_0) { | |
2851 | verbose("BPF_CALL uses reserved fields\n"); | |
2852 | return -EINVAL; | |
2853 | } | |
2854 | ||
2855 | err = check_call(env, insn->imm); | |
2856 | if (err) | |
2857 | return err; | |
2858 | ||
2859 | } else if (opcode == BPF_JA) { | |
2860 | if (BPF_SRC(insn->code) != BPF_K || | |
2861 | insn->imm != 0 || | |
2862 | insn->src_reg != BPF_REG_0 || | |
2863 | insn->dst_reg != BPF_REG_0) { | |
2864 | verbose("BPF_JA uses reserved fields\n"); | |
2865 | return -EINVAL; | |
2866 | } | |
2867 | ||
2868 | insn_idx += insn->off + 1; | |
2869 | continue; | |
2870 | ||
2871 | } else if (opcode == BPF_EXIT) { | |
2872 | if (BPF_SRC(insn->code) != BPF_K || | |
2873 | insn->imm != 0 || | |
2874 | insn->src_reg != BPF_REG_0 || | |
2875 | insn->dst_reg != BPF_REG_0) { | |
2876 | verbose("BPF_EXIT uses reserved fields\n"); | |
2877 | return -EINVAL; | |
2878 | } | |
2879 | ||
2880 | /* eBPF calling convetion is such that R0 is used | |
2881 | * to return the value from eBPF program. | |
2882 | * Make sure that it's readable at this time | |
2883 | * of bpf_exit, which means that program wrote | |
2884 | * something into it earlier | |
2885 | */ | |
2886 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); | |
2887 | if (err) | |
2888 | return err; | |
2889 | ||
1be7f75d AS |
2890 | if (is_pointer_value(env, BPF_REG_0)) { |
2891 | verbose("R0 leaks addr as return value\n"); | |
2892 | return -EACCES; | |
2893 | } | |
2894 | ||
f1bca824 | 2895 | process_bpf_exit: |
17a52670 AS |
2896 | insn_idx = pop_stack(env, &prev_insn_idx); |
2897 | if (insn_idx < 0) { | |
2898 | break; | |
2899 | } else { | |
2900 | do_print_state = true; | |
2901 | continue; | |
2902 | } | |
2903 | } else { | |
2904 | err = check_cond_jmp_op(env, insn, &insn_idx); | |
2905 | if (err) | |
2906 | return err; | |
2907 | } | |
2908 | } else if (class == BPF_LD) { | |
2909 | u8 mode = BPF_MODE(insn->code); | |
2910 | ||
2911 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
2912 | err = check_ld_abs(env, insn); |
2913 | if (err) | |
2914 | return err; | |
2915 | ||
17a52670 AS |
2916 | } else if (mode == BPF_IMM) { |
2917 | err = check_ld_imm(env, insn); | |
2918 | if (err) | |
2919 | return err; | |
2920 | ||
2921 | insn_idx++; | |
2922 | } else { | |
2923 | verbose("invalid BPF_LD mode\n"); | |
2924 | return -EINVAL; | |
2925 | } | |
48461135 | 2926 | reset_reg_range_values(regs, insn->dst_reg); |
17a52670 AS |
2927 | } else { |
2928 | verbose("unknown insn class %d\n", class); | |
2929 | return -EINVAL; | |
2930 | } | |
2931 | ||
2932 | insn_idx++; | |
2933 | } | |
2934 | ||
1a0dc1ac | 2935 | verbose("processed %d insns\n", insn_processed); |
17a52670 AS |
2936 | return 0; |
2937 | } | |
2938 | ||
fdc15d38 AS |
2939 | static int check_map_prog_compatibility(struct bpf_map *map, |
2940 | struct bpf_prog *prog) | |
2941 | ||
2942 | { | |
2943 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT && | |
2944 | (map->map_type == BPF_MAP_TYPE_HASH || | |
2945 | map->map_type == BPF_MAP_TYPE_PERCPU_HASH) && | |
2946 | (map->map_flags & BPF_F_NO_PREALLOC)) { | |
2947 | verbose("perf_event programs can only use preallocated hash map\n"); | |
2948 | return -EINVAL; | |
2949 | } | |
2950 | return 0; | |
2951 | } | |
2952 | ||
0246e64d AS |
2953 | /* look for pseudo eBPF instructions that access map FDs and |
2954 | * replace them with actual map pointers | |
2955 | */ | |
58e2af8b | 2956 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
2957 | { |
2958 | struct bpf_insn *insn = env->prog->insnsi; | |
2959 | int insn_cnt = env->prog->len; | |
fdc15d38 | 2960 | int i, j, err; |
0246e64d | 2961 | |
f1f7714e | 2962 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
2963 | if (err) |
2964 | return err; | |
2965 | ||
0246e64d | 2966 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 2967 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 2968 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
9bac3d6d AS |
2969 | verbose("BPF_LDX uses reserved fields\n"); |
2970 | return -EINVAL; | |
2971 | } | |
2972 | ||
d691f9e8 AS |
2973 | if (BPF_CLASS(insn->code) == BPF_STX && |
2974 | ((BPF_MODE(insn->code) != BPF_MEM && | |
2975 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
2976 | verbose("BPF_STX uses reserved fields\n"); | |
2977 | return -EINVAL; | |
2978 | } | |
2979 | ||
0246e64d AS |
2980 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
2981 | struct bpf_map *map; | |
2982 | struct fd f; | |
2983 | ||
2984 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
2985 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
2986 | insn[1].off != 0) { | |
2987 | verbose("invalid bpf_ld_imm64 insn\n"); | |
2988 | return -EINVAL; | |
2989 | } | |
2990 | ||
2991 | if (insn->src_reg == 0) | |
2992 | /* valid generic load 64-bit imm */ | |
2993 | goto next_insn; | |
2994 | ||
2995 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { | |
2996 | verbose("unrecognized bpf_ld_imm64 insn\n"); | |
2997 | return -EINVAL; | |
2998 | } | |
2999 | ||
3000 | f = fdget(insn->imm); | |
c2101297 | 3001 | map = __bpf_map_get(f); |
0246e64d AS |
3002 | if (IS_ERR(map)) { |
3003 | verbose("fd %d is not pointing to valid bpf_map\n", | |
3004 | insn->imm); | |
0246e64d AS |
3005 | return PTR_ERR(map); |
3006 | } | |
3007 | ||
fdc15d38 AS |
3008 | err = check_map_prog_compatibility(map, env->prog); |
3009 | if (err) { | |
3010 | fdput(f); | |
3011 | return err; | |
3012 | } | |
3013 | ||
0246e64d AS |
3014 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
3015 | insn[0].imm = (u32) (unsigned long) map; | |
3016 | insn[1].imm = ((u64) (unsigned long) map) >> 32; | |
3017 | ||
3018 | /* check whether we recorded this map already */ | |
3019 | for (j = 0; j < env->used_map_cnt; j++) | |
3020 | if (env->used_maps[j] == map) { | |
3021 | fdput(f); | |
3022 | goto next_insn; | |
3023 | } | |
3024 | ||
3025 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
3026 | fdput(f); | |
3027 | return -E2BIG; | |
3028 | } | |
3029 | ||
0246e64d AS |
3030 | /* hold the map. If the program is rejected by verifier, |
3031 | * the map will be released by release_maps() or it | |
3032 | * will be used by the valid program until it's unloaded | |
3033 | * and all maps are released in free_bpf_prog_info() | |
3034 | */ | |
92117d84 AS |
3035 | map = bpf_map_inc(map, false); |
3036 | if (IS_ERR(map)) { | |
3037 | fdput(f); | |
3038 | return PTR_ERR(map); | |
3039 | } | |
3040 | env->used_maps[env->used_map_cnt++] = map; | |
3041 | ||
0246e64d AS |
3042 | fdput(f); |
3043 | next_insn: | |
3044 | insn++; | |
3045 | i++; | |
3046 | } | |
3047 | } | |
3048 | ||
3049 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
3050 | * 'struct bpf_map *' into a register instead of user map_fd. | |
3051 | * These pointers will be used later by verifier to validate map access. | |
3052 | */ | |
3053 | return 0; | |
3054 | } | |
3055 | ||
3056 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 3057 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d AS |
3058 | { |
3059 | int i; | |
3060 | ||
3061 | for (i = 0; i < env->used_map_cnt; i++) | |
3062 | bpf_map_put(env->used_maps[i]); | |
3063 | } | |
3064 | ||
3065 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 3066 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
3067 | { |
3068 | struct bpf_insn *insn = env->prog->insnsi; | |
3069 | int insn_cnt = env->prog->len; | |
3070 | int i; | |
3071 | ||
3072 | for (i = 0; i < insn_cnt; i++, insn++) | |
3073 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
3074 | insn->src_reg = 0; | |
3075 | } | |
3076 | ||
9bac3d6d AS |
3077 | /* convert load instructions that access fields of 'struct __sk_buff' |
3078 | * into sequence of instructions that access fields of 'struct sk_buff' | |
3079 | */ | |
58e2af8b | 3080 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 3081 | { |
36bbef52 | 3082 | const struct bpf_verifier_ops *ops = env->prog->aux->ops; |
3df126f3 | 3083 | const int insn_cnt = env->prog->len; |
36bbef52 | 3084 | struct bpf_insn insn_buf[16], *insn; |
9bac3d6d | 3085 | struct bpf_prog *new_prog; |
d691f9e8 | 3086 | enum bpf_access_type type; |
3df126f3 | 3087 | int i, cnt, delta = 0; |
9bac3d6d | 3088 | |
36bbef52 DB |
3089 | if (ops->gen_prologue) { |
3090 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, | |
3091 | env->prog); | |
3092 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
3093 | verbose("bpf verifier is misconfigured\n"); | |
3094 | return -EINVAL; | |
3095 | } else if (cnt) { | |
3096 | new_prog = bpf_patch_insn_single(env->prog, 0, | |
3097 | insn_buf, cnt); | |
3098 | if (!new_prog) | |
3099 | return -ENOMEM; | |
3100 | env->prog = new_prog; | |
3df126f3 | 3101 | delta += cnt - 1; |
36bbef52 DB |
3102 | } |
3103 | } | |
3104 | ||
3105 | if (!ops->convert_ctx_access) | |
9bac3d6d AS |
3106 | return 0; |
3107 | ||
3df126f3 | 3108 | insn = env->prog->insnsi + delta; |
36bbef52 | 3109 | |
9bac3d6d | 3110 | for (i = 0; i < insn_cnt; i++, insn++) { |
ea2e7ce5 AS |
3111 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
3112 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) | |
d691f9e8 | 3113 | type = BPF_READ; |
ea2e7ce5 AS |
3114 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
3115 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) | |
d691f9e8 AS |
3116 | type = BPF_WRITE; |
3117 | else | |
9bac3d6d AS |
3118 | continue; |
3119 | ||
3df126f3 | 3120 | if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX) |
9bac3d6d | 3121 | continue; |
9bac3d6d | 3122 | |
36bbef52 DB |
3123 | cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg, |
3124 | insn->off, insn_buf, env->prog); | |
9bac3d6d AS |
3125 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
3126 | verbose("bpf verifier is misconfigured\n"); | |
3127 | return -EINVAL; | |
3128 | } | |
3129 | ||
3df126f3 JK |
3130 | new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf, |
3131 | cnt); | |
9bac3d6d AS |
3132 | if (!new_prog) |
3133 | return -ENOMEM; | |
3134 | ||
3df126f3 | 3135 | delta += cnt - 1; |
9bac3d6d AS |
3136 | |
3137 | /* keep walking new program and skip insns we just inserted */ | |
3138 | env->prog = new_prog; | |
3df126f3 | 3139 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
3140 | } |
3141 | ||
3142 | return 0; | |
3143 | } | |
3144 | ||
58e2af8b | 3145 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 3146 | { |
58e2af8b | 3147 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
3148 | int i; |
3149 | ||
3150 | if (!env->explored_states) | |
3151 | return; | |
3152 | ||
3153 | for (i = 0; i < env->prog->len; i++) { | |
3154 | sl = env->explored_states[i]; | |
3155 | ||
3156 | if (sl) | |
3157 | while (sl != STATE_LIST_MARK) { | |
3158 | sln = sl->next; | |
3159 | kfree(sl); | |
3160 | sl = sln; | |
3161 | } | |
3162 | } | |
3163 | ||
3164 | kfree(env->explored_states); | |
3165 | } | |
3166 | ||
9bac3d6d | 3167 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
51580e79 | 3168 | { |
cbd35700 | 3169 | char __user *log_ubuf = NULL; |
58e2af8b | 3170 | struct bpf_verifier_env *env; |
51580e79 AS |
3171 | int ret = -EINVAL; |
3172 | ||
58e2af8b | 3173 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
3174 | * allocate/free it every time bpf_check() is called |
3175 | */ | |
58e2af8b | 3176 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
3177 | if (!env) |
3178 | return -ENOMEM; | |
3179 | ||
3df126f3 JK |
3180 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
3181 | (*prog)->len); | |
3182 | ret = -ENOMEM; | |
3183 | if (!env->insn_aux_data) | |
3184 | goto err_free_env; | |
9bac3d6d | 3185 | env->prog = *prog; |
0246e64d | 3186 | |
cbd35700 AS |
3187 | /* grab the mutex to protect few globals used by verifier */ |
3188 | mutex_lock(&bpf_verifier_lock); | |
3189 | ||
3190 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
3191 | /* user requested verbose verifier output | |
3192 | * and supplied buffer to store the verification trace | |
3193 | */ | |
3194 | log_level = attr->log_level; | |
3195 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; | |
3196 | log_size = attr->log_size; | |
3197 | log_len = 0; | |
3198 | ||
3199 | ret = -EINVAL; | |
3200 | /* log_* values have to be sane */ | |
3201 | if (log_size < 128 || log_size > UINT_MAX >> 8 || | |
3202 | log_level == 0 || log_ubuf == NULL) | |
3df126f3 | 3203 | goto err_unlock; |
cbd35700 AS |
3204 | |
3205 | ret = -ENOMEM; | |
3206 | log_buf = vmalloc(log_size); | |
3207 | if (!log_buf) | |
3df126f3 | 3208 | goto err_unlock; |
cbd35700 AS |
3209 | } else { |
3210 | log_level = 0; | |
3211 | } | |
3212 | ||
0246e64d AS |
3213 | ret = replace_map_fd_with_map_ptr(env); |
3214 | if (ret < 0) | |
3215 | goto skip_full_check; | |
3216 | ||
9bac3d6d | 3217 | env->explored_states = kcalloc(env->prog->len, |
58e2af8b | 3218 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
3219 | GFP_USER); |
3220 | ret = -ENOMEM; | |
3221 | if (!env->explored_states) | |
3222 | goto skip_full_check; | |
3223 | ||
475fb78f AS |
3224 | ret = check_cfg(env); |
3225 | if (ret < 0) | |
3226 | goto skip_full_check; | |
3227 | ||
1be7f75d AS |
3228 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
3229 | ||
17a52670 | 3230 | ret = do_check(env); |
cbd35700 | 3231 | |
0246e64d | 3232 | skip_full_check: |
17a52670 | 3233 | while (pop_stack(env, NULL) >= 0); |
f1bca824 | 3234 | free_states(env); |
0246e64d | 3235 | |
9bac3d6d AS |
3236 | if (ret == 0) |
3237 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
3238 | ret = convert_ctx_accesses(env); | |
3239 | ||
cbd35700 AS |
3240 | if (log_level && log_len >= log_size - 1) { |
3241 | BUG_ON(log_len >= log_size); | |
3242 | /* verifier log exceeded user supplied buffer */ | |
3243 | ret = -ENOSPC; | |
3244 | /* fall through to return what was recorded */ | |
3245 | } | |
3246 | ||
3247 | /* copy verifier log back to user space including trailing zero */ | |
3248 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { | |
3249 | ret = -EFAULT; | |
3250 | goto free_log_buf; | |
3251 | } | |
3252 | ||
0246e64d AS |
3253 | if (ret == 0 && env->used_map_cnt) { |
3254 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
3255 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
3256 | sizeof(env->used_maps[0]), | |
3257 | GFP_KERNEL); | |
0246e64d | 3258 | |
9bac3d6d | 3259 | if (!env->prog->aux->used_maps) { |
0246e64d AS |
3260 | ret = -ENOMEM; |
3261 | goto free_log_buf; | |
3262 | } | |
3263 | ||
9bac3d6d | 3264 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 3265 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 3266 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
3267 | |
3268 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
3269 | * bpf_ld_imm64 instructions | |
3270 | */ | |
3271 | convert_pseudo_ld_imm64(env); | |
3272 | } | |
cbd35700 AS |
3273 | |
3274 | free_log_buf: | |
3275 | if (log_level) | |
3276 | vfree(log_buf); | |
9bac3d6d | 3277 | if (!env->prog->aux->used_maps) |
0246e64d AS |
3278 | /* if we didn't copy map pointers into bpf_prog_info, release |
3279 | * them now. Otherwise free_bpf_prog_info() will release them. | |
3280 | */ | |
3281 | release_maps(env); | |
9bac3d6d | 3282 | *prog = env->prog; |
3df126f3 | 3283 | err_unlock: |
cbd35700 | 3284 | mutex_unlock(&bpf_verifier_lock); |
3df126f3 JK |
3285 | vfree(env->insn_aux_data); |
3286 | err_free_env: | |
3287 | kfree(env); | |
51580e79 AS |
3288 | return ret; |
3289 | } | |
13a27dfc JK |
3290 | |
3291 | int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops, | |
3292 | void *priv) | |
3293 | { | |
3294 | struct bpf_verifier_env *env; | |
3295 | int ret; | |
3296 | ||
3297 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); | |
3298 | if (!env) | |
3299 | return -ENOMEM; | |
3300 | ||
3301 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * | |
3302 | prog->len); | |
3303 | ret = -ENOMEM; | |
3304 | if (!env->insn_aux_data) | |
3305 | goto err_free_env; | |
3306 | env->prog = prog; | |
3307 | env->analyzer_ops = ops; | |
3308 | env->analyzer_priv = priv; | |
3309 | ||
3310 | /* grab the mutex to protect few globals used by verifier */ | |
3311 | mutex_lock(&bpf_verifier_lock); | |
3312 | ||
3313 | log_level = 0; | |
3314 | ||
3315 | env->explored_states = kcalloc(env->prog->len, | |
3316 | sizeof(struct bpf_verifier_state_list *), | |
3317 | GFP_KERNEL); | |
3318 | ret = -ENOMEM; | |
3319 | if (!env->explored_states) | |
3320 | goto skip_full_check; | |
3321 | ||
3322 | ret = check_cfg(env); | |
3323 | if (ret < 0) | |
3324 | goto skip_full_check; | |
3325 | ||
3326 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); | |
3327 | ||
3328 | ret = do_check(env); | |
3329 | ||
3330 | skip_full_check: | |
3331 | while (pop_stack(env, NULL) >= 0); | |
3332 | free_states(env); | |
3333 | ||
3334 | mutex_unlock(&bpf_verifier_lock); | |
3335 | vfree(env->insn_aux_data); | |
3336 | err_free_env: | |
3337 | kfree(env); | |
3338 | return ret; | |
3339 | } | |
3340 | EXPORT_SYMBOL_GPL(bpf_analyzer); |