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