Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
51580e79 | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
969bf05e | 3 | * Copyright (c) 2016 Facebook |
fd978bf7 | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
51580e79 | 5 | */ |
838e9690 | 6 | #include <uapi/linux/btf.h> |
51580e79 AS |
7 | #include <linux/kernel.h> |
8 | #include <linux/types.h> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/bpf.h> | |
838e9690 | 11 | #include <linux/btf.h> |
58e2af8b | 12 | #include <linux/bpf_verifier.h> |
51580e79 AS |
13 | #include <linux/filter.h> |
14 | #include <net/netlink.h> | |
15 | #include <linux/file.h> | |
16 | #include <linux/vmalloc.h> | |
ebb676da | 17 | #include <linux/stringify.h> |
cc8b0b92 AS |
18 | #include <linux/bsearch.h> |
19 | #include <linux/sort.h> | |
c195651e | 20 | #include <linux/perf_event.h> |
d9762e84 | 21 | #include <linux/ctype.h> |
6ba43b76 | 22 | #include <linux/error-injection.h> |
9e4e01df | 23 | #include <linux/bpf_lsm.h> |
51580e79 | 24 | |
f4ac7e0b JK |
25 | #include "disasm.h" |
26 | ||
00176a34 | 27 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 28 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
29 | [_id] = & _name ## _verifier_ops, |
30 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 31 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
32 | #include <linux/bpf_types.h> |
33 | #undef BPF_PROG_TYPE | |
34 | #undef BPF_MAP_TYPE | |
f2e10bff | 35 | #undef BPF_LINK_TYPE |
00176a34 JK |
36 | }; |
37 | ||
51580e79 AS |
38 | /* bpf_check() is a static code analyzer that walks eBPF program |
39 | * instruction by instruction and updates register/stack state. | |
40 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
41 | * | |
42 | * The first pass is depth-first-search to check that the program is a DAG. | |
43 | * It rejects the following programs: | |
44 | * - larger than BPF_MAXINSNS insns | |
45 | * - if loop is present (detected via back-edge) | |
46 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
47 | * - out of bounds or malformed jumps | |
48 | * The second pass is all possible path descent from the 1st insn. | |
49 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 50 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
51 | * insn is less then 4K, but there are too many branches that change stack/regs. |
52 | * Number of 'branches to be analyzed' is limited to 1k | |
53 | * | |
54 | * On entry to each instruction, each register has a type, and the instruction | |
55 | * changes the types of the registers depending on instruction semantics. | |
56 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
57 | * copied to R1. | |
58 | * | |
59 | * All registers are 64-bit. | |
60 | * R0 - return register | |
61 | * R1-R5 argument passing registers | |
62 | * R6-R9 callee saved registers | |
63 | * R10 - frame pointer read-only | |
64 | * | |
65 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
66 | * and has type PTR_TO_CTX. | |
67 | * | |
68 | * Verifier tracks arithmetic operations on pointers in case: | |
69 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
70 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
71 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
72 | * and 2nd arithmetic instruction is pattern matched to recognize | |
73 | * that it wants to construct a pointer to some element within stack. | |
74 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
75 | * (and -20 constant is saved for further stack bounds checking). | |
76 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
77 | * | |
f1174f77 | 78 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 79 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 80 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
81 | * |
82 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
83 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
84 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
85 | * |
86 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
87 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
88 | * | |
89 | * registers used to pass values to function calls are checked against | |
90 | * function argument constraints. | |
91 | * | |
92 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
93 | * It means that the register type passed to this function must be | |
94 | * PTR_TO_STACK and it will be used inside the function as | |
95 | * 'pointer to map element key' | |
96 | * | |
97 | * For example the argument constraints for bpf_map_lookup_elem(): | |
98 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
99 | * .arg1_type = ARG_CONST_MAP_PTR, | |
100 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
101 | * | |
102 | * ret_type says that this function returns 'pointer to map elem value or null' | |
103 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
104 | * 2nd argument should be a pointer to stack, which will be used inside | |
105 | * the helper function as a pointer to map element key. | |
106 | * | |
107 | * On the kernel side the helper function looks like: | |
108 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
109 | * { | |
110 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
111 | * void *key = (void *) (unsigned long) r2; | |
112 | * void *value; | |
113 | * | |
114 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
115 | * [key, key + map->key_size) bytes are valid and were initialized on | |
116 | * the stack of eBPF program. | |
117 | * } | |
118 | * | |
119 | * Corresponding eBPF program may look like: | |
120 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
121 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
122 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
123 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
124 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
125 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
126 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
127 | * | |
128 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
129 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
130 | * and were initialized prior to this call. | |
131 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
132 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
133 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
134 | * returns ether pointer to map value or NULL. | |
135 | * | |
136 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
137 | * insn, the register holding that pointer in the true branch changes state to | |
138 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
139 | * branch. See check_cond_jmp_op(). | |
140 | * | |
141 | * After the call R0 is set to return type of the function and registers R1-R5 | |
142 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
143 | * |
144 | * The following reference types represent a potential reference to a kernel | |
145 | * resource which, after first being allocated, must be checked and freed by | |
146 | * the BPF program: | |
147 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
148 | * | |
149 | * When the verifier sees a helper call return a reference type, it allocates a | |
150 | * pointer id for the reference and stores it in the current function state. | |
151 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
152 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
153 | * passes through a NULL-check conditional. For the branch wherein the state is | |
154 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
155 | * |
156 | * For each helper function that allocates a reference, such as | |
157 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
158 | * bpf_sk_release(). When a reference type passes into the release function, | |
159 | * the verifier also releases the reference. If any unchecked or unreleased | |
160 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
161 | */ |
162 | ||
17a52670 | 163 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 164 | struct bpf_verifier_stack_elem { |
17a52670 AS |
165 | /* verifer state is 'st' |
166 | * before processing instruction 'insn_idx' | |
167 | * and after processing instruction 'prev_insn_idx' | |
168 | */ | |
58e2af8b | 169 | struct bpf_verifier_state st; |
17a52670 AS |
170 | int insn_idx; |
171 | int prev_insn_idx; | |
58e2af8b | 172 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
173 | /* length of verifier log at the time this state was pushed on stack */ |
174 | u32 log_pos; | |
cbd35700 AS |
175 | }; |
176 | ||
b285fcb7 | 177 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 178 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 179 | |
d2e4c1e6 DB |
180 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
181 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
182 | ||
c93552c4 DB |
183 | #define BPF_MAP_PTR_UNPRIV 1UL |
184 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
185 | POISON_POINTER_DELTA)) | |
186 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
187 | ||
188 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) | |
189 | { | |
d2e4c1e6 | 190 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
191 | } |
192 | ||
193 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
194 | { | |
d2e4c1e6 | 195 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
196 | } |
197 | ||
198 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
199 | const struct bpf_map *map, bool unpriv) | |
200 | { | |
201 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
202 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
203 | aux->map_ptr_state = (unsigned long)map | |
204 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
205 | } | |
206 | ||
207 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
208 | { | |
209 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
210 | } | |
211 | ||
212 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
213 | { | |
214 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
215 | } | |
216 | ||
217 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
218 | { | |
219 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
220 | } | |
221 | ||
222 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
223 | { | |
224 | bool poisoned = bpf_map_key_poisoned(aux); | |
225 | ||
226 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
227 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 228 | } |
fad73a1a | 229 | |
33ff9823 DB |
230 | struct bpf_call_arg_meta { |
231 | struct bpf_map *map_ptr; | |
435faee1 | 232 | bool raw_mode; |
36bbef52 | 233 | bool pkt_access; |
435faee1 DB |
234 | int regno; |
235 | int access_size; | |
457f4436 | 236 | int mem_size; |
10060503 | 237 | u64 msize_max_value; |
1b986589 | 238 | int ref_obj_id; |
d83525ca | 239 | int func_id; |
a7658e1a | 240 | u32 btf_id; |
33ff9823 DB |
241 | }; |
242 | ||
8580ac94 AS |
243 | struct btf *btf_vmlinux; |
244 | ||
cbd35700 AS |
245 | static DEFINE_MUTEX(bpf_verifier_lock); |
246 | ||
d9762e84 MKL |
247 | static const struct bpf_line_info * |
248 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
249 | { | |
250 | const struct bpf_line_info *linfo; | |
251 | const struct bpf_prog *prog; | |
252 | u32 i, nr_linfo; | |
253 | ||
254 | prog = env->prog; | |
255 | nr_linfo = prog->aux->nr_linfo; | |
256 | ||
257 | if (!nr_linfo || insn_off >= prog->len) | |
258 | return NULL; | |
259 | ||
260 | linfo = prog->aux->linfo; | |
261 | for (i = 1; i < nr_linfo; i++) | |
262 | if (insn_off < linfo[i].insn_off) | |
263 | break; | |
264 | ||
265 | return &linfo[i - 1]; | |
266 | } | |
267 | ||
77d2e05a MKL |
268 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
269 | va_list args) | |
cbd35700 | 270 | { |
a2a7d570 | 271 | unsigned int n; |
cbd35700 | 272 | |
a2a7d570 | 273 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
274 | |
275 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
276 | "verifier log line truncated - local buffer too short\n"); | |
277 | ||
278 | n = min(log->len_total - log->len_used - 1, n); | |
279 | log->kbuf[n] = '\0'; | |
280 | ||
8580ac94 AS |
281 | if (log->level == BPF_LOG_KERNEL) { |
282 | pr_err("BPF:%s\n", log->kbuf); | |
283 | return; | |
284 | } | |
a2a7d570 JK |
285 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
286 | log->len_used += n; | |
287 | else | |
288 | log->ubuf = NULL; | |
cbd35700 | 289 | } |
abe08840 | 290 | |
6f8a57cc AN |
291 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
292 | { | |
293 | char zero = 0; | |
294 | ||
295 | if (!bpf_verifier_log_needed(log)) | |
296 | return; | |
297 | ||
298 | log->len_used = new_pos; | |
299 | if (put_user(zero, log->ubuf + new_pos)) | |
300 | log->ubuf = NULL; | |
301 | } | |
302 | ||
abe08840 JO |
303 | /* log_level controls verbosity level of eBPF verifier. |
304 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
305 | * so the user can figure out what's wrong with the program | |
430e68d1 | 306 | */ |
abe08840 JO |
307 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
308 | const char *fmt, ...) | |
309 | { | |
310 | va_list args; | |
311 | ||
77d2e05a MKL |
312 | if (!bpf_verifier_log_needed(&env->log)) |
313 | return; | |
314 | ||
abe08840 | 315 | va_start(args, fmt); |
77d2e05a | 316 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
317 | va_end(args); |
318 | } | |
319 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
320 | ||
321 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
322 | { | |
77d2e05a | 323 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
324 | va_list args; |
325 | ||
77d2e05a MKL |
326 | if (!bpf_verifier_log_needed(&env->log)) |
327 | return; | |
328 | ||
abe08840 | 329 | va_start(args, fmt); |
77d2e05a | 330 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
331 | va_end(args); |
332 | } | |
cbd35700 | 333 | |
9e15db66 AS |
334 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
335 | const char *fmt, ...) | |
336 | { | |
337 | va_list args; | |
338 | ||
339 | if (!bpf_verifier_log_needed(log)) | |
340 | return; | |
341 | ||
342 | va_start(args, fmt); | |
343 | bpf_verifier_vlog(log, fmt, args); | |
344 | va_end(args); | |
345 | } | |
346 | ||
d9762e84 MKL |
347 | static const char *ltrim(const char *s) |
348 | { | |
349 | while (isspace(*s)) | |
350 | s++; | |
351 | ||
352 | return s; | |
353 | } | |
354 | ||
355 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
356 | u32 insn_off, | |
357 | const char *prefix_fmt, ...) | |
358 | { | |
359 | const struct bpf_line_info *linfo; | |
360 | ||
361 | if (!bpf_verifier_log_needed(&env->log)) | |
362 | return; | |
363 | ||
364 | linfo = find_linfo(env, insn_off); | |
365 | if (!linfo || linfo == env->prev_linfo) | |
366 | return; | |
367 | ||
368 | if (prefix_fmt) { | |
369 | va_list args; | |
370 | ||
371 | va_start(args, prefix_fmt); | |
372 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
373 | va_end(args); | |
374 | } | |
375 | ||
376 | verbose(env, "%s\n", | |
377 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
378 | linfo->line_off))); | |
379 | ||
380 | env->prev_linfo = linfo; | |
381 | } | |
382 | ||
de8f3a83 DB |
383 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
384 | { | |
385 | return type == PTR_TO_PACKET || | |
386 | type == PTR_TO_PACKET_META; | |
387 | } | |
388 | ||
46f8bc92 MKL |
389 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
390 | { | |
391 | return type == PTR_TO_SOCKET || | |
655a51e5 | 392 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
393 | type == PTR_TO_TCP_SOCK || |
394 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
395 | } |
396 | ||
cac616db JF |
397 | static bool reg_type_not_null(enum bpf_reg_type type) |
398 | { | |
399 | return type == PTR_TO_SOCKET || | |
400 | type == PTR_TO_TCP_SOCK || | |
401 | type == PTR_TO_MAP_VALUE || | |
402 | type == PTR_TO_SOCK_COMMON || | |
403 | type == PTR_TO_BTF_ID; | |
404 | } | |
405 | ||
840b9615 JS |
406 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
407 | { | |
fd978bf7 | 408 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 409 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 | 410 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
b121b341 | 411 | type == PTR_TO_TCP_SOCK_OR_NULL || |
457f4436 AN |
412 | type == PTR_TO_BTF_ID_OR_NULL || |
413 | type == PTR_TO_MEM_OR_NULL; | |
fd978bf7 JS |
414 | } |
415 | ||
d83525ca AS |
416 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
417 | { | |
418 | return reg->type == PTR_TO_MAP_VALUE && | |
419 | map_value_has_spin_lock(reg->map_ptr); | |
420 | } | |
421 | ||
cba368c1 MKL |
422 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
423 | { | |
424 | return type == PTR_TO_SOCKET || | |
425 | type == PTR_TO_SOCKET_OR_NULL || | |
426 | type == PTR_TO_TCP_SOCK || | |
457f4436 AN |
427 | type == PTR_TO_TCP_SOCK_OR_NULL || |
428 | type == PTR_TO_MEM || | |
429 | type == PTR_TO_MEM_OR_NULL; | |
cba368c1 MKL |
430 | } |
431 | ||
1b986589 | 432 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 433 | { |
1b986589 | 434 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
435 | } |
436 | ||
437 | /* Determine whether the function releases some resources allocated by another | |
438 | * function call. The first reference type argument will be assumed to be | |
439 | * released by release_reference(). | |
440 | */ | |
441 | static bool is_release_function(enum bpf_func_id func_id) | |
442 | { | |
457f4436 AN |
443 | return func_id == BPF_FUNC_sk_release || |
444 | func_id == BPF_FUNC_ringbuf_submit || | |
445 | func_id == BPF_FUNC_ringbuf_discard; | |
840b9615 JS |
446 | } |
447 | ||
64d85290 | 448 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
46f8bc92 MKL |
449 | { |
450 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 | 451 | func_id == BPF_FUNC_sk_lookup_udp || |
64d85290 | 452 | func_id == BPF_FUNC_skc_lookup_tcp || |
457f4436 AN |
453 | func_id == BPF_FUNC_map_lookup_elem || |
454 | func_id == BPF_FUNC_ringbuf_reserve; | |
64d85290 JS |
455 | } |
456 | ||
457 | static bool is_acquire_function(enum bpf_func_id func_id, | |
458 | const struct bpf_map *map) | |
459 | { | |
460 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
461 | ||
462 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
463 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 AN |
464 | func_id == BPF_FUNC_skc_lookup_tcp || |
465 | func_id == BPF_FUNC_ringbuf_reserve) | |
64d85290 JS |
466 | return true; |
467 | ||
468 | if (func_id == BPF_FUNC_map_lookup_elem && | |
469 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
470 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
471 | return true; | |
472 | ||
473 | return false; | |
46f8bc92 MKL |
474 | } |
475 | ||
1b986589 MKL |
476 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
477 | { | |
478 | return func_id == BPF_FUNC_tcp_sock || | |
479 | func_id == BPF_FUNC_sk_fullsock; | |
480 | } | |
481 | ||
17a52670 AS |
482 | /* string representation of 'enum bpf_reg_type' */ |
483 | static const char * const reg_type_str[] = { | |
484 | [NOT_INIT] = "?", | |
f1174f77 | 485 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
486 | [PTR_TO_CTX] = "ctx", |
487 | [CONST_PTR_TO_MAP] = "map_ptr", | |
488 | [PTR_TO_MAP_VALUE] = "map_value", | |
489 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 490 | [PTR_TO_STACK] = "fp", |
969bf05e | 491 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 492 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 493 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 494 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
495 | [PTR_TO_SOCKET] = "sock", |
496 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
497 | [PTR_TO_SOCK_COMMON] = "sock_common", |
498 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
499 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
500 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 501 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 502 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
9e15db66 | 503 | [PTR_TO_BTF_ID] = "ptr_", |
b121b341 | 504 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
457f4436 AN |
505 | [PTR_TO_MEM] = "mem", |
506 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", | |
17a52670 AS |
507 | }; |
508 | ||
8efea21d EC |
509 | static char slot_type_char[] = { |
510 | [STACK_INVALID] = '?', | |
511 | [STACK_SPILL] = 'r', | |
512 | [STACK_MISC] = 'm', | |
513 | [STACK_ZERO] = '0', | |
514 | }; | |
515 | ||
4e92024a AS |
516 | static void print_liveness(struct bpf_verifier_env *env, |
517 | enum bpf_reg_liveness live) | |
518 | { | |
9242b5f5 | 519 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
520 | verbose(env, "_"); |
521 | if (live & REG_LIVE_READ) | |
522 | verbose(env, "r"); | |
523 | if (live & REG_LIVE_WRITTEN) | |
524 | verbose(env, "w"); | |
9242b5f5 AS |
525 | if (live & REG_LIVE_DONE) |
526 | verbose(env, "D"); | |
4e92024a AS |
527 | } |
528 | ||
f4d7e40a AS |
529 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
530 | const struct bpf_reg_state *reg) | |
531 | { | |
532 | struct bpf_verifier_state *cur = env->cur_state; | |
533 | ||
534 | return cur->frame[reg->frameno]; | |
535 | } | |
536 | ||
9e15db66 AS |
537 | const char *kernel_type_name(u32 id) |
538 | { | |
539 | return btf_name_by_offset(btf_vmlinux, | |
540 | btf_type_by_id(btf_vmlinux, id)->name_off); | |
541 | } | |
542 | ||
61bd5218 | 543 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 544 | const struct bpf_func_state *state) |
17a52670 | 545 | { |
f4d7e40a | 546 | const struct bpf_reg_state *reg; |
17a52670 AS |
547 | enum bpf_reg_type t; |
548 | int i; | |
549 | ||
f4d7e40a AS |
550 | if (state->frameno) |
551 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 552 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
553 | reg = &state->regs[i]; |
554 | t = reg->type; | |
17a52670 AS |
555 | if (t == NOT_INIT) |
556 | continue; | |
4e92024a AS |
557 | verbose(env, " R%d", i); |
558 | print_liveness(env, reg->live); | |
559 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
560 | if (t == SCALAR_VALUE && reg->precise) |
561 | verbose(env, "P"); | |
f1174f77 EC |
562 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
563 | tnum_is_const(reg->var_off)) { | |
564 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 565 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 566 | } else { |
b121b341 | 567 | if (t == PTR_TO_BTF_ID || t == PTR_TO_BTF_ID_OR_NULL) |
9e15db66 | 568 | verbose(env, "%s", kernel_type_name(reg->btf_id)); |
cba368c1 MKL |
569 | verbose(env, "(id=%d", reg->id); |
570 | if (reg_type_may_be_refcounted_or_null(t)) | |
571 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 572 | if (t != SCALAR_VALUE) |
61bd5218 | 573 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 574 | if (type_is_pkt_pointer(t)) |
61bd5218 | 575 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
576 | else if (t == CONST_PTR_TO_MAP || |
577 | t == PTR_TO_MAP_VALUE || | |
578 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 579 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
580 | reg->map_ptr->key_size, |
581 | reg->map_ptr->value_size); | |
7d1238f2 EC |
582 | if (tnum_is_const(reg->var_off)) { |
583 | /* Typically an immediate SCALAR_VALUE, but | |
584 | * could be a pointer whose offset is too big | |
585 | * for reg->off | |
586 | */ | |
61bd5218 | 587 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
588 | } else { |
589 | if (reg->smin_value != reg->umin_value && | |
590 | reg->smin_value != S64_MIN) | |
61bd5218 | 591 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
592 | (long long)reg->smin_value); |
593 | if (reg->smax_value != reg->umax_value && | |
594 | reg->smax_value != S64_MAX) | |
61bd5218 | 595 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
596 | (long long)reg->smax_value); |
597 | if (reg->umin_value != 0) | |
61bd5218 | 598 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
599 | (unsigned long long)reg->umin_value); |
600 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 601 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
602 | (unsigned long long)reg->umax_value); |
603 | if (!tnum_is_unknown(reg->var_off)) { | |
604 | char tn_buf[48]; | |
f1174f77 | 605 | |
7d1238f2 | 606 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 607 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 608 | } |
3f50f132 JF |
609 | if (reg->s32_min_value != reg->smin_value && |
610 | reg->s32_min_value != S32_MIN) | |
611 | verbose(env, ",s32_min_value=%d", | |
612 | (int)(reg->s32_min_value)); | |
613 | if (reg->s32_max_value != reg->smax_value && | |
614 | reg->s32_max_value != S32_MAX) | |
615 | verbose(env, ",s32_max_value=%d", | |
616 | (int)(reg->s32_max_value)); | |
617 | if (reg->u32_min_value != reg->umin_value && | |
618 | reg->u32_min_value != U32_MIN) | |
619 | verbose(env, ",u32_min_value=%d", | |
620 | (int)(reg->u32_min_value)); | |
621 | if (reg->u32_max_value != reg->umax_value && | |
622 | reg->u32_max_value != U32_MAX) | |
623 | verbose(env, ",u32_max_value=%d", | |
624 | (int)(reg->u32_max_value)); | |
f1174f77 | 625 | } |
61bd5218 | 626 | verbose(env, ")"); |
f1174f77 | 627 | } |
17a52670 | 628 | } |
638f5b90 | 629 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
630 | char types_buf[BPF_REG_SIZE + 1]; |
631 | bool valid = false; | |
632 | int j; | |
633 | ||
634 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
635 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
636 | valid = true; | |
637 | types_buf[j] = slot_type_char[ | |
638 | state->stack[i].slot_type[j]]; | |
639 | } | |
640 | types_buf[BPF_REG_SIZE] = 0; | |
641 | if (!valid) | |
642 | continue; | |
643 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
644 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
b5dc0163 AS |
645 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
646 | reg = &state->stack[i].spilled_ptr; | |
647 | t = reg->type; | |
648 | verbose(env, "=%s", reg_type_str[t]); | |
649 | if (t == SCALAR_VALUE && reg->precise) | |
650 | verbose(env, "P"); | |
651 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
652 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
653 | } else { | |
8efea21d | 654 | verbose(env, "=%s", types_buf); |
b5dc0163 | 655 | } |
17a52670 | 656 | } |
fd978bf7 JS |
657 | if (state->acquired_refs && state->refs[0].id) { |
658 | verbose(env, " refs=%d", state->refs[0].id); | |
659 | for (i = 1; i < state->acquired_refs; i++) | |
660 | if (state->refs[i].id) | |
661 | verbose(env, ",%d", state->refs[i].id); | |
662 | } | |
61bd5218 | 663 | verbose(env, "\n"); |
17a52670 AS |
664 | } |
665 | ||
84dbf350 JS |
666 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
667 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
668 | const struct bpf_func_state *src) \ | |
669 | { \ | |
670 | if (!src->FIELD) \ | |
671 | return 0; \ | |
672 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
673 | /* internal bug, make state invalid to reject the program */ \ | |
674 | memset(dst, 0, sizeof(*dst)); \ | |
675 | return -EFAULT; \ | |
676 | } \ | |
677 | memcpy(dst->FIELD, src->FIELD, \ | |
678 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
679 | return 0; \ | |
638f5b90 | 680 | } |
fd978bf7 JS |
681 | /* copy_reference_state() */ |
682 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
683 | /* copy_stack_state() */ |
684 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
685 | #undef COPY_STATE_FN | |
686 | ||
687 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
688 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
689 | bool copy_old) \ | |
690 | { \ | |
691 | u32 old_size = state->COUNT; \ | |
692 | struct bpf_##NAME##_state *new_##FIELD; \ | |
693 | int slot = size / SIZE; \ | |
694 | \ | |
695 | if (size <= old_size || !size) { \ | |
696 | if (copy_old) \ | |
697 | return 0; \ | |
698 | state->COUNT = slot * SIZE; \ | |
699 | if (!size && old_size) { \ | |
700 | kfree(state->FIELD); \ | |
701 | state->FIELD = NULL; \ | |
702 | } \ | |
703 | return 0; \ | |
704 | } \ | |
705 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
706 | GFP_KERNEL); \ | |
707 | if (!new_##FIELD) \ | |
708 | return -ENOMEM; \ | |
709 | if (copy_old) { \ | |
710 | if (state->FIELD) \ | |
711 | memcpy(new_##FIELD, state->FIELD, \ | |
712 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
713 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
714 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
715 | } \ | |
716 | state->COUNT = slot * SIZE; \ | |
717 | kfree(state->FIELD); \ | |
718 | state->FIELD = new_##FIELD; \ | |
719 | return 0; \ | |
720 | } | |
fd978bf7 JS |
721 | /* realloc_reference_state() */ |
722 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
723 | /* realloc_stack_state() */ |
724 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
725 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
726 | |
727 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
728 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 729 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
730 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
731 | * which realloc_stack_state() copies over. It points to previous | |
732 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 733 | */ |
fd978bf7 JS |
734 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
735 | int refs_size, bool copy_old) | |
638f5b90 | 736 | { |
fd978bf7 JS |
737 | int err = realloc_reference_state(state, refs_size, copy_old); |
738 | if (err) | |
739 | return err; | |
740 | return realloc_stack_state(state, stack_size, copy_old); | |
741 | } | |
742 | ||
743 | /* Acquire a pointer id from the env and update the state->refs to include | |
744 | * this new pointer reference. | |
745 | * On success, returns a valid pointer id to associate with the register | |
746 | * On failure, returns a negative errno. | |
638f5b90 | 747 | */ |
fd978bf7 | 748 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 749 | { |
fd978bf7 JS |
750 | struct bpf_func_state *state = cur_func(env); |
751 | int new_ofs = state->acquired_refs; | |
752 | int id, err; | |
753 | ||
754 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
755 | if (err) | |
756 | return err; | |
757 | id = ++env->id_gen; | |
758 | state->refs[new_ofs].id = id; | |
759 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 760 | |
fd978bf7 JS |
761 | return id; |
762 | } | |
763 | ||
764 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 765 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
766 | { |
767 | int i, last_idx; | |
768 | ||
fd978bf7 JS |
769 | last_idx = state->acquired_refs - 1; |
770 | for (i = 0; i < state->acquired_refs; i++) { | |
771 | if (state->refs[i].id == ptr_id) { | |
772 | if (last_idx && i != last_idx) | |
773 | memcpy(&state->refs[i], &state->refs[last_idx], | |
774 | sizeof(*state->refs)); | |
775 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
776 | state->acquired_refs--; | |
638f5b90 | 777 | return 0; |
638f5b90 | 778 | } |
638f5b90 | 779 | } |
46f8bc92 | 780 | return -EINVAL; |
fd978bf7 JS |
781 | } |
782 | ||
783 | static int transfer_reference_state(struct bpf_func_state *dst, | |
784 | struct bpf_func_state *src) | |
785 | { | |
786 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
787 | if (err) | |
788 | return err; | |
789 | err = copy_reference_state(dst, src); | |
790 | if (err) | |
791 | return err; | |
638f5b90 AS |
792 | return 0; |
793 | } | |
794 | ||
f4d7e40a AS |
795 | static void free_func_state(struct bpf_func_state *state) |
796 | { | |
5896351e AS |
797 | if (!state) |
798 | return; | |
fd978bf7 | 799 | kfree(state->refs); |
f4d7e40a AS |
800 | kfree(state->stack); |
801 | kfree(state); | |
802 | } | |
803 | ||
b5dc0163 AS |
804 | static void clear_jmp_history(struct bpf_verifier_state *state) |
805 | { | |
806 | kfree(state->jmp_history); | |
807 | state->jmp_history = NULL; | |
808 | state->jmp_history_cnt = 0; | |
809 | } | |
810 | ||
1969db47 AS |
811 | static void free_verifier_state(struct bpf_verifier_state *state, |
812 | bool free_self) | |
638f5b90 | 813 | { |
f4d7e40a AS |
814 | int i; |
815 | ||
816 | for (i = 0; i <= state->curframe; i++) { | |
817 | free_func_state(state->frame[i]); | |
818 | state->frame[i] = NULL; | |
819 | } | |
b5dc0163 | 820 | clear_jmp_history(state); |
1969db47 AS |
821 | if (free_self) |
822 | kfree(state); | |
638f5b90 AS |
823 | } |
824 | ||
825 | /* copy verifier state from src to dst growing dst stack space | |
826 | * when necessary to accommodate larger src stack | |
827 | */ | |
f4d7e40a AS |
828 | static int copy_func_state(struct bpf_func_state *dst, |
829 | const struct bpf_func_state *src) | |
638f5b90 AS |
830 | { |
831 | int err; | |
832 | ||
fd978bf7 JS |
833 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
834 | false); | |
835 | if (err) | |
836 | return err; | |
837 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
838 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
839 | if (err) |
840 | return err; | |
638f5b90 AS |
841 | return copy_stack_state(dst, src); |
842 | } | |
843 | ||
f4d7e40a AS |
844 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
845 | const struct bpf_verifier_state *src) | |
846 | { | |
847 | struct bpf_func_state *dst; | |
b5dc0163 | 848 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
f4d7e40a AS |
849 | int i, err; |
850 | ||
b5dc0163 AS |
851 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
852 | kfree(dst_state->jmp_history); | |
853 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); | |
854 | if (!dst_state->jmp_history) | |
855 | return -ENOMEM; | |
856 | } | |
857 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); | |
858 | dst_state->jmp_history_cnt = src->jmp_history_cnt; | |
859 | ||
f4d7e40a AS |
860 | /* if dst has more stack frames then src frame, free them */ |
861 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
862 | free_func_state(dst_state->frame[i]); | |
863 | dst_state->frame[i] = NULL; | |
864 | } | |
979d63d5 | 865 | dst_state->speculative = src->speculative; |
f4d7e40a | 866 | dst_state->curframe = src->curframe; |
d83525ca | 867 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
868 | dst_state->branches = src->branches; |
869 | dst_state->parent = src->parent; | |
b5dc0163 AS |
870 | dst_state->first_insn_idx = src->first_insn_idx; |
871 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
872 | for (i = 0; i <= src->curframe; i++) { |
873 | dst = dst_state->frame[i]; | |
874 | if (!dst) { | |
875 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
876 | if (!dst) | |
877 | return -ENOMEM; | |
878 | dst_state->frame[i] = dst; | |
879 | } | |
880 | err = copy_func_state(dst, src->frame[i]); | |
881 | if (err) | |
882 | return err; | |
883 | } | |
884 | return 0; | |
885 | } | |
886 | ||
2589726d AS |
887 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
888 | { | |
889 | while (st) { | |
890 | u32 br = --st->branches; | |
891 | ||
892 | /* WARN_ON(br > 1) technically makes sense here, | |
893 | * but see comment in push_stack(), hence: | |
894 | */ | |
895 | WARN_ONCE((int)br < 0, | |
896 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
897 | br); | |
898 | if (br) | |
899 | break; | |
900 | st = st->parent; | |
901 | } | |
902 | } | |
903 | ||
638f5b90 | 904 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 905 | int *insn_idx, bool pop_log) |
638f5b90 AS |
906 | { |
907 | struct bpf_verifier_state *cur = env->cur_state; | |
908 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
909 | int err; | |
17a52670 AS |
910 | |
911 | if (env->head == NULL) | |
638f5b90 | 912 | return -ENOENT; |
17a52670 | 913 | |
638f5b90 AS |
914 | if (cur) { |
915 | err = copy_verifier_state(cur, &head->st); | |
916 | if (err) | |
917 | return err; | |
918 | } | |
6f8a57cc AN |
919 | if (pop_log) |
920 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
921 | if (insn_idx) |
922 | *insn_idx = head->insn_idx; | |
17a52670 | 923 | if (prev_insn_idx) |
638f5b90 AS |
924 | *prev_insn_idx = head->prev_insn_idx; |
925 | elem = head->next; | |
1969db47 | 926 | free_verifier_state(&head->st, false); |
638f5b90 | 927 | kfree(head); |
17a52670 AS |
928 | env->head = elem; |
929 | env->stack_size--; | |
638f5b90 | 930 | return 0; |
17a52670 AS |
931 | } |
932 | ||
58e2af8b | 933 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
934 | int insn_idx, int prev_insn_idx, |
935 | bool speculative) | |
17a52670 | 936 | { |
638f5b90 | 937 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 938 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 939 | int err; |
17a52670 | 940 | |
638f5b90 | 941 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
942 | if (!elem) |
943 | goto err; | |
944 | ||
17a52670 AS |
945 | elem->insn_idx = insn_idx; |
946 | elem->prev_insn_idx = prev_insn_idx; | |
947 | elem->next = env->head; | |
6f8a57cc | 948 | elem->log_pos = env->log.len_used; |
17a52670 AS |
949 | env->head = elem; |
950 | env->stack_size++; | |
1969db47 AS |
951 | err = copy_verifier_state(&elem->st, cur); |
952 | if (err) | |
953 | goto err; | |
979d63d5 | 954 | elem->st.speculative |= speculative; |
b285fcb7 AS |
955 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
956 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
957 | env->stack_size); | |
17a52670 AS |
958 | goto err; |
959 | } | |
2589726d AS |
960 | if (elem->st.parent) { |
961 | ++elem->st.parent->branches; | |
962 | /* WARN_ON(branches > 2) technically makes sense here, | |
963 | * but | |
964 | * 1. speculative states will bump 'branches' for non-branch | |
965 | * instructions | |
966 | * 2. is_state_visited() heuristics may decide not to create | |
967 | * a new state for a sequence of branches and all such current | |
968 | * and cloned states will be pointing to a single parent state | |
969 | * which might have large 'branches' count. | |
970 | */ | |
971 | } | |
17a52670 AS |
972 | return &elem->st; |
973 | err: | |
5896351e AS |
974 | free_verifier_state(env->cur_state, true); |
975 | env->cur_state = NULL; | |
17a52670 | 976 | /* pop all elements and return */ |
6f8a57cc | 977 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
978 | return NULL; |
979 | } | |
980 | ||
981 | #define CALLER_SAVED_REGS 6 | |
982 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
983 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
984 | }; | |
985 | ||
f54c7898 DB |
986 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
987 | struct bpf_reg_state *reg); | |
f1174f77 | 988 | |
b03c9f9f EC |
989 | /* Mark the unknown part of a register (variable offset or scalar value) as |
990 | * known to have the value @imm. | |
991 | */ | |
992 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
993 | { | |
a9c676bc AS |
994 | /* Clear id, off, and union(map_ptr, range) */ |
995 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
996 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
997 | reg->var_off = tnum_const(imm); |
998 | reg->smin_value = (s64)imm; | |
999 | reg->smax_value = (s64)imm; | |
1000 | reg->umin_value = imm; | |
1001 | reg->umax_value = imm; | |
3f50f132 JF |
1002 | |
1003 | reg->s32_min_value = (s32)imm; | |
1004 | reg->s32_max_value = (s32)imm; | |
1005 | reg->u32_min_value = (u32)imm; | |
1006 | reg->u32_max_value = (u32)imm; | |
1007 | } | |
1008 | ||
1009 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) | |
1010 | { | |
1011 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1012 | reg->s32_min_value = (s32)imm; | |
1013 | reg->s32_max_value = (s32)imm; | |
1014 | reg->u32_min_value = (u32)imm; | |
1015 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1016 | } |
1017 | ||
f1174f77 EC |
1018 | /* Mark the 'variable offset' part of a register as zero. This should be |
1019 | * used only on registers holding a pointer type. | |
1020 | */ | |
1021 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1022 | { |
b03c9f9f | 1023 | __mark_reg_known(reg, 0); |
f1174f77 | 1024 | } |
a9789ef9 | 1025 | |
cc2b14d5 AS |
1026 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1027 | { | |
1028 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1029 | reg->type = SCALAR_VALUE; |
1030 | } | |
1031 | ||
61bd5218 JK |
1032 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1033 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1034 | { |
1035 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1036 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1037 | /* Something bad happened, let's kill all regs */ |
1038 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1039 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1040 | return; |
1041 | } | |
1042 | __mark_reg_known_zero(regs + regno); | |
1043 | } | |
1044 | ||
de8f3a83 DB |
1045 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1046 | { | |
1047 | return type_is_pkt_pointer(reg->type); | |
1048 | } | |
1049 | ||
1050 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1051 | { | |
1052 | return reg_is_pkt_pointer(reg) || | |
1053 | reg->type == PTR_TO_PACKET_END; | |
1054 | } | |
1055 | ||
1056 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1057 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1058 | enum bpf_reg_type which) | |
1059 | { | |
1060 | /* The register can already have a range from prior markings. | |
1061 | * This is fine as long as it hasn't been advanced from its | |
1062 | * origin. | |
1063 | */ | |
1064 | return reg->type == which && | |
1065 | reg->id == 0 && | |
1066 | reg->off == 0 && | |
1067 | tnum_equals_const(reg->var_off, 0); | |
1068 | } | |
1069 | ||
3f50f132 JF |
1070 | /* Reset the min/max bounds of a register */ |
1071 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1072 | { | |
1073 | reg->smin_value = S64_MIN; | |
1074 | reg->smax_value = S64_MAX; | |
1075 | reg->umin_value = 0; | |
1076 | reg->umax_value = U64_MAX; | |
1077 | ||
1078 | reg->s32_min_value = S32_MIN; | |
1079 | reg->s32_max_value = S32_MAX; | |
1080 | reg->u32_min_value = 0; | |
1081 | reg->u32_max_value = U32_MAX; | |
1082 | } | |
1083 | ||
1084 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1085 | { | |
1086 | reg->smin_value = S64_MIN; | |
1087 | reg->smax_value = S64_MAX; | |
1088 | reg->umin_value = 0; | |
1089 | reg->umax_value = U64_MAX; | |
1090 | } | |
1091 | ||
1092 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1093 | { | |
1094 | reg->s32_min_value = S32_MIN; | |
1095 | reg->s32_max_value = S32_MAX; | |
1096 | reg->u32_min_value = 0; | |
1097 | reg->u32_max_value = U32_MAX; | |
1098 | } | |
1099 | ||
1100 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1101 | { | |
1102 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1103 | ||
1104 | /* min signed is max(sign bit) | min(other bits) */ | |
1105 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1106 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1107 | /* max signed is min(sign bit) | max(other bits) */ | |
1108 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1109 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1110 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1111 | reg->u32_max_value = min(reg->u32_max_value, | |
1112 | (u32)(var32_off.value | var32_off.mask)); | |
1113 | } | |
1114 | ||
1115 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1116 | { |
1117 | /* min signed is max(sign bit) | min(other bits) */ | |
1118 | reg->smin_value = max_t(s64, reg->smin_value, | |
1119 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1120 | /* max signed is min(sign bit) | max(other bits) */ | |
1121 | reg->smax_value = min_t(s64, reg->smax_value, | |
1122 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1123 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1124 | reg->umax_value = min(reg->umax_value, | |
1125 | reg->var_off.value | reg->var_off.mask); | |
1126 | } | |
1127 | ||
3f50f132 JF |
1128 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1129 | { | |
1130 | __update_reg32_bounds(reg); | |
1131 | __update_reg64_bounds(reg); | |
1132 | } | |
1133 | ||
b03c9f9f | 1134 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1135 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1136 | { | |
1137 | /* Learn sign from signed bounds. | |
1138 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1139 | * are the same, so combine. This works even in the negative case, e.g. | |
1140 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1141 | */ | |
1142 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1143 | reg->s32_min_value = reg->u32_min_value = | |
1144 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1145 | reg->s32_max_value = reg->u32_max_value = | |
1146 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1147 | return; | |
1148 | } | |
1149 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1150 | * boundary, so we must be careful. | |
1151 | */ | |
1152 | if ((s32)reg->u32_max_value >= 0) { | |
1153 | /* Positive. We can't learn anything from the smin, but smax | |
1154 | * is positive, hence safe. | |
1155 | */ | |
1156 | reg->s32_min_value = reg->u32_min_value; | |
1157 | reg->s32_max_value = reg->u32_max_value = | |
1158 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1159 | } else if ((s32)reg->u32_min_value < 0) { | |
1160 | /* Negative. We can't learn anything from the smax, but smin | |
1161 | * is negative, hence safe. | |
1162 | */ | |
1163 | reg->s32_min_value = reg->u32_min_value = | |
1164 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1165 | reg->s32_max_value = reg->u32_max_value; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1170 | { |
1171 | /* Learn sign from signed bounds. | |
1172 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1173 | * are the same, so combine. This works even in the negative case, e.g. | |
1174 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1175 | */ | |
1176 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1177 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1178 | reg->umin_value); | |
1179 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1180 | reg->umax_value); | |
1181 | return; | |
1182 | } | |
1183 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1184 | * boundary, so we must be careful. | |
1185 | */ | |
1186 | if ((s64)reg->umax_value >= 0) { | |
1187 | /* Positive. We can't learn anything from the smin, but smax | |
1188 | * is positive, hence safe. | |
1189 | */ | |
1190 | reg->smin_value = reg->umin_value; | |
1191 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1192 | reg->umax_value); | |
1193 | } else if ((s64)reg->umin_value < 0) { | |
1194 | /* Negative. We can't learn anything from the smax, but smin | |
1195 | * is negative, hence safe. | |
1196 | */ | |
1197 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1198 | reg->umin_value); | |
1199 | reg->smax_value = reg->umax_value; | |
1200 | } | |
1201 | } | |
1202 | ||
3f50f132 JF |
1203 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1204 | { | |
1205 | __reg32_deduce_bounds(reg); | |
1206 | __reg64_deduce_bounds(reg); | |
1207 | } | |
1208 | ||
b03c9f9f EC |
1209 | /* Attempts to improve var_off based on unsigned min/max information */ |
1210 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1211 | { | |
3f50f132 JF |
1212 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1213 | tnum_range(reg->umin_value, | |
1214 | reg->umax_value)); | |
1215 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1216 | tnum_range(reg->u32_min_value, | |
1217 | reg->u32_max_value)); | |
1218 | ||
1219 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1220 | } |
1221 | ||
3f50f132 | 1222 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1223 | { |
3f50f132 JF |
1224 | reg->umin_value = reg->u32_min_value; |
1225 | reg->umax_value = reg->u32_max_value; | |
1226 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds | |
1227 | * but must be positive otherwise set to worse case bounds | |
1228 | * and refine later from tnum. | |
1229 | */ | |
3a71dc36 | 1230 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
3f50f132 JF |
1231 | reg->smax_value = reg->s32_max_value; |
1232 | else | |
1233 | reg->smax_value = U32_MAX; | |
3a71dc36 JF |
1234 | if (reg->s32_min_value >= 0) |
1235 | reg->smin_value = reg->s32_min_value; | |
1236 | else | |
1237 | reg->smin_value = 0; | |
3f50f132 JF |
1238 | } |
1239 | ||
1240 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1241 | { | |
1242 | /* special case when 64-bit register has upper 32-bit register | |
1243 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1244 | * allowing us to use 32-bit bounds directly, | |
1245 | */ | |
1246 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1247 | __reg_assign_32_into_64(reg); | |
1248 | } else { | |
1249 | /* Otherwise the best we can do is push lower 32bit known and | |
1250 | * unknown bits into register (var_off set from jmp logic) | |
1251 | * then learn as much as possible from the 64-bit tnum | |
1252 | * known and unknown bits. The previous smin/smax bounds are | |
1253 | * invalid here because of jmp32 compare so mark them unknown | |
1254 | * so they do not impact tnum bounds calculation. | |
1255 | */ | |
1256 | __mark_reg64_unbounded(reg); | |
1257 | __update_reg_bounds(reg); | |
1258 | } | |
1259 | ||
1260 | /* Intersecting with the old var_off might have improved our bounds | |
1261 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1262 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1263 | */ | |
1264 | __reg_deduce_bounds(reg); | |
1265 | __reg_bound_offset(reg); | |
1266 | __update_reg_bounds(reg); | |
1267 | } | |
1268 | ||
1269 | static bool __reg64_bound_s32(s64 a) | |
1270 | { | |
1271 | if (a > S32_MIN && a < S32_MAX) | |
1272 | return true; | |
1273 | return false; | |
1274 | } | |
1275 | ||
1276 | static bool __reg64_bound_u32(u64 a) | |
1277 | { | |
1278 | if (a > U32_MIN && a < U32_MAX) | |
1279 | return true; | |
1280 | return false; | |
1281 | } | |
1282 | ||
1283 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1284 | { | |
1285 | __mark_reg32_unbounded(reg); | |
1286 | ||
1287 | if (__reg64_bound_s32(reg->smin_value)) | |
1288 | reg->s32_min_value = (s32)reg->smin_value; | |
1289 | if (__reg64_bound_s32(reg->smax_value)) | |
1290 | reg->s32_max_value = (s32)reg->smax_value; | |
1291 | if (__reg64_bound_u32(reg->umin_value)) | |
1292 | reg->u32_min_value = (u32)reg->umin_value; | |
1293 | if (__reg64_bound_u32(reg->umax_value)) | |
1294 | reg->u32_max_value = (u32)reg->umax_value; | |
1295 | ||
1296 | /* Intersecting with the old var_off might have improved our bounds | |
1297 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1298 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1299 | */ | |
1300 | __reg_deduce_bounds(reg); | |
1301 | __reg_bound_offset(reg); | |
1302 | __update_reg_bounds(reg); | |
b03c9f9f EC |
1303 | } |
1304 | ||
f1174f77 | 1305 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1306 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1307 | struct bpf_reg_state *reg) | |
f1174f77 | 1308 | { |
a9c676bc AS |
1309 | /* |
1310 | * Clear type, id, off, and union(map_ptr, range) and | |
1311 | * padding between 'type' and union | |
1312 | */ | |
1313 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1314 | reg->type = SCALAR_VALUE; |
f1174f77 | 1315 | reg->var_off = tnum_unknown; |
f4d7e40a | 1316 | reg->frameno = 0; |
2c78ee89 | 1317 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1318 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1319 | } |
1320 | ||
61bd5218 JK |
1321 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1322 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1323 | { |
1324 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1325 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1326 | /* Something bad happened, let's kill all regs except FP */ |
1327 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1328 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1329 | return; |
1330 | } | |
f54c7898 | 1331 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1332 | } |
1333 | ||
f54c7898 DB |
1334 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1335 | struct bpf_reg_state *reg) | |
f1174f77 | 1336 | { |
f54c7898 | 1337 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1338 | reg->type = NOT_INIT; |
1339 | } | |
1340 | ||
61bd5218 JK |
1341 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1342 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1343 | { |
1344 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1345 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1346 | /* Something bad happened, let's kill all regs except FP */ |
1347 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1348 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1349 | return; |
1350 | } | |
f54c7898 | 1351 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1352 | } |
1353 | ||
5327ed3d | 1354 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1355 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1356 | struct bpf_func_state *state) |
17a52670 | 1357 | { |
f4d7e40a | 1358 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1359 | int i; |
1360 | ||
dc503a8a | 1361 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1362 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1363 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1364 | regs[i].parent = NULL; |
5327ed3d | 1365 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1366 | } |
17a52670 AS |
1367 | |
1368 | /* frame pointer */ | |
f1174f77 | 1369 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1370 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1371 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1372 | } |
1373 | ||
f4d7e40a AS |
1374 | #define BPF_MAIN_FUNC (-1) |
1375 | static void init_func_state(struct bpf_verifier_env *env, | |
1376 | struct bpf_func_state *state, | |
1377 | int callsite, int frameno, int subprogno) | |
1378 | { | |
1379 | state->callsite = callsite; | |
1380 | state->frameno = frameno; | |
1381 | state->subprogno = subprogno; | |
1382 | init_reg_state(env, state); | |
1383 | } | |
1384 | ||
17a52670 AS |
1385 | enum reg_arg_type { |
1386 | SRC_OP, /* register is used as source operand */ | |
1387 | DST_OP, /* register is used as destination operand */ | |
1388 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1389 | }; | |
1390 | ||
cc8b0b92 AS |
1391 | static int cmp_subprogs(const void *a, const void *b) |
1392 | { | |
9c8105bd JW |
1393 | return ((struct bpf_subprog_info *)a)->start - |
1394 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1395 | } |
1396 | ||
1397 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1398 | { | |
9c8105bd | 1399 | struct bpf_subprog_info *p; |
cc8b0b92 | 1400 | |
9c8105bd JW |
1401 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1402 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1403 | if (!p) |
1404 | return -ENOENT; | |
9c8105bd | 1405 | return p - env->subprog_info; |
cc8b0b92 AS |
1406 | |
1407 | } | |
1408 | ||
1409 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1410 | { | |
1411 | int insn_cnt = env->prog->len; | |
1412 | int ret; | |
1413 | ||
1414 | if (off >= insn_cnt || off < 0) { | |
1415 | verbose(env, "call to invalid destination\n"); | |
1416 | return -EINVAL; | |
1417 | } | |
1418 | ret = find_subprog(env, off); | |
1419 | if (ret >= 0) | |
1420 | return 0; | |
4cb3d99c | 1421 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1422 | verbose(env, "too many subprograms\n"); |
1423 | return -E2BIG; | |
1424 | } | |
9c8105bd JW |
1425 | env->subprog_info[env->subprog_cnt++].start = off; |
1426 | sort(env->subprog_info, env->subprog_cnt, | |
1427 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1428 | return 0; |
1429 | } | |
1430 | ||
1431 | static int check_subprogs(struct bpf_verifier_env *env) | |
1432 | { | |
1433 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1434 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1435 | struct bpf_insn *insn = env->prog->insnsi; |
1436 | int insn_cnt = env->prog->len; | |
1437 | ||
f910cefa JW |
1438 | /* Add entry function. */ |
1439 | ret = add_subprog(env, 0); | |
1440 | if (ret < 0) | |
1441 | return ret; | |
1442 | ||
cc8b0b92 AS |
1443 | /* determine subprog starts. The end is one before the next starts */ |
1444 | for (i = 0; i < insn_cnt; i++) { | |
1445 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1446 | continue; | |
1447 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1448 | continue; | |
2c78ee89 AS |
1449 | if (!env->bpf_capable) { |
1450 | verbose(env, | |
1451 | "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); | |
cc8b0b92 AS |
1452 | return -EPERM; |
1453 | } | |
cc8b0b92 AS |
1454 | ret = add_subprog(env, i + insn[i].imm + 1); |
1455 | if (ret < 0) | |
1456 | return ret; | |
1457 | } | |
1458 | ||
4cb3d99c JW |
1459 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1460 | * logic. 'subprog_cnt' should not be increased. | |
1461 | */ | |
1462 | subprog[env->subprog_cnt].start = insn_cnt; | |
1463 | ||
06ee7115 | 1464 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1465 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1466 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1467 | |
1468 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1469 | subprog_start = subprog[cur_subprog].start; |
1470 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1471 | for (i = 0; i < insn_cnt; i++) { |
1472 | u8 code = insn[i].code; | |
1473 | ||
092ed096 | 1474 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1475 | goto next; |
1476 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1477 | goto next; | |
1478 | off = i + insn[i].off + 1; | |
1479 | if (off < subprog_start || off >= subprog_end) { | |
1480 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1481 | return -EINVAL; | |
1482 | } | |
1483 | next: | |
1484 | if (i == subprog_end - 1) { | |
1485 | /* to avoid fall-through from one subprog into another | |
1486 | * the last insn of the subprog should be either exit | |
1487 | * or unconditional jump back | |
1488 | */ | |
1489 | if (code != (BPF_JMP | BPF_EXIT) && | |
1490 | code != (BPF_JMP | BPF_JA)) { | |
1491 | verbose(env, "last insn is not an exit or jmp\n"); | |
1492 | return -EINVAL; | |
1493 | } | |
1494 | subprog_start = subprog_end; | |
4cb3d99c JW |
1495 | cur_subprog++; |
1496 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1497 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1498 | } |
1499 | } | |
1500 | return 0; | |
1501 | } | |
1502 | ||
679c782d EC |
1503 | /* Parentage chain of this register (or stack slot) should take care of all |
1504 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1505 | */ | |
f4d7e40a | 1506 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 1507 | const struct bpf_reg_state *state, |
5327ed3d | 1508 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
1509 | { |
1510 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1511 | int cnt = 0; |
dc503a8a EC |
1512 | |
1513 | while (parent) { | |
1514 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1515 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1516 | break; |
9242b5f5 AS |
1517 | if (parent->live & REG_LIVE_DONE) { |
1518 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1519 | reg_type_str[parent->type], | |
1520 | parent->var_off.value, parent->off); | |
1521 | return -EFAULT; | |
1522 | } | |
5327ed3d JW |
1523 | /* The first condition is more likely to be true than the |
1524 | * second, checked it first. | |
1525 | */ | |
1526 | if ((parent->live & REG_LIVE_READ) == flag || | |
1527 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
1528 | /* The parentage chain never changes and |
1529 | * this parent was already marked as LIVE_READ. | |
1530 | * There is no need to keep walking the chain again and | |
1531 | * keep re-marking all parents as LIVE_READ. | |
1532 | * This case happens when the same register is read | |
1533 | * multiple times without writes into it in-between. | |
5327ed3d JW |
1534 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
1535 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
1536 | */ |
1537 | break; | |
dc503a8a | 1538 | /* ... then we depend on parent's value */ |
5327ed3d JW |
1539 | parent->live |= flag; |
1540 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
1541 | if (flag == REG_LIVE_READ64) | |
1542 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
1543 | state = parent; |
1544 | parent = state->parent; | |
f4d7e40a | 1545 | writes = true; |
06ee7115 | 1546 | cnt++; |
dc503a8a | 1547 | } |
06ee7115 AS |
1548 | |
1549 | if (env->longest_mark_read_walk < cnt) | |
1550 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1551 | return 0; |
dc503a8a EC |
1552 | } |
1553 | ||
5327ed3d JW |
1554 | /* This function is supposed to be used by the following 32-bit optimization |
1555 | * code only. It returns TRUE if the source or destination register operates | |
1556 | * on 64-bit, otherwise return FALSE. | |
1557 | */ | |
1558 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
1559 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
1560 | { | |
1561 | u8 code, class, op; | |
1562 | ||
1563 | code = insn->code; | |
1564 | class = BPF_CLASS(code); | |
1565 | op = BPF_OP(code); | |
1566 | if (class == BPF_JMP) { | |
1567 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
1568 | * conservatively. | |
1569 | */ | |
1570 | if (op == BPF_EXIT) | |
1571 | return true; | |
1572 | if (op == BPF_CALL) { | |
1573 | /* BPF to BPF call will reach here because of marking | |
1574 | * caller saved clobber with DST_OP_NO_MARK for which we | |
1575 | * don't care the register def because they are anyway | |
1576 | * marked as NOT_INIT already. | |
1577 | */ | |
1578 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1579 | return false; | |
1580 | /* Helper call will reach here because of arg type | |
1581 | * check, conservatively return TRUE. | |
1582 | */ | |
1583 | if (t == SRC_OP) | |
1584 | return true; | |
1585 | ||
1586 | return false; | |
1587 | } | |
1588 | } | |
1589 | ||
1590 | if (class == BPF_ALU64 || class == BPF_JMP || | |
1591 | /* BPF_END always use BPF_ALU class. */ | |
1592 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
1593 | return true; | |
1594 | ||
1595 | if (class == BPF_ALU || class == BPF_JMP32) | |
1596 | return false; | |
1597 | ||
1598 | if (class == BPF_LDX) { | |
1599 | if (t != SRC_OP) | |
1600 | return BPF_SIZE(code) == BPF_DW; | |
1601 | /* LDX source must be ptr. */ | |
1602 | return true; | |
1603 | } | |
1604 | ||
1605 | if (class == BPF_STX) { | |
1606 | if (reg->type != SCALAR_VALUE) | |
1607 | return true; | |
1608 | return BPF_SIZE(code) == BPF_DW; | |
1609 | } | |
1610 | ||
1611 | if (class == BPF_LD) { | |
1612 | u8 mode = BPF_MODE(code); | |
1613 | ||
1614 | /* LD_IMM64 */ | |
1615 | if (mode == BPF_IMM) | |
1616 | return true; | |
1617 | ||
1618 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
1619 | if (t != SRC_OP) | |
1620 | return false; | |
1621 | ||
1622 | /* Implicit ctx ptr. */ | |
1623 | if (regno == BPF_REG_6) | |
1624 | return true; | |
1625 | ||
1626 | /* Explicit source could be any width. */ | |
1627 | return true; | |
1628 | } | |
1629 | ||
1630 | if (class == BPF_ST) | |
1631 | /* The only source register for BPF_ST is a ptr. */ | |
1632 | return true; | |
1633 | ||
1634 | /* Conservatively return true at default. */ | |
1635 | return true; | |
1636 | } | |
1637 | ||
b325fbca JW |
1638 | /* Return TRUE if INSN doesn't have explicit value define. */ |
1639 | static bool insn_no_def(struct bpf_insn *insn) | |
1640 | { | |
1641 | u8 class = BPF_CLASS(insn->code); | |
1642 | ||
1643 | return (class == BPF_JMP || class == BPF_JMP32 || | |
1644 | class == BPF_STX || class == BPF_ST); | |
1645 | } | |
1646 | ||
1647 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
1648 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
1649 | { | |
1650 | if (insn_no_def(insn)) | |
1651 | return false; | |
1652 | ||
1653 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); | |
1654 | } | |
1655 | ||
5327ed3d JW |
1656 | static void mark_insn_zext(struct bpf_verifier_env *env, |
1657 | struct bpf_reg_state *reg) | |
1658 | { | |
1659 | s32 def_idx = reg->subreg_def; | |
1660 | ||
1661 | if (def_idx == DEF_NOT_SUBREG) | |
1662 | return; | |
1663 | ||
1664 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
1665 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
1666 | reg->subreg_def = DEF_NOT_SUBREG; | |
1667 | } | |
1668 | ||
dc503a8a | 1669 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
1670 | enum reg_arg_type t) |
1671 | { | |
f4d7e40a AS |
1672 | struct bpf_verifier_state *vstate = env->cur_state; |
1673 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 1674 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 1675 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 1676 | bool rw64; |
dc503a8a | 1677 | |
17a52670 | 1678 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1679 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1680 | return -EINVAL; |
1681 | } | |
1682 | ||
c342dc10 | 1683 | reg = ®s[regno]; |
5327ed3d | 1684 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
1685 | if (t == SRC_OP) { |
1686 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1687 | if (reg->type == NOT_INIT) { |
61bd5218 | 1688 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1689 | return -EACCES; |
1690 | } | |
679c782d | 1691 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1692 | if (regno == BPF_REG_FP) |
1693 | return 0; | |
1694 | ||
5327ed3d JW |
1695 | if (rw64) |
1696 | mark_insn_zext(env, reg); | |
1697 | ||
1698 | return mark_reg_read(env, reg, reg->parent, | |
1699 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
1700 | } else { |
1701 | /* check whether register used as dest operand can be written to */ | |
1702 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1703 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1704 | return -EACCES; |
1705 | } | |
c342dc10 | 1706 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 1707 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 1708 | if (t == DST_OP) |
61bd5218 | 1709 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1710 | } |
1711 | return 0; | |
1712 | } | |
1713 | ||
b5dc0163 AS |
1714 | /* for any branch, call, exit record the history of jmps in the given state */ |
1715 | static int push_jmp_history(struct bpf_verifier_env *env, | |
1716 | struct bpf_verifier_state *cur) | |
1717 | { | |
1718 | u32 cnt = cur->jmp_history_cnt; | |
1719 | struct bpf_idx_pair *p; | |
1720 | ||
1721 | cnt++; | |
1722 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
1723 | if (!p) | |
1724 | return -ENOMEM; | |
1725 | p[cnt - 1].idx = env->insn_idx; | |
1726 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
1727 | cur->jmp_history = p; | |
1728 | cur->jmp_history_cnt = cnt; | |
1729 | return 0; | |
1730 | } | |
1731 | ||
1732 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
1733 | * history then previous instruction came from straight line execution. | |
1734 | */ | |
1735 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
1736 | u32 *history) | |
1737 | { | |
1738 | u32 cnt = *history; | |
1739 | ||
1740 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
1741 | i = st->jmp_history[cnt - 1].prev_idx; | |
1742 | (*history)--; | |
1743 | } else { | |
1744 | i--; | |
1745 | } | |
1746 | return i; | |
1747 | } | |
1748 | ||
1749 | /* For given verifier state backtrack_insn() is called from the last insn to | |
1750 | * the first insn. Its purpose is to compute a bitmask of registers and | |
1751 | * stack slots that needs precision in the parent verifier state. | |
1752 | */ | |
1753 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
1754 | u32 *reg_mask, u64 *stack_mask) | |
1755 | { | |
1756 | const struct bpf_insn_cbs cbs = { | |
1757 | .cb_print = verbose, | |
1758 | .private_data = env, | |
1759 | }; | |
1760 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
1761 | u8 class = BPF_CLASS(insn->code); | |
1762 | u8 opcode = BPF_OP(insn->code); | |
1763 | u8 mode = BPF_MODE(insn->code); | |
1764 | u32 dreg = 1u << insn->dst_reg; | |
1765 | u32 sreg = 1u << insn->src_reg; | |
1766 | u32 spi; | |
1767 | ||
1768 | if (insn->code == 0) | |
1769 | return 0; | |
1770 | if (env->log.level & BPF_LOG_LEVEL) { | |
1771 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
1772 | verbose(env, "%d: ", idx); | |
1773 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
1774 | } | |
1775 | ||
1776 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1777 | if (!(*reg_mask & dreg)) | |
1778 | return 0; | |
1779 | if (opcode == BPF_MOV) { | |
1780 | if (BPF_SRC(insn->code) == BPF_X) { | |
1781 | /* dreg = sreg | |
1782 | * dreg needs precision after this insn | |
1783 | * sreg needs precision before this insn | |
1784 | */ | |
1785 | *reg_mask &= ~dreg; | |
1786 | *reg_mask |= sreg; | |
1787 | } else { | |
1788 | /* dreg = K | |
1789 | * dreg needs precision after this insn. | |
1790 | * Corresponding register is already marked | |
1791 | * as precise=true in this verifier state. | |
1792 | * No further markings in parent are necessary | |
1793 | */ | |
1794 | *reg_mask &= ~dreg; | |
1795 | } | |
1796 | } else { | |
1797 | if (BPF_SRC(insn->code) == BPF_X) { | |
1798 | /* dreg += sreg | |
1799 | * both dreg and sreg need precision | |
1800 | * before this insn | |
1801 | */ | |
1802 | *reg_mask |= sreg; | |
1803 | } /* else dreg += K | |
1804 | * dreg still needs precision before this insn | |
1805 | */ | |
1806 | } | |
1807 | } else if (class == BPF_LDX) { | |
1808 | if (!(*reg_mask & dreg)) | |
1809 | return 0; | |
1810 | *reg_mask &= ~dreg; | |
1811 | ||
1812 | /* scalars can only be spilled into stack w/o losing precision. | |
1813 | * Load from any other memory can be zero extended. | |
1814 | * The desire to keep that precision is already indicated | |
1815 | * by 'precise' mark in corresponding register of this state. | |
1816 | * No further tracking necessary. | |
1817 | */ | |
1818 | if (insn->src_reg != BPF_REG_FP) | |
1819 | return 0; | |
1820 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1821 | return 0; | |
1822 | ||
1823 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
1824 | * that [fp - off] slot contains scalar that needs to be | |
1825 | * tracked with precision | |
1826 | */ | |
1827 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1828 | if (spi >= 64) { | |
1829 | verbose(env, "BUG spi %d\n", spi); | |
1830 | WARN_ONCE(1, "verifier backtracking bug"); | |
1831 | return -EFAULT; | |
1832 | } | |
1833 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 1834 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 1835 | if (*reg_mask & dreg) |
b3b50f05 | 1836 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
1837 | * to access memory. It means backtracking |
1838 | * encountered a case of pointer subtraction. | |
1839 | */ | |
1840 | return -ENOTSUPP; | |
1841 | /* scalars can only be spilled into stack */ | |
1842 | if (insn->dst_reg != BPF_REG_FP) | |
1843 | return 0; | |
1844 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1845 | return 0; | |
1846 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1847 | if (spi >= 64) { | |
1848 | verbose(env, "BUG spi %d\n", spi); | |
1849 | WARN_ONCE(1, "verifier backtracking bug"); | |
1850 | return -EFAULT; | |
1851 | } | |
1852 | if (!(*stack_mask & (1ull << spi))) | |
1853 | return 0; | |
1854 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
1855 | if (class == BPF_STX) |
1856 | *reg_mask |= sreg; | |
b5dc0163 AS |
1857 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
1858 | if (opcode == BPF_CALL) { | |
1859 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1860 | return -ENOTSUPP; | |
1861 | /* regular helper call sets R0 */ | |
1862 | *reg_mask &= ~1; | |
1863 | if (*reg_mask & 0x3f) { | |
1864 | /* if backtracing was looking for registers R1-R5 | |
1865 | * they should have been found already. | |
1866 | */ | |
1867 | verbose(env, "BUG regs %x\n", *reg_mask); | |
1868 | WARN_ONCE(1, "verifier backtracking bug"); | |
1869 | return -EFAULT; | |
1870 | } | |
1871 | } else if (opcode == BPF_EXIT) { | |
1872 | return -ENOTSUPP; | |
1873 | } | |
1874 | } else if (class == BPF_LD) { | |
1875 | if (!(*reg_mask & dreg)) | |
1876 | return 0; | |
1877 | *reg_mask &= ~dreg; | |
1878 | /* It's ld_imm64 or ld_abs or ld_ind. | |
1879 | * For ld_imm64 no further tracking of precision | |
1880 | * into parent is necessary | |
1881 | */ | |
1882 | if (mode == BPF_IND || mode == BPF_ABS) | |
1883 | /* to be analyzed */ | |
1884 | return -ENOTSUPP; | |
b5dc0163 AS |
1885 | } |
1886 | return 0; | |
1887 | } | |
1888 | ||
1889 | /* the scalar precision tracking algorithm: | |
1890 | * . at the start all registers have precise=false. | |
1891 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
1892 | * . once precise value of the scalar register is used in: | |
1893 | * . ptr + scalar alu | |
1894 | * . if (scalar cond K|scalar) | |
1895 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
1896 | * backtrack through the verifier states and mark all registers and | |
1897 | * stack slots with spilled constants that these scalar regisers | |
1898 | * should be precise. | |
1899 | * . during state pruning two registers (or spilled stack slots) | |
1900 | * are equivalent if both are not precise. | |
1901 | * | |
1902 | * Note the verifier cannot simply walk register parentage chain, | |
1903 | * since many different registers and stack slots could have been | |
1904 | * used to compute single precise scalar. | |
1905 | * | |
1906 | * The approach of starting with precise=true for all registers and then | |
1907 | * backtrack to mark a register as not precise when the verifier detects | |
1908 | * that program doesn't care about specific value (e.g., when helper | |
1909 | * takes register as ARG_ANYTHING parameter) is not safe. | |
1910 | * | |
1911 | * It's ok to walk single parentage chain of the verifier states. | |
1912 | * It's possible that this backtracking will go all the way till 1st insn. | |
1913 | * All other branches will be explored for needing precision later. | |
1914 | * | |
1915 | * The backtracking needs to deal with cases like: | |
1916 | * R8=map_value(id=0,off=0,ks=4,vs=1952,imm=0) R9_w=map_value(id=0,off=40,ks=4,vs=1952,imm=0) | |
1917 | * r9 -= r8 | |
1918 | * r5 = r9 | |
1919 | * if r5 > 0x79f goto pc+7 | |
1920 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
1921 | * r5 += 1 | |
1922 | * ... | |
1923 | * call bpf_perf_event_output#25 | |
1924 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
1925 | * | |
1926 | * and this case: | |
1927 | * r6 = 1 | |
1928 | * call foo // uses callee's r6 inside to compute r0 | |
1929 | * r0 += r6 | |
1930 | * if r0 == 0 goto | |
1931 | * | |
1932 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
1933 | * | |
1934 | * Also if parent's curframe > frame where backtracking started, | |
1935 | * the verifier need to mark registers in both frames, otherwise callees | |
1936 | * may incorrectly prune callers. This is similar to | |
1937 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
1938 | * | |
1939 | * For now backtracking falls back into conservative marking. | |
1940 | */ | |
1941 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
1942 | struct bpf_verifier_state *st) | |
1943 | { | |
1944 | struct bpf_func_state *func; | |
1945 | struct bpf_reg_state *reg; | |
1946 | int i, j; | |
1947 | ||
1948 | /* big hammer: mark all scalars precise in this path. | |
1949 | * pop_stack may still get !precise scalars. | |
1950 | */ | |
1951 | for (; st; st = st->parent) | |
1952 | for (i = 0; i <= st->curframe; i++) { | |
1953 | func = st->frame[i]; | |
1954 | for (j = 0; j < BPF_REG_FP; j++) { | |
1955 | reg = &func->regs[j]; | |
1956 | if (reg->type != SCALAR_VALUE) | |
1957 | continue; | |
1958 | reg->precise = true; | |
1959 | } | |
1960 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
1961 | if (func->stack[j].slot_type[0] != STACK_SPILL) | |
1962 | continue; | |
1963 | reg = &func->stack[j].spilled_ptr; | |
1964 | if (reg->type != SCALAR_VALUE) | |
1965 | continue; | |
1966 | reg->precise = true; | |
1967 | } | |
1968 | } | |
1969 | } | |
1970 | ||
a3ce685d AS |
1971 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
1972 | int spi) | |
b5dc0163 AS |
1973 | { |
1974 | struct bpf_verifier_state *st = env->cur_state; | |
1975 | int first_idx = st->first_insn_idx; | |
1976 | int last_idx = env->insn_idx; | |
1977 | struct bpf_func_state *func; | |
1978 | struct bpf_reg_state *reg; | |
a3ce685d AS |
1979 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
1980 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 1981 | bool skip_first = true; |
a3ce685d | 1982 | bool new_marks = false; |
b5dc0163 AS |
1983 | int i, err; |
1984 | ||
2c78ee89 | 1985 | if (!env->bpf_capable) |
b5dc0163 AS |
1986 | return 0; |
1987 | ||
1988 | func = st->frame[st->curframe]; | |
a3ce685d AS |
1989 | if (regno >= 0) { |
1990 | reg = &func->regs[regno]; | |
1991 | if (reg->type != SCALAR_VALUE) { | |
1992 | WARN_ONCE(1, "backtracing misuse"); | |
1993 | return -EFAULT; | |
1994 | } | |
1995 | if (!reg->precise) | |
1996 | new_marks = true; | |
1997 | else | |
1998 | reg_mask = 0; | |
1999 | reg->precise = true; | |
b5dc0163 | 2000 | } |
b5dc0163 | 2001 | |
a3ce685d AS |
2002 | while (spi >= 0) { |
2003 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { | |
2004 | stack_mask = 0; | |
2005 | break; | |
2006 | } | |
2007 | reg = &func->stack[spi].spilled_ptr; | |
2008 | if (reg->type != SCALAR_VALUE) { | |
2009 | stack_mask = 0; | |
2010 | break; | |
2011 | } | |
2012 | if (!reg->precise) | |
2013 | new_marks = true; | |
2014 | else | |
2015 | stack_mask = 0; | |
2016 | reg->precise = true; | |
2017 | break; | |
2018 | } | |
2019 | ||
2020 | if (!new_marks) | |
2021 | return 0; | |
2022 | if (!reg_mask && !stack_mask) | |
2023 | return 0; | |
b5dc0163 AS |
2024 | for (;;) { |
2025 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2026 | u32 history = st->jmp_history_cnt; |
2027 | ||
2028 | if (env->log.level & BPF_LOG_LEVEL) | |
2029 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
2030 | for (i = last_idx;;) { | |
2031 | if (skip_first) { | |
2032 | err = 0; | |
2033 | skip_first = false; | |
2034 | } else { | |
2035 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2036 | } | |
2037 | if (err == -ENOTSUPP) { | |
2038 | mark_all_scalars_precise(env, st); | |
2039 | return 0; | |
2040 | } else if (err) { | |
2041 | return err; | |
2042 | } | |
2043 | if (!reg_mask && !stack_mask) | |
2044 | /* Found assignment(s) into tracked register in this state. | |
2045 | * Since this state is already marked, just return. | |
2046 | * Nothing to be tracked further in the parent state. | |
2047 | */ | |
2048 | return 0; | |
2049 | if (i == first_idx) | |
2050 | break; | |
2051 | i = get_prev_insn_idx(st, i, &history); | |
2052 | if (i >= env->prog->len) { | |
2053 | /* This can happen if backtracking reached insn 0 | |
2054 | * and there are still reg_mask or stack_mask | |
2055 | * to backtrack. | |
2056 | * It means the backtracking missed the spot where | |
2057 | * particular register was initialized with a constant. | |
2058 | */ | |
2059 | verbose(env, "BUG backtracking idx %d\n", i); | |
2060 | WARN_ONCE(1, "verifier backtracking bug"); | |
2061 | return -EFAULT; | |
2062 | } | |
2063 | } | |
2064 | st = st->parent; | |
2065 | if (!st) | |
2066 | break; | |
2067 | ||
a3ce685d | 2068 | new_marks = false; |
b5dc0163 AS |
2069 | func = st->frame[st->curframe]; |
2070 | bitmap_from_u64(mask, reg_mask); | |
2071 | for_each_set_bit(i, mask, 32) { | |
2072 | reg = &func->regs[i]; | |
a3ce685d AS |
2073 | if (reg->type != SCALAR_VALUE) { |
2074 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2075 | continue; |
a3ce685d | 2076 | } |
b5dc0163 AS |
2077 | if (!reg->precise) |
2078 | new_marks = true; | |
2079 | reg->precise = true; | |
2080 | } | |
2081 | ||
2082 | bitmap_from_u64(mask, stack_mask); | |
2083 | for_each_set_bit(i, mask, 64) { | |
2084 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2085 | /* the sequence of instructions: |
2086 | * 2: (bf) r3 = r10 | |
2087 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2088 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2089 | * doesn't contain jmps. It's backtracked | |
2090 | * as a single block. | |
2091 | * During backtracking insn 3 is not recognized as | |
2092 | * stack access, so at the end of backtracking | |
2093 | * stack slot fp-8 is still marked in stack_mask. | |
2094 | * However the parent state may not have accessed | |
2095 | * fp-8 and it's "unallocated" stack space. | |
2096 | * In such case fallback to conservative. | |
b5dc0163 | 2097 | */ |
2339cd6c AS |
2098 | mark_all_scalars_precise(env, st); |
2099 | return 0; | |
b5dc0163 AS |
2100 | } |
2101 | ||
a3ce685d AS |
2102 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
2103 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2104 | continue; |
a3ce685d | 2105 | } |
b5dc0163 | 2106 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2107 | if (reg->type != SCALAR_VALUE) { |
2108 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2109 | continue; |
a3ce685d | 2110 | } |
b5dc0163 AS |
2111 | if (!reg->precise) |
2112 | new_marks = true; | |
2113 | reg->precise = true; | |
2114 | } | |
2115 | if (env->log.level & BPF_LOG_LEVEL) { | |
2116 | print_verifier_state(env, func); | |
2117 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
2118 | new_marks ? "didn't have" : "already had", | |
2119 | reg_mask, stack_mask); | |
2120 | } | |
2121 | ||
a3ce685d AS |
2122 | if (!reg_mask && !stack_mask) |
2123 | break; | |
b5dc0163 AS |
2124 | if (!new_marks) |
2125 | break; | |
2126 | ||
2127 | last_idx = st->last_insn_idx; | |
2128 | first_idx = st->first_insn_idx; | |
2129 | } | |
2130 | return 0; | |
2131 | } | |
2132 | ||
a3ce685d AS |
2133 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
2134 | { | |
2135 | return __mark_chain_precision(env, regno, -1); | |
2136 | } | |
2137 | ||
2138 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2139 | { | |
2140 | return __mark_chain_precision(env, -1, spi); | |
2141 | } | |
b5dc0163 | 2142 | |
1be7f75d AS |
2143 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2144 | { | |
2145 | switch (type) { | |
2146 | case PTR_TO_MAP_VALUE: | |
2147 | case PTR_TO_MAP_VALUE_OR_NULL: | |
2148 | case PTR_TO_STACK: | |
2149 | case PTR_TO_CTX: | |
969bf05e | 2150 | case PTR_TO_PACKET: |
de8f3a83 | 2151 | case PTR_TO_PACKET_META: |
969bf05e | 2152 | case PTR_TO_PACKET_END: |
d58e468b | 2153 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2154 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
2155 | case PTR_TO_SOCKET: |
2156 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
2157 | case PTR_TO_SOCK_COMMON: |
2158 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
2159 | case PTR_TO_TCP_SOCK: |
2160 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 2161 | case PTR_TO_XDP_SOCK: |
65726b5b | 2162 | case PTR_TO_BTF_ID: |
b121b341 | 2163 | case PTR_TO_BTF_ID_OR_NULL: |
1be7f75d AS |
2164 | return true; |
2165 | default: | |
2166 | return false; | |
2167 | } | |
2168 | } | |
2169 | ||
cc2b14d5 AS |
2170 | /* Does this register contain a constant zero? */ |
2171 | static bool register_is_null(struct bpf_reg_state *reg) | |
2172 | { | |
2173 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2174 | } | |
2175 | ||
f7cf25b2 AS |
2176 | static bool register_is_const(struct bpf_reg_state *reg) |
2177 | { | |
2178 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2179 | } | |
2180 | ||
6e7e63cb JH |
2181 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2182 | const struct bpf_reg_state *reg) | |
2183 | { | |
2184 | if (allow_ptr_leaks) | |
2185 | return false; | |
2186 | ||
2187 | return reg->type != SCALAR_VALUE; | |
2188 | } | |
2189 | ||
f7cf25b2 AS |
2190 | static void save_register_state(struct bpf_func_state *state, |
2191 | int spi, struct bpf_reg_state *reg) | |
2192 | { | |
2193 | int i; | |
2194 | ||
2195 | state->stack[spi].spilled_ptr = *reg; | |
2196 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2197 | ||
2198 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2199 | state->stack[spi].slot_type[i] = STACK_SPILL; | |
2200 | } | |
2201 | ||
17a52670 AS |
2202 | /* check_stack_read/write functions track spill/fill of registers, |
2203 | * stack boundary and alignment are checked in check_mem_access() | |
2204 | */ | |
61bd5218 | 2205 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 2206 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 2207 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 2208 | { |
f4d7e40a | 2209 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 2210 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 2211 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 2212 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 2213 | |
f4d7e40a | 2214 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 2215 | state->acquired_refs, true); |
638f5b90 AS |
2216 | if (err) |
2217 | return err; | |
9c399760 AS |
2218 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
2219 | * so it's aligned access and [off, off + size) are within stack limits | |
2220 | */ | |
638f5b90 AS |
2221 | if (!env->allow_ptr_leaks && |
2222 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
2223 | size != BPF_REG_SIZE) { | |
2224 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
2225 | return -EACCES; | |
2226 | } | |
17a52670 | 2227 | |
f4d7e40a | 2228 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
2229 | if (value_regno >= 0) |
2230 | reg = &cur->regs[value_regno]; | |
17a52670 | 2231 | |
f7cf25b2 | 2232 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
2c78ee89 | 2233 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
2234 | if (dst_reg != BPF_REG_FP) { |
2235 | /* The backtracking logic can only recognize explicit | |
2236 | * stack slot address like [fp - 8]. Other spill of | |
2237 | * scalar via different register has to be conervative. | |
2238 | * Backtrack from here and mark all registers as precise | |
2239 | * that contributed into 'reg' being a constant. | |
2240 | */ | |
2241 | err = mark_chain_precision(env, value_regno); | |
2242 | if (err) | |
2243 | return err; | |
2244 | } | |
f7cf25b2 AS |
2245 | save_register_state(state, spi, reg); |
2246 | } else if (reg && is_spillable_regtype(reg->type)) { | |
17a52670 | 2247 | /* register containing pointer is being spilled into stack */ |
9c399760 | 2248 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 2249 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 2250 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
2251 | return -EACCES; |
2252 | } | |
2253 | ||
f7cf25b2 | 2254 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2255 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2256 | return -EINVAL; | |
2257 | } | |
2258 | ||
2c78ee89 | 2259 | if (!env->bypass_spec_v4) { |
f7cf25b2 | 2260 | bool sanitize = false; |
17a52670 | 2261 | |
f7cf25b2 AS |
2262 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
2263 | register_is_const(&state->stack[spi].spilled_ptr)) | |
2264 | sanitize = true; | |
2265 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2266 | if (state->stack[spi].slot_type[i] == STACK_MISC) { | |
2267 | sanitize = true; | |
2268 | break; | |
2269 | } | |
2270 | if (sanitize) { | |
af86ca4e AS |
2271 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
2272 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
2273 | ||
2274 | /* detected reuse of integer stack slot with a pointer | |
2275 | * which means either llvm is reusing stack slot or | |
2276 | * an attacker is trying to exploit CVE-2018-3639 | |
2277 | * (speculative store bypass) | |
2278 | * Have to sanitize that slot with preemptive | |
2279 | * store of zero. | |
2280 | */ | |
2281 | if (*poff && *poff != soff) { | |
2282 | /* disallow programs where single insn stores | |
2283 | * into two different stack slots, since verifier | |
2284 | * cannot sanitize them | |
2285 | */ | |
2286 | verbose(env, | |
2287 | "insn %d cannot access two stack slots fp%d and fp%d", | |
2288 | insn_idx, *poff, soff); | |
2289 | return -EINVAL; | |
2290 | } | |
2291 | *poff = soff; | |
2292 | } | |
af86ca4e | 2293 | } |
f7cf25b2 | 2294 | save_register_state(state, spi, reg); |
9c399760 | 2295 | } else { |
cc2b14d5 AS |
2296 | u8 type = STACK_MISC; |
2297 | ||
679c782d EC |
2298 | /* regular write of data into stack destroys any spilled ptr */ |
2299 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
2300 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
2301 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
2302 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2303 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 2304 | |
cc2b14d5 AS |
2305 | /* only mark the slot as written if all 8 bytes were written |
2306 | * otherwise read propagation may incorrectly stop too soon | |
2307 | * when stack slots are partially written. | |
2308 | * This heuristic means that read propagation will be | |
2309 | * conservative, since it will add reg_live_read marks | |
2310 | * to stack slots all the way to first state when programs | |
2311 | * writes+reads less than 8 bytes | |
2312 | */ | |
2313 | if (size == BPF_REG_SIZE) | |
2314 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2315 | ||
2316 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2317 | if (reg && register_is_null(reg)) { |
2318 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2319 | err = mark_chain_precision(env, value_regno); | |
2320 | if (err) | |
2321 | return err; | |
cc2b14d5 | 2322 | type = STACK_ZERO; |
b5dc0163 | 2323 | } |
cc2b14d5 | 2324 | |
0bae2d4d | 2325 | /* Mark slots affected by this stack write. */ |
9c399760 | 2326 | for (i = 0; i < size; i++) |
638f5b90 | 2327 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2328 | type; |
17a52670 AS |
2329 | } |
2330 | return 0; | |
2331 | } | |
2332 | ||
61bd5218 | 2333 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
2334 | struct bpf_func_state *reg_state /* func where register points to */, |
2335 | int off, int size, int value_regno) | |
17a52670 | 2336 | { |
f4d7e40a AS |
2337 | struct bpf_verifier_state *vstate = env->cur_state; |
2338 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 2339 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 2340 | struct bpf_reg_state *reg; |
638f5b90 | 2341 | u8 *stype; |
17a52670 | 2342 | |
f4d7e40a | 2343 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
2344 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
2345 | off, size); | |
2346 | return -EACCES; | |
2347 | } | |
f4d7e40a | 2348 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 2349 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 2350 | |
638f5b90 | 2351 | if (stype[0] == STACK_SPILL) { |
9c399760 | 2352 | if (size != BPF_REG_SIZE) { |
f7cf25b2 AS |
2353 | if (reg->type != SCALAR_VALUE) { |
2354 | verbose_linfo(env, env->insn_idx, "; "); | |
2355 | verbose(env, "invalid size of register fill\n"); | |
2356 | return -EACCES; | |
2357 | } | |
2358 | if (value_regno >= 0) { | |
2359 | mark_reg_unknown(env, state->regs, value_regno); | |
2360 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
2361 | } | |
2362 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
2363 | return 0; | |
17a52670 | 2364 | } |
9c399760 | 2365 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 2366 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 2367 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
2368 | return -EACCES; |
2369 | } | |
2370 | } | |
2371 | ||
dc503a8a | 2372 | if (value_regno >= 0) { |
17a52670 | 2373 | /* restore register state from stack */ |
f7cf25b2 | 2374 | state->regs[value_regno] = *reg; |
2f18f62e AS |
2375 | /* mark reg as written since spilled pointer state likely |
2376 | * has its liveness marks cleared by is_state_visited() | |
2377 | * which resets stack/reg liveness for state transitions | |
2378 | */ | |
2379 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
6e7e63cb JH |
2380 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
2381 | /* If value_regno==-1, the caller is asking us whether | |
2382 | * it is acceptable to use this value as a SCALAR_VALUE | |
2383 | * (e.g. for XADD). | |
2384 | * We must not allow unprivileged callers to do that | |
2385 | * with spilled pointers. | |
2386 | */ | |
2387 | verbose(env, "leaking pointer from stack off %d\n", | |
2388 | off); | |
2389 | return -EACCES; | |
dc503a8a | 2390 | } |
f7cf25b2 | 2391 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 | 2392 | } else { |
cc2b14d5 AS |
2393 | int zeros = 0; |
2394 | ||
17a52670 | 2395 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
2396 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
2397 | continue; | |
2398 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
2399 | zeros++; | |
2400 | continue; | |
17a52670 | 2401 | } |
cc2b14d5 AS |
2402 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
2403 | off, i, size); | |
2404 | return -EACCES; | |
2405 | } | |
f7cf25b2 | 2406 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
cc2b14d5 AS |
2407 | if (value_regno >= 0) { |
2408 | if (zeros == size) { | |
2409 | /* any size read into register is zero extended, | |
2410 | * so the whole register == const_zero | |
2411 | */ | |
2412 | __mark_reg_const_zero(&state->regs[value_regno]); | |
b5dc0163 AS |
2413 | /* backtracking doesn't support STACK_ZERO yet, |
2414 | * so mark it precise here, so that later | |
2415 | * backtracking can stop here. | |
2416 | * Backtracking may not need this if this register | |
2417 | * doesn't participate in pointer adjustment. | |
2418 | * Forward propagation of precise flag is not | |
2419 | * necessary either. This mark is only to stop | |
2420 | * backtracking. Any register that contributed | |
2421 | * to const 0 was marked precise before spill. | |
2422 | */ | |
2423 | state->regs[value_regno].precise = true; | |
cc2b14d5 AS |
2424 | } else { |
2425 | /* have read misc data from the stack */ | |
2426 | mark_reg_unknown(env, state->regs, value_regno); | |
2427 | } | |
2428 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 2429 | } |
17a52670 | 2430 | } |
f7cf25b2 | 2431 | return 0; |
17a52670 AS |
2432 | } |
2433 | ||
e4298d25 DB |
2434 | static int check_stack_access(struct bpf_verifier_env *env, |
2435 | const struct bpf_reg_state *reg, | |
2436 | int off, int size) | |
2437 | { | |
2438 | /* Stack accesses must be at a fixed offset, so that we | |
2439 | * can determine what type of data were returned. See | |
2440 | * check_stack_read(). | |
2441 | */ | |
2442 | if (!tnum_is_const(reg->var_off)) { | |
2443 | char tn_buf[48]; | |
2444 | ||
2445 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 2446 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
2447 | tn_buf, off, size); |
2448 | return -EACCES; | |
2449 | } | |
2450 | ||
2451 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
2452 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
2453 | return -EACCES; | |
2454 | } | |
2455 | ||
2456 | return 0; | |
2457 | } | |
2458 | ||
591fe988 DB |
2459 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
2460 | int off, int size, enum bpf_access_type type) | |
2461 | { | |
2462 | struct bpf_reg_state *regs = cur_regs(env); | |
2463 | struct bpf_map *map = regs[regno].map_ptr; | |
2464 | u32 cap = bpf_map_flags_to_cap(map); | |
2465 | ||
2466 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
2467 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
2468 | map->value_size, off, size); | |
2469 | return -EACCES; | |
2470 | } | |
2471 | ||
2472 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
2473 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
2474 | map->value_size, off, size); | |
2475 | return -EACCES; | |
2476 | } | |
2477 | ||
2478 | return 0; | |
2479 | } | |
2480 | ||
457f4436 AN |
2481 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
2482 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
2483 | int off, int size, u32 mem_size, | |
2484 | bool zero_size_allowed) | |
17a52670 | 2485 | { |
457f4436 AN |
2486 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
2487 | struct bpf_reg_state *reg; | |
2488 | ||
2489 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
2490 | return 0; | |
17a52670 | 2491 | |
457f4436 AN |
2492 | reg = &cur_regs(env)[regno]; |
2493 | switch (reg->type) { | |
2494 | case PTR_TO_MAP_VALUE: | |
61bd5218 | 2495 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
2496 | mem_size, off, size); |
2497 | break; | |
2498 | case PTR_TO_PACKET: | |
2499 | case PTR_TO_PACKET_META: | |
2500 | case PTR_TO_PACKET_END: | |
2501 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
2502 | off, size, regno, reg->id, off, mem_size); | |
2503 | break; | |
2504 | case PTR_TO_MEM: | |
2505 | default: | |
2506 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
2507 | mem_size, off, size); | |
17a52670 | 2508 | } |
457f4436 AN |
2509 | |
2510 | return -EACCES; | |
17a52670 AS |
2511 | } |
2512 | ||
457f4436 AN |
2513 | /* check read/write into a memory region with possible variable offset */ |
2514 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
2515 | int off, int size, u32 mem_size, | |
2516 | bool zero_size_allowed) | |
dbcfe5f7 | 2517 | { |
f4d7e40a AS |
2518 | struct bpf_verifier_state *vstate = env->cur_state; |
2519 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
2520 | struct bpf_reg_state *reg = &state->regs[regno]; |
2521 | int err; | |
2522 | ||
457f4436 | 2523 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
2524 | * need to try adding each of min_value and max_value to off |
2525 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 2526 | */ |
06ee7115 | 2527 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 2528 | print_verifier_state(env, state); |
b7137c4e | 2529 | |
dbcfe5f7 GB |
2530 | /* The minimum value is only important with signed |
2531 | * comparisons where we can't assume the floor of a | |
2532 | * value is 0. If we are using signed variables for our | |
2533 | * index'es we need to make sure that whatever we use | |
2534 | * will have a set floor within our range. | |
2535 | */ | |
b7137c4e DB |
2536 | if (reg->smin_value < 0 && |
2537 | (reg->smin_value == S64_MIN || | |
2538 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
2539 | reg->smin_value + off < 0)) { | |
61bd5218 | 2540 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
2541 | regno); |
2542 | return -EACCES; | |
2543 | } | |
457f4436 AN |
2544 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
2545 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 2546 | if (err) { |
457f4436 | 2547 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 2548 | regno); |
dbcfe5f7 GB |
2549 | return err; |
2550 | } | |
2551 | ||
b03c9f9f EC |
2552 | /* If we haven't set a max value then we need to bail since we can't be |
2553 | * sure we won't do bad things. | |
2554 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 2555 | */ |
b03c9f9f | 2556 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 2557 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
2558 | regno); |
2559 | return -EACCES; | |
2560 | } | |
457f4436 AN |
2561 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
2562 | mem_size, zero_size_allowed); | |
2563 | if (err) { | |
2564 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 2565 | regno); |
457f4436 AN |
2566 | return err; |
2567 | } | |
2568 | ||
2569 | return 0; | |
2570 | } | |
d83525ca | 2571 | |
457f4436 AN |
2572 | /* check read/write into a map element with possible variable offset */ |
2573 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
2574 | int off, int size, bool zero_size_allowed) | |
2575 | { | |
2576 | struct bpf_verifier_state *vstate = env->cur_state; | |
2577 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
2578 | struct bpf_reg_state *reg = &state->regs[regno]; | |
2579 | struct bpf_map *map = reg->map_ptr; | |
2580 | int err; | |
2581 | ||
2582 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
2583 | zero_size_allowed); | |
2584 | if (err) | |
2585 | return err; | |
2586 | ||
2587 | if (map_value_has_spin_lock(map)) { | |
2588 | u32 lock = map->spin_lock_off; | |
d83525ca AS |
2589 | |
2590 | /* if any part of struct bpf_spin_lock can be touched by | |
2591 | * load/store reject this program. | |
2592 | * To check that [x1, x2) overlaps with [y1, y2) | |
2593 | * it is sufficient to check x1 < y2 && y1 < x2. | |
2594 | */ | |
2595 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
2596 | lock < reg->umax_value + off + size) { | |
2597 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
2598 | return -EACCES; | |
2599 | } | |
2600 | } | |
f1174f77 | 2601 | return err; |
dbcfe5f7 GB |
2602 | } |
2603 | ||
969bf05e AS |
2604 | #define MAX_PACKET_OFF 0xffff |
2605 | ||
58e2af8b | 2606 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
2607 | const struct bpf_call_arg_meta *meta, |
2608 | enum bpf_access_type t) | |
4acf6c0b | 2609 | { |
36bbef52 | 2610 | switch (env->prog->type) { |
5d66fa7d | 2611 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
2612 | case BPF_PROG_TYPE_LWT_IN: |
2613 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 2614 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 2615 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 2616 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 2617 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
2618 | if (t == BPF_WRITE) |
2619 | return false; | |
7e57fbb2 | 2620 | /* fallthrough */ |
5d66fa7d DB |
2621 | |
2622 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
2623 | case BPF_PROG_TYPE_SCHED_CLS: |
2624 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 2625 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 2626 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 2627 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 2628 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
2629 | if (meta) |
2630 | return meta->pkt_access; | |
2631 | ||
2632 | env->seen_direct_write = true; | |
4acf6c0b | 2633 | return true; |
0d01da6a SF |
2634 | |
2635 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2636 | if (t == BPF_WRITE) | |
2637 | env->seen_direct_write = true; | |
2638 | ||
2639 | return true; | |
2640 | ||
4acf6c0b BB |
2641 | default: |
2642 | return false; | |
2643 | } | |
2644 | } | |
2645 | ||
f1174f77 | 2646 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2647 | int size, bool zero_size_allowed) |
f1174f77 | 2648 | { |
638f5b90 | 2649 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
2650 | struct bpf_reg_state *reg = ®s[regno]; |
2651 | int err; | |
2652 | ||
2653 | /* We may have added a variable offset to the packet pointer; but any | |
2654 | * reg->range we have comes after that. We are only checking the fixed | |
2655 | * offset. | |
2656 | */ | |
2657 | ||
2658 | /* We don't allow negative numbers, because we aren't tracking enough | |
2659 | * detail to prove they're safe. | |
2660 | */ | |
b03c9f9f | 2661 | if (reg->smin_value < 0) { |
61bd5218 | 2662 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
2663 | regno); |
2664 | return -EACCES; | |
2665 | } | |
457f4436 AN |
2666 | err = __check_mem_access(env, regno, off, size, reg->range, |
2667 | zero_size_allowed); | |
f1174f77 | 2668 | if (err) { |
61bd5218 | 2669 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
2670 | return err; |
2671 | } | |
e647815a | 2672 | |
457f4436 | 2673 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
2674 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
2675 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 2676 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
2677 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
2678 | */ | |
2679 | env->prog->aux->max_pkt_offset = | |
2680 | max_t(u32, env->prog->aux->max_pkt_offset, | |
2681 | off + reg->umax_value + size - 1); | |
2682 | ||
f1174f77 EC |
2683 | return err; |
2684 | } | |
2685 | ||
2686 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 2687 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 AS |
2688 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
2689 | u32 *btf_id) | |
17a52670 | 2690 | { |
f96da094 DB |
2691 | struct bpf_insn_access_aux info = { |
2692 | .reg_type = *reg_type, | |
9e15db66 | 2693 | .log = &env->log, |
f96da094 | 2694 | }; |
31fd8581 | 2695 | |
4f9218aa | 2696 | if (env->ops->is_valid_access && |
5e43f899 | 2697 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
2698 | /* A non zero info.ctx_field_size indicates that this field is a |
2699 | * candidate for later verifier transformation to load the whole | |
2700 | * field and then apply a mask when accessed with a narrower | |
2701 | * access than actual ctx access size. A zero info.ctx_field_size | |
2702 | * will only allow for whole field access and rejects any other | |
2703 | * type of narrower access. | |
31fd8581 | 2704 | */ |
23994631 | 2705 | *reg_type = info.reg_type; |
31fd8581 | 2706 | |
b121b341 | 2707 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) |
9e15db66 AS |
2708 | *btf_id = info.btf_id; |
2709 | else | |
2710 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; | |
32bbe007 AS |
2711 | /* remember the offset of last byte accessed in ctx */ |
2712 | if (env->prog->aux->max_ctx_offset < off + size) | |
2713 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 2714 | return 0; |
32bbe007 | 2715 | } |
17a52670 | 2716 | |
61bd5218 | 2717 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
2718 | return -EACCES; |
2719 | } | |
2720 | ||
d58e468b PP |
2721 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
2722 | int size) | |
2723 | { | |
2724 | if (size < 0 || off < 0 || | |
2725 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
2726 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
2727 | off, size); | |
2728 | return -EACCES; | |
2729 | } | |
2730 | return 0; | |
2731 | } | |
2732 | ||
5f456649 MKL |
2733 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
2734 | u32 regno, int off, int size, | |
2735 | enum bpf_access_type t) | |
c64b7983 JS |
2736 | { |
2737 | struct bpf_reg_state *regs = cur_regs(env); | |
2738 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 2739 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 2740 | bool valid; |
c64b7983 JS |
2741 | |
2742 | if (reg->smin_value < 0) { | |
2743 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
2744 | regno); | |
2745 | return -EACCES; | |
2746 | } | |
2747 | ||
46f8bc92 MKL |
2748 | switch (reg->type) { |
2749 | case PTR_TO_SOCK_COMMON: | |
2750 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
2751 | break; | |
2752 | case PTR_TO_SOCKET: | |
2753 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
2754 | break; | |
655a51e5 MKL |
2755 | case PTR_TO_TCP_SOCK: |
2756 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
2757 | break; | |
fada7fdc JL |
2758 | case PTR_TO_XDP_SOCK: |
2759 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
2760 | break; | |
46f8bc92 MKL |
2761 | default: |
2762 | valid = false; | |
c64b7983 JS |
2763 | } |
2764 | ||
5f456649 | 2765 | |
46f8bc92 MKL |
2766 | if (valid) { |
2767 | env->insn_aux_data[insn_idx].ctx_field_size = | |
2768 | info.ctx_field_size; | |
2769 | return 0; | |
2770 | } | |
2771 | ||
2772 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
2773 | regno, reg_type_str[reg->type], off, size); | |
2774 | ||
2775 | return -EACCES; | |
c64b7983 JS |
2776 | } |
2777 | ||
2a159c6f DB |
2778 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
2779 | { | |
2780 | return cur_regs(env) + regno; | |
2781 | } | |
2782 | ||
4cabc5b1 DB |
2783 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
2784 | { | |
2a159c6f | 2785 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
2786 | } |
2787 | ||
f37a8cb8 DB |
2788 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
2789 | { | |
2a159c6f | 2790 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 2791 | |
46f8bc92 MKL |
2792 | return reg->type == PTR_TO_CTX; |
2793 | } | |
2794 | ||
2795 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
2796 | { | |
2797 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2798 | ||
2799 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
2800 | } |
2801 | ||
ca369602 DB |
2802 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
2803 | { | |
2a159c6f | 2804 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
2805 | |
2806 | return type_is_pkt_pointer(reg->type); | |
2807 | } | |
2808 | ||
4b5defde DB |
2809 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
2810 | { | |
2811 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2812 | ||
2813 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
2814 | return reg->type == PTR_TO_FLOW_KEYS; | |
2815 | } | |
2816 | ||
61bd5218 JK |
2817 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
2818 | const struct bpf_reg_state *reg, | |
d1174416 | 2819 | int off, int size, bool strict) |
969bf05e | 2820 | { |
f1174f77 | 2821 | struct tnum reg_off; |
e07b98d9 | 2822 | int ip_align; |
d1174416 DM |
2823 | |
2824 | /* Byte size accesses are always allowed. */ | |
2825 | if (!strict || size == 1) | |
2826 | return 0; | |
2827 | ||
e4eda884 DM |
2828 | /* For platforms that do not have a Kconfig enabling |
2829 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
2830 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
2831 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
2832 | * to this code only in strict mode where we want to emulate | |
2833 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
2834 | * unconditional IP align value of '2'. | |
e07b98d9 | 2835 | */ |
e4eda884 | 2836 | ip_align = 2; |
f1174f77 EC |
2837 | |
2838 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
2839 | if (!tnum_is_aligned(reg_off, size)) { | |
2840 | char tn_buf[48]; | |
2841 | ||
2842 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
2843 | verbose(env, |
2844 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 2845 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
2846 | return -EACCES; |
2847 | } | |
79adffcd | 2848 | |
969bf05e AS |
2849 | return 0; |
2850 | } | |
2851 | ||
61bd5218 JK |
2852 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
2853 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
2854 | const char *pointer_desc, |
2855 | int off, int size, bool strict) | |
79adffcd | 2856 | { |
f1174f77 EC |
2857 | struct tnum reg_off; |
2858 | ||
2859 | /* Byte size accesses are always allowed. */ | |
2860 | if (!strict || size == 1) | |
2861 | return 0; | |
2862 | ||
2863 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
2864 | if (!tnum_is_aligned(reg_off, size)) { | |
2865 | char tn_buf[48]; | |
2866 | ||
2867 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 2868 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 2869 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
2870 | return -EACCES; |
2871 | } | |
2872 | ||
969bf05e AS |
2873 | return 0; |
2874 | } | |
2875 | ||
e07b98d9 | 2876 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
2877 | const struct bpf_reg_state *reg, int off, |
2878 | int size, bool strict_alignment_once) | |
79adffcd | 2879 | { |
ca369602 | 2880 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 2881 | const char *pointer_desc = ""; |
d1174416 | 2882 | |
79adffcd DB |
2883 | switch (reg->type) { |
2884 | case PTR_TO_PACKET: | |
de8f3a83 DB |
2885 | case PTR_TO_PACKET_META: |
2886 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
2887 | * right in front, treat it the very same way. | |
2888 | */ | |
61bd5218 | 2889 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
2890 | case PTR_TO_FLOW_KEYS: |
2891 | pointer_desc = "flow keys "; | |
2892 | break; | |
f1174f77 EC |
2893 | case PTR_TO_MAP_VALUE: |
2894 | pointer_desc = "value "; | |
2895 | break; | |
2896 | case PTR_TO_CTX: | |
2897 | pointer_desc = "context "; | |
2898 | break; | |
2899 | case PTR_TO_STACK: | |
2900 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
2901 | /* The stack spill tracking logic in check_stack_write() |
2902 | * and check_stack_read() relies on stack accesses being | |
2903 | * aligned. | |
2904 | */ | |
2905 | strict = true; | |
f1174f77 | 2906 | break; |
c64b7983 JS |
2907 | case PTR_TO_SOCKET: |
2908 | pointer_desc = "sock "; | |
2909 | break; | |
46f8bc92 MKL |
2910 | case PTR_TO_SOCK_COMMON: |
2911 | pointer_desc = "sock_common "; | |
2912 | break; | |
655a51e5 MKL |
2913 | case PTR_TO_TCP_SOCK: |
2914 | pointer_desc = "tcp_sock "; | |
2915 | break; | |
fada7fdc JL |
2916 | case PTR_TO_XDP_SOCK: |
2917 | pointer_desc = "xdp_sock "; | |
2918 | break; | |
79adffcd | 2919 | default: |
f1174f77 | 2920 | break; |
79adffcd | 2921 | } |
61bd5218 JK |
2922 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
2923 | strict); | |
79adffcd DB |
2924 | } |
2925 | ||
f4d7e40a AS |
2926 | static int update_stack_depth(struct bpf_verifier_env *env, |
2927 | const struct bpf_func_state *func, | |
2928 | int off) | |
2929 | { | |
9c8105bd | 2930 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
2931 | |
2932 | if (stack >= -off) | |
2933 | return 0; | |
2934 | ||
2935 | /* update known max for given subprogram */ | |
9c8105bd | 2936 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
2937 | return 0; |
2938 | } | |
f4d7e40a | 2939 | |
70a87ffe AS |
2940 | /* starting from main bpf function walk all instructions of the function |
2941 | * and recursively walk all callees that given function can call. | |
2942 | * Ignore jump and exit insns. | |
2943 | * Since recursion is prevented by check_cfg() this algorithm | |
2944 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
2945 | */ | |
2946 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
2947 | { | |
9c8105bd JW |
2948 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
2949 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 2950 | struct bpf_insn *insn = env->prog->insnsi; |
70a87ffe AS |
2951 | int ret_insn[MAX_CALL_FRAMES]; |
2952 | int ret_prog[MAX_CALL_FRAMES]; | |
f4d7e40a | 2953 | |
70a87ffe AS |
2954 | process_func: |
2955 | /* round up to 32-bytes, since this is granularity | |
2956 | * of interpreter stack size | |
2957 | */ | |
9c8105bd | 2958 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 2959 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 2960 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 2961 | frame + 1, depth); |
f4d7e40a AS |
2962 | return -EACCES; |
2963 | } | |
70a87ffe | 2964 | continue_func: |
4cb3d99c | 2965 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
2966 | for (; i < subprog_end; i++) { |
2967 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
2968 | continue; | |
2969 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
2970 | continue; | |
2971 | /* remember insn and function to return to */ | |
2972 | ret_insn[frame] = i + 1; | |
9c8105bd | 2973 | ret_prog[frame] = idx; |
70a87ffe AS |
2974 | |
2975 | /* find the callee */ | |
2976 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
2977 | idx = find_subprog(env, i); |
2978 | if (idx < 0) { | |
70a87ffe AS |
2979 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
2980 | i); | |
2981 | return -EFAULT; | |
2982 | } | |
70a87ffe AS |
2983 | frame++; |
2984 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
2985 | verbose(env, "the call stack of %d frames is too deep !\n", |
2986 | frame); | |
2987 | return -E2BIG; | |
70a87ffe AS |
2988 | } |
2989 | goto process_func; | |
2990 | } | |
2991 | /* end of for() loop means the last insn of the 'subprog' | |
2992 | * was reached. Doesn't matter whether it was JA or EXIT | |
2993 | */ | |
2994 | if (frame == 0) | |
2995 | return 0; | |
9c8105bd | 2996 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
2997 | frame--; |
2998 | i = ret_insn[frame]; | |
9c8105bd | 2999 | idx = ret_prog[frame]; |
70a87ffe | 3000 | goto continue_func; |
f4d7e40a AS |
3001 | } |
3002 | ||
19d28fbd | 3003 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
3004 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
3005 | const struct bpf_insn *insn, int idx) | |
3006 | { | |
3007 | int start = idx + insn->imm + 1, subprog; | |
3008 | ||
3009 | subprog = find_subprog(env, start); | |
3010 | if (subprog < 0) { | |
3011 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
3012 | start); | |
3013 | return -EFAULT; | |
3014 | } | |
9c8105bd | 3015 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 3016 | } |
19d28fbd | 3017 | #endif |
1ea47e01 | 3018 | |
51c39bb1 AS |
3019 | int check_ctx_reg(struct bpf_verifier_env *env, |
3020 | const struct bpf_reg_state *reg, int regno) | |
58990d1f DB |
3021 | { |
3022 | /* Access to ctx or passing it to a helper is only allowed in | |
3023 | * its original, unmodified form. | |
3024 | */ | |
3025 | ||
3026 | if (reg->off) { | |
3027 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
3028 | regno, reg->off); | |
3029 | return -EACCES; | |
3030 | } | |
3031 | ||
3032 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3033 | char tn_buf[48]; | |
3034 | ||
3035 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3036 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
3037 | return -EACCES; | |
3038 | } | |
3039 | ||
3040 | return 0; | |
3041 | } | |
3042 | ||
9df1c28b MM |
3043 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
3044 | const struct bpf_reg_state *reg, | |
3045 | int regno, int off, int size) | |
3046 | { | |
3047 | if (off < 0) { | |
3048 | verbose(env, | |
3049 | "R%d invalid tracepoint buffer access: off=%d, size=%d", | |
3050 | regno, off, size); | |
3051 | return -EACCES; | |
3052 | } | |
3053 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3054 | char tn_buf[48]; | |
3055 | ||
3056 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3057 | verbose(env, | |
3058 | "R%d invalid variable buffer offset: off=%d, var_off=%s", | |
3059 | regno, off, tn_buf); | |
3060 | return -EACCES; | |
3061 | } | |
3062 | if (off + size > env->prog->aux->max_tp_access) | |
3063 | env->prog->aux->max_tp_access = off + size; | |
3064 | ||
3065 | return 0; | |
3066 | } | |
3067 | ||
3f50f132 JF |
3068 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
3069 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
3070 | { | |
3071 | reg->var_off = tnum_subreg(reg->var_off); | |
3072 | __reg_assign_32_into_64(reg); | |
3073 | } | |
9df1c28b | 3074 | |
0c17d1d2 JH |
3075 | /* truncate register to smaller size (in bytes) |
3076 | * must be called with size < BPF_REG_SIZE | |
3077 | */ | |
3078 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
3079 | { | |
3080 | u64 mask; | |
3081 | ||
3082 | /* clear high bits in bit representation */ | |
3083 | reg->var_off = tnum_cast(reg->var_off, size); | |
3084 | ||
3085 | /* fix arithmetic bounds */ | |
3086 | mask = ((u64)1 << (size * 8)) - 1; | |
3087 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
3088 | reg->umin_value &= mask; | |
3089 | reg->umax_value &= mask; | |
3090 | } else { | |
3091 | reg->umin_value = 0; | |
3092 | reg->umax_value = mask; | |
3093 | } | |
3094 | reg->smin_value = reg->umin_value; | |
3095 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
3096 | |
3097 | /* If size is smaller than 32bit register the 32bit register | |
3098 | * values are also truncated so we push 64-bit bounds into | |
3099 | * 32-bit bounds. Above were truncated < 32-bits already. | |
3100 | */ | |
3101 | if (size >= 4) | |
3102 | return; | |
3103 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
3104 | } |
3105 | ||
a23740ec AN |
3106 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
3107 | { | |
3108 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; | |
3109 | } | |
3110 | ||
3111 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
3112 | { | |
3113 | void *ptr; | |
3114 | u64 addr; | |
3115 | int err; | |
3116 | ||
3117 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
3118 | if (err) | |
3119 | return err; | |
2dedd7d2 | 3120 | ptr = (void *)(long)addr + off; |
a23740ec AN |
3121 | |
3122 | switch (size) { | |
3123 | case sizeof(u8): | |
3124 | *val = (u64)*(u8 *)ptr; | |
3125 | break; | |
3126 | case sizeof(u16): | |
3127 | *val = (u64)*(u16 *)ptr; | |
3128 | break; | |
3129 | case sizeof(u32): | |
3130 | *val = (u64)*(u32 *)ptr; | |
3131 | break; | |
3132 | case sizeof(u64): | |
3133 | *val = *(u64 *)ptr; | |
3134 | break; | |
3135 | default: | |
3136 | return -EINVAL; | |
3137 | } | |
3138 | return 0; | |
3139 | } | |
3140 | ||
9e15db66 AS |
3141 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
3142 | struct bpf_reg_state *regs, | |
3143 | int regno, int off, int size, | |
3144 | enum bpf_access_type atype, | |
3145 | int value_regno) | |
3146 | { | |
3147 | struct bpf_reg_state *reg = regs + regno; | |
3148 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); | |
3149 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
3150 | u32 btf_id; | |
3151 | int ret; | |
3152 | ||
9e15db66 AS |
3153 | if (off < 0) { |
3154 | verbose(env, | |
3155 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
3156 | regno, tname, off); | |
3157 | return -EACCES; | |
3158 | } | |
3159 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3160 | char tn_buf[48]; | |
3161 | ||
3162 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3163 | verbose(env, | |
3164 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
3165 | regno, tname, off, tn_buf); | |
3166 | return -EACCES; | |
3167 | } | |
3168 | ||
27ae7997 MKL |
3169 | if (env->ops->btf_struct_access) { |
3170 | ret = env->ops->btf_struct_access(&env->log, t, off, size, | |
3171 | atype, &btf_id); | |
3172 | } else { | |
3173 | if (atype != BPF_READ) { | |
3174 | verbose(env, "only read is supported\n"); | |
3175 | return -EACCES; | |
3176 | } | |
3177 | ||
3178 | ret = btf_struct_access(&env->log, t, off, size, atype, | |
3179 | &btf_id); | |
3180 | } | |
3181 | ||
9e15db66 AS |
3182 | if (ret < 0) |
3183 | return ret; | |
3184 | ||
8ff3571f | 3185 | if (atype == BPF_READ && value_regno >= 0) { |
27ae7997 MKL |
3186 | if (ret == SCALAR_VALUE) { |
3187 | mark_reg_unknown(env, regs, value_regno); | |
3188 | return 0; | |
3189 | } | |
3190 | mark_reg_known_zero(env, regs, value_regno); | |
3191 | regs[value_regno].type = PTR_TO_BTF_ID; | |
3192 | regs[value_regno].btf_id = btf_id; | |
9e15db66 | 3193 | } |
27ae7997 | 3194 | |
9e15db66 AS |
3195 | return 0; |
3196 | } | |
3197 | ||
17a52670 AS |
3198 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
3199 | * if t==write, value_regno is a register which value is stored into memory | |
3200 | * if t==read, value_regno is a register which will receive the value from memory | |
3201 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
3202 | * if t==read && value_regno==-1, don't care what we read from memory | |
3203 | */ | |
ca369602 DB |
3204 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
3205 | int off, int bpf_size, enum bpf_access_type t, | |
3206 | int value_regno, bool strict_alignment_once) | |
17a52670 | 3207 | { |
638f5b90 AS |
3208 | struct bpf_reg_state *regs = cur_regs(env); |
3209 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 3210 | struct bpf_func_state *state; |
17a52670 AS |
3211 | int size, err = 0; |
3212 | ||
3213 | size = bpf_size_to_bytes(bpf_size); | |
3214 | if (size < 0) | |
3215 | return size; | |
3216 | ||
f1174f77 | 3217 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 3218 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
3219 | if (err) |
3220 | return err; | |
17a52670 | 3221 | |
f1174f77 EC |
3222 | /* for access checks, reg->off is just part of off */ |
3223 | off += reg->off; | |
3224 | ||
3225 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
3226 | if (t == BPF_WRITE && value_regno >= 0 && |
3227 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3228 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
3229 | return -EACCES; |
3230 | } | |
591fe988 DB |
3231 | err = check_map_access_type(env, regno, off, size, t); |
3232 | if (err) | |
3233 | return err; | |
9fd29c08 | 3234 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
3235 | if (!err && t == BPF_READ && value_regno >= 0) { |
3236 | struct bpf_map *map = reg->map_ptr; | |
3237 | ||
3238 | /* if map is read-only, track its contents as scalars */ | |
3239 | if (tnum_is_const(reg->var_off) && | |
3240 | bpf_map_is_rdonly(map) && | |
3241 | map->ops->map_direct_value_addr) { | |
3242 | int map_off = off + reg->var_off.value; | |
3243 | u64 val = 0; | |
3244 | ||
3245 | err = bpf_map_direct_read(map, map_off, size, | |
3246 | &val); | |
3247 | if (err) | |
3248 | return err; | |
3249 | ||
3250 | regs[value_regno].type = SCALAR_VALUE; | |
3251 | __mark_reg_known(®s[value_regno], val); | |
3252 | } else { | |
3253 | mark_reg_unknown(env, regs, value_regno); | |
3254 | } | |
3255 | } | |
457f4436 AN |
3256 | } else if (reg->type == PTR_TO_MEM) { |
3257 | if (t == BPF_WRITE && value_regno >= 0 && | |
3258 | is_pointer_value(env, value_regno)) { | |
3259 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
3260 | return -EACCES; | |
3261 | } | |
3262 | err = check_mem_region_access(env, regno, off, size, | |
3263 | reg->mem_size, false); | |
3264 | if (!err && t == BPF_READ && value_regno >= 0) | |
3265 | mark_reg_unknown(env, regs, value_regno); | |
1a0dc1ac | 3266 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 3267 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
9e15db66 | 3268 | u32 btf_id = 0; |
19de99f7 | 3269 | |
1be7f75d AS |
3270 | if (t == BPF_WRITE && value_regno >= 0 && |
3271 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3272 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
3273 | return -EACCES; |
3274 | } | |
f1174f77 | 3275 | |
58990d1f DB |
3276 | err = check_ctx_reg(env, reg, regno); |
3277 | if (err < 0) | |
3278 | return err; | |
3279 | ||
9e15db66 AS |
3280 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
3281 | if (err) | |
3282 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 3283 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 3284 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
3285 | * PTR_TO_PACKET[_META,_END]. In the latter |
3286 | * case, we know the offset is zero. | |
f1174f77 | 3287 | */ |
46f8bc92 | 3288 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 3289 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 3290 | } else { |
638f5b90 | 3291 | mark_reg_known_zero(env, regs, |
61bd5218 | 3292 | value_regno); |
46f8bc92 MKL |
3293 | if (reg_type_may_be_null(reg_type)) |
3294 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
3295 | /* A load of ctx field could have different |
3296 | * actual load size with the one encoded in the | |
3297 | * insn. When the dst is PTR, it is for sure not | |
3298 | * a sub-register. | |
3299 | */ | |
3300 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
b121b341 YS |
3301 | if (reg_type == PTR_TO_BTF_ID || |
3302 | reg_type == PTR_TO_BTF_ID_OR_NULL) | |
9e15db66 | 3303 | regs[value_regno].btf_id = btf_id; |
46f8bc92 | 3304 | } |
638f5b90 | 3305 | regs[value_regno].type = reg_type; |
969bf05e | 3306 | } |
17a52670 | 3307 | |
f1174f77 | 3308 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 3309 | off += reg->var_off.value; |
e4298d25 DB |
3310 | err = check_stack_access(env, reg, off, size); |
3311 | if (err) | |
3312 | return err; | |
8726679a | 3313 | |
f4d7e40a AS |
3314 | state = func(env, reg); |
3315 | err = update_stack_depth(env, state, off); | |
3316 | if (err) | |
3317 | return err; | |
8726679a | 3318 | |
638f5b90 | 3319 | if (t == BPF_WRITE) |
61bd5218 | 3320 | err = check_stack_write(env, state, off, size, |
af86ca4e | 3321 | value_regno, insn_idx); |
638f5b90 | 3322 | else |
61bd5218 JK |
3323 | err = check_stack_read(env, state, off, size, |
3324 | value_regno); | |
de8f3a83 | 3325 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 3326 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 3327 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
3328 | return -EACCES; |
3329 | } | |
4acf6c0b BB |
3330 | if (t == BPF_WRITE && value_regno >= 0 && |
3331 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
3332 | verbose(env, "R%d leaks addr into packet\n", |
3333 | value_regno); | |
4acf6c0b BB |
3334 | return -EACCES; |
3335 | } | |
9fd29c08 | 3336 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 3337 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 3338 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
3339 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
3340 | if (t == BPF_WRITE && value_regno >= 0 && | |
3341 | is_pointer_value(env, value_regno)) { | |
3342 | verbose(env, "R%d leaks addr into flow keys\n", | |
3343 | value_regno); | |
3344 | return -EACCES; | |
3345 | } | |
3346 | ||
3347 | err = check_flow_keys_access(env, off, size); | |
3348 | if (!err && t == BPF_READ && value_regno >= 0) | |
3349 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 3350 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 3351 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
3352 | verbose(env, "R%d cannot write into %s\n", |
3353 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
3354 | return -EACCES; |
3355 | } | |
5f456649 | 3356 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
3357 | if (!err && value_regno >= 0) |
3358 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
3359 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
3360 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
3361 | if (!err && t == BPF_READ && value_regno >= 0) | |
3362 | mark_reg_unknown(env, regs, value_regno); | |
9e15db66 AS |
3363 | } else if (reg->type == PTR_TO_BTF_ID) { |
3364 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, | |
3365 | value_regno); | |
17a52670 | 3366 | } else { |
61bd5218 JK |
3367 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
3368 | reg_type_str[reg->type]); | |
17a52670 AS |
3369 | return -EACCES; |
3370 | } | |
969bf05e | 3371 | |
f1174f77 | 3372 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 3373 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 3374 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 3375 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 3376 | } |
17a52670 AS |
3377 | return err; |
3378 | } | |
3379 | ||
31fd8581 | 3380 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 3381 | { |
17a52670 AS |
3382 | int err; |
3383 | ||
3384 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
3385 | insn->imm != 0) { | |
61bd5218 | 3386 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
3387 | return -EINVAL; |
3388 | } | |
3389 | ||
3390 | /* check src1 operand */ | |
dc503a8a | 3391 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3392 | if (err) |
3393 | return err; | |
3394 | ||
3395 | /* check src2 operand */ | |
dc503a8a | 3396 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3397 | if (err) |
3398 | return err; | |
3399 | ||
6bdf6abc | 3400 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 3401 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
3402 | return -EACCES; |
3403 | } | |
3404 | ||
ca369602 | 3405 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 3406 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
3407 | is_flow_key_reg(env, insn->dst_reg) || |
3408 | is_sk_reg(env, insn->dst_reg)) { | |
ca369602 | 3409 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
2a159c6f DB |
3410 | insn->dst_reg, |
3411 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
3412 | return -EACCES; |
3413 | } | |
3414 | ||
17a52670 | 3415 | /* check whether atomic_add can read the memory */ |
31fd8581 | 3416 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3417 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
3418 | if (err) |
3419 | return err; | |
3420 | ||
3421 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 3422 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3423 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
3424 | } |
3425 | ||
2011fccf AI |
3426 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
3427 | int off, int access_size, | |
3428 | bool zero_size_allowed) | |
3429 | { | |
3430 | struct bpf_reg_state *reg = reg_state(env, regno); | |
3431 | ||
3432 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
3433 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
3434 | if (tnum_is_const(reg->var_off)) { | |
3435 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
3436 | regno, off, access_size); | |
3437 | } else { | |
3438 | char tn_buf[48]; | |
3439 | ||
3440 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3441 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
3442 | regno, tn_buf, access_size); | |
3443 | } | |
3444 | return -EACCES; | |
3445 | } | |
3446 | return 0; | |
3447 | } | |
3448 | ||
17a52670 AS |
3449 | /* when register 'regno' is passed into function that will read 'access_size' |
3450 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
3451 | * and all elements of stack are initialized. |
3452 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
3453 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 3454 | */ |
58e2af8b | 3455 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
3456 | int access_size, bool zero_size_allowed, |
3457 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3458 | { |
2a159c6f | 3459 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 3460 | struct bpf_func_state *state = func(env, reg); |
f7cf25b2 | 3461 | int err, min_off, max_off, i, j, slot, spi; |
17a52670 | 3462 | |
914cb781 | 3463 | if (reg->type != PTR_TO_STACK) { |
f1174f77 | 3464 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 3465 | if (zero_size_allowed && access_size == 0 && |
914cb781 | 3466 | register_is_null(reg)) |
8e2fe1d9 DB |
3467 | return 0; |
3468 | ||
61bd5218 | 3469 | verbose(env, "R%d type=%s expected=%s\n", regno, |
914cb781 | 3470 | reg_type_str[reg->type], |
8e2fe1d9 | 3471 | reg_type_str[PTR_TO_STACK]); |
17a52670 | 3472 | return -EACCES; |
8e2fe1d9 | 3473 | } |
17a52670 | 3474 | |
2011fccf AI |
3475 | if (tnum_is_const(reg->var_off)) { |
3476 | min_off = max_off = reg->var_off.value + reg->off; | |
3477 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
3478 | zero_size_allowed); | |
3479 | if (err) | |
3480 | return err; | |
3481 | } else { | |
088ec26d AI |
3482 | /* Variable offset is prohibited for unprivileged mode for |
3483 | * simplicity since it requires corresponding support in | |
3484 | * Spectre masking for stack ALU. | |
3485 | * See also retrieve_ptr_limit(). | |
3486 | */ | |
2c78ee89 | 3487 | if (!env->bypass_spec_v1) { |
088ec26d | 3488 | char tn_buf[48]; |
f1174f77 | 3489 | |
088ec26d AI |
3490 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3491 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
3492 | regno, tn_buf); | |
3493 | return -EACCES; | |
3494 | } | |
f2bcd05e AI |
3495 | /* Only initialized buffer on stack is allowed to be accessed |
3496 | * with variable offset. With uninitialized buffer it's hard to | |
3497 | * guarantee that whole memory is marked as initialized on | |
3498 | * helper return since specific bounds are unknown what may | |
3499 | * cause uninitialized stack leaking. | |
3500 | */ | |
3501 | if (meta && meta->raw_mode) | |
3502 | meta = NULL; | |
3503 | ||
107c26a7 AI |
3504 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
3505 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
3506 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
3507 | regno); | |
3508 | return -EACCES; | |
3509 | } | |
2011fccf | 3510 | min_off = reg->smin_value + reg->off; |
107c26a7 | 3511 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
3512 | err = __check_stack_boundary(env, regno, min_off, access_size, |
3513 | zero_size_allowed); | |
107c26a7 AI |
3514 | if (err) { |
3515 | verbose(env, "R%d min value is outside of stack bound\n", | |
3516 | regno); | |
2011fccf | 3517 | return err; |
107c26a7 | 3518 | } |
2011fccf AI |
3519 | err = __check_stack_boundary(env, regno, max_off, access_size, |
3520 | zero_size_allowed); | |
107c26a7 AI |
3521 | if (err) { |
3522 | verbose(env, "R%d max value is outside of stack bound\n", | |
3523 | regno); | |
2011fccf | 3524 | return err; |
107c26a7 | 3525 | } |
17a52670 AS |
3526 | } |
3527 | ||
435faee1 DB |
3528 | if (meta && meta->raw_mode) { |
3529 | meta->access_size = access_size; | |
3530 | meta->regno = regno; | |
3531 | return 0; | |
3532 | } | |
3533 | ||
2011fccf | 3534 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
3535 | u8 *stype; |
3536 | ||
2011fccf | 3537 | slot = -i - 1; |
638f5b90 | 3538 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
3539 | if (state->allocated_stack <= slot) |
3540 | goto err; | |
3541 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
3542 | if (*stype == STACK_MISC) | |
3543 | goto mark; | |
3544 | if (*stype == STACK_ZERO) { | |
3545 | /* helper can write anything into the stack */ | |
3546 | *stype = STACK_MISC; | |
3547 | goto mark; | |
17a52670 | 3548 | } |
1d68f22b YS |
3549 | |
3550 | if (state->stack[spi].slot_type[0] == STACK_SPILL && | |
3551 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) | |
3552 | goto mark; | |
3553 | ||
f7cf25b2 AS |
3554 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
3555 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { | |
f54c7898 | 3556 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
f7cf25b2 AS |
3557 | for (j = 0; j < BPF_REG_SIZE; j++) |
3558 | state->stack[spi].slot_type[j] = STACK_MISC; | |
3559 | goto mark; | |
3560 | } | |
3561 | ||
cc2b14d5 | 3562 | err: |
2011fccf AI |
3563 | if (tnum_is_const(reg->var_off)) { |
3564 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
3565 | min_off, i - min_off, access_size); | |
3566 | } else { | |
3567 | char tn_buf[48]; | |
3568 | ||
3569 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3570 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
3571 | tn_buf, i - min_off, access_size); | |
3572 | } | |
cc2b14d5 AS |
3573 | return -EACCES; |
3574 | mark: | |
3575 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
3576 | * the whole slot to be marked as 'read' | |
3577 | */ | |
679c782d | 3578 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
3579 | state->stack[spi].spilled_ptr.parent, |
3580 | REG_LIVE_READ64); | |
17a52670 | 3581 | } |
2011fccf | 3582 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
3583 | } |
3584 | ||
06c1c049 GB |
3585 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
3586 | int access_size, bool zero_size_allowed, | |
3587 | struct bpf_call_arg_meta *meta) | |
3588 | { | |
638f5b90 | 3589 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 3590 | |
f1174f77 | 3591 | switch (reg->type) { |
06c1c049 | 3592 | case PTR_TO_PACKET: |
de8f3a83 | 3593 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
3594 | return check_packet_access(env, regno, reg->off, access_size, |
3595 | zero_size_allowed); | |
06c1c049 | 3596 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
3597 | if (check_map_access_type(env, regno, reg->off, access_size, |
3598 | meta && meta->raw_mode ? BPF_WRITE : | |
3599 | BPF_READ)) | |
3600 | return -EACCES; | |
9fd29c08 YS |
3601 | return check_map_access(env, regno, reg->off, access_size, |
3602 | zero_size_allowed); | |
457f4436 AN |
3603 | case PTR_TO_MEM: |
3604 | return check_mem_region_access(env, regno, reg->off, | |
3605 | access_size, reg->mem_size, | |
3606 | zero_size_allowed); | |
f1174f77 | 3607 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
3608 | return check_stack_boundary(env, regno, access_size, |
3609 | zero_size_allowed, meta); | |
3610 | } | |
3611 | } | |
3612 | ||
d83525ca AS |
3613 | /* Implementation details: |
3614 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
3615 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
3616 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
3617 | * value_or_null->value transition, since the verifier only cares about | |
3618 | * the range of access to valid map value pointer and doesn't care about actual | |
3619 | * address of the map element. | |
3620 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
3621 | * reg->id > 0 after value_or_null->value transition. By doing so | |
3622 | * two bpf_map_lookups will be considered two different pointers that | |
3623 | * point to different bpf_spin_locks. | |
3624 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
3625 | * dead-locks. | |
3626 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
3627 | * reg_is_refcounted() logic. The verifier needs to remember only | |
3628 | * one spin_lock instead of array of acquired_refs. | |
3629 | * cur_state->active_spin_lock remembers which map value element got locked | |
3630 | * and clears it after bpf_spin_unlock. | |
3631 | */ | |
3632 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
3633 | bool is_lock) | |
3634 | { | |
3635 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
3636 | struct bpf_verifier_state *cur = env->cur_state; | |
3637 | bool is_const = tnum_is_const(reg->var_off); | |
3638 | struct bpf_map *map = reg->map_ptr; | |
3639 | u64 val = reg->var_off.value; | |
3640 | ||
3641 | if (reg->type != PTR_TO_MAP_VALUE) { | |
3642 | verbose(env, "R%d is not a pointer to map_value\n", regno); | |
3643 | return -EINVAL; | |
3644 | } | |
3645 | if (!is_const) { | |
3646 | verbose(env, | |
3647 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
3648 | regno); | |
3649 | return -EINVAL; | |
3650 | } | |
3651 | if (!map->btf) { | |
3652 | verbose(env, | |
3653 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
3654 | map->name); | |
3655 | return -EINVAL; | |
3656 | } | |
3657 | if (!map_value_has_spin_lock(map)) { | |
3658 | if (map->spin_lock_off == -E2BIG) | |
3659 | verbose(env, | |
3660 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
3661 | map->name); | |
3662 | else if (map->spin_lock_off == -ENOENT) | |
3663 | verbose(env, | |
3664 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
3665 | map->name); | |
3666 | else | |
3667 | verbose(env, | |
3668 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
3669 | map->name); | |
3670 | return -EINVAL; | |
3671 | } | |
3672 | if (map->spin_lock_off != val + reg->off) { | |
3673 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
3674 | val + reg->off); | |
3675 | return -EINVAL; | |
3676 | } | |
3677 | if (is_lock) { | |
3678 | if (cur->active_spin_lock) { | |
3679 | verbose(env, | |
3680 | "Locking two bpf_spin_locks are not allowed\n"); | |
3681 | return -EINVAL; | |
3682 | } | |
3683 | cur->active_spin_lock = reg->id; | |
3684 | } else { | |
3685 | if (!cur->active_spin_lock) { | |
3686 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
3687 | return -EINVAL; | |
3688 | } | |
3689 | if (cur->active_spin_lock != reg->id) { | |
3690 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
3691 | return -EINVAL; | |
3692 | } | |
3693 | cur->active_spin_lock = 0; | |
3694 | } | |
3695 | return 0; | |
3696 | } | |
3697 | ||
90133415 DB |
3698 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
3699 | { | |
3700 | return type == ARG_PTR_TO_MEM || | |
3701 | type == ARG_PTR_TO_MEM_OR_NULL || | |
3702 | type == ARG_PTR_TO_UNINIT_MEM; | |
3703 | } | |
3704 | ||
3705 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
3706 | { | |
3707 | return type == ARG_CONST_SIZE || | |
3708 | type == ARG_CONST_SIZE_OR_ZERO; | |
3709 | } | |
3710 | ||
457f4436 AN |
3711 | static bool arg_type_is_alloc_mem_ptr(enum bpf_arg_type type) |
3712 | { | |
3713 | return type == ARG_PTR_TO_ALLOC_MEM || | |
3714 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; | |
3715 | } | |
3716 | ||
3717 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) | |
3718 | { | |
3719 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; | |
3720 | } | |
3721 | ||
57c3bb72 AI |
3722 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
3723 | { | |
3724 | return type == ARG_PTR_TO_INT || | |
3725 | type == ARG_PTR_TO_LONG; | |
3726 | } | |
3727 | ||
3728 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
3729 | { | |
3730 | if (type == ARG_PTR_TO_INT) | |
3731 | return sizeof(u32); | |
3732 | else if (type == ARG_PTR_TO_LONG) | |
3733 | return sizeof(u64); | |
3734 | ||
3735 | return -EINVAL; | |
3736 | } | |
3737 | ||
58e2af8b | 3738 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
3739 | enum bpf_arg_type arg_type, |
3740 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3741 | { |
638f5b90 | 3742 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 3743 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
3744 | int err = 0; |
3745 | ||
80f1d68c | 3746 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
3747 | return 0; |
3748 | ||
dc503a8a EC |
3749 | err = check_reg_arg(env, regno, SRC_OP); |
3750 | if (err) | |
3751 | return err; | |
17a52670 | 3752 | |
1be7f75d AS |
3753 | if (arg_type == ARG_ANYTHING) { |
3754 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
3755 | verbose(env, "R%d leaks addr into helper function\n", |
3756 | regno); | |
1be7f75d AS |
3757 | return -EACCES; |
3758 | } | |
80f1d68c | 3759 | return 0; |
1be7f75d | 3760 | } |
80f1d68c | 3761 | |
de8f3a83 | 3762 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 3763 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 3764 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
3765 | return -EACCES; |
3766 | } | |
3767 | ||
8e2fe1d9 | 3768 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
2ea864c5 | 3769 | arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3770 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
3771 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
17a52670 | 3772 | expected_type = PTR_TO_STACK; |
6ac99e8f MKL |
3773 | if (register_is_null(reg) && |
3774 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) | |
3775 | /* final test in check_stack_boundary() */; | |
3776 | else if (!type_is_pkt_pointer(type) && | |
3777 | type != PTR_TO_MAP_VALUE && | |
3778 | type != expected_type) | |
6841de8b | 3779 | goto err_type; |
39f19ebb | 3780 | } else if (arg_type == ARG_CONST_SIZE || |
457f4436 AN |
3781 | arg_type == ARG_CONST_SIZE_OR_ZERO || |
3782 | arg_type == ARG_CONST_ALLOC_SIZE_OR_ZERO) { | |
f1174f77 EC |
3783 | expected_type = SCALAR_VALUE; |
3784 | if (type != expected_type) | |
6841de8b | 3785 | goto err_type; |
17a52670 AS |
3786 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
3787 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
3788 | if (type != expected_type) |
3789 | goto err_type; | |
f318903c DB |
3790 | } else if (arg_type == ARG_PTR_TO_CTX || |
3791 | arg_type == ARG_PTR_TO_CTX_OR_NULL) { | |
608cd71a | 3792 | expected_type = PTR_TO_CTX; |
f318903c DB |
3793 | if (!(register_is_null(reg) && |
3794 | arg_type == ARG_PTR_TO_CTX_OR_NULL)) { | |
3795 | if (type != expected_type) | |
3796 | goto err_type; | |
3797 | err = check_ctx_reg(env, reg, regno); | |
3798 | if (err < 0) | |
3799 | return err; | |
3800 | } | |
46f8bc92 MKL |
3801 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
3802 | expected_type = PTR_TO_SOCK_COMMON; | |
3803 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ | |
3804 | if (!type_is_sk_pointer(type)) | |
3805 | goto err_type; | |
1b986589 MKL |
3806 | if (reg->ref_obj_id) { |
3807 | if (meta->ref_obj_id) { | |
3808 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
3809 | regno, reg->ref_obj_id, | |
3810 | meta->ref_obj_id); | |
3811 | return -EFAULT; | |
3812 | } | |
3813 | meta->ref_obj_id = reg->ref_obj_id; | |
fd978bf7 | 3814 | } |
6ac99e8f MKL |
3815 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
3816 | expected_type = PTR_TO_SOCKET; | |
3817 | if (type != expected_type) | |
3818 | goto err_type; | |
a7658e1a AS |
3819 | } else if (arg_type == ARG_PTR_TO_BTF_ID) { |
3820 | expected_type = PTR_TO_BTF_ID; | |
3821 | if (type != expected_type) | |
3822 | goto err_type; | |
3823 | if (reg->btf_id != meta->btf_id) { | |
3824 | verbose(env, "Helper has type %s got %s in R%d\n", | |
3825 | kernel_type_name(meta->btf_id), | |
3826 | kernel_type_name(reg->btf_id), regno); | |
3827 | ||
3828 | return -EACCES; | |
3829 | } | |
3830 | if (!tnum_is_const(reg->var_off) || reg->var_off.value || reg->off) { | |
3831 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", | |
3832 | regno); | |
3833 | return -EACCES; | |
3834 | } | |
d83525ca AS |
3835 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
3836 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
3837 | if (process_spin_lock(env, regno, true)) | |
3838 | return -EACCES; | |
3839 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
3840 | if (process_spin_lock(env, regno, false)) | |
3841 | return -EACCES; | |
3842 | } else { | |
3843 | verbose(env, "verifier internal error\n"); | |
3844 | return -EFAULT; | |
3845 | } | |
90133415 | 3846 | } else if (arg_type_is_mem_ptr(arg_type)) { |
8e2fe1d9 DB |
3847 | expected_type = PTR_TO_STACK; |
3848 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 3849 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
3850 | * happens during stack boundary checking. |
3851 | */ | |
914cb781 | 3852 | if (register_is_null(reg) && |
457f4436 AN |
3853 | (arg_type == ARG_PTR_TO_MEM_OR_NULL || |
3854 | arg_type == ARG_PTR_TO_ALLOC_MEM_OR_NULL)) | |
6841de8b | 3855 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
3856 | else if (!type_is_pkt_pointer(type) && |
3857 | type != PTR_TO_MAP_VALUE && | |
457f4436 | 3858 | type != PTR_TO_MEM && |
f1174f77 | 3859 | type != expected_type) |
6841de8b | 3860 | goto err_type; |
39f19ebb | 3861 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
457f4436 AN |
3862 | } else if (arg_type_is_alloc_mem_ptr(arg_type)) { |
3863 | expected_type = PTR_TO_MEM; | |
3864 | if (register_is_null(reg) && | |
3865 | arg_type == ARG_PTR_TO_ALLOC_MEM_OR_NULL) | |
3866 | /* final test in check_stack_boundary() */; | |
3867 | else if (type != expected_type) | |
3868 | goto err_type; | |
3869 | if (meta->ref_obj_id) { | |
3870 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
3871 | regno, reg->ref_obj_id, | |
3872 | meta->ref_obj_id); | |
3873 | return -EFAULT; | |
3874 | } | |
3875 | meta->ref_obj_id = reg->ref_obj_id; | |
57c3bb72 AI |
3876 | } else if (arg_type_is_int_ptr(arg_type)) { |
3877 | expected_type = PTR_TO_STACK; | |
3878 | if (!type_is_pkt_pointer(type) && | |
3879 | type != PTR_TO_MAP_VALUE && | |
3880 | type != expected_type) | |
3881 | goto err_type; | |
17a52670 | 3882 | } else { |
61bd5218 | 3883 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
3884 | return -EFAULT; |
3885 | } | |
3886 | ||
17a52670 AS |
3887 | if (arg_type == ARG_CONST_MAP_PTR) { |
3888 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 3889 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
3890 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
3891 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
3892 | * check that [key, key + map->key_size) are within | |
3893 | * stack limits and initialized | |
3894 | */ | |
33ff9823 | 3895 | if (!meta->map_ptr) { |
17a52670 AS |
3896 | /* in function declaration map_ptr must come before |
3897 | * map_key, so that it's verified and known before | |
3898 | * we have to check map_key here. Otherwise it means | |
3899 | * that kernel subsystem misconfigured verifier | |
3900 | */ | |
61bd5218 | 3901 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
3902 | return -EACCES; |
3903 | } | |
d71962f3 PC |
3904 | err = check_helper_mem_access(env, regno, |
3905 | meta->map_ptr->key_size, false, | |
3906 | NULL); | |
2ea864c5 | 3907 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3908 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
3909 | !register_is_null(reg)) || | |
2ea864c5 | 3910 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
3911 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
3912 | * check [value, value + map->value_size) validity | |
3913 | */ | |
33ff9823 | 3914 | if (!meta->map_ptr) { |
17a52670 | 3915 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 3916 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
3917 | return -EACCES; |
3918 | } | |
2ea864c5 | 3919 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
3920 | err = check_helper_mem_access(env, regno, |
3921 | meta->map_ptr->value_size, false, | |
2ea864c5 | 3922 | meta); |
90133415 | 3923 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 3924 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 3925 | |
10060503 JF |
3926 | /* This is used to refine r0 return value bounds for helpers |
3927 | * that enforce this value as an upper bound on return values. | |
3928 | * See do_refine_retval_range() for helpers that can refine | |
3929 | * the return value. C type of helper is u32 so we pull register | |
3930 | * bound from umax_value however, if negative verifier errors | |
3931 | * out. Only upper bounds can be learned because retval is an | |
3932 | * int type and negative retvals are allowed. | |
849fa506 | 3933 | */ |
10060503 | 3934 | meta->msize_max_value = reg->umax_value; |
849fa506 | 3935 | |
f1174f77 EC |
3936 | /* The register is SCALAR_VALUE; the access check |
3937 | * happens using its boundaries. | |
06c1c049 | 3938 | */ |
f1174f77 | 3939 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
3940 | /* For unprivileged variable accesses, disable raw |
3941 | * mode so that the program is required to | |
3942 | * initialize all the memory that the helper could | |
3943 | * just partially fill up. | |
3944 | */ | |
3945 | meta = NULL; | |
3946 | ||
b03c9f9f | 3947 | if (reg->smin_value < 0) { |
61bd5218 | 3948 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
3949 | regno); |
3950 | return -EACCES; | |
3951 | } | |
06c1c049 | 3952 | |
b03c9f9f | 3953 | if (reg->umin_value == 0) { |
f1174f77 EC |
3954 | err = check_helper_mem_access(env, regno - 1, 0, |
3955 | zero_size_allowed, | |
3956 | meta); | |
06c1c049 GB |
3957 | if (err) |
3958 | return err; | |
06c1c049 | 3959 | } |
f1174f77 | 3960 | |
b03c9f9f | 3961 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 3962 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
3963 | regno); |
3964 | return -EACCES; | |
3965 | } | |
3966 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 3967 | reg->umax_value, |
f1174f77 | 3968 | zero_size_allowed, meta); |
b5dc0163 AS |
3969 | if (!err) |
3970 | err = mark_chain_precision(env, regno); | |
457f4436 AN |
3971 | } else if (arg_type_is_alloc_size(arg_type)) { |
3972 | if (!tnum_is_const(reg->var_off)) { | |
3973 | verbose(env, "R%d unbounded size, use 'var &= const' or 'if (var < const)'\n", | |
3974 | regno); | |
3975 | return -EACCES; | |
3976 | } | |
3977 | meta->mem_size = reg->var_off.value; | |
57c3bb72 AI |
3978 | } else if (arg_type_is_int_ptr(arg_type)) { |
3979 | int size = int_ptr_type_to_size(arg_type); | |
3980 | ||
3981 | err = check_helper_mem_access(env, regno, size, false, meta); | |
3982 | if (err) | |
3983 | return err; | |
3984 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
3985 | } |
3986 | ||
3987 | return err; | |
6841de8b | 3988 | err_type: |
61bd5218 | 3989 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
3990 | reg_type_str[type], reg_type_str[expected_type]); |
3991 | return -EACCES; | |
17a52670 AS |
3992 | } |
3993 | ||
61bd5218 JK |
3994 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
3995 | struct bpf_map *map, int func_id) | |
35578d79 | 3996 | { |
35578d79 KX |
3997 | if (!map) |
3998 | return 0; | |
3999 | ||
6aff67c8 AS |
4000 | /* We need a two way check, first is from map perspective ... */ |
4001 | switch (map->map_type) { | |
4002 | case BPF_MAP_TYPE_PROG_ARRAY: | |
4003 | if (func_id != BPF_FUNC_tail_call) | |
4004 | goto error; | |
4005 | break; | |
4006 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
4007 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 4008 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 4009 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
4010 | func_id != BPF_FUNC_perf_event_read_value && |
4011 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
4012 | goto error; |
4013 | break; | |
457f4436 AN |
4014 | case BPF_MAP_TYPE_RINGBUF: |
4015 | if (func_id != BPF_FUNC_ringbuf_output && | |
4016 | func_id != BPF_FUNC_ringbuf_reserve && | |
4017 | func_id != BPF_FUNC_ringbuf_submit && | |
4018 | func_id != BPF_FUNC_ringbuf_discard && | |
4019 | func_id != BPF_FUNC_ringbuf_query) | |
4020 | goto error; | |
4021 | break; | |
6aff67c8 AS |
4022 | case BPF_MAP_TYPE_STACK_TRACE: |
4023 | if (func_id != BPF_FUNC_get_stackid) | |
4024 | goto error; | |
4025 | break; | |
4ed8ec52 | 4026 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 4027 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 4028 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
4029 | goto error; |
4030 | break; | |
cd339431 | 4031 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 4032 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
4033 | if (func_id != BPF_FUNC_get_local_storage) |
4034 | goto error; | |
4035 | break; | |
546ac1ff | 4036 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 4037 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
4038 | if (func_id != BPF_FUNC_redirect_map && |
4039 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
4040 | goto error; |
4041 | break; | |
fbfc504a BT |
4042 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
4043 | * appear. | |
4044 | */ | |
6710e112 JDB |
4045 | case BPF_MAP_TYPE_CPUMAP: |
4046 | if (func_id != BPF_FUNC_redirect_map) | |
4047 | goto error; | |
4048 | break; | |
fada7fdc JL |
4049 | case BPF_MAP_TYPE_XSKMAP: |
4050 | if (func_id != BPF_FUNC_redirect_map && | |
4051 | func_id != BPF_FUNC_map_lookup_elem) | |
4052 | goto error; | |
4053 | break; | |
56f668df | 4054 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 4055 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
4056 | if (func_id != BPF_FUNC_map_lookup_elem) |
4057 | goto error; | |
16a43625 | 4058 | break; |
174a79ff JF |
4059 | case BPF_MAP_TYPE_SOCKMAP: |
4060 | if (func_id != BPF_FUNC_sk_redirect_map && | |
4061 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 4062 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 4063 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 JS |
4064 | func_id != BPF_FUNC_sk_select_reuseport && |
4065 | func_id != BPF_FUNC_map_lookup_elem) | |
174a79ff JF |
4066 | goto error; |
4067 | break; | |
81110384 JF |
4068 | case BPF_MAP_TYPE_SOCKHASH: |
4069 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
4070 | func_id != BPF_FUNC_sock_hash_update && | |
4071 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 4072 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 JS |
4073 | func_id != BPF_FUNC_sk_select_reuseport && |
4074 | func_id != BPF_FUNC_map_lookup_elem) | |
81110384 JF |
4075 | goto error; |
4076 | break; | |
2dbb9b9e MKL |
4077 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
4078 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
4079 | goto error; | |
4080 | break; | |
f1a2e44a MV |
4081 | case BPF_MAP_TYPE_QUEUE: |
4082 | case BPF_MAP_TYPE_STACK: | |
4083 | if (func_id != BPF_FUNC_map_peek_elem && | |
4084 | func_id != BPF_FUNC_map_pop_elem && | |
4085 | func_id != BPF_FUNC_map_push_elem) | |
4086 | goto error; | |
4087 | break; | |
6ac99e8f MKL |
4088 | case BPF_MAP_TYPE_SK_STORAGE: |
4089 | if (func_id != BPF_FUNC_sk_storage_get && | |
4090 | func_id != BPF_FUNC_sk_storage_delete) | |
4091 | goto error; | |
4092 | break; | |
6aff67c8 AS |
4093 | default: |
4094 | break; | |
4095 | } | |
4096 | ||
4097 | /* ... and second from the function itself. */ | |
4098 | switch (func_id) { | |
4099 | case BPF_FUNC_tail_call: | |
4100 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
4101 | goto error; | |
f910cefa | 4102 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
4103 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
4104 | return -EINVAL; | |
4105 | } | |
6aff67c8 AS |
4106 | break; |
4107 | case BPF_FUNC_perf_event_read: | |
4108 | case BPF_FUNC_perf_event_output: | |
908432ca | 4109 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 4110 | case BPF_FUNC_skb_output: |
d831ee84 | 4111 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
4112 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
4113 | goto error; | |
4114 | break; | |
4115 | case BPF_FUNC_get_stackid: | |
4116 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
4117 | goto error; | |
4118 | break; | |
60d20f91 | 4119 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 4120 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
4121 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
4122 | goto error; | |
4123 | break; | |
97f91a7c | 4124 | case BPF_FUNC_redirect_map: |
9c270af3 | 4125 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 4126 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
4127 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
4128 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
4129 | goto error; |
4130 | break; | |
174a79ff | 4131 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 4132 | case BPF_FUNC_msg_redirect_map: |
81110384 | 4133 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
4134 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
4135 | goto error; | |
4136 | break; | |
81110384 JF |
4137 | case BPF_FUNC_sk_redirect_hash: |
4138 | case BPF_FUNC_msg_redirect_hash: | |
4139 | case BPF_FUNC_sock_hash_update: | |
4140 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
4141 | goto error; |
4142 | break; | |
cd339431 | 4143 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
4144 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
4145 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
4146 | goto error; |
4147 | break; | |
2dbb9b9e | 4148 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
4149 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
4150 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
4151 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
4152 | goto error; |
4153 | break; | |
f1a2e44a MV |
4154 | case BPF_FUNC_map_peek_elem: |
4155 | case BPF_FUNC_map_pop_elem: | |
4156 | case BPF_FUNC_map_push_elem: | |
4157 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
4158 | map->map_type != BPF_MAP_TYPE_STACK) | |
4159 | goto error; | |
4160 | break; | |
6ac99e8f MKL |
4161 | case BPF_FUNC_sk_storage_get: |
4162 | case BPF_FUNC_sk_storage_delete: | |
4163 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
4164 | goto error; | |
4165 | break; | |
6aff67c8 AS |
4166 | default: |
4167 | break; | |
35578d79 KX |
4168 | } |
4169 | ||
4170 | return 0; | |
6aff67c8 | 4171 | error: |
61bd5218 | 4172 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 4173 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 4174 | return -EINVAL; |
35578d79 KX |
4175 | } |
4176 | ||
90133415 | 4177 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
4178 | { |
4179 | int count = 0; | |
4180 | ||
39f19ebb | 4181 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4182 | count++; |
39f19ebb | 4183 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4184 | count++; |
39f19ebb | 4185 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4186 | count++; |
39f19ebb | 4187 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4188 | count++; |
39f19ebb | 4189 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
4190 | count++; |
4191 | ||
90133415 DB |
4192 | /* We only support one arg being in raw mode at the moment, |
4193 | * which is sufficient for the helper functions we have | |
4194 | * right now. | |
4195 | */ | |
4196 | return count <= 1; | |
4197 | } | |
4198 | ||
4199 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
4200 | enum bpf_arg_type arg_next) | |
4201 | { | |
4202 | return (arg_type_is_mem_ptr(arg_curr) && | |
4203 | !arg_type_is_mem_size(arg_next)) || | |
4204 | (!arg_type_is_mem_ptr(arg_curr) && | |
4205 | arg_type_is_mem_size(arg_next)); | |
4206 | } | |
4207 | ||
4208 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
4209 | { | |
4210 | /* bpf_xxx(..., buf, len) call will access 'len' | |
4211 | * bytes from memory 'buf'. Both arg types need | |
4212 | * to be paired, so make sure there's no buggy | |
4213 | * helper function specification. | |
4214 | */ | |
4215 | if (arg_type_is_mem_size(fn->arg1_type) || | |
4216 | arg_type_is_mem_ptr(fn->arg5_type) || | |
4217 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
4218 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
4219 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
4220 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
4221 | return false; | |
4222 | ||
4223 | return true; | |
4224 | } | |
4225 | ||
1b986589 | 4226 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
4227 | { |
4228 | int count = 0; | |
4229 | ||
1b986589 | 4230 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 4231 | count++; |
1b986589 | 4232 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 4233 | count++; |
1b986589 | 4234 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 4235 | count++; |
1b986589 | 4236 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 4237 | count++; |
1b986589 | 4238 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
4239 | count++; |
4240 | ||
1b986589 MKL |
4241 | /* A reference acquiring function cannot acquire |
4242 | * another refcounted ptr. | |
4243 | */ | |
64d85290 | 4244 | if (may_be_acquire_function(func_id) && count) |
1b986589 MKL |
4245 | return false; |
4246 | ||
fd978bf7 JS |
4247 | /* We only support one arg being unreferenced at the moment, |
4248 | * which is sufficient for the helper functions we have right now. | |
4249 | */ | |
4250 | return count <= 1; | |
4251 | } | |
4252 | ||
1b986589 | 4253 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
4254 | { |
4255 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 4256 | check_arg_pair_ok(fn) && |
1b986589 | 4257 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
4258 | } |
4259 | ||
de8f3a83 DB |
4260 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
4261 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 4262 | */ |
f4d7e40a AS |
4263 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
4264 | struct bpf_func_state *state) | |
969bf05e | 4265 | { |
58e2af8b | 4266 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
4267 | int i; |
4268 | ||
4269 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 4270 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 4271 | mark_reg_unknown(env, regs, i); |
969bf05e | 4272 | |
f3709f69 JS |
4273 | bpf_for_each_spilled_reg(i, state, reg) { |
4274 | if (!reg) | |
969bf05e | 4275 | continue; |
de8f3a83 | 4276 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 4277 | __mark_reg_unknown(env, reg); |
969bf05e AS |
4278 | } |
4279 | } | |
4280 | ||
f4d7e40a AS |
4281 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
4282 | { | |
4283 | struct bpf_verifier_state *vstate = env->cur_state; | |
4284 | int i; | |
4285 | ||
4286 | for (i = 0; i <= vstate->curframe; i++) | |
4287 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
4288 | } | |
4289 | ||
fd978bf7 | 4290 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
4291 | struct bpf_func_state *state, |
4292 | int ref_obj_id) | |
fd978bf7 JS |
4293 | { |
4294 | struct bpf_reg_state *regs = state->regs, *reg; | |
4295 | int i; | |
4296 | ||
4297 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 4298 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
4299 | mark_reg_unknown(env, regs, i); |
4300 | ||
4301 | bpf_for_each_spilled_reg(i, state, reg) { | |
4302 | if (!reg) | |
4303 | continue; | |
1b986589 | 4304 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 4305 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
4306 | } |
4307 | } | |
4308 | ||
4309 | /* The pointer with the specified id has released its reference to kernel | |
4310 | * resources. Identify all copies of the same pointer and clear the reference. | |
4311 | */ | |
4312 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 4313 | int ref_obj_id) |
fd978bf7 JS |
4314 | { |
4315 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 4316 | int err; |
fd978bf7 JS |
4317 | int i; |
4318 | ||
1b986589 MKL |
4319 | err = release_reference_state(cur_func(env), ref_obj_id); |
4320 | if (err) | |
4321 | return err; | |
4322 | ||
fd978bf7 | 4323 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 4324 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 4325 | |
1b986589 | 4326 | return 0; |
fd978bf7 JS |
4327 | } |
4328 | ||
51c39bb1 AS |
4329 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
4330 | struct bpf_reg_state *regs) | |
4331 | { | |
4332 | int i; | |
4333 | ||
4334 | /* after the call registers r0 - r5 were scratched */ | |
4335 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
4336 | mark_reg_not_init(env, regs, caller_saved[i]); | |
4337 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
4338 | } | |
4339 | } | |
4340 | ||
f4d7e40a AS |
4341 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
4342 | int *insn_idx) | |
4343 | { | |
4344 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 4345 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 4346 | struct bpf_func_state *caller, *callee; |
fd978bf7 | 4347 | int i, err, subprog, target_insn; |
51c39bb1 | 4348 | bool is_global = false; |
f4d7e40a | 4349 | |
aada9ce6 | 4350 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 4351 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 4352 | state->curframe + 2); |
f4d7e40a AS |
4353 | return -E2BIG; |
4354 | } | |
4355 | ||
4356 | target_insn = *insn_idx + insn->imm; | |
4357 | subprog = find_subprog(env, target_insn + 1); | |
4358 | if (subprog < 0) { | |
4359 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
4360 | target_insn + 1); | |
4361 | return -EFAULT; | |
4362 | } | |
4363 | ||
4364 | caller = state->frame[state->curframe]; | |
4365 | if (state->frame[state->curframe + 1]) { | |
4366 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
4367 | state->curframe + 1); | |
4368 | return -EFAULT; | |
4369 | } | |
4370 | ||
51c39bb1 AS |
4371 | func_info_aux = env->prog->aux->func_info_aux; |
4372 | if (func_info_aux) | |
4373 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
4374 | err = btf_check_func_arg_match(env, subprog, caller->regs); | |
4375 | if (err == -EFAULT) | |
4376 | return err; | |
4377 | if (is_global) { | |
4378 | if (err) { | |
4379 | verbose(env, "Caller passes invalid args into func#%d\n", | |
4380 | subprog); | |
4381 | return err; | |
4382 | } else { | |
4383 | if (env->log.level & BPF_LOG_LEVEL) | |
4384 | verbose(env, | |
4385 | "Func#%d is global and valid. Skipping.\n", | |
4386 | subprog); | |
4387 | clear_caller_saved_regs(env, caller->regs); | |
4388 | ||
4389 | /* All global functions return SCALAR_VALUE */ | |
4390 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
4391 | ||
4392 | /* continue with next insn after call */ | |
4393 | return 0; | |
4394 | } | |
4395 | } | |
4396 | ||
f4d7e40a AS |
4397 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
4398 | if (!callee) | |
4399 | return -ENOMEM; | |
4400 | state->frame[state->curframe + 1] = callee; | |
4401 | ||
4402 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
4403 | * into its own stack before reading from it. | |
4404 | * callee can read/write into caller's stack | |
4405 | */ | |
4406 | init_func_state(env, callee, | |
4407 | /* remember the callsite, it will be used by bpf_exit */ | |
4408 | *insn_idx /* callsite */, | |
4409 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 4410 | subprog /* subprog number within this prog */); |
f4d7e40a | 4411 | |
fd978bf7 JS |
4412 | /* Transfer references to the callee */ |
4413 | err = transfer_reference_state(callee, caller); | |
4414 | if (err) | |
4415 | return err; | |
4416 | ||
679c782d EC |
4417 | /* copy r1 - r5 args that callee can access. The copy includes parent |
4418 | * pointers, which connects us up to the liveness chain | |
4419 | */ | |
f4d7e40a AS |
4420 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
4421 | callee->regs[i] = caller->regs[i]; | |
4422 | ||
51c39bb1 | 4423 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
4424 | |
4425 | /* only increment it after check_reg_arg() finished */ | |
4426 | state->curframe++; | |
4427 | ||
4428 | /* and go analyze first insn of the callee */ | |
4429 | *insn_idx = target_insn; | |
4430 | ||
06ee7115 | 4431 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4432 | verbose(env, "caller:\n"); |
4433 | print_verifier_state(env, caller); | |
4434 | verbose(env, "callee:\n"); | |
4435 | print_verifier_state(env, callee); | |
4436 | } | |
4437 | return 0; | |
4438 | } | |
4439 | ||
4440 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
4441 | { | |
4442 | struct bpf_verifier_state *state = env->cur_state; | |
4443 | struct bpf_func_state *caller, *callee; | |
4444 | struct bpf_reg_state *r0; | |
fd978bf7 | 4445 | int err; |
f4d7e40a AS |
4446 | |
4447 | callee = state->frame[state->curframe]; | |
4448 | r0 = &callee->regs[BPF_REG_0]; | |
4449 | if (r0->type == PTR_TO_STACK) { | |
4450 | /* technically it's ok to return caller's stack pointer | |
4451 | * (or caller's caller's pointer) back to the caller, | |
4452 | * since these pointers are valid. Only current stack | |
4453 | * pointer will be invalid as soon as function exits, | |
4454 | * but let's be conservative | |
4455 | */ | |
4456 | verbose(env, "cannot return stack pointer to the caller\n"); | |
4457 | return -EINVAL; | |
4458 | } | |
4459 | ||
4460 | state->curframe--; | |
4461 | caller = state->frame[state->curframe]; | |
4462 | /* return to the caller whatever r0 had in the callee */ | |
4463 | caller->regs[BPF_REG_0] = *r0; | |
4464 | ||
fd978bf7 JS |
4465 | /* Transfer references to the caller */ |
4466 | err = transfer_reference_state(caller, callee); | |
4467 | if (err) | |
4468 | return err; | |
4469 | ||
f4d7e40a | 4470 | *insn_idx = callee->callsite + 1; |
06ee7115 | 4471 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4472 | verbose(env, "returning from callee:\n"); |
4473 | print_verifier_state(env, callee); | |
4474 | verbose(env, "to caller at %d:\n", *insn_idx); | |
4475 | print_verifier_state(env, caller); | |
4476 | } | |
4477 | /* clear everything in the callee */ | |
4478 | free_func_state(callee); | |
4479 | state->frame[state->curframe + 1] = NULL; | |
4480 | return 0; | |
4481 | } | |
4482 | ||
849fa506 YS |
4483 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
4484 | int func_id, | |
4485 | struct bpf_call_arg_meta *meta) | |
4486 | { | |
4487 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
4488 | ||
4489 | if (ret_type != RET_INTEGER || | |
4490 | (func_id != BPF_FUNC_get_stack && | |
47cc0ed5 DB |
4491 | func_id != BPF_FUNC_probe_read_str && |
4492 | func_id != BPF_FUNC_probe_read_kernel_str && | |
4493 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
4494 | return; |
4495 | ||
10060503 | 4496 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 4497 | ret_reg->s32_max_value = meta->msize_max_value; |
849fa506 YS |
4498 | __reg_deduce_bounds(ret_reg); |
4499 | __reg_bound_offset(ret_reg); | |
10060503 | 4500 | __update_reg_bounds(ret_reg); |
849fa506 YS |
4501 | } |
4502 | ||
c93552c4 DB |
4503 | static int |
4504 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4505 | int func_id, int insn_idx) | |
4506 | { | |
4507 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 4508 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
4509 | |
4510 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
4511 | func_id != BPF_FUNC_map_lookup_elem && |
4512 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
4513 | func_id != BPF_FUNC_map_delete_elem && |
4514 | func_id != BPF_FUNC_map_push_elem && | |
4515 | func_id != BPF_FUNC_map_pop_elem && | |
4516 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 4517 | return 0; |
09772d92 | 4518 | |
591fe988 | 4519 | if (map == NULL) { |
c93552c4 DB |
4520 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
4521 | return -EINVAL; | |
4522 | } | |
4523 | ||
591fe988 DB |
4524 | /* In case of read-only, some additional restrictions |
4525 | * need to be applied in order to prevent altering the | |
4526 | * state of the map from program side. | |
4527 | */ | |
4528 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
4529 | (func_id == BPF_FUNC_map_delete_elem || | |
4530 | func_id == BPF_FUNC_map_update_elem || | |
4531 | func_id == BPF_FUNC_map_push_elem || | |
4532 | func_id == BPF_FUNC_map_pop_elem)) { | |
4533 | verbose(env, "write into map forbidden\n"); | |
4534 | return -EACCES; | |
4535 | } | |
4536 | ||
d2e4c1e6 | 4537 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 4538 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 4539 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 4540 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 4541 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 4542 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
4543 | return 0; |
4544 | } | |
4545 | ||
d2e4c1e6 DB |
4546 | static int |
4547 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4548 | int func_id, int insn_idx) | |
4549 | { | |
4550 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
4551 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
4552 | struct bpf_map *map = meta->map_ptr; | |
4553 | struct tnum range; | |
4554 | u64 val; | |
cc52d914 | 4555 | int err; |
d2e4c1e6 DB |
4556 | |
4557 | if (func_id != BPF_FUNC_tail_call) | |
4558 | return 0; | |
4559 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
4560 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
4561 | return -EINVAL; | |
4562 | } | |
4563 | ||
4564 | range = tnum_range(0, map->max_entries - 1); | |
4565 | reg = ®s[BPF_REG_3]; | |
4566 | ||
4567 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
4568 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4569 | return 0; | |
4570 | } | |
4571 | ||
cc52d914 DB |
4572 | err = mark_chain_precision(env, BPF_REG_3); |
4573 | if (err) | |
4574 | return err; | |
4575 | ||
d2e4c1e6 DB |
4576 | val = reg->var_off.value; |
4577 | if (bpf_map_key_unseen(aux)) | |
4578 | bpf_map_key_store(aux, val); | |
4579 | else if (!bpf_map_key_poisoned(aux) && | |
4580 | bpf_map_key_immediate(aux) != val) | |
4581 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4582 | return 0; | |
4583 | } | |
4584 | ||
fd978bf7 JS |
4585 | static int check_reference_leak(struct bpf_verifier_env *env) |
4586 | { | |
4587 | struct bpf_func_state *state = cur_func(env); | |
4588 | int i; | |
4589 | ||
4590 | for (i = 0; i < state->acquired_refs; i++) { | |
4591 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
4592 | state->refs[i].id, state->refs[i].insn_idx); | |
4593 | } | |
4594 | return state->acquired_refs ? -EINVAL : 0; | |
4595 | } | |
4596 | ||
f4d7e40a | 4597 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 4598 | { |
17a52670 | 4599 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 4600 | struct bpf_reg_state *regs; |
33ff9823 | 4601 | struct bpf_call_arg_meta meta; |
969bf05e | 4602 | bool changes_data; |
17a52670 AS |
4603 | int i, err; |
4604 | ||
4605 | /* find function prototype */ | |
4606 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
4607 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
4608 | func_id); | |
17a52670 AS |
4609 | return -EINVAL; |
4610 | } | |
4611 | ||
00176a34 | 4612 | if (env->ops->get_func_proto) |
5e43f899 | 4613 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 4614 | if (!fn) { |
61bd5218 JK |
4615 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
4616 | func_id); | |
17a52670 AS |
4617 | return -EINVAL; |
4618 | } | |
4619 | ||
4620 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 4621 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 4622 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
4623 | return -EINVAL; |
4624 | } | |
4625 | ||
04514d13 | 4626 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 4627 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
4628 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
4629 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
4630 | func_id_name(func_id), func_id); | |
4631 | return -EINVAL; | |
4632 | } | |
969bf05e | 4633 | |
33ff9823 | 4634 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 4635 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 4636 | |
1b986589 | 4637 | err = check_func_proto(fn, func_id); |
435faee1 | 4638 | if (err) { |
61bd5218 | 4639 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 4640 | func_id_name(func_id), func_id); |
435faee1 DB |
4641 | return err; |
4642 | } | |
4643 | ||
d83525ca | 4644 | meta.func_id = func_id; |
17a52670 | 4645 | /* check args */ |
a7658e1a | 4646 | for (i = 0; i < 5; i++) { |
9cc31b3a AS |
4647 | err = btf_resolve_helper_id(&env->log, fn, i); |
4648 | if (err > 0) | |
4649 | meta.btf_id = err; | |
a7658e1a AS |
4650 | err = check_func_arg(env, BPF_REG_1 + i, fn->arg_type[i], &meta); |
4651 | if (err) | |
4652 | return err; | |
4653 | } | |
17a52670 | 4654 | |
c93552c4 DB |
4655 | err = record_func_map(env, &meta, func_id, insn_idx); |
4656 | if (err) | |
4657 | return err; | |
4658 | ||
d2e4c1e6 DB |
4659 | err = record_func_key(env, &meta, func_id, insn_idx); |
4660 | if (err) | |
4661 | return err; | |
4662 | ||
435faee1 DB |
4663 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
4664 | * is inferred from register state. | |
4665 | */ | |
4666 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
4667 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
4668 | BPF_WRITE, -1, false); | |
435faee1 DB |
4669 | if (err) |
4670 | return err; | |
4671 | } | |
4672 | ||
fd978bf7 JS |
4673 | if (func_id == BPF_FUNC_tail_call) { |
4674 | err = check_reference_leak(env); | |
4675 | if (err) { | |
4676 | verbose(env, "tail_call would lead to reference leak\n"); | |
4677 | return err; | |
4678 | } | |
4679 | } else if (is_release_function(func_id)) { | |
1b986589 | 4680 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
4681 | if (err) { |
4682 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
4683 | func_id_name(func_id), func_id); | |
fd978bf7 | 4684 | return err; |
46f8bc92 | 4685 | } |
fd978bf7 JS |
4686 | } |
4687 | ||
638f5b90 | 4688 | regs = cur_regs(env); |
cd339431 RG |
4689 | |
4690 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
4691 | * this is required because get_local_storage() can't return an error. | |
4692 | */ | |
4693 | if (func_id == BPF_FUNC_get_local_storage && | |
4694 | !register_is_null(®s[BPF_REG_2])) { | |
4695 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
4696 | return -EINVAL; | |
4697 | } | |
4698 | ||
17a52670 | 4699 | /* reset caller saved regs */ |
dc503a8a | 4700 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 4701 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
4702 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
4703 | } | |
17a52670 | 4704 | |
5327ed3d JW |
4705 | /* helper call returns 64-bit value. */ |
4706 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
4707 | ||
dc503a8a | 4708 | /* update return register (already marked as written above) */ |
17a52670 | 4709 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 4710 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 4711 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
4712 | } else if (fn->ret_type == RET_VOID) { |
4713 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
4714 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
4715 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 4716 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 4717 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
4718 | /* remember map_ptr, so that check_map_access() |
4719 | * can check 'value_size' boundary of memory access | |
4720 | * to map element returned from bpf_map_lookup_elem() | |
4721 | */ | |
33ff9823 | 4722 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
4723 | verbose(env, |
4724 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
4725 | return -EINVAL; |
4726 | } | |
33ff9823 | 4727 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
4728 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
4729 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
4730 | if (map_value_has_spin_lock(meta.map_ptr)) |
4731 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
4732 | } else { |
4733 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
4734 | regs[BPF_REG_0].id = ++env->id_gen; | |
4735 | } | |
c64b7983 JS |
4736 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
4737 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4738 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
0f3adc28 | 4739 | regs[BPF_REG_0].id = ++env->id_gen; |
85a51f8c LB |
4740 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
4741 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4742 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
4743 | regs[BPF_REG_0].id = ++env->id_gen; | |
655a51e5 MKL |
4744 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
4745 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4746 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
4747 | regs[BPF_REG_0].id = ++env->id_gen; | |
457f4436 AN |
4748 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
4749 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4750 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; | |
4751 | regs[BPF_REG_0].id = ++env->id_gen; | |
4752 | regs[BPF_REG_0].mem_size = meta.mem_size; | |
17a52670 | 4753 | } else { |
61bd5218 | 4754 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 4755 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
4756 | return -EINVAL; |
4757 | } | |
04fd61ab | 4758 | |
0f3adc28 | 4759 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
4760 | /* For release_reference() */ |
4761 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 4762 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
4763 | int id = acquire_reference_state(env, insn_idx); |
4764 | ||
4765 | if (id < 0) | |
4766 | return id; | |
4767 | /* For mark_ptr_or_null_reg() */ | |
4768 | regs[BPF_REG_0].id = id; | |
4769 | /* For release_reference() */ | |
4770 | regs[BPF_REG_0].ref_obj_id = id; | |
4771 | } | |
1b986589 | 4772 | |
849fa506 YS |
4773 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
4774 | ||
61bd5218 | 4775 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
4776 | if (err) |
4777 | return err; | |
04fd61ab | 4778 | |
c195651e YS |
4779 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
4780 | const char *err_str; | |
4781 | ||
4782 | #ifdef CONFIG_PERF_EVENTS | |
4783 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
4784 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
4785 | #else | |
4786 | err = -ENOTSUPP; | |
4787 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
4788 | #endif | |
4789 | if (err) { | |
4790 | verbose(env, err_str, func_id_name(func_id), func_id); | |
4791 | return err; | |
4792 | } | |
4793 | ||
4794 | env->prog->has_callchain_buf = true; | |
4795 | } | |
4796 | ||
969bf05e AS |
4797 | if (changes_data) |
4798 | clear_all_pkt_pointers(env); | |
4799 | return 0; | |
4800 | } | |
4801 | ||
b03c9f9f EC |
4802 | static bool signed_add_overflows(s64 a, s64 b) |
4803 | { | |
4804 | /* Do the add in u64, where overflow is well-defined */ | |
4805 | s64 res = (s64)((u64)a + (u64)b); | |
4806 | ||
4807 | if (b < 0) | |
4808 | return res > a; | |
4809 | return res < a; | |
4810 | } | |
4811 | ||
3f50f132 JF |
4812 | static bool signed_add32_overflows(s64 a, s64 b) |
4813 | { | |
4814 | /* Do the add in u32, where overflow is well-defined */ | |
4815 | s32 res = (s32)((u32)a + (u32)b); | |
4816 | ||
4817 | if (b < 0) | |
4818 | return res > a; | |
4819 | return res < a; | |
4820 | } | |
4821 | ||
4822 | static bool signed_sub_overflows(s32 a, s32 b) | |
b03c9f9f EC |
4823 | { |
4824 | /* Do the sub in u64, where overflow is well-defined */ | |
4825 | s64 res = (s64)((u64)a - (u64)b); | |
4826 | ||
4827 | if (b < 0) | |
4828 | return res < a; | |
4829 | return res > a; | |
969bf05e AS |
4830 | } |
4831 | ||
3f50f132 JF |
4832 | static bool signed_sub32_overflows(s32 a, s32 b) |
4833 | { | |
4834 | /* Do the sub in u64, where overflow is well-defined */ | |
4835 | s32 res = (s32)((u32)a - (u32)b); | |
4836 | ||
4837 | if (b < 0) | |
4838 | return res < a; | |
4839 | return res > a; | |
4840 | } | |
4841 | ||
bb7f0f98 AS |
4842 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
4843 | const struct bpf_reg_state *reg, | |
4844 | enum bpf_reg_type type) | |
4845 | { | |
4846 | bool known = tnum_is_const(reg->var_off); | |
4847 | s64 val = reg->var_off.value; | |
4848 | s64 smin = reg->smin_value; | |
4849 | ||
4850 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
4851 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
4852 | reg_type_str[type], val); | |
4853 | return false; | |
4854 | } | |
4855 | ||
4856 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
4857 | verbose(env, "%s pointer offset %d is not allowed\n", | |
4858 | reg_type_str[type], reg->off); | |
4859 | return false; | |
4860 | } | |
4861 | ||
4862 | if (smin == S64_MIN) { | |
4863 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
4864 | reg_type_str[type]); | |
4865 | return false; | |
4866 | } | |
4867 | ||
4868 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
4869 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
4870 | smin, reg_type_str[type]); | |
4871 | return false; | |
4872 | } | |
4873 | ||
4874 | return true; | |
4875 | } | |
4876 | ||
979d63d5 DB |
4877 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
4878 | { | |
4879 | return &env->insn_aux_data[env->insn_idx]; | |
4880 | } | |
4881 | ||
4882 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
4883 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
4884 | { | |
4885 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
4886 | (opcode == BPF_SUB && !off_is_neg); | |
4887 | u32 off; | |
4888 | ||
4889 | switch (ptr_reg->type) { | |
4890 | case PTR_TO_STACK: | |
088ec26d AI |
4891 | /* Indirect variable offset stack access is prohibited in |
4892 | * unprivileged mode so it's not handled here. | |
4893 | */ | |
979d63d5 DB |
4894 | off = ptr_reg->off + ptr_reg->var_off.value; |
4895 | if (mask_to_left) | |
4896 | *ptr_limit = MAX_BPF_STACK + off; | |
4897 | else | |
4898 | *ptr_limit = -off; | |
4899 | return 0; | |
4900 | case PTR_TO_MAP_VALUE: | |
4901 | if (mask_to_left) { | |
4902 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
4903 | } else { | |
4904 | off = ptr_reg->smin_value + ptr_reg->off; | |
4905 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
4906 | } | |
4907 | return 0; | |
4908 | default: | |
4909 | return -EINVAL; | |
4910 | } | |
4911 | } | |
4912 | ||
d3bd7413 DB |
4913 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
4914 | const struct bpf_insn *insn) | |
4915 | { | |
2c78ee89 | 4916 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
4917 | } |
4918 | ||
4919 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
4920 | u32 alu_state, u32 alu_limit) | |
4921 | { | |
4922 | /* If we arrived here from different branches with different | |
4923 | * state or limits to sanitize, then this won't work. | |
4924 | */ | |
4925 | if (aux->alu_state && | |
4926 | (aux->alu_state != alu_state || | |
4927 | aux->alu_limit != alu_limit)) | |
4928 | return -EACCES; | |
4929 | ||
4930 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
4931 | aux->alu_state = alu_state; | |
4932 | aux->alu_limit = alu_limit; | |
4933 | return 0; | |
4934 | } | |
4935 | ||
4936 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
4937 | struct bpf_insn *insn) | |
4938 | { | |
4939 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4940 | ||
4941 | if (can_skip_alu_sanitation(env, insn)) | |
4942 | return 0; | |
4943 | ||
4944 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
4945 | } | |
4946 | ||
979d63d5 DB |
4947 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
4948 | struct bpf_insn *insn, | |
4949 | const struct bpf_reg_state *ptr_reg, | |
4950 | struct bpf_reg_state *dst_reg, | |
4951 | bool off_is_neg) | |
4952 | { | |
4953 | struct bpf_verifier_state *vstate = env->cur_state; | |
4954 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4955 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
4956 | u8 opcode = BPF_OP(insn->code); | |
4957 | u32 alu_state, alu_limit; | |
4958 | struct bpf_reg_state tmp; | |
4959 | bool ret; | |
4960 | ||
d3bd7413 | 4961 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
4962 | return 0; |
4963 | ||
4964 | /* We already marked aux for masking from non-speculative | |
4965 | * paths, thus we got here in the first place. We only care | |
4966 | * to explore bad access from here. | |
4967 | */ | |
4968 | if (vstate->speculative) | |
4969 | goto do_sim; | |
4970 | ||
4971 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
4972 | alu_state |= ptr_is_dst_reg ? | |
4973 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
4974 | ||
4975 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
4976 | return 0; | |
d3bd7413 | 4977 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 4978 | return -EACCES; |
979d63d5 DB |
4979 | do_sim: |
4980 | /* Simulate and find potential out-of-bounds access under | |
4981 | * speculative execution from truncation as a result of | |
4982 | * masking when off was not within expected range. If off | |
4983 | * sits in dst, then we temporarily need to move ptr there | |
4984 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
4985 | * for cases where we use K-based arithmetic in one direction | |
4986 | * and truncated reg-based in the other in order to explore | |
4987 | * bad access. | |
4988 | */ | |
4989 | if (!ptr_is_dst_reg) { | |
4990 | tmp = *dst_reg; | |
4991 | *dst_reg = *ptr_reg; | |
4992 | } | |
4993 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 4994 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
4995 | *dst_reg = tmp; |
4996 | return !ret ? -EFAULT : 0; | |
4997 | } | |
4998 | ||
f1174f77 | 4999 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
5000 | * Caller should also handle BPF_MOV case separately. |
5001 | * If we return -EACCES, caller may want to try again treating pointer as a | |
5002 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
5003 | */ | |
5004 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
5005 | struct bpf_insn *insn, | |
5006 | const struct bpf_reg_state *ptr_reg, | |
5007 | const struct bpf_reg_state *off_reg) | |
969bf05e | 5008 | { |
f4d7e40a AS |
5009 | struct bpf_verifier_state *vstate = env->cur_state; |
5010 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5011 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 5012 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
5013 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
5014 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
5015 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
5016 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 5017 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 5018 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 5019 | int ret; |
969bf05e | 5020 | |
f1174f77 | 5021 | dst_reg = ®s[dst]; |
969bf05e | 5022 | |
6f16101e DB |
5023 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
5024 | smin_val > smax_val || umin_val > umax_val) { | |
5025 | /* Taint dst register if offset had invalid bounds derived from | |
5026 | * e.g. dead branches. | |
5027 | */ | |
f54c7898 | 5028 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 5029 | return 0; |
f1174f77 EC |
5030 | } |
5031 | ||
5032 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
5033 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
5034 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
5035 | __mark_reg_unknown(env, dst_reg); | |
5036 | return 0; | |
5037 | } | |
5038 | ||
82abbf8d AS |
5039 | verbose(env, |
5040 | "R%d 32-bit pointer arithmetic prohibited\n", | |
5041 | dst); | |
f1174f77 | 5042 | return -EACCES; |
969bf05e AS |
5043 | } |
5044 | ||
aad2eeaf JS |
5045 | switch (ptr_reg->type) { |
5046 | case PTR_TO_MAP_VALUE_OR_NULL: | |
5047 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
5048 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5049 | return -EACCES; |
aad2eeaf JS |
5050 | case CONST_PTR_TO_MAP: |
5051 | case PTR_TO_PACKET_END: | |
c64b7983 JS |
5052 | case PTR_TO_SOCKET: |
5053 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
5054 | case PTR_TO_SOCK_COMMON: |
5055 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
5056 | case PTR_TO_TCP_SOCK: |
5057 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 5058 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
5059 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
5060 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5061 | return -EACCES; |
9d7eceed DB |
5062 | case PTR_TO_MAP_VALUE: |
5063 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
5064 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
5065 | off_reg == dst_reg ? dst : src); | |
5066 | return -EACCES; | |
5067 | } | |
5068 | /* fall-through */ | |
aad2eeaf JS |
5069 | default: |
5070 | break; | |
f1174f77 EC |
5071 | } |
5072 | ||
5073 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
5074 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 5075 | */ |
f1174f77 EC |
5076 | dst_reg->type = ptr_reg->type; |
5077 | dst_reg->id = ptr_reg->id; | |
969bf05e | 5078 | |
bb7f0f98 AS |
5079 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
5080 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
5081 | return -EINVAL; | |
5082 | ||
3f50f132 JF |
5083 | /* pointer types do not carry 32-bit bounds at the moment. */ |
5084 | __mark_reg32_unbounded(dst_reg); | |
5085 | ||
f1174f77 EC |
5086 | switch (opcode) { |
5087 | case BPF_ADD: | |
979d63d5 DB |
5088 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5089 | if (ret < 0) { | |
5090 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
5091 | return ret; | |
5092 | } | |
f1174f77 EC |
5093 | /* We can take a fixed offset as long as it doesn't overflow |
5094 | * the s32 'off' field | |
969bf05e | 5095 | */ |
b03c9f9f EC |
5096 | if (known && (ptr_reg->off + smin_val == |
5097 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 5098 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
5099 | dst_reg->smin_value = smin_ptr; |
5100 | dst_reg->smax_value = smax_ptr; | |
5101 | dst_reg->umin_value = umin_ptr; | |
5102 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 5103 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 5104 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 5105 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5106 | break; |
5107 | } | |
f1174f77 EC |
5108 | /* A new variable offset is created. Note that off_reg->off |
5109 | * == 0, since it's a scalar. | |
5110 | * dst_reg gets the pointer type and since some positive | |
5111 | * integer value was added to the pointer, give it a new 'id' | |
5112 | * if it's a PTR_TO_PACKET. | |
5113 | * this creates a new 'base' pointer, off_reg (variable) gets | |
5114 | * added into the variable offset, and we copy the fixed offset | |
5115 | * from ptr_reg. | |
969bf05e | 5116 | */ |
b03c9f9f EC |
5117 | if (signed_add_overflows(smin_ptr, smin_val) || |
5118 | signed_add_overflows(smax_ptr, smax_val)) { | |
5119 | dst_reg->smin_value = S64_MIN; | |
5120 | dst_reg->smax_value = S64_MAX; | |
5121 | } else { | |
5122 | dst_reg->smin_value = smin_ptr + smin_val; | |
5123 | dst_reg->smax_value = smax_ptr + smax_val; | |
5124 | } | |
5125 | if (umin_ptr + umin_val < umin_ptr || | |
5126 | umax_ptr + umax_val < umax_ptr) { | |
5127 | dst_reg->umin_value = 0; | |
5128 | dst_reg->umax_value = U64_MAX; | |
5129 | } else { | |
5130 | dst_reg->umin_value = umin_ptr + umin_val; | |
5131 | dst_reg->umax_value = umax_ptr + umax_val; | |
5132 | } | |
f1174f77 EC |
5133 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
5134 | dst_reg->off = ptr_reg->off; | |
0962590e | 5135 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5136 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5137 | dst_reg->id = ++env->id_gen; |
5138 | /* something was added to pkt_ptr, set range to zero */ | |
0962590e | 5139 | dst_reg->raw = 0; |
f1174f77 EC |
5140 | } |
5141 | break; | |
5142 | case BPF_SUB: | |
979d63d5 DB |
5143 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5144 | if (ret < 0) { | |
5145 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
5146 | return ret; | |
5147 | } | |
f1174f77 EC |
5148 | if (dst_reg == off_reg) { |
5149 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
5150 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
5151 | dst); | |
f1174f77 EC |
5152 | return -EACCES; |
5153 | } | |
5154 | /* We don't allow subtraction from FP, because (according to | |
5155 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
5156 | * be able to deal with it. | |
969bf05e | 5157 | */ |
f1174f77 | 5158 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
5159 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
5160 | dst); | |
f1174f77 EC |
5161 | return -EACCES; |
5162 | } | |
b03c9f9f EC |
5163 | if (known && (ptr_reg->off - smin_val == |
5164 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 5165 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
5166 | dst_reg->smin_value = smin_ptr; |
5167 | dst_reg->smax_value = smax_ptr; | |
5168 | dst_reg->umin_value = umin_ptr; | |
5169 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
5170 | dst_reg->var_off = ptr_reg->var_off; |
5171 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 5172 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 5173 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5174 | break; |
5175 | } | |
f1174f77 EC |
5176 | /* A new variable offset is created. If the subtrahend is known |
5177 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 5178 | */ |
b03c9f9f EC |
5179 | if (signed_sub_overflows(smin_ptr, smax_val) || |
5180 | signed_sub_overflows(smax_ptr, smin_val)) { | |
5181 | /* Overflow possible, we know nothing */ | |
5182 | dst_reg->smin_value = S64_MIN; | |
5183 | dst_reg->smax_value = S64_MAX; | |
5184 | } else { | |
5185 | dst_reg->smin_value = smin_ptr - smax_val; | |
5186 | dst_reg->smax_value = smax_ptr - smin_val; | |
5187 | } | |
5188 | if (umin_ptr < umax_val) { | |
5189 | /* Overflow possible, we know nothing */ | |
5190 | dst_reg->umin_value = 0; | |
5191 | dst_reg->umax_value = U64_MAX; | |
5192 | } else { | |
5193 | /* Cannot overflow (as long as bounds are consistent) */ | |
5194 | dst_reg->umin_value = umin_ptr - umax_val; | |
5195 | dst_reg->umax_value = umax_ptr - umin_val; | |
5196 | } | |
f1174f77 EC |
5197 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
5198 | dst_reg->off = ptr_reg->off; | |
0962590e | 5199 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5200 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5201 | dst_reg->id = ++env->id_gen; |
5202 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 5203 | if (smin_val < 0) |
0962590e | 5204 | dst_reg->raw = 0; |
43188702 | 5205 | } |
f1174f77 EC |
5206 | break; |
5207 | case BPF_AND: | |
5208 | case BPF_OR: | |
5209 | case BPF_XOR: | |
82abbf8d AS |
5210 | /* bitwise ops on pointers are troublesome, prohibit. */ |
5211 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
5212 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
5213 | return -EACCES; |
5214 | default: | |
5215 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
5216 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
5217 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 5218 | return -EACCES; |
43188702 JF |
5219 | } |
5220 | ||
bb7f0f98 AS |
5221 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
5222 | return -EINVAL; | |
5223 | ||
b03c9f9f EC |
5224 | __update_reg_bounds(dst_reg); |
5225 | __reg_deduce_bounds(dst_reg); | |
5226 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
5227 | |
5228 | /* For unprivileged we require that resulting offset must be in bounds | |
5229 | * in order to be able to sanitize access later on. | |
5230 | */ | |
2c78ee89 | 5231 | if (!env->bypass_spec_v1) { |
e4298d25 DB |
5232 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
5233 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
5234 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
5235 | "prohibited for !root\n", dst); | |
5236 | return -EACCES; | |
5237 | } else if (dst_reg->type == PTR_TO_STACK && | |
5238 | check_stack_access(env, dst_reg, dst_reg->off + | |
5239 | dst_reg->var_off.value, 1)) { | |
5240 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
5241 | "prohibited for !root\n", dst); | |
5242 | return -EACCES; | |
5243 | } | |
0d6303db DB |
5244 | } |
5245 | ||
43188702 JF |
5246 | return 0; |
5247 | } | |
5248 | ||
3f50f132 JF |
5249 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
5250 | struct bpf_reg_state *src_reg) | |
5251 | { | |
5252 | s32 smin_val = src_reg->s32_min_value; | |
5253 | s32 smax_val = src_reg->s32_max_value; | |
5254 | u32 umin_val = src_reg->u32_min_value; | |
5255 | u32 umax_val = src_reg->u32_max_value; | |
5256 | ||
5257 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
5258 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
5259 | dst_reg->s32_min_value = S32_MIN; | |
5260 | dst_reg->s32_max_value = S32_MAX; | |
5261 | } else { | |
5262 | dst_reg->s32_min_value += smin_val; | |
5263 | dst_reg->s32_max_value += smax_val; | |
5264 | } | |
5265 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
5266 | dst_reg->u32_max_value + umax_val < umax_val) { | |
5267 | dst_reg->u32_min_value = 0; | |
5268 | dst_reg->u32_max_value = U32_MAX; | |
5269 | } else { | |
5270 | dst_reg->u32_min_value += umin_val; | |
5271 | dst_reg->u32_max_value += umax_val; | |
5272 | } | |
5273 | } | |
5274 | ||
07cd2631 JF |
5275 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
5276 | struct bpf_reg_state *src_reg) | |
5277 | { | |
5278 | s64 smin_val = src_reg->smin_value; | |
5279 | s64 smax_val = src_reg->smax_value; | |
5280 | u64 umin_val = src_reg->umin_value; | |
5281 | u64 umax_val = src_reg->umax_value; | |
5282 | ||
5283 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
5284 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
5285 | dst_reg->smin_value = S64_MIN; | |
5286 | dst_reg->smax_value = S64_MAX; | |
5287 | } else { | |
5288 | dst_reg->smin_value += smin_val; | |
5289 | dst_reg->smax_value += smax_val; | |
5290 | } | |
5291 | if (dst_reg->umin_value + umin_val < umin_val || | |
5292 | dst_reg->umax_value + umax_val < umax_val) { | |
5293 | dst_reg->umin_value = 0; | |
5294 | dst_reg->umax_value = U64_MAX; | |
5295 | } else { | |
5296 | dst_reg->umin_value += umin_val; | |
5297 | dst_reg->umax_value += umax_val; | |
5298 | } | |
3f50f132 JF |
5299 | } |
5300 | ||
5301 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
5302 | struct bpf_reg_state *src_reg) | |
5303 | { | |
5304 | s32 smin_val = src_reg->s32_min_value; | |
5305 | s32 smax_val = src_reg->s32_max_value; | |
5306 | u32 umin_val = src_reg->u32_min_value; | |
5307 | u32 umax_val = src_reg->u32_max_value; | |
5308 | ||
5309 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
5310 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
5311 | /* Overflow possible, we know nothing */ | |
5312 | dst_reg->s32_min_value = S32_MIN; | |
5313 | dst_reg->s32_max_value = S32_MAX; | |
5314 | } else { | |
5315 | dst_reg->s32_min_value -= smax_val; | |
5316 | dst_reg->s32_max_value -= smin_val; | |
5317 | } | |
5318 | if (dst_reg->u32_min_value < umax_val) { | |
5319 | /* Overflow possible, we know nothing */ | |
5320 | dst_reg->u32_min_value = 0; | |
5321 | dst_reg->u32_max_value = U32_MAX; | |
5322 | } else { | |
5323 | /* Cannot overflow (as long as bounds are consistent) */ | |
5324 | dst_reg->u32_min_value -= umax_val; | |
5325 | dst_reg->u32_max_value -= umin_val; | |
5326 | } | |
07cd2631 JF |
5327 | } |
5328 | ||
5329 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
5330 | struct bpf_reg_state *src_reg) | |
5331 | { | |
5332 | s64 smin_val = src_reg->smin_value; | |
5333 | s64 smax_val = src_reg->smax_value; | |
5334 | u64 umin_val = src_reg->umin_value; | |
5335 | u64 umax_val = src_reg->umax_value; | |
5336 | ||
5337 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
5338 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
5339 | /* Overflow possible, we know nothing */ | |
5340 | dst_reg->smin_value = S64_MIN; | |
5341 | dst_reg->smax_value = S64_MAX; | |
5342 | } else { | |
5343 | dst_reg->smin_value -= smax_val; | |
5344 | dst_reg->smax_value -= smin_val; | |
5345 | } | |
5346 | if (dst_reg->umin_value < umax_val) { | |
5347 | /* Overflow possible, we know nothing */ | |
5348 | dst_reg->umin_value = 0; | |
5349 | dst_reg->umax_value = U64_MAX; | |
5350 | } else { | |
5351 | /* Cannot overflow (as long as bounds are consistent) */ | |
5352 | dst_reg->umin_value -= umax_val; | |
5353 | dst_reg->umax_value -= umin_val; | |
5354 | } | |
3f50f132 JF |
5355 | } |
5356 | ||
5357 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
5358 | struct bpf_reg_state *src_reg) | |
5359 | { | |
5360 | s32 smin_val = src_reg->s32_min_value; | |
5361 | u32 umin_val = src_reg->u32_min_value; | |
5362 | u32 umax_val = src_reg->u32_max_value; | |
5363 | ||
5364 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
5365 | /* Ain't nobody got time to multiply that sign */ | |
5366 | __mark_reg32_unbounded(dst_reg); | |
5367 | return; | |
5368 | } | |
5369 | /* Both values are positive, so we can work with unsigned and | |
5370 | * copy the result to signed (unless it exceeds S32_MAX). | |
5371 | */ | |
5372 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
5373 | /* Potential overflow, we know nothing */ | |
5374 | __mark_reg32_unbounded(dst_reg); | |
5375 | return; | |
5376 | } | |
5377 | dst_reg->u32_min_value *= umin_val; | |
5378 | dst_reg->u32_max_value *= umax_val; | |
5379 | if (dst_reg->u32_max_value > S32_MAX) { | |
5380 | /* Overflow possible, we know nothing */ | |
5381 | dst_reg->s32_min_value = S32_MIN; | |
5382 | dst_reg->s32_max_value = S32_MAX; | |
5383 | } else { | |
5384 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5385 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5386 | } | |
07cd2631 JF |
5387 | } |
5388 | ||
5389 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
5390 | struct bpf_reg_state *src_reg) | |
5391 | { | |
5392 | s64 smin_val = src_reg->smin_value; | |
5393 | u64 umin_val = src_reg->umin_value; | |
5394 | u64 umax_val = src_reg->umax_value; | |
5395 | ||
07cd2631 JF |
5396 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
5397 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 5398 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5399 | return; |
5400 | } | |
5401 | /* Both values are positive, so we can work with unsigned and | |
5402 | * copy the result to signed (unless it exceeds S64_MAX). | |
5403 | */ | |
5404 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
5405 | /* Potential overflow, we know nothing */ | |
3f50f132 | 5406 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5407 | return; |
5408 | } | |
5409 | dst_reg->umin_value *= umin_val; | |
5410 | dst_reg->umax_value *= umax_val; | |
5411 | if (dst_reg->umax_value > S64_MAX) { | |
5412 | /* Overflow possible, we know nothing */ | |
5413 | dst_reg->smin_value = S64_MIN; | |
5414 | dst_reg->smax_value = S64_MAX; | |
5415 | } else { | |
5416 | dst_reg->smin_value = dst_reg->umin_value; | |
5417 | dst_reg->smax_value = dst_reg->umax_value; | |
5418 | } | |
5419 | } | |
5420 | ||
3f50f132 JF |
5421 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
5422 | struct bpf_reg_state *src_reg) | |
5423 | { | |
5424 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5425 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5426 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5427 | s32 smin_val = src_reg->s32_min_value; | |
5428 | u32 umax_val = src_reg->u32_max_value; | |
5429 | ||
5430 | /* Assuming scalar64_min_max_and will be called so its safe | |
5431 | * to skip updating register for known 32-bit case. | |
5432 | */ | |
5433 | if (src_known && dst_known) | |
5434 | return; | |
5435 | ||
5436 | /* We get our minimum from the var_off, since that's inherently | |
5437 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5438 | */ | |
5439 | dst_reg->u32_min_value = var32_off.value; | |
5440 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
5441 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
5442 | /* Lose signed bounds when ANDing negative numbers, | |
5443 | * ain't nobody got time for that. | |
5444 | */ | |
5445 | dst_reg->s32_min_value = S32_MIN; | |
5446 | dst_reg->s32_max_value = S32_MAX; | |
5447 | } else { | |
5448 | /* ANDing two positives gives a positive, so safe to | |
5449 | * cast result into s64. | |
5450 | */ | |
5451 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5452 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5453 | } | |
5454 | ||
5455 | } | |
5456 | ||
07cd2631 JF |
5457 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
5458 | struct bpf_reg_state *src_reg) | |
5459 | { | |
3f50f132 JF |
5460 | bool src_known = tnum_is_const(src_reg->var_off); |
5461 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
5462 | s64 smin_val = src_reg->smin_value; |
5463 | u64 umax_val = src_reg->umax_value; | |
5464 | ||
3f50f132 JF |
5465 | if (src_known && dst_known) { |
5466 | __mark_reg_known(dst_reg, dst_reg->var_off.value & | |
5467 | src_reg->var_off.value); | |
5468 | return; | |
5469 | } | |
5470 | ||
07cd2631 JF |
5471 | /* We get our minimum from the var_off, since that's inherently |
5472 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5473 | */ | |
07cd2631 JF |
5474 | dst_reg->umin_value = dst_reg->var_off.value; |
5475 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
5476 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
5477 | /* Lose signed bounds when ANDing negative numbers, | |
5478 | * ain't nobody got time for that. | |
5479 | */ | |
5480 | dst_reg->smin_value = S64_MIN; | |
5481 | dst_reg->smax_value = S64_MAX; | |
5482 | } else { | |
5483 | /* ANDing two positives gives a positive, so safe to | |
5484 | * cast result into s64. | |
5485 | */ | |
5486 | dst_reg->smin_value = dst_reg->umin_value; | |
5487 | dst_reg->smax_value = dst_reg->umax_value; | |
5488 | } | |
5489 | /* We may learn something more from the var_off */ | |
5490 | __update_reg_bounds(dst_reg); | |
5491 | } | |
5492 | ||
3f50f132 JF |
5493 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
5494 | struct bpf_reg_state *src_reg) | |
5495 | { | |
5496 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5497 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5498 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5499 | s32 smin_val = src_reg->smin_value; | |
5500 | u32 umin_val = src_reg->umin_value; | |
5501 | ||
5502 | /* Assuming scalar64_min_max_or will be called so it is safe | |
5503 | * to skip updating register for known case. | |
5504 | */ | |
5505 | if (src_known && dst_known) | |
5506 | return; | |
5507 | ||
5508 | /* We get our maximum from the var_off, and our minimum is the | |
5509 | * maximum of the operands' minima | |
5510 | */ | |
5511 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
5512 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
5513 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
5514 | /* Lose signed bounds when ORing negative numbers, | |
5515 | * ain't nobody got time for that. | |
5516 | */ | |
5517 | dst_reg->s32_min_value = S32_MIN; | |
5518 | dst_reg->s32_max_value = S32_MAX; | |
5519 | } else { | |
5520 | /* ORing two positives gives a positive, so safe to | |
5521 | * cast result into s64. | |
5522 | */ | |
5523 | dst_reg->s32_min_value = dst_reg->umin_value; | |
5524 | dst_reg->s32_max_value = dst_reg->umax_value; | |
5525 | } | |
5526 | } | |
5527 | ||
07cd2631 JF |
5528 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
5529 | struct bpf_reg_state *src_reg) | |
5530 | { | |
3f50f132 JF |
5531 | bool src_known = tnum_is_const(src_reg->var_off); |
5532 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
5533 | s64 smin_val = src_reg->smin_value; |
5534 | u64 umin_val = src_reg->umin_value; | |
5535 | ||
3f50f132 JF |
5536 | if (src_known && dst_known) { |
5537 | __mark_reg_known(dst_reg, dst_reg->var_off.value | | |
5538 | src_reg->var_off.value); | |
5539 | return; | |
5540 | } | |
5541 | ||
07cd2631 JF |
5542 | /* We get our maximum from the var_off, and our minimum is the |
5543 | * maximum of the operands' minima | |
5544 | */ | |
07cd2631 JF |
5545 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
5546 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
5547 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
5548 | /* Lose signed bounds when ORing negative numbers, | |
5549 | * ain't nobody got time for that. | |
5550 | */ | |
5551 | dst_reg->smin_value = S64_MIN; | |
5552 | dst_reg->smax_value = S64_MAX; | |
5553 | } else { | |
5554 | /* ORing two positives gives a positive, so safe to | |
5555 | * cast result into s64. | |
5556 | */ | |
5557 | dst_reg->smin_value = dst_reg->umin_value; | |
5558 | dst_reg->smax_value = dst_reg->umax_value; | |
5559 | } | |
5560 | /* We may learn something more from the var_off */ | |
5561 | __update_reg_bounds(dst_reg); | |
5562 | } | |
5563 | ||
3f50f132 JF |
5564 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
5565 | u64 umin_val, u64 umax_val) | |
07cd2631 | 5566 | { |
07cd2631 JF |
5567 | /* We lose all sign bit information (except what we can pick |
5568 | * up from var_off) | |
5569 | */ | |
3f50f132 JF |
5570 | dst_reg->s32_min_value = S32_MIN; |
5571 | dst_reg->s32_max_value = S32_MAX; | |
5572 | /* If we might shift our top bit out, then we know nothing */ | |
5573 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
5574 | dst_reg->u32_min_value = 0; | |
5575 | dst_reg->u32_max_value = U32_MAX; | |
5576 | } else { | |
5577 | dst_reg->u32_min_value <<= umin_val; | |
5578 | dst_reg->u32_max_value <<= umax_val; | |
5579 | } | |
5580 | } | |
5581 | ||
5582 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
5583 | struct bpf_reg_state *src_reg) | |
5584 | { | |
5585 | u32 umax_val = src_reg->u32_max_value; | |
5586 | u32 umin_val = src_reg->u32_min_value; | |
5587 | /* u32 alu operation will zext upper bits */ | |
5588 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
5589 | ||
5590 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
5591 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
5592 | /* Not required but being careful mark reg64 bounds as unknown so | |
5593 | * that we are forced to pick them up from tnum and zext later and | |
5594 | * if some path skips this step we are still safe. | |
5595 | */ | |
5596 | __mark_reg64_unbounded(dst_reg); | |
5597 | __update_reg32_bounds(dst_reg); | |
5598 | } | |
5599 | ||
5600 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
5601 | u64 umin_val, u64 umax_val) | |
5602 | { | |
5603 | /* Special case <<32 because it is a common compiler pattern to sign | |
5604 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
5605 | * positive we know this shift will also be positive so we can track | |
5606 | * bounds correctly. Otherwise we lose all sign bit information except | |
5607 | * what we can pick up from var_off. Perhaps we can generalize this | |
5608 | * later to shifts of any length. | |
5609 | */ | |
5610 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
5611 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
5612 | else | |
5613 | dst_reg->smax_value = S64_MAX; | |
5614 | ||
5615 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
5616 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
5617 | else | |
5618 | dst_reg->smin_value = S64_MIN; | |
5619 | ||
07cd2631 JF |
5620 | /* If we might shift our top bit out, then we know nothing */ |
5621 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
5622 | dst_reg->umin_value = 0; | |
5623 | dst_reg->umax_value = U64_MAX; | |
5624 | } else { | |
5625 | dst_reg->umin_value <<= umin_val; | |
5626 | dst_reg->umax_value <<= umax_val; | |
5627 | } | |
3f50f132 JF |
5628 | } |
5629 | ||
5630 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
5631 | struct bpf_reg_state *src_reg) | |
5632 | { | |
5633 | u64 umax_val = src_reg->umax_value; | |
5634 | u64 umin_val = src_reg->umin_value; | |
5635 | ||
5636 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
5637 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
5638 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
5639 | ||
07cd2631 JF |
5640 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
5641 | /* We may learn something more from the var_off */ | |
5642 | __update_reg_bounds(dst_reg); | |
5643 | } | |
5644 | ||
3f50f132 JF |
5645 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
5646 | struct bpf_reg_state *src_reg) | |
5647 | { | |
5648 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
5649 | u32 umax_val = src_reg->u32_max_value; | |
5650 | u32 umin_val = src_reg->u32_min_value; | |
5651 | ||
5652 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
5653 | * be negative, then either: | |
5654 | * 1) src_reg might be zero, so the sign bit of the result is | |
5655 | * unknown, so we lose our signed bounds | |
5656 | * 2) it's known negative, thus the unsigned bounds capture the | |
5657 | * signed bounds | |
5658 | * 3) the signed bounds cross zero, so they tell us nothing | |
5659 | * about the result | |
5660 | * If the value in dst_reg is known nonnegative, then again the | |
5661 | * unsigned bounts capture the signed bounds. | |
5662 | * Thus, in all cases it suffices to blow away our signed bounds | |
5663 | * and rely on inferring new ones from the unsigned bounds and | |
5664 | * var_off of the result. | |
5665 | */ | |
5666 | dst_reg->s32_min_value = S32_MIN; | |
5667 | dst_reg->s32_max_value = S32_MAX; | |
5668 | ||
5669 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
5670 | dst_reg->u32_min_value >>= umax_val; | |
5671 | dst_reg->u32_max_value >>= umin_val; | |
5672 | ||
5673 | __mark_reg64_unbounded(dst_reg); | |
5674 | __update_reg32_bounds(dst_reg); | |
5675 | } | |
5676 | ||
07cd2631 JF |
5677 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
5678 | struct bpf_reg_state *src_reg) | |
5679 | { | |
5680 | u64 umax_val = src_reg->umax_value; | |
5681 | u64 umin_val = src_reg->umin_value; | |
5682 | ||
5683 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
5684 | * be negative, then either: | |
5685 | * 1) src_reg might be zero, so the sign bit of the result is | |
5686 | * unknown, so we lose our signed bounds | |
5687 | * 2) it's known negative, thus the unsigned bounds capture the | |
5688 | * signed bounds | |
5689 | * 3) the signed bounds cross zero, so they tell us nothing | |
5690 | * about the result | |
5691 | * If the value in dst_reg is known nonnegative, then again the | |
5692 | * unsigned bounts capture the signed bounds. | |
5693 | * Thus, in all cases it suffices to blow away our signed bounds | |
5694 | * and rely on inferring new ones from the unsigned bounds and | |
5695 | * var_off of the result. | |
5696 | */ | |
5697 | dst_reg->smin_value = S64_MIN; | |
5698 | dst_reg->smax_value = S64_MAX; | |
5699 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
5700 | dst_reg->umin_value >>= umax_val; | |
5701 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
5702 | |
5703 | /* Its not easy to operate on alu32 bounds here because it depends | |
5704 | * on bits being shifted in. Take easy way out and mark unbounded | |
5705 | * so we can recalculate later from tnum. | |
5706 | */ | |
5707 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
5708 | __update_reg_bounds(dst_reg); |
5709 | } | |
5710 | ||
3f50f132 JF |
5711 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
5712 | struct bpf_reg_state *src_reg) | |
07cd2631 | 5713 | { |
3f50f132 | 5714 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
5715 | |
5716 | /* Upon reaching here, src_known is true and | |
5717 | * umax_val is equal to umin_val. | |
5718 | */ | |
3f50f132 JF |
5719 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
5720 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 5721 | |
3f50f132 JF |
5722 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
5723 | ||
5724 | /* blow away the dst_reg umin_value/umax_value and rely on | |
5725 | * dst_reg var_off to refine the result. | |
5726 | */ | |
5727 | dst_reg->u32_min_value = 0; | |
5728 | dst_reg->u32_max_value = U32_MAX; | |
5729 | ||
5730 | __mark_reg64_unbounded(dst_reg); | |
5731 | __update_reg32_bounds(dst_reg); | |
5732 | } | |
5733 | ||
5734 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
5735 | struct bpf_reg_state *src_reg) | |
5736 | { | |
5737 | u64 umin_val = src_reg->umin_value; | |
5738 | ||
5739 | /* Upon reaching here, src_known is true and umax_val is equal | |
5740 | * to umin_val. | |
5741 | */ | |
5742 | dst_reg->smin_value >>= umin_val; | |
5743 | dst_reg->smax_value >>= umin_val; | |
5744 | ||
5745 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
5746 | |
5747 | /* blow away the dst_reg umin_value/umax_value and rely on | |
5748 | * dst_reg var_off to refine the result. | |
5749 | */ | |
5750 | dst_reg->umin_value = 0; | |
5751 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
5752 | |
5753 | /* Its not easy to operate on alu32 bounds here because it depends | |
5754 | * on bits being shifted in from upper 32-bits. Take easy way out | |
5755 | * and mark unbounded so we can recalculate later from tnum. | |
5756 | */ | |
5757 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
5758 | __update_reg_bounds(dst_reg); |
5759 | } | |
5760 | ||
468f6eaf JH |
5761 | /* WARNING: This function does calculations on 64-bit values, but the actual |
5762 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
5763 | * need extra checks in the 32-bit case. | |
5764 | */ | |
f1174f77 EC |
5765 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
5766 | struct bpf_insn *insn, | |
5767 | struct bpf_reg_state *dst_reg, | |
5768 | struct bpf_reg_state src_reg) | |
969bf05e | 5769 | { |
638f5b90 | 5770 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 5771 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 5772 | bool src_known; |
b03c9f9f EC |
5773 | s64 smin_val, smax_val; |
5774 | u64 umin_val, umax_val; | |
3f50f132 JF |
5775 | s32 s32_min_val, s32_max_val; |
5776 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 5777 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
5778 | u32 dst = insn->dst_reg; |
5779 | int ret; | |
3f50f132 | 5780 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
b799207e | 5781 | |
b03c9f9f EC |
5782 | smin_val = src_reg.smin_value; |
5783 | smax_val = src_reg.smax_value; | |
5784 | umin_val = src_reg.umin_value; | |
5785 | umax_val = src_reg.umax_value; | |
f23cc643 | 5786 | |
3f50f132 JF |
5787 | s32_min_val = src_reg.s32_min_value; |
5788 | s32_max_val = src_reg.s32_max_value; | |
5789 | u32_min_val = src_reg.u32_min_value; | |
5790 | u32_max_val = src_reg.u32_max_value; | |
5791 | ||
5792 | if (alu32) { | |
5793 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
5794 | if ((src_known && |
5795 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
5796 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
5797 | /* Taint dst register if offset had invalid bounds | |
5798 | * derived from e.g. dead branches. | |
5799 | */ | |
5800 | __mark_reg_unknown(env, dst_reg); | |
5801 | return 0; | |
5802 | } | |
5803 | } else { | |
5804 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
5805 | if ((src_known && |
5806 | (smin_val != smax_val || umin_val != umax_val)) || | |
5807 | smin_val > smax_val || umin_val > umax_val) { | |
5808 | /* Taint dst register if offset had invalid bounds | |
5809 | * derived from e.g. dead branches. | |
5810 | */ | |
5811 | __mark_reg_unknown(env, dst_reg); | |
5812 | return 0; | |
5813 | } | |
6f16101e DB |
5814 | } |
5815 | ||
bb7f0f98 AS |
5816 | if (!src_known && |
5817 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 5818 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
5819 | return 0; |
5820 | } | |
5821 | ||
3f50f132 JF |
5822 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
5823 | * There are two classes of instructions: The first class we track both | |
5824 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
5825 | * greatest amount of precision when alu operations are mixed with jmp32 | |
5826 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
5827 | * and BPF_OR. This is possible because these ops have fairly easy to | |
5828 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
5829 | * See alu32 verifier tests for examples. The second class of | |
5830 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
5831 | * with regards to tracking sign/unsigned bounds because the bits may | |
5832 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
5833 | * the reg unbounded in the subreg bound space and use the resulting | |
5834 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
5835 | */ | |
48461135 JB |
5836 | switch (opcode) { |
5837 | case BPF_ADD: | |
d3bd7413 DB |
5838 | ret = sanitize_val_alu(env, insn); |
5839 | if (ret < 0) { | |
5840 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
5841 | return ret; | |
5842 | } | |
3f50f132 | 5843 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 5844 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 5845 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
5846 | break; |
5847 | case BPF_SUB: | |
d3bd7413 DB |
5848 | ret = sanitize_val_alu(env, insn); |
5849 | if (ret < 0) { | |
5850 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
5851 | return ret; | |
5852 | } | |
3f50f132 | 5853 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 5854 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 5855 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
5856 | break; |
5857 | case BPF_MUL: | |
3f50f132 JF |
5858 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
5859 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 5860 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
5861 | break; |
5862 | case BPF_AND: | |
3f50f132 JF |
5863 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
5864 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 5865 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
5866 | break; |
5867 | case BPF_OR: | |
3f50f132 JF |
5868 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
5869 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 5870 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 JB |
5871 | break; |
5872 | case BPF_LSH: | |
468f6eaf JH |
5873 | if (umax_val >= insn_bitness) { |
5874 | /* Shifts greater than 31 or 63 are undefined. | |
5875 | * This includes shifts by a negative number. | |
b03c9f9f | 5876 | */ |
61bd5218 | 5877 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
5878 | break; |
5879 | } | |
3f50f132 JF |
5880 | if (alu32) |
5881 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
5882 | else | |
5883 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
5884 | break; |
5885 | case BPF_RSH: | |
468f6eaf JH |
5886 | if (umax_val >= insn_bitness) { |
5887 | /* Shifts greater than 31 or 63 are undefined. | |
5888 | * This includes shifts by a negative number. | |
b03c9f9f | 5889 | */ |
61bd5218 | 5890 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
5891 | break; |
5892 | } | |
3f50f132 JF |
5893 | if (alu32) |
5894 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
5895 | else | |
5896 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 5897 | break; |
9cbe1f5a YS |
5898 | case BPF_ARSH: |
5899 | if (umax_val >= insn_bitness) { | |
5900 | /* Shifts greater than 31 or 63 are undefined. | |
5901 | * This includes shifts by a negative number. | |
5902 | */ | |
5903 | mark_reg_unknown(env, regs, insn->dst_reg); | |
5904 | break; | |
5905 | } | |
3f50f132 JF |
5906 | if (alu32) |
5907 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
5908 | else | |
5909 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 5910 | break; |
48461135 | 5911 | default: |
61bd5218 | 5912 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
5913 | break; |
5914 | } | |
5915 | ||
3f50f132 JF |
5916 | /* ALU32 ops are zero extended into 64bit register */ |
5917 | if (alu32) | |
5918 | zext_32_to_64(dst_reg); | |
468f6eaf | 5919 | |
294f2fc6 | 5920 | __update_reg_bounds(dst_reg); |
b03c9f9f EC |
5921 | __reg_deduce_bounds(dst_reg); |
5922 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
5923 | return 0; |
5924 | } | |
5925 | ||
5926 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
5927 | * and var_off. | |
5928 | */ | |
5929 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
5930 | struct bpf_insn *insn) | |
5931 | { | |
f4d7e40a AS |
5932 | struct bpf_verifier_state *vstate = env->cur_state; |
5933 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5934 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
5935 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
5936 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 5937 | int err; |
f1174f77 EC |
5938 | |
5939 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
5940 | src_reg = NULL; |
5941 | if (dst_reg->type != SCALAR_VALUE) | |
5942 | ptr_reg = dst_reg; | |
5943 | if (BPF_SRC(insn->code) == BPF_X) { | |
5944 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
5945 | if (src_reg->type != SCALAR_VALUE) { |
5946 | if (dst_reg->type != SCALAR_VALUE) { | |
5947 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
5948 | * an arbitrary scalar. Disallow all math except |
5949 | * pointer subtraction | |
f1174f77 | 5950 | */ |
dd066823 | 5951 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
5952 | mark_reg_unknown(env, regs, insn->dst_reg); |
5953 | return 0; | |
f1174f77 | 5954 | } |
82abbf8d AS |
5955 | verbose(env, "R%d pointer %s pointer prohibited\n", |
5956 | insn->dst_reg, | |
5957 | bpf_alu_string[opcode >> 4]); | |
5958 | return -EACCES; | |
f1174f77 EC |
5959 | } else { |
5960 | /* scalar += pointer | |
5961 | * This is legal, but we have to reverse our | |
5962 | * src/dest handling in computing the range | |
5963 | */ | |
b5dc0163 AS |
5964 | err = mark_chain_precision(env, insn->dst_reg); |
5965 | if (err) | |
5966 | return err; | |
82abbf8d AS |
5967 | return adjust_ptr_min_max_vals(env, insn, |
5968 | src_reg, dst_reg); | |
f1174f77 EC |
5969 | } |
5970 | } else if (ptr_reg) { | |
5971 | /* pointer += scalar */ | |
b5dc0163 AS |
5972 | err = mark_chain_precision(env, insn->src_reg); |
5973 | if (err) | |
5974 | return err; | |
82abbf8d AS |
5975 | return adjust_ptr_min_max_vals(env, insn, |
5976 | dst_reg, src_reg); | |
f1174f77 EC |
5977 | } |
5978 | } else { | |
5979 | /* Pretend the src is a reg with a known value, since we only | |
5980 | * need to be able to read from this state. | |
5981 | */ | |
5982 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 5983 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 5984 | src_reg = &off_reg; |
82abbf8d AS |
5985 | if (ptr_reg) /* pointer += K */ |
5986 | return adjust_ptr_min_max_vals(env, insn, | |
5987 | ptr_reg, src_reg); | |
f1174f77 EC |
5988 | } |
5989 | ||
5990 | /* Got here implies adding two SCALAR_VALUEs */ | |
5991 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 5992 | print_verifier_state(env, state); |
61bd5218 | 5993 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
5994 | return -EINVAL; |
5995 | } | |
5996 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 5997 | print_verifier_state(env, state); |
61bd5218 | 5998 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
5999 | return -EINVAL; |
6000 | } | |
6001 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
6002 | } |
6003 | ||
17a52670 | 6004 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 6005 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 6006 | { |
638f5b90 | 6007 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
6008 | u8 opcode = BPF_OP(insn->code); |
6009 | int err; | |
6010 | ||
6011 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
6012 | if (opcode == BPF_NEG) { | |
6013 | if (BPF_SRC(insn->code) != 0 || | |
6014 | insn->src_reg != BPF_REG_0 || | |
6015 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 6016 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
6017 | return -EINVAL; |
6018 | } | |
6019 | } else { | |
6020 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
6021 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
6022 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 6023 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
6024 | return -EINVAL; |
6025 | } | |
6026 | } | |
6027 | ||
6028 | /* check src operand */ | |
dc503a8a | 6029 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6030 | if (err) |
6031 | return err; | |
6032 | ||
1be7f75d | 6033 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 6034 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
6035 | insn->dst_reg); |
6036 | return -EACCES; | |
6037 | } | |
6038 | ||
17a52670 | 6039 | /* check dest operand */ |
dc503a8a | 6040 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
6041 | if (err) |
6042 | return err; | |
6043 | ||
6044 | } else if (opcode == BPF_MOV) { | |
6045 | ||
6046 | if (BPF_SRC(insn->code) == BPF_X) { | |
6047 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6048 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6049 | return -EINVAL; |
6050 | } | |
6051 | ||
6052 | /* check src operand */ | |
dc503a8a | 6053 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6054 | if (err) |
6055 | return err; | |
6056 | } else { | |
6057 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6058 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6059 | return -EINVAL; |
6060 | } | |
6061 | } | |
6062 | ||
fbeb1603 AF |
6063 | /* check dest operand, mark as required later */ |
6064 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
6065 | if (err) |
6066 | return err; | |
6067 | ||
6068 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
6069 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
6070 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
6071 | ||
17a52670 AS |
6072 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6073 | /* case: R1 = R2 | |
6074 | * copy register state to dest reg | |
6075 | */ | |
e434b8cd JW |
6076 | *dst_reg = *src_reg; |
6077 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 6078 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 6079 | } else { |
f1174f77 | 6080 | /* R1 = (u32) R2 */ |
1be7f75d | 6081 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
6082 | verbose(env, |
6083 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
6084 | insn->src_reg); |
6085 | return -EACCES; | |
e434b8cd JW |
6086 | } else if (src_reg->type == SCALAR_VALUE) { |
6087 | *dst_reg = *src_reg; | |
6088 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 6089 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
6090 | } else { |
6091 | mark_reg_unknown(env, regs, | |
6092 | insn->dst_reg); | |
1be7f75d | 6093 | } |
3f50f132 | 6094 | zext_32_to_64(dst_reg); |
17a52670 AS |
6095 | } |
6096 | } else { | |
6097 | /* case: R = imm | |
6098 | * remember the value we stored into this reg | |
6099 | */ | |
fbeb1603 AF |
6100 | /* clear any state __mark_reg_known doesn't set */ |
6101 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 6102 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
6103 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6104 | __mark_reg_known(regs + insn->dst_reg, | |
6105 | insn->imm); | |
6106 | } else { | |
6107 | __mark_reg_known(regs + insn->dst_reg, | |
6108 | (u32)insn->imm); | |
6109 | } | |
17a52670 AS |
6110 | } |
6111 | ||
6112 | } else if (opcode > BPF_END) { | |
61bd5218 | 6113 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
6114 | return -EINVAL; |
6115 | ||
6116 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
6117 | ||
17a52670 AS |
6118 | if (BPF_SRC(insn->code) == BPF_X) { |
6119 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6120 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6121 | return -EINVAL; |
6122 | } | |
6123 | /* check src1 operand */ | |
dc503a8a | 6124 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6125 | if (err) |
6126 | return err; | |
6127 | } else { | |
6128 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6129 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6130 | return -EINVAL; |
6131 | } | |
6132 | } | |
6133 | ||
6134 | /* check src2 operand */ | |
dc503a8a | 6135 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6136 | if (err) |
6137 | return err; | |
6138 | ||
6139 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
6140 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 6141 | verbose(env, "div by zero\n"); |
17a52670 AS |
6142 | return -EINVAL; |
6143 | } | |
6144 | ||
229394e8 RV |
6145 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
6146 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
6147 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
6148 | ||
6149 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 6150 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
6151 | return -EINVAL; |
6152 | } | |
6153 | } | |
6154 | ||
1a0dc1ac | 6155 | /* check dest operand */ |
dc503a8a | 6156 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
6157 | if (err) |
6158 | return err; | |
6159 | ||
f1174f77 | 6160 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
6161 | } |
6162 | ||
6163 | return 0; | |
6164 | } | |
6165 | ||
c6a9efa1 PC |
6166 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
6167 | struct bpf_reg_state *dst_reg, | |
6168 | enum bpf_reg_type type, u16 new_range) | |
6169 | { | |
6170 | struct bpf_reg_state *reg; | |
6171 | int i; | |
6172 | ||
6173 | for (i = 0; i < MAX_BPF_REG; i++) { | |
6174 | reg = &state->regs[i]; | |
6175 | if (reg->type == type && reg->id == dst_reg->id) | |
6176 | /* keep the maximum range already checked */ | |
6177 | reg->range = max(reg->range, new_range); | |
6178 | } | |
6179 | ||
6180 | bpf_for_each_spilled_reg(i, state, reg) { | |
6181 | if (!reg) | |
6182 | continue; | |
6183 | if (reg->type == type && reg->id == dst_reg->id) | |
6184 | reg->range = max(reg->range, new_range); | |
6185 | } | |
6186 | } | |
6187 | ||
f4d7e40a | 6188 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 6189 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 6190 | enum bpf_reg_type type, |
fb2a311a | 6191 | bool range_right_open) |
969bf05e | 6192 | { |
fb2a311a | 6193 | u16 new_range; |
c6a9efa1 | 6194 | int i; |
2d2be8ca | 6195 | |
fb2a311a DB |
6196 | if (dst_reg->off < 0 || |
6197 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
6198 | /* This doesn't give us any range */ |
6199 | return; | |
6200 | ||
b03c9f9f EC |
6201 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
6202 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
6203 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
6204 | * than pkt_end, but that's because it's also less than pkt. | |
6205 | */ | |
6206 | return; | |
6207 | ||
fb2a311a DB |
6208 | new_range = dst_reg->off; |
6209 | if (range_right_open) | |
6210 | new_range--; | |
6211 | ||
6212 | /* Examples for register markings: | |
2d2be8ca | 6213 | * |
fb2a311a | 6214 | * pkt_data in dst register: |
2d2be8ca DB |
6215 | * |
6216 | * r2 = r3; | |
6217 | * r2 += 8; | |
6218 | * if (r2 > pkt_end) goto <handle exception> | |
6219 | * <access okay> | |
6220 | * | |
b4e432f1 DB |
6221 | * r2 = r3; |
6222 | * r2 += 8; | |
6223 | * if (r2 < pkt_end) goto <access okay> | |
6224 | * <handle exception> | |
6225 | * | |
2d2be8ca DB |
6226 | * Where: |
6227 | * r2 == dst_reg, pkt_end == src_reg | |
6228 | * r2=pkt(id=n,off=8,r=0) | |
6229 | * r3=pkt(id=n,off=0,r=0) | |
6230 | * | |
fb2a311a | 6231 | * pkt_data in src register: |
2d2be8ca DB |
6232 | * |
6233 | * r2 = r3; | |
6234 | * r2 += 8; | |
6235 | * if (pkt_end >= r2) goto <access okay> | |
6236 | * <handle exception> | |
6237 | * | |
b4e432f1 DB |
6238 | * r2 = r3; |
6239 | * r2 += 8; | |
6240 | * if (pkt_end <= r2) goto <handle exception> | |
6241 | * <access okay> | |
6242 | * | |
2d2be8ca DB |
6243 | * Where: |
6244 | * pkt_end == dst_reg, r2 == src_reg | |
6245 | * r2=pkt(id=n,off=8,r=0) | |
6246 | * r3=pkt(id=n,off=0,r=0) | |
6247 | * | |
6248 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
6249 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
6250 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
6251 | * the check. | |
969bf05e | 6252 | */ |
2d2be8ca | 6253 | |
f1174f77 EC |
6254 | /* If our ids match, then we must have the same max_value. And we |
6255 | * don't care about the other reg's fixed offset, since if it's too big | |
6256 | * the range won't allow anything. | |
6257 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
6258 | */ | |
c6a9efa1 PC |
6259 | for (i = 0; i <= vstate->curframe; i++) |
6260 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
6261 | new_range); | |
969bf05e AS |
6262 | } |
6263 | ||
3f50f132 | 6264 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 6265 | { |
3f50f132 JF |
6266 | struct tnum subreg = tnum_subreg(reg->var_off); |
6267 | s32 sval = (s32)val; | |
a72dafaf | 6268 | |
3f50f132 JF |
6269 | switch (opcode) { |
6270 | case BPF_JEQ: | |
6271 | if (tnum_is_const(subreg)) | |
6272 | return !!tnum_equals_const(subreg, val); | |
6273 | break; | |
6274 | case BPF_JNE: | |
6275 | if (tnum_is_const(subreg)) | |
6276 | return !tnum_equals_const(subreg, val); | |
6277 | break; | |
6278 | case BPF_JSET: | |
6279 | if ((~subreg.mask & subreg.value) & val) | |
6280 | return 1; | |
6281 | if (!((subreg.mask | subreg.value) & val)) | |
6282 | return 0; | |
6283 | break; | |
6284 | case BPF_JGT: | |
6285 | if (reg->u32_min_value > val) | |
6286 | return 1; | |
6287 | else if (reg->u32_max_value <= val) | |
6288 | return 0; | |
6289 | break; | |
6290 | case BPF_JSGT: | |
6291 | if (reg->s32_min_value > sval) | |
6292 | return 1; | |
6293 | else if (reg->s32_max_value < sval) | |
6294 | return 0; | |
6295 | break; | |
6296 | case BPF_JLT: | |
6297 | if (reg->u32_max_value < val) | |
6298 | return 1; | |
6299 | else if (reg->u32_min_value >= val) | |
6300 | return 0; | |
6301 | break; | |
6302 | case BPF_JSLT: | |
6303 | if (reg->s32_max_value < sval) | |
6304 | return 1; | |
6305 | else if (reg->s32_min_value >= sval) | |
6306 | return 0; | |
6307 | break; | |
6308 | case BPF_JGE: | |
6309 | if (reg->u32_min_value >= val) | |
6310 | return 1; | |
6311 | else if (reg->u32_max_value < val) | |
6312 | return 0; | |
6313 | break; | |
6314 | case BPF_JSGE: | |
6315 | if (reg->s32_min_value >= sval) | |
6316 | return 1; | |
6317 | else if (reg->s32_max_value < sval) | |
6318 | return 0; | |
6319 | break; | |
6320 | case BPF_JLE: | |
6321 | if (reg->u32_max_value <= val) | |
6322 | return 1; | |
6323 | else if (reg->u32_min_value > val) | |
6324 | return 0; | |
6325 | break; | |
6326 | case BPF_JSLE: | |
6327 | if (reg->s32_max_value <= sval) | |
6328 | return 1; | |
6329 | else if (reg->s32_min_value > sval) | |
6330 | return 0; | |
6331 | break; | |
6332 | } | |
4f7b3e82 | 6333 | |
3f50f132 JF |
6334 | return -1; |
6335 | } | |
092ed096 | 6336 | |
3f50f132 JF |
6337 | |
6338 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
6339 | { | |
6340 | s64 sval = (s64)val; | |
a72dafaf | 6341 | |
4f7b3e82 AS |
6342 | switch (opcode) { |
6343 | case BPF_JEQ: | |
6344 | if (tnum_is_const(reg->var_off)) | |
6345 | return !!tnum_equals_const(reg->var_off, val); | |
6346 | break; | |
6347 | case BPF_JNE: | |
6348 | if (tnum_is_const(reg->var_off)) | |
6349 | return !tnum_equals_const(reg->var_off, val); | |
6350 | break; | |
960ea056 JK |
6351 | case BPF_JSET: |
6352 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
6353 | return 1; | |
6354 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
6355 | return 0; | |
6356 | break; | |
4f7b3e82 AS |
6357 | case BPF_JGT: |
6358 | if (reg->umin_value > val) | |
6359 | return 1; | |
6360 | else if (reg->umax_value <= val) | |
6361 | return 0; | |
6362 | break; | |
6363 | case BPF_JSGT: | |
a72dafaf | 6364 | if (reg->smin_value > sval) |
4f7b3e82 | 6365 | return 1; |
a72dafaf | 6366 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6367 | return 0; |
6368 | break; | |
6369 | case BPF_JLT: | |
6370 | if (reg->umax_value < val) | |
6371 | return 1; | |
6372 | else if (reg->umin_value >= val) | |
6373 | return 0; | |
6374 | break; | |
6375 | case BPF_JSLT: | |
a72dafaf | 6376 | if (reg->smax_value < sval) |
4f7b3e82 | 6377 | return 1; |
a72dafaf | 6378 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
6379 | return 0; |
6380 | break; | |
6381 | case BPF_JGE: | |
6382 | if (reg->umin_value >= val) | |
6383 | return 1; | |
6384 | else if (reg->umax_value < val) | |
6385 | return 0; | |
6386 | break; | |
6387 | case BPF_JSGE: | |
a72dafaf | 6388 | if (reg->smin_value >= sval) |
4f7b3e82 | 6389 | return 1; |
a72dafaf | 6390 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6391 | return 0; |
6392 | break; | |
6393 | case BPF_JLE: | |
6394 | if (reg->umax_value <= val) | |
6395 | return 1; | |
6396 | else if (reg->umin_value > val) | |
6397 | return 0; | |
6398 | break; | |
6399 | case BPF_JSLE: | |
a72dafaf | 6400 | if (reg->smax_value <= sval) |
4f7b3e82 | 6401 | return 1; |
a72dafaf | 6402 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
6403 | return 0; |
6404 | break; | |
6405 | } | |
6406 | ||
6407 | return -1; | |
6408 | } | |
6409 | ||
3f50f132 JF |
6410 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
6411 | * and return: | |
6412 | * 1 - branch will be taken and "goto target" will be executed | |
6413 | * 0 - branch will not be taken and fall-through to next insn | |
6414 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
6415 | * range [0,10] | |
604dca5e | 6416 | */ |
3f50f132 JF |
6417 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
6418 | bool is_jmp32) | |
604dca5e | 6419 | { |
cac616db JF |
6420 | if (__is_pointer_value(false, reg)) { |
6421 | if (!reg_type_not_null(reg->type)) | |
6422 | return -1; | |
6423 | ||
6424 | /* If pointer is valid tests against zero will fail so we can | |
6425 | * use this to direct branch taken. | |
6426 | */ | |
6427 | if (val != 0) | |
6428 | return -1; | |
6429 | ||
6430 | switch (opcode) { | |
6431 | case BPF_JEQ: | |
6432 | return 0; | |
6433 | case BPF_JNE: | |
6434 | return 1; | |
6435 | default: | |
6436 | return -1; | |
6437 | } | |
6438 | } | |
604dca5e | 6439 | |
3f50f132 JF |
6440 | if (is_jmp32) |
6441 | return is_branch32_taken(reg, val, opcode); | |
6442 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
6443 | } |
6444 | ||
48461135 JB |
6445 | /* Adjusts the register min/max values in the case that the dst_reg is the |
6446 | * variable register that we are working on, and src_reg is a constant or we're | |
6447 | * simply doing a BPF_K check. | |
f1174f77 | 6448 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
6449 | */ |
6450 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
6451 | struct bpf_reg_state *false_reg, |
6452 | u64 val, u32 val32, | |
092ed096 | 6453 | u8 opcode, bool is_jmp32) |
48461135 | 6454 | { |
3f50f132 JF |
6455 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
6456 | struct tnum false_64off = false_reg->var_off; | |
6457 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
6458 | struct tnum true_64off = true_reg->var_off; | |
6459 | s64 sval = (s64)val; | |
6460 | s32 sval32 = (s32)val32; | |
a72dafaf | 6461 | |
f1174f77 EC |
6462 | /* If the dst_reg is a pointer, we can't learn anything about its |
6463 | * variable offset from the compare (unless src_reg were a pointer into | |
6464 | * the same object, but we don't bother with that. | |
6465 | * Since false_reg and true_reg have the same type by construction, we | |
6466 | * only need to check one of them for pointerness. | |
6467 | */ | |
6468 | if (__is_pointer_value(false, false_reg)) | |
6469 | return; | |
4cabc5b1 | 6470 | |
48461135 JB |
6471 | switch (opcode) { |
6472 | case BPF_JEQ: | |
48461135 | 6473 | case BPF_JNE: |
a72dafaf JW |
6474 | { |
6475 | struct bpf_reg_state *reg = | |
6476 | opcode == BPF_JEQ ? true_reg : false_reg; | |
6477 | ||
6478 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but | |
6479 | * if it is true we know the value for sure. Likewise for | |
6480 | * BPF_JNE. | |
48461135 | 6481 | */ |
3f50f132 JF |
6482 | if (is_jmp32) |
6483 | __mark_reg32_known(reg, val32); | |
6484 | else | |
092ed096 | 6485 | __mark_reg_known(reg, val); |
48461135 | 6486 | break; |
a72dafaf | 6487 | } |
960ea056 | 6488 | case BPF_JSET: |
3f50f132 JF |
6489 | if (is_jmp32) { |
6490 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
6491 | if (is_power_of_2(val32)) | |
6492 | true_32off = tnum_or(true_32off, | |
6493 | tnum_const(val32)); | |
6494 | } else { | |
6495 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
6496 | if (is_power_of_2(val)) | |
6497 | true_64off = tnum_or(true_64off, | |
6498 | tnum_const(val)); | |
6499 | } | |
960ea056 | 6500 | break; |
48461135 | 6501 | case BPF_JGE: |
a72dafaf JW |
6502 | case BPF_JGT: |
6503 | { | |
3f50f132 JF |
6504 | if (is_jmp32) { |
6505 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
6506 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
6507 | ||
6508 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
6509 | false_umax); | |
6510 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
6511 | true_umin); | |
6512 | } else { | |
6513 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
6514 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
6515 | ||
6516 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
6517 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
6518 | } | |
b03c9f9f | 6519 | break; |
a72dafaf | 6520 | } |
48461135 | 6521 | case BPF_JSGE: |
a72dafaf JW |
6522 | case BPF_JSGT: |
6523 | { | |
3f50f132 JF |
6524 | if (is_jmp32) { |
6525 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
6526 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 6527 | |
3f50f132 JF |
6528 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
6529 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
6530 | } else { | |
6531 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
6532 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
6533 | ||
6534 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
6535 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
6536 | } | |
48461135 | 6537 | break; |
a72dafaf | 6538 | } |
b4e432f1 | 6539 | case BPF_JLE: |
a72dafaf JW |
6540 | case BPF_JLT: |
6541 | { | |
3f50f132 JF |
6542 | if (is_jmp32) { |
6543 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
6544 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
6545 | ||
6546 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
6547 | false_umin); | |
6548 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
6549 | true_umax); | |
6550 | } else { | |
6551 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
6552 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
6553 | ||
6554 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
6555 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
6556 | } | |
b4e432f1 | 6557 | break; |
a72dafaf | 6558 | } |
b4e432f1 | 6559 | case BPF_JSLE: |
a72dafaf JW |
6560 | case BPF_JSLT: |
6561 | { | |
3f50f132 JF |
6562 | if (is_jmp32) { |
6563 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
6564 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 6565 | |
3f50f132 JF |
6566 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
6567 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
6568 | } else { | |
6569 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
6570 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
6571 | ||
6572 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
6573 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
6574 | } | |
b4e432f1 | 6575 | break; |
a72dafaf | 6576 | } |
48461135 | 6577 | default: |
0fc31b10 | 6578 | return; |
48461135 JB |
6579 | } |
6580 | ||
3f50f132 JF |
6581 | if (is_jmp32) { |
6582 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
6583 | tnum_subreg(false_32off)); | |
6584 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
6585 | tnum_subreg(true_32off)); | |
6586 | __reg_combine_32_into_64(false_reg); | |
6587 | __reg_combine_32_into_64(true_reg); | |
6588 | } else { | |
6589 | false_reg->var_off = false_64off; | |
6590 | true_reg->var_off = true_64off; | |
6591 | __reg_combine_64_into_32(false_reg); | |
6592 | __reg_combine_64_into_32(true_reg); | |
6593 | } | |
48461135 JB |
6594 | } |
6595 | ||
f1174f77 EC |
6596 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
6597 | * the variable reg. | |
48461135 JB |
6598 | */ |
6599 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
6600 | struct bpf_reg_state *false_reg, |
6601 | u64 val, u32 val32, | |
092ed096 | 6602 | u8 opcode, bool is_jmp32) |
48461135 | 6603 | { |
0fc31b10 JH |
6604 | /* How can we transform "a <op> b" into "b <op> a"? */ |
6605 | static const u8 opcode_flip[16] = { | |
6606 | /* these stay the same */ | |
6607 | [BPF_JEQ >> 4] = BPF_JEQ, | |
6608 | [BPF_JNE >> 4] = BPF_JNE, | |
6609 | [BPF_JSET >> 4] = BPF_JSET, | |
6610 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
6611 | [BPF_JGE >> 4] = BPF_JLE, | |
6612 | [BPF_JGT >> 4] = BPF_JLT, | |
6613 | [BPF_JLE >> 4] = BPF_JGE, | |
6614 | [BPF_JLT >> 4] = BPF_JGT, | |
6615 | [BPF_JSGE >> 4] = BPF_JSLE, | |
6616 | [BPF_JSGT >> 4] = BPF_JSLT, | |
6617 | [BPF_JSLE >> 4] = BPF_JSGE, | |
6618 | [BPF_JSLT >> 4] = BPF_JSGT | |
6619 | }; | |
6620 | opcode = opcode_flip[opcode >> 4]; | |
6621 | /* This uses zero as "not present in table"; luckily the zero opcode, | |
6622 | * BPF_JA, can't get here. | |
b03c9f9f | 6623 | */ |
0fc31b10 | 6624 | if (opcode) |
3f50f132 | 6625 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
6626 | } |
6627 | ||
6628 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
6629 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
6630 | struct bpf_reg_state *dst_reg) | |
6631 | { | |
b03c9f9f EC |
6632 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
6633 | dst_reg->umin_value); | |
6634 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
6635 | dst_reg->umax_value); | |
6636 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
6637 | dst_reg->smin_value); | |
6638 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
6639 | dst_reg->smax_value); | |
f1174f77 EC |
6640 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
6641 | dst_reg->var_off); | |
b03c9f9f EC |
6642 | /* We might have learned new bounds from the var_off. */ |
6643 | __update_reg_bounds(src_reg); | |
6644 | __update_reg_bounds(dst_reg); | |
6645 | /* We might have learned something about the sign bit. */ | |
6646 | __reg_deduce_bounds(src_reg); | |
6647 | __reg_deduce_bounds(dst_reg); | |
6648 | /* We might have learned some bits from the bounds. */ | |
6649 | __reg_bound_offset(src_reg); | |
6650 | __reg_bound_offset(dst_reg); | |
6651 | /* Intersecting with the old var_off might have improved our bounds | |
6652 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
6653 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
6654 | */ | |
6655 | __update_reg_bounds(src_reg); | |
6656 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
6657 | } |
6658 | ||
6659 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
6660 | struct bpf_reg_state *true_dst, | |
6661 | struct bpf_reg_state *false_src, | |
6662 | struct bpf_reg_state *false_dst, | |
6663 | u8 opcode) | |
6664 | { | |
6665 | switch (opcode) { | |
6666 | case BPF_JEQ: | |
6667 | __reg_combine_min_max(true_src, true_dst); | |
6668 | break; | |
6669 | case BPF_JNE: | |
6670 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 6671 | break; |
4cabc5b1 | 6672 | } |
48461135 JB |
6673 | } |
6674 | ||
fd978bf7 JS |
6675 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
6676 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 6677 | bool is_null) |
57a09bf0 | 6678 | { |
840b9615 | 6679 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
6680 | /* Old offset (both fixed and variable parts) should |
6681 | * have been known-zero, because we don't allow pointer | |
6682 | * arithmetic on pointers that might be NULL. | |
6683 | */ | |
b03c9f9f EC |
6684 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
6685 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 6686 | reg->off)) { |
b03c9f9f EC |
6687 | __mark_reg_known_zero(reg); |
6688 | reg->off = 0; | |
f1174f77 EC |
6689 | } |
6690 | if (is_null) { | |
6691 | reg->type = SCALAR_VALUE; | |
840b9615 | 6692 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
64d85290 JS |
6693 | const struct bpf_map *map = reg->map_ptr; |
6694 | ||
6695 | if (map->inner_map_meta) { | |
840b9615 | 6696 | reg->type = CONST_PTR_TO_MAP; |
64d85290 JS |
6697 | reg->map_ptr = map->inner_map_meta; |
6698 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { | |
fada7fdc | 6699 | reg->type = PTR_TO_XDP_SOCK; |
64d85290 JS |
6700 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
6701 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
6702 | reg->type = PTR_TO_SOCKET; | |
840b9615 JS |
6703 | } else { |
6704 | reg->type = PTR_TO_MAP_VALUE; | |
6705 | } | |
c64b7983 JS |
6706 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
6707 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
6708 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
6709 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
6710 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
6711 | reg->type = PTR_TO_TCP_SOCK; | |
b121b341 YS |
6712 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
6713 | reg->type = PTR_TO_BTF_ID; | |
457f4436 AN |
6714 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
6715 | reg->type = PTR_TO_MEM; | |
56f668df | 6716 | } |
1b986589 MKL |
6717 | if (is_null) { |
6718 | /* We don't need id and ref_obj_id from this point | |
6719 | * onwards anymore, thus we should better reset it, | |
6720 | * so that state pruning has chances to take effect. | |
6721 | */ | |
6722 | reg->id = 0; | |
6723 | reg->ref_obj_id = 0; | |
6724 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
6725 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
6726 | * in release_reg_references(). | |
6727 | * | |
6728 | * reg->id is still used by spin_lock ptr. Other | |
6729 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
6730 | */ |
6731 | reg->id = 0; | |
56f668df | 6732 | } |
57a09bf0 TG |
6733 | } |
6734 | } | |
6735 | ||
c6a9efa1 PC |
6736 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
6737 | bool is_null) | |
6738 | { | |
6739 | struct bpf_reg_state *reg; | |
6740 | int i; | |
6741 | ||
6742 | for (i = 0; i < MAX_BPF_REG; i++) | |
6743 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
6744 | ||
6745 | bpf_for_each_spilled_reg(i, state, reg) { | |
6746 | if (!reg) | |
6747 | continue; | |
6748 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
6749 | } | |
6750 | } | |
6751 | ||
57a09bf0 TG |
6752 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
6753 | * be folded together at some point. | |
6754 | */ | |
840b9615 JS |
6755 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
6756 | bool is_null) | |
57a09bf0 | 6757 | { |
f4d7e40a | 6758 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 6759 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 6760 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 6761 | u32 id = regs[regno].id; |
c6a9efa1 | 6762 | int i; |
57a09bf0 | 6763 | |
1b986589 MKL |
6764 | if (ref_obj_id && ref_obj_id == id && is_null) |
6765 | /* regs[regno] is in the " == NULL" branch. | |
6766 | * No one could have freed the reference state before | |
6767 | * doing the NULL check. | |
6768 | */ | |
6769 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 6770 | |
c6a9efa1 PC |
6771 | for (i = 0; i <= vstate->curframe; i++) |
6772 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
6773 | } |
6774 | ||
5beca081 DB |
6775 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
6776 | struct bpf_reg_state *dst_reg, | |
6777 | struct bpf_reg_state *src_reg, | |
6778 | struct bpf_verifier_state *this_branch, | |
6779 | struct bpf_verifier_state *other_branch) | |
6780 | { | |
6781 | if (BPF_SRC(insn->code) != BPF_X) | |
6782 | return false; | |
6783 | ||
092ed096 JW |
6784 | /* Pointers are always 64-bit. */ |
6785 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
6786 | return false; | |
6787 | ||
5beca081 DB |
6788 | switch (BPF_OP(insn->code)) { |
6789 | case BPF_JGT: | |
6790 | if ((dst_reg->type == PTR_TO_PACKET && | |
6791 | src_reg->type == PTR_TO_PACKET_END) || | |
6792 | (dst_reg->type == PTR_TO_PACKET_META && | |
6793 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
6794 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
6795 | find_good_pkt_pointers(this_branch, dst_reg, | |
6796 | dst_reg->type, false); | |
6797 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
6798 | src_reg->type == PTR_TO_PACKET) || | |
6799 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
6800 | src_reg->type == PTR_TO_PACKET_META)) { | |
6801 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
6802 | find_good_pkt_pointers(other_branch, src_reg, | |
6803 | src_reg->type, true); | |
6804 | } else { | |
6805 | return false; | |
6806 | } | |
6807 | break; | |
6808 | case BPF_JLT: | |
6809 | if ((dst_reg->type == PTR_TO_PACKET && | |
6810 | src_reg->type == PTR_TO_PACKET_END) || | |
6811 | (dst_reg->type == PTR_TO_PACKET_META && | |
6812 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
6813 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
6814 | find_good_pkt_pointers(other_branch, dst_reg, | |
6815 | dst_reg->type, true); | |
6816 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
6817 | src_reg->type == PTR_TO_PACKET) || | |
6818 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
6819 | src_reg->type == PTR_TO_PACKET_META)) { | |
6820 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
6821 | find_good_pkt_pointers(this_branch, src_reg, | |
6822 | src_reg->type, false); | |
6823 | } else { | |
6824 | return false; | |
6825 | } | |
6826 | break; | |
6827 | case BPF_JGE: | |
6828 | if ((dst_reg->type == PTR_TO_PACKET && | |
6829 | src_reg->type == PTR_TO_PACKET_END) || | |
6830 | (dst_reg->type == PTR_TO_PACKET_META && | |
6831 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
6832 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
6833 | find_good_pkt_pointers(this_branch, dst_reg, | |
6834 | dst_reg->type, true); | |
6835 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
6836 | src_reg->type == PTR_TO_PACKET) || | |
6837 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
6838 | src_reg->type == PTR_TO_PACKET_META)) { | |
6839 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
6840 | find_good_pkt_pointers(other_branch, src_reg, | |
6841 | src_reg->type, false); | |
6842 | } else { | |
6843 | return false; | |
6844 | } | |
6845 | break; | |
6846 | case BPF_JLE: | |
6847 | if ((dst_reg->type == PTR_TO_PACKET && | |
6848 | src_reg->type == PTR_TO_PACKET_END) || | |
6849 | (dst_reg->type == PTR_TO_PACKET_META && | |
6850 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
6851 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
6852 | find_good_pkt_pointers(other_branch, dst_reg, | |
6853 | dst_reg->type, false); | |
6854 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
6855 | src_reg->type == PTR_TO_PACKET) || | |
6856 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
6857 | src_reg->type == PTR_TO_PACKET_META)) { | |
6858 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
6859 | find_good_pkt_pointers(this_branch, src_reg, | |
6860 | src_reg->type, true); | |
6861 | } else { | |
6862 | return false; | |
6863 | } | |
6864 | break; | |
6865 | default: | |
6866 | return false; | |
6867 | } | |
6868 | ||
6869 | return true; | |
6870 | } | |
6871 | ||
58e2af8b | 6872 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
6873 | struct bpf_insn *insn, int *insn_idx) |
6874 | { | |
f4d7e40a AS |
6875 | struct bpf_verifier_state *this_branch = env->cur_state; |
6876 | struct bpf_verifier_state *other_branch; | |
6877 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 6878 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 6879 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 6880 | bool is_jmp32; |
fb8d251e | 6881 | int pred = -1; |
17a52670 AS |
6882 | int err; |
6883 | ||
092ed096 JW |
6884 | /* Only conditional jumps are expected to reach here. */ |
6885 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
6886 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
6887 | return -EINVAL; |
6888 | } | |
6889 | ||
6890 | if (BPF_SRC(insn->code) == BPF_X) { | |
6891 | if (insn->imm != 0) { | |
092ed096 | 6892 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
6893 | return -EINVAL; |
6894 | } | |
6895 | ||
6896 | /* check src1 operand */ | |
dc503a8a | 6897 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6898 | if (err) |
6899 | return err; | |
1be7f75d AS |
6900 | |
6901 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 6902 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
6903 | insn->src_reg); |
6904 | return -EACCES; | |
6905 | } | |
fb8d251e | 6906 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
6907 | } else { |
6908 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 6909 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
6910 | return -EINVAL; |
6911 | } | |
6912 | } | |
6913 | ||
6914 | /* check src2 operand */ | |
dc503a8a | 6915 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6916 | if (err) |
6917 | return err; | |
6918 | ||
1a0dc1ac | 6919 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 6920 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 6921 | |
3f50f132 JF |
6922 | if (BPF_SRC(insn->code) == BPF_K) { |
6923 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
6924 | } else if (src_reg->type == SCALAR_VALUE && | |
6925 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
6926 | pred = is_branch_taken(dst_reg, | |
6927 | tnum_subreg(src_reg->var_off).value, | |
6928 | opcode, | |
6929 | is_jmp32); | |
6930 | } else if (src_reg->type == SCALAR_VALUE && | |
6931 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
6932 | pred = is_branch_taken(dst_reg, | |
6933 | src_reg->var_off.value, | |
6934 | opcode, | |
6935 | is_jmp32); | |
6936 | } | |
6937 | ||
b5dc0163 | 6938 | if (pred >= 0) { |
cac616db JF |
6939 | /* If we get here with a dst_reg pointer type it is because |
6940 | * above is_branch_taken() special cased the 0 comparison. | |
6941 | */ | |
6942 | if (!__is_pointer_value(false, dst_reg)) | |
6943 | err = mark_chain_precision(env, insn->dst_reg); | |
b5dc0163 AS |
6944 | if (BPF_SRC(insn->code) == BPF_X && !err) |
6945 | err = mark_chain_precision(env, insn->src_reg); | |
6946 | if (err) | |
6947 | return err; | |
6948 | } | |
fb8d251e AS |
6949 | if (pred == 1) { |
6950 | /* only follow the goto, ignore fall-through */ | |
6951 | *insn_idx += insn->off; | |
6952 | return 0; | |
6953 | } else if (pred == 0) { | |
6954 | /* only follow fall-through branch, since | |
6955 | * that's where the program will go | |
6956 | */ | |
6957 | return 0; | |
17a52670 AS |
6958 | } |
6959 | ||
979d63d5 DB |
6960 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
6961 | false); | |
17a52670 AS |
6962 | if (!other_branch) |
6963 | return -EFAULT; | |
f4d7e40a | 6964 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 6965 | |
48461135 JB |
6966 | /* detect if we are comparing against a constant value so we can adjust |
6967 | * our min/max values for our dst register. | |
f1174f77 EC |
6968 | * this is only legit if both are scalars (or pointers to the same |
6969 | * object, I suppose, but we don't support that right now), because | |
6970 | * otherwise the different base pointers mean the offsets aren't | |
6971 | * comparable. | |
48461135 JB |
6972 | */ |
6973 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 6974 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 6975 | |
f1174f77 | 6976 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
6977 | src_reg->type == SCALAR_VALUE) { |
6978 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
6979 | (is_jmp32 && |
6980 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 6981 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 6982 | dst_reg, |
3f50f132 JF |
6983 | src_reg->var_off.value, |
6984 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
6985 | opcode, is_jmp32); |
6986 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
6987 | (is_jmp32 && |
6988 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 6989 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 6990 | src_reg, |
3f50f132 JF |
6991 | dst_reg->var_off.value, |
6992 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
6993 | opcode, is_jmp32); |
6994 | else if (!is_jmp32 && | |
6995 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 6996 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
6997 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
6998 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 6999 | src_reg, dst_reg, opcode); |
f1174f77 EC |
7000 | } |
7001 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 7002 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
7003 | dst_reg, insn->imm, (u32)insn->imm, |
7004 | opcode, is_jmp32); | |
48461135 JB |
7005 | } |
7006 | ||
092ed096 JW |
7007 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
7008 | * NOTE: these optimizations below are related with pointer comparison | |
7009 | * which will never be JMP32. | |
7010 | */ | |
7011 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 7012 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
7013 | reg_type_may_be_null(dst_reg->type)) { |
7014 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
7015 | * safe or unknown depending R == 0 or R != 0 conditional. |
7016 | */ | |
840b9615 JS |
7017 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
7018 | opcode == BPF_JNE); | |
7019 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
7020 | opcode == BPF_JEQ); | |
5beca081 DB |
7021 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
7022 | this_branch, other_branch) && | |
7023 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
7024 | verbose(env, "R%d pointer comparison prohibited\n", |
7025 | insn->dst_reg); | |
1be7f75d | 7026 | return -EACCES; |
17a52670 | 7027 | } |
06ee7115 | 7028 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 7029 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
7030 | return 0; |
7031 | } | |
7032 | ||
17a52670 | 7033 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 7034 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 7035 | { |
d8eca5bb | 7036 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 7037 | struct bpf_reg_state *regs = cur_regs(env); |
d8eca5bb | 7038 | struct bpf_map *map; |
17a52670 AS |
7039 | int err; |
7040 | ||
7041 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 7042 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
7043 | return -EINVAL; |
7044 | } | |
7045 | if (insn->off != 0) { | |
61bd5218 | 7046 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
7047 | return -EINVAL; |
7048 | } | |
7049 | ||
dc503a8a | 7050 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
7051 | if (err) |
7052 | return err; | |
7053 | ||
6b173873 | 7054 | if (insn->src_reg == 0) { |
6b173873 JK |
7055 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
7056 | ||
f1174f77 | 7057 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 7058 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 7059 | return 0; |
6b173873 | 7060 | } |
17a52670 | 7061 | |
d8eca5bb DB |
7062 | map = env->used_maps[aux->map_index]; |
7063 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
7064 | regs[insn->dst_reg].map_ptr = map; | |
7065 | ||
7066 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
7067 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
7068 | regs[insn->dst_reg].off = aux->map_off; | |
7069 | if (map_value_has_spin_lock(map)) | |
7070 | regs[insn->dst_reg].id = ++env->id_gen; | |
7071 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
7072 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
7073 | } else { | |
7074 | verbose(env, "bpf verifier is misconfigured\n"); | |
7075 | return -EINVAL; | |
7076 | } | |
17a52670 | 7077 | |
17a52670 AS |
7078 | return 0; |
7079 | } | |
7080 | ||
96be4325 DB |
7081 | static bool may_access_skb(enum bpf_prog_type type) |
7082 | { | |
7083 | switch (type) { | |
7084 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
7085 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 7086 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
7087 | return true; |
7088 | default: | |
7089 | return false; | |
7090 | } | |
7091 | } | |
7092 | ||
ddd872bc AS |
7093 | /* verify safety of LD_ABS|LD_IND instructions: |
7094 | * - they can only appear in the programs where ctx == skb | |
7095 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
7096 | * preserve R6-R9, and store return value into R0 | |
7097 | * | |
7098 | * Implicit input: | |
7099 | * ctx == skb == R6 == CTX | |
7100 | * | |
7101 | * Explicit input: | |
7102 | * SRC == any register | |
7103 | * IMM == 32-bit immediate | |
7104 | * | |
7105 | * Output: | |
7106 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
7107 | */ | |
58e2af8b | 7108 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 7109 | { |
638f5b90 | 7110 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 7111 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 7112 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
7113 | int i, err; |
7114 | ||
24701ece | 7115 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 7116 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
7117 | return -EINVAL; |
7118 | } | |
7119 | ||
e0cea7ce DB |
7120 | if (!env->ops->gen_ld_abs) { |
7121 | verbose(env, "bpf verifier is misconfigured\n"); | |
7122 | return -EINVAL; | |
7123 | } | |
7124 | ||
f910cefa | 7125 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
7126 | /* when program has LD_ABS insn JITs and interpreter assume |
7127 | * that r1 == ctx == skb which is not the case for callees | |
7128 | * that can have arbitrary arguments. It's problematic | |
7129 | * for main prog as well since JITs would need to analyze | |
7130 | * all functions in order to make proper register save/restore | |
7131 | * decisions in the main prog. Hence disallow LD_ABS with calls | |
7132 | */ | |
7133 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); | |
7134 | return -EINVAL; | |
7135 | } | |
7136 | ||
ddd872bc | 7137 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 7138 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 7139 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 7140 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
7141 | return -EINVAL; |
7142 | } | |
7143 | ||
7144 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 7145 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
7146 | if (err) |
7147 | return err; | |
7148 | ||
fd978bf7 JS |
7149 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
7150 | * gen_ld_abs() may terminate the program at runtime, leading to | |
7151 | * reference leak. | |
7152 | */ | |
7153 | err = check_reference_leak(env); | |
7154 | if (err) { | |
7155 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
7156 | return err; | |
7157 | } | |
7158 | ||
d83525ca AS |
7159 | if (env->cur_state->active_spin_lock) { |
7160 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
7161 | return -EINVAL; | |
7162 | } | |
7163 | ||
6d4f151a | 7164 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
7165 | verbose(env, |
7166 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
7167 | return -EINVAL; |
7168 | } | |
7169 | ||
7170 | if (mode == BPF_IND) { | |
7171 | /* check explicit source operand */ | |
dc503a8a | 7172 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
7173 | if (err) |
7174 | return err; | |
7175 | } | |
7176 | ||
6d4f151a DB |
7177 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
7178 | if (err < 0) | |
7179 | return err; | |
7180 | ||
ddd872bc | 7181 | /* reset caller saved regs to unreadable */ |
dc503a8a | 7182 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 7183 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
7184 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
7185 | } | |
ddd872bc AS |
7186 | |
7187 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
7188 | * the value fetched from the packet. |
7189 | * Already marked as written above. | |
ddd872bc | 7190 | */ |
61bd5218 | 7191 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
7192 | /* ld_abs load up to 32-bit skb data. */ |
7193 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
7194 | return 0; |
7195 | } | |
7196 | ||
390ee7e2 AS |
7197 | static int check_return_code(struct bpf_verifier_env *env) |
7198 | { | |
5cf1e914 | 7199 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 7200 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
7201 | struct bpf_reg_state *reg; |
7202 | struct tnum range = tnum_range(0, 1); | |
27ae7997 MKL |
7203 | int err; |
7204 | ||
9e4e01df KS |
7205 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
7206 | if ((env->prog->type == BPF_PROG_TYPE_STRUCT_OPS || | |
7207 | env->prog->type == BPF_PROG_TYPE_LSM) && | |
27ae7997 MKL |
7208 | !prog->aux->attach_func_proto->type) |
7209 | return 0; | |
7210 | ||
7211 | /* eBPF calling convetion is such that R0 is used | |
7212 | * to return the value from eBPF program. | |
7213 | * Make sure that it's readable at this time | |
7214 | * of bpf_exit, which means that program wrote | |
7215 | * something into it earlier | |
7216 | */ | |
7217 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
7218 | if (err) | |
7219 | return err; | |
7220 | ||
7221 | if (is_pointer_value(env, BPF_REG_0)) { | |
7222 | verbose(env, "R0 leaks addr as return value\n"); | |
7223 | return -EACCES; | |
7224 | } | |
390ee7e2 AS |
7225 | |
7226 | switch (env->prog->type) { | |
983695fa DB |
7227 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
7228 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
7229 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
7230 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
7231 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
7232 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
7233 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 7234 | range = tnum_range(1, 1); |
ed4ed404 | 7235 | break; |
390ee7e2 | 7236 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 7237 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
7238 | range = tnum_range(0, 3); | |
7239 | enforce_attach_type_range = tnum_range(2, 3); | |
7240 | } | |
ed4ed404 | 7241 | break; |
390ee7e2 AS |
7242 | case BPF_PROG_TYPE_CGROUP_SOCK: |
7243 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 7244 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 7245 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 7246 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 7247 | break; |
15ab09bd AS |
7248 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
7249 | if (!env->prog->aux->attach_btf_id) | |
7250 | return 0; | |
7251 | range = tnum_const(0); | |
7252 | break; | |
15d83c4d | 7253 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
7254 | switch (env->prog->expected_attach_type) { |
7255 | case BPF_TRACE_FENTRY: | |
7256 | case BPF_TRACE_FEXIT: | |
7257 | range = tnum_const(0); | |
7258 | break; | |
7259 | case BPF_TRACE_RAW_TP: | |
7260 | case BPF_MODIFY_RETURN: | |
15d83c4d | 7261 | return 0; |
2ec0616e DB |
7262 | case BPF_TRACE_ITER: |
7263 | break; | |
e92888c7 YS |
7264 | default: |
7265 | return -ENOTSUPP; | |
7266 | } | |
15d83c4d | 7267 | break; |
e92888c7 YS |
7268 | case BPF_PROG_TYPE_EXT: |
7269 | /* freplace program can return anything as its return value | |
7270 | * depends on the to-be-replaced kernel func or bpf program. | |
7271 | */ | |
390ee7e2 AS |
7272 | default: |
7273 | return 0; | |
7274 | } | |
7275 | ||
638f5b90 | 7276 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 7277 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 7278 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
7279 | reg_type_str[reg->type]); |
7280 | return -EINVAL; | |
7281 | } | |
7282 | ||
7283 | if (!tnum_in(range, reg->var_off)) { | |
5cf1e914 | 7284 | char tn_buf[48]; |
7285 | ||
61bd5218 | 7286 | verbose(env, "At program exit the register R0 "); |
390ee7e2 | 7287 | if (!tnum_is_unknown(reg->var_off)) { |
390ee7e2 | 7288 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 7289 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 7290 | } else { |
61bd5218 | 7291 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 7292 | } |
5cf1e914 | 7293 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
983695fa | 7294 | verbose(env, " should have been in %s\n", tn_buf); |
390ee7e2 AS |
7295 | return -EINVAL; |
7296 | } | |
5cf1e914 | 7297 | |
7298 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
7299 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
7300 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
7301 | return 0; |
7302 | } | |
7303 | ||
475fb78f AS |
7304 | /* non-recursive DFS pseudo code |
7305 | * 1 procedure DFS-iterative(G,v): | |
7306 | * 2 label v as discovered | |
7307 | * 3 let S be a stack | |
7308 | * 4 S.push(v) | |
7309 | * 5 while S is not empty | |
7310 | * 6 t <- S.pop() | |
7311 | * 7 if t is what we're looking for: | |
7312 | * 8 return t | |
7313 | * 9 for all edges e in G.adjacentEdges(t) do | |
7314 | * 10 if edge e is already labelled | |
7315 | * 11 continue with the next edge | |
7316 | * 12 w <- G.adjacentVertex(t,e) | |
7317 | * 13 if vertex w is not discovered and not explored | |
7318 | * 14 label e as tree-edge | |
7319 | * 15 label w as discovered | |
7320 | * 16 S.push(w) | |
7321 | * 17 continue at 5 | |
7322 | * 18 else if vertex w is discovered | |
7323 | * 19 label e as back-edge | |
7324 | * 20 else | |
7325 | * 21 // vertex w is explored | |
7326 | * 22 label e as forward- or cross-edge | |
7327 | * 23 label t as explored | |
7328 | * 24 S.pop() | |
7329 | * | |
7330 | * convention: | |
7331 | * 0x10 - discovered | |
7332 | * 0x11 - discovered and fall-through edge labelled | |
7333 | * 0x12 - discovered and fall-through and branch edges labelled | |
7334 | * 0x20 - explored | |
7335 | */ | |
7336 | ||
7337 | enum { | |
7338 | DISCOVERED = 0x10, | |
7339 | EXPLORED = 0x20, | |
7340 | FALLTHROUGH = 1, | |
7341 | BRANCH = 2, | |
7342 | }; | |
7343 | ||
dc2a4ebc AS |
7344 | static u32 state_htab_size(struct bpf_verifier_env *env) |
7345 | { | |
7346 | return env->prog->len; | |
7347 | } | |
7348 | ||
5d839021 AS |
7349 | static struct bpf_verifier_state_list **explored_state( |
7350 | struct bpf_verifier_env *env, | |
7351 | int idx) | |
7352 | { | |
dc2a4ebc AS |
7353 | struct bpf_verifier_state *cur = env->cur_state; |
7354 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
7355 | ||
7356 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
7357 | } |
7358 | ||
7359 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
7360 | { | |
a8f500af | 7361 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 7362 | } |
f1bca824 | 7363 | |
475fb78f AS |
7364 | /* t, w, e - match pseudo-code above: |
7365 | * t - index of current instruction | |
7366 | * w - next instruction | |
7367 | * e - edge | |
7368 | */ | |
2589726d AS |
7369 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
7370 | bool loop_ok) | |
475fb78f | 7371 | { |
7df737e9 AS |
7372 | int *insn_stack = env->cfg.insn_stack; |
7373 | int *insn_state = env->cfg.insn_state; | |
7374 | ||
475fb78f AS |
7375 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
7376 | return 0; | |
7377 | ||
7378 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
7379 | return 0; | |
7380 | ||
7381 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 7382 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 7383 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
7384 | return -EINVAL; |
7385 | } | |
7386 | ||
f1bca824 AS |
7387 | if (e == BRANCH) |
7388 | /* mark branch target for state pruning */ | |
5d839021 | 7389 | init_explored_state(env, w); |
f1bca824 | 7390 | |
475fb78f AS |
7391 | if (insn_state[w] == 0) { |
7392 | /* tree-edge */ | |
7393 | insn_state[t] = DISCOVERED | e; | |
7394 | insn_state[w] = DISCOVERED; | |
7df737e9 | 7395 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 7396 | return -E2BIG; |
7df737e9 | 7397 | insn_stack[env->cfg.cur_stack++] = w; |
475fb78f AS |
7398 | return 1; |
7399 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2c78ee89 | 7400 | if (loop_ok && env->bpf_capable) |
2589726d | 7401 | return 0; |
d9762e84 MKL |
7402 | verbose_linfo(env, t, "%d: ", t); |
7403 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 7404 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
7405 | return -EINVAL; |
7406 | } else if (insn_state[w] == EXPLORED) { | |
7407 | /* forward- or cross-edge */ | |
7408 | insn_state[t] = DISCOVERED | e; | |
7409 | } else { | |
61bd5218 | 7410 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
7411 | return -EFAULT; |
7412 | } | |
7413 | return 0; | |
7414 | } | |
7415 | ||
7416 | /* non-recursive depth-first-search to detect loops in BPF program | |
7417 | * loop == back-edge in directed graph | |
7418 | */ | |
58e2af8b | 7419 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
7420 | { |
7421 | struct bpf_insn *insns = env->prog->insnsi; | |
7422 | int insn_cnt = env->prog->len; | |
7df737e9 | 7423 | int *insn_stack, *insn_state; |
475fb78f AS |
7424 | int ret = 0; |
7425 | int i, t; | |
7426 | ||
7df737e9 | 7427 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
7428 | if (!insn_state) |
7429 | return -ENOMEM; | |
7430 | ||
7df737e9 | 7431 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 7432 | if (!insn_stack) { |
71dde681 | 7433 | kvfree(insn_state); |
475fb78f AS |
7434 | return -ENOMEM; |
7435 | } | |
7436 | ||
7437 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
7438 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 7439 | env->cfg.cur_stack = 1; |
475fb78f AS |
7440 | |
7441 | peek_stack: | |
7df737e9 | 7442 | if (env->cfg.cur_stack == 0) |
475fb78f | 7443 | goto check_state; |
7df737e9 | 7444 | t = insn_stack[env->cfg.cur_stack - 1]; |
475fb78f | 7445 | |
092ed096 JW |
7446 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
7447 | BPF_CLASS(insns[t].code) == BPF_JMP32) { | |
475fb78f AS |
7448 | u8 opcode = BPF_OP(insns[t].code); |
7449 | ||
7450 | if (opcode == BPF_EXIT) { | |
7451 | goto mark_explored; | |
7452 | } else if (opcode == BPF_CALL) { | |
2589726d | 7453 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
7454 | if (ret == 1) |
7455 | goto peek_stack; | |
7456 | else if (ret < 0) | |
7457 | goto err_free; | |
07016151 | 7458 | if (t + 1 < insn_cnt) |
5d839021 | 7459 | init_explored_state(env, t + 1); |
cc8b0b92 | 7460 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
5d839021 | 7461 | init_explored_state(env, t); |
2589726d AS |
7462 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
7463 | env, false); | |
cc8b0b92 AS |
7464 | if (ret == 1) |
7465 | goto peek_stack; | |
7466 | else if (ret < 0) | |
7467 | goto err_free; | |
7468 | } | |
475fb78f AS |
7469 | } else if (opcode == BPF_JA) { |
7470 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
7471 | ret = -EINVAL; | |
7472 | goto err_free; | |
7473 | } | |
7474 | /* unconditional jump with single edge */ | |
7475 | ret = push_insn(t, t + insns[t].off + 1, | |
2589726d | 7476 | FALLTHROUGH, env, true); |
475fb78f AS |
7477 | if (ret == 1) |
7478 | goto peek_stack; | |
7479 | else if (ret < 0) | |
7480 | goto err_free; | |
b5dc0163 AS |
7481 | /* unconditional jmp is not a good pruning point, |
7482 | * but it's marked, since backtracking needs | |
7483 | * to record jmp history in is_state_visited(). | |
7484 | */ | |
7485 | init_explored_state(env, t + insns[t].off + 1); | |
f1bca824 AS |
7486 | /* tell verifier to check for equivalent states |
7487 | * after every call and jump | |
7488 | */ | |
c3de6317 | 7489 | if (t + 1 < insn_cnt) |
5d839021 | 7490 | init_explored_state(env, t + 1); |
475fb78f AS |
7491 | } else { |
7492 | /* conditional jump with two edges */ | |
5d839021 | 7493 | init_explored_state(env, t); |
2589726d | 7494 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
475fb78f AS |
7495 | if (ret == 1) |
7496 | goto peek_stack; | |
7497 | else if (ret < 0) | |
7498 | goto err_free; | |
7499 | ||
2589726d | 7500 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
475fb78f AS |
7501 | if (ret == 1) |
7502 | goto peek_stack; | |
7503 | else if (ret < 0) | |
7504 | goto err_free; | |
7505 | } | |
7506 | } else { | |
7507 | /* all other non-branch instructions with single | |
7508 | * fall-through edge | |
7509 | */ | |
2589726d | 7510 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
7511 | if (ret == 1) |
7512 | goto peek_stack; | |
7513 | else if (ret < 0) | |
7514 | goto err_free; | |
7515 | } | |
7516 | ||
7517 | mark_explored: | |
7518 | insn_state[t] = EXPLORED; | |
7df737e9 | 7519 | if (env->cfg.cur_stack-- <= 0) { |
61bd5218 | 7520 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
7521 | ret = -EFAULT; |
7522 | goto err_free; | |
7523 | } | |
7524 | goto peek_stack; | |
7525 | ||
7526 | check_state: | |
7527 | for (i = 0; i < insn_cnt; i++) { | |
7528 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 7529 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
7530 | ret = -EINVAL; |
7531 | goto err_free; | |
7532 | } | |
7533 | } | |
7534 | ret = 0; /* cfg looks good */ | |
7535 | ||
7536 | err_free: | |
71dde681 AS |
7537 | kvfree(insn_state); |
7538 | kvfree(insn_stack); | |
7df737e9 | 7539 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
7540 | return ret; |
7541 | } | |
7542 | ||
838e9690 YS |
7543 | /* The minimum supported BTF func info size */ |
7544 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
7545 | #define MAX_FUNCINFO_REC_SIZE 252 | |
7546 | ||
c454a46b MKL |
7547 | static int check_btf_func(struct bpf_verifier_env *env, |
7548 | const union bpf_attr *attr, | |
7549 | union bpf_attr __user *uattr) | |
838e9690 | 7550 | { |
d0b2818e | 7551 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 7552 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 7553 | struct bpf_func_info *krecord; |
8c1b6e69 | 7554 | struct bpf_func_info_aux *info_aux = NULL; |
838e9690 | 7555 | const struct btf_type *type; |
c454a46b MKL |
7556 | struct bpf_prog *prog; |
7557 | const struct btf *btf; | |
838e9690 | 7558 | void __user *urecord; |
d0b2818e | 7559 | u32 prev_offset = 0; |
e7ed83d6 | 7560 | int ret = -ENOMEM; |
838e9690 YS |
7561 | |
7562 | nfuncs = attr->func_info_cnt; | |
7563 | if (!nfuncs) | |
7564 | return 0; | |
7565 | ||
7566 | if (nfuncs != env->subprog_cnt) { | |
7567 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
7568 | return -EINVAL; | |
7569 | } | |
7570 | ||
7571 | urec_size = attr->func_info_rec_size; | |
7572 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
7573 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
7574 | urec_size % sizeof(u32)) { | |
7575 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
7576 | return -EINVAL; | |
7577 | } | |
7578 | ||
c454a46b MKL |
7579 | prog = env->prog; |
7580 | btf = prog->aux->btf; | |
838e9690 YS |
7581 | |
7582 | urecord = u64_to_user_ptr(attr->func_info); | |
7583 | min_size = min_t(u32, krec_size, urec_size); | |
7584 | ||
ba64e7d8 | 7585 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
7586 | if (!krecord) |
7587 | return -ENOMEM; | |
8c1b6e69 AS |
7588 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
7589 | if (!info_aux) | |
7590 | goto err_free; | |
ba64e7d8 | 7591 | |
838e9690 YS |
7592 | for (i = 0; i < nfuncs; i++) { |
7593 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
7594 | if (ret) { | |
7595 | if (ret == -E2BIG) { | |
7596 | verbose(env, "nonzero tailing record in func info"); | |
7597 | /* set the size kernel expects so loader can zero | |
7598 | * out the rest of the record. | |
7599 | */ | |
7600 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
7601 | ret = -EFAULT; | |
7602 | } | |
c454a46b | 7603 | goto err_free; |
838e9690 YS |
7604 | } |
7605 | ||
ba64e7d8 | 7606 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 7607 | ret = -EFAULT; |
c454a46b | 7608 | goto err_free; |
838e9690 YS |
7609 | } |
7610 | ||
d30d42e0 | 7611 | /* check insn_off */ |
838e9690 | 7612 | if (i == 0) { |
d30d42e0 | 7613 | if (krecord[i].insn_off) { |
838e9690 | 7614 | verbose(env, |
d30d42e0 MKL |
7615 | "nonzero insn_off %u for the first func info record", |
7616 | krecord[i].insn_off); | |
838e9690 | 7617 | ret = -EINVAL; |
c454a46b | 7618 | goto err_free; |
838e9690 | 7619 | } |
d30d42e0 | 7620 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
7621 | verbose(env, |
7622 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 7623 | krecord[i].insn_off, prev_offset); |
838e9690 | 7624 | ret = -EINVAL; |
c454a46b | 7625 | goto err_free; |
838e9690 YS |
7626 | } |
7627 | ||
d30d42e0 | 7628 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 YS |
7629 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
7630 | ret = -EINVAL; | |
c454a46b | 7631 | goto err_free; |
838e9690 YS |
7632 | } |
7633 | ||
7634 | /* check type_id */ | |
ba64e7d8 | 7635 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 7636 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 7637 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 7638 | krecord[i].type_id); |
838e9690 | 7639 | ret = -EINVAL; |
c454a46b | 7640 | goto err_free; |
838e9690 | 7641 | } |
51c39bb1 | 7642 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
d30d42e0 | 7643 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
7644 | urecord += urec_size; |
7645 | } | |
7646 | ||
ba64e7d8 YS |
7647 | prog->aux->func_info = krecord; |
7648 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 7649 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
7650 | return 0; |
7651 | ||
c454a46b | 7652 | err_free: |
ba64e7d8 | 7653 | kvfree(krecord); |
8c1b6e69 | 7654 | kfree(info_aux); |
838e9690 YS |
7655 | return ret; |
7656 | } | |
7657 | ||
ba64e7d8 YS |
7658 | static void adjust_btf_func(struct bpf_verifier_env *env) |
7659 | { | |
8c1b6e69 | 7660 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
7661 | int i; |
7662 | ||
8c1b6e69 | 7663 | if (!aux->func_info) |
ba64e7d8 YS |
7664 | return; |
7665 | ||
7666 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 7667 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
7668 | } |
7669 | ||
c454a46b MKL |
7670 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
7671 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
7672 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
7673 | ||
7674 | static int check_btf_line(struct bpf_verifier_env *env, | |
7675 | const union bpf_attr *attr, | |
7676 | union bpf_attr __user *uattr) | |
7677 | { | |
7678 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
7679 | struct bpf_subprog_info *sub; | |
7680 | struct bpf_line_info *linfo; | |
7681 | struct bpf_prog *prog; | |
7682 | const struct btf *btf; | |
7683 | void __user *ulinfo; | |
7684 | int err; | |
7685 | ||
7686 | nr_linfo = attr->line_info_cnt; | |
7687 | if (!nr_linfo) | |
7688 | return 0; | |
7689 | ||
7690 | rec_size = attr->line_info_rec_size; | |
7691 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
7692 | rec_size > MAX_LINEINFO_REC_SIZE || | |
7693 | rec_size & (sizeof(u32) - 1)) | |
7694 | return -EINVAL; | |
7695 | ||
7696 | /* Need to zero it in case the userspace may | |
7697 | * pass in a smaller bpf_line_info object. | |
7698 | */ | |
7699 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
7700 | GFP_KERNEL | __GFP_NOWARN); | |
7701 | if (!linfo) | |
7702 | return -ENOMEM; | |
7703 | ||
7704 | prog = env->prog; | |
7705 | btf = prog->aux->btf; | |
7706 | ||
7707 | s = 0; | |
7708 | sub = env->subprog_info; | |
7709 | ulinfo = u64_to_user_ptr(attr->line_info); | |
7710 | expected_size = sizeof(struct bpf_line_info); | |
7711 | ncopy = min_t(u32, expected_size, rec_size); | |
7712 | for (i = 0; i < nr_linfo; i++) { | |
7713 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
7714 | if (err) { | |
7715 | if (err == -E2BIG) { | |
7716 | verbose(env, "nonzero tailing record in line_info"); | |
7717 | if (put_user(expected_size, | |
7718 | &uattr->line_info_rec_size)) | |
7719 | err = -EFAULT; | |
7720 | } | |
7721 | goto err_free; | |
7722 | } | |
7723 | ||
7724 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
7725 | err = -EFAULT; | |
7726 | goto err_free; | |
7727 | } | |
7728 | ||
7729 | /* | |
7730 | * Check insn_off to ensure | |
7731 | * 1) strictly increasing AND | |
7732 | * 2) bounded by prog->len | |
7733 | * | |
7734 | * The linfo[0].insn_off == 0 check logically falls into | |
7735 | * the later "missing bpf_line_info for func..." case | |
7736 | * because the first linfo[0].insn_off must be the | |
7737 | * first sub also and the first sub must have | |
7738 | * subprog_info[0].start == 0. | |
7739 | */ | |
7740 | if ((i && linfo[i].insn_off <= prev_offset) || | |
7741 | linfo[i].insn_off >= prog->len) { | |
7742 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
7743 | i, linfo[i].insn_off, prev_offset, | |
7744 | prog->len); | |
7745 | err = -EINVAL; | |
7746 | goto err_free; | |
7747 | } | |
7748 | ||
fdbaa0be MKL |
7749 | if (!prog->insnsi[linfo[i].insn_off].code) { |
7750 | verbose(env, | |
7751 | "Invalid insn code at line_info[%u].insn_off\n", | |
7752 | i); | |
7753 | err = -EINVAL; | |
7754 | goto err_free; | |
7755 | } | |
7756 | ||
23127b33 MKL |
7757 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
7758 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
7759 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
7760 | err = -EINVAL; | |
7761 | goto err_free; | |
7762 | } | |
7763 | ||
7764 | if (s != env->subprog_cnt) { | |
7765 | if (linfo[i].insn_off == sub[s].start) { | |
7766 | sub[s].linfo_idx = i; | |
7767 | s++; | |
7768 | } else if (sub[s].start < linfo[i].insn_off) { | |
7769 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
7770 | err = -EINVAL; | |
7771 | goto err_free; | |
7772 | } | |
7773 | } | |
7774 | ||
7775 | prev_offset = linfo[i].insn_off; | |
7776 | ulinfo += rec_size; | |
7777 | } | |
7778 | ||
7779 | if (s != env->subprog_cnt) { | |
7780 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
7781 | env->subprog_cnt - s, s); | |
7782 | err = -EINVAL; | |
7783 | goto err_free; | |
7784 | } | |
7785 | ||
7786 | prog->aux->linfo = linfo; | |
7787 | prog->aux->nr_linfo = nr_linfo; | |
7788 | ||
7789 | return 0; | |
7790 | ||
7791 | err_free: | |
7792 | kvfree(linfo); | |
7793 | return err; | |
7794 | } | |
7795 | ||
7796 | static int check_btf_info(struct bpf_verifier_env *env, | |
7797 | const union bpf_attr *attr, | |
7798 | union bpf_attr __user *uattr) | |
7799 | { | |
7800 | struct btf *btf; | |
7801 | int err; | |
7802 | ||
7803 | if (!attr->func_info_cnt && !attr->line_info_cnt) | |
7804 | return 0; | |
7805 | ||
7806 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
7807 | if (IS_ERR(btf)) | |
7808 | return PTR_ERR(btf); | |
7809 | env->prog->aux->btf = btf; | |
7810 | ||
7811 | err = check_btf_func(env, attr, uattr); | |
7812 | if (err) | |
7813 | return err; | |
7814 | ||
7815 | err = check_btf_line(env, attr, uattr); | |
7816 | if (err) | |
7817 | return err; | |
7818 | ||
7819 | return 0; | |
ba64e7d8 YS |
7820 | } |
7821 | ||
f1174f77 EC |
7822 | /* check %cur's range satisfies %old's */ |
7823 | static bool range_within(struct bpf_reg_state *old, | |
7824 | struct bpf_reg_state *cur) | |
7825 | { | |
b03c9f9f EC |
7826 | return old->umin_value <= cur->umin_value && |
7827 | old->umax_value >= cur->umax_value && | |
7828 | old->smin_value <= cur->smin_value && | |
7829 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
7830 | } |
7831 | ||
7832 | /* Maximum number of register states that can exist at once */ | |
7833 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
7834 | struct idpair { | |
7835 | u32 old; | |
7836 | u32 cur; | |
7837 | }; | |
7838 | ||
7839 | /* If in the old state two registers had the same id, then they need to have | |
7840 | * the same id in the new state as well. But that id could be different from | |
7841 | * the old state, so we need to track the mapping from old to new ids. | |
7842 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
7843 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
7844 | * regs with a different old id could still have new id 9, we don't care about | |
7845 | * that. | |
7846 | * So we look through our idmap to see if this old id has been seen before. If | |
7847 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 7848 | */ |
f1174f77 | 7849 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 7850 | { |
f1174f77 | 7851 | unsigned int i; |
969bf05e | 7852 | |
f1174f77 EC |
7853 | for (i = 0; i < ID_MAP_SIZE; i++) { |
7854 | if (!idmap[i].old) { | |
7855 | /* Reached an empty slot; haven't seen this id before */ | |
7856 | idmap[i].old = old_id; | |
7857 | idmap[i].cur = cur_id; | |
7858 | return true; | |
7859 | } | |
7860 | if (idmap[i].old == old_id) | |
7861 | return idmap[i].cur == cur_id; | |
7862 | } | |
7863 | /* We ran out of idmap slots, which should be impossible */ | |
7864 | WARN_ON_ONCE(1); | |
7865 | return false; | |
7866 | } | |
7867 | ||
9242b5f5 AS |
7868 | static void clean_func_state(struct bpf_verifier_env *env, |
7869 | struct bpf_func_state *st) | |
7870 | { | |
7871 | enum bpf_reg_liveness live; | |
7872 | int i, j; | |
7873 | ||
7874 | for (i = 0; i < BPF_REG_FP; i++) { | |
7875 | live = st->regs[i].live; | |
7876 | /* liveness must not touch this register anymore */ | |
7877 | st->regs[i].live |= REG_LIVE_DONE; | |
7878 | if (!(live & REG_LIVE_READ)) | |
7879 | /* since the register is unused, clear its state | |
7880 | * to make further comparison simpler | |
7881 | */ | |
f54c7898 | 7882 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
7883 | } |
7884 | ||
7885 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
7886 | live = st->stack[i].spilled_ptr.live; | |
7887 | /* liveness must not touch this stack slot anymore */ | |
7888 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
7889 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 7890 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
7891 | for (j = 0; j < BPF_REG_SIZE; j++) |
7892 | st->stack[i].slot_type[j] = STACK_INVALID; | |
7893 | } | |
7894 | } | |
7895 | } | |
7896 | ||
7897 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
7898 | struct bpf_verifier_state *st) | |
7899 | { | |
7900 | int i; | |
7901 | ||
7902 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
7903 | /* all regs in this state in all frames were already marked */ | |
7904 | return; | |
7905 | ||
7906 | for (i = 0; i <= st->curframe; i++) | |
7907 | clean_func_state(env, st->frame[i]); | |
7908 | } | |
7909 | ||
7910 | /* the parentage chains form a tree. | |
7911 | * the verifier states are added to state lists at given insn and | |
7912 | * pushed into state stack for future exploration. | |
7913 | * when the verifier reaches bpf_exit insn some of the verifer states | |
7914 | * stored in the state lists have their final liveness state already, | |
7915 | * but a lot of states will get revised from liveness point of view when | |
7916 | * the verifier explores other branches. | |
7917 | * Example: | |
7918 | * 1: r0 = 1 | |
7919 | * 2: if r1 == 100 goto pc+1 | |
7920 | * 3: r0 = 2 | |
7921 | * 4: exit | |
7922 | * when the verifier reaches exit insn the register r0 in the state list of | |
7923 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
7924 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
7925 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
7926 | * | |
7927 | * Since the verifier pushes the branch states as it sees them while exploring | |
7928 | * the program the condition of walking the branch instruction for the second | |
7929 | * time means that all states below this branch were already explored and | |
7930 | * their final liveness markes are already propagated. | |
7931 | * Hence when the verifier completes the search of state list in is_state_visited() | |
7932 | * we can call this clean_live_states() function to mark all liveness states | |
7933 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
7934 | * will not be used. | |
7935 | * This function also clears the registers and stack for states that !READ | |
7936 | * to simplify state merging. | |
7937 | * | |
7938 | * Important note here that walking the same branch instruction in the callee | |
7939 | * doesn't meant that the states are DONE. The verifier has to compare | |
7940 | * the callsites | |
7941 | */ | |
7942 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
7943 | struct bpf_verifier_state *cur) | |
7944 | { | |
7945 | struct bpf_verifier_state_list *sl; | |
7946 | int i; | |
7947 | ||
5d839021 | 7948 | sl = *explored_state(env, insn); |
a8f500af | 7949 | while (sl) { |
2589726d AS |
7950 | if (sl->state.branches) |
7951 | goto next; | |
dc2a4ebc AS |
7952 | if (sl->state.insn_idx != insn || |
7953 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
7954 | goto next; |
7955 | for (i = 0; i <= cur->curframe; i++) | |
7956 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
7957 | goto next; | |
7958 | clean_verifier_state(env, &sl->state); | |
7959 | next: | |
7960 | sl = sl->next; | |
7961 | } | |
7962 | } | |
7963 | ||
f1174f77 | 7964 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
7965 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
7966 | struct idpair *idmap) | |
f1174f77 | 7967 | { |
f4d7e40a AS |
7968 | bool equal; |
7969 | ||
dc503a8a EC |
7970 | if (!(rold->live & REG_LIVE_READ)) |
7971 | /* explored state didn't use this */ | |
7972 | return true; | |
7973 | ||
679c782d | 7974 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
7975 | |
7976 | if (rold->type == PTR_TO_STACK) | |
7977 | /* two stack pointers are equal only if they're pointing to | |
7978 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
7979 | */ | |
7980 | return equal && rold->frameno == rcur->frameno; | |
7981 | ||
7982 | if (equal) | |
969bf05e AS |
7983 | return true; |
7984 | ||
f1174f77 EC |
7985 | if (rold->type == NOT_INIT) |
7986 | /* explored state can't have used this */ | |
969bf05e | 7987 | return true; |
f1174f77 EC |
7988 | if (rcur->type == NOT_INIT) |
7989 | return false; | |
7990 | switch (rold->type) { | |
7991 | case SCALAR_VALUE: | |
7992 | if (rcur->type == SCALAR_VALUE) { | |
b5dc0163 AS |
7993 | if (!rold->precise && !rcur->precise) |
7994 | return true; | |
f1174f77 EC |
7995 | /* new val must satisfy old val knowledge */ |
7996 | return range_within(rold, rcur) && | |
7997 | tnum_in(rold->var_off, rcur->var_off); | |
7998 | } else { | |
179d1c56 JH |
7999 | /* We're trying to use a pointer in place of a scalar. |
8000 | * Even if the scalar was unbounded, this could lead to | |
8001 | * pointer leaks because scalars are allowed to leak | |
8002 | * while pointers are not. We could make this safe in | |
8003 | * special cases if root is calling us, but it's | |
8004 | * probably not worth the hassle. | |
f1174f77 | 8005 | */ |
179d1c56 | 8006 | return false; |
f1174f77 EC |
8007 | } |
8008 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
8009 | /* If the new min/max/var_off satisfy the old ones and |
8010 | * everything else matches, we are OK. | |
d83525ca AS |
8011 | * 'id' is not compared, since it's only used for maps with |
8012 | * bpf_spin_lock inside map element and in such cases if | |
8013 | * the rest of the prog is valid for one map element then | |
8014 | * it's valid for all map elements regardless of the key | |
8015 | * used in bpf_map_lookup() | |
1b688a19 EC |
8016 | */ |
8017 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
8018 | range_within(rold, rcur) && | |
8019 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
8020 | case PTR_TO_MAP_VALUE_OR_NULL: |
8021 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
8022 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
8023 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
8024 | * checked, doing so could have affected others with the same | |
8025 | * id, and we can't check for that because we lost the id when | |
8026 | * we converted to a PTR_TO_MAP_VALUE. | |
8027 | */ | |
8028 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
8029 | return false; | |
8030 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
8031 | return false; | |
8032 | /* Check our ids match any regs they're supposed to */ | |
8033 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 8034 | case PTR_TO_PACKET_META: |
f1174f77 | 8035 | case PTR_TO_PACKET: |
de8f3a83 | 8036 | if (rcur->type != rold->type) |
f1174f77 EC |
8037 | return false; |
8038 | /* We must have at least as much range as the old ptr | |
8039 | * did, so that any accesses which were safe before are | |
8040 | * still safe. This is true even if old range < old off, | |
8041 | * since someone could have accessed through (ptr - k), or | |
8042 | * even done ptr -= k in a register, to get a safe access. | |
8043 | */ | |
8044 | if (rold->range > rcur->range) | |
8045 | return false; | |
8046 | /* If the offsets don't match, we can't trust our alignment; | |
8047 | * nor can we be sure that we won't fall out of range. | |
8048 | */ | |
8049 | if (rold->off != rcur->off) | |
8050 | return false; | |
8051 | /* id relations must be preserved */ | |
8052 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
8053 | return false; | |
8054 | /* new val must satisfy old val knowledge */ | |
8055 | return range_within(rold, rcur) && | |
8056 | tnum_in(rold->var_off, rcur->var_off); | |
8057 | case PTR_TO_CTX: | |
8058 | case CONST_PTR_TO_MAP: | |
f1174f77 | 8059 | case PTR_TO_PACKET_END: |
d58e468b | 8060 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
8061 | case PTR_TO_SOCKET: |
8062 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
8063 | case PTR_TO_SOCK_COMMON: |
8064 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
8065 | case PTR_TO_TCP_SOCK: |
8066 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 8067 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
8068 | /* Only valid matches are exact, which memcmp() above |
8069 | * would have accepted | |
8070 | */ | |
8071 | default: | |
8072 | /* Don't know what's going on, just say it's not safe */ | |
8073 | return false; | |
8074 | } | |
969bf05e | 8075 | |
f1174f77 EC |
8076 | /* Shouldn't get here; if we do, say it's not safe */ |
8077 | WARN_ON_ONCE(1); | |
969bf05e AS |
8078 | return false; |
8079 | } | |
8080 | ||
f4d7e40a AS |
8081 | static bool stacksafe(struct bpf_func_state *old, |
8082 | struct bpf_func_state *cur, | |
638f5b90 AS |
8083 | struct idpair *idmap) |
8084 | { | |
8085 | int i, spi; | |
8086 | ||
638f5b90 AS |
8087 | /* walk slots of the explored stack and ignore any additional |
8088 | * slots in the current stack, since explored(safe) state | |
8089 | * didn't use them | |
8090 | */ | |
8091 | for (i = 0; i < old->allocated_stack; i++) { | |
8092 | spi = i / BPF_REG_SIZE; | |
8093 | ||
b233920c AS |
8094 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
8095 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 8096 | /* explored state didn't use this */ |
fd05e57b | 8097 | continue; |
b233920c | 8098 | } |
cc2b14d5 | 8099 | |
638f5b90 AS |
8100 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
8101 | continue; | |
19e2dbb7 AS |
8102 | |
8103 | /* explored stack has more populated slots than current stack | |
8104 | * and these slots were used | |
8105 | */ | |
8106 | if (i >= cur->allocated_stack) | |
8107 | return false; | |
8108 | ||
cc2b14d5 AS |
8109 | /* if old state was safe with misc data in the stack |
8110 | * it will be safe with zero-initialized stack. | |
8111 | * The opposite is not true | |
8112 | */ | |
8113 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
8114 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
8115 | continue; | |
638f5b90 AS |
8116 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
8117 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
8118 | /* Ex: old explored (safe) state has STACK_SPILL in | |
8119 | * this stack slot, but current has has STACK_MISC -> | |
8120 | * this verifier states are not equivalent, | |
8121 | * return false to continue verification of this path | |
8122 | */ | |
8123 | return false; | |
8124 | if (i % BPF_REG_SIZE) | |
8125 | continue; | |
8126 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
8127 | continue; | |
8128 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
8129 | &cur->stack[spi].spilled_ptr, | |
8130 | idmap)) | |
8131 | /* when explored and current stack slot are both storing | |
8132 | * spilled registers, check that stored pointers types | |
8133 | * are the same as well. | |
8134 | * Ex: explored safe path could have stored | |
8135 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
8136 | * but current path has stored: | |
8137 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
8138 | * such verifier states are not equivalent. | |
8139 | * return false to continue verification of this path | |
8140 | */ | |
8141 | return false; | |
8142 | } | |
8143 | return true; | |
8144 | } | |
8145 | ||
fd978bf7 JS |
8146 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
8147 | { | |
8148 | if (old->acquired_refs != cur->acquired_refs) | |
8149 | return false; | |
8150 | return !memcmp(old->refs, cur->refs, | |
8151 | sizeof(*old->refs) * old->acquired_refs); | |
8152 | } | |
8153 | ||
f1bca824 AS |
8154 | /* compare two verifier states |
8155 | * | |
8156 | * all states stored in state_list are known to be valid, since | |
8157 | * verifier reached 'bpf_exit' instruction through them | |
8158 | * | |
8159 | * this function is called when verifier exploring different branches of | |
8160 | * execution popped from the state stack. If it sees an old state that has | |
8161 | * more strict register state and more strict stack state then this execution | |
8162 | * branch doesn't need to be explored further, since verifier already | |
8163 | * concluded that more strict state leads to valid finish. | |
8164 | * | |
8165 | * Therefore two states are equivalent if register state is more conservative | |
8166 | * and explored stack state is more conservative than the current one. | |
8167 | * Example: | |
8168 | * explored current | |
8169 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
8170 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
8171 | * | |
8172 | * In other words if current stack state (one being explored) has more | |
8173 | * valid slots than old one that already passed validation, it means | |
8174 | * the verifier can stop exploring and conclude that current state is valid too | |
8175 | * | |
8176 | * Similarly with registers. If explored state has register type as invalid | |
8177 | * whereas register type in current state is meaningful, it means that | |
8178 | * the current state will reach 'bpf_exit' instruction safely | |
8179 | */ | |
f4d7e40a AS |
8180 | static bool func_states_equal(struct bpf_func_state *old, |
8181 | struct bpf_func_state *cur) | |
f1bca824 | 8182 | { |
f1174f77 EC |
8183 | struct idpair *idmap; |
8184 | bool ret = false; | |
f1bca824 AS |
8185 | int i; |
8186 | ||
f1174f77 EC |
8187 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
8188 | /* If we failed to allocate the idmap, just say it's not safe */ | |
8189 | if (!idmap) | |
1a0dc1ac | 8190 | return false; |
f1174f77 EC |
8191 | |
8192 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 8193 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 8194 | goto out_free; |
f1bca824 AS |
8195 | } |
8196 | ||
638f5b90 AS |
8197 | if (!stacksafe(old, cur, idmap)) |
8198 | goto out_free; | |
fd978bf7 JS |
8199 | |
8200 | if (!refsafe(old, cur)) | |
8201 | goto out_free; | |
f1174f77 EC |
8202 | ret = true; |
8203 | out_free: | |
8204 | kfree(idmap); | |
8205 | return ret; | |
f1bca824 AS |
8206 | } |
8207 | ||
f4d7e40a AS |
8208 | static bool states_equal(struct bpf_verifier_env *env, |
8209 | struct bpf_verifier_state *old, | |
8210 | struct bpf_verifier_state *cur) | |
8211 | { | |
8212 | int i; | |
8213 | ||
8214 | if (old->curframe != cur->curframe) | |
8215 | return false; | |
8216 | ||
979d63d5 DB |
8217 | /* Verification state from speculative execution simulation |
8218 | * must never prune a non-speculative execution one. | |
8219 | */ | |
8220 | if (old->speculative && !cur->speculative) | |
8221 | return false; | |
8222 | ||
d83525ca AS |
8223 | if (old->active_spin_lock != cur->active_spin_lock) |
8224 | return false; | |
8225 | ||
f4d7e40a AS |
8226 | /* for states to be equal callsites have to be the same |
8227 | * and all frame states need to be equivalent | |
8228 | */ | |
8229 | for (i = 0; i <= old->curframe; i++) { | |
8230 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
8231 | return false; | |
8232 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
8233 | return false; | |
8234 | } | |
8235 | return true; | |
8236 | } | |
8237 | ||
5327ed3d JW |
8238 | /* Return 0 if no propagation happened. Return negative error code if error |
8239 | * happened. Otherwise, return the propagated bit. | |
8240 | */ | |
55e7f3b5 JW |
8241 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
8242 | struct bpf_reg_state *reg, | |
8243 | struct bpf_reg_state *parent_reg) | |
8244 | { | |
5327ed3d JW |
8245 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
8246 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
8247 | int err; |
8248 | ||
5327ed3d JW |
8249 | /* When comes here, read flags of PARENT_REG or REG could be any of |
8250 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
8251 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
8252 | */ | |
8253 | if (parent_flag == REG_LIVE_READ64 || | |
8254 | /* Or if there is no read flag from REG. */ | |
8255 | !flag || | |
8256 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
8257 | parent_flag == flag) | |
55e7f3b5 JW |
8258 | return 0; |
8259 | ||
5327ed3d | 8260 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
8261 | if (err) |
8262 | return err; | |
8263 | ||
5327ed3d | 8264 | return flag; |
55e7f3b5 JW |
8265 | } |
8266 | ||
8e9cd9ce | 8267 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
8268 | * straight-line code between a state and its parent. When we arrive at an |
8269 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
8270 | * code, so read marks in the state must propagate to the parent regardless | |
8271 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 8272 | * in mark_reg_read() is for. |
8e9cd9ce | 8273 | */ |
f4d7e40a AS |
8274 | static int propagate_liveness(struct bpf_verifier_env *env, |
8275 | const struct bpf_verifier_state *vstate, | |
8276 | struct bpf_verifier_state *vparent) | |
dc503a8a | 8277 | { |
3f8cafa4 | 8278 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 8279 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 8280 | int i, frame, err = 0; |
dc503a8a | 8281 | |
f4d7e40a AS |
8282 | if (vparent->curframe != vstate->curframe) { |
8283 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
8284 | vparent->curframe, vstate->curframe); | |
8285 | return -EFAULT; | |
8286 | } | |
dc503a8a EC |
8287 | /* Propagate read liveness of registers... */ |
8288 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 8289 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
8290 | parent = vparent->frame[frame]; |
8291 | state = vstate->frame[frame]; | |
8292 | parent_reg = parent->regs; | |
8293 | state_reg = state->regs; | |
83d16312 JK |
8294 | /* We don't need to worry about FP liveness, it's read-only */ |
8295 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
8296 | err = propagate_liveness_reg(env, &state_reg[i], |
8297 | &parent_reg[i]); | |
5327ed3d | 8298 | if (err < 0) |
3f8cafa4 | 8299 | return err; |
5327ed3d JW |
8300 | if (err == REG_LIVE_READ64) |
8301 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 8302 | } |
f4d7e40a | 8303 | |
1b04aee7 | 8304 | /* Propagate stack slots. */ |
f4d7e40a AS |
8305 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
8306 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
8307 | parent_reg = &parent->stack[i].spilled_ptr; |
8308 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
8309 | err = propagate_liveness_reg(env, state_reg, |
8310 | parent_reg); | |
5327ed3d | 8311 | if (err < 0) |
3f8cafa4 | 8312 | return err; |
dc503a8a EC |
8313 | } |
8314 | } | |
5327ed3d | 8315 | return 0; |
dc503a8a EC |
8316 | } |
8317 | ||
a3ce685d AS |
8318 | /* find precise scalars in the previous equivalent state and |
8319 | * propagate them into the current state | |
8320 | */ | |
8321 | static int propagate_precision(struct bpf_verifier_env *env, | |
8322 | const struct bpf_verifier_state *old) | |
8323 | { | |
8324 | struct bpf_reg_state *state_reg; | |
8325 | struct bpf_func_state *state; | |
8326 | int i, err = 0; | |
8327 | ||
8328 | state = old->frame[old->curframe]; | |
8329 | state_reg = state->regs; | |
8330 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
8331 | if (state_reg->type != SCALAR_VALUE || | |
8332 | !state_reg->precise) | |
8333 | continue; | |
8334 | if (env->log.level & BPF_LOG_LEVEL2) | |
8335 | verbose(env, "propagating r%d\n", i); | |
8336 | err = mark_chain_precision(env, i); | |
8337 | if (err < 0) | |
8338 | return err; | |
8339 | } | |
8340 | ||
8341 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
8342 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
8343 | continue; | |
8344 | state_reg = &state->stack[i].spilled_ptr; | |
8345 | if (state_reg->type != SCALAR_VALUE || | |
8346 | !state_reg->precise) | |
8347 | continue; | |
8348 | if (env->log.level & BPF_LOG_LEVEL2) | |
8349 | verbose(env, "propagating fp%d\n", | |
8350 | (-i - 1) * BPF_REG_SIZE); | |
8351 | err = mark_chain_precision_stack(env, i); | |
8352 | if (err < 0) | |
8353 | return err; | |
8354 | } | |
8355 | return 0; | |
8356 | } | |
8357 | ||
2589726d AS |
8358 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
8359 | struct bpf_verifier_state *cur) | |
8360 | { | |
8361 | struct bpf_func_state *fold, *fcur; | |
8362 | int i, fr = cur->curframe; | |
8363 | ||
8364 | if (old->curframe != fr) | |
8365 | return false; | |
8366 | ||
8367 | fold = old->frame[fr]; | |
8368 | fcur = cur->frame[fr]; | |
8369 | for (i = 0; i < MAX_BPF_REG; i++) | |
8370 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
8371 | offsetof(struct bpf_reg_state, parent))) | |
8372 | return false; | |
8373 | return true; | |
8374 | } | |
8375 | ||
8376 | ||
58e2af8b | 8377 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 8378 | { |
58e2af8b | 8379 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 8380 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 8381 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 8382 | int i, j, err, states_cnt = 0; |
10d274e8 | 8383 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 8384 | |
b5dc0163 | 8385 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 8386 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
8387 | /* this 'insn_idx' instruction wasn't marked, so we will not |
8388 | * be doing state search here | |
8389 | */ | |
8390 | return 0; | |
8391 | ||
2589726d AS |
8392 | /* bpf progs typically have pruning point every 4 instructions |
8393 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
8394 | * Do not add new state for future pruning if the verifier hasn't seen | |
8395 | * at least 2 jumps and at least 8 instructions. | |
8396 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
8397 | * In tests that amounts to up to 50% reduction into total verifier | |
8398 | * memory consumption and 20% verifier time speedup. | |
8399 | */ | |
8400 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
8401 | env->insn_processed - env->prev_insn_processed >= 8) | |
8402 | add_new_state = true; | |
8403 | ||
a8f500af AS |
8404 | pprev = explored_state(env, insn_idx); |
8405 | sl = *pprev; | |
8406 | ||
9242b5f5 AS |
8407 | clean_live_states(env, insn_idx, cur); |
8408 | ||
a8f500af | 8409 | while (sl) { |
dc2a4ebc AS |
8410 | states_cnt++; |
8411 | if (sl->state.insn_idx != insn_idx) | |
8412 | goto next; | |
2589726d AS |
8413 | if (sl->state.branches) { |
8414 | if (states_maybe_looping(&sl->state, cur) && | |
8415 | states_equal(env, &sl->state, cur)) { | |
8416 | verbose_linfo(env, insn_idx, "; "); | |
8417 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
8418 | return -EINVAL; | |
8419 | } | |
8420 | /* if the verifier is processing a loop, avoid adding new state | |
8421 | * too often, since different loop iterations have distinct | |
8422 | * states and may not help future pruning. | |
8423 | * This threshold shouldn't be too low to make sure that | |
8424 | * a loop with large bound will be rejected quickly. | |
8425 | * The most abusive loop will be: | |
8426 | * r1 += 1 | |
8427 | * if r1 < 1000000 goto pc-2 | |
8428 | * 1M insn_procssed limit / 100 == 10k peak states. | |
8429 | * This threshold shouldn't be too high either, since states | |
8430 | * at the end of the loop are likely to be useful in pruning. | |
8431 | */ | |
8432 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
8433 | env->insn_processed - env->prev_insn_processed < 100) | |
8434 | add_new_state = false; | |
8435 | goto miss; | |
8436 | } | |
638f5b90 | 8437 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 8438 | sl->hit_cnt++; |
f1bca824 | 8439 | /* reached equivalent register/stack state, |
dc503a8a EC |
8440 | * prune the search. |
8441 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
8442 | * If we have any write marks in env->cur_state, they |
8443 | * will prevent corresponding reads in the continuation | |
8444 | * from reaching our parent (an explored_state). Our | |
8445 | * own state will get the read marks recorded, but | |
8446 | * they'll be immediately forgotten as we're pruning | |
8447 | * this state and will pop a new one. | |
f1bca824 | 8448 | */ |
f4d7e40a | 8449 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
8450 | |
8451 | /* if previous state reached the exit with precision and | |
8452 | * current state is equivalent to it (except precsion marks) | |
8453 | * the precision needs to be propagated back in | |
8454 | * the current state. | |
8455 | */ | |
8456 | err = err ? : push_jmp_history(env, cur); | |
8457 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
8458 | if (err) |
8459 | return err; | |
f1bca824 | 8460 | return 1; |
dc503a8a | 8461 | } |
2589726d AS |
8462 | miss: |
8463 | /* when new state is not going to be added do not increase miss count. | |
8464 | * Otherwise several loop iterations will remove the state | |
8465 | * recorded earlier. The goal of these heuristics is to have | |
8466 | * states from some iterations of the loop (some in the beginning | |
8467 | * and some at the end) to help pruning. | |
8468 | */ | |
8469 | if (add_new_state) | |
8470 | sl->miss_cnt++; | |
9f4686c4 AS |
8471 | /* heuristic to determine whether this state is beneficial |
8472 | * to keep checking from state equivalence point of view. | |
8473 | * Higher numbers increase max_states_per_insn and verification time, | |
8474 | * but do not meaningfully decrease insn_processed. | |
8475 | */ | |
8476 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
8477 | /* the state is unlikely to be useful. Remove it to | |
8478 | * speed up verification | |
8479 | */ | |
8480 | *pprev = sl->next; | |
8481 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
8482 | u32 br = sl->state.branches; |
8483 | ||
8484 | WARN_ONCE(br, | |
8485 | "BUG live_done but branches_to_explore %d\n", | |
8486 | br); | |
9f4686c4 AS |
8487 | free_verifier_state(&sl->state, false); |
8488 | kfree(sl); | |
8489 | env->peak_states--; | |
8490 | } else { | |
8491 | /* cannot free this state, since parentage chain may | |
8492 | * walk it later. Add it for free_list instead to | |
8493 | * be freed at the end of verification | |
8494 | */ | |
8495 | sl->next = env->free_list; | |
8496 | env->free_list = sl; | |
8497 | } | |
8498 | sl = *pprev; | |
8499 | continue; | |
8500 | } | |
dc2a4ebc | 8501 | next: |
9f4686c4 AS |
8502 | pprev = &sl->next; |
8503 | sl = *pprev; | |
f1bca824 AS |
8504 | } |
8505 | ||
06ee7115 AS |
8506 | if (env->max_states_per_insn < states_cnt) |
8507 | env->max_states_per_insn = states_cnt; | |
8508 | ||
2c78ee89 | 8509 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 8510 | return push_jmp_history(env, cur); |
ceefbc96 | 8511 | |
2589726d | 8512 | if (!add_new_state) |
b5dc0163 | 8513 | return push_jmp_history(env, cur); |
ceefbc96 | 8514 | |
2589726d AS |
8515 | /* There were no equivalent states, remember the current one. |
8516 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 8517 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 8518 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 8519 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
8520 | * again on the way to bpf_exit. |
8521 | * When looping the sl->state.branches will be > 0 and this state | |
8522 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 8523 | */ |
638f5b90 | 8524 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
8525 | if (!new_sl) |
8526 | return -ENOMEM; | |
06ee7115 AS |
8527 | env->total_states++; |
8528 | env->peak_states++; | |
2589726d AS |
8529 | env->prev_jmps_processed = env->jmps_processed; |
8530 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
8531 | |
8532 | /* add new state to the head of linked list */ | |
679c782d EC |
8533 | new = &new_sl->state; |
8534 | err = copy_verifier_state(new, cur); | |
1969db47 | 8535 | if (err) { |
679c782d | 8536 | free_verifier_state(new, false); |
1969db47 AS |
8537 | kfree(new_sl); |
8538 | return err; | |
8539 | } | |
dc2a4ebc | 8540 | new->insn_idx = insn_idx; |
2589726d AS |
8541 | WARN_ONCE(new->branches != 1, |
8542 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 8543 | |
2589726d | 8544 | cur->parent = new; |
b5dc0163 AS |
8545 | cur->first_insn_idx = insn_idx; |
8546 | clear_jmp_history(cur); | |
5d839021 AS |
8547 | new_sl->next = *explored_state(env, insn_idx); |
8548 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
8549 | /* connect new state to parentage chain. Current frame needs all |
8550 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
8551 | * to the stack implicitly by JITs) so in callers' frames connect just | |
8552 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
8553 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
8554 | * from callee with its full parentage chain, anyway. | |
8555 | */ | |
8e9cd9ce EC |
8556 | /* clear write marks in current state: the writes we did are not writes |
8557 | * our child did, so they don't screen off its reads from us. | |
8558 | * (There are no read marks in current state, because reads always mark | |
8559 | * their parent and current state never has children yet. Only | |
8560 | * explored_states can get read marks.) | |
8561 | */ | |
eea1c227 AS |
8562 | for (j = 0; j <= cur->curframe; j++) { |
8563 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
8564 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
8565 | for (i = 0; i < BPF_REG_FP; i++) | |
8566 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
8567 | } | |
f4d7e40a AS |
8568 | |
8569 | /* all stack frames are accessible from callee, clear them all */ | |
8570 | for (j = 0; j <= cur->curframe; j++) { | |
8571 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 8572 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 8573 | |
679c782d | 8574 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 8575 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
8576 | frame->stack[i].spilled_ptr.parent = |
8577 | &newframe->stack[i].spilled_ptr; | |
8578 | } | |
f4d7e40a | 8579 | } |
f1bca824 AS |
8580 | return 0; |
8581 | } | |
8582 | ||
c64b7983 JS |
8583 | /* Return true if it's OK to have the same insn return a different type. */ |
8584 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
8585 | { | |
8586 | switch (type) { | |
8587 | case PTR_TO_CTX: | |
8588 | case PTR_TO_SOCKET: | |
8589 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
8590 | case PTR_TO_SOCK_COMMON: |
8591 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
8592 | case PTR_TO_TCP_SOCK: |
8593 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 8594 | case PTR_TO_XDP_SOCK: |
2a02759e | 8595 | case PTR_TO_BTF_ID: |
b121b341 | 8596 | case PTR_TO_BTF_ID_OR_NULL: |
c64b7983 JS |
8597 | return false; |
8598 | default: | |
8599 | return true; | |
8600 | } | |
8601 | } | |
8602 | ||
8603 | /* If an instruction was previously used with particular pointer types, then we | |
8604 | * need to be careful to avoid cases such as the below, where it may be ok | |
8605 | * for one branch accessing the pointer, but not ok for the other branch: | |
8606 | * | |
8607 | * R1 = sock_ptr | |
8608 | * goto X; | |
8609 | * ... | |
8610 | * R1 = some_other_valid_ptr; | |
8611 | * goto X; | |
8612 | * ... | |
8613 | * R2 = *(u32 *)(R1 + 0); | |
8614 | */ | |
8615 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
8616 | { | |
8617 | return src != prev && (!reg_type_mismatch_ok(src) || | |
8618 | !reg_type_mismatch_ok(prev)); | |
8619 | } | |
8620 | ||
58e2af8b | 8621 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 8622 | { |
6f8a57cc | 8623 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 8624 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 8625 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 8626 | struct bpf_reg_state *regs; |
06ee7115 | 8627 | int insn_cnt = env->prog->len; |
17a52670 | 8628 | bool do_print_state = false; |
b5dc0163 | 8629 | int prev_insn_idx = -1; |
17a52670 | 8630 | |
17a52670 AS |
8631 | for (;;) { |
8632 | struct bpf_insn *insn; | |
8633 | u8 class; | |
8634 | int err; | |
8635 | ||
b5dc0163 | 8636 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 8637 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 8638 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 8639 | env->insn_idx, insn_cnt); |
17a52670 AS |
8640 | return -EFAULT; |
8641 | } | |
8642 | ||
c08435ec | 8643 | insn = &insns[env->insn_idx]; |
17a52670 AS |
8644 | class = BPF_CLASS(insn->code); |
8645 | ||
06ee7115 | 8646 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
8647 | verbose(env, |
8648 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 8649 | env->insn_processed); |
17a52670 AS |
8650 | return -E2BIG; |
8651 | } | |
8652 | ||
c08435ec | 8653 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
8654 | if (err < 0) |
8655 | return err; | |
8656 | if (err == 1) { | |
8657 | /* found equivalent state, can prune the search */ | |
06ee7115 | 8658 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 8659 | if (do_print_state) |
979d63d5 DB |
8660 | verbose(env, "\nfrom %d to %d%s: safe\n", |
8661 | env->prev_insn_idx, env->insn_idx, | |
8662 | env->cur_state->speculative ? | |
8663 | " (speculative execution)" : ""); | |
f1bca824 | 8664 | else |
c08435ec | 8665 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
8666 | } |
8667 | goto process_bpf_exit; | |
8668 | } | |
8669 | ||
c3494801 AS |
8670 | if (signal_pending(current)) |
8671 | return -EAGAIN; | |
8672 | ||
3c2ce60b DB |
8673 | if (need_resched()) |
8674 | cond_resched(); | |
8675 | ||
06ee7115 AS |
8676 | if (env->log.level & BPF_LOG_LEVEL2 || |
8677 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
8678 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 8679 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 8680 | else |
979d63d5 DB |
8681 | verbose(env, "\nfrom %d to %d%s:", |
8682 | env->prev_insn_idx, env->insn_idx, | |
8683 | env->cur_state->speculative ? | |
8684 | " (speculative execution)" : ""); | |
f4d7e40a | 8685 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
8686 | do_print_state = false; |
8687 | } | |
8688 | ||
06ee7115 | 8689 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
8690 | const struct bpf_insn_cbs cbs = { |
8691 | .cb_print = verbose, | |
abe08840 | 8692 | .private_data = env, |
7105e828 DB |
8693 | }; |
8694 | ||
c08435ec DB |
8695 | verbose_linfo(env, env->insn_idx, "; "); |
8696 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 8697 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
8698 | } |
8699 | ||
cae1927c | 8700 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
8701 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
8702 | env->prev_insn_idx); | |
cae1927c JK |
8703 | if (err) |
8704 | return err; | |
8705 | } | |
13a27dfc | 8706 | |
638f5b90 | 8707 | regs = cur_regs(env); |
51c39bb1 | 8708 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
b5dc0163 | 8709 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 8710 | |
17a52670 | 8711 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 8712 | err = check_alu_op(env, insn); |
17a52670 AS |
8713 | if (err) |
8714 | return err; | |
8715 | ||
8716 | } else if (class == BPF_LDX) { | |
3df126f3 | 8717 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
8718 | |
8719 | /* check for reserved fields is already done */ | |
8720 | ||
17a52670 | 8721 | /* check src operand */ |
dc503a8a | 8722 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8723 | if (err) |
8724 | return err; | |
8725 | ||
dc503a8a | 8726 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
8727 | if (err) |
8728 | return err; | |
8729 | ||
725f9dcd AS |
8730 | src_reg_type = regs[insn->src_reg].type; |
8731 | ||
17a52670 AS |
8732 | /* check that memory (src_reg + off) is readable, |
8733 | * the state of dst_reg will be updated by this func | |
8734 | */ | |
c08435ec DB |
8735 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
8736 | insn->off, BPF_SIZE(insn->code), | |
8737 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
8738 | if (err) |
8739 | return err; | |
8740 | ||
c08435ec | 8741 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
8742 | |
8743 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
8744 | /* saw a valid insn |
8745 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 8746 | * save type to validate intersecting paths |
9bac3d6d | 8747 | */ |
3df126f3 | 8748 | *prev_src_type = src_reg_type; |
9bac3d6d | 8749 | |
c64b7983 | 8750 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
8751 | /* ABuser program is trying to use the same insn |
8752 | * dst_reg = *(u32*) (src_reg + off) | |
8753 | * with different pointer types: | |
8754 | * src_reg == ctx in one branch and | |
8755 | * src_reg == stack|map in some other branch. | |
8756 | * Reject it. | |
8757 | */ | |
61bd5218 | 8758 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
8759 | return -EINVAL; |
8760 | } | |
8761 | ||
17a52670 | 8762 | } else if (class == BPF_STX) { |
3df126f3 | 8763 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 8764 | |
17a52670 | 8765 | if (BPF_MODE(insn->code) == BPF_XADD) { |
c08435ec | 8766 | err = check_xadd(env, env->insn_idx, insn); |
17a52670 AS |
8767 | if (err) |
8768 | return err; | |
c08435ec | 8769 | env->insn_idx++; |
17a52670 AS |
8770 | continue; |
8771 | } | |
8772 | ||
17a52670 | 8773 | /* check src1 operand */ |
dc503a8a | 8774 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8775 | if (err) |
8776 | return err; | |
8777 | /* check src2 operand */ | |
dc503a8a | 8778 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8779 | if (err) |
8780 | return err; | |
8781 | ||
d691f9e8 AS |
8782 | dst_reg_type = regs[insn->dst_reg].type; |
8783 | ||
17a52670 | 8784 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
8785 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
8786 | insn->off, BPF_SIZE(insn->code), | |
8787 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
8788 | if (err) |
8789 | return err; | |
8790 | ||
c08435ec | 8791 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
8792 | |
8793 | if (*prev_dst_type == NOT_INIT) { | |
8794 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 8795 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 8796 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
8797 | return -EINVAL; |
8798 | } | |
8799 | ||
17a52670 AS |
8800 | } else if (class == BPF_ST) { |
8801 | if (BPF_MODE(insn->code) != BPF_MEM || | |
8802 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 8803 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
8804 | return -EINVAL; |
8805 | } | |
8806 | /* check src operand */ | |
dc503a8a | 8807 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8808 | if (err) |
8809 | return err; | |
8810 | ||
f37a8cb8 | 8811 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 8812 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
8813 | insn->dst_reg, |
8814 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
8815 | return -EACCES; |
8816 | } | |
8817 | ||
17a52670 | 8818 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
8819 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
8820 | insn->off, BPF_SIZE(insn->code), | |
8821 | BPF_WRITE, -1, false); | |
17a52670 AS |
8822 | if (err) |
8823 | return err; | |
8824 | ||
092ed096 | 8825 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
8826 | u8 opcode = BPF_OP(insn->code); |
8827 | ||
2589726d | 8828 | env->jmps_processed++; |
17a52670 AS |
8829 | if (opcode == BPF_CALL) { |
8830 | if (BPF_SRC(insn->code) != BPF_K || | |
8831 | insn->off != 0 || | |
f4d7e40a AS |
8832 | (insn->src_reg != BPF_REG_0 && |
8833 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
8834 | insn->dst_reg != BPF_REG_0 || |
8835 | class == BPF_JMP32) { | |
61bd5218 | 8836 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
8837 | return -EINVAL; |
8838 | } | |
8839 | ||
d83525ca AS |
8840 | if (env->cur_state->active_spin_lock && |
8841 | (insn->src_reg == BPF_PSEUDO_CALL || | |
8842 | insn->imm != BPF_FUNC_spin_unlock)) { | |
8843 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
8844 | return -EINVAL; | |
8845 | } | |
f4d7e40a | 8846 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 8847 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 8848 | else |
c08435ec | 8849 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
8850 | if (err) |
8851 | return err; | |
8852 | ||
8853 | } else if (opcode == BPF_JA) { | |
8854 | if (BPF_SRC(insn->code) != BPF_K || | |
8855 | insn->imm != 0 || | |
8856 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
8857 | insn->dst_reg != BPF_REG_0 || |
8858 | class == BPF_JMP32) { | |
61bd5218 | 8859 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
8860 | return -EINVAL; |
8861 | } | |
8862 | ||
c08435ec | 8863 | env->insn_idx += insn->off + 1; |
17a52670 AS |
8864 | continue; |
8865 | ||
8866 | } else if (opcode == BPF_EXIT) { | |
8867 | if (BPF_SRC(insn->code) != BPF_K || | |
8868 | insn->imm != 0 || | |
8869 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
8870 | insn->dst_reg != BPF_REG_0 || |
8871 | class == BPF_JMP32) { | |
61bd5218 | 8872 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
8873 | return -EINVAL; |
8874 | } | |
8875 | ||
d83525ca AS |
8876 | if (env->cur_state->active_spin_lock) { |
8877 | verbose(env, "bpf_spin_unlock is missing\n"); | |
8878 | return -EINVAL; | |
8879 | } | |
8880 | ||
f4d7e40a AS |
8881 | if (state->curframe) { |
8882 | /* exit from nested function */ | |
c08435ec | 8883 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
8884 | if (err) |
8885 | return err; | |
8886 | do_print_state = true; | |
8887 | continue; | |
8888 | } | |
8889 | ||
fd978bf7 JS |
8890 | err = check_reference_leak(env); |
8891 | if (err) | |
8892 | return err; | |
8893 | ||
390ee7e2 AS |
8894 | err = check_return_code(env); |
8895 | if (err) | |
8896 | return err; | |
f1bca824 | 8897 | process_bpf_exit: |
2589726d | 8898 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 8899 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 8900 | &env->insn_idx, pop_log); |
638f5b90 AS |
8901 | if (err < 0) { |
8902 | if (err != -ENOENT) | |
8903 | return err; | |
17a52670 AS |
8904 | break; |
8905 | } else { | |
8906 | do_print_state = true; | |
8907 | continue; | |
8908 | } | |
8909 | } else { | |
c08435ec | 8910 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
8911 | if (err) |
8912 | return err; | |
8913 | } | |
8914 | } else if (class == BPF_LD) { | |
8915 | u8 mode = BPF_MODE(insn->code); | |
8916 | ||
8917 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
8918 | err = check_ld_abs(env, insn); |
8919 | if (err) | |
8920 | return err; | |
8921 | ||
17a52670 AS |
8922 | } else if (mode == BPF_IMM) { |
8923 | err = check_ld_imm(env, insn); | |
8924 | if (err) | |
8925 | return err; | |
8926 | ||
c08435ec | 8927 | env->insn_idx++; |
51c39bb1 | 8928 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
17a52670 | 8929 | } else { |
61bd5218 | 8930 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
8931 | return -EINVAL; |
8932 | } | |
8933 | } else { | |
61bd5218 | 8934 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
8935 | return -EINVAL; |
8936 | } | |
8937 | ||
c08435ec | 8938 | env->insn_idx++; |
17a52670 AS |
8939 | } |
8940 | ||
8941 | return 0; | |
8942 | } | |
8943 | ||
56f668df MKL |
8944 | static int check_map_prealloc(struct bpf_map *map) |
8945 | { | |
8946 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
8947 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
8948 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
8949 | !(map->map_flags & BPF_F_NO_PREALLOC); |
8950 | } | |
8951 | ||
d83525ca AS |
8952 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
8953 | { | |
8954 | switch (type) { | |
8955 | case BPF_PROG_TYPE_KPROBE: | |
8956 | case BPF_PROG_TYPE_TRACEPOINT: | |
8957 | case BPF_PROG_TYPE_PERF_EVENT: | |
8958 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
8959 | return true; | |
8960 | default: | |
8961 | return false; | |
8962 | } | |
8963 | } | |
8964 | ||
94dacdbd TG |
8965 | static bool is_preallocated_map(struct bpf_map *map) |
8966 | { | |
8967 | if (!check_map_prealloc(map)) | |
8968 | return false; | |
8969 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
8970 | return false; | |
8971 | return true; | |
8972 | } | |
8973 | ||
61bd5218 JK |
8974 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
8975 | struct bpf_map *map, | |
fdc15d38 AS |
8976 | struct bpf_prog *prog) |
8977 | ||
8978 | { | |
94dacdbd TG |
8979 | /* |
8980 | * Validate that trace type programs use preallocated hash maps. | |
8981 | * | |
8982 | * For programs attached to PERF events this is mandatory as the | |
8983 | * perf NMI can hit any arbitrary code sequence. | |
8984 | * | |
8985 | * All other trace types using preallocated hash maps are unsafe as | |
8986 | * well because tracepoint or kprobes can be inside locked regions | |
8987 | * of the memory allocator or at a place where a recursion into the | |
8988 | * memory allocator would see inconsistent state. | |
8989 | * | |
2ed905c5 TG |
8990 | * On RT enabled kernels run-time allocation of all trace type |
8991 | * programs is strictly prohibited due to lock type constraints. On | |
8992 | * !RT kernels it is allowed for backwards compatibility reasons for | |
8993 | * now, but warnings are emitted so developers are made aware of | |
8994 | * the unsafety and can fix their programs before this is enforced. | |
56f668df | 8995 | */ |
94dacdbd TG |
8996 | if (is_tracing_prog_type(prog->type) && !is_preallocated_map(map)) { |
8997 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
61bd5218 | 8998 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
8999 | return -EINVAL; |
9000 | } | |
2ed905c5 TG |
9001 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
9002 | verbose(env, "trace type programs can only use preallocated hash map\n"); | |
9003 | return -EINVAL; | |
9004 | } | |
94dacdbd TG |
9005 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
9006 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
fdc15d38 | 9007 | } |
a3884572 | 9008 | |
d83525ca AS |
9009 | if ((is_tracing_prog_type(prog->type) || |
9010 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && | |
9011 | map_value_has_spin_lock(map)) { | |
9012 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
9013 | return -EINVAL; | |
9014 | } | |
9015 | ||
a3884572 | 9016 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 9017 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
9018 | verbose(env, "offload device mismatch between prog and map\n"); |
9019 | return -EINVAL; | |
9020 | } | |
9021 | ||
85d33df3 MKL |
9022 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
9023 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
9024 | return -EINVAL; | |
9025 | } | |
9026 | ||
fdc15d38 AS |
9027 | return 0; |
9028 | } | |
9029 | ||
b741f163 RG |
9030 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
9031 | { | |
9032 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
9033 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
9034 | } | |
9035 | ||
0246e64d AS |
9036 | /* look for pseudo eBPF instructions that access map FDs and |
9037 | * replace them with actual map pointers | |
9038 | */ | |
58e2af8b | 9039 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
9040 | { |
9041 | struct bpf_insn *insn = env->prog->insnsi; | |
9042 | int insn_cnt = env->prog->len; | |
fdc15d38 | 9043 | int i, j, err; |
0246e64d | 9044 | |
f1f7714e | 9045 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
9046 | if (err) |
9047 | return err; | |
9048 | ||
0246e64d | 9049 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 9050 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 9051 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 9052 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
9053 | return -EINVAL; |
9054 | } | |
9055 | ||
d691f9e8 AS |
9056 | if (BPF_CLASS(insn->code) == BPF_STX && |
9057 | ((BPF_MODE(insn->code) != BPF_MEM && | |
9058 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 9059 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
9060 | return -EINVAL; |
9061 | } | |
9062 | ||
0246e64d | 9063 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 9064 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
9065 | struct bpf_map *map; |
9066 | struct fd f; | |
d8eca5bb | 9067 | u64 addr; |
0246e64d AS |
9068 | |
9069 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
9070 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
9071 | insn[1].off != 0) { | |
61bd5218 | 9072 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
9073 | return -EINVAL; |
9074 | } | |
9075 | ||
d8eca5bb | 9076 | if (insn[0].src_reg == 0) |
0246e64d AS |
9077 | /* valid generic load 64-bit imm */ |
9078 | goto next_insn; | |
9079 | ||
d8eca5bb DB |
9080 | /* In final convert_pseudo_ld_imm64() step, this is |
9081 | * converted into regular 64-bit imm load insn. | |
9082 | */ | |
9083 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
9084 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
9085 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
9086 | insn[1].imm != 0)) { | |
9087 | verbose(env, | |
9088 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
9089 | return -EINVAL; |
9090 | } | |
9091 | ||
20182390 | 9092 | f = fdget(insn[0].imm); |
c2101297 | 9093 | map = __bpf_map_get(f); |
0246e64d | 9094 | if (IS_ERR(map)) { |
61bd5218 | 9095 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 9096 | insn[0].imm); |
0246e64d AS |
9097 | return PTR_ERR(map); |
9098 | } | |
9099 | ||
61bd5218 | 9100 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
9101 | if (err) { |
9102 | fdput(f); | |
9103 | return err; | |
9104 | } | |
9105 | ||
d8eca5bb DB |
9106 | aux = &env->insn_aux_data[i]; |
9107 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
9108 | addr = (unsigned long)map; | |
9109 | } else { | |
9110 | u32 off = insn[1].imm; | |
9111 | ||
9112 | if (off >= BPF_MAX_VAR_OFF) { | |
9113 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
9114 | fdput(f); | |
9115 | return -EINVAL; | |
9116 | } | |
9117 | ||
9118 | if (!map->ops->map_direct_value_addr) { | |
9119 | verbose(env, "no direct value access support for this map type\n"); | |
9120 | fdput(f); | |
9121 | return -EINVAL; | |
9122 | } | |
9123 | ||
9124 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
9125 | if (err) { | |
9126 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
9127 | map->value_size, off); | |
9128 | fdput(f); | |
9129 | return err; | |
9130 | } | |
9131 | ||
9132 | aux->map_off = off; | |
9133 | addr += off; | |
9134 | } | |
9135 | ||
9136 | insn[0].imm = (u32)addr; | |
9137 | insn[1].imm = addr >> 32; | |
0246e64d AS |
9138 | |
9139 | /* check whether we recorded this map already */ | |
d8eca5bb | 9140 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 9141 | if (env->used_maps[j] == map) { |
d8eca5bb | 9142 | aux->map_index = j; |
0246e64d AS |
9143 | fdput(f); |
9144 | goto next_insn; | |
9145 | } | |
d8eca5bb | 9146 | } |
0246e64d AS |
9147 | |
9148 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
9149 | fdput(f); | |
9150 | return -E2BIG; | |
9151 | } | |
9152 | ||
0246e64d AS |
9153 | /* hold the map. If the program is rejected by verifier, |
9154 | * the map will be released by release_maps() or it | |
9155 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 9156 | * and all maps are released in free_used_maps() |
0246e64d | 9157 | */ |
1e0bd5a0 | 9158 | bpf_map_inc(map); |
d8eca5bb DB |
9159 | |
9160 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
9161 | env->used_maps[env->used_map_cnt++] = map; |
9162 | ||
b741f163 | 9163 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 9164 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 9165 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
9166 | fdput(f); |
9167 | return -EBUSY; | |
9168 | } | |
9169 | ||
0246e64d AS |
9170 | fdput(f); |
9171 | next_insn: | |
9172 | insn++; | |
9173 | i++; | |
5e581dad DB |
9174 | continue; |
9175 | } | |
9176 | ||
9177 | /* Basic sanity check before we invest more work here. */ | |
9178 | if (!bpf_opcode_in_insntable(insn->code)) { | |
9179 | verbose(env, "unknown opcode %02x\n", insn->code); | |
9180 | return -EINVAL; | |
0246e64d AS |
9181 | } |
9182 | } | |
9183 | ||
9184 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
9185 | * 'struct bpf_map *' into a register instead of user map_fd. | |
9186 | * These pointers will be used later by verifier to validate map access. | |
9187 | */ | |
9188 | return 0; | |
9189 | } | |
9190 | ||
9191 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 9192 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 9193 | { |
a2ea0746 DB |
9194 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
9195 | env->used_map_cnt); | |
0246e64d AS |
9196 | } |
9197 | ||
9198 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 9199 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
9200 | { |
9201 | struct bpf_insn *insn = env->prog->insnsi; | |
9202 | int insn_cnt = env->prog->len; | |
9203 | int i; | |
9204 | ||
9205 | for (i = 0; i < insn_cnt; i++, insn++) | |
9206 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
9207 | insn->src_reg = 0; | |
9208 | } | |
9209 | ||
8041902d AS |
9210 | /* single env->prog->insni[off] instruction was replaced with the range |
9211 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
9212 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
9213 | */ | |
b325fbca JW |
9214 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
9215 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d AS |
9216 | { |
9217 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
b325fbca JW |
9218 | struct bpf_insn *insn = new_prog->insnsi; |
9219 | u32 prog_len; | |
c131187d | 9220 | int i; |
8041902d | 9221 | |
b325fbca JW |
9222 | /* aux info at OFF always needs adjustment, no matter fast path |
9223 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
9224 | * original insn at old prog. | |
9225 | */ | |
9226 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
9227 | ||
8041902d AS |
9228 | if (cnt == 1) |
9229 | return 0; | |
b325fbca | 9230 | prog_len = new_prog->len; |
fad953ce KC |
9231 | new_data = vzalloc(array_size(prog_len, |
9232 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
9233 | if (!new_data) |
9234 | return -ENOMEM; | |
9235 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
9236 | memcpy(new_data + off + cnt - 1, old_data + off, | |
9237 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 9238 | for (i = off; i < off + cnt - 1; i++) { |
51c39bb1 | 9239 | new_data[i].seen = env->pass_cnt; |
b325fbca JW |
9240 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
9241 | } | |
8041902d AS |
9242 | env->insn_aux_data = new_data; |
9243 | vfree(old_data); | |
9244 | return 0; | |
9245 | } | |
9246 | ||
cc8b0b92 AS |
9247 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
9248 | { | |
9249 | int i; | |
9250 | ||
9251 | if (len == 1) | |
9252 | return; | |
4cb3d99c JW |
9253 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
9254 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 9255 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 9256 | continue; |
9c8105bd | 9257 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
9258 | } |
9259 | } | |
9260 | ||
8041902d AS |
9261 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
9262 | const struct bpf_insn *patch, u32 len) | |
9263 | { | |
9264 | struct bpf_prog *new_prog; | |
9265 | ||
9266 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
9267 | if (IS_ERR(new_prog)) { |
9268 | if (PTR_ERR(new_prog) == -ERANGE) | |
9269 | verbose(env, | |
9270 | "insn %d cannot be patched due to 16-bit range\n", | |
9271 | env->insn_aux_data[off].orig_idx); | |
8041902d | 9272 | return NULL; |
4f73379e | 9273 | } |
b325fbca | 9274 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
8041902d | 9275 | return NULL; |
cc8b0b92 | 9276 | adjust_subprog_starts(env, off, len); |
8041902d AS |
9277 | return new_prog; |
9278 | } | |
9279 | ||
52875a04 JK |
9280 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
9281 | u32 off, u32 cnt) | |
9282 | { | |
9283 | int i, j; | |
9284 | ||
9285 | /* find first prog starting at or after off (first to remove) */ | |
9286 | for (i = 0; i < env->subprog_cnt; i++) | |
9287 | if (env->subprog_info[i].start >= off) | |
9288 | break; | |
9289 | /* find first prog starting at or after off + cnt (first to stay) */ | |
9290 | for (j = i; j < env->subprog_cnt; j++) | |
9291 | if (env->subprog_info[j].start >= off + cnt) | |
9292 | break; | |
9293 | /* if j doesn't start exactly at off + cnt, we are just removing | |
9294 | * the front of previous prog | |
9295 | */ | |
9296 | if (env->subprog_info[j].start != off + cnt) | |
9297 | j--; | |
9298 | ||
9299 | if (j > i) { | |
9300 | struct bpf_prog_aux *aux = env->prog->aux; | |
9301 | int move; | |
9302 | ||
9303 | /* move fake 'exit' subprog as well */ | |
9304 | move = env->subprog_cnt + 1 - j; | |
9305 | ||
9306 | memmove(env->subprog_info + i, | |
9307 | env->subprog_info + j, | |
9308 | sizeof(*env->subprog_info) * move); | |
9309 | env->subprog_cnt -= j - i; | |
9310 | ||
9311 | /* remove func_info */ | |
9312 | if (aux->func_info) { | |
9313 | move = aux->func_info_cnt - j; | |
9314 | ||
9315 | memmove(aux->func_info + i, | |
9316 | aux->func_info + j, | |
9317 | sizeof(*aux->func_info) * move); | |
9318 | aux->func_info_cnt -= j - i; | |
9319 | /* func_info->insn_off is set after all code rewrites, | |
9320 | * in adjust_btf_func() - no need to adjust | |
9321 | */ | |
9322 | } | |
9323 | } else { | |
9324 | /* convert i from "first prog to remove" to "first to adjust" */ | |
9325 | if (env->subprog_info[i].start == off) | |
9326 | i++; | |
9327 | } | |
9328 | ||
9329 | /* update fake 'exit' subprog as well */ | |
9330 | for (; i <= env->subprog_cnt; i++) | |
9331 | env->subprog_info[i].start -= cnt; | |
9332 | ||
9333 | return 0; | |
9334 | } | |
9335 | ||
9336 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
9337 | u32 cnt) | |
9338 | { | |
9339 | struct bpf_prog *prog = env->prog; | |
9340 | u32 i, l_off, l_cnt, nr_linfo; | |
9341 | struct bpf_line_info *linfo; | |
9342 | ||
9343 | nr_linfo = prog->aux->nr_linfo; | |
9344 | if (!nr_linfo) | |
9345 | return 0; | |
9346 | ||
9347 | linfo = prog->aux->linfo; | |
9348 | ||
9349 | /* find first line info to remove, count lines to be removed */ | |
9350 | for (i = 0; i < nr_linfo; i++) | |
9351 | if (linfo[i].insn_off >= off) | |
9352 | break; | |
9353 | ||
9354 | l_off = i; | |
9355 | l_cnt = 0; | |
9356 | for (; i < nr_linfo; i++) | |
9357 | if (linfo[i].insn_off < off + cnt) | |
9358 | l_cnt++; | |
9359 | else | |
9360 | break; | |
9361 | ||
9362 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
9363 | * last removed linfo. prog is already modified, so prog->len == off | |
9364 | * means no live instructions after (tail of the program was removed). | |
9365 | */ | |
9366 | if (prog->len != off && l_cnt && | |
9367 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
9368 | l_cnt--; | |
9369 | linfo[--i].insn_off = off + cnt; | |
9370 | } | |
9371 | ||
9372 | /* remove the line info which refer to the removed instructions */ | |
9373 | if (l_cnt) { | |
9374 | memmove(linfo + l_off, linfo + i, | |
9375 | sizeof(*linfo) * (nr_linfo - i)); | |
9376 | ||
9377 | prog->aux->nr_linfo -= l_cnt; | |
9378 | nr_linfo = prog->aux->nr_linfo; | |
9379 | } | |
9380 | ||
9381 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
9382 | for (i = l_off; i < nr_linfo; i++) | |
9383 | linfo[i].insn_off -= cnt; | |
9384 | ||
9385 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
9386 | for (i = 0; i <= env->subprog_cnt; i++) | |
9387 | if (env->subprog_info[i].linfo_idx > l_off) { | |
9388 | /* program may have started in the removed region but | |
9389 | * may not be fully removed | |
9390 | */ | |
9391 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
9392 | env->subprog_info[i].linfo_idx -= l_cnt; | |
9393 | else | |
9394 | env->subprog_info[i].linfo_idx = l_off; | |
9395 | } | |
9396 | ||
9397 | return 0; | |
9398 | } | |
9399 | ||
9400 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
9401 | { | |
9402 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
9403 | unsigned int orig_prog_len = env->prog->len; | |
9404 | int err; | |
9405 | ||
08ca90af JK |
9406 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9407 | bpf_prog_offload_remove_insns(env, off, cnt); | |
9408 | ||
52875a04 JK |
9409 | err = bpf_remove_insns(env->prog, off, cnt); |
9410 | if (err) | |
9411 | return err; | |
9412 | ||
9413 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
9414 | if (err) | |
9415 | return err; | |
9416 | ||
9417 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
9418 | if (err) | |
9419 | return err; | |
9420 | ||
9421 | memmove(aux_data + off, aux_data + off + cnt, | |
9422 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
9423 | ||
9424 | return 0; | |
9425 | } | |
9426 | ||
2a5418a1 DB |
9427 | /* The verifier does more data flow analysis than llvm and will not |
9428 | * explore branches that are dead at run time. Malicious programs can | |
9429 | * have dead code too. Therefore replace all dead at-run-time code | |
9430 | * with 'ja -1'. | |
9431 | * | |
9432 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
9433 | * program and through another bug we would manage to jump there, then | |
9434 | * we'd execute beyond program memory otherwise. Returning exception | |
9435 | * code also wouldn't work since we can have subprogs where the dead | |
9436 | * code could be located. | |
c131187d AS |
9437 | */ |
9438 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
9439 | { | |
9440 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 9441 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
9442 | struct bpf_insn *insn = env->prog->insnsi; |
9443 | const int insn_cnt = env->prog->len; | |
9444 | int i; | |
9445 | ||
9446 | for (i = 0; i < insn_cnt; i++) { | |
9447 | if (aux_data[i].seen) | |
9448 | continue; | |
2a5418a1 | 9449 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
9450 | } |
9451 | } | |
9452 | ||
e2ae4ca2 JK |
9453 | static bool insn_is_cond_jump(u8 code) |
9454 | { | |
9455 | u8 op; | |
9456 | ||
092ed096 JW |
9457 | if (BPF_CLASS(code) == BPF_JMP32) |
9458 | return true; | |
9459 | ||
e2ae4ca2 JK |
9460 | if (BPF_CLASS(code) != BPF_JMP) |
9461 | return false; | |
9462 | ||
9463 | op = BPF_OP(code); | |
9464 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
9465 | } | |
9466 | ||
9467 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
9468 | { | |
9469 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
9470 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
9471 | struct bpf_insn *insn = env->prog->insnsi; | |
9472 | const int insn_cnt = env->prog->len; | |
9473 | int i; | |
9474 | ||
9475 | for (i = 0; i < insn_cnt; i++, insn++) { | |
9476 | if (!insn_is_cond_jump(insn->code)) | |
9477 | continue; | |
9478 | ||
9479 | if (!aux_data[i + 1].seen) | |
9480 | ja.off = insn->off; | |
9481 | else if (!aux_data[i + 1 + insn->off].seen) | |
9482 | ja.off = 0; | |
9483 | else | |
9484 | continue; | |
9485 | ||
08ca90af JK |
9486 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9487 | bpf_prog_offload_replace_insn(env, i, &ja); | |
9488 | ||
e2ae4ca2 JK |
9489 | memcpy(insn, &ja, sizeof(ja)); |
9490 | } | |
9491 | } | |
9492 | ||
52875a04 JK |
9493 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
9494 | { | |
9495 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
9496 | int insn_cnt = env->prog->len; | |
9497 | int i, err; | |
9498 | ||
9499 | for (i = 0; i < insn_cnt; i++) { | |
9500 | int j; | |
9501 | ||
9502 | j = 0; | |
9503 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
9504 | j++; | |
9505 | if (!j) | |
9506 | continue; | |
9507 | ||
9508 | err = verifier_remove_insns(env, i, j); | |
9509 | if (err) | |
9510 | return err; | |
9511 | insn_cnt = env->prog->len; | |
9512 | } | |
9513 | ||
9514 | return 0; | |
9515 | } | |
9516 | ||
a1b14abc JK |
9517 | static int opt_remove_nops(struct bpf_verifier_env *env) |
9518 | { | |
9519 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
9520 | struct bpf_insn *insn = env->prog->insnsi; | |
9521 | int insn_cnt = env->prog->len; | |
9522 | int i, err; | |
9523 | ||
9524 | for (i = 0; i < insn_cnt; i++) { | |
9525 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
9526 | continue; | |
9527 | ||
9528 | err = verifier_remove_insns(env, i, 1); | |
9529 | if (err) | |
9530 | return err; | |
9531 | insn_cnt--; | |
9532 | i--; | |
9533 | } | |
9534 | ||
9535 | return 0; | |
9536 | } | |
9537 | ||
d6c2308c JW |
9538 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
9539 | const union bpf_attr *attr) | |
a4b1d3c1 | 9540 | { |
d6c2308c | 9541 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 9542 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 9543 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 9544 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 9545 | struct bpf_prog *new_prog; |
d6c2308c | 9546 | bool rnd_hi32; |
a4b1d3c1 | 9547 | |
d6c2308c | 9548 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 9549 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
9550 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
9551 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
9552 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
9553 | for (i = 0; i < len; i++) { |
9554 | int adj_idx = i + delta; | |
9555 | struct bpf_insn insn; | |
9556 | ||
d6c2308c JW |
9557 | insn = insns[adj_idx]; |
9558 | if (!aux[adj_idx].zext_dst) { | |
9559 | u8 code, class; | |
9560 | u32 imm_rnd; | |
9561 | ||
9562 | if (!rnd_hi32) | |
9563 | continue; | |
9564 | ||
9565 | code = insn.code; | |
9566 | class = BPF_CLASS(code); | |
9567 | if (insn_no_def(&insn)) | |
9568 | continue; | |
9569 | ||
9570 | /* NOTE: arg "reg" (the fourth one) is only used for | |
9571 | * BPF_STX which has been ruled out in above | |
9572 | * check, it is safe to pass NULL here. | |
9573 | */ | |
9574 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { | |
9575 | if (class == BPF_LD && | |
9576 | BPF_MODE(code) == BPF_IMM) | |
9577 | i++; | |
9578 | continue; | |
9579 | } | |
9580 | ||
9581 | /* ctx load could be transformed into wider load. */ | |
9582 | if (class == BPF_LDX && | |
9583 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
9584 | continue; | |
9585 | ||
9586 | imm_rnd = get_random_int(); | |
9587 | rnd_hi32_patch[0] = insn; | |
9588 | rnd_hi32_patch[1].imm = imm_rnd; | |
9589 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; | |
9590 | patch = rnd_hi32_patch; | |
9591 | patch_len = 4; | |
9592 | goto apply_patch_buffer; | |
9593 | } | |
9594 | ||
9595 | if (!bpf_jit_needs_zext()) | |
a4b1d3c1 JW |
9596 | continue; |
9597 | ||
a4b1d3c1 JW |
9598 | zext_patch[0] = insn; |
9599 | zext_patch[1].dst_reg = insn.dst_reg; | |
9600 | zext_patch[1].src_reg = insn.dst_reg; | |
d6c2308c JW |
9601 | patch = zext_patch; |
9602 | patch_len = 2; | |
9603 | apply_patch_buffer: | |
9604 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
9605 | if (!new_prog) |
9606 | return -ENOMEM; | |
9607 | env->prog = new_prog; | |
9608 | insns = new_prog->insnsi; | |
9609 | aux = env->insn_aux_data; | |
d6c2308c | 9610 | delta += patch_len - 1; |
a4b1d3c1 JW |
9611 | } |
9612 | ||
9613 | return 0; | |
9614 | } | |
9615 | ||
c64b7983 JS |
9616 | /* convert load instructions that access fields of a context type into a |
9617 | * sequence of instructions that access fields of the underlying structure: | |
9618 | * struct __sk_buff -> struct sk_buff | |
9619 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 9620 | */ |
58e2af8b | 9621 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 9622 | { |
00176a34 | 9623 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 9624 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 9625 | const int insn_cnt = env->prog->len; |
36bbef52 | 9626 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 9627 | u32 target_size, size_default, off; |
9bac3d6d | 9628 | struct bpf_prog *new_prog; |
d691f9e8 | 9629 | enum bpf_access_type type; |
f96da094 | 9630 | bool is_narrower_load; |
9bac3d6d | 9631 | |
b09928b9 DB |
9632 | if (ops->gen_prologue || env->seen_direct_write) { |
9633 | if (!ops->gen_prologue) { | |
9634 | verbose(env, "bpf verifier is misconfigured\n"); | |
9635 | return -EINVAL; | |
9636 | } | |
36bbef52 DB |
9637 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
9638 | env->prog); | |
9639 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 9640 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
9641 | return -EINVAL; |
9642 | } else if (cnt) { | |
8041902d | 9643 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
9644 | if (!new_prog) |
9645 | return -ENOMEM; | |
8041902d | 9646 | |
36bbef52 | 9647 | env->prog = new_prog; |
3df126f3 | 9648 | delta += cnt - 1; |
36bbef52 DB |
9649 | } |
9650 | } | |
9651 | ||
c64b7983 | 9652 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
9653 | return 0; |
9654 | ||
3df126f3 | 9655 | insn = env->prog->insnsi + delta; |
36bbef52 | 9656 | |
9bac3d6d | 9657 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
9658 | bpf_convert_ctx_access_t convert_ctx_access; |
9659 | ||
62c7989b DB |
9660 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
9661 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
9662 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 9663 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 9664 | type = BPF_READ; |
62c7989b DB |
9665 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
9666 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
9667 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 9668 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
9669 | type = BPF_WRITE; |
9670 | else | |
9bac3d6d AS |
9671 | continue; |
9672 | ||
af86ca4e AS |
9673 | if (type == BPF_WRITE && |
9674 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
9675 | struct bpf_insn patch[] = { | |
9676 | /* Sanitize suspicious stack slot with zero. | |
9677 | * There are no memory dependencies for this store, | |
9678 | * since it's only using frame pointer and immediate | |
9679 | * constant of zero | |
9680 | */ | |
9681 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
9682 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
9683 | 0), | |
9684 | /* the original STX instruction will immediately | |
9685 | * overwrite the same stack slot with appropriate value | |
9686 | */ | |
9687 | *insn, | |
9688 | }; | |
9689 | ||
9690 | cnt = ARRAY_SIZE(patch); | |
9691 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
9692 | if (!new_prog) | |
9693 | return -ENOMEM; | |
9694 | ||
9695 | delta += cnt - 1; | |
9696 | env->prog = new_prog; | |
9697 | insn = new_prog->insnsi + i + delta; | |
9698 | continue; | |
9699 | } | |
9700 | ||
c64b7983 JS |
9701 | switch (env->insn_aux_data[i + delta].ptr_type) { |
9702 | case PTR_TO_CTX: | |
9703 | if (!ops->convert_ctx_access) | |
9704 | continue; | |
9705 | convert_ctx_access = ops->convert_ctx_access; | |
9706 | break; | |
9707 | case PTR_TO_SOCKET: | |
46f8bc92 | 9708 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
9709 | convert_ctx_access = bpf_sock_convert_ctx_access; |
9710 | break; | |
655a51e5 MKL |
9711 | case PTR_TO_TCP_SOCK: |
9712 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
9713 | break; | |
fada7fdc JL |
9714 | case PTR_TO_XDP_SOCK: |
9715 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
9716 | break; | |
2a02759e | 9717 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
9718 | if (type == BPF_READ) { |
9719 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
9720 | BPF_SIZE((insn)->code); | |
9721 | env->prog->aux->num_exentries++; | |
9722 | } else if (env->prog->type != BPF_PROG_TYPE_STRUCT_OPS) { | |
2a02759e AS |
9723 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
9724 | return -EINVAL; | |
9725 | } | |
2a02759e | 9726 | continue; |
c64b7983 | 9727 | default: |
9bac3d6d | 9728 | continue; |
c64b7983 | 9729 | } |
9bac3d6d | 9730 | |
31fd8581 | 9731 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 9732 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
9733 | |
9734 | /* If the read access is a narrower load of the field, | |
9735 | * convert to a 4/8-byte load, to minimum program type specific | |
9736 | * convert_ctx_access changes. If conversion is successful, | |
9737 | * we will apply proper mask to the result. | |
9738 | */ | |
f96da094 | 9739 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
9740 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
9741 | off = insn->off; | |
31fd8581 | 9742 | if (is_narrower_load) { |
f96da094 DB |
9743 | u8 size_code; |
9744 | ||
9745 | if (type == BPF_WRITE) { | |
61bd5218 | 9746 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
9747 | return -EINVAL; |
9748 | } | |
31fd8581 | 9749 | |
f96da094 | 9750 | size_code = BPF_H; |
31fd8581 YS |
9751 | if (ctx_field_size == 4) |
9752 | size_code = BPF_W; | |
9753 | else if (ctx_field_size == 8) | |
9754 | size_code = BPF_DW; | |
f96da094 | 9755 | |
bc23105c | 9756 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
9757 | insn->code = BPF_LDX | BPF_MEM | size_code; |
9758 | } | |
f96da094 DB |
9759 | |
9760 | target_size = 0; | |
c64b7983 JS |
9761 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
9762 | &target_size); | |
f96da094 DB |
9763 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
9764 | (ctx_field_size && !target_size)) { | |
61bd5218 | 9765 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
9766 | return -EINVAL; |
9767 | } | |
f96da094 DB |
9768 | |
9769 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
9770 | u8 shift = bpf_ctx_narrow_access_offset( |
9771 | off, size, size_default) * 8; | |
46f53a65 AI |
9772 | if (ctx_field_size <= 4) { |
9773 | if (shift) | |
9774 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
9775 | insn->dst_reg, | |
9776 | shift); | |
31fd8581 | 9777 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 9778 | (1 << size * 8) - 1); |
46f53a65 AI |
9779 | } else { |
9780 | if (shift) | |
9781 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
9782 | insn->dst_reg, | |
9783 | shift); | |
31fd8581 | 9784 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 9785 | (1ULL << size * 8) - 1); |
46f53a65 | 9786 | } |
31fd8581 | 9787 | } |
9bac3d6d | 9788 | |
8041902d | 9789 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
9790 | if (!new_prog) |
9791 | return -ENOMEM; | |
9792 | ||
3df126f3 | 9793 | delta += cnt - 1; |
9bac3d6d AS |
9794 | |
9795 | /* keep walking new program and skip insns we just inserted */ | |
9796 | env->prog = new_prog; | |
3df126f3 | 9797 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
9798 | } |
9799 | ||
9800 | return 0; | |
9801 | } | |
9802 | ||
1c2a088a AS |
9803 | static int jit_subprogs(struct bpf_verifier_env *env) |
9804 | { | |
9805 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
9806 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
7105e828 | 9807 | struct bpf_insn *insn; |
1c2a088a | 9808 | void *old_bpf_func; |
c454a46b | 9809 | int err; |
1c2a088a | 9810 | |
f910cefa | 9811 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
9812 | return 0; |
9813 | ||
7105e828 | 9814 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
9815 | if (insn->code != (BPF_JMP | BPF_CALL) || |
9816 | insn->src_reg != BPF_PSEUDO_CALL) | |
9817 | continue; | |
c7a89784 DB |
9818 | /* Upon error here we cannot fall back to interpreter but |
9819 | * need a hard reject of the program. Thus -EFAULT is | |
9820 | * propagated in any case. | |
9821 | */ | |
1c2a088a AS |
9822 | subprog = find_subprog(env, i + insn->imm + 1); |
9823 | if (subprog < 0) { | |
9824 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
9825 | i + insn->imm + 1); | |
9826 | return -EFAULT; | |
9827 | } | |
9828 | /* temporarily remember subprog id inside insn instead of | |
9829 | * aux_data, since next loop will split up all insns into funcs | |
9830 | */ | |
f910cefa | 9831 | insn->off = subprog; |
1c2a088a AS |
9832 | /* remember original imm in case JIT fails and fallback |
9833 | * to interpreter will be needed | |
9834 | */ | |
9835 | env->insn_aux_data[i].call_imm = insn->imm; | |
9836 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
9837 | insn->imm = 1; | |
9838 | } | |
9839 | ||
c454a46b MKL |
9840 | err = bpf_prog_alloc_jited_linfo(prog); |
9841 | if (err) | |
9842 | goto out_undo_insn; | |
9843 | ||
9844 | err = -ENOMEM; | |
6396bb22 | 9845 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 9846 | if (!func) |
c7a89784 | 9847 | goto out_undo_insn; |
1c2a088a | 9848 | |
f910cefa | 9849 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 9850 | subprog_start = subprog_end; |
4cb3d99c | 9851 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
9852 | |
9853 | len = subprog_end - subprog_start; | |
492ecee8 AS |
9854 | /* BPF_PROG_RUN doesn't call subprogs directly, |
9855 | * hence main prog stats include the runtime of subprogs. | |
9856 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
9857 | * func[i]->aux->stats will never be accessed and stays NULL | |
9858 | */ | |
9859 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
9860 | if (!func[i]) |
9861 | goto out_free; | |
9862 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
9863 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 9864 | func[i]->type = prog->type; |
1c2a088a | 9865 | func[i]->len = len; |
4f74d809 DB |
9866 | if (bpf_prog_calc_tag(func[i])) |
9867 | goto out_free; | |
1c2a088a | 9868 | func[i]->is_func = 1; |
ba64e7d8 YS |
9869 | func[i]->aux->func_idx = i; |
9870 | /* the btf and func_info will be freed only at prog->aux */ | |
9871 | func[i]->aux->btf = prog->aux->btf; | |
9872 | func[i]->aux->func_info = prog->aux->func_info; | |
9873 | ||
1c2a088a AS |
9874 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
9875 | * Long term would need debug info to populate names | |
9876 | */ | |
9877 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 9878 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 9879 | func[i]->jit_requested = 1; |
c454a46b MKL |
9880 | func[i]->aux->linfo = prog->aux->linfo; |
9881 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
9882 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
9883 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
1c2a088a AS |
9884 | func[i] = bpf_int_jit_compile(func[i]); |
9885 | if (!func[i]->jited) { | |
9886 | err = -ENOTSUPP; | |
9887 | goto out_free; | |
9888 | } | |
9889 | cond_resched(); | |
9890 | } | |
9891 | /* at this point all bpf functions were successfully JITed | |
9892 | * now populate all bpf_calls with correct addresses and | |
9893 | * run last pass of JIT | |
9894 | */ | |
f910cefa | 9895 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
9896 | insn = func[i]->insnsi; |
9897 | for (j = 0; j < func[i]->len; j++, insn++) { | |
9898 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
9899 | insn->src_reg != BPF_PSEUDO_CALL) | |
9900 | continue; | |
9901 | subprog = insn->off; | |
0d306c31 PB |
9902 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
9903 | __bpf_call_base; | |
1c2a088a | 9904 | } |
2162fed4 SD |
9905 | |
9906 | /* we use the aux data to keep a list of the start addresses | |
9907 | * of the JITed images for each function in the program | |
9908 | * | |
9909 | * for some architectures, such as powerpc64, the imm field | |
9910 | * might not be large enough to hold the offset of the start | |
9911 | * address of the callee's JITed image from __bpf_call_base | |
9912 | * | |
9913 | * in such cases, we can lookup the start address of a callee | |
9914 | * by using its subprog id, available from the off field of | |
9915 | * the call instruction, as an index for this list | |
9916 | */ | |
9917 | func[i]->aux->func = func; | |
9918 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 9919 | } |
f910cefa | 9920 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
9921 | old_bpf_func = func[i]->bpf_func; |
9922 | tmp = bpf_int_jit_compile(func[i]); | |
9923 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
9924 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 9925 | err = -ENOTSUPP; |
1c2a088a AS |
9926 | goto out_free; |
9927 | } | |
9928 | cond_resched(); | |
9929 | } | |
9930 | ||
9931 | /* finally lock prog and jit images for all functions and | |
9932 | * populate kallsysm | |
9933 | */ | |
f910cefa | 9934 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
9935 | bpf_prog_lock_ro(func[i]); |
9936 | bpf_prog_kallsyms_add(func[i]); | |
9937 | } | |
7105e828 DB |
9938 | |
9939 | /* Last step: make now unused interpreter insns from main | |
9940 | * prog consistent for later dump requests, so they can | |
9941 | * later look the same as if they were interpreted only. | |
9942 | */ | |
9943 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
9944 | if (insn->code != (BPF_JMP | BPF_CALL) || |
9945 | insn->src_reg != BPF_PSEUDO_CALL) | |
9946 | continue; | |
9947 | insn->off = env->insn_aux_data[i].call_imm; | |
9948 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 9949 | insn->imm = subprog; |
7105e828 DB |
9950 | } |
9951 | ||
1c2a088a AS |
9952 | prog->jited = 1; |
9953 | prog->bpf_func = func[0]->bpf_func; | |
9954 | prog->aux->func = func; | |
f910cefa | 9955 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 9956 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
9957 | return 0; |
9958 | out_free: | |
f910cefa | 9959 | for (i = 0; i < env->subprog_cnt; i++) |
1c2a088a AS |
9960 | if (func[i]) |
9961 | bpf_jit_free(func[i]); | |
9962 | kfree(func); | |
c7a89784 | 9963 | out_undo_insn: |
1c2a088a AS |
9964 | /* cleanup main prog to be interpreted */ |
9965 | prog->jit_requested = 0; | |
9966 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
9967 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
9968 | insn->src_reg != BPF_PSEUDO_CALL) | |
9969 | continue; | |
9970 | insn->off = 0; | |
9971 | insn->imm = env->insn_aux_data[i].call_imm; | |
9972 | } | |
c454a46b | 9973 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
9974 | return err; |
9975 | } | |
9976 | ||
1ea47e01 AS |
9977 | static int fixup_call_args(struct bpf_verifier_env *env) |
9978 | { | |
19d28fbd | 9979 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
9980 | struct bpf_prog *prog = env->prog; |
9981 | struct bpf_insn *insn = prog->insnsi; | |
9982 | int i, depth; | |
19d28fbd | 9983 | #endif |
e4052d06 | 9984 | int err = 0; |
1ea47e01 | 9985 | |
e4052d06 QM |
9986 | if (env->prog->jit_requested && |
9987 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
9988 | err = jit_subprogs(env); |
9989 | if (err == 0) | |
1c2a088a | 9990 | return 0; |
c7a89784 DB |
9991 | if (err == -EFAULT) |
9992 | return err; | |
19d28fbd DM |
9993 | } |
9994 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 AS |
9995 | for (i = 0; i < prog->len; i++, insn++) { |
9996 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
9997 | insn->src_reg != BPF_PSEUDO_CALL) | |
9998 | continue; | |
9999 | depth = get_callee_stack_depth(env, insn, i); | |
10000 | if (depth < 0) | |
10001 | return depth; | |
10002 | bpf_patch_call_args(insn, depth); | |
10003 | } | |
19d28fbd DM |
10004 | err = 0; |
10005 | #endif | |
10006 | return err; | |
1ea47e01 AS |
10007 | } |
10008 | ||
79741b3b | 10009 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 10010 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
10011 | * |
10012 | * this function is called after eBPF program passed verification | |
10013 | */ | |
79741b3b | 10014 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 10015 | { |
79741b3b | 10016 | struct bpf_prog *prog = env->prog; |
d2e4c1e6 | 10017 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
79741b3b | 10018 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 10019 | const struct bpf_func_proto *fn; |
79741b3b | 10020 | const int insn_cnt = prog->len; |
09772d92 | 10021 | const struct bpf_map_ops *ops; |
c93552c4 | 10022 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
10023 | struct bpf_insn insn_buf[16]; |
10024 | struct bpf_prog *new_prog; | |
10025 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 10026 | int i, ret, cnt, delta = 0; |
e245c5c6 | 10027 | |
79741b3b | 10028 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
10029 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
10030 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
10031 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 10032 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
10033 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
10034 | struct bpf_insn mask_and_div[] = { | |
10035 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
10036 | /* Rx div 0 -> 0 */ | |
10037 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
10038 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
10039 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
10040 | *insn, | |
10041 | }; | |
10042 | struct bpf_insn mask_and_mod[] = { | |
10043 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
10044 | /* Rx mod 0 -> Rx */ | |
10045 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
10046 | *insn, | |
10047 | }; | |
10048 | struct bpf_insn *patchlet; | |
10049 | ||
10050 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
10051 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
10052 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
10053 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
10054 | } else { | |
10055 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
10056 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
10057 | } | |
10058 | ||
10059 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
10060 | if (!new_prog) |
10061 | return -ENOMEM; | |
10062 | ||
10063 | delta += cnt - 1; | |
10064 | env->prog = prog = new_prog; | |
10065 | insn = new_prog->insnsi + i + delta; | |
10066 | continue; | |
10067 | } | |
10068 | ||
e0cea7ce DB |
10069 | if (BPF_CLASS(insn->code) == BPF_LD && |
10070 | (BPF_MODE(insn->code) == BPF_ABS || | |
10071 | BPF_MODE(insn->code) == BPF_IND)) { | |
10072 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
10073 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
10074 | verbose(env, "bpf verifier is misconfigured\n"); | |
10075 | return -EINVAL; | |
10076 | } | |
10077 | ||
10078 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10079 | if (!new_prog) | |
10080 | return -ENOMEM; | |
10081 | ||
10082 | delta += cnt - 1; | |
10083 | env->prog = prog = new_prog; | |
10084 | insn = new_prog->insnsi + i + delta; | |
10085 | continue; | |
10086 | } | |
10087 | ||
979d63d5 DB |
10088 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
10089 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
10090 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
10091 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
10092 | struct bpf_insn insn_buf[16]; | |
10093 | struct bpf_insn *patch = &insn_buf[0]; | |
10094 | bool issrc, isneg; | |
10095 | u32 off_reg; | |
10096 | ||
10097 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
10098 | if (!aux->alu_state || |
10099 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
10100 | continue; |
10101 | ||
10102 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
10103 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
10104 | BPF_ALU_SANITIZE_SRC; | |
10105 | ||
10106 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
10107 | if (isneg) | |
10108 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
10109 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
10110 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
10111 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
10112 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
10113 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
10114 | if (issrc) { | |
10115 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
10116 | off_reg); | |
10117 | insn->src_reg = BPF_REG_AX; | |
10118 | } else { | |
10119 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
10120 | BPF_REG_AX); | |
10121 | } | |
10122 | if (isneg) | |
10123 | insn->code = insn->code == code_add ? | |
10124 | code_sub : code_add; | |
10125 | *patch++ = *insn; | |
10126 | if (issrc && isneg) | |
10127 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
10128 | cnt = patch - insn_buf; | |
10129 | ||
10130 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10131 | if (!new_prog) | |
10132 | return -ENOMEM; | |
10133 | ||
10134 | delta += cnt - 1; | |
10135 | env->prog = prog = new_prog; | |
10136 | insn = new_prog->insnsi + i + delta; | |
10137 | continue; | |
10138 | } | |
10139 | ||
79741b3b AS |
10140 | if (insn->code != (BPF_JMP | BPF_CALL)) |
10141 | continue; | |
cc8b0b92 AS |
10142 | if (insn->src_reg == BPF_PSEUDO_CALL) |
10143 | continue; | |
e245c5c6 | 10144 | |
79741b3b AS |
10145 | if (insn->imm == BPF_FUNC_get_route_realm) |
10146 | prog->dst_needed = 1; | |
10147 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
10148 | bpf_user_rnd_init_once(); | |
9802d865 JB |
10149 | if (insn->imm == BPF_FUNC_override_return) |
10150 | prog->kprobe_override = 1; | |
79741b3b | 10151 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
10152 | /* If we tail call into other programs, we |
10153 | * cannot make any assumptions since they can | |
10154 | * be replaced dynamically during runtime in | |
10155 | * the program array. | |
10156 | */ | |
10157 | prog->cb_access = 1; | |
80a58d02 | 10158 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
e647815a | 10159 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
7b9f6da1 | 10160 | |
79741b3b AS |
10161 | /* mark bpf_tail_call as different opcode to avoid |
10162 | * conditional branch in the interpeter for every normal | |
10163 | * call and to prevent accidental JITing by JIT compiler | |
10164 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 10165 | */ |
79741b3b | 10166 | insn->imm = 0; |
71189fa9 | 10167 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 10168 | |
c93552c4 | 10169 | aux = &env->insn_aux_data[i + delta]; |
2c78ee89 | 10170 | if (env->bpf_capable && !expect_blinding && |
cc52d914 | 10171 | prog->jit_requested && |
d2e4c1e6 DB |
10172 | !bpf_map_key_poisoned(aux) && |
10173 | !bpf_map_ptr_poisoned(aux) && | |
10174 | !bpf_map_ptr_unpriv(aux)) { | |
10175 | struct bpf_jit_poke_descriptor desc = { | |
10176 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
10177 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
10178 | .tail_call.key = bpf_map_key_immediate(aux), | |
10179 | }; | |
10180 | ||
10181 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
10182 | if (ret < 0) { | |
10183 | verbose(env, "adding tail call poke descriptor failed\n"); | |
10184 | return ret; | |
10185 | } | |
10186 | ||
10187 | insn->imm = ret + 1; | |
10188 | continue; | |
10189 | } | |
10190 | ||
c93552c4 DB |
10191 | if (!bpf_map_ptr_unpriv(aux)) |
10192 | continue; | |
10193 | ||
b2157399 AS |
10194 | /* instead of changing every JIT dealing with tail_call |
10195 | * emit two extra insns: | |
10196 | * if (index >= max_entries) goto out; | |
10197 | * index &= array->index_mask; | |
10198 | * to avoid out-of-bounds cpu speculation | |
10199 | */ | |
c93552c4 | 10200 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 10201 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
10202 | return -EINVAL; |
10203 | } | |
c93552c4 | 10204 | |
d2e4c1e6 | 10205 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
10206 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
10207 | map_ptr->max_entries, 2); | |
10208 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
10209 | container_of(map_ptr, | |
10210 | struct bpf_array, | |
10211 | map)->index_mask); | |
10212 | insn_buf[2] = *insn; | |
10213 | cnt = 3; | |
10214 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10215 | if (!new_prog) | |
10216 | return -ENOMEM; | |
10217 | ||
10218 | delta += cnt - 1; | |
10219 | env->prog = prog = new_prog; | |
10220 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
10221 | continue; |
10222 | } | |
e245c5c6 | 10223 | |
89c63074 | 10224 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
10225 | * and other inlining handlers are currently limited to 64 bit |
10226 | * only. | |
89c63074 | 10227 | */ |
60b58afc | 10228 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
10229 | (insn->imm == BPF_FUNC_map_lookup_elem || |
10230 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
10231 | insn->imm == BPF_FUNC_map_delete_elem || |
10232 | insn->imm == BPF_FUNC_map_push_elem || | |
10233 | insn->imm == BPF_FUNC_map_pop_elem || | |
10234 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
10235 | aux = &env->insn_aux_data[i + delta]; |
10236 | if (bpf_map_ptr_poisoned(aux)) | |
10237 | goto patch_call_imm; | |
10238 | ||
d2e4c1e6 | 10239 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
10240 | ops = map_ptr->ops; |
10241 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
10242 | ops->map_gen_lookup) { | |
10243 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
10244 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
10245 | verbose(env, "bpf verifier is misconfigured\n"); | |
10246 | return -EINVAL; | |
10247 | } | |
81ed18ab | 10248 | |
09772d92 DB |
10249 | new_prog = bpf_patch_insn_data(env, i + delta, |
10250 | insn_buf, cnt); | |
10251 | if (!new_prog) | |
10252 | return -ENOMEM; | |
81ed18ab | 10253 | |
09772d92 DB |
10254 | delta += cnt - 1; |
10255 | env->prog = prog = new_prog; | |
10256 | insn = new_prog->insnsi + i + delta; | |
10257 | continue; | |
10258 | } | |
81ed18ab | 10259 | |
09772d92 DB |
10260 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
10261 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
10262 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
10263 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
10264 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
10265 | (int (*)(struct bpf_map *map, void *key, void *value, | |
10266 | u64 flags))NULL)); | |
84430d42 DB |
10267 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
10268 | (int (*)(struct bpf_map *map, void *value, | |
10269 | u64 flags))NULL)); | |
10270 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
10271 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
10272 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
10273 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
10274 | ||
09772d92 DB |
10275 | switch (insn->imm) { |
10276 | case BPF_FUNC_map_lookup_elem: | |
10277 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
10278 | __bpf_call_base; | |
10279 | continue; | |
10280 | case BPF_FUNC_map_update_elem: | |
10281 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
10282 | __bpf_call_base; | |
10283 | continue; | |
10284 | case BPF_FUNC_map_delete_elem: | |
10285 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
10286 | __bpf_call_base; | |
10287 | continue; | |
84430d42 DB |
10288 | case BPF_FUNC_map_push_elem: |
10289 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
10290 | __bpf_call_base; | |
10291 | continue; | |
10292 | case BPF_FUNC_map_pop_elem: | |
10293 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
10294 | __bpf_call_base; | |
10295 | continue; | |
10296 | case BPF_FUNC_map_peek_elem: | |
10297 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
10298 | __bpf_call_base; | |
10299 | continue; | |
09772d92 | 10300 | } |
81ed18ab | 10301 | |
09772d92 | 10302 | goto patch_call_imm; |
81ed18ab AS |
10303 | } |
10304 | ||
5576b991 MKL |
10305 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
10306 | insn->imm == BPF_FUNC_jiffies64) { | |
10307 | struct bpf_insn ld_jiffies_addr[2] = { | |
10308 | BPF_LD_IMM64(BPF_REG_0, | |
10309 | (unsigned long)&jiffies), | |
10310 | }; | |
10311 | ||
10312 | insn_buf[0] = ld_jiffies_addr[0]; | |
10313 | insn_buf[1] = ld_jiffies_addr[1]; | |
10314 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
10315 | BPF_REG_0, 0); | |
10316 | cnt = 3; | |
10317 | ||
10318 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
10319 | cnt); | |
10320 | if (!new_prog) | |
10321 | return -ENOMEM; | |
10322 | ||
10323 | delta += cnt - 1; | |
10324 | env->prog = prog = new_prog; | |
10325 | insn = new_prog->insnsi + i + delta; | |
10326 | continue; | |
10327 | } | |
10328 | ||
81ed18ab | 10329 | patch_call_imm: |
5e43f899 | 10330 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
10331 | /* all functions that have prototype and verifier allowed |
10332 | * programs to call them, must be real in-kernel functions | |
10333 | */ | |
10334 | if (!fn->func) { | |
61bd5218 JK |
10335 | verbose(env, |
10336 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
10337 | func_id_name(insn->imm), insn->imm); |
10338 | return -EFAULT; | |
e245c5c6 | 10339 | } |
79741b3b | 10340 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 10341 | } |
e245c5c6 | 10342 | |
d2e4c1e6 DB |
10343 | /* Since poke tab is now finalized, publish aux to tracker. */ |
10344 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
10345 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
10346 | if (!map_ptr->ops->map_poke_track || | |
10347 | !map_ptr->ops->map_poke_untrack || | |
10348 | !map_ptr->ops->map_poke_run) { | |
10349 | verbose(env, "bpf verifier is misconfigured\n"); | |
10350 | return -EINVAL; | |
10351 | } | |
10352 | ||
10353 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
10354 | if (ret < 0) { | |
10355 | verbose(env, "tracking tail call prog failed\n"); | |
10356 | return ret; | |
10357 | } | |
10358 | } | |
10359 | ||
79741b3b AS |
10360 | return 0; |
10361 | } | |
e245c5c6 | 10362 | |
58e2af8b | 10363 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 10364 | { |
58e2af8b | 10365 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
10366 | int i; |
10367 | ||
9f4686c4 AS |
10368 | sl = env->free_list; |
10369 | while (sl) { | |
10370 | sln = sl->next; | |
10371 | free_verifier_state(&sl->state, false); | |
10372 | kfree(sl); | |
10373 | sl = sln; | |
10374 | } | |
51c39bb1 | 10375 | env->free_list = NULL; |
9f4686c4 | 10376 | |
f1bca824 AS |
10377 | if (!env->explored_states) |
10378 | return; | |
10379 | ||
dc2a4ebc | 10380 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
10381 | sl = env->explored_states[i]; |
10382 | ||
a8f500af AS |
10383 | while (sl) { |
10384 | sln = sl->next; | |
10385 | free_verifier_state(&sl->state, false); | |
10386 | kfree(sl); | |
10387 | sl = sln; | |
10388 | } | |
51c39bb1 | 10389 | env->explored_states[i] = NULL; |
f1bca824 | 10390 | } |
51c39bb1 | 10391 | } |
f1bca824 | 10392 | |
51c39bb1 AS |
10393 | /* The verifier is using insn_aux_data[] to store temporary data during |
10394 | * verification and to store information for passes that run after the | |
10395 | * verification like dead code sanitization. do_check_common() for subprogram N | |
10396 | * may analyze many other subprograms. sanitize_insn_aux_data() clears all | |
10397 | * temporary data after do_check_common() finds that subprogram N cannot be | |
10398 | * verified independently. pass_cnt counts the number of times | |
10399 | * do_check_common() was run and insn->aux->seen tells the pass number | |
10400 | * insn_aux_data was touched. These variables are compared to clear temporary | |
10401 | * data from failed pass. For testing and experiments do_check_common() can be | |
10402 | * run multiple times even when prior attempt to verify is unsuccessful. | |
10403 | */ | |
10404 | static void sanitize_insn_aux_data(struct bpf_verifier_env *env) | |
10405 | { | |
10406 | struct bpf_insn *insn = env->prog->insnsi; | |
10407 | struct bpf_insn_aux_data *aux; | |
10408 | int i, class; | |
10409 | ||
10410 | for (i = 0; i < env->prog->len; i++) { | |
10411 | class = BPF_CLASS(insn[i].code); | |
10412 | if (class != BPF_LDX && class != BPF_STX) | |
10413 | continue; | |
10414 | aux = &env->insn_aux_data[i]; | |
10415 | if (aux->seen != env->pass_cnt) | |
10416 | continue; | |
10417 | memset(aux, 0, offsetof(typeof(*aux), orig_idx)); | |
10418 | } | |
f1bca824 AS |
10419 | } |
10420 | ||
51c39bb1 AS |
10421 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
10422 | { | |
6f8a57cc | 10423 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
10424 | struct bpf_verifier_state *state; |
10425 | struct bpf_reg_state *regs; | |
10426 | int ret, i; | |
10427 | ||
10428 | env->prev_linfo = NULL; | |
10429 | env->pass_cnt++; | |
10430 | ||
10431 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
10432 | if (!state) | |
10433 | return -ENOMEM; | |
10434 | state->curframe = 0; | |
10435 | state->speculative = false; | |
10436 | state->branches = 1; | |
10437 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
10438 | if (!state->frame[0]) { | |
10439 | kfree(state); | |
10440 | return -ENOMEM; | |
10441 | } | |
10442 | env->cur_state = state; | |
10443 | init_func_state(env, state->frame[0], | |
10444 | BPF_MAIN_FUNC /* callsite */, | |
10445 | 0 /* frameno */, | |
10446 | subprog); | |
10447 | ||
10448 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 10449 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
10450 | ret = btf_prepare_func_args(env, subprog, regs); |
10451 | if (ret) | |
10452 | goto out; | |
10453 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
10454 | if (regs[i].type == PTR_TO_CTX) | |
10455 | mark_reg_known_zero(env, regs, i); | |
10456 | else if (regs[i].type == SCALAR_VALUE) | |
10457 | mark_reg_unknown(env, regs, i); | |
10458 | } | |
10459 | } else { | |
10460 | /* 1st arg to a function */ | |
10461 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
10462 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
10463 | ret = btf_check_func_arg_match(env, subprog, regs); | |
10464 | if (ret == -EFAULT) | |
10465 | /* unlikely verifier bug. abort. | |
10466 | * ret == 0 and ret < 0 are sadly acceptable for | |
10467 | * main() function due to backward compatibility. | |
10468 | * Like socket filter program may be written as: | |
10469 | * int bpf_prog(struct pt_regs *ctx) | |
10470 | * and never dereference that ctx in the program. | |
10471 | * 'struct pt_regs' is a type mismatch for socket | |
10472 | * filter that should be using 'struct __sk_buff'. | |
10473 | */ | |
10474 | goto out; | |
10475 | } | |
10476 | ||
10477 | ret = do_check(env); | |
10478 | out: | |
f59bbfc2 AS |
10479 | /* check for NULL is necessary, since cur_state can be freed inside |
10480 | * do_check() under memory pressure. | |
10481 | */ | |
10482 | if (env->cur_state) { | |
10483 | free_verifier_state(env->cur_state, true); | |
10484 | env->cur_state = NULL; | |
10485 | } | |
6f8a57cc AN |
10486 | while (!pop_stack(env, NULL, NULL, false)); |
10487 | if (!ret && pop_log) | |
10488 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 AS |
10489 | free_states(env); |
10490 | if (ret) | |
10491 | /* clean aux data in case subprog was rejected */ | |
10492 | sanitize_insn_aux_data(env); | |
10493 | return ret; | |
10494 | } | |
10495 | ||
10496 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
10497 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
10498 | * Consider: | |
10499 | * int bar(int); | |
10500 | * int foo(int f) | |
10501 | * { | |
10502 | * return bar(f); | |
10503 | * } | |
10504 | * int bar(int b) | |
10505 | * { | |
10506 | * ... | |
10507 | * } | |
10508 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
10509 | * will be assumed that bar() already verified successfully and call to bar() | |
10510 | * from foo() will be checked for type match only. Later bar() will be verified | |
10511 | * independently to check that it's safe for R1=any_scalar_value. | |
10512 | */ | |
10513 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
10514 | { | |
10515 | struct bpf_prog_aux *aux = env->prog->aux; | |
10516 | int i, ret; | |
10517 | ||
10518 | if (!aux->func_info) | |
10519 | return 0; | |
10520 | ||
10521 | for (i = 1; i < env->subprog_cnt; i++) { | |
10522 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
10523 | continue; | |
10524 | env->insn_idx = env->subprog_info[i].start; | |
10525 | WARN_ON_ONCE(env->insn_idx == 0); | |
10526 | ret = do_check_common(env, i); | |
10527 | if (ret) { | |
10528 | return ret; | |
10529 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
10530 | verbose(env, | |
10531 | "Func#%d is safe for any args that match its prototype\n", | |
10532 | i); | |
10533 | } | |
10534 | } | |
10535 | return 0; | |
10536 | } | |
10537 | ||
10538 | static int do_check_main(struct bpf_verifier_env *env) | |
10539 | { | |
10540 | int ret; | |
10541 | ||
10542 | env->insn_idx = 0; | |
10543 | ret = do_check_common(env, 0); | |
10544 | if (!ret) | |
10545 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
10546 | return ret; | |
10547 | } | |
10548 | ||
10549 | ||
06ee7115 AS |
10550 | static void print_verification_stats(struct bpf_verifier_env *env) |
10551 | { | |
10552 | int i; | |
10553 | ||
10554 | if (env->log.level & BPF_LOG_STATS) { | |
10555 | verbose(env, "verification time %lld usec\n", | |
10556 | div_u64(env->verification_time, 1000)); | |
10557 | verbose(env, "stack depth "); | |
10558 | for (i = 0; i < env->subprog_cnt; i++) { | |
10559 | u32 depth = env->subprog_info[i].stack_depth; | |
10560 | ||
10561 | verbose(env, "%d", depth); | |
10562 | if (i + 1 < env->subprog_cnt) | |
10563 | verbose(env, "+"); | |
10564 | } | |
10565 | verbose(env, "\n"); | |
10566 | } | |
10567 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
10568 | "total_states %d peak_states %d mark_read %d\n", | |
10569 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
10570 | env->max_states_per_insn, env->total_states, | |
10571 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
10572 | } |
10573 | ||
27ae7997 MKL |
10574 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
10575 | { | |
10576 | const struct btf_type *t, *func_proto; | |
10577 | const struct bpf_struct_ops *st_ops; | |
10578 | const struct btf_member *member; | |
10579 | struct bpf_prog *prog = env->prog; | |
10580 | u32 btf_id, member_idx; | |
10581 | const char *mname; | |
10582 | ||
10583 | btf_id = prog->aux->attach_btf_id; | |
10584 | st_ops = bpf_struct_ops_find(btf_id); | |
10585 | if (!st_ops) { | |
10586 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
10587 | btf_id); | |
10588 | return -ENOTSUPP; | |
10589 | } | |
10590 | ||
10591 | t = st_ops->type; | |
10592 | member_idx = prog->expected_attach_type; | |
10593 | if (member_idx >= btf_type_vlen(t)) { | |
10594 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
10595 | member_idx, st_ops->name); | |
10596 | return -EINVAL; | |
10597 | } | |
10598 | ||
10599 | member = &btf_type_member(t)[member_idx]; | |
10600 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
10601 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
10602 | NULL); | |
10603 | if (!func_proto) { | |
10604 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
10605 | mname, member_idx, st_ops->name); | |
10606 | return -EINVAL; | |
10607 | } | |
10608 | ||
10609 | if (st_ops->check_member) { | |
10610 | int err = st_ops->check_member(t, member); | |
10611 | ||
10612 | if (err) { | |
10613 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
10614 | mname, st_ops->name); | |
10615 | return err; | |
10616 | } | |
10617 | } | |
10618 | ||
10619 | prog->aux->attach_func_proto = func_proto; | |
10620 | prog->aux->attach_func_name = mname; | |
10621 | env->ops = st_ops->verifier_ops; | |
10622 | ||
10623 | return 0; | |
10624 | } | |
6ba43b76 KS |
10625 | #define SECURITY_PREFIX "security_" |
10626 | ||
18644cec | 10627 | static int check_attach_modify_return(struct bpf_prog *prog, unsigned long addr) |
6ba43b76 | 10628 | { |
69191754 KS |
10629 | if (within_error_injection_list(addr) || |
10630 | !strncmp(SECURITY_PREFIX, prog->aux->attach_func_name, | |
10631 | sizeof(SECURITY_PREFIX) - 1)) | |
6ba43b76 | 10632 | return 0; |
6ba43b76 | 10633 | |
6ba43b76 KS |
10634 | return -EINVAL; |
10635 | } | |
27ae7997 | 10636 | |
38207291 MKL |
10637 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
10638 | { | |
10639 | struct bpf_prog *prog = env->prog; | |
be8704ff | 10640 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
5b92a28a | 10641 | struct bpf_prog *tgt_prog = prog->aux->linked_prog; |
38207291 | 10642 | u32 btf_id = prog->aux->attach_btf_id; |
f1b9509c | 10643 | const char prefix[] = "btf_trace_"; |
15d83c4d | 10644 | struct btf_func_model fmodel; |
5b92a28a | 10645 | int ret = 0, subprog = -1, i; |
fec56f58 | 10646 | struct bpf_trampoline *tr; |
38207291 | 10647 | const struct btf_type *t; |
5b92a28a | 10648 | bool conservative = true; |
38207291 | 10649 | const char *tname; |
5b92a28a | 10650 | struct btf *btf; |
fec56f58 | 10651 | long addr; |
5b92a28a | 10652 | u64 key; |
38207291 | 10653 | |
27ae7997 MKL |
10654 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
10655 | return check_struct_ops_btf_id(env); | |
10656 | ||
9e4e01df KS |
10657 | if (prog->type != BPF_PROG_TYPE_TRACING && |
10658 | prog->type != BPF_PROG_TYPE_LSM && | |
10659 | !prog_extension) | |
f1b9509c | 10660 | return 0; |
38207291 | 10661 | |
f1b9509c AS |
10662 | if (!btf_id) { |
10663 | verbose(env, "Tracing programs must provide btf_id\n"); | |
10664 | return -EINVAL; | |
10665 | } | |
5b92a28a AS |
10666 | btf = bpf_prog_get_target_btf(prog); |
10667 | if (!btf) { | |
10668 | verbose(env, | |
10669 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); | |
10670 | return -EINVAL; | |
10671 | } | |
10672 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c AS |
10673 | if (!t) { |
10674 | verbose(env, "attach_btf_id %u is invalid\n", btf_id); | |
10675 | return -EINVAL; | |
10676 | } | |
5b92a28a | 10677 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c AS |
10678 | if (!tname) { |
10679 | verbose(env, "attach_btf_id %u doesn't have a name\n", btf_id); | |
10680 | return -EINVAL; | |
10681 | } | |
5b92a28a AS |
10682 | if (tgt_prog) { |
10683 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
10684 | ||
10685 | for (i = 0; i < aux->func_info_cnt; i++) | |
10686 | if (aux->func_info[i].type_id == btf_id) { | |
10687 | subprog = i; | |
10688 | break; | |
10689 | } | |
10690 | if (subprog == -1) { | |
10691 | verbose(env, "Subprog %s doesn't exist\n", tname); | |
10692 | return -EINVAL; | |
10693 | } | |
10694 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
10695 | if (prog_extension) { |
10696 | if (conservative) { | |
10697 | verbose(env, | |
10698 | "Cannot replace static functions\n"); | |
10699 | return -EINVAL; | |
10700 | } | |
10701 | if (!prog->jit_requested) { | |
10702 | verbose(env, | |
10703 | "Extension programs should be JITed\n"); | |
10704 | return -EINVAL; | |
10705 | } | |
10706 | env->ops = bpf_verifier_ops[tgt_prog->type]; | |
03f87c0b | 10707 | prog->expected_attach_type = tgt_prog->expected_attach_type; |
be8704ff AS |
10708 | } |
10709 | if (!tgt_prog->jited) { | |
10710 | verbose(env, "Can attach to only JITed progs\n"); | |
10711 | return -EINVAL; | |
10712 | } | |
10713 | if (tgt_prog->type == prog->type) { | |
10714 | /* Cannot fentry/fexit another fentry/fexit program. | |
10715 | * Cannot attach program extension to another extension. | |
10716 | * It's ok to attach fentry/fexit to extension program. | |
10717 | */ | |
10718 | verbose(env, "Cannot recursively attach\n"); | |
10719 | return -EINVAL; | |
10720 | } | |
10721 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
10722 | prog_extension && | |
10723 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
10724 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
10725 | /* Program extensions can extend all program types | |
10726 | * except fentry/fexit. The reason is the following. | |
10727 | * The fentry/fexit programs are used for performance | |
10728 | * analysis, stats and can be attached to any program | |
10729 | * type except themselves. When extension program is | |
10730 | * replacing XDP function it is necessary to allow | |
10731 | * performance analysis of all functions. Both original | |
10732 | * XDP program and its program extension. Hence | |
10733 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
10734 | * allowed. If extending of fentry/fexit was allowed it | |
10735 | * would be possible to create long call chain | |
10736 | * fentry->extension->fentry->extension beyond | |
10737 | * reasonable stack size. Hence extending fentry is not | |
10738 | * allowed. | |
10739 | */ | |
10740 | verbose(env, "Cannot extend fentry/fexit\n"); | |
10741 | return -EINVAL; | |
10742 | } | |
5b92a28a AS |
10743 | key = ((u64)aux->id) << 32 | btf_id; |
10744 | } else { | |
be8704ff AS |
10745 | if (prog_extension) { |
10746 | verbose(env, "Cannot replace kernel functions\n"); | |
10747 | return -EINVAL; | |
10748 | } | |
5b92a28a AS |
10749 | key = btf_id; |
10750 | } | |
f1b9509c AS |
10751 | |
10752 | switch (prog->expected_attach_type) { | |
10753 | case BPF_TRACE_RAW_TP: | |
5b92a28a AS |
10754 | if (tgt_prog) { |
10755 | verbose(env, | |
10756 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); | |
10757 | return -EINVAL; | |
10758 | } | |
38207291 MKL |
10759 | if (!btf_type_is_typedef(t)) { |
10760 | verbose(env, "attach_btf_id %u is not a typedef\n", | |
10761 | btf_id); | |
10762 | return -EINVAL; | |
10763 | } | |
f1b9509c | 10764 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
38207291 MKL |
10765 | verbose(env, "attach_btf_id %u points to wrong type name %s\n", |
10766 | btf_id, tname); | |
10767 | return -EINVAL; | |
10768 | } | |
10769 | tname += sizeof(prefix) - 1; | |
5b92a28a | 10770 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
10771 | if (!btf_type_is_ptr(t)) |
10772 | /* should never happen in valid vmlinux build */ | |
10773 | return -EINVAL; | |
5b92a28a | 10774 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
10775 | if (!btf_type_is_func_proto(t)) |
10776 | /* should never happen in valid vmlinux build */ | |
10777 | return -EINVAL; | |
10778 | ||
10779 | /* remember two read only pointers that are valid for | |
10780 | * the life time of the kernel | |
10781 | */ | |
10782 | prog->aux->attach_func_name = tname; | |
10783 | prog->aux->attach_func_proto = t; | |
10784 | prog->aux->attach_btf_trace = true; | |
f1b9509c | 10785 | return 0; |
15d83c4d YS |
10786 | case BPF_TRACE_ITER: |
10787 | if (!btf_type_is_func(t)) { | |
10788 | verbose(env, "attach_btf_id %u is not a function\n", | |
10789 | btf_id); | |
10790 | return -EINVAL; | |
10791 | } | |
10792 | t = btf_type_by_id(btf, t->type); | |
10793 | if (!btf_type_is_func_proto(t)) | |
10794 | return -EINVAL; | |
10795 | prog->aux->attach_func_name = tname; | |
10796 | prog->aux->attach_func_proto = t; | |
10797 | if (!bpf_iter_prog_supported(prog)) | |
10798 | return -EINVAL; | |
10799 | ret = btf_distill_func_proto(&env->log, btf, t, | |
10800 | tname, &fmodel); | |
10801 | return ret; | |
be8704ff AS |
10802 | default: |
10803 | if (!prog_extension) | |
10804 | return -EINVAL; | |
10805 | /* fallthrough */ | |
ae240823 | 10806 | case BPF_MODIFY_RETURN: |
9e4e01df | 10807 | case BPF_LSM_MAC: |
fec56f58 AS |
10808 | case BPF_TRACE_FENTRY: |
10809 | case BPF_TRACE_FEXIT: | |
9e4e01df KS |
10810 | prog->aux->attach_func_name = tname; |
10811 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
10812 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
10813 | if (ret < 0) | |
10814 | return ret; | |
10815 | } | |
10816 | ||
fec56f58 AS |
10817 | if (!btf_type_is_func(t)) { |
10818 | verbose(env, "attach_btf_id %u is not a function\n", | |
10819 | btf_id); | |
10820 | return -EINVAL; | |
10821 | } | |
be8704ff AS |
10822 | if (prog_extension && |
10823 | btf_check_type_match(env, prog, btf, t)) | |
10824 | return -EINVAL; | |
5b92a28a | 10825 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
10826 | if (!btf_type_is_func_proto(t)) |
10827 | return -EINVAL; | |
5b92a28a | 10828 | tr = bpf_trampoline_lookup(key); |
fec56f58 AS |
10829 | if (!tr) |
10830 | return -ENOMEM; | |
5b92a28a | 10831 | /* t is either vmlinux type or another program's type */ |
fec56f58 AS |
10832 | prog->aux->attach_func_proto = t; |
10833 | mutex_lock(&tr->mutex); | |
10834 | if (tr->func.addr) { | |
10835 | prog->aux->trampoline = tr; | |
10836 | goto out; | |
10837 | } | |
5b92a28a AS |
10838 | if (tgt_prog && conservative) { |
10839 | prog->aux->attach_func_proto = NULL; | |
10840 | t = NULL; | |
10841 | } | |
10842 | ret = btf_distill_func_proto(&env->log, btf, t, | |
fec56f58 AS |
10843 | tname, &tr->func.model); |
10844 | if (ret < 0) | |
10845 | goto out; | |
5b92a28a | 10846 | if (tgt_prog) { |
e9eeec58 YS |
10847 | if (subprog == 0) |
10848 | addr = (long) tgt_prog->bpf_func; | |
10849 | else | |
10850 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
10851 | } else { |
10852 | addr = kallsyms_lookup_name(tname); | |
10853 | if (!addr) { | |
10854 | verbose(env, | |
10855 | "The address of function %s cannot be found\n", | |
10856 | tname); | |
10857 | ret = -ENOENT; | |
10858 | goto out; | |
10859 | } | |
fec56f58 | 10860 | } |
18644cec AS |
10861 | |
10862 | if (prog->expected_attach_type == BPF_MODIFY_RETURN) { | |
10863 | ret = check_attach_modify_return(prog, addr); | |
10864 | if (ret) | |
10865 | verbose(env, "%s() is not modifiable\n", | |
10866 | prog->aux->attach_func_name); | |
10867 | } | |
10868 | ||
10869 | if (ret) | |
10870 | goto out; | |
fec56f58 AS |
10871 | tr->func.addr = (void *)addr; |
10872 | prog->aux->trampoline = tr; | |
10873 | out: | |
10874 | mutex_unlock(&tr->mutex); | |
10875 | if (ret) | |
10876 | bpf_trampoline_put(tr); | |
10877 | return ret; | |
38207291 | 10878 | } |
38207291 MKL |
10879 | } |
10880 | ||
838e9690 YS |
10881 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
10882 | union bpf_attr __user *uattr) | |
51580e79 | 10883 | { |
06ee7115 | 10884 | u64 start_time = ktime_get_ns(); |
58e2af8b | 10885 | struct bpf_verifier_env *env; |
b9193c1b | 10886 | struct bpf_verifier_log *log; |
9e4c24e7 | 10887 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 10888 | bool is_priv; |
51580e79 | 10889 | |
eba0c929 AB |
10890 | /* no program is valid */ |
10891 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
10892 | return -EINVAL; | |
10893 | ||
58e2af8b | 10894 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
10895 | * allocate/free it every time bpf_check() is called |
10896 | */ | |
58e2af8b | 10897 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
10898 | if (!env) |
10899 | return -ENOMEM; | |
61bd5218 | 10900 | log = &env->log; |
cbd35700 | 10901 | |
9e4c24e7 | 10902 | len = (*prog)->len; |
fad953ce | 10903 | env->insn_aux_data = |
9e4c24e7 | 10904 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
10905 | ret = -ENOMEM; |
10906 | if (!env->insn_aux_data) | |
10907 | goto err_free_env; | |
9e4c24e7 JK |
10908 | for (i = 0; i < len; i++) |
10909 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 10910 | env->prog = *prog; |
00176a34 | 10911 | env->ops = bpf_verifier_ops[env->prog->type]; |
2c78ee89 | 10912 | is_priv = bpf_capable(); |
0246e64d | 10913 | |
8580ac94 AS |
10914 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
10915 | mutex_lock(&bpf_verifier_lock); | |
10916 | if (!btf_vmlinux) | |
10917 | btf_vmlinux = btf_parse_vmlinux(); | |
10918 | mutex_unlock(&bpf_verifier_lock); | |
10919 | } | |
10920 | ||
cbd35700 | 10921 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
10922 | if (!is_priv) |
10923 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
10924 | |
10925 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
10926 | /* user requested verbose verifier output | |
10927 | * and supplied buffer to store the verification trace | |
10928 | */ | |
e7bf8249 JK |
10929 | log->level = attr->log_level; |
10930 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
10931 | log->len_total = attr->log_size; | |
cbd35700 AS |
10932 | |
10933 | ret = -EINVAL; | |
e7bf8249 | 10934 | /* log attributes have to be sane */ |
7a9f5c65 | 10935 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 10936 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 10937 | goto err_unlock; |
cbd35700 | 10938 | } |
1ad2f583 | 10939 | |
8580ac94 AS |
10940 | if (IS_ERR(btf_vmlinux)) { |
10941 | /* Either gcc or pahole or kernel are broken. */ | |
10942 | verbose(env, "in-kernel BTF is malformed\n"); | |
10943 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 10944 | goto skip_full_check; |
8580ac94 AS |
10945 | } |
10946 | ||
1ad2f583 DB |
10947 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
10948 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 10949 | env->strict_alignment = true; |
e9ee9efc DM |
10950 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
10951 | env->strict_alignment = false; | |
cbd35700 | 10952 | |
2c78ee89 AS |
10953 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
10954 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); | |
10955 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
10956 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 10957 | |
10d274e8 AS |
10958 | if (is_priv) |
10959 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
10960 | ||
f4e3ec0d JK |
10961 | ret = replace_map_fd_with_map_ptr(env); |
10962 | if (ret < 0) | |
10963 | goto skip_full_check; | |
10964 | ||
cae1927c | 10965 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 10966 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 10967 | if (ret) |
f4e3ec0d | 10968 | goto skip_full_check; |
ab3f0063 JK |
10969 | } |
10970 | ||
dc2a4ebc | 10971 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 10972 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
10973 | GFP_USER); |
10974 | ret = -ENOMEM; | |
10975 | if (!env->explored_states) | |
10976 | goto skip_full_check; | |
10977 | ||
d9762e84 | 10978 | ret = check_subprogs(env); |
475fb78f AS |
10979 | if (ret < 0) |
10980 | goto skip_full_check; | |
10981 | ||
c454a46b | 10982 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
10983 | if (ret < 0) |
10984 | goto skip_full_check; | |
10985 | ||
be8704ff AS |
10986 | ret = check_attach_btf_id(env); |
10987 | if (ret) | |
10988 | goto skip_full_check; | |
10989 | ||
d9762e84 MKL |
10990 | ret = check_cfg(env); |
10991 | if (ret < 0) | |
10992 | goto skip_full_check; | |
10993 | ||
51c39bb1 AS |
10994 | ret = do_check_subprogs(env); |
10995 | ret = ret ?: do_check_main(env); | |
cbd35700 | 10996 | |
c941ce9c QM |
10997 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
10998 | ret = bpf_prog_offload_finalize(env); | |
10999 | ||
0246e64d | 11000 | skip_full_check: |
51c39bb1 | 11001 | kvfree(env->explored_states); |
0246e64d | 11002 | |
c131187d | 11003 | if (ret == 0) |
9b38c405 | 11004 | ret = check_max_stack_depth(env); |
c131187d | 11005 | |
9b38c405 | 11006 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
11007 | if (is_priv) { |
11008 | if (ret == 0) | |
11009 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
11010 | if (ret == 0) |
11011 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
11012 | if (ret == 0) |
11013 | ret = opt_remove_nops(env); | |
52875a04 JK |
11014 | } else { |
11015 | if (ret == 0) | |
11016 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
11017 | } |
11018 | ||
9bac3d6d AS |
11019 | if (ret == 0) |
11020 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
11021 | ret = convert_ctx_accesses(env); | |
11022 | ||
e245c5c6 | 11023 | if (ret == 0) |
79741b3b | 11024 | ret = fixup_bpf_calls(env); |
e245c5c6 | 11025 | |
a4b1d3c1 JW |
11026 | /* do 32-bit optimization after insn patching has done so those patched |
11027 | * insns could be handled correctly. | |
11028 | */ | |
d6c2308c JW |
11029 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
11030 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
11031 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
11032 | : false; | |
a4b1d3c1 JW |
11033 | } |
11034 | ||
1ea47e01 AS |
11035 | if (ret == 0) |
11036 | ret = fixup_call_args(env); | |
11037 | ||
06ee7115 AS |
11038 | env->verification_time = ktime_get_ns() - start_time; |
11039 | print_verification_stats(env); | |
11040 | ||
a2a7d570 | 11041 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 11042 | ret = -ENOSPC; |
a2a7d570 | 11043 | if (log->level && !log->ubuf) { |
cbd35700 | 11044 | ret = -EFAULT; |
a2a7d570 | 11045 | goto err_release_maps; |
cbd35700 AS |
11046 | } |
11047 | ||
0246e64d AS |
11048 | if (ret == 0 && env->used_map_cnt) { |
11049 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
11050 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
11051 | sizeof(env->used_maps[0]), | |
11052 | GFP_KERNEL); | |
0246e64d | 11053 | |
9bac3d6d | 11054 | if (!env->prog->aux->used_maps) { |
0246e64d | 11055 | ret = -ENOMEM; |
a2a7d570 | 11056 | goto err_release_maps; |
0246e64d AS |
11057 | } |
11058 | ||
9bac3d6d | 11059 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 11060 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 11061 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
11062 | |
11063 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
11064 | * bpf_ld_imm64 instructions | |
11065 | */ | |
11066 | convert_pseudo_ld_imm64(env); | |
11067 | } | |
cbd35700 | 11068 | |
ba64e7d8 YS |
11069 | if (ret == 0) |
11070 | adjust_btf_func(env); | |
11071 | ||
a2a7d570 | 11072 | err_release_maps: |
9bac3d6d | 11073 | if (!env->prog->aux->used_maps) |
0246e64d | 11074 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 11075 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
11076 | */ |
11077 | release_maps(env); | |
03f87c0b THJ |
11078 | |
11079 | /* extension progs temporarily inherit the attach_type of their targets | |
11080 | for verification purposes, so set it back to zero before returning | |
11081 | */ | |
11082 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
11083 | env->prog->expected_attach_type = 0; | |
11084 | ||
9bac3d6d | 11085 | *prog = env->prog; |
3df126f3 | 11086 | err_unlock: |
45a73c17 AS |
11087 | if (!is_priv) |
11088 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
11089 | vfree(env->insn_aux_data); |
11090 | err_free_env: | |
11091 | kfree(env); | |
51580e79 AS |
11092 | return ret; |
11093 | } |