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