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> |
cc8b0b92 AS |
23 | #include <linux/bsearch.h> |
24 | #include <linux/sort.h> | |
51580e79 | 25 | |
f4ac7e0b JK |
26 | #include "disasm.h" |
27 | ||
00176a34 JK |
28 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
29 | #define BPF_PROG_TYPE(_id, _name) \ | |
30 | [_id] = & _name ## _verifier_ops, | |
31 | #define BPF_MAP_TYPE(_id, _ops) | |
32 | #include <linux/bpf_types.h> | |
33 | #undef BPF_PROG_TYPE | |
34 | #undef BPF_MAP_TYPE | |
35 | }; | |
36 | ||
51580e79 AS |
37 | /* bpf_check() is a static code analyzer that walks eBPF program |
38 | * instruction by instruction and updates register/stack state. | |
39 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
40 | * | |
41 | * The first pass is depth-first-search to check that the program is a DAG. | |
42 | * It rejects the following programs: | |
43 | * - larger than BPF_MAXINSNS insns | |
44 | * - if loop is present (detected via back-edge) | |
45 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
46 | * - out of bounds or malformed jumps | |
47 | * The second pass is all possible path descent from the 1st insn. | |
48 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 49 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
50 | * insn is less then 4K, but there are too many branches that change stack/regs. |
51 | * Number of 'branches to be analyzed' is limited to 1k | |
52 | * | |
53 | * On entry to each instruction, each register has a type, and the instruction | |
54 | * changes the types of the registers depending on instruction semantics. | |
55 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
56 | * copied to R1. | |
57 | * | |
58 | * All registers are 64-bit. | |
59 | * R0 - return register | |
60 | * R1-R5 argument passing registers | |
61 | * R6-R9 callee saved registers | |
62 | * R10 - frame pointer read-only | |
63 | * | |
64 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
65 | * and has type PTR_TO_CTX. | |
66 | * | |
67 | * Verifier tracks arithmetic operations on pointers in case: | |
68 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
69 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
70 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
71 | * and 2nd arithmetic instruction is pattern matched to recognize | |
72 | * that it wants to construct a pointer to some element within stack. | |
73 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
74 | * (and -20 constant is saved for further stack bounds checking). | |
75 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
76 | * | |
f1174f77 | 77 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 78 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 79 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
80 | * |
81 | * When verifier sees load or store instructions the type of base register | |
f1174f77 | 82 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer |
51580e79 AS |
83 | * types recognized by check_mem_access() function. |
84 | * | |
85 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
86 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
87 | * | |
88 | * registers used to pass values to function calls are checked against | |
89 | * function argument constraints. | |
90 | * | |
91 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
92 | * It means that the register type passed to this function must be | |
93 | * PTR_TO_STACK and it will be used inside the function as | |
94 | * 'pointer to map element key' | |
95 | * | |
96 | * For example the argument constraints for bpf_map_lookup_elem(): | |
97 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
98 | * .arg1_type = ARG_CONST_MAP_PTR, | |
99 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
100 | * | |
101 | * ret_type says that this function returns 'pointer to map elem value or null' | |
102 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
103 | * 2nd argument should be a pointer to stack, which will be used inside | |
104 | * the helper function as a pointer to map element key. | |
105 | * | |
106 | * On the kernel side the helper function looks like: | |
107 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
108 | * { | |
109 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
110 | * void *key = (void *) (unsigned long) r2; | |
111 | * void *value; | |
112 | * | |
113 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
114 | * [key, key + map->key_size) bytes are valid and were initialized on | |
115 | * the stack of eBPF program. | |
116 | * } | |
117 | * | |
118 | * Corresponding eBPF program may look like: | |
119 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
120 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
121 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
122 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
123 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
124 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
125 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
126 | * | |
127 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
128 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
129 | * and were initialized prior to this call. | |
130 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
131 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
132 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
133 | * returns ether pointer to map value or NULL. | |
134 | * | |
135 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
136 | * insn, the register holding that pointer in the true branch changes state to | |
137 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
138 | * branch. See check_cond_jmp_op(). | |
139 | * | |
140 | * After the call R0 is set to return type of the function and registers R1-R5 | |
141 | * are set to NOT_INIT to indicate that they are no longer readable. | |
142 | */ | |
143 | ||
17a52670 | 144 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 145 | struct bpf_verifier_stack_elem { |
17a52670 AS |
146 | /* verifer state is 'st' |
147 | * before processing instruction 'insn_idx' | |
148 | * and after processing instruction 'prev_insn_idx' | |
149 | */ | |
58e2af8b | 150 | struct bpf_verifier_state st; |
17a52670 AS |
151 | int insn_idx; |
152 | int prev_insn_idx; | |
58e2af8b | 153 | struct bpf_verifier_stack_elem *next; |
cbd35700 AS |
154 | }; |
155 | ||
8e17c1b1 | 156 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
07016151 DB |
157 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
158 | ||
fad73a1a MKL |
159 | #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA) |
160 | ||
33ff9823 DB |
161 | struct bpf_call_arg_meta { |
162 | struct bpf_map *map_ptr; | |
435faee1 | 163 | bool raw_mode; |
36bbef52 | 164 | bool pkt_access; |
435faee1 DB |
165 | int regno; |
166 | int access_size; | |
33ff9823 DB |
167 | }; |
168 | ||
cbd35700 AS |
169 | static DEFINE_MUTEX(bpf_verifier_lock); |
170 | ||
171 | /* log_level controls verbosity level of eBPF verifier. | |
430e68d1 QM |
172 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
173 | * so the user can figure out what's wrong with the program | |
cbd35700 | 174 | */ |
430e68d1 QM |
175 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
176 | const char *fmt, ...) | |
cbd35700 | 177 | { |
61bd5218 | 178 | struct bpf_verifer_log *log = &env->log; |
a2a7d570 | 179 | unsigned int n; |
cbd35700 AS |
180 | va_list args; |
181 | ||
a2a7d570 | 182 | if (!log->level || !log->ubuf || bpf_verifier_log_full(log)) |
cbd35700 AS |
183 | return; |
184 | ||
185 | va_start(args, fmt); | |
a2a7d570 | 186 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
cbd35700 | 187 | va_end(args); |
a2a7d570 JK |
188 | |
189 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
190 | "verifier log line truncated - local buffer too short\n"); | |
191 | ||
192 | n = min(log->len_total - log->len_used - 1, n); | |
193 | log->kbuf[n] = '\0'; | |
194 | ||
195 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) | |
196 | log->len_used += n; | |
197 | else | |
198 | log->ubuf = NULL; | |
cbd35700 | 199 | } |
430e68d1 QM |
200 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
201 | /* Historically bpf_verifier_log_write was called verbose, but the name was too | |
202 | * generic for symbol export. The function was renamed, but not the calls in | |
203 | * the verifier to avoid complicating backports. Hence the alias below. | |
204 | */ | |
205 | static __printf(2, 3) void verbose(struct bpf_verifier_env *env, | |
206 | const char *fmt, ...) | |
207 | __attribute__((alias("bpf_verifier_log_write"))); | |
cbd35700 | 208 | |
de8f3a83 DB |
209 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
210 | { | |
211 | return type == PTR_TO_PACKET || | |
212 | type == PTR_TO_PACKET_META; | |
213 | } | |
214 | ||
17a52670 AS |
215 | /* string representation of 'enum bpf_reg_type' */ |
216 | static const char * const reg_type_str[] = { | |
217 | [NOT_INIT] = "?", | |
f1174f77 | 218 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
219 | [PTR_TO_CTX] = "ctx", |
220 | [CONST_PTR_TO_MAP] = "map_ptr", | |
221 | [PTR_TO_MAP_VALUE] = "map_value", | |
222 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 223 | [PTR_TO_STACK] = "fp", |
969bf05e | 224 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 225 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 226 | [PTR_TO_PACKET_END] = "pkt_end", |
17a52670 AS |
227 | }; |
228 | ||
4e92024a AS |
229 | static void print_liveness(struct bpf_verifier_env *env, |
230 | enum bpf_reg_liveness live) | |
231 | { | |
232 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) | |
233 | verbose(env, "_"); | |
234 | if (live & REG_LIVE_READ) | |
235 | verbose(env, "r"); | |
236 | if (live & REG_LIVE_WRITTEN) | |
237 | verbose(env, "w"); | |
238 | } | |
239 | ||
f4d7e40a AS |
240 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
241 | const struct bpf_reg_state *reg) | |
242 | { | |
243 | struct bpf_verifier_state *cur = env->cur_state; | |
244 | ||
245 | return cur->frame[reg->frameno]; | |
246 | } | |
247 | ||
61bd5218 | 248 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 249 | const struct bpf_func_state *state) |
17a52670 | 250 | { |
f4d7e40a | 251 | const struct bpf_reg_state *reg; |
17a52670 AS |
252 | enum bpf_reg_type t; |
253 | int i; | |
254 | ||
f4d7e40a AS |
255 | if (state->frameno) |
256 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 257 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
258 | reg = &state->regs[i]; |
259 | t = reg->type; | |
17a52670 AS |
260 | if (t == NOT_INIT) |
261 | continue; | |
4e92024a AS |
262 | verbose(env, " R%d", i); |
263 | print_liveness(env, reg->live); | |
264 | verbose(env, "=%s", reg_type_str[t]); | |
f1174f77 EC |
265 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
266 | tnum_is_const(reg->var_off)) { | |
267 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 268 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f4d7e40a AS |
269 | if (t == PTR_TO_STACK) |
270 | verbose(env, ",call_%d", func(env, reg)->callsite); | |
f1174f77 | 271 | } else { |
61bd5218 | 272 | verbose(env, "(id=%d", reg->id); |
f1174f77 | 273 | if (t != SCALAR_VALUE) |
61bd5218 | 274 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 275 | if (type_is_pkt_pointer(t)) |
61bd5218 | 276 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
277 | else if (t == CONST_PTR_TO_MAP || |
278 | t == PTR_TO_MAP_VALUE || | |
279 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 280 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
281 | reg->map_ptr->key_size, |
282 | reg->map_ptr->value_size); | |
7d1238f2 EC |
283 | if (tnum_is_const(reg->var_off)) { |
284 | /* Typically an immediate SCALAR_VALUE, but | |
285 | * could be a pointer whose offset is too big | |
286 | * for reg->off | |
287 | */ | |
61bd5218 | 288 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
289 | } else { |
290 | if (reg->smin_value != reg->umin_value && | |
291 | reg->smin_value != S64_MIN) | |
61bd5218 | 292 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
293 | (long long)reg->smin_value); |
294 | if (reg->smax_value != reg->umax_value && | |
295 | reg->smax_value != S64_MAX) | |
61bd5218 | 296 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
297 | (long long)reg->smax_value); |
298 | if (reg->umin_value != 0) | |
61bd5218 | 299 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
300 | (unsigned long long)reg->umin_value); |
301 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 302 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
303 | (unsigned long long)reg->umax_value); |
304 | if (!tnum_is_unknown(reg->var_off)) { | |
305 | char tn_buf[48]; | |
f1174f77 | 306 | |
7d1238f2 | 307 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 308 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 309 | } |
f1174f77 | 310 | } |
61bd5218 | 311 | verbose(env, ")"); |
f1174f77 | 312 | } |
17a52670 | 313 | } |
638f5b90 | 314 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
4e92024a AS |
315 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
316 | verbose(env, " fp%d", | |
317 | (-i - 1) * BPF_REG_SIZE); | |
318 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
319 | verbose(env, "=%s", | |
638f5b90 | 320 | reg_type_str[state->stack[i].spilled_ptr.type]); |
4e92024a | 321 | } |
cc2b14d5 AS |
322 | if (state->stack[i].slot_type[0] == STACK_ZERO) |
323 | verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE); | |
17a52670 | 324 | } |
61bd5218 | 325 | verbose(env, "\n"); |
17a52670 AS |
326 | } |
327 | ||
f4d7e40a AS |
328 | static int copy_stack_state(struct bpf_func_state *dst, |
329 | const struct bpf_func_state *src) | |
17a52670 | 330 | { |
638f5b90 AS |
331 | if (!src->stack) |
332 | return 0; | |
333 | if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) { | |
334 | /* internal bug, make state invalid to reject the program */ | |
335 | memset(dst, 0, sizeof(*dst)); | |
336 | return -EFAULT; | |
337 | } | |
338 | memcpy(dst->stack, src->stack, | |
339 | sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE)); | |
340 | return 0; | |
341 | } | |
342 | ||
343 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
344 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 345 | * the program calls into realloc_func_state() to grow the stack size. |
638f5b90 AS |
346 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
347 | * which this function copies over. It points to previous bpf_verifier_state | |
348 | * which is never reallocated | |
349 | */ | |
f4d7e40a AS |
350 | static int realloc_func_state(struct bpf_func_state *state, int size, |
351 | bool copy_old) | |
638f5b90 AS |
352 | { |
353 | u32 old_size = state->allocated_stack; | |
354 | struct bpf_stack_state *new_stack; | |
355 | int slot = size / BPF_REG_SIZE; | |
356 | ||
357 | if (size <= old_size || !size) { | |
358 | if (copy_old) | |
359 | return 0; | |
360 | state->allocated_stack = slot * BPF_REG_SIZE; | |
361 | if (!size && old_size) { | |
362 | kfree(state->stack); | |
363 | state->stack = NULL; | |
364 | } | |
365 | return 0; | |
366 | } | |
367 | new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state), | |
368 | GFP_KERNEL); | |
369 | if (!new_stack) | |
370 | return -ENOMEM; | |
371 | if (copy_old) { | |
372 | if (state->stack) | |
373 | memcpy(new_stack, state->stack, | |
374 | sizeof(*new_stack) * (old_size / BPF_REG_SIZE)); | |
375 | memset(new_stack + old_size / BPF_REG_SIZE, 0, | |
376 | sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE); | |
377 | } | |
378 | state->allocated_stack = slot * BPF_REG_SIZE; | |
379 | kfree(state->stack); | |
380 | state->stack = new_stack; | |
381 | return 0; | |
382 | } | |
383 | ||
f4d7e40a AS |
384 | static void free_func_state(struct bpf_func_state *state) |
385 | { | |
5896351e AS |
386 | if (!state) |
387 | return; | |
f4d7e40a AS |
388 | kfree(state->stack); |
389 | kfree(state); | |
390 | } | |
391 | ||
1969db47 AS |
392 | static void free_verifier_state(struct bpf_verifier_state *state, |
393 | bool free_self) | |
638f5b90 | 394 | { |
f4d7e40a AS |
395 | int i; |
396 | ||
397 | for (i = 0; i <= state->curframe; i++) { | |
398 | free_func_state(state->frame[i]); | |
399 | state->frame[i] = NULL; | |
400 | } | |
1969db47 AS |
401 | if (free_self) |
402 | kfree(state); | |
638f5b90 AS |
403 | } |
404 | ||
405 | /* copy verifier state from src to dst growing dst stack space | |
406 | * when necessary to accommodate larger src stack | |
407 | */ | |
f4d7e40a AS |
408 | static int copy_func_state(struct bpf_func_state *dst, |
409 | const struct bpf_func_state *src) | |
638f5b90 AS |
410 | { |
411 | int err; | |
412 | ||
f4d7e40a | 413 | err = realloc_func_state(dst, src->allocated_stack, false); |
638f5b90 AS |
414 | if (err) |
415 | return err; | |
f4d7e40a | 416 | memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack)); |
638f5b90 AS |
417 | return copy_stack_state(dst, src); |
418 | } | |
419 | ||
f4d7e40a AS |
420 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
421 | const struct bpf_verifier_state *src) | |
422 | { | |
423 | struct bpf_func_state *dst; | |
424 | int i, err; | |
425 | ||
426 | /* if dst has more stack frames then src frame, free them */ | |
427 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
428 | free_func_state(dst_state->frame[i]); | |
429 | dst_state->frame[i] = NULL; | |
430 | } | |
431 | dst_state->curframe = src->curframe; | |
432 | dst_state->parent = src->parent; | |
433 | for (i = 0; i <= src->curframe; i++) { | |
434 | dst = dst_state->frame[i]; | |
435 | if (!dst) { | |
436 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
437 | if (!dst) | |
438 | return -ENOMEM; | |
439 | dst_state->frame[i] = dst; | |
440 | } | |
441 | err = copy_func_state(dst, src->frame[i]); | |
442 | if (err) | |
443 | return err; | |
444 | } | |
445 | return 0; | |
446 | } | |
447 | ||
638f5b90 AS |
448 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
449 | int *insn_idx) | |
450 | { | |
451 | struct bpf_verifier_state *cur = env->cur_state; | |
452 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
453 | int err; | |
17a52670 AS |
454 | |
455 | if (env->head == NULL) | |
638f5b90 | 456 | return -ENOENT; |
17a52670 | 457 | |
638f5b90 AS |
458 | if (cur) { |
459 | err = copy_verifier_state(cur, &head->st); | |
460 | if (err) | |
461 | return err; | |
462 | } | |
463 | if (insn_idx) | |
464 | *insn_idx = head->insn_idx; | |
17a52670 | 465 | if (prev_insn_idx) |
638f5b90 AS |
466 | *prev_insn_idx = head->prev_insn_idx; |
467 | elem = head->next; | |
1969db47 | 468 | free_verifier_state(&head->st, false); |
638f5b90 | 469 | kfree(head); |
17a52670 AS |
470 | env->head = elem; |
471 | env->stack_size--; | |
638f5b90 | 472 | return 0; |
17a52670 AS |
473 | } |
474 | ||
58e2af8b JK |
475 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
476 | int insn_idx, int prev_insn_idx) | |
17a52670 | 477 | { |
638f5b90 | 478 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 479 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 480 | int err; |
17a52670 | 481 | |
638f5b90 | 482 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
483 | if (!elem) |
484 | goto err; | |
485 | ||
17a52670 AS |
486 | elem->insn_idx = insn_idx; |
487 | elem->prev_insn_idx = prev_insn_idx; | |
488 | elem->next = env->head; | |
489 | env->head = elem; | |
490 | env->stack_size++; | |
1969db47 AS |
491 | err = copy_verifier_state(&elem->st, cur); |
492 | if (err) | |
493 | goto err; | |
07016151 | 494 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
61bd5218 | 495 | verbose(env, "BPF program is too complex\n"); |
17a52670 AS |
496 | goto err; |
497 | } | |
498 | return &elem->st; | |
499 | err: | |
5896351e AS |
500 | free_verifier_state(env->cur_state, true); |
501 | env->cur_state = NULL; | |
17a52670 | 502 | /* pop all elements and return */ |
638f5b90 | 503 | while (!pop_stack(env, NULL, NULL)); |
17a52670 AS |
504 | return NULL; |
505 | } | |
506 | ||
507 | #define CALLER_SAVED_REGS 6 | |
508 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
509 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
510 | }; | |
f4d7e40a AS |
511 | #define CALLEE_SAVED_REGS 5 |
512 | static const int callee_saved[CALLEE_SAVED_REGS] = { | |
513 | BPF_REG_6, BPF_REG_7, BPF_REG_8, BPF_REG_9 | |
514 | }; | |
17a52670 | 515 | |
f1174f77 EC |
516 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
517 | ||
b03c9f9f EC |
518 | /* Mark the unknown part of a register (variable offset or scalar value) as |
519 | * known to have the value @imm. | |
520 | */ | |
521 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
522 | { | |
523 | reg->id = 0; | |
524 | reg->var_off = tnum_const(imm); | |
525 | reg->smin_value = (s64)imm; | |
526 | reg->smax_value = (s64)imm; | |
527 | reg->umin_value = imm; | |
528 | reg->umax_value = imm; | |
529 | } | |
530 | ||
f1174f77 EC |
531 | /* Mark the 'variable offset' part of a register as zero. This should be |
532 | * used only on registers holding a pointer type. | |
533 | */ | |
534 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 535 | { |
b03c9f9f | 536 | __mark_reg_known(reg, 0); |
f1174f77 | 537 | } |
a9789ef9 | 538 | |
cc2b14d5 AS |
539 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
540 | { | |
541 | __mark_reg_known(reg, 0); | |
542 | reg->off = 0; | |
543 | reg->type = SCALAR_VALUE; | |
544 | } | |
545 | ||
61bd5218 JK |
546 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
547 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
548 | { |
549 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 550 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
551 | /* Something bad happened, let's kill all regs */ |
552 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
553 | __mark_reg_not_init(regs + regno); | |
554 | return; | |
555 | } | |
556 | __mark_reg_known_zero(regs + regno); | |
557 | } | |
558 | ||
de8f3a83 DB |
559 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
560 | { | |
561 | return type_is_pkt_pointer(reg->type); | |
562 | } | |
563 | ||
564 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
565 | { | |
566 | return reg_is_pkt_pointer(reg) || | |
567 | reg->type == PTR_TO_PACKET_END; | |
568 | } | |
569 | ||
570 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
571 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
572 | enum bpf_reg_type which) | |
573 | { | |
574 | /* The register can already have a range from prior markings. | |
575 | * This is fine as long as it hasn't been advanced from its | |
576 | * origin. | |
577 | */ | |
578 | return reg->type == which && | |
579 | reg->id == 0 && | |
580 | reg->off == 0 && | |
581 | tnum_equals_const(reg->var_off, 0); | |
582 | } | |
583 | ||
b03c9f9f EC |
584 | /* Attempts to improve min/max values based on var_off information */ |
585 | static void __update_reg_bounds(struct bpf_reg_state *reg) | |
586 | { | |
587 | /* min signed is max(sign bit) | min(other bits) */ | |
588 | reg->smin_value = max_t(s64, reg->smin_value, | |
589 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
590 | /* max signed is min(sign bit) | max(other bits) */ | |
591 | reg->smax_value = min_t(s64, reg->smax_value, | |
592 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
593 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
594 | reg->umax_value = min(reg->umax_value, | |
595 | reg->var_off.value | reg->var_off.mask); | |
596 | } | |
597 | ||
598 | /* Uses signed min/max values to inform unsigned, and vice-versa */ | |
599 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) | |
600 | { | |
601 | /* Learn sign from signed bounds. | |
602 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
603 | * are the same, so combine. This works even in the negative case, e.g. | |
604 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
605 | */ | |
606 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
607 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
608 | reg->umin_value); | |
609 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
610 | reg->umax_value); | |
611 | return; | |
612 | } | |
613 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
614 | * boundary, so we must be careful. | |
615 | */ | |
616 | if ((s64)reg->umax_value >= 0) { | |
617 | /* Positive. We can't learn anything from the smin, but smax | |
618 | * is positive, hence safe. | |
619 | */ | |
620 | reg->smin_value = reg->umin_value; | |
621 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
622 | reg->umax_value); | |
623 | } else if ((s64)reg->umin_value < 0) { | |
624 | /* Negative. We can't learn anything from the smax, but smin | |
625 | * is negative, hence safe. | |
626 | */ | |
627 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
628 | reg->umin_value); | |
629 | reg->smax_value = reg->umax_value; | |
630 | } | |
631 | } | |
632 | ||
633 | /* Attempts to improve var_off based on unsigned min/max information */ | |
634 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
635 | { | |
636 | reg->var_off = tnum_intersect(reg->var_off, | |
637 | tnum_range(reg->umin_value, | |
638 | reg->umax_value)); | |
639 | } | |
640 | ||
641 | /* Reset the min/max bounds of a register */ | |
642 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
643 | { | |
644 | reg->smin_value = S64_MIN; | |
645 | reg->smax_value = S64_MAX; | |
646 | reg->umin_value = 0; | |
647 | reg->umax_value = U64_MAX; | |
648 | } | |
649 | ||
f1174f77 EC |
650 | /* Mark a register as having a completely unknown (scalar) value. */ |
651 | static void __mark_reg_unknown(struct bpf_reg_state *reg) | |
652 | { | |
653 | reg->type = SCALAR_VALUE; | |
654 | reg->id = 0; | |
655 | reg->off = 0; | |
656 | reg->var_off = tnum_unknown; | |
f4d7e40a | 657 | reg->frameno = 0; |
b03c9f9f | 658 | __mark_reg_unbounded(reg); |
f1174f77 EC |
659 | } |
660 | ||
61bd5218 JK |
661 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
662 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
663 | { |
664 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 665 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
666 | /* Something bad happened, let's kill all regs except FP */ |
667 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
668 | __mark_reg_not_init(regs + regno); |
669 | return; | |
670 | } | |
671 | __mark_reg_unknown(regs + regno); | |
672 | } | |
673 | ||
674 | static void __mark_reg_not_init(struct bpf_reg_state *reg) | |
675 | { | |
676 | __mark_reg_unknown(reg); | |
677 | reg->type = NOT_INIT; | |
678 | } | |
679 | ||
61bd5218 JK |
680 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
681 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
682 | { |
683 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 684 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
685 | /* Something bad happened, let's kill all regs except FP */ |
686 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
687 | __mark_reg_not_init(regs + regno); |
688 | return; | |
689 | } | |
690 | __mark_reg_not_init(regs + regno); | |
a9789ef9 DB |
691 | } |
692 | ||
61bd5218 | 693 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 694 | struct bpf_func_state *state) |
17a52670 | 695 | { |
f4d7e40a | 696 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
697 | int i; |
698 | ||
dc503a8a | 699 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 700 | mark_reg_not_init(env, regs, i); |
dc503a8a EC |
701 | regs[i].live = REG_LIVE_NONE; |
702 | } | |
17a52670 AS |
703 | |
704 | /* frame pointer */ | |
f1174f77 | 705 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 706 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 707 | regs[BPF_REG_FP].frameno = state->frameno; |
17a52670 AS |
708 | |
709 | /* 1st arg to a function */ | |
710 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
61bd5218 | 711 | mark_reg_known_zero(env, regs, BPF_REG_1); |
6760bf2d DB |
712 | } |
713 | ||
f4d7e40a AS |
714 | #define BPF_MAIN_FUNC (-1) |
715 | static void init_func_state(struct bpf_verifier_env *env, | |
716 | struct bpf_func_state *state, | |
717 | int callsite, int frameno, int subprogno) | |
718 | { | |
719 | state->callsite = callsite; | |
720 | state->frameno = frameno; | |
721 | state->subprogno = subprogno; | |
722 | init_reg_state(env, state); | |
723 | } | |
724 | ||
17a52670 AS |
725 | enum reg_arg_type { |
726 | SRC_OP, /* register is used as source operand */ | |
727 | DST_OP, /* register is used as destination operand */ | |
728 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
729 | }; | |
730 | ||
cc8b0b92 AS |
731 | static int cmp_subprogs(const void *a, const void *b) |
732 | { | |
733 | return *(int *)a - *(int *)b; | |
734 | } | |
735 | ||
736 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
737 | { | |
738 | u32 *p; | |
739 | ||
740 | p = bsearch(&off, env->subprog_starts, env->subprog_cnt, | |
741 | sizeof(env->subprog_starts[0]), cmp_subprogs); | |
742 | if (!p) | |
743 | return -ENOENT; | |
744 | return p - env->subprog_starts; | |
745 | ||
746 | } | |
747 | ||
748 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
749 | { | |
750 | int insn_cnt = env->prog->len; | |
751 | int ret; | |
752 | ||
753 | if (off >= insn_cnt || off < 0) { | |
754 | verbose(env, "call to invalid destination\n"); | |
755 | return -EINVAL; | |
756 | } | |
757 | ret = find_subprog(env, off); | |
758 | if (ret >= 0) | |
759 | return 0; | |
760 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { | |
761 | verbose(env, "too many subprograms\n"); | |
762 | return -E2BIG; | |
763 | } | |
764 | env->subprog_starts[env->subprog_cnt++] = off; | |
765 | sort(env->subprog_starts, env->subprog_cnt, | |
766 | sizeof(env->subprog_starts[0]), cmp_subprogs, NULL); | |
767 | return 0; | |
768 | } | |
769 | ||
770 | static int check_subprogs(struct bpf_verifier_env *env) | |
771 | { | |
772 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
773 | struct bpf_insn *insn = env->prog->insnsi; | |
774 | int insn_cnt = env->prog->len; | |
775 | ||
776 | /* determine subprog starts. The end is one before the next starts */ | |
777 | for (i = 0; i < insn_cnt; i++) { | |
778 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
779 | continue; | |
780 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
781 | continue; | |
782 | if (!env->allow_ptr_leaks) { | |
783 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); | |
784 | return -EPERM; | |
785 | } | |
786 | if (bpf_prog_is_dev_bound(env->prog->aux)) { | |
e90004d5 | 787 | verbose(env, "function calls in offloaded programs are not supported yet\n"); |
cc8b0b92 AS |
788 | return -EINVAL; |
789 | } | |
790 | ret = add_subprog(env, i + insn[i].imm + 1); | |
791 | if (ret < 0) | |
792 | return ret; | |
793 | } | |
794 | ||
795 | if (env->log.level > 1) | |
796 | for (i = 0; i < env->subprog_cnt; i++) | |
797 | verbose(env, "func#%d @%d\n", i, env->subprog_starts[i]); | |
798 | ||
799 | /* now check that all jumps are within the same subprog */ | |
800 | subprog_start = 0; | |
801 | if (env->subprog_cnt == cur_subprog) | |
802 | subprog_end = insn_cnt; | |
803 | else | |
804 | subprog_end = env->subprog_starts[cur_subprog++]; | |
805 | for (i = 0; i < insn_cnt; i++) { | |
806 | u8 code = insn[i].code; | |
807 | ||
808 | if (BPF_CLASS(code) != BPF_JMP) | |
809 | goto next; | |
810 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
811 | goto next; | |
812 | off = i + insn[i].off + 1; | |
813 | if (off < subprog_start || off >= subprog_end) { | |
814 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
815 | return -EINVAL; | |
816 | } | |
817 | next: | |
818 | if (i == subprog_end - 1) { | |
819 | /* to avoid fall-through from one subprog into another | |
820 | * the last insn of the subprog should be either exit | |
821 | * or unconditional jump back | |
822 | */ | |
823 | if (code != (BPF_JMP | BPF_EXIT) && | |
824 | code != (BPF_JMP | BPF_JA)) { | |
825 | verbose(env, "last insn is not an exit or jmp\n"); | |
826 | return -EINVAL; | |
827 | } | |
828 | subprog_start = subprog_end; | |
829 | if (env->subprog_cnt == cur_subprog) | |
830 | subprog_end = insn_cnt; | |
831 | else | |
832 | subprog_end = env->subprog_starts[cur_subprog++]; | |
833 | } | |
834 | } | |
835 | return 0; | |
836 | } | |
837 | ||
fa2d41ad | 838 | static |
f4d7e40a AS |
839 | struct bpf_verifier_state *skip_callee(struct bpf_verifier_env *env, |
840 | const struct bpf_verifier_state *state, | |
841 | struct bpf_verifier_state *parent, | |
842 | u32 regno) | |
dc503a8a | 843 | { |
f4d7e40a AS |
844 | struct bpf_verifier_state *tmp = NULL; |
845 | ||
846 | /* 'parent' could be a state of caller and | |
847 | * 'state' could be a state of callee. In such case | |
848 | * parent->curframe < state->curframe | |
849 | * and it's ok for r1 - r5 registers | |
850 | * | |
851 | * 'parent' could be a callee's state after it bpf_exit-ed. | |
852 | * In such case parent->curframe > state->curframe | |
853 | * and it's ok for r0 only | |
854 | */ | |
855 | if (parent->curframe == state->curframe || | |
856 | (parent->curframe < state->curframe && | |
857 | regno >= BPF_REG_1 && regno <= BPF_REG_5) || | |
858 | (parent->curframe > state->curframe && | |
859 | regno == BPF_REG_0)) | |
860 | return parent; | |
861 | ||
862 | if (parent->curframe > state->curframe && | |
863 | regno >= BPF_REG_6) { | |
864 | /* for callee saved regs we have to skip the whole chain | |
865 | * of states that belong to callee and mark as LIVE_READ | |
866 | * the registers before the call | |
867 | */ | |
868 | tmp = parent; | |
869 | while (tmp && tmp->curframe != state->curframe) { | |
870 | tmp = tmp->parent; | |
871 | } | |
872 | if (!tmp) | |
873 | goto bug; | |
874 | parent = tmp; | |
875 | } else { | |
876 | goto bug; | |
877 | } | |
878 | return parent; | |
879 | bug: | |
880 | verbose(env, "verifier bug regno %d tmp %p\n", regno, tmp); | |
881 | verbose(env, "regno %d parent frame %d current frame %d\n", | |
882 | regno, parent->curframe, state->curframe); | |
fa2d41ad | 883 | return NULL; |
f4d7e40a AS |
884 | } |
885 | ||
886 | static int mark_reg_read(struct bpf_verifier_env *env, | |
887 | const struct bpf_verifier_state *state, | |
888 | struct bpf_verifier_state *parent, | |
889 | u32 regno) | |
890 | { | |
891 | bool writes = parent == state->parent; /* Observe write marks */ | |
dc503a8a | 892 | |
8fe2d6cc AS |
893 | if (regno == BPF_REG_FP) |
894 | /* We don't need to worry about FP liveness because it's read-only */ | |
f4d7e40a | 895 | return 0; |
8fe2d6cc | 896 | |
dc503a8a EC |
897 | while (parent) { |
898 | /* if read wasn't screened by an earlier write ... */ | |
f4d7e40a | 899 | if (writes && state->frame[state->curframe]->regs[regno].live & REG_LIVE_WRITTEN) |
dc503a8a | 900 | break; |
f4d7e40a AS |
901 | parent = skip_callee(env, state, parent, regno); |
902 | if (!parent) | |
903 | return -EFAULT; | |
dc503a8a | 904 | /* ... then we depend on parent's value */ |
f4d7e40a | 905 | parent->frame[parent->curframe]->regs[regno].live |= REG_LIVE_READ; |
dc503a8a EC |
906 | state = parent; |
907 | parent = state->parent; | |
f4d7e40a | 908 | writes = true; |
dc503a8a | 909 | } |
f4d7e40a | 910 | return 0; |
dc503a8a EC |
911 | } |
912 | ||
913 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, | |
17a52670 AS |
914 | enum reg_arg_type t) |
915 | { | |
f4d7e40a AS |
916 | struct bpf_verifier_state *vstate = env->cur_state; |
917 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
918 | struct bpf_reg_state *regs = state->regs; | |
dc503a8a | 919 | |
17a52670 | 920 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 921 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
922 | return -EINVAL; |
923 | } | |
924 | ||
925 | if (t == SRC_OP) { | |
926 | /* check whether register used as source operand can be read */ | |
927 | if (regs[regno].type == NOT_INIT) { | |
61bd5218 | 928 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
929 | return -EACCES; |
930 | } | |
f4d7e40a | 931 | return mark_reg_read(env, vstate, vstate->parent, regno); |
17a52670 AS |
932 | } else { |
933 | /* check whether register used as dest operand can be written to */ | |
934 | if (regno == BPF_REG_FP) { | |
61bd5218 | 935 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
936 | return -EACCES; |
937 | } | |
dc503a8a | 938 | regs[regno].live |= REG_LIVE_WRITTEN; |
17a52670 | 939 | if (t == DST_OP) |
61bd5218 | 940 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
941 | } |
942 | return 0; | |
943 | } | |
944 | ||
1be7f75d AS |
945 | static bool is_spillable_regtype(enum bpf_reg_type type) |
946 | { | |
947 | switch (type) { | |
948 | case PTR_TO_MAP_VALUE: | |
949 | case PTR_TO_MAP_VALUE_OR_NULL: | |
950 | case PTR_TO_STACK: | |
951 | case PTR_TO_CTX: | |
969bf05e | 952 | case PTR_TO_PACKET: |
de8f3a83 | 953 | case PTR_TO_PACKET_META: |
969bf05e | 954 | case PTR_TO_PACKET_END: |
1be7f75d AS |
955 | case CONST_PTR_TO_MAP: |
956 | return true; | |
957 | default: | |
958 | return false; | |
959 | } | |
960 | } | |
961 | ||
cc2b14d5 AS |
962 | /* Does this register contain a constant zero? */ |
963 | static bool register_is_null(struct bpf_reg_state *reg) | |
964 | { | |
965 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
966 | } | |
967 | ||
17a52670 AS |
968 | /* check_stack_read/write functions track spill/fill of registers, |
969 | * stack boundary and alignment are checked in check_mem_access() | |
970 | */ | |
61bd5218 | 971 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a AS |
972 | struct bpf_func_state *state, /* func where register points to */ |
973 | int off, int size, int value_regno) | |
17a52670 | 974 | { |
f4d7e40a | 975 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 976 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
f4d7e40a | 977 | enum bpf_reg_type type; |
638f5b90 | 978 | |
f4d7e40a AS |
979 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
980 | true); | |
638f5b90 AS |
981 | if (err) |
982 | return err; | |
9c399760 AS |
983 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
984 | * so it's aligned access and [off, off + size) are within stack limits | |
985 | */ | |
638f5b90 AS |
986 | if (!env->allow_ptr_leaks && |
987 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
988 | size != BPF_REG_SIZE) { | |
989 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
990 | return -EACCES; | |
991 | } | |
17a52670 | 992 | |
f4d7e40a | 993 | cur = env->cur_state->frame[env->cur_state->curframe]; |
17a52670 | 994 | if (value_regno >= 0 && |
f4d7e40a | 995 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
17a52670 AS |
996 | |
997 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 998 | if (size != BPF_REG_SIZE) { |
61bd5218 | 999 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1000 | return -EACCES; |
1001 | } | |
1002 | ||
f4d7e40a AS |
1003 | if (state != cur && type == PTR_TO_STACK) { |
1004 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); | |
1005 | return -EINVAL; | |
1006 | } | |
1007 | ||
17a52670 | 1008 | /* save register state */ |
f4d7e40a | 1009 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
638f5b90 | 1010 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
17a52670 | 1011 | |
9c399760 | 1012 | for (i = 0; i < BPF_REG_SIZE; i++) |
638f5b90 | 1013 | state->stack[spi].slot_type[i] = STACK_SPILL; |
9c399760 | 1014 | } else { |
cc2b14d5 AS |
1015 | u8 type = STACK_MISC; |
1016 | ||
17a52670 | 1017 | /* regular write of data into stack */ |
638f5b90 | 1018 | state->stack[spi].spilled_ptr = (struct bpf_reg_state) {}; |
9c399760 | 1019 | |
cc2b14d5 AS |
1020 | /* only mark the slot as written if all 8 bytes were written |
1021 | * otherwise read propagation may incorrectly stop too soon | |
1022 | * when stack slots are partially written. | |
1023 | * This heuristic means that read propagation will be | |
1024 | * conservative, since it will add reg_live_read marks | |
1025 | * to stack slots all the way to first state when programs | |
1026 | * writes+reads less than 8 bytes | |
1027 | */ | |
1028 | if (size == BPF_REG_SIZE) | |
1029 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1030 | ||
1031 | /* when we zero initialize stack slots mark them as such */ | |
1032 | if (value_regno >= 0 && | |
1033 | register_is_null(&cur->regs[value_regno])) | |
1034 | type = STACK_ZERO; | |
1035 | ||
9c399760 | 1036 | for (i = 0; i < size; i++) |
638f5b90 | 1037 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 1038 | type; |
17a52670 AS |
1039 | } |
1040 | return 0; | |
1041 | } | |
1042 | ||
f4d7e40a AS |
1043 | /* registers of every function are unique and mark_reg_read() propagates |
1044 | * the liveness in the following cases: | |
1045 | * - from callee into caller for R1 - R5 that were used as arguments | |
1046 | * - from caller into callee for R0 that used as result of the call | |
1047 | * - from caller to the same caller skipping states of the callee for R6 - R9, | |
1048 | * since R6 - R9 are callee saved by implicit function prologue and | |
1049 | * caller's R6 != callee's R6, so when we propagate liveness up to | |
1050 | * parent states we need to skip callee states for R6 - R9. | |
1051 | * | |
1052 | * stack slot marking is different, since stacks of caller and callee are | |
1053 | * accessible in both (since caller can pass a pointer to caller's stack to | |
1054 | * callee which can pass it to another function), hence mark_stack_slot_read() | |
1055 | * has to propagate the stack liveness to all parent states at given frame number. | |
1056 | * Consider code: | |
1057 | * f1() { | |
1058 | * ptr = fp - 8; | |
1059 | * *ptr = ctx; | |
1060 | * call f2 { | |
1061 | * .. = *ptr; | |
1062 | * } | |
1063 | * .. = *ptr; | |
1064 | * } | |
1065 | * First *ptr is reading from f1's stack and mark_stack_slot_read() has | |
1066 | * to mark liveness at the f1's frame and not f2's frame. | |
1067 | * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has | |
1068 | * to propagate liveness to f2 states at f1's frame level and further into | |
1069 | * f1 states at f1's frame level until write into that stack slot | |
1070 | */ | |
1071 | static void mark_stack_slot_read(struct bpf_verifier_env *env, | |
1072 | const struct bpf_verifier_state *state, | |
1073 | struct bpf_verifier_state *parent, | |
1074 | int slot, int frameno) | |
dc503a8a | 1075 | { |
f4d7e40a | 1076 | bool writes = parent == state->parent; /* Observe write marks */ |
dc503a8a EC |
1077 | |
1078 | while (parent) { | |
cc2b14d5 AS |
1079 | if (parent->frame[frameno]->allocated_stack <= slot * BPF_REG_SIZE) |
1080 | /* since LIVE_WRITTEN mark is only done for full 8-byte | |
1081 | * write the read marks are conservative and parent | |
1082 | * state may not even have the stack allocated. In such case | |
1083 | * end the propagation, since the loop reached beginning | |
1084 | * of the function | |
1085 | */ | |
1086 | break; | |
dc503a8a | 1087 | /* if read wasn't screened by an earlier write ... */ |
f4d7e40a | 1088 | if (writes && state->frame[frameno]->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN) |
dc503a8a EC |
1089 | break; |
1090 | /* ... then we depend on parent's value */ | |
f4d7e40a | 1091 | parent->frame[frameno]->stack[slot].spilled_ptr.live |= REG_LIVE_READ; |
dc503a8a EC |
1092 | state = parent; |
1093 | parent = state->parent; | |
f4d7e40a | 1094 | writes = true; |
dc503a8a EC |
1095 | } |
1096 | } | |
1097 | ||
61bd5218 | 1098 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
1099 | struct bpf_func_state *reg_state /* func where register points to */, |
1100 | int off, int size, int value_regno) | |
17a52670 | 1101 | { |
f4d7e40a AS |
1102 | struct bpf_verifier_state *vstate = env->cur_state; |
1103 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 AS |
1104 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
1105 | u8 *stype; | |
17a52670 | 1106 | |
f4d7e40a | 1107 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
1108 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
1109 | off, size); | |
1110 | return -EACCES; | |
1111 | } | |
f4d7e40a | 1112 | stype = reg_state->stack[spi].slot_type; |
17a52670 | 1113 | |
638f5b90 | 1114 | if (stype[0] == STACK_SPILL) { |
9c399760 | 1115 | if (size != BPF_REG_SIZE) { |
61bd5218 | 1116 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1117 | return -EACCES; |
1118 | } | |
9c399760 | 1119 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 1120 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 1121 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
1122 | return -EACCES; |
1123 | } | |
1124 | } | |
1125 | ||
dc503a8a | 1126 | if (value_regno >= 0) { |
17a52670 | 1127 | /* restore register state from stack */ |
f4d7e40a | 1128 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
2f18f62e AS |
1129 | /* mark reg as written since spilled pointer state likely |
1130 | * has its liveness marks cleared by is_state_visited() | |
1131 | * which resets stack/reg liveness for state transitions | |
1132 | */ | |
1133 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
dc503a8a | 1134 | } |
cc2b14d5 AS |
1135 | mark_stack_slot_read(env, vstate, vstate->parent, spi, |
1136 | reg_state->frameno); | |
17a52670 AS |
1137 | return 0; |
1138 | } else { | |
cc2b14d5 AS |
1139 | int zeros = 0; |
1140 | ||
17a52670 | 1141 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
1142 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
1143 | continue; | |
1144 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
1145 | zeros++; | |
1146 | continue; | |
17a52670 | 1147 | } |
cc2b14d5 AS |
1148 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
1149 | off, i, size); | |
1150 | return -EACCES; | |
1151 | } | |
1152 | mark_stack_slot_read(env, vstate, vstate->parent, spi, | |
1153 | reg_state->frameno); | |
1154 | if (value_regno >= 0) { | |
1155 | if (zeros == size) { | |
1156 | /* any size read into register is zero extended, | |
1157 | * so the whole register == const_zero | |
1158 | */ | |
1159 | __mark_reg_const_zero(&state->regs[value_regno]); | |
1160 | } else { | |
1161 | /* have read misc data from the stack */ | |
1162 | mark_reg_unknown(env, state->regs, value_regno); | |
1163 | } | |
1164 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 1165 | } |
17a52670 AS |
1166 | return 0; |
1167 | } | |
1168 | } | |
1169 | ||
1170 | /* check read/write into map element returned by bpf_map_lookup_elem() */ | |
f1174f77 | 1171 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 1172 | int size, bool zero_size_allowed) |
17a52670 | 1173 | { |
638f5b90 AS |
1174 | struct bpf_reg_state *regs = cur_regs(env); |
1175 | struct bpf_map *map = regs[regno].map_ptr; | |
17a52670 | 1176 | |
9fd29c08 YS |
1177 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
1178 | off + size > map->value_size) { | |
61bd5218 | 1179 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
17a52670 AS |
1180 | map->value_size, off, size); |
1181 | return -EACCES; | |
1182 | } | |
1183 | return 0; | |
1184 | } | |
1185 | ||
f1174f77 EC |
1186 | /* check read/write into a map element with possible variable offset */ |
1187 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
9fd29c08 | 1188 | int off, int size, bool zero_size_allowed) |
dbcfe5f7 | 1189 | { |
f4d7e40a AS |
1190 | struct bpf_verifier_state *vstate = env->cur_state; |
1191 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
1192 | struct bpf_reg_state *reg = &state->regs[regno]; |
1193 | int err; | |
1194 | ||
f1174f77 EC |
1195 | /* We may have adjusted the register to this map value, so we |
1196 | * need to try adding each of min_value and max_value to off | |
1197 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 1198 | */ |
61bd5218 JK |
1199 | if (env->log.level) |
1200 | print_verifier_state(env, state); | |
dbcfe5f7 GB |
1201 | /* The minimum value is only important with signed |
1202 | * comparisons where we can't assume the floor of a | |
1203 | * value is 0. If we are using signed variables for our | |
1204 | * index'es we need to make sure that whatever we use | |
1205 | * will have a set floor within our range. | |
1206 | */ | |
b03c9f9f | 1207 | if (reg->smin_value < 0) { |
61bd5218 | 1208 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
1209 | regno); |
1210 | return -EACCES; | |
1211 | } | |
9fd29c08 YS |
1212 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
1213 | zero_size_allowed); | |
dbcfe5f7 | 1214 | if (err) { |
61bd5218 JK |
1215 | verbose(env, "R%d min value is outside of the array range\n", |
1216 | regno); | |
dbcfe5f7 GB |
1217 | return err; |
1218 | } | |
1219 | ||
b03c9f9f EC |
1220 | /* If we haven't set a max value then we need to bail since we can't be |
1221 | * sure we won't do bad things. | |
1222 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 1223 | */ |
b03c9f9f | 1224 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
61bd5218 | 1225 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
dbcfe5f7 GB |
1226 | regno); |
1227 | return -EACCES; | |
1228 | } | |
9fd29c08 YS |
1229 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
1230 | zero_size_allowed); | |
f1174f77 | 1231 | if (err) |
61bd5218 JK |
1232 | verbose(env, "R%d max value is outside of the array range\n", |
1233 | regno); | |
f1174f77 | 1234 | return err; |
dbcfe5f7 GB |
1235 | } |
1236 | ||
969bf05e AS |
1237 | #define MAX_PACKET_OFF 0xffff |
1238 | ||
58e2af8b | 1239 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
1240 | const struct bpf_call_arg_meta *meta, |
1241 | enum bpf_access_type t) | |
4acf6c0b | 1242 | { |
36bbef52 | 1243 | switch (env->prog->type) { |
3a0af8fd TG |
1244 | case BPF_PROG_TYPE_LWT_IN: |
1245 | case BPF_PROG_TYPE_LWT_OUT: | |
1246 | /* dst_input() and dst_output() can't write for now */ | |
1247 | if (t == BPF_WRITE) | |
1248 | return false; | |
7e57fbb2 | 1249 | /* fallthrough */ |
36bbef52 DB |
1250 | case BPF_PROG_TYPE_SCHED_CLS: |
1251 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 1252 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 1253 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 1254 | case BPF_PROG_TYPE_SK_SKB: |
36bbef52 DB |
1255 | if (meta) |
1256 | return meta->pkt_access; | |
1257 | ||
1258 | env->seen_direct_write = true; | |
4acf6c0b BB |
1259 | return true; |
1260 | default: | |
1261 | return false; | |
1262 | } | |
1263 | } | |
1264 | ||
f1174f77 | 1265 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
9fd29c08 | 1266 | int off, int size, bool zero_size_allowed) |
969bf05e | 1267 | { |
638f5b90 | 1268 | struct bpf_reg_state *regs = cur_regs(env); |
58e2af8b | 1269 | struct bpf_reg_state *reg = ®s[regno]; |
969bf05e | 1270 | |
9fd29c08 YS |
1271 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
1272 | (u64)off + size > reg->range) { | |
61bd5218 | 1273 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
d91b28ed | 1274 | off, size, regno, reg->id, reg->off, reg->range); |
969bf05e AS |
1275 | return -EACCES; |
1276 | } | |
1277 | return 0; | |
1278 | } | |
1279 | ||
f1174f77 | 1280 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 1281 | int size, bool zero_size_allowed) |
f1174f77 | 1282 | { |
638f5b90 | 1283 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
1284 | struct bpf_reg_state *reg = ®s[regno]; |
1285 | int err; | |
1286 | ||
1287 | /* We may have added a variable offset to the packet pointer; but any | |
1288 | * reg->range we have comes after that. We are only checking the fixed | |
1289 | * offset. | |
1290 | */ | |
1291 | ||
1292 | /* We don't allow negative numbers, because we aren't tracking enough | |
1293 | * detail to prove they're safe. | |
1294 | */ | |
b03c9f9f | 1295 | if (reg->smin_value < 0) { |
61bd5218 | 1296 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
1297 | regno); |
1298 | return -EACCES; | |
1299 | } | |
9fd29c08 | 1300 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
f1174f77 | 1301 | if (err) { |
61bd5218 | 1302 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
1303 | return err; |
1304 | } | |
1305 | return err; | |
1306 | } | |
1307 | ||
1308 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 1309 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
19de99f7 | 1310 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 1311 | { |
f96da094 DB |
1312 | struct bpf_insn_access_aux info = { |
1313 | .reg_type = *reg_type, | |
1314 | }; | |
31fd8581 | 1315 | |
4f9218aa JK |
1316 | if (env->ops->is_valid_access && |
1317 | env->ops->is_valid_access(off, size, t, &info)) { | |
f96da094 DB |
1318 | /* A non zero info.ctx_field_size indicates that this field is a |
1319 | * candidate for later verifier transformation to load the whole | |
1320 | * field and then apply a mask when accessed with a narrower | |
1321 | * access than actual ctx access size. A zero info.ctx_field_size | |
1322 | * will only allow for whole field access and rejects any other | |
1323 | * type of narrower access. | |
31fd8581 | 1324 | */ |
23994631 | 1325 | *reg_type = info.reg_type; |
31fd8581 | 1326 | |
4f9218aa | 1327 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
32bbe007 AS |
1328 | /* remember the offset of last byte accessed in ctx */ |
1329 | if (env->prog->aux->max_ctx_offset < off + size) | |
1330 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 1331 | return 0; |
32bbe007 | 1332 | } |
17a52670 | 1333 | |
61bd5218 | 1334 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
1335 | return -EACCES; |
1336 | } | |
1337 | ||
4cabc5b1 DB |
1338 | static bool __is_pointer_value(bool allow_ptr_leaks, |
1339 | const struct bpf_reg_state *reg) | |
1be7f75d | 1340 | { |
4cabc5b1 | 1341 | if (allow_ptr_leaks) |
1be7f75d AS |
1342 | return false; |
1343 | ||
f1174f77 | 1344 | return reg->type != SCALAR_VALUE; |
1be7f75d AS |
1345 | } |
1346 | ||
4cabc5b1 DB |
1347 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
1348 | { | |
638f5b90 | 1349 | return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno); |
4cabc5b1 DB |
1350 | } |
1351 | ||
61bd5218 JK |
1352 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
1353 | const struct bpf_reg_state *reg, | |
d1174416 | 1354 | int off, int size, bool strict) |
969bf05e | 1355 | { |
f1174f77 | 1356 | struct tnum reg_off; |
e07b98d9 | 1357 | int ip_align; |
d1174416 DM |
1358 | |
1359 | /* Byte size accesses are always allowed. */ | |
1360 | if (!strict || size == 1) | |
1361 | return 0; | |
1362 | ||
e4eda884 DM |
1363 | /* For platforms that do not have a Kconfig enabling |
1364 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
1365 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
1366 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
1367 | * to this code only in strict mode where we want to emulate | |
1368 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
1369 | * unconditional IP align value of '2'. | |
e07b98d9 | 1370 | */ |
e4eda884 | 1371 | ip_align = 2; |
f1174f77 EC |
1372 | |
1373 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
1374 | if (!tnum_is_aligned(reg_off, size)) { | |
1375 | char tn_buf[48]; | |
1376 | ||
1377 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
1378 | verbose(env, |
1379 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 1380 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
1381 | return -EACCES; |
1382 | } | |
79adffcd | 1383 | |
969bf05e AS |
1384 | return 0; |
1385 | } | |
1386 | ||
61bd5218 JK |
1387 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
1388 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
1389 | const char *pointer_desc, |
1390 | int off, int size, bool strict) | |
79adffcd | 1391 | { |
f1174f77 EC |
1392 | struct tnum reg_off; |
1393 | ||
1394 | /* Byte size accesses are always allowed. */ | |
1395 | if (!strict || size == 1) | |
1396 | return 0; | |
1397 | ||
1398 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
1399 | if (!tnum_is_aligned(reg_off, size)) { | |
1400 | char tn_buf[48]; | |
1401 | ||
1402 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 1403 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 1404 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
1405 | return -EACCES; |
1406 | } | |
1407 | ||
969bf05e AS |
1408 | return 0; |
1409 | } | |
1410 | ||
e07b98d9 DM |
1411 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
1412 | const struct bpf_reg_state *reg, | |
79adffcd DB |
1413 | int off, int size) |
1414 | { | |
e07b98d9 | 1415 | bool strict = env->strict_alignment; |
f1174f77 | 1416 | const char *pointer_desc = ""; |
d1174416 | 1417 | |
79adffcd DB |
1418 | switch (reg->type) { |
1419 | case PTR_TO_PACKET: | |
de8f3a83 DB |
1420 | case PTR_TO_PACKET_META: |
1421 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
1422 | * right in front, treat it the very same way. | |
1423 | */ | |
61bd5218 | 1424 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
f1174f77 EC |
1425 | case PTR_TO_MAP_VALUE: |
1426 | pointer_desc = "value "; | |
1427 | break; | |
1428 | case PTR_TO_CTX: | |
1429 | pointer_desc = "context "; | |
1430 | break; | |
1431 | case PTR_TO_STACK: | |
1432 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
1433 | /* The stack spill tracking logic in check_stack_write() |
1434 | * and check_stack_read() relies on stack accesses being | |
1435 | * aligned. | |
1436 | */ | |
1437 | strict = true; | |
f1174f77 | 1438 | break; |
79adffcd | 1439 | default: |
f1174f77 | 1440 | break; |
79adffcd | 1441 | } |
61bd5218 JK |
1442 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
1443 | strict); | |
79adffcd DB |
1444 | } |
1445 | ||
f4d7e40a AS |
1446 | static int update_stack_depth(struct bpf_verifier_env *env, |
1447 | const struct bpf_func_state *func, | |
1448 | int off) | |
1449 | { | |
70a87ffe | 1450 | u16 stack = env->subprog_stack_depth[func->subprogno]; |
f4d7e40a AS |
1451 | |
1452 | if (stack >= -off) | |
1453 | return 0; | |
1454 | ||
1455 | /* update known max for given subprogram */ | |
1456 | env->subprog_stack_depth[func->subprogno] = -off; | |
70a87ffe AS |
1457 | return 0; |
1458 | } | |
f4d7e40a | 1459 | |
70a87ffe AS |
1460 | /* starting from main bpf function walk all instructions of the function |
1461 | * and recursively walk all callees that given function can call. | |
1462 | * Ignore jump and exit insns. | |
1463 | * Since recursion is prevented by check_cfg() this algorithm | |
1464 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
1465 | */ | |
1466 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
1467 | { | |
1468 | int depth = 0, frame = 0, subprog = 0, i = 0, subprog_end; | |
1469 | struct bpf_insn *insn = env->prog->insnsi; | |
1470 | int insn_cnt = env->prog->len; | |
1471 | int ret_insn[MAX_CALL_FRAMES]; | |
1472 | int ret_prog[MAX_CALL_FRAMES]; | |
f4d7e40a | 1473 | |
70a87ffe AS |
1474 | process_func: |
1475 | /* round up to 32-bytes, since this is granularity | |
1476 | * of interpreter stack size | |
1477 | */ | |
1478 | depth += round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); | |
1479 | if (depth > MAX_BPF_STACK) { | |
f4d7e40a | 1480 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 1481 | frame + 1, depth); |
f4d7e40a AS |
1482 | return -EACCES; |
1483 | } | |
70a87ffe AS |
1484 | continue_func: |
1485 | if (env->subprog_cnt == subprog) | |
1486 | subprog_end = insn_cnt; | |
1487 | else | |
1488 | subprog_end = env->subprog_starts[subprog]; | |
1489 | for (; i < subprog_end; i++) { | |
1490 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1491 | continue; | |
1492 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1493 | continue; | |
1494 | /* remember insn and function to return to */ | |
1495 | ret_insn[frame] = i + 1; | |
1496 | ret_prog[frame] = subprog; | |
1497 | ||
1498 | /* find the callee */ | |
1499 | i = i + insn[i].imm + 1; | |
1500 | subprog = find_subprog(env, i); | |
1501 | if (subprog < 0) { | |
1502 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
1503 | i); | |
1504 | return -EFAULT; | |
1505 | } | |
1506 | subprog++; | |
1507 | frame++; | |
1508 | if (frame >= MAX_CALL_FRAMES) { | |
1509 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); | |
1510 | return -EFAULT; | |
1511 | } | |
1512 | goto process_func; | |
1513 | } | |
1514 | /* end of for() loop means the last insn of the 'subprog' | |
1515 | * was reached. Doesn't matter whether it was JA or EXIT | |
1516 | */ | |
1517 | if (frame == 0) | |
1518 | return 0; | |
1519 | depth -= round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); | |
1520 | frame--; | |
1521 | i = ret_insn[frame]; | |
1522 | subprog = ret_prog[frame]; | |
1523 | goto continue_func; | |
f4d7e40a AS |
1524 | } |
1525 | ||
19d28fbd | 1526 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
1527 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
1528 | const struct bpf_insn *insn, int idx) | |
1529 | { | |
1530 | int start = idx + insn->imm + 1, subprog; | |
1531 | ||
1532 | subprog = find_subprog(env, start); | |
1533 | if (subprog < 0) { | |
1534 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
1535 | start); | |
1536 | return -EFAULT; | |
1537 | } | |
1538 | subprog++; | |
1539 | return env->subprog_stack_depth[subprog]; | |
1540 | } | |
19d28fbd | 1541 | #endif |
1ea47e01 | 1542 | |
0c17d1d2 JH |
1543 | /* truncate register to smaller size (in bytes) |
1544 | * must be called with size < BPF_REG_SIZE | |
1545 | */ | |
1546 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
1547 | { | |
1548 | u64 mask; | |
1549 | ||
1550 | /* clear high bits in bit representation */ | |
1551 | reg->var_off = tnum_cast(reg->var_off, size); | |
1552 | ||
1553 | /* fix arithmetic bounds */ | |
1554 | mask = ((u64)1 << (size * 8)) - 1; | |
1555 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
1556 | reg->umin_value &= mask; | |
1557 | reg->umax_value &= mask; | |
1558 | } else { | |
1559 | reg->umin_value = 0; | |
1560 | reg->umax_value = mask; | |
1561 | } | |
1562 | reg->smin_value = reg->umin_value; | |
1563 | reg->smax_value = reg->umax_value; | |
1564 | } | |
1565 | ||
17a52670 AS |
1566 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
1567 | * if t==write, value_regno is a register which value is stored into memory | |
1568 | * if t==read, value_regno is a register which will receive the value from memory | |
1569 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
1570 | * if t==read && value_regno==-1, don't care what we read from memory | |
1571 | */ | |
31fd8581 | 1572 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off, |
17a52670 AS |
1573 | int bpf_size, enum bpf_access_type t, |
1574 | int value_regno) | |
1575 | { | |
638f5b90 AS |
1576 | struct bpf_reg_state *regs = cur_regs(env); |
1577 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 1578 | struct bpf_func_state *state; |
17a52670 AS |
1579 | int size, err = 0; |
1580 | ||
1581 | size = bpf_size_to_bytes(bpf_size); | |
1582 | if (size < 0) | |
1583 | return size; | |
1584 | ||
f1174f77 | 1585 | /* alignment checks will add in reg->off themselves */ |
e07b98d9 | 1586 | err = check_ptr_alignment(env, reg, off, size); |
969bf05e AS |
1587 | if (err) |
1588 | return err; | |
17a52670 | 1589 | |
f1174f77 EC |
1590 | /* for access checks, reg->off is just part of off */ |
1591 | off += reg->off; | |
1592 | ||
1593 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
1594 | if (t == BPF_WRITE && value_regno >= 0 && |
1595 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 1596 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
1597 | return -EACCES; |
1598 | } | |
48461135 | 1599 | |
9fd29c08 | 1600 | err = check_map_access(env, regno, off, size, false); |
17a52670 | 1601 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 1602 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 1603 | |
1a0dc1ac | 1604 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 1605 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
19de99f7 | 1606 | |
1be7f75d AS |
1607 | if (t == BPF_WRITE && value_regno >= 0 && |
1608 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 1609 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
1610 | return -EACCES; |
1611 | } | |
f1174f77 EC |
1612 | /* ctx accesses must be at a fixed offset, so that we can |
1613 | * determine what type of data were returned. | |
1614 | */ | |
28e33f9d | 1615 | if (reg->off) { |
f8ddadc4 DM |
1616 | verbose(env, |
1617 | "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n", | |
28e33f9d JK |
1618 | regno, reg->off, off - reg->off); |
1619 | return -EACCES; | |
1620 | } | |
1621 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
f1174f77 EC |
1622 | char tn_buf[48]; |
1623 | ||
1624 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
1625 | verbose(env, |
1626 | "variable ctx access var_off=%s off=%d size=%d", | |
f1174f77 EC |
1627 | tn_buf, off, size); |
1628 | return -EACCES; | |
1629 | } | |
31fd8581 | 1630 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
969bf05e | 1631 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 1632 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
1633 | * PTR_TO_PACKET[_META,_END]. In the latter |
1634 | * case, we know the offset is zero. | |
f1174f77 EC |
1635 | */ |
1636 | if (reg_type == SCALAR_VALUE) | |
638f5b90 | 1637 | mark_reg_unknown(env, regs, value_regno); |
f1174f77 | 1638 | else |
638f5b90 | 1639 | mark_reg_known_zero(env, regs, |
61bd5218 | 1640 | value_regno); |
638f5b90 AS |
1641 | regs[value_regno].id = 0; |
1642 | regs[value_regno].off = 0; | |
1643 | regs[value_regno].range = 0; | |
1644 | regs[value_regno].type = reg_type; | |
969bf05e | 1645 | } |
17a52670 | 1646 | |
f1174f77 EC |
1647 | } else if (reg->type == PTR_TO_STACK) { |
1648 | /* stack accesses must be at a fixed offset, so that we can | |
1649 | * determine what type of data were returned. | |
1650 | * See check_stack_read(). | |
1651 | */ | |
1652 | if (!tnum_is_const(reg->var_off)) { | |
1653 | char tn_buf[48]; | |
1654 | ||
1655 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 1656 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
f1174f77 EC |
1657 | tn_buf, off, size); |
1658 | return -EACCES; | |
1659 | } | |
1660 | off += reg->var_off.value; | |
17a52670 | 1661 | if (off >= 0 || off < -MAX_BPF_STACK) { |
61bd5218 JK |
1662 | verbose(env, "invalid stack off=%d size=%d\n", off, |
1663 | size); | |
17a52670 AS |
1664 | return -EACCES; |
1665 | } | |
8726679a | 1666 | |
f4d7e40a AS |
1667 | state = func(env, reg); |
1668 | err = update_stack_depth(env, state, off); | |
1669 | if (err) | |
1670 | return err; | |
8726679a | 1671 | |
638f5b90 | 1672 | if (t == BPF_WRITE) |
61bd5218 JK |
1673 | err = check_stack_write(env, state, off, size, |
1674 | value_regno); | |
638f5b90 | 1675 | else |
61bd5218 JK |
1676 | err = check_stack_read(env, state, off, size, |
1677 | value_regno); | |
de8f3a83 | 1678 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 1679 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 1680 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
1681 | return -EACCES; |
1682 | } | |
4acf6c0b BB |
1683 | if (t == BPF_WRITE && value_regno >= 0 && |
1684 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
1685 | verbose(env, "R%d leaks addr into packet\n", |
1686 | value_regno); | |
4acf6c0b BB |
1687 | return -EACCES; |
1688 | } | |
9fd29c08 | 1689 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 1690 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 1691 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 1692 | } else { |
61bd5218 JK |
1693 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
1694 | reg_type_str[reg->type]); | |
17a52670 AS |
1695 | return -EACCES; |
1696 | } | |
969bf05e | 1697 | |
f1174f77 | 1698 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 1699 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 1700 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 1701 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 1702 | } |
17a52670 AS |
1703 | return err; |
1704 | } | |
1705 | ||
31fd8581 | 1706 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 1707 | { |
17a52670 AS |
1708 | int err; |
1709 | ||
1710 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
1711 | insn->imm != 0) { | |
61bd5218 | 1712 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
1713 | return -EINVAL; |
1714 | } | |
1715 | ||
1716 | /* check src1 operand */ | |
dc503a8a | 1717 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
1718 | if (err) |
1719 | return err; | |
1720 | ||
1721 | /* check src2 operand */ | |
dc503a8a | 1722 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
1723 | if (err) |
1724 | return err; | |
1725 | ||
6bdf6abc | 1726 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 1727 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
1728 | return -EACCES; |
1729 | } | |
1730 | ||
17a52670 | 1731 | /* check whether atomic_add can read the memory */ |
31fd8581 | 1732 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
1733 | BPF_SIZE(insn->code), BPF_READ, -1); |
1734 | if (err) | |
1735 | return err; | |
1736 | ||
1737 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 1738 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
1739 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
1740 | } | |
1741 | ||
1742 | /* when register 'regno' is passed into function that will read 'access_size' | |
1743 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
1744 | * and all elements of stack are initialized. |
1745 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
1746 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 1747 | */ |
58e2af8b | 1748 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
1749 | int access_size, bool zero_size_allowed, |
1750 | struct bpf_call_arg_meta *meta) | |
17a52670 | 1751 | { |
914cb781 | 1752 | struct bpf_reg_state *reg = cur_regs(env) + regno; |
f4d7e40a | 1753 | struct bpf_func_state *state = func(env, reg); |
638f5b90 | 1754 | int off, i, slot, spi; |
17a52670 | 1755 | |
914cb781 | 1756 | if (reg->type != PTR_TO_STACK) { |
f1174f77 | 1757 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 1758 | if (zero_size_allowed && access_size == 0 && |
914cb781 | 1759 | register_is_null(reg)) |
8e2fe1d9 DB |
1760 | return 0; |
1761 | ||
61bd5218 | 1762 | verbose(env, "R%d type=%s expected=%s\n", regno, |
914cb781 | 1763 | reg_type_str[reg->type], |
8e2fe1d9 | 1764 | reg_type_str[PTR_TO_STACK]); |
17a52670 | 1765 | return -EACCES; |
8e2fe1d9 | 1766 | } |
17a52670 | 1767 | |
f1174f77 | 1768 | /* Only allow fixed-offset stack reads */ |
914cb781 | 1769 | if (!tnum_is_const(reg->var_off)) { |
f1174f77 EC |
1770 | char tn_buf[48]; |
1771 | ||
914cb781 | 1772 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 1773 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
f1174f77 | 1774 | regno, tn_buf); |
ea25f914 | 1775 | return -EACCES; |
f1174f77 | 1776 | } |
914cb781 | 1777 | off = reg->off + reg->var_off.value; |
17a52670 | 1778 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
9fd29c08 | 1779 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
61bd5218 | 1780 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
17a52670 AS |
1781 | regno, off, access_size); |
1782 | return -EACCES; | |
1783 | } | |
1784 | ||
435faee1 DB |
1785 | if (meta && meta->raw_mode) { |
1786 | meta->access_size = access_size; | |
1787 | meta->regno = regno; | |
1788 | return 0; | |
1789 | } | |
1790 | ||
17a52670 | 1791 | for (i = 0; i < access_size; i++) { |
cc2b14d5 AS |
1792 | u8 *stype; |
1793 | ||
638f5b90 AS |
1794 | slot = -(off + i) - 1; |
1795 | spi = slot / BPF_REG_SIZE; | |
cc2b14d5 AS |
1796 | if (state->allocated_stack <= slot) |
1797 | goto err; | |
1798 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
1799 | if (*stype == STACK_MISC) | |
1800 | goto mark; | |
1801 | if (*stype == STACK_ZERO) { | |
1802 | /* helper can write anything into the stack */ | |
1803 | *stype = STACK_MISC; | |
1804 | goto mark; | |
17a52670 | 1805 | } |
cc2b14d5 AS |
1806 | err: |
1807 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
1808 | off, i, access_size); | |
1809 | return -EACCES; | |
1810 | mark: | |
1811 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
1812 | * the whole slot to be marked as 'read' | |
1813 | */ | |
1814 | mark_stack_slot_read(env, env->cur_state, env->cur_state->parent, | |
1815 | spi, state->frameno); | |
17a52670 | 1816 | } |
f4d7e40a | 1817 | return update_stack_depth(env, state, off); |
17a52670 AS |
1818 | } |
1819 | ||
06c1c049 GB |
1820 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
1821 | int access_size, bool zero_size_allowed, | |
1822 | struct bpf_call_arg_meta *meta) | |
1823 | { | |
638f5b90 | 1824 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 1825 | |
f1174f77 | 1826 | switch (reg->type) { |
06c1c049 | 1827 | case PTR_TO_PACKET: |
de8f3a83 | 1828 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
1829 | return check_packet_access(env, regno, reg->off, access_size, |
1830 | zero_size_allowed); | |
06c1c049 | 1831 | case PTR_TO_MAP_VALUE: |
9fd29c08 YS |
1832 | return check_map_access(env, regno, reg->off, access_size, |
1833 | zero_size_allowed); | |
f1174f77 | 1834 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
1835 | return check_stack_boundary(env, regno, access_size, |
1836 | zero_size_allowed, meta); | |
1837 | } | |
1838 | } | |
1839 | ||
58e2af8b | 1840 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
1841 | enum bpf_arg_type arg_type, |
1842 | struct bpf_call_arg_meta *meta) | |
17a52670 | 1843 | { |
638f5b90 | 1844 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 1845 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
1846 | int err = 0; |
1847 | ||
80f1d68c | 1848 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
1849 | return 0; |
1850 | ||
dc503a8a EC |
1851 | err = check_reg_arg(env, regno, SRC_OP); |
1852 | if (err) | |
1853 | return err; | |
17a52670 | 1854 | |
1be7f75d AS |
1855 | if (arg_type == ARG_ANYTHING) { |
1856 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
1857 | verbose(env, "R%d leaks addr into helper function\n", |
1858 | regno); | |
1be7f75d AS |
1859 | return -EACCES; |
1860 | } | |
80f1d68c | 1861 | return 0; |
1be7f75d | 1862 | } |
80f1d68c | 1863 | |
de8f3a83 | 1864 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 1865 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 1866 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
1867 | return -EACCES; |
1868 | } | |
1869 | ||
8e2fe1d9 | 1870 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
17a52670 AS |
1871 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
1872 | expected_type = PTR_TO_STACK; | |
de8f3a83 DB |
1873 | if (!type_is_pkt_pointer(type) && |
1874 | type != expected_type) | |
6841de8b | 1875 | goto err_type; |
39f19ebb AS |
1876 | } else if (arg_type == ARG_CONST_SIZE || |
1877 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
f1174f77 EC |
1878 | expected_type = SCALAR_VALUE; |
1879 | if (type != expected_type) | |
6841de8b | 1880 | goto err_type; |
17a52670 AS |
1881 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
1882 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
1883 | if (type != expected_type) |
1884 | goto err_type; | |
608cd71a AS |
1885 | } else if (arg_type == ARG_PTR_TO_CTX) { |
1886 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
1887 | if (type != expected_type) |
1888 | goto err_type; | |
39f19ebb | 1889 | } else if (arg_type == ARG_PTR_TO_MEM || |
db1ac496 | 1890 | arg_type == ARG_PTR_TO_MEM_OR_NULL || |
39f19ebb | 1891 | arg_type == ARG_PTR_TO_UNINIT_MEM) { |
8e2fe1d9 DB |
1892 | expected_type = PTR_TO_STACK; |
1893 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 1894 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
1895 | * happens during stack boundary checking. |
1896 | */ | |
914cb781 | 1897 | if (register_is_null(reg) && |
db1ac496 | 1898 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
6841de8b | 1899 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
1900 | else if (!type_is_pkt_pointer(type) && |
1901 | type != PTR_TO_MAP_VALUE && | |
f1174f77 | 1902 | type != expected_type) |
6841de8b | 1903 | goto err_type; |
39f19ebb | 1904 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
17a52670 | 1905 | } else { |
61bd5218 | 1906 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
1907 | return -EFAULT; |
1908 | } | |
1909 | ||
17a52670 AS |
1910 | if (arg_type == ARG_CONST_MAP_PTR) { |
1911 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 1912 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
1913 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
1914 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
1915 | * check that [key, key + map->key_size) are within | |
1916 | * stack limits and initialized | |
1917 | */ | |
33ff9823 | 1918 | if (!meta->map_ptr) { |
17a52670 AS |
1919 | /* in function declaration map_ptr must come before |
1920 | * map_key, so that it's verified and known before | |
1921 | * we have to check map_key here. Otherwise it means | |
1922 | * that kernel subsystem misconfigured verifier | |
1923 | */ | |
61bd5218 | 1924 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
1925 | return -EACCES; |
1926 | } | |
de8f3a83 | 1927 | if (type_is_pkt_pointer(type)) |
f1174f77 | 1928 | err = check_packet_access(env, regno, reg->off, |
9fd29c08 YS |
1929 | meta->map_ptr->key_size, |
1930 | false); | |
6841de8b AS |
1931 | else |
1932 | err = check_stack_boundary(env, regno, | |
1933 | meta->map_ptr->key_size, | |
1934 | false, NULL); | |
17a52670 AS |
1935 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
1936 | /* bpf_map_xxx(..., map_ptr, ..., value) call: | |
1937 | * check [value, value + map->value_size) validity | |
1938 | */ | |
33ff9823 | 1939 | if (!meta->map_ptr) { |
17a52670 | 1940 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 1941 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
1942 | return -EACCES; |
1943 | } | |
de8f3a83 | 1944 | if (type_is_pkt_pointer(type)) |
f1174f77 | 1945 | err = check_packet_access(env, regno, reg->off, |
9fd29c08 YS |
1946 | meta->map_ptr->value_size, |
1947 | false); | |
6841de8b AS |
1948 | else |
1949 | err = check_stack_boundary(env, regno, | |
1950 | meta->map_ptr->value_size, | |
1951 | false, NULL); | |
39f19ebb AS |
1952 | } else if (arg_type == ARG_CONST_SIZE || |
1953 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
1954 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); | |
17a52670 | 1955 | |
17a52670 AS |
1956 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
1957 | * from stack pointer 'buf'. Check it | |
1958 | * note: regno == len, regno - 1 == buf | |
1959 | */ | |
1960 | if (regno == 0) { | |
1961 | /* kernel subsystem misconfigured verifier */ | |
61bd5218 JK |
1962 | verbose(env, |
1963 | "ARG_CONST_SIZE cannot be first argument\n"); | |
17a52670 AS |
1964 | return -EACCES; |
1965 | } | |
06c1c049 | 1966 | |
f1174f77 EC |
1967 | /* The register is SCALAR_VALUE; the access check |
1968 | * happens using its boundaries. | |
06c1c049 | 1969 | */ |
f1174f77 EC |
1970 | |
1971 | if (!tnum_is_const(reg->var_off)) | |
06c1c049 GB |
1972 | /* For unprivileged variable accesses, disable raw |
1973 | * mode so that the program is required to | |
1974 | * initialize all the memory that the helper could | |
1975 | * just partially fill up. | |
1976 | */ | |
1977 | meta = NULL; | |
1978 | ||
b03c9f9f | 1979 | if (reg->smin_value < 0) { |
61bd5218 | 1980 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
1981 | regno); |
1982 | return -EACCES; | |
1983 | } | |
06c1c049 | 1984 | |
b03c9f9f | 1985 | if (reg->umin_value == 0) { |
f1174f77 EC |
1986 | err = check_helper_mem_access(env, regno - 1, 0, |
1987 | zero_size_allowed, | |
1988 | meta); | |
06c1c049 GB |
1989 | if (err) |
1990 | return err; | |
06c1c049 | 1991 | } |
f1174f77 | 1992 | |
b03c9f9f | 1993 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 1994 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
1995 | regno); |
1996 | return -EACCES; | |
1997 | } | |
1998 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 1999 | reg->umax_value, |
f1174f77 | 2000 | zero_size_allowed, meta); |
17a52670 AS |
2001 | } |
2002 | ||
2003 | return err; | |
6841de8b | 2004 | err_type: |
61bd5218 | 2005 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
2006 | reg_type_str[type], reg_type_str[expected_type]); |
2007 | return -EACCES; | |
17a52670 AS |
2008 | } |
2009 | ||
61bd5218 JK |
2010 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
2011 | struct bpf_map *map, int func_id) | |
35578d79 | 2012 | { |
35578d79 KX |
2013 | if (!map) |
2014 | return 0; | |
2015 | ||
6aff67c8 AS |
2016 | /* We need a two way check, first is from map perspective ... */ |
2017 | switch (map->map_type) { | |
2018 | case BPF_MAP_TYPE_PROG_ARRAY: | |
2019 | if (func_id != BPF_FUNC_tail_call) | |
2020 | goto error; | |
2021 | break; | |
2022 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
2023 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca YS |
2024 | func_id != BPF_FUNC_perf_event_output && |
2025 | func_id != BPF_FUNC_perf_event_read_value) | |
6aff67c8 AS |
2026 | goto error; |
2027 | break; | |
2028 | case BPF_MAP_TYPE_STACK_TRACE: | |
2029 | if (func_id != BPF_FUNC_get_stackid) | |
2030 | goto error; | |
2031 | break; | |
4ed8ec52 | 2032 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 2033 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 2034 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
2035 | goto error; |
2036 | break; | |
546ac1ff JF |
2037 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
2038 | * allow to be modified from bpf side. So do not allow lookup elements | |
2039 | * for now. | |
2040 | */ | |
2041 | case BPF_MAP_TYPE_DEVMAP: | |
2ddf71e2 | 2042 | if (func_id != BPF_FUNC_redirect_map) |
546ac1ff JF |
2043 | goto error; |
2044 | break; | |
6710e112 JDB |
2045 | /* Restrict bpf side of cpumap, open when use-cases appear */ |
2046 | case BPF_MAP_TYPE_CPUMAP: | |
2047 | if (func_id != BPF_FUNC_redirect_map) | |
2048 | goto error; | |
2049 | break; | |
56f668df | 2050 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 2051 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
2052 | if (func_id != BPF_FUNC_map_lookup_elem) |
2053 | goto error; | |
16a43625 | 2054 | break; |
174a79ff JF |
2055 | case BPF_MAP_TYPE_SOCKMAP: |
2056 | if (func_id != BPF_FUNC_sk_redirect_map && | |
2057 | func_id != BPF_FUNC_sock_map_update && | |
2058 | func_id != BPF_FUNC_map_delete_elem) | |
2059 | goto error; | |
2060 | break; | |
6aff67c8 AS |
2061 | default: |
2062 | break; | |
2063 | } | |
2064 | ||
2065 | /* ... and second from the function itself. */ | |
2066 | switch (func_id) { | |
2067 | case BPF_FUNC_tail_call: | |
2068 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
2069 | goto error; | |
f4d7e40a AS |
2070 | if (env->subprog_cnt) { |
2071 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); | |
2072 | return -EINVAL; | |
2073 | } | |
6aff67c8 AS |
2074 | break; |
2075 | case BPF_FUNC_perf_event_read: | |
2076 | case BPF_FUNC_perf_event_output: | |
908432ca | 2077 | case BPF_FUNC_perf_event_read_value: |
6aff67c8 AS |
2078 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
2079 | goto error; | |
2080 | break; | |
2081 | case BPF_FUNC_get_stackid: | |
2082 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
2083 | goto error; | |
2084 | break; | |
60d20f91 | 2085 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 2086 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
2087 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
2088 | goto error; | |
2089 | break; | |
97f91a7c | 2090 | case BPF_FUNC_redirect_map: |
9c270af3 JDB |
2091 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
2092 | map->map_type != BPF_MAP_TYPE_CPUMAP) | |
97f91a7c JF |
2093 | goto error; |
2094 | break; | |
174a79ff JF |
2095 | case BPF_FUNC_sk_redirect_map: |
2096 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) | |
2097 | goto error; | |
2098 | break; | |
2099 | case BPF_FUNC_sock_map_update: | |
2100 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) | |
2101 | goto error; | |
2102 | break; | |
6aff67c8 AS |
2103 | default: |
2104 | break; | |
35578d79 KX |
2105 | } |
2106 | ||
2107 | return 0; | |
6aff67c8 | 2108 | error: |
61bd5218 | 2109 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 2110 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 2111 | return -EINVAL; |
35578d79 KX |
2112 | } |
2113 | ||
435faee1 DB |
2114 | static int check_raw_mode(const struct bpf_func_proto *fn) |
2115 | { | |
2116 | int count = 0; | |
2117 | ||
39f19ebb | 2118 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2119 | count++; |
39f19ebb | 2120 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2121 | count++; |
39f19ebb | 2122 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2123 | count++; |
39f19ebb | 2124 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2125 | count++; |
39f19ebb | 2126 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
2127 | count++; |
2128 | ||
2129 | return count > 1 ? -EINVAL : 0; | |
2130 | } | |
2131 | ||
de8f3a83 DB |
2132 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
2133 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 2134 | */ |
f4d7e40a AS |
2135 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
2136 | struct bpf_func_state *state) | |
969bf05e | 2137 | { |
58e2af8b | 2138 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
2139 | int i; |
2140 | ||
2141 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 2142 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 2143 | mark_reg_unknown(env, regs, i); |
969bf05e | 2144 | |
638f5b90 AS |
2145 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
2146 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
969bf05e | 2147 | continue; |
638f5b90 | 2148 | reg = &state->stack[i].spilled_ptr; |
de8f3a83 DB |
2149 | if (reg_is_pkt_pointer_any(reg)) |
2150 | __mark_reg_unknown(reg); | |
969bf05e AS |
2151 | } |
2152 | } | |
2153 | ||
f4d7e40a AS |
2154 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
2155 | { | |
2156 | struct bpf_verifier_state *vstate = env->cur_state; | |
2157 | int i; | |
2158 | ||
2159 | for (i = 0; i <= vstate->curframe; i++) | |
2160 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
2161 | } | |
2162 | ||
2163 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2164 | int *insn_idx) | |
2165 | { | |
2166 | struct bpf_verifier_state *state = env->cur_state; | |
2167 | struct bpf_func_state *caller, *callee; | |
2168 | int i, subprog, target_insn; | |
2169 | ||
aada9ce6 | 2170 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 2171 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 2172 | state->curframe + 2); |
f4d7e40a AS |
2173 | return -E2BIG; |
2174 | } | |
2175 | ||
2176 | target_insn = *insn_idx + insn->imm; | |
2177 | subprog = find_subprog(env, target_insn + 1); | |
2178 | if (subprog < 0) { | |
2179 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
2180 | target_insn + 1); | |
2181 | return -EFAULT; | |
2182 | } | |
2183 | ||
2184 | caller = state->frame[state->curframe]; | |
2185 | if (state->frame[state->curframe + 1]) { | |
2186 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
2187 | state->curframe + 1); | |
2188 | return -EFAULT; | |
2189 | } | |
2190 | ||
2191 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); | |
2192 | if (!callee) | |
2193 | return -ENOMEM; | |
2194 | state->frame[state->curframe + 1] = callee; | |
2195 | ||
2196 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
2197 | * into its own stack before reading from it. | |
2198 | * callee can read/write into caller's stack | |
2199 | */ | |
2200 | init_func_state(env, callee, | |
2201 | /* remember the callsite, it will be used by bpf_exit */ | |
2202 | *insn_idx /* callsite */, | |
2203 | state->curframe + 1 /* frameno within this callchain */, | |
2204 | subprog + 1 /* subprog number within this prog */); | |
2205 | ||
2206 | /* copy r1 - r5 args that callee can access */ | |
2207 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
2208 | callee->regs[i] = caller->regs[i]; | |
2209 | ||
2210 | /* after the call regsiters r0 - r5 were scratched */ | |
2211 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
2212 | mark_reg_not_init(env, caller->regs, caller_saved[i]); | |
2213 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
2214 | } | |
2215 | ||
2216 | /* only increment it after check_reg_arg() finished */ | |
2217 | state->curframe++; | |
2218 | ||
2219 | /* and go analyze first insn of the callee */ | |
2220 | *insn_idx = target_insn; | |
2221 | ||
2222 | if (env->log.level) { | |
2223 | verbose(env, "caller:\n"); | |
2224 | print_verifier_state(env, caller); | |
2225 | verbose(env, "callee:\n"); | |
2226 | print_verifier_state(env, callee); | |
2227 | } | |
2228 | return 0; | |
2229 | } | |
2230 | ||
2231 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
2232 | { | |
2233 | struct bpf_verifier_state *state = env->cur_state; | |
2234 | struct bpf_func_state *caller, *callee; | |
2235 | struct bpf_reg_state *r0; | |
2236 | ||
2237 | callee = state->frame[state->curframe]; | |
2238 | r0 = &callee->regs[BPF_REG_0]; | |
2239 | if (r0->type == PTR_TO_STACK) { | |
2240 | /* technically it's ok to return caller's stack pointer | |
2241 | * (or caller's caller's pointer) back to the caller, | |
2242 | * since these pointers are valid. Only current stack | |
2243 | * pointer will be invalid as soon as function exits, | |
2244 | * but let's be conservative | |
2245 | */ | |
2246 | verbose(env, "cannot return stack pointer to the caller\n"); | |
2247 | return -EINVAL; | |
2248 | } | |
2249 | ||
2250 | state->curframe--; | |
2251 | caller = state->frame[state->curframe]; | |
2252 | /* return to the caller whatever r0 had in the callee */ | |
2253 | caller->regs[BPF_REG_0] = *r0; | |
2254 | ||
2255 | *insn_idx = callee->callsite + 1; | |
2256 | if (env->log.level) { | |
2257 | verbose(env, "returning from callee:\n"); | |
2258 | print_verifier_state(env, callee); | |
2259 | verbose(env, "to caller at %d:\n", *insn_idx); | |
2260 | print_verifier_state(env, caller); | |
2261 | } | |
2262 | /* clear everything in the callee */ | |
2263 | free_func_state(callee); | |
2264 | state->frame[state->curframe + 1] = NULL; | |
2265 | return 0; | |
2266 | } | |
2267 | ||
2268 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) | |
17a52670 | 2269 | { |
17a52670 | 2270 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 2271 | struct bpf_reg_state *regs; |
33ff9823 | 2272 | struct bpf_call_arg_meta meta; |
969bf05e | 2273 | bool changes_data; |
17a52670 AS |
2274 | int i, err; |
2275 | ||
2276 | /* find function prototype */ | |
2277 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
2278 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
2279 | func_id); | |
17a52670 AS |
2280 | return -EINVAL; |
2281 | } | |
2282 | ||
00176a34 JK |
2283 | if (env->ops->get_func_proto) |
2284 | fn = env->ops->get_func_proto(func_id); | |
17a52670 AS |
2285 | |
2286 | if (!fn) { | |
61bd5218 JK |
2287 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
2288 | func_id); | |
17a52670 AS |
2289 | return -EINVAL; |
2290 | } | |
2291 | ||
2292 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 2293 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
61bd5218 | 2294 | verbose(env, "cannot call GPL only function from proprietary program\n"); |
17a52670 AS |
2295 | return -EINVAL; |
2296 | } | |
2297 | ||
04514d13 | 2298 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 2299 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
2300 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
2301 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
2302 | func_id_name(func_id), func_id); | |
2303 | return -EINVAL; | |
2304 | } | |
969bf05e | 2305 | |
33ff9823 | 2306 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 2307 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 2308 | |
435faee1 DB |
2309 | /* We only support one arg being in raw mode at the moment, which |
2310 | * is sufficient for the helper functions we have right now. | |
2311 | */ | |
2312 | err = check_raw_mode(fn); | |
2313 | if (err) { | |
61bd5218 | 2314 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 2315 | func_id_name(func_id), func_id); |
435faee1 DB |
2316 | return err; |
2317 | } | |
2318 | ||
17a52670 | 2319 | /* check args */ |
33ff9823 | 2320 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
2321 | if (err) |
2322 | return err; | |
33ff9823 | 2323 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
2324 | if (err) |
2325 | return err; | |
b2157399 AS |
2326 | if (func_id == BPF_FUNC_tail_call) { |
2327 | if (meta.map_ptr == NULL) { | |
2328 | verbose(env, "verifier bug\n"); | |
2329 | return -EINVAL; | |
2330 | } | |
2331 | env->insn_aux_data[insn_idx].map_ptr = meta.map_ptr; | |
2332 | } | |
33ff9823 | 2333 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
2334 | if (err) |
2335 | return err; | |
33ff9823 | 2336 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
2337 | if (err) |
2338 | return err; | |
33ff9823 | 2339 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
2340 | if (err) |
2341 | return err; | |
2342 | ||
435faee1 DB |
2343 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
2344 | * is inferred from register state. | |
2345 | */ | |
2346 | for (i = 0; i < meta.access_size; i++) { | |
31fd8581 | 2347 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1); |
435faee1 DB |
2348 | if (err) |
2349 | return err; | |
2350 | } | |
2351 | ||
638f5b90 | 2352 | regs = cur_regs(env); |
17a52670 | 2353 | /* reset caller saved regs */ |
dc503a8a | 2354 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 2355 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
2356 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
2357 | } | |
17a52670 | 2358 | |
dc503a8a | 2359 | /* update return register (already marked as written above) */ |
17a52670 | 2360 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 2361 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 2362 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
2363 | } else if (fn->ret_type == RET_VOID) { |
2364 | regs[BPF_REG_0].type = NOT_INIT; | |
2365 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { | |
fad73a1a MKL |
2366 | struct bpf_insn_aux_data *insn_aux; |
2367 | ||
17a52670 | 2368 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
f1174f77 | 2369 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 2370 | mark_reg_known_zero(env, regs, BPF_REG_0); |
f1174f77 | 2371 | regs[BPF_REG_0].off = 0; |
17a52670 AS |
2372 | /* remember map_ptr, so that check_map_access() |
2373 | * can check 'value_size' boundary of memory access | |
2374 | * to map element returned from bpf_map_lookup_elem() | |
2375 | */ | |
33ff9823 | 2376 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
2377 | verbose(env, |
2378 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
2379 | return -EINVAL; |
2380 | } | |
33ff9823 | 2381 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
57a09bf0 | 2382 | regs[BPF_REG_0].id = ++env->id_gen; |
fad73a1a MKL |
2383 | insn_aux = &env->insn_aux_data[insn_idx]; |
2384 | if (!insn_aux->map_ptr) | |
2385 | insn_aux->map_ptr = meta.map_ptr; | |
2386 | else if (insn_aux->map_ptr != meta.map_ptr) | |
2387 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; | |
17a52670 | 2388 | } else { |
61bd5218 | 2389 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 2390 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
2391 | return -EINVAL; |
2392 | } | |
04fd61ab | 2393 | |
61bd5218 | 2394 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
2395 | if (err) |
2396 | return err; | |
04fd61ab | 2397 | |
969bf05e AS |
2398 | if (changes_data) |
2399 | clear_all_pkt_pointers(env); | |
2400 | return 0; | |
2401 | } | |
2402 | ||
b03c9f9f EC |
2403 | static bool signed_add_overflows(s64 a, s64 b) |
2404 | { | |
2405 | /* Do the add in u64, where overflow is well-defined */ | |
2406 | s64 res = (s64)((u64)a + (u64)b); | |
2407 | ||
2408 | if (b < 0) | |
2409 | return res > a; | |
2410 | return res < a; | |
2411 | } | |
2412 | ||
2413 | static bool signed_sub_overflows(s64 a, s64 b) | |
2414 | { | |
2415 | /* Do the sub in u64, where overflow is well-defined */ | |
2416 | s64 res = (s64)((u64)a - (u64)b); | |
2417 | ||
2418 | if (b < 0) | |
2419 | return res < a; | |
2420 | return res > a; | |
969bf05e AS |
2421 | } |
2422 | ||
bb7f0f98 AS |
2423 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
2424 | const struct bpf_reg_state *reg, | |
2425 | enum bpf_reg_type type) | |
2426 | { | |
2427 | bool known = tnum_is_const(reg->var_off); | |
2428 | s64 val = reg->var_off.value; | |
2429 | s64 smin = reg->smin_value; | |
2430 | ||
2431 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
2432 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
2433 | reg_type_str[type], val); | |
2434 | return false; | |
2435 | } | |
2436 | ||
2437 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
2438 | verbose(env, "%s pointer offset %d is not allowed\n", | |
2439 | reg_type_str[type], reg->off); | |
2440 | return false; | |
2441 | } | |
2442 | ||
2443 | if (smin == S64_MIN) { | |
2444 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
2445 | reg_type_str[type]); | |
2446 | return false; | |
2447 | } | |
2448 | ||
2449 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
2450 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
2451 | smin, reg_type_str[type]); | |
2452 | return false; | |
2453 | } | |
2454 | ||
2455 | return true; | |
2456 | } | |
2457 | ||
f1174f77 | 2458 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
2459 | * Caller should also handle BPF_MOV case separately. |
2460 | * If we return -EACCES, caller may want to try again treating pointer as a | |
2461 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
2462 | */ | |
2463 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
2464 | struct bpf_insn *insn, | |
2465 | const struct bpf_reg_state *ptr_reg, | |
2466 | const struct bpf_reg_state *off_reg) | |
969bf05e | 2467 | { |
f4d7e40a AS |
2468 | struct bpf_verifier_state *vstate = env->cur_state; |
2469 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
2470 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 2471 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
2472 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
2473 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
2474 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
2475 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
969bf05e | 2476 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 2477 | u32 dst = insn->dst_reg; |
969bf05e | 2478 | |
f1174f77 | 2479 | dst_reg = ®s[dst]; |
969bf05e | 2480 | |
b03c9f9f | 2481 | if (WARN_ON_ONCE(known && (smin_val != smax_val))) { |
f4d7e40a | 2482 | print_verifier_state(env, state); |
61bd5218 JK |
2483 | verbose(env, |
2484 | "verifier internal error: known but bad sbounds\n"); | |
b03c9f9f EC |
2485 | return -EINVAL; |
2486 | } | |
2487 | if (WARN_ON_ONCE(known && (umin_val != umax_val))) { | |
f4d7e40a | 2488 | print_verifier_state(env, state); |
61bd5218 JK |
2489 | verbose(env, |
2490 | "verifier internal error: known but bad ubounds\n"); | |
f1174f77 EC |
2491 | return -EINVAL; |
2492 | } | |
2493 | ||
2494 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
2495 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
82abbf8d AS |
2496 | verbose(env, |
2497 | "R%d 32-bit pointer arithmetic prohibited\n", | |
2498 | dst); | |
f1174f77 | 2499 | return -EACCES; |
969bf05e AS |
2500 | } |
2501 | ||
f1174f77 | 2502 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
82abbf8d AS |
2503 | verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n", |
2504 | dst); | |
f1174f77 EC |
2505 | return -EACCES; |
2506 | } | |
2507 | if (ptr_reg->type == CONST_PTR_TO_MAP) { | |
82abbf8d AS |
2508 | verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n", |
2509 | dst); | |
f1174f77 EC |
2510 | return -EACCES; |
2511 | } | |
2512 | if (ptr_reg->type == PTR_TO_PACKET_END) { | |
82abbf8d AS |
2513 | verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n", |
2514 | dst); | |
f1174f77 EC |
2515 | return -EACCES; |
2516 | } | |
2517 | ||
2518 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
2519 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 2520 | */ |
f1174f77 EC |
2521 | dst_reg->type = ptr_reg->type; |
2522 | dst_reg->id = ptr_reg->id; | |
969bf05e | 2523 | |
bb7f0f98 AS |
2524 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
2525 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
2526 | return -EINVAL; | |
2527 | ||
f1174f77 EC |
2528 | switch (opcode) { |
2529 | case BPF_ADD: | |
2530 | /* We can take a fixed offset as long as it doesn't overflow | |
2531 | * the s32 'off' field | |
969bf05e | 2532 | */ |
b03c9f9f EC |
2533 | if (known && (ptr_reg->off + smin_val == |
2534 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 2535 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
2536 | dst_reg->smin_value = smin_ptr; |
2537 | dst_reg->smax_value = smax_ptr; | |
2538 | dst_reg->umin_value = umin_ptr; | |
2539 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 2540 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 2541 | dst_reg->off = ptr_reg->off + smin_val; |
f1174f77 EC |
2542 | dst_reg->range = ptr_reg->range; |
2543 | break; | |
2544 | } | |
f1174f77 EC |
2545 | /* A new variable offset is created. Note that off_reg->off |
2546 | * == 0, since it's a scalar. | |
2547 | * dst_reg gets the pointer type and since some positive | |
2548 | * integer value was added to the pointer, give it a new 'id' | |
2549 | * if it's a PTR_TO_PACKET. | |
2550 | * this creates a new 'base' pointer, off_reg (variable) gets | |
2551 | * added into the variable offset, and we copy the fixed offset | |
2552 | * from ptr_reg. | |
969bf05e | 2553 | */ |
b03c9f9f EC |
2554 | if (signed_add_overflows(smin_ptr, smin_val) || |
2555 | signed_add_overflows(smax_ptr, smax_val)) { | |
2556 | dst_reg->smin_value = S64_MIN; | |
2557 | dst_reg->smax_value = S64_MAX; | |
2558 | } else { | |
2559 | dst_reg->smin_value = smin_ptr + smin_val; | |
2560 | dst_reg->smax_value = smax_ptr + smax_val; | |
2561 | } | |
2562 | if (umin_ptr + umin_val < umin_ptr || | |
2563 | umax_ptr + umax_val < umax_ptr) { | |
2564 | dst_reg->umin_value = 0; | |
2565 | dst_reg->umax_value = U64_MAX; | |
2566 | } else { | |
2567 | dst_reg->umin_value = umin_ptr + umin_val; | |
2568 | dst_reg->umax_value = umax_ptr + umax_val; | |
2569 | } | |
f1174f77 EC |
2570 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
2571 | dst_reg->off = ptr_reg->off; | |
de8f3a83 | 2572 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
2573 | dst_reg->id = ++env->id_gen; |
2574 | /* something was added to pkt_ptr, set range to zero */ | |
2575 | dst_reg->range = 0; | |
2576 | } | |
2577 | break; | |
2578 | case BPF_SUB: | |
2579 | if (dst_reg == off_reg) { | |
2580 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
2581 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
2582 | dst); | |
f1174f77 EC |
2583 | return -EACCES; |
2584 | } | |
2585 | /* We don't allow subtraction from FP, because (according to | |
2586 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
2587 | * be able to deal with it. | |
969bf05e | 2588 | */ |
f1174f77 | 2589 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
2590 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
2591 | dst); | |
f1174f77 EC |
2592 | return -EACCES; |
2593 | } | |
b03c9f9f EC |
2594 | if (known && (ptr_reg->off - smin_val == |
2595 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 2596 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
2597 | dst_reg->smin_value = smin_ptr; |
2598 | dst_reg->smax_value = smax_ptr; | |
2599 | dst_reg->umin_value = umin_ptr; | |
2600 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
2601 | dst_reg->var_off = ptr_reg->var_off; |
2602 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 2603 | dst_reg->off = ptr_reg->off - smin_val; |
f1174f77 EC |
2604 | dst_reg->range = ptr_reg->range; |
2605 | break; | |
2606 | } | |
f1174f77 EC |
2607 | /* A new variable offset is created. If the subtrahend is known |
2608 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 2609 | */ |
b03c9f9f EC |
2610 | if (signed_sub_overflows(smin_ptr, smax_val) || |
2611 | signed_sub_overflows(smax_ptr, smin_val)) { | |
2612 | /* Overflow possible, we know nothing */ | |
2613 | dst_reg->smin_value = S64_MIN; | |
2614 | dst_reg->smax_value = S64_MAX; | |
2615 | } else { | |
2616 | dst_reg->smin_value = smin_ptr - smax_val; | |
2617 | dst_reg->smax_value = smax_ptr - smin_val; | |
2618 | } | |
2619 | if (umin_ptr < umax_val) { | |
2620 | /* Overflow possible, we know nothing */ | |
2621 | dst_reg->umin_value = 0; | |
2622 | dst_reg->umax_value = U64_MAX; | |
2623 | } else { | |
2624 | /* Cannot overflow (as long as bounds are consistent) */ | |
2625 | dst_reg->umin_value = umin_ptr - umax_val; | |
2626 | dst_reg->umax_value = umax_ptr - umin_val; | |
2627 | } | |
f1174f77 EC |
2628 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
2629 | dst_reg->off = ptr_reg->off; | |
de8f3a83 | 2630 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
2631 | dst_reg->id = ++env->id_gen; |
2632 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 2633 | if (smin_val < 0) |
f1174f77 | 2634 | dst_reg->range = 0; |
43188702 | 2635 | } |
f1174f77 EC |
2636 | break; |
2637 | case BPF_AND: | |
2638 | case BPF_OR: | |
2639 | case BPF_XOR: | |
82abbf8d AS |
2640 | /* bitwise ops on pointers are troublesome, prohibit. */ |
2641 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
2642 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
2643 | return -EACCES; |
2644 | default: | |
2645 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
2646 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
2647 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 2648 | return -EACCES; |
43188702 JF |
2649 | } |
2650 | ||
bb7f0f98 AS |
2651 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
2652 | return -EINVAL; | |
2653 | ||
b03c9f9f EC |
2654 | __update_reg_bounds(dst_reg); |
2655 | __reg_deduce_bounds(dst_reg); | |
2656 | __reg_bound_offset(dst_reg); | |
43188702 JF |
2657 | return 0; |
2658 | } | |
2659 | ||
468f6eaf JH |
2660 | /* WARNING: This function does calculations on 64-bit values, but the actual |
2661 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
2662 | * need extra checks in the 32-bit case. | |
2663 | */ | |
f1174f77 EC |
2664 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
2665 | struct bpf_insn *insn, | |
2666 | struct bpf_reg_state *dst_reg, | |
2667 | struct bpf_reg_state src_reg) | |
969bf05e | 2668 | { |
638f5b90 | 2669 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 2670 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 2671 | bool src_known, dst_known; |
b03c9f9f EC |
2672 | s64 smin_val, smax_val; |
2673 | u64 umin_val, umax_val; | |
468f6eaf | 2674 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
48461135 | 2675 | |
b03c9f9f EC |
2676 | smin_val = src_reg.smin_value; |
2677 | smax_val = src_reg.smax_value; | |
2678 | umin_val = src_reg.umin_value; | |
2679 | umax_val = src_reg.umax_value; | |
f1174f77 EC |
2680 | src_known = tnum_is_const(src_reg.var_off); |
2681 | dst_known = tnum_is_const(dst_reg->var_off); | |
f23cc643 | 2682 | |
bb7f0f98 AS |
2683 | if (!src_known && |
2684 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
2685 | __mark_reg_unknown(dst_reg); | |
2686 | return 0; | |
2687 | } | |
2688 | ||
48461135 JB |
2689 | switch (opcode) { |
2690 | case BPF_ADD: | |
b03c9f9f EC |
2691 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
2692 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
2693 | dst_reg->smin_value = S64_MIN; | |
2694 | dst_reg->smax_value = S64_MAX; | |
2695 | } else { | |
2696 | dst_reg->smin_value += smin_val; | |
2697 | dst_reg->smax_value += smax_val; | |
2698 | } | |
2699 | if (dst_reg->umin_value + umin_val < umin_val || | |
2700 | dst_reg->umax_value + umax_val < umax_val) { | |
2701 | dst_reg->umin_value = 0; | |
2702 | dst_reg->umax_value = U64_MAX; | |
2703 | } else { | |
2704 | dst_reg->umin_value += umin_val; | |
2705 | dst_reg->umax_value += umax_val; | |
2706 | } | |
f1174f77 | 2707 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
2708 | break; |
2709 | case BPF_SUB: | |
b03c9f9f EC |
2710 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
2711 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
2712 | /* Overflow possible, we know nothing */ | |
2713 | dst_reg->smin_value = S64_MIN; | |
2714 | dst_reg->smax_value = S64_MAX; | |
2715 | } else { | |
2716 | dst_reg->smin_value -= smax_val; | |
2717 | dst_reg->smax_value -= smin_val; | |
2718 | } | |
2719 | if (dst_reg->umin_value < umax_val) { | |
2720 | /* Overflow possible, we know nothing */ | |
2721 | dst_reg->umin_value = 0; | |
2722 | dst_reg->umax_value = U64_MAX; | |
2723 | } else { | |
2724 | /* Cannot overflow (as long as bounds are consistent) */ | |
2725 | dst_reg->umin_value -= umax_val; | |
2726 | dst_reg->umax_value -= umin_val; | |
2727 | } | |
f1174f77 | 2728 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
2729 | break; |
2730 | case BPF_MUL: | |
b03c9f9f EC |
2731 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
2732 | if (smin_val < 0 || dst_reg->smin_value < 0) { | |
f1174f77 | 2733 | /* Ain't nobody got time to multiply that sign */ |
b03c9f9f EC |
2734 | __mark_reg_unbounded(dst_reg); |
2735 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
2736 | break; |
2737 | } | |
b03c9f9f EC |
2738 | /* Both values are positive, so we can work with unsigned and |
2739 | * copy the result to signed (unless it exceeds S64_MAX). | |
f1174f77 | 2740 | */ |
b03c9f9f EC |
2741 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
2742 | /* Potential overflow, we know nothing */ | |
2743 | __mark_reg_unbounded(dst_reg); | |
2744 | /* (except what we can learn from the var_off) */ | |
2745 | __update_reg_bounds(dst_reg); | |
2746 | break; | |
2747 | } | |
2748 | dst_reg->umin_value *= umin_val; | |
2749 | dst_reg->umax_value *= umax_val; | |
2750 | if (dst_reg->umax_value > S64_MAX) { | |
2751 | /* Overflow possible, we know nothing */ | |
2752 | dst_reg->smin_value = S64_MIN; | |
2753 | dst_reg->smax_value = S64_MAX; | |
2754 | } else { | |
2755 | dst_reg->smin_value = dst_reg->umin_value; | |
2756 | dst_reg->smax_value = dst_reg->umax_value; | |
2757 | } | |
48461135 JB |
2758 | break; |
2759 | case BPF_AND: | |
f1174f77 | 2760 | if (src_known && dst_known) { |
b03c9f9f EC |
2761 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
2762 | src_reg.var_off.value); | |
f1174f77 EC |
2763 | break; |
2764 | } | |
b03c9f9f EC |
2765 | /* We get our minimum from the var_off, since that's inherently |
2766 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
f23cc643 | 2767 | */ |
f1174f77 | 2768 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
b03c9f9f EC |
2769 | dst_reg->umin_value = dst_reg->var_off.value; |
2770 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
2771 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
2772 | /* Lose signed bounds when ANDing negative numbers, | |
2773 | * ain't nobody got time for that. | |
2774 | */ | |
2775 | dst_reg->smin_value = S64_MIN; | |
2776 | dst_reg->smax_value = S64_MAX; | |
2777 | } else { | |
2778 | /* ANDing two positives gives a positive, so safe to | |
2779 | * cast result into s64. | |
2780 | */ | |
2781 | dst_reg->smin_value = dst_reg->umin_value; | |
2782 | dst_reg->smax_value = dst_reg->umax_value; | |
2783 | } | |
2784 | /* We may learn something more from the var_off */ | |
2785 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
2786 | break; |
2787 | case BPF_OR: | |
2788 | if (src_known && dst_known) { | |
b03c9f9f EC |
2789 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
2790 | src_reg.var_off.value); | |
f1174f77 EC |
2791 | break; |
2792 | } | |
b03c9f9f EC |
2793 | /* We get our maximum from the var_off, and our minimum is the |
2794 | * maximum of the operands' minima | |
f1174f77 EC |
2795 | */ |
2796 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); | |
b03c9f9f EC |
2797 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
2798 | dst_reg->umax_value = dst_reg->var_off.value | | |
2799 | dst_reg->var_off.mask; | |
2800 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
2801 | /* Lose signed bounds when ORing negative numbers, | |
2802 | * ain't nobody got time for that. | |
2803 | */ | |
2804 | dst_reg->smin_value = S64_MIN; | |
2805 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 2806 | } else { |
b03c9f9f EC |
2807 | /* ORing two positives gives a positive, so safe to |
2808 | * cast result into s64. | |
2809 | */ | |
2810 | dst_reg->smin_value = dst_reg->umin_value; | |
2811 | dst_reg->smax_value = dst_reg->umax_value; | |
f1174f77 | 2812 | } |
b03c9f9f EC |
2813 | /* We may learn something more from the var_off */ |
2814 | __update_reg_bounds(dst_reg); | |
48461135 JB |
2815 | break; |
2816 | case BPF_LSH: | |
468f6eaf JH |
2817 | if (umax_val >= insn_bitness) { |
2818 | /* Shifts greater than 31 or 63 are undefined. | |
2819 | * This includes shifts by a negative number. | |
b03c9f9f | 2820 | */ |
61bd5218 | 2821 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
2822 | break; |
2823 | } | |
b03c9f9f EC |
2824 | /* We lose all sign bit information (except what we can pick |
2825 | * up from var_off) | |
48461135 | 2826 | */ |
b03c9f9f EC |
2827 | dst_reg->smin_value = S64_MIN; |
2828 | dst_reg->smax_value = S64_MAX; | |
2829 | /* If we might shift our top bit out, then we know nothing */ | |
2830 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
2831 | dst_reg->umin_value = 0; | |
2832 | dst_reg->umax_value = U64_MAX; | |
d1174416 | 2833 | } else { |
b03c9f9f EC |
2834 | dst_reg->umin_value <<= umin_val; |
2835 | dst_reg->umax_value <<= umax_val; | |
d1174416 | 2836 | } |
b03c9f9f EC |
2837 | if (src_known) |
2838 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); | |
2839 | else | |
2840 | dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val); | |
2841 | /* We may learn something more from the var_off */ | |
2842 | __update_reg_bounds(dst_reg); | |
48461135 JB |
2843 | break; |
2844 | case BPF_RSH: | |
468f6eaf JH |
2845 | if (umax_val >= insn_bitness) { |
2846 | /* Shifts greater than 31 or 63 are undefined. | |
2847 | * This includes shifts by a negative number. | |
b03c9f9f | 2848 | */ |
61bd5218 | 2849 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
2850 | break; |
2851 | } | |
4374f256 EC |
2852 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
2853 | * be negative, then either: | |
2854 | * 1) src_reg might be zero, so the sign bit of the result is | |
2855 | * unknown, so we lose our signed bounds | |
2856 | * 2) it's known negative, thus the unsigned bounds capture the | |
2857 | * signed bounds | |
2858 | * 3) the signed bounds cross zero, so they tell us nothing | |
2859 | * about the result | |
2860 | * If the value in dst_reg is known nonnegative, then again the | |
2861 | * unsigned bounts capture the signed bounds. | |
2862 | * Thus, in all cases it suffices to blow away our signed bounds | |
2863 | * and rely on inferring new ones from the unsigned bounds and | |
2864 | * var_off of the result. | |
2865 | */ | |
2866 | dst_reg->smin_value = S64_MIN; | |
2867 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 2868 | if (src_known) |
b03c9f9f EC |
2869 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, |
2870 | umin_val); | |
f1174f77 | 2871 | else |
b03c9f9f EC |
2872 | dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val); |
2873 | dst_reg->umin_value >>= umax_val; | |
2874 | dst_reg->umax_value >>= umin_val; | |
2875 | /* We may learn something more from the var_off */ | |
2876 | __update_reg_bounds(dst_reg); | |
48461135 JB |
2877 | break; |
2878 | default: | |
61bd5218 | 2879 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
2880 | break; |
2881 | } | |
2882 | ||
468f6eaf JH |
2883 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
2884 | /* 32-bit ALU ops are (32,32)->32 */ | |
2885 | coerce_reg_to_size(dst_reg, 4); | |
2886 | coerce_reg_to_size(&src_reg, 4); | |
2887 | } | |
2888 | ||
b03c9f9f EC |
2889 | __reg_deduce_bounds(dst_reg); |
2890 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
2891 | return 0; |
2892 | } | |
2893 | ||
2894 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
2895 | * and var_off. | |
2896 | */ | |
2897 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
2898 | struct bpf_insn *insn) | |
2899 | { | |
f4d7e40a AS |
2900 | struct bpf_verifier_state *vstate = env->cur_state; |
2901 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
2902 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
2903 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
2904 | u8 opcode = BPF_OP(insn->code); | |
f1174f77 EC |
2905 | |
2906 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
2907 | src_reg = NULL; |
2908 | if (dst_reg->type != SCALAR_VALUE) | |
2909 | ptr_reg = dst_reg; | |
2910 | if (BPF_SRC(insn->code) == BPF_X) { | |
2911 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
2912 | if (src_reg->type != SCALAR_VALUE) { |
2913 | if (dst_reg->type != SCALAR_VALUE) { | |
2914 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
2915 | * an arbitrary scalar. Disallow all math except |
2916 | * pointer subtraction | |
f1174f77 | 2917 | */ |
82abbf8d AS |
2918 | if (opcode == BPF_SUB){ |
2919 | mark_reg_unknown(env, regs, insn->dst_reg); | |
2920 | return 0; | |
f1174f77 | 2921 | } |
82abbf8d AS |
2922 | verbose(env, "R%d pointer %s pointer prohibited\n", |
2923 | insn->dst_reg, | |
2924 | bpf_alu_string[opcode >> 4]); | |
2925 | return -EACCES; | |
f1174f77 EC |
2926 | } else { |
2927 | /* scalar += pointer | |
2928 | * This is legal, but we have to reverse our | |
2929 | * src/dest handling in computing the range | |
2930 | */ | |
82abbf8d AS |
2931 | return adjust_ptr_min_max_vals(env, insn, |
2932 | src_reg, dst_reg); | |
f1174f77 EC |
2933 | } |
2934 | } else if (ptr_reg) { | |
2935 | /* pointer += scalar */ | |
82abbf8d AS |
2936 | return adjust_ptr_min_max_vals(env, insn, |
2937 | dst_reg, src_reg); | |
f1174f77 EC |
2938 | } |
2939 | } else { | |
2940 | /* Pretend the src is a reg with a known value, since we only | |
2941 | * need to be able to read from this state. | |
2942 | */ | |
2943 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 2944 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 2945 | src_reg = &off_reg; |
82abbf8d AS |
2946 | if (ptr_reg) /* pointer += K */ |
2947 | return adjust_ptr_min_max_vals(env, insn, | |
2948 | ptr_reg, src_reg); | |
f1174f77 EC |
2949 | } |
2950 | ||
2951 | /* Got here implies adding two SCALAR_VALUEs */ | |
2952 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 2953 | print_verifier_state(env, state); |
61bd5218 | 2954 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
2955 | return -EINVAL; |
2956 | } | |
2957 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 2958 | print_verifier_state(env, state); |
61bd5218 | 2959 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
2960 | return -EINVAL; |
2961 | } | |
2962 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
2963 | } |
2964 | ||
17a52670 | 2965 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 2966 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 2967 | { |
638f5b90 | 2968 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
2969 | u8 opcode = BPF_OP(insn->code); |
2970 | int err; | |
2971 | ||
2972 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
2973 | if (opcode == BPF_NEG) { | |
2974 | if (BPF_SRC(insn->code) != 0 || | |
2975 | insn->src_reg != BPF_REG_0 || | |
2976 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 2977 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
2978 | return -EINVAL; |
2979 | } | |
2980 | } else { | |
2981 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
2982 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
2983 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 2984 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
2985 | return -EINVAL; |
2986 | } | |
2987 | } | |
2988 | ||
2989 | /* check src operand */ | |
dc503a8a | 2990 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
2991 | if (err) |
2992 | return err; | |
2993 | ||
1be7f75d | 2994 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 2995 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
2996 | insn->dst_reg); |
2997 | return -EACCES; | |
2998 | } | |
2999 | ||
17a52670 | 3000 | /* check dest operand */ |
dc503a8a | 3001 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
3002 | if (err) |
3003 | return err; | |
3004 | ||
3005 | } else if (opcode == BPF_MOV) { | |
3006 | ||
3007 | if (BPF_SRC(insn->code) == BPF_X) { | |
3008 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 3009 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
3010 | return -EINVAL; |
3011 | } | |
3012 | ||
3013 | /* check src operand */ | |
dc503a8a | 3014 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3015 | if (err) |
3016 | return err; | |
3017 | } else { | |
3018 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 3019 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
3020 | return -EINVAL; |
3021 | } | |
3022 | } | |
3023 | ||
3024 | /* check dest operand */ | |
dc503a8a | 3025 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
3026 | if (err) |
3027 | return err; | |
3028 | ||
3029 | if (BPF_SRC(insn->code) == BPF_X) { | |
3030 | if (BPF_CLASS(insn->code) == BPF_ALU64) { | |
3031 | /* case: R1 = R2 | |
3032 | * copy register state to dest reg | |
3033 | */ | |
3034 | regs[insn->dst_reg] = regs[insn->src_reg]; | |
8fe2d6cc | 3035 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
17a52670 | 3036 | } else { |
f1174f77 | 3037 | /* R1 = (u32) R2 */ |
1be7f75d | 3038 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
3039 | verbose(env, |
3040 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
3041 | insn->src_reg); |
3042 | return -EACCES; | |
3043 | } | |
61bd5218 | 3044 | mark_reg_unknown(env, regs, insn->dst_reg); |
0c17d1d2 | 3045 | coerce_reg_to_size(®s[insn->dst_reg], 4); |
17a52670 AS |
3046 | } |
3047 | } else { | |
3048 | /* case: R = imm | |
3049 | * remember the value we stored into this reg | |
3050 | */ | |
f1174f77 | 3051 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
3052 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
3053 | __mark_reg_known(regs + insn->dst_reg, | |
3054 | insn->imm); | |
3055 | } else { | |
3056 | __mark_reg_known(regs + insn->dst_reg, | |
3057 | (u32)insn->imm); | |
3058 | } | |
17a52670 AS |
3059 | } |
3060 | ||
3061 | } else if (opcode > BPF_END) { | |
61bd5218 | 3062 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
3063 | return -EINVAL; |
3064 | ||
3065 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
3066 | ||
17a52670 AS |
3067 | if (BPF_SRC(insn->code) == BPF_X) { |
3068 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 3069 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
3070 | return -EINVAL; |
3071 | } | |
3072 | /* check src1 operand */ | |
dc503a8a | 3073 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3074 | if (err) |
3075 | return err; | |
3076 | } else { | |
3077 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 3078 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
3079 | return -EINVAL; |
3080 | } | |
3081 | } | |
3082 | ||
3083 | /* check src2 operand */ | |
dc503a8a | 3084 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3085 | if (err) |
3086 | return err; | |
3087 | ||
3088 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
3089 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 3090 | verbose(env, "div by zero\n"); |
17a52670 AS |
3091 | return -EINVAL; |
3092 | } | |
3093 | ||
229394e8 RV |
3094 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
3095 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
3096 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
3097 | ||
3098 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 3099 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
3100 | return -EINVAL; |
3101 | } | |
3102 | } | |
3103 | ||
1a0dc1ac | 3104 | /* check dest operand */ |
dc503a8a | 3105 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
3106 | if (err) |
3107 | return err; | |
3108 | ||
f1174f77 | 3109 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
3110 | } |
3111 | ||
3112 | return 0; | |
3113 | } | |
3114 | ||
f4d7e40a | 3115 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 3116 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 3117 | enum bpf_reg_type type, |
fb2a311a | 3118 | bool range_right_open) |
969bf05e | 3119 | { |
f4d7e40a | 3120 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
58e2af8b | 3121 | struct bpf_reg_state *regs = state->regs, *reg; |
fb2a311a | 3122 | u16 new_range; |
f4d7e40a | 3123 | int i, j; |
2d2be8ca | 3124 | |
fb2a311a DB |
3125 | if (dst_reg->off < 0 || |
3126 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
3127 | /* This doesn't give us any range */ |
3128 | return; | |
3129 | ||
b03c9f9f EC |
3130 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
3131 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
3132 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
3133 | * than pkt_end, but that's because it's also less than pkt. | |
3134 | */ | |
3135 | return; | |
3136 | ||
fb2a311a DB |
3137 | new_range = dst_reg->off; |
3138 | if (range_right_open) | |
3139 | new_range--; | |
3140 | ||
3141 | /* Examples for register markings: | |
2d2be8ca | 3142 | * |
fb2a311a | 3143 | * pkt_data in dst register: |
2d2be8ca DB |
3144 | * |
3145 | * r2 = r3; | |
3146 | * r2 += 8; | |
3147 | * if (r2 > pkt_end) goto <handle exception> | |
3148 | * <access okay> | |
3149 | * | |
b4e432f1 DB |
3150 | * r2 = r3; |
3151 | * r2 += 8; | |
3152 | * if (r2 < pkt_end) goto <access okay> | |
3153 | * <handle exception> | |
3154 | * | |
2d2be8ca DB |
3155 | * Where: |
3156 | * r2 == dst_reg, pkt_end == src_reg | |
3157 | * r2=pkt(id=n,off=8,r=0) | |
3158 | * r3=pkt(id=n,off=0,r=0) | |
3159 | * | |
fb2a311a | 3160 | * pkt_data in src register: |
2d2be8ca DB |
3161 | * |
3162 | * r2 = r3; | |
3163 | * r2 += 8; | |
3164 | * if (pkt_end >= r2) goto <access okay> | |
3165 | * <handle exception> | |
3166 | * | |
b4e432f1 DB |
3167 | * r2 = r3; |
3168 | * r2 += 8; | |
3169 | * if (pkt_end <= r2) goto <handle exception> | |
3170 | * <access okay> | |
3171 | * | |
2d2be8ca DB |
3172 | * Where: |
3173 | * pkt_end == dst_reg, r2 == src_reg | |
3174 | * r2=pkt(id=n,off=8,r=0) | |
3175 | * r3=pkt(id=n,off=0,r=0) | |
3176 | * | |
3177 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
3178 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
3179 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
3180 | * the check. | |
969bf05e | 3181 | */ |
2d2be8ca | 3182 | |
f1174f77 EC |
3183 | /* If our ids match, then we must have the same max_value. And we |
3184 | * don't care about the other reg's fixed offset, since if it's too big | |
3185 | * the range won't allow anything. | |
3186 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
3187 | */ | |
969bf05e | 3188 | for (i = 0; i < MAX_BPF_REG; i++) |
de8f3a83 | 3189 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
b1977682 | 3190 | /* keep the maximum range already checked */ |
fb2a311a | 3191 | regs[i].range = max(regs[i].range, new_range); |
969bf05e | 3192 | |
f4d7e40a AS |
3193 | for (j = 0; j <= vstate->curframe; j++) { |
3194 | state = vstate->frame[j]; | |
3195 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
3196 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
3197 | continue; | |
3198 | reg = &state->stack[i].spilled_ptr; | |
3199 | if (reg->type == type && reg->id == dst_reg->id) | |
3200 | reg->range = max(reg->range, new_range); | |
3201 | } | |
969bf05e AS |
3202 | } |
3203 | } | |
3204 | ||
48461135 JB |
3205 | /* Adjusts the register min/max values in the case that the dst_reg is the |
3206 | * variable register that we are working on, and src_reg is a constant or we're | |
3207 | * simply doing a BPF_K check. | |
f1174f77 | 3208 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
3209 | */ |
3210 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3211 | struct bpf_reg_state *false_reg, u64 val, | |
3212 | u8 opcode) | |
3213 | { | |
f1174f77 EC |
3214 | /* If the dst_reg is a pointer, we can't learn anything about its |
3215 | * variable offset from the compare (unless src_reg were a pointer into | |
3216 | * the same object, but we don't bother with that. | |
3217 | * Since false_reg and true_reg have the same type by construction, we | |
3218 | * only need to check one of them for pointerness. | |
3219 | */ | |
3220 | if (__is_pointer_value(false, false_reg)) | |
3221 | return; | |
4cabc5b1 | 3222 | |
48461135 JB |
3223 | switch (opcode) { |
3224 | case BPF_JEQ: | |
3225 | /* If this is false then we know nothing Jon Snow, but if it is | |
3226 | * true then we know for sure. | |
3227 | */ | |
b03c9f9f | 3228 | __mark_reg_known(true_reg, val); |
48461135 JB |
3229 | break; |
3230 | case BPF_JNE: | |
3231 | /* If this is true we know nothing Jon Snow, but if it is false | |
3232 | * we know the value for sure; | |
3233 | */ | |
b03c9f9f | 3234 | __mark_reg_known(false_reg, val); |
48461135 JB |
3235 | break; |
3236 | case BPF_JGT: | |
b03c9f9f EC |
3237 | false_reg->umax_value = min(false_reg->umax_value, val); |
3238 | true_reg->umin_value = max(true_reg->umin_value, val + 1); | |
3239 | break; | |
48461135 | 3240 | case BPF_JSGT: |
b03c9f9f EC |
3241 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
3242 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); | |
48461135 | 3243 | break; |
b4e432f1 DB |
3244 | case BPF_JLT: |
3245 | false_reg->umin_value = max(false_reg->umin_value, val); | |
3246 | true_reg->umax_value = min(true_reg->umax_value, val - 1); | |
3247 | break; | |
3248 | case BPF_JSLT: | |
3249 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); | |
3250 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); | |
3251 | break; | |
48461135 | 3252 | case BPF_JGE: |
b03c9f9f EC |
3253 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
3254 | true_reg->umin_value = max(true_reg->umin_value, val); | |
3255 | break; | |
48461135 | 3256 | case BPF_JSGE: |
b03c9f9f EC |
3257 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
3258 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); | |
48461135 | 3259 | break; |
b4e432f1 DB |
3260 | case BPF_JLE: |
3261 | false_reg->umin_value = max(false_reg->umin_value, val + 1); | |
3262 | true_reg->umax_value = min(true_reg->umax_value, val); | |
3263 | break; | |
3264 | case BPF_JSLE: | |
3265 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); | |
3266 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); | |
3267 | break; | |
48461135 JB |
3268 | default: |
3269 | break; | |
3270 | } | |
3271 | ||
b03c9f9f EC |
3272 | __reg_deduce_bounds(false_reg); |
3273 | __reg_deduce_bounds(true_reg); | |
3274 | /* We might have learned some bits from the bounds. */ | |
3275 | __reg_bound_offset(false_reg); | |
3276 | __reg_bound_offset(true_reg); | |
3277 | /* Intersecting with the old var_off might have improved our bounds | |
3278 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
3279 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
3280 | */ | |
3281 | __update_reg_bounds(false_reg); | |
3282 | __update_reg_bounds(true_reg); | |
48461135 JB |
3283 | } |
3284 | ||
f1174f77 EC |
3285 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
3286 | * the variable reg. | |
48461135 JB |
3287 | */ |
3288 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3289 | struct bpf_reg_state *false_reg, u64 val, | |
3290 | u8 opcode) | |
3291 | { | |
f1174f77 EC |
3292 | if (__is_pointer_value(false, false_reg)) |
3293 | return; | |
4cabc5b1 | 3294 | |
48461135 JB |
3295 | switch (opcode) { |
3296 | case BPF_JEQ: | |
3297 | /* If this is false then we know nothing Jon Snow, but if it is | |
3298 | * true then we know for sure. | |
3299 | */ | |
b03c9f9f | 3300 | __mark_reg_known(true_reg, val); |
48461135 JB |
3301 | break; |
3302 | case BPF_JNE: | |
3303 | /* If this is true we know nothing Jon Snow, but if it is false | |
3304 | * we know the value for sure; | |
3305 | */ | |
b03c9f9f | 3306 | __mark_reg_known(false_reg, val); |
48461135 JB |
3307 | break; |
3308 | case BPF_JGT: | |
b03c9f9f EC |
3309 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
3310 | false_reg->umin_value = max(false_reg->umin_value, val); | |
3311 | break; | |
48461135 | 3312 | case BPF_JSGT: |
b03c9f9f EC |
3313 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
3314 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); | |
48461135 | 3315 | break; |
b4e432f1 DB |
3316 | case BPF_JLT: |
3317 | true_reg->umin_value = max(true_reg->umin_value, val + 1); | |
3318 | false_reg->umax_value = min(false_reg->umax_value, val); | |
3319 | break; | |
3320 | case BPF_JSLT: | |
3321 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); | |
3322 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); | |
3323 | break; | |
48461135 | 3324 | case BPF_JGE: |
b03c9f9f EC |
3325 | true_reg->umax_value = min(true_reg->umax_value, val); |
3326 | false_reg->umin_value = max(false_reg->umin_value, val + 1); | |
3327 | break; | |
48461135 | 3328 | case BPF_JSGE: |
b03c9f9f EC |
3329 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
3330 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); | |
48461135 | 3331 | break; |
b4e432f1 DB |
3332 | case BPF_JLE: |
3333 | true_reg->umin_value = max(true_reg->umin_value, val); | |
3334 | false_reg->umax_value = min(false_reg->umax_value, val - 1); | |
3335 | break; | |
3336 | case BPF_JSLE: | |
3337 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); | |
3338 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); | |
3339 | break; | |
48461135 JB |
3340 | default: |
3341 | break; | |
3342 | } | |
3343 | ||
b03c9f9f EC |
3344 | __reg_deduce_bounds(false_reg); |
3345 | __reg_deduce_bounds(true_reg); | |
3346 | /* We might have learned some bits from the bounds. */ | |
3347 | __reg_bound_offset(false_reg); | |
3348 | __reg_bound_offset(true_reg); | |
3349 | /* Intersecting with the old var_off might have improved our bounds | |
3350 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
3351 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
3352 | */ | |
3353 | __update_reg_bounds(false_reg); | |
3354 | __update_reg_bounds(true_reg); | |
f1174f77 EC |
3355 | } |
3356 | ||
3357 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
3358 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
3359 | struct bpf_reg_state *dst_reg) | |
3360 | { | |
b03c9f9f EC |
3361 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
3362 | dst_reg->umin_value); | |
3363 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
3364 | dst_reg->umax_value); | |
3365 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
3366 | dst_reg->smin_value); | |
3367 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
3368 | dst_reg->smax_value); | |
f1174f77 EC |
3369 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
3370 | dst_reg->var_off); | |
b03c9f9f EC |
3371 | /* We might have learned new bounds from the var_off. */ |
3372 | __update_reg_bounds(src_reg); | |
3373 | __update_reg_bounds(dst_reg); | |
3374 | /* We might have learned something about the sign bit. */ | |
3375 | __reg_deduce_bounds(src_reg); | |
3376 | __reg_deduce_bounds(dst_reg); | |
3377 | /* We might have learned some bits from the bounds. */ | |
3378 | __reg_bound_offset(src_reg); | |
3379 | __reg_bound_offset(dst_reg); | |
3380 | /* Intersecting with the old var_off might have improved our bounds | |
3381 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
3382 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
3383 | */ | |
3384 | __update_reg_bounds(src_reg); | |
3385 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
3386 | } |
3387 | ||
3388 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
3389 | struct bpf_reg_state *true_dst, | |
3390 | struct bpf_reg_state *false_src, | |
3391 | struct bpf_reg_state *false_dst, | |
3392 | u8 opcode) | |
3393 | { | |
3394 | switch (opcode) { | |
3395 | case BPF_JEQ: | |
3396 | __reg_combine_min_max(true_src, true_dst); | |
3397 | break; | |
3398 | case BPF_JNE: | |
3399 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 3400 | break; |
4cabc5b1 | 3401 | } |
48461135 JB |
3402 | } |
3403 | ||
57a09bf0 | 3404 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
f1174f77 | 3405 | bool is_null) |
57a09bf0 TG |
3406 | { |
3407 | struct bpf_reg_state *reg = ®s[regno]; | |
3408 | ||
3409 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { | |
f1174f77 EC |
3410 | /* Old offset (both fixed and variable parts) should |
3411 | * have been known-zero, because we don't allow pointer | |
3412 | * arithmetic on pointers that might be NULL. | |
3413 | */ | |
b03c9f9f EC |
3414 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
3415 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 3416 | reg->off)) { |
b03c9f9f EC |
3417 | __mark_reg_known_zero(reg); |
3418 | reg->off = 0; | |
f1174f77 EC |
3419 | } |
3420 | if (is_null) { | |
3421 | reg->type = SCALAR_VALUE; | |
56f668df MKL |
3422 | } else if (reg->map_ptr->inner_map_meta) { |
3423 | reg->type = CONST_PTR_TO_MAP; | |
3424 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
3425 | } else { | |
f1174f77 | 3426 | reg->type = PTR_TO_MAP_VALUE; |
56f668df | 3427 | } |
a08dd0da DB |
3428 | /* We don't need id from this point onwards anymore, thus we |
3429 | * should better reset it, so that state pruning has chances | |
3430 | * to take effect. | |
3431 | */ | |
3432 | reg->id = 0; | |
57a09bf0 TG |
3433 | } |
3434 | } | |
3435 | ||
3436 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
3437 | * be folded together at some point. | |
3438 | */ | |
f4d7e40a | 3439 | static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, |
f1174f77 | 3440 | bool is_null) |
57a09bf0 | 3441 | { |
f4d7e40a | 3442 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
57a09bf0 | 3443 | struct bpf_reg_state *regs = state->regs; |
a08dd0da | 3444 | u32 id = regs[regno].id; |
f4d7e40a | 3445 | int i, j; |
57a09bf0 TG |
3446 | |
3447 | for (i = 0; i < MAX_BPF_REG; i++) | |
f1174f77 | 3448 | mark_map_reg(regs, i, id, is_null); |
57a09bf0 | 3449 | |
f4d7e40a AS |
3450 | for (j = 0; j <= vstate->curframe; j++) { |
3451 | state = vstate->frame[j]; | |
3452 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
3453 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
3454 | continue; | |
3455 | mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); | |
3456 | } | |
57a09bf0 TG |
3457 | } |
3458 | } | |
3459 | ||
5beca081 DB |
3460 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
3461 | struct bpf_reg_state *dst_reg, | |
3462 | struct bpf_reg_state *src_reg, | |
3463 | struct bpf_verifier_state *this_branch, | |
3464 | struct bpf_verifier_state *other_branch) | |
3465 | { | |
3466 | if (BPF_SRC(insn->code) != BPF_X) | |
3467 | return false; | |
3468 | ||
3469 | switch (BPF_OP(insn->code)) { | |
3470 | case BPF_JGT: | |
3471 | if ((dst_reg->type == PTR_TO_PACKET && | |
3472 | src_reg->type == PTR_TO_PACKET_END) || | |
3473 | (dst_reg->type == PTR_TO_PACKET_META && | |
3474 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
3475 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
3476 | find_good_pkt_pointers(this_branch, dst_reg, | |
3477 | dst_reg->type, false); | |
3478 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
3479 | src_reg->type == PTR_TO_PACKET) || | |
3480 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
3481 | src_reg->type == PTR_TO_PACKET_META)) { | |
3482 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
3483 | find_good_pkt_pointers(other_branch, src_reg, | |
3484 | src_reg->type, true); | |
3485 | } else { | |
3486 | return false; | |
3487 | } | |
3488 | break; | |
3489 | case BPF_JLT: | |
3490 | if ((dst_reg->type == PTR_TO_PACKET && | |
3491 | src_reg->type == PTR_TO_PACKET_END) || | |
3492 | (dst_reg->type == PTR_TO_PACKET_META && | |
3493 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
3494 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
3495 | find_good_pkt_pointers(other_branch, dst_reg, | |
3496 | dst_reg->type, true); | |
3497 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
3498 | src_reg->type == PTR_TO_PACKET) || | |
3499 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
3500 | src_reg->type == PTR_TO_PACKET_META)) { | |
3501 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
3502 | find_good_pkt_pointers(this_branch, src_reg, | |
3503 | src_reg->type, false); | |
3504 | } else { | |
3505 | return false; | |
3506 | } | |
3507 | break; | |
3508 | case BPF_JGE: | |
3509 | if ((dst_reg->type == PTR_TO_PACKET && | |
3510 | src_reg->type == PTR_TO_PACKET_END) || | |
3511 | (dst_reg->type == PTR_TO_PACKET_META && | |
3512 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
3513 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
3514 | find_good_pkt_pointers(this_branch, dst_reg, | |
3515 | dst_reg->type, true); | |
3516 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
3517 | src_reg->type == PTR_TO_PACKET) || | |
3518 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
3519 | src_reg->type == PTR_TO_PACKET_META)) { | |
3520 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
3521 | find_good_pkt_pointers(other_branch, src_reg, | |
3522 | src_reg->type, false); | |
3523 | } else { | |
3524 | return false; | |
3525 | } | |
3526 | break; | |
3527 | case BPF_JLE: | |
3528 | if ((dst_reg->type == PTR_TO_PACKET && | |
3529 | src_reg->type == PTR_TO_PACKET_END) || | |
3530 | (dst_reg->type == PTR_TO_PACKET_META && | |
3531 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
3532 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
3533 | find_good_pkt_pointers(other_branch, dst_reg, | |
3534 | dst_reg->type, false); | |
3535 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
3536 | src_reg->type == PTR_TO_PACKET) || | |
3537 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
3538 | src_reg->type == PTR_TO_PACKET_META)) { | |
3539 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
3540 | find_good_pkt_pointers(this_branch, src_reg, | |
3541 | src_reg->type, true); | |
3542 | } else { | |
3543 | return false; | |
3544 | } | |
3545 | break; | |
3546 | default: | |
3547 | return false; | |
3548 | } | |
3549 | ||
3550 | return true; | |
3551 | } | |
3552 | ||
58e2af8b | 3553 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
3554 | struct bpf_insn *insn, int *insn_idx) |
3555 | { | |
f4d7e40a AS |
3556 | struct bpf_verifier_state *this_branch = env->cur_state; |
3557 | struct bpf_verifier_state *other_branch; | |
3558 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
3559 | struct bpf_reg_state *dst_reg, *other_branch_regs; | |
17a52670 AS |
3560 | u8 opcode = BPF_OP(insn->code); |
3561 | int err; | |
3562 | ||
b4e432f1 | 3563 | if (opcode > BPF_JSLE) { |
61bd5218 | 3564 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
17a52670 AS |
3565 | return -EINVAL; |
3566 | } | |
3567 | ||
3568 | if (BPF_SRC(insn->code) == BPF_X) { | |
3569 | if (insn->imm != 0) { | |
61bd5218 | 3570 | verbose(env, "BPF_JMP uses reserved fields\n"); |
17a52670 AS |
3571 | return -EINVAL; |
3572 | } | |
3573 | ||
3574 | /* check src1 operand */ | |
dc503a8a | 3575 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3576 | if (err) |
3577 | return err; | |
1be7f75d AS |
3578 | |
3579 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 3580 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
3581 | insn->src_reg); |
3582 | return -EACCES; | |
3583 | } | |
17a52670 AS |
3584 | } else { |
3585 | if (insn->src_reg != BPF_REG_0) { | |
61bd5218 | 3586 | verbose(env, "BPF_JMP uses reserved fields\n"); |
17a52670 AS |
3587 | return -EINVAL; |
3588 | } | |
3589 | } | |
3590 | ||
3591 | /* check src2 operand */ | |
dc503a8a | 3592 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3593 | if (err) |
3594 | return err; | |
3595 | ||
1a0dc1ac AS |
3596 | dst_reg = ®s[insn->dst_reg]; |
3597 | ||
17a52670 AS |
3598 | /* detect if R == 0 where R was initialized to zero earlier */ |
3599 | if (BPF_SRC(insn->code) == BPF_K && | |
3600 | (opcode == BPF_JEQ || opcode == BPF_JNE) && | |
f1174f77 | 3601 | dst_reg->type == SCALAR_VALUE && |
3bf15921 AS |
3602 | tnum_is_const(dst_reg->var_off)) { |
3603 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || | |
3604 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { | |
17a52670 AS |
3605 | /* if (imm == imm) goto pc+off; |
3606 | * only follow the goto, ignore fall-through | |
3607 | */ | |
3608 | *insn_idx += insn->off; | |
3609 | return 0; | |
3610 | } else { | |
3611 | /* if (imm != imm) goto pc+off; | |
3612 | * only follow fall-through branch, since | |
3613 | * that's where the program will go | |
3614 | */ | |
3615 | return 0; | |
3616 | } | |
3617 | } | |
3618 | ||
3619 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); | |
3620 | if (!other_branch) | |
3621 | return -EFAULT; | |
f4d7e40a | 3622 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 3623 | |
48461135 JB |
3624 | /* detect if we are comparing against a constant value so we can adjust |
3625 | * our min/max values for our dst register. | |
f1174f77 EC |
3626 | * this is only legit if both are scalars (or pointers to the same |
3627 | * object, I suppose, but we don't support that right now), because | |
3628 | * otherwise the different base pointers mean the offsets aren't | |
3629 | * comparable. | |
48461135 JB |
3630 | */ |
3631 | if (BPF_SRC(insn->code) == BPF_X) { | |
f1174f77 EC |
3632 | if (dst_reg->type == SCALAR_VALUE && |
3633 | regs[insn->src_reg].type == SCALAR_VALUE) { | |
3634 | if (tnum_is_const(regs[insn->src_reg].var_off)) | |
f4d7e40a | 3635 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
f1174f77 EC |
3636 | dst_reg, regs[insn->src_reg].var_off.value, |
3637 | opcode); | |
3638 | else if (tnum_is_const(dst_reg->var_off)) | |
f4d7e40a | 3639 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
f1174f77 EC |
3640 | ®s[insn->src_reg], |
3641 | dst_reg->var_off.value, opcode); | |
3642 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) | |
3643 | /* Comparing for equality, we can combine knowledge */ | |
f4d7e40a AS |
3644 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
3645 | &other_branch_regs[insn->dst_reg], | |
f1174f77 EC |
3646 | ®s[insn->src_reg], |
3647 | ®s[insn->dst_reg], opcode); | |
3648 | } | |
3649 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 3650 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
48461135 JB |
3651 | dst_reg, insn->imm, opcode); |
3652 | } | |
3653 | ||
58e2af8b | 3654 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */ |
17a52670 | 3655 | if (BPF_SRC(insn->code) == BPF_K && |
1a0dc1ac AS |
3656 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
3657 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { | |
57a09bf0 TG |
3658 | /* Mark all identical map registers in each branch as either |
3659 | * safe or unknown depending R == 0 or R != 0 conditional. | |
3660 | */ | |
f1174f77 EC |
3661 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
3662 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); | |
5beca081 DB |
3663 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
3664 | this_branch, other_branch) && | |
3665 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
3666 | verbose(env, "R%d pointer comparison prohibited\n", |
3667 | insn->dst_reg); | |
1be7f75d | 3668 | return -EACCES; |
17a52670 | 3669 | } |
61bd5218 | 3670 | if (env->log.level) |
f4d7e40a | 3671 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
3672 | return 0; |
3673 | } | |
3674 | ||
0246e64d AS |
3675 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
3676 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) | |
3677 | { | |
3678 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; | |
3679 | ||
3680 | return (struct bpf_map *) (unsigned long) imm64; | |
3681 | } | |
3682 | ||
17a52670 | 3683 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 3684 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 3685 | { |
638f5b90 | 3686 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
3687 | int err; |
3688 | ||
3689 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 3690 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
3691 | return -EINVAL; |
3692 | } | |
3693 | if (insn->off != 0) { | |
61bd5218 | 3694 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
3695 | return -EINVAL; |
3696 | } | |
3697 | ||
dc503a8a | 3698 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
3699 | if (err) |
3700 | return err; | |
3701 | ||
6b173873 | 3702 | if (insn->src_reg == 0) { |
6b173873 JK |
3703 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
3704 | ||
f1174f77 | 3705 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 3706 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 3707 | return 0; |
6b173873 | 3708 | } |
17a52670 AS |
3709 | |
3710 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ | |
3711 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); | |
3712 | ||
3713 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
3714 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); | |
3715 | return 0; | |
3716 | } | |
3717 | ||
96be4325 DB |
3718 | static bool may_access_skb(enum bpf_prog_type type) |
3719 | { | |
3720 | switch (type) { | |
3721 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
3722 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 3723 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
3724 | return true; |
3725 | default: | |
3726 | return false; | |
3727 | } | |
3728 | } | |
3729 | ||
ddd872bc AS |
3730 | /* verify safety of LD_ABS|LD_IND instructions: |
3731 | * - they can only appear in the programs where ctx == skb | |
3732 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
3733 | * preserve R6-R9, and store return value into R0 | |
3734 | * | |
3735 | * Implicit input: | |
3736 | * ctx == skb == R6 == CTX | |
3737 | * | |
3738 | * Explicit input: | |
3739 | * SRC == any register | |
3740 | * IMM == 32-bit immediate | |
3741 | * | |
3742 | * Output: | |
3743 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
3744 | */ | |
58e2af8b | 3745 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 3746 | { |
638f5b90 | 3747 | struct bpf_reg_state *regs = cur_regs(env); |
ddd872bc | 3748 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
3749 | int i, err; |
3750 | ||
24701ece | 3751 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 3752 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
3753 | return -EINVAL; |
3754 | } | |
3755 | ||
f4d7e40a AS |
3756 | if (env->subprog_cnt) { |
3757 | /* when program has LD_ABS insn JITs and interpreter assume | |
3758 | * that r1 == ctx == skb which is not the case for callees | |
3759 | * that can have arbitrary arguments. It's problematic | |
3760 | * for main prog as well since JITs would need to analyze | |
3761 | * all functions in order to make proper register save/restore | |
3762 | * decisions in the main prog. Hence disallow LD_ABS with calls | |
3763 | */ | |
3764 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); | |
3765 | return -EINVAL; | |
3766 | } | |
3767 | ||
ddd872bc | 3768 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 3769 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 3770 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 3771 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
3772 | return -EINVAL; |
3773 | } | |
3774 | ||
3775 | /* check whether implicit source operand (register R6) is readable */ | |
dc503a8a | 3776 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
ddd872bc AS |
3777 | if (err) |
3778 | return err; | |
3779 | ||
3780 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { | |
61bd5218 JK |
3781 | verbose(env, |
3782 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
3783 | return -EINVAL; |
3784 | } | |
3785 | ||
3786 | if (mode == BPF_IND) { | |
3787 | /* check explicit source operand */ | |
dc503a8a | 3788 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
3789 | if (err) |
3790 | return err; | |
3791 | } | |
3792 | ||
3793 | /* reset caller saved regs to unreadable */ | |
dc503a8a | 3794 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 3795 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
3796 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
3797 | } | |
ddd872bc AS |
3798 | |
3799 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
3800 | * the value fetched from the packet. |
3801 | * Already marked as written above. | |
ddd872bc | 3802 | */ |
61bd5218 | 3803 | mark_reg_unknown(env, regs, BPF_REG_0); |
ddd872bc AS |
3804 | return 0; |
3805 | } | |
3806 | ||
390ee7e2 AS |
3807 | static int check_return_code(struct bpf_verifier_env *env) |
3808 | { | |
3809 | struct bpf_reg_state *reg; | |
3810 | struct tnum range = tnum_range(0, 1); | |
3811 | ||
3812 | switch (env->prog->type) { | |
3813 | case BPF_PROG_TYPE_CGROUP_SKB: | |
3814 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
3815 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 3816 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
390ee7e2 AS |
3817 | break; |
3818 | default: | |
3819 | return 0; | |
3820 | } | |
3821 | ||
638f5b90 | 3822 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 3823 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 3824 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
3825 | reg_type_str[reg->type]); |
3826 | return -EINVAL; | |
3827 | } | |
3828 | ||
3829 | if (!tnum_in(range, reg->var_off)) { | |
61bd5218 | 3830 | verbose(env, "At program exit the register R0 "); |
390ee7e2 AS |
3831 | if (!tnum_is_unknown(reg->var_off)) { |
3832 | char tn_buf[48]; | |
3833 | ||
3834 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 3835 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 3836 | } else { |
61bd5218 | 3837 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 3838 | } |
61bd5218 | 3839 | verbose(env, " should have been 0 or 1\n"); |
390ee7e2 AS |
3840 | return -EINVAL; |
3841 | } | |
3842 | return 0; | |
3843 | } | |
3844 | ||
475fb78f AS |
3845 | /* non-recursive DFS pseudo code |
3846 | * 1 procedure DFS-iterative(G,v): | |
3847 | * 2 label v as discovered | |
3848 | * 3 let S be a stack | |
3849 | * 4 S.push(v) | |
3850 | * 5 while S is not empty | |
3851 | * 6 t <- S.pop() | |
3852 | * 7 if t is what we're looking for: | |
3853 | * 8 return t | |
3854 | * 9 for all edges e in G.adjacentEdges(t) do | |
3855 | * 10 if edge e is already labelled | |
3856 | * 11 continue with the next edge | |
3857 | * 12 w <- G.adjacentVertex(t,e) | |
3858 | * 13 if vertex w is not discovered and not explored | |
3859 | * 14 label e as tree-edge | |
3860 | * 15 label w as discovered | |
3861 | * 16 S.push(w) | |
3862 | * 17 continue at 5 | |
3863 | * 18 else if vertex w is discovered | |
3864 | * 19 label e as back-edge | |
3865 | * 20 else | |
3866 | * 21 // vertex w is explored | |
3867 | * 22 label e as forward- or cross-edge | |
3868 | * 23 label t as explored | |
3869 | * 24 S.pop() | |
3870 | * | |
3871 | * convention: | |
3872 | * 0x10 - discovered | |
3873 | * 0x11 - discovered and fall-through edge labelled | |
3874 | * 0x12 - discovered and fall-through and branch edges labelled | |
3875 | * 0x20 - explored | |
3876 | */ | |
3877 | ||
3878 | enum { | |
3879 | DISCOVERED = 0x10, | |
3880 | EXPLORED = 0x20, | |
3881 | FALLTHROUGH = 1, | |
3882 | BRANCH = 2, | |
3883 | }; | |
3884 | ||
58e2af8b | 3885 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
f1bca824 | 3886 | |
475fb78f AS |
3887 | static int *insn_stack; /* stack of insns to process */ |
3888 | static int cur_stack; /* current stack index */ | |
3889 | static int *insn_state; | |
3890 | ||
3891 | /* t, w, e - match pseudo-code above: | |
3892 | * t - index of current instruction | |
3893 | * w - next instruction | |
3894 | * e - edge | |
3895 | */ | |
58e2af8b | 3896 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
475fb78f AS |
3897 | { |
3898 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) | |
3899 | return 0; | |
3900 | ||
3901 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
3902 | return 0; | |
3903 | ||
3904 | if (w < 0 || w >= env->prog->len) { | |
61bd5218 | 3905 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
3906 | return -EINVAL; |
3907 | } | |
3908 | ||
f1bca824 AS |
3909 | if (e == BRANCH) |
3910 | /* mark branch target for state pruning */ | |
3911 | env->explored_states[w] = STATE_LIST_MARK; | |
3912 | ||
475fb78f AS |
3913 | if (insn_state[w] == 0) { |
3914 | /* tree-edge */ | |
3915 | insn_state[t] = DISCOVERED | e; | |
3916 | insn_state[w] = DISCOVERED; | |
3917 | if (cur_stack >= env->prog->len) | |
3918 | return -E2BIG; | |
3919 | insn_stack[cur_stack++] = w; | |
3920 | return 1; | |
3921 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
61bd5218 | 3922 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
3923 | return -EINVAL; |
3924 | } else if (insn_state[w] == EXPLORED) { | |
3925 | /* forward- or cross-edge */ | |
3926 | insn_state[t] = DISCOVERED | e; | |
3927 | } else { | |
61bd5218 | 3928 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
3929 | return -EFAULT; |
3930 | } | |
3931 | return 0; | |
3932 | } | |
3933 | ||
3934 | /* non-recursive depth-first-search to detect loops in BPF program | |
3935 | * loop == back-edge in directed graph | |
3936 | */ | |
58e2af8b | 3937 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
3938 | { |
3939 | struct bpf_insn *insns = env->prog->insnsi; | |
3940 | int insn_cnt = env->prog->len; | |
3941 | int ret = 0; | |
3942 | int i, t; | |
3943 | ||
cc8b0b92 AS |
3944 | ret = check_subprogs(env); |
3945 | if (ret < 0) | |
3946 | return ret; | |
3947 | ||
475fb78f AS |
3948 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
3949 | if (!insn_state) | |
3950 | return -ENOMEM; | |
3951 | ||
3952 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
3953 | if (!insn_stack) { | |
3954 | kfree(insn_state); | |
3955 | return -ENOMEM; | |
3956 | } | |
3957 | ||
3958 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
3959 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
3960 | cur_stack = 1; | |
3961 | ||
3962 | peek_stack: | |
3963 | if (cur_stack == 0) | |
3964 | goto check_state; | |
3965 | t = insn_stack[cur_stack - 1]; | |
3966 | ||
3967 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { | |
3968 | u8 opcode = BPF_OP(insns[t].code); | |
3969 | ||
3970 | if (opcode == BPF_EXIT) { | |
3971 | goto mark_explored; | |
3972 | } else if (opcode == BPF_CALL) { | |
3973 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
3974 | if (ret == 1) | |
3975 | goto peek_stack; | |
3976 | else if (ret < 0) | |
3977 | goto err_free; | |
07016151 DB |
3978 | if (t + 1 < insn_cnt) |
3979 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
cc8b0b92 AS |
3980 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
3981 | env->explored_states[t] = STATE_LIST_MARK; | |
3982 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); | |
3983 | if (ret == 1) | |
3984 | goto peek_stack; | |
3985 | else if (ret < 0) | |
3986 | goto err_free; | |
3987 | } | |
475fb78f AS |
3988 | } else if (opcode == BPF_JA) { |
3989 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
3990 | ret = -EINVAL; | |
3991 | goto err_free; | |
3992 | } | |
3993 | /* unconditional jump with single edge */ | |
3994 | ret = push_insn(t, t + insns[t].off + 1, | |
3995 | FALLTHROUGH, env); | |
3996 | if (ret == 1) | |
3997 | goto peek_stack; | |
3998 | else if (ret < 0) | |
3999 | goto err_free; | |
f1bca824 AS |
4000 | /* tell verifier to check for equivalent states |
4001 | * after every call and jump | |
4002 | */ | |
c3de6317 AS |
4003 | if (t + 1 < insn_cnt) |
4004 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
4005 | } else { |
4006 | /* conditional jump with two edges */ | |
3c2ce60b | 4007 | env->explored_states[t] = STATE_LIST_MARK; |
475fb78f AS |
4008 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
4009 | if (ret == 1) | |
4010 | goto peek_stack; | |
4011 | else if (ret < 0) | |
4012 | goto err_free; | |
4013 | ||
4014 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
4015 | if (ret == 1) | |
4016 | goto peek_stack; | |
4017 | else if (ret < 0) | |
4018 | goto err_free; | |
4019 | } | |
4020 | } else { | |
4021 | /* all other non-branch instructions with single | |
4022 | * fall-through edge | |
4023 | */ | |
4024 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
4025 | if (ret == 1) | |
4026 | goto peek_stack; | |
4027 | else if (ret < 0) | |
4028 | goto err_free; | |
4029 | } | |
4030 | ||
4031 | mark_explored: | |
4032 | insn_state[t] = EXPLORED; | |
4033 | if (cur_stack-- <= 0) { | |
61bd5218 | 4034 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
4035 | ret = -EFAULT; |
4036 | goto err_free; | |
4037 | } | |
4038 | goto peek_stack; | |
4039 | ||
4040 | check_state: | |
4041 | for (i = 0; i < insn_cnt; i++) { | |
4042 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 4043 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
4044 | ret = -EINVAL; |
4045 | goto err_free; | |
4046 | } | |
4047 | } | |
4048 | ret = 0; /* cfg looks good */ | |
4049 | ||
4050 | err_free: | |
4051 | kfree(insn_state); | |
4052 | kfree(insn_stack); | |
4053 | return ret; | |
4054 | } | |
4055 | ||
f1174f77 EC |
4056 | /* check %cur's range satisfies %old's */ |
4057 | static bool range_within(struct bpf_reg_state *old, | |
4058 | struct bpf_reg_state *cur) | |
4059 | { | |
b03c9f9f EC |
4060 | return old->umin_value <= cur->umin_value && |
4061 | old->umax_value >= cur->umax_value && | |
4062 | old->smin_value <= cur->smin_value && | |
4063 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
4064 | } |
4065 | ||
4066 | /* Maximum number of register states that can exist at once */ | |
4067 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
4068 | struct idpair { | |
4069 | u32 old; | |
4070 | u32 cur; | |
4071 | }; | |
4072 | ||
4073 | /* If in the old state two registers had the same id, then they need to have | |
4074 | * the same id in the new state as well. But that id could be different from | |
4075 | * the old state, so we need to track the mapping from old to new ids. | |
4076 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
4077 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
4078 | * regs with a different old id could still have new id 9, we don't care about | |
4079 | * that. | |
4080 | * So we look through our idmap to see if this old id has been seen before. If | |
4081 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 4082 | */ |
f1174f77 | 4083 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 4084 | { |
f1174f77 | 4085 | unsigned int i; |
969bf05e | 4086 | |
f1174f77 EC |
4087 | for (i = 0; i < ID_MAP_SIZE; i++) { |
4088 | if (!idmap[i].old) { | |
4089 | /* Reached an empty slot; haven't seen this id before */ | |
4090 | idmap[i].old = old_id; | |
4091 | idmap[i].cur = cur_id; | |
4092 | return true; | |
4093 | } | |
4094 | if (idmap[i].old == old_id) | |
4095 | return idmap[i].cur == cur_id; | |
4096 | } | |
4097 | /* We ran out of idmap slots, which should be impossible */ | |
4098 | WARN_ON_ONCE(1); | |
4099 | return false; | |
4100 | } | |
4101 | ||
4102 | /* Returns true if (rold safe implies rcur safe) */ | |
1b688a19 EC |
4103 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
4104 | struct idpair *idmap) | |
f1174f77 | 4105 | { |
f4d7e40a AS |
4106 | bool equal; |
4107 | ||
dc503a8a EC |
4108 | if (!(rold->live & REG_LIVE_READ)) |
4109 | /* explored state didn't use this */ | |
4110 | return true; | |
4111 | ||
f4d7e40a AS |
4112 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0; |
4113 | ||
4114 | if (rold->type == PTR_TO_STACK) | |
4115 | /* two stack pointers are equal only if they're pointing to | |
4116 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
4117 | */ | |
4118 | return equal && rold->frameno == rcur->frameno; | |
4119 | ||
4120 | if (equal) | |
969bf05e AS |
4121 | return true; |
4122 | ||
f1174f77 EC |
4123 | if (rold->type == NOT_INIT) |
4124 | /* explored state can't have used this */ | |
969bf05e | 4125 | return true; |
f1174f77 EC |
4126 | if (rcur->type == NOT_INIT) |
4127 | return false; | |
4128 | switch (rold->type) { | |
4129 | case SCALAR_VALUE: | |
4130 | if (rcur->type == SCALAR_VALUE) { | |
4131 | /* new val must satisfy old val knowledge */ | |
4132 | return range_within(rold, rcur) && | |
4133 | tnum_in(rold->var_off, rcur->var_off); | |
4134 | } else { | |
179d1c56 JH |
4135 | /* We're trying to use a pointer in place of a scalar. |
4136 | * Even if the scalar was unbounded, this could lead to | |
4137 | * pointer leaks because scalars are allowed to leak | |
4138 | * while pointers are not. We could make this safe in | |
4139 | * special cases if root is calling us, but it's | |
4140 | * probably not worth the hassle. | |
f1174f77 | 4141 | */ |
179d1c56 | 4142 | return false; |
f1174f77 EC |
4143 | } |
4144 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
4145 | /* If the new min/max/var_off satisfy the old ones and |
4146 | * everything else matches, we are OK. | |
4147 | * We don't care about the 'id' value, because nothing | |
4148 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) | |
4149 | */ | |
4150 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
4151 | range_within(rold, rcur) && | |
4152 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
4153 | case PTR_TO_MAP_VALUE_OR_NULL: |
4154 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
4155 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
4156 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
4157 | * checked, doing so could have affected others with the same | |
4158 | * id, and we can't check for that because we lost the id when | |
4159 | * we converted to a PTR_TO_MAP_VALUE. | |
4160 | */ | |
4161 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
4162 | return false; | |
4163 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
4164 | return false; | |
4165 | /* Check our ids match any regs they're supposed to */ | |
4166 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 4167 | case PTR_TO_PACKET_META: |
f1174f77 | 4168 | case PTR_TO_PACKET: |
de8f3a83 | 4169 | if (rcur->type != rold->type) |
f1174f77 EC |
4170 | return false; |
4171 | /* We must have at least as much range as the old ptr | |
4172 | * did, so that any accesses which were safe before are | |
4173 | * still safe. This is true even if old range < old off, | |
4174 | * since someone could have accessed through (ptr - k), or | |
4175 | * even done ptr -= k in a register, to get a safe access. | |
4176 | */ | |
4177 | if (rold->range > rcur->range) | |
4178 | return false; | |
4179 | /* If the offsets don't match, we can't trust our alignment; | |
4180 | * nor can we be sure that we won't fall out of range. | |
4181 | */ | |
4182 | if (rold->off != rcur->off) | |
4183 | return false; | |
4184 | /* id relations must be preserved */ | |
4185 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
4186 | return false; | |
4187 | /* new val must satisfy old val knowledge */ | |
4188 | return range_within(rold, rcur) && | |
4189 | tnum_in(rold->var_off, rcur->var_off); | |
4190 | case PTR_TO_CTX: | |
4191 | case CONST_PTR_TO_MAP: | |
f1174f77 EC |
4192 | case PTR_TO_PACKET_END: |
4193 | /* Only valid matches are exact, which memcmp() above | |
4194 | * would have accepted | |
4195 | */ | |
4196 | default: | |
4197 | /* Don't know what's going on, just say it's not safe */ | |
4198 | return false; | |
4199 | } | |
969bf05e | 4200 | |
f1174f77 EC |
4201 | /* Shouldn't get here; if we do, say it's not safe */ |
4202 | WARN_ON_ONCE(1); | |
969bf05e AS |
4203 | return false; |
4204 | } | |
4205 | ||
f4d7e40a AS |
4206 | static bool stacksafe(struct bpf_func_state *old, |
4207 | struct bpf_func_state *cur, | |
638f5b90 AS |
4208 | struct idpair *idmap) |
4209 | { | |
4210 | int i, spi; | |
4211 | ||
4212 | /* if explored stack has more populated slots than current stack | |
4213 | * such stacks are not equivalent | |
4214 | */ | |
4215 | if (old->allocated_stack > cur->allocated_stack) | |
4216 | return false; | |
4217 | ||
4218 | /* walk slots of the explored stack and ignore any additional | |
4219 | * slots in the current stack, since explored(safe) state | |
4220 | * didn't use them | |
4221 | */ | |
4222 | for (i = 0; i < old->allocated_stack; i++) { | |
4223 | spi = i / BPF_REG_SIZE; | |
4224 | ||
cc2b14d5 AS |
4225 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
4226 | /* explored state didn't use this */ | |
fd05e57b | 4227 | continue; |
cc2b14d5 | 4228 | |
638f5b90 AS |
4229 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
4230 | continue; | |
cc2b14d5 AS |
4231 | /* if old state was safe with misc data in the stack |
4232 | * it will be safe with zero-initialized stack. | |
4233 | * The opposite is not true | |
4234 | */ | |
4235 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
4236 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
4237 | continue; | |
638f5b90 AS |
4238 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
4239 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
4240 | /* Ex: old explored (safe) state has STACK_SPILL in | |
4241 | * this stack slot, but current has has STACK_MISC -> | |
4242 | * this verifier states are not equivalent, | |
4243 | * return false to continue verification of this path | |
4244 | */ | |
4245 | return false; | |
4246 | if (i % BPF_REG_SIZE) | |
4247 | continue; | |
4248 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
4249 | continue; | |
4250 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
4251 | &cur->stack[spi].spilled_ptr, | |
4252 | idmap)) | |
4253 | /* when explored and current stack slot are both storing | |
4254 | * spilled registers, check that stored pointers types | |
4255 | * are the same as well. | |
4256 | * Ex: explored safe path could have stored | |
4257 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
4258 | * but current path has stored: | |
4259 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
4260 | * such verifier states are not equivalent. | |
4261 | * return false to continue verification of this path | |
4262 | */ | |
4263 | return false; | |
4264 | } | |
4265 | return true; | |
4266 | } | |
4267 | ||
f1bca824 AS |
4268 | /* compare two verifier states |
4269 | * | |
4270 | * all states stored in state_list are known to be valid, since | |
4271 | * verifier reached 'bpf_exit' instruction through them | |
4272 | * | |
4273 | * this function is called when verifier exploring different branches of | |
4274 | * execution popped from the state stack. If it sees an old state that has | |
4275 | * more strict register state and more strict stack state then this execution | |
4276 | * branch doesn't need to be explored further, since verifier already | |
4277 | * concluded that more strict state leads to valid finish. | |
4278 | * | |
4279 | * Therefore two states are equivalent if register state is more conservative | |
4280 | * and explored stack state is more conservative than the current one. | |
4281 | * Example: | |
4282 | * explored current | |
4283 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
4284 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
4285 | * | |
4286 | * In other words if current stack state (one being explored) has more | |
4287 | * valid slots than old one that already passed validation, it means | |
4288 | * the verifier can stop exploring and conclude that current state is valid too | |
4289 | * | |
4290 | * Similarly with registers. If explored state has register type as invalid | |
4291 | * whereas register type in current state is meaningful, it means that | |
4292 | * the current state will reach 'bpf_exit' instruction safely | |
4293 | */ | |
f4d7e40a AS |
4294 | static bool func_states_equal(struct bpf_func_state *old, |
4295 | struct bpf_func_state *cur) | |
f1bca824 | 4296 | { |
f1174f77 EC |
4297 | struct idpair *idmap; |
4298 | bool ret = false; | |
f1bca824 AS |
4299 | int i; |
4300 | ||
f1174f77 EC |
4301 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
4302 | /* If we failed to allocate the idmap, just say it's not safe */ | |
4303 | if (!idmap) | |
1a0dc1ac | 4304 | return false; |
f1174f77 EC |
4305 | |
4306 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 4307 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 4308 | goto out_free; |
f1bca824 AS |
4309 | } |
4310 | ||
638f5b90 AS |
4311 | if (!stacksafe(old, cur, idmap)) |
4312 | goto out_free; | |
f1174f77 EC |
4313 | ret = true; |
4314 | out_free: | |
4315 | kfree(idmap); | |
4316 | return ret; | |
f1bca824 AS |
4317 | } |
4318 | ||
f4d7e40a AS |
4319 | static bool states_equal(struct bpf_verifier_env *env, |
4320 | struct bpf_verifier_state *old, | |
4321 | struct bpf_verifier_state *cur) | |
4322 | { | |
4323 | int i; | |
4324 | ||
4325 | if (old->curframe != cur->curframe) | |
4326 | return false; | |
4327 | ||
4328 | /* for states to be equal callsites have to be the same | |
4329 | * and all frame states need to be equivalent | |
4330 | */ | |
4331 | for (i = 0; i <= old->curframe; i++) { | |
4332 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
4333 | return false; | |
4334 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
4335 | return false; | |
4336 | } | |
4337 | return true; | |
4338 | } | |
4339 | ||
8e9cd9ce | 4340 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
4341 | * straight-line code between a state and its parent. When we arrive at an |
4342 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
4343 | * code, so read marks in the state must propagate to the parent regardless | |
4344 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
4345 | * in mark_reg_read() and mark_stack_slot_read() is for. | |
8e9cd9ce | 4346 | */ |
f4d7e40a AS |
4347 | static int propagate_liveness(struct bpf_verifier_env *env, |
4348 | const struct bpf_verifier_state *vstate, | |
4349 | struct bpf_verifier_state *vparent) | |
dc503a8a | 4350 | { |
f4d7e40a AS |
4351 | int i, frame, err = 0; |
4352 | struct bpf_func_state *state, *parent; | |
dc503a8a | 4353 | |
f4d7e40a AS |
4354 | if (vparent->curframe != vstate->curframe) { |
4355 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
4356 | vparent->curframe, vstate->curframe); | |
4357 | return -EFAULT; | |
4358 | } | |
dc503a8a EC |
4359 | /* Propagate read liveness of registers... */ |
4360 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
4361 | /* We don't need to worry about FP liveness because it's read-only */ | |
4362 | for (i = 0; i < BPF_REG_FP; i++) { | |
f4d7e40a | 4363 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
63f45f84 | 4364 | continue; |
f4d7e40a AS |
4365 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
4366 | err = mark_reg_read(env, vstate, vparent, i); | |
4367 | if (err) | |
4368 | return err; | |
dc503a8a EC |
4369 | } |
4370 | } | |
f4d7e40a | 4371 | |
dc503a8a | 4372 | /* ... and stack slots */ |
f4d7e40a AS |
4373 | for (frame = 0; frame <= vstate->curframe; frame++) { |
4374 | state = vstate->frame[frame]; | |
4375 | parent = vparent->frame[frame]; | |
4376 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && | |
4377 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
f4d7e40a AS |
4378 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
4379 | continue; | |
4380 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) | |
4381 | mark_stack_slot_read(env, vstate, vparent, i, frame); | |
dc503a8a EC |
4382 | } |
4383 | } | |
f4d7e40a | 4384 | return err; |
dc503a8a EC |
4385 | } |
4386 | ||
58e2af8b | 4387 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 4388 | { |
58e2af8b JK |
4389 | struct bpf_verifier_state_list *new_sl; |
4390 | struct bpf_verifier_state_list *sl; | |
638f5b90 | 4391 | struct bpf_verifier_state *cur = env->cur_state; |
f4d7e40a | 4392 | int i, j, err; |
f1bca824 AS |
4393 | |
4394 | sl = env->explored_states[insn_idx]; | |
4395 | if (!sl) | |
4396 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
4397 | * be doing state search here | |
4398 | */ | |
4399 | return 0; | |
4400 | ||
4401 | while (sl != STATE_LIST_MARK) { | |
638f5b90 | 4402 | if (states_equal(env, &sl->state, cur)) { |
f1bca824 | 4403 | /* reached equivalent register/stack state, |
dc503a8a EC |
4404 | * prune the search. |
4405 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
4406 | * If we have any write marks in env->cur_state, they |
4407 | * will prevent corresponding reads in the continuation | |
4408 | * from reaching our parent (an explored_state). Our | |
4409 | * own state will get the read marks recorded, but | |
4410 | * they'll be immediately forgotten as we're pruning | |
4411 | * this state and will pop a new one. | |
f1bca824 | 4412 | */ |
f4d7e40a AS |
4413 | err = propagate_liveness(env, &sl->state, cur); |
4414 | if (err) | |
4415 | return err; | |
f1bca824 | 4416 | return 1; |
dc503a8a | 4417 | } |
f1bca824 AS |
4418 | sl = sl->next; |
4419 | } | |
4420 | ||
4421 | /* there were no equivalent states, remember current one. | |
4422 | * technically the current state is not proven to be safe yet, | |
f4d7e40a AS |
4423 | * but it will either reach outer most bpf_exit (which means it's safe) |
4424 | * or it will be rejected. Since there are no loops, we won't be | |
4425 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) | |
4426 | * again on the way to bpf_exit | |
f1bca824 | 4427 | */ |
638f5b90 | 4428 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
4429 | if (!new_sl) |
4430 | return -ENOMEM; | |
4431 | ||
4432 | /* add new state to the head of linked list */ | |
1969db47 AS |
4433 | err = copy_verifier_state(&new_sl->state, cur); |
4434 | if (err) { | |
4435 | free_verifier_state(&new_sl->state, false); | |
4436 | kfree(new_sl); | |
4437 | return err; | |
4438 | } | |
f1bca824 AS |
4439 | new_sl->next = env->explored_states[insn_idx]; |
4440 | env->explored_states[insn_idx] = new_sl; | |
dc503a8a | 4441 | /* connect new state to parentage chain */ |
638f5b90 | 4442 | cur->parent = &new_sl->state; |
8e9cd9ce EC |
4443 | /* clear write marks in current state: the writes we did are not writes |
4444 | * our child did, so they don't screen off its reads from us. | |
4445 | * (There are no read marks in current state, because reads always mark | |
4446 | * their parent and current state never has children yet. Only | |
4447 | * explored_states can get read marks.) | |
4448 | */ | |
dc503a8a | 4449 | for (i = 0; i < BPF_REG_FP; i++) |
f4d7e40a AS |
4450 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
4451 | ||
4452 | /* all stack frames are accessible from callee, clear them all */ | |
4453 | for (j = 0; j <= cur->curframe; j++) { | |
4454 | struct bpf_func_state *frame = cur->frame[j]; | |
4455 | ||
4456 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) | |
cc2b14d5 | 4457 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
f4d7e40a | 4458 | } |
f1bca824 AS |
4459 | return 0; |
4460 | } | |
4461 | ||
58e2af8b | 4462 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 4463 | { |
638f5b90 | 4464 | struct bpf_verifier_state *state; |
17a52670 | 4465 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 4466 | struct bpf_reg_state *regs; |
f4d7e40a | 4467 | int insn_cnt = env->prog->len, i; |
17a52670 AS |
4468 | int insn_idx, prev_insn_idx = 0; |
4469 | int insn_processed = 0; | |
4470 | bool do_print_state = false; | |
4471 | ||
638f5b90 AS |
4472 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
4473 | if (!state) | |
4474 | return -ENOMEM; | |
f4d7e40a | 4475 | state->curframe = 0; |
dc503a8a | 4476 | state->parent = NULL; |
f4d7e40a AS |
4477 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
4478 | if (!state->frame[0]) { | |
4479 | kfree(state); | |
4480 | return -ENOMEM; | |
4481 | } | |
4482 | env->cur_state = state; | |
4483 | init_func_state(env, state->frame[0], | |
4484 | BPF_MAIN_FUNC /* callsite */, | |
4485 | 0 /* frameno */, | |
4486 | 0 /* subprogno, zero == main subprog */); | |
17a52670 AS |
4487 | insn_idx = 0; |
4488 | for (;;) { | |
4489 | struct bpf_insn *insn; | |
4490 | u8 class; | |
4491 | int err; | |
4492 | ||
4493 | if (insn_idx >= insn_cnt) { | |
61bd5218 | 4494 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
17a52670 AS |
4495 | insn_idx, insn_cnt); |
4496 | return -EFAULT; | |
4497 | } | |
4498 | ||
4499 | insn = &insns[insn_idx]; | |
4500 | class = BPF_CLASS(insn->code); | |
4501 | ||
07016151 | 4502 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
4503 | verbose(env, |
4504 | "BPF program is too large. Processed %d insn\n", | |
17a52670 AS |
4505 | insn_processed); |
4506 | return -E2BIG; | |
4507 | } | |
4508 | ||
f1bca824 AS |
4509 | err = is_state_visited(env, insn_idx); |
4510 | if (err < 0) | |
4511 | return err; | |
4512 | if (err == 1) { | |
4513 | /* found equivalent state, can prune the search */ | |
61bd5218 | 4514 | if (env->log.level) { |
f1bca824 | 4515 | if (do_print_state) |
61bd5218 | 4516 | verbose(env, "\nfrom %d to %d: safe\n", |
f1bca824 AS |
4517 | prev_insn_idx, insn_idx); |
4518 | else | |
61bd5218 | 4519 | verbose(env, "%d: safe\n", insn_idx); |
f1bca824 AS |
4520 | } |
4521 | goto process_bpf_exit; | |
4522 | } | |
4523 | ||
3c2ce60b DB |
4524 | if (need_resched()) |
4525 | cond_resched(); | |
4526 | ||
61bd5218 JK |
4527 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
4528 | if (env->log.level > 1) | |
4529 | verbose(env, "%d:", insn_idx); | |
c5fc9692 | 4530 | else |
61bd5218 | 4531 | verbose(env, "\nfrom %d to %d:", |
c5fc9692 | 4532 | prev_insn_idx, insn_idx); |
f4d7e40a | 4533 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
4534 | do_print_state = false; |
4535 | } | |
4536 | ||
61bd5218 | 4537 | if (env->log.level) { |
7105e828 DB |
4538 | const struct bpf_insn_cbs cbs = { |
4539 | .cb_print = verbose, | |
4540 | }; | |
4541 | ||
61bd5218 | 4542 | verbose(env, "%d: ", insn_idx); |
7105e828 | 4543 | print_bpf_insn(&cbs, env, insn, env->allow_ptr_leaks); |
17a52670 AS |
4544 | } |
4545 | ||
cae1927c JK |
4546 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
4547 | err = bpf_prog_offload_verify_insn(env, insn_idx, | |
4548 | prev_insn_idx); | |
4549 | if (err) | |
4550 | return err; | |
4551 | } | |
13a27dfc | 4552 | |
638f5b90 | 4553 | regs = cur_regs(env); |
c131187d | 4554 | env->insn_aux_data[insn_idx].seen = true; |
17a52670 | 4555 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 4556 | err = check_alu_op(env, insn); |
17a52670 AS |
4557 | if (err) |
4558 | return err; | |
4559 | ||
4560 | } else if (class == BPF_LDX) { | |
3df126f3 | 4561 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
4562 | |
4563 | /* check for reserved fields is already done */ | |
4564 | ||
17a52670 | 4565 | /* check src operand */ |
dc503a8a | 4566 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4567 | if (err) |
4568 | return err; | |
4569 | ||
dc503a8a | 4570 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
4571 | if (err) |
4572 | return err; | |
4573 | ||
725f9dcd AS |
4574 | src_reg_type = regs[insn->src_reg].type; |
4575 | ||
17a52670 AS |
4576 | /* check that memory (src_reg + off) is readable, |
4577 | * the state of dst_reg will be updated by this func | |
4578 | */ | |
31fd8581 | 4579 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
17a52670 AS |
4580 | BPF_SIZE(insn->code), BPF_READ, |
4581 | insn->dst_reg); | |
4582 | if (err) | |
4583 | return err; | |
4584 | ||
3df126f3 JK |
4585 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
4586 | ||
4587 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
4588 | /* saw a valid insn |
4589 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 4590 | * save type to validate intersecting paths |
9bac3d6d | 4591 | */ |
3df126f3 | 4592 | *prev_src_type = src_reg_type; |
9bac3d6d | 4593 | |
3df126f3 | 4594 | } else if (src_reg_type != *prev_src_type && |
9bac3d6d | 4595 | (src_reg_type == PTR_TO_CTX || |
3df126f3 | 4596 | *prev_src_type == PTR_TO_CTX)) { |
9bac3d6d AS |
4597 | /* ABuser program is trying to use the same insn |
4598 | * dst_reg = *(u32*) (src_reg + off) | |
4599 | * with different pointer types: | |
4600 | * src_reg == ctx in one branch and | |
4601 | * src_reg == stack|map in some other branch. | |
4602 | * Reject it. | |
4603 | */ | |
61bd5218 | 4604 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
4605 | return -EINVAL; |
4606 | } | |
4607 | ||
17a52670 | 4608 | } else if (class == BPF_STX) { |
3df126f3 | 4609 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 4610 | |
17a52670 | 4611 | if (BPF_MODE(insn->code) == BPF_XADD) { |
31fd8581 | 4612 | err = check_xadd(env, insn_idx, insn); |
17a52670 AS |
4613 | if (err) |
4614 | return err; | |
4615 | insn_idx++; | |
4616 | continue; | |
4617 | } | |
4618 | ||
17a52670 | 4619 | /* check src1 operand */ |
dc503a8a | 4620 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4621 | if (err) |
4622 | return err; | |
4623 | /* check src2 operand */ | |
dc503a8a | 4624 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4625 | if (err) |
4626 | return err; | |
4627 | ||
d691f9e8 AS |
4628 | dst_reg_type = regs[insn->dst_reg].type; |
4629 | ||
17a52670 | 4630 | /* check that memory (dst_reg + off) is writeable */ |
31fd8581 | 4631 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
4632 | BPF_SIZE(insn->code), BPF_WRITE, |
4633 | insn->src_reg); | |
4634 | if (err) | |
4635 | return err; | |
4636 | ||
3df126f3 JK |
4637 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
4638 | ||
4639 | if (*prev_dst_type == NOT_INIT) { | |
4640 | *prev_dst_type = dst_reg_type; | |
4641 | } else if (dst_reg_type != *prev_dst_type && | |
d691f9e8 | 4642 | (dst_reg_type == PTR_TO_CTX || |
3df126f3 | 4643 | *prev_dst_type == PTR_TO_CTX)) { |
61bd5218 | 4644 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
4645 | return -EINVAL; |
4646 | } | |
4647 | ||
17a52670 AS |
4648 | } else if (class == BPF_ST) { |
4649 | if (BPF_MODE(insn->code) != BPF_MEM || | |
4650 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 4651 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
4652 | return -EINVAL; |
4653 | } | |
4654 | /* check src operand */ | |
dc503a8a | 4655 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4656 | if (err) |
4657 | return err; | |
4658 | ||
4659 | /* check that memory (dst_reg + off) is writeable */ | |
31fd8581 | 4660 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
4661 | BPF_SIZE(insn->code), BPF_WRITE, |
4662 | -1); | |
4663 | if (err) | |
4664 | return err; | |
4665 | ||
4666 | } else if (class == BPF_JMP) { | |
4667 | u8 opcode = BPF_OP(insn->code); | |
4668 | ||
4669 | if (opcode == BPF_CALL) { | |
4670 | if (BPF_SRC(insn->code) != BPF_K || | |
4671 | insn->off != 0 || | |
f4d7e40a AS |
4672 | (insn->src_reg != BPF_REG_0 && |
4673 | insn->src_reg != BPF_PSEUDO_CALL) || | |
17a52670 | 4674 | insn->dst_reg != BPF_REG_0) { |
61bd5218 | 4675 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
4676 | return -EINVAL; |
4677 | } | |
4678 | ||
f4d7e40a AS |
4679 | if (insn->src_reg == BPF_PSEUDO_CALL) |
4680 | err = check_func_call(env, insn, &insn_idx); | |
4681 | else | |
4682 | err = check_helper_call(env, insn->imm, insn_idx); | |
17a52670 AS |
4683 | if (err) |
4684 | return err; | |
4685 | ||
4686 | } else if (opcode == BPF_JA) { | |
4687 | if (BPF_SRC(insn->code) != BPF_K || | |
4688 | insn->imm != 0 || | |
4689 | insn->src_reg != BPF_REG_0 || | |
4690 | insn->dst_reg != BPF_REG_0) { | |
61bd5218 | 4691 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
4692 | return -EINVAL; |
4693 | } | |
4694 | ||
4695 | insn_idx += insn->off + 1; | |
4696 | continue; | |
4697 | ||
4698 | } else if (opcode == BPF_EXIT) { | |
4699 | if (BPF_SRC(insn->code) != BPF_K || | |
4700 | insn->imm != 0 || | |
4701 | insn->src_reg != BPF_REG_0 || | |
4702 | insn->dst_reg != BPF_REG_0) { | |
61bd5218 | 4703 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
4704 | return -EINVAL; |
4705 | } | |
4706 | ||
f4d7e40a AS |
4707 | if (state->curframe) { |
4708 | /* exit from nested function */ | |
4709 | prev_insn_idx = insn_idx; | |
4710 | err = prepare_func_exit(env, &insn_idx); | |
4711 | if (err) | |
4712 | return err; | |
4713 | do_print_state = true; | |
4714 | continue; | |
4715 | } | |
4716 | ||
17a52670 AS |
4717 | /* eBPF calling convetion is such that R0 is used |
4718 | * to return the value from eBPF program. | |
4719 | * Make sure that it's readable at this time | |
4720 | * of bpf_exit, which means that program wrote | |
4721 | * something into it earlier | |
4722 | */ | |
dc503a8a | 4723 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
17a52670 AS |
4724 | if (err) |
4725 | return err; | |
4726 | ||
1be7f75d | 4727 | if (is_pointer_value(env, BPF_REG_0)) { |
61bd5218 | 4728 | verbose(env, "R0 leaks addr as return value\n"); |
1be7f75d AS |
4729 | return -EACCES; |
4730 | } | |
4731 | ||
390ee7e2 AS |
4732 | err = check_return_code(env); |
4733 | if (err) | |
4734 | return err; | |
f1bca824 | 4735 | process_bpf_exit: |
638f5b90 AS |
4736 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
4737 | if (err < 0) { | |
4738 | if (err != -ENOENT) | |
4739 | return err; | |
17a52670 AS |
4740 | break; |
4741 | } else { | |
4742 | do_print_state = true; | |
4743 | continue; | |
4744 | } | |
4745 | } else { | |
4746 | err = check_cond_jmp_op(env, insn, &insn_idx); | |
4747 | if (err) | |
4748 | return err; | |
4749 | } | |
4750 | } else if (class == BPF_LD) { | |
4751 | u8 mode = BPF_MODE(insn->code); | |
4752 | ||
4753 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
4754 | err = check_ld_abs(env, insn); |
4755 | if (err) | |
4756 | return err; | |
4757 | ||
17a52670 AS |
4758 | } else if (mode == BPF_IMM) { |
4759 | err = check_ld_imm(env, insn); | |
4760 | if (err) | |
4761 | return err; | |
4762 | ||
4763 | insn_idx++; | |
c131187d | 4764 | env->insn_aux_data[insn_idx].seen = true; |
17a52670 | 4765 | } else { |
61bd5218 | 4766 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
4767 | return -EINVAL; |
4768 | } | |
4769 | } else { | |
61bd5218 | 4770 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
4771 | return -EINVAL; |
4772 | } | |
4773 | ||
4774 | insn_idx++; | |
4775 | } | |
4776 | ||
f4d7e40a AS |
4777 | verbose(env, "processed %d insns, stack depth ", insn_processed); |
4778 | for (i = 0; i < env->subprog_cnt + 1; i++) { | |
4779 | u32 depth = env->subprog_stack_depth[i]; | |
4780 | ||
4781 | verbose(env, "%d", depth); | |
4782 | if (i + 1 < env->subprog_cnt + 1) | |
4783 | verbose(env, "+"); | |
4784 | } | |
4785 | verbose(env, "\n"); | |
4786 | env->prog->aux->stack_depth = env->subprog_stack_depth[0]; | |
17a52670 AS |
4787 | return 0; |
4788 | } | |
4789 | ||
56f668df MKL |
4790 | static int check_map_prealloc(struct bpf_map *map) |
4791 | { | |
4792 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
4793 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
4794 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
4795 | !(map->map_flags & BPF_F_NO_PREALLOC); |
4796 | } | |
4797 | ||
61bd5218 JK |
4798 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
4799 | struct bpf_map *map, | |
fdc15d38 AS |
4800 | struct bpf_prog *prog) |
4801 | ||
4802 | { | |
56f668df MKL |
4803 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
4804 | * preallocated hash maps, since doing memory allocation | |
4805 | * in overflow_handler can crash depending on where nmi got | |
4806 | * triggered. | |
4807 | */ | |
4808 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
4809 | if (!check_map_prealloc(map)) { | |
61bd5218 | 4810 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
4811 | return -EINVAL; |
4812 | } | |
4813 | if (map->inner_map_meta && | |
4814 | !check_map_prealloc(map->inner_map_meta)) { | |
61bd5218 | 4815 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
56f668df MKL |
4816 | return -EINVAL; |
4817 | } | |
fdc15d38 | 4818 | } |
a3884572 JK |
4819 | |
4820 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && | |
4821 | !bpf_offload_dev_match(prog, map)) { | |
4822 | verbose(env, "offload device mismatch between prog and map\n"); | |
4823 | return -EINVAL; | |
4824 | } | |
4825 | ||
fdc15d38 AS |
4826 | return 0; |
4827 | } | |
4828 | ||
0246e64d AS |
4829 | /* look for pseudo eBPF instructions that access map FDs and |
4830 | * replace them with actual map pointers | |
4831 | */ | |
58e2af8b | 4832 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
4833 | { |
4834 | struct bpf_insn *insn = env->prog->insnsi; | |
4835 | int insn_cnt = env->prog->len; | |
fdc15d38 | 4836 | int i, j, err; |
0246e64d | 4837 | |
f1f7714e | 4838 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
4839 | if (err) |
4840 | return err; | |
4841 | ||
0246e64d | 4842 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 4843 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 4844 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 4845 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
4846 | return -EINVAL; |
4847 | } | |
4848 | ||
d691f9e8 AS |
4849 | if (BPF_CLASS(insn->code) == BPF_STX && |
4850 | ((BPF_MODE(insn->code) != BPF_MEM && | |
4851 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 4852 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
4853 | return -EINVAL; |
4854 | } | |
4855 | ||
0246e64d AS |
4856 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
4857 | struct bpf_map *map; | |
4858 | struct fd f; | |
4859 | ||
4860 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
4861 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
4862 | insn[1].off != 0) { | |
61bd5218 | 4863 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
4864 | return -EINVAL; |
4865 | } | |
4866 | ||
4867 | if (insn->src_reg == 0) | |
4868 | /* valid generic load 64-bit imm */ | |
4869 | goto next_insn; | |
4870 | ||
4871 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { | |
61bd5218 JK |
4872 | verbose(env, |
4873 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
4874 | return -EINVAL; |
4875 | } | |
4876 | ||
4877 | f = fdget(insn->imm); | |
c2101297 | 4878 | map = __bpf_map_get(f); |
0246e64d | 4879 | if (IS_ERR(map)) { |
61bd5218 | 4880 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
0246e64d | 4881 | insn->imm); |
0246e64d AS |
4882 | return PTR_ERR(map); |
4883 | } | |
4884 | ||
61bd5218 | 4885 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
4886 | if (err) { |
4887 | fdput(f); | |
4888 | return err; | |
4889 | } | |
4890 | ||
0246e64d AS |
4891 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
4892 | insn[0].imm = (u32) (unsigned long) map; | |
4893 | insn[1].imm = ((u64) (unsigned long) map) >> 32; | |
4894 | ||
4895 | /* check whether we recorded this map already */ | |
4896 | for (j = 0; j < env->used_map_cnt; j++) | |
4897 | if (env->used_maps[j] == map) { | |
4898 | fdput(f); | |
4899 | goto next_insn; | |
4900 | } | |
4901 | ||
4902 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
4903 | fdput(f); | |
4904 | return -E2BIG; | |
4905 | } | |
4906 | ||
0246e64d AS |
4907 | /* hold the map. If the program is rejected by verifier, |
4908 | * the map will be released by release_maps() or it | |
4909 | * will be used by the valid program until it's unloaded | |
4910 | * and all maps are released in free_bpf_prog_info() | |
4911 | */ | |
92117d84 AS |
4912 | map = bpf_map_inc(map, false); |
4913 | if (IS_ERR(map)) { | |
4914 | fdput(f); | |
4915 | return PTR_ERR(map); | |
4916 | } | |
4917 | env->used_maps[env->used_map_cnt++] = map; | |
4918 | ||
0246e64d AS |
4919 | fdput(f); |
4920 | next_insn: | |
4921 | insn++; | |
4922 | i++; | |
4923 | } | |
4924 | } | |
4925 | ||
4926 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
4927 | * 'struct bpf_map *' into a register instead of user map_fd. | |
4928 | * These pointers will be used later by verifier to validate map access. | |
4929 | */ | |
4930 | return 0; | |
4931 | } | |
4932 | ||
4933 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 4934 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d AS |
4935 | { |
4936 | int i; | |
4937 | ||
4938 | for (i = 0; i < env->used_map_cnt; i++) | |
4939 | bpf_map_put(env->used_maps[i]); | |
4940 | } | |
4941 | ||
4942 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 4943 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
4944 | { |
4945 | struct bpf_insn *insn = env->prog->insnsi; | |
4946 | int insn_cnt = env->prog->len; | |
4947 | int i; | |
4948 | ||
4949 | for (i = 0; i < insn_cnt; i++, insn++) | |
4950 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
4951 | insn->src_reg = 0; | |
4952 | } | |
4953 | ||
8041902d AS |
4954 | /* single env->prog->insni[off] instruction was replaced with the range |
4955 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
4956 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
4957 | */ | |
4958 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, | |
4959 | u32 off, u32 cnt) | |
4960 | { | |
4961 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
c131187d | 4962 | int i; |
8041902d AS |
4963 | |
4964 | if (cnt == 1) | |
4965 | return 0; | |
4966 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); | |
4967 | if (!new_data) | |
4968 | return -ENOMEM; | |
4969 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
4970 | memcpy(new_data + off + cnt - 1, old_data + off, | |
4971 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
c131187d AS |
4972 | for (i = off; i < off + cnt - 1; i++) |
4973 | new_data[i].seen = true; | |
8041902d AS |
4974 | env->insn_aux_data = new_data; |
4975 | vfree(old_data); | |
4976 | return 0; | |
4977 | } | |
4978 | ||
cc8b0b92 AS |
4979 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
4980 | { | |
4981 | int i; | |
4982 | ||
4983 | if (len == 1) | |
4984 | return; | |
4985 | for (i = 0; i < env->subprog_cnt; i++) { | |
4986 | if (env->subprog_starts[i] < off) | |
4987 | continue; | |
4988 | env->subprog_starts[i] += len - 1; | |
4989 | } | |
4990 | } | |
4991 | ||
8041902d AS |
4992 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
4993 | const struct bpf_insn *patch, u32 len) | |
4994 | { | |
4995 | struct bpf_prog *new_prog; | |
4996 | ||
4997 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4998 | if (!new_prog) | |
4999 | return NULL; | |
5000 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) | |
5001 | return NULL; | |
cc8b0b92 | 5002 | adjust_subprog_starts(env, off, len); |
8041902d AS |
5003 | return new_prog; |
5004 | } | |
5005 | ||
c131187d AS |
5006 | /* The verifier does more data flow analysis than llvm and will not explore |
5007 | * branches that are dead at run time. Malicious programs can have dead code | |
5008 | * too. Therefore replace all dead at-run-time code with nops. | |
5009 | */ | |
5010 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
5011 | { | |
5012 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
5013 | struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0); | |
5014 | struct bpf_insn *insn = env->prog->insnsi; | |
5015 | const int insn_cnt = env->prog->len; | |
5016 | int i; | |
5017 | ||
5018 | for (i = 0; i < insn_cnt; i++) { | |
5019 | if (aux_data[i].seen) | |
5020 | continue; | |
5021 | memcpy(insn + i, &nop, sizeof(nop)); | |
5022 | } | |
5023 | } | |
5024 | ||
9bac3d6d AS |
5025 | /* convert load instructions that access fields of 'struct __sk_buff' |
5026 | * into sequence of instructions that access fields of 'struct sk_buff' | |
5027 | */ | |
58e2af8b | 5028 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 5029 | { |
00176a34 | 5030 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 5031 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 5032 | const int insn_cnt = env->prog->len; |
36bbef52 | 5033 | struct bpf_insn insn_buf[16], *insn; |
9bac3d6d | 5034 | struct bpf_prog *new_prog; |
d691f9e8 | 5035 | enum bpf_access_type type; |
f96da094 DB |
5036 | bool is_narrower_load; |
5037 | u32 target_size; | |
9bac3d6d | 5038 | |
36bbef52 DB |
5039 | if (ops->gen_prologue) { |
5040 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, | |
5041 | env->prog); | |
5042 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 5043 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
5044 | return -EINVAL; |
5045 | } else if (cnt) { | |
8041902d | 5046 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
5047 | if (!new_prog) |
5048 | return -ENOMEM; | |
8041902d | 5049 | |
36bbef52 | 5050 | env->prog = new_prog; |
3df126f3 | 5051 | delta += cnt - 1; |
36bbef52 DB |
5052 | } |
5053 | } | |
5054 | ||
5055 | if (!ops->convert_ctx_access) | |
9bac3d6d AS |
5056 | return 0; |
5057 | ||
3df126f3 | 5058 | insn = env->prog->insnsi + delta; |
36bbef52 | 5059 | |
9bac3d6d | 5060 | for (i = 0; i < insn_cnt; i++, insn++) { |
62c7989b DB |
5061 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
5062 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
5063 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 5064 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 5065 | type = BPF_READ; |
62c7989b DB |
5066 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
5067 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
5068 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 5069 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
5070 | type = BPF_WRITE; |
5071 | else | |
9bac3d6d AS |
5072 | continue; |
5073 | ||
8041902d | 5074 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
9bac3d6d | 5075 | continue; |
9bac3d6d | 5076 | |
31fd8581 | 5077 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 5078 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
5079 | |
5080 | /* If the read access is a narrower load of the field, | |
5081 | * convert to a 4/8-byte load, to minimum program type specific | |
5082 | * convert_ctx_access changes. If conversion is successful, | |
5083 | * we will apply proper mask to the result. | |
5084 | */ | |
f96da094 | 5085 | is_narrower_load = size < ctx_field_size; |
31fd8581 | 5086 | if (is_narrower_load) { |
f96da094 DB |
5087 | u32 off = insn->off; |
5088 | u8 size_code; | |
5089 | ||
5090 | if (type == BPF_WRITE) { | |
61bd5218 | 5091 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
5092 | return -EINVAL; |
5093 | } | |
31fd8581 | 5094 | |
f96da094 | 5095 | size_code = BPF_H; |
31fd8581 YS |
5096 | if (ctx_field_size == 4) |
5097 | size_code = BPF_W; | |
5098 | else if (ctx_field_size == 8) | |
5099 | size_code = BPF_DW; | |
f96da094 | 5100 | |
31fd8581 YS |
5101 | insn->off = off & ~(ctx_field_size - 1); |
5102 | insn->code = BPF_LDX | BPF_MEM | size_code; | |
5103 | } | |
f96da094 DB |
5104 | |
5105 | target_size = 0; | |
5106 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, | |
5107 | &target_size); | |
5108 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || | |
5109 | (ctx_field_size && !target_size)) { | |
61bd5218 | 5110 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
5111 | return -EINVAL; |
5112 | } | |
f96da094 DB |
5113 | |
5114 | if (is_narrower_load && size < target_size) { | |
31fd8581 YS |
5115 | if (ctx_field_size <= 4) |
5116 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 5117 | (1 << size * 8) - 1); |
31fd8581 YS |
5118 | else |
5119 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 5120 | (1 << size * 8) - 1); |
31fd8581 | 5121 | } |
9bac3d6d | 5122 | |
8041902d | 5123 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
5124 | if (!new_prog) |
5125 | return -ENOMEM; | |
5126 | ||
3df126f3 | 5127 | delta += cnt - 1; |
9bac3d6d AS |
5128 | |
5129 | /* keep walking new program and skip insns we just inserted */ | |
5130 | env->prog = new_prog; | |
3df126f3 | 5131 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
5132 | } |
5133 | ||
5134 | return 0; | |
5135 | } | |
5136 | ||
1c2a088a AS |
5137 | static int jit_subprogs(struct bpf_verifier_env *env) |
5138 | { | |
5139 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
5140 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
7105e828 | 5141 | struct bpf_insn *insn; |
1c2a088a AS |
5142 | void *old_bpf_func; |
5143 | int err = -ENOMEM; | |
5144 | ||
5145 | if (env->subprog_cnt == 0) | |
5146 | return 0; | |
5147 | ||
7105e828 | 5148 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
5149 | if (insn->code != (BPF_JMP | BPF_CALL) || |
5150 | insn->src_reg != BPF_PSEUDO_CALL) | |
5151 | continue; | |
5152 | subprog = find_subprog(env, i + insn->imm + 1); | |
5153 | if (subprog < 0) { | |
5154 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
5155 | i + insn->imm + 1); | |
5156 | return -EFAULT; | |
5157 | } | |
5158 | /* temporarily remember subprog id inside insn instead of | |
5159 | * aux_data, since next loop will split up all insns into funcs | |
5160 | */ | |
5161 | insn->off = subprog + 1; | |
5162 | /* remember original imm in case JIT fails and fallback | |
5163 | * to interpreter will be needed | |
5164 | */ | |
5165 | env->insn_aux_data[i].call_imm = insn->imm; | |
5166 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
5167 | insn->imm = 1; | |
5168 | } | |
5169 | ||
5170 | func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL); | |
5171 | if (!func) | |
5172 | return -ENOMEM; | |
5173 | ||
5174 | for (i = 0; i <= env->subprog_cnt; i++) { | |
5175 | subprog_start = subprog_end; | |
5176 | if (env->subprog_cnt == i) | |
5177 | subprog_end = prog->len; | |
5178 | else | |
5179 | subprog_end = env->subprog_starts[i]; | |
5180 | ||
5181 | len = subprog_end - subprog_start; | |
5182 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); | |
5183 | if (!func[i]) | |
5184 | goto out_free; | |
5185 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
5186 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 5187 | func[i]->type = prog->type; |
1c2a088a | 5188 | func[i]->len = len; |
4f74d809 DB |
5189 | if (bpf_prog_calc_tag(func[i])) |
5190 | goto out_free; | |
1c2a088a AS |
5191 | func[i]->is_func = 1; |
5192 | /* Use bpf_prog_F_tag to indicate functions in stack traces. | |
5193 | * Long term would need debug info to populate names | |
5194 | */ | |
5195 | func[i]->aux->name[0] = 'F'; | |
5196 | func[i]->aux->stack_depth = env->subprog_stack_depth[i]; | |
5197 | func[i]->jit_requested = 1; | |
5198 | func[i] = bpf_int_jit_compile(func[i]); | |
5199 | if (!func[i]->jited) { | |
5200 | err = -ENOTSUPP; | |
5201 | goto out_free; | |
5202 | } | |
5203 | cond_resched(); | |
5204 | } | |
5205 | /* at this point all bpf functions were successfully JITed | |
5206 | * now populate all bpf_calls with correct addresses and | |
5207 | * run last pass of JIT | |
5208 | */ | |
5209 | for (i = 0; i <= env->subprog_cnt; i++) { | |
5210 | insn = func[i]->insnsi; | |
5211 | for (j = 0; j < func[i]->len; j++, insn++) { | |
5212 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
5213 | insn->src_reg != BPF_PSEUDO_CALL) | |
5214 | continue; | |
5215 | subprog = insn->off; | |
5216 | insn->off = 0; | |
5217 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) | |
5218 | func[subprog]->bpf_func - | |
5219 | __bpf_call_base; | |
5220 | } | |
5221 | } | |
5222 | for (i = 0; i <= env->subprog_cnt; i++) { | |
5223 | old_bpf_func = func[i]->bpf_func; | |
5224 | tmp = bpf_int_jit_compile(func[i]); | |
5225 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
5226 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
5227 | err = -EFAULT; | |
5228 | goto out_free; | |
5229 | } | |
5230 | cond_resched(); | |
5231 | } | |
5232 | ||
5233 | /* finally lock prog and jit images for all functions and | |
5234 | * populate kallsysm | |
5235 | */ | |
5236 | for (i = 0; i <= env->subprog_cnt; i++) { | |
5237 | bpf_prog_lock_ro(func[i]); | |
5238 | bpf_prog_kallsyms_add(func[i]); | |
5239 | } | |
7105e828 DB |
5240 | |
5241 | /* Last step: make now unused interpreter insns from main | |
5242 | * prog consistent for later dump requests, so they can | |
5243 | * later look the same as if they were interpreted only. | |
5244 | */ | |
5245 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
5246 | unsigned long addr; | |
5247 | ||
5248 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
5249 | insn->src_reg != BPF_PSEUDO_CALL) | |
5250 | continue; | |
5251 | insn->off = env->insn_aux_data[i].call_imm; | |
5252 | subprog = find_subprog(env, i + insn->off + 1); | |
5253 | addr = (unsigned long)func[subprog + 1]->bpf_func; | |
5254 | addr &= PAGE_MASK; | |
5255 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) | |
5256 | addr - __bpf_call_base; | |
5257 | } | |
5258 | ||
1c2a088a AS |
5259 | prog->jited = 1; |
5260 | prog->bpf_func = func[0]->bpf_func; | |
5261 | prog->aux->func = func; | |
5262 | prog->aux->func_cnt = env->subprog_cnt + 1; | |
5263 | return 0; | |
5264 | out_free: | |
5265 | for (i = 0; i <= env->subprog_cnt; i++) | |
5266 | if (func[i]) | |
5267 | bpf_jit_free(func[i]); | |
5268 | kfree(func); | |
5269 | /* cleanup main prog to be interpreted */ | |
5270 | prog->jit_requested = 0; | |
5271 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
5272 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
5273 | insn->src_reg != BPF_PSEUDO_CALL) | |
5274 | continue; | |
5275 | insn->off = 0; | |
5276 | insn->imm = env->insn_aux_data[i].call_imm; | |
5277 | } | |
5278 | return err; | |
5279 | } | |
5280 | ||
1ea47e01 AS |
5281 | static int fixup_call_args(struct bpf_verifier_env *env) |
5282 | { | |
19d28fbd | 5283 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
5284 | struct bpf_prog *prog = env->prog; |
5285 | struct bpf_insn *insn = prog->insnsi; | |
5286 | int i, depth; | |
19d28fbd DM |
5287 | #endif |
5288 | int err; | |
1ea47e01 | 5289 | |
19d28fbd DM |
5290 | err = 0; |
5291 | if (env->prog->jit_requested) { | |
5292 | err = jit_subprogs(env); | |
5293 | if (err == 0) | |
1c2a088a | 5294 | return 0; |
19d28fbd DM |
5295 | } |
5296 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 AS |
5297 | for (i = 0; i < prog->len; i++, insn++) { |
5298 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
5299 | insn->src_reg != BPF_PSEUDO_CALL) | |
5300 | continue; | |
5301 | depth = get_callee_stack_depth(env, insn, i); | |
5302 | if (depth < 0) | |
5303 | return depth; | |
5304 | bpf_patch_call_args(insn, depth); | |
5305 | } | |
19d28fbd DM |
5306 | err = 0; |
5307 | #endif | |
5308 | return err; | |
1ea47e01 AS |
5309 | } |
5310 | ||
79741b3b | 5311 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 5312 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
5313 | * |
5314 | * this function is called after eBPF program passed verification | |
5315 | */ | |
79741b3b | 5316 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 5317 | { |
79741b3b AS |
5318 | struct bpf_prog *prog = env->prog; |
5319 | struct bpf_insn *insn = prog->insnsi; | |
e245c5c6 | 5320 | const struct bpf_func_proto *fn; |
79741b3b | 5321 | const int insn_cnt = prog->len; |
81ed18ab AS |
5322 | struct bpf_insn insn_buf[16]; |
5323 | struct bpf_prog *new_prog; | |
5324 | struct bpf_map *map_ptr; | |
5325 | int i, cnt, delta = 0; | |
e245c5c6 | 5326 | |
79741b3b AS |
5327 | for (i = 0; i < insn_cnt; i++, insn++) { |
5328 | if (insn->code != (BPF_JMP | BPF_CALL)) | |
5329 | continue; | |
cc8b0b92 AS |
5330 | if (insn->src_reg == BPF_PSEUDO_CALL) |
5331 | continue; | |
e245c5c6 | 5332 | |
79741b3b AS |
5333 | if (insn->imm == BPF_FUNC_get_route_realm) |
5334 | prog->dst_needed = 1; | |
5335 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
5336 | bpf_user_rnd_init_once(); | |
9802d865 JB |
5337 | if (insn->imm == BPF_FUNC_override_return) |
5338 | prog->kprobe_override = 1; | |
79741b3b | 5339 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
5340 | /* If we tail call into other programs, we |
5341 | * cannot make any assumptions since they can | |
5342 | * be replaced dynamically during runtime in | |
5343 | * the program array. | |
5344 | */ | |
5345 | prog->cb_access = 1; | |
80a58d02 | 5346 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
7b9f6da1 | 5347 | |
79741b3b AS |
5348 | /* mark bpf_tail_call as different opcode to avoid |
5349 | * conditional branch in the interpeter for every normal | |
5350 | * call and to prevent accidental JITing by JIT compiler | |
5351 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 5352 | */ |
79741b3b | 5353 | insn->imm = 0; |
71189fa9 | 5354 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 AS |
5355 | |
5356 | /* instead of changing every JIT dealing with tail_call | |
5357 | * emit two extra insns: | |
5358 | * if (index >= max_entries) goto out; | |
5359 | * index &= array->index_mask; | |
5360 | * to avoid out-of-bounds cpu speculation | |
5361 | */ | |
5362 | map_ptr = env->insn_aux_data[i + delta].map_ptr; | |
5363 | if (map_ptr == BPF_MAP_PTR_POISON) { | |
5364 | verbose(env, "tail_call obusing map_ptr\n"); | |
5365 | return -EINVAL; | |
5366 | } | |
5367 | if (!map_ptr->unpriv_array) | |
5368 | continue; | |
5369 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, | |
5370 | map_ptr->max_entries, 2); | |
5371 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
5372 | container_of(map_ptr, | |
5373 | struct bpf_array, | |
5374 | map)->index_mask); | |
5375 | insn_buf[2] = *insn; | |
5376 | cnt = 3; | |
5377 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
5378 | if (!new_prog) | |
5379 | return -ENOMEM; | |
5380 | ||
5381 | delta += cnt - 1; | |
5382 | env->prog = prog = new_prog; | |
5383 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
5384 | continue; |
5385 | } | |
e245c5c6 | 5386 | |
89c63074 DB |
5387 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
5388 | * handlers are currently limited to 64 bit only. | |
5389 | */ | |
60b58afc | 5390 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
89c63074 | 5391 | insn->imm == BPF_FUNC_map_lookup_elem) { |
81ed18ab | 5392 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
fad73a1a MKL |
5393 | if (map_ptr == BPF_MAP_PTR_POISON || |
5394 | !map_ptr->ops->map_gen_lookup) | |
81ed18ab AS |
5395 | goto patch_call_imm; |
5396 | ||
5397 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); | |
5398 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 5399 | verbose(env, "bpf verifier is misconfigured\n"); |
81ed18ab AS |
5400 | return -EINVAL; |
5401 | } | |
5402 | ||
5403 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
5404 | cnt); | |
5405 | if (!new_prog) | |
5406 | return -ENOMEM; | |
5407 | ||
5408 | delta += cnt - 1; | |
5409 | ||
5410 | /* keep walking new program and skip insns we just inserted */ | |
5411 | env->prog = prog = new_prog; | |
5412 | insn = new_prog->insnsi + i + delta; | |
5413 | continue; | |
5414 | } | |
5415 | ||
109980b8 | 5416 | if (insn->imm == BPF_FUNC_redirect_map) { |
7c300131 DB |
5417 | /* Note, we cannot use prog directly as imm as subsequent |
5418 | * rewrites would still change the prog pointer. The only | |
5419 | * stable address we can use is aux, which also works with | |
5420 | * prog clones during blinding. | |
5421 | */ | |
5422 | u64 addr = (unsigned long)prog->aux; | |
109980b8 DB |
5423 | struct bpf_insn r4_ld[] = { |
5424 | BPF_LD_IMM64(BPF_REG_4, addr), | |
5425 | *insn, | |
5426 | }; | |
5427 | cnt = ARRAY_SIZE(r4_ld); | |
5428 | ||
5429 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); | |
5430 | if (!new_prog) | |
5431 | return -ENOMEM; | |
5432 | ||
5433 | delta += cnt - 1; | |
5434 | env->prog = prog = new_prog; | |
5435 | insn = new_prog->insnsi + i + delta; | |
5436 | } | |
81ed18ab | 5437 | patch_call_imm: |
00176a34 | 5438 | fn = env->ops->get_func_proto(insn->imm); |
79741b3b AS |
5439 | /* all functions that have prototype and verifier allowed |
5440 | * programs to call them, must be real in-kernel functions | |
5441 | */ | |
5442 | if (!fn->func) { | |
61bd5218 JK |
5443 | verbose(env, |
5444 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
5445 | func_id_name(insn->imm), insn->imm); |
5446 | return -EFAULT; | |
e245c5c6 | 5447 | } |
79741b3b | 5448 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 5449 | } |
e245c5c6 | 5450 | |
79741b3b AS |
5451 | return 0; |
5452 | } | |
e245c5c6 | 5453 | |
58e2af8b | 5454 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 5455 | { |
58e2af8b | 5456 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
5457 | int i; |
5458 | ||
5459 | if (!env->explored_states) | |
5460 | return; | |
5461 | ||
5462 | for (i = 0; i < env->prog->len; i++) { | |
5463 | sl = env->explored_states[i]; | |
5464 | ||
5465 | if (sl) | |
5466 | while (sl != STATE_LIST_MARK) { | |
5467 | sln = sl->next; | |
1969db47 | 5468 | free_verifier_state(&sl->state, false); |
f1bca824 AS |
5469 | kfree(sl); |
5470 | sl = sln; | |
5471 | } | |
5472 | } | |
5473 | ||
5474 | kfree(env->explored_states); | |
5475 | } | |
5476 | ||
9bac3d6d | 5477 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
51580e79 | 5478 | { |
58e2af8b | 5479 | struct bpf_verifier_env *env; |
61bd5218 | 5480 | struct bpf_verifer_log *log; |
51580e79 AS |
5481 | int ret = -EINVAL; |
5482 | ||
eba0c929 AB |
5483 | /* no program is valid */ |
5484 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
5485 | return -EINVAL; | |
5486 | ||
58e2af8b | 5487 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
5488 | * allocate/free it every time bpf_check() is called |
5489 | */ | |
58e2af8b | 5490 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
5491 | if (!env) |
5492 | return -ENOMEM; | |
61bd5218 | 5493 | log = &env->log; |
cbd35700 | 5494 | |
3df126f3 JK |
5495 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
5496 | (*prog)->len); | |
5497 | ret = -ENOMEM; | |
5498 | if (!env->insn_aux_data) | |
5499 | goto err_free_env; | |
9bac3d6d | 5500 | env->prog = *prog; |
00176a34 | 5501 | env->ops = bpf_verifier_ops[env->prog->type]; |
0246e64d | 5502 | |
cbd35700 AS |
5503 | /* grab the mutex to protect few globals used by verifier */ |
5504 | mutex_lock(&bpf_verifier_lock); | |
5505 | ||
5506 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
5507 | /* user requested verbose verifier output | |
5508 | * and supplied buffer to store the verification trace | |
5509 | */ | |
e7bf8249 JK |
5510 | log->level = attr->log_level; |
5511 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
5512 | log->len_total = attr->log_size; | |
cbd35700 AS |
5513 | |
5514 | ret = -EINVAL; | |
e7bf8249 JK |
5515 | /* log attributes have to be sane */ |
5516 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || | |
5517 | !log->level || !log->ubuf) | |
3df126f3 | 5518 | goto err_unlock; |
cbd35700 | 5519 | } |
1ad2f583 DB |
5520 | |
5521 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); | |
5522 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 5523 | env->strict_alignment = true; |
cbd35700 | 5524 | |
cae1927c | 5525 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
ab3f0063 JK |
5526 | ret = bpf_prog_offload_verifier_prep(env); |
5527 | if (ret) | |
5528 | goto err_unlock; | |
5529 | } | |
5530 | ||
0246e64d AS |
5531 | ret = replace_map_fd_with_map_ptr(env); |
5532 | if (ret < 0) | |
5533 | goto skip_full_check; | |
5534 | ||
9bac3d6d | 5535 | env->explored_states = kcalloc(env->prog->len, |
58e2af8b | 5536 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
5537 | GFP_USER); |
5538 | ret = -ENOMEM; | |
5539 | if (!env->explored_states) | |
5540 | goto skip_full_check; | |
5541 | ||
cc8b0b92 AS |
5542 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
5543 | ||
475fb78f AS |
5544 | ret = check_cfg(env); |
5545 | if (ret < 0) | |
5546 | goto skip_full_check; | |
5547 | ||
17a52670 | 5548 | ret = do_check(env); |
8c01c4f8 CG |
5549 | if (env->cur_state) { |
5550 | free_verifier_state(env->cur_state, true); | |
5551 | env->cur_state = NULL; | |
5552 | } | |
cbd35700 | 5553 | |
0246e64d | 5554 | skip_full_check: |
638f5b90 | 5555 | while (!pop_stack(env, NULL, NULL)); |
f1bca824 | 5556 | free_states(env); |
0246e64d | 5557 | |
c131187d AS |
5558 | if (ret == 0) |
5559 | sanitize_dead_code(env); | |
5560 | ||
70a87ffe AS |
5561 | if (ret == 0) |
5562 | ret = check_max_stack_depth(env); | |
5563 | ||
9bac3d6d AS |
5564 | if (ret == 0) |
5565 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
5566 | ret = convert_ctx_accesses(env); | |
5567 | ||
e245c5c6 | 5568 | if (ret == 0) |
79741b3b | 5569 | ret = fixup_bpf_calls(env); |
e245c5c6 | 5570 | |
1ea47e01 AS |
5571 | if (ret == 0) |
5572 | ret = fixup_call_args(env); | |
5573 | ||
a2a7d570 | 5574 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 5575 | ret = -ENOSPC; |
a2a7d570 | 5576 | if (log->level && !log->ubuf) { |
cbd35700 | 5577 | ret = -EFAULT; |
a2a7d570 | 5578 | goto err_release_maps; |
cbd35700 AS |
5579 | } |
5580 | ||
0246e64d AS |
5581 | if (ret == 0 && env->used_map_cnt) { |
5582 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
5583 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
5584 | sizeof(env->used_maps[0]), | |
5585 | GFP_KERNEL); | |
0246e64d | 5586 | |
9bac3d6d | 5587 | if (!env->prog->aux->used_maps) { |
0246e64d | 5588 | ret = -ENOMEM; |
a2a7d570 | 5589 | goto err_release_maps; |
0246e64d AS |
5590 | } |
5591 | ||
9bac3d6d | 5592 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 5593 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 5594 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
5595 | |
5596 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
5597 | * bpf_ld_imm64 instructions | |
5598 | */ | |
5599 | convert_pseudo_ld_imm64(env); | |
5600 | } | |
cbd35700 | 5601 | |
a2a7d570 | 5602 | err_release_maps: |
9bac3d6d | 5603 | if (!env->prog->aux->used_maps) |
0246e64d AS |
5604 | /* if we didn't copy map pointers into bpf_prog_info, release |
5605 | * them now. Otherwise free_bpf_prog_info() will release them. | |
5606 | */ | |
5607 | release_maps(env); | |
9bac3d6d | 5608 | *prog = env->prog; |
3df126f3 | 5609 | err_unlock: |
cbd35700 | 5610 | mutex_unlock(&bpf_verifier_lock); |
3df126f3 JK |
5611 | vfree(env->insn_aux_data); |
5612 | err_free_env: | |
5613 | kfree(env); | |
51580e79 AS |
5614 | return ret; |
5615 | } |