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