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