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