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> |
1e6c62a8 | 24 | #include <linux/btf_ids.h> |
51580e79 | 25 | |
f4ac7e0b JK |
26 | #include "disasm.h" |
27 | ||
00176a34 | 28 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 29 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
30 | [_id] = & _name ## _verifier_ops, |
31 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 32 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
33 | #include <linux/bpf_types.h> |
34 | #undef BPF_PROG_TYPE | |
35 | #undef BPF_MAP_TYPE | |
f2e10bff | 36 | #undef BPF_LINK_TYPE |
00176a34 JK |
37 | }; |
38 | ||
51580e79 AS |
39 | /* bpf_check() is a static code analyzer that walks eBPF program |
40 | * instruction by instruction and updates register/stack state. | |
41 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
42 | * | |
43 | * The first pass is depth-first-search to check that the program is a DAG. | |
44 | * It rejects the following programs: | |
45 | * - larger than BPF_MAXINSNS insns | |
46 | * - if loop is present (detected via back-edge) | |
47 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
48 | * - out of bounds or malformed jumps | |
49 | * The second pass is all possible path descent from the 1st insn. | |
8fb33b60 | 50 | * Since it's analyzing all paths through the program, the length of the |
eba38a96 | 51 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
52 | * insn is less then 4K, but there are too many branches that change stack/regs. |
53 | * Number of 'branches to be analyzed' is limited to 1k | |
54 | * | |
55 | * On entry to each instruction, each register has a type, and the instruction | |
56 | * changes the types of the registers depending on instruction semantics. | |
57 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
58 | * copied to R1. | |
59 | * | |
60 | * All registers are 64-bit. | |
61 | * R0 - return register | |
62 | * R1-R5 argument passing registers | |
63 | * R6-R9 callee saved registers | |
64 | * R10 - frame pointer read-only | |
65 | * | |
66 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
67 | * and has type PTR_TO_CTX. | |
68 | * | |
69 | * Verifier tracks arithmetic operations on pointers in case: | |
70 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
71 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
72 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
73 | * and 2nd arithmetic instruction is pattern matched to recognize | |
74 | * that it wants to construct a pointer to some element within stack. | |
75 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
76 | * (and -20 constant is saved for further stack bounds checking). | |
77 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
78 | * | |
f1174f77 | 79 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 80 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 81 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
82 | * |
83 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
84 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
85 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
86 | * |
87 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
88 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
89 | * | |
90 | * registers used to pass values to function calls are checked against | |
91 | * function argument constraints. | |
92 | * | |
93 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
94 | * It means that the register type passed to this function must be | |
95 | * PTR_TO_STACK and it will be used inside the function as | |
96 | * 'pointer to map element key' | |
97 | * | |
98 | * For example the argument constraints for bpf_map_lookup_elem(): | |
99 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
100 | * .arg1_type = ARG_CONST_MAP_PTR, | |
101 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
102 | * | |
103 | * ret_type says that this function returns 'pointer to map elem value or null' | |
104 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
105 | * 2nd argument should be a pointer to stack, which will be used inside | |
106 | * the helper function as a pointer to map element key. | |
107 | * | |
108 | * On the kernel side the helper function looks like: | |
109 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
110 | * { | |
111 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
112 | * void *key = (void *) (unsigned long) r2; | |
113 | * void *value; | |
114 | * | |
115 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
116 | * [key, key + map->key_size) bytes are valid and were initialized on | |
117 | * the stack of eBPF program. | |
118 | * } | |
119 | * | |
120 | * Corresponding eBPF program may look like: | |
121 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
122 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
123 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
124 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
125 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
126 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
127 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
128 | * | |
129 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
130 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
131 | * and were initialized prior to this call. | |
132 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
133 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
134 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
8fb33b60 | 135 | * returns either pointer to map value or NULL. |
51580e79 AS |
136 | * |
137 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
138 | * insn, the register holding that pointer in the true branch changes state to | |
139 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
140 | * branch. See check_cond_jmp_op(). | |
141 | * | |
142 | * After the call R0 is set to return type of the function and registers R1-R5 | |
143 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
144 | * |
145 | * The following reference types represent a potential reference to a kernel | |
146 | * resource which, after first being allocated, must be checked and freed by | |
147 | * the BPF program: | |
148 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
149 | * | |
150 | * When the verifier sees a helper call return a reference type, it allocates a | |
151 | * pointer id for the reference and stores it in the current function state. | |
152 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
153 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
154 | * passes through a NULL-check conditional. For the branch wherein the state is | |
155 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
156 | * |
157 | * For each helper function that allocates a reference, such as | |
158 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
159 | * bpf_sk_release(). When a reference type passes into the release function, | |
160 | * the verifier also releases the reference. If any unchecked or unreleased | |
161 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
162 | */ |
163 | ||
17a52670 | 164 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 165 | struct bpf_verifier_stack_elem { |
17a52670 AS |
166 | /* verifer state is 'st' |
167 | * before processing instruction 'insn_idx' | |
168 | * and after processing instruction 'prev_insn_idx' | |
169 | */ | |
58e2af8b | 170 | struct bpf_verifier_state st; |
17a52670 AS |
171 | int insn_idx; |
172 | int prev_insn_idx; | |
58e2af8b | 173 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
174 | /* length of verifier log at the time this state was pushed on stack */ |
175 | u32 log_pos; | |
cbd35700 AS |
176 | }; |
177 | ||
b285fcb7 | 178 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 179 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 180 | |
d2e4c1e6 DB |
181 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
182 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
183 | ||
c93552c4 DB |
184 | #define BPF_MAP_PTR_UNPRIV 1UL |
185 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
186 | POISON_POINTER_DELTA)) | |
187 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
188 | ||
189 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) | |
190 | { | |
d2e4c1e6 | 191 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
192 | } |
193 | ||
194 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
195 | { | |
d2e4c1e6 | 196 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
197 | } |
198 | ||
199 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
200 | const struct bpf_map *map, bool unpriv) | |
201 | { | |
202 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
203 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
204 | aux->map_ptr_state = (unsigned long)map | |
205 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
206 | } | |
207 | ||
208 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
209 | { | |
210 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
211 | } | |
212 | ||
213 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
214 | { | |
215 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
216 | } | |
217 | ||
218 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
219 | { | |
220 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
221 | } | |
222 | ||
223 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
224 | { | |
225 | bool poisoned = bpf_map_key_poisoned(aux); | |
226 | ||
227 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
228 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 229 | } |
fad73a1a | 230 | |
23a2d70c YS |
231 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
232 | { | |
233 | return insn->code == (BPF_JMP | BPF_CALL) && | |
234 | insn->src_reg == BPF_PSEUDO_CALL; | |
235 | } | |
236 | ||
e6ac2450 MKL |
237 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
238 | { | |
239 | return insn->code == (BPF_JMP | BPF_CALL) && | |
240 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; | |
241 | } | |
242 | ||
33ff9823 DB |
243 | struct bpf_call_arg_meta { |
244 | struct bpf_map *map_ptr; | |
435faee1 | 245 | bool raw_mode; |
36bbef52 | 246 | bool pkt_access; |
435faee1 DB |
247 | int regno; |
248 | int access_size; | |
457f4436 | 249 | int mem_size; |
10060503 | 250 | u64 msize_max_value; |
1b986589 | 251 | int ref_obj_id; |
3e8ce298 | 252 | int map_uid; |
d83525ca | 253 | int func_id; |
22dc4a0f | 254 | struct btf *btf; |
eaa6bcb7 | 255 | u32 btf_id; |
22dc4a0f | 256 | struct btf *ret_btf; |
eaa6bcb7 | 257 | u32 ret_btf_id; |
69c087ba | 258 | u32 subprogno; |
33ff9823 DB |
259 | }; |
260 | ||
8580ac94 AS |
261 | struct btf *btf_vmlinux; |
262 | ||
cbd35700 AS |
263 | static DEFINE_MUTEX(bpf_verifier_lock); |
264 | ||
d9762e84 MKL |
265 | static const struct bpf_line_info * |
266 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
267 | { | |
268 | const struct bpf_line_info *linfo; | |
269 | const struct bpf_prog *prog; | |
270 | u32 i, nr_linfo; | |
271 | ||
272 | prog = env->prog; | |
273 | nr_linfo = prog->aux->nr_linfo; | |
274 | ||
275 | if (!nr_linfo || insn_off >= prog->len) | |
276 | return NULL; | |
277 | ||
278 | linfo = prog->aux->linfo; | |
279 | for (i = 1; i < nr_linfo; i++) | |
280 | if (insn_off < linfo[i].insn_off) | |
281 | break; | |
282 | ||
283 | return &linfo[i - 1]; | |
284 | } | |
285 | ||
77d2e05a MKL |
286 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
287 | va_list args) | |
cbd35700 | 288 | { |
a2a7d570 | 289 | unsigned int n; |
cbd35700 | 290 | |
a2a7d570 | 291 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
292 | |
293 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
294 | "verifier log line truncated - local buffer too short\n"); | |
295 | ||
296 | n = min(log->len_total - log->len_used - 1, n); | |
297 | log->kbuf[n] = '\0'; | |
298 | ||
8580ac94 AS |
299 | if (log->level == BPF_LOG_KERNEL) { |
300 | pr_err("BPF:%s\n", log->kbuf); | |
301 | return; | |
302 | } | |
a2a7d570 JK |
303 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
304 | log->len_used += n; | |
305 | else | |
306 | log->ubuf = NULL; | |
cbd35700 | 307 | } |
abe08840 | 308 | |
6f8a57cc AN |
309 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
310 | { | |
311 | char zero = 0; | |
312 | ||
313 | if (!bpf_verifier_log_needed(log)) | |
314 | return; | |
315 | ||
316 | log->len_used = new_pos; | |
317 | if (put_user(zero, log->ubuf + new_pos)) | |
318 | log->ubuf = NULL; | |
319 | } | |
320 | ||
abe08840 JO |
321 | /* log_level controls verbosity level of eBPF verifier. |
322 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
323 | * so the user can figure out what's wrong with the program | |
430e68d1 | 324 | */ |
abe08840 JO |
325 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
326 | const char *fmt, ...) | |
327 | { | |
328 | va_list args; | |
329 | ||
77d2e05a MKL |
330 | if (!bpf_verifier_log_needed(&env->log)) |
331 | return; | |
332 | ||
abe08840 | 333 | va_start(args, fmt); |
77d2e05a | 334 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
335 | va_end(args); |
336 | } | |
337 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
338 | ||
339 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
340 | { | |
77d2e05a | 341 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
342 | va_list args; |
343 | ||
77d2e05a MKL |
344 | if (!bpf_verifier_log_needed(&env->log)) |
345 | return; | |
346 | ||
abe08840 | 347 | va_start(args, fmt); |
77d2e05a | 348 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
349 | va_end(args); |
350 | } | |
cbd35700 | 351 | |
9e15db66 AS |
352 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
353 | const char *fmt, ...) | |
354 | { | |
355 | va_list args; | |
356 | ||
357 | if (!bpf_verifier_log_needed(log)) | |
358 | return; | |
359 | ||
360 | va_start(args, fmt); | |
361 | bpf_verifier_vlog(log, fmt, args); | |
362 | va_end(args); | |
363 | } | |
364 | ||
d9762e84 MKL |
365 | static const char *ltrim(const char *s) |
366 | { | |
367 | while (isspace(*s)) | |
368 | s++; | |
369 | ||
370 | return s; | |
371 | } | |
372 | ||
373 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
374 | u32 insn_off, | |
375 | const char *prefix_fmt, ...) | |
376 | { | |
377 | const struct bpf_line_info *linfo; | |
378 | ||
379 | if (!bpf_verifier_log_needed(&env->log)) | |
380 | return; | |
381 | ||
382 | linfo = find_linfo(env, insn_off); | |
383 | if (!linfo || linfo == env->prev_linfo) | |
384 | return; | |
385 | ||
386 | if (prefix_fmt) { | |
387 | va_list args; | |
388 | ||
389 | va_start(args, prefix_fmt); | |
390 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
391 | va_end(args); | |
392 | } | |
393 | ||
394 | verbose(env, "%s\n", | |
395 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
396 | linfo->line_off))); | |
397 | ||
398 | env->prev_linfo = linfo; | |
399 | } | |
400 | ||
bc2591d6 YS |
401 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
402 | struct bpf_reg_state *reg, | |
403 | struct tnum *range, const char *ctx, | |
404 | const char *reg_name) | |
405 | { | |
406 | char tn_buf[48]; | |
407 | ||
408 | verbose(env, "At %s the register %s ", ctx, reg_name); | |
409 | if (!tnum_is_unknown(reg->var_off)) { | |
410 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
411 | verbose(env, "has value %s", tn_buf); | |
412 | } else { | |
413 | verbose(env, "has unknown scalar value"); | |
414 | } | |
415 | tnum_strn(tn_buf, sizeof(tn_buf), *range); | |
416 | verbose(env, " should have been in %s\n", tn_buf); | |
417 | } | |
418 | ||
de8f3a83 DB |
419 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
420 | { | |
421 | return type == PTR_TO_PACKET || | |
422 | type == PTR_TO_PACKET_META; | |
423 | } | |
424 | ||
46f8bc92 MKL |
425 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
426 | { | |
427 | return type == PTR_TO_SOCKET || | |
655a51e5 | 428 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
429 | type == PTR_TO_TCP_SOCK || |
430 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
431 | } |
432 | ||
cac616db JF |
433 | static bool reg_type_not_null(enum bpf_reg_type type) |
434 | { | |
435 | return type == PTR_TO_SOCKET || | |
436 | type == PTR_TO_TCP_SOCK || | |
437 | type == PTR_TO_MAP_VALUE || | |
69c087ba | 438 | type == PTR_TO_MAP_KEY || |
01c66c48 | 439 | type == PTR_TO_SOCK_COMMON; |
cac616db JF |
440 | } |
441 | ||
840b9615 JS |
442 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
443 | { | |
fd978bf7 | 444 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 445 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 | 446 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
b121b341 | 447 | type == PTR_TO_TCP_SOCK_OR_NULL || |
457f4436 | 448 | type == PTR_TO_BTF_ID_OR_NULL || |
afbf21dc YS |
449 | type == PTR_TO_MEM_OR_NULL || |
450 | type == PTR_TO_RDONLY_BUF_OR_NULL || | |
451 | type == PTR_TO_RDWR_BUF_OR_NULL; | |
fd978bf7 JS |
452 | } |
453 | ||
d83525ca AS |
454 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
455 | { | |
456 | return reg->type == PTR_TO_MAP_VALUE && | |
457 | map_value_has_spin_lock(reg->map_ptr); | |
458 | } | |
459 | ||
cba368c1 MKL |
460 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
461 | { | |
462 | return type == PTR_TO_SOCKET || | |
463 | type == PTR_TO_SOCKET_OR_NULL || | |
464 | type == PTR_TO_TCP_SOCK || | |
457f4436 AN |
465 | type == PTR_TO_TCP_SOCK_OR_NULL || |
466 | type == PTR_TO_MEM || | |
467 | type == PTR_TO_MEM_OR_NULL; | |
cba368c1 MKL |
468 | } |
469 | ||
1b986589 | 470 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 471 | { |
1b986589 | 472 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
473 | } |
474 | ||
fd1b0d60 LB |
475 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
476 | { | |
477 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || | |
478 | type == ARG_PTR_TO_MEM_OR_NULL || | |
479 | type == ARG_PTR_TO_CTX_OR_NULL || | |
480 | type == ARG_PTR_TO_SOCKET_OR_NULL || | |
69c087ba YS |
481 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL || |
482 | type == ARG_PTR_TO_STACK_OR_NULL; | |
fd1b0d60 LB |
483 | } |
484 | ||
fd978bf7 JS |
485 | /* Determine whether the function releases some resources allocated by another |
486 | * function call. The first reference type argument will be assumed to be | |
487 | * released by release_reference(). | |
488 | */ | |
489 | static bool is_release_function(enum bpf_func_id func_id) | |
490 | { | |
457f4436 AN |
491 | return func_id == BPF_FUNC_sk_release || |
492 | func_id == BPF_FUNC_ringbuf_submit || | |
493 | func_id == BPF_FUNC_ringbuf_discard; | |
840b9615 JS |
494 | } |
495 | ||
64d85290 | 496 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
46f8bc92 MKL |
497 | { |
498 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 | 499 | func_id == BPF_FUNC_sk_lookup_udp || |
64d85290 | 500 | func_id == BPF_FUNC_skc_lookup_tcp || |
457f4436 AN |
501 | func_id == BPF_FUNC_map_lookup_elem || |
502 | func_id == BPF_FUNC_ringbuf_reserve; | |
64d85290 JS |
503 | } |
504 | ||
505 | static bool is_acquire_function(enum bpf_func_id func_id, | |
506 | const struct bpf_map *map) | |
507 | { | |
508 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
509 | ||
510 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
511 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 AN |
512 | func_id == BPF_FUNC_skc_lookup_tcp || |
513 | func_id == BPF_FUNC_ringbuf_reserve) | |
64d85290 JS |
514 | return true; |
515 | ||
516 | if (func_id == BPF_FUNC_map_lookup_elem && | |
517 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
518 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
519 | return true; | |
520 | ||
521 | return false; | |
46f8bc92 MKL |
522 | } |
523 | ||
1b986589 MKL |
524 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
525 | { | |
526 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
527 | func_id == BPF_FUNC_sk_fullsock || |
528 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
529 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
530 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
531 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || | |
532 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
533 | } |
534 | ||
39491867 BJ |
535 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
536 | { | |
537 | return BPF_CLASS(insn->code) == BPF_STX && | |
538 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
539 | insn->imm == BPF_CMPXCHG; | |
540 | } | |
541 | ||
17a52670 AS |
542 | /* string representation of 'enum bpf_reg_type' */ |
543 | static const char * const reg_type_str[] = { | |
544 | [NOT_INIT] = "?", | |
f1174f77 | 545 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
546 | [PTR_TO_CTX] = "ctx", |
547 | [CONST_PTR_TO_MAP] = "map_ptr", | |
548 | [PTR_TO_MAP_VALUE] = "map_value", | |
549 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 550 | [PTR_TO_STACK] = "fp", |
969bf05e | 551 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 552 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 553 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 554 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
555 | [PTR_TO_SOCKET] = "sock", |
556 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
557 | [PTR_TO_SOCK_COMMON] = "sock_common", |
558 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
559 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
560 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 561 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 562 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
9e15db66 | 563 | [PTR_TO_BTF_ID] = "ptr_", |
b121b341 | 564 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
eaa6bcb7 | 565 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
457f4436 AN |
566 | [PTR_TO_MEM] = "mem", |
567 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", | |
afbf21dc YS |
568 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
569 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", | |
570 | [PTR_TO_RDWR_BUF] = "rdwr_buf", | |
571 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", | |
69c087ba YS |
572 | [PTR_TO_FUNC] = "func", |
573 | [PTR_TO_MAP_KEY] = "map_key", | |
17a52670 AS |
574 | }; |
575 | ||
8efea21d EC |
576 | static char slot_type_char[] = { |
577 | [STACK_INVALID] = '?', | |
578 | [STACK_SPILL] = 'r', | |
579 | [STACK_MISC] = 'm', | |
580 | [STACK_ZERO] = '0', | |
581 | }; | |
582 | ||
4e92024a AS |
583 | static void print_liveness(struct bpf_verifier_env *env, |
584 | enum bpf_reg_liveness live) | |
585 | { | |
9242b5f5 | 586 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
587 | verbose(env, "_"); |
588 | if (live & REG_LIVE_READ) | |
589 | verbose(env, "r"); | |
590 | if (live & REG_LIVE_WRITTEN) | |
591 | verbose(env, "w"); | |
9242b5f5 AS |
592 | if (live & REG_LIVE_DONE) |
593 | verbose(env, "D"); | |
4e92024a AS |
594 | } |
595 | ||
f4d7e40a AS |
596 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
597 | const struct bpf_reg_state *reg) | |
598 | { | |
599 | struct bpf_verifier_state *cur = env->cur_state; | |
600 | ||
601 | return cur->frame[reg->frameno]; | |
602 | } | |
603 | ||
22dc4a0f | 604 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
9e15db66 | 605 | { |
22dc4a0f | 606 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
607 | } |
608 | ||
27113c59 MKL |
609 | /* The reg state of a pointer or a bounded scalar was saved when |
610 | * it was spilled to the stack. | |
611 | */ | |
612 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
613 | { | |
614 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
615 | } | |
616 | ||
354e8f19 MKL |
617 | static void scrub_spilled_slot(u8 *stype) |
618 | { | |
619 | if (*stype != STACK_INVALID) | |
620 | *stype = STACK_MISC; | |
621 | } | |
622 | ||
61bd5218 | 623 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 624 | const struct bpf_func_state *state) |
17a52670 | 625 | { |
f4d7e40a | 626 | const struct bpf_reg_state *reg; |
17a52670 AS |
627 | enum bpf_reg_type t; |
628 | int i; | |
629 | ||
f4d7e40a AS |
630 | if (state->frameno) |
631 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 632 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
633 | reg = &state->regs[i]; |
634 | t = reg->type; | |
17a52670 AS |
635 | if (t == NOT_INIT) |
636 | continue; | |
4e92024a AS |
637 | verbose(env, " R%d", i); |
638 | print_liveness(env, reg->live); | |
639 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
640 | if (t == SCALAR_VALUE && reg->precise) |
641 | verbose(env, "P"); | |
f1174f77 EC |
642 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
643 | tnum_is_const(reg->var_off)) { | |
644 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 645 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 646 | } else { |
eaa6bcb7 HL |
647 | if (t == PTR_TO_BTF_ID || |
648 | t == PTR_TO_BTF_ID_OR_NULL || | |
649 | t == PTR_TO_PERCPU_BTF_ID) | |
22dc4a0f | 650 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
cba368c1 MKL |
651 | verbose(env, "(id=%d", reg->id); |
652 | if (reg_type_may_be_refcounted_or_null(t)) | |
653 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 654 | if (t != SCALAR_VALUE) |
61bd5218 | 655 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 656 | if (type_is_pkt_pointer(t)) |
61bd5218 | 657 | verbose(env, ",r=%d", reg->range); |
f1174f77 | 658 | else if (t == CONST_PTR_TO_MAP || |
69c087ba | 659 | t == PTR_TO_MAP_KEY || |
f1174f77 EC |
660 | t == PTR_TO_MAP_VALUE || |
661 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 662 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
663 | reg->map_ptr->key_size, |
664 | reg->map_ptr->value_size); | |
7d1238f2 EC |
665 | if (tnum_is_const(reg->var_off)) { |
666 | /* Typically an immediate SCALAR_VALUE, but | |
667 | * could be a pointer whose offset is too big | |
668 | * for reg->off | |
669 | */ | |
61bd5218 | 670 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
671 | } else { |
672 | if (reg->smin_value != reg->umin_value && | |
673 | reg->smin_value != S64_MIN) | |
61bd5218 | 674 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
675 | (long long)reg->smin_value); |
676 | if (reg->smax_value != reg->umax_value && | |
677 | reg->smax_value != S64_MAX) | |
61bd5218 | 678 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
679 | (long long)reg->smax_value); |
680 | if (reg->umin_value != 0) | |
61bd5218 | 681 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
682 | (unsigned long long)reg->umin_value); |
683 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 684 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
685 | (unsigned long long)reg->umax_value); |
686 | if (!tnum_is_unknown(reg->var_off)) { | |
687 | char tn_buf[48]; | |
f1174f77 | 688 | |
7d1238f2 | 689 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 690 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 691 | } |
3f50f132 JF |
692 | if (reg->s32_min_value != reg->smin_value && |
693 | reg->s32_min_value != S32_MIN) | |
694 | verbose(env, ",s32_min_value=%d", | |
695 | (int)(reg->s32_min_value)); | |
696 | if (reg->s32_max_value != reg->smax_value && | |
697 | reg->s32_max_value != S32_MAX) | |
698 | verbose(env, ",s32_max_value=%d", | |
699 | (int)(reg->s32_max_value)); | |
700 | if (reg->u32_min_value != reg->umin_value && | |
701 | reg->u32_min_value != U32_MIN) | |
702 | verbose(env, ",u32_min_value=%d", | |
703 | (int)(reg->u32_min_value)); | |
704 | if (reg->u32_max_value != reg->umax_value && | |
705 | reg->u32_max_value != U32_MAX) | |
706 | verbose(env, ",u32_max_value=%d", | |
707 | (int)(reg->u32_max_value)); | |
f1174f77 | 708 | } |
61bd5218 | 709 | verbose(env, ")"); |
f1174f77 | 710 | } |
17a52670 | 711 | } |
638f5b90 | 712 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
713 | char types_buf[BPF_REG_SIZE + 1]; |
714 | bool valid = false; | |
715 | int j; | |
716 | ||
717 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
718 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
719 | valid = true; | |
720 | types_buf[j] = slot_type_char[ | |
721 | state->stack[i].slot_type[j]]; | |
722 | } | |
723 | types_buf[BPF_REG_SIZE] = 0; | |
724 | if (!valid) | |
725 | continue; | |
726 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
727 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
27113c59 | 728 | if (is_spilled_reg(&state->stack[i])) { |
b5dc0163 AS |
729 | reg = &state->stack[i].spilled_ptr; |
730 | t = reg->type; | |
731 | verbose(env, "=%s", reg_type_str[t]); | |
732 | if (t == SCALAR_VALUE && reg->precise) | |
733 | verbose(env, "P"); | |
734 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
735 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
736 | } else { | |
8efea21d | 737 | verbose(env, "=%s", types_buf); |
b5dc0163 | 738 | } |
17a52670 | 739 | } |
fd978bf7 JS |
740 | if (state->acquired_refs && state->refs[0].id) { |
741 | verbose(env, " refs=%d", state->refs[0].id); | |
742 | for (i = 1; i < state->acquired_refs; i++) | |
743 | if (state->refs[i].id) | |
744 | verbose(env, ",%d", state->refs[i].id); | |
745 | } | |
bfc6bb74 AS |
746 | if (state->in_callback_fn) |
747 | verbose(env, " cb"); | |
748 | if (state->in_async_callback_fn) | |
749 | verbose(env, " async_cb"); | |
61bd5218 | 750 | verbose(env, "\n"); |
17a52670 AS |
751 | } |
752 | ||
c69431aa LB |
753 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
754 | * small to hold src. This is different from krealloc since we don't want to preserve | |
755 | * the contents of dst. | |
756 | * | |
757 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
758 | * not be allocated. | |
638f5b90 | 759 | */ |
c69431aa | 760 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 761 | { |
c69431aa LB |
762 | size_t bytes; |
763 | ||
764 | if (ZERO_OR_NULL_PTR(src)) | |
765 | goto out; | |
766 | ||
767 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
768 | return NULL; | |
769 | ||
770 | if (ksize(dst) < bytes) { | |
771 | kfree(dst); | |
772 | dst = kmalloc_track_caller(bytes, flags); | |
773 | if (!dst) | |
774 | return NULL; | |
775 | } | |
776 | ||
777 | memcpy(dst, src, bytes); | |
778 | out: | |
779 | return dst ? dst : ZERO_SIZE_PTR; | |
780 | } | |
781 | ||
782 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
783 | * small to hold new_n items. new items are zeroed out if the array grows. | |
784 | * | |
785 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
786 | */ | |
787 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
788 | { | |
789 | if (!new_n || old_n == new_n) | |
790 | goto out; | |
791 | ||
792 | arr = krealloc_array(arr, new_n, size, GFP_KERNEL); | |
793 | if (!arr) | |
794 | return NULL; | |
795 | ||
796 | if (new_n > old_n) | |
797 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
798 | ||
799 | out: | |
800 | return arr ? arr : ZERO_SIZE_PTR; | |
801 | } | |
802 | ||
803 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
804 | { | |
805 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
806 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
807 | if (!dst->refs) | |
808 | return -ENOMEM; | |
809 | ||
810 | dst->acquired_refs = src->acquired_refs; | |
811 | return 0; | |
812 | } | |
813 | ||
814 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
815 | { | |
816 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
817 | ||
818 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
819 | GFP_KERNEL); | |
820 | if (!dst->stack) | |
821 | return -ENOMEM; | |
822 | ||
823 | dst->allocated_stack = src->allocated_stack; | |
824 | return 0; | |
825 | } | |
826 | ||
827 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
828 | { | |
829 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
830 | sizeof(struct bpf_reference_state)); | |
831 | if (!state->refs) | |
832 | return -ENOMEM; | |
833 | ||
834 | state->acquired_refs = n; | |
835 | return 0; | |
836 | } | |
837 | ||
838 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
839 | { | |
840 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
841 | ||
842 | if (old_n >= n) | |
843 | return 0; | |
844 | ||
845 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
846 | if (!state->stack) | |
847 | return -ENOMEM; | |
848 | ||
849 | state->allocated_stack = size; | |
850 | return 0; | |
fd978bf7 JS |
851 | } |
852 | ||
853 | /* Acquire a pointer id from the env and update the state->refs to include | |
854 | * this new pointer reference. | |
855 | * On success, returns a valid pointer id to associate with the register | |
856 | * On failure, returns a negative errno. | |
638f5b90 | 857 | */ |
fd978bf7 | 858 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 859 | { |
fd978bf7 JS |
860 | struct bpf_func_state *state = cur_func(env); |
861 | int new_ofs = state->acquired_refs; | |
862 | int id, err; | |
863 | ||
c69431aa | 864 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
865 | if (err) |
866 | return err; | |
867 | id = ++env->id_gen; | |
868 | state->refs[new_ofs].id = id; | |
869 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 870 | |
fd978bf7 JS |
871 | return id; |
872 | } | |
873 | ||
874 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 875 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
876 | { |
877 | int i, last_idx; | |
878 | ||
fd978bf7 JS |
879 | last_idx = state->acquired_refs - 1; |
880 | for (i = 0; i < state->acquired_refs; i++) { | |
881 | if (state->refs[i].id == ptr_id) { | |
882 | if (last_idx && i != last_idx) | |
883 | memcpy(&state->refs[i], &state->refs[last_idx], | |
884 | sizeof(*state->refs)); | |
885 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
886 | state->acquired_refs--; | |
638f5b90 | 887 | return 0; |
638f5b90 | 888 | } |
638f5b90 | 889 | } |
46f8bc92 | 890 | return -EINVAL; |
fd978bf7 JS |
891 | } |
892 | ||
f4d7e40a AS |
893 | static void free_func_state(struct bpf_func_state *state) |
894 | { | |
5896351e AS |
895 | if (!state) |
896 | return; | |
fd978bf7 | 897 | kfree(state->refs); |
f4d7e40a AS |
898 | kfree(state->stack); |
899 | kfree(state); | |
900 | } | |
901 | ||
b5dc0163 AS |
902 | static void clear_jmp_history(struct bpf_verifier_state *state) |
903 | { | |
904 | kfree(state->jmp_history); | |
905 | state->jmp_history = NULL; | |
906 | state->jmp_history_cnt = 0; | |
907 | } | |
908 | ||
1969db47 AS |
909 | static void free_verifier_state(struct bpf_verifier_state *state, |
910 | bool free_self) | |
638f5b90 | 911 | { |
f4d7e40a AS |
912 | int i; |
913 | ||
914 | for (i = 0; i <= state->curframe; i++) { | |
915 | free_func_state(state->frame[i]); | |
916 | state->frame[i] = NULL; | |
917 | } | |
b5dc0163 | 918 | clear_jmp_history(state); |
1969db47 AS |
919 | if (free_self) |
920 | kfree(state); | |
638f5b90 AS |
921 | } |
922 | ||
923 | /* copy verifier state from src to dst growing dst stack space | |
924 | * when necessary to accommodate larger src stack | |
925 | */ | |
f4d7e40a AS |
926 | static int copy_func_state(struct bpf_func_state *dst, |
927 | const struct bpf_func_state *src) | |
638f5b90 AS |
928 | { |
929 | int err; | |
930 | ||
fd978bf7 JS |
931 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
932 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
933 | if (err) |
934 | return err; | |
638f5b90 AS |
935 | return copy_stack_state(dst, src); |
936 | } | |
937 | ||
f4d7e40a AS |
938 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
939 | const struct bpf_verifier_state *src) | |
940 | { | |
941 | struct bpf_func_state *dst; | |
942 | int i, err; | |
943 | ||
06ab6a50 LB |
944 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
945 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
946 | GFP_USER); | |
947 | if (!dst_state->jmp_history) | |
948 | return -ENOMEM; | |
b5dc0163 AS |
949 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
950 | ||
f4d7e40a AS |
951 | /* if dst has more stack frames then src frame, free them */ |
952 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
953 | free_func_state(dst_state->frame[i]); | |
954 | dst_state->frame[i] = NULL; | |
955 | } | |
979d63d5 | 956 | dst_state->speculative = src->speculative; |
f4d7e40a | 957 | dst_state->curframe = src->curframe; |
d83525ca | 958 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
959 | dst_state->branches = src->branches; |
960 | dst_state->parent = src->parent; | |
b5dc0163 AS |
961 | dst_state->first_insn_idx = src->first_insn_idx; |
962 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
963 | for (i = 0; i <= src->curframe; i++) { |
964 | dst = dst_state->frame[i]; | |
965 | if (!dst) { | |
966 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
967 | if (!dst) | |
968 | return -ENOMEM; | |
969 | dst_state->frame[i] = dst; | |
970 | } | |
971 | err = copy_func_state(dst, src->frame[i]); | |
972 | if (err) | |
973 | return err; | |
974 | } | |
975 | return 0; | |
976 | } | |
977 | ||
2589726d AS |
978 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
979 | { | |
980 | while (st) { | |
981 | u32 br = --st->branches; | |
982 | ||
983 | /* WARN_ON(br > 1) technically makes sense here, | |
984 | * but see comment in push_stack(), hence: | |
985 | */ | |
986 | WARN_ONCE((int)br < 0, | |
987 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
988 | br); | |
989 | if (br) | |
990 | break; | |
991 | st = st->parent; | |
992 | } | |
993 | } | |
994 | ||
638f5b90 | 995 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 996 | int *insn_idx, bool pop_log) |
638f5b90 AS |
997 | { |
998 | struct bpf_verifier_state *cur = env->cur_state; | |
999 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1000 | int err; | |
17a52670 AS |
1001 | |
1002 | if (env->head == NULL) | |
638f5b90 | 1003 | return -ENOENT; |
17a52670 | 1004 | |
638f5b90 AS |
1005 | if (cur) { |
1006 | err = copy_verifier_state(cur, &head->st); | |
1007 | if (err) | |
1008 | return err; | |
1009 | } | |
6f8a57cc AN |
1010 | if (pop_log) |
1011 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1012 | if (insn_idx) |
1013 | *insn_idx = head->insn_idx; | |
17a52670 | 1014 | if (prev_insn_idx) |
638f5b90 AS |
1015 | *prev_insn_idx = head->prev_insn_idx; |
1016 | elem = head->next; | |
1969db47 | 1017 | free_verifier_state(&head->st, false); |
638f5b90 | 1018 | kfree(head); |
17a52670 AS |
1019 | env->head = elem; |
1020 | env->stack_size--; | |
638f5b90 | 1021 | return 0; |
17a52670 AS |
1022 | } |
1023 | ||
58e2af8b | 1024 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1025 | int insn_idx, int prev_insn_idx, |
1026 | bool speculative) | |
17a52670 | 1027 | { |
638f5b90 | 1028 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1029 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1030 | int err; |
17a52670 | 1031 | |
638f5b90 | 1032 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1033 | if (!elem) |
1034 | goto err; | |
1035 | ||
17a52670 AS |
1036 | elem->insn_idx = insn_idx; |
1037 | elem->prev_insn_idx = prev_insn_idx; | |
1038 | elem->next = env->head; | |
6f8a57cc | 1039 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1040 | env->head = elem; |
1041 | env->stack_size++; | |
1969db47 AS |
1042 | err = copy_verifier_state(&elem->st, cur); |
1043 | if (err) | |
1044 | goto err; | |
979d63d5 | 1045 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1046 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1047 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1048 | env->stack_size); | |
17a52670 AS |
1049 | goto err; |
1050 | } | |
2589726d AS |
1051 | if (elem->st.parent) { |
1052 | ++elem->st.parent->branches; | |
1053 | /* WARN_ON(branches > 2) technically makes sense here, | |
1054 | * but | |
1055 | * 1. speculative states will bump 'branches' for non-branch | |
1056 | * instructions | |
1057 | * 2. is_state_visited() heuristics may decide not to create | |
1058 | * a new state for a sequence of branches and all such current | |
1059 | * and cloned states will be pointing to a single parent state | |
1060 | * which might have large 'branches' count. | |
1061 | */ | |
1062 | } | |
17a52670 AS |
1063 | return &elem->st; |
1064 | err: | |
5896351e AS |
1065 | free_verifier_state(env->cur_state, true); |
1066 | env->cur_state = NULL; | |
17a52670 | 1067 | /* pop all elements and return */ |
6f8a57cc | 1068 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1069 | return NULL; |
1070 | } | |
1071 | ||
1072 | #define CALLER_SAVED_REGS 6 | |
1073 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1074 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1075 | }; | |
1076 | ||
f54c7898 DB |
1077 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1078 | struct bpf_reg_state *reg); | |
f1174f77 | 1079 | |
e688c3db AS |
1080 | /* This helper doesn't clear reg->id */ |
1081 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1082 | { |
b03c9f9f EC |
1083 | reg->var_off = tnum_const(imm); |
1084 | reg->smin_value = (s64)imm; | |
1085 | reg->smax_value = (s64)imm; | |
1086 | reg->umin_value = imm; | |
1087 | reg->umax_value = imm; | |
3f50f132 JF |
1088 | |
1089 | reg->s32_min_value = (s32)imm; | |
1090 | reg->s32_max_value = (s32)imm; | |
1091 | reg->u32_min_value = (u32)imm; | |
1092 | reg->u32_max_value = (u32)imm; | |
1093 | } | |
1094 | ||
e688c3db AS |
1095 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1096 | * known to have the value @imm. | |
1097 | */ | |
1098 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1099 | { | |
1100 | /* Clear id, off, and union(map_ptr, range) */ | |
1101 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1102 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
1103 | ___mark_reg_known(reg, imm); | |
1104 | } | |
1105 | ||
3f50f132 JF |
1106 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1107 | { | |
1108 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1109 | reg->s32_min_value = (s32)imm; | |
1110 | reg->s32_max_value = (s32)imm; | |
1111 | reg->u32_min_value = (u32)imm; | |
1112 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1113 | } |
1114 | ||
f1174f77 EC |
1115 | /* Mark the 'variable offset' part of a register as zero. This should be |
1116 | * used only on registers holding a pointer type. | |
1117 | */ | |
1118 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1119 | { |
b03c9f9f | 1120 | __mark_reg_known(reg, 0); |
f1174f77 | 1121 | } |
a9789ef9 | 1122 | |
cc2b14d5 AS |
1123 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1124 | { | |
1125 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1126 | reg->type = SCALAR_VALUE; |
1127 | } | |
1128 | ||
61bd5218 JK |
1129 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1130 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1131 | { |
1132 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1133 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1134 | /* Something bad happened, let's kill all regs */ |
1135 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1136 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1137 | return; |
1138 | } | |
1139 | __mark_reg_known_zero(regs + regno); | |
1140 | } | |
1141 | ||
4ddb7416 DB |
1142 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1143 | { | |
1144 | switch (reg->type) { | |
1145 | case PTR_TO_MAP_VALUE_OR_NULL: { | |
1146 | const struct bpf_map *map = reg->map_ptr; | |
1147 | ||
1148 | if (map->inner_map_meta) { | |
1149 | reg->type = CONST_PTR_TO_MAP; | |
1150 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1151 | /* transfer reg's id which is unique for every map_lookup_elem |
1152 | * as UID of the inner map. | |
1153 | */ | |
34d11a44 AS |
1154 | if (map_value_has_timer(map->inner_map_meta)) |
1155 | reg->map_uid = reg->id; | |
4ddb7416 DB |
1156 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1157 | reg->type = PTR_TO_XDP_SOCK; | |
1158 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1159 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1160 | reg->type = PTR_TO_SOCKET; | |
1161 | } else { | |
1162 | reg->type = PTR_TO_MAP_VALUE; | |
1163 | } | |
1164 | break; | |
1165 | } | |
1166 | case PTR_TO_SOCKET_OR_NULL: | |
1167 | reg->type = PTR_TO_SOCKET; | |
1168 | break; | |
1169 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
1170 | reg->type = PTR_TO_SOCK_COMMON; | |
1171 | break; | |
1172 | case PTR_TO_TCP_SOCK_OR_NULL: | |
1173 | reg->type = PTR_TO_TCP_SOCK; | |
1174 | break; | |
1175 | case PTR_TO_BTF_ID_OR_NULL: | |
1176 | reg->type = PTR_TO_BTF_ID; | |
1177 | break; | |
1178 | case PTR_TO_MEM_OR_NULL: | |
1179 | reg->type = PTR_TO_MEM; | |
1180 | break; | |
1181 | case PTR_TO_RDONLY_BUF_OR_NULL: | |
1182 | reg->type = PTR_TO_RDONLY_BUF; | |
1183 | break; | |
1184 | case PTR_TO_RDWR_BUF_OR_NULL: | |
1185 | reg->type = PTR_TO_RDWR_BUF; | |
1186 | break; | |
1187 | default: | |
33ccec5f | 1188 | WARN_ONCE(1, "unknown nullable register type"); |
4ddb7416 DB |
1189 | } |
1190 | } | |
1191 | ||
de8f3a83 DB |
1192 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1193 | { | |
1194 | return type_is_pkt_pointer(reg->type); | |
1195 | } | |
1196 | ||
1197 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1198 | { | |
1199 | return reg_is_pkt_pointer(reg) || | |
1200 | reg->type == PTR_TO_PACKET_END; | |
1201 | } | |
1202 | ||
1203 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1204 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1205 | enum bpf_reg_type which) | |
1206 | { | |
1207 | /* The register can already have a range from prior markings. | |
1208 | * This is fine as long as it hasn't been advanced from its | |
1209 | * origin. | |
1210 | */ | |
1211 | return reg->type == which && | |
1212 | reg->id == 0 && | |
1213 | reg->off == 0 && | |
1214 | tnum_equals_const(reg->var_off, 0); | |
1215 | } | |
1216 | ||
3f50f132 JF |
1217 | /* Reset the min/max bounds of a register */ |
1218 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1219 | { | |
1220 | reg->smin_value = S64_MIN; | |
1221 | reg->smax_value = S64_MAX; | |
1222 | reg->umin_value = 0; | |
1223 | reg->umax_value = U64_MAX; | |
1224 | ||
1225 | reg->s32_min_value = S32_MIN; | |
1226 | reg->s32_max_value = S32_MAX; | |
1227 | reg->u32_min_value = 0; | |
1228 | reg->u32_max_value = U32_MAX; | |
1229 | } | |
1230 | ||
1231 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1232 | { | |
1233 | reg->smin_value = S64_MIN; | |
1234 | reg->smax_value = S64_MAX; | |
1235 | reg->umin_value = 0; | |
1236 | reg->umax_value = U64_MAX; | |
1237 | } | |
1238 | ||
1239 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1240 | { | |
1241 | reg->s32_min_value = S32_MIN; | |
1242 | reg->s32_max_value = S32_MAX; | |
1243 | reg->u32_min_value = 0; | |
1244 | reg->u32_max_value = U32_MAX; | |
1245 | } | |
1246 | ||
1247 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1248 | { | |
1249 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1250 | ||
1251 | /* min signed is max(sign bit) | min(other bits) */ | |
1252 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1253 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1254 | /* max signed is min(sign bit) | max(other bits) */ | |
1255 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1256 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1257 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1258 | reg->u32_max_value = min(reg->u32_max_value, | |
1259 | (u32)(var32_off.value | var32_off.mask)); | |
1260 | } | |
1261 | ||
1262 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1263 | { |
1264 | /* min signed is max(sign bit) | min(other bits) */ | |
1265 | reg->smin_value = max_t(s64, reg->smin_value, | |
1266 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1267 | /* max signed is min(sign bit) | max(other bits) */ | |
1268 | reg->smax_value = min_t(s64, reg->smax_value, | |
1269 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1270 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1271 | reg->umax_value = min(reg->umax_value, | |
1272 | reg->var_off.value | reg->var_off.mask); | |
1273 | } | |
1274 | ||
3f50f132 JF |
1275 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1276 | { | |
1277 | __update_reg32_bounds(reg); | |
1278 | __update_reg64_bounds(reg); | |
1279 | } | |
1280 | ||
b03c9f9f | 1281 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1282 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1283 | { | |
1284 | /* Learn sign from signed bounds. | |
1285 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1286 | * are the same, so combine. This works even in the negative case, e.g. | |
1287 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1288 | */ | |
1289 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1290 | reg->s32_min_value = reg->u32_min_value = | |
1291 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1292 | reg->s32_max_value = reg->u32_max_value = | |
1293 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1294 | return; | |
1295 | } | |
1296 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1297 | * boundary, so we must be careful. | |
1298 | */ | |
1299 | if ((s32)reg->u32_max_value >= 0) { | |
1300 | /* Positive. We can't learn anything from the smin, but smax | |
1301 | * is positive, hence safe. | |
1302 | */ | |
1303 | reg->s32_min_value = reg->u32_min_value; | |
1304 | reg->s32_max_value = reg->u32_max_value = | |
1305 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1306 | } else if ((s32)reg->u32_min_value < 0) { | |
1307 | /* Negative. We can't learn anything from the smax, but smin | |
1308 | * is negative, hence safe. | |
1309 | */ | |
1310 | reg->s32_min_value = reg->u32_min_value = | |
1311 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1312 | reg->s32_max_value = reg->u32_max_value; | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1317 | { |
1318 | /* Learn sign from signed bounds. | |
1319 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1320 | * are the same, so combine. This works even in the negative case, e.g. | |
1321 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1322 | */ | |
1323 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1324 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1325 | reg->umin_value); | |
1326 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1327 | reg->umax_value); | |
1328 | return; | |
1329 | } | |
1330 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1331 | * boundary, so we must be careful. | |
1332 | */ | |
1333 | if ((s64)reg->umax_value >= 0) { | |
1334 | /* Positive. We can't learn anything from the smin, but smax | |
1335 | * is positive, hence safe. | |
1336 | */ | |
1337 | reg->smin_value = reg->umin_value; | |
1338 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1339 | reg->umax_value); | |
1340 | } else if ((s64)reg->umin_value < 0) { | |
1341 | /* Negative. We can't learn anything from the smax, but smin | |
1342 | * is negative, hence safe. | |
1343 | */ | |
1344 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1345 | reg->umin_value); | |
1346 | reg->smax_value = reg->umax_value; | |
1347 | } | |
1348 | } | |
1349 | ||
3f50f132 JF |
1350 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1351 | { | |
1352 | __reg32_deduce_bounds(reg); | |
1353 | __reg64_deduce_bounds(reg); | |
1354 | } | |
1355 | ||
b03c9f9f EC |
1356 | /* Attempts to improve var_off based on unsigned min/max information */ |
1357 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1358 | { | |
3f50f132 JF |
1359 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1360 | tnum_range(reg->umin_value, | |
1361 | reg->umax_value)); | |
1362 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1363 | tnum_range(reg->u32_min_value, | |
1364 | reg->u32_max_value)); | |
1365 | ||
1366 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1367 | } |
1368 | ||
3f50f132 | 1369 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1370 | { |
3f50f132 JF |
1371 | reg->umin_value = reg->u32_min_value; |
1372 | reg->umax_value = reg->u32_max_value; | |
1373 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds | |
1374 | * but must be positive otherwise set to worse case bounds | |
1375 | * and refine later from tnum. | |
1376 | */ | |
3a71dc36 | 1377 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
3f50f132 JF |
1378 | reg->smax_value = reg->s32_max_value; |
1379 | else | |
1380 | reg->smax_value = U32_MAX; | |
3a71dc36 JF |
1381 | if (reg->s32_min_value >= 0) |
1382 | reg->smin_value = reg->s32_min_value; | |
1383 | else | |
1384 | reg->smin_value = 0; | |
3f50f132 JF |
1385 | } |
1386 | ||
1387 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1388 | { | |
1389 | /* special case when 64-bit register has upper 32-bit register | |
1390 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1391 | * allowing us to use 32-bit bounds directly, | |
1392 | */ | |
1393 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1394 | __reg_assign_32_into_64(reg); | |
1395 | } else { | |
1396 | /* Otherwise the best we can do is push lower 32bit known and | |
1397 | * unknown bits into register (var_off set from jmp logic) | |
1398 | * then learn as much as possible from the 64-bit tnum | |
1399 | * known and unknown bits. The previous smin/smax bounds are | |
1400 | * invalid here because of jmp32 compare so mark them unknown | |
1401 | * so they do not impact tnum bounds calculation. | |
1402 | */ | |
1403 | __mark_reg64_unbounded(reg); | |
1404 | __update_reg_bounds(reg); | |
1405 | } | |
1406 | ||
1407 | /* Intersecting with the old var_off might have improved our bounds | |
1408 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1409 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1410 | */ | |
1411 | __reg_deduce_bounds(reg); | |
1412 | __reg_bound_offset(reg); | |
1413 | __update_reg_bounds(reg); | |
1414 | } | |
1415 | ||
1416 | static bool __reg64_bound_s32(s64 a) | |
1417 | { | |
388e2c0b | 1418 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
1419 | } |
1420 | ||
1421 | static bool __reg64_bound_u32(u64 a) | |
1422 | { | |
b9979db8 | 1423 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
1424 | } |
1425 | ||
1426 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1427 | { | |
1428 | __mark_reg32_unbounded(reg); | |
1429 | ||
b0270958 | 1430 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 1431 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 1432 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 1433 | } |
10bf4e83 | 1434 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 1435 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 1436 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 1437 | } |
3f50f132 JF |
1438 | |
1439 | /* Intersecting with the old var_off might have improved our bounds | |
1440 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1441 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1442 | */ | |
1443 | __reg_deduce_bounds(reg); | |
1444 | __reg_bound_offset(reg); | |
1445 | __update_reg_bounds(reg); | |
b03c9f9f EC |
1446 | } |
1447 | ||
f1174f77 | 1448 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1449 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1450 | struct bpf_reg_state *reg) | |
f1174f77 | 1451 | { |
a9c676bc AS |
1452 | /* |
1453 | * Clear type, id, off, and union(map_ptr, range) and | |
1454 | * padding between 'type' and union | |
1455 | */ | |
1456 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1457 | reg->type = SCALAR_VALUE; |
f1174f77 | 1458 | reg->var_off = tnum_unknown; |
f4d7e40a | 1459 | reg->frameno = 0; |
2c78ee89 | 1460 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1461 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1462 | } |
1463 | ||
61bd5218 JK |
1464 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1465 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1466 | { |
1467 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1468 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1469 | /* Something bad happened, let's kill all regs except FP */ |
1470 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1471 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1472 | return; |
1473 | } | |
f54c7898 | 1474 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1475 | } |
1476 | ||
f54c7898 DB |
1477 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1478 | struct bpf_reg_state *reg) | |
f1174f77 | 1479 | { |
f54c7898 | 1480 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1481 | reg->type = NOT_INIT; |
1482 | } | |
1483 | ||
61bd5218 JK |
1484 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1485 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1486 | { |
1487 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1488 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1489 | /* Something bad happened, let's kill all regs except FP */ |
1490 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1491 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1492 | return; |
1493 | } | |
f54c7898 | 1494 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1495 | } |
1496 | ||
41c48f3a AI |
1497 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1498 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f AN |
1499 | enum bpf_reg_type reg_type, |
1500 | struct btf *btf, u32 btf_id) | |
41c48f3a AI |
1501 | { |
1502 | if (reg_type == SCALAR_VALUE) { | |
1503 | mark_reg_unknown(env, regs, regno); | |
1504 | return; | |
1505 | } | |
1506 | mark_reg_known_zero(env, regs, regno); | |
1507 | regs[regno].type = PTR_TO_BTF_ID; | |
22dc4a0f | 1508 | regs[regno].btf = btf; |
41c48f3a AI |
1509 | regs[regno].btf_id = btf_id; |
1510 | } | |
1511 | ||
5327ed3d | 1512 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1513 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1514 | struct bpf_func_state *state) |
17a52670 | 1515 | { |
f4d7e40a | 1516 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1517 | int i; |
1518 | ||
dc503a8a | 1519 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1520 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1521 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1522 | regs[i].parent = NULL; |
5327ed3d | 1523 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1524 | } |
17a52670 AS |
1525 | |
1526 | /* frame pointer */ | |
f1174f77 | 1527 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1528 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1529 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1530 | } |
1531 | ||
f4d7e40a AS |
1532 | #define BPF_MAIN_FUNC (-1) |
1533 | static void init_func_state(struct bpf_verifier_env *env, | |
1534 | struct bpf_func_state *state, | |
1535 | int callsite, int frameno, int subprogno) | |
1536 | { | |
1537 | state->callsite = callsite; | |
1538 | state->frameno = frameno; | |
1539 | state->subprogno = subprogno; | |
1540 | init_reg_state(env, state); | |
1541 | } | |
1542 | ||
bfc6bb74 AS |
1543 | /* Similar to push_stack(), but for async callbacks */ |
1544 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
1545 | int insn_idx, int prev_insn_idx, | |
1546 | int subprog) | |
1547 | { | |
1548 | struct bpf_verifier_stack_elem *elem; | |
1549 | struct bpf_func_state *frame; | |
1550 | ||
1551 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
1552 | if (!elem) | |
1553 | goto err; | |
1554 | ||
1555 | elem->insn_idx = insn_idx; | |
1556 | elem->prev_insn_idx = prev_insn_idx; | |
1557 | elem->next = env->head; | |
1558 | elem->log_pos = env->log.len_used; | |
1559 | env->head = elem; | |
1560 | env->stack_size++; | |
1561 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
1562 | verbose(env, | |
1563 | "The sequence of %d jumps is too complex for async cb.\n", | |
1564 | env->stack_size); | |
1565 | goto err; | |
1566 | } | |
1567 | /* Unlike push_stack() do not copy_verifier_state(). | |
1568 | * The caller state doesn't matter. | |
1569 | * This is async callback. It starts in a fresh stack. | |
1570 | * Initialize it similar to do_check_common(). | |
1571 | */ | |
1572 | elem->st.branches = 1; | |
1573 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
1574 | if (!frame) | |
1575 | goto err; | |
1576 | init_func_state(env, frame, | |
1577 | BPF_MAIN_FUNC /* callsite */, | |
1578 | 0 /* frameno within this callchain */, | |
1579 | subprog /* subprog number within this prog */); | |
1580 | elem->st.frame[0] = frame; | |
1581 | return &elem->st; | |
1582 | err: | |
1583 | free_verifier_state(env->cur_state, true); | |
1584 | env->cur_state = NULL; | |
1585 | /* pop all elements and return */ | |
1586 | while (!pop_stack(env, NULL, NULL, false)); | |
1587 | return NULL; | |
1588 | } | |
1589 | ||
1590 | ||
17a52670 AS |
1591 | enum reg_arg_type { |
1592 | SRC_OP, /* register is used as source operand */ | |
1593 | DST_OP, /* register is used as destination operand */ | |
1594 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1595 | }; | |
1596 | ||
cc8b0b92 AS |
1597 | static int cmp_subprogs(const void *a, const void *b) |
1598 | { | |
9c8105bd JW |
1599 | return ((struct bpf_subprog_info *)a)->start - |
1600 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1601 | } |
1602 | ||
1603 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1604 | { | |
9c8105bd | 1605 | struct bpf_subprog_info *p; |
cc8b0b92 | 1606 | |
9c8105bd JW |
1607 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1608 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1609 | if (!p) |
1610 | return -ENOENT; | |
9c8105bd | 1611 | return p - env->subprog_info; |
cc8b0b92 AS |
1612 | |
1613 | } | |
1614 | ||
1615 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1616 | { | |
1617 | int insn_cnt = env->prog->len; | |
1618 | int ret; | |
1619 | ||
1620 | if (off >= insn_cnt || off < 0) { | |
1621 | verbose(env, "call to invalid destination\n"); | |
1622 | return -EINVAL; | |
1623 | } | |
1624 | ret = find_subprog(env, off); | |
1625 | if (ret >= 0) | |
282a0f46 | 1626 | return ret; |
4cb3d99c | 1627 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1628 | verbose(env, "too many subprograms\n"); |
1629 | return -E2BIG; | |
1630 | } | |
e6ac2450 | 1631 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
1632 | env->subprog_info[env->subprog_cnt++].start = off; |
1633 | sort(env->subprog_info, env->subprog_cnt, | |
1634 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 1635 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
1636 | } |
1637 | ||
2357672c KKD |
1638 | #define MAX_KFUNC_DESCS 256 |
1639 | #define MAX_KFUNC_BTFS 256 | |
1640 | ||
e6ac2450 MKL |
1641 | struct bpf_kfunc_desc { |
1642 | struct btf_func_model func_model; | |
1643 | u32 func_id; | |
1644 | s32 imm; | |
2357672c KKD |
1645 | u16 offset; |
1646 | }; | |
1647 | ||
1648 | struct bpf_kfunc_btf { | |
1649 | struct btf *btf; | |
1650 | struct module *module; | |
1651 | u16 offset; | |
e6ac2450 MKL |
1652 | }; |
1653 | ||
e6ac2450 MKL |
1654 | struct bpf_kfunc_desc_tab { |
1655 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
1656 | u32 nr_descs; | |
1657 | }; | |
1658 | ||
2357672c KKD |
1659 | struct bpf_kfunc_btf_tab { |
1660 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
1661 | u32 nr_descs; | |
1662 | }; | |
1663 | ||
1664 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
1665 | { |
1666 | const struct bpf_kfunc_desc *d0 = a; | |
1667 | const struct bpf_kfunc_desc *d1 = b; | |
1668 | ||
1669 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
1670 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
1671 | } | |
1672 | ||
1673 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
1674 | { | |
1675 | const struct bpf_kfunc_btf *d0 = a; | |
1676 | const struct bpf_kfunc_btf *d1 = b; | |
1677 | ||
1678 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
1679 | } |
1680 | ||
1681 | static const struct bpf_kfunc_desc * | |
2357672c | 1682 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
1683 | { |
1684 | struct bpf_kfunc_desc desc = { | |
1685 | .func_id = func_id, | |
2357672c | 1686 | .offset = offset, |
e6ac2450 MKL |
1687 | }; |
1688 | struct bpf_kfunc_desc_tab *tab; | |
1689 | ||
1690 | tab = prog->aux->kfunc_tab; | |
1691 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
1692 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
1693 | } | |
1694 | ||
1695 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
1696 | s16 offset, struct module **btf_modp) | |
1697 | { | |
1698 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
1699 | struct bpf_kfunc_btf_tab *tab; | |
1700 | struct bpf_kfunc_btf *b; | |
1701 | struct module *mod; | |
1702 | struct btf *btf; | |
1703 | int btf_fd; | |
1704 | ||
1705 | tab = env->prog->aux->kfunc_btf_tab; | |
1706 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
1707 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
1708 | if (!b) { | |
1709 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
1710 | verbose(env, "too many different module BTFs\n"); | |
1711 | return ERR_PTR(-E2BIG); | |
1712 | } | |
1713 | ||
1714 | if (bpfptr_is_null(env->fd_array)) { | |
1715 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
1716 | return ERR_PTR(-EPROTO); | |
1717 | } | |
1718 | ||
1719 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
1720 | offset * sizeof(btf_fd), | |
1721 | sizeof(btf_fd))) | |
1722 | return ERR_PTR(-EFAULT); | |
1723 | ||
1724 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
1725 | if (IS_ERR(btf)) { |
1726 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 1727 | return btf; |
588cd7ef | 1728 | } |
2357672c KKD |
1729 | |
1730 | if (!btf_is_module(btf)) { | |
1731 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
1732 | btf_put(btf); | |
1733 | return ERR_PTR(-EINVAL); | |
1734 | } | |
1735 | ||
1736 | mod = btf_try_get_module(btf); | |
1737 | if (!mod) { | |
1738 | btf_put(btf); | |
1739 | return ERR_PTR(-ENXIO); | |
1740 | } | |
1741 | ||
1742 | b = &tab->descs[tab->nr_descs++]; | |
1743 | b->btf = btf; | |
1744 | b->module = mod; | |
1745 | b->offset = offset; | |
1746 | ||
1747 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1748 | kfunc_btf_cmp_by_off, NULL); | |
1749 | } | |
1750 | if (btf_modp) | |
1751 | *btf_modp = b->module; | |
1752 | return b->btf; | |
e6ac2450 MKL |
1753 | } |
1754 | ||
2357672c KKD |
1755 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
1756 | { | |
1757 | if (!tab) | |
1758 | return; | |
1759 | ||
1760 | while (tab->nr_descs--) { | |
1761 | module_put(tab->descs[tab->nr_descs].module); | |
1762 | btf_put(tab->descs[tab->nr_descs].btf); | |
1763 | } | |
1764 | kfree(tab); | |
1765 | } | |
1766 | ||
1767 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
1768 | u32 func_id, s16 offset, | |
1769 | struct module **btf_modp) | |
1770 | { | |
2357672c KKD |
1771 | if (offset) { |
1772 | if (offset < 0) { | |
1773 | /* In the future, this can be allowed to increase limit | |
1774 | * of fd index into fd_array, interpreted as u16. | |
1775 | */ | |
1776 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
1777 | return ERR_PTR(-EINVAL); | |
1778 | } | |
1779 | ||
588cd7ef | 1780 | return __find_kfunc_desc_btf(env, offset, btf_modp); |
2357672c KKD |
1781 | } |
1782 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
1783 | } |
1784 | ||
2357672c | 1785 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
1786 | { |
1787 | const struct btf_type *func, *func_proto; | |
2357672c | 1788 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
1789 | struct bpf_kfunc_desc_tab *tab; |
1790 | struct bpf_prog_aux *prog_aux; | |
1791 | struct bpf_kfunc_desc *desc; | |
1792 | const char *func_name; | |
2357672c | 1793 | struct btf *desc_btf; |
e6ac2450 MKL |
1794 | unsigned long addr; |
1795 | int err; | |
1796 | ||
1797 | prog_aux = env->prog->aux; | |
1798 | tab = prog_aux->kfunc_tab; | |
2357672c | 1799 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
1800 | if (!tab) { |
1801 | if (!btf_vmlinux) { | |
1802 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
1803 | return -ENOTSUPP; | |
1804 | } | |
1805 | ||
1806 | if (!env->prog->jit_requested) { | |
1807 | verbose(env, "JIT is required for calling kernel function\n"); | |
1808 | return -ENOTSUPP; | |
1809 | } | |
1810 | ||
1811 | if (!bpf_jit_supports_kfunc_call()) { | |
1812 | verbose(env, "JIT does not support calling kernel function\n"); | |
1813 | return -ENOTSUPP; | |
1814 | } | |
1815 | ||
1816 | if (!env->prog->gpl_compatible) { | |
1817 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
1818 | return -EINVAL; | |
1819 | } | |
1820 | ||
1821 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
1822 | if (!tab) | |
1823 | return -ENOMEM; | |
1824 | prog_aux->kfunc_tab = tab; | |
1825 | } | |
1826 | ||
a5d82727 KKD |
1827 | /* func_id == 0 is always invalid, but instead of returning an error, be |
1828 | * conservative and wait until the code elimination pass before returning | |
1829 | * error, so that invalid calls that get pruned out can be in BPF programs | |
1830 | * loaded from userspace. It is also required that offset be untouched | |
1831 | * for such calls. | |
1832 | */ | |
1833 | if (!func_id && !offset) | |
1834 | return 0; | |
1835 | ||
2357672c KKD |
1836 | if (!btf_tab && offset) { |
1837 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
1838 | if (!btf_tab) | |
1839 | return -ENOMEM; | |
1840 | prog_aux->kfunc_btf_tab = btf_tab; | |
1841 | } | |
1842 | ||
1843 | desc_btf = find_kfunc_desc_btf(env, func_id, offset, NULL); | |
1844 | if (IS_ERR(desc_btf)) { | |
1845 | verbose(env, "failed to find BTF for kernel function\n"); | |
1846 | return PTR_ERR(desc_btf); | |
1847 | } | |
1848 | ||
1849 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
1850 | return 0; |
1851 | ||
1852 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
1853 | verbose(env, "too many different kernel function calls\n"); | |
1854 | return -E2BIG; | |
1855 | } | |
1856 | ||
2357672c | 1857 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
1858 | if (!func || !btf_type_is_func(func)) { |
1859 | verbose(env, "kernel btf_id %u is not a function\n", | |
1860 | func_id); | |
1861 | return -EINVAL; | |
1862 | } | |
2357672c | 1863 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
1864 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
1865 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
1866 | func_id); | |
1867 | return -EINVAL; | |
1868 | } | |
1869 | ||
2357672c | 1870 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
1871 | addr = kallsyms_lookup_name(func_name); |
1872 | if (!addr) { | |
1873 | verbose(env, "cannot find address for kernel function %s\n", | |
1874 | func_name); | |
1875 | return -EINVAL; | |
1876 | } | |
1877 | ||
1878 | desc = &tab->descs[tab->nr_descs++]; | |
1879 | desc->func_id = func_id; | |
3d717fad | 1880 | desc->imm = BPF_CALL_IMM(addr); |
2357672c KKD |
1881 | desc->offset = offset; |
1882 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
1883 | func_proto, func_name, |
1884 | &desc->func_model); | |
1885 | if (!err) | |
1886 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 1887 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
1888 | return err; |
1889 | } | |
1890 | ||
1891 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
1892 | { | |
1893 | const struct bpf_kfunc_desc *d0 = a; | |
1894 | const struct bpf_kfunc_desc *d1 = b; | |
1895 | ||
1896 | if (d0->imm > d1->imm) | |
1897 | return 1; | |
1898 | else if (d0->imm < d1->imm) | |
1899 | return -1; | |
1900 | return 0; | |
1901 | } | |
1902 | ||
1903 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
1904 | { | |
1905 | struct bpf_kfunc_desc_tab *tab; | |
1906 | ||
1907 | tab = prog->aux->kfunc_tab; | |
1908 | if (!tab) | |
1909 | return; | |
1910 | ||
1911 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1912 | kfunc_desc_cmp_by_imm, NULL); | |
1913 | } | |
1914 | ||
1915 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
1916 | { | |
1917 | return !!prog->aux->kfunc_tab; | |
1918 | } | |
1919 | ||
1920 | const struct btf_func_model * | |
1921 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
1922 | const struct bpf_insn *insn) | |
1923 | { | |
1924 | const struct bpf_kfunc_desc desc = { | |
1925 | .imm = insn->imm, | |
1926 | }; | |
1927 | const struct bpf_kfunc_desc *res; | |
1928 | struct bpf_kfunc_desc_tab *tab; | |
1929 | ||
1930 | tab = prog->aux->kfunc_tab; | |
1931 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
1932 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
1933 | ||
1934 | return res ? &res->func_model : NULL; | |
1935 | } | |
1936 | ||
1937 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 1938 | { |
9c8105bd | 1939 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 1940 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 1941 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 1942 | |
f910cefa JW |
1943 | /* Add entry function. */ |
1944 | ret = add_subprog(env, 0); | |
e6ac2450 | 1945 | if (ret) |
f910cefa JW |
1946 | return ret; |
1947 | ||
e6ac2450 MKL |
1948 | for (i = 0; i < insn_cnt; i++, insn++) { |
1949 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
1950 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 1951 | continue; |
e6ac2450 | 1952 | |
2c78ee89 | 1953 | if (!env->bpf_capable) { |
e6ac2450 | 1954 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
1955 | return -EPERM; |
1956 | } | |
e6ac2450 | 1957 | |
3990ed4c | 1958 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 1959 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 1960 | else |
2357672c | 1961 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 1962 | |
cc8b0b92 AS |
1963 | if (ret < 0) |
1964 | return ret; | |
1965 | } | |
1966 | ||
4cb3d99c JW |
1967 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1968 | * logic. 'subprog_cnt' should not be increased. | |
1969 | */ | |
1970 | subprog[env->subprog_cnt].start = insn_cnt; | |
1971 | ||
06ee7115 | 1972 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1973 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1974 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 1975 | |
e6ac2450 MKL |
1976 | return 0; |
1977 | } | |
1978 | ||
1979 | static int check_subprogs(struct bpf_verifier_env *env) | |
1980 | { | |
1981 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
1982 | struct bpf_subprog_info *subprog = env->subprog_info; | |
1983 | struct bpf_insn *insn = env->prog->insnsi; | |
1984 | int insn_cnt = env->prog->len; | |
1985 | ||
cc8b0b92 | 1986 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
1987 | subprog_start = subprog[cur_subprog].start; |
1988 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1989 | for (i = 0; i < insn_cnt; i++) { |
1990 | u8 code = insn[i].code; | |
1991 | ||
7f6e4312 MF |
1992 | if (code == (BPF_JMP | BPF_CALL) && |
1993 | insn[i].imm == BPF_FUNC_tail_call && | |
1994 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
1995 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
1996 | if (BPF_CLASS(code) == BPF_LD && |
1997 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
1998 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 1999 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2000 | goto next; |
2001 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2002 | goto next; | |
2003 | off = i + insn[i].off + 1; | |
2004 | if (off < subprog_start || off >= subprog_end) { | |
2005 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2006 | return -EINVAL; | |
2007 | } | |
2008 | next: | |
2009 | if (i == subprog_end - 1) { | |
2010 | /* to avoid fall-through from one subprog into another | |
2011 | * the last insn of the subprog should be either exit | |
2012 | * or unconditional jump back | |
2013 | */ | |
2014 | if (code != (BPF_JMP | BPF_EXIT) && | |
2015 | code != (BPF_JMP | BPF_JA)) { | |
2016 | verbose(env, "last insn is not an exit or jmp\n"); | |
2017 | return -EINVAL; | |
2018 | } | |
2019 | subprog_start = subprog_end; | |
4cb3d99c JW |
2020 | cur_subprog++; |
2021 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2022 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2023 | } |
2024 | } | |
2025 | return 0; | |
2026 | } | |
2027 | ||
679c782d EC |
2028 | /* Parentage chain of this register (or stack slot) should take care of all |
2029 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2030 | */ | |
f4d7e40a | 2031 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2032 | const struct bpf_reg_state *state, |
5327ed3d | 2033 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2034 | { |
2035 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2036 | int cnt = 0; |
dc503a8a EC |
2037 | |
2038 | while (parent) { | |
2039 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2040 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2041 | break; |
9242b5f5 AS |
2042 | if (parent->live & REG_LIVE_DONE) { |
2043 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
2044 | reg_type_str[parent->type], | |
2045 | parent->var_off.value, parent->off); | |
2046 | return -EFAULT; | |
2047 | } | |
5327ed3d JW |
2048 | /* The first condition is more likely to be true than the |
2049 | * second, checked it first. | |
2050 | */ | |
2051 | if ((parent->live & REG_LIVE_READ) == flag || | |
2052 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2053 | /* The parentage chain never changes and |
2054 | * this parent was already marked as LIVE_READ. | |
2055 | * There is no need to keep walking the chain again and | |
2056 | * keep re-marking all parents as LIVE_READ. | |
2057 | * This case happens when the same register is read | |
2058 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2059 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2060 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2061 | */ |
2062 | break; | |
dc503a8a | 2063 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2064 | parent->live |= flag; |
2065 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2066 | if (flag == REG_LIVE_READ64) | |
2067 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2068 | state = parent; |
2069 | parent = state->parent; | |
f4d7e40a | 2070 | writes = true; |
06ee7115 | 2071 | cnt++; |
dc503a8a | 2072 | } |
06ee7115 AS |
2073 | |
2074 | if (env->longest_mark_read_walk < cnt) | |
2075 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2076 | return 0; |
dc503a8a EC |
2077 | } |
2078 | ||
5327ed3d JW |
2079 | /* This function is supposed to be used by the following 32-bit optimization |
2080 | * code only. It returns TRUE if the source or destination register operates | |
2081 | * on 64-bit, otherwise return FALSE. | |
2082 | */ | |
2083 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2084 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2085 | { | |
2086 | u8 code, class, op; | |
2087 | ||
2088 | code = insn->code; | |
2089 | class = BPF_CLASS(code); | |
2090 | op = BPF_OP(code); | |
2091 | if (class == BPF_JMP) { | |
2092 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2093 | * conservatively. | |
2094 | */ | |
2095 | if (op == BPF_EXIT) | |
2096 | return true; | |
2097 | if (op == BPF_CALL) { | |
2098 | /* BPF to BPF call will reach here because of marking | |
2099 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2100 | * don't care the register def because they are anyway | |
2101 | * marked as NOT_INIT already. | |
2102 | */ | |
2103 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2104 | return false; | |
2105 | /* Helper call will reach here because of arg type | |
2106 | * check, conservatively return TRUE. | |
2107 | */ | |
2108 | if (t == SRC_OP) | |
2109 | return true; | |
2110 | ||
2111 | return false; | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2116 | /* BPF_END always use BPF_ALU class. */ | |
2117 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2118 | return true; | |
2119 | ||
2120 | if (class == BPF_ALU || class == BPF_JMP32) | |
2121 | return false; | |
2122 | ||
2123 | if (class == BPF_LDX) { | |
2124 | if (t != SRC_OP) | |
2125 | return BPF_SIZE(code) == BPF_DW; | |
2126 | /* LDX source must be ptr. */ | |
2127 | return true; | |
2128 | } | |
2129 | ||
2130 | if (class == BPF_STX) { | |
83a28819 IL |
2131 | /* BPF_STX (including atomic variants) has multiple source |
2132 | * operands, one of which is a ptr. Check whether the caller is | |
2133 | * asking about it. | |
2134 | */ | |
2135 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2136 | return true; |
2137 | return BPF_SIZE(code) == BPF_DW; | |
2138 | } | |
2139 | ||
2140 | if (class == BPF_LD) { | |
2141 | u8 mode = BPF_MODE(code); | |
2142 | ||
2143 | /* LD_IMM64 */ | |
2144 | if (mode == BPF_IMM) | |
2145 | return true; | |
2146 | ||
2147 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
2148 | if (t != SRC_OP) | |
2149 | return false; | |
2150 | ||
2151 | /* Implicit ctx ptr. */ | |
2152 | if (regno == BPF_REG_6) | |
2153 | return true; | |
2154 | ||
2155 | /* Explicit source could be any width. */ | |
2156 | return true; | |
2157 | } | |
2158 | ||
2159 | if (class == BPF_ST) | |
2160 | /* The only source register for BPF_ST is a ptr. */ | |
2161 | return true; | |
2162 | ||
2163 | /* Conservatively return true at default. */ | |
2164 | return true; | |
2165 | } | |
2166 | ||
83a28819 IL |
2167 | /* Return the regno defined by the insn, or -1. */ |
2168 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 2169 | { |
83a28819 IL |
2170 | switch (BPF_CLASS(insn->code)) { |
2171 | case BPF_JMP: | |
2172 | case BPF_JMP32: | |
2173 | case BPF_ST: | |
2174 | return -1; | |
2175 | case BPF_STX: | |
2176 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
2177 | (insn->imm & BPF_FETCH)) { | |
2178 | if (insn->imm == BPF_CMPXCHG) | |
2179 | return BPF_REG_0; | |
2180 | else | |
2181 | return insn->src_reg; | |
2182 | } else { | |
2183 | return -1; | |
2184 | } | |
2185 | default: | |
2186 | return insn->dst_reg; | |
2187 | } | |
b325fbca JW |
2188 | } |
2189 | ||
2190 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
2191 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
2192 | { | |
83a28819 IL |
2193 | int dst_reg = insn_def_regno(insn); |
2194 | ||
2195 | if (dst_reg == -1) | |
b325fbca JW |
2196 | return false; |
2197 | ||
83a28819 | 2198 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
2199 | } |
2200 | ||
5327ed3d JW |
2201 | static void mark_insn_zext(struct bpf_verifier_env *env, |
2202 | struct bpf_reg_state *reg) | |
2203 | { | |
2204 | s32 def_idx = reg->subreg_def; | |
2205 | ||
2206 | if (def_idx == DEF_NOT_SUBREG) | |
2207 | return; | |
2208 | ||
2209 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
2210 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
2211 | reg->subreg_def = DEF_NOT_SUBREG; | |
2212 | } | |
2213 | ||
dc503a8a | 2214 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
2215 | enum reg_arg_type t) |
2216 | { | |
f4d7e40a AS |
2217 | struct bpf_verifier_state *vstate = env->cur_state; |
2218 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 2219 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 2220 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 2221 | bool rw64; |
dc503a8a | 2222 | |
17a52670 | 2223 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 2224 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
2225 | return -EINVAL; |
2226 | } | |
2227 | ||
c342dc10 | 2228 | reg = ®s[regno]; |
5327ed3d | 2229 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
2230 | if (t == SRC_OP) { |
2231 | /* check whether register used as source operand can be read */ | |
c342dc10 | 2232 | if (reg->type == NOT_INIT) { |
61bd5218 | 2233 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
2234 | return -EACCES; |
2235 | } | |
679c782d | 2236 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
2237 | if (regno == BPF_REG_FP) |
2238 | return 0; | |
2239 | ||
5327ed3d JW |
2240 | if (rw64) |
2241 | mark_insn_zext(env, reg); | |
2242 | ||
2243 | return mark_reg_read(env, reg, reg->parent, | |
2244 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
2245 | } else { |
2246 | /* check whether register used as dest operand can be written to */ | |
2247 | if (regno == BPF_REG_FP) { | |
61bd5218 | 2248 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
2249 | return -EACCES; |
2250 | } | |
c342dc10 | 2251 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 2252 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 2253 | if (t == DST_OP) |
61bd5218 | 2254 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
2255 | } |
2256 | return 0; | |
2257 | } | |
2258 | ||
b5dc0163 AS |
2259 | /* for any branch, call, exit record the history of jmps in the given state */ |
2260 | static int push_jmp_history(struct bpf_verifier_env *env, | |
2261 | struct bpf_verifier_state *cur) | |
2262 | { | |
2263 | u32 cnt = cur->jmp_history_cnt; | |
2264 | struct bpf_idx_pair *p; | |
2265 | ||
2266 | cnt++; | |
2267 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
2268 | if (!p) | |
2269 | return -ENOMEM; | |
2270 | p[cnt - 1].idx = env->insn_idx; | |
2271 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
2272 | cur->jmp_history = p; | |
2273 | cur->jmp_history_cnt = cnt; | |
2274 | return 0; | |
2275 | } | |
2276 | ||
2277 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
2278 | * history then previous instruction came from straight line execution. | |
2279 | */ | |
2280 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
2281 | u32 *history) | |
2282 | { | |
2283 | u32 cnt = *history; | |
2284 | ||
2285 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
2286 | i = st->jmp_history[cnt - 1].prev_idx; | |
2287 | (*history)--; | |
2288 | } else { | |
2289 | i--; | |
2290 | } | |
2291 | return i; | |
2292 | } | |
2293 | ||
e6ac2450 MKL |
2294 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
2295 | { | |
2296 | const struct btf_type *func; | |
2357672c | 2297 | struct btf *desc_btf; |
e6ac2450 MKL |
2298 | |
2299 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
2300 | return NULL; | |
2301 | ||
2357672c KKD |
2302 | desc_btf = find_kfunc_desc_btf(data, insn->imm, insn->off, NULL); |
2303 | if (IS_ERR(desc_btf)) | |
2304 | return "<error>"; | |
2305 | ||
2306 | func = btf_type_by_id(desc_btf, insn->imm); | |
2307 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
2308 | } |
2309 | ||
b5dc0163 AS |
2310 | /* For given verifier state backtrack_insn() is called from the last insn to |
2311 | * the first insn. Its purpose is to compute a bitmask of registers and | |
2312 | * stack slots that needs precision in the parent verifier state. | |
2313 | */ | |
2314 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
2315 | u32 *reg_mask, u64 *stack_mask) | |
2316 | { | |
2317 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 2318 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
2319 | .cb_print = verbose, |
2320 | .private_data = env, | |
2321 | }; | |
2322 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
2323 | u8 class = BPF_CLASS(insn->code); | |
2324 | u8 opcode = BPF_OP(insn->code); | |
2325 | u8 mode = BPF_MODE(insn->code); | |
2326 | u32 dreg = 1u << insn->dst_reg; | |
2327 | u32 sreg = 1u << insn->src_reg; | |
2328 | u32 spi; | |
2329 | ||
2330 | if (insn->code == 0) | |
2331 | return 0; | |
2332 | if (env->log.level & BPF_LOG_LEVEL) { | |
2333 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
2334 | verbose(env, "%d: ", idx); | |
2335 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
2336 | } | |
2337 | ||
2338 | if (class == BPF_ALU || class == BPF_ALU64) { | |
2339 | if (!(*reg_mask & dreg)) | |
2340 | return 0; | |
2341 | if (opcode == BPF_MOV) { | |
2342 | if (BPF_SRC(insn->code) == BPF_X) { | |
2343 | /* dreg = sreg | |
2344 | * dreg needs precision after this insn | |
2345 | * sreg needs precision before this insn | |
2346 | */ | |
2347 | *reg_mask &= ~dreg; | |
2348 | *reg_mask |= sreg; | |
2349 | } else { | |
2350 | /* dreg = K | |
2351 | * dreg needs precision after this insn. | |
2352 | * Corresponding register is already marked | |
2353 | * as precise=true in this verifier state. | |
2354 | * No further markings in parent are necessary | |
2355 | */ | |
2356 | *reg_mask &= ~dreg; | |
2357 | } | |
2358 | } else { | |
2359 | if (BPF_SRC(insn->code) == BPF_X) { | |
2360 | /* dreg += sreg | |
2361 | * both dreg and sreg need precision | |
2362 | * before this insn | |
2363 | */ | |
2364 | *reg_mask |= sreg; | |
2365 | } /* else dreg += K | |
2366 | * dreg still needs precision before this insn | |
2367 | */ | |
2368 | } | |
2369 | } else if (class == BPF_LDX) { | |
2370 | if (!(*reg_mask & dreg)) | |
2371 | return 0; | |
2372 | *reg_mask &= ~dreg; | |
2373 | ||
2374 | /* scalars can only be spilled into stack w/o losing precision. | |
2375 | * Load from any other memory can be zero extended. | |
2376 | * The desire to keep that precision is already indicated | |
2377 | * by 'precise' mark in corresponding register of this state. | |
2378 | * No further tracking necessary. | |
2379 | */ | |
2380 | if (insn->src_reg != BPF_REG_FP) | |
2381 | return 0; | |
2382 | if (BPF_SIZE(insn->code) != BPF_DW) | |
2383 | return 0; | |
2384 | ||
2385 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
2386 | * that [fp - off] slot contains scalar that needs to be | |
2387 | * tracked with precision | |
2388 | */ | |
2389 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2390 | if (spi >= 64) { | |
2391 | verbose(env, "BUG spi %d\n", spi); | |
2392 | WARN_ONCE(1, "verifier backtracking bug"); | |
2393 | return -EFAULT; | |
2394 | } | |
2395 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 2396 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 2397 | if (*reg_mask & dreg) |
b3b50f05 | 2398 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
2399 | * to access memory. It means backtracking |
2400 | * encountered a case of pointer subtraction. | |
2401 | */ | |
2402 | return -ENOTSUPP; | |
2403 | /* scalars can only be spilled into stack */ | |
2404 | if (insn->dst_reg != BPF_REG_FP) | |
2405 | return 0; | |
2406 | if (BPF_SIZE(insn->code) != BPF_DW) | |
2407 | return 0; | |
2408 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2409 | if (spi >= 64) { | |
2410 | verbose(env, "BUG spi %d\n", spi); | |
2411 | WARN_ONCE(1, "verifier backtracking bug"); | |
2412 | return -EFAULT; | |
2413 | } | |
2414 | if (!(*stack_mask & (1ull << spi))) | |
2415 | return 0; | |
2416 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
2417 | if (class == BPF_STX) |
2418 | *reg_mask |= sreg; | |
b5dc0163 AS |
2419 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
2420 | if (opcode == BPF_CALL) { | |
2421 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2422 | return -ENOTSUPP; | |
2423 | /* regular helper call sets R0 */ | |
2424 | *reg_mask &= ~1; | |
2425 | if (*reg_mask & 0x3f) { | |
2426 | /* if backtracing was looking for registers R1-R5 | |
2427 | * they should have been found already. | |
2428 | */ | |
2429 | verbose(env, "BUG regs %x\n", *reg_mask); | |
2430 | WARN_ONCE(1, "verifier backtracking bug"); | |
2431 | return -EFAULT; | |
2432 | } | |
2433 | } else if (opcode == BPF_EXIT) { | |
2434 | return -ENOTSUPP; | |
2435 | } | |
2436 | } else if (class == BPF_LD) { | |
2437 | if (!(*reg_mask & dreg)) | |
2438 | return 0; | |
2439 | *reg_mask &= ~dreg; | |
2440 | /* It's ld_imm64 or ld_abs or ld_ind. | |
2441 | * For ld_imm64 no further tracking of precision | |
2442 | * into parent is necessary | |
2443 | */ | |
2444 | if (mode == BPF_IND || mode == BPF_ABS) | |
2445 | /* to be analyzed */ | |
2446 | return -ENOTSUPP; | |
b5dc0163 AS |
2447 | } |
2448 | return 0; | |
2449 | } | |
2450 | ||
2451 | /* the scalar precision tracking algorithm: | |
2452 | * . at the start all registers have precise=false. | |
2453 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
2454 | * . once precise value of the scalar register is used in: | |
2455 | * . ptr + scalar alu | |
2456 | * . if (scalar cond K|scalar) | |
2457 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
2458 | * backtrack through the verifier states and mark all registers and | |
2459 | * stack slots with spilled constants that these scalar regisers | |
2460 | * should be precise. | |
2461 | * . during state pruning two registers (or spilled stack slots) | |
2462 | * are equivalent if both are not precise. | |
2463 | * | |
2464 | * Note the verifier cannot simply walk register parentage chain, | |
2465 | * since many different registers and stack slots could have been | |
2466 | * used to compute single precise scalar. | |
2467 | * | |
2468 | * The approach of starting with precise=true for all registers and then | |
2469 | * backtrack to mark a register as not precise when the verifier detects | |
2470 | * that program doesn't care about specific value (e.g., when helper | |
2471 | * takes register as ARG_ANYTHING parameter) is not safe. | |
2472 | * | |
2473 | * It's ok to walk single parentage chain of the verifier states. | |
2474 | * It's possible that this backtracking will go all the way till 1st insn. | |
2475 | * All other branches will be explored for needing precision later. | |
2476 | * | |
2477 | * The backtracking needs to deal with cases like: | |
2478 | * 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) | |
2479 | * r9 -= r8 | |
2480 | * r5 = r9 | |
2481 | * if r5 > 0x79f goto pc+7 | |
2482 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
2483 | * r5 += 1 | |
2484 | * ... | |
2485 | * call bpf_perf_event_output#25 | |
2486 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
2487 | * | |
2488 | * and this case: | |
2489 | * r6 = 1 | |
2490 | * call foo // uses callee's r6 inside to compute r0 | |
2491 | * r0 += r6 | |
2492 | * if r0 == 0 goto | |
2493 | * | |
2494 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
2495 | * | |
2496 | * Also if parent's curframe > frame where backtracking started, | |
2497 | * the verifier need to mark registers in both frames, otherwise callees | |
2498 | * may incorrectly prune callers. This is similar to | |
2499 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
2500 | * | |
2501 | * For now backtracking falls back into conservative marking. | |
2502 | */ | |
2503 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
2504 | struct bpf_verifier_state *st) | |
2505 | { | |
2506 | struct bpf_func_state *func; | |
2507 | struct bpf_reg_state *reg; | |
2508 | int i, j; | |
2509 | ||
2510 | /* big hammer: mark all scalars precise in this path. | |
2511 | * pop_stack may still get !precise scalars. | |
2512 | */ | |
2513 | for (; st; st = st->parent) | |
2514 | for (i = 0; i <= st->curframe; i++) { | |
2515 | func = st->frame[i]; | |
2516 | for (j = 0; j < BPF_REG_FP; j++) { | |
2517 | reg = &func->regs[j]; | |
2518 | if (reg->type != SCALAR_VALUE) | |
2519 | continue; | |
2520 | reg->precise = true; | |
2521 | } | |
2522 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 2523 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
2524 | continue; |
2525 | reg = &func->stack[j].spilled_ptr; | |
2526 | if (reg->type != SCALAR_VALUE) | |
2527 | continue; | |
2528 | reg->precise = true; | |
2529 | } | |
2530 | } | |
2531 | } | |
2532 | ||
a3ce685d AS |
2533 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
2534 | int spi) | |
b5dc0163 AS |
2535 | { |
2536 | struct bpf_verifier_state *st = env->cur_state; | |
2537 | int first_idx = st->first_insn_idx; | |
2538 | int last_idx = env->insn_idx; | |
2539 | struct bpf_func_state *func; | |
2540 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2541 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2542 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2543 | bool skip_first = true; |
a3ce685d | 2544 | bool new_marks = false; |
b5dc0163 AS |
2545 | int i, err; |
2546 | ||
2c78ee89 | 2547 | if (!env->bpf_capable) |
b5dc0163 AS |
2548 | return 0; |
2549 | ||
2550 | func = st->frame[st->curframe]; | |
a3ce685d AS |
2551 | if (regno >= 0) { |
2552 | reg = &func->regs[regno]; | |
2553 | if (reg->type != SCALAR_VALUE) { | |
2554 | WARN_ONCE(1, "backtracing misuse"); | |
2555 | return -EFAULT; | |
2556 | } | |
2557 | if (!reg->precise) | |
2558 | new_marks = true; | |
2559 | else | |
2560 | reg_mask = 0; | |
2561 | reg->precise = true; | |
b5dc0163 | 2562 | } |
b5dc0163 | 2563 | |
a3ce685d | 2564 | while (spi >= 0) { |
27113c59 | 2565 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
2566 | stack_mask = 0; |
2567 | break; | |
2568 | } | |
2569 | reg = &func->stack[spi].spilled_ptr; | |
2570 | if (reg->type != SCALAR_VALUE) { | |
2571 | stack_mask = 0; | |
2572 | break; | |
2573 | } | |
2574 | if (!reg->precise) | |
2575 | new_marks = true; | |
2576 | else | |
2577 | stack_mask = 0; | |
2578 | reg->precise = true; | |
2579 | break; | |
2580 | } | |
2581 | ||
2582 | if (!new_marks) | |
2583 | return 0; | |
2584 | if (!reg_mask && !stack_mask) | |
2585 | return 0; | |
b5dc0163 AS |
2586 | for (;;) { |
2587 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2588 | u32 history = st->jmp_history_cnt; |
2589 | ||
2590 | if (env->log.level & BPF_LOG_LEVEL) | |
2591 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
2592 | for (i = last_idx;;) { | |
2593 | if (skip_first) { | |
2594 | err = 0; | |
2595 | skip_first = false; | |
2596 | } else { | |
2597 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2598 | } | |
2599 | if (err == -ENOTSUPP) { | |
2600 | mark_all_scalars_precise(env, st); | |
2601 | return 0; | |
2602 | } else if (err) { | |
2603 | return err; | |
2604 | } | |
2605 | if (!reg_mask && !stack_mask) | |
2606 | /* Found assignment(s) into tracked register in this state. | |
2607 | * Since this state is already marked, just return. | |
2608 | * Nothing to be tracked further in the parent state. | |
2609 | */ | |
2610 | return 0; | |
2611 | if (i == first_idx) | |
2612 | break; | |
2613 | i = get_prev_insn_idx(st, i, &history); | |
2614 | if (i >= env->prog->len) { | |
2615 | /* This can happen if backtracking reached insn 0 | |
2616 | * and there are still reg_mask or stack_mask | |
2617 | * to backtrack. | |
2618 | * It means the backtracking missed the spot where | |
2619 | * particular register was initialized with a constant. | |
2620 | */ | |
2621 | verbose(env, "BUG backtracking idx %d\n", i); | |
2622 | WARN_ONCE(1, "verifier backtracking bug"); | |
2623 | return -EFAULT; | |
2624 | } | |
2625 | } | |
2626 | st = st->parent; | |
2627 | if (!st) | |
2628 | break; | |
2629 | ||
a3ce685d | 2630 | new_marks = false; |
b5dc0163 AS |
2631 | func = st->frame[st->curframe]; |
2632 | bitmap_from_u64(mask, reg_mask); | |
2633 | for_each_set_bit(i, mask, 32) { | |
2634 | reg = &func->regs[i]; | |
a3ce685d AS |
2635 | if (reg->type != SCALAR_VALUE) { |
2636 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2637 | continue; |
a3ce685d | 2638 | } |
b5dc0163 AS |
2639 | if (!reg->precise) |
2640 | new_marks = true; | |
2641 | reg->precise = true; | |
2642 | } | |
2643 | ||
2644 | bitmap_from_u64(mask, stack_mask); | |
2645 | for_each_set_bit(i, mask, 64) { | |
2646 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2647 | /* the sequence of instructions: |
2648 | * 2: (bf) r3 = r10 | |
2649 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2650 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2651 | * doesn't contain jmps. It's backtracked | |
2652 | * as a single block. | |
2653 | * During backtracking insn 3 is not recognized as | |
2654 | * stack access, so at the end of backtracking | |
2655 | * stack slot fp-8 is still marked in stack_mask. | |
2656 | * However the parent state may not have accessed | |
2657 | * fp-8 and it's "unallocated" stack space. | |
2658 | * In such case fallback to conservative. | |
b5dc0163 | 2659 | */ |
2339cd6c AS |
2660 | mark_all_scalars_precise(env, st); |
2661 | return 0; | |
b5dc0163 AS |
2662 | } |
2663 | ||
27113c59 | 2664 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 2665 | stack_mask &= ~(1ull << i); |
b5dc0163 | 2666 | continue; |
a3ce685d | 2667 | } |
b5dc0163 | 2668 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2669 | if (reg->type != SCALAR_VALUE) { |
2670 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2671 | continue; |
a3ce685d | 2672 | } |
b5dc0163 AS |
2673 | if (!reg->precise) |
2674 | new_marks = true; | |
2675 | reg->precise = true; | |
2676 | } | |
2677 | if (env->log.level & BPF_LOG_LEVEL) { | |
2678 | print_verifier_state(env, func); | |
2679 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
2680 | new_marks ? "didn't have" : "already had", | |
2681 | reg_mask, stack_mask); | |
2682 | } | |
2683 | ||
a3ce685d AS |
2684 | if (!reg_mask && !stack_mask) |
2685 | break; | |
b5dc0163 AS |
2686 | if (!new_marks) |
2687 | break; | |
2688 | ||
2689 | last_idx = st->last_insn_idx; | |
2690 | first_idx = st->first_insn_idx; | |
2691 | } | |
2692 | return 0; | |
2693 | } | |
2694 | ||
a3ce685d AS |
2695 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
2696 | { | |
2697 | return __mark_chain_precision(env, regno, -1); | |
2698 | } | |
2699 | ||
2700 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2701 | { | |
2702 | return __mark_chain_precision(env, -1, spi); | |
2703 | } | |
b5dc0163 | 2704 | |
1be7f75d AS |
2705 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2706 | { | |
2707 | switch (type) { | |
2708 | case PTR_TO_MAP_VALUE: | |
2709 | case PTR_TO_MAP_VALUE_OR_NULL: | |
2710 | case PTR_TO_STACK: | |
2711 | case PTR_TO_CTX: | |
969bf05e | 2712 | case PTR_TO_PACKET: |
de8f3a83 | 2713 | case PTR_TO_PACKET_META: |
969bf05e | 2714 | case PTR_TO_PACKET_END: |
d58e468b | 2715 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2716 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
2717 | case PTR_TO_SOCKET: |
2718 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
2719 | case PTR_TO_SOCK_COMMON: |
2720 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
2721 | case PTR_TO_TCP_SOCK: |
2722 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 2723 | case PTR_TO_XDP_SOCK: |
65726b5b | 2724 | case PTR_TO_BTF_ID: |
b121b341 | 2725 | case PTR_TO_BTF_ID_OR_NULL: |
afbf21dc YS |
2726 | case PTR_TO_RDONLY_BUF: |
2727 | case PTR_TO_RDONLY_BUF_OR_NULL: | |
2728 | case PTR_TO_RDWR_BUF: | |
2729 | case PTR_TO_RDWR_BUF_OR_NULL: | |
eaa6bcb7 | 2730 | case PTR_TO_PERCPU_BTF_ID: |
744ea4e3 GR |
2731 | case PTR_TO_MEM: |
2732 | case PTR_TO_MEM_OR_NULL: | |
69c087ba YS |
2733 | case PTR_TO_FUNC: |
2734 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
2735 | return true; |
2736 | default: | |
2737 | return false; | |
2738 | } | |
2739 | } | |
2740 | ||
cc2b14d5 AS |
2741 | /* Does this register contain a constant zero? */ |
2742 | static bool register_is_null(struct bpf_reg_state *reg) | |
2743 | { | |
2744 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2745 | } | |
2746 | ||
f7cf25b2 AS |
2747 | static bool register_is_const(struct bpf_reg_state *reg) |
2748 | { | |
2749 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2750 | } | |
2751 | ||
5689d49b YS |
2752 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
2753 | { | |
2754 | return tnum_is_unknown(reg->var_off) && | |
2755 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
2756 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
2757 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
2758 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
2759 | } | |
2760 | ||
2761 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
2762 | { | |
2763 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
2764 | } | |
2765 | ||
6e7e63cb JH |
2766 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2767 | const struct bpf_reg_state *reg) | |
2768 | { | |
2769 | if (allow_ptr_leaks) | |
2770 | return false; | |
2771 | ||
2772 | return reg->type != SCALAR_VALUE; | |
2773 | } | |
2774 | ||
f7cf25b2 | 2775 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
2776 | int spi, struct bpf_reg_state *reg, |
2777 | int size) | |
f7cf25b2 AS |
2778 | { |
2779 | int i; | |
2780 | ||
2781 | state->stack[spi].spilled_ptr = *reg; | |
354e8f19 MKL |
2782 | if (size == BPF_REG_SIZE) |
2783 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 2784 | |
354e8f19 MKL |
2785 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
2786 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 2787 | |
354e8f19 MKL |
2788 | /* size < 8 bytes spill */ |
2789 | for (; i; i--) | |
2790 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
2791 | } |
2792 | ||
01f810ac | 2793 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
2794 | * stack boundary and alignment are checked in check_mem_access() |
2795 | */ | |
01f810ac AM |
2796 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
2797 | /* stack frame we're writing to */ | |
2798 | struct bpf_func_state *state, | |
2799 | int off, int size, int value_regno, | |
2800 | int insn_idx) | |
17a52670 | 2801 | { |
f4d7e40a | 2802 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 2803 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 2804 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 2805 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 2806 | |
c69431aa | 2807 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
2808 | if (err) |
2809 | return err; | |
9c399760 AS |
2810 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
2811 | * so it's aligned access and [off, off + size) are within stack limits | |
2812 | */ | |
638f5b90 AS |
2813 | if (!env->allow_ptr_leaks && |
2814 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
2815 | size != BPF_REG_SIZE) { | |
2816 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
2817 | return -EACCES; | |
2818 | } | |
17a52670 | 2819 | |
f4d7e40a | 2820 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
2821 | if (value_regno >= 0) |
2822 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
2823 | if (!env->bypass_spec_v4) { |
2824 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
2825 | ||
2826 | for (i = 0; i < size; i++) { | |
2827 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { | |
2828 | sanitize = true; | |
2829 | break; | |
2830 | } | |
2831 | } | |
2832 | ||
2833 | if (sanitize) | |
2834 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
2835 | } | |
17a52670 | 2836 | |
354e8f19 | 2837 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 2838 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
2839 | if (dst_reg != BPF_REG_FP) { |
2840 | /* The backtracking logic can only recognize explicit | |
2841 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 2842 | * scalar via different register has to be conservative. |
b5dc0163 AS |
2843 | * Backtrack from here and mark all registers as precise |
2844 | * that contributed into 'reg' being a constant. | |
2845 | */ | |
2846 | err = mark_chain_precision(env, value_regno); | |
2847 | if (err) | |
2848 | return err; | |
2849 | } | |
354e8f19 | 2850 | save_register_state(state, spi, reg, size); |
f7cf25b2 | 2851 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 2852 | /* register containing pointer is being spilled into stack */ |
9c399760 | 2853 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 2854 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 2855 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
2856 | return -EACCES; |
2857 | } | |
f7cf25b2 | 2858 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2859 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2860 | return -EINVAL; | |
2861 | } | |
354e8f19 | 2862 | save_register_state(state, spi, reg, size); |
9c399760 | 2863 | } else { |
cc2b14d5 AS |
2864 | u8 type = STACK_MISC; |
2865 | ||
679c782d EC |
2866 | /* regular write of data into stack destroys any spilled ptr */ |
2867 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d | 2868 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
27113c59 | 2869 | if (is_spilled_reg(&state->stack[spi])) |
0bae2d4d | 2870 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 2871 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 2872 | |
cc2b14d5 AS |
2873 | /* only mark the slot as written if all 8 bytes were written |
2874 | * otherwise read propagation may incorrectly stop too soon | |
2875 | * when stack slots are partially written. | |
2876 | * This heuristic means that read propagation will be | |
2877 | * conservative, since it will add reg_live_read marks | |
2878 | * to stack slots all the way to first state when programs | |
2879 | * writes+reads less than 8 bytes | |
2880 | */ | |
2881 | if (size == BPF_REG_SIZE) | |
2882 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2883 | ||
2884 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2885 | if (reg && register_is_null(reg)) { |
2886 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2887 | err = mark_chain_precision(env, value_regno); | |
2888 | if (err) | |
2889 | return err; | |
cc2b14d5 | 2890 | type = STACK_ZERO; |
b5dc0163 | 2891 | } |
cc2b14d5 | 2892 | |
0bae2d4d | 2893 | /* Mark slots affected by this stack write. */ |
9c399760 | 2894 | for (i = 0; i < size; i++) |
638f5b90 | 2895 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2896 | type; |
17a52670 AS |
2897 | } |
2898 | return 0; | |
2899 | } | |
2900 | ||
01f810ac AM |
2901 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
2902 | * known to contain a variable offset. | |
2903 | * This function checks whether the write is permitted and conservatively | |
2904 | * tracks the effects of the write, considering that each stack slot in the | |
2905 | * dynamic range is potentially written to. | |
2906 | * | |
2907 | * 'off' includes 'regno->off'. | |
2908 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
2909 | * the stack. | |
2910 | * | |
2911 | * Spilled pointers in range are not marked as written because we don't know | |
2912 | * what's going to be actually written. This means that read propagation for | |
2913 | * future reads cannot be terminated by this write. | |
2914 | * | |
2915 | * For privileged programs, uninitialized stack slots are considered | |
2916 | * initialized by this write (even though we don't know exactly what offsets | |
2917 | * are going to be written to). The idea is that we don't want the verifier to | |
2918 | * reject future reads that access slots written to through variable offsets. | |
2919 | */ | |
2920 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
2921 | /* func where register points to */ | |
2922 | struct bpf_func_state *state, | |
2923 | int ptr_regno, int off, int size, | |
2924 | int value_regno, int insn_idx) | |
2925 | { | |
2926 | struct bpf_func_state *cur; /* state of the current function */ | |
2927 | int min_off, max_off; | |
2928 | int i, err; | |
2929 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
2930 | bool writing_zero = false; | |
2931 | /* set if the fact that we're writing a zero is used to let any | |
2932 | * stack slots remain STACK_ZERO | |
2933 | */ | |
2934 | bool zero_used = false; | |
2935 | ||
2936 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
2937 | ptr_reg = &cur->regs[ptr_regno]; | |
2938 | min_off = ptr_reg->smin_value + off; | |
2939 | max_off = ptr_reg->smax_value + off + size; | |
2940 | if (value_regno >= 0) | |
2941 | value_reg = &cur->regs[value_regno]; | |
2942 | if (value_reg && register_is_null(value_reg)) | |
2943 | writing_zero = true; | |
2944 | ||
c69431aa | 2945 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
2946 | if (err) |
2947 | return err; | |
2948 | ||
2949 | ||
2950 | /* Variable offset writes destroy any spilled pointers in range. */ | |
2951 | for (i = min_off; i < max_off; i++) { | |
2952 | u8 new_type, *stype; | |
2953 | int slot, spi; | |
2954 | ||
2955 | slot = -i - 1; | |
2956 | spi = slot / BPF_REG_SIZE; | |
2957 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
2958 | ||
2959 | if (!env->allow_ptr_leaks | |
2960 | && *stype != NOT_INIT | |
2961 | && *stype != SCALAR_VALUE) { | |
2962 | /* Reject the write if there's are spilled pointers in | |
2963 | * range. If we didn't reject here, the ptr status | |
2964 | * would be erased below (even though not all slots are | |
2965 | * actually overwritten), possibly opening the door to | |
2966 | * leaks. | |
2967 | */ | |
2968 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
2969 | insn_idx, i); | |
2970 | return -EINVAL; | |
2971 | } | |
2972 | ||
2973 | /* Erase all spilled pointers. */ | |
2974 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
2975 | ||
2976 | /* Update the slot type. */ | |
2977 | new_type = STACK_MISC; | |
2978 | if (writing_zero && *stype == STACK_ZERO) { | |
2979 | new_type = STACK_ZERO; | |
2980 | zero_used = true; | |
2981 | } | |
2982 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
2983 | * pretend that it will be initialized by this write. The slot | |
2984 | * might not actually be written to, and so if we mark it as | |
2985 | * initialized future reads might leak uninitialized memory. | |
2986 | * For privileged programs, we will accept such reads to slots | |
2987 | * that may or may not be written because, if we're reject | |
2988 | * them, the error would be too confusing. | |
2989 | */ | |
2990 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
2991 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
2992 | insn_idx, i); | |
2993 | return -EINVAL; | |
2994 | } | |
2995 | *stype = new_type; | |
2996 | } | |
2997 | if (zero_used) { | |
2998 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2999 | err = mark_chain_precision(env, value_regno); | |
3000 | if (err) | |
3001 | return err; | |
3002 | } | |
3003 | return 0; | |
3004 | } | |
3005 | ||
3006 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
3007 | * max_off), we set the register's type according to the types of the | |
3008 | * respective stack slots. If all the stack values are known to be zeros, then | |
3009 | * so is the destination reg. Otherwise, the register is considered to be | |
3010 | * SCALAR. This function does not deal with register filling; the caller must | |
3011 | * ensure that all spilled registers in the stack range have been marked as | |
3012 | * read. | |
3013 | */ | |
3014 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
3015 | /* func where src register points to */ | |
3016 | struct bpf_func_state *ptr_state, | |
3017 | int min_off, int max_off, int dst_regno) | |
3018 | { | |
3019 | struct bpf_verifier_state *vstate = env->cur_state; | |
3020 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3021 | int i, slot, spi; | |
3022 | u8 *stype; | |
3023 | int zeros = 0; | |
3024 | ||
3025 | for (i = min_off; i < max_off; i++) { | |
3026 | slot = -i - 1; | |
3027 | spi = slot / BPF_REG_SIZE; | |
3028 | stype = ptr_state->stack[spi].slot_type; | |
3029 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
3030 | break; | |
3031 | zeros++; | |
3032 | } | |
3033 | if (zeros == max_off - min_off) { | |
3034 | /* any access_size read into register is zero extended, | |
3035 | * so the whole register == const_zero | |
3036 | */ | |
3037 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
3038 | /* backtracking doesn't support STACK_ZERO yet, | |
3039 | * so mark it precise here, so that later | |
3040 | * backtracking can stop here. | |
3041 | * Backtracking may not need this if this register | |
3042 | * doesn't participate in pointer adjustment. | |
3043 | * Forward propagation of precise flag is not | |
3044 | * necessary either. This mark is only to stop | |
3045 | * backtracking. Any register that contributed | |
3046 | * to const 0 was marked precise before spill. | |
3047 | */ | |
3048 | state->regs[dst_regno].precise = true; | |
3049 | } else { | |
3050 | /* have read misc data from the stack */ | |
3051 | mark_reg_unknown(env, state->regs, dst_regno); | |
3052 | } | |
3053 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
3054 | } | |
3055 | ||
3056 | /* Read the stack at 'off' and put the results into the register indicated by | |
3057 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
3058 | * spilled reg. | |
3059 | * | |
3060 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
3061 | * register. | |
3062 | * | |
3063 | * The access is assumed to be within the current stack bounds. | |
3064 | */ | |
3065 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
3066 | /* func where src register points to */ | |
3067 | struct bpf_func_state *reg_state, | |
3068 | int off, int size, int dst_regno) | |
17a52670 | 3069 | { |
f4d7e40a AS |
3070 | struct bpf_verifier_state *vstate = env->cur_state; |
3071 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 3072 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 3073 | struct bpf_reg_state *reg; |
354e8f19 | 3074 | u8 *stype, type; |
17a52670 | 3075 | |
f4d7e40a | 3076 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 3077 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 3078 | |
27113c59 | 3079 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
3080 | u8 spill_size = 1; |
3081 | ||
3082 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
3083 | spill_size++; | |
354e8f19 | 3084 | |
f30d4968 | 3085 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
3086 | if (reg->type != SCALAR_VALUE) { |
3087 | verbose_linfo(env, env->insn_idx, "; "); | |
3088 | verbose(env, "invalid size of register fill\n"); | |
3089 | return -EACCES; | |
3090 | } | |
354e8f19 MKL |
3091 | |
3092 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
3093 | if (dst_regno < 0) | |
3094 | return 0; | |
3095 | ||
f30d4968 | 3096 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
3097 | /* The earlier check_reg_arg() has decided the |
3098 | * subreg_def for this insn. Save it first. | |
3099 | */ | |
3100 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
3101 | ||
3102 | state->regs[dst_regno] = *reg; | |
3103 | state->regs[dst_regno].subreg_def = subreg_def; | |
3104 | } else { | |
3105 | for (i = 0; i < size; i++) { | |
3106 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
3107 | if (type == STACK_SPILL) | |
3108 | continue; | |
3109 | if (type == STACK_MISC) | |
3110 | continue; | |
3111 | verbose(env, "invalid read from stack off %d+%d size %d\n", | |
3112 | off, i, size); | |
3113 | return -EACCES; | |
3114 | } | |
01f810ac | 3115 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 3116 | } |
354e8f19 | 3117 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 3118 | return 0; |
17a52670 | 3119 | } |
17a52670 | 3120 | |
01f810ac | 3121 | if (dst_regno >= 0) { |
17a52670 | 3122 | /* restore register state from stack */ |
01f810ac | 3123 | state->regs[dst_regno] = *reg; |
2f18f62e AS |
3124 | /* mark reg as written since spilled pointer state likely |
3125 | * has its liveness marks cleared by is_state_visited() | |
3126 | * which resets stack/reg liveness for state transitions | |
3127 | */ | |
01f810ac | 3128 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 3129 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 3130 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
3131 | * it is acceptable to use this value as a SCALAR_VALUE |
3132 | * (e.g. for XADD). | |
3133 | * We must not allow unprivileged callers to do that | |
3134 | * with spilled pointers. | |
3135 | */ | |
3136 | verbose(env, "leaking pointer from stack off %d\n", | |
3137 | off); | |
3138 | return -EACCES; | |
dc503a8a | 3139 | } |
f7cf25b2 | 3140 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
3141 | } else { |
3142 | for (i = 0; i < size; i++) { | |
01f810ac AM |
3143 | type = stype[(slot - i) % BPF_REG_SIZE]; |
3144 | if (type == STACK_MISC) | |
cc2b14d5 | 3145 | continue; |
01f810ac | 3146 | if (type == STACK_ZERO) |
cc2b14d5 | 3147 | continue; |
cc2b14d5 AS |
3148 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
3149 | off, i, size); | |
3150 | return -EACCES; | |
3151 | } | |
f7cf25b2 | 3152 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
3153 | if (dst_regno >= 0) |
3154 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 3155 | } |
f7cf25b2 | 3156 | return 0; |
17a52670 AS |
3157 | } |
3158 | ||
01f810ac AM |
3159 | enum stack_access_src { |
3160 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ | |
3161 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
3162 | }; | |
3163 | ||
3164 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
3165 | int regno, int off, int access_size, | |
3166 | bool zero_size_allowed, | |
3167 | enum stack_access_src type, | |
3168 | struct bpf_call_arg_meta *meta); | |
3169 | ||
3170 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
3171 | { | |
3172 | return cur_regs(env) + regno; | |
3173 | } | |
3174 | ||
3175 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
3176 | * 'dst_regno'. | |
3177 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
3178 | * but not its variable offset. | |
3179 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
3180 | * | |
3181 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
3182 | * filling registers (i.e. reads of spilled register cannot be detected when | |
3183 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
3184 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
3185 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
3186 | * instead. | |
3187 | */ | |
3188 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
3189 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 3190 | { |
01f810ac AM |
3191 | /* The state of the source register. */ |
3192 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3193 | struct bpf_func_state *ptr_state = func(env, reg); | |
3194 | int err; | |
3195 | int min_off, max_off; | |
3196 | ||
3197 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 3198 | */ |
01f810ac AM |
3199 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
3200 | false, ACCESS_DIRECT, NULL); | |
3201 | if (err) | |
3202 | return err; | |
3203 | ||
3204 | min_off = reg->smin_value + off; | |
3205 | max_off = reg->smax_value + off; | |
3206 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
3207 | return 0; | |
3208 | } | |
3209 | ||
3210 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
3211 | * check_stack_read_var_off. | |
3212 | * | |
3213 | * The caller must ensure that the offset falls within the allocated stack | |
3214 | * bounds. | |
3215 | * | |
3216 | * 'dst_regno' is a register which will receive the value from the stack. It | |
3217 | * can be -1, meaning that the read value is not going to a register. | |
3218 | */ | |
3219 | static int check_stack_read(struct bpf_verifier_env *env, | |
3220 | int ptr_regno, int off, int size, | |
3221 | int dst_regno) | |
3222 | { | |
3223 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3224 | struct bpf_func_state *state = func(env, reg); | |
3225 | int err; | |
3226 | /* Some accesses are only permitted with a static offset. */ | |
3227 | bool var_off = !tnum_is_const(reg->var_off); | |
3228 | ||
3229 | /* The offset is required to be static when reads don't go to a | |
3230 | * register, in order to not leak pointers (see | |
3231 | * check_stack_read_fixed_off). | |
3232 | */ | |
3233 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
3234 | char tn_buf[48]; |
3235 | ||
3236 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 3237 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
3238 | tn_buf, off, size); |
3239 | return -EACCES; | |
3240 | } | |
01f810ac AM |
3241 | /* Variable offset is prohibited for unprivileged mode for simplicity |
3242 | * since it requires corresponding support in Spectre masking for stack | |
3243 | * ALU. See also retrieve_ptr_limit(). | |
3244 | */ | |
3245 | if (!env->bypass_spec_v1 && var_off) { | |
3246 | char tn_buf[48]; | |
e4298d25 | 3247 | |
01f810ac AM |
3248 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3249 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", | |
3250 | ptr_regno, tn_buf); | |
e4298d25 DB |
3251 | return -EACCES; |
3252 | } | |
3253 | ||
01f810ac AM |
3254 | if (!var_off) { |
3255 | off += reg->var_off.value; | |
3256 | err = check_stack_read_fixed_off(env, state, off, size, | |
3257 | dst_regno); | |
3258 | } else { | |
3259 | /* Variable offset stack reads need more conservative handling | |
3260 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
3261 | * branch. | |
3262 | */ | |
3263 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
3264 | dst_regno); | |
3265 | } | |
3266 | return err; | |
3267 | } | |
3268 | ||
3269 | ||
3270 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
3271 | * check_stack_write_var_off. | |
3272 | * | |
3273 | * 'ptr_regno' is the register used as a pointer into the stack. | |
3274 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
3275 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
3276 | * be -1, meaning that we're not writing from a register. | |
3277 | * | |
3278 | * The caller must ensure that the offset falls within the maximum stack size. | |
3279 | */ | |
3280 | static int check_stack_write(struct bpf_verifier_env *env, | |
3281 | int ptr_regno, int off, int size, | |
3282 | int value_regno, int insn_idx) | |
3283 | { | |
3284 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3285 | struct bpf_func_state *state = func(env, reg); | |
3286 | int err; | |
3287 | ||
3288 | if (tnum_is_const(reg->var_off)) { | |
3289 | off += reg->var_off.value; | |
3290 | err = check_stack_write_fixed_off(env, state, off, size, | |
3291 | value_regno, insn_idx); | |
3292 | } else { | |
3293 | /* Variable offset stack reads need more conservative handling | |
3294 | * than fixed offset ones. | |
3295 | */ | |
3296 | err = check_stack_write_var_off(env, state, | |
3297 | ptr_regno, off, size, | |
3298 | value_regno, insn_idx); | |
3299 | } | |
3300 | return err; | |
e4298d25 DB |
3301 | } |
3302 | ||
591fe988 DB |
3303 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
3304 | int off, int size, enum bpf_access_type type) | |
3305 | { | |
3306 | struct bpf_reg_state *regs = cur_regs(env); | |
3307 | struct bpf_map *map = regs[regno].map_ptr; | |
3308 | u32 cap = bpf_map_flags_to_cap(map); | |
3309 | ||
3310 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
3311 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
3312 | map->value_size, off, size); | |
3313 | return -EACCES; | |
3314 | } | |
3315 | ||
3316 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
3317 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
3318 | map->value_size, off, size); | |
3319 | return -EACCES; | |
3320 | } | |
3321 | ||
3322 | return 0; | |
3323 | } | |
3324 | ||
457f4436 AN |
3325 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
3326 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
3327 | int off, int size, u32 mem_size, | |
3328 | bool zero_size_allowed) | |
17a52670 | 3329 | { |
457f4436 AN |
3330 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
3331 | struct bpf_reg_state *reg; | |
3332 | ||
3333 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
3334 | return 0; | |
17a52670 | 3335 | |
457f4436 AN |
3336 | reg = &cur_regs(env)[regno]; |
3337 | switch (reg->type) { | |
69c087ba YS |
3338 | case PTR_TO_MAP_KEY: |
3339 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
3340 | mem_size, off, size); | |
3341 | break; | |
457f4436 | 3342 | case PTR_TO_MAP_VALUE: |
61bd5218 | 3343 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
3344 | mem_size, off, size); |
3345 | break; | |
3346 | case PTR_TO_PACKET: | |
3347 | case PTR_TO_PACKET_META: | |
3348 | case PTR_TO_PACKET_END: | |
3349 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
3350 | off, size, regno, reg->id, off, mem_size); | |
3351 | break; | |
3352 | case PTR_TO_MEM: | |
3353 | default: | |
3354 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
3355 | mem_size, off, size); | |
17a52670 | 3356 | } |
457f4436 AN |
3357 | |
3358 | return -EACCES; | |
17a52670 AS |
3359 | } |
3360 | ||
457f4436 AN |
3361 | /* check read/write into a memory region with possible variable offset */ |
3362 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
3363 | int off, int size, u32 mem_size, | |
3364 | bool zero_size_allowed) | |
dbcfe5f7 | 3365 | { |
f4d7e40a AS |
3366 | struct bpf_verifier_state *vstate = env->cur_state; |
3367 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
3368 | struct bpf_reg_state *reg = &state->regs[regno]; |
3369 | int err; | |
3370 | ||
457f4436 | 3371 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
3372 | * need to try adding each of min_value and max_value to off |
3373 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 3374 | */ |
06ee7115 | 3375 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 3376 | print_verifier_state(env, state); |
b7137c4e | 3377 | |
dbcfe5f7 GB |
3378 | /* The minimum value is only important with signed |
3379 | * comparisons where we can't assume the floor of a | |
3380 | * value is 0. If we are using signed variables for our | |
3381 | * index'es we need to make sure that whatever we use | |
3382 | * will have a set floor within our range. | |
3383 | */ | |
b7137c4e DB |
3384 | if (reg->smin_value < 0 && |
3385 | (reg->smin_value == S64_MIN || | |
3386 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
3387 | reg->smin_value + off < 0)) { | |
61bd5218 | 3388 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
3389 | regno); |
3390 | return -EACCES; | |
3391 | } | |
457f4436 AN |
3392 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
3393 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 3394 | if (err) { |
457f4436 | 3395 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 3396 | regno); |
dbcfe5f7 GB |
3397 | return err; |
3398 | } | |
3399 | ||
b03c9f9f EC |
3400 | /* If we haven't set a max value then we need to bail since we can't be |
3401 | * sure we won't do bad things. | |
3402 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 3403 | */ |
b03c9f9f | 3404 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 3405 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
3406 | regno); |
3407 | return -EACCES; | |
3408 | } | |
457f4436 AN |
3409 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
3410 | mem_size, zero_size_allowed); | |
3411 | if (err) { | |
3412 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 3413 | regno); |
457f4436 AN |
3414 | return err; |
3415 | } | |
3416 | ||
3417 | return 0; | |
3418 | } | |
d83525ca | 3419 | |
457f4436 AN |
3420 | /* check read/write into a map element with possible variable offset */ |
3421 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
3422 | int off, int size, bool zero_size_allowed) | |
3423 | { | |
3424 | struct bpf_verifier_state *vstate = env->cur_state; | |
3425 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3426 | struct bpf_reg_state *reg = &state->regs[regno]; | |
3427 | struct bpf_map *map = reg->map_ptr; | |
3428 | int err; | |
3429 | ||
3430 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
3431 | zero_size_allowed); | |
3432 | if (err) | |
3433 | return err; | |
3434 | ||
3435 | if (map_value_has_spin_lock(map)) { | |
3436 | u32 lock = map->spin_lock_off; | |
d83525ca AS |
3437 | |
3438 | /* if any part of struct bpf_spin_lock can be touched by | |
3439 | * load/store reject this program. | |
3440 | * To check that [x1, x2) overlaps with [y1, y2) | |
3441 | * it is sufficient to check x1 < y2 && y1 < x2. | |
3442 | */ | |
3443 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
3444 | lock < reg->umax_value + off + size) { | |
3445 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
3446 | return -EACCES; | |
3447 | } | |
3448 | } | |
68134668 AS |
3449 | if (map_value_has_timer(map)) { |
3450 | u32 t = map->timer_off; | |
3451 | ||
3452 | if (reg->smin_value + off < t + sizeof(struct bpf_timer) && | |
3453 | t < reg->umax_value + off + size) { | |
3454 | verbose(env, "bpf_timer cannot be accessed directly by load/store\n"); | |
3455 | return -EACCES; | |
3456 | } | |
3457 | } | |
f1174f77 | 3458 | return err; |
dbcfe5f7 GB |
3459 | } |
3460 | ||
969bf05e AS |
3461 | #define MAX_PACKET_OFF 0xffff |
3462 | ||
7e40781c UP |
3463 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
3464 | { | |
3aac1ead | 3465 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
7e40781c UP |
3466 | } |
3467 | ||
58e2af8b | 3468 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
3469 | const struct bpf_call_arg_meta *meta, |
3470 | enum bpf_access_type t) | |
4acf6c0b | 3471 | { |
7e40781c UP |
3472 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
3473 | ||
3474 | switch (prog_type) { | |
5d66fa7d | 3475 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
3476 | case BPF_PROG_TYPE_LWT_IN: |
3477 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 3478 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 3479 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 3480 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 3481 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
3482 | if (t == BPF_WRITE) |
3483 | return false; | |
8731745e | 3484 | fallthrough; |
5d66fa7d DB |
3485 | |
3486 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
3487 | case BPF_PROG_TYPE_SCHED_CLS: |
3488 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 3489 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 3490 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 3491 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 3492 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
3493 | if (meta) |
3494 | return meta->pkt_access; | |
3495 | ||
3496 | env->seen_direct_write = true; | |
4acf6c0b | 3497 | return true; |
0d01da6a SF |
3498 | |
3499 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
3500 | if (t == BPF_WRITE) | |
3501 | env->seen_direct_write = true; | |
3502 | ||
3503 | return true; | |
3504 | ||
4acf6c0b BB |
3505 | default: |
3506 | return false; | |
3507 | } | |
3508 | } | |
3509 | ||
f1174f77 | 3510 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 3511 | int size, bool zero_size_allowed) |
f1174f77 | 3512 | { |
638f5b90 | 3513 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
3514 | struct bpf_reg_state *reg = ®s[regno]; |
3515 | int err; | |
3516 | ||
3517 | /* We may have added a variable offset to the packet pointer; but any | |
3518 | * reg->range we have comes after that. We are only checking the fixed | |
3519 | * offset. | |
3520 | */ | |
3521 | ||
3522 | /* We don't allow negative numbers, because we aren't tracking enough | |
3523 | * detail to prove they're safe. | |
3524 | */ | |
b03c9f9f | 3525 | if (reg->smin_value < 0) { |
61bd5218 | 3526 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
3527 | regno); |
3528 | return -EACCES; | |
3529 | } | |
6d94e741 AS |
3530 | |
3531 | err = reg->range < 0 ? -EINVAL : | |
3532 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 3533 | zero_size_allowed); |
f1174f77 | 3534 | if (err) { |
61bd5218 | 3535 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
3536 | return err; |
3537 | } | |
e647815a | 3538 | |
457f4436 | 3539 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
3540 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
3541 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 3542 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
3543 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
3544 | */ | |
3545 | env->prog->aux->max_pkt_offset = | |
3546 | max_t(u32, env->prog->aux->max_pkt_offset, | |
3547 | off + reg->umax_value + size - 1); | |
3548 | ||
f1174f77 EC |
3549 | return err; |
3550 | } | |
3551 | ||
3552 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 3553 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 3554 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 3555 | struct btf **btf, u32 *btf_id) |
17a52670 | 3556 | { |
f96da094 DB |
3557 | struct bpf_insn_access_aux info = { |
3558 | .reg_type = *reg_type, | |
9e15db66 | 3559 | .log = &env->log, |
f96da094 | 3560 | }; |
31fd8581 | 3561 | |
4f9218aa | 3562 | if (env->ops->is_valid_access && |
5e43f899 | 3563 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
3564 | /* A non zero info.ctx_field_size indicates that this field is a |
3565 | * candidate for later verifier transformation to load the whole | |
3566 | * field and then apply a mask when accessed with a narrower | |
3567 | * access than actual ctx access size. A zero info.ctx_field_size | |
3568 | * will only allow for whole field access and rejects any other | |
3569 | * type of narrower access. | |
31fd8581 | 3570 | */ |
23994631 | 3571 | *reg_type = info.reg_type; |
31fd8581 | 3572 | |
22dc4a0f AN |
3573 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) { |
3574 | *btf = info.btf; | |
9e15db66 | 3575 | *btf_id = info.btf_id; |
22dc4a0f | 3576 | } else { |
9e15db66 | 3577 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 3578 | } |
32bbe007 AS |
3579 | /* remember the offset of last byte accessed in ctx */ |
3580 | if (env->prog->aux->max_ctx_offset < off + size) | |
3581 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 3582 | return 0; |
32bbe007 | 3583 | } |
17a52670 | 3584 | |
61bd5218 | 3585 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
3586 | return -EACCES; |
3587 | } | |
3588 | ||
d58e468b PP |
3589 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
3590 | int size) | |
3591 | { | |
3592 | if (size < 0 || off < 0 || | |
3593 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
3594 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
3595 | off, size); | |
3596 | return -EACCES; | |
3597 | } | |
3598 | return 0; | |
3599 | } | |
3600 | ||
5f456649 MKL |
3601 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
3602 | u32 regno, int off, int size, | |
3603 | enum bpf_access_type t) | |
c64b7983 JS |
3604 | { |
3605 | struct bpf_reg_state *regs = cur_regs(env); | |
3606 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 3607 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 3608 | bool valid; |
c64b7983 JS |
3609 | |
3610 | if (reg->smin_value < 0) { | |
3611 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
3612 | regno); | |
3613 | return -EACCES; | |
3614 | } | |
3615 | ||
46f8bc92 MKL |
3616 | switch (reg->type) { |
3617 | case PTR_TO_SOCK_COMMON: | |
3618 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
3619 | break; | |
3620 | case PTR_TO_SOCKET: | |
3621 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
3622 | break; | |
655a51e5 MKL |
3623 | case PTR_TO_TCP_SOCK: |
3624 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
3625 | break; | |
fada7fdc JL |
3626 | case PTR_TO_XDP_SOCK: |
3627 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
3628 | break; | |
46f8bc92 MKL |
3629 | default: |
3630 | valid = false; | |
c64b7983 JS |
3631 | } |
3632 | ||
5f456649 | 3633 | |
46f8bc92 MKL |
3634 | if (valid) { |
3635 | env->insn_aux_data[insn_idx].ctx_field_size = | |
3636 | info.ctx_field_size; | |
3637 | return 0; | |
3638 | } | |
3639 | ||
3640 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
3641 | regno, reg_type_str[reg->type], off, size); | |
3642 | ||
3643 | return -EACCES; | |
c64b7983 JS |
3644 | } |
3645 | ||
4cabc5b1 DB |
3646 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
3647 | { | |
2a159c6f | 3648 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
3649 | } |
3650 | ||
f37a8cb8 DB |
3651 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
3652 | { | |
2a159c6f | 3653 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 3654 | |
46f8bc92 MKL |
3655 | return reg->type == PTR_TO_CTX; |
3656 | } | |
3657 | ||
3658 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
3659 | { | |
3660 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
3661 | ||
3662 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
3663 | } |
3664 | ||
ca369602 DB |
3665 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
3666 | { | |
2a159c6f | 3667 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
3668 | |
3669 | return type_is_pkt_pointer(reg->type); | |
3670 | } | |
3671 | ||
4b5defde DB |
3672 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
3673 | { | |
3674 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
3675 | ||
3676 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
3677 | return reg->type == PTR_TO_FLOW_KEYS; | |
3678 | } | |
3679 | ||
61bd5218 JK |
3680 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
3681 | const struct bpf_reg_state *reg, | |
d1174416 | 3682 | int off, int size, bool strict) |
969bf05e | 3683 | { |
f1174f77 | 3684 | struct tnum reg_off; |
e07b98d9 | 3685 | int ip_align; |
d1174416 DM |
3686 | |
3687 | /* Byte size accesses are always allowed. */ | |
3688 | if (!strict || size == 1) | |
3689 | return 0; | |
3690 | ||
e4eda884 DM |
3691 | /* For platforms that do not have a Kconfig enabling |
3692 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
3693 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
3694 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
3695 | * to this code only in strict mode where we want to emulate | |
3696 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
3697 | * unconditional IP align value of '2'. | |
e07b98d9 | 3698 | */ |
e4eda884 | 3699 | ip_align = 2; |
f1174f77 EC |
3700 | |
3701 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
3702 | if (!tnum_is_aligned(reg_off, size)) { | |
3703 | char tn_buf[48]; | |
3704 | ||
3705 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
3706 | verbose(env, |
3707 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 3708 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
3709 | return -EACCES; |
3710 | } | |
79adffcd | 3711 | |
969bf05e AS |
3712 | return 0; |
3713 | } | |
3714 | ||
61bd5218 JK |
3715 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
3716 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
3717 | const char *pointer_desc, |
3718 | int off, int size, bool strict) | |
79adffcd | 3719 | { |
f1174f77 EC |
3720 | struct tnum reg_off; |
3721 | ||
3722 | /* Byte size accesses are always allowed. */ | |
3723 | if (!strict || size == 1) | |
3724 | return 0; | |
3725 | ||
3726 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
3727 | if (!tnum_is_aligned(reg_off, size)) { | |
3728 | char tn_buf[48]; | |
3729 | ||
3730 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 3731 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 3732 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
3733 | return -EACCES; |
3734 | } | |
3735 | ||
969bf05e AS |
3736 | return 0; |
3737 | } | |
3738 | ||
e07b98d9 | 3739 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
3740 | const struct bpf_reg_state *reg, int off, |
3741 | int size, bool strict_alignment_once) | |
79adffcd | 3742 | { |
ca369602 | 3743 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 3744 | const char *pointer_desc = ""; |
d1174416 | 3745 | |
79adffcd DB |
3746 | switch (reg->type) { |
3747 | case PTR_TO_PACKET: | |
de8f3a83 DB |
3748 | case PTR_TO_PACKET_META: |
3749 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
3750 | * right in front, treat it the very same way. | |
3751 | */ | |
61bd5218 | 3752 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
3753 | case PTR_TO_FLOW_KEYS: |
3754 | pointer_desc = "flow keys "; | |
3755 | break; | |
69c087ba YS |
3756 | case PTR_TO_MAP_KEY: |
3757 | pointer_desc = "key "; | |
3758 | break; | |
f1174f77 EC |
3759 | case PTR_TO_MAP_VALUE: |
3760 | pointer_desc = "value "; | |
3761 | break; | |
3762 | case PTR_TO_CTX: | |
3763 | pointer_desc = "context "; | |
3764 | break; | |
3765 | case PTR_TO_STACK: | |
3766 | pointer_desc = "stack "; | |
01f810ac AM |
3767 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
3768 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
3769 | * aligned. |
3770 | */ | |
3771 | strict = true; | |
f1174f77 | 3772 | break; |
c64b7983 JS |
3773 | case PTR_TO_SOCKET: |
3774 | pointer_desc = "sock "; | |
3775 | break; | |
46f8bc92 MKL |
3776 | case PTR_TO_SOCK_COMMON: |
3777 | pointer_desc = "sock_common "; | |
3778 | break; | |
655a51e5 MKL |
3779 | case PTR_TO_TCP_SOCK: |
3780 | pointer_desc = "tcp_sock "; | |
3781 | break; | |
fada7fdc JL |
3782 | case PTR_TO_XDP_SOCK: |
3783 | pointer_desc = "xdp_sock "; | |
3784 | break; | |
79adffcd | 3785 | default: |
f1174f77 | 3786 | break; |
79adffcd | 3787 | } |
61bd5218 JK |
3788 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
3789 | strict); | |
79adffcd DB |
3790 | } |
3791 | ||
f4d7e40a AS |
3792 | static int update_stack_depth(struct bpf_verifier_env *env, |
3793 | const struct bpf_func_state *func, | |
3794 | int off) | |
3795 | { | |
9c8105bd | 3796 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
3797 | |
3798 | if (stack >= -off) | |
3799 | return 0; | |
3800 | ||
3801 | /* update known max for given subprogram */ | |
9c8105bd | 3802 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
3803 | return 0; |
3804 | } | |
f4d7e40a | 3805 | |
70a87ffe AS |
3806 | /* starting from main bpf function walk all instructions of the function |
3807 | * and recursively walk all callees that given function can call. | |
3808 | * Ignore jump and exit insns. | |
3809 | * Since recursion is prevented by check_cfg() this algorithm | |
3810 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
3811 | */ | |
3812 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
3813 | { | |
9c8105bd JW |
3814 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
3815 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 3816 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 3817 | bool tail_call_reachable = false; |
70a87ffe AS |
3818 | int ret_insn[MAX_CALL_FRAMES]; |
3819 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 3820 | int j; |
f4d7e40a | 3821 | |
70a87ffe | 3822 | process_func: |
7f6e4312 MF |
3823 | /* protect against potential stack overflow that might happen when |
3824 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
3825 | * depth for such case down to 256 so that the worst case scenario | |
3826 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
3827 | * 8k). | |
3828 | * | |
3829 | * To get the idea what might happen, see an example: | |
3830 | * func1 -> sub rsp, 128 | |
3831 | * subfunc1 -> sub rsp, 256 | |
3832 | * tailcall1 -> add rsp, 256 | |
3833 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
3834 | * subfunc2 -> sub rsp, 64 | |
3835 | * subfunc22 -> sub rsp, 128 | |
3836 | * tailcall2 -> add rsp, 128 | |
3837 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
3838 | * | |
3839 | * tailcall will unwind the current stack frame but it will not get rid | |
3840 | * of caller's stack as shown on the example above. | |
3841 | */ | |
3842 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
3843 | verbose(env, | |
3844 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
3845 | depth); | |
3846 | return -EACCES; | |
3847 | } | |
70a87ffe AS |
3848 | /* round up to 32-bytes, since this is granularity |
3849 | * of interpreter stack size | |
3850 | */ | |
9c8105bd | 3851 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 3852 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 3853 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 3854 | frame + 1, depth); |
f4d7e40a AS |
3855 | return -EACCES; |
3856 | } | |
70a87ffe | 3857 | continue_func: |
4cb3d99c | 3858 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 3859 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
3860 | int next_insn; |
3861 | ||
69c087ba | 3862 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
3863 | continue; |
3864 | /* remember insn and function to return to */ | |
3865 | ret_insn[frame] = i + 1; | |
9c8105bd | 3866 | ret_prog[frame] = idx; |
70a87ffe AS |
3867 | |
3868 | /* find the callee */ | |
7ddc80a4 AS |
3869 | next_insn = i + insn[i].imm + 1; |
3870 | idx = find_subprog(env, next_insn); | |
9c8105bd | 3871 | if (idx < 0) { |
70a87ffe | 3872 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 3873 | next_insn); |
70a87ffe AS |
3874 | return -EFAULT; |
3875 | } | |
7ddc80a4 AS |
3876 | if (subprog[idx].is_async_cb) { |
3877 | if (subprog[idx].has_tail_call) { | |
3878 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
3879 | return -EFAULT; | |
3880 | } | |
3881 | /* async callbacks don't increase bpf prog stack size */ | |
3882 | continue; | |
3883 | } | |
3884 | i = next_insn; | |
ebf7d1f5 MF |
3885 | |
3886 | if (subprog[idx].has_tail_call) | |
3887 | tail_call_reachable = true; | |
3888 | ||
70a87ffe AS |
3889 | frame++; |
3890 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
3891 | verbose(env, "the call stack of %d frames is too deep !\n", |
3892 | frame); | |
3893 | return -E2BIG; | |
70a87ffe AS |
3894 | } |
3895 | goto process_func; | |
3896 | } | |
ebf7d1f5 MF |
3897 | /* if tail call got detected across bpf2bpf calls then mark each of the |
3898 | * currently present subprog frames as tail call reachable subprogs; | |
3899 | * this info will be utilized by JIT so that we will be preserving the | |
3900 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
3901 | */ | |
3902 | if (tail_call_reachable) | |
3903 | for (j = 0; j < frame; j++) | |
3904 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
3905 | if (subprog[0].tail_call_reachable) |
3906 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 3907 | |
70a87ffe AS |
3908 | /* end of for() loop means the last insn of the 'subprog' |
3909 | * was reached. Doesn't matter whether it was JA or EXIT | |
3910 | */ | |
3911 | if (frame == 0) | |
3912 | return 0; | |
9c8105bd | 3913 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
3914 | frame--; |
3915 | i = ret_insn[frame]; | |
9c8105bd | 3916 | idx = ret_prog[frame]; |
70a87ffe | 3917 | goto continue_func; |
f4d7e40a AS |
3918 | } |
3919 | ||
19d28fbd | 3920 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
3921 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
3922 | const struct bpf_insn *insn, int idx) | |
3923 | { | |
3924 | int start = idx + insn->imm + 1, subprog; | |
3925 | ||
3926 | subprog = find_subprog(env, start); | |
3927 | if (subprog < 0) { | |
3928 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
3929 | start); | |
3930 | return -EFAULT; | |
3931 | } | |
9c8105bd | 3932 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 3933 | } |
19d28fbd | 3934 | #endif |
1ea47e01 | 3935 | |
51c39bb1 AS |
3936 | int check_ctx_reg(struct bpf_verifier_env *env, |
3937 | const struct bpf_reg_state *reg, int regno) | |
58990d1f DB |
3938 | { |
3939 | /* Access to ctx or passing it to a helper is only allowed in | |
3940 | * its original, unmodified form. | |
3941 | */ | |
3942 | ||
3943 | if (reg->off) { | |
3944 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
3945 | regno, reg->off); | |
3946 | return -EACCES; | |
3947 | } | |
3948 | ||
3949 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3950 | char tn_buf[48]; | |
3951 | ||
3952 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3953 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
3954 | return -EACCES; | |
3955 | } | |
3956 | ||
3957 | return 0; | |
3958 | } | |
3959 | ||
afbf21dc YS |
3960 | static int __check_buffer_access(struct bpf_verifier_env *env, |
3961 | const char *buf_info, | |
3962 | const struct bpf_reg_state *reg, | |
3963 | int regno, int off, int size) | |
9df1c28b MM |
3964 | { |
3965 | if (off < 0) { | |
3966 | verbose(env, | |
4fc00b79 | 3967 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 3968 | regno, buf_info, off, size); |
9df1c28b MM |
3969 | return -EACCES; |
3970 | } | |
3971 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3972 | char tn_buf[48]; | |
3973 | ||
3974 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3975 | verbose(env, | |
4fc00b79 | 3976 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
3977 | regno, off, tn_buf); |
3978 | return -EACCES; | |
3979 | } | |
afbf21dc YS |
3980 | |
3981 | return 0; | |
3982 | } | |
3983 | ||
3984 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
3985 | const struct bpf_reg_state *reg, | |
3986 | int regno, int off, int size) | |
3987 | { | |
3988 | int err; | |
3989 | ||
3990 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
3991 | if (err) | |
3992 | return err; | |
3993 | ||
9df1c28b MM |
3994 | if (off + size > env->prog->aux->max_tp_access) |
3995 | env->prog->aux->max_tp_access = off + size; | |
3996 | ||
3997 | return 0; | |
3998 | } | |
3999 | ||
afbf21dc YS |
4000 | static int check_buffer_access(struct bpf_verifier_env *env, |
4001 | const struct bpf_reg_state *reg, | |
4002 | int regno, int off, int size, | |
4003 | bool zero_size_allowed, | |
4004 | const char *buf_info, | |
4005 | u32 *max_access) | |
4006 | { | |
4007 | int err; | |
4008 | ||
4009 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
4010 | if (err) | |
4011 | return err; | |
4012 | ||
4013 | if (off + size > *max_access) | |
4014 | *max_access = off + size; | |
4015 | ||
4016 | return 0; | |
4017 | } | |
4018 | ||
3f50f132 JF |
4019 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
4020 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
4021 | { | |
4022 | reg->var_off = tnum_subreg(reg->var_off); | |
4023 | __reg_assign_32_into_64(reg); | |
4024 | } | |
9df1c28b | 4025 | |
0c17d1d2 JH |
4026 | /* truncate register to smaller size (in bytes) |
4027 | * must be called with size < BPF_REG_SIZE | |
4028 | */ | |
4029 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
4030 | { | |
4031 | u64 mask; | |
4032 | ||
4033 | /* clear high bits in bit representation */ | |
4034 | reg->var_off = tnum_cast(reg->var_off, size); | |
4035 | ||
4036 | /* fix arithmetic bounds */ | |
4037 | mask = ((u64)1 << (size * 8)) - 1; | |
4038 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
4039 | reg->umin_value &= mask; | |
4040 | reg->umax_value &= mask; | |
4041 | } else { | |
4042 | reg->umin_value = 0; | |
4043 | reg->umax_value = mask; | |
4044 | } | |
4045 | reg->smin_value = reg->umin_value; | |
4046 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
4047 | |
4048 | /* If size is smaller than 32bit register the 32bit register | |
4049 | * values are also truncated so we push 64-bit bounds into | |
4050 | * 32-bit bounds. Above were truncated < 32-bits already. | |
4051 | */ | |
4052 | if (size >= 4) | |
4053 | return; | |
4054 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
4055 | } |
4056 | ||
a23740ec AN |
4057 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
4058 | { | |
4059 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; | |
4060 | } | |
4061 | ||
4062 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
4063 | { | |
4064 | void *ptr; | |
4065 | u64 addr; | |
4066 | int err; | |
4067 | ||
4068 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
4069 | if (err) | |
4070 | return err; | |
2dedd7d2 | 4071 | ptr = (void *)(long)addr + off; |
a23740ec AN |
4072 | |
4073 | switch (size) { | |
4074 | case sizeof(u8): | |
4075 | *val = (u64)*(u8 *)ptr; | |
4076 | break; | |
4077 | case sizeof(u16): | |
4078 | *val = (u64)*(u16 *)ptr; | |
4079 | break; | |
4080 | case sizeof(u32): | |
4081 | *val = (u64)*(u32 *)ptr; | |
4082 | break; | |
4083 | case sizeof(u64): | |
4084 | *val = *(u64 *)ptr; | |
4085 | break; | |
4086 | default: | |
4087 | return -EINVAL; | |
4088 | } | |
4089 | return 0; | |
4090 | } | |
4091 | ||
9e15db66 AS |
4092 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
4093 | struct bpf_reg_state *regs, | |
4094 | int regno, int off, int size, | |
4095 | enum bpf_access_type atype, | |
4096 | int value_regno) | |
4097 | { | |
4098 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
4099 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
4100 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
9e15db66 AS |
4101 | u32 btf_id; |
4102 | int ret; | |
4103 | ||
9e15db66 AS |
4104 | if (off < 0) { |
4105 | verbose(env, | |
4106 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
4107 | regno, tname, off); | |
4108 | return -EACCES; | |
4109 | } | |
4110 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4111 | char tn_buf[48]; | |
4112 | ||
4113 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4114 | verbose(env, | |
4115 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
4116 | regno, tname, off, tn_buf); | |
4117 | return -EACCES; | |
4118 | } | |
4119 | ||
27ae7997 | 4120 | if (env->ops->btf_struct_access) { |
22dc4a0f AN |
4121 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
4122 | off, size, atype, &btf_id); | |
27ae7997 MKL |
4123 | } else { |
4124 | if (atype != BPF_READ) { | |
4125 | verbose(env, "only read is supported\n"); | |
4126 | return -EACCES; | |
4127 | } | |
4128 | ||
22dc4a0f AN |
4129 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
4130 | atype, &btf_id); | |
27ae7997 MKL |
4131 | } |
4132 | ||
9e15db66 AS |
4133 | if (ret < 0) |
4134 | return ret; | |
4135 | ||
41c48f3a | 4136 | if (atype == BPF_READ && value_regno >= 0) |
22dc4a0f | 4137 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id); |
41c48f3a AI |
4138 | |
4139 | return 0; | |
4140 | } | |
4141 | ||
4142 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
4143 | struct bpf_reg_state *regs, | |
4144 | int regno, int off, int size, | |
4145 | enum bpf_access_type atype, | |
4146 | int value_regno) | |
4147 | { | |
4148 | struct bpf_reg_state *reg = regs + regno; | |
4149 | struct bpf_map *map = reg->map_ptr; | |
4150 | const struct btf_type *t; | |
4151 | const char *tname; | |
4152 | u32 btf_id; | |
4153 | int ret; | |
4154 | ||
4155 | if (!btf_vmlinux) { | |
4156 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
4157 | return -ENOTSUPP; | |
4158 | } | |
4159 | ||
4160 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
4161 | verbose(env, "map_ptr access not supported for map type %d\n", | |
4162 | map->map_type); | |
4163 | return -ENOTSUPP; | |
4164 | } | |
4165 | ||
4166 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
4167 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
4168 | ||
4169 | if (!env->allow_ptr_to_map_access) { | |
4170 | verbose(env, | |
4171 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
4172 | tname); | |
4173 | return -EPERM; | |
9e15db66 | 4174 | } |
27ae7997 | 4175 | |
41c48f3a AI |
4176 | if (off < 0) { |
4177 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
4178 | regno, tname, off); | |
4179 | return -EACCES; | |
4180 | } | |
4181 | ||
4182 | if (atype != BPF_READ) { | |
4183 | verbose(env, "only read from %s is supported\n", tname); | |
4184 | return -EACCES; | |
4185 | } | |
4186 | ||
22dc4a0f | 4187 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id); |
41c48f3a AI |
4188 | if (ret < 0) |
4189 | return ret; | |
4190 | ||
4191 | if (value_regno >= 0) | |
22dc4a0f | 4192 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id); |
41c48f3a | 4193 | |
9e15db66 AS |
4194 | return 0; |
4195 | } | |
4196 | ||
01f810ac AM |
4197 | /* Check that the stack access at the given offset is within bounds. The |
4198 | * maximum valid offset is -1. | |
4199 | * | |
4200 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
4201 | * -state->allocated_stack for reads. | |
4202 | */ | |
4203 | static int check_stack_slot_within_bounds(int off, | |
4204 | struct bpf_func_state *state, | |
4205 | enum bpf_access_type t) | |
4206 | { | |
4207 | int min_valid_off; | |
4208 | ||
4209 | if (t == BPF_WRITE) | |
4210 | min_valid_off = -MAX_BPF_STACK; | |
4211 | else | |
4212 | min_valid_off = -state->allocated_stack; | |
4213 | ||
4214 | if (off < min_valid_off || off > -1) | |
4215 | return -EACCES; | |
4216 | return 0; | |
4217 | } | |
4218 | ||
4219 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
4220 | * bounds. | |
4221 | * | |
4222 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
4223 | */ | |
4224 | static int check_stack_access_within_bounds( | |
4225 | struct bpf_verifier_env *env, | |
4226 | int regno, int off, int access_size, | |
4227 | enum stack_access_src src, enum bpf_access_type type) | |
4228 | { | |
4229 | struct bpf_reg_state *regs = cur_regs(env); | |
4230 | struct bpf_reg_state *reg = regs + regno; | |
4231 | struct bpf_func_state *state = func(env, reg); | |
4232 | int min_off, max_off; | |
4233 | int err; | |
4234 | char *err_extra; | |
4235 | ||
4236 | if (src == ACCESS_HELPER) | |
4237 | /* We don't know if helpers are reading or writing (or both). */ | |
4238 | err_extra = " indirect access to"; | |
4239 | else if (type == BPF_READ) | |
4240 | err_extra = " read from"; | |
4241 | else | |
4242 | err_extra = " write to"; | |
4243 | ||
4244 | if (tnum_is_const(reg->var_off)) { | |
4245 | min_off = reg->var_off.value + off; | |
4246 | if (access_size > 0) | |
4247 | max_off = min_off + access_size - 1; | |
4248 | else | |
4249 | max_off = min_off; | |
4250 | } else { | |
4251 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
4252 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
4253 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
4254 | err_extra, regno); | |
4255 | return -EACCES; | |
4256 | } | |
4257 | min_off = reg->smin_value + off; | |
4258 | if (access_size > 0) | |
4259 | max_off = reg->smax_value + off + access_size - 1; | |
4260 | else | |
4261 | max_off = min_off; | |
4262 | } | |
4263 | ||
4264 | err = check_stack_slot_within_bounds(min_off, state, type); | |
4265 | if (!err) | |
4266 | err = check_stack_slot_within_bounds(max_off, state, type); | |
4267 | ||
4268 | if (err) { | |
4269 | if (tnum_is_const(reg->var_off)) { | |
4270 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
4271 | err_extra, regno, off, access_size); | |
4272 | } else { | |
4273 | char tn_buf[48]; | |
4274 | ||
4275 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4276 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
4277 | err_extra, regno, tn_buf, access_size); | |
4278 | } | |
4279 | } | |
4280 | return err; | |
4281 | } | |
41c48f3a | 4282 | |
17a52670 AS |
4283 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
4284 | * if t==write, value_regno is a register which value is stored into memory | |
4285 | * if t==read, value_regno is a register which will receive the value from memory | |
4286 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
4287 | * if t==read && value_regno==-1, don't care what we read from memory | |
4288 | */ | |
ca369602 DB |
4289 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
4290 | int off, int bpf_size, enum bpf_access_type t, | |
4291 | int value_regno, bool strict_alignment_once) | |
17a52670 | 4292 | { |
638f5b90 AS |
4293 | struct bpf_reg_state *regs = cur_regs(env); |
4294 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 4295 | struct bpf_func_state *state; |
17a52670 AS |
4296 | int size, err = 0; |
4297 | ||
4298 | size = bpf_size_to_bytes(bpf_size); | |
4299 | if (size < 0) | |
4300 | return size; | |
4301 | ||
f1174f77 | 4302 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 4303 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
4304 | if (err) |
4305 | return err; | |
17a52670 | 4306 | |
f1174f77 EC |
4307 | /* for access checks, reg->off is just part of off */ |
4308 | off += reg->off; | |
4309 | ||
69c087ba YS |
4310 | if (reg->type == PTR_TO_MAP_KEY) { |
4311 | if (t == BPF_WRITE) { | |
4312 | verbose(env, "write to change key R%d not allowed\n", regno); | |
4313 | return -EACCES; | |
4314 | } | |
4315 | ||
4316 | err = check_mem_region_access(env, regno, off, size, | |
4317 | reg->map_ptr->key_size, false); | |
4318 | if (err) | |
4319 | return err; | |
4320 | if (value_regno >= 0) | |
4321 | mark_reg_unknown(env, regs, value_regno); | |
4322 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
4323 | if (t == BPF_WRITE && value_regno >= 0 && |
4324 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4325 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
4326 | return -EACCES; |
4327 | } | |
591fe988 DB |
4328 | err = check_map_access_type(env, regno, off, size, t); |
4329 | if (err) | |
4330 | return err; | |
9fd29c08 | 4331 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
4332 | if (!err && t == BPF_READ && value_regno >= 0) { |
4333 | struct bpf_map *map = reg->map_ptr; | |
4334 | ||
4335 | /* if map is read-only, track its contents as scalars */ | |
4336 | if (tnum_is_const(reg->var_off) && | |
4337 | bpf_map_is_rdonly(map) && | |
4338 | map->ops->map_direct_value_addr) { | |
4339 | int map_off = off + reg->var_off.value; | |
4340 | u64 val = 0; | |
4341 | ||
4342 | err = bpf_map_direct_read(map, map_off, size, | |
4343 | &val); | |
4344 | if (err) | |
4345 | return err; | |
4346 | ||
4347 | regs[value_regno].type = SCALAR_VALUE; | |
4348 | __mark_reg_known(®s[value_regno], val); | |
4349 | } else { | |
4350 | mark_reg_unknown(env, regs, value_regno); | |
4351 | } | |
4352 | } | |
457f4436 AN |
4353 | } else if (reg->type == PTR_TO_MEM) { |
4354 | if (t == BPF_WRITE && value_regno >= 0 && | |
4355 | is_pointer_value(env, value_regno)) { | |
4356 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
4357 | return -EACCES; | |
4358 | } | |
4359 | err = check_mem_region_access(env, regno, off, size, | |
4360 | reg->mem_size, false); | |
4361 | if (!err && t == BPF_READ && value_regno >= 0) | |
4362 | mark_reg_unknown(env, regs, value_regno); | |
1a0dc1ac | 4363 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 4364 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 4365 | struct btf *btf = NULL; |
9e15db66 | 4366 | u32 btf_id = 0; |
19de99f7 | 4367 | |
1be7f75d AS |
4368 | if (t == BPF_WRITE && value_regno >= 0 && |
4369 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4370 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
4371 | return -EACCES; |
4372 | } | |
f1174f77 | 4373 | |
58990d1f DB |
4374 | err = check_ctx_reg(env, reg, regno); |
4375 | if (err < 0) | |
4376 | return err; | |
4377 | ||
22dc4a0f | 4378 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id); |
9e15db66 AS |
4379 | if (err) |
4380 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 4381 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 4382 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
4383 | * PTR_TO_PACKET[_META,_END]. In the latter |
4384 | * case, we know the offset is zero. | |
f1174f77 | 4385 | */ |
46f8bc92 | 4386 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 4387 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 4388 | } else { |
638f5b90 | 4389 | mark_reg_known_zero(env, regs, |
61bd5218 | 4390 | value_regno); |
46f8bc92 MKL |
4391 | if (reg_type_may_be_null(reg_type)) |
4392 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
4393 | /* A load of ctx field could have different |
4394 | * actual load size with the one encoded in the | |
4395 | * insn. When the dst is PTR, it is for sure not | |
4396 | * a sub-register. | |
4397 | */ | |
4398 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
b121b341 | 4399 | if (reg_type == PTR_TO_BTF_ID || |
22dc4a0f AN |
4400 | reg_type == PTR_TO_BTF_ID_OR_NULL) { |
4401 | regs[value_regno].btf = btf; | |
9e15db66 | 4402 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 4403 | } |
46f8bc92 | 4404 | } |
638f5b90 | 4405 | regs[value_regno].type = reg_type; |
969bf05e | 4406 | } |
17a52670 | 4407 | |
f1174f77 | 4408 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
4409 | /* Basic bounds checks. */ |
4410 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
4411 | if (err) |
4412 | return err; | |
8726679a | 4413 | |
f4d7e40a AS |
4414 | state = func(env, reg); |
4415 | err = update_stack_depth(env, state, off); | |
4416 | if (err) | |
4417 | return err; | |
8726679a | 4418 | |
01f810ac AM |
4419 | if (t == BPF_READ) |
4420 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 4421 | value_regno); |
01f810ac AM |
4422 | else |
4423 | err = check_stack_write(env, regno, off, size, | |
4424 | value_regno, insn_idx); | |
de8f3a83 | 4425 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 4426 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 4427 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
4428 | return -EACCES; |
4429 | } | |
4acf6c0b BB |
4430 | if (t == BPF_WRITE && value_regno >= 0 && |
4431 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
4432 | verbose(env, "R%d leaks addr into packet\n", |
4433 | value_regno); | |
4acf6c0b BB |
4434 | return -EACCES; |
4435 | } | |
9fd29c08 | 4436 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 4437 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 4438 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
4439 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
4440 | if (t == BPF_WRITE && value_regno >= 0 && | |
4441 | is_pointer_value(env, value_regno)) { | |
4442 | verbose(env, "R%d leaks addr into flow keys\n", | |
4443 | value_regno); | |
4444 | return -EACCES; | |
4445 | } | |
4446 | ||
4447 | err = check_flow_keys_access(env, off, size); | |
4448 | if (!err && t == BPF_READ && value_regno >= 0) | |
4449 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 4450 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 4451 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
4452 | verbose(env, "R%d cannot write into %s\n", |
4453 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
4454 | return -EACCES; |
4455 | } | |
5f456649 | 4456 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
4457 | if (!err && value_regno >= 0) |
4458 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
4459 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
4460 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
4461 | if (!err && t == BPF_READ && value_regno >= 0) | |
4462 | mark_reg_unknown(env, regs, value_regno); | |
9e15db66 AS |
4463 | } else if (reg->type == PTR_TO_BTF_ID) { |
4464 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, | |
4465 | value_regno); | |
41c48f3a AI |
4466 | } else if (reg->type == CONST_PTR_TO_MAP) { |
4467 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
4468 | value_regno); | |
afbf21dc YS |
4469 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
4470 | if (t == BPF_WRITE) { | |
4471 | verbose(env, "R%d cannot write into %s\n", | |
4472 | regno, reg_type_str[reg->type]); | |
4473 | return -EACCES; | |
4474 | } | |
f6dfbe31 CIK |
4475 | err = check_buffer_access(env, reg, regno, off, size, false, |
4476 | "rdonly", | |
afbf21dc YS |
4477 | &env->prog->aux->max_rdonly_access); |
4478 | if (!err && value_regno >= 0) | |
4479 | mark_reg_unknown(env, regs, value_regno); | |
4480 | } else if (reg->type == PTR_TO_RDWR_BUF) { | |
f6dfbe31 CIK |
4481 | err = check_buffer_access(env, reg, regno, off, size, false, |
4482 | "rdwr", | |
afbf21dc YS |
4483 | &env->prog->aux->max_rdwr_access); |
4484 | if (!err && t == BPF_READ && value_regno >= 0) | |
4485 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 4486 | } else { |
61bd5218 JK |
4487 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
4488 | reg_type_str[reg->type]); | |
17a52670 AS |
4489 | return -EACCES; |
4490 | } | |
969bf05e | 4491 | |
f1174f77 | 4492 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 4493 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 4494 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 4495 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 4496 | } |
17a52670 AS |
4497 | return err; |
4498 | } | |
4499 | ||
91c960b0 | 4500 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 4501 | { |
5ffa2550 | 4502 | int load_reg; |
17a52670 AS |
4503 | int err; |
4504 | ||
5ca419f2 BJ |
4505 | switch (insn->imm) { |
4506 | case BPF_ADD: | |
4507 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
4508 | case BPF_AND: |
4509 | case BPF_AND | BPF_FETCH: | |
4510 | case BPF_OR: | |
4511 | case BPF_OR | BPF_FETCH: | |
4512 | case BPF_XOR: | |
4513 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
4514 | case BPF_XCHG: |
4515 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
4516 | break; |
4517 | default: | |
91c960b0 BJ |
4518 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
4519 | return -EINVAL; | |
4520 | } | |
4521 | ||
4522 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
4523 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
4524 | return -EINVAL; |
4525 | } | |
4526 | ||
4527 | /* check src1 operand */ | |
dc503a8a | 4528 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4529 | if (err) |
4530 | return err; | |
4531 | ||
4532 | /* check src2 operand */ | |
dc503a8a | 4533 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4534 | if (err) |
4535 | return err; | |
4536 | ||
5ffa2550 BJ |
4537 | if (insn->imm == BPF_CMPXCHG) { |
4538 | /* Check comparison of R0 with memory location */ | |
4539 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
4540 | if (err) | |
4541 | return err; | |
4542 | } | |
4543 | ||
6bdf6abc | 4544 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 4545 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
4546 | return -EACCES; |
4547 | } | |
4548 | ||
ca369602 | 4549 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 4550 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
4551 | is_flow_key_reg(env, insn->dst_reg) || |
4552 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 4553 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f DB |
4554 | insn->dst_reg, |
4555 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
4556 | return -EACCES; |
4557 | } | |
4558 | ||
37086bfd BJ |
4559 | if (insn->imm & BPF_FETCH) { |
4560 | if (insn->imm == BPF_CMPXCHG) | |
4561 | load_reg = BPF_REG_0; | |
4562 | else | |
4563 | load_reg = insn->src_reg; | |
4564 | ||
4565 | /* check and record load of old value */ | |
4566 | err = check_reg_arg(env, load_reg, DST_OP); | |
4567 | if (err) | |
4568 | return err; | |
4569 | } else { | |
4570 | /* This instruction accesses a memory location but doesn't | |
4571 | * actually load it into a register. | |
4572 | */ | |
4573 | load_reg = -1; | |
4574 | } | |
4575 | ||
91c960b0 | 4576 | /* check whether we can read the memory */ |
31fd8581 | 4577 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
37086bfd | 4578 | BPF_SIZE(insn->code), BPF_READ, load_reg, true); |
17a52670 AS |
4579 | if (err) |
4580 | return err; | |
4581 | ||
91c960b0 | 4582 | /* check whether we can write into the same memory */ |
5ca419f2 BJ |
4583 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
4584 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
4585 | if (err) | |
4586 | return err; | |
4587 | ||
5ca419f2 | 4588 | return 0; |
17a52670 AS |
4589 | } |
4590 | ||
01f810ac AM |
4591 | /* When register 'regno' is used to read the stack (either directly or through |
4592 | * a helper function) make sure that it's within stack boundary and, depending | |
4593 | * on the access type, that all elements of the stack are initialized. | |
4594 | * | |
4595 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
4596 | * | |
4597 | * All registers that have been spilled on the stack in the slots within the | |
4598 | * read offsets are marked as read. | |
4599 | */ | |
4600 | static int check_stack_range_initialized( | |
4601 | struct bpf_verifier_env *env, int regno, int off, | |
4602 | int access_size, bool zero_size_allowed, | |
4603 | enum stack_access_src type, struct bpf_call_arg_meta *meta) | |
2011fccf AI |
4604 | { |
4605 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
4606 | struct bpf_func_state *state = func(env, reg); |
4607 | int err, min_off, max_off, i, j, slot, spi; | |
4608 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
4609 | enum bpf_access_type bounds_check_type; | |
4610 | /* Some accesses can write anything into the stack, others are | |
4611 | * read-only. | |
4612 | */ | |
4613 | bool clobber = false; | |
2011fccf | 4614 | |
01f810ac AM |
4615 | if (access_size == 0 && !zero_size_allowed) { |
4616 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
4617 | return -EACCES; |
4618 | } | |
2011fccf | 4619 | |
01f810ac AM |
4620 | if (type == ACCESS_HELPER) { |
4621 | /* The bounds checks for writes are more permissive than for | |
4622 | * reads. However, if raw_mode is not set, we'll do extra | |
4623 | * checks below. | |
4624 | */ | |
4625 | bounds_check_type = BPF_WRITE; | |
4626 | clobber = true; | |
4627 | } else { | |
4628 | bounds_check_type = BPF_READ; | |
4629 | } | |
4630 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
4631 | type, bounds_check_type); | |
4632 | if (err) | |
4633 | return err; | |
4634 | ||
17a52670 | 4635 | |
2011fccf | 4636 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 4637 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 4638 | } else { |
088ec26d AI |
4639 | /* Variable offset is prohibited for unprivileged mode for |
4640 | * simplicity since it requires corresponding support in | |
4641 | * Spectre masking for stack ALU. | |
4642 | * See also retrieve_ptr_limit(). | |
4643 | */ | |
2c78ee89 | 4644 | if (!env->bypass_spec_v1) { |
088ec26d | 4645 | char tn_buf[48]; |
f1174f77 | 4646 | |
088ec26d | 4647 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
4648 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
4649 | regno, err_extra, tn_buf); | |
088ec26d AI |
4650 | return -EACCES; |
4651 | } | |
f2bcd05e AI |
4652 | /* Only initialized buffer on stack is allowed to be accessed |
4653 | * with variable offset. With uninitialized buffer it's hard to | |
4654 | * guarantee that whole memory is marked as initialized on | |
4655 | * helper return since specific bounds are unknown what may | |
4656 | * cause uninitialized stack leaking. | |
4657 | */ | |
4658 | if (meta && meta->raw_mode) | |
4659 | meta = NULL; | |
4660 | ||
01f810ac AM |
4661 | min_off = reg->smin_value + off; |
4662 | max_off = reg->smax_value + off; | |
17a52670 AS |
4663 | } |
4664 | ||
435faee1 DB |
4665 | if (meta && meta->raw_mode) { |
4666 | meta->access_size = access_size; | |
4667 | meta->regno = regno; | |
4668 | return 0; | |
4669 | } | |
4670 | ||
2011fccf | 4671 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
4672 | u8 *stype; |
4673 | ||
2011fccf | 4674 | slot = -i - 1; |
638f5b90 | 4675 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
4676 | if (state->allocated_stack <= slot) |
4677 | goto err; | |
4678 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
4679 | if (*stype == STACK_MISC) | |
4680 | goto mark; | |
4681 | if (*stype == STACK_ZERO) { | |
01f810ac AM |
4682 | if (clobber) { |
4683 | /* helper can write anything into the stack */ | |
4684 | *stype = STACK_MISC; | |
4685 | } | |
cc2b14d5 | 4686 | goto mark; |
17a52670 | 4687 | } |
1d68f22b | 4688 | |
27113c59 | 4689 | if (is_spilled_reg(&state->stack[spi]) && |
1d68f22b YS |
4690 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
4691 | goto mark; | |
4692 | ||
27113c59 | 4693 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
4694 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
4695 | env->allow_ptr_leaks)) { | |
01f810ac AM |
4696 | if (clobber) { |
4697 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
4698 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 4699 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 4700 | } |
f7cf25b2 AS |
4701 | goto mark; |
4702 | } | |
4703 | ||
cc2b14d5 | 4704 | err: |
2011fccf | 4705 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
4706 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
4707 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
4708 | } else { |
4709 | char tn_buf[48]; | |
4710 | ||
4711 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
4712 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
4713 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 4714 | } |
cc2b14d5 AS |
4715 | return -EACCES; |
4716 | mark: | |
4717 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
4718 | * the whole slot to be marked as 'read' | |
4719 | */ | |
679c782d | 4720 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
4721 | state->stack[spi].spilled_ptr.parent, |
4722 | REG_LIVE_READ64); | |
17a52670 | 4723 | } |
2011fccf | 4724 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
4725 | } |
4726 | ||
06c1c049 GB |
4727 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
4728 | int access_size, bool zero_size_allowed, | |
4729 | struct bpf_call_arg_meta *meta) | |
4730 | { | |
638f5b90 | 4731 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 4732 | |
f1174f77 | 4733 | switch (reg->type) { |
06c1c049 | 4734 | case PTR_TO_PACKET: |
de8f3a83 | 4735 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
4736 | return check_packet_access(env, regno, reg->off, access_size, |
4737 | zero_size_allowed); | |
69c087ba YS |
4738 | case PTR_TO_MAP_KEY: |
4739 | return check_mem_region_access(env, regno, reg->off, access_size, | |
4740 | reg->map_ptr->key_size, false); | |
06c1c049 | 4741 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
4742 | if (check_map_access_type(env, regno, reg->off, access_size, |
4743 | meta && meta->raw_mode ? BPF_WRITE : | |
4744 | BPF_READ)) | |
4745 | return -EACCES; | |
9fd29c08 YS |
4746 | return check_map_access(env, regno, reg->off, access_size, |
4747 | zero_size_allowed); | |
457f4436 AN |
4748 | case PTR_TO_MEM: |
4749 | return check_mem_region_access(env, regno, reg->off, | |
4750 | access_size, reg->mem_size, | |
4751 | zero_size_allowed); | |
afbf21dc YS |
4752 | case PTR_TO_RDONLY_BUF: |
4753 | if (meta && meta->raw_mode) | |
4754 | return -EACCES; | |
4755 | return check_buffer_access(env, reg, regno, reg->off, | |
4756 | access_size, zero_size_allowed, | |
4757 | "rdonly", | |
4758 | &env->prog->aux->max_rdonly_access); | |
4759 | case PTR_TO_RDWR_BUF: | |
4760 | return check_buffer_access(env, reg, regno, reg->off, | |
4761 | access_size, zero_size_allowed, | |
4762 | "rdwr", | |
4763 | &env->prog->aux->max_rdwr_access); | |
0d004c02 | 4764 | case PTR_TO_STACK: |
01f810ac AM |
4765 | return check_stack_range_initialized( |
4766 | env, | |
4767 | regno, reg->off, access_size, | |
4768 | zero_size_allowed, ACCESS_HELPER, meta); | |
0d004c02 LB |
4769 | default: /* scalar_value or invalid ptr */ |
4770 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
4771 | if (zero_size_allowed && access_size == 0 && | |
4772 | register_is_null(reg)) | |
4773 | return 0; | |
4774 | ||
4775 | verbose(env, "R%d type=%s expected=%s\n", regno, | |
4776 | reg_type_str[reg->type], | |
4777 | reg_type_str[PTR_TO_STACK]); | |
4778 | return -EACCES; | |
06c1c049 GB |
4779 | } |
4780 | } | |
4781 | ||
e5069b9c DB |
4782 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
4783 | u32 regno, u32 mem_size) | |
4784 | { | |
4785 | if (register_is_null(reg)) | |
4786 | return 0; | |
4787 | ||
4788 | if (reg_type_may_be_null(reg->type)) { | |
4789 | /* Assuming that the register contains a value check if the memory | |
4790 | * access is safe. Temporarily save and restore the register's state as | |
4791 | * the conversion shouldn't be visible to a caller. | |
4792 | */ | |
4793 | const struct bpf_reg_state saved_reg = *reg; | |
4794 | int rv; | |
4795 | ||
4796 | mark_ptr_not_null_reg(reg); | |
4797 | rv = check_helper_mem_access(env, regno, mem_size, true, NULL); | |
4798 | *reg = saved_reg; | |
4799 | return rv; | |
4800 | } | |
4801 | ||
4802 | return check_helper_mem_access(env, regno, mem_size, true, NULL); | |
4803 | } | |
4804 | ||
d83525ca AS |
4805 | /* Implementation details: |
4806 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
4807 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
4808 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
4809 | * value_or_null->value transition, since the verifier only cares about | |
4810 | * the range of access to valid map value pointer and doesn't care about actual | |
4811 | * address of the map element. | |
4812 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
4813 | * reg->id > 0 after value_or_null->value transition. By doing so | |
4814 | * two bpf_map_lookups will be considered two different pointers that | |
4815 | * point to different bpf_spin_locks. | |
4816 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
4817 | * dead-locks. | |
4818 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
4819 | * reg_is_refcounted() logic. The verifier needs to remember only | |
4820 | * one spin_lock instead of array of acquired_refs. | |
4821 | * cur_state->active_spin_lock remembers which map value element got locked | |
4822 | * and clears it after bpf_spin_unlock. | |
4823 | */ | |
4824 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
4825 | bool is_lock) | |
4826 | { | |
4827 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
4828 | struct bpf_verifier_state *cur = env->cur_state; | |
4829 | bool is_const = tnum_is_const(reg->var_off); | |
4830 | struct bpf_map *map = reg->map_ptr; | |
4831 | u64 val = reg->var_off.value; | |
4832 | ||
d83525ca AS |
4833 | if (!is_const) { |
4834 | verbose(env, | |
4835 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
4836 | regno); | |
4837 | return -EINVAL; | |
4838 | } | |
4839 | if (!map->btf) { | |
4840 | verbose(env, | |
4841 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
4842 | map->name); | |
4843 | return -EINVAL; | |
4844 | } | |
4845 | if (!map_value_has_spin_lock(map)) { | |
4846 | if (map->spin_lock_off == -E2BIG) | |
4847 | verbose(env, | |
4848 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
4849 | map->name); | |
4850 | else if (map->spin_lock_off == -ENOENT) | |
4851 | verbose(env, | |
4852 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
4853 | map->name); | |
4854 | else | |
4855 | verbose(env, | |
4856 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
4857 | map->name); | |
4858 | return -EINVAL; | |
4859 | } | |
4860 | if (map->spin_lock_off != val + reg->off) { | |
4861 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
4862 | val + reg->off); | |
4863 | return -EINVAL; | |
4864 | } | |
4865 | if (is_lock) { | |
4866 | if (cur->active_spin_lock) { | |
4867 | verbose(env, | |
4868 | "Locking two bpf_spin_locks are not allowed\n"); | |
4869 | return -EINVAL; | |
4870 | } | |
4871 | cur->active_spin_lock = reg->id; | |
4872 | } else { | |
4873 | if (!cur->active_spin_lock) { | |
4874 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
4875 | return -EINVAL; | |
4876 | } | |
4877 | if (cur->active_spin_lock != reg->id) { | |
4878 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
4879 | return -EINVAL; | |
4880 | } | |
4881 | cur->active_spin_lock = 0; | |
4882 | } | |
4883 | return 0; | |
4884 | } | |
4885 | ||
b00628b1 AS |
4886 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
4887 | struct bpf_call_arg_meta *meta) | |
4888 | { | |
4889 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
4890 | bool is_const = tnum_is_const(reg->var_off); | |
4891 | struct bpf_map *map = reg->map_ptr; | |
4892 | u64 val = reg->var_off.value; | |
4893 | ||
4894 | if (!is_const) { | |
4895 | verbose(env, | |
4896 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
4897 | regno); | |
4898 | return -EINVAL; | |
4899 | } | |
4900 | if (!map->btf) { | |
4901 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
4902 | map->name); | |
4903 | return -EINVAL; | |
4904 | } | |
68134668 AS |
4905 | if (!map_value_has_timer(map)) { |
4906 | if (map->timer_off == -E2BIG) | |
4907 | verbose(env, | |
4908 | "map '%s' has more than one 'struct bpf_timer'\n", | |
4909 | map->name); | |
4910 | else if (map->timer_off == -ENOENT) | |
4911 | verbose(env, | |
4912 | "map '%s' doesn't have 'struct bpf_timer'\n", | |
4913 | map->name); | |
4914 | else | |
4915 | verbose(env, | |
4916 | "map '%s' is not a struct type or bpf_timer is mangled\n", | |
4917 | map->name); | |
4918 | return -EINVAL; | |
4919 | } | |
4920 | if (map->timer_off != val + reg->off) { | |
4921 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", | |
4922 | val + reg->off, map->timer_off); | |
b00628b1 AS |
4923 | return -EINVAL; |
4924 | } | |
4925 | if (meta->map_ptr) { | |
4926 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
4927 | return -EFAULT; | |
4928 | } | |
3e8ce298 | 4929 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
4930 | meta->map_ptr = map; |
4931 | return 0; | |
4932 | } | |
4933 | ||
90133415 DB |
4934 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
4935 | { | |
4936 | return type == ARG_PTR_TO_MEM || | |
4937 | type == ARG_PTR_TO_MEM_OR_NULL || | |
4938 | type == ARG_PTR_TO_UNINIT_MEM; | |
4939 | } | |
4940 | ||
4941 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
4942 | { | |
4943 | return type == ARG_CONST_SIZE || | |
4944 | type == ARG_CONST_SIZE_OR_ZERO; | |
4945 | } | |
4946 | ||
457f4436 AN |
4947 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
4948 | { | |
4949 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; | |
4950 | } | |
4951 | ||
57c3bb72 AI |
4952 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
4953 | { | |
4954 | return type == ARG_PTR_TO_INT || | |
4955 | type == ARG_PTR_TO_LONG; | |
4956 | } | |
4957 | ||
4958 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
4959 | { | |
4960 | if (type == ARG_PTR_TO_INT) | |
4961 | return sizeof(u32); | |
4962 | else if (type == ARG_PTR_TO_LONG) | |
4963 | return sizeof(u64); | |
4964 | ||
4965 | return -EINVAL; | |
4966 | } | |
4967 | ||
912f442c LB |
4968 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
4969 | const struct bpf_call_arg_meta *meta, | |
4970 | enum bpf_arg_type *arg_type) | |
4971 | { | |
4972 | if (!meta->map_ptr) { | |
4973 | /* kernel subsystem misconfigured verifier */ | |
4974 | verbose(env, "invalid map_ptr to access map->type\n"); | |
4975 | return -EACCES; | |
4976 | } | |
4977 | ||
4978 | switch (meta->map_ptr->map_type) { | |
4979 | case BPF_MAP_TYPE_SOCKMAP: | |
4980 | case BPF_MAP_TYPE_SOCKHASH: | |
4981 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 4982 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
4983 | } else { |
4984 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
4985 | return -EINVAL; | |
4986 | } | |
4987 | break; | |
9330986c JK |
4988 | case BPF_MAP_TYPE_BLOOM_FILTER: |
4989 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
4990 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
4991 | break; | |
912f442c LB |
4992 | default: |
4993 | break; | |
4994 | } | |
4995 | return 0; | |
4996 | } | |
4997 | ||
f79e7ea5 LB |
4998 | struct bpf_reg_types { |
4999 | const enum bpf_reg_type types[10]; | |
1df8f55a | 5000 | u32 *btf_id; |
f79e7ea5 LB |
5001 | }; |
5002 | ||
5003 | static const struct bpf_reg_types map_key_value_types = { | |
5004 | .types = { | |
5005 | PTR_TO_STACK, | |
5006 | PTR_TO_PACKET, | |
5007 | PTR_TO_PACKET_META, | |
69c087ba | 5008 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5009 | PTR_TO_MAP_VALUE, |
5010 | }, | |
5011 | }; | |
5012 | ||
5013 | static const struct bpf_reg_types sock_types = { | |
5014 | .types = { | |
5015 | PTR_TO_SOCK_COMMON, | |
5016 | PTR_TO_SOCKET, | |
5017 | PTR_TO_TCP_SOCK, | |
5018 | PTR_TO_XDP_SOCK, | |
5019 | }, | |
5020 | }; | |
5021 | ||
49a2a4d4 | 5022 | #ifdef CONFIG_NET |
1df8f55a MKL |
5023 | static const struct bpf_reg_types btf_id_sock_common_types = { |
5024 | .types = { | |
5025 | PTR_TO_SOCK_COMMON, | |
5026 | PTR_TO_SOCKET, | |
5027 | PTR_TO_TCP_SOCK, | |
5028 | PTR_TO_XDP_SOCK, | |
5029 | PTR_TO_BTF_ID, | |
5030 | }, | |
5031 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
5032 | }; | |
49a2a4d4 | 5033 | #endif |
1df8f55a | 5034 | |
f79e7ea5 LB |
5035 | static const struct bpf_reg_types mem_types = { |
5036 | .types = { | |
5037 | PTR_TO_STACK, | |
5038 | PTR_TO_PACKET, | |
5039 | PTR_TO_PACKET_META, | |
69c087ba | 5040 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5041 | PTR_TO_MAP_VALUE, |
5042 | PTR_TO_MEM, | |
5043 | PTR_TO_RDONLY_BUF, | |
5044 | PTR_TO_RDWR_BUF, | |
5045 | }, | |
5046 | }; | |
5047 | ||
5048 | static const struct bpf_reg_types int_ptr_types = { | |
5049 | .types = { | |
5050 | PTR_TO_STACK, | |
5051 | PTR_TO_PACKET, | |
5052 | PTR_TO_PACKET_META, | |
69c087ba | 5053 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5054 | PTR_TO_MAP_VALUE, |
5055 | }, | |
5056 | }; | |
5057 | ||
5058 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; | |
5059 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
5060 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
5061 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; | |
5062 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; | |
5063 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
5064 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; | |
eaa6bcb7 | 5065 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
69c087ba YS |
5066 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
5067 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 5068 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 5069 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
f79e7ea5 | 5070 | |
0789e13b | 5071 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
f79e7ea5 LB |
5072 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
5073 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, | |
5074 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, | |
5075 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, | |
5076 | [ARG_CONST_SIZE] = &scalar_types, | |
5077 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
5078 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
5079 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
5080 | [ARG_PTR_TO_CTX] = &context_types, | |
5081 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, | |
5082 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, | |
49a2a4d4 | 5083 | #ifdef CONFIG_NET |
1df8f55a | 5084 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 5085 | #endif |
f79e7ea5 LB |
5086 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
5087 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, | |
5088 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, | |
5089 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
5090 | [ARG_PTR_TO_MEM] = &mem_types, | |
5091 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, | |
5092 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, | |
5093 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, | |
5094 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, | |
5095 | [ARG_PTR_TO_INT] = &int_ptr_types, | |
5096 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 5097 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba YS |
5098 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
5099 | [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types, | |
fff13c4b | 5100 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 5101 | [ARG_PTR_TO_TIMER] = &timer_types, |
f79e7ea5 LB |
5102 | }; |
5103 | ||
5104 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 MKL |
5105 | enum bpf_arg_type arg_type, |
5106 | const u32 *arg_btf_id) | |
f79e7ea5 LB |
5107 | { |
5108 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5109 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 5110 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
5111 | int i, j; |
5112 | ||
a968d5e2 MKL |
5113 | compatible = compatible_reg_types[arg_type]; |
5114 | if (!compatible) { | |
5115 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
5116 | return -EFAULT; | |
5117 | } | |
5118 | ||
f79e7ea5 LB |
5119 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
5120 | expected = compatible->types[i]; | |
5121 | if (expected == NOT_INIT) | |
5122 | break; | |
5123 | ||
5124 | if (type == expected) | |
a968d5e2 | 5125 | goto found; |
f79e7ea5 LB |
5126 | } |
5127 | ||
5128 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); | |
5129 | for (j = 0; j + 1 < i; j++) | |
5130 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); | |
5131 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); | |
5132 | return -EACCES; | |
a968d5e2 MKL |
5133 | |
5134 | found: | |
5135 | if (type == PTR_TO_BTF_ID) { | |
1df8f55a MKL |
5136 | if (!arg_btf_id) { |
5137 | if (!compatible->btf_id) { | |
5138 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
5139 | return -EFAULT; | |
5140 | } | |
5141 | arg_btf_id = compatible->btf_id; | |
5142 | } | |
5143 | ||
22dc4a0f AN |
5144 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, |
5145 | btf_vmlinux, *arg_btf_id)) { | |
a968d5e2 | 5146 | verbose(env, "R%d is of type %s but %s is expected\n", |
22dc4a0f AN |
5147 | regno, kernel_type_name(reg->btf, reg->btf_id), |
5148 | kernel_type_name(btf_vmlinux, *arg_btf_id)); | |
a968d5e2 MKL |
5149 | return -EACCES; |
5150 | } | |
5151 | ||
5152 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5153 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", | |
5154 | regno); | |
5155 | return -EACCES; | |
5156 | } | |
5157 | } | |
5158 | ||
5159 | return 0; | |
f79e7ea5 LB |
5160 | } |
5161 | ||
af7ec138 YS |
5162 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
5163 | struct bpf_call_arg_meta *meta, | |
5164 | const struct bpf_func_proto *fn) | |
17a52670 | 5165 | { |
af7ec138 | 5166 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 5167 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 5168 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 5169 | enum bpf_reg_type type = reg->type; |
17a52670 AS |
5170 | int err = 0; |
5171 | ||
80f1d68c | 5172 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
5173 | return 0; |
5174 | ||
dc503a8a EC |
5175 | err = check_reg_arg(env, regno, SRC_OP); |
5176 | if (err) | |
5177 | return err; | |
17a52670 | 5178 | |
1be7f75d AS |
5179 | if (arg_type == ARG_ANYTHING) { |
5180 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
5181 | verbose(env, "R%d leaks addr into helper function\n", |
5182 | regno); | |
1be7f75d AS |
5183 | return -EACCES; |
5184 | } | |
80f1d68c | 5185 | return 0; |
1be7f75d | 5186 | } |
80f1d68c | 5187 | |
de8f3a83 | 5188 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 5189 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 5190 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
5191 | return -EACCES; |
5192 | } | |
5193 | ||
912f442c LB |
5194 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
5195 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || | |
5196 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
5197 | err = resolve_map_arg_type(env, meta, &arg_type); | |
5198 | if (err) | |
5199 | return err; | |
5200 | } | |
5201 | ||
fd1b0d60 LB |
5202 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
5203 | /* A NULL register has a SCALAR_VALUE type, so skip | |
5204 | * type checking. | |
5205 | */ | |
5206 | goto skip_type_check; | |
5207 | ||
a968d5e2 | 5208 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
f79e7ea5 LB |
5209 | if (err) |
5210 | return err; | |
5211 | ||
a968d5e2 | 5212 | if (type == PTR_TO_CTX) { |
feec7040 LB |
5213 | err = check_ctx_reg(env, reg, regno); |
5214 | if (err < 0) | |
5215 | return err; | |
d7b9454a LB |
5216 | } |
5217 | ||
fd1b0d60 | 5218 | skip_type_check: |
02f7c958 | 5219 | if (reg->ref_obj_id) { |
457f4436 AN |
5220 | if (meta->ref_obj_id) { |
5221 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
5222 | regno, reg->ref_obj_id, | |
5223 | meta->ref_obj_id); | |
5224 | return -EFAULT; | |
5225 | } | |
5226 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
5227 | } |
5228 | ||
17a52670 AS |
5229 | if (arg_type == ARG_CONST_MAP_PTR) { |
5230 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
3e8ce298 AS |
5231 | if (meta->map_ptr) { |
5232 | /* Use map_uid (which is unique id of inner map) to reject: | |
5233 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
5234 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
5235 | * if (inner_map1 && inner_map2) { | |
5236 | * timer = bpf_map_lookup_elem(inner_map1); | |
5237 | * if (timer) | |
5238 | * // mismatch would have been allowed | |
5239 | * bpf_timer_init(timer, inner_map2); | |
5240 | * } | |
5241 | * | |
5242 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
5243 | */ | |
5244 | if (meta->map_ptr != reg->map_ptr || | |
5245 | meta->map_uid != reg->map_uid) { | |
5246 | verbose(env, | |
5247 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
5248 | meta->map_uid, reg->map_uid); | |
5249 | return -EINVAL; | |
5250 | } | |
b00628b1 | 5251 | } |
33ff9823 | 5252 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 5253 | meta->map_uid = reg->map_uid; |
17a52670 AS |
5254 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
5255 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
5256 | * check that [key, key + map->key_size) are within | |
5257 | * stack limits and initialized | |
5258 | */ | |
33ff9823 | 5259 | if (!meta->map_ptr) { |
17a52670 AS |
5260 | /* in function declaration map_ptr must come before |
5261 | * map_key, so that it's verified and known before | |
5262 | * we have to check map_key here. Otherwise it means | |
5263 | * that kernel subsystem misconfigured verifier | |
5264 | */ | |
61bd5218 | 5265 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
5266 | return -EACCES; |
5267 | } | |
d71962f3 PC |
5268 | err = check_helper_mem_access(env, regno, |
5269 | meta->map_ptr->key_size, false, | |
5270 | NULL); | |
2ea864c5 | 5271 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
5272 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
5273 | !register_is_null(reg)) || | |
2ea864c5 | 5274 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
5275 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
5276 | * check [value, value + map->value_size) validity | |
5277 | */ | |
33ff9823 | 5278 | if (!meta->map_ptr) { |
17a52670 | 5279 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 5280 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
5281 | return -EACCES; |
5282 | } | |
2ea864c5 | 5283 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
5284 | err = check_helper_mem_access(env, regno, |
5285 | meta->map_ptr->value_size, false, | |
2ea864c5 | 5286 | meta); |
eaa6bcb7 HL |
5287 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
5288 | if (!reg->btf_id) { | |
5289 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
5290 | return -EACCES; | |
5291 | } | |
22dc4a0f | 5292 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 5293 | meta->ret_btf_id = reg->btf_id; |
c18f0b6a LB |
5294 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
5295 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
5296 | if (process_spin_lock(env, regno, true)) | |
5297 | return -EACCES; | |
5298 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
5299 | if (process_spin_lock(env, regno, false)) | |
5300 | return -EACCES; | |
5301 | } else { | |
5302 | verbose(env, "verifier internal error\n"); | |
5303 | return -EFAULT; | |
5304 | } | |
b00628b1 AS |
5305 | } else if (arg_type == ARG_PTR_TO_TIMER) { |
5306 | if (process_timer_func(env, regno, meta)) | |
5307 | return -EACCES; | |
69c087ba YS |
5308 | } else if (arg_type == ARG_PTR_TO_FUNC) { |
5309 | meta->subprogno = reg->subprogno; | |
a2bbe7cc LB |
5310 | } else if (arg_type_is_mem_ptr(arg_type)) { |
5311 | /* The access to this pointer is only checked when we hit the | |
5312 | * next is_mem_size argument below. | |
5313 | */ | |
5314 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); | |
90133415 | 5315 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 5316 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 5317 | |
10060503 JF |
5318 | /* This is used to refine r0 return value bounds for helpers |
5319 | * that enforce this value as an upper bound on return values. | |
5320 | * See do_refine_retval_range() for helpers that can refine | |
5321 | * the return value. C type of helper is u32 so we pull register | |
5322 | * bound from umax_value however, if negative verifier errors | |
5323 | * out. Only upper bounds can be learned because retval is an | |
5324 | * int type and negative retvals are allowed. | |
849fa506 | 5325 | */ |
10060503 | 5326 | meta->msize_max_value = reg->umax_value; |
849fa506 | 5327 | |
f1174f77 EC |
5328 | /* The register is SCALAR_VALUE; the access check |
5329 | * happens using its boundaries. | |
06c1c049 | 5330 | */ |
f1174f77 | 5331 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
5332 | /* For unprivileged variable accesses, disable raw |
5333 | * mode so that the program is required to | |
5334 | * initialize all the memory that the helper could | |
5335 | * just partially fill up. | |
5336 | */ | |
5337 | meta = NULL; | |
5338 | ||
b03c9f9f | 5339 | if (reg->smin_value < 0) { |
61bd5218 | 5340 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
5341 | regno); |
5342 | return -EACCES; | |
5343 | } | |
06c1c049 | 5344 | |
b03c9f9f | 5345 | if (reg->umin_value == 0) { |
f1174f77 EC |
5346 | err = check_helper_mem_access(env, regno - 1, 0, |
5347 | zero_size_allowed, | |
5348 | meta); | |
06c1c049 GB |
5349 | if (err) |
5350 | return err; | |
06c1c049 | 5351 | } |
f1174f77 | 5352 | |
b03c9f9f | 5353 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 5354 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
5355 | regno); |
5356 | return -EACCES; | |
5357 | } | |
5358 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 5359 | reg->umax_value, |
f1174f77 | 5360 | zero_size_allowed, meta); |
b5dc0163 AS |
5361 | if (!err) |
5362 | err = mark_chain_precision(env, regno); | |
457f4436 AN |
5363 | } else if (arg_type_is_alloc_size(arg_type)) { |
5364 | if (!tnum_is_const(reg->var_off)) { | |
28a8add6 | 5365 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
5366 | regno); |
5367 | return -EACCES; | |
5368 | } | |
5369 | meta->mem_size = reg->var_off.value; | |
57c3bb72 AI |
5370 | } else if (arg_type_is_int_ptr(arg_type)) { |
5371 | int size = int_ptr_type_to_size(arg_type); | |
5372 | ||
5373 | err = check_helper_mem_access(env, regno, size, false, meta); | |
5374 | if (err) | |
5375 | return err; | |
5376 | err = check_ptr_alignment(env, reg, 0, size, true); | |
fff13c4b FR |
5377 | } else if (arg_type == ARG_PTR_TO_CONST_STR) { |
5378 | struct bpf_map *map = reg->map_ptr; | |
5379 | int map_off; | |
5380 | u64 map_addr; | |
5381 | char *str_ptr; | |
5382 | ||
a8fad73e | 5383 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
5384 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
5385 | return -EACCES; | |
5386 | } | |
5387 | ||
5388 | if (!tnum_is_const(reg->var_off)) { | |
5389 | verbose(env, "R%d is not a constant address'\n", regno); | |
5390 | return -EACCES; | |
5391 | } | |
5392 | ||
5393 | if (!map->ops->map_direct_value_addr) { | |
5394 | verbose(env, "no direct value access support for this map type\n"); | |
5395 | return -EACCES; | |
5396 | } | |
5397 | ||
5398 | err = check_map_access(env, regno, reg->off, | |
5399 | map->value_size - reg->off, false); | |
5400 | if (err) | |
5401 | return err; | |
5402 | ||
5403 | map_off = reg->off + reg->var_off.value; | |
5404 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
5405 | if (err) { | |
5406 | verbose(env, "direct value access on string failed\n"); | |
5407 | return err; | |
5408 | } | |
5409 | ||
5410 | str_ptr = (char *)(long)(map_addr); | |
5411 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
5412 | verbose(env, "string is not zero-terminated\n"); | |
5413 | return -EINVAL; | |
5414 | } | |
17a52670 AS |
5415 | } |
5416 | ||
5417 | return err; | |
5418 | } | |
5419 | ||
0126240f LB |
5420 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
5421 | { | |
5422 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 5423 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
5424 | |
5425 | if (func_id != BPF_FUNC_map_update_elem) | |
5426 | return false; | |
5427 | ||
5428 | /* It's not possible to get access to a locked struct sock in these | |
5429 | * contexts, so updating is safe. | |
5430 | */ | |
5431 | switch (type) { | |
5432 | case BPF_PROG_TYPE_TRACING: | |
5433 | if (eatype == BPF_TRACE_ITER) | |
5434 | return true; | |
5435 | break; | |
5436 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
5437 | case BPF_PROG_TYPE_SCHED_CLS: | |
5438 | case BPF_PROG_TYPE_SCHED_ACT: | |
5439 | case BPF_PROG_TYPE_XDP: | |
5440 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
5441 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
5442 | case BPF_PROG_TYPE_SK_LOOKUP: | |
5443 | return true; | |
5444 | default: | |
5445 | break; | |
5446 | } | |
5447 | ||
5448 | verbose(env, "cannot update sockmap in this context\n"); | |
5449 | return false; | |
5450 | } | |
5451 | ||
e411901c MF |
5452 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
5453 | { | |
5454 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); | |
5455 | } | |
5456 | ||
61bd5218 JK |
5457 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
5458 | struct bpf_map *map, int func_id) | |
35578d79 | 5459 | { |
35578d79 KX |
5460 | if (!map) |
5461 | return 0; | |
5462 | ||
6aff67c8 AS |
5463 | /* We need a two way check, first is from map perspective ... */ |
5464 | switch (map->map_type) { | |
5465 | case BPF_MAP_TYPE_PROG_ARRAY: | |
5466 | if (func_id != BPF_FUNC_tail_call) | |
5467 | goto error; | |
5468 | break; | |
5469 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
5470 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 5471 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 5472 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
5473 | func_id != BPF_FUNC_perf_event_read_value && |
5474 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
5475 | goto error; |
5476 | break; | |
457f4436 AN |
5477 | case BPF_MAP_TYPE_RINGBUF: |
5478 | if (func_id != BPF_FUNC_ringbuf_output && | |
5479 | func_id != BPF_FUNC_ringbuf_reserve && | |
457f4436 AN |
5480 | func_id != BPF_FUNC_ringbuf_query) |
5481 | goto error; | |
5482 | break; | |
6aff67c8 AS |
5483 | case BPF_MAP_TYPE_STACK_TRACE: |
5484 | if (func_id != BPF_FUNC_get_stackid) | |
5485 | goto error; | |
5486 | break; | |
4ed8ec52 | 5487 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 5488 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 5489 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
5490 | goto error; |
5491 | break; | |
cd339431 | 5492 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 5493 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
5494 | if (func_id != BPF_FUNC_get_local_storage) |
5495 | goto error; | |
5496 | break; | |
546ac1ff | 5497 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 5498 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
5499 | if (func_id != BPF_FUNC_redirect_map && |
5500 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
5501 | goto error; |
5502 | break; | |
fbfc504a BT |
5503 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
5504 | * appear. | |
5505 | */ | |
6710e112 JDB |
5506 | case BPF_MAP_TYPE_CPUMAP: |
5507 | if (func_id != BPF_FUNC_redirect_map) | |
5508 | goto error; | |
5509 | break; | |
fada7fdc JL |
5510 | case BPF_MAP_TYPE_XSKMAP: |
5511 | if (func_id != BPF_FUNC_redirect_map && | |
5512 | func_id != BPF_FUNC_map_lookup_elem) | |
5513 | goto error; | |
5514 | break; | |
56f668df | 5515 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 5516 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
5517 | if (func_id != BPF_FUNC_map_lookup_elem) |
5518 | goto error; | |
16a43625 | 5519 | break; |
174a79ff JF |
5520 | case BPF_MAP_TYPE_SOCKMAP: |
5521 | if (func_id != BPF_FUNC_sk_redirect_map && | |
5522 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 5523 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 5524 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 5525 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
5526 | func_id != BPF_FUNC_map_lookup_elem && |
5527 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
5528 | goto error; |
5529 | break; | |
81110384 JF |
5530 | case BPF_MAP_TYPE_SOCKHASH: |
5531 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
5532 | func_id != BPF_FUNC_sock_hash_update && | |
5533 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 5534 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 5535 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
5536 | func_id != BPF_FUNC_map_lookup_elem && |
5537 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
5538 | goto error; |
5539 | break; | |
2dbb9b9e MKL |
5540 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
5541 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
5542 | goto error; | |
5543 | break; | |
f1a2e44a MV |
5544 | case BPF_MAP_TYPE_QUEUE: |
5545 | case BPF_MAP_TYPE_STACK: | |
5546 | if (func_id != BPF_FUNC_map_peek_elem && | |
5547 | func_id != BPF_FUNC_map_pop_elem && | |
5548 | func_id != BPF_FUNC_map_push_elem) | |
5549 | goto error; | |
5550 | break; | |
6ac99e8f MKL |
5551 | case BPF_MAP_TYPE_SK_STORAGE: |
5552 | if (func_id != BPF_FUNC_sk_storage_get && | |
5553 | func_id != BPF_FUNC_sk_storage_delete) | |
5554 | goto error; | |
5555 | break; | |
8ea63684 KS |
5556 | case BPF_MAP_TYPE_INODE_STORAGE: |
5557 | if (func_id != BPF_FUNC_inode_storage_get && | |
5558 | func_id != BPF_FUNC_inode_storage_delete) | |
5559 | goto error; | |
5560 | break; | |
4cf1bc1f KS |
5561 | case BPF_MAP_TYPE_TASK_STORAGE: |
5562 | if (func_id != BPF_FUNC_task_storage_get && | |
5563 | func_id != BPF_FUNC_task_storage_delete) | |
5564 | goto error; | |
5565 | break; | |
9330986c JK |
5566 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5567 | if (func_id != BPF_FUNC_map_peek_elem && | |
5568 | func_id != BPF_FUNC_map_push_elem) | |
5569 | goto error; | |
5570 | break; | |
6aff67c8 AS |
5571 | default: |
5572 | break; | |
5573 | } | |
5574 | ||
5575 | /* ... and second from the function itself. */ | |
5576 | switch (func_id) { | |
5577 | case BPF_FUNC_tail_call: | |
5578 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
5579 | goto error; | |
e411901c MF |
5580 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
5581 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
5582 | return -EINVAL; |
5583 | } | |
6aff67c8 AS |
5584 | break; |
5585 | case BPF_FUNC_perf_event_read: | |
5586 | case BPF_FUNC_perf_event_output: | |
908432ca | 5587 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 5588 | case BPF_FUNC_skb_output: |
d831ee84 | 5589 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
5590 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
5591 | goto error; | |
5592 | break; | |
5b029a32 DB |
5593 | case BPF_FUNC_ringbuf_output: |
5594 | case BPF_FUNC_ringbuf_reserve: | |
5595 | case BPF_FUNC_ringbuf_query: | |
5596 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) | |
5597 | goto error; | |
5598 | break; | |
6aff67c8 AS |
5599 | case BPF_FUNC_get_stackid: |
5600 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
5601 | goto error; | |
5602 | break; | |
60d20f91 | 5603 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 5604 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
5605 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
5606 | goto error; | |
5607 | break; | |
97f91a7c | 5608 | case BPF_FUNC_redirect_map: |
9c270af3 | 5609 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 5610 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
5611 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
5612 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
5613 | goto error; |
5614 | break; | |
174a79ff | 5615 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 5616 | case BPF_FUNC_msg_redirect_map: |
81110384 | 5617 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
5618 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
5619 | goto error; | |
5620 | break; | |
81110384 JF |
5621 | case BPF_FUNC_sk_redirect_hash: |
5622 | case BPF_FUNC_msg_redirect_hash: | |
5623 | case BPF_FUNC_sock_hash_update: | |
5624 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
5625 | goto error; |
5626 | break; | |
cd339431 | 5627 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
5628 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
5629 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
5630 | goto error; |
5631 | break; | |
2dbb9b9e | 5632 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
5633 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
5634 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
5635 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
5636 | goto error; |
5637 | break; | |
f1a2e44a | 5638 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
5639 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
5640 | map->map_type != BPF_MAP_TYPE_STACK) | |
5641 | goto error; | |
5642 | break; | |
9330986c JK |
5643 | case BPF_FUNC_map_peek_elem: |
5644 | case BPF_FUNC_map_push_elem: | |
5645 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
5646 | map->map_type != BPF_MAP_TYPE_STACK && | |
5647 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
5648 | goto error; | |
5649 | break; | |
6ac99e8f MKL |
5650 | case BPF_FUNC_sk_storage_get: |
5651 | case BPF_FUNC_sk_storage_delete: | |
5652 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
5653 | goto error; | |
5654 | break; | |
8ea63684 KS |
5655 | case BPF_FUNC_inode_storage_get: |
5656 | case BPF_FUNC_inode_storage_delete: | |
5657 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
5658 | goto error; | |
5659 | break; | |
4cf1bc1f KS |
5660 | case BPF_FUNC_task_storage_get: |
5661 | case BPF_FUNC_task_storage_delete: | |
5662 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
5663 | goto error; | |
5664 | break; | |
6aff67c8 AS |
5665 | default: |
5666 | break; | |
35578d79 KX |
5667 | } |
5668 | ||
5669 | return 0; | |
6aff67c8 | 5670 | error: |
61bd5218 | 5671 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 5672 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 5673 | return -EINVAL; |
35578d79 KX |
5674 | } |
5675 | ||
90133415 | 5676 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
5677 | { |
5678 | int count = 0; | |
5679 | ||
39f19ebb | 5680 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5681 | count++; |
39f19ebb | 5682 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5683 | count++; |
39f19ebb | 5684 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5685 | count++; |
39f19ebb | 5686 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5687 | count++; |
39f19ebb | 5688 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
5689 | count++; |
5690 | ||
90133415 DB |
5691 | /* We only support one arg being in raw mode at the moment, |
5692 | * which is sufficient for the helper functions we have | |
5693 | * right now. | |
5694 | */ | |
5695 | return count <= 1; | |
5696 | } | |
5697 | ||
5698 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
5699 | enum bpf_arg_type arg_next) | |
5700 | { | |
5701 | return (arg_type_is_mem_ptr(arg_curr) && | |
5702 | !arg_type_is_mem_size(arg_next)) || | |
5703 | (!arg_type_is_mem_ptr(arg_curr) && | |
5704 | arg_type_is_mem_size(arg_next)); | |
5705 | } | |
5706 | ||
5707 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
5708 | { | |
5709 | /* bpf_xxx(..., buf, len) call will access 'len' | |
5710 | * bytes from memory 'buf'. Both arg types need | |
5711 | * to be paired, so make sure there's no buggy | |
5712 | * helper function specification. | |
5713 | */ | |
5714 | if (arg_type_is_mem_size(fn->arg1_type) || | |
5715 | arg_type_is_mem_ptr(fn->arg5_type) || | |
5716 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
5717 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
5718 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
5719 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
5720 | return false; | |
5721 | ||
5722 | return true; | |
5723 | } | |
5724 | ||
1b986589 | 5725 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
5726 | { |
5727 | int count = 0; | |
5728 | ||
1b986589 | 5729 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 5730 | count++; |
1b986589 | 5731 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 5732 | count++; |
1b986589 | 5733 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 5734 | count++; |
1b986589 | 5735 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 5736 | count++; |
1b986589 | 5737 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
5738 | count++; |
5739 | ||
1b986589 MKL |
5740 | /* A reference acquiring function cannot acquire |
5741 | * another refcounted ptr. | |
5742 | */ | |
64d85290 | 5743 | if (may_be_acquire_function(func_id) && count) |
1b986589 MKL |
5744 | return false; |
5745 | ||
fd978bf7 JS |
5746 | /* We only support one arg being unreferenced at the moment, |
5747 | * which is sufficient for the helper functions we have right now. | |
5748 | */ | |
5749 | return count <= 1; | |
5750 | } | |
5751 | ||
9436ef6e LB |
5752 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
5753 | { | |
5754 | int i; | |
5755 | ||
1df8f55a | 5756 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
9436ef6e LB |
5757 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
5758 | return false; | |
5759 | ||
1df8f55a MKL |
5760 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
5761 | return false; | |
5762 | } | |
5763 | ||
9436ef6e LB |
5764 | return true; |
5765 | } | |
5766 | ||
1b986589 | 5767 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
5768 | { |
5769 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 5770 | check_arg_pair_ok(fn) && |
9436ef6e | 5771 | check_btf_id_ok(fn) && |
1b986589 | 5772 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
5773 | } |
5774 | ||
de8f3a83 DB |
5775 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
5776 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 5777 | */ |
f4d7e40a AS |
5778 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
5779 | struct bpf_func_state *state) | |
969bf05e | 5780 | { |
58e2af8b | 5781 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
5782 | int i; |
5783 | ||
5784 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 5785 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 5786 | mark_reg_unknown(env, regs, i); |
969bf05e | 5787 | |
f3709f69 JS |
5788 | bpf_for_each_spilled_reg(i, state, reg) { |
5789 | if (!reg) | |
969bf05e | 5790 | continue; |
de8f3a83 | 5791 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 5792 | __mark_reg_unknown(env, reg); |
969bf05e AS |
5793 | } |
5794 | } | |
5795 | ||
f4d7e40a AS |
5796 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
5797 | { | |
5798 | struct bpf_verifier_state *vstate = env->cur_state; | |
5799 | int i; | |
5800 | ||
5801 | for (i = 0; i <= vstate->curframe; i++) | |
5802 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
5803 | } | |
5804 | ||
6d94e741 AS |
5805 | enum { |
5806 | AT_PKT_END = -1, | |
5807 | BEYOND_PKT_END = -2, | |
5808 | }; | |
5809 | ||
5810 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
5811 | { | |
5812 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5813 | struct bpf_reg_state *reg = &state->regs[regn]; | |
5814 | ||
5815 | if (reg->type != PTR_TO_PACKET) | |
5816 | /* PTR_TO_PACKET_META is not supported yet */ | |
5817 | return; | |
5818 | ||
5819 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
5820 | * How far beyond pkt_end it goes is unknown. | |
5821 | * if (!range_open) it's the case of pkt >= pkt_end | |
5822 | * if (range_open) it's the case of pkt > pkt_end | |
5823 | * hence this pointer is at least 1 byte bigger than pkt_end | |
5824 | */ | |
5825 | if (range_open) | |
5826 | reg->range = BEYOND_PKT_END; | |
5827 | else | |
5828 | reg->range = AT_PKT_END; | |
5829 | } | |
5830 | ||
fd978bf7 | 5831 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
5832 | struct bpf_func_state *state, |
5833 | int ref_obj_id) | |
fd978bf7 JS |
5834 | { |
5835 | struct bpf_reg_state *regs = state->regs, *reg; | |
5836 | int i; | |
5837 | ||
5838 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 5839 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
5840 | mark_reg_unknown(env, regs, i); |
5841 | ||
5842 | bpf_for_each_spilled_reg(i, state, reg) { | |
5843 | if (!reg) | |
5844 | continue; | |
1b986589 | 5845 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 5846 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
5847 | } |
5848 | } | |
5849 | ||
5850 | /* The pointer with the specified id has released its reference to kernel | |
5851 | * resources. Identify all copies of the same pointer and clear the reference. | |
5852 | */ | |
5853 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 5854 | int ref_obj_id) |
fd978bf7 JS |
5855 | { |
5856 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 5857 | int err; |
fd978bf7 JS |
5858 | int i; |
5859 | ||
1b986589 MKL |
5860 | err = release_reference_state(cur_func(env), ref_obj_id); |
5861 | if (err) | |
5862 | return err; | |
5863 | ||
fd978bf7 | 5864 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 5865 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 5866 | |
1b986589 | 5867 | return 0; |
fd978bf7 JS |
5868 | } |
5869 | ||
51c39bb1 AS |
5870 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
5871 | struct bpf_reg_state *regs) | |
5872 | { | |
5873 | int i; | |
5874 | ||
5875 | /* after the call registers r0 - r5 were scratched */ | |
5876 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
5877 | mark_reg_not_init(env, regs, caller_saved[i]); | |
5878 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
5879 | } | |
5880 | } | |
5881 | ||
14351375 YS |
5882 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
5883 | struct bpf_func_state *caller, | |
5884 | struct bpf_func_state *callee, | |
5885 | int insn_idx); | |
5886 | ||
5887 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
5888 | int *insn_idx, int subprog, | |
5889 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
5890 | { |
5891 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 5892 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 5893 | struct bpf_func_state *caller, *callee; |
14351375 | 5894 | int err; |
51c39bb1 | 5895 | bool is_global = false; |
f4d7e40a | 5896 | |
aada9ce6 | 5897 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 5898 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 5899 | state->curframe + 2); |
f4d7e40a AS |
5900 | return -E2BIG; |
5901 | } | |
5902 | ||
f4d7e40a AS |
5903 | caller = state->frame[state->curframe]; |
5904 | if (state->frame[state->curframe + 1]) { | |
5905 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
5906 | state->curframe + 1); | |
5907 | return -EFAULT; | |
5908 | } | |
5909 | ||
51c39bb1 AS |
5910 | func_info_aux = env->prog->aux->func_info_aux; |
5911 | if (func_info_aux) | |
5912 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
34747c41 | 5913 | err = btf_check_subprog_arg_match(env, subprog, caller->regs); |
51c39bb1 AS |
5914 | if (err == -EFAULT) |
5915 | return err; | |
5916 | if (is_global) { | |
5917 | if (err) { | |
5918 | verbose(env, "Caller passes invalid args into func#%d\n", | |
5919 | subprog); | |
5920 | return err; | |
5921 | } else { | |
5922 | if (env->log.level & BPF_LOG_LEVEL) | |
5923 | verbose(env, | |
5924 | "Func#%d is global and valid. Skipping.\n", | |
5925 | subprog); | |
5926 | clear_caller_saved_regs(env, caller->regs); | |
5927 | ||
45159b27 | 5928 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 5929 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 5930 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
5931 | |
5932 | /* continue with next insn after call */ | |
5933 | return 0; | |
5934 | } | |
5935 | } | |
5936 | ||
bfc6bb74 AS |
5937 | if (insn->code == (BPF_JMP | BPF_CALL) && |
5938 | insn->imm == BPF_FUNC_timer_set_callback) { | |
5939 | struct bpf_verifier_state *async_cb; | |
5940 | ||
5941 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 5942 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
5943 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
5944 | *insn_idx, subprog); | |
5945 | if (!async_cb) | |
5946 | return -EFAULT; | |
5947 | callee = async_cb->frame[0]; | |
5948 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
5949 | ||
5950 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
5951 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
5952 | if (err) | |
5953 | return err; | |
5954 | ||
5955 | clear_caller_saved_regs(env, caller->regs); | |
5956 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
5957 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
5958 | /* continue with next insn after call */ | |
5959 | return 0; | |
5960 | } | |
5961 | ||
f4d7e40a AS |
5962 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
5963 | if (!callee) | |
5964 | return -ENOMEM; | |
5965 | state->frame[state->curframe + 1] = callee; | |
5966 | ||
5967 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
5968 | * into its own stack before reading from it. | |
5969 | * callee can read/write into caller's stack | |
5970 | */ | |
5971 | init_func_state(env, callee, | |
5972 | /* remember the callsite, it will be used by bpf_exit */ | |
5973 | *insn_idx /* callsite */, | |
5974 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 5975 | subprog /* subprog number within this prog */); |
f4d7e40a | 5976 | |
fd978bf7 | 5977 | /* Transfer references to the callee */ |
c69431aa | 5978 | err = copy_reference_state(callee, caller); |
fd978bf7 JS |
5979 | if (err) |
5980 | return err; | |
5981 | ||
14351375 YS |
5982 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
5983 | if (err) | |
5984 | return err; | |
f4d7e40a | 5985 | |
51c39bb1 | 5986 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
5987 | |
5988 | /* only increment it after check_reg_arg() finished */ | |
5989 | state->curframe++; | |
5990 | ||
5991 | /* and go analyze first insn of the callee */ | |
14351375 | 5992 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 5993 | |
06ee7115 | 5994 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
5995 | verbose(env, "caller:\n"); |
5996 | print_verifier_state(env, caller); | |
5997 | verbose(env, "callee:\n"); | |
5998 | print_verifier_state(env, callee); | |
5999 | } | |
6000 | return 0; | |
6001 | } | |
6002 | ||
314ee05e YS |
6003 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
6004 | struct bpf_func_state *caller, | |
6005 | struct bpf_func_state *callee) | |
6006 | { | |
6007 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
6008 | * void *callback_ctx, u64 flags); | |
6009 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
6010 | * void *callback_ctx); | |
6011 | */ | |
6012 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6013 | ||
6014 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6015 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6016 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6017 | ||
6018 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6019 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6020 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6021 | ||
6022 | /* pointer to stack or null */ | |
6023 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
6024 | ||
6025 | /* unused */ | |
6026 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6027 | return 0; | |
6028 | } | |
6029 | ||
14351375 YS |
6030 | static int set_callee_state(struct bpf_verifier_env *env, |
6031 | struct bpf_func_state *caller, | |
6032 | struct bpf_func_state *callee, int insn_idx) | |
6033 | { | |
6034 | int i; | |
6035 | ||
6036 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
6037 | * pointers, which connects us up to the liveness chain | |
6038 | */ | |
6039 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
6040 | callee->regs[i] = caller->regs[i]; | |
6041 | return 0; | |
6042 | } | |
6043 | ||
6044 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
6045 | int *insn_idx) | |
6046 | { | |
6047 | int subprog, target_insn; | |
6048 | ||
6049 | target_insn = *insn_idx + insn->imm + 1; | |
6050 | subprog = find_subprog(env, target_insn); | |
6051 | if (subprog < 0) { | |
6052 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
6053 | target_insn); | |
6054 | return -EFAULT; | |
6055 | } | |
6056 | ||
6057 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
6058 | } | |
6059 | ||
69c087ba YS |
6060 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
6061 | struct bpf_func_state *caller, | |
6062 | struct bpf_func_state *callee, | |
6063 | int insn_idx) | |
6064 | { | |
6065 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
6066 | struct bpf_map *map; | |
6067 | int err; | |
6068 | ||
6069 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
6070 | verbose(env, "tail_call abusing map_ptr\n"); | |
6071 | return -EINVAL; | |
6072 | } | |
6073 | ||
6074 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
6075 | if (!map->ops->map_set_for_each_callback_args || | |
6076 | !map->ops->map_for_each_callback) { | |
6077 | verbose(env, "callback function not allowed for map\n"); | |
6078 | return -ENOTSUPP; | |
6079 | } | |
6080 | ||
6081 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
6082 | if (err) | |
6083 | return err; | |
6084 | ||
6085 | callee->in_callback_fn = true; | |
6086 | return 0; | |
6087 | } | |
6088 | ||
b00628b1 AS |
6089 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
6090 | struct bpf_func_state *caller, | |
6091 | struct bpf_func_state *callee, | |
6092 | int insn_idx) | |
6093 | { | |
6094 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6095 | ||
6096 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
6097 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
6098 | */ | |
6099 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
6100 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
6101 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
6102 | ||
6103 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6104 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6105 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
6106 | ||
6107 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6108 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6109 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
6110 | ||
6111 | /* unused */ | |
6112 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6113 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 6114 | callee->in_async_callback_fn = true; |
b00628b1 AS |
6115 | return 0; |
6116 | } | |
6117 | ||
f4d7e40a AS |
6118 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
6119 | { | |
6120 | struct bpf_verifier_state *state = env->cur_state; | |
6121 | struct bpf_func_state *caller, *callee; | |
6122 | struct bpf_reg_state *r0; | |
fd978bf7 | 6123 | int err; |
f4d7e40a AS |
6124 | |
6125 | callee = state->frame[state->curframe]; | |
6126 | r0 = &callee->regs[BPF_REG_0]; | |
6127 | if (r0->type == PTR_TO_STACK) { | |
6128 | /* technically it's ok to return caller's stack pointer | |
6129 | * (or caller's caller's pointer) back to the caller, | |
6130 | * since these pointers are valid. Only current stack | |
6131 | * pointer will be invalid as soon as function exits, | |
6132 | * but let's be conservative | |
6133 | */ | |
6134 | verbose(env, "cannot return stack pointer to the caller\n"); | |
6135 | return -EINVAL; | |
6136 | } | |
6137 | ||
6138 | state->curframe--; | |
6139 | caller = state->frame[state->curframe]; | |
69c087ba YS |
6140 | if (callee->in_callback_fn) { |
6141 | /* enforce R0 return value range [0, 1]. */ | |
6142 | struct tnum range = tnum_range(0, 1); | |
6143 | ||
6144 | if (r0->type != SCALAR_VALUE) { | |
6145 | verbose(env, "R0 not a scalar value\n"); | |
6146 | return -EACCES; | |
6147 | } | |
6148 | if (!tnum_in(range, r0->var_off)) { | |
6149 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
6150 | return -EINVAL; | |
6151 | } | |
6152 | } else { | |
6153 | /* return to the caller whatever r0 had in the callee */ | |
6154 | caller->regs[BPF_REG_0] = *r0; | |
6155 | } | |
f4d7e40a | 6156 | |
fd978bf7 | 6157 | /* Transfer references to the caller */ |
c69431aa | 6158 | err = copy_reference_state(caller, callee); |
fd978bf7 JS |
6159 | if (err) |
6160 | return err; | |
6161 | ||
f4d7e40a | 6162 | *insn_idx = callee->callsite + 1; |
06ee7115 | 6163 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
6164 | verbose(env, "returning from callee:\n"); |
6165 | print_verifier_state(env, callee); | |
6166 | verbose(env, "to caller at %d:\n", *insn_idx); | |
6167 | print_verifier_state(env, caller); | |
6168 | } | |
6169 | /* clear everything in the callee */ | |
6170 | free_func_state(callee); | |
6171 | state->frame[state->curframe + 1] = NULL; | |
6172 | return 0; | |
6173 | } | |
6174 | ||
849fa506 YS |
6175 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
6176 | int func_id, | |
6177 | struct bpf_call_arg_meta *meta) | |
6178 | { | |
6179 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
6180 | ||
6181 | if (ret_type != RET_INTEGER || | |
6182 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 6183 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
6184 | func_id != BPF_FUNC_probe_read_str && |
6185 | func_id != BPF_FUNC_probe_read_kernel_str && | |
6186 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
6187 | return; |
6188 | ||
10060503 | 6189 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 6190 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
6191 | ret_reg->smin_value = -MAX_ERRNO; |
6192 | ret_reg->s32_min_value = -MAX_ERRNO; | |
849fa506 YS |
6193 | __reg_deduce_bounds(ret_reg); |
6194 | __reg_bound_offset(ret_reg); | |
10060503 | 6195 | __update_reg_bounds(ret_reg); |
849fa506 YS |
6196 | } |
6197 | ||
c93552c4 DB |
6198 | static int |
6199 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
6200 | int func_id, int insn_idx) | |
6201 | { | |
6202 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 6203 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
6204 | |
6205 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
6206 | func_id != BPF_FUNC_map_lookup_elem && |
6207 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
6208 | func_id != BPF_FUNC_map_delete_elem && |
6209 | func_id != BPF_FUNC_map_push_elem && | |
6210 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 6211 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f BT |
6212 | func_id != BPF_FUNC_for_each_map_elem && |
6213 | func_id != BPF_FUNC_redirect_map) | |
c93552c4 | 6214 | return 0; |
09772d92 | 6215 | |
591fe988 | 6216 | if (map == NULL) { |
c93552c4 DB |
6217 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
6218 | return -EINVAL; | |
6219 | } | |
6220 | ||
591fe988 DB |
6221 | /* In case of read-only, some additional restrictions |
6222 | * need to be applied in order to prevent altering the | |
6223 | * state of the map from program side. | |
6224 | */ | |
6225 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
6226 | (func_id == BPF_FUNC_map_delete_elem || | |
6227 | func_id == BPF_FUNC_map_update_elem || | |
6228 | func_id == BPF_FUNC_map_push_elem || | |
6229 | func_id == BPF_FUNC_map_pop_elem)) { | |
6230 | verbose(env, "write into map forbidden\n"); | |
6231 | return -EACCES; | |
6232 | } | |
6233 | ||
d2e4c1e6 | 6234 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 6235 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 6236 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 6237 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 6238 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 6239 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
6240 | return 0; |
6241 | } | |
6242 | ||
d2e4c1e6 DB |
6243 | static int |
6244 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
6245 | int func_id, int insn_idx) | |
6246 | { | |
6247 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
6248 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
6249 | struct bpf_map *map = meta->map_ptr; | |
6250 | struct tnum range; | |
6251 | u64 val; | |
cc52d914 | 6252 | int err; |
d2e4c1e6 DB |
6253 | |
6254 | if (func_id != BPF_FUNC_tail_call) | |
6255 | return 0; | |
6256 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
6257 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
6258 | return -EINVAL; | |
6259 | } | |
6260 | ||
6261 | range = tnum_range(0, map->max_entries - 1); | |
6262 | reg = ®s[BPF_REG_3]; | |
6263 | ||
6264 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
6265 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
6266 | return 0; | |
6267 | } | |
6268 | ||
cc52d914 DB |
6269 | err = mark_chain_precision(env, BPF_REG_3); |
6270 | if (err) | |
6271 | return err; | |
6272 | ||
d2e4c1e6 DB |
6273 | val = reg->var_off.value; |
6274 | if (bpf_map_key_unseen(aux)) | |
6275 | bpf_map_key_store(aux, val); | |
6276 | else if (!bpf_map_key_poisoned(aux) && | |
6277 | bpf_map_key_immediate(aux) != val) | |
6278 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
6279 | return 0; | |
6280 | } | |
6281 | ||
fd978bf7 JS |
6282 | static int check_reference_leak(struct bpf_verifier_env *env) |
6283 | { | |
6284 | struct bpf_func_state *state = cur_func(env); | |
6285 | int i; | |
6286 | ||
6287 | for (i = 0; i < state->acquired_refs; i++) { | |
6288 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
6289 | state->refs[i].id, state->refs[i].insn_idx); | |
6290 | } | |
6291 | return state->acquired_refs ? -EINVAL : 0; | |
6292 | } | |
6293 | ||
7b15523a FR |
6294 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
6295 | struct bpf_reg_state *regs) | |
6296 | { | |
6297 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
6298 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
6299 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
6300 | int err, fmt_map_off, num_args; | |
6301 | u64 fmt_addr; | |
6302 | char *fmt; | |
6303 | ||
6304 | /* data must be an array of u64 */ | |
6305 | if (data_len_reg->var_off.value % 8) | |
6306 | return -EINVAL; | |
6307 | num_args = data_len_reg->var_off.value / 8; | |
6308 | ||
6309 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
6310 | * and map_direct_value_addr is set. | |
6311 | */ | |
6312 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
6313 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
6314 | fmt_map_off); | |
8e8ee109 FR |
6315 | if (err) { |
6316 | verbose(env, "verifier bug\n"); | |
6317 | return -EFAULT; | |
6318 | } | |
7b15523a FR |
6319 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
6320 | ||
6321 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
6322 | * can focus on validating the format specifiers. | |
6323 | */ | |
48cac3f4 | 6324 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
7b15523a FR |
6325 | if (err < 0) |
6326 | verbose(env, "Invalid format string\n"); | |
6327 | ||
6328 | return err; | |
6329 | } | |
6330 | ||
9b99edca JO |
6331 | static int check_get_func_ip(struct bpf_verifier_env *env) |
6332 | { | |
6333 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
6334 | enum bpf_prog_type type = resolve_prog_type(env->prog); | |
6335 | int func_id = BPF_FUNC_get_func_ip; | |
6336 | ||
6337 | if (type == BPF_PROG_TYPE_TRACING) { | |
6338 | if (eatype != BPF_TRACE_FENTRY && eatype != BPF_TRACE_FEXIT && | |
6339 | eatype != BPF_MODIFY_RETURN) { | |
6340 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", | |
6341 | func_id_name(func_id), func_id); | |
6342 | return -ENOTSUPP; | |
6343 | } | |
6344 | return 0; | |
9ffd9f3f JO |
6345 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
6346 | return 0; | |
9b99edca JO |
6347 | } |
6348 | ||
6349 | verbose(env, "func %s#%d not supported for program type %d\n", | |
6350 | func_id_name(func_id), func_id, type); | |
6351 | return -ENOTSUPP; | |
6352 | } | |
6353 | ||
69c087ba YS |
6354 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
6355 | int *insn_idx_p) | |
17a52670 | 6356 | { |
17a52670 | 6357 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 6358 | struct bpf_reg_state *regs; |
33ff9823 | 6359 | struct bpf_call_arg_meta meta; |
69c087ba | 6360 | int insn_idx = *insn_idx_p; |
969bf05e | 6361 | bool changes_data; |
69c087ba | 6362 | int i, err, func_id; |
17a52670 AS |
6363 | |
6364 | /* find function prototype */ | |
69c087ba | 6365 | func_id = insn->imm; |
17a52670 | 6366 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
6367 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
6368 | func_id); | |
17a52670 AS |
6369 | return -EINVAL; |
6370 | } | |
6371 | ||
00176a34 | 6372 | if (env->ops->get_func_proto) |
5e43f899 | 6373 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 6374 | if (!fn) { |
61bd5218 JK |
6375 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
6376 | func_id); | |
17a52670 AS |
6377 | return -EINVAL; |
6378 | } | |
6379 | ||
6380 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 6381 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 6382 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
6383 | return -EINVAL; |
6384 | } | |
6385 | ||
eae2e83e JO |
6386 | if (fn->allowed && !fn->allowed(env->prog)) { |
6387 | verbose(env, "helper call is not allowed in probe\n"); | |
6388 | return -EINVAL; | |
6389 | } | |
6390 | ||
04514d13 | 6391 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 6392 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
6393 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
6394 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
6395 | func_id_name(func_id), func_id); | |
6396 | return -EINVAL; | |
6397 | } | |
969bf05e | 6398 | |
33ff9823 | 6399 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 6400 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 6401 | |
1b986589 | 6402 | err = check_func_proto(fn, func_id); |
435faee1 | 6403 | if (err) { |
61bd5218 | 6404 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 6405 | func_id_name(func_id), func_id); |
435faee1 DB |
6406 | return err; |
6407 | } | |
6408 | ||
d83525ca | 6409 | meta.func_id = func_id; |
17a52670 | 6410 | /* check args */ |
523a4cf4 | 6411 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
af7ec138 | 6412 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
6413 | if (err) |
6414 | return err; | |
6415 | } | |
17a52670 | 6416 | |
c93552c4 DB |
6417 | err = record_func_map(env, &meta, func_id, insn_idx); |
6418 | if (err) | |
6419 | return err; | |
6420 | ||
d2e4c1e6 DB |
6421 | err = record_func_key(env, &meta, func_id, insn_idx); |
6422 | if (err) | |
6423 | return err; | |
6424 | ||
435faee1 DB |
6425 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
6426 | * is inferred from register state. | |
6427 | */ | |
6428 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
6429 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
6430 | BPF_WRITE, -1, false); | |
435faee1 DB |
6431 | if (err) |
6432 | return err; | |
6433 | } | |
6434 | ||
fd978bf7 JS |
6435 | if (func_id == BPF_FUNC_tail_call) { |
6436 | err = check_reference_leak(env); | |
6437 | if (err) { | |
6438 | verbose(env, "tail_call would lead to reference leak\n"); | |
6439 | return err; | |
6440 | } | |
6441 | } else if (is_release_function(func_id)) { | |
1b986589 | 6442 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
6443 | if (err) { |
6444 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
6445 | func_id_name(func_id), func_id); | |
fd978bf7 | 6446 | return err; |
46f8bc92 | 6447 | } |
fd978bf7 JS |
6448 | } |
6449 | ||
638f5b90 | 6450 | regs = cur_regs(env); |
cd339431 RG |
6451 | |
6452 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
6453 | * this is required because get_local_storage() can't return an error. | |
6454 | */ | |
6455 | if (func_id == BPF_FUNC_get_local_storage && | |
6456 | !register_is_null(®s[BPF_REG_2])) { | |
6457 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
6458 | return -EINVAL; | |
6459 | } | |
6460 | ||
69c087ba YS |
6461 | if (func_id == BPF_FUNC_for_each_map_elem) { |
6462 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
6463 | set_map_elem_callback_state); | |
6464 | if (err < 0) | |
6465 | return -EINVAL; | |
6466 | } | |
6467 | ||
b00628b1 AS |
6468 | if (func_id == BPF_FUNC_timer_set_callback) { |
6469 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
6470 | set_timer_callback_state); | |
6471 | if (err < 0) | |
6472 | return -EINVAL; | |
6473 | } | |
6474 | ||
7b15523a FR |
6475 | if (func_id == BPF_FUNC_snprintf) { |
6476 | err = check_bpf_snprintf_call(env, regs); | |
6477 | if (err < 0) | |
6478 | return err; | |
6479 | } | |
6480 | ||
17a52670 | 6481 | /* reset caller saved regs */ |
dc503a8a | 6482 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 6483 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
6484 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
6485 | } | |
17a52670 | 6486 | |
5327ed3d JW |
6487 | /* helper call returns 64-bit value. */ |
6488 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
6489 | ||
dc503a8a | 6490 | /* update return register (already marked as written above) */ |
17a52670 | 6491 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 6492 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 6493 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
6494 | } else if (fn->ret_type == RET_VOID) { |
6495 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
6496 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
6497 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 6498 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 6499 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
6500 | /* remember map_ptr, so that check_map_access() |
6501 | * can check 'value_size' boundary of memory access | |
6502 | * to map element returned from bpf_map_lookup_elem() | |
6503 | */ | |
33ff9823 | 6504 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
6505 | verbose(env, |
6506 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
6507 | return -EINVAL; |
6508 | } | |
33ff9823 | 6509 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 6510 | regs[BPF_REG_0].map_uid = meta.map_uid; |
4d31f301 DB |
6511 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
6512 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
6513 | if (map_value_has_spin_lock(meta.map_ptr)) |
6514 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
6515 | } else { |
6516 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
4d31f301 | 6517 | } |
c64b7983 JS |
6518 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
6519 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6520 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
85a51f8c LB |
6521 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
6522 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6523 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
655a51e5 MKL |
6524 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
6525 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6526 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
457f4436 AN |
6527 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
6528 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6529 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; | |
457f4436 | 6530 | regs[BPF_REG_0].mem_size = meta.mem_size; |
63d9b80d HL |
6531 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
6532 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { | |
eaa6bcb7 HL |
6533 | const struct btf_type *t; |
6534 | ||
6535 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 6536 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
6537 | if (!btf_type_is_struct(t)) { |
6538 | u32 tsize; | |
6539 | const struct btf_type *ret; | |
6540 | const char *tname; | |
6541 | ||
6542 | /* resolve the type size of ksym. */ | |
22dc4a0f | 6543 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 6544 | if (IS_ERR(ret)) { |
22dc4a0f | 6545 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
6546 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
6547 | tname, PTR_ERR(ret)); | |
6548 | return -EINVAL; | |
6549 | } | |
63d9b80d HL |
6550 | regs[BPF_REG_0].type = |
6551 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
6552 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; | |
eaa6bcb7 HL |
6553 | regs[BPF_REG_0].mem_size = tsize; |
6554 | } else { | |
63d9b80d HL |
6555 | regs[BPF_REG_0].type = |
6556 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
6557 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; | |
22dc4a0f | 6558 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
6559 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
6560 | } | |
3ca1032a KS |
6561 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL || |
6562 | fn->ret_type == RET_PTR_TO_BTF_ID) { | |
af7ec138 YS |
6563 | int ret_btf_id; |
6564 | ||
6565 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
3ca1032a KS |
6566 | regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ? |
6567 | PTR_TO_BTF_ID : | |
6568 | PTR_TO_BTF_ID_OR_NULL; | |
af7ec138 YS |
6569 | ret_btf_id = *fn->ret_btf_id; |
6570 | if (ret_btf_id == 0) { | |
6571 | verbose(env, "invalid return type %d of func %s#%d\n", | |
6572 | fn->ret_type, func_id_name(func_id), func_id); | |
6573 | return -EINVAL; | |
6574 | } | |
22dc4a0f AN |
6575 | /* current BPF helper definitions are only coming from |
6576 | * built-in code with type IDs from vmlinux BTF | |
6577 | */ | |
6578 | regs[BPF_REG_0].btf = btf_vmlinux; | |
af7ec138 | 6579 | regs[BPF_REG_0].btf_id = ret_btf_id; |
17a52670 | 6580 | } else { |
61bd5218 | 6581 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 6582 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
6583 | return -EINVAL; |
6584 | } | |
04fd61ab | 6585 | |
93c230e3 MKL |
6586 | if (reg_type_may_be_null(regs[BPF_REG_0].type)) |
6587 | regs[BPF_REG_0].id = ++env->id_gen; | |
6588 | ||
0f3adc28 | 6589 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
6590 | /* For release_reference() */ |
6591 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 6592 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
6593 | int id = acquire_reference_state(env, insn_idx); |
6594 | ||
6595 | if (id < 0) | |
6596 | return id; | |
6597 | /* For mark_ptr_or_null_reg() */ | |
6598 | regs[BPF_REG_0].id = id; | |
6599 | /* For release_reference() */ | |
6600 | regs[BPF_REG_0].ref_obj_id = id; | |
6601 | } | |
1b986589 | 6602 | |
849fa506 YS |
6603 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
6604 | ||
61bd5218 | 6605 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
6606 | if (err) |
6607 | return err; | |
04fd61ab | 6608 | |
fa28dcb8 SL |
6609 | if ((func_id == BPF_FUNC_get_stack || |
6610 | func_id == BPF_FUNC_get_task_stack) && | |
6611 | !env->prog->has_callchain_buf) { | |
c195651e YS |
6612 | const char *err_str; |
6613 | ||
6614 | #ifdef CONFIG_PERF_EVENTS | |
6615 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
6616 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
6617 | #else | |
6618 | err = -ENOTSUPP; | |
6619 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
6620 | #endif | |
6621 | if (err) { | |
6622 | verbose(env, err_str, func_id_name(func_id), func_id); | |
6623 | return err; | |
6624 | } | |
6625 | ||
6626 | env->prog->has_callchain_buf = true; | |
6627 | } | |
6628 | ||
5d99cb2c SL |
6629 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
6630 | env->prog->call_get_stack = true; | |
6631 | ||
9b99edca JO |
6632 | if (func_id == BPF_FUNC_get_func_ip) { |
6633 | if (check_get_func_ip(env)) | |
6634 | return -ENOTSUPP; | |
6635 | env->prog->call_get_func_ip = true; | |
6636 | } | |
6637 | ||
969bf05e AS |
6638 | if (changes_data) |
6639 | clear_all_pkt_pointers(env); | |
6640 | return 0; | |
6641 | } | |
6642 | ||
e6ac2450 MKL |
6643 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
6644 | * the BTF func_proto's return value size and argument. | |
6645 | */ | |
6646 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
6647 | size_t reg_size) | |
6648 | { | |
6649 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
6650 | ||
6651 | if (regno == BPF_REG_0) { | |
6652 | /* Function return value */ | |
6653 | reg->live |= REG_LIVE_WRITTEN; | |
6654 | reg->subreg_def = reg_size == sizeof(u64) ? | |
6655 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
6656 | } else { | |
6657 | /* Function argument */ | |
6658 | if (reg_size == sizeof(u64)) { | |
6659 | mark_insn_zext(env, reg); | |
6660 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
6661 | } else { | |
6662 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
6663 | } | |
6664 | } | |
6665 | } | |
6666 | ||
6667 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
6668 | { | |
6669 | const struct btf_type *t, *func, *func_proto, *ptr_type; | |
6670 | struct bpf_reg_state *regs = cur_regs(env); | |
6671 | const char *func_name, *ptr_type_name; | |
6672 | u32 i, nargs, func_id, ptr_type_id; | |
2357672c | 6673 | struct module *btf_mod = NULL; |
e6ac2450 | 6674 | const struct btf_param *args; |
2357672c | 6675 | struct btf *desc_btf; |
e6ac2450 MKL |
6676 | int err; |
6677 | ||
a5d82727 KKD |
6678 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
6679 | if (!insn->imm) | |
6680 | return 0; | |
6681 | ||
2357672c KKD |
6682 | desc_btf = find_kfunc_desc_btf(env, insn->imm, insn->off, &btf_mod); |
6683 | if (IS_ERR(desc_btf)) | |
6684 | return PTR_ERR(desc_btf); | |
6685 | ||
e6ac2450 | 6686 | func_id = insn->imm; |
2357672c KKD |
6687 | func = btf_type_by_id(desc_btf, func_id); |
6688 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
6689 | func_proto = btf_type_by_id(desc_btf, func->type); | |
e6ac2450 MKL |
6690 | |
6691 | if (!env->ops->check_kfunc_call || | |
2357672c | 6692 | !env->ops->check_kfunc_call(func_id, btf_mod)) { |
e6ac2450 MKL |
6693 | verbose(env, "calling kernel function %s is not allowed\n", |
6694 | func_name); | |
6695 | return -EACCES; | |
6696 | } | |
6697 | ||
6698 | /* Check the arguments */ | |
2357672c | 6699 | err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs); |
e6ac2450 MKL |
6700 | if (err) |
6701 | return err; | |
6702 | ||
6703 | for (i = 0; i < CALLER_SAVED_REGS; i++) | |
6704 | mark_reg_not_init(env, regs, caller_saved[i]); | |
6705 | ||
6706 | /* Check return type */ | |
2357672c | 6707 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
e6ac2450 MKL |
6708 | if (btf_type_is_scalar(t)) { |
6709 | mark_reg_unknown(env, regs, BPF_REG_0); | |
6710 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
6711 | } else if (btf_type_is_ptr(t)) { | |
2357672c | 6712 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, |
e6ac2450 MKL |
6713 | &ptr_type_id); |
6714 | if (!btf_type_is_struct(ptr_type)) { | |
2357672c | 6715 | ptr_type_name = btf_name_by_offset(desc_btf, |
e6ac2450 MKL |
6716 | ptr_type->name_off); |
6717 | verbose(env, "kernel function %s returns pointer type %s %s is not supported\n", | |
6718 | func_name, btf_type_str(ptr_type), | |
6719 | ptr_type_name); | |
6720 | return -EINVAL; | |
6721 | } | |
6722 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
2357672c | 6723 | regs[BPF_REG_0].btf = desc_btf; |
e6ac2450 MKL |
6724 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; |
6725 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
6726 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); | |
6727 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ | |
6728 | ||
6729 | nargs = btf_type_vlen(func_proto); | |
6730 | args = (const struct btf_param *)(func_proto + 1); | |
6731 | for (i = 0; i < nargs; i++) { | |
6732 | u32 regno = i + 1; | |
6733 | ||
2357672c | 6734 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
6735 | if (btf_type_is_ptr(t)) |
6736 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
6737 | else | |
6738 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
6739 | mark_btf_func_reg_size(env, regno, t->size); | |
6740 | } | |
6741 | ||
6742 | return 0; | |
6743 | } | |
6744 | ||
b03c9f9f EC |
6745 | static bool signed_add_overflows(s64 a, s64 b) |
6746 | { | |
6747 | /* Do the add in u64, where overflow is well-defined */ | |
6748 | s64 res = (s64)((u64)a + (u64)b); | |
6749 | ||
6750 | if (b < 0) | |
6751 | return res > a; | |
6752 | return res < a; | |
6753 | } | |
6754 | ||
bc895e8b | 6755 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
6756 | { |
6757 | /* Do the add in u32, where overflow is well-defined */ | |
6758 | s32 res = (s32)((u32)a + (u32)b); | |
6759 | ||
6760 | if (b < 0) | |
6761 | return res > a; | |
6762 | return res < a; | |
6763 | } | |
6764 | ||
bc895e8b | 6765 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
6766 | { |
6767 | /* Do the sub in u64, where overflow is well-defined */ | |
6768 | s64 res = (s64)((u64)a - (u64)b); | |
6769 | ||
6770 | if (b < 0) | |
6771 | return res < a; | |
6772 | return res > a; | |
969bf05e AS |
6773 | } |
6774 | ||
3f50f132 JF |
6775 | static bool signed_sub32_overflows(s32 a, s32 b) |
6776 | { | |
bc895e8b | 6777 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
6778 | s32 res = (s32)((u32)a - (u32)b); |
6779 | ||
6780 | if (b < 0) | |
6781 | return res < a; | |
6782 | return res > a; | |
6783 | } | |
6784 | ||
bb7f0f98 AS |
6785 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
6786 | const struct bpf_reg_state *reg, | |
6787 | enum bpf_reg_type type) | |
6788 | { | |
6789 | bool known = tnum_is_const(reg->var_off); | |
6790 | s64 val = reg->var_off.value; | |
6791 | s64 smin = reg->smin_value; | |
6792 | ||
6793 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
6794 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
6795 | reg_type_str[type], val); | |
6796 | return false; | |
6797 | } | |
6798 | ||
6799 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
6800 | verbose(env, "%s pointer offset %d is not allowed\n", | |
6801 | reg_type_str[type], reg->off); | |
6802 | return false; | |
6803 | } | |
6804 | ||
6805 | if (smin == S64_MIN) { | |
6806 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
6807 | reg_type_str[type]); | |
6808 | return false; | |
6809 | } | |
6810 | ||
6811 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
6812 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
6813 | smin, reg_type_str[type]); | |
6814 | return false; | |
6815 | } | |
6816 | ||
6817 | return true; | |
6818 | } | |
6819 | ||
979d63d5 DB |
6820 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
6821 | { | |
6822 | return &env->insn_aux_data[env->insn_idx]; | |
6823 | } | |
6824 | ||
a6aaece0 DB |
6825 | enum { |
6826 | REASON_BOUNDS = -1, | |
6827 | REASON_TYPE = -2, | |
6828 | REASON_PATHS = -3, | |
6829 | REASON_LIMIT = -4, | |
6830 | REASON_STACK = -5, | |
6831 | }; | |
6832 | ||
979d63d5 | 6833 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 6834 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 6835 | { |
7fedb63a | 6836 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
6837 | |
6838 | switch (ptr_reg->type) { | |
6839 | case PTR_TO_STACK: | |
1b1597e6 | 6840 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
6841 | * left direction, see BPF_REG_FP. Also, unknown scalar |
6842 | * offset where we would need to deal with min/max bounds is | |
6843 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
6844 | */ |
6845 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 6846 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 6847 | break; |
979d63d5 | 6848 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 6849 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
6850 | ptr_limit = (mask_to_left ? |
6851 | ptr_reg->smin_value : | |
6852 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 6853 | break; |
979d63d5 | 6854 | default: |
a6aaece0 | 6855 | return REASON_TYPE; |
979d63d5 | 6856 | } |
b658bbb8 DB |
6857 | |
6858 | if (ptr_limit >= max) | |
a6aaece0 | 6859 | return REASON_LIMIT; |
b658bbb8 DB |
6860 | *alu_limit = ptr_limit; |
6861 | return 0; | |
979d63d5 DB |
6862 | } |
6863 | ||
d3bd7413 DB |
6864 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
6865 | const struct bpf_insn *insn) | |
6866 | { | |
2c78ee89 | 6867 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
6868 | } |
6869 | ||
6870 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
6871 | u32 alu_state, u32 alu_limit) | |
6872 | { | |
6873 | /* If we arrived here from different branches with different | |
6874 | * state or limits to sanitize, then this won't work. | |
6875 | */ | |
6876 | if (aux->alu_state && | |
6877 | (aux->alu_state != alu_state || | |
6878 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 6879 | return REASON_PATHS; |
d3bd7413 | 6880 | |
e6ac5933 | 6881 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
6882 | aux->alu_state = alu_state; |
6883 | aux->alu_limit = alu_limit; | |
6884 | return 0; | |
6885 | } | |
6886 | ||
6887 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
6888 | struct bpf_insn *insn) | |
6889 | { | |
6890 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
6891 | ||
6892 | if (can_skip_alu_sanitation(env, insn)) | |
6893 | return 0; | |
6894 | ||
6895 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
6896 | } | |
6897 | ||
f5288193 DB |
6898 | static bool sanitize_needed(u8 opcode) |
6899 | { | |
6900 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
6901 | } | |
6902 | ||
3d0220f6 DB |
6903 | struct bpf_sanitize_info { |
6904 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 6905 | bool mask_to_left; |
3d0220f6 DB |
6906 | }; |
6907 | ||
9183671a DB |
6908 | static struct bpf_verifier_state * |
6909 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
6910 | const struct bpf_insn *insn, | |
6911 | u32 next_idx, u32 curr_idx) | |
6912 | { | |
6913 | struct bpf_verifier_state *branch; | |
6914 | struct bpf_reg_state *regs; | |
6915 | ||
6916 | branch = push_stack(env, next_idx, curr_idx, true); | |
6917 | if (branch && insn) { | |
6918 | regs = branch->frame[branch->curframe]->regs; | |
6919 | if (BPF_SRC(insn->code) == BPF_K) { | |
6920 | mark_reg_unknown(env, regs, insn->dst_reg); | |
6921 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
6922 | mark_reg_unknown(env, regs, insn->dst_reg); | |
6923 | mark_reg_unknown(env, regs, insn->src_reg); | |
6924 | } | |
6925 | } | |
6926 | return branch; | |
6927 | } | |
6928 | ||
979d63d5 DB |
6929 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
6930 | struct bpf_insn *insn, | |
6931 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 6932 | const struct bpf_reg_state *off_reg, |
979d63d5 | 6933 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 6934 | struct bpf_sanitize_info *info, |
7fedb63a | 6935 | const bool commit_window) |
979d63d5 | 6936 | { |
3d0220f6 | 6937 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 6938 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 6939 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 6940 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
6941 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
6942 | u8 opcode = BPF_OP(insn->code); | |
6943 | u32 alu_state, alu_limit; | |
6944 | struct bpf_reg_state tmp; | |
6945 | bool ret; | |
f232326f | 6946 | int err; |
979d63d5 | 6947 | |
d3bd7413 | 6948 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
6949 | return 0; |
6950 | ||
6951 | /* We already marked aux for masking from non-speculative | |
6952 | * paths, thus we got here in the first place. We only care | |
6953 | * to explore bad access from here. | |
6954 | */ | |
6955 | if (vstate->speculative) | |
6956 | goto do_sim; | |
6957 | ||
bb01a1bb DB |
6958 | if (!commit_window) { |
6959 | if (!tnum_is_const(off_reg->var_off) && | |
6960 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
6961 | return REASON_BOUNDS; | |
6962 | ||
6963 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
6964 | (opcode == BPF_SUB && !off_is_neg); | |
6965 | } | |
6966 | ||
6967 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
6968 | if (err < 0) |
6969 | return err; | |
6970 | ||
7fedb63a DB |
6971 | if (commit_window) { |
6972 | /* In commit phase we narrow the masking window based on | |
6973 | * the observed pointer move after the simulated operation. | |
6974 | */ | |
3d0220f6 DB |
6975 | alu_state = info->aux.alu_state; |
6976 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
6977 | } else { |
6978 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 6979 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
6980 | alu_state |= ptr_is_dst_reg ? |
6981 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
6982 | |
6983 | /* Limit pruning on unknown scalars to enable deep search for | |
6984 | * potential masking differences from other program paths. | |
6985 | */ | |
6986 | if (!off_is_imm) | |
6987 | env->explore_alu_limits = true; | |
7fedb63a DB |
6988 | } |
6989 | ||
f232326f PK |
6990 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
6991 | if (err < 0) | |
6992 | return err; | |
979d63d5 | 6993 | do_sim: |
7fedb63a DB |
6994 | /* If we're in commit phase, we're done here given we already |
6995 | * pushed the truncated dst_reg into the speculative verification | |
6996 | * stack. | |
a7036191 DB |
6997 | * |
6998 | * Also, when register is a known constant, we rewrite register-based | |
6999 | * operation to immediate-based, and thus do not need masking (and as | |
7000 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 7001 | */ |
a7036191 | 7002 | if (commit_window || off_is_imm) |
7fedb63a DB |
7003 | return 0; |
7004 | ||
979d63d5 DB |
7005 | /* Simulate and find potential out-of-bounds access under |
7006 | * speculative execution from truncation as a result of | |
7007 | * masking when off was not within expected range. If off | |
7008 | * sits in dst, then we temporarily need to move ptr there | |
7009 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
7010 | * for cases where we use K-based arithmetic in one direction | |
7011 | * and truncated reg-based in the other in order to explore | |
7012 | * bad access. | |
7013 | */ | |
7014 | if (!ptr_is_dst_reg) { | |
7015 | tmp = *dst_reg; | |
7016 | *dst_reg = *ptr_reg; | |
7017 | } | |
9183671a DB |
7018 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
7019 | env->insn_idx); | |
0803278b | 7020 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 7021 | *dst_reg = tmp; |
a6aaece0 DB |
7022 | return !ret ? REASON_STACK : 0; |
7023 | } | |
7024 | ||
fe9a5ca7 DB |
7025 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
7026 | { | |
7027 | struct bpf_verifier_state *vstate = env->cur_state; | |
7028 | ||
7029 | /* If we simulate paths under speculation, we don't update the | |
7030 | * insn as 'seen' such that when we verify unreachable paths in | |
7031 | * the non-speculative domain, sanitize_dead_code() can still | |
7032 | * rewrite/sanitize them. | |
7033 | */ | |
7034 | if (!vstate->speculative) | |
7035 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
7036 | } | |
7037 | ||
a6aaece0 DB |
7038 | static int sanitize_err(struct bpf_verifier_env *env, |
7039 | const struct bpf_insn *insn, int reason, | |
7040 | const struct bpf_reg_state *off_reg, | |
7041 | const struct bpf_reg_state *dst_reg) | |
7042 | { | |
7043 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
7044 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
7045 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
7046 | ||
7047 | switch (reason) { | |
7048 | case REASON_BOUNDS: | |
7049 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
7050 | off_reg == dst_reg ? dst : src, err); | |
7051 | break; | |
7052 | case REASON_TYPE: | |
7053 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
7054 | off_reg == dst_reg ? src : dst, err); | |
7055 | break; | |
7056 | case REASON_PATHS: | |
7057 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
7058 | dst, op, err); | |
7059 | break; | |
7060 | case REASON_LIMIT: | |
7061 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
7062 | dst, op, err); | |
7063 | break; | |
7064 | case REASON_STACK: | |
7065 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
7066 | dst, err); | |
7067 | break; | |
7068 | default: | |
7069 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
7070 | reason); | |
7071 | break; | |
7072 | } | |
7073 | ||
7074 | return -EACCES; | |
979d63d5 DB |
7075 | } |
7076 | ||
01f810ac AM |
7077 | /* check that stack access falls within stack limits and that 'reg' doesn't |
7078 | * have a variable offset. | |
7079 | * | |
7080 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
7081 | * requires corresponding support in Spectre masking for stack ALU. See also | |
7082 | * retrieve_ptr_limit(). | |
7083 | * | |
7084 | * | |
7085 | * 'off' includes 'reg->off'. | |
7086 | */ | |
7087 | static int check_stack_access_for_ptr_arithmetic( | |
7088 | struct bpf_verifier_env *env, | |
7089 | int regno, | |
7090 | const struct bpf_reg_state *reg, | |
7091 | int off) | |
7092 | { | |
7093 | if (!tnum_is_const(reg->var_off)) { | |
7094 | char tn_buf[48]; | |
7095 | ||
7096 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
7097 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
7098 | regno, tn_buf, off); | |
7099 | return -EACCES; | |
7100 | } | |
7101 | ||
7102 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
7103 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
7104 | "prohibited for !root; off=%d\n", regno, off); | |
7105 | return -EACCES; | |
7106 | } | |
7107 | ||
7108 | return 0; | |
7109 | } | |
7110 | ||
073815b7 DB |
7111 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
7112 | const struct bpf_insn *insn, | |
7113 | const struct bpf_reg_state *dst_reg) | |
7114 | { | |
7115 | u32 dst = insn->dst_reg; | |
7116 | ||
7117 | /* For unprivileged we require that resulting offset must be in bounds | |
7118 | * in order to be able to sanitize access later on. | |
7119 | */ | |
7120 | if (env->bypass_spec_v1) | |
7121 | return 0; | |
7122 | ||
7123 | switch (dst_reg->type) { | |
7124 | case PTR_TO_STACK: | |
7125 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
7126 | dst_reg->off + dst_reg->var_off.value)) | |
7127 | return -EACCES; | |
7128 | break; | |
7129 | case PTR_TO_MAP_VALUE: | |
7130 | if (check_map_access(env, dst, dst_reg->off, 1, false)) { | |
7131 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
7132 | "prohibited for !root\n", dst); | |
7133 | return -EACCES; | |
7134 | } | |
7135 | break; | |
7136 | default: | |
7137 | break; | |
7138 | } | |
7139 | ||
7140 | return 0; | |
7141 | } | |
01f810ac | 7142 | |
f1174f77 | 7143 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
7144 | * Caller should also handle BPF_MOV case separately. |
7145 | * If we return -EACCES, caller may want to try again treating pointer as a | |
7146 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
7147 | */ | |
7148 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
7149 | struct bpf_insn *insn, | |
7150 | const struct bpf_reg_state *ptr_reg, | |
7151 | const struct bpf_reg_state *off_reg) | |
969bf05e | 7152 | { |
f4d7e40a AS |
7153 | struct bpf_verifier_state *vstate = env->cur_state; |
7154 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
7155 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 7156 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
7157 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
7158 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
7159 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
7160 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 7161 | struct bpf_sanitize_info info = {}; |
969bf05e | 7162 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 7163 | u32 dst = insn->dst_reg; |
979d63d5 | 7164 | int ret; |
969bf05e | 7165 | |
f1174f77 | 7166 | dst_reg = ®s[dst]; |
969bf05e | 7167 | |
6f16101e DB |
7168 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
7169 | smin_val > smax_val || umin_val > umax_val) { | |
7170 | /* Taint dst register if offset had invalid bounds derived from | |
7171 | * e.g. dead branches. | |
7172 | */ | |
f54c7898 | 7173 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 7174 | return 0; |
f1174f77 EC |
7175 | } |
7176 | ||
7177 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
7178 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
7179 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
7180 | __mark_reg_unknown(env, dst_reg); | |
7181 | return 0; | |
7182 | } | |
7183 | ||
82abbf8d AS |
7184 | verbose(env, |
7185 | "R%d 32-bit pointer arithmetic prohibited\n", | |
7186 | dst); | |
f1174f77 | 7187 | return -EACCES; |
969bf05e AS |
7188 | } |
7189 | ||
aad2eeaf JS |
7190 | switch (ptr_reg->type) { |
7191 | case PTR_TO_MAP_VALUE_OR_NULL: | |
7192 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
7193 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 7194 | return -EACCES; |
aad2eeaf | 7195 | case CONST_PTR_TO_MAP: |
7c696732 YS |
7196 | /* smin_val represents the known value */ |
7197 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
7198 | break; | |
8731745e | 7199 | fallthrough; |
aad2eeaf | 7200 | case PTR_TO_PACKET_END: |
c64b7983 JS |
7201 | case PTR_TO_SOCKET: |
7202 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
7203 | case PTR_TO_SOCK_COMMON: |
7204 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
7205 | case PTR_TO_TCP_SOCK: |
7206 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 7207 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
7208 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
7209 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 7210 | return -EACCES; |
aad2eeaf JS |
7211 | default: |
7212 | break; | |
f1174f77 EC |
7213 | } |
7214 | ||
7215 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
7216 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 7217 | */ |
f1174f77 EC |
7218 | dst_reg->type = ptr_reg->type; |
7219 | dst_reg->id = ptr_reg->id; | |
969bf05e | 7220 | |
bb7f0f98 AS |
7221 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
7222 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
7223 | return -EINVAL; | |
7224 | ||
3f50f132 JF |
7225 | /* pointer types do not carry 32-bit bounds at the moment. */ |
7226 | __mark_reg32_unbounded(dst_reg); | |
7227 | ||
7fedb63a DB |
7228 | if (sanitize_needed(opcode)) { |
7229 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 7230 | &info, false); |
a6aaece0 DB |
7231 | if (ret < 0) |
7232 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 7233 | } |
a6aaece0 | 7234 | |
f1174f77 EC |
7235 | switch (opcode) { |
7236 | case BPF_ADD: | |
7237 | /* We can take a fixed offset as long as it doesn't overflow | |
7238 | * the s32 'off' field | |
969bf05e | 7239 | */ |
b03c9f9f EC |
7240 | if (known && (ptr_reg->off + smin_val == |
7241 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 7242 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
7243 | dst_reg->smin_value = smin_ptr; |
7244 | dst_reg->smax_value = smax_ptr; | |
7245 | dst_reg->umin_value = umin_ptr; | |
7246 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 7247 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 7248 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 7249 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
7250 | break; |
7251 | } | |
f1174f77 EC |
7252 | /* A new variable offset is created. Note that off_reg->off |
7253 | * == 0, since it's a scalar. | |
7254 | * dst_reg gets the pointer type and since some positive | |
7255 | * integer value was added to the pointer, give it a new 'id' | |
7256 | * if it's a PTR_TO_PACKET. | |
7257 | * this creates a new 'base' pointer, off_reg (variable) gets | |
7258 | * added into the variable offset, and we copy the fixed offset | |
7259 | * from ptr_reg. | |
969bf05e | 7260 | */ |
b03c9f9f EC |
7261 | if (signed_add_overflows(smin_ptr, smin_val) || |
7262 | signed_add_overflows(smax_ptr, smax_val)) { | |
7263 | dst_reg->smin_value = S64_MIN; | |
7264 | dst_reg->smax_value = S64_MAX; | |
7265 | } else { | |
7266 | dst_reg->smin_value = smin_ptr + smin_val; | |
7267 | dst_reg->smax_value = smax_ptr + smax_val; | |
7268 | } | |
7269 | if (umin_ptr + umin_val < umin_ptr || | |
7270 | umax_ptr + umax_val < umax_ptr) { | |
7271 | dst_reg->umin_value = 0; | |
7272 | dst_reg->umax_value = U64_MAX; | |
7273 | } else { | |
7274 | dst_reg->umin_value = umin_ptr + umin_val; | |
7275 | dst_reg->umax_value = umax_ptr + umax_val; | |
7276 | } | |
f1174f77 EC |
7277 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
7278 | dst_reg->off = ptr_reg->off; | |
0962590e | 7279 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 7280 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
7281 | dst_reg->id = ++env->id_gen; |
7282 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 7283 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
7284 | } |
7285 | break; | |
7286 | case BPF_SUB: | |
7287 | if (dst_reg == off_reg) { | |
7288 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
7289 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
7290 | dst); | |
f1174f77 EC |
7291 | return -EACCES; |
7292 | } | |
7293 | /* We don't allow subtraction from FP, because (according to | |
7294 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
7295 | * be able to deal with it. | |
969bf05e | 7296 | */ |
f1174f77 | 7297 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
7298 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
7299 | dst); | |
f1174f77 EC |
7300 | return -EACCES; |
7301 | } | |
b03c9f9f EC |
7302 | if (known && (ptr_reg->off - smin_val == |
7303 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 7304 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
7305 | dst_reg->smin_value = smin_ptr; |
7306 | dst_reg->smax_value = smax_ptr; | |
7307 | dst_reg->umin_value = umin_ptr; | |
7308 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
7309 | dst_reg->var_off = ptr_reg->var_off; |
7310 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 7311 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 7312 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
7313 | break; |
7314 | } | |
f1174f77 EC |
7315 | /* A new variable offset is created. If the subtrahend is known |
7316 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 7317 | */ |
b03c9f9f EC |
7318 | if (signed_sub_overflows(smin_ptr, smax_val) || |
7319 | signed_sub_overflows(smax_ptr, smin_val)) { | |
7320 | /* Overflow possible, we know nothing */ | |
7321 | dst_reg->smin_value = S64_MIN; | |
7322 | dst_reg->smax_value = S64_MAX; | |
7323 | } else { | |
7324 | dst_reg->smin_value = smin_ptr - smax_val; | |
7325 | dst_reg->smax_value = smax_ptr - smin_val; | |
7326 | } | |
7327 | if (umin_ptr < umax_val) { | |
7328 | /* Overflow possible, we know nothing */ | |
7329 | dst_reg->umin_value = 0; | |
7330 | dst_reg->umax_value = U64_MAX; | |
7331 | } else { | |
7332 | /* Cannot overflow (as long as bounds are consistent) */ | |
7333 | dst_reg->umin_value = umin_ptr - umax_val; | |
7334 | dst_reg->umax_value = umax_ptr - umin_val; | |
7335 | } | |
f1174f77 EC |
7336 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
7337 | dst_reg->off = ptr_reg->off; | |
0962590e | 7338 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 7339 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
7340 | dst_reg->id = ++env->id_gen; |
7341 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 7342 | if (smin_val < 0) |
22dc4a0f | 7343 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 7344 | } |
f1174f77 EC |
7345 | break; |
7346 | case BPF_AND: | |
7347 | case BPF_OR: | |
7348 | case BPF_XOR: | |
82abbf8d AS |
7349 | /* bitwise ops on pointers are troublesome, prohibit. */ |
7350 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
7351 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
7352 | return -EACCES; |
7353 | default: | |
7354 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
7355 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
7356 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 7357 | return -EACCES; |
43188702 JF |
7358 | } |
7359 | ||
bb7f0f98 AS |
7360 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
7361 | return -EINVAL; | |
7362 | ||
b03c9f9f EC |
7363 | __update_reg_bounds(dst_reg); |
7364 | __reg_deduce_bounds(dst_reg); | |
7365 | __reg_bound_offset(dst_reg); | |
0d6303db | 7366 | |
073815b7 DB |
7367 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
7368 | return -EACCES; | |
7fedb63a DB |
7369 | if (sanitize_needed(opcode)) { |
7370 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 7371 | &info, true); |
7fedb63a DB |
7372 | if (ret < 0) |
7373 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
7374 | } |
7375 | ||
43188702 JF |
7376 | return 0; |
7377 | } | |
7378 | ||
3f50f132 JF |
7379 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
7380 | struct bpf_reg_state *src_reg) | |
7381 | { | |
7382 | s32 smin_val = src_reg->s32_min_value; | |
7383 | s32 smax_val = src_reg->s32_max_value; | |
7384 | u32 umin_val = src_reg->u32_min_value; | |
7385 | u32 umax_val = src_reg->u32_max_value; | |
7386 | ||
7387 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
7388 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
7389 | dst_reg->s32_min_value = S32_MIN; | |
7390 | dst_reg->s32_max_value = S32_MAX; | |
7391 | } else { | |
7392 | dst_reg->s32_min_value += smin_val; | |
7393 | dst_reg->s32_max_value += smax_val; | |
7394 | } | |
7395 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
7396 | dst_reg->u32_max_value + umax_val < umax_val) { | |
7397 | dst_reg->u32_min_value = 0; | |
7398 | dst_reg->u32_max_value = U32_MAX; | |
7399 | } else { | |
7400 | dst_reg->u32_min_value += umin_val; | |
7401 | dst_reg->u32_max_value += umax_val; | |
7402 | } | |
7403 | } | |
7404 | ||
07cd2631 JF |
7405 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
7406 | struct bpf_reg_state *src_reg) | |
7407 | { | |
7408 | s64 smin_val = src_reg->smin_value; | |
7409 | s64 smax_val = src_reg->smax_value; | |
7410 | u64 umin_val = src_reg->umin_value; | |
7411 | u64 umax_val = src_reg->umax_value; | |
7412 | ||
7413 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
7414 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
7415 | dst_reg->smin_value = S64_MIN; | |
7416 | dst_reg->smax_value = S64_MAX; | |
7417 | } else { | |
7418 | dst_reg->smin_value += smin_val; | |
7419 | dst_reg->smax_value += smax_val; | |
7420 | } | |
7421 | if (dst_reg->umin_value + umin_val < umin_val || | |
7422 | dst_reg->umax_value + umax_val < umax_val) { | |
7423 | dst_reg->umin_value = 0; | |
7424 | dst_reg->umax_value = U64_MAX; | |
7425 | } else { | |
7426 | dst_reg->umin_value += umin_val; | |
7427 | dst_reg->umax_value += umax_val; | |
7428 | } | |
3f50f132 JF |
7429 | } |
7430 | ||
7431 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
7432 | struct bpf_reg_state *src_reg) | |
7433 | { | |
7434 | s32 smin_val = src_reg->s32_min_value; | |
7435 | s32 smax_val = src_reg->s32_max_value; | |
7436 | u32 umin_val = src_reg->u32_min_value; | |
7437 | u32 umax_val = src_reg->u32_max_value; | |
7438 | ||
7439 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
7440 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
7441 | /* Overflow possible, we know nothing */ | |
7442 | dst_reg->s32_min_value = S32_MIN; | |
7443 | dst_reg->s32_max_value = S32_MAX; | |
7444 | } else { | |
7445 | dst_reg->s32_min_value -= smax_val; | |
7446 | dst_reg->s32_max_value -= smin_val; | |
7447 | } | |
7448 | if (dst_reg->u32_min_value < umax_val) { | |
7449 | /* Overflow possible, we know nothing */ | |
7450 | dst_reg->u32_min_value = 0; | |
7451 | dst_reg->u32_max_value = U32_MAX; | |
7452 | } else { | |
7453 | /* Cannot overflow (as long as bounds are consistent) */ | |
7454 | dst_reg->u32_min_value -= umax_val; | |
7455 | dst_reg->u32_max_value -= umin_val; | |
7456 | } | |
07cd2631 JF |
7457 | } |
7458 | ||
7459 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
7460 | struct bpf_reg_state *src_reg) | |
7461 | { | |
7462 | s64 smin_val = src_reg->smin_value; | |
7463 | s64 smax_val = src_reg->smax_value; | |
7464 | u64 umin_val = src_reg->umin_value; | |
7465 | u64 umax_val = src_reg->umax_value; | |
7466 | ||
7467 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
7468 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
7469 | /* Overflow possible, we know nothing */ | |
7470 | dst_reg->smin_value = S64_MIN; | |
7471 | dst_reg->smax_value = S64_MAX; | |
7472 | } else { | |
7473 | dst_reg->smin_value -= smax_val; | |
7474 | dst_reg->smax_value -= smin_val; | |
7475 | } | |
7476 | if (dst_reg->umin_value < umax_val) { | |
7477 | /* Overflow possible, we know nothing */ | |
7478 | dst_reg->umin_value = 0; | |
7479 | dst_reg->umax_value = U64_MAX; | |
7480 | } else { | |
7481 | /* Cannot overflow (as long as bounds are consistent) */ | |
7482 | dst_reg->umin_value -= umax_val; | |
7483 | dst_reg->umax_value -= umin_val; | |
7484 | } | |
3f50f132 JF |
7485 | } |
7486 | ||
7487 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
7488 | struct bpf_reg_state *src_reg) | |
7489 | { | |
7490 | s32 smin_val = src_reg->s32_min_value; | |
7491 | u32 umin_val = src_reg->u32_min_value; | |
7492 | u32 umax_val = src_reg->u32_max_value; | |
7493 | ||
7494 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
7495 | /* Ain't nobody got time to multiply that sign */ | |
7496 | __mark_reg32_unbounded(dst_reg); | |
7497 | return; | |
7498 | } | |
7499 | /* Both values are positive, so we can work with unsigned and | |
7500 | * copy the result to signed (unless it exceeds S32_MAX). | |
7501 | */ | |
7502 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
7503 | /* Potential overflow, we know nothing */ | |
7504 | __mark_reg32_unbounded(dst_reg); | |
7505 | return; | |
7506 | } | |
7507 | dst_reg->u32_min_value *= umin_val; | |
7508 | dst_reg->u32_max_value *= umax_val; | |
7509 | if (dst_reg->u32_max_value > S32_MAX) { | |
7510 | /* Overflow possible, we know nothing */ | |
7511 | dst_reg->s32_min_value = S32_MIN; | |
7512 | dst_reg->s32_max_value = S32_MAX; | |
7513 | } else { | |
7514 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7515 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7516 | } | |
07cd2631 JF |
7517 | } |
7518 | ||
7519 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
7520 | struct bpf_reg_state *src_reg) | |
7521 | { | |
7522 | s64 smin_val = src_reg->smin_value; | |
7523 | u64 umin_val = src_reg->umin_value; | |
7524 | u64 umax_val = src_reg->umax_value; | |
7525 | ||
07cd2631 JF |
7526 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
7527 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 7528 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
7529 | return; |
7530 | } | |
7531 | /* Both values are positive, so we can work with unsigned and | |
7532 | * copy the result to signed (unless it exceeds S64_MAX). | |
7533 | */ | |
7534 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
7535 | /* Potential overflow, we know nothing */ | |
3f50f132 | 7536 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
7537 | return; |
7538 | } | |
7539 | dst_reg->umin_value *= umin_val; | |
7540 | dst_reg->umax_value *= umax_val; | |
7541 | if (dst_reg->umax_value > S64_MAX) { | |
7542 | /* Overflow possible, we know nothing */ | |
7543 | dst_reg->smin_value = S64_MIN; | |
7544 | dst_reg->smax_value = S64_MAX; | |
7545 | } else { | |
7546 | dst_reg->smin_value = dst_reg->umin_value; | |
7547 | dst_reg->smax_value = dst_reg->umax_value; | |
7548 | } | |
7549 | } | |
7550 | ||
3f50f132 JF |
7551 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
7552 | struct bpf_reg_state *src_reg) | |
7553 | { | |
7554 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7555 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7556 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
7557 | s32 smin_val = src_reg->s32_min_value; | |
7558 | u32 umax_val = src_reg->u32_max_value; | |
7559 | ||
049c4e13 DB |
7560 | if (src_known && dst_known) { |
7561 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 7562 | return; |
049c4e13 | 7563 | } |
3f50f132 JF |
7564 | |
7565 | /* We get our minimum from the var_off, since that's inherently | |
7566 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
7567 | */ | |
7568 | dst_reg->u32_min_value = var32_off.value; | |
7569 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
7570 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
7571 | /* Lose signed bounds when ANDing negative numbers, | |
7572 | * ain't nobody got time for that. | |
7573 | */ | |
7574 | dst_reg->s32_min_value = S32_MIN; | |
7575 | dst_reg->s32_max_value = S32_MAX; | |
7576 | } else { | |
7577 | /* ANDing two positives gives a positive, so safe to | |
7578 | * cast result into s64. | |
7579 | */ | |
7580 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7581 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7582 | } | |
3f50f132 JF |
7583 | } |
7584 | ||
07cd2631 JF |
7585 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
7586 | struct bpf_reg_state *src_reg) | |
7587 | { | |
3f50f132 JF |
7588 | bool src_known = tnum_is_const(src_reg->var_off); |
7589 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
7590 | s64 smin_val = src_reg->smin_value; |
7591 | u64 umax_val = src_reg->umax_value; | |
7592 | ||
3f50f132 | 7593 | if (src_known && dst_known) { |
4fbb38a3 | 7594 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
7595 | return; |
7596 | } | |
7597 | ||
07cd2631 JF |
7598 | /* We get our minimum from the var_off, since that's inherently |
7599 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
7600 | */ | |
07cd2631 JF |
7601 | dst_reg->umin_value = dst_reg->var_off.value; |
7602 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
7603 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
7604 | /* Lose signed bounds when ANDing negative numbers, | |
7605 | * ain't nobody got time for that. | |
7606 | */ | |
7607 | dst_reg->smin_value = S64_MIN; | |
7608 | dst_reg->smax_value = S64_MAX; | |
7609 | } else { | |
7610 | /* ANDing two positives gives a positive, so safe to | |
7611 | * cast result into s64. | |
7612 | */ | |
7613 | dst_reg->smin_value = dst_reg->umin_value; | |
7614 | dst_reg->smax_value = dst_reg->umax_value; | |
7615 | } | |
7616 | /* We may learn something more from the var_off */ | |
7617 | __update_reg_bounds(dst_reg); | |
7618 | } | |
7619 | ||
3f50f132 JF |
7620 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
7621 | struct bpf_reg_state *src_reg) | |
7622 | { | |
7623 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7624 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7625 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
7626 | s32 smin_val = src_reg->s32_min_value; |
7627 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 7628 | |
049c4e13 DB |
7629 | if (src_known && dst_known) { |
7630 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 7631 | return; |
049c4e13 | 7632 | } |
3f50f132 JF |
7633 | |
7634 | /* We get our maximum from the var_off, and our minimum is the | |
7635 | * maximum of the operands' minima | |
7636 | */ | |
7637 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
7638 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
7639 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
7640 | /* Lose signed bounds when ORing negative numbers, | |
7641 | * ain't nobody got time for that. | |
7642 | */ | |
7643 | dst_reg->s32_min_value = S32_MIN; | |
7644 | dst_reg->s32_max_value = S32_MAX; | |
7645 | } else { | |
7646 | /* ORing two positives gives a positive, so safe to | |
7647 | * cast result into s64. | |
7648 | */ | |
5b9fbeb7 DB |
7649 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
7650 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
7651 | } |
7652 | } | |
7653 | ||
07cd2631 JF |
7654 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
7655 | struct bpf_reg_state *src_reg) | |
7656 | { | |
3f50f132 JF |
7657 | bool src_known = tnum_is_const(src_reg->var_off); |
7658 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
7659 | s64 smin_val = src_reg->smin_value; |
7660 | u64 umin_val = src_reg->umin_value; | |
7661 | ||
3f50f132 | 7662 | if (src_known && dst_known) { |
4fbb38a3 | 7663 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
7664 | return; |
7665 | } | |
7666 | ||
07cd2631 JF |
7667 | /* We get our maximum from the var_off, and our minimum is the |
7668 | * maximum of the operands' minima | |
7669 | */ | |
07cd2631 JF |
7670 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
7671 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
7672 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
7673 | /* Lose signed bounds when ORing negative numbers, | |
7674 | * ain't nobody got time for that. | |
7675 | */ | |
7676 | dst_reg->smin_value = S64_MIN; | |
7677 | dst_reg->smax_value = S64_MAX; | |
7678 | } else { | |
7679 | /* ORing two positives gives a positive, so safe to | |
7680 | * cast result into s64. | |
7681 | */ | |
7682 | dst_reg->smin_value = dst_reg->umin_value; | |
7683 | dst_reg->smax_value = dst_reg->umax_value; | |
7684 | } | |
7685 | /* We may learn something more from the var_off */ | |
7686 | __update_reg_bounds(dst_reg); | |
7687 | } | |
7688 | ||
2921c90d YS |
7689 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
7690 | struct bpf_reg_state *src_reg) | |
7691 | { | |
7692 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7693 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7694 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
7695 | s32 smin_val = src_reg->s32_min_value; | |
7696 | ||
049c4e13 DB |
7697 | if (src_known && dst_known) { |
7698 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 7699 | return; |
049c4e13 | 7700 | } |
2921c90d YS |
7701 | |
7702 | /* We get both minimum and maximum from the var32_off. */ | |
7703 | dst_reg->u32_min_value = var32_off.value; | |
7704 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
7705 | ||
7706 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
7707 | /* XORing two positive sign numbers gives a positive, | |
7708 | * so safe to cast u32 result into s32. | |
7709 | */ | |
7710 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7711 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7712 | } else { | |
7713 | dst_reg->s32_min_value = S32_MIN; | |
7714 | dst_reg->s32_max_value = S32_MAX; | |
7715 | } | |
7716 | } | |
7717 | ||
7718 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
7719 | struct bpf_reg_state *src_reg) | |
7720 | { | |
7721 | bool src_known = tnum_is_const(src_reg->var_off); | |
7722 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
7723 | s64 smin_val = src_reg->smin_value; | |
7724 | ||
7725 | if (src_known && dst_known) { | |
7726 | /* dst_reg->var_off.value has been updated earlier */ | |
7727 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
7728 | return; | |
7729 | } | |
7730 | ||
7731 | /* We get both minimum and maximum from the var_off. */ | |
7732 | dst_reg->umin_value = dst_reg->var_off.value; | |
7733 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
7734 | ||
7735 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
7736 | /* XORing two positive sign numbers gives a positive, | |
7737 | * so safe to cast u64 result into s64. | |
7738 | */ | |
7739 | dst_reg->smin_value = dst_reg->umin_value; | |
7740 | dst_reg->smax_value = dst_reg->umax_value; | |
7741 | } else { | |
7742 | dst_reg->smin_value = S64_MIN; | |
7743 | dst_reg->smax_value = S64_MAX; | |
7744 | } | |
7745 | ||
7746 | __update_reg_bounds(dst_reg); | |
7747 | } | |
7748 | ||
3f50f132 JF |
7749 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
7750 | u64 umin_val, u64 umax_val) | |
07cd2631 | 7751 | { |
07cd2631 JF |
7752 | /* We lose all sign bit information (except what we can pick |
7753 | * up from var_off) | |
7754 | */ | |
3f50f132 JF |
7755 | dst_reg->s32_min_value = S32_MIN; |
7756 | dst_reg->s32_max_value = S32_MAX; | |
7757 | /* If we might shift our top bit out, then we know nothing */ | |
7758 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
7759 | dst_reg->u32_min_value = 0; | |
7760 | dst_reg->u32_max_value = U32_MAX; | |
7761 | } else { | |
7762 | dst_reg->u32_min_value <<= umin_val; | |
7763 | dst_reg->u32_max_value <<= umax_val; | |
7764 | } | |
7765 | } | |
7766 | ||
7767 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
7768 | struct bpf_reg_state *src_reg) | |
7769 | { | |
7770 | u32 umax_val = src_reg->u32_max_value; | |
7771 | u32 umin_val = src_reg->u32_min_value; | |
7772 | /* u32 alu operation will zext upper bits */ | |
7773 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
7774 | ||
7775 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
7776 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
7777 | /* Not required but being careful mark reg64 bounds as unknown so | |
7778 | * that we are forced to pick them up from tnum and zext later and | |
7779 | * if some path skips this step we are still safe. | |
7780 | */ | |
7781 | __mark_reg64_unbounded(dst_reg); | |
7782 | __update_reg32_bounds(dst_reg); | |
7783 | } | |
7784 | ||
7785 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
7786 | u64 umin_val, u64 umax_val) | |
7787 | { | |
7788 | /* Special case <<32 because it is a common compiler pattern to sign | |
7789 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
7790 | * positive we know this shift will also be positive so we can track | |
7791 | * bounds correctly. Otherwise we lose all sign bit information except | |
7792 | * what we can pick up from var_off. Perhaps we can generalize this | |
7793 | * later to shifts of any length. | |
7794 | */ | |
7795 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
7796 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
7797 | else | |
7798 | dst_reg->smax_value = S64_MAX; | |
7799 | ||
7800 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
7801 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
7802 | else | |
7803 | dst_reg->smin_value = S64_MIN; | |
7804 | ||
07cd2631 JF |
7805 | /* If we might shift our top bit out, then we know nothing */ |
7806 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
7807 | dst_reg->umin_value = 0; | |
7808 | dst_reg->umax_value = U64_MAX; | |
7809 | } else { | |
7810 | dst_reg->umin_value <<= umin_val; | |
7811 | dst_reg->umax_value <<= umax_val; | |
7812 | } | |
3f50f132 JF |
7813 | } |
7814 | ||
7815 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
7816 | struct bpf_reg_state *src_reg) | |
7817 | { | |
7818 | u64 umax_val = src_reg->umax_value; | |
7819 | u64 umin_val = src_reg->umin_value; | |
7820 | ||
7821 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
7822 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
7823 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
7824 | ||
07cd2631 JF |
7825 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
7826 | /* We may learn something more from the var_off */ | |
7827 | __update_reg_bounds(dst_reg); | |
7828 | } | |
7829 | ||
3f50f132 JF |
7830 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
7831 | struct bpf_reg_state *src_reg) | |
7832 | { | |
7833 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
7834 | u32 umax_val = src_reg->u32_max_value; | |
7835 | u32 umin_val = src_reg->u32_min_value; | |
7836 | ||
7837 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
7838 | * be negative, then either: | |
7839 | * 1) src_reg might be zero, so the sign bit of the result is | |
7840 | * unknown, so we lose our signed bounds | |
7841 | * 2) it's known negative, thus the unsigned bounds capture the | |
7842 | * signed bounds | |
7843 | * 3) the signed bounds cross zero, so they tell us nothing | |
7844 | * about the result | |
7845 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 7846 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
7847 | * Thus, in all cases it suffices to blow away our signed bounds |
7848 | * and rely on inferring new ones from the unsigned bounds and | |
7849 | * var_off of the result. | |
7850 | */ | |
7851 | dst_reg->s32_min_value = S32_MIN; | |
7852 | dst_reg->s32_max_value = S32_MAX; | |
7853 | ||
7854 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
7855 | dst_reg->u32_min_value >>= umax_val; | |
7856 | dst_reg->u32_max_value >>= umin_val; | |
7857 | ||
7858 | __mark_reg64_unbounded(dst_reg); | |
7859 | __update_reg32_bounds(dst_reg); | |
7860 | } | |
7861 | ||
07cd2631 JF |
7862 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
7863 | struct bpf_reg_state *src_reg) | |
7864 | { | |
7865 | u64 umax_val = src_reg->umax_value; | |
7866 | u64 umin_val = src_reg->umin_value; | |
7867 | ||
7868 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
7869 | * be negative, then either: | |
7870 | * 1) src_reg might be zero, so the sign bit of the result is | |
7871 | * unknown, so we lose our signed bounds | |
7872 | * 2) it's known negative, thus the unsigned bounds capture the | |
7873 | * signed bounds | |
7874 | * 3) the signed bounds cross zero, so they tell us nothing | |
7875 | * about the result | |
7876 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 7877 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
7878 | * Thus, in all cases it suffices to blow away our signed bounds |
7879 | * and rely on inferring new ones from the unsigned bounds and | |
7880 | * var_off of the result. | |
7881 | */ | |
7882 | dst_reg->smin_value = S64_MIN; | |
7883 | dst_reg->smax_value = S64_MAX; | |
7884 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
7885 | dst_reg->umin_value >>= umax_val; | |
7886 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
7887 | |
7888 | /* Its not easy to operate on alu32 bounds here because it depends | |
7889 | * on bits being shifted in. Take easy way out and mark unbounded | |
7890 | * so we can recalculate later from tnum. | |
7891 | */ | |
7892 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
7893 | __update_reg_bounds(dst_reg); |
7894 | } | |
7895 | ||
3f50f132 JF |
7896 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
7897 | struct bpf_reg_state *src_reg) | |
07cd2631 | 7898 | { |
3f50f132 | 7899 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
7900 | |
7901 | /* Upon reaching here, src_known is true and | |
7902 | * umax_val is equal to umin_val. | |
7903 | */ | |
3f50f132 JF |
7904 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
7905 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 7906 | |
3f50f132 JF |
7907 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
7908 | ||
7909 | /* blow away the dst_reg umin_value/umax_value and rely on | |
7910 | * dst_reg var_off to refine the result. | |
7911 | */ | |
7912 | dst_reg->u32_min_value = 0; | |
7913 | dst_reg->u32_max_value = U32_MAX; | |
7914 | ||
7915 | __mark_reg64_unbounded(dst_reg); | |
7916 | __update_reg32_bounds(dst_reg); | |
7917 | } | |
7918 | ||
7919 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
7920 | struct bpf_reg_state *src_reg) | |
7921 | { | |
7922 | u64 umin_val = src_reg->umin_value; | |
7923 | ||
7924 | /* Upon reaching here, src_known is true and umax_val is equal | |
7925 | * to umin_val. | |
7926 | */ | |
7927 | dst_reg->smin_value >>= umin_val; | |
7928 | dst_reg->smax_value >>= umin_val; | |
7929 | ||
7930 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
7931 | |
7932 | /* blow away the dst_reg umin_value/umax_value and rely on | |
7933 | * dst_reg var_off to refine the result. | |
7934 | */ | |
7935 | dst_reg->umin_value = 0; | |
7936 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
7937 | |
7938 | /* Its not easy to operate on alu32 bounds here because it depends | |
7939 | * on bits being shifted in from upper 32-bits. Take easy way out | |
7940 | * and mark unbounded so we can recalculate later from tnum. | |
7941 | */ | |
7942 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
7943 | __update_reg_bounds(dst_reg); |
7944 | } | |
7945 | ||
468f6eaf JH |
7946 | /* WARNING: This function does calculations on 64-bit values, but the actual |
7947 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
7948 | * need extra checks in the 32-bit case. | |
7949 | */ | |
f1174f77 EC |
7950 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
7951 | struct bpf_insn *insn, | |
7952 | struct bpf_reg_state *dst_reg, | |
7953 | struct bpf_reg_state src_reg) | |
969bf05e | 7954 | { |
638f5b90 | 7955 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 7956 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 7957 | bool src_known; |
b03c9f9f EC |
7958 | s64 smin_val, smax_val; |
7959 | u64 umin_val, umax_val; | |
3f50f132 JF |
7960 | s32 s32_min_val, s32_max_val; |
7961 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 7962 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 7963 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 7964 | int ret; |
b799207e | 7965 | |
b03c9f9f EC |
7966 | smin_val = src_reg.smin_value; |
7967 | smax_val = src_reg.smax_value; | |
7968 | umin_val = src_reg.umin_value; | |
7969 | umax_val = src_reg.umax_value; | |
f23cc643 | 7970 | |
3f50f132 JF |
7971 | s32_min_val = src_reg.s32_min_value; |
7972 | s32_max_val = src_reg.s32_max_value; | |
7973 | u32_min_val = src_reg.u32_min_value; | |
7974 | u32_max_val = src_reg.u32_max_value; | |
7975 | ||
7976 | if (alu32) { | |
7977 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
7978 | if ((src_known && |
7979 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
7980 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
7981 | /* Taint dst register if offset had invalid bounds | |
7982 | * derived from e.g. dead branches. | |
7983 | */ | |
7984 | __mark_reg_unknown(env, dst_reg); | |
7985 | return 0; | |
7986 | } | |
7987 | } else { | |
7988 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
7989 | if ((src_known && |
7990 | (smin_val != smax_val || umin_val != umax_val)) || | |
7991 | smin_val > smax_val || umin_val > umax_val) { | |
7992 | /* Taint dst register if offset had invalid bounds | |
7993 | * derived from e.g. dead branches. | |
7994 | */ | |
7995 | __mark_reg_unknown(env, dst_reg); | |
7996 | return 0; | |
7997 | } | |
6f16101e DB |
7998 | } |
7999 | ||
bb7f0f98 AS |
8000 | if (!src_known && |
8001 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 8002 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
8003 | return 0; |
8004 | } | |
8005 | ||
f5288193 DB |
8006 | if (sanitize_needed(opcode)) { |
8007 | ret = sanitize_val_alu(env, insn); | |
8008 | if (ret < 0) | |
8009 | return sanitize_err(env, insn, ret, NULL, NULL); | |
8010 | } | |
8011 | ||
3f50f132 JF |
8012 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
8013 | * There are two classes of instructions: The first class we track both | |
8014 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
8015 | * greatest amount of precision when alu operations are mixed with jmp32 | |
8016 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
8017 | * and BPF_OR. This is possible because these ops have fairly easy to | |
8018 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
8019 | * See alu32 verifier tests for examples. The second class of | |
8020 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
8021 | * with regards to tracking sign/unsigned bounds because the bits may | |
8022 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
8023 | * the reg unbounded in the subreg bound space and use the resulting | |
8024 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
8025 | */ | |
48461135 JB |
8026 | switch (opcode) { |
8027 | case BPF_ADD: | |
3f50f132 | 8028 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 8029 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 8030 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
8031 | break; |
8032 | case BPF_SUB: | |
3f50f132 | 8033 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 8034 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 8035 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
8036 | break; |
8037 | case BPF_MUL: | |
3f50f132 JF |
8038 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
8039 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 8040 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
8041 | break; |
8042 | case BPF_AND: | |
3f50f132 JF |
8043 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
8044 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 8045 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
8046 | break; |
8047 | case BPF_OR: | |
3f50f132 JF |
8048 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
8049 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 8050 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 8051 | break; |
2921c90d YS |
8052 | case BPF_XOR: |
8053 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
8054 | scalar32_min_max_xor(dst_reg, &src_reg); | |
8055 | scalar_min_max_xor(dst_reg, &src_reg); | |
8056 | break; | |
48461135 | 8057 | case BPF_LSH: |
468f6eaf JH |
8058 | if (umax_val >= insn_bitness) { |
8059 | /* Shifts greater than 31 or 63 are undefined. | |
8060 | * This includes shifts by a negative number. | |
b03c9f9f | 8061 | */ |
61bd5218 | 8062 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
8063 | break; |
8064 | } | |
3f50f132 JF |
8065 | if (alu32) |
8066 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
8067 | else | |
8068 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
8069 | break; |
8070 | case BPF_RSH: | |
468f6eaf JH |
8071 | if (umax_val >= insn_bitness) { |
8072 | /* Shifts greater than 31 or 63 are undefined. | |
8073 | * This includes shifts by a negative number. | |
b03c9f9f | 8074 | */ |
61bd5218 | 8075 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
8076 | break; |
8077 | } | |
3f50f132 JF |
8078 | if (alu32) |
8079 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
8080 | else | |
8081 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 8082 | break; |
9cbe1f5a YS |
8083 | case BPF_ARSH: |
8084 | if (umax_val >= insn_bitness) { | |
8085 | /* Shifts greater than 31 or 63 are undefined. | |
8086 | * This includes shifts by a negative number. | |
8087 | */ | |
8088 | mark_reg_unknown(env, regs, insn->dst_reg); | |
8089 | break; | |
8090 | } | |
3f50f132 JF |
8091 | if (alu32) |
8092 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
8093 | else | |
8094 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 8095 | break; |
48461135 | 8096 | default: |
61bd5218 | 8097 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
8098 | break; |
8099 | } | |
8100 | ||
3f50f132 JF |
8101 | /* ALU32 ops are zero extended into 64bit register */ |
8102 | if (alu32) | |
8103 | zext_32_to_64(dst_reg); | |
468f6eaf | 8104 | |
294f2fc6 | 8105 | __update_reg_bounds(dst_reg); |
b03c9f9f EC |
8106 | __reg_deduce_bounds(dst_reg); |
8107 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
8108 | return 0; |
8109 | } | |
8110 | ||
8111 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
8112 | * and var_off. | |
8113 | */ | |
8114 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
8115 | struct bpf_insn *insn) | |
8116 | { | |
f4d7e40a AS |
8117 | struct bpf_verifier_state *vstate = env->cur_state; |
8118 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
8119 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
8120 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
8121 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 8122 | int err; |
f1174f77 EC |
8123 | |
8124 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
8125 | src_reg = NULL; |
8126 | if (dst_reg->type != SCALAR_VALUE) | |
8127 | ptr_reg = dst_reg; | |
75748837 AS |
8128 | else |
8129 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
8130 | * incorrectly propagated into other registers by find_equal_scalars() | |
8131 | */ | |
8132 | dst_reg->id = 0; | |
f1174f77 EC |
8133 | if (BPF_SRC(insn->code) == BPF_X) { |
8134 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
8135 | if (src_reg->type != SCALAR_VALUE) { |
8136 | if (dst_reg->type != SCALAR_VALUE) { | |
8137 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
8138 | * an arbitrary scalar. Disallow all math except |
8139 | * pointer subtraction | |
f1174f77 | 8140 | */ |
dd066823 | 8141 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
8142 | mark_reg_unknown(env, regs, insn->dst_reg); |
8143 | return 0; | |
f1174f77 | 8144 | } |
82abbf8d AS |
8145 | verbose(env, "R%d pointer %s pointer prohibited\n", |
8146 | insn->dst_reg, | |
8147 | bpf_alu_string[opcode >> 4]); | |
8148 | return -EACCES; | |
f1174f77 EC |
8149 | } else { |
8150 | /* scalar += pointer | |
8151 | * This is legal, but we have to reverse our | |
8152 | * src/dest handling in computing the range | |
8153 | */ | |
b5dc0163 AS |
8154 | err = mark_chain_precision(env, insn->dst_reg); |
8155 | if (err) | |
8156 | return err; | |
82abbf8d AS |
8157 | return adjust_ptr_min_max_vals(env, insn, |
8158 | src_reg, dst_reg); | |
f1174f77 EC |
8159 | } |
8160 | } else if (ptr_reg) { | |
8161 | /* pointer += scalar */ | |
b5dc0163 AS |
8162 | err = mark_chain_precision(env, insn->src_reg); |
8163 | if (err) | |
8164 | return err; | |
82abbf8d AS |
8165 | return adjust_ptr_min_max_vals(env, insn, |
8166 | dst_reg, src_reg); | |
f1174f77 EC |
8167 | } |
8168 | } else { | |
8169 | /* Pretend the src is a reg with a known value, since we only | |
8170 | * need to be able to read from this state. | |
8171 | */ | |
8172 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 8173 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 8174 | src_reg = &off_reg; |
82abbf8d AS |
8175 | if (ptr_reg) /* pointer += K */ |
8176 | return adjust_ptr_min_max_vals(env, insn, | |
8177 | ptr_reg, src_reg); | |
f1174f77 EC |
8178 | } |
8179 | ||
8180 | /* Got here implies adding two SCALAR_VALUEs */ | |
8181 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 8182 | print_verifier_state(env, state); |
61bd5218 | 8183 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
8184 | return -EINVAL; |
8185 | } | |
8186 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 8187 | print_verifier_state(env, state); |
61bd5218 | 8188 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
8189 | return -EINVAL; |
8190 | } | |
8191 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
8192 | } |
8193 | ||
17a52670 | 8194 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 8195 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 8196 | { |
638f5b90 | 8197 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
8198 | u8 opcode = BPF_OP(insn->code); |
8199 | int err; | |
8200 | ||
8201 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
8202 | if (opcode == BPF_NEG) { | |
8203 | if (BPF_SRC(insn->code) != 0 || | |
8204 | insn->src_reg != BPF_REG_0 || | |
8205 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 8206 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
8207 | return -EINVAL; |
8208 | } | |
8209 | } else { | |
8210 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
8211 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
8212 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 8213 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
8214 | return -EINVAL; |
8215 | } | |
8216 | } | |
8217 | ||
8218 | /* check src operand */ | |
dc503a8a | 8219 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8220 | if (err) |
8221 | return err; | |
8222 | ||
1be7f75d | 8223 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 8224 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
8225 | insn->dst_reg); |
8226 | return -EACCES; | |
8227 | } | |
8228 | ||
17a52670 | 8229 | /* check dest operand */ |
dc503a8a | 8230 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
8231 | if (err) |
8232 | return err; | |
8233 | ||
8234 | } else if (opcode == BPF_MOV) { | |
8235 | ||
8236 | if (BPF_SRC(insn->code) == BPF_X) { | |
8237 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 8238 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
8239 | return -EINVAL; |
8240 | } | |
8241 | ||
8242 | /* check src operand */ | |
dc503a8a | 8243 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8244 | if (err) |
8245 | return err; | |
8246 | } else { | |
8247 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 8248 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
8249 | return -EINVAL; |
8250 | } | |
8251 | } | |
8252 | ||
fbeb1603 AF |
8253 | /* check dest operand, mark as required later */ |
8254 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
8255 | if (err) |
8256 | return err; | |
8257 | ||
8258 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
8259 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
8260 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
8261 | ||
17a52670 AS |
8262 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
8263 | /* case: R1 = R2 | |
8264 | * copy register state to dest reg | |
8265 | */ | |
75748837 AS |
8266 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
8267 | /* Assign src and dst registers the same ID | |
8268 | * that will be used by find_equal_scalars() | |
8269 | * to propagate min/max range. | |
8270 | */ | |
8271 | src_reg->id = ++env->id_gen; | |
e434b8cd JW |
8272 | *dst_reg = *src_reg; |
8273 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 8274 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 8275 | } else { |
f1174f77 | 8276 | /* R1 = (u32) R2 */ |
1be7f75d | 8277 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
8278 | verbose(env, |
8279 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
8280 | insn->src_reg); |
8281 | return -EACCES; | |
e434b8cd JW |
8282 | } else if (src_reg->type == SCALAR_VALUE) { |
8283 | *dst_reg = *src_reg; | |
75748837 AS |
8284 | /* Make sure ID is cleared otherwise |
8285 | * dst_reg min/max could be incorrectly | |
8286 | * propagated into src_reg by find_equal_scalars() | |
8287 | */ | |
8288 | dst_reg->id = 0; | |
e434b8cd | 8289 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 8290 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
8291 | } else { |
8292 | mark_reg_unknown(env, regs, | |
8293 | insn->dst_reg); | |
1be7f75d | 8294 | } |
3f50f132 | 8295 | zext_32_to_64(dst_reg); |
17a52670 AS |
8296 | } |
8297 | } else { | |
8298 | /* case: R = imm | |
8299 | * remember the value we stored into this reg | |
8300 | */ | |
fbeb1603 AF |
8301 | /* clear any state __mark_reg_known doesn't set */ |
8302 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 8303 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
8304 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
8305 | __mark_reg_known(regs + insn->dst_reg, | |
8306 | insn->imm); | |
8307 | } else { | |
8308 | __mark_reg_known(regs + insn->dst_reg, | |
8309 | (u32)insn->imm); | |
8310 | } | |
17a52670 AS |
8311 | } |
8312 | ||
8313 | } else if (opcode > BPF_END) { | |
61bd5218 | 8314 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
8315 | return -EINVAL; |
8316 | ||
8317 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
8318 | ||
17a52670 AS |
8319 | if (BPF_SRC(insn->code) == BPF_X) { |
8320 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 8321 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
8322 | return -EINVAL; |
8323 | } | |
8324 | /* check src1 operand */ | |
dc503a8a | 8325 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8326 | if (err) |
8327 | return err; | |
8328 | } else { | |
8329 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 8330 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
8331 | return -EINVAL; |
8332 | } | |
8333 | } | |
8334 | ||
8335 | /* check src2 operand */ | |
dc503a8a | 8336 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8337 | if (err) |
8338 | return err; | |
8339 | ||
8340 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
8341 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 8342 | verbose(env, "div by zero\n"); |
17a52670 AS |
8343 | return -EINVAL; |
8344 | } | |
8345 | ||
229394e8 RV |
8346 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
8347 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
8348 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
8349 | ||
8350 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 8351 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
8352 | return -EINVAL; |
8353 | } | |
8354 | } | |
8355 | ||
1a0dc1ac | 8356 | /* check dest operand */ |
dc503a8a | 8357 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
8358 | if (err) |
8359 | return err; | |
8360 | ||
f1174f77 | 8361 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
8362 | } |
8363 | ||
8364 | return 0; | |
8365 | } | |
8366 | ||
c6a9efa1 PC |
8367 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
8368 | struct bpf_reg_state *dst_reg, | |
6d94e741 | 8369 | enum bpf_reg_type type, int new_range) |
c6a9efa1 PC |
8370 | { |
8371 | struct bpf_reg_state *reg; | |
8372 | int i; | |
8373 | ||
8374 | for (i = 0; i < MAX_BPF_REG; i++) { | |
8375 | reg = &state->regs[i]; | |
8376 | if (reg->type == type && reg->id == dst_reg->id) | |
8377 | /* keep the maximum range already checked */ | |
8378 | reg->range = max(reg->range, new_range); | |
8379 | } | |
8380 | ||
8381 | bpf_for_each_spilled_reg(i, state, reg) { | |
8382 | if (!reg) | |
8383 | continue; | |
8384 | if (reg->type == type && reg->id == dst_reg->id) | |
8385 | reg->range = max(reg->range, new_range); | |
8386 | } | |
8387 | } | |
8388 | ||
f4d7e40a | 8389 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 8390 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 8391 | enum bpf_reg_type type, |
fb2a311a | 8392 | bool range_right_open) |
969bf05e | 8393 | { |
6d94e741 | 8394 | int new_range, i; |
2d2be8ca | 8395 | |
fb2a311a DB |
8396 | if (dst_reg->off < 0 || |
8397 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
8398 | /* This doesn't give us any range */ |
8399 | return; | |
8400 | ||
b03c9f9f EC |
8401 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
8402 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
8403 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
8404 | * than pkt_end, but that's because it's also less than pkt. | |
8405 | */ | |
8406 | return; | |
8407 | ||
fb2a311a DB |
8408 | new_range = dst_reg->off; |
8409 | if (range_right_open) | |
8410 | new_range--; | |
8411 | ||
8412 | /* Examples for register markings: | |
2d2be8ca | 8413 | * |
fb2a311a | 8414 | * pkt_data in dst register: |
2d2be8ca DB |
8415 | * |
8416 | * r2 = r3; | |
8417 | * r2 += 8; | |
8418 | * if (r2 > pkt_end) goto <handle exception> | |
8419 | * <access okay> | |
8420 | * | |
b4e432f1 DB |
8421 | * r2 = r3; |
8422 | * r2 += 8; | |
8423 | * if (r2 < pkt_end) goto <access okay> | |
8424 | * <handle exception> | |
8425 | * | |
2d2be8ca DB |
8426 | * Where: |
8427 | * r2 == dst_reg, pkt_end == src_reg | |
8428 | * r2=pkt(id=n,off=8,r=0) | |
8429 | * r3=pkt(id=n,off=0,r=0) | |
8430 | * | |
fb2a311a | 8431 | * pkt_data in src register: |
2d2be8ca DB |
8432 | * |
8433 | * r2 = r3; | |
8434 | * r2 += 8; | |
8435 | * if (pkt_end >= r2) goto <access okay> | |
8436 | * <handle exception> | |
8437 | * | |
b4e432f1 DB |
8438 | * r2 = r3; |
8439 | * r2 += 8; | |
8440 | * if (pkt_end <= r2) goto <handle exception> | |
8441 | * <access okay> | |
8442 | * | |
2d2be8ca DB |
8443 | * Where: |
8444 | * pkt_end == dst_reg, r2 == src_reg | |
8445 | * r2=pkt(id=n,off=8,r=0) | |
8446 | * r3=pkt(id=n,off=0,r=0) | |
8447 | * | |
8448 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
8449 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
8450 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
8451 | * the check. | |
969bf05e | 8452 | */ |
2d2be8ca | 8453 | |
f1174f77 EC |
8454 | /* If our ids match, then we must have the same max_value. And we |
8455 | * don't care about the other reg's fixed offset, since if it's too big | |
8456 | * the range won't allow anything. | |
8457 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
8458 | */ | |
c6a9efa1 PC |
8459 | for (i = 0; i <= vstate->curframe; i++) |
8460 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
8461 | new_range); | |
969bf05e AS |
8462 | } |
8463 | ||
3f50f132 | 8464 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 8465 | { |
3f50f132 JF |
8466 | struct tnum subreg = tnum_subreg(reg->var_off); |
8467 | s32 sval = (s32)val; | |
a72dafaf | 8468 | |
3f50f132 JF |
8469 | switch (opcode) { |
8470 | case BPF_JEQ: | |
8471 | if (tnum_is_const(subreg)) | |
8472 | return !!tnum_equals_const(subreg, val); | |
8473 | break; | |
8474 | case BPF_JNE: | |
8475 | if (tnum_is_const(subreg)) | |
8476 | return !tnum_equals_const(subreg, val); | |
8477 | break; | |
8478 | case BPF_JSET: | |
8479 | if ((~subreg.mask & subreg.value) & val) | |
8480 | return 1; | |
8481 | if (!((subreg.mask | subreg.value) & val)) | |
8482 | return 0; | |
8483 | break; | |
8484 | case BPF_JGT: | |
8485 | if (reg->u32_min_value > val) | |
8486 | return 1; | |
8487 | else if (reg->u32_max_value <= val) | |
8488 | return 0; | |
8489 | break; | |
8490 | case BPF_JSGT: | |
8491 | if (reg->s32_min_value > sval) | |
8492 | return 1; | |
ee114dd6 | 8493 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
8494 | return 0; |
8495 | break; | |
8496 | case BPF_JLT: | |
8497 | if (reg->u32_max_value < val) | |
8498 | return 1; | |
8499 | else if (reg->u32_min_value >= val) | |
8500 | return 0; | |
8501 | break; | |
8502 | case BPF_JSLT: | |
8503 | if (reg->s32_max_value < sval) | |
8504 | return 1; | |
8505 | else if (reg->s32_min_value >= sval) | |
8506 | return 0; | |
8507 | break; | |
8508 | case BPF_JGE: | |
8509 | if (reg->u32_min_value >= val) | |
8510 | return 1; | |
8511 | else if (reg->u32_max_value < val) | |
8512 | return 0; | |
8513 | break; | |
8514 | case BPF_JSGE: | |
8515 | if (reg->s32_min_value >= sval) | |
8516 | return 1; | |
8517 | else if (reg->s32_max_value < sval) | |
8518 | return 0; | |
8519 | break; | |
8520 | case BPF_JLE: | |
8521 | if (reg->u32_max_value <= val) | |
8522 | return 1; | |
8523 | else if (reg->u32_min_value > val) | |
8524 | return 0; | |
8525 | break; | |
8526 | case BPF_JSLE: | |
8527 | if (reg->s32_max_value <= sval) | |
8528 | return 1; | |
8529 | else if (reg->s32_min_value > sval) | |
8530 | return 0; | |
8531 | break; | |
8532 | } | |
4f7b3e82 | 8533 | |
3f50f132 JF |
8534 | return -1; |
8535 | } | |
092ed096 | 8536 | |
3f50f132 JF |
8537 | |
8538 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
8539 | { | |
8540 | s64 sval = (s64)val; | |
a72dafaf | 8541 | |
4f7b3e82 AS |
8542 | switch (opcode) { |
8543 | case BPF_JEQ: | |
8544 | if (tnum_is_const(reg->var_off)) | |
8545 | return !!tnum_equals_const(reg->var_off, val); | |
8546 | break; | |
8547 | case BPF_JNE: | |
8548 | if (tnum_is_const(reg->var_off)) | |
8549 | return !tnum_equals_const(reg->var_off, val); | |
8550 | break; | |
960ea056 JK |
8551 | case BPF_JSET: |
8552 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
8553 | return 1; | |
8554 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
8555 | return 0; | |
8556 | break; | |
4f7b3e82 AS |
8557 | case BPF_JGT: |
8558 | if (reg->umin_value > val) | |
8559 | return 1; | |
8560 | else if (reg->umax_value <= val) | |
8561 | return 0; | |
8562 | break; | |
8563 | case BPF_JSGT: | |
a72dafaf | 8564 | if (reg->smin_value > sval) |
4f7b3e82 | 8565 | return 1; |
ee114dd6 | 8566 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
8567 | return 0; |
8568 | break; | |
8569 | case BPF_JLT: | |
8570 | if (reg->umax_value < val) | |
8571 | return 1; | |
8572 | else if (reg->umin_value >= val) | |
8573 | return 0; | |
8574 | break; | |
8575 | case BPF_JSLT: | |
a72dafaf | 8576 | if (reg->smax_value < sval) |
4f7b3e82 | 8577 | return 1; |
a72dafaf | 8578 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
8579 | return 0; |
8580 | break; | |
8581 | case BPF_JGE: | |
8582 | if (reg->umin_value >= val) | |
8583 | return 1; | |
8584 | else if (reg->umax_value < val) | |
8585 | return 0; | |
8586 | break; | |
8587 | case BPF_JSGE: | |
a72dafaf | 8588 | if (reg->smin_value >= sval) |
4f7b3e82 | 8589 | return 1; |
a72dafaf | 8590 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
8591 | return 0; |
8592 | break; | |
8593 | case BPF_JLE: | |
8594 | if (reg->umax_value <= val) | |
8595 | return 1; | |
8596 | else if (reg->umin_value > val) | |
8597 | return 0; | |
8598 | break; | |
8599 | case BPF_JSLE: | |
a72dafaf | 8600 | if (reg->smax_value <= sval) |
4f7b3e82 | 8601 | return 1; |
a72dafaf | 8602 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
8603 | return 0; |
8604 | break; | |
8605 | } | |
8606 | ||
8607 | return -1; | |
8608 | } | |
8609 | ||
3f50f132 JF |
8610 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
8611 | * and return: | |
8612 | * 1 - branch will be taken and "goto target" will be executed | |
8613 | * 0 - branch will not be taken and fall-through to next insn | |
8614 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
8615 | * range [0,10] | |
604dca5e | 8616 | */ |
3f50f132 JF |
8617 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
8618 | bool is_jmp32) | |
604dca5e | 8619 | { |
cac616db JF |
8620 | if (__is_pointer_value(false, reg)) { |
8621 | if (!reg_type_not_null(reg->type)) | |
8622 | return -1; | |
8623 | ||
8624 | /* If pointer is valid tests against zero will fail so we can | |
8625 | * use this to direct branch taken. | |
8626 | */ | |
8627 | if (val != 0) | |
8628 | return -1; | |
8629 | ||
8630 | switch (opcode) { | |
8631 | case BPF_JEQ: | |
8632 | return 0; | |
8633 | case BPF_JNE: | |
8634 | return 1; | |
8635 | default: | |
8636 | return -1; | |
8637 | } | |
8638 | } | |
604dca5e | 8639 | |
3f50f132 JF |
8640 | if (is_jmp32) |
8641 | return is_branch32_taken(reg, val, opcode); | |
8642 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
8643 | } |
8644 | ||
6d94e741 AS |
8645 | static int flip_opcode(u32 opcode) |
8646 | { | |
8647 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
8648 | static const u8 opcode_flip[16] = { | |
8649 | /* these stay the same */ | |
8650 | [BPF_JEQ >> 4] = BPF_JEQ, | |
8651 | [BPF_JNE >> 4] = BPF_JNE, | |
8652 | [BPF_JSET >> 4] = BPF_JSET, | |
8653 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
8654 | [BPF_JGE >> 4] = BPF_JLE, | |
8655 | [BPF_JGT >> 4] = BPF_JLT, | |
8656 | [BPF_JLE >> 4] = BPF_JGE, | |
8657 | [BPF_JLT >> 4] = BPF_JGT, | |
8658 | [BPF_JSGE >> 4] = BPF_JSLE, | |
8659 | [BPF_JSGT >> 4] = BPF_JSLT, | |
8660 | [BPF_JSLE >> 4] = BPF_JSGE, | |
8661 | [BPF_JSLT >> 4] = BPF_JSGT | |
8662 | }; | |
8663 | return opcode_flip[opcode >> 4]; | |
8664 | } | |
8665 | ||
8666 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
8667 | struct bpf_reg_state *src_reg, | |
8668 | u8 opcode) | |
8669 | { | |
8670 | struct bpf_reg_state *pkt; | |
8671 | ||
8672 | if (src_reg->type == PTR_TO_PACKET_END) { | |
8673 | pkt = dst_reg; | |
8674 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
8675 | pkt = src_reg; | |
8676 | opcode = flip_opcode(opcode); | |
8677 | } else { | |
8678 | return -1; | |
8679 | } | |
8680 | ||
8681 | if (pkt->range >= 0) | |
8682 | return -1; | |
8683 | ||
8684 | switch (opcode) { | |
8685 | case BPF_JLE: | |
8686 | /* pkt <= pkt_end */ | |
8687 | fallthrough; | |
8688 | case BPF_JGT: | |
8689 | /* pkt > pkt_end */ | |
8690 | if (pkt->range == BEYOND_PKT_END) | |
8691 | /* pkt has at last one extra byte beyond pkt_end */ | |
8692 | return opcode == BPF_JGT; | |
8693 | break; | |
8694 | case BPF_JLT: | |
8695 | /* pkt < pkt_end */ | |
8696 | fallthrough; | |
8697 | case BPF_JGE: | |
8698 | /* pkt >= pkt_end */ | |
8699 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
8700 | return opcode == BPF_JGE; | |
8701 | break; | |
8702 | } | |
8703 | return -1; | |
8704 | } | |
8705 | ||
48461135 JB |
8706 | /* Adjusts the register min/max values in the case that the dst_reg is the |
8707 | * variable register that we are working on, and src_reg is a constant or we're | |
8708 | * simply doing a BPF_K check. | |
f1174f77 | 8709 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
8710 | */ |
8711 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
8712 | struct bpf_reg_state *false_reg, |
8713 | u64 val, u32 val32, | |
092ed096 | 8714 | u8 opcode, bool is_jmp32) |
48461135 | 8715 | { |
3f50f132 JF |
8716 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
8717 | struct tnum false_64off = false_reg->var_off; | |
8718 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
8719 | struct tnum true_64off = true_reg->var_off; | |
8720 | s64 sval = (s64)val; | |
8721 | s32 sval32 = (s32)val32; | |
a72dafaf | 8722 | |
f1174f77 EC |
8723 | /* If the dst_reg is a pointer, we can't learn anything about its |
8724 | * variable offset from the compare (unless src_reg were a pointer into | |
8725 | * the same object, but we don't bother with that. | |
8726 | * Since false_reg and true_reg have the same type by construction, we | |
8727 | * only need to check one of them for pointerness. | |
8728 | */ | |
8729 | if (__is_pointer_value(false, false_reg)) | |
8730 | return; | |
4cabc5b1 | 8731 | |
48461135 JB |
8732 | switch (opcode) { |
8733 | case BPF_JEQ: | |
48461135 | 8734 | case BPF_JNE: |
a72dafaf JW |
8735 | { |
8736 | struct bpf_reg_state *reg = | |
8737 | opcode == BPF_JEQ ? true_reg : false_reg; | |
8738 | ||
e688c3db AS |
8739 | /* JEQ/JNE comparison doesn't change the register equivalence. |
8740 | * r1 = r2; | |
8741 | * if (r1 == 42) goto label; | |
8742 | * ... | |
8743 | * label: // here both r1 and r2 are known to be 42. | |
8744 | * | |
8745 | * Hence when marking register as known preserve it's ID. | |
48461135 | 8746 | */ |
3f50f132 JF |
8747 | if (is_jmp32) |
8748 | __mark_reg32_known(reg, val32); | |
8749 | else | |
e688c3db | 8750 | ___mark_reg_known(reg, val); |
48461135 | 8751 | break; |
a72dafaf | 8752 | } |
960ea056 | 8753 | case BPF_JSET: |
3f50f132 JF |
8754 | if (is_jmp32) { |
8755 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
8756 | if (is_power_of_2(val32)) | |
8757 | true_32off = tnum_or(true_32off, | |
8758 | tnum_const(val32)); | |
8759 | } else { | |
8760 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
8761 | if (is_power_of_2(val)) | |
8762 | true_64off = tnum_or(true_64off, | |
8763 | tnum_const(val)); | |
8764 | } | |
960ea056 | 8765 | break; |
48461135 | 8766 | case BPF_JGE: |
a72dafaf JW |
8767 | case BPF_JGT: |
8768 | { | |
3f50f132 JF |
8769 | if (is_jmp32) { |
8770 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
8771 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
8772 | ||
8773 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
8774 | false_umax); | |
8775 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
8776 | true_umin); | |
8777 | } else { | |
8778 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
8779 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
8780 | ||
8781 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
8782 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
8783 | } | |
b03c9f9f | 8784 | break; |
a72dafaf | 8785 | } |
48461135 | 8786 | case BPF_JSGE: |
a72dafaf JW |
8787 | case BPF_JSGT: |
8788 | { | |
3f50f132 JF |
8789 | if (is_jmp32) { |
8790 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
8791 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 8792 | |
3f50f132 JF |
8793 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
8794 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
8795 | } else { | |
8796 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
8797 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
8798 | ||
8799 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
8800 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
8801 | } | |
48461135 | 8802 | break; |
a72dafaf | 8803 | } |
b4e432f1 | 8804 | case BPF_JLE: |
a72dafaf JW |
8805 | case BPF_JLT: |
8806 | { | |
3f50f132 JF |
8807 | if (is_jmp32) { |
8808 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
8809 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
8810 | ||
8811 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
8812 | false_umin); | |
8813 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
8814 | true_umax); | |
8815 | } else { | |
8816 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
8817 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
8818 | ||
8819 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
8820 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
8821 | } | |
b4e432f1 | 8822 | break; |
a72dafaf | 8823 | } |
b4e432f1 | 8824 | case BPF_JSLE: |
a72dafaf JW |
8825 | case BPF_JSLT: |
8826 | { | |
3f50f132 JF |
8827 | if (is_jmp32) { |
8828 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
8829 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 8830 | |
3f50f132 JF |
8831 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
8832 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
8833 | } else { | |
8834 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
8835 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
8836 | ||
8837 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
8838 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
8839 | } | |
b4e432f1 | 8840 | break; |
a72dafaf | 8841 | } |
48461135 | 8842 | default: |
0fc31b10 | 8843 | return; |
48461135 JB |
8844 | } |
8845 | ||
3f50f132 JF |
8846 | if (is_jmp32) { |
8847 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
8848 | tnum_subreg(false_32off)); | |
8849 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
8850 | tnum_subreg(true_32off)); | |
8851 | __reg_combine_32_into_64(false_reg); | |
8852 | __reg_combine_32_into_64(true_reg); | |
8853 | } else { | |
8854 | false_reg->var_off = false_64off; | |
8855 | true_reg->var_off = true_64off; | |
8856 | __reg_combine_64_into_32(false_reg); | |
8857 | __reg_combine_64_into_32(true_reg); | |
8858 | } | |
48461135 JB |
8859 | } |
8860 | ||
f1174f77 EC |
8861 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
8862 | * the variable reg. | |
48461135 JB |
8863 | */ |
8864 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
8865 | struct bpf_reg_state *false_reg, |
8866 | u64 val, u32 val32, | |
092ed096 | 8867 | u8 opcode, bool is_jmp32) |
48461135 | 8868 | { |
6d94e741 | 8869 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
8870 | /* This uses zero as "not present in table"; luckily the zero opcode, |
8871 | * BPF_JA, can't get here. | |
b03c9f9f | 8872 | */ |
0fc31b10 | 8873 | if (opcode) |
3f50f132 | 8874 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
8875 | } |
8876 | ||
8877 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
8878 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
8879 | struct bpf_reg_state *dst_reg) | |
8880 | { | |
b03c9f9f EC |
8881 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
8882 | dst_reg->umin_value); | |
8883 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
8884 | dst_reg->umax_value); | |
8885 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
8886 | dst_reg->smin_value); | |
8887 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
8888 | dst_reg->smax_value); | |
f1174f77 EC |
8889 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
8890 | dst_reg->var_off); | |
b03c9f9f EC |
8891 | /* We might have learned new bounds from the var_off. */ |
8892 | __update_reg_bounds(src_reg); | |
8893 | __update_reg_bounds(dst_reg); | |
8894 | /* We might have learned something about the sign bit. */ | |
8895 | __reg_deduce_bounds(src_reg); | |
8896 | __reg_deduce_bounds(dst_reg); | |
8897 | /* We might have learned some bits from the bounds. */ | |
8898 | __reg_bound_offset(src_reg); | |
8899 | __reg_bound_offset(dst_reg); | |
8900 | /* Intersecting with the old var_off might have improved our bounds | |
8901 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
8902 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
8903 | */ | |
8904 | __update_reg_bounds(src_reg); | |
8905 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
8906 | } |
8907 | ||
8908 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
8909 | struct bpf_reg_state *true_dst, | |
8910 | struct bpf_reg_state *false_src, | |
8911 | struct bpf_reg_state *false_dst, | |
8912 | u8 opcode) | |
8913 | { | |
8914 | switch (opcode) { | |
8915 | case BPF_JEQ: | |
8916 | __reg_combine_min_max(true_src, true_dst); | |
8917 | break; | |
8918 | case BPF_JNE: | |
8919 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 8920 | break; |
4cabc5b1 | 8921 | } |
48461135 JB |
8922 | } |
8923 | ||
fd978bf7 JS |
8924 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
8925 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 8926 | bool is_null) |
57a09bf0 | 8927 | { |
93c230e3 MKL |
8928 | if (reg_type_may_be_null(reg->type) && reg->id == id && |
8929 | !WARN_ON_ONCE(!reg->id)) { | |
f1174f77 EC |
8930 | /* Old offset (both fixed and variable parts) should |
8931 | * have been known-zero, because we don't allow pointer | |
8932 | * arithmetic on pointers that might be NULL. | |
8933 | */ | |
b03c9f9f EC |
8934 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
8935 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 8936 | reg->off)) { |
b03c9f9f EC |
8937 | __mark_reg_known_zero(reg); |
8938 | reg->off = 0; | |
f1174f77 EC |
8939 | } |
8940 | if (is_null) { | |
8941 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
8942 | /* We don't need id and ref_obj_id from this point |
8943 | * onwards anymore, thus we should better reset it, | |
8944 | * so that state pruning has chances to take effect. | |
8945 | */ | |
8946 | reg->id = 0; | |
8947 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
8948 | |
8949 | return; | |
8950 | } | |
8951 | ||
8952 | mark_ptr_not_null_reg(reg); | |
8953 | ||
8954 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 MKL |
8955 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
8956 | * in release_reg_references(). | |
8957 | * | |
8958 | * reg->id is still used by spin_lock ptr. Other | |
8959 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
8960 | */ |
8961 | reg->id = 0; | |
56f668df | 8962 | } |
57a09bf0 TG |
8963 | } |
8964 | } | |
8965 | ||
c6a9efa1 PC |
8966 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
8967 | bool is_null) | |
8968 | { | |
8969 | struct bpf_reg_state *reg; | |
8970 | int i; | |
8971 | ||
8972 | for (i = 0; i < MAX_BPF_REG; i++) | |
8973 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
8974 | ||
8975 | bpf_for_each_spilled_reg(i, state, reg) { | |
8976 | if (!reg) | |
8977 | continue; | |
8978 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
8979 | } | |
8980 | } | |
8981 | ||
57a09bf0 TG |
8982 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
8983 | * be folded together at some point. | |
8984 | */ | |
840b9615 JS |
8985 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
8986 | bool is_null) | |
57a09bf0 | 8987 | { |
f4d7e40a | 8988 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 8989 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 8990 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 8991 | u32 id = regs[regno].id; |
c6a9efa1 | 8992 | int i; |
57a09bf0 | 8993 | |
1b986589 MKL |
8994 | if (ref_obj_id && ref_obj_id == id && is_null) |
8995 | /* regs[regno] is in the " == NULL" branch. | |
8996 | * No one could have freed the reference state before | |
8997 | * doing the NULL check. | |
8998 | */ | |
8999 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 9000 | |
c6a9efa1 PC |
9001 | for (i = 0; i <= vstate->curframe; i++) |
9002 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
9003 | } |
9004 | ||
5beca081 DB |
9005 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
9006 | struct bpf_reg_state *dst_reg, | |
9007 | struct bpf_reg_state *src_reg, | |
9008 | struct bpf_verifier_state *this_branch, | |
9009 | struct bpf_verifier_state *other_branch) | |
9010 | { | |
9011 | if (BPF_SRC(insn->code) != BPF_X) | |
9012 | return false; | |
9013 | ||
092ed096 JW |
9014 | /* Pointers are always 64-bit. */ |
9015 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
9016 | return false; | |
9017 | ||
5beca081 DB |
9018 | switch (BPF_OP(insn->code)) { |
9019 | case BPF_JGT: | |
9020 | if ((dst_reg->type == PTR_TO_PACKET && | |
9021 | src_reg->type == PTR_TO_PACKET_END) || | |
9022 | (dst_reg->type == PTR_TO_PACKET_META && | |
9023 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9024 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
9025 | find_good_pkt_pointers(this_branch, dst_reg, | |
9026 | dst_reg->type, false); | |
6d94e741 | 9027 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
9028 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9029 | src_reg->type == PTR_TO_PACKET) || | |
9030 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9031 | src_reg->type == PTR_TO_PACKET_META)) { | |
9032 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
9033 | find_good_pkt_pointers(other_branch, src_reg, | |
9034 | src_reg->type, true); | |
6d94e741 | 9035 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
9036 | } else { |
9037 | return false; | |
9038 | } | |
9039 | break; | |
9040 | case BPF_JLT: | |
9041 | if ((dst_reg->type == PTR_TO_PACKET && | |
9042 | src_reg->type == PTR_TO_PACKET_END) || | |
9043 | (dst_reg->type == PTR_TO_PACKET_META && | |
9044 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9045 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
9046 | find_good_pkt_pointers(other_branch, dst_reg, | |
9047 | dst_reg->type, true); | |
6d94e741 | 9048 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
9049 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9050 | src_reg->type == PTR_TO_PACKET) || | |
9051 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9052 | src_reg->type == PTR_TO_PACKET_META)) { | |
9053 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
9054 | find_good_pkt_pointers(this_branch, src_reg, | |
9055 | src_reg->type, false); | |
6d94e741 | 9056 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
9057 | } else { |
9058 | return false; | |
9059 | } | |
9060 | break; | |
9061 | case BPF_JGE: | |
9062 | if ((dst_reg->type == PTR_TO_PACKET && | |
9063 | src_reg->type == PTR_TO_PACKET_END) || | |
9064 | (dst_reg->type == PTR_TO_PACKET_META && | |
9065 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9066 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
9067 | find_good_pkt_pointers(this_branch, dst_reg, | |
9068 | dst_reg->type, true); | |
6d94e741 | 9069 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
9070 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9071 | src_reg->type == PTR_TO_PACKET) || | |
9072 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9073 | src_reg->type == PTR_TO_PACKET_META)) { | |
9074 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
9075 | find_good_pkt_pointers(other_branch, src_reg, | |
9076 | src_reg->type, false); | |
6d94e741 | 9077 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
9078 | } else { |
9079 | return false; | |
9080 | } | |
9081 | break; | |
9082 | case BPF_JLE: | |
9083 | if ((dst_reg->type == PTR_TO_PACKET && | |
9084 | src_reg->type == PTR_TO_PACKET_END) || | |
9085 | (dst_reg->type == PTR_TO_PACKET_META && | |
9086 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9087 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
9088 | find_good_pkt_pointers(other_branch, dst_reg, | |
9089 | dst_reg->type, false); | |
6d94e741 | 9090 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
9091 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9092 | src_reg->type == PTR_TO_PACKET) || | |
9093 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9094 | src_reg->type == PTR_TO_PACKET_META)) { | |
9095 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
9096 | find_good_pkt_pointers(this_branch, src_reg, | |
9097 | src_reg->type, true); | |
6d94e741 | 9098 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
9099 | } else { |
9100 | return false; | |
9101 | } | |
9102 | break; | |
9103 | default: | |
9104 | return false; | |
9105 | } | |
9106 | ||
9107 | return true; | |
9108 | } | |
9109 | ||
75748837 AS |
9110 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
9111 | struct bpf_reg_state *known_reg) | |
9112 | { | |
9113 | struct bpf_func_state *state; | |
9114 | struct bpf_reg_state *reg; | |
9115 | int i, j; | |
9116 | ||
9117 | for (i = 0; i <= vstate->curframe; i++) { | |
9118 | state = vstate->frame[i]; | |
9119 | for (j = 0; j < MAX_BPF_REG; j++) { | |
9120 | reg = &state->regs[j]; | |
9121 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
9122 | *reg = *known_reg; | |
9123 | } | |
9124 | ||
9125 | bpf_for_each_spilled_reg(j, state, reg) { | |
9126 | if (!reg) | |
9127 | continue; | |
9128 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
9129 | *reg = *known_reg; | |
9130 | } | |
9131 | } | |
9132 | } | |
9133 | ||
58e2af8b | 9134 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
9135 | struct bpf_insn *insn, int *insn_idx) |
9136 | { | |
f4d7e40a AS |
9137 | struct bpf_verifier_state *this_branch = env->cur_state; |
9138 | struct bpf_verifier_state *other_branch; | |
9139 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 9140 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 9141 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 9142 | bool is_jmp32; |
fb8d251e | 9143 | int pred = -1; |
17a52670 AS |
9144 | int err; |
9145 | ||
092ed096 JW |
9146 | /* Only conditional jumps are expected to reach here. */ |
9147 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
9148 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
9149 | return -EINVAL; |
9150 | } | |
9151 | ||
9152 | if (BPF_SRC(insn->code) == BPF_X) { | |
9153 | if (insn->imm != 0) { | |
092ed096 | 9154 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
9155 | return -EINVAL; |
9156 | } | |
9157 | ||
9158 | /* check src1 operand */ | |
dc503a8a | 9159 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9160 | if (err) |
9161 | return err; | |
1be7f75d AS |
9162 | |
9163 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 9164 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
9165 | insn->src_reg); |
9166 | return -EACCES; | |
9167 | } | |
fb8d251e | 9168 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
9169 | } else { |
9170 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 9171 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
9172 | return -EINVAL; |
9173 | } | |
9174 | } | |
9175 | ||
9176 | /* check src2 operand */ | |
dc503a8a | 9177 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9178 | if (err) |
9179 | return err; | |
9180 | ||
1a0dc1ac | 9181 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 9182 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 9183 | |
3f50f132 JF |
9184 | if (BPF_SRC(insn->code) == BPF_K) { |
9185 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
9186 | } else if (src_reg->type == SCALAR_VALUE && | |
9187 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
9188 | pred = is_branch_taken(dst_reg, | |
9189 | tnum_subreg(src_reg->var_off).value, | |
9190 | opcode, | |
9191 | is_jmp32); | |
9192 | } else if (src_reg->type == SCALAR_VALUE && | |
9193 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
9194 | pred = is_branch_taken(dst_reg, | |
9195 | src_reg->var_off.value, | |
9196 | opcode, | |
9197 | is_jmp32); | |
6d94e741 AS |
9198 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
9199 | reg_is_pkt_pointer_any(src_reg) && | |
9200 | !is_jmp32) { | |
9201 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
9202 | } |
9203 | ||
b5dc0163 | 9204 | if (pred >= 0) { |
cac616db JF |
9205 | /* If we get here with a dst_reg pointer type it is because |
9206 | * above is_branch_taken() special cased the 0 comparison. | |
9207 | */ | |
9208 | if (!__is_pointer_value(false, dst_reg)) | |
9209 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
9210 | if (BPF_SRC(insn->code) == BPF_X && !err && |
9211 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
9212 | err = mark_chain_precision(env, insn->src_reg); |
9213 | if (err) | |
9214 | return err; | |
9215 | } | |
9183671a | 9216 | |
fb8d251e | 9217 | if (pred == 1) { |
9183671a DB |
9218 | /* Only follow the goto, ignore fall-through. If needed, push |
9219 | * the fall-through branch for simulation under speculative | |
9220 | * execution. | |
9221 | */ | |
9222 | if (!env->bypass_spec_v1 && | |
9223 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
9224 | *insn_idx)) | |
9225 | return -EFAULT; | |
fb8d251e AS |
9226 | *insn_idx += insn->off; |
9227 | return 0; | |
9228 | } else if (pred == 0) { | |
9183671a DB |
9229 | /* Only follow the fall-through branch, since that's where the |
9230 | * program will go. If needed, push the goto branch for | |
9231 | * simulation under speculative execution. | |
fb8d251e | 9232 | */ |
9183671a DB |
9233 | if (!env->bypass_spec_v1 && |
9234 | !sanitize_speculative_path(env, insn, | |
9235 | *insn_idx + insn->off + 1, | |
9236 | *insn_idx)) | |
9237 | return -EFAULT; | |
fb8d251e | 9238 | return 0; |
17a52670 AS |
9239 | } |
9240 | ||
979d63d5 DB |
9241 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
9242 | false); | |
17a52670 AS |
9243 | if (!other_branch) |
9244 | return -EFAULT; | |
f4d7e40a | 9245 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 9246 | |
48461135 JB |
9247 | /* detect if we are comparing against a constant value so we can adjust |
9248 | * our min/max values for our dst register. | |
f1174f77 EC |
9249 | * this is only legit if both are scalars (or pointers to the same |
9250 | * object, I suppose, but we don't support that right now), because | |
9251 | * otherwise the different base pointers mean the offsets aren't | |
9252 | * comparable. | |
48461135 JB |
9253 | */ |
9254 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 9255 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 9256 | |
f1174f77 | 9257 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
9258 | src_reg->type == SCALAR_VALUE) { |
9259 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
9260 | (is_jmp32 && |
9261 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 9262 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 9263 | dst_reg, |
3f50f132 JF |
9264 | src_reg->var_off.value, |
9265 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
9266 | opcode, is_jmp32); |
9267 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
9268 | (is_jmp32 && |
9269 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 9270 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 9271 | src_reg, |
3f50f132 JF |
9272 | dst_reg->var_off.value, |
9273 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
9274 | opcode, is_jmp32); |
9275 | else if (!is_jmp32 && | |
9276 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 9277 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
9278 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
9279 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 9280 | src_reg, dst_reg, opcode); |
e688c3db AS |
9281 | if (src_reg->id && |
9282 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
9283 | find_equal_scalars(this_branch, src_reg); |
9284 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
9285 | } | |
9286 | ||
f1174f77 EC |
9287 | } |
9288 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 9289 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
9290 | dst_reg, insn->imm, (u32)insn->imm, |
9291 | opcode, is_jmp32); | |
48461135 JB |
9292 | } |
9293 | ||
e688c3db AS |
9294 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
9295 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
9296 | find_equal_scalars(this_branch, dst_reg); |
9297 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
9298 | } | |
9299 | ||
092ed096 JW |
9300 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
9301 | * NOTE: these optimizations below are related with pointer comparison | |
9302 | * which will never be JMP32. | |
9303 | */ | |
9304 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 9305 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
9306 | reg_type_may_be_null(dst_reg->type)) { |
9307 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
9308 | * safe or unknown depending R == 0 or R != 0 conditional. |
9309 | */ | |
840b9615 JS |
9310 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
9311 | opcode == BPF_JNE); | |
9312 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
9313 | opcode == BPF_JEQ); | |
5beca081 DB |
9314 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
9315 | this_branch, other_branch) && | |
9316 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
9317 | verbose(env, "R%d pointer comparison prohibited\n", |
9318 | insn->dst_reg); | |
1be7f75d | 9319 | return -EACCES; |
17a52670 | 9320 | } |
06ee7115 | 9321 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 9322 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
9323 | return 0; |
9324 | } | |
9325 | ||
17a52670 | 9326 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 9327 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 9328 | { |
d8eca5bb | 9329 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 9330 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 9331 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 9332 | struct bpf_map *map; |
17a52670 AS |
9333 | int err; |
9334 | ||
9335 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 9336 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
9337 | return -EINVAL; |
9338 | } | |
9339 | if (insn->off != 0) { | |
61bd5218 | 9340 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
9341 | return -EINVAL; |
9342 | } | |
9343 | ||
dc503a8a | 9344 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
9345 | if (err) |
9346 | return err; | |
9347 | ||
4976b718 | 9348 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 9349 | if (insn->src_reg == 0) { |
6b173873 JK |
9350 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
9351 | ||
4976b718 | 9352 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 9353 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 9354 | return 0; |
6b173873 | 9355 | } |
17a52670 | 9356 | |
4976b718 HL |
9357 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
9358 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
9359 | ||
9360 | dst_reg->type = aux->btf_var.reg_type; | |
9361 | switch (dst_reg->type) { | |
9362 | case PTR_TO_MEM: | |
9363 | dst_reg->mem_size = aux->btf_var.mem_size; | |
9364 | break; | |
9365 | case PTR_TO_BTF_ID: | |
eaa6bcb7 | 9366 | case PTR_TO_PERCPU_BTF_ID: |
22dc4a0f | 9367 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
9368 | dst_reg->btf_id = aux->btf_var.btf_id; |
9369 | break; | |
9370 | default: | |
9371 | verbose(env, "bpf verifier is misconfigured\n"); | |
9372 | return -EFAULT; | |
9373 | } | |
9374 | return 0; | |
9375 | } | |
9376 | ||
69c087ba YS |
9377 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
9378 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
9379 | u32 subprogno = find_subprog(env, |
9380 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
9381 | |
9382 | if (!aux->func_info) { | |
9383 | verbose(env, "missing btf func_info\n"); | |
9384 | return -EINVAL; | |
9385 | } | |
9386 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
9387 | verbose(env, "callback function not static\n"); | |
9388 | return -EINVAL; | |
9389 | } | |
9390 | ||
9391 | dst_reg->type = PTR_TO_FUNC; | |
9392 | dst_reg->subprogno = subprogno; | |
9393 | return 0; | |
9394 | } | |
9395 | ||
d8eca5bb DB |
9396 | map = env->used_maps[aux->map_index]; |
9397 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 9398 | dst_reg->map_ptr = map; |
d8eca5bb | 9399 | |
387544bf AS |
9400 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
9401 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
9402 | dst_reg->type = PTR_TO_MAP_VALUE; |
9403 | dst_reg->off = aux->map_off; | |
d8eca5bb | 9404 | if (map_value_has_spin_lock(map)) |
4976b718 | 9405 | dst_reg->id = ++env->id_gen; |
387544bf AS |
9406 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
9407 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 9408 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
9409 | } else { |
9410 | verbose(env, "bpf verifier is misconfigured\n"); | |
9411 | return -EINVAL; | |
9412 | } | |
17a52670 | 9413 | |
17a52670 AS |
9414 | return 0; |
9415 | } | |
9416 | ||
96be4325 DB |
9417 | static bool may_access_skb(enum bpf_prog_type type) |
9418 | { | |
9419 | switch (type) { | |
9420 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
9421 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 9422 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
9423 | return true; |
9424 | default: | |
9425 | return false; | |
9426 | } | |
9427 | } | |
9428 | ||
ddd872bc AS |
9429 | /* verify safety of LD_ABS|LD_IND instructions: |
9430 | * - they can only appear in the programs where ctx == skb | |
9431 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
9432 | * preserve R6-R9, and store return value into R0 | |
9433 | * | |
9434 | * Implicit input: | |
9435 | * ctx == skb == R6 == CTX | |
9436 | * | |
9437 | * Explicit input: | |
9438 | * SRC == any register | |
9439 | * IMM == 32-bit immediate | |
9440 | * | |
9441 | * Output: | |
9442 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
9443 | */ | |
58e2af8b | 9444 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 9445 | { |
638f5b90 | 9446 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 9447 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 9448 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
9449 | int i, err; |
9450 | ||
7e40781c | 9451 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 9452 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
9453 | return -EINVAL; |
9454 | } | |
9455 | ||
e0cea7ce DB |
9456 | if (!env->ops->gen_ld_abs) { |
9457 | verbose(env, "bpf verifier is misconfigured\n"); | |
9458 | return -EINVAL; | |
9459 | } | |
9460 | ||
ddd872bc | 9461 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 9462 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 9463 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 9464 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
9465 | return -EINVAL; |
9466 | } | |
9467 | ||
9468 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 9469 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
9470 | if (err) |
9471 | return err; | |
9472 | ||
fd978bf7 JS |
9473 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
9474 | * gen_ld_abs() may terminate the program at runtime, leading to | |
9475 | * reference leak. | |
9476 | */ | |
9477 | err = check_reference_leak(env); | |
9478 | if (err) { | |
9479 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
9480 | return err; | |
9481 | } | |
9482 | ||
d83525ca AS |
9483 | if (env->cur_state->active_spin_lock) { |
9484 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
9485 | return -EINVAL; | |
9486 | } | |
9487 | ||
6d4f151a | 9488 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
9489 | verbose(env, |
9490 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
9491 | return -EINVAL; |
9492 | } | |
9493 | ||
9494 | if (mode == BPF_IND) { | |
9495 | /* check explicit source operand */ | |
dc503a8a | 9496 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
9497 | if (err) |
9498 | return err; | |
9499 | } | |
9500 | ||
6d4f151a DB |
9501 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
9502 | if (err < 0) | |
9503 | return err; | |
9504 | ||
ddd872bc | 9505 | /* reset caller saved regs to unreadable */ |
dc503a8a | 9506 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 9507 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
9508 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
9509 | } | |
ddd872bc AS |
9510 | |
9511 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
9512 | * the value fetched from the packet. |
9513 | * Already marked as written above. | |
ddd872bc | 9514 | */ |
61bd5218 | 9515 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
9516 | /* ld_abs load up to 32-bit skb data. */ |
9517 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
9518 | return 0; |
9519 | } | |
9520 | ||
390ee7e2 AS |
9521 | static int check_return_code(struct bpf_verifier_env *env) |
9522 | { | |
5cf1e914 | 9523 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 9524 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
9525 | struct bpf_reg_state *reg; |
9526 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 9527 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 9528 | int err; |
bfc6bb74 AS |
9529 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
9530 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 9531 | |
9e4e01df | 9532 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
f782e2c3 DB |
9533 | if (!is_subprog && |
9534 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || | |
7e40781c | 9535 | prog_type == BPF_PROG_TYPE_LSM) && |
27ae7997 MKL |
9536 | !prog->aux->attach_func_proto->type) |
9537 | return 0; | |
9538 | ||
8fb33b60 | 9539 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
9540 | * to return the value from eBPF program. |
9541 | * Make sure that it's readable at this time | |
9542 | * of bpf_exit, which means that program wrote | |
9543 | * something into it earlier | |
9544 | */ | |
9545 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
9546 | if (err) | |
9547 | return err; | |
9548 | ||
9549 | if (is_pointer_value(env, BPF_REG_0)) { | |
9550 | verbose(env, "R0 leaks addr as return value\n"); | |
9551 | return -EACCES; | |
9552 | } | |
390ee7e2 | 9553 | |
f782e2c3 | 9554 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
9555 | |
9556 | if (frame->in_async_callback_fn) { | |
9557 | /* enforce return zero from async callbacks like timer */ | |
9558 | if (reg->type != SCALAR_VALUE) { | |
9559 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
9560 | reg_type_str[reg->type]); | |
9561 | return -EINVAL; | |
9562 | } | |
9563 | ||
9564 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
9565 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
9566 | return -EINVAL; | |
9567 | } | |
9568 | return 0; | |
9569 | } | |
9570 | ||
f782e2c3 DB |
9571 | if (is_subprog) { |
9572 | if (reg->type != SCALAR_VALUE) { | |
9573 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
9574 | reg_type_str[reg->type]); | |
9575 | return -EINVAL; | |
9576 | } | |
9577 | return 0; | |
9578 | } | |
9579 | ||
7e40781c | 9580 | switch (prog_type) { |
983695fa DB |
9581 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
9582 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
9583 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
9584 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
9585 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
9586 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
9587 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 9588 | range = tnum_range(1, 1); |
77241217 SF |
9589 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
9590 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
9591 | range = tnum_range(0, 3); | |
ed4ed404 | 9592 | break; |
390ee7e2 | 9593 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 9594 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
9595 | range = tnum_range(0, 3); | |
9596 | enforce_attach_type_range = tnum_range(2, 3); | |
9597 | } | |
ed4ed404 | 9598 | break; |
390ee7e2 AS |
9599 | case BPF_PROG_TYPE_CGROUP_SOCK: |
9600 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 9601 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 9602 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 9603 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 9604 | break; |
15ab09bd AS |
9605 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
9606 | if (!env->prog->aux->attach_btf_id) | |
9607 | return 0; | |
9608 | range = tnum_const(0); | |
9609 | break; | |
15d83c4d | 9610 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
9611 | switch (env->prog->expected_attach_type) { |
9612 | case BPF_TRACE_FENTRY: | |
9613 | case BPF_TRACE_FEXIT: | |
9614 | range = tnum_const(0); | |
9615 | break; | |
9616 | case BPF_TRACE_RAW_TP: | |
9617 | case BPF_MODIFY_RETURN: | |
15d83c4d | 9618 | return 0; |
2ec0616e DB |
9619 | case BPF_TRACE_ITER: |
9620 | break; | |
e92888c7 YS |
9621 | default: |
9622 | return -ENOTSUPP; | |
9623 | } | |
15d83c4d | 9624 | break; |
e9ddbb77 JS |
9625 | case BPF_PROG_TYPE_SK_LOOKUP: |
9626 | range = tnum_range(SK_DROP, SK_PASS); | |
9627 | break; | |
e92888c7 YS |
9628 | case BPF_PROG_TYPE_EXT: |
9629 | /* freplace program can return anything as its return value | |
9630 | * depends on the to-be-replaced kernel func or bpf program. | |
9631 | */ | |
390ee7e2 AS |
9632 | default: |
9633 | return 0; | |
9634 | } | |
9635 | ||
390ee7e2 | 9636 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 9637 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
9638 | reg_type_str[reg->type]); |
9639 | return -EINVAL; | |
9640 | } | |
9641 | ||
9642 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 9643 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
390ee7e2 AS |
9644 | return -EINVAL; |
9645 | } | |
5cf1e914 | 9646 | |
9647 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
9648 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
9649 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
9650 | return 0; |
9651 | } | |
9652 | ||
475fb78f AS |
9653 | /* non-recursive DFS pseudo code |
9654 | * 1 procedure DFS-iterative(G,v): | |
9655 | * 2 label v as discovered | |
9656 | * 3 let S be a stack | |
9657 | * 4 S.push(v) | |
9658 | * 5 while S is not empty | |
9659 | * 6 t <- S.pop() | |
9660 | * 7 if t is what we're looking for: | |
9661 | * 8 return t | |
9662 | * 9 for all edges e in G.adjacentEdges(t) do | |
9663 | * 10 if edge e is already labelled | |
9664 | * 11 continue with the next edge | |
9665 | * 12 w <- G.adjacentVertex(t,e) | |
9666 | * 13 if vertex w is not discovered and not explored | |
9667 | * 14 label e as tree-edge | |
9668 | * 15 label w as discovered | |
9669 | * 16 S.push(w) | |
9670 | * 17 continue at 5 | |
9671 | * 18 else if vertex w is discovered | |
9672 | * 19 label e as back-edge | |
9673 | * 20 else | |
9674 | * 21 // vertex w is explored | |
9675 | * 22 label e as forward- or cross-edge | |
9676 | * 23 label t as explored | |
9677 | * 24 S.pop() | |
9678 | * | |
9679 | * convention: | |
9680 | * 0x10 - discovered | |
9681 | * 0x11 - discovered and fall-through edge labelled | |
9682 | * 0x12 - discovered and fall-through and branch edges labelled | |
9683 | * 0x20 - explored | |
9684 | */ | |
9685 | ||
9686 | enum { | |
9687 | DISCOVERED = 0x10, | |
9688 | EXPLORED = 0x20, | |
9689 | FALLTHROUGH = 1, | |
9690 | BRANCH = 2, | |
9691 | }; | |
9692 | ||
dc2a4ebc AS |
9693 | static u32 state_htab_size(struct bpf_verifier_env *env) |
9694 | { | |
9695 | return env->prog->len; | |
9696 | } | |
9697 | ||
5d839021 AS |
9698 | static struct bpf_verifier_state_list **explored_state( |
9699 | struct bpf_verifier_env *env, | |
9700 | int idx) | |
9701 | { | |
dc2a4ebc AS |
9702 | struct bpf_verifier_state *cur = env->cur_state; |
9703 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
9704 | ||
9705 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
9706 | } |
9707 | ||
9708 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
9709 | { | |
a8f500af | 9710 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 9711 | } |
f1bca824 | 9712 | |
59e2e27d WAF |
9713 | enum { |
9714 | DONE_EXPLORING = 0, | |
9715 | KEEP_EXPLORING = 1, | |
9716 | }; | |
9717 | ||
475fb78f AS |
9718 | /* t, w, e - match pseudo-code above: |
9719 | * t - index of current instruction | |
9720 | * w - next instruction | |
9721 | * e - edge | |
9722 | */ | |
2589726d AS |
9723 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
9724 | bool loop_ok) | |
475fb78f | 9725 | { |
7df737e9 AS |
9726 | int *insn_stack = env->cfg.insn_stack; |
9727 | int *insn_state = env->cfg.insn_state; | |
9728 | ||
475fb78f | 9729 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 9730 | return DONE_EXPLORING; |
475fb78f AS |
9731 | |
9732 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 9733 | return DONE_EXPLORING; |
475fb78f AS |
9734 | |
9735 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 9736 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 9737 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
9738 | return -EINVAL; |
9739 | } | |
9740 | ||
f1bca824 AS |
9741 | if (e == BRANCH) |
9742 | /* mark branch target for state pruning */ | |
5d839021 | 9743 | init_explored_state(env, w); |
f1bca824 | 9744 | |
475fb78f AS |
9745 | if (insn_state[w] == 0) { |
9746 | /* tree-edge */ | |
9747 | insn_state[t] = DISCOVERED | e; | |
9748 | insn_state[w] = DISCOVERED; | |
7df737e9 | 9749 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 9750 | return -E2BIG; |
7df737e9 | 9751 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 9752 | return KEEP_EXPLORING; |
475fb78f | 9753 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 9754 | if (loop_ok && env->bpf_capable) |
59e2e27d | 9755 | return DONE_EXPLORING; |
d9762e84 MKL |
9756 | verbose_linfo(env, t, "%d: ", t); |
9757 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 9758 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
9759 | return -EINVAL; |
9760 | } else if (insn_state[w] == EXPLORED) { | |
9761 | /* forward- or cross-edge */ | |
9762 | insn_state[t] = DISCOVERED | e; | |
9763 | } else { | |
61bd5218 | 9764 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
9765 | return -EFAULT; |
9766 | } | |
59e2e27d WAF |
9767 | return DONE_EXPLORING; |
9768 | } | |
9769 | ||
efdb22de YS |
9770 | static int visit_func_call_insn(int t, int insn_cnt, |
9771 | struct bpf_insn *insns, | |
9772 | struct bpf_verifier_env *env, | |
9773 | bool visit_callee) | |
9774 | { | |
9775 | int ret; | |
9776 | ||
9777 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
9778 | if (ret) | |
9779 | return ret; | |
9780 | ||
9781 | if (t + 1 < insn_cnt) | |
9782 | init_explored_state(env, t + 1); | |
9783 | if (visit_callee) { | |
9784 | init_explored_state(env, t); | |
86fc6ee6 AS |
9785 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
9786 | /* It's ok to allow recursion from CFG point of | |
9787 | * view. __check_func_call() will do the actual | |
9788 | * check. | |
9789 | */ | |
9790 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
9791 | } |
9792 | return ret; | |
9793 | } | |
9794 | ||
59e2e27d WAF |
9795 | /* Visits the instruction at index t and returns one of the following: |
9796 | * < 0 - an error occurred | |
9797 | * DONE_EXPLORING - the instruction was fully explored | |
9798 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
9799 | */ | |
9800 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) | |
9801 | { | |
9802 | struct bpf_insn *insns = env->prog->insnsi; | |
9803 | int ret; | |
9804 | ||
69c087ba YS |
9805 | if (bpf_pseudo_func(insns + t)) |
9806 | return visit_func_call_insn(t, insn_cnt, insns, env, true); | |
9807 | ||
59e2e27d WAF |
9808 | /* All non-branch instructions have a single fall-through edge. */ |
9809 | if (BPF_CLASS(insns[t].code) != BPF_JMP && | |
9810 | BPF_CLASS(insns[t].code) != BPF_JMP32) | |
9811 | return push_insn(t, t + 1, FALLTHROUGH, env, false); | |
9812 | ||
9813 | switch (BPF_OP(insns[t].code)) { | |
9814 | case BPF_EXIT: | |
9815 | return DONE_EXPLORING; | |
9816 | ||
9817 | case BPF_CALL: | |
bfc6bb74 AS |
9818 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
9819 | /* Mark this call insn to trigger is_state_visited() check | |
9820 | * before call itself is processed by __check_func_call(). | |
9821 | * Otherwise new async state will be pushed for further | |
9822 | * exploration. | |
9823 | */ | |
9824 | init_explored_state(env, t); | |
efdb22de YS |
9825 | return visit_func_call_insn(t, insn_cnt, insns, env, |
9826 | insns[t].src_reg == BPF_PSEUDO_CALL); | |
59e2e27d WAF |
9827 | |
9828 | case BPF_JA: | |
9829 | if (BPF_SRC(insns[t].code) != BPF_K) | |
9830 | return -EINVAL; | |
9831 | ||
9832 | /* unconditional jump with single edge */ | |
9833 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, | |
9834 | true); | |
9835 | if (ret) | |
9836 | return ret; | |
9837 | ||
9838 | /* unconditional jmp is not a good pruning point, | |
9839 | * but it's marked, since backtracking needs | |
9840 | * to record jmp history in is_state_visited(). | |
9841 | */ | |
9842 | init_explored_state(env, t + insns[t].off + 1); | |
9843 | /* tell verifier to check for equivalent states | |
9844 | * after every call and jump | |
9845 | */ | |
9846 | if (t + 1 < insn_cnt) | |
9847 | init_explored_state(env, t + 1); | |
9848 | ||
9849 | return ret; | |
9850 | ||
9851 | default: | |
9852 | /* conditional jump with two edges */ | |
9853 | init_explored_state(env, t); | |
9854 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); | |
9855 | if (ret) | |
9856 | return ret; | |
9857 | ||
9858 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); | |
9859 | } | |
475fb78f AS |
9860 | } |
9861 | ||
9862 | /* non-recursive depth-first-search to detect loops in BPF program | |
9863 | * loop == back-edge in directed graph | |
9864 | */ | |
58e2af8b | 9865 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 9866 | { |
475fb78f | 9867 | int insn_cnt = env->prog->len; |
7df737e9 | 9868 | int *insn_stack, *insn_state; |
475fb78f | 9869 | int ret = 0; |
59e2e27d | 9870 | int i; |
475fb78f | 9871 | |
7df737e9 | 9872 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
9873 | if (!insn_state) |
9874 | return -ENOMEM; | |
9875 | ||
7df737e9 | 9876 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 9877 | if (!insn_stack) { |
71dde681 | 9878 | kvfree(insn_state); |
475fb78f AS |
9879 | return -ENOMEM; |
9880 | } | |
9881 | ||
9882 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
9883 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 9884 | env->cfg.cur_stack = 1; |
475fb78f | 9885 | |
59e2e27d WAF |
9886 | while (env->cfg.cur_stack > 0) { |
9887 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 9888 | |
59e2e27d WAF |
9889 | ret = visit_insn(t, insn_cnt, env); |
9890 | switch (ret) { | |
9891 | case DONE_EXPLORING: | |
9892 | insn_state[t] = EXPLORED; | |
9893 | env->cfg.cur_stack--; | |
9894 | break; | |
9895 | case KEEP_EXPLORING: | |
9896 | break; | |
9897 | default: | |
9898 | if (ret > 0) { | |
9899 | verbose(env, "visit_insn internal bug\n"); | |
9900 | ret = -EFAULT; | |
475fb78f | 9901 | } |
475fb78f | 9902 | goto err_free; |
59e2e27d | 9903 | } |
475fb78f AS |
9904 | } |
9905 | ||
59e2e27d | 9906 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 9907 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
9908 | ret = -EFAULT; |
9909 | goto err_free; | |
9910 | } | |
475fb78f | 9911 | |
475fb78f AS |
9912 | for (i = 0; i < insn_cnt; i++) { |
9913 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 9914 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
9915 | ret = -EINVAL; |
9916 | goto err_free; | |
9917 | } | |
9918 | } | |
9919 | ret = 0; /* cfg looks good */ | |
9920 | ||
9921 | err_free: | |
71dde681 AS |
9922 | kvfree(insn_state); |
9923 | kvfree(insn_stack); | |
7df737e9 | 9924 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
9925 | return ret; |
9926 | } | |
9927 | ||
09b28d76 AS |
9928 | static int check_abnormal_return(struct bpf_verifier_env *env) |
9929 | { | |
9930 | int i; | |
9931 | ||
9932 | for (i = 1; i < env->subprog_cnt; i++) { | |
9933 | if (env->subprog_info[i].has_ld_abs) { | |
9934 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
9935 | return -EINVAL; | |
9936 | } | |
9937 | if (env->subprog_info[i].has_tail_call) { | |
9938 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
9939 | return -EINVAL; | |
9940 | } | |
9941 | } | |
9942 | return 0; | |
9943 | } | |
9944 | ||
838e9690 YS |
9945 | /* The minimum supported BTF func info size */ |
9946 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
9947 | #define MAX_FUNCINFO_REC_SIZE 252 | |
9948 | ||
c454a46b MKL |
9949 | static int check_btf_func(struct bpf_verifier_env *env, |
9950 | const union bpf_attr *attr, | |
af2ac3e1 | 9951 | bpfptr_t uattr) |
838e9690 | 9952 | { |
09b28d76 | 9953 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 9954 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 9955 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 9956 | struct bpf_func_info *krecord; |
8c1b6e69 | 9957 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
9958 | struct bpf_prog *prog; |
9959 | const struct btf *btf; | |
af2ac3e1 | 9960 | bpfptr_t urecord; |
d0b2818e | 9961 | u32 prev_offset = 0; |
09b28d76 | 9962 | bool scalar_return; |
e7ed83d6 | 9963 | int ret = -ENOMEM; |
838e9690 YS |
9964 | |
9965 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
9966 | if (!nfuncs) { |
9967 | if (check_abnormal_return(env)) | |
9968 | return -EINVAL; | |
838e9690 | 9969 | return 0; |
09b28d76 | 9970 | } |
838e9690 YS |
9971 | |
9972 | if (nfuncs != env->subprog_cnt) { | |
9973 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
9974 | return -EINVAL; | |
9975 | } | |
9976 | ||
9977 | urec_size = attr->func_info_rec_size; | |
9978 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
9979 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
9980 | urec_size % sizeof(u32)) { | |
9981 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
9982 | return -EINVAL; | |
9983 | } | |
9984 | ||
c454a46b MKL |
9985 | prog = env->prog; |
9986 | btf = prog->aux->btf; | |
838e9690 | 9987 | |
af2ac3e1 | 9988 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
9989 | min_size = min_t(u32, krec_size, urec_size); |
9990 | ||
ba64e7d8 | 9991 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
9992 | if (!krecord) |
9993 | return -ENOMEM; | |
8c1b6e69 AS |
9994 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
9995 | if (!info_aux) | |
9996 | goto err_free; | |
ba64e7d8 | 9997 | |
838e9690 YS |
9998 | for (i = 0; i < nfuncs; i++) { |
9999 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
10000 | if (ret) { | |
10001 | if (ret == -E2BIG) { | |
10002 | verbose(env, "nonzero tailing record in func info"); | |
10003 | /* set the size kernel expects so loader can zero | |
10004 | * out the rest of the record. | |
10005 | */ | |
af2ac3e1 AS |
10006 | if (copy_to_bpfptr_offset(uattr, |
10007 | offsetof(union bpf_attr, func_info_rec_size), | |
10008 | &min_size, sizeof(min_size))) | |
838e9690 YS |
10009 | ret = -EFAULT; |
10010 | } | |
c454a46b | 10011 | goto err_free; |
838e9690 YS |
10012 | } |
10013 | ||
af2ac3e1 | 10014 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 10015 | ret = -EFAULT; |
c454a46b | 10016 | goto err_free; |
838e9690 YS |
10017 | } |
10018 | ||
d30d42e0 | 10019 | /* check insn_off */ |
09b28d76 | 10020 | ret = -EINVAL; |
838e9690 | 10021 | if (i == 0) { |
d30d42e0 | 10022 | if (krecord[i].insn_off) { |
838e9690 | 10023 | verbose(env, |
d30d42e0 MKL |
10024 | "nonzero insn_off %u for the first func info record", |
10025 | krecord[i].insn_off); | |
c454a46b | 10026 | goto err_free; |
838e9690 | 10027 | } |
d30d42e0 | 10028 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
10029 | verbose(env, |
10030 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 10031 | krecord[i].insn_off, prev_offset); |
c454a46b | 10032 | goto err_free; |
838e9690 YS |
10033 | } |
10034 | ||
d30d42e0 | 10035 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 10036 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 10037 | goto err_free; |
838e9690 YS |
10038 | } |
10039 | ||
10040 | /* check type_id */ | |
ba64e7d8 | 10041 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 10042 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 10043 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 10044 | krecord[i].type_id); |
c454a46b | 10045 | goto err_free; |
838e9690 | 10046 | } |
51c39bb1 | 10047 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
10048 | |
10049 | func_proto = btf_type_by_id(btf, type->type); | |
10050 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
10051 | /* btf_func_check() already verified it during BTF load */ | |
10052 | goto err_free; | |
10053 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
10054 | scalar_return = | |
10055 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); | |
10056 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { | |
10057 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
10058 | goto err_free; | |
10059 | } | |
10060 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
10061 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
10062 | goto err_free; | |
10063 | } | |
10064 | ||
d30d42e0 | 10065 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 10066 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
10067 | } |
10068 | ||
ba64e7d8 YS |
10069 | prog->aux->func_info = krecord; |
10070 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 10071 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
10072 | return 0; |
10073 | ||
c454a46b | 10074 | err_free: |
ba64e7d8 | 10075 | kvfree(krecord); |
8c1b6e69 | 10076 | kfree(info_aux); |
838e9690 YS |
10077 | return ret; |
10078 | } | |
10079 | ||
ba64e7d8 YS |
10080 | static void adjust_btf_func(struct bpf_verifier_env *env) |
10081 | { | |
8c1b6e69 | 10082 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
10083 | int i; |
10084 | ||
8c1b6e69 | 10085 | if (!aux->func_info) |
ba64e7d8 YS |
10086 | return; |
10087 | ||
10088 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 10089 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
10090 | } |
10091 | ||
c454a46b MKL |
10092 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
10093 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
10094 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
10095 | ||
10096 | static int check_btf_line(struct bpf_verifier_env *env, | |
10097 | const union bpf_attr *attr, | |
af2ac3e1 | 10098 | bpfptr_t uattr) |
c454a46b MKL |
10099 | { |
10100 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
10101 | struct bpf_subprog_info *sub; | |
10102 | struct bpf_line_info *linfo; | |
10103 | struct bpf_prog *prog; | |
10104 | const struct btf *btf; | |
af2ac3e1 | 10105 | bpfptr_t ulinfo; |
c454a46b MKL |
10106 | int err; |
10107 | ||
10108 | nr_linfo = attr->line_info_cnt; | |
10109 | if (!nr_linfo) | |
10110 | return 0; | |
0e6491b5 BC |
10111 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
10112 | return -EINVAL; | |
c454a46b MKL |
10113 | |
10114 | rec_size = attr->line_info_rec_size; | |
10115 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
10116 | rec_size > MAX_LINEINFO_REC_SIZE || | |
10117 | rec_size & (sizeof(u32) - 1)) | |
10118 | return -EINVAL; | |
10119 | ||
10120 | /* Need to zero it in case the userspace may | |
10121 | * pass in a smaller bpf_line_info object. | |
10122 | */ | |
10123 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
10124 | GFP_KERNEL | __GFP_NOWARN); | |
10125 | if (!linfo) | |
10126 | return -ENOMEM; | |
10127 | ||
10128 | prog = env->prog; | |
10129 | btf = prog->aux->btf; | |
10130 | ||
10131 | s = 0; | |
10132 | sub = env->subprog_info; | |
af2ac3e1 | 10133 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
10134 | expected_size = sizeof(struct bpf_line_info); |
10135 | ncopy = min_t(u32, expected_size, rec_size); | |
10136 | for (i = 0; i < nr_linfo; i++) { | |
10137 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
10138 | if (err) { | |
10139 | if (err == -E2BIG) { | |
10140 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
10141 | if (copy_to_bpfptr_offset(uattr, |
10142 | offsetof(union bpf_attr, line_info_rec_size), | |
10143 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
10144 | err = -EFAULT; |
10145 | } | |
10146 | goto err_free; | |
10147 | } | |
10148 | ||
af2ac3e1 | 10149 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
10150 | err = -EFAULT; |
10151 | goto err_free; | |
10152 | } | |
10153 | ||
10154 | /* | |
10155 | * Check insn_off to ensure | |
10156 | * 1) strictly increasing AND | |
10157 | * 2) bounded by prog->len | |
10158 | * | |
10159 | * The linfo[0].insn_off == 0 check logically falls into | |
10160 | * the later "missing bpf_line_info for func..." case | |
10161 | * because the first linfo[0].insn_off must be the | |
10162 | * first sub also and the first sub must have | |
10163 | * subprog_info[0].start == 0. | |
10164 | */ | |
10165 | if ((i && linfo[i].insn_off <= prev_offset) || | |
10166 | linfo[i].insn_off >= prog->len) { | |
10167 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
10168 | i, linfo[i].insn_off, prev_offset, | |
10169 | prog->len); | |
10170 | err = -EINVAL; | |
10171 | goto err_free; | |
10172 | } | |
10173 | ||
fdbaa0be MKL |
10174 | if (!prog->insnsi[linfo[i].insn_off].code) { |
10175 | verbose(env, | |
10176 | "Invalid insn code at line_info[%u].insn_off\n", | |
10177 | i); | |
10178 | err = -EINVAL; | |
10179 | goto err_free; | |
10180 | } | |
10181 | ||
23127b33 MKL |
10182 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
10183 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
10184 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
10185 | err = -EINVAL; | |
10186 | goto err_free; | |
10187 | } | |
10188 | ||
10189 | if (s != env->subprog_cnt) { | |
10190 | if (linfo[i].insn_off == sub[s].start) { | |
10191 | sub[s].linfo_idx = i; | |
10192 | s++; | |
10193 | } else if (sub[s].start < linfo[i].insn_off) { | |
10194 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
10195 | err = -EINVAL; | |
10196 | goto err_free; | |
10197 | } | |
10198 | } | |
10199 | ||
10200 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 10201 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
10202 | } |
10203 | ||
10204 | if (s != env->subprog_cnt) { | |
10205 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
10206 | env->subprog_cnt - s, s); | |
10207 | err = -EINVAL; | |
10208 | goto err_free; | |
10209 | } | |
10210 | ||
10211 | prog->aux->linfo = linfo; | |
10212 | prog->aux->nr_linfo = nr_linfo; | |
10213 | ||
10214 | return 0; | |
10215 | ||
10216 | err_free: | |
10217 | kvfree(linfo); | |
10218 | return err; | |
10219 | } | |
10220 | ||
10221 | static int check_btf_info(struct bpf_verifier_env *env, | |
10222 | const union bpf_attr *attr, | |
af2ac3e1 | 10223 | bpfptr_t uattr) |
c454a46b MKL |
10224 | { |
10225 | struct btf *btf; | |
10226 | int err; | |
10227 | ||
09b28d76 AS |
10228 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
10229 | if (check_abnormal_return(env)) | |
10230 | return -EINVAL; | |
c454a46b | 10231 | return 0; |
09b28d76 | 10232 | } |
c454a46b MKL |
10233 | |
10234 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
10235 | if (IS_ERR(btf)) | |
10236 | return PTR_ERR(btf); | |
350a5c4d AS |
10237 | if (btf_is_kernel(btf)) { |
10238 | btf_put(btf); | |
10239 | return -EACCES; | |
10240 | } | |
c454a46b MKL |
10241 | env->prog->aux->btf = btf; |
10242 | ||
10243 | err = check_btf_func(env, attr, uattr); | |
10244 | if (err) | |
10245 | return err; | |
10246 | ||
10247 | err = check_btf_line(env, attr, uattr); | |
10248 | if (err) | |
10249 | return err; | |
10250 | ||
10251 | return 0; | |
ba64e7d8 YS |
10252 | } |
10253 | ||
f1174f77 EC |
10254 | /* check %cur's range satisfies %old's */ |
10255 | static bool range_within(struct bpf_reg_state *old, | |
10256 | struct bpf_reg_state *cur) | |
10257 | { | |
b03c9f9f EC |
10258 | return old->umin_value <= cur->umin_value && |
10259 | old->umax_value >= cur->umax_value && | |
10260 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
10261 | old->smax_value >= cur->smax_value && |
10262 | old->u32_min_value <= cur->u32_min_value && | |
10263 | old->u32_max_value >= cur->u32_max_value && | |
10264 | old->s32_min_value <= cur->s32_min_value && | |
10265 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
10266 | } |
10267 | ||
f1174f77 EC |
10268 | /* If in the old state two registers had the same id, then they need to have |
10269 | * the same id in the new state as well. But that id could be different from | |
10270 | * the old state, so we need to track the mapping from old to new ids. | |
10271 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
10272 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
10273 | * regs with a different old id could still have new id 9, we don't care about | |
10274 | * that. | |
10275 | * So we look through our idmap to see if this old id has been seen before. If | |
10276 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 10277 | */ |
c9e73e3d | 10278 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 10279 | { |
f1174f77 | 10280 | unsigned int i; |
969bf05e | 10281 | |
c9e73e3d | 10282 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
10283 | if (!idmap[i].old) { |
10284 | /* Reached an empty slot; haven't seen this id before */ | |
10285 | idmap[i].old = old_id; | |
10286 | idmap[i].cur = cur_id; | |
10287 | return true; | |
10288 | } | |
10289 | if (idmap[i].old == old_id) | |
10290 | return idmap[i].cur == cur_id; | |
10291 | } | |
10292 | /* We ran out of idmap slots, which should be impossible */ | |
10293 | WARN_ON_ONCE(1); | |
10294 | return false; | |
10295 | } | |
10296 | ||
9242b5f5 AS |
10297 | static void clean_func_state(struct bpf_verifier_env *env, |
10298 | struct bpf_func_state *st) | |
10299 | { | |
10300 | enum bpf_reg_liveness live; | |
10301 | int i, j; | |
10302 | ||
10303 | for (i = 0; i < BPF_REG_FP; i++) { | |
10304 | live = st->regs[i].live; | |
10305 | /* liveness must not touch this register anymore */ | |
10306 | st->regs[i].live |= REG_LIVE_DONE; | |
10307 | if (!(live & REG_LIVE_READ)) | |
10308 | /* since the register is unused, clear its state | |
10309 | * to make further comparison simpler | |
10310 | */ | |
f54c7898 | 10311 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
10312 | } |
10313 | ||
10314 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
10315 | live = st->stack[i].spilled_ptr.live; | |
10316 | /* liveness must not touch this stack slot anymore */ | |
10317 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
10318 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 10319 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
10320 | for (j = 0; j < BPF_REG_SIZE; j++) |
10321 | st->stack[i].slot_type[j] = STACK_INVALID; | |
10322 | } | |
10323 | } | |
10324 | } | |
10325 | ||
10326 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
10327 | struct bpf_verifier_state *st) | |
10328 | { | |
10329 | int i; | |
10330 | ||
10331 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
10332 | /* all regs in this state in all frames were already marked */ | |
10333 | return; | |
10334 | ||
10335 | for (i = 0; i <= st->curframe; i++) | |
10336 | clean_func_state(env, st->frame[i]); | |
10337 | } | |
10338 | ||
10339 | /* the parentage chains form a tree. | |
10340 | * the verifier states are added to state lists at given insn and | |
10341 | * pushed into state stack for future exploration. | |
10342 | * when the verifier reaches bpf_exit insn some of the verifer states | |
10343 | * stored in the state lists have their final liveness state already, | |
10344 | * but a lot of states will get revised from liveness point of view when | |
10345 | * the verifier explores other branches. | |
10346 | * Example: | |
10347 | * 1: r0 = 1 | |
10348 | * 2: if r1 == 100 goto pc+1 | |
10349 | * 3: r0 = 2 | |
10350 | * 4: exit | |
10351 | * when the verifier reaches exit insn the register r0 in the state list of | |
10352 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
10353 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
10354 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
10355 | * | |
10356 | * Since the verifier pushes the branch states as it sees them while exploring | |
10357 | * the program the condition of walking the branch instruction for the second | |
10358 | * time means that all states below this branch were already explored and | |
8fb33b60 | 10359 | * their final liveness marks are already propagated. |
9242b5f5 AS |
10360 | * Hence when the verifier completes the search of state list in is_state_visited() |
10361 | * we can call this clean_live_states() function to mark all liveness states | |
10362 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
10363 | * will not be used. | |
10364 | * This function also clears the registers and stack for states that !READ | |
10365 | * to simplify state merging. | |
10366 | * | |
10367 | * Important note here that walking the same branch instruction in the callee | |
10368 | * doesn't meant that the states are DONE. The verifier has to compare | |
10369 | * the callsites | |
10370 | */ | |
10371 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
10372 | struct bpf_verifier_state *cur) | |
10373 | { | |
10374 | struct bpf_verifier_state_list *sl; | |
10375 | int i; | |
10376 | ||
5d839021 | 10377 | sl = *explored_state(env, insn); |
a8f500af | 10378 | while (sl) { |
2589726d AS |
10379 | if (sl->state.branches) |
10380 | goto next; | |
dc2a4ebc AS |
10381 | if (sl->state.insn_idx != insn || |
10382 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
10383 | goto next; |
10384 | for (i = 0; i <= cur->curframe; i++) | |
10385 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
10386 | goto next; | |
10387 | clean_verifier_state(env, &sl->state); | |
10388 | next: | |
10389 | sl = sl->next; | |
10390 | } | |
10391 | } | |
10392 | ||
f1174f77 | 10393 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
10394 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
10395 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 10396 | { |
f4d7e40a AS |
10397 | bool equal; |
10398 | ||
dc503a8a EC |
10399 | if (!(rold->live & REG_LIVE_READ)) |
10400 | /* explored state didn't use this */ | |
10401 | return true; | |
10402 | ||
679c782d | 10403 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
10404 | |
10405 | if (rold->type == PTR_TO_STACK) | |
10406 | /* two stack pointers are equal only if they're pointing to | |
10407 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
10408 | */ | |
10409 | return equal && rold->frameno == rcur->frameno; | |
10410 | ||
10411 | if (equal) | |
969bf05e AS |
10412 | return true; |
10413 | ||
f1174f77 EC |
10414 | if (rold->type == NOT_INIT) |
10415 | /* explored state can't have used this */ | |
969bf05e | 10416 | return true; |
f1174f77 EC |
10417 | if (rcur->type == NOT_INIT) |
10418 | return false; | |
10419 | switch (rold->type) { | |
10420 | case SCALAR_VALUE: | |
e042aa53 DB |
10421 | if (env->explore_alu_limits) |
10422 | return false; | |
f1174f77 | 10423 | if (rcur->type == SCALAR_VALUE) { |
b5dc0163 AS |
10424 | if (!rold->precise && !rcur->precise) |
10425 | return true; | |
f1174f77 EC |
10426 | /* new val must satisfy old val knowledge */ |
10427 | return range_within(rold, rcur) && | |
10428 | tnum_in(rold->var_off, rcur->var_off); | |
10429 | } else { | |
179d1c56 JH |
10430 | /* We're trying to use a pointer in place of a scalar. |
10431 | * Even if the scalar was unbounded, this could lead to | |
10432 | * pointer leaks because scalars are allowed to leak | |
10433 | * while pointers are not. We could make this safe in | |
10434 | * special cases if root is calling us, but it's | |
10435 | * probably not worth the hassle. | |
f1174f77 | 10436 | */ |
179d1c56 | 10437 | return false; |
f1174f77 | 10438 | } |
69c087ba | 10439 | case PTR_TO_MAP_KEY: |
f1174f77 | 10440 | case PTR_TO_MAP_VALUE: |
1b688a19 EC |
10441 | /* If the new min/max/var_off satisfy the old ones and |
10442 | * everything else matches, we are OK. | |
d83525ca AS |
10443 | * 'id' is not compared, since it's only used for maps with |
10444 | * bpf_spin_lock inside map element and in such cases if | |
10445 | * the rest of the prog is valid for one map element then | |
10446 | * it's valid for all map elements regardless of the key | |
10447 | * used in bpf_map_lookup() | |
1b688a19 EC |
10448 | */ |
10449 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
10450 | range_within(rold, rcur) && | |
10451 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
10452 | case PTR_TO_MAP_VALUE_OR_NULL: |
10453 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
10454 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
10455 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
10456 | * checked, doing so could have affected others with the same | |
10457 | * id, and we can't check for that because we lost the id when | |
10458 | * we converted to a PTR_TO_MAP_VALUE. | |
10459 | */ | |
10460 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
10461 | return false; | |
10462 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
10463 | return false; | |
10464 | /* Check our ids match any regs they're supposed to */ | |
10465 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 10466 | case PTR_TO_PACKET_META: |
f1174f77 | 10467 | case PTR_TO_PACKET: |
de8f3a83 | 10468 | if (rcur->type != rold->type) |
f1174f77 EC |
10469 | return false; |
10470 | /* We must have at least as much range as the old ptr | |
10471 | * did, so that any accesses which were safe before are | |
10472 | * still safe. This is true even if old range < old off, | |
10473 | * since someone could have accessed through (ptr - k), or | |
10474 | * even done ptr -= k in a register, to get a safe access. | |
10475 | */ | |
10476 | if (rold->range > rcur->range) | |
10477 | return false; | |
10478 | /* If the offsets don't match, we can't trust our alignment; | |
10479 | * nor can we be sure that we won't fall out of range. | |
10480 | */ | |
10481 | if (rold->off != rcur->off) | |
10482 | return false; | |
10483 | /* id relations must be preserved */ | |
10484 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
10485 | return false; | |
10486 | /* new val must satisfy old val knowledge */ | |
10487 | return range_within(rold, rcur) && | |
10488 | tnum_in(rold->var_off, rcur->var_off); | |
10489 | case PTR_TO_CTX: | |
10490 | case CONST_PTR_TO_MAP: | |
f1174f77 | 10491 | case PTR_TO_PACKET_END: |
d58e468b | 10492 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
10493 | case PTR_TO_SOCKET: |
10494 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
10495 | case PTR_TO_SOCK_COMMON: |
10496 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
10497 | case PTR_TO_TCP_SOCK: |
10498 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 10499 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
10500 | /* Only valid matches are exact, which memcmp() above |
10501 | * would have accepted | |
10502 | */ | |
10503 | default: | |
10504 | /* Don't know what's going on, just say it's not safe */ | |
10505 | return false; | |
10506 | } | |
969bf05e | 10507 | |
f1174f77 EC |
10508 | /* Shouldn't get here; if we do, say it's not safe */ |
10509 | WARN_ON_ONCE(1); | |
969bf05e AS |
10510 | return false; |
10511 | } | |
10512 | ||
e042aa53 DB |
10513 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
10514 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
10515 | { |
10516 | int i, spi; | |
10517 | ||
638f5b90 AS |
10518 | /* walk slots of the explored stack and ignore any additional |
10519 | * slots in the current stack, since explored(safe) state | |
10520 | * didn't use them | |
10521 | */ | |
10522 | for (i = 0; i < old->allocated_stack; i++) { | |
10523 | spi = i / BPF_REG_SIZE; | |
10524 | ||
b233920c AS |
10525 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
10526 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 10527 | /* explored state didn't use this */ |
fd05e57b | 10528 | continue; |
b233920c | 10529 | } |
cc2b14d5 | 10530 | |
638f5b90 AS |
10531 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
10532 | continue; | |
19e2dbb7 AS |
10533 | |
10534 | /* explored stack has more populated slots than current stack | |
10535 | * and these slots were used | |
10536 | */ | |
10537 | if (i >= cur->allocated_stack) | |
10538 | return false; | |
10539 | ||
cc2b14d5 AS |
10540 | /* if old state was safe with misc data in the stack |
10541 | * it will be safe with zero-initialized stack. | |
10542 | * The opposite is not true | |
10543 | */ | |
10544 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
10545 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
10546 | continue; | |
638f5b90 AS |
10547 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
10548 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
10549 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 10550 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
10551 | * this verifier states are not equivalent, |
10552 | * return false to continue verification of this path | |
10553 | */ | |
10554 | return false; | |
27113c59 | 10555 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 10556 | continue; |
27113c59 | 10557 | if (!is_spilled_reg(&old->stack[spi])) |
638f5b90 | 10558 | continue; |
e042aa53 DB |
10559 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
10560 | &cur->stack[spi].spilled_ptr, idmap)) | |
638f5b90 AS |
10561 | /* when explored and current stack slot are both storing |
10562 | * spilled registers, check that stored pointers types | |
10563 | * are the same as well. | |
10564 | * Ex: explored safe path could have stored | |
10565 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
10566 | * but current path has stored: | |
10567 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
10568 | * such verifier states are not equivalent. | |
10569 | * return false to continue verification of this path | |
10570 | */ | |
10571 | return false; | |
10572 | } | |
10573 | return true; | |
10574 | } | |
10575 | ||
fd978bf7 JS |
10576 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
10577 | { | |
10578 | if (old->acquired_refs != cur->acquired_refs) | |
10579 | return false; | |
10580 | return !memcmp(old->refs, cur->refs, | |
10581 | sizeof(*old->refs) * old->acquired_refs); | |
10582 | } | |
10583 | ||
f1bca824 AS |
10584 | /* compare two verifier states |
10585 | * | |
10586 | * all states stored in state_list are known to be valid, since | |
10587 | * verifier reached 'bpf_exit' instruction through them | |
10588 | * | |
10589 | * this function is called when verifier exploring different branches of | |
10590 | * execution popped from the state stack. If it sees an old state that has | |
10591 | * more strict register state and more strict stack state then this execution | |
10592 | * branch doesn't need to be explored further, since verifier already | |
10593 | * concluded that more strict state leads to valid finish. | |
10594 | * | |
10595 | * Therefore two states are equivalent if register state is more conservative | |
10596 | * and explored stack state is more conservative than the current one. | |
10597 | * Example: | |
10598 | * explored current | |
10599 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
10600 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
10601 | * | |
10602 | * In other words if current stack state (one being explored) has more | |
10603 | * valid slots than old one that already passed validation, it means | |
10604 | * the verifier can stop exploring and conclude that current state is valid too | |
10605 | * | |
10606 | * Similarly with registers. If explored state has register type as invalid | |
10607 | * whereas register type in current state is meaningful, it means that | |
10608 | * the current state will reach 'bpf_exit' instruction safely | |
10609 | */ | |
c9e73e3d | 10610 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 10611 | struct bpf_func_state *cur) |
f1bca824 AS |
10612 | { |
10613 | int i; | |
10614 | ||
c9e73e3d LB |
10615 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
10616 | for (i = 0; i < MAX_BPF_REG; i++) | |
e042aa53 DB |
10617 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
10618 | env->idmap_scratch)) | |
c9e73e3d | 10619 | return false; |
f1bca824 | 10620 | |
e042aa53 | 10621 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 10622 | return false; |
fd978bf7 JS |
10623 | |
10624 | if (!refsafe(old, cur)) | |
c9e73e3d LB |
10625 | return false; |
10626 | ||
10627 | return true; | |
f1bca824 AS |
10628 | } |
10629 | ||
f4d7e40a AS |
10630 | static bool states_equal(struct bpf_verifier_env *env, |
10631 | struct bpf_verifier_state *old, | |
10632 | struct bpf_verifier_state *cur) | |
10633 | { | |
10634 | int i; | |
10635 | ||
10636 | if (old->curframe != cur->curframe) | |
10637 | return false; | |
10638 | ||
979d63d5 DB |
10639 | /* Verification state from speculative execution simulation |
10640 | * must never prune a non-speculative execution one. | |
10641 | */ | |
10642 | if (old->speculative && !cur->speculative) | |
10643 | return false; | |
10644 | ||
d83525ca AS |
10645 | if (old->active_spin_lock != cur->active_spin_lock) |
10646 | return false; | |
10647 | ||
f4d7e40a AS |
10648 | /* for states to be equal callsites have to be the same |
10649 | * and all frame states need to be equivalent | |
10650 | */ | |
10651 | for (i = 0; i <= old->curframe; i++) { | |
10652 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
10653 | return false; | |
c9e73e3d | 10654 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
10655 | return false; |
10656 | } | |
10657 | return true; | |
10658 | } | |
10659 | ||
5327ed3d JW |
10660 | /* Return 0 if no propagation happened. Return negative error code if error |
10661 | * happened. Otherwise, return the propagated bit. | |
10662 | */ | |
55e7f3b5 JW |
10663 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
10664 | struct bpf_reg_state *reg, | |
10665 | struct bpf_reg_state *parent_reg) | |
10666 | { | |
5327ed3d JW |
10667 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
10668 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
10669 | int err; |
10670 | ||
5327ed3d JW |
10671 | /* When comes here, read flags of PARENT_REG or REG could be any of |
10672 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
10673 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
10674 | */ | |
10675 | if (parent_flag == REG_LIVE_READ64 || | |
10676 | /* Or if there is no read flag from REG. */ | |
10677 | !flag || | |
10678 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
10679 | parent_flag == flag) | |
55e7f3b5 JW |
10680 | return 0; |
10681 | ||
5327ed3d | 10682 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
10683 | if (err) |
10684 | return err; | |
10685 | ||
5327ed3d | 10686 | return flag; |
55e7f3b5 JW |
10687 | } |
10688 | ||
8e9cd9ce | 10689 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
10690 | * straight-line code between a state and its parent. When we arrive at an |
10691 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
10692 | * code, so read marks in the state must propagate to the parent regardless | |
10693 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 10694 | * in mark_reg_read() is for. |
8e9cd9ce | 10695 | */ |
f4d7e40a AS |
10696 | static int propagate_liveness(struct bpf_verifier_env *env, |
10697 | const struct bpf_verifier_state *vstate, | |
10698 | struct bpf_verifier_state *vparent) | |
dc503a8a | 10699 | { |
3f8cafa4 | 10700 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 10701 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 10702 | int i, frame, err = 0; |
dc503a8a | 10703 | |
f4d7e40a AS |
10704 | if (vparent->curframe != vstate->curframe) { |
10705 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
10706 | vparent->curframe, vstate->curframe); | |
10707 | return -EFAULT; | |
10708 | } | |
dc503a8a EC |
10709 | /* Propagate read liveness of registers... */ |
10710 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 10711 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
10712 | parent = vparent->frame[frame]; |
10713 | state = vstate->frame[frame]; | |
10714 | parent_reg = parent->regs; | |
10715 | state_reg = state->regs; | |
83d16312 JK |
10716 | /* We don't need to worry about FP liveness, it's read-only */ |
10717 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
10718 | err = propagate_liveness_reg(env, &state_reg[i], |
10719 | &parent_reg[i]); | |
5327ed3d | 10720 | if (err < 0) |
3f8cafa4 | 10721 | return err; |
5327ed3d JW |
10722 | if (err == REG_LIVE_READ64) |
10723 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 10724 | } |
f4d7e40a | 10725 | |
1b04aee7 | 10726 | /* Propagate stack slots. */ |
f4d7e40a AS |
10727 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
10728 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
10729 | parent_reg = &parent->stack[i].spilled_ptr; |
10730 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
10731 | err = propagate_liveness_reg(env, state_reg, |
10732 | parent_reg); | |
5327ed3d | 10733 | if (err < 0) |
3f8cafa4 | 10734 | return err; |
dc503a8a EC |
10735 | } |
10736 | } | |
5327ed3d | 10737 | return 0; |
dc503a8a EC |
10738 | } |
10739 | ||
a3ce685d AS |
10740 | /* find precise scalars in the previous equivalent state and |
10741 | * propagate them into the current state | |
10742 | */ | |
10743 | static int propagate_precision(struct bpf_verifier_env *env, | |
10744 | const struct bpf_verifier_state *old) | |
10745 | { | |
10746 | struct bpf_reg_state *state_reg; | |
10747 | struct bpf_func_state *state; | |
10748 | int i, err = 0; | |
10749 | ||
10750 | state = old->frame[old->curframe]; | |
10751 | state_reg = state->regs; | |
10752 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
10753 | if (state_reg->type != SCALAR_VALUE || | |
10754 | !state_reg->precise) | |
10755 | continue; | |
10756 | if (env->log.level & BPF_LOG_LEVEL2) | |
10757 | verbose(env, "propagating r%d\n", i); | |
10758 | err = mark_chain_precision(env, i); | |
10759 | if (err < 0) | |
10760 | return err; | |
10761 | } | |
10762 | ||
10763 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
27113c59 | 10764 | if (!is_spilled_reg(&state->stack[i])) |
a3ce685d AS |
10765 | continue; |
10766 | state_reg = &state->stack[i].spilled_ptr; | |
10767 | if (state_reg->type != SCALAR_VALUE || | |
10768 | !state_reg->precise) | |
10769 | continue; | |
10770 | if (env->log.level & BPF_LOG_LEVEL2) | |
10771 | verbose(env, "propagating fp%d\n", | |
10772 | (-i - 1) * BPF_REG_SIZE); | |
10773 | err = mark_chain_precision_stack(env, i); | |
10774 | if (err < 0) | |
10775 | return err; | |
10776 | } | |
10777 | return 0; | |
10778 | } | |
10779 | ||
2589726d AS |
10780 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
10781 | struct bpf_verifier_state *cur) | |
10782 | { | |
10783 | struct bpf_func_state *fold, *fcur; | |
10784 | int i, fr = cur->curframe; | |
10785 | ||
10786 | if (old->curframe != fr) | |
10787 | return false; | |
10788 | ||
10789 | fold = old->frame[fr]; | |
10790 | fcur = cur->frame[fr]; | |
10791 | for (i = 0; i < MAX_BPF_REG; i++) | |
10792 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
10793 | offsetof(struct bpf_reg_state, parent))) | |
10794 | return false; | |
10795 | return true; | |
10796 | } | |
10797 | ||
10798 | ||
58e2af8b | 10799 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 10800 | { |
58e2af8b | 10801 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 10802 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 10803 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 10804 | int i, j, err, states_cnt = 0; |
10d274e8 | 10805 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 10806 | |
b5dc0163 | 10807 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 10808 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
10809 | /* this 'insn_idx' instruction wasn't marked, so we will not |
10810 | * be doing state search here | |
10811 | */ | |
10812 | return 0; | |
10813 | ||
2589726d AS |
10814 | /* bpf progs typically have pruning point every 4 instructions |
10815 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
10816 | * Do not add new state for future pruning if the verifier hasn't seen | |
10817 | * at least 2 jumps and at least 8 instructions. | |
10818 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
10819 | * In tests that amounts to up to 50% reduction into total verifier | |
10820 | * memory consumption and 20% verifier time speedup. | |
10821 | */ | |
10822 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
10823 | env->insn_processed - env->prev_insn_processed >= 8) | |
10824 | add_new_state = true; | |
10825 | ||
a8f500af AS |
10826 | pprev = explored_state(env, insn_idx); |
10827 | sl = *pprev; | |
10828 | ||
9242b5f5 AS |
10829 | clean_live_states(env, insn_idx, cur); |
10830 | ||
a8f500af | 10831 | while (sl) { |
dc2a4ebc AS |
10832 | states_cnt++; |
10833 | if (sl->state.insn_idx != insn_idx) | |
10834 | goto next; | |
bfc6bb74 | 10835 | |
2589726d | 10836 | if (sl->state.branches) { |
bfc6bb74 AS |
10837 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
10838 | ||
10839 | if (frame->in_async_callback_fn && | |
10840 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
10841 | /* Different async_entry_cnt means that the verifier is | |
10842 | * processing another entry into async callback. | |
10843 | * Seeing the same state is not an indication of infinite | |
10844 | * loop or infinite recursion. | |
10845 | * But finding the same state doesn't mean that it's safe | |
10846 | * to stop processing the current state. The previous state | |
10847 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
10848 | * Checking in_async_callback_fn alone is not enough either. | |
10849 | * Since the verifier still needs to catch infinite loops | |
10850 | * inside async callbacks. | |
10851 | */ | |
10852 | } else if (states_maybe_looping(&sl->state, cur) && | |
10853 | states_equal(env, &sl->state, cur)) { | |
2589726d AS |
10854 | verbose_linfo(env, insn_idx, "; "); |
10855 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
10856 | return -EINVAL; | |
10857 | } | |
10858 | /* if the verifier is processing a loop, avoid adding new state | |
10859 | * too often, since different loop iterations have distinct | |
10860 | * states and may not help future pruning. | |
10861 | * This threshold shouldn't be too low to make sure that | |
10862 | * a loop with large bound will be rejected quickly. | |
10863 | * The most abusive loop will be: | |
10864 | * r1 += 1 | |
10865 | * if r1 < 1000000 goto pc-2 | |
10866 | * 1M insn_procssed limit / 100 == 10k peak states. | |
10867 | * This threshold shouldn't be too high either, since states | |
10868 | * at the end of the loop are likely to be useful in pruning. | |
10869 | */ | |
10870 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
10871 | env->insn_processed - env->prev_insn_processed < 100) | |
10872 | add_new_state = false; | |
10873 | goto miss; | |
10874 | } | |
638f5b90 | 10875 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 10876 | sl->hit_cnt++; |
f1bca824 | 10877 | /* reached equivalent register/stack state, |
dc503a8a EC |
10878 | * prune the search. |
10879 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
10880 | * If we have any write marks in env->cur_state, they |
10881 | * will prevent corresponding reads in the continuation | |
10882 | * from reaching our parent (an explored_state). Our | |
10883 | * own state will get the read marks recorded, but | |
10884 | * they'll be immediately forgotten as we're pruning | |
10885 | * this state and will pop a new one. | |
f1bca824 | 10886 | */ |
f4d7e40a | 10887 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
10888 | |
10889 | /* if previous state reached the exit with precision and | |
10890 | * current state is equivalent to it (except precsion marks) | |
10891 | * the precision needs to be propagated back in | |
10892 | * the current state. | |
10893 | */ | |
10894 | err = err ? : push_jmp_history(env, cur); | |
10895 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
10896 | if (err) |
10897 | return err; | |
f1bca824 | 10898 | return 1; |
dc503a8a | 10899 | } |
2589726d AS |
10900 | miss: |
10901 | /* when new state is not going to be added do not increase miss count. | |
10902 | * Otherwise several loop iterations will remove the state | |
10903 | * recorded earlier. The goal of these heuristics is to have | |
10904 | * states from some iterations of the loop (some in the beginning | |
10905 | * and some at the end) to help pruning. | |
10906 | */ | |
10907 | if (add_new_state) | |
10908 | sl->miss_cnt++; | |
9f4686c4 AS |
10909 | /* heuristic to determine whether this state is beneficial |
10910 | * to keep checking from state equivalence point of view. | |
10911 | * Higher numbers increase max_states_per_insn and verification time, | |
10912 | * but do not meaningfully decrease insn_processed. | |
10913 | */ | |
10914 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
10915 | /* the state is unlikely to be useful. Remove it to | |
10916 | * speed up verification | |
10917 | */ | |
10918 | *pprev = sl->next; | |
10919 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
10920 | u32 br = sl->state.branches; |
10921 | ||
10922 | WARN_ONCE(br, | |
10923 | "BUG live_done but branches_to_explore %d\n", | |
10924 | br); | |
9f4686c4 AS |
10925 | free_verifier_state(&sl->state, false); |
10926 | kfree(sl); | |
10927 | env->peak_states--; | |
10928 | } else { | |
10929 | /* cannot free this state, since parentage chain may | |
10930 | * walk it later. Add it for free_list instead to | |
10931 | * be freed at the end of verification | |
10932 | */ | |
10933 | sl->next = env->free_list; | |
10934 | env->free_list = sl; | |
10935 | } | |
10936 | sl = *pprev; | |
10937 | continue; | |
10938 | } | |
dc2a4ebc | 10939 | next: |
9f4686c4 AS |
10940 | pprev = &sl->next; |
10941 | sl = *pprev; | |
f1bca824 AS |
10942 | } |
10943 | ||
06ee7115 AS |
10944 | if (env->max_states_per_insn < states_cnt) |
10945 | env->max_states_per_insn = states_cnt; | |
10946 | ||
2c78ee89 | 10947 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 10948 | return push_jmp_history(env, cur); |
ceefbc96 | 10949 | |
2589726d | 10950 | if (!add_new_state) |
b5dc0163 | 10951 | return push_jmp_history(env, cur); |
ceefbc96 | 10952 | |
2589726d AS |
10953 | /* There were no equivalent states, remember the current one. |
10954 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 10955 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 10956 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 10957 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
10958 | * again on the way to bpf_exit. |
10959 | * When looping the sl->state.branches will be > 0 and this state | |
10960 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 10961 | */ |
638f5b90 | 10962 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
10963 | if (!new_sl) |
10964 | return -ENOMEM; | |
06ee7115 AS |
10965 | env->total_states++; |
10966 | env->peak_states++; | |
2589726d AS |
10967 | env->prev_jmps_processed = env->jmps_processed; |
10968 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
10969 | |
10970 | /* add new state to the head of linked list */ | |
679c782d EC |
10971 | new = &new_sl->state; |
10972 | err = copy_verifier_state(new, cur); | |
1969db47 | 10973 | if (err) { |
679c782d | 10974 | free_verifier_state(new, false); |
1969db47 AS |
10975 | kfree(new_sl); |
10976 | return err; | |
10977 | } | |
dc2a4ebc | 10978 | new->insn_idx = insn_idx; |
2589726d AS |
10979 | WARN_ONCE(new->branches != 1, |
10980 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 10981 | |
2589726d | 10982 | cur->parent = new; |
b5dc0163 AS |
10983 | cur->first_insn_idx = insn_idx; |
10984 | clear_jmp_history(cur); | |
5d839021 AS |
10985 | new_sl->next = *explored_state(env, insn_idx); |
10986 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
10987 | /* connect new state to parentage chain. Current frame needs all |
10988 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
10989 | * to the stack implicitly by JITs) so in callers' frames connect just | |
10990 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
10991 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
10992 | * from callee with its full parentage chain, anyway. | |
10993 | */ | |
8e9cd9ce EC |
10994 | /* clear write marks in current state: the writes we did are not writes |
10995 | * our child did, so they don't screen off its reads from us. | |
10996 | * (There are no read marks in current state, because reads always mark | |
10997 | * their parent and current state never has children yet. Only | |
10998 | * explored_states can get read marks.) | |
10999 | */ | |
eea1c227 AS |
11000 | for (j = 0; j <= cur->curframe; j++) { |
11001 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
11002 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
11003 | for (i = 0; i < BPF_REG_FP; i++) | |
11004 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
11005 | } | |
f4d7e40a AS |
11006 | |
11007 | /* all stack frames are accessible from callee, clear them all */ | |
11008 | for (j = 0; j <= cur->curframe; j++) { | |
11009 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 11010 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 11011 | |
679c782d | 11012 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 11013 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
11014 | frame->stack[i].spilled_ptr.parent = |
11015 | &newframe->stack[i].spilled_ptr; | |
11016 | } | |
f4d7e40a | 11017 | } |
f1bca824 AS |
11018 | return 0; |
11019 | } | |
11020 | ||
c64b7983 JS |
11021 | /* Return true if it's OK to have the same insn return a different type. */ |
11022 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
11023 | { | |
11024 | switch (type) { | |
11025 | case PTR_TO_CTX: | |
11026 | case PTR_TO_SOCKET: | |
11027 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
11028 | case PTR_TO_SOCK_COMMON: |
11029 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
11030 | case PTR_TO_TCP_SOCK: |
11031 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 11032 | case PTR_TO_XDP_SOCK: |
2a02759e | 11033 | case PTR_TO_BTF_ID: |
b121b341 | 11034 | case PTR_TO_BTF_ID_OR_NULL: |
c64b7983 JS |
11035 | return false; |
11036 | default: | |
11037 | return true; | |
11038 | } | |
11039 | } | |
11040 | ||
11041 | /* If an instruction was previously used with particular pointer types, then we | |
11042 | * need to be careful to avoid cases such as the below, where it may be ok | |
11043 | * for one branch accessing the pointer, but not ok for the other branch: | |
11044 | * | |
11045 | * R1 = sock_ptr | |
11046 | * goto X; | |
11047 | * ... | |
11048 | * R1 = some_other_valid_ptr; | |
11049 | * goto X; | |
11050 | * ... | |
11051 | * R2 = *(u32 *)(R1 + 0); | |
11052 | */ | |
11053 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
11054 | { | |
11055 | return src != prev && (!reg_type_mismatch_ok(src) || | |
11056 | !reg_type_mismatch_ok(prev)); | |
11057 | } | |
11058 | ||
58e2af8b | 11059 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 11060 | { |
6f8a57cc | 11061 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 11062 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 11063 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 11064 | struct bpf_reg_state *regs; |
06ee7115 | 11065 | int insn_cnt = env->prog->len; |
17a52670 | 11066 | bool do_print_state = false; |
b5dc0163 | 11067 | int prev_insn_idx = -1; |
17a52670 | 11068 | |
17a52670 AS |
11069 | for (;;) { |
11070 | struct bpf_insn *insn; | |
11071 | u8 class; | |
11072 | int err; | |
11073 | ||
b5dc0163 | 11074 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 11075 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 11076 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 11077 | env->insn_idx, insn_cnt); |
17a52670 AS |
11078 | return -EFAULT; |
11079 | } | |
11080 | ||
c08435ec | 11081 | insn = &insns[env->insn_idx]; |
17a52670 AS |
11082 | class = BPF_CLASS(insn->code); |
11083 | ||
06ee7115 | 11084 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
11085 | verbose(env, |
11086 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 11087 | env->insn_processed); |
17a52670 AS |
11088 | return -E2BIG; |
11089 | } | |
11090 | ||
c08435ec | 11091 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
11092 | if (err < 0) |
11093 | return err; | |
11094 | if (err == 1) { | |
11095 | /* found equivalent state, can prune the search */ | |
06ee7115 | 11096 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 11097 | if (do_print_state) |
979d63d5 DB |
11098 | verbose(env, "\nfrom %d to %d%s: safe\n", |
11099 | env->prev_insn_idx, env->insn_idx, | |
11100 | env->cur_state->speculative ? | |
11101 | " (speculative execution)" : ""); | |
f1bca824 | 11102 | else |
c08435ec | 11103 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
11104 | } |
11105 | goto process_bpf_exit; | |
11106 | } | |
11107 | ||
c3494801 AS |
11108 | if (signal_pending(current)) |
11109 | return -EAGAIN; | |
11110 | ||
3c2ce60b DB |
11111 | if (need_resched()) |
11112 | cond_resched(); | |
11113 | ||
06ee7115 AS |
11114 | if (env->log.level & BPF_LOG_LEVEL2 || |
11115 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
11116 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 11117 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 11118 | else |
979d63d5 DB |
11119 | verbose(env, "\nfrom %d to %d%s:", |
11120 | env->prev_insn_idx, env->insn_idx, | |
11121 | env->cur_state->speculative ? | |
11122 | " (speculative execution)" : ""); | |
f4d7e40a | 11123 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
11124 | do_print_state = false; |
11125 | } | |
11126 | ||
06ee7115 | 11127 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 11128 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 11129 | .cb_call = disasm_kfunc_name, |
7105e828 | 11130 | .cb_print = verbose, |
abe08840 | 11131 | .private_data = env, |
7105e828 DB |
11132 | }; |
11133 | ||
c08435ec DB |
11134 | verbose_linfo(env, env->insn_idx, "; "); |
11135 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 11136 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
11137 | } |
11138 | ||
cae1927c | 11139 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
11140 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
11141 | env->prev_insn_idx); | |
cae1927c JK |
11142 | if (err) |
11143 | return err; | |
11144 | } | |
13a27dfc | 11145 | |
638f5b90 | 11146 | regs = cur_regs(env); |
fe9a5ca7 | 11147 | sanitize_mark_insn_seen(env); |
b5dc0163 | 11148 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 11149 | |
17a52670 | 11150 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 11151 | err = check_alu_op(env, insn); |
17a52670 AS |
11152 | if (err) |
11153 | return err; | |
11154 | ||
11155 | } else if (class == BPF_LDX) { | |
3df126f3 | 11156 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
11157 | |
11158 | /* check for reserved fields is already done */ | |
11159 | ||
17a52670 | 11160 | /* check src operand */ |
dc503a8a | 11161 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
11162 | if (err) |
11163 | return err; | |
11164 | ||
dc503a8a | 11165 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
11166 | if (err) |
11167 | return err; | |
11168 | ||
725f9dcd AS |
11169 | src_reg_type = regs[insn->src_reg].type; |
11170 | ||
17a52670 AS |
11171 | /* check that memory (src_reg + off) is readable, |
11172 | * the state of dst_reg will be updated by this func | |
11173 | */ | |
c08435ec DB |
11174 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
11175 | insn->off, BPF_SIZE(insn->code), | |
11176 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
11177 | if (err) |
11178 | return err; | |
11179 | ||
c08435ec | 11180 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
11181 | |
11182 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
11183 | /* saw a valid insn |
11184 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 11185 | * save type to validate intersecting paths |
9bac3d6d | 11186 | */ |
3df126f3 | 11187 | *prev_src_type = src_reg_type; |
9bac3d6d | 11188 | |
c64b7983 | 11189 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
11190 | /* ABuser program is trying to use the same insn |
11191 | * dst_reg = *(u32*) (src_reg + off) | |
11192 | * with different pointer types: | |
11193 | * src_reg == ctx in one branch and | |
11194 | * src_reg == stack|map in some other branch. | |
11195 | * Reject it. | |
11196 | */ | |
61bd5218 | 11197 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
11198 | return -EINVAL; |
11199 | } | |
11200 | ||
17a52670 | 11201 | } else if (class == BPF_STX) { |
3df126f3 | 11202 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 11203 | |
91c960b0 BJ |
11204 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
11205 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
11206 | if (err) |
11207 | return err; | |
c08435ec | 11208 | env->insn_idx++; |
17a52670 AS |
11209 | continue; |
11210 | } | |
11211 | ||
5ca419f2 BJ |
11212 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
11213 | verbose(env, "BPF_STX uses reserved fields\n"); | |
11214 | return -EINVAL; | |
11215 | } | |
11216 | ||
17a52670 | 11217 | /* check src1 operand */ |
dc503a8a | 11218 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
11219 | if (err) |
11220 | return err; | |
11221 | /* check src2 operand */ | |
dc503a8a | 11222 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11223 | if (err) |
11224 | return err; | |
11225 | ||
d691f9e8 AS |
11226 | dst_reg_type = regs[insn->dst_reg].type; |
11227 | ||
17a52670 | 11228 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
11229 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
11230 | insn->off, BPF_SIZE(insn->code), | |
11231 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
11232 | if (err) |
11233 | return err; | |
11234 | ||
c08435ec | 11235 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
11236 | |
11237 | if (*prev_dst_type == NOT_INIT) { | |
11238 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 11239 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 11240 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
11241 | return -EINVAL; |
11242 | } | |
11243 | ||
17a52670 AS |
11244 | } else if (class == BPF_ST) { |
11245 | if (BPF_MODE(insn->code) != BPF_MEM || | |
11246 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 11247 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
11248 | return -EINVAL; |
11249 | } | |
11250 | /* check src operand */ | |
dc503a8a | 11251 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11252 | if (err) |
11253 | return err; | |
11254 | ||
f37a8cb8 | 11255 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 11256 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
11257 | insn->dst_reg, |
11258 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
11259 | return -EACCES; |
11260 | } | |
11261 | ||
17a52670 | 11262 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
11263 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
11264 | insn->off, BPF_SIZE(insn->code), | |
11265 | BPF_WRITE, -1, false); | |
17a52670 AS |
11266 | if (err) |
11267 | return err; | |
11268 | ||
092ed096 | 11269 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
11270 | u8 opcode = BPF_OP(insn->code); |
11271 | ||
2589726d | 11272 | env->jmps_processed++; |
17a52670 AS |
11273 | if (opcode == BPF_CALL) { |
11274 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
11275 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
11276 | && insn->off != 0) || | |
f4d7e40a | 11277 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
11278 | insn->src_reg != BPF_PSEUDO_CALL && |
11279 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
11280 | insn->dst_reg != BPF_REG_0 || |
11281 | class == BPF_JMP32) { | |
61bd5218 | 11282 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
11283 | return -EINVAL; |
11284 | } | |
11285 | ||
d83525ca AS |
11286 | if (env->cur_state->active_spin_lock && |
11287 | (insn->src_reg == BPF_PSEUDO_CALL || | |
11288 | insn->imm != BPF_FUNC_spin_unlock)) { | |
11289 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
11290 | return -EINVAL; | |
11291 | } | |
f4d7e40a | 11292 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 11293 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 MKL |
11294 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
11295 | err = check_kfunc_call(env, insn); | |
f4d7e40a | 11296 | else |
69c087ba | 11297 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
11298 | if (err) |
11299 | return err; | |
17a52670 AS |
11300 | } else if (opcode == BPF_JA) { |
11301 | if (BPF_SRC(insn->code) != BPF_K || | |
11302 | insn->imm != 0 || | |
11303 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
11304 | insn->dst_reg != BPF_REG_0 || |
11305 | class == BPF_JMP32) { | |
61bd5218 | 11306 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
11307 | return -EINVAL; |
11308 | } | |
11309 | ||
c08435ec | 11310 | env->insn_idx += insn->off + 1; |
17a52670 AS |
11311 | continue; |
11312 | ||
11313 | } else if (opcode == BPF_EXIT) { | |
11314 | if (BPF_SRC(insn->code) != BPF_K || | |
11315 | insn->imm != 0 || | |
11316 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
11317 | insn->dst_reg != BPF_REG_0 || |
11318 | class == BPF_JMP32) { | |
61bd5218 | 11319 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
11320 | return -EINVAL; |
11321 | } | |
11322 | ||
d83525ca AS |
11323 | if (env->cur_state->active_spin_lock) { |
11324 | verbose(env, "bpf_spin_unlock is missing\n"); | |
11325 | return -EINVAL; | |
11326 | } | |
11327 | ||
f4d7e40a AS |
11328 | if (state->curframe) { |
11329 | /* exit from nested function */ | |
c08435ec | 11330 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
11331 | if (err) |
11332 | return err; | |
11333 | do_print_state = true; | |
11334 | continue; | |
11335 | } | |
11336 | ||
fd978bf7 JS |
11337 | err = check_reference_leak(env); |
11338 | if (err) | |
11339 | return err; | |
11340 | ||
390ee7e2 AS |
11341 | err = check_return_code(env); |
11342 | if (err) | |
11343 | return err; | |
f1bca824 | 11344 | process_bpf_exit: |
2589726d | 11345 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 11346 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 11347 | &env->insn_idx, pop_log); |
638f5b90 AS |
11348 | if (err < 0) { |
11349 | if (err != -ENOENT) | |
11350 | return err; | |
17a52670 AS |
11351 | break; |
11352 | } else { | |
11353 | do_print_state = true; | |
11354 | continue; | |
11355 | } | |
11356 | } else { | |
c08435ec | 11357 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
11358 | if (err) |
11359 | return err; | |
11360 | } | |
11361 | } else if (class == BPF_LD) { | |
11362 | u8 mode = BPF_MODE(insn->code); | |
11363 | ||
11364 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
11365 | err = check_ld_abs(env, insn); |
11366 | if (err) | |
11367 | return err; | |
11368 | ||
17a52670 AS |
11369 | } else if (mode == BPF_IMM) { |
11370 | err = check_ld_imm(env, insn); | |
11371 | if (err) | |
11372 | return err; | |
11373 | ||
c08435ec | 11374 | env->insn_idx++; |
fe9a5ca7 | 11375 | sanitize_mark_insn_seen(env); |
17a52670 | 11376 | } else { |
61bd5218 | 11377 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
11378 | return -EINVAL; |
11379 | } | |
11380 | } else { | |
61bd5218 | 11381 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
11382 | return -EINVAL; |
11383 | } | |
11384 | ||
c08435ec | 11385 | env->insn_idx++; |
17a52670 AS |
11386 | } |
11387 | ||
11388 | return 0; | |
11389 | } | |
11390 | ||
541c3bad AN |
11391 | static int find_btf_percpu_datasec(struct btf *btf) |
11392 | { | |
11393 | const struct btf_type *t; | |
11394 | const char *tname; | |
11395 | int i, n; | |
11396 | ||
11397 | /* | |
11398 | * Both vmlinux and module each have their own ".data..percpu" | |
11399 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
11400 | * types to look at only module's own BTF types. | |
11401 | */ | |
11402 | n = btf_nr_types(btf); | |
11403 | if (btf_is_module(btf)) | |
11404 | i = btf_nr_types(btf_vmlinux); | |
11405 | else | |
11406 | i = 1; | |
11407 | ||
11408 | for(; i < n; i++) { | |
11409 | t = btf_type_by_id(btf, i); | |
11410 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
11411 | continue; | |
11412 | ||
11413 | tname = btf_name_by_offset(btf, t->name_off); | |
11414 | if (!strcmp(tname, ".data..percpu")) | |
11415 | return i; | |
11416 | } | |
11417 | ||
11418 | return -ENOENT; | |
11419 | } | |
11420 | ||
4976b718 HL |
11421 | /* replace pseudo btf_id with kernel symbol address */ |
11422 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
11423 | struct bpf_insn *insn, | |
11424 | struct bpf_insn_aux_data *aux) | |
11425 | { | |
eaa6bcb7 HL |
11426 | const struct btf_var_secinfo *vsi; |
11427 | const struct btf_type *datasec; | |
541c3bad | 11428 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
11429 | const struct btf_type *t; |
11430 | const char *sym_name; | |
eaa6bcb7 | 11431 | bool percpu = false; |
f16e6313 | 11432 | u32 type, id = insn->imm; |
541c3bad | 11433 | struct btf *btf; |
f16e6313 | 11434 | s32 datasec_id; |
4976b718 | 11435 | u64 addr; |
541c3bad | 11436 | int i, btf_fd, err; |
4976b718 | 11437 | |
541c3bad AN |
11438 | btf_fd = insn[1].imm; |
11439 | if (btf_fd) { | |
11440 | btf = btf_get_by_fd(btf_fd); | |
11441 | if (IS_ERR(btf)) { | |
11442 | verbose(env, "invalid module BTF object FD specified.\n"); | |
11443 | return -EINVAL; | |
11444 | } | |
11445 | } else { | |
11446 | if (!btf_vmlinux) { | |
11447 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
11448 | return -EINVAL; | |
11449 | } | |
11450 | btf = btf_vmlinux; | |
11451 | btf_get(btf); | |
4976b718 HL |
11452 | } |
11453 | ||
541c3bad | 11454 | t = btf_type_by_id(btf, id); |
4976b718 HL |
11455 | if (!t) { |
11456 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
11457 | err = -ENOENT; |
11458 | goto err_put; | |
4976b718 HL |
11459 | } |
11460 | ||
11461 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
11462 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
11463 | err = -EINVAL; | |
11464 | goto err_put; | |
4976b718 HL |
11465 | } |
11466 | ||
541c3bad | 11467 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
11468 | addr = kallsyms_lookup_name(sym_name); |
11469 | if (!addr) { | |
11470 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
11471 | sym_name); | |
541c3bad AN |
11472 | err = -ENOENT; |
11473 | goto err_put; | |
4976b718 HL |
11474 | } |
11475 | ||
541c3bad | 11476 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 11477 | if (datasec_id > 0) { |
541c3bad | 11478 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
11479 | for_each_vsi(i, datasec, vsi) { |
11480 | if (vsi->type == id) { | |
11481 | percpu = true; | |
11482 | break; | |
11483 | } | |
11484 | } | |
11485 | } | |
11486 | ||
4976b718 HL |
11487 | insn[0].imm = (u32)addr; |
11488 | insn[1].imm = addr >> 32; | |
11489 | ||
11490 | type = t->type; | |
541c3bad | 11491 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 HL |
11492 | if (percpu) { |
11493 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; | |
541c3bad | 11494 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
11495 | aux->btf_var.btf_id = type; |
11496 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
11497 | const struct btf_type *ret; |
11498 | const char *tname; | |
11499 | u32 tsize; | |
11500 | ||
11501 | /* resolve the type size of ksym. */ | |
541c3bad | 11502 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 11503 | if (IS_ERR(ret)) { |
541c3bad | 11504 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
11505 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
11506 | tname, PTR_ERR(ret)); | |
541c3bad AN |
11507 | err = -EINVAL; |
11508 | goto err_put; | |
4976b718 HL |
11509 | } |
11510 | aux->btf_var.reg_type = PTR_TO_MEM; | |
11511 | aux->btf_var.mem_size = tsize; | |
11512 | } else { | |
11513 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 11514 | aux->btf_var.btf = btf; |
4976b718 HL |
11515 | aux->btf_var.btf_id = type; |
11516 | } | |
541c3bad AN |
11517 | |
11518 | /* check whether we recorded this BTF (and maybe module) already */ | |
11519 | for (i = 0; i < env->used_btf_cnt; i++) { | |
11520 | if (env->used_btfs[i].btf == btf) { | |
11521 | btf_put(btf); | |
11522 | return 0; | |
11523 | } | |
11524 | } | |
11525 | ||
11526 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
11527 | err = -E2BIG; | |
11528 | goto err_put; | |
11529 | } | |
11530 | ||
11531 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
11532 | btf_mod->btf = btf; | |
11533 | btf_mod->module = NULL; | |
11534 | ||
11535 | /* if we reference variables from kernel module, bump its refcount */ | |
11536 | if (btf_is_module(btf)) { | |
11537 | btf_mod->module = btf_try_get_module(btf); | |
11538 | if (!btf_mod->module) { | |
11539 | err = -ENXIO; | |
11540 | goto err_put; | |
11541 | } | |
11542 | } | |
11543 | ||
11544 | env->used_btf_cnt++; | |
11545 | ||
4976b718 | 11546 | return 0; |
541c3bad AN |
11547 | err_put: |
11548 | btf_put(btf); | |
11549 | return err; | |
4976b718 HL |
11550 | } |
11551 | ||
56f668df MKL |
11552 | static int check_map_prealloc(struct bpf_map *map) |
11553 | { | |
11554 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
11555 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
11556 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
11557 | !(map->map_flags & BPF_F_NO_PREALLOC); |
11558 | } | |
11559 | ||
d83525ca AS |
11560 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
11561 | { | |
11562 | switch (type) { | |
11563 | case BPF_PROG_TYPE_KPROBE: | |
11564 | case BPF_PROG_TYPE_TRACEPOINT: | |
11565 | case BPF_PROG_TYPE_PERF_EVENT: | |
11566 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
11567 | return true; | |
11568 | default: | |
11569 | return false; | |
11570 | } | |
11571 | } | |
11572 | ||
94dacdbd TG |
11573 | static bool is_preallocated_map(struct bpf_map *map) |
11574 | { | |
11575 | if (!check_map_prealloc(map)) | |
11576 | return false; | |
11577 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
11578 | return false; | |
11579 | return true; | |
11580 | } | |
11581 | ||
61bd5218 JK |
11582 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
11583 | struct bpf_map *map, | |
fdc15d38 AS |
11584 | struct bpf_prog *prog) |
11585 | ||
11586 | { | |
7e40781c | 11587 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
94dacdbd TG |
11588 | /* |
11589 | * Validate that trace type programs use preallocated hash maps. | |
11590 | * | |
11591 | * For programs attached to PERF events this is mandatory as the | |
11592 | * perf NMI can hit any arbitrary code sequence. | |
11593 | * | |
11594 | * All other trace types using preallocated hash maps are unsafe as | |
11595 | * well because tracepoint or kprobes can be inside locked regions | |
11596 | * of the memory allocator or at a place where a recursion into the | |
11597 | * memory allocator would see inconsistent state. | |
11598 | * | |
2ed905c5 TG |
11599 | * On RT enabled kernels run-time allocation of all trace type |
11600 | * programs is strictly prohibited due to lock type constraints. On | |
11601 | * !RT kernels it is allowed for backwards compatibility reasons for | |
11602 | * now, but warnings are emitted so developers are made aware of | |
11603 | * the unsafety and can fix their programs before this is enforced. | |
56f668df | 11604 | */ |
7e40781c UP |
11605 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
11606 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { | |
61bd5218 | 11607 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
11608 | return -EINVAL; |
11609 | } | |
2ed905c5 TG |
11610 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
11611 | verbose(env, "trace type programs can only use preallocated hash map\n"); | |
11612 | return -EINVAL; | |
11613 | } | |
94dacdbd TG |
11614 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
11615 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
fdc15d38 | 11616 | } |
a3884572 | 11617 | |
9e7a4d98 KS |
11618 | if (map_value_has_spin_lock(map)) { |
11619 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { | |
11620 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
11621 | return -EINVAL; | |
11622 | } | |
11623 | ||
11624 | if (is_tracing_prog_type(prog_type)) { | |
11625 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
11626 | return -EINVAL; | |
11627 | } | |
11628 | ||
11629 | if (prog->aux->sleepable) { | |
11630 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
11631 | return -EINVAL; | |
11632 | } | |
d83525ca AS |
11633 | } |
11634 | ||
a3884572 | 11635 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 11636 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
11637 | verbose(env, "offload device mismatch between prog and map\n"); |
11638 | return -EINVAL; | |
11639 | } | |
11640 | ||
85d33df3 MKL |
11641 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
11642 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
11643 | return -EINVAL; | |
11644 | } | |
11645 | ||
1e6c62a8 AS |
11646 | if (prog->aux->sleepable) |
11647 | switch (map->map_type) { | |
11648 | case BPF_MAP_TYPE_HASH: | |
11649 | case BPF_MAP_TYPE_LRU_HASH: | |
11650 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
11651 | case BPF_MAP_TYPE_PERCPU_HASH: |
11652 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
11653 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
11654 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
11655 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
1e6c62a8 AS |
11656 | if (!is_preallocated_map(map)) { |
11657 | verbose(env, | |
638e4b82 | 11658 | "Sleepable programs can only use preallocated maps\n"); |
1e6c62a8 AS |
11659 | return -EINVAL; |
11660 | } | |
11661 | break; | |
ba90c2cc KS |
11662 | case BPF_MAP_TYPE_RINGBUF: |
11663 | break; | |
1e6c62a8 AS |
11664 | default: |
11665 | verbose(env, | |
ba90c2cc | 11666 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
1e6c62a8 AS |
11667 | return -EINVAL; |
11668 | } | |
11669 | ||
fdc15d38 AS |
11670 | return 0; |
11671 | } | |
11672 | ||
b741f163 RG |
11673 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
11674 | { | |
11675 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
11676 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
11677 | } | |
11678 | ||
4976b718 HL |
11679 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
11680 | * | |
11681 | * 1. if it accesses map FD, replace it with actual map pointer. | |
11682 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
11683 | * | |
11684 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 11685 | */ |
4976b718 | 11686 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
11687 | { |
11688 | struct bpf_insn *insn = env->prog->insnsi; | |
11689 | int insn_cnt = env->prog->len; | |
fdc15d38 | 11690 | int i, j, err; |
0246e64d | 11691 | |
f1f7714e | 11692 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
11693 | if (err) |
11694 | return err; | |
11695 | ||
0246e64d | 11696 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 11697 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 11698 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 11699 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
11700 | return -EINVAL; |
11701 | } | |
11702 | ||
0246e64d | 11703 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 11704 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
11705 | struct bpf_map *map; |
11706 | struct fd f; | |
d8eca5bb | 11707 | u64 addr; |
387544bf | 11708 | u32 fd; |
0246e64d AS |
11709 | |
11710 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
11711 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
11712 | insn[1].off != 0) { | |
61bd5218 | 11713 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
11714 | return -EINVAL; |
11715 | } | |
11716 | ||
d8eca5bb | 11717 | if (insn[0].src_reg == 0) |
0246e64d AS |
11718 | /* valid generic load 64-bit imm */ |
11719 | goto next_insn; | |
11720 | ||
4976b718 HL |
11721 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
11722 | aux = &env->insn_aux_data[i]; | |
11723 | err = check_pseudo_btf_id(env, insn, aux); | |
11724 | if (err) | |
11725 | return err; | |
11726 | goto next_insn; | |
11727 | } | |
11728 | ||
69c087ba YS |
11729 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
11730 | aux = &env->insn_aux_data[i]; | |
11731 | aux->ptr_type = PTR_TO_FUNC; | |
11732 | goto next_insn; | |
11733 | } | |
11734 | ||
d8eca5bb DB |
11735 | /* In final convert_pseudo_ld_imm64() step, this is |
11736 | * converted into regular 64-bit imm load insn. | |
11737 | */ | |
387544bf AS |
11738 | switch (insn[0].src_reg) { |
11739 | case BPF_PSEUDO_MAP_VALUE: | |
11740 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
11741 | break; | |
11742 | case BPF_PSEUDO_MAP_FD: | |
11743 | case BPF_PSEUDO_MAP_IDX: | |
11744 | if (insn[1].imm == 0) | |
11745 | break; | |
11746 | fallthrough; | |
11747 | default: | |
11748 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
11749 | return -EINVAL; |
11750 | } | |
11751 | ||
387544bf AS |
11752 | switch (insn[0].src_reg) { |
11753 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
11754 | case BPF_PSEUDO_MAP_IDX: | |
11755 | if (bpfptr_is_null(env->fd_array)) { | |
11756 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
11757 | return -EPROTO; | |
11758 | } | |
11759 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
11760 | insn[0].imm * sizeof(fd), | |
11761 | sizeof(fd))) | |
11762 | return -EFAULT; | |
11763 | break; | |
11764 | default: | |
11765 | fd = insn[0].imm; | |
11766 | break; | |
11767 | } | |
11768 | ||
11769 | f = fdget(fd); | |
c2101297 | 11770 | map = __bpf_map_get(f); |
0246e64d | 11771 | if (IS_ERR(map)) { |
61bd5218 | 11772 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 11773 | insn[0].imm); |
0246e64d AS |
11774 | return PTR_ERR(map); |
11775 | } | |
11776 | ||
61bd5218 | 11777 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
11778 | if (err) { |
11779 | fdput(f); | |
11780 | return err; | |
11781 | } | |
11782 | ||
d8eca5bb | 11783 | aux = &env->insn_aux_data[i]; |
387544bf AS |
11784 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
11785 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
11786 | addr = (unsigned long)map; |
11787 | } else { | |
11788 | u32 off = insn[1].imm; | |
11789 | ||
11790 | if (off >= BPF_MAX_VAR_OFF) { | |
11791 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
11792 | fdput(f); | |
11793 | return -EINVAL; | |
11794 | } | |
11795 | ||
11796 | if (!map->ops->map_direct_value_addr) { | |
11797 | verbose(env, "no direct value access support for this map type\n"); | |
11798 | fdput(f); | |
11799 | return -EINVAL; | |
11800 | } | |
11801 | ||
11802 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
11803 | if (err) { | |
11804 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
11805 | map->value_size, off); | |
11806 | fdput(f); | |
11807 | return err; | |
11808 | } | |
11809 | ||
11810 | aux->map_off = off; | |
11811 | addr += off; | |
11812 | } | |
11813 | ||
11814 | insn[0].imm = (u32)addr; | |
11815 | insn[1].imm = addr >> 32; | |
0246e64d AS |
11816 | |
11817 | /* check whether we recorded this map already */ | |
d8eca5bb | 11818 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 11819 | if (env->used_maps[j] == map) { |
d8eca5bb | 11820 | aux->map_index = j; |
0246e64d AS |
11821 | fdput(f); |
11822 | goto next_insn; | |
11823 | } | |
d8eca5bb | 11824 | } |
0246e64d AS |
11825 | |
11826 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
11827 | fdput(f); | |
11828 | return -E2BIG; | |
11829 | } | |
11830 | ||
0246e64d AS |
11831 | /* hold the map. If the program is rejected by verifier, |
11832 | * the map will be released by release_maps() or it | |
11833 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 11834 | * and all maps are released in free_used_maps() |
0246e64d | 11835 | */ |
1e0bd5a0 | 11836 | bpf_map_inc(map); |
d8eca5bb DB |
11837 | |
11838 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
11839 | env->used_maps[env->used_map_cnt++] = map; |
11840 | ||
b741f163 | 11841 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 11842 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 11843 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
11844 | fdput(f); |
11845 | return -EBUSY; | |
11846 | } | |
11847 | ||
0246e64d AS |
11848 | fdput(f); |
11849 | next_insn: | |
11850 | insn++; | |
11851 | i++; | |
5e581dad DB |
11852 | continue; |
11853 | } | |
11854 | ||
11855 | /* Basic sanity check before we invest more work here. */ | |
11856 | if (!bpf_opcode_in_insntable(insn->code)) { | |
11857 | verbose(env, "unknown opcode %02x\n", insn->code); | |
11858 | return -EINVAL; | |
0246e64d AS |
11859 | } |
11860 | } | |
11861 | ||
11862 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
11863 | * 'struct bpf_map *' into a register instead of user map_fd. | |
11864 | * These pointers will be used later by verifier to validate map access. | |
11865 | */ | |
11866 | return 0; | |
11867 | } | |
11868 | ||
11869 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 11870 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 11871 | { |
a2ea0746 DB |
11872 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
11873 | env->used_map_cnt); | |
0246e64d AS |
11874 | } |
11875 | ||
541c3bad AN |
11876 | /* drop refcnt of maps used by the rejected program */ |
11877 | static void release_btfs(struct bpf_verifier_env *env) | |
11878 | { | |
11879 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
11880 | env->used_btf_cnt); | |
11881 | } | |
11882 | ||
0246e64d | 11883 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 11884 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
11885 | { |
11886 | struct bpf_insn *insn = env->prog->insnsi; | |
11887 | int insn_cnt = env->prog->len; | |
11888 | int i; | |
11889 | ||
69c087ba YS |
11890 | for (i = 0; i < insn_cnt; i++, insn++) { |
11891 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
11892 | continue; | |
11893 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
11894 | continue; | |
11895 | insn->src_reg = 0; | |
11896 | } | |
0246e64d AS |
11897 | } |
11898 | ||
8041902d AS |
11899 | /* single env->prog->insni[off] instruction was replaced with the range |
11900 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
11901 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
11902 | */ | |
75f0fc7b HF |
11903 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
11904 | struct bpf_insn_aux_data *new_data, | |
11905 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 11906 | { |
75f0fc7b | 11907 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 11908 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 11909 | u32 old_seen = old_data[off].seen; |
b325fbca | 11910 | u32 prog_len; |
c131187d | 11911 | int i; |
8041902d | 11912 | |
b325fbca JW |
11913 | /* aux info at OFF always needs adjustment, no matter fast path |
11914 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
11915 | * original insn at old prog. | |
11916 | */ | |
11917 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
11918 | ||
8041902d | 11919 | if (cnt == 1) |
75f0fc7b | 11920 | return; |
b325fbca | 11921 | prog_len = new_prog->len; |
75f0fc7b | 11922 | |
8041902d AS |
11923 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
11924 | memcpy(new_data + off + cnt - 1, old_data + off, | |
11925 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 11926 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
11927 | /* Expand insni[off]'s seen count to the patched range. */ |
11928 | new_data[i].seen = old_seen; | |
b325fbca JW |
11929 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
11930 | } | |
8041902d AS |
11931 | env->insn_aux_data = new_data; |
11932 | vfree(old_data); | |
8041902d AS |
11933 | } |
11934 | ||
cc8b0b92 AS |
11935 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
11936 | { | |
11937 | int i; | |
11938 | ||
11939 | if (len == 1) | |
11940 | return; | |
4cb3d99c JW |
11941 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
11942 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 11943 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 11944 | continue; |
9c8105bd | 11945 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
11946 | } |
11947 | } | |
11948 | ||
7506d211 | 11949 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
11950 | { |
11951 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
11952 | int i, sz = prog->aux->size_poke_tab; | |
11953 | struct bpf_jit_poke_descriptor *desc; | |
11954 | ||
11955 | for (i = 0; i < sz; i++) { | |
11956 | desc = &tab[i]; | |
7506d211 JF |
11957 | if (desc->insn_idx <= off) |
11958 | continue; | |
a748c697 MF |
11959 | desc->insn_idx += len - 1; |
11960 | } | |
11961 | } | |
11962 | ||
8041902d AS |
11963 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
11964 | const struct bpf_insn *patch, u32 len) | |
11965 | { | |
11966 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
11967 | struct bpf_insn_aux_data *new_data = NULL; |
11968 | ||
11969 | if (len > 1) { | |
11970 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
11971 | sizeof(struct bpf_insn_aux_data))); | |
11972 | if (!new_data) | |
11973 | return NULL; | |
11974 | } | |
8041902d AS |
11975 | |
11976 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
11977 | if (IS_ERR(new_prog)) { |
11978 | if (PTR_ERR(new_prog) == -ERANGE) | |
11979 | verbose(env, | |
11980 | "insn %d cannot be patched due to 16-bit range\n", | |
11981 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 11982 | vfree(new_data); |
8041902d | 11983 | return NULL; |
4f73379e | 11984 | } |
75f0fc7b | 11985 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 11986 | adjust_subprog_starts(env, off, len); |
7506d211 | 11987 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
11988 | return new_prog; |
11989 | } | |
11990 | ||
52875a04 JK |
11991 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
11992 | u32 off, u32 cnt) | |
11993 | { | |
11994 | int i, j; | |
11995 | ||
11996 | /* find first prog starting at or after off (first to remove) */ | |
11997 | for (i = 0; i < env->subprog_cnt; i++) | |
11998 | if (env->subprog_info[i].start >= off) | |
11999 | break; | |
12000 | /* find first prog starting at or after off + cnt (first to stay) */ | |
12001 | for (j = i; j < env->subprog_cnt; j++) | |
12002 | if (env->subprog_info[j].start >= off + cnt) | |
12003 | break; | |
12004 | /* if j doesn't start exactly at off + cnt, we are just removing | |
12005 | * the front of previous prog | |
12006 | */ | |
12007 | if (env->subprog_info[j].start != off + cnt) | |
12008 | j--; | |
12009 | ||
12010 | if (j > i) { | |
12011 | struct bpf_prog_aux *aux = env->prog->aux; | |
12012 | int move; | |
12013 | ||
12014 | /* move fake 'exit' subprog as well */ | |
12015 | move = env->subprog_cnt + 1 - j; | |
12016 | ||
12017 | memmove(env->subprog_info + i, | |
12018 | env->subprog_info + j, | |
12019 | sizeof(*env->subprog_info) * move); | |
12020 | env->subprog_cnt -= j - i; | |
12021 | ||
12022 | /* remove func_info */ | |
12023 | if (aux->func_info) { | |
12024 | move = aux->func_info_cnt - j; | |
12025 | ||
12026 | memmove(aux->func_info + i, | |
12027 | aux->func_info + j, | |
12028 | sizeof(*aux->func_info) * move); | |
12029 | aux->func_info_cnt -= j - i; | |
12030 | /* func_info->insn_off is set after all code rewrites, | |
12031 | * in adjust_btf_func() - no need to adjust | |
12032 | */ | |
12033 | } | |
12034 | } else { | |
12035 | /* convert i from "first prog to remove" to "first to adjust" */ | |
12036 | if (env->subprog_info[i].start == off) | |
12037 | i++; | |
12038 | } | |
12039 | ||
12040 | /* update fake 'exit' subprog as well */ | |
12041 | for (; i <= env->subprog_cnt; i++) | |
12042 | env->subprog_info[i].start -= cnt; | |
12043 | ||
12044 | return 0; | |
12045 | } | |
12046 | ||
12047 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
12048 | u32 cnt) | |
12049 | { | |
12050 | struct bpf_prog *prog = env->prog; | |
12051 | u32 i, l_off, l_cnt, nr_linfo; | |
12052 | struct bpf_line_info *linfo; | |
12053 | ||
12054 | nr_linfo = prog->aux->nr_linfo; | |
12055 | if (!nr_linfo) | |
12056 | return 0; | |
12057 | ||
12058 | linfo = prog->aux->linfo; | |
12059 | ||
12060 | /* find first line info to remove, count lines to be removed */ | |
12061 | for (i = 0; i < nr_linfo; i++) | |
12062 | if (linfo[i].insn_off >= off) | |
12063 | break; | |
12064 | ||
12065 | l_off = i; | |
12066 | l_cnt = 0; | |
12067 | for (; i < nr_linfo; i++) | |
12068 | if (linfo[i].insn_off < off + cnt) | |
12069 | l_cnt++; | |
12070 | else | |
12071 | break; | |
12072 | ||
12073 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
12074 | * last removed linfo. prog is already modified, so prog->len == off | |
12075 | * means no live instructions after (tail of the program was removed). | |
12076 | */ | |
12077 | if (prog->len != off && l_cnt && | |
12078 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
12079 | l_cnt--; | |
12080 | linfo[--i].insn_off = off + cnt; | |
12081 | } | |
12082 | ||
12083 | /* remove the line info which refer to the removed instructions */ | |
12084 | if (l_cnt) { | |
12085 | memmove(linfo + l_off, linfo + i, | |
12086 | sizeof(*linfo) * (nr_linfo - i)); | |
12087 | ||
12088 | prog->aux->nr_linfo -= l_cnt; | |
12089 | nr_linfo = prog->aux->nr_linfo; | |
12090 | } | |
12091 | ||
12092 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
12093 | for (i = l_off; i < nr_linfo; i++) | |
12094 | linfo[i].insn_off -= cnt; | |
12095 | ||
12096 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
12097 | for (i = 0; i <= env->subprog_cnt; i++) | |
12098 | if (env->subprog_info[i].linfo_idx > l_off) { | |
12099 | /* program may have started in the removed region but | |
12100 | * may not be fully removed | |
12101 | */ | |
12102 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
12103 | env->subprog_info[i].linfo_idx -= l_cnt; | |
12104 | else | |
12105 | env->subprog_info[i].linfo_idx = l_off; | |
12106 | } | |
12107 | ||
12108 | return 0; | |
12109 | } | |
12110 | ||
12111 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
12112 | { | |
12113 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12114 | unsigned int orig_prog_len = env->prog->len; | |
12115 | int err; | |
12116 | ||
08ca90af JK |
12117 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
12118 | bpf_prog_offload_remove_insns(env, off, cnt); | |
12119 | ||
52875a04 JK |
12120 | err = bpf_remove_insns(env->prog, off, cnt); |
12121 | if (err) | |
12122 | return err; | |
12123 | ||
12124 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
12125 | if (err) | |
12126 | return err; | |
12127 | ||
12128 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
12129 | if (err) | |
12130 | return err; | |
12131 | ||
12132 | memmove(aux_data + off, aux_data + off + cnt, | |
12133 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
12134 | ||
12135 | return 0; | |
12136 | } | |
12137 | ||
2a5418a1 DB |
12138 | /* The verifier does more data flow analysis than llvm and will not |
12139 | * explore branches that are dead at run time. Malicious programs can | |
12140 | * have dead code too. Therefore replace all dead at-run-time code | |
12141 | * with 'ja -1'. | |
12142 | * | |
12143 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
12144 | * program and through another bug we would manage to jump there, then | |
12145 | * we'd execute beyond program memory otherwise. Returning exception | |
12146 | * code also wouldn't work since we can have subprogs where the dead | |
12147 | * code could be located. | |
c131187d AS |
12148 | */ |
12149 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
12150 | { | |
12151 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 12152 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
12153 | struct bpf_insn *insn = env->prog->insnsi; |
12154 | const int insn_cnt = env->prog->len; | |
12155 | int i; | |
12156 | ||
12157 | for (i = 0; i < insn_cnt; i++) { | |
12158 | if (aux_data[i].seen) | |
12159 | continue; | |
2a5418a1 | 12160 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 12161 | aux_data[i].zext_dst = false; |
c131187d AS |
12162 | } |
12163 | } | |
12164 | ||
e2ae4ca2 JK |
12165 | static bool insn_is_cond_jump(u8 code) |
12166 | { | |
12167 | u8 op; | |
12168 | ||
092ed096 JW |
12169 | if (BPF_CLASS(code) == BPF_JMP32) |
12170 | return true; | |
12171 | ||
e2ae4ca2 JK |
12172 | if (BPF_CLASS(code) != BPF_JMP) |
12173 | return false; | |
12174 | ||
12175 | op = BPF_OP(code); | |
12176 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
12177 | } | |
12178 | ||
12179 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
12180 | { | |
12181 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12182 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
12183 | struct bpf_insn *insn = env->prog->insnsi; | |
12184 | const int insn_cnt = env->prog->len; | |
12185 | int i; | |
12186 | ||
12187 | for (i = 0; i < insn_cnt; i++, insn++) { | |
12188 | if (!insn_is_cond_jump(insn->code)) | |
12189 | continue; | |
12190 | ||
12191 | if (!aux_data[i + 1].seen) | |
12192 | ja.off = insn->off; | |
12193 | else if (!aux_data[i + 1 + insn->off].seen) | |
12194 | ja.off = 0; | |
12195 | else | |
12196 | continue; | |
12197 | ||
08ca90af JK |
12198 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
12199 | bpf_prog_offload_replace_insn(env, i, &ja); | |
12200 | ||
e2ae4ca2 JK |
12201 | memcpy(insn, &ja, sizeof(ja)); |
12202 | } | |
12203 | } | |
12204 | ||
52875a04 JK |
12205 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
12206 | { | |
12207 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12208 | int insn_cnt = env->prog->len; | |
12209 | int i, err; | |
12210 | ||
12211 | for (i = 0; i < insn_cnt; i++) { | |
12212 | int j; | |
12213 | ||
12214 | j = 0; | |
12215 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
12216 | j++; | |
12217 | if (!j) | |
12218 | continue; | |
12219 | ||
12220 | err = verifier_remove_insns(env, i, j); | |
12221 | if (err) | |
12222 | return err; | |
12223 | insn_cnt = env->prog->len; | |
12224 | } | |
12225 | ||
12226 | return 0; | |
12227 | } | |
12228 | ||
a1b14abc JK |
12229 | static int opt_remove_nops(struct bpf_verifier_env *env) |
12230 | { | |
12231 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
12232 | struct bpf_insn *insn = env->prog->insnsi; | |
12233 | int insn_cnt = env->prog->len; | |
12234 | int i, err; | |
12235 | ||
12236 | for (i = 0; i < insn_cnt; i++) { | |
12237 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
12238 | continue; | |
12239 | ||
12240 | err = verifier_remove_insns(env, i, 1); | |
12241 | if (err) | |
12242 | return err; | |
12243 | insn_cnt--; | |
12244 | i--; | |
12245 | } | |
12246 | ||
12247 | return 0; | |
12248 | } | |
12249 | ||
d6c2308c JW |
12250 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
12251 | const union bpf_attr *attr) | |
a4b1d3c1 | 12252 | { |
d6c2308c | 12253 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 12254 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 12255 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 12256 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 12257 | struct bpf_prog *new_prog; |
d6c2308c | 12258 | bool rnd_hi32; |
a4b1d3c1 | 12259 | |
d6c2308c | 12260 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 12261 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
12262 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
12263 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
12264 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
12265 | for (i = 0; i < len; i++) { |
12266 | int adj_idx = i + delta; | |
12267 | struct bpf_insn insn; | |
83a28819 | 12268 | int load_reg; |
a4b1d3c1 | 12269 | |
d6c2308c | 12270 | insn = insns[adj_idx]; |
83a28819 | 12271 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
12272 | if (!aux[adj_idx].zext_dst) { |
12273 | u8 code, class; | |
12274 | u32 imm_rnd; | |
12275 | ||
12276 | if (!rnd_hi32) | |
12277 | continue; | |
12278 | ||
12279 | code = insn.code; | |
12280 | class = BPF_CLASS(code); | |
83a28819 | 12281 | if (load_reg == -1) |
d6c2308c JW |
12282 | continue; |
12283 | ||
12284 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
12285 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
12286 | * here. | |
d6c2308c | 12287 | */ |
83a28819 | 12288 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
12289 | if (class == BPF_LD && |
12290 | BPF_MODE(code) == BPF_IMM) | |
12291 | i++; | |
12292 | continue; | |
12293 | } | |
12294 | ||
12295 | /* ctx load could be transformed into wider load. */ | |
12296 | if (class == BPF_LDX && | |
12297 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
12298 | continue; | |
12299 | ||
12300 | imm_rnd = get_random_int(); | |
12301 | rnd_hi32_patch[0] = insn; | |
12302 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 12303 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
12304 | patch = rnd_hi32_patch; |
12305 | patch_len = 4; | |
12306 | goto apply_patch_buffer; | |
12307 | } | |
12308 | ||
39491867 BJ |
12309 | /* Add in an zero-extend instruction if a) the JIT has requested |
12310 | * it or b) it's a CMPXCHG. | |
12311 | * | |
12312 | * The latter is because: BPF_CMPXCHG always loads a value into | |
12313 | * R0, therefore always zero-extends. However some archs' | |
12314 | * equivalent instruction only does this load when the | |
12315 | * comparison is successful. This detail of CMPXCHG is | |
12316 | * orthogonal to the general zero-extension behaviour of the | |
12317 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
12318 | */ | |
12319 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
12320 | continue; |
12321 | ||
83a28819 IL |
12322 | if (WARN_ON(load_reg == -1)) { |
12323 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
12324 | return -EFAULT; | |
b2e37a71 IL |
12325 | } |
12326 | ||
a4b1d3c1 | 12327 | zext_patch[0] = insn; |
b2e37a71 IL |
12328 | zext_patch[1].dst_reg = load_reg; |
12329 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
12330 | patch = zext_patch; |
12331 | patch_len = 2; | |
12332 | apply_patch_buffer: | |
12333 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
12334 | if (!new_prog) |
12335 | return -ENOMEM; | |
12336 | env->prog = new_prog; | |
12337 | insns = new_prog->insnsi; | |
12338 | aux = env->insn_aux_data; | |
d6c2308c | 12339 | delta += patch_len - 1; |
a4b1d3c1 JW |
12340 | } |
12341 | ||
12342 | return 0; | |
12343 | } | |
12344 | ||
c64b7983 JS |
12345 | /* convert load instructions that access fields of a context type into a |
12346 | * sequence of instructions that access fields of the underlying structure: | |
12347 | * struct __sk_buff -> struct sk_buff | |
12348 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 12349 | */ |
58e2af8b | 12350 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 12351 | { |
00176a34 | 12352 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 12353 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 12354 | const int insn_cnt = env->prog->len; |
36bbef52 | 12355 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 12356 | u32 target_size, size_default, off; |
9bac3d6d | 12357 | struct bpf_prog *new_prog; |
d691f9e8 | 12358 | enum bpf_access_type type; |
f96da094 | 12359 | bool is_narrower_load; |
9bac3d6d | 12360 | |
b09928b9 DB |
12361 | if (ops->gen_prologue || env->seen_direct_write) { |
12362 | if (!ops->gen_prologue) { | |
12363 | verbose(env, "bpf verifier is misconfigured\n"); | |
12364 | return -EINVAL; | |
12365 | } | |
36bbef52 DB |
12366 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
12367 | env->prog); | |
12368 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 12369 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
12370 | return -EINVAL; |
12371 | } else if (cnt) { | |
8041902d | 12372 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
12373 | if (!new_prog) |
12374 | return -ENOMEM; | |
8041902d | 12375 | |
36bbef52 | 12376 | env->prog = new_prog; |
3df126f3 | 12377 | delta += cnt - 1; |
36bbef52 DB |
12378 | } |
12379 | } | |
12380 | ||
c64b7983 | 12381 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
12382 | return 0; |
12383 | ||
3df126f3 | 12384 | insn = env->prog->insnsi + delta; |
36bbef52 | 12385 | |
9bac3d6d | 12386 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 | 12387 | bpf_convert_ctx_access_t convert_ctx_access; |
2039f26f | 12388 | bool ctx_access; |
c64b7983 | 12389 | |
62c7989b DB |
12390 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
12391 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
12392 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 12393 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 12394 | type = BPF_READ; |
2039f26f DB |
12395 | ctx_access = true; |
12396 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || | |
12397 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
12398 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
12399 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
12400 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
12401 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
12402 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
12403 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 12404 | type = BPF_WRITE; |
2039f26f DB |
12405 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
12406 | } else { | |
9bac3d6d | 12407 | continue; |
2039f26f | 12408 | } |
9bac3d6d | 12409 | |
af86ca4e | 12410 | if (type == BPF_WRITE && |
2039f26f | 12411 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 12412 | struct bpf_insn patch[] = { |
af86ca4e | 12413 | *insn, |
2039f26f | 12414 | BPF_ST_NOSPEC(), |
af86ca4e AS |
12415 | }; |
12416 | ||
12417 | cnt = ARRAY_SIZE(patch); | |
12418 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
12419 | if (!new_prog) | |
12420 | return -ENOMEM; | |
12421 | ||
12422 | delta += cnt - 1; | |
12423 | env->prog = new_prog; | |
12424 | insn = new_prog->insnsi + i + delta; | |
12425 | continue; | |
12426 | } | |
12427 | ||
2039f26f DB |
12428 | if (!ctx_access) |
12429 | continue; | |
12430 | ||
c64b7983 JS |
12431 | switch (env->insn_aux_data[i + delta].ptr_type) { |
12432 | case PTR_TO_CTX: | |
12433 | if (!ops->convert_ctx_access) | |
12434 | continue; | |
12435 | convert_ctx_access = ops->convert_ctx_access; | |
12436 | break; | |
12437 | case PTR_TO_SOCKET: | |
46f8bc92 | 12438 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
12439 | convert_ctx_access = bpf_sock_convert_ctx_access; |
12440 | break; | |
655a51e5 MKL |
12441 | case PTR_TO_TCP_SOCK: |
12442 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
12443 | break; | |
fada7fdc JL |
12444 | case PTR_TO_XDP_SOCK: |
12445 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
12446 | break; | |
2a02759e | 12447 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
12448 | if (type == BPF_READ) { |
12449 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
12450 | BPF_SIZE((insn)->code); | |
12451 | env->prog->aux->num_exentries++; | |
7e40781c | 12452 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
2a02759e AS |
12453 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
12454 | return -EINVAL; | |
12455 | } | |
2a02759e | 12456 | continue; |
c64b7983 | 12457 | default: |
9bac3d6d | 12458 | continue; |
c64b7983 | 12459 | } |
9bac3d6d | 12460 | |
31fd8581 | 12461 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 12462 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
12463 | |
12464 | /* If the read access is a narrower load of the field, | |
12465 | * convert to a 4/8-byte load, to minimum program type specific | |
12466 | * convert_ctx_access changes. If conversion is successful, | |
12467 | * we will apply proper mask to the result. | |
12468 | */ | |
f96da094 | 12469 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
12470 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
12471 | off = insn->off; | |
31fd8581 | 12472 | if (is_narrower_load) { |
f96da094 DB |
12473 | u8 size_code; |
12474 | ||
12475 | if (type == BPF_WRITE) { | |
61bd5218 | 12476 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
12477 | return -EINVAL; |
12478 | } | |
31fd8581 | 12479 | |
f96da094 | 12480 | size_code = BPF_H; |
31fd8581 YS |
12481 | if (ctx_field_size == 4) |
12482 | size_code = BPF_W; | |
12483 | else if (ctx_field_size == 8) | |
12484 | size_code = BPF_DW; | |
f96da094 | 12485 | |
bc23105c | 12486 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
12487 | insn->code = BPF_LDX | BPF_MEM | size_code; |
12488 | } | |
f96da094 DB |
12489 | |
12490 | target_size = 0; | |
c64b7983 JS |
12491 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
12492 | &target_size); | |
f96da094 DB |
12493 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
12494 | (ctx_field_size && !target_size)) { | |
61bd5218 | 12495 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
12496 | return -EINVAL; |
12497 | } | |
f96da094 DB |
12498 | |
12499 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
12500 | u8 shift = bpf_ctx_narrow_access_offset( |
12501 | off, size, size_default) * 8; | |
d7af7e49 AI |
12502 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
12503 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
12504 | return -EINVAL; | |
12505 | } | |
46f53a65 AI |
12506 | if (ctx_field_size <= 4) { |
12507 | if (shift) | |
12508 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
12509 | insn->dst_reg, | |
12510 | shift); | |
31fd8581 | 12511 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 12512 | (1 << size * 8) - 1); |
46f53a65 AI |
12513 | } else { |
12514 | if (shift) | |
12515 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
12516 | insn->dst_reg, | |
12517 | shift); | |
31fd8581 | 12518 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 12519 | (1ULL << size * 8) - 1); |
46f53a65 | 12520 | } |
31fd8581 | 12521 | } |
9bac3d6d | 12522 | |
8041902d | 12523 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
12524 | if (!new_prog) |
12525 | return -ENOMEM; | |
12526 | ||
3df126f3 | 12527 | delta += cnt - 1; |
9bac3d6d AS |
12528 | |
12529 | /* keep walking new program and skip insns we just inserted */ | |
12530 | env->prog = new_prog; | |
3df126f3 | 12531 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
12532 | } |
12533 | ||
12534 | return 0; | |
12535 | } | |
12536 | ||
1c2a088a AS |
12537 | static int jit_subprogs(struct bpf_verifier_env *env) |
12538 | { | |
12539 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
12540 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 12541 | struct bpf_map *map_ptr; |
7105e828 | 12542 | struct bpf_insn *insn; |
1c2a088a | 12543 | void *old_bpf_func; |
c4c0bdc0 | 12544 | int err, num_exentries; |
1c2a088a | 12545 | |
f910cefa | 12546 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
12547 | return 0; |
12548 | ||
7105e828 | 12549 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 12550 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 12551 | continue; |
69c087ba | 12552 | |
c7a89784 DB |
12553 | /* Upon error here we cannot fall back to interpreter but |
12554 | * need a hard reject of the program. Thus -EFAULT is | |
12555 | * propagated in any case. | |
12556 | */ | |
1c2a088a AS |
12557 | subprog = find_subprog(env, i + insn->imm + 1); |
12558 | if (subprog < 0) { | |
12559 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
12560 | i + insn->imm + 1); | |
12561 | return -EFAULT; | |
12562 | } | |
12563 | /* temporarily remember subprog id inside insn instead of | |
12564 | * aux_data, since next loop will split up all insns into funcs | |
12565 | */ | |
f910cefa | 12566 | insn->off = subprog; |
1c2a088a AS |
12567 | /* remember original imm in case JIT fails and fallback |
12568 | * to interpreter will be needed | |
12569 | */ | |
12570 | env->insn_aux_data[i].call_imm = insn->imm; | |
12571 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
12572 | insn->imm = 1; | |
3990ed4c MKL |
12573 | if (bpf_pseudo_func(insn)) |
12574 | /* jit (e.g. x86_64) may emit fewer instructions | |
12575 | * if it learns a u32 imm is the same as a u64 imm. | |
12576 | * Force a non zero here. | |
12577 | */ | |
12578 | insn[1].imm = 1; | |
1c2a088a AS |
12579 | } |
12580 | ||
c454a46b MKL |
12581 | err = bpf_prog_alloc_jited_linfo(prog); |
12582 | if (err) | |
12583 | goto out_undo_insn; | |
12584 | ||
12585 | err = -ENOMEM; | |
6396bb22 | 12586 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 12587 | if (!func) |
c7a89784 | 12588 | goto out_undo_insn; |
1c2a088a | 12589 | |
f910cefa | 12590 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 12591 | subprog_start = subprog_end; |
4cb3d99c | 12592 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
12593 | |
12594 | len = subprog_end - subprog_start; | |
fb7dd8bc | 12595 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
12596 | * hence main prog stats include the runtime of subprogs. |
12597 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 12598 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
12599 | */ |
12600 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
12601 | if (!func[i]) |
12602 | goto out_free; | |
12603 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
12604 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 12605 | func[i]->type = prog->type; |
1c2a088a | 12606 | func[i]->len = len; |
4f74d809 DB |
12607 | if (bpf_prog_calc_tag(func[i])) |
12608 | goto out_free; | |
1c2a088a | 12609 | func[i]->is_func = 1; |
ba64e7d8 | 12610 | func[i]->aux->func_idx = i; |
f263a814 | 12611 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
12612 | func[i]->aux->btf = prog->aux->btf; |
12613 | func[i]->aux->func_info = prog->aux->func_info; | |
f263a814 JF |
12614 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
12615 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 12616 | |
a748c697 | 12617 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 12618 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 12619 | |
f263a814 JF |
12620 | poke = &prog->aux->poke_tab[j]; |
12621 | if (poke->insn_idx < subprog_end && | |
12622 | poke->insn_idx >= subprog_start) | |
12623 | poke->aux = func[i]->aux; | |
a748c697 MF |
12624 | } |
12625 | ||
1c2a088a AS |
12626 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
12627 | * Long term would need debug info to populate names | |
12628 | */ | |
12629 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 12630 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 12631 | func[i]->jit_requested = 1; |
e6ac2450 | 12632 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 12633 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
12634 | func[i]->aux->linfo = prog->aux->linfo; |
12635 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
12636 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
12637 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
12638 | num_exentries = 0; |
12639 | insn = func[i]->insnsi; | |
12640 | for (j = 0; j < func[i]->len; j++, insn++) { | |
12641 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
12642 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
12643 | num_exentries++; | |
12644 | } | |
12645 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 12646 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
12647 | func[i] = bpf_int_jit_compile(func[i]); |
12648 | if (!func[i]->jited) { | |
12649 | err = -ENOTSUPP; | |
12650 | goto out_free; | |
12651 | } | |
12652 | cond_resched(); | |
12653 | } | |
a748c697 | 12654 | |
1c2a088a AS |
12655 | /* at this point all bpf functions were successfully JITed |
12656 | * now populate all bpf_calls with correct addresses and | |
12657 | * run last pass of JIT | |
12658 | */ | |
f910cefa | 12659 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
12660 | insn = func[i]->insnsi; |
12661 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 12662 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 12663 | subprog = insn->off; |
69c087ba YS |
12664 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
12665 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
12666 | continue; | |
12667 | } | |
23a2d70c | 12668 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
12669 | continue; |
12670 | subprog = insn->off; | |
3d717fad | 12671 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 12672 | } |
2162fed4 SD |
12673 | |
12674 | /* we use the aux data to keep a list of the start addresses | |
12675 | * of the JITed images for each function in the program | |
12676 | * | |
12677 | * for some architectures, such as powerpc64, the imm field | |
12678 | * might not be large enough to hold the offset of the start | |
12679 | * address of the callee's JITed image from __bpf_call_base | |
12680 | * | |
12681 | * in such cases, we can lookup the start address of a callee | |
12682 | * by using its subprog id, available from the off field of | |
12683 | * the call instruction, as an index for this list | |
12684 | */ | |
12685 | func[i]->aux->func = func; | |
12686 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 12687 | } |
f910cefa | 12688 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
12689 | old_bpf_func = func[i]->bpf_func; |
12690 | tmp = bpf_int_jit_compile(func[i]); | |
12691 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
12692 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 12693 | err = -ENOTSUPP; |
1c2a088a AS |
12694 | goto out_free; |
12695 | } | |
12696 | cond_resched(); | |
12697 | } | |
12698 | ||
12699 | /* finally lock prog and jit images for all functions and | |
12700 | * populate kallsysm | |
12701 | */ | |
f910cefa | 12702 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
12703 | bpf_prog_lock_ro(func[i]); |
12704 | bpf_prog_kallsyms_add(func[i]); | |
12705 | } | |
7105e828 DB |
12706 | |
12707 | /* Last step: make now unused interpreter insns from main | |
12708 | * prog consistent for later dump requests, so they can | |
12709 | * later look the same as if they were interpreted only. | |
12710 | */ | |
12711 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
12712 | if (bpf_pseudo_func(insn)) { |
12713 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
12714 | insn[1].imm = insn->off; |
12715 | insn->off = 0; | |
69c087ba YS |
12716 | continue; |
12717 | } | |
23a2d70c | 12718 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
12719 | continue; |
12720 | insn->off = env->insn_aux_data[i].call_imm; | |
12721 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 12722 | insn->imm = subprog; |
7105e828 DB |
12723 | } |
12724 | ||
1c2a088a AS |
12725 | prog->jited = 1; |
12726 | prog->bpf_func = func[0]->bpf_func; | |
12727 | prog->aux->func = func; | |
f910cefa | 12728 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 12729 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
12730 | return 0; |
12731 | out_free: | |
f263a814 JF |
12732 | /* We failed JIT'ing, so at this point we need to unregister poke |
12733 | * descriptors from subprogs, so that kernel is not attempting to | |
12734 | * patch it anymore as we're freeing the subprog JIT memory. | |
12735 | */ | |
12736 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
12737 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
12738 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
12739 | } | |
12740 | /* At this point we're guaranteed that poke descriptors are not | |
12741 | * live anymore. We can just unlink its descriptor table as it's | |
12742 | * released with the main prog. | |
12743 | */ | |
a748c697 MF |
12744 | for (i = 0; i < env->subprog_cnt; i++) { |
12745 | if (!func[i]) | |
12746 | continue; | |
f263a814 | 12747 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
12748 | bpf_jit_free(func[i]); |
12749 | } | |
1c2a088a | 12750 | kfree(func); |
c7a89784 | 12751 | out_undo_insn: |
1c2a088a AS |
12752 | /* cleanup main prog to be interpreted */ |
12753 | prog->jit_requested = 0; | |
12754 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
23a2d70c | 12755 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
12756 | continue; |
12757 | insn->off = 0; | |
12758 | insn->imm = env->insn_aux_data[i].call_imm; | |
12759 | } | |
e16301fb | 12760 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
12761 | return err; |
12762 | } | |
12763 | ||
1ea47e01 AS |
12764 | static int fixup_call_args(struct bpf_verifier_env *env) |
12765 | { | |
19d28fbd | 12766 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
12767 | struct bpf_prog *prog = env->prog; |
12768 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 12769 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 12770 | int i, depth; |
19d28fbd | 12771 | #endif |
e4052d06 | 12772 | int err = 0; |
1ea47e01 | 12773 | |
e4052d06 QM |
12774 | if (env->prog->jit_requested && |
12775 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
12776 | err = jit_subprogs(env); |
12777 | if (err == 0) | |
1c2a088a | 12778 | return 0; |
c7a89784 DB |
12779 | if (err == -EFAULT) |
12780 | return err; | |
19d28fbd DM |
12781 | } |
12782 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
12783 | if (has_kfunc_call) { |
12784 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
12785 | return -EINVAL; | |
12786 | } | |
e411901c MF |
12787 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
12788 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
12789 | * have to be rejected, since interpreter doesn't support them yet. | |
12790 | */ | |
12791 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
12792 | return -EINVAL; | |
12793 | } | |
1ea47e01 | 12794 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
12795 | if (bpf_pseudo_func(insn)) { |
12796 | /* When JIT fails the progs with callback calls | |
12797 | * have to be rejected, since interpreter doesn't support them yet. | |
12798 | */ | |
12799 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
12800 | return -EINVAL; | |
12801 | } | |
12802 | ||
23a2d70c | 12803 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
12804 | continue; |
12805 | depth = get_callee_stack_depth(env, insn, i); | |
12806 | if (depth < 0) | |
12807 | return depth; | |
12808 | bpf_patch_call_args(insn, depth); | |
12809 | } | |
19d28fbd DM |
12810 | err = 0; |
12811 | #endif | |
12812 | return err; | |
1ea47e01 AS |
12813 | } |
12814 | ||
e6ac2450 MKL |
12815 | static int fixup_kfunc_call(struct bpf_verifier_env *env, |
12816 | struct bpf_insn *insn) | |
12817 | { | |
12818 | const struct bpf_kfunc_desc *desc; | |
12819 | ||
a5d82727 KKD |
12820 | if (!insn->imm) { |
12821 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
12822 | return -EINVAL; | |
12823 | } | |
12824 | ||
e6ac2450 MKL |
12825 | /* insn->imm has the btf func_id. Replace it with |
12826 | * an address (relative to __bpf_base_call). | |
12827 | */ | |
2357672c | 12828 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
12829 | if (!desc) { |
12830 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
12831 | insn->imm); | |
12832 | return -EFAULT; | |
12833 | } | |
12834 | ||
12835 | insn->imm = desc->imm; | |
12836 | ||
12837 | return 0; | |
12838 | } | |
12839 | ||
e6ac5933 BJ |
12840 | /* Do various post-verification rewrites in a single program pass. |
12841 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 12842 | */ |
e6ac5933 | 12843 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 12844 | { |
79741b3b | 12845 | struct bpf_prog *prog = env->prog; |
d2e4c1e6 | 12846 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
9b99edca | 12847 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 12848 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 12849 | const struct bpf_func_proto *fn; |
79741b3b | 12850 | const int insn_cnt = prog->len; |
09772d92 | 12851 | const struct bpf_map_ops *ops; |
c93552c4 | 12852 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
12853 | struct bpf_insn insn_buf[16]; |
12854 | struct bpf_prog *new_prog; | |
12855 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 12856 | int i, ret, cnt, delta = 0; |
e245c5c6 | 12857 | |
79741b3b | 12858 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 12859 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
12860 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
12861 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
12862 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 12863 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 12864 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
12865 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
12866 | struct bpf_insn *patchlet; | |
12867 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 12868 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
12869 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
12870 | BPF_JNE | BPF_K, insn->src_reg, | |
12871 | 0, 2, 0), | |
f6b1b3bf DB |
12872 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
12873 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
12874 | *insn, | |
12875 | }; | |
e88b2c6e | 12876 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 12877 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
12878 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
12879 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 12880 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 12881 | *insn, |
9b00f1b7 DB |
12882 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
12883 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 12884 | }; |
f6b1b3bf | 12885 | |
e88b2c6e DB |
12886 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
12887 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 12888 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
12889 | |
12890 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
12891 | if (!new_prog) |
12892 | return -ENOMEM; | |
12893 | ||
12894 | delta += cnt - 1; | |
12895 | env->prog = prog = new_prog; | |
12896 | insn = new_prog->insnsi + i + delta; | |
12897 | continue; | |
12898 | } | |
12899 | ||
e6ac5933 | 12900 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
12901 | if (BPF_CLASS(insn->code) == BPF_LD && |
12902 | (BPF_MODE(insn->code) == BPF_ABS || | |
12903 | BPF_MODE(insn->code) == BPF_IND)) { | |
12904 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
12905 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
12906 | verbose(env, "bpf verifier is misconfigured\n"); | |
12907 | return -EINVAL; | |
12908 | } | |
12909 | ||
12910 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
12911 | if (!new_prog) | |
12912 | return -ENOMEM; | |
12913 | ||
12914 | delta += cnt - 1; | |
12915 | env->prog = prog = new_prog; | |
12916 | insn = new_prog->insnsi + i + delta; | |
12917 | continue; | |
12918 | } | |
12919 | ||
e6ac5933 | 12920 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
12921 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
12922 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
12923 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
12924 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 12925 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 12926 | bool issrc, isneg, isimm; |
979d63d5 DB |
12927 | u32 off_reg; |
12928 | ||
12929 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
12930 | if (!aux->alu_state || |
12931 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
12932 | continue; |
12933 | ||
12934 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
12935 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
12936 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 12937 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
12938 | |
12939 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
12940 | if (isimm) { |
12941 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
12942 | } else { | |
12943 | if (isneg) | |
12944 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
12945 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
12946 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
12947 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
12948 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
12949 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
12950 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
12951 | } | |
b9b34ddb DB |
12952 | if (!issrc) |
12953 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
12954 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
12955 | if (isneg) |
12956 | insn->code = insn->code == code_add ? | |
12957 | code_sub : code_add; | |
12958 | *patch++ = *insn; | |
801c6058 | 12959 | if (issrc && isneg && !isimm) |
979d63d5 DB |
12960 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
12961 | cnt = patch - insn_buf; | |
12962 | ||
12963 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
12964 | if (!new_prog) | |
12965 | return -ENOMEM; | |
12966 | ||
12967 | delta += cnt - 1; | |
12968 | env->prog = prog = new_prog; | |
12969 | insn = new_prog->insnsi + i + delta; | |
12970 | continue; | |
12971 | } | |
12972 | ||
79741b3b AS |
12973 | if (insn->code != (BPF_JMP | BPF_CALL)) |
12974 | continue; | |
cc8b0b92 AS |
12975 | if (insn->src_reg == BPF_PSEUDO_CALL) |
12976 | continue; | |
e6ac2450 MKL |
12977 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
12978 | ret = fixup_kfunc_call(env, insn); | |
12979 | if (ret) | |
12980 | return ret; | |
12981 | continue; | |
12982 | } | |
e245c5c6 | 12983 | |
79741b3b AS |
12984 | if (insn->imm == BPF_FUNC_get_route_realm) |
12985 | prog->dst_needed = 1; | |
12986 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
12987 | bpf_user_rnd_init_once(); | |
9802d865 JB |
12988 | if (insn->imm == BPF_FUNC_override_return) |
12989 | prog->kprobe_override = 1; | |
79741b3b | 12990 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
12991 | /* If we tail call into other programs, we |
12992 | * cannot make any assumptions since they can | |
12993 | * be replaced dynamically during runtime in | |
12994 | * the program array. | |
12995 | */ | |
12996 | prog->cb_access = 1; | |
e411901c MF |
12997 | if (!allow_tail_call_in_subprogs(env)) |
12998 | prog->aux->stack_depth = MAX_BPF_STACK; | |
12999 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 13000 | |
79741b3b | 13001 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 13002 | * conditional branch in the interpreter for every normal |
79741b3b AS |
13003 | * call and to prevent accidental JITing by JIT compiler |
13004 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 13005 | */ |
79741b3b | 13006 | insn->imm = 0; |
71189fa9 | 13007 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 13008 | |
c93552c4 | 13009 | aux = &env->insn_aux_data[i + delta]; |
2c78ee89 | 13010 | if (env->bpf_capable && !expect_blinding && |
cc52d914 | 13011 | prog->jit_requested && |
d2e4c1e6 DB |
13012 | !bpf_map_key_poisoned(aux) && |
13013 | !bpf_map_ptr_poisoned(aux) && | |
13014 | !bpf_map_ptr_unpriv(aux)) { | |
13015 | struct bpf_jit_poke_descriptor desc = { | |
13016 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
13017 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
13018 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 13019 | .insn_idx = i + delta, |
d2e4c1e6 DB |
13020 | }; |
13021 | ||
13022 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
13023 | if (ret < 0) { | |
13024 | verbose(env, "adding tail call poke descriptor failed\n"); | |
13025 | return ret; | |
13026 | } | |
13027 | ||
13028 | insn->imm = ret + 1; | |
13029 | continue; | |
13030 | } | |
13031 | ||
c93552c4 DB |
13032 | if (!bpf_map_ptr_unpriv(aux)) |
13033 | continue; | |
13034 | ||
b2157399 AS |
13035 | /* instead of changing every JIT dealing with tail_call |
13036 | * emit two extra insns: | |
13037 | * if (index >= max_entries) goto out; | |
13038 | * index &= array->index_mask; | |
13039 | * to avoid out-of-bounds cpu speculation | |
13040 | */ | |
c93552c4 | 13041 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 13042 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
13043 | return -EINVAL; |
13044 | } | |
c93552c4 | 13045 | |
d2e4c1e6 | 13046 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
13047 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
13048 | map_ptr->max_entries, 2); | |
13049 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
13050 | container_of(map_ptr, | |
13051 | struct bpf_array, | |
13052 | map)->index_mask); | |
13053 | insn_buf[2] = *insn; | |
13054 | cnt = 3; | |
13055 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13056 | if (!new_prog) | |
13057 | return -ENOMEM; | |
13058 | ||
13059 | delta += cnt - 1; | |
13060 | env->prog = prog = new_prog; | |
13061 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
13062 | continue; |
13063 | } | |
e245c5c6 | 13064 | |
b00628b1 AS |
13065 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
13066 | /* The verifier will process callback_fn as many times as necessary | |
13067 | * with different maps and the register states prepared by | |
13068 | * set_timer_callback_state will be accurate. | |
13069 | * | |
13070 | * The following use case is valid: | |
13071 | * map1 is shared by prog1, prog2, prog3. | |
13072 | * prog1 calls bpf_timer_init for some map1 elements | |
13073 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
13074 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
13075 | * prog3 calls bpf_timer_start for some map1 elements. | |
13076 | * Those that were not both bpf_timer_init-ed and | |
13077 | * bpf_timer_set_callback-ed will return -EINVAL. | |
13078 | */ | |
13079 | struct bpf_insn ld_addrs[2] = { | |
13080 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
13081 | }; | |
13082 | ||
13083 | insn_buf[0] = ld_addrs[0]; | |
13084 | insn_buf[1] = ld_addrs[1]; | |
13085 | insn_buf[2] = *insn; | |
13086 | cnt = 3; | |
13087 | ||
13088 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13089 | if (!new_prog) | |
13090 | return -ENOMEM; | |
13091 | ||
13092 | delta += cnt - 1; | |
13093 | env->prog = prog = new_prog; | |
13094 | insn = new_prog->insnsi + i + delta; | |
13095 | goto patch_call_imm; | |
13096 | } | |
13097 | ||
89c63074 | 13098 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
13099 | * and other inlining handlers are currently limited to 64 bit |
13100 | * only. | |
89c63074 | 13101 | */ |
60b58afc | 13102 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
13103 | (insn->imm == BPF_FUNC_map_lookup_elem || |
13104 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
13105 | insn->imm == BPF_FUNC_map_delete_elem || |
13106 | insn->imm == BPF_FUNC_map_push_elem || | |
13107 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 13108 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c AI |
13109 | insn->imm == BPF_FUNC_redirect_map || |
13110 | insn->imm == BPF_FUNC_for_each_map_elem)) { | |
c93552c4 DB |
13111 | aux = &env->insn_aux_data[i + delta]; |
13112 | if (bpf_map_ptr_poisoned(aux)) | |
13113 | goto patch_call_imm; | |
13114 | ||
d2e4c1e6 | 13115 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
13116 | ops = map_ptr->ops; |
13117 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
13118 | ops->map_gen_lookup) { | |
13119 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
13120 | if (cnt == -EOPNOTSUPP) |
13121 | goto patch_map_ops_generic; | |
13122 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
13123 | verbose(env, "bpf verifier is misconfigured\n"); |
13124 | return -EINVAL; | |
13125 | } | |
81ed18ab | 13126 | |
09772d92 DB |
13127 | new_prog = bpf_patch_insn_data(env, i + delta, |
13128 | insn_buf, cnt); | |
13129 | if (!new_prog) | |
13130 | return -ENOMEM; | |
81ed18ab | 13131 | |
09772d92 DB |
13132 | delta += cnt - 1; |
13133 | env->prog = prog = new_prog; | |
13134 | insn = new_prog->insnsi + i + delta; | |
13135 | continue; | |
13136 | } | |
81ed18ab | 13137 | |
09772d92 DB |
13138 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
13139 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
13140 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
13141 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
13142 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
13143 | (int (*)(struct bpf_map *map, void *key, void *value, | |
13144 | u64 flags))NULL)); | |
84430d42 DB |
13145 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
13146 | (int (*)(struct bpf_map *map, void *value, | |
13147 | u64 flags))NULL)); | |
13148 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
13149 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
13150 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
13151 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
e6a4750f BT |
13152 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
13153 | (int (*)(struct bpf_map *map, u32 ifindex, u64 flags))NULL)); | |
0640c77c AI |
13154 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
13155 | (int (*)(struct bpf_map *map, | |
13156 | bpf_callback_t callback_fn, | |
13157 | void *callback_ctx, | |
13158 | u64 flags))NULL)); | |
e6a4750f | 13159 | |
4a8f87e6 | 13160 | patch_map_ops_generic: |
09772d92 DB |
13161 | switch (insn->imm) { |
13162 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 13163 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
13164 | continue; |
13165 | case BPF_FUNC_map_update_elem: | |
3d717fad | 13166 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
13167 | continue; |
13168 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 13169 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 13170 | continue; |
84430d42 | 13171 | case BPF_FUNC_map_push_elem: |
3d717fad | 13172 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
13173 | continue; |
13174 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 13175 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
13176 | continue; |
13177 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 13178 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 13179 | continue; |
e6a4750f | 13180 | case BPF_FUNC_redirect_map: |
3d717fad | 13181 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 13182 | continue; |
0640c77c AI |
13183 | case BPF_FUNC_for_each_map_elem: |
13184 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 13185 | continue; |
09772d92 | 13186 | } |
81ed18ab | 13187 | |
09772d92 | 13188 | goto patch_call_imm; |
81ed18ab AS |
13189 | } |
13190 | ||
e6ac5933 | 13191 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
13192 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
13193 | insn->imm == BPF_FUNC_jiffies64) { | |
13194 | struct bpf_insn ld_jiffies_addr[2] = { | |
13195 | BPF_LD_IMM64(BPF_REG_0, | |
13196 | (unsigned long)&jiffies), | |
13197 | }; | |
13198 | ||
13199 | insn_buf[0] = ld_jiffies_addr[0]; | |
13200 | insn_buf[1] = ld_jiffies_addr[1]; | |
13201 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
13202 | BPF_REG_0, 0); | |
13203 | cnt = 3; | |
13204 | ||
13205 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
13206 | cnt); | |
13207 | if (!new_prog) | |
13208 | return -ENOMEM; | |
13209 | ||
13210 | delta += cnt - 1; | |
13211 | env->prog = prog = new_prog; | |
13212 | insn = new_prog->insnsi + i + delta; | |
13213 | continue; | |
13214 | } | |
13215 | ||
9b99edca JO |
13216 | /* Implement bpf_get_func_ip inline. */ |
13217 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13218 | insn->imm == BPF_FUNC_get_func_ip) { | |
13219 | /* Load IP address from ctx - 8 */ | |
13220 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
13221 | ||
13222 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
13223 | if (!new_prog) | |
13224 | return -ENOMEM; | |
13225 | ||
13226 | env->prog = prog = new_prog; | |
13227 | insn = new_prog->insnsi + i + delta; | |
13228 | continue; | |
13229 | } | |
13230 | ||
81ed18ab | 13231 | patch_call_imm: |
5e43f899 | 13232 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
13233 | /* all functions that have prototype and verifier allowed |
13234 | * programs to call them, must be real in-kernel functions | |
13235 | */ | |
13236 | if (!fn->func) { | |
61bd5218 JK |
13237 | verbose(env, |
13238 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
13239 | func_id_name(insn->imm), insn->imm); |
13240 | return -EFAULT; | |
e245c5c6 | 13241 | } |
79741b3b | 13242 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 13243 | } |
e245c5c6 | 13244 | |
d2e4c1e6 DB |
13245 | /* Since poke tab is now finalized, publish aux to tracker. */ |
13246 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
13247 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
13248 | if (!map_ptr->ops->map_poke_track || | |
13249 | !map_ptr->ops->map_poke_untrack || | |
13250 | !map_ptr->ops->map_poke_run) { | |
13251 | verbose(env, "bpf verifier is misconfigured\n"); | |
13252 | return -EINVAL; | |
13253 | } | |
13254 | ||
13255 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
13256 | if (ret < 0) { | |
13257 | verbose(env, "tracking tail call prog failed\n"); | |
13258 | return ret; | |
13259 | } | |
13260 | } | |
13261 | ||
e6ac2450 MKL |
13262 | sort_kfunc_descs_by_imm(env->prog); |
13263 | ||
79741b3b AS |
13264 | return 0; |
13265 | } | |
e245c5c6 | 13266 | |
58e2af8b | 13267 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 13268 | { |
58e2af8b | 13269 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
13270 | int i; |
13271 | ||
9f4686c4 AS |
13272 | sl = env->free_list; |
13273 | while (sl) { | |
13274 | sln = sl->next; | |
13275 | free_verifier_state(&sl->state, false); | |
13276 | kfree(sl); | |
13277 | sl = sln; | |
13278 | } | |
51c39bb1 | 13279 | env->free_list = NULL; |
9f4686c4 | 13280 | |
f1bca824 AS |
13281 | if (!env->explored_states) |
13282 | return; | |
13283 | ||
dc2a4ebc | 13284 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
13285 | sl = env->explored_states[i]; |
13286 | ||
a8f500af AS |
13287 | while (sl) { |
13288 | sln = sl->next; | |
13289 | free_verifier_state(&sl->state, false); | |
13290 | kfree(sl); | |
13291 | sl = sln; | |
13292 | } | |
51c39bb1 | 13293 | env->explored_states[i] = NULL; |
f1bca824 | 13294 | } |
51c39bb1 | 13295 | } |
f1bca824 | 13296 | |
51c39bb1 AS |
13297 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
13298 | { | |
6f8a57cc | 13299 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
13300 | struct bpf_verifier_state *state; |
13301 | struct bpf_reg_state *regs; | |
13302 | int ret, i; | |
13303 | ||
13304 | env->prev_linfo = NULL; | |
13305 | env->pass_cnt++; | |
13306 | ||
13307 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
13308 | if (!state) | |
13309 | return -ENOMEM; | |
13310 | state->curframe = 0; | |
13311 | state->speculative = false; | |
13312 | state->branches = 1; | |
13313 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
13314 | if (!state->frame[0]) { | |
13315 | kfree(state); | |
13316 | return -ENOMEM; | |
13317 | } | |
13318 | env->cur_state = state; | |
13319 | init_func_state(env, state->frame[0], | |
13320 | BPF_MAIN_FUNC /* callsite */, | |
13321 | 0 /* frameno */, | |
13322 | subprog); | |
13323 | ||
13324 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 13325 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
13326 | ret = btf_prepare_func_args(env, subprog, regs); |
13327 | if (ret) | |
13328 | goto out; | |
13329 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
13330 | if (regs[i].type == PTR_TO_CTX) | |
13331 | mark_reg_known_zero(env, regs, i); | |
13332 | else if (regs[i].type == SCALAR_VALUE) | |
13333 | mark_reg_unknown(env, regs, i); | |
e5069b9c DB |
13334 | else if (regs[i].type == PTR_TO_MEM_OR_NULL) { |
13335 | const u32 mem_size = regs[i].mem_size; | |
13336 | ||
13337 | mark_reg_known_zero(env, regs, i); | |
13338 | regs[i].mem_size = mem_size; | |
13339 | regs[i].id = ++env->id_gen; | |
13340 | } | |
51c39bb1 AS |
13341 | } |
13342 | } else { | |
13343 | /* 1st arg to a function */ | |
13344 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
13345 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 13346 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
13347 | if (ret == -EFAULT) |
13348 | /* unlikely verifier bug. abort. | |
13349 | * ret == 0 and ret < 0 are sadly acceptable for | |
13350 | * main() function due to backward compatibility. | |
13351 | * Like socket filter program may be written as: | |
13352 | * int bpf_prog(struct pt_regs *ctx) | |
13353 | * and never dereference that ctx in the program. | |
13354 | * 'struct pt_regs' is a type mismatch for socket | |
13355 | * filter that should be using 'struct __sk_buff'. | |
13356 | */ | |
13357 | goto out; | |
13358 | } | |
13359 | ||
13360 | ret = do_check(env); | |
13361 | out: | |
f59bbfc2 AS |
13362 | /* check for NULL is necessary, since cur_state can be freed inside |
13363 | * do_check() under memory pressure. | |
13364 | */ | |
13365 | if (env->cur_state) { | |
13366 | free_verifier_state(env->cur_state, true); | |
13367 | env->cur_state = NULL; | |
13368 | } | |
6f8a57cc AN |
13369 | while (!pop_stack(env, NULL, NULL, false)); |
13370 | if (!ret && pop_log) | |
13371 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 13372 | free_states(env); |
51c39bb1 AS |
13373 | return ret; |
13374 | } | |
13375 | ||
13376 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
13377 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
13378 | * Consider: | |
13379 | * int bar(int); | |
13380 | * int foo(int f) | |
13381 | * { | |
13382 | * return bar(f); | |
13383 | * } | |
13384 | * int bar(int b) | |
13385 | * { | |
13386 | * ... | |
13387 | * } | |
13388 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
13389 | * will be assumed that bar() already verified successfully and call to bar() | |
13390 | * from foo() will be checked for type match only. Later bar() will be verified | |
13391 | * independently to check that it's safe for R1=any_scalar_value. | |
13392 | */ | |
13393 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
13394 | { | |
13395 | struct bpf_prog_aux *aux = env->prog->aux; | |
13396 | int i, ret; | |
13397 | ||
13398 | if (!aux->func_info) | |
13399 | return 0; | |
13400 | ||
13401 | for (i = 1; i < env->subprog_cnt; i++) { | |
13402 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
13403 | continue; | |
13404 | env->insn_idx = env->subprog_info[i].start; | |
13405 | WARN_ON_ONCE(env->insn_idx == 0); | |
13406 | ret = do_check_common(env, i); | |
13407 | if (ret) { | |
13408 | return ret; | |
13409 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
13410 | verbose(env, | |
13411 | "Func#%d is safe for any args that match its prototype\n", | |
13412 | i); | |
13413 | } | |
13414 | } | |
13415 | return 0; | |
13416 | } | |
13417 | ||
13418 | static int do_check_main(struct bpf_verifier_env *env) | |
13419 | { | |
13420 | int ret; | |
13421 | ||
13422 | env->insn_idx = 0; | |
13423 | ret = do_check_common(env, 0); | |
13424 | if (!ret) | |
13425 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
13426 | return ret; | |
13427 | } | |
13428 | ||
13429 | ||
06ee7115 AS |
13430 | static void print_verification_stats(struct bpf_verifier_env *env) |
13431 | { | |
13432 | int i; | |
13433 | ||
13434 | if (env->log.level & BPF_LOG_STATS) { | |
13435 | verbose(env, "verification time %lld usec\n", | |
13436 | div_u64(env->verification_time, 1000)); | |
13437 | verbose(env, "stack depth "); | |
13438 | for (i = 0; i < env->subprog_cnt; i++) { | |
13439 | u32 depth = env->subprog_info[i].stack_depth; | |
13440 | ||
13441 | verbose(env, "%d", depth); | |
13442 | if (i + 1 < env->subprog_cnt) | |
13443 | verbose(env, "+"); | |
13444 | } | |
13445 | verbose(env, "\n"); | |
13446 | } | |
13447 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
13448 | "total_states %d peak_states %d mark_read %d\n", | |
13449 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
13450 | env->max_states_per_insn, env->total_states, | |
13451 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
13452 | } |
13453 | ||
27ae7997 MKL |
13454 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
13455 | { | |
13456 | const struct btf_type *t, *func_proto; | |
13457 | const struct bpf_struct_ops *st_ops; | |
13458 | const struct btf_member *member; | |
13459 | struct bpf_prog *prog = env->prog; | |
13460 | u32 btf_id, member_idx; | |
13461 | const char *mname; | |
13462 | ||
12aa8a94 THJ |
13463 | if (!prog->gpl_compatible) { |
13464 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
13465 | return -EINVAL; | |
13466 | } | |
13467 | ||
27ae7997 MKL |
13468 | btf_id = prog->aux->attach_btf_id; |
13469 | st_ops = bpf_struct_ops_find(btf_id); | |
13470 | if (!st_ops) { | |
13471 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
13472 | btf_id); | |
13473 | return -ENOTSUPP; | |
13474 | } | |
13475 | ||
13476 | t = st_ops->type; | |
13477 | member_idx = prog->expected_attach_type; | |
13478 | if (member_idx >= btf_type_vlen(t)) { | |
13479 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
13480 | member_idx, st_ops->name); | |
13481 | return -EINVAL; | |
13482 | } | |
13483 | ||
13484 | member = &btf_type_member(t)[member_idx]; | |
13485 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
13486 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
13487 | NULL); | |
13488 | if (!func_proto) { | |
13489 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
13490 | mname, member_idx, st_ops->name); | |
13491 | return -EINVAL; | |
13492 | } | |
13493 | ||
13494 | if (st_ops->check_member) { | |
13495 | int err = st_ops->check_member(t, member); | |
13496 | ||
13497 | if (err) { | |
13498 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
13499 | mname, st_ops->name); | |
13500 | return err; | |
13501 | } | |
13502 | } | |
13503 | ||
13504 | prog->aux->attach_func_proto = func_proto; | |
13505 | prog->aux->attach_func_name = mname; | |
13506 | env->ops = st_ops->verifier_ops; | |
13507 | ||
13508 | return 0; | |
13509 | } | |
6ba43b76 KS |
13510 | #define SECURITY_PREFIX "security_" |
13511 | ||
f7b12b6f | 13512 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 13513 | { |
69191754 | 13514 | if (within_error_injection_list(addr) || |
f7b12b6f | 13515 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 13516 | return 0; |
6ba43b76 | 13517 | |
6ba43b76 KS |
13518 | return -EINVAL; |
13519 | } | |
27ae7997 | 13520 | |
1e6c62a8 AS |
13521 | /* list of non-sleepable functions that are otherwise on |
13522 | * ALLOW_ERROR_INJECTION list | |
13523 | */ | |
13524 | BTF_SET_START(btf_non_sleepable_error_inject) | |
13525 | /* Three functions below can be called from sleepable and non-sleepable context. | |
13526 | * Assume non-sleepable from bpf safety point of view. | |
13527 | */ | |
9dd3d069 | 13528 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
13529 | BTF_ID(func, should_fail_alloc_page) |
13530 | BTF_ID(func, should_failslab) | |
13531 | BTF_SET_END(btf_non_sleepable_error_inject) | |
13532 | ||
13533 | static int check_non_sleepable_error_inject(u32 btf_id) | |
13534 | { | |
13535 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
13536 | } | |
13537 | ||
f7b12b6f THJ |
13538 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
13539 | const struct bpf_prog *prog, | |
13540 | const struct bpf_prog *tgt_prog, | |
13541 | u32 btf_id, | |
13542 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 13543 | { |
be8704ff | 13544 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 13545 | const char prefix[] = "btf_trace_"; |
5b92a28a | 13546 | int ret = 0, subprog = -1, i; |
38207291 | 13547 | const struct btf_type *t; |
5b92a28a | 13548 | bool conservative = true; |
38207291 | 13549 | const char *tname; |
5b92a28a | 13550 | struct btf *btf; |
f7b12b6f | 13551 | long addr = 0; |
38207291 | 13552 | |
f1b9509c | 13553 | if (!btf_id) { |
efc68158 | 13554 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
13555 | return -EINVAL; |
13556 | } | |
22dc4a0f | 13557 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 13558 | if (!btf) { |
efc68158 | 13559 | bpf_log(log, |
5b92a28a AS |
13560 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
13561 | return -EINVAL; | |
13562 | } | |
13563 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 13564 | if (!t) { |
efc68158 | 13565 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
13566 | return -EINVAL; |
13567 | } | |
5b92a28a | 13568 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 13569 | if (!tname) { |
efc68158 | 13570 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
13571 | return -EINVAL; |
13572 | } | |
5b92a28a AS |
13573 | if (tgt_prog) { |
13574 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
13575 | ||
13576 | for (i = 0; i < aux->func_info_cnt; i++) | |
13577 | if (aux->func_info[i].type_id == btf_id) { | |
13578 | subprog = i; | |
13579 | break; | |
13580 | } | |
13581 | if (subprog == -1) { | |
efc68158 | 13582 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
13583 | return -EINVAL; |
13584 | } | |
13585 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
13586 | if (prog_extension) { |
13587 | if (conservative) { | |
efc68158 | 13588 | bpf_log(log, |
be8704ff AS |
13589 | "Cannot replace static functions\n"); |
13590 | return -EINVAL; | |
13591 | } | |
13592 | if (!prog->jit_requested) { | |
efc68158 | 13593 | bpf_log(log, |
be8704ff AS |
13594 | "Extension programs should be JITed\n"); |
13595 | return -EINVAL; | |
13596 | } | |
be8704ff AS |
13597 | } |
13598 | if (!tgt_prog->jited) { | |
efc68158 | 13599 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
13600 | return -EINVAL; |
13601 | } | |
13602 | if (tgt_prog->type == prog->type) { | |
13603 | /* Cannot fentry/fexit another fentry/fexit program. | |
13604 | * Cannot attach program extension to another extension. | |
13605 | * It's ok to attach fentry/fexit to extension program. | |
13606 | */ | |
efc68158 | 13607 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
13608 | return -EINVAL; |
13609 | } | |
13610 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
13611 | prog_extension && | |
13612 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
13613 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
13614 | /* Program extensions can extend all program types | |
13615 | * except fentry/fexit. The reason is the following. | |
13616 | * The fentry/fexit programs are used for performance | |
13617 | * analysis, stats and can be attached to any program | |
13618 | * type except themselves. When extension program is | |
13619 | * replacing XDP function it is necessary to allow | |
13620 | * performance analysis of all functions. Both original | |
13621 | * XDP program and its program extension. Hence | |
13622 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
13623 | * allowed. If extending of fentry/fexit was allowed it | |
13624 | * would be possible to create long call chain | |
13625 | * fentry->extension->fentry->extension beyond | |
13626 | * reasonable stack size. Hence extending fentry is not | |
13627 | * allowed. | |
13628 | */ | |
efc68158 | 13629 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
13630 | return -EINVAL; |
13631 | } | |
5b92a28a | 13632 | } else { |
be8704ff | 13633 | if (prog_extension) { |
efc68158 | 13634 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
13635 | return -EINVAL; |
13636 | } | |
5b92a28a | 13637 | } |
f1b9509c AS |
13638 | |
13639 | switch (prog->expected_attach_type) { | |
13640 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 13641 | if (tgt_prog) { |
efc68158 | 13642 | bpf_log(log, |
5b92a28a AS |
13643 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
13644 | return -EINVAL; | |
13645 | } | |
38207291 | 13646 | if (!btf_type_is_typedef(t)) { |
efc68158 | 13647 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
13648 | btf_id); |
13649 | return -EINVAL; | |
13650 | } | |
f1b9509c | 13651 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 13652 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
13653 | btf_id, tname); |
13654 | return -EINVAL; | |
13655 | } | |
13656 | tname += sizeof(prefix) - 1; | |
5b92a28a | 13657 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
13658 | if (!btf_type_is_ptr(t)) |
13659 | /* should never happen in valid vmlinux build */ | |
13660 | return -EINVAL; | |
5b92a28a | 13661 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
13662 | if (!btf_type_is_func_proto(t)) |
13663 | /* should never happen in valid vmlinux build */ | |
13664 | return -EINVAL; | |
13665 | ||
f7b12b6f | 13666 | break; |
15d83c4d YS |
13667 | case BPF_TRACE_ITER: |
13668 | if (!btf_type_is_func(t)) { | |
efc68158 | 13669 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
13670 | btf_id); |
13671 | return -EINVAL; | |
13672 | } | |
13673 | t = btf_type_by_id(btf, t->type); | |
13674 | if (!btf_type_is_func_proto(t)) | |
13675 | return -EINVAL; | |
f7b12b6f THJ |
13676 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
13677 | if (ret) | |
13678 | return ret; | |
13679 | break; | |
be8704ff AS |
13680 | default: |
13681 | if (!prog_extension) | |
13682 | return -EINVAL; | |
df561f66 | 13683 | fallthrough; |
ae240823 | 13684 | case BPF_MODIFY_RETURN: |
9e4e01df | 13685 | case BPF_LSM_MAC: |
fec56f58 AS |
13686 | case BPF_TRACE_FENTRY: |
13687 | case BPF_TRACE_FEXIT: | |
13688 | if (!btf_type_is_func(t)) { | |
efc68158 | 13689 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
13690 | btf_id); |
13691 | return -EINVAL; | |
13692 | } | |
be8704ff | 13693 | if (prog_extension && |
efc68158 | 13694 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 13695 | return -EINVAL; |
5b92a28a | 13696 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
13697 | if (!btf_type_is_func_proto(t)) |
13698 | return -EINVAL; | |
f7b12b6f | 13699 | |
4a1e7c0c THJ |
13700 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
13701 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
13702 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
13703 | return -EINVAL; | |
13704 | ||
f7b12b6f | 13705 | if (tgt_prog && conservative) |
5b92a28a | 13706 | t = NULL; |
f7b12b6f THJ |
13707 | |
13708 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 13709 | if (ret < 0) |
f7b12b6f THJ |
13710 | return ret; |
13711 | ||
5b92a28a | 13712 | if (tgt_prog) { |
e9eeec58 YS |
13713 | if (subprog == 0) |
13714 | addr = (long) tgt_prog->bpf_func; | |
13715 | else | |
13716 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
13717 | } else { |
13718 | addr = kallsyms_lookup_name(tname); | |
13719 | if (!addr) { | |
efc68158 | 13720 | bpf_log(log, |
5b92a28a AS |
13721 | "The address of function %s cannot be found\n", |
13722 | tname); | |
f7b12b6f | 13723 | return -ENOENT; |
5b92a28a | 13724 | } |
fec56f58 | 13725 | } |
18644cec | 13726 | |
1e6c62a8 AS |
13727 | if (prog->aux->sleepable) { |
13728 | ret = -EINVAL; | |
13729 | switch (prog->type) { | |
13730 | case BPF_PROG_TYPE_TRACING: | |
13731 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
13732 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
13733 | */ | |
13734 | if (!check_non_sleepable_error_inject(btf_id) && | |
13735 | within_error_injection_list(addr)) | |
13736 | ret = 0; | |
13737 | break; | |
13738 | case BPF_PROG_TYPE_LSM: | |
13739 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
13740 | * Only some of them are sleepable. | |
13741 | */ | |
423f1610 | 13742 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
13743 | ret = 0; |
13744 | break; | |
13745 | default: | |
13746 | break; | |
13747 | } | |
f7b12b6f THJ |
13748 | if (ret) { |
13749 | bpf_log(log, "%s is not sleepable\n", tname); | |
13750 | return ret; | |
13751 | } | |
1e6c62a8 | 13752 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 13753 | if (tgt_prog) { |
efc68158 | 13754 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
13755 | return -EINVAL; |
13756 | } | |
13757 | ret = check_attach_modify_return(addr, tname); | |
13758 | if (ret) { | |
13759 | bpf_log(log, "%s() is not modifiable\n", tname); | |
13760 | return ret; | |
1af9270e | 13761 | } |
18644cec | 13762 | } |
f7b12b6f THJ |
13763 | |
13764 | break; | |
13765 | } | |
13766 | tgt_info->tgt_addr = addr; | |
13767 | tgt_info->tgt_name = tname; | |
13768 | tgt_info->tgt_type = t; | |
13769 | return 0; | |
13770 | } | |
13771 | ||
35e3815f JO |
13772 | BTF_SET_START(btf_id_deny) |
13773 | BTF_ID_UNUSED | |
13774 | #ifdef CONFIG_SMP | |
13775 | BTF_ID(func, migrate_disable) | |
13776 | BTF_ID(func, migrate_enable) | |
13777 | #endif | |
13778 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
13779 | BTF_ID(func, rcu_read_unlock_strict) | |
13780 | #endif | |
13781 | BTF_SET_END(btf_id_deny) | |
13782 | ||
f7b12b6f THJ |
13783 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
13784 | { | |
13785 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 13786 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
13787 | struct bpf_attach_target_info tgt_info = {}; |
13788 | u32 btf_id = prog->aux->attach_btf_id; | |
13789 | struct bpf_trampoline *tr; | |
13790 | int ret; | |
13791 | u64 key; | |
13792 | ||
79a7f8bd AS |
13793 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
13794 | if (prog->aux->sleepable) | |
13795 | /* attach_btf_id checked to be zero already */ | |
13796 | return 0; | |
13797 | verbose(env, "Syscall programs can only be sleepable\n"); | |
13798 | return -EINVAL; | |
13799 | } | |
13800 | ||
f7b12b6f THJ |
13801 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
13802 | prog->type != BPF_PROG_TYPE_LSM) { | |
13803 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); | |
13804 | return -EINVAL; | |
13805 | } | |
13806 | ||
13807 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
13808 | return check_struct_ops_btf_id(env); | |
13809 | ||
13810 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
13811 | prog->type != BPF_PROG_TYPE_LSM && | |
13812 | prog->type != BPF_PROG_TYPE_EXT) | |
13813 | return 0; | |
13814 | ||
13815 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
13816 | if (ret) | |
fec56f58 | 13817 | return ret; |
f7b12b6f THJ |
13818 | |
13819 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
13820 | /* to make freplace equivalent to their targets, they need to |
13821 | * inherit env->ops and expected_attach_type for the rest of the | |
13822 | * verification | |
13823 | */ | |
f7b12b6f THJ |
13824 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
13825 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
13826 | } | |
13827 | ||
13828 | /* store info about the attachment target that will be used later */ | |
13829 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
13830 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
13831 | ||
4a1e7c0c THJ |
13832 | if (tgt_prog) { |
13833 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
13834 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
13835 | } | |
13836 | ||
f7b12b6f THJ |
13837 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
13838 | prog->aux->attach_btf_trace = true; | |
13839 | return 0; | |
13840 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
13841 | if (!bpf_iter_prog_supported(prog)) | |
13842 | return -EINVAL; | |
13843 | return 0; | |
13844 | } | |
13845 | ||
13846 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
13847 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
13848 | if (ret < 0) | |
13849 | return ret; | |
35e3815f JO |
13850 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
13851 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
13852 | return -EINVAL; | |
38207291 | 13853 | } |
f7b12b6f | 13854 | |
22dc4a0f | 13855 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
13856 | tr = bpf_trampoline_get(key, &tgt_info); |
13857 | if (!tr) | |
13858 | return -ENOMEM; | |
13859 | ||
3aac1ead | 13860 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 13861 | return 0; |
38207291 MKL |
13862 | } |
13863 | ||
76654e67 AM |
13864 | struct btf *bpf_get_btf_vmlinux(void) |
13865 | { | |
13866 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
13867 | mutex_lock(&bpf_verifier_lock); | |
13868 | if (!btf_vmlinux) | |
13869 | btf_vmlinux = btf_parse_vmlinux(); | |
13870 | mutex_unlock(&bpf_verifier_lock); | |
13871 | } | |
13872 | return btf_vmlinux; | |
13873 | } | |
13874 | ||
af2ac3e1 | 13875 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 13876 | { |
06ee7115 | 13877 | u64 start_time = ktime_get_ns(); |
58e2af8b | 13878 | struct bpf_verifier_env *env; |
b9193c1b | 13879 | struct bpf_verifier_log *log; |
9e4c24e7 | 13880 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 13881 | bool is_priv; |
51580e79 | 13882 | |
eba0c929 AB |
13883 | /* no program is valid */ |
13884 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
13885 | return -EINVAL; | |
13886 | ||
58e2af8b | 13887 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
13888 | * allocate/free it every time bpf_check() is called |
13889 | */ | |
58e2af8b | 13890 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
13891 | if (!env) |
13892 | return -ENOMEM; | |
61bd5218 | 13893 | log = &env->log; |
cbd35700 | 13894 | |
9e4c24e7 | 13895 | len = (*prog)->len; |
fad953ce | 13896 | env->insn_aux_data = |
9e4c24e7 | 13897 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
13898 | ret = -ENOMEM; |
13899 | if (!env->insn_aux_data) | |
13900 | goto err_free_env; | |
9e4c24e7 JK |
13901 | for (i = 0; i < len; i++) |
13902 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 13903 | env->prog = *prog; |
00176a34 | 13904 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 13905 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 13906 | is_priv = bpf_capable(); |
0246e64d | 13907 | |
76654e67 | 13908 | bpf_get_btf_vmlinux(); |
8580ac94 | 13909 | |
cbd35700 | 13910 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
13911 | if (!is_priv) |
13912 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
13913 | |
13914 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
13915 | /* user requested verbose verifier output | |
13916 | * and supplied buffer to store the verification trace | |
13917 | */ | |
e7bf8249 JK |
13918 | log->level = attr->log_level; |
13919 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
13920 | log->len_total = attr->log_size; | |
cbd35700 AS |
13921 | |
13922 | ret = -EINVAL; | |
e7bf8249 | 13923 | /* log attributes have to be sane */ |
7a9f5c65 | 13924 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 13925 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 13926 | goto err_unlock; |
cbd35700 | 13927 | } |
1ad2f583 | 13928 | |
8580ac94 AS |
13929 | if (IS_ERR(btf_vmlinux)) { |
13930 | /* Either gcc or pahole or kernel are broken. */ | |
13931 | verbose(env, "in-kernel BTF is malformed\n"); | |
13932 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 13933 | goto skip_full_check; |
8580ac94 AS |
13934 | } |
13935 | ||
1ad2f583 DB |
13936 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
13937 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 13938 | env->strict_alignment = true; |
e9ee9efc DM |
13939 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
13940 | env->strict_alignment = false; | |
cbd35700 | 13941 | |
2c78ee89 | 13942 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 13943 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
41c48f3a | 13944 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
13945 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
13946 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
13947 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 13948 | |
10d274e8 AS |
13949 | if (is_priv) |
13950 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
13951 | ||
dc2a4ebc | 13952 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 13953 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
13954 | GFP_USER); |
13955 | ret = -ENOMEM; | |
13956 | if (!env->explored_states) | |
13957 | goto skip_full_check; | |
13958 | ||
e6ac2450 MKL |
13959 | ret = add_subprog_and_kfunc(env); |
13960 | if (ret < 0) | |
13961 | goto skip_full_check; | |
13962 | ||
d9762e84 | 13963 | ret = check_subprogs(env); |
475fb78f AS |
13964 | if (ret < 0) |
13965 | goto skip_full_check; | |
13966 | ||
c454a46b | 13967 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
13968 | if (ret < 0) |
13969 | goto skip_full_check; | |
13970 | ||
be8704ff AS |
13971 | ret = check_attach_btf_id(env); |
13972 | if (ret) | |
13973 | goto skip_full_check; | |
13974 | ||
4976b718 HL |
13975 | ret = resolve_pseudo_ldimm64(env); |
13976 | if (ret < 0) | |
13977 | goto skip_full_check; | |
13978 | ||
ceb11679 YZ |
13979 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
13980 | ret = bpf_prog_offload_verifier_prep(env->prog); | |
13981 | if (ret) | |
13982 | goto skip_full_check; | |
13983 | } | |
13984 | ||
d9762e84 MKL |
13985 | ret = check_cfg(env); |
13986 | if (ret < 0) | |
13987 | goto skip_full_check; | |
13988 | ||
51c39bb1 AS |
13989 | ret = do_check_subprogs(env); |
13990 | ret = ret ?: do_check_main(env); | |
cbd35700 | 13991 | |
c941ce9c QM |
13992 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
13993 | ret = bpf_prog_offload_finalize(env); | |
13994 | ||
0246e64d | 13995 | skip_full_check: |
51c39bb1 | 13996 | kvfree(env->explored_states); |
0246e64d | 13997 | |
c131187d | 13998 | if (ret == 0) |
9b38c405 | 13999 | ret = check_max_stack_depth(env); |
c131187d | 14000 | |
9b38c405 | 14001 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
14002 | if (is_priv) { |
14003 | if (ret == 0) | |
14004 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
14005 | if (ret == 0) |
14006 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
14007 | if (ret == 0) |
14008 | ret = opt_remove_nops(env); | |
52875a04 JK |
14009 | } else { |
14010 | if (ret == 0) | |
14011 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
14012 | } |
14013 | ||
9bac3d6d AS |
14014 | if (ret == 0) |
14015 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
14016 | ret = convert_ctx_accesses(env); | |
14017 | ||
e245c5c6 | 14018 | if (ret == 0) |
e6ac5933 | 14019 | ret = do_misc_fixups(env); |
e245c5c6 | 14020 | |
a4b1d3c1 JW |
14021 | /* do 32-bit optimization after insn patching has done so those patched |
14022 | * insns could be handled correctly. | |
14023 | */ | |
d6c2308c JW |
14024 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
14025 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
14026 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
14027 | : false; | |
a4b1d3c1 JW |
14028 | } |
14029 | ||
1ea47e01 AS |
14030 | if (ret == 0) |
14031 | ret = fixup_call_args(env); | |
14032 | ||
06ee7115 AS |
14033 | env->verification_time = ktime_get_ns() - start_time; |
14034 | print_verification_stats(env); | |
aba64c7d | 14035 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 14036 | |
a2a7d570 | 14037 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 14038 | ret = -ENOSPC; |
a2a7d570 | 14039 | if (log->level && !log->ubuf) { |
cbd35700 | 14040 | ret = -EFAULT; |
a2a7d570 | 14041 | goto err_release_maps; |
cbd35700 AS |
14042 | } |
14043 | ||
541c3bad AN |
14044 | if (ret) |
14045 | goto err_release_maps; | |
14046 | ||
14047 | if (env->used_map_cnt) { | |
0246e64d | 14048 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
14049 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
14050 | sizeof(env->used_maps[0]), | |
14051 | GFP_KERNEL); | |
0246e64d | 14052 | |
9bac3d6d | 14053 | if (!env->prog->aux->used_maps) { |
0246e64d | 14054 | ret = -ENOMEM; |
a2a7d570 | 14055 | goto err_release_maps; |
0246e64d AS |
14056 | } |
14057 | ||
9bac3d6d | 14058 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 14059 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 14060 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
14061 | } |
14062 | if (env->used_btf_cnt) { | |
14063 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
14064 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
14065 | sizeof(env->used_btfs[0]), | |
14066 | GFP_KERNEL); | |
14067 | if (!env->prog->aux->used_btfs) { | |
14068 | ret = -ENOMEM; | |
14069 | goto err_release_maps; | |
14070 | } | |
0246e64d | 14071 | |
541c3bad AN |
14072 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
14073 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
14074 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
14075 | } | |
14076 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
14077 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
14078 | * bpf_ld_imm64 instructions | |
14079 | */ | |
14080 | convert_pseudo_ld_imm64(env); | |
14081 | } | |
cbd35700 | 14082 | |
541c3bad | 14083 | adjust_btf_func(env); |
ba64e7d8 | 14084 | |
a2a7d570 | 14085 | err_release_maps: |
9bac3d6d | 14086 | if (!env->prog->aux->used_maps) |
0246e64d | 14087 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 14088 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
14089 | */ |
14090 | release_maps(env); | |
541c3bad AN |
14091 | if (!env->prog->aux->used_btfs) |
14092 | release_btfs(env); | |
03f87c0b THJ |
14093 | |
14094 | /* extension progs temporarily inherit the attach_type of their targets | |
14095 | for verification purposes, so set it back to zero before returning | |
14096 | */ | |
14097 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
14098 | env->prog->expected_attach_type = 0; | |
14099 | ||
9bac3d6d | 14100 | *prog = env->prog; |
3df126f3 | 14101 | err_unlock: |
45a73c17 AS |
14102 | if (!is_priv) |
14103 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
14104 | vfree(env->insn_aux_data); |
14105 | err_free_env: | |
14106 | kfree(env); | |
51580e79 AS |
14107 | return ret; |
14108 | } |