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