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