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> |
aef2feda | 7 | #include <linux/bpf-cgroup.h> |
51580e79 AS |
8 | #include <linux/kernel.h> |
9 | #include <linux/types.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/bpf.h> | |
838e9690 | 12 | #include <linux/btf.h> |
58e2af8b | 13 | #include <linux/bpf_verifier.h> |
51580e79 AS |
14 | #include <linux/filter.h> |
15 | #include <net/netlink.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/vmalloc.h> | |
ebb676da | 18 | #include <linux/stringify.h> |
cc8b0b92 AS |
19 | #include <linux/bsearch.h> |
20 | #include <linux/sort.h> | |
c195651e | 21 | #include <linux/perf_event.h> |
d9762e84 | 22 | #include <linux/ctype.h> |
6ba43b76 | 23 | #include <linux/error-injection.h> |
9e4e01df | 24 | #include <linux/bpf_lsm.h> |
1e6c62a8 | 25 | #include <linux/btf_ids.h> |
47e34cb7 | 26 | #include <linux/poison.h> |
bd5314f8 | 27 | #include <linux/module.h> |
51580e79 | 28 | |
f4ac7e0b JK |
29 | #include "disasm.h" |
30 | ||
00176a34 | 31 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 32 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
33 | [_id] = & _name ## _verifier_ops, |
34 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 35 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
36 | #include <linux/bpf_types.h> |
37 | #undef BPF_PROG_TYPE | |
38 | #undef BPF_MAP_TYPE | |
f2e10bff | 39 | #undef BPF_LINK_TYPE |
00176a34 JK |
40 | }; |
41 | ||
51580e79 AS |
42 | /* bpf_check() is a static code analyzer that walks eBPF program |
43 | * instruction by instruction and updates register/stack state. | |
44 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
45 | * | |
46 | * The first pass is depth-first-search to check that the program is a DAG. | |
47 | * It rejects the following programs: | |
48 | * - larger than BPF_MAXINSNS insns | |
49 | * - if loop is present (detected via back-edge) | |
50 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
51 | * - out of bounds or malformed jumps | |
52 | * The second pass is all possible path descent from the 1st insn. | |
8fb33b60 | 53 | * Since it's analyzing all paths through the program, the length of the |
eba38a96 | 54 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
55 | * insn is less then 4K, but there are too many branches that change stack/regs. |
56 | * Number of 'branches to be analyzed' is limited to 1k | |
57 | * | |
58 | * On entry to each instruction, each register has a type, and the instruction | |
59 | * changes the types of the registers depending on instruction semantics. | |
60 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
61 | * copied to R1. | |
62 | * | |
63 | * All registers are 64-bit. | |
64 | * R0 - return register | |
65 | * R1-R5 argument passing registers | |
66 | * R6-R9 callee saved registers | |
67 | * R10 - frame pointer read-only | |
68 | * | |
69 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
70 | * and has type PTR_TO_CTX. | |
71 | * | |
72 | * Verifier tracks arithmetic operations on pointers in case: | |
73 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
74 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
75 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
76 | * and 2nd arithmetic instruction is pattern matched to recognize | |
77 | * that it wants to construct a pointer to some element within stack. | |
78 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
79 | * (and -20 constant is saved for further stack bounds checking). | |
80 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
81 | * | |
f1174f77 | 82 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 83 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 84 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
85 | * |
86 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
87 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
88 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
89 | * |
90 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
91 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
92 | * | |
93 | * registers used to pass values to function calls are checked against | |
94 | * function argument constraints. | |
95 | * | |
96 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
97 | * It means that the register type passed to this function must be | |
98 | * PTR_TO_STACK and it will be used inside the function as | |
99 | * 'pointer to map element key' | |
100 | * | |
101 | * For example the argument constraints for bpf_map_lookup_elem(): | |
102 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
103 | * .arg1_type = ARG_CONST_MAP_PTR, | |
104 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
105 | * | |
106 | * ret_type says that this function returns 'pointer to map elem value or null' | |
107 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
108 | * 2nd argument should be a pointer to stack, which will be used inside | |
109 | * the helper function as a pointer to map element key. | |
110 | * | |
111 | * On the kernel side the helper function looks like: | |
112 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
113 | * { | |
114 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
115 | * void *key = (void *) (unsigned long) r2; | |
116 | * void *value; | |
117 | * | |
118 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
119 | * [key, key + map->key_size) bytes are valid and were initialized on | |
120 | * the stack of eBPF program. | |
121 | * } | |
122 | * | |
123 | * Corresponding eBPF program may look like: | |
124 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
125 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
126 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
127 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
128 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
129 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
130 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
131 | * | |
132 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
133 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
134 | * and were initialized prior to this call. | |
135 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
136 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
137 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
8fb33b60 | 138 | * returns either pointer to map value or NULL. |
51580e79 AS |
139 | * |
140 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
141 | * insn, the register holding that pointer in the true branch changes state to | |
142 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
143 | * branch. See check_cond_jmp_op(). | |
144 | * | |
145 | * After the call R0 is set to return type of the function and registers R1-R5 | |
146 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
147 | * |
148 | * The following reference types represent a potential reference to a kernel | |
149 | * resource which, after first being allocated, must be checked and freed by | |
150 | * the BPF program: | |
151 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
152 | * | |
153 | * When the verifier sees a helper call return a reference type, it allocates a | |
154 | * pointer id for the reference and stores it in the current function state. | |
155 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
156 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
157 | * passes through a NULL-check conditional. For the branch wherein the state is | |
158 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
159 | * |
160 | * For each helper function that allocates a reference, such as | |
161 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
162 | * bpf_sk_release(). When a reference type passes into the release function, | |
163 | * the verifier also releases the reference. If any unchecked or unreleased | |
164 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
165 | */ |
166 | ||
17a52670 | 167 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 168 | struct bpf_verifier_stack_elem { |
17a52670 AS |
169 | /* verifer state is 'st' |
170 | * before processing instruction 'insn_idx' | |
171 | * and after processing instruction 'prev_insn_idx' | |
172 | */ | |
58e2af8b | 173 | struct bpf_verifier_state st; |
17a52670 AS |
174 | int insn_idx; |
175 | int prev_insn_idx; | |
58e2af8b | 176 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
177 | /* length of verifier log at the time this state was pushed on stack */ |
178 | u32 log_pos; | |
cbd35700 AS |
179 | }; |
180 | ||
b285fcb7 | 181 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 182 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 183 | |
d2e4c1e6 DB |
184 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
185 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
186 | ||
c93552c4 DB |
187 | #define BPF_MAP_PTR_UNPRIV 1UL |
188 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
189 | POISON_POINTER_DELTA)) | |
190 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
191 | ||
bc34dee6 JK |
192 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx); |
193 | static int release_reference(struct bpf_verifier_env *env, int ref_obj_id); | |
6a3cd331 | 194 | static void invalidate_non_owning_refs(struct bpf_verifier_env *env); |
5d92ddc3 | 195 | static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env); |
6a3cd331 DM |
196 | static int ref_set_non_owning(struct bpf_verifier_env *env, |
197 | struct bpf_reg_state *reg); | |
bc34dee6 | 198 | |
c93552c4 DB |
199 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
200 | { | |
d2e4c1e6 | 201 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
202 | } |
203 | ||
204 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
205 | { | |
d2e4c1e6 | 206 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
207 | } |
208 | ||
209 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
210 | const struct bpf_map *map, bool unpriv) | |
211 | { | |
212 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
213 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
214 | aux->map_ptr_state = (unsigned long)map | |
215 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
216 | } | |
217 | ||
218 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
219 | { | |
220 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
221 | } | |
222 | ||
223 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
224 | { | |
225 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
226 | } | |
227 | ||
228 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
229 | { | |
230 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
231 | } | |
232 | ||
233 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
234 | { | |
235 | bool poisoned = bpf_map_key_poisoned(aux); | |
236 | ||
237 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
238 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 239 | } |
fad73a1a | 240 | |
23a2d70c YS |
241 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
242 | { | |
243 | return insn->code == (BPF_JMP | BPF_CALL) && | |
244 | insn->src_reg == BPF_PSEUDO_CALL; | |
245 | } | |
246 | ||
e6ac2450 MKL |
247 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
248 | { | |
249 | return insn->code == (BPF_JMP | BPF_CALL) && | |
250 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; | |
251 | } | |
252 | ||
33ff9823 DB |
253 | struct bpf_call_arg_meta { |
254 | struct bpf_map *map_ptr; | |
435faee1 | 255 | bool raw_mode; |
36bbef52 | 256 | bool pkt_access; |
8f14852e | 257 | u8 release_regno; |
435faee1 DB |
258 | int regno; |
259 | int access_size; | |
457f4436 | 260 | int mem_size; |
10060503 | 261 | u64 msize_max_value; |
1b986589 | 262 | int ref_obj_id; |
f8064ab9 | 263 | int dynptr_id; |
3e8ce298 | 264 | int map_uid; |
d83525ca | 265 | int func_id; |
22dc4a0f | 266 | struct btf *btf; |
eaa6bcb7 | 267 | u32 btf_id; |
22dc4a0f | 268 | struct btf *ret_btf; |
eaa6bcb7 | 269 | u32 ret_btf_id; |
69c087ba | 270 | u32 subprogno; |
aa3496ac | 271 | struct btf_field *kptr_field; |
33ff9823 DB |
272 | }; |
273 | ||
d0e1ac22 AN |
274 | struct bpf_kfunc_call_arg_meta { |
275 | /* In parameters */ | |
276 | struct btf *btf; | |
277 | u32 func_id; | |
278 | u32 kfunc_flags; | |
279 | const struct btf_type *func_proto; | |
280 | const char *func_name; | |
281 | /* Out parameters */ | |
282 | u32 ref_obj_id; | |
283 | u8 release_regno; | |
284 | bool r0_rdonly; | |
285 | u32 ret_btf_id; | |
286 | u64 r0_size; | |
287 | u32 subprogno; | |
288 | struct { | |
289 | u64 value; | |
290 | bool found; | |
291 | } arg_constant; | |
292 | struct { | |
293 | struct btf *btf; | |
294 | u32 btf_id; | |
295 | } arg_obj_drop; | |
296 | struct { | |
297 | struct btf_field *field; | |
298 | } arg_list_head; | |
299 | struct { | |
300 | struct btf_field *field; | |
301 | } arg_rbtree_root; | |
302 | struct { | |
303 | enum bpf_dynptr_type type; | |
304 | u32 id; | |
305 | } initialized_dynptr; | |
06accc87 AN |
306 | struct { |
307 | u8 spi; | |
308 | u8 frameno; | |
309 | } iter; | |
d0e1ac22 AN |
310 | u64 mem_size; |
311 | }; | |
312 | ||
8580ac94 AS |
313 | struct btf *btf_vmlinux; |
314 | ||
cbd35700 AS |
315 | static DEFINE_MUTEX(bpf_verifier_lock); |
316 | ||
d9762e84 MKL |
317 | static const struct bpf_line_info * |
318 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
319 | { | |
320 | const struct bpf_line_info *linfo; | |
321 | const struct bpf_prog *prog; | |
322 | u32 i, nr_linfo; | |
323 | ||
324 | prog = env->prog; | |
325 | nr_linfo = prog->aux->nr_linfo; | |
326 | ||
327 | if (!nr_linfo || insn_off >= prog->len) | |
328 | return NULL; | |
329 | ||
330 | linfo = prog->aux->linfo; | |
331 | for (i = 1; i < nr_linfo; i++) | |
332 | if (insn_off < linfo[i].insn_off) | |
333 | break; | |
334 | ||
335 | return &linfo[i - 1]; | |
336 | } | |
337 | ||
77d2e05a MKL |
338 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
339 | va_list args) | |
cbd35700 | 340 | { |
a2a7d570 | 341 | unsigned int n; |
cbd35700 | 342 | |
a2a7d570 | 343 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
344 | |
345 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
346 | "verifier log line truncated - local buffer too short\n"); | |
347 | ||
8580ac94 | 348 | if (log->level == BPF_LOG_KERNEL) { |
436d404c HT |
349 | bool newline = n > 0 && log->kbuf[n - 1] == '\n'; |
350 | ||
351 | pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); | |
8580ac94 AS |
352 | return; |
353 | } | |
436d404c HT |
354 | |
355 | n = min(log->len_total - log->len_used - 1, n); | |
356 | log->kbuf[n] = '\0'; | |
a2a7d570 JK |
357 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
358 | log->len_used += n; | |
359 | else | |
360 | log->ubuf = NULL; | |
cbd35700 | 361 | } |
abe08840 | 362 | |
6f8a57cc AN |
363 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
364 | { | |
365 | char zero = 0; | |
366 | ||
367 | if (!bpf_verifier_log_needed(log)) | |
368 | return; | |
369 | ||
370 | log->len_used = new_pos; | |
371 | if (put_user(zero, log->ubuf + new_pos)) | |
372 | log->ubuf = NULL; | |
373 | } | |
374 | ||
abe08840 JO |
375 | /* log_level controls verbosity level of eBPF verifier. |
376 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
377 | * so the user can figure out what's wrong with the program | |
430e68d1 | 378 | */ |
abe08840 JO |
379 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
380 | const char *fmt, ...) | |
381 | { | |
382 | va_list args; | |
383 | ||
77d2e05a MKL |
384 | if (!bpf_verifier_log_needed(&env->log)) |
385 | return; | |
386 | ||
abe08840 | 387 | va_start(args, fmt); |
77d2e05a | 388 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
389 | va_end(args); |
390 | } | |
391 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
392 | ||
393 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
394 | { | |
77d2e05a | 395 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
396 | va_list args; |
397 | ||
77d2e05a MKL |
398 | if (!bpf_verifier_log_needed(&env->log)) |
399 | return; | |
400 | ||
abe08840 | 401 | va_start(args, fmt); |
77d2e05a | 402 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
403 | va_end(args); |
404 | } | |
cbd35700 | 405 | |
9e15db66 AS |
406 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
407 | const char *fmt, ...) | |
408 | { | |
409 | va_list args; | |
410 | ||
411 | if (!bpf_verifier_log_needed(log)) | |
412 | return; | |
413 | ||
414 | va_start(args, fmt); | |
415 | bpf_verifier_vlog(log, fmt, args); | |
416 | va_end(args); | |
417 | } | |
84c6ac41 | 418 | EXPORT_SYMBOL_GPL(bpf_log); |
9e15db66 | 419 | |
d9762e84 MKL |
420 | static const char *ltrim(const char *s) |
421 | { | |
422 | while (isspace(*s)) | |
423 | s++; | |
424 | ||
425 | return s; | |
426 | } | |
427 | ||
428 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
429 | u32 insn_off, | |
430 | const char *prefix_fmt, ...) | |
431 | { | |
432 | const struct bpf_line_info *linfo; | |
433 | ||
434 | if (!bpf_verifier_log_needed(&env->log)) | |
435 | return; | |
436 | ||
437 | linfo = find_linfo(env, insn_off); | |
438 | if (!linfo || linfo == env->prev_linfo) | |
439 | return; | |
440 | ||
441 | if (prefix_fmt) { | |
442 | va_list args; | |
443 | ||
444 | va_start(args, prefix_fmt); | |
445 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
446 | va_end(args); | |
447 | } | |
448 | ||
449 | verbose(env, "%s\n", | |
450 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
451 | linfo->line_off))); | |
452 | ||
453 | env->prev_linfo = linfo; | |
454 | } | |
455 | ||
bc2591d6 YS |
456 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
457 | struct bpf_reg_state *reg, | |
458 | struct tnum *range, const char *ctx, | |
459 | const char *reg_name) | |
460 | { | |
461 | char tn_buf[48]; | |
462 | ||
463 | verbose(env, "At %s the register %s ", ctx, reg_name); | |
464 | if (!tnum_is_unknown(reg->var_off)) { | |
465 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
466 | verbose(env, "has value %s", tn_buf); | |
467 | } else { | |
468 | verbose(env, "has unknown scalar value"); | |
469 | } | |
470 | tnum_strn(tn_buf, sizeof(tn_buf), *range); | |
471 | verbose(env, " should have been in %s\n", tn_buf); | |
472 | } | |
473 | ||
de8f3a83 DB |
474 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
475 | { | |
0c9a7a7e | 476 | type = base_type(type); |
de8f3a83 DB |
477 | return type == PTR_TO_PACKET || |
478 | type == PTR_TO_PACKET_META; | |
479 | } | |
480 | ||
46f8bc92 MKL |
481 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
482 | { | |
483 | return type == PTR_TO_SOCKET || | |
655a51e5 | 484 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
485 | type == PTR_TO_TCP_SOCK || |
486 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
487 | } |
488 | ||
1057d299 AS |
489 | static bool type_may_be_null(u32 type) |
490 | { | |
491 | return type & PTR_MAYBE_NULL; | |
492 | } | |
493 | ||
cac616db JF |
494 | static bool reg_type_not_null(enum bpf_reg_type type) |
495 | { | |
1057d299 AS |
496 | if (type_may_be_null(type)) |
497 | return false; | |
498 | ||
499 | type = base_type(type); | |
cac616db JF |
500 | return type == PTR_TO_SOCKET || |
501 | type == PTR_TO_TCP_SOCK || | |
502 | type == PTR_TO_MAP_VALUE || | |
69c087ba | 503 | type == PTR_TO_MAP_KEY || |
d5271c5b AN |
504 | type == PTR_TO_SOCK_COMMON || |
505 | type == PTR_TO_MEM; | |
cac616db JF |
506 | } |
507 | ||
d8939cb0 DM |
508 | static bool type_is_ptr_alloc_obj(u32 type) |
509 | { | |
510 | return base_type(type) == PTR_TO_BTF_ID && type_flag(type) & MEM_ALLOC; | |
511 | } | |
512 | ||
6a3cd331 DM |
513 | static bool type_is_non_owning_ref(u32 type) |
514 | { | |
515 | return type_is_ptr_alloc_obj(type) && type_flag(type) & NON_OWN_REF; | |
516 | } | |
517 | ||
4e814da0 KKD |
518 | static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg) |
519 | { | |
520 | struct btf_record *rec = NULL; | |
521 | struct btf_struct_meta *meta; | |
522 | ||
523 | if (reg->type == PTR_TO_MAP_VALUE) { | |
524 | rec = reg->map_ptr->record; | |
d8939cb0 | 525 | } else if (type_is_ptr_alloc_obj(reg->type)) { |
4e814da0 KKD |
526 | meta = btf_find_struct_meta(reg->btf, reg->btf_id); |
527 | if (meta) | |
528 | rec = meta->record; | |
529 | } | |
530 | return rec; | |
531 | } | |
532 | ||
d83525ca AS |
533 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
534 | { | |
4e814da0 | 535 | return btf_record_has_field(reg_btf_record(reg), BPF_SPIN_LOCK); |
cba368c1 MKL |
536 | } |
537 | ||
20b2aff4 HL |
538 | static bool type_is_rdonly_mem(u32 type) |
539 | { | |
540 | return type & MEM_RDONLY; | |
cba368c1 MKL |
541 | } |
542 | ||
64d85290 JS |
543 | static bool is_acquire_function(enum bpf_func_id func_id, |
544 | const struct bpf_map *map) | |
545 | { | |
546 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
547 | ||
548 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
549 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 | 550 | func_id == BPF_FUNC_skc_lookup_tcp || |
c0a5a21c KKD |
551 | func_id == BPF_FUNC_ringbuf_reserve || |
552 | func_id == BPF_FUNC_kptr_xchg) | |
64d85290 JS |
553 | return true; |
554 | ||
555 | if (func_id == BPF_FUNC_map_lookup_elem && | |
556 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
557 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
558 | return true; | |
559 | ||
560 | return false; | |
46f8bc92 MKL |
561 | } |
562 | ||
1b986589 MKL |
563 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
564 | { | |
565 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
566 | func_id == BPF_FUNC_sk_fullsock || |
567 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
568 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
569 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
3bc253c2 | 570 | func_id == BPF_FUNC_skc_to_mptcp_sock || |
1df8f55a MKL |
571 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
572 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
573 | } |
574 | ||
88374342 | 575 | static bool is_dynptr_ref_function(enum bpf_func_id func_id) |
b2d8ef19 DM |
576 | { |
577 | return func_id == BPF_FUNC_dynptr_data; | |
578 | } | |
579 | ||
be2ef816 AN |
580 | static bool is_callback_calling_function(enum bpf_func_id func_id) |
581 | { | |
582 | return func_id == BPF_FUNC_for_each_map_elem || | |
583 | func_id == BPF_FUNC_timer_set_callback || | |
584 | func_id == BPF_FUNC_find_vma || | |
585 | func_id == BPF_FUNC_loop || | |
586 | func_id == BPF_FUNC_user_ringbuf_drain; | |
587 | } | |
588 | ||
9bb00b28 YS |
589 | static bool is_storage_get_function(enum bpf_func_id func_id) |
590 | { | |
591 | return func_id == BPF_FUNC_sk_storage_get || | |
592 | func_id == BPF_FUNC_inode_storage_get || | |
593 | func_id == BPF_FUNC_task_storage_get || | |
594 | func_id == BPF_FUNC_cgrp_storage_get; | |
595 | } | |
596 | ||
b2d8ef19 DM |
597 | static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id, |
598 | const struct bpf_map *map) | |
599 | { | |
600 | int ref_obj_uses = 0; | |
601 | ||
602 | if (is_ptr_cast_function(func_id)) | |
603 | ref_obj_uses++; | |
604 | if (is_acquire_function(func_id, map)) | |
605 | ref_obj_uses++; | |
88374342 | 606 | if (is_dynptr_ref_function(func_id)) |
b2d8ef19 DM |
607 | ref_obj_uses++; |
608 | ||
609 | return ref_obj_uses > 1; | |
610 | } | |
611 | ||
39491867 BJ |
612 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
613 | { | |
614 | return BPF_CLASS(insn->code) == BPF_STX && | |
615 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
616 | insn->imm == BPF_CMPXCHG; | |
617 | } | |
618 | ||
c25b2ae1 HL |
619 | /* string representation of 'enum bpf_reg_type' |
620 | * | |
621 | * Note that reg_type_str() can not appear more than once in a single verbose() | |
622 | * statement. | |
623 | */ | |
624 | static const char *reg_type_str(struct bpf_verifier_env *env, | |
625 | enum bpf_reg_type type) | |
626 | { | |
ef66c547 | 627 | char postfix[16] = {0}, prefix[64] = {0}; |
c25b2ae1 HL |
628 | static const char * const str[] = { |
629 | [NOT_INIT] = "?", | |
7df5072c | 630 | [SCALAR_VALUE] = "scalar", |
c25b2ae1 HL |
631 | [PTR_TO_CTX] = "ctx", |
632 | [CONST_PTR_TO_MAP] = "map_ptr", | |
633 | [PTR_TO_MAP_VALUE] = "map_value", | |
634 | [PTR_TO_STACK] = "fp", | |
635 | [PTR_TO_PACKET] = "pkt", | |
636 | [PTR_TO_PACKET_META] = "pkt_meta", | |
637 | [PTR_TO_PACKET_END] = "pkt_end", | |
638 | [PTR_TO_FLOW_KEYS] = "flow_keys", | |
639 | [PTR_TO_SOCKET] = "sock", | |
640 | [PTR_TO_SOCK_COMMON] = "sock_common", | |
641 | [PTR_TO_TCP_SOCK] = "tcp_sock", | |
642 | [PTR_TO_TP_BUFFER] = "tp_buffer", | |
643 | [PTR_TO_XDP_SOCK] = "xdp_sock", | |
644 | [PTR_TO_BTF_ID] = "ptr_", | |
c25b2ae1 | 645 | [PTR_TO_MEM] = "mem", |
20b2aff4 | 646 | [PTR_TO_BUF] = "buf", |
c25b2ae1 HL |
647 | [PTR_TO_FUNC] = "func", |
648 | [PTR_TO_MAP_KEY] = "map_key", | |
27060531 | 649 | [CONST_PTR_TO_DYNPTR] = "dynptr_ptr", |
c25b2ae1 HL |
650 | }; |
651 | ||
652 | if (type & PTR_MAYBE_NULL) { | |
5844101a | 653 | if (base_type(type) == PTR_TO_BTF_ID) |
c25b2ae1 HL |
654 | strncpy(postfix, "or_null_", 16); |
655 | else | |
656 | strncpy(postfix, "_or_null", 16); | |
657 | } | |
658 | ||
9bb00b28 | 659 | snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s", |
ef66c547 DV |
660 | type & MEM_RDONLY ? "rdonly_" : "", |
661 | type & MEM_RINGBUF ? "ringbuf_" : "", | |
662 | type & MEM_USER ? "user_" : "", | |
663 | type & MEM_PERCPU ? "percpu_" : "", | |
9bb00b28 | 664 | type & MEM_RCU ? "rcu_" : "", |
3f00c523 DV |
665 | type & PTR_UNTRUSTED ? "untrusted_" : "", |
666 | type & PTR_TRUSTED ? "trusted_" : "" | |
ef66c547 | 667 | ); |
20b2aff4 HL |
668 | |
669 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", | |
670 | prefix, str[base_type(type)], postfix); | |
c25b2ae1 HL |
671 | return env->type_str_buf; |
672 | } | |
17a52670 | 673 | |
8efea21d EC |
674 | static char slot_type_char[] = { |
675 | [STACK_INVALID] = '?', | |
676 | [STACK_SPILL] = 'r', | |
677 | [STACK_MISC] = 'm', | |
678 | [STACK_ZERO] = '0', | |
97e03f52 | 679 | [STACK_DYNPTR] = 'd', |
06accc87 | 680 | [STACK_ITER] = 'i', |
8efea21d EC |
681 | }; |
682 | ||
4e92024a AS |
683 | static void print_liveness(struct bpf_verifier_env *env, |
684 | enum bpf_reg_liveness live) | |
685 | { | |
9242b5f5 | 686 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
687 | verbose(env, "_"); |
688 | if (live & REG_LIVE_READ) | |
689 | verbose(env, "r"); | |
690 | if (live & REG_LIVE_WRITTEN) | |
691 | verbose(env, "w"); | |
9242b5f5 AS |
692 | if (live & REG_LIVE_DONE) |
693 | verbose(env, "D"); | |
4e92024a AS |
694 | } |
695 | ||
79168a66 | 696 | static int __get_spi(s32 off) |
97e03f52 JK |
697 | { |
698 | return (-off - 1) / BPF_REG_SIZE; | |
699 | } | |
700 | ||
f5b625e5 KKD |
701 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
702 | const struct bpf_reg_state *reg) | |
703 | { | |
704 | struct bpf_verifier_state *cur = env->cur_state; | |
705 | ||
706 | return cur->frame[reg->frameno]; | |
707 | } | |
708 | ||
97e03f52 JK |
709 | static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots) |
710 | { | |
f5b625e5 | 711 | int allocated_slots = state->allocated_stack / BPF_REG_SIZE; |
97e03f52 | 712 | |
f5b625e5 KKD |
713 | /* We need to check that slots between [spi - nr_slots + 1, spi] are |
714 | * within [0, allocated_stack). | |
715 | * | |
716 | * Please note that the spi grows downwards. For example, a dynptr | |
717 | * takes the size of two stack slots; the first slot will be at | |
718 | * spi and the second slot will be at spi - 1. | |
719 | */ | |
720 | return spi - nr_slots + 1 >= 0 && spi < allocated_slots; | |
97e03f52 JK |
721 | } |
722 | ||
a461f5ad AN |
723 | static int stack_slot_obj_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
724 | const char *obj_kind, int nr_slots) | |
f4d7e40a | 725 | { |
79168a66 | 726 | int off, spi; |
f4d7e40a | 727 | |
79168a66 | 728 | if (!tnum_is_const(reg->var_off)) { |
a461f5ad | 729 | verbose(env, "%s has to be at a constant offset\n", obj_kind); |
79168a66 KKD |
730 | return -EINVAL; |
731 | } | |
732 | ||
733 | off = reg->off + reg->var_off.value; | |
734 | if (off % BPF_REG_SIZE) { | |
a461f5ad | 735 | verbose(env, "cannot pass in %s at an offset=%d\n", obj_kind, off); |
79168a66 KKD |
736 | return -EINVAL; |
737 | } | |
738 | ||
739 | spi = __get_spi(off); | |
a461f5ad AN |
740 | if (spi + 1 < nr_slots) { |
741 | verbose(env, "cannot pass in %s at an offset=%d\n", obj_kind, off); | |
79168a66 KKD |
742 | return -EINVAL; |
743 | } | |
97e03f52 | 744 | |
a461f5ad | 745 | if (!is_spi_bounds_valid(func(env, reg), spi, nr_slots)) |
f5b625e5 KKD |
746 | return -ERANGE; |
747 | return spi; | |
f4d7e40a AS |
748 | } |
749 | ||
a461f5ad AN |
750 | static int dynptr_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
751 | { | |
752 | return stack_slot_obj_get_spi(env, reg, "dynptr", BPF_DYNPTR_NR_SLOTS); | |
753 | } | |
754 | ||
06accc87 AN |
755 | static int iter_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int nr_slots) |
756 | { | |
757 | return stack_slot_obj_get_spi(env, reg, "iter", nr_slots); | |
758 | } | |
759 | ||
b32a5dae | 760 | static const char *btf_type_name(const struct btf *btf, u32 id) |
9e15db66 | 761 | { |
22dc4a0f | 762 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
763 | } |
764 | ||
d54e0f6c AN |
765 | static const char *dynptr_type_str(enum bpf_dynptr_type type) |
766 | { | |
767 | switch (type) { | |
768 | case BPF_DYNPTR_TYPE_LOCAL: | |
769 | return "local"; | |
770 | case BPF_DYNPTR_TYPE_RINGBUF: | |
771 | return "ringbuf"; | |
772 | case BPF_DYNPTR_TYPE_SKB: | |
773 | return "skb"; | |
774 | case BPF_DYNPTR_TYPE_XDP: | |
775 | return "xdp"; | |
776 | case BPF_DYNPTR_TYPE_INVALID: | |
777 | return "<invalid>"; | |
778 | default: | |
779 | WARN_ONCE(1, "unknown dynptr type %d\n", type); | |
780 | return "<unknown>"; | |
781 | } | |
782 | } | |
783 | ||
06accc87 AN |
784 | static const char *iter_type_str(const struct btf *btf, u32 btf_id) |
785 | { | |
786 | if (!btf || btf_id == 0) | |
787 | return "<invalid>"; | |
788 | ||
789 | /* we already validated that type is valid and has conforming name */ | |
b32a5dae | 790 | return btf_type_name(btf, btf_id) + sizeof(ITER_PREFIX) - 1; |
06accc87 AN |
791 | } |
792 | ||
793 | static const char *iter_state_str(enum bpf_iter_state state) | |
794 | { | |
795 | switch (state) { | |
796 | case BPF_ITER_STATE_ACTIVE: | |
797 | return "active"; | |
798 | case BPF_ITER_STATE_DRAINED: | |
799 | return "drained"; | |
800 | case BPF_ITER_STATE_INVALID: | |
801 | return "<invalid>"; | |
802 | default: | |
803 | WARN_ONCE(1, "unknown iter state %d\n", state); | |
804 | return "<unknown>"; | |
805 | } | |
806 | } | |
807 | ||
0f55f9ed CL |
808 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
809 | { | |
810 | env->scratched_regs |= 1U << regno; | |
811 | } | |
812 | ||
813 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
814 | { | |
343e5375 | 815 | env->scratched_stack_slots |= 1ULL << spi; |
0f55f9ed CL |
816 | } |
817 | ||
818 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
819 | { | |
820 | return (env->scratched_regs >> regno) & 1; | |
821 | } | |
822 | ||
823 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
824 | { | |
825 | return (env->scratched_stack_slots >> regno) & 1; | |
826 | } | |
827 | ||
828 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
829 | { | |
830 | return env->scratched_regs || env->scratched_stack_slots; | |
831 | } | |
832 | ||
833 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
834 | { | |
835 | env->scratched_regs = 0U; | |
343e5375 | 836 | env->scratched_stack_slots = 0ULL; |
0f55f9ed CL |
837 | } |
838 | ||
839 | /* Used for printing the entire verifier state. */ | |
840 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
841 | { | |
842 | env->scratched_regs = ~0U; | |
343e5375 | 843 | env->scratched_stack_slots = ~0ULL; |
0f55f9ed CL |
844 | } |
845 | ||
97e03f52 JK |
846 | static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type) |
847 | { | |
848 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
849 | case DYNPTR_TYPE_LOCAL: | |
850 | return BPF_DYNPTR_TYPE_LOCAL; | |
bc34dee6 JK |
851 | case DYNPTR_TYPE_RINGBUF: |
852 | return BPF_DYNPTR_TYPE_RINGBUF; | |
b5964b96 JK |
853 | case DYNPTR_TYPE_SKB: |
854 | return BPF_DYNPTR_TYPE_SKB; | |
05421aec JK |
855 | case DYNPTR_TYPE_XDP: |
856 | return BPF_DYNPTR_TYPE_XDP; | |
97e03f52 JK |
857 | default: |
858 | return BPF_DYNPTR_TYPE_INVALID; | |
859 | } | |
860 | } | |
861 | ||
66e3a13e JK |
862 | static enum bpf_type_flag get_dynptr_type_flag(enum bpf_dynptr_type type) |
863 | { | |
864 | switch (type) { | |
865 | case BPF_DYNPTR_TYPE_LOCAL: | |
866 | return DYNPTR_TYPE_LOCAL; | |
867 | case BPF_DYNPTR_TYPE_RINGBUF: | |
868 | return DYNPTR_TYPE_RINGBUF; | |
869 | case BPF_DYNPTR_TYPE_SKB: | |
870 | return DYNPTR_TYPE_SKB; | |
871 | case BPF_DYNPTR_TYPE_XDP: | |
872 | return DYNPTR_TYPE_XDP; | |
873 | default: | |
874 | return 0; | |
875 | } | |
876 | } | |
877 | ||
bc34dee6 JK |
878 | static bool dynptr_type_refcounted(enum bpf_dynptr_type type) |
879 | { | |
880 | return type == BPF_DYNPTR_TYPE_RINGBUF; | |
881 | } | |
882 | ||
27060531 KKD |
883 | static void __mark_dynptr_reg(struct bpf_reg_state *reg, |
884 | enum bpf_dynptr_type type, | |
f8064ab9 | 885 | bool first_slot, int dynptr_id); |
27060531 KKD |
886 | |
887 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, | |
888 | struct bpf_reg_state *reg); | |
889 | ||
f8064ab9 KKD |
890 | static void mark_dynptr_stack_regs(struct bpf_verifier_env *env, |
891 | struct bpf_reg_state *sreg1, | |
27060531 KKD |
892 | struct bpf_reg_state *sreg2, |
893 | enum bpf_dynptr_type type) | |
894 | { | |
f8064ab9 KKD |
895 | int id = ++env->id_gen; |
896 | ||
897 | __mark_dynptr_reg(sreg1, type, true, id); | |
898 | __mark_dynptr_reg(sreg2, type, false, id); | |
27060531 KKD |
899 | } |
900 | ||
f8064ab9 KKD |
901 | static void mark_dynptr_cb_reg(struct bpf_verifier_env *env, |
902 | struct bpf_reg_state *reg, | |
27060531 KKD |
903 | enum bpf_dynptr_type type) |
904 | { | |
f8064ab9 | 905 | __mark_dynptr_reg(reg, type, true, ++env->id_gen); |
27060531 KKD |
906 | } |
907 | ||
ef8fc7a0 KKD |
908 | static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env, |
909 | struct bpf_func_state *state, int spi); | |
27060531 | 910 | |
97e03f52 JK |
911 | static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
912 | enum bpf_arg_type arg_type, int insn_idx) | |
913 | { | |
914 | struct bpf_func_state *state = func(env, reg); | |
915 | enum bpf_dynptr_type type; | |
379d4ba8 | 916 | int spi, i, id, err; |
97e03f52 | 917 | |
79168a66 KKD |
918 | spi = dynptr_get_spi(env, reg); |
919 | if (spi < 0) | |
920 | return spi; | |
97e03f52 | 921 | |
379d4ba8 KKD |
922 | /* We cannot assume both spi and spi - 1 belong to the same dynptr, |
923 | * hence we need to call destroy_if_dynptr_stack_slot twice for both, | |
924 | * to ensure that for the following example: | |
925 | * [d1][d1][d2][d2] | |
926 | * spi 3 2 1 0 | |
927 | * So marking spi = 2 should lead to destruction of both d1 and d2. In | |
928 | * case they do belong to same dynptr, second call won't see slot_type | |
929 | * as STACK_DYNPTR and will simply skip destruction. | |
930 | */ | |
931 | err = destroy_if_dynptr_stack_slot(env, state, spi); | |
932 | if (err) | |
933 | return err; | |
934 | err = destroy_if_dynptr_stack_slot(env, state, spi - 1); | |
935 | if (err) | |
936 | return err; | |
97e03f52 JK |
937 | |
938 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
939 | state->stack[spi].slot_type[i] = STACK_DYNPTR; | |
940 | state->stack[spi - 1].slot_type[i] = STACK_DYNPTR; | |
941 | } | |
942 | ||
943 | type = arg_to_dynptr_type(arg_type); | |
944 | if (type == BPF_DYNPTR_TYPE_INVALID) | |
945 | return -EINVAL; | |
946 | ||
f8064ab9 | 947 | mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr, |
27060531 | 948 | &state->stack[spi - 1].spilled_ptr, type); |
97e03f52 | 949 | |
bc34dee6 JK |
950 | if (dynptr_type_refcounted(type)) { |
951 | /* The id is used to track proper releasing */ | |
952 | id = acquire_reference_state(env, insn_idx); | |
953 | if (id < 0) | |
954 | return id; | |
955 | ||
27060531 KKD |
956 | state->stack[spi].spilled_ptr.ref_obj_id = id; |
957 | state->stack[spi - 1].spilled_ptr.ref_obj_id = id; | |
bc34dee6 JK |
958 | } |
959 | ||
d6fefa11 KKD |
960 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
961 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
962 | ||
97e03f52 JK |
963 | return 0; |
964 | } | |
965 | ||
966 | static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
967 | { | |
968 | struct bpf_func_state *state = func(env, reg); | |
969 | int spi, i; | |
970 | ||
79168a66 KKD |
971 | spi = dynptr_get_spi(env, reg); |
972 | if (spi < 0) | |
973 | return spi; | |
97e03f52 JK |
974 | |
975 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
976 | state->stack[spi].slot_type[i] = STACK_INVALID; | |
977 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
978 | } | |
979 | ||
bc34dee6 | 980 | /* Invalidate any slices associated with this dynptr */ |
27060531 KKD |
981 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) |
982 | WARN_ON_ONCE(release_reference(env, state->stack[spi].spilled_ptr.ref_obj_id)); | |
97e03f52 | 983 | |
27060531 KKD |
984 | __mark_reg_not_init(env, &state->stack[spi].spilled_ptr); |
985 | __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr); | |
d6fefa11 KKD |
986 | |
987 | /* Why do we need to set REG_LIVE_WRITTEN for STACK_INVALID slot? | |
988 | * | |
989 | * While we don't allow reading STACK_INVALID, it is still possible to | |
990 | * do <8 byte writes marking some but not all slots as STACK_MISC. Then, | |
991 | * helpers or insns can do partial read of that part without failing, | |
992 | * but check_stack_range_initialized, check_stack_read_var_off, and | |
993 | * check_stack_read_fixed_off will do mark_reg_read for all 8-bytes of | |
994 | * the slot conservatively. Hence we need to prevent those liveness | |
995 | * marking walks. | |
996 | * | |
997 | * This was not a problem before because STACK_INVALID is only set by | |
998 | * default (where the default reg state has its reg->parent as NULL), or | |
999 | * in clean_live_states after REG_LIVE_DONE (at which point | |
1000 | * mark_reg_read won't walk reg->parent chain), but not randomly during | |
1001 | * verifier state exploration (like we did above). Hence, for our case | |
1002 | * parentage chain will still be live (i.e. reg->parent may be | |
1003 | * non-NULL), while earlier reg->parent was NULL, so we need | |
1004 | * REG_LIVE_WRITTEN to screen off read marker propagation when it is | |
1005 | * done later on reads or by mark_dynptr_read as well to unnecessary | |
1006 | * mark registers in verifier state. | |
1007 | */ | |
1008 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1009 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1010 | ||
97e03f52 JK |
1011 | return 0; |
1012 | } | |
1013 | ||
ef8fc7a0 KKD |
1014 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1015 | struct bpf_reg_state *reg); | |
1016 | ||
dbd8d228 KKD |
1017 | static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
1018 | { | |
1019 | if (!env->allow_ptr_leaks) | |
1020 | __mark_reg_not_init(env, reg); | |
1021 | else | |
1022 | __mark_reg_unknown(env, reg); | |
1023 | } | |
1024 | ||
ef8fc7a0 KKD |
1025 | static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env, |
1026 | struct bpf_func_state *state, int spi) | |
97e03f52 | 1027 | { |
f8064ab9 KKD |
1028 | struct bpf_func_state *fstate; |
1029 | struct bpf_reg_state *dreg; | |
1030 | int i, dynptr_id; | |
27060531 | 1031 | |
ef8fc7a0 KKD |
1032 | /* We always ensure that STACK_DYNPTR is never set partially, |
1033 | * hence just checking for slot_type[0] is enough. This is | |
1034 | * different for STACK_SPILL, where it may be only set for | |
1035 | * 1 byte, so code has to use is_spilled_reg. | |
1036 | */ | |
1037 | if (state->stack[spi].slot_type[0] != STACK_DYNPTR) | |
1038 | return 0; | |
97e03f52 | 1039 | |
ef8fc7a0 KKD |
1040 | /* Reposition spi to first slot */ |
1041 | if (!state->stack[spi].spilled_ptr.dynptr.first_slot) | |
1042 | spi = spi + 1; | |
1043 | ||
1044 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) { | |
1045 | verbose(env, "cannot overwrite referenced dynptr\n"); | |
1046 | return -EINVAL; | |
1047 | } | |
1048 | ||
1049 | mark_stack_slot_scratched(env, spi); | |
1050 | mark_stack_slot_scratched(env, spi - 1); | |
97e03f52 | 1051 | |
ef8fc7a0 | 1052 | /* Writing partially to one dynptr stack slot destroys both. */ |
97e03f52 | 1053 | for (i = 0; i < BPF_REG_SIZE; i++) { |
ef8fc7a0 KKD |
1054 | state->stack[spi].slot_type[i] = STACK_INVALID; |
1055 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
97e03f52 JK |
1056 | } |
1057 | ||
f8064ab9 KKD |
1058 | dynptr_id = state->stack[spi].spilled_ptr.id; |
1059 | /* Invalidate any slices associated with this dynptr */ | |
1060 | bpf_for_each_reg_in_vstate(env->cur_state, fstate, dreg, ({ | |
1061 | /* Dynptr slices are only PTR_TO_MEM_OR_NULL and PTR_TO_MEM */ | |
1062 | if (dreg->type != (PTR_TO_MEM | PTR_MAYBE_NULL) && dreg->type != PTR_TO_MEM) | |
1063 | continue; | |
dbd8d228 KKD |
1064 | if (dreg->dynptr_id == dynptr_id) |
1065 | mark_reg_invalid(env, dreg); | |
f8064ab9 | 1066 | })); |
ef8fc7a0 KKD |
1067 | |
1068 | /* Do not release reference state, we are destroying dynptr on stack, | |
1069 | * not using some helper to release it. Just reset register. | |
1070 | */ | |
1071 | __mark_reg_not_init(env, &state->stack[spi].spilled_ptr); | |
1072 | __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr); | |
1073 | ||
1074 | /* Same reason as unmark_stack_slots_dynptr above */ | |
1075 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1076 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1077 | ||
1078 | return 0; | |
1079 | } | |
1080 | ||
7e0dac28 | 1081 | static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
97e03f52 | 1082 | { |
7e0dac28 JK |
1083 | int spi; |
1084 | ||
27060531 KKD |
1085 | if (reg->type == CONST_PTR_TO_DYNPTR) |
1086 | return false; | |
97e03f52 | 1087 | |
7e0dac28 JK |
1088 | spi = dynptr_get_spi(env, reg); |
1089 | ||
1090 | /* -ERANGE (i.e. spi not falling into allocated stack slots) isn't an | |
1091 | * error because this just means the stack state hasn't been updated yet. | |
1092 | * We will do check_mem_access to check and update stack bounds later. | |
f5b625e5 | 1093 | */ |
7e0dac28 JK |
1094 | if (spi < 0 && spi != -ERANGE) |
1095 | return false; | |
1096 | ||
1097 | /* We don't need to check if the stack slots are marked by previous | |
1098 | * dynptr initializations because we allow overwriting existing unreferenced | |
1099 | * STACK_DYNPTR slots, see mark_stack_slots_dynptr which calls | |
1100 | * destroy_if_dynptr_stack_slot to ensure dynptr objects at the slots we are | |
1101 | * touching are completely destructed before we reinitialize them for a new | |
1102 | * one. For referenced ones, destroy_if_dynptr_stack_slot returns an error early | |
1103 | * instead of delaying it until the end where the user will get "Unreleased | |
379d4ba8 KKD |
1104 | * reference" error. |
1105 | */ | |
97e03f52 JK |
1106 | return true; |
1107 | } | |
1108 | ||
7e0dac28 | 1109 | static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
97e03f52 JK |
1110 | { |
1111 | struct bpf_func_state *state = func(env, reg); | |
7e0dac28 | 1112 | int i, spi; |
97e03f52 | 1113 | |
7e0dac28 JK |
1114 | /* This already represents first slot of initialized bpf_dynptr. |
1115 | * | |
1116 | * CONST_PTR_TO_DYNPTR already has fixed and var_off as 0 due to | |
1117 | * check_func_arg_reg_off's logic, so we don't need to check its | |
1118 | * offset and alignment. | |
1119 | */ | |
27060531 KKD |
1120 | if (reg->type == CONST_PTR_TO_DYNPTR) |
1121 | return true; | |
1122 | ||
7e0dac28 | 1123 | spi = dynptr_get_spi(env, reg); |
79168a66 KKD |
1124 | if (spi < 0) |
1125 | return false; | |
f5b625e5 | 1126 | if (!state->stack[spi].spilled_ptr.dynptr.first_slot) |
97e03f52 JK |
1127 | return false; |
1128 | ||
1129 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
1130 | if (state->stack[spi].slot_type[i] != STACK_DYNPTR || | |
1131 | state->stack[spi - 1].slot_type[i] != STACK_DYNPTR) | |
1132 | return false; | |
1133 | } | |
1134 | ||
e9e315b4 RS |
1135 | return true; |
1136 | } | |
1137 | ||
6b75bd3d KKD |
1138 | static bool is_dynptr_type_expected(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
1139 | enum bpf_arg_type arg_type) | |
e9e315b4 RS |
1140 | { |
1141 | struct bpf_func_state *state = func(env, reg); | |
1142 | enum bpf_dynptr_type dynptr_type; | |
27060531 | 1143 | int spi; |
e9e315b4 | 1144 | |
97e03f52 JK |
1145 | /* ARG_PTR_TO_DYNPTR takes any type of dynptr */ |
1146 | if (arg_type == ARG_PTR_TO_DYNPTR) | |
1147 | return true; | |
1148 | ||
e9e315b4 | 1149 | dynptr_type = arg_to_dynptr_type(arg_type); |
27060531 KKD |
1150 | if (reg->type == CONST_PTR_TO_DYNPTR) { |
1151 | return reg->dynptr.type == dynptr_type; | |
1152 | } else { | |
79168a66 KKD |
1153 | spi = dynptr_get_spi(env, reg); |
1154 | if (spi < 0) | |
1155 | return false; | |
27060531 KKD |
1156 | return state->stack[spi].spilled_ptr.dynptr.type == dynptr_type; |
1157 | } | |
97e03f52 JK |
1158 | } |
1159 | ||
06accc87 AN |
1160 | static void __mark_reg_known_zero(struct bpf_reg_state *reg); |
1161 | ||
1162 | static int mark_stack_slots_iter(struct bpf_verifier_env *env, | |
1163 | struct bpf_reg_state *reg, int insn_idx, | |
1164 | struct btf *btf, u32 btf_id, int nr_slots) | |
1165 | { | |
1166 | struct bpf_func_state *state = func(env, reg); | |
1167 | int spi, i, j, id; | |
1168 | ||
1169 | spi = iter_get_spi(env, reg, nr_slots); | |
1170 | if (spi < 0) | |
1171 | return spi; | |
1172 | ||
1173 | id = acquire_reference_state(env, insn_idx); | |
1174 | if (id < 0) | |
1175 | return id; | |
1176 | ||
1177 | for (i = 0; i < nr_slots; i++) { | |
1178 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1179 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1180 | ||
1181 | __mark_reg_known_zero(st); | |
1182 | st->type = PTR_TO_STACK; /* we don't have dedicated reg type */ | |
1183 | st->live |= REG_LIVE_WRITTEN; | |
1184 | st->ref_obj_id = i == 0 ? id : 0; | |
1185 | st->iter.btf = btf; | |
1186 | st->iter.btf_id = btf_id; | |
1187 | st->iter.state = BPF_ITER_STATE_ACTIVE; | |
1188 | st->iter.depth = 0; | |
1189 | ||
1190 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1191 | slot->slot_type[j] = STACK_ITER; | |
1192 | ||
1193 | mark_stack_slot_scratched(env, spi - i); | |
1194 | } | |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | static int unmark_stack_slots_iter(struct bpf_verifier_env *env, | |
1200 | struct bpf_reg_state *reg, int nr_slots) | |
1201 | { | |
1202 | struct bpf_func_state *state = func(env, reg); | |
1203 | int spi, i, j; | |
1204 | ||
1205 | spi = iter_get_spi(env, reg, nr_slots); | |
1206 | if (spi < 0) | |
1207 | return spi; | |
1208 | ||
1209 | for (i = 0; i < nr_slots; i++) { | |
1210 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1211 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1212 | ||
1213 | if (i == 0) | |
1214 | WARN_ON_ONCE(release_reference(env, st->ref_obj_id)); | |
1215 | ||
1216 | __mark_reg_not_init(env, st); | |
1217 | ||
1218 | /* see unmark_stack_slots_dynptr() for why we need to set REG_LIVE_WRITTEN */ | |
1219 | st->live |= REG_LIVE_WRITTEN; | |
1220 | ||
1221 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1222 | slot->slot_type[j] = STACK_INVALID; | |
1223 | ||
1224 | mark_stack_slot_scratched(env, spi - i); | |
1225 | } | |
1226 | ||
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | static bool is_iter_reg_valid_uninit(struct bpf_verifier_env *env, | |
1231 | struct bpf_reg_state *reg, int nr_slots) | |
1232 | { | |
1233 | struct bpf_func_state *state = func(env, reg); | |
1234 | int spi, i, j; | |
1235 | ||
1236 | /* For -ERANGE (i.e. spi not falling into allocated stack slots), we | |
1237 | * will do check_mem_access to check and update stack bounds later, so | |
1238 | * return true for that case. | |
1239 | */ | |
1240 | spi = iter_get_spi(env, reg, nr_slots); | |
1241 | if (spi == -ERANGE) | |
1242 | return true; | |
1243 | if (spi < 0) | |
1244 | return false; | |
1245 | ||
1246 | for (i = 0; i < nr_slots; i++) { | |
1247 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1248 | ||
1249 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1250 | if (slot->slot_type[j] == STACK_ITER) | |
1251 | return false; | |
1252 | } | |
1253 | ||
1254 | return true; | |
1255 | } | |
1256 | ||
1257 | static bool is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg, | |
1258 | struct btf *btf, u32 btf_id, int nr_slots) | |
1259 | { | |
1260 | struct bpf_func_state *state = func(env, reg); | |
1261 | int spi, i, j; | |
1262 | ||
1263 | spi = iter_get_spi(env, reg, nr_slots); | |
1264 | if (spi < 0) | |
1265 | return false; | |
1266 | ||
1267 | for (i = 0; i < nr_slots; i++) { | |
1268 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1269 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1270 | ||
1271 | /* only main (first) slot has ref_obj_id set */ | |
1272 | if (i == 0 && !st->ref_obj_id) | |
1273 | return false; | |
1274 | if (i != 0 && st->ref_obj_id) | |
1275 | return false; | |
1276 | if (st->iter.btf != btf || st->iter.btf_id != btf_id) | |
1277 | return false; | |
1278 | ||
1279 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1280 | if (slot->slot_type[j] != STACK_ITER) | |
1281 | return false; | |
1282 | } | |
1283 | ||
1284 | return true; | |
1285 | } | |
1286 | ||
1287 | /* Check if given stack slot is "special": | |
1288 | * - spilled register state (STACK_SPILL); | |
1289 | * - dynptr state (STACK_DYNPTR); | |
1290 | * - iter state (STACK_ITER). | |
1291 | */ | |
1292 | static bool is_stack_slot_special(const struct bpf_stack_state *stack) | |
1293 | { | |
1294 | enum bpf_stack_slot_type type = stack->slot_type[BPF_REG_SIZE - 1]; | |
1295 | ||
1296 | switch (type) { | |
1297 | case STACK_SPILL: | |
1298 | case STACK_DYNPTR: | |
1299 | case STACK_ITER: | |
1300 | return true; | |
1301 | case STACK_INVALID: | |
1302 | case STACK_MISC: | |
1303 | case STACK_ZERO: | |
1304 | return false; | |
1305 | default: | |
1306 | WARN_ONCE(1, "unknown stack slot type %d\n", type); | |
1307 | return true; | |
1308 | } | |
1309 | } | |
1310 | ||
27113c59 MKL |
1311 | /* The reg state of a pointer or a bounded scalar was saved when |
1312 | * it was spilled to the stack. | |
1313 | */ | |
1314 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
1315 | { | |
1316 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
1317 | } | |
1318 | ||
354e8f19 MKL |
1319 | static void scrub_spilled_slot(u8 *stype) |
1320 | { | |
1321 | if (*stype != STACK_INVALID) | |
1322 | *stype = STACK_MISC; | |
1323 | } | |
1324 | ||
61bd5218 | 1325 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
1326 | const struct bpf_func_state *state, |
1327 | bool print_all) | |
17a52670 | 1328 | { |
f4d7e40a | 1329 | const struct bpf_reg_state *reg; |
17a52670 AS |
1330 | enum bpf_reg_type t; |
1331 | int i; | |
1332 | ||
f4d7e40a AS |
1333 | if (state->frameno) |
1334 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 1335 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
1336 | reg = &state->regs[i]; |
1337 | t = reg->type; | |
17a52670 AS |
1338 | if (t == NOT_INIT) |
1339 | continue; | |
0f55f9ed CL |
1340 | if (!print_all && !reg_scratched(env, i)) |
1341 | continue; | |
4e92024a AS |
1342 | verbose(env, " R%d", i); |
1343 | print_liveness(env, reg->live); | |
7df5072c | 1344 | verbose(env, "="); |
b5dc0163 AS |
1345 | if (t == SCALAR_VALUE && reg->precise) |
1346 | verbose(env, "P"); | |
f1174f77 EC |
1347 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
1348 | tnum_is_const(reg->var_off)) { | |
1349 | /* reg->off should be 0 for SCALAR_VALUE */ | |
7df5072c | 1350 | verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
61bd5218 | 1351 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 1352 | } else { |
7df5072c ML |
1353 | const char *sep = ""; |
1354 | ||
1355 | verbose(env, "%s", reg_type_str(env, t)); | |
5844101a | 1356 | if (base_type(t) == PTR_TO_BTF_ID) |
b32a5dae | 1357 | verbose(env, "%s", btf_type_name(reg->btf, reg->btf_id)); |
7df5072c ML |
1358 | verbose(env, "("); |
1359 | /* | |
1360 | * _a stands for append, was shortened to avoid multiline statements below. | |
1361 | * This macro is used to output a comma separated list of attributes. | |
1362 | */ | |
1363 | #define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) | |
1364 | ||
1365 | if (reg->id) | |
1366 | verbose_a("id=%d", reg->id); | |
a28ace78 | 1367 | if (reg->ref_obj_id) |
7df5072c | 1368 | verbose_a("ref_obj_id=%d", reg->ref_obj_id); |
6a3cd331 DM |
1369 | if (type_is_non_owning_ref(reg->type)) |
1370 | verbose_a("%s", "non_own_ref"); | |
f1174f77 | 1371 | if (t != SCALAR_VALUE) |
7df5072c | 1372 | verbose_a("off=%d", reg->off); |
de8f3a83 | 1373 | if (type_is_pkt_pointer(t)) |
7df5072c | 1374 | verbose_a("r=%d", reg->range); |
c25b2ae1 HL |
1375 | else if (base_type(t) == CONST_PTR_TO_MAP || |
1376 | base_type(t) == PTR_TO_MAP_KEY || | |
1377 | base_type(t) == PTR_TO_MAP_VALUE) | |
7df5072c ML |
1378 | verbose_a("ks=%d,vs=%d", |
1379 | reg->map_ptr->key_size, | |
1380 | reg->map_ptr->value_size); | |
7d1238f2 EC |
1381 | if (tnum_is_const(reg->var_off)) { |
1382 | /* Typically an immediate SCALAR_VALUE, but | |
1383 | * could be a pointer whose offset is too big | |
1384 | * for reg->off | |
1385 | */ | |
7df5072c | 1386 | verbose_a("imm=%llx", reg->var_off.value); |
7d1238f2 EC |
1387 | } else { |
1388 | if (reg->smin_value != reg->umin_value && | |
1389 | reg->smin_value != S64_MIN) | |
7df5072c | 1390 | verbose_a("smin=%lld", (long long)reg->smin_value); |
7d1238f2 EC |
1391 | if (reg->smax_value != reg->umax_value && |
1392 | reg->smax_value != S64_MAX) | |
7df5072c | 1393 | verbose_a("smax=%lld", (long long)reg->smax_value); |
7d1238f2 | 1394 | if (reg->umin_value != 0) |
7df5072c | 1395 | verbose_a("umin=%llu", (unsigned long long)reg->umin_value); |
7d1238f2 | 1396 | if (reg->umax_value != U64_MAX) |
7df5072c | 1397 | verbose_a("umax=%llu", (unsigned long long)reg->umax_value); |
7d1238f2 EC |
1398 | if (!tnum_is_unknown(reg->var_off)) { |
1399 | char tn_buf[48]; | |
f1174f77 | 1400 | |
7d1238f2 | 1401 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
7df5072c | 1402 | verbose_a("var_off=%s", tn_buf); |
7d1238f2 | 1403 | } |
3f50f132 JF |
1404 | if (reg->s32_min_value != reg->smin_value && |
1405 | reg->s32_min_value != S32_MIN) | |
7df5072c | 1406 | verbose_a("s32_min=%d", (int)(reg->s32_min_value)); |
3f50f132 JF |
1407 | if (reg->s32_max_value != reg->smax_value && |
1408 | reg->s32_max_value != S32_MAX) | |
7df5072c | 1409 | verbose_a("s32_max=%d", (int)(reg->s32_max_value)); |
3f50f132 JF |
1410 | if (reg->u32_min_value != reg->umin_value && |
1411 | reg->u32_min_value != U32_MIN) | |
7df5072c | 1412 | verbose_a("u32_min=%d", (int)(reg->u32_min_value)); |
3f50f132 JF |
1413 | if (reg->u32_max_value != reg->umax_value && |
1414 | reg->u32_max_value != U32_MAX) | |
7df5072c | 1415 | verbose_a("u32_max=%d", (int)(reg->u32_max_value)); |
f1174f77 | 1416 | } |
7df5072c ML |
1417 | #undef verbose_a |
1418 | ||
61bd5218 | 1419 | verbose(env, ")"); |
f1174f77 | 1420 | } |
17a52670 | 1421 | } |
638f5b90 | 1422 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
1423 | char types_buf[BPF_REG_SIZE + 1]; |
1424 | bool valid = false; | |
1425 | int j; | |
1426 | ||
1427 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
1428 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
1429 | valid = true; | |
d54e0f6c | 1430 | types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; |
8efea21d EC |
1431 | } |
1432 | types_buf[BPF_REG_SIZE] = 0; | |
1433 | if (!valid) | |
1434 | continue; | |
0f55f9ed CL |
1435 | if (!print_all && !stack_slot_scratched(env, i)) |
1436 | continue; | |
d54e0f6c AN |
1437 | switch (state->stack[i].slot_type[BPF_REG_SIZE - 1]) { |
1438 | case STACK_SPILL: | |
b5dc0163 AS |
1439 | reg = &state->stack[i].spilled_ptr; |
1440 | t = reg->type; | |
d54e0f6c AN |
1441 | |
1442 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1443 | print_liveness(env, reg->live); | |
7df5072c | 1444 | verbose(env, "=%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
b5dc0163 AS |
1445 | if (t == SCALAR_VALUE && reg->precise) |
1446 | verbose(env, "P"); | |
1447 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
1448 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
d54e0f6c AN |
1449 | break; |
1450 | case STACK_DYNPTR: | |
1451 | i += BPF_DYNPTR_NR_SLOTS - 1; | |
1452 | reg = &state->stack[i].spilled_ptr; | |
1453 | ||
1454 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1455 | print_liveness(env, reg->live); | |
1456 | verbose(env, "=dynptr_%s", dynptr_type_str(reg->dynptr.type)); | |
1457 | if (reg->ref_obj_id) | |
1458 | verbose(env, "(ref_id=%d)", reg->ref_obj_id); | |
1459 | break; | |
06accc87 AN |
1460 | case STACK_ITER: |
1461 | /* only main slot has ref_obj_id set; skip others */ | |
1462 | reg = &state->stack[i].spilled_ptr; | |
1463 | if (!reg->ref_obj_id) | |
1464 | continue; | |
1465 | ||
1466 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1467 | print_liveness(env, reg->live); | |
1468 | verbose(env, "=iter_%s(ref_id=%d,state=%s,depth=%u)", | |
1469 | iter_type_str(reg->iter.btf, reg->iter.btf_id), | |
1470 | reg->ref_obj_id, iter_state_str(reg->iter.state), | |
1471 | reg->iter.depth); | |
1472 | break; | |
d54e0f6c AN |
1473 | case STACK_MISC: |
1474 | case STACK_ZERO: | |
1475 | default: | |
1476 | reg = &state->stack[i].spilled_ptr; | |
1477 | ||
1478 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1479 | types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; | |
1480 | types_buf[BPF_REG_SIZE] = 0; | |
1481 | ||
1482 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1483 | print_liveness(env, reg->live); | |
8efea21d | 1484 | verbose(env, "=%s", types_buf); |
d54e0f6c | 1485 | break; |
b5dc0163 | 1486 | } |
17a52670 | 1487 | } |
fd978bf7 JS |
1488 | if (state->acquired_refs && state->refs[0].id) { |
1489 | verbose(env, " refs=%d", state->refs[0].id); | |
1490 | for (i = 1; i < state->acquired_refs; i++) | |
1491 | if (state->refs[i].id) | |
1492 | verbose(env, ",%d", state->refs[i].id); | |
1493 | } | |
bfc6bb74 AS |
1494 | if (state->in_callback_fn) |
1495 | verbose(env, " cb"); | |
1496 | if (state->in_async_callback_fn) | |
1497 | verbose(env, " async_cb"); | |
61bd5218 | 1498 | verbose(env, "\n"); |
0f55f9ed | 1499 | mark_verifier_state_clean(env); |
17a52670 AS |
1500 | } |
1501 | ||
2e576648 CL |
1502 | static inline u32 vlog_alignment(u32 pos) |
1503 | { | |
1504 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
1505 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
1506 | } | |
1507 | ||
1508 | static void print_insn_state(struct bpf_verifier_env *env, | |
1509 | const struct bpf_func_state *state) | |
1510 | { | |
1511 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { | |
1512 | /* remove new line character */ | |
1513 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); | |
1514 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); | |
1515 | } else { | |
1516 | verbose(env, "%d:", env->insn_idx); | |
1517 | } | |
1518 | print_verifier_state(env, state, false); | |
17a52670 AS |
1519 | } |
1520 | ||
c69431aa LB |
1521 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
1522 | * small to hold src. This is different from krealloc since we don't want to preserve | |
1523 | * the contents of dst. | |
1524 | * | |
1525 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
1526 | * not be allocated. | |
638f5b90 | 1527 | */ |
c69431aa | 1528 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 1529 | { |
45435d8d KC |
1530 | size_t alloc_bytes; |
1531 | void *orig = dst; | |
c69431aa LB |
1532 | size_t bytes; |
1533 | ||
1534 | if (ZERO_OR_NULL_PTR(src)) | |
1535 | goto out; | |
1536 | ||
1537 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
1538 | return NULL; | |
1539 | ||
45435d8d KC |
1540 | alloc_bytes = max(ksize(orig), kmalloc_size_roundup(bytes)); |
1541 | dst = krealloc(orig, alloc_bytes, flags); | |
1542 | if (!dst) { | |
1543 | kfree(orig); | |
1544 | return NULL; | |
c69431aa LB |
1545 | } |
1546 | ||
1547 | memcpy(dst, src, bytes); | |
1548 | out: | |
1549 | return dst ? dst : ZERO_SIZE_PTR; | |
1550 | } | |
1551 | ||
1552 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
1553 | * small to hold new_n items. new items are zeroed out if the array grows. | |
1554 | * | |
1555 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
1556 | */ | |
1557 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
1558 | { | |
ceb35b66 | 1559 | size_t alloc_size; |
42378a9c KC |
1560 | void *new_arr; |
1561 | ||
c69431aa LB |
1562 | if (!new_n || old_n == new_n) |
1563 | goto out; | |
1564 | ||
ceb35b66 KC |
1565 | alloc_size = kmalloc_size_roundup(size_mul(new_n, size)); |
1566 | new_arr = krealloc(arr, alloc_size, GFP_KERNEL); | |
42378a9c KC |
1567 | if (!new_arr) { |
1568 | kfree(arr); | |
c69431aa | 1569 | return NULL; |
42378a9c KC |
1570 | } |
1571 | arr = new_arr; | |
c69431aa LB |
1572 | |
1573 | if (new_n > old_n) | |
1574 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
1575 | ||
1576 | out: | |
1577 | return arr ? arr : ZERO_SIZE_PTR; | |
1578 | } | |
1579 | ||
1580 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1581 | { | |
1582 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
1583 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
1584 | if (!dst->refs) | |
1585 | return -ENOMEM; | |
1586 | ||
1587 | dst->acquired_refs = src->acquired_refs; | |
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1592 | { | |
1593 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
1594 | ||
1595 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
1596 | GFP_KERNEL); | |
1597 | if (!dst->stack) | |
1598 | return -ENOMEM; | |
1599 | ||
1600 | dst->allocated_stack = src->allocated_stack; | |
1601 | return 0; | |
1602 | } | |
1603 | ||
1604 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
1605 | { | |
1606 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
1607 | sizeof(struct bpf_reference_state)); | |
1608 | if (!state->refs) | |
1609 | return -ENOMEM; | |
1610 | ||
1611 | state->acquired_refs = n; | |
1612 | return 0; | |
1613 | } | |
1614 | ||
1615 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
1616 | { | |
1617 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
1618 | ||
1619 | if (old_n >= n) | |
1620 | return 0; | |
1621 | ||
1622 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
1623 | if (!state->stack) | |
1624 | return -ENOMEM; | |
1625 | ||
1626 | state->allocated_stack = size; | |
1627 | return 0; | |
fd978bf7 JS |
1628 | } |
1629 | ||
1630 | /* Acquire a pointer id from the env and update the state->refs to include | |
1631 | * this new pointer reference. | |
1632 | * On success, returns a valid pointer id to associate with the register | |
1633 | * On failure, returns a negative errno. | |
638f5b90 | 1634 | */ |
fd978bf7 | 1635 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 1636 | { |
fd978bf7 JS |
1637 | struct bpf_func_state *state = cur_func(env); |
1638 | int new_ofs = state->acquired_refs; | |
1639 | int id, err; | |
1640 | ||
c69431aa | 1641 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
1642 | if (err) |
1643 | return err; | |
1644 | id = ++env->id_gen; | |
1645 | state->refs[new_ofs].id = id; | |
1646 | state->refs[new_ofs].insn_idx = insn_idx; | |
9d9d00ac | 1647 | state->refs[new_ofs].callback_ref = state->in_callback_fn ? state->frameno : 0; |
638f5b90 | 1648 | |
fd978bf7 JS |
1649 | return id; |
1650 | } | |
1651 | ||
1652 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 1653 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
1654 | { |
1655 | int i, last_idx; | |
1656 | ||
fd978bf7 JS |
1657 | last_idx = state->acquired_refs - 1; |
1658 | for (i = 0; i < state->acquired_refs; i++) { | |
1659 | if (state->refs[i].id == ptr_id) { | |
9d9d00ac KKD |
1660 | /* Cannot release caller references in callbacks */ |
1661 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) | |
1662 | return -EINVAL; | |
fd978bf7 JS |
1663 | if (last_idx && i != last_idx) |
1664 | memcpy(&state->refs[i], &state->refs[last_idx], | |
1665 | sizeof(*state->refs)); | |
1666 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
1667 | state->acquired_refs--; | |
638f5b90 | 1668 | return 0; |
638f5b90 | 1669 | } |
638f5b90 | 1670 | } |
46f8bc92 | 1671 | return -EINVAL; |
fd978bf7 JS |
1672 | } |
1673 | ||
f4d7e40a AS |
1674 | static void free_func_state(struct bpf_func_state *state) |
1675 | { | |
5896351e AS |
1676 | if (!state) |
1677 | return; | |
fd978bf7 | 1678 | kfree(state->refs); |
f4d7e40a AS |
1679 | kfree(state->stack); |
1680 | kfree(state); | |
1681 | } | |
1682 | ||
b5dc0163 AS |
1683 | static void clear_jmp_history(struct bpf_verifier_state *state) |
1684 | { | |
1685 | kfree(state->jmp_history); | |
1686 | state->jmp_history = NULL; | |
1687 | state->jmp_history_cnt = 0; | |
1688 | } | |
1689 | ||
1969db47 AS |
1690 | static void free_verifier_state(struct bpf_verifier_state *state, |
1691 | bool free_self) | |
638f5b90 | 1692 | { |
f4d7e40a AS |
1693 | int i; |
1694 | ||
1695 | for (i = 0; i <= state->curframe; i++) { | |
1696 | free_func_state(state->frame[i]); | |
1697 | state->frame[i] = NULL; | |
1698 | } | |
b5dc0163 | 1699 | clear_jmp_history(state); |
1969db47 AS |
1700 | if (free_self) |
1701 | kfree(state); | |
638f5b90 AS |
1702 | } |
1703 | ||
1704 | /* copy verifier state from src to dst growing dst stack space | |
1705 | * when necessary to accommodate larger src stack | |
1706 | */ | |
f4d7e40a AS |
1707 | static int copy_func_state(struct bpf_func_state *dst, |
1708 | const struct bpf_func_state *src) | |
638f5b90 AS |
1709 | { |
1710 | int err; | |
1711 | ||
fd978bf7 JS |
1712 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
1713 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
1714 | if (err) |
1715 | return err; | |
638f5b90 AS |
1716 | return copy_stack_state(dst, src); |
1717 | } | |
1718 | ||
f4d7e40a AS |
1719 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1720 | const struct bpf_verifier_state *src) | |
1721 | { | |
1722 | struct bpf_func_state *dst; | |
1723 | int i, err; | |
1724 | ||
06ab6a50 LB |
1725 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1726 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1727 | GFP_USER); | |
1728 | if (!dst_state->jmp_history) | |
1729 | return -ENOMEM; | |
b5dc0163 AS |
1730 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1731 | ||
f4d7e40a AS |
1732 | /* if dst has more stack frames then src frame, free them */ |
1733 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1734 | free_func_state(dst_state->frame[i]); | |
1735 | dst_state->frame[i] = NULL; | |
1736 | } | |
979d63d5 | 1737 | dst_state->speculative = src->speculative; |
9bb00b28 | 1738 | dst_state->active_rcu_lock = src->active_rcu_lock; |
f4d7e40a | 1739 | dst_state->curframe = src->curframe; |
d0d78c1d KKD |
1740 | dst_state->active_lock.ptr = src->active_lock.ptr; |
1741 | dst_state->active_lock.id = src->active_lock.id; | |
2589726d AS |
1742 | dst_state->branches = src->branches; |
1743 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1744 | dst_state->first_insn_idx = src->first_insn_idx; |
1745 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1746 | for (i = 0; i <= src->curframe; i++) { |
1747 | dst = dst_state->frame[i]; | |
1748 | if (!dst) { | |
1749 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1750 | if (!dst) | |
1751 | return -ENOMEM; | |
1752 | dst_state->frame[i] = dst; | |
1753 | } | |
1754 | err = copy_func_state(dst, src->frame[i]); | |
1755 | if (err) | |
1756 | return err; | |
1757 | } | |
1758 | return 0; | |
1759 | } | |
1760 | ||
2589726d AS |
1761 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1762 | { | |
1763 | while (st) { | |
1764 | u32 br = --st->branches; | |
1765 | ||
1766 | /* WARN_ON(br > 1) technically makes sense here, | |
1767 | * but see comment in push_stack(), hence: | |
1768 | */ | |
1769 | WARN_ONCE((int)br < 0, | |
1770 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1771 | br); | |
1772 | if (br) | |
1773 | break; | |
1774 | st = st->parent; | |
1775 | } | |
1776 | } | |
1777 | ||
638f5b90 | 1778 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1779 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1780 | { |
1781 | struct bpf_verifier_state *cur = env->cur_state; | |
1782 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1783 | int err; | |
17a52670 AS |
1784 | |
1785 | if (env->head == NULL) | |
638f5b90 | 1786 | return -ENOENT; |
17a52670 | 1787 | |
638f5b90 AS |
1788 | if (cur) { |
1789 | err = copy_verifier_state(cur, &head->st); | |
1790 | if (err) | |
1791 | return err; | |
1792 | } | |
6f8a57cc AN |
1793 | if (pop_log) |
1794 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1795 | if (insn_idx) |
1796 | *insn_idx = head->insn_idx; | |
17a52670 | 1797 | if (prev_insn_idx) |
638f5b90 AS |
1798 | *prev_insn_idx = head->prev_insn_idx; |
1799 | elem = head->next; | |
1969db47 | 1800 | free_verifier_state(&head->st, false); |
638f5b90 | 1801 | kfree(head); |
17a52670 AS |
1802 | env->head = elem; |
1803 | env->stack_size--; | |
638f5b90 | 1804 | return 0; |
17a52670 AS |
1805 | } |
1806 | ||
58e2af8b | 1807 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1808 | int insn_idx, int prev_insn_idx, |
1809 | bool speculative) | |
17a52670 | 1810 | { |
638f5b90 | 1811 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1812 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1813 | int err; |
17a52670 | 1814 | |
638f5b90 | 1815 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1816 | if (!elem) |
1817 | goto err; | |
1818 | ||
17a52670 AS |
1819 | elem->insn_idx = insn_idx; |
1820 | elem->prev_insn_idx = prev_insn_idx; | |
1821 | elem->next = env->head; | |
6f8a57cc | 1822 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1823 | env->head = elem; |
1824 | env->stack_size++; | |
1969db47 AS |
1825 | err = copy_verifier_state(&elem->st, cur); |
1826 | if (err) | |
1827 | goto err; | |
979d63d5 | 1828 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1829 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1830 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1831 | env->stack_size); | |
17a52670 AS |
1832 | goto err; |
1833 | } | |
2589726d AS |
1834 | if (elem->st.parent) { |
1835 | ++elem->st.parent->branches; | |
1836 | /* WARN_ON(branches > 2) technically makes sense here, | |
1837 | * but | |
1838 | * 1. speculative states will bump 'branches' for non-branch | |
1839 | * instructions | |
1840 | * 2. is_state_visited() heuristics may decide not to create | |
1841 | * a new state for a sequence of branches and all such current | |
1842 | * and cloned states will be pointing to a single parent state | |
1843 | * which might have large 'branches' count. | |
1844 | */ | |
1845 | } | |
17a52670 AS |
1846 | return &elem->st; |
1847 | err: | |
5896351e AS |
1848 | free_verifier_state(env->cur_state, true); |
1849 | env->cur_state = NULL; | |
17a52670 | 1850 | /* pop all elements and return */ |
6f8a57cc | 1851 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1852 | return NULL; |
1853 | } | |
1854 | ||
1855 | #define CALLER_SAVED_REGS 6 | |
1856 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1857 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1858 | }; | |
1859 | ||
e688c3db AS |
1860 | /* This helper doesn't clear reg->id */ |
1861 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1862 | { |
b03c9f9f EC |
1863 | reg->var_off = tnum_const(imm); |
1864 | reg->smin_value = (s64)imm; | |
1865 | reg->smax_value = (s64)imm; | |
1866 | reg->umin_value = imm; | |
1867 | reg->umax_value = imm; | |
3f50f132 JF |
1868 | |
1869 | reg->s32_min_value = (s32)imm; | |
1870 | reg->s32_max_value = (s32)imm; | |
1871 | reg->u32_min_value = (u32)imm; | |
1872 | reg->u32_max_value = (u32)imm; | |
1873 | } | |
1874 | ||
e688c3db AS |
1875 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1876 | * known to have the value @imm. | |
1877 | */ | |
1878 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1879 | { | |
a73bf9f2 | 1880 | /* Clear off and union(map_ptr, range) */ |
e688c3db AS |
1881 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
1882 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
a73bf9f2 AN |
1883 | reg->id = 0; |
1884 | reg->ref_obj_id = 0; | |
e688c3db AS |
1885 | ___mark_reg_known(reg, imm); |
1886 | } | |
1887 | ||
3f50f132 JF |
1888 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1889 | { | |
1890 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1891 | reg->s32_min_value = (s32)imm; | |
1892 | reg->s32_max_value = (s32)imm; | |
1893 | reg->u32_min_value = (u32)imm; | |
1894 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1895 | } |
1896 | ||
f1174f77 EC |
1897 | /* Mark the 'variable offset' part of a register as zero. This should be |
1898 | * used only on registers holding a pointer type. | |
1899 | */ | |
1900 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1901 | { |
b03c9f9f | 1902 | __mark_reg_known(reg, 0); |
f1174f77 | 1903 | } |
a9789ef9 | 1904 | |
cc2b14d5 AS |
1905 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1906 | { | |
1907 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1908 | reg->type = SCALAR_VALUE; |
1909 | } | |
1910 | ||
61bd5218 JK |
1911 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1912 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1913 | { |
1914 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1915 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1916 | /* Something bad happened, let's kill all regs */ |
1917 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1918 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1919 | return; |
1920 | } | |
1921 | __mark_reg_known_zero(regs + regno); | |
1922 | } | |
1923 | ||
27060531 | 1924 | static void __mark_dynptr_reg(struct bpf_reg_state *reg, enum bpf_dynptr_type type, |
f8064ab9 | 1925 | bool first_slot, int dynptr_id) |
27060531 KKD |
1926 | { |
1927 | /* reg->type has no meaning for STACK_DYNPTR, but when we set reg for | |
1928 | * callback arguments, it does need to be CONST_PTR_TO_DYNPTR, so simply | |
1929 | * set it unconditionally as it is ignored for STACK_DYNPTR anyway. | |
1930 | */ | |
1931 | __mark_reg_known_zero(reg); | |
1932 | reg->type = CONST_PTR_TO_DYNPTR; | |
f8064ab9 KKD |
1933 | /* Give each dynptr a unique id to uniquely associate slices to it. */ |
1934 | reg->id = dynptr_id; | |
27060531 KKD |
1935 | reg->dynptr.type = type; |
1936 | reg->dynptr.first_slot = first_slot; | |
1937 | } | |
1938 | ||
4ddb7416 DB |
1939 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1940 | { | |
c25b2ae1 | 1941 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
4ddb7416 DB |
1942 | const struct bpf_map *map = reg->map_ptr; |
1943 | ||
1944 | if (map->inner_map_meta) { | |
1945 | reg->type = CONST_PTR_TO_MAP; | |
1946 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1947 | /* transfer reg's id which is unique for every map_lookup_elem |
1948 | * as UID of the inner map. | |
1949 | */ | |
db559117 | 1950 | if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER)) |
34d11a44 | 1951 | reg->map_uid = reg->id; |
4ddb7416 DB |
1952 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1953 | reg->type = PTR_TO_XDP_SOCK; | |
1954 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1955 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1956 | reg->type = PTR_TO_SOCKET; | |
1957 | } else { | |
1958 | reg->type = PTR_TO_MAP_VALUE; | |
1959 | } | |
c25b2ae1 | 1960 | return; |
4ddb7416 | 1961 | } |
c25b2ae1 HL |
1962 | |
1963 | reg->type &= ~PTR_MAYBE_NULL; | |
4ddb7416 DB |
1964 | } |
1965 | ||
5d92ddc3 DM |
1966 | static void mark_reg_graph_node(struct bpf_reg_state *regs, u32 regno, |
1967 | struct btf_field_graph_root *ds_head) | |
1968 | { | |
1969 | __mark_reg_known_zero(®s[regno]); | |
1970 | regs[regno].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
1971 | regs[regno].btf = ds_head->btf; | |
1972 | regs[regno].btf_id = ds_head->value_btf_id; | |
1973 | regs[regno].off = ds_head->node_offset; | |
1974 | } | |
1975 | ||
de8f3a83 DB |
1976 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1977 | { | |
1978 | return type_is_pkt_pointer(reg->type); | |
1979 | } | |
1980 | ||
1981 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1982 | { | |
1983 | return reg_is_pkt_pointer(reg) || | |
1984 | reg->type == PTR_TO_PACKET_END; | |
1985 | } | |
1986 | ||
66e3a13e JK |
1987 | static bool reg_is_dynptr_slice_pkt(const struct bpf_reg_state *reg) |
1988 | { | |
1989 | return base_type(reg->type) == PTR_TO_MEM && | |
1990 | (reg->type & DYNPTR_TYPE_SKB || reg->type & DYNPTR_TYPE_XDP); | |
1991 | } | |
1992 | ||
de8f3a83 DB |
1993 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
1994 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1995 | enum bpf_reg_type which) | |
1996 | { | |
1997 | /* The register can already have a range from prior markings. | |
1998 | * This is fine as long as it hasn't been advanced from its | |
1999 | * origin. | |
2000 | */ | |
2001 | return reg->type == which && | |
2002 | reg->id == 0 && | |
2003 | reg->off == 0 && | |
2004 | tnum_equals_const(reg->var_off, 0); | |
2005 | } | |
2006 | ||
3f50f132 JF |
2007 | /* Reset the min/max bounds of a register */ |
2008 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
2009 | { | |
2010 | reg->smin_value = S64_MIN; | |
2011 | reg->smax_value = S64_MAX; | |
2012 | reg->umin_value = 0; | |
2013 | reg->umax_value = U64_MAX; | |
2014 | ||
2015 | reg->s32_min_value = S32_MIN; | |
2016 | reg->s32_max_value = S32_MAX; | |
2017 | reg->u32_min_value = 0; | |
2018 | reg->u32_max_value = U32_MAX; | |
2019 | } | |
2020 | ||
2021 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
2022 | { | |
2023 | reg->smin_value = S64_MIN; | |
2024 | reg->smax_value = S64_MAX; | |
2025 | reg->umin_value = 0; | |
2026 | reg->umax_value = U64_MAX; | |
2027 | } | |
2028 | ||
2029 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
2030 | { | |
2031 | reg->s32_min_value = S32_MIN; | |
2032 | reg->s32_max_value = S32_MAX; | |
2033 | reg->u32_min_value = 0; | |
2034 | reg->u32_max_value = U32_MAX; | |
2035 | } | |
2036 | ||
2037 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
2038 | { | |
2039 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
2040 | ||
2041 | /* min signed is max(sign bit) | min(other bits) */ | |
2042 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
2043 | var32_off.value | (var32_off.mask & S32_MIN)); | |
2044 | /* max signed is min(sign bit) | max(other bits) */ | |
2045 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
2046 | var32_off.value | (var32_off.mask & S32_MAX)); | |
2047 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
2048 | reg->u32_max_value = min(reg->u32_max_value, | |
2049 | (u32)(var32_off.value | var32_off.mask)); | |
2050 | } | |
2051 | ||
2052 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
2053 | { |
2054 | /* min signed is max(sign bit) | min(other bits) */ | |
2055 | reg->smin_value = max_t(s64, reg->smin_value, | |
2056 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
2057 | /* max signed is min(sign bit) | max(other bits) */ | |
2058 | reg->smax_value = min_t(s64, reg->smax_value, | |
2059 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
2060 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
2061 | reg->umax_value = min(reg->umax_value, | |
2062 | reg->var_off.value | reg->var_off.mask); | |
2063 | } | |
2064 | ||
3f50f132 JF |
2065 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
2066 | { | |
2067 | __update_reg32_bounds(reg); | |
2068 | __update_reg64_bounds(reg); | |
2069 | } | |
2070 | ||
b03c9f9f | 2071 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
2072 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
2073 | { | |
2074 | /* Learn sign from signed bounds. | |
2075 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
2076 | * are the same, so combine. This works even in the negative case, e.g. | |
2077 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
2078 | */ | |
2079 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
2080 | reg->s32_min_value = reg->u32_min_value = | |
2081 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
2082 | reg->s32_max_value = reg->u32_max_value = | |
2083 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
2084 | return; | |
2085 | } | |
2086 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
2087 | * boundary, so we must be careful. | |
2088 | */ | |
2089 | if ((s32)reg->u32_max_value >= 0) { | |
2090 | /* Positive. We can't learn anything from the smin, but smax | |
2091 | * is positive, hence safe. | |
2092 | */ | |
2093 | reg->s32_min_value = reg->u32_min_value; | |
2094 | reg->s32_max_value = reg->u32_max_value = | |
2095 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
2096 | } else if ((s32)reg->u32_min_value < 0) { | |
2097 | /* Negative. We can't learn anything from the smax, but smin | |
2098 | * is negative, hence safe. | |
2099 | */ | |
2100 | reg->s32_min_value = reg->u32_min_value = | |
2101 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
2102 | reg->s32_max_value = reg->u32_max_value; | |
2103 | } | |
2104 | } | |
2105 | ||
2106 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
2107 | { |
2108 | /* Learn sign from signed bounds. | |
2109 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
2110 | * are the same, so combine. This works even in the negative case, e.g. | |
2111 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
2112 | */ | |
2113 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
2114 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
2115 | reg->umin_value); | |
2116 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
2117 | reg->umax_value); | |
2118 | return; | |
2119 | } | |
2120 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
2121 | * boundary, so we must be careful. | |
2122 | */ | |
2123 | if ((s64)reg->umax_value >= 0) { | |
2124 | /* Positive. We can't learn anything from the smin, but smax | |
2125 | * is positive, hence safe. | |
2126 | */ | |
2127 | reg->smin_value = reg->umin_value; | |
2128 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
2129 | reg->umax_value); | |
2130 | } else if ((s64)reg->umin_value < 0) { | |
2131 | /* Negative. We can't learn anything from the smax, but smin | |
2132 | * is negative, hence safe. | |
2133 | */ | |
2134 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
2135 | reg->umin_value); | |
2136 | reg->smax_value = reg->umax_value; | |
2137 | } | |
2138 | } | |
2139 | ||
3f50f132 JF |
2140 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
2141 | { | |
2142 | __reg32_deduce_bounds(reg); | |
2143 | __reg64_deduce_bounds(reg); | |
2144 | } | |
2145 | ||
b03c9f9f EC |
2146 | /* Attempts to improve var_off based on unsigned min/max information */ |
2147 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
2148 | { | |
3f50f132 JF |
2149 | struct tnum var64_off = tnum_intersect(reg->var_off, |
2150 | tnum_range(reg->umin_value, | |
2151 | reg->umax_value)); | |
7be14c1c DB |
2152 | struct tnum var32_off = tnum_intersect(tnum_subreg(var64_off), |
2153 | tnum_range(reg->u32_min_value, | |
2154 | reg->u32_max_value)); | |
3f50f132 JF |
2155 | |
2156 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
2157 | } |
2158 | ||
3844d153 DB |
2159 | static void reg_bounds_sync(struct bpf_reg_state *reg) |
2160 | { | |
2161 | /* We might have learned new bounds from the var_off. */ | |
2162 | __update_reg_bounds(reg); | |
2163 | /* We might have learned something about the sign bit. */ | |
2164 | __reg_deduce_bounds(reg); | |
2165 | /* We might have learned some bits from the bounds. */ | |
2166 | __reg_bound_offset(reg); | |
2167 | /* Intersecting with the old var_off might have improved our bounds | |
2168 | * slightly, e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
2169 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
2170 | */ | |
2171 | __update_reg_bounds(reg); | |
2172 | } | |
2173 | ||
e572ff80 DB |
2174 | static bool __reg32_bound_s64(s32 a) |
2175 | { | |
2176 | return a >= 0 && a <= S32_MAX; | |
2177 | } | |
2178 | ||
3f50f132 | 2179 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 2180 | { |
3f50f132 JF |
2181 | reg->umin_value = reg->u32_min_value; |
2182 | reg->umax_value = reg->u32_max_value; | |
e572ff80 DB |
2183 | |
2184 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must | |
2185 | * be positive otherwise set to worse case bounds and refine later | |
2186 | * from tnum. | |
3f50f132 | 2187 | */ |
e572ff80 DB |
2188 | if (__reg32_bound_s64(reg->s32_min_value) && |
2189 | __reg32_bound_s64(reg->s32_max_value)) { | |
3a71dc36 | 2190 | reg->smin_value = reg->s32_min_value; |
e572ff80 DB |
2191 | reg->smax_value = reg->s32_max_value; |
2192 | } else { | |
3a71dc36 | 2193 | reg->smin_value = 0; |
e572ff80 DB |
2194 | reg->smax_value = U32_MAX; |
2195 | } | |
3f50f132 JF |
2196 | } |
2197 | ||
2198 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
2199 | { | |
2200 | /* special case when 64-bit register has upper 32-bit register | |
2201 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
2202 | * allowing us to use 32-bit bounds directly, | |
2203 | */ | |
2204 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
2205 | __reg_assign_32_into_64(reg); | |
2206 | } else { | |
2207 | /* Otherwise the best we can do is push lower 32bit known and | |
2208 | * unknown bits into register (var_off set from jmp logic) | |
2209 | * then learn as much as possible from the 64-bit tnum | |
2210 | * known and unknown bits. The previous smin/smax bounds are | |
2211 | * invalid here because of jmp32 compare so mark them unknown | |
2212 | * so they do not impact tnum bounds calculation. | |
2213 | */ | |
2214 | __mark_reg64_unbounded(reg); | |
3f50f132 | 2215 | } |
3844d153 | 2216 | reg_bounds_sync(reg); |
3f50f132 JF |
2217 | } |
2218 | ||
2219 | static bool __reg64_bound_s32(s64 a) | |
2220 | { | |
388e2c0b | 2221 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
2222 | } |
2223 | ||
2224 | static bool __reg64_bound_u32(u64 a) | |
2225 | { | |
b9979db8 | 2226 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
2227 | } |
2228 | ||
2229 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
2230 | { | |
2231 | __mark_reg32_unbounded(reg); | |
b0270958 | 2232 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 2233 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 2234 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 2235 | } |
10bf4e83 | 2236 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 2237 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 2238 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 2239 | } |
3844d153 | 2240 | reg_bounds_sync(reg); |
b03c9f9f EC |
2241 | } |
2242 | ||
f1174f77 | 2243 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
2244 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
2245 | struct bpf_reg_state *reg) | |
f1174f77 | 2246 | { |
a9c676bc | 2247 | /* |
a73bf9f2 | 2248 | * Clear type, off, and union(map_ptr, range) and |
a9c676bc AS |
2249 | * padding between 'type' and union |
2250 | */ | |
2251 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 2252 | reg->type = SCALAR_VALUE; |
a73bf9f2 AN |
2253 | reg->id = 0; |
2254 | reg->ref_obj_id = 0; | |
f1174f77 | 2255 | reg->var_off = tnum_unknown; |
f4d7e40a | 2256 | reg->frameno = 0; |
be2ef816 | 2257 | reg->precise = !env->bpf_capable; |
b03c9f9f | 2258 | __mark_reg_unbounded(reg); |
f1174f77 EC |
2259 | } |
2260 | ||
61bd5218 JK |
2261 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
2262 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
2263 | { |
2264 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 2265 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
2266 | /* Something bad happened, let's kill all regs except FP */ |
2267 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 2268 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
2269 | return; |
2270 | } | |
f54c7898 | 2271 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
2272 | } |
2273 | ||
f54c7898 DB |
2274 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
2275 | struct bpf_reg_state *reg) | |
f1174f77 | 2276 | { |
f54c7898 | 2277 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
2278 | reg->type = NOT_INIT; |
2279 | } | |
2280 | ||
61bd5218 JK |
2281 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
2282 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
2283 | { |
2284 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 2285 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
2286 | /* Something bad happened, let's kill all regs except FP */ |
2287 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 2288 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
2289 | return; |
2290 | } | |
f54c7898 | 2291 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
2292 | } |
2293 | ||
41c48f3a AI |
2294 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
2295 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f | 2296 | enum bpf_reg_type reg_type, |
c6f1bfe8 YS |
2297 | struct btf *btf, u32 btf_id, |
2298 | enum bpf_type_flag flag) | |
41c48f3a AI |
2299 | { |
2300 | if (reg_type == SCALAR_VALUE) { | |
2301 | mark_reg_unknown(env, regs, regno); | |
2302 | return; | |
2303 | } | |
2304 | mark_reg_known_zero(env, regs, regno); | |
c6f1bfe8 | 2305 | regs[regno].type = PTR_TO_BTF_ID | flag; |
22dc4a0f | 2306 | regs[regno].btf = btf; |
41c48f3a AI |
2307 | regs[regno].btf_id = btf_id; |
2308 | } | |
2309 | ||
5327ed3d | 2310 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 2311 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 2312 | struct bpf_func_state *state) |
17a52670 | 2313 | { |
f4d7e40a | 2314 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
2315 | int i; |
2316 | ||
dc503a8a | 2317 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 2318 | mark_reg_not_init(env, regs, i); |
dc503a8a | 2319 | regs[i].live = REG_LIVE_NONE; |
679c782d | 2320 | regs[i].parent = NULL; |
5327ed3d | 2321 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 2322 | } |
17a52670 AS |
2323 | |
2324 | /* frame pointer */ | |
f1174f77 | 2325 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 2326 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 2327 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
2328 | } |
2329 | ||
f4d7e40a AS |
2330 | #define BPF_MAIN_FUNC (-1) |
2331 | static void init_func_state(struct bpf_verifier_env *env, | |
2332 | struct bpf_func_state *state, | |
2333 | int callsite, int frameno, int subprogno) | |
2334 | { | |
2335 | state->callsite = callsite; | |
2336 | state->frameno = frameno; | |
2337 | state->subprogno = subprogno; | |
1bfe26fb | 2338 | state->callback_ret_range = tnum_range(0, 0); |
f4d7e40a | 2339 | init_reg_state(env, state); |
0f55f9ed | 2340 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
2341 | } |
2342 | ||
bfc6bb74 AS |
2343 | /* Similar to push_stack(), but for async callbacks */ |
2344 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
2345 | int insn_idx, int prev_insn_idx, | |
2346 | int subprog) | |
2347 | { | |
2348 | struct bpf_verifier_stack_elem *elem; | |
2349 | struct bpf_func_state *frame; | |
2350 | ||
2351 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
2352 | if (!elem) | |
2353 | goto err; | |
2354 | ||
2355 | elem->insn_idx = insn_idx; | |
2356 | elem->prev_insn_idx = prev_insn_idx; | |
2357 | elem->next = env->head; | |
2358 | elem->log_pos = env->log.len_used; | |
2359 | env->head = elem; | |
2360 | env->stack_size++; | |
2361 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
2362 | verbose(env, | |
2363 | "The sequence of %d jumps is too complex for async cb.\n", | |
2364 | env->stack_size); | |
2365 | goto err; | |
2366 | } | |
2367 | /* Unlike push_stack() do not copy_verifier_state(). | |
2368 | * The caller state doesn't matter. | |
2369 | * This is async callback. It starts in a fresh stack. | |
2370 | * Initialize it similar to do_check_common(). | |
2371 | */ | |
2372 | elem->st.branches = 1; | |
2373 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
2374 | if (!frame) | |
2375 | goto err; | |
2376 | init_func_state(env, frame, | |
2377 | BPF_MAIN_FUNC /* callsite */, | |
2378 | 0 /* frameno within this callchain */, | |
2379 | subprog /* subprog number within this prog */); | |
2380 | elem->st.frame[0] = frame; | |
2381 | return &elem->st; | |
2382 | err: | |
2383 | free_verifier_state(env->cur_state, true); | |
2384 | env->cur_state = NULL; | |
2385 | /* pop all elements and return */ | |
2386 | while (!pop_stack(env, NULL, NULL, false)); | |
2387 | return NULL; | |
2388 | } | |
2389 | ||
2390 | ||
17a52670 AS |
2391 | enum reg_arg_type { |
2392 | SRC_OP, /* register is used as source operand */ | |
2393 | DST_OP, /* register is used as destination operand */ | |
2394 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
2395 | }; | |
2396 | ||
cc8b0b92 AS |
2397 | static int cmp_subprogs(const void *a, const void *b) |
2398 | { | |
9c8105bd JW |
2399 | return ((struct bpf_subprog_info *)a)->start - |
2400 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
2401 | } |
2402 | ||
2403 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
2404 | { | |
9c8105bd | 2405 | struct bpf_subprog_info *p; |
cc8b0b92 | 2406 | |
9c8105bd JW |
2407 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
2408 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
2409 | if (!p) |
2410 | return -ENOENT; | |
9c8105bd | 2411 | return p - env->subprog_info; |
cc8b0b92 AS |
2412 | |
2413 | } | |
2414 | ||
2415 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
2416 | { | |
2417 | int insn_cnt = env->prog->len; | |
2418 | int ret; | |
2419 | ||
2420 | if (off >= insn_cnt || off < 0) { | |
2421 | verbose(env, "call to invalid destination\n"); | |
2422 | return -EINVAL; | |
2423 | } | |
2424 | ret = find_subprog(env, off); | |
2425 | if (ret >= 0) | |
282a0f46 | 2426 | return ret; |
4cb3d99c | 2427 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
2428 | verbose(env, "too many subprograms\n"); |
2429 | return -E2BIG; | |
2430 | } | |
e6ac2450 | 2431 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
2432 | env->subprog_info[env->subprog_cnt++].start = off; |
2433 | sort(env->subprog_info, env->subprog_cnt, | |
2434 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 2435 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
2436 | } |
2437 | ||
2357672c KKD |
2438 | #define MAX_KFUNC_DESCS 256 |
2439 | #define MAX_KFUNC_BTFS 256 | |
2440 | ||
e6ac2450 MKL |
2441 | struct bpf_kfunc_desc { |
2442 | struct btf_func_model func_model; | |
2443 | u32 func_id; | |
2444 | s32 imm; | |
2357672c KKD |
2445 | u16 offset; |
2446 | }; | |
2447 | ||
2448 | struct bpf_kfunc_btf { | |
2449 | struct btf *btf; | |
2450 | struct module *module; | |
2451 | u16 offset; | |
e6ac2450 MKL |
2452 | }; |
2453 | ||
e6ac2450 MKL |
2454 | struct bpf_kfunc_desc_tab { |
2455 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
2456 | u32 nr_descs; | |
2457 | }; | |
2458 | ||
2357672c KKD |
2459 | struct bpf_kfunc_btf_tab { |
2460 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
2461 | u32 nr_descs; | |
2462 | }; | |
2463 | ||
2464 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
2465 | { |
2466 | const struct bpf_kfunc_desc *d0 = a; | |
2467 | const struct bpf_kfunc_desc *d1 = b; | |
2468 | ||
2469 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
2470 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
2471 | } | |
2472 | ||
2473 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
2474 | { | |
2475 | const struct bpf_kfunc_btf *d0 = a; | |
2476 | const struct bpf_kfunc_btf *d1 = b; | |
2477 | ||
2478 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
2479 | } |
2480 | ||
2481 | static const struct bpf_kfunc_desc * | |
2357672c | 2482 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
2483 | { |
2484 | struct bpf_kfunc_desc desc = { | |
2485 | .func_id = func_id, | |
2357672c | 2486 | .offset = offset, |
e6ac2450 MKL |
2487 | }; |
2488 | struct bpf_kfunc_desc_tab *tab; | |
2489 | ||
2490 | tab = prog->aux->kfunc_tab; | |
2491 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
2492 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
2493 | } | |
2494 | ||
2495 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
b202d844 | 2496 | s16 offset) |
2357672c KKD |
2497 | { |
2498 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
2499 | struct bpf_kfunc_btf_tab *tab; | |
2500 | struct bpf_kfunc_btf *b; | |
2501 | struct module *mod; | |
2502 | struct btf *btf; | |
2503 | int btf_fd; | |
2504 | ||
2505 | tab = env->prog->aux->kfunc_btf_tab; | |
2506 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
2507 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
2508 | if (!b) { | |
2509 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
2510 | verbose(env, "too many different module BTFs\n"); | |
2511 | return ERR_PTR(-E2BIG); | |
2512 | } | |
2513 | ||
2514 | if (bpfptr_is_null(env->fd_array)) { | |
2515 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
2516 | return ERR_PTR(-EPROTO); | |
2517 | } | |
2518 | ||
2519 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
2520 | offset * sizeof(btf_fd), | |
2521 | sizeof(btf_fd))) | |
2522 | return ERR_PTR(-EFAULT); | |
2523 | ||
2524 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
2525 | if (IS_ERR(btf)) { |
2526 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 2527 | return btf; |
588cd7ef | 2528 | } |
2357672c KKD |
2529 | |
2530 | if (!btf_is_module(btf)) { | |
2531 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
2532 | btf_put(btf); | |
2533 | return ERR_PTR(-EINVAL); | |
2534 | } | |
2535 | ||
2536 | mod = btf_try_get_module(btf); | |
2537 | if (!mod) { | |
2538 | btf_put(btf); | |
2539 | return ERR_PTR(-ENXIO); | |
2540 | } | |
2541 | ||
2542 | b = &tab->descs[tab->nr_descs++]; | |
2543 | b->btf = btf; | |
2544 | b->module = mod; | |
2545 | b->offset = offset; | |
2546 | ||
2547 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2548 | kfunc_btf_cmp_by_off, NULL); | |
2549 | } | |
2357672c | 2550 | return b->btf; |
e6ac2450 MKL |
2551 | } |
2552 | ||
2357672c KKD |
2553 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
2554 | { | |
2555 | if (!tab) | |
2556 | return; | |
2557 | ||
2558 | while (tab->nr_descs--) { | |
2559 | module_put(tab->descs[tab->nr_descs].module); | |
2560 | btf_put(tab->descs[tab->nr_descs].btf); | |
2561 | } | |
2562 | kfree(tab); | |
2563 | } | |
2564 | ||
43bf0878 | 2565 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset) |
2357672c | 2566 | { |
2357672c KKD |
2567 | if (offset) { |
2568 | if (offset < 0) { | |
2569 | /* In the future, this can be allowed to increase limit | |
2570 | * of fd index into fd_array, interpreted as u16. | |
2571 | */ | |
2572 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
2573 | return ERR_PTR(-EINVAL); | |
2574 | } | |
2575 | ||
b202d844 | 2576 | return __find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2577 | } |
2578 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
2579 | } |
2580 | ||
2357672c | 2581 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
2582 | { |
2583 | const struct btf_type *func, *func_proto; | |
2357672c | 2584 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
2585 | struct bpf_kfunc_desc_tab *tab; |
2586 | struct bpf_prog_aux *prog_aux; | |
2587 | struct bpf_kfunc_desc *desc; | |
2588 | const char *func_name; | |
2357672c | 2589 | struct btf *desc_btf; |
8cbf062a | 2590 | unsigned long call_imm; |
e6ac2450 MKL |
2591 | unsigned long addr; |
2592 | int err; | |
2593 | ||
2594 | prog_aux = env->prog->aux; | |
2595 | tab = prog_aux->kfunc_tab; | |
2357672c | 2596 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
2597 | if (!tab) { |
2598 | if (!btf_vmlinux) { | |
2599 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
2600 | return -ENOTSUPP; | |
2601 | } | |
2602 | ||
2603 | if (!env->prog->jit_requested) { | |
2604 | verbose(env, "JIT is required for calling kernel function\n"); | |
2605 | return -ENOTSUPP; | |
2606 | } | |
2607 | ||
2608 | if (!bpf_jit_supports_kfunc_call()) { | |
2609 | verbose(env, "JIT does not support calling kernel function\n"); | |
2610 | return -ENOTSUPP; | |
2611 | } | |
2612 | ||
2613 | if (!env->prog->gpl_compatible) { | |
2614 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
2615 | return -EINVAL; | |
2616 | } | |
2617 | ||
2618 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
2619 | if (!tab) | |
2620 | return -ENOMEM; | |
2621 | prog_aux->kfunc_tab = tab; | |
2622 | } | |
2623 | ||
a5d82727 KKD |
2624 | /* func_id == 0 is always invalid, but instead of returning an error, be |
2625 | * conservative and wait until the code elimination pass before returning | |
2626 | * error, so that invalid calls that get pruned out can be in BPF programs | |
2627 | * loaded from userspace. It is also required that offset be untouched | |
2628 | * for such calls. | |
2629 | */ | |
2630 | if (!func_id && !offset) | |
2631 | return 0; | |
2632 | ||
2357672c KKD |
2633 | if (!btf_tab && offset) { |
2634 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
2635 | if (!btf_tab) | |
2636 | return -ENOMEM; | |
2637 | prog_aux->kfunc_btf_tab = btf_tab; | |
2638 | } | |
2639 | ||
43bf0878 | 2640 | desc_btf = find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2641 | if (IS_ERR(desc_btf)) { |
2642 | verbose(env, "failed to find BTF for kernel function\n"); | |
2643 | return PTR_ERR(desc_btf); | |
2644 | } | |
2645 | ||
2646 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
2647 | return 0; |
2648 | ||
2649 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
2650 | verbose(env, "too many different kernel function calls\n"); | |
2651 | return -E2BIG; | |
2652 | } | |
2653 | ||
2357672c | 2654 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
2655 | if (!func || !btf_type_is_func(func)) { |
2656 | verbose(env, "kernel btf_id %u is not a function\n", | |
2657 | func_id); | |
2658 | return -EINVAL; | |
2659 | } | |
2357672c | 2660 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
2661 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
2662 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
2663 | func_id); | |
2664 | return -EINVAL; | |
2665 | } | |
2666 | ||
2357672c | 2667 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
2668 | addr = kallsyms_lookup_name(func_name); |
2669 | if (!addr) { | |
2670 | verbose(env, "cannot find address for kernel function %s\n", | |
2671 | func_name); | |
2672 | return -EINVAL; | |
2673 | } | |
2674 | ||
8cbf062a HT |
2675 | call_imm = BPF_CALL_IMM(addr); |
2676 | /* Check whether or not the relative offset overflows desc->imm */ | |
2677 | if ((unsigned long)(s32)call_imm != call_imm) { | |
2678 | verbose(env, "address of kernel function %s is out of range\n", | |
2679 | func_name); | |
2680 | return -EINVAL; | |
2681 | } | |
2682 | ||
3d76a4d3 SF |
2683 | if (bpf_dev_bound_kfunc_id(func_id)) { |
2684 | err = bpf_dev_bound_kfunc_check(&env->log, prog_aux); | |
2685 | if (err) | |
2686 | return err; | |
2687 | } | |
2688 | ||
e6ac2450 MKL |
2689 | desc = &tab->descs[tab->nr_descs++]; |
2690 | desc->func_id = func_id; | |
8cbf062a | 2691 | desc->imm = call_imm; |
2357672c KKD |
2692 | desc->offset = offset; |
2693 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
2694 | func_proto, func_name, |
2695 | &desc->func_model); | |
2696 | if (!err) | |
2697 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 2698 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
2699 | return err; |
2700 | } | |
2701 | ||
2702 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
2703 | { | |
2704 | const struct bpf_kfunc_desc *d0 = a; | |
2705 | const struct bpf_kfunc_desc *d1 = b; | |
2706 | ||
2707 | if (d0->imm > d1->imm) | |
2708 | return 1; | |
2709 | else if (d0->imm < d1->imm) | |
2710 | return -1; | |
2711 | return 0; | |
2712 | } | |
2713 | ||
2714 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
2715 | { | |
2716 | struct bpf_kfunc_desc_tab *tab; | |
2717 | ||
2718 | tab = prog->aux->kfunc_tab; | |
2719 | if (!tab) | |
2720 | return; | |
2721 | ||
2722 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2723 | kfunc_desc_cmp_by_imm, NULL); | |
2724 | } | |
2725 | ||
2726 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
2727 | { | |
2728 | return !!prog->aux->kfunc_tab; | |
2729 | } | |
2730 | ||
2731 | const struct btf_func_model * | |
2732 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2733 | const struct bpf_insn *insn) | |
2734 | { | |
2735 | const struct bpf_kfunc_desc desc = { | |
2736 | .imm = insn->imm, | |
2737 | }; | |
2738 | const struct bpf_kfunc_desc *res; | |
2739 | struct bpf_kfunc_desc_tab *tab; | |
2740 | ||
2741 | tab = prog->aux->kfunc_tab; | |
2742 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
2743 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
2744 | ||
2745 | return res ? &res->func_model : NULL; | |
2746 | } | |
2747 | ||
2748 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2749 | { |
9c8105bd | 2750 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2751 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2752 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2753 | |
f910cefa JW |
2754 | /* Add entry function. */ |
2755 | ret = add_subprog(env, 0); | |
e6ac2450 | 2756 | if (ret) |
f910cefa JW |
2757 | return ret; |
2758 | ||
e6ac2450 MKL |
2759 | for (i = 0; i < insn_cnt; i++, insn++) { |
2760 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2761 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2762 | continue; |
e6ac2450 | 2763 | |
2c78ee89 | 2764 | if (!env->bpf_capable) { |
e6ac2450 | 2765 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2766 | return -EPERM; |
2767 | } | |
e6ac2450 | 2768 | |
3990ed4c | 2769 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2770 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2771 | else |
2357672c | 2772 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2773 | |
cc8b0b92 AS |
2774 | if (ret < 0) |
2775 | return ret; | |
2776 | } | |
2777 | ||
4cb3d99c JW |
2778 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2779 | * logic. 'subprog_cnt' should not be increased. | |
2780 | */ | |
2781 | subprog[env->subprog_cnt].start = insn_cnt; | |
2782 | ||
06ee7115 | 2783 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2784 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2785 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2786 | |
e6ac2450 MKL |
2787 | return 0; |
2788 | } | |
2789 | ||
2790 | static int check_subprogs(struct bpf_verifier_env *env) | |
2791 | { | |
2792 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2793 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2794 | struct bpf_insn *insn = env->prog->insnsi; | |
2795 | int insn_cnt = env->prog->len; | |
2796 | ||
cc8b0b92 | 2797 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2798 | subprog_start = subprog[cur_subprog].start; |
2799 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2800 | for (i = 0; i < insn_cnt; i++) { |
2801 | u8 code = insn[i].code; | |
2802 | ||
7f6e4312 | 2803 | if (code == (BPF_JMP | BPF_CALL) && |
df2ccc18 IL |
2804 | insn[i].src_reg == 0 && |
2805 | insn[i].imm == BPF_FUNC_tail_call) | |
7f6e4312 | 2806 | subprog[cur_subprog].has_tail_call = true; |
09b28d76 AS |
2807 | if (BPF_CLASS(code) == BPF_LD && |
2808 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2809 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2810 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2811 | goto next; |
2812 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2813 | goto next; | |
2814 | off = i + insn[i].off + 1; | |
2815 | if (off < subprog_start || off >= subprog_end) { | |
2816 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2817 | return -EINVAL; | |
2818 | } | |
2819 | next: | |
2820 | if (i == subprog_end - 1) { | |
2821 | /* to avoid fall-through from one subprog into another | |
2822 | * the last insn of the subprog should be either exit | |
2823 | * or unconditional jump back | |
2824 | */ | |
2825 | if (code != (BPF_JMP | BPF_EXIT) && | |
2826 | code != (BPF_JMP | BPF_JA)) { | |
2827 | verbose(env, "last insn is not an exit or jmp\n"); | |
2828 | return -EINVAL; | |
2829 | } | |
2830 | subprog_start = subprog_end; | |
4cb3d99c JW |
2831 | cur_subprog++; |
2832 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2833 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2834 | } |
2835 | } | |
2836 | return 0; | |
2837 | } | |
2838 | ||
679c782d EC |
2839 | /* Parentage chain of this register (or stack slot) should take care of all |
2840 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2841 | */ | |
f4d7e40a | 2842 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2843 | const struct bpf_reg_state *state, |
5327ed3d | 2844 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2845 | { |
2846 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2847 | int cnt = 0; |
dc503a8a EC |
2848 | |
2849 | while (parent) { | |
2850 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2851 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2852 | break; |
9242b5f5 AS |
2853 | if (parent->live & REG_LIVE_DONE) { |
2854 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
c25b2ae1 | 2855 | reg_type_str(env, parent->type), |
9242b5f5 AS |
2856 | parent->var_off.value, parent->off); |
2857 | return -EFAULT; | |
2858 | } | |
5327ed3d JW |
2859 | /* The first condition is more likely to be true than the |
2860 | * second, checked it first. | |
2861 | */ | |
2862 | if ((parent->live & REG_LIVE_READ) == flag || | |
2863 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2864 | /* The parentage chain never changes and |
2865 | * this parent was already marked as LIVE_READ. | |
2866 | * There is no need to keep walking the chain again and | |
2867 | * keep re-marking all parents as LIVE_READ. | |
2868 | * This case happens when the same register is read | |
2869 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2870 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2871 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2872 | */ |
2873 | break; | |
dc503a8a | 2874 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2875 | parent->live |= flag; |
2876 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2877 | if (flag == REG_LIVE_READ64) | |
2878 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2879 | state = parent; |
2880 | parent = state->parent; | |
f4d7e40a | 2881 | writes = true; |
06ee7115 | 2882 | cnt++; |
dc503a8a | 2883 | } |
06ee7115 AS |
2884 | |
2885 | if (env->longest_mark_read_walk < cnt) | |
2886 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2887 | return 0; |
dc503a8a EC |
2888 | } |
2889 | ||
d6fefa11 KKD |
2890 | static int mark_dynptr_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
2891 | { | |
2892 | struct bpf_func_state *state = func(env, reg); | |
2893 | int spi, ret; | |
2894 | ||
2895 | /* For CONST_PTR_TO_DYNPTR, it must have already been done by | |
2896 | * check_reg_arg in check_helper_call and mark_btf_func_reg_size in | |
2897 | * check_kfunc_call. | |
2898 | */ | |
2899 | if (reg->type == CONST_PTR_TO_DYNPTR) | |
2900 | return 0; | |
79168a66 KKD |
2901 | spi = dynptr_get_spi(env, reg); |
2902 | if (spi < 0) | |
2903 | return spi; | |
d6fefa11 KKD |
2904 | /* Caller ensures dynptr is valid and initialized, which means spi is in |
2905 | * bounds and spi is the first dynptr slot. Simply mark stack slot as | |
2906 | * read. | |
2907 | */ | |
2908 | ret = mark_reg_read(env, &state->stack[spi].spilled_ptr, | |
2909 | state->stack[spi].spilled_ptr.parent, REG_LIVE_READ64); | |
2910 | if (ret) | |
2911 | return ret; | |
2912 | return mark_reg_read(env, &state->stack[spi - 1].spilled_ptr, | |
2913 | state->stack[spi - 1].spilled_ptr.parent, REG_LIVE_READ64); | |
2914 | } | |
2915 | ||
06accc87 AN |
2916 | static int mark_iter_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
2917 | int spi, int nr_slots) | |
2918 | { | |
2919 | struct bpf_func_state *state = func(env, reg); | |
2920 | int err, i; | |
2921 | ||
2922 | for (i = 0; i < nr_slots; i++) { | |
2923 | struct bpf_reg_state *st = &state->stack[spi - i].spilled_ptr; | |
2924 | ||
2925 | err = mark_reg_read(env, st, st->parent, REG_LIVE_READ64); | |
2926 | if (err) | |
2927 | return err; | |
2928 | ||
2929 | mark_stack_slot_scratched(env, spi - i); | |
2930 | } | |
2931 | ||
2932 | return 0; | |
2933 | } | |
2934 | ||
5327ed3d JW |
2935 | /* This function is supposed to be used by the following 32-bit optimization |
2936 | * code only. It returns TRUE if the source or destination register operates | |
2937 | * on 64-bit, otherwise return FALSE. | |
2938 | */ | |
2939 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2940 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2941 | { | |
2942 | u8 code, class, op; | |
2943 | ||
2944 | code = insn->code; | |
2945 | class = BPF_CLASS(code); | |
2946 | op = BPF_OP(code); | |
2947 | if (class == BPF_JMP) { | |
2948 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2949 | * conservatively. | |
2950 | */ | |
2951 | if (op == BPF_EXIT) | |
2952 | return true; | |
2953 | if (op == BPF_CALL) { | |
2954 | /* BPF to BPF call will reach here because of marking | |
2955 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2956 | * don't care the register def because they are anyway | |
2957 | * marked as NOT_INIT already. | |
2958 | */ | |
2959 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2960 | return false; | |
2961 | /* Helper call will reach here because of arg type | |
2962 | * check, conservatively return TRUE. | |
2963 | */ | |
2964 | if (t == SRC_OP) | |
2965 | return true; | |
2966 | ||
2967 | return false; | |
2968 | } | |
2969 | } | |
2970 | ||
2971 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2972 | /* BPF_END always use BPF_ALU class. */ | |
2973 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2974 | return true; | |
2975 | ||
2976 | if (class == BPF_ALU || class == BPF_JMP32) | |
2977 | return false; | |
2978 | ||
2979 | if (class == BPF_LDX) { | |
2980 | if (t != SRC_OP) | |
2981 | return BPF_SIZE(code) == BPF_DW; | |
2982 | /* LDX source must be ptr. */ | |
2983 | return true; | |
2984 | } | |
2985 | ||
2986 | if (class == BPF_STX) { | |
83a28819 IL |
2987 | /* BPF_STX (including atomic variants) has multiple source |
2988 | * operands, one of which is a ptr. Check whether the caller is | |
2989 | * asking about it. | |
2990 | */ | |
2991 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2992 | return true; |
2993 | return BPF_SIZE(code) == BPF_DW; | |
2994 | } | |
2995 | ||
2996 | if (class == BPF_LD) { | |
2997 | u8 mode = BPF_MODE(code); | |
2998 | ||
2999 | /* LD_IMM64 */ | |
3000 | if (mode == BPF_IMM) | |
3001 | return true; | |
3002 | ||
3003 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
3004 | if (t != SRC_OP) | |
3005 | return false; | |
3006 | ||
3007 | /* Implicit ctx ptr. */ | |
3008 | if (regno == BPF_REG_6) | |
3009 | return true; | |
3010 | ||
3011 | /* Explicit source could be any width. */ | |
3012 | return true; | |
3013 | } | |
3014 | ||
3015 | if (class == BPF_ST) | |
3016 | /* The only source register for BPF_ST is a ptr. */ | |
3017 | return true; | |
3018 | ||
3019 | /* Conservatively return true at default. */ | |
3020 | return true; | |
3021 | } | |
3022 | ||
83a28819 IL |
3023 | /* Return the regno defined by the insn, or -1. */ |
3024 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 3025 | { |
83a28819 IL |
3026 | switch (BPF_CLASS(insn->code)) { |
3027 | case BPF_JMP: | |
3028 | case BPF_JMP32: | |
3029 | case BPF_ST: | |
3030 | return -1; | |
3031 | case BPF_STX: | |
3032 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
3033 | (insn->imm & BPF_FETCH)) { | |
3034 | if (insn->imm == BPF_CMPXCHG) | |
3035 | return BPF_REG_0; | |
3036 | else | |
3037 | return insn->src_reg; | |
3038 | } else { | |
3039 | return -1; | |
3040 | } | |
3041 | default: | |
3042 | return insn->dst_reg; | |
3043 | } | |
b325fbca JW |
3044 | } |
3045 | ||
3046 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
3047 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
3048 | { | |
83a28819 IL |
3049 | int dst_reg = insn_def_regno(insn); |
3050 | ||
3051 | if (dst_reg == -1) | |
b325fbca JW |
3052 | return false; |
3053 | ||
83a28819 | 3054 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
3055 | } |
3056 | ||
5327ed3d JW |
3057 | static void mark_insn_zext(struct bpf_verifier_env *env, |
3058 | struct bpf_reg_state *reg) | |
3059 | { | |
3060 | s32 def_idx = reg->subreg_def; | |
3061 | ||
3062 | if (def_idx == DEF_NOT_SUBREG) | |
3063 | return; | |
3064 | ||
3065 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
3066 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
3067 | reg->subreg_def = DEF_NOT_SUBREG; | |
3068 | } | |
3069 | ||
dc503a8a | 3070 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
3071 | enum reg_arg_type t) |
3072 | { | |
f4d7e40a AS |
3073 | struct bpf_verifier_state *vstate = env->cur_state; |
3074 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 3075 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 3076 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 3077 | bool rw64; |
dc503a8a | 3078 | |
17a52670 | 3079 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 3080 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
3081 | return -EINVAL; |
3082 | } | |
3083 | ||
0f55f9ed CL |
3084 | mark_reg_scratched(env, regno); |
3085 | ||
c342dc10 | 3086 | reg = ®s[regno]; |
5327ed3d | 3087 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
3088 | if (t == SRC_OP) { |
3089 | /* check whether register used as source operand can be read */ | |
c342dc10 | 3090 | if (reg->type == NOT_INIT) { |
61bd5218 | 3091 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
3092 | return -EACCES; |
3093 | } | |
679c782d | 3094 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
3095 | if (regno == BPF_REG_FP) |
3096 | return 0; | |
3097 | ||
5327ed3d JW |
3098 | if (rw64) |
3099 | mark_insn_zext(env, reg); | |
3100 | ||
3101 | return mark_reg_read(env, reg, reg->parent, | |
3102 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
3103 | } else { |
3104 | /* check whether register used as dest operand can be written to */ | |
3105 | if (regno == BPF_REG_FP) { | |
61bd5218 | 3106 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
3107 | return -EACCES; |
3108 | } | |
c342dc10 | 3109 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 3110 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 3111 | if (t == DST_OP) |
61bd5218 | 3112 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
3113 | } |
3114 | return 0; | |
3115 | } | |
3116 | ||
bffdeaa8 AN |
3117 | static void mark_jmp_point(struct bpf_verifier_env *env, int idx) |
3118 | { | |
3119 | env->insn_aux_data[idx].jmp_point = true; | |
3120 | } | |
3121 | ||
3122 | static bool is_jmp_point(struct bpf_verifier_env *env, int insn_idx) | |
3123 | { | |
3124 | return env->insn_aux_data[insn_idx].jmp_point; | |
3125 | } | |
3126 | ||
b5dc0163 AS |
3127 | /* for any branch, call, exit record the history of jmps in the given state */ |
3128 | static int push_jmp_history(struct bpf_verifier_env *env, | |
3129 | struct bpf_verifier_state *cur) | |
3130 | { | |
3131 | u32 cnt = cur->jmp_history_cnt; | |
3132 | struct bpf_idx_pair *p; | |
ceb35b66 | 3133 | size_t alloc_size; |
b5dc0163 | 3134 | |
bffdeaa8 AN |
3135 | if (!is_jmp_point(env, env->insn_idx)) |
3136 | return 0; | |
3137 | ||
b5dc0163 | 3138 | cnt++; |
ceb35b66 KC |
3139 | alloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p))); |
3140 | p = krealloc(cur->jmp_history, alloc_size, GFP_USER); | |
b5dc0163 AS |
3141 | if (!p) |
3142 | return -ENOMEM; | |
3143 | p[cnt - 1].idx = env->insn_idx; | |
3144 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
3145 | cur->jmp_history = p; | |
3146 | cur->jmp_history_cnt = cnt; | |
3147 | return 0; | |
3148 | } | |
3149 | ||
3150 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
3151 | * history then previous instruction came from straight line execution. | |
3152 | */ | |
3153 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
3154 | u32 *history) | |
3155 | { | |
3156 | u32 cnt = *history; | |
3157 | ||
3158 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
3159 | i = st->jmp_history[cnt - 1].prev_idx; | |
3160 | (*history)--; | |
3161 | } else { | |
3162 | i--; | |
3163 | } | |
3164 | return i; | |
3165 | } | |
3166 | ||
e6ac2450 MKL |
3167 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
3168 | { | |
3169 | const struct btf_type *func; | |
2357672c | 3170 | struct btf *desc_btf; |
e6ac2450 MKL |
3171 | |
3172 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
3173 | return NULL; | |
3174 | ||
43bf0878 | 3175 | desc_btf = find_kfunc_desc_btf(data, insn->off); |
2357672c KKD |
3176 | if (IS_ERR(desc_btf)) |
3177 | return "<error>"; | |
3178 | ||
3179 | func = btf_type_by_id(desc_btf, insn->imm); | |
3180 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
3181 | } |
3182 | ||
b5dc0163 AS |
3183 | /* For given verifier state backtrack_insn() is called from the last insn to |
3184 | * the first insn. Its purpose is to compute a bitmask of registers and | |
3185 | * stack slots that needs precision in the parent verifier state. | |
3186 | */ | |
3187 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
3188 | u32 *reg_mask, u64 *stack_mask) | |
3189 | { | |
3190 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 3191 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
3192 | .cb_print = verbose, |
3193 | .private_data = env, | |
3194 | }; | |
3195 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
3196 | u8 class = BPF_CLASS(insn->code); | |
3197 | u8 opcode = BPF_OP(insn->code); | |
3198 | u8 mode = BPF_MODE(insn->code); | |
3199 | u32 dreg = 1u << insn->dst_reg; | |
3200 | u32 sreg = 1u << insn->src_reg; | |
3201 | u32 spi; | |
3202 | ||
3203 | if (insn->code == 0) | |
3204 | return 0; | |
496f3324 | 3205 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
3206 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
3207 | verbose(env, "%d: ", idx); | |
3208 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
3209 | } | |
3210 | ||
3211 | if (class == BPF_ALU || class == BPF_ALU64) { | |
3212 | if (!(*reg_mask & dreg)) | |
3213 | return 0; | |
3214 | if (opcode == BPF_MOV) { | |
3215 | if (BPF_SRC(insn->code) == BPF_X) { | |
3216 | /* dreg = sreg | |
3217 | * dreg needs precision after this insn | |
3218 | * sreg needs precision before this insn | |
3219 | */ | |
3220 | *reg_mask &= ~dreg; | |
3221 | *reg_mask |= sreg; | |
3222 | } else { | |
3223 | /* dreg = K | |
3224 | * dreg needs precision after this insn. | |
3225 | * Corresponding register is already marked | |
3226 | * as precise=true in this verifier state. | |
3227 | * No further markings in parent are necessary | |
3228 | */ | |
3229 | *reg_mask &= ~dreg; | |
3230 | } | |
3231 | } else { | |
3232 | if (BPF_SRC(insn->code) == BPF_X) { | |
3233 | /* dreg += sreg | |
3234 | * both dreg and sreg need precision | |
3235 | * before this insn | |
3236 | */ | |
3237 | *reg_mask |= sreg; | |
3238 | } /* else dreg += K | |
3239 | * dreg still needs precision before this insn | |
3240 | */ | |
3241 | } | |
3242 | } else if (class == BPF_LDX) { | |
3243 | if (!(*reg_mask & dreg)) | |
3244 | return 0; | |
3245 | *reg_mask &= ~dreg; | |
3246 | ||
3247 | /* scalars can only be spilled into stack w/o losing precision. | |
3248 | * Load from any other memory can be zero extended. | |
3249 | * The desire to keep that precision is already indicated | |
3250 | * by 'precise' mark in corresponding register of this state. | |
3251 | * No further tracking necessary. | |
3252 | */ | |
3253 | if (insn->src_reg != BPF_REG_FP) | |
3254 | return 0; | |
b5dc0163 AS |
3255 | |
3256 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
3257 | * that [fp - off] slot contains scalar that needs to be | |
3258 | * tracked with precision | |
3259 | */ | |
3260 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
3261 | if (spi >= 64) { | |
3262 | verbose(env, "BUG spi %d\n", spi); | |
3263 | WARN_ONCE(1, "verifier backtracking bug"); | |
3264 | return -EFAULT; | |
3265 | } | |
3266 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 3267 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 3268 | if (*reg_mask & dreg) |
b3b50f05 | 3269 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
3270 | * to access memory. It means backtracking |
3271 | * encountered a case of pointer subtraction. | |
3272 | */ | |
3273 | return -ENOTSUPP; | |
3274 | /* scalars can only be spilled into stack */ | |
3275 | if (insn->dst_reg != BPF_REG_FP) | |
3276 | return 0; | |
b5dc0163 AS |
3277 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
3278 | if (spi >= 64) { | |
3279 | verbose(env, "BUG spi %d\n", spi); | |
3280 | WARN_ONCE(1, "verifier backtracking bug"); | |
3281 | return -EFAULT; | |
3282 | } | |
3283 | if (!(*stack_mask & (1ull << spi))) | |
3284 | return 0; | |
3285 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
3286 | if (class == BPF_STX) |
3287 | *reg_mask |= sreg; | |
b5dc0163 AS |
3288 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
3289 | if (opcode == BPF_CALL) { | |
3290 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
3291 | return -ENOTSUPP; | |
be2ef816 AN |
3292 | /* BPF helpers that invoke callback subprogs are |
3293 | * equivalent to BPF_PSEUDO_CALL above | |
3294 | */ | |
3295 | if (insn->src_reg == 0 && is_callback_calling_function(insn->imm)) | |
3296 | return -ENOTSUPP; | |
d3178e8a HS |
3297 | /* kfunc with imm==0 is invalid and fixup_kfunc_call will |
3298 | * catch this error later. Make backtracking conservative | |
3299 | * with ENOTSUPP. | |
3300 | */ | |
3301 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && insn->imm == 0) | |
3302 | return -ENOTSUPP; | |
b5dc0163 AS |
3303 | /* regular helper call sets R0 */ |
3304 | *reg_mask &= ~1; | |
3305 | if (*reg_mask & 0x3f) { | |
3306 | /* if backtracing was looking for registers R1-R5 | |
3307 | * they should have been found already. | |
3308 | */ | |
3309 | verbose(env, "BUG regs %x\n", *reg_mask); | |
3310 | WARN_ONCE(1, "verifier backtracking bug"); | |
3311 | return -EFAULT; | |
3312 | } | |
3313 | } else if (opcode == BPF_EXIT) { | |
3314 | return -ENOTSUPP; | |
3315 | } | |
3316 | } else if (class == BPF_LD) { | |
3317 | if (!(*reg_mask & dreg)) | |
3318 | return 0; | |
3319 | *reg_mask &= ~dreg; | |
3320 | /* It's ld_imm64 or ld_abs or ld_ind. | |
3321 | * For ld_imm64 no further tracking of precision | |
3322 | * into parent is necessary | |
3323 | */ | |
3324 | if (mode == BPF_IND || mode == BPF_ABS) | |
3325 | /* to be analyzed */ | |
3326 | return -ENOTSUPP; | |
b5dc0163 AS |
3327 | } |
3328 | return 0; | |
3329 | } | |
3330 | ||
3331 | /* the scalar precision tracking algorithm: | |
3332 | * . at the start all registers have precise=false. | |
3333 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
3334 | * . once precise value of the scalar register is used in: | |
3335 | * . ptr + scalar alu | |
3336 | * . if (scalar cond K|scalar) | |
3337 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
3338 | * backtrack through the verifier states and mark all registers and | |
3339 | * stack slots with spilled constants that these scalar regisers | |
3340 | * should be precise. | |
3341 | * . during state pruning two registers (or spilled stack slots) | |
3342 | * are equivalent if both are not precise. | |
3343 | * | |
3344 | * Note the verifier cannot simply walk register parentage chain, | |
3345 | * since many different registers and stack slots could have been | |
3346 | * used to compute single precise scalar. | |
3347 | * | |
3348 | * The approach of starting with precise=true for all registers and then | |
3349 | * backtrack to mark a register as not precise when the verifier detects | |
3350 | * that program doesn't care about specific value (e.g., when helper | |
3351 | * takes register as ARG_ANYTHING parameter) is not safe. | |
3352 | * | |
3353 | * It's ok to walk single parentage chain of the verifier states. | |
3354 | * It's possible that this backtracking will go all the way till 1st insn. | |
3355 | * All other branches will be explored for needing precision later. | |
3356 | * | |
3357 | * The backtracking needs to deal with cases like: | |
3358 | * 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) | |
3359 | * r9 -= r8 | |
3360 | * r5 = r9 | |
3361 | * if r5 > 0x79f goto pc+7 | |
3362 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
3363 | * r5 += 1 | |
3364 | * ... | |
3365 | * call bpf_perf_event_output#25 | |
3366 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
3367 | * | |
3368 | * and this case: | |
3369 | * r6 = 1 | |
3370 | * call foo // uses callee's r6 inside to compute r0 | |
3371 | * r0 += r6 | |
3372 | * if r0 == 0 goto | |
3373 | * | |
3374 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
3375 | * | |
3376 | * Also if parent's curframe > frame where backtracking started, | |
3377 | * the verifier need to mark registers in both frames, otherwise callees | |
3378 | * may incorrectly prune callers. This is similar to | |
3379 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
3380 | * | |
3381 | * For now backtracking falls back into conservative marking. | |
3382 | */ | |
3383 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
3384 | struct bpf_verifier_state *st) | |
3385 | { | |
3386 | struct bpf_func_state *func; | |
3387 | struct bpf_reg_state *reg; | |
3388 | int i, j; | |
3389 | ||
3390 | /* big hammer: mark all scalars precise in this path. | |
3391 | * pop_stack may still get !precise scalars. | |
f63181b6 AN |
3392 | * We also skip current state and go straight to first parent state, |
3393 | * because precision markings in current non-checkpointed state are | |
3394 | * not needed. See why in the comment in __mark_chain_precision below. | |
b5dc0163 | 3395 | */ |
f63181b6 | 3396 | for (st = st->parent; st; st = st->parent) { |
b5dc0163 AS |
3397 | for (i = 0; i <= st->curframe; i++) { |
3398 | func = st->frame[i]; | |
3399 | for (j = 0; j < BPF_REG_FP; j++) { | |
3400 | reg = &func->regs[j]; | |
3401 | if (reg->type != SCALAR_VALUE) | |
3402 | continue; | |
3403 | reg->precise = true; | |
3404 | } | |
3405 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 3406 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
3407 | continue; |
3408 | reg = &func->stack[j].spilled_ptr; | |
3409 | if (reg->type != SCALAR_VALUE) | |
3410 | continue; | |
3411 | reg->precise = true; | |
3412 | } | |
3413 | } | |
f63181b6 | 3414 | } |
b5dc0163 AS |
3415 | } |
3416 | ||
7a830b53 AN |
3417 | static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
3418 | { | |
3419 | struct bpf_func_state *func; | |
3420 | struct bpf_reg_state *reg; | |
3421 | int i, j; | |
3422 | ||
3423 | for (i = 0; i <= st->curframe; i++) { | |
3424 | func = st->frame[i]; | |
3425 | for (j = 0; j < BPF_REG_FP; j++) { | |
3426 | reg = &func->regs[j]; | |
3427 | if (reg->type != SCALAR_VALUE) | |
3428 | continue; | |
3429 | reg->precise = false; | |
3430 | } | |
3431 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
3432 | if (!is_spilled_reg(&func->stack[j])) | |
3433 | continue; | |
3434 | reg = &func->stack[j].spilled_ptr; | |
3435 | if (reg->type != SCALAR_VALUE) | |
3436 | continue; | |
3437 | reg->precise = false; | |
3438 | } | |
3439 | } | |
3440 | } | |
3441 | ||
f63181b6 AN |
3442 | /* |
3443 | * __mark_chain_precision() backtracks BPF program instruction sequence and | |
3444 | * chain of verifier states making sure that register *regno* (if regno >= 0) | |
3445 | * and/or stack slot *spi* (if spi >= 0) are marked as precisely tracked | |
3446 | * SCALARS, as well as any other registers and slots that contribute to | |
3447 | * a tracked state of given registers/stack slots, depending on specific BPF | |
3448 | * assembly instructions (see backtrack_insns() for exact instruction handling | |
3449 | * logic). This backtracking relies on recorded jmp_history and is able to | |
3450 | * traverse entire chain of parent states. This process ends only when all the | |
3451 | * necessary registers/slots and their transitive dependencies are marked as | |
3452 | * precise. | |
3453 | * | |
3454 | * One important and subtle aspect is that precise marks *do not matter* in | |
3455 | * the currently verified state (current state). It is important to understand | |
3456 | * why this is the case. | |
3457 | * | |
3458 | * First, note that current state is the state that is not yet "checkpointed", | |
3459 | * i.e., it is not yet put into env->explored_states, and it has no children | |
3460 | * states as well. It's ephemeral, and can end up either a) being discarded if | |
3461 | * compatible explored state is found at some point or BPF_EXIT instruction is | |
3462 | * reached or b) checkpointed and put into env->explored_states, branching out | |
3463 | * into one or more children states. | |
3464 | * | |
3465 | * In the former case, precise markings in current state are completely | |
3466 | * ignored by state comparison code (see regsafe() for details). Only | |
3467 | * checkpointed ("old") state precise markings are important, and if old | |
3468 | * state's register/slot is precise, regsafe() assumes current state's | |
3469 | * register/slot as precise and checks value ranges exactly and precisely. If | |
3470 | * states turn out to be compatible, current state's necessary precise | |
3471 | * markings and any required parent states' precise markings are enforced | |
3472 | * after the fact with propagate_precision() logic, after the fact. But it's | |
3473 | * important to realize that in this case, even after marking current state | |
3474 | * registers/slots as precise, we immediately discard current state. So what | |
3475 | * actually matters is any of the precise markings propagated into current | |
3476 | * state's parent states, which are always checkpointed (due to b) case above). | |
3477 | * As such, for scenario a) it doesn't matter if current state has precise | |
3478 | * markings set or not. | |
3479 | * | |
3480 | * Now, for the scenario b), checkpointing and forking into child(ren) | |
3481 | * state(s). Note that before current state gets to checkpointing step, any | |
3482 | * processed instruction always assumes precise SCALAR register/slot | |
3483 | * knowledge: if precise value or range is useful to prune jump branch, BPF | |
3484 | * verifier takes this opportunity enthusiastically. Similarly, when | |
3485 | * register's value is used to calculate offset or memory address, exact | |
3486 | * knowledge of SCALAR range is assumed, checked, and enforced. So, similar to | |
3487 | * what we mentioned above about state comparison ignoring precise markings | |
3488 | * during state comparison, BPF verifier ignores and also assumes precise | |
3489 | * markings *at will* during instruction verification process. But as verifier | |
3490 | * assumes precision, it also propagates any precision dependencies across | |
3491 | * parent states, which are not yet finalized, so can be further restricted | |
3492 | * based on new knowledge gained from restrictions enforced by their children | |
3493 | * states. This is so that once those parent states are finalized, i.e., when | |
3494 | * they have no more active children state, state comparison logic in | |
3495 | * is_state_visited() would enforce strict and precise SCALAR ranges, if | |
3496 | * required for correctness. | |
3497 | * | |
3498 | * To build a bit more intuition, note also that once a state is checkpointed, | |
3499 | * the path we took to get to that state is not important. This is crucial | |
3500 | * property for state pruning. When state is checkpointed and finalized at | |
3501 | * some instruction index, it can be correctly and safely used to "short | |
3502 | * circuit" any *compatible* state that reaches exactly the same instruction | |
3503 | * index. I.e., if we jumped to that instruction from a completely different | |
3504 | * code path than original finalized state was derived from, it doesn't | |
3505 | * matter, current state can be discarded because from that instruction | |
3506 | * forward having a compatible state will ensure we will safely reach the | |
3507 | * exit. States describe preconditions for further exploration, but completely | |
3508 | * forget the history of how we got here. | |
3509 | * | |
3510 | * This also means that even if we needed precise SCALAR range to get to | |
3511 | * finalized state, but from that point forward *that same* SCALAR register is | |
3512 | * never used in a precise context (i.e., it's precise value is not needed for | |
3513 | * correctness), it's correct and safe to mark such register as "imprecise" | |
3514 | * (i.e., precise marking set to false). This is what we rely on when we do | |
3515 | * not set precise marking in current state. If no child state requires | |
3516 | * precision for any given SCALAR register, it's safe to dictate that it can | |
3517 | * be imprecise. If any child state does require this register to be precise, | |
3518 | * we'll mark it precise later retroactively during precise markings | |
3519 | * propagation from child state to parent states. | |
7a830b53 AN |
3520 | * |
3521 | * Skipping precise marking setting in current state is a mild version of | |
3522 | * relying on the above observation. But we can utilize this property even | |
3523 | * more aggressively by proactively forgetting any precise marking in the | |
3524 | * current state (which we inherited from the parent state), right before we | |
3525 | * checkpoint it and branch off into new child state. This is done by | |
3526 | * mark_all_scalars_imprecise() to hopefully get more permissive and generic | |
3527 | * finalized states which help in short circuiting more future states. | |
f63181b6 | 3528 | */ |
529409ea | 3529 | static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int regno, |
a3ce685d | 3530 | int spi) |
b5dc0163 AS |
3531 | { |
3532 | struct bpf_verifier_state *st = env->cur_state; | |
3533 | int first_idx = st->first_insn_idx; | |
3534 | int last_idx = env->insn_idx; | |
3535 | struct bpf_func_state *func; | |
3536 | struct bpf_reg_state *reg; | |
a3ce685d AS |
3537 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
3538 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 3539 | bool skip_first = true; |
a3ce685d | 3540 | bool new_marks = false; |
b5dc0163 AS |
3541 | int i, err; |
3542 | ||
2c78ee89 | 3543 | if (!env->bpf_capable) |
b5dc0163 AS |
3544 | return 0; |
3545 | ||
f63181b6 AN |
3546 | /* Do sanity checks against current state of register and/or stack |
3547 | * slot, but don't set precise flag in current state, as precision | |
3548 | * tracking in the current state is unnecessary. | |
3549 | */ | |
529409ea | 3550 | func = st->frame[frame]; |
a3ce685d AS |
3551 | if (regno >= 0) { |
3552 | reg = &func->regs[regno]; | |
3553 | if (reg->type != SCALAR_VALUE) { | |
3554 | WARN_ONCE(1, "backtracing misuse"); | |
3555 | return -EFAULT; | |
3556 | } | |
f63181b6 | 3557 | new_marks = true; |
b5dc0163 | 3558 | } |
b5dc0163 | 3559 | |
a3ce685d | 3560 | while (spi >= 0) { |
27113c59 | 3561 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
3562 | stack_mask = 0; |
3563 | break; | |
3564 | } | |
3565 | reg = &func->stack[spi].spilled_ptr; | |
3566 | if (reg->type != SCALAR_VALUE) { | |
3567 | stack_mask = 0; | |
3568 | break; | |
3569 | } | |
f63181b6 | 3570 | new_marks = true; |
a3ce685d AS |
3571 | break; |
3572 | } | |
3573 | ||
3574 | if (!new_marks) | |
3575 | return 0; | |
3576 | if (!reg_mask && !stack_mask) | |
3577 | return 0; | |
be2ef816 | 3578 | |
b5dc0163 AS |
3579 | for (;;) { |
3580 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
3581 | u32 history = st->jmp_history_cnt; |
3582 | ||
496f3324 | 3583 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 | 3584 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
be2ef816 AN |
3585 | |
3586 | if (last_idx < 0) { | |
3587 | /* we are at the entry into subprog, which | |
3588 | * is expected for global funcs, but only if | |
3589 | * requested precise registers are R1-R5 | |
3590 | * (which are global func's input arguments) | |
3591 | */ | |
3592 | if (st->curframe == 0 && | |
3593 | st->frame[0]->subprogno > 0 && | |
3594 | st->frame[0]->callsite == BPF_MAIN_FUNC && | |
3595 | stack_mask == 0 && (reg_mask & ~0x3e) == 0) { | |
3596 | bitmap_from_u64(mask, reg_mask); | |
3597 | for_each_set_bit(i, mask, 32) { | |
3598 | reg = &st->frame[0]->regs[i]; | |
3599 | if (reg->type != SCALAR_VALUE) { | |
3600 | reg_mask &= ~(1u << i); | |
3601 | continue; | |
3602 | } | |
3603 | reg->precise = true; | |
3604 | } | |
3605 | return 0; | |
3606 | } | |
3607 | ||
3608 | verbose(env, "BUG backtracing func entry subprog %d reg_mask %x stack_mask %llx\n", | |
3609 | st->frame[0]->subprogno, reg_mask, stack_mask); | |
3610 | WARN_ONCE(1, "verifier backtracking bug"); | |
3611 | return -EFAULT; | |
3612 | } | |
3613 | ||
b5dc0163 AS |
3614 | for (i = last_idx;;) { |
3615 | if (skip_first) { | |
3616 | err = 0; | |
3617 | skip_first = false; | |
3618 | } else { | |
3619 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
3620 | } | |
3621 | if (err == -ENOTSUPP) { | |
3622 | mark_all_scalars_precise(env, st); | |
3623 | return 0; | |
3624 | } else if (err) { | |
3625 | return err; | |
3626 | } | |
3627 | if (!reg_mask && !stack_mask) | |
3628 | /* Found assignment(s) into tracked register in this state. | |
3629 | * Since this state is already marked, just return. | |
3630 | * Nothing to be tracked further in the parent state. | |
3631 | */ | |
3632 | return 0; | |
3633 | if (i == first_idx) | |
3634 | break; | |
3635 | i = get_prev_insn_idx(st, i, &history); | |
3636 | if (i >= env->prog->len) { | |
3637 | /* This can happen if backtracking reached insn 0 | |
3638 | * and there are still reg_mask or stack_mask | |
3639 | * to backtrack. | |
3640 | * It means the backtracking missed the spot where | |
3641 | * particular register was initialized with a constant. | |
3642 | */ | |
3643 | verbose(env, "BUG backtracking idx %d\n", i); | |
3644 | WARN_ONCE(1, "verifier backtracking bug"); | |
3645 | return -EFAULT; | |
3646 | } | |
3647 | } | |
3648 | st = st->parent; | |
3649 | if (!st) | |
3650 | break; | |
3651 | ||
a3ce685d | 3652 | new_marks = false; |
529409ea | 3653 | func = st->frame[frame]; |
b5dc0163 AS |
3654 | bitmap_from_u64(mask, reg_mask); |
3655 | for_each_set_bit(i, mask, 32) { | |
3656 | reg = &func->regs[i]; | |
a3ce685d AS |
3657 | if (reg->type != SCALAR_VALUE) { |
3658 | reg_mask &= ~(1u << i); | |
b5dc0163 | 3659 | continue; |
a3ce685d | 3660 | } |
b5dc0163 AS |
3661 | if (!reg->precise) |
3662 | new_marks = true; | |
3663 | reg->precise = true; | |
3664 | } | |
3665 | ||
3666 | bitmap_from_u64(mask, stack_mask); | |
3667 | for_each_set_bit(i, mask, 64) { | |
3668 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
3669 | /* the sequence of instructions: |
3670 | * 2: (bf) r3 = r10 | |
3671 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
3672 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
3673 | * doesn't contain jmps. It's backtracked | |
3674 | * as a single block. | |
3675 | * During backtracking insn 3 is not recognized as | |
3676 | * stack access, so at the end of backtracking | |
3677 | * stack slot fp-8 is still marked in stack_mask. | |
3678 | * However the parent state may not have accessed | |
3679 | * fp-8 and it's "unallocated" stack space. | |
3680 | * In such case fallback to conservative. | |
b5dc0163 | 3681 | */ |
2339cd6c AS |
3682 | mark_all_scalars_precise(env, st); |
3683 | return 0; | |
b5dc0163 AS |
3684 | } |
3685 | ||
27113c59 | 3686 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 3687 | stack_mask &= ~(1ull << i); |
b5dc0163 | 3688 | continue; |
a3ce685d | 3689 | } |
b5dc0163 | 3690 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
3691 | if (reg->type != SCALAR_VALUE) { |
3692 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 3693 | continue; |
a3ce685d | 3694 | } |
b5dc0163 AS |
3695 | if (!reg->precise) |
3696 | new_marks = true; | |
3697 | reg->precise = true; | |
3698 | } | |
496f3324 | 3699 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 3700 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
3701 | new_marks ? "didn't have" : "already had", |
3702 | reg_mask, stack_mask); | |
2e576648 | 3703 | print_verifier_state(env, func, true); |
b5dc0163 AS |
3704 | } |
3705 | ||
a3ce685d AS |
3706 | if (!reg_mask && !stack_mask) |
3707 | break; | |
b5dc0163 AS |
3708 | if (!new_marks) |
3709 | break; | |
3710 | ||
3711 | last_idx = st->last_insn_idx; | |
3712 | first_idx = st->first_insn_idx; | |
3713 | } | |
3714 | return 0; | |
3715 | } | |
3716 | ||
eb1f7f71 | 3717 | int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
a3ce685d | 3718 | { |
529409ea | 3719 | return __mark_chain_precision(env, env->cur_state->curframe, regno, -1); |
a3ce685d AS |
3720 | } |
3721 | ||
529409ea | 3722 | static int mark_chain_precision_frame(struct bpf_verifier_env *env, int frame, int regno) |
a3ce685d | 3723 | { |
529409ea | 3724 | return __mark_chain_precision(env, frame, regno, -1); |
a3ce685d AS |
3725 | } |
3726 | ||
529409ea | 3727 | static int mark_chain_precision_stack_frame(struct bpf_verifier_env *env, int frame, int spi) |
a3ce685d | 3728 | { |
529409ea | 3729 | return __mark_chain_precision(env, frame, -1, spi); |
a3ce685d | 3730 | } |
b5dc0163 | 3731 | |
1be7f75d AS |
3732 | static bool is_spillable_regtype(enum bpf_reg_type type) |
3733 | { | |
c25b2ae1 | 3734 | switch (base_type(type)) { |
1be7f75d | 3735 | case PTR_TO_MAP_VALUE: |
1be7f75d AS |
3736 | case PTR_TO_STACK: |
3737 | case PTR_TO_CTX: | |
969bf05e | 3738 | case PTR_TO_PACKET: |
de8f3a83 | 3739 | case PTR_TO_PACKET_META: |
969bf05e | 3740 | case PTR_TO_PACKET_END: |
d58e468b | 3741 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 3742 | case CONST_PTR_TO_MAP: |
c64b7983 | 3743 | case PTR_TO_SOCKET: |
46f8bc92 | 3744 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 3745 | case PTR_TO_TCP_SOCK: |
fada7fdc | 3746 | case PTR_TO_XDP_SOCK: |
65726b5b | 3747 | case PTR_TO_BTF_ID: |
20b2aff4 | 3748 | case PTR_TO_BUF: |
744ea4e3 | 3749 | case PTR_TO_MEM: |
69c087ba YS |
3750 | case PTR_TO_FUNC: |
3751 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
3752 | return true; |
3753 | default: | |
3754 | return false; | |
3755 | } | |
3756 | } | |
3757 | ||
cc2b14d5 AS |
3758 | /* Does this register contain a constant zero? */ |
3759 | static bool register_is_null(struct bpf_reg_state *reg) | |
3760 | { | |
3761 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
3762 | } | |
3763 | ||
f7cf25b2 AS |
3764 | static bool register_is_const(struct bpf_reg_state *reg) |
3765 | { | |
3766 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
3767 | } | |
3768 | ||
5689d49b YS |
3769 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
3770 | { | |
3771 | return tnum_is_unknown(reg->var_off) && | |
3772 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
3773 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
3774 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
3775 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
3776 | } | |
3777 | ||
3778 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
3779 | { | |
3780 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
3781 | } | |
3782 | ||
6e7e63cb JH |
3783 | static bool __is_pointer_value(bool allow_ptr_leaks, |
3784 | const struct bpf_reg_state *reg) | |
3785 | { | |
3786 | if (allow_ptr_leaks) | |
3787 | return false; | |
3788 | ||
3789 | return reg->type != SCALAR_VALUE; | |
3790 | } | |
3791 | ||
71f656a5 EZ |
3792 | /* Copy src state preserving dst->parent and dst->live fields */ |
3793 | static void copy_register_state(struct bpf_reg_state *dst, const struct bpf_reg_state *src) | |
3794 | { | |
3795 | struct bpf_reg_state *parent = dst->parent; | |
3796 | enum bpf_reg_liveness live = dst->live; | |
3797 | ||
3798 | *dst = *src; | |
3799 | dst->parent = parent; | |
3800 | dst->live = live; | |
3801 | } | |
3802 | ||
f7cf25b2 | 3803 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
3804 | int spi, struct bpf_reg_state *reg, |
3805 | int size) | |
f7cf25b2 AS |
3806 | { |
3807 | int i; | |
3808 | ||
71f656a5 | 3809 | copy_register_state(&state->stack[spi].spilled_ptr, reg); |
354e8f19 MKL |
3810 | if (size == BPF_REG_SIZE) |
3811 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 3812 | |
354e8f19 MKL |
3813 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
3814 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 3815 | |
354e8f19 MKL |
3816 | /* size < 8 bytes spill */ |
3817 | for (; i; i--) | |
3818 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
3819 | } |
3820 | ||
ecdf985d EZ |
3821 | static bool is_bpf_st_mem(struct bpf_insn *insn) |
3822 | { | |
3823 | return BPF_CLASS(insn->code) == BPF_ST && BPF_MODE(insn->code) == BPF_MEM; | |
3824 | } | |
3825 | ||
01f810ac | 3826 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
3827 | * stack boundary and alignment are checked in check_mem_access() |
3828 | */ | |
01f810ac AM |
3829 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
3830 | /* stack frame we're writing to */ | |
3831 | struct bpf_func_state *state, | |
3832 | int off, int size, int value_regno, | |
3833 | int insn_idx) | |
17a52670 | 3834 | { |
f4d7e40a | 3835 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 3836 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
ecdf985d | 3837 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; |
f7cf25b2 | 3838 | struct bpf_reg_state *reg = NULL; |
ecdf985d | 3839 | u32 dst_reg = insn->dst_reg; |
638f5b90 | 3840 | |
c69431aa | 3841 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
3842 | if (err) |
3843 | return err; | |
9c399760 AS |
3844 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
3845 | * so it's aligned access and [off, off + size) are within stack limits | |
3846 | */ | |
638f5b90 AS |
3847 | if (!env->allow_ptr_leaks && |
3848 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
3849 | size != BPF_REG_SIZE) { | |
3850 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
3851 | return -EACCES; | |
3852 | } | |
17a52670 | 3853 | |
f4d7e40a | 3854 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
3855 | if (value_regno >= 0) |
3856 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
3857 | if (!env->bypass_spec_v4) { |
3858 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
3859 | ||
3860 | for (i = 0; i < size; i++) { | |
e4f4db47 LG |
3861 | u8 type = state->stack[spi].slot_type[i]; |
3862 | ||
3863 | if (type != STACK_MISC && type != STACK_ZERO) { | |
2039f26f DB |
3864 | sanitize = true; |
3865 | break; | |
3866 | } | |
3867 | } | |
3868 | ||
3869 | if (sanitize) | |
3870 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
3871 | } | |
17a52670 | 3872 | |
ef8fc7a0 KKD |
3873 | err = destroy_if_dynptr_stack_slot(env, state, spi); |
3874 | if (err) | |
3875 | return err; | |
3876 | ||
0f55f9ed | 3877 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 3878 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 3879 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
3880 | if (dst_reg != BPF_REG_FP) { |
3881 | /* The backtracking logic can only recognize explicit | |
3882 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 3883 | * scalar via different register has to be conservative. |
b5dc0163 AS |
3884 | * Backtrack from here and mark all registers as precise |
3885 | * that contributed into 'reg' being a constant. | |
3886 | */ | |
3887 | err = mark_chain_precision(env, value_regno); | |
3888 | if (err) | |
3889 | return err; | |
3890 | } | |
354e8f19 | 3891 | save_register_state(state, spi, reg, size); |
ecdf985d EZ |
3892 | } else if (!reg && !(off % BPF_REG_SIZE) && is_bpf_st_mem(insn) && |
3893 | insn->imm != 0 && env->bpf_capable) { | |
3894 | struct bpf_reg_state fake_reg = {}; | |
3895 | ||
3896 | __mark_reg_known(&fake_reg, (u32)insn->imm); | |
3897 | fake_reg.type = SCALAR_VALUE; | |
3898 | save_register_state(state, spi, &fake_reg, size); | |
f7cf25b2 | 3899 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 3900 | /* register containing pointer is being spilled into stack */ |
9c399760 | 3901 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 3902 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 3903 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
3904 | return -EACCES; |
3905 | } | |
f7cf25b2 | 3906 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
3907 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
3908 | return -EINVAL; | |
3909 | } | |
354e8f19 | 3910 | save_register_state(state, spi, reg, size); |
9c399760 | 3911 | } else { |
cc2b14d5 AS |
3912 | u8 type = STACK_MISC; |
3913 | ||
679c782d EC |
3914 | /* regular write of data into stack destroys any spilled ptr */ |
3915 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
06accc87 AN |
3916 | /* Mark slots as STACK_MISC if they belonged to spilled ptr/dynptr/iter. */ |
3917 | if (is_stack_slot_special(&state->stack[spi])) | |
0bae2d4d | 3918 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 3919 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 3920 | |
cc2b14d5 AS |
3921 | /* only mark the slot as written if all 8 bytes were written |
3922 | * otherwise read propagation may incorrectly stop too soon | |
3923 | * when stack slots are partially written. | |
3924 | * This heuristic means that read propagation will be | |
3925 | * conservative, since it will add reg_live_read marks | |
3926 | * to stack slots all the way to first state when programs | |
3927 | * writes+reads less than 8 bytes | |
3928 | */ | |
3929 | if (size == BPF_REG_SIZE) | |
3930 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
3931 | ||
3932 | /* when we zero initialize stack slots mark them as such */ | |
ecdf985d EZ |
3933 | if ((reg && register_is_null(reg)) || |
3934 | (!reg && is_bpf_st_mem(insn) && insn->imm == 0)) { | |
b5dc0163 AS |
3935 | /* backtracking doesn't work for STACK_ZERO yet. */ |
3936 | err = mark_chain_precision(env, value_regno); | |
3937 | if (err) | |
3938 | return err; | |
cc2b14d5 | 3939 | type = STACK_ZERO; |
b5dc0163 | 3940 | } |
cc2b14d5 | 3941 | |
0bae2d4d | 3942 | /* Mark slots affected by this stack write. */ |
9c399760 | 3943 | for (i = 0; i < size; i++) |
638f5b90 | 3944 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 3945 | type; |
17a52670 AS |
3946 | } |
3947 | return 0; | |
3948 | } | |
3949 | ||
01f810ac AM |
3950 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
3951 | * known to contain a variable offset. | |
3952 | * This function checks whether the write is permitted and conservatively | |
3953 | * tracks the effects of the write, considering that each stack slot in the | |
3954 | * dynamic range is potentially written to. | |
3955 | * | |
3956 | * 'off' includes 'regno->off'. | |
3957 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
3958 | * the stack. | |
3959 | * | |
3960 | * Spilled pointers in range are not marked as written because we don't know | |
3961 | * what's going to be actually written. This means that read propagation for | |
3962 | * future reads cannot be terminated by this write. | |
3963 | * | |
3964 | * For privileged programs, uninitialized stack slots are considered | |
3965 | * initialized by this write (even though we don't know exactly what offsets | |
3966 | * are going to be written to). The idea is that we don't want the verifier to | |
3967 | * reject future reads that access slots written to through variable offsets. | |
3968 | */ | |
3969 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
3970 | /* func where register points to */ | |
3971 | struct bpf_func_state *state, | |
3972 | int ptr_regno, int off, int size, | |
3973 | int value_regno, int insn_idx) | |
3974 | { | |
3975 | struct bpf_func_state *cur; /* state of the current function */ | |
3976 | int min_off, max_off; | |
3977 | int i, err; | |
3978 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
31ff2135 | 3979 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; |
01f810ac AM |
3980 | bool writing_zero = false; |
3981 | /* set if the fact that we're writing a zero is used to let any | |
3982 | * stack slots remain STACK_ZERO | |
3983 | */ | |
3984 | bool zero_used = false; | |
3985 | ||
3986 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
3987 | ptr_reg = &cur->regs[ptr_regno]; | |
3988 | min_off = ptr_reg->smin_value + off; | |
3989 | max_off = ptr_reg->smax_value + off + size; | |
3990 | if (value_regno >= 0) | |
3991 | value_reg = &cur->regs[value_regno]; | |
31ff2135 EZ |
3992 | if ((value_reg && register_is_null(value_reg)) || |
3993 | (!value_reg && is_bpf_st_mem(insn) && insn->imm == 0)) | |
01f810ac AM |
3994 | writing_zero = true; |
3995 | ||
c69431aa | 3996 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
3997 | if (err) |
3998 | return err; | |
3999 | ||
ef8fc7a0 KKD |
4000 | for (i = min_off; i < max_off; i++) { |
4001 | int spi; | |
4002 | ||
4003 | spi = __get_spi(i); | |
4004 | err = destroy_if_dynptr_stack_slot(env, state, spi); | |
4005 | if (err) | |
4006 | return err; | |
4007 | } | |
01f810ac AM |
4008 | |
4009 | /* Variable offset writes destroy any spilled pointers in range. */ | |
4010 | for (i = min_off; i < max_off; i++) { | |
4011 | u8 new_type, *stype; | |
4012 | int slot, spi; | |
4013 | ||
4014 | slot = -i - 1; | |
4015 | spi = slot / BPF_REG_SIZE; | |
4016 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 4017 | mark_stack_slot_scratched(env, spi); |
01f810ac | 4018 | |
f5e477a8 KKD |
4019 | if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) { |
4020 | /* Reject the write if range we may write to has not | |
4021 | * been initialized beforehand. If we didn't reject | |
4022 | * here, the ptr status would be erased below (even | |
4023 | * though not all slots are actually overwritten), | |
4024 | * possibly opening the door to leaks. | |
4025 | * | |
4026 | * We do however catch STACK_INVALID case below, and | |
4027 | * only allow reading possibly uninitialized memory | |
4028 | * later for CAP_PERFMON, as the write may not happen to | |
4029 | * that slot. | |
01f810ac AM |
4030 | */ |
4031 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
4032 | insn_idx, i); | |
4033 | return -EINVAL; | |
4034 | } | |
4035 | ||
4036 | /* Erase all spilled pointers. */ | |
4037 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
4038 | ||
4039 | /* Update the slot type. */ | |
4040 | new_type = STACK_MISC; | |
4041 | if (writing_zero && *stype == STACK_ZERO) { | |
4042 | new_type = STACK_ZERO; | |
4043 | zero_used = true; | |
4044 | } | |
4045 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
4046 | * pretend that it will be initialized by this write. The slot | |
4047 | * might not actually be written to, and so if we mark it as | |
4048 | * initialized future reads might leak uninitialized memory. | |
4049 | * For privileged programs, we will accept such reads to slots | |
4050 | * that may or may not be written because, if we're reject | |
4051 | * them, the error would be too confusing. | |
4052 | */ | |
4053 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
4054 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
4055 | insn_idx, i); | |
4056 | return -EINVAL; | |
4057 | } | |
4058 | *stype = new_type; | |
4059 | } | |
4060 | if (zero_used) { | |
4061 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
4062 | err = mark_chain_precision(env, value_regno); | |
4063 | if (err) | |
4064 | return err; | |
4065 | } | |
4066 | return 0; | |
4067 | } | |
4068 | ||
4069 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
4070 | * max_off), we set the register's type according to the types of the | |
4071 | * respective stack slots. If all the stack values are known to be zeros, then | |
4072 | * so is the destination reg. Otherwise, the register is considered to be | |
4073 | * SCALAR. This function does not deal with register filling; the caller must | |
4074 | * ensure that all spilled registers in the stack range have been marked as | |
4075 | * read. | |
4076 | */ | |
4077 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
4078 | /* func where src register points to */ | |
4079 | struct bpf_func_state *ptr_state, | |
4080 | int min_off, int max_off, int dst_regno) | |
4081 | { | |
4082 | struct bpf_verifier_state *vstate = env->cur_state; | |
4083 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4084 | int i, slot, spi; | |
4085 | u8 *stype; | |
4086 | int zeros = 0; | |
4087 | ||
4088 | for (i = min_off; i < max_off; i++) { | |
4089 | slot = -i - 1; | |
4090 | spi = slot / BPF_REG_SIZE; | |
4091 | stype = ptr_state->stack[spi].slot_type; | |
4092 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
4093 | break; | |
4094 | zeros++; | |
4095 | } | |
4096 | if (zeros == max_off - min_off) { | |
4097 | /* any access_size read into register is zero extended, | |
4098 | * so the whole register == const_zero | |
4099 | */ | |
4100 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
4101 | /* backtracking doesn't support STACK_ZERO yet, | |
4102 | * so mark it precise here, so that later | |
4103 | * backtracking can stop here. | |
4104 | * Backtracking may not need this if this register | |
4105 | * doesn't participate in pointer adjustment. | |
4106 | * Forward propagation of precise flag is not | |
4107 | * necessary either. This mark is only to stop | |
4108 | * backtracking. Any register that contributed | |
4109 | * to const 0 was marked precise before spill. | |
4110 | */ | |
4111 | state->regs[dst_regno].precise = true; | |
4112 | } else { | |
4113 | /* have read misc data from the stack */ | |
4114 | mark_reg_unknown(env, state->regs, dst_regno); | |
4115 | } | |
4116 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
4117 | } | |
4118 | ||
4119 | /* Read the stack at 'off' and put the results into the register indicated by | |
4120 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
4121 | * spilled reg. | |
4122 | * | |
4123 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
4124 | * register. | |
4125 | * | |
4126 | * The access is assumed to be within the current stack bounds. | |
4127 | */ | |
4128 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
4129 | /* func where src register points to */ | |
4130 | struct bpf_func_state *reg_state, | |
4131 | int off, int size, int dst_regno) | |
17a52670 | 4132 | { |
f4d7e40a AS |
4133 | struct bpf_verifier_state *vstate = env->cur_state; |
4134 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 4135 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 4136 | struct bpf_reg_state *reg; |
354e8f19 | 4137 | u8 *stype, type; |
17a52670 | 4138 | |
f4d7e40a | 4139 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 4140 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 4141 | |
27113c59 | 4142 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
4143 | u8 spill_size = 1; |
4144 | ||
4145 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
4146 | spill_size++; | |
354e8f19 | 4147 | |
f30d4968 | 4148 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
4149 | if (reg->type != SCALAR_VALUE) { |
4150 | verbose_linfo(env, env->insn_idx, "; "); | |
4151 | verbose(env, "invalid size of register fill\n"); | |
4152 | return -EACCES; | |
4153 | } | |
354e8f19 MKL |
4154 | |
4155 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
4156 | if (dst_regno < 0) | |
4157 | return 0; | |
4158 | ||
f30d4968 | 4159 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
4160 | /* The earlier check_reg_arg() has decided the |
4161 | * subreg_def for this insn. Save it first. | |
4162 | */ | |
4163 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
4164 | ||
71f656a5 | 4165 | copy_register_state(&state->regs[dst_regno], reg); |
354e8f19 MKL |
4166 | state->regs[dst_regno].subreg_def = subreg_def; |
4167 | } else { | |
4168 | for (i = 0; i < size; i++) { | |
4169 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
4170 | if (type == STACK_SPILL) | |
4171 | continue; | |
4172 | if (type == STACK_MISC) | |
4173 | continue; | |
6715df8d EZ |
4174 | if (type == STACK_INVALID && env->allow_uninit_stack) |
4175 | continue; | |
354e8f19 MKL |
4176 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
4177 | off, i, size); | |
4178 | return -EACCES; | |
4179 | } | |
01f810ac | 4180 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 4181 | } |
354e8f19 | 4182 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 4183 | return 0; |
17a52670 | 4184 | } |
17a52670 | 4185 | |
01f810ac | 4186 | if (dst_regno >= 0) { |
17a52670 | 4187 | /* restore register state from stack */ |
71f656a5 | 4188 | copy_register_state(&state->regs[dst_regno], reg); |
2f18f62e AS |
4189 | /* mark reg as written since spilled pointer state likely |
4190 | * has its liveness marks cleared by is_state_visited() | |
4191 | * which resets stack/reg liveness for state transitions | |
4192 | */ | |
01f810ac | 4193 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 4194 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 4195 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
4196 | * it is acceptable to use this value as a SCALAR_VALUE |
4197 | * (e.g. for XADD). | |
4198 | * We must not allow unprivileged callers to do that | |
4199 | * with spilled pointers. | |
4200 | */ | |
4201 | verbose(env, "leaking pointer from stack off %d\n", | |
4202 | off); | |
4203 | return -EACCES; | |
dc503a8a | 4204 | } |
f7cf25b2 | 4205 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
4206 | } else { |
4207 | for (i = 0; i < size; i++) { | |
01f810ac AM |
4208 | type = stype[(slot - i) % BPF_REG_SIZE]; |
4209 | if (type == STACK_MISC) | |
cc2b14d5 | 4210 | continue; |
01f810ac | 4211 | if (type == STACK_ZERO) |
cc2b14d5 | 4212 | continue; |
6715df8d EZ |
4213 | if (type == STACK_INVALID && env->allow_uninit_stack) |
4214 | continue; | |
cc2b14d5 AS |
4215 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
4216 | off, i, size); | |
4217 | return -EACCES; | |
4218 | } | |
f7cf25b2 | 4219 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
4220 | if (dst_regno >= 0) |
4221 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 4222 | } |
f7cf25b2 | 4223 | return 0; |
17a52670 AS |
4224 | } |
4225 | ||
61df10c7 | 4226 | enum bpf_access_src { |
01f810ac AM |
4227 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
4228 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
4229 | }; | |
4230 | ||
4231 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
4232 | int regno, int off, int access_size, | |
4233 | bool zero_size_allowed, | |
61df10c7 | 4234 | enum bpf_access_src type, |
01f810ac AM |
4235 | struct bpf_call_arg_meta *meta); |
4236 | ||
4237 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
4238 | { | |
4239 | return cur_regs(env) + regno; | |
4240 | } | |
4241 | ||
4242 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
4243 | * 'dst_regno'. | |
4244 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
4245 | * but not its variable offset. | |
4246 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
4247 | * | |
4248 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
4249 | * filling registers (i.e. reads of spilled register cannot be detected when | |
4250 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
4251 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
4252 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
4253 | * instead. | |
4254 | */ | |
4255 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
4256 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 4257 | { |
01f810ac AM |
4258 | /* The state of the source register. */ |
4259 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4260 | struct bpf_func_state *ptr_state = func(env, reg); | |
4261 | int err; | |
4262 | int min_off, max_off; | |
4263 | ||
4264 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 4265 | */ |
01f810ac AM |
4266 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
4267 | false, ACCESS_DIRECT, NULL); | |
4268 | if (err) | |
4269 | return err; | |
4270 | ||
4271 | min_off = reg->smin_value + off; | |
4272 | max_off = reg->smax_value + off; | |
4273 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
4274 | return 0; | |
4275 | } | |
4276 | ||
4277 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
4278 | * check_stack_read_var_off. | |
4279 | * | |
4280 | * The caller must ensure that the offset falls within the allocated stack | |
4281 | * bounds. | |
4282 | * | |
4283 | * 'dst_regno' is a register which will receive the value from the stack. It | |
4284 | * can be -1, meaning that the read value is not going to a register. | |
4285 | */ | |
4286 | static int check_stack_read(struct bpf_verifier_env *env, | |
4287 | int ptr_regno, int off, int size, | |
4288 | int dst_regno) | |
4289 | { | |
4290 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4291 | struct bpf_func_state *state = func(env, reg); | |
4292 | int err; | |
4293 | /* Some accesses are only permitted with a static offset. */ | |
4294 | bool var_off = !tnum_is_const(reg->var_off); | |
4295 | ||
4296 | /* The offset is required to be static when reads don't go to a | |
4297 | * register, in order to not leak pointers (see | |
4298 | * check_stack_read_fixed_off). | |
4299 | */ | |
4300 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
4301 | char tn_buf[48]; |
4302 | ||
4303 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 4304 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
4305 | tn_buf, off, size); |
4306 | return -EACCES; | |
4307 | } | |
01f810ac AM |
4308 | /* Variable offset is prohibited for unprivileged mode for simplicity |
4309 | * since it requires corresponding support in Spectre masking for stack | |
082cdc69 LG |
4310 | * ALU. See also retrieve_ptr_limit(). The check in |
4311 | * check_stack_access_for_ptr_arithmetic() called by | |
4312 | * adjust_ptr_min_max_vals() prevents users from creating stack pointers | |
4313 | * with variable offsets, therefore no check is required here. Further, | |
4314 | * just checking it here would be insufficient as speculative stack | |
4315 | * writes could still lead to unsafe speculative behaviour. | |
01f810ac | 4316 | */ |
01f810ac AM |
4317 | if (!var_off) { |
4318 | off += reg->var_off.value; | |
4319 | err = check_stack_read_fixed_off(env, state, off, size, | |
4320 | dst_regno); | |
4321 | } else { | |
4322 | /* Variable offset stack reads need more conservative handling | |
4323 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
4324 | * branch. | |
4325 | */ | |
4326 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
4327 | dst_regno); | |
4328 | } | |
4329 | return err; | |
4330 | } | |
4331 | ||
4332 | ||
4333 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
4334 | * check_stack_write_var_off. | |
4335 | * | |
4336 | * 'ptr_regno' is the register used as a pointer into the stack. | |
4337 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
4338 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
4339 | * be -1, meaning that we're not writing from a register. | |
4340 | * | |
4341 | * The caller must ensure that the offset falls within the maximum stack size. | |
4342 | */ | |
4343 | static int check_stack_write(struct bpf_verifier_env *env, | |
4344 | int ptr_regno, int off, int size, | |
4345 | int value_regno, int insn_idx) | |
4346 | { | |
4347 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4348 | struct bpf_func_state *state = func(env, reg); | |
4349 | int err; | |
4350 | ||
4351 | if (tnum_is_const(reg->var_off)) { | |
4352 | off += reg->var_off.value; | |
4353 | err = check_stack_write_fixed_off(env, state, off, size, | |
4354 | value_regno, insn_idx); | |
4355 | } else { | |
4356 | /* Variable offset stack reads need more conservative handling | |
4357 | * than fixed offset ones. | |
4358 | */ | |
4359 | err = check_stack_write_var_off(env, state, | |
4360 | ptr_regno, off, size, | |
4361 | value_regno, insn_idx); | |
4362 | } | |
4363 | return err; | |
e4298d25 DB |
4364 | } |
4365 | ||
591fe988 DB |
4366 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
4367 | int off, int size, enum bpf_access_type type) | |
4368 | { | |
4369 | struct bpf_reg_state *regs = cur_regs(env); | |
4370 | struct bpf_map *map = regs[regno].map_ptr; | |
4371 | u32 cap = bpf_map_flags_to_cap(map); | |
4372 | ||
4373 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
4374 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
4375 | map->value_size, off, size); | |
4376 | return -EACCES; | |
4377 | } | |
4378 | ||
4379 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
4380 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
4381 | map->value_size, off, size); | |
4382 | return -EACCES; | |
4383 | } | |
4384 | ||
4385 | return 0; | |
4386 | } | |
4387 | ||
457f4436 AN |
4388 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
4389 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
4390 | int off, int size, u32 mem_size, | |
4391 | bool zero_size_allowed) | |
17a52670 | 4392 | { |
457f4436 AN |
4393 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
4394 | struct bpf_reg_state *reg; | |
4395 | ||
4396 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
4397 | return 0; | |
17a52670 | 4398 | |
457f4436 AN |
4399 | reg = &cur_regs(env)[regno]; |
4400 | switch (reg->type) { | |
69c087ba YS |
4401 | case PTR_TO_MAP_KEY: |
4402 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
4403 | mem_size, off, size); | |
4404 | break; | |
457f4436 | 4405 | case PTR_TO_MAP_VALUE: |
61bd5218 | 4406 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
4407 | mem_size, off, size); |
4408 | break; | |
4409 | case PTR_TO_PACKET: | |
4410 | case PTR_TO_PACKET_META: | |
4411 | case PTR_TO_PACKET_END: | |
4412 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
4413 | off, size, regno, reg->id, off, mem_size); | |
4414 | break; | |
4415 | case PTR_TO_MEM: | |
4416 | default: | |
4417 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
4418 | mem_size, off, size); | |
17a52670 | 4419 | } |
457f4436 AN |
4420 | |
4421 | return -EACCES; | |
17a52670 AS |
4422 | } |
4423 | ||
457f4436 AN |
4424 | /* check read/write into a memory region with possible variable offset */ |
4425 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
4426 | int off, int size, u32 mem_size, | |
4427 | bool zero_size_allowed) | |
dbcfe5f7 | 4428 | { |
f4d7e40a AS |
4429 | struct bpf_verifier_state *vstate = env->cur_state; |
4430 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
4431 | struct bpf_reg_state *reg = &state->regs[regno]; |
4432 | int err; | |
4433 | ||
457f4436 | 4434 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
4435 | * need to try adding each of min_value and max_value to off |
4436 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
4437 | * |
4438 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
4439 | * comparisons where we can't assume the floor of a |
4440 | * value is 0. If we are using signed variables for our | |
4441 | * index'es we need to make sure that whatever we use | |
4442 | * will have a set floor within our range. | |
4443 | */ | |
b7137c4e DB |
4444 | if (reg->smin_value < 0 && |
4445 | (reg->smin_value == S64_MIN || | |
4446 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
4447 | reg->smin_value + off < 0)) { | |
61bd5218 | 4448 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
4449 | regno); |
4450 | return -EACCES; | |
4451 | } | |
457f4436 AN |
4452 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
4453 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 4454 | if (err) { |
457f4436 | 4455 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 4456 | regno); |
dbcfe5f7 GB |
4457 | return err; |
4458 | } | |
4459 | ||
b03c9f9f EC |
4460 | /* If we haven't set a max value then we need to bail since we can't be |
4461 | * sure we won't do bad things. | |
4462 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 4463 | */ |
b03c9f9f | 4464 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 4465 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
4466 | regno); |
4467 | return -EACCES; | |
4468 | } | |
457f4436 AN |
4469 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
4470 | mem_size, zero_size_allowed); | |
4471 | if (err) { | |
4472 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 4473 | regno); |
457f4436 AN |
4474 | return err; |
4475 | } | |
4476 | ||
4477 | return 0; | |
4478 | } | |
d83525ca | 4479 | |
e9147b44 KKD |
4480 | static int __check_ptr_off_reg(struct bpf_verifier_env *env, |
4481 | const struct bpf_reg_state *reg, int regno, | |
4482 | bool fixed_off_ok) | |
4483 | { | |
4484 | /* Access to this pointer-typed register or passing it to a helper | |
4485 | * is only allowed in its original, unmodified form. | |
4486 | */ | |
4487 | ||
4488 | if (reg->off < 0) { | |
4489 | verbose(env, "negative offset %s ptr R%d off=%d disallowed\n", | |
4490 | reg_type_str(env, reg->type), regno, reg->off); | |
4491 | return -EACCES; | |
4492 | } | |
4493 | ||
4494 | if (!fixed_off_ok && reg->off) { | |
4495 | verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n", | |
4496 | reg_type_str(env, reg->type), regno, reg->off); | |
4497 | return -EACCES; | |
4498 | } | |
4499 | ||
4500 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4501 | char tn_buf[48]; | |
4502 | ||
4503 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4504 | verbose(env, "variable %s access var_off=%s disallowed\n", | |
4505 | reg_type_str(env, reg->type), tn_buf); | |
4506 | return -EACCES; | |
4507 | } | |
4508 | ||
4509 | return 0; | |
4510 | } | |
4511 | ||
4512 | int check_ptr_off_reg(struct bpf_verifier_env *env, | |
4513 | const struct bpf_reg_state *reg, int regno) | |
4514 | { | |
4515 | return __check_ptr_off_reg(env, reg, regno, false); | |
4516 | } | |
4517 | ||
61df10c7 | 4518 | static int map_kptr_match_type(struct bpf_verifier_env *env, |
aa3496ac | 4519 | struct btf_field *kptr_field, |
61df10c7 KKD |
4520 | struct bpf_reg_state *reg, u32 regno) |
4521 | { | |
b32a5dae | 4522 | const char *targ_name = btf_type_name(kptr_field->kptr.btf, kptr_field->kptr.btf_id); |
20c09d92 | 4523 | int perm_flags = PTR_MAYBE_NULL | PTR_TRUSTED | MEM_RCU; |
61df10c7 KKD |
4524 | const char *reg_name = ""; |
4525 | ||
6efe152d | 4526 | /* Only unreferenced case accepts untrusted pointers */ |
aa3496ac | 4527 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
4528 | perm_flags |= PTR_UNTRUSTED; |
4529 | ||
4530 | if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags)) | |
61df10c7 KKD |
4531 | goto bad_type; |
4532 | ||
4533 | if (!btf_is_kernel(reg->btf)) { | |
4534 | verbose(env, "R%d must point to kernel BTF\n", regno); | |
4535 | return -EINVAL; | |
4536 | } | |
4537 | /* We need to verify reg->type and reg->btf, before accessing reg->btf */ | |
b32a5dae | 4538 | reg_name = btf_type_name(reg->btf, reg->btf_id); |
61df10c7 | 4539 | |
c0a5a21c KKD |
4540 | /* For ref_ptr case, release function check should ensure we get one |
4541 | * referenced PTR_TO_BTF_ID, and that its fixed offset is 0. For the | |
4542 | * normal store of unreferenced kptr, we must ensure var_off is zero. | |
4543 | * Since ref_ptr cannot be accessed directly by BPF insns, checks for | |
4544 | * reg->off and reg->ref_obj_id are not needed here. | |
4545 | */ | |
61df10c7 KKD |
4546 | if (__check_ptr_off_reg(env, reg, regno, true)) |
4547 | return -EACCES; | |
4548 | ||
4549 | /* A full type match is needed, as BTF can be vmlinux or module BTF, and | |
4550 | * we also need to take into account the reg->off. | |
4551 | * | |
4552 | * We want to support cases like: | |
4553 | * | |
4554 | * struct foo { | |
4555 | * struct bar br; | |
4556 | * struct baz bz; | |
4557 | * }; | |
4558 | * | |
4559 | * struct foo *v; | |
4560 | * v = func(); // PTR_TO_BTF_ID | |
4561 | * val->foo = v; // reg->off is zero, btf and btf_id match type | |
4562 | * val->bar = &v->br; // reg->off is still zero, but we need to retry with | |
4563 | * // first member type of struct after comparison fails | |
4564 | * val->baz = &v->bz; // reg->off is non-zero, so struct needs to be walked | |
4565 | * // to match type | |
4566 | * | |
4567 | * In the kptr_ref case, check_func_arg_reg_off already ensures reg->off | |
2ab3b380 KKD |
4568 | * is zero. We must also ensure that btf_struct_ids_match does not walk |
4569 | * the struct to match type against first member of struct, i.e. reject | |
4570 | * second case from above. Hence, when type is BPF_KPTR_REF, we set | |
4571 | * strict mode to true for type match. | |
61df10c7 KKD |
4572 | */ |
4573 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
aa3496ac KKD |
4574 | kptr_field->kptr.btf, kptr_field->kptr.btf_id, |
4575 | kptr_field->type == BPF_KPTR_REF)) | |
61df10c7 KKD |
4576 | goto bad_type; |
4577 | return 0; | |
4578 | bad_type: | |
4579 | verbose(env, "invalid kptr access, R%d type=%s%s ", regno, | |
4580 | reg_type_str(env, reg->type), reg_name); | |
6efe152d | 4581 | verbose(env, "expected=%s%s", reg_type_str(env, PTR_TO_BTF_ID), targ_name); |
aa3496ac | 4582 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
4583 | verbose(env, " or %s%s\n", reg_type_str(env, PTR_TO_BTF_ID | PTR_UNTRUSTED), |
4584 | targ_name); | |
4585 | else | |
4586 | verbose(env, "\n"); | |
61df10c7 KKD |
4587 | return -EINVAL; |
4588 | } | |
4589 | ||
20c09d92 AS |
4590 | /* The non-sleepable programs and sleepable programs with explicit bpf_rcu_read_lock() |
4591 | * can dereference RCU protected pointers and result is PTR_TRUSTED. | |
4592 | */ | |
4593 | static bool in_rcu_cs(struct bpf_verifier_env *env) | |
4594 | { | |
4595 | return env->cur_state->active_rcu_lock || !env->prog->aux->sleepable; | |
4596 | } | |
4597 | ||
4598 | /* Once GCC supports btf_type_tag the following mechanism will be replaced with tag check */ | |
4599 | BTF_SET_START(rcu_protected_types) | |
4600 | BTF_ID(struct, prog_test_ref_kfunc) | |
4601 | BTF_ID(struct, cgroup) | |
63d2d83d | 4602 | BTF_ID(struct, bpf_cpumask) |
d02c48fa | 4603 | BTF_ID(struct, task_struct) |
20c09d92 AS |
4604 | BTF_SET_END(rcu_protected_types) |
4605 | ||
4606 | static bool rcu_protected_object(const struct btf *btf, u32 btf_id) | |
4607 | { | |
4608 | if (!btf_is_kernel(btf)) | |
4609 | return false; | |
4610 | return btf_id_set_contains(&rcu_protected_types, btf_id); | |
4611 | } | |
4612 | ||
4613 | static bool rcu_safe_kptr(const struct btf_field *field) | |
4614 | { | |
4615 | const struct btf_field_kptr *kptr = &field->kptr; | |
4616 | ||
4617 | return field->type == BPF_KPTR_REF && rcu_protected_object(kptr->btf, kptr->btf_id); | |
4618 | } | |
4619 | ||
61df10c7 KKD |
4620 | static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, |
4621 | int value_regno, int insn_idx, | |
aa3496ac | 4622 | struct btf_field *kptr_field) |
61df10c7 KKD |
4623 | { |
4624 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; | |
4625 | int class = BPF_CLASS(insn->code); | |
4626 | struct bpf_reg_state *val_reg; | |
4627 | ||
4628 | /* Things we already checked for in check_map_access and caller: | |
4629 | * - Reject cases where variable offset may touch kptr | |
4630 | * - size of access (must be BPF_DW) | |
4631 | * - tnum_is_const(reg->var_off) | |
aa3496ac | 4632 | * - kptr_field->offset == off + reg->var_off.value |
61df10c7 KKD |
4633 | */ |
4634 | /* Only BPF_[LDX,STX,ST] | BPF_MEM | BPF_DW is supported */ | |
4635 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
4636 | verbose(env, "kptr in map can only be accessed using BPF_MEM instruction mode\n"); | |
4637 | return -EACCES; | |
4638 | } | |
4639 | ||
6efe152d KKD |
4640 | /* We only allow loading referenced kptr, since it will be marked as |
4641 | * untrusted, similar to unreferenced kptr. | |
4642 | */ | |
aa3496ac | 4643 | if (class != BPF_LDX && kptr_field->type == BPF_KPTR_REF) { |
6efe152d | 4644 | verbose(env, "store to referenced kptr disallowed\n"); |
c0a5a21c KKD |
4645 | return -EACCES; |
4646 | } | |
4647 | ||
61df10c7 KKD |
4648 | if (class == BPF_LDX) { |
4649 | val_reg = reg_state(env, value_regno); | |
4650 | /* We can simply mark the value_regno receiving the pointer | |
4651 | * value from map as PTR_TO_BTF_ID, with the correct type. | |
4652 | */ | |
aa3496ac | 4653 | mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, kptr_field->kptr.btf, |
20c09d92 AS |
4654 | kptr_field->kptr.btf_id, |
4655 | rcu_safe_kptr(kptr_field) && in_rcu_cs(env) ? | |
4656 | PTR_MAYBE_NULL | MEM_RCU : | |
4657 | PTR_MAYBE_NULL | PTR_UNTRUSTED); | |
61df10c7 KKD |
4658 | /* For mark_ptr_or_null_reg */ |
4659 | val_reg->id = ++env->id_gen; | |
4660 | } else if (class == BPF_STX) { | |
4661 | val_reg = reg_state(env, value_regno); | |
4662 | if (!register_is_null(val_reg) && | |
aa3496ac | 4663 | map_kptr_match_type(env, kptr_field, val_reg, value_regno)) |
61df10c7 KKD |
4664 | return -EACCES; |
4665 | } else if (class == BPF_ST) { | |
4666 | if (insn->imm) { | |
4667 | verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%u\n", | |
aa3496ac | 4668 | kptr_field->offset); |
61df10c7 KKD |
4669 | return -EACCES; |
4670 | } | |
4671 | } else { | |
4672 | verbose(env, "kptr in map can only be accessed using BPF_LDX/BPF_STX/BPF_ST\n"); | |
4673 | return -EACCES; | |
4674 | } | |
4675 | return 0; | |
4676 | } | |
4677 | ||
457f4436 AN |
4678 | /* check read/write into a map element with possible variable offset */ |
4679 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
61df10c7 KKD |
4680 | int off, int size, bool zero_size_allowed, |
4681 | enum bpf_access_src src) | |
457f4436 AN |
4682 | { |
4683 | struct bpf_verifier_state *vstate = env->cur_state; | |
4684 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4685 | struct bpf_reg_state *reg = &state->regs[regno]; | |
4686 | struct bpf_map *map = reg->map_ptr; | |
aa3496ac KKD |
4687 | struct btf_record *rec; |
4688 | int err, i; | |
457f4436 AN |
4689 | |
4690 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
4691 | zero_size_allowed); | |
4692 | if (err) | |
4693 | return err; | |
4694 | ||
aa3496ac KKD |
4695 | if (IS_ERR_OR_NULL(map->record)) |
4696 | return 0; | |
4697 | rec = map->record; | |
4698 | for (i = 0; i < rec->cnt; i++) { | |
4699 | struct btf_field *field = &rec->fields[i]; | |
4700 | u32 p = field->offset; | |
d83525ca | 4701 | |
db559117 KKD |
4702 | /* If any part of a field can be touched by load/store, reject |
4703 | * this program. To check that [x1, x2) overlaps with [y1, y2), | |
d83525ca AS |
4704 | * it is sufficient to check x1 < y2 && y1 < x2. |
4705 | */ | |
aa3496ac KKD |
4706 | if (reg->smin_value + off < p + btf_field_type_size(field->type) && |
4707 | p < reg->umax_value + off + size) { | |
4708 | switch (field->type) { | |
4709 | case BPF_KPTR_UNREF: | |
4710 | case BPF_KPTR_REF: | |
61df10c7 KKD |
4711 | if (src != ACCESS_DIRECT) { |
4712 | verbose(env, "kptr cannot be accessed indirectly by helper\n"); | |
4713 | return -EACCES; | |
4714 | } | |
4715 | if (!tnum_is_const(reg->var_off)) { | |
4716 | verbose(env, "kptr access cannot have variable offset\n"); | |
4717 | return -EACCES; | |
4718 | } | |
4719 | if (p != off + reg->var_off.value) { | |
4720 | verbose(env, "kptr access misaligned expected=%u off=%llu\n", | |
4721 | p, off + reg->var_off.value); | |
4722 | return -EACCES; | |
4723 | } | |
4724 | if (size != bpf_size_to_bytes(BPF_DW)) { | |
4725 | verbose(env, "kptr access size must be BPF_DW\n"); | |
4726 | return -EACCES; | |
4727 | } | |
4728 | break; | |
aa3496ac | 4729 | default: |
db559117 KKD |
4730 | verbose(env, "%s cannot be accessed directly by load/store\n", |
4731 | btf_field_type_name(field->type)); | |
aa3496ac | 4732 | return -EACCES; |
61df10c7 KKD |
4733 | } |
4734 | } | |
4735 | } | |
aa3496ac | 4736 | return 0; |
dbcfe5f7 GB |
4737 | } |
4738 | ||
969bf05e AS |
4739 | #define MAX_PACKET_OFF 0xffff |
4740 | ||
58e2af8b | 4741 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
4742 | const struct bpf_call_arg_meta *meta, |
4743 | enum bpf_access_type t) | |
4acf6c0b | 4744 | { |
7e40781c UP |
4745 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
4746 | ||
4747 | switch (prog_type) { | |
5d66fa7d | 4748 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
4749 | case BPF_PROG_TYPE_LWT_IN: |
4750 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 4751 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 4752 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 4753 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 4754 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
4755 | if (t == BPF_WRITE) |
4756 | return false; | |
8731745e | 4757 | fallthrough; |
5d66fa7d DB |
4758 | |
4759 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
4760 | case BPF_PROG_TYPE_SCHED_CLS: |
4761 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 4762 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 4763 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 4764 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 4765 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
4766 | if (meta) |
4767 | return meta->pkt_access; | |
4768 | ||
4769 | env->seen_direct_write = true; | |
4acf6c0b | 4770 | return true; |
0d01da6a SF |
4771 | |
4772 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
4773 | if (t == BPF_WRITE) | |
4774 | env->seen_direct_write = true; | |
4775 | ||
4776 | return true; | |
4777 | ||
4acf6c0b BB |
4778 | default: |
4779 | return false; | |
4780 | } | |
4781 | } | |
4782 | ||
f1174f77 | 4783 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 4784 | int size, bool zero_size_allowed) |
f1174f77 | 4785 | { |
638f5b90 | 4786 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
4787 | struct bpf_reg_state *reg = ®s[regno]; |
4788 | int err; | |
4789 | ||
4790 | /* We may have added a variable offset to the packet pointer; but any | |
4791 | * reg->range we have comes after that. We are only checking the fixed | |
4792 | * offset. | |
4793 | */ | |
4794 | ||
4795 | /* We don't allow negative numbers, because we aren't tracking enough | |
4796 | * detail to prove they're safe. | |
4797 | */ | |
b03c9f9f | 4798 | if (reg->smin_value < 0) { |
61bd5218 | 4799 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
4800 | regno); |
4801 | return -EACCES; | |
4802 | } | |
6d94e741 AS |
4803 | |
4804 | err = reg->range < 0 ? -EINVAL : | |
4805 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 4806 | zero_size_allowed); |
f1174f77 | 4807 | if (err) { |
61bd5218 | 4808 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
4809 | return err; |
4810 | } | |
e647815a | 4811 | |
457f4436 | 4812 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
4813 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
4814 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 4815 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
4816 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
4817 | */ | |
4818 | env->prog->aux->max_pkt_offset = | |
4819 | max_t(u32, env->prog->aux->max_pkt_offset, | |
4820 | off + reg->umax_value + size - 1); | |
4821 | ||
f1174f77 EC |
4822 | return err; |
4823 | } | |
4824 | ||
4825 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 4826 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 4827 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 4828 | struct btf **btf, u32 *btf_id) |
17a52670 | 4829 | { |
f96da094 DB |
4830 | struct bpf_insn_access_aux info = { |
4831 | .reg_type = *reg_type, | |
9e15db66 | 4832 | .log = &env->log, |
f96da094 | 4833 | }; |
31fd8581 | 4834 | |
4f9218aa | 4835 | if (env->ops->is_valid_access && |
5e43f899 | 4836 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
4837 | /* A non zero info.ctx_field_size indicates that this field is a |
4838 | * candidate for later verifier transformation to load the whole | |
4839 | * field and then apply a mask when accessed with a narrower | |
4840 | * access than actual ctx access size. A zero info.ctx_field_size | |
4841 | * will only allow for whole field access and rejects any other | |
4842 | * type of narrower access. | |
31fd8581 | 4843 | */ |
23994631 | 4844 | *reg_type = info.reg_type; |
31fd8581 | 4845 | |
c25b2ae1 | 4846 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 4847 | *btf = info.btf; |
9e15db66 | 4848 | *btf_id = info.btf_id; |
22dc4a0f | 4849 | } else { |
9e15db66 | 4850 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 4851 | } |
32bbe007 AS |
4852 | /* remember the offset of last byte accessed in ctx */ |
4853 | if (env->prog->aux->max_ctx_offset < off + size) | |
4854 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 4855 | return 0; |
32bbe007 | 4856 | } |
17a52670 | 4857 | |
61bd5218 | 4858 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
4859 | return -EACCES; |
4860 | } | |
4861 | ||
d58e468b PP |
4862 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
4863 | int size) | |
4864 | { | |
4865 | if (size < 0 || off < 0 || | |
4866 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
4867 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
4868 | off, size); | |
4869 | return -EACCES; | |
4870 | } | |
4871 | return 0; | |
4872 | } | |
4873 | ||
5f456649 MKL |
4874 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
4875 | u32 regno, int off, int size, | |
4876 | enum bpf_access_type t) | |
c64b7983 JS |
4877 | { |
4878 | struct bpf_reg_state *regs = cur_regs(env); | |
4879 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 4880 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 4881 | bool valid; |
c64b7983 JS |
4882 | |
4883 | if (reg->smin_value < 0) { | |
4884 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
4885 | regno); | |
4886 | return -EACCES; | |
4887 | } | |
4888 | ||
46f8bc92 MKL |
4889 | switch (reg->type) { |
4890 | case PTR_TO_SOCK_COMMON: | |
4891 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
4892 | break; | |
4893 | case PTR_TO_SOCKET: | |
4894 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
4895 | break; | |
655a51e5 MKL |
4896 | case PTR_TO_TCP_SOCK: |
4897 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
4898 | break; | |
fada7fdc JL |
4899 | case PTR_TO_XDP_SOCK: |
4900 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
4901 | break; | |
46f8bc92 MKL |
4902 | default: |
4903 | valid = false; | |
c64b7983 JS |
4904 | } |
4905 | ||
5f456649 | 4906 | |
46f8bc92 MKL |
4907 | if (valid) { |
4908 | env->insn_aux_data[insn_idx].ctx_field_size = | |
4909 | info.ctx_field_size; | |
4910 | return 0; | |
4911 | } | |
4912 | ||
4913 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
c25b2ae1 | 4914 | regno, reg_type_str(env, reg->type), off, size); |
46f8bc92 MKL |
4915 | |
4916 | return -EACCES; | |
c64b7983 JS |
4917 | } |
4918 | ||
4cabc5b1 DB |
4919 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
4920 | { | |
2a159c6f | 4921 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
4922 | } |
4923 | ||
f37a8cb8 DB |
4924 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
4925 | { | |
2a159c6f | 4926 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 4927 | |
46f8bc92 MKL |
4928 | return reg->type == PTR_TO_CTX; |
4929 | } | |
4930 | ||
4931 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
4932 | { | |
4933 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4934 | ||
4935 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
4936 | } |
4937 | ||
ca369602 DB |
4938 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
4939 | { | |
2a159c6f | 4940 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
4941 | |
4942 | return type_is_pkt_pointer(reg->type); | |
4943 | } | |
4944 | ||
4b5defde DB |
4945 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
4946 | { | |
4947 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4948 | ||
4949 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
4950 | return reg->type == PTR_TO_FLOW_KEYS; | |
4951 | } | |
4952 | ||
9bb00b28 YS |
4953 | static bool is_trusted_reg(const struct bpf_reg_state *reg) |
4954 | { | |
4955 | /* A referenced register is always trusted. */ | |
4956 | if (reg->ref_obj_id) | |
4957 | return true; | |
4958 | ||
4959 | /* If a register is not referenced, it is trusted if it has the | |
fca1aa75 | 4960 | * MEM_ALLOC or PTR_TRUSTED type modifiers, and no others. Some of the |
9bb00b28 YS |
4961 | * other type modifiers may be safe, but we elect to take an opt-in |
4962 | * approach here as some (e.g. PTR_UNTRUSTED and PTR_MAYBE_NULL) are | |
4963 | * not. | |
4964 | * | |
4965 | * Eventually, we should make PTR_TRUSTED the single source of truth | |
4966 | * for whether a register is trusted. | |
4967 | */ | |
4968 | return type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS && | |
4969 | !bpf_type_has_unsafe_modifiers(reg->type); | |
4970 | } | |
4971 | ||
fca1aa75 YS |
4972 | static bool is_rcu_reg(const struct bpf_reg_state *reg) |
4973 | { | |
4974 | return reg->type & MEM_RCU; | |
4975 | } | |
4976 | ||
61bd5218 JK |
4977 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
4978 | const struct bpf_reg_state *reg, | |
d1174416 | 4979 | int off, int size, bool strict) |
969bf05e | 4980 | { |
f1174f77 | 4981 | struct tnum reg_off; |
e07b98d9 | 4982 | int ip_align; |
d1174416 DM |
4983 | |
4984 | /* Byte size accesses are always allowed. */ | |
4985 | if (!strict || size == 1) | |
4986 | return 0; | |
4987 | ||
e4eda884 DM |
4988 | /* For platforms that do not have a Kconfig enabling |
4989 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
4990 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
4991 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
4992 | * to this code only in strict mode where we want to emulate | |
4993 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
4994 | * unconditional IP align value of '2'. | |
e07b98d9 | 4995 | */ |
e4eda884 | 4996 | ip_align = 2; |
f1174f77 EC |
4997 | |
4998 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
4999 | if (!tnum_is_aligned(reg_off, size)) { | |
5000 | char tn_buf[48]; | |
5001 | ||
5002 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
5003 | verbose(env, |
5004 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 5005 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
5006 | return -EACCES; |
5007 | } | |
79adffcd | 5008 | |
969bf05e AS |
5009 | return 0; |
5010 | } | |
5011 | ||
61bd5218 JK |
5012 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
5013 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
5014 | const char *pointer_desc, |
5015 | int off, int size, bool strict) | |
79adffcd | 5016 | { |
f1174f77 EC |
5017 | struct tnum reg_off; |
5018 | ||
5019 | /* Byte size accesses are always allowed. */ | |
5020 | if (!strict || size == 1) | |
5021 | return 0; | |
5022 | ||
5023 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
5024 | if (!tnum_is_aligned(reg_off, size)) { | |
5025 | char tn_buf[48]; | |
5026 | ||
5027 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 5028 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 5029 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
5030 | return -EACCES; |
5031 | } | |
5032 | ||
969bf05e AS |
5033 | return 0; |
5034 | } | |
5035 | ||
e07b98d9 | 5036 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
5037 | const struct bpf_reg_state *reg, int off, |
5038 | int size, bool strict_alignment_once) | |
79adffcd | 5039 | { |
ca369602 | 5040 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 5041 | const char *pointer_desc = ""; |
d1174416 | 5042 | |
79adffcd DB |
5043 | switch (reg->type) { |
5044 | case PTR_TO_PACKET: | |
de8f3a83 DB |
5045 | case PTR_TO_PACKET_META: |
5046 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
5047 | * right in front, treat it the very same way. | |
5048 | */ | |
61bd5218 | 5049 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
5050 | case PTR_TO_FLOW_KEYS: |
5051 | pointer_desc = "flow keys "; | |
5052 | break; | |
69c087ba YS |
5053 | case PTR_TO_MAP_KEY: |
5054 | pointer_desc = "key "; | |
5055 | break; | |
f1174f77 EC |
5056 | case PTR_TO_MAP_VALUE: |
5057 | pointer_desc = "value "; | |
5058 | break; | |
5059 | case PTR_TO_CTX: | |
5060 | pointer_desc = "context "; | |
5061 | break; | |
5062 | case PTR_TO_STACK: | |
5063 | pointer_desc = "stack "; | |
01f810ac AM |
5064 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
5065 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
5066 | * aligned. |
5067 | */ | |
5068 | strict = true; | |
f1174f77 | 5069 | break; |
c64b7983 JS |
5070 | case PTR_TO_SOCKET: |
5071 | pointer_desc = "sock "; | |
5072 | break; | |
46f8bc92 MKL |
5073 | case PTR_TO_SOCK_COMMON: |
5074 | pointer_desc = "sock_common "; | |
5075 | break; | |
655a51e5 MKL |
5076 | case PTR_TO_TCP_SOCK: |
5077 | pointer_desc = "tcp_sock "; | |
5078 | break; | |
fada7fdc JL |
5079 | case PTR_TO_XDP_SOCK: |
5080 | pointer_desc = "xdp_sock "; | |
5081 | break; | |
79adffcd | 5082 | default: |
f1174f77 | 5083 | break; |
79adffcd | 5084 | } |
61bd5218 JK |
5085 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
5086 | strict); | |
79adffcd DB |
5087 | } |
5088 | ||
f4d7e40a AS |
5089 | static int update_stack_depth(struct bpf_verifier_env *env, |
5090 | const struct bpf_func_state *func, | |
5091 | int off) | |
5092 | { | |
9c8105bd | 5093 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
5094 | |
5095 | if (stack >= -off) | |
5096 | return 0; | |
5097 | ||
5098 | /* update known max for given subprogram */ | |
9c8105bd | 5099 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
5100 | return 0; |
5101 | } | |
f4d7e40a | 5102 | |
70a87ffe AS |
5103 | /* starting from main bpf function walk all instructions of the function |
5104 | * and recursively walk all callees that given function can call. | |
5105 | * Ignore jump and exit insns. | |
5106 | * Since recursion is prevented by check_cfg() this algorithm | |
5107 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
5108 | */ | |
5109 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
5110 | { | |
9c8105bd JW |
5111 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
5112 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 5113 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 5114 | bool tail_call_reachable = false; |
70a87ffe AS |
5115 | int ret_insn[MAX_CALL_FRAMES]; |
5116 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 5117 | int j; |
f4d7e40a | 5118 | |
70a87ffe | 5119 | process_func: |
7f6e4312 MF |
5120 | /* protect against potential stack overflow that might happen when |
5121 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
5122 | * depth for such case down to 256 so that the worst case scenario | |
5123 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
5124 | * 8k). | |
5125 | * | |
5126 | * To get the idea what might happen, see an example: | |
5127 | * func1 -> sub rsp, 128 | |
5128 | * subfunc1 -> sub rsp, 256 | |
5129 | * tailcall1 -> add rsp, 256 | |
5130 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
5131 | * subfunc2 -> sub rsp, 64 | |
5132 | * subfunc22 -> sub rsp, 128 | |
5133 | * tailcall2 -> add rsp, 128 | |
5134 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
5135 | * | |
5136 | * tailcall will unwind the current stack frame but it will not get rid | |
5137 | * of caller's stack as shown on the example above. | |
5138 | */ | |
5139 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
5140 | verbose(env, | |
5141 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
5142 | depth); | |
5143 | return -EACCES; | |
5144 | } | |
70a87ffe AS |
5145 | /* round up to 32-bytes, since this is granularity |
5146 | * of interpreter stack size | |
5147 | */ | |
9c8105bd | 5148 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 5149 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 5150 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 5151 | frame + 1, depth); |
f4d7e40a AS |
5152 | return -EACCES; |
5153 | } | |
70a87ffe | 5154 | continue_func: |
4cb3d99c | 5155 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 5156 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
5157 | int next_insn; |
5158 | ||
69c087ba | 5159 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
5160 | continue; |
5161 | /* remember insn and function to return to */ | |
5162 | ret_insn[frame] = i + 1; | |
9c8105bd | 5163 | ret_prog[frame] = idx; |
70a87ffe AS |
5164 | |
5165 | /* find the callee */ | |
7ddc80a4 AS |
5166 | next_insn = i + insn[i].imm + 1; |
5167 | idx = find_subprog(env, next_insn); | |
9c8105bd | 5168 | if (idx < 0) { |
70a87ffe | 5169 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 5170 | next_insn); |
70a87ffe AS |
5171 | return -EFAULT; |
5172 | } | |
7ddc80a4 AS |
5173 | if (subprog[idx].is_async_cb) { |
5174 | if (subprog[idx].has_tail_call) { | |
5175 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
5176 | return -EFAULT; | |
5177 | } | |
5178 | /* async callbacks don't increase bpf prog stack size */ | |
5179 | continue; | |
5180 | } | |
5181 | i = next_insn; | |
ebf7d1f5 MF |
5182 | |
5183 | if (subprog[idx].has_tail_call) | |
5184 | tail_call_reachable = true; | |
5185 | ||
70a87ffe AS |
5186 | frame++; |
5187 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
5188 | verbose(env, "the call stack of %d frames is too deep !\n", |
5189 | frame); | |
5190 | return -E2BIG; | |
70a87ffe AS |
5191 | } |
5192 | goto process_func; | |
5193 | } | |
ebf7d1f5 MF |
5194 | /* if tail call got detected across bpf2bpf calls then mark each of the |
5195 | * currently present subprog frames as tail call reachable subprogs; | |
5196 | * this info will be utilized by JIT so that we will be preserving the | |
5197 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
5198 | */ | |
5199 | if (tail_call_reachable) | |
5200 | for (j = 0; j < frame; j++) | |
5201 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
5202 | if (subprog[0].tail_call_reachable) |
5203 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 5204 | |
70a87ffe AS |
5205 | /* end of for() loop means the last insn of the 'subprog' |
5206 | * was reached. Doesn't matter whether it was JA or EXIT | |
5207 | */ | |
5208 | if (frame == 0) | |
5209 | return 0; | |
9c8105bd | 5210 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
5211 | frame--; |
5212 | i = ret_insn[frame]; | |
9c8105bd | 5213 | idx = ret_prog[frame]; |
70a87ffe | 5214 | goto continue_func; |
f4d7e40a AS |
5215 | } |
5216 | ||
19d28fbd | 5217 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
5218 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
5219 | const struct bpf_insn *insn, int idx) | |
5220 | { | |
5221 | int start = idx + insn->imm + 1, subprog; | |
5222 | ||
5223 | subprog = find_subprog(env, start); | |
5224 | if (subprog < 0) { | |
5225 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
5226 | start); | |
5227 | return -EFAULT; | |
5228 | } | |
9c8105bd | 5229 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 5230 | } |
19d28fbd | 5231 | #endif |
1ea47e01 | 5232 | |
afbf21dc YS |
5233 | static int __check_buffer_access(struct bpf_verifier_env *env, |
5234 | const char *buf_info, | |
5235 | const struct bpf_reg_state *reg, | |
5236 | int regno, int off, int size) | |
9df1c28b MM |
5237 | { |
5238 | if (off < 0) { | |
5239 | verbose(env, | |
4fc00b79 | 5240 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 5241 | regno, buf_info, off, size); |
9df1c28b MM |
5242 | return -EACCES; |
5243 | } | |
5244 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5245 | char tn_buf[48]; | |
5246 | ||
5247 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5248 | verbose(env, | |
4fc00b79 | 5249 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
5250 | regno, off, tn_buf); |
5251 | return -EACCES; | |
5252 | } | |
afbf21dc YS |
5253 | |
5254 | return 0; | |
5255 | } | |
5256 | ||
5257 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
5258 | const struct bpf_reg_state *reg, | |
5259 | int regno, int off, int size) | |
5260 | { | |
5261 | int err; | |
5262 | ||
5263 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
5264 | if (err) | |
5265 | return err; | |
5266 | ||
9df1c28b MM |
5267 | if (off + size > env->prog->aux->max_tp_access) |
5268 | env->prog->aux->max_tp_access = off + size; | |
5269 | ||
5270 | return 0; | |
5271 | } | |
5272 | ||
afbf21dc YS |
5273 | static int check_buffer_access(struct bpf_verifier_env *env, |
5274 | const struct bpf_reg_state *reg, | |
5275 | int regno, int off, int size, | |
5276 | bool zero_size_allowed, | |
afbf21dc YS |
5277 | u32 *max_access) |
5278 | { | |
44e9a741 | 5279 | const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; |
afbf21dc YS |
5280 | int err; |
5281 | ||
5282 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
5283 | if (err) | |
5284 | return err; | |
5285 | ||
5286 | if (off + size > *max_access) | |
5287 | *max_access = off + size; | |
5288 | ||
5289 | return 0; | |
5290 | } | |
5291 | ||
3f50f132 JF |
5292 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
5293 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
5294 | { | |
5295 | reg->var_off = tnum_subreg(reg->var_off); | |
5296 | __reg_assign_32_into_64(reg); | |
5297 | } | |
9df1c28b | 5298 | |
0c17d1d2 JH |
5299 | /* truncate register to smaller size (in bytes) |
5300 | * must be called with size < BPF_REG_SIZE | |
5301 | */ | |
5302 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
5303 | { | |
5304 | u64 mask; | |
5305 | ||
5306 | /* clear high bits in bit representation */ | |
5307 | reg->var_off = tnum_cast(reg->var_off, size); | |
5308 | ||
5309 | /* fix arithmetic bounds */ | |
5310 | mask = ((u64)1 << (size * 8)) - 1; | |
5311 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
5312 | reg->umin_value &= mask; | |
5313 | reg->umax_value &= mask; | |
5314 | } else { | |
5315 | reg->umin_value = 0; | |
5316 | reg->umax_value = mask; | |
5317 | } | |
5318 | reg->smin_value = reg->umin_value; | |
5319 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
5320 | |
5321 | /* If size is smaller than 32bit register the 32bit register | |
5322 | * values are also truncated so we push 64-bit bounds into | |
5323 | * 32-bit bounds. Above were truncated < 32-bits already. | |
5324 | */ | |
5325 | if (size >= 4) | |
5326 | return; | |
5327 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
5328 | } |
5329 | ||
a23740ec AN |
5330 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
5331 | { | |
353050be DB |
5332 | /* A map is considered read-only if the following condition are true: |
5333 | * | |
5334 | * 1) BPF program side cannot change any of the map content. The | |
5335 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
5336 | * and was set at map creation time. | |
5337 | * 2) The map value(s) have been initialized from user space by a | |
5338 | * loader and then "frozen", such that no new map update/delete | |
5339 | * operations from syscall side are possible for the rest of | |
5340 | * the map's lifetime from that point onwards. | |
5341 | * 3) Any parallel/pending map update/delete operations from syscall | |
5342 | * side have been completed. Only after that point, it's safe to | |
5343 | * assume that map value(s) are immutable. | |
5344 | */ | |
5345 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
5346 | READ_ONCE(map->frozen) && | |
5347 | !bpf_map_write_active(map); | |
a23740ec AN |
5348 | } |
5349 | ||
5350 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
5351 | { | |
5352 | void *ptr; | |
5353 | u64 addr; | |
5354 | int err; | |
5355 | ||
5356 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
5357 | if (err) | |
5358 | return err; | |
2dedd7d2 | 5359 | ptr = (void *)(long)addr + off; |
a23740ec AN |
5360 | |
5361 | switch (size) { | |
5362 | case sizeof(u8): | |
5363 | *val = (u64)*(u8 *)ptr; | |
5364 | break; | |
5365 | case sizeof(u16): | |
5366 | *val = (u64)*(u16 *)ptr; | |
5367 | break; | |
5368 | case sizeof(u32): | |
5369 | *val = (u64)*(u32 *)ptr; | |
5370 | break; | |
5371 | case sizeof(u64): | |
5372 | *val = *(u64 *)ptr; | |
5373 | break; | |
5374 | default: | |
5375 | return -EINVAL; | |
5376 | } | |
5377 | return 0; | |
5378 | } | |
5379 | ||
6fcd486b AS |
5380 | #define BTF_TYPE_SAFE_RCU(__type) __PASTE(__type, __safe_rcu) |
5381 | #define BTF_TYPE_SAFE_TRUSTED(__type) __PASTE(__type, __safe_trusted) | |
57539b1c | 5382 | |
6fcd486b AS |
5383 | /* |
5384 | * Allow list few fields as RCU trusted or full trusted. | |
5385 | * This logic doesn't allow mix tagging and will be removed once GCC supports | |
5386 | * btf_type_tag. | |
5387 | */ | |
5388 | ||
5389 | /* RCU trusted: these fields are trusted in RCU CS and never NULL */ | |
5390 | BTF_TYPE_SAFE_RCU(struct task_struct) { | |
57539b1c | 5391 | const cpumask_t *cpus_ptr; |
8d093b4e | 5392 | struct css_set __rcu *cgroups; |
6fcd486b AS |
5393 | struct task_struct __rcu *real_parent; |
5394 | struct task_struct *group_leader; | |
8d093b4e AS |
5395 | }; |
5396 | ||
6fcd486b | 5397 | BTF_TYPE_SAFE_RCU(struct css_set) { |
8d093b4e | 5398 | struct cgroup *dfl_cgrp; |
57539b1c DV |
5399 | }; |
5400 | ||
6fcd486b AS |
5401 | /* full trusted: these fields are trusted even outside of RCU CS and never NULL */ |
5402 | BTF_TYPE_SAFE_TRUSTED(struct bpf_iter_meta) { | |
5403 | __bpf_md_ptr(struct seq_file *, seq); | |
5404 | }; | |
5405 | ||
5406 | BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task) { | |
5407 | __bpf_md_ptr(struct bpf_iter_meta *, meta); | |
5408 | __bpf_md_ptr(struct task_struct *, task); | |
5409 | }; | |
5410 | ||
5411 | BTF_TYPE_SAFE_TRUSTED(struct linux_binprm) { | |
5412 | struct file *file; | |
5413 | }; | |
5414 | ||
5415 | BTF_TYPE_SAFE_TRUSTED(struct file) { | |
5416 | struct inode *f_inode; | |
5417 | }; | |
5418 | ||
5419 | BTF_TYPE_SAFE_TRUSTED(struct dentry) { | |
5420 | /* no negative dentry-s in places where bpf can see it */ | |
5421 | struct inode *d_inode; | |
5422 | }; | |
5423 | ||
5424 | BTF_TYPE_SAFE_TRUSTED(struct socket) { | |
5425 | struct sock *sk; | |
5426 | }; | |
5427 | ||
5428 | static bool type_is_rcu(struct bpf_verifier_env *env, | |
5429 | struct bpf_reg_state *reg, | |
5430 | int off) | |
57539b1c | 5431 | { |
6fcd486b AS |
5432 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU(struct task_struct)); |
5433 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU(struct css_set)); | |
57539b1c | 5434 | |
6fcd486b AS |
5435 | return btf_nested_type_is_trusted(&env->log, reg, off, "__safe_rcu"); |
5436 | } | |
57539b1c | 5437 | |
6fcd486b AS |
5438 | static bool type_is_trusted(struct bpf_verifier_env *env, |
5439 | struct bpf_reg_state *reg, | |
5440 | int off) | |
5441 | { | |
5442 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter_meta)); | |
5443 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task)); | |
5444 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct linux_binprm)); | |
5445 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct file)); | |
5446 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct dentry)); | |
5447 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct socket)); | |
5448 | ||
5449 | return btf_nested_type_is_trusted(&env->log, reg, off, "__safe_trusted"); | |
57539b1c DV |
5450 | } |
5451 | ||
9e15db66 AS |
5452 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
5453 | struct bpf_reg_state *regs, | |
5454 | int regno, int off, int size, | |
5455 | enum bpf_access_type atype, | |
5456 | int value_regno) | |
5457 | { | |
5458 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
5459 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
5460 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
c6f1bfe8 | 5461 | enum bpf_type_flag flag = 0; |
9e15db66 AS |
5462 | u32 btf_id; |
5463 | int ret; | |
5464 | ||
c67cae55 AS |
5465 | if (!env->allow_ptr_leaks) { |
5466 | verbose(env, | |
5467 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
5468 | tname); | |
5469 | return -EPERM; | |
5470 | } | |
5471 | if (!env->prog->gpl_compatible && btf_is_kernel(reg->btf)) { | |
5472 | verbose(env, | |
5473 | "Cannot access kernel 'struct %s' from non-GPL compatible program\n", | |
5474 | tname); | |
5475 | return -EINVAL; | |
5476 | } | |
9e15db66 AS |
5477 | if (off < 0) { |
5478 | verbose(env, | |
5479 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
5480 | regno, tname, off); | |
5481 | return -EACCES; | |
5482 | } | |
5483 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5484 | char tn_buf[48]; | |
5485 | ||
5486 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5487 | verbose(env, | |
5488 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
5489 | regno, tname, off, tn_buf); | |
5490 | return -EACCES; | |
5491 | } | |
5492 | ||
c6f1bfe8 YS |
5493 | if (reg->type & MEM_USER) { |
5494 | verbose(env, | |
5495 | "R%d is ptr_%s access user memory: off=%d\n", | |
5496 | regno, tname, off); | |
5497 | return -EACCES; | |
5498 | } | |
5499 | ||
5844101a HL |
5500 | if (reg->type & MEM_PERCPU) { |
5501 | verbose(env, | |
5502 | "R%d is ptr_%s access percpu memory: off=%d\n", | |
5503 | regno, tname, off); | |
5504 | return -EACCES; | |
5505 | } | |
5506 | ||
7d64c513 | 5507 | if (env->ops->btf_struct_access && !type_is_alloc(reg->type) && atype == BPF_WRITE) { |
282de143 KKD |
5508 | if (!btf_is_kernel(reg->btf)) { |
5509 | verbose(env, "verifier internal error: reg->btf must be kernel btf\n"); | |
5510 | return -EFAULT; | |
5511 | } | |
6728aea7 | 5512 | ret = env->ops->btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 | 5513 | } else { |
282de143 KKD |
5514 | /* Writes are permitted with default btf_struct_access for |
5515 | * program allocated objects (which always have ref_obj_id > 0), | |
5516 | * but not for untrusted PTR_TO_BTF_ID | MEM_ALLOC. | |
5517 | */ | |
5518 | if (atype != BPF_READ && reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
27ae7997 MKL |
5519 | verbose(env, "only read is supported\n"); |
5520 | return -EACCES; | |
5521 | } | |
5522 | ||
6a3cd331 DM |
5523 | if (type_is_alloc(reg->type) && !type_is_non_owning_ref(reg->type) && |
5524 | !reg->ref_obj_id) { | |
282de143 KKD |
5525 | verbose(env, "verifier internal error: ref_obj_id for allocated object must be non-zero\n"); |
5526 | return -EFAULT; | |
5527 | } | |
5528 | ||
6728aea7 | 5529 | ret = btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 MKL |
5530 | } |
5531 | ||
9e15db66 AS |
5532 | if (ret < 0) |
5533 | return ret; | |
5534 | ||
6fcd486b AS |
5535 | if (ret != PTR_TO_BTF_ID) { |
5536 | /* just mark; */ | |
6efe152d | 5537 | |
6fcd486b AS |
5538 | } else if (type_flag(reg->type) & PTR_UNTRUSTED) { |
5539 | /* If this is an untrusted pointer, all pointers formed by walking it | |
5540 | * also inherit the untrusted flag. | |
5541 | */ | |
5542 | flag = PTR_UNTRUSTED; | |
5543 | ||
5544 | } else if (is_trusted_reg(reg) || is_rcu_reg(reg)) { | |
5545 | /* By default any pointer obtained from walking a trusted pointer is no | |
5546 | * longer trusted, unless the field being accessed has explicitly been | |
5547 | * marked as inheriting its parent's state of trust (either full or RCU). | |
5548 | * For example: | |
5549 | * 'cgroups' pointer is untrusted if task->cgroups dereference | |
5550 | * happened in a sleepable program outside of bpf_rcu_read_lock() | |
5551 | * section. In a non-sleepable program it's trusted while in RCU CS (aka MEM_RCU). | |
5552 | * Note bpf_rcu_read_unlock() converts MEM_RCU pointers to PTR_UNTRUSTED. | |
5553 | * | |
5554 | * A regular RCU-protected pointer with __rcu tag can also be deemed | |
5555 | * trusted if we are in an RCU CS. Such pointer can be NULL. | |
20c09d92 | 5556 | */ |
6fcd486b AS |
5557 | if (type_is_trusted(env, reg, off)) { |
5558 | flag |= PTR_TRUSTED; | |
5559 | } else if (in_rcu_cs(env) && !type_may_be_null(reg->type)) { | |
5560 | if (type_is_rcu(env, reg, off)) { | |
5561 | /* ignore __rcu tag and mark it MEM_RCU */ | |
5562 | flag |= MEM_RCU; | |
5563 | } else if (flag & MEM_RCU) { | |
5564 | /* __rcu tagged pointers can be NULL */ | |
5565 | flag |= PTR_MAYBE_NULL; | |
5566 | } else if (flag & (MEM_PERCPU | MEM_USER)) { | |
5567 | /* keep as-is */ | |
5568 | } else { | |
5569 | /* walking unknown pointers yields untrusted pointer */ | |
5570 | flag = PTR_UNTRUSTED; | |
5571 | } | |
5572 | } else { | |
5573 | /* | |
5574 | * If not in RCU CS or MEM_RCU pointer can be NULL then | |
5575 | * aggressively mark as untrusted otherwise such | |
5576 | * pointers will be plain PTR_TO_BTF_ID without flags | |
5577 | * and will be allowed to be passed into helpers for | |
5578 | * compat reasons. | |
5579 | */ | |
5580 | flag = PTR_UNTRUSTED; | |
5581 | } | |
20c09d92 | 5582 | } else { |
6fcd486b | 5583 | /* Old compat. Deprecated */ |
57539b1c | 5584 | flag &= ~PTR_TRUSTED; |
20c09d92 | 5585 | } |
3f00c523 | 5586 | |
41c48f3a | 5587 | if (atype == BPF_READ && value_regno >= 0) |
c6f1bfe8 | 5588 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); |
41c48f3a AI |
5589 | |
5590 | return 0; | |
5591 | } | |
5592 | ||
5593 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
5594 | struct bpf_reg_state *regs, | |
5595 | int regno, int off, int size, | |
5596 | enum bpf_access_type atype, | |
5597 | int value_regno) | |
5598 | { | |
5599 | struct bpf_reg_state *reg = regs + regno; | |
5600 | struct bpf_map *map = reg->map_ptr; | |
6728aea7 | 5601 | struct bpf_reg_state map_reg; |
c6f1bfe8 | 5602 | enum bpf_type_flag flag = 0; |
41c48f3a AI |
5603 | const struct btf_type *t; |
5604 | const char *tname; | |
5605 | u32 btf_id; | |
5606 | int ret; | |
5607 | ||
5608 | if (!btf_vmlinux) { | |
5609 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
5610 | return -ENOTSUPP; | |
5611 | } | |
5612 | ||
5613 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
5614 | verbose(env, "map_ptr access not supported for map type %d\n", | |
5615 | map->map_type); | |
5616 | return -ENOTSUPP; | |
5617 | } | |
5618 | ||
5619 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
5620 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
5621 | ||
c67cae55 | 5622 | if (!env->allow_ptr_leaks) { |
41c48f3a | 5623 | verbose(env, |
c67cae55 | 5624 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
41c48f3a AI |
5625 | tname); |
5626 | return -EPERM; | |
9e15db66 | 5627 | } |
27ae7997 | 5628 | |
41c48f3a AI |
5629 | if (off < 0) { |
5630 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
5631 | regno, tname, off); | |
5632 | return -EACCES; | |
5633 | } | |
5634 | ||
5635 | if (atype != BPF_READ) { | |
5636 | verbose(env, "only read from %s is supported\n", tname); | |
5637 | return -EACCES; | |
5638 | } | |
5639 | ||
6728aea7 KKD |
5640 | /* Simulate access to a PTR_TO_BTF_ID */ |
5641 | memset(&map_reg, 0, sizeof(map_reg)); | |
5642 | mark_btf_ld_reg(env, &map_reg, 0, PTR_TO_BTF_ID, btf_vmlinux, *map->ops->map_btf_id, 0); | |
5643 | ret = btf_struct_access(&env->log, &map_reg, off, size, atype, &btf_id, &flag); | |
41c48f3a AI |
5644 | if (ret < 0) |
5645 | return ret; | |
5646 | ||
5647 | if (value_regno >= 0) | |
c6f1bfe8 | 5648 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id, flag); |
41c48f3a | 5649 | |
9e15db66 AS |
5650 | return 0; |
5651 | } | |
5652 | ||
01f810ac AM |
5653 | /* Check that the stack access at the given offset is within bounds. The |
5654 | * maximum valid offset is -1. | |
5655 | * | |
5656 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
5657 | * -state->allocated_stack for reads. | |
5658 | */ | |
5659 | static int check_stack_slot_within_bounds(int off, | |
5660 | struct bpf_func_state *state, | |
5661 | enum bpf_access_type t) | |
5662 | { | |
5663 | int min_valid_off; | |
5664 | ||
5665 | if (t == BPF_WRITE) | |
5666 | min_valid_off = -MAX_BPF_STACK; | |
5667 | else | |
5668 | min_valid_off = -state->allocated_stack; | |
5669 | ||
5670 | if (off < min_valid_off || off > -1) | |
5671 | return -EACCES; | |
5672 | return 0; | |
5673 | } | |
5674 | ||
5675 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
5676 | * bounds. | |
5677 | * | |
5678 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
5679 | */ | |
5680 | static int check_stack_access_within_bounds( | |
5681 | struct bpf_verifier_env *env, | |
5682 | int regno, int off, int access_size, | |
61df10c7 | 5683 | enum bpf_access_src src, enum bpf_access_type type) |
01f810ac AM |
5684 | { |
5685 | struct bpf_reg_state *regs = cur_regs(env); | |
5686 | struct bpf_reg_state *reg = regs + regno; | |
5687 | struct bpf_func_state *state = func(env, reg); | |
5688 | int min_off, max_off; | |
5689 | int err; | |
5690 | char *err_extra; | |
5691 | ||
5692 | if (src == ACCESS_HELPER) | |
5693 | /* We don't know if helpers are reading or writing (or both). */ | |
5694 | err_extra = " indirect access to"; | |
5695 | else if (type == BPF_READ) | |
5696 | err_extra = " read from"; | |
5697 | else | |
5698 | err_extra = " write to"; | |
5699 | ||
5700 | if (tnum_is_const(reg->var_off)) { | |
5701 | min_off = reg->var_off.value + off; | |
5702 | if (access_size > 0) | |
5703 | max_off = min_off + access_size - 1; | |
5704 | else | |
5705 | max_off = min_off; | |
5706 | } else { | |
5707 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
5708 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
5709 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
5710 | err_extra, regno); | |
5711 | return -EACCES; | |
5712 | } | |
5713 | min_off = reg->smin_value + off; | |
5714 | if (access_size > 0) | |
5715 | max_off = reg->smax_value + off + access_size - 1; | |
5716 | else | |
5717 | max_off = min_off; | |
5718 | } | |
5719 | ||
5720 | err = check_stack_slot_within_bounds(min_off, state, type); | |
5721 | if (!err) | |
5722 | err = check_stack_slot_within_bounds(max_off, state, type); | |
5723 | ||
5724 | if (err) { | |
5725 | if (tnum_is_const(reg->var_off)) { | |
5726 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
5727 | err_extra, regno, off, access_size); | |
5728 | } else { | |
5729 | char tn_buf[48]; | |
5730 | ||
5731 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5732 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
5733 | err_extra, regno, tn_buf, access_size); | |
5734 | } | |
5735 | } | |
5736 | return err; | |
5737 | } | |
41c48f3a | 5738 | |
17a52670 AS |
5739 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
5740 | * if t==write, value_regno is a register which value is stored into memory | |
5741 | * if t==read, value_regno is a register which will receive the value from memory | |
5742 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
5743 | * if t==read && value_regno==-1, don't care what we read from memory | |
5744 | */ | |
ca369602 DB |
5745 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
5746 | int off, int bpf_size, enum bpf_access_type t, | |
5747 | int value_regno, bool strict_alignment_once) | |
17a52670 | 5748 | { |
638f5b90 AS |
5749 | struct bpf_reg_state *regs = cur_regs(env); |
5750 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 5751 | struct bpf_func_state *state; |
17a52670 AS |
5752 | int size, err = 0; |
5753 | ||
5754 | size = bpf_size_to_bytes(bpf_size); | |
5755 | if (size < 0) | |
5756 | return size; | |
5757 | ||
f1174f77 | 5758 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 5759 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
5760 | if (err) |
5761 | return err; | |
17a52670 | 5762 | |
f1174f77 EC |
5763 | /* for access checks, reg->off is just part of off */ |
5764 | off += reg->off; | |
5765 | ||
69c087ba YS |
5766 | if (reg->type == PTR_TO_MAP_KEY) { |
5767 | if (t == BPF_WRITE) { | |
5768 | verbose(env, "write to change key R%d not allowed\n", regno); | |
5769 | return -EACCES; | |
5770 | } | |
5771 | ||
5772 | err = check_mem_region_access(env, regno, off, size, | |
5773 | reg->map_ptr->key_size, false); | |
5774 | if (err) | |
5775 | return err; | |
5776 | if (value_regno >= 0) | |
5777 | mark_reg_unknown(env, regs, value_regno); | |
5778 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
aa3496ac | 5779 | struct btf_field *kptr_field = NULL; |
61df10c7 | 5780 | |
1be7f75d AS |
5781 | if (t == BPF_WRITE && value_regno >= 0 && |
5782 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5783 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
5784 | return -EACCES; |
5785 | } | |
591fe988 DB |
5786 | err = check_map_access_type(env, regno, off, size, t); |
5787 | if (err) | |
5788 | return err; | |
61df10c7 KKD |
5789 | err = check_map_access(env, regno, off, size, false, ACCESS_DIRECT); |
5790 | if (err) | |
5791 | return err; | |
5792 | if (tnum_is_const(reg->var_off)) | |
aa3496ac KKD |
5793 | kptr_field = btf_record_find(reg->map_ptr->record, |
5794 | off + reg->var_off.value, BPF_KPTR); | |
5795 | if (kptr_field) { | |
5796 | err = check_map_kptr_access(env, regno, value_regno, insn_idx, kptr_field); | |
61df10c7 | 5797 | } else if (t == BPF_READ && value_regno >= 0) { |
a23740ec AN |
5798 | struct bpf_map *map = reg->map_ptr; |
5799 | ||
5800 | /* if map is read-only, track its contents as scalars */ | |
5801 | if (tnum_is_const(reg->var_off) && | |
5802 | bpf_map_is_rdonly(map) && | |
5803 | map->ops->map_direct_value_addr) { | |
5804 | int map_off = off + reg->var_off.value; | |
5805 | u64 val = 0; | |
5806 | ||
5807 | err = bpf_map_direct_read(map, map_off, size, | |
5808 | &val); | |
5809 | if (err) | |
5810 | return err; | |
5811 | ||
5812 | regs[value_regno].type = SCALAR_VALUE; | |
5813 | __mark_reg_known(®s[value_regno], val); | |
5814 | } else { | |
5815 | mark_reg_unknown(env, regs, value_regno); | |
5816 | } | |
5817 | } | |
34d3a78c HL |
5818 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
5819 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
5820 | ||
5821 | if (type_may_be_null(reg->type)) { | |
5822 | verbose(env, "R%d invalid mem access '%s'\n", regno, | |
5823 | reg_type_str(env, reg->type)); | |
5824 | return -EACCES; | |
5825 | } | |
5826 | ||
5827 | if (t == BPF_WRITE && rdonly_mem) { | |
5828 | verbose(env, "R%d cannot write into %s\n", | |
5829 | regno, reg_type_str(env, reg->type)); | |
5830 | return -EACCES; | |
5831 | } | |
5832 | ||
457f4436 AN |
5833 | if (t == BPF_WRITE && value_regno >= 0 && |
5834 | is_pointer_value(env, value_regno)) { | |
5835 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
5836 | return -EACCES; | |
5837 | } | |
34d3a78c | 5838 | |
457f4436 AN |
5839 | err = check_mem_region_access(env, regno, off, size, |
5840 | reg->mem_size, false); | |
34d3a78c | 5841 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
457f4436 | 5842 | mark_reg_unknown(env, regs, value_regno); |
1a0dc1ac | 5843 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 5844 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 5845 | struct btf *btf = NULL; |
9e15db66 | 5846 | u32 btf_id = 0; |
19de99f7 | 5847 | |
1be7f75d AS |
5848 | if (t == BPF_WRITE && value_regno >= 0 && |
5849 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5850 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
5851 | return -EACCES; |
5852 | } | |
f1174f77 | 5853 | |
be80a1d3 | 5854 | err = check_ptr_off_reg(env, reg, regno); |
58990d1f DB |
5855 | if (err < 0) |
5856 | return err; | |
5857 | ||
c6f1bfe8 YS |
5858 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, |
5859 | &btf_id); | |
9e15db66 AS |
5860 | if (err) |
5861 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 5862 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 5863 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
5864 | * PTR_TO_PACKET[_META,_END]. In the latter |
5865 | * case, we know the offset is zero. | |
f1174f77 | 5866 | */ |
46f8bc92 | 5867 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 5868 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 5869 | } else { |
638f5b90 | 5870 | mark_reg_known_zero(env, regs, |
61bd5218 | 5871 | value_regno); |
c25b2ae1 | 5872 | if (type_may_be_null(reg_type)) |
46f8bc92 | 5873 | regs[value_regno].id = ++env->id_gen; |
5327ed3d JW |
5874 | /* A load of ctx field could have different |
5875 | * actual load size with the one encoded in the | |
5876 | * insn. When the dst is PTR, it is for sure not | |
5877 | * a sub-register. | |
5878 | */ | |
5879 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
c25b2ae1 | 5880 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 5881 | regs[value_regno].btf = btf; |
9e15db66 | 5882 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 5883 | } |
46f8bc92 | 5884 | } |
638f5b90 | 5885 | regs[value_regno].type = reg_type; |
969bf05e | 5886 | } |
17a52670 | 5887 | |
f1174f77 | 5888 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
5889 | /* Basic bounds checks. */ |
5890 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
5891 | if (err) |
5892 | return err; | |
8726679a | 5893 | |
f4d7e40a AS |
5894 | state = func(env, reg); |
5895 | err = update_stack_depth(env, state, off); | |
5896 | if (err) | |
5897 | return err; | |
8726679a | 5898 | |
01f810ac AM |
5899 | if (t == BPF_READ) |
5900 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 5901 | value_regno); |
01f810ac AM |
5902 | else |
5903 | err = check_stack_write(env, regno, off, size, | |
5904 | value_regno, insn_idx); | |
de8f3a83 | 5905 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 5906 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 5907 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
5908 | return -EACCES; |
5909 | } | |
4acf6c0b BB |
5910 | if (t == BPF_WRITE && value_regno >= 0 && |
5911 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
5912 | verbose(env, "R%d leaks addr into packet\n", |
5913 | value_regno); | |
4acf6c0b BB |
5914 | return -EACCES; |
5915 | } | |
9fd29c08 | 5916 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 5917 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 5918 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
5919 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
5920 | if (t == BPF_WRITE && value_regno >= 0 && | |
5921 | is_pointer_value(env, value_regno)) { | |
5922 | verbose(env, "R%d leaks addr into flow keys\n", | |
5923 | value_regno); | |
5924 | return -EACCES; | |
5925 | } | |
5926 | ||
5927 | err = check_flow_keys_access(env, off, size); | |
5928 | if (!err && t == BPF_READ && value_regno >= 0) | |
5929 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 5930 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 5931 | if (t == BPF_WRITE) { |
46f8bc92 | 5932 | verbose(env, "R%d cannot write into %s\n", |
c25b2ae1 | 5933 | regno, reg_type_str(env, reg->type)); |
c64b7983 JS |
5934 | return -EACCES; |
5935 | } | |
5f456649 | 5936 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
5937 | if (!err && value_regno >= 0) |
5938 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
5939 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
5940 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
5941 | if (!err && t == BPF_READ && value_regno >= 0) | |
5942 | mark_reg_unknown(env, regs, value_regno); | |
bff61f6f HL |
5943 | } else if (base_type(reg->type) == PTR_TO_BTF_ID && |
5944 | !type_may_be_null(reg->type)) { | |
9e15db66 AS |
5945 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
5946 | value_regno); | |
41c48f3a AI |
5947 | } else if (reg->type == CONST_PTR_TO_MAP) { |
5948 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
5949 | value_regno); | |
20b2aff4 HL |
5950 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
5951 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
20b2aff4 HL |
5952 | u32 *max_access; |
5953 | ||
5954 | if (rdonly_mem) { | |
5955 | if (t == BPF_WRITE) { | |
5956 | verbose(env, "R%d cannot write into %s\n", | |
5957 | regno, reg_type_str(env, reg->type)); | |
5958 | return -EACCES; | |
5959 | } | |
20b2aff4 HL |
5960 | max_access = &env->prog->aux->max_rdonly_access; |
5961 | } else { | |
20b2aff4 | 5962 | max_access = &env->prog->aux->max_rdwr_access; |
afbf21dc | 5963 | } |
20b2aff4 | 5964 | |
f6dfbe31 | 5965 | err = check_buffer_access(env, reg, regno, off, size, false, |
44e9a741 | 5966 | max_access); |
20b2aff4 HL |
5967 | |
5968 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) | |
afbf21dc | 5969 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 5970 | } else { |
61bd5218 | 5971 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
c25b2ae1 | 5972 | reg_type_str(env, reg->type)); |
17a52670 AS |
5973 | return -EACCES; |
5974 | } | |
969bf05e | 5975 | |
f1174f77 | 5976 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 5977 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 5978 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 5979 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 5980 | } |
17a52670 AS |
5981 | return err; |
5982 | } | |
5983 | ||
91c960b0 | 5984 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 5985 | { |
5ffa2550 | 5986 | int load_reg; |
17a52670 AS |
5987 | int err; |
5988 | ||
5ca419f2 BJ |
5989 | switch (insn->imm) { |
5990 | case BPF_ADD: | |
5991 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
5992 | case BPF_AND: |
5993 | case BPF_AND | BPF_FETCH: | |
5994 | case BPF_OR: | |
5995 | case BPF_OR | BPF_FETCH: | |
5996 | case BPF_XOR: | |
5997 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
5998 | case BPF_XCHG: |
5999 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
6000 | break; |
6001 | default: | |
91c960b0 BJ |
6002 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
6003 | return -EINVAL; | |
6004 | } | |
6005 | ||
6006 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
6007 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
6008 | return -EINVAL; |
6009 | } | |
6010 | ||
6011 | /* check src1 operand */ | |
dc503a8a | 6012 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6013 | if (err) |
6014 | return err; | |
6015 | ||
6016 | /* check src2 operand */ | |
dc503a8a | 6017 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6018 | if (err) |
6019 | return err; | |
6020 | ||
5ffa2550 BJ |
6021 | if (insn->imm == BPF_CMPXCHG) { |
6022 | /* Check comparison of R0 with memory location */ | |
a82fe085 DB |
6023 | const u32 aux_reg = BPF_REG_0; |
6024 | ||
6025 | err = check_reg_arg(env, aux_reg, SRC_OP); | |
5ffa2550 BJ |
6026 | if (err) |
6027 | return err; | |
a82fe085 DB |
6028 | |
6029 | if (is_pointer_value(env, aux_reg)) { | |
6030 | verbose(env, "R%d leaks addr into mem\n", aux_reg); | |
6031 | return -EACCES; | |
6032 | } | |
5ffa2550 BJ |
6033 | } |
6034 | ||
6bdf6abc | 6035 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 6036 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
6037 | return -EACCES; |
6038 | } | |
6039 | ||
ca369602 | 6040 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 6041 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
6042 | is_flow_key_reg(env, insn->dst_reg) || |
6043 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 6044 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f | 6045 | insn->dst_reg, |
c25b2ae1 | 6046 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
6047 | return -EACCES; |
6048 | } | |
6049 | ||
37086bfd BJ |
6050 | if (insn->imm & BPF_FETCH) { |
6051 | if (insn->imm == BPF_CMPXCHG) | |
6052 | load_reg = BPF_REG_0; | |
6053 | else | |
6054 | load_reg = insn->src_reg; | |
6055 | ||
6056 | /* check and record load of old value */ | |
6057 | err = check_reg_arg(env, load_reg, DST_OP); | |
6058 | if (err) | |
6059 | return err; | |
6060 | } else { | |
6061 | /* This instruction accesses a memory location but doesn't | |
6062 | * actually load it into a register. | |
6063 | */ | |
6064 | load_reg = -1; | |
6065 | } | |
6066 | ||
7d3baf0a DB |
6067 | /* Check whether we can read the memory, with second call for fetch |
6068 | * case to simulate the register fill. | |
6069 | */ | |
31fd8581 | 6070 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
7d3baf0a DB |
6071 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
6072 | if (!err && load_reg >= 0) | |
6073 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, | |
6074 | BPF_SIZE(insn->code), BPF_READ, load_reg, | |
6075 | true); | |
17a52670 AS |
6076 | if (err) |
6077 | return err; | |
6078 | ||
7d3baf0a | 6079 | /* Check whether we can write into the same memory. */ |
5ca419f2 BJ |
6080 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
6081 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
6082 | if (err) | |
6083 | return err; | |
6084 | ||
5ca419f2 | 6085 | return 0; |
17a52670 AS |
6086 | } |
6087 | ||
01f810ac AM |
6088 | /* When register 'regno' is used to read the stack (either directly or through |
6089 | * a helper function) make sure that it's within stack boundary and, depending | |
6090 | * on the access type, that all elements of the stack are initialized. | |
6091 | * | |
6092 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
6093 | * | |
6094 | * All registers that have been spilled on the stack in the slots within the | |
6095 | * read offsets are marked as read. | |
6096 | */ | |
6097 | static int check_stack_range_initialized( | |
6098 | struct bpf_verifier_env *env, int regno, int off, | |
6099 | int access_size, bool zero_size_allowed, | |
61df10c7 | 6100 | enum bpf_access_src type, struct bpf_call_arg_meta *meta) |
2011fccf AI |
6101 | { |
6102 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
6103 | struct bpf_func_state *state = func(env, reg); |
6104 | int err, min_off, max_off, i, j, slot, spi; | |
6105 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
6106 | enum bpf_access_type bounds_check_type; | |
6107 | /* Some accesses can write anything into the stack, others are | |
6108 | * read-only. | |
6109 | */ | |
6110 | bool clobber = false; | |
2011fccf | 6111 | |
01f810ac AM |
6112 | if (access_size == 0 && !zero_size_allowed) { |
6113 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
6114 | return -EACCES; |
6115 | } | |
2011fccf | 6116 | |
01f810ac AM |
6117 | if (type == ACCESS_HELPER) { |
6118 | /* The bounds checks for writes are more permissive than for | |
6119 | * reads. However, if raw_mode is not set, we'll do extra | |
6120 | * checks below. | |
6121 | */ | |
6122 | bounds_check_type = BPF_WRITE; | |
6123 | clobber = true; | |
6124 | } else { | |
6125 | bounds_check_type = BPF_READ; | |
6126 | } | |
6127 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
6128 | type, bounds_check_type); | |
6129 | if (err) | |
6130 | return err; | |
6131 | ||
17a52670 | 6132 | |
2011fccf | 6133 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 6134 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 6135 | } else { |
088ec26d AI |
6136 | /* Variable offset is prohibited for unprivileged mode for |
6137 | * simplicity since it requires corresponding support in | |
6138 | * Spectre masking for stack ALU. | |
6139 | * See also retrieve_ptr_limit(). | |
6140 | */ | |
2c78ee89 | 6141 | if (!env->bypass_spec_v1) { |
088ec26d | 6142 | char tn_buf[48]; |
f1174f77 | 6143 | |
088ec26d | 6144 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
6145 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
6146 | regno, err_extra, tn_buf); | |
088ec26d AI |
6147 | return -EACCES; |
6148 | } | |
f2bcd05e AI |
6149 | /* Only initialized buffer on stack is allowed to be accessed |
6150 | * with variable offset. With uninitialized buffer it's hard to | |
6151 | * guarantee that whole memory is marked as initialized on | |
6152 | * helper return since specific bounds are unknown what may | |
6153 | * cause uninitialized stack leaking. | |
6154 | */ | |
6155 | if (meta && meta->raw_mode) | |
6156 | meta = NULL; | |
6157 | ||
01f810ac AM |
6158 | min_off = reg->smin_value + off; |
6159 | max_off = reg->smax_value + off; | |
17a52670 AS |
6160 | } |
6161 | ||
435faee1 | 6162 | if (meta && meta->raw_mode) { |
ef8fc7a0 KKD |
6163 | /* Ensure we won't be overwriting dynptrs when simulating byte |
6164 | * by byte access in check_helper_call using meta.access_size. | |
6165 | * This would be a problem if we have a helper in the future | |
6166 | * which takes: | |
6167 | * | |
6168 | * helper(uninit_mem, len, dynptr) | |
6169 | * | |
6170 | * Now, uninint_mem may overlap with dynptr pointer. Hence, it | |
6171 | * may end up writing to dynptr itself when touching memory from | |
6172 | * arg 1. This can be relaxed on a case by case basis for known | |
6173 | * safe cases, but reject due to the possibilitiy of aliasing by | |
6174 | * default. | |
6175 | */ | |
6176 | for (i = min_off; i < max_off + access_size; i++) { | |
6177 | int stack_off = -i - 1; | |
6178 | ||
6179 | spi = __get_spi(i); | |
6180 | /* raw_mode may write past allocated_stack */ | |
6181 | if (state->allocated_stack <= stack_off) | |
6182 | continue; | |
6183 | if (state->stack[spi].slot_type[stack_off % BPF_REG_SIZE] == STACK_DYNPTR) { | |
6184 | verbose(env, "potential write to dynptr at off=%d disallowed\n", i); | |
6185 | return -EACCES; | |
6186 | } | |
6187 | } | |
435faee1 DB |
6188 | meta->access_size = access_size; |
6189 | meta->regno = regno; | |
6190 | return 0; | |
6191 | } | |
6192 | ||
2011fccf | 6193 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
6194 | u8 *stype; |
6195 | ||
2011fccf | 6196 | slot = -i - 1; |
638f5b90 | 6197 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
6198 | if (state->allocated_stack <= slot) |
6199 | goto err; | |
6200 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
6201 | if (*stype == STACK_MISC) | |
6202 | goto mark; | |
6715df8d EZ |
6203 | if ((*stype == STACK_ZERO) || |
6204 | (*stype == STACK_INVALID && env->allow_uninit_stack)) { | |
01f810ac AM |
6205 | if (clobber) { |
6206 | /* helper can write anything into the stack */ | |
6207 | *stype = STACK_MISC; | |
6208 | } | |
cc2b14d5 | 6209 | goto mark; |
17a52670 | 6210 | } |
1d68f22b | 6211 | |
27113c59 | 6212 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
6213 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
6214 | env->allow_ptr_leaks)) { | |
01f810ac AM |
6215 | if (clobber) { |
6216 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
6217 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 6218 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 6219 | } |
f7cf25b2 AS |
6220 | goto mark; |
6221 | } | |
6222 | ||
cc2b14d5 | 6223 | err: |
2011fccf | 6224 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
6225 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
6226 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
6227 | } else { |
6228 | char tn_buf[48]; | |
6229 | ||
6230 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
6231 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
6232 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 6233 | } |
cc2b14d5 AS |
6234 | return -EACCES; |
6235 | mark: | |
6236 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
6237 | * the whole slot to be marked as 'read' | |
6238 | */ | |
679c782d | 6239 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
6240 | state->stack[spi].spilled_ptr.parent, |
6241 | REG_LIVE_READ64); | |
261f4664 KKD |
6242 | /* We do not set REG_LIVE_WRITTEN for stack slot, as we can not |
6243 | * be sure that whether stack slot is written to or not. Hence, | |
6244 | * we must still conservatively propagate reads upwards even if | |
6245 | * helper may write to the entire memory range. | |
6246 | */ | |
17a52670 | 6247 | } |
2011fccf | 6248 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
6249 | } |
6250 | ||
06c1c049 GB |
6251 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
6252 | int access_size, bool zero_size_allowed, | |
6253 | struct bpf_call_arg_meta *meta) | |
6254 | { | |
638f5b90 | 6255 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
20b2aff4 | 6256 | u32 *max_access; |
06c1c049 | 6257 | |
20b2aff4 | 6258 | switch (base_type(reg->type)) { |
06c1c049 | 6259 | case PTR_TO_PACKET: |
de8f3a83 | 6260 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
6261 | return check_packet_access(env, regno, reg->off, access_size, |
6262 | zero_size_allowed); | |
69c087ba | 6263 | case PTR_TO_MAP_KEY: |
7b3552d3 KKD |
6264 | if (meta && meta->raw_mode) { |
6265 | verbose(env, "R%d cannot write into %s\n", regno, | |
6266 | reg_type_str(env, reg->type)); | |
6267 | return -EACCES; | |
6268 | } | |
69c087ba YS |
6269 | return check_mem_region_access(env, regno, reg->off, access_size, |
6270 | reg->map_ptr->key_size, false); | |
06c1c049 | 6271 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
6272 | if (check_map_access_type(env, regno, reg->off, access_size, |
6273 | meta && meta->raw_mode ? BPF_WRITE : | |
6274 | BPF_READ)) | |
6275 | return -EACCES; | |
9fd29c08 | 6276 | return check_map_access(env, regno, reg->off, access_size, |
61df10c7 | 6277 | zero_size_allowed, ACCESS_HELPER); |
457f4436 | 6278 | case PTR_TO_MEM: |
97e6d7da KKD |
6279 | if (type_is_rdonly_mem(reg->type)) { |
6280 | if (meta && meta->raw_mode) { | |
6281 | verbose(env, "R%d cannot write into %s\n", regno, | |
6282 | reg_type_str(env, reg->type)); | |
6283 | return -EACCES; | |
6284 | } | |
6285 | } | |
457f4436 AN |
6286 | return check_mem_region_access(env, regno, reg->off, |
6287 | access_size, reg->mem_size, | |
6288 | zero_size_allowed); | |
20b2aff4 HL |
6289 | case PTR_TO_BUF: |
6290 | if (type_is_rdonly_mem(reg->type)) { | |
97e6d7da KKD |
6291 | if (meta && meta->raw_mode) { |
6292 | verbose(env, "R%d cannot write into %s\n", regno, | |
6293 | reg_type_str(env, reg->type)); | |
20b2aff4 | 6294 | return -EACCES; |
97e6d7da | 6295 | } |
20b2aff4 | 6296 | |
20b2aff4 HL |
6297 | max_access = &env->prog->aux->max_rdonly_access; |
6298 | } else { | |
20b2aff4 HL |
6299 | max_access = &env->prog->aux->max_rdwr_access; |
6300 | } | |
afbf21dc YS |
6301 | return check_buffer_access(env, reg, regno, reg->off, |
6302 | access_size, zero_size_allowed, | |
44e9a741 | 6303 | max_access); |
0d004c02 | 6304 | case PTR_TO_STACK: |
01f810ac AM |
6305 | return check_stack_range_initialized( |
6306 | env, | |
6307 | regno, reg->off, access_size, | |
6308 | zero_size_allowed, ACCESS_HELPER, meta); | |
3e30be42 AS |
6309 | case PTR_TO_BTF_ID: |
6310 | return check_ptr_to_btf_access(env, regs, regno, reg->off, | |
6311 | access_size, BPF_READ, -1); | |
15baa55f BT |
6312 | case PTR_TO_CTX: |
6313 | /* in case the function doesn't know how to access the context, | |
6314 | * (because we are in a program of type SYSCALL for example), we | |
6315 | * can not statically check its size. | |
6316 | * Dynamically check it now. | |
6317 | */ | |
6318 | if (!env->ops->convert_ctx_access) { | |
6319 | enum bpf_access_type atype = meta && meta->raw_mode ? BPF_WRITE : BPF_READ; | |
6320 | int offset = access_size - 1; | |
6321 | ||
6322 | /* Allow zero-byte read from PTR_TO_CTX */ | |
6323 | if (access_size == 0) | |
6324 | return zero_size_allowed ? 0 : -EACCES; | |
6325 | ||
6326 | return check_mem_access(env, env->insn_idx, regno, offset, BPF_B, | |
6327 | atype, -1, false); | |
6328 | } | |
6329 | ||
6330 | fallthrough; | |
0d004c02 LB |
6331 | default: /* scalar_value or invalid ptr */ |
6332 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
6333 | if (zero_size_allowed && access_size == 0 && | |
6334 | register_is_null(reg)) | |
6335 | return 0; | |
6336 | ||
c25b2ae1 HL |
6337 | verbose(env, "R%d type=%s ", regno, |
6338 | reg_type_str(env, reg->type)); | |
6339 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); | |
0d004c02 | 6340 | return -EACCES; |
06c1c049 GB |
6341 | } |
6342 | } | |
6343 | ||
d583691c KKD |
6344 | static int check_mem_size_reg(struct bpf_verifier_env *env, |
6345 | struct bpf_reg_state *reg, u32 regno, | |
6346 | bool zero_size_allowed, | |
6347 | struct bpf_call_arg_meta *meta) | |
6348 | { | |
6349 | int err; | |
6350 | ||
6351 | /* This is used to refine r0 return value bounds for helpers | |
6352 | * that enforce this value as an upper bound on return values. | |
6353 | * See do_refine_retval_range() for helpers that can refine | |
6354 | * the return value. C type of helper is u32 so we pull register | |
6355 | * bound from umax_value however, if negative verifier errors | |
6356 | * out. Only upper bounds can be learned because retval is an | |
6357 | * int type and negative retvals are allowed. | |
6358 | */ | |
be77354a | 6359 | meta->msize_max_value = reg->umax_value; |
d583691c KKD |
6360 | |
6361 | /* The register is SCALAR_VALUE; the access check | |
6362 | * happens using its boundaries. | |
6363 | */ | |
6364 | if (!tnum_is_const(reg->var_off)) | |
6365 | /* For unprivileged variable accesses, disable raw | |
6366 | * mode so that the program is required to | |
6367 | * initialize all the memory that the helper could | |
6368 | * just partially fill up. | |
6369 | */ | |
6370 | meta = NULL; | |
6371 | ||
6372 | if (reg->smin_value < 0) { | |
6373 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", | |
6374 | regno); | |
6375 | return -EACCES; | |
6376 | } | |
6377 | ||
6378 | if (reg->umin_value == 0) { | |
6379 | err = check_helper_mem_access(env, regno - 1, 0, | |
6380 | zero_size_allowed, | |
6381 | meta); | |
6382 | if (err) | |
6383 | return err; | |
6384 | } | |
6385 | ||
6386 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { | |
6387 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
6388 | regno); | |
6389 | return -EACCES; | |
6390 | } | |
6391 | err = check_helper_mem_access(env, regno - 1, | |
6392 | reg->umax_value, | |
6393 | zero_size_allowed, meta); | |
6394 | if (!err) | |
6395 | err = mark_chain_precision(env, regno); | |
6396 | return err; | |
6397 | } | |
6398 | ||
e5069b9c DB |
6399 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
6400 | u32 regno, u32 mem_size) | |
6401 | { | |
be77354a KKD |
6402 | bool may_be_null = type_may_be_null(reg->type); |
6403 | struct bpf_reg_state saved_reg; | |
6404 | struct bpf_call_arg_meta meta; | |
6405 | int err; | |
6406 | ||
e5069b9c DB |
6407 | if (register_is_null(reg)) |
6408 | return 0; | |
6409 | ||
be77354a KKD |
6410 | memset(&meta, 0, sizeof(meta)); |
6411 | /* Assuming that the register contains a value check if the memory | |
6412 | * access is safe. Temporarily save and restore the register's state as | |
6413 | * the conversion shouldn't be visible to a caller. | |
6414 | */ | |
6415 | if (may_be_null) { | |
6416 | saved_reg = *reg; | |
e5069b9c | 6417 | mark_ptr_not_null_reg(reg); |
e5069b9c DB |
6418 | } |
6419 | ||
be77354a KKD |
6420 | err = check_helper_mem_access(env, regno, mem_size, true, &meta); |
6421 | /* Check access for BPF_WRITE */ | |
6422 | meta.raw_mode = true; | |
6423 | err = err ?: check_helper_mem_access(env, regno, mem_size, true, &meta); | |
6424 | ||
6425 | if (may_be_null) | |
6426 | *reg = saved_reg; | |
6427 | ||
6428 | return err; | |
e5069b9c DB |
6429 | } |
6430 | ||
00b85860 KKD |
6431 | static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
6432 | u32 regno) | |
d583691c KKD |
6433 | { |
6434 | struct bpf_reg_state *mem_reg = &cur_regs(env)[regno - 1]; | |
6435 | bool may_be_null = type_may_be_null(mem_reg->type); | |
6436 | struct bpf_reg_state saved_reg; | |
be77354a | 6437 | struct bpf_call_arg_meta meta; |
d583691c KKD |
6438 | int err; |
6439 | ||
6440 | WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); | |
6441 | ||
be77354a KKD |
6442 | memset(&meta, 0, sizeof(meta)); |
6443 | ||
d583691c KKD |
6444 | if (may_be_null) { |
6445 | saved_reg = *mem_reg; | |
6446 | mark_ptr_not_null_reg(mem_reg); | |
6447 | } | |
6448 | ||
be77354a KKD |
6449 | err = check_mem_size_reg(env, reg, regno, true, &meta); |
6450 | /* Check access for BPF_WRITE */ | |
6451 | meta.raw_mode = true; | |
6452 | err = err ?: check_mem_size_reg(env, reg, regno, true, &meta); | |
d583691c KKD |
6453 | |
6454 | if (may_be_null) | |
6455 | *mem_reg = saved_reg; | |
6456 | return err; | |
6457 | } | |
6458 | ||
d83525ca | 6459 | /* Implementation details: |
4e814da0 KKD |
6460 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL. |
6461 | * bpf_obj_new returns PTR_TO_BTF_ID | MEM_ALLOC | PTR_MAYBE_NULL. | |
d83525ca | 6462 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
4e814da0 KKD |
6463 | * Two separate bpf_obj_new will also have different reg->id. |
6464 | * For traditional PTR_TO_MAP_VALUE or PTR_TO_BTF_ID | MEM_ALLOC, the verifier | |
6465 | * clears reg->id after value_or_null->value transition, since the verifier only | |
6466 | * cares about the range of access to valid map value pointer and doesn't care | |
6467 | * about actual address of the map element. | |
d83525ca AS |
6468 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
6469 | * reg->id > 0 after value_or_null->value transition. By doing so | |
6470 | * two bpf_map_lookups will be considered two different pointers that | |
4e814da0 KKD |
6471 | * point to different bpf_spin_locks. Likewise for pointers to allocated objects |
6472 | * returned from bpf_obj_new. | |
d83525ca AS |
6473 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
6474 | * dead-locks. | |
6475 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
6476 | * reg_is_refcounted() logic. The verifier needs to remember only | |
6477 | * one spin_lock instead of array of acquired_refs. | |
d0d78c1d | 6478 | * cur_state->active_lock remembers which map value element or allocated |
4e814da0 | 6479 | * object got locked and clears it after bpf_spin_unlock. |
d83525ca AS |
6480 | */ |
6481 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
6482 | bool is_lock) | |
6483 | { | |
6484 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6485 | struct bpf_verifier_state *cur = env->cur_state; | |
6486 | bool is_const = tnum_is_const(reg->var_off); | |
d83525ca | 6487 | u64 val = reg->var_off.value; |
4e814da0 KKD |
6488 | struct bpf_map *map = NULL; |
6489 | struct btf *btf = NULL; | |
6490 | struct btf_record *rec; | |
d83525ca | 6491 | |
d83525ca AS |
6492 | if (!is_const) { |
6493 | verbose(env, | |
6494 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
6495 | regno); | |
6496 | return -EINVAL; | |
6497 | } | |
4e814da0 KKD |
6498 | if (reg->type == PTR_TO_MAP_VALUE) { |
6499 | map = reg->map_ptr; | |
6500 | if (!map->btf) { | |
6501 | verbose(env, | |
6502 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
6503 | map->name); | |
6504 | return -EINVAL; | |
6505 | } | |
6506 | } else { | |
6507 | btf = reg->btf; | |
d83525ca | 6508 | } |
4e814da0 KKD |
6509 | |
6510 | rec = reg_btf_record(reg); | |
6511 | if (!btf_record_has_field(rec, BPF_SPIN_LOCK)) { | |
6512 | verbose(env, "%s '%s' has no valid bpf_spin_lock\n", map ? "map" : "local", | |
6513 | map ? map->name : "kptr"); | |
d83525ca AS |
6514 | return -EINVAL; |
6515 | } | |
4e814da0 | 6516 | if (rec->spin_lock_off != val + reg->off) { |
db559117 | 6517 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock' that is at %d\n", |
4e814da0 | 6518 | val + reg->off, rec->spin_lock_off); |
d83525ca AS |
6519 | return -EINVAL; |
6520 | } | |
6521 | if (is_lock) { | |
d0d78c1d | 6522 | if (cur->active_lock.ptr) { |
d83525ca AS |
6523 | verbose(env, |
6524 | "Locking two bpf_spin_locks are not allowed\n"); | |
6525 | return -EINVAL; | |
6526 | } | |
d0d78c1d KKD |
6527 | if (map) |
6528 | cur->active_lock.ptr = map; | |
6529 | else | |
6530 | cur->active_lock.ptr = btf; | |
6531 | cur->active_lock.id = reg->id; | |
d83525ca | 6532 | } else { |
d0d78c1d KKD |
6533 | void *ptr; |
6534 | ||
6535 | if (map) | |
6536 | ptr = map; | |
6537 | else | |
6538 | ptr = btf; | |
6539 | ||
6540 | if (!cur->active_lock.ptr) { | |
d83525ca AS |
6541 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
6542 | return -EINVAL; | |
6543 | } | |
d0d78c1d KKD |
6544 | if (cur->active_lock.ptr != ptr || |
6545 | cur->active_lock.id != reg->id) { | |
d83525ca AS |
6546 | verbose(env, "bpf_spin_unlock of different lock\n"); |
6547 | return -EINVAL; | |
6548 | } | |
534e86bc | 6549 | |
6a3cd331 | 6550 | invalidate_non_owning_refs(env); |
534e86bc | 6551 | |
6a3cd331 DM |
6552 | cur->active_lock.ptr = NULL; |
6553 | cur->active_lock.id = 0; | |
d83525ca AS |
6554 | } |
6555 | return 0; | |
6556 | } | |
6557 | ||
b00628b1 AS |
6558 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
6559 | struct bpf_call_arg_meta *meta) | |
6560 | { | |
6561 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6562 | bool is_const = tnum_is_const(reg->var_off); | |
6563 | struct bpf_map *map = reg->map_ptr; | |
6564 | u64 val = reg->var_off.value; | |
6565 | ||
6566 | if (!is_const) { | |
6567 | verbose(env, | |
6568 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
6569 | regno); | |
6570 | return -EINVAL; | |
6571 | } | |
6572 | if (!map->btf) { | |
6573 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
6574 | map->name); | |
6575 | return -EINVAL; | |
6576 | } | |
db559117 KKD |
6577 | if (!btf_record_has_field(map->record, BPF_TIMER)) { |
6578 | verbose(env, "map '%s' has no valid bpf_timer\n", map->name); | |
68134668 AS |
6579 | return -EINVAL; |
6580 | } | |
db559117 | 6581 | if (map->record->timer_off != val + reg->off) { |
68134668 | 6582 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
db559117 | 6583 | val + reg->off, map->record->timer_off); |
b00628b1 AS |
6584 | return -EINVAL; |
6585 | } | |
6586 | if (meta->map_ptr) { | |
6587 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
6588 | return -EFAULT; | |
6589 | } | |
3e8ce298 | 6590 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
6591 | meta->map_ptr = map; |
6592 | return 0; | |
6593 | } | |
6594 | ||
c0a5a21c KKD |
6595 | static int process_kptr_func(struct bpf_verifier_env *env, int regno, |
6596 | struct bpf_call_arg_meta *meta) | |
6597 | { | |
6598 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
c0a5a21c | 6599 | struct bpf_map *map_ptr = reg->map_ptr; |
aa3496ac | 6600 | struct btf_field *kptr_field; |
c0a5a21c | 6601 | u32 kptr_off; |
c0a5a21c KKD |
6602 | |
6603 | if (!tnum_is_const(reg->var_off)) { | |
6604 | verbose(env, | |
6605 | "R%d doesn't have constant offset. kptr has to be at the constant offset\n", | |
6606 | regno); | |
6607 | return -EINVAL; | |
6608 | } | |
6609 | if (!map_ptr->btf) { | |
6610 | verbose(env, "map '%s' has to have BTF in order to use bpf_kptr_xchg\n", | |
6611 | map_ptr->name); | |
6612 | return -EINVAL; | |
6613 | } | |
aa3496ac KKD |
6614 | if (!btf_record_has_field(map_ptr->record, BPF_KPTR)) { |
6615 | verbose(env, "map '%s' has no valid kptr\n", map_ptr->name); | |
c0a5a21c KKD |
6616 | return -EINVAL; |
6617 | } | |
6618 | ||
6619 | meta->map_ptr = map_ptr; | |
6620 | kptr_off = reg->off + reg->var_off.value; | |
aa3496ac KKD |
6621 | kptr_field = btf_record_find(map_ptr->record, kptr_off, BPF_KPTR); |
6622 | if (!kptr_field) { | |
c0a5a21c KKD |
6623 | verbose(env, "off=%d doesn't point to kptr\n", kptr_off); |
6624 | return -EACCES; | |
6625 | } | |
aa3496ac | 6626 | if (kptr_field->type != BPF_KPTR_REF) { |
c0a5a21c KKD |
6627 | verbose(env, "off=%d kptr isn't referenced kptr\n", kptr_off); |
6628 | return -EACCES; | |
6629 | } | |
aa3496ac | 6630 | meta->kptr_field = kptr_field; |
c0a5a21c KKD |
6631 | return 0; |
6632 | } | |
6633 | ||
27060531 KKD |
6634 | /* There are two register types representing a bpf_dynptr, one is PTR_TO_STACK |
6635 | * which points to a stack slot, and the other is CONST_PTR_TO_DYNPTR. | |
6636 | * | |
6637 | * In both cases we deal with the first 8 bytes, but need to mark the next 8 | |
6638 | * bytes as STACK_DYNPTR in case of PTR_TO_STACK. In case of | |
6639 | * CONST_PTR_TO_DYNPTR, we are guaranteed to get the beginning of the object. | |
6640 | * | |
6641 | * Mutability of bpf_dynptr is at two levels, one is at the level of struct | |
6642 | * bpf_dynptr itself, i.e. whether the helper is receiving a pointer to struct | |
6643 | * bpf_dynptr or pointer to const struct bpf_dynptr. In the former case, it can | |
6644 | * mutate the view of the dynptr and also possibly destroy it. In the latter | |
6645 | * case, it cannot mutate the bpf_dynptr itself but it can still mutate the | |
6646 | * memory that dynptr points to. | |
6647 | * | |
6648 | * The verifier will keep track both levels of mutation (bpf_dynptr's in | |
6649 | * reg->type and the memory's in reg->dynptr.type), but there is no support for | |
6650 | * readonly dynptr view yet, hence only the first case is tracked and checked. | |
6651 | * | |
6652 | * This is consistent with how C applies the const modifier to a struct object, | |
6653 | * where the pointer itself inside bpf_dynptr becomes const but not what it | |
6654 | * points to. | |
6655 | * | |
6656 | * Helpers which do not mutate the bpf_dynptr set MEM_RDONLY in their argument | |
6657 | * type, and declare it as 'const struct bpf_dynptr *' in their prototype. | |
6658 | */ | |
1d18feb2 JK |
6659 | static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx, |
6660 | enum bpf_arg_type arg_type) | |
6b75bd3d KKD |
6661 | { |
6662 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
1d18feb2 | 6663 | int err; |
6b75bd3d | 6664 | |
27060531 KKD |
6665 | /* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an |
6666 | * ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*): | |
6667 | */ | |
6668 | if ((arg_type & (MEM_UNINIT | MEM_RDONLY)) == (MEM_UNINIT | MEM_RDONLY)) { | |
6669 | verbose(env, "verifier internal error: misconfigured dynptr helper type flags\n"); | |
6670 | return -EFAULT; | |
6671 | } | |
79168a66 | 6672 | |
27060531 KKD |
6673 | /* MEM_UNINIT - Points to memory that is an appropriate candidate for |
6674 | * constructing a mutable bpf_dynptr object. | |
6675 | * | |
6676 | * Currently, this is only possible with PTR_TO_STACK | |
6677 | * pointing to a region of at least 16 bytes which doesn't | |
6678 | * contain an existing bpf_dynptr. | |
6679 | * | |
6680 | * MEM_RDONLY - Points to a initialized bpf_dynptr that will not be | |
6681 | * mutated or destroyed. However, the memory it points to | |
6682 | * may be mutated. | |
6683 | * | |
6684 | * None - Points to a initialized dynptr that can be mutated and | |
6685 | * destroyed, including mutation of the memory it points | |
6686 | * to. | |
6b75bd3d | 6687 | */ |
6b75bd3d | 6688 | if (arg_type & MEM_UNINIT) { |
1d18feb2 JK |
6689 | int i; |
6690 | ||
7e0dac28 | 6691 | if (!is_dynptr_reg_valid_uninit(env, reg)) { |
6b75bd3d KKD |
6692 | verbose(env, "Dynptr has to be an uninitialized dynptr\n"); |
6693 | return -EINVAL; | |
6694 | } | |
6695 | ||
1d18feb2 JK |
6696 | /* we write BPF_DW bits (8 bytes) at a time */ |
6697 | for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) { | |
6698 | err = check_mem_access(env, insn_idx, regno, | |
6699 | i, BPF_DW, BPF_WRITE, -1, false); | |
6700 | if (err) | |
6701 | return err; | |
6b75bd3d KKD |
6702 | } |
6703 | ||
1d18feb2 | 6704 | err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx); |
27060531 KKD |
6705 | } else /* MEM_RDONLY and None case from above */ { |
6706 | /* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */ | |
6707 | if (reg->type == CONST_PTR_TO_DYNPTR && !(arg_type & MEM_RDONLY)) { | |
6708 | verbose(env, "cannot pass pointer to const bpf_dynptr, the helper mutates it\n"); | |
6709 | return -EINVAL; | |
6710 | } | |
6711 | ||
7e0dac28 | 6712 | if (!is_dynptr_reg_valid_init(env, reg)) { |
6b75bd3d KKD |
6713 | verbose(env, |
6714 | "Expected an initialized dynptr as arg #%d\n", | |
6715 | regno); | |
6716 | return -EINVAL; | |
6717 | } | |
6718 | ||
27060531 KKD |
6719 | /* Fold modifiers (in this case, MEM_RDONLY) when checking expected type */ |
6720 | if (!is_dynptr_type_expected(env, reg, arg_type & ~MEM_RDONLY)) { | |
6b75bd3d KKD |
6721 | verbose(env, |
6722 | "Expected a dynptr of type %s as arg #%d\n", | |
d54e0f6c | 6723 | dynptr_type_str(arg_to_dynptr_type(arg_type)), regno); |
6b75bd3d KKD |
6724 | return -EINVAL; |
6725 | } | |
d6fefa11 KKD |
6726 | |
6727 | err = mark_dynptr_read(env, reg); | |
6b75bd3d | 6728 | } |
1d18feb2 | 6729 | return err; |
6b75bd3d KKD |
6730 | } |
6731 | ||
06accc87 AN |
6732 | static u32 iter_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int spi) |
6733 | { | |
6734 | struct bpf_func_state *state = func(env, reg); | |
6735 | ||
6736 | return state->stack[spi].spilled_ptr.ref_obj_id; | |
6737 | } | |
6738 | ||
6739 | static bool is_iter_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6740 | { | |
6741 | return meta->kfunc_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY); | |
6742 | } | |
6743 | ||
6744 | static bool is_iter_new_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6745 | { | |
6746 | return meta->kfunc_flags & KF_ITER_NEW; | |
6747 | } | |
6748 | ||
6749 | static bool is_iter_next_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6750 | { | |
6751 | return meta->kfunc_flags & KF_ITER_NEXT; | |
6752 | } | |
6753 | ||
6754 | static bool is_iter_destroy_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6755 | { | |
6756 | return meta->kfunc_flags & KF_ITER_DESTROY; | |
6757 | } | |
6758 | ||
6759 | static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg) | |
6760 | { | |
6761 | /* btf_check_iter_kfuncs() guarantees that first argument of any iter | |
6762 | * kfunc is iter state pointer | |
6763 | */ | |
6764 | return arg == 0 && is_iter_kfunc(meta); | |
6765 | } | |
6766 | ||
6767 | static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idx, | |
6768 | struct bpf_kfunc_call_arg_meta *meta) | |
6769 | { | |
6770 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6771 | const struct btf_type *t; | |
6772 | const struct btf_param *arg; | |
6773 | int spi, err, i, nr_slots; | |
6774 | u32 btf_id; | |
6775 | ||
6776 | /* btf_check_iter_kfuncs() ensures we don't need to validate anything here */ | |
6777 | arg = &btf_params(meta->func_proto)[0]; | |
6778 | t = btf_type_skip_modifiers(meta->btf, arg->type, NULL); /* PTR */ | |
6779 | t = btf_type_skip_modifiers(meta->btf, t->type, &btf_id); /* STRUCT */ | |
6780 | nr_slots = t->size / BPF_REG_SIZE; | |
6781 | ||
06accc87 AN |
6782 | if (is_iter_new_kfunc(meta)) { |
6783 | /* bpf_iter_<type>_new() expects pointer to uninit iter state */ | |
6784 | if (!is_iter_reg_valid_uninit(env, reg, nr_slots)) { | |
6785 | verbose(env, "expected uninitialized iter_%s as arg #%d\n", | |
6786 | iter_type_str(meta->btf, btf_id), regno); | |
6787 | return -EINVAL; | |
6788 | } | |
6789 | ||
6790 | for (i = 0; i < nr_slots * 8; i += BPF_REG_SIZE) { | |
6791 | err = check_mem_access(env, insn_idx, regno, | |
6792 | i, BPF_DW, BPF_WRITE, -1, false); | |
6793 | if (err) | |
6794 | return err; | |
6795 | } | |
6796 | ||
6797 | err = mark_stack_slots_iter(env, reg, insn_idx, meta->btf, btf_id, nr_slots); | |
6798 | if (err) | |
6799 | return err; | |
6800 | } else { | |
6801 | /* iter_next() or iter_destroy() expect initialized iter state*/ | |
6802 | if (!is_iter_reg_valid_init(env, reg, meta->btf, btf_id, nr_slots)) { | |
6803 | verbose(env, "expected an initialized iter_%s as arg #%d\n", | |
6804 | iter_type_str(meta->btf, btf_id), regno); | |
6805 | return -EINVAL; | |
6806 | } | |
6807 | ||
b63cbc49 AN |
6808 | spi = iter_get_spi(env, reg, nr_slots); |
6809 | if (spi < 0) | |
6810 | return spi; | |
6811 | ||
06accc87 AN |
6812 | err = mark_iter_read(env, reg, spi, nr_slots); |
6813 | if (err) | |
6814 | return err; | |
6815 | ||
b63cbc49 AN |
6816 | /* remember meta->iter info for process_iter_next_call() */ |
6817 | meta->iter.spi = spi; | |
6818 | meta->iter.frameno = reg->frameno; | |
06accc87 AN |
6819 | meta->ref_obj_id = iter_ref_obj_id(env, reg, spi); |
6820 | ||
6821 | if (is_iter_destroy_kfunc(meta)) { | |
6822 | err = unmark_stack_slots_iter(env, reg, nr_slots); | |
6823 | if (err) | |
6824 | return err; | |
6825 | } | |
6826 | } | |
6827 | ||
6828 | return 0; | |
6829 | } | |
6830 | ||
6831 | /* process_iter_next_call() is called when verifier gets to iterator's next | |
6832 | * "method" (e.g., bpf_iter_num_next() for numbers iterator) call. We'll refer | |
6833 | * to it as just "iter_next()" in comments below. | |
6834 | * | |
6835 | * BPF verifier relies on a crucial contract for any iter_next() | |
6836 | * implementation: it should *eventually* return NULL, and once that happens | |
6837 | * it should keep returning NULL. That is, once iterator exhausts elements to | |
6838 | * iterate, it should never reset or spuriously return new elements. | |
6839 | * | |
6840 | * With the assumption of such contract, process_iter_next_call() simulates | |
6841 | * a fork in the verifier state to validate loop logic correctness and safety | |
6842 | * without having to simulate infinite amount of iterations. | |
6843 | * | |
6844 | * In current state, we first assume that iter_next() returned NULL and | |
6845 | * iterator state is set to DRAINED (BPF_ITER_STATE_DRAINED). In such | |
6846 | * conditions we should not form an infinite loop and should eventually reach | |
6847 | * exit. | |
6848 | * | |
6849 | * Besides that, we also fork current state and enqueue it for later | |
6850 | * verification. In a forked state we keep iterator state as ACTIVE | |
6851 | * (BPF_ITER_STATE_ACTIVE) and assume non-NULL return from iter_next(). We | |
6852 | * also bump iteration depth to prevent erroneous infinite loop detection | |
6853 | * later on (see iter_active_depths_differ() comment for details). In this | |
6854 | * state we assume that we'll eventually loop back to another iter_next() | |
6855 | * calls (it could be in exactly same location or in some other instruction, | |
6856 | * it doesn't matter, we don't make any unnecessary assumptions about this, | |
6857 | * everything revolves around iterator state in a stack slot, not which | |
6858 | * instruction is calling iter_next()). When that happens, we either will come | |
6859 | * to iter_next() with equivalent state and can conclude that next iteration | |
6860 | * will proceed in exactly the same way as we just verified, so it's safe to | |
6861 | * assume that loop converges. If not, we'll go on another iteration | |
6862 | * simulation with a different input state, until all possible starting states | |
6863 | * are validated or we reach maximum number of instructions limit. | |
6864 | * | |
6865 | * This way, we will either exhaustively discover all possible input states | |
6866 | * that iterator loop can start with and eventually will converge, or we'll | |
6867 | * effectively regress into bounded loop simulation logic and either reach | |
6868 | * maximum number of instructions if loop is not provably convergent, or there | |
6869 | * is some statically known limit on number of iterations (e.g., if there is | |
6870 | * an explicit `if n > 100 then break;` statement somewhere in the loop). | |
6871 | * | |
6872 | * One very subtle but very important aspect is that we *always* simulate NULL | |
6873 | * condition first (as the current state) before we simulate non-NULL case. | |
6874 | * This has to do with intricacies of scalar precision tracking. By simulating | |
6875 | * "exit condition" of iter_next() returning NULL first, we make sure all the | |
6876 | * relevant precision marks *that will be set **after** we exit iterator loop* | |
6877 | * are propagated backwards to common parent state of NULL and non-NULL | |
6878 | * branches. Thanks to that, state equivalence checks done later in forked | |
6879 | * state, when reaching iter_next() for ACTIVE iterator, can assume that | |
6880 | * precision marks are finalized and won't change. Because simulating another | |
6881 | * ACTIVE iterator iteration won't change them (because given same input | |
6882 | * states we'll end up with exactly same output states which we are currently | |
6883 | * comparing; and verification after the loop already propagated back what | |
6884 | * needs to be **additionally** tracked as precise). It's subtle, grok | |
6885 | * precision tracking for more intuitive understanding. | |
6886 | */ | |
6887 | static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, | |
6888 | struct bpf_kfunc_call_arg_meta *meta) | |
6889 | { | |
6890 | struct bpf_verifier_state *cur_st = env->cur_state, *queued_st; | |
6891 | struct bpf_func_state *cur_fr = cur_st->frame[cur_st->curframe], *queued_fr; | |
6892 | struct bpf_reg_state *cur_iter, *queued_iter; | |
6893 | int iter_frameno = meta->iter.frameno; | |
6894 | int iter_spi = meta->iter.spi; | |
6895 | ||
6896 | BTF_TYPE_EMIT(struct bpf_iter); | |
6897 | ||
6898 | cur_iter = &env->cur_state->frame[iter_frameno]->stack[iter_spi].spilled_ptr; | |
6899 | ||
6900 | if (cur_iter->iter.state != BPF_ITER_STATE_ACTIVE && | |
6901 | cur_iter->iter.state != BPF_ITER_STATE_DRAINED) { | |
6902 | verbose(env, "verifier internal error: unexpected iterator state %d (%s)\n", | |
6903 | cur_iter->iter.state, iter_state_str(cur_iter->iter.state)); | |
6904 | return -EFAULT; | |
6905 | } | |
6906 | ||
6907 | if (cur_iter->iter.state == BPF_ITER_STATE_ACTIVE) { | |
6908 | /* branch out active iter state */ | |
6909 | queued_st = push_stack(env, insn_idx + 1, insn_idx, false); | |
6910 | if (!queued_st) | |
6911 | return -ENOMEM; | |
6912 | ||
6913 | queued_iter = &queued_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr; | |
6914 | queued_iter->iter.state = BPF_ITER_STATE_ACTIVE; | |
6915 | queued_iter->iter.depth++; | |
6916 | ||
6917 | queued_fr = queued_st->frame[queued_st->curframe]; | |
6918 | mark_ptr_not_null_reg(&queued_fr->regs[BPF_REG_0]); | |
6919 | } | |
6920 | ||
6921 | /* switch to DRAINED state, but keep the depth unchanged */ | |
6922 | /* mark current iter state as drained and assume returned NULL */ | |
6923 | cur_iter->iter.state = BPF_ITER_STATE_DRAINED; | |
6924 | __mark_reg_const_zero(&cur_fr->regs[BPF_REG_0]); | |
6925 | ||
6926 | return 0; | |
6927 | } | |
6928 | ||
90133415 DB |
6929 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
6930 | { | |
6931 | return type == ARG_CONST_SIZE || | |
6932 | type == ARG_CONST_SIZE_OR_ZERO; | |
6933 | } | |
6934 | ||
8f14852e KKD |
6935 | static bool arg_type_is_release(enum bpf_arg_type type) |
6936 | { | |
6937 | return type & OBJ_RELEASE; | |
6938 | } | |
6939 | ||
97e03f52 JK |
6940 | static bool arg_type_is_dynptr(enum bpf_arg_type type) |
6941 | { | |
6942 | return base_type(type) == ARG_PTR_TO_DYNPTR; | |
6943 | } | |
6944 | ||
57c3bb72 AI |
6945 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
6946 | { | |
6947 | if (type == ARG_PTR_TO_INT) | |
6948 | return sizeof(u32); | |
6949 | else if (type == ARG_PTR_TO_LONG) | |
6950 | return sizeof(u64); | |
6951 | ||
6952 | return -EINVAL; | |
6953 | } | |
6954 | ||
912f442c LB |
6955 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
6956 | const struct bpf_call_arg_meta *meta, | |
6957 | enum bpf_arg_type *arg_type) | |
6958 | { | |
6959 | if (!meta->map_ptr) { | |
6960 | /* kernel subsystem misconfigured verifier */ | |
6961 | verbose(env, "invalid map_ptr to access map->type\n"); | |
6962 | return -EACCES; | |
6963 | } | |
6964 | ||
6965 | switch (meta->map_ptr->map_type) { | |
6966 | case BPF_MAP_TYPE_SOCKMAP: | |
6967 | case BPF_MAP_TYPE_SOCKHASH: | |
6968 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 6969 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
6970 | } else { |
6971 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
6972 | return -EINVAL; | |
6973 | } | |
6974 | break; | |
9330986c JK |
6975 | case BPF_MAP_TYPE_BLOOM_FILTER: |
6976 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
6977 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
6978 | break; | |
912f442c LB |
6979 | default: |
6980 | break; | |
6981 | } | |
6982 | return 0; | |
6983 | } | |
6984 | ||
f79e7ea5 LB |
6985 | struct bpf_reg_types { |
6986 | const enum bpf_reg_type types[10]; | |
1df8f55a | 6987 | u32 *btf_id; |
f79e7ea5 LB |
6988 | }; |
6989 | ||
f79e7ea5 LB |
6990 | static const struct bpf_reg_types sock_types = { |
6991 | .types = { | |
6992 | PTR_TO_SOCK_COMMON, | |
6993 | PTR_TO_SOCKET, | |
6994 | PTR_TO_TCP_SOCK, | |
6995 | PTR_TO_XDP_SOCK, | |
6996 | }, | |
6997 | }; | |
6998 | ||
49a2a4d4 | 6999 | #ifdef CONFIG_NET |
1df8f55a MKL |
7000 | static const struct bpf_reg_types btf_id_sock_common_types = { |
7001 | .types = { | |
7002 | PTR_TO_SOCK_COMMON, | |
7003 | PTR_TO_SOCKET, | |
7004 | PTR_TO_TCP_SOCK, | |
7005 | PTR_TO_XDP_SOCK, | |
7006 | PTR_TO_BTF_ID, | |
3f00c523 | 7007 | PTR_TO_BTF_ID | PTR_TRUSTED, |
1df8f55a MKL |
7008 | }, |
7009 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
7010 | }; | |
49a2a4d4 | 7011 | #endif |
1df8f55a | 7012 | |
f79e7ea5 LB |
7013 | static const struct bpf_reg_types mem_types = { |
7014 | .types = { | |
7015 | PTR_TO_STACK, | |
7016 | PTR_TO_PACKET, | |
7017 | PTR_TO_PACKET_META, | |
69c087ba | 7018 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
7019 | PTR_TO_MAP_VALUE, |
7020 | PTR_TO_MEM, | |
894f2a8b | 7021 | PTR_TO_MEM | MEM_RINGBUF, |
20b2aff4 | 7022 | PTR_TO_BUF, |
3e30be42 | 7023 | PTR_TO_BTF_ID | PTR_TRUSTED, |
f79e7ea5 LB |
7024 | }, |
7025 | }; | |
7026 | ||
7027 | static const struct bpf_reg_types int_ptr_types = { | |
7028 | .types = { | |
7029 | PTR_TO_STACK, | |
7030 | PTR_TO_PACKET, | |
7031 | PTR_TO_PACKET_META, | |
69c087ba | 7032 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
7033 | PTR_TO_MAP_VALUE, |
7034 | }, | |
7035 | }; | |
7036 | ||
4e814da0 KKD |
7037 | static const struct bpf_reg_types spin_lock_types = { |
7038 | .types = { | |
7039 | PTR_TO_MAP_VALUE, | |
7040 | PTR_TO_BTF_ID | MEM_ALLOC, | |
7041 | } | |
7042 | }; | |
7043 | ||
f79e7ea5 LB |
7044 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
7045 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
7046 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
894f2a8b | 7047 | static const struct bpf_reg_types ringbuf_mem_types = { .types = { PTR_TO_MEM | MEM_RINGBUF } }; |
f79e7ea5 | 7048 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
3f00c523 DV |
7049 | static const struct bpf_reg_types btf_ptr_types = { |
7050 | .types = { | |
7051 | PTR_TO_BTF_ID, | |
7052 | PTR_TO_BTF_ID | PTR_TRUSTED, | |
fca1aa75 | 7053 | PTR_TO_BTF_ID | MEM_RCU, |
3f00c523 DV |
7054 | }, |
7055 | }; | |
7056 | static const struct bpf_reg_types percpu_btf_ptr_types = { | |
7057 | .types = { | |
7058 | PTR_TO_BTF_ID | MEM_PERCPU, | |
7059 | PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED, | |
7060 | } | |
7061 | }; | |
69c087ba YS |
7062 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
7063 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 7064 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 7065 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
c0a5a21c | 7066 | static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
20571567 DV |
7067 | static const struct bpf_reg_types dynptr_types = { |
7068 | .types = { | |
7069 | PTR_TO_STACK, | |
27060531 | 7070 | CONST_PTR_TO_DYNPTR, |
20571567 DV |
7071 | } |
7072 | }; | |
f79e7ea5 | 7073 | |
0789e13b | 7074 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
d1673304 DM |
7075 | [ARG_PTR_TO_MAP_KEY] = &mem_types, |
7076 | [ARG_PTR_TO_MAP_VALUE] = &mem_types, | |
f79e7ea5 LB |
7077 | [ARG_CONST_SIZE] = &scalar_types, |
7078 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
7079 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
7080 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
7081 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 7082 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 7083 | #ifdef CONFIG_NET |
1df8f55a | 7084 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 7085 | #endif |
f79e7ea5 | 7086 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
7087 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
7088 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
7089 | [ARG_PTR_TO_MEM] = &mem_types, | |
894f2a8b | 7090 | [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, |
f79e7ea5 LB |
7091 | [ARG_PTR_TO_INT] = &int_ptr_types, |
7092 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 7093 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 7094 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 7095 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 7096 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 7097 | [ARG_PTR_TO_TIMER] = &timer_types, |
c0a5a21c | 7098 | [ARG_PTR_TO_KPTR] = &kptr_types, |
20571567 | 7099 | [ARG_PTR_TO_DYNPTR] = &dynptr_types, |
f79e7ea5 LB |
7100 | }; |
7101 | ||
7102 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 | 7103 | enum bpf_arg_type arg_type, |
c0a5a21c KKD |
7104 | const u32 *arg_btf_id, |
7105 | struct bpf_call_arg_meta *meta) | |
f79e7ea5 LB |
7106 | { |
7107 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
7108 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 7109 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
7110 | int i, j; |
7111 | ||
48946bd6 | 7112 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
7113 | if (!compatible) { |
7114 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
7115 | return -EFAULT; | |
7116 | } | |
7117 | ||
216e3cd2 HL |
7118 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
7119 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY | |
7120 | * | |
7121 | * Same for MAYBE_NULL: | |
7122 | * | |
7123 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, | |
7124 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL | |
7125 | * | |
7126 | * Therefore we fold these flags depending on the arg_type before comparison. | |
7127 | */ | |
7128 | if (arg_type & MEM_RDONLY) | |
7129 | type &= ~MEM_RDONLY; | |
7130 | if (arg_type & PTR_MAYBE_NULL) | |
7131 | type &= ~PTR_MAYBE_NULL; | |
7132 | ||
738c96d5 DM |
7133 | if (meta->func_id == BPF_FUNC_kptr_xchg && type & MEM_ALLOC) |
7134 | type &= ~MEM_ALLOC; | |
7135 | ||
f79e7ea5 LB |
7136 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
7137 | expected = compatible->types[i]; | |
7138 | if (expected == NOT_INIT) | |
7139 | break; | |
7140 | ||
7141 | if (type == expected) | |
a968d5e2 | 7142 | goto found; |
f79e7ea5 LB |
7143 | } |
7144 | ||
216e3cd2 | 7145 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
f79e7ea5 | 7146 | for (j = 0; j + 1 < i; j++) |
c25b2ae1 HL |
7147 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
7148 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); | |
f79e7ea5 | 7149 | return -EACCES; |
a968d5e2 MKL |
7150 | |
7151 | found: | |
da03e43a KKD |
7152 | if (base_type(reg->type) != PTR_TO_BTF_ID) |
7153 | return 0; | |
7154 | ||
3e30be42 AS |
7155 | if (compatible == &mem_types) { |
7156 | if (!(arg_type & MEM_RDONLY)) { | |
7157 | verbose(env, | |
7158 | "%s() may write into memory pointed by R%d type=%s\n", | |
7159 | func_id_name(meta->func_id), | |
7160 | regno, reg_type_str(env, reg->type)); | |
7161 | return -EACCES; | |
7162 | } | |
7163 | return 0; | |
7164 | } | |
7165 | ||
da03e43a KKD |
7166 | switch ((int)reg->type) { |
7167 | case PTR_TO_BTF_ID: | |
7168 | case PTR_TO_BTF_ID | PTR_TRUSTED: | |
7169 | case PTR_TO_BTF_ID | MEM_RCU: | |
7170 | { | |
2ab3b380 KKD |
7171 | /* For bpf_sk_release, it needs to match against first member |
7172 | * 'struct sock_common', hence make an exception for it. This | |
7173 | * allows bpf_sk_release to work for multiple socket types. | |
7174 | */ | |
7175 | bool strict_type_match = arg_type_is_release(arg_type) && | |
7176 | meta->func_id != BPF_FUNC_sk_release; | |
7177 | ||
1df8f55a MKL |
7178 | if (!arg_btf_id) { |
7179 | if (!compatible->btf_id) { | |
7180 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
7181 | return -EFAULT; | |
7182 | } | |
7183 | arg_btf_id = compatible->btf_id; | |
7184 | } | |
7185 | ||
c0a5a21c | 7186 | if (meta->func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac | 7187 | if (map_kptr_match_type(env, meta->kptr_field, reg, regno)) |
c0a5a21c | 7188 | return -EACCES; |
47e34cb7 DM |
7189 | } else { |
7190 | if (arg_btf_id == BPF_PTR_POISON) { | |
7191 | verbose(env, "verifier internal error:"); | |
7192 | verbose(env, "R%d has non-overwritten BPF_PTR_POISON type\n", | |
7193 | regno); | |
7194 | return -EACCES; | |
7195 | } | |
7196 | ||
7197 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
7198 | btf_vmlinux, *arg_btf_id, | |
7199 | strict_type_match)) { | |
7200 | verbose(env, "R%d is of type %s but %s is expected\n", | |
b32a5dae DM |
7201 | regno, btf_type_name(reg->btf, reg->btf_id), |
7202 | btf_type_name(btf_vmlinux, *arg_btf_id)); | |
47e34cb7 DM |
7203 | return -EACCES; |
7204 | } | |
a968d5e2 | 7205 | } |
da03e43a KKD |
7206 | break; |
7207 | } | |
e4c2acab DV |
7208 | case PTR_TO_BTF_ID | PTR_MAYBE_NULL: |
7209 | case PTR_TO_BTF_ID | PTR_MAYBE_NULL | MEM_RCU: | |
7210 | verbose(env, "Possibly NULL pointer passed to helper arg%d\n", regno); | |
7211 | return -EACCES; | |
da03e43a | 7212 | case PTR_TO_BTF_ID | MEM_ALLOC: |
738c96d5 DM |
7213 | if (meta->func_id != BPF_FUNC_spin_lock && meta->func_id != BPF_FUNC_spin_unlock && |
7214 | meta->func_id != BPF_FUNC_kptr_xchg) { | |
4e814da0 KKD |
7215 | verbose(env, "verifier internal error: unimplemented handling of MEM_ALLOC\n"); |
7216 | return -EFAULT; | |
7217 | } | |
da03e43a KKD |
7218 | /* Handled by helper specific checks */ |
7219 | break; | |
7220 | case PTR_TO_BTF_ID | MEM_PERCPU: | |
7221 | case PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED: | |
7222 | /* Handled by helper specific checks */ | |
7223 | break; | |
7224 | default: | |
7225 | verbose(env, "verifier internal error: invalid PTR_TO_BTF_ID register for type match\n"); | |
7226 | return -EFAULT; | |
a968d5e2 | 7227 | } |
a968d5e2 | 7228 | return 0; |
f79e7ea5 LB |
7229 | } |
7230 | ||
6a3cd331 DM |
7231 | static struct btf_field * |
7232 | reg_find_field_offset(const struct bpf_reg_state *reg, s32 off, u32 fields) | |
7233 | { | |
7234 | struct btf_field *field; | |
7235 | struct btf_record *rec; | |
7236 | ||
7237 | rec = reg_btf_record(reg); | |
7238 | if (!rec) | |
7239 | return NULL; | |
7240 | ||
7241 | field = btf_record_find(rec, off, fields); | |
7242 | if (!field) | |
7243 | return NULL; | |
7244 | ||
7245 | return field; | |
7246 | } | |
7247 | ||
25b35dd2 KKD |
7248 | int check_func_arg_reg_off(struct bpf_verifier_env *env, |
7249 | const struct bpf_reg_state *reg, int regno, | |
8f14852e | 7250 | enum bpf_arg_type arg_type) |
25b35dd2 | 7251 | { |
184c9bdb | 7252 | u32 type = reg->type; |
25b35dd2 | 7253 | |
184c9bdb KKD |
7254 | /* When referenced register is passed to release function, its fixed |
7255 | * offset must be 0. | |
7256 | * | |
7257 | * We will check arg_type_is_release reg has ref_obj_id when storing | |
7258 | * meta->release_regno. | |
7259 | */ | |
7260 | if (arg_type_is_release(arg_type)) { | |
7261 | /* ARG_PTR_TO_DYNPTR with OBJ_RELEASE is a bit special, as it | |
7262 | * may not directly point to the object being released, but to | |
7263 | * dynptr pointing to such object, which might be at some offset | |
7264 | * on the stack. In that case, we simply to fallback to the | |
7265 | * default handling. | |
7266 | */ | |
7267 | if (arg_type_is_dynptr(arg_type) && type == PTR_TO_STACK) | |
7268 | return 0; | |
6a3cd331 DM |
7269 | |
7270 | if ((type_is_ptr_alloc_obj(type) || type_is_non_owning_ref(type)) && reg->off) { | |
7271 | if (reg_find_field_offset(reg, reg->off, BPF_GRAPH_NODE_OR_ROOT)) | |
7272 | return __check_ptr_off_reg(env, reg, regno, true); | |
7273 | ||
7274 | verbose(env, "R%d must have zero offset when passed to release func\n", | |
7275 | regno); | |
7276 | verbose(env, "No graph node or root found at R%d type:%s off:%d\n", regno, | |
b32a5dae | 7277 | btf_type_name(reg->btf, reg->btf_id), reg->off); |
6a3cd331 DM |
7278 | return -EINVAL; |
7279 | } | |
7280 | ||
184c9bdb KKD |
7281 | /* Doing check_ptr_off_reg check for the offset will catch this |
7282 | * because fixed_off_ok is false, but checking here allows us | |
7283 | * to give the user a better error message. | |
7284 | */ | |
7285 | if (reg->off) { | |
7286 | verbose(env, "R%d must have zero offset when passed to release func or trusted arg to kfunc\n", | |
7287 | regno); | |
7288 | return -EINVAL; | |
7289 | } | |
7290 | return __check_ptr_off_reg(env, reg, regno, false); | |
7291 | } | |
7292 | ||
7293 | switch (type) { | |
7294 | /* Pointer types where both fixed and variable offset is explicitly allowed: */ | |
97e03f52 | 7295 | case PTR_TO_STACK: |
25b35dd2 KKD |
7296 | case PTR_TO_PACKET: |
7297 | case PTR_TO_PACKET_META: | |
7298 | case PTR_TO_MAP_KEY: | |
7299 | case PTR_TO_MAP_VALUE: | |
7300 | case PTR_TO_MEM: | |
7301 | case PTR_TO_MEM | MEM_RDONLY: | |
894f2a8b | 7302 | case PTR_TO_MEM | MEM_RINGBUF: |
25b35dd2 KKD |
7303 | case PTR_TO_BUF: |
7304 | case PTR_TO_BUF | MEM_RDONLY: | |
97e03f52 | 7305 | case SCALAR_VALUE: |
184c9bdb | 7306 | return 0; |
25b35dd2 KKD |
7307 | /* All the rest must be rejected, except PTR_TO_BTF_ID which allows |
7308 | * fixed offset. | |
7309 | */ | |
7310 | case PTR_TO_BTF_ID: | |
282de143 | 7311 | case PTR_TO_BTF_ID | MEM_ALLOC: |
3f00c523 | 7312 | case PTR_TO_BTF_ID | PTR_TRUSTED: |
fca1aa75 | 7313 | case PTR_TO_BTF_ID | MEM_RCU: |
6a3cd331 | 7314 | case PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF: |
24d5bb80 | 7315 | /* When referenced PTR_TO_BTF_ID is passed to release function, |
184c9bdb KKD |
7316 | * its fixed offset must be 0. In the other cases, fixed offset |
7317 | * can be non-zero. This was already checked above. So pass | |
7318 | * fixed_off_ok as true to allow fixed offset for all other | |
7319 | * cases. var_off always must be 0 for PTR_TO_BTF_ID, hence we | |
7320 | * still need to do checks instead of returning. | |
24d5bb80 | 7321 | */ |
184c9bdb | 7322 | return __check_ptr_off_reg(env, reg, regno, true); |
25b35dd2 | 7323 | default: |
184c9bdb | 7324 | return __check_ptr_off_reg(env, reg, regno, false); |
25b35dd2 | 7325 | } |
25b35dd2 KKD |
7326 | } |
7327 | ||
485ec51e JK |
7328 | static struct bpf_reg_state *get_dynptr_arg_reg(struct bpf_verifier_env *env, |
7329 | const struct bpf_func_proto *fn, | |
7330 | struct bpf_reg_state *regs) | |
7331 | { | |
7332 | struct bpf_reg_state *state = NULL; | |
7333 | int i; | |
7334 | ||
7335 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) | |
7336 | if (arg_type_is_dynptr(fn->arg_type[i])) { | |
7337 | if (state) { | |
7338 | verbose(env, "verifier internal error: multiple dynptr args\n"); | |
7339 | return NULL; | |
7340 | } | |
7341 | state = ®s[BPF_REG_1 + i]; | |
7342 | } | |
7343 | ||
7344 | if (!state) | |
7345 | verbose(env, "verifier internal error: no dynptr arg found\n"); | |
7346 | ||
7347 | return state; | |
7348 | } | |
7349 | ||
f8064ab9 | 7350 | static int dynptr_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
34d4ef57 JK |
7351 | { |
7352 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7353 | int spi; |
34d4ef57 | 7354 | |
27060531 | 7355 | if (reg->type == CONST_PTR_TO_DYNPTR) |
f8064ab9 KKD |
7356 | return reg->id; |
7357 | spi = dynptr_get_spi(env, reg); | |
7358 | if (spi < 0) | |
7359 | return spi; | |
7360 | return state->stack[spi].spilled_ptr.id; | |
7361 | } | |
7362 | ||
79168a66 | 7363 | static int dynptr_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
34d4ef57 JK |
7364 | { |
7365 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7366 | int spi; |
27060531 | 7367 | |
27060531 KKD |
7368 | if (reg->type == CONST_PTR_TO_DYNPTR) |
7369 | return reg->ref_obj_id; | |
79168a66 KKD |
7370 | spi = dynptr_get_spi(env, reg); |
7371 | if (spi < 0) | |
7372 | return spi; | |
27060531 | 7373 | return state->stack[spi].spilled_ptr.ref_obj_id; |
34d4ef57 JK |
7374 | } |
7375 | ||
b5964b96 JK |
7376 | static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env, |
7377 | struct bpf_reg_state *reg) | |
7378 | { | |
7379 | struct bpf_func_state *state = func(env, reg); | |
7380 | int spi; | |
7381 | ||
7382 | if (reg->type == CONST_PTR_TO_DYNPTR) | |
7383 | return reg->dynptr.type; | |
7384 | ||
7385 | spi = __get_spi(reg->off); | |
7386 | if (spi < 0) { | |
7387 | verbose(env, "verifier internal error: invalid spi when querying dynptr type\n"); | |
7388 | return BPF_DYNPTR_TYPE_INVALID; | |
7389 | } | |
7390 | ||
7391 | return state->stack[spi].spilled_ptr.dynptr.type; | |
7392 | } | |
7393 | ||
af7ec138 YS |
7394 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
7395 | struct bpf_call_arg_meta *meta, | |
1d18feb2 JK |
7396 | const struct bpf_func_proto *fn, |
7397 | int insn_idx) | |
17a52670 | 7398 | { |
af7ec138 | 7399 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 7400 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 7401 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 7402 | enum bpf_reg_type type = reg->type; |
508362ac | 7403 | u32 *arg_btf_id = NULL; |
17a52670 AS |
7404 | int err = 0; |
7405 | ||
80f1d68c | 7406 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
7407 | return 0; |
7408 | ||
dc503a8a EC |
7409 | err = check_reg_arg(env, regno, SRC_OP); |
7410 | if (err) | |
7411 | return err; | |
17a52670 | 7412 | |
1be7f75d AS |
7413 | if (arg_type == ARG_ANYTHING) { |
7414 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
7415 | verbose(env, "R%d leaks addr into helper function\n", |
7416 | regno); | |
1be7f75d AS |
7417 | return -EACCES; |
7418 | } | |
80f1d68c | 7419 | return 0; |
1be7f75d | 7420 | } |
80f1d68c | 7421 | |
de8f3a83 | 7422 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 7423 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 7424 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
7425 | return -EACCES; |
7426 | } | |
7427 | ||
16d1e00c | 7428 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE) { |
912f442c LB |
7429 | err = resolve_map_arg_type(env, meta, &arg_type); |
7430 | if (err) | |
7431 | return err; | |
7432 | } | |
7433 | ||
48946bd6 | 7434 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
7435 | /* A NULL register has a SCALAR_VALUE type, so skip |
7436 | * type checking. | |
7437 | */ | |
7438 | goto skip_type_check; | |
7439 | ||
508362ac | 7440 | /* arg_btf_id and arg_size are in a union. */ |
4e814da0 KKD |
7441 | if (base_type(arg_type) == ARG_PTR_TO_BTF_ID || |
7442 | base_type(arg_type) == ARG_PTR_TO_SPIN_LOCK) | |
508362ac MM |
7443 | arg_btf_id = fn->arg_btf_id[arg]; |
7444 | ||
7445 | err = check_reg_type(env, regno, arg_type, arg_btf_id, meta); | |
f79e7ea5 LB |
7446 | if (err) |
7447 | return err; | |
7448 | ||
8f14852e | 7449 | err = check_func_arg_reg_off(env, reg, regno, arg_type); |
25b35dd2 KKD |
7450 | if (err) |
7451 | return err; | |
d7b9454a | 7452 | |
fd1b0d60 | 7453 | skip_type_check: |
8f14852e | 7454 | if (arg_type_is_release(arg_type)) { |
bc34dee6 JK |
7455 | if (arg_type_is_dynptr(arg_type)) { |
7456 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7457 | int spi; |
bc34dee6 | 7458 | |
27060531 KKD |
7459 | /* Only dynptr created on stack can be released, thus |
7460 | * the get_spi and stack state checks for spilled_ptr | |
7461 | * should only be done before process_dynptr_func for | |
7462 | * PTR_TO_STACK. | |
7463 | */ | |
7464 | if (reg->type == PTR_TO_STACK) { | |
79168a66 | 7465 | spi = dynptr_get_spi(env, reg); |
f5b625e5 | 7466 | if (spi < 0 || !state->stack[spi].spilled_ptr.ref_obj_id) { |
27060531 KKD |
7467 | verbose(env, "arg %d is an unacquired reference\n", regno); |
7468 | return -EINVAL; | |
7469 | } | |
7470 | } else { | |
7471 | verbose(env, "cannot release unowned const bpf_dynptr\n"); | |
bc34dee6 JK |
7472 | return -EINVAL; |
7473 | } | |
7474 | } else if (!reg->ref_obj_id && !register_is_null(reg)) { | |
8f14852e KKD |
7475 | verbose(env, "R%d must be referenced when passed to release function\n", |
7476 | regno); | |
7477 | return -EINVAL; | |
7478 | } | |
7479 | if (meta->release_regno) { | |
7480 | verbose(env, "verifier internal error: more than one release argument\n"); | |
7481 | return -EFAULT; | |
7482 | } | |
7483 | meta->release_regno = regno; | |
7484 | } | |
7485 | ||
02f7c958 | 7486 | if (reg->ref_obj_id) { |
457f4436 AN |
7487 | if (meta->ref_obj_id) { |
7488 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
7489 | regno, reg->ref_obj_id, | |
7490 | meta->ref_obj_id); | |
7491 | return -EFAULT; | |
7492 | } | |
7493 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
7494 | } |
7495 | ||
8ab4cdcf JK |
7496 | switch (base_type(arg_type)) { |
7497 | case ARG_CONST_MAP_PTR: | |
17a52670 | 7498 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
3e8ce298 AS |
7499 | if (meta->map_ptr) { |
7500 | /* Use map_uid (which is unique id of inner map) to reject: | |
7501 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
7502 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
7503 | * if (inner_map1 && inner_map2) { | |
7504 | * timer = bpf_map_lookup_elem(inner_map1); | |
7505 | * if (timer) | |
7506 | * // mismatch would have been allowed | |
7507 | * bpf_timer_init(timer, inner_map2); | |
7508 | * } | |
7509 | * | |
7510 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
7511 | */ | |
7512 | if (meta->map_ptr != reg->map_ptr || | |
7513 | meta->map_uid != reg->map_uid) { | |
7514 | verbose(env, | |
7515 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
7516 | meta->map_uid, reg->map_uid); | |
7517 | return -EINVAL; | |
7518 | } | |
b00628b1 | 7519 | } |
33ff9823 | 7520 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 7521 | meta->map_uid = reg->map_uid; |
8ab4cdcf JK |
7522 | break; |
7523 | case ARG_PTR_TO_MAP_KEY: | |
17a52670 AS |
7524 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
7525 | * check that [key, key + map->key_size) are within | |
7526 | * stack limits and initialized | |
7527 | */ | |
33ff9823 | 7528 | if (!meta->map_ptr) { |
17a52670 AS |
7529 | /* in function declaration map_ptr must come before |
7530 | * map_key, so that it's verified and known before | |
7531 | * we have to check map_key here. Otherwise it means | |
7532 | * that kernel subsystem misconfigured verifier | |
7533 | */ | |
61bd5218 | 7534 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
7535 | return -EACCES; |
7536 | } | |
d71962f3 PC |
7537 | err = check_helper_mem_access(env, regno, |
7538 | meta->map_ptr->key_size, false, | |
7539 | NULL); | |
8ab4cdcf JK |
7540 | break; |
7541 | case ARG_PTR_TO_MAP_VALUE: | |
48946bd6 HL |
7542 | if (type_may_be_null(arg_type) && register_is_null(reg)) |
7543 | return 0; | |
7544 | ||
17a52670 AS |
7545 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
7546 | * check [value, value + map->value_size) validity | |
7547 | */ | |
33ff9823 | 7548 | if (!meta->map_ptr) { |
17a52670 | 7549 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 7550 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
7551 | return -EACCES; |
7552 | } | |
16d1e00c | 7553 | meta->raw_mode = arg_type & MEM_UNINIT; |
d71962f3 PC |
7554 | err = check_helper_mem_access(env, regno, |
7555 | meta->map_ptr->value_size, false, | |
2ea864c5 | 7556 | meta); |
8ab4cdcf JK |
7557 | break; |
7558 | case ARG_PTR_TO_PERCPU_BTF_ID: | |
eaa6bcb7 HL |
7559 | if (!reg->btf_id) { |
7560 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
7561 | return -EACCES; | |
7562 | } | |
22dc4a0f | 7563 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 7564 | meta->ret_btf_id = reg->btf_id; |
8ab4cdcf JK |
7565 | break; |
7566 | case ARG_PTR_TO_SPIN_LOCK: | |
5d92ddc3 DM |
7567 | if (in_rbtree_lock_required_cb(env)) { |
7568 | verbose(env, "can't spin_{lock,unlock} in rbtree cb\n"); | |
7569 | return -EACCES; | |
7570 | } | |
c18f0b6a | 7571 | if (meta->func_id == BPF_FUNC_spin_lock) { |
ac50fe51 KKD |
7572 | err = process_spin_lock(env, regno, true); |
7573 | if (err) | |
7574 | return err; | |
c18f0b6a | 7575 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
ac50fe51 KKD |
7576 | err = process_spin_lock(env, regno, false); |
7577 | if (err) | |
7578 | return err; | |
c18f0b6a LB |
7579 | } else { |
7580 | verbose(env, "verifier internal error\n"); | |
7581 | return -EFAULT; | |
7582 | } | |
8ab4cdcf JK |
7583 | break; |
7584 | case ARG_PTR_TO_TIMER: | |
ac50fe51 KKD |
7585 | err = process_timer_func(env, regno, meta); |
7586 | if (err) | |
7587 | return err; | |
8ab4cdcf JK |
7588 | break; |
7589 | case ARG_PTR_TO_FUNC: | |
69c087ba | 7590 | meta->subprogno = reg->subprogno; |
8ab4cdcf JK |
7591 | break; |
7592 | case ARG_PTR_TO_MEM: | |
a2bbe7cc LB |
7593 | /* The access to this pointer is only checked when we hit the |
7594 | * next is_mem_size argument below. | |
7595 | */ | |
16d1e00c | 7596 | meta->raw_mode = arg_type & MEM_UNINIT; |
508362ac MM |
7597 | if (arg_type & MEM_FIXED_SIZE) { |
7598 | err = check_helper_mem_access(env, regno, | |
7599 | fn->arg_size[arg], false, | |
7600 | meta); | |
7601 | } | |
8ab4cdcf JK |
7602 | break; |
7603 | case ARG_CONST_SIZE: | |
7604 | err = check_mem_size_reg(env, reg, regno, false, meta); | |
7605 | break; | |
7606 | case ARG_CONST_SIZE_OR_ZERO: | |
7607 | err = check_mem_size_reg(env, reg, regno, true, meta); | |
7608 | break; | |
7609 | case ARG_PTR_TO_DYNPTR: | |
1d18feb2 | 7610 | err = process_dynptr_func(env, regno, insn_idx, arg_type); |
ac50fe51 KKD |
7611 | if (err) |
7612 | return err; | |
8ab4cdcf JK |
7613 | break; |
7614 | case ARG_CONST_ALLOC_SIZE_OR_ZERO: | |
457f4436 | 7615 | if (!tnum_is_const(reg->var_off)) { |
28a8add6 | 7616 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
7617 | regno); |
7618 | return -EACCES; | |
7619 | } | |
7620 | meta->mem_size = reg->var_off.value; | |
2fc31465 KKD |
7621 | err = mark_chain_precision(env, regno); |
7622 | if (err) | |
7623 | return err; | |
8ab4cdcf JK |
7624 | break; |
7625 | case ARG_PTR_TO_INT: | |
7626 | case ARG_PTR_TO_LONG: | |
7627 | { | |
57c3bb72 AI |
7628 | int size = int_ptr_type_to_size(arg_type); |
7629 | ||
7630 | err = check_helper_mem_access(env, regno, size, false, meta); | |
7631 | if (err) | |
7632 | return err; | |
7633 | err = check_ptr_alignment(env, reg, 0, size, true); | |
8ab4cdcf JK |
7634 | break; |
7635 | } | |
7636 | case ARG_PTR_TO_CONST_STR: | |
7637 | { | |
fff13c4b FR |
7638 | struct bpf_map *map = reg->map_ptr; |
7639 | int map_off; | |
7640 | u64 map_addr; | |
7641 | char *str_ptr; | |
7642 | ||
a8fad73e | 7643 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
7644 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
7645 | return -EACCES; | |
7646 | } | |
7647 | ||
7648 | if (!tnum_is_const(reg->var_off)) { | |
7649 | verbose(env, "R%d is not a constant address'\n", regno); | |
7650 | return -EACCES; | |
7651 | } | |
7652 | ||
7653 | if (!map->ops->map_direct_value_addr) { | |
7654 | verbose(env, "no direct value access support for this map type\n"); | |
7655 | return -EACCES; | |
7656 | } | |
7657 | ||
7658 | err = check_map_access(env, regno, reg->off, | |
61df10c7 KKD |
7659 | map->value_size - reg->off, false, |
7660 | ACCESS_HELPER); | |
fff13c4b FR |
7661 | if (err) |
7662 | return err; | |
7663 | ||
7664 | map_off = reg->off + reg->var_off.value; | |
7665 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
7666 | if (err) { | |
7667 | verbose(env, "direct value access on string failed\n"); | |
7668 | return err; | |
7669 | } | |
7670 | ||
7671 | str_ptr = (char *)(long)(map_addr); | |
7672 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
7673 | verbose(env, "string is not zero-terminated\n"); | |
7674 | return -EINVAL; | |
7675 | } | |
8ab4cdcf JK |
7676 | break; |
7677 | } | |
7678 | case ARG_PTR_TO_KPTR: | |
ac50fe51 KKD |
7679 | err = process_kptr_func(env, regno, meta); |
7680 | if (err) | |
7681 | return err; | |
8ab4cdcf | 7682 | break; |
17a52670 AS |
7683 | } |
7684 | ||
7685 | return err; | |
7686 | } | |
7687 | ||
0126240f LB |
7688 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
7689 | { | |
7690 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 7691 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
7692 | |
7693 | if (func_id != BPF_FUNC_map_update_elem) | |
7694 | return false; | |
7695 | ||
7696 | /* It's not possible to get access to a locked struct sock in these | |
7697 | * contexts, so updating is safe. | |
7698 | */ | |
7699 | switch (type) { | |
7700 | case BPF_PROG_TYPE_TRACING: | |
7701 | if (eatype == BPF_TRACE_ITER) | |
7702 | return true; | |
7703 | break; | |
7704 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
7705 | case BPF_PROG_TYPE_SCHED_CLS: | |
7706 | case BPF_PROG_TYPE_SCHED_ACT: | |
7707 | case BPF_PROG_TYPE_XDP: | |
7708 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
7709 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
7710 | case BPF_PROG_TYPE_SK_LOOKUP: | |
7711 | return true; | |
7712 | default: | |
7713 | break; | |
7714 | } | |
7715 | ||
7716 | verbose(env, "cannot update sockmap in this context\n"); | |
7717 | return false; | |
7718 | } | |
7719 | ||
e411901c MF |
7720 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
7721 | { | |
95acd881 TA |
7722 | return env->prog->jit_requested && |
7723 | bpf_jit_supports_subprog_tailcalls(); | |
e411901c MF |
7724 | } |
7725 | ||
61bd5218 JK |
7726 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
7727 | struct bpf_map *map, int func_id) | |
35578d79 | 7728 | { |
35578d79 KX |
7729 | if (!map) |
7730 | return 0; | |
7731 | ||
6aff67c8 AS |
7732 | /* We need a two way check, first is from map perspective ... */ |
7733 | switch (map->map_type) { | |
7734 | case BPF_MAP_TYPE_PROG_ARRAY: | |
7735 | if (func_id != BPF_FUNC_tail_call) | |
7736 | goto error; | |
7737 | break; | |
7738 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
7739 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 7740 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 7741 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
7742 | func_id != BPF_FUNC_perf_event_read_value && |
7743 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
7744 | goto error; |
7745 | break; | |
457f4436 AN |
7746 | case BPF_MAP_TYPE_RINGBUF: |
7747 | if (func_id != BPF_FUNC_ringbuf_output && | |
7748 | func_id != BPF_FUNC_ringbuf_reserve && | |
bc34dee6 JK |
7749 | func_id != BPF_FUNC_ringbuf_query && |
7750 | func_id != BPF_FUNC_ringbuf_reserve_dynptr && | |
7751 | func_id != BPF_FUNC_ringbuf_submit_dynptr && | |
7752 | func_id != BPF_FUNC_ringbuf_discard_dynptr) | |
457f4436 AN |
7753 | goto error; |
7754 | break; | |
583c1f42 | 7755 | case BPF_MAP_TYPE_USER_RINGBUF: |
20571567 DV |
7756 | if (func_id != BPF_FUNC_user_ringbuf_drain) |
7757 | goto error; | |
7758 | break; | |
6aff67c8 AS |
7759 | case BPF_MAP_TYPE_STACK_TRACE: |
7760 | if (func_id != BPF_FUNC_get_stackid) | |
7761 | goto error; | |
7762 | break; | |
4ed8ec52 | 7763 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 7764 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 7765 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
7766 | goto error; |
7767 | break; | |
cd339431 | 7768 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 7769 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
7770 | if (func_id != BPF_FUNC_get_local_storage) |
7771 | goto error; | |
7772 | break; | |
546ac1ff | 7773 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 7774 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
7775 | if (func_id != BPF_FUNC_redirect_map && |
7776 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
7777 | goto error; |
7778 | break; | |
fbfc504a BT |
7779 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
7780 | * appear. | |
7781 | */ | |
6710e112 JDB |
7782 | case BPF_MAP_TYPE_CPUMAP: |
7783 | if (func_id != BPF_FUNC_redirect_map) | |
7784 | goto error; | |
7785 | break; | |
fada7fdc JL |
7786 | case BPF_MAP_TYPE_XSKMAP: |
7787 | if (func_id != BPF_FUNC_redirect_map && | |
7788 | func_id != BPF_FUNC_map_lookup_elem) | |
7789 | goto error; | |
7790 | break; | |
56f668df | 7791 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 7792 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
7793 | if (func_id != BPF_FUNC_map_lookup_elem) |
7794 | goto error; | |
16a43625 | 7795 | break; |
174a79ff JF |
7796 | case BPF_MAP_TYPE_SOCKMAP: |
7797 | if (func_id != BPF_FUNC_sk_redirect_map && | |
7798 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 7799 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 7800 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 7801 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
7802 | func_id != BPF_FUNC_map_lookup_elem && |
7803 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
7804 | goto error; |
7805 | break; | |
81110384 JF |
7806 | case BPF_MAP_TYPE_SOCKHASH: |
7807 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
7808 | func_id != BPF_FUNC_sock_hash_update && | |
7809 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 7810 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 7811 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
7812 | func_id != BPF_FUNC_map_lookup_elem && |
7813 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
7814 | goto error; |
7815 | break; | |
2dbb9b9e MKL |
7816 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
7817 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
7818 | goto error; | |
7819 | break; | |
f1a2e44a MV |
7820 | case BPF_MAP_TYPE_QUEUE: |
7821 | case BPF_MAP_TYPE_STACK: | |
7822 | if (func_id != BPF_FUNC_map_peek_elem && | |
7823 | func_id != BPF_FUNC_map_pop_elem && | |
7824 | func_id != BPF_FUNC_map_push_elem) | |
7825 | goto error; | |
7826 | break; | |
6ac99e8f MKL |
7827 | case BPF_MAP_TYPE_SK_STORAGE: |
7828 | if (func_id != BPF_FUNC_sk_storage_get && | |
9db44fdd KKD |
7829 | func_id != BPF_FUNC_sk_storage_delete && |
7830 | func_id != BPF_FUNC_kptr_xchg) | |
6ac99e8f MKL |
7831 | goto error; |
7832 | break; | |
8ea63684 KS |
7833 | case BPF_MAP_TYPE_INODE_STORAGE: |
7834 | if (func_id != BPF_FUNC_inode_storage_get && | |
9db44fdd KKD |
7835 | func_id != BPF_FUNC_inode_storage_delete && |
7836 | func_id != BPF_FUNC_kptr_xchg) | |
8ea63684 KS |
7837 | goto error; |
7838 | break; | |
4cf1bc1f KS |
7839 | case BPF_MAP_TYPE_TASK_STORAGE: |
7840 | if (func_id != BPF_FUNC_task_storage_get && | |
9db44fdd KKD |
7841 | func_id != BPF_FUNC_task_storage_delete && |
7842 | func_id != BPF_FUNC_kptr_xchg) | |
4cf1bc1f KS |
7843 | goto error; |
7844 | break; | |
c4bcfb38 YS |
7845 | case BPF_MAP_TYPE_CGRP_STORAGE: |
7846 | if (func_id != BPF_FUNC_cgrp_storage_get && | |
9db44fdd KKD |
7847 | func_id != BPF_FUNC_cgrp_storage_delete && |
7848 | func_id != BPF_FUNC_kptr_xchg) | |
c4bcfb38 YS |
7849 | goto error; |
7850 | break; | |
9330986c JK |
7851 | case BPF_MAP_TYPE_BLOOM_FILTER: |
7852 | if (func_id != BPF_FUNC_map_peek_elem && | |
7853 | func_id != BPF_FUNC_map_push_elem) | |
7854 | goto error; | |
7855 | break; | |
6aff67c8 AS |
7856 | default: |
7857 | break; | |
7858 | } | |
7859 | ||
7860 | /* ... and second from the function itself. */ | |
7861 | switch (func_id) { | |
7862 | case BPF_FUNC_tail_call: | |
7863 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
7864 | goto error; | |
e411901c MF |
7865 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
7866 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
7867 | return -EINVAL; |
7868 | } | |
6aff67c8 AS |
7869 | break; |
7870 | case BPF_FUNC_perf_event_read: | |
7871 | case BPF_FUNC_perf_event_output: | |
908432ca | 7872 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 7873 | case BPF_FUNC_skb_output: |
d831ee84 | 7874 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
7875 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
7876 | goto error; | |
7877 | break; | |
5b029a32 DB |
7878 | case BPF_FUNC_ringbuf_output: |
7879 | case BPF_FUNC_ringbuf_reserve: | |
7880 | case BPF_FUNC_ringbuf_query: | |
bc34dee6 JK |
7881 | case BPF_FUNC_ringbuf_reserve_dynptr: |
7882 | case BPF_FUNC_ringbuf_submit_dynptr: | |
7883 | case BPF_FUNC_ringbuf_discard_dynptr: | |
5b029a32 DB |
7884 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
7885 | goto error; | |
7886 | break; | |
20571567 DV |
7887 | case BPF_FUNC_user_ringbuf_drain: |
7888 | if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF) | |
7889 | goto error; | |
7890 | break; | |
6aff67c8 AS |
7891 | case BPF_FUNC_get_stackid: |
7892 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
7893 | goto error; | |
7894 | break; | |
60d20f91 | 7895 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 7896 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
7897 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
7898 | goto error; | |
7899 | break; | |
97f91a7c | 7900 | case BPF_FUNC_redirect_map: |
9c270af3 | 7901 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 7902 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
7903 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
7904 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
7905 | goto error; |
7906 | break; | |
174a79ff | 7907 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 7908 | case BPF_FUNC_msg_redirect_map: |
81110384 | 7909 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
7910 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
7911 | goto error; | |
7912 | break; | |
81110384 JF |
7913 | case BPF_FUNC_sk_redirect_hash: |
7914 | case BPF_FUNC_msg_redirect_hash: | |
7915 | case BPF_FUNC_sock_hash_update: | |
7916 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
7917 | goto error; |
7918 | break; | |
cd339431 | 7919 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
7920 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
7921 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
7922 | goto error; |
7923 | break; | |
2dbb9b9e | 7924 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
7925 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
7926 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
7927 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
7928 | goto error; |
7929 | break; | |
f1a2e44a | 7930 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
7931 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
7932 | map->map_type != BPF_MAP_TYPE_STACK) | |
7933 | goto error; | |
7934 | break; | |
9330986c JK |
7935 | case BPF_FUNC_map_peek_elem: |
7936 | case BPF_FUNC_map_push_elem: | |
7937 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
7938 | map->map_type != BPF_MAP_TYPE_STACK && | |
7939 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
7940 | goto error; | |
7941 | break; | |
07343110 FZ |
7942 | case BPF_FUNC_map_lookup_percpu_elem: |
7943 | if (map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && | |
7944 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && | |
7945 | map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH) | |
7946 | goto error; | |
7947 | break; | |
6ac99e8f MKL |
7948 | case BPF_FUNC_sk_storage_get: |
7949 | case BPF_FUNC_sk_storage_delete: | |
7950 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
7951 | goto error; | |
7952 | break; | |
8ea63684 KS |
7953 | case BPF_FUNC_inode_storage_get: |
7954 | case BPF_FUNC_inode_storage_delete: | |
7955 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
7956 | goto error; | |
7957 | break; | |
4cf1bc1f KS |
7958 | case BPF_FUNC_task_storage_get: |
7959 | case BPF_FUNC_task_storage_delete: | |
7960 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
7961 | goto error; | |
7962 | break; | |
c4bcfb38 YS |
7963 | case BPF_FUNC_cgrp_storage_get: |
7964 | case BPF_FUNC_cgrp_storage_delete: | |
7965 | if (map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) | |
7966 | goto error; | |
7967 | break; | |
6aff67c8 AS |
7968 | default: |
7969 | break; | |
35578d79 KX |
7970 | } |
7971 | ||
7972 | return 0; | |
6aff67c8 | 7973 | error: |
61bd5218 | 7974 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 7975 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 7976 | return -EINVAL; |
35578d79 KX |
7977 | } |
7978 | ||
90133415 | 7979 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
7980 | { |
7981 | int count = 0; | |
7982 | ||
39f19ebb | 7983 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7984 | count++; |
39f19ebb | 7985 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7986 | count++; |
39f19ebb | 7987 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7988 | count++; |
39f19ebb | 7989 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7990 | count++; |
39f19ebb | 7991 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
7992 | count++; |
7993 | ||
90133415 DB |
7994 | /* We only support one arg being in raw mode at the moment, |
7995 | * which is sufficient for the helper functions we have | |
7996 | * right now. | |
7997 | */ | |
7998 | return count <= 1; | |
7999 | } | |
8000 | ||
508362ac | 8001 | static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg) |
90133415 | 8002 | { |
508362ac MM |
8003 | bool is_fixed = fn->arg_type[arg] & MEM_FIXED_SIZE; |
8004 | bool has_size = fn->arg_size[arg] != 0; | |
8005 | bool is_next_size = false; | |
8006 | ||
8007 | if (arg + 1 < ARRAY_SIZE(fn->arg_type)) | |
8008 | is_next_size = arg_type_is_mem_size(fn->arg_type[arg + 1]); | |
8009 | ||
8010 | if (base_type(fn->arg_type[arg]) != ARG_PTR_TO_MEM) | |
8011 | return is_next_size; | |
8012 | ||
8013 | return has_size == is_next_size || is_next_size == is_fixed; | |
90133415 DB |
8014 | } |
8015 | ||
8016 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
8017 | { | |
8018 | /* bpf_xxx(..., buf, len) call will access 'len' | |
8019 | * bytes from memory 'buf'. Both arg types need | |
8020 | * to be paired, so make sure there's no buggy | |
8021 | * helper function specification. | |
8022 | */ | |
8023 | if (arg_type_is_mem_size(fn->arg1_type) || | |
508362ac MM |
8024 | check_args_pair_invalid(fn, 0) || |
8025 | check_args_pair_invalid(fn, 1) || | |
8026 | check_args_pair_invalid(fn, 2) || | |
8027 | check_args_pair_invalid(fn, 3) || | |
8028 | check_args_pair_invalid(fn, 4)) | |
90133415 DB |
8029 | return false; |
8030 | ||
8031 | return true; | |
8032 | } | |
8033 | ||
9436ef6e LB |
8034 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
8035 | { | |
8036 | int i; | |
8037 | ||
1df8f55a | 8038 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
4e814da0 KKD |
8039 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_BTF_ID) |
8040 | return !!fn->arg_btf_id[i]; | |
8041 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_SPIN_LOCK) | |
8042 | return fn->arg_btf_id[i] == BPF_PTR_POISON; | |
508362ac MM |
8043 | if (base_type(fn->arg_type[i]) != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i] && |
8044 | /* arg_btf_id and arg_size are in a union. */ | |
8045 | (base_type(fn->arg_type[i]) != ARG_PTR_TO_MEM || | |
8046 | !(fn->arg_type[i] & MEM_FIXED_SIZE))) | |
1df8f55a MKL |
8047 | return false; |
8048 | } | |
8049 | ||
9436ef6e LB |
8050 | return true; |
8051 | } | |
8052 | ||
0c9a7a7e | 8053 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
8054 | { |
8055 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 8056 | check_arg_pair_ok(fn) && |
b2d8ef19 | 8057 | check_btf_id_ok(fn) ? 0 : -EINVAL; |
435faee1 DB |
8058 | } |
8059 | ||
de8f3a83 DB |
8060 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
8061 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
66e3a13e JK |
8062 | * |
8063 | * This also applies to dynptr slices belonging to skb and xdp dynptrs, | |
8064 | * since these slices point to packet data. | |
f1174f77 | 8065 | */ |
b239da34 | 8066 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 8067 | { |
b239da34 KKD |
8068 | struct bpf_func_state *state; |
8069 | struct bpf_reg_state *reg; | |
969bf05e | 8070 | |
b239da34 | 8071 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
66e3a13e | 8072 | if (reg_is_pkt_pointer_any(reg) || reg_is_dynptr_slice_pkt(reg)) |
dbd8d228 | 8073 | mark_reg_invalid(env, reg); |
b239da34 | 8074 | })); |
f4d7e40a AS |
8075 | } |
8076 | ||
6d94e741 AS |
8077 | enum { |
8078 | AT_PKT_END = -1, | |
8079 | BEYOND_PKT_END = -2, | |
8080 | }; | |
8081 | ||
8082 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
8083 | { | |
8084 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
8085 | struct bpf_reg_state *reg = &state->regs[regn]; | |
8086 | ||
8087 | if (reg->type != PTR_TO_PACKET) | |
8088 | /* PTR_TO_PACKET_META is not supported yet */ | |
8089 | return; | |
8090 | ||
8091 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
8092 | * How far beyond pkt_end it goes is unknown. | |
8093 | * if (!range_open) it's the case of pkt >= pkt_end | |
8094 | * if (range_open) it's the case of pkt > pkt_end | |
8095 | * hence this pointer is at least 1 byte bigger than pkt_end | |
8096 | */ | |
8097 | if (range_open) | |
8098 | reg->range = BEYOND_PKT_END; | |
8099 | else | |
8100 | reg->range = AT_PKT_END; | |
8101 | } | |
8102 | ||
fd978bf7 JS |
8103 | /* The pointer with the specified id has released its reference to kernel |
8104 | * resources. Identify all copies of the same pointer and clear the reference. | |
8105 | */ | |
8106 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 8107 | int ref_obj_id) |
fd978bf7 | 8108 | { |
b239da34 KKD |
8109 | struct bpf_func_state *state; |
8110 | struct bpf_reg_state *reg; | |
1b986589 | 8111 | int err; |
fd978bf7 | 8112 | |
1b986589 MKL |
8113 | err = release_reference_state(cur_func(env), ref_obj_id); |
8114 | if (err) | |
8115 | return err; | |
8116 | ||
b239da34 | 8117 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
dbd8d228 KKD |
8118 | if (reg->ref_obj_id == ref_obj_id) |
8119 | mark_reg_invalid(env, reg); | |
b239da34 | 8120 | })); |
fd978bf7 | 8121 | |
1b986589 | 8122 | return 0; |
fd978bf7 JS |
8123 | } |
8124 | ||
6a3cd331 DM |
8125 | static void invalidate_non_owning_refs(struct bpf_verifier_env *env) |
8126 | { | |
8127 | struct bpf_func_state *unused; | |
8128 | struct bpf_reg_state *reg; | |
8129 | ||
8130 | bpf_for_each_reg_in_vstate(env->cur_state, unused, reg, ({ | |
8131 | if (type_is_non_owning_ref(reg->type)) | |
dbd8d228 | 8132 | mark_reg_invalid(env, reg); |
6a3cd331 DM |
8133 | })); |
8134 | } | |
8135 | ||
51c39bb1 AS |
8136 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
8137 | struct bpf_reg_state *regs) | |
8138 | { | |
8139 | int i; | |
8140 | ||
8141 | /* after the call registers r0 - r5 were scratched */ | |
8142 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
8143 | mark_reg_not_init(env, regs, caller_saved[i]); | |
8144 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
8145 | } | |
8146 | } | |
8147 | ||
14351375 YS |
8148 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
8149 | struct bpf_func_state *caller, | |
8150 | struct bpf_func_state *callee, | |
8151 | int insn_idx); | |
8152 | ||
be2ef816 AN |
8153 | static int set_callee_state(struct bpf_verifier_env *env, |
8154 | struct bpf_func_state *caller, | |
8155 | struct bpf_func_state *callee, int insn_idx); | |
8156 | ||
5d92ddc3 DM |
8157 | static bool is_callback_calling_kfunc(u32 btf_id); |
8158 | ||
14351375 YS |
8159 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8160 | int *insn_idx, int subprog, | |
8161 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
8162 | { |
8163 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 8164 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 8165 | struct bpf_func_state *caller, *callee; |
14351375 | 8166 | int err; |
51c39bb1 | 8167 | bool is_global = false; |
f4d7e40a | 8168 | |
aada9ce6 | 8169 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 8170 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 8171 | state->curframe + 2); |
f4d7e40a AS |
8172 | return -E2BIG; |
8173 | } | |
8174 | ||
f4d7e40a AS |
8175 | caller = state->frame[state->curframe]; |
8176 | if (state->frame[state->curframe + 1]) { | |
8177 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
8178 | state->curframe + 1); | |
8179 | return -EFAULT; | |
8180 | } | |
8181 | ||
51c39bb1 AS |
8182 | func_info_aux = env->prog->aux->func_info_aux; |
8183 | if (func_info_aux) | |
8184 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
95f2f26f | 8185 | err = btf_check_subprog_call(env, subprog, caller->regs); |
51c39bb1 AS |
8186 | if (err == -EFAULT) |
8187 | return err; | |
8188 | if (is_global) { | |
8189 | if (err) { | |
8190 | verbose(env, "Caller passes invalid args into func#%d\n", | |
8191 | subprog); | |
8192 | return err; | |
8193 | } else { | |
8194 | if (env->log.level & BPF_LOG_LEVEL) | |
8195 | verbose(env, | |
8196 | "Func#%d is global and valid. Skipping.\n", | |
8197 | subprog); | |
8198 | clear_caller_saved_regs(env, caller->regs); | |
8199 | ||
45159b27 | 8200 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 8201 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 8202 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
8203 | |
8204 | /* continue with next insn after call */ | |
8205 | return 0; | |
8206 | } | |
8207 | } | |
8208 | ||
be2ef816 AN |
8209 | /* set_callee_state is used for direct subprog calls, but we are |
8210 | * interested in validating only BPF helpers that can call subprogs as | |
8211 | * callbacks | |
8212 | */ | |
5d92ddc3 DM |
8213 | if (set_callee_state_cb != set_callee_state) { |
8214 | if (bpf_pseudo_kfunc_call(insn) && | |
8215 | !is_callback_calling_kfunc(insn->imm)) { | |
8216 | verbose(env, "verifier bug: kfunc %s#%d not marked as callback-calling\n", | |
8217 | func_id_name(insn->imm), insn->imm); | |
8218 | return -EFAULT; | |
8219 | } else if (!bpf_pseudo_kfunc_call(insn) && | |
8220 | !is_callback_calling_function(insn->imm)) { /* helper */ | |
8221 | verbose(env, "verifier bug: helper %s#%d not marked as callback-calling\n", | |
8222 | func_id_name(insn->imm), insn->imm); | |
8223 | return -EFAULT; | |
8224 | } | |
be2ef816 AN |
8225 | } |
8226 | ||
bfc6bb74 | 8227 | if (insn->code == (BPF_JMP | BPF_CALL) && |
a5bebc4f | 8228 | insn->src_reg == 0 && |
bfc6bb74 AS |
8229 | insn->imm == BPF_FUNC_timer_set_callback) { |
8230 | struct bpf_verifier_state *async_cb; | |
8231 | ||
8232 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 8233 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
8234 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
8235 | *insn_idx, subprog); | |
8236 | if (!async_cb) | |
8237 | return -EFAULT; | |
8238 | callee = async_cb->frame[0]; | |
8239 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
8240 | ||
8241 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
8242 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
8243 | if (err) | |
8244 | return err; | |
8245 | ||
8246 | clear_caller_saved_regs(env, caller->regs); | |
8247 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
8248 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
8249 | /* continue with next insn after call */ | |
8250 | return 0; | |
8251 | } | |
8252 | ||
f4d7e40a AS |
8253 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
8254 | if (!callee) | |
8255 | return -ENOMEM; | |
8256 | state->frame[state->curframe + 1] = callee; | |
8257 | ||
8258 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
8259 | * into its own stack before reading from it. | |
8260 | * callee can read/write into caller's stack | |
8261 | */ | |
8262 | init_func_state(env, callee, | |
8263 | /* remember the callsite, it will be used by bpf_exit */ | |
8264 | *insn_idx /* callsite */, | |
8265 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 8266 | subprog /* subprog number within this prog */); |
f4d7e40a | 8267 | |
fd978bf7 | 8268 | /* Transfer references to the callee */ |
c69431aa | 8269 | err = copy_reference_state(callee, caller); |
fd978bf7 | 8270 | if (err) |
eb86559a | 8271 | goto err_out; |
fd978bf7 | 8272 | |
14351375 YS |
8273 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
8274 | if (err) | |
eb86559a | 8275 | goto err_out; |
f4d7e40a | 8276 | |
51c39bb1 | 8277 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
8278 | |
8279 | /* only increment it after check_reg_arg() finished */ | |
8280 | state->curframe++; | |
8281 | ||
8282 | /* and go analyze first insn of the callee */ | |
14351375 | 8283 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 8284 | |
06ee7115 | 8285 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 8286 | verbose(env, "caller:\n"); |
0f55f9ed | 8287 | print_verifier_state(env, caller, true); |
f4d7e40a | 8288 | verbose(env, "callee:\n"); |
0f55f9ed | 8289 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
8290 | } |
8291 | return 0; | |
eb86559a WY |
8292 | |
8293 | err_out: | |
8294 | free_func_state(callee); | |
8295 | state->frame[state->curframe + 1] = NULL; | |
8296 | return err; | |
f4d7e40a AS |
8297 | } |
8298 | ||
314ee05e YS |
8299 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
8300 | struct bpf_func_state *caller, | |
8301 | struct bpf_func_state *callee) | |
8302 | { | |
8303 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
8304 | * void *callback_ctx, u64 flags); | |
8305 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
8306 | * void *callback_ctx); | |
8307 | */ | |
8308 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
8309 | ||
8310 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
8311 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8312 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8313 | ||
8314 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
8315 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
8316 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8317 | ||
8318 | /* pointer to stack or null */ | |
8319 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
8320 | ||
8321 | /* unused */ | |
8322 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8323 | return 0; | |
8324 | } | |
8325 | ||
14351375 YS |
8326 | static int set_callee_state(struct bpf_verifier_env *env, |
8327 | struct bpf_func_state *caller, | |
8328 | struct bpf_func_state *callee, int insn_idx) | |
8329 | { | |
8330 | int i; | |
8331 | ||
8332 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
8333 | * pointers, which connects us up to the liveness chain | |
8334 | */ | |
8335 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
8336 | callee->regs[i] = caller->regs[i]; | |
8337 | return 0; | |
8338 | } | |
8339 | ||
8340 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
8341 | int *insn_idx) | |
8342 | { | |
8343 | int subprog, target_insn; | |
8344 | ||
8345 | target_insn = *insn_idx + insn->imm + 1; | |
8346 | subprog = find_subprog(env, target_insn); | |
8347 | if (subprog < 0) { | |
8348 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
8349 | target_insn); | |
8350 | return -EFAULT; | |
8351 | } | |
8352 | ||
8353 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
8354 | } | |
8355 | ||
69c087ba YS |
8356 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
8357 | struct bpf_func_state *caller, | |
8358 | struct bpf_func_state *callee, | |
8359 | int insn_idx) | |
8360 | { | |
8361 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
8362 | struct bpf_map *map; | |
8363 | int err; | |
8364 | ||
8365 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
8366 | verbose(env, "tail_call abusing map_ptr\n"); | |
8367 | return -EINVAL; | |
8368 | } | |
8369 | ||
8370 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
8371 | if (!map->ops->map_set_for_each_callback_args || | |
8372 | !map->ops->map_for_each_callback) { | |
8373 | verbose(env, "callback function not allowed for map\n"); | |
8374 | return -ENOTSUPP; | |
8375 | } | |
8376 | ||
8377 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
8378 | if (err) | |
8379 | return err; | |
8380 | ||
8381 | callee->in_callback_fn = true; | |
1bfe26fb | 8382 | callee->callback_ret_range = tnum_range(0, 1); |
69c087ba YS |
8383 | return 0; |
8384 | } | |
8385 | ||
e6f2dd0f JK |
8386 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
8387 | struct bpf_func_state *caller, | |
8388 | struct bpf_func_state *callee, | |
8389 | int insn_idx) | |
8390 | { | |
8391 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
8392 | * u64 flags); | |
8393 | * callback_fn(u32 index, void *callback_ctx); | |
8394 | */ | |
8395 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
8396 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
8397 | ||
8398 | /* unused */ | |
8399 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8400 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8401 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8402 | ||
8403 | callee->in_callback_fn = true; | |
1bfe26fb | 8404 | callee->callback_ret_range = tnum_range(0, 1); |
e6f2dd0f JK |
8405 | return 0; |
8406 | } | |
8407 | ||
b00628b1 AS |
8408 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
8409 | struct bpf_func_state *caller, | |
8410 | struct bpf_func_state *callee, | |
8411 | int insn_idx) | |
8412 | { | |
8413 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8414 | ||
8415 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
8416 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
8417 | */ | |
8418 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
8419 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
8420 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
8421 | ||
8422 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
8423 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8424 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
8425 | ||
8426 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
8427 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
8428 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
8429 | ||
8430 | /* unused */ | |
8431 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8432 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 8433 | callee->in_async_callback_fn = true; |
1bfe26fb | 8434 | callee->callback_ret_range = tnum_range(0, 1); |
b00628b1 AS |
8435 | return 0; |
8436 | } | |
8437 | ||
7c7e3d31 SL |
8438 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
8439 | struct bpf_func_state *caller, | |
8440 | struct bpf_func_state *callee, | |
8441 | int insn_idx) | |
8442 | { | |
8443 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
8444 | * void *callback_fn, void *callback_ctx, u64 flags) | |
8445 | * (callback_fn)(struct task_struct *task, | |
8446 | * struct vm_area_struct *vma, void *callback_ctx); | |
8447 | */ | |
8448 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
8449 | ||
8450 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
8451 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8452 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 8453 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
8454 | |
8455 | /* pointer to stack or null */ | |
8456 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
8457 | ||
8458 | /* unused */ | |
8459 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8460 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8461 | callee->in_callback_fn = true; | |
1bfe26fb | 8462 | callee->callback_ret_range = tnum_range(0, 1); |
7c7e3d31 SL |
8463 | return 0; |
8464 | } | |
8465 | ||
20571567 DV |
8466 | static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env, |
8467 | struct bpf_func_state *caller, | |
8468 | struct bpf_func_state *callee, | |
8469 | int insn_idx) | |
8470 | { | |
8471 | /* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void | |
8472 | * callback_ctx, u64 flags); | |
27060531 | 8473 | * callback_fn(const struct bpf_dynptr_t* dynptr, void *callback_ctx); |
20571567 DV |
8474 | */ |
8475 | __mark_reg_not_init(env, &callee->regs[BPF_REG_0]); | |
f8064ab9 | 8476 | mark_dynptr_cb_reg(env, &callee->regs[BPF_REG_1], BPF_DYNPTR_TYPE_LOCAL); |
20571567 DV |
8477 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; |
8478 | ||
8479 | /* unused */ | |
8480 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8481 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8482 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8483 | ||
8484 | callee->in_callback_fn = true; | |
c92a7a52 | 8485 | callee->callback_ret_range = tnum_range(0, 1); |
20571567 DV |
8486 | return 0; |
8487 | } | |
8488 | ||
5d92ddc3 DM |
8489 | static int set_rbtree_add_callback_state(struct bpf_verifier_env *env, |
8490 | struct bpf_func_state *caller, | |
8491 | struct bpf_func_state *callee, | |
8492 | int insn_idx) | |
8493 | { | |
8494 | /* void bpf_rbtree_add(struct bpf_rb_root *root, struct bpf_rb_node *node, | |
8495 | * bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b)); | |
8496 | * | |
8497 | * 'struct bpf_rb_node *node' arg to bpf_rbtree_add is the same PTR_TO_BTF_ID w/ offset | |
8498 | * that 'less' callback args will be receiving. However, 'node' arg was release_reference'd | |
8499 | * by this point, so look at 'root' | |
8500 | */ | |
8501 | struct btf_field *field; | |
8502 | ||
8503 | field = reg_find_field_offset(&caller->regs[BPF_REG_1], caller->regs[BPF_REG_1].off, | |
8504 | BPF_RB_ROOT); | |
8505 | if (!field || !field->graph_root.value_btf_id) | |
8506 | return -EFAULT; | |
8507 | ||
8508 | mark_reg_graph_node(callee->regs, BPF_REG_1, &field->graph_root); | |
8509 | ref_set_non_owning(env, &callee->regs[BPF_REG_1]); | |
8510 | mark_reg_graph_node(callee->regs, BPF_REG_2, &field->graph_root); | |
8511 | ref_set_non_owning(env, &callee->regs[BPF_REG_2]); | |
8512 | ||
8513 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8514 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8515 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8516 | callee->in_callback_fn = true; | |
8517 | callee->callback_ret_range = tnum_range(0, 1); | |
8518 | return 0; | |
8519 | } | |
8520 | ||
8521 | static bool is_rbtree_lock_required_kfunc(u32 btf_id); | |
8522 | ||
8523 | /* Are we currently verifying the callback for a rbtree helper that must | |
8524 | * be called with lock held? If so, no need to complain about unreleased | |
8525 | * lock | |
8526 | */ | |
8527 | static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env) | |
8528 | { | |
8529 | struct bpf_verifier_state *state = env->cur_state; | |
8530 | struct bpf_insn *insn = env->prog->insnsi; | |
8531 | struct bpf_func_state *callee; | |
8532 | int kfunc_btf_id; | |
8533 | ||
8534 | if (!state->curframe) | |
8535 | return false; | |
8536 | ||
8537 | callee = state->frame[state->curframe]; | |
8538 | ||
8539 | if (!callee->in_callback_fn) | |
8540 | return false; | |
8541 | ||
8542 | kfunc_btf_id = insn[callee->callsite].imm; | |
8543 | return is_rbtree_lock_required_kfunc(kfunc_btf_id); | |
8544 | } | |
8545 | ||
f4d7e40a AS |
8546 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
8547 | { | |
8548 | struct bpf_verifier_state *state = env->cur_state; | |
8549 | struct bpf_func_state *caller, *callee; | |
8550 | struct bpf_reg_state *r0; | |
fd978bf7 | 8551 | int err; |
f4d7e40a AS |
8552 | |
8553 | callee = state->frame[state->curframe]; | |
8554 | r0 = &callee->regs[BPF_REG_0]; | |
8555 | if (r0->type == PTR_TO_STACK) { | |
8556 | /* technically it's ok to return caller's stack pointer | |
8557 | * (or caller's caller's pointer) back to the caller, | |
8558 | * since these pointers are valid. Only current stack | |
8559 | * pointer will be invalid as soon as function exits, | |
8560 | * but let's be conservative | |
8561 | */ | |
8562 | verbose(env, "cannot return stack pointer to the caller\n"); | |
8563 | return -EINVAL; | |
8564 | } | |
8565 | ||
eb86559a | 8566 | caller = state->frame[state->curframe - 1]; |
69c087ba YS |
8567 | if (callee->in_callback_fn) { |
8568 | /* enforce R0 return value range [0, 1]. */ | |
1bfe26fb | 8569 | struct tnum range = callee->callback_ret_range; |
69c087ba YS |
8570 | |
8571 | if (r0->type != SCALAR_VALUE) { | |
8572 | verbose(env, "R0 not a scalar value\n"); | |
8573 | return -EACCES; | |
8574 | } | |
8575 | if (!tnum_in(range, r0->var_off)) { | |
8576 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
8577 | return -EINVAL; | |
8578 | } | |
8579 | } else { | |
8580 | /* return to the caller whatever r0 had in the callee */ | |
8581 | caller->regs[BPF_REG_0] = *r0; | |
8582 | } | |
f4d7e40a | 8583 | |
9d9d00ac KKD |
8584 | /* callback_fn frame should have released its own additions to parent's |
8585 | * reference state at this point, or check_reference_leak would | |
8586 | * complain, hence it must be the same as the caller. There is no need | |
8587 | * to copy it back. | |
8588 | */ | |
8589 | if (!callee->in_callback_fn) { | |
8590 | /* Transfer references to the caller */ | |
8591 | err = copy_reference_state(caller, callee); | |
8592 | if (err) | |
8593 | return err; | |
8594 | } | |
fd978bf7 | 8595 | |
f4d7e40a | 8596 | *insn_idx = callee->callsite + 1; |
06ee7115 | 8597 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 8598 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 8599 | print_verifier_state(env, callee, true); |
f4d7e40a | 8600 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 8601 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
8602 | } |
8603 | /* clear everything in the callee */ | |
8604 | free_func_state(callee); | |
eb86559a | 8605 | state->frame[state->curframe--] = NULL; |
f4d7e40a AS |
8606 | return 0; |
8607 | } | |
8608 | ||
849fa506 YS |
8609 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
8610 | int func_id, | |
8611 | struct bpf_call_arg_meta *meta) | |
8612 | { | |
8613 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
8614 | ||
8615 | if (ret_type != RET_INTEGER || | |
8616 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 8617 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
8618 | func_id != BPF_FUNC_probe_read_str && |
8619 | func_id != BPF_FUNC_probe_read_kernel_str && | |
8620 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
8621 | return; |
8622 | ||
10060503 | 8623 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 8624 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
8625 | ret_reg->smin_value = -MAX_ERRNO; |
8626 | ret_reg->s32_min_value = -MAX_ERRNO; | |
3844d153 | 8627 | reg_bounds_sync(ret_reg); |
849fa506 YS |
8628 | } |
8629 | ||
c93552c4 DB |
8630 | static int |
8631 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
8632 | int func_id, int insn_idx) | |
8633 | { | |
8634 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 8635 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
8636 | |
8637 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
8638 | func_id != BPF_FUNC_map_lookup_elem && |
8639 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
8640 | func_id != BPF_FUNC_map_delete_elem && |
8641 | func_id != BPF_FUNC_map_push_elem && | |
8642 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 8643 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f | 8644 | func_id != BPF_FUNC_for_each_map_elem && |
07343110 FZ |
8645 | func_id != BPF_FUNC_redirect_map && |
8646 | func_id != BPF_FUNC_map_lookup_percpu_elem) | |
c93552c4 | 8647 | return 0; |
09772d92 | 8648 | |
591fe988 | 8649 | if (map == NULL) { |
c93552c4 DB |
8650 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
8651 | return -EINVAL; | |
8652 | } | |
8653 | ||
591fe988 DB |
8654 | /* In case of read-only, some additional restrictions |
8655 | * need to be applied in order to prevent altering the | |
8656 | * state of the map from program side. | |
8657 | */ | |
8658 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
8659 | (func_id == BPF_FUNC_map_delete_elem || | |
8660 | func_id == BPF_FUNC_map_update_elem || | |
8661 | func_id == BPF_FUNC_map_push_elem || | |
8662 | func_id == BPF_FUNC_map_pop_elem)) { | |
8663 | verbose(env, "write into map forbidden\n"); | |
8664 | return -EACCES; | |
8665 | } | |
8666 | ||
d2e4c1e6 | 8667 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 8668 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 8669 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 8670 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 8671 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 8672 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
8673 | return 0; |
8674 | } | |
8675 | ||
d2e4c1e6 DB |
8676 | static int |
8677 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
8678 | int func_id, int insn_idx) | |
8679 | { | |
8680 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
8681 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
8682 | struct bpf_map *map = meta->map_ptr; | |
a657182a | 8683 | u64 val, max; |
cc52d914 | 8684 | int err; |
d2e4c1e6 DB |
8685 | |
8686 | if (func_id != BPF_FUNC_tail_call) | |
8687 | return 0; | |
8688 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
8689 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
8690 | return -EINVAL; | |
8691 | } | |
8692 | ||
d2e4c1e6 | 8693 | reg = ®s[BPF_REG_3]; |
a657182a DB |
8694 | val = reg->var_off.value; |
8695 | max = map->max_entries; | |
d2e4c1e6 | 8696 | |
a657182a | 8697 | if (!(register_is_const(reg) && val < max)) { |
d2e4c1e6 DB |
8698 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
8699 | return 0; | |
8700 | } | |
8701 | ||
cc52d914 DB |
8702 | err = mark_chain_precision(env, BPF_REG_3); |
8703 | if (err) | |
8704 | return err; | |
d2e4c1e6 DB |
8705 | if (bpf_map_key_unseen(aux)) |
8706 | bpf_map_key_store(aux, val); | |
8707 | else if (!bpf_map_key_poisoned(aux) && | |
8708 | bpf_map_key_immediate(aux) != val) | |
8709 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
8710 | return 0; | |
8711 | } | |
8712 | ||
fd978bf7 JS |
8713 | static int check_reference_leak(struct bpf_verifier_env *env) |
8714 | { | |
8715 | struct bpf_func_state *state = cur_func(env); | |
9d9d00ac | 8716 | bool refs_lingering = false; |
fd978bf7 JS |
8717 | int i; |
8718 | ||
9d9d00ac KKD |
8719 | if (state->frameno && !state->in_callback_fn) |
8720 | return 0; | |
8721 | ||
fd978bf7 | 8722 | for (i = 0; i < state->acquired_refs; i++) { |
9d9d00ac KKD |
8723 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) |
8724 | continue; | |
fd978bf7 JS |
8725 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
8726 | state->refs[i].id, state->refs[i].insn_idx); | |
9d9d00ac | 8727 | refs_lingering = true; |
fd978bf7 | 8728 | } |
9d9d00ac | 8729 | return refs_lingering ? -EINVAL : 0; |
fd978bf7 JS |
8730 | } |
8731 | ||
7b15523a FR |
8732 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
8733 | struct bpf_reg_state *regs) | |
8734 | { | |
8735 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
8736 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
8737 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
78aa1cc9 | 8738 | struct bpf_bprintf_data data = {}; |
7b15523a FR |
8739 | int err, fmt_map_off, num_args; |
8740 | u64 fmt_addr; | |
8741 | char *fmt; | |
8742 | ||
8743 | /* data must be an array of u64 */ | |
8744 | if (data_len_reg->var_off.value % 8) | |
8745 | return -EINVAL; | |
8746 | num_args = data_len_reg->var_off.value / 8; | |
8747 | ||
8748 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
8749 | * and map_direct_value_addr is set. | |
8750 | */ | |
8751 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
8752 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
8753 | fmt_map_off); | |
8e8ee109 FR |
8754 | if (err) { |
8755 | verbose(env, "verifier bug\n"); | |
8756 | return -EFAULT; | |
8757 | } | |
7b15523a FR |
8758 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
8759 | ||
8760 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
8761 | * can focus on validating the format specifiers. | |
8762 | */ | |
78aa1cc9 | 8763 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, num_args, &data); |
7b15523a FR |
8764 | if (err < 0) |
8765 | verbose(env, "Invalid format string\n"); | |
8766 | ||
8767 | return err; | |
8768 | } | |
8769 | ||
9b99edca JO |
8770 | static int check_get_func_ip(struct bpf_verifier_env *env) |
8771 | { | |
9b99edca JO |
8772 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
8773 | int func_id = BPF_FUNC_get_func_ip; | |
8774 | ||
8775 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 8776 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
8777 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
8778 | func_id_name(func_id), func_id); | |
8779 | return -ENOTSUPP; | |
8780 | } | |
8781 | return 0; | |
9ffd9f3f JO |
8782 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
8783 | return 0; | |
9b99edca JO |
8784 | } |
8785 | ||
8786 | verbose(env, "func %s#%d not supported for program type %d\n", | |
8787 | func_id_name(func_id), func_id, type); | |
8788 | return -ENOTSUPP; | |
8789 | } | |
8790 | ||
1ade2371 EZ |
8791 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
8792 | { | |
8793 | return &env->insn_aux_data[env->insn_idx]; | |
8794 | } | |
8795 | ||
8796 | static bool loop_flag_is_zero(struct bpf_verifier_env *env) | |
8797 | { | |
8798 | struct bpf_reg_state *regs = cur_regs(env); | |
8799 | struct bpf_reg_state *reg = ®s[BPF_REG_4]; | |
8800 | bool reg_is_null = register_is_null(reg); | |
8801 | ||
8802 | if (reg_is_null) | |
8803 | mark_chain_precision(env, BPF_REG_4); | |
8804 | ||
8805 | return reg_is_null; | |
8806 | } | |
8807 | ||
8808 | static void update_loop_inline_state(struct bpf_verifier_env *env, u32 subprogno) | |
8809 | { | |
8810 | struct bpf_loop_inline_state *state = &cur_aux(env)->loop_inline_state; | |
8811 | ||
8812 | if (!state->initialized) { | |
8813 | state->initialized = 1; | |
8814 | state->fit_for_inline = loop_flag_is_zero(env); | |
8815 | state->callback_subprogno = subprogno; | |
8816 | return; | |
8817 | } | |
8818 | ||
8819 | if (!state->fit_for_inline) | |
8820 | return; | |
8821 | ||
8822 | state->fit_for_inline = (loop_flag_is_zero(env) && | |
8823 | state->callback_subprogno == subprogno); | |
8824 | } | |
8825 | ||
69c087ba YS |
8826 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8827 | int *insn_idx_p) | |
17a52670 | 8828 | { |
aef9d4a3 | 8829 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
17a52670 | 8830 | const struct bpf_func_proto *fn = NULL; |
3c480732 | 8831 | enum bpf_return_type ret_type; |
c25b2ae1 | 8832 | enum bpf_type_flag ret_flag; |
638f5b90 | 8833 | struct bpf_reg_state *regs; |
33ff9823 | 8834 | struct bpf_call_arg_meta meta; |
69c087ba | 8835 | int insn_idx = *insn_idx_p; |
969bf05e | 8836 | bool changes_data; |
69c087ba | 8837 | int i, err, func_id; |
17a52670 AS |
8838 | |
8839 | /* find function prototype */ | |
69c087ba | 8840 | func_id = insn->imm; |
17a52670 | 8841 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
8842 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
8843 | func_id); | |
17a52670 AS |
8844 | return -EINVAL; |
8845 | } | |
8846 | ||
00176a34 | 8847 | if (env->ops->get_func_proto) |
5e43f899 | 8848 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 8849 | if (!fn) { |
61bd5218 JK |
8850 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
8851 | func_id); | |
17a52670 AS |
8852 | return -EINVAL; |
8853 | } | |
8854 | ||
8855 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 8856 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 8857 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
8858 | return -EINVAL; |
8859 | } | |
8860 | ||
eae2e83e JO |
8861 | if (fn->allowed && !fn->allowed(env->prog)) { |
8862 | verbose(env, "helper call is not allowed in probe\n"); | |
8863 | return -EINVAL; | |
8864 | } | |
8865 | ||
01685c5b YS |
8866 | if (!env->prog->aux->sleepable && fn->might_sleep) { |
8867 | verbose(env, "helper call might sleep in a non-sleepable prog\n"); | |
8868 | return -EINVAL; | |
8869 | } | |
8870 | ||
04514d13 | 8871 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 8872 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
8873 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
8874 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
8875 | func_id_name(func_id), func_id); | |
8876 | return -EINVAL; | |
8877 | } | |
969bf05e | 8878 | |
33ff9823 | 8879 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 8880 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 8881 | |
0c9a7a7e | 8882 | err = check_func_proto(fn, func_id); |
435faee1 | 8883 | if (err) { |
61bd5218 | 8884 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 8885 | func_id_name(func_id), func_id); |
435faee1 DB |
8886 | return err; |
8887 | } | |
8888 | ||
9bb00b28 YS |
8889 | if (env->cur_state->active_rcu_lock) { |
8890 | if (fn->might_sleep) { | |
8891 | verbose(env, "sleepable helper %s#%d in rcu_read_lock region\n", | |
8892 | func_id_name(func_id), func_id); | |
8893 | return -EINVAL; | |
8894 | } | |
8895 | ||
8896 | if (env->prog->aux->sleepable && is_storage_get_function(func_id)) | |
8897 | env->insn_aux_data[insn_idx].storage_get_func_atomic = true; | |
8898 | } | |
8899 | ||
d83525ca | 8900 | meta.func_id = func_id; |
17a52670 | 8901 | /* check args */ |
523a4cf4 | 8902 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
1d18feb2 | 8903 | err = check_func_arg(env, i, &meta, fn, insn_idx); |
a7658e1a AS |
8904 | if (err) |
8905 | return err; | |
8906 | } | |
17a52670 | 8907 | |
c93552c4 DB |
8908 | err = record_func_map(env, &meta, func_id, insn_idx); |
8909 | if (err) | |
8910 | return err; | |
8911 | ||
d2e4c1e6 DB |
8912 | err = record_func_key(env, &meta, func_id, insn_idx); |
8913 | if (err) | |
8914 | return err; | |
8915 | ||
435faee1 DB |
8916 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
8917 | * is inferred from register state. | |
8918 | */ | |
8919 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
8920 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
8921 | BPF_WRITE, -1, false); | |
435faee1 DB |
8922 | if (err) |
8923 | return err; | |
8924 | } | |
8925 | ||
8f14852e KKD |
8926 | regs = cur_regs(env); |
8927 | ||
8928 | if (meta.release_regno) { | |
8929 | err = -EINVAL; | |
27060531 KKD |
8930 | /* This can only be set for PTR_TO_STACK, as CONST_PTR_TO_DYNPTR cannot |
8931 | * be released by any dynptr helper. Hence, unmark_stack_slots_dynptr | |
8932 | * is safe to do directly. | |
8933 | */ | |
8934 | if (arg_type_is_dynptr(fn->arg_type[meta.release_regno - BPF_REG_1])) { | |
8935 | if (regs[meta.release_regno].type == CONST_PTR_TO_DYNPTR) { | |
8936 | verbose(env, "verifier internal error: CONST_PTR_TO_DYNPTR cannot be released\n"); | |
8937 | return -EFAULT; | |
8938 | } | |
97e03f52 | 8939 | err = unmark_stack_slots_dynptr(env, ®s[meta.release_regno]); |
27060531 | 8940 | } else if (meta.ref_obj_id) { |
8f14852e | 8941 | err = release_reference(env, meta.ref_obj_id); |
27060531 KKD |
8942 | } else if (register_is_null(®s[meta.release_regno])) { |
8943 | /* meta.ref_obj_id can only be 0 if register that is meant to be | |
8944 | * released is NULL, which must be > R0. | |
8945 | */ | |
8f14852e | 8946 | err = 0; |
27060531 | 8947 | } |
46f8bc92 MKL |
8948 | if (err) { |
8949 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
8950 | func_id_name(func_id), func_id); | |
fd978bf7 | 8951 | return err; |
46f8bc92 | 8952 | } |
fd978bf7 JS |
8953 | } |
8954 | ||
e6f2dd0f JK |
8955 | switch (func_id) { |
8956 | case BPF_FUNC_tail_call: | |
8957 | err = check_reference_leak(env); | |
8958 | if (err) { | |
8959 | verbose(env, "tail_call would lead to reference leak\n"); | |
8960 | return err; | |
8961 | } | |
8962 | break; | |
8963 | case BPF_FUNC_get_local_storage: | |
8964 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
8965 | * this is required because get_local_storage() can't return an error. | |
8966 | */ | |
8967 | if (!register_is_null(®s[BPF_REG_2])) { | |
8968 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
8969 | return -EINVAL; | |
8970 | } | |
8971 | break; | |
8972 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
8973 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8974 | set_map_elem_callback_state); | |
e6f2dd0f JK |
8975 | break; |
8976 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
8977 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8978 | set_timer_callback_state); | |
e6f2dd0f JK |
8979 | break; |
8980 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
8981 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8982 | set_find_vma_callback_state); | |
e6f2dd0f JK |
8983 | break; |
8984 | case BPF_FUNC_snprintf: | |
7b15523a | 8985 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
8986 | break; |
8987 | case BPF_FUNC_loop: | |
1ade2371 | 8988 | update_loop_inline_state(env, meta.subprogno); |
e6f2dd0f JK |
8989 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8990 | set_loop_callback_state); | |
8991 | break; | |
263ae152 JK |
8992 | case BPF_FUNC_dynptr_from_mem: |
8993 | if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) { | |
8994 | verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n", | |
8995 | reg_type_str(env, regs[BPF_REG_1].type)); | |
8996 | return -EACCES; | |
8997 | } | |
69fd337a SF |
8998 | break; |
8999 | case BPF_FUNC_set_retval: | |
aef9d4a3 SF |
9000 | if (prog_type == BPF_PROG_TYPE_LSM && |
9001 | env->prog->expected_attach_type == BPF_LSM_CGROUP) { | |
69fd337a SF |
9002 | if (!env->prog->aux->attach_func_proto->type) { |
9003 | /* Make sure programs that attach to void | |
9004 | * hooks don't try to modify return value. | |
9005 | */ | |
9006 | verbose(env, "BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
9007 | return -EINVAL; | |
9008 | } | |
9009 | } | |
9010 | break; | |
88374342 | 9011 | case BPF_FUNC_dynptr_data: |
485ec51e JK |
9012 | { |
9013 | struct bpf_reg_state *reg; | |
9014 | int id, ref_obj_id; | |
20571567 | 9015 | |
485ec51e JK |
9016 | reg = get_dynptr_arg_reg(env, fn, regs); |
9017 | if (!reg) | |
9018 | return -EFAULT; | |
f8064ab9 | 9019 | |
f8064ab9 | 9020 | |
485ec51e JK |
9021 | if (meta.dynptr_id) { |
9022 | verbose(env, "verifier internal error: meta.dynptr_id already set\n"); | |
9023 | return -EFAULT; | |
88374342 | 9024 | } |
485ec51e JK |
9025 | if (meta.ref_obj_id) { |
9026 | verbose(env, "verifier internal error: meta.ref_obj_id already set\n"); | |
88374342 JK |
9027 | return -EFAULT; |
9028 | } | |
485ec51e JK |
9029 | |
9030 | id = dynptr_id(env, reg); | |
9031 | if (id < 0) { | |
9032 | verbose(env, "verifier internal error: failed to obtain dynptr id\n"); | |
9033 | return id; | |
9034 | } | |
9035 | ||
9036 | ref_obj_id = dynptr_ref_obj_id(env, reg); | |
9037 | if (ref_obj_id < 0) { | |
9038 | verbose(env, "verifier internal error: failed to obtain dynptr ref_obj_id\n"); | |
9039 | return ref_obj_id; | |
9040 | } | |
9041 | ||
9042 | meta.dynptr_id = id; | |
9043 | meta.ref_obj_id = ref_obj_id; | |
9044 | ||
88374342 | 9045 | break; |
485ec51e | 9046 | } |
b5964b96 JK |
9047 | case BPF_FUNC_dynptr_write: |
9048 | { | |
9049 | enum bpf_dynptr_type dynptr_type; | |
9050 | struct bpf_reg_state *reg; | |
9051 | ||
9052 | reg = get_dynptr_arg_reg(env, fn, regs); | |
9053 | if (!reg) | |
9054 | return -EFAULT; | |
9055 | ||
9056 | dynptr_type = dynptr_get_type(env, reg); | |
9057 | if (dynptr_type == BPF_DYNPTR_TYPE_INVALID) | |
9058 | return -EFAULT; | |
9059 | ||
9060 | if (dynptr_type == BPF_DYNPTR_TYPE_SKB) | |
9061 | /* this will trigger clear_all_pkt_pointers(), which will | |
9062 | * invalidate all dynptr slices associated with the skb | |
9063 | */ | |
9064 | changes_data = true; | |
9065 | ||
9066 | break; | |
9067 | } | |
20571567 DV |
9068 | case BPF_FUNC_user_ringbuf_drain: |
9069 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
9070 | set_user_ringbuf_callback_state); | |
9071 | break; | |
7b15523a FR |
9072 | } |
9073 | ||
e6f2dd0f JK |
9074 | if (err) |
9075 | return err; | |
9076 | ||
17a52670 | 9077 | /* reset caller saved regs */ |
dc503a8a | 9078 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 9079 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
9080 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
9081 | } | |
17a52670 | 9082 | |
5327ed3d JW |
9083 | /* helper call returns 64-bit value. */ |
9084 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
9085 | ||
dc503a8a | 9086 | /* update return register (already marked as written above) */ |
3c480732 | 9087 | ret_type = fn->ret_type; |
0c9a7a7e JK |
9088 | ret_flag = type_flag(ret_type); |
9089 | ||
9090 | switch (base_type(ret_type)) { | |
9091 | case RET_INTEGER: | |
f1174f77 | 9092 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 9093 | mark_reg_unknown(env, regs, BPF_REG_0); |
0c9a7a7e JK |
9094 | break; |
9095 | case RET_VOID: | |
17a52670 | 9096 | regs[BPF_REG_0].type = NOT_INIT; |
0c9a7a7e JK |
9097 | break; |
9098 | case RET_PTR_TO_MAP_VALUE: | |
f1174f77 | 9099 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 9100 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
9101 | /* remember map_ptr, so that check_map_access() |
9102 | * can check 'value_size' boundary of memory access | |
9103 | * to map element returned from bpf_map_lookup_elem() | |
9104 | */ | |
33ff9823 | 9105 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
9106 | verbose(env, |
9107 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
9108 | return -EINVAL; |
9109 | } | |
33ff9823 | 9110 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 9111 | regs[BPF_REG_0].map_uid = meta.map_uid; |
c25b2ae1 HL |
9112 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
9113 | if (!type_may_be_null(ret_type) && | |
db559117 | 9114 | btf_record_has_field(meta.map_ptr->record, BPF_SPIN_LOCK)) { |
c25b2ae1 | 9115 | regs[BPF_REG_0].id = ++env->id_gen; |
4d31f301 | 9116 | } |
0c9a7a7e JK |
9117 | break; |
9118 | case RET_PTR_TO_SOCKET: | |
c64b7983 | 9119 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9120 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
0c9a7a7e JK |
9121 | break; |
9122 | case RET_PTR_TO_SOCK_COMMON: | |
85a51f8c | 9123 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9124 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
0c9a7a7e JK |
9125 | break; |
9126 | case RET_PTR_TO_TCP_SOCK: | |
655a51e5 | 9127 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9128 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
0c9a7a7e | 9129 | break; |
2de2669b | 9130 | case RET_PTR_TO_MEM: |
457f4436 | 9131 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9132 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
457f4436 | 9133 | regs[BPF_REG_0].mem_size = meta.mem_size; |
0c9a7a7e JK |
9134 | break; |
9135 | case RET_PTR_TO_MEM_OR_BTF_ID: | |
9136 | { | |
eaa6bcb7 HL |
9137 | const struct btf_type *t; |
9138 | ||
9139 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 9140 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
9141 | if (!btf_type_is_struct(t)) { |
9142 | u32 tsize; | |
9143 | const struct btf_type *ret; | |
9144 | const char *tname; | |
9145 | ||
9146 | /* resolve the type size of ksym. */ | |
22dc4a0f | 9147 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 9148 | if (IS_ERR(ret)) { |
22dc4a0f | 9149 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
9150 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
9151 | tname, PTR_ERR(ret)); | |
9152 | return -EINVAL; | |
9153 | } | |
c25b2ae1 | 9154 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
eaa6bcb7 HL |
9155 | regs[BPF_REG_0].mem_size = tsize; |
9156 | } else { | |
34d3a78c HL |
9157 | /* MEM_RDONLY may be carried from ret_flag, but it |
9158 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise | |
9159 | * it will confuse the check of PTR_TO_BTF_ID in | |
9160 | * check_mem_access(). | |
9161 | */ | |
9162 | ret_flag &= ~MEM_RDONLY; | |
9163 | ||
c25b2ae1 | 9164 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
22dc4a0f | 9165 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
9166 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
9167 | } | |
0c9a7a7e JK |
9168 | break; |
9169 | } | |
9170 | case RET_PTR_TO_BTF_ID: | |
9171 | { | |
c0a5a21c | 9172 | struct btf *ret_btf; |
af7ec138 YS |
9173 | int ret_btf_id; |
9174 | ||
9175 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
c25b2ae1 | 9176 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
c0a5a21c | 9177 | if (func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac KKD |
9178 | ret_btf = meta.kptr_field->kptr.btf; |
9179 | ret_btf_id = meta.kptr_field->kptr.btf_id; | |
738c96d5 DM |
9180 | if (!btf_is_kernel(ret_btf)) |
9181 | regs[BPF_REG_0].type |= MEM_ALLOC; | |
c0a5a21c | 9182 | } else { |
47e34cb7 DM |
9183 | if (fn->ret_btf_id == BPF_PTR_POISON) { |
9184 | verbose(env, "verifier internal error:"); | |
9185 | verbose(env, "func %s has non-overwritten BPF_PTR_POISON return type\n", | |
9186 | func_id_name(func_id)); | |
9187 | return -EINVAL; | |
9188 | } | |
c0a5a21c KKD |
9189 | ret_btf = btf_vmlinux; |
9190 | ret_btf_id = *fn->ret_btf_id; | |
9191 | } | |
af7ec138 | 9192 | if (ret_btf_id == 0) { |
3c480732 HL |
9193 | verbose(env, "invalid return type %u of func %s#%d\n", |
9194 | base_type(ret_type), func_id_name(func_id), | |
9195 | func_id); | |
af7ec138 YS |
9196 | return -EINVAL; |
9197 | } | |
c0a5a21c | 9198 | regs[BPF_REG_0].btf = ret_btf; |
af7ec138 | 9199 | regs[BPF_REG_0].btf_id = ret_btf_id; |
0c9a7a7e JK |
9200 | break; |
9201 | } | |
9202 | default: | |
3c480732 HL |
9203 | verbose(env, "unknown return type %u of func %s#%d\n", |
9204 | base_type(ret_type), func_id_name(func_id), func_id); | |
17a52670 AS |
9205 | return -EINVAL; |
9206 | } | |
04fd61ab | 9207 | |
c25b2ae1 | 9208 | if (type_may_be_null(regs[BPF_REG_0].type)) |
93c230e3 MKL |
9209 | regs[BPF_REG_0].id = ++env->id_gen; |
9210 | ||
b2d8ef19 DM |
9211 | if (helper_multiple_ref_obj_use(func_id, meta.map_ptr)) { |
9212 | verbose(env, "verifier internal error: func %s#%d sets ref_obj_id more than once\n", | |
9213 | func_id_name(func_id), func_id); | |
9214 | return -EFAULT; | |
9215 | } | |
9216 | ||
f8064ab9 KKD |
9217 | if (is_dynptr_ref_function(func_id)) |
9218 | regs[BPF_REG_0].dynptr_id = meta.dynptr_id; | |
9219 | ||
88374342 | 9220 | if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) { |
1b986589 MKL |
9221 | /* For release_reference() */ |
9222 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 9223 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
9224 | int id = acquire_reference_state(env, insn_idx); |
9225 | ||
9226 | if (id < 0) | |
9227 | return id; | |
9228 | /* For mark_ptr_or_null_reg() */ | |
9229 | regs[BPF_REG_0].id = id; | |
9230 | /* For release_reference() */ | |
9231 | regs[BPF_REG_0].ref_obj_id = id; | |
9232 | } | |
1b986589 | 9233 | |
849fa506 YS |
9234 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
9235 | ||
61bd5218 | 9236 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
9237 | if (err) |
9238 | return err; | |
04fd61ab | 9239 | |
fa28dcb8 SL |
9240 | if ((func_id == BPF_FUNC_get_stack || |
9241 | func_id == BPF_FUNC_get_task_stack) && | |
9242 | !env->prog->has_callchain_buf) { | |
c195651e YS |
9243 | const char *err_str; |
9244 | ||
9245 | #ifdef CONFIG_PERF_EVENTS | |
9246 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
9247 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
9248 | #else | |
9249 | err = -ENOTSUPP; | |
9250 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
9251 | #endif | |
9252 | if (err) { | |
9253 | verbose(env, err_str, func_id_name(func_id), func_id); | |
9254 | return err; | |
9255 | } | |
9256 | ||
9257 | env->prog->has_callchain_buf = true; | |
9258 | } | |
9259 | ||
5d99cb2c SL |
9260 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
9261 | env->prog->call_get_stack = true; | |
9262 | ||
9b99edca JO |
9263 | if (func_id == BPF_FUNC_get_func_ip) { |
9264 | if (check_get_func_ip(env)) | |
9265 | return -ENOTSUPP; | |
9266 | env->prog->call_get_func_ip = true; | |
9267 | } | |
9268 | ||
969bf05e AS |
9269 | if (changes_data) |
9270 | clear_all_pkt_pointers(env); | |
9271 | return 0; | |
9272 | } | |
9273 | ||
e6ac2450 MKL |
9274 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
9275 | * the BTF func_proto's return value size and argument. | |
9276 | */ | |
9277 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
9278 | size_t reg_size) | |
9279 | { | |
9280 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
9281 | ||
9282 | if (regno == BPF_REG_0) { | |
9283 | /* Function return value */ | |
9284 | reg->live |= REG_LIVE_WRITTEN; | |
9285 | reg->subreg_def = reg_size == sizeof(u64) ? | |
9286 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
9287 | } else { | |
9288 | /* Function argument */ | |
9289 | if (reg_size == sizeof(u64)) { | |
9290 | mark_insn_zext(env, reg); | |
9291 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
9292 | } else { | |
9293 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
9294 | } | |
9295 | } | |
9296 | } | |
9297 | ||
00b85860 KKD |
9298 | static bool is_kfunc_acquire(struct bpf_kfunc_call_arg_meta *meta) |
9299 | { | |
9300 | return meta->kfunc_flags & KF_ACQUIRE; | |
9301 | } | |
a5d82727 | 9302 | |
00b85860 KKD |
9303 | static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) |
9304 | { | |
9305 | return meta->kfunc_flags & KF_RET_NULL; | |
9306 | } | |
2357672c | 9307 | |
00b85860 KKD |
9308 | static bool is_kfunc_release(struct bpf_kfunc_call_arg_meta *meta) |
9309 | { | |
9310 | return meta->kfunc_flags & KF_RELEASE; | |
9311 | } | |
e6ac2450 | 9312 | |
00b85860 KKD |
9313 | static bool is_kfunc_trusted_args(struct bpf_kfunc_call_arg_meta *meta) |
9314 | { | |
6c831c46 | 9315 | return (meta->kfunc_flags & KF_TRUSTED_ARGS) || is_kfunc_release(meta); |
00b85860 | 9316 | } |
4dd48c6f | 9317 | |
00b85860 KKD |
9318 | static bool is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta) |
9319 | { | |
9320 | return meta->kfunc_flags & KF_SLEEPABLE; | |
9321 | } | |
5c073f26 | 9322 | |
00b85860 KKD |
9323 | static bool is_kfunc_destructive(struct bpf_kfunc_call_arg_meta *meta) |
9324 | { | |
9325 | return meta->kfunc_flags & KF_DESTRUCTIVE; | |
9326 | } | |
eb1f7f71 | 9327 | |
fca1aa75 YS |
9328 | static bool is_kfunc_rcu(struct bpf_kfunc_call_arg_meta *meta) |
9329 | { | |
9330 | return meta->kfunc_flags & KF_RCU; | |
9331 | } | |
9332 | ||
00b85860 KKD |
9333 | static bool is_kfunc_arg_kptr_get(struct bpf_kfunc_call_arg_meta *meta, int arg) |
9334 | { | |
9335 | return arg == 0 && (meta->kfunc_flags & KF_KPTR_GET); | |
9336 | } | |
e6ac2450 | 9337 | |
a50388db KKD |
9338 | static bool __kfunc_param_match_suffix(const struct btf *btf, |
9339 | const struct btf_param *arg, | |
9340 | const char *suffix) | |
00b85860 | 9341 | { |
a50388db | 9342 | int suffix_len = strlen(suffix), len; |
00b85860 | 9343 | const char *param_name; |
e6ac2450 | 9344 | |
00b85860 KKD |
9345 | /* In the future, this can be ported to use BTF tagging */ |
9346 | param_name = btf_name_by_offset(btf, arg->name_off); | |
9347 | if (str_is_empty(param_name)) | |
9348 | return false; | |
9349 | len = strlen(param_name); | |
a50388db | 9350 | if (len < suffix_len) |
00b85860 | 9351 | return false; |
a50388db KKD |
9352 | param_name += len - suffix_len; |
9353 | return !strncmp(param_name, suffix, suffix_len); | |
9354 | } | |
5c073f26 | 9355 | |
a50388db KKD |
9356 | static bool is_kfunc_arg_mem_size(const struct btf *btf, |
9357 | const struct btf_param *arg, | |
9358 | const struct bpf_reg_state *reg) | |
9359 | { | |
9360 | const struct btf_type *t; | |
5c073f26 | 9361 | |
a50388db KKD |
9362 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
9363 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
00b85860 | 9364 | return false; |
eb1f7f71 | 9365 | |
a50388db KKD |
9366 | return __kfunc_param_match_suffix(btf, arg, "__sz"); |
9367 | } | |
eb1f7f71 | 9368 | |
66e3a13e JK |
9369 | static bool is_kfunc_arg_const_mem_size(const struct btf *btf, |
9370 | const struct btf_param *arg, | |
9371 | const struct bpf_reg_state *reg) | |
9372 | { | |
9373 | const struct btf_type *t; | |
9374 | ||
9375 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
9376 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
9377 | return false; | |
9378 | ||
9379 | return __kfunc_param_match_suffix(btf, arg, "__szk"); | |
9380 | } | |
9381 | ||
a50388db KKD |
9382 | static bool is_kfunc_arg_constant(const struct btf *btf, const struct btf_param *arg) |
9383 | { | |
9384 | return __kfunc_param_match_suffix(btf, arg, "__k"); | |
00b85860 | 9385 | } |
eb1f7f71 | 9386 | |
958cf2e2 KKD |
9387 | static bool is_kfunc_arg_ignore(const struct btf *btf, const struct btf_param *arg) |
9388 | { | |
9389 | return __kfunc_param_match_suffix(btf, arg, "__ign"); | |
9390 | } | |
5c073f26 | 9391 | |
ac9f0605 KKD |
9392 | static bool is_kfunc_arg_alloc_obj(const struct btf *btf, const struct btf_param *arg) |
9393 | { | |
9394 | return __kfunc_param_match_suffix(btf, arg, "__alloc"); | |
9395 | } | |
e6ac2450 | 9396 | |
d96d937d JK |
9397 | static bool is_kfunc_arg_uninit(const struct btf *btf, const struct btf_param *arg) |
9398 | { | |
9399 | return __kfunc_param_match_suffix(btf, arg, "__uninit"); | |
9400 | } | |
9401 | ||
00b85860 KKD |
9402 | static bool is_kfunc_arg_scalar_with_name(const struct btf *btf, |
9403 | const struct btf_param *arg, | |
9404 | const char *name) | |
9405 | { | |
9406 | int len, target_len = strlen(name); | |
9407 | const char *param_name; | |
e6ac2450 | 9408 | |
00b85860 KKD |
9409 | param_name = btf_name_by_offset(btf, arg->name_off); |
9410 | if (str_is_empty(param_name)) | |
9411 | return false; | |
9412 | len = strlen(param_name); | |
9413 | if (len != target_len) | |
9414 | return false; | |
9415 | if (strcmp(param_name, name)) | |
9416 | return false; | |
e6ac2450 | 9417 | |
00b85860 | 9418 | return true; |
e6ac2450 MKL |
9419 | } |
9420 | ||
00b85860 KKD |
9421 | enum { |
9422 | KF_ARG_DYNPTR_ID, | |
8cab76ec KKD |
9423 | KF_ARG_LIST_HEAD_ID, |
9424 | KF_ARG_LIST_NODE_ID, | |
cd6791b4 DM |
9425 | KF_ARG_RB_ROOT_ID, |
9426 | KF_ARG_RB_NODE_ID, | |
00b85860 | 9427 | }; |
b03c9f9f | 9428 | |
00b85860 KKD |
9429 | BTF_ID_LIST(kf_arg_btf_ids) |
9430 | BTF_ID(struct, bpf_dynptr_kern) | |
8cab76ec KKD |
9431 | BTF_ID(struct, bpf_list_head) |
9432 | BTF_ID(struct, bpf_list_node) | |
bd1279ae DM |
9433 | BTF_ID(struct, bpf_rb_root) |
9434 | BTF_ID(struct, bpf_rb_node) | |
b03c9f9f | 9435 | |
8cab76ec KKD |
9436 | static bool __is_kfunc_ptr_arg_type(const struct btf *btf, |
9437 | const struct btf_param *arg, int type) | |
3f50f132 | 9438 | { |
00b85860 KKD |
9439 | const struct btf_type *t; |
9440 | u32 res_id; | |
3f50f132 | 9441 | |
00b85860 KKD |
9442 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
9443 | if (!t) | |
9444 | return false; | |
9445 | if (!btf_type_is_ptr(t)) | |
9446 | return false; | |
9447 | t = btf_type_skip_modifiers(btf, t->type, &res_id); | |
9448 | if (!t) | |
9449 | return false; | |
8cab76ec | 9450 | return btf_types_are_same(btf, res_id, btf_vmlinux, kf_arg_btf_ids[type]); |
3f50f132 JF |
9451 | } |
9452 | ||
8cab76ec | 9453 | static bool is_kfunc_arg_dynptr(const struct btf *btf, const struct btf_param *arg) |
b03c9f9f | 9454 | { |
8cab76ec | 9455 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_DYNPTR_ID); |
969bf05e AS |
9456 | } |
9457 | ||
8cab76ec | 9458 | static bool is_kfunc_arg_list_head(const struct btf *btf, const struct btf_param *arg) |
3f50f132 | 9459 | { |
8cab76ec | 9460 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_HEAD_ID); |
3f50f132 JF |
9461 | } |
9462 | ||
8cab76ec | 9463 | static bool is_kfunc_arg_list_node(const struct btf *btf, const struct btf_param *arg) |
bb7f0f98 | 9464 | { |
8cab76ec | 9465 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_NODE_ID); |
00b85860 KKD |
9466 | } |
9467 | ||
cd6791b4 DM |
9468 | static bool is_kfunc_arg_rbtree_root(const struct btf *btf, const struct btf_param *arg) |
9469 | { | |
9470 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_ROOT_ID); | |
9471 | } | |
9472 | ||
9473 | static bool is_kfunc_arg_rbtree_node(const struct btf *btf, const struct btf_param *arg) | |
9474 | { | |
9475 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_NODE_ID); | |
9476 | } | |
9477 | ||
5d92ddc3 DM |
9478 | static bool is_kfunc_arg_callback(struct bpf_verifier_env *env, const struct btf *btf, |
9479 | const struct btf_param *arg) | |
9480 | { | |
9481 | const struct btf_type *t; | |
9482 | ||
9483 | t = btf_type_resolve_func_ptr(btf, arg->type, NULL); | |
9484 | if (!t) | |
9485 | return false; | |
9486 | ||
9487 | return true; | |
9488 | } | |
9489 | ||
00b85860 KKD |
9490 | /* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ |
9491 | static bool __btf_type_is_scalar_struct(struct bpf_verifier_env *env, | |
9492 | const struct btf *btf, | |
9493 | const struct btf_type *t, int rec) | |
9494 | { | |
9495 | const struct btf_type *member_type; | |
9496 | const struct btf_member *member; | |
9497 | u32 i; | |
9498 | ||
9499 | if (!btf_type_is_struct(t)) | |
9500 | return false; | |
9501 | ||
9502 | for_each_member(i, t, member) { | |
9503 | const struct btf_array *array; | |
9504 | ||
9505 | member_type = btf_type_skip_modifiers(btf, member->type, NULL); | |
9506 | if (btf_type_is_struct(member_type)) { | |
9507 | if (rec >= 3) { | |
9508 | verbose(env, "max struct nesting depth exceeded\n"); | |
9509 | return false; | |
9510 | } | |
9511 | if (!__btf_type_is_scalar_struct(env, btf, member_type, rec + 1)) | |
9512 | return false; | |
9513 | continue; | |
9514 | } | |
9515 | if (btf_type_is_array(member_type)) { | |
9516 | array = btf_array(member_type); | |
9517 | if (!array->nelems) | |
9518 | return false; | |
9519 | member_type = btf_type_skip_modifiers(btf, array->type, NULL); | |
9520 | if (!btf_type_is_scalar(member_type)) | |
9521 | return false; | |
9522 | continue; | |
9523 | } | |
9524 | if (!btf_type_is_scalar(member_type)) | |
9525 | return false; | |
9526 | } | |
9527 | return true; | |
9528 | } | |
9529 | ||
9530 | ||
9531 | static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { | |
9532 | #ifdef CONFIG_NET | |
9533 | [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], | |
9534 | [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
9535 | [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], | |
9536 | #endif | |
9537 | }; | |
9538 | ||
9539 | enum kfunc_ptr_arg_type { | |
9540 | KF_ARG_PTR_TO_CTX, | |
ac9f0605 | 9541 | KF_ARG_PTR_TO_ALLOC_BTF_ID, /* Allocated object */ |
00b85860 KKD |
9542 | KF_ARG_PTR_TO_KPTR, /* PTR_TO_KPTR but type specific */ |
9543 | KF_ARG_PTR_TO_DYNPTR, | |
06accc87 | 9544 | KF_ARG_PTR_TO_ITER, |
8cab76ec KKD |
9545 | KF_ARG_PTR_TO_LIST_HEAD, |
9546 | KF_ARG_PTR_TO_LIST_NODE, | |
00b85860 KKD |
9547 | KF_ARG_PTR_TO_BTF_ID, /* Also covers reg2btf_ids conversions */ |
9548 | KF_ARG_PTR_TO_MEM, | |
9549 | KF_ARG_PTR_TO_MEM_SIZE, /* Size derived from next argument, skip it */ | |
5d92ddc3 | 9550 | KF_ARG_PTR_TO_CALLBACK, |
cd6791b4 DM |
9551 | KF_ARG_PTR_TO_RB_ROOT, |
9552 | KF_ARG_PTR_TO_RB_NODE, | |
00b85860 KKD |
9553 | }; |
9554 | ||
ac9f0605 KKD |
9555 | enum special_kfunc_type { |
9556 | KF_bpf_obj_new_impl, | |
9557 | KF_bpf_obj_drop_impl, | |
8cab76ec KKD |
9558 | KF_bpf_list_push_front, |
9559 | KF_bpf_list_push_back, | |
9560 | KF_bpf_list_pop_front, | |
9561 | KF_bpf_list_pop_back, | |
fd264ca0 | 9562 | KF_bpf_cast_to_kern_ctx, |
a35b9af4 | 9563 | KF_bpf_rdonly_cast, |
9bb00b28 YS |
9564 | KF_bpf_rcu_read_lock, |
9565 | KF_bpf_rcu_read_unlock, | |
bd1279ae DM |
9566 | KF_bpf_rbtree_remove, |
9567 | KF_bpf_rbtree_add, | |
9568 | KF_bpf_rbtree_first, | |
b5964b96 | 9569 | KF_bpf_dynptr_from_skb, |
05421aec | 9570 | KF_bpf_dynptr_from_xdp, |
66e3a13e JK |
9571 | KF_bpf_dynptr_slice, |
9572 | KF_bpf_dynptr_slice_rdwr, | |
ac9f0605 KKD |
9573 | }; |
9574 | ||
9575 | BTF_SET_START(special_kfunc_set) | |
9576 | BTF_ID(func, bpf_obj_new_impl) | |
9577 | BTF_ID(func, bpf_obj_drop_impl) | |
8cab76ec KKD |
9578 | BTF_ID(func, bpf_list_push_front) |
9579 | BTF_ID(func, bpf_list_push_back) | |
9580 | BTF_ID(func, bpf_list_pop_front) | |
9581 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 9582 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 9583 | BTF_ID(func, bpf_rdonly_cast) |
bd1279ae DM |
9584 | BTF_ID(func, bpf_rbtree_remove) |
9585 | BTF_ID(func, bpf_rbtree_add) | |
9586 | BTF_ID(func, bpf_rbtree_first) | |
b5964b96 | 9587 | BTF_ID(func, bpf_dynptr_from_skb) |
05421aec | 9588 | BTF_ID(func, bpf_dynptr_from_xdp) |
66e3a13e JK |
9589 | BTF_ID(func, bpf_dynptr_slice) |
9590 | BTF_ID(func, bpf_dynptr_slice_rdwr) | |
ac9f0605 KKD |
9591 | BTF_SET_END(special_kfunc_set) |
9592 | ||
9593 | BTF_ID_LIST(special_kfunc_list) | |
9594 | BTF_ID(func, bpf_obj_new_impl) | |
9595 | BTF_ID(func, bpf_obj_drop_impl) | |
8cab76ec KKD |
9596 | BTF_ID(func, bpf_list_push_front) |
9597 | BTF_ID(func, bpf_list_push_back) | |
9598 | BTF_ID(func, bpf_list_pop_front) | |
9599 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 9600 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 9601 | BTF_ID(func, bpf_rdonly_cast) |
9bb00b28 YS |
9602 | BTF_ID(func, bpf_rcu_read_lock) |
9603 | BTF_ID(func, bpf_rcu_read_unlock) | |
bd1279ae DM |
9604 | BTF_ID(func, bpf_rbtree_remove) |
9605 | BTF_ID(func, bpf_rbtree_add) | |
9606 | BTF_ID(func, bpf_rbtree_first) | |
b5964b96 | 9607 | BTF_ID(func, bpf_dynptr_from_skb) |
05421aec | 9608 | BTF_ID(func, bpf_dynptr_from_xdp) |
66e3a13e JK |
9609 | BTF_ID(func, bpf_dynptr_slice) |
9610 | BTF_ID(func, bpf_dynptr_slice_rdwr) | |
9bb00b28 YS |
9611 | |
9612 | static bool is_kfunc_bpf_rcu_read_lock(struct bpf_kfunc_call_arg_meta *meta) | |
9613 | { | |
9614 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_lock]; | |
9615 | } | |
9616 | ||
9617 | static bool is_kfunc_bpf_rcu_read_unlock(struct bpf_kfunc_call_arg_meta *meta) | |
9618 | { | |
9619 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_unlock]; | |
9620 | } | |
ac9f0605 | 9621 | |
00b85860 KKD |
9622 | static enum kfunc_ptr_arg_type |
9623 | get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, | |
9624 | struct bpf_kfunc_call_arg_meta *meta, | |
9625 | const struct btf_type *t, const struct btf_type *ref_t, | |
9626 | const char *ref_tname, const struct btf_param *args, | |
9627 | int argno, int nargs) | |
9628 | { | |
9629 | u32 regno = argno + 1; | |
9630 | struct bpf_reg_state *regs = cur_regs(env); | |
9631 | struct bpf_reg_state *reg = ®s[regno]; | |
9632 | bool arg_mem_size = false; | |
9633 | ||
fd264ca0 YS |
9634 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) |
9635 | return KF_ARG_PTR_TO_CTX; | |
9636 | ||
00b85860 KKD |
9637 | /* In this function, we verify the kfunc's BTF as per the argument type, |
9638 | * leaving the rest of the verification with respect to the register | |
9639 | * type to our caller. When a set of conditions hold in the BTF type of | |
9640 | * arguments, we resolve it to a known kfunc_ptr_arg_type. | |
9641 | */ | |
9642 | if (btf_get_prog_ctx_type(&env->log, meta->btf, t, resolve_prog_type(env->prog), argno)) | |
9643 | return KF_ARG_PTR_TO_CTX; | |
9644 | ||
ac9f0605 KKD |
9645 | if (is_kfunc_arg_alloc_obj(meta->btf, &args[argno])) |
9646 | return KF_ARG_PTR_TO_ALLOC_BTF_ID; | |
9647 | ||
00b85860 KKD |
9648 | if (is_kfunc_arg_kptr_get(meta, argno)) { |
9649 | if (!btf_type_is_ptr(ref_t)) { | |
9650 | verbose(env, "arg#0 BTF type must be a double pointer for kptr_get kfunc\n"); | |
9651 | return -EINVAL; | |
9652 | } | |
9653 | ref_t = btf_type_by_id(meta->btf, ref_t->type); | |
9654 | ref_tname = btf_name_by_offset(meta->btf, ref_t->name_off); | |
9655 | if (!btf_type_is_struct(ref_t)) { | |
9656 | verbose(env, "kernel function %s args#0 pointer type %s %s is not supported\n", | |
9657 | meta->func_name, btf_type_str(ref_t), ref_tname); | |
9658 | return -EINVAL; | |
9659 | } | |
9660 | return KF_ARG_PTR_TO_KPTR; | |
9661 | } | |
9662 | ||
9663 | if (is_kfunc_arg_dynptr(meta->btf, &args[argno])) | |
9664 | return KF_ARG_PTR_TO_DYNPTR; | |
9665 | ||
06accc87 AN |
9666 | if (is_kfunc_arg_iter(meta, argno)) |
9667 | return KF_ARG_PTR_TO_ITER; | |
9668 | ||
8cab76ec KKD |
9669 | if (is_kfunc_arg_list_head(meta->btf, &args[argno])) |
9670 | return KF_ARG_PTR_TO_LIST_HEAD; | |
9671 | ||
9672 | if (is_kfunc_arg_list_node(meta->btf, &args[argno])) | |
9673 | return KF_ARG_PTR_TO_LIST_NODE; | |
9674 | ||
cd6791b4 DM |
9675 | if (is_kfunc_arg_rbtree_root(meta->btf, &args[argno])) |
9676 | return KF_ARG_PTR_TO_RB_ROOT; | |
9677 | ||
9678 | if (is_kfunc_arg_rbtree_node(meta->btf, &args[argno])) | |
9679 | return KF_ARG_PTR_TO_RB_NODE; | |
9680 | ||
00b85860 KKD |
9681 | if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { |
9682 | if (!btf_type_is_struct(ref_t)) { | |
9683 | verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n", | |
9684 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
9685 | return -EINVAL; | |
9686 | } | |
9687 | return KF_ARG_PTR_TO_BTF_ID; | |
9688 | } | |
9689 | ||
5d92ddc3 DM |
9690 | if (is_kfunc_arg_callback(env, meta->btf, &args[argno])) |
9691 | return KF_ARG_PTR_TO_CALLBACK; | |
9692 | ||
66e3a13e JK |
9693 | |
9694 | if (argno + 1 < nargs && | |
9695 | (is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1]) || | |
9696 | is_kfunc_arg_const_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1]))) | |
00b85860 KKD |
9697 | arg_mem_size = true; |
9698 | ||
9699 | /* This is the catch all argument type of register types supported by | |
9700 | * check_helper_mem_access. However, we only allow when argument type is | |
9701 | * pointer to scalar, or struct composed (recursively) of scalars. When | |
9702 | * arg_mem_size is true, the pointer can be void *. | |
9703 | */ | |
9704 | if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(env, meta->btf, ref_t, 0) && | |
9705 | (arg_mem_size ? !btf_type_is_void(ref_t) : 1)) { | |
9706 | verbose(env, "arg#%d pointer type %s %s must point to %sscalar, or struct with scalar\n", | |
9707 | argno, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : ""); | |
9708 | return -EINVAL; | |
9709 | } | |
9710 | return arg_mem_size ? KF_ARG_PTR_TO_MEM_SIZE : KF_ARG_PTR_TO_MEM; | |
9711 | } | |
9712 | ||
9713 | static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env, | |
9714 | struct bpf_reg_state *reg, | |
9715 | const struct btf_type *ref_t, | |
9716 | const char *ref_tname, u32 ref_id, | |
9717 | struct bpf_kfunc_call_arg_meta *meta, | |
9718 | int argno) | |
9719 | { | |
9720 | const struct btf_type *reg_ref_t; | |
9721 | bool strict_type_match = false; | |
9722 | const struct btf *reg_btf; | |
9723 | const char *reg_ref_tname; | |
9724 | u32 reg_ref_id; | |
9725 | ||
3f00c523 | 9726 | if (base_type(reg->type) == PTR_TO_BTF_ID) { |
00b85860 KKD |
9727 | reg_btf = reg->btf; |
9728 | reg_ref_id = reg->btf_id; | |
9729 | } else { | |
9730 | reg_btf = btf_vmlinux; | |
9731 | reg_ref_id = *reg2btf_ids[base_type(reg->type)]; | |
9732 | } | |
9733 | ||
b613d335 DV |
9734 | /* Enforce strict type matching for calls to kfuncs that are acquiring |
9735 | * or releasing a reference, or are no-cast aliases. We do _not_ | |
9736 | * enforce strict matching for plain KF_TRUSTED_ARGS kfuncs by default, | |
9737 | * as we want to enable BPF programs to pass types that are bitwise | |
9738 | * equivalent without forcing them to explicitly cast with something | |
9739 | * like bpf_cast_to_kern_ctx(). | |
9740 | * | |
9741 | * For example, say we had a type like the following: | |
9742 | * | |
9743 | * struct bpf_cpumask { | |
9744 | * cpumask_t cpumask; | |
9745 | * refcount_t usage; | |
9746 | * }; | |
9747 | * | |
9748 | * Note that as specified in <linux/cpumask.h>, cpumask_t is typedef'ed | |
9749 | * to a struct cpumask, so it would be safe to pass a struct | |
9750 | * bpf_cpumask * to a kfunc expecting a struct cpumask *. | |
9751 | * | |
9752 | * The philosophy here is similar to how we allow scalars of different | |
9753 | * types to be passed to kfuncs as long as the size is the same. The | |
9754 | * only difference here is that we're simply allowing | |
9755 | * btf_struct_ids_match() to walk the struct at the 0th offset, and | |
9756 | * resolve types. | |
9757 | */ | |
9758 | if (is_kfunc_acquire(meta) || | |
9759 | (is_kfunc_release(meta) && reg->ref_obj_id) || | |
9760 | btf_type_ids_nocast_alias(&env->log, reg_btf, reg_ref_id, meta->btf, ref_id)) | |
00b85860 KKD |
9761 | strict_type_match = true; |
9762 | ||
b613d335 DV |
9763 | WARN_ON_ONCE(is_kfunc_trusted_args(meta) && reg->off); |
9764 | ||
00b85860 KKD |
9765 | reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id, ®_ref_id); |
9766 | reg_ref_tname = btf_name_by_offset(reg_btf, reg_ref_t->name_off); | |
9767 | if (!btf_struct_ids_match(&env->log, reg_btf, reg_ref_id, reg->off, meta->btf, ref_id, strict_type_match)) { | |
9768 | verbose(env, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", | |
9769 | meta->func_name, argno, btf_type_str(ref_t), ref_tname, argno + 1, | |
9770 | btf_type_str(reg_ref_t), reg_ref_tname); | |
9771 | return -EINVAL; | |
9772 | } | |
9773 | return 0; | |
9774 | } | |
9775 | ||
9776 | static int process_kf_arg_ptr_to_kptr(struct bpf_verifier_env *env, | |
9777 | struct bpf_reg_state *reg, | |
9778 | const struct btf_type *ref_t, | |
9779 | const char *ref_tname, | |
9780 | struct bpf_kfunc_call_arg_meta *meta, | |
9781 | int argno) | |
9782 | { | |
9783 | struct btf_field *kptr_field; | |
9784 | ||
9785 | /* check_func_arg_reg_off allows var_off for | |
9786 | * PTR_TO_MAP_VALUE, but we need fixed offset to find | |
9787 | * off_desc. | |
9788 | */ | |
9789 | if (!tnum_is_const(reg->var_off)) { | |
9790 | verbose(env, "arg#0 must have constant offset\n"); | |
9791 | return -EINVAL; | |
9792 | } | |
9793 | ||
9794 | kptr_field = btf_record_find(reg->map_ptr->record, reg->off + reg->var_off.value, BPF_KPTR); | |
9795 | if (!kptr_field || kptr_field->type != BPF_KPTR_REF) { | |
9796 | verbose(env, "arg#0 no referenced kptr at map value offset=%llu\n", | |
9797 | reg->off + reg->var_off.value); | |
9798 | return -EINVAL; | |
9799 | } | |
9800 | ||
9801 | if (!btf_struct_ids_match(&env->log, meta->btf, ref_t->type, 0, kptr_field->kptr.btf, | |
9802 | kptr_field->kptr.btf_id, true)) { | |
9803 | verbose(env, "kernel function %s args#%d expected pointer to %s %s\n", | |
9804 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
9805 | return -EINVAL; | |
9806 | } | |
9807 | return 0; | |
9808 | } | |
9809 | ||
6a3cd331 | 9810 | static int ref_set_non_owning(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
534e86bc | 9811 | { |
6a3cd331 DM |
9812 | struct bpf_verifier_state *state = env->cur_state; |
9813 | ||
9814 | if (!state->active_lock.ptr) { | |
9815 | verbose(env, "verifier internal error: ref_set_non_owning w/o active lock\n"); | |
9816 | return -EFAULT; | |
9817 | } | |
9818 | ||
9819 | if (type_flag(reg->type) & NON_OWN_REF) { | |
9820 | verbose(env, "verifier internal error: NON_OWN_REF already set\n"); | |
9821 | return -EFAULT; | |
9822 | } | |
9823 | ||
9824 | reg->type |= NON_OWN_REF; | |
9825 | return 0; | |
9826 | } | |
9827 | ||
9828 | static int ref_convert_owning_non_owning(struct bpf_verifier_env *env, u32 ref_obj_id) | |
9829 | { | |
9830 | struct bpf_func_state *state, *unused; | |
534e86bc KKD |
9831 | struct bpf_reg_state *reg; |
9832 | int i; | |
9833 | ||
6a3cd331 DM |
9834 | state = cur_func(env); |
9835 | ||
534e86bc | 9836 | if (!ref_obj_id) { |
6a3cd331 DM |
9837 | verbose(env, "verifier internal error: ref_obj_id is zero for " |
9838 | "owning -> non-owning conversion\n"); | |
534e86bc KKD |
9839 | return -EFAULT; |
9840 | } | |
6a3cd331 | 9841 | |
534e86bc | 9842 | for (i = 0; i < state->acquired_refs; i++) { |
6a3cd331 DM |
9843 | if (state->refs[i].id != ref_obj_id) |
9844 | continue; | |
9845 | ||
9846 | /* Clear ref_obj_id here so release_reference doesn't clobber | |
9847 | * the whole reg | |
9848 | */ | |
9849 | bpf_for_each_reg_in_vstate(env->cur_state, unused, reg, ({ | |
9850 | if (reg->ref_obj_id == ref_obj_id) { | |
9851 | reg->ref_obj_id = 0; | |
9852 | ref_set_non_owning(env, reg); | |
534e86bc | 9853 | } |
6a3cd331 DM |
9854 | })); |
9855 | return 0; | |
534e86bc | 9856 | } |
6a3cd331 | 9857 | |
534e86bc KKD |
9858 | verbose(env, "verifier internal error: ref state missing for ref_obj_id\n"); |
9859 | return -EFAULT; | |
9860 | } | |
9861 | ||
8cab76ec KKD |
9862 | /* Implementation details: |
9863 | * | |
9864 | * Each register points to some region of memory, which we define as an | |
9865 | * allocation. Each allocation may embed a bpf_spin_lock which protects any | |
9866 | * special BPF objects (bpf_list_head, bpf_rb_root, etc.) part of the same | |
9867 | * allocation. The lock and the data it protects are colocated in the same | |
9868 | * memory region. | |
9869 | * | |
9870 | * Hence, everytime a register holds a pointer value pointing to such | |
9871 | * allocation, the verifier preserves a unique reg->id for it. | |
9872 | * | |
9873 | * The verifier remembers the lock 'ptr' and the lock 'id' whenever | |
9874 | * bpf_spin_lock is called. | |
9875 | * | |
9876 | * To enable this, lock state in the verifier captures two values: | |
9877 | * active_lock.ptr = Register's type specific pointer | |
9878 | * active_lock.id = A unique ID for each register pointer value | |
9879 | * | |
9880 | * Currently, PTR_TO_MAP_VALUE and PTR_TO_BTF_ID | MEM_ALLOC are the two | |
9881 | * supported register types. | |
9882 | * | |
9883 | * The active_lock.ptr in case of map values is the reg->map_ptr, and in case of | |
9884 | * allocated objects is the reg->btf pointer. | |
9885 | * | |
9886 | * The active_lock.id is non-unique for maps supporting direct_value_addr, as we | |
9887 | * can establish the provenance of the map value statically for each distinct | |
9888 | * lookup into such maps. They always contain a single map value hence unique | |
9889 | * IDs for each pseudo load pessimizes the algorithm and rejects valid programs. | |
9890 | * | |
9891 | * So, in case of global variables, they use array maps with max_entries = 1, | |
9892 | * hence their active_lock.ptr becomes map_ptr and id = 0 (since they all point | |
9893 | * into the same map value as max_entries is 1, as described above). | |
9894 | * | |
9895 | * In case of inner map lookups, the inner map pointer has same map_ptr as the | |
9896 | * outer map pointer (in verifier context), but each lookup into an inner map | |
9897 | * assigns a fresh reg->id to the lookup, so while lookups into distinct inner | |
9898 | * maps from the same outer map share the same map_ptr as active_lock.ptr, they | |
9899 | * will get different reg->id assigned to each lookup, hence different | |
9900 | * active_lock.id. | |
9901 | * | |
9902 | * In case of allocated objects, active_lock.ptr is the reg->btf, and the | |
9903 | * reg->id is a unique ID preserved after the NULL pointer check on the pointer | |
9904 | * returned from bpf_obj_new. Each allocation receives a new reg->id. | |
9905 | */ | |
9906 | static int check_reg_allocation_locked(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
9907 | { | |
9908 | void *ptr; | |
9909 | u32 id; | |
9910 | ||
9911 | switch ((int)reg->type) { | |
9912 | case PTR_TO_MAP_VALUE: | |
9913 | ptr = reg->map_ptr; | |
9914 | break; | |
9915 | case PTR_TO_BTF_ID | MEM_ALLOC: | |
9916 | ptr = reg->btf; | |
9917 | break; | |
9918 | default: | |
9919 | verbose(env, "verifier internal error: unknown reg type for lock check\n"); | |
9920 | return -EFAULT; | |
9921 | } | |
9922 | id = reg->id; | |
9923 | ||
9924 | if (!env->cur_state->active_lock.ptr) | |
9925 | return -EINVAL; | |
9926 | if (env->cur_state->active_lock.ptr != ptr || | |
9927 | env->cur_state->active_lock.id != id) { | |
9928 | verbose(env, "held lock and object are not in the same allocation\n"); | |
9929 | return -EINVAL; | |
9930 | } | |
9931 | return 0; | |
9932 | } | |
9933 | ||
9934 | static bool is_bpf_list_api_kfunc(u32 btf_id) | |
9935 | { | |
9936 | return btf_id == special_kfunc_list[KF_bpf_list_push_front] || | |
9937 | btf_id == special_kfunc_list[KF_bpf_list_push_back] || | |
9938 | btf_id == special_kfunc_list[KF_bpf_list_pop_front] || | |
9939 | btf_id == special_kfunc_list[KF_bpf_list_pop_back]; | |
9940 | } | |
9941 | ||
cd6791b4 DM |
9942 | static bool is_bpf_rbtree_api_kfunc(u32 btf_id) |
9943 | { | |
9944 | return btf_id == special_kfunc_list[KF_bpf_rbtree_add] || | |
9945 | btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
9946 | btf_id == special_kfunc_list[KF_bpf_rbtree_first]; | |
9947 | } | |
9948 | ||
9949 | static bool is_bpf_graph_api_kfunc(u32 btf_id) | |
9950 | { | |
9951 | return is_bpf_list_api_kfunc(btf_id) || is_bpf_rbtree_api_kfunc(btf_id); | |
9952 | } | |
9953 | ||
5d92ddc3 DM |
9954 | static bool is_callback_calling_kfunc(u32 btf_id) |
9955 | { | |
9956 | return btf_id == special_kfunc_list[KF_bpf_rbtree_add]; | |
9957 | } | |
9958 | ||
9959 | static bool is_rbtree_lock_required_kfunc(u32 btf_id) | |
9960 | { | |
9961 | return is_bpf_rbtree_api_kfunc(btf_id); | |
9962 | } | |
9963 | ||
cd6791b4 DM |
9964 | static bool check_kfunc_is_graph_root_api(struct bpf_verifier_env *env, |
9965 | enum btf_field_type head_field_type, | |
9966 | u32 kfunc_btf_id) | |
9967 | { | |
9968 | bool ret; | |
9969 | ||
9970 | switch (head_field_type) { | |
9971 | case BPF_LIST_HEAD: | |
9972 | ret = is_bpf_list_api_kfunc(kfunc_btf_id); | |
9973 | break; | |
9974 | case BPF_RB_ROOT: | |
9975 | ret = is_bpf_rbtree_api_kfunc(kfunc_btf_id); | |
9976 | break; | |
9977 | default: | |
9978 | verbose(env, "verifier internal error: unexpected graph root argument type %s\n", | |
9979 | btf_field_type_name(head_field_type)); | |
9980 | return false; | |
9981 | } | |
9982 | ||
9983 | if (!ret) | |
9984 | verbose(env, "verifier internal error: %s head arg for unknown kfunc\n", | |
9985 | btf_field_type_name(head_field_type)); | |
9986 | return ret; | |
9987 | } | |
9988 | ||
9989 | static bool check_kfunc_is_graph_node_api(struct bpf_verifier_env *env, | |
9990 | enum btf_field_type node_field_type, | |
9991 | u32 kfunc_btf_id) | |
8cab76ec | 9992 | { |
cd6791b4 DM |
9993 | bool ret; |
9994 | ||
9995 | switch (node_field_type) { | |
9996 | case BPF_LIST_NODE: | |
9997 | ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_front] || | |
9998 | kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_back]); | |
9999 | break; | |
10000 | case BPF_RB_NODE: | |
10001 | ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
10002 | kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_add]); | |
10003 | break; | |
10004 | default: | |
10005 | verbose(env, "verifier internal error: unexpected graph node argument type %s\n", | |
10006 | btf_field_type_name(node_field_type)); | |
10007 | return false; | |
10008 | } | |
10009 | ||
10010 | if (!ret) | |
10011 | verbose(env, "verifier internal error: %s node arg for unknown kfunc\n", | |
10012 | btf_field_type_name(node_field_type)); | |
10013 | return ret; | |
10014 | } | |
10015 | ||
10016 | static int | |
10017 | __process_kf_arg_ptr_to_graph_root(struct bpf_verifier_env *env, | |
10018 | struct bpf_reg_state *reg, u32 regno, | |
10019 | struct bpf_kfunc_call_arg_meta *meta, | |
10020 | enum btf_field_type head_field_type, | |
10021 | struct btf_field **head_field) | |
10022 | { | |
10023 | const char *head_type_name; | |
8cab76ec KKD |
10024 | struct btf_field *field; |
10025 | struct btf_record *rec; | |
cd6791b4 | 10026 | u32 head_off; |
8cab76ec | 10027 | |
cd6791b4 DM |
10028 | if (meta->btf != btf_vmlinux) { |
10029 | verbose(env, "verifier internal error: unexpected btf mismatch in kfunc call\n"); | |
8cab76ec KKD |
10030 | return -EFAULT; |
10031 | } | |
10032 | ||
cd6791b4 DM |
10033 | if (!check_kfunc_is_graph_root_api(env, head_field_type, meta->func_id)) |
10034 | return -EFAULT; | |
10035 | ||
10036 | head_type_name = btf_field_type_name(head_field_type); | |
8cab76ec KKD |
10037 | if (!tnum_is_const(reg->var_off)) { |
10038 | verbose(env, | |
cd6791b4 DM |
10039 | "R%d doesn't have constant offset. %s has to be at the constant offset\n", |
10040 | regno, head_type_name); | |
8cab76ec KKD |
10041 | return -EINVAL; |
10042 | } | |
10043 | ||
10044 | rec = reg_btf_record(reg); | |
cd6791b4 DM |
10045 | head_off = reg->off + reg->var_off.value; |
10046 | field = btf_record_find(rec, head_off, head_field_type); | |
8cab76ec | 10047 | if (!field) { |
cd6791b4 | 10048 | verbose(env, "%s not found at offset=%u\n", head_type_name, head_off); |
8cab76ec KKD |
10049 | return -EINVAL; |
10050 | } | |
10051 | ||
10052 | /* All functions require bpf_list_head to be protected using a bpf_spin_lock */ | |
10053 | if (check_reg_allocation_locked(env, reg)) { | |
cd6791b4 DM |
10054 | verbose(env, "bpf_spin_lock at off=%d must be held for %s\n", |
10055 | rec->spin_lock_off, head_type_name); | |
8cab76ec KKD |
10056 | return -EINVAL; |
10057 | } | |
10058 | ||
cd6791b4 DM |
10059 | if (*head_field) { |
10060 | verbose(env, "verifier internal error: repeating %s arg\n", head_type_name); | |
8cab76ec KKD |
10061 | return -EFAULT; |
10062 | } | |
cd6791b4 | 10063 | *head_field = field; |
8cab76ec KKD |
10064 | return 0; |
10065 | } | |
10066 | ||
cd6791b4 | 10067 | static int process_kf_arg_ptr_to_list_head(struct bpf_verifier_env *env, |
8cab76ec KKD |
10068 | struct bpf_reg_state *reg, u32 regno, |
10069 | struct bpf_kfunc_call_arg_meta *meta) | |
10070 | { | |
cd6791b4 DM |
10071 | return __process_kf_arg_ptr_to_graph_root(env, reg, regno, meta, BPF_LIST_HEAD, |
10072 | &meta->arg_list_head.field); | |
10073 | } | |
10074 | ||
10075 | static int process_kf_arg_ptr_to_rbtree_root(struct bpf_verifier_env *env, | |
10076 | struct bpf_reg_state *reg, u32 regno, | |
10077 | struct bpf_kfunc_call_arg_meta *meta) | |
10078 | { | |
10079 | return __process_kf_arg_ptr_to_graph_root(env, reg, regno, meta, BPF_RB_ROOT, | |
10080 | &meta->arg_rbtree_root.field); | |
10081 | } | |
10082 | ||
10083 | static int | |
10084 | __process_kf_arg_ptr_to_graph_node(struct bpf_verifier_env *env, | |
10085 | struct bpf_reg_state *reg, u32 regno, | |
10086 | struct bpf_kfunc_call_arg_meta *meta, | |
10087 | enum btf_field_type head_field_type, | |
10088 | enum btf_field_type node_field_type, | |
10089 | struct btf_field **node_field) | |
10090 | { | |
10091 | const char *node_type_name; | |
8cab76ec KKD |
10092 | const struct btf_type *et, *t; |
10093 | struct btf_field *field; | |
cd6791b4 | 10094 | u32 node_off; |
8cab76ec | 10095 | |
cd6791b4 DM |
10096 | if (meta->btf != btf_vmlinux) { |
10097 | verbose(env, "verifier internal error: unexpected btf mismatch in kfunc call\n"); | |
8cab76ec KKD |
10098 | return -EFAULT; |
10099 | } | |
10100 | ||
cd6791b4 DM |
10101 | if (!check_kfunc_is_graph_node_api(env, node_field_type, meta->func_id)) |
10102 | return -EFAULT; | |
10103 | ||
10104 | node_type_name = btf_field_type_name(node_field_type); | |
8cab76ec KKD |
10105 | if (!tnum_is_const(reg->var_off)) { |
10106 | verbose(env, | |
cd6791b4 DM |
10107 | "R%d doesn't have constant offset. %s has to be at the constant offset\n", |
10108 | regno, node_type_name); | |
8cab76ec KKD |
10109 | return -EINVAL; |
10110 | } | |
10111 | ||
cd6791b4 DM |
10112 | node_off = reg->off + reg->var_off.value; |
10113 | field = reg_find_field_offset(reg, node_off, node_field_type); | |
10114 | if (!field || field->offset != node_off) { | |
10115 | verbose(env, "%s not found at offset=%u\n", node_type_name, node_off); | |
8cab76ec KKD |
10116 | return -EINVAL; |
10117 | } | |
10118 | ||
cd6791b4 | 10119 | field = *node_field; |
8cab76ec | 10120 | |
30465003 | 10121 | et = btf_type_by_id(field->graph_root.btf, field->graph_root.value_btf_id); |
8cab76ec | 10122 | t = btf_type_by_id(reg->btf, reg->btf_id); |
30465003 DM |
10123 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, 0, field->graph_root.btf, |
10124 | field->graph_root.value_btf_id, true)) { | |
cd6791b4 | 10125 | verbose(env, "operation on %s expects arg#1 %s at offset=%d " |
8cab76ec | 10126 | "in struct %s, but arg is at offset=%d in struct %s\n", |
cd6791b4 DM |
10127 | btf_field_type_name(head_field_type), |
10128 | btf_field_type_name(node_field_type), | |
30465003 DM |
10129 | field->graph_root.node_offset, |
10130 | btf_name_by_offset(field->graph_root.btf, et->name_off), | |
cd6791b4 | 10131 | node_off, btf_name_by_offset(reg->btf, t->name_off)); |
8cab76ec KKD |
10132 | return -EINVAL; |
10133 | } | |
10134 | ||
cd6791b4 DM |
10135 | if (node_off != field->graph_root.node_offset) { |
10136 | verbose(env, "arg#1 offset=%d, but expected %s at offset=%d in struct %s\n", | |
10137 | node_off, btf_field_type_name(node_field_type), | |
10138 | field->graph_root.node_offset, | |
30465003 | 10139 | btf_name_by_offset(field->graph_root.btf, et->name_off)); |
8cab76ec KKD |
10140 | return -EINVAL; |
10141 | } | |
6a3cd331 DM |
10142 | |
10143 | return 0; | |
8cab76ec KKD |
10144 | } |
10145 | ||
cd6791b4 DM |
10146 | static int process_kf_arg_ptr_to_list_node(struct bpf_verifier_env *env, |
10147 | struct bpf_reg_state *reg, u32 regno, | |
10148 | struct bpf_kfunc_call_arg_meta *meta) | |
10149 | { | |
10150 | return __process_kf_arg_ptr_to_graph_node(env, reg, regno, meta, | |
10151 | BPF_LIST_HEAD, BPF_LIST_NODE, | |
10152 | &meta->arg_list_head.field); | |
10153 | } | |
10154 | ||
10155 | static int process_kf_arg_ptr_to_rbtree_node(struct bpf_verifier_env *env, | |
10156 | struct bpf_reg_state *reg, u32 regno, | |
10157 | struct bpf_kfunc_call_arg_meta *meta) | |
10158 | { | |
10159 | return __process_kf_arg_ptr_to_graph_node(env, reg, regno, meta, | |
10160 | BPF_RB_ROOT, BPF_RB_NODE, | |
10161 | &meta->arg_rbtree_root.field); | |
10162 | } | |
10163 | ||
1d18feb2 JK |
10164 | static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta, |
10165 | int insn_idx) | |
00b85860 KKD |
10166 | { |
10167 | const char *func_name = meta->func_name, *ref_tname; | |
10168 | const struct btf *btf = meta->btf; | |
10169 | const struct btf_param *args; | |
10170 | u32 i, nargs; | |
10171 | int ret; | |
10172 | ||
10173 | args = (const struct btf_param *)(meta->func_proto + 1); | |
10174 | nargs = btf_type_vlen(meta->func_proto); | |
10175 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { | |
10176 | verbose(env, "Function %s has %d > %d args\n", func_name, nargs, | |
10177 | MAX_BPF_FUNC_REG_ARGS); | |
10178 | return -EINVAL; | |
10179 | } | |
10180 | ||
10181 | /* Check that BTF function arguments match actual types that the | |
10182 | * verifier sees. | |
10183 | */ | |
10184 | for (i = 0; i < nargs; i++) { | |
10185 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[i + 1]; | |
10186 | const struct btf_type *t, *ref_t, *resolve_ret; | |
10187 | enum bpf_arg_type arg_type = ARG_DONTCARE; | |
10188 | u32 regno = i + 1, ref_id, type_size; | |
10189 | bool is_ret_buf_sz = false; | |
10190 | int kf_arg_type; | |
10191 | ||
10192 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); | |
958cf2e2 KKD |
10193 | |
10194 | if (is_kfunc_arg_ignore(btf, &args[i])) | |
10195 | continue; | |
10196 | ||
00b85860 KKD |
10197 | if (btf_type_is_scalar(t)) { |
10198 | if (reg->type != SCALAR_VALUE) { | |
10199 | verbose(env, "R%d is not a scalar\n", regno); | |
10200 | return -EINVAL; | |
10201 | } | |
a50388db KKD |
10202 | |
10203 | if (is_kfunc_arg_constant(meta->btf, &args[i])) { | |
10204 | if (meta->arg_constant.found) { | |
10205 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
10206 | return -EFAULT; | |
10207 | } | |
10208 | if (!tnum_is_const(reg->var_off)) { | |
10209 | verbose(env, "R%d must be a known constant\n", regno); | |
10210 | return -EINVAL; | |
10211 | } | |
10212 | ret = mark_chain_precision(env, regno); | |
10213 | if (ret < 0) | |
10214 | return ret; | |
10215 | meta->arg_constant.found = true; | |
10216 | meta->arg_constant.value = reg->var_off.value; | |
10217 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) { | |
00b85860 KKD |
10218 | meta->r0_rdonly = true; |
10219 | is_ret_buf_sz = true; | |
10220 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) { | |
10221 | is_ret_buf_sz = true; | |
10222 | } | |
10223 | ||
10224 | if (is_ret_buf_sz) { | |
10225 | if (meta->r0_size) { | |
10226 | verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc"); | |
10227 | return -EINVAL; | |
10228 | } | |
10229 | ||
10230 | if (!tnum_is_const(reg->var_off)) { | |
10231 | verbose(env, "R%d is not a const\n", regno); | |
10232 | return -EINVAL; | |
10233 | } | |
10234 | ||
10235 | meta->r0_size = reg->var_off.value; | |
10236 | ret = mark_chain_precision(env, regno); | |
10237 | if (ret) | |
10238 | return ret; | |
10239 | } | |
10240 | continue; | |
10241 | } | |
10242 | ||
10243 | if (!btf_type_is_ptr(t)) { | |
10244 | verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t)); | |
10245 | return -EINVAL; | |
10246 | } | |
10247 | ||
20c09d92 | 10248 | if ((is_kfunc_trusted_args(meta) || is_kfunc_rcu(meta)) && |
caf713c3 DV |
10249 | (register_is_null(reg) || type_may_be_null(reg->type))) { |
10250 | verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i); | |
10251 | return -EACCES; | |
10252 | } | |
10253 | ||
00b85860 KKD |
10254 | if (reg->ref_obj_id) { |
10255 | if (is_kfunc_release(meta) && meta->ref_obj_id) { | |
10256 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
10257 | regno, reg->ref_obj_id, | |
10258 | meta->ref_obj_id); | |
10259 | return -EFAULT; | |
10260 | } | |
10261 | meta->ref_obj_id = reg->ref_obj_id; | |
10262 | if (is_kfunc_release(meta)) | |
10263 | meta->release_regno = regno; | |
10264 | } | |
10265 | ||
10266 | ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); | |
10267 | ref_tname = btf_name_by_offset(btf, ref_t->name_off); | |
10268 | ||
10269 | kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs); | |
10270 | if (kf_arg_type < 0) | |
10271 | return kf_arg_type; | |
10272 | ||
10273 | switch (kf_arg_type) { | |
ac9f0605 | 10274 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
00b85860 | 10275 | case KF_ARG_PTR_TO_BTF_ID: |
fca1aa75 | 10276 | if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta)) |
00b85860 | 10277 | break; |
3f00c523 DV |
10278 | |
10279 | if (!is_trusted_reg(reg)) { | |
fca1aa75 YS |
10280 | if (!is_kfunc_rcu(meta)) { |
10281 | verbose(env, "R%d must be referenced or trusted\n", regno); | |
10282 | return -EINVAL; | |
10283 | } | |
10284 | if (!is_rcu_reg(reg)) { | |
10285 | verbose(env, "R%d must be a rcu pointer\n", regno); | |
10286 | return -EINVAL; | |
10287 | } | |
00b85860 | 10288 | } |
fca1aa75 | 10289 | |
00b85860 KKD |
10290 | fallthrough; |
10291 | case KF_ARG_PTR_TO_CTX: | |
10292 | /* Trusted arguments have the same offset checks as release arguments */ | |
10293 | arg_type |= OBJ_RELEASE; | |
10294 | break; | |
10295 | case KF_ARG_PTR_TO_KPTR: | |
10296 | case KF_ARG_PTR_TO_DYNPTR: | |
06accc87 | 10297 | case KF_ARG_PTR_TO_ITER: |
8cab76ec KKD |
10298 | case KF_ARG_PTR_TO_LIST_HEAD: |
10299 | case KF_ARG_PTR_TO_LIST_NODE: | |
cd6791b4 DM |
10300 | case KF_ARG_PTR_TO_RB_ROOT: |
10301 | case KF_ARG_PTR_TO_RB_NODE: | |
00b85860 KKD |
10302 | case KF_ARG_PTR_TO_MEM: |
10303 | case KF_ARG_PTR_TO_MEM_SIZE: | |
5d92ddc3 | 10304 | case KF_ARG_PTR_TO_CALLBACK: |
00b85860 KKD |
10305 | /* Trusted by default */ |
10306 | break; | |
10307 | default: | |
10308 | WARN_ON_ONCE(1); | |
10309 | return -EFAULT; | |
10310 | } | |
10311 | ||
10312 | if (is_kfunc_release(meta) && reg->ref_obj_id) | |
10313 | arg_type |= OBJ_RELEASE; | |
10314 | ret = check_func_arg_reg_off(env, reg, regno, arg_type); | |
10315 | if (ret < 0) | |
10316 | return ret; | |
10317 | ||
10318 | switch (kf_arg_type) { | |
10319 | case KF_ARG_PTR_TO_CTX: | |
10320 | if (reg->type != PTR_TO_CTX) { | |
10321 | verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t)); | |
10322 | return -EINVAL; | |
10323 | } | |
fd264ca0 YS |
10324 | |
10325 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { | |
10326 | ret = get_kern_ctx_btf_id(&env->log, resolve_prog_type(env->prog)); | |
10327 | if (ret < 0) | |
10328 | return -EINVAL; | |
10329 | meta->ret_btf_id = ret; | |
10330 | } | |
00b85860 | 10331 | break; |
ac9f0605 KKD |
10332 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
10333 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10334 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10335 | return -EINVAL; | |
10336 | } | |
10337 | if (!reg->ref_obj_id) { | |
10338 | verbose(env, "allocated object must be referenced\n"); | |
10339 | return -EINVAL; | |
10340 | } | |
10341 | if (meta->btf == btf_vmlinux && | |
10342 | meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { | |
10343 | meta->arg_obj_drop.btf = reg->btf; | |
10344 | meta->arg_obj_drop.btf_id = reg->btf_id; | |
10345 | } | |
10346 | break; | |
00b85860 KKD |
10347 | case KF_ARG_PTR_TO_KPTR: |
10348 | if (reg->type != PTR_TO_MAP_VALUE) { | |
10349 | verbose(env, "arg#0 expected pointer to map value\n"); | |
10350 | return -EINVAL; | |
10351 | } | |
10352 | ret = process_kf_arg_ptr_to_kptr(env, reg, ref_t, ref_tname, meta, i); | |
10353 | if (ret < 0) | |
10354 | return ret; | |
10355 | break; | |
10356 | case KF_ARG_PTR_TO_DYNPTR: | |
d96d937d JK |
10357 | { |
10358 | enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR; | |
10359 | ||
6b75bd3d | 10360 | if (reg->type != PTR_TO_STACK && |
27060531 | 10361 | reg->type != CONST_PTR_TO_DYNPTR) { |
6b75bd3d | 10362 | verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i); |
00b85860 KKD |
10363 | return -EINVAL; |
10364 | } | |
10365 | ||
d96d937d JK |
10366 | if (reg->type == CONST_PTR_TO_DYNPTR) |
10367 | dynptr_arg_type |= MEM_RDONLY; | |
10368 | ||
10369 | if (is_kfunc_arg_uninit(btf, &args[i])) | |
10370 | dynptr_arg_type |= MEM_UNINIT; | |
10371 | ||
b5964b96 JK |
10372 | if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) |
10373 | dynptr_arg_type |= DYNPTR_TYPE_SKB; | |
05421aec JK |
10374 | else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp]) |
10375 | dynptr_arg_type |= DYNPTR_TYPE_XDP; | |
b5964b96 | 10376 | |
d96d937d | 10377 | ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type); |
6b75bd3d KKD |
10378 | if (ret < 0) |
10379 | return ret; | |
66e3a13e JK |
10380 | |
10381 | if (!(dynptr_arg_type & MEM_UNINIT)) { | |
10382 | int id = dynptr_id(env, reg); | |
10383 | ||
10384 | if (id < 0) { | |
10385 | verbose(env, "verifier internal error: failed to obtain dynptr id\n"); | |
10386 | return id; | |
10387 | } | |
10388 | meta->initialized_dynptr.id = id; | |
10389 | meta->initialized_dynptr.type = dynptr_get_type(env, reg); | |
10390 | } | |
10391 | ||
00b85860 | 10392 | break; |
d96d937d | 10393 | } |
06accc87 AN |
10394 | case KF_ARG_PTR_TO_ITER: |
10395 | ret = process_iter_arg(env, regno, insn_idx, meta); | |
10396 | if (ret < 0) | |
10397 | return ret; | |
10398 | break; | |
8cab76ec KKD |
10399 | case KF_ARG_PTR_TO_LIST_HEAD: |
10400 | if (reg->type != PTR_TO_MAP_VALUE && | |
10401 | reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10402 | verbose(env, "arg#%d expected pointer to map value or allocated object\n", i); | |
10403 | return -EINVAL; | |
10404 | } | |
10405 | if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) { | |
10406 | verbose(env, "allocated object must be referenced\n"); | |
10407 | return -EINVAL; | |
10408 | } | |
10409 | ret = process_kf_arg_ptr_to_list_head(env, reg, regno, meta); | |
10410 | if (ret < 0) | |
10411 | return ret; | |
10412 | break; | |
cd6791b4 DM |
10413 | case KF_ARG_PTR_TO_RB_ROOT: |
10414 | if (reg->type != PTR_TO_MAP_VALUE && | |
10415 | reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10416 | verbose(env, "arg#%d expected pointer to map value or allocated object\n", i); | |
10417 | return -EINVAL; | |
10418 | } | |
10419 | if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) { | |
10420 | verbose(env, "allocated object must be referenced\n"); | |
10421 | return -EINVAL; | |
10422 | } | |
10423 | ret = process_kf_arg_ptr_to_rbtree_root(env, reg, regno, meta); | |
10424 | if (ret < 0) | |
10425 | return ret; | |
10426 | break; | |
8cab76ec KKD |
10427 | case KF_ARG_PTR_TO_LIST_NODE: |
10428 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10429 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10430 | return -EINVAL; | |
10431 | } | |
10432 | if (!reg->ref_obj_id) { | |
10433 | verbose(env, "allocated object must be referenced\n"); | |
10434 | return -EINVAL; | |
10435 | } | |
10436 | ret = process_kf_arg_ptr_to_list_node(env, reg, regno, meta); | |
10437 | if (ret < 0) | |
10438 | return ret; | |
10439 | break; | |
cd6791b4 | 10440 | case KF_ARG_PTR_TO_RB_NODE: |
a40d3632 DM |
10441 | if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_remove]) { |
10442 | if (!type_is_non_owning_ref(reg->type) || reg->ref_obj_id) { | |
10443 | verbose(env, "rbtree_remove node input must be non-owning ref\n"); | |
10444 | return -EINVAL; | |
10445 | } | |
10446 | if (in_rbtree_lock_required_cb(env)) { | |
10447 | verbose(env, "rbtree_remove not allowed in rbtree cb\n"); | |
10448 | return -EINVAL; | |
10449 | } | |
10450 | } else { | |
10451 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10452 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10453 | return -EINVAL; | |
10454 | } | |
10455 | if (!reg->ref_obj_id) { | |
10456 | verbose(env, "allocated object must be referenced\n"); | |
10457 | return -EINVAL; | |
10458 | } | |
cd6791b4 | 10459 | } |
a40d3632 | 10460 | |
cd6791b4 DM |
10461 | ret = process_kf_arg_ptr_to_rbtree_node(env, reg, regno, meta); |
10462 | if (ret < 0) | |
10463 | return ret; | |
10464 | break; | |
00b85860 KKD |
10465 | case KF_ARG_PTR_TO_BTF_ID: |
10466 | /* Only base_type is checked, further checks are done here */ | |
3f00c523 | 10467 | if ((base_type(reg->type) != PTR_TO_BTF_ID || |
fca1aa75 | 10468 | (bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) && |
3f00c523 DV |
10469 | !reg2btf_ids[base_type(reg->type)]) { |
10470 | verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type)); | |
10471 | verbose(env, "expected %s or socket\n", | |
10472 | reg_type_str(env, base_type(reg->type) | | |
10473 | (type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS))); | |
00b85860 KKD |
10474 | return -EINVAL; |
10475 | } | |
10476 | ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i); | |
10477 | if (ret < 0) | |
10478 | return ret; | |
10479 | break; | |
10480 | case KF_ARG_PTR_TO_MEM: | |
10481 | resolve_ret = btf_resolve_size(btf, ref_t, &type_size); | |
10482 | if (IS_ERR(resolve_ret)) { | |
10483 | verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n", | |
10484 | i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret)); | |
10485 | return -EINVAL; | |
10486 | } | |
10487 | ret = check_mem_reg(env, reg, regno, type_size); | |
10488 | if (ret < 0) | |
10489 | return ret; | |
10490 | break; | |
10491 | case KF_ARG_PTR_TO_MEM_SIZE: | |
66e3a13e JK |
10492 | { |
10493 | struct bpf_reg_state *size_reg = ®s[regno + 1]; | |
10494 | const struct btf_param *size_arg = &args[i + 1]; | |
10495 | ||
10496 | ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1); | |
00b85860 KKD |
10497 | if (ret < 0) { |
10498 | verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1); | |
10499 | return ret; | |
10500 | } | |
66e3a13e JK |
10501 | |
10502 | if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) { | |
10503 | if (meta->arg_constant.found) { | |
10504 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
10505 | return -EFAULT; | |
10506 | } | |
10507 | if (!tnum_is_const(size_reg->var_off)) { | |
10508 | verbose(env, "R%d must be a known constant\n", regno + 1); | |
10509 | return -EINVAL; | |
10510 | } | |
10511 | meta->arg_constant.found = true; | |
10512 | meta->arg_constant.value = size_reg->var_off.value; | |
10513 | } | |
10514 | ||
10515 | /* Skip next '__sz' or '__szk' argument */ | |
00b85860 KKD |
10516 | i++; |
10517 | break; | |
66e3a13e | 10518 | } |
5d92ddc3 DM |
10519 | case KF_ARG_PTR_TO_CALLBACK: |
10520 | meta->subprogno = reg->subprogno; | |
10521 | break; | |
00b85860 KKD |
10522 | } |
10523 | } | |
10524 | ||
10525 | if (is_kfunc_release(meta) && !meta->release_regno) { | |
10526 | verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n", | |
10527 | func_name); | |
10528 | return -EINVAL; | |
10529 | } | |
10530 | ||
10531 | return 0; | |
10532 | } | |
10533 | ||
07236eab AN |
10534 | static int fetch_kfunc_meta(struct bpf_verifier_env *env, |
10535 | struct bpf_insn *insn, | |
10536 | struct bpf_kfunc_call_arg_meta *meta, | |
10537 | const char **kfunc_name) | |
e6ac2450 | 10538 | { |
07236eab AN |
10539 | const struct btf_type *func, *func_proto; |
10540 | u32 func_id, *kfunc_flags; | |
10541 | const char *func_name; | |
2357672c | 10542 | struct btf *desc_btf; |
e6ac2450 | 10543 | |
07236eab AN |
10544 | if (kfunc_name) |
10545 | *kfunc_name = NULL; | |
10546 | ||
a5d82727 | 10547 | if (!insn->imm) |
07236eab | 10548 | return -EINVAL; |
a5d82727 | 10549 | |
43bf0878 | 10550 | desc_btf = find_kfunc_desc_btf(env, insn->off); |
2357672c KKD |
10551 | if (IS_ERR(desc_btf)) |
10552 | return PTR_ERR(desc_btf); | |
10553 | ||
e6ac2450 | 10554 | func_id = insn->imm; |
2357672c KKD |
10555 | func = btf_type_by_id(desc_btf, func_id); |
10556 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
07236eab AN |
10557 | if (kfunc_name) |
10558 | *kfunc_name = func_name; | |
2357672c | 10559 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 | 10560 | |
a4703e31 KKD |
10561 | kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id); |
10562 | if (!kfunc_flags) { | |
e6ac2450 MKL |
10563 | return -EACCES; |
10564 | } | |
00b85860 | 10565 | |
07236eab AN |
10566 | memset(meta, 0, sizeof(*meta)); |
10567 | meta->btf = desc_btf; | |
10568 | meta->func_id = func_id; | |
10569 | meta->kfunc_flags = *kfunc_flags; | |
10570 | meta->func_proto = func_proto; | |
10571 | meta->func_name = func_name; | |
10572 | ||
10573 | return 0; | |
10574 | } | |
10575 | ||
10576 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
10577 | int *insn_idx_p) | |
10578 | { | |
10579 | const struct btf_type *t, *ptr_type; | |
10580 | u32 i, nargs, ptr_type_id, release_ref_obj_id; | |
10581 | struct bpf_reg_state *regs = cur_regs(env); | |
10582 | const char *func_name, *ptr_type_name; | |
10583 | bool sleepable, rcu_lock, rcu_unlock; | |
10584 | struct bpf_kfunc_call_arg_meta meta; | |
10585 | struct bpf_insn_aux_data *insn_aux; | |
10586 | int err, insn_idx = *insn_idx_p; | |
10587 | const struct btf_param *args; | |
10588 | const struct btf_type *ret_t; | |
10589 | struct btf *desc_btf; | |
10590 | ||
10591 | /* skip for now, but return error when we find this in fixup_kfunc_call */ | |
10592 | if (!insn->imm) | |
10593 | return 0; | |
10594 | ||
10595 | err = fetch_kfunc_meta(env, insn, &meta, &func_name); | |
10596 | if (err == -EACCES && func_name) | |
10597 | verbose(env, "calling kernel function %s is not allowed\n", func_name); | |
10598 | if (err) | |
10599 | return err; | |
10600 | desc_btf = meta.btf; | |
10601 | insn_aux = &env->insn_aux_data[insn_idx]; | |
00b85860 | 10602 | |
06accc87 AN |
10603 | insn_aux->is_iter_next = is_iter_next_kfunc(&meta); |
10604 | ||
00b85860 KKD |
10605 | if (is_kfunc_destructive(&meta) && !capable(CAP_SYS_BOOT)) { |
10606 | verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capability\n"); | |
4dd48c6f AS |
10607 | return -EACCES; |
10608 | } | |
10609 | ||
9bb00b28 YS |
10610 | sleepable = is_kfunc_sleepable(&meta); |
10611 | if (sleepable && !env->prog->aux->sleepable) { | |
00b85860 KKD |
10612 | verbose(env, "program must be sleepable to call sleepable kfunc %s\n", func_name); |
10613 | return -EACCES; | |
10614 | } | |
eb1f7f71 | 10615 | |
9bb00b28 YS |
10616 | rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta); |
10617 | rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta); | |
9bb00b28 YS |
10618 | |
10619 | if (env->cur_state->active_rcu_lock) { | |
10620 | struct bpf_func_state *state; | |
10621 | struct bpf_reg_state *reg; | |
10622 | ||
10623 | if (rcu_lock) { | |
10624 | verbose(env, "nested rcu read lock (kernel function %s)\n", func_name); | |
10625 | return -EINVAL; | |
10626 | } else if (rcu_unlock) { | |
10627 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ | |
10628 | if (reg->type & MEM_RCU) { | |
fca1aa75 | 10629 | reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL); |
9bb00b28 YS |
10630 | reg->type |= PTR_UNTRUSTED; |
10631 | } | |
10632 | })); | |
10633 | env->cur_state->active_rcu_lock = false; | |
10634 | } else if (sleepable) { | |
10635 | verbose(env, "kernel func %s is sleepable within rcu_read_lock region\n", func_name); | |
10636 | return -EACCES; | |
10637 | } | |
10638 | } else if (rcu_lock) { | |
10639 | env->cur_state->active_rcu_lock = true; | |
10640 | } else if (rcu_unlock) { | |
10641 | verbose(env, "unmatched rcu read unlock (kernel function %s)\n", func_name); | |
10642 | return -EINVAL; | |
10643 | } | |
10644 | ||
e6ac2450 | 10645 | /* Check the arguments */ |
1d18feb2 | 10646 | err = check_kfunc_args(env, &meta, insn_idx); |
5c073f26 | 10647 | if (err < 0) |
e6ac2450 | 10648 | return err; |
5c073f26 | 10649 | /* In case of release function, we get register number of refcounted |
00b85860 | 10650 | * PTR_TO_BTF_ID in bpf_kfunc_arg_meta, do the release now. |
5c073f26 | 10651 | */ |
00b85860 KKD |
10652 | if (meta.release_regno) { |
10653 | err = release_reference(env, regs[meta.release_regno].ref_obj_id); | |
5c073f26 KKD |
10654 | if (err) { |
10655 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
07236eab | 10656 | func_name, meta.func_id); |
5c073f26 KKD |
10657 | return err; |
10658 | } | |
10659 | } | |
e6ac2450 | 10660 | |
6a3cd331 | 10661 | if (meta.func_id == special_kfunc_list[KF_bpf_list_push_front] || |
bd1279ae DM |
10662 | meta.func_id == special_kfunc_list[KF_bpf_list_push_back] || |
10663 | meta.func_id == special_kfunc_list[KF_bpf_rbtree_add]) { | |
6a3cd331 DM |
10664 | release_ref_obj_id = regs[BPF_REG_2].ref_obj_id; |
10665 | err = ref_convert_owning_non_owning(env, release_ref_obj_id); | |
10666 | if (err) { | |
10667 | verbose(env, "kfunc %s#%d conversion of owning ref to non-owning failed\n", | |
07236eab | 10668 | func_name, meta.func_id); |
6a3cd331 DM |
10669 | return err; |
10670 | } | |
10671 | ||
10672 | err = release_reference(env, release_ref_obj_id); | |
10673 | if (err) { | |
10674 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
07236eab | 10675 | func_name, meta.func_id); |
6a3cd331 DM |
10676 | return err; |
10677 | } | |
10678 | } | |
10679 | ||
5d92ddc3 DM |
10680 | if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_add]) { |
10681 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
10682 | set_rbtree_add_callback_state); | |
10683 | if (err) { | |
10684 | verbose(env, "kfunc %s#%d failed callback verification\n", | |
07236eab | 10685 | func_name, meta.func_id); |
5d92ddc3 DM |
10686 | return err; |
10687 | } | |
10688 | } | |
10689 | ||
e6ac2450 MKL |
10690 | for (i = 0; i < CALLER_SAVED_REGS; i++) |
10691 | mark_reg_not_init(env, regs, caller_saved[i]); | |
10692 | ||
10693 | /* Check return type */ | |
07236eab | 10694 | t = btf_type_skip_modifiers(desc_btf, meta.func_proto->type, NULL); |
5c073f26 | 10695 | |
00b85860 | 10696 | if (is_kfunc_acquire(&meta) && !btf_type_is_struct_ptr(meta.btf, t)) { |
958cf2e2 KKD |
10697 | /* Only exception is bpf_obj_new_impl */ |
10698 | if (meta.btf != btf_vmlinux || meta.func_id != special_kfunc_list[KF_bpf_obj_new_impl]) { | |
10699 | verbose(env, "acquire kernel function does not return PTR_TO_BTF_ID\n"); | |
10700 | return -EINVAL; | |
10701 | } | |
5c073f26 KKD |
10702 | } |
10703 | ||
e6ac2450 MKL |
10704 | if (btf_type_is_scalar(t)) { |
10705 | mark_reg_unknown(env, regs, BPF_REG_0); | |
10706 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
10707 | } else if (btf_type_is_ptr(t)) { | |
958cf2e2 KKD |
10708 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, &ptr_type_id); |
10709 | ||
10710 | if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) { | |
10711 | if (meta.func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
958cf2e2 KKD |
10712 | struct btf *ret_btf; |
10713 | u32 ret_btf_id; | |
10714 | ||
e181d3f1 KKD |
10715 | if (unlikely(!bpf_global_ma_set)) |
10716 | return -ENOMEM; | |
10717 | ||
958cf2e2 KKD |
10718 | if (((u64)(u32)meta.arg_constant.value) != meta.arg_constant.value) { |
10719 | verbose(env, "local type ID argument must be in range [0, U32_MAX]\n"); | |
10720 | return -EINVAL; | |
10721 | } | |
10722 | ||
10723 | ret_btf = env->prog->aux->btf; | |
10724 | ret_btf_id = meta.arg_constant.value; | |
10725 | ||
10726 | /* This may be NULL due to user not supplying a BTF */ | |
10727 | if (!ret_btf) { | |
10728 | verbose(env, "bpf_obj_new requires prog BTF\n"); | |
10729 | return -EINVAL; | |
10730 | } | |
10731 | ||
10732 | ret_t = btf_type_by_id(ret_btf, ret_btf_id); | |
10733 | if (!ret_t || !__btf_type_is_struct(ret_t)) { | |
10734 | verbose(env, "bpf_obj_new type ID argument must be of a struct\n"); | |
10735 | return -EINVAL; | |
10736 | } | |
10737 | ||
10738 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10739 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
10740 | regs[BPF_REG_0].btf = ret_btf; | |
10741 | regs[BPF_REG_0].btf_id = ret_btf_id; | |
10742 | ||
07236eab AN |
10743 | insn_aux->obj_new_size = ret_t->size; |
10744 | insn_aux->kptr_struct_meta = | |
958cf2e2 | 10745 | btf_find_struct_meta(ret_btf, ret_btf_id); |
8cab76ec KKD |
10746 | } else if (meta.func_id == special_kfunc_list[KF_bpf_list_pop_front] || |
10747 | meta.func_id == special_kfunc_list[KF_bpf_list_pop_back]) { | |
10748 | struct btf_field *field = meta.arg_list_head.field; | |
10749 | ||
a40d3632 DM |
10750 | mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root); |
10751 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
10752 | meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { | |
10753 | struct btf_field *field = meta.arg_rbtree_root.field; | |
10754 | ||
10755 | mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root); | |
fd264ca0 YS |
10756 | } else if (meta.func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { |
10757 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10758 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED; | |
10759 | regs[BPF_REG_0].btf = desc_btf; | |
10760 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; | |
a35b9af4 YS |
10761 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { |
10762 | ret_t = btf_type_by_id(desc_btf, meta.arg_constant.value); | |
10763 | if (!ret_t || !btf_type_is_struct(ret_t)) { | |
10764 | verbose(env, | |
10765 | "kfunc bpf_rdonly_cast type ID argument must be of a struct\n"); | |
10766 | return -EINVAL; | |
10767 | } | |
10768 | ||
10769 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10770 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_UNTRUSTED; | |
10771 | regs[BPF_REG_0].btf = desc_btf; | |
10772 | regs[BPF_REG_0].btf_id = meta.arg_constant.value; | |
66e3a13e JK |
10773 | } else if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice] || |
10774 | meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) { | |
10775 | enum bpf_type_flag type_flag = get_dynptr_type_flag(meta.initialized_dynptr.type); | |
10776 | ||
10777 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10778 | ||
10779 | if (!meta.arg_constant.found) { | |
10780 | verbose(env, "verifier internal error: bpf_dynptr_slice(_rdwr) no constant size\n"); | |
10781 | return -EFAULT; | |
10782 | } | |
10783 | ||
10784 | regs[BPF_REG_0].mem_size = meta.arg_constant.value; | |
10785 | ||
10786 | /* PTR_MAYBE_NULL will be added when is_kfunc_ret_null is checked */ | |
10787 | regs[BPF_REG_0].type = PTR_TO_MEM | type_flag; | |
10788 | ||
10789 | if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice]) { | |
10790 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
10791 | } else { | |
10792 | /* this will set env->seen_direct_write to true */ | |
10793 | if (!may_access_direct_pkt_data(env, NULL, BPF_WRITE)) { | |
10794 | verbose(env, "the prog does not allow writes to packet data\n"); | |
10795 | return -EINVAL; | |
10796 | } | |
10797 | } | |
10798 | ||
10799 | if (!meta.initialized_dynptr.id) { | |
10800 | verbose(env, "verifier internal error: no dynptr id\n"); | |
10801 | return -EFAULT; | |
10802 | } | |
10803 | regs[BPF_REG_0].dynptr_id = meta.initialized_dynptr.id; | |
10804 | ||
10805 | /* we don't need to set BPF_REG_0's ref obj id | |
10806 | * because packet slices are not refcounted (see | |
10807 | * dynptr_type_refcounted) | |
10808 | */ | |
958cf2e2 KKD |
10809 | } else { |
10810 | verbose(env, "kernel function %s unhandled dynamic return type\n", | |
10811 | meta.func_name); | |
10812 | return -EFAULT; | |
10813 | } | |
10814 | } else if (!__btf_type_is_struct(ptr_type)) { | |
f4b4eee6 AN |
10815 | if (!meta.r0_size) { |
10816 | __u32 sz; | |
10817 | ||
10818 | if (!IS_ERR(btf_resolve_size(desc_btf, ptr_type, &sz))) { | |
10819 | meta.r0_size = sz; | |
10820 | meta.r0_rdonly = true; | |
10821 | } | |
10822 | } | |
eb1f7f71 BT |
10823 | if (!meta.r0_size) { |
10824 | ptr_type_name = btf_name_by_offset(desc_btf, | |
10825 | ptr_type->name_off); | |
10826 | verbose(env, | |
10827 | "kernel function %s returns pointer type %s %s is not supported\n", | |
10828 | func_name, | |
10829 | btf_type_str(ptr_type), | |
10830 | ptr_type_name); | |
10831 | return -EINVAL; | |
10832 | } | |
10833 | ||
10834 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10835 | regs[BPF_REG_0].type = PTR_TO_MEM; | |
10836 | regs[BPF_REG_0].mem_size = meta.r0_size; | |
10837 | ||
10838 | if (meta.r0_rdonly) | |
10839 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
10840 | ||
10841 | /* Ensures we don't access the memory after a release_reference() */ | |
10842 | if (meta.ref_obj_id) | |
10843 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
10844 | } else { | |
10845 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10846 | regs[BPF_REG_0].btf = desc_btf; | |
10847 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; | |
10848 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
e6ac2450 | 10849 | } |
958cf2e2 | 10850 | |
00b85860 | 10851 | if (is_kfunc_ret_null(&meta)) { |
5c073f26 KKD |
10852 | regs[BPF_REG_0].type |= PTR_MAYBE_NULL; |
10853 | /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ | |
10854 | regs[BPF_REG_0].id = ++env->id_gen; | |
10855 | } | |
e6ac2450 | 10856 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
00b85860 | 10857 | if (is_kfunc_acquire(&meta)) { |
5c073f26 KKD |
10858 | int id = acquire_reference_state(env, insn_idx); |
10859 | ||
10860 | if (id < 0) | |
10861 | return id; | |
00b85860 KKD |
10862 | if (is_kfunc_ret_null(&meta)) |
10863 | regs[BPF_REG_0].id = id; | |
5c073f26 | 10864 | regs[BPF_REG_0].ref_obj_id = id; |
a40d3632 DM |
10865 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { |
10866 | ref_set_non_owning(env, ®s[BPF_REG_0]); | |
5c073f26 | 10867 | } |
a40d3632 DM |
10868 | |
10869 | if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_remove]) | |
10870 | invalidate_non_owning_refs(env); | |
10871 | ||
00b85860 KKD |
10872 | if (reg_may_point_to_spin_lock(®s[BPF_REG_0]) && !regs[BPF_REG_0].id) |
10873 | regs[BPF_REG_0].id = ++env->id_gen; | |
f6a6a5a9 DM |
10874 | } else if (btf_type_is_void(t)) { |
10875 | if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) { | |
10876 | if (meta.func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { | |
10877 | insn_aux->kptr_struct_meta = | |
10878 | btf_find_struct_meta(meta.arg_obj_drop.btf, | |
10879 | meta.arg_obj_drop.btf_id); | |
10880 | } | |
10881 | } | |
10882 | } | |
e6ac2450 | 10883 | |
07236eab AN |
10884 | nargs = btf_type_vlen(meta.func_proto); |
10885 | args = (const struct btf_param *)(meta.func_proto + 1); | |
e6ac2450 MKL |
10886 | for (i = 0; i < nargs; i++) { |
10887 | u32 regno = i + 1; | |
10888 | ||
2357672c | 10889 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
10890 | if (btf_type_is_ptr(t)) |
10891 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
10892 | else | |
10893 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
10894 | mark_btf_func_reg_size(env, regno, t->size); | |
10895 | } | |
10896 | ||
06accc87 AN |
10897 | if (is_iter_next_kfunc(&meta)) { |
10898 | err = process_iter_next_call(env, insn_idx, &meta); | |
10899 | if (err) | |
10900 | return err; | |
10901 | } | |
10902 | ||
e6ac2450 MKL |
10903 | return 0; |
10904 | } | |
10905 | ||
b03c9f9f EC |
10906 | static bool signed_add_overflows(s64 a, s64 b) |
10907 | { | |
10908 | /* Do the add in u64, where overflow is well-defined */ | |
10909 | s64 res = (s64)((u64)a + (u64)b); | |
10910 | ||
10911 | if (b < 0) | |
10912 | return res > a; | |
10913 | return res < a; | |
10914 | } | |
10915 | ||
bc895e8b | 10916 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
10917 | { |
10918 | /* Do the add in u32, where overflow is well-defined */ | |
10919 | s32 res = (s32)((u32)a + (u32)b); | |
10920 | ||
10921 | if (b < 0) | |
10922 | return res > a; | |
10923 | return res < a; | |
10924 | } | |
10925 | ||
bc895e8b | 10926 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
10927 | { |
10928 | /* Do the sub in u64, where overflow is well-defined */ | |
10929 | s64 res = (s64)((u64)a - (u64)b); | |
10930 | ||
10931 | if (b < 0) | |
10932 | return res < a; | |
10933 | return res > a; | |
969bf05e AS |
10934 | } |
10935 | ||
3f50f132 JF |
10936 | static bool signed_sub32_overflows(s32 a, s32 b) |
10937 | { | |
bc895e8b | 10938 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
10939 | s32 res = (s32)((u32)a - (u32)b); |
10940 | ||
10941 | if (b < 0) | |
10942 | return res < a; | |
10943 | return res > a; | |
10944 | } | |
10945 | ||
bb7f0f98 AS |
10946 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
10947 | const struct bpf_reg_state *reg, | |
10948 | enum bpf_reg_type type) | |
10949 | { | |
10950 | bool known = tnum_is_const(reg->var_off); | |
10951 | s64 val = reg->var_off.value; | |
10952 | s64 smin = reg->smin_value; | |
10953 | ||
10954 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
10955 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
c25b2ae1 | 10956 | reg_type_str(env, type), val); |
bb7f0f98 AS |
10957 | return false; |
10958 | } | |
10959 | ||
10960 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
10961 | verbose(env, "%s pointer offset %d is not allowed\n", | |
c25b2ae1 | 10962 | reg_type_str(env, type), reg->off); |
bb7f0f98 AS |
10963 | return false; |
10964 | } | |
10965 | ||
10966 | if (smin == S64_MIN) { | |
10967 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
c25b2ae1 | 10968 | reg_type_str(env, type)); |
bb7f0f98 AS |
10969 | return false; |
10970 | } | |
10971 | ||
10972 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
10973 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
c25b2ae1 | 10974 | smin, reg_type_str(env, type)); |
bb7f0f98 AS |
10975 | return false; |
10976 | } | |
10977 | ||
10978 | return true; | |
10979 | } | |
10980 | ||
a6aaece0 DB |
10981 | enum { |
10982 | REASON_BOUNDS = -1, | |
10983 | REASON_TYPE = -2, | |
10984 | REASON_PATHS = -3, | |
10985 | REASON_LIMIT = -4, | |
10986 | REASON_STACK = -5, | |
10987 | }; | |
10988 | ||
979d63d5 | 10989 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 10990 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 10991 | { |
7fedb63a | 10992 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
10993 | |
10994 | switch (ptr_reg->type) { | |
10995 | case PTR_TO_STACK: | |
1b1597e6 | 10996 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
10997 | * left direction, see BPF_REG_FP. Also, unknown scalar |
10998 | * offset where we would need to deal with min/max bounds is | |
10999 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
11000 | */ |
11001 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 11002 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 11003 | break; |
979d63d5 | 11004 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 11005 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
11006 | ptr_limit = (mask_to_left ? |
11007 | ptr_reg->smin_value : | |
11008 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 11009 | break; |
979d63d5 | 11010 | default: |
a6aaece0 | 11011 | return REASON_TYPE; |
979d63d5 | 11012 | } |
b658bbb8 DB |
11013 | |
11014 | if (ptr_limit >= max) | |
a6aaece0 | 11015 | return REASON_LIMIT; |
b658bbb8 DB |
11016 | *alu_limit = ptr_limit; |
11017 | return 0; | |
979d63d5 DB |
11018 | } |
11019 | ||
d3bd7413 DB |
11020 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
11021 | const struct bpf_insn *insn) | |
11022 | { | |
2c78ee89 | 11023 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
11024 | } |
11025 | ||
11026 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
11027 | u32 alu_state, u32 alu_limit) | |
11028 | { | |
11029 | /* If we arrived here from different branches with different | |
11030 | * state or limits to sanitize, then this won't work. | |
11031 | */ | |
11032 | if (aux->alu_state && | |
11033 | (aux->alu_state != alu_state || | |
11034 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 11035 | return REASON_PATHS; |
d3bd7413 | 11036 | |
e6ac5933 | 11037 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
11038 | aux->alu_state = alu_state; |
11039 | aux->alu_limit = alu_limit; | |
11040 | return 0; | |
11041 | } | |
11042 | ||
11043 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
11044 | struct bpf_insn *insn) | |
11045 | { | |
11046 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
11047 | ||
11048 | if (can_skip_alu_sanitation(env, insn)) | |
11049 | return 0; | |
11050 | ||
11051 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
11052 | } | |
11053 | ||
f5288193 DB |
11054 | static bool sanitize_needed(u8 opcode) |
11055 | { | |
11056 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
11057 | } | |
11058 | ||
3d0220f6 DB |
11059 | struct bpf_sanitize_info { |
11060 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 11061 | bool mask_to_left; |
3d0220f6 DB |
11062 | }; |
11063 | ||
9183671a DB |
11064 | static struct bpf_verifier_state * |
11065 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
11066 | const struct bpf_insn *insn, | |
11067 | u32 next_idx, u32 curr_idx) | |
11068 | { | |
11069 | struct bpf_verifier_state *branch; | |
11070 | struct bpf_reg_state *regs; | |
11071 | ||
11072 | branch = push_stack(env, next_idx, curr_idx, true); | |
11073 | if (branch && insn) { | |
11074 | regs = branch->frame[branch->curframe]->regs; | |
11075 | if (BPF_SRC(insn->code) == BPF_K) { | |
11076 | mark_reg_unknown(env, regs, insn->dst_reg); | |
11077 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
11078 | mark_reg_unknown(env, regs, insn->dst_reg); | |
11079 | mark_reg_unknown(env, regs, insn->src_reg); | |
11080 | } | |
11081 | } | |
11082 | return branch; | |
11083 | } | |
11084 | ||
979d63d5 DB |
11085 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
11086 | struct bpf_insn *insn, | |
11087 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 11088 | const struct bpf_reg_state *off_reg, |
979d63d5 | 11089 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 11090 | struct bpf_sanitize_info *info, |
7fedb63a | 11091 | const bool commit_window) |
979d63d5 | 11092 | { |
3d0220f6 | 11093 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 11094 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 11095 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 11096 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
11097 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
11098 | u8 opcode = BPF_OP(insn->code); | |
11099 | u32 alu_state, alu_limit; | |
11100 | struct bpf_reg_state tmp; | |
11101 | bool ret; | |
f232326f | 11102 | int err; |
979d63d5 | 11103 | |
d3bd7413 | 11104 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
11105 | return 0; |
11106 | ||
11107 | /* We already marked aux for masking from non-speculative | |
11108 | * paths, thus we got here in the first place. We only care | |
11109 | * to explore bad access from here. | |
11110 | */ | |
11111 | if (vstate->speculative) | |
11112 | goto do_sim; | |
11113 | ||
bb01a1bb DB |
11114 | if (!commit_window) { |
11115 | if (!tnum_is_const(off_reg->var_off) && | |
11116 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
11117 | return REASON_BOUNDS; | |
11118 | ||
11119 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
11120 | (opcode == BPF_SUB && !off_is_neg); | |
11121 | } | |
11122 | ||
11123 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
11124 | if (err < 0) |
11125 | return err; | |
11126 | ||
7fedb63a DB |
11127 | if (commit_window) { |
11128 | /* In commit phase we narrow the masking window based on | |
11129 | * the observed pointer move after the simulated operation. | |
11130 | */ | |
3d0220f6 DB |
11131 | alu_state = info->aux.alu_state; |
11132 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
11133 | } else { |
11134 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 11135 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
11136 | alu_state |= ptr_is_dst_reg ? |
11137 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
11138 | |
11139 | /* Limit pruning on unknown scalars to enable deep search for | |
11140 | * potential masking differences from other program paths. | |
11141 | */ | |
11142 | if (!off_is_imm) | |
11143 | env->explore_alu_limits = true; | |
7fedb63a DB |
11144 | } |
11145 | ||
f232326f PK |
11146 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
11147 | if (err < 0) | |
11148 | return err; | |
979d63d5 | 11149 | do_sim: |
7fedb63a DB |
11150 | /* If we're in commit phase, we're done here given we already |
11151 | * pushed the truncated dst_reg into the speculative verification | |
11152 | * stack. | |
a7036191 DB |
11153 | * |
11154 | * Also, when register is a known constant, we rewrite register-based | |
11155 | * operation to immediate-based, and thus do not need masking (and as | |
11156 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 11157 | */ |
a7036191 | 11158 | if (commit_window || off_is_imm) |
7fedb63a DB |
11159 | return 0; |
11160 | ||
979d63d5 DB |
11161 | /* Simulate and find potential out-of-bounds access under |
11162 | * speculative execution from truncation as a result of | |
11163 | * masking when off was not within expected range. If off | |
11164 | * sits in dst, then we temporarily need to move ptr there | |
11165 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
11166 | * for cases where we use K-based arithmetic in one direction | |
11167 | * and truncated reg-based in the other in order to explore | |
11168 | * bad access. | |
11169 | */ | |
11170 | if (!ptr_is_dst_reg) { | |
11171 | tmp = *dst_reg; | |
71f656a5 | 11172 | copy_register_state(dst_reg, ptr_reg); |
979d63d5 | 11173 | } |
9183671a DB |
11174 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
11175 | env->insn_idx); | |
0803278b | 11176 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 11177 | *dst_reg = tmp; |
a6aaece0 DB |
11178 | return !ret ? REASON_STACK : 0; |
11179 | } | |
11180 | ||
fe9a5ca7 DB |
11181 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
11182 | { | |
11183 | struct bpf_verifier_state *vstate = env->cur_state; | |
11184 | ||
11185 | /* If we simulate paths under speculation, we don't update the | |
11186 | * insn as 'seen' such that when we verify unreachable paths in | |
11187 | * the non-speculative domain, sanitize_dead_code() can still | |
11188 | * rewrite/sanitize them. | |
11189 | */ | |
11190 | if (!vstate->speculative) | |
11191 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
11192 | } | |
11193 | ||
a6aaece0 DB |
11194 | static int sanitize_err(struct bpf_verifier_env *env, |
11195 | const struct bpf_insn *insn, int reason, | |
11196 | const struct bpf_reg_state *off_reg, | |
11197 | const struct bpf_reg_state *dst_reg) | |
11198 | { | |
11199 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
11200 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
11201 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
11202 | ||
11203 | switch (reason) { | |
11204 | case REASON_BOUNDS: | |
11205 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
11206 | off_reg == dst_reg ? dst : src, err); | |
11207 | break; | |
11208 | case REASON_TYPE: | |
11209 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
11210 | off_reg == dst_reg ? src : dst, err); | |
11211 | break; | |
11212 | case REASON_PATHS: | |
11213 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
11214 | dst, op, err); | |
11215 | break; | |
11216 | case REASON_LIMIT: | |
11217 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
11218 | dst, op, err); | |
11219 | break; | |
11220 | case REASON_STACK: | |
11221 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
11222 | dst, err); | |
11223 | break; | |
11224 | default: | |
11225 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
11226 | reason); | |
11227 | break; | |
11228 | } | |
11229 | ||
11230 | return -EACCES; | |
979d63d5 DB |
11231 | } |
11232 | ||
01f810ac AM |
11233 | /* check that stack access falls within stack limits and that 'reg' doesn't |
11234 | * have a variable offset. | |
11235 | * | |
11236 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
11237 | * requires corresponding support in Spectre masking for stack ALU. See also | |
11238 | * retrieve_ptr_limit(). | |
11239 | * | |
11240 | * | |
11241 | * 'off' includes 'reg->off'. | |
11242 | */ | |
11243 | static int check_stack_access_for_ptr_arithmetic( | |
11244 | struct bpf_verifier_env *env, | |
11245 | int regno, | |
11246 | const struct bpf_reg_state *reg, | |
11247 | int off) | |
11248 | { | |
11249 | if (!tnum_is_const(reg->var_off)) { | |
11250 | char tn_buf[48]; | |
11251 | ||
11252 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
11253 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
11254 | regno, tn_buf, off); | |
11255 | return -EACCES; | |
11256 | } | |
11257 | ||
11258 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
11259 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
11260 | "prohibited for !root; off=%d\n", regno, off); | |
11261 | return -EACCES; | |
11262 | } | |
11263 | ||
11264 | return 0; | |
11265 | } | |
11266 | ||
073815b7 DB |
11267 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
11268 | const struct bpf_insn *insn, | |
11269 | const struct bpf_reg_state *dst_reg) | |
11270 | { | |
11271 | u32 dst = insn->dst_reg; | |
11272 | ||
11273 | /* For unprivileged we require that resulting offset must be in bounds | |
11274 | * in order to be able to sanitize access later on. | |
11275 | */ | |
11276 | if (env->bypass_spec_v1) | |
11277 | return 0; | |
11278 | ||
11279 | switch (dst_reg->type) { | |
11280 | case PTR_TO_STACK: | |
11281 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
11282 | dst_reg->off + dst_reg->var_off.value)) | |
11283 | return -EACCES; | |
11284 | break; | |
11285 | case PTR_TO_MAP_VALUE: | |
61df10c7 | 11286 | if (check_map_access(env, dst, dst_reg->off, 1, false, ACCESS_HELPER)) { |
073815b7 DB |
11287 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
11288 | "prohibited for !root\n", dst); | |
11289 | return -EACCES; | |
11290 | } | |
11291 | break; | |
11292 | default: | |
11293 | break; | |
11294 | } | |
11295 | ||
11296 | return 0; | |
11297 | } | |
01f810ac | 11298 | |
f1174f77 | 11299 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
11300 | * Caller should also handle BPF_MOV case separately. |
11301 | * If we return -EACCES, caller may want to try again treating pointer as a | |
11302 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
11303 | */ | |
11304 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
11305 | struct bpf_insn *insn, | |
11306 | const struct bpf_reg_state *ptr_reg, | |
11307 | const struct bpf_reg_state *off_reg) | |
969bf05e | 11308 | { |
f4d7e40a AS |
11309 | struct bpf_verifier_state *vstate = env->cur_state; |
11310 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
11311 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 11312 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
11313 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
11314 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
11315 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
11316 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 11317 | struct bpf_sanitize_info info = {}; |
969bf05e | 11318 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 11319 | u32 dst = insn->dst_reg; |
979d63d5 | 11320 | int ret; |
969bf05e | 11321 | |
f1174f77 | 11322 | dst_reg = ®s[dst]; |
969bf05e | 11323 | |
6f16101e DB |
11324 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
11325 | smin_val > smax_val || umin_val > umax_val) { | |
11326 | /* Taint dst register if offset had invalid bounds derived from | |
11327 | * e.g. dead branches. | |
11328 | */ | |
f54c7898 | 11329 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 11330 | return 0; |
f1174f77 EC |
11331 | } |
11332 | ||
11333 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
11334 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
11335 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
11336 | __mark_reg_unknown(env, dst_reg); | |
11337 | return 0; | |
11338 | } | |
11339 | ||
82abbf8d AS |
11340 | verbose(env, |
11341 | "R%d 32-bit pointer arithmetic prohibited\n", | |
11342 | dst); | |
f1174f77 | 11343 | return -EACCES; |
969bf05e AS |
11344 | } |
11345 | ||
c25b2ae1 | 11346 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
aad2eeaf | 11347 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
c25b2ae1 | 11348 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 11349 | return -EACCES; |
c25b2ae1 HL |
11350 | } |
11351 | ||
11352 | switch (base_type(ptr_reg->type)) { | |
aad2eeaf | 11353 | case CONST_PTR_TO_MAP: |
7c696732 YS |
11354 | /* smin_val represents the known value */ |
11355 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
11356 | break; | |
8731745e | 11357 | fallthrough; |
aad2eeaf | 11358 | case PTR_TO_PACKET_END: |
c64b7983 | 11359 | case PTR_TO_SOCKET: |
46f8bc92 | 11360 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 11361 | case PTR_TO_TCP_SOCK: |
fada7fdc | 11362 | case PTR_TO_XDP_SOCK: |
aad2eeaf | 11363 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
c25b2ae1 | 11364 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 11365 | return -EACCES; |
aad2eeaf JS |
11366 | default: |
11367 | break; | |
f1174f77 EC |
11368 | } |
11369 | ||
11370 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
11371 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 11372 | */ |
f1174f77 EC |
11373 | dst_reg->type = ptr_reg->type; |
11374 | dst_reg->id = ptr_reg->id; | |
969bf05e | 11375 | |
bb7f0f98 AS |
11376 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
11377 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
11378 | return -EINVAL; | |
11379 | ||
3f50f132 JF |
11380 | /* pointer types do not carry 32-bit bounds at the moment. */ |
11381 | __mark_reg32_unbounded(dst_reg); | |
11382 | ||
7fedb63a DB |
11383 | if (sanitize_needed(opcode)) { |
11384 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 11385 | &info, false); |
a6aaece0 DB |
11386 | if (ret < 0) |
11387 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 11388 | } |
a6aaece0 | 11389 | |
f1174f77 EC |
11390 | switch (opcode) { |
11391 | case BPF_ADD: | |
11392 | /* We can take a fixed offset as long as it doesn't overflow | |
11393 | * the s32 'off' field | |
969bf05e | 11394 | */ |
b03c9f9f EC |
11395 | if (known && (ptr_reg->off + smin_val == |
11396 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 11397 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
11398 | dst_reg->smin_value = smin_ptr; |
11399 | dst_reg->smax_value = smax_ptr; | |
11400 | dst_reg->umin_value = umin_ptr; | |
11401 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 11402 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 11403 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 11404 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
11405 | break; |
11406 | } | |
f1174f77 EC |
11407 | /* A new variable offset is created. Note that off_reg->off |
11408 | * == 0, since it's a scalar. | |
11409 | * dst_reg gets the pointer type and since some positive | |
11410 | * integer value was added to the pointer, give it a new 'id' | |
11411 | * if it's a PTR_TO_PACKET. | |
11412 | * this creates a new 'base' pointer, off_reg (variable) gets | |
11413 | * added into the variable offset, and we copy the fixed offset | |
11414 | * from ptr_reg. | |
969bf05e | 11415 | */ |
b03c9f9f EC |
11416 | if (signed_add_overflows(smin_ptr, smin_val) || |
11417 | signed_add_overflows(smax_ptr, smax_val)) { | |
11418 | dst_reg->smin_value = S64_MIN; | |
11419 | dst_reg->smax_value = S64_MAX; | |
11420 | } else { | |
11421 | dst_reg->smin_value = smin_ptr + smin_val; | |
11422 | dst_reg->smax_value = smax_ptr + smax_val; | |
11423 | } | |
11424 | if (umin_ptr + umin_val < umin_ptr || | |
11425 | umax_ptr + umax_val < umax_ptr) { | |
11426 | dst_reg->umin_value = 0; | |
11427 | dst_reg->umax_value = U64_MAX; | |
11428 | } else { | |
11429 | dst_reg->umin_value = umin_ptr + umin_val; | |
11430 | dst_reg->umax_value = umax_ptr + umax_val; | |
11431 | } | |
f1174f77 EC |
11432 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
11433 | dst_reg->off = ptr_reg->off; | |
0962590e | 11434 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 11435 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
11436 | dst_reg->id = ++env->id_gen; |
11437 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 11438 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
11439 | } |
11440 | break; | |
11441 | case BPF_SUB: | |
11442 | if (dst_reg == off_reg) { | |
11443 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
11444 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
11445 | dst); | |
f1174f77 EC |
11446 | return -EACCES; |
11447 | } | |
11448 | /* We don't allow subtraction from FP, because (according to | |
11449 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
11450 | * be able to deal with it. | |
969bf05e | 11451 | */ |
f1174f77 | 11452 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
11453 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
11454 | dst); | |
f1174f77 EC |
11455 | return -EACCES; |
11456 | } | |
b03c9f9f EC |
11457 | if (known && (ptr_reg->off - smin_val == |
11458 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 11459 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
11460 | dst_reg->smin_value = smin_ptr; |
11461 | dst_reg->smax_value = smax_ptr; | |
11462 | dst_reg->umin_value = umin_ptr; | |
11463 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
11464 | dst_reg->var_off = ptr_reg->var_off; |
11465 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 11466 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 11467 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
11468 | break; |
11469 | } | |
f1174f77 EC |
11470 | /* A new variable offset is created. If the subtrahend is known |
11471 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 11472 | */ |
b03c9f9f EC |
11473 | if (signed_sub_overflows(smin_ptr, smax_val) || |
11474 | signed_sub_overflows(smax_ptr, smin_val)) { | |
11475 | /* Overflow possible, we know nothing */ | |
11476 | dst_reg->smin_value = S64_MIN; | |
11477 | dst_reg->smax_value = S64_MAX; | |
11478 | } else { | |
11479 | dst_reg->smin_value = smin_ptr - smax_val; | |
11480 | dst_reg->smax_value = smax_ptr - smin_val; | |
11481 | } | |
11482 | if (umin_ptr < umax_val) { | |
11483 | /* Overflow possible, we know nothing */ | |
11484 | dst_reg->umin_value = 0; | |
11485 | dst_reg->umax_value = U64_MAX; | |
11486 | } else { | |
11487 | /* Cannot overflow (as long as bounds are consistent) */ | |
11488 | dst_reg->umin_value = umin_ptr - umax_val; | |
11489 | dst_reg->umax_value = umax_ptr - umin_val; | |
11490 | } | |
f1174f77 EC |
11491 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
11492 | dst_reg->off = ptr_reg->off; | |
0962590e | 11493 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 11494 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
11495 | dst_reg->id = ++env->id_gen; |
11496 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 11497 | if (smin_val < 0) |
22dc4a0f | 11498 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 11499 | } |
f1174f77 EC |
11500 | break; |
11501 | case BPF_AND: | |
11502 | case BPF_OR: | |
11503 | case BPF_XOR: | |
82abbf8d AS |
11504 | /* bitwise ops on pointers are troublesome, prohibit. */ |
11505 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
11506 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
11507 | return -EACCES; |
11508 | default: | |
11509 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
11510 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
11511 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 11512 | return -EACCES; |
43188702 JF |
11513 | } |
11514 | ||
bb7f0f98 AS |
11515 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
11516 | return -EINVAL; | |
3844d153 | 11517 | reg_bounds_sync(dst_reg); |
073815b7 DB |
11518 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
11519 | return -EACCES; | |
7fedb63a DB |
11520 | if (sanitize_needed(opcode)) { |
11521 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 11522 | &info, true); |
7fedb63a DB |
11523 | if (ret < 0) |
11524 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
11525 | } |
11526 | ||
43188702 JF |
11527 | return 0; |
11528 | } | |
11529 | ||
3f50f132 JF |
11530 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
11531 | struct bpf_reg_state *src_reg) | |
11532 | { | |
11533 | s32 smin_val = src_reg->s32_min_value; | |
11534 | s32 smax_val = src_reg->s32_max_value; | |
11535 | u32 umin_val = src_reg->u32_min_value; | |
11536 | u32 umax_val = src_reg->u32_max_value; | |
11537 | ||
11538 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
11539 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
11540 | dst_reg->s32_min_value = S32_MIN; | |
11541 | dst_reg->s32_max_value = S32_MAX; | |
11542 | } else { | |
11543 | dst_reg->s32_min_value += smin_val; | |
11544 | dst_reg->s32_max_value += smax_val; | |
11545 | } | |
11546 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
11547 | dst_reg->u32_max_value + umax_val < umax_val) { | |
11548 | dst_reg->u32_min_value = 0; | |
11549 | dst_reg->u32_max_value = U32_MAX; | |
11550 | } else { | |
11551 | dst_reg->u32_min_value += umin_val; | |
11552 | dst_reg->u32_max_value += umax_val; | |
11553 | } | |
11554 | } | |
11555 | ||
07cd2631 JF |
11556 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
11557 | struct bpf_reg_state *src_reg) | |
11558 | { | |
11559 | s64 smin_val = src_reg->smin_value; | |
11560 | s64 smax_val = src_reg->smax_value; | |
11561 | u64 umin_val = src_reg->umin_value; | |
11562 | u64 umax_val = src_reg->umax_value; | |
11563 | ||
11564 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
11565 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
11566 | dst_reg->smin_value = S64_MIN; | |
11567 | dst_reg->smax_value = S64_MAX; | |
11568 | } else { | |
11569 | dst_reg->smin_value += smin_val; | |
11570 | dst_reg->smax_value += smax_val; | |
11571 | } | |
11572 | if (dst_reg->umin_value + umin_val < umin_val || | |
11573 | dst_reg->umax_value + umax_val < umax_val) { | |
11574 | dst_reg->umin_value = 0; | |
11575 | dst_reg->umax_value = U64_MAX; | |
11576 | } else { | |
11577 | dst_reg->umin_value += umin_val; | |
11578 | dst_reg->umax_value += umax_val; | |
11579 | } | |
3f50f132 JF |
11580 | } |
11581 | ||
11582 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
11583 | struct bpf_reg_state *src_reg) | |
11584 | { | |
11585 | s32 smin_val = src_reg->s32_min_value; | |
11586 | s32 smax_val = src_reg->s32_max_value; | |
11587 | u32 umin_val = src_reg->u32_min_value; | |
11588 | u32 umax_val = src_reg->u32_max_value; | |
11589 | ||
11590 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
11591 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
11592 | /* Overflow possible, we know nothing */ | |
11593 | dst_reg->s32_min_value = S32_MIN; | |
11594 | dst_reg->s32_max_value = S32_MAX; | |
11595 | } else { | |
11596 | dst_reg->s32_min_value -= smax_val; | |
11597 | dst_reg->s32_max_value -= smin_val; | |
11598 | } | |
11599 | if (dst_reg->u32_min_value < umax_val) { | |
11600 | /* Overflow possible, we know nothing */ | |
11601 | dst_reg->u32_min_value = 0; | |
11602 | dst_reg->u32_max_value = U32_MAX; | |
11603 | } else { | |
11604 | /* Cannot overflow (as long as bounds are consistent) */ | |
11605 | dst_reg->u32_min_value -= umax_val; | |
11606 | dst_reg->u32_max_value -= umin_val; | |
11607 | } | |
07cd2631 JF |
11608 | } |
11609 | ||
11610 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
11611 | struct bpf_reg_state *src_reg) | |
11612 | { | |
11613 | s64 smin_val = src_reg->smin_value; | |
11614 | s64 smax_val = src_reg->smax_value; | |
11615 | u64 umin_val = src_reg->umin_value; | |
11616 | u64 umax_val = src_reg->umax_value; | |
11617 | ||
11618 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
11619 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
11620 | /* Overflow possible, we know nothing */ | |
11621 | dst_reg->smin_value = S64_MIN; | |
11622 | dst_reg->smax_value = S64_MAX; | |
11623 | } else { | |
11624 | dst_reg->smin_value -= smax_val; | |
11625 | dst_reg->smax_value -= smin_val; | |
11626 | } | |
11627 | if (dst_reg->umin_value < umax_val) { | |
11628 | /* Overflow possible, we know nothing */ | |
11629 | dst_reg->umin_value = 0; | |
11630 | dst_reg->umax_value = U64_MAX; | |
11631 | } else { | |
11632 | /* Cannot overflow (as long as bounds are consistent) */ | |
11633 | dst_reg->umin_value -= umax_val; | |
11634 | dst_reg->umax_value -= umin_val; | |
11635 | } | |
3f50f132 JF |
11636 | } |
11637 | ||
11638 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
11639 | struct bpf_reg_state *src_reg) | |
11640 | { | |
11641 | s32 smin_val = src_reg->s32_min_value; | |
11642 | u32 umin_val = src_reg->u32_min_value; | |
11643 | u32 umax_val = src_reg->u32_max_value; | |
11644 | ||
11645 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
11646 | /* Ain't nobody got time to multiply that sign */ | |
11647 | __mark_reg32_unbounded(dst_reg); | |
11648 | return; | |
11649 | } | |
11650 | /* Both values are positive, so we can work with unsigned and | |
11651 | * copy the result to signed (unless it exceeds S32_MAX). | |
11652 | */ | |
11653 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
11654 | /* Potential overflow, we know nothing */ | |
11655 | __mark_reg32_unbounded(dst_reg); | |
11656 | return; | |
11657 | } | |
11658 | dst_reg->u32_min_value *= umin_val; | |
11659 | dst_reg->u32_max_value *= umax_val; | |
11660 | if (dst_reg->u32_max_value > S32_MAX) { | |
11661 | /* Overflow possible, we know nothing */ | |
11662 | dst_reg->s32_min_value = S32_MIN; | |
11663 | dst_reg->s32_max_value = S32_MAX; | |
11664 | } else { | |
11665 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11666 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11667 | } | |
07cd2631 JF |
11668 | } |
11669 | ||
11670 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
11671 | struct bpf_reg_state *src_reg) | |
11672 | { | |
11673 | s64 smin_val = src_reg->smin_value; | |
11674 | u64 umin_val = src_reg->umin_value; | |
11675 | u64 umax_val = src_reg->umax_value; | |
11676 | ||
07cd2631 JF |
11677 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
11678 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 11679 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
11680 | return; |
11681 | } | |
11682 | /* Both values are positive, so we can work with unsigned and | |
11683 | * copy the result to signed (unless it exceeds S64_MAX). | |
11684 | */ | |
11685 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
11686 | /* Potential overflow, we know nothing */ | |
3f50f132 | 11687 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
11688 | return; |
11689 | } | |
11690 | dst_reg->umin_value *= umin_val; | |
11691 | dst_reg->umax_value *= umax_val; | |
11692 | if (dst_reg->umax_value > S64_MAX) { | |
11693 | /* Overflow possible, we know nothing */ | |
11694 | dst_reg->smin_value = S64_MIN; | |
11695 | dst_reg->smax_value = S64_MAX; | |
11696 | } else { | |
11697 | dst_reg->smin_value = dst_reg->umin_value; | |
11698 | dst_reg->smax_value = dst_reg->umax_value; | |
11699 | } | |
11700 | } | |
11701 | ||
3f50f132 JF |
11702 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
11703 | struct bpf_reg_state *src_reg) | |
11704 | { | |
11705 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11706 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11707 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
11708 | s32 smin_val = src_reg->s32_min_value; | |
11709 | u32 umax_val = src_reg->u32_max_value; | |
11710 | ||
049c4e13 DB |
11711 | if (src_known && dst_known) { |
11712 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 11713 | return; |
049c4e13 | 11714 | } |
3f50f132 JF |
11715 | |
11716 | /* We get our minimum from the var_off, since that's inherently | |
11717 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
11718 | */ | |
11719 | dst_reg->u32_min_value = var32_off.value; | |
11720 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
11721 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
11722 | /* Lose signed bounds when ANDing negative numbers, | |
11723 | * ain't nobody got time for that. | |
11724 | */ | |
11725 | dst_reg->s32_min_value = S32_MIN; | |
11726 | dst_reg->s32_max_value = S32_MAX; | |
11727 | } else { | |
11728 | /* ANDing two positives gives a positive, so safe to | |
11729 | * cast result into s64. | |
11730 | */ | |
11731 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11732 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11733 | } | |
3f50f132 JF |
11734 | } |
11735 | ||
07cd2631 JF |
11736 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
11737 | struct bpf_reg_state *src_reg) | |
11738 | { | |
3f50f132 JF |
11739 | bool src_known = tnum_is_const(src_reg->var_off); |
11740 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
11741 | s64 smin_val = src_reg->smin_value; |
11742 | u64 umax_val = src_reg->umax_value; | |
11743 | ||
3f50f132 | 11744 | if (src_known && dst_known) { |
4fbb38a3 | 11745 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
11746 | return; |
11747 | } | |
11748 | ||
07cd2631 JF |
11749 | /* We get our minimum from the var_off, since that's inherently |
11750 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
11751 | */ | |
07cd2631 JF |
11752 | dst_reg->umin_value = dst_reg->var_off.value; |
11753 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
11754 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
11755 | /* Lose signed bounds when ANDing negative numbers, | |
11756 | * ain't nobody got time for that. | |
11757 | */ | |
11758 | dst_reg->smin_value = S64_MIN; | |
11759 | dst_reg->smax_value = S64_MAX; | |
11760 | } else { | |
11761 | /* ANDing two positives gives a positive, so safe to | |
11762 | * cast result into s64. | |
11763 | */ | |
11764 | dst_reg->smin_value = dst_reg->umin_value; | |
11765 | dst_reg->smax_value = dst_reg->umax_value; | |
11766 | } | |
11767 | /* We may learn something more from the var_off */ | |
11768 | __update_reg_bounds(dst_reg); | |
11769 | } | |
11770 | ||
3f50f132 JF |
11771 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
11772 | struct bpf_reg_state *src_reg) | |
11773 | { | |
11774 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11775 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11776 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
11777 | s32 smin_val = src_reg->s32_min_value; |
11778 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 11779 | |
049c4e13 DB |
11780 | if (src_known && dst_known) { |
11781 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 11782 | return; |
049c4e13 | 11783 | } |
3f50f132 JF |
11784 | |
11785 | /* We get our maximum from the var_off, and our minimum is the | |
11786 | * maximum of the operands' minima | |
11787 | */ | |
11788 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
11789 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
11790 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
11791 | /* Lose signed bounds when ORing negative numbers, | |
11792 | * ain't nobody got time for that. | |
11793 | */ | |
11794 | dst_reg->s32_min_value = S32_MIN; | |
11795 | dst_reg->s32_max_value = S32_MAX; | |
11796 | } else { | |
11797 | /* ORing two positives gives a positive, so safe to | |
11798 | * cast result into s64. | |
11799 | */ | |
5b9fbeb7 DB |
11800 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
11801 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
11802 | } |
11803 | } | |
11804 | ||
07cd2631 JF |
11805 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
11806 | struct bpf_reg_state *src_reg) | |
11807 | { | |
3f50f132 JF |
11808 | bool src_known = tnum_is_const(src_reg->var_off); |
11809 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
11810 | s64 smin_val = src_reg->smin_value; |
11811 | u64 umin_val = src_reg->umin_value; | |
11812 | ||
3f50f132 | 11813 | if (src_known && dst_known) { |
4fbb38a3 | 11814 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
11815 | return; |
11816 | } | |
11817 | ||
07cd2631 JF |
11818 | /* We get our maximum from the var_off, and our minimum is the |
11819 | * maximum of the operands' minima | |
11820 | */ | |
07cd2631 JF |
11821 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
11822 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
11823 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
11824 | /* Lose signed bounds when ORing negative numbers, | |
11825 | * ain't nobody got time for that. | |
11826 | */ | |
11827 | dst_reg->smin_value = S64_MIN; | |
11828 | dst_reg->smax_value = S64_MAX; | |
11829 | } else { | |
11830 | /* ORing two positives gives a positive, so safe to | |
11831 | * cast result into s64. | |
11832 | */ | |
11833 | dst_reg->smin_value = dst_reg->umin_value; | |
11834 | dst_reg->smax_value = dst_reg->umax_value; | |
11835 | } | |
11836 | /* We may learn something more from the var_off */ | |
11837 | __update_reg_bounds(dst_reg); | |
11838 | } | |
11839 | ||
2921c90d YS |
11840 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
11841 | struct bpf_reg_state *src_reg) | |
11842 | { | |
11843 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11844 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11845 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
11846 | s32 smin_val = src_reg->s32_min_value; | |
11847 | ||
049c4e13 DB |
11848 | if (src_known && dst_known) { |
11849 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 11850 | return; |
049c4e13 | 11851 | } |
2921c90d YS |
11852 | |
11853 | /* We get both minimum and maximum from the var32_off. */ | |
11854 | dst_reg->u32_min_value = var32_off.value; | |
11855 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
11856 | ||
11857 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
11858 | /* XORing two positive sign numbers gives a positive, | |
11859 | * so safe to cast u32 result into s32. | |
11860 | */ | |
11861 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11862 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11863 | } else { | |
11864 | dst_reg->s32_min_value = S32_MIN; | |
11865 | dst_reg->s32_max_value = S32_MAX; | |
11866 | } | |
11867 | } | |
11868 | ||
11869 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
11870 | struct bpf_reg_state *src_reg) | |
11871 | { | |
11872 | bool src_known = tnum_is_const(src_reg->var_off); | |
11873 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
11874 | s64 smin_val = src_reg->smin_value; | |
11875 | ||
11876 | if (src_known && dst_known) { | |
11877 | /* dst_reg->var_off.value has been updated earlier */ | |
11878 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
11879 | return; | |
11880 | } | |
11881 | ||
11882 | /* We get both minimum and maximum from the var_off. */ | |
11883 | dst_reg->umin_value = dst_reg->var_off.value; | |
11884 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
11885 | ||
11886 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
11887 | /* XORing two positive sign numbers gives a positive, | |
11888 | * so safe to cast u64 result into s64. | |
11889 | */ | |
11890 | dst_reg->smin_value = dst_reg->umin_value; | |
11891 | dst_reg->smax_value = dst_reg->umax_value; | |
11892 | } else { | |
11893 | dst_reg->smin_value = S64_MIN; | |
11894 | dst_reg->smax_value = S64_MAX; | |
11895 | } | |
11896 | ||
11897 | __update_reg_bounds(dst_reg); | |
11898 | } | |
11899 | ||
3f50f132 JF |
11900 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
11901 | u64 umin_val, u64 umax_val) | |
07cd2631 | 11902 | { |
07cd2631 JF |
11903 | /* We lose all sign bit information (except what we can pick |
11904 | * up from var_off) | |
11905 | */ | |
3f50f132 JF |
11906 | dst_reg->s32_min_value = S32_MIN; |
11907 | dst_reg->s32_max_value = S32_MAX; | |
11908 | /* If we might shift our top bit out, then we know nothing */ | |
11909 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
11910 | dst_reg->u32_min_value = 0; | |
11911 | dst_reg->u32_max_value = U32_MAX; | |
11912 | } else { | |
11913 | dst_reg->u32_min_value <<= umin_val; | |
11914 | dst_reg->u32_max_value <<= umax_val; | |
11915 | } | |
11916 | } | |
11917 | ||
11918 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
11919 | struct bpf_reg_state *src_reg) | |
11920 | { | |
11921 | u32 umax_val = src_reg->u32_max_value; | |
11922 | u32 umin_val = src_reg->u32_min_value; | |
11923 | /* u32 alu operation will zext upper bits */ | |
11924 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
11925 | ||
11926 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
11927 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
11928 | /* Not required but being careful mark reg64 bounds as unknown so | |
11929 | * that we are forced to pick them up from tnum and zext later and | |
11930 | * if some path skips this step we are still safe. | |
11931 | */ | |
11932 | __mark_reg64_unbounded(dst_reg); | |
11933 | __update_reg32_bounds(dst_reg); | |
11934 | } | |
11935 | ||
11936 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
11937 | u64 umin_val, u64 umax_val) | |
11938 | { | |
11939 | /* Special case <<32 because it is a common compiler pattern to sign | |
11940 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
11941 | * positive we know this shift will also be positive so we can track | |
11942 | * bounds correctly. Otherwise we lose all sign bit information except | |
11943 | * what we can pick up from var_off. Perhaps we can generalize this | |
11944 | * later to shifts of any length. | |
11945 | */ | |
11946 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
11947 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
11948 | else | |
11949 | dst_reg->smax_value = S64_MAX; | |
11950 | ||
11951 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
11952 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
11953 | else | |
11954 | dst_reg->smin_value = S64_MIN; | |
11955 | ||
07cd2631 JF |
11956 | /* If we might shift our top bit out, then we know nothing */ |
11957 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
11958 | dst_reg->umin_value = 0; | |
11959 | dst_reg->umax_value = U64_MAX; | |
11960 | } else { | |
11961 | dst_reg->umin_value <<= umin_val; | |
11962 | dst_reg->umax_value <<= umax_val; | |
11963 | } | |
3f50f132 JF |
11964 | } |
11965 | ||
11966 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
11967 | struct bpf_reg_state *src_reg) | |
11968 | { | |
11969 | u64 umax_val = src_reg->umax_value; | |
11970 | u64 umin_val = src_reg->umin_value; | |
11971 | ||
11972 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
11973 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
11974 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
11975 | ||
07cd2631 JF |
11976 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
11977 | /* We may learn something more from the var_off */ | |
11978 | __update_reg_bounds(dst_reg); | |
11979 | } | |
11980 | ||
3f50f132 JF |
11981 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
11982 | struct bpf_reg_state *src_reg) | |
11983 | { | |
11984 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
11985 | u32 umax_val = src_reg->u32_max_value; | |
11986 | u32 umin_val = src_reg->u32_min_value; | |
11987 | ||
11988 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
11989 | * be negative, then either: | |
11990 | * 1) src_reg might be zero, so the sign bit of the result is | |
11991 | * unknown, so we lose our signed bounds | |
11992 | * 2) it's known negative, thus the unsigned bounds capture the | |
11993 | * signed bounds | |
11994 | * 3) the signed bounds cross zero, so they tell us nothing | |
11995 | * about the result | |
11996 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 11997 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
11998 | * Thus, in all cases it suffices to blow away our signed bounds |
11999 | * and rely on inferring new ones from the unsigned bounds and | |
12000 | * var_off of the result. | |
12001 | */ | |
12002 | dst_reg->s32_min_value = S32_MIN; | |
12003 | dst_reg->s32_max_value = S32_MAX; | |
12004 | ||
12005 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
12006 | dst_reg->u32_min_value >>= umax_val; | |
12007 | dst_reg->u32_max_value >>= umin_val; | |
12008 | ||
12009 | __mark_reg64_unbounded(dst_reg); | |
12010 | __update_reg32_bounds(dst_reg); | |
12011 | } | |
12012 | ||
07cd2631 JF |
12013 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
12014 | struct bpf_reg_state *src_reg) | |
12015 | { | |
12016 | u64 umax_val = src_reg->umax_value; | |
12017 | u64 umin_val = src_reg->umin_value; | |
12018 | ||
12019 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
12020 | * be negative, then either: | |
12021 | * 1) src_reg might be zero, so the sign bit of the result is | |
12022 | * unknown, so we lose our signed bounds | |
12023 | * 2) it's known negative, thus the unsigned bounds capture the | |
12024 | * signed bounds | |
12025 | * 3) the signed bounds cross zero, so they tell us nothing | |
12026 | * about the result | |
12027 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 12028 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
12029 | * Thus, in all cases it suffices to blow away our signed bounds |
12030 | * and rely on inferring new ones from the unsigned bounds and | |
12031 | * var_off of the result. | |
12032 | */ | |
12033 | dst_reg->smin_value = S64_MIN; | |
12034 | dst_reg->smax_value = S64_MAX; | |
12035 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
12036 | dst_reg->umin_value >>= umax_val; | |
12037 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
12038 | |
12039 | /* Its not easy to operate on alu32 bounds here because it depends | |
12040 | * on bits being shifted in. Take easy way out and mark unbounded | |
12041 | * so we can recalculate later from tnum. | |
12042 | */ | |
12043 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
12044 | __update_reg_bounds(dst_reg); |
12045 | } | |
12046 | ||
3f50f132 JF |
12047 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
12048 | struct bpf_reg_state *src_reg) | |
07cd2631 | 12049 | { |
3f50f132 | 12050 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
12051 | |
12052 | /* Upon reaching here, src_known is true and | |
12053 | * umax_val is equal to umin_val. | |
12054 | */ | |
3f50f132 JF |
12055 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
12056 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 12057 | |
3f50f132 JF |
12058 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
12059 | ||
12060 | /* blow away the dst_reg umin_value/umax_value and rely on | |
12061 | * dst_reg var_off to refine the result. | |
12062 | */ | |
12063 | dst_reg->u32_min_value = 0; | |
12064 | dst_reg->u32_max_value = U32_MAX; | |
12065 | ||
12066 | __mark_reg64_unbounded(dst_reg); | |
12067 | __update_reg32_bounds(dst_reg); | |
12068 | } | |
12069 | ||
12070 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
12071 | struct bpf_reg_state *src_reg) | |
12072 | { | |
12073 | u64 umin_val = src_reg->umin_value; | |
12074 | ||
12075 | /* Upon reaching here, src_known is true and umax_val is equal | |
12076 | * to umin_val. | |
12077 | */ | |
12078 | dst_reg->smin_value >>= umin_val; | |
12079 | dst_reg->smax_value >>= umin_val; | |
12080 | ||
12081 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
12082 | |
12083 | /* blow away the dst_reg umin_value/umax_value and rely on | |
12084 | * dst_reg var_off to refine the result. | |
12085 | */ | |
12086 | dst_reg->umin_value = 0; | |
12087 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
12088 | |
12089 | /* Its not easy to operate on alu32 bounds here because it depends | |
12090 | * on bits being shifted in from upper 32-bits. Take easy way out | |
12091 | * and mark unbounded so we can recalculate later from tnum. | |
12092 | */ | |
12093 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
12094 | __update_reg_bounds(dst_reg); |
12095 | } | |
12096 | ||
468f6eaf JH |
12097 | /* WARNING: This function does calculations on 64-bit values, but the actual |
12098 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
12099 | * need extra checks in the 32-bit case. | |
12100 | */ | |
f1174f77 EC |
12101 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
12102 | struct bpf_insn *insn, | |
12103 | struct bpf_reg_state *dst_reg, | |
12104 | struct bpf_reg_state src_reg) | |
969bf05e | 12105 | { |
638f5b90 | 12106 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 12107 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 12108 | bool src_known; |
b03c9f9f EC |
12109 | s64 smin_val, smax_val; |
12110 | u64 umin_val, umax_val; | |
3f50f132 JF |
12111 | s32 s32_min_val, s32_max_val; |
12112 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 12113 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 12114 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 12115 | int ret; |
b799207e | 12116 | |
b03c9f9f EC |
12117 | smin_val = src_reg.smin_value; |
12118 | smax_val = src_reg.smax_value; | |
12119 | umin_val = src_reg.umin_value; | |
12120 | umax_val = src_reg.umax_value; | |
f23cc643 | 12121 | |
3f50f132 JF |
12122 | s32_min_val = src_reg.s32_min_value; |
12123 | s32_max_val = src_reg.s32_max_value; | |
12124 | u32_min_val = src_reg.u32_min_value; | |
12125 | u32_max_val = src_reg.u32_max_value; | |
12126 | ||
12127 | if (alu32) { | |
12128 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
12129 | if ((src_known && |
12130 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
12131 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
12132 | /* Taint dst register if offset had invalid bounds | |
12133 | * derived from e.g. dead branches. | |
12134 | */ | |
12135 | __mark_reg_unknown(env, dst_reg); | |
12136 | return 0; | |
12137 | } | |
12138 | } else { | |
12139 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
12140 | if ((src_known && |
12141 | (smin_val != smax_val || umin_val != umax_val)) || | |
12142 | smin_val > smax_val || umin_val > umax_val) { | |
12143 | /* Taint dst register if offset had invalid bounds | |
12144 | * derived from e.g. dead branches. | |
12145 | */ | |
12146 | __mark_reg_unknown(env, dst_reg); | |
12147 | return 0; | |
12148 | } | |
6f16101e DB |
12149 | } |
12150 | ||
bb7f0f98 AS |
12151 | if (!src_known && |
12152 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 12153 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
12154 | return 0; |
12155 | } | |
12156 | ||
f5288193 DB |
12157 | if (sanitize_needed(opcode)) { |
12158 | ret = sanitize_val_alu(env, insn); | |
12159 | if (ret < 0) | |
12160 | return sanitize_err(env, insn, ret, NULL, NULL); | |
12161 | } | |
12162 | ||
3f50f132 JF |
12163 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
12164 | * There are two classes of instructions: The first class we track both | |
12165 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
12166 | * greatest amount of precision when alu operations are mixed with jmp32 | |
12167 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
12168 | * and BPF_OR. This is possible because these ops have fairly easy to | |
12169 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
12170 | * See alu32 verifier tests for examples. The second class of | |
12171 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
12172 | * with regards to tracking sign/unsigned bounds because the bits may | |
12173 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
12174 | * the reg unbounded in the subreg bound space and use the resulting | |
12175 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
12176 | */ | |
48461135 JB |
12177 | switch (opcode) { |
12178 | case BPF_ADD: | |
3f50f132 | 12179 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 12180 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 12181 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
12182 | break; |
12183 | case BPF_SUB: | |
3f50f132 | 12184 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 12185 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 12186 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
12187 | break; |
12188 | case BPF_MUL: | |
3f50f132 JF |
12189 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
12190 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 12191 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
12192 | break; |
12193 | case BPF_AND: | |
3f50f132 JF |
12194 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
12195 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 12196 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
12197 | break; |
12198 | case BPF_OR: | |
3f50f132 JF |
12199 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
12200 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 12201 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 12202 | break; |
2921c90d YS |
12203 | case BPF_XOR: |
12204 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
12205 | scalar32_min_max_xor(dst_reg, &src_reg); | |
12206 | scalar_min_max_xor(dst_reg, &src_reg); | |
12207 | break; | |
48461135 | 12208 | case BPF_LSH: |
468f6eaf JH |
12209 | if (umax_val >= insn_bitness) { |
12210 | /* Shifts greater than 31 or 63 are undefined. | |
12211 | * This includes shifts by a negative number. | |
b03c9f9f | 12212 | */ |
61bd5218 | 12213 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
12214 | break; |
12215 | } | |
3f50f132 JF |
12216 | if (alu32) |
12217 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
12218 | else | |
12219 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
12220 | break; |
12221 | case BPF_RSH: | |
468f6eaf JH |
12222 | if (umax_val >= insn_bitness) { |
12223 | /* Shifts greater than 31 or 63 are undefined. | |
12224 | * This includes shifts by a negative number. | |
b03c9f9f | 12225 | */ |
61bd5218 | 12226 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
12227 | break; |
12228 | } | |
3f50f132 JF |
12229 | if (alu32) |
12230 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
12231 | else | |
12232 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 12233 | break; |
9cbe1f5a YS |
12234 | case BPF_ARSH: |
12235 | if (umax_val >= insn_bitness) { | |
12236 | /* Shifts greater than 31 or 63 are undefined. | |
12237 | * This includes shifts by a negative number. | |
12238 | */ | |
12239 | mark_reg_unknown(env, regs, insn->dst_reg); | |
12240 | break; | |
12241 | } | |
3f50f132 JF |
12242 | if (alu32) |
12243 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
12244 | else | |
12245 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 12246 | break; |
48461135 | 12247 | default: |
61bd5218 | 12248 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
12249 | break; |
12250 | } | |
12251 | ||
3f50f132 JF |
12252 | /* ALU32 ops are zero extended into 64bit register */ |
12253 | if (alu32) | |
12254 | zext_32_to_64(dst_reg); | |
3844d153 | 12255 | reg_bounds_sync(dst_reg); |
f1174f77 EC |
12256 | return 0; |
12257 | } | |
12258 | ||
12259 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
12260 | * and var_off. | |
12261 | */ | |
12262 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
12263 | struct bpf_insn *insn) | |
12264 | { | |
f4d7e40a AS |
12265 | struct bpf_verifier_state *vstate = env->cur_state; |
12266 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
12267 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
12268 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
12269 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 12270 | int err; |
f1174f77 EC |
12271 | |
12272 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
12273 | src_reg = NULL; |
12274 | if (dst_reg->type != SCALAR_VALUE) | |
12275 | ptr_reg = dst_reg; | |
75748837 AS |
12276 | else |
12277 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
12278 | * incorrectly propagated into other registers by find_equal_scalars() | |
12279 | */ | |
12280 | dst_reg->id = 0; | |
f1174f77 EC |
12281 | if (BPF_SRC(insn->code) == BPF_X) { |
12282 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
12283 | if (src_reg->type != SCALAR_VALUE) { |
12284 | if (dst_reg->type != SCALAR_VALUE) { | |
12285 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
12286 | * an arbitrary scalar. Disallow all math except |
12287 | * pointer subtraction | |
f1174f77 | 12288 | */ |
dd066823 | 12289 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
12290 | mark_reg_unknown(env, regs, insn->dst_reg); |
12291 | return 0; | |
f1174f77 | 12292 | } |
82abbf8d AS |
12293 | verbose(env, "R%d pointer %s pointer prohibited\n", |
12294 | insn->dst_reg, | |
12295 | bpf_alu_string[opcode >> 4]); | |
12296 | return -EACCES; | |
f1174f77 EC |
12297 | } else { |
12298 | /* scalar += pointer | |
12299 | * This is legal, but we have to reverse our | |
12300 | * src/dest handling in computing the range | |
12301 | */ | |
b5dc0163 AS |
12302 | err = mark_chain_precision(env, insn->dst_reg); |
12303 | if (err) | |
12304 | return err; | |
82abbf8d AS |
12305 | return adjust_ptr_min_max_vals(env, insn, |
12306 | src_reg, dst_reg); | |
f1174f77 EC |
12307 | } |
12308 | } else if (ptr_reg) { | |
12309 | /* pointer += scalar */ | |
b5dc0163 AS |
12310 | err = mark_chain_precision(env, insn->src_reg); |
12311 | if (err) | |
12312 | return err; | |
82abbf8d AS |
12313 | return adjust_ptr_min_max_vals(env, insn, |
12314 | dst_reg, src_reg); | |
a3b666bf AN |
12315 | } else if (dst_reg->precise) { |
12316 | /* if dst_reg is precise, src_reg should be precise as well */ | |
12317 | err = mark_chain_precision(env, insn->src_reg); | |
12318 | if (err) | |
12319 | return err; | |
f1174f77 EC |
12320 | } |
12321 | } else { | |
12322 | /* Pretend the src is a reg with a known value, since we only | |
12323 | * need to be able to read from this state. | |
12324 | */ | |
12325 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 12326 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 12327 | src_reg = &off_reg; |
82abbf8d AS |
12328 | if (ptr_reg) /* pointer += K */ |
12329 | return adjust_ptr_min_max_vals(env, insn, | |
12330 | ptr_reg, src_reg); | |
f1174f77 EC |
12331 | } |
12332 | ||
12333 | /* Got here implies adding two SCALAR_VALUEs */ | |
12334 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 12335 | print_verifier_state(env, state, true); |
61bd5218 | 12336 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
12337 | return -EINVAL; |
12338 | } | |
12339 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 12340 | print_verifier_state(env, state, true); |
61bd5218 | 12341 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
12342 | return -EINVAL; |
12343 | } | |
12344 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
12345 | } |
12346 | ||
17a52670 | 12347 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 12348 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 12349 | { |
638f5b90 | 12350 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
12351 | u8 opcode = BPF_OP(insn->code); |
12352 | int err; | |
12353 | ||
12354 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
12355 | if (opcode == BPF_NEG) { | |
395e942d | 12356 | if (BPF_SRC(insn->code) != BPF_K || |
17a52670 AS |
12357 | insn->src_reg != BPF_REG_0 || |
12358 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 12359 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
12360 | return -EINVAL; |
12361 | } | |
12362 | } else { | |
12363 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
12364 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
12365 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 12366 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
12367 | return -EINVAL; |
12368 | } | |
12369 | } | |
12370 | ||
12371 | /* check src operand */ | |
dc503a8a | 12372 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12373 | if (err) |
12374 | return err; | |
12375 | ||
1be7f75d | 12376 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 12377 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
12378 | insn->dst_reg); |
12379 | return -EACCES; | |
12380 | } | |
12381 | ||
17a52670 | 12382 | /* check dest operand */ |
dc503a8a | 12383 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
12384 | if (err) |
12385 | return err; | |
12386 | ||
12387 | } else if (opcode == BPF_MOV) { | |
12388 | ||
12389 | if (BPF_SRC(insn->code) == BPF_X) { | |
12390 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 12391 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
12392 | return -EINVAL; |
12393 | } | |
12394 | ||
12395 | /* check src operand */ | |
dc503a8a | 12396 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12397 | if (err) |
12398 | return err; | |
12399 | } else { | |
12400 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 12401 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
12402 | return -EINVAL; |
12403 | } | |
12404 | } | |
12405 | ||
fbeb1603 AF |
12406 | /* check dest operand, mark as required later */ |
12407 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
12408 | if (err) |
12409 | return err; | |
12410 | ||
12411 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
12412 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
12413 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
12414 | ||
17a52670 AS |
12415 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
12416 | /* case: R1 = R2 | |
12417 | * copy register state to dest reg | |
12418 | */ | |
75748837 AS |
12419 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
12420 | /* Assign src and dst registers the same ID | |
12421 | * that will be used by find_equal_scalars() | |
12422 | * to propagate min/max range. | |
12423 | */ | |
12424 | src_reg->id = ++env->id_gen; | |
71f656a5 | 12425 | copy_register_state(dst_reg, src_reg); |
e434b8cd | 12426 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 12427 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 12428 | } else { |
f1174f77 | 12429 | /* R1 = (u32) R2 */ |
1be7f75d | 12430 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
12431 | verbose(env, |
12432 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
12433 | insn->src_reg); |
12434 | return -EACCES; | |
e434b8cd | 12435 | } else if (src_reg->type == SCALAR_VALUE) { |
71f656a5 | 12436 | copy_register_state(dst_reg, src_reg); |
75748837 AS |
12437 | /* Make sure ID is cleared otherwise |
12438 | * dst_reg min/max could be incorrectly | |
12439 | * propagated into src_reg by find_equal_scalars() | |
12440 | */ | |
12441 | dst_reg->id = 0; | |
e434b8cd | 12442 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 12443 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
12444 | } else { |
12445 | mark_reg_unknown(env, regs, | |
12446 | insn->dst_reg); | |
1be7f75d | 12447 | } |
3f50f132 | 12448 | zext_32_to_64(dst_reg); |
3844d153 | 12449 | reg_bounds_sync(dst_reg); |
17a52670 AS |
12450 | } |
12451 | } else { | |
12452 | /* case: R = imm | |
12453 | * remember the value we stored into this reg | |
12454 | */ | |
fbeb1603 AF |
12455 | /* clear any state __mark_reg_known doesn't set */ |
12456 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 12457 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
12458 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
12459 | __mark_reg_known(regs + insn->dst_reg, | |
12460 | insn->imm); | |
12461 | } else { | |
12462 | __mark_reg_known(regs + insn->dst_reg, | |
12463 | (u32)insn->imm); | |
12464 | } | |
17a52670 AS |
12465 | } |
12466 | ||
12467 | } else if (opcode > BPF_END) { | |
61bd5218 | 12468 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
12469 | return -EINVAL; |
12470 | ||
12471 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
12472 | ||
17a52670 AS |
12473 | if (BPF_SRC(insn->code) == BPF_X) { |
12474 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 12475 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
12476 | return -EINVAL; |
12477 | } | |
12478 | /* check src1 operand */ | |
dc503a8a | 12479 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12480 | if (err) |
12481 | return err; | |
12482 | } else { | |
12483 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 12484 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
12485 | return -EINVAL; |
12486 | } | |
12487 | } | |
12488 | ||
12489 | /* check src2 operand */ | |
dc503a8a | 12490 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12491 | if (err) |
12492 | return err; | |
12493 | ||
12494 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
12495 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 12496 | verbose(env, "div by zero\n"); |
17a52670 AS |
12497 | return -EINVAL; |
12498 | } | |
12499 | ||
229394e8 RV |
12500 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
12501 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
12502 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
12503 | ||
12504 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 12505 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
12506 | return -EINVAL; |
12507 | } | |
12508 | } | |
12509 | ||
1a0dc1ac | 12510 | /* check dest operand */ |
dc503a8a | 12511 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
12512 | if (err) |
12513 | return err; | |
12514 | ||
f1174f77 | 12515 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
12516 | } |
12517 | ||
12518 | return 0; | |
12519 | } | |
12520 | ||
f4d7e40a | 12521 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 12522 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 12523 | enum bpf_reg_type type, |
fb2a311a | 12524 | bool range_right_open) |
969bf05e | 12525 | { |
b239da34 KKD |
12526 | struct bpf_func_state *state; |
12527 | struct bpf_reg_state *reg; | |
12528 | int new_range; | |
2d2be8ca | 12529 | |
fb2a311a DB |
12530 | if (dst_reg->off < 0 || |
12531 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
12532 | /* This doesn't give us any range */ |
12533 | return; | |
12534 | ||
b03c9f9f EC |
12535 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
12536 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
12537 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
12538 | * than pkt_end, but that's because it's also less than pkt. | |
12539 | */ | |
12540 | return; | |
12541 | ||
fb2a311a DB |
12542 | new_range = dst_reg->off; |
12543 | if (range_right_open) | |
2fa7d94a | 12544 | new_range++; |
fb2a311a DB |
12545 | |
12546 | /* Examples for register markings: | |
2d2be8ca | 12547 | * |
fb2a311a | 12548 | * pkt_data in dst register: |
2d2be8ca DB |
12549 | * |
12550 | * r2 = r3; | |
12551 | * r2 += 8; | |
12552 | * if (r2 > pkt_end) goto <handle exception> | |
12553 | * <access okay> | |
12554 | * | |
b4e432f1 DB |
12555 | * r2 = r3; |
12556 | * r2 += 8; | |
12557 | * if (r2 < pkt_end) goto <access okay> | |
12558 | * <handle exception> | |
12559 | * | |
2d2be8ca DB |
12560 | * Where: |
12561 | * r2 == dst_reg, pkt_end == src_reg | |
12562 | * r2=pkt(id=n,off=8,r=0) | |
12563 | * r3=pkt(id=n,off=0,r=0) | |
12564 | * | |
fb2a311a | 12565 | * pkt_data in src register: |
2d2be8ca DB |
12566 | * |
12567 | * r2 = r3; | |
12568 | * r2 += 8; | |
12569 | * if (pkt_end >= r2) goto <access okay> | |
12570 | * <handle exception> | |
12571 | * | |
b4e432f1 DB |
12572 | * r2 = r3; |
12573 | * r2 += 8; | |
12574 | * if (pkt_end <= r2) goto <handle exception> | |
12575 | * <access okay> | |
12576 | * | |
2d2be8ca DB |
12577 | * Where: |
12578 | * pkt_end == dst_reg, r2 == src_reg | |
12579 | * r2=pkt(id=n,off=8,r=0) | |
12580 | * r3=pkt(id=n,off=0,r=0) | |
12581 | * | |
12582 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
12583 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
12584 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
12585 | * the check. | |
969bf05e | 12586 | */ |
2d2be8ca | 12587 | |
f1174f77 EC |
12588 | /* If our ids match, then we must have the same max_value. And we |
12589 | * don't care about the other reg's fixed offset, since if it's too big | |
12590 | * the range won't allow anything. | |
12591 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
12592 | */ | |
b239da34 KKD |
12593 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
12594 | if (reg->type == type && reg->id == dst_reg->id) | |
12595 | /* keep the maximum range already checked */ | |
12596 | reg->range = max(reg->range, new_range); | |
12597 | })); | |
969bf05e AS |
12598 | } |
12599 | ||
3f50f132 | 12600 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 12601 | { |
3f50f132 JF |
12602 | struct tnum subreg = tnum_subreg(reg->var_off); |
12603 | s32 sval = (s32)val; | |
a72dafaf | 12604 | |
3f50f132 JF |
12605 | switch (opcode) { |
12606 | case BPF_JEQ: | |
12607 | if (tnum_is_const(subreg)) | |
12608 | return !!tnum_equals_const(subreg, val); | |
12609 | break; | |
12610 | case BPF_JNE: | |
12611 | if (tnum_is_const(subreg)) | |
12612 | return !tnum_equals_const(subreg, val); | |
12613 | break; | |
12614 | case BPF_JSET: | |
12615 | if ((~subreg.mask & subreg.value) & val) | |
12616 | return 1; | |
12617 | if (!((subreg.mask | subreg.value) & val)) | |
12618 | return 0; | |
12619 | break; | |
12620 | case BPF_JGT: | |
12621 | if (reg->u32_min_value > val) | |
12622 | return 1; | |
12623 | else if (reg->u32_max_value <= val) | |
12624 | return 0; | |
12625 | break; | |
12626 | case BPF_JSGT: | |
12627 | if (reg->s32_min_value > sval) | |
12628 | return 1; | |
ee114dd6 | 12629 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
12630 | return 0; |
12631 | break; | |
12632 | case BPF_JLT: | |
12633 | if (reg->u32_max_value < val) | |
12634 | return 1; | |
12635 | else if (reg->u32_min_value >= val) | |
12636 | return 0; | |
12637 | break; | |
12638 | case BPF_JSLT: | |
12639 | if (reg->s32_max_value < sval) | |
12640 | return 1; | |
12641 | else if (reg->s32_min_value >= sval) | |
12642 | return 0; | |
12643 | break; | |
12644 | case BPF_JGE: | |
12645 | if (reg->u32_min_value >= val) | |
12646 | return 1; | |
12647 | else if (reg->u32_max_value < val) | |
12648 | return 0; | |
12649 | break; | |
12650 | case BPF_JSGE: | |
12651 | if (reg->s32_min_value >= sval) | |
12652 | return 1; | |
12653 | else if (reg->s32_max_value < sval) | |
12654 | return 0; | |
12655 | break; | |
12656 | case BPF_JLE: | |
12657 | if (reg->u32_max_value <= val) | |
12658 | return 1; | |
12659 | else if (reg->u32_min_value > val) | |
12660 | return 0; | |
12661 | break; | |
12662 | case BPF_JSLE: | |
12663 | if (reg->s32_max_value <= sval) | |
12664 | return 1; | |
12665 | else if (reg->s32_min_value > sval) | |
12666 | return 0; | |
12667 | break; | |
12668 | } | |
4f7b3e82 | 12669 | |
3f50f132 JF |
12670 | return -1; |
12671 | } | |
092ed096 | 12672 | |
3f50f132 JF |
12673 | |
12674 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
12675 | { | |
12676 | s64 sval = (s64)val; | |
a72dafaf | 12677 | |
4f7b3e82 AS |
12678 | switch (opcode) { |
12679 | case BPF_JEQ: | |
12680 | if (tnum_is_const(reg->var_off)) | |
12681 | return !!tnum_equals_const(reg->var_off, val); | |
12682 | break; | |
12683 | case BPF_JNE: | |
12684 | if (tnum_is_const(reg->var_off)) | |
12685 | return !tnum_equals_const(reg->var_off, val); | |
12686 | break; | |
960ea056 JK |
12687 | case BPF_JSET: |
12688 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
12689 | return 1; | |
12690 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
12691 | return 0; | |
12692 | break; | |
4f7b3e82 AS |
12693 | case BPF_JGT: |
12694 | if (reg->umin_value > val) | |
12695 | return 1; | |
12696 | else if (reg->umax_value <= val) | |
12697 | return 0; | |
12698 | break; | |
12699 | case BPF_JSGT: | |
a72dafaf | 12700 | if (reg->smin_value > sval) |
4f7b3e82 | 12701 | return 1; |
ee114dd6 | 12702 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
12703 | return 0; |
12704 | break; | |
12705 | case BPF_JLT: | |
12706 | if (reg->umax_value < val) | |
12707 | return 1; | |
12708 | else if (reg->umin_value >= val) | |
12709 | return 0; | |
12710 | break; | |
12711 | case BPF_JSLT: | |
a72dafaf | 12712 | if (reg->smax_value < sval) |
4f7b3e82 | 12713 | return 1; |
a72dafaf | 12714 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
12715 | return 0; |
12716 | break; | |
12717 | case BPF_JGE: | |
12718 | if (reg->umin_value >= val) | |
12719 | return 1; | |
12720 | else if (reg->umax_value < val) | |
12721 | return 0; | |
12722 | break; | |
12723 | case BPF_JSGE: | |
a72dafaf | 12724 | if (reg->smin_value >= sval) |
4f7b3e82 | 12725 | return 1; |
a72dafaf | 12726 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
12727 | return 0; |
12728 | break; | |
12729 | case BPF_JLE: | |
12730 | if (reg->umax_value <= val) | |
12731 | return 1; | |
12732 | else if (reg->umin_value > val) | |
12733 | return 0; | |
12734 | break; | |
12735 | case BPF_JSLE: | |
a72dafaf | 12736 | if (reg->smax_value <= sval) |
4f7b3e82 | 12737 | return 1; |
a72dafaf | 12738 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
12739 | return 0; |
12740 | break; | |
12741 | } | |
12742 | ||
12743 | return -1; | |
12744 | } | |
12745 | ||
3f50f132 JF |
12746 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
12747 | * and return: | |
12748 | * 1 - branch will be taken and "goto target" will be executed | |
12749 | * 0 - branch will not be taken and fall-through to next insn | |
12750 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
12751 | * range [0,10] | |
604dca5e | 12752 | */ |
3f50f132 JF |
12753 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
12754 | bool is_jmp32) | |
604dca5e | 12755 | { |
cac616db JF |
12756 | if (__is_pointer_value(false, reg)) { |
12757 | if (!reg_type_not_null(reg->type)) | |
12758 | return -1; | |
12759 | ||
12760 | /* If pointer is valid tests against zero will fail so we can | |
12761 | * use this to direct branch taken. | |
12762 | */ | |
12763 | if (val != 0) | |
12764 | return -1; | |
12765 | ||
12766 | switch (opcode) { | |
12767 | case BPF_JEQ: | |
12768 | return 0; | |
12769 | case BPF_JNE: | |
12770 | return 1; | |
12771 | default: | |
12772 | return -1; | |
12773 | } | |
12774 | } | |
604dca5e | 12775 | |
3f50f132 JF |
12776 | if (is_jmp32) |
12777 | return is_branch32_taken(reg, val, opcode); | |
12778 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
12779 | } |
12780 | ||
6d94e741 AS |
12781 | static int flip_opcode(u32 opcode) |
12782 | { | |
12783 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
12784 | static const u8 opcode_flip[16] = { | |
12785 | /* these stay the same */ | |
12786 | [BPF_JEQ >> 4] = BPF_JEQ, | |
12787 | [BPF_JNE >> 4] = BPF_JNE, | |
12788 | [BPF_JSET >> 4] = BPF_JSET, | |
12789 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
12790 | [BPF_JGE >> 4] = BPF_JLE, | |
12791 | [BPF_JGT >> 4] = BPF_JLT, | |
12792 | [BPF_JLE >> 4] = BPF_JGE, | |
12793 | [BPF_JLT >> 4] = BPF_JGT, | |
12794 | [BPF_JSGE >> 4] = BPF_JSLE, | |
12795 | [BPF_JSGT >> 4] = BPF_JSLT, | |
12796 | [BPF_JSLE >> 4] = BPF_JSGE, | |
12797 | [BPF_JSLT >> 4] = BPF_JSGT | |
12798 | }; | |
12799 | return opcode_flip[opcode >> 4]; | |
12800 | } | |
12801 | ||
12802 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
12803 | struct bpf_reg_state *src_reg, | |
12804 | u8 opcode) | |
12805 | { | |
12806 | struct bpf_reg_state *pkt; | |
12807 | ||
12808 | if (src_reg->type == PTR_TO_PACKET_END) { | |
12809 | pkt = dst_reg; | |
12810 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
12811 | pkt = src_reg; | |
12812 | opcode = flip_opcode(opcode); | |
12813 | } else { | |
12814 | return -1; | |
12815 | } | |
12816 | ||
12817 | if (pkt->range >= 0) | |
12818 | return -1; | |
12819 | ||
12820 | switch (opcode) { | |
12821 | case BPF_JLE: | |
12822 | /* pkt <= pkt_end */ | |
12823 | fallthrough; | |
12824 | case BPF_JGT: | |
12825 | /* pkt > pkt_end */ | |
12826 | if (pkt->range == BEYOND_PKT_END) | |
12827 | /* pkt has at last one extra byte beyond pkt_end */ | |
12828 | return opcode == BPF_JGT; | |
12829 | break; | |
12830 | case BPF_JLT: | |
12831 | /* pkt < pkt_end */ | |
12832 | fallthrough; | |
12833 | case BPF_JGE: | |
12834 | /* pkt >= pkt_end */ | |
12835 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
12836 | return opcode == BPF_JGE; | |
12837 | break; | |
12838 | } | |
12839 | return -1; | |
12840 | } | |
12841 | ||
48461135 JB |
12842 | /* Adjusts the register min/max values in the case that the dst_reg is the |
12843 | * variable register that we are working on, and src_reg is a constant or we're | |
12844 | * simply doing a BPF_K check. | |
f1174f77 | 12845 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
12846 | */ |
12847 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
12848 | struct bpf_reg_state *false_reg, |
12849 | u64 val, u32 val32, | |
092ed096 | 12850 | u8 opcode, bool is_jmp32) |
48461135 | 12851 | { |
3f50f132 JF |
12852 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
12853 | struct tnum false_64off = false_reg->var_off; | |
12854 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
12855 | struct tnum true_64off = true_reg->var_off; | |
12856 | s64 sval = (s64)val; | |
12857 | s32 sval32 = (s32)val32; | |
a72dafaf | 12858 | |
f1174f77 EC |
12859 | /* If the dst_reg is a pointer, we can't learn anything about its |
12860 | * variable offset from the compare (unless src_reg were a pointer into | |
12861 | * the same object, but we don't bother with that. | |
12862 | * Since false_reg and true_reg have the same type by construction, we | |
12863 | * only need to check one of them for pointerness. | |
12864 | */ | |
12865 | if (__is_pointer_value(false, false_reg)) | |
12866 | return; | |
4cabc5b1 | 12867 | |
48461135 | 12868 | switch (opcode) { |
a12ca627 DB |
12869 | /* JEQ/JNE comparison doesn't change the register equivalence. |
12870 | * | |
12871 | * r1 = r2; | |
12872 | * if (r1 == 42) goto label; | |
12873 | * ... | |
12874 | * label: // here both r1 and r2 are known to be 42. | |
12875 | * | |
12876 | * Hence when marking register as known preserve it's ID. | |
12877 | */ | |
48461135 | 12878 | case BPF_JEQ: |
a12ca627 DB |
12879 | if (is_jmp32) { |
12880 | __mark_reg32_known(true_reg, val32); | |
12881 | true_32off = tnum_subreg(true_reg->var_off); | |
12882 | } else { | |
12883 | ___mark_reg_known(true_reg, val); | |
12884 | true_64off = true_reg->var_off; | |
12885 | } | |
12886 | break; | |
48461135 | 12887 | case BPF_JNE: |
a12ca627 DB |
12888 | if (is_jmp32) { |
12889 | __mark_reg32_known(false_reg, val32); | |
12890 | false_32off = tnum_subreg(false_reg->var_off); | |
12891 | } else { | |
12892 | ___mark_reg_known(false_reg, val); | |
12893 | false_64off = false_reg->var_off; | |
12894 | } | |
48461135 | 12895 | break; |
960ea056 | 12896 | case BPF_JSET: |
3f50f132 JF |
12897 | if (is_jmp32) { |
12898 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
12899 | if (is_power_of_2(val32)) | |
12900 | true_32off = tnum_or(true_32off, | |
12901 | tnum_const(val32)); | |
12902 | } else { | |
12903 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
12904 | if (is_power_of_2(val)) | |
12905 | true_64off = tnum_or(true_64off, | |
12906 | tnum_const(val)); | |
12907 | } | |
960ea056 | 12908 | break; |
48461135 | 12909 | case BPF_JGE: |
a72dafaf JW |
12910 | case BPF_JGT: |
12911 | { | |
3f50f132 JF |
12912 | if (is_jmp32) { |
12913 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
12914 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
12915 | ||
12916 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
12917 | false_umax); | |
12918 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
12919 | true_umin); | |
12920 | } else { | |
12921 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
12922 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
12923 | ||
12924 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
12925 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
12926 | } | |
b03c9f9f | 12927 | break; |
a72dafaf | 12928 | } |
48461135 | 12929 | case BPF_JSGE: |
a72dafaf JW |
12930 | case BPF_JSGT: |
12931 | { | |
3f50f132 JF |
12932 | if (is_jmp32) { |
12933 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
12934 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 12935 | |
3f50f132 JF |
12936 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
12937 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
12938 | } else { | |
12939 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
12940 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
12941 | ||
12942 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
12943 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
12944 | } | |
48461135 | 12945 | break; |
a72dafaf | 12946 | } |
b4e432f1 | 12947 | case BPF_JLE: |
a72dafaf JW |
12948 | case BPF_JLT: |
12949 | { | |
3f50f132 JF |
12950 | if (is_jmp32) { |
12951 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
12952 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
12953 | ||
12954 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
12955 | false_umin); | |
12956 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
12957 | true_umax); | |
12958 | } else { | |
12959 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
12960 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
12961 | ||
12962 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
12963 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
12964 | } | |
b4e432f1 | 12965 | break; |
a72dafaf | 12966 | } |
b4e432f1 | 12967 | case BPF_JSLE: |
a72dafaf JW |
12968 | case BPF_JSLT: |
12969 | { | |
3f50f132 JF |
12970 | if (is_jmp32) { |
12971 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
12972 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 12973 | |
3f50f132 JF |
12974 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
12975 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
12976 | } else { | |
12977 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
12978 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
12979 | ||
12980 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
12981 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
12982 | } | |
b4e432f1 | 12983 | break; |
a72dafaf | 12984 | } |
48461135 | 12985 | default: |
0fc31b10 | 12986 | return; |
48461135 JB |
12987 | } |
12988 | ||
3f50f132 JF |
12989 | if (is_jmp32) { |
12990 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
12991 | tnum_subreg(false_32off)); | |
12992 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
12993 | tnum_subreg(true_32off)); | |
12994 | __reg_combine_32_into_64(false_reg); | |
12995 | __reg_combine_32_into_64(true_reg); | |
12996 | } else { | |
12997 | false_reg->var_off = false_64off; | |
12998 | true_reg->var_off = true_64off; | |
12999 | __reg_combine_64_into_32(false_reg); | |
13000 | __reg_combine_64_into_32(true_reg); | |
13001 | } | |
48461135 JB |
13002 | } |
13003 | ||
f1174f77 EC |
13004 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
13005 | * the variable reg. | |
48461135 JB |
13006 | */ |
13007 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
13008 | struct bpf_reg_state *false_reg, |
13009 | u64 val, u32 val32, | |
092ed096 | 13010 | u8 opcode, bool is_jmp32) |
48461135 | 13011 | { |
6d94e741 | 13012 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
13013 | /* This uses zero as "not present in table"; luckily the zero opcode, |
13014 | * BPF_JA, can't get here. | |
b03c9f9f | 13015 | */ |
0fc31b10 | 13016 | if (opcode) |
3f50f132 | 13017 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
13018 | } |
13019 | ||
13020 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
13021 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
13022 | struct bpf_reg_state *dst_reg) | |
13023 | { | |
b03c9f9f EC |
13024 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
13025 | dst_reg->umin_value); | |
13026 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
13027 | dst_reg->umax_value); | |
13028 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
13029 | dst_reg->smin_value); | |
13030 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
13031 | dst_reg->smax_value); | |
f1174f77 EC |
13032 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
13033 | dst_reg->var_off); | |
3844d153 DB |
13034 | reg_bounds_sync(src_reg); |
13035 | reg_bounds_sync(dst_reg); | |
f1174f77 EC |
13036 | } |
13037 | ||
13038 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
13039 | struct bpf_reg_state *true_dst, | |
13040 | struct bpf_reg_state *false_src, | |
13041 | struct bpf_reg_state *false_dst, | |
13042 | u8 opcode) | |
13043 | { | |
13044 | switch (opcode) { | |
13045 | case BPF_JEQ: | |
13046 | __reg_combine_min_max(true_src, true_dst); | |
13047 | break; | |
13048 | case BPF_JNE: | |
13049 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 13050 | break; |
4cabc5b1 | 13051 | } |
48461135 JB |
13052 | } |
13053 | ||
fd978bf7 JS |
13054 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
13055 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 13056 | bool is_null) |
57a09bf0 | 13057 | { |
c25b2ae1 | 13058 | if (type_may_be_null(reg->type) && reg->id == id && |
fca1aa75 | 13059 | (is_rcu_reg(reg) || !WARN_ON_ONCE(!reg->id))) { |
df57f38a KKD |
13060 | /* Old offset (both fixed and variable parts) should have been |
13061 | * known-zero, because we don't allow pointer arithmetic on | |
13062 | * pointers that might be NULL. If we see this happening, don't | |
13063 | * convert the register. | |
13064 | * | |
13065 | * But in some cases, some helpers that return local kptrs | |
13066 | * advance offset for the returned pointer. In those cases, it | |
13067 | * is fine to expect to see reg->off. | |
13068 | */ | |
13069 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || !tnum_equals_const(reg->var_off, 0))) | |
13070 | return; | |
6a3cd331 DM |
13071 | if (!(type_is_ptr_alloc_obj(reg->type) || type_is_non_owning_ref(reg->type)) && |
13072 | WARN_ON_ONCE(reg->off)) | |
e60b0d12 | 13073 | return; |
6a3cd331 | 13074 | |
f1174f77 EC |
13075 | if (is_null) { |
13076 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
13077 | /* We don't need id and ref_obj_id from this point |
13078 | * onwards anymore, thus we should better reset it, | |
13079 | * so that state pruning has chances to take effect. | |
13080 | */ | |
13081 | reg->id = 0; | |
13082 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
13083 | |
13084 | return; | |
13085 | } | |
13086 | ||
13087 | mark_ptr_not_null_reg(reg); | |
13088 | ||
13089 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 | 13090 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
b239da34 | 13091 | * in release_reference(). |
1b986589 MKL |
13092 | * |
13093 | * reg->id is still used by spin_lock ptr. Other | |
13094 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
13095 | */ |
13096 | reg->id = 0; | |
56f668df | 13097 | } |
57a09bf0 TG |
13098 | } |
13099 | } | |
13100 | ||
13101 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
13102 | * be folded together at some point. | |
13103 | */ | |
840b9615 JS |
13104 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
13105 | bool is_null) | |
57a09bf0 | 13106 | { |
f4d7e40a | 13107 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
b239da34 | 13108 | struct bpf_reg_state *regs = state->regs, *reg; |
1b986589 | 13109 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 13110 | u32 id = regs[regno].id; |
57a09bf0 | 13111 | |
1b986589 MKL |
13112 | if (ref_obj_id && ref_obj_id == id && is_null) |
13113 | /* regs[regno] is in the " == NULL" branch. | |
13114 | * No one could have freed the reference state before | |
13115 | * doing the NULL check. | |
13116 | */ | |
13117 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 13118 | |
b239da34 KKD |
13119 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
13120 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
13121 | })); | |
57a09bf0 TG |
13122 | } |
13123 | ||
5beca081 DB |
13124 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
13125 | struct bpf_reg_state *dst_reg, | |
13126 | struct bpf_reg_state *src_reg, | |
13127 | struct bpf_verifier_state *this_branch, | |
13128 | struct bpf_verifier_state *other_branch) | |
13129 | { | |
13130 | if (BPF_SRC(insn->code) != BPF_X) | |
13131 | return false; | |
13132 | ||
092ed096 JW |
13133 | /* Pointers are always 64-bit. */ |
13134 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
13135 | return false; | |
13136 | ||
5beca081 DB |
13137 | switch (BPF_OP(insn->code)) { |
13138 | case BPF_JGT: | |
13139 | if ((dst_reg->type == PTR_TO_PACKET && | |
13140 | src_reg->type == PTR_TO_PACKET_END) || | |
13141 | (dst_reg->type == PTR_TO_PACKET_META && | |
13142 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13143 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
13144 | find_good_pkt_pointers(this_branch, dst_reg, | |
13145 | dst_reg->type, false); | |
6d94e741 | 13146 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
13147 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13148 | src_reg->type == PTR_TO_PACKET) || | |
13149 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13150 | src_reg->type == PTR_TO_PACKET_META)) { | |
13151 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
13152 | find_good_pkt_pointers(other_branch, src_reg, | |
13153 | src_reg->type, true); | |
6d94e741 | 13154 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
13155 | } else { |
13156 | return false; | |
13157 | } | |
13158 | break; | |
13159 | case BPF_JLT: | |
13160 | if ((dst_reg->type == PTR_TO_PACKET && | |
13161 | src_reg->type == PTR_TO_PACKET_END) || | |
13162 | (dst_reg->type == PTR_TO_PACKET_META && | |
13163 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13164 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
13165 | find_good_pkt_pointers(other_branch, dst_reg, | |
13166 | dst_reg->type, true); | |
6d94e741 | 13167 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
13168 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13169 | src_reg->type == PTR_TO_PACKET) || | |
13170 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13171 | src_reg->type == PTR_TO_PACKET_META)) { | |
13172 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
13173 | find_good_pkt_pointers(this_branch, src_reg, | |
13174 | src_reg->type, false); | |
6d94e741 | 13175 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
13176 | } else { |
13177 | return false; | |
13178 | } | |
13179 | break; | |
13180 | case BPF_JGE: | |
13181 | if ((dst_reg->type == PTR_TO_PACKET && | |
13182 | src_reg->type == PTR_TO_PACKET_END) || | |
13183 | (dst_reg->type == PTR_TO_PACKET_META && | |
13184 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13185 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
13186 | find_good_pkt_pointers(this_branch, dst_reg, | |
13187 | dst_reg->type, true); | |
6d94e741 | 13188 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
13189 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13190 | src_reg->type == PTR_TO_PACKET) || | |
13191 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13192 | src_reg->type == PTR_TO_PACKET_META)) { | |
13193 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
13194 | find_good_pkt_pointers(other_branch, src_reg, | |
13195 | src_reg->type, false); | |
6d94e741 | 13196 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
13197 | } else { |
13198 | return false; | |
13199 | } | |
13200 | break; | |
13201 | case BPF_JLE: | |
13202 | if ((dst_reg->type == PTR_TO_PACKET && | |
13203 | src_reg->type == PTR_TO_PACKET_END) || | |
13204 | (dst_reg->type == PTR_TO_PACKET_META && | |
13205 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13206 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
13207 | find_good_pkt_pointers(other_branch, dst_reg, | |
13208 | dst_reg->type, false); | |
6d94e741 | 13209 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
13210 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13211 | src_reg->type == PTR_TO_PACKET) || | |
13212 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13213 | src_reg->type == PTR_TO_PACKET_META)) { | |
13214 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
13215 | find_good_pkt_pointers(this_branch, src_reg, | |
13216 | src_reg->type, true); | |
6d94e741 | 13217 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
13218 | } else { |
13219 | return false; | |
13220 | } | |
13221 | break; | |
13222 | default: | |
13223 | return false; | |
13224 | } | |
13225 | ||
13226 | return true; | |
13227 | } | |
13228 | ||
75748837 AS |
13229 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
13230 | struct bpf_reg_state *known_reg) | |
13231 | { | |
13232 | struct bpf_func_state *state; | |
13233 | struct bpf_reg_state *reg; | |
75748837 | 13234 | |
b239da34 KKD |
13235 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
13236 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
71f656a5 | 13237 | copy_register_state(reg, known_reg); |
b239da34 | 13238 | })); |
75748837 AS |
13239 | } |
13240 | ||
58e2af8b | 13241 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
13242 | struct bpf_insn *insn, int *insn_idx) |
13243 | { | |
f4d7e40a AS |
13244 | struct bpf_verifier_state *this_branch = env->cur_state; |
13245 | struct bpf_verifier_state *other_branch; | |
13246 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 13247 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
befae758 | 13248 | struct bpf_reg_state *eq_branch_regs; |
17a52670 | 13249 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 13250 | bool is_jmp32; |
fb8d251e | 13251 | int pred = -1; |
17a52670 AS |
13252 | int err; |
13253 | ||
092ed096 JW |
13254 | /* Only conditional jumps are expected to reach here. */ |
13255 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
13256 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
13257 | return -EINVAL; |
13258 | } | |
13259 | ||
13260 | if (BPF_SRC(insn->code) == BPF_X) { | |
13261 | if (insn->imm != 0) { | |
092ed096 | 13262 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
13263 | return -EINVAL; |
13264 | } | |
13265 | ||
13266 | /* check src1 operand */ | |
dc503a8a | 13267 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
13268 | if (err) |
13269 | return err; | |
1be7f75d AS |
13270 | |
13271 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 13272 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
13273 | insn->src_reg); |
13274 | return -EACCES; | |
13275 | } | |
fb8d251e | 13276 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
13277 | } else { |
13278 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 13279 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
13280 | return -EINVAL; |
13281 | } | |
13282 | } | |
13283 | ||
13284 | /* check src2 operand */ | |
dc503a8a | 13285 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
13286 | if (err) |
13287 | return err; | |
13288 | ||
1a0dc1ac | 13289 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 13290 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 13291 | |
3f50f132 JF |
13292 | if (BPF_SRC(insn->code) == BPF_K) { |
13293 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
13294 | } else if (src_reg->type == SCALAR_VALUE && | |
13295 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
13296 | pred = is_branch_taken(dst_reg, | |
13297 | tnum_subreg(src_reg->var_off).value, | |
13298 | opcode, | |
13299 | is_jmp32); | |
13300 | } else if (src_reg->type == SCALAR_VALUE && | |
13301 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
13302 | pred = is_branch_taken(dst_reg, | |
13303 | src_reg->var_off.value, | |
13304 | opcode, | |
13305 | is_jmp32); | |
6d94e741 AS |
13306 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
13307 | reg_is_pkt_pointer_any(src_reg) && | |
13308 | !is_jmp32) { | |
13309 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
13310 | } |
13311 | ||
b5dc0163 | 13312 | if (pred >= 0) { |
cac616db JF |
13313 | /* If we get here with a dst_reg pointer type it is because |
13314 | * above is_branch_taken() special cased the 0 comparison. | |
13315 | */ | |
13316 | if (!__is_pointer_value(false, dst_reg)) | |
13317 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
13318 | if (BPF_SRC(insn->code) == BPF_X && !err && |
13319 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
13320 | err = mark_chain_precision(env, insn->src_reg); |
13321 | if (err) | |
13322 | return err; | |
13323 | } | |
9183671a | 13324 | |
fb8d251e | 13325 | if (pred == 1) { |
9183671a DB |
13326 | /* Only follow the goto, ignore fall-through. If needed, push |
13327 | * the fall-through branch for simulation under speculative | |
13328 | * execution. | |
13329 | */ | |
13330 | if (!env->bypass_spec_v1 && | |
13331 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
13332 | *insn_idx)) | |
13333 | return -EFAULT; | |
fb8d251e AS |
13334 | *insn_idx += insn->off; |
13335 | return 0; | |
13336 | } else if (pred == 0) { | |
9183671a DB |
13337 | /* Only follow the fall-through branch, since that's where the |
13338 | * program will go. If needed, push the goto branch for | |
13339 | * simulation under speculative execution. | |
fb8d251e | 13340 | */ |
9183671a DB |
13341 | if (!env->bypass_spec_v1 && |
13342 | !sanitize_speculative_path(env, insn, | |
13343 | *insn_idx + insn->off + 1, | |
13344 | *insn_idx)) | |
13345 | return -EFAULT; | |
fb8d251e | 13346 | return 0; |
17a52670 AS |
13347 | } |
13348 | ||
979d63d5 DB |
13349 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
13350 | false); | |
17a52670 AS |
13351 | if (!other_branch) |
13352 | return -EFAULT; | |
f4d7e40a | 13353 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 13354 | |
48461135 JB |
13355 | /* detect if we are comparing against a constant value so we can adjust |
13356 | * our min/max values for our dst register. | |
f1174f77 | 13357 | * this is only legit if both are scalars (or pointers to the same |
befae758 EZ |
13358 | * object, I suppose, see the PTR_MAYBE_NULL related if block below), |
13359 | * because otherwise the different base pointers mean the offsets aren't | |
f1174f77 | 13360 | * comparable. |
48461135 JB |
13361 | */ |
13362 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 13363 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 13364 | |
f1174f77 | 13365 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
13366 | src_reg->type == SCALAR_VALUE) { |
13367 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
13368 | (is_jmp32 && |
13369 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 13370 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 13371 | dst_reg, |
3f50f132 JF |
13372 | src_reg->var_off.value, |
13373 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
13374 | opcode, is_jmp32); |
13375 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
13376 | (is_jmp32 && |
13377 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 13378 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 13379 | src_reg, |
3f50f132 JF |
13380 | dst_reg->var_off.value, |
13381 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
13382 | opcode, is_jmp32); |
13383 | else if (!is_jmp32 && | |
13384 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 13385 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
13386 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
13387 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 13388 | src_reg, dst_reg, opcode); |
e688c3db AS |
13389 | if (src_reg->id && |
13390 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
13391 | find_equal_scalars(this_branch, src_reg); |
13392 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
13393 | } | |
13394 | ||
f1174f77 EC |
13395 | } |
13396 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 13397 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
13398 | dst_reg, insn->imm, (u32)insn->imm, |
13399 | opcode, is_jmp32); | |
48461135 JB |
13400 | } |
13401 | ||
e688c3db AS |
13402 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
13403 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
13404 | find_equal_scalars(this_branch, dst_reg); |
13405 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
13406 | } | |
13407 | ||
befae758 EZ |
13408 | /* if one pointer register is compared to another pointer |
13409 | * register check if PTR_MAYBE_NULL could be lifted. | |
13410 | * E.g. register A - maybe null | |
13411 | * register B - not null | |
13412 | * for JNE A, B, ... - A is not null in the false branch; | |
13413 | * for JEQ A, B, ... - A is not null in the true branch. | |
8374bfd5 HS |
13414 | * |
13415 | * Since PTR_TO_BTF_ID points to a kernel struct that does | |
13416 | * not need to be null checked by the BPF program, i.e., | |
13417 | * could be null even without PTR_MAYBE_NULL marking, so | |
13418 | * only propagate nullness when neither reg is that type. | |
befae758 EZ |
13419 | */ |
13420 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_X && | |
13421 | __is_pointer_value(false, src_reg) && __is_pointer_value(false, dst_reg) && | |
8374bfd5 HS |
13422 | type_may_be_null(src_reg->type) != type_may_be_null(dst_reg->type) && |
13423 | base_type(src_reg->type) != PTR_TO_BTF_ID && | |
13424 | base_type(dst_reg->type) != PTR_TO_BTF_ID) { | |
befae758 EZ |
13425 | eq_branch_regs = NULL; |
13426 | switch (opcode) { | |
13427 | case BPF_JEQ: | |
13428 | eq_branch_regs = other_branch_regs; | |
13429 | break; | |
13430 | case BPF_JNE: | |
13431 | eq_branch_regs = regs; | |
13432 | break; | |
13433 | default: | |
13434 | /* do nothing */ | |
13435 | break; | |
13436 | } | |
13437 | if (eq_branch_regs) { | |
13438 | if (type_may_be_null(src_reg->type)) | |
13439 | mark_ptr_not_null_reg(&eq_branch_regs[insn->src_reg]); | |
13440 | else | |
13441 | mark_ptr_not_null_reg(&eq_branch_regs[insn->dst_reg]); | |
13442 | } | |
13443 | } | |
13444 | ||
092ed096 JW |
13445 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
13446 | * NOTE: these optimizations below are related with pointer comparison | |
13447 | * which will never be JMP32. | |
13448 | */ | |
13449 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 13450 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
c25b2ae1 | 13451 | type_may_be_null(dst_reg->type)) { |
840b9615 | 13452 | /* Mark all identical registers in each branch as either |
57a09bf0 TG |
13453 | * safe or unknown depending R == 0 or R != 0 conditional. |
13454 | */ | |
840b9615 JS |
13455 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
13456 | opcode == BPF_JNE); | |
13457 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
13458 | opcode == BPF_JEQ); | |
5beca081 DB |
13459 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
13460 | this_branch, other_branch) && | |
13461 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
13462 | verbose(env, "R%d pointer comparison prohibited\n", |
13463 | insn->dst_reg); | |
1be7f75d | 13464 | return -EACCES; |
17a52670 | 13465 | } |
06ee7115 | 13466 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 13467 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
13468 | return 0; |
13469 | } | |
13470 | ||
17a52670 | 13471 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 13472 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 13473 | { |
d8eca5bb | 13474 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 13475 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 13476 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 13477 | struct bpf_map *map; |
17a52670 AS |
13478 | int err; |
13479 | ||
13480 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 13481 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
13482 | return -EINVAL; |
13483 | } | |
13484 | if (insn->off != 0) { | |
61bd5218 | 13485 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
13486 | return -EINVAL; |
13487 | } | |
13488 | ||
dc503a8a | 13489 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
13490 | if (err) |
13491 | return err; | |
13492 | ||
4976b718 | 13493 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 13494 | if (insn->src_reg == 0) { |
6b173873 JK |
13495 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
13496 | ||
4976b718 | 13497 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 13498 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 13499 | return 0; |
6b173873 | 13500 | } |
17a52670 | 13501 | |
d400a6cf DB |
13502 | /* All special src_reg cases are listed below. From this point onwards |
13503 | * we either succeed and assign a corresponding dst_reg->type after | |
13504 | * zeroing the offset, or fail and reject the program. | |
13505 | */ | |
13506 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 13507 | |
d400a6cf | 13508 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
4976b718 | 13509 | dst_reg->type = aux->btf_var.reg_type; |
34d3a78c | 13510 | switch (base_type(dst_reg->type)) { |
4976b718 HL |
13511 | case PTR_TO_MEM: |
13512 | dst_reg->mem_size = aux->btf_var.mem_size; | |
13513 | break; | |
13514 | case PTR_TO_BTF_ID: | |
22dc4a0f | 13515 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
13516 | dst_reg->btf_id = aux->btf_var.btf_id; |
13517 | break; | |
13518 | default: | |
13519 | verbose(env, "bpf verifier is misconfigured\n"); | |
13520 | return -EFAULT; | |
13521 | } | |
13522 | return 0; | |
13523 | } | |
13524 | ||
69c087ba YS |
13525 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
13526 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
13527 | u32 subprogno = find_subprog(env, |
13528 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
13529 | |
13530 | if (!aux->func_info) { | |
13531 | verbose(env, "missing btf func_info\n"); | |
13532 | return -EINVAL; | |
13533 | } | |
13534 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
13535 | verbose(env, "callback function not static\n"); | |
13536 | return -EINVAL; | |
13537 | } | |
13538 | ||
13539 | dst_reg->type = PTR_TO_FUNC; | |
13540 | dst_reg->subprogno = subprogno; | |
13541 | return 0; | |
13542 | } | |
13543 | ||
d8eca5bb | 13544 | map = env->used_maps[aux->map_index]; |
4976b718 | 13545 | dst_reg->map_ptr = map; |
d8eca5bb | 13546 | |
387544bf AS |
13547 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
13548 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
13549 | dst_reg->type = PTR_TO_MAP_VALUE; |
13550 | dst_reg->off = aux->map_off; | |
d0d78c1d KKD |
13551 | WARN_ON_ONCE(map->max_entries != 1); |
13552 | /* We want reg->id to be same (0) as map_value is not distinct */ | |
387544bf AS |
13553 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
13554 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 13555 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
13556 | } else { |
13557 | verbose(env, "bpf verifier is misconfigured\n"); | |
13558 | return -EINVAL; | |
13559 | } | |
17a52670 | 13560 | |
17a52670 AS |
13561 | return 0; |
13562 | } | |
13563 | ||
96be4325 DB |
13564 | static bool may_access_skb(enum bpf_prog_type type) |
13565 | { | |
13566 | switch (type) { | |
13567 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
13568 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 13569 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
13570 | return true; |
13571 | default: | |
13572 | return false; | |
13573 | } | |
13574 | } | |
13575 | ||
ddd872bc AS |
13576 | /* verify safety of LD_ABS|LD_IND instructions: |
13577 | * - they can only appear in the programs where ctx == skb | |
13578 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
13579 | * preserve R6-R9, and store return value into R0 | |
13580 | * | |
13581 | * Implicit input: | |
13582 | * ctx == skb == R6 == CTX | |
13583 | * | |
13584 | * Explicit input: | |
13585 | * SRC == any register | |
13586 | * IMM == 32-bit immediate | |
13587 | * | |
13588 | * Output: | |
13589 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
13590 | */ | |
58e2af8b | 13591 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 13592 | { |
638f5b90 | 13593 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 13594 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 13595 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
13596 | int i, err; |
13597 | ||
7e40781c | 13598 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 13599 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
13600 | return -EINVAL; |
13601 | } | |
13602 | ||
e0cea7ce DB |
13603 | if (!env->ops->gen_ld_abs) { |
13604 | verbose(env, "bpf verifier is misconfigured\n"); | |
13605 | return -EINVAL; | |
13606 | } | |
13607 | ||
ddd872bc | 13608 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 13609 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 13610 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 13611 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
13612 | return -EINVAL; |
13613 | } | |
13614 | ||
13615 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 13616 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
13617 | if (err) |
13618 | return err; | |
13619 | ||
fd978bf7 JS |
13620 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
13621 | * gen_ld_abs() may terminate the program at runtime, leading to | |
13622 | * reference leak. | |
13623 | */ | |
13624 | err = check_reference_leak(env); | |
13625 | if (err) { | |
13626 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
13627 | return err; | |
13628 | } | |
13629 | ||
d0d78c1d | 13630 | if (env->cur_state->active_lock.ptr) { |
d83525ca AS |
13631 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
13632 | return -EINVAL; | |
13633 | } | |
13634 | ||
9bb00b28 YS |
13635 | if (env->cur_state->active_rcu_lock) { |
13636 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_rcu_read_lock-ed region\n"); | |
13637 | return -EINVAL; | |
13638 | } | |
13639 | ||
6d4f151a | 13640 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
13641 | verbose(env, |
13642 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
13643 | return -EINVAL; |
13644 | } | |
13645 | ||
13646 | if (mode == BPF_IND) { | |
13647 | /* check explicit source operand */ | |
dc503a8a | 13648 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
13649 | if (err) |
13650 | return err; | |
13651 | } | |
13652 | ||
be80a1d3 | 13653 | err = check_ptr_off_reg(env, ®s[ctx_reg], ctx_reg); |
6d4f151a DB |
13654 | if (err < 0) |
13655 | return err; | |
13656 | ||
ddd872bc | 13657 | /* reset caller saved regs to unreadable */ |
dc503a8a | 13658 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 13659 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
13660 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
13661 | } | |
ddd872bc AS |
13662 | |
13663 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
13664 | * the value fetched from the packet. |
13665 | * Already marked as written above. | |
ddd872bc | 13666 | */ |
61bd5218 | 13667 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
13668 | /* ld_abs load up to 32-bit skb data. */ |
13669 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
13670 | return 0; |
13671 | } | |
13672 | ||
390ee7e2 AS |
13673 | static int check_return_code(struct bpf_verifier_env *env) |
13674 | { | |
5cf1e914 | 13675 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 13676 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
13677 | struct bpf_reg_state *reg; |
13678 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 13679 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 13680 | int err; |
bfc6bb74 AS |
13681 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
13682 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 13683 | |
9e4e01df | 13684 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
d1a6edec SF |
13685 | if (!is_subprog) { |
13686 | switch (prog_type) { | |
13687 | case BPF_PROG_TYPE_LSM: | |
13688 | if (prog->expected_attach_type == BPF_LSM_CGROUP) | |
13689 | /* See below, can be 0 or 0-1 depending on hook. */ | |
13690 | break; | |
13691 | fallthrough; | |
13692 | case BPF_PROG_TYPE_STRUCT_OPS: | |
13693 | if (!prog->aux->attach_func_proto->type) | |
13694 | return 0; | |
13695 | break; | |
13696 | default: | |
13697 | break; | |
13698 | } | |
13699 | } | |
27ae7997 | 13700 | |
8fb33b60 | 13701 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
13702 | * to return the value from eBPF program. |
13703 | * Make sure that it's readable at this time | |
13704 | * of bpf_exit, which means that program wrote | |
13705 | * something into it earlier | |
13706 | */ | |
13707 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
13708 | if (err) | |
13709 | return err; | |
13710 | ||
13711 | if (is_pointer_value(env, BPF_REG_0)) { | |
13712 | verbose(env, "R0 leaks addr as return value\n"); | |
13713 | return -EACCES; | |
13714 | } | |
390ee7e2 | 13715 | |
f782e2c3 | 13716 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
13717 | |
13718 | if (frame->in_async_callback_fn) { | |
13719 | /* enforce return zero from async callbacks like timer */ | |
13720 | if (reg->type != SCALAR_VALUE) { | |
13721 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
c25b2ae1 | 13722 | reg_type_str(env, reg->type)); |
bfc6bb74 AS |
13723 | return -EINVAL; |
13724 | } | |
13725 | ||
13726 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
13727 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
13728 | return -EINVAL; | |
13729 | } | |
13730 | return 0; | |
13731 | } | |
13732 | ||
f782e2c3 DB |
13733 | if (is_subprog) { |
13734 | if (reg->type != SCALAR_VALUE) { | |
13735 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
c25b2ae1 | 13736 | reg_type_str(env, reg->type)); |
f782e2c3 DB |
13737 | return -EINVAL; |
13738 | } | |
13739 | return 0; | |
13740 | } | |
13741 | ||
7e40781c | 13742 | switch (prog_type) { |
983695fa DB |
13743 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
13744 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
13745 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
13746 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
13747 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
13748 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
13749 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 13750 | range = tnum_range(1, 1); |
77241217 SF |
13751 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
13752 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
13753 | range = tnum_range(0, 3); | |
ed4ed404 | 13754 | break; |
390ee7e2 | 13755 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 13756 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
13757 | range = tnum_range(0, 3); | |
13758 | enforce_attach_type_range = tnum_range(2, 3); | |
13759 | } | |
ed4ed404 | 13760 | break; |
390ee7e2 AS |
13761 | case BPF_PROG_TYPE_CGROUP_SOCK: |
13762 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 13763 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 13764 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 13765 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 13766 | break; |
15ab09bd AS |
13767 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
13768 | if (!env->prog->aux->attach_btf_id) | |
13769 | return 0; | |
13770 | range = tnum_const(0); | |
13771 | break; | |
15d83c4d | 13772 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
13773 | switch (env->prog->expected_attach_type) { |
13774 | case BPF_TRACE_FENTRY: | |
13775 | case BPF_TRACE_FEXIT: | |
13776 | range = tnum_const(0); | |
13777 | break; | |
13778 | case BPF_TRACE_RAW_TP: | |
13779 | case BPF_MODIFY_RETURN: | |
15d83c4d | 13780 | return 0; |
2ec0616e DB |
13781 | case BPF_TRACE_ITER: |
13782 | break; | |
e92888c7 YS |
13783 | default: |
13784 | return -ENOTSUPP; | |
13785 | } | |
15d83c4d | 13786 | break; |
e9ddbb77 JS |
13787 | case BPF_PROG_TYPE_SK_LOOKUP: |
13788 | range = tnum_range(SK_DROP, SK_PASS); | |
13789 | break; | |
69fd337a SF |
13790 | |
13791 | case BPF_PROG_TYPE_LSM: | |
13792 | if (env->prog->expected_attach_type != BPF_LSM_CGROUP) { | |
13793 | /* Regular BPF_PROG_TYPE_LSM programs can return | |
13794 | * any value. | |
13795 | */ | |
13796 | return 0; | |
13797 | } | |
13798 | if (!env->prog->aux->attach_func_proto->type) { | |
13799 | /* Make sure programs that attach to void | |
13800 | * hooks don't try to modify return value. | |
13801 | */ | |
13802 | range = tnum_range(1, 1); | |
13803 | } | |
13804 | break; | |
13805 | ||
e92888c7 YS |
13806 | case BPF_PROG_TYPE_EXT: |
13807 | /* freplace program can return anything as its return value | |
13808 | * depends on the to-be-replaced kernel func or bpf program. | |
13809 | */ | |
390ee7e2 AS |
13810 | default: |
13811 | return 0; | |
13812 | } | |
13813 | ||
390ee7e2 | 13814 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 13815 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
c25b2ae1 | 13816 | reg_type_str(env, reg->type)); |
390ee7e2 AS |
13817 | return -EINVAL; |
13818 | } | |
13819 | ||
13820 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 13821 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
69fd337a | 13822 | if (prog->expected_attach_type == BPF_LSM_CGROUP && |
d1a6edec | 13823 | prog_type == BPF_PROG_TYPE_LSM && |
69fd337a SF |
13824 | !prog->aux->attach_func_proto->type) |
13825 | verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
390ee7e2 AS |
13826 | return -EINVAL; |
13827 | } | |
5cf1e914 | 13828 | |
13829 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
13830 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
13831 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
13832 | return 0; |
13833 | } | |
13834 | ||
475fb78f AS |
13835 | /* non-recursive DFS pseudo code |
13836 | * 1 procedure DFS-iterative(G,v): | |
13837 | * 2 label v as discovered | |
13838 | * 3 let S be a stack | |
13839 | * 4 S.push(v) | |
13840 | * 5 while S is not empty | |
b6d20799 | 13841 | * 6 t <- S.peek() |
475fb78f AS |
13842 | * 7 if t is what we're looking for: |
13843 | * 8 return t | |
13844 | * 9 for all edges e in G.adjacentEdges(t) do | |
13845 | * 10 if edge e is already labelled | |
13846 | * 11 continue with the next edge | |
13847 | * 12 w <- G.adjacentVertex(t,e) | |
13848 | * 13 if vertex w is not discovered and not explored | |
13849 | * 14 label e as tree-edge | |
13850 | * 15 label w as discovered | |
13851 | * 16 S.push(w) | |
13852 | * 17 continue at 5 | |
13853 | * 18 else if vertex w is discovered | |
13854 | * 19 label e as back-edge | |
13855 | * 20 else | |
13856 | * 21 // vertex w is explored | |
13857 | * 22 label e as forward- or cross-edge | |
13858 | * 23 label t as explored | |
13859 | * 24 S.pop() | |
13860 | * | |
13861 | * convention: | |
13862 | * 0x10 - discovered | |
13863 | * 0x11 - discovered and fall-through edge labelled | |
13864 | * 0x12 - discovered and fall-through and branch edges labelled | |
13865 | * 0x20 - explored | |
13866 | */ | |
13867 | ||
13868 | enum { | |
13869 | DISCOVERED = 0x10, | |
13870 | EXPLORED = 0x20, | |
13871 | FALLTHROUGH = 1, | |
13872 | BRANCH = 2, | |
13873 | }; | |
13874 | ||
dc2a4ebc AS |
13875 | static u32 state_htab_size(struct bpf_verifier_env *env) |
13876 | { | |
13877 | return env->prog->len; | |
13878 | } | |
13879 | ||
5d839021 AS |
13880 | static struct bpf_verifier_state_list **explored_state( |
13881 | struct bpf_verifier_env *env, | |
13882 | int idx) | |
13883 | { | |
dc2a4ebc AS |
13884 | struct bpf_verifier_state *cur = env->cur_state; |
13885 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
13886 | ||
13887 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
13888 | } |
13889 | ||
bffdeaa8 | 13890 | static void mark_prune_point(struct bpf_verifier_env *env, int idx) |
5d839021 | 13891 | { |
a8f500af | 13892 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 13893 | } |
f1bca824 | 13894 | |
bffdeaa8 AN |
13895 | static bool is_prune_point(struct bpf_verifier_env *env, int insn_idx) |
13896 | { | |
13897 | return env->insn_aux_data[insn_idx].prune_point; | |
13898 | } | |
13899 | ||
4b5ce570 AN |
13900 | static void mark_force_checkpoint(struct bpf_verifier_env *env, int idx) |
13901 | { | |
13902 | env->insn_aux_data[idx].force_checkpoint = true; | |
13903 | } | |
13904 | ||
13905 | static bool is_force_checkpoint(struct bpf_verifier_env *env, int insn_idx) | |
13906 | { | |
13907 | return env->insn_aux_data[insn_idx].force_checkpoint; | |
13908 | } | |
13909 | ||
13910 | ||
59e2e27d WAF |
13911 | enum { |
13912 | DONE_EXPLORING = 0, | |
13913 | KEEP_EXPLORING = 1, | |
13914 | }; | |
13915 | ||
475fb78f AS |
13916 | /* t, w, e - match pseudo-code above: |
13917 | * t - index of current instruction | |
13918 | * w - next instruction | |
13919 | * e - edge | |
13920 | */ | |
2589726d AS |
13921 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
13922 | bool loop_ok) | |
475fb78f | 13923 | { |
7df737e9 AS |
13924 | int *insn_stack = env->cfg.insn_stack; |
13925 | int *insn_state = env->cfg.insn_state; | |
13926 | ||
475fb78f | 13927 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 13928 | return DONE_EXPLORING; |
475fb78f AS |
13929 | |
13930 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 13931 | return DONE_EXPLORING; |
475fb78f AS |
13932 | |
13933 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 13934 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 13935 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
13936 | return -EINVAL; |
13937 | } | |
13938 | ||
bffdeaa8 | 13939 | if (e == BRANCH) { |
f1bca824 | 13940 | /* mark branch target for state pruning */ |
bffdeaa8 AN |
13941 | mark_prune_point(env, w); |
13942 | mark_jmp_point(env, w); | |
13943 | } | |
f1bca824 | 13944 | |
475fb78f AS |
13945 | if (insn_state[w] == 0) { |
13946 | /* tree-edge */ | |
13947 | insn_state[t] = DISCOVERED | e; | |
13948 | insn_state[w] = DISCOVERED; | |
7df737e9 | 13949 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 13950 | return -E2BIG; |
7df737e9 | 13951 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 13952 | return KEEP_EXPLORING; |
475fb78f | 13953 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 13954 | if (loop_ok && env->bpf_capable) |
59e2e27d | 13955 | return DONE_EXPLORING; |
d9762e84 MKL |
13956 | verbose_linfo(env, t, "%d: ", t); |
13957 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 13958 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
13959 | return -EINVAL; |
13960 | } else if (insn_state[w] == EXPLORED) { | |
13961 | /* forward- or cross-edge */ | |
13962 | insn_state[t] = DISCOVERED | e; | |
13963 | } else { | |
61bd5218 | 13964 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
13965 | return -EFAULT; |
13966 | } | |
59e2e27d WAF |
13967 | return DONE_EXPLORING; |
13968 | } | |
13969 | ||
dcb2288b | 13970 | static int visit_func_call_insn(int t, struct bpf_insn *insns, |
efdb22de YS |
13971 | struct bpf_verifier_env *env, |
13972 | bool visit_callee) | |
13973 | { | |
13974 | int ret; | |
13975 | ||
13976 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
13977 | if (ret) | |
13978 | return ret; | |
13979 | ||
618945fb AN |
13980 | mark_prune_point(env, t + 1); |
13981 | /* when we exit from subprog, we need to record non-linear history */ | |
13982 | mark_jmp_point(env, t + 1); | |
13983 | ||
efdb22de | 13984 | if (visit_callee) { |
bffdeaa8 | 13985 | mark_prune_point(env, t); |
86fc6ee6 AS |
13986 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
13987 | /* It's ok to allow recursion from CFG point of | |
13988 | * view. __check_func_call() will do the actual | |
13989 | * check. | |
13990 | */ | |
13991 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
13992 | } |
13993 | return ret; | |
13994 | } | |
13995 | ||
59e2e27d WAF |
13996 | /* Visits the instruction at index t and returns one of the following: |
13997 | * < 0 - an error occurred | |
13998 | * DONE_EXPLORING - the instruction was fully explored | |
13999 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
14000 | */ | |
dcb2288b | 14001 | static int visit_insn(int t, struct bpf_verifier_env *env) |
59e2e27d | 14002 | { |
653ae3a8 | 14003 | struct bpf_insn *insns = env->prog->insnsi, *insn = &insns[t]; |
59e2e27d WAF |
14004 | int ret; |
14005 | ||
653ae3a8 | 14006 | if (bpf_pseudo_func(insn)) |
dcb2288b | 14007 | return visit_func_call_insn(t, insns, env, true); |
69c087ba | 14008 | |
59e2e27d | 14009 | /* All non-branch instructions have a single fall-through edge. */ |
653ae3a8 AN |
14010 | if (BPF_CLASS(insn->code) != BPF_JMP && |
14011 | BPF_CLASS(insn->code) != BPF_JMP32) | |
59e2e27d WAF |
14012 | return push_insn(t, t + 1, FALLTHROUGH, env, false); |
14013 | ||
653ae3a8 | 14014 | switch (BPF_OP(insn->code)) { |
59e2e27d WAF |
14015 | case BPF_EXIT: |
14016 | return DONE_EXPLORING; | |
14017 | ||
14018 | case BPF_CALL: | |
c1ee85a9 | 14019 | if (insn->src_reg == 0 && insn->imm == BPF_FUNC_timer_set_callback) |
618945fb AN |
14020 | /* Mark this call insn as a prune point to trigger |
14021 | * is_state_visited() check before call itself is | |
14022 | * processed by __check_func_call(). Otherwise new | |
14023 | * async state will be pushed for further exploration. | |
bfc6bb74 | 14024 | */ |
bffdeaa8 | 14025 | mark_prune_point(env, t); |
06accc87 AN |
14026 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
14027 | struct bpf_kfunc_call_arg_meta meta; | |
14028 | ||
14029 | ret = fetch_kfunc_meta(env, insn, &meta, NULL); | |
4b5ce570 | 14030 | if (ret == 0 && is_iter_next_kfunc(&meta)) { |
06accc87 | 14031 | mark_prune_point(env, t); |
4b5ce570 AN |
14032 | /* Checking and saving state checkpoints at iter_next() call |
14033 | * is crucial for fast convergence of open-coded iterator loop | |
14034 | * logic, so we need to force it. If we don't do that, | |
14035 | * is_state_visited() might skip saving a checkpoint, causing | |
14036 | * unnecessarily long sequence of not checkpointed | |
14037 | * instructions and jumps, leading to exhaustion of jump | |
14038 | * history buffer, and potentially other undesired outcomes. | |
14039 | * It is expected that with correct open-coded iterators | |
14040 | * convergence will happen quickly, so we don't run a risk of | |
14041 | * exhausting memory. | |
14042 | */ | |
14043 | mark_force_checkpoint(env, t); | |
14044 | } | |
06accc87 | 14045 | } |
653ae3a8 | 14046 | return visit_func_call_insn(t, insns, env, insn->src_reg == BPF_PSEUDO_CALL); |
59e2e27d WAF |
14047 | |
14048 | case BPF_JA: | |
653ae3a8 | 14049 | if (BPF_SRC(insn->code) != BPF_K) |
59e2e27d WAF |
14050 | return -EINVAL; |
14051 | ||
14052 | /* unconditional jump with single edge */ | |
653ae3a8 | 14053 | ret = push_insn(t, t + insn->off + 1, FALLTHROUGH, env, |
59e2e27d WAF |
14054 | true); |
14055 | if (ret) | |
14056 | return ret; | |
14057 | ||
653ae3a8 AN |
14058 | mark_prune_point(env, t + insn->off + 1); |
14059 | mark_jmp_point(env, t + insn->off + 1); | |
59e2e27d WAF |
14060 | |
14061 | return ret; | |
14062 | ||
14063 | default: | |
14064 | /* conditional jump with two edges */ | |
bffdeaa8 | 14065 | mark_prune_point(env, t); |
618945fb | 14066 | |
59e2e27d WAF |
14067 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
14068 | if (ret) | |
14069 | return ret; | |
14070 | ||
653ae3a8 | 14071 | return push_insn(t, t + insn->off + 1, BRANCH, env, true); |
59e2e27d | 14072 | } |
475fb78f AS |
14073 | } |
14074 | ||
14075 | /* non-recursive depth-first-search to detect loops in BPF program | |
14076 | * loop == back-edge in directed graph | |
14077 | */ | |
58e2af8b | 14078 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 14079 | { |
475fb78f | 14080 | int insn_cnt = env->prog->len; |
7df737e9 | 14081 | int *insn_stack, *insn_state; |
475fb78f | 14082 | int ret = 0; |
59e2e27d | 14083 | int i; |
475fb78f | 14084 | |
7df737e9 | 14085 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
14086 | if (!insn_state) |
14087 | return -ENOMEM; | |
14088 | ||
7df737e9 | 14089 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 14090 | if (!insn_stack) { |
71dde681 | 14091 | kvfree(insn_state); |
475fb78f AS |
14092 | return -ENOMEM; |
14093 | } | |
14094 | ||
14095 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
14096 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 14097 | env->cfg.cur_stack = 1; |
475fb78f | 14098 | |
59e2e27d WAF |
14099 | while (env->cfg.cur_stack > 0) { |
14100 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 14101 | |
dcb2288b | 14102 | ret = visit_insn(t, env); |
59e2e27d WAF |
14103 | switch (ret) { |
14104 | case DONE_EXPLORING: | |
14105 | insn_state[t] = EXPLORED; | |
14106 | env->cfg.cur_stack--; | |
14107 | break; | |
14108 | case KEEP_EXPLORING: | |
14109 | break; | |
14110 | default: | |
14111 | if (ret > 0) { | |
14112 | verbose(env, "visit_insn internal bug\n"); | |
14113 | ret = -EFAULT; | |
475fb78f | 14114 | } |
475fb78f | 14115 | goto err_free; |
59e2e27d | 14116 | } |
475fb78f AS |
14117 | } |
14118 | ||
59e2e27d | 14119 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 14120 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
14121 | ret = -EFAULT; |
14122 | goto err_free; | |
14123 | } | |
475fb78f | 14124 | |
475fb78f AS |
14125 | for (i = 0; i < insn_cnt; i++) { |
14126 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 14127 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
14128 | ret = -EINVAL; |
14129 | goto err_free; | |
14130 | } | |
14131 | } | |
14132 | ret = 0; /* cfg looks good */ | |
14133 | ||
14134 | err_free: | |
71dde681 AS |
14135 | kvfree(insn_state); |
14136 | kvfree(insn_stack); | |
7df737e9 | 14137 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
14138 | return ret; |
14139 | } | |
14140 | ||
09b28d76 AS |
14141 | static int check_abnormal_return(struct bpf_verifier_env *env) |
14142 | { | |
14143 | int i; | |
14144 | ||
14145 | for (i = 1; i < env->subprog_cnt; i++) { | |
14146 | if (env->subprog_info[i].has_ld_abs) { | |
14147 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
14148 | return -EINVAL; | |
14149 | } | |
14150 | if (env->subprog_info[i].has_tail_call) { | |
14151 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
14152 | return -EINVAL; | |
14153 | } | |
14154 | } | |
14155 | return 0; | |
14156 | } | |
14157 | ||
838e9690 YS |
14158 | /* The minimum supported BTF func info size */ |
14159 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
14160 | #define MAX_FUNCINFO_REC_SIZE 252 | |
14161 | ||
c454a46b MKL |
14162 | static int check_btf_func(struct bpf_verifier_env *env, |
14163 | const union bpf_attr *attr, | |
af2ac3e1 | 14164 | bpfptr_t uattr) |
838e9690 | 14165 | { |
09b28d76 | 14166 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 14167 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 14168 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 14169 | struct bpf_func_info *krecord; |
8c1b6e69 | 14170 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
14171 | struct bpf_prog *prog; |
14172 | const struct btf *btf; | |
af2ac3e1 | 14173 | bpfptr_t urecord; |
d0b2818e | 14174 | u32 prev_offset = 0; |
09b28d76 | 14175 | bool scalar_return; |
e7ed83d6 | 14176 | int ret = -ENOMEM; |
838e9690 YS |
14177 | |
14178 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
14179 | if (!nfuncs) { |
14180 | if (check_abnormal_return(env)) | |
14181 | return -EINVAL; | |
838e9690 | 14182 | return 0; |
09b28d76 | 14183 | } |
838e9690 YS |
14184 | |
14185 | if (nfuncs != env->subprog_cnt) { | |
14186 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
14187 | return -EINVAL; | |
14188 | } | |
14189 | ||
14190 | urec_size = attr->func_info_rec_size; | |
14191 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
14192 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
14193 | urec_size % sizeof(u32)) { | |
14194 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
14195 | return -EINVAL; | |
14196 | } | |
14197 | ||
c454a46b MKL |
14198 | prog = env->prog; |
14199 | btf = prog->aux->btf; | |
838e9690 | 14200 | |
af2ac3e1 | 14201 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
14202 | min_size = min_t(u32, krec_size, urec_size); |
14203 | ||
ba64e7d8 | 14204 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
14205 | if (!krecord) |
14206 | return -ENOMEM; | |
8c1b6e69 AS |
14207 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
14208 | if (!info_aux) | |
14209 | goto err_free; | |
ba64e7d8 | 14210 | |
838e9690 YS |
14211 | for (i = 0; i < nfuncs; i++) { |
14212 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
14213 | if (ret) { | |
14214 | if (ret == -E2BIG) { | |
14215 | verbose(env, "nonzero tailing record in func info"); | |
14216 | /* set the size kernel expects so loader can zero | |
14217 | * out the rest of the record. | |
14218 | */ | |
af2ac3e1 AS |
14219 | if (copy_to_bpfptr_offset(uattr, |
14220 | offsetof(union bpf_attr, func_info_rec_size), | |
14221 | &min_size, sizeof(min_size))) | |
838e9690 YS |
14222 | ret = -EFAULT; |
14223 | } | |
c454a46b | 14224 | goto err_free; |
838e9690 YS |
14225 | } |
14226 | ||
af2ac3e1 | 14227 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 14228 | ret = -EFAULT; |
c454a46b | 14229 | goto err_free; |
838e9690 YS |
14230 | } |
14231 | ||
d30d42e0 | 14232 | /* check insn_off */ |
09b28d76 | 14233 | ret = -EINVAL; |
838e9690 | 14234 | if (i == 0) { |
d30d42e0 | 14235 | if (krecord[i].insn_off) { |
838e9690 | 14236 | verbose(env, |
d30d42e0 MKL |
14237 | "nonzero insn_off %u for the first func info record", |
14238 | krecord[i].insn_off); | |
c454a46b | 14239 | goto err_free; |
838e9690 | 14240 | } |
d30d42e0 | 14241 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
14242 | verbose(env, |
14243 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 14244 | krecord[i].insn_off, prev_offset); |
c454a46b | 14245 | goto err_free; |
838e9690 YS |
14246 | } |
14247 | ||
d30d42e0 | 14248 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 14249 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 14250 | goto err_free; |
838e9690 YS |
14251 | } |
14252 | ||
14253 | /* check type_id */ | |
ba64e7d8 | 14254 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 14255 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 14256 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 14257 | krecord[i].type_id); |
c454a46b | 14258 | goto err_free; |
838e9690 | 14259 | } |
51c39bb1 | 14260 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
14261 | |
14262 | func_proto = btf_type_by_id(btf, type->type); | |
14263 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
14264 | /* btf_func_check() already verified it during BTF load */ | |
14265 | goto err_free; | |
14266 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
14267 | scalar_return = | |
6089fb32 | 14268 | btf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type); |
09b28d76 AS |
14269 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
14270 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
14271 | goto err_free; | |
14272 | } | |
14273 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
14274 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
14275 | goto err_free; | |
14276 | } | |
14277 | ||
d30d42e0 | 14278 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 14279 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
14280 | } |
14281 | ||
ba64e7d8 YS |
14282 | prog->aux->func_info = krecord; |
14283 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 14284 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
14285 | return 0; |
14286 | ||
c454a46b | 14287 | err_free: |
ba64e7d8 | 14288 | kvfree(krecord); |
8c1b6e69 | 14289 | kfree(info_aux); |
838e9690 YS |
14290 | return ret; |
14291 | } | |
14292 | ||
ba64e7d8 YS |
14293 | static void adjust_btf_func(struct bpf_verifier_env *env) |
14294 | { | |
8c1b6e69 | 14295 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
14296 | int i; |
14297 | ||
8c1b6e69 | 14298 | if (!aux->func_info) |
ba64e7d8 YS |
14299 | return; |
14300 | ||
14301 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 14302 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
14303 | } |
14304 | ||
1b773d00 | 14305 | #define MIN_BPF_LINEINFO_SIZE offsetofend(struct bpf_line_info, line_col) |
c454a46b MKL |
14306 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
14307 | ||
14308 | static int check_btf_line(struct bpf_verifier_env *env, | |
14309 | const union bpf_attr *attr, | |
af2ac3e1 | 14310 | bpfptr_t uattr) |
c454a46b MKL |
14311 | { |
14312 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
14313 | struct bpf_subprog_info *sub; | |
14314 | struct bpf_line_info *linfo; | |
14315 | struct bpf_prog *prog; | |
14316 | const struct btf *btf; | |
af2ac3e1 | 14317 | bpfptr_t ulinfo; |
c454a46b MKL |
14318 | int err; |
14319 | ||
14320 | nr_linfo = attr->line_info_cnt; | |
14321 | if (!nr_linfo) | |
14322 | return 0; | |
0e6491b5 BC |
14323 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
14324 | return -EINVAL; | |
c454a46b MKL |
14325 | |
14326 | rec_size = attr->line_info_rec_size; | |
14327 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
14328 | rec_size > MAX_LINEINFO_REC_SIZE || | |
14329 | rec_size & (sizeof(u32) - 1)) | |
14330 | return -EINVAL; | |
14331 | ||
14332 | /* Need to zero it in case the userspace may | |
14333 | * pass in a smaller bpf_line_info object. | |
14334 | */ | |
14335 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
14336 | GFP_KERNEL | __GFP_NOWARN); | |
14337 | if (!linfo) | |
14338 | return -ENOMEM; | |
14339 | ||
14340 | prog = env->prog; | |
14341 | btf = prog->aux->btf; | |
14342 | ||
14343 | s = 0; | |
14344 | sub = env->subprog_info; | |
af2ac3e1 | 14345 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
14346 | expected_size = sizeof(struct bpf_line_info); |
14347 | ncopy = min_t(u32, expected_size, rec_size); | |
14348 | for (i = 0; i < nr_linfo; i++) { | |
14349 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
14350 | if (err) { | |
14351 | if (err == -E2BIG) { | |
14352 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
14353 | if (copy_to_bpfptr_offset(uattr, |
14354 | offsetof(union bpf_attr, line_info_rec_size), | |
14355 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
14356 | err = -EFAULT; |
14357 | } | |
14358 | goto err_free; | |
14359 | } | |
14360 | ||
af2ac3e1 | 14361 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
14362 | err = -EFAULT; |
14363 | goto err_free; | |
14364 | } | |
14365 | ||
14366 | /* | |
14367 | * Check insn_off to ensure | |
14368 | * 1) strictly increasing AND | |
14369 | * 2) bounded by prog->len | |
14370 | * | |
14371 | * The linfo[0].insn_off == 0 check logically falls into | |
14372 | * the later "missing bpf_line_info for func..." case | |
14373 | * because the first linfo[0].insn_off must be the | |
14374 | * first sub also and the first sub must have | |
14375 | * subprog_info[0].start == 0. | |
14376 | */ | |
14377 | if ((i && linfo[i].insn_off <= prev_offset) || | |
14378 | linfo[i].insn_off >= prog->len) { | |
14379 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
14380 | i, linfo[i].insn_off, prev_offset, | |
14381 | prog->len); | |
14382 | err = -EINVAL; | |
14383 | goto err_free; | |
14384 | } | |
14385 | ||
fdbaa0be MKL |
14386 | if (!prog->insnsi[linfo[i].insn_off].code) { |
14387 | verbose(env, | |
14388 | "Invalid insn code at line_info[%u].insn_off\n", | |
14389 | i); | |
14390 | err = -EINVAL; | |
14391 | goto err_free; | |
14392 | } | |
14393 | ||
23127b33 MKL |
14394 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
14395 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
14396 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
14397 | err = -EINVAL; | |
14398 | goto err_free; | |
14399 | } | |
14400 | ||
14401 | if (s != env->subprog_cnt) { | |
14402 | if (linfo[i].insn_off == sub[s].start) { | |
14403 | sub[s].linfo_idx = i; | |
14404 | s++; | |
14405 | } else if (sub[s].start < linfo[i].insn_off) { | |
14406 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
14407 | err = -EINVAL; | |
14408 | goto err_free; | |
14409 | } | |
14410 | } | |
14411 | ||
14412 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 14413 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
14414 | } |
14415 | ||
14416 | if (s != env->subprog_cnt) { | |
14417 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
14418 | env->subprog_cnt - s, s); | |
14419 | err = -EINVAL; | |
14420 | goto err_free; | |
14421 | } | |
14422 | ||
14423 | prog->aux->linfo = linfo; | |
14424 | prog->aux->nr_linfo = nr_linfo; | |
14425 | ||
14426 | return 0; | |
14427 | ||
14428 | err_free: | |
14429 | kvfree(linfo); | |
14430 | return err; | |
14431 | } | |
14432 | ||
fbd94c7a AS |
14433 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
14434 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
14435 | ||
14436 | static int check_core_relo(struct bpf_verifier_env *env, | |
14437 | const union bpf_attr *attr, | |
14438 | bpfptr_t uattr) | |
14439 | { | |
14440 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
14441 | struct bpf_core_relo core_relo = {}; | |
14442 | struct bpf_prog *prog = env->prog; | |
14443 | const struct btf *btf = prog->aux->btf; | |
14444 | struct bpf_core_ctx ctx = { | |
14445 | .log = &env->log, | |
14446 | .btf = btf, | |
14447 | }; | |
14448 | bpfptr_t u_core_relo; | |
14449 | int err; | |
14450 | ||
14451 | nr_core_relo = attr->core_relo_cnt; | |
14452 | if (!nr_core_relo) | |
14453 | return 0; | |
14454 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
14455 | return -EINVAL; | |
14456 | ||
14457 | rec_size = attr->core_relo_rec_size; | |
14458 | if (rec_size < MIN_CORE_RELO_SIZE || | |
14459 | rec_size > MAX_CORE_RELO_SIZE || | |
14460 | rec_size % sizeof(u32)) | |
14461 | return -EINVAL; | |
14462 | ||
14463 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
14464 | expected_size = sizeof(struct bpf_core_relo); | |
14465 | ncopy = min_t(u32, expected_size, rec_size); | |
14466 | ||
14467 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
14468 | * relocation record one at a time. | |
14469 | */ | |
14470 | for (i = 0; i < nr_core_relo; i++) { | |
14471 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
14472 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
14473 | if (err) { | |
14474 | if (err == -E2BIG) { | |
14475 | verbose(env, "nonzero tailing record in core_relo"); | |
14476 | if (copy_to_bpfptr_offset(uattr, | |
14477 | offsetof(union bpf_attr, core_relo_rec_size), | |
14478 | &expected_size, sizeof(expected_size))) | |
14479 | err = -EFAULT; | |
14480 | } | |
14481 | break; | |
14482 | } | |
14483 | ||
14484 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
14485 | err = -EFAULT; | |
14486 | break; | |
14487 | } | |
14488 | ||
14489 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
14490 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
14491 | i, core_relo.insn_off, prog->len); | |
14492 | err = -EINVAL; | |
14493 | break; | |
14494 | } | |
14495 | ||
14496 | err = bpf_core_apply(&ctx, &core_relo, i, | |
14497 | &prog->insnsi[core_relo.insn_off / 8]); | |
14498 | if (err) | |
14499 | break; | |
14500 | bpfptr_add(&u_core_relo, rec_size); | |
14501 | } | |
14502 | return err; | |
14503 | } | |
14504 | ||
c454a46b MKL |
14505 | static int check_btf_info(struct bpf_verifier_env *env, |
14506 | const union bpf_attr *attr, | |
af2ac3e1 | 14507 | bpfptr_t uattr) |
c454a46b MKL |
14508 | { |
14509 | struct btf *btf; | |
14510 | int err; | |
14511 | ||
09b28d76 AS |
14512 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
14513 | if (check_abnormal_return(env)) | |
14514 | return -EINVAL; | |
c454a46b | 14515 | return 0; |
09b28d76 | 14516 | } |
c454a46b MKL |
14517 | |
14518 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
14519 | if (IS_ERR(btf)) | |
14520 | return PTR_ERR(btf); | |
350a5c4d AS |
14521 | if (btf_is_kernel(btf)) { |
14522 | btf_put(btf); | |
14523 | return -EACCES; | |
14524 | } | |
c454a46b MKL |
14525 | env->prog->aux->btf = btf; |
14526 | ||
14527 | err = check_btf_func(env, attr, uattr); | |
14528 | if (err) | |
14529 | return err; | |
14530 | ||
14531 | err = check_btf_line(env, attr, uattr); | |
14532 | if (err) | |
14533 | return err; | |
14534 | ||
fbd94c7a AS |
14535 | err = check_core_relo(env, attr, uattr); |
14536 | if (err) | |
14537 | return err; | |
14538 | ||
c454a46b | 14539 | return 0; |
ba64e7d8 YS |
14540 | } |
14541 | ||
f1174f77 EC |
14542 | /* check %cur's range satisfies %old's */ |
14543 | static bool range_within(struct bpf_reg_state *old, | |
14544 | struct bpf_reg_state *cur) | |
14545 | { | |
b03c9f9f EC |
14546 | return old->umin_value <= cur->umin_value && |
14547 | old->umax_value >= cur->umax_value && | |
14548 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
14549 | old->smax_value >= cur->smax_value && |
14550 | old->u32_min_value <= cur->u32_min_value && | |
14551 | old->u32_max_value >= cur->u32_max_value && | |
14552 | old->s32_min_value <= cur->s32_min_value && | |
14553 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
14554 | } |
14555 | ||
f1174f77 EC |
14556 | /* If in the old state two registers had the same id, then they need to have |
14557 | * the same id in the new state as well. But that id could be different from | |
14558 | * the old state, so we need to track the mapping from old to new ids. | |
14559 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
14560 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
14561 | * regs with a different old id could still have new id 9, we don't care about | |
14562 | * that. | |
14563 | * So we look through our idmap to see if this old id has been seen before. If | |
14564 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 14565 | */ |
c9e73e3d | 14566 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 14567 | { |
f1174f77 | 14568 | unsigned int i; |
969bf05e | 14569 | |
4633a006 AN |
14570 | /* either both IDs should be set or both should be zero */ |
14571 | if (!!old_id != !!cur_id) | |
14572 | return false; | |
14573 | ||
14574 | if (old_id == 0) /* cur_id == 0 as well */ | |
14575 | return true; | |
14576 | ||
c9e73e3d | 14577 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
14578 | if (!idmap[i].old) { |
14579 | /* Reached an empty slot; haven't seen this id before */ | |
14580 | idmap[i].old = old_id; | |
14581 | idmap[i].cur = cur_id; | |
14582 | return true; | |
14583 | } | |
14584 | if (idmap[i].old == old_id) | |
14585 | return idmap[i].cur == cur_id; | |
14586 | } | |
14587 | /* We ran out of idmap slots, which should be impossible */ | |
14588 | WARN_ON_ONCE(1); | |
14589 | return false; | |
14590 | } | |
14591 | ||
9242b5f5 AS |
14592 | static void clean_func_state(struct bpf_verifier_env *env, |
14593 | struct bpf_func_state *st) | |
14594 | { | |
14595 | enum bpf_reg_liveness live; | |
14596 | int i, j; | |
14597 | ||
14598 | for (i = 0; i < BPF_REG_FP; i++) { | |
14599 | live = st->regs[i].live; | |
14600 | /* liveness must not touch this register anymore */ | |
14601 | st->regs[i].live |= REG_LIVE_DONE; | |
14602 | if (!(live & REG_LIVE_READ)) | |
14603 | /* since the register is unused, clear its state | |
14604 | * to make further comparison simpler | |
14605 | */ | |
f54c7898 | 14606 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
14607 | } |
14608 | ||
14609 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
14610 | live = st->stack[i].spilled_ptr.live; | |
14611 | /* liveness must not touch this stack slot anymore */ | |
14612 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
14613 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 14614 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
14615 | for (j = 0; j < BPF_REG_SIZE; j++) |
14616 | st->stack[i].slot_type[j] = STACK_INVALID; | |
14617 | } | |
14618 | } | |
14619 | } | |
14620 | ||
14621 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
14622 | struct bpf_verifier_state *st) | |
14623 | { | |
14624 | int i; | |
14625 | ||
14626 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
14627 | /* all regs in this state in all frames were already marked */ | |
14628 | return; | |
14629 | ||
14630 | for (i = 0; i <= st->curframe; i++) | |
14631 | clean_func_state(env, st->frame[i]); | |
14632 | } | |
14633 | ||
14634 | /* the parentage chains form a tree. | |
14635 | * the verifier states are added to state lists at given insn and | |
14636 | * pushed into state stack for future exploration. | |
14637 | * when the verifier reaches bpf_exit insn some of the verifer states | |
14638 | * stored in the state lists have their final liveness state already, | |
14639 | * but a lot of states will get revised from liveness point of view when | |
14640 | * the verifier explores other branches. | |
14641 | * Example: | |
14642 | * 1: r0 = 1 | |
14643 | * 2: if r1 == 100 goto pc+1 | |
14644 | * 3: r0 = 2 | |
14645 | * 4: exit | |
14646 | * when the verifier reaches exit insn the register r0 in the state list of | |
14647 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
14648 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
14649 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
14650 | * | |
14651 | * Since the verifier pushes the branch states as it sees them while exploring | |
14652 | * the program the condition of walking the branch instruction for the second | |
14653 | * time means that all states below this branch were already explored and | |
8fb33b60 | 14654 | * their final liveness marks are already propagated. |
9242b5f5 AS |
14655 | * Hence when the verifier completes the search of state list in is_state_visited() |
14656 | * we can call this clean_live_states() function to mark all liveness states | |
14657 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
14658 | * will not be used. | |
14659 | * This function also clears the registers and stack for states that !READ | |
14660 | * to simplify state merging. | |
14661 | * | |
14662 | * Important note here that walking the same branch instruction in the callee | |
14663 | * doesn't meant that the states are DONE. The verifier has to compare | |
14664 | * the callsites | |
14665 | */ | |
14666 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
14667 | struct bpf_verifier_state *cur) | |
14668 | { | |
14669 | struct bpf_verifier_state_list *sl; | |
14670 | int i; | |
14671 | ||
5d839021 | 14672 | sl = *explored_state(env, insn); |
a8f500af | 14673 | while (sl) { |
2589726d AS |
14674 | if (sl->state.branches) |
14675 | goto next; | |
dc2a4ebc AS |
14676 | if (sl->state.insn_idx != insn || |
14677 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
14678 | goto next; |
14679 | for (i = 0; i <= cur->curframe; i++) | |
14680 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
14681 | goto next; | |
14682 | clean_verifier_state(env, &sl->state); | |
14683 | next: | |
14684 | sl = sl->next; | |
14685 | } | |
14686 | } | |
14687 | ||
4a95c85c | 14688 | static bool regs_exact(const struct bpf_reg_state *rold, |
4633a006 AN |
14689 | const struct bpf_reg_state *rcur, |
14690 | struct bpf_id_pair *idmap) | |
4a95c85c | 14691 | { |
4633a006 AN |
14692 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
14693 | check_ids(rold->id, rcur->id, idmap) && | |
14694 | check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap); | |
4a95c85c AN |
14695 | } |
14696 | ||
f1174f77 | 14697 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
14698 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
14699 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 14700 | { |
dc503a8a EC |
14701 | if (!(rold->live & REG_LIVE_READ)) |
14702 | /* explored state didn't use this */ | |
14703 | return true; | |
f1174f77 EC |
14704 | if (rold->type == NOT_INIT) |
14705 | /* explored state can't have used this */ | |
969bf05e | 14706 | return true; |
f1174f77 EC |
14707 | if (rcur->type == NOT_INIT) |
14708 | return false; | |
7f4ce97c | 14709 | |
910f6999 AN |
14710 | /* Enforce that register types have to match exactly, including their |
14711 | * modifiers (like PTR_MAYBE_NULL, MEM_RDONLY, etc), as a general | |
14712 | * rule. | |
14713 | * | |
14714 | * One can make a point that using a pointer register as unbounded | |
14715 | * SCALAR would be technically acceptable, but this could lead to | |
14716 | * pointer leaks because scalars are allowed to leak while pointers | |
14717 | * are not. We could make this safe in special cases if root is | |
14718 | * calling us, but it's probably not worth the hassle. | |
14719 | * | |
14720 | * Also, register types that are *not* MAYBE_NULL could technically be | |
14721 | * safe to use as their MAYBE_NULL variants (e.g., PTR_TO_MAP_VALUE | |
14722 | * is safe to be used as PTR_TO_MAP_VALUE_OR_NULL, provided both point | |
14723 | * to the same map). | |
7f4ce97c AN |
14724 | * However, if the old MAYBE_NULL register then got NULL checked, |
14725 | * doing so could have affected others with the same id, and we can't | |
14726 | * check for that because we lost the id when we converted to | |
14727 | * a non-MAYBE_NULL variant. | |
14728 | * So, as a general rule we don't allow mixing MAYBE_NULL and | |
910f6999 | 14729 | * non-MAYBE_NULL registers as well. |
7f4ce97c | 14730 | */ |
910f6999 | 14731 | if (rold->type != rcur->type) |
7f4ce97c AN |
14732 | return false; |
14733 | ||
c25b2ae1 | 14734 | switch (base_type(rold->type)) { |
f1174f77 | 14735 | case SCALAR_VALUE: |
4633a006 | 14736 | if (regs_exact(rold, rcur, idmap)) |
7c884339 | 14737 | return true; |
e042aa53 DB |
14738 | if (env->explore_alu_limits) |
14739 | return false; | |
910f6999 AN |
14740 | if (!rold->precise) |
14741 | return true; | |
14742 | /* new val must satisfy old val knowledge */ | |
14743 | return range_within(rold, rcur) && | |
14744 | tnum_in(rold->var_off, rcur->var_off); | |
69c087ba | 14745 | case PTR_TO_MAP_KEY: |
f1174f77 | 14746 | case PTR_TO_MAP_VALUE: |
567da5d2 AN |
14747 | case PTR_TO_MEM: |
14748 | case PTR_TO_BUF: | |
14749 | case PTR_TO_TP_BUFFER: | |
1b688a19 EC |
14750 | /* If the new min/max/var_off satisfy the old ones and |
14751 | * everything else matches, we are OK. | |
1b688a19 | 14752 | */ |
a73bf9f2 | 14753 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, var_off)) == 0 && |
1b688a19 | 14754 | range_within(rold, rcur) && |
4ea2bb15 | 14755 | tnum_in(rold->var_off, rcur->var_off) && |
567da5d2 AN |
14756 | check_ids(rold->id, rcur->id, idmap) && |
14757 | check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap); | |
de8f3a83 | 14758 | case PTR_TO_PACKET_META: |
f1174f77 | 14759 | case PTR_TO_PACKET: |
f1174f77 EC |
14760 | /* We must have at least as much range as the old ptr |
14761 | * did, so that any accesses which were safe before are | |
14762 | * still safe. This is true even if old range < old off, | |
14763 | * since someone could have accessed through (ptr - k), or | |
14764 | * even done ptr -= k in a register, to get a safe access. | |
14765 | */ | |
14766 | if (rold->range > rcur->range) | |
14767 | return false; | |
14768 | /* If the offsets don't match, we can't trust our alignment; | |
14769 | * nor can we be sure that we won't fall out of range. | |
14770 | */ | |
14771 | if (rold->off != rcur->off) | |
14772 | return false; | |
14773 | /* id relations must be preserved */ | |
4633a006 | 14774 | if (!check_ids(rold->id, rcur->id, idmap)) |
f1174f77 EC |
14775 | return false; |
14776 | /* new val must satisfy old val knowledge */ | |
14777 | return range_within(rold, rcur) && | |
14778 | tnum_in(rold->var_off, rcur->var_off); | |
7c884339 EZ |
14779 | case PTR_TO_STACK: |
14780 | /* two stack pointers are equal only if they're pointing to | |
14781 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
f1174f77 | 14782 | */ |
4633a006 | 14783 | return regs_exact(rold, rcur, idmap) && rold->frameno == rcur->frameno; |
f1174f77 | 14784 | default: |
4633a006 | 14785 | return regs_exact(rold, rcur, idmap); |
f1174f77 | 14786 | } |
969bf05e AS |
14787 | } |
14788 | ||
e042aa53 DB |
14789 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
14790 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
14791 | { |
14792 | int i, spi; | |
14793 | ||
638f5b90 AS |
14794 | /* walk slots of the explored stack and ignore any additional |
14795 | * slots in the current stack, since explored(safe) state | |
14796 | * didn't use them | |
14797 | */ | |
14798 | for (i = 0; i < old->allocated_stack; i++) { | |
06accc87 AN |
14799 | struct bpf_reg_state *old_reg, *cur_reg; |
14800 | ||
638f5b90 AS |
14801 | spi = i / BPF_REG_SIZE; |
14802 | ||
b233920c AS |
14803 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
14804 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 14805 | /* explored state didn't use this */ |
fd05e57b | 14806 | continue; |
b233920c | 14807 | } |
cc2b14d5 | 14808 | |
638f5b90 AS |
14809 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
14810 | continue; | |
19e2dbb7 | 14811 | |
6715df8d EZ |
14812 | if (env->allow_uninit_stack && |
14813 | old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC) | |
14814 | continue; | |
14815 | ||
19e2dbb7 AS |
14816 | /* explored stack has more populated slots than current stack |
14817 | * and these slots were used | |
14818 | */ | |
14819 | if (i >= cur->allocated_stack) | |
14820 | return false; | |
14821 | ||
cc2b14d5 AS |
14822 | /* if old state was safe with misc data in the stack |
14823 | * it will be safe with zero-initialized stack. | |
14824 | * The opposite is not true | |
14825 | */ | |
14826 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
14827 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
14828 | continue; | |
638f5b90 AS |
14829 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
14830 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
14831 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 14832 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
14833 | * this verifier states are not equivalent, |
14834 | * return false to continue verification of this path | |
14835 | */ | |
14836 | return false; | |
27113c59 | 14837 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 14838 | continue; |
d6fefa11 KKD |
14839 | /* Both old and cur are having same slot_type */ |
14840 | switch (old->stack[spi].slot_type[BPF_REG_SIZE - 1]) { | |
14841 | case STACK_SPILL: | |
638f5b90 AS |
14842 | /* when explored and current stack slot are both storing |
14843 | * spilled registers, check that stored pointers types | |
14844 | * are the same as well. | |
14845 | * Ex: explored safe path could have stored | |
14846 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
14847 | * but current path has stored: | |
14848 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
14849 | * such verifier states are not equivalent. | |
14850 | * return false to continue verification of this path | |
14851 | */ | |
d6fefa11 KKD |
14852 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
14853 | &cur->stack[spi].spilled_ptr, idmap)) | |
14854 | return false; | |
14855 | break; | |
14856 | case STACK_DYNPTR: | |
d6fefa11 KKD |
14857 | old_reg = &old->stack[spi].spilled_ptr; |
14858 | cur_reg = &cur->stack[spi].spilled_ptr; | |
14859 | if (old_reg->dynptr.type != cur_reg->dynptr.type || | |
14860 | old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot || | |
14861 | !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap)) | |
14862 | return false; | |
14863 | break; | |
06accc87 AN |
14864 | case STACK_ITER: |
14865 | old_reg = &old->stack[spi].spilled_ptr; | |
14866 | cur_reg = &cur->stack[spi].spilled_ptr; | |
14867 | /* iter.depth is not compared between states as it | |
14868 | * doesn't matter for correctness and would otherwise | |
14869 | * prevent convergence; we maintain it only to prevent | |
14870 | * infinite loop check triggering, see | |
14871 | * iter_active_depths_differ() | |
14872 | */ | |
14873 | if (old_reg->iter.btf != cur_reg->iter.btf || | |
14874 | old_reg->iter.btf_id != cur_reg->iter.btf_id || | |
14875 | old_reg->iter.state != cur_reg->iter.state || | |
14876 | /* ignore {old_reg,cur_reg}->iter.depth, see above */ | |
14877 | !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap)) | |
14878 | return false; | |
14879 | break; | |
d6fefa11 KKD |
14880 | case STACK_MISC: |
14881 | case STACK_ZERO: | |
14882 | case STACK_INVALID: | |
14883 | continue; | |
14884 | /* Ensure that new unhandled slot types return false by default */ | |
14885 | default: | |
638f5b90 | 14886 | return false; |
d6fefa11 | 14887 | } |
638f5b90 AS |
14888 | } |
14889 | return true; | |
14890 | } | |
14891 | ||
e8f55fcf AN |
14892 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur, |
14893 | struct bpf_id_pair *idmap) | |
fd978bf7 | 14894 | { |
e8f55fcf AN |
14895 | int i; |
14896 | ||
fd978bf7 JS |
14897 | if (old->acquired_refs != cur->acquired_refs) |
14898 | return false; | |
e8f55fcf AN |
14899 | |
14900 | for (i = 0; i < old->acquired_refs; i++) { | |
14901 | if (!check_ids(old->refs[i].id, cur->refs[i].id, idmap)) | |
14902 | return false; | |
14903 | } | |
14904 | ||
14905 | return true; | |
fd978bf7 JS |
14906 | } |
14907 | ||
f1bca824 AS |
14908 | /* compare two verifier states |
14909 | * | |
14910 | * all states stored in state_list are known to be valid, since | |
14911 | * verifier reached 'bpf_exit' instruction through them | |
14912 | * | |
14913 | * this function is called when verifier exploring different branches of | |
14914 | * execution popped from the state stack. If it sees an old state that has | |
14915 | * more strict register state and more strict stack state then this execution | |
14916 | * branch doesn't need to be explored further, since verifier already | |
14917 | * concluded that more strict state leads to valid finish. | |
14918 | * | |
14919 | * Therefore two states are equivalent if register state is more conservative | |
14920 | * and explored stack state is more conservative than the current one. | |
14921 | * Example: | |
14922 | * explored current | |
14923 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
14924 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
14925 | * | |
14926 | * In other words if current stack state (one being explored) has more | |
14927 | * valid slots than old one that already passed validation, it means | |
14928 | * the verifier can stop exploring and conclude that current state is valid too | |
14929 | * | |
14930 | * Similarly with registers. If explored state has register type as invalid | |
14931 | * whereas register type in current state is meaningful, it means that | |
14932 | * the current state will reach 'bpf_exit' instruction safely | |
14933 | */ | |
c9e73e3d | 14934 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 14935 | struct bpf_func_state *cur) |
f1bca824 AS |
14936 | { |
14937 | int i; | |
14938 | ||
c9e73e3d | 14939 | for (i = 0; i < MAX_BPF_REG; i++) |
e042aa53 DB |
14940 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
14941 | env->idmap_scratch)) | |
c9e73e3d | 14942 | return false; |
f1bca824 | 14943 | |
e042aa53 | 14944 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 14945 | return false; |
fd978bf7 | 14946 | |
e8f55fcf | 14947 | if (!refsafe(old, cur, env->idmap_scratch)) |
c9e73e3d LB |
14948 | return false; |
14949 | ||
14950 | return true; | |
f1bca824 AS |
14951 | } |
14952 | ||
f4d7e40a AS |
14953 | static bool states_equal(struct bpf_verifier_env *env, |
14954 | struct bpf_verifier_state *old, | |
14955 | struct bpf_verifier_state *cur) | |
14956 | { | |
14957 | int i; | |
14958 | ||
14959 | if (old->curframe != cur->curframe) | |
14960 | return false; | |
14961 | ||
5dd9cdbc EZ |
14962 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
14963 | ||
979d63d5 DB |
14964 | /* Verification state from speculative execution simulation |
14965 | * must never prune a non-speculative execution one. | |
14966 | */ | |
14967 | if (old->speculative && !cur->speculative) | |
14968 | return false; | |
14969 | ||
4ea2bb15 EZ |
14970 | if (old->active_lock.ptr != cur->active_lock.ptr) |
14971 | return false; | |
14972 | ||
14973 | /* Old and cur active_lock's have to be either both present | |
14974 | * or both absent. | |
14975 | */ | |
14976 | if (!!old->active_lock.id != !!cur->active_lock.id) | |
14977 | return false; | |
14978 | ||
14979 | if (old->active_lock.id && | |
14980 | !check_ids(old->active_lock.id, cur->active_lock.id, env->idmap_scratch)) | |
d83525ca AS |
14981 | return false; |
14982 | ||
9bb00b28 | 14983 | if (old->active_rcu_lock != cur->active_rcu_lock) |
d83525ca AS |
14984 | return false; |
14985 | ||
f4d7e40a AS |
14986 | /* for states to be equal callsites have to be the same |
14987 | * and all frame states need to be equivalent | |
14988 | */ | |
14989 | for (i = 0; i <= old->curframe; i++) { | |
14990 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
14991 | return false; | |
c9e73e3d | 14992 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
14993 | return false; |
14994 | } | |
14995 | return true; | |
14996 | } | |
14997 | ||
5327ed3d JW |
14998 | /* Return 0 if no propagation happened. Return negative error code if error |
14999 | * happened. Otherwise, return the propagated bit. | |
15000 | */ | |
55e7f3b5 JW |
15001 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
15002 | struct bpf_reg_state *reg, | |
15003 | struct bpf_reg_state *parent_reg) | |
15004 | { | |
5327ed3d JW |
15005 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
15006 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
15007 | int err; |
15008 | ||
5327ed3d JW |
15009 | /* When comes here, read flags of PARENT_REG or REG could be any of |
15010 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
15011 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
15012 | */ | |
15013 | if (parent_flag == REG_LIVE_READ64 || | |
15014 | /* Or if there is no read flag from REG. */ | |
15015 | !flag || | |
15016 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
15017 | parent_flag == flag) | |
55e7f3b5 JW |
15018 | return 0; |
15019 | ||
5327ed3d | 15020 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
15021 | if (err) |
15022 | return err; | |
15023 | ||
5327ed3d | 15024 | return flag; |
55e7f3b5 JW |
15025 | } |
15026 | ||
8e9cd9ce | 15027 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
15028 | * straight-line code between a state and its parent. When we arrive at an |
15029 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
15030 | * code, so read marks in the state must propagate to the parent regardless | |
15031 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 15032 | * in mark_reg_read() is for. |
8e9cd9ce | 15033 | */ |
f4d7e40a AS |
15034 | static int propagate_liveness(struct bpf_verifier_env *env, |
15035 | const struct bpf_verifier_state *vstate, | |
15036 | struct bpf_verifier_state *vparent) | |
dc503a8a | 15037 | { |
3f8cafa4 | 15038 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 15039 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 15040 | int i, frame, err = 0; |
dc503a8a | 15041 | |
f4d7e40a AS |
15042 | if (vparent->curframe != vstate->curframe) { |
15043 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
15044 | vparent->curframe, vstate->curframe); | |
15045 | return -EFAULT; | |
15046 | } | |
dc503a8a EC |
15047 | /* Propagate read liveness of registers... */ |
15048 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 15049 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
15050 | parent = vparent->frame[frame]; |
15051 | state = vstate->frame[frame]; | |
15052 | parent_reg = parent->regs; | |
15053 | state_reg = state->regs; | |
83d16312 JK |
15054 | /* We don't need to worry about FP liveness, it's read-only */ |
15055 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
15056 | err = propagate_liveness_reg(env, &state_reg[i], |
15057 | &parent_reg[i]); | |
5327ed3d | 15058 | if (err < 0) |
3f8cafa4 | 15059 | return err; |
5327ed3d JW |
15060 | if (err == REG_LIVE_READ64) |
15061 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 15062 | } |
f4d7e40a | 15063 | |
1b04aee7 | 15064 | /* Propagate stack slots. */ |
f4d7e40a AS |
15065 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
15066 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
15067 | parent_reg = &parent->stack[i].spilled_ptr; |
15068 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
15069 | err = propagate_liveness_reg(env, state_reg, |
15070 | parent_reg); | |
5327ed3d | 15071 | if (err < 0) |
3f8cafa4 | 15072 | return err; |
dc503a8a EC |
15073 | } |
15074 | } | |
5327ed3d | 15075 | return 0; |
dc503a8a EC |
15076 | } |
15077 | ||
a3ce685d AS |
15078 | /* find precise scalars in the previous equivalent state and |
15079 | * propagate them into the current state | |
15080 | */ | |
15081 | static int propagate_precision(struct bpf_verifier_env *env, | |
15082 | const struct bpf_verifier_state *old) | |
15083 | { | |
15084 | struct bpf_reg_state *state_reg; | |
15085 | struct bpf_func_state *state; | |
529409ea | 15086 | int i, err = 0, fr; |
a3ce685d | 15087 | |
529409ea AN |
15088 | for (fr = old->curframe; fr >= 0; fr--) { |
15089 | state = old->frame[fr]; | |
15090 | state_reg = state->regs; | |
15091 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
15092 | if (state_reg->type != SCALAR_VALUE || | |
52c2b005 AN |
15093 | !state_reg->precise || |
15094 | !(state_reg->live & REG_LIVE_READ)) | |
529409ea AN |
15095 | continue; |
15096 | if (env->log.level & BPF_LOG_LEVEL2) | |
34f0677e | 15097 | verbose(env, "frame %d: propagating r%d\n", fr, i); |
529409ea AN |
15098 | err = mark_chain_precision_frame(env, fr, i); |
15099 | if (err < 0) | |
15100 | return err; | |
15101 | } | |
a3ce685d | 15102 | |
529409ea AN |
15103 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
15104 | if (!is_spilled_reg(&state->stack[i])) | |
15105 | continue; | |
15106 | state_reg = &state->stack[i].spilled_ptr; | |
15107 | if (state_reg->type != SCALAR_VALUE || | |
52c2b005 AN |
15108 | !state_reg->precise || |
15109 | !(state_reg->live & REG_LIVE_READ)) | |
529409ea AN |
15110 | continue; |
15111 | if (env->log.level & BPF_LOG_LEVEL2) | |
15112 | verbose(env, "frame %d: propagating fp%d\n", | |
34f0677e | 15113 | fr, (-i - 1) * BPF_REG_SIZE); |
529409ea AN |
15114 | err = mark_chain_precision_stack_frame(env, fr, i); |
15115 | if (err < 0) | |
15116 | return err; | |
15117 | } | |
a3ce685d AS |
15118 | } |
15119 | return 0; | |
15120 | } | |
15121 | ||
2589726d AS |
15122 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
15123 | struct bpf_verifier_state *cur) | |
15124 | { | |
15125 | struct bpf_func_state *fold, *fcur; | |
15126 | int i, fr = cur->curframe; | |
15127 | ||
15128 | if (old->curframe != fr) | |
15129 | return false; | |
15130 | ||
15131 | fold = old->frame[fr]; | |
15132 | fcur = cur->frame[fr]; | |
15133 | for (i = 0; i < MAX_BPF_REG; i++) | |
15134 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
15135 | offsetof(struct bpf_reg_state, parent))) | |
15136 | return false; | |
15137 | return true; | |
15138 | } | |
15139 | ||
06accc87 AN |
15140 | static bool is_iter_next_insn(struct bpf_verifier_env *env, int insn_idx) |
15141 | { | |
15142 | return env->insn_aux_data[insn_idx].is_iter_next; | |
15143 | } | |
15144 | ||
15145 | /* is_state_visited() handles iter_next() (see process_iter_next_call() for | |
15146 | * terminology) calls specially: as opposed to bounded BPF loops, it *expects* | |
15147 | * states to match, which otherwise would look like an infinite loop. So while | |
15148 | * iter_next() calls are taken care of, we still need to be careful and | |
15149 | * prevent erroneous and too eager declaration of "ininite loop", when | |
15150 | * iterators are involved. | |
15151 | * | |
15152 | * Here's a situation in pseudo-BPF assembly form: | |
15153 | * | |
15154 | * 0: again: ; set up iter_next() call args | |
15155 | * 1: r1 = &it ; <CHECKPOINT HERE> | |
15156 | * 2: call bpf_iter_num_next ; this is iter_next() call | |
15157 | * 3: if r0 == 0 goto done | |
15158 | * 4: ... something useful here ... | |
15159 | * 5: goto again ; another iteration | |
15160 | * 6: done: | |
15161 | * 7: r1 = &it | |
15162 | * 8: call bpf_iter_num_destroy ; clean up iter state | |
15163 | * 9: exit | |
15164 | * | |
15165 | * This is a typical loop. Let's assume that we have a prune point at 1:, | |
15166 | * before we get to `call bpf_iter_num_next` (e.g., because of that `goto | |
15167 | * again`, assuming other heuristics don't get in a way). | |
15168 | * | |
15169 | * When we first time come to 1:, let's say we have some state X. We proceed | |
15170 | * to 2:, fork states, enqueue ACTIVE, validate NULL case successfully, exit. | |
15171 | * Now we come back to validate that forked ACTIVE state. We proceed through | |
15172 | * 3-5, come to goto, jump to 1:. Let's assume our state didn't change, so we | |
15173 | * are converging. But the problem is that we don't know that yet, as this | |
15174 | * convergence has to happen at iter_next() call site only. So if nothing is | |
15175 | * done, at 1: verifier will use bounded loop logic and declare infinite | |
15176 | * looping (and would be *technically* correct, if not for iterator's | |
15177 | * "eventual sticky NULL" contract, see process_iter_next_call()). But we | |
15178 | * don't want that. So what we do in process_iter_next_call() when we go on | |
15179 | * another ACTIVE iteration, we bump slot->iter.depth, to mark that it's | |
15180 | * a different iteration. So when we suspect an infinite loop, we additionally | |
15181 | * check if any of the *ACTIVE* iterator states depths differ. If yes, we | |
15182 | * pretend we are not looping and wait for next iter_next() call. | |
15183 | * | |
15184 | * This only applies to ACTIVE state. In DRAINED state we don't expect to | |
15185 | * loop, because that would actually mean infinite loop, as DRAINED state is | |
15186 | * "sticky", and so we'll keep returning into the same instruction with the | |
15187 | * same state (at least in one of possible code paths). | |
15188 | * | |
15189 | * This approach allows to keep infinite loop heuristic even in the face of | |
15190 | * active iterator. E.g., C snippet below is and will be detected as | |
15191 | * inifintely looping: | |
15192 | * | |
15193 | * struct bpf_iter_num it; | |
15194 | * int *p, x; | |
15195 | * | |
15196 | * bpf_iter_num_new(&it, 0, 10); | |
15197 | * while ((p = bpf_iter_num_next(&t))) { | |
15198 | * x = p; | |
15199 | * while (x--) {} // <<-- infinite loop here | |
15200 | * } | |
15201 | * | |
15202 | */ | |
15203 | static bool iter_active_depths_differ(struct bpf_verifier_state *old, struct bpf_verifier_state *cur) | |
15204 | { | |
15205 | struct bpf_reg_state *slot, *cur_slot; | |
15206 | struct bpf_func_state *state; | |
15207 | int i, fr; | |
15208 | ||
15209 | for (fr = old->curframe; fr >= 0; fr--) { | |
15210 | state = old->frame[fr]; | |
15211 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
15212 | if (state->stack[i].slot_type[0] != STACK_ITER) | |
15213 | continue; | |
15214 | ||
15215 | slot = &state->stack[i].spilled_ptr; | |
15216 | if (slot->iter.state != BPF_ITER_STATE_ACTIVE) | |
15217 | continue; | |
15218 | ||
15219 | cur_slot = &cur->frame[fr]->stack[i].spilled_ptr; | |
15220 | if (cur_slot->iter.depth != slot->iter.depth) | |
15221 | return true; | |
15222 | } | |
15223 | } | |
15224 | return false; | |
15225 | } | |
2589726d | 15226 | |
58e2af8b | 15227 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 15228 | { |
58e2af8b | 15229 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 15230 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 15231 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 15232 | int i, j, err, states_cnt = 0; |
4b5ce570 AN |
15233 | bool force_new_state = env->test_state_freq || is_force_checkpoint(env, insn_idx); |
15234 | bool add_new_state = force_new_state; | |
f1bca824 | 15235 | |
2589726d AS |
15236 | /* bpf progs typically have pruning point every 4 instructions |
15237 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
15238 | * Do not add new state for future pruning if the verifier hasn't seen | |
15239 | * at least 2 jumps and at least 8 instructions. | |
15240 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
15241 | * In tests that amounts to up to 50% reduction into total verifier | |
15242 | * memory consumption and 20% verifier time speedup. | |
15243 | */ | |
15244 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
15245 | env->insn_processed - env->prev_insn_processed >= 8) | |
15246 | add_new_state = true; | |
15247 | ||
a8f500af AS |
15248 | pprev = explored_state(env, insn_idx); |
15249 | sl = *pprev; | |
15250 | ||
9242b5f5 AS |
15251 | clean_live_states(env, insn_idx, cur); |
15252 | ||
a8f500af | 15253 | while (sl) { |
dc2a4ebc AS |
15254 | states_cnt++; |
15255 | if (sl->state.insn_idx != insn_idx) | |
15256 | goto next; | |
bfc6bb74 | 15257 | |
2589726d | 15258 | if (sl->state.branches) { |
bfc6bb74 AS |
15259 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
15260 | ||
15261 | if (frame->in_async_callback_fn && | |
15262 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
15263 | /* Different async_entry_cnt means that the verifier is | |
15264 | * processing another entry into async callback. | |
15265 | * Seeing the same state is not an indication of infinite | |
15266 | * loop or infinite recursion. | |
15267 | * But finding the same state doesn't mean that it's safe | |
15268 | * to stop processing the current state. The previous state | |
15269 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
15270 | * Checking in_async_callback_fn alone is not enough either. | |
15271 | * Since the verifier still needs to catch infinite loops | |
15272 | * inside async callbacks. | |
15273 | */ | |
06accc87 AN |
15274 | goto skip_inf_loop_check; |
15275 | } | |
15276 | /* BPF open-coded iterators loop detection is special. | |
15277 | * states_maybe_looping() logic is too simplistic in detecting | |
15278 | * states that *might* be equivalent, because it doesn't know | |
15279 | * about ID remapping, so don't even perform it. | |
15280 | * See process_iter_next_call() and iter_active_depths_differ() | |
15281 | * for overview of the logic. When current and one of parent | |
15282 | * states are detected as equivalent, it's a good thing: we prove | |
15283 | * convergence and can stop simulating further iterations. | |
15284 | * It's safe to assume that iterator loop will finish, taking into | |
15285 | * account iter_next() contract of eventually returning | |
15286 | * sticky NULL result. | |
15287 | */ | |
15288 | if (is_iter_next_insn(env, insn_idx)) { | |
15289 | if (states_equal(env, &sl->state, cur)) { | |
15290 | struct bpf_func_state *cur_frame; | |
15291 | struct bpf_reg_state *iter_state, *iter_reg; | |
15292 | int spi; | |
15293 | ||
15294 | cur_frame = cur->frame[cur->curframe]; | |
15295 | /* btf_check_iter_kfuncs() enforces that | |
15296 | * iter state pointer is always the first arg | |
15297 | */ | |
15298 | iter_reg = &cur_frame->regs[BPF_REG_1]; | |
15299 | /* current state is valid due to states_equal(), | |
15300 | * so we can assume valid iter and reg state, | |
15301 | * no need for extra (re-)validations | |
15302 | */ | |
15303 | spi = __get_spi(iter_reg->off + iter_reg->var_off.value); | |
15304 | iter_state = &func(env, iter_reg)->stack[spi].spilled_ptr; | |
15305 | if (iter_state->iter.state == BPF_ITER_STATE_ACTIVE) | |
15306 | goto hit; | |
15307 | } | |
15308 | goto skip_inf_loop_check; | |
15309 | } | |
15310 | /* attempt to detect infinite loop to avoid unnecessary doomed work */ | |
15311 | if (states_maybe_looping(&sl->state, cur) && | |
15312 | states_equal(env, &sl->state, cur) && | |
15313 | !iter_active_depths_differ(&sl->state, cur)) { | |
2589726d AS |
15314 | verbose_linfo(env, insn_idx, "; "); |
15315 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
15316 | return -EINVAL; | |
15317 | } | |
15318 | /* if the verifier is processing a loop, avoid adding new state | |
15319 | * too often, since different loop iterations have distinct | |
15320 | * states and may not help future pruning. | |
15321 | * This threshold shouldn't be too low to make sure that | |
15322 | * a loop with large bound will be rejected quickly. | |
15323 | * The most abusive loop will be: | |
15324 | * r1 += 1 | |
15325 | * if r1 < 1000000 goto pc-2 | |
15326 | * 1M insn_procssed limit / 100 == 10k peak states. | |
15327 | * This threshold shouldn't be too high either, since states | |
15328 | * at the end of the loop are likely to be useful in pruning. | |
15329 | */ | |
06accc87 | 15330 | skip_inf_loop_check: |
4b5ce570 | 15331 | if (!force_new_state && |
98ddcf38 | 15332 | env->jmps_processed - env->prev_jmps_processed < 20 && |
2589726d AS |
15333 | env->insn_processed - env->prev_insn_processed < 100) |
15334 | add_new_state = false; | |
15335 | goto miss; | |
15336 | } | |
638f5b90 | 15337 | if (states_equal(env, &sl->state, cur)) { |
06accc87 | 15338 | hit: |
9f4686c4 | 15339 | sl->hit_cnt++; |
f1bca824 | 15340 | /* reached equivalent register/stack state, |
dc503a8a EC |
15341 | * prune the search. |
15342 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
15343 | * If we have any write marks in env->cur_state, they |
15344 | * will prevent corresponding reads in the continuation | |
15345 | * from reaching our parent (an explored_state). Our | |
15346 | * own state will get the read marks recorded, but | |
15347 | * they'll be immediately forgotten as we're pruning | |
15348 | * this state and will pop a new one. | |
f1bca824 | 15349 | */ |
f4d7e40a | 15350 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
15351 | |
15352 | /* if previous state reached the exit with precision and | |
15353 | * current state is equivalent to it (except precsion marks) | |
15354 | * the precision needs to be propagated back in | |
15355 | * the current state. | |
15356 | */ | |
15357 | err = err ? : push_jmp_history(env, cur); | |
15358 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
15359 | if (err) |
15360 | return err; | |
f1bca824 | 15361 | return 1; |
dc503a8a | 15362 | } |
2589726d AS |
15363 | miss: |
15364 | /* when new state is not going to be added do not increase miss count. | |
15365 | * Otherwise several loop iterations will remove the state | |
15366 | * recorded earlier. The goal of these heuristics is to have | |
15367 | * states from some iterations of the loop (some in the beginning | |
15368 | * and some at the end) to help pruning. | |
15369 | */ | |
15370 | if (add_new_state) | |
15371 | sl->miss_cnt++; | |
9f4686c4 AS |
15372 | /* heuristic to determine whether this state is beneficial |
15373 | * to keep checking from state equivalence point of view. | |
15374 | * Higher numbers increase max_states_per_insn and verification time, | |
15375 | * but do not meaningfully decrease insn_processed. | |
15376 | */ | |
15377 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
15378 | /* the state is unlikely to be useful. Remove it to | |
15379 | * speed up verification | |
15380 | */ | |
15381 | *pprev = sl->next; | |
15382 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
15383 | u32 br = sl->state.branches; |
15384 | ||
15385 | WARN_ONCE(br, | |
15386 | "BUG live_done but branches_to_explore %d\n", | |
15387 | br); | |
9f4686c4 AS |
15388 | free_verifier_state(&sl->state, false); |
15389 | kfree(sl); | |
15390 | env->peak_states--; | |
15391 | } else { | |
15392 | /* cannot free this state, since parentage chain may | |
15393 | * walk it later. Add it for free_list instead to | |
15394 | * be freed at the end of verification | |
15395 | */ | |
15396 | sl->next = env->free_list; | |
15397 | env->free_list = sl; | |
15398 | } | |
15399 | sl = *pprev; | |
15400 | continue; | |
15401 | } | |
dc2a4ebc | 15402 | next: |
9f4686c4 AS |
15403 | pprev = &sl->next; |
15404 | sl = *pprev; | |
f1bca824 AS |
15405 | } |
15406 | ||
06ee7115 AS |
15407 | if (env->max_states_per_insn < states_cnt) |
15408 | env->max_states_per_insn = states_cnt; | |
15409 | ||
2c78ee89 | 15410 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
a095f421 | 15411 | return 0; |
ceefbc96 | 15412 | |
2589726d | 15413 | if (!add_new_state) |
a095f421 | 15414 | return 0; |
ceefbc96 | 15415 | |
2589726d AS |
15416 | /* There were no equivalent states, remember the current one. |
15417 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 15418 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 15419 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 15420 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
15421 | * again on the way to bpf_exit. |
15422 | * When looping the sl->state.branches will be > 0 and this state | |
15423 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 15424 | */ |
638f5b90 | 15425 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
15426 | if (!new_sl) |
15427 | return -ENOMEM; | |
06ee7115 AS |
15428 | env->total_states++; |
15429 | env->peak_states++; | |
2589726d AS |
15430 | env->prev_jmps_processed = env->jmps_processed; |
15431 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 | 15432 | |
7a830b53 AN |
15433 | /* forget precise markings we inherited, see __mark_chain_precision */ |
15434 | if (env->bpf_capable) | |
15435 | mark_all_scalars_imprecise(env, cur); | |
15436 | ||
f1bca824 | 15437 | /* add new state to the head of linked list */ |
679c782d EC |
15438 | new = &new_sl->state; |
15439 | err = copy_verifier_state(new, cur); | |
1969db47 | 15440 | if (err) { |
679c782d | 15441 | free_verifier_state(new, false); |
1969db47 AS |
15442 | kfree(new_sl); |
15443 | return err; | |
15444 | } | |
dc2a4ebc | 15445 | new->insn_idx = insn_idx; |
2589726d AS |
15446 | WARN_ONCE(new->branches != 1, |
15447 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 15448 | |
2589726d | 15449 | cur->parent = new; |
b5dc0163 AS |
15450 | cur->first_insn_idx = insn_idx; |
15451 | clear_jmp_history(cur); | |
5d839021 AS |
15452 | new_sl->next = *explored_state(env, insn_idx); |
15453 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
15454 | /* connect new state to parentage chain. Current frame needs all |
15455 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
15456 | * to the stack implicitly by JITs) so in callers' frames connect just | |
15457 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
15458 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
15459 | * from callee with its full parentage chain, anyway. | |
15460 | */ | |
8e9cd9ce EC |
15461 | /* clear write marks in current state: the writes we did are not writes |
15462 | * our child did, so they don't screen off its reads from us. | |
15463 | * (There are no read marks in current state, because reads always mark | |
15464 | * their parent and current state never has children yet. Only | |
15465 | * explored_states can get read marks.) | |
15466 | */ | |
eea1c227 AS |
15467 | for (j = 0; j <= cur->curframe; j++) { |
15468 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
15469 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
15470 | for (i = 0; i < BPF_REG_FP; i++) | |
15471 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
15472 | } | |
f4d7e40a AS |
15473 | |
15474 | /* all stack frames are accessible from callee, clear them all */ | |
15475 | for (j = 0; j <= cur->curframe; j++) { | |
15476 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 15477 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 15478 | |
679c782d | 15479 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 15480 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
15481 | frame->stack[i].spilled_ptr.parent = |
15482 | &newframe->stack[i].spilled_ptr; | |
15483 | } | |
f4d7e40a | 15484 | } |
f1bca824 AS |
15485 | return 0; |
15486 | } | |
15487 | ||
c64b7983 JS |
15488 | /* Return true if it's OK to have the same insn return a different type. */ |
15489 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
15490 | { | |
c25b2ae1 | 15491 | switch (base_type(type)) { |
c64b7983 JS |
15492 | case PTR_TO_CTX: |
15493 | case PTR_TO_SOCKET: | |
46f8bc92 | 15494 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 15495 | case PTR_TO_TCP_SOCK: |
fada7fdc | 15496 | case PTR_TO_XDP_SOCK: |
2a02759e | 15497 | case PTR_TO_BTF_ID: |
c64b7983 JS |
15498 | return false; |
15499 | default: | |
15500 | return true; | |
15501 | } | |
15502 | } | |
15503 | ||
15504 | /* If an instruction was previously used with particular pointer types, then we | |
15505 | * need to be careful to avoid cases such as the below, where it may be ok | |
15506 | * for one branch accessing the pointer, but not ok for the other branch: | |
15507 | * | |
15508 | * R1 = sock_ptr | |
15509 | * goto X; | |
15510 | * ... | |
15511 | * R1 = some_other_valid_ptr; | |
15512 | * goto X; | |
15513 | * ... | |
15514 | * R2 = *(u32 *)(R1 + 0); | |
15515 | */ | |
15516 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
15517 | { | |
15518 | return src != prev && (!reg_type_mismatch_ok(src) || | |
15519 | !reg_type_mismatch_ok(prev)); | |
15520 | } | |
15521 | ||
0d80a619 EZ |
15522 | static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type, |
15523 | bool allow_trust_missmatch) | |
15524 | { | |
15525 | enum bpf_reg_type *prev_type = &env->insn_aux_data[env->insn_idx].ptr_type; | |
15526 | ||
15527 | if (*prev_type == NOT_INIT) { | |
15528 | /* Saw a valid insn | |
15529 | * dst_reg = *(u32 *)(src_reg + off) | |
15530 | * save type to validate intersecting paths | |
15531 | */ | |
15532 | *prev_type = type; | |
15533 | } else if (reg_type_mismatch(type, *prev_type)) { | |
15534 | /* Abuser program is trying to use the same insn | |
15535 | * dst_reg = *(u32*) (src_reg + off) | |
15536 | * with different pointer types: | |
15537 | * src_reg == ctx in one branch and | |
15538 | * src_reg == stack|map in some other branch. | |
15539 | * Reject it. | |
15540 | */ | |
15541 | if (allow_trust_missmatch && | |
15542 | base_type(type) == PTR_TO_BTF_ID && | |
15543 | base_type(*prev_type) == PTR_TO_BTF_ID) { | |
15544 | /* | |
15545 | * Have to support a use case when one path through | |
15546 | * the program yields TRUSTED pointer while another | |
15547 | * is UNTRUSTED. Fallback to UNTRUSTED to generate | |
15548 | * BPF_PROBE_MEM. | |
15549 | */ | |
15550 | *prev_type = PTR_TO_BTF_ID | PTR_UNTRUSTED; | |
15551 | } else { | |
15552 | verbose(env, "same insn cannot be used with different pointers\n"); | |
15553 | return -EINVAL; | |
15554 | } | |
15555 | } | |
15556 | ||
15557 | return 0; | |
15558 | } | |
15559 | ||
58e2af8b | 15560 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 15561 | { |
6f8a57cc | 15562 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 15563 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 15564 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 15565 | struct bpf_reg_state *regs; |
06ee7115 | 15566 | int insn_cnt = env->prog->len; |
17a52670 | 15567 | bool do_print_state = false; |
b5dc0163 | 15568 | int prev_insn_idx = -1; |
17a52670 | 15569 | |
17a52670 AS |
15570 | for (;;) { |
15571 | struct bpf_insn *insn; | |
15572 | u8 class; | |
15573 | int err; | |
15574 | ||
b5dc0163 | 15575 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 15576 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 15577 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 15578 | env->insn_idx, insn_cnt); |
17a52670 AS |
15579 | return -EFAULT; |
15580 | } | |
15581 | ||
c08435ec | 15582 | insn = &insns[env->insn_idx]; |
17a52670 AS |
15583 | class = BPF_CLASS(insn->code); |
15584 | ||
06ee7115 | 15585 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
15586 | verbose(env, |
15587 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 15588 | env->insn_processed); |
17a52670 AS |
15589 | return -E2BIG; |
15590 | } | |
15591 | ||
a095f421 AN |
15592 | state->last_insn_idx = env->prev_insn_idx; |
15593 | ||
15594 | if (is_prune_point(env, env->insn_idx)) { | |
15595 | err = is_state_visited(env, env->insn_idx); | |
15596 | if (err < 0) | |
15597 | return err; | |
15598 | if (err == 1) { | |
15599 | /* found equivalent state, can prune the search */ | |
15600 | if (env->log.level & BPF_LOG_LEVEL) { | |
15601 | if (do_print_state) | |
15602 | verbose(env, "\nfrom %d to %d%s: safe\n", | |
15603 | env->prev_insn_idx, env->insn_idx, | |
15604 | env->cur_state->speculative ? | |
15605 | " (speculative execution)" : ""); | |
15606 | else | |
15607 | verbose(env, "%d: safe\n", env->insn_idx); | |
15608 | } | |
15609 | goto process_bpf_exit; | |
f1bca824 | 15610 | } |
a095f421 AN |
15611 | } |
15612 | ||
15613 | if (is_jmp_point(env, env->insn_idx)) { | |
15614 | err = push_jmp_history(env, state); | |
15615 | if (err) | |
15616 | return err; | |
f1bca824 AS |
15617 | } |
15618 | ||
c3494801 AS |
15619 | if (signal_pending(current)) |
15620 | return -EAGAIN; | |
15621 | ||
3c2ce60b DB |
15622 | if (need_resched()) |
15623 | cond_resched(); | |
15624 | ||
2e576648 CL |
15625 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
15626 | verbose(env, "\nfrom %d to %d%s:", | |
15627 | env->prev_insn_idx, env->insn_idx, | |
15628 | env->cur_state->speculative ? | |
15629 | " (speculative execution)" : ""); | |
15630 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
15631 | do_print_state = false; |
15632 | } | |
15633 | ||
06ee7115 | 15634 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 15635 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 15636 | .cb_call = disasm_kfunc_name, |
7105e828 | 15637 | .cb_print = verbose, |
abe08840 | 15638 | .private_data = env, |
7105e828 DB |
15639 | }; |
15640 | ||
2e576648 CL |
15641 | if (verifier_state_scratched(env)) |
15642 | print_insn_state(env, state->frame[state->curframe]); | |
15643 | ||
c08435ec | 15644 | verbose_linfo(env, env->insn_idx, "; "); |
2e576648 | 15645 | env->prev_log_len = env->log.len_used; |
c08435ec | 15646 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 15647 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
2e576648 CL |
15648 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
15649 | env->prev_log_len = env->log.len_used; | |
17a52670 AS |
15650 | } |
15651 | ||
9d03ebc7 | 15652 | if (bpf_prog_is_offloaded(env->prog->aux)) { |
c08435ec DB |
15653 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
15654 | env->prev_insn_idx); | |
cae1927c JK |
15655 | if (err) |
15656 | return err; | |
15657 | } | |
13a27dfc | 15658 | |
638f5b90 | 15659 | regs = cur_regs(env); |
fe9a5ca7 | 15660 | sanitize_mark_insn_seen(env); |
b5dc0163 | 15661 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 15662 | |
17a52670 | 15663 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 15664 | err = check_alu_op(env, insn); |
17a52670 AS |
15665 | if (err) |
15666 | return err; | |
15667 | ||
15668 | } else if (class == BPF_LDX) { | |
0d80a619 | 15669 | enum bpf_reg_type src_reg_type; |
9bac3d6d AS |
15670 | |
15671 | /* check for reserved fields is already done */ | |
15672 | ||
17a52670 | 15673 | /* check src operand */ |
dc503a8a | 15674 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
15675 | if (err) |
15676 | return err; | |
15677 | ||
dc503a8a | 15678 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
15679 | if (err) |
15680 | return err; | |
15681 | ||
725f9dcd AS |
15682 | src_reg_type = regs[insn->src_reg].type; |
15683 | ||
17a52670 AS |
15684 | /* check that memory (src_reg + off) is readable, |
15685 | * the state of dst_reg will be updated by this func | |
15686 | */ | |
c08435ec DB |
15687 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
15688 | insn->off, BPF_SIZE(insn->code), | |
15689 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
15690 | if (err) |
15691 | return err; | |
15692 | ||
0d80a619 EZ |
15693 | err = save_aux_ptr_type(env, src_reg_type, true); |
15694 | if (err) | |
15695 | return err; | |
17a52670 | 15696 | } else if (class == BPF_STX) { |
0d80a619 | 15697 | enum bpf_reg_type dst_reg_type; |
d691f9e8 | 15698 | |
91c960b0 BJ |
15699 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
15700 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
15701 | if (err) |
15702 | return err; | |
c08435ec | 15703 | env->insn_idx++; |
17a52670 AS |
15704 | continue; |
15705 | } | |
15706 | ||
5ca419f2 BJ |
15707 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
15708 | verbose(env, "BPF_STX uses reserved fields\n"); | |
15709 | return -EINVAL; | |
15710 | } | |
15711 | ||
17a52670 | 15712 | /* check src1 operand */ |
dc503a8a | 15713 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
15714 | if (err) |
15715 | return err; | |
15716 | /* check src2 operand */ | |
dc503a8a | 15717 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
15718 | if (err) |
15719 | return err; | |
15720 | ||
d691f9e8 AS |
15721 | dst_reg_type = regs[insn->dst_reg].type; |
15722 | ||
17a52670 | 15723 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
15724 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
15725 | insn->off, BPF_SIZE(insn->code), | |
15726 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
15727 | if (err) |
15728 | return err; | |
15729 | ||
0d80a619 EZ |
15730 | err = save_aux_ptr_type(env, dst_reg_type, false); |
15731 | if (err) | |
15732 | return err; | |
17a52670 | 15733 | } else if (class == BPF_ST) { |
0d80a619 EZ |
15734 | enum bpf_reg_type dst_reg_type; |
15735 | ||
17a52670 AS |
15736 | if (BPF_MODE(insn->code) != BPF_MEM || |
15737 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 15738 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
15739 | return -EINVAL; |
15740 | } | |
15741 | /* check src operand */ | |
dc503a8a | 15742 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
15743 | if (err) |
15744 | return err; | |
15745 | ||
0d80a619 | 15746 | dst_reg_type = regs[insn->dst_reg].type; |
f37a8cb8 | 15747 | |
17a52670 | 15748 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
15749 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
15750 | insn->off, BPF_SIZE(insn->code), | |
15751 | BPF_WRITE, -1, false); | |
17a52670 AS |
15752 | if (err) |
15753 | return err; | |
15754 | ||
0d80a619 EZ |
15755 | err = save_aux_ptr_type(env, dst_reg_type, false); |
15756 | if (err) | |
15757 | return err; | |
092ed096 | 15758 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
15759 | u8 opcode = BPF_OP(insn->code); |
15760 | ||
2589726d | 15761 | env->jmps_processed++; |
17a52670 AS |
15762 | if (opcode == BPF_CALL) { |
15763 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
15764 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
15765 | && insn->off != 0) || | |
f4d7e40a | 15766 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
15767 | insn->src_reg != BPF_PSEUDO_CALL && |
15768 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
15769 | insn->dst_reg != BPF_REG_0 || |
15770 | class == BPF_JMP32) { | |
61bd5218 | 15771 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
15772 | return -EINVAL; |
15773 | } | |
15774 | ||
8cab76ec KKD |
15775 | if (env->cur_state->active_lock.ptr) { |
15776 | if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) || | |
15777 | (insn->src_reg == BPF_PSEUDO_CALL) || | |
15778 | (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && | |
cd6791b4 | 15779 | (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) { |
8cab76ec KKD |
15780 | verbose(env, "function calls are not allowed while holding a lock\n"); |
15781 | return -EINVAL; | |
15782 | } | |
d83525ca | 15783 | } |
f4d7e40a | 15784 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 15785 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 | 15786 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
5c073f26 | 15787 | err = check_kfunc_call(env, insn, &env->insn_idx); |
f4d7e40a | 15788 | else |
69c087ba | 15789 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
15790 | if (err) |
15791 | return err; | |
553a64a8 AN |
15792 | |
15793 | mark_reg_scratched(env, BPF_REG_0); | |
17a52670 AS |
15794 | } else if (opcode == BPF_JA) { |
15795 | if (BPF_SRC(insn->code) != BPF_K || | |
15796 | insn->imm != 0 || | |
15797 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
15798 | insn->dst_reg != BPF_REG_0 || |
15799 | class == BPF_JMP32) { | |
61bd5218 | 15800 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
15801 | return -EINVAL; |
15802 | } | |
15803 | ||
c08435ec | 15804 | env->insn_idx += insn->off + 1; |
17a52670 AS |
15805 | continue; |
15806 | ||
15807 | } else if (opcode == BPF_EXIT) { | |
15808 | if (BPF_SRC(insn->code) != BPF_K || | |
15809 | insn->imm != 0 || | |
15810 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
15811 | insn->dst_reg != BPF_REG_0 || |
15812 | class == BPF_JMP32) { | |
61bd5218 | 15813 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
15814 | return -EINVAL; |
15815 | } | |
15816 | ||
5d92ddc3 DM |
15817 | if (env->cur_state->active_lock.ptr && |
15818 | !in_rbtree_lock_required_cb(env)) { | |
d83525ca AS |
15819 | verbose(env, "bpf_spin_unlock is missing\n"); |
15820 | return -EINVAL; | |
15821 | } | |
15822 | ||
9bb00b28 YS |
15823 | if (env->cur_state->active_rcu_lock) { |
15824 | verbose(env, "bpf_rcu_read_unlock is missing\n"); | |
15825 | return -EINVAL; | |
15826 | } | |
15827 | ||
9d9d00ac KKD |
15828 | /* We must do check_reference_leak here before |
15829 | * prepare_func_exit to handle the case when | |
15830 | * state->curframe > 0, it may be a callback | |
15831 | * function, for which reference_state must | |
15832 | * match caller reference state when it exits. | |
15833 | */ | |
15834 | err = check_reference_leak(env); | |
15835 | if (err) | |
15836 | return err; | |
15837 | ||
f4d7e40a AS |
15838 | if (state->curframe) { |
15839 | /* exit from nested function */ | |
c08435ec | 15840 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
15841 | if (err) |
15842 | return err; | |
15843 | do_print_state = true; | |
15844 | continue; | |
15845 | } | |
15846 | ||
390ee7e2 AS |
15847 | err = check_return_code(env); |
15848 | if (err) | |
15849 | return err; | |
f1bca824 | 15850 | process_bpf_exit: |
0f55f9ed | 15851 | mark_verifier_state_scratched(env); |
2589726d | 15852 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 15853 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 15854 | &env->insn_idx, pop_log); |
638f5b90 AS |
15855 | if (err < 0) { |
15856 | if (err != -ENOENT) | |
15857 | return err; | |
17a52670 AS |
15858 | break; |
15859 | } else { | |
15860 | do_print_state = true; | |
15861 | continue; | |
15862 | } | |
15863 | } else { | |
c08435ec | 15864 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
15865 | if (err) |
15866 | return err; | |
15867 | } | |
15868 | } else if (class == BPF_LD) { | |
15869 | u8 mode = BPF_MODE(insn->code); | |
15870 | ||
15871 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
15872 | err = check_ld_abs(env, insn); |
15873 | if (err) | |
15874 | return err; | |
15875 | ||
17a52670 AS |
15876 | } else if (mode == BPF_IMM) { |
15877 | err = check_ld_imm(env, insn); | |
15878 | if (err) | |
15879 | return err; | |
15880 | ||
c08435ec | 15881 | env->insn_idx++; |
fe9a5ca7 | 15882 | sanitize_mark_insn_seen(env); |
17a52670 | 15883 | } else { |
61bd5218 | 15884 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
15885 | return -EINVAL; |
15886 | } | |
15887 | } else { | |
61bd5218 | 15888 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
15889 | return -EINVAL; |
15890 | } | |
15891 | ||
c08435ec | 15892 | env->insn_idx++; |
17a52670 AS |
15893 | } |
15894 | ||
15895 | return 0; | |
15896 | } | |
15897 | ||
541c3bad AN |
15898 | static int find_btf_percpu_datasec(struct btf *btf) |
15899 | { | |
15900 | const struct btf_type *t; | |
15901 | const char *tname; | |
15902 | int i, n; | |
15903 | ||
15904 | /* | |
15905 | * Both vmlinux and module each have their own ".data..percpu" | |
15906 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
15907 | * types to look at only module's own BTF types. | |
15908 | */ | |
15909 | n = btf_nr_types(btf); | |
15910 | if (btf_is_module(btf)) | |
15911 | i = btf_nr_types(btf_vmlinux); | |
15912 | else | |
15913 | i = 1; | |
15914 | ||
15915 | for(; i < n; i++) { | |
15916 | t = btf_type_by_id(btf, i); | |
15917 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
15918 | continue; | |
15919 | ||
15920 | tname = btf_name_by_offset(btf, t->name_off); | |
15921 | if (!strcmp(tname, ".data..percpu")) | |
15922 | return i; | |
15923 | } | |
15924 | ||
15925 | return -ENOENT; | |
15926 | } | |
15927 | ||
4976b718 HL |
15928 | /* replace pseudo btf_id with kernel symbol address */ |
15929 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
15930 | struct bpf_insn *insn, | |
15931 | struct bpf_insn_aux_data *aux) | |
15932 | { | |
eaa6bcb7 HL |
15933 | const struct btf_var_secinfo *vsi; |
15934 | const struct btf_type *datasec; | |
541c3bad | 15935 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
15936 | const struct btf_type *t; |
15937 | const char *sym_name; | |
eaa6bcb7 | 15938 | bool percpu = false; |
f16e6313 | 15939 | u32 type, id = insn->imm; |
541c3bad | 15940 | struct btf *btf; |
f16e6313 | 15941 | s32 datasec_id; |
4976b718 | 15942 | u64 addr; |
541c3bad | 15943 | int i, btf_fd, err; |
4976b718 | 15944 | |
541c3bad AN |
15945 | btf_fd = insn[1].imm; |
15946 | if (btf_fd) { | |
15947 | btf = btf_get_by_fd(btf_fd); | |
15948 | if (IS_ERR(btf)) { | |
15949 | verbose(env, "invalid module BTF object FD specified.\n"); | |
15950 | return -EINVAL; | |
15951 | } | |
15952 | } else { | |
15953 | if (!btf_vmlinux) { | |
15954 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
15955 | return -EINVAL; | |
15956 | } | |
15957 | btf = btf_vmlinux; | |
15958 | btf_get(btf); | |
4976b718 HL |
15959 | } |
15960 | ||
541c3bad | 15961 | t = btf_type_by_id(btf, id); |
4976b718 HL |
15962 | if (!t) { |
15963 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
15964 | err = -ENOENT; |
15965 | goto err_put; | |
4976b718 HL |
15966 | } |
15967 | ||
58aa2afb AS |
15968 | if (!btf_type_is_var(t) && !btf_type_is_func(t)) { |
15969 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR or KIND_FUNC\n", id); | |
541c3bad AN |
15970 | err = -EINVAL; |
15971 | goto err_put; | |
4976b718 HL |
15972 | } |
15973 | ||
541c3bad | 15974 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
15975 | addr = kallsyms_lookup_name(sym_name); |
15976 | if (!addr) { | |
15977 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
15978 | sym_name); | |
541c3bad AN |
15979 | err = -ENOENT; |
15980 | goto err_put; | |
4976b718 | 15981 | } |
58aa2afb AS |
15982 | insn[0].imm = (u32)addr; |
15983 | insn[1].imm = addr >> 32; | |
15984 | ||
15985 | if (btf_type_is_func(t)) { | |
15986 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; | |
15987 | aux->btf_var.mem_size = 0; | |
15988 | goto check_btf; | |
15989 | } | |
4976b718 | 15990 | |
541c3bad | 15991 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 15992 | if (datasec_id > 0) { |
541c3bad | 15993 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
15994 | for_each_vsi(i, datasec, vsi) { |
15995 | if (vsi->type == id) { | |
15996 | percpu = true; | |
15997 | break; | |
15998 | } | |
15999 | } | |
16000 | } | |
16001 | ||
4976b718 | 16002 | type = t->type; |
541c3bad | 16003 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 | 16004 | if (percpu) { |
5844101a | 16005 | aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; |
541c3bad | 16006 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
16007 | aux->btf_var.btf_id = type; |
16008 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
16009 | const struct btf_type *ret; |
16010 | const char *tname; | |
16011 | u32 tsize; | |
16012 | ||
16013 | /* resolve the type size of ksym. */ | |
541c3bad | 16014 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 16015 | if (IS_ERR(ret)) { |
541c3bad | 16016 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
16017 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
16018 | tname, PTR_ERR(ret)); | |
541c3bad AN |
16019 | err = -EINVAL; |
16020 | goto err_put; | |
4976b718 | 16021 | } |
34d3a78c | 16022 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
4976b718 HL |
16023 | aux->btf_var.mem_size = tsize; |
16024 | } else { | |
16025 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 16026 | aux->btf_var.btf = btf; |
4976b718 HL |
16027 | aux->btf_var.btf_id = type; |
16028 | } | |
58aa2afb | 16029 | check_btf: |
541c3bad AN |
16030 | /* check whether we recorded this BTF (and maybe module) already */ |
16031 | for (i = 0; i < env->used_btf_cnt; i++) { | |
16032 | if (env->used_btfs[i].btf == btf) { | |
16033 | btf_put(btf); | |
16034 | return 0; | |
16035 | } | |
16036 | } | |
16037 | ||
16038 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
16039 | err = -E2BIG; | |
16040 | goto err_put; | |
16041 | } | |
16042 | ||
16043 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
16044 | btf_mod->btf = btf; | |
16045 | btf_mod->module = NULL; | |
16046 | ||
16047 | /* if we reference variables from kernel module, bump its refcount */ | |
16048 | if (btf_is_module(btf)) { | |
16049 | btf_mod->module = btf_try_get_module(btf); | |
16050 | if (!btf_mod->module) { | |
16051 | err = -ENXIO; | |
16052 | goto err_put; | |
16053 | } | |
16054 | } | |
16055 | ||
16056 | env->used_btf_cnt++; | |
16057 | ||
4976b718 | 16058 | return 0; |
541c3bad AN |
16059 | err_put: |
16060 | btf_put(btf); | |
16061 | return err; | |
4976b718 HL |
16062 | } |
16063 | ||
d83525ca AS |
16064 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
16065 | { | |
16066 | switch (type) { | |
16067 | case BPF_PROG_TYPE_KPROBE: | |
16068 | case BPF_PROG_TYPE_TRACEPOINT: | |
16069 | case BPF_PROG_TYPE_PERF_EVENT: | |
16070 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
5002615a | 16071 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
d83525ca AS |
16072 | return true; |
16073 | default: | |
16074 | return false; | |
16075 | } | |
16076 | } | |
16077 | ||
61bd5218 JK |
16078 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
16079 | struct bpf_map *map, | |
fdc15d38 AS |
16080 | struct bpf_prog *prog) |
16081 | ||
16082 | { | |
7e40781c | 16083 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
a3884572 | 16084 | |
9c395c1b DM |
16085 | if (btf_record_has_field(map->record, BPF_LIST_HEAD) || |
16086 | btf_record_has_field(map->record, BPF_RB_ROOT)) { | |
f0c5941f | 16087 | if (is_tracing_prog_type(prog_type)) { |
9c395c1b | 16088 | verbose(env, "tracing progs cannot use bpf_{list_head,rb_root} yet\n"); |
f0c5941f KKD |
16089 | return -EINVAL; |
16090 | } | |
16091 | } | |
16092 | ||
db559117 | 16093 | if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) { |
9e7a4d98 KS |
16094 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
16095 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
16096 | return -EINVAL; | |
16097 | } | |
16098 | ||
16099 | if (is_tracing_prog_type(prog_type)) { | |
16100 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
16101 | return -EINVAL; | |
16102 | } | |
16103 | ||
16104 | if (prog->aux->sleepable) { | |
16105 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
16106 | return -EINVAL; | |
16107 | } | |
d83525ca AS |
16108 | } |
16109 | ||
db559117 | 16110 | if (btf_record_has_field(map->record, BPF_TIMER)) { |
5e0bc308 DB |
16111 | if (is_tracing_prog_type(prog_type)) { |
16112 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
16113 | return -EINVAL; | |
16114 | } | |
16115 | } | |
16116 | ||
9d03ebc7 | 16117 | if ((bpf_prog_is_offloaded(prog->aux) || bpf_map_is_offloaded(map)) && |
09728266 | 16118 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
16119 | verbose(env, "offload device mismatch between prog and map\n"); |
16120 | return -EINVAL; | |
16121 | } | |
16122 | ||
85d33df3 MKL |
16123 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
16124 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
16125 | return -EINVAL; | |
16126 | } | |
16127 | ||
1e6c62a8 AS |
16128 | if (prog->aux->sleepable) |
16129 | switch (map->map_type) { | |
16130 | case BPF_MAP_TYPE_HASH: | |
16131 | case BPF_MAP_TYPE_LRU_HASH: | |
16132 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
16133 | case BPF_MAP_TYPE_PERCPU_HASH: |
16134 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
16135 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
16136 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
16137 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
ba90c2cc | 16138 | case BPF_MAP_TYPE_RINGBUF: |
583c1f42 | 16139 | case BPF_MAP_TYPE_USER_RINGBUF: |
0fe4b381 KS |
16140 | case BPF_MAP_TYPE_INODE_STORAGE: |
16141 | case BPF_MAP_TYPE_SK_STORAGE: | |
16142 | case BPF_MAP_TYPE_TASK_STORAGE: | |
2c40d97d | 16143 | case BPF_MAP_TYPE_CGRP_STORAGE: |
ba90c2cc | 16144 | break; |
1e6c62a8 AS |
16145 | default: |
16146 | verbose(env, | |
2c40d97d | 16147 | "Sleepable programs can only use array, hash, ringbuf and local storage maps\n"); |
1e6c62a8 AS |
16148 | return -EINVAL; |
16149 | } | |
16150 | ||
fdc15d38 AS |
16151 | return 0; |
16152 | } | |
16153 | ||
b741f163 RG |
16154 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
16155 | { | |
16156 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
16157 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
16158 | } | |
16159 | ||
4976b718 HL |
16160 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
16161 | * | |
16162 | * 1. if it accesses map FD, replace it with actual map pointer. | |
16163 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
16164 | * | |
16165 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 16166 | */ |
4976b718 | 16167 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
16168 | { |
16169 | struct bpf_insn *insn = env->prog->insnsi; | |
16170 | int insn_cnt = env->prog->len; | |
fdc15d38 | 16171 | int i, j, err; |
0246e64d | 16172 | |
f1f7714e | 16173 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
16174 | if (err) |
16175 | return err; | |
16176 | ||
0246e64d | 16177 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 16178 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 16179 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 16180 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
16181 | return -EINVAL; |
16182 | } | |
16183 | ||
0246e64d | 16184 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 16185 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
16186 | struct bpf_map *map; |
16187 | struct fd f; | |
d8eca5bb | 16188 | u64 addr; |
387544bf | 16189 | u32 fd; |
0246e64d AS |
16190 | |
16191 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
16192 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
16193 | insn[1].off != 0) { | |
61bd5218 | 16194 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
16195 | return -EINVAL; |
16196 | } | |
16197 | ||
d8eca5bb | 16198 | if (insn[0].src_reg == 0) |
0246e64d AS |
16199 | /* valid generic load 64-bit imm */ |
16200 | goto next_insn; | |
16201 | ||
4976b718 HL |
16202 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
16203 | aux = &env->insn_aux_data[i]; | |
16204 | err = check_pseudo_btf_id(env, insn, aux); | |
16205 | if (err) | |
16206 | return err; | |
16207 | goto next_insn; | |
16208 | } | |
16209 | ||
69c087ba YS |
16210 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
16211 | aux = &env->insn_aux_data[i]; | |
16212 | aux->ptr_type = PTR_TO_FUNC; | |
16213 | goto next_insn; | |
16214 | } | |
16215 | ||
d8eca5bb DB |
16216 | /* In final convert_pseudo_ld_imm64() step, this is |
16217 | * converted into regular 64-bit imm load insn. | |
16218 | */ | |
387544bf AS |
16219 | switch (insn[0].src_reg) { |
16220 | case BPF_PSEUDO_MAP_VALUE: | |
16221 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
16222 | break; | |
16223 | case BPF_PSEUDO_MAP_FD: | |
16224 | case BPF_PSEUDO_MAP_IDX: | |
16225 | if (insn[1].imm == 0) | |
16226 | break; | |
16227 | fallthrough; | |
16228 | default: | |
16229 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
16230 | return -EINVAL; |
16231 | } | |
16232 | ||
387544bf AS |
16233 | switch (insn[0].src_reg) { |
16234 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
16235 | case BPF_PSEUDO_MAP_IDX: | |
16236 | if (bpfptr_is_null(env->fd_array)) { | |
16237 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
16238 | return -EPROTO; | |
16239 | } | |
16240 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
16241 | insn[0].imm * sizeof(fd), | |
16242 | sizeof(fd))) | |
16243 | return -EFAULT; | |
16244 | break; | |
16245 | default: | |
16246 | fd = insn[0].imm; | |
16247 | break; | |
16248 | } | |
16249 | ||
16250 | f = fdget(fd); | |
c2101297 | 16251 | map = __bpf_map_get(f); |
0246e64d | 16252 | if (IS_ERR(map)) { |
61bd5218 | 16253 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 16254 | insn[0].imm); |
0246e64d AS |
16255 | return PTR_ERR(map); |
16256 | } | |
16257 | ||
61bd5218 | 16258 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
16259 | if (err) { |
16260 | fdput(f); | |
16261 | return err; | |
16262 | } | |
16263 | ||
d8eca5bb | 16264 | aux = &env->insn_aux_data[i]; |
387544bf AS |
16265 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
16266 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
16267 | addr = (unsigned long)map; |
16268 | } else { | |
16269 | u32 off = insn[1].imm; | |
16270 | ||
16271 | if (off >= BPF_MAX_VAR_OFF) { | |
16272 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
16273 | fdput(f); | |
16274 | return -EINVAL; | |
16275 | } | |
16276 | ||
16277 | if (!map->ops->map_direct_value_addr) { | |
16278 | verbose(env, "no direct value access support for this map type\n"); | |
16279 | fdput(f); | |
16280 | return -EINVAL; | |
16281 | } | |
16282 | ||
16283 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
16284 | if (err) { | |
16285 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
16286 | map->value_size, off); | |
16287 | fdput(f); | |
16288 | return err; | |
16289 | } | |
16290 | ||
16291 | aux->map_off = off; | |
16292 | addr += off; | |
16293 | } | |
16294 | ||
16295 | insn[0].imm = (u32)addr; | |
16296 | insn[1].imm = addr >> 32; | |
0246e64d AS |
16297 | |
16298 | /* check whether we recorded this map already */ | |
d8eca5bb | 16299 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 16300 | if (env->used_maps[j] == map) { |
d8eca5bb | 16301 | aux->map_index = j; |
0246e64d AS |
16302 | fdput(f); |
16303 | goto next_insn; | |
16304 | } | |
d8eca5bb | 16305 | } |
0246e64d AS |
16306 | |
16307 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
16308 | fdput(f); | |
16309 | return -E2BIG; | |
16310 | } | |
16311 | ||
0246e64d AS |
16312 | /* hold the map. If the program is rejected by verifier, |
16313 | * the map will be released by release_maps() or it | |
16314 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 16315 | * and all maps are released in free_used_maps() |
0246e64d | 16316 | */ |
1e0bd5a0 | 16317 | bpf_map_inc(map); |
d8eca5bb DB |
16318 | |
16319 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
16320 | env->used_maps[env->used_map_cnt++] = map; |
16321 | ||
b741f163 | 16322 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 16323 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 16324 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
16325 | fdput(f); |
16326 | return -EBUSY; | |
16327 | } | |
16328 | ||
0246e64d AS |
16329 | fdput(f); |
16330 | next_insn: | |
16331 | insn++; | |
16332 | i++; | |
5e581dad DB |
16333 | continue; |
16334 | } | |
16335 | ||
16336 | /* Basic sanity check before we invest more work here. */ | |
16337 | if (!bpf_opcode_in_insntable(insn->code)) { | |
16338 | verbose(env, "unknown opcode %02x\n", insn->code); | |
16339 | return -EINVAL; | |
0246e64d AS |
16340 | } |
16341 | } | |
16342 | ||
16343 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
16344 | * 'struct bpf_map *' into a register instead of user map_fd. | |
16345 | * These pointers will be used later by verifier to validate map access. | |
16346 | */ | |
16347 | return 0; | |
16348 | } | |
16349 | ||
16350 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 16351 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 16352 | { |
a2ea0746 DB |
16353 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
16354 | env->used_map_cnt); | |
0246e64d AS |
16355 | } |
16356 | ||
541c3bad AN |
16357 | /* drop refcnt of maps used by the rejected program */ |
16358 | static void release_btfs(struct bpf_verifier_env *env) | |
16359 | { | |
16360 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
16361 | env->used_btf_cnt); | |
16362 | } | |
16363 | ||
0246e64d | 16364 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 16365 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
16366 | { |
16367 | struct bpf_insn *insn = env->prog->insnsi; | |
16368 | int insn_cnt = env->prog->len; | |
16369 | int i; | |
16370 | ||
69c087ba YS |
16371 | for (i = 0; i < insn_cnt; i++, insn++) { |
16372 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
16373 | continue; | |
16374 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
16375 | continue; | |
16376 | insn->src_reg = 0; | |
16377 | } | |
0246e64d AS |
16378 | } |
16379 | ||
8041902d AS |
16380 | /* single env->prog->insni[off] instruction was replaced with the range |
16381 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
16382 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
16383 | */ | |
75f0fc7b HF |
16384 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
16385 | struct bpf_insn_aux_data *new_data, | |
16386 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 16387 | { |
75f0fc7b | 16388 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 16389 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 16390 | u32 old_seen = old_data[off].seen; |
b325fbca | 16391 | u32 prog_len; |
c131187d | 16392 | int i; |
8041902d | 16393 | |
b325fbca JW |
16394 | /* aux info at OFF always needs adjustment, no matter fast path |
16395 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
16396 | * original insn at old prog. | |
16397 | */ | |
16398 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
16399 | ||
8041902d | 16400 | if (cnt == 1) |
75f0fc7b | 16401 | return; |
b325fbca | 16402 | prog_len = new_prog->len; |
75f0fc7b | 16403 | |
8041902d AS |
16404 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
16405 | memcpy(new_data + off + cnt - 1, old_data + off, | |
16406 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 16407 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
16408 | /* Expand insni[off]'s seen count to the patched range. */ |
16409 | new_data[i].seen = old_seen; | |
b325fbca JW |
16410 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
16411 | } | |
8041902d AS |
16412 | env->insn_aux_data = new_data; |
16413 | vfree(old_data); | |
8041902d AS |
16414 | } |
16415 | ||
cc8b0b92 AS |
16416 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
16417 | { | |
16418 | int i; | |
16419 | ||
16420 | if (len == 1) | |
16421 | return; | |
4cb3d99c JW |
16422 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
16423 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 16424 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 16425 | continue; |
9c8105bd | 16426 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
16427 | } |
16428 | } | |
16429 | ||
7506d211 | 16430 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
16431 | { |
16432 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
16433 | int i, sz = prog->aux->size_poke_tab; | |
16434 | struct bpf_jit_poke_descriptor *desc; | |
16435 | ||
16436 | for (i = 0; i < sz; i++) { | |
16437 | desc = &tab[i]; | |
7506d211 JF |
16438 | if (desc->insn_idx <= off) |
16439 | continue; | |
a748c697 MF |
16440 | desc->insn_idx += len - 1; |
16441 | } | |
16442 | } | |
16443 | ||
8041902d AS |
16444 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
16445 | const struct bpf_insn *patch, u32 len) | |
16446 | { | |
16447 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
16448 | struct bpf_insn_aux_data *new_data = NULL; |
16449 | ||
16450 | if (len > 1) { | |
16451 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
16452 | sizeof(struct bpf_insn_aux_data))); | |
16453 | if (!new_data) | |
16454 | return NULL; | |
16455 | } | |
8041902d AS |
16456 | |
16457 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
16458 | if (IS_ERR(new_prog)) { |
16459 | if (PTR_ERR(new_prog) == -ERANGE) | |
16460 | verbose(env, | |
16461 | "insn %d cannot be patched due to 16-bit range\n", | |
16462 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 16463 | vfree(new_data); |
8041902d | 16464 | return NULL; |
4f73379e | 16465 | } |
75f0fc7b | 16466 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 16467 | adjust_subprog_starts(env, off, len); |
7506d211 | 16468 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
16469 | return new_prog; |
16470 | } | |
16471 | ||
52875a04 JK |
16472 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
16473 | u32 off, u32 cnt) | |
16474 | { | |
16475 | int i, j; | |
16476 | ||
16477 | /* find first prog starting at or after off (first to remove) */ | |
16478 | for (i = 0; i < env->subprog_cnt; i++) | |
16479 | if (env->subprog_info[i].start >= off) | |
16480 | break; | |
16481 | /* find first prog starting at or after off + cnt (first to stay) */ | |
16482 | for (j = i; j < env->subprog_cnt; j++) | |
16483 | if (env->subprog_info[j].start >= off + cnt) | |
16484 | break; | |
16485 | /* if j doesn't start exactly at off + cnt, we are just removing | |
16486 | * the front of previous prog | |
16487 | */ | |
16488 | if (env->subprog_info[j].start != off + cnt) | |
16489 | j--; | |
16490 | ||
16491 | if (j > i) { | |
16492 | struct bpf_prog_aux *aux = env->prog->aux; | |
16493 | int move; | |
16494 | ||
16495 | /* move fake 'exit' subprog as well */ | |
16496 | move = env->subprog_cnt + 1 - j; | |
16497 | ||
16498 | memmove(env->subprog_info + i, | |
16499 | env->subprog_info + j, | |
16500 | sizeof(*env->subprog_info) * move); | |
16501 | env->subprog_cnt -= j - i; | |
16502 | ||
16503 | /* remove func_info */ | |
16504 | if (aux->func_info) { | |
16505 | move = aux->func_info_cnt - j; | |
16506 | ||
16507 | memmove(aux->func_info + i, | |
16508 | aux->func_info + j, | |
16509 | sizeof(*aux->func_info) * move); | |
16510 | aux->func_info_cnt -= j - i; | |
16511 | /* func_info->insn_off is set after all code rewrites, | |
16512 | * in adjust_btf_func() - no need to adjust | |
16513 | */ | |
16514 | } | |
16515 | } else { | |
16516 | /* convert i from "first prog to remove" to "first to adjust" */ | |
16517 | if (env->subprog_info[i].start == off) | |
16518 | i++; | |
16519 | } | |
16520 | ||
16521 | /* update fake 'exit' subprog as well */ | |
16522 | for (; i <= env->subprog_cnt; i++) | |
16523 | env->subprog_info[i].start -= cnt; | |
16524 | ||
16525 | return 0; | |
16526 | } | |
16527 | ||
16528 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
16529 | u32 cnt) | |
16530 | { | |
16531 | struct bpf_prog *prog = env->prog; | |
16532 | u32 i, l_off, l_cnt, nr_linfo; | |
16533 | struct bpf_line_info *linfo; | |
16534 | ||
16535 | nr_linfo = prog->aux->nr_linfo; | |
16536 | if (!nr_linfo) | |
16537 | return 0; | |
16538 | ||
16539 | linfo = prog->aux->linfo; | |
16540 | ||
16541 | /* find first line info to remove, count lines to be removed */ | |
16542 | for (i = 0; i < nr_linfo; i++) | |
16543 | if (linfo[i].insn_off >= off) | |
16544 | break; | |
16545 | ||
16546 | l_off = i; | |
16547 | l_cnt = 0; | |
16548 | for (; i < nr_linfo; i++) | |
16549 | if (linfo[i].insn_off < off + cnt) | |
16550 | l_cnt++; | |
16551 | else | |
16552 | break; | |
16553 | ||
16554 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
16555 | * last removed linfo. prog is already modified, so prog->len == off | |
16556 | * means no live instructions after (tail of the program was removed). | |
16557 | */ | |
16558 | if (prog->len != off && l_cnt && | |
16559 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
16560 | l_cnt--; | |
16561 | linfo[--i].insn_off = off + cnt; | |
16562 | } | |
16563 | ||
16564 | /* remove the line info which refer to the removed instructions */ | |
16565 | if (l_cnt) { | |
16566 | memmove(linfo + l_off, linfo + i, | |
16567 | sizeof(*linfo) * (nr_linfo - i)); | |
16568 | ||
16569 | prog->aux->nr_linfo -= l_cnt; | |
16570 | nr_linfo = prog->aux->nr_linfo; | |
16571 | } | |
16572 | ||
16573 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
16574 | for (i = l_off; i < nr_linfo; i++) | |
16575 | linfo[i].insn_off -= cnt; | |
16576 | ||
16577 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
16578 | for (i = 0; i <= env->subprog_cnt; i++) | |
16579 | if (env->subprog_info[i].linfo_idx > l_off) { | |
16580 | /* program may have started in the removed region but | |
16581 | * may not be fully removed | |
16582 | */ | |
16583 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
16584 | env->subprog_info[i].linfo_idx -= l_cnt; | |
16585 | else | |
16586 | env->subprog_info[i].linfo_idx = l_off; | |
16587 | } | |
16588 | ||
16589 | return 0; | |
16590 | } | |
16591 | ||
16592 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
16593 | { | |
16594 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16595 | unsigned int orig_prog_len = env->prog->len; | |
16596 | int err; | |
16597 | ||
9d03ebc7 | 16598 | if (bpf_prog_is_offloaded(env->prog->aux)) |
08ca90af JK |
16599 | bpf_prog_offload_remove_insns(env, off, cnt); |
16600 | ||
52875a04 JK |
16601 | err = bpf_remove_insns(env->prog, off, cnt); |
16602 | if (err) | |
16603 | return err; | |
16604 | ||
16605 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
16606 | if (err) | |
16607 | return err; | |
16608 | ||
16609 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
16610 | if (err) | |
16611 | return err; | |
16612 | ||
16613 | memmove(aux_data + off, aux_data + off + cnt, | |
16614 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
16615 | ||
16616 | return 0; | |
16617 | } | |
16618 | ||
2a5418a1 DB |
16619 | /* The verifier does more data flow analysis than llvm and will not |
16620 | * explore branches that are dead at run time. Malicious programs can | |
16621 | * have dead code too. Therefore replace all dead at-run-time code | |
16622 | * with 'ja -1'. | |
16623 | * | |
16624 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
16625 | * program and through another bug we would manage to jump there, then | |
16626 | * we'd execute beyond program memory otherwise. Returning exception | |
16627 | * code also wouldn't work since we can have subprogs where the dead | |
16628 | * code could be located. | |
c131187d AS |
16629 | */ |
16630 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
16631 | { | |
16632 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 16633 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
16634 | struct bpf_insn *insn = env->prog->insnsi; |
16635 | const int insn_cnt = env->prog->len; | |
16636 | int i; | |
16637 | ||
16638 | for (i = 0; i < insn_cnt; i++) { | |
16639 | if (aux_data[i].seen) | |
16640 | continue; | |
2a5418a1 | 16641 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 16642 | aux_data[i].zext_dst = false; |
c131187d AS |
16643 | } |
16644 | } | |
16645 | ||
e2ae4ca2 JK |
16646 | static bool insn_is_cond_jump(u8 code) |
16647 | { | |
16648 | u8 op; | |
16649 | ||
092ed096 JW |
16650 | if (BPF_CLASS(code) == BPF_JMP32) |
16651 | return true; | |
16652 | ||
e2ae4ca2 JK |
16653 | if (BPF_CLASS(code) != BPF_JMP) |
16654 | return false; | |
16655 | ||
16656 | op = BPF_OP(code); | |
16657 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
16658 | } | |
16659 | ||
16660 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
16661 | { | |
16662 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16663 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
16664 | struct bpf_insn *insn = env->prog->insnsi; | |
16665 | const int insn_cnt = env->prog->len; | |
16666 | int i; | |
16667 | ||
16668 | for (i = 0; i < insn_cnt; i++, insn++) { | |
16669 | if (!insn_is_cond_jump(insn->code)) | |
16670 | continue; | |
16671 | ||
16672 | if (!aux_data[i + 1].seen) | |
16673 | ja.off = insn->off; | |
16674 | else if (!aux_data[i + 1 + insn->off].seen) | |
16675 | ja.off = 0; | |
16676 | else | |
16677 | continue; | |
16678 | ||
9d03ebc7 | 16679 | if (bpf_prog_is_offloaded(env->prog->aux)) |
08ca90af JK |
16680 | bpf_prog_offload_replace_insn(env, i, &ja); |
16681 | ||
e2ae4ca2 JK |
16682 | memcpy(insn, &ja, sizeof(ja)); |
16683 | } | |
16684 | } | |
16685 | ||
52875a04 JK |
16686 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
16687 | { | |
16688 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16689 | int insn_cnt = env->prog->len; | |
16690 | int i, err; | |
16691 | ||
16692 | for (i = 0; i < insn_cnt; i++) { | |
16693 | int j; | |
16694 | ||
16695 | j = 0; | |
16696 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
16697 | j++; | |
16698 | if (!j) | |
16699 | continue; | |
16700 | ||
16701 | err = verifier_remove_insns(env, i, j); | |
16702 | if (err) | |
16703 | return err; | |
16704 | insn_cnt = env->prog->len; | |
16705 | } | |
16706 | ||
16707 | return 0; | |
16708 | } | |
16709 | ||
a1b14abc JK |
16710 | static int opt_remove_nops(struct bpf_verifier_env *env) |
16711 | { | |
16712 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
16713 | struct bpf_insn *insn = env->prog->insnsi; | |
16714 | int insn_cnt = env->prog->len; | |
16715 | int i, err; | |
16716 | ||
16717 | for (i = 0; i < insn_cnt; i++) { | |
16718 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
16719 | continue; | |
16720 | ||
16721 | err = verifier_remove_insns(env, i, 1); | |
16722 | if (err) | |
16723 | return err; | |
16724 | insn_cnt--; | |
16725 | i--; | |
16726 | } | |
16727 | ||
16728 | return 0; | |
16729 | } | |
16730 | ||
d6c2308c JW |
16731 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
16732 | const union bpf_attr *attr) | |
a4b1d3c1 | 16733 | { |
d6c2308c | 16734 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 16735 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 16736 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 16737 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 16738 | struct bpf_prog *new_prog; |
d6c2308c | 16739 | bool rnd_hi32; |
a4b1d3c1 | 16740 | |
d6c2308c | 16741 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 16742 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
16743 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
16744 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
16745 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
16746 | for (i = 0; i < len; i++) { |
16747 | int adj_idx = i + delta; | |
16748 | struct bpf_insn insn; | |
83a28819 | 16749 | int load_reg; |
a4b1d3c1 | 16750 | |
d6c2308c | 16751 | insn = insns[adj_idx]; |
83a28819 | 16752 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
16753 | if (!aux[adj_idx].zext_dst) { |
16754 | u8 code, class; | |
16755 | u32 imm_rnd; | |
16756 | ||
16757 | if (!rnd_hi32) | |
16758 | continue; | |
16759 | ||
16760 | code = insn.code; | |
16761 | class = BPF_CLASS(code); | |
83a28819 | 16762 | if (load_reg == -1) |
d6c2308c JW |
16763 | continue; |
16764 | ||
16765 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
16766 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
16767 | * here. | |
d6c2308c | 16768 | */ |
83a28819 | 16769 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
16770 | if (class == BPF_LD && |
16771 | BPF_MODE(code) == BPF_IMM) | |
16772 | i++; | |
16773 | continue; | |
16774 | } | |
16775 | ||
16776 | /* ctx load could be transformed into wider load. */ | |
16777 | if (class == BPF_LDX && | |
16778 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
16779 | continue; | |
16780 | ||
a251c17a | 16781 | imm_rnd = get_random_u32(); |
d6c2308c JW |
16782 | rnd_hi32_patch[0] = insn; |
16783 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 16784 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
16785 | patch = rnd_hi32_patch; |
16786 | patch_len = 4; | |
16787 | goto apply_patch_buffer; | |
16788 | } | |
16789 | ||
39491867 BJ |
16790 | /* Add in an zero-extend instruction if a) the JIT has requested |
16791 | * it or b) it's a CMPXCHG. | |
16792 | * | |
16793 | * The latter is because: BPF_CMPXCHG always loads a value into | |
16794 | * R0, therefore always zero-extends. However some archs' | |
16795 | * equivalent instruction only does this load when the | |
16796 | * comparison is successful. This detail of CMPXCHG is | |
16797 | * orthogonal to the general zero-extension behaviour of the | |
16798 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
16799 | */ | |
16800 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
16801 | continue; |
16802 | ||
d35af0a7 BT |
16803 | /* Zero-extension is done by the caller. */ |
16804 | if (bpf_pseudo_kfunc_call(&insn)) | |
16805 | continue; | |
16806 | ||
83a28819 IL |
16807 | if (WARN_ON(load_reg == -1)) { |
16808 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
16809 | return -EFAULT; | |
b2e37a71 IL |
16810 | } |
16811 | ||
a4b1d3c1 | 16812 | zext_patch[0] = insn; |
b2e37a71 IL |
16813 | zext_patch[1].dst_reg = load_reg; |
16814 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
16815 | patch = zext_patch; |
16816 | patch_len = 2; | |
16817 | apply_patch_buffer: | |
16818 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
16819 | if (!new_prog) |
16820 | return -ENOMEM; | |
16821 | env->prog = new_prog; | |
16822 | insns = new_prog->insnsi; | |
16823 | aux = env->insn_aux_data; | |
d6c2308c | 16824 | delta += patch_len - 1; |
a4b1d3c1 JW |
16825 | } |
16826 | ||
16827 | return 0; | |
16828 | } | |
16829 | ||
c64b7983 JS |
16830 | /* convert load instructions that access fields of a context type into a |
16831 | * sequence of instructions that access fields of the underlying structure: | |
16832 | * struct __sk_buff -> struct sk_buff | |
16833 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 16834 | */ |
58e2af8b | 16835 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 16836 | { |
00176a34 | 16837 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 16838 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 16839 | const int insn_cnt = env->prog->len; |
36bbef52 | 16840 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 16841 | u32 target_size, size_default, off; |
9bac3d6d | 16842 | struct bpf_prog *new_prog; |
d691f9e8 | 16843 | enum bpf_access_type type; |
f96da094 | 16844 | bool is_narrower_load; |
9bac3d6d | 16845 | |
b09928b9 DB |
16846 | if (ops->gen_prologue || env->seen_direct_write) { |
16847 | if (!ops->gen_prologue) { | |
16848 | verbose(env, "bpf verifier is misconfigured\n"); | |
16849 | return -EINVAL; | |
16850 | } | |
36bbef52 DB |
16851 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
16852 | env->prog); | |
16853 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 16854 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
16855 | return -EINVAL; |
16856 | } else if (cnt) { | |
8041902d | 16857 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
16858 | if (!new_prog) |
16859 | return -ENOMEM; | |
8041902d | 16860 | |
36bbef52 | 16861 | env->prog = new_prog; |
3df126f3 | 16862 | delta += cnt - 1; |
36bbef52 DB |
16863 | } |
16864 | } | |
16865 | ||
9d03ebc7 | 16866 | if (bpf_prog_is_offloaded(env->prog->aux)) |
9bac3d6d AS |
16867 | return 0; |
16868 | ||
3df126f3 | 16869 | insn = env->prog->insnsi + delta; |
36bbef52 | 16870 | |
9bac3d6d | 16871 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
16872 | bpf_convert_ctx_access_t convert_ctx_access; |
16873 | ||
62c7989b DB |
16874 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
16875 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
16876 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 16877 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 16878 | type = BPF_READ; |
2039f26f DB |
16879 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
16880 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
16881 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
16882 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
16883 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
16884 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
16885 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
16886 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 16887 | type = BPF_WRITE; |
2039f26f | 16888 | } else { |
9bac3d6d | 16889 | continue; |
2039f26f | 16890 | } |
9bac3d6d | 16891 | |
af86ca4e | 16892 | if (type == BPF_WRITE && |
2039f26f | 16893 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 16894 | struct bpf_insn patch[] = { |
af86ca4e | 16895 | *insn, |
2039f26f | 16896 | BPF_ST_NOSPEC(), |
af86ca4e AS |
16897 | }; |
16898 | ||
16899 | cnt = ARRAY_SIZE(patch); | |
16900 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
16901 | if (!new_prog) | |
16902 | return -ENOMEM; | |
16903 | ||
16904 | delta += cnt - 1; | |
16905 | env->prog = new_prog; | |
16906 | insn = new_prog->insnsi + i + delta; | |
16907 | continue; | |
16908 | } | |
16909 | ||
6efe152d | 16910 | switch ((int)env->insn_aux_data[i + delta].ptr_type) { |
c64b7983 JS |
16911 | case PTR_TO_CTX: |
16912 | if (!ops->convert_ctx_access) | |
16913 | continue; | |
16914 | convert_ctx_access = ops->convert_ctx_access; | |
16915 | break; | |
16916 | case PTR_TO_SOCKET: | |
46f8bc92 | 16917 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
16918 | convert_ctx_access = bpf_sock_convert_ctx_access; |
16919 | break; | |
655a51e5 MKL |
16920 | case PTR_TO_TCP_SOCK: |
16921 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
16922 | break; | |
fada7fdc JL |
16923 | case PTR_TO_XDP_SOCK: |
16924 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
16925 | break; | |
2a02759e | 16926 | case PTR_TO_BTF_ID: |
6efe152d | 16927 | case PTR_TO_BTF_ID | PTR_UNTRUSTED: |
282de143 KKD |
16928 | /* PTR_TO_BTF_ID | MEM_ALLOC always has a valid lifetime, unlike |
16929 | * PTR_TO_BTF_ID, and an active ref_obj_id, but the same cannot | |
16930 | * be said once it is marked PTR_UNTRUSTED, hence we must handle | |
16931 | * any faults for loads into such types. BPF_WRITE is disallowed | |
16932 | * for this case. | |
16933 | */ | |
16934 | case PTR_TO_BTF_ID | MEM_ALLOC | PTR_UNTRUSTED: | |
27ae7997 MKL |
16935 | if (type == BPF_READ) { |
16936 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
16937 | BPF_SIZE((insn)->code); | |
16938 | env->prog->aux->num_exentries++; | |
2a02759e | 16939 | } |
2a02759e | 16940 | continue; |
c64b7983 | 16941 | default: |
9bac3d6d | 16942 | continue; |
c64b7983 | 16943 | } |
9bac3d6d | 16944 | |
31fd8581 | 16945 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 16946 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
16947 | |
16948 | /* If the read access is a narrower load of the field, | |
16949 | * convert to a 4/8-byte load, to minimum program type specific | |
16950 | * convert_ctx_access changes. If conversion is successful, | |
16951 | * we will apply proper mask to the result. | |
16952 | */ | |
f96da094 | 16953 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
16954 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
16955 | off = insn->off; | |
31fd8581 | 16956 | if (is_narrower_load) { |
f96da094 DB |
16957 | u8 size_code; |
16958 | ||
16959 | if (type == BPF_WRITE) { | |
61bd5218 | 16960 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
16961 | return -EINVAL; |
16962 | } | |
31fd8581 | 16963 | |
f96da094 | 16964 | size_code = BPF_H; |
31fd8581 YS |
16965 | if (ctx_field_size == 4) |
16966 | size_code = BPF_W; | |
16967 | else if (ctx_field_size == 8) | |
16968 | size_code = BPF_DW; | |
f96da094 | 16969 | |
bc23105c | 16970 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
16971 | insn->code = BPF_LDX | BPF_MEM | size_code; |
16972 | } | |
f96da094 DB |
16973 | |
16974 | target_size = 0; | |
c64b7983 JS |
16975 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
16976 | &target_size); | |
f96da094 DB |
16977 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
16978 | (ctx_field_size && !target_size)) { | |
61bd5218 | 16979 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
16980 | return -EINVAL; |
16981 | } | |
f96da094 DB |
16982 | |
16983 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
16984 | u8 shift = bpf_ctx_narrow_access_offset( |
16985 | off, size, size_default) * 8; | |
d7af7e49 AI |
16986 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
16987 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
16988 | return -EINVAL; | |
16989 | } | |
46f53a65 AI |
16990 | if (ctx_field_size <= 4) { |
16991 | if (shift) | |
16992 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
16993 | insn->dst_reg, | |
16994 | shift); | |
31fd8581 | 16995 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 16996 | (1 << size * 8) - 1); |
46f53a65 AI |
16997 | } else { |
16998 | if (shift) | |
16999 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
17000 | insn->dst_reg, | |
17001 | shift); | |
31fd8581 | 17002 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 17003 | (1ULL << size * 8) - 1); |
46f53a65 | 17004 | } |
31fd8581 | 17005 | } |
9bac3d6d | 17006 | |
8041902d | 17007 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
17008 | if (!new_prog) |
17009 | return -ENOMEM; | |
17010 | ||
3df126f3 | 17011 | delta += cnt - 1; |
9bac3d6d AS |
17012 | |
17013 | /* keep walking new program and skip insns we just inserted */ | |
17014 | env->prog = new_prog; | |
3df126f3 | 17015 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
17016 | } |
17017 | ||
17018 | return 0; | |
17019 | } | |
17020 | ||
1c2a088a AS |
17021 | static int jit_subprogs(struct bpf_verifier_env *env) |
17022 | { | |
17023 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
17024 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 17025 | struct bpf_map *map_ptr; |
7105e828 | 17026 | struct bpf_insn *insn; |
1c2a088a | 17027 | void *old_bpf_func; |
c4c0bdc0 | 17028 | int err, num_exentries; |
1c2a088a | 17029 | |
f910cefa | 17030 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
17031 | return 0; |
17032 | ||
7105e828 | 17033 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 17034 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 17035 | continue; |
69c087ba | 17036 | |
c7a89784 DB |
17037 | /* Upon error here we cannot fall back to interpreter but |
17038 | * need a hard reject of the program. Thus -EFAULT is | |
17039 | * propagated in any case. | |
17040 | */ | |
1c2a088a AS |
17041 | subprog = find_subprog(env, i + insn->imm + 1); |
17042 | if (subprog < 0) { | |
17043 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
17044 | i + insn->imm + 1); | |
17045 | return -EFAULT; | |
17046 | } | |
17047 | /* temporarily remember subprog id inside insn instead of | |
17048 | * aux_data, since next loop will split up all insns into funcs | |
17049 | */ | |
f910cefa | 17050 | insn->off = subprog; |
1c2a088a AS |
17051 | /* remember original imm in case JIT fails and fallback |
17052 | * to interpreter will be needed | |
17053 | */ | |
17054 | env->insn_aux_data[i].call_imm = insn->imm; | |
17055 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
17056 | insn->imm = 1; | |
3990ed4c MKL |
17057 | if (bpf_pseudo_func(insn)) |
17058 | /* jit (e.g. x86_64) may emit fewer instructions | |
17059 | * if it learns a u32 imm is the same as a u64 imm. | |
17060 | * Force a non zero here. | |
17061 | */ | |
17062 | insn[1].imm = 1; | |
1c2a088a AS |
17063 | } |
17064 | ||
c454a46b MKL |
17065 | err = bpf_prog_alloc_jited_linfo(prog); |
17066 | if (err) | |
17067 | goto out_undo_insn; | |
17068 | ||
17069 | err = -ENOMEM; | |
6396bb22 | 17070 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 17071 | if (!func) |
c7a89784 | 17072 | goto out_undo_insn; |
1c2a088a | 17073 | |
f910cefa | 17074 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 17075 | subprog_start = subprog_end; |
4cb3d99c | 17076 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
17077 | |
17078 | len = subprog_end - subprog_start; | |
fb7dd8bc | 17079 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
17080 | * hence main prog stats include the runtime of subprogs. |
17081 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 17082 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
17083 | */ |
17084 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
17085 | if (!func[i]) |
17086 | goto out_free; | |
17087 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
17088 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 17089 | func[i]->type = prog->type; |
1c2a088a | 17090 | func[i]->len = len; |
4f74d809 DB |
17091 | if (bpf_prog_calc_tag(func[i])) |
17092 | goto out_free; | |
1c2a088a | 17093 | func[i]->is_func = 1; |
ba64e7d8 | 17094 | func[i]->aux->func_idx = i; |
f263a814 | 17095 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
17096 | func[i]->aux->btf = prog->aux->btf; |
17097 | func[i]->aux->func_info = prog->aux->func_info; | |
9c7c48d6 | 17098 | func[i]->aux->func_info_cnt = prog->aux->func_info_cnt; |
f263a814 JF |
17099 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
17100 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 17101 | |
a748c697 | 17102 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 17103 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 17104 | |
f263a814 JF |
17105 | poke = &prog->aux->poke_tab[j]; |
17106 | if (poke->insn_idx < subprog_end && | |
17107 | poke->insn_idx >= subprog_start) | |
17108 | poke->aux = func[i]->aux; | |
a748c697 MF |
17109 | } |
17110 | ||
1c2a088a | 17111 | func[i]->aux->name[0] = 'F'; |
9c8105bd | 17112 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 17113 | func[i]->jit_requested = 1; |
d2a3b7c5 | 17114 | func[i]->blinding_requested = prog->blinding_requested; |
e6ac2450 | 17115 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 17116 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
17117 | func[i]->aux->linfo = prog->aux->linfo; |
17118 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
17119 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
17120 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
17121 | num_exentries = 0; |
17122 | insn = func[i]->insnsi; | |
17123 | for (j = 0; j < func[i]->len; j++, insn++) { | |
17124 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
17125 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
17126 | num_exentries++; | |
17127 | } | |
17128 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 17129 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
17130 | func[i] = bpf_int_jit_compile(func[i]); |
17131 | if (!func[i]->jited) { | |
17132 | err = -ENOTSUPP; | |
17133 | goto out_free; | |
17134 | } | |
17135 | cond_resched(); | |
17136 | } | |
a748c697 | 17137 | |
1c2a088a AS |
17138 | /* at this point all bpf functions were successfully JITed |
17139 | * now populate all bpf_calls with correct addresses and | |
17140 | * run last pass of JIT | |
17141 | */ | |
f910cefa | 17142 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17143 | insn = func[i]->insnsi; |
17144 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 17145 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 17146 | subprog = insn->off; |
69c087ba YS |
17147 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
17148 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
17149 | continue; | |
17150 | } | |
23a2d70c | 17151 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
17152 | continue; |
17153 | subprog = insn->off; | |
3d717fad | 17154 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 17155 | } |
2162fed4 SD |
17156 | |
17157 | /* we use the aux data to keep a list of the start addresses | |
17158 | * of the JITed images for each function in the program | |
17159 | * | |
17160 | * for some architectures, such as powerpc64, the imm field | |
17161 | * might not be large enough to hold the offset of the start | |
17162 | * address of the callee's JITed image from __bpf_call_base | |
17163 | * | |
17164 | * in such cases, we can lookup the start address of a callee | |
17165 | * by using its subprog id, available from the off field of | |
17166 | * the call instruction, as an index for this list | |
17167 | */ | |
17168 | func[i]->aux->func = func; | |
17169 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 17170 | } |
f910cefa | 17171 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17172 | old_bpf_func = func[i]->bpf_func; |
17173 | tmp = bpf_int_jit_compile(func[i]); | |
17174 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
17175 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 17176 | err = -ENOTSUPP; |
1c2a088a AS |
17177 | goto out_free; |
17178 | } | |
17179 | cond_resched(); | |
17180 | } | |
17181 | ||
17182 | /* finally lock prog and jit images for all functions and | |
17183 | * populate kallsysm | |
17184 | */ | |
f910cefa | 17185 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17186 | bpf_prog_lock_ro(func[i]); |
17187 | bpf_prog_kallsyms_add(func[i]); | |
17188 | } | |
7105e828 DB |
17189 | |
17190 | /* Last step: make now unused interpreter insns from main | |
17191 | * prog consistent for later dump requests, so they can | |
17192 | * later look the same as if they were interpreted only. | |
17193 | */ | |
17194 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
17195 | if (bpf_pseudo_func(insn)) { |
17196 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
17197 | insn[1].imm = insn->off; |
17198 | insn->off = 0; | |
69c087ba YS |
17199 | continue; |
17200 | } | |
23a2d70c | 17201 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
17202 | continue; |
17203 | insn->off = env->insn_aux_data[i].call_imm; | |
17204 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 17205 | insn->imm = subprog; |
7105e828 DB |
17206 | } |
17207 | ||
1c2a088a AS |
17208 | prog->jited = 1; |
17209 | prog->bpf_func = func[0]->bpf_func; | |
d00c6473 | 17210 | prog->jited_len = func[0]->jited_len; |
1c2a088a | 17211 | prog->aux->func = func; |
f910cefa | 17212 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 17213 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
17214 | return 0; |
17215 | out_free: | |
f263a814 JF |
17216 | /* We failed JIT'ing, so at this point we need to unregister poke |
17217 | * descriptors from subprogs, so that kernel is not attempting to | |
17218 | * patch it anymore as we're freeing the subprog JIT memory. | |
17219 | */ | |
17220 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
17221 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
17222 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
17223 | } | |
17224 | /* At this point we're guaranteed that poke descriptors are not | |
17225 | * live anymore. We can just unlink its descriptor table as it's | |
17226 | * released with the main prog. | |
17227 | */ | |
a748c697 MF |
17228 | for (i = 0; i < env->subprog_cnt; i++) { |
17229 | if (!func[i]) | |
17230 | continue; | |
f263a814 | 17231 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
17232 | bpf_jit_free(func[i]); |
17233 | } | |
1c2a088a | 17234 | kfree(func); |
c7a89784 | 17235 | out_undo_insn: |
1c2a088a AS |
17236 | /* cleanup main prog to be interpreted */ |
17237 | prog->jit_requested = 0; | |
d2a3b7c5 | 17238 | prog->blinding_requested = 0; |
1c2a088a | 17239 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
23a2d70c | 17240 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
17241 | continue; |
17242 | insn->off = 0; | |
17243 | insn->imm = env->insn_aux_data[i].call_imm; | |
17244 | } | |
e16301fb | 17245 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
17246 | return err; |
17247 | } | |
17248 | ||
1ea47e01 AS |
17249 | static int fixup_call_args(struct bpf_verifier_env *env) |
17250 | { | |
19d28fbd | 17251 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
17252 | struct bpf_prog *prog = env->prog; |
17253 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 17254 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 17255 | int i, depth; |
19d28fbd | 17256 | #endif |
e4052d06 | 17257 | int err = 0; |
1ea47e01 | 17258 | |
e4052d06 | 17259 | if (env->prog->jit_requested && |
9d03ebc7 | 17260 | !bpf_prog_is_offloaded(env->prog->aux)) { |
19d28fbd DM |
17261 | err = jit_subprogs(env); |
17262 | if (err == 0) | |
1c2a088a | 17263 | return 0; |
c7a89784 DB |
17264 | if (err == -EFAULT) |
17265 | return err; | |
19d28fbd DM |
17266 | } |
17267 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
17268 | if (has_kfunc_call) { |
17269 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
17270 | return -EINVAL; | |
17271 | } | |
e411901c MF |
17272 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
17273 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
17274 | * have to be rejected, since interpreter doesn't support them yet. | |
17275 | */ | |
17276 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
17277 | return -EINVAL; | |
17278 | } | |
1ea47e01 | 17279 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
17280 | if (bpf_pseudo_func(insn)) { |
17281 | /* When JIT fails the progs with callback calls | |
17282 | * have to be rejected, since interpreter doesn't support them yet. | |
17283 | */ | |
17284 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
17285 | return -EINVAL; | |
17286 | } | |
17287 | ||
23a2d70c | 17288 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
17289 | continue; |
17290 | depth = get_callee_stack_depth(env, insn, i); | |
17291 | if (depth < 0) | |
17292 | return depth; | |
17293 | bpf_patch_call_args(insn, depth); | |
17294 | } | |
19d28fbd DM |
17295 | err = 0; |
17296 | #endif | |
17297 | return err; | |
1ea47e01 AS |
17298 | } |
17299 | ||
958cf2e2 KKD |
17300 | static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
17301 | struct bpf_insn *insn_buf, int insn_idx, int *cnt) | |
e6ac2450 MKL |
17302 | { |
17303 | const struct bpf_kfunc_desc *desc; | |
3d76a4d3 | 17304 | void *xdp_kfunc; |
e6ac2450 | 17305 | |
a5d82727 KKD |
17306 | if (!insn->imm) { |
17307 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
17308 | return -EINVAL; | |
17309 | } | |
17310 | ||
3d76a4d3 SF |
17311 | *cnt = 0; |
17312 | ||
17313 | if (bpf_dev_bound_kfunc_id(insn->imm)) { | |
17314 | xdp_kfunc = bpf_dev_bound_resolve_kfunc(env->prog, insn->imm); | |
17315 | if (xdp_kfunc) { | |
17316 | insn->imm = BPF_CALL_IMM(xdp_kfunc); | |
17317 | return 0; | |
17318 | } | |
17319 | ||
17320 | /* fallback to default kfunc when not supported by netdev */ | |
17321 | } | |
17322 | ||
e6ac2450 | 17323 | /* insn->imm has the btf func_id. Replace it with |
c2cc0ce7 | 17324 | * an address (relative to __bpf_call_base). |
e6ac2450 | 17325 | */ |
2357672c | 17326 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
17327 | if (!desc) { |
17328 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
17329 | insn->imm); | |
17330 | return -EFAULT; | |
17331 | } | |
17332 | ||
17333 | insn->imm = desc->imm; | |
958cf2e2 KKD |
17334 | if (insn->off) |
17335 | return 0; | |
17336 | if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
17337 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
17338 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
17339 | u64 obj_new_size = env->insn_aux_data[insn_idx].obj_new_size; | |
e6ac2450 | 17340 | |
958cf2e2 KKD |
17341 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_1, obj_new_size); |
17342 | insn_buf[1] = addr[0]; | |
17343 | insn_buf[2] = addr[1]; | |
17344 | insn_buf[3] = *insn; | |
17345 | *cnt = 4; | |
ac9f0605 KKD |
17346 | } else if (desc->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { |
17347 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
17348 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
17349 | ||
17350 | insn_buf[0] = addr[0]; | |
17351 | insn_buf[1] = addr[1]; | |
17352 | insn_buf[2] = *insn; | |
17353 | *cnt = 3; | |
a35b9af4 YS |
17354 | } else if (desc->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] || |
17355 | desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { | |
fd264ca0 YS |
17356 | insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1); |
17357 | *cnt = 1; | |
b5964b96 JK |
17358 | } else if (desc->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { |
17359 | bool seen_direct_write = env->seen_direct_write; | |
17360 | bool is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE); | |
17361 | ||
17362 | if (is_rdonly) | |
17363 | insn->imm = BPF_CALL_IMM(bpf_dynptr_from_skb_rdonly); | |
17364 | ||
17365 | /* restore env->seen_direct_write to its original value, since | |
17366 | * may_access_direct_pkt_data mutates it | |
17367 | */ | |
17368 | env->seen_direct_write = seen_direct_write; | |
958cf2e2 | 17369 | } |
e6ac2450 MKL |
17370 | return 0; |
17371 | } | |
17372 | ||
e6ac5933 BJ |
17373 | /* Do various post-verification rewrites in a single program pass. |
17374 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 17375 | */ |
e6ac5933 | 17376 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 17377 | { |
79741b3b | 17378 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 17379 | enum bpf_attach_type eatype = prog->expected_attach_type; |
9b99edca | 17380 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 17381 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 17382 | const struct bpf_func_proto *fn; |
79741b3b | 17383 | const int insn_cnt = prog->len; |
09772d92 | 17384 | const struct bpf_map_ops *ops; |
c93552c4 | 17385 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
17386 | struct bpf_insn insn_buf[16]; |
17387 | struct bpf_prog *new_prog; | |
17388 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 17389 | int i, ret, cnt, delta = 0; |
e245c5c6 | 17390 | |
79741b3b | 17391 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 17392 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
17393 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
17394 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
17395 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 17396 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 17397 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
17398 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
17399 | struct bpf_insn *patchlet; | |
17400 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 17401 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
17402 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
17403 | BPF_JNE | BPF_K, insn->src_reg, | |
17404 | 0, 2, 0), | |
f6b1b3bf DB |
17405 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
17406 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
17407 | *insn, | |
17408 | }; | |
e88b2c6e | 17409 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 17410 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
17411 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
17412 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 17413 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 17414 | *insn, |
9b00f1b7 DB |
17415 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
17416 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 17417 | }; |
f6b1b3bf | 17418 | |
e88b2c6e DB |
17419 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
17420 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 17421 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
17422 | |
17423 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
17424 | if (!new_prog) |
17425 | return -ENOMEM; | |
17426 | ||
17427 | delta += cnt - 1; | |
17428 | env->prog = prog = new_prog; | |
17429 | insn = new_prog->insnsi + i + delta; | |
17430 | continue; | |
17431 | } | |
17432 | ||
e6ac5933 | 17433 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
17434 | if (BPF_CLASS(insn->code) == BPF_LD && |
17435 | (BPF_MODE(insn->code) == BPF_ABS || | |
17436 | BPF_MODE(insn->code) == BPF_IND)) { | |
17437 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
17438 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
17439 | verbose(env, "bpf verifier is misconfigured\n"); | |
17440 | return -EINVAL; | |
17441 | } | |
17442 | ||
17443 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17444 | if (!new_prog) | |
17445 | return -ENOMEM; | |
17446 | ||
17447 | delta += cnt - 1; | |
17448 | env->prog = prog = new_prog; | |
17449 | insn = new_prog->insnsi + i + delta; | |
17450 | continue; | |
17451 | } | |
17452 | ||
e6ac5933 | 17453 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
17454 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
17455 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
17456 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
17457 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 17458 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 17459 | bool issrc, isneg, isimm; |
979d63d5 DB |
17460 | u32 off_reg; |
17461 | ||
17462 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
17463 | if (!aux->alu_state || |
17464 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
17465 | continue; |
17466 | ||
17467 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
17468 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
17469 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 17470 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
17471 | |
17472 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
17473 | if (isimm) { |
17474 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
17475 | } else { | |
17476 | if (isneg) | |
17477 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
17478 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
17479 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
17480 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
17481 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
17482 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
17483 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
17484 | } | |
b9b34ddb DB |
17485 | if (!issrc) |
17486 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
17487 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
17488 | if (isneg) |
17489 | insn->code = insn->code == code_add ? | |
17490 | code_sub : code_add; | |
17491 | *patch++ = *insn; | |
801c6058 | 17492 | if (issrc && isneg && !isimm) |
979d63d5 DB |
17493 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
17494 | cnt = patch - insn_buf; | |
17495 | ||
17496 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17497 | if (!new_prog) | |
17498 | return -ENOMEM; | |
17499 | ||
17500 | delta += cnt - 1; | |
17501 | env->prog = prog = new_prog; | |
17502 | insn = new_prog->insnsi + i + delta; | |
17503 | continue; | |
17504 | } | |
17505 | ||
79741b3b AS |
17506 | if (insn->code != (BPF_JMP | BPF_CALL)) |
17507 | continue; | |
cc8b0b92 AS |
17508 | if (insn->src_reg == BPF_PSEUDO_CALL) |
17509 | continue; | |
e6ac2450 | 17510 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
958cf2e2 | 17511 | ret = fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt); |
e6ac2450 MKL |
17512 | if (ret) |
17513 | return ret; | |
958cf2e2 KKD |
17514 | if (cnt == 0) |
17515 | continue; | |
17516 | ||
17517 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17518 | if (!new_prog) | |
17519 | return -ENOMEM; | |
17520 | ||
17521 | delta += cnt - 1; | |
17522 | env->prog = prog = new_prog; | |
17523 | insn = new_prog->insnsi + i + delta; | |
e6ac2450 MKL |
17524 | continue; |
17525 | } | |
e245c5c6 | 17526 | |
79741b3b AS |
17527 | if (insn->imm == BPF_FUNC_get_route_realm) |
17528 | prog->dst_needed = 1; | |
17529 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
17530 | bpf_user_rnd_init_once(); | |
9802d865 JB |
17531 | if (insn->imm == BPF_FUNC_override_return) |
17532 | prog->kprobe_override = 1; | |
79741b3b | 17533 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
17534 | /* If we tail call into other programs, we |
17535 | * cannot make any assumptions since they can | |
17536 | * be replaced dynamically during runtime in | |
17537 | * the program array. | |
17538 | */ | |
17539 | prog->cb_access = 1; | |
e411901c MF |
17540 | if (!allow_tail_call_in_subprogs(env)) |
17541 | prog->aux->stack_depth = MAX_BPF_STACK; | |
17542 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 17543 | |
79741b3b | 17544 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 17545 | * conditional branch in the interpreter for every normal |
79741b3b AS |
17546 | * call and to prevent accidental JITing by JIT compiler |
17547 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 17548 | */ |
79741b3b | 17549 | insn->imm = 0; |
71189fa9 | 17550 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 17551 | |
c93552c4 | 17552 | aux = &env->insn_aux_data[i + delta]; |
d2a3b7c5 | 17553 | if (env->bpf_capable && !prog->blinding_requested && |
cc52d914 | 17554 | prog->jit_requested && |
d2e4c1e6 DB |
17555 | !bpf_map_key_poisoned(aux) && |
17556 | !bpf_map_ptr_poisoned(aux) && | |
17557 | !bpf_map_ptr_unpriv(aux)) { | |
17558 | struct bpf_jit_poke_descriptor desc = { | |
17559 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
17560 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
17561 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 17562 | .insn_idx = i + delta, |
d2e4c1e6 DB |
17563 | }; |
17564 | ||
17565 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
17566 | if (ret < 0) { | |
17567 | verbose(env, "adding tail call poke descriptor failed\n"); | |
17568 | return ret; | |
17569 | } | |
17570 | ||
17571 | insn->imm = ret + 1; | |
17572 | continue; | |
17573 | } | |
17574 | ||
c93552c4 DB |
17575 | if (!bpf_map_ptr_unpriv(aux)) |
17576 | continue; | |
17577 | ||
b2157399 AS |
17578 | /* instead of changing every JIT dealing with tail_call |
17579 | * emit two extra insns: | |
17580 | * if (index >= max_entries) goto out; | |
17581 | * index &= array->index_mask; | |
17582 | * to avoid out-of-bounds cpu speculation | |
17583 | */ | |
c93552c4 | 17584 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 17585 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
17586 | return -EINVAL; |
17587 | } | |
c93552c4 | 17588 | |
d2e4c1e6 | 17589 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
17590 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
17591 | map_ptr->max_entries, 2); | |
17592 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
17593 | container_of(map_ptr, | |
17594 | struct bpf_array, | |
17595 | map)->index_mask); | |
17596 | insn_buf[2] = *insn; | |
17597 | cnt = 3; | |
17598 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17599 | if (!new_prog) | |
17600 | return -ENOMEM; | |
17601 | ||
17602 | delta += cnt - 1; | |
17603 | env->prog = prog = new_prog; | |
17604 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
17605 | continue; |
17606 | } | |
e245c5c6 | 17607 | |
b00628b1 AS |
17608 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
17609 | /* The verifier will process callback_fn as many times as necessary | |
17610 | * with different maps and the register states prepared by | |
17611 | * set_timer_callback_state will be accurate. | |
17612 | * | |
17613 | * The following use case is valid: | |
17614 | * map1 is shared by prog1, prog2, prog3. | |
17615 | * prog1 calls bpf_timer_init for some map1 elements | |
17616 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
17617 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
17618 | * prog3 calls bpf_timer_start for some map1 elements. | |
17619 | * Those that were not both bpf_timer_init-ed and | |
17620 | * bpf_timer_set_callback-ed will return -EINVAL. | |
17621 | */ | |
17622 | struct bpf_insn ld_addrs[2] = { | |
17623 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
17624 | }; | |
17625 | ||
17626 | insn_buf[0] = ld_addrs[0]; | |
17627 | insn_buf[1] = ld_addrs[1]; | |
17628 | insn_buf[2] = *insn; | |
17629 | cnt = 3; | |
17630 | ||
17631 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17632 | if (!new_prog) | |
17633 | return -ENOMEM; | |
17634 | ||
17635 | delta += cnt - 1; | |
17636 | env->prog = prog = new_prog; | |
17637 | insn = new_prog->insnsi + i + delta; | |
17638 | goto patch_call_imm; | |
17639 | } | |
17640 | ||
9bb00b28 YS |
17641 | if (is_storage_get_function(insn->imm)) { |
17642 | if (!env->prog->aux->sleepable || | |
17643 | env->insn_aux_data[i + delta].storage_get_func_atomic) | |
d56c9fe6 | 17644 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); |
9bb00b28 YS |
17645 | else |
17646 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); | |
b00fa38a JK |
17647 | insn_buf[1] = *insn; |
17648 | cnt = 2; | |
17649 | ||
17650 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17651 | if (!new_prog) | |
17652 | return -ENOMEM; | |
17653 | ||
17654 | delta += cnt - 1; | |
17655 | env->prog = prog = new_prog; | |
17656 | insn = new_prog->insnsi + i + delta; | |
17657 | goto patch_call_imm; | |
17658 | } | |
17659 | ||
89c63074 | 17660 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
17661 | * and other inlining handlers are currently limited to 64 bit |
17662 | * only. | |
89c63074 | 17663 | */ |
60b58afc | 17664 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
17665 | (insn->imm == BPF_FUNC_map_lookup_elem || |
17666 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
17667 | insn->imm == BPF_FUNC_map_delete_elem || |
17668 | insn->imm == BPF_FUNC_map_push_elem || | |
17669 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 17670 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c | 17671 | insn->imm == BPF_FUNC_redirect_map || |
07343110 FZ |
17672 | insn->imm == BPF_FUNC_for_each_map_elem || |
17673 | insn->imm == BPF_FUNC_map_lookup_percpu_elem)) { | |
c93552c4 DB |
17674 | aux = &env->insn_aux_data[i + delta]; |
17675 | if (bpf_map_ptr_poisoned(aux)) | |
17676 | goto patch_call_imm; | |
17677 | ||
d2e4c1e6 | 17678 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
17679 | ops = map_ptr->ops; |
17680 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
17681 | ops->map_gen_lookup) { | |
17682 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
17683 | if (cnt == -EOPNOTSUPP) |
17684 | goto patch_map_ops_generic; | |
17685 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
17686 | verbose(env, "bpf verifier is misconfigured\n"); |
17687 | return -EINVAL; | |
17688 | } | |
81ed18ab | 17689 | |
09772d92 DB |
17690 | new_prog = bpf_patch_insn_data(env, i + delta, |
17691 | insn_buf, cnt); | |
17692 | if (!new_prog) | |
17693 | return -ENOMEM; | |
81ed18ab | 17694 | |
09772d92 DB |
17695 | delta += cnt - 1; |
17696 | env->prog = prog = new_prog; | |
17697 | insn = new_prog->insnsi + i + delta; | |
17698 | continue; | |
17699 | } | |
81ed18ab | 17700 | |
09772d92 DB |
17701 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
17702 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
17703 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
d7ba4cc9 | 17704 | (long (*)(struct bpf_map *map, void *key))NULL)); |
09772d92 | 17705 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
d7ba4cc9 | 17706 | (long (*)(struct bpf_map *map, void *key, void *value, |
09772d92 | 17707 | u64 flags))NULL)); |
84430d42 | 17708 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
d7ba4cc9 | 17709 | (long (*)(struct bpf_map *map, void *value, |
84430d42 DB |
17710 | u64 flags))NULL)); |
17711 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
d7ba4cc9 | 17712 | (long (*)(struct bpf_map *map, void *value))NULL)); |
84430d42 | 17713 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
d7ba4cc9 | 17714 | (long (*)(struct bpf_map *map, void *value))NULL)); |
e6a4750f | 17715 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
d7ba4cc9 | 17716 | (long (*)(struct bpf_map *map, u64 index, u64 flags))NULL)); |
0640c77c | 17717 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
d7ba4cc9 | 17718 | (long (*)(struct bpf_map *map, |
0640c77c AI |
17719 | bpf_callback_t callback_fn, |
17720 | void *callback_ctx, | |
17721 | u64 flags))NULL)); | |
07343110 FZ |
17722 | BUILD_BUG_ON(!__same_type(ops->map_lookup_percpu_elem, |
17723 | (void *(*)(struct bpf_map *map, void *key, u32 cpu))NULL)); | |
e6a4750f | 17724 | |
4a8f87e6 | 17725 | patch_map_ops_generic: |
09772d92 DB |
17726 | switch (insn->imm) { |
17727 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 17728 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
17729 | continue; |
17730 | case BPF_FUNC_map_update_elem: | |
3d717fad | 17731 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
17732 | continue; |
17733 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 17734 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 17735 | continue; |
84430d42 | 17736 | case BPF_FUNC_map_push_elem: |
3d717fad | 17737 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
17738 | continue; |
17739 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 17740 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
17741 | continue; |
17742 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 17743 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 17744 | continue; |
e6a4750f | 17745 | case BPF_FUNC_redirect_map: |
3d717fad | 17746 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 17747 | continue; |
0640c77c AI |
17748 | case BPF_FUNC_for_each_map_elem: |
17749 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 17750 | continue; |
07343110 FZ |
17751 | case BPF_FUNC_map_lookup_percpu_elem: |
17752 | insn->imm = BPF_CALL_IMM(ops->map_lookup_percpu_elem); | |
17753 | continue; | |
09772d92 | 17754 | } |
81ed18ab | 17755 | |
09772d92 | 17756 | goto patch_call_imm; |
81ed18ab AS |
17757 | } |
17758 | ||
e6ac5933 | 17759 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
17760 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
17761 | insn->imm == BPF_FUNC_jiffies64) { | |
17762 | struct bpf_insn ld_jiffies_addr[2] = { | |
17763 | BPF_LD_IMM64(BPF_REG_0, | |
17764 | (unsigned long)&jiffies), | |
17765 | }; | |
17766 | ||
17767 | insn_buf[0] = ld_jiffies_addr[0]; | |
17768 | insn_buf[1] = ld_jiffies_addr[1]; | |
17769 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
17770 | BPF_REG_0, 0); | |
17771 | cnt = 3; | |
17772 | ||
17773 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
17774 | cnt); | |
17775 | if (!new_prog) | |
17776 | return -ENOMEM; | |
17777 | ||
17778 | delta += cnt - 1; | |
17779 | env->prog = prog = new_prog; | |
17780 | insn = new_prog->insnsi + i + delta; | |
17781 | continue; | |
17782 | } | |
17783 | ||
f92c1e18 JO |
17784 | /* Implement bpf_get_func_arg inline. */ |
17785 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17786 | insn->imm == BPF_FUNC_get_func_arg) { | |
17787 | /* Load nr_args from ctx - 8 */ | |
17788 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17789 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
17790 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
17791 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
17792 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
17793 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
17794 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
17795 | insn_buf[7] = BPF_JMP_A(1); | |
17796 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
17797 | cnt = 9; | |
17798 | ||
17799 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17800 | if (!new_prog) | |
17801 | return -ENOMEM; | |
17802 | ||
17803 | delta += cnt - 1; | |
17804 | env->prog = prog = new_prog; | |
17805 | insn = new_prog->insnsi + i + delta; | |
17806 | continue; | |
17807 | } | |
17808 | ||
17809 | /* Implement bpf_get_func_ret inline. */ | |
17810 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17811 | insn->imm == BPF_FUNC_get_func_ret) { | |
17812 | if (eatype == BPF_TRACE_FEXIT || | |
17813 | eatype == BPF_MODIFY_RETURN) { | |
17814 | /* Load nr_args from ctx - 8 */ | |
17815 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17816 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
17817 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
17818 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
17819 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
17820 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
17821 | cnt = 6; | |
17822 | } else { | |
17823 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
17824 | cnt = 1; | |
17825 | } | |
17826 | ||
17827 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17828 | if (!new_prog) | |
17829 | return -ENOMEM; | |
17830 | ||
17831 | delta += cnt - 1; | |
17832 | env->prog = prog = new_prog; | |
17833 | insn = new_prog->insnsi + i + delta; | |
17834 | continue; | |
17835 | } | |
17836 | ||
17837 | /* Implement get_func_arg_cnt inline. */ | |
17838 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17839 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
17840 | /* Load nr_args from ctx - 8 */ | |
17841 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17842 | ||
17843 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
17844 | if (!new_prog) | |
17845 | return -ENOMEM; | |
17846 | ||
17847 | env->prog = prog = new_prog; | |
17848 | insn = new_prog->insnsi + i + delta; | |
17849 | continue; | |
17850 | } | |
17851 | ||
f705ec76 | 17852 | /* Implement bpf_get_func_ip inline. */ |
9b99edca JO |
17853 | if (prog_type == BPF_PROG_TYPE_TRACING && |
17854 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
17855 | /* Load IP address from ctx - 16 */ |
17856 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
17857 | |
17858 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
17859 | if (!new_prog) | |
17860 | return -ENOMEM; | |
17861 | ||
17862 | env->prog = prog = new_prog; | |
17863 | insn = new_prog->insnsi + i + delta; | |
17864 | continue; | |
17865 | } | |
17866 | ||
81ed18ab | 17867 | patch_call_imm: |
5e43f899 | 17868 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
17869 | /* all functions that have prototype and verifier allowed |
17870 | * programs to call them, must be real in-kernel functions | |
17871 | */ | |
17872 | if (!fn->func) { | |
61bd5218 JK |
17873 | verbose(env, |
17874 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
17875 | func_id_name(insn->imm), insn->imm); |
17876 | return -EFAULT; | |
e245c5c6 | 17877 | } |
79741b3b | 17878 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 17879 | } |
e245c5c6 | 17880 | |
d2e4c1e6 DB |
17881 | /* Since poke tab is now finalized, publish aux to tracker. */ |
17882 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
17883 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
17884 | if (!map_ptr->ops->map_poke_track || | |
17885 | !map_ptr->ops->map_poke_untrack || | |
17886 | !map_ptr->ops->map_poke_run) { | |
17887 | verbose(env, "bpf verifier is misconfigured\n"); | |
17888 | return -EINVAL; | |
17889 | } | |
17890 | ||
17891 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
17892 | if (ret < 0) { | |
17893 | verbose(env, "tracking tail call prog failed\n"); | |
17894 | return ret; | |
17895 | } | |
17896 | } | |
17897 | ||
e6ac2450 MKL |
17898 | sort_kfunc_descs_by_imm(env->prog); |
17899 | ||
79741b3b AS |
17900 | return 0; |
17901 | } | |
e245c5c6 | 17902 | |
1ade2371 EZ |
17903 | static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env, |
17904 | int position, | |
17905 | s32 stack_base, | |
17906 | u32 callback_subprogno, | |
17907 | u32 *cnt) | |
17908 | { | |
17909 | s32 r6_offset = stack_base + 0 * BPF_REG_SIZE; | |
17910 | s32 r7_offset = stack_base + 1 * BPF_REG_SIZE; | |
17911 | s32 r8_offset = stack_base + 2 * BPF_REG_SIZE; | |
17912 | int reg_loop_max = BPF_REG_6; | |
17913 | int reg_loop_cnt = BPF_REG_7; | |
17914 | int reg_loop_ctx = BPF_REG_8; | |
17915 | ||
17916 | struct bpf_prog *new_prog; | |
17917 | u32 callback_start; | |
17918 | u32 call_insn_offset; | |
17919 | s32 callback_offset; | |
17920 | ||
17921 | /* This represents an inlined version of bpf_iter.c:bpf_loop, | |
17922 | * be careful to modify this code in sync. | |
17923 | */ | |
17924 | struct bpf_insn insn_buf[] = { | |
17925 | /* Return error and jump to the end of the patch if | |
17926 | * expected number of iterations is too big. | |
17927 | */ | |
17928 | BPF_JMP_IMM(BPF_JLE, BPF_REG_1, BPF_MAX_LOOPS, 2), | |
17929 | BPF_MOV32_IMM(BPF_REG_0, -E2BIG), | |
17930 | BPF_JMP_IMM(BPF_JA, 0, 0, 16), | |
17931 | /* spill R6, R7, R8 to use these as loop vars */ | |
17932 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, r6_offset), | |
17933 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_7, r7_offset), | |
17934 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_8, r8_offset), | |
17935 | /* initialize loop vars */ | |
17936 | BPF_MOV64_REG(reg_loop_max, BPF_REG_1), | |
17937 | BPF_MOV32_IMM(reg_loop_cnt, 0), | |
17938 | BPF_MOV64_REG(reg_loop_ctx, BPF_REG_3), | |
17939 | /* loop header, | |
17940 | * if reg_loop_cnt >= reg_loop_max skip the loop body | |
17941 | */ | |
17942 | BPF_JMP_REG(BPF_JGE, reg_loop_cnt, reg_loop_max, 5), | |
17943 | /* callback call, | |
17944 | * correct callback offset would be set after patching | |
17945 | */ | |
17946 | BPF_MOV64_REG(BPF_REG_1, reg_loop_cnt), | |
17947 | BPF_MOV64_REG(BPF_REG_2, reg_loop_ctx), | |
17948 | BPF_CALL_REL(0), | |
17949 | /* increment loop counter */ | |
17950 | BPF_ALU64_IMM(BPF_ADD, reg_loop_cnt, 1), | |
17951 | /* jump to loop header if callback returned 0 */ | |
17952 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -6), | |
17953 | /* return value of bpf_loop, | |
17954 | * set R0 to the number of iterations | |
17955 | */ | |
17956 | BPF_MOV64_REG(BPF_REG_0, reg_loop_cnt), | |
17957 | /* restore original values of R6, R7, R8 */ | |
17958 | BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, r6_offset), | |
17959 | BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, r7_offset), | |
17960 | BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_10, r8_offset), | |
17961 | }; | |
17962 | ||
17963 | *cnt = ARRAY_SIZE(insn_buf); | |
17964 | new_prog = bpf_patch_insn_data(env, position, insn_buf, *cnt); | |
17965 | if (!new_prog) | |
17966 | return new_prog; | |
17967 | ||
17968 | /* callback start is known only after patching */ | |
17969 | callback_start = env->subprog_info[callback_subprogno].start; | |
17970 | /* Note: insn_buf[12] is an offset of BPF_CALL_REL instruction */ | |
17971 | call_insn_offset = position + 12; | |
17972 | callback_offset = callback_start - call_insn_offset - 1; | |
fb4e3b33 | 17973 | new_prog->insnsi[call_insn_offset].imm = callback_offset; |
1ade2371 EZ |
17974 | |
17975 | return new_prog; | |
17976 | } | |
17977 | ||
17978 | static bool is_bpf_loop_call(struct bpf_insn *insn) | |
17979 | { | |
17980 | return insn->code == (BPF_JMP | BPF_CALL) && | |
17981 | insn->src_reg == 0 && | |
17982 | insn->imm == BPF_FUNC_loop; | |
17983 | } | |
17984 | ||
17985 | /* For all sub-programs in the program (including main) check | |
17986 | * insn_aux_data to see if there are bpf_loop calls that require | |
17987 | * inlining. If such calls are found the calls are replaced with a | |
17988 | * sequence of instructions produced by `inline_bpf_loop` function and | |
17989 | * subprog stack_depth is increased by the size of 3 registers. | |
17990 | * This stack space is used to spill values of the R6, R7, R8. These | |
17991 | * registers are used to store the loop bound, counter and context | |
17992 | * variables. | |
17993 | */ | |
17994 | static int optimize_bpf_loop(struct bpf_verifier_env *env) | |
17995 | { | |
17996 | struct bpf_subprog_info *subprogs = env->subprog_info; | |
17997 | int i, cur_subprog = 0, cnt, delta = 0; | |
17998 | struct bpf_insn *insn = env->prog->insnsi; | |
17999 | int insn_cnt = env->prog->len; | |
18000 | u16 stack_depth = subprogs[cur_subprog].stack_depth; | |
18001 | u16 stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
18002 | u16 stack_depth_extra = 0; | |
18003 | ||
18004 | for (i = 0; i < insn_cnt; i++, insn++) { | |
18005 | struct bpf_loop_inline_state *inline_state = | |
18006 | &env->insn_aux_data[i + delta].loop_inline_state; | |
18007 | ||
18008 | if (is_bpf_loop_call(insn) && inline_state->fit_for_inline) { | |
18009 | struct bpf_prog *new_prog; | |
18010 | ||
18011 | stack_depth_extra = BPF_REG_SIZE * 3 + stack_depth_roundup; | |
18012 | new_prog = inline_bpf_loop(env, | |
18013 | i + delta, | |
18014 | -(stack_depth + stack_depth_extra), | |
18015 | inline_state->callback_subprogno, | |
18016 | &cnt); | |
18017 | if (!new_prog) | |
18018 | return -ENOMEM; | |
18019 | ||
18020 | delta += cnt - 1; | |
18021 | env->prog = new_prog; | |
18022 | insn = new_prog->insnsi + i + delta; | |
18023 | } | |
18024 | ||
18025 | if (subprogs[cur_subprog + 1].start == i + delta + 1) { | |
18026 | subprogs[cur_subprog].stack_depth += stack_depth_extra; | |
18027 | cur_subprog++; | |
18028 | stack_depth = subprogs[cur_subprog].stack_depth; | |
18029 | stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
18030 | stack_depth_extra = 0; | |
18031 | } | |
18032 | } | |
18033 | ||
18034 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
18035 | ||
18036 | return 0; | |
18037 | } | |
18038 | ||
58e2af8b | 18039 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 18040 | { |
58e2af8b | 18041 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
18042 | int i; |
18043 | ||
9f4686c4 AS |
18044 | sl = env->free_list; |
18045 | while (sl) { | |
18046 | sln = sl->next; | |
18047 | free_verifier_state(&sl->state, false); | |
18048 | kfree(sl); | |
18049 | sl = sln; | |
18050 | } | |
51c39bb1 | 18051 | env->free_list = NULL; |
9f4686c4 | 18052 | |
f1bca824 AS |
18053 | if (!env->explored_states) |
18054 | return; | |
18055 | ||
dc2a4ebc | 18056 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
18057 | sl = env->explored_states[i]; |
18058 | ||
a8f500af AS |
18059 | while (sl) { |
18060 | sln = sl->next; | |
18061 | free_verifier_state(&sl->state, false); | |
18062 | kfree(sl); | |
18063 | sl = sln; | |
18064 | } | |
51c39bb1 | 18065 | env->explored_states[i] = NULL; |
f1bca824 | 18066 | } |
51c39bb1 | 18067 | } |
f1bca824 | 18068 | |
51c39bb1 AS |
18069 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
18070 | { | |
6f8a57cc | 18071 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
18072 | struct bpf_verifier_state *state; |
18073 | struct bpf_reg_state *regs; | |
18074 | int ret, i; | |
18075 | ||
18076 | env->prev_linfo = NULL; | |
18077 | env->pass_cnt++; | |
18078 | ||
18079 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
18080 | if (!state) | |
18081 | return -ENOMEM; | |
18082 | state->curframe = 0; | |
18083 | state->speculative = false; | |
18084 | state->branches = 1; | |
18085 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
18086 | if (!state->frame[0]) { | |
18087 | kfree(state); | |
18088 | return -ENOMEM; | |
18089 | } | |
18090 | env->cur_state = state; | |
18091 | init_func_state(env, state->frame[0], | |
18092 | BPF_MAIN_FUNC /* callsite */, | |
18093 | 0 /* frameno */, | |
18094 | subprog); | |
be2ef816 AN |
18095 | state->first_insn_idx = env->subprog_info[subprog].start; |
18096 | state->last_insn_idx = -1; | |
51c39bb1 AS |
18097 | |
18098 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 18099 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
18100 | ret = btf_prepare_func_args(env, subprog, regs); |
18101 | if (ret) | |
18102 | goto out; | |
18103 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
18104 | if (regs[i].type == PTR_TO_CTX) | |
18105 | mark_reg_known_zero(env, regs, i); | |
18106 | else if (regs[i].type == SCALAR_VALUE) | |
18107 | mark_reg_unknown(env, regs, i); | |
cf9f2f8d | 18108 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
e5069b9c DB |
18109 | const u32 mem_size = regs[i].mem_size; |
18110 | ||
18111 | mark_reg_known_zero(env, regs, i); | |
18112 | regs[i].mem_size = mem_size; | |
18113 | regs[i].id = ++env->id_gen; | |
18114 | } | |
51c39bb1 AS |
18115 | } |
18116 | } else { | |
18117 | /* 1st arg to a function */ | |
18118 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
18119 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 18120 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
18121 | if (ret == -EFAULT) |
18122 | /* unlikely verifier bug. abort. | |
18123 | * ret == 0 and ret < 0 are sadly acceptable for | |
18124 | * main() function due to backward compatibility. | |
18125 | * Like socket filter program may be written as: | |
18126 | * int bpf_prog(struct pt_regs *ctx) | |
18127 | * and never dereference that ctx in the program. | |
18128 | * 'struct pt_regs' is a type mismatch for socket | |
18129 | * filter that should be using 'struct __sk_buff'. | |
18130 | */ | |
18131 | goto out; | |
18132 | } | |
18133 | ||
18134 | ret = do_check(env); | |
18135 | out: | |
f59bbfc2 AS |
18136 | /* check for NULL is necessary, since cur_state can be freed inside |
18137 | * do_check() under memory pressure. | |
18138 | */ | |
18139 | if (env->cur_state) { | |
18140 | free_verifier_state(env->cur_state, true); | |
18141 | env->cur_state = NULL; | |
18142 | } | |
6f8a57cc AN |
18143 | while (!pop_stack(env, NULL, NULL, false)); |
18144 | if (!ret && pop_log) | |
18145 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 18146 | free_states(env); |
51c39bb1 AS |
18147 | return ret; |
18148 | } | |
18149 | ||
18150 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
18151 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
18152 | * Consider: | |
18153 | * int bar(int); | |
18154 | * int foo(int f) | |
18155 | * { | |
18156 | * return bar(f); | |
18157 | * } | |
18158 | * int bar(int b) | |
18159 | * { | |
18160 | * ... | |
18161 | * } | |
18162 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
18163 | * will be assumed that bar() already verified successfully and call to bar() | |
18164 | * from foo() will be checked for type match only. Later bar() will be verified | |
18165 | * independently to check that it's safe for R1=any_scalar_value. | |
18166 | */ | |
18167 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
18168 | { | |
18169 | struct bpf_prog_aux *aux = env->prog->aux; | |
18170 | int i, ret; | |
18171 | ||
18172 | if (!aux->func_info) | |
18173 | return 0; | |
18174 | ||
18175 | for (i = 1; i < env->subprog_cnt; i++) { | |
18176 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
18177 | continue; | |
18178 | env->insn_idx = env->subprog_info[i].start; | |
18179 | WARN_ON_ONCE(env->insn_idx == 0); | |
18180 | ret = do_check_common(env, i); | |
18181 | if (ret) { | |
18182 | return ret; | |
18183 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
18184 | verbose(env, | |
18185 | "Func#%d is safe for any args that match its prototype\n", | |
18186 | i); | |
18187 | } | |
18188 | } | |
18189 | return 0; | |
18190 | } | |
18191 | ||
18192 | static int do_check_main(struct bpf_verifier_env *env) | |
18193 | { | |
18194 | int ret; | |
18195 | ||
18196 | env->insn_idx = 0; | |
18197 | ret = do_check_common(env, 0); | |
18198 | if (!ret) | |
18199 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
18200 | return ret; | |
18201 | } | |
18202 | ||
18203 | ||
06ee7115 AS |
18204 | static void print_verification_stats(struct bpf_verifier_env *env) |
18205 | { | |
18206 | int i; | |
18207 | ||
18208 | if (env->log.level & BPF_LOG_STATS) { | |
18209 | verbose(env, "verification time %lld usec\n", | |
18210 | div_u64(env->verification_time, 1000)); | |
18211 | verbose(env, "stack depth "); | |
18212 | for (i = 0; i < env->subprog_cnt; i++) { | |
18213 | u32 depth = env->subprog_info[i].stack_depth; | |
18214 | ||
18215 | verbose(env, "%d", depth); | |
18216 | if (i + 1 < env->subprog_cnt) | |
18217 | verbose(env, "+"); | |
18218 | } | |
18219 | verbose(env, "\n"); | |
18220 | } | |
18221 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
18222 | "total_states %d peak_states %d mark_read %d\n", | |
18223 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
18224 | env->max_states_per_insn, env->total_states, | |
18225 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
18226 | } |
18227 | ||
27ae7997 MKL |
18228 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
18229 | { | |
18230 | const struct btf_type *t, *func_proto; | |
18231 | const struct bpf_struct_ops *st_ops; | |
18232 | const struct btf_member *member; | |
18233 | struct bpf_prog *prog = env->prog; | |
18234 | u32 btf_id, member_idx; | |
18235 | const char *mname; | |
18236 | ||
12aa8a94 THJ |
18237 | if (!prog->gpl_compatible) { |
18238 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
18239 | return -EINVAL; | |
18240 | } | |
18241 | ||
27ae7997 MKL |
18242 | btf_id = prog->aux->attach_btf_id; |
18243 | st_ops = bpf_struct_ops_find(btf_id); | |
18244 | if (!st_ops) { | |
18245 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
18246 | btf_id); | |
18247 | return -ENOTSUPP; | |
18248 | } | |
18249 | ||
18250 | t = st_ops->type; | |
18251 | member_idx = prog->expected_attach_type; | |
18252 | if (member_idx >= btf_type_vlen(t)) { | |
18253 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
18254 | member_idx, st_ops->name); | |
18255 | return -EINVAL; | |
18256 | } | |
18257 | ||
18258 | member = &btf_type_member(t)[member_idx]; | |
18259 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
18260 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
18261 | NULL); | |
18262 | if (!func_proto) { | |
18263 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
18264 | mname, member_idx, st_ops->name); | |
18265 | return -EINVAL; | |
18266 | } | |
18267 | ||
18268 | if (st_ops->check_member) { | |
51a52a29 | 18269 | int err = st_ops->check_member(t, member, prog); |
27ae7997 MKL |
18270 | |
18271 | if (err) { | |
18272 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
18273 | mname, st_ops->name); | |
18274 | return err; | |
18275 | } | |
18276 | } | |
18277 | ||
18278 | prog->aux->attach_func_proto = func_proto; | |
18279 | prog->aux->attach_func_name = mname; | |
18280 | env->ops = st_ops->verifier_ops; | |
18281 | ||
18282 | return 0; | |
18283 | } | |
6ba43b76 KS |
18284 | #define SECURITY_PREFIX "security_" |
18285 | ||
f7b12b6f | 18286 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 18287 | { |
69191754 | 18288 | if (within_error_injection_list(addr) || |
f7b12b6f | 18289 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 18290 | return 0; |
6ba43b76 | 18291 | |
6ba43b76 KS |
18292 | return -EINVAL; |
18293 | } | |
27ae7997 | 18294 | |
1e6c62a8 AS |
18295 | /* list of non-sleepable functions that are otherwise on |
18296 | * ALLOW_ERROR_INJECTION list | |
18297 | */ | |
18298 | BTF_SET_START(btf_non_sleepable_error_inject) | |
18299 | /* Three functions below can be called from sleepable and non-sleepable context. | |
18300 | * Assume non-sleepable from bpf safety point of view. | |
18301 | */ | |
9dd3d069 | 18302 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
18303 | BTF_ID(func, should_fail_alloc_page) |
18304 | BTF_ID(func, should_failslab) | |
18305 | BTF_SET_END(btf_non_sleepable_error_inject) | |
18306 | ||
18307 | static int check_non_sleepable_error_inject(u32 btf_id) | |
18308 | { | |
18309 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
18310 | } | |
18311 | ||
f7b12b6f THJ |
18312 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
18313 | const struct bpf_prog *prog, | |
18314 | const struct bpf_prog *tgt_prog, | |
18315 | u32 btf_id, | |
18316 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 18317 | { |
be8704ff | 18318 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 18319 | const char prefix[] = "btf_trace_"; |
5b92a28a | 18320 | int ret = 0, subprog = -1, i; |
38207291 | 18321 | const struct btf_type *t; |
5b92a28a | 18322 | bool conservative = true; |
38207291 | 18323 | const char *tname; |
5b92a28a | 18324 | struct btf *btf; |
f7b12b6f | 18325 | long addr = 0; |
31bf1dbc | 18326 | struct module *mod = NULL; |
38207291 | 18327 | |
f1b9509c | 18328 | if (!btf_id) { |
efc68158 | 18329 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
18330 | return -EINVAL; |
18331 | } | |
22dc4a0f | 18332 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 18333 | if (!btf) { |
efc68158 | 18334 | bpf_log(log, |
5b92a28a AS |
18335 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
18336 | return -EINVAL; | |
18337 | } | |
18338 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 18339 | if (!t) { |
efc68158 | 18340 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
18341 | return -EINVAL; |
18342 | } | |
5b92a28a | 18343 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 18344 | if (!tname) { |
efc68158 | 18345 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
18346 | return -EINVAL; |
18347 | } | |
5b92a28a AS |
18348 | if (tgt_prog) { |
18349 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
18350 | ||
fd7c211d THJ |
18351 | if (bpf_prog_is_dev_bound(prog->aux) && |
18352 | !bpf_prog_dev_bound_match(prog, tgt_prog)) { | |
18353 | bpf_log(log, "Target program bound device mismatch"); | |
3d76a4d3 SF |
18354 | return -EINVAL; |
18355 | } | |
18356 | ||
5b92a28a AS |
18357 | for (i = 0; i < aux->func_info_cnt; i++) |
18358 | if (aux->func_info[i].type_id == btf_id) { | |
18359 | subprog = i; | |
18360 | break; | |
18361 | } | |
18362 | if (subprog == -1) { | |
efc68158 | 18363 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
18364 | return -EINVAL; |
18365 | } | |
18366 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
18367 | if (prog_extension) { |
18368 | if (conservative) { | |
efc68158 | 18369 | bpf_log(log, |
be8704ff AS |
18370 | "Cannot replace static functions\n"); |
18371 | return -EINVAL; | |
18372 | } | |
18373 | if (!prog->jit_requested) { | |
efc68158 | 18374 | bpf_log(log, |
be8704ff AS |
18375 | "Extension programs should be JITed\n"); |
18376 | return -EINVAL; | |
18377 | } | |
be8704ff AS |
18378 | } |
18379 | if (!tgt_prog->jited) { | |
efc68158 | 18380 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
18381 | return -EINVAL; |
18382 | } | |
18383 | if (tgt_prog->type == prog->type) { | |
18384 | /* Cannot fentry/fexit another fentry/fexit program. | |
18385 | * Cannot attach program extension to another extension. | |
18386 | * It's ok to attach fentry/fexit to extension program. | |
18387 | */ | |
efc68158 | 18388 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
18389 | return -EINVAL; |
18390 | } | |
18391 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
18392 | prog_extension && | |
18393 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
18394 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
18395 | /* Program extensions can extend all program types | |
18396 | * except fentry/fexit. The reason is the following. | |
18397 | * The fentry/fexit programs are used for performance | |
18398 | * analysis, stats and can be attached to any program | |
18399 | * type except themselves. When extension program is | |
18400 | * replacing XDP function it is necessary to allow | |
18401 | * performance analysis of all functions. Both original | |
18402 | * XDP program and its program extension. Hence | |
18403 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
18404 | * allowed. If extending of fentry/fexit was allowed it | |
18405 | * would be possible to create long call chain | |
18406 | * fentry->extension->fentry->extension beyond | |
18407 | * reasonable stack size. Hence extending fentry is not | |
18408 | * allowed. | |
18409 | */ | |
efc68158 | 18410 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
18411 | return -EINVAL; |
18412 | } | |
5b92a28a | 18413 | } else { |
be8704ff | 18414 | if (prog_extension) { |
efc68158 | 18415 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
18416 | return -EINVAL; |
18417 | } | |
5b92a28a | 18418 | } |
f1b9509c AS |
18419 | |
18420 | switch (prog->expected_attach_type) { | |
18421 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 18422 | if (tgt_prog) { |
efc68158 | 18423 | bpf_log(log, |
5b92a28a AS |
18424 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
18425 | return -EINVAL; | |
18426 | } | |
38207291 | 18427 | if (!btf_type_is_typedef(t)) { |
efc68158 | 18428 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
18429 | btf_id); |
18430 | return -EINVAL; | |
18431 | } | |
f1b9509c | 18432 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 18433 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
18434 | btf_id, tname); |
18435 | return -EINVAL; | |
18436 | } | |
18437 | tname += sizeof(prefix) - 1; | |
5b92a28a | 18438 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
18439 | if (!btf_type_is_ptr(t)) |
18440 | /* should never happen in valid vmlinux build */ | |
18441 | return -EINVAL; | |
5b92a28a | 18442 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
18443 | if (!btf_type_is_func_proto(t)) |
18444 | /* should never happen in valid vmlinux build */ | |
18445 | return -EINVAL; | |
18446 | ||
f7b12b6f | 18447 | break; |
15d83c4d YS |
18448 | case BPF_TRACE_ITER: |
18449 | if (!btf_type_is_func(t)) { | |
efc68158 | 18450 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
18451 | btf_id); |
18452 | return -EINVAL; | |
18453 | } | |
18454 | t = btf_type_by_id(btf, t->type); | |
18455 | if (!btf_type_is_func_proto(t)) | |
18456 | return -EINVAL; | |
f7b12b6f THJ |
18457 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
18458 | if (ret) | |
18459 | return ret; | |
18460 | break; | |
be8704ff AS |
18461 | default: |
18462 | if (!prog_extension) | |
18463 | return -EINVAL; | |
df561f66 | 18464 | fallthrough; |
ae240823 | 18465 | case BPF_MODIFY_RETURN: |
9e4e01df | 18466 | case BPF_LSM_MAC: |
69fd337a | 18467 | case BPF_LSM_CGROUP: |
fec56f58 AS |
18468 | case BPF_TRACE_FENTRY: |
18469 | case BPF_TRACE_FEXIT: | |
18470 | if (!btf_type_is_func(t)) { | |
efc68158 | 18471 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
18472 | btf_id); |
18473 | return -EINVAL; | |
18474 | } | |
be8704ff | 18475 | if (prog_extension && |
efc68158 | 18476 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 18477 | return -EINVAL; |
5b92a28a | 18478 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
18479 | if (!btf_type_is_func_proto(t)) |
18480 | return -EINVAL; | |
f7b12b6f | 18481 | |
4a1e7c0c THJ |
18482 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
18483 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
18484 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
18485 | return -EINVAL; | |
18486 | ||
f7b12b6f | 18487 | if (tgt_prog && conservative) |
5b92a28a | 18488 | t = NULL; |
f7b12b6f THJ |
18489 | |
18490 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 18491 | if (ret < 0) |
f7b12b6f THJ |
18492 | return ret; |
18493 | ||
5b92a28a | 18494 | if (tgt_prog) { |
e9eeec58 YS |
18495 | if (subprog == 0) |
18496 | addr = (long) tgt_prog->bpf_func; | |
18497 | else | |
18498 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a | 18499 | } else { |
31bf1dbc VM |
18500 | if (btf_is_module(btf)) { |
18501 | mod = btf_try_get_module(btf); | |
18502 | if (mod) | |
18503 | addr = find_kallsyms_symbol_value(mod, tname); | |
18504 | else | |
18505 | addr = 0; | |
18506 | } else { | |
18507 | addr = kallsyms_lookup_name(tname); | |
18508 | } | |
5b92a28a | 18509 | if (!addr) { |
31bf1dbc | 18510 | module_put(mod); |
efc68158 | 18511 | bpf_log(log, |
5b92a28a AS |
18512 | "The address of function %s cannot be found\n", |
18513 | tname); | |
f7b12b6f | 18514 | return -ENOENT; |
5b92a28a | 18515 | } |
fec56f58 | 18516 | } |
18644cec | 18517 | |
1e6c62a8 AS |
18518 | if (prog->aux->sleepable) { |
18519 | ret = -EINVAL; | |
18520 | switch (prog->type) { | |
18521 | case BPF_PROG_TYPE_TRACING: | |
5b481aca BT |
18522 | |
18523 | /* fentry/fexit/fmod_ret progs can be sleepable if they are | |
1e6c62a8 AS |
18524 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
18525 | */ | |
18526 | if (!check_non_sleepable_error_inject(btf_id) && | |
18527 | within_error_injection_list(addr)) | |
18528 | ret = 0; | |
5b481aca BT |
18529 | /* fentry/fexit/fmod_ret progs can also be sleepable if they are |
18530 | * in the fmodret id set with the KF_SLEEPABLE flag. | |
18531 | */ | |
18532 | else { | |
18533 | u32 *flags = btf_kfunc_is_modify_return(btf, btf_id); | |
18534 | ||
18535 | if (flags && (*flags & KF_SLEEPABLE)) | |
18536 | ret = 0; | |
18537 | } | |
1e6c62a8 AS |
18538 | break; |
18539 | case BPF_PROG_TYPE_LSM: | |
18540 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
18541 | * Only some of them are sleepable. | |
18542 | */ | |
423f1610 | 18543 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
18544 | ret = 0; |
18545 | break; | |
18546 | default: | |
18547 | break; | |
18548 | } | |
f7b12b6f | 18549 | if (ret) { |
31bf1dbc | 18550 | module_put(mod); |
f7b12b6f THJ |
18551 | bpf_log(log, "%s is not sleepable\n", tname); |
18552 | return ret; | |
18553 | } | |
1e6c62a8 | 18554 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 18555 | if (tgt_prog) { |
31bf1dbc | 18556 | module_put(mod); |
efc68158 | 18557 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
18558 | return -EINVAL; |
18559 | } | |
5b481aca BT |
18560 | ret = -EINVAL; |
18561 | if (btf_kfunc_is_modify_return(btf, btf_id) || | |
18562 | !check_attach_modify_return(addr, tname)) | |
18563 | ret = 0; | |
f7b12b6f | 18564 | if (ret) { |
31bf1dbc | 18565 | module_put(mod); |
f7b12b6f THJ |
18566 | bpf_log(log, "%s() is not modifiable\n", tname); |
18567 | return ret; | |
1af9270e | 18568 | } |
18644cec | 18569 | } |
f7b12b6f THJ |
18570 | |
18571 | break; | |
18572 | } | |
18573 | tgt_info->tgt_addr = addr; | |
18574 | tgt_info->tgt_name = tname; | |
18575 | tgt_info->tgt_type = t; | |
31bf1dbc | 18576 | tgt_info->tgt_mod = mod; |
f7b12b6f THJ |
18577 | return 0; |
18578 | } | |
18579 | ||
35e3815f JO |
18580 | BTF_SET_START(btf_id_deny) |
18581 | BTF_ID_UNUSED | |
18582 | #ifdef CONFIG_SMP | |
18583 | BTF_ID(func, migrate_disable) | |
18584 | BTF_ID(func, migrate_enable) | |
18585 | #endif | |
18586 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
18587 | BTF_ID(func, rcu_read_unlock_strict) | |
18588 | #endif | |
18589 | BTF_SET_END(btf_id_deny) | |
18590 | ||
700e6f85 JO |
18591 | static bool can_be_sleepable(struct bpf_prog *prog) |
18592 | { | |
18593 | if (prog->type == BPF_PROG_TYPE_TRACING) { | |
18594 | switch (prog->expected_attach_type) { | |
18595 | case BPF_TRACE_FENTRY: | |
18596 | case BPF_TRACE_FEXIT: | |
18597 | case BPF_MODIFY_RETURN: | |
18598 | case BPF_TRACE_ITER: | |
18599 | return true; | |
18600 | default: | |
18601 | return false; | |
18602 | } | |
18603 | } | |
18604 | return prog->type == BPF_PROG_TYPE_LSM || | |
1e12d3ef DV |
18605 | prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ || |
18606 | prog->type == BPF_PROG_TYPE_STRUCT_OPS; | |
700e6f85 JO |
18607 | } |
18608 | ||
f7b12b6f THJ |
18609 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
18610 | { | |
18611 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 18612 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
18613 | struct bpf_attach_target_info tgt_info = {}; |
18614 | u32 btf_id = prog->aux->attach_btf_id; | |
18615 | struct bpf_trampoline *tr; | |
18616 | int ret; | |
18617 | u64 key; | |
18618 | ||
79a7f8bd AS |
18619 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
18620 | if (prog->aux->sleepable) | |
18621 | /* attach_btf_id checked to be zero already */ | |
18622 | return 0; | |
18623 | verbose(env, "Syscall programs can only be sleepable\n"); | |
18624 | return -EINVAL; | |
18625 | } | |
18626 | ||
700e6f85 | 18627 | if (prog->aux->sleepable && !can_be_sleepable(prog)) { |
1e12d3ef | 18628 | verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n"); |
f7b12b6f THJ |
18629 | return -EINVAL; |
18630 | } | |
18631 | ||
18632 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
18633 | return check_struct_ops_btf_id(env); | |
18634 | ||
18635 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
18636 | prog->type != BPF_PROG_TYPE_LSM && | |
18637 | prog->type != BPF_PROG_TYPE_EXT) | |
18638 | return 0; | |
18639 | ||
18640 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
18641 | if (ret) | |
fec56f58 | 18642 | return ret; |
f7b12b6f THJ |
18643 | |
18644 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
18645 | /* to make freplace equivalent to their targets, they need to |
18646 | * inherit env->ops and expected_attach_type for the rest of the | |
18647 | * verification | |
18648 | */ | |
f7b12b6f THJ |
18649 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
18650 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
18651 | } | |
18652 | ||
18653 | /* store info about the attachment target that will be used later */ | |
18654 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
18655 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
31bf1dbc | 18656 | prog->aux->mod = tgt_info.tgt_mod; |
f7b12b6f | 18657 | |
4a1e7c0c THJ |
18658 | if (tgt_prog) { |
18659 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
18660 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
18661 | } | |
18662 | ||
f7b12b6f THJ |
18663 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
18664 | prog->aux->attach_btf_trace = true; | |
18665 | return 0; | |
18666 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
18667 | if (!bpf_iter_prog_supported(prog)) | |
18668 | return -EINVAL; | |
18669 | return 0; | |
18670 | } | |
18671 | ||
18672 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
18673 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
18674 | if (ret < 0) | |
18675 | return ret; | |
35e3815f JO |
18676 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
18677 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
18678 | return -EINVAL; | |
38207291 | 18679 | } |
f7b12b6f | 18680 | |
22dc4a0f | 18681 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
18682 | tr = bpf_trampoline_get(key, &tgt_info); |
18683 | if (!tr) | |
18684 | return -ENOMEM; | |
18685 | ||
3aac1ead | 18686 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 18687 | return 0; |
38207291 MKL |
18688 | } |
18689 | ||
76654e67 AM |
18690 | struct btf *bpf_get_btf_vmlinux(void) |
18691 | { | |
18692 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
18693 | mutex_lock(&bpf_verifier_lock); | |
18694 | if (!btf_vmlinux) | |
18695 | btf_vmlinux = btf_parse_vmlinux(); | |
18696 | mutex_unlock(&bpf_verifier_lock); | |
18697 | } | |
18698 | return btf_vmlinux; | |
18699 | } | |
18700 | ||
af2ac3e1 | 18701 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 18702 | { |
06ee7115 | 18703 | u64 start_time = ktime_get_ns(); |
58e2af8b | 18704 | struct bpf_verifier_env *env; |
b9193c1b | 18705 | struct bpf_verifier_log *log; |
9e4c24e7 | 18706 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 18707 | bool is_priv; |
51580e79 | 18708 | |
eba0c929 AB |
18709 | /* no program is valid */ |
18710 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
18711 | return -EINVAL; | |
18712 | ||
58e2af8b | 18713 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
18714 | * allocate/free it every time bpf_check() is called |
18715 | */ | |
58e2af8b | 18716 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
18717 | if (!env) |
18718 | return -ENOMEM; | |
61bd5218 | 18719 | log = &env->log; |
cbd35700 | 18720 | |
9e4c24e7 | 18721 | len = (*prog)->len; |
fad953ce | 18722 | env->insn_aux_data = |
9e4c24e7 | 18723 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
18724 | ret = -ENOMEM; |
18725 | if (!env->insn_aux_data) | |
18726 | goto err_free_env; | |
9e4c24e7 JK |
18727 | for (i = 0; i < len; i++) |
18728 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 18729 | env->prog = *prog; |
00176a34 | 18730 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 18731 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 18732 | is_priv = bpf_capable(); |
0246e64d | 18733 | |
76654e67 | 18734 | bpf_get_btf_vmlinux(); |
8580ac94 | 18735 | |
cbd35700 | 18736 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
18737 | if (!is_priv) |
18738 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
18739 | |
18740 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
18741 | /* user requested verbose verifier output | |
18742 | * and supplied buffer to store the verification trace | |
18743 | */ | |
e7bf8249 JK |
18744 | log->level = attr->log_level; |
18745 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
18746 | log->len_total = attr->log_size; | |
cbd35700 | 18747 | |
e7bf8249 | 18748 | /* log attributes have to be sane */ |
866de407 HT |
18749 | if (!bpf_verifier_log_attr_valid(log)) { |
18750 | ret = -EINVAL; | |
3df126f3 | 18751 | goto err_unlock; |
866de407 | 18752 | } |
cbd35700 | 18753 | } |
1ad2f583 | 18754 | |
0f55f9ed CL |
18755 | mark_verifier_state_clean(env); |
18756 | ||
8580ac94 AS |
18757 | if (IS_ERR(btf_vmlinux)) { |
18758 | /* Either gcc or pahole or kernel are broken. */ | |
18759 | verbose(env, "in-kernel BTF is malformed\n"); | |
18760 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 18761 | goto skip_full_check; |
8580ac94 AS |
18762 | } |
18763 | ||
1ad2f583 DB |
18764 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
18765 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 18766 | env->strict_alignment = true; |
e9ee9efc DM |
18767 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
18768 | env->strict_alignment = false; | |
cbd35700 | 18769 | |
2c78ee89 | 18770 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 18771 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
2c78ee89 AS |
18772 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
18773 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
18774 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 18775 | |
10d274e8 AS |
18776 | if (is_priv) |
18777 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
18778 | ||
dc2a4ebc | 18779 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 18780 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
18781 | GFP_USER); |
18782 | ret = -ENOMEM; | |
18783 | if (!env->explored_states) | |
18784 | goto skip_full_check; | |
18785 | ||
e6ac2450 MKL |
18786 | ret = add_subprog_and_kfunc(env); |
18787 | if (ret < 0) | |
18788 | goto skip_full_check; | |
18789 | ||
d9762e84 | 18790 | ret = check_subprogs(env); |
475fb78f AS |
18791 | if (ret < 0) |
18792 | goto skip_full_check; | |
18793 | ||
c454a46b | 18794 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
18795 | if (ret < 0) |
18796 | goto skip_full_check; | |
18797 | ||
be8704ff AS |
18798 | ret = check_attach_btf_id(env); |
18799 | if (ret) | |
18800 | goto skip_full_check; | |
18801 | ||
4976b718 HL |
18802 | ret = resolve_pseudo_ldimm64(env); |
18803 | if (ret < 0) | |
18804 | goto skip_full_check; | |
18805 | ||
9d03ebc7 | 18806 | if (bpf_prog_is_offloaded(env->prog->aux)) { |
ceb11679 YZ |
18807 | ret = bpf_prog_offload_verifier_prep(env->prog); |
18808 | if (ret) | |
18809 | goto skip_full_check; | |
18810 | } | |
18811 | ||
d9762e84 MKL |
18812 | ret = check_cfg(env); |
18813 | if (ret < 0) | |
18814 | goto skip_full_check; | |
18815 | ||
51c39bb1 AS |
18816 | ret = do_check_subprogs(env); |
18817 | ret = ret ?: do_check_main(env); | |
cbd35700 | 18818 | |
9d03ebc7 | 18819 | if (ret == 0 && bpf_prog_is_offloaded(env->prog->aux)) |
c941ce9c QM |
18820 | ret = bpf_prog_offload_finalize(env); |
18821 | ||
0246e64d | 18822 | skip_full_check: |
51c39bb1 | 18823 | kvfree(env->explored_states); |
0246e64d | 18824 | |
c131187d | 18825 | if (ret == 0) |
9b38c405 | 18826 | ret = check_max_stack_depth(env); |
c131187d | 18827 | |
9b38c405 | 18828 | /* instruction rewrites happen after this point */ |
1ade2371 EZ |
18829 | if (ret == 0) |
18830 | ret = optimize_bpf_loop(env); | |
18831 | ||
e2ae4ca2 JK |
18832 | if (is_priv) { |
18833 | if (ret == 0) | |
18834 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
18835 | if (ret == 0) |
18836 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
18837 | if (ret == 0) |
18838 | ret = opt_remove_nops(env); | |
52875a04 JK |
18839 | } else { |
18840 | if (ret == 0) | |
18841 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
18842 | } |
18843 | ||
9bac3d6d AS |
18844 | if (ret == 0) |
18845 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
18846 | ret = convert_ctx_accesses(env); | |
18847 | ||
e245c5c6 | 18848 | if (ret == 0) |
e6ac5933 | 18849 | ret = do_misc_fixups(env); |
e245c5c6 | 18850 | |
a4b1d3c1 JW |
18851 | /* do 32-bit optimization after insn patching has done so those patched |
18852 | * insns could be handled correctly. | |
18853 | */ | |
9d03ebc7 | 18854 | if (ret == 0 && !bpf_prog_is_offloaded(env->prog->aux)) { |
d6c2308c JW |
18855 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
18856 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
18857 | : false; | |
a4b1d3c1 JW |
18858 | } |
18859 | ||
1ea47e01 AS |
18860 | if (ret == 0) |
18861 | ret = fixup_call_args(env); | |
18862 | ||
06ee7115 AS |
18863 | env->verification_time = ktime_get_ns() - start_time; |
18864 | print_verification_stats(env); | |
aba64c7d | 18865 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 18866 | |
a2a7d570 | 18867 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 18868 | ret = -ENOSPC; |
a2a7d570 | 18869 | if (log->level && !log->ubuf) { |
cbd35700 | 18870 | ret = -EFAULT; |
a2a7d570 | 18871 | goto err_release_maps; |
cbd35700 AS |
18872 | } |
18873 | ||
541c3bad AN |
18874 | if (ret) |
18875 | goto err_release_maps; | |
18876 | ||
18877 | if (env->used_map_cnt) { | |
0246e64d | 18878 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
18879 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
18880 | sizeof(env->used_maps[0]), | |
18881 | GFP_KERNEL); | |
0246e64d | 18882 | |
9bac3d6d | 18883 | if (!env->prog->aux->used_maps) { |
0246e64d | 18884 | ret = -ENOMEM; |
a2a7d570 | 18885 | goto err_release_maps; |
0246e64d AS |
18886 | } |
18887 | ||
9bac3d6d | 18888 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 18889 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 18890 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
18891 | } |
18892 | if (env->used_btf_cnt) { | |
18893 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
18894 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
18895 | sizeof(env->used_btfs[0]), | |
18896 | GFP_KERNEL); | |
18897 | if (!env->prog->aux->used_btfs) { | |
18898 | ret = -ENOMEM; | |
18899 | goto err_release_maps; | |
18900 | } | |
0246e64d | 18901 | |
541c3bad AN |
18902 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
18903 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
18904 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
18905 | } | |
18906 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
18907 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
18908 | * bpf_ld_imm64 instructions | |
18909 | */ | |
18910 | convert_pseudo_ld_imm64(env); | |
18911 | } | |
cbd35700 | 18912 | |
541c3bad | 18913 | adjust_btf_func(env); |
ba64e7d8 | 18914 | |
a2a7d570 | 18915 | err_release_maps: |
9bac3d6d | 18916 | if (!env->prog->aux->used_maps) |
0246e64d | 18917 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 18918 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
18919 | */ |
18920 | release_maps(env); | |
541c3bad AN |
18921 | if (!env->prog->aux->used_btfs) |
18922 | release_btfs(env); | |
03f87c0b THJ |
18923 | |
18924 | /* extension progs temporarily inherit the attach_type of their targets | |
18925 | for verification purposes, so set it back to zero before returning | |
18926 | */ | |
18927 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
18928 | env->prog->expected_attach_type = 0; | |
18929 | ||
9bac3d6d | 18930 | *prog = env->prog; |
3df126f3 | 18931 | err_unlock: |
45a73c17 AS |
18932 | if (!is_priv) |
18933 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
18934 | vfree(env->insn_aux_data); |
18935 | err_free_env: | |
18936 | kfree(env); | |
51580e79 AS |
18937 | return ret; |
18938 | } |