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> |
51580e79 | 27 | |
f4ac7e0b JK |
28 | #include "disasm.h" |
29 | ||
00176a34 | 30 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 31 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
32 | [_id] = & _name ## _verifier_ops, |
33 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 34 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
35 | #include <linux/bpf_types.h> |
36 | #undef BPF_PROG_TYPE | |
37 | #undef BPF_MAP_TYPE | |
f2e10bff | 38 | #undef BPF_LINK_TYPE |
00176a34 JK |
39 | }; |
40 | ||
51580e79 AS |
41 | /* bpf_check() is a static code analyzer that walks eBPF program |
42 | * instruction by instruction and updates register/stack state. | |
43 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
44 | * | |
45 | * The first pass is depth-first-search to check that the program is a DAG. | |
46 | * It rejects the following programs: | |
47 | * - larger than BPF_MAXINSNS insns | |
48 | * - if loop is present (detected via back-edge) | |
49 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
50 | * - out of bounds or malformed jumps | |
51 | * The second pass is all possible path descent from the 1st insn. | |
8fb33b60 | 52 | * Since it's analyzing all paths through the program, the length of the |
eba38a96 | 53 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
54 | * insn is less then 4K, but there are too many branches that change stack/regs. |
55 | * Number of 'branches to be analyzed' is limited to 1k | |
56 | * | |
57 | * On entry to each instruction, each register has a type, and the instruction | |
58 | * changes the types of the registers depending on instruction semantics. | |
59 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
60 | * copied to R1. | |
61 | * | |
62 | * All registers are 64-bit. | |
63 | * R0 - return register | |
64 | * R1-R5 argument passing registers | |
65 | * R6-R9 callee saved registers | |
66 | * R10 - frame pointer read-only | |
67 | * | |
68 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
69 | * and has type PTR_TO_CTX. | |
70 | * | |
71 | * Verifier tracks arithmetic operations on pointers in case: | |
72 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
73 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
74 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
75 | * and 2nd arithmetic instruction is pattern matched to recognize | |
76 | * that it wants to construct a pointer to some element within stack. | |
77 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
78 | * (and -20 constant is saved for further stack bounds checking). | |
79 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
80 | * | |
f1174f77 | 81 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 82 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 83 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
84 | * |
85 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
86 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
87 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
88 | * |
89 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
90 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
91 | * | |
92 | * registers used to pass values to function calls are checked against | |
93 | * function argument constraints. | |
94 | * | |
95 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
96 | * It means that the register type passed to this function must be | |
97 | * PTR_TO_STACK and it will be used inside the function as | |
98 | * 'pointer to map element key' | |
99 | * | |
100 | * For example the argument constraints for bpf_map_lookup_elem(): | |
101 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
102 | * .arg1_type = ARG_CONST_MAP_PTR, | |
103 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
104 | * | |
105 | * ret_type says that this function returns 'pointer to map elem value or null' | |
106 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
107 | * 2nd argument should be a pointer to stack, which will be used inside | |
108 | * the helper function as a pointer to map element key. | |
109 | * | |
110 | * On the kernel side the helper function looks like: | |
111 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
112 | * { | |
113 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
114 | * void *key = (void *) (unsigned long) r2; | |
115 | * void *value; | |
116 | * | |
117 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
118 | * [key, key + map->key_size) bytes are valid and were initialized on | |
119 | * the stack of eBPF program. | |
120 | * } | |
121 | * | |
122 | * Corresponding eBPF program may look like: | |
123 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
124 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
125 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
126 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
127 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
128 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
129 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
130 | * | |
131 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
132 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
133 | * and were initialized prior to this call. | |
134 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
135 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
136 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
8fb33b60 | 137 | * returns either pointer to map value or NULL. |
51580e79 AS |
138 | * |
139 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
140 | * insn, the register holding that pointer in the true branch changes state to | |
141 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
142 | * branch. See check_cond_jmp_op(). | |
143 | * | |
144 | * After the call R0 is set to return type of the function and registers R1-R5 | |
145 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
146 | * |
147 | * The following reference types represent a potential reference to a kernel | |
148 | * resource which, after first being allocated, must be checked and freed by | |
149 | * the BPF program: | |
150 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
151 | * | |
152 | * When the verifier sees a helper call return a reference type, it allocates a | |
153 | * pointer id for the reference and stores it in the current function state. | |
154 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
155 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
156 | * passes through a NULL-check conditional. For the branch wherein the state is | |
157 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
158 | * |
159 | * For each helper function that allocates a reference, such as | |
160 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
161 | * bpf_sk_release(). When a reference type passes into the release function, | |
162 | * the verifier also releases the reference. If any unchecked or unreleased | |
163 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
164 | */ |
165 | ||
17a52670 | 166 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 167 | struct bpf_verifier_stack_elem { |
17a52670 AS |
168 | /* verifer state is 'st' |
169 | * before processing instruction 'insn_idx' | |
170 | * and after processing instruction 'prev_insn_idx' | |
171 | */ | |
58e2af8b | 172 | struct bpf_verifier_state st; |
17a52670 AS |
173 | int insn_idx; |
174 | int prev_insn_idx; | |
58e2af8b | 175 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
176 | /* length of verifier log at the time this state was pushed on stack */ |
177 | u32 log_pos; | |
cbd35700 AS |
178 | }; |
179 | ||
b285fcb7 | 180 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 181 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 182 | |
d2e4c1e6 DB |
183 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
184 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
185 | ||
c93552c4 DB |
186 | #define BPF_MAP_PTR_UNPRIV 1UL |
187 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
188 | POISON_POINTER_DELTA)) | |
189 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
190 | ||
bc34dee6 JK |
191 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx); |
192 | static int release_reference(struct bpf_verifier_env *env, int ref_obj_id); | |
6a3cd331 | 193 | static void invalidate_non_owning_refs(struct bpf_verifier_env *env); |
5d92ddc3 | 194 | static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env); |
6a3cd331 DM |
195 | static int ref_set_non_owning(struct bpf_verifier_env *env, |
196 | struct bpf_reg_state *reg); | |
bc34dee6 | 197 | |
c93552c4 DB |
198 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
199 | { | |
d2e4c1e6 | 200 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
201 | } |
202 | ||
203 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
204 | { | |
d2e4c1e6 | 205 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
206 | } |
207 | ||
208 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
209 | const struct bpf_map *map, bool unpriv) | |
210 | { | |
211 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
212 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
213 | aux->map_ptr_state = (unsigned long)map | |
214 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
215 | } | |
216 | ||
217 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
218 | { | |
219 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
220 | } | |
221 | ||
222 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
223 | { | |
224 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
225 | } | |
226 | ||
227 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
228 | { | |
229 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
230 | } | |
231 | ||
232 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
233 | { | |
234 | bool poisoned = bpf_map_key_poisoned(aux); | |
235 | ||
236 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
237 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 238 | } |
fad73a1a | 239 | |
23a2d70c YS |
240 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
241 | { | |
242 | return insn->code == (BPF_JMP | BPF_CALL) && | |
243 | insn->src_reg == BPF_PSEUDO_CALL; | |
244 | } | |
245 | ||
e6ac2450 MKL |
246 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
247 | { | |
248 | return insn->code == (BPF_JMP | BPF_CALL) && | |
249 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; | |
250 | } | |
251 | ||
33ff9823 DB |
252 | struct bpf_call_arg_meta { |
253 | struct bpf_map *map_ptr; | |
435faee1 | 254 | bool raw_mode; |
36bbef52 | 255 | bool pkt_access; |
8f14852e | 256 | u8 release_regno; |
435faee1 DB |
257 | int regno; |
258 | int access_size; | |
457f4436 | 259 | int mem_size; |
10060503 | 260 | u64 msize_max_value; |
1b986589 | 261 | int ref_obj_id; |
f8064ab9 | 262 | int dynptr_id; |
3e8ce298 | 263 | int map_uid; |
d83525ca | 264 | int func_id; |
22dc4a0f | 265 | struct btf *btf; |
eaa6bcb7 | 266 | u32 btf_id; |
22dc4a0f | 267 | struct btf *ret_btf; |
eaa6bcb7 | 268 | u32 ret_btf_id; |
69c087ba | 269 | u32 subprogno; |
aa3496ac | 270 | struct btf_field *kptr_field; |
33ff9823 DB |
271 | }; |
272 | ||
d0e1ac22 AN |
273 | struct bpf_kfunc_call_arg_meta { |
274 | /* In parameters */ | |
275 | struct btf *btf; | |
276 | u32 func_id; | |
277 | u32 kfunc_flags; | |
278 | const struct btf_type *func_proto; | |
279 | const char *func_name; | |
280 | /* Out parameters */ | |
281 | u32 ref_obj_id; | |
282 | u8 release_regno; | |
283 | bool r0_rdonly; | |
284 | u32 ret_btf_id; | |
285 | u64 r0_size; | |
286 | u32 subprogno; | |
287 | struct { | |
288 | u64 value; | |
289 | bool found; | |
290 | } arg_constant; | |
291 | struct { | |
292 | struct btf *btf; | |
293 | u32 btf_id; | |
294 | } arg_obj_drop; | |
295 | struct { | |
296 | struct btf_field *field; | |
297 | } arg_list_head; | |
298 | struct { | |
299 | struct btf_field *field; | |
300 | } arg_rbtree_root; | |
301 | struct { | |
302 | enum bpf_dynptr_type type; | |
303 | u32 id; | |
304 | } initialized_dynptr; | |
06accc87 AN |
305 | struct { |
306 | u8 spi; | |
307 | u8 frameno; | |
308 | } iter; | |
d0e1ac22 AN |
309 | u64 mem_size; |
310 | }; | |
311 | ||
8580ac94 AS |
312 | struct btf *btf_vmlinux; |
313 | ||
cbd35700 AS |
314 | static DEFINE_MUTEX(bpf_verifier_lock); |
315 | ||
d9762e84 MKL |
316 | static const struct bpf_line_info * |
317 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
318 | { | |
319 | const struct bpf_line_info *linfo; | |
320 | const struct bpf_prog *prog; | |
321 | u32 i, nr_linfo; | |
322 | ||
323 | prog = env->prog; | |
324 | nr_linfo = prog->aux->nr_linfo; | |
325 | ||
326 | if (!nr_linfo || insn_off >= prog->len) | |
327 | return NULL; | |
328 | ||
329 | linfo = prog->aux->linfo; | |
330 | for (i = 1; i < nr_linfo; i++) | |
331 | if (insn_off < linfo[i].insn_off) | |
332 | break; | |
333 | ||
334 | return &linfo[i - 1]; | |
335 | } | |
336 | ||
77d2e05a MKL |
337 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
338 | va_list args) | |
cbd35700 | 339 | { |
a2a7d570 | 340 | unsigned int n; |
cbd35700 | 341 | |
a2a7d570 | 342 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
343 | |
344 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
345 | "verifier log line truncated - local buffer too short\n"); | |
346 | ||
8580ac94 | 347 | if (log->level == BPF_LOG_KERNEL) { |
436d404c HT |
348 | bool newline = n > 0 && log->kbuf[n - 1] == '\n'; |
349 | ||
350 | pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); | |
8580ac94 AS |
351 | return; |
352 | } | |
436d404c HT |
353 | |
354 | n = min(log->len_total - log->len_used - 1, n); | |
355 | log->kbuf[n] = '\0'; | |
a2a7d570 JK |
356 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
357 | log->len_used += n; | |
358 | else | |
359 | log->ubuf = NULL; | |
cbd35700 | 360 | } |
abe08840 | 361 | |
6f8a57cc AN |
362 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
363 | { | |
364 | char zero = 0; | |
365 | ||
366 | if (!bpf_verifier_log_needed(log)) | |
367 | return; | |
368 | ||
369 | log->len_used = new_pos; | |
370 | if (put_user(zero, log->ubuf + new_pos)) | |
371 | log->ubuf = NULL; | |
372 | } | |
373 | ||
abe08840 JO |
374 | /* log_level controls verbosity level of eBPF verifier. |
375 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
376 | * so the user can figure out what's wrong with the program | |
430e68d1 | 377 | */ |
abe08840 JO |
378 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
379 | const char *fmt, ...) | |
380 | { | |
381 | va_list args; | |
382 | ||
77d2e05a MKL |
383 | if (!bpf_verifier_log_needed(&env->log)) |
384 | return; | |
385 | ||
abe08840 | 386 | va_start(args, fmt); |
77d2e05a | 387 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
388 | va_end(args); |
389 | } | |
390 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
391 | ||
392 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
393 | { | |
77d2e05a | 394 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
395 | va_list args; |
396 | ||
77d2e05a MKL |
397 | if (!bpf_verifier_log_needed(&env->log)) |
398 | return; | |
399 | ||
abe08840 | 400 | va_start(args, fmt); |
77d2e05a | 401 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
402 | va_end(args); |
403 | } | |
cbd35700 | 404 | |
9e15db66 AS |
405 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
406 | const char *fmt, ...) | |
407 | { | |
408 | va_list args; | |
409 | ||
410 | if (!bpf_verifier_log_needed(log)) | |
411 | return; | |
412 | ||
413 | va_start(args, fmt); | |
414 | bpf_verifier_vlog(log, fmt, args); | |
415 | va_end(args); | |
416 | } | |
84c6ac41 | 417 | EXPORT_SYMBOL_GPL(bpf_log); |
9e15db66 | 418 | |
d9762e84 MKL |
419 | static const char *ltrim(const char *s) |
420 | { | |
421 | while (isspace(*s)) | |
422 | s++; | |
423 | ||
424 | return s; | |
425 | } | |
426 | ||
427 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
428 | u32 insn_off, | |
429 | const char *prefix_fmt, ...) | |
430 | { | |
431 | const struct bpf_line_info *linfo; | |
432 | ||
433 | if (!bpf_verifier_log_needed(&env->log)) | |
434 | return; | |
435 | ||
436 | linfo = find_linfo(env, insn_off); | |
437 | if (!linfo || linfo == env->prev_linfo) | |
438 | return; | |
439 | ||
440 | if (prefix_fmt) { | |
441 | va_list args; | |
442 | ||
443 | va_start(args, prefix_fmt); | |
444 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
445 | va_end(args); | |
446 | } | |
447 | ||
448 | verbose(env, "%s\n", | |
449 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
450 | linfo->line_off))); | |
451 | ||
452 | env->prev_linfo = linfo; | |
453 | } | |
454 | ||
bc2591d6 YS |
455 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
456 | struct bpf_reg_state *reg, | |
457 | struct tnum *range, const char *ctx, | |
458 | const char *reg_name) | |
459 | { | |
460 | char tn_buf[48]; | |
461 | ||
462 | verbose(env, "At %s the register %s ", ctx, reg_name); | |
463 | if (!tnum_is_unknown(reg->var_off)) { | |
464 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
465 | verbose(env, "has value %s", tn_buf); | |
466 | } else { | |
467 | verbose(env, "has unknown scalar value"); | |
468 | } | |
469 | tnum_strn(tn_buf, sizeof(tn_buf), *range); | |
470 | verbose(env, " should have been in %s\n", tn_buf); | |
471 | } | |
472 | ||
de8f3a83 DB |
473 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
474 | { | |
0c9a7a7e | 475 | type = base_type(type); |
de8f3a83 DB |
476 | return type == PTR_TO_PACKET || |
477 | type == PTR_TO_PACKET_META; | |
478 | } | |
479 | ||
46f8bc92 MKL |
480 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
481 | { | |
482 | return type == PTR_TO_SOCKET || | |
655a51e5 | 483 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
484 | type == PTR_TO_TCP_SOCK || |
485 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
486 | } |
487 | ||
cac616db JF |
488 | static bool reg_type_not_null(enum bpf_reg_type type) |
489 | { | |
490 | return type == PTR_TO_SOCKET || | |
491 | type == PTR_TO_TCP_SOCK || | |
492 | type == PTR_TO_MAP_VALUE || | |
69c087ba | 493 | type == PTR_TO_MAP_KEY || |
d5271c5b AN |
494 | type == PTR_TO_SOCK_COMMON || |
495 | type == PTR_TO_MEM; | |
cac616db JF |
496 | } |
497 | ||
d8939cb0 DM |
498 | static bool type_is_ptr_alloc_obj(u32 type) |
499 | { | |
500 | return base_type(type) == PTR_TO_BTF_ID && type_flag(type) & MEM_ALLOC; | |
501 | } | |
502 | ||
6a3cd331 DM |
503 | static bool type_is_non_owning_ref(u32 type) |
504 | { | |
505 | return type_is_ptr_alloc_obj(type) && type_flag(type) & NON_OWN_REF; | |
506 | } | |
507 | ||
4e814da0 KKD |
508 | static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg) |
509 | { | |
510 | struct btf_record *rec = NULL; | |
511 | struct btf_struct_meta *meta; | |
512 | ||
513 | if (reg->type == PTR_TO_MAP_VALUE) { | |
514 | rec = reg->map_ptr->record; | |
d8939cb0 | 515 | } else if (type_is_ptr_alloc_obj(reg->type)) { |
4e814da0 KKD |
516 | meta = btf_find_struct_meta(reg->btf, reg->btf_id); |
517 | if (meta) | |
518 | rec = meta->record; | |
519 | } | |
520 | return rec; | |
521 | } | |
522 | ||
d83525ca AS |
523 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
524 | { | |
4e814da0 | 525 | return btf_record_has_field(reg_btf_record(reg), BPF_SPIN_LOCK); |
cba368c1 MKL |
526 | } |
527 | ||
20b2aff4 HL |
528 | static bool type_is_rdonly_mem(u32 type) |
529 | { | |
530 | return type & MEM_RDONLY; | |
cba368c1 MKL |
531 | } |
532 | ||
48946bd6 | 533 | static bool type_may_be_null(u32 type) |
fd1b0d60 | 534 | { |
48946bd6 | 535 | return type & PTR_MAYBE_NULL; |
fd1b0d60 LB |
536 | } |
537 | ||
64d85290 JS |
538 | static bool is_acquire_function(enum bpf_func_id func_id, |
539 | const struct bpf_map *map) | |
540 | { | |
541 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
542 | ||
543 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
544 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 | 545 | func_id == BPF_FUNC_skc_lookup_tcp || |
c0a5a21c KKD |
546 | func_id == BPF_FUNC_ringbuf_reserve || |
547 | func_id == BPF_FUNC_kptr_xchg) | |
64d85290 JS |
548 | return true; |
549 | ||
550 | if (func_id == BPF_FUNC_map_lookup_elem && | |
551 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
552 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
553 | return true; | |
554 | ||
555 | return false; | |
46f8bc92 MKL |
556 | } |
557 | ||
1b986589 MKL |
558 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
559 | { | |
560 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
561 | func_id == BPF_FUNC_sk_fullsock || |
562 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
563 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
564 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
3bc253c2 | 565 | func_id == BPF_FUNC_skc_to_mptcp_sock || |
1df8f55a MKL |
566 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
567 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
568 | } |
569 | ||
88374342 | 570 | static bool is_dynptr_ref_function(enum bpf_func_id func_id) |
b2d8ef19 DM |
571 | { |
572 | return func_id == BPF_FUNC_dynptr_data; | |
573 | } | |
574 | ||
be2ef816 AN |
575 | static bool is_callback_calling_function(enum bpf_func_id func_id) |
576 | { | |
577 | return func_id == BPF_FUNC_for_each_map_elem || | |
578 | func_id == BPF_FUNC_timer_set_callback || | |
579 | func_id == BPF_FUNC_find_vma || | |
580 | func_id == BPF_FUNC_loop || | |
581 | func_id == BPF_FUNC_user_ringbuf_drain; | |
582 | } | |
583 | ||
9bb00b28 YS |
584 | static bool is_storage_get_function(enum bpf_func_id func_id) |
585 | { | |
586 | return func_id == BPF_FUNC_sk_storage_get || | |
587 | func_id == BPF_FUNC_inode_storage_get || | |
588 | func_id == BPF_FUNC_task_storage_get || | |
589 | func_id == BPF_FUNC_cgrp_storage_get; | |
590 | } | |
591 | ||
b2d8ef19 DM |
592 | static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id, |
593 | const struct bpf_map *map) | |
594 | { | |
595 | int ref_obj_uses = 0; | |
596 | ||
597 | if (is_ptr_cast_function(func_id)) | |
598 | ref_obj_uses++; | |
599 | if (is_acquire_function(func_id, map)) | |
600 | ref_obj_uses++; | |
88374342 | 601 | if (is_dynptr_ref_function(func_id)) |
b2d8ef19 DM |
602 | ref_obj_uses++; |
603 | ||
604 | return ref_obj_uses > 1; | |
605 | } | |
606 | ||
39491867 BJ |
607 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
608 | { | |
609 | return BPF_CLASS(insn->code) == BPF_STX && | |
610 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
611 | insn->imm == BPF_CMPXCHG; | |
612 | } | |
613 | ||
c25b2ae1 HL |
614 | /* string representation of 'enum bpf_reg_type' |
615 | * | |
616 | * Note that reg_type_str() can not appear more than once in a single verbose() | |
617 | * statement. | |
618 | */ | |
619 | static const char *reg_type_str(struct bpf_verifier_env *env, | |
620 | enum bpf_reg_type type) | |
621 | { | |
ef66c547 | 622 | char postfix[16] = {0}, prefix[64] = {0}; |
c25b2ae1 HL |
623 | static const char * const str[] = { |
624 | [NOT_INIT] = "?", | |
7df5072c | 625 | [SCALAR_VALUE] = "scalar", |
c25b2ae1 HL |
626 | [PTR_TO_CTX] = "ctx", |
627 | [CONST_PTR_TO_MAP] = "map_ptr", | |
628 | [PTR_TO_MAP_VALUE] = "map_value", | |
629 | [PTR_TO_STACK] = "fp", | |
630 | [PTR_TO_PACKET] = "pkt", | |
631 | [PTR_TO_PACKET_META] = "pkt_meta", | |
632 | [PTR_TO_PACKET_END] = "pkt_end", | |
633 | [PTR_TO_FLOW_KEYS] = "flow_keys", | |
634 | [PTR_TO_SOCKET] = "sock", | |
635 | [PTR_TO_SOCK_COMMON] = "sock_common", | |
636 | [PTR_TO_TCP_SOCK] = "tcp_sock", | |
637 | [PTR_TO_TP_BUFFER] = "tp_buffer", | |
638 | [PTR_TO_XDP_SOCK] = "xdp_sock", | |
639 | [PTR_TO_BTF_ID] = "ptr_", | |
c25b2ae1 | 640 | [PTR_TO_MEM] = "mem", |
20b2aff4 | 641 | [PTR_TO_BUF] = "buf", |
c25b2ae1 HL |
642 | [PTR_TO_FUNC] = "func", |
643 | [PTR_TO_MAP_KEY] = "map_key", | |
27060531 | 644 | [CONST_PTR_TO_DYNPTR] = "dynptr_ptr", |
c25b2ae1 HL |
645 | }; |
646 | ||
647 | if (type & PTR_MAYBE_NULL) { | |
5844101a | 648 | if (base_type(type) == PTR_TO_BTF_ID) |
c25b2ae1 HL |
649 | strncpy(postfix, "or_null_", 16); |
650 | else | |
651 | strncpy(postfix, "_or_null", 16); | |
652 | } | |
653 | ||
9bb00b28 | 654 | snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s", |
ef66c547 DV |
655 | type & MEM_RDONLY ? "rdonly_" : "", |
656 | type & MEM_RINGBUF ? "ringbuf_" : "", | |
657 | type & MEM_USER ? "user_" : "", | |
658 | type & MEM_PERCPU ? "percpu_" : "", | |
9bb00b28 | 659 | type & MEM_RCU ? "rcu_" : "", |
3f00c523 DV |
660 | type & PTR_UNTRUSTED ? "untrusted_" : "", |
661 | type & PTR_TRUSTED ? "trusted_" : "" | |
ef66c547 | 662 | ); |
20b2aff4 HL |
663 | |
664 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", | |
665 | prefix, str[base_type(type)], postfix); | |
c25b2ae1 HL |
666 | return env->type_str_buf; |
667 | } | |
17a52670 | 668 | |
8efea21d EC |
669 | static char slot_type_char[] = { |
670 | [STACK_INVALID] = '?', | |
671 | [STACK_SPILL] = 'r', | |
672 | [STACK_MISC] = 'm', | |
673 | [STACK_ZERO] = '0', | |
97e03f52 | 674 | [STACK_DYNPTR] = 'd', |
06accc87 | 675 | [STACK_ITER] = 'i', |
8efea21d EC |
676 | }; |
677 | ||
4e92024a AS |
678 | static void print_liveness(struct bpf_verifier_env *env, |
679 | enum bpf_reg_liveness live) | |
680 | { | |
9242b5f5 | 681 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
682 | verbose(env, "_"); |
683 | if (live & REG_LIVE_READ) | |
684 | verbose(env, "r"); | |
685 | if (live & REG_LIVE_WRITTEN) | |
686 | verbose(env, "w"); | |
9242b5f5 AS |
687 | if (live & REG_LIVE_DONE) |
688 | verbose(env, "D"); | |
4e92024a AS |
689 | } |
690 | ||
79168a66 | 691 | static int __get_spi(s32 off) |
97e03f52 JK |
692 | { |
693 | return (-off - 1) / BPF_REG_SIZE; | |
694 | } | |
695 | ||
f5b625e5 KKD |
696 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
697 | const struct bpf_reg_state *reg) | |
698 | { | |
699 | struct bpf_verifier_state *cur = env->cur_state; | |
700 | ||
701 | return cur->frame[reg->frameno]; | |
702 | } | |
703 | ||
97e03f52 JK |
704 | static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots) |
705 | { | |
f5b625e5 | 706 | int allocated_slots = state->allocated_stack / BPF_REG_SIZE; |
97e03f52 | 707 | |
f5b625e5 KKD |
708 | /* We need to check that slots between [spi - nr_slots + 1, spi] are |
709 | * within [0, allocated_stack). | |
710 | * | |
711 | * Please note that the spi grows downwards. For example, a dynptr | |
712 | * takes the size of two stack slots; the first slot will be at | |
713 | * spi and the second slot will be at spi - 1. | |
714 | */ | |
715 | return spi - nr_slots + 1 >= 0 && spi < allocated_slots; | |
97e03f52 JK |
716 | } |
717 | ||
a461f5ad AN |
718 | static int stack_slot_obj_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
719 | const char *obj_kind, int nr_slots) | |
f4d7e40a | 720 | { |
79168a66 | 721 | int off, spi; |
f4d7e40a | 722 | |
79168a66 | 723 | if (!tnum_is_const(reg->var_off)) { |
a461f5ad | 724 | verbose(env, "%s has to be at a constant offset\n", obj_kind); |
79168a66 KKD |
725 | return -EINVAL; |
726 | } | |
727 | ||
728 | off = reg->off + reg->var_off.value; | |
729 | if (off % BPF_REG_SIZE) { | |
a461f5ad | 730 | verbose(env, "cannot pass in %s at an offset=%d\n", obj_kind, off); |
79168a66 KKD |
731 | return -EINVAL; |
732 | } | |
733 | ||
734 | spi = __get_spi(off); | |
a461f5ad AN |
735 | if (spi + 1 < nr_slots) { |
736 | verbose(env, "cannot pass in %s at an offset=%d\n", obj_kind, off); | |
79168a66 KKD |
737 | return -EINVAL; |
738 | } | |
97e03f52 | 739 | |
a461f5ad | 740 | if (!is_spi_bounds_valid(func(env, reg), spi, nr_slots)) |
f5b625e5 KKD |
741 | return -ERANGE; |
742 | return spi; | |
f4d7e40a AS |
743 | } |
744 | ||
a461f5ad AN |
745 | static int dynptr_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
746 | { | |
747 | return stack_slot_obj_get_spi(env, reg, "dynptr", BPF_DYNPTR_NR_SLOTS); | |
748 | } | |
749 | ||
06accc87 AN |
750 | static int iter_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int nr_slots) |
751 | { | |
752 | return stack_slot_obj_get_spi(env, reg, "iter", nr_slots); | |
753 | } | |
754 | ||
b32a5dae | 755 | static const char *btf_type_name(const struct btf *btf, u32 id) |
9e15db66 | 756 | { |
22dc4a0f | 757 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
758 | } |
759 | ||
d54e0f6c AN |
760 | static const char *dynptr_type_str(enum bpf_dynptr_type type) |
761 | { | |
762 | switch (type) { | |
763 | case BPF_DYNPTR_TYPE_LOCAL: | |
764 | return "local"; | |
765 | case BPF_DYNPTR_TYPE_RINGBUF: | |
766 | return "ringbuf"; | |
767 | case BPF_DYNPTR_TYPE_SKB: | |
768 | return "skb"; | |
769 | case BPF_DYNPTR_TYPE_XDP: | |
770 | return "xdp"; | |
771 | case BPF_DYNPTR_TYPE_INVALID: | |
772 | return "<invalid>"; | |
773 | default: | |
774 | WARN_ONCE(1, "unknown dynptr type %d\n", type); | |
775 | return "<unknown>"; | |
776 | } | |
777 | } | |
778 | ||
06accc87 AN |
779 | static const char *iter_type_str(const struct btf *btf, u32 btf_id) |
780 | { | |
781 | if (!btf || btf_id == 0) | |
782 | return "<invalid>"; | |
783 | ||
784 | /* we already validated that type is valid and has conforming name */ | |
b32a5dae | 785 | return btf_type_name(btf, btf_id) + sizeof(ITER_PREFIX) - 1; |
06accc87 AN |
786 | } |
787 | ||
788 | static const char *iter_state_str(enum bpf_iter_state state) | |
789 | { | |
790 | switch (state) { | |
791 | case BPF_ITER_STATE_ACTIVE: | |
792 | return "active"; | |
793 | case BPF_ITER_STATE_DRAINED: | |
794 | return "drained"; | |
795 | case BPF_ITER_STATE_INVALID: | |
796 | return "<invalid>"; | |
797 | default: | |
798 | WARN_ONCE(1, "unknown iter state %d\n", state); | |
799 | return "<unknown>"; | |
800 | } | |
801 | } | |
802 | ||
0f55f9ed CL |
803 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
804 | { | |
805 | env->scratched_regs |= 1U << regno; | |
806 | } | |
807 | ||
808 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
809 | { | |
343e5375 | 810 | env->scratched_stack_slots |= 1ULL << spi; |
0f55f9ed CL |
811 | } |
812 | ||
813 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
814 | { | |
815 | return (env->scratched_regs >> regno) & 1; | |
816 | } | |
817 | ||
818 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
819 | { | |
820 | return (env->scratched_stack_slots >> regno) & 1; | |
821 | } | |
822 | ||
823 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
824 | { | |
825 | return env->scratched_regs || env->scratched_stack_slots; | |
826 | } | |
827 | ||
828 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
829 | { | |
830 | env->scratched_regs = 0U; | |
343e5375 | 831 | env->scratched_stack_slots = 0ULL; |
0f55f9ed CL |
832 | } |
833 | ||
834 | /* Used for printing the entire verifier state. */ | |
835 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
836 | { | |
837 | env->scratched_regs = ~0U; | |
343e5375 | 838 | env->scratched_stack_slots = ~0ULL; |
0f55f9ed CL |
839 | } |
840 | ||
97e03f52 JK |
841 | static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type) |
842 | { | |
843 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
844 | case DYNPTR_TYPE_LOCAL: | |
845 | return BPF_DYNPTR_TYPE_LOCAL; | |
bc34dee6 JK |
846 | case DYNPTR_TYPE_RINGBUF: |
847 | return BPF_DYNPTR_TYPE_RINGBUF; | |
b5964b96 JK |
848 | case DYNPTR_TYPE_SKB: |
849 | return BPF_DYNPTR_TYPE_SKB; | |
05421aec JK |
850 | case DYNPTR_TYPE_XDP: |
851 | return BPF_DYNPTR_TYPE_XDP; | |
97e03f52 JK |
852 | default: |
853 | return BPF_DYNPTR_TYPE_INVALID; | |
854 | } | |
855 | } | |
856 | ||
66e3a13e JK |
857 | static enum bpf_type_flag get_dynptr_type_flag(enum bpf_dynptr_type type) |
858 | { | |
859 | switch (type) { | |
860 | case BPF_DYNPTR_TYPE_LOCAL: | |
861 | return DYNPTR_TYPE_LOCAL; | |
862 | case BPF_DYNPTR_TYPE_RINGBUF: | |
863 | return DYNPTR_TYPE_RINGBUF; | |
864 | case BPF_DYNPTR_TYPE_SKB: | |
865 | return DYNPTR_TYPE_SKB; | |
866 | case BPF_DYNPTR_TYPE_XDP: | |
867 | return DYNPTR_TYPE_XDP; | |
868 | default: | |
869 | return 0; | |
870 | } | |
871 | } | |
872 | ||
bc34dee6 JK |
873 | static bool dynptr_type_refcounted(enum bpf_dynptr_type type) |
874 | { | |
875 | return type == BPF_DYNPTR_TYPE_RINGBUF; | |
876 | } | |
877 | ||
27060531 KKD |
878 | static void __mark_dynptr_reg(struct bpf_reg_state *reg, |
879 | enum bpf_dynptr_type type, | |
f8064ab9 | 880 | bool first_slot, int dynptr_id); |
27060531 KKD |
881 | |
882 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, | |
883 | struct bpf_reg_state *reg); | |
884 | ||
f8064ab9 KKD |
885 | static void mark_dynptr_stack_regs(struct bpf_verifier_env *env, |
886 | struct bpf_reg_state *sreg1, | |
27060531 KKD |
887 | struct bpf_reg_state *sreg2, |
888 | enum bpf_dynptr_type type) | |
889 | { | |
f8064ab9 KKD |
890 | int id = ++env->id_gen; |
891 | ||
892 | __mark_dynptr_reg(sreg1, type, true, id); | |
893 | __mark_dynptr_reg(sreg2, type, false, id); | |
27060531 KKD |
894 | } |
895 | ||
f8064ab9 KKD |
896 | static void mark_dynptr_cb_reg(struct bpf_verifier_env *env, |
897 | struct bpf_reg_state *reg, | |
27060531 KKD |
898 | enum bpf_dynptr_type type) |
899 | { | |
f8064ab9 | 900 | __mark_dynptr_reg(reg, type, true, ++env->id_gen); |
27060531 KKD |
901 | } |
902 | ||
ef8fc7a0 KKD |
903 | static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env, |
904 | struct bpf_func_state *state, int spi); | |
27060531 | 905 | |
97e03f52 JK |
906 | static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
907 | enum bpf_arg_type arg_type, int insn_idx) | |
908 | { | |
909 | struct bpf_func_state *state = func(env, reg); | |
910 | enum bpf_dynptr_type type; | |
379d4ba8 | 911 | int spi, i, id, err; |
97e03f52 | 912 | |
79168a66 KKD |
913 | spi = dynptr_get_spi(env, reg); |
914 | if (spi < 0) | |
915 | return spi; | |
97e03f52 | 916 | |
379d4ba8 KKD |
917 | /* We cannot assume both spi and spi - 1 belong to the same dynptr, |
918 | * hence we need to call destroy_if_dynptr_stack_slot twice for both, | |
919 | * to ensure that for the following example: | |
920 | * [d1][d1][d2][d2] | |
921 | * spi 3 2 1 0 | |
922 | * So marking spi = 2 should lead to destruction of both d1 and d2. In | |
923 | * case they do belong to same dynptr, second call won't see slot_type | |
924 | * as STACK_DYNPTR and will simply skip destruction. | |
925 | */ | |
926 | err = destroy_if_dynptr_stack_slot(env, state, spi); | |
927 | if (err) | |
928 | return err; | |
929 | err = destroy_if_dynptr_stack_slot(env, state, spi - 1); | |
930 | if (err) | |
931 | return err; | |
97e03f52 JK |
932 | |
933 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
934 | state->stack[spi].slot_type[i] = STACK_DYNPTR; | |
935 | state->stack[spi - 1].slot_type[i] = STACK_DYNPTR; | |
936 | } | |
937 | ||
938 | type = arg_to_dynptr_type(arg_type); | |
939 | if (type == BPF_DYNPTR_TYPE_INVALID) | |
940 | return -EINVAL; | |
941 | ||
f8064ab9 | 942 | mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr, |
27060531 | 943 | &state->stack[spi - 1].spilled_ptr, type); |
97e03f52 | 944 | |
bc34dee6 JK |
945 | if (dynptr_type_refcounted(type)) { |
946 | /* The id is used to track proper releasing */ | |
947 | id = acquire_reference_state(env, insn_idx); | |
948 | if (id < 0) | |
949 | return id; | |
950 | ||
27060531 KKD |
951 | state->stack[spi].spilled_ptr.ref_obj_id = id; |
952 | state->stack[spi - 1].spilled_ptr.ref_obj_id = id; | |
bc34dee6 JK |
953 | } |
954 | ||
d6fefa11 KKD |
955 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
956 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
957 | ||
97e03f52 JK |
958 | return 0; |
959 | } | |
960 | ||
961 | static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
962 | { | |
963 | struct bpf_func_state *state = func(env, reg); | |
964 | int spi, i; | |
965 | ||
79168a66 KKD |
966 | spi = dynptr_get_spi(env, reg); |
967 | if (spi < 0) | |
968 | return spi; | |
97e03f52 JK |
969 | |
970 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
971 | state->stack[spi].slot_type[i] = STACK_INVALID; | |
972 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
973 | } | |
974 | ||
bc34dee6 | 975 | /* Invalidate any slices associated with this dynptr */ |
27060531 KKD |
976 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) |
977 | WARN_ON_ONCE(release_reference(env, state->stack[spi].spilled_ptr.ref_obj_id)); | |
97e03f52 | 978 | |
27060531 KKD |
979 | __mark_reg_not_init(env, &state->stack[spi].spilled_ptr); |
980 | __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr); | |
d6fefa11 KKD |
981 | |
982 | /* Why do we need to set REG_LIVE_WRITTEN for STACK_INVALID slot? | |
983 | * | |
984 | * While we don't allow reading STACK_INVALID, it is still possible to | |
985 | * do <8 byte writes marking some but not all slots as STACK_MISC. Then, | |
986 | * helpers or insns can do partial read of that part without failing, | |
987 | * but check_stack_range_initialized, check_stack_read_var_off, and | |
988 | * check_stack_read_fixed_off will do mark_reg_read for all 8-bytes of | |
989 | * the slot conservatively. Hence we need to prevent those liveness | |
990 | * marking walks. | |
991 | * | |
992 | * This was not a problem before because STACK_INVALID is only set by | |
993 | * default (where the default reg state has its reg->parent as NULL), or | |
994 | * in clean_live_states after REG_LIVE_DONE (at which point | |
995 | * mark_reg_read won't walk reg->parent chain), but not randomly during | |
996 | * verifier state exploration (like we did above). Hence, for our case | |
997 | * parentage chain will still be live (i.e. reg->parent may be | |
998 | * non-NULL), while earlier reg->parent was NULL, so we need | |
999 | * REG_LIVE_WRITTEN to screen off read marker propagation when it is | |
1000 | * done later on reads or by mark_dynptr_read as well to unnecessary | |
1001 | * mark registers in verifier state. | |
1002 | */ | |
1003 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1004 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1005 | ||
97e03f52 JK |
1006 | return 0; |
1007 | } | |
1008 | ||
ef8fc7a0 KKD |
1009 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1010 | struct bpf_reg_state *reg); | |
1011 | ||
dbd8d228 KKD |
1012 | static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
1013 | { | |
1014 | if (!env->allow_ptr_leaks) | |
1015 | __mark_reg_not_init(env, reg); | |
1016 | else | |
1017 | __mark_reg_unknown(env, reg); | |
1018 | } | |
1019 | ||
ef8fc7a0 KKD |
1020 | static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env, |
1021 | struct bpf_func_state *state, int spi) | |
97e03f52 | 1022 | { |
f8064ab9 KKD |
1023 | struct bpf_func_state *fstate; |
1024 | struct bpf_reg_state *dreg; | |
1025 | int i, dynptr_id; | |
27060531 | 1026 | |
ef8fc7a0 KKD |
1027 | /* We always ensure that STACK_DYNPTR is never set partially, |
1028 | * hence just checking for slot_type[0] is enough. This is | |
1029 | * different for STACK_SPILL, where it may be only set for | |
1030 | * 1 byte, so code has to use is_spilled_reg. | |
1031 | */ | |
1032 | if (state->stack[spi].slot_type[0] != STACK_DYNPTR) | |
1033 | return 0; | |
97e03f52 | 1034 | |
ef8fc7a0 KKD |
1035 | /* Reposition spi to first slot */ |
1036 | if (!state->stack[spi].spilled_ptr.dynptr.first_slot) | |
1037 | spi = spi + 1; | |
1038 | ||
1039 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) { | |
1040 | verbose(env, "cannot overwrite referenced dynptr\n"); | |
1041 | return -EINVAL; | |
1042 | } | |
1043 | ||
1044 | mark_stack_slot_scratched(env, spi); | |
1045 | mark_stack_slot_scratched(env, spi - 1); | |
97e03f52 | 1046 | |
ef8fc7a0 | 1047 | /* Writing partially to one dynptr stack slot destroys both. */ |
97e03f52 | 1048 | for (i = 0; i < BPF_REG_SIZE; i++) { |
ef8fc7a0 KKD |
1049 | state->stack[spi].slot_type[i] = STACK_INVALID; |
1050 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
97e03f52 JK |
1051 | } |
1052 | ||
f8064ab9 KKD |
1053 | dynptr_id = state->stack[spi].spilled_ptr.id; |
1054 | /* Invalidate any slices associated with this dynptr */ | |
1055 | bpf_for_each_reg_in_vstate(env->cur_state, fstate, dreg, ({ | |
1056 | /* Dynptr slices are only PTR_TO_MEM_OR_NULL and PTR_TO_MEM */ | |
1057 | if (dreg->type != (PTR_TO_MEM | PTR_MAYBE_NULL) && dreg->type != PTR_TO_MEM) | |
1058 | continue; | |
dbd8d228 KKD |
1059 | if (dreg->dynptr_id == dynptr_id) |
1060 | mark_reg_invalid(env, dreg); | |
f8064ab9 | 1061 | })); |
ef8fc7a0 KKD |
1062 | |
1063 | /* Do not release reference state, we are destroying dynptr on stack, | |
1064 | * not using some helper to release it. Just reset register. | |
1065 | */ | |
1066 | __mark_reg_not_init(env, &state->stack[spi].spilled_ptr); | |
1067 | __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr); | |
1068 | ||
1069 | /* Same reason as unmark_stack_slots_dynptr above */ | |
1070 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1071 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1072 | ||
1073 | return 0; | |
1074 | } | |
1075 | ||
7e0dac28 | 1076 | static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
97e03f52 | 1077 | { |
7e0dac28 JK |
1078 | int spi; |
1079 | ||
27060531 KKD |
1080 | if (reg->type == CONST_PTR_TO_DYNPTR) |
1081 | return false; | |
97e03f52 | 1082 | |
7e0dac28 JK |
1083 | spi = dynptr_get_spi(env, reg); |
1084 | ||
1085 | /* -ERANGE (i.e. spi not falling into allocated stack slots) isn't an | |
1086 | * error because this just means the stack state hasn't been updated yet. | |
1087 | * We will do check_mem_access to check and update stack bounds later. | |
f5b625e5 | 1088 | */ |
7e0dac28 JK |
1089 | if (spi < 0 && spi != -ERANGE) |
1090 | return false; | |
1091 | ||
1092 | /* We don't need to check if the stack slots are marked by previous | |
1093 | * dynptr initializations because we allow overwriting existing unreferenced | |
1094 | * STACK_DYNPTR slots, see mark_stack_slots_dynptr which calls | |
1095 | * destroy_if_dynptr_stack_slot to ensure dynptr objects at the slots we are | |
1096 | * touching are completely destructed before we reinitialize them for a new | |
1097 | * one. For referenced ones, destroy_if_dynptr_stack_slot returns an error early | |
1098 | * instead of delaying it until the end where the user will get "Unreleased | |
379d4ba8 KKD |
1099 | * reference" error. |
1100 | */ | |
97e03f52 JK |
1101 | return true; |
1102 | } | |
1103 | ||
7e0dac28 | 1104 | static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
97e03f52 JK |
1105 | { |
1106 | struct bpf_func_state *state = func(env, reg); | |
7e0dac28 | 1107 | int i, spi; |
97e03f52 | 1108 | |
7e0dac28 JK |
1109 | /* This already represents first slot of initialized bpf_dynptr. |
1110 | * | |
1111 | * CONST_PTR_TO_DYNPTR already has fixed and var_off as 0 due to | |
1112 | * check_func_arg_reg_off's logic, so we don't need to check its | |
1113 | * offset and alignment. | |
1114 | */ | |
27060531 KKD |
1115 | if (reg->type == CONST_PTR_TO_DYNPTR) |
1116 | return true; | |
1117 | ||
7e0dac28 | 1118 | spi = dynptr_get_spi(env, reg); |
79168a66 KKD |
1119 | if (spi < 0) |
1120 | return false; | |
f5b625e5 | 1121 | if (!state->stack[spi].spilled_ptr.dynptr.first_slot) |
97e03f52 JK |
1122 | return false; |
1123 | ||
1124 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
1125 | if (state->stack[spi].slot_type[i] != STACK_DYNPTR || | |
1126 | state->stack[spi - 1].slot_type[i] != STACK_DYNPTR) | |
1127 | return false; | |
1128 | } | |
1129 | ||
e9e315b4 RS |
1130 | return true; |
1131 | } | |
1132 | ||
6b75bd3d KKD |
1133 | static bool is_dynptr_type_expected(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
1134 | enum bpf_arg_type arg_type) | |
e9e315b4 RS |
1135 | { |
1136 | struct bpf_func_state *state = func(env, reg); | |
1137 | enum bpf_dynptr_type dynptr_type; | |
27060531 | 1138 | int spi; |
e9e315b4 | 1139 | |
97e03f52 JK |
1140 | /* ARG_PTR_TO_DYNPTR takes any type of dynptr */ |
1141 | if (arg_type == ARG_PTR_TO_DYNPTR) | |
1142 | return true; | |
1143 | ||
e9e315b4 | 1144 | dynptr_type = arg_to_dynptr_type(arg_type); |
27060531 KKD |
1145 | if (reg->type == CONST_PTR_TO_DYNPTR) { |
1146 | return reg->dynptr.type == dynptr_type; | |
1147 | } else { | |
79168a66 KKD |
1148 | spi = dynptr_get_spi(env, reg); |
1149 | if (spi < 0) | |
1150 | return false; | |
27060531 KKD |
1151 | return state->stack[spi].spilled_ptr.dynptr.type == dynptr_type; |
1152 | } | |
97e03f52 JK |
1153 | } |
1154 | ||
06accc87 AN |
1155 | static void __mark_reg_known_zero(struct bpf_reg_state *reg); |
1156 | ||
1157 | static int mark_stack_slots_iter(struct bpf_verifier_env *env, | |
1158 | struct bpf_reg_state *reg, int insn_idx, | |
1159 | struct btf *btf, u32 btf_id, int nr_slots) | |
1160 | { | |
1161 | struct bpf_func_state *state = func(env, reg); | |
1162 | int spi, i, j, id; | |
1163 | ||
1164 | spi = iter_get_spi(env, reg, nr_slots); | |
1165 | if (spi < 0) | |
1166 | return spi; | |
1167 | ||
1168 | id = acquire_reference_state(env, insn_idx); | |
1169 | if (id < 0) | |
1170 | return id; | |
1171 | ||
1172 | for (i = 0; i < nr_slots; i++) { | |
1173 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1174 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1175 | ||
1176 | __mark_reg_known_zero(st); | |
1177 | st->type = PTR_TO_STACK; /* we don't have dedicated reg type */ | |
1178 | st->live |= REG_LIVE_WRITTEN; | |
1179 | st->ref_obj_id = i == 0 ? id : 0; | |
1180 | st->iter.btf = btf; | |
1181 | st->iter.btf_id = btf_id; | |
1182 | st->iter.state = BPF_ITER_STATE_ACTIVE; | |
1183 | st->iter.depth = 0; | |
1184 | ||
1185 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1186 | slot->slot_type[j] = STACK_ITER; | |
1187 | ||
1188 | mark_stack_slot_scratched(env, spi - i); | |
1189 | } | |
1190 | ||
1191 | return 0; | |
1192 | } | |
1193 | ||
1194 | static int unmark_stack_slots_iter(struct bpf_verifier_env *env, | |
1195 | struct bpf_reg_state *reg, int nr_slots) | |
1196 | { | |
1197 | struct bpf_func_state *state = func(env, reg); | |
1198 | int spi, i, j; | |
1199 | ||
1200 | spi = iter_get_spi(env, reg, nr_slots); | |
1201 | if (spi < 0) | |
1202 | return spi; | |
1203 | ||
1204 | for (i = 0; i < nr_slots; i++) { | |
1205 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1206 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1207 | ||
1208 | if (i == 0) | |
1209 | WARN_ON_ONCE(release_reference(env, st->ref_obj_id)); | |
1210 | ||
1211 | __mark_reg_not_init(env, st); | |
1212 | ||
1213 | /* see unmark_stack_slots_dynptr() for why we need to set REG_LIVE_WRITTEN */ | |
1214 | st->live |= REG_LIVE_WRITTEN; | |
1215 | ||
1216 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1217 | slot->slot_type[j] = STACK_INVALID; | |
1218 | ||
1219 | mark_stack_slot_scratched(env, spi - i); | |
1220 | } | |
1221 | ||
1222 | return 0; | |
1223 | } | |
1224 | ||
1225 | static bool is_iter_reg_valid_uninit(struct bpf_verifier_env *env, | |
1226 | struct bpf_reg_state *reg, int nr_slots) | |
1227 | { | |
1228 | struct bpf_func_state *state = func(env, reg); | |
1229 | int spi, i, j; | |
1230 | ||
1231 | /* For -ERANGE (i.e. spi not falling into allocated stack slots), we | |
1232 | * will do check_mem_access to check and update stack bounds later, so | |
1233 | * return true for that case. | |
1234 | */ | |
1235 | spi = iter_get_spi(env, reg, nr_slots); | |
1236 | if (spi == -ERANGE) | |
1237 | return true; | |
1238 | if (spi < 0) | |
1239 | return false; | |
1240 | ||
1241 | for (i = 0; i < nr_slots; i++) { | |
1242 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1243 | ||
1244 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1245 | if (slot->slot_type[j] == STACK_ITER) | |
1246 | return false; | |
1247 | } | |
1248 | ||
1249 | return true; | |
1250 | } | |
1251 | ||
1252 | static bool is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg, | |
1253 | struct btf *btf, u32 btf_id, int nr_slots) | |
1254 | { | |
1255 | struct bpf_func_state *state = func(env, reg); | |
1256 | int spi, i, j; | |
1257 | ||
1258 | spi = iter_get_spi(env, reg, nr_slots); | |
1259 | if (spi < 0) | |
1260 | return false; | |
1261 | ||
1262 | for (i = 0; i < nr_slots; i++) { | |
1263 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1264 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1265 | ||
1266 | /* only main (first) slot has ref_obj_id set */ | |
1267 | if (i == 0 && !st->ref_obj_id) | |
1268 | return false; | |
1269 | if (i != 0 && st->ref_obj_id) | |
1270 | return false; | |
1271 | if (st->iter.btf != btf || st->iter.btf_id != btf_id) | |
1272 | return false; | |
1273 | ||
1274 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1275 | if (slot->slot_type[j] != STACK_ITER) | |
1276 | return false; | |
1277 | } | |
1278 | ||
1279 | return true; | |
1280 | } | |
1281 | ||
1282 | /* Check if given stack slot is "special": | |
1283 | * - spilled register state (STACK_SPILL); | |
1284 | * - dynptr state (STACK_DYNPTR); | |
1285 | * - iter state (STACK_ITER). | |
1286 | */ | |
1287 | static bool is_stack_slot_special(const struct bpf_stack_state *stack) | |
1288 | { | |
1289 | enum bpf_stack_slot_type type = stack->slot_type[BPF_REG_SIZE - 1]; | |
1290 | ||
1291 | switch (type) { | |
1292 | case STACK_SPILL: | |
1293 | case STACK_DYNPTR: | |
1294 | case STACK_ITER: | |
1295 | return true; | |
1296 | case STACK_INVALID: | |
1297 | case STACK_MISC: | |
1298 | case STACK_ZERO: | |
1299 | return false; | |
1300 | default: | |
1301 | WARN_ONCE(1, "unknown stack slot type %d\n", type); | |
1302 | return true; | |
1303 | } | |
1304 | } | |
1305 | ||
27113c59 MKL |
1306 | /* The reg state of a pointer or a bounded scalar was saved when |
1307 | * it was spilled to the stack. | |
1308 | */ | |
1309 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
1310 | { | |
1311 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
1312 | } | |
1313 | ||
354e8f19 MKL |
1314 | static void scrub_spilled_slot(u8 *stype) |
1315 | { | |
1316 | if (*stype != STACK_INVALID) | |
1317 | *stype = STACK_MISC; | |
1318 | } | |
1319 | ||
61bd5218 | 1320 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
1321 | const struct bpf_func_state *state, |
1322 | bool print_all) | |
17a52670 | 1323 | { |
f4d7e40a | 1324 | const struct bpf_reg_state *reg; |
17a52670 AS |
1325 | enum bpf_reg_type t; |
1326 | int i; | |
1327 | ||
f4d7e40a AS |
1328 | if (state->frameno) |
1329 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 1330 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
1331 | reg = &state->regs[i]; |
1332 | t = reg->type; | |
17a52670 AS |
1333 | if (t == NOT_INIT) |
1334 | continue; | |
0f55f9ed CL |
1335 | if (!print_all && !reg_scratched(env, i)) |
1336 | continue; | |
4e92024a AS |
1337 | verbose(env, " R%d", i); |
1338 | print_liveness(env, reg->live); | |
7df5072c | 1339 | verbose(env, "="); |
b5dc0163 AS |
1340 | if (t == SCALAR_VALUE && reg->precise) |
1341 | verbose(env, "P"); | |
f1174f77 EC |
1342 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
1343 | tnum_is_const(reg->var_off)) { | |
1344 | /* reg->off should be 0 for SCALAR_VALUE */ | |
7df5072c | 1345 | verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
61bd5218 | 1346 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 1347 | } else { |
7df5072c ML |
1348 | const char *sep = ""; |
1349 | ||
1350 | verbose(env, "%s", reg_type_str(env, t)); | |
5844101a | 1351 | if (base_type(t) == PTR_TO_BTF_ID) |
b32a5dae | 1352 | verbose(env, "%s", btf_type_name(reg->btf, reg->btf_id)); |
7df5072c ML |
1353 | verbose(env, "("); |
1354 | /* | |
1355 | * _a stands for append, was shortened to avoid multiline statements below. | |
1356 | * This macro is used to output a comma separated list of attributes. | |
1357 | */ | |
1358 | #define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) | |
1359 | ||
1360 | if (reg->id) | |
1361 | verbose_a("id=%d", reg->id); | |
a28ace78 | 1362 | if (reg->ref_obj_id) |
7df5072c | 1363 | verbose_a("ref_obj_id=%d", reg->ref_obj_id); |
6a3cd331 DM |
1364 | if (type_is_non_owning_ref(reg->type)) |
1365 | verbose_a("%s", "non_own_ref"); | |
f1174f77 | 1366 | if (t != SCALAR_VALUE) |
7df5072c | 1367 | verbose_a("off=%d", reg->off); |
de8f3a83 | 1368 | if (type_is_pkt_pointer(t)) |
7df5072c | 1369 | verbose_a("r=%d", reg->range); |
c25b2ae1 HL |
1370 | else if (base_type(t) == CONST_PTR_TO_MAP || |
1371 | base_type(t) == PTR_TO_MAP_KEY || | |
1372 | base_type(t) == PTR_TO_MAP_VALUE) | |
7df5072c ML |
1373 | verbose_a("ks=%d,vs=%d", |
1374 | reg->map_ptr->key_size, | |
1375 | reg->map_ptr->value_size); | |
7d1238f2 EC |
1376 | if (tnum_is_const(reg->var_off)) { |
1377 | /* Typically an immediate SCALAR_VALUE, but | |
1378 | * could be a pointer whose offset is too big | |
1379 | * for reg->off | |
1380 | */ | |
7df5072c | 1381 | verbose_a("imm=%llx", reg->var_off.value); |
7d1238f2 EC |
1382 | } else { |
1383 | if (reg->smin_value != reg->umin_value && | |
1384 | reg->smin_value != S64_MIN) | |
7df5072c | 1385 | verbose_a("smin=%lld", (long long)reg->smin_value); |
7d1238f2 EC |
1386 | if (reg->smax_value != reg->umax_value && |
1387 | reg->smax_value != S64_MAX) | |
7df5072c | 1388 | verbose_a("smax=%lld", (long long)reg->smax_value); |
7d1238f2 | 1389 | if (reg->umin_value != 0) |
7df5072c | 1390 | verbose_a("umin=%llu", (unsigned long long)reg->umin_value); |
7d1238f2 | 1391 | if (reg->umax_value != U64_MAX) |
7df5072c | 1392 | verbose_a("umax=%llu", (unsigned long long)reg->umax_value); |
7d1238f2 EC |
1393 | if (!tnum_is_unknown(reg->var_off)) { |
1394 | char tn_buf[48]; | |
f1174f77 | 1395 | |
7d1238f2 | 1396 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
7df5072c | 1397 | verbose_a("var_off=%s", tn_buf); |
7d1238f2 | 1398 | } |
3f50f132 JF |
1399 | if (reg->s32_min_value != reg->smin_value && |
1400 | reg->s32_min_value != S32_MIN) | |
7df5072c | 1401 | verbose_a("s32_min=%d", (int)(reg->s32_min_value)); |
3f50f132 JF |
1402 | if (reg->s32_max_value != reg->smax_value && |
1403 | reg->s32_max_value != S32_MAX) | |
7df5072c | 1404 | verbose_a("s32_max=%d", (int)(reg->s32_max_value)); |
3f50f132 JF |
1405 | if (reg->u32_min_value != reg->umin_value && |
1406 | reg->u32_min_value != U32_MIN) | |
7df5072c | 1407 | verbose_a("u32_min=%d", (int)(reg->u32_min_value)); |
3f50f132 JF |
1408 | if (reg->u32_max_value != reg->umax_value && |
1409 | reg->u32_max_value != U32_MAX) | |
7df5072c | 1410 | verbose_a("u32_max=%d", (int)(reg->u32_max_value)); |
f1174f77 | 1411 | } |
7df5072c ML |
1412 | #undef verbose_a |
1413 | ||
61bd5218 | 1414 | verbose(env, ")"); |
f1174f77 | 1415 | } |
17a52670 | 1416 | } |
638f5b90 | 1417 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
1418 | char types_buf[BPF_REG_SIZE + 1]; |
1419 | bool valid = false; | |
1420 | int j; | |
1421 | ||
1422 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
1423 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
1424 | valid = true; | |
d54e0f6c | 1425 | types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; |
8efea21d EC |
1426 | } |
1427 | types_buf[BPF_REG_SIZE] = 0; | |
1428 | if (!valid) | |
1429 | continue; | |
0f55f9ed CL |
1430 | if (!print_all && !stack_slot_scratched(env, i)) |
1431 | continue; | |
d54e0f6c AN |
1432 | switch (state->stack[i].slot_type[BPF_REG_SIZE - 1]) { |
1433 | case STACK_SPILL: | |
b5dc0163 AS |
1434 | reg = &state->stack[i].spilled_ptr; |
1435 | t = reg->type; | |
d54e0f6c AN |
1436 | |
1437 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1438 | print_liveness(env, reg->live); | |
7df5072c | 1439 | verbose(env, "=%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
b5dc0163 AS |
1440 | if (t == SCALAR_VALUE && reg->precise) |
1441 | verbose(env, "P"); | |
1442 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
1443 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
d54e0f6c AN |
1444 | break; |
1445 | case STACK_DYNPTR: | |
1446 | i += BPF_DYNPTR_NR_SLOTS - 1; | |
1447 | reg = &state->stack[i].spilled_ptr; | |
1448 | ||
1449 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1450 | print_liveness(env, reg->live); | |
1451 | verbose(env, "=dynptr_%s", dynptr_type_str(reg->dynptr.type)); | |
1452 | if (reg->ref_obj_id) | |
1453 | verbose(env, "(ref_id=%d)", reg->ref_obj_id); | |
1454 | break; | |
06accc87 AN |
1455 | case STACK_ITER: |
1456 | /* only main slot has ref_obj_id set; skip others */ | |
1457 | reg = &state->stack[i].spilled_ptr; | |
1458 | if (!reg->ref_obj_id) | |
1459 | continue; | |
1460 | ||
1461 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1462 | print_liveness(env, reg->live); | |
1463 | verbose(env, "=iter_%s(ref_id=%d,state=%s,depth=%u)", | |
1464 | iter_type_str(reg->iter.btf, reg->iter.btf_id), | |
1465 | reg->ref_obj_id, iter_state_str(reg->iter.state), | |
1466 | reg->iter.depth); | |
1467 | break; | |
d54e0f6c AN |
1468 | case STACK_MISC: |
1469 | case STACK_ZERO: | |
1470 | default: | |
1471 | reg = &state->stack[i].spilled_ptr; | |
1472 | ||
1473 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1474 | types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; | |
1475 | types_buf[BPF_REG_SIZE] = 0; | |
1476 | ||
1477 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1478 | print_liveness(env, reg->live); | |
8efea21d | 1479 | verbose(env, "=%s", types_buf); |
d54e0f6c | 1480 | break; |
b5dc0163 | 1481 | } |
17a52670 | 1482 | } |
fd978bf7 JS |
1483 | if (state->acquired_refs && state->refs[0].id) { |
1484 | verbose(env, " refs=%d", state->refs[0].id); | |
1485 | for (i = 1; i < state->acquired_refs; i++) | |
1486 | if (state->refs[i].id) | |
1487 | verbose(env, ",%d", state->refs[i].id); | |
1488 | } | |
bfc6bb74 AS |
1489 | if (state->in_callback_fn) |
1490 | verbose(env, " cb"); | |
1491 | if (state->in_async_callback_fn) | |
1492 | verbose(env, " async_cb"); | |
61bd5218 | 1493 | verbose(env, "\n"); |
0f55f9ed | 1494 | mark_verifier_state_clean(env); |
17a52670 AS |
1495 | } |
1496 | ||
2e576648 CL |
1497 | static inline u32 vlog_alignment(u32 pos) |
1498 | { | |
1499 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
1500 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
1501 | } | |
1502 | ||
1503 | static void print_insn_state(struct bpf_verifier_env *env, | |
1504 | const struct bpf_func_state *state) | |
1505 | { | |
1506 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { | |
1507 | /* remove new line character */ | |
1508 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); | |
1509 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); | |
1510 | } else { | |
1511 | verbose(env, "%d:", env->insn_idx); | |
1512 | } | |
1513 | print_verifier_state(env, state, false); | |
17a52670 AS |
1514 | } |
1515 | ||
c69431aa LB |
1516 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
1517 | * small to hold src. This is different from krealloc since we don't want to preserve | |
1518 | * the contents of dst. | |
1519 | * | |
1520 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
1521 | * not be allocated. | |
638f5b90 | 1522 | */ |
c69431aa | 1523 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 1524 | { |
45435d8d KC |
1525 | size_t alloc_bytes; |
1526 | void *orig = dst; | |
c69431aa LB |
1527 | size_t bytes; |
1528 | ||
1529 | if (ZERO_OR_NULL_PTR(src)) | |
1530 | goto out; | |
1531 | ||
1532 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
1533 | return NULL; | |
1534 | ||
45435d8d KC |
1535 | alloc_bytes = max(ksize(orig), kmalloc_size_roundup(bytes)); |
1536 | dst = krealloc(orig, alloc_bytes, flags); | |
1537 | if (!dst) { | |
1538 | kfree(orig); | |
1539 | return NULL; | |
c69431aa LB |
1540 | } |
1541 | ||
1542 | memcpy(dst, src, bytes); | |
1543 | out: | |
1544 | return dst ? dst : ZERO_SIZE_PTR; | |
1545 | } | |
1546 | ||
1547 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
1548 | * small to hold new_n items. new items are zeroed out if the array grows. | |
1549 | * | |
1550 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
1551 | */ | |
1552 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
1553 | { | |
ceb35b66 | 1554 | size_t alloc_size; |
42378a9c KC |
1555 | void *new_arr; |
1556 | ||
c69431aa LB |
1557 | if (!new_n || old_n == new_n) |
1558 | goto out; | |
1559 | ||
ceb35b66 KC |
1560 | alloc_size = kmalloc_size_roundup(size_mul(new_n, size)); |
1561 | new_arr = krealloc(arr, alloc_size, GFP_KERNEL); | |
42378a9c KC |
1562 | if (!new_arr) { |
1563 | kfree(arr); | |
c69431aa | 1564 | return NULL; |
42378a9c KC |
1565 | } |
1566 | arr = new_arr; | |
c69431aa LB |
1567 | |
1568 | if (new_n > old_n) | |
1569 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
1570 | ||
1571 | out: | |
1572 | return arr ? arr : ZERO_SIZE_PTR; | |
1573 | } | |
1574 | ||
1575 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1576 | { | |
1577 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
1578 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
1579 | if (!dst->refs) | |
1580 | return -ENOMEM; | |
1581 | ||
1582 | dst->acquired_refs = src->acquired_refs; | |
1583 | return 0; | |
1584 | } | |
1585 | ||
1586 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1587 | { | |
1588 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
1589 | ||
1590 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
1591 | GFP_KERNEL); | |
1592 | if (!dst->stack) | |
1593 | return -ENOMEM; | |
1594 | ||
1595 | dst->allocated_stack = src->allocated_stack; | |
1596 | return 0; | |
1597 | } | |
1598 | ||
1599 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
1600 | { | |
1601 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
1602 | sizeof(struct bpf_reference_state)); | |
1603 | if (!state->refs) | |
1604 | return -ENOMEM; | |
1605 | ||
1606 | state->acquired_refs = n; | |
1607 | return 0; | |
1608 | } | |
1609 | ||
1610 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
1611 | { | |
1612 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
1613 | ||
1614 | if (old_n >= n) | |
1615 | return 0; | |
1616 | ||
1617 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
1618 | if (!state->stack) | |
1619 | return -ENOMEM; | |
1620 | ||
1621 | state->allocated_stack = size; | |
1622 | return 0; | |
fd978bf7 JS |
1623 | } |
1624 | ||
1625 | /* Acquire a pointer id from the env and update the state->refs to include | |
1626 | * this new pointer reference. | |
1627 | * On success, returns a valid pointer id to associate with the register | |
1628 | * On failure, returns a negative errno. | |
638f5b90 | 1629 | */ |
fd978bf7 | 1630 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 1631 | { |
fd978bf7 JS |
1632 | struct bpf_func_state *state = cur_func(env); |
1633 | int new_ofs = state->acquired_refs; | |
1634 | int id, err; | |
1635 | ||
c69431aa | 1636 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
1637 | if (err) |
1638 | return err; | |
1639 | id = ++env->id_gen; | |
1640 | state->refs[new_ofs].id = id; | |
1641 | state->refs[new_ofs].insn_idx = insn_idx; | |
9d9d00ac | 1642 | state->refs[new_ofs].callback_ref = state->in_callback_fn ? state->frameno : 0; |
638f5b90 | 1643 | |
fd978bf7 JS |
1644 | return id; |
1645 | } | |
1646 | ||
1647 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 1648 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
1649 | { |
1650 | int i, last_idx; | |
1651 | ||
fd978bf7 JS |
1652 | last_idx = state->acquired_refs - 1; |
1653 | for (i = 0; i < state->acquired_refs; i++) { | |
1654 | if (state->refs[i].id == ptr_id) { | |
9d9d00ac KKD |
1655 | /* Cannot release caller references in callbacks */ |
1656 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) | |
1657 | return -EINVAL; | |
fd978bf7 JS |
1658 | if (last_idx && i != last_idx) |
1659 | memcpy(&state->refs[i], &state->refs[last_idx], | |
1660 | sizeof(*state->refs)); | |
1661 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
1662 | state->acquired_refs--; | |
638f5b90 | 1663 | return 0; |
638f5b90 | 1664 | } |
638f5b90 | 1665 | } |
46f8bc92 | 1666 | return -EINVAL; |
fd978bf7 JS |
1667 | } |
1668 | ||
f4d7e40a AS |
1669 | static void free_func_state(struct bpf_func_state *state) |
1670 | { | |
5896351e AS |
1671 | if (!state) |
1672 | return; | |
fd978bf7 | 1673 | kfree(state->refs); |
f4d7e40a AS |
1674 | kfree(state->stack); |
1675 | kfree(state); | |
1676 | } | |
1677 | ||
b5dc0163 AS |
1678 | static void clear_jmp_history(struct bpf_verifier_state *state) |
1679 | { | |
1680 | kfree(state->jmp_history); | |
1681 | state->jmp_history = NULL; | |
1682 | state->jmp_history_cnt = 0; | |
1683 | } | |
1684 | ||
1969db47 AS |
1685 | static void free_verifier_state(struct bpf_verifier_state *state, |
1686 | bool free_self) | |
638f5b90 | 1687 | { |
f4d7e40a AS |
1688 | int i; |
1689 | ||
1690 | for (i = 0; i <= state->curframe; i++) { | |
1691 | free_func_state(state->frame[i]); | |
1692 | state->frame[i] = NULL; | |
1693 | } | |
b5dc0163 | 1694 | clear_jmp_history(state); |
1969db47 AS |
1695 | if (free_self) |
1696 | kfree(state); | |
638f5b90 AS |
1697 | } |
1698 | ||
1699 | /* copy verifier state from src to dst growing dst stack space | |
1700 | * when necessary to accommodate larger src stack | |
1701 | */ | |
f4d7e40a AS |
1702 | static int copy_func_state(struct bpf_func_state *dst, |
1703 | const struct bpf_func_state *src) | |
638f5b90 AS |
1704 | { |
1705 | int err; | |
1706 | ||
fd978bf7 JS |
1707 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
1708 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
1709 | if (err) |
1710 | return err; | |
638f5b90 AS |
1711 | return copy_stack_state(dst, src); |
1712 | } | |
1713 | ||
f4d7e40a AS |
1714 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1715 | const struct bpf_verifier_state *src) | |
1716 | { | |
1717 | struct bpf_func_state *dst; | |
1718 | int i, err; | |
1719 | ||
06ab6a50 LB |
1720 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1721 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1722 | GFP_USER); | |
1723 | if (!dst_state->jmp_history) | |
1724 | return -ENOMEM; | |
b5dc0163 AS |
1725 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1726 | ||
f4d7e40a AS |
1727 | /* if dst has more stack frames then src frame, free them */ |
1728 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1729 | free_func_state(dst_state->frame[i]); | |
1730 | dst_state->frame[i] = NULL; | |
1731 | } | |
979d63d5 | 1732 | dst_state->speculative = src->speculative; |
9bb00b28 | 1733 | dst_state->active_rcu_lock = src->active_rcu_lock; |
f4d7e40a | 1734 | dst_state->curframe = src->curframe; |
d0d78c1d KKD |
1735 | dst_state->active_lock.ptr = src->active_lock.ptr; |
1736 | dst_state->active_lock.id = src->active_lock.id; | |
2589726d AS |
1737 | dst_state->branches = src->branches; |
1738 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1739 | dst_state->first_insn_idx = src->first_insn_idx; |
1740 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1741 | for (i = 0; i <= src->curframe; i++) { |
1742 | dst = dst_state->frame[i]; | |
1743 | if (!dst) { | |
1744 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1745 | if (!dst) | |
1746 | return -ENOMEM; | |
1747 | dst_state->frame[i] = dst; | |
1748 | } | |
1749 | err = copy_func_state(dst, src->frame[i]); | |
1750 | if (err) | |
1751 | return err; | |
1752 | } | |
1753 | return 0; | |
1754 | } | |
1755 | ||
2589726d AS |
1756 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1757 | { | |
1758 | while (st) { | |
1759 | u32 br = --st->branches; | |
1760 | ||
1761 | /* WARN_ON(br > 1) technically makes sense here, | |
1762 | * but see comment in push_stack(), hence: | |
1763 | */ | |
1764 | WARN_ONCE((int)br < 0, | |
1765 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1766 | br); | |
1767 | if (br) | |
1768 | break; | |
1769 | st = st->parent; | |
1770 | } | |
1771 | } | |
1772 | ||
638f5b90 | 1773 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1774 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1775 | { |
1776 | struct bpf_verifier_state *cur = env->cur_state; | |
1777 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1778 | int err; | |
17a52670 AS |
1779 | |
1780 | if (env->head == NULL) | |
638f5b90 | 1781 | return -ENOENT; |
17a52670 | 1782 | |
638f5b90 AS |
1783 | if (cur) { |
1784 | err = copy_verifier_state(cur, &head->st); | |
1785 | if (err) | |
1786 | return err; | |
1787 | } | |
6f8a57cc AN |
1788 | if (pop_log) |
1789 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1790 | if (insn_idx) |
1791 | *insn_idx = head->insn_idx; | |
17a52670 | 1792 | if (prev_insn_idx) |
638f5b90 AS |
1793 | *prev_insn_idx = head->prev_insn_idx; |
1794 | elem = head->next; | |
1969db47 | 1795 | free_verifier_state(&head->st, false); |
638f5b90 | 1796 | kfree(head); |
17a52670 AS |
1797 | env->head = elem; |
1798 | env->stack_size--; | |
638f5b90 | 1799 | return 0; |
17a52670 AS |
1800 | } |
1801 | ||
58e2af8b | 1802 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1803 | int insn_idx, int prev_insn_idx, |
1804 | bool speculative) | |
17a52670 | 1805 | { |
638f5b90 | 1806 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1807 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1808 | int err; |
17a52670 | 1809 | |
638f5b90 | 1810 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1811 | if (!elem) |
1812 | goto err; | |
1813 | ||
17a52670 AS |
1814 | elem->insn_idx = insn_idx; |
1815 | elem->prev_insn_idx = prev_insn_idx; | |
1816 | elem->next = env->head; | |
6f8a57cc | 1817 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1818 | env->head = elem; |
1819 | env->stack_size++; | |
1969db47 AS |
1820 | err = copy_verifier_state(&elem->st, cur); |
1821 | if (err) | |
1822 | goto err; | |
979d63d5 | 1823 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1824 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1825 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1826 | env->stack_size); | |
17a52670 AS |
1827 | goto err; |
1828 | } | |
2589726d AS |
1829 | if (elem->st.parent) { |
1830 | ++elem->st.parent->branches; | |
1831 | /* WARN_ON(branches > 2) technically makes sense here, | |
1832 | * but | |
1833 | * 1. speculative states will bump 'branches' for non-branch | |
1834 | * instructions | |
1835 | * 2. is_state_visited() heuristics may decide not to create | |
1836 | * a new state for a sequence of branches and all such current | |
1837 | * and cloned states will be pointing to a single parent state | |
1838 | * which might have large 'branches' count. | |
1839 | */ | |
1840 | } | |
17a52670 AS |
1841 | return &elem->st; |
1842 | err: | |
5896351e AS |
1843 | free_verifier_state(env->cur_state, true); |
1844 | env->cur_state = NULL; | |
17a52670 | 1845 | /* pop all elements and return */ |
6f8a57cc | 1846 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1847 | return NULL; |
1848 | } | |
1849 | ||
1850 | #define CALLER_SAVED_REGS 6 | |
1851 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1852 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1853 | }; | |
1854 | ||
e688c3db AS |
1855 | /* This helper doesn't clear reg->id */ |
1856 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1857 | { |
b03c9f9f EC |
1858 | reg->var_off = tnum_const(imm); |
1859 | reg->smin_value = (s64)imm; | |
1860 | reg->smax_value = (s64)imm; | |
1861 | reg->umin_value = imm; | |
1862 | reg->umax_value = imm; | |
3f50f132 JF |
1863 | |
1864 | reg->s32_min_value = (s32)imm; | |
1865 | reg->s32_max_value = (s32)imm; | |
1866 | reg->u32_min_value = (u32)imm; | |
1867 | reg->u32_max_value = (u32)imm; | |
1868 | } | |
1869 | ||
e688c3db AS |
1870 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1871 | * known to have the value @imm. | |
1872 | */ | |
1873 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1874 | { | |
a73bf9f2 | 1875 | /* Clear off and union(map_ptr, range) */ |
e688c3db AS |
1876 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
1877 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
a73bf9f2 AN |
1878 | reg->id = 0; |
1879 | reg->ref_obj_id = 0; | |
e688c3db AS |
1880 | ___mark_reg_known(reg, imm); |
1881 | } | |
1882 | ||
3f50f132 JF |
1883 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1884 | { | |
1885 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1886 | reg->s32_min_value = (s32)imm; | |
1887 | reg->s32_max_value = (s32)imm; | |
1888 | reg->u32_min_value = (u32)imm; | |
1889 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1890 | } |
1891 | ||
f1174f77 EC |
1892 | /* Mark the 'variable offset' part of a register as zero. This should be |
1893 | * used only on registers holding a pointer type. | |
1894 | */ | |
1895 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1896 | { |
b03c9f9f | 1897 | __mark_reg_known(reg, 0); |
f1174f77 | 1898 | } |
a9789ef9 | 1899 | |
cc2b14d5 AS |
1900 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1901 | { | |
1902 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1903 | reg->type = SCALAR_VALUE; |
1904 | } | |
1905 | ||
61bd5218 JK |
1906 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1907 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1908 | { |
1909 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1910 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1911 | /* Something bad happened, let's kill all regs */ |
1912 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1913 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1914 | return; |
1915 | } | |
1916 | __mark_reg_known_zero(regs + regno); | |
1917 | } | |
1918 | ||
27060531 | 1919 | static void __mark_dynptr_reg(struct bpf_reg_state *reg, enum bpf_dynptr_type type, |
f8064ab9 | 1920 | bool first_slot, int dynptr_id) |
27060531 KKD |
1921 | { |
1922 | /* reg->type has no meaning for STACK_DYNPTR, but when we set reg for | |
1923 | * callback arguments, it does need to be CONST_PTR_TO_DYNPTR, so simply | |
1924 | * set it unconditionally as it is ignored for STACK_DYNPTR anyway. | |
1925 | */ | |
1926 | __mark_reg_known_zero(reg); | |
1927 | reg->type = CONST_PTR_TO_DYNPTR; | |
f8064ab9 KKD |
1928 | /* Give each dynptr a unique id to uniquely associate slices to it. */ |
1929 | reg->id = dynptr_id; | |
27060531 KKD |
1930 | reg->dynptr.type = type; |
1931 | reg->dynptr.first_slot = first_slot; | |
1932 | } | |
1933 | ||
4ddb7416 DB |
1934 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1935 | { | |
c25b2ae1 | 1936 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
4ddb7416 DB |
1937 | const struct bpf_map *map = reg->map_ptr; |
1938 | ||
1939 | if (map->inner_map_meta) { | |
1940 | reg->type = CONST_PTR_TO_MAP; | |
1941 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1942 | /* transfer reg's id which is unique for every map_lookup_elem |
1943 | * as UID of the inner map. | |
1944 | */ | |
db559117 | 1945 | if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER)) |
34d11a44 | 1946 | reg->map_uid = reg->id; |
4ddb7416 DB |
1947 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1948 | reg->type = PTR_TO_XDP_SOCK; | |
1949 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1950 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1951 | reg->type = PTR_TO_SOCKET; | |
1952 | } else { | |
1953 | reg->type = PTR_TO_MAP_VALUE; | |
1954 | } | |
c25b2ae1 | 1955 | return; |
4ddb7416 | 1956 | } |
c25b2ae1 HL |
1957 | |
1958 | reg->type &= ~PTR_MAYBE_NULL; | |
4ddb7416 DB |
1959 | } |
1960 | ||
5d92ddc3 DM |
1961 | static void mark_reg_graph_node(struct bpf_reg_state *regs, u32 regno, |
1962 | struct btf_field_graph_root *ds_head) | |
1963 | { | |
1964 | __mark_reg_known_zero(®s[regno]); | |
1965 | regs[regno].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
1966 | regs[regno].btf = ds_head->btf; | |
1967 | regs[regno].btf_id = ds_head->value_btf_id; | |
1968 | regs[regno].off = ds_head->node_offset; | |
1969 | } | |
1970 | ||
de8f3a83 DB |
1971 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1972 | { | |
1973 | return type_is_pkt_pointer(reg->type); | |
1974 | } | |
1975 | ||
1976 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1977 | { | |
1978 | return reg_is_pkt_pointer(reg) || | |
1979 | reg->type == PTR_TO_PACKET_END; | |
1980 | } | |
1981 | ||
66e3a13e JK |
1982 | static bool reg_is_dynptr_slice_pkt(const struct bpf_reg_state *reg) |
1983 | { | |
1984 | return base_type(reg->type) == PTR_TO_MEM && | |
1985 | (reg->type & DYNPTR_TYPE_SKB || reg->type & DYNPTR_TYPE_XDP); | |
1986 | } | |
1987 | ||
de8f3a83 DB |
1988 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
1989 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1990 | enum bpf_reg_type which) | |
1991 | { | |
1992 | /* The register can already have a range from prior markings. | |
1993 | * This is fine as long as it hasn't been advanced from its | |
1994 | * origin. | |
1995 | */ | |
1996 | return reg->type == which && | |
1997 | reg->id == 0 && | |
1998 | reg->off == 0 && | |
1999 | tnum_equals_const(reg->var_off, 0); | |
2000 | } | |
2001 | ||
3f50f132 JF |
2002 | /* Reset the min/max bounds of a register */ |
2003 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
2004 | { | |
2005 | reg->smin_value = S64_MIN; | |
2006 | reg->smax_value = S64_MAX; | |
2007 | reg->umin_value = 0; | |
2008 | reg->umax_value = U64_MAX; | |
2009 | ||
2010 | reg->s32_min_value = S32_MIN; | |
2011 | reg->s32_max_value = S32_MAX; | |
2012 | reg->u32_min_value = 0; | |
2013 | reg->u32_max_value = U32_MAX; | |
2014 | } | |
2015 | ||
2016 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
2017 | { | |
2018 | reg->smin_value = S64_MIN; | |
2019 | reg->smax_value = S64_MAX; | |
2020 | reg->umin_value = 0; | |
2021 | reg->umax_value = U64_MAX; | |
2022 | } | |
2023 | ||
2024 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
2025 | { | |
2026 | reg->s32_min_value = S32_MIN; | |
2027 | reg->s32_max_value = S32_MAX; | |
2028 | reg->u32_min_value = 0; | |
2029 | reg->u32_max_value = U32_MAX; | |
2030 | } | |
2031 | ||
2032 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
2033 | { | |
2034 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
2035 | ||
2036 | /* min signed is max(sign bit) | min(other bits) */ | |
2037 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
2038 | var32_off.value | (var32_off.mask & S32_MIN)); | |
2039 | /* max signed is min(sign bit) | max(other bits) */ | |
2040 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
2041 | var32_off.value | (var32_off.mask & S32_MAX)); | |
2042 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
2043 | reg->u32_max_value = min(reg->u32_max_value, | |
2044 | (u32)(var32_off.value | var32_off.mask)); | |
2045 | } | |
2046 | ||
2047 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
2048 | { |
2049 | /* min signed is max(sign bit) | min(other bits) */ | |
2050 | reg->smin_value = max_t(s64, reg->smin_value, | |
2051 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
2052 | /* max signed is min(sign bit) | max(other bits) */ | |
2053 | reg->smax_value = min_t(s64, reg->smax_value, | |
2054 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
2055 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
2056 | reg->umax_value = min(reg->umax_value, | |
2057 | reg->var_off.value | reg->var_off.mask); | |
2058 | } | |
2059 | ||
3f50f132 JF |
2060 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
2061 | { | |
2062 | __update_reg32_bounds(reg); | |
2063 | __update_reg64_bounds(reg); | |
2064 | } | |
2065 | ||
b03c9f9f | 2066 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
2067 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
2068 | { | |
2069 | /* Learn sign from signed bounds. | |
2070 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
2071 | * are the same, so combine. This works even in the negative case, e.g. | |
2072 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
2073 | */ | |
2074 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
2075 | reg->s32_min_value = reg->u32_min_value = | |
2076 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
2077 | reg->s32_max_value = reg->u32_max_value = | |
2078 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
2079 | return; | |
2080 | } | |
2081 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
2082 | * boundary, so we must be careful. | |
2083 | */ | |
2084 | if ((s32)reg->u32_max_value >= 0) { | |
2085 | /* Positive. We can't learn anything from the smin, but smax | |
2086 | * is positive, hence safe. | |
2087 | */ | |
2088 | reg->s32_min_value = reg->u32_min_value; | |
2089 | reg->s32_max_value = reg->u32_max_value = | |
2090 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
2091 | } else if ((s32)reg->u32_min_value < 0) { | |
2092 | /* Negative. We can't learn anything from the smax, but smin | |
2093 | * is negative, hence safe. | |
2094 | */ | |
2095 | reg->s32_min_value = reg->u32_min_value = | |
2096 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
2097 | reg->s32_max_value = reg->u32_max_value; | |
2098 | } | |
2099 | } | |
2100 | ||
2101 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
2102 | { |
2103 | /* Learn sign from signed bounds. | |
2104 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
2105 | * are the same, so combine. This works even in the negative case, e.g. | |
2106 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
2107 | */ | |
2108 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
2109 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
2110 | reg->umin_value); | |
2111 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
2112 | reg->umax_value); | |
2113 | return; | |
2114 | } | |
2115 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
2116 | * boundary, so we must be careful. | |
2117 | */ | |
2118 | if ((s64)reg->umax_value >= 0) { | |
2119 | /* Positive. We can't learn anything from the smin, but smax | |
2120 | * is positive, hence safe. | |
2121 | */ | |
2122 | reg->smin_value = reg->umin_value; | |
2123 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
2124 | reg->umax_value); | |
2125 | } else if ((s64)reg->umin_value < 0) { | |
2126 | /* Negative. We can't learn anything from the smax, but smin | |
2127 | * is negative, hence safe. | |
2128 | */ | |
2129 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
2130 | reg->umin_value); | |
2131 | reg->smax_value = reg->umax_value; | |
2132 | } | |
2133 | } | |
2134 | ||
3f50f132 JF |
2135 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
2136 | { | |
2137 | __reg32_deduce_bounds(reg); | |
2138 | __reg64_deduce_bounds(reg); | |
2139 | } | |
2140 | ||
b03c9f9f EC |
2141 | /* Attempts to improve var_off based on unsigned min/max information */ |
2142 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
2143 | { | |
3f50f132 JF |
2144 | struct tnum var64_off = tnum_intersect(reg->var_off, |
2145 | tnum_range(reg->umin_value, | |
2146 | reg->umax_value)); | |
2147 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
2148 | tnum_range(reg->u32_min_value, | |
2149 | reg->u32_max_value)); | |
2150 | ||
2151 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
2152 | } |
2153 | ||
3844d153 DB |
2154 | static void reg_bounds_sync(struct bpf_reg_state *reg) |
2155 | { | |
2156 | /* We might have learned new bounds from the var_off. */ | |
2157 | __update_reg_bounds(reg); | |
2158 | /* We might have learned something about the sign bit. */ | |
2159 | __reg_deduce_bounds(reg); | |
2160 | /* We might have learned some bits from the bounds. */ | |
2161 | __reg_bound_offset(reg); | |
2162 | /* Intersecting with the old var_off might have improved our bounds | |
2163 | * slightly, e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
2164 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
2165 | */ | |
2166 | __update_reg_bounds(reg); | |
2167 | } | |
2168 | ||
e572ff80 DB |
2169 | static bool __reg32_bound_s64(s32 a) |
2170 | { | |
2171 | return a >= 0 && a <= S32_MAX; | |
2172 | } | |
2173 | ||
3f50f132 | 2174 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 2175 | { |
3f50f132 JF |
2176 | reg->umin_value = reg->u32_min_value; |
2177 | reg->umax_value = reg->u32_max_value; | |
e572ff80 DB |
2178 | |
2179 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must | |
2180 | * be positive otherwise set to worse case bounds and refine later | |
2181 | * from tnum. | |
3f50f132 | 2182 | */ |
e572ff80 DB |
2183 | if (__reg32_bound_s64(reg->s32_min_value) && |
2184 | __reg32_bound_s64(reg->s32_max_value)) { | |
3a71dc36 | 2185 | reg->smin_value = reg->s32_min_value; |
e572ff80 DB |
2186 | reg->smax_value = reg->s32_max_value; |
2187 | } else { | |
3a71dc36 | 2188 | reg->smin_value = 0; |
e572ff80 DB |
2189 | reg->smax_value = U32_MAX; |
2190 | } | |
3f50f132 JF |
2191 | } |
2192 | ||
2193 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
2194 | { | |
2195 | /* special case when 64-bit register has upper 32-bit register | |
2196 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
2197 | * allowing us to use 32-bit bounds directly, | |
2198 | */ | |
2199 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
2200 | __reg_assign_32_into_64(reg); | |
2201 | } else { | |
2202 | /* Otherwise the best we can do is push lower 32bit known and | |
2203 | * unknown bits into register (var_off set from jmp logic) | |
2204 | * then learn as much as possible from the 64-bit tnum | |
2205 | * known and unknown bits. The previous smin/smax bounds are | |
2206 | * invalid here because of jmp32 compare so mark them unknown | |
2207 | * so they do not impact tnum bounds calculation. | |
2208 | */ | |
2209 | __mark_reg64_unbounded(reg); | |
3f50f132 | 2210 | } |
3844d153 | 2211 | reg_bounds_sync(reg); |
3f50f132 JF |
2212 | } |
2213 | ||
2214 | static bool __reg64_bound_s32(s64 a) | |
2215 | { | |
388e2c0b | 2216 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
2217 | } |
2218 | ||
2219 | static bool __reg64_bound_u32(u64 a) | |
2220 | { | |
b9979db8 | 2221 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
2222 | } |
2223 | ||
2224 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
2225 | { | |
2226 | __mark_reg32_unbounded(reg); | |
b0270958 | 2227 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 2228 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 2229 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 2230 | } |
10bf4e83 | 2231 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 2232 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 2233 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 2234 | } |
3844d153 | 2235 | reg_bounds_sync(reg); |
b03c9f9f EC |
2236 | } |
2237 | ||
f1174f77 | 2238 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
2239 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
2240 | struct bpf_reg_state *reg) | |
f1174f77 | 2241 | { |
a9c676bc | 2242 | /* |
a73bf9f2 | 2243 | * Clear type, off, and union(map_ptr, range) and |
a9c676bc AS |
2244 | * padding between 'type' and union |
2245 | */ | |
2246 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 2247 | reg->type = SCALAR_VALUE; |
a73bf9f2 AN |
2248 | reg->id = 0; |
2249 | reg->ref_obj_id = 0; | |
f1174f77 | 2250 | reg->var_off = tnum_unknown; |
f4d7e40a | 2251 | reg->frameno = 0; |
be2ef816 | 2252 | reg->precise = !env->bpf_capable; |
b03c9f9f | 2253 | __mark_reg_unbounded(reg); |
f1174f77 EC |
2254 | } |
2255 | ||
61bd5218 JK |
2256 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
2257 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
2258 | { |
2259 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 2260 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
2261 | /* Something bad happened, let's kill all regs except FP */ |
2262 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 2263 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
2264 | return; |
2265 | } | |
f54c7898 | 2266 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
2267 | } |
2268 | ||
f54c7898 DB |
2269 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
2270 | struct bpf_reg_state *reg) | |
f1174f77 | 2271 | { |
f54c7898 | 2272 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
2273 | reg->type = NOT_INIT; |
2274 | } | |
2275 | ||
61bd5218 JK |
2276 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
2277 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
2278 | { |
2279 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 2280 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
2281 | /* Something bad happened, let's kill all regs except FP */ |
2282 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 2283 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
2284 | return; |
2285 | } | |
f54c7898 | 2286 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
2287 | } |
2288 | ||
41c48f3a AI |
2289 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
2290 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f | 2291 | enum bpf_reg_type reg_type, |
c6f1bfe8 YS |
2292 | struct btf *btf, u32 btf_id, |
2293 | enum bpf_type_flag flag) | |
41c48f3a AI |
2294 | { |
2295 | if (reg_type == SCALAR_VALUE) { | |
2296 | mark_reg_unknown(env, regs, regno); | |
2297 | return; | |
2298 | } | |
2299 | mark_reg_known_zero(env, regs, regno); | |
c6f1bfe8 | 2300 | regs[regno].type = PTR_TO_BTF_ID | flag; |
22dc4a0f | 2301 | regs[regno].btf = btf; |
41c48f3a AI |
2302 | regs[regno].btf_id = btf_id; |
2303 | } | |
2304 | ||
5327ed3d | 2305 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 2306 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 2307 | struct bpf_func_state *state) |
17a52670 | 2308 | { |
f4d7e40a | 2309 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
2310 | int i; |
2311 | ||
dc503a8a | 2312 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 2313 | mark_reg_not_init(env, regs, i); |
dc503a8a | 2314 | regs[i].live = REG_LIVE_NONE; |
679c782d | 2315 | regs[i].parent = NULL; |
5327ed3d | 2316 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 2317 | } |
17a52670 AS |
2318 | |
2319 | /* frame pointer */ | |
f1174f77 | 2320 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 2321 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 2322 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
2323 | } |
2324 | ||
f4d7e40a AS |
2325 | #define BPF_MAIN_FUNC (-1) |
2326 | static void init_func_state(struct bpf_verifier_env *env, | |
2327 | struct bpf_func_state *state, | |
2328 | int callsite, int frameno, int subprogno) | |
2329 | { | |
2330 | state->callsite = callsite; | |
2331 | state->frameno = frameno; | |
2332 | state->subprogno = subprogno; | |
1bfe26fb | 2333 | state->callback_ret_range = tnum_range(0, 0); |
f4d7e40a | 2334 | init_reg_state(env, state); |
0f55f9ed | 2335 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
2336 | } |
2337 | ||
bfc6bb74 AS |
2338 | /* Similar to push_stack(), but for async callbacks */ |
2339 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
2340 | int insn_idx, int prev_insn_idx, | |
2341 | int subprog) | |
2342 | { | |
2343 | struct bpf_verifier_stack_elem *elem; | |
2344 | struct bpf_func_state *frame; | |
2345 | ||
2346 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
2347 | if (!elem) | |
2348 | goto err; | |
2349 | ||
2350 | elem->insn_idx = insn_idx; | |
2351 | elem->prev_insn_idx = prev_insn_idx; | |
2352 | elem->next = env->head; | |
2353 | elem->log_pos = env->log.len_used; | |
2354 | env->head = elem; | |
2355 | env->stack_size++; | |
2356 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
2357 | verbose(env, | |
2358 | "The sequence of %d jumps is too complex for async cb.\n", | |
2359 | env->stack_size); | |
2360 | goto err; | |
2361 | } | |
2362 | /* Unlike push_stack() do not copy_verifier_state(). | |
2363 | * The caller state doesn't matter. | |
2364 | * This is async callback. It starts in a fresh stack. | |
2365 | * Initialize it similar to do_check_common(). | |
2366 | */ | |
2367 | elem->st.branches = 1; | |
2368 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
2369 | if (!frame) | |
2370 | goto err; | |
2371 | init_func_state(env, frame, | |
2372 | BPF_MAIN_FUNC /* callsite */, | |
2373 | 0 /* frameno within this callchain */, | |
2374 | subprog /* subprog number within this prog */); | |
2375 | elem->st.frame[0] = frame; | |
2376 | return &elem->st; | |
2377 | err: | |
2378 | free_verifier_state(env->cur_state, true); | |
2379 | env->cur_state = NULL; | |
2380 | /* pop all elements and return */ | |
2381 | while (!pop_stack(env, NULL, NULL, false)); | |
2382 | return NULL; | |
2383 | } | |
2384 | ||
2385 | ||
17a52670 AS |
2386 | enum reg_arg_type { |
2387 | SRC_OP, /* register is used as source operand */ | |
2388 | DST_OP, /* register is used as destination operand */ | |
2389 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
2390 | }; | |
2391 | ||
cc8b0b92 AS |
2392 | static int cmp_subprogs(const void *a, const void *b) |
2393 | { | |
9c8105bd JW |
2394 | return ((struct bpf_subprog_info *)a)->start - |
2395 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
2396 | } |
2397 | ||
2398 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
2399 | { | |
9c8105bd | 2400 | struct bpf_subprog_info *p; |
cc8b0b92 | 2401 | |
9c8105bd JW |
2402 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
2403 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
2404 | if (!p) |
2405 | return -ENOENT; | |
9c8105bd | 2406 | return p - env->subprog_info; |
cc8b0b92 AS |
2407 | |
2408 | } | |
2409 | ||
2410 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
2411 | { | |
2412 | int insn_cnt = env->prog->len; | |
2413 | int ret; | |
2414 | ||
2415 | if (off >= insn_cnt || off < 0) { | |
2416 | verbose(env, "call to invalid destination\n"); | |
2417 | return -EINVAL; | |
2418 | } | |
2419 | ret = find_subprog(env, off); | |
2420 | if (ret >= 0) | |
282a0f46 | 2421 | return ret; |
4cb3d99c | 2422 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
2423 | verbose(env, "too many subprograms\n"); |
2424 | return -E2BIG; | |
2425 | } | |
e6ac2450 | 2426 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
2427 | env->subprog_info[env->subprog_cnt++].start = off; |
2428 | sort(env->subprog_info, env->subprog_cnt, | |
2429 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 2430 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
2431 | } |
2432 | ||
2357672c KKD |
2433 | #define MAX_KFUNC_DESCS 256 |
2434 | #define MAX_KFUNC_BTFS 256 | |
2435 | ||
e6ac2450 MKL |
2436 | struct bpf_kfunc_desc { |
2437 | struct btf_func_model func_model; | |
2438 | u32 func_id; | |
2439 | s32 imm; | |
2357672c KKD |
2440 | u16 offset; |
2441 | }; | |
2442 | ||
2443 | struct bpf_kfunc_btf { | |
2444 | struct btf *btf; | |
2445 | struct module *module; | |
2446 | u16 offset; | |
e6ac2450 MKL |
2447 | }; |
2448 | ||
e6ac2450 MKL |
2449 | struct bpf_kfunc_desc_tab { |
2450 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
2451 | u32 nr_descs; | |
2452 | }; | |
2453 | ||
2357672c KKD |
2454 | struct bpf_kfunc_btf_tab { |
2455 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
2456 | u32 nr_descs; | |
2457 | }; | |
2458 | ||
2459 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
2460 | { |
2461 | const struct bpf_kfunc_desc *d0 = a; | |
2462 | const struct bpf_kfunc_desc *d1 = b; | |
2463 | ||
2464 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
2465 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
2466 | } | |
2467 | ||
2468 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
2469 | { | |
2470 | const struct bpf_kfunc_btf *d0 = a; | |
2471 | const struct bpf_kfunc_btf *d1 = b; | |
2472 | ||
2473 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
2474 | } |
2475 | ||
2476 | static const struct bpf_kfunc_desc * | |
2357672c | 2477 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
2478 | { |
2479 | struct bpf_kfunc_desc desc = { | |
2480 | .func_id = func_id, | |
2357672c | 2481 | .offset = offset, |
e6ac2450 MKL |
2482 | }; |
2483 | struct bpf_kfunc_desc_tab *tab; | |
2484 | ||
2485 | tab = prog->aux->kfunc_tab; | |
2486 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
2487 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
2488 | } | |
2489 | ||
2490 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
b202d844 | 2491 | s16 offset) |
2357672c KKD |
2492 | { |
2493 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
2494 | struct bpf_kfunc_btf_tab *tab; | |
2495 | struct bpf_kfunc_btf *b; | |
2496 | struct module *mod; | |
2497 | struct btf *btf; | |
2498 | int btf_fd; | |
2499 | ||
2500 | tab = env->prog->aux->kfunc_btf_tab; | |
2501 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
2502 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
2503 | if (!b) { | |
2504 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
2505 | verbose(env, "too many different module BTFs\n"); | |
2506 | return ERR_PTR(-E2BIG); | |
2507 | } | |
2508 | ||
2509 | if (bpfptr_is_null(env->fd_array)) { | |
2510 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
2511 | return ERR_PTR(-EPROTO); | |
2512 | } | |
2513 | ||
2514 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
2515 | offset * sizeof(btf_fd), | |
2516 | sizeof(btf_fd))) | |
2517 | return ERR_PTR(-EFAULT); | |
2518 | ||
2519 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
2520 | if (IS_ERR(btf)) { |
2521 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 2522 | return btf; |
588cd7ef | 2523 | } |
2357672c KKD |
2524 | |
2525 | if (!btf_is_module(btf)) { | |
2526 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
2527 | btf_put(btf); | |
2528 | return ERR_PTR(-EINVAL); | |
2529 | } | |
2530 | ||
2531 | mod = btf_try_get_module(btf); | |
2532 | if (!mod) { | |
2533 | btf_put(btf); | |
2534 | return ERR_PTR(-ENXIO); | |
2535 | } | |
2536 | ||
2537 | b = &tab->descs[tab->nr_descs++]; | |
2538 | b->btf = btf; | |
2539 | b->module = mod; | |
2540 | b->offset = offset; | |
2541 | ||
2542 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2543 | kfunc_btf_cmp_by_off, NULL); | |
2544 | } | |
2357672c | 2545 | return b->btf; |
e6ac2450 MKL |
2546 | } |
2547 | ||
2357672c KKD |
2548 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
2549 | { | |
2550 | if (!tab) | |
2551 | return; | |
2552 | ||
2553 | while (tab->nr_descs--) { | |
2554 | module_put(tab->descs[tab->nr_descs].module); | |
2555 | btf_put(tab->descs[tab->nr_descs].btf); | |
2556 | } | |
2557 | kfree(tab); | |
2558 | } | |
2559 | ||
43bf0878 | 2560 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset) |
2357672c | 2561 | { |
2357672c KKD |
2562 | if (offset) { |
2563 | if (offset < 0) { | |
2564 | /* In the future, this can be allowed to increase limit | |
2565 | * of fd index into fd_array, interpreted as u16. | |
2566 | */ | |
2567 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
2568 | return ERR_PTR(-EINVAL); | |
2569 | } | |
2570 | ||
b202d844 | 2571 | return __find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2572 | } |
2573 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
2574 | } |
2575 | ||
2357672c | 2576 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
2577 | { |
2578 | const struct btf_type *func, *func_proto; | |
2357672c | 2579 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
2580 | struct bpf_kfunc_desc_tab *tab; |
2581 | struct bpf_prog_aux *prog_aux; | |
2582 | struct bpf_kfunc_desc *desc; | |
2583 | const char *func_name; | |
2357672c | 2584 | struct btf *desc_btf; |
8cbf062a | 2585 | unsigned long call_imm; |
e6ac2450 MKL |
2586 | unsigned long addr; |
2587 | int err; | |
2588 | ||
2589 | prog_aux = env->prog->aux; | |
2590 | tab = prog_aux->kfunc_tab; | |
2357672c | 2591 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
2592 | if (!tab) { |
2593 | if (!btf_vmlinux) { | |
2594 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
2595 | return -ENOTSUPP; | |
2596 | } | |
2597 | ||
2598 | if (!env->prog->jit_requested) { | |
2599 | verbose(env, "JIT is required for calling kernel function\n"); | |
2600 | return -ENOTSUPP; | |
2601 | } | |
2602 | ||
2603 | if (!bpf_jit_supports_kfunc_call()) { | |
2604 | verbose(env, "JIT does not support calling kernel function\n"); | |
2605 | return -ENOTSUPP; | |
2606 | } | |
2607 | ||
2608 | if (!env->prog->gpl_compatible) { | |
2609 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
2610 | return -EINVAL; | |
2611 | } | |
2612 | ||
2613 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
2614 | if (!tab) | |
2615 | return -ENOMEM; | |
2616 | prog_aux->kfunc_tab = tab; | |
2617 | } | |
2618 | ||
a5d82727 KKD |
2619 | /* func_id == 0 is always invalid, but instead of returning an error, be |
2620 | * conservative and wait until the code elimination pass before returning | |
2621 | * error, so that invalid calls that get pruned out can be in BPF programs | |
2622 | * loaded from userspace. It is also required that offset be untouched | |
2623 | * for such calls. | |
2624 | */ | |
2625 | if (!func_id && !offset) | |
2626 | return 0; | |
2627 | ||
2357672c KKD |
2628 | if (!btf_tab && offset) { |
2629 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
2630 | if (!btf_tab) | |
2631 | return -ENOMEM; | |
2632 | prog_aux->kfunc_btf_tab = btf_tab; | |
2633 | } | |
2634 | ||
43bf0878 | 2635 | desc_btf = find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2636 | if (IS_ERR(desc_btf)) { |
2637 | verbose(env, "failed to find BTF for kernel function\n"); | |
2638 | return PTR_ERR(desc_btf); | |
2639 | } | |
2640 | ||
2641 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
2642 | return 0; |
2643 | ||
2644 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
2645 | verbose(env, "too many different kernel function calls\n"); | |
2646 | return -E2BIG; | |
2647 | } | |
2648 | ||
2357672c | 2649 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
2650 | if (!func || !btf_type_is_func(func)) { |
2651 | verbose(env, "kernel btf_id %u is not a function\n", | |
2652 | func_id); | |
2653 | return -EINVAL; | |
2654 | } | |
2357672c | 2655 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
2656 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
2657 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
2658 | func_id); | |
2659 | return -EINVAL; | |
2660 | } | |
2661 | ||
2357672c | 2662 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
2663 | addr = kallsyms_lookup_name(func_name); |
2664 | if (!addr) { | |
2665 | verbose(env, "cannot find address for kernel function %s\n", | |
2666 | func_name); | |
2667 | return -EINVAL; | |
2668 | } | |
2669 | ||
8cbf062a HT |
2670 | call_imm = BPF_CALL_IMM(addr); |
2671 | /* Check whether or not the relative offset overflows desc->imm */ | |
2672 | if ((unsigned long)(s32)call_imm != call_imm) { | |
2673 | verbose(env, "address of kernel function %s is out of range\n", | |
2674 | func_name); | |
2675 | return -EINVAL; | |
2676 | } | |
2677 | ||
3d76a4d3 SF |
2678 | if (bpf_dev_bound_kfunc_id(func_id)) { |
2679 | err = bpf_dev_bound_kfunc_check(&env->log, prog_aux); | |
2680 | if (err) | |
2681 | return err; | |
2682 | } | |
2683 | ||
e6ac2450 MKL |
2684 | desc = &tab->descs[tab->nr_descs++]; |
2685 | desc->func_id = func_id; | |
8cbf062a | 2686 | desc->imm = call_imm; |
2357672c KKD |
2687 | desc->offset = offset; |
2688 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
2689 | func_proto, func_name, |
2690 | &desc->func_model); | |
2691 | if (!err) | |
2692 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 2693 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
2694 | return err; |
2695 | } | |
2696 | ||
2697 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
2698 | { | |
2699 | const struct bpf_kfunc_desc *d0 = a; | |
2700 | const struct bpf_kfunc_desc *d1 = b; | |
2701 | ||
2702 | if (d0->imm > d1->imm) | |
2703 | return 1; | |
2704 | else if (d0->imm < d1->imm) | |
2705 | return -1; | |
2706 | return 0; | |
2707 | } | |
2708 | ||
2709 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
2710 | { | |
2711 | struct bpf_kfunc_desc_tab *tab; | |
2712 | ||
2713 | tab = prog->aux->kfunc_tab; | |
2714 | if (!tab) | |
2715 | return; | |
2716 | ||
2717 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2718 | kfunc_desc_cmp_by_imm, NULL); | |
2719 | } | |
2720 | ||
2721 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
2722 | { | |
2723 | return !!prog->aux->kfunc_tab; | |
2724 | } | |
2725 | ||
2726 | const struct btf_func_model * | |
2727 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2728 | const struct bpf_insn *insn) | |
2729 | { | |
2730 | const struct bpf_kfunc_desc desc = { | |
2731 | .imm = insn->imm, | |
2732 | }; | |
2733 | const struct bpf_kfunc_desc *res; | |
2734 | struct bpf_kfunc_desc_tab *tab; | |
2735 | ||
2736 | tab = prog->aux->kfunc_tab; | |
2737 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
2738 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
2739 | ||
2740 | return res ? &res->func_model : NULL; | |
2741 | } | |
2742 | ||
2743 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2744 | { |
9c8105bd | 2745 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2746 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2747 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2748 | |
f910cefa JW |
2749 | /* Add entry function. */ |
2750 | ret = add_subprog(env, 0); | |
e6ac2450 | 2751 | if (ret) |
f910cefa JW |
2752 | return ret; |
2753 | ||
e6ac2450 MKL |
2754 | for (i = 0; i < insn_cnt; i++, insn++) { |
2755 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2756 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2757 | continue; |
e6ac2450 | 2758 | |
2c78ee89 | 2759 | if (!env->bpf_capable) { |
e6ac2450 | 2760 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2761 | return -EPERM; |
2762 | } | |
e6ac2450 | 2763 | |
3990ed4c | 2764 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2765 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2766 | else |
2357672c | 2767 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2768 | |
cc8b0b92 AS |
2769 | if (ret < 0) |
2770 | return ret; | |
2771 | } | |
2772 | ||
4cb3d99c JW |
2773 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2774 | * logic. 'subprog_cnt' should not be increased. | |
2775 | */ | |
2776 | subprog[env->subprog_cnt].start = insn_cnt; | |
2777 | ||
06ee7115 | 2778 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2779 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2780 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2781 | |
e6ac2450 MKL |
2782 | return 0; |
2783 | } | |
2784 | ||
2785 | static int check_subprogs(struct bpf_verifier_env *env) | |
2786 | { | |
2787 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2788 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2789 | struct bpf_insn *insn = env->prog->insnsi; | |
2790 | int insn_cnt = env->prog->len; | |
2791 | ||
cc8b0b92 | 2792 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2793 | subprog_start = subprog[cur_subprog].start; |
2794 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2795 | for (i = 0; i < insn_cnt; i++) { |
2796 | u8 code = insn[i].code; | |
2797 | ||
7f6e4312 | 2798 | if (code == (BPF_JMP | BPF_CALL) && |
df2ccc18 IL |
2799 | insn[i].src_reg == 0 && |
2800 | insn[i].imm == BPF_FUNC_tail_call) | |
7f6e4312 | 2801 | subprog[cur_subprog].has_tail_call = true; |
09b28d76 AS |
2802 | if (BPF_CLASS(code) == BPF_LD && |
2803 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2804 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2805 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2806 | goto next; |
2807 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2808 | goto next; | |
2809 | off = i + insn[i].off + 1; | |
2810 | if (off < subprog_start || off >= subprog_end) { | |
2811 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2812 | return -EINVAL; | |
2813 | } | |
2814 | next: | |
2815 | if (i == subprog_end - 1) { | |
2816 | /* to avoid fall-through from one subprog into another | |
2817 | * the last insn of the subprog should be either exit | |
2818 | * or unconditional jump back | |
2819 | */ | |
2820 | if (code != (BPF_JMP | BPF_EXIT) && | |
2821 | code != (BPF_JMP | BPF_JA)) { | |
2822 | verbose(env, "last insn is not an exit or jmp\n"); | |
2823 | return -EINVAL; | |
2824 | } | |
2825 | subprog_start = subprog_end; | |
4cb3d99c JW |
2826 | cur_subprog++; |
2827 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2828 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2829 | } |
2830 | } | |
2831 | return 0; | |
2832 | } | |
2833 | ||
679c782d EC |
2834 | /* Parentage chain of this register (or stack slot) should take care of all |
2835 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2836 | */ | |
f4d7e40a | 2837 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2838 | const struct bpf_reg_state *state, |
5327ed3d | 2839 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2840 | { |
2841 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2842 | int cnt = 0; |
dc503a8a EC |
2843 | |
2844 | while (parent) { | |
2845 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2846 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2847 | break; |
9242b5f5 AS |
2848 | if (parent->live & REG_LIVE_DONE) { |
2849 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
c25b2ae1 | 2850 | reg_type_str(env, parent->type), |
9242b5f5 AS |
2851 | parent->var_off.value, parent->off); |
2852 | return -EFAULT; | |
2853 | } | |
5327ed3d JW |
2854 | /* The first condition is more likely to be true than the |
2855 | * second, checked it first. | |
2856 | */ | |
2857 | if ((parent->live & REG_LIVE_READ) == flag || | |
2858 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2859 | /* The parentage chain never changes and |
2860 | * this parent was already marked as LIVE_READ. | |
2861 | * There is no need to keep walking the chain again and | |
2862 | * keep re-marking all parents as LIVE_READ. | |
2863 | * This case happens when the same register is read | |
2864 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2865 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2866 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2867 | */ |
2868 | break; | |
dc503a8a | 2869 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2870 | parent->live |= flag; |
2871 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2872 | if (flag == REG_LIVE_READ64) | |
2873 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2874 | state = parent; |
2875 | parent = state->parent; | |
f4d7e40a | 2876 | writes = true; |
06ee7115 | 2877 | cnt++; |
dc503a8a | 2878 | } |
06ee7115 AS |
2879 | |
2880 | if (env->longest_mark_read_walk < cnt) | |
2881 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2882 | return 0; |
dc503a8a EC |
2883 | } |
2884 | ||
d6fefa11 KKD |
2885 | static int mark_dynptr_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
2886 | { | |
2887 | struct bpf_func_state *state = func(env, reg); | |
2888 | int spi, ret; | |
2889 | ||
2890 | /* For CONST_PTR_TO_DYNPTR, it must have already been done by | |
2891 | * check_reg_arg in check_helper_call and mark_btf_func_reg_size in | |
2892 | * check_kfunc_call. | |
2893 | */ | |
2894 | if (reg->type == CONST_PTR_TO_DYNPTR) | |
2895 | return 0; | |
79168a66 KKD |
2896 | spi = dynptr_get_spi(env, reg); |
2897 | if (spi < 0) | |
2898 | return spi; | |
d6fefa11 KKD |
2899 | /* Caller ensures dynptr is valid and initialized, which means spi is in |
2900 | * bounds and spi is the first dynptr slot. Simply mark stack slot as | |
2901 | * read. | |
2902 | */ | |
2903 | ret = mark_reg_read(env, &state->stack[spi].spilled_ptr, | |
2904 | state->stack[spi].spilled_ptr.parent, REG_LIVE_READ64); | |
2905 | if (ret) | |
2906 | return ret; | |
2907 | return mark_reg_read(env, &state->stack[spi - 1].spilled_ptr, | |
2908 | state->stack[spi - 1].spilled_ptr.parent, REG_LIVE_READ64); | |
2909 | } | |
2910 | ||
06accc87 AN |
2911 | static int mark_iter_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
2912 | int spi, int nr_slots) | |
2913 | { | |
2914 | struct bpf_func_state *state = func(env, reg); | |
2915 | int err, i; | |
2916 | ||
2917 | for (i = 0; i < nr_slots; i++) { | |
2918 | struct bpf_reg_state *st = &state->stack[spi - i].spilled_ptr; | |
2919 | ||
2920 | err = mark_reg_read(env, st, st->parent, REG_LIVE_READ64); | |
2921 | if (err) | |
2922 | return err; | |
2923 | ||
2924 | mark_stack_slot_scratched(env, spi - i); | |
2925 | } | |
2926 | ||
2927 | return 0; | |
2928 | } | |
2929 | ||
5327ed3d JW |
2930 | /* This function is supposed to be used by the following 32-bit optimization |
2931 | * code only. It returns TRUE if the source or destination register operates | |
2932 | * on 64-bit, otherwise return FALSE. | |
2933 | */ | |
2934 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2935 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2936 | { | |
2937 | u8 code, class, op; | |
2938 | ||
2939 | code = insn->code; | |
2940 | class = BPF_CLASS(code); | |
2941 | op = BPF_OP(code); | |
2942 | if (class == BPF_JMP) { | |
2943 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2944 | * conservatively. | |
2945 | */ | |
2946 | if (op == BPF_EXIT) | |
2947 | return true; | |
2948 | if (op == BPF_CALL) { | |
2949 | /* BPF to BPF call will reach here because of marking | |
2950 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2951 | * don't care the register def because they are anyway | |
2952 | * marked as NOT_INIT already. | |
2953 | */ | |
2954 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2955 | return false; | |
2956 | /* Helper call will reach here because of arg type | |
2957 | * check, conservatively return TRUE. | |
2958 | */ | |
2959 | if (t == SRC_OP) | |
2960 | return true; | |
2961 | ||
2962 | return false; | |
2963 | } | |
2964 | } | |
2965 | ||
2966 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2967 | /* BPF_END always use BPF_ALU class. */ | |
2968 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2969 | return true; | |
2970 | ||
2971 | if (class == BPF_ALU || class == BPF_JMP32) | |
2972 | return false; | |
2973 | ||
2974 | if (class == BPF_LDX) { | |
2975 | if (t != SRC_OP) | |
2976 | return BPF_SIZE(code) == BPF_DW; | |
2977 | /* LDX source must be ptr. */ | |
2978 | return true; | |
2979 | } | |
2980 | ||
2981 | if (class == BPF_STX) { | |
83a28819 IL |
2982 | /* BPF_STX (including atomic variants) has multiple source |
2983 | * operands, one of which is a ptr. Check whether the caller is | |
2984 | * asking about it. | |
2985 | */ | |
2986 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2987 | return true; |
2988 | return BPF_SIZE(code) == BPF_DW; | |
2989 | } | |
2990 | ||
2991 | if (class == BPF_LD) { | |
2992 | u8 mode = BPF_MODE(code); | |
2993 | ||
2994 | /* LD_IMM64 */ | |
2995 | if (mode == BPF_IMM) | |
2996 | return true; | |
2997 | ||
2998 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
2999 | if (t != SRC_OP) | |
3000 | return false; | |
3001 | ||
3002 | /* Implicit ctx ptr. */ | |
3003 | if (regno == BPF_REG_6) | |
3004 | return true; | |
3005 | ||
3006 | /* Explicit source could be any width. */ | |
3007 | return true; | |
3008 | } | |
3009 | ||
3010 | if (class == BPF_ST) | |
3011 | /* The only source register for BPF_ST is a ptr. */ | |
3012 | return true; | |
3013 | ||
3014 | /* Conservatively return true at default. */ | |
3015 | return true; | |
3016 | } | |
3017 | ||
83a28819 IL |
3018 | /* Return the regno defined by the insn, or -1. */ |
3019 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 3020 | { |
83a28819 IL |
3021 | switch (BPF_CLASS(insn->code)) { |
3022 | case BPF_JMP: | |
3023 | case BPF_JMP32: | |
3024 | case BPF_ST: | |
3025 | return -1; | |
3026 | case BPF_STX: | |
3027 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
3028 | (insn->imm & BPF_FETCH)) { | |
3029 | if (insn->imm == BPF_CMPXCHG) | |
3030 | return BPF_REG_0; | |
3031 | else | |
3032 | return insn->src_reg; | |
3033 | } else { | |
3034 | return -1; | |
3035 | } | |
3036 | default: | |
3037 | return insn->dst_reg; | |
3038 | } | |
b325fbca JW |
3039 | } |
3040 | ||
3041 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
3042 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
3043 | { | |
83a28819 IL |
3044 | int dst_reg = insn_def_regno(insn); |
3045 | ||
3046 | if (dst_reg == -1) | |
b325fbca JW |
3047 | return false; |
3048 | ||
83a28819 | 3049 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
3050 | } |
3051 | ||
5327ed3d JW |
3052 | static void mark_insn_zext(struct bpf_verifier_env *env, |
3053 | struct bpf_reg_state *reg) | |
3054 | { | |
3055 | s32 def_idx = reg->subreg_def; | |
3056 | ||
3057 | if (def_idx == DEF_NOT_SUBREG) | |
3058 | return; | |
3059 | ||
3060 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
3061 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
3062 | reg->subreg_def = DEF_NOT_SUBREG; | |
3063 | } | |
3064 | ||
dc503a8a | 3065 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
3066 | enum reg_arg_type t) |
3067 | { | |
f4d7e40a AS |
3068 | struct bpf_verifier_state *vstate = env->cur_state; |
3069 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 3070 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 3071 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 3072 | bool rw64; |
dc503a8a | 3073 | |
17a52670 | 3074 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 3075 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
3076 | return -EINVAL; |
3077 | } | |
3078 | ||
0f55f9ed CL |
3079 | mark_reg_scratched(env, regno); |
3080 | ||
c342dc10 | 3081 | reg = ®s[regno]; |
5327ed3d | 3082 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
3083 | if (t == SRC_OP) { |
3084 | /* check whether register used as source operand can be read */ | |
c342dc10 | 3085 | if (reg->type == NOT_INIT) { |
61bd5218 | 3086 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
3087 | return -EACCES; |
3088 | } | |
679c782d | 3089 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
3090 | if (regno == BPF_REG_FP) |
3091 | return 0; | |
3092 | ||
5327ed3d JW |
3093 | if (rw64) |
3094 | mark_insn_zext(env, reg); | |
3095 | ||
3096 | return mark_reg_read(env, reg, reg->parent, | |
3097 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
3098 | } else { |
3099 | /* check whether register used as dest operand can be written to */ | |
3100 | if (regno == BPF_REG_FP) { | |
61bd5218 | 3101 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
3102 | return -EACCES; |
3103 | } | |
c342dc10 | 3104 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 3105 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 3106 | if (t == DST_OP) |
61bd5218 | 3107 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
3108 | } |
3109 | return 0; | |
3110 | } | |
3111 | ||
bffdeaa8 AN |
3112 | static void mark_jmp_point(struct bpf_verifier_env *env, int idx) |
3113 | { | |
3114 | env->insn_aux_data[idx].jmp_point = true; | |
3115 | } | |
3116 | ||
3117 | static bool is_jmp_point(struct bpf_verifier_env *env, int insn_idx) | |
3118 | { | |
3119 | return env->insn_aux_data[insn_idx].jmp_point; | |
3120 | } | |
3121 | ||
b5dc0163 AS |
3122 | /* for any branch, call, exit record the history of jmps in the given state */ |
3123 | static int push_jmp_history(struct bpf_verifier_env *env, | |
3124 | struct bpf_verifier_state *cur) | |
3125 | { | |
3126 | u32 cnt = cur->jmp_history_cnt; | |
3127 | struct bpf_idx_pair *p; | |
ceb35b66 | 3128 | size_t alloc_size; |
b5dc0163 | 3129 | |
bffdeaa8 AN |
3130 | if (!is_jmp_point(env, env->insn_idx)) |
3131 | return 0; | |
3132 | ||
b5dc0163 | 3133 | cnt++; |
ceb35b66 KC |
3134 | alloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p))); |
3135 | p = krealloc(cur->jmp_history, alloc_size, GFP_USER); | |
b5dc0163 AS |
3136 | if (!p) |
3137 | return -ENOMEM; | |
3138 | p[cnt - 1].idx = env->insn_idx; | |
3139 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
3140 | cur->jmp_history = p; | |
3141 | cur->jmp_history_cnt = cnt; | |
3142 | return 0; | |
3143 | } | |
3144 | ||
3145 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
3146 | * history then previous instruction came from straight line execution. | |
3147 | */ | |
3148 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
3149 | u32 *history) | |
3150 | { | |
3151 | u32 cnt = *history; | |
3152 | ||
3153 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
3154 | i = st->jmp_history[cnt - 1].prev_idx; | |
3155 | (*history)--; | |
3156 | } else { | |
3157 | i--; | |
3158 | } | |
3159 | return i; | |
3160 | } | |
3161 | ||
e6ac2450 MKL |
3162 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
3163 | { | |
3164 | const struct btf_type *func; | |
2357672c | 3165 | struct btf *desc_btf; |
e6ac2450 MKL |
3166 | |
3167 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
3168 | return NULL; | |
3169 | ||
43bf0878 | 3170 | desc_btf = find_kfunc_desc_btf(data, insn->off); |
2357672c KKD |
3171 | if (IS_ERR(desc_btf)) |
3172 | return "<error>"; | |
3173 | ||
3174 | func = btf_type_by_id(desc_btf, insn->imm); | |
3175 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
3176 | } |
3177 | ||
b5dc0163 AS |
3178 | /* For given verifier state backtrack_insn() is called from the last insn to |
3179 | * the first insn. Its purpose is to compute a bitmask of registers and | |
3180 | * stack slots that needs precision in the parent verifier state. | |
3181 | */ | |
3182 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
3183 | u32 *reg_mask, u64 *stack_mask) | |
3184 | { | |
3185 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 3186 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
3187 | .cb_print = verbose, |
3188 | .private_data = env, | |
3189 | }; | |
3190 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
3191 | u8 class = BPF_CLASS(insn->code); | |
3192 | u8 opcode = BPF_OP(insn->code); | |
3193 | u8 mode = BPF_MODE(insn->code); | |
3194 | u32 dreg = 1u << insn->dst_reg; | |
3195 | u32 sreg = 1u << insn->src_reg; | |
3196 | u32 spi; | |
3197 | ||
3198 | if (insn->code == 0) | |
3199 | return 0; | |
496f3324 | 3200 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
3201 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
3202 | verbose(env, "%d: ", idx); | |
3203 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
3204 | } | |
3205 | ||
3206 | if (class == BPF_ALU || class == BPF_ALU64) { | |
3207 | if (!(*reg_mask & dreg)) | |
3208 | return 0; | |
3209 | if (opcode == BPF_MOV) { | |
3210 | if (BPF_SRC(insn->code) == BPF_X) { | |
3211 | /* dreg = sreg | |
3212 | * dreg needs precision after this insn | |
3213 | * sreg needs precision before this insn | |
3214 | */ | |
3215 | *reg_mask &= ~dreg; | |
3216 | *reg_mask |= sreg; | |
3217 | } else { | |
3218 | /* dreg = K | |
3219 | * dreg needs precision after this insn. | |
3220 | * Corresponding register is already marked | |
3221 | * as precise=true in this verifier state. | |
3222 | * No further markings in parent are necessary | |
3223 | */ | |
3224 | *reg_mask &= ~dreg; | |
3225 | } | |
3226 | } else { | |
3227 | if (BPF_SRC(insn->code) == BPF_X) { | |
3228 | /* dreg += sreg | |
3229 | * both dreg and sreg need precision | |
3230 | * before this insn | |
3231 | */ | |
3232 | *reg_mask |= sreg; | |
3233 | } /* else dreg += K | |
3234 | * dreg still needs precision before this insn | |
3235 | */ | |
3236 | } | |
3237 | } else if (class == BPF_LDX) { | |
3238 | if (!(*reg_mask & dreg)) | |
3239 | return 0; | |
3240 | *reg_mask &= ~dreg; | |
3241 | ||
3242 | /* scalars can only be spilled into stack w/o losing precision. | |
3243 | * Load from any other memory can be zero extended. | |
3244 | * The desire to keep that precision is already indicated | |
3245 | * by 'precise' mark in corresponding register of this state. | |
3246 | * No further tracking necessary. | |
3247 | */ | |
3248 | if (insn->src_reg != BPF_REG_FP) | |
3249 | return 0; | |
b5dc0163 AS |
3250 | |
3251 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
3252 | * that [fp - off] slot contains scalar that needs to be | |
3253 | * tracked with precision | |
3254 | */ | |
3255 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
3256 | if (spi >= 64) { | |
3257 | verbose(env, "BUG spi %d\n", spi); | |
3258 | WARN_ONCE(1, "verifier backtracking bug"); | |
3259 | return -EFAULT; | |
3260 | } | |
3261 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 3262 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 3263 | if (*reg_mask & dreg) |
b3b50f05 | 3264 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
3265 | * to access memory. It means backtracking |
3266 | * encountered a case of pointer subtraction. | |
3267 | */ | |
3268 | return -ENOTSUPP; | |
3269 | /* scalars can only be spilled into stack */ | |
3270 | if (insn->dst_reg != BPF_REG_FP) | |
3271 | return 0; | |
b5dc0163 AS |
3272 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
3273 | if (spi >= 64) { | |
3274 | verbose(env, "BUG spi %d\n", spi); | |
3275 | WARN_ONCE(1, "verifier backtracking bug"); | |
3276 | return -EFAULT; | |
3277 | } | |
3278 | if (!(*stack_mask & (1ull << spi))) | |
3279 | return 0; | |
3280 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
3281 | if (class == BPF_STX) |
3282 | *reg_mask |= sreg; | |
b5dc0163 AS |
3283 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
3284 | if (opcode == BPF_CALL) { | |
3285 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
3286 | return -ENOTSUPP; | |
be2ef816 AN |
3287 | /* BPF helpers that invoke callback subprogs are |
3288 | * equivalent to BPF_PSEUDO_CALL above | |
3289 | */ | |
3290 | if (insn->src_reg == 0 && is_callback_calling_function(insn->imm)) | |
3291 | return -ENOTSUPP; | |
d3178e8a HS |
3292 | /* kfunc with imm==0 is invalid and fixup_kfunc_call will |
3293 | * catch this error later. Make backtracking conservative | |
3294 | * with ENOTSUPP. | |
3295 | */ | |
3296 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && insn->imm == 0) | |
3297 | return -ENOTSUPP; | |
b5dc0163 AS |
3298 | /* regular helper call sets R0 */ |
3299 | *reg_mask &= ~1; | |
3300 | if (*reg_mask & 0x3f) { | |
3301 | /* if backtracing was looking for registers R1-R5 | |
3302 | * they should have been found already. | |
3303 | */ | |
3304 | verbose(env, "BUG regs %x\n", *reg_mask); | |
3305 | WARN_ONCE(1, "verifier backtracking bug"); | |
3306 | return -EFAULT; | |
3307 | } | |
3308 | } else if (opcode == BPF_EXIT) { | |
3309 | return -ENOTSUPP; | |
3310 | } | |
3311 | } else if (class == BPF_LD) { | |
3312 | if (!(*reg_mask & dreg)) | |
3313 | return 0; | |
3314 | *reg_mask &= ~dreg; | |
3315 | /* It's ld_imm64 or ld_abs or ld_ind. | |
3316 | * For ld_imm64 no further tracking of precision | |
3317 | * into parent is necessary | |
3318 | */ | |
3319 | if (mode == BPF_IND || mode == BPF_ABS) | |
3320 | /* to be analyzed */ | |
3321 | return -ENOTSUPP; | |
b5dc0163 AS |
3322 | } |
3323 | return 0; | |
3324 | } | |
3325 | ||
3326 | /* the scalar precision tracking algorithm: | |
3327 | * . at the start all registers have precise=false. | |
3328 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
3329 | * . once precise value of the scalar register is used in: | |
3330 | * . ptr + scalar alu | |
3331 | * . if (scalar cond K|scalar) | |
3332 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
3333 | * backtrack through the verifier states and mark all registers and | |
3334 | * stack slots with spilled constants that these scalar regisers | |
3335 | * should be precise. | |
3336 | * . during state pruning two registers (or spilled stack slots) | |
3337 | * are equivalent if both are not precise. | |
3338 | * | |
3339 | * Note the verifier cannot simply walk register parentage chain, | |
3340 | * since many different registers and stack slots could have been | |
3341 | * used to compute single precise scalar. | |
3342 | * | |
3343 | * The approach of starting with precise=true for all registers and then | |
3344 | * backtrack to mark a register as not precise when the verifier detects | |
3345 | * that program doesn't care about specific value (e.g., when helper | |
3346 | * takes register as ARG_ANYTHING parameter) is not safe. | |
3347 | * | |
3348 | * It's ok to walk single parentage chain of the verifier states. | |
3349 | * It's possible that this backtracking will go all the way till 1st insn. | |
3350 | * All other branches will be explored for needing precision later. | |
3351 | * | |
3352 | * The backtracking needs to deal with cases like: | |
3353 | * 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) | |
3354 | * r9 -= r8 | |
3355 | * r5 = r9 | |
3356 | * if r5 > 0x79f goto pc+7 | |
3357 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
3358 | * r5 += 1 | |
3359 | * ... | |
3360 | * call bpf_perf_event_output#25 | |
3361 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
3362 | * | |
3363 | * and this case: | |
3364 | * r6 = 1 | |
3365 | * call foo // uses callee's r6 inside to compute r0 | |
3366 | * r0 += r6 | |
3367 | * if r0 == 0 goto | |
3368 | * | |
3369 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
3370 | * | |
3371 | * Also if parent's curframe > frame where backtracking started, | |
3372 | * the verifier need to mark registers in both frames, otherwise callees | |
3373 | * may incorrectly prune callers. This is similar to | |
3374 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
3375 | * | |
3376 | * For now backtracking falls back into conservative marking. | |
3377 | */ | |
3378 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
3379 | struct bpf_verifier_state *st) | |
3380 | { | |
3381 | struct bpf_func_state *func; | |
3382 | struct bpf_reg_state *reg; | |
3383 | int i, j; | |
3384 | ||
3385 | /* big hammer: mark all scalars precise in this path. | |
3386 | * pop_stack may still get !precise scalars. | |
f63181b6 AN |
3387 | * We also skip current state and go straight to first parent state, |
3388 | * because precision markings in current non-checkpointed state are | |
3389 | * not needed. See why in the comment in __mark_chain_precision below. | |
b5dc0163 | 3390 | */ |
f63181b6 | 3391 | for (st = st->parent; st; st = st->parent) { |
b5dc0163 AS |
3392 | for (i = 0; i <= st->curframe; i++) { |
3393 | func = st->frame[i]; | |
3394 | for (j = 0; j < BPF_REG_FP; j++) { | |
3395 | reg = &func->regs[j]; | |
3396 | if (reg->type != SCALAR_VALUE) | |
3397 | continue; | |
3398 | reg->precise = true; | |
3399 | } | |
3400 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 3401 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
3402 | continue; |
3403 | reg = &func->stack[j].spilled_ptr; | |
3404 | if (reg->type != SCALAR_VALUE) | |
3405 | continue; | |
3406 | reg->precise = true; | |
3407 | } | |
3408 | } | |
f63181b6 | 3409 | } |
b5dc0163 AS |
3410 | } |
3411 | ||
7a830b53 AN |
3412 | static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
3413 | { | |
3414 | struct bpf_func_state *func; | |
3415 | struct bpf_reg_state *reg; | |
3416 | int i, j; | |
3417 | ||
3418 | for (i = 0; i <= st->curframe; i++) { | |
3419 | func = st->frame[i]; | |
3420 | for (j = 0; j < BPF_REG_FP; j++) { | |
3421 | reg = &func->regs[j]; | |
3422 | if (reg->type != SCALAR_VALUE) | |
3423 | continue; | |
3424 | reg->precise = false; | |
3425 | } | |
3426 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
3427 | if (!is_spilled_reg(&func->stack[j])) | |
3428 | continue; | |
3429 | reg = &func->stack[j].spilled_ptr; | |
3430 | if (reg->type != SCALAR_VALUE) | |
3431 | continue; | |
3432 | reg->precise = false; | |
3433 | } | |
3434 | } | |
3435 | } | |
3436 | ||
f63181b6 AN |
3437 | /* |
3438 | * __mark_chain_precision() backtracks BPF program instruction sequence and | |
3439 | * chain of verifier states making sure that register *regno* (if regno >= 0) | |
3440 | * and/or stack slot *spi* (if spi >= 0) are marked as precisely tracked | |
3441 | * SCALARS, as well as any other registers and slots that contribute to | |
3442 | * a tracked state of given registers/stack slots, depending on specific BPF | |
3443 | * assembly instructions (see backtrack_insns() for exact instruction handling | |
3444 | * logic). This backtracking relies on recorded jmp_history and is able to | |
3445 | * traverse entire chain of parent states. This process ends only when all the | |
3446 | * necessary registers/slots and their transitive dependencies are marked as | |
3447 | * precise. | |
3448 | * | |
3449 | * One important and subtle aspect is that precise marks *do not matter* in | |
3450 | * the currently verified state (current state). It is important to understand | |
3451 | * why this is the case. | |
3452 | * | |
3453 | * First, note that current state is the state that is not yet "checkpointed", | |
3454 | * i.e., it is not yet put into env->explored_states, and it has no children | |
3455 | * states as well. It's ephemeral, and can end up either a) being discarded if | |
3456 | * compatible explored state is found at some point or BPF_EXIT instruction is | |
3457 | * reached or b) checkpointed and put into env->explored_states, branching out | |
3458 | * into one or more children states. | |
3459 | * | |
3460 | * In the former case, precise markings in current state are completely | |
3461 | * ignored by state comparison code (see regsafe() for details). Only | |
3462 | * checkpointed ("old") state precise markings are important, and if old | |
3463 | * state's register/slot is precise, regsafe() assumes current state's | |
3464 | * register/slot as precise and checks value ranges exactly and precisely. If | |
3465 | * states turn out to be compatible, current state's necessary precise | |
3466 | * markings and any required parent states' precise markings are enforced | |
3467 | * after the fact with propagate_precision() logic, after the fact. But it's | |
3468 | * important to realize that in this case, even after marking current state | |
3469 | * registers/slots as precise, we immediately discard current state. So what | |
3470 | * actually matters is any of the precise markings propagated into current | |
3471 | * state's parent states, which are always checkpointed (due to b) case above). | |
3472 | * As such, for scenario a) it doesn't matter if current state has precise | |
3473 | * markings set or not. | |
3474 | * | |
3475 | * Now, for the scenario b), checkpointing and forking into child(ren) | |
3476 | * state(s). Note that before current state gets to checkpointing step, any | |
3477 | * processed instruction always assumes precise SCALAR register/slot | |
3478 | * knowledge: if precise value or range is useful to prune jump branch, BPF | |
3479 | * verifier takes this opportunity enthusiastically. Similarly, when | |
3480 | * register's value is used to calculate offset or memory address, exact | |
3481 | * knowledge of SCALAR range is assumed, checked, and enforced. So, similar to | |
3482 | * what we mentioned above about state comparison ignoring precise markings | |
3483 | * during state comparison, BPF verifier ignores and also assumes precise | |
3484 | * markings *at will* during instruction verification process. But as verifier | |
3485 | * assumes precision, it also propagates any precision dependencies across | |
3486 | * parent states, which are not yet finalized, so can be further restricted | |
3487 | * based on new knowledge gained from restrictions enforced by their children | |
3488 | * states. This is so that once those parent states are finalized, i.e., when | |
3489 | * they have no more active children state, state comparison logic in | |
3490 | * is_state_visited() would enforce strict and precise SCALAR ranges, if | |
3491 | * required for correctness. | |
3492 | * | |
3493 | * To build a bit more intuition, note also that once a state is checkpointed, | |
3494 | * the path we took to get to that state is not important. This is crucial | |
3495 | * property for state pruning. When state is checkpointed and finalized at | |
3496 | * some instruction index, it can be correctly and safely used to "short | |
3497 | * circuit" any *compatible* state that reaches exactly the same instruction | |
3498 | * index. I.e., if we jumped to that instruction from a completely different | |
3499 | * code path than original finalized state was derived from, it doesn't | |
3500 | * matter, current state can be discarded because from that instruction | |
3501 | * forward having a compatible state will ensure we will safely reach the | |
3502 | * exit. States describe preconditions for further exploration, but completely | |
3503 | * forget the history of how we got here. | |
3504 | * | |
3505 | * This also means that even if we needed precise SCALAR range to get to | |
3506 | * finalized state, but from that point forward *that same* SCALAR register is | |
3507 | * never used in a precise context (i.e., it's precise value is not needed for | |
3508 | * correctness), it's correct and safe to mark such register as "imprecise" | |
3509 | * (i.e., precise marking set to false). This is what we rely on when we do | |
3510 | * not set precise marking in current state. If no child state requires | |
3511 | * precision for any given SCALAR register, it's safe to dictate that it can | |
3512 | * be imprecise. If any child state does require this register to be precise, | |
3513 | * we'll mark it precise later retroactively during precise markings | |
3514 | * propagation from child state to parent states. | |
7a830b53 AN |
3515 | * |
3516 | * Skipping precise marking setting in current state is a mild version of | |
3517 | * relying on the above observation. But we can utilize this property even | |
3518 | * more aggressively by proactively forgetting any precise marking in the | |
3519 | * current state (which we inherited from the parent state), right before we | |
3520 | * checkpoint it and branch off into new child state. This is done by | |
3521 | * mark_all_scalars_imprecise() to hopefully get more permissive and generic | |
3522 | * finalized states which help in short circuiting more future states. | |
f63181b6 | 3523 | */ |
529409ea | 3524 | static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int regno, |
a3ce685d | 3525 | int spi) |
b5dc0163 AS |
3526 | { |
3527 | struct bpf_verifier_state *st = env->cur_state; | |
3528 | int first_idx = st->first_insn_idx; | |
3529 | int last_idx = env->insn_idx; | |
3530 | struct bpf_func_state *func; | |
3531 | struct bpf_reg_state *reg; | |
a3ce685d AS |
3532 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
3533 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 3534 | bool skip_first = true; |
a3ce685d | 3535 | bool new_marks = false; |
b5dc0163 AS |
3536 | int i, err; |
3537 | ||
2c78ee89 | 3538 | if (!env->bpf_capable) |
b5dc0163 AS |
3539 | return 0; |
3540 | ||
f63181b6 AN |
3541 | /* Do sanity checks against current state of register and/or stack |
3542 | * slot, but don't set precise flag in current state, as precision | |
3543 | * tracking in the current state is unnecessary. | |
3544 | */ | |
529409ea | 3545 | func = st->frame[frame]; |
a3ce685d AS |
3546 | if (regno >= 0) { |
3547 | reg = &func->regs[regno]; | |
3548 | if (reg->type != SCALAR_VALUE) { | |
3549 | WARN_ONCE(1, "backtracing misuse"); | |
3550 | return -EFAULT; | |
3551 | } | |
f63181b6 | 3552 | new_marks = true; |
b5dc0163 | 3553 | } |
b5dc0163 | 3554 | |
a3ce685d | 3555 | while (spi >= 0) { |
27113c59 | 3556 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
3557 | stack_mask = 0; |
3558 | break; | |
3559 | } | |
3560 | reg = &func->stack[spi].spilled_ptr; | |
3561 | if (reg->type != SCALAR_VALUE) { | |
3562 | stack_mask = 0; | |
3563 | break; | |
3564 | } | |
f63181b6 | 3565 | new_marks = true; |
a3ce685d AS |
3566 | break; |
3567 | } | |
3568 | ||
3569 | if (!new_marks) | |
3570 | return 0; | |
3571 | if (!reg_mask && !stack_mask) | |
3572 | return 0; | |
be2ef816 | 3573 | |
b5dc0163 AS |
3574 | for (;;) { |
3575 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
3576 | u32 history = st->jmp_history_cnt; |
3577 | ||
496f3324 | 3578 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 | 3579 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
be2ef816 AN |
3580 | |
3581 | if (last_idx < 0) { | |
3582 | /* we are at the entry into subprog, which | |
3583 | * is expected for global funcs, but only if | |
3584 | * requested precise registers are R1-R5 | |
3585 | * (which are global func's input arguments) | |
3586 | */ | |
3587 | if (st->curframe == 0 && | |
3588 | st->frame[0]->subprogno > 0 && | |
3589 | st->frame[0]->callsite == BPF_MAIN_FUNC && | |
3590 | stack_mask == 0 && (reg_mask & ~0x3e) == 0) { | |
3591 | bitmap_from_u64(mask, reg_mask); | |
3592 | for_each_set_bit(i, mask, 32) { | |
3593 | reg = &st->frame[0]->regs[i]; | |
3594 | if (reg->type != SCALAR_VALUE) { | |
3595 | reg_mask &= ~(1u << i); | |
3596 | continue; | |
3597 | } | |
3598 | reg->precise = true; | |
3599 | } | |
3600 | return 0; | |
3601 | } | |
3602 | ||
3603 | verbose(env, "BUG backtracing func entry subprog %d reg_mask %x stack_mask %llx\n", | |
3604 | st->frame[0]->subprogno, reg_mask, stack_mask); | |
3605 | WARN_ONCE(1, "verifier backtracking bug"); | |
3606 | return -EFAULT; | |
3607 | } | |
3608 | ||
b5dc0163 AS |
3609 | for (i = last_idx;;) { |
3610 | if (skip_first) { | |
3611 | err = 0; | |
3612 | skip_first = false; | |
3613 | } else { | |
3614 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
3615 | } | |
3616 | if (err == -ENOTSUPP) { | |
3617 | mark_all_scalars_precise(env, st); | |
3618 | return 0; | |
3619 | } else if (err) { | |
3620 | return err; | |
3621 | } | |
3622 | if (!reg_mask && !stack_mask) | |
3623 | /* Found assignment(s) into tracked register in this state. | |
3624 | * Since this state is already marked, just return. | |
3625 | * Nothing to be tracked further in the parent state. | |
3626 | */ | |
3627 | return 0; | |
3628 | if (i == first_idx) | |
3629 | break; | |
3630 | i = get_prev_insn_idx(st, i, &history); | |
3631 | if (i >= env->prog->len) { | |
3632 | /* This can happen if backtracking reached insn 0 | |
3633 | * and there are still reg_mask or stack_mask | |
3634 | * to backtrack. | |
3635 | * It means the backtracking missed the spot where | |
3636 | * particular register was initialized with a constant. | |
3637 | */ | |
3638 | verbose(env, "BUG backtracking idx %d\n", i); | |
3639 | WARN_ONCE(1, "verifier backtracking bug"); | |
3640 | return -EFAULT; | |
3641 | } | |
3642 | } | |
3643 | st = st->parent; | |
3644 | if (!st) | |
3645 | break; | |
3646 | ||
a3ce685d | 3647 | new_marks = false; |
529409ea | 3648 | func = st->frame[frame]; |
b5dc0163 AS |
3649 | bitmap_from_u64(mask, reg_mask); |
3650 | for_each_set_bit(i, mask, 32) { | |
3651 | reg = &func->regs[i]; | |
a3ce685d AS |
3652 | if (reg->type != SCALAR_VALUE) { |
3653 | reg_mask &= ~(1u << i); | |
b5dc0163 | 3654 | continue; |
a3ce685d | 3655 | } |
b5dc0163 AS |
3656 | if (!reg->precise) |
3657 | new_marks = true; | |
3658 | reg->precise = true; | |
3659 | } | |
3660 | ||
3661 | bitmap_from_u64(mask, stack_mask); | |
3662 | for_each_set_bit(i, mask, 64) { | |
3663 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
3664 | /* the sequence of instructions: |
3665 | * 2: (bf) r3 = r10 | |
3666 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
3667 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
3668 | * doesn't contain jmps. It's backtracked | |
3669 | * as a single block. | |
3670 | * During backtracking insn 3 is not recognized as | |
3671 | * stack access, so at the end of backtracking | |
3672 | * stack slot fp-8 is still marked in stack_mask. | |
3673 | * However the parent state may not have accessed | |
3674 | * fp-8 and it's "unallocated" stack space. | |
3675 | * In such case fallback to conservative. | |
b5dc0163 | 3676 | */ |
2339cd6c AS |
3677 | mark_all_scalars_precise(env, st); |
3678 | return 0; | |
b5dc0163 AS |
3679 | } |
3680 | ||
27113c59 | 3681 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 3682 | stack_mask &= ~(1ull << i); |
b5dc0163 | 3683 | continue; |
a3ce685d | 3684 | } |
b5dc0163 | 3685 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
3686 | if (reg->type != SCALAR_VALUE) { |
3687 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 3688 | continue; |
a3ce685d | 3689 | } |
b5dc0163 AS |
3690 | if (!reg->precise) |
3691 | new_marks = true; | |
3692 | reg->precise = true; | |
3693 | } | |
496f3324 | 3694 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 3695 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
3696 | new_marks ? "didn't have" : "already had", |
3697 | reg_mask, stack_mask); | |
2e576648 | 3698 | print_verifier_state(env, func, true); |
b5dc0163 AS |
3699 | } |
3700 | ||
a3ce685d AS |
3701 | if (!reg_mask && !stack_mask) |
3702 | break; | |
b5dc0163 AS |
3703 | if (!new_marks) |
3704 | break; | |
3705 | ||
3706 | last_idx = st->last_insn_idx; | |
3707 | first_idx = st->first_insn_idx; | |
3708 | } | |
3709 | return 0; | |
3710 | } | |
3711 | ||
eb1f7f71 | 3712 | int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
a3ce685d | 3713 | { |
529409ea | 3714 | return __mark_chain_precision(env, env->cur_state->curframe, regno, -1); |
a3ce685d AS |
3715 | } |
3716 | ||
529409ea | 3717 | static int mark_chain_precision_frame(struct bpf_verifier_env *env, int frame, int regno) |
a3ce685d | 3718 | { |
529409ea | 3719 | return __mark_chain_precision(env, frame, regno, -1); |
a3ce685d AS |
3720 | } |
3721 | ||
529409ea | 3722 | static int mark_chain_precision_stack_frame(struct bpf_verifier_env *env, int frame, int spi) |
a3ce685d | 3723 | { |
529409ea | 3724 | return __mark_chain_precision(env, frame, -1, spi); |
a3ce685d | 3725 | } |
b5dc0163 | 3726 | |
1be7f75d AS |
3727 | static bool is_spillable_regtype(enum bpf_reg_type type) |
3728 | { | |
c25b2ae1 | 3729 | switch (base_type(type)) { |
1be7f75d | 3730 | case PTR_TO_MAP_VALUE: |
1be7f75d AS |
3731 | case PTR_TO_STACK: |
3732 | case PTR_TO_CTX: | |
969bf05e | 3733 | case PTR_TO_PACKET: |
de8f3a83 | 3734 | case PTR_TO_PACKET_META: |
969bf05e | 3735 | case PTR_TO_PACKET_END: |
d58e468b | 3736 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 3737 | case CONST_PTR_TO_MAP: |
c64b7983 | 3738 | case PTR_TO_SOCKET: |
46f8bc92 | 3739 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 3740 | case PTR_TO_TCP_SOCK: |
fada7fdc | 3741 | case PTR_TO_XDP_SOCK: |
65726b5b | 3742 | case PTR_TO_BTF_ID: |
20b2aff4 | 3743 | case PTR_TO_BUF: |
744ea4e3 | 3744 | case PTR_TO_MEM: |
69c087ba YS |
3745 | case PTR_TO_FUNC: |
3746 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
3747 | return true; |
3748 | default: | |
3749 | return false; | |
3750 | } | |
3751 | } | |
3752 | ||
cc2b14d5 AS |
3753 | /* Does this register contain a constant zero? */ |
3754 | static bool register_is_null(struct bpf_reg_state *reg) | |
3755 | { | |
3756 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
3757 | } | |
3758 | ||
f7cf25b2 AS |
3759 | static bool register_is_const(struct bpf_reg_state *reg) |
3760 | { | |
3761 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
3762 | } | |
3763 | ||
5689d49b YS |
3764 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
3765 | { | |
3766 | return tnum_is_unknown(reg->var_off) && | |
3767 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
3768 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
3769 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
3770 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
3771 | } | |
3772 | ||
3773 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
3774 | { | |
3775 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
3776 | } | |
3777 | ||
6e7e63cb JH |
3778 | static bool __is_pointer_value(bool allow_ptr_leaks, |
3779 | const struct bpf_reg_state *reg) | |
3780 | { | |
3781 | if (allow_ptr_leaks) | |
3782 | return false; | |
3783 | ||
3784 | return reg->type != SCALAR_VALUE; | |
3785 | } | |
3786 | ||
71f656a5 EZ |
3787 | /* Copy src state preserving dst->parent and dst->live fields */ |
3788 | static void copy_register_state(struct bpf_reg_state *dst, const struct bpf_reg_state *src) | |
3789 | { | |
3790 | struct bpf_reg_state *parent = dst->parent; | |
3791 | enum bpf_reg_liveness live = dst->live; | |
3792 | ||
3793 | *dst = *src; | |
3794 | dst->parent = parent; | |
3795 | dst->live = live; | |
3796 | } | |
3797 | ||
f7cf25b2 | 3798 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
3799 | int spi, struct bpf_reg_state *reg, |
3800 | int size) | |
f7cf25b2 AS |
3801 | { |
3802 | int i; | |
3803 | ||
71f656a5 | 3804 | copy_register_state(&state->stack[spi].spilled_ptr, reg); |
354e8f19 MKL |
3805 | if (size == BPF_REG_SIZE) |
3806 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 3807 | |
354e8f19 MKL |
3808 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
3809 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 3810 | |
354e8f19 MKL |
3811 | /* size < 8 bytes spill */ |
3812 | for (; i; i--) | |
3813 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
3814 | } |
3815 | ||
ecdf985d EZ |
3816 | static bool is_bpf_st_mem(struct bpf_insn *insn) |
3817 | { | |
3818 | return BPF_CLASS(insn->code) == BPF_ST && BPF_MODE(insn->code) == BPF_MEM; | |
3819 | } | |
3820 | ||
01f810ac | 3821 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
3822 | * stack boundary and alignment are checked in check_mem_access() |
3823 | */ | |
01f810ac AM |
3824 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
3825 | /* stack frame we're writing to */ | |
3826 | struct bpf_func_state *state, | |
3827 | int off, int size, int value_regno, | |
3828 | int insn_idx) | |
17a52670 | 3829 | { |
f4d7e40a | 3830 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 3831 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
ecdf985d | 3832 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; |
f7cf25b2 | 3833 | struct bpf_reg_state *reg = NULL; |
ecdf985d | 3834 | u32 dst_reg = insn->dst_reg; |
638f5b90 | 3835 | |
c69431aa | 3836 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
3837 | if (err) |
3838 | return err; | |
9c399760 AS |
3839 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
3840 | * so it's aligned access and [off, off + size) are within stack limits | |
3841 | */ | |
638f5b90 AS |
3842 | if (!env->allow_ptr_leaks && |
3843 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
3844 | size != BPF_REG_SIZE) { | |
3845 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
3846 | return -EACCES; | |
3847 | } | |
17a52670 | 3848 | |
f4d7e40a | 3849 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
3850 | if (value_regno >= 0) |
3851 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
3852 | if (!env->bypass_spec_v4) { |
3853 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
3854 | ||
3855 | for (i = 0; i < size; i++) { | |
e4f4db47 LG |
3856 | u8 type = state->stack[spi].slot_type[i]; |
3857 | ||
3858 | if (type != STACK_MISC && type != STACK_ZERO) { | |
2039f26f DB |
3859 | sanitize = true; |
3860 | break; | |
3861 | } | |
3862 | } | |
3863 | ||
3864 | if (sanitize) | |
3865 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
3866 | } | |
17a52670 | 3867 | |
ef8fc7a0 KKD |
3868 | err = destroy_if_dynptr_stack_slot(env, state, spi); |
3869 | if (err) | |
3870 | return err; | |
3871 | ||
0f55f9ed | 3872 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 3873 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 3874 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
3875 | if (dst_reg != BPF_REG_FP) { |
3876 | /* The backtracking logic can only recognize explicit | |
3877 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 3878 | * scalar via different register has to be conservative. |
b5dc0163 AS |
3879 | * Backtrack from here and mark all registers as precise |
3880 | * that contributed into 'reg' being a constant. | |
3881 | */ | |
3882 | err = mark_chain_precision(env, value_regno); | |
3883 | if (err) | |
3884 | return err; | |
3885 | } | |
354e8f19 | 3886 | save_register_state(state, spi, reg, size); |
ecdf985d EZ |
3887 | } else if (!reg && !(off % BPF_REG_SIZE) && is_bpf_st_mem(insn) && |
3888 | insn->imm != 0 && env->bpf_capable) { | |
3889 | struct bpf_reg_state fake_reg = {}; | |
3890 | ||
3891 | __mark_reg_known(&fake_reg, (u32)insn->imm); | |
3892 | fake_reg.type = SCALAR_VALUE; | |
3893 | save_register_state(state, spi, &fake_reg, size); | |
f7cf25b2 | 3894 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 3895 | /* register containing pointer is being spilled into stack */ |
9c399760 | 3896 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 3897 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 3898 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
3899 | return -EACCES; |
3900 | } | |
f7cf25b2 | 3901 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
3902 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
3903 | return -EINVAL; | |
3904 | } | |
354e8f19 | 3905 | save_register_state(state, spi, reg, size); |
9c399760 | 3906 | } else { |
cc2b14d5 AS |
3907 | u8 type = STACK_MISC; |
3908 | ||
679c782d EC |
3909 | /* regular write of data into stack destroys any spilled ptr */ |
3910 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
06accc87 AN |
3911 | /* Mark slots as STACK_MISC if they belonged to spilled ptr/dynptr/iter. */ |
3912 | if (is_stack_slot_special(&state->stack[spi])) | |
0bae2d4d | 3913 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 3914 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 3915 | |
cc2b14d5 AS |
3916 | /* only mark the slot as written if all 8 bytes were written |
3917 | * otherwise read propagation may incorrectly stop too soon | |
3918 | * when stack slots are partially written. | |
3919 | * This heuristic means that read propagation will be | |
3920 | * conservative, since it will add reg_live_read marks | |
3921 | * to stack slots all the way to first state when programs | |
3922 | * writes+reads less than 8 bytes | |
3923 | */ | |
3924 | if (size == BPF_REG_SIZE) | |
3925 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
3926 | ||
3927 | /* when we zero initialize stack slots mark them as such */ | |
ecdf985d EZ |
3928 | if ((reg && register_is_null(reg)) || |
3929 | (!reg && is_bpf_st_mem(insn) && insn->imm == 0)) { | |
b5dc0163 AS |
3930 | /* backtracking doesn't work for STACK_ZERO yet. */ |
3931 | err = mark_chain_precision(env, value_regno); | |
3932 | if (err) | |
3933 | return err; | |
cc2b14d5 | 3934 | type = STACK_ZERO; |
b5dc0163 | 3935 | } |
cc2b14d5 | 3936 | |
0bae2d4d | 3937 | /* Mark slots affected by this stack write. */ |
9c399760 | 3938 | for (i = 0; i < size; i++) |
638f5b90 | 3939 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 3940 | type; |
17a52670 AS |
3941 | } |
3942 | return 0; | |
3943 | } | |
3944 | ||
01f810ac AM |
3945 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
3946 | * known to contain a variable offset. | |
3947 | * This function checks whether the write is permitted and conservatively | |
3948 | * tracks the effects of the write, considering that each stack slot in the | |
3949 | * dynamic range is potentially written to. | |
3950 | * | |
3951 | * 'off' includes 'regno->off'. | |
3952 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
3953 | * the stack. | |
3954 | * | |
3955 | * Spilled pointers in range are not marked as written because we don't know | |
3956 | * what's going to be actually written. This means that read propagation for | |
3957 | * future reads cannot be terminated by this write. | |
3958 | * | |
3959 | * For privileged programs, uninitialized stack slots are considered | |
3960 | * initialized by this write (even though we don't know exactly what offsets | |
3961 | * are going to be written to). The idea is that we don't want the verifier to | |
3962 | * reject future reads that access slots written to through variable offsets. | |
3963 | */ | |
3964 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
3965 | /* func where register points to */ | |
3966 | struct bpf_func_state *state, | |
3967 | int ptr_regno, int off, int size, | |
3968 | int value_regno, int insn_idx) | |
3969 | { | |
3970 | struct bpf_func_state *cur; /* state of the current function */ | |
3971 | int min_off, max_off; | |
3972 | int i, err; | |
3973 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
31ff2135 | 3974 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; |
01f810ac AM |
3975 | bool writing_zero = false; |
3976 | /* set if the fact that we're writing a zero is used to let any | |
3977 | * stack slots remain STACK_ZERO | |
3978 | */ | |
3979 | bool zero_used = false; | |
3980 | ||
3981 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
3982 | ptr_reg = &cur->regs[ptr_regno]; | |
3983 | min_off = ptr_reg->smin_value + off; | |
3984 | max_off = ptr_reg->smax_value + off + size; | |
3985 | if (value_regno >= 0) | |
3986 | value_reg = &cur->regs[value_regno]; | |
31ff2135 EZ |
3987 | if ((value_reg && register_is_null(value_reg)) || |
3988 | (!value_reg && is_bpf_st_mem(insn) && insn->imm == 0)) | |
01f810ac AM |
3989 | writing_zero = true; |
3990 | ||
c69431aa | 3991 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
3992 | if (err) |
3993 | return err; | |
3994 | ||
ef8fc7a0 KKD |
3995 | for (i = min_off; i < max_off; i++) { |
3996 | int spi; | |
3997 | ||
3998 | spi = __get_spi(i); | |
3999 | err = destroy_if_dynptr_stack_slot(env, state, spi); | |
4000 | if (err) | |
4001 | return err; | |
4002 | } | |
01f810ac AM |
4003 | |
4004 | /* Variable offset writes destroy any spilled pointers in range. */ | |
4005 | for (i = min_off; i < max_off; i++) { | |
4006 | u8 new_type, *stype; | |
4007 | int slot, spi; | |
4008 | ||
4009 | slot = -i - 1; | |
4010 | spi = slot / BPF_REG_SIZE; | |
4011 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 4012 | mark_stack_slot_scratched(env, spi); |
01f810ac | 4013 | |
f5e477a8 KKD |
4014 | if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) { |
4015 | /* Reject the write if range we may write to has not | |
4016 | * been initialized beforehand. If we didn't reject | |
4017 | * here, the ptr status would be erased below (even | |
4018 | * though not all slots are actually overwritten), | |
4019 | * possibly opening the door to leaks. | |
4020 | * | |
4021 | * We do however catch STACK_INVALID case below, and | |
4022 | * only allow reading possibly uninitialized memory | |
4023 | * later for CAP_PERFMON, as the write may not happen to | |
4024 | * that slot. | |
01f810ac AM |
4025 | */ |
4026 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
4027 | insn_idx, i); | |
4028 | return -EINVAL; | |
4029 | } | |
4030 | ||
4031 | /* Erase all spilled pointers. */ | |
4032 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
4033 | ||
4034 | /* Update the slot type. */ | |
4035 | new_type = STACK_MISC; | |
4036 | if (writing_zero && *stype == STACK_ZERO) { | |
4037 | new_type = STACK_ZERO; | |
4038 | zero_used = true; | |
4039 | } | |
4040 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
4041 | * pretend that it will be initialized by this write. The slot | |
4042 | * might not actually be written to, and so if we mark it as | |
4043 | * initialized future reads might leak uninitialized memory. | |
4044 | * For privileged programs, we will accept such reads to slots | |
4045 | * that may or may not be written because, if we're reject | |
4046 | * them, the error would be too confusing. | |
4047 | */ | |
4048 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
4049 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
4050 | insn_idx, i); | |
4051 | return -EINVAL; | |
4052 | } | |
4053 | *stype = new_type; | |
4054 | } | |
4055 | if (zero_used) { | |
4056 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
4057 | err = mark_chain_precision(env, value_regno); | |
4058 | if (err) | |
4059 | return err; | |
4060 | } | |
4061 | return 0; | |
4062 | } | |
4063 | ||
4064 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
4065 | * max_off), we set the register's type according to the types of the | |
4066 | * respective stack slots. If all the stack values are known to be zeros, then | |
4067 | * so is the destination reg. Otherwise, the register is considered to be | |
4068 | * SCALAR. This function does not deal with register filling; the caller must | |
4069 | * ensure that all spilled registers in the stack range have been marked as | |
4070 | * read. | |
4071 | */ | |
4072 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
4073 | /* func where src register points to */ | |
4074 | struct bpf_func_state *ptr_state, | |
4075 | int min_off, int max_off, int dst_regno) | |
4076 | { | |
4077 | struct bpf_verifier_state *vstate = env->cur_state; | |
4078 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4079 | int i, slot, spi; | |
4080 | u8 *stype; | |
4081 | int zeros = 0; | |
4082 | ||
4083 | for (i = min_off; i < max_off; i++) { | |
4084 | slot = -i - 1; | |
4085 | spi = slot / BPF_REG_SIZE; | |
4086 | stype = ptr_state->stack[spi].slot_type; | |
4087 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
4088 | break; | |
4089 | zeros++; | |
4090 | } | |
4091 | if (zeros == max_off - min_off) { | |
4092 | /* any access_size read into register is zero extended, | |
4093 | * so the whole register == const_zero | |
4094 | */ | |
4095 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
4096 | /* backtracking doesn't support STACK_ZERO yet, | |
4097 | * so mark it precise here, so that later | |
4098 | * backtracking can stop here. | |
4099 | * Backtracking may not need this if this register | |
4100 | * doesn't participate in pointer adjustment. | |
4101 | * Forward propagation of precise flag is not | |
4102 | * necessary either. This mark is only to stop | |
4103 | * backtracking. Any register that contributed | |
4104 | * to const 0 was marked precise before spill. | |
4105 | */ | |
4106 | state->regs[dst_regno].precise = true; | |
4107 | } else { | |
4108 | /* have read misc data from the stack */ | |
4109 | mark_reg_unknown(env, state->regs, dst_regno); | |
4110 | } | |
4111 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
4112 | } | |
4113 | ||
4114 | /* Read the stack at 'off' and put the results into the register indicated by | |
4115 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
4116 | * spilled reg. | |
4117 | * | |
4118 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
4119 | * register. | |
4120 | * | |
4121 | * The access is assumed to be within the current stack bounds. | |
4122 | */ | |
4123 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
4124 | /* func where src register points to */ | |
4125 | struct bpf_func_state *reg_state, | |
4126 | int off, int size, int dst_regno) | |
17a52670 | 4127 | { |
f4d7e40a AS |
4128 | struct bpf_verifier_state *vstate = env->cur_state; |
4129 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 4130 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 4131 | struct bpf_reg_state *reg; |
354e8f19 | 4132 | u8 *stype, type; |
17a52670 | 4133 | |
f4d7e40a | 4134 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 4135 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 4136 | |
27113c59 | 4137 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
4138 | u8 spill_size = 1; |
4139 | ||
4140 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
4141 | spill_size++; | |
354e8f19 | 4142 | |
f30d4968 | 4143 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
4144 | if (reg->type != SCALAR_VALUE) { |
4145 | verbose_linfo(env, env->insn_idx, "; "); | |
4146 | verbose(env, "invalid size of register fill\n"); | |
4147 | return -EACCES; | |
4148 | } | |
354e8f19 MKL |
4149 | |
4150 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
4151 | if (dst_regno < 0) | |
4152 | return 0; | |
4153 | ||
f30d4968 | 4154 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
4155 | /* The earlier check_reg_arg() has decided the |
4156 | * subreg_def for this insn. Save it first. | |
4157 | */ | |
4158 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
4159 | ||
71f656a5 | 4160 | copy_register_state(&state->regs[dst_regno], reg); |
354e8f19 MKL |
4161 | state->regs[dst_regno].subreg_def = subreg_def; |
4162 | } else { | |
4163 | for (i = 0; i < size; i++) { | |
4164 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
4165 | if (type == STACK_SPILL) | |
4166 | continue; | |
4167 | if (type == STACK_MISC) | |
4168 | continue; | |
6715df8d EZ |
4169 | if (type == STACK_INVALID && env->allow_uninit_stack) |
4170 | continue; | |
354e8f19 MKL |
4171 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
4172 | off, i, size); | |
4173 | return -EACCES; | |
4174 | } | |
01f810ac | 4175 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 4176 | } |
354e8f19 | 4177 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 4178 | return 0; |
17a52670 | 4179 | } |
17a52670 | 4180 | |
01f810ac | 4181 | if (dst_regno >= 0) { |
17a52670 | 4182 | /* restore register state from stack */ |
71f656a5 | 4183 | copy_register_state(&state->regs[dst_regno], reg); |
2f18f62e AS |
4184 | /* mark reg as written since spilled pointer state likely |
4185 | * has its liveness marks cleared by is_state_visited() | |
4186 | * which resets stack/reg liveness for state transitions | |
4187 | */ | |
01f810ac | 4188 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 4189 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 4190 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
4191 | * it is acceptable to use this value as a SCALAR_VALUE |
4192 | * (e.g. for XADD). | |
4193 | * We must not allow unprivileged callers to do that | |
4194 | * with spilled pointers. | |
4195 | */ | |
4196 | verbose(env, "leaking pointer from stack off %d\n", | |
4197 | off); | |
4198 | return -EACCES; | |
dc503a8a | 4199 | } |
f7cf25b2 | 4200 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
4201 | } else { |
4202 | for (i = 0; i < size; i++) { | |
01f810ac AM |
4203 | type = stype[(slot - i) % BPF_REG_SIZE]; |
4204 | if (type == STACK_MISC) | |
cc2b14d5 | 4205 | continue; |
01f810ac | 4206 | if (type == STACK_ZERO) |
cc2b14d5 | 4207 | continue; |
6715df8d EZ |
4208 | if (type == STACK_INVALID && env->allow_uninit_stack) |
4209 | continue; | |
cc2b14d5 AS |
4210 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
4211 | off, i, size); | |
4212 | return -EACCES; | |
4213 | } | |
f7cf25b2 | 4214 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
4215 | if (dst_regno >= 0) |
4216 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 4217 | } |
f7cf25b2 | 4218 | return 0; |
17a52670 AS |
4219 | } |
4220 | ||
61df10c7 | 4221 | enum bpf_access_src { |
01f810ac AM |
4222 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
4223 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
4224 | }; | |
4225 | ||
4226 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
4227 | int regno, int off, int access_size, | |
4228 | bool zero_size_allowed, | |
61df10c7 | 4229 | enum bpf_access_src type, |
01f810ac AM |
4230 | struct bpf_call_arg_meta *meta); |
4231 | ||
4232 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
4233 | { | |
4234 | return cur_regs(env) + regno; | |
4235 | } | |
4236 | ||
4237 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
4238 | * 'dst_regno'. | |
4239 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
4240 | * but not its variable offset. | |
4241 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
4242 | * | |
4243 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
4244 | * filling registers (i.e. reads of spilled register cannot be detected when | |
4245 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
4246 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
4247 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
4248 | * instead. | |
4249 | */ | |
4250 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
4251 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 4252 | { |
01f810ac AM |
4253 | /* The state of the source register. */ |
4254 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4255 | struct bpf_func_state *ptr_state = func(env, reg); | |
4256 | int err; | |
4257 | int min_off, max_off; | |
4258 | ||
4259 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 4260 | */ |
01f810ac AM |
4261 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
4262 | false, ACCESS_DIRECT, NULL); | |
4263 | if (err) | |
4264 | return err; | |
4265 | ||
4266 | min_off = reg->smin_value + off; | |
4267 | max_off = reg->smax_value + off; | |
4268 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
4269 | return 0; | |
4270 | } | |
4271 | ||
4272 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
4273 | * check_stack_read_var_off. | |
4274 | * | |
4275 | * The caller must ensure that the offset falls within the allocated stack | |
4276 | * bounds. | |
4277 | * | |
4278 | * 'dst_regno' is a register which will receive the value from the stack. It | |
4279 | * can be -1, meaning that the read value is not going to a register. | |
4280 | */ | |
4281 | static int check_stack_read(struct bpf_verifier_env *env, | |
4282 | int ptr_regno, int off, int size, | |
4283 | int dst_regno) | |
4284 | { | |
4285 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4286 | struct bpf_func_state *state = func(env, reg); | |
4287 | int err; | |
4288 | /* Some accesses are only permitted with a static offset. */ | |
4289 | bool var_off = !tnum_is_const(reg->var_off); | |
4290 | ||
4291 | /* The offset is required to be static when reads don't go to a | |
4292 | * register, in order to not leak pointers (see | |
4293 | * check_stack_read_fixed_off). | |
4294 | */ | |
4295 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
4296 | char tn_buf[48]; |
4297 | ||
4298 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 4299 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
4300 | tn_buf, off, size); |
4301 | return -EACCES; | |
4302 | } | |
01f810ac AM |
4303 | /* Variable offset is prohibited for unprivileged mode for simplicity |
4304 | * since it requires corresponding support in Spectre masking for stack | |
4305 | * ALU. See also retrieve_ptr_limit(). | |
4306 | */ | |
4307 | if (!env->bypass_spec_v1 && var_off) { | |
4308 | char tn_buf[48]; | |
e4298d25 | 4309 | |
01f810ac AM |
4310 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
4311 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", | |
4312 | ptr_regno, tn_buf); | |
e4298d25 DB |
4313 | return -EACCES; |
4314 | } | |
4315 | ||
01f810ac AM |
4316 | if (!var_off) { |
4317 | off += reg->var_off.value; | |
4318 | err = check_stack_read_fixed_off(env, state, off, size, | |
4319 | dst_regno); | |
4320 | } else { | |
4321 | /* Variable offset stack reads need more conservative handling | |
4322 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
4323 | * branch. | |
4324 | */ | |
4325 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
4326 | dst_regno); | |
4327 | } | |
4328 | return err; | |
4329 | } | |
4330 | ||
4331 | ||
4332 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
4333 | * check_stack_write_var_off. | |
4334 | * | |
4335 | * 'ptr_regno' is the register used as a pointer into the stack. | |
4336 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
4337 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
4338 | * be -1, meaning that we're not writing from a register. | |
4339 | * | |
4340 | * The caller must ensure that the offset falls within the maximum stack size. | |
4341 | */ | |
4342 | static int check_stack_write(struct bpf_verifier_env *env, | |
4343 | int ptr_regno, int off, int size, | |
4344 | int value_regno, int insn_idx) | |
4345 | { | |
4346 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4347 | struct bpf_func_state *state = func(env, reg); | |
4348 | int err; | |
4349 | ||
4350 | if (tnum_is_const(reg->var_off)) { | |
4351 | off += reg->var_off.value; | |
4352 | err = check_stack_write_fixed_off(env, state, off, size, | |
4353 | value_regno, insn_idx); | |
4354 | } else { | |
4355 | /* Variable offset stack reads need more conservative handling | |
4356 | * than fixed offset ones. | |
4357 | */ | |
4358 | err = check_stack_write_var_off(env, state, | |
4359 | ptr_regno, off, size, | |
4360 | value_regno, insn_idx); | |
4361 | } | |
4362 | return err; | |
e4298d25 DB |
4363 | } |
4364 | ||
591fe988 DB |
4365 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
4366 | int off, int size, enum bpf_access_type type) | |
4367 | { | |
4368 | struct bpf_reg_state *regs = cur_regs(env); | |
4369 | struct bpf_map *map = regs[regno].map_ptr; | |
4370 | u32 cap = bpf_map_flags_to_cap(map); | |
4371 | ||
4372 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
4373 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
4374 | map->value_size, off, size); | |
4375 | return -EACCES; | |
4376 | } | |
4377 | ||
4378 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
4379 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
4380 | map->value_size, off, size); | |
4381 | return -EACCES; | |
4382 | } | |
4383 | ||
4384 | return 0; | |
4385 | } | |
4386 | ||
457f4436 AN |
4387 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
4388 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
4389 | int off, int size, u32 mem_size, | |
4390 | bool zero_size_allowed) | |
17a52670 | 4391 | { |
457f4436 AN |
4392 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
4393 | struct bpf_reg_state *reg; | |
4394 | ||
4395 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
4396 | return 0; | |
17a52670 | 4397 | |
457f4436 AN |
4398 | reg = &cur_regs(env)[regno]; |
4399 | switch (reg->type) { | |
69c087ba YS |
4400 | case PTR_TO_MAP_KEY: |
4401 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
4402 | mem_size, off, size); | |
4403 | break; | |
457f4436 | 4404 | case PTR_TO_MAP_VALUE: |
61bd5218 | 4405 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
4406 | mem_size, off, size); |
4407 | break; | |
4408 | case PTR_TO_PACKET: | |
4409 | case PTR_TO_PACKET_META: | |
4410 | case PTR_TO_PACKET_END: | |
4411 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
4412 | off, size, regno, reg->id, off, mem_size); | |
4413 | break; | |
4414 | case PTR_TO_MEM: | |
4415 | default: | |
4416 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
4417 | mem_size, off, size); | |
17a52670 | 4418 | } |
457f4436 AN |
4419 | |
4420 | return -EACCES; | |
17a52670 AS |
4421 | } |
4422 | ||
457f4436 AN |
4423 | /* check read/write into a memory region with possible variable offset */ |
4424 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
4425 | int off, int size, u32 mem_size, | |
4426 | bool zero_size_allowed) | |
dbcfe5f7 | 4427 | { |
f4d7e40a AS |
4428 | struct bpf_verifier_state *vstate = env->cur_state; |
4429 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
4430 | struct bpf_reg_state *reg = &state->regs[regno]; |
4431 | int err; | |
4432 | ||
457f4436 | 4433 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
4434 | * need to try adding each of min_value and max_value to off |
4435 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
4436 | * |
4437 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
4438 | * comparisons where we can't assume the floor of a |
4439 | * value is 0. If we are using signed variables for our | |
4440 | * index'es we need to make sure that whatever we use | |
4441 | * will have a set floor within our range. | |
4442 | */ | |
b7137c4e DB |
4443 | if (reg->smin_value < 0 && |
4444 | (reg->smin_value == S64_MIN || | |
4445 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
4446 | reg->smin_value + off < 0)) { | |
61bd5218 | 4447 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
4448 | regno); |
4449 | return -EACCES; | |
4450 | } | |
457f4436 AN |
4451 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
4452 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 4453 | if (err) { |
457f4436 | 4454 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 4455 | regno); |
dbcfe5f7 GB |
4456 | return err; |
4457 | } | |
4458 | ||
b03c9f9f EC |
4459 | /* If we haven't set a max value then we need to bail since we can't be |
4460 | * sure we won't do bad things. | |
4461 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 4462 | */ |
b03c9f9f | 4463 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 4464 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
4465 | regno); |
4466 | return -EACCES; | |
4467 | } | |
457f4436 AN |
4468 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
4469 | mem_size, zero_size_allowed); | |
4470 | if (err) { | |
4471 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 4472 | regno); |
457f4436 AN |
4473 | return err; |
4474 | } | |
4475 | ||
4476 | return 0; | |
4477 | } | |
d83525ca | 4478 | |
e9147b44 KKD |
4479 | static int __check_ptr_off_reg(struct bpf_verifier_env *env, |
4480 | const struct bpf_reg_state *reg, int regno, | |
4481 | bool fixed_off_ok) | |
4482 | { | |
4483 | /* Access to this pointer-typed register or passing it to a helper | |
4484 | * is only allowed in its original, unmodified form. | |
4485 | */ | |
4486 | ||
4487 | if (reg->off < 0) { | |
4488 | verbose(env, "negative offset %s ptr R%d off=%d disallowed\n", | |
4489 | reg_type_str(env, reg->type), regno, reg->off); | |
4490 | return -EACCES; | |
4491 | } | |
4492 | ||
4493 | if (!fixed_off_ok && reg->off) { | |
4494 | verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n", | |
4495 | reg_type_str(env, reg->type), regno, reg->off); | |
4496 | return -EACCES; | |
4497 | } | |
4498 | ||
4499 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4500 | char tn_buf[48]; | |
4501 | ||
4502 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4503 | verbose(env, "variable %s access var_off=%s disallowed\n", | |
4504 | reg_type_str(env, reg->type), tn_buf); | |
4505 | return -EACCES; | |
4506 | } | |
4507 | ||
4508 | return 0; | |
4509 | } | |
4510 | ||
4511 | int check_ptr_off_reg(struct bpf_verifier_env *env, | |
4512 | const struct bpf_reg_state *reg, int regno) | |
4513 | { | |
4514 | return __check_ptr_off_reg(env, reg, regno, false); | |
4515 | } | |
4516 | ||
61df10c7 | 4517 | static int map_kptr_match_type(struct bpf_verifier_env *env, |
aa3496ac | 4518 | struct btf_field *kptr_field, |
61df10c7 KKD |
4519 | struct bpf_reg_state *reg, u32 regno) |
4520 | { | |
b32a5dae | 4521 | const char *targ_name = btf_type_name(kptr_field->kptr.btf, kptr_field->kptr.btf_id); |
20c09d92 | 4522 | int perm_flags = PTR_MAYBE_NULL | PTR_TRUSTED | MEM_RCU; |
61df10c7 KKD |
4523 | const char *reg_name = ""; |
4524 | ||
6efe152d | 4525 | /* Only unreferenced case accepts untrusted pointers */ |
aa3496ac | 4526 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
4527 | perm_flags |= PTR_UNTRUSTED; |
4528 | ||
4529 | if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags)) | |
61df10c7 KKD |
4530 | goto bad_type; |
4531 | ||
4532 | if (!btf_is_kernel(reg->btf)) { | |
4533 | verbose(env, "R%d must point to kernel BTF\n", regno); | |
4534 | return -EINVAL; | |
4535 | } | |
4536 | /* We need to verify reg->type and reg->btf, before accessing reg->btf */ | |
b32a5dae | 4537 | reg_name = btf_type_name(reg->btf, reg->btf_id); |
61df10c7 | 4538 | |
c0a5a21c KKD |
4539 | /* For ref_ptr case, release function check should ensure we get one |
4540 | * referenced PTR_TO_BTF_ID, and that its fixed offset is 0. For the | |
4541 | * normal store of unreferenced kptr, we must ensure var_off is zero. | |
4542 | * Since ref_ptr cannot be accessed directly by BPF insns, checks for | |
4543 | * reg->off and reg->ref_obj_id are not needed here. | |
4544 | */ | |
61df10c7 KKD |
4545 | if (__check_ptr_off_reg(env, reg, regno, true)) |
4546 | return -EACCES; | |
4547 | ||
4548 | /* A full type match is needed, as BTF can be vmlinux or module BTF, and | |
4549 | * we also need to take into account the reg->off. | |
4550 | * | |
4551 | * We want to support cases like: | |
4552 | * | |
4553 | * struct foo { | |
4554 | * struct bar br; | |
4555 | * struct baz bz; | |
4556 | * }; | |
4557 | * | |
4558 | * struct foo *v; | |
4559 | * v = func(); // PTR_TO_BTF_ID | |
4560 | * val->foo = v; // reg->off is zero, btf and btf_id match type | |
4561 | * val->bar = &v->br; // reg->off is still zero, but we need to retry with | |
4562 | * // first member type of struct after comparison fails | |
4563 | * val->baz = &v->bz; // reg->off is non-zero, so struct needs to be walked | |
4564 | * // to match type | |
4565 | * | |
4566 | * In the kptr_ref case, check_func_arg_reg_off already ensures reg->off | |
2ab3b380 KKD |
4567 | * is zero. We must also ensure that btf_struct_ids_match does not walk |
4568 | * the struct to match type against first member of struct, i.e. reject | |
4569 | * second case from above. Hence, when type is BPF_KPTR_REF, we set | |
4570 | * strict mode to true for type match. | |
61df10c7 KKD |
4571 | */ |
4572 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
aa3496ac KKD |
4573 | kptr_field->kptr.btf, kptr_field->kptr.btf_id, |
4574 | kptr_field->type == BPF_KPTR_REF)) | |
61df10c7 KKD |
4575 | goto bad_type; |
4576 | return 0; | |
4577 | bad_type: | |
4578 | verbose(env, "invalid kptr access, R%d type=%s%s ", regno, | |
4579 | reg_type_str(env, reg->type), reg_name); | |
6efe152d | 4580 | verbose(env, "expected=%s%s", reg_type_str(env, PTR_TO_BTF_ID), targ_name); |
aa3496ac | 4581 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
4582 | verbose(env, " or %s%s\n", reg_type_str(env, PTR_TO_BTF_ID | PTR_UNTRUSTED), |
4583 | targ_name); | |
4584 | else | |
4585 | verbose(env, "\n"); | |
61df10c7 KKD |
4586 | return -EINVAL; |
4587 | } | |
4588 | ||
20c09d92 AS |
4589 | /* The non-sleepable programs and sleepable programs with explicit bpf_rcu_read_lock() |
4590 | * can dereference RCU protected pointers and result is PTR_TRUSTED. | |
4591 | */ | |
4592 | static bool in_rcu_cs(struct bpf_verifier_env *env) | |
4593 | { | |
4594 | return env->cur_state->active_rcu_lock || !env->prog->aux->sleepable; | |
4595 | } | |
4596 | ||
4597 | /* Once GCC supports btf_type_tag the following mechanism will be replaced with tag check */ | |
4598 | BTF_SET_START(rcu_protected_types) | |
4599 | BTF_ID(struct, prog_test_ref_kfunc) | |
4600 | BTF_ID(struct, cgroup) | |
4601 | BTF_SET_END(rcu_protected_types) | |
4602 | ||
4603 | static bool rcu_protected_object(const struct btf *btf, u32 btf_id) | |
4604 | { | |
4605 | if (!btf_is_kernel(btf)) | |
4606 | return false; | |
4607 | return btf_id_set_contains(&rcu_protected_types, btf_id); | |
4608 | } | |
4609 | ||
4610 | static bool rcu_safe_kptr(const struct btf_field *field) | |
4611 | { | |
4612 | const struct btf_field_kptr *kptr = &field->kptr; | |
4613 | ||
4614 | return field->type == BPF_KPTR_REF && rcu_protected_object(kptr->btf, kptr->btf_id); | |
4615 | } | |
4616 | ||
61df10c7 KKD |
4617 | static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, |
4618 | int value_regno, int insn_idx, | |
aa3496ac | 4619 | struct btf_field *kptr_field) |
61df10c7 KKD |
4620 | { |
4621 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; | |
4622 | int class = BPF_CLASS(insn->code); | |
4623 | struct bpf_reg_state *val_reg; | |
4624 | ||
4625 | /* Things we already checked for in check_map_access and caller: | |
4626 | * - Reject cases where variable offset may touch kptr | |
4627 | * - size of access (must be BPF_DW) | |
4628 | * - tnum_is_const(reg->var_off) | |
aa3496ac | 4629 | * - kptr_field->offset == off + reg->var_off.value |
61df10c7 KKD |
4630 | */ |
4631 | /* Only BPF_[LDX,STX,ST] | BPF_MEM | BPF_DW is supported */ | |
4632 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
4633 | verbose(env, "kptr in map can only be accessed using BPF_MEM instruction mode\n"); | |
4634 | return -EACCES; | |
4635 | } | |
4636 | ||
6efe152d KKD |
4637 | /* We only allow loading referenced kptr, since it will be marked as |
4638 | * untrusted, similar to unreferenced kptr. | |
4639 | */ | |
aa3496ac | 4640 | if (class != BPF_LDX && kptr_field->type == BPF_KPTR_REF) { |
6efe152d | 4641 | verbose(env, "store to referenced kptr disallowed\n"); |
c0a5a21c KKD |
4642 | return -EACCES; |
4643 | } | |
4644 | ||
61df10c7 KKD |
4645 | if (class == BPF_LDX) { |
4646 | val_reg = reg_state(env, value_regno); | |
4647 | /* We can simply mark the value_regno receiving the pointer | |
4648 | * value from map as PTR_TO_BTF_ID, with the correct type. | |
4649 | */ | |
aa3496ac | 4650 | mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, kptr_field->kptr.btf, |
20c09d92 AS |
4651 | kptr_field->kptr.btf_id, |
4652 | rcu_safe_kptr(kptr_field) && in_rcu_cs(env) ? | |
4653 | PTR_MAYBE_NULL | MEM_RCU : | |
4654 | PTR_MAYBE_NULL | PTR_UNTRUSTED); | |
61df10c7 KKD |
4655 | /* For mark_ptr_or_null_reg */ |
4656 | val_reg->id = ++env->id_gen; | |
4657 | } else if (class == BPF_STX) { | |
4658 | val_reg = reg_state(env, value_regno); | |
4659 | if (!register_is_null(val_reg) && | |
aa3496ac | 4660 | map_kptr_match_type(env, kptr_field, val_reg, value_regno)) |
61df10c7 KKD |
4661 | return -EACCES; |
4662 | } else if (class == BPF_ST) { | |
4663 | if (insn->imm) { | |
4664 | verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%u\n", | |
aa3496ac | 4665 | kptr_field->offset); |
61df10c7 KKD |
4666 | return -EACCES; |
4667 | } | |
4668 | } else { | |
4669 | verbose(env, "kptr in map can only be accessed using BPF_LDX/BPF_STX/BPF_ST\n"); | |
4670 | return -EACCES; | |
4671 | } | |
4672 | return 0; | |
4673 | } | |
4674 | ||
457f4436 AN |
4675 | /* check read/write into a map element with possible variable offset */ |
4676 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
61df10c7 KKD |
4677 | int off, int size, bool zero_size_allowed, |
4678 | enum bpf_access_src src) | |
457f4436 AN |
4679 | { |
4680 | struct bpf_verifier_state *vstate = env->cur_state; | |
4681 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4682 | struct bpf_reg_state *reg = &state->regs[regno]; | |
4683 | struct bpf_map *map = reg->map_ptr; | |
aa3496ac KKD |
4684 | struct btf_record *rec; |
4685 | int err, i; | |
457f4436 AN |
4686 | |
4687 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
4688 | zero_size_allowed); | |
4689 | if (err) | |
4690 | return err; | |
4691 | ||
aa3496ac KKD |
4692 | if (IS_ERR_OR_NULL(map->record)) |
4693 | return 0; | |
4694 | rec = map->record; | |
4695 | for (i = 0; i < rec->cnt; i++) { | |
4696 | struct btf_field *field = &rec->fields[i]; | |
4697 | u32 p = field->offset; | |
d83525ca | 4698 | |
db559117 KKD |
4699 | /* If any part of a field can be touched by load/store, reject |
4700 | * this program. To check that [x1, x2) overlaps with [y1, y2), | |
d83525ca AS |
4701 | * it is sufficient to check x1 < y2 && y1 < x2. |
4702 | */ | |
aa3496ac KKD |
4703 | if (reg->smin_value + off < p + btf_field_type_size(field->type) && |
4704 | p < reg->umax_value + off + size) { | |
4705 | switch (field->type) { | |
4706 | case BPF_KPTR_UNREF: | |
4707 | case BPF_KPTR_REF: | |
61df10c7 KKD |
4708 | if (src != ACCESS_DIRECT) { |
4709 | verbose(env, "kptr cannot be accessed indirectly by helper\n"); | |
4710 | return -EACCES; | |
4711 | } | |
4712 | if (!tnum_is_const(reg->var_off)) { | |
4713 | verbose(env, "kptr access cannot have variable offset\n"); | |
4714 | return -EACCES; | |
4715 | } | |
4716 | if (p != off + reg->var_off.value) { | |
4717 | verbose(env, "kptr access misaligned expected=%u off=%llu\n", | |
4718 | p, off + reg->var_off.value); | |
4719 | return -EACCES; | |
4720 | } | |
4721 | if (size != bpf_size_to_bytes(BPF_DW)) { | |
4722 | verbose(env, "kptr access size must be BPF_DW\n"); | |
4723 | return -EACCES; | |
4724 | } | |
4725 | break; | |
aa3496ac | 4726 | default: |
db559117 KKD |
4727 | verbose(env, "%s cannot be accessed directly by load/store\n", |
4728 | btf_field_type_name(field->type)); | |
aa3496ac | 4729 | return -EACCES; |
61df10c7 KKD |
4730 | } |
4731 | } | |
4732 | } | |
aa3496ac | 4733 | return 0; |
dbcfe5f7 GB |
4734 | } |
4735 | ||
969bf05e AS |
4736 | #define MAX_PACKET_OFF 0xffff |
4737 | ||
58e2af8b | 4738 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
4739 | const struct bpf_call_arg_meta *meta, |
4740 | enum bpf_access_type t) | |
4acf6c0b | 4741 | { |
7e40781c UP |
4742 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
4743 | ||
4744 | switch (prog_type) { | |
5d66fa7d | 4745 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
4746 | case BPF_PROG_TYPE_LWT_IN: |
4747 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 4748 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 4749 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 4750 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 4751 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
4752 | if (t == BPF_WRITE) |
4753 | return false; | |
8731745e | 4754 | fallthrough; |
5d66fa7d DB |
4755 | |
4756 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
4757 | case BPF_PROG_TYPE_SCHED_CLS: |
4758 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 4759 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 4760 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 4761 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 4762 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
4763 | if (meta) |
4764 | return meta->pkt_access; | |
4765 | ||
4766 | env->seen_direct_write = true; | |
4acf6c0b | 4767 | return true; |
0d01da6a SF |
4768 | |
4769 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
4770 | if (t == BPF_WRITE) | |
4771 | env->seen_direct_write = true; | |
4772 | ||
4773 | return true; | |
4774 | ||
4acf6c0b BB |
4775 | default: |
4776 | return false; | |
4777 | } | |
4778 | } | |
4779 | ||
f1174f77 | 4780 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 4781 | int size, bool zero_size_allowed) |
f1174f77 | 4782 | { |
638f5b90 | 4783 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
4784 | struct bpf_reg_state *reg = ®s[regno]; |
4785 | int err; | |
4786 | ||
4787 | /* We may have added a variable offset to the packet pointer; but any | |
4788 | * reg->range we have comes after that. We are only checking the fixed | |
4789 | * offset. | |
4790 | */ | |
4791 | ||
4792 | /* We don't allow negative numbers, because we aren't tracking enough | |
4793 | * detail to prove they're safe. | |
4794 | */ | |
b03c9f9f | 4795 | if (reg->smin_value < 0) { |
61bd5218 | 4796 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
4797 | regno); |
4798 | return -EACCES; | |
4799 | } | |
6d94e741 AS |
4800 | |
4801 | err = reg->range < 0 ? -EINVAL : | |
4802 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 4803 | zero_size_allowed); |
f1174f77 | 4804 | if (err) { |
61bd5218 | 4805 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
4806 | return err; |
4807 | } | |
e647815a | 4808 | |
457f4436 | 4809 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
4810 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
4811 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 4812 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
4813 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
4814 | */ | |
4815 | env->prog->aux->max_pkt_offset = | |
4816 | max_t(u32, env->prog->aux->max_pkt_offset, | |
4817 | off + reg->umax_value + size - 1); | |
4818 | ||
f1174f77 EC |
4819 | return err; |
4820 | } | |
4821 | ||
4822 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 4823 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 4824 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 4825 | struct btf **btf, u32 *btf_id) |
17a52670 | 4826 | { |
f96da094 DB |
4827 | struct bpf_insn_access_aux info = { |
4828 | .reg_type = *reg_type, | |
9e15db66 | 4829 | .log = &env->log, |
f96da094 | 4830 | }; |
31fd8581 | 4831 | |
4f9218aa | 4832 | if (env->ops->is_valid_access && |
5e43f899 | 4833 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
4834 | /* A non zero info.ctx_field_size indicates that this field is a |
4835 | * candidate for later verifier transformation to load the whole | |
4836 | * field and then apply a mask when accessed with a narrower | |
4837 | * access than actual ctx access size. A zero info.ctx_field_size | |
4838 | * will only allow for whole field access and rejects any other | |
4839 | * type of narrower access. | |
31fd8581 | 4840 | */ |
23994631 | 4841 | *reg_type = info.reg_type; |
31fd8581 | 4842 | |
c25b2ae1 | 4843 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 4844 | *btf = info.btf; |
9e15db66 | 4845 | *btf_id = info.btf_id; |
22dc4a0f | 4846 | } else { |
9e15db66 | 4847 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 4848 | } |
32bbe007 AS |
4849 | /* remember the offset of last byte accessed in ctx */ |
4850 | if (env->prog->aux->max_ctx_offset < off + size) | |
4851 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 4852 | return 0; |
32bbe007 | 4853 | } |
17a52670 | 4854 | |
61bd5218 | 4855 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
4856 | return -EACCES; |
4857 | } | |
4858 | ||
d58e468b PP |
4859 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
4860 | int size) | |
4861 | { | |
4862 | if (size < 0 || off < 0 || | |
4863 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
4864 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
4865 | off, size); | |
4866 | return -EACCES; | |
4867 | } | |
4868 | return 0; | |
4869 | } | |
4870 | ||
5f456649 MKL |
4871 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
4872 | u32 regno, int off, int size, | |
4873 | enum bpf_access_type t) | |
c64b7983 JS |
4874 | { |
4875 | struct bpf_reg_state *regs = cur_regs(env); | |
4876 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 4877 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 4878 | bool valid; |
c64b7983 JS |
4879 | |
4880 | if (reg->smin_value < 0) { | |
4881 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
4882 | regno); | |
4883 | return -EACCES; | |
4884 | } | |
4885 | ||
46f8bc92 MKL |
4886 | switch (reg->type) { |
4887 | case PTR_TO_SOCK_COMMON: | |
4888 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
4889 | break; | |
4890 | case PTR_TO_SOCKET: | |
4891 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
4892 | break; | |
655a51e5 MKL |
4893 | case PTR_TO_TCP_SOCK: |
4894 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
4895 | break; | |
fada7fdc JL |
4896 | case PTR_TO_XDP_SOCK: |
4897 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
4898 | break; | |
46f8bc92 MKL |
4899 | default: |
4900 | valid = false; | |
c64b7983 JS |
4901 | } |
4902 | ||
5f456649 | 4903 | |
46f8bc92 MKL |
4904 | if (valid) { |
4905 | env->insn_aux_data[insn_idx].ctx_field_size = | |
4906 | info.ctx_field_size; | |
4907 | return 0; | |
4908 | } | |
4909 | ||
4910 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
c25b2ae1 | 4911 | regno, reg_type_str(env, reg->type), off, size); |
46f8bc92 MKL |
4912 | |
4913 | return -EACCES; | |
c64b7983 JS |
4914 | } |
4915 | ||
4cabc5b1 DB |
4916 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
4917 | { | |
2a159c6f | 4918 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
4919 | } |
4920 | ||
f37a8cb8 DB |
4921 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
4922 | { | |
2a159c6f | 4923 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 4924 | |
46f8bc92 MKL |
4925 | return reg->type == PTR_TO_CTX; |
4926 | } | |
4927 | ||
4928 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
4929 | { | |
4930 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4931 | ||
4932 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
4933 | } |
4934 | ||
ca369602 DB |
4935 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
4936 | { | |
2a159c6f | 4937 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
4938 | |
4939 | return type_is_pkt_pointer(reg->type); | |
4940 | } | |
4941 | ||
4b5defde DB |
4942 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
4943 | { | |
4944 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4945 | ||
4946 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
4947 | return reg->type == PTR_TO_FLOW_KEYS; | |
4948 | } | |
4949 | ||
9bb00b28 YS |
4950 | static bool is_trusted_reg(const struct bpf_reg_state *reg) |
4951 | { | |
4952 | /* A referenced register is always trusted. */ | |
4953 | if (reg->ref_obj_id) | |
4954 | return true; | |
4955 | ||
4956 | /* If a register is not referenced, it is trusted if it has the | |
fca1aa75 | 4957 | * MEM_ALLOC or PTR_TRUSTED type modifiers, and no others. Some of the |
9bb00b28 YS |
4958 | * other type modifiers may be safe, but we elect to take an opt-in |
4959 | * approach here as some (e.g. PTR_UNTRUSTED and PTR_MAYBE_NULL) are | |
4960 | * not. | |
4961 | * | |
4962 | * Eventually, we should make PTR_TRUSTED the single source of truth | |
4963 | * for whether a register is trusted. | |
4964 | */ | |
4965 | return type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS && | |
4966 | !bpf_type_has_unsafe_modifiers(reg->type); | |
4967 | } | |
4968 | ||
fca1aa75 YS |
4969 | static bool is_rcu_reg(const struct bpf_reg_state *reg) |
4970 | { | |
4971 | return reg->type & MEM_RCU; | |
4972 | } | |
4973 | ||
61bd5218 JK |
4974 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
4975 | const struct bpf_reg_state *reg, | |
d1174416 | 4976 | int off, int size, bool strict) |
969bf05e | 4977 | { |
f1174f77 | 4978 | struct tnum reg_off; |
e07b98d9 | 4979 | int ip_align; |
d1174416 DM |
4980 | |
4981 | /* Byte size accesses are always allowed. */ | |
4982 | if (!strict || size == 1) | |
4983 | return 0; | |
4984 | ||
e4eda884 DM |
4985 | /* For platforms that do not have a Kconfig enabling |
4986 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
4987 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
4988 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
4989 | * to this code only in strict mode where we want to emulate | |
4990 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
4991 | * unconditional IP align value of '2'. | |
e07b98d9 | 4992 | */ |
e4eda884 | 4993 | ip_align = 2; |
f1174f77 EC |
4994 | |
4995 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
4996 | if (!tnum_is_aligned(reg_off, size)) { | |
4997 | char tn_buf[48]; | |
4998 | ||
4999 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
5000 | verbose(env, |
5001 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 5002 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
5003 | return -EACCES; |
5004 | } | |
79adffcd | 5005 | |
969bf05e AS |
5006 | return 0; |
5007 | } | |
5008 | ||
61bd5218 JK |
5009 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
5010 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
5011 | const char *pointer_desc, |
5012 | int off, int size, bool strict) | |
79adffcd | 5013 | { |
f1174f77 EC |
5014 | struct tnum reg_off; |
5015 | ||
5016 | /* Byte size accesses are always allowed. */ | |
5017 | if (!strict || size == 1) | |
5018 | return 0; | |
5019 | ||
5020 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
5021 | if (!tnum_is_aligned(reg_off, size)) { | |
5022 | char tn_buf[48]; | |
5023 | ||
5024 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 5025 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 5026 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
5027 | return -EACCES; |
5028 | } | |
5029 | ||
969bf05e AS |
5030 | return 0; |
5031 | } | |
5032 | ||
e07b98d9 | 5033 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
5034 | const struct bpf_reg_state *reg, int off, |
5035 | int size, bool strict_alignment_once) | |
79adffcd | 5036 | { |
ca369602 | 5037 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 5038 | const char *pointer_desc = ""; |
d1174416 | 5039 | |
79adffcd DB |
5040 | switch (reg->type) { |
5041 | case PTR_TO_PACKET: | |
de8f3a83 DB |
5042 | case PTR_TO_PACKET_META: |
5043 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
5044 | * right in front, treat it the very same way. | |
5045 | */ | |
61bd5218 | 5046 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
5047 | case PTR_TO_FLOW_KEYS: |
5048 | pointer_desc = "flow keys "; | |
5049 | break; | |
69c087ba YS |
5050 | case PTR_TO_MAP_KEY: |
5051 | pointer_desc = "key "; | |
5052 | break; | |
f1174f77 EC |
5053 | case PTR_TO_MAP_VALUE: |
5054 | pointer_desc = "value "; | |
5055 | break; | |
5056 | case PTR_TO_CTX: | |
5057 | pointer_desc = "context "; | |
5058 | break; | |
5059 | case PTR_TO_STACK: | |
5060 | pointer_desc = "stack "; | |
01f810ac AM |
5061 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
5062 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
5063 | * aligned. |
5064 | */ | |
5065 | strict = true; | |
f1174f77 | 5066 | break; |
c64b7983 JS |
5067 | case PTR_TO_SOCKET: |
5068 | pointer_desc = "sock "; | |
5069 | break; | |
46f8bc92 MKL |
5070 | case PTR_TO_SOCK_COMMON: |
5071 | pointer_desc = "sock_common "; | |
5072 | break; | |
655a51e5 MKL |
5073 | case PTR_TO_TCP_SOCK: |
5074 | pointer_desc = "tcp_sock "; | |
5075 | break; | |
fada7fdc JL |
5076 | case PTR_TO_XDP_SOCK: |
5077 | pointer_desc = "xdp_sock "; | |
5078 | break; | |
79adffcd | 5079 | default: |
f1174f77 | 5080 | break; |
79adffcd | 5081 | } |
61bd5218 JK |
5082 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
5083 | strict); | |
79adffcd DB |
5084 | } |
5085 | ||
f4d7e40a AS |
5086 | static int update_stack_depth(struct bpf_verifier_env *env, |
5087 | const struct bpf_func_state *func, | |
5088 | int off) | |
5089 | { | |
9c8105bd | 5090 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
5091 | |
5092 | if (stack >= -off) | |
5093 | return 0; | |
5094 | ||
5095 | /* update known max for given subprogram */ | |
9c8105bd | 5096 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
5097 | return 0; |
5098 | } | |
f4d7e40a | 5099 | |
70a87ffe AS |
5100 | /* starting from main bpf function walk all instructions of the function |
5101 | * and recursively walk all callees that given function can call. | |
5102 | * Ignore jump and exit insns. | |
5103 | * Since recursion is prevented by check_cfg() this algorithm | |
5104 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
5105 | */ | |
5106 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
5107 | { | |
9c8105bd JW |
5108 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
5109 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 5110 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 5111 | bool tail_call_reachable = false; |
70a87ffe AS |
5112 | int ret_insn[MAX_CALL_FRAMES]; |
5113 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 5114 | int j; |
f4d7e40a | 5115 | |
70a87ffe | 5116 | process_func: |
7f6e4312 MF |
5117 | /* protect against potential stack overflow that might happen when |
5118 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
5119 | * depth for such case down to 256 so that the worst case scenario | |
5120 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
5121 | * 8k). | |
5122 | * | |
5123 | * To get the idea what might happen, see an example: | |
5124 | * func1 -> sub rsp, 128 | |
5125 | * subfunc1 -> sub rsp, 256 | |
5126 | * tailcall1 -> add rsp, 256 | |
5127 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
5128 | * subfunc2 -> sub rsp, 64 | |
5129 | * subfunc22 -> sub rsp, 128 | |
5130 | * tailcall2 -> add rsp, 128 | |
5131 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
5132 | * | |
5133 | * tailcall will unwind the current stack frame but it will not get rid | |
5134 | * of caller's stack as shown on the example above. | |
5135 | */ | |
5136 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
5137 | verbose(env, | |
5138 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
5139 | depth); | |
5140 | return -EACCES; | |
5141 | } | |
70a87ffe AS |
5142 | /* round up to 32-bytes, since this is granularity |
5143 | * of interpreter stack size | |
5144 | */ | |
9c8105bd | 5145 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 5146 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 5147 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 5148 | frame + 1, depth); |
f4d7e40a AS |
5149 | return -EACCES; |
5150 | } | |
70a87ffe | 5151 | continue_func: |
4cb3d99c | 5152 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 5153 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
5154 | int next_insn; |
5155 | ||
69c087ba | 5156 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
5157 | continue; |
5158 | /* remember insn and function to return to */ | |
5159 | ret_insn[frame] = i + 1; | |
9c8105bd | 5160 | ret_prog[frame] = idx; |
70a87ffe AS |
5161 | |
5162 | /* find the callee */ | |
7ddc80a4 AS |
5163 | next_insn = i + insn[i].imm + 1; |
5164 | idx = find_subprog(env, next_insn); | |
9c8105bd | 5165 | if (idx < 0) { |
70a87ffe | 5166 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 5167 | next_insn); |
70a87ffe AS |
5168 | return -EFAULT; |
5169 | } | |
7ddc80a4 AS |
5170 | if (subprog[idx].is_async_cb) { |
5171 | if (subprog[idx].has_tail_call) { | |
5172 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
5173 | return -EFAULT; | |
5174 | } | |
5175 | /* async callbacks don't increase bpf prog stack size */ | |
5176 | continue; | |
5177 | } | |
5178 | i = next_insn; | |
ebf7d1f5 MF |
5179 | |
5180 | if (subprog[idx].has_tail_call) | |
5181 | tail_call_reachable = true; | |
5182 | ||
70a87ffe AS |
5183 | frame++; |
5184 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
5185 | verbose(env, "the call stack of %d frames is too deep !\n", |
5186 | frame); | |
5187 | return -E2BIG; | |
70a87ffe AS |
5188 | } |
5189 | goto process_func; | |
5190 | } | |
ebf7d1f5 MF |
5191 | /* if tail call got detected across bpf2bpf calls then mark each of the |
5192 | * currently present subprog frames as tail call reachable subprogs; | |
5193 | * this info will be utilized by JIT so that we will be preserving the | |
5194 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
5195 | */ | |
5196 | if (tail_call_reachable) | |
5197 | for (j = 0; j < frame; j++) | |
5198 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
5199 | if (subprog[0].tail_call_reachable) |
5200 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 5201 | |
70a87ffe AS |
5202 | /* end of for() loop means the last insn of the 'subprog' |
5203 | * was reached. Doesn't matter whether it was JA or EXIT | |
5204 | */ | |
5205 | if (frame == 0) | |
5206 | return 0; | |
9c8105bd | 5207 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
5208 | frame--; |
5209 | i = ret_insn[frame]; | |
9c8105bd | 5210 | idx = ret_prog[frame]; |
70a87ffe | 5211 | goto continue_func; |
f4d7e40a AS |
5212 | } |
5213 | ||
19d28fbd | 5214 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
5215 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
5216 | const struct bpf_insn *insn, int idx) | |
5217 | { | |
5218 | int start = idx + insn->imm + 1, subprog; | |
5219 | ||
5220 | subprog = find_subprog(env, start); | |
5221 | if (subprog < 0) { | |
5222 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
5223 | start); | |
5224 | return -EFAULT; | |
5225 | } | |
9c8105bd | 5226 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 5227 | } |
19d28fbd | 5228 | #endif |
1ea47e01 | 5229 | |
afbf21dc YS |
5230 | static int __check_buffer_access(struct bpf_verifier_env *env, |
5231 | const char *buf_info, | |
5232 | const struct bpf_reg_state *reg, | |
5233 | int regno, int off, int size) | |
9df1c28b MM |
5234 | { |
5235 | if (off < 0) { | |
5236 | verbose(env, | |
4fc00b79 | 5237 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 5238 | regno, buf_info, off, size); |
9df1c28b MM |
5239 | return -EACCES; |
5240 | } | |
5241 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5242 | char tn_buf[48]; | |
5243 | ||
5244 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5245 | verbose(env, | |
4fc00b79 | 5246 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
5247 | regno, off, tn_buf); |
5248 | return -EACCES; | |
5249 | } | |
afbf21dc YS |
5250 | |
5251 | return 0; | |
5252 | } | |
5253 | ||
5254 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
5255 | const struct bpf_reg_state *reg, | |
5256 | int regno, int off, int size) | |
5257 | { | |
5258 | int err; | |
5259 | ||
5260 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
5261 | if (err) | |
5262 | return err; | |
5263 | ||
9df1c28b MM |
5264 | if (off + size > env->prog->aux->max_tp_access) |
5265 | env->prog->aux->max_tp_access = off + size; | |
5266 | ||
5267 | return 0; | |
5268 | } | |
5269 | ||
afbf21dc YS |
5270 | static int check_buffer_access(struct bpf_verifier_env *env, |
5271 | const struct bpf_reg_state *reg, | |
5272 | int regno, int off, int size, | |
5273 | bool zero_size_allowed, | |
afbf21dc YS |
5274 | u32 *max_access) |
5275 | { | |
44e9a741 | 5276 | const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; |
afbf21dc YS |
5277 | int err; |
5278 | ||
5279 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
5280 | if (err) | |
5281 | return err; | |
5282 | ||
5283 | if (off + size > *max_access) | |
5284 | *max_access = off + size; | |
5285 | ||
5286 | return 0; | |
5287 | } | |
5288 | ||
3f50f132 JF |
5289 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
5290 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
5291 | { | |
5292 | reg->var_off = tnum_subreg(reg->var_off); | |
5293 | __reg_assign_32_into_64(reg); | |
5294 | } | |
9df1c28b | 5295 | |
0c17d1d2 JH |
5296 | /* truncate register to smaller size (in bytes) |
5297 | * must be called with size < BPF_REG_SIZE | |
5298 | */ | |
5299 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
5300 | { | |
5301 | u64 mask; | |
5302 | ||
5303 | /* clear high bits in bit representation */ | |
5304 | reg->var_off = tnum_cast(reg->var_off, size); | |
5305 | ||
5306 | /* fix arithmetic bounds */ | |
5307 | mask = ((u64)1 << (size * 8)) - 1; | |
5308 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
5309 | reg->umin_value &= mask; | |
5310 | reg->umax_value &= mask; | |
5311 | } else { | |
5312 | reg->umin_value = 0; | |
5313 | reg->umax_value = mask; | |
5314 | } | |
5315 | reg->smin_value = reg->umin_value; | |
5316 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
5317 | |
5318 | /* If size is smaller than 32bit register the 32bit register | |
5319 | * values are also truncated so we push 64-bit bounds into | |
5320 | * 32-bit bounds. Above were truncated < 32-bits already. | |
5321 | */ | |
5322 | if (size >= 4) | |
5323 | return; | |
5324 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
5325 | } |
5326 | ||
a23740ec AN |
5327 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
5328 | { | |
353050be DB |
5329 | /* A map is considered read-only if the following condition are true: |
5330 | * | |
5331 | * 1) BPF program side cannot change any of the map content. The | |
5332 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
5333 | * and was set at map creation time. | |
5334 | * 2) The map value(s) have been initialized from user space by a | |
5335 | * loader and then "frozen", such that no new map update/delete | |
5336 | * operations from syscall side are possible for the rest of | |
5337 | * the map's lifetime from that point onwards. | |
5338 | * 3) Any parallel/pending map update/delete operations from syscall | |
5339 | * side have been completed. Only after that point, it's safe to | |
5340 | * assume that map value(s) are immutable. | |
5341 | */ | |
5342 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
5343 | READ_ONCE(map->frozen) && | |
5344 | !bpf_map_write_active(map); | |
a23740ec AN |
5345 | } |
5346 | ||
5347 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
5348 | { | |
5349 | void *ptr; | |
5350 | u64 addr; | |
5351 | int err; | |
5352 | ||
5353 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
5354 | if (err) | |
5355 | return err; | |
2dedd7d2 | 5356 | ptr = (void *)(long)addr + off; |
a23740ec AN |
5357 | |
5358 | switch (size) { | |
5359 | case sizeof(u8): | |
5360 | *val = (u64)*(u8 *)ptr; | |
5361 | break; | |
5362 | case sizeof(u16): | |
5363 | *val = (u64)*(u16 *)ptr; | |
5364 | break; | |
5365 | case sizeof(u32): | |
5366 | *val = (u64)*(u32 *)ptr; | |
5367 | break; | |
5368 | case sizeof(u64): | |
5369 | *val = *(u64 *)ptr; | |
5370 | break; | |
5371 | default: | |
5372 | return -EINVAL; | |
5373 | } | |
5374 | return 0; | |
5375 | } | |
5376 | ||
6fcd486b AS |
5377 | #define BTF_TYPE_SAFE_RCU(__type) __PASTE(__type, __safe_rcu) |
5378 | #define BTF_TYPE_SAFE_TRUSTED(__type) __PASTE(__type, __safe_trusted) | |
57539b1c | 5379 | |
6fcd486b AS |
5380 | /* |
5381 | * Allow list few fields as RCU trusted or full trusted. | |
5382 | * This logic doesn't allow mix tagging and will be removed once GCC supports | |
5383 | * btf_type_tag. | |
5384 | */ | |
5385 | ||
5386 | /* RCU trusted: these fields are trusted in RCU CS and never NULL */ | |
5387 | BTF_TYPE_SAFE_RCU(struct task_struct) { | |
57539b1c | 5388 | const cpumask_t *cpus_ptr; |
8d093b4e | 5389 | struct css_set __rcu *cgroups; |
6fcd486b AS |
5390 | struct task_struct __rcu *real_parent; |
5391 | struct task_struct *group_leader; | |
8d093b4e AS |
5392 | }; |
5393 | ||
6fcd486b | 5394 | BTF_TYPE_SAFE_RCU(struct css_set) { |
8d093b4e | 5395 | struct cgroup *dfl_cgrp; |
57539b1c DV |
5396 | }; |
5397 | ||
6fcd486b AS |
5398 | /* full trusted: these fields are trusted even outside of RCU CS and never NULL */ |
5399 | BTF_TYPE_SAFE_TRUSTED(struct bpf_iter_meta) { | |
5400 | __bpf_md_ptr(struct seq_file *, seq); | |
5401 | }; | |
5402 | ||
5403 | BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task) { | |
5404 | __bpf_md_ptr(struct bpf_iter_meta *, meta); | |
5405 | __bpf_md_ptr(struct task_struct *, task); | |
5406 | }; | |
5407 | ||
5408 | BTF_TYPE_SAFE_TRUSTED(struct linux_binprm) { | |
5409 | struct file *file; | |
5410 | }; | |
5411 | ||
5412 | BTF_TYPE_SAFE_TRUSTED(struct file) { | |
5413 | struct inode *f_inode; | |
5414 | }; | |
5415 | ||
5416 | BTF_TYPE_SAFE_TRUSTED(struct dentry) { | |
5417 | /* no negative dentry-s in places where bpf can see it */ | |
5418 | struct inode *d_inode; | |
5419 | }; | |
5420 | ||
5421 | BTF_TYPE_SAFE_TRUSTED(struct socket) { | |
5422 | struct sock *sk; | |
5423 | }; | |
5424 | ||
5425 | static bool type_is_rcu(struct bpf_verifier_env *env, | |
5426 | struct bpf_reg_state *reg, | |
5427 | int off) | |
57539b1c | 5428 | { |
6fcd486b AS |
5429 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU(struct task_struct)); |
5430 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU(struct css_set)); | |
57539b1c | 5431 | |
6fcd486b AS |
5432 | return btf_nested_type_is_trusted(&env->log, reg, off, "__safe_rcu"); |
5433 | } | |
57539b1c | 5434 | |
6fcd486b AS |
5435 | static bool type_is_trusted(struct bpf_verifier_env *env, |
5436 | struct bpf_reg_state *reg, | |
5437 | int off) | |
5438 | { | |
5439 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter_meta)); | |
5440 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task)); | |
5441 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct linux_binprm)); | |
5442 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct file)); | |
5443 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct dentry)); | |
5444 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct socket)); | |
5445 | ||
5446 | return btf_nested_type_is_trusted(&env->log, reg, off, "__safe_trusted"); | |
57539b1c DV |
5447 | } |
5448 | ||
9e15db66 AS |
5449 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
5450 | struct bpf_reg_state *regs, | |
5451 | int regno, int off, int size, | |
5452 | enum bpf_access_type atype, | |
5453 | int value_regno) | |
5454 | { | |
5455 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
5456 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
5457 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
c6f1bfe8 | 5458 | enum bpf_type_flag flag = 0; |
9e15db66 AS |
5459 | u32 btf_id; |
5460 | int ret; | |
5461 | ||
c67cae55 AS |
5462 | if (!env->allow_ptr_leaks) { |
5463 | verbose(env, | |
5464 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
5465 | tname); | |
5466 | return -EPERM; | |
5467 | } | |
5468 | if (!env->prog->gpl_compatible && btf_is_kernel(reg->btf)) { | |
5469 | verbose(env, | |
5470 | "Cannot access kernel 'struct %s' from non-GPL compatible program\n", | |
5471 | tname); | |
5472 | return -EINVAL; | |
5473 | } | |
9e15db66 AS |
5474 | if (off < 0) { |
5475 | verbose(env, | |
5476 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
5477 | regno, tname, off); | |
5478 | return -EACCES; | |
5479 | } | |
5480 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5481 | char tn_buf[48]; | |
5482 | ||
5483 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5484 | verbose(env, | |
5485 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
5486 | regno, tname, off, tn_buf); | |
5487 | return -EACCES; | |
5488 | } | |
5489 | ||
c6f1bfe8 YS |
5490 | if (reg->type & MEM_USER) { |
5491 | verbose(env, | |
5492 | "R%d is ptr_%s access user memory: off=%d\n", | |
5493 | regno, tname, off); | |
5494 | return -EACCES; | |
5495 | } | |
5496 | ||
5844101a HL |
5497 | if (reg->type & MEM_PERCPU) { |
5498 | verbose(env, | |
5499 | "R%d is ptr_%s access percpu memory: off=%d\n", | |
5500 | regno, tname, off); | |
5501 | return -EACCES; | |
5502 | } | |
5503 | ||
282de143 KKD |
5504 | if (env->ops->btf_struct_access && !type_is_alloc(reg->type)) { |
5505 | if (!btf_is_kernel(reg->btf)) { | |
5506 | verbose(env, "verifier internal error: reg->btf must be kernel btf\n"); | |
5507 | return -EFAULT; | |
5508 | } | |
6728aea7 | 5509 | ret = env->ops->btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 | 5510 | } else { |
282de143 KKD |
5511 | /* Writes are permitted with default btf_struct_access for |
5512 | * program allocated objects (which always have ref_obj_id > 0), | |
5513 | * but not for untrusted PTR_TO_BTF_ID | MEM_ALLOC. | |
5514 | */ | |
5515 | if (atype != BPF_READ && reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
27ae7997 MKL |
5516 | verbose(env, "only read is supported\n"); |
5517 | return -EACCES; | |
5518 | } | |
5519 | ||
6a3cd331 DM |
5520 | if (type_is_alloc(reg->type) && !type_is_non_owning_ref(reg->type) && |
5521 | !reg->ref_obj_id) { | |
282de143 KKD |
5522 | verbose(env, "verifier internal error: ref_obj_id for allocated object must be non-zero\n"); |
5523 | return -EFAULT; | |
5524 | } | |
5525 | ||
6728aea7 | 5526 | ret = btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 MKL |
5527 | } |
5528 | ||
9e15db66 AS |
5529 | if (ret < 0) |
5530 | return ret; | |
5531 | ||
6fcd486b AS |
5532 | if (ret != PTR_TO_BTF_ID) { |
5533 | /* just mark; */ | |
6efe152d | 5534 | |
6fcd486b AS |
5535 | } else if (type_flag(reg->type) & PTR_UNTRUSTED) { |
5536 | /* If this is an untrusted pointer, all pointers formed by walking it | |
5537 | * also inherit the untrusted flag. | |
5538 | */ | |
5539 | flag = PTR_UNTRUSTED; | |
5540 | ||
5541 | } else if (is_trusted_reg(reg) || is_rcu_reg(reg)) { | |
5542 | /* By default any pointer obtained from walking a trusted pointer is no | |
5543 | * longer trusted, unless the field being accessed has explicitly been | |
5544 | * marked as inheriting its parent's state of trust (either full or RCU). | |
5545 | * For example: | |
5546 | * 'cgroups' pointer is untrusted if task->cgroups dereference | |
5547 | * happened in a sleepable program outside of bpf_rcu_read_lock() | |
5548 | * section. In a non-sleepable program it's trusted while in RCU CS (aka MEM_RCU). | |
5549 | * Note bpf_rcu_read_unlock() converts MEM_RCU pointers to PTR_UNTRUSTED. | |
5550 | * | |
5551 | * A regular RCU-protected pointer with __rcu tag can also be deemed | |
5552 | * trusted if we are in an RCU CS. Such pointer can be NULL. | |
20c09d92 | 5553 | */ |
6fcd486b AS |
5554 | if (type_is_trusted(env, reg, off)) { |
5555 | flag |= PTR_TRUSTED; | |
5556 | } else if (in_rcu_cs(env) && !type_may_be_null(reg->type)) { | |
5557 | if (type_is_rcu(env, reg, off)) { | |
5558 | /* ignore __rcu tag and mark it MEM_RCU */ | |
5559 | flag |= MEM_RCU; | |
5560 | } else if (flag & MEM_RCU) { | |
5561 | /* __rcu tagged pointers can be NULL */ | |
5562 | flag |= PTR_MAYBE_NULL; | |
5563 | } else if (flag & (MEM_PERCPU | MEM_USER)) { | |
5564 | /* keep as-is */ | |
5565 | } else { | |
5566 | /* walking unknown pointers yields untrusted pointer */ | |
5567 | flag = PTR_UNTRUSTED; | |
5568 | } | |
5569 | } else { | |
5570 | /* | |
5571 | * If not in RCU CS or MEM_RCU pointer can be NULL then | |
5572 | * aggressively mark as untrusted otherwise such | |
5573 | * pointers will be plain PTR_TO_BTF_ID without flags | |
5574 | * and will be allowed to be passed into helpers for | |
5575 | * compat reasons. | |
5576 | */ | |
5577 | flag = PTR_UNTRUSTED; | |
5578 | } | |
20c09d92 | 5579 | } else { |
6fcd486b | 5580 | /* Old compat. Deprecated */ |
57539b1c | 5581 | flag &= ~PTR_TRUSTED; |
20c09d92 | 5582 | } |
3f00c523 | 5583 | |
41c48f3a | 5584 | if (atype == BPF_READ && value_regno >= 0) |
c6f1bfe8 | 5585 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); |
41c48f3a AI |
5586 | |
5587 | return 0; | |
5588 | } | |
5589 | ||
5590 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
5591 | struct bpf_reg_state *regs, | |
5592 | int regno, int off, int size, | |
5593 | enum bpf_access_type atype, | |
5594 | int value_regno) | |
5595 | { | |
5596 | struct bpf_reg_state *reg = regs + regno; | |
5597 | struct bpf_map *map = reg->map_ptr; | |
6728aea7 | 5598 | struct bpf_reg_state map_reg; |
c6f1bfe8 | 5599 | enum bpf_type_flag flag = 0; |
41c48f3a AI |
5600 | const struct btf_type *t; |
5601 | const char *tname; | |
5602 | u32 btf_id; | |
5603 | int ret; | |
5604 | ||
5605 | if (!btf_vmlinux) { | |
5606 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
5607 | return -ENOTSUPP; | |
5608 | } | |
5609 | ||
5610 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
5611 | verbose(env, "map_ptr access not supported for map type %d\n", | |
5612 | map->map_type); | |
5613 | return -ENOTSUPP; | |
5614 | } | |
5615 | ||
5616 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
5617 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
5618 | ||
c67cae55 | 5619 | if (!env->allow_ptr_leaks) { |
41c48f3a | 5620 | verbose(env, |
c67cae55 | 5621 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
41c48f3a AI |
5622 | tname); |
5623 | return -EPERM; | |
9e15db66 | 5624 | } |
27ae7997 | 5625 | |
41c48f3a AI |
5626 | if (off < 0) { |
5627 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
5628 | regno, tname, off); | |
5629 | return -EACCES; | |
5630 | } | |
5631 | ||
5632 | if (atype != BPF_READ) { | |
5633 | verbose(env, "only read from %s is supported\n", tname); | |
5634 | return -EACCES; | |
5635 | } | |
5636 | ||
6728aea7 KKD |
5637 | /* Simulate access to a PTR_TO_BTF_ID */ |
5638 | memset(&map_reg, 0, sizeof(map_reg)); | |
5639 | mark_btf_ld_reg(env, &map_reg, 0, PTR_TO_BTF_ID, btf_vmlinux, *map->ops->map_btf_id, 0); | |
5640 | ret = btf_struct_access(&env->log, &map_reg, off, size, atype, &btf_id, &flag); | |
41c48f3a AI |
5641 | if (ret < 0) |
5642 | return ret; | |
5643 | ||
5644 | if (value_regno >= 0) | |
c6f1bfe8 | 5645 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id, flag); |
41c48f3a | 5646 | |
9e15db66 AS |
5647 | return 0; |
5648 | } | |
5649 | ||
01f810ac AM |
5650 | /* Check that the stack access at the given offset is within bounds. The |
5651 | * maximum valid offset is -1. | |
5652 | * | |
5653 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
5654 | * -state->allocated_stack for reads. | |
5655 | */ | |
5656 | static int check_stack_slot_within_bounds(int off, | |
5657 | struct bpf_func_state *state, | |
5658 | enum bpf_access_type t) | |
5659 | { | |
5660 | int min_valid_off; | |
5661 | ||
5662 | if (t == BPF_WRITE) | |
5663 | min_valid_off = -MAX_BPF_STACK; | |
5664 | else | |
5665 | min_valid_off = -state->allocated_stack; | |
5666 | ||
5667 | if (off < min_valid_off || off > -1) | |
5668 | return -EACCES; | |
5669 | return 0; | |
5670 | } | |
5671 | ||
5672 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
5673 | * bounds. | |
5674 | * | |
5675 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
5676 | */ | |
5677 | static int check_stack_access_within_bounds( | |
5678 | struct bpf_verifier_env *env, | |
5679 | int regno, int off, int access_size, | |
61df10c7 | 5680 | enum bpf_access_src src, enum bpf_access_type type) |
01f810ac AM |
5681 | { |
5682 | struct bpf_reg_state *regs = cur_regs(env); | |
5683 | struct bpf_reg_state *reg = regs + regno; | |
5684 | struct bpf_func_state *state = func(env, reg); | |
5685 | int min_off, max_off; | |
5686 | int err; | |
5687 | char *err_extra; | |
5688 | ||
5689 | if (src == ACCESS_HELPER) | |
5690 | /* We don't know if helpers are reading or writing (or both). */ | |
5691 | err_extra = " indirect access to"; | |
5692 | else if (type == BPF_READ) | |
5693 | err_extra = " read from"; | |
5694 | else | |
5695 | err_extra = " write to"; | |
5696 | ||
5697 | if (tnum_is_const(reg->var_off)) { | |
5698 | min_off = reg->var_off.value + off; | |
5699 | if (access_size > 0) | |
5700 | max_off = min_off + access_size - 1; | |
5701 | else | |
5702 | max_off = min_off; | |
5703 | } else { | |
5704 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
5705 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
5706 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
5707 | err_extra, regno); | |
5708 | return -EACCES; | |
5709 | } | |
5710 | min_off = reg->smin_value + off; | |
5711 | if (access_size > 0) | |
5712 | max_off = reg->smax_value + off + access_size - 1; | |
5713 | else | |
5714 | max_off = min_off; | |
5715 | } | |
5716 | ||
5717 | err = check_stack_slot_within_bounds(min_off, state, type); | |
5718 | if (!err) | |
5719 | err = check_stack_slot_within_bounds(max_off, state, type); | |
5720 | ||
5721 | if (err) { | |
5722 | if (tnum_is_const(reg->var_off)) { | |
5723 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
5724 | err_extra, regno, off, access_size); | |
5725 | } else { | |
5726 | char tn_buf[48]; | |
5727 | ||
5728 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5729 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
5730 | err_extra, regno, tn_buf, access_size); | |
5731 | } | |
5732 | } | |
5733 | return err; | |
5734 | } | |
41c48f3a | 5735 | |
17a52670 AS |
5736 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
5737 | * if t==write, value_regno is a register which value is stored into memory | |
5738 | * if t==read, value_regno is a register which will receive the value from memory | |
5739 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
5740 | * if t==read && value_regno==-1, don't care what we read from memory | |
5741 | */ | |
ca369602 DB |
5742 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
5743 | int off, int bpf_size, enum bpf_access_type t, | |
5744 | int value_regno, bool strict_alignment_once) | |
17a52670 | 5745 | { |
638f5b90 AS |
5746 | struct bpf_reg_state *regs = cur_regs(env); |
5747 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 5748 | struct bpf_func_state *state; |
17a52670 AS |
5749 | int size, err = 0; |
5750 | ||
5751 | size = bpf_size_to_bytes(bpf_size); | |
5752 | if (size < 0) | |
5753 | return size; | |
5754 | ||
f1174f77 | 5755 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 5756 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
5757 | if (err) |
5758 | return err; | |
17a52670 | 5759 | |
f1174f77 EC |
5760 | /* for access checks, reg->off is just part of off */ |
5761 | off += reg->off; | |
5762 | ||
69c087ba YS |
5763 | if (reg->type == PTR_TO_MAP_KEY) { |
5764 | if (t == BPF_WRITE) { | |
5765 | verbose(env, "write to change key R%d not allowed\n", regno); | |
5766 | return -EACCES; | |
5767 | } | |
5768 | ||
5769 | err = check_mem_region_access(env, regno, off, size, | |
5770 | reg->map_ptr->key_size, false); | |
5771 | if (err) | |
5772 | return err; | |
5773 | if (value_regno >= 0) | |
5774 | mark_reg_unknown(env, regs, value_regno); | |
5775 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
aa3496ac | 5776 | struct btf_field *kptr_field = NULL; |
61df10c7 | 5777 | |
1be7f75d AS |
5778 | if (t == BPF_WRITE && value_regno >= 0 && |
5779 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5780 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
5781 | return -EACCES; |
5782 | } | |
591fe988 DB |
5783 | err = check_map_access_type(env, regno, off, size, t); |
5784 | if (err) | |
5785 | return err; | |
61df10c7 KKD |
5786 | err = check_map_access(env, regno, off, size, false, ACCESS_DIRECT); |
5787 | if (err) | |
5788 | return err; | |
5789 | if (tnum_is_const(reg->var_off)) | |
aa3496ac KKD |
5790 | kptr_field = btf_record_find(reg->map_ptr->record, |
5791 | off + reg->var_off.value, BPF_KPTR); | |
5792 | if (kptr_field) { | |
5793 | err = check_map_kptr_access(env, regno, value_regno, insn_idx, kptr_field); | |
61df10c7 | 5794 | } else if (t == BPF_READ && value_regno >= 0) { |
a23740ec AN |
5795 | struct bpf_map *map = reg->map_ptr; |
5796 | ||
5797 | /* if map is read-only, track its contents as scalars */ | |
5798 | if (tnum_is_const(reg->var_off) && | |
5799 | bpf_map_is_rdonly(map) && | |
5800 | map->ops->map_direct_value_addr) { | |
5801 | int map_off = off + reg->var_off.value; | |
5802 | u64 val = 0; | |
5803 | ||
5804 | err = bpf_map_direct_read(map, map_off, size, | |
5805 | &val); | |
5806 | if (err) | |
5807 | return err; | |
5808 | ||
5809 | regs[value_regno].type = SCALAR_VALUE; | |
5810 | __mark_reg_known(®s[value_regno], val); | |
5811 | } else { | |
5812 | mark_reg_unknown(env, regs, value_regno); | |
5813 | } | |
5814 | } | |
34d3a78c HL |
5815 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
5816 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
5817 | ||
5818 | if (type_may_be_null(reg->type)) { | |
5819 | verbose(env, "R%d invalid mem access '%s'\n", regno, | |
5820 | reg_type_str(env, reg->type)); | |
5821 | return -EACCES; | |
5822 | } | |
5823 | ||
5824 | if (t == BPF_WRITE && rdonly_mem) { | |
5825 | verbose(env, "R%d cannot write into %s\n", | |
5826 | regno, reg_type_str(env, reg->type)); | |
5827 | return -EACCES; | |
5828 | } | |
5829 | ||
457f4436 AN |
5830 | if (t == BPF_WRITE && value_regno >= 0 && |
5831 | is_pointer_value(env, value_regno)) { | |
5832 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
5833 | return -EACCES; | |
5834 | } | |
34d3a78c | 5835 | |
457f4436 AN |
5836 | err = check_mem_region_access(env, regno, off, size, |
5837 | reg->mem_size, false); | |
34d3a78c | 5838 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
457f4436 | 5839 | mark_reg_unknown(env, regs, value_regno); |
1a0dc1ac | 5840 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 5841 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 5842 | struct btf *btf = NULL; |
9e15db66 | 5843 | u32 btf_id = 0; |
19de99f7 | 5844 | |
1be7f75d AS |
5845 | if (t == BPF_WRITE && value_regno >= 0 && |
5846 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5847 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
5848 | return -EACCES; |
5849 | } | |
f1174f77 | 5850 | |
be80a1d3 | 5851 | err = check_ptr_off_reg(env, reg, regno); |
58990d1f DB |
5852 | if (err < 0) |
5853 | return err; | |
5854 | ||
c6f1bfe8 YS |
5855 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, |
5856 | &btf_id); | |
9e15db66 AS |
5857 | if (err) |
5858 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 5859 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 5860 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
5861 | * PTR_TO_PACKET[_META,_END]. In the latter |
5862 | * case, we know the offset is zero. | |
f1174f77 | 5863 | */ |
46f8bc92 | 5864 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 5865 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 5866 | } else { |
638f5b90 | 5867 | mark_reg_known_zero(env, regs, |
61bd5218 | 5868 | value_regno); |
c25b2ae1 | 5869 | if (type_may_be_null(reg_type)) |
46f8bc92 | 5870 | regs[value_regno].id = ++env->id_gen; |
5327ed3d JW |
5871 | /* A load of ctx field could have different |
5872 | * actual load size with the one encoded in the | |
5873 | * insn. When the dst is PTR, it is for sure not | |
5874 | * a sub-register. | |
5875 | */ | |
5876 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
c25b2ae1 | 5877 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 5878 | regs[value_regno].btf = btf; |
9e15db66 | 5879 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 5880 | } |
46f8bc92 | 5881 | } |
638f5b90 | 5882 | regs[value_regno].type = reg_type; |
969bf05e | 5883 | } |
17a52670 | 5884 | |
f1174f77 | 5885 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
5886 | /* Basic bounds checks. */ |
5887 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
5888 | if (err) |
5889 | return err; | |
8726679a | 5890 | |
f4d7e40a AS |
5891 | state = func(env, reg); |
5892 | err = update_stack_depth(env, state, off); | |
5893 | if (err) | |
5894 | return err; | |
8726679a | 5895 | |
01f810ac AM |
5896 | if (t == BPF_READ) |
5897 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 5898 | value_regno); |
01f810ac AM |
5899 | else |
5900 | err = check_stack_write(env, regno, off, size, | |
5901 | value_regno, insn_idx); | |
de8f3a83 | 5902 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 5903 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 5904 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
5905 | return -EACCES; |
5906 | } | |
4acf6c0b BB |
5907 | if (t == BPF_WRITE && value_regno >= 0 && |
5908 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
5909 | verbose(env, "R%d leaks addr into packet\n", |
5910 | value_regno); | |
4acf6c0b BB |
5911 | return -EACCES; |
5912 | } | |
9fd29c08 | 5913 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 5914 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 5915 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
5916 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
5917 | if (t == BPF_WRITE && value_regno >= 0 && | |
5918 | is_pointer_value(env, value_regno)) { | |
5919 | verbose(env, "R%d leaks addr into flow keys\n", | |
5920 | value_regno); | |
5921 | return -EACCES; | |
5922 | } | |
5923 | ||
5924 | err = check_flow_keys_access(env, off, size); | |
5925 | if (!err && t == BPF_READ && value_regno >= 0) | |
5926 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 5927 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 5928 | if (t == BPF_WRITE) { |
46f8bc92 | 5929 | verbose(env, "R%d cannot write into %s\n", |
c25b2ae1 | 5930 | regno, reg_type_str(env, reg->type)); |
c64b7983 JS |
5931 | return -EACCES; |
5932 | } | |
5f456649 | 5933 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
5934 | if (!err && value_regno >= 0) |
5935 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
5936 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
5937 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
5938 | if (!err && t == BPF_READ && value_regno >= 0) | |
5939 | mark_reg_unknown(env, regs, value_regno); | |
bff61f6f HL |
5940 | } else if (base_type(reg->type) == PTR_TO_BTF_ID && |
5941 | !type_may_be_null(reg->type)) { | |
9e15db66 AS |
5942 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
5943 | value_regno); | |
41c48f3a AI |
5944 | } else if (reg->type == CONST_PTR_TO_MAP) { |
5945 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
5946 | value_regno); | |
20b2aff4 HL |
5947 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
5948 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
20b2aff4 HL |
5949 | u32 *max_access; |
5950 | ||
5951 | if (rdonly_mem) { | |
5952 | if (t == BPF_WRITE) { | |
5953 | verbose(env, "R%d cannot write into %s\n", | |
5954 | regno, reg_type_str(env, reg->type)); | |
5955 | return -EACCES; | |
5956 | } | |
20b2aff4 HL |
5957 | max_access = &env->prog->aux->max_rdonly_access; |
5958 | } else { | |
20b2aff4 | 5959 | max_access = &env->prog->aux->max_rdwr_access; |
afbf21dc | 5960 | } |
20b2aff4 | 5961 | |
f6dfbe31 | 5962 | err = check_buffer_access(env, reg, regno, off, size, false, |
44e9a741 | 5963 | max_access); |
20b2aff4 HL |
5964 | |
5965 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) | |
afbf21dc | 5966 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 5967 | } else { |
61bd5218 | 5968 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
c25b2ae1 | 5969 | reg_type_str(env, reg->type)); |
17a52670 AS |
5970 | return -EACCES; |
5971 | } | |
969bf05e | 5972 | |
f1174f77 | 5973 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 5974 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 5975 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 5976 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 5977 | } |
17a52670 AS |
5978 | return err; |
5979 | } | |
5980 | ||
91c960b0 | 5981 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 5982 | { |
5ffa2550 | 5983 | int load_reg; |
17a52670 AS |
5984 | int err; |
5985 | ||
5ca419f2 BJ |
5986 | switch (insn->imm) { |
5987 | case BPF_ADD: | |
5988 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
5989 | case BPF_AND: |
5990 | case BPF_AND | BPF_FETCH: | |
5991 | case BPF_OR: | |
5992 | case BPF_OR | BPF_FETCH: | |
5993 | case BPF_XOR: | |
5994 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
5995 | case BPF_XCHG: |
5996 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
5997 | break; |
5998 | default: | |
91c960b0 BJ |
5999 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
6000 | return -EINVAL; | |
6001 | } | |
6002 | ||
6003 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
6004 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
6005 | return -EINVAL; |
6006 | } | |
6007 | ||
6008 | /* check src1 operand */ | |
dc503a8a | 6009 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6010 | if (err) |
6011 | return err; | |
6012 | ||
6013 | /* check src2 operand */ | |
dc503a8a | 6014 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6015 | if (err) |
6016 | return err; | |
6017 | ||
5ffa2550 BJ |
6018 | if (insn->imm == BPF_CMPXCHG) { |
6019 | /* Check comparison of R0 with memory location */ | |
a82fe085 DB |
6020 | const u32 aux_reg = BPF_REG_0; |
6021 | ||
6022 | err = check_reg_arg(env, aux_reg, SRC_OP); | |
5ffa2550 BJ |
6023 | if (err) |
6024 | return err; | |
a82fe085 DB |
6025 | |
6026 | if (is_pointer_value(env, aux_reg)) { | |
6027 | verbose(env, "R%d leaks addr into mem\n", aux_reg); | |
6028 | return -EACCES; | |
6029 | } | |
5ffa2550 BJ |
6030 | } |
6031 | ||
6bdf6abc | 6032 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 6033 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
6034 | return -EACCES; |
6035 | } | |
6036 | ||
ca369602 | 6037 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 6038 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
6039 | is_flow_key_reg(env, insn->dst_reg) || |
6040 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 6041 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f | 6042 | insn->dst_reg, |
c25b2ae1 | 6043 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
6044 | return -EACCES; |
6045 | } | |
6046 | ||
37086bfd BJ |
6047 | if (insn->imm & BPF_FETCH) { |
6048 | if (insn->imm == BPF_CMPXCHG) | |
6049 | load_reg = BPF_REG_0; | |
6050 | else | |
6051 | load_reg = insn->src_reg; | |
6052 | ||
6053 | /* check and record load of old value */ | |
6054 | err = check_reg_arg(env, load_reg, DST_OP); | |
6055 | if (err) | |
6056 | return err; | |
6057 | } else { | |
6058 | /* This instruction accesses a memory location but doesn't | |
6059 | * actually load it into a register. | |
6060 | */ | |
6061 | load_reg = -1; | |
6062 | } | |
6063 | ||
7d3baf0a DB |
6064 | /* Check whether we can read the memory, with second call for fetch |
6065 | * case to simulate the register fill. | |
6066 | */ | |
31fd8581 | 6067 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
7d3baf0a DB |
6068 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
6069 | if (!err && load_reg >= 0) | |
6070 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, | |
6071 | BPF_SIZE(insn->code), BPF_READ, load_reg, | |
6072 | true); | |
17a52670 AS |
6073 | if (err) |
6074 | return err; | |
6075 | ||
7d3baf0a | 6076 | /* Check whether we can write into the same memory. */ |
5ca419f2 BJ |
6077 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
6078 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
6079 | if (err) | |
6080 | return err; | |
6081 | ||
5ca419f2 | 6082 | return 0; |
17a52670 AS |
6083 | } |
6084 | ||
01f810ac AM |
6085 | /* When register 'regno' is used to read the stack (either directly or through |
6086 | * a helper function) make sure that it's within stack boundary and, depending | |
6087 | * on the access type, that all elements of the stack are initialized. | |
6088 | * | |
6089 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
6090 | * | |
6091 | * All registers that have been spilled on the stack in the slots within the | |
6092 | * read offsets are marked as read. | |
6093 | */ | |
6094 | static int check_stack_range_initialized( | |
6095 | struct bpf_verifier_env *env, int regno, int off, | |
6096 | int access_size, bool zero_size_allowed, | |
61df10c7 | 6097 | enum bpf_access_src type, struct bpf_call_arg_meta *meta) |
2011fccf AI |
6098 | { |
6099 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
6100 | struct bpf_func_state *state = func(env, reg); |
6101 | int err, min_off, max_off, i, j, slot, spi; | |
6102 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
6103 | enum bpf_access_type bounds_check_type; | |
6104 | /* Some accesses can write anything into the stack, others are | |
6105 | * read-only. | |
6106 | */ | |
6107 | bool clobber = false; | |
2011fccf | 6108 | |
01f810ac AM |
6109 | if (access_size == 0 && !zero_size_allowed) { |
6110 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
6111 | return -EACCES; |
6112 | } | |
2011fccf | 6113 | |
01f810ac AM |
6114 | if (type == ACCESS_HELPER) { |
6115 | /* The bounds checks for writes are more permissive than for | |
6116 | * reads. However, if raw_mode is not set, we'll do extra | |
6117 | * checks below. | |
6118 | */ | |
6119 | bounds_check_type = BPF_WRITE; | |
6120 | clobber = true; | |
6121 | } else { | |
6122 | bounds_check_type = BPF_READ; | |
6123 | } | |
6124 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
6125 | type, bounds_check_type); | |
6126 | if (err) | |
6127 | return err; | |
6128 | ||
17a52670 | 6129 | |
2011fccf | 6130 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 6131 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 6132 | } else { |
088ec26d AI |
6133 | /* Variable offset is prohibited for unprivileged mode for |
6134 | * simplicity since it requires corresponding support in | |
6135 | * Spectre masking for stack ALU. | |
6136 | * See also retrieve_ptr_limit(). | |
6137 | */ | |
2c78ee89 | 6138 | if (!env->bypass_spec_v1) { |
088ec26d | 6139 | char tn_buf[48]; |
f1174f77 | 6140 | |
088ec26d | 6141 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
6142 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
6143 | regno, err_extra, tn_buf); | |
088ec26d AI |
6144 | return -EACCES; |
6145 | } | |
f2bcd05e AI |
6146 | /* Only initialized buffer on stack is allowed to be accessed |
6147 | * with variable offset. With uninitialized buffer it's hard to | |
6148 | * guarantee that whole memory is marked as initialized on | |
6149 | * helper return since specific bounds are unknown what may | |
6150 | * cause uninitialized stack leaking. | |
6151 | */ | |
6152 | if (meta && meta->raw_mode) | |
6153 | meta = NULL; | |
6154 | ||
01f810ac AM |
6155 | min_off = reg->smin_value + off; |
6156 | max_off = reg->smax_value + off; | |
17a52670 AS |
6157 | } |
6158 | ||
435faee1 | 6159 | if (meta && meta->raw_mode) { |
ef8fc7a0 KKD |
6160 | /* Ensure we won't be overwriting dynptrs when simulating byte |
6161 | * by byte access in check_helper_call using meta.access_size. | |
6162 | * This would be a problem if we have a helper in the future | |
6163 | * which takes: | |
6164 | * | |
6165 | * helper(uninit_mem, len, dynptr) | |
6166 | * | |
6167 | * Now, uninint_mem may overlap with dynptr pointer. Hence, it | |
6168 | * may end up writing to dynptr itself when touching memory from | |
6169 | * arg 1. This can be relaxed on a case by case basis for known | |
6170 | * safe cases, but reject due to the possibilitiy of aliasing by | |
6171 | * default. | |
6172 | */ | |
6173 | for (i = min_off; i < max_off + access_size; i++) { | |
6174 | int stack_off = -i - 1; | |
6175 | ||
6176 | spi = __get_spi(i); | |
6177 | /* raw_mode may write past allocated_stack */ | |
6178 | if (state->allocated_stack <= stack_off) | |
6179 | continue; | |
6180 | if (state->stack[spi].slot_type[stack_off % BPF_REG_SIZE] == STACK_DYNPTR) { | |
6181 | verbose(env, "potential write to dynptr at off=%d disallowed\n", i); | |
6182 | return -EACCES; | |
6183 | } | |
6184 | } | |
435faee1 DB |
6185 | meta->access_size = access_size; |
6186 | meta->regno = regno; | |
6187 | return 0; | |
6188 | } | |
6189 | ||
2011fccf | 6190 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
6191 | u8 *stype; |
6192 | ||
2011fccf | 6193 | slot = -i - 1; |
638f5b90 | 6194 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
6195 | if (state->allocated_stack <= slot) |
6196 | goto err; | |
6197 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
6198 | if (*stype == STACK_MISC) | |
6199 | goto mark; | |
6715df8d EZ |
6200 | if ((*stype == STACK_ZERO) || |
6201 | (*stype == STACK_INVALID && env->allow_uninit_stack)) { | |
01f810ac AM |
6202 | if (clobber) { |
6203 | /* helper can write anything into the stack */ | |
6204 | *stype = STACK_MISC; | |
6205 | } | |
cc2b14d5 | 6206 | goto mark; |
17a52670 | 6207 | } |
1d68f22b | 6208 | |
27113c59 | 6209 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
6210 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
6211 | env->allow_ptr_leaks)) { | |
01f810ac AM |
6212 | if (clobber) { |
6213 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
6214 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 6215 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 6216 | } |
f7cf25b2 AS |
6217 | goto mark; |
6218 | } | |
6219 | ||
cc2b14d5 | 6220 | err: |
2011fccf | 6221 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
6222 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
6223 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
6224 | } else { |
6225 | char tn_buf[48]; | |
6226 | ||
6227 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
6228 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
6229 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 6230 | } |
cc2b14d5 AS |
6231 | return -EACCES; |
6232 | mark: | |
6233 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
6234 | * the whole slot to be marked as 'read' | |
6235 | */ | |
679c782d | 6236 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
6237 | state->stack[spi].spilled_ptr.parent, |
6238 | REG_LIVE_READ64); | |
261f4664 KKD |
6239 | /* We do not set REG_LIVE_WRITTEN for stack slot, as we can not |
6240 | * be sure that whether stack slot is written to or not. Hence, | |
6241 | * we must still conservatively propagate reads upwards even if | |
6242 | * helper may write to the entire memory range. | |
6243 | */ | |
17a52670 | 6244 | } |
2011fccf | 6245 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
6246 | } |
6247 | ||
06c1c049 GB |
6248 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
6249 | int access_size, bool zero_size_allowed, | |
6250 | struct bpf_call_arg_meta *meta) | |
6251 | { | |
638f5b90 | 6252 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
20b2aff4 | 6253 | u32 *max_access; |
06c1c049 | 6254 | |
20b2aff4 | 6255 | switch (base_type(reg->type)) { |
06c1c049 | 6256 | case PTR_TO_PACKET: |
de8f3a83 | 6257 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
6258 | return check_packet_access(env, regno, reg->off, access_size, |
6259 | zero_size_allowed); | |
69c087ba | 6260 | case PTR_TO_MAP_KEY: |
7b3552d3 KKD |
6261 | if (meta && meta->raw_mode) { |
6262 | verbose(env, "R%d cannot write into %s\n", regno, | |
6263 | reg_type_str(env, reg->type)); | |
6264 | return -EACCES; | |
6265 | } | |
69c087ba YS |
6266 | return check_mem_region_access(env, regno, reg->off, access_size, |
6267 | reg->map_ptr->key_size, false); | |
06c1c049 | 6268 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
6269 | if (check_map_access_type(env, regno, reg->off, access_size, |
6270 | meta && meta->raw_mode ? BPF_WRITE : | |
6271 | BPF_READ)) | |
6272 | return -EACCES; | |
9fd29c08 | 6273 | return check_map_access(env, regno, reg->off, access_size, |
61df10c7 | 6274 | zero_size_allowed, ACCESS_HELPER); |
457f4436 | 6275 | case PTR_TO_MEM: |
97e6d7da KKD |
6276 | if (type_is_rdonly_mem(reg->type)) { |
6277 | if (meta && meta->raw_mode) { | |
6278 | verbose(env, "R%d cannot write into %s\n", regno, | |
6279 | reg_type_str(env, reg->type)); | |
6280 | return -EACCES; | |
6281 | } | |
6282 | } | |
457f4436 AN |
6283 | return check_mem_region_access(env, regno, reg->off, |
6284 | access_size, reg->mem_size, | |
6285 | zero_size_allowed); | |
20b2aff4 HL |
6286 | case PTR_TO_BUF: |
6287 | if (type_is_rdonly_mem(reg->type)) { | |
97e6d7da KKD |
6288 | if (meta && meta->raw_mode) { |
6289 | verbose(env, "R%d cannot write into %s\n", regno, | |
6290 | reg_type_str(env, reg->type)); | |
20b2aff4 | 6291 | return -EACCES; |
97e6d7da | 6292 | } |
20b2aff4 | 6293 | |
20b2aff4 HL |
6294 | max_access = &env->prog->aux->max_rdonly_access; |
6295 | } else { | |
20b2aff4 HL |
6296 | max_access = &env->prog->aux->max_rdwr_access; |
6297 | } | |
afbf21dc YS |
6298 | return check_buffer_access(env, reg, regno, reg->off, |
6299 | access_size, zero_size_allowed, | |
44e9a741 | 6300 | max_access); |
0d004c02 | 6301 | case PTR_TO_STACK: |
01f810ac AM |
6302 | return check_stack_range_initialized( |
6303 | env, | |
6304 | regno, reg->off, access_size, | |
6305 | zero_size_allowed, ACCESS_HELPER, meta); | |
15baa55f BT |
6306 | case PTR_TO_CTX: |
6307 | /* in case the function doesn't know how to access the context, | |
6308 | * (because we are in a program of type SYSCALL for example), we | |
6309 | * can not statically check its size. | |
6310 | * Dynamically check it now. | |
6311 | */ | |
6312 | if (!env->ops->convert_ctx_access) { | |
6313 | enum bpf_access_type atype = meta && meta->raw_mode ? BPF_WRITE : BPF_READ; | |
6314 | int offset = access_size - 1; | |
6315 | ||
6316 | /* Allow zero-byte read from PTR_TO_CTX */ | |
6317 | if (access_size == 0) | |
6318 | return zero_size_allowed ? 0 : -EACCES; | |
6319 | ||
6320 | return check_mem_access(env, env->insn_idx, regno, offset, BPF_B, | |
6321 | atype, -1, false); | |
6322 | } | |
6323 | ||
6324 | fallthrough; | |
0d004c02 LB |
6325 | default: /* scalar_value or invalid ptr */ |
6326 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
6327 | if (zero_size_allowed && access_size == 0 && | |
6328 | register_is_null(reg)) | |
6329 | return 0; | |
6330 | ||
c25b2ae1 HL |
6331 | verbose(env, "R%d type=%s ", regno, |
6332 | reg_type_str(env, reg->type)); | |
6333 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); | |
0d004c02 | 6334 | return -EACCES; |
06c1c049 GB |
6335 | } |
6336 | } | |
6337 | ||
d583691c KKD |
6338 | static int check_mem_size_reg(struct bpf_verifier_env *env, |
6339 | struct bpf_reg_state *reg, u32 regno, | |
6340 | bool zero_size_allowed, | |
6341 | struct bpf_call_arg_meta *meta) | |
6342 | { | |
6343 | int err; | |
6344 | ||
6345 | /* This is used to refine r0 return value bounds for helpers | |
6346 | * that enforce this value as an upper bound on return values. | |
6347 | * See do_refine_retval_range() for helpers that can refine | |
6348 | * the return value. C type of helper is u32 so we pull register | |
6349 | * bound from umax_value however, if negative verifier errors | |
6350 | * out. Only upper bounds can be learned because retval is an | |
6351 | * int type and negative retvals are allowed. | |
6352 | */ | |
be77354a | 6353 | meta->msize_max_value = reg->umax_value; |
d583691c KKD |
6354 | |
6355 | /* The register is SCALAR_VALUE; the access check | |
6356 | * happens using its boundaries. | |
6357 | */ | |
6358 | if (!tnum_is_const(reg->var_off)) | |
6359 | /* For unprivileged variable accesses, disable raw | |
6360 | * mode so that the program is required to | |
6361 | * initialize all the memory that the helper could | |
6362 | * just partially fill up. | |
6363 | */ | |
6364 | meta = NULL; | |
6365 | ||
6366 | if (reg->smin_value < 0) { | |
6367 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", | |
6368 | regno); | |
6369 | return -EACCES; | |
6370 | } | |
6371 | ||
6372 | if (reg->umin_value == 0) { | |
6373 | err = check_helper_mem_access(env, regno - 1, 0, | |
6374 | zero_size_allowed, | |
6375 | meta); | |
6376 | if (err) | |
6377 | return err; | |
6378 | } | |
6379 | ||
6380 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { | |
6381 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
6382 | regno); | |
6383 | return -EACCES; | |
6384 | } | |
6385 | err = check_helper_mem_access(env, regno - 1, | |
6386 | reg->umax_value, | |
6387 | zero_size_allowed, meta); | |
6388 | if (!err) | |
6389 | err = mark_chain_precision(env, regno); | |
6390 | return err; | |
6391 | } | |
6392 | ||
e5069b9c DB |
6393 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
6394 | u32 regno, u32 mem_size) | |
6395 | { | |
be77354a KKD |
6396 | bool may_be_null = type_may_be_null(reg->type); |
6397 | struct bpf_reg_state saved_reg; | |
6398 | struct bpf_call_arg_meta meta; | |
6399 | int err; | |
6400 | ||
e5069b9c DB |
6401 | if (register_is_null(reg)) |
6402 | return 0; | |
6403 | ||
be77354a KKD |
6404 | memset(&meta, 0, sizeof(meta)); |
6405 | /* Assuming that the register contains a value check if the memory | |
6406 | * access is safe. Temporarily save and restore the register's state as | |
6407 | * the conversion shouldn't be visible to a caller. | |
6408 | */ | |
6409 | if (may_be_null) { | |
6410 | saved_reg = *reg; | |
e5069b9c | 6411 | mark_ptr_not_null_reg(reg); |
e5069b9c DB |
6412 | } |
6413 | ||
be77354a KKD |
6414 | err = check_helper_mem_access(env, regno, mem_size, true, &meta); |
6415 | /* Check access for BPF_WRITE */ | |
6416 | meta.raw_mode = true; | |
6417 | err = err ?: check_helper_mem_access(env, regno, mem_size, true, &meta); | |
6418 | ||
6419 | if (may_be_null) | |
6420 | *reg = saved_reg; | |
6421 | ||
6422 | return err; | |
e5069b9c DB |
6423 | } |
6424 | ||
00b85860 KKD |
6425 | static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
6426 | u32 regno) | |
d583691c KKD |
6427 | { |
6428 | struct bpf_reg_state *mem_reg = &cur_regs(env)[regno - 1]; | |
6429 | bool may_be_null = type_may_be_null(mem_reg->type); | |
6430 | struct bpf_reg_state saved_reg; | |
be77354a | 6431 | struct bpf_call_arg_meta meta; |
d583691c KKD |
6432 | int err; |
6433 | ||
6434 | WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); | |
6435 | ||
be77354a KKD |
6436 | memset(&meta, 0, sizeof(meta)); |
6437 | ||
d583691c KKD |
6438 | if (may_be_null) { |
6439 | saved_reg = *mem_reg; | |
6440 | mark_ptr_not_null_reg(mem_reg); | |
6441 | } | |
6442 | ||
be77354a KKD |
6443 | err = check_mem_size_reg(env, reg, regno, true, &meta); |
6444 | /* Check access for BPF_WRITE */ | |
6445 | meta.raw_mode = true; | |
6446 | err = err ?: check_mem_size_reg(env, reg, regno, true, &meta); | |
d583691c KKD |
6447 | |
6448 | if (may_be_null) | |
6449 | *mem_reg = saved_reg; | |
6450 | return err; | |
6451 | } | |
6452 | ||
d83525ca | 6453 | /* Implementation details: |
4e814da0 KKD |
6454 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL. |
6455 | * bpf_obj_new returns PTR_TO_BTF_ID | MEM_ALLOC | PTR_MAYBE_NULL. | |
d83525ca | 6456 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
4e814da0 KKD |
6457 | * Two separate bpf_obj_new will also have different reg->id. |
6458 | * For traditional PTR_TO_MAP_VALUE or PTR_TO_BTF_ID | MEM_ALLOC, the verifier | |
6459 | * clears reg->id after value_or_null->value transition, since the verifier only | |
6460 | * cares about the range of access to valid map value pointer and doesn't care | |
6461 | * about actual address of the map element. | |
d83525ca AS |
6462 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
6463 | * reg->id > 0 after value_or_null->value transition. By doing so | |
6464 | * two bpf_map_lookups will be considered two different pointers that | |
4e814da0 KKD |
6465 | * point to different bpf_spin_locks. Likewise for pointers to allocated objects |
6466 | * returned from bpf_obj_new. | |
d83525ca AS |
6467 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
6468 | * dead-locks. | |
6469 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
6470 | * reg_is_refcounted() logic. The verifier needs to remember only | |
6471 | * one spin_lock instead of array of acquired_refs. | |
d0d78c1d | 6472 | * cur_state->active_lock remembers which map value element or allocated |
4e814da0 | 6473 | * object got locked and clears it after bpf_spin_unlock. |
d83525ca AS |
6474 | */ |
6475 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
6476 | bool is_lock) | |
6477 | { | |
6478 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6479 | struct bpf_verifier_state *cur = env->cur_state; | |
6480 | bool is_const = tnum_is_const(reg->var_off); | |
d83525ca | 6481 | u64 val = reg->var_off.value; |
4e814da0 KKD |
6482 | struct bpf_map *map = NULL; |
6483 | struct btf *btf = NULL; | |
6484 | struct btf_record *rec; | |
d83525ca | 6485 | |
d83525ca AS |
6486 | if (!is_const) { |
6487 | verbose(env, | |
6488 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
6489 | regno); | |
6490 | return -EINVAL; | |
6491 | } | |
4e814da0 KKD |
6492 | if (reg->type == PTR_TO_MAP_VALUE) { |
6493 | map = reg->map_ptr; | |
6494 | if (!map->btf) { | |
6495 | verbose(env, | |
6496 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
6497 | map->name); | |
6498 | return -EINVAL; | |
6499 | } | |
6500 | } else { | |
6501 | btf = reg->btf; | |
d83525ca | 6502 | } |
4e814da0 KKD |
6503 | |
6504 | rec = reg_btf_record(reg); | |
6505 | if (!btf_record_has_field(rec, BPF_SPIN_LOCK)) { | |
6506 | verbose(env, "%s '%s' has no valid bpf_spin_lock\n", map ? "map" : "local", | |
6507 | map ? map->name : "kptr"); | |
d83525ca AS |
6508 | return -EINVAL; |
6509 | } | |
4e814da0 | 6510 | if (rec->spin_lock_off != val + reg->off) { |
db559117 | 6511 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock' that is at %d\n", |
4e814da0 | 6512 | val + reg->off, rec->spin_lock_off); |
d83525ca AS |
6513 | return -EINVAL; |
6514 | } | |
6515 | if (is_lock) { | |
d0d78c1d | 6516 | if (cur->active_lock.ptr) { |
d83525ca AS |
6517 | verbose(env, |
6518 | "Locking two bpf_spin_locks are not allowed\n"); | |
6519 | return -EINVAL; | |
6520 | } | |
d0d78c1d KKD |
6521 | if (map) |
6522 | cur->active_lock.ptr = map; | |
6523 | else | |
6524 | cur->active_lock.ptr = btf; | |
6525 | cur->active_lock.id = reg->id; | |
d83525ca | 6526 | } else { |
d0d78c1d KKD |
6527 | void *ptr; |
6528 | ||
6529 | if (map) | |
6530 | ptr = map; | |
6531 | else | |
6532 | ptr = btf; | |
6533 | ||
6534 | if (!cur->active_lock.ptr) { | |
d83525ca AS |
6535 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
6536 | return -EINVAL; | |
6537 | } | |
d0d78c1d KKD |
6538 | if (cur->active_lock.ptr != ptr || |
6539 | cur->active_lock.id != reg->id) { | |
d83525ca AS |
6540 | verbose(env, "bpf_spin_unlock of different lock\n"); |
6541 | return -EINVAL; | |
6542 | } | |
534e86bc | 6543 | |
6a3cd331 | 6544 | invalidate_non_owning_refs(env); |
534e86bc | 6545 | |
6a3cd331 DM |
6546 | cur->active_lock.ptr = NULL; |
6547 | cur->active_lock.id = 0; | |
d83525ca AS |
6548 | } |
6549 | return 0; | |
6550 | } | |
6551 | ||
b00628b1 AS |
6552 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
6553 | struct bpf_call_arg_meta *meta) | |
6554 | { | |
6555 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6556 | bool is_const = tnum_is_const(reg->var_off); | |
6557 | struct bpf_map *map = reg->map_ptr; | |
6558 | u64 val = reg->var_off.value; | |
6559 | ||
6560 | if (!is_const) { | |
6561 | verbose(env, | |
6562 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
6563 | regno); | |
6564 | return -EINVAL; | |
6565 | } | |
6566 | if (!map->btf) { | |
6567 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
6568 | map->name); | |
6569 | return -EINVAL; | |
6570 | } | |
db559117 KKD |
6571 | if (!btf_record_has_field(map->record, BPF_TIMER)) { |
6572 | verbose(env, "map '%s' has no valid bpf_timer\n", map->name); | |
68134668 AS |
6573 | return -EINVAL; |
6574 | } | |
db559117 | 6575 | if (map->record->timer_off != val + reg->off) { |
68134668 | 6576 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
db559117 | 6577 | val + reg->off, map->record->timer_off); |
b00628b1 AS |
6578 | return -EINVAL; |
6579 | } | |
6580 | if (meta->map_ptr) { | |
6581 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
6582 | return -EFAULT; | |
6583 | } | |
3e8ce298 | 6584 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
6585 | meta->map_ptr = map; |
6586 | return 0; | |
6587 | } | |
6588 | ||
c0a5a21c KKD |
6589 | static int process_kptr_func(struct bpf_verifier_env *env, int regno, |
6590 | struct bpf_call_arg_meta *meta) | |
6591 | { | |
6592 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
c0a5a21c | 6593 | struct bpf_map *map_ptr = reg->map_ptr; |
aa3496ac | 6594 | struct btf_field *kptr_field; |
c0a5a21c | 6595 | u32 kptr_off; |
c0a5a21c KKD |
6596 | |
6597 | if (!tnum_is_const(reg->var_off)) { | |
6598 | verbose(env, | |
6599 | "R%d doesn't have constant offset. kptr has to be at the constant offset\n", | |
6600 | regno); | |
6601 | return -EINVAL; | |
6602 | } | |
6603 | if (!map_ptr->btf) { | |
6604 | verbose(env, "map '%s' has to have BTF in order to use bpf_kptr_xchg\n", | |
6605 | map_ptr->name); | |
6606 | return -EINVAL; | |
6607 | } | |
aa3496ac KKD |
6608 | if (!btf_record_has_field(map_ptr->record, BPF_KPTR)) { |
6609 | verbose(env, "map '%s' has no valid kptr\n", map_ptr->name); | |
c0a5a21c KKD |
6610 | return -EINVAL; |
6611 | } | |
6612 | ||
6613 | meta->map_ptr = map_ptr; | |
6614 | kptr_off = reg->off + reg->var_off.value; | |
aa3496ac KKD |
6615 | kptr_field = btf_record_find(map_ptr->record, kptr_off, BPF_KPTR); |
6616 | if (!kptr_field) { | |
c0a5a21c KKD |
6617 | verbose(env, "off=%d doesn't point to kptr\n", kptr_off); |
6618 | return -EACCES; | |
6619 | } | |
aa3496ac | 6620 | if (kptr_field->type != BPF_KPTR_REF) { |
c0a5a21c KKD |
6621 | verbose(env, "off=%d kptr isn't referenced kptr\n", kptr_off); |
6622 | return -EACCES; | |
6623 | } | |
aa3496ac | 6624 | meta->kptr_field = kptr_field; |
c0a5a21c KKD |
6625 | return 0; |
6626 | } | |
6627 | ||
27060531 KKD |
6628 | /* There are two register types representing a bpf_dynptr, one is PTR_TO_STACK |
6629 | * which points to a stack slot, and the other is CONST_PTR_TO_DYNPTR. | |
6630 | * | |
6631 | * In both cases we deal with the first 8 bytes, but need to mark the next 8 | |
6632 | * bytes as STACK_DYNPTR in case of PTR_TO_STACK. In case of | |
6633 | * CONST_PTR_TO_DYNPTR, we are guaranteed to get the beginning of the object. | |
6634 | * | |
6635 | * Mutability of bpf_dynptr is at two levels, one is at the level of struct | |
6636 | * bpf_dynptr itself, i.e. whether the helper is receiving a pointer to struct | |
6637 | * bpf_dynptr or pointer to const struct bpf_dynptr. In the former case, it can | |
6638 | * mutate the view of the dynptr and also possibly destroy it. In the latter | |
6639 | * case, it cannot mutate the bpf_dynptr itself but it can still mutate the | |
6640 | * memory that dynptr points to. | |
6641 | * | |
6642 | * The verifier will keep track both levels of mutation (bpf_dynptr's in | |
6643 | * reg->type and the memory's in reg->dynptr.type), but there is no support for | |
6644 | * readonly dynptr view yet, hence only the first case is tracked and checked. | |
6645 | * | |
6646 | * This is consistent with how C applies the const modifier to a struct object, | |
6647 | * where the pointer itself inside bpf_dynptr becomes const but not what it | |
6648 | * points to. | |
6649 | * | |
6650 | * Helpers which do not mutate the bpf_dynptr set MEM_RDONLY in their argument | |
6651 | * type, and declare it as 'const struct bpf_dynptr *' in their prototype. | |
6652 | */ | |
1d18feb2 JK |
6653 | static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx, |
6654 | enum bpf_arg_type arg_type) | |
6b75bd3d KKD |
6655 | { |
6656 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
1d18feb2 | 6657 | int err; |
6b75bd3d | 6658 | |
27060531 KKD |
6659 | /* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an |
6660 | * ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*): | |
6661 | */ | |
6662 | if ((arg_type & (MEM_UNINIT | MEM_RDONLY)) == (MEM_UNINIT | MEM_RDONLY)) { | |
6663 | verbose(env, "verifier internal error: misconfigured dynptr helper type flags\n"); | |
6664 | return -EFAULT; | |
6665 | } | |
79168a66 | 6666 | |
27060531 KKD |
6667 | /* MEM_UNINIT - Points to memory that is an appropriate candidate for |
6668 | * constructing a mutable bpf_dynptr object. | |
6669 | * | |
6670 | * Currently, this is only possible with PTR_TO_STACK | |
6671 | * pointing to a region of at least 16 bytes which doesn't | |
6672 | * contain an existing bpf_dynptr. | |
6673 | * | |
6674 | * MEM_RDONLY - Points to a initialized bpf_dynptr that will not be | |
6675 | * mutated or destroyed. However, the memory it points to | |
6676 | * may be mutated. | |
6677 | * | |
6678 | * None - Points to a initialized dynptr that can be mutated and | |
6679 | * destroyed, including mutation of the memory it points | |
6680 | * to. | |
6b75bd3d | 6681 | */ |
6b75bd3d | 6682 | if (arg_type & MEM_UNINIT) { |
1d18feb2 JK |
6683 | int i; |
6684 | ||
7e0dac28 | 6685 | if (!is_dynptr_reg_valid_uninit(env, reg)) { |
6b75bd3d KKD |
6686 | verbose(env, "Dynptr has to be an uninitialized dynptr\n"); |
6687 | return -EINVAL; | |
6688 | } | |
6689 | ||
1d18feb2 JK |
6690 | /* we write BPF_DW bits (8 bytes) at a time */ |
6691 | for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) { | |
6692 | err = check_mem_access(env, insn_idx, regno, | |
6693 | i, BPF_DW, BPF_WRITE, -1, false); | |
6694 | if (err) | |
6695 | return err; | |
6b75bd3d KKD |
6696 | } |
6697 | ||
1d18feb2 | 6698 | err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx); |
27060531 KKD |
6699 | } else /* MEM_RDONLY and None case from above */ { |
6700 | /* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */ | |
6701 | if (reg->type == CONST_PTR_TO_DYNPTR && !(arg_type & MEM_RDONLY)) { | |
6702 | verbose(env, "cannot pass pointer to const bpf_dynptr, the helper mutates it\n"); | |
6703 | return -EINVAL; | |
6704 | } | |
6705 | ||
7e0dac28 | 6706 | if (!is_dynptr_reg_valid_init(env, reg)) { |
6b75bd3d KKD |
6707 | verbose(env, |
6708 | "Expected an initialized dynptr as arg #%d\n", | |
6709 | regno); | |
6710 | return -EINVAL; | |
6711 | } | |
6712 | ||
27060531 KKD |
6713 | /* Fold modifiers (in this case, MEM_RDONLY) when checking expected type */ |
6714 | if (!is_dynptr_type_expected(env, reg, arg_type & ~MEM_RDONLY)) { | |
6b75bd3d KKD |
6715 | verbose(env, |
6716 | "Expected a dynptr of type %s as arg #%d\n", | |
d54e0f6c | 6717 | dynptr_type_str(arg_to_dynptr_type(arg_type)), regno); |
6b75bd3d KKD |
6718 | return -EINVAL; |
6719 | } | |
d6fefa11 KKD |
6720 | |
6721 | err = mark_dynptr_read(env, reg); | |
6b75bd3d | 6722 | } |
1d18feb2 | 6723 | return err; |
6b75bd3d KKD |
6724 | } |
6725 | ||
06accc87 AN |
6726 | static u32 iter_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int spi) |
6727 | { | |
6728 | struct bpf_func_state *state = func(env, reg); | |
6729 | ||
6730 | return state->stack[spi].spilled_ptr.ref_obj_id; | |
6731 | } | |
6732 | ||
6733 | static bool is_iter_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6734 | { | |
6735 | return meta->kfunc_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY); | |
6736 | } | |
6737 | ||
6738 | static bool is_iter_new_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6739 | { | |
6740 | return meta->kfunc_flags & KF_ITER_NEW; | |
6741 | } | |
6742 | ||
6743 | static bool is_iter_next_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6744 | { | |
6745 | return meta->kfunc_flags & KF_ITER_NEXT; | |
6746 | } | |
6747 | ||
6748 | static bool is_iter_destroy_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6749 | { | |
6750 | return meta->kfunc_flags & KF_ITER_DESTROY; | |
6751 | } | |
6752 | ||
6753 | static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg) | |
6754 | { | |
6755 | /* btf_check_iter_kfuncs() guarantees that first argument of any iter | |
6756 | * kfunc is iter state pointer | |
6757 | */ | |
6758 | return arg == 0 && is_iter_kfunc(meta); | |
6759 | } | |
6760 | ||
6761 | static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idx, | |
6762 | struct bpf_kfunc_call_arg_meta *meta) | |
6763 | { | |
6764 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6765 | const struct btf_type *t; | |
6766 | const struct btf_param *arg; | |
6767 | int spi, err, i, nr_slots; | |
6768 | u32 btf_id; | |
6769 | ||
6770 | /* btf_check_iter_kfuncs() ensures we don't need to validate anything here */ | |
6771 | arg = &btf_params(meta->func_proto)[0]; | |
6772 | t = btf_type_skip_modifiers(meta->btf, arg->type, NULL); /* PTR */ | |
6773 | t = btf_type_skip_modifiers(meta->btf, t->type, &btf_id); /* STRUCT */ | |
6774 | nr_slots = t->size / BPF_REG_SIZE; | |
6775 | ||
6776 | spi = iter_get_spi(env, reg, nr_slots); | |
6777 | if (spi < 0 && spi != -ERANGE) | |
6778 | return spi; | |
6779 | ||
6780 | meta->iter.spi = spi; | |
6781 | meta->iter.frameno = reg->frameno; | |
6782 | ||
6783 | if (is_iter_new_kfunc(meta)) { | |
6784 | /* bpf_iter_<type>_new() expects pointer to uninit iter state */ | |
6785 | if (!is_iter_reg_valid_uninit(env, reg, nr_slots)) { | |
6786 | verbose(env, "expected uninitialized iter_%s as arg #%d\n", | |
6787 | iter_type_str(meta->btf, btf_id), regno); | |
6788 | return -EINVAL; | |
6789 | } | |
6790 | ||
6791 | for (i = 0; i < nr_slots * 8; i += BPF_REG_SIZE) { | |
6792 | err = check_mem_access(env, insn_idx, regno, | |
6793 | i, BPF_DW, BPF_WRITE, -1, false); | |
6794 | if (err) | |
6795 | return err; | |
6796 | } | |
6797 | ||
6798 | err = mark_stack_slots_iter(env, reg, insn_idx, meta->btf, btf_id, nr_slots); | |
6799 | if (err) | |
6800 | return err; | |
6801 | } else { | |
6802 | /* iter_next() or iter_destroy() expect initialized iter state*/ | |
6803 | if (!is_iter_reg_valid_init(env, reg, meta->btf, btf_id, nr_slots)) { | |
6804 | verbose(env, "expected an initialized iter_%s as arg #%d\n", | |
6805 | iter_type_str(meta->btf, btf_id), regno); | |
6806 | return -EINVAL; | |
6807 | } | |
6808 | ||
6809 | err = mark_iter_read(env, reg, spi, nr_slots); | |
6810 | if (err) | |
6811 | return err; | |
6812 | ||
6813 | meta->ref_obj_id = iter_ref_obj_id(env, reg, spi); | |
6814 | ||
6815 | if (is_iter_destroy_kfunc(meta)) { | |
6816 | err = unmark_stack_slots_iter(env, reg, nr_slots); | |
6817 | if (err) | |
6818 | return err; | |
6819 | } | |
6820 | } | |
6821 | ||
6822 | return 0; | |
6823 | } | |
6824 | ||
6825 | /* process_iter_next_call() is called when verifier gets to iterator's next | |
6826 | * "method" (e.g., bpf_iter_num_next() for numbers iterator) call. We'll refer | |
6827 | * to it as just "iter_next()" in comments below. | |
6828 | * | |
6829 | * BPF verifier relies on a crucial contract for any iter_next() | |
6830 | * implementation: it should *eventually* return NULL, and once that happens | |
6831 | * it should keep returning NULL. That is, once iterator exhausts elements to | |
6832 | * iterate, it should never reset or spuriously return new elements. | |
6833 | * | |
6834 | * With the assumption of such contract, process_iter_next_call() simulates | |
6835 | * a fork in the verifier state to validate loop logic correctness and safety | |
6836 | * without having to simulate infinite amount of iterations. | |
6837 | * | |
6838 | * In current state, we first assume that iter_next() returned NULL and | |
6839 | * iterator state is set to DRAINED (BPF_ITER_STATE_DRAINED). In such | |
6840 | * conditions we should not form an infinite loop and should eventually reach | |
6841 | * exit. | |
6842 | * | |
6843 | * Besides that, we also fork current state and enqueue it for later | |
6844 | * verification. In a forked state we keep iterator state as ACTIVE | |
6845 | * (BPF_ITER_STATE_ACTIVE) and assume non-NULL return from iter_next(). We | |
6846 | * also bump iteration depth to prevent erroneous infinite loop detection | |
6847 | * later on (see iter_active_depths_differ() comment for details). In this | |
6848 | * state we assume that we'll eventually loop back to another iter_next() | |
6849 | * calls (it could be in exactly same location or in some other instruction, | |
6850 | * it doesn't matter, we don't make any unnecessary assumptions about this, | |
6851 | * everything revolves around iterator state in a stack slot, not which | |
6852 | * instruction is calling iter_next()). When that happens, we either will come | |
6853 | * to iter_next() with equivalent state and can conclude that next iteration | |
6854 | * will proceed in exactly the same way as we just verified, so it's safe to | |
6855 | * assume that loop converges. If not, we'll go on another iteration | |
6856 | * simulation with a different input state, until all possible starting states | |
6857 | * are validated or we reach maximum number of instructions limit. | |
6858 | * | |
6859 | * This way, we will either exhaustively discover all possible input states | |
6860 | * that iterator loop can start with and eventually will converge, or we'll | |
6861 | * effectively regress into bounded loop simulation logic and either reach | |
6862 | * maximum number of instructions if loop is not provably convergent, or there | |
6863 | * is some statically known limit on number of iterations (e.g., if there is | |
6864 | * an explicit `if n > 100 then break;` statement somewhere in the loop). | |
6865 | * | |
6866 | * One very subtle but very important aspect is that we *always* simulate NULL | |
6867 | * condition first (as the current state) before we simulate non-NULL case. | |
6868 | * This has to do with intricacies of scalar precision tracking. By simulating | |
6869 | * "exit condition" of iter_next() returning NULL first, we make sure all the | |
6870 | * relevant precision marks *that will be set **after** we exit iterator loop* | |
6871 | * are propagated backwards to common parent state of NULL and non-NULL | |
6872 | * branches. Thanks to that, state equivalence checks done later in forked | |
6873 | * state, when reaching iter_next() for ACTIVE iterator, can assume that | |
6874 | * precision marks are finalized and won't change. Because simulating another | |
6875 | * ACTIVE iterator iteration won't change them (because given same input | |
6876 | * states we'll end up with exactly same output states which we are currently | |
6877 | * comparing; and verification after the loop already propagated back what | |
6878 | * needs to be **additionally** tracked as precise). It's subtle, grok | |
6879 | * precision tracking for more intuitive understanding. | |
6880 | */ | |
6881 | static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, | |
6882 | struct bpf_kfunc_call_arg_meta *meta) | |
6883 | { | |
6884 | struct bpf_verifier_state *cur_st = env->cur_state, *queued_st; | |
6885 | struct bpf_func_state *cur_fr = cur_st->frame[cur_st->curframe], *queued_fr; | |
6886 | struct bpf_reg_state *cur_iter, *queued_iter; | |
6887 | int iter_frameno = meta->iter.frameno; | |
6888 | int iter_spi = meta->iter.spi; | |
6889 | ||
6890 | BTF_TYPE_EMIT(struct bpf_iter); | |
6891 | ||
6892 | cur_iter = &env->cur_state->frame[iter_frameno]->stack[iter_spi].spilled_ptr; | |
6893 | ||
6894 | if (cur_iter->iter.state != BPF_ITER_STATE_ACTIVE && | |
6895 | cur_iter->iter.state != BPF_ITER_STATE_DRAINED) { | |
6896 | verbose(env, "verifier internal error: unexpected iterator state %d (%s)\n", | |
6897 | cur_iter->iter.state, iter_state_str(cur_iter->iter.state)); | |
6898 | return -EFAULT; | |
6899 | } | |
6900 | ||
6901 | if (cur_iter->iter.state == BPF_ITER_STATE_ACTIVE) { | |
6902 | /* branch out active iter state */ | |
6903 | queued_st = push_stack(env, insn_idx + 1, insn_idx, false); | |
6904 | if (!queued_st) | |
6905 | return -ENOMEM; | |
6906 | ||
6907 | queued_iter = &queued_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr; | |
6908 | queued_iter->iter.state = BPF_ITER_STATE_ACTIVE; | |
6909 | queued_iter->iter.depth++; | |
6910 | ||
6911 | queued_fr = queued_st->frame[queued_st->curframe]; | |
6912 | mark_ptr_not_null_reg(&queued_fr->regs[BPF_REG_0]); | |
6913 | } | |
6914 | ||
6915 | /* switch to DRAINED state, but keep the depth unchanged */ | |
6916 | /* mark current iter state as drained and assume returned NULL */ | |
6917 | cur_iter->iter.state = BPF_ITER_STATE_DRAINED; | |
6918 | __mark_reg_const_zero(&cur_fr->regs[BPF_REG_0]); | |
6919 | ||
6920 | return 0; | |
6921 | } | |
6922 | ||
90133415 DB |
6923 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
6924 | { | |
6925 | return type == ARG_CONST_SIZE || | |
6926 | type == ARG_CONST_SIZE_OR_ZERO; | |
6927 | } | |
6928 | ||
8f14852e KKD |
6929 | static bool arg_type_is_release(enum bpf_arg_type type) |
6930 | { | |
6931 | return type & OBJ_RELEASE; | |
6932 | } | |
6933 | ||
97e03f52 JK |
6934 | static bool arg_type_is_dynptr(enum bpf_arg_type type) |
6935 | { | |
6936 | return base_type(type) == ARG_PTR_TO_DYNPTR; | |
6937 | } | |
6938 | ||
57c3bb72 AI |
6939 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
6940 | { | |
6941 | if (type == ARG_PTR_TO_INT) | |
6942 | return sizeof(u32); | |
6943 | else if (type == ARG_PTR_TO_LONG) | |
6944 | return sizeof(u64); | |
6945 | ||
6946 | return -EINVAL; | |
6947 | } | |
6948 | ||
912f442c LB |
6949 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
6950 | const struct bpf_call_arg_meta *meta, | |
6951 | enum bpf_arg_type *arg_type) | |
6952 | { | |
6953 | if (!meta->map_ptr) { | |
6954 | /* kernel subsystem misconfigured verifier */ | |
6955 | verbose(env, "invalid map_ptr to access map->type\n"); | |
6956 | return -EACCES; | |
6957 | } | |
6958 | ||
6959 | switch (meta->map_ptr->map_type) { | |
6960 | case BPF_MAP_TYPE_SOCKMAP: | |
6961 | case BPF_MAP_TYPE_SOCKHASH: | |
6962 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 6963 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
6964 | } else { |
6965 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
6966 | return -EINVAL; | |
6967 | } | |
6968 | break; | |
9330986c JK |
6969 | case BPF_MAP_TYPE_BLOOM_FILTER: |
6970 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
6971 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
6972 | break; | |
912f442c LB |
6973 | default: |
6974 | break; | |
6975 | } | |
6976 | return 0; | |
6977 | } | |
6978 | ||
f79e7ea5 LB |
6979 | struct bpf_reg_types { |
6980 | const enum bpf_reg_type types[10]; | |
1df8f55a | 6981 | u32 *btf_id; |
f79e7ea5 LB |
6982 | }; |
6983 | ||
f79e7ea5 LB |
6984 | static const struct bpf_reg_types sock_types = { |
6985 | .types = { | |
6986 | PTR_TO_SOCK_COMMON, | |
6987 | PTR_TO_SOCKET, | |
6988 | PTR_TO_TCP_SOCK, | |
6989 | PTR_TO_XDP_SOCK, | |
6990 | }, | |
6991 | }; | |
6992 | ||
49a2a4d4 | 6993 | #ifdef CONFIG_NET |
1df8f55a MKL |
6994 | static const struct bpf_reg_types btf_id_sock_common_types = { |
6995 | .types = { | |
6996 | PTR_TO_SOCK_COMMON, | |
6997 | PTR_TO_SOCKET, | |
6998 | PTR_TO_TCP_SOCK, | |
6999 | PTR_TO_XDP_SOCK, | |
7000 | PTR_TO_BTF_ID, | |
3f00c523 | 7001 | PTR_TO_BTF_ID | PTR_TRUSTED, |
1df8f55a MKL |
7002 | }, |
7003 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
7004 | }; | |
49a2a4d4 | 7005 | #endif |
1df8f55a | 7006 | |
f79e7ea5 LB |
7007 | static const struct bpf_reg_types mem_types = { |
7008 | .types = { | |
7009 | PTR_TO_STACK, | |
7010 | PTR_TO_PACKET, | |
7011 | PTR_TO_PACKET_META, | |
69c087ba | 7012 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
7013 | PTR_TO_MAP_VALUE, |
7014 | PTR_TO_MEM, | |
894f2a8b | 7015 | PTR_TO_MEM | MEM_RINGBUF, |
20b2aff4 | 7016 | PTR_TO_BUF, |
f79e7ea5 LB |
7017 | }, |
7018 | }; | |
7019 | ||
7020 | static const struct bpf_reg_types int_ptr_types = { | |
7021 | .types = { | |
7022 | PTR_TO_STACK, | |
7023 | PTR_TO_PACKET, | |
7024 | PTR_TO_PACKET_META, | |
69c087ba | 7025 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
7026 | PTR_TO_MAP_VALUE, |
7027 | }, | |
7028 | }; | |
7029 | ||
4e814da0 KKD |
7030 | static const struct bpf_reg_types spin_lock_types = { |
7031 | .types = { | |
7032 | PTR_TO_MAP_VALUE, | |
7033 | PTR_TO_BTF_ID | MEM_ALLOC, | |
7034 | } | |
7035 | }; | |
7036 | ||
f79e7ea5 LB |
7037 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
7038 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
7039 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
894f2a8b | 7040 | static const struct bpf_reg_types ringbuf_mem_types = { .types = { PTR_TO_MEM | MEM_RINGBUF } }; |
f79e7ea5 | 7041 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
3f00c523 DV |
7042 | static const struct bpf_reg_types btf_ptr_types = { |
7043 | .types = { | |
7044 | PTR_TO_BTF_ID, | |
7045 | PTR_TO_BTF_ID | PTR_TRUSTED, | |
fca1aa75 | 7046 | PTR_TO_BTF_ID | MEM_RCU, |
3f00c523 DV |
7047 | }, |
7048 | }; | |
7049 | static const struct bpf_reg_types percpu_btf_ptr_types = { | |
7050 | .types = { | |
7051 | PTR_TO_BTF_ID | MEM_PERCPU, | |
7052 | PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED, | |
7053 | } | |
7054 | }; | |
69c087ba YS |
7055 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
7056 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 7057 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 7058 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
c0a5a21c | 7059 | static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
20571567 DV |
7060 | static const struct bpf_reg_types dynptr_types = { |
7061 | .types = { | |
7062 | PTR_TO_STACK, | |
27060531 | 7063 | CONST_PTR_TO_DYNPTR, |
20571567 DV |
7064 | } |
7065 | }; | |
f79e7ea5 | 7066 | |
0789e13b | 7067 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
d1673304 DM |
7068 | [ARG_PTR_TO_MAP_KEY] = &mem_types, |
7069 | [ARG_PTR_TO_MAP_VALUE] = &mem_types, | |
f79e7ea5 LB |
7070 | [ARG_CONST_SIZE] = &scalar_types, |
7071 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
7072 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
7073 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
7074 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 7075 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 7076 | #ifdef CONFIG_NET |
1df8f55a | 7077 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 7078 | #endif |
f79e7ea5 | 7079 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
7080 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
7081 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
7082 | [ARG_PTR_TO_MEM] = &mem_types, | |
894f2a8b | 7083 | [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, |
f79e7ea5 LB |
7084 | [ARG_PTR_TO_INT] = &int_ptr_types, |
7085 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 7086 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 7087 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 7088 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 7089 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 7090 | [ARG_PTR_TO_TIMER] = &timer_types, |
c0a5a21c | 7091 | [ARG_PTR_TO_KPTR] = &kptr_types, |
20571567 | 7092 | [ARG_PTR_TO_DYNPTR] = &dynptr_types, |
f79e7ea5 LB |
7093 | }; |
7094 | ||
7095 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 | 7096 | enum bpf_arg_type arg_type, |
c0a5a21c KKD |
7097 | const u32 *arg_btf_id, |
7098 | struct bpf_call_arg_meta *meta) | |
f79e7ea5 LB |
7099 | { |
7100 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
7101 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 7102 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
7103 | int i, j; |
7104 | ||
48946bd6 | 7105 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
7106 | if (!compatible) { |
7107 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
7108 | return -EFAULT; | |
7109 | } | |
7110 | ||
216e3cd2 HL |
7111 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
7112 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY | |
7113 | * | |
7114 | * Same for MAYBE_NULL: | |
7115 | * | |
7116 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, | |
7117 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL | |
7118 | * | |
7119 | * Therefore we fold these flags depending on the arg_type before comparison. | |
7120 | */ | |
7121 | if (arg_type & MEM_RDONLY) | |
7122 | type &= ~MEM_RDONLY; | |
7123 | if (arg_type & PTR_MAYBE_NULL) | |
7124 | type &= ~PTR_MAYBE_NULL; | |
7125 | ||
f79e7ea5 LB |
7126 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
7127 | expected = compatible->types[i]; | |
7128 | if (expected == NOT_INIT) | |
7129 | break; | |
7130 | ||
7131 | if (type == expected) | |
a968d5e2 | 7132 | goto found; |
f79e7ea5 LB |
7133 | } |
7134 | ||
216e3cd2 | 7135 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
f79e7ea5 | 7136 | for (j = 0; j + 1 < i; j++) |
c25b2ae1 HL |
7137 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
7138 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); | |
f79e7ea5 | 7139 | return -EACCES; |
a968d5e2 MKL |
7140 | |
7141 | found: | |
da03e43a KKD |
7142 | if (base_type(reg->type) != PTR_TO_BTF_ID) |
7143 | return 0; | |
7144 | ||
7145 | switch ((int)reg->type) { | |
7146 | case PTR_TO_BTF_ID: | |
7147 | case PTR_TO_BTF_ID | PTR_TRUSTED: | |
7148 | case PTR_TO_BTF_ID | MEM_RCU: | |
7149 | { | |
2ab3b380 KKD |
7150 | /* For bpf_sk_release, it needs to match against first member |
7151 | * 'struct sock_common', hence make an exception for it. This | |
7152 | * allows bpf_sk_release to work for multiple socket types. | |
7153 | */ | |
7154 | bool strict_type_match = arg_type_is_release(arg_type) && | |
7155 | meta->func_id != BPF_FUNC_sk_release; | |
7156 | ||
1df8f55a MKL |
7157 | if (!arg_btf_id) { |
7158 | if (!compatible->btf_id) { | |
7159 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
7160 | return -EFAULT; | |
7161 | } | |
7162 | arg_btf_id = compatible->btf_id; | |
7163 | } | |
7164 | ||
c0a5a21c | 7165 | if (meta->func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac | 7166 | if (map_kptr_match_type(env, meta->kptr_field, reg, regno)) |
c0a5a21c | 7167 | return -EACCES; |
47e34cb7 DM |
7168 | } else { |
7169 | if (arg_btf_id == BPF_PTR_POISON) { | |
7170 | verbose(env, "verifier internal error:"); | |
7171 | verbose(env, "R%d has non-overwritten BPF_PTR_POISON type\n", | |
7172 | regno); | |
7173 | return -EACCES; | |
7174 | } | |
7175 | ||
7176 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
7177 | btf_vmlinux, *arg_btf_id, | |
7178 | strict_type_match)) { | |
7179 | verbose(env, "R%d is of type %s but %s is expected\n", | |
b32a5dae DM |
7180 | regno, btf_type_name(reg->btf, reg->btf_id), |
7181 | btf_type_name(btf_vmlinux, *arg_btf_id)); | |
47e34cb7 DM |
7182 | return -EACCES; |
7183 | } | |
a968d5e2 | 7184 | } |
da03e43a KKD |
7185 | break; |
7186 | } | |
7187 | case PTR_TO_BTF_ID | MEM_ALLOC: | |
4e814da0 KKD |
7188 | if (meta->func_id != BPF_FUNC_spin_lock && meta->func_id != BPF_FUNC_spin_unlock) { |
7189 | verbose(env, "verifier internal error: unimplemented handling of MEM_ALLOC\n"); | |
7190 | return -EFAULT; | |
7191 | } | |
da03e43a KKD |
7192 | /* Handled by helper specific checks */ |
7193 | break; | |
7194 | case PTR_TO_BTF_ID | MEM_PERCPU: | |
7195 | case PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED: | |
7196 | /* Handled by helper specific checks */ | |
7197 | break; | |
7198 | default: | |
7199 | verbose(env, "verifier internal error: invalid PTR_TO_BTF_ID register for type match\n"); | |
7200 | return -EFAULT; | |
a968d5e2 | 7201 | } |
a968d5e2 | 7202 | return 0; |
f79e7ea5 LB |
7203 | } |
7204 | ||
6a3cd331 DM |
7205 | static struct btf_field * |
7206 | reg_find_field_offset(const struct bpf_reg_state *reg, s32 off, u32 fields) | |
7207 | { | |
7208 | struct btf_field *field; | |
7209 | struct btf_record *rec; | |
7210 | ||
7211 | rec = reg_btf_record(reg); | |
7212 | if (!rec) | |
7213 | return NULL; | |
7214 | ||
7215 | field = btf_record_find(rec, off, fields); | |
7216 | if (!field) | |
7217 | return NULL; | |
7218 | ||
7219 | return field; | |
7220 | } | |
7221 | ||
25b35dd2 KKD |
7222 | int check_func_arg_reg_off(struct bpf_verifier_env *env, |
7223 | const struct bpf_reg_state *reg, int regno, | |
8f14852e | 7224 | enum bpf_arg_type arg_type) |
25b35dd2 | 7225 | { |
184c9bdb | 7226 | u32 type = reg->type; |
25b35dd2 | 7227 | |
184c9bdb KKD |
7228 | /* When referenced register is passed to release function, its fixed |
7229 | * offset must be 0. | |
7230 | * | |
7231 | * We will check arg_type_is_release reg has ref_obj_id when storing | |
7232 | * meta->release_regno. | |
7233 | */ | |
7234 | if (arg_type_is_release(arg_type)) { | |
7235 | /* ARG_PTR_TO_DYNPTR with OBJ_RELEASE is a bit special, as it | |
7236 | * may not directly point to the object being released, but to | |
7237 | * dynptr pointing to such object, which might be at some offset | |
7238 | * on the stack. In that case, we simply to fallback to the | |
7239 | * default handling. | |
7240 | */ | |
7241 | if (arg_type_is_dynptr(arg_type) && type == PTR_TO_STACK) | |
7242 | return 0; | |
6a3cd331 DM |
7243 | |
7244 | if ((type_is_ptr_alloc_obj(type) || type_is_non_owning_ref(type)) && reg->off) { | |
7245 | if (reg_find_field_offset(reg, reg->off, BPF_GRAPH_NODE_OR_ROOT)) | |
7246 | return __check_ptr_off_reg(env, reg, regno, true); | |
7247 | ||
7248 | verbose(env, "R%d must have zero offset when passed to release func\n", | |
7249 | regno); | |
7250 | verbose(env, "No graph node or root found at R%d type:%s off:%d\n", regno, | |
b32a5dae | 7251 | btf_type_name(reg->btf, reg->btf_id), reg->off); |
6a3cd331 DM |
7252 | return -EINVAL; |
7253 | } | |
7254 | ||
184c9bdb KKD |
7255 | /* Doing check_ptr_off_reg check for the offset will catch this |
7256 | * because fixed_off_ok is false, but checking here allows us | |
7257 | * to give the user a better error message. | |
7258 | */ | |
7259 | if (reg->off) { | |
7260 | verbose(env, "R%d must have zero offset when passed to release func or trusted arg to kfunc\n", | |
7261 | regno); | |
7262 | return -EINVAL; | |
7263 | } | |
7264 | return __check_ptr_off_reg(env, reg, regno, false); | |
7265 | } | |
7266 | ||
7267 | switch (type) { | |
7268 | /* Pointer types where both fixed and variable offset is explicitly allowed: */ | |
97e03f52 | 7269 | case PTR_TO_STACK: |
25b35dd2 KKD |
7270 | case PTR_TO_PACKET: |
7271 | case PTR_TO_PACKET_META: | |
7272 | case PTR_TO_MAP_KEY: | |
7273 | case PTR_TO_MAP_VALUE: | |
7274 | case PTR_TO_MEM: | |
7275 | case PTR_TO_MEM | MEM_RDONLY: | |
894f2a8b | 7276 | case PTR_TO_MEM | MEM_RINGBUF: |
25b35dd2 KKD |
7277 | case PTR_TO_BUF: |
7278 | case PTR_TO_BUF | MEM_RDONLY: | |
97e03f52 | 7279 | case SCALAR_VALUE: |
184c9bdb | 7280 | return 0; |
25b35dd2 KKD |
7281 | /* All the rest must be rejected, except PTR_TO_BTF_ID which allows |
7282 | * fixed offset. | |
7283 | */ | |
7284 | case PTR_TO_BTF_ID: | |
282de143 | 7285 | case PTR_TO_BTF_ID | MEM_ALLOC: |
3f00c523 | 7286 | case PTR_TO_BTF_ID | PTR_TRUSTED: |
fca1aa75 | 7287 | case PTR_TO_BTF_ID | MEM_RCU: |
6a3cd331 | 7288 | case PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF: |
24d5bb80 | 7289 | /* When referenced PTR_TO_BTF_ID is passed to release function, |
184c9bdb KKD |
7290 | * its fixed offset must be 0. In the other cases, fixed offset |
7291 | * can be non-zero. This was already checked above. So pass | |
7292 | * fixed_off_ok as true to allow fixed offset for all other | |
7293 | * cases. var_off always must be 0 for PTR_TO_BTF_ID, hence we | |
7294 | * still need to do checks instead of returning. | |
24d5bb80 | 7295 | */ |
184c9bdb | 7296 | return __check_ptr_off_reg(env, reg, regno, true); |
25b35dd2 | 7297 | default: |
184c9bdb | 7298 | return __check_ptr_off_reg(env, reg, regno, false); |
25b35dd2 | 7299 | } |
25b35dd2 KKD |
7300 | } |
7301 | ||
485ec51e JK |
7302 | static struct bpf_reg_state *get_dynptr_arg_reg(struct bpf_verifier_env *env, |
7303 | const struct bpf_func_proto *fn, | |
7304 | struct bpf_reg_state *regs) | |
7305 | { | |
7306 | struct bpf_reg_state *state = NULL; | |
7307 | int i; | |
7308 | ||
7309 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) | |
7310 | if (arg_type_is_dynptr(fn->arg_type[i])) { | |
7311 | if (state) { | |
7312 | verbose(env, "verifier internal error: multiple dynptr args\n"); | |
7313 | return NULL; | |
7314 | } | |
7315 | state = ®s[BPF_REG_1 + i]; | |
7316 | } | |
7317 | ||
7318 | if (!state) | |
7319 | verbose(env, "verifier internal error: no dynptr arg found\n"); | |
7320 | ||
7321 | return state; | |
7322 | } | |
7323 | ||
f8064ab9 | 7324 | static int dynptr_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
34d4ef57 JK |
7325 | { |
7326 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7327 | int spi; |
34d4ef57 | 7328 | |
27060531 | 7329 | if (reg->type == CONST_PTR_TO_DYNPTR) |
f8064ab9 KKD |
7330 | return reg->id; |
7331 | spi = dynptr_get_spi(env, reg); | |
7332 | if (spi < 0) | |
7333 | return spi; | |
7334 | return state->stack[spi].spilled_ptr.id; | |
7335 | } | |
7336 | ||
79168a66 | 7337 | static int dynptr_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
34d4ef57 JK |
7338 | { |
7339 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7340 | int spi; |
27060531 | 7341 | |
27060531 KKD |
7342 | if (reg->type == CONST_PTR_TO_DYNPTR) |
7343 | return reg->ref_obj_id; | |
79168a66 KKD |
7344 | spi = dynptr_get_spi(env, reg); |
7345 | if (spi < 0) | |
7346 | return spi; | |
27060531 | 7347 | return state->stack[spi].spilled_ptr.ref_obj_id; |
34d4ef57 JK |
7348 | } |
7349 | ||
b5964b96 JK |
7350 | static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env, |
7351 | struct bpf_reg_state *reg) | |
7352 | { | |
7353 | struct bpf_func_state *state = func(env, reg); | |
7354 | int spi; | |
7355 | ||
7356 | if (reg->type == CONST_PTR_TO_DYNPTR) | |
7357 | return reg->dynptr.type; | |
7358 | ||
7359 | spi = __get_spi(reg->off); | |
7360 | if (spi < 0) { | |
7361 | verbose(env, "verifier internal error: invalid spi when querying dynptr type\n"); | |
7362 | return BPF_DYNPTR_TYPE_INVALID; | |
7363 | } | |
7364 | ||
7365 | return state->stack[spi].spilled_ptr.dynptr.type; | |
7366 | } | |
7367 | ||
af7ec138 YS |
7368 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
7369 | struct bpf_call_arg_meta *meta, | |
1d18feb2 JK |
7370 | const struct bpf_func_proto *fn, |
7371 | int insn_idx) | |
17a52670 | 7372 | { |
af7ec138 | 7373 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 7374 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 7375 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 7376 | enum bpf_reg_type type = reg->type; |
508362ac | 7377 | u32 *arg_btf_id = NULL; |
17a52670 AS |
7378 | int err = 0; |
7379 | ||
80f1d68c | 7380 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
7381 | return 0; |
7382 | ||
dc503a8a EC |
7383 | err = check_reg_arg(env, regno, SRC_OP); |
7384 | if (err) | |
7385 | return err; | |
17a52670 | 7386 | |
1be7f75d AS |
7387 | if (arg_type == ARG_ANYTHING) { |
7388 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
7389 | verbose(env, "R%d leaks addr into helper function\n", |
7390 | regno); | |
1be7f75d AS |
7391 | return -EACCES; |
7392 | } | |
80f1d68c | 7393 | return 0; |
1be7f75d | 7394 | } |
80f1d68c | 7395 | |
de8f3a83 | 7396 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 7397 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 7398 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
7399 | return -EACCES; |
7400 | } | |
7401 | ||
16d1e00c | 7402 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE) { |
912f442c LB |
7403 | err = resolve_map_arg_type(env, meta, &arg_type); |
7404 | if (err) | |
7405 | return err; | |
7406 | } | |
7407 | ||
48946bd6 | 7408 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
7409 | /* A NULL register has a SCALAR_VALUE type, so skip |
7410 | * type checking. | |
7411 | */ | |
7412 | goto skip_type_check; | |
7413 | ||
508362ac | 7414 | /* arg_btf_id and arg_size are in a union. */ |
4e814da0 KKD |
7415 | if (base_type(arg_type) == ARG_PTR_TO_BTF_ID || |
7416 | base_type(arg_type) == ARG_PTR_TO_SPIN_LOCK) | |
508362ac MM |
7417 | arg_btf_id = fn->arg_btf_id[arg]; |
7418 | ||
7419 | err = check_reg_type(env, regno, arg_type, arg_btf_id, meta); | |
f79e7ea5 LB |
7420 | if (err) |
7421 | return err; | |
7422 | ||
8f14852e | 7423 | err = check_func_arg_reg_off(env, reg, regno, arg_type); |
25b35dd2 KKD |
7424 | if (err) |
7425 | return err; | |
d7b9454a | 7426 | |
fd1b0d60 | 7427 | skip_type_check: |
8f14852e | 7428 | if (arg_type_is_release(arg_type)) { |
bc34dee6 JK |
7429 | if (arg_type_is_dynptr(arg_type)) { |
7430 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7431 | int spi; |
bc34dee6 | 7432 | |
27060531 KKD |
7433 | /* Only dynptr created on stack can be released, thus |
7434 | * the get_spi and stack state checks for spilled_ptr | |
7435 | * should only be done before process_dynptr_func for | |
7436 | * PTR_TO_STACK. | |
7437 | */ | |
7438 | if (reg->type == PTR_TO_STACK) { | |
79168a66 | 7439 | spi = dynptr_get_spi(env, reg); |
f5b625e5 | 7440 | if (spi < 0 || !state->stack[spi].spilled_ptr.ref_obj_id) { |
27060531 KKD |
7441 | verbose(env, "arg %d is an unacquired reference\n", regno); |
7442 | return -EINVAL; | |
7443 | } | |
7444 | } else { | |
7445 | verbose(env, "cannot release unowned const bpf_dynptr\n"); | |
bc34dee6 JK |
7446 | return -EINVAL; |
7447 | } | |
7448 | } else if (!reg->ref_obj_id && !register_is_null(reg)) { | |
8f14852e KKD |
7449 | verbose(env, "R%d must be referenced when passed to release function\n", |
7450 | regno); | |
7451 | return -EINVAL; | |
7452 | } | |
7453 | if (meta->release_regno) { | |
7454 | verbose(env, "verifier internal error: more than one release argument\n"); | |
7455 | return -EFAULT; | |
7456 | } | |
7457 | meta->release_regno = regno; | |
7458 | } | |
7459 | ||
02f7c958 | 7460 | if (reg->ref_obj_id) { |
457f4436 AN |
7461 | if (meta->ref_obj_id) { |
7462 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
7463 | regno, reg->ref_obj_id, | |
7464 | meta->ref_obj_id); | |
7465 | return -EFAULT; | |
7466 | } | |
7467 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
7468 | } |
7469 | ||
8ab4cdcf JK |
7470 | switch (base_type(arg_type)) { |
7471 | case ARG_CONST_MAP_PTR: | |
17a52670 | 7472 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
3e8ce298 AS |
7473 | if (meta->map_ptr) { |
7474 | /* Use map_uid (which is unique id of inner map) to reject: | |
7475 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
7476 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
7477 | * if (inner_map1 && inner_map2) { | |
7478 | * timer = bpf_map_lookup_elem(inner_map1); | |
7479 | * if (timer) | |
7480 | * // mismatch would have been allowed | |
7481 | * bpf_timer_init(timer, inner_map2); | |
7482 | * } | |
7483 | * | |
7484 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
7485 | */ | |
7486 | if (meta->map_ptr != reg->map_ptr || | |
7487 | meta->map_uid != reg->map_uid) { | |
7488 | verbose(env, | |
7489 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
7490 | meta->map_uid, reg->map_uid); | |
7491 | return -EINVAL; | |
7492 | } | |
b00628b1 | 7493 | } |
33ff9823 | 7494 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 7495 | meta->map_uid = reg->map_uid; |
8ab4cdcf JK |
7496 | break; |
7497 | case ARG_PTR_TO_MAP_KEY: | |
17a52670 AS |
7498 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
7499 | * check that [key, key + map->key_size) are within | |
7500 | * stack limits and initialized | |
7501 | */ | |
33ff9823 | 7502 | if (!meta->map_ptr) { |
17a52670 AS |
7503 | /* in function declaration map_ptr must come before |
7504 | * map_key, so that it's verified and known before | |
7505 | * we have to check map_key here. Otherwise it means | |
7506 | * that kernel subsystem misconfigured verifier | |
7507 | */ | |
61bd5218 | 7508 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
7509 | return -EACCES; |
7510 | } | |
d71962f3 PC |
7511 | err = check_helper_mem_access(env, regno, |
7512 | meta->map_ptr->key_size, false, | |
7513 | NULL); | |
8ab4cdcf JK |
7514 | break; |
7515 | case ARG_PTR_TO_MAP_VALUE: | |
48946bd6 HL |
7516 | if (type_may_be_null(arg_type) && register_is_null(reg)) |
7517 | return 0; | |
7518 | ||
17a52670 AS |
7519 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
7520 | * check [value, value + map->value_size) validity | |
7521 | */ | |
33ff9823 | 7522 | if (!meta->map_ptr) { |
17a52670 | 7523 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 7524 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
7525 | return -EACCES; |
7526 | } | |
16d1e00c | 7527 | meta->raw_mode = arg_type & MEM_UNINIT; |
d71962f3 PC |
7528 | err = check_helper_mem_access(env, regno, |
7529 | meta->map_ptr->value_size, false, | |
2ea864c5 | 7530 | meta); |
8ab4cdcf JK |
7531 | break; |
7532 | case ARG_PTR_TO_PERCPU_BTF_ID: | |
eaa6bcb7 HL |
7533 | if (!reg->btf_id) { |
7534 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
7535 | return -EACCES; | |
7536 | } | |
22dc4a0f | 7537 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 7538 | meta->ret_btf_id = reg->btf_id; |
8ab4cdcf JK |
7539 | break; |
7540 | case ARG_PTR_TO_SPIN_LOCK: | |
5d92ddc3 DM |
7541 | if (in_rbtree_lock_required_cb(env)) { |
7542 | verbose(env, "can't spin_{lock,unlock} in rbtree cb\n"); | |
7543 | return -EACCES; | |
7544 | } | |
c18f0b6a | 7545 | if (meta->func_id == BPF_FUNC_spin_lock) { |
ac50fe51 KKD |
7546 | err = process_spin_lock(env, regno, true); |
7547 | if (err) | |
7548 | return err; | |
c18f0b6a | 7549 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
ac50fe51 KKD |
7550 | err = process_spin_lock(env, regno, false); |
7551 | if (err) | |
7552 | return err; | |
c18f0b6a LB |
7553 | } else { |
7554 | verbose(env, "verifier internal error\n"); | |
7555 | return -EFAULT; | |
7556 | } | |
8ab4cdcf JK |
7557 | break; |
7558 | case ARG_PTR_TO_TIMER: | |
ac50fe51 KKD |
7559 | err = process_timer_func(env, regno, meta); |
7560 | if (err) | |
7561 | return err; | |
8ab4cdcf JK |
7562 | break; |
7563 | case ARG_PTR_TO_FUNC: | |
69c087ba | 7564 | meta->subprogno = reg->subprogno; |
8ab4cdcf JK |
7565 | break; |
7566 | case ARG_PTR_TO_MEM: | |
a2bbe7cc LB |
7567 | /* The access to this pointer is only checked when we hit the |
7568 | * next is_mem_size argument below. | |
7569 | */ | |
16d1e00c | 7570 | meta->raw_mode = arg_type & MEM_UNINIT; |
508362ac MM |
7571 | if (arg_type & MEM_FIXED_SIZE) { |
7572 | err = check_helper_mem_access(env, regno, | |
7573 | fn->arg_size[arg], false, | |
7574 | meta); | |
7575 | } | |
8ab4cdcf JK |
7576 | break; |
7577 | case ARG_CONST_SIZE: | |
7578 | err = check_mem_size_reg(env, reg, regno, false, meta); | |
7579 | break; | |
7580 | case ARG_CONST_SIZE_OR_ZERO: | |
7581 | err = check_mem_size_reg(env, reg, regno, true, meta); | |
7582 | break; | |
7583 | case ARG_PTR_TO_DYNPTR: | |
1d18feb2 | 7584 | err = process_dynptr_func(env, regno, insn_idx, arg_type); |
ac50fe51 KKD |
7585 | if (err) |
7586 | return err; | |
8ab4cdcf JK |
7587 | break; |
7588 | case ARG_CONST_ALLOC_SIZE_OR_ZERO: | |
457f4436 | 7589 | if (!tnum_is_const(reg->var_off)) { |
28a8add6 | 7590 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
7591 | regno); |
7592 | return -EACCES; | |
7593 | } | |
7594 | meta->mem_size = reg->var_off.value; | |
2fc31465 KKD |
7595 | err = mark_chain_precision(env, regno); |
7596 | if (err) | |
7597 | return err; | |
8ab4cdcf JK |
7598 | break; |
7599 | case ARG_PTR_TO_INT: | |
7600 | case ARG_PTR_TO_LONG: | |
7601 | { | |
57c3bb72 AI |
7602 | int size = int_ptr_type_to_size(arg_type); |
7603 | ||
7604 | err = check_helper_mem_access(env, regno, size, false, meta); | |
7605 | if (err) | |
7606 | return err; | |
7607 | err = check_ptr_alignment(env, reg, 0, size, true); | |
8ab4cdcf JK |
7608 | break; |
7609 | } | |
7610 | case ARG_PTR_TO_CONST_STR: | |
7611 | { | |
fff13c4b FR |
7612 | struct bpf_map *map = reg->map_ptr; |
7613 | int map_off; | |
7614 | u64 map_addr; | |
7615 | char *str_ptr; | |
7616 | ||
a8fad73e | 7617 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
7618 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
7619 | return -EACCES; | |
7620 | } | |
7621 | ||
7622 | if (!tnum_is_const(reg->var_off)) { | |
7623 | verbose(env, "R%d is not a constant address'\n", regno); | |
7624 | return -EACCES; | |
7625 | } | |
7626 | ||
7627 | if (!map->ops->map_direct_value_addr) { | |
7628 | verbose(env, "no direct value access support for this map type\n"); | |
7629 | return -EACCES; | |
7630 | } | |
7631 | ||
7632 | err = check_map_access(env, regno, reg->off, | |
61df10c7 KKD |
7633 | map->value_size - reg->off, false, |
7634 | ACCESS_HELPER); | |
fff13c4b FR |
7635 | if (err) |
7636 | return err; | |
7637 | ||
7638 | map_off = reg->off + reg->var_off.value; | |
7639 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
7640 | if (err) { | |
7641 | verbose(env, "direct value access on string failed\n"); | |
7642 | return err; | |
7643 | } | |
7644 | ||
7645 | str_ptr = (char *)(long)(map_addr); | |
7646 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
7647 | verbose(env, "string is not zero-terminated\n"); | |
7648 | return -EINVAL; | |
7649 | } | |
8ab4cdcf JK |
7650 | break; |
7651 | } | |
7652 | case ARG_PTR_TO_KPTR: | |
ac50fe51 KKD |
7653 | err = process_kptr_func(env, regno, meta); |
7654 | if (err) | |
7655 | return err; | |
8ab4cdcf | 7656 | break; |
17a52670 AS |
7657 | } |
7658 | ||
7659 | return err; | |
7660 | } | |
7661 | ||
0126240f LB |
7662 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
7663 | { | |
7664 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 7665 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
7666 | |
7667 | if (func_id != BPF_FUNC_map_update_elem) | |
7668 | return false; | |
7669 | ||
7670 | /* It's not possible to get access to a locked struct sock in these | |
7671 | * contexts, so updating is safe. | |
7672 | */ | |
7673 | switch (type) { | |
7674 | case BPF_PROG_TYPE_TRACING: | |
7675 | if (eatype == BPF_TRACE_ITER) | |
7676 | return true; | |
7677 | break; | |
7678 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
7679 | case BPF_PROG_TYPE_SCHED_CLS: | |
7680 | case BPF_PROG_TYPE_SCHED_ACT: | |
7681 | case BPF_PROG_TYPE_XDP: | |
7682 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
7683 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
7684 | case BPF_PROG_TYPE_SK_LOOKUP: | |
7685 | return true; | |
7686 | default: | |
7687 | break; | |
7688 | } | |
7689 | ||
7690 | verbose(env, "cannot update sockmap in this context\n"); | |
7691 | return false; | |
7692 | } | |
7693 | ||
e411901c MF |
7694 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
7695 | { | |
95acd881 TA |
7696 | return env->prog->jit_requested && |
7697 | bpf_jit_supports_subprog_tailcalls(); | |
e411901c MF |
7698 | } |
7699 | ||
61bd5218 JK |
7700 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
7701 | struct bpf_map *map, int func_id) | |
35578d79 | 7702 | { |
35578d79 KX |
7703 | if (!map) |
7704 | return 0; | |
7705 | ||
6aff67c8 AS |
7706 | /* We need a two way check, first is from map perspective ... */ |
7707 | switch (map->map_type) { | |
7708 | case BPF_MAP_TYPE_PROG_ARRAY: | |
7709 | if (func_id != BPF_FUNC_tail_call) | |
7710 | goto error; | |
7711 | break; | |
7712 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
7713 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 7714 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 7715 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
7716 | func_id != BPF_FUNC_perf_event_read_value && |
7717 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
7718 | goto error; |
7719 | break; | |
457f4436 AN |
7720 | case BPF_MAP_TYPE_RINGBUF: |
7721 | if (func_id != BPF_FUNC_ringbuf_output && | |
7722 | func_id != BPF_FUNC_ringbuf_reserve && | |
bc34dee6 JK |
7723 | func_id != BPF_FUNC_ringbuf_query && |
7724 | func_id != BPF_FUNC_ringbuf_reserve_dynptr && | |
7725 | func_id != BPF_FUNC_ringbuf_submit_dynptr && | |
7726 | func_id != BPF_FUNC_ringbuf_discard_dynptr) | |
457f4436 AN |
7727 | goto error; |
7728 | break; | |
583c1f42 | 7729 | case BPF_MAP_TYPE_USER_RINGBUF: |
20571567 DV |
7730 | if (func_id != BPF_FUNC_user_ringbuf_drain) |
7731 | goto error; | |
7732 | break; | |
6aff67c8 AS |
7733 | case BPF_MAP_TYPE_STACK_TRACE: |
7734 | if (func_id != BPF_FUNC_get_stackid) | |
7735 | goto error; | |
7736 | break; | |
4ed8ec52 | 7737 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 7738 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 7739 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
7740 | goto error; |
7741 | break; | |
cd339431 | 7742 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 7743 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
7744 | if (func_id != BPF_FUNC_get_local_storage) |
7745 | goto error; | |
7746 | break; | |
546ac1ff | 7747 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 7748 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
7749 | if (func_id != BPF_FUNC_redirect_map && |
7750 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
7751 | goto error; |
7752 | break; | |
fbfc504a BT |
7753 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
7754 | * appear. | |
7755 | */ | |
6710e112 JDB |
7756 | case BPF_MAP_TYPE_CPUMAP: |
7757 | if (func_id != BPF_FUNC_redirect_map) | |
7758 | goto error; | |
7759 | break; | |
fada7fdc JL |
7760 | case BPF_MAP_TYPE_XSKMAP: |
7761 | if (func_id != BPF_FUNC_redirect_map && | |
7762 | func_id != BPF_FUNC_map_lookup_elem) | |
7763 | goto error; | |
7764 | break; | |
56f668df | 7765 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 7766 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
7767 | if (func_id != BPF_FUNC_map_lookup_elem) |
7768 | goto error; | |
16a43625 | 7769 | break; |
174a79ff JF |
7770 | case BPF_MAP_TYPE_SOCKMAP: |
7771 | if (func_id != BPF_FUNC_sk_redirect_map && | |
7772 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 7773 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 7774 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 7775 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
7776 | func_id != BPF_FUNC_map_lookup_elem && |
7777 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
7778 | goto error; |
7779 | break; | |
81110384 JF |
7780 | case BPF_MAP_TYPE_SOCKHASH: |
7781 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
7782 | func_id != BPF_FUNC_sock_hash_update && | |
7783 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 7784 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 7785 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
7786 | func_id != BPF_FUNC_map_lookup_elem && |
7787 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
7788 | goto error; |
7789 | break; | |
2dbb9b9e MKL |
7790 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
7791 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
7792 | goto error; | |
7793 | break; | |
f1a2e44a MV |
7794 | case BPF_MAP_TYPE_QUEUE: |
7795 | case BPF_MAP_TYPE_STACK: | |
7796 | if (func_id != BPF_FUNC_map_peek_elem && | |
7797 | func_id != BPF_FUNC_map_pop_elem && | |
7798 | func_id != BPF_FUNC_map_push_elem) | |
7799 | goto error; | |
7800 | break; | |
6ac99e8f MKL |
7801 | case BPF_MAP_TYPE_SK_STORAGE: |
7802 | if (func_id != BPF_FUNC_sk_storage_get && | |
9db44fdd KKD |
7803 | func_id != BPF_FUNC_sk_storage_delete && |
7804 | func_id != BPF_FUNC_kptr_xchg) | |
6ac99e8f MKL |
7805 | goto error; |
7806 | break; | |
8ea63684 KS |
7807 | case BPF_MAP_TYPE_INODE_STORAGE: |
7808 | if (func_id != BPF_FUNC_inode_storage_get && | |
9db44fdd KKD |
7809 | func_id != BPF_FUNC_inode_storage_delete && |
7810 | func_id != BPF_FUNC_kptr_xchg) | |
8ea63684 KS |
7811 | goto error; |
7812 | break; | |
4cf1bc1f KS |
7813 | case BPF_MAP_TYPE_TASK_STORAGE: |
7814 | if (func_id != BPF_FUNC_task_storage_get && | |
9db44fdd KKD |
7815 | func_id != BPF_FUNC_task_storage_delete && |
7816 | func_id != BPF_FUNC_kptr_xchg) | |
4cf1bc1f KS |
7817 | goto error; |
7818 | break; | |
c4bcfb38 YS |
7819 | case BPF_MAP_TYPE_CGRP_STORAGE: |
7820 | if (func_id != BPF_FUNC_cgrp_storage_get && | |
9db44fdd KKD |
7821 | func_id != BPF_FUNC_cgrp_storage_delete && |
7822 | func_id != BPF_FUNC_kptr_xchg) | |
c4bcfb38 YS |
7823 | goto error; |
7824 | break; | |
9330986c JK |
7825 | case BPF_MAP_TYPE_BLOOM_FILTER: |
7826 | if (func_id != BPF_FUNC_map_peek_elem && | |
7827 | func_id != BPF_FUNC_map_push_elem) | |
7828 | goto error; | |
7829 | break; | |
6aff67c8 AS |
7830 | default: |
7831 | break; | |
7832 | } | |
7833 | ||
7834 | /* ... and second from the function itself. */ | |
7835 | switch (func_id) { | |
7836 | case BPF_FUNC_tail_call: | |
7837 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
7838 | goto error; | |
e411901c MF |
7839 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
7840 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
7841 | return -EINVAL; |
7842 | } | |
6aff67c8 AS |
7843 | break; |
7844 | case BPF_FUNC_perf_event_read: | |
7845 | case BPF_FUNC_perf_event_output: | |
908432ca | 7846 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 7847 | case BPF_FUNC_skb_output: |
d831ee84 | 7848 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
7849 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
7850 | goto error; | |
7851 | break; | |
5b029a32 DB |
7852 | case BPF_FUNC_ringbuf_output: |
7853 | case BPF_FUNC_ringbuf_reserve: | |
7854 | case BPF_FUNC_ringbuf_query: | |
bc34dee6 JK |
7855 | case BPF_FUNC_ringbuf_reserve_dynptr: |
7856 | case BPF_FUNC_ringbuf_submit_dynptr: | |
7857 | case BPF_FUNC_ringbuf_discard_dynptr: | |
5b029a32 DB |
7858 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
7859 | goto error; | |
7860 | break; | |
20571567 DV |
7861 | case BPF_FUNC_user_ringbuf_drain: |
7862 | if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF) | |
7863 | goto error; | |
7864 | break; | |
6aff67c8 AS |
7865 | case BPF_FUNC_get_stackid: |
7866 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
7867 | goto error; | |
7868 | break; | |
60d20f91 | 7869 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 7870 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
7871 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
7872 | goto error; | |
7873 | break; | |
97f91a7c | 7874 | case BPF_FUNC_redirect_map: |
9c270af3 | 7875 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 7876 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
7877 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
7878 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
7879 | goto error; |
7880 | break; | |
174a79ff | 7881 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 7882 | case BPF_FUNC_msg_redirect_map: |
81110384 | 7883 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
7884 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
7885 | goto error; | |
7886 | break; | |
81110384 JF |
7887 | case BPF_FUNC_sk_redirect_hash: |
7888 | case BPF_FUNC_msg_redirect_hash: | |
7889 | case BPF_FUNC_sock_hash_update: | |
7890 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
7891 | goto error; |
7892 | break; | |
cd339431 | 7893 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
7894 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
7895 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
7896 | goto error; |
7897 | break; | |
2dbb9b9e | 7898 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
7899 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
7900 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
7901 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
7902 | goto error; |
7903 | break; | |
f1a2e44a | 7904 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
7905 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
7906 | map->map_type != BPF_MAP_TYPE_STACK) | |
7907 | goto error; | |
7908 | break; | |
9330986c JK |
7909 | case BPF_FUNC_map_peek_elem: |
7910 | case BPF_FUNC_map_push_elem: | |
7911 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
7912 | map->map_type != BPF_MAP_TYPE_STACK && | |
7913 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
7914 | goto error; | |
7915 | break; | |
07343110 FZ |
7916 | case BPF_FUNC_map_lookup_percpu_elem: |
7917 | if (map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && | |
7918 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && | |
7919 | map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH) | |
7920 | goto error; | |
7921 | break; | |
6ac99e8f MKL |
7922 | case BPF_FUNC_sk_storage_get: |
7923 | case BPF_FUNC_sk_storage_delete: | |
7924 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
7925 | goto error; | |
7926 | break; | |
8ea63684 KS |
7927 | case BPF_FUNC_inode_storage_get: |
7928 | case BPF_FUNC_inode_storage_delete: | |
7929 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
7930 | goto error; | |
7931 | break; | |
4cf1bc1f KS |
7932 | case BPF_FUNC_task_storage_get: |
7933 | case BPF_FUNC_task_storage_delete: | |
7934 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
7935 | goto error; | |
7936 | break; | |
c4bcfb38 YS |
7937 | case BPF_FUNC_cgrp_storage_get: |
7938 | case BPF_FUNC_cgrp_storage_delete: | |
7939 | if (map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) | |
7940 | goto error; | |
7941 | break; | |
6aff67c8 AS |
7942 | default: |
7943 | break; | |
35578d79 KX |
7944 | } |
7945 | ||
7946 | return 0; | |
6aff67c8 | 7947 | error: |
61bd5218 | 7948 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 7949 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 7950 | return -EINVAL; |
35578d79 KX |
7951 | } |
7952 | ||
90133415 | 7953 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
7954 | { |
7955 | int count = 0; | |
7956 | ||
39f19ebb | 7957 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7958 | count++; |
39f19ebb | 7959 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7960 | count++; |
39f19ebb | 7961 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7962 | count++; |
39f19ebb | 7963 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 7964 | count++; |
39f19ebb | 7965 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
7966 | count++; |
7967 | ||
90133415 DB |
7968 | /* We only support one arg being in raw mode at the moment, |
7969 | * which is sufficient for the helper functions we have | |
7970 | * right now. | |
7971 | */ | |
7972 | return count <= 1; | |
7973 | } | |
7974 | ||
508362ac | 7975 | static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg) |
90133415 | 7976 | { |
508362ac MM |
7977 | bool is_fixed = fn->arg_type[arg] & MEM_FIXED_SIZE; |
7978 | bool has_size = fn->arg_size[arg] != 0; | |
7979 | bool is_next_size = false; | |
7980 | ||
7981 | if (arg + 1 < ARRAY_SIZE(fn->arg_type)) | |
7982 | is_next_size = arg_type_is_mem_size(fn->arg_type[arg + 1]); | |
7983 | ||
7984 | if (base_type(fn->arg_type[arg]) != ARG_PTR_TO_MEM) | |
7985 | return is_next_size; | |
7986 | ||
7987 | return has_size == is_next_size || is_next_size == is_fixed; | |
90133415 DB |
7988 | } |
7989 | ||
7990 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
7991 | { | |
7992 | /* bpf_xxx(..., buf, len) call will access 'len' | |
7993 | * bytes from memory 'buf'. Both arg types need | |
7994 | * to be paired, so make sure there's no buggy | |
7995 | * helper function specification. | |
7996 | */ | |
7997 | if (arg_type_is_mem_size(fn->arg1_type) || | |
508362ac MM |
7998 | check_args_pair_invalid(fn, 0) || |
7999 | check_args_pair_invalid(fn, 1) || | |
8000 | check_args_pair_invalid(fn, 2) || | |
8001 | check_args_pair_invalid(fn, 3) || | |
8002 | check_args_pair_invalid(fn, 4)) | |
90133415 DB |
8003 | return false; |
8004 | ||
8005 | return true; | |
8006 | } | |
8007 | ||
9436ef6e LB |
8008 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
8009 | { | |
8010 | int i; | |
8011 | ||
1df8f55a | 8012 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
4e814da0 KKD |
8013 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_BTF_ID) |
8014 | return !!fn->arg_btf_id[i]; | |
8015 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_SPIN_LOCK) | |
8016 | return fn->arg_btf_id[i] == BPF_PTR_POISON; | |
508362ac MM |
8017 | if (base_type(fn->arg_type[i]) != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i] && |
8018 | /* arg_btf_id and arg_size are in a union. */ | |
8019 | (base_type(fn->arg_type[i]) != ARG_PTR_TO_MEM || | |
8020 | !(fn->arg_type[i] & MEM_FIXED_SIZE))) | |
1df8f55a MKL |
8021 | return false; |
8022 | } | |
8023 | ||
9436ef6e LB |
8024 | return true; |
8025 | } | |
8026 | ||
0c9a7a7e | 8027 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
8028 | { |
8029 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 8030 | check_arg_pair_ok(fn) && |
b2d8ef19 | 8031 | check_btf_id_ok(fn) ? 0 : -EINVAL; |
435faee1 DB |
8032 | } |
8033 | ||
de8f3a83 DB |
8034 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
8035 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
66e3a13e JK |
8036 | * |
8037 | * This also applies to dynptr slices belonging to skb and xdp dynptrs, | |
8038 | * since these slices point to packet data. | |
f1174f77 | 8039 | */ |
b239da34 | 8040 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 8041 | { |
b239da34 KKD |
8042 | struct bpf_func_state *state; |
8043 | struct bpf_reg_state *reg; | |
969bf05e | 8044 | |
b239da34 | 8045 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
66e3a13e | 8046 | if (reg_is_pkt_pointer_any(reg) || reg_is_dynptr_slice_pkt(reg)) |
dbd8d228 | 8047 | mark_reg_invalid(env, reg); |
b239da34 | 8048 | })); |
f4d7e40a AS |
8049 | } |
8050 | ||
6d94e741 AS |
8051 | enum { |
8052 | AT_PKT_END = -1, | |
8053 | BEYOND_PKT_END = -2, | |
8054 | }; | |
8055 | ||
8056 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
8057 | { | |
8058 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
8059 | struct bpf_reg_state *reg = &state->regs[regn]; | |
8060 | ||
8061 | if (reg->type != PTR_TO_PACKET) | |
8062 | /* PTR_TO_PACKET_META is not supported yet */ | |
8063 | return; | |
8064 | ||
8065 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
8066 | * How far beyond pkt_end it goes is unknown. | |
8067 | * if (!range_open) it's the case of pkt >= pkt_end | |
8068 | * if (range_open) it's the case of pkt > pkt_end | |
8069 | * hence this pointer is at least 1 byte bigger than pkt_end | |
8070 | */ | |
8071 | if (range_open) | |
8072 | reg->range = BEYOND_PKT_END; | |
8073 | else | |
8074 | reg->range = AT_PKT_END; | |
8075 | } | |
8076 | ||
fd978bf7 JS |
8077 | /* The pointer with the specified id has released its reference to kernel |
8078 | * resources. Identify all copies of the same pointer and clear the reference. | |
8079 | */ | |
8080 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 8081 | int ref_obj_id) |
fd978bf7 | 8082 | { |
b239da34 KKD |
8083 | struct bpf_func_state *state; |
8084 | struct bpf_reg_state *reg; | |
1b986589 | 8085 | int err; |
fd978bf7 | 8086 | |
1b986589 MKL |
8087 | err = release_reference_state(cur_func(env), ref_obj_id); |
8088 | if (err) | |
8089 | return err; | |
8090 | ||
b239da34 | 8091 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
dbd8d228 KKD |
8092 | if (reg->ref_obj_id == ref_obj_id) |
8093 | mark_reg_invalid(env, reg); | |
b239da34 | 8094 | })); |
fd978bf7 | 8095 | |
1b986589 | 8096 | return 0; |
fd978bf7 JS |
8097 | } |
8098 | ||
6a3cd331 DM |
8099 | static void invalidate_non_owning_refs(struct bpf_verifier_env *env) |
8100 | { | |
8101 | struct bpf_func_state *unused; | |
8102 | struct bpf_reg_state *reg; | |
8103 | ||
8104 | bpf_for_each_reg_in_vstate(env->cur_state, unused, reg, ({ | |
8105 | if (type_is_non_owning_ref(reg->type)) | |
dbd8d228 | 8106 | mark_reg_invalid(env, reg); |
6a3cd331 DM |
8107 | })); |
8108 | } | |
8109 | ||
51c39bb1 AS |
8110 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
8111 | struct bpf_reg_state *regs) | |
8112 | { | |
8113 | int i; | |
8114 | ||
8115 | /* after the call registers r0 - r5 were scratched */ | |
8116 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
8117 | mark_reg_not_init(env, regs, caller_saved[i]); | |
8118 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
8119 | } | |
8120 | } | |
8121 | ||
14351375 YS |
8122 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
8123 | struct bpf_func_state *caller, | |
8124 | struct bpf_func_state *callee, | |
8125 | int insn_idx); | |
8126 | ||
be2ef816 AN |
8127 | static int set_callee_state(struct bpf_verifier_env *env, |
8128 | struct bpf_func_state *caller, | |
8129 | struct bpf_func_state *callee, int insn_idx); | |
8130 | ||
5d92ddc3 DM |
8131 | static bool is_callback_calling_kfunc(u32 btf_id); |
8132 | ||
14351375 YS |
8133 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8134 | int *insn_idx, int subprog, | |
8135 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
8136 | { |
8137 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 8138 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 8139 | struct bpf_func_state *caller, *callee; |
14351375 | 8140 | int err; |
51c39bb1 | 8141 | bool is_global = false; |
f4d7e40a | 8142 | |
aada9ce6 | 8143 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 8144 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 8145 | state->curframe + 2); |
f4d7e40a AS |
8146 | return -E2BIG; |
8147 | } | |
8148 | ||
f4d7e40a AS |
8149 | caller = state->frame[state->curframe]; |
8150 | if (state->frame[state->curframe + 1]) { | |
8151 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
8152 | state->curframe + 1); | |
8153 | return -EFAULT; | |
8154 | } | |
8155 | ||
51c39bb1 AS |
8156 | func_info_aux = env->prog->aux->func_info_aux; |
8157 | if (func_info_aux) | |
8158 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
95f2f26f | 8159 | err = btf_check_subprog_call(env, subprog, caller->regs); |
51c39bb1 AS |
8160 | if (err == -EFAULT) |
8161 | return err; | |
8162 | if (is_global) { | |
8163 | if (err) { | |
8164 | verbose(env, "Caller passes invalid args into func#%d\n", | |
8165 | subprog); | |
8166 | return err; | |
8167 | } else { | |
8168 | if (env->log.level & BPF_LOG_LEVEL) | |
8169 | verbose(env, | |
8170 | "Func#%d is global and valid. Skipping.\n", | |
8171 | subprog); | |
8172 | clear_caller_saved_regs(env, caller->regs); | |
8173 | ||
45159b27 | 8174 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 8175 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 8176 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
8177 | |
8178 | /* continue with next insn after call */ | |
8179 | return 0; | |
8180 | } | |
8181 | } | |
8182 | ||
be2ef816 AN |
8183 | /* set_callee_state is used for direct subprog calls, but we are |
8184 | * interested in validating only BPF helpers that can call subprogs as | |
8185 | * callbacks | |
8186 | */ | |
5d92ddc3 DM |
8187 | if (set_callee_state_cb != set_callee_state) { |
8188 | if (bpf_pseudo_kfunc_call(insn) && | |
8189 | !is_callback_calling_kfunc(insn->imm)) { | |
8190 | verbose(env, "verifier bug: kfunc %s#%d not marked as callback-calling\n", | |
8191 | func_id_name(insn->imm), insn->imm); | |
8192 | return -EFAULT; | |
8193 | } else if (!bpf_pseudo_kfunc_call(insn) && | |
8194 | !is_callback_calling_function(insn->imm)) { /* helper */ | |
8195 | verbose(env, "verifier bug: helper %s#%d not marked as callback-calling\n", | |
8196 | func_id_name(insn->imm), insn->imm); | |
8197 | return -EFAULT; | |
8198 | } | |
be2ef816 AN |
8199 | } |
8200 | ||
bfc6bb74 | 8201 | if (insn->code == (BPF_JMP | BPF_CALL) && |
a5bebc4f | 8202 | insn->src_reg == 0 && |
bfc6bb74 AS |
8203 | insn->imm == BPF_FUNC_timer_set_callback) { |
8204 | struct bpf_verifier_state *async_cb; | |
8205 | ||
8206 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 8207 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
8208 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
8209 | *insn_idx, subprog); | |
8210 | if (!async_cb) | |
8211 | return -EFAULT; | |
8212 | callee = async_cb->frame[0]; | |
8213 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
8214 | ||
8215 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
8216 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
8217 | if (err) | |
8218 | return err; | |
8219 | ||
8220 | clear_caller_saved_regs(env, caller->regs); | |
8221 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
8222 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
8223 | /* continue with next insn after call */ | |
8224 | return 0; | |
8225 | } | |
8226 | ||
f4d7e40a AS |
8227 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
8228 | if (!callee) | |
8229 | return -ENOMEM; | |
8230 | state->frame[state->curframe + 1] = callee; | |
8231 | ||
8232 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
8233 | * into its own stack before reading from it. | |
8234 | * callee can read/write into caller's stack | |
8235 | */ | |
8236 | init_func_state(env, callee, | |
8237 | /* remember the callsite, it will be used by bpf_exit */ | |
8238 | *insn_idx /* callsite */, | |
8239 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 8240 | subprog /* subprog number within this prog */); |
f4d7e40a | 8241 | |
fd978bf7 | 8242 | /* Transfer references to the callee */ |
c69431aa | 8243 | err = copy_reference_state(callee, caller); |
fd978bf7 | 8244 | if (err) |
eb86559a | 8245 | goto err_out; |
fd978bf7 | 8246 | |
14351375 YS |
8247 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
8248 | if (err) | |
eb86559a | 8249 | goto err_out; |
f4d7e40a | 8250 | |
51c39bb1 | 8251 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
8252 | |
8253 | /* only increment it after check_reg_arg() finished */ | |
8254 | state->curframe++; | |
8255 | ||
8256 | /* and go analyze first insn of the callee */ | |
14351375 | 8257 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 8258 | |
06ee7115 | 8259 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 8260 | verbose(env, "caller:\n"); |
0f55f9ed | 8261 | print_verifier_state(env, caller, true); |
f4d7e40a | 8262 | verbose(env, "callee:\n"); |
0f55f9ed | 8263 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
8264 | } |
8265 | return 0; | |
eb86559a WY |
8266 | |
8267 | err_out: | |
8268 | free_func_state(callee); | |
8269 | state->frame[state->curframe + 1] = NULL; | |
8270 | return err; | |
f4d7e40a AS |
8271 | } |
8272 | ||
314ee05e YS |
8273 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
8274 | struct bpf_func_state *caller, | |
8275 | struct bpf_func_state *callee) | |
8276 | { | |
8277 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
8278 | * void *callback_ctx, u64 flags); | |
8279 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
8280 | * void *callback_ctx); | |
8281 | */ | |
8282 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
8283 | ||
8284 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
8285 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8286 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8287 | ||
8288 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
8289 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
8290 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8291 | ||
8292 | /* pointer to stack or null */ | |
8293 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
8294 | ||
8295 | /* unused */ | |
8296 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8297 | return 0; | |
8298 | } | |
8299 | ||
14351375 YS |
8300 | static int set_callee_state(struct bpf_verifier_env *env, |
8301 | struct bpf_func_state *caller, | |
8302 | struct bpf_func_state *callee, int insn_idx) | |
8303 | { | |
8304 | int i; | |
8305 | ||
8306 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
8307 | * pointers, which connects us up to the liveness chain | |
8308 | */ | |
8309 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
8310 | callee->regs[i] = caller->regs[i]; | |
8311 | return 0; | |
8312 | } | |
8313 | ||
8314 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
8315 | int *insn_idx) | |
8316 | { | |
8317 | int subprog, target_insn; | |
8318 | ||
8319 | target_insn = *insn_idx + insn->imm + 1; | |
8320 | subprog = find_subprog(env, target_insn); | |
8321 | if (subprog < 0) { | |
8322 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
8323 | target_insn); | |
8324 | return -EFAULT; | |
8325 | } | |
8326 | ||
8327 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
8328 | } | |
8329 | ||
69c087ba YS |
8330 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
8331 | struct bpf_func_state *caller, | |
8332 | struct bpf_func_state *callee, | |
8333 | int insn_idx) | |
8334 | { | |
8335 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
8336 | struct bpf_map *map; | |
8337 | int err; | |
8338 | ||
8339 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
8340 | verbose(env, "tail_call abusing map_ptr\n"); | |
8341 | return -EINVAL; | |
8342 | } | |
8343 | ||
8344 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
8345 | if (!map->ops->map_set_for_each_callback_args || | |
8346 | !map->ops->map_for_each_callback) { | |
8347 | verbose(env, "callback function not allowed for map\n"); | |
8348 | return -ENOTSUPP; | |
8349 | } | |
8350 | ||
8351 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
8352 | if (err) | |
8353 | return err; | |
8354 | ||
8355 | callee->in_callback_fn = true; | |
1bfe26fb | 8356 | callee->callback_ret_range = tnum_range(0, 1); |
69c087ba YS |
8357 | return 0; |
8358 | } | |
8359 | ||
e6f2dd0f JK |
8360 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
8361 | struct bpf_func_state *caller, | |
8362 | struct bpf_func_state *callee, | |
8363 | int insn_idx) | |
8364 | { | |
8365 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
8366 | * u64 flags); | |
8367 | * callback_fn(u32 index, void *callback_ctx); | |
8368 | */ | |
8369 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
8370 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
8371 | ||
8372 | /* unused */ | |
8373 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8374 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8375 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8376 | ||
8377 | callee->in_callback_fn = true; | |
1bfe26fb | 8378 | callee->callback_ret_range = tnum_range(0, 1); |
e6f2dd0f JK |
8379 | return 0; |
8380 | } | |
8381 | ||
b00628b1 AS |
8382 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
8383 | struct bpf_func_state *caller, | |
8384 | struct bpf_func_state *callee, | |
8385 | int insn_idx) | |
8386 | { | |
8387 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8388 | ||
8389 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
8390 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
8391 | */ | |
8392 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
8393 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
8394 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
8395 | ||
8396 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
8397 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8398 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
8399 | ||
8400 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
8401 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
8402 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
8403 | ||
8404 | /* unused */ | |
8405 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8406 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 8407 | callee->in_async_callback_fn = true; |
1bfe26fb | 8408 | callee->callback_ret_range = tnum_range(0, 1); |
b00628b1 AS |
8409 | return 0; |
8410 | } | |
8411 | ||
7c7e3d31 SL |
8412 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
8413 | struct bpf_func_state *caller, | |
8414 | struct bpf_func_state *callee, | |
8415 | int insn_idx) | |
8416 | { | |
8417 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
8418 | * void *callback_fn, void *callback_ctx, u64 flags) | |
8419 | * (callback_fn)(struct task_struct *task, | |
8420 | * struct vm_area_struct *vma, void *callback_ctx); | |
8421 | */ | |
8422 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
8423 | ||
8424 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
8425 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8426 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 8427 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
8428 | |
8429 | /* pointer to stack or null */ | |
8430 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
8431 | ||
8432 | /* unused */ | |
8433 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8434 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8435 | callee->in_callback_fn = true; | |
1bfe26fb | 8436 | callee->callback_ret_range = tnum_range(0, 1); |
7c7e3d31 SL |
8437 | return 0; |
8438 | } | |
8439 | ||
20571567 DV |
8440 | static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env, |
8441 | struct bpf_func_state *caller, | |
8442 | struct bpf_func_state *callee, | |
8443 | int insn_idx) | |
8444 | { | |
8445 | /* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void | |
8446 | * callback_ctx, u64 flags); | |
27060531 | 8447 | * callback_fn(const struct bpf_dynptr_t* dynptr, void *callback_ctx); |
20571567 DV |
8448 | */ |
8449 | __mark_reg_not_init(env, &callee->regs[BPF_REG_0]); | |
f8064ab9 | 8450 | mark_dynptr_cb_reg(env, &callee->regs[BPF_REG_1], BPF_DYNPTR_TYPE_LOCAL); |
20571567 DV |
8451 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; |
8452 | ||
8453 | /* unused */ | |
8454 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8455 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8456 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8457 | ||
8458 | callee->in_callback_fn = true; | |
c92a7a52 | 8459 | callee->callback_ret_range = tnum_range(0, 1); |
20571567 DV |
8460 | return 0; |
8461 | } | |
8462 | ||
5d92ddc3 DM |
8463 | static int set_rbtree_add_callback_state(struct bpf_verifier_env *env, |
8464 | struct bpf_func_state *caller, | |
8465 | struct bpf_func_state *callee, | |
8466 | int insn_idx) | |
8467 | { | |
8468 | /* void bpf_rbtree_add(struct bpf_rb_root *root, struct bpf_rb_node *node, | |
8469 | * bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b)); | |
8470 | * | |
8471 | * 'struct bpf_rb_node *node' arg to bpf_rbtree_add is the same PTR_TO_BTF_ID w/ offset | |
8472 | * that 'less' callback args will be receiving. However, 'node' arg was release_reference'd | |
8473 | * by this point, so look at 'root' | |
8474 | */ | |
8475 | struct btf_field *field; | |
8476 | ||
8477 | field = reg_find_field_offset(&caller->regs[BPF_REG_1], caller->regs[BPF_REG_1].off, | |
8478 | BPF_RB_ROOT); | |
8479 | if (!field || !field->graph_root.value_btf_id) | |
8480 | return -EFAULT; | |
8481 | ||
8482 | mark_reg_graph_node(callee->regs, BPF_REG_1, &field->graph_root); | |
8483 | ref_set_non_owning(env, &callee->regs[BPF_REG_1]); | |
8484 | mark_reg_graph_node(callee->regs, BPF_REG_2, &field->graph_root); | |
8485 | ref_set_non_owning(env, &callee->regs[BPF_REG_2]); | |
8486 | ||
8487 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8488 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8489 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8490 | callee->in_callback_fn = true; | |
8491 | callee->callback_ret_range = tnum_range(0, 1); | |
8492 | return 0; | |
8493 | } | |
8494 | ||
8495 | static bool is_rbtree_lock_required_kfunc(u32 btf_id); | |
8496 | ||
8497 | /* Are we currently verifying the callback for a rbtree helper that must | |
8498 | * be called with lock held? If so, no need to complain about unreleased | |
8499 | * lock | |
8500 | */ | |
8501 | static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env) | |
8502 | { | |
8503 | struct bpf_verifier_state *state = env->cur_state; | |
8504 | struct bpf_insn *insn = env->prog->insnsi; | |
8505 | struct bpf_func_state *callee; | |
8506 | int kfunc_btf_id; | |
8507 | ||
8508 | if (!state->curframe) | |
8509 | return false; | |
8510 | ||
8511 | callee = state->frame[state->curframe]; | |
8512 | ||
8513 | if (!callee->in_callback_fn) | |
8514 | return false; | |
8515 | ||
8516 | kfunc_btf_id = insn[callee->callsite].imm; | |
8517 | return is_rbtree_lock_required_kfunc(kfunc_btf_id); | |
8518 | } | |
8519 | ||
f4d7e40a AS |
8520 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
8521 | { | |
8522 | struct bpf_verifier_state *state = env->cur_state; | |
8523 | struct bpf_func_state *caller, *callee; | |
8524 | struct bpf_reg_state *r0; | |
fd978bf7 | 8525 | int err; |
f4d7e40a AS |
8526 | |
8527 | callee = state->frame[state->curframe]; | |
8528 | r0 = &callee->regs[BPF_REG_0]; | |
8529 | if (r0->type == PTR_TO_STACK) { | |
8530 | /* technically it's ok to return caller's stack pointer | |
8531 | * (or caller's caller's pointer) back to the caller, | |
8532 | * since these pointers are valid. Only current stack | |
8533 | * pointer will be invalid as soon as function exits, | |
8534 | * but let's be conservative | |
8535 | */ | |
8536 | verbose(env, "cannot return stack pointer to the caller\n"); | |
8537 | return -EINVAL; | |
8538 | } | |
8539 | ||
eb86559a | 8540 | caller = state->frame[state->curframe - 1]; |
69c087ba YS |
8541 | if (callee->in_callback_fn) { |
8542 | /* enforce R0 return value range [0, 1]. */ | |
1bfe26fb | 8543 | struct tnum range = callee->callback_ret_range; |
69c087ba YS |
8544 | |
8545 | if (r0->type != SCALAR_VALUE) { | |
8546 | verbose(env, "R0 not a scalar value\n"); | |
8547 | return -EACCES; | |
8548 | } | |
8549 | if (!tnum_in(range, r0->var_off)) { | |
8550 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
8551 | return -EINVAL; | |
8552 | } | |
8553 | } else { | |
8554 | /* return to the caller whatever r0 had in the callee */ | |
8555 | caller->regs[BPF_REG_0] = *r0; | |
8556 | } | |
f4d7e40a | 8557 | |
9d9d00ac KKD |
8558 | /* callback_fn frame should have released its own additions to parent's |
8559 | * reference state at this point, or check_reference_leak would | |
8560 | * complain, hence it must be the same as the caller. There is no need | |
8561 | * to copy it back. | |
8562 | */ | |
8563 | if (!callee->in_callback_fn) { | |
8564 | /* Transfer references to the caller */ | |
8565 | err = copy_reference_state(caller, callee); | |
8566 | if (err) | |
8567 | return err; | |
8568 | } | |
fd978bf7 | 8569 | |
f4d7e40a | 8570 | *insn_idx = callee->callsite + 1; |
06ee7115 | 8571 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 8572 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 8573 | print_verifier_state(env, callee, true); |
f4d7e40a | 8574 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 8575 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
8576 | } |
8577 | /* clear everything in the callee */ | |
8578 | free_func_state(callee); | |
eb86559a | 8579 | state->frame[state->curframe--] = NULL; |
f4d7e40a AS |
8580 | return 0; |
8581 | } | |
8582 | ||
849fa506 YS |
8583 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
8584 | int func_id, | |
8585 | struct bpf_call_arg_meta *meta) | |
8586 | { | |
8587 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
8588 | ||
8589 | if (ret_type != RET_INTEGER || | |
8590 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 8591 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
8592 | func_id != BPF_FUNC_probe_read_str && |
8593 | func_id != BPF_FUNC_probe_read_kernel_str && | |
8594 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
8595 | return; |
8596 | ||
10060503 | 8597 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 8598 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
8599 | ret_reg->smin_value = -MAX_ERRNO; |
8600 | ret_reg->s32_min_value = -MAX_ERRNO; | |
3844d153 | 8601 | reg_bounds_sync(ret_reg); |
849fa506 YS |
8602 | } |
8603 | ||
c93552c4 DB |
8604 | static int |
8605 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
8606 | int func_id, int insn_idx) | |
8607 | { | |
8608 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 8609 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
8610 | |
8611 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
8612 | func_id != BPF_FUNC_map_lookup_elem && |
8613 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
8614 | func_id != BPF_FUNC_map_delete_elem && |
8615 | func_id != BPF_FUNC_map_push_elem && | |
8616 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 8617 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f | 8618 | func_id != BPF_FUNC_for_each_map_elem && |
07343110 FZ |
8619 | func_id != BPF_FUNC_redirect_map && |
8620 | func_id != BPF_FUNC_map_lookup_percpu_elem) | |
c93552c4 | 8621 | return 0; |
09772d92 | 8622 | |
591fe988 | 8623 | if (map == NULL) { |
c93552c4 DB |
8624 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
8625 | return -EINVAL; | |
8626 | } | |
8627 | ||
591fe988 DB |
8628 | /* In case of read-only, some additional restrictions |
8629 | * need to be applied in order to prevent altering the | |
8630 | * state of the map from program side. | |
8631 | */ | |
8632 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
8633 | (func_id == BPF_FUNC_map_delete_elem || | |
8634 | func_id == BPF_FUNC_map_update_elem || | |
8635 | func_id == BPF_FUNC_map_push_elem || | |
8636 | func_id == BPF_FUNC_map_pop_elem)) { | |
8637 | verbose(env, "write into map forbidden\n"); | |
8638 | return -EACCES; | |
8639 | } | |
8640 | ||
d2e4c1e6 | 8641 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 8642 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 8643 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 8644 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 8645 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 8646 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
8647 | return 0; |
8648 | } | |
8649 | ||
d2e4c1e6 DB |
8650 | static int |
8651 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
8652 | int func_id, int insn_idx) | |
8653 | { | |
8654 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
8655 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
8656 | struct bpf_map *map = meta->map_ptr; | |
a657182a | 8657 | u64 val, max; |
cc52d914 | 8658 | int err; |
d2e4c1e6 DB |
8659 | |
8660 | if (func_id != BPF_FUNC_tail_call) | |
8661 | return 0; | |
8662 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
8663 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
8664 | return -EINVAL; | |
8665 | } | |
8666 | ||
d2e4c1e6 | 8667 | reg = ®s[BPF_REG_3]; |
a657182a DB |
8668 | val = reg->var_off.value; |
8669 | max = map->max_entries; | |
d2e4c1e6 | 8670 | |
a657182a | 8671 | if (!(register_is_const(reg) && val < max)) { |
d2e4c1e6 DB |
8672 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
8673 | return 0; | |
8674 | } | |
8675 | ||
cc52d914 DB |
8676 | err = mark_chain_precision(env, BPF_REG_3); |
8677 | if (err) | |
8678 | return err; | |
d2e4c1e6 DB |
8679 | if (bpf_map_key_unseen(aux)) |
8680 | bpf_map_key_store(aux, val); | |
8681 | else if (!bpf_map_key_poisoned(aux) && | |
8682 | bpf_map_key_immediate(aux) != val) | |
8683 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
8684 | return 0; | |
8685 | } | |
8686 | ||
fd978bf7 JS |
8687 | static int check_reference_leak(struct bpf_verifier_env *env) |
8688 | { | |
8689 | struct bpf_func_state *state = cur_func(env); | |
9d9d00ac | 8690 | bool refs_lingering = false; |
fd978bf7 JS |
8691 | int i; |
8692 | ||
9d9d00ac KKD |
8693 | if (state->frameno && !state->in_callback_fn) |
8694 | return 0; | |
8695 | ||
fd978bf7 | 8696 | for (i = 0; i < state->acquired_refs; i++) { |
9d9d00ac KKD |
8697 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) |
8698 | continue; | |
fd978bf7 JS |
8699 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
8700 | state->refs[i].id, state->refs[i].insn_idx); | |
9d9d00ac | 8701 | refs_lingering = true; |
fd978bf7 | 8702 | } |
9d9d00ac | 8703 | return refs_lingering ? -EINVAL : 0; |
fd978bf7 JS |
8704 | } |
8705 | ||
7b15523a FR |
8706 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
8707 | struct bpf_reg_state *regs) | |
8708 | { | |
8709 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
8710 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
8711 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
78aa1cc9 | 8712 | struct bpf_bprintf_data data = {}; |
7b15523a FR |
8713 | int err, fmt_map_off, num_args; |
8714 | u64 fmt_addr; | |
8715 | char *fmt; | |
8716 | ||
8717 | /* data must be an array of u64 */ | |
8718 | if (data_len_reg->var_off.value % 8) | |
8719 | return -EINVAL; | |
8720 | num_args = data_len_reg->var_off.value / 8; | |
8721 | ||
8722 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
8723 | * and map_direct_value_addr is set. | |
8724 | */ | |
8725 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
8726 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
8727 | fmt_map_off); | |
8e8ee109 FR |
8728 | if (err) { |
8729 | verbose(env, "verifier bug\n"); | |
8730 | return -EFAULT; | |
8731 | } | |
7b15523a FR |
8732 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
8733 | ||
8734 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
8735 | * can focus on validating the format specifiers. | |
8736 | */ | |
78aa1cc9 | 8737 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, num_args, &data); |
7b15523a FR |
8738 | if (err < 0) |
8739 | verbose(env, "Invalid format string\n"); | |
8740 | ||
8741 | return err; | |
8742 | } | |
8743 | ||
9b99edca JO |
8744 | static int check_get_func_ip(struct bpf_verifier_env *env) |
8745 | { | |
9b99edca JO |
8746 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
8747 | int func_id = BPF_FUNC_get_func_ip; | |
8748 | ||
8749 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 8750 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
8751 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
8752 | func_id_name(func_id), func_id); | |
8753 | return -ENOTSUPP; | |
8754 | } | |
8755 | return 0; | |
9ffd9f3f JO |
8756 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
8757 | return 0; | |
9b99edca JO |
8758 | } |
8759 | ||
8760 | verbose(env, "func %s#%d not supported for program type %d\n", | |
8761 | func_id_name(func_id), func_id, type); | |
8762 | return -ENOTSUPP; | |
8763 | } | |
8764 | ||
1ade2371 EZ |
8765 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
8766 | { | |
8767 | return &env->insn_aux_data[env->insn_idx]; | |
8768 | } | |
8769 | ||
8770 | static bool loop_flag_is_zero(struct bpf_verifier_env *env) | |
8771 | { | |
8772 | struct bpf_reg_state *regs = cur_regs(env); | |
8773 | struct bpf_reg_state *reg = ®s[BPF_REG_4]; | |
8774 | bool reg_is_null = register_is_null(reg); | |
8775 | ||
8776 | if (reg_is_null) | |
8777 | mark_chain_precision(env, BPF_REG_4); | |
8778 | ||
8779 | return reg_is_null; | |
8780 | } | |
8781 | ||
8782 | static void update_loop_inline_state(struct bpf_verifier_env *env, u32 subprogno) | |
8783 | { | |
8784 | struct bpf_loop_inline_state *state = &cur_aux(env)->loop_inline_state; | |
8785 | ||
8786 | if (!state->initialized) { | |
8787 | state->initialized = 1; | |
8788 | state->fit_for_inline = loop_flag_is_zero(env); | |
8789 | state->callback_subprogno = subprogno; | |
8790 | return; | |
8791 | } | |
8792 | ||
8793 | if (!state->fit_for_inline) | |
8794 | return; | |
8795 | ||
8796 | state->fit_for_inline = (loop_flag_is_zero(env) && | |
8797 | state->callback_subprogno == subprogno); | |
8798 | } | |
8799 | ||
69c087ba YS |
8800 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8801 | int *insn_idx_p) | |
17a52670 | 8802 | { |
aef9d4a3 | 8803 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
17a52670 | 8804 | const struct bpf_func_proto *fn = NULL; |
3c480732 | 8805 | enum bpf_return_type ret_type; |
c25b2ae1 | 8806 | enum bpf_type_flag ret_flag; |
638f5b90 | 8807 | struct bpf_reg_state *regs; |
33ff9823 | 8808 | struct bpf_call_arg_meta meta; |
69c087ba | 8809 | int insn_idx = *insn_idx_p; |
969bf05e | 8810 | bool changes_data; |
69c087ba | 8811 | int i, err, func_id; |
17a52670 AS |
8812 | |
8813 | /* find function prototype */ | |
69c087ba | 8814 | func_id = insn->imm; |
17a52670 | 8815 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
8816 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
8817 | func_id); | |
17a52670 AS |
8818 | return -EINVAL; |
8819 | } | |
8820 | ||
00176a34 | 8821 | if (env->ops->get_func_proto) |
5e43f899 | 8822 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 8823 | if (!fn) { |
61bd5218 JK |
8824 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
8825 | func_id); | |
17a52670 AS |
8826 | return -EINVAL; |
8827 | } | |
8828 | ||
8829 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 8830 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 8831 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
8832 | return -EINVAL; |
8833 | } | |
8834 | ||
eae2e83e JO |
8835 | if (fn->allowed && !fn->allowed(env->prog)) { |
8836 | verbose(env, "helper call is not allowed in probe\n"); | |
8837 | return -EINVAL; | |
8838 | } | |
8839 | ||
01685c5b YS |
8840 | if (!env->prog->aux->sleepable && fn->might_sleep) { |
8841 | verbose(env, "helper call might sleep in a non-sleepable prog\n"); | |
8842 | return -EINVAL; | |
8843 | } | |
8844 | ||
04514d13 | 8845 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 8846 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
8847 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
8848 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
8849 | func_id_name(func_id), func_id); | |
8850 | return -EINVAL; | |
8851 | } | |
969bf05e | 8852 | |
33ff9823 | 8853 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 8854 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 8855 | |
0c9a7a7e | 8856 | err = check_func_proto(fn, func_id); |
435faee1 | 8857 | if (err) { |
61bd5218 | 8858 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 8859 | func_id_name(func_id), func_id); |
435faee1 DB |
8860 | return err; |
8861 | } | |
8862 | ||
9bb00b28 YS |
8863 | if (env->cur_state->active_rcu_lock) { |
8864 | if (fn->might_sleep) { | |
8865 | verbose(env, "sleepable helper %s#%d in rcu_read_lock region\n", | |
8866 | func_id_name(func_id), func_id); | |
8867 | return -EINVAL; | |
8868 | } | |
8869 | ||
8870 | if (env->prog->aux->sleepable && is_storage_get_function(func_id)) | |
8871 | env->insn_aux_data[insn_idx].storage_get_func_atomic = true; | |
8872 | } | |
8873 | ||
d83525ca | 8874 | meta.func_id = func_id; |
17a52670 | 8875 | /* check args */ |
523a4cf4 | 8876 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
1d18feb2 | 8877 | err = check_func_arg(env, i, &meta, fn, insn_idx); |
a7658e1a AS |
8878 | if (err) |
8879 | return err; | |
8880 | } | |
17a52670 | 8881 | |
c93552c4 DB |
8882 | err = record_func_map(env, &meta, func_id, insn_idx); |
8883 | if (err) | |
8884 | return err; | |
8885 | ||
d2e4c1e6 DB |
8886 | err = record_func_key(env, &meta, func_id, insn_idx); |
8887 | if (err) | |
8888 | return err; | |
8889 | ||
435faee1 DB |
8890 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
8891 | * is inferred from register state. | |
8892 | */ | |
8893 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
8894 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
8895 | BPF_WRITE, -1, false); | |
435faee1 DB |
8896 | if (err) |
8897 | return err; | |
8898 | } | |
8899 | ||
8f14852e KKD |
8900 | regs = cur_regs(env); |
8901 | ||
8902 | if (meta.release_regno) { | |
8903 | err = -EINVAL; | |
27060531 KKD |
8904 | /* This can only be set for PTR_TO_STACK, as CONST_PTR_TO_DYNPTR cannot |
8905 | * be released by any dynptr helper. Hence, unmark_stack_slots_dynptr | |
8906 | * is safe to do directly. | |
8907 | */ | |
8908 | if (arg_type_is_dynptr(fn->arg_type[meta.release_regno - BPF_REG_1])) { | |
8909 | if (regs[meta.release_regno].type == CONST_PTR_TO_DYNPTR) { | |
8910 | verbose(env, "verifier internal error: CONST_PTR_TO_DYNPTR cannot be released\n"); | |
8911 | return -EFAULT; | |
8912 | } | |
97e03f52 | 8913 | err = unmark_stack_slots_dynptr(env, ®s[meta.release_regno]); |
27060531 | 8914 | } else if (meta.ref_obj_id) { |
8f14852e | 8915 | err = release_reference(env, meta.ref_obj_id); |
27060531 KKD |
8916 | } else if (register_is_null(®s[meta.release_regno])) { |
8917 | /* meta.ref_obj_id can only be 0 if register that is meant to be | |
8918 | * released is NULL, which must be > R0. | |
8919 | */ | |
8f14852e | 8920 | err = 0; |
27060531 | 8921 | } |
46f8bc92 MKL |
8922 | if (err) { |
8923 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
8924 | func_id_name(func_id), func_id); | |
fd978bf7 | 8925 | return err; |
46f8bc92 | 8926 | } |
fd978bf7 JS |
8927 | } |
8928 | ||
e6f2dd0f JK |
8929 | switch (func_id) { |
8930 | case BPF_FUNC_tail_call: | |
8931 | err = check_reference_leak(env); | |
8932 | if (err) { | |
8933 | verbose(env, "tail_call would lead to reference leak\n"); | |
8934 | return err; | |
8935 | } | |
8936 | break; | |
8937 | case BPF_FUNC_get_local_storage: | |
8938 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
8939 | * this is required because get_local_storage() can't return an error. | |
8940 | */ | |
8941 | if (!register_is_null(®s[BPF_REG_2])) { | |
8942 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
8943 | return -EINVAL; | |
8944 | } | |
8945 | break; | |
8946 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
8947 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8948 | set_map_elem_callback_state); | |
e6f2dd0f JK |
8949 | break; |
8950 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
8951 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8952 | set_timer_callback_state); | |
e6f2dd0f JK |
8953 | break; |
8954 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
8955 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8956 | set_find_vma_callback_state); | |
e6f2dd0f JK |
8957 | break; |
8958 | case BPF_FUNC_snprintf: | |
7b15523a | 8959 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
8960 | break; |
8961 | case BPF_FUNC_loop: | |
1ade2371 | 8962 | update_loop_inline_state(env, meta.subprogno); |
e6f2dd0f JK |
8963 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
8964 | set_loop_callback_state); | |
8965 | break; | |
263ae152 JK |
8966 | case BPF_FUNC_dynptr_from_mem: |
8967 | if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) { | |
8968 | verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n", | |
8969 | reg_type_str(env, regs[BPF_REG_1].type)); | |
8970 | return -EACCES; | |
8971 | } | |
69fd337a SF |
8972 | break; |
8973 | case BPF_FUNC_set_retval: | |
aef9d4a3 SF |
8974 | if (prog_type == BPF_PROG_TYPE_LSM && |
8975 | env->prog->expected_attach_type == BPF_LSM_CGROUP) { | |
69fd337a SF |
8976 | if (!env->prog->aux->attach_func_proto->type) { |
8977 | /* Make sure programs that attach to void | |
8978 | * hooks don't try to modify return value. | |
8979 | */ | |
8980 | verbose(env, "BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
8981 | return -EINVAL; | |
8982 | } | |
8983 | } | |
8984 | break; | |
88374342 | 8985 | case BPF_FUNC_dynptr_data: |
485ec51e JK |
8986 | { |
8987 | struct bpf_reg_state *reg; | |
8988 | int id, ref_obj_id; | |
20571567 | 8989 | |
485ec51e JK |
8990 | reg = get_dynptr_arg_reg(env, fn, regs); |
8991 | if (!reg) | |
8992 | return -EFAULT; | |
f8064ab9 | 8993 | |
f8064ab9 | 8994 | |
485ec51e JK |
8995 | if (meta.dynptr_id) { |
8996 | verbose(env, "verifier internal error: meta.dynptr_id already set\n"); | |
8997 | return -EFAULT; | |
88374342 | 8998 | } |
485ec51e JK |
8999 | if (meta.ref_obj_id) { |
9000 | verbose(env, "verifier internal error: meta.ref_obj_id already set\n"); | |
88374342 JK |
9001 | return -EFAULT; |
9002 | } | |
485ec51e JK |
9003 | |
9004 | id = dynptr_id(env, reg); | |
9005 | if (id < 0) { | |
9006 | verbose(env, "verifier internal error: failed to obtain dynptr id\n"); | |
9007 | return id; | |
9008 | } | |
9009 | ||
9010 | ref_obj_id = dynptr_ref_obj_id(env, reg); | |
9011 | if (ref_obj_id < 0) { | |
9012 | verbose(env, "verifier internal error: failed to obtain dynptr ref_obj_id\n"); | |
9013 | return ref_obj_id; | |
9014 | } | |
9015 | ||
9016 | meta.dynptr_id = id; | |
9017 | meta.ref_obj_id = ref_obj_id; | |
9018 | ||
88374342 | 9019 | break; |
485ec51e | 9020 | } |
b5964b96 JK |
9021 | case BPF_FUNC_dynptr_write: |
9022 | { | |
9023 | enum bpf_dynptr_type dynptr_type; | |
9024 | struct bpf_reg_state *reg; | |
9025 | ||
9026 | reg = get_dynptr_arg_reg(env, fn, regs); | |
9027 | if (!reg) | |
9028 | return -EFAULT; | |
9029 | ||
9030 | dynptr_type = dynptr_get_type(env, reg); | |
9031 | if (dynptr_type == BPF_DYNPTR_TYPE_INVALID) | |
9032 | return -EFAULT; | |
9033 | ||
9034 | if (dynptr_type == BPF_DYNPTR_TYPE_SKB) | |
9035 | /* this will trigger clear_all_pkt_pointers(), which will | |
9036 | * invalidate all dynptr slices associated with the skb | |
9037 | */ | |
9038 | changes_data = true; | |
9039 | ||
9040 | break; | |
9041 | } | |
20571567 DV |
9042 | case BPF_FUNC_user_ringbuf_drain: |
9043 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
9044 | set_user_ringbuf_callback_state); | |
9045 | break; | |
7b15523a FR |
9046 | } |
9047 | ||
e6f2dd0f JK |
9048 | if (err) |
9049 | return err; | |
9050 | ||
17a52670 | 9051 | /* reset caller saved regs */ |
dc503a8a | 9052 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 9053 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
9054 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
9055 | } | |
17a52670 | 9056 | |
5327ed3d JW |
9057 | /* helper call returns 64-bit value. */ |
9058 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
9059 | ||
dc503a8a | 9060 | /* update return register (already marked as written above) */ |
3c480732 | 9061 | ret_type = fn->ret_type; |
0c9a7a7e JK |
9062 | ret_flag = type_flag(ret_type); |
9063 | ||
9064 | switch (base_type(ret_type)) { | |
9065 | case RET_INTEGER: | |
f1174f77 | 9066 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 9067 | mark_reg_unknown(env, regs, BPF_REG_0); |
0c9a7a7e JK |
9068 | break; |
9069 | case RET_VOID: | |
17a52670 | 9070 | regs[BPF_REG_0].type = NOT_INIT; |
0c9a7a7e JK |
9071 | break; |
9072 | case RET_PTR_TO_MAP_VALUE: | |
f1174f77 | 9073 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 9074 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
9075 | /* remember map_ptr, so that check_map_access() |
9076 | * can check 'value_size' boundary of memory access | |
9077 | * to map element returned from bpf_map_lookup_elem() | |
9078 | */ | |
33ff9823 | 9079 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
9080 | verbose(env, |
9081 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
9082 | return -EINVAL; |
9083 | } | |
33ff9823 | 9084 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 9085 | regs[BPF_REG_0].map_uid = meta.map_uid; |
c25b2ae1 HL |
9086 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
9087 | if (!type_may_be_null(ret_type) && | |
db559117 | 9088 | btf_record_has_field(meta.map_ptr->record, BPF_SPIN_LOCK)) { |
c25b2ae1 | 9089 | regs[BPF_REG_0].id = ++env->id_gen; |
4d31f301 | 9090 | } |
0c9a7a7e JK |
9091 | break; |
9092 | case RET_PTR_TO_SOCKET: | |
c64b7983 | 9093 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9094 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
0c9a7a7e JK |
9095 | break; |
9096 | case RET_PTR_TO_SOCK_COMMON: | |
85a51f8c | 9097 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9098 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
0c9a7a7e JK |
9099 | break; |
9100 | case RET_PTR_TO_TCP_SOCK: | |
655a51e5 | 9101 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9102 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
0c9a7a7e | 9103 | break; |
2de2669b | 9104 | case RET_PTR_TO_MEM: |
457f4436 | 9105 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9106 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
457f4436 | 9107 | regs[BPF_REG_0].mem_size = meta.mem_size; |
0c9a7a7e JK |
9108 | break; |
9109 | case RET_PTR_TO_MEM_OR_BTF_ID: | |
9110 | { | |
eaa6bcb7 HL |
9111 | const struct btf_type *t; |
9112 | ||
9113 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 9114 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
9115 | if (!btf_type_is_struct(t)) { |
9116 | u32 tsize; | |
9117 | const struct btf_type *ret; | |
9118 | const char *tname; | |
9119 | ||
9120 | /* resolve the type size of ksym. */ | |
22dc4a0f | 9121 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 9122 | if (IS_ERR(ret)) { |
22dc4a0f | 9123 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
9124 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
9125 | tname, PTR_ERR(ret)); | |
9126 | return -EINVAL; | |
9127 | } | |
c25b2ae1 | 9128 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
eaa6bcb7 HL |
9129 | regs[BPF_REG_0].mem_size = tsize; |
9130 | } else { | |
34d3a78c HL |
9131 | /* MEM_RDONLY may be carried from ret_flag, but it |
9132 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise | |
9133 | * it will confuse the check of PTR_TO_BTF_ID in | |
9134 | * check_mem_access(). | |
9135 | */ | |
9136 | ret_flag &= ~MEM_RDONLY; | |
9137 | ||
c25b2ae1 | 9138 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
22dc4a0f | 9139 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
9140 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
9141 | } | |
0c9a7a7e JK |
9142 | break; |
9143 | } | |
9144 | case RET_PTR_TO_BTF_ID: | |
9145 | { | |
c0a5a21c | 9146 | struct btf *ret_btf; |
af7ec138 YS |
9147 | int ret_btf_id; |
9148 | ||
9149 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
c25b2ae1 | 9150 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
c0a5a21c | 9151 | if (func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac KKD |
9152 | ret_btf = meta.kptr_field->kptr.btf; |
9153 | ret_btf_id = meta.kptr_field->kptr.btf_id; | |
c0a5a21c | 9154 | } else { |
47e34cb7 DM |
9155 | if (fn->ret_btf_id == BPF_PTR_POISON) { |
9156 | verbose(env, "verifier internal error:"); | |
9157 | verbose(env, "func %s has non-overwritten BPF_PTR_POISON return type\n", | |
9158 | func_id_name(func_id)); | |
9159 | return -EINVAL; | |
9160 | } | |
c0a5a21c KKD |
9161 | ret_btf = btf_vmlinux; |
9162 | ret_btf_id = *fn->ret_btf_id; | |
9163 | } | |
af7ec138 | 9164 | if (ret_btf_id == 0) { |
3c480732 HL |
9165 | verbose(env, "invalid return type %u of func %s#%d\n", |
9166 | base_type(ret_type), func_id_name(func_id), | |
9167 | func_id); | |
af7ec138 YS |
9168 | return -EINVAL; |
9169 | } | |
c0a5a21c | 9170 | regs[BPF_REG_0].btf = ret_btf; |
af7ec138 | 9171 | regs[BPF_REG_0].btf_id = ret_btf_id; |
0c9a7a7e JK |
9172 | break; |
9173 | } | |
9174 | default: | |
3c480732 HL |
9175 | verbose(env, "unknown return type %u of func %s#%d\n", |
9176 | base_type(ret_type), func_id_name(func_id), func_id); | |
17a52670 AS |
9177 | return -EINVAL; |
9178 | } | |
04fd61ab | 9179 | |
c25b2ae1 | 9180 | if (type_may_be_null(regs[BPF_REG_0].type)) |
93c230e3 MKL |
9181 | regs[BPF_REG_0].id = ++env->id_gen; |
9182 | ||
b2d8ef19 DM |
9183 | if (helper_multiple_ref_obj_use(func_id, meta.map_ptr)) { |
9184 | verbose(env, "verifier internal error: func %s#%d sets ref_obj_id more than once\n", | |
9185 | func_id_name(func_id), func_id); | |
9186 | return -EFAULT; | |
9187 | } | |
9188 | ||
f8064ab9 KKD |
9189 | if (is_dynptr_ref_function(func_id)) |
9190 | regs[BPF_REG_0].dynptr_id = meta.dynptr_id; | |
9191 | ||
88374342 | 9192 | if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) { |
1b986589 MKL |
9193 | /* For release_reference() */ |
9194 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 9195 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
9196 | int id = acquire_reference_state(env, insn_idx); |
9197 | ||
9198 | if (id < 0) | |
9199 | return id; | |
9200 | /* For mark_ptr_or_null_reg() */ | |
9201 | regs[BPF_REG_0].id = id; | |
9202 | /* For release_reference() */ | |
9203 | regs[BPF_REG_0].ref_obj_id = id; | |
9204 | } | |
1b986589 | 9205 | |
849fa506 YS |
9206 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
9207 | ||
61bd5218 | 9208 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
9209 | if (err) |
9210 | return err; | |
04fd61ab | 9211 | |
fa28dcb8 SL |
9212 | if ((func_id == BPF_FUNC_get_stack || |
9213 | func_id == BPF_FUNC_get_task_stack) && | |
9214 | !env->prog->has_callchain_buf) { | |
c195651e YS |
9215 | const char *err_str; |
9216 | ||
9217 | #ifdef CONFIG_PERF_EVENTS | |
9218 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
9219 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
9220 | #else | |
9221 | err = -ENOTSUPP; | |
9222 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
9223 | #endif | |
9224 | if (err) { | |
9225 | verbose(env, err_str, func_id_name(func_id), func_id); | |
9226 | return err; | |
9227 | } | |
9228 | ||
9229 | env->prog->has_callchain_buf = true; | |
9230 | } | |
9231 | ||
5d99cb2c SL |
9232 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
9233 | env->prog->call_get_stack = true; | |
9234 | ||
9b99edca JO |
9235 | if (func_id == BPF_FUNC_get_func_ip) { |
9236 | if (check_get_func_ip(env)) | |
9237 | return -ENOTSUPP; | |
9238 | env->prog->call_get_func_ip = true; | |
9239 | } | |
9240 | ||
969bf05e AS |
9241 | if (changes_data) |
9242 | clear_all_pkt_pointers(env); | |
9243 | return 0; | |
9244 | } | |
9245 | ||
e6ac2450 MKL |
9246 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
9247 | * the BTF func_proto's return value size and argument. | |
9248 | */ | |
9249 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
9250 | size_t reg_size) | |
9251 | { | |
9252 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
9253 | ||
9254 | if (regno == BPF_REG_0) { | |
9255 | /* Function return value */ | |
9256 | reg->live |= REG_LIVE_WRITTEN; | |
9257 | reg->subreg_def = reg_size == sizeof(u64) ? | |
9258 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
9259 | } else { | |
9260 | /* Function argument */ | |
9261 | if (reg_size == sizeof(u64)) { | |
9262 | mark_insn_zext(env, reg); | |
9263 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
9264 | } else { | |
9265 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
9266 | } | |
9267 | } | |
9268 | } | |
9269 | ||
00b85860 KKD |
9270 | static bool is_kfunc_acquire(struct bpf_kfunc_call_arg_meta *meta) |
9271 | { | |
9272 | return meta->kfunc_flags & KF_ACQUIRE; | |
9273 | } | |
a5d82727 | 9274 | |
00b85860 KKD |
9275 | static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) |
9276 | { | |
9277 | return meta->kfunc_flags & KF_RET_NULL; | |
9278 | } | |
2357672c | 9279 | |
00b85860 KKD |
9280 | static bool is_kfunc_release(struct bpf_kfunc_call_arg_meta *meta) |
9281 | { | |
9282 | return meta->kfunc_flags & KF_RELEASE; | |
9283 | } | |
e6ac2450 | 9284 | |
00b85860 KKD |
9285 | static bool is_kfunc_trusted_args(struct bpf_kfunc_call_arg_meta *meta) |
9286 | { | |
9287 | return meta->kfunc_flags & KF_TRUSTED_ARGS; | |
9288 | } | |
4dd48c6f | 9289 | |
00b85860 KKD |
9290 | static bool is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta) |
9291 | { | |
9292 | return meta->kfunc_flags & KF_SLEEPABLE; | |
9293 | } | |
5c073f26 | 9294 | |
00b85860 KKD |
9295 | static bool is_kfunc_destructive(struct bpf_kfunc_call_arg_meta *meta) |
9296 | { | |
9297 | return meta->kfunc_flags & KF_DESTRUCTIVE; | |
9298 | } | |
eb1f7f71 | 9299 | |
fca1aa75 YS |
9300 | static bool is_kfunc_rcu(struct bpf_kfunc_call_arg_meta *meta) |
9301 | { | |
9302 | return meta->kfunc_flags & KF_RCU; | |
9303 | } | |
9304 | ||
00b85860 KKD |
9305 | static bool is_kfunc_arg_kptr_get(struct bpf_kfunc_call_arg_meta *meta, int arg) |
9306 | { | |
9307 | return arg == 0 && (meta->kfunc_flags & KF_KPTR_GET); | |
9308 | } | |
e6ac2450 | 9309 | |
a50388db KKD |
9310 | static bool __kfunc_param_match_suffix(const struct btf *btf, |
9311 | const struct btf_param *arg, | |
9312 | const char *suffix) | |
00b85860 | 9313 | { |
a50388db | 9314 | int suffix_len = strlen(suffix), len; |
00b85860 | 9315 | const char *param_name; |
e6ac2450 | 9316 | |
00b85860 KKD |
9317 | /* In the future, this can be ported to use BTF tagging */ |
9318 | param_name = btf_name_by_offset(btf, arg->name_off); | |
9319 | if (str_is_empty(param_name)) | |
9320 | return false; | |
9321 | len = strlen(param_name); | |
a50388db | 9322 | if (len < suffix_len) |
00b85860 | 9323 | return false; |
a50388db KKD |
9324 | param_name += len - suffix_len; |
9325 | return !strncmp(param_name, suffix, suffix_len); | |
9326 | } | |
5c073f26 | 9327 | |
a50388db KKD |
9328 | static bool is_kfunc_arg_mem_size(const struct btf *btf, |
9329 | const struct btf_param *arg, | |
9330 | const struct bpf_reg_state *reg) | |
9331 | { | |
9332 | const struct btf_type *t; | |
5c073f26 | 9333 | |
a50388db KKD |
9334 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
9335 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
00b85860 | 9336 | return false; |
eb1f7f71 | 9337 | |
a50388db KKD |
9338 | return __kfunc_param_match_suffix(btf, arg, "__sz"); |
9339 | } | |
eb1f7f71 | 9340 | |
66e3a13e JK |
9341 | static bool is_kfunc_arg_const_mem_size(const struct btf *btf, |
9342 | const struct btf_param *arg, | |
9343 | const struct bpf_reg_state *reg) | |
9344 | { | |
9345 | const struct btf_type *t; | |
9346 | ||
9347 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
9348 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
9349 | return false; | |
9350 | ||
9351 | return __kfunc_param_match_suffix(btf, arg, "__szk"); | |
9352 | } | |
9353 | ||
a50388db KKD |
9354 | static bool is_kfunc_arg_constant(const struct btf *btf, const struct btf_param *arg) |
9355 | { | |
9356 | return __kfunc_param_match_suffix(btf, arg, "__k"); | |
00b85860 | 9357 | } |
eb1f7f71 | 9358 | |
958cf2e2 KKD |
9359 | static bool is_kfunc_arg_ignore(const struct btf *btf, const struct btf_param *arg) |
9360 | { | |
9361 | return __kfunc_param_match_suffix(btf, arg, "__ign"); | |
9362 | } | |
5c073f26 | 9363 | |
ac9f0605 KKD |
9364 | static bool is_kfunc_arg_alloc_obj(const struct btf *btf, const struct btf_param *arg) |
9365 | { | |
9366 | return __kfunc_param_match_suffix(btf, arg, "__alloc"); | |
9367 | } | |
e6ac2450 | 9368 | |
d96d937d JK |
9369 | static bool is_kfunc_arg_uninit(const struct btf *btf, const struct btf_param *arg) |
9370 | { | |
9371 | return __kfunc_param_match_suffix(btf, arg, "__uninit"); | |
9372 | } | |
9373 | ||
00b85860 KKD |
9374 | static bool is_kfunc_arg_scalar_with_name(const struct btf *btf, |
9375 | const struct btf_param *arg, | |
9376 | const char *name) | |
9377 | { | |
9378 | int len, target_len = strlen(name); | |
9379 | const char *param_name; | |
e6ac2450 | 9380 | |
00b85860 KKD |
9381 | param_name = btf_name_by_offset(btf, arg->name_off); |
9382 | if (str_is_empty(param_name)) | |
9383 | return false; | |
9384 | len = strlen(param_name); | |
9385 | if (len != target_len) | |
9386 | return false; | |
9387 | if (strcmp(param_name, name)) | |
9388 | return false; | |
e6ac2450 | 9389 | |
00b85860 | 9390 | return true; |
e6ac2450 MKL |
9391 | } |
9392 | ||
00b85860 KKD |
9393 | enum { |
9394 | KF_ARG_DYNPTR_ID, | |
8cab76ec KKD |
9395 | KF_ARG_LIST_HEAD_ID, |
9396 | KF_ARG_LIST_NODE_ID, | |
cd6791b4 DM |
9397 | KF_ARG_RB_ROOT_ID, |
9398 | KF_ARG_RB_NODE_ID, | |
00b85860 | 9399 | }; |
b03c9f9f | 9400 | |
00b85860 KKD |
9401 | BTF_ID_LIST(kf_arg_btf_ids) |
9402 | BTF_ID(struct, bpf_dynptr_kern) | |
8cab76ec KKD |
9403 | BTF_ID(struct, bpf_list_head) |
9404 | BTF_ID(struct, bpf_list_node) | |
bd1279ae DM |
9405 | BTF_ID(struct, bpf_rb_root) |
9406 | BTF_ID(struct, bpf_rb_node) | |
b03c9f9f | 9407 | |
8cab76ec KKD |
9408 | static bool __is_kfunc_ptr_arg_type(const struct btf *btf, |
9409 | const struct btf_param *arg, int type) | |
3f50f132 | 9410 | { |
00b85860 KKD |
9411 | const struct btf_type *t; |
9412 | u32 res_id; | |
3f50f132 | 9413 | |
00b85860 KKD |
9414 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
9415 | if (!t) | |
9416 | return false; | |
9417 | if (!btf_type_is_ptr(t)) | |
9418 | return false; | |
9419 | t = btf_type_skip_modifiers(btf, t->type, &res_id); | |
9420 | if (!t) | |
9421 | return false; | |
8cab76ec | 9422 | return btf_types_are_same(btf, res_id, btf_vmlinux, kf_arg_btf_ids[type]); |
3f50f132 JF |
9423 | } |
9424 | ||
8cab76ec | 9425 | static bool is_kfunc_arg_dynptr(const struct btf *btf, const struct btf_param *arg) |
b03c9f9f | 9426 | { |
8cab76ec | 9427 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_DYNPTR_ID); |
969bf05e AS |
9428 | } |
9429 | ||
8cab76ec | 9430 | static bool is_kfunc_arg_list_head(const struct btf *btf, const struct btf_param *arg) |
3f50f132 | 9431 | { |
8cab76ec | 9432 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_HEAD_ID); |
3f50f132 JF |
9433 | } |
9434 | ||
8cab76ec | 9435 | static bool is_kfunc_arg_list_node(const struct btf *btf, const struct btf_param *arg) |
bb7f0f98 | 9436 | { |
8cab76ec | 9437 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_NODE_ID); |
00b85860 KKD |
9438 | } |
9439 | ||
cd6791b4 DM |
9440 | static bool is_kfunc_arg_rbtree_root(const struct btf *btf, const struct btf_param *arg) |
9441 | { | |
9442 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_ROOT_ID); | |
9443 | } | |
9444 | ||
9445 | static bool is_kfunc_arg_rbtree_node(const struct btf *btf, const struct btf_param *arg) | |
9446 | { | |
9447 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_NODE_ID); | |
9448 | } | |
9449 | ||
5d92ddc3 DM |
9450 | static bool is_kfunc_arg_callback(struct bpf_verifier_env *env, const struct btf *btf, |
9451 | const struct btf_param *arg) | |
9452 | { | |
9453 | const struct btf_type *t; | |
9454 | ||
9455 | t = btf_type_resolve_func_ptr(btf, arg->type, NULL); | |
9456 | if (!t) | |
9457 | return false; | |
9458 | ||
9459 | return true; | |
9460 | } | |
9461 | ||
00b85860 KKD |
9462 | /* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ |
9463 | static bool __btf_type_is_scalar_struct(struct bpf_verifier_env *env, | |
9464 | const struct btf *btf, | |
9465 | const struct btf_type *t, int rec) | |
9466 | { | |
9467 | const struct btf_type *member_type; | |
9468 | const struct btf_member *member; | |
9469 | u32 i; | |
9470 | ||
9471 | if (!btf_type_is_struct(t)) | |
9472 | return false; | |
9473 | ||
9474 | for_each_member(i, t, member) { | |
9475 | const struct btf_array *array; | |
9476 | ||
9477 | member_type = btf_type_skip_modifiers(btf, member->type, NULL); | |
9478 | if (btf_type_is_struct(member_type)) { | |
9479 | if (rec >= 3) { | |
9480 | verbose(env, "max struct nesting depth exceeded\n"); | |
9481 | return false; | |
9482 | } | |
9483 | if (!__btf_type_is_scalar_struct(env, btf, member_type, rec + 1)) | |
9484 | return false; | |
9485 | continue; | |
9486 | } | |
9487 | if (btf_type_is_array(member_type)) { | |
9488 | array = btf_array(member_type); | |
9489 | if (!array->nelems) | |
9490 | return false; | |
9491 | member_type = btf_type_skip_modifiers(btf, array->type, NULL); | |
9492 | if (!btf_type_is_scalar(member_type)) | |
9493 | return false; | |
9494 | continue; | |
9495 | } | |
9496 | if (!btf_type_is_scalar(member_type)) | |
9497 | return false; | |
9498 | } | |
9499 | return true; | |
9500 | } | |
9501 | ||
9502 | ||
9503 | static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { | |
9504 | #ifdef CONFIG_NET | |
9505 | [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], | |
9506 | [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
9507 | [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], | |
9508 | #endif | |
9509 | }; | |
9510 | ||
9511 | enum kfunc_ptr_arg_type { | |
9512 | KF_ARG_PTR_TO_CTX, | |
ac9f0605 | 9513 | KF_ARG_PTR_TO_ALLOC_BTF_ID, /* Allocated object */ |
00b85860 KKD |
9514 | KF_ARG_PTR_TO_KPTR, /* PTR_TO_KPTR but type specific */ |
9515 | KF_ARG_PTR_TO_DYNPTR, | |
06accc87 | 9516 | KF_ARG_PTR_TO_ITER, |
8cab76ec KKD |
9517 | KF_ARG_PTR_TO_LIST_HEAD, |
9518 | KF_ARG_PTR_TO_LIST_NODE, | |
00b85860 KKD |
9519 | KF_ARG_PTR_TO_BTF_ID, /* Also covers reg2btf_ids conversions */ |
9520 | KF_ARG_PTR_TO_MEM, | |
9521 | KF_ARG_PTR_TO_MEM_SIZE, /* Size derived from next argument, skip it */ | |
5d92ddc3 | 9522 | KF_ARG_PTR_TO_CALLBACK, |
cd6791b4 DM |
9523 | KF_ARG_PTR_TO_RB_ROOT, |
9524 | KF_ARG_PTR_TO_RB_NODE, | |
00b85860 KKD |
9525 | }; |
9526 | ||
ac9f0605 KKD |
9527 | enum special_kfunc_type { |
9528 | KF_bpf_obj_new_impl, | |
9529 | KF_bpf_obj_drop_impl, | |
8cab76ec KKD |
9530 | KF_bpf_list_push_front, |
9531 | KF_bpf_list_push_back, | |
9532 | KF_bpf_list_pop_front, | |
9533 | KF_bpf_list_pop_back, | |
fd264ca0 | 9534 | KF_bpf_cast_to_kern_ctx, |
a35b9af4 | 9535 | KF_bpf_rdonly_cast, |
9bb00b28 YS |
9536 | KF_bpf_rcu_read_lock, |
9537 | KF_bpf_rcu_read_unlock, | |
bd1279ae DM |
9538 | KF_bpf_rbtree_remove, |
9539 | KF_bpf_rbtree_add, | |
9540 | KF_bpf_rbtree_first, | |
b5964b96 | 9541 | KF_bpf_dynptr_from_skb, |
05421aec | 9542 | KF_bpf_dynptr_from_xdp, |
66e3a13e JK |
9543 | KF_bpf_dynptr_slice, |
9544 | KF_bpf_dynptr_slice_rdwr, | |
ac9f0605 KKD |
9545 | }; |
9546 | ||
9547 | BTF_SET_START(special_kfunc_set) | |
9548 | BTF_ID(func, bpf_obj_new_impl) | |
9549 | BTF_ID(func, bpf_obj_drop_impl) | |
8cab76ec KKD |
9550 | BTF_ID(func, bpf_list_push_front) |
9551 | BTF_ID(func, bpf_list_push_back) | |
9552 | BTF_ID(func, bpf_list_pop_front) | |
9553 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 9554 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 9555 | BTF_ID(func, bpf_rdonly_cast) |
bd1279ae DM |
9556 | BTF_ID(func, bpf_rbtree_remove) |
9557 | BTF_ID(func, bpf_rbtree_add) | |
9558 | BTF_ID(func, bpf_rbtree_first) | |
b5964b96 | 9559 | BTF_ID(func, bpf_dynptr_from_skb) |
05421aec | 9560 | BTF_ID(func, bpf_dynptr_from_xdp) |
66e3a13e JK |
9561 | BTF_ID(func, bpf_dynptr_slice) |
9562 | BTF_ID(func, bpf_dynptr_slice_rdwr) | |
ac9f0605 KKD |
9563 | BTF_SET_END(special_kfunc_set) |
9564 | ||
9565 | BTF_ID_LIST(special_kfunc_list) | |
9566 | BTF_ID(func, bpf_obj_new_impl) | |
9567 | BTF_ID(func, bpf_obj_drop_impl) | |
8cab76ec KKD |
9568 | BTF_ID(func, bpf_list_push_front) |
9569 | BTF_ID(func, bpf_list_push_back) | |
9570 | BTF_ID(func, bpf_list_pop_front) | |
9571 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 9572 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 9573 | BTF_ID(func, bpf_rdonly_cast) |
9bb00b28 YS |
9574 | BTF_ID(func, bpf_rcu_read_lock) |
9575 | BTF_ID(func, bpf_rcu_read_unlock) | |
bd1279ae DM |
9576 | BTF_ID(func, bpf_rbtree_remove) |
9577 | BTF_ID(func, bpf_rbtree_add) | |
9578 | BTF_ID(func, bpf_rbtree_first) | |
b5964b96 | 9579 | BTF_ID(func, bpf_dynptr_from_skb) |
05421aec | 9580 | BTF_ID(func, bpf_dynptr_from_xdp) |
66e3a13e JK |
9581 | BTF_ID(func, bpf_dynptr_slice) |
9582 | BTF_ID(func, bpf_dynptr_slice_rdwr) | |
9bb00b28 YS |
9583 | |
9584 | static bool is_kfunc_bpf_rcu_read_lock(struct bpf_kfunc_call_arg_meta *meta) | |
9585 | { | |
9586 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_lock]; | |
9587 | } | |
9588 | ||
9589 | static bool is_kfunc_bpf_rcu_read_unlock(struct bpf_kfunc_call_arg_meta *meta) | |
9590 | { | |
9591 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_unlock]; | |
9592 | } | |
ac9f0605 | 9593 | |
00b85860 KKD |
9594 | static enum kfunc_ptr_arg_type |
9595 | get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, | |
9596 | struct bpf_kfunc_call_arg_meta *meta, | |
9597 | const struct btf_type *t, const struct btf_type *ref_t, | |
9598 | const char *ref_tname, const struct btf_param *args, | |
9599 | int argno, int nargs) | |
9600 | { | |
9601 | u32 regno = argno + 1; | |
9602 | struct bpf_reg_state *regs = cur_regs(env); | |
9603 | struct bpf_reg_state *reg = ®s[regno]; | |
9604 | bool arg_mem_size = false; | |
9605 | ||
fd264ca0 YS |
9606 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) |
9607 | return KF_ARG_PTR_TO_CTX; | |
9608 | ||
00b85860 KKD |
9609 | /* In this function, we verify the kfunc's BTF as per the argument type, |
9610 | * leaving the rest of the verification with respect to the register | |
9611 | * type to our caller. When a set of conditions hold in the BTF type of | |
9612 | * arguments, we resolve it to a known kfunc_ptr_arg_type. | |
9613 | */ | |
9614 | if (btf_get_prog_ctx_type(&env->log, meta->btf, t, resolve_prog_type(env->prog), argno)) | |
9615 | return KF_ARG_PTR_TO_CTX; | |
9616 | ||
ac9f0605 KKD |
9617 | if (is_kfunc_arg_alloc_obj(meta->btf, &args[argno])) |
9618 | return KF_ARG_PTR_TO_ALLOC_BTF_ID; | |
9619 | ||
00b85860 KKD |
9620 | if (is_kfunc_arg_kptr_get(meta, argno)) { |
9621 | if (!btf_type_is_ptr(ref_t)) { | |
9622 | verbose(env, "arg#0 BTF type must be a double pointer for kptr_get kfunc\n"); | |
9623 | return -EINVAL; | |
9624 | } | |
9625 | ref_t = btf_type_by_id(meta->btf, ref_t->type); | |
9626 | ref_tname = btf_name_by_offset(meta->btf, ref_t->name_off); | |
9627 | if (!btf_type_is_struct(ref_t)) { | |
9628 | verbose(env, "kernel function %s args#0 pointer type %s %s is not supported\n", | |
9629 | meta->func_name, btf_type_str(ref_t), ref_tname); | |
9630 | return -EINVAL; | |
9631 | } | |
9632 | return KF_ARG_PTR_TO_KPTR; | |
9633 | } | |
9634 | ||
9635 | if (is_kfunc_arg_dynptr(meta->btf, &args[argno])) | |
9636 | return KF_ARG_PTR_TO_DYNPTR; | |
9637 | ||
06accc87 AN |
9638 | if (is_kfunc_arg_iter(meta, argno)) |
9639 | return KF_ARG_PTR_TO_ITER; | |
9640 | ||
8cab76ec KKD |
9641 | if (is_kfunc_arg_list_head(meta->btf, &args[argno])) |
9642 | return KF_ARG_PTR_TO_LIST_HEAD; | |
9643 | ||
9644 | if (is_kfunc_arg_list_node(meta->btf, &args[argno])) | |
9645 | return KF_ARG_PTR_TO_LIST_NODE; | |
9646 | ||
cd6791b4 DM |
9647 | if (is_kfunc_arg_rbtree_root(meta->btf, &args[argno])) |
9648 | return KF_ARG_PTR_TO_RB_ROOT; | |
9649 | ||
9650 | if (is_kfunc_arg_rbtree_node(meta->btf, &args[argno])) | |
9651 | return KF_ARG_PTR_TO_RB_NODE; | |
9652 | ||
00b85860 KKD |
9653 | if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { |
9654 | if (!btf_type_is_struct(ref_t)) { | |
9655 | verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n", | |
9656 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
9657 | return -EINVAL; | |
9658 | } | |
9659 | return KF_ARG_PTR_TO_BTF_ID; | |
9660 | } | |
9661 | ||
5d92ddc3 DM |
9662 | if (is_kfunc_arg_callback(env, meta->btf, &args[argno])) |
9663 | return KF_ARG_PTR_TO_CALLBACK; | |
9664 | ||
66e3a13e JK |
9665 | |
9666 | if (argno + 1 < nargs && | |
9667 | (is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1]) || | |
9668 | is_kfunc_arg_const_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1]))) | |
00b85860 KKD |
9669 | arg_mem_size = true; |
9670 | ||
9671 | /* This is the catch all argument type of register types supported by | |
9672 | * check_helper_mem_access. However, we only allow when argument type is | |
9673 | * pointer to scalar, or struct composed (recursively) of scalars. When | |
9674 | * arg_mem_size is true, the pointer can be void *. | |
9675 | */ | |
9676 | if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(env, meta->btf, ref_t, 0) && | |
9677 | (arg_mem_size ? !btf_type_is_void(ref_t) : 1)) { | |
9678 | verbose(env, "arg#%d pointer type %s %s must point to %sscalar, or struct with scalar\n", | |
9679 | argno, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : ""); | |
9680 | return -EINVAL; | |
9681 | } | |
9682 | return arg_mem_size ? KF_ARG_PTR_TO_MEM_SIZE : KF_ARG_PTR_TO_MEM; | |
9683 | } | |
9684 | ||
9685 | static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env, | |
9686 | struct bpf_reg_state *reg, | |
9687 | const struct btf_type *ref_t, | |
9688 | const char *ref_tname, u32 ref_id, | |
9689 | struct bpf_kfunc_call_arg_meta *meta, | |
9690 | int argno) | |
9691 | { | |
9692 | const struct btf_type *reg_ref_t; | |
9693 | bool strict_type_match = false; | |
9694 | const struct btf *reg_btf; | |
9695 | const char *reg_ref_tname; | |
9696 | u32 reg_ref_id; | |
9697 | ||
3f00c523 | 9698 | if (base_type(reg->type) == PTR_TO_BTF_ID) { |
00b85860 KKD |
9699 | reg_btf = reg->btf; |
9700 | reg_ref_id = reg->btf_id; | |
9701 | } else { | |
9702 | reg_btf = btf_vmlinux; | |
9703 | reg_ref_id = *reg2btf_ids[base_type(reg->type)]; | |
9704 | } | |
9705 | ||
b613d335 DV |
9706 | /* Enforce strict type matching for calls to kfuncs that are acquiring |
9707 | * or releasing a reference, or are no-cast aliases. We do _not_ | |
9708 | * enforce strict matching for plain KF_TRUSTED_ARGS kfuncs by default, | |
9709 | * as we want to enable BPF programs to pass types that are bitwise | |
9710 | * equivalent without forcing them to explicitly cast with something | |
9711 | * like bpf_cast_to_kern_ctx(). | |
9712 | * | |
9713 | * For example, say we had a type like the following: | |
9714 | * | |
9715 | * struct bpf_cpumask { | |
9716 | * cpumask_t cpumask; | |
9717 | * refcount_t usage; | |
9718 | * }; | |
9719 | * | |
9720 | * Note that as specified in <linux/cpumask.h>, cpumask_t is typedef'ed | |
9721 | * to a struct cpumask, so it would be safe to pass a struct | |
9722 | * bpf_cpumask * to a kfunc expecting a struct cpumask *. | |
9723 | * | |
9724 | * The philosophy here is similar to how we allow scalars of different | |
9725 | * types to be passed to kfuncs as long as the size is the same. The | |
9726 | * only difference here is that we're simply allowing | |
9727 | * btf_struct_ids_match() to walk the struct at the 0th offset, and | |
9728 | * resolve types. | |
9729 | */ | |
9730 | if (is_kfunc_acquire(meta) || | |
9731 | (is_kfunc_release(meta) && reg->ref_obj_id) || | |
9732 | btf_type_ids_nocast_alias(&env->log, reg_btf, reg_ref_id, meta->btf, ref_id)) | |
00b85860 KKD |
9733 | strict_type_match = true; |
9734 | ||
b613d335 DV |
9735 | WARN_ON_ONCE(is_kfunc_trusted_args(meta) && reg->off); |
9736 | ||
00b85860 KKD |
9737 | reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id, ®_ref_id); |
9738 | reg_ref_tname = btf_name_by_offset(reg_btf, reg_ref_t->name_off); | |
9739 | if (!btf_struct_ids_match(&env->log, reg_btf, reg_ref_id, reg->off, meta->btf, ref_id, strict_type_match)) { | |
9740 | verbose(env, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", | |
9741 | meta->func_name, argno, btf_type_str(ref_t), ref_tname, argno + 1, | |
9742 | btf_type_str(reg_ref_t), reg_ref_tname); | |
9743 | return -EINVAL; | |
9744 | } | |
9745 | return 0; | |
9746 | } | |
9747 | ||
9748 | static int process_kf_arg_ptr_to_kptr(struct bpf_verifier_env *env, | |
9749 | struct bpf_reg_state *reg, | |
9750 | const struct btf_type *ref_t, | |
9751 | const char *ref_tname, | |
9752 | struct bpf_kfunc_call_arg_meta *meta, | |
9753 | int argno) | |
9754 | { | |
9755 | struct btf_field *kptr_field; | |
9756 | ||
9757 | /* check_func_arg_reg_off allows var_off for | |
9758 | * PTR_TO_MAP_VALUE, but we need fixed offset to find | |
9759 | * off_desc. | |
9760 | */ | |
9761 | if (!tnum_is_const(reg->var_off)) { | |
9762 | verbose(env, "arg#0 must have constant offset\n"); | |
9763 | return -EINVAL; | |
9764 | } | |
9765 | ||
9766 | kptr_field = btf_record_find(reg->map_ptr->record, reg->off + reg->var_off.value, BPF_KPTR); | |
9767 | if (!kptr_field || kptr_field->type != BPF_KPTR_REF) { | |
9768 | verbose(env, "arg#0 no referenced kptr at map value offset=%llu\n", | |
9769 | reg->off + reg->var_off.value); | |
9770 | return -EINVAL; | |
9771 | } | |
9772 | ||
9773 | if (!btf_struct_ids_match(&env->log, meta->btf, ref_t->type, 0, kptr_field->kptr.btf, | |
9774 | kptr_field->kptr.btf_id, true)) { | |
9775 | verbose(env, "kernel function %s args#%d expected pointer to %s %s\n", | |
9776 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
9777 | return -EINVAL; | |
9778 | } | |
9779 | return 0; | |
9780 | } | |
9781 | ||
6a3cd331 | 9782 | static int ref_set_non_owning(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
534e86bc | 9783 | { |
6a3cd331 DM |
9784 | struct bpf_verifier_state *state = env->cur_state; |
9785 | ||
9786 | if (!state->active_lock.ptr) { | |
9787 | verbose(env, "verifier internal error: ref_set_non_owning w/o active lock\n"); | |
9788 | return -EFAULT; | |
9789 | } | |
9790 | ||
9791 | if (type_flag(reg->type) & NON_OWN_REF) { | |
9792 | verbose(env, "verifier internal error: NON_OWN_REF already set\n"); | |
9793 | return -EFAULT; | |
9794 | } | |
9795 | ||
9796 | reg->type |= NON_OWN_REF; | |
9797 | return 0; | |
9798 | } | |
9799 | ||
9800 | static int ref_convert_owning_non_owning(struct bpf_verifier_env *env, u32 ref_obj_id) | |
9801 | { | |
9802 | struct bpf_func_state *state, *unused; | |
534e86bc KKD |
9803 | struct bpf_reg_state *reg; |
9804 | int i; | |
9805 | ||
6a3cd331 DM |
9806 | state = cur_func(env); |
9807 | ||
534e86bc | 9808 | if (!ref_obj_id) { |
6a3cd331 DM |
9809 | verbose(env, "verifier internal error: ref_obj_id is zero for " |
9810 | "owning -> non-owning conversion\n"); | |
534e86bc KKD |
9811 | return -EFAULT; |
9812 | } | |
6a3cd331 | 9813 | |
534e86bc | 9814 | for (i = 0; i < state->acquired_refs; i++) { |
6a3cd331 DM |
9815 | if (state->refs[i].id != ref_obj_id) |
9816 | continue; | |
9817 | ||
9818 | /* Clear ref_obj_id here so release_reference doesn't clobber | |
9819 | * the whole reg | |
9820 | */ | |
9821 | bpf_for_each_reg_in_vstate(env->cur_state, unused, reg, ({ | |
9822 | if (reg->ref_obj_id == ref_obj_id) { | |
9823 | reg->ref_obj_id = 0; | |
9824 | ref_set_non_owning(env, reg); | |
534e86bc | 9825 | } |
6a3cd331 DM |
9826 | })); |
9827 | return 0; | |
534e86bc | 9828 | } |
6a3cd331 | 9829 | |
534e86bc KKD |
9830 | verbose(env, "verifier internal error: ref state missing for ref_obj_id\n"); |
9831 | return -EFAULT; | |
9832 | } | |
9833 | ||
8cab76ec KKD |
9834 | /* Implementation details: |
9835 | * | |
9836 | * Each register points to some region of memory, which we define as an | |
9837 | * allocation. Each allocation may embed a bpf_spin_lock which protects any | |
9838 | * special BPF objects (bpf_list_head, bpf_rb_root, etc.) part of the same | |
9839 | * allocation. The lock and the data it protects are colocated in the same | |
9840 | * memory region. | |
9841 | * | |
9842 | * Hence, everytime a register holds a pointer value pointing to such | |
9843 | * allocation, the verifier preserves a unique reg->id for it. | |
9844 | * | |
9845 | * The verifier remembers the lock 'ptr' and the lock 'id' whenever | |
9846 | * bpf_spin_lock is called. | |
9847 | * | |
9848 | * To enable this, lock state in the verifier captures two values: | |
9849 | * active_lock.ptr = Register's type specific pointer | |
9850 | * active_lock.id = A unique ID for each register pointer value | |
9851 | * | |
9852 | * Currently, PTR_TO_MAP_VALUE and PTR_TO_BTF_ID | MEM_ALLOC are the two | |
9853 | * supported register types. | |
9854 | * | |
9855 | * The active_lock.ptr in case of map values is the reg->map_ptr, and in case of | |
9856 | * allocated objects is the reg->btf pointer. | |
9857 | * | |
9858 | * The active_lock.id is non-unique for maps supporting direct_value_addr, as we | |
9859 | * can establish the provenance of the map value statically for each distinct | |
9860 | * lookup into such maps. They always contain a single map value hence unique | |
9861 | * IDs for each pseudo load pessimizes the algorithm and rejects valid programs. | |
9862 | * | |
9863 | * So, in case of global variables, they use array maps with max_entries = 1, | |
9864 | * hence their active_lock.ptr becomes map_ptr and id = 0 (since they all point | |
9865 | * into the same map value as max_entries is 1, as described above). | |
9866 | * | |
9867 | * In case of inner map lookups, the inner map pointer has same map_ptr as the | |
9868 | * outer map pointer (in verifier context), but each lookup into an inner map | |
9869 | * assigns a fresh reg->id to the lookup, so while lookups into distinct inner | |
9870 | * maps from the same outer map share the same map_ptr as active_lock.ptr, they | |
9871 | * will get different reg->id assigned to each lookup, hence different | |
9872 | * active_lock.id. | |
9873 | * | |
9874 | * In case of allocated objects, active_lock.ptr is the reg->btf, and the | |
9875 | * reg->id is a unique ID preserved after the NULL pointer check on the pointer | |
9876 | * returned from bpf_obj_new. Each allocation receives a new reg->id. | |
9877 | */ | |
9878 | static int check_reg_allocation_locked(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
9879 | { | |
9880 | void *ptr; | |
9881 | u32 id; | |
9882 | ||
9883 | switch ((int)reg->type) { | |
9884 | case PTR_TO_MAP_VALUE: | |
9885 | ptr = reg->map_ptr; | |
9886 | break; | |
9887 | case PTR_TO_BTF_ID | MEM_ALLOC: | |
9888 | ptr = reg->btf; | |
9889 | break; | |
9890 | default: | |
9891 | verbose(env, "verifier internal error: unknown reg type for lock check\n"); | |
9892 | return -EFAULT; | |
9893 | } | |
9894 | id = reg->id; | |
9895 | ||
9896 | if (!env->cur_state->active_lock.ptr) | |
9897 | return -EINVAL; | |
9898 | if (env->cur_state->active_lock.ptr != ptr || | |
9899 | env->cur_state->active_lock.id != id) { | |
9900 | verbose(env, "held lock and object are not in the same allocation\n"); | |
9901 | return -EINVAL; | |
9902 | } | |
9903 | return 0; | |
9904 | } | |
9905 | ||
9906 | static bool is_bpf_list_api_kfunc(u32 btf_id) | |
9907 | { | |
9908 | return btf_id == special_kfunc_list[KF_bpf_list_push_front] || | |
9909 | btf_id == special_kfunc_list[KF_bpf_list_push_back] || | |
9910 | btf_id == special_kfunc_list[KF_bpf_list_pop_front] || | |
9911 | btf_id == special_kfunc_list[KF_bpf_list_pop_back]; | |
9912 | } | |
9913 | ||
cd6791b4 DM |
9914 | static bool is_bpf_rbtree_api_kfunc(u32 btf_id) |
9915 | { | |
9916 | return btf_id == special_kfunc_list[KF_bpf_rbtree_add] || | |
9917 | btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
9918 | btf_id == special_kfunc_list[KF_bpf_rbtree_first]; | |
9919 | } | |
9920 | ||
9921 | static bool is_bpf_graph_api_kfunc(u32 btf_id) | |
9922 | { | |
9923 | return is_bpf_list_api_kfunc(btf_id) || is_bpf_rbtree_api_kfunc(btf_id); | |
9924 | } | |
9925 | ||
5d92ddc3 DM |
9926 | static bool is_callback_calling_kfunc(u32 btf_id) |
9927 | { | |
9928 | return btf_id == special_kfunc_list[KF_bpf_rbtree_add]; | |
9929 | } | |
9930 | ||
9931 | static bool is_rbtree_lock_required_kfunc(u32 btf_id) | |
9932 | { | |
9933 | return is_bpf_rbtree_api_kfunc(btf_id); | |
9934 | } | |
9935 | ||
cd6791b4 DM |
9936 | static bool check_kfunc_is_graph_root_api(struct bpf_verifier_env *env, |
9937 | enum btf_field_type head_field_type, | |
9938 | u32 kfunc_btf_id) | |
9939 | { | |
9940 | bool ret; | |
9941 | ||
9942 | switch (head_field_type) { | |
9943 | case BPF_LIST_HEAD: | |
9944 | ret = is_bpf_list_api_kfunc(kfunc_btf_id); | |
9945 | break; | |
9946 | case BPF_RB_ROOT: | |
9947 | ret = is_bpf_rbtree_api_kfunc(kfunc_btf_id); | |
9948 | break; | |
9949 | default: | |
9950 | verbose(env, "verifier internal error: unexpected graph root argument type %s\n", | |
9951 | btf_field_type_name(head_field_type)); | |
9952 | return false; | |
9953 | } | |
9954 | ||
9955 | if (!ret) | |
9956 | verbose(env, "verifier internal error: %s head arg for unknown kfunc\n", | |
9957 | btf_field_type_name(head_field_type)); | |
9958 | return ret; | |
9959 | } | |
9960 | ||
9961 | static bool check_kfunc_is_graph_node_api(struct bpf_verifier_env *env, | |
9962 | enum btf_field_type node_field_type, | |
9963 | u32 kfunc_btf_id) | |
8cab76ec | 9964 | { |
cd6791b4 DM |
9965 | bool ret; |
9966 | ||
9967 | switch (node_field_type) { | |
9968 | case BPF_LIST_NODE: | |
9969 | ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_front] || | |
9970 | kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_back]); | |
9971 | break; | |
9972 | case BPF_RB_NODE: | |
9973 | ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
9974 | kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_add]); | |
9975 | break; | |
9976 | default: | |
9977 | verbose(env, "verifier internal error: unexpected graph node argument type %s\n", | |
9978 | btf_field_type_name(node_field_type)); | |
9979 | return false; | |
9980 | } | |
9981 | ||
9982 | if (!ret) | |
9983 | verbose(env, "verifier internal error: %s node arg for unknown kfunc\n", | |
9984 | btf_field_type_name(node_field_type)); | |
9985 | return ret; | |
9986 | } | |
9987 | ||
9988 | static int | |
9989 | __process_kf_arg_ptr_to_graph_root(struct bpf_verifier_env *env, | |
9990 | struct bpf_reg_state *reg, u32 regno, | |
9991 | struct bpf_kfunc_call_arg_meta *meta, | |
9992 | enum btf_field_type head_field_type, | |
9993 | struct btf_field **head_field) | |
9994 | { | |
9995 | const char *head_type_name; | |
8cab76ec KKD |
9996 | struct btf_field *field; |
9997 | struct btf_record *rec; | |
cd6791b4 | 9998 | u32 head_off; |
8cab76ec | 9999 | |
cd6791b4 DM |
10000 | if (meta->btf != btf_vmlinux) { |
10001 | verbose(env, "verifier internal error: unexpected btf mismatch in kfunc call\n"); | |
8cab76ec KKD |
10002 | return -EFAULT; |
10003 | } | |
10004 | ||
cd6791b4 DM |
10005 | if (!check_kfunc_is_graph_root_api(env, head_field_type, meta->func_id)) |
10006 | return -EFAULT; | |
10007 | ||
10008 | head_type_name = btf_field_type_name(head_field_type); | |
8cab76ec KKD |
10009 | if (!tnum_is_const(reg->var_off)) { |
10010 | verbose(env, | |
cd6791b4 DM |
10011 | "R%d doesn't have constant offset. %s has to be at the constant offset\n", |
10012 | regno, head_type_name); | |
8cab76ec KKD |
10013 | return -EINVAL; |
10014 | } | |
10015 | ||
10016 | rec = reg_btf_record(reg); | |
cd6791b4 DM |
10017 | head_off = reg->off + reg->var_off.value; |
10018 | field = btf_record_find(rec, head_off, head_field_type); | |
8cab76ec | 10019 | if (!field) { |
cd6791b4 | 10020 | verbose(env, "%s not found at offset=%u\n", head_type_name, head_off); |
8cab76ec KKD |
10021 | return -EINVAL; |
10022 | } | |
10023 | ||
10024 | /* All functions require bpf_list_head to be protected using a bpf_spin_lock */ | |
10025 | if (check_reg_allocation_locked(env, reg)) { | |
cd6791b4 DM |
10026 | verbose(env, "bpf_spin_lock at off=%d must be held for %s\n", |
10027 | rec->spin_lock_off, head_type_name); | |
8cab76ec KKD |
10028 | return -EINVAL; |
10029 | } | |
10030 | ||
cd6791b4 DM |
10031 | if (*head_field) { |
10032 | verbose(env, "verifier internal error: repeating %s arg\n", head_type_name); | |
8cab76ec KKD |
10033 | return -EFAULT; |
10034 | } | |
cd6791b4 | 10035 | *head_field = field; |
8cab76ec KKD |
10036 | return 0; |
10037 | } | |
10038 | ||
cd6791b4 | 10039 | static int process_kf_arg_ptr_to_list_head(struct bpf_verifier_env *env, |
8cab76ec KKD |
10040 | struct bpf_reg_state *reg, u32 regno, |
10041 | struct bpf_kfunc_call_arg_meta *meta) | |
10042 | { | |
cd6791b4 DM |
10043 | return __process_kf_arg_ptr_to_graph_root(env, reg, regno, meta, BPF_LIST_HEAD, |
10044 | &meta->arg_list_head.field); | |
10045 | } | |
10046 | ||
10047 | static int process_kf_arg_ptr_to_rbtree_root(struct bpf_verifier_env *env, | |
10048 | struct bpf_reg_state *reg, u32 regno, | |
10049 | struct bpf_kfunc_call_arg_meta *meta) | |
10050 | { | |
10051 | return __process_kf_arg_ptr_to_graph_root(env, reg, regno, meta, BPF_RB_ROOT, | |
10052 | &meta->arg_rbtree_root.field); | |
10053 | } | |
10054 | ||
10055 | static int | |
10056 | __process_kf_arg_ptr_to_graph_node(struct bpf_verifier_env *env, | |
10057 | struct bpf_reg_state *reg, u32 regno, | |
10058 | struct bpf_kfunc_call_arg_meta *meta, | |
10059 | enum btf_field_type head_field_type, | |
10060 | enum btf_field_type node_field_type, | |
10061 | struct btf_field **node_field) | |
10062 | { | |
10063 | const char *node_type_name; | |
8cab76ec KKD |
10064 | const struct btf_type *et, *t; |
10065 | struct btf_field *field; | |
cd6791b4 | 10066 | u32 node_off; |
8cab76ec | 10067 | |
cd6791b4 DM |
10068 | if (meta->btf != btf_vmlinux) { |
10069 | verbose(env, "verifier internal error: unexpected btf mismatch in kfunc call\n"); | |
8cab76ec KKD |
10070 | return -EFAULT; |
10071 | } | |
10072 | ||
cd6791b4 DM |
10073 | if (!check_kfunc_is_graph_node_api(env, node_field_type, meta->func_id)) |
10074 | return -EFAULT; | |
10075 | ||
10076 | node_type_name = btf_field_type_name(node_field_type); | |
8cab76ec KKD |
10077 | if (!tnum_is_const(reg->var_off)) { |
10078 | verbose(env, | |
cd6791b4 DM |
10079 | "R%d doesn't have constant offset. %s has to be at the constant offset\n", |
10080 | regno, node_type_name); | |
8cab76ec KKD |
10081 | return -EINVAL; |
10082 | } | |
10083 | ||
cd6791b4 DM |
10084 | node_off = reg->off + reg->var_off.value; |
10085 | field = reg_find_field_offset(reg, node_off, node_field_type); | |
10086 | if (!field || field->offset != node_off) { | |
10087 | verbose(env, "%s not found at offset=%u\n", node_type_name, node_off); | |
8cab76ec KKD |
10088 | return -EINVAL; |
10089 | } | |
10090 | ||
cd6791b4 | 10091 | field = *node_field; |
8cab76ec | 10092 | |
30465003 | 10093 | et = btf_type_by_id(field->graph_root.btf, field->graph_root.value_btf_id); |
8cab76ec | 10094 | t = btf_type_by_id(reg->btf, reg->btf_id); |
30465003 DM |
10095 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, 0, field->graph_root.btf, |
10096 | field->graph_root.value_btf_id, true)) { | |
cd6791b4 | 10097 | verbose(env, "operation on %s expects arg#1 %s at offset=%d " |
8cab76ec | 10098 | "in struct %s, but arg is at offset=%d in struct %s\n", |
cd6791b4 DM |
10099 | btf_field_type_name(head_field_type), |
10100 | btf_field_type_name(node_field_type), | |
30465003 DM |
10101 | field->graph_root.node_offset, |
10102 | btf_name_by_offset(field->graph_root.btf, et->name_off), | |
cd6791b4 | 10103 | node_off, btf_name_by_offset(reg->btf, t->name_off)); |
8cab76ec KKD |
10104 | return -EINVAL; |
10105 | } | |
10106 | ||
cd6791b4 DM |
10107 | if (node_off != field->graph_root.node_offset) { |
10108 | verbose(env, "arg#1 offset=%d, but expected %s at offset=%d in struct %s\n", | |
10109 | node_off, btf_field_type_name(node_field_type), | |
10110 | field->graph_root.node_offset, | |
30465003 | 10111 | btf_name_by_offset(field->graph_root.btf, et->name_off)); |
8cab76ec KKD |
10112 | return -EINVAL; |
10113 | } | |
6a3cd331 DM |
10114 | |
10115 | return 0; | |
8cab76ec KKD |
10116 | } |
10117 | ||
cd6791b4 DM |
10118 | static int process_kf_arg_ptr_to_list_node(struct bpf_verifier_env *env, |
10119 | struct bpf_reg_state *reg, u32 regno, | |
10120 | struct bpf_kfunc_call_arg_meta *meta) | |
10121 | { | |
10122 | return __process_kf_arg_ptr_to_graph_node(env, reg, regno, meta, | |
10123 | BPF_LIST_HEAD, BPF_LIST_NODE, | |
10124 | &meta->arg_list_head.field); | |
10125 | } | |
10126 | ||
10127 | static int process_kf_arg_ptr_to_rbtree_node(struct bpf_verifier_env *env, | |
10128 | struct bpf_reg_state *reg, u32 regno, | |
10129 | struct bpf_kfunc_call_arg_meta *meta) | |
10130 | { | |
10131 | return __process_kf_arg_ptr_to_graph_node(env, reg, regno, meta, | |
10132 | BPF_RB_ROOT, BPF_RB_NODE, | |
10133 | &meta->arg_rbtree_root.field); | |
10134 | } | |
10135 | ||
1d18feb2 JK |
10136 | static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta, |
10137 | int insn_idx) | |
00b85860 KKD |
10138 | { |
10139 | const char *func_name = meta->func_name, *ref_tname; | |
10140 | const struct btf *btf = meta->btf; | |
10141 | const struct btf_param *args; | |
10142 | u32 i, nargs; | |
10143 | int ret; | |
10144 | ||
10145 | args = (const struct btf_param *)(meta->func_proto + 1); | |
10146 | nargs = btf_type_vlen(meta->func_proto); | |
10147 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { | |
10148 | verbose(env, "Function %s has %d > %d args\n", func_name, nargs, | |
10149 | MAX_BPF_FUNC_REG_ARGS); | |
10150 | return -EINVAL; | |
10151 | } | |
10152 | ||
10153 | /* Check that BTF function arguments match actual types that the | |
10154 | * verifier sees. | |
10155 | */ | |
10156 | for (i = 0; i < nargs; i++) { | |
10157 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[i + 1]; | |
10158 | const struct btf_type *t, *ref_t, *resolve_ret; | |
10159 | enum bpf_arg_type arg_type = ARG_DONTCARE; | |
10160 | u32 regno = i + 1, ref_id, type_size; | |
10161 | bool is_ret_buf_sz = false; | |
10162 | int kf_arg_type; | |
10163 | ||
10164 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); | |
958cf2e2 KKD |
10165 | |
10166 | if (is_kfunc_arg_ignore(btf, &args[i])) | |
10167 | continue; | |
10168 | ||
00b85860 KKD |
10169 | if (btf_type_is_scalar(t)) { |
10170 | if (reg->type != SCALAR_VALUE) { | |
10171 | verbose(env, "R%d is not a scalar\n", regno); | |
10172 | return -EINVAL; | |
10173 | } | |
a50388db KKD |
10174 | |
10175 | if (is_kfunc_arg_constant(meta->btf, &args[i])) { | |
10176 | if (meta->arg_constant.found) { | |
10177 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
10178 | return -EFAULT; | |
10179 | } | |
10180 | if (!tnum_is_const(reg->var_off)) { | |
10181 | verbose(env, "R%d must be a known constant\n", regno); | |
10182 | return -EINVAL; | |
10183 | } | |
10184 | ret = mark_chain_precision(env, regno); | |
10185 | if (ret < 0) | |
10186 | return ret; | |
10187 | meta->arg_constant.found = true; | |
10188 | meta->arg_constant.value = reg->var_off.value; | |
10189 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) { | |
00b85860 KKD |
10190 | meta->r0_rdonly = true; |
10191 | is_ret_buf_sz = true; | |
10192 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) { | |
10193 | is_ret_buf_sz = true; | |
10194 | } | |
10195 | ||
10196 | if (is_ret_buf_sz) { | |
10197 | if (meta->r0_size) { | |
10198 | verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc"); | |
10199 | return -EINVAL; | |
10200 | } | |
10201 | ||
10202 | if (!tnum_is_const(reg->var_off)) { | |
10203 | verbose(env, "R%d is not a const\n", regno); | |
10204 | return -EINVAL; | |
10205 | } | |
10206 | ||
10207 | meta->r0_size = reg->var_off.value; | |
10208 | ret = mark_chain_precision(env, regno); | |
10209 | if (ret) | |
10210 | return ret; | |
10211 | } | |
10212 | continue; | |
10213 | } | |
10214 | ||
10215 | if (!btf_type_is_ptr(t)) { | |
10216 | verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t)); | |
10217 | return -EINVAL; | |
10218 | } | |
10219 | ||
20c09d92 | 10220 | if ((is_kfunc_trusted_args(meta) || is_kfunc_rcu(meta)) && |
caf713c3 DV |
10221 | (register_is_null(reg) || type_may_be_null(reg->type))) { |
10222 | verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i); | |
10223 | return -EACCES; | |
10224 | } | |
10225 | ||
00b85860 KKD |
10226 | if (reg->ref_obj_id) { |
10227 | if (is_kfunc_release(meta) && meta->ref_obj_id) { | |
10228 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
10229 | regno, reg->ref_obj_id, | |
10230 | meta->ref_obj_id); | |
10231 | return -EFAULT; | |
10232 | } | |
10233 | meta->ref_obj_id = reg->ref_obj_id; | |
10234 | if (is_kfunc_release(meta)) | |
10235 | meta->release_regno = regno; | |
10236 | } | |
10237 | ||
10238 | ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); | |
10239 | ref_tname = btf_name_by_offset(btf, ref_t->name_off); | |
10240 | ||
10241 | kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs); | |
10242 | if (kf_arg_type < 0) | |
10243 | return kf_arg_type; | |
10244 | ||
10245 | switch (kf_arg_type) { | |
ac9f0605 | 10246 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
00b85860 | 10247 | case KF_ARG_PTR_TO_BTF_ID: |
fca1aa75 | 10248 | if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta)) |
00b85860 | 10249 | break; |
3f00c523 DV |
10250 | |
10251 | if (!is_trusted_reg(reg)) { | |
fca1aa75 YS |
10252 | if (!is_kfunc_rcu(meta)) { |
10253 | verbose(env, "R%d must be referenced or trusted\n", regno); | |
10254 | return -EINVAL; | |
10255 | } | |
10256 | if (!is_rcu_reg(reg)) { | |
10257 | verbose(env, "R%d must be a rcu pointer\n", regno); | |
10258 | return -EINVAL; | |
10259 | } | |
00b85860 | 10260 | } |
fca1aa75 | 10261 | |
00b85860 KKD |
10262 | fallthrough; |
10263 | case KF_ARG_PTR_TO_CTX: | |
10264 | /* Trusted arguments have the same offset checks as release arguments */ | |
10265 | arg_type |= OBJ_RELEASE; | |
10266 | break; | |
10267 | case KF_ARG_PTR_TO_KPTR: | |
10268 | case KF_ARG_PTR_TO_DYNPTR: | |
06accc87 | 10269 | case KF_ARG_PTR_TO_ITER: |
8cab76ec KKD |
10270 | case KF_ARG_PTR_TO_LIST_HEAD: |
10271 | case KF_ARG_PTR_TO_LIST_NODE: | |
cd6791b4 DM |
10272 | case KF_ARG_PTR_TO_RB_ROOT: |
10273 | case KF_ARG_PTR_TO_RB_NODE: | |
00b85860 KKD |
10274 | case KF_ARG_PTR_TO_MEM: |
10275 | case KF_ARG_PTR_TO_MEM_SIZE: | |
5d92ddc3 | 10276 | case KF_ARG_PTR_TO_CALLBACK: |
00b85860 KKD |
10277 | /* Trusted by default */ |
10278 | break; | |
10279 | default: | |
10280 | WARN_ON_ONCE(1); | |
10281 | return -EFAULT; | |
10282 | } | |
10283 | ||
10284 | if (is_kfunc_release(meta) && reg->ref_obj_id) | |
10285 | arg_type |= OBJ_RELEASE; | |
10286 | ret = check_func_arg_reg_off(env, reg, regno, arg_type); | |
10287 | if (ret < 0) | |
10288 | return ret; | |
10289 | ||
10290 | switch (kf_arg_type) { | |
10291 | case KF_ARG_PTR_TO_CTX: | |
10292 | if (reg->type != PTR_TO_CTX) { | |
10293 | verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t)); | |
10294 | return -EINVAL; | |
10295 | } | |
fd264ca0 YS |
10296 | |
10297 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { | |
10298 | ret = get_kern_ctx_btf_id(&env->log, resolve_prog_type(env->prog)); | |
10299 | if (ret < 0) | |
10300 | return -EINVAL; | |
10301 | meta->ret_btf_id = ret; | |
10302 | } | |
00b85860 | 10303 | break; |
ac9f0605 KKD |
10304 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
10305 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10306 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10307 | return -EINVAL; | |
10308 | } | |
10309 | if (!reg->ref_obj_id) { | |
10310 | verbose(env, "allocated object must be referenced\n"); | |
10311 | return -EINVAL; | |
10312 | } | |
10313 | if (meta->btf == btf_vmlinux && | |
10314 | meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { | |
10315 | meta->arg_obj_drop.btf = reg->btf; | |
10316 | meta->arg_obj_drop.btf_id = reg->btf_id; | |
10317 | } | |
10318 | break; | |
00b85860 KKD |
10319 | case KF_ARG_PTR_TO_KPTR: |
10320 | if (reg->type != PTR_TO_MAP_VALUE) { | |
10321 | verbose(env, "arg#0 expected pointer to map value\n"); | |
10322 | return -EINVAL; | |
10323 | } | |
10324 | ret = process_kf_arg_ptr_to_kptr(env, reg, ref_t, ref_tname, meta, i); | |
10325 | if (ret < 0) | |
10326 | return ret; | |
10327 | break; | |
10328 | case KF_ARG_PTR_TO_DYNPTR: | |
d96d937d JK |
10329 | { |
10330 | enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR; | |
10331 | ||
6b75bd3d | 10332 | if (reg->type != PTR_TO_STACK && |
27060531 | 10333 | reg->type != CONST_PTR_TO_DYNPTR) { |
6b75bd3d | 10334 | verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i); |
00b85860 KKD |
10335 | return -EINVAL; |
10336 | } | |
10337 | ||
d96d937d JK |
10338 | if (reg->type == CONST_PTR_TO_DYNPTR) |
10339 | dynptr_arg_type |= MEM_RDONLY; | |
10340 | ||
10341 | if (is_kfunc_arg_uninit(btf, &args[i])) | |
10342 | dynptr_arg_type |= MEM_UNINIT; | |
10343 | ||
b5964b96 JK |
10344 | if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) |
10345 | dynptr_arg_type |= DYNPTR_TYPE_SKB; | |
05421aec JK |
10346 | else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp]) |
10347 | dynptr_arg_type |= DYNPTR_TYPE_XDP; | |
b5964b96 | 10348 | |
d96d937d | 10349 | ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type); |
6b75bd3d KKD |
10350 | if (ret < 0) |
10351 | return ret; | |
66e3a13e JK |
10352 | |
10353 | if (!(dynptr_arg_type & MEM_UNINIT)) { | |
10354 | int id = dynptr_id(env, reg); | |
10355 | ||
10356 | if (id < 0) { | |
10357 | verbose(env, "verifier internal error: failed to obtain dynptr id\n"); | |
10358 | return id; | |
10359 | } | |
10360 | meta->initialized_dynptr.id = id; | |
10361 | meta->initialized_dynptr.type = dynptr_get_type(env, reg); | |
10362 | } | |
10363 | ||
00b85860 | 10364 | break; |
d96d937d | 10365 | } |
06accc87 AN |
10366 | case KF_ARG_PTR_TO_ITER: |
10367 | ret = process_iter_arg(env, regno, insn_idx, meta); | |
10368 | if (ret < 0) | |
10369 | return ret; | |
10370 | break; | |
8cab76ec KKD |
10371 | case KF_ARG_PTR_TO_LIST_HEAD: |
10372 | if (reg->type != PTR_TO_MAP_VALUE && | |
10373 | reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10374 | verbose(env, "arg#%d expected pointer to map value or allocated object\n", i); | |
10375 | return -EINVAL; | |
10376 | } | |
10377 | if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) { | |
10378 | verbose(env, "allocated object must be referenced\n"); | |
10379 | return -EINVAL; | |
10380 | } | |
10381 | ret = process_kf_arg_ptr_to_list_head(env, reg, regno, meta); | |
10382 | if (ret < 0) | |
10383 | return ret; | |
10384 | break; | |
cd6791b4 DM |
10385 | case KF_ARG_PTR_TO_RB_ROOT: |
10386 | if (reg->type != PTR_TO_MAP_VALUE && | |
10387 | reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10388 | verbose(env, "arg#%d expected pointer to map value or allocated object\n", i); | |
10389 | return -EINVAL; | |
10390 | } | |
10391 | if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) { | |
10392 | verbose(env, "allocated object must be referenced\n"); | |
10393 | return -EINVAL; | |
10394 | } | |
10395 | ret = process_kf_arg_ptr_to_rbtree_root(env, reg, regno, meta); | |
10396 | if (ret < 0) | |
10397 | return ret; | |
10398 | break; | |
8cab76ec KKD |
10399 | case KF_ARG_PTR_TO_LIST_NODE: |
10400 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10401 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10402 | return -EINVAL; | |
10403 | } | |
10404 | if (!reg->ref_obj_id) { | |
10405 | verbose(env, "allocated object must be referenced\n"); | |
10406 | return -EINVAL; | |
10407 | } | |
10408 | ret = process_kf_arg_ptr_to_list_node(env, reg, regno, meta); | |
10409 | if (ret < 0) | |
10410 | return ret; | |
10411 | break; | |
cd6791b4 | 10412 | case KF_ARG_PTR_TO_RB_NODE: |
a40d3632 DM |
10413 | if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_remove]) { |
10414 | if (!type_is_non_owning_ref(reg->type) || reg->ref_obj_id) { | |
10415 | verbose(env, "rbtree_remove node input must be non-owning ref\n"); | |
10416 | return -EINVAL; | |
10417 | } | |
10418 | if (in_rbtree_lock_required_cb(env)) { | |
10419 | verbose(env, "rbtree_remove not allowed in rbtree cb\n"); | |
10420 | return -EINVAL; | |
10421 | } | |
10422 | } else { | |
10423 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10424 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10425 | return -EINVAL; | |
10426 | } | |
10427 | if (!reg->ref_obj_id) { | |
10428 | verbose(env, "allocated object must be referenced\n"); | |
10429 | return -EINVAL; | |
10430 | } | |
cd6791b4 | 10431 | } |
a40d3632 | 10432 | |
cd6791b4 DM |
10433 | ret = process_kf_arg_ptr_to_rbtree_node(env, reg, regno, meta); |
10434 | if (ret < 0) | |
10435 | return ret; | |
10436 | break; | |
00b85860 KKD |
10437 | case KF_ARG_PTR_TO_BTF_ID: |
10438 | /* Only base_type is checked, further checks are done here */ | |
3f00c523 | 10439 | if ((base_type(reg->type) != PTR_TO_BTF_ID || |
fca1aa75 | 10440 | (bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) && |
3f00c523 DV |
10441 | !reg2btf_ids[base_type(reg->type)]) { |
10442 | verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type)); | |
10443 | verbose(env, "expected %s or socket\n", | |
10444 | reg_type_str(env, base_type(reg->type) | | |
10445 | (type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS))); | |
00b85860 KKD |
10446 | return -EINVAL; |
10447 | } | |
10448 | ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i); | |
10449 | if (ret < 0) | |
10450 | return ret; | |
10451 | break; | |
10452 | case KF_ARG_PTR_TO_MEM: | |
10453 | resolve_ret = btf_resolve_size(btf, ref_t, &type_size); | |
10454 | if (IS_ERR(resolve_ret)) { | |
10455 | verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n", | |
10456 | i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret)); | |
10457 | return -EINVAL; | |
10458 | } | |
10459 | ret = check_mem_reg(env, reg, regno, type_size); | |
10460 | if (ret < 0) | |
10461 | return ret; | |
10462 | break; | |
10463 | case KF_ARG_PTR_TO_MEM_SIZE: | |
66e3a13e JK |
10464 | { |
10465 | struct bpf_reg_state *size_reg = ®s[regno + 1]; | |
10466 | const struct btf_param *size_arg = &args[i + 1]; | |
10467 | ||
10468 | ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1); | |
00b85860 KKD |
10469 | if (ret < 0) { |
10470 | verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1); | |
10471 | return ret; | |
10472 | } | |
66e3a13e JK |
10473 | |
10474 | if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) { | |
10475 | if (meta->arg_constant.found) { | |
10476 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
10477 | return -EFAULT; | |
10478 | } | |
10479 | if (!tnum_is_const(size_reg->var_off)) { | |
10480 | verbose(env, "R%d must be a known constant\n", regno + 1); | |
10481 | return -EINVAL; | |
10482 | } | |
10483 | meta->arg_constant.found = true; | |
10484 | meta->arg_constant.value = size_reg->var_off.value; | |
10485 | } | |
10486 | ||
10487 | /* Skip next '__sz' or '__szk' argument */ | |
00b85860 KKD |
10488 | i++; |
10489 | break; | |
66e3a13e | 10490 | } |
5d92ddc3 DM |
10491 | case KF_ARG_PTR_TO_CALLBACK: |
10492 | meta->subprogno = reg->subprogno; | |
10493 | break; | |
00b85860 KKD |
10494 | } |
10495 | } | |
10496 | ||
10497 | if (is_kfunc_release(meta) && !meta->release_regno) { | |
10498 | verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n", | |
10499 | func_name); | |
10500 | return -EINVAL; | |
10501 | } | |
10502 | ||
10503 | return 0; | |
10504 | } | |
10505 | ||
07236eab AN |
10506 | static int fetch_kfunc_meta(struct bpf_verifier_env *env, |
10507 | struct bpf_insn *insn, | |
10508 | struct bpf_kfunc_call_arg_meta *meta, | |
10509 | const char **kfunc_name) | |
e6ac2450 | 10510 | { |
07236eab AN |
10511 | const struct btf_type *func, *func_proto; |
10512 | u32 func_id, *kfunc_flags; | |
10513 | const char *func_name; | |
2357672c | 10514 | struct btf *desc_btf; |
e6ac2450 | 10515 | |
07236eab AN |
10516 | if (kfunc_name) |
10517 | *kfunc_name = NULL; | |
10518 | ||
a5d82727 | 10519 | if (!insn->imm) |
07236eab | 10520 | return -EINVAL; |
a5d82727 | 10521 | |
43bf0878 | 10522 | desc_btf = find_kfunc_desc_btf(env, insn->off); |
2357672c KKD |
10523 | if (IS_ERR(desc_btf)) |
10524 | return PTR_ERR(desc_btf); | |
10525 | ||
e6ac2450 | 10526 | func_id = insn->imm; |
2357672c KKD |
10527 | func = btf_type_by_id(desc_btf, func_id); |
10528 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
07236eab AN |
10529 | if (kfunc_name) |
10530 | *kfunc_name = func_name; | |
2357672c | 10531 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 | 10532 | |
a4703e31 KKD |
10533 | kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id); |
10534 | if (!kfunc_flags) { | |
e6ac2450 MKL |
10535 | return -EACCES; |
10536 | } | |
00b85860 | 10537 | |
07236eab AN |
10538 | memset(meta, 0, sizeof(*meta)); |
10539 | meta->btf = desc_btf; | |
10540 | meta->func_id = func_id; | |
10541 | meta->kfunc_flags = *kfunc_flags; | |
10542 | meta->func_proto = func_proto; | |
10543 | meta->func_name = func_name; | |
10544 | ||
10545 | return 0; | |
10546 | } | |
10547 | ||
10548 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
10549 | int *insn_idx_p) | |
10550 | { | |
10551 | const struct btf_type *t, *ptr_type; | |
10552 | u32 i, nargs, ptr_type_id, release_ref_obj_id; | |
10553 | struct bpf_reg_state *regs = cur_regs(env); | |
10554 | const char *func_name, *ptr_type_name; | |
10555 | bool sleepable, rcu_lock, rcu_unlock; | |
10556 | struct bpf_kfunc_call_arg_meta meta; | |
10557 | struct bpf_insn_aux_data *insn_aux; | |
10558 | int err, insn_idx = *insn_idx_p; | |
10559 | const struct btf_param *args; | |
10560 | const struct btf_type *ret_t; | |
10561 | struct btf *desc_btf; | |
10562 | ||
10563 | /* skip for now, but return error when we find this in fixup_kfunc_call */ | |
10564 | if (!insn->imm) | |
10565 | return 0; | |
10566 | ||
10567 | err = fetch_kfunc_meta(env, insn, &meta, &func_name); | |
10568 | if (err == -EACCES && func_name) | |
10569 | verbose(env, "calling kernel function %s is not allowed\n", func_name); | |
10570 | if (err) | |
10571 | return err; | |
10572 | desc_btf = meta.btf; | |
10573 | insn_aux = &env->insn_aux_data[insn_idx]; | |
00b85860 | 10574 | |
06accc87 AN |
10575 | insn_aux->is_iter_next = is_iter_next_kfunc(&meta); |
10576 | ||
00b85860 KKD |
10577 | if (is_kfunc_destructive(&meta) && !capable(CAP_SYS_BOOT)) { |
10578 | verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capability\n"); | |
4dd48c6f AS |
10579 | return -EACCES; |
10580 | } | |
10581 | ||
9bb00b28 YS |
10582 | sleepable = is_kfunc_sleepable(&meta); |
10583 | if (sleepable && !env->prog->aux->sleepable) { | |
00b85860 KKD |
10584 | verbose(env, "program must be sleepable to call sleepable kfunc %s\n", func_name); |
10585 | return -EACCES; | |
10586 | } | |
eb1f7f71 | 10587 | |
9bb00b28 YS |
10588 | rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta); |
10589 | rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta); | |
9bb00b28 YS |
10590 | |
10591 | if (env->cur_state->active_rcu_lock) { | |
10592 | struct bpf_func_state *state; | |
10593 | struct bpf_reg_state *reg; | |
10594 | ||
10595 | if (rcu_lock) { | |
10596 | verbose(env, "nested rcu read lock (kernel function %s)\n", func_name); | |
10597 | return -EINVAL; | |
10598 | } else if (rcu_unlock) { | |
10599 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ | |
10600 | if (reg->type & MEM_RCU) { | |
fca1aa75 | 10601 | reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL); |
9bb00b28 YS |
10602 | reg->type |= PTR_UNTRUSTED; |
10603 | } | |
10604 | })); | |
10605 | env->cur_state->active_rcu_lock = false; | |
10606 | } else if (sleepable) { | |
10607 | verbose(env, "kernel func %s is sleepable within rcu_read_lock region\n", func_name); | |
10608 | return -EACCES; | |
10609 | } | |
10610 | } else if (rcu_lock) { | |
10611 | env->cur_state->active_rcu_lock = true; | |
10612 | } else if (rcu_unlock) { | |
10613 | verbose(env, "unmatched rcu read unlock (kernel function %s)\n", func_name); | |
10614 | return -EINVAL; | |
10615 | } | |
10616 | ||
e6ac2450 | 10617 | /* Check the arguments */ |
1d18feb2 | 10618 | err = check_kfunc_args(env, &meta, insn_idx); |
5c073f26 | 10619 | if (err < 0) |
e6ac2450 | 10620 | return err; |
5c073f26 | 10621 | /* In case of release function, we get register number of refcounted |
00b85860 | 10622 | * PTR_TO_BTF_ID in bpf_kfunc_arg_meta, do the release now. |
5c073f26 | 10623 | */ |
00b85860 KKD |
10624 | if (meta.release_regno) { |
10625 | err = release_reference(env, regs[meta.release_regno].ref_obj_id); | |
5c073f26 KKD |
10626 | if (err) { |
10627 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
07236eab | 10628 | func_name, meta.func_id); |
5c073f26 KKD |
10629 | return err; |
10630 | } | |
10631 | } | |
e6ac2450 | 10632 | |
6a3cd331 | 10633 | if (meta.func_id == special_kfunc_list[KF_bpf_list_push_front] || |
bd1279ae DM |
10634 | meta.func_id == special_kfunc_list[KF_bpf_list_push_back] || |
10635 | meta.func_id == special_kfunc_list[KF_bpf_rbtree_add]) { | |
6a3cd331 DM |
10636 | release_ref_obj_id = regs[BPF_REG_2].ref_obj_id; |
10637 | err = ref_convert_owning_non_owning(env, release_ref_obj_id); | |
10638 | if (err) { | |
10639 | verbose(env, "kfunc %s#%d conversion of owning ref to non-owning failed\n", | |
07236eab | 10640 | func_name, meta.func_id); |
6a3cd331 DM |
10641 | return err; |
10642 | } | |
10643 | ||
10644 | err = release_reference(env, release_ref_obj_id); | |
10645 | if (err) { | |
10646 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
07236eab | 10647 | func_name, meta.func_id); |
6a3cd331 DM |
10648 | return err; |
10649 | } | |
10650 | } | |
10651 | ||
5d92ddc3 DM |
10652 | if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_add]) { |
10653 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
10654 | set_rbtree_add_callback_state); | |
10655 | if (err) { | |
10656 | verbose(env, "kfunc %s#%d failed callback verification\n", | |
07236eab | 10657 | func_name, meta.func_id); |
5d92ddc3 DM |
10658 | return err; |
10659 | } | |
10660 | } | |
10661 | ||
e6ac2450 MKL |
10662 | for (i = 0; i < CALLER_SAVED_REGS; i++) |
10663 | mark_reg_not_init(env, regs, caller_saved[i]); | |
10664 | ||
10665 | /* Check return type */ | |
07236eab | 10666 | t = btf_type_skip_modifiers(desc_btf, meta.func_proto->type, NULL); |
5c073f26 | 10667 | |
00b85860 | 10668 | if (is_kfunc_acquire(&meta) && !btf_type_is_struct_ptr(meta.btf, t)) { |
958cf2e2 KKD |
10669 | /* Only exception is bpf_obj_new_impl */ |
10670 | if (meta.btf != btf_vmlinux || meta.func_id != special_kfunc_list[KF_bpf_obj_new_impl]) { | |
10671 | verbose(env, "acquire kernel function does not return PTR_TO_BTF_ID\n"); | |
10672 | return -EINVAL; | |
10673 | } | |
5c073f26 KKD |
10674 | } |
10675 | ||
e6ac2450 MKL |
10676 | if (btf_type_is_scalar(t)) { |
10677 | mark_reg_unknown(env, regs, BPF_REG_0); | |
10678 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
10679 | } else if (btf_type_is_ptr(t)) { | |
958cf2e2 KKD |
10680 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, &ptr_type_id); |
10681 | ||
10682 | if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) { | |
10683 | if (meta.func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
958cf2e2 KKD |
10684 | struct btf *ret_btf; |
10685 | u32 ret_btf_id; | |
10686 | ||
e181d3f1 KKD |
10687 | if (unlikely(!bpf_global_ma_set)) |
10688 | return -ENOMEM; | |
10689 | ||
958cf2e2 KKD |
10690 | if (((u64)(u32)meta.arg_constant.value) != meta.arg_constant.value) { |
10691 | verbose(env, "local type ID argument must be in range [0, U32_MAX]\n"); | |
10692 | return -EINVAL; | |
10693 | } | |
10694 | ||
10695 | ret_btf = env->prog->aux->btf; | |
10696 | ret_btf_id = meta.arg_constant.value; | |
10697 | ||
10698 | /* This may be NULL due to user not supplying a BTF */ | |
10699 | if (!ret_btf) { | |
10700 | verbose(env, "bpf_obj_new requires prog BTF\n"); | |
10701 | return -EINVAL; | |
10702 | } | |
10703 | ||
10704 | ret_t = btf_type_by_id(ret_btf, ret_btf_id); | |
10705 | if (!ret_t || !__btf_type_is_struct(ret_t)) { | |
10706 | verbose(env, "bpf_obj_new type ID argument must be of a struct\n"); | |
10707 | return -EINVAL; | |
10708 | } | |
10709 | ||
10710 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10711 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
10712 | regs[BPF_REG_0].btf = ret_btf; | |
10713 | regs[BPF_REG_0].btf_id = ret_btf_id; | |
10714 | ||
07236eab AN |
10715 | insn_aux->obj_new_size = ret_t->size; |
10716 | insn_aux->kptr_struct_meta = | |
958cf2e2 | 10717 | btf_find_struct_meta(ret_btf, ret_btf_id); |
ac9f0605 | 10718 | } else if (meta.func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { |
07236eab | 10719 | insn_aux->kptr_struct_meta = |
ac9f0605 KKD |
10720 | btf_find_struct_meta(meta.arg_obj_drop.btf, |
10721 | meta.arg_obj_drop.btf_id); | |
8cab76ec KKD |
10722 | } else if (meta.func_id == special_kfunc_list[KF_bpf_list_pop_front] || |
10723 | meta.func_id == special_kfunc_list[KF_bpf_list_pop_back]) { | |
10724 | struct btf_field *field = meta.arg_list_head.field; | |
10725 | ||
a40d3632 DM |
10726 | mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root); |
10727 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
10728 | meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { | |
10729 | struct btf_field *field = meta.arg_rbtree_root.field; | |
10730 | ||
10731 | mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root); | |
fd264ca0 YS |
10732 | } else if (meta.func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { |
10733 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10734 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED; | |
10735 | regs[BPF_REG_0].btf = desc_btf; | |
10736 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; | |
a35b9af4 YS |
10737 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { |
10738 | ret_t = btf_type_by_id(desc_btf, meta.arg_constant.value); | |
10739 | if (!ret_t || !btf_type_is_struct(ret_t)) { | |
10740 | verbose(env, | |
10741 | "kfunc bpf_rdonly_cast type ID argument must be of a struct\n"); | |
10742 | return -EINVAL; | |
10743 | } | |
10744 | ||
10745 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10746 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_UNTRUSTED; | |
10747 | regs[BPF_REG_0].btf = desc_btf; | |
10748 | regs[BPF_REG_0].btf_id = meta.arg_constant.value; | |
66e3a13e JK |
10749 | } else if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice] || |
10750 | meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) { | |
10751 | enum bpf_type_flag type_flag = get_dynptr_type_flag(meta.initialized_dynptr.type); | |
10752 | ||
10753 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10754 | ||
10755 | if (!meta.arg_constant.found) { | |
10756 | verbose(env, "verifier internal error: bpf_dynptr_slice(_rdwr) no constant size\n"); | |
10757 | return -EFAULT; | |
10758 | } | |
10759 | ||
10760 | regs[BPF_REG_0].mem_size = meta.arg_constant.value; | |
10761 | ||
10762 | /* PTR_MAYBE_NULL will be added when is_kfunc_ret_null is checked */ | |
10763 | regs[BPF_REG_0].type = PTR_TO_MEM | type_flag; | |
10764 | ||
10765 | if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice]) { | |
10766 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
10767 | } else { | |
10768 | /* this will set env->seen_direct_write to true */ | |
10769 | if (!may_access_direct_pkt_data(env, NULL, BPF_WRITE)) { | |
10770 | verbose(env, "the prog does not allow writes to packet data\n"); | |
10771 | return -EINVAL; | |
10772 | } | |
10773 | } | |
10774 | ||
10775 | if (!meta.initialized_dynptr.id) { | |
10776 | verbose(env, "verifier internal error: no dynptr id\n"); | |
10777 | return -EFAULT; | |
10778 | } | |
10779 | regs[BPF_REG_0].dynptr_id = meta.initialized_dynptr.id; | |
10780 | ||
10781 | /* we don't need to set BPF_REG_0's ref obj id | |
10782 | * because packet slices are not refcounted (see | |
10783 | * dynptr_type_refcounted) | |
10784 | */ | |
958cf2e2 KKD |
10785 | } else { |
10786 | verbose(env, "kernel function %s unhandled dynamic return type\n", | |
10787 | meta.func_name); | |
10788 | return -EFAULT; | |
10789 | } | |
10790 | } else if (!__btf_type_is_struct(ptr_type)) { | |
f4b4eee6 AN |
10791 | if (!meta.r0_size) { |
10792 | __u32 sz; | |
10793 | ||
10794 | if (!IS_ERR(btf_resolve_size(desc_btf, ptr_type, &sz))) { | |
10795 | meta.r0_size = sz; | |
10796 | meta.r0_rdonly = true; | |
10797 | } | |
10798 | } | |
eb1f7f71 BT |
10799 | if (!meta.r0_size) { |
10800 | ptr_type_name = btf_name_by_offset(desc_btf, | |
10801 | ptr_type->name_off); | |
10802 | verbose(env, | |
10803 | "kernel function %s returns pointer type %s %s is not supported\n", | |
10804 | func_name, | |
10805 | btf_type_str(ptr_type), | |
10806 | ptr_type_name); | |
10807 | return -EINVAL; | |
10808 | } | |
10809 | ||
10810 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10811 | regs[BPF_REG_0].type = PTR_TO_MEM; | |
10812 | regs[BPF_REG_0].mem_size = meta.r0_size; | |
10813 | ||
10814 | if (meta.r0_rdonly) | |
10815 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
10816 | ||
10817 | /* Ensures we don't access the memory after a release_reference() */ | |
10818 | if (meta.ref_obj_id) | |
10819 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
10820 | } else { | |
10821 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10822 | regs[BPF_REG_0].btf = desc_btf; | |
10823 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; | |
10824 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
e6ac2450 | 10825 | } |
958cf2e2 | 10826 | |
00b85860 | 10827 | if (is_kfunc_ret_null(&meta)) { |
5c073f26 KKD |
10828 | regs[BPF_REG_0].type |= PTR_MAYBE_NULL; |
10829 | /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ | |
10830 | regs[BPF_REG_0].id = ++env->id_gen; | |
10831 | } | |
e6ac2450 | 10832 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
00b85860 | 10833 | if (is_kfunc_acquire(&meta)) { |
5c073f26 KKD |
10834 | int id = acquire_reference_state(env, insn_idx); |
10835 | ||
10836 | if (id < 0) | |
10837 | return id; | |
00b85860 KKD |
10838 | if (is_kfunc_ret_null(&meta)) |
10839 | regs[BPF_REG_0].id = id; | |
5c073f26 | 10840 | regs[BPF_REG_0].ref_obj_id = id; |
a40d3632 DM |
10841 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { |
10842 | ref_set_non_owning(env, ®s[BPF_REG_0]); | |
5c073f26 | 10843 | } |
a40d3632 DM |
10844 | |
10845 | if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_remove]) | |
10846 | invalidate_non_owning_refs(env); | |
10847 | ||
00b85860 KKD |
10848 | if (reg_may_point_to_spin_lock(®s[BPF_REG_0]) && !regs[BPF_REG_0].id) |
10849 | regs[BPF_REG_0].id = ++env->id_gen; | |
e6ac2450 MKL |
10850 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ |
10851 | ||
07236eab AN |
10852 | nargs = btf_type_vlen(meta.func_proto); |
10853 | args = (const struct btf_param *)(meta.func_proto + 1); | |
e6ac2450 MKL |
10854 | for (i = 0; i < nargs; i++) { |
10855 | u32 regno = i + 1; | |
10856 | ||
2357672c | 10857 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
10858 | if (btf_type_is_ptr(t)) |
10859 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
10860 | else | |
10861 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
10862 | mark_btf_func_reg_size(env, regno, t->size); | |
10863 | } | |
10864 | ||
06accc87 AN |
10865 | if (is_iter_next_kfunc(&meta)) { |
10866 | err = process_iter_next_call(env, insn_idx, &meta); | |
10867 | if (err) | |
10868 | return err; | |
10869 | } | |
10870 | ||
e6ac2450 MKL |
10871 | return 0; |
10872 | } | |
10873 | ||
b03c9f9f EC |
10874 | static bool signed_add_overflows(s64 a, s64 b) |
10875 | { | |
10876 | /* Do the add in u64, where overflow is well-defined */ | |
10877 | s64 res = (s64)((u64)a + (u64)b); | |
10878 | ||
10879 | if (b < 0) | |
10880 | return res > a; | |
10881 | return res < a; | |
10882 | } | |
10883 | ||
bc895e8b | 10884 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
10885 | { |
10886 | /* Do the add in u32, where overflow is well-defined */ | |
10887 | s32 res = (s32)((u32)a + (u32)b); | |
10888 | ||
10889 | if (b < 0) | |
10890 | return res > a; | |
10891 | return res < a; | |
10892 | } | |
10893 | ||
bc895e8b | 10894 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
10895 | { |
10896 | /* Do the sub in u64, where overflow is well-defined */ | |
10897 | s64 res = (s64)((u64)a - (u64)b); | |
10898 | ||
10899 | if (b < 0) | |
10900 | return res < a; | |
10901 | return res > a; | |
969bf05e AS |
10902 | } |
10903 | ||
3f50f132 JF |
10904 | static bool signed_sub32_overflows(s32 a, s32 b) |
10905 | { | |
bc895e8b | 10906 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
10907 | s32 res = (s32)((u32)a - (u32)b); |
10908 | ||
10909 | if (b < 0) | |
10910 | return res < a; | |
10911 | return res > a; | |
10912 | } | |
10913 | ||
bb7f0f98 AS |
10914 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
10915 | const struct bpf_reg_state *reg, | |
10916 | enum bpf_reg_type type) | |
10917 | { | |
10918 | bool known = tnum_is_const(reg->var_off); | |
10919 | s64 val = reg->var_off.value; | |
10920 | s64 smin = reg->smin_value; | |
10921 | ||
10922 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
10923 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
c25b2ae1 | 10924 | reg_type_str(env, type), val); |
bb7f0f98 AS |
10925 | return false; |
10926 | } | |
10927 | ||
10928 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
10929 | verbose(env, "%s pointer offset %d is not allowed\n", | |
c25b2ae1 | 10930 | reg_type_str(env, type), reg->off); |
bb7f0f98 AS |
10931 | return false; |
10932 | } | |
10933 | ||
10934 | if (smin == S64_MIN) { | |
10935 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
c25b2ae1 | 10936 | reg_type_str(env, type)); |
bb7f0f98 AS |
10937 | return false; |
10938 | } | |
10939 | ||
10940 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
10941 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
c25b2ae1 | 10942 | smin, reg_type_str(env, type)); |
bb7f0f98 AS |
10943 | return false; |
10944 | } | |
10945 | ||
10946 | return true; | |
10947 | } | |
10948 | ||
a6aaece0 DB |
10949 | enum { |
10950 | REASON_BOUNDS = -1, | |
10951 | REASON_TYPE = -2, | |
10952 | REASON_PATHS = -3, | |
10953 | REASON_LIMIT = -4, | |
10954 | REASON_STACK = -5, | |
10955 | }; | |
10956 | ||
979d63d5 | 10957 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 10958 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 10959 | { |
7fedb63a | 10960 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
10961 | |
10962 | switch (ptr_reg->type) { | |
10963 | case PTR_TO_STACK: | |
1b1597e6 | 10964 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
10965 | * left direction, see BPF_REG_FP. Also, unknown scalar |
10966 | * offset where we would need to deal with min/max bounds is | |
10967 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
10968 | */ |
10969 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 10970 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 10971 | break; |
979d63d5 | 10972 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 10973 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
10974 | ptr_limit = (mask_to_left ? |
10975 | ptr_reg->smin_value : | |
10976 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 10977 | break; |
979d63d5 | 10978 | default: |
a6aaece0 | 10979 | return REASON_TYPE; |
979d63d5 | 10980 | } |
b658bbb8 DB |
10981 | |
10982 | if (ptr_limit >= max) | |
a6aaece0 | 10983 | return REASON_LIMIT; |
b658bbb8 DB |
10984 | *alu_limit = ptr_limit; |
10985 | return 0; | |
979d63d5 DB |
10986 | } |
10987 | ||
d3bd7413 DB |
10988 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
10989 | const struct bpf_insn *insn) | |
10990 | { | |
2c78ee89 | 10991 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
10992 | } |
10993 | ||
10994 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
10995 | u32 alu_state, u32 alu_limit) | |
10996 | { | |
10997 | /* If we arrived here from different branches with different | |
10998 | * state or limits to sanitize, then this won't work. | |
10999 | */ | |
11000 | if (aux->alu_state && | |
11001 | (aux->alu_state != alu_state || | |
11002 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 11003 | return REASON_PATHS; |
d3bd7413 | 11004 | |
e6ac5933 | 11005 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
11006 | aux->alu_state = alu_state; |
11007 | aux->alu_limit = alu_limit; | |
11008 | return 0; | |
11009 | } | |
11010 | ||
11011 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
11012 | struct bpf_insn *insn) | |
11013 | { | |
11014 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
11015 | ||
11016 | if (can_skip_alu_sanitation(env, insn)) | |
11017 | return 0; | |
11018 | ||
11019 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
11020 | } | |
11021 | ||
f5288193 DB |
11022 | static bool sanitize_needed(u8 opcode) |
11023 | { | |
11024 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
11025 | } | |
11026 | ||
3d0220f6 DB |
11027 | struct bpf_sanitize_info { |
11028 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 11029 | bool mask_to_left; |
3d0220f6 DB |
11030 | }; |
11031 | ||
9183671a DB |
11032 | static struct bpf_verifier_state * |
11033 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
11034 | const struct bpf_insn *insn, | |
11035 | u32 next_idx, u32 curr_idx) | |
11036 | { | |
11037 | struct bpf_verifier_state *branch; | |
11038 | struct bpf_reg_state *regs; | |
11039 | ||
11040 | branch = push_stack(env, next_idx, curr_idx, true); | |
11041 | if (branch && insn) { | |
11042 | regs = branch->frame[branch->curframe]->regs; | |
11043 | if (BPF_SRC(insn->code) == BPF_K) { | |
11044 | mark_reg_unknown(env, regs, insn->dst_reg); | |
11045 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
11046 | mark_reg_unknown(env, regs, insn->dst_reg); | |
11047 | mark_reg_unknown(env, regs, insn->src_reg); | |
11048 | } | |
11049 | } | |
11050 | return branch; | |
11051 | } | |
11052 | ||
979d63d5 DB |
11053 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
11054 | struct bpf_insn *insn, | |
11055 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 11056 | const struct bpf_reg_state *off_reg, |
979d63d5 | 11057 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 11058 | struct bpf_sanitize_info *info, |
7fedb63a | 11059 | const bool commit_window) |
979d63d5 | 11060 | { |
3d0220f6 | 11061 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 11062 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 11063 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 11064 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
11065 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
11066 | u8 opcode = BPF_OP(insn->code); | |
11067 | u32 alu_state, alu_limit; | |
11068 | struct bpf_reg_state tmp; | |
11069 | bool ret; | |
f232326f | 11070 | int err; |
979d63d5 | 11071 | |
d3bd7413 | 11072 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
11073 | return 0; |
11074 | ||
11075 | /* We already marked aux for masking from non-speculative | |
11076 | * paths, thus we got here in the first place. We only care | |
11077 | * to explore bad access from here. | |
11078 | */ | |
11079 | if (vstate->speculative) | |
11080 | goto do_sim; | |
11081 | ||
bb01a1bb DB |
11082 | if (!commit_window) { |
11083 | if (!tnum_is_const(off_reg->var_off) && | |
11084 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
11085 | return REASON_BOUNDS; | |
11086 | ||
11087 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
11088 | (opcode == BPF_SUB && !off_is_neg); | |
11089 | } | |
11090 | ||
11091 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
11092 | if (err < 0) |
11093 | return err; | |
11094 | ||
7fedb63a DB |
11095 | if (commit_window) { |
11096 | /* In commit phase we narrow the masking window based on | |
11097 | * the observed pointer move after the simulated operation. | |
11098 | */ | |
3d0220f6 DB |
11099 | alu_state = info->aux.alu_state; |
11100 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
11101 | } else { |
11102 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 11103 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
11104 | alu_state |= ptr_is_dst_reg ? |
11105 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
11106 | |
11107 | /* Limit pruning on unknown scalars to enable deep search for | |
11108 | * potential masking differences from other program paths. | |
11109 | */ | |
11110 | if (!off_is_imm) | |
11111 | env->explore_alu_limits = true; | |
7fedb63a DB |
11112 | } |
11113 | ||
f232326f PK |
11114 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
11115 | if (err < 0) | |
11116 | return err; | |
979d63d5 | 11117 | do_sim: |
7fedb63a DB |
11118 | /* If we're in commit phase, we're done here given we already |
11119 | * pushed the truncated dst_reg into the speculative verification | |
11120 | * stack. | |
a7036191 DB |
11121 | * |
11122 | * Also, when register is a known constant, we rewrite register-based | |
11123 | * operation to immediate-based, and thus do not need masking (and as | |
11124 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 11125 | */ |
a7036191 | 11126 | if (commit_window || off_is_imm) |
7fedb63a DB |
11127 | return 0; |
11128 | ||
979d63d5 DB |
11129 | /* Simulate and find potential out-of-bounds access under |
11130 | * speculative execution from truncation as a result of | |
11131 | * masking when off was not within expected range. If off | |
11132 | * sits in dst, then we temporarily need to move ptr there | |
11133 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
11134 | * for cases where we use K-based arithmetic in one direction | |
11135 | * and truncated reg-based in the other in order to explore | |
11136 | * bad access. | |
11137 | */ | |
11138 | if (!ptr_is_dst_reg) { | |
11139 | tmp = *dst_reg; | |
71f656a5 | 11140 | copy_register_state(dst_reg, ptr_reg); |
979d63d5 | 11141 | } |
9183671a DB |
11142 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
11143 | env->insn_idx); | |
0803278b | 11144 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 11145 | *dst_reg = tmp; |
a6aaece0 DB |
11146 | return !ret ? REASON_STACK : 0; |
11147 | } | |
11148 | ||
fe9a5ca7 DB |
11149 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
11150 | { | |
11151 | struct bpf_verifier_state *vstate = env->cur_state; | |
11152 | ||
11153 | /* If we simulate paths under speculation, we don't update the | |
11154 | * insn as 'seen' such that when we verify unreachable paths in | |
11155 | * the non-speculative domain, sanitize_dead_code() can still | |
11156 | * rewrite/sanitize them. | |
11157 | */ | |
11158 | if (!vstate->speculative) | |
11159 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
11160 | } | |
11161 | ||
a6aaece0 DB |
11162 | static int sanitize_err(struct bpf_verifier_env *env, |
11163 | const struct bpf_insn *insn, int reason, | |
11164 | const struct bpf_reg_state *off_reg, | |
11165 | const struct bpf_reg_state *dst_reg) | |
11166 | { | |
11167 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
11168 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
11169 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
11170 | ||
11171 | switch (reason) { | |
11172 | case REASON_BOUNDS: | |
11173 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
11174 | off_reg == dst_reg ? dst : src, err); | |
11175 | break; | |
11176 | case REASON_TYPE: | |
11177 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
11178 | off_reg == dst_reg ? src : dst, err); | |
11179 | break; | |
11180 | case REASON_PATHS: | |
11181 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
11182 | dst, op, err); | |
11183 | break; | |
11184 | case REASON_LIMIT: | |
11185 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
11186 | dst, op, err); | |
11187 | break; | |
11188 | case REASON_STACK: | |
11189 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
11190 | dst, err); | |
11191 | break; | |
11192 | default: | |
11193 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
11194 | reason); | |
11195 | break; | |
11196 | } | |
11197 | ||
11198 | return -EACCES; | |
979d63d5 DB |
11199 | } |
11200 | ||
01f810ac AM |
11201 | /* check that stack access falls within stack limits and that 'reg' doesn't |
11202 | * have a variable offset. | |
11203 | * | |
11204 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
11205 | * requires corresponding support in Spectre masking for stack ALU. See also | |
11206 | * retrieve_ptr_limit(). | |
11207 | * | |
11208 | * | |
11209 | * 'off' includes 'reg->off'. | |
11210 | */ | |
11211 | static int check_stack_access_for_ptr_arithmetic( | |
11212 | struct bpf_verifier_env *env, | |
11213 | int regno, | |
11214 | const struct bpf_reg_state *reg, | |
11215 | int off) | |
11216 | { | |
11217 | if (!tnum_is_const(reg->var_off)) { | |
11218 | char tn_buf[48]; | |
11219 | ||
11220 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
11221 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
11222 | regno, tn_buf, off); | |
11223 | return -EACCES; | |
11224 | } | |
11225 | ||
11226 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
11227 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
11228 | "prohibited for !root; off=%d\n", regno, off); | |
11229 | return -EACCES; | |
11230 | } | |
11231 | ||
11232 | return 0; | |
11233 | } | |
11234 | ||
073815b7 DB |
11235 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
11236 | const struct bpf_insn *insn, | |
11237 | const struct bpf_reg_state *dst_reg) | |
11238 | { | |
11239 | u32 dst = insn->dst_reg; | |
11240 | ||
11241 | /* For unprivileged we require that resulting offset must be in bounds | |
11242 | * in order to be able to sanitize access later on. | |
11243 | */ | |
11244 | if (env->bypass_spec_v1) | |
11245 | return 0; | |
11246 | ||
11247 | switch (dst_reg->type) { | |
11248 | case PTR_TO_STACK: | |
11249 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
11250 | dst_reg->off + dst_reg->var_off.value)) | |
11251 | return -EACCES; | |
11252 | break; | |
11253 | case PTR_TO_MAP_VALUE: | |
61df10c7 | 11254 | if (check_map_access(env, dst, dst_reg->off, 1, false, ACCESS_HELPER)) { |
073815b7 DB |
11255 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
11256 | "prohibited for !root\n", dst); | |
11257 | return -EACCES; | |
11258 | } | |
11259 | break; | |
11260 | default: | |
11261 | break; | |
11262 | } | |
11263 | ||
11264 | return 0; | |
11265 | } | |
01f810ac | 11266 | |
f1174f77 | 11267 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
11268 | * Caller should also handle BPF_MOV case separately. |
11269 | * If we return -EACCES, caller may want to try again treating pointer as a | |
11270 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
11271 | */ | |
11272 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
11273 | struct bpf_insn *insn, | |
11274 | const struct bpf_reg_state *ptr_reg, | |
11275 | const struct bpf_reg_state *off_reg) | |
969bf05e | 11276 | { |
f4d7e40a AS |
11277 | struct bpf_verifier_state *vstate = env->cur_state; |
11278 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
11279 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 11280 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
11281 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
11282 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
11283 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
11284 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 11285 | struct bpf_sanitize_info info = {}; |
969bf05e | 11286 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 11287 | u32 dst = insn->dst_reg; |
979d63d5 | 11288 | int ret; |
969bf05e | 11289 | |
f1174f77 | 11290 | dst_reg = ®s[dst]; |
969bf05e | 11291 | |
6f16101e DB |
11292 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
11293 | smin_val > smax_val || umin_val > umax_val) { | |
11294 | /* Taint dst register if offset had invalid bounds derived from | |
11295 | * e.g. dead branches. | |
11296 | */ | |
f54c7898 | 11297 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 11298 | return 0; |
f1174f77 EC |
11299 | } |
11300 | ||
11301 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
11302 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
11303 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
11304 | __mark_reg_unknown(env, dst_reg); | |
11305 | return 0; | |
11306 | } | |
11307 | ||
82abbf8d AS |
11308 | verbose(env, |
11309 | "R%d 32-bit pointer arithmetic prohibited\n", | |
11310 | dst); | |
f1174f77 | 11311 | return -EACCES; |
969bf05e AS |
11312 | } |
11313 | ||
c25b2ae1 | 11314 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
aad2eeaf | 11315 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
c25b2ae1 | 11316 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 11317 | return -EACCES; |
c25b2ae1 HL |
11318 | } |
11319 | ||
11320 | switch (base_type(ptr_reg->type)) { | |
aad2eeaf | 11321 | case CONST_PTR_TO_MAP: |
7c696732 YS |
11322 | /* smin_val represents the known value */ |
11323 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
11324 | break; | |
8731745e | 11325 | fallthrough; |
aad2eeaf | 11326 | case PTR_TO_PACKET_END: |
c64b7983 | 11327 | case PTR_TO_SOCKET: |
46f8bc92 | 11328 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 11329 | case PTR_TO_TCP_SOCK: |
fada7fdc | 11330 | case PTR_TO_XDP_SOCK: |
aad2eeaf | 11331 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
c25b2ae1 | 11332 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 11333 | return -EACCES; |
aad2eeaf JS |
11334 | default: |
11335 | break; | |
f1174f77 EC |
11336 | } |
11337 | ||
11338 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
11339 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 11340 | */ |
f1174f77 EC |
11341 | dst_reg->type = ptr_reg->type; |
11342 | dst_reg->id = ptr_reg->id; | |
969bf05e | 11343 | |
bb7f0f98 AS |
11344 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
11345 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
11346 | return -EINVAL; | |
11347 | ||
3f50f132 JF |
11348 | /* pointer types do not carry 32-bit bounds at the moment. */ |
11349 | __mark_reg32_unbounded(dst_reg); | |
11350 | ||
7fedb63a DB |
11351 | if (sanitize_needed(opcode)) { |
11352 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 11353 | &info, false); |
a6aaece0 DB |
11354 | if (ret < 0) |
11355 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 11356 | } |
a6aaece0 | 11357 | |
f1174f77 EC |
11358 | switch (opcode) { |
11359 | case BPF_ADD: | |
11360 | /* We can take a fixed offset as long as it doesn't overflow | |
11361 | * the s32 'off' field | |
969bf05e | 11362 | */ |
b03c9f9f EC |
11363 | if (known && (ptr_reg->off + smin_val == |
11364 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 11365 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
11366 | dst_reg->smin_value = smin_ptr; |
11367 | dst_reg->smax_value = smax_ptr; | |
11368 | dst_reg->umin_value = umin_ptr; | |
11369 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 11370 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 11371 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 11372 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
11373 | break; |
11374 | } | |
f1174f77 EC |
11375 | /* A new variable offset is created. Note that off_reg->off |
11376 | * == 0, since it's a scalar. | |
11377 | * dst_reg gets the pointer type and since some positive | |
11378 | * integer value was added to the pointer, give it a new 'id' | |
11379 | * if it's a PTR_TO_PACKET. | |
11380 | * this creates a new 'base' pointer, off_reg (variable) gets | |
11381 | * added into the variable offset, and we copy the fixed offset | |
11382 | * from ptr_reg. | |
969bf05e | 11383 | */ |
b03c9f9f EC |
11384 | if (signed_add_overflows(smin_ptr, smin_val) || |
11385 | signed_add_overflows(smax_ptr, smax_val)) { | |
11386 | dst_reg->smin_value = S64_MIN; | |
11387 | dst_reg->smax_value = S64_MAX; | |
11388 | } else { | |
11389 | dst_reg->smin_value = smin_ptr + smin_val; | |
11390 | dst_reg->smax_value = smax_ptr + smax_val; | |
11391 | } | |
11392 | if (umin_ptr + umin_val < umin_ptr || | |
11393 | umax_ptr + umax_val < umax_ptr) { | |
11394 | dst_reg->umin_value = 0; | |
11395 | dst_reg->umax_value = U64_MAX; | |
11396 | } else { | |
11397 | dst_reg->umin_value = umin_ptr + umin_val; | |
11398 | dst_reg->umax_value = umax_ptr + umax_val; | |
11399 | } | |
f1174f77 EC |
11400 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
11401 | dst_reg->off = ptr_reg->off; | |
0962590e | 11402 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 11403 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
11404 | dst_reg->id = ++env->id_gen; |
11405 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 11406 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
11407 | } |
11408 | break; | |
11409 | case BPF_SUB: | |
11410 | if (dst_reg == off_reg) { | |
11411 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
11412 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
11413 | dst); | |
f1174f77 EC |
11414 | return -EACCES; |
11415 | } | |
11416 | /* We don't allow subtraction from FP, because (according to | |
11417 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
11418 | * be able to deal with it. | |
969bf05e | 11419 | */ |
f1174f77 | 11420 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
11421 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
11422 | dst); | |
f1174f77 EC |
11423 | return -EACCES; |
11424 | } | |
b03c9f9f EC |
11425 | if (known && (ptr_reg->off - smin_val == |
11426 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 11427 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
11428 | dst_reg->smin_value = smin_ptr; |
11429 | dst_reg->smax_value = smax_ptr; | |
11430 | dst_reg->umin_value = umin_ptr; | |
11431 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
11432 | dst_reg->var_off = ptr_reg->var_off; |
11433 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 11434 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 11435 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
11436 | break; |
11437 | } | |
f1174f77 EC |
11438 | /* A new variable offset is created. If the subtrahend is known |
11439 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 11440 | */ |
b03c9f9f EC |
11441 | if (signed_sub_overflows(smin_ptr, smax_val) || |
11442 | signed_sub_overflows(smax_ptr, smin_val)) { | |
11443 | /* Overflow possible, we know nothing */ | |
11444 | dst_reg->smin_value = S64_MIN; | |
11445 | dst_reg->smax_value = S64_MAX; | |
11446 | } else { | |
11447 | dst_reg->smin_value = smin_ptr - smax_val; | |
11448 | dst_reg->smax_value = smax_ptr - smin_val; | |
11449 | } | |
11450 | if (umin_ptr < umax_val) { | |
11451 | /* Overflow possible, we know nothing */ | |
11452 | dst_reg->umin_value = 0; | |
11453 | dst_reg->umax_value = U64_MAX; | |
11454 | } else { | |
11455 | /* Cannot overflow (as long as bounds are consistent) */ | |
11456 | dst_reg->umin_value = umin_ptr - umax_val; | |
11457 | dst_reg->umax_value = umax_ptr - umin_val; | |
11458 | } | |
f1174f77 EC |
11459 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
11460 | dst_reg->off = ptr_reg->off; | |
0962590e | 11461 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 11462 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
11463 | dst_reg->id = ++env->id_gen; |
11464 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 11465 | if (smin_val < 0) |
22dc4a0f | 11466 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 11467 | } |
f1174f77 EC |
11468 | break; |
11469 | case BPF_AND: | |
11470 | case BPF_OR: | |
11471 | case BPF_XOR: | |
82abbf8d AS |
11472 | /* bitwise ops on pointers are troublesome, prohibit. */ |
11473 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
11474 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
11475 | return -EACCES; |
11476 | default: | |
11477 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
11478 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
11479 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 11480 | return -EACCES; |
43188702 JF |
11481 | } |
11482 | ||
bb7f0f98 AS |
11483 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
11484 | return -EINVAL; | |
3844d153 | 11485 | reg_bounds_sync(dst_reg); |
073815b7 DB |
11486 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
11487 | return -EACCES; | |
7fedb63a DB |
11488 | if (sanitize_needed(opcode)) { |
11489 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 11490 | &info, true); |
7fedb63a DB |
11491 | if (ret < 0) |
11492 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
11493 | } |
11494 | ||
43188702 JF |
11495 | return 0; |
11496 | } | |
11497 | ||
3f50f132 JF |
11498 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
11499 | struct bpf_reg_state *src_reg) | |
11500 | { | |
11501 | s32 smin_val = src_reg->s32_min_value; | |
11502 | s32 smax_val = src_reg->s32_max_value; | |
11503 | u32 umin_val = src_reg->u32_min_value; | |
11504 | u32 umax_val = src_reg->u32_max_value; | |
11505 | ||
11506 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
11507 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
11508 | dst_reg->s32_min_value = S32_MIN; | |
11509 | dst_reg->s32_max_value = S32_MAX; | |
11510 | } else { | |
11511 | dst_reg->s32_min_value += smin_val; | |
11512 | dst_reg->s32_max_value += smax_val; | |
11513 | } | |
11514 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
11515 | dst_reg->u32_max_value + umax_val < umax_val) { | |
11516 | dst_reg->u32_min_value = 0; | |
11517 | dst_reg->u32_max_value = U32_MAX; | |
11518 | } else { | |
11519 | dst_reg->u32_min_value += umin_val; | |
11520 | dst_reg->u32_max_value += umax_val; | |
11521 | } | |
11522 | } | |
11523 | ||
07cd2631 JF |
11524 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
11525 | struct bpf_reg_state *src_reg) | |
11526 | { | |
11527 | s64 smin_val = src_reg->smin_value; | |
11528 | s64 smax_val = src_reg->smax_value; | |
11529 | u64 umin_val = src_reg->umin_value; | |
11530 | u64 umax_val = src_reg->umax_value; | |
11531 | ||
11532 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
11533 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
11534 | dst_reg->smin_value = S64_MIN; | |
11535 | dst_reg->smax_value = S64_MAX; | |
11536 | } else { | |
11537 | dst_reg->smin_value += smin_val; | |
11538 | dst_reg->smax_value += smax_val; | |
11539 | } | |
11540 | if (dst_reg->umin_value + umin_val < umin_val || | |
11541 | dst_reg->umax_value + umax_val < umax_val) { | |
11542 | dst_reg->umin_value = 0; | |
11543 | dst_reg->umax_value = U64_MAX; | |
11544 | } else { | |
11545 | dst_reg->umin_value += umin_val; | |
11546 | dst_reg->umax_value += umax_val; | |
11547 | } | |
3f50f132 JF |
11548 | } |
11549 | ||
11550 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
11551 | struct bpf_reg_state *src_reg) | |
11552 | { | |
11553 | s32 smin_val = src_reg->s32_min_value; | |
11554 | s32 smax_val = src_reg->s32_max_value; | |
11555 | u32 umin_val = src_reg->u32_min_value; | |
11556 | u32 umax_val = src_reg->u32_max_value; | |
11557 | ||
11558 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
11559 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
11560 | /* Overflow possible, we know nothing */ | |
11561 | dst_reg->s32_min_value = S32_MIN; | |
11562 | dst_reg->s32_max_value = S32_MAX; | |
11563 | } else { | |
11564 | dst_reg->s32_min_value -= smax_val; | |
11565 | dst_reg->s32_max_value -= smin_val; | |
11566 | } | |
11567 | if (dst_reg->u32_min_value < umax_val) { | |
11568 | /* Overflow possible, we know nothing */ | |
11569 | dst_reg->u32_min_value = 0; | |
11570 | dst_reg->u32_max_value = U32_MAX; | |
11571 | } else { | |
11572 | /* Cannot overflow (as long as bounds are consistent) */ | |
11573 | dst_reg->u32_min_value -= umax_val; | |
11574 | dst_reg->u32_max_value -= umin_val; | |
11575 | } | |
07cd2631 JF |
11576 | } |
11577 | ||
11578 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
11579 | struct bpf_reg_state *src_reg) | |
11580 | { | |
11581 | s64 smin_val = src_reg->smin_value; | |
11582 | s64 smax_val = src_reg->smax_value; | |
11583 | u64 umin_val = src_reg->umin_value; | |
11584 | u64 umax_val = src_reg->umax_value; | |
11585 | ||
11586 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
11587 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
11588 | /* Overflow possible, we know nothing */ | |
11589 | dst_reg->smin_value = S64_MIN; | |
11590 | dst_reg->smax_value = S64_MAX; | |
11591 | } else { | |
11592 | dst_reg->smin_value -= smax_val; | |
11593 | dst_reg->smax_value -= smin_val; | |
11594 | } | |
11595 | if (dst_reg->umin_value < umax_val) { | |
11596 | /* Overflow possible, we know nothing */ | |
11597 | dst_reg->umin_value = 0; | |
11598 | dst_reg->umax_value = U64_MAX; | |
11599 | } else { | |
11600 | /* Cannot overflow (as long as bounds are consistent) */ | |
11601 | dst_reg->umin_value -= umax_val; | |
11602 | dst_reg->umax_value -= umin_val; | |
11603 | } | |
3f50f132 JF |
11604 | } |
11605 | ||
11606 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
11607 | struct bpf_reg_state *src_reg) | |
11608 | { | |
11609 | s32 smin_val = src_reg->s32_min_value; | |
11610 | u32 umin_val = src_reg->u32_min_value; | |
11611 | u32 umax_val = src_reg->u32_max_value; | |
11612 | ||
11613 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
11614 | /* Ain't nobody got time to multiply that sign */ | |
11615 | __mark_reg32_unbounded(dst_reg); | |
11616 | return; | |
11617 | } | |
11618 | /* Both values are positive, so we can work with unsigned and | |
11619 | * copy the result to signed (unless it exceeds S32_MAX). | |
11620 | */ | |
11621 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
11622 | /* Potential overflow, we know nothing */ | |
11623 | __mark_reg32_unbounded(dst_reg); | |
11624 | return; | |
11625 | } | |
11626 | dst_reg->u32_min_value *= umin_val; | |
11627 | dst_reg->u32_max_value *= umax_val; | |
11628 | if (dst_reg->u32_max_value > S32_MAX) { | |
11629 | /* Overflow possible, we know nothing */ | |
11630 | dst_reg->s32_min_value = S32_MIN; | |
11631 | dst_reg->s32_max_value = S32_MAX; | |
11632 | } else { | |
11633 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11634 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11635 | } | |
07cd2631 JF |
11636 | } |
11637 | ||
11638 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
11639 | struct bpf_reg_state *src_reg) | |
11640 | { | |
11641 | s64 smin_val = src_reg->smin_value; | |
11642 | u64 umin_val = src_reg->umin_value; | |
11643 | u64 umax_val = src_reg->umax_value; | |
11644 | ||
07cd2631 JF |
11645 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
11646 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 11647 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
11648 | return; |
11649 | } | |
11650 | /* Both values are positive, so we can work with unsigned and | |
11651 | * copy the result to signed (unless it exceeds S64_MAX). | |
11652 | */ | |
11653 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
11654 | /* Potential overflow, we know nothing */ | |
3f50f132 | 11655 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
11656 | return; |
11657 | } | |
11658 | dst_reg->umin_value *= umin_val; | |
11659 | dst_reg->umax_value *= umax_val; | |
11660 | if (dst_reg->umax_value > S64_MAX) { | |
11661 | /* Overflow possible, we know nothing */ | |
11662 | dst_reg->smin_value = S64_MIN; | |
11663 | dst_reg->smax_value = S64_MAX; | |
11664 | } else { | |
11665 | dst_reg->smin_value = dst_reg->umin_value; | |
11666 | dst_reg->smax_value = dst_reg->umax_value; | |
11667 | } | |
11668 | } | |
11669 | ||
3f50f132 JF |
11670 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
11671 | struct bpf_reg_state *src_reg) | |
11672 | { | |
11673 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11674 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11675 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
11676 | s32 smin_val = src_reg->s32_min_value; | |
11677 | u32 umax_val = src_reg->u32_max_value; | |
11678 | ||
049c4e13 DB |
11679 | if (src_known && dst_known) { |
11680 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 11681 | return; |
049c4e13 | 11682 | } |
3f50f132 JF |
11683 | |
11684 | /* We get our minimum from the var_off, since that's inherently | |
11685 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
11686 | */ | |
11687 | dst_reg->u32_min_value = var32_off.value; | |
11688 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
11689 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
11690 | /* Lose signed bounds when ANDing negative numbers, | |
11691 | * ain't nobody got time for that. | |
11692 | */ | |
11693 | dst_reg->s32_min_value = S32_MIN; | |
11694 | dst_reg->s32_max_value = S32_MAX; | |
11695 | } else { | |
11696 | /* ANDing two positives gives a positive, so safe to | |
11697 | * cast result into s64. | |
11698 | */ | |
11699 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11700 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11701 | } | |
3f50f132 JF |
11702 | } |
11703 | ||
07cd2631 JF |
11704 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
11705 | struct bpf_reg_state *src_reg) | |
11706 | { | |
3f50f132 JF |
11707 | bool src_known = tnum_is_const(src_reg->var_off); |
11708 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
11709 | s64 smin_val = src_reg->smin_value; |
11710 | u64 umax_val = src_reg->umax_value; | |
11711 | ||
3f50f132 | 11712 | if (src_known && dst_known) { |
4fbb38a3 | 11713 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
11714 | return; |
11715 | } | |
11716 | ||
07cd2631 JF |
11717 | /* We get our minimum from the var_off, since that's inherently |
11718 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
11719 | */ | |
07cd2631 JF |
11720 | dst_reg->umin_value = dst_reg->var_off.value; |
11721 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
11722 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
11723 | /* Lose signed bounds when ANDing negative numbers, | |
11724 | * ain't nobody got time for that. | |
11725 | */ | |
11726 | dst_reg->smin_value = S64_MIN; | |
11727 | dst_reg->smax_value = S64_MAX; | |
11728 | } else { | |
11729 | /* ANDing two positives gives a positive, so safe to | |
11730 | * cast result into s64. | |
11731 | */ | |
11732 | dst_reg->smin_value = dst_reg->umin_value; | |
11733 | dst_reg->smax_value = dst_reg->umax_value; | |
11734 | } | |
11735 | /* We may learn something more from the var_off */ | |
11736 | __update_reg_bounds(dst_reg); | |
11737 | } | |
11738 | ||
3f50f132 JF |
11739 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
11740 | struct bpf_reg_state *src_reg) | |
11741 | { | |
11742 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11743 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11744 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
11745 | s32 smin_val = src_reg->s32_min_value; |
11746 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 11747 | |
049c4e13 DB |
11748 | if (src_known && dst_known) { |
11749 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 11750 | return; |
049c4e13 | 11751 | } |
3f50f132 JF |
11752 | |
11753 | /* We get our maximum from the var_off, and our minimum is the | |
11754 | * maximum of the operands' minima | |
11755 | */ | |
11756 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
11757 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
11758 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
11759 | /* Lose signed bounds when ORing negative numbers, | |
11760 | * ain't nobody got time for that. | |
11761 | */ | |
11762 | dst_reg->s32_min_value = S32_MIN; | |
11763 | dst_reg->s32_max_value = S32_MAX; | |
11764 | } else { | |
11765 | /* ORing two positives gives a positive, so safe to | |
11766 | * cast result into s64. | |
11767 | */ | |
5b9fbeb7 DB |
11768 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
11769 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
11770 | } |
11771 | } | |
11772 | ||
07cd2631 JF |
11773 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
11774 | struct bpf_reg_state *src_reg) | |
11775 | { | |
3f50f132 JF |
11776 | bool src_known = tnum_is_const(src_reg->var_off); |
11777 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
11778 | s64 smin_val = src_reg->smin_value; |
11779 | u64 umin_val = src_reg->umin_value; | |
11780 | ||
3f50f132 | 11781 | if (src_known && dst_known) { |
4fbb38a3 | 11782 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
11783 | return; |
11784 | } | |
11785 | ||
07cd2631 JF |
11786 | /* We get our maximum from the var_off, and our minimum is the |
11787 | * maximum of the operands' minima | |
11788 | */ | |
07cd2631 JF |
11789 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
11790 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
11791 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
11792 | /* Lose signed bounds when ORing negative numbers, | |
11793 | * ain't nobody got time for that. | |
11794 | */ | |
11795 | dst_reg->smin_value = S64_MIN; | |
11796 | dst_reg->smax_value = S64_MAX; | |
11797 | } else { | |
11798 | /* ORing two positives gives a positive, so safe to | |
11799 | * cast result into s64. | |
11800 | */ | |
11801 | dst_reg->smin_value = dst_reg->umin_value; | |
11802 | dst_reg->smax_value = dst_reg->umax_value; | |
11803 | } | |
11804 | /* We may learn something more from the var_off */ | |
11805 | __update_reg_bounds(dst_reg); | |
11806 | } | |
11807 | ||
2921c90d YS |
11808 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
11809 | struct bpf_reg_state *src_reg) | |
11810 | { | |
11811 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11812 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11813 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
11814 | s32 smin_val = src_reg->s32_min_value; | |
11815 | ||
049c4e13 DB |
11816 | if (src_known && dst_known) { |
11817 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 11818 | return; |
049c4e13 | 11819 | } |
2921c90d YS |
11820 | |
11821 | /* We get both minimum and maximum from the var32_off. */ | |
11822 | dst_reg->u32_min_value = var32_off.value; | |
11823 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
11824 | ||
11825 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
11826 | /* XORing two positive sign numbers gives a positive, | |
11827 | * so safe to cast u32 result into s32. | |
11828 | */ | |
11829 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11830 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11831 | } else { | |
11832 | dst_reg->s32_min_value = S32_MIN; | |
11833 | dst_reg->s32_max_value = S32_MAX; | |
11834 | } | |
11835 | } | |
11836 | ||
11837 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
11838 | struct bpf_reg_state *src_reg) | |
11839 | { | |
11840 | bool src_known = tnum_is_const(src_reg->var_off); | |
11841 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
11842 | s64 smin_val = src_reg->smin_value; | |
11843 | ||
11844 | if (src_known && dst_known) { | |
11845 | /* dst_reg->var_off.value has been updated earlier */ | |
11846 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
11847 | return; | |
11848 | } | |
11849 | ||
11850 | /* We get both minimum and maximum from the var_off. */ | |
11851 | dst_reg->umin_value = dst_reg->var_off.value; | |
11852 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
11853 | ||
11854 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
11855 | /* XORing two positive sign numbers gives a positive, | |
11856 | * so safe to cast u64 result into s64. | |
11857 | */ | |
11858 | dst_reg->smin_value = dst_reg->umin_value; | |
11859 | dst_reg->smax_value = dst_reg->umax_value; | |
11860 | } else { | |
11861 | dst_reg->smin_value = S64_MIN; | |
11862 | dst_reg->smax_value = S64_MAX; | |
11863 | } | |
11864 | ||
11865 | __update_reg_bounds(dst_reg); | |
11866 | } | |
11867 | ||
3f50f132 JF |
11868 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
11869 | u64 umin_val, u64 umax_val) | |
07cd2631 | 11870 | { |
07cd2631 JF |
11871 | /* We lose all sign bit information (except what we can pick |
11872 | * up from var_off) | |
11873 | */ | |
3f50f132 JF |
11874 | dst_reg->s32_min_value = S32_MIN; |
11875 | dst_reg->s32_max_value = S32_MAX; | |
11876 | /* If we might shift our top bit out, then we know nothing */ | |
11877 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
11878 | dst_reg->u32_min_value = 0; | |
11879 | dst_reg->u32_max_value = U32_MAX; | |
11880 | } else { | |
11881 | dst_reg->u32_min_value <<= umin_val; | |
11882 | dst_reg->u32_max_value <<= umax_val; | |
11883 | } | |
11884 | } | |
11885 | ||
11886 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
11887 | struct bpf_reg_state *src_reg) | |
11888 | { | |
11889 | u32 umax_val = src_reg->u32_max_value; | |
11890 | u32 umin_val = src_reg->u32_min_value; | |
11891 | /* u32 alu operation will zext upper bits */ | |
11892 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
11893 | ||
11894 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
11895 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
11896 | /* Not required but being careful mark reg64 bounds as unknown so | |
11897 | * that we are forced to pick them up from tnum and zext later and | |
11898 | * if some path skips this step we are still safe. | |
11899 | */ | |
11900 | __mark_reg64_unbounded(dst_reg); | |
11901 | __update_reg32_bounds(dst_reg); | |
11902 | } | |
11903 | ||
11904 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
11905 | u64 umin_val, u64 umax_val) | |
11906 | { | |
11907 | /* Special case <<32 because it is a common compiler pattern to sign | |
11908 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
11909 | * positive we know this shift will also be positive so we can track | |
11910 | * bounds correctly. Otherwise we lose all sign bit information except | |
11911 | * what we can pick up from var_off. Perhaps we can generalize this | |
11912 | * later to shifts of any length. | |
11913 | */ | |
11914 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
11915 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
11916 | else | |
11917 | dst_reg->smax_value = S64_MAX; | |
11918 | ||
11919 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
11920 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
11921 | else | |
11922 | dst_reg->smin_value = S64_MIN; | |
11923 | ||
07cd2631 JF |
11924 | /* If we might shift our top bit out, then we know nothing */ |
11925 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
11926 | dst_reg->umin_value = 0; | |
11927 | dst_reg->umax_value = U64_MAX; | |
11928 | } else { | |
11929 | dst_reg->umin_value <<= umin_val; | |
11930 | dst_reg->umax_value <<= umax_val; | |
11931 | } | |
3f50f132 JF |
11932 | } |
11933 | ||
11934 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
11935 | struct bpf_reg_state *src_reg) | |
11936 | { | |
11937 | u64 umax_val = src_reg->umax_value; | |
11938 | u64 umin_val = src_reg->umin_value; | |
11939 | ||
11940 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
11941 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
11942 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
11943 | ||
07cd2631 JF |
11944 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
11945 | /* We may learn something more from the var_off */ | |
11946 | __update_reg_bounds(dst_reg); | |
11947 | } | |
11948 | ||
3f50f132 JF |
11949 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
11950 | struct bpf_reg_state *src_reg) | |
11951 | { | |
11952 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
11953 | u32 umax_val = src_reg->u32_max_value; | |
11954 | u32 umin_val = src_reg->u32_min_value; | |
11955 | ||
11956 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
11957 | * be negative, then either: | |
11958 | * 1) src_reg might be zero, so the sign bit of the result is | |
11959 | * unknown, so we lose our signed bounds | |
11960 | * 2) it's known negative, thus the unsigned bounds capture the | |
11961 | * signed bounds | |
11962 | * 3) the signed bounds cross zero, so they tell us nothing | |
11963 | * about the result | |
11964 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 11965 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
11966 | * Thus, in all cases it suffices to blow away our signed bounds |
11967 | * and rely on inferring new ones from the unsigned bounds and | |
11968 | * var_off of the result. | |
11969 | */ | |
11970 | dst_reg->s32_min_value = S32_MIN; | |
11971 | dst_reg->s32_max_value = S32_MAX; | |
11972 | ||
11973 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
11974 | dst_reg->u32_min_value >>= umax_val; | |
11975 | dst_reg->u32_max_value >>= umin_val; | |
11976 | ||
11977 | __mark_reg64_unbounded(dst_reg); | |
11978 | __update_reg32_bounds(dst_reg); | |
11979 | } | |
11980 | ||
07cd2631 JF |
11981 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
11982 | struct bpf_reg_state *src_reg) | |
11983 | { | |
11984 | u64 umax_val = src_reg->umax_value; | |
11985 | u64 umin_val = src_reg->umin_value; | |
11986 | ||
11987 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
11988 | * be negative, then either: | |
11989 | * 1) src_reg might be zero, so the sign bit of the result is | |
11990 | * unknown, so we lose our signed bounds | |
11991 | * 2) it's known negative, thus the unsigned bounds capture the | |
11992 | * signed bounds | |
11993 | * 3) the signed bounds cross zero, so they tell us nothing | |
11994 | * about the result | |
11995 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 11996 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
11997 | * Thus, in all cases it suffices to blow away our signed bounds |
11998 | * and rely on inferring new ones from the unsigned bounds and | |
11999 | * var_off of the result. | |
12000 | */ | |
12001 | dst_reg->smin_value = S64_MIN; | |
12002 | dst_reg->smax_value = S64_MAX; | |
12003 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
12004 | dst_reg->umin_value >>= umax_val; | |
12005 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
12006 | |
12007 | /* Its not easy to operate on alu32 bounds here because it depends | |
12008 | * on bits being shifted in. Take easy way out and mark unbounded | |
12009 | * so we can recalculate later from tnum. | |
12010 | */ | |
12011 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
12012 | __update_reg_bounds(dst_reg); |
12013 | } | |
12014 | ||
3f50f132 JF |
12015 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
12016 | struct bpf_reg_state *src_reg) | |
07cd2631 | 12017 | { |
3f50f132 | 12018 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
12019 | |
12020 | /* Upon reaching here, src_known is true and | |
12021 | * umax_val is equal to umin_val. | |
12022 | */ | |
3f50f132 JF |
12023 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
12024 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 12025 | |
3f50f132 JF |
12026 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
12027 | ||
12028 | /* blow away the dst_reg umin_value/umax_value and rely on | |
12029 | * dst_reg var_off to refine the result. | |
12030 | */ | |
12031 | dst_reg->u32_min_value = 0; | |
12032 | dst_reg->u32_max_value = U32_MAX; | |
12033 | ||
12034 | __mark_reg64_unbounded(dst_reg); | |
12035 | __update_reg32_bounds(dst_reg); | |
12036 | } | |
12037 | ||
12038 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
12039 | struct bpf_reg_state *src_reg) | |
12040 | { | |
12041 | u64 umin_val = src_reg->umin_value; | |
12042 | ||
12043 | /* Upon reaching here, src_known is true and umax_val is equal | |
12044 | * to umin_val. | |
12045 | */ | |
12046 | dst_reg->smin_value >>= umin_val; | |
12047 | dst_reg->smax_value >>= umin_val; | |
12048 | ||
12049 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
12050 | |
12051 | /* blow away the dst_reg umin_value/umax_value and rely on | |
12052 | * dst_reg var_off to refine the result. | |
12053 | */ | |
12054 | dst_reg->umin_value = 0; | |
12055 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
12056 | |
12057 | /* Its not easy to operate on alu32 bounds here because it depends | |
12058 | * on bits being shifted in from upper 32-bits. Take easy way out | |
12059 | * and mark unbounded so we can recalculate later from tnum. | |
12060 | */ | |
12061 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
12062 | __update_reg_bounds(dst_reg); |
12063 | } | |
12064 | ||
468f6eaf JH |
12065 | /* WARNING: This function does calculations on 64-bit values, but the actual |
12066 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
12067 | * need extra checks in the 32-bit case. | |
12068 | */ | |
f1174f77 EC |
12069 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
12070 | struct bpf_insn *insn, | |
12071 | struct bpf_reg_state *dst_reg, | |
12072 | struct bpf_reg_state src_reg) | |
969bf05e | 12073 | { |
638f5b90 | 12074 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 12075 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 12076 | bool src_known; |
b03c9f9f EC |
12077 | s64 smin_val, smax_val; |
12078 | u64 umin_val, umax_val; | |
3f50f132 JF |
12079 | s32 s32_min_val, s32_max_val; |
12080 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 12081 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 12082 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 12083 | int ret; |
b799207e | 12084 | |
b03c9f9f EC |
12085 | smin_val = src_reg.smin_value; |
12086 | smax_val = src_reg.smax_value; | |
12087 | umin_val = src_reg.umin_value; | |
12088 | umax_val = src_reg.umax_value; | |
f23cc643 | 12089 | |
3f50f132 JF |
12090 | s32_min_val = src_reg.s32_min_value; |
12091 | s32_max_val = src_reg.s32_max_value; | |
12092 | u32_min_val = src_reg.u32_min_value; | |
12093 | u32_max_val = src_reg.u32_max_value; | |
12094 | ||
12095 | if (alu32) { | |
12096 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
12097 | if ((src_known && |
12098 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
12099 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
12100 | /* Taint dst register if offset had invalid bounds | |
12101 | * derived from e.g. dead branches. | |
12102 | */ | |
12103 | __mark_reg_unknown(env, dst_reg); | |
12104 | return 0; | |
12105 | } | |
12106 | } else { | |
12107 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
12108 | if ((src_known && |
12109 | (smin_val != smax_val || umin_val != umax_val)) || | |
12110 | smin_val > smax_val || umin_val > umax_val) { | |
12111 | /* Taint dst register if offset had invalid bounds | |
12112 | * derived from e.g. dead branches. | |
12113 | */ | |
12114 | __mark_reg_unknown(env, dst_reg); | |
12115 | return 0; | |
12116 | } | |
6f16101e DB |
12117 | } |
12118 | ||
bb7f0f98 AS |
12119 | if (!src_known && |
12120 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 12121 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
12122 | return 0; |
12123 | } | |
12124 | ||
f5288193 DB |
12125 | if (sanitize_needed(opcode)) { |
12126 | ret = sanitize_val_alu(env, insn); | |
12127 | if (ret < 0) | |
12128 | return sanitize_err(env, insn, ret, NULL, NULL); | |
12129 | } | |
12130 | ||
3f50f132 JF |
12131 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
12132 | * There are two classes of instructions: The first class we track both | |
12133 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
12134 | * greatest amount of precision when alu operations are mixed with jmp32 | |
12135 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
12136 | * and BPF_OR. This is possible because these ops have fairly easy to | |
12137 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
12138 | * See alu32 verifier tests for examples. The second class of | |
12139 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
12140 | * with regards to tracking sign/unsigned bounds because the bits may | |
12141 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
12142 | * the reg unbounded in the subreg bound space and use the resulting | |
12143 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
12144 | */ | |
48461135 JB |
12145 | switch (opcode) { |
12146 | case BPF_ADD: | |
3f50f132 | 12147 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 12148 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 12149 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
12150 | break; |
12151 | case BPF_SUB: | |
3f50f132 | 12152 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 12153 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 12154 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
12155 | break; |
12156 | case BPF_MUL: | |
3f50f132 JF |
12157 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
12158 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 12159 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
12160 | break; |
12161 | case BPF_AND: | |
3f50f132 JF |
12162 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
12163 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 12164 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
12165 | break; |
12166 | case BPF_OR: | |
3f50f132 JF |
12167 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
12168 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 12169 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 12170 | break; |
2921c90d YS |
12171 | case BPF_XOR: |
12172 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
12173 | scalar32_min_max_xor(dst_reg, &src_reg); | |
12174 | scalar_min_max_xor(dst_reg, &src_reg); | |
12175 | break; | |
48461135 | 12176 | case BPF_LSH: |
468f6eaf JH |
12177 | if (umax_val >= insn_bitness) { |
12178 | /* Shifts greater than 31 or 63 are undefined. | |
12179 | * This includes shifts by a negative number. | |
b03c9f9f | 12180 | */ |
61bd5218 | 12181 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
12182 | break; |
12183 | } | |
3f50f132 JF |
12184 | if (alu32) |
12185 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
12186 | else | |
12187 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
12188 | break; |
12189 | case BPF_RSH: | |
468f6eaf JH |
12190 | if (umax_val >= insn_bitness) { |
12191 | /* Shifts greater than 31 or 63 are undefined. | |
12192 | * This includes shifts by a negative number. | |
b03c9f9f | 12193 | */ |
61bd5218 | 12194 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
12195 | break; |
12196 | } | |
3f50f132 JF |
12197 | if (alu32) |
12198 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
12199 | else | |
12200 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 12201 | break; |
9cbe1f5a YS |
12202 | case BPF_ARSH: |
12203 | if (umax_val >= insn_bitness) { | |
12204 | /* Shifts greater than 31 or 63 are undefined. | |
12205 | * This includes shifts by a negative number. | |
12206 | */ | |
12207 | mark_reg_unknown(env, regs, insn->dst_reg); | |
12208 | break; | |
12209 | } | |
3f50f132 JF |
12210 | if (alu32) |
12211 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
12212 | else | |
12213 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 12214 | break; |
48461135 | 12215 | default: |
61bd5218 | 12216 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
12217 | break; |
12218 | } | |
12219 | ||
3f50f132 JF |
12220 | /* ALU32 ops are zero extended into 64bit register */ |
12221 | if (alu32) | |
12222 | zext_32_to_64(dst_reg); | |
3844d153 | 12223 | reg_bounds_sync(dst_reg); |
f1174f77 EC |
12224 | return 0; |
12225 | } | |
12226 | ||
12227 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
12228 | * and var_off. | |
12229 | */ | |
12230 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
12231 | struct bpf_insn *insn) | |
12232 | { | |
f4d7e40a AS |
12233 | struct bpf_verifier_state *vstate = env->cur_state; |
12234 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
12235 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
12236 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
12237 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 12238 | int err; |
f1174f77 EC |
12239 | |
12240 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
12241 | src_reg = NULL; |
12242 | if (dst_reg->type != SCALAR_VALUE) | |
12243 | ptr_reg = dst_reg; | |
75748837 AS |
12244 | else |
12245 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
12246 | * incorrectly propagated into other registers by find_equal_scalars() | |
12247 | */ | |
12248 | dst_reg->id = 0; | |
f1174f77 EC |
12249 | if (BPF_SRC(insn->code) == BPF_X) { |
12250 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
12251 | if (src_reg->type != SCALAR_VALUE) { |
12252 | if (dst_reg->type != SCALAR_VALUE) { | |
12253 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
12254 | * an arbitrary scalar. Disallow all math except |
12255 | * pointer subtraction | |
f1174f77 | 12256 | */ |
dd066823 | 12257 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
12258 | mark_reg_unknown(env, regs, insn->dst_reg); |
12259 | return 0; | |
f1174f77 | 12260 | } |
82abbf8d AS |
12261 | verbose(env, "R%d pointer %s pointer prohibited\n", |
12262 | insn->dst_reg, | |
12263 | bpf_alu_string[opcode >> 4]); | |
12264 | return -EACCES; | |
f1174f77 EC |
12265 | } else { |
12266 | /* scalar += pointer | |
12267 | * This is legal, but we have to reverse our | |
12268 | * src/dest handling in computing the range | |
12269 | */ | |
b5dc0163 AS |
12270 | err = mark_chain_precision(env, insn->dst_reg); |
12271 | if (err) | |
12272 | return err; | |
82abbf8d AS |
12273 | return adjust_ptr_min_max_vals(env, insn, |
12274 | src_reg, dst_reg); | |
f1174f77 EC |
12275 | } |
12276 | } else if (ptr_reg) { | |
12277 | /* pointer += scalar */ | |
b5dc0163 AS |
12278 | err = mark_chain_precision(env, insn->src_reg); |
12279 | if (err) | |
12280 | return err; | |
82abbf8d AS |
12281 | return adjust_ptr_min_max_vals(env, insn, |
12282 | dst_reg, src_reg); | |
a3b666bf AN |
12283 | } else if (dst_reg->precise) { |
12284 | /* if dst_reg is precise, src_reg should be precise as well */ | |
12285 | err = mark_chain_precision(env, insn->src_reg); | |
12286 | if (err) | |
12287 | return err; | |
f1174f77 EC |
12288 | } |
12289 | } else { | |
12290 | /* Pretend the src is a reg with a known value, since we only | |
12291 | * need to be able to read from this state. | |
12292 | */ | |
12293 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 12294 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 12295 | src_reg = &off_reg; |
82abbf8d AS |
12296 | if (ptr_reg) /* pointer += K */ |
12297 | return adjust_ptr_min_max_vals(env, insn, | |
12298 | ptr_reg, src_reg); | |
f1174f77 EC |
12299 | } |
12300 | ||
12301 | /* Got here implies adding two SCALAR_VALUEs */ | |
12302 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 12303 | print_verifier_state(env, state, true); |
61bd5218 | 12304 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
12305 | return -EINVAL; |
12306 | } | |
12307 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 12308 | print_verifier_state(env, state, true); |
61bd5218 | 12309 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
12310 | return -EINVAL; |
12311 | } | |
12312 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
12313 | } |
12314 | ||
17a52670 | 12315 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 12316 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 12317 | { |
638f5b90 | 12318 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
12319 | u8 opcode = BPF_OP(insn->code); |
12320 | int err; | |
12321 | ||
12322 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
12323 | if (opcode == BPF_NEG) { | |
395e942d | 12324 | if (BPF_SRC(insn->code) != BPF_K || |
17a52670 AS |
12325 | insn->src_reg != BPF_REG_0 || |
12326 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 12327 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
12328 | return -EINVAL; |
12329 | } | |
12330 | } else { | |
12331 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
12332 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
12333 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 12334 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
12335 | return -EINVAL; |
12336 | } | |
12337 | } | |
12338 | ||
12339 | /* check src operand */ | |
dc503a8a | 12340 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12341 | if (err) |
12342 | return err; | |
12343 | ||
1be7f75d | 12344 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 12345 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
12346 | insn->dst_reg); |
12347 | return -EACCES; | |
12348 | } | |
12349 | ||
17a52670 | 12350 | /* check dest operand */ |
dc503a8a | 12351 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
12352 | if (err) |
12353 | return err; | |
12354 | ||
12355 | } else if (opcode == BPF_MOV) { | |
12356 | ||
12357 | if (BPF_SRC(insn->code) == BPF_X) { | |
12358 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 12359 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
12360 | return -EINVAL; |
12361 | } | |
12362 | ||
12363 | /* check src operand */ | |
dc503a8a | 12364 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12365 | if (err) |
12366 | return err; | |
12367 | } else { | |
12368 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 12369 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
12370 | return -EINVAL; |
12371 | } | |
12372 | } | |
12373 | ||
fbeb1603 AF |
12374 | /* check dest operand, mark as required later */ |
12375 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
12376 | if (err) |
12377 | return err; | |
12378 | ||
12379 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
12380 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
12381 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
12382 | ||
17a52670 AS |
12383 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
12384 | /* case: R1 = R2 | |
12385 | * copy register state to dest reg | |
12386 | */ | |
75748837 AS |
12387 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
12388 | /* Assign src and dst registers the same ID | |
12389 | * that will be used by find_equal_scalars() | |
12390 | * to propagate min/max range. | |
12391 | */ | |
12392 | src_reg->id = ++env->id_gen; | |
71f656a5 | 12393 | copy_register_state(dst_reg, src_reg); |
e434b8cd | 12394 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 12395 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 12396 | } else { |
f1174f77 | 12397 | /* R1 = (u32) R2 */ |
1be7f75d | 12398 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
12399 | verbose(env, |
12400 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
12401 | insn->src_reg); |
12402 | return -EACCES; | |
e434b8cd | 12403 | } else if (src_reg->type == SCALAR_VALUE) { |
71f656a5 | 12404 | copy_register_state(dst_reg, src_reg); |
75748837 AS |
12405 | /* Make sure ID is cleared otherwise |
12406 | * dst_reg min/max could be incorrectly | |
12407 | * propagated into src_reg by find_equal_scalars() | |
12408 | */ | |
12409 | dst_reg->id = 0; | |
e434b8cd | 12410 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 12411 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
12412 | } else { |
12413 | mark_reg_unknown(env, regs, | |
12414 | insn->dst_reg); | |
1be7f75d | 12415 | } |
3f50f132 | 12416 | zext_32_to_64(dst_reg); |
3844d153 | 12417 | reg_bounds_sync(dst_reg); |
17a52670 AS |
12418 | } |
12419 | } else { | |
12420 | /* case: R = imm | |
12421 | * remember the value we stored into this reg | |
12422 | */ | |
fbeb1603 AF |
12423 | /* clear any state __mark_reg_known doesn't set */ |
12424 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 12425 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
12426 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
12427 | __mark_reg_known(regs + insn->dst_reg, | |
12428 | insn->imm); | |
12429 | } else { | |
12430 | __mark_reg_known(regs + insn->dst_reg, | |
12431 | (u32)insn->imm); | |
12432 | } | |
17a52670 AS |
12433 | } |
12434 | ||
12435 | } else if (opcode > BPF_END) { | |
61bd5218 | 12436 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
12437 | return -EINVAL; |
12438 | ||
12439 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
12440 | ||
17a52670 AS |
12441 | if (BPF_SRC(insn->code) == BPF_X) { |
12442 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 12443 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
12444 | return -EINVAL; |
12445 | } | |
12446 | /* check src1 operand */ | |
dc503a8a | 12447 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12448 | if (err) |
12449 | return err; | |
12450 | } else { | |
12451 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 12452 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
12453 | return -EINVAL; |
12454 | } | |
12455 | } | |
12456 | ||
12457 | /* check src2 operand */ | |
dc503a8a | 12458 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12459 | if (err) |
12460 | return err; | |
12461 | ||
12462 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
12463 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 12464 | verbose(env, "div by zero\n"); |
17a52670 AS |
12465 | return -EINVAL; |
12466 | } | |
12467 | ||
229394e8 RV |
12468 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
12469 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
12470 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
12471 | ||
12472 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 12473 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
12474 | return -EINVAL; |
12475 | } | |
12476 | } | |
12477 | ||
1a0dc1ac | 12478 | /* check dest operand */ |
dc503a8a | 12479 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
12480 | if (err) |
12481 | return err; | |
12482 | ||
f1174f77 | 12483 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
12484 | } |
12485 | ||
12486 | return 0; | |
12487 | } | |
12488 | ||
f4d7e40a | 12489 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 12490 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 12491 | enum bpf_reg_type type, |
fb2a311a | 12492 | bool range_right_open) |
969bf05e | 12493 | { |
b239da34 KKD |
12494 | struct bpf_func_state *state; |
12495 | struct bpf_reg_state *reg; | |
12496 | int new_range; | |
2d2be8ca | 12497 | |
fb2a311a DB |
12498 | if (dst_reg->off < 0 || |
12499 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
12500 | /* This doesn't give us any range */ |
12501 | return; | |
12502 | ||
b03c9f9f EC |
12503 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
12504 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
12505 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
12506 | * than pkt_end, but that's because it's also less than pkt. | |
12507 | */ | |
12508 | return; | |
12509 | ||
fb2a311a DB |
12510 | new_range = dst_reg->off; |
12511 | if (range_right_open) | |
2fa7d94a | 12512 | new_range++; |
fb2a311a DB |
12513 | |
12514 | /* Examples for register markings: | |
2d2be8ca | 12515 | * |
fb2a311a | 12516 | * pkt_data in dst register: |
2d2be8ca DB |
12517 | * |
12518 | * r2 = r3; | |
12519 | * r2 += 8; | |
12520 | * if (r2 > pkt_end) goto <handle exception> | |
12521 | * <access okay> | |
12522 | * | |
b4e432f1 DB |
12523 | * r2 = r3; |
12524 | * r2 += 8; | |
12525 | * if (r2 < pkt_end) goto <access okay> | |
12526 | * <handle exception> | |
12527 | * | |
2d2be8ca DB |
12528 | * Where: |
12529 | * r2 == dst_reg, pkt_end == src_reg | |
12530 | * r2=pkt(id=n,off=8,r=0) | |
12531 | * r3=pkt(id=n,off=0,r=0) | |
12532 | * | |
fb2a311a | 12533 | * pkt_data in src register: |
2d2be8ca DB |
12534 | * |
12535 | * r2 = r3; | |
12536 | * r2 += 8; | |
12537 | * if (pkt_end >= r2) goto <access okay> | |
12538 | * <handle exception> | |
12539 | * | |
b4e432f1 DB |
12540 | * r2 = r3; |
12541 | * r2 += 8; | |
12542 | * if (pkt_end <= r2) goto <handle exception> | |
12543 | * <access okay> | |
12544 | * | |
2d2be8ca DB |
12545 | * Where: |
12546 | * pkt_end == dst_reg, r2 == src_reg | |
12547 | * r2=pkt(id=n,off=8,r=0) | |
12548 | * r3=pkt(id=n,off=0,r=0) | |
12549 | * | |
12550 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
12551 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
12552 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
12553 | * the check. | |
969bf05e | 12554 | */ |
2d2be8ca | 12555 | |
f1174f77 EC |
12556 | /* If our ids match, then we must have the same max_value. And we |
12557 | * don't care about the other reg's fixed offset, since if it's too big | |
12558 | * the range won't allow anything. | |
12559 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
12560 | */ | |
b239da34 KKD |
12561 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
12562 | if (reg->type == type && reg->id == dst_reg->id) | |
12563 | /* keep the maximum range already checked */ | |
12564 | reg->range = max(reg->range, new_range); | |
12565 | })); | |
969bf05e AS |
12566 | } |
12567 | ||
3f50f132 | 12568 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 12569 | { |
3f50f132 JF |
12570 | struct tnum subreg = tnum_subreg(reg->var_off); |
12571 | s32 sval = (s32)val; | |
a72dafaf | 12572 | |
3f50f132 JF |
12573 | switch (opcode) { |
12574 | case BPF_JEQ: | |
12575 | if (tnum_is_const(subreg)) | |
12576 | return !!tnum_equals_const(subreg, val); | |
12577 | break; | |
12578 | case BPF_JNE: | |
12579 | if (tnum_is_const(subreg)) | |
12580 | return !tnum_equals_const(subreg, val); | |
12581 | break; | |
12582 | case BPF_JSET: | |
12583 | if ((~subreg.mask & subreg.value) & val) | |
12584 | return 1; | |
12585 | if (!((subreg.mask | subreg.value) & val)) | |
12586 | return 0; | |
12587 | break; | |
12588 | case BPF_JGT: | |
12589 | if (reg->u32_min_value > val) | |
12590 | return 1; | |
12591 | else if (reg->u32_max_value <= val) | |
12592 | return 0; | |
12593 | break; | |
12594 | case BPF_JSGT: | |
12595 | if (reg->s32_min_value > sval) | |
12596 | return 1; | |
ee114dd6 | 12597 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
12598 | return 0; |
12599 | break; | |
12600 | case BPF_JLT: | |
12601 | if (reg->u32_max_value < val) | |
12602 | return 1; | |
12603 | else if (reg->u32_min_value >= val) | |
12604 | return 0; | |
12605 | break; | |
12606 | case BPF_JSLT: | |
12607 | if (reg->s32_max_value < sval) | |
12608 | return 1; | |
12609 | else if (reg->s32_min_value >= sval) | |
12610 | return 0; | |
12611 | break; | |
12612 | case BPF_JGE: | |
12613 | if (reg->u32_min_value >= val) | |
12614 | return 1; | |
12615 | else if (reg->u32_max_value < val) | |
12616 | return 0; | |
12617 | break; | |
12618 | case BPF_JSGE: | |
12619 | if (reg->s32_min_value >= sval) | |
12620 | return 1; | |
12621 | else if (reg->s32_max_value < sval) | |
12622 | return 0; | |
12623 | break; | |
12624 | case BPF_JLE: | |
12625 | if (reg->u32_max_value <= val) | |
12626 | return 1; | |
12627 | else if (reg->u32_min_value > val) | |
12628 | return 0; | |
12629 | break; | |
12630 | case BPF_JSLE: | |
12631 | if (reg->s32_max_value <= sval) | |
12632 | return 1; | |
12633 | else if (reg->s32_min_value > sval) | |
12634 | return 0; | |
12635 | break; | |
12636 | } | |
4f7b3e82 | 12637 | |
3f50f132 JF |
12638 | return -1; |
12639 | } | |
092ed096 | 12640 | |
3f50f132 JF |
12641 | |
12642 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
12643 | { | |
12644 | s64 sval = (s64)val; | |
a72dafaf | 12645 | |
4f7b3e82 AS |
12646 | switch (opcode) { |
12647 | case BPF_JEQ: | |
12648 | if (tnum_is_const(reg->var_off)) | |
12649 | return !!tnum_equals_const(reg->var_off, val); | |
12650 | break; | |
12651 | case BPF_JNE: | |
12652 | if (tnum_is_const(reg->var_off)) | |
12653 | return !tnum_equals_const(reg->var_off, val); | |
12654 | break; | |
960ea056 JK |
12655 | case BPF_JSET: |
12656 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
12657 | return 1; | |
12658 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
12659 | return 0; | |
12660 | break; | |
4f7b3e82 AS |
12661 | case BPF_JGT: |
12662 | if (reg->umin_value > val) | |
12663 | return 1; | |
12664 | else if (reg->umax_value <= val) | |
12665 | return 0; | |
12666 | break; | |
12667 | case BPF_JSGT: | |
a72dafaf | 12668 | if (reg->smin_value > sval) |
4f7b3e82 | 12669 | return 1; |
ee114dd6 | 12670 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
12671 | return 0; |
12672 | break; | |
12673 | case BPF_JLT: | |
12674 | if (reg->umax_value < val) | |
12675 | return 1; | |
12676 | else if (reg->umin_value >= val) | |
12677 | return 0; | |
12678 | break; | |
12679 | case BPF_JSLT: | |
a72dafaf | 12680 | if (reg->smax_value < sval) |
4f7b3e82 | 12681 | return 1; |
a72dafaf | 12682 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
12683 | return 0; |
12684 | break; | |
12685 | case BPF_JGE: | |
12686 | if (reg->umin_value >= val) | |
12687 | return 1; | |
12688 | else if (reg->umax_value < val) | |
12689 | return 0; | |
12690 | break; | |
12691 | case BPF_JSGE: | |
a72dafaf | 12692 | if (reg->smin_value >= sval) |
4f7b3e82 | 12693 | return 1; |
a72dafaf | 12694 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
12695 | return 0; |
12696 | break; | |
12697 | case BPF_JLE: | |
12698 | if (reg->umax_value <= val) | |
12699 | return 1; | |
12700 | else if (reg->umin_value > val) | |
12701 | return 0; | |
12702 | break; | |
12703 | case BPF_JSLE: | |
a72dafaf | 12704 | if (reg->smax_value <= sval) |
4f7b3e82 | 12705 | return 1; |
a72dafaf | 12706 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
12707 | return 0; |
12708 | break; | |
12709 | } | |
12710 | ||
12711 | return -1; | |
12712 | } | |
12713 | ||
3f50f132 JF |
12714 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
12715 | * and return: | |
12716 | * 1 - branch will be taken and "goto target" will be executed | |
12717 | * 0 - branch will not be taken and fall-through to next insn | |
12718 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
12719 | * range [0,10] | |
604dca5e | 12720 | */ |
3f50f132 JF |
12721 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
12722 | bool is_jmp32) | |
604dca5e | 12723 | { |
cac616db JF |
12724 | if (__is_pointer_value(false, reg)) { |
12725 | if (!reg_type_not_null(reg->type)) | |
12726 | return -1; | |
12727 | ||
12728 | /* If pointer is valid tests against zero will fail so we can | |
12729 | * use this to direct branch taken. | |
12730 | */ | |
12731 | if (val != 0) | |
12732 | return -1; | |
12733 | ||
12734 | switch (opcode) { | |
12735 | case BPF_JEQ: | |
12736 | return 0; | |
12737 | case BPF_JNE: | |
12738 | return 1; | |
12739 | default: | |
12740 | return -1; | |
12741 | } | |
12742 | } | |
604dca5e | 12743 | |
3f50f132 JF |
12744 | if (is_jmp32) |
12745 | return is_branch32_taken(reg, val, opcode); | |
12746 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
12747 | } |
12748 | ||
6d94e741 AS |
12749 | static int flip_opcode(u32 opcode) |
12750 | { | |
12751 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
12752 | static const u8 opcode_flip[16] = { | |
12753 | /* these stay the same */ | |
12754 | [BPF_JEQ >> 4] = BPF_JEQ, | |
12755 | [BPF_JNE >> 4] = BPF_JNE, | |
12756 | [BPF_JSET >> 4] = BPF_JSET, | |
12757 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
12758 | [BPF_JGE >> 4] = BPF_JLE, | |
12759 | [BPF_JGT >> 4] = BPF_JLT, | |
12760 | [BPF_JLE >> 4] = BPF_JGE, | |
12761 | [BPF_JLT >> 4] = BPF_JGT, | |
12762 | [BPF_JSGE >> 4] = BPF_JSLE, | |
12763 | [BPF_JSGT >> 4] = BPF_JSLT, | |
12764 | [BPF_JSLE >> 4] = BPF_JSGE, | |
12765 | [BPF_JSLT >> 4] = BPF_JSGT | |
12766 | }; | |
12767 | return opcode_flip[opcode >> 4]; | |
12768 | } | |
12769 | ||
12770 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
12771 | struct bpf_reg_state *src_reg, | |
12772 | u8 opcode) | |
12773 | { | |
12774 | struct bpf_reg_state *pkt; | |
12775 | ||
12776 | if (src_reg->type == PTR_TO_PACKET_END) { | |
12777 | pkt = dst_reg; | |
12778 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
12779 | pkt = src_reg; | |
12780 | opcode = flip_opcode(opcode); | |
12781 | } else { | |
12782 | return -1; | |
12783 | } | |
12784 | ||
12785 | if (pkt->range >= 0) | |
12786 | return -1; | |
12787 | ||
12788 | switch (opcode) { | |
12789 | case BPF_JLE: | |
12790 | /* pkt <= pkt_end */ | |
12791 | fallthrough; | |
12792 | case BPF_JGT: | |
12793 | /* pkt > pkt_end */ | |
12794 | if (pkt->range == BEYOND_PKT_END) | |
12795 | /* pkt has at last one extra byte beyond pkt_end */ | |
12796 | return opcode == BPF_JGT; | |
12797 | break; | |
12798 | case BPF_JLT: | |
12799 | /* pkt < pkt_end */ | |
12800 | fallthrough; | |
12801 | case BPF_JGE: | |
12802 | /* pkt >= pkt_end */ | |
12803 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
12804 | return opcode == BPF_JGE; | |
12805 | break; | |
12806 | } | |
12807 | return -1; | |
12808 | } | |
12809 | ||
48461135 JB |
12810 | /* Adjusts the register min/max values in the case that the dst_reg is the |
12811 | * variable register that we are working on, and src_reg is a constant or we're | |
12812 | * simply doing a BPF_K check. | |
f1174f77 | 12813 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
12814 | */ |
12815 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
12816 | struct bpf_reg_state *false_reg, |
12817 | u64 val, u32 val32, | |
092ed096 | 12818 | u8 opcode, bool is_jmp32) |
48461135 | 12819 | { |
3f50f132 JF |
12820 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
12821 | struct tnum false_64off = false_reg->var_off; | |
12822 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
12823 | struct tnum true_64off = true_reg->var_off; | |
12824 | s64 sval = (s64)val; | |
12825 | s32 sval32 = (s32)val32; | |
a72dafaf | 12826 | |
f1174f77 EC |
12827 | /* If the dst_reg is a pointer, we can't learn anything about its |
12828 | * variable offset from the compare (unless src_reg were a pointer into | |
12829 | * the same object, but we don't bother with that. | |
12830 | * Since false_reg and true_reg have the same type by construction, we | |
12831 | * only need to check one of them for pointerness. | |
12832 | */ | |
12833 | if (__is_pointer_value(false, false_reg)) | |
12834 | return; | |
4cabc5b1 | 12835 | |
48461135 | 12836 | switch (opcode) { |
a12ca627 DB |
12837 | /* JEQ/JNE comparison doesn't change the register equivalence. |
12838 | * | |
12839 | * r1 = r2; | |
12840 | * if (r1 == 42) goto label; | |
12841 | * ... | |
12842 | * label: // here both r1 and r2 are known to be 42. | |
12843 | * | |
12844 | * Hence when marking register as known preserve it's ID. | |
12845 | */ | |
48461135 | 12846 | case BPF_JEQ: |
a12ca627 DB |
12847 | if (is_jmp32) { |
12848 | __mark_reg32_known(true_reg, val32); | |
12849 | true_32off = tnum_subreg(true_reg->var_off); | |
12850 | } else { | |
12851 | ___mark_reg_known(true_reg, val); | |
12852 | true_64off = true_reg->var_off; | |
12853 | } | |
12854 | break; | |
48461135 | 12855 | case BPF_JNE: |
a12ca627 DB |
12856 | if (is_jmp32) { |
12857 | __mark_reg32_known(false_reg, val32); | |
12858 | false_32off = tnum_subreg(false_reg->var_off); | |
12859 | } else { | |
12860 | ___mark_reg_known(false_reg, val); | |
12861 | false_64off = false_reg->var_off; | |
12862 | } | |
48461135 | 12863 | break; |
960ea056 | 12864 | case BPF_JSET: |
3f50f132 JF |
12865 | if (is_jmp32) { |
12866 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
12867 | if (is_power_of_2(val32)) | |
12868 | true_32off = tnum_or(true_32off, | |
12869 | tnum_const(val32)); | |
12870 | } else { | |
12871 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
12872 | if (is_power_of_2(val)) | |
12873 | true_64off = tnum_or(true_64off, | |
12874 | tnum_const(val)); | |
12875 | } | |
960ea056 | 12876 | break; |
48461135 | 12877 | case BPF_JGE: |
a72dafaf JW |
12878 | case BPF_JGT: |
12879 | { | |
3f50f132 JF |
12880 | if (is_jmp32) { |
12881 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
12882 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
12883 | ||
12884 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
12885 | false_umax); | |
12886 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
12887 | true_umin); | |
12888 | } else { | |
12889 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
12890 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
12891 | ||
12892 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
12893 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
12894 | } | |
b03c9f9f | 12895 | break; |
a72dafaf | 12896 | } |
48461135 | 12897 | case BPF_JSGE: |
a72dafaf JW |
12898 | case BPF_JSGT: |
12899 | { | |
3f50f132 JF |
12900 | if (is_jmp32) { |
12901 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
12902 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 12903 | |
3f50f132 JF |
12904 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
12905 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
12906 | } else { | |
12907 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
12908 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
12909 | ||
12910 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
12911 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
12912 | } | |
48461135 | 12913 | break; |
a72dafaf | 12914 | } |
b4e432f1 | 12915 | case BPF_JLE: |
a72dafaf JW |
12916 | case BPF_JLT: |
12917 | { | |
3f50f132 JF |
12918 | if (is_jmp32) { |
12919 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
12920 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
12921 | ||
12922 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
12923 | false_umin); | |
12924 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
12925 | true_umax); | |
12926 | } else { | |
12927 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
12928 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
12929 | ||
12930 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
12931 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
12932 | } | |
b4e432f1 | 12933 | break; |
a72dafaf | 12934 | } |
b4e432f1 | 12935 | case BPF_JSLE: |
a72dafaf JW |
12936 | case BPF_JSLT: |
12937 | { | |
3f50f132 JF |
12938 | if (is_jmp32) { |
12939 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
12940 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 12941 | |
3f50f132 JF |
12942 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
12943 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
12944 | } else { | |
12945 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
12946 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
12947 | ||
12948 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
12949 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
12950 | } | |
b4e432f1 | 12951 | break; |
a72dafaf | 12952 | } |
48461135 | 12953 | default: |
0fc31b10 | 12954 | return; |
48461135 JB |
12955 | } |
12956 | ||
3f50f132 JF |
12957 | if (is_jmp32) { |
12958 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
12959 | tnum_subreg(false_32off)); | |
12960 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
12961 | tnum_subreg(true_32off)); | |
12962 | __reg_combine_32_into_64(false_reg); | |
12963 | __reg_combine_32_into_64(true_reg); | |
12964 | } else { | |
12965 | false_reg->var_off = false_64off; | |
12966 | true_reg->var_off = true_64off; | |
12967 | __reg_combine_64_into_32(false_reg); | |
12968 | __reg_combine_64_into_32(true_reg); | |
12969 | } | |
48461135 JB |
12970 | } |
12971 | ||
f1174f77 EC |
12972 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
12973 | * the variable reg. | |
48461135 JB |
12974 | */ |
12975 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
12976 | struct bpf_reg_state *false_reg, |
12977 | u64 val, u32 val32, | |
092ed096 | 12978 | u8 opcode, bool is_jmp32) |
48461135 | 12979 | { |
6d94e741 | 12980 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
12981 | /* This uses zero as "not present in table"; luckily the zero opcode, |
12982 | * BPF_JA, can't get here. | |
b03c9f9f | 12983 | */ |
0fc31b10 | 12984 | if (opcode) |
3f50f132 | 12985 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
12986 | } |
12987 | ||
12988 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
12989 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
12990 | struct bpf_reg_state *dst_reg) | |
12991 | { | |
b03c9f9f EC |
12992 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
12993 | dst_reg->umin_value); | |
12994 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
12995 | dst_reg->umax_value); | |
12996 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
12997 | dst_reg->smin_value); | |
12998 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
12999 | dst_reg->smax_value); | |
f1174f77 EC |
13000 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
13001 | dst_reg->var_off); | |
3844d153 DB |
13002 | reg_bounds_sync(src_reg); |
13003 | reg_bounds_sync(dst_reg); | |
f1174f77 EC |
13004 | } |
13005 | ||
13006 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
13007 | struct bpf_reg_state *true_dst, | |
13008 | struct bpf_reg_state *false_src, | |
13009 | struct bpf_reg_state *false_dst, | |
13010 | u8 opcode) | |
13011 | { | |
13012 | switch (opcode) { | |
13013 | case BPF_JEQ: | |
13014 | __reg_combine_min_max(true_src, true_dst); | |
13015 | break; | |
13016 | case BPF_JNE: | |
13017 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 13018 | break; |
4cabc5b1 | 13019 | } |
48461135 JB |
13020 | } |
13021 | ||
fd978bf7 JS |
13022 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
13023 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 13024 | bool is_null) |
57a09bf0 | 13025 | { |
c25b2ae1 | 13026 | if (type_may_be_null(reg->type) && reg->id == id && |
fca1aa75 | 13027 | (is_rcu_reg(reg) || !WARN_ON_ONCE(!reg->id))) { |
df57f38a KKD |
13028 | /* Old offset (both fixed and variable parts) should have been |
13029 | * known-zero, because we don't allow pointer arithmetic on | |
13030 | * pointers that might be NULL. If we see this happening, don't | |
13031 | * convert the register. | |
13032 | * | |
13033 | * But in some cases, some helpers that return local kptrs | |
13034 | * advance offset for the returned pointer. In those cases, it | |
13035 | * is fine to expect to see reg->off. | |
13036 | */ | |
13037 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || !tnum_equals_const(reg->var_off, 0))) | |
13038 | return; | |
6a3cd331 DM |
13039 | if (!(type_is_ptr_alloc_obj(reg->type) || type_is_non_owning_ref(reg->type)) && |
13040 | WARN_ON_ONCE(reg->off)) | |
e60b0d12 | 13041 | return; |
6a3cd331 | 13042 | |
f1174f77 EC |
13043 | if (is_null) { |
13044 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
13045 | /* We don't need id and ref_obj_id from this point |
13046 | * onwards anymore, thus we should better reset it, | |
13047 | * so that state pruning has chances to take effect. | |
13048 | */ | |
13049 | reg->id = 0; | |
13050 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
13051 | |
13052 | return; | |
13053 | } | |
13054 | ||
13055 | mark_ptr_not_null_reg(reg); | |
13056 | ||
13057 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 | 13058 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
b239da34 | 13059 | * in release_reference(). |
1b986589 MKL |
13060 | * |
13061 | * reg->id is still used by spin_lock ptr. Other | |
13062 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
13063 | */ |
13064 | reg->id = 0; | |
56f668df | 13065 | } |
57a09bf0 TG |
13066 | } |
13067 | } | |
13068 | ||
13069 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
13070 | * be folded together at some point. | |
13071 | */ | |
840b9615 JS |
13072 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
13073 | bool is_null) | |
57a09bf0 | 13074 | { |
f4d7e40a | 13075 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
b239da34 | 13076 | struct bpf_reg_state *regs = state->regs, *reg; |
1b986589 | 13077 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 13078 | u32 id = regs[regno].id; |
57a09bf0 | 13079 | |
1b986589 MKL |
13080 | if (ref_obj_id && ref_obj_id == id && is_null) |
13081 | /* regs[regno] is in the " == NULL" branch. | |
13082 | * No one could have freed the reference state before | |
13083 | * doing the NULL check. | |
13084 | */ | |
13085 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 13086 | |
b239da34 KKD |
13087 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
13088 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
13089 | })); | |
57a09bf0 TG |
13090 | } |
13091 | ||
5beca081 DB |
13092 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
13093 | struct bpf_reg_state *dst_reg, | |
13094 | struct bpf_reg_state *src_reg, | |
13095 | struct bpf_verifier_state *this_branch, | |
13096 | struct bpf_verifier_state *other_branch) | |
13097 | { | |
13098 | if (BPF_SRC(insn->code) != BPF_X) | |
13099 | return false; | |
13100 | ||
092ed096 JW |
13101 | /* Pointers are always 64-bit. */ |
13102 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
13103 | return false; | |
13104 | ||
5beca081 DB |
13105 | switch (BPF_OP(insn->code)) { |
13106 | case BPF_JGT: | |
13107 | if ((dst_reg->type == PTR_TO_PACKET && | |
13108 | src_reg->type == PTR_TO_PACKET_END) || | |
13109 | (dst_reg->type == PTR_TO_PACKET_META && | |
13110 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13111 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
13112 | find_good_pkt_pointers(this_branch, dst_reg, | |
13113 | dst_reg->type, false); | |
6d94e741 | 13114 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
13115 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13116 | src_reg->type == PTR_TO_PACKET) || | |
13117 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13118 | src_reg->type == PTR_TO_PACKET_META)) { | |
13119 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
13120 | find_good_pkt_pointers(other_branch, src_reg, | |
13121 | src_reg->type, true); | |
6d94e741 | 13122 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
13123 | } else { |
13124 | return false; | |
13125 | } | |
13126 | break; | |
13127 | case BPF_JLT: | |
13128 | if ((dst_reg->type == PTR_TO_PACKET && | |
13129 | src_reg->type == PTR_TO_PACKET_END) || | |
13130 | (dst_reg->type == PTR_TO_PACKET_META && | |
13131 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13132 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
13133 | find_good_pkt_pointers(other_branch, dst_reg, | |
13134 | dst_reg->type, true); | |
6d94e741 | 13135 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
13136 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13137 | src_reg->type == PTR_TO_PACKET) || | |
13138 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13139 | src_reg->type == PTR_TO_PACKET_META)) { | |
13140 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
13141 | find_good_pkt_pointers(this_branch, src_reg, | |
13142 | src_reg->type, false); | |
6d94e741 | 13143 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
13144 | } else { |
13145 | return false; | |
13146 | } | |
13147 | break; | |
13148 | case BPF_JGE: | |
13149 | if ((dst_reg->type == PTR_TO_PACKET && | |
13150 | src_reg->type == PTR_TO_PACKET_END) || | |
13151 | (dst_reg->type == PTR_TO_PACKET_META && | |
13152 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13153 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
13154 | find_good_pkt_pointers(this_branch, dst_reg, | |
13155 | dst_reg->type, true); | |
6d94e741 | 13156 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
13157 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13158 | src_reg->type == PTR_TO_PACKET) || | |
13159 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13160 | src_reg->type == PTR_TO_PACKET_META)) { | |
13161 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
13162 | find_good_pkt_pointers(other_branch, src_reg, | |
13163 | src_reg->type, false); | |
6d94e741 | 13164 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
13165 | } else { |
13166 | return false; | |
13167 | } | |
13168 | break; | |
13169 | case BPF_JLE: | |
13170 | if ((dst_reg->type == PTR_TO_PACKET && | |
13171 | src_reg->type == PTR_TO_PACKET_END) || | |
13172 | (dst_reg->type == PTR_TO_PACKET_META && | |
13173 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13174 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
13175 | find_good_pkt_pointers(other_branch, dst_reg, | |
13176 | dst_reg->type, false); | |
6d94e741 | 13177 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
13178 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13179 | src_reg->type == PTR_TO_PACKET) || | |
13180 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13181 | src_reg->type == PTR_TO_PACKET_META)) { | |
13182 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
13183 | find_good_pkt_pointers(this_branch, src_reg, | |
13184 | src_reg->type, true); | |
6d94e741 | 13185 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
13186 | } else { |
13187 | return false; | |
13188 | } | |
13189 | break; | |
13190 | default: | |
13191 | return false; | |
13192 | } | |
13193 | ||
13194 | return true; | |
13195 | } | |
13196 | ||
75748837 AS |
13197 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
13198 | struct bpf_reg_state *known_reg) | |
13199 | { | |
13200 | struct bpf_func_state *state; | |
13201 | struct bpf_reg_state *reg; | |
75748837 | 13202 | |
b239da34 KKD |
13203 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
13204 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
71f656a5 | 13205 | copy_register_state(reg, known_reg); |
b239da34 | 13206 | })); |
75748837 AS |
13207 | } |
13208 | ||
58e2af8b | 13209 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
13210 | struct bpf_insn *insn, int *insn_idx) |
13211 | { | |
f4d7e40a AS |
13212 | struct bpf_verifier_state *this_branch = env->cur_state; |
13213 | struct bpf_verifier_state *other_branch; | |
13214 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 13215 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
befae758 | 13216 | struct bpf_reg_state *eq_branch_regs; |
17a52670 | 13217 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 13218 | bool is_jmp32; |
fb8d251e | 13219 | int pred = -1; |
17a52670 AS |
13220 | int err; |
13221 | ||
092ed096 JW |
13222 | /* Only conditional jumps are expected to reach here. */ |
13223 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
13224 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
13225 | return -EINVAL; |
13226 | } | |
13227 | ||
13228 | if (BPF_SRC(insn->code) == BPF_X) { | |
13229 | if (insn->imm != 0) { | |
092ed096 | 13230 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
13231 | return -EINVAL; |
13232 | } | |
13233 | ||
13234 | /* check src1 operand */ | |
dc503a8a | 13235 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
13236 | if (err) |
13237 | return err; | |
1be7f75d AS |
13238 | |
13239 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 13240 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
13241 | insn->src_reg); |
13242 | return -EACCES; | |
13243 | } | |
fb8d251e | 13244 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
13245 | } else { |
13246 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 13247 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
13248 | return -EINVAL; |
13249 | } | |
13250 | } | |
13251 | ||
13252 | /* check src2 operand */ | |
dc503a8a | 13253 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
13254 | if (err) |
13255 | return err; | |
13256 | ||
1a0dc1ac | 13257 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 13258 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 13259 | |
3f50f132 JF |
13260 | if (BPF_SRC(insn->code) == BPF_K) { |
13261 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
13262 | } else if (src_reg->type == SCALAR_VALUE && | |
13263 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
13264 | pred = is_branch_taken(dst_reg, | |
13265 | tnum_subreg(src_reg->var_off).value, | |
13266 | opcode, | |
13267 | is_jmp32); | |
13268 | } else if (src_reg->type == SCALAR_VALUE && | |
13269 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
13270 | pred = is_branch_taken(dst_reg, | |
13271 | src_reg->var_off.value, | |
13272 | opcode, | |
13273 | is_jmp32); | |
6d94e741 AS |
13274 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
13275 | reg_is_pkt_pointer_any(src_reg) && | |
13276 | !is_jmp32) { | |
13277 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
13278 | } |
13279 | ||
b5dc0163 | 13280 | if (pred >= 0) { |
cac616db JF |
13281 | /* If we get here with a dst_reg pointer type it is because |
13282 | * above is_branch_taken() special cased the 0 comparison. | |
13283 | */ | |
13284 | if (!__is_pointer_value(false, dst_reg)) | |
13285 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
13286 | if (BPF_SRC(insn->code) == BPF_X && !err && |
13287 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
13288 | err = mark_chain_precision(env, insn->src_reg); |
13289 | if (err) | |
13290 | return err; | |
13291 | } | |
9183671a | 13292 | |
fb8d251e | 13293 | if (pred == 1) { |
9183671a DB |
13294 | /* Only follow the goto, ignore fall-through. If needed, push |
13295 | * the fall-through branch for simulation under speculative | |
13296 | * execution. | |
13297 | */ | |
13298 | if (!env->bypass_spec_v1 && | |
13299 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
13300 | *insn_idx)) | |
13301 | return -EFAULT; | |
fb8d251e AS |
13302 | *insn_idx += insn->off; |
13303 | return 0; | |
13304 | } else if (pred == 0) { | |
9183671a DB |
13305 | /* Only follow the fall-through branch, since that's where the |
13306 | * program will go. If needed, push the goto branch for | |
13307 | * simulation under speculative execution. | |
fb8d251e | 13308 | */ |
9183671a DB |
13309 | if (!env->bypass_spec_v1 && |
13310 | !sanitize_speculative_path(env, insn, | |
13311 | *insn_idx + insn->off + 1, | |
13312 | *insn_idx)) | |
13313 | return -EFAULT; | |
fb8d251e | 13314 | return 0; |
17a52670 AS |
13315 | } |
13316 | ||
979d63d5 DB |
13317 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
13318 | false); | |
17a52670 AS |
13319 | if (!other_branch) |
13320 | return -EFAULT; | |
f4d7e40a | 13321 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 13322 | |
48461135 JB |
13323 | /* detect if we are comparing against a constant value so we can adjust |
13324 | * our min/max values for our dst register. | |
f1174f77 | 13325 | * this is only legit if both are scalars (or pointers to the same |
befae758 EZ |
13326 | * object, I suppose, see the PTR_MAYBE_NULL related if block below), |
13327 | * because otherwise the different base pointers mean the offsets aren't | |
f1174f77 | 13328 | * comparable. |
48461135 JB |
13329 | */ |
13330 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 13331 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 13332 | |
f1174f77 | 13333 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
13334 | src_reg->type == SCALAR_VALUE) { |
13335 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
13336 | (is_jmp32 && |
13337 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 13338 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 13339 | dst_reg, |
3f50f132 JF |
13340 | src_reg->var_off.value, |
13341 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
13342 | opcode, is_jmp32); |
13343 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
13344 | (is_jmp32 && |
13345 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 13346 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 13347 | src_reg, |
3f50f132 JF |
13348 | dst_reg->var_off.value, |
13349 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
13350 | opcode, is_jmp32); |
13351 | else if (!is_jmp32 && | |
13352 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 13353 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
13354 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
13355 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 13356 | src_reg, dst_reg, opcode); |
e688c3db AS |
13357 | if (src_reg->id && |
13358 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
13359 | find_equal_scalars(this_branch, src_reg); |
13360 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
13361 | } | |
13362 | ||
f1174f77 EC |
13363 | } |
13364 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 13365 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
13366 | dst_reg, insn->imm, (u32)insn->imm, |
13367 | opcode, is_jmp32); | |
48461135 JB |
13368 | } |
13369 | ||
e688c3db AS |
13370 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
13371 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
13372 | find_equal_scalars(this_branch, dst_reg); |
13373 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
13374 | } | |
13375 | ||
befae758 EZ |
13376 | /* if one pointer register is compared to another pointer |
13377 | * register check if PTR_MAYBE_NULL could be lifted. | |
13378 | * E.g. register A - maybe null | |
13379 | * register B - not null | |
13380 | * for JNE A, B, ... - A is not null in the false branch; | |
13381 | * for JEQ A, B, ... - A is not null in the true branch. | |
8374bfd5 HS |
13382 | * |
13383 | * Since PTR_TO_BTF_ID points to a kernel struct that does | |
13384 | * not need to be null checked by the BPF program, i.e., | |
13385 | * could be null even without PTR_MAYBE_NULL marking, so | |
13386 | * only propagate nullness when neither reg is that type. | |
befae758 EZ |
13387 | */ |
13388 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_X && | |
13389 | __is_pointer_value(false, src_reg) && __is_pointer_value(false, dst_reg) && | |
8374bfd5 HS |
13390 | type_may_be_null(src_reg->type) != type_may_be_null(dst_reg->type) && |
13391 | base_type(src_reg->type) != PTR_TO_BTF_ID && | |
13392 | base_type(dst_reg->type) != PTR_TO_BTF_ID) { | |
befae758 EZ |
13393 | eq_branch_regs = NULL; |
13394 | switch (opcode) { | |
13395 | case BPF_JEQ: | |
13396 | eq_branch_regs = other_branch_regs; | |
13397 | break; | |
13398 | case BPF_JNE: | |
13399 | eq_branch_regs = regs; | |
13400 | break; | |
13401 | default: | |
13402 | /* do nothing */ | |
13403 | break; | |
13404 | } | |
13405 | if (eq_branch_regs) { | |
13406 | if (type_may_be_null(src_reg->type)) | |
13407 | mark_ptr_not_null_reg(&eq_branch_regs[insn->src_reg]); | |
13408 | else | |
13409 | mark_ptr_not_null_reg(&eq_branch_regs[insn->dst_reg]); | |
13410 | } | |
13411 | } | |
13412 | ||
092ed096 JW |
13413 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
13414 | * NOTE: these optimizations below are related with pointer comparison | |
13415 | * which will never be JMP32. | |
13416 | */ | |
13417 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 13418 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
c25b2ae1 | 13419 | type_may_be_null(dst_reg->type)) { |
840b9615 | 13420 | /* Mark all identical registers in each branch as either |
57a09bf0 TG |
13421 | * safe or unknown depending R == 0 or R != 0 conditional. |
13422 | */ | |
840b9615 JS |
13423 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
13424 | opcode == BPF_JNE); | |
13425 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
13426 | opcode == BPF_JEQ); | |
5beca081 DB |
13427 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
13428 | this_branch, other_branch) && | |
13429 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
13430 | verbose(env, "R%d pointer comparison prohibited\n", |
13431 | insn->dst_reg); | |
1be7f75d | 13432 | return -EACCES; |
17a52670 | 13433 | } |
06ee7115 | 13434 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 13435 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
13436 | return 0; |
13437 | } | |
13438 | ||
17a52670 | 13439 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 13440 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 13441 | { |
d8eca5bb | 13442 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 13443 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 13444 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 13445 | struct bpf_map *map; |
17a52670 AS |
13446 | int err; |
13447 | ||
13448 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 13449 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
13450 | return -EINVAL; |
13451 | } | |
13452 | if (insn->off != 0) { | |
61bd5218 | 13453 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
13454 | return -EINVAL; |
13455 | } | |
13456 | ||
dc503a8a | 13457 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
13458 | if (err) |
13459 | return err; | |
13460 | ||
4976b718 | 13461 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 13462 | if (insn->src_reg == 0) { |
6b173873 JK |
13463 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
13464 | ||
4976b718 | 13465 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 13466 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 13467 | return 0; |
6b173873 | 13468 | } |
17a52670 | 13469 | |
d400a6cf DB |
13470 | /* All special src_reg cases are listed below. From this point onwards |
13471 | * we either succeed and assign a corresponding dst_reg->type after | |
13472 | * zeroing the offset, or fail and reject the program. | |
13473 | */ | |
13474 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 13475 | |
d400a6cf | 13476 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
4976b718 | 13477 | dst_reg->type = aux->btf_var.reg_type; |
34d3a78c | 13478 | switch (base_type(dst_reg->type)) { |
4976b718 HL |
13479 | case PTR_TO_MEM: |
13480 | dst_reg->mem_size = aux->btf_var.mem_size; | |
13481 | break; | |
13482 | case PTR_TO_BTF_ID: | |
22dc4a0f | 13483 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
13484 | dst_reg->btf_id = aux->btf_var.btf_id; |
13485 | break; | |
13486 | default: | |
13487 | verbose(env, "bpf verifier is misconfigured\n"); | |
13488 | return -EFAULT; | |
13489 | } | |
13490 | return 0; | |
13491 | } | |
13492 | ||
69c087ba YS |
13493 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
13494 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
13495 | u32 subprogno = find_subprog(env, |
13496 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
13497 | |
13498 | if (!aux->func_info) { | |
13499 | verbose(env, "missing btf func_info\n"); | |
13500 | return -EINVAL; | |
13501 | } | |
13502 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
13503 | verbose(env, "callback function not static\n"); | |
13504 | return -EINVAL; | |
13505 | } | |
13506 | ||
13507 | dst_reg->type = PTR_TO_FUNC; | |
13508 | dst_reg->subprogno = subprogno; | |
13509 | return 0; | |
13510 | } | |
13511 | ||
d8eca5bb | 13512 | map = env->used_maps[aux->map_index]; |
4976b718 | 13513 | dst_reg->map_ptr = map; |
d8eca5bb | 13514 | |
387544bf AS |
13515 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
13516 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
13517 | dst_reg->type = PTR_TO_MAP_VALUE; |
13518 | dst_reg->off = aux->map_off; | |
d0d78c1d KKD |
13519 | WARN_ON_ONCE(map->max_entries != 1); |
13520 | /* We want reg->id to be same (0) as map_value is not distinct */ | |
387544bf AS |
13521 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
13522 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 13523 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
13524 | } else { |
13525 | verbose(env, "bpf verifier is misconfigured\n"); | |
13526 | return -EINVAL; | |
13527 | } | |
17a52670 | 13528 | |
17a52670 AS |
13529 | return 0; |
13530 | } | |
13531 | ||
96be4325 DB |
13532 | static bool may_access_skb(enum bpf_prog_type type) |
13533 | { | |
13534 | switch (type) { | |
13535 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
13536 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 13537 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
13538 | return true; |
13539 | default: | |
13540 | return false; | |
13541 | } | |
13542 | } | |
13543 | ||
ddd872bc AS |
13544 | /* verify safety of LD_ABS|LD_IND instructions: |
13545 | * - they can only appear in the programs where ctx == skb | |
13546 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
13547 | * preserve R6-R9, and store return value into R0 | |
13548 | * | |
13549 | * Implicit input: | |
13550 | * ctx == skb == R6 == CTX | |
13551 | * | |
13552 | * Explicit input: | |
13553 | * SRC == any register | |
13554 | * IMM == 32-bit immediate | |
13555 | * | |
13556 | * Output: | |
13557 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
13558 | */ | |
58e2af8b | 13559 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 13560 | { |
638f5b90 | 13561 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 13562 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 13563 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
13564 | int i, err; |
13565 | ||
7e40781c | 13566 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 13567 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
13568 | return -EINVAL; |
13569 | } | |
13570 | ||
e0cea7ce DB |
13571 | if (!env->ops->gen_ld_abs) { |
13572 | verbose(env, "bpf verifier is misconfigured\n"); | |
13573 | return -EINVAL; | |
13574 | } | |
13575 | ||
ddd872bc | 13576 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 13577 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 13578 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 13579 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
13580 | return -EINVAL; |
13581 | } | |
13582 | ||
13583 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 13584 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
13585 | if (err) |
13586 | return err; | |
13587 | ||
fd978bf7 JS |
13588 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
13589 | * gen_ld_abs() may terminate the program at runtime, leading to | |
13590 | * reference leak. | |
13591 | */ | |
13592 | err = check_reference_leak(env); | |
13593 | if (err) { | |
13594 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
13595 | return err; | |
13596 | } | |
13597 | ||
d0d78c1d | 13598 | if (env->cur_state->active_lock.ptr) { |
d83525ca AS |
13599 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
13600 | return -EINVAL; | |
13601 | } | |
13602 | ||
9bb00b28 YS |
13603 | if (env->cur_state->active_rcu_lock) { |
13604 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_rcu_read_lock-ed region\n"); | |
13605 | return -EINVAL; | |
13606 | } | |
13607 | ||
6d4f151a | 13608 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
13609 | verbose(env, |
13610 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
13611 | return -EINVAL; |
13612 | } | |
13613 | ||
13614 | if (mode == BPF_IND) { | |
13615 | /* check explicit source operand */ | |
dc503a8a | 13616 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
13617 | if (err) |
13618 | return err; | |
13619 | } | |
13620 | ||
be80a1d3 | 13621 | err = check_ptr_off_reg(env, ®s[ctx_reg], ctx_reg); |
6d4f151a DB |
13622 | if (err < 0) |
13623 | return err; | |
13624 | ||
ddd872bc | 13625 | /* reset caller saved regs to unreadable */ |
dc503a8a | 13626 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 13627 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
13628 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
13629 | } | |
ddd872bc AS |
13630 | |
13631 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
13632 | * the value fetched from the packet. |
13633 | * Already marked as written above. | |
ddd872bc | 13634 | */ |
61bd5218 | 13635 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
13636 | /* ld_abs load up to 32-bit skb data. */ |
13637 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
13638 | return 0; |
13639 | } | |
13640 | ||
390ee7e2 AS |
13641 | static int check_return_code(struct bpf_verifier_env *env) |
13642 | { | |
5cf1e914 | 13643 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 13644 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
13645 | struct bpf_reg_state *reg; |
13646 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 13647 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 13648 | int err; |
bfc6bb74 AS |
13649 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
13650 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 13651 | |
9e4e01df | 13652 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
d1a6edec SF |
13653 | if (!is_subprog) { |
13654 | switch (prog_type) { | |
13655 | case BPF_PROG_TYPE_LSM: | |
13656 | if (prog->expected_attach_type == BPF_LSM_CGROUP) | |
13657 | /* See below, can be 0 or 0-1 depending on hook. */ | |
13658 | break; | |
13659 | fallthrough; | |
13660 | case BPF_PROG_TYPE_STRUCT_OPS: | |
13661 | if (!prog->aux->attach_func_proto->type) | |
13662 | return 0; | |
13663 | break; | |
13664 | default: | |
13665 | break; | |
13666 | } | |
13667 | } | |
27ae7997 | 13668 | |
8fb33b60 | 13669 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
13670 | * to return the value from eBPF program. |
13671 | * Make sure that it's readable at this time | |
13672 | * of bpf_exit, which means that program wrote | |
13673 | * something into it earlier | |
13674 | */ | |
13675 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
13676 | if (err) | |
13677 | return err; | |
13678 | ||
13679 | if (is_pointer_value(env, BPF_REG_0)) { | |
13680 | verbose(env, "R0 leaks addr as return value\n"); | |
13681 | return -EACCES; | |
13682 | } | |
390ee7e2 | 13683 | |
f782e2c3 | 13684 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
13685 | |
13686 | if (frame->in_async_callback_fn) { | |
13687 | /* enforce return zero from async callbacks like timer */ | |
13688 | if (reg->type != SCALAR_VALUE) { | |
13689 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
c25b2ae1 | 13690 | reg_type_str(env, reg->type)); |
bfc6bb74 AS |
13691 | return -EINVAL; |
13692 | } | |
13693 | ||
13694 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
13695 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
13696 | return -EINVAL; | |
13697 | } | |
13698 | return 0; | |
13699 | } | |
13700 | ||
f782e2c3 DB |
13701 | if (is_subprog) { |
13702 | if (reg->type != SCALAR_VALUE) { | |
13703 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
c25b2ae1 | 13704 | reg_type_str(env, reg->type)); |
f782e2c3 DB |
13705 | return -EINVAL; |
13706 | } | |
13707 | return 0; | |
13708 | } | |
13709 | ||
7e40781c | 13710 | switch (prog_type) { |
983695fa DB |
13711 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
13712 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
13713 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
13714 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
13715 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
13716 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
13717 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 13718 | range = tnum_range(1, 1); |
77241217 SF |
13719 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
13720 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
13721 | range = tnum_range(0, 3); | |
ed4ed404 | 13722 | break; |
390ee7e2 | 13723 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 13724 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
13725 | range = tnum_range(0, 3); | |
13726 | enforce_attach_type_range = tnum_range(2, 3); | |
13727 | } | |
ed4ed404 | 13728 | break; |
390ee7e2 AS |
13729 | case BPF_PROG_TYPE_CGROUP_SOCK: |
13730 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 13731 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 13732 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 13733 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 13734 | break; |
15ab09bd AS |
13735 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
13736 | if (!env->prog->aux->attach_btf_id) | |
13737 | return 0; | |
13738 | range = tnum_const(0); | |
13739 | break; | |
15d83c4d | 13740 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
13741 | switch (env->prog->expected_attach_type) { |
13742 | case BPF_TRACE_FENTRY: | |
13743 | case BPF_TRACE_FEXIT: | |
13744 | range = tnum_const(0); | |
13745 | break; | |
13746 | case BPF_TRACE_RAW_TP: | |
13747 | case BPF_MODIFY_RETURN: | |
15d83c4d | 13748 | return 0; |
2ec0616e DB |
13749 | case BPF_TRACE_ITER: |
13750 | break; | |
e92888c7 YS |
13751 | default: |
13752 | return -ENOTSUPP; | |
13753 | } | |
15d83c4d | 13754 | break; |
e9ddbb77 JS |
13755 | case BPF_PROG_TYPE_SK_LOOKUP: |
13756 | range = tnum_range(SK_DROP, SK_PASS); | |
13757 | break; | |
69fd337a SF |
13758 | |
13759 | case BPF_PROG_TYPE_LSM: | |
13760 | if (env->prog->expected_attach_type != BPF_LSM_CGROUP) { | |
13761 | /* Regular BPF_PROG_TYPE_LSM programs can return | |
13762 | * any value. | |
13763 | */ | |
13764 | return 0; | |
13765 | } | |
13766 | if (!env->prog->aux->attach_func_proto->type) { | |
13767 | /* Make sure programs that attach to void | |
13768 | * hooks don't try to modify return value. | |
13769 | */ | |
13770 | range = tnum_range(1, 1); | |
13771 | } | |
13772 | break; | |
13773 | ||
e92888c7 YS |
13774 | case BPF_PROG_TYPE_EXT: |
13775 | /* freplace program can return anything as its return value | |
13776 | * depends on the to-be-replaced kernel func or bpf program. | |
13777 | */ | |
390ee7e2 AS |
13778 | default: |
13779 | return 0; | |
13780 | } | |
13781 | ||
390ee7e2 | 13782 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 13783 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
c25b2ae1 | 13784 | reg_type_str(env, reg->type)); |
390ee7e2 AS |
13785 | return -EINVAL; |
13786 | } | |
13787 | ||
13788 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 13789 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
69fd337a | 13790 | if (prog->expected_attach_type == BPF_LSM_CGROUP && |
d1a6edec | 13791 | prog_type == BPF_PROG_TYPE_LSM && |
69fd337a SF |
13792 | !prog->aux->attach_func_proto->type) |
13793 | verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
390ee7e2 AS |
13794 | return -EINVAL; |
13795 | } | |
5cf1e914 | 13796 | |
13797 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
13798 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
13799 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
13800 | return 0; |
13801 | } | |
13802 | ||
475fb78f AS |
13803 | /* non-recursive DFS pseudo code |
13804 | * 1 procedure DFS-iterative(G,v): | |
13805 | * 2 label v as discovered | |
13806 | * 3 let S be a stack | |
13807 | * 4 S.push(v) | |
13808 | * 5 while S is not empty | |
b6d20799 | 13809 | * 6 t <- S.peek() |
475fb78f AS |
13810 | * 7 if t is what we're looking for: |
13811 | * 8 return t | |
13812 | * 9 for all edges e in G.adjacentEdges(t) do | |
13813 | * 10 if edge e is already labelled | |
13814 | * 11 continue with the next edge | |
13815 | * 12 w <- G.adjacentVertex(t,e) | |
13816 | * 13 if vertex w is not discovered and not explored | |
13817 | * 14 label e as tree-edge | |
13818 | * 15 label w as discovered | |
13819 | * 16 S.push(w) | |
13820 | * 17 continue at 5 | |
13821 | * 18 else if vertex w is discovered | |
13822 | * 19 label e as back-edge | |
13823 | * 20 else | |
13824 | * 21 // vertex w is explored | |
13825 | * 22 label e as forward- or cross-edge | |
13826 | * 23 label t as explored | |
13827 | * 24 S.pop() | |
13828 | * | |
13829 | * convention: | |
13830 | * 0x10 - discovered | |
13831 | * 0x11 - discovered and fall-through edge labelled | |
13832 | * 0x12 - discovered and fall-through and branch edges labelled | |
13833 | * 0x20 - explored | |
13834 | */ | |
13835 | ||
13836 | enum { | |
13837 | DISCOVERED = 0x10, | |
13838 | EXPLORED = 0x20, | |
13839 | FALLTHROUGH = 1, | |
13840 | BRANCH = 2, | |
13841 | }; | |
13842 | ||
dc2a4ebc AS |
13843 | static u32 state_htab_size(struct bpf_verifier_env *env) |
13844 | { | |
13845 | return env->prog->len; | |
13846 | } | |
13847 | ||
5d839021 AS |
13848 | static struct bpf_verifier_state_list **explored_state( |
13849 | struct bpf_verifier_env *env, | |
13850 | int idx) | |
13851 | { | |
dc2a4ebc AS |
13852 | struct bpf_verifier_state *cur = env->cur_state; |
13853 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
13854 | ||
13855 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
13856 | } |
13857 | ||
bffdeaa8 | 13858 | static void mark_prune_point(struct bpf_verifier_env *env, int idx) |
5d839021 | 13859 | { |
a8f500af | 13860 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 13861 | } |
f1bca824 | 13862 | |
bffdeaa8 AN |
13863 | static bool is_prune_point(struct bpf_verifier_env *env, int insn_idx) |
13864 | { | |
13865 | return env->insn_aux_data[insn_idx].prune_point; | |
13866 | } | |
13867 | ||
4b5ce570 AN |
13868 | static void mark_force_checkpoint(struct bpf_verifier_env *env, int idx) |
13869 | { | |
13870 | env->insn_aux_data[idx].force_checkpoint = true; | |
13871 | } | |
13872 | ||
13873 | static bool is_force_checkpoint(struct bpf_verifier_env *env, int insn_idx) | |
13874 | { | |
13875 | return env->insn_aux_data[insn_idx].force_checkpoint; | |
13876 | } | |
13877 | ||
13878 | ||
59e2e27d WAF |
13879 | enum { |
13880 | DONE_EXPLORING = 0, | |
13881 | KEEP_EXPLORING = 1, | |
13882 | }; | |
13883 | ||
475fb78f AS |
13884 | /* t, w, e - match pseudo-code above: |
13885 | * t - index of current instruction | |
13886 | * w - next instruction | |
13887 | * e - edge | |
13888 | */ | |
2589726d AS |
13889 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
13890 | bool loop_ok) | |
475fb78f | 13891 | { |
7df737e9 AS |
13892 | int *insn_stack = env->cfg.insn_stack; |
13893 | int *insn_state = env->cfg.insn_state; | |
13894 | ||
475fb78f | 13895 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 13896 | return DONE_EXPLORING; |
475fb78f AS |
13897 | |
13898 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 13899 | return DONE_EXPLORING; |
475fb78f AS |
13900 | |
13901 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 13902 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 13903 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
13904 | return -EINVAL; |
13905 | } | |
13906 | ||
bffdeaa8 | 13907 | if (e == BRANCH) { |
f1bca824 | 13908 | /* mark branch target for state pruning */ |
bffdeaa8 AN |
13909 | mark_prune_point(env, w); |
13910 | mark_jmp_point(env, w); | |
13911 | } | |
f1bca824 | 13912 | |
475fb78f AS |
13913 | if (insn_state[w] == 0) { |
13914 | /* tree-edge */ | |
13915 | insn_state[t] = DISCOVERED | e; | |
13916 | insn_state[w] = DISCOVERED; | |
7df737e9 | 13917 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 13918 | return -E2BIG; |
7df737e9 | 13919 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 13920 | return KEEP_EXPLORING; |
475fb78f | 13921 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 13922 | if (loop_ok && env->bpf_capable) |
59e2e27d | 13923 | return DONE_EXPLORING; |
d9762e84 MKL |
13924 | verbose_linfo(env, t, "%d: ", t); |
13925 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 13926 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
13927 | return -EINVAL; |
13928 | } else if (insn_state[w] == EXPLORED) { | |
13929 | /* forward- or cross-edge */ | |
13930 | insn_state[t] = DISCOVERED | e; | |
13931 | } else { | |
61bd5218 | 13932 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
13933 | return -EFAULT; |
13934 | } | |
59e2e27d WAF |
13935 | return DONE_EXPLORING; |
13936 | } | |
13937 | ||
dcb2288b | 13938 | static int visit_func_call_insn(int t, struct bpf_insn *insns, |
efdb22de YS |
13939 | struct bpf_verifier_env *env, |
13940 | bool visit_callee) | |
13941 | { | |
13942 | int ret; | |
13943 | ||
13944 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
13945 | if (ret) | |
13946 | return ret; | |
13947 | ||
618945fb AN |
13948 | mark_prune_point(env, t + 1); |
13949 | /* when we exit from subprog, we need to record non-linear history */ | |
13950 | mark_jmp_point(env, t + 1); | |
13951 | ||
efdb22de | 13952 | if (visit_callee) { |
bffdeaa8 | 13953 | mark_prune_point(env, t); |
86fc6ee6 AS |
13954 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
13955 | /* It's ok to allow recursion from CFG point of | |
13956 | * view. __check_func_call() will do the actual | |
13957 | * check. | |
13958 | */ | |
13959 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
13960 | } |
13961 | return ret; | |
13962 | } | |
13963 | ||
59e2e27d WAF |
13964 | /* Visits the instruction at index t and returns one of the following: |
13965 | * < 0 - an error occurred | |
13966 | * DONE_EXPLORING - the instruction was fully explored | |
13967 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
13968 | */ | |
dcb2288b | 13969 | static int visit_insn(int t, struct bpf_verifier_env *env) |
59e2e27d | 13970 | { |
653ae3a8 | 13971 | struct bpf_insn *insns = env->prog->insnsi, *insn = &insns[t]; |
59e2e27d WAF |
13972 | int ret; |
13973 | ||
653ae3a8 | 13974 | if (bpf_pseudo_func(insn)) |
dcb2288b | 13975 | return visit_func_call_insn(t, insns, env, true); |
69c087ba | 13976 | |
59e2e27d | 13977 | /* All non-branch instructions have a single fall-through edge. */ |
653ae3a8 AN |
13978 | if (BPF_CLASS(insn->code) != BPF_JMP && |
13979 | BPF_CLASS(insn->code) != BPF_JMP32) | |
59e2e27d WAF |
13980 | return push_insn(t, t + 1, FALLTHROUGH, env, false); |
13981 | ||
653ae3a8 | 13982 | switch (BPF_OP(insn->code)) { |
59e2e27d WAF |
13983 | case BPF_EXIT: |
13984 | return DONE_EXPLORING; | |
13985 | ||
13986 | case BPF_CALL: | |
c1ee85a9 | 13987 | if (insn->src_reg == 0 && insn->imm == BPF_FUNC_timer_set_callback) |
618945fb AN |
13988 | /* Mark this call insn as a prune point to trigger |
13989 | * is_state_visited() check before call itself is | |
13990 | * processed by __check_func_call(). Otherwise new | |
13991 | * async state will be pushed for further exploration. | |
bfc6bb74 | 13992 | */ |
bffdeaa8 | 13993 | mark_prune_point(env, t); |
06accc87 AN |
13994 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
13995 | struct bpf_kfunc_call_arg_meta meta; | |
13996 | ||
13997 | ret = fetch_kfunc_meta(env, insn, &meta, NULL); | |
4b5ce570 | 13998 | if (ret == 0 && is_iter_next_kfunc(&meta)) { |
06accc87 | 13999 | mark_prune_point(env, t); |
4b5ce570 AN |
14000 | /* Checking and saving state checkpoints at iter_next() call |
14001 | * is crucial for fast convergence of open-coded iterator loop | |
14002 | * logic, so we need to force it. If we don't do that, | |
14003 | * is_state_visited() might skip saving a checkpoint, causing | |
14004 | * unnecessarily long sequence of not checkpointed | |
14005 | * instructions and jumps, leading to exhaustion of jump | |
14006 | * history buffer, and potentially other undesired outcomes. | |
14007 | * It is expected that with correct open-coded iterators | |
14008 | * convergence will happen quickly, so we don't run a risk of | |
14009 | * exhausting memory. | |
14010 | */ | |
14011 | mark_force_checkpoint(env, t); | |
14012 | } | |
06accc87 | 14013 | } |
653ae3a8 | 14014 | return visit_func_call_insn(t, insns, env, insn->src_reg == BPF_PSEUDO_CALL); |
59e2e27d WAF |
14015 | |
14016 | case BPF_JA: | |
653ae3a8 | 14017 | if (BPF_SRC(insn->code) != BPF_K) |
59e2e27d WAF |
14018 | return -EINVAL; |
14019 | ||
14020 | /* unconditional jump with single edge */ | |
653ae3a8 | 14021 | ret = push_insn(t, t + insn->off + 1, FALLTHROUGH, env, |
59e2e27d WAF |
14022 | true); |
14023 | if (ret) | |
14024 | return ret; | |
14025 | ||
653ae3a8 AN |
14026 | mark_prune_point(env, t + insn->off + 1); |
14027 | mark_jmp_point(env, t + insn->off + 1); | |
59e2e27d WAF |
14028 | |
14029 | return ret; | |
14030 | ||
14031 | default: | |
14032 | /* conditional jump with two edges */ | |
bffdeaa8 | 14033 | mark_prune_point(env, t); |
618945fb | 14034 | |
59e2e27d WAF |
14035 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
14036 | if (ret) | |
14037 | return ret; | |
14038 | ||
653ae3a8 | 14039 | return push_insn(t, t + insn->off + 1, BRANCH, env, true); |
59e2e27d | 14040 | } |
475fb78f AS |
14041 | } |
14042 | ||
14043 | /* non-recursive depth-first-search to detect loops in BPF program | |
14044 | * loop == back-edge in directed graph | |
14045 | */ | |
58e2af8b | 14046 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 14047 | { |
475fb78f | 14048 | int insn_cnt = env->prog->len; |
7df737e9 | 14049 | int *insn_stack, *insn_state; |
475fb78f | 14050 | int ret = 0; |
59e2e27d | 14051 | int i; |
475fb78f | 14052 | |
7df737e9 | 14053 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
14054 | if (!insn_state) |
14055 | return -ENOMEM; | |
14056 | ||
7df737e9 | 14057 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 14058 | if (!insn_stack) { |
71dde681 | 14059 | kvfree(insn_state); |
475fb78f AS |
14060 | return -ENOMEM; |
14061 | } | |
14062 | ||
14063 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
14064 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 14065 | env->cfg.cur_stack = 1; |
475fb78f | 14066 | |
59e2e27d WAF |
14067 | while (env->cfg.cur_stack > 0) { |
14068 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 14069 | |
dcb2288b | 14070 | ret = visit_insn(t, env); |
59e2e27d WAF |
14071 | switch (ret) { |
14072 | case DONE_EXPLORING: | |
14073 | insn_state[t] = EXPLORED; | |
14074 | env->cfg.cur_stack--; | |
14075 | break; | |
14076 | case KEEP_EXPLORING: | |
14077 | break; | |
14078 | default: | |
14079 | if (ret > 0) { | |
14080 | verbose(env, "visit_insn internal bug\n"); | |
14081 | ret = -EFAULT; | |
475fb78f | 14082 | } |
475fb78f | 14083 | goto err_free; |
59e2e27d | 14084 | } |
475fb78f AS |
14085 | } |
14086 | ||
59e2e27d | 14087 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 14088 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
14089 | ret = -EFAULT; |
14090 | goto err_free; | |
14091 | } | |
475fb78f | 14092 | |
475fb78f AS |
14093 | for (i = 0; i < insn_cnt; i++) { |
14094 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 14095 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
14096 | ret = -EINVAL; |
14097 | goto err_free; | |
14098 | } | |
14099 | } | |
14100 | ret = 0; /* cfg looks good */ | |
14101 | ||
14102 | err_free: | |
71dde681 AS |
14103 | kvfree(insn_state); |
14104 | kvfree(insn_stack); | |
7df737e9 | 14105 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
14106 | return ret; |
14107 | } | |
14108 | ||
09b28d76 AS |
14109 | static int check_abnormal_return(struct bpf_verifier_env *env) |
14110 | { | |
14111 | int i; | |
14112 | ||
14113 | for (i = 1; i < env->subprog_cnt; i++) { | |
14114 | if (env->subprog_info[i].has_ld_abs) { | |
14115 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
14116 | return -EINVAL; | |
14117 | } | |
14118 | if (env->subprog_info[i].has_tail_call) { | |
14119 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
14120 | return -EINVAL; | |
14121 | } | |
14122 | } | |
14123 | return 0; | |
14124 | } | |
14125 | ||
838e9690 YS |
14126 | /* The minimum supported BTF func info size */ |
14127 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
14128 | #define MAX_FUNCINFO_REC_SIZE 252 | |
14129 | ||
c454a46b MKL |
14130 | static int check_btf_func(struct bpf_verifier_env *env, |
14131 | const union bpf_attr *attr, | |
af2ac3e1 | 14132 | bpfptr_t uattr) |
838e9690 | 14133 | { |
09b28d76 | 14134 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 14135 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 14136 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 14137 | struct bpf_func_info *krecord; |
8c1b6e69 | 14138 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
14139 | struct bpf_prog *prog; |
14140 | const struct btf *btf; | |
af2ac3e1 | 14141 | bpfptr_t urecord; |
d0b2818e | 14142 | u32 prev_offset = 0; |
09b28d76 | 14143 | bool scalar_return; |
e7ed83d6 | 14144 | int ret = -ENOMEM; |
838e9690 YS |
14145 | |
14146 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
14147 | if (!nfuncs) { |
14148 | if (check_abnormal_return(env)) | |
14149 | return -EINVAL; | |
838e9690 | 14150 | return 0; |
09b28d76 | 14151 | } |
838e9690 YS |
14152 | |
14153 | if (nfuncs != env->subprog_cnt) { | |
14154 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
14155 | return -EINVAL; | |
14156 | } | |
14157 | ||
14158 | urec_size = attr->func_info_rec_size; | |
14159 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
14160 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
14161 | urec_size % sizeof(u32)) { | |
14162 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
14163 | return -EINVAL; | |
14164 | } | |
14165 | ||
c454a46b MKL |
14166 | prog = env->prog; |
14167 | btf = prog->aux->btf; | |
838e9690 | 14168 | |
af2ac3e1 | 14169 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
14170 | min_size = min_t(u32, krec_size, urec_size); |
14171 | ||
ba64e7d8 | 14172 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
14173 | if (!krecord) |
14174 | return -ENOMEM; | |
8c1b6e69 AS |
14175 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
14176 | if (!info_aux) | |
14177 | goto err_free; | |
ba64e7d8 | 14178 | |
838e9690 YS |
14179 | for (i = 0; i < nfuncs; i++) { |
14180 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
14181 | if (ret) { | |
14182 | if (ret == -E2BIG) { | |
14183 | verbose(env, "nonzero tailing record in func info"); | |
14184 | /* set the size kernel expects so loader can zero | |
14185 | * out the rest of the record. | |
14186 | */ | |
af2ac3e1 AS |
14187 | if (copy_to_bpfptr_offset(uattr, |
14188 | offsetof(union bpf_attr, func_info_rec_size), | |
14189 | &min_size, sizeof(min_size))) | |
838e9690 YS |
14190 | ret = -EFAULT; |
14191 | } | |
c454a46b | 14192 | goto err_free; |
838e9690 YS |
14193 | } |
14194 | ||
af2ac3e1 | 14195 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 14196 | ret = -EFAULT; |
c454a46b | 14197 | goto err_free; |
838e9690 YS |
14198 | } |
14199 | ||
d30d42e0 | 14200 | /* check insn_off */ |
09b28d76 | 14201 | ret = -EINVAL; |
838e9690 | 14202 | if (i == 0) { |
d30d42e0 | 14203 | if (krecord[i].insn_off) { |
838e9690 | 14204 | verbose(env, |
d30d42e0 MKL |
14205 | "nonzero insn_off %u for the first func info record", |
14206 | krecord[i].insn_off); | |
c454a46b | 14207 | goto err_free; |
838e9690 | 14208 | } |
d30d42e0 | 14209 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
14210 | verbose(env, |
14211 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 14212 | krecord[i].insn_off, prev_offset); |
c454a46b | 14213 | goto err_free; |
838e9690 YS |
14214 | } |
14215 | ||
d30d42e0 | 14216 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 14217 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 14218 | goto err_free; |
838e9690 YS |
14219 | } |
14220 | ||
14221 | /* check type_id */ | |
ba64e7d8 | 14222 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 14223 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 14224 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 14225 | krecord[i].type_id); |
c454a46b | 14226 | goto err_free; |
838e9690 | 14227 | } |
51c39bb1 | 14228 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
14229 | |
14230 | func_proto = btf_type_by_id(btf, type->type); | |
14231 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
14232 | /* btf_func_check() already verified it during BTF load */ | |
14233 | goto err_free; | |
14234 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
14235 | scalar_return = | |
6089fb32 | 14236 | btf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type); |
09b28d76 AS |
14237 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
14238 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
14239 | goto err_free; | |
14240 | } | |
14241 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
14242 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
14243 | goto err_free; | |
14244 | } | |
14245 | ||
d30d42e0 | 14246 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 14247 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
14248 | } |
14249 | ||
ba64e7d8 YS |
14250 | prog->aux->func_info = krecord; |
14251 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 14252 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
14253 | return 0; |
14254 | ||
c454a46b | 14255 | err_free: |
ba64e7d8 | 14256 | kvfree(krecord); |
8c1b6e69 | 14257 | kfree(info_aux); |
838e9690 YS |
14258 | return ret; |
14259 | } | |
14260 | ||
ba64e7d8 YS |
14261 | static void adjust_btf_func(struct bpf_verifier_env *env) |
14262 | { | |
8c1b6e69 | 14263 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
14264 | int i; |
14265 | ||
8c1b6e69 | 14266 | if (!aux->func_info) |
ba64e7d8 YS |
14267 | return; |
14268 | ||
14269 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 14270 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
14271 | } |
14272 | ||
1b773d00 | 14273 | #define MIN_BPF_LINEINFO_SIZE offsetofend(struct bpf_line_info, line_col) |
c454a46b MKL |
14274 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
14275 | ||
14276 | static int check_btf_line(struct bpf_verifier_env *env, | |
14277 | const union bpf_attr *attr, | |
af2ac3e1 | 14278 | bpfptr_t uattr) |
c454a46b MKL |
14279 | { |
14280 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
14281 | struct bpf_subprog_info *sub; | |
14282 | struct bpf_line_info *linfo; | |
14283 | struct bpf_prog *prog; | |
14284 | const struct btf *btf; | |
af2ac3e1 | 14285 | bpfptr_t ulinfo; |
c454a46b MKL |
14286 | int err; |
14287 | ||
14288 | nr_linfo = attr->line_info_cnt; | |
14289 | if (!nr_linfo) | |
14290 | return 0; | |
0e6491b5 BC |
14291 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
14292 | return -EINVAL; | |
c454a46b MKL |
14293 | |
14294 | rec_size = attr->line_info_rec_size; | |
14295 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
14296 | rec_size > MAX_LINEINFO_REC_SIZE || | |
14297 | rec_size & (sizeof(u32) - 1)) | |
14298 | return -EINVAL; | |
14299 | ||
14300 | /* Need to zero it in case the userspace may | |
14301 | * pass in a smaller bpf_line_info object. | |
14302 | */ | |
14303 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
14304 | GFP_KERNEL | __GFP_NOWARN); | |
14305 | if (!linfo) | |
14306 | return -ENOMEM; | |
14307 | ||
14308 | prog = env->prog; | |
14309 | btf = prog->aux->btf; | |
14310 | ||
14311 | s = 0; | |
14312 | sub = env->subprog_info; | |
af2ac3e1 | 14313 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
14314 | expected_size = sizeof(struct bpf_line_info); |
14315 | ncopy = min_t(u32, expected_size, rec_size); | |
14316 | for (i = 0; i < nr_linfo; i++) { | |
14317 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
14318 | if (err) { | |
14319 | if (err == -E2BIG) { | |
14320 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
14321 | if (copy_to_bpfptr_offset(uattr, |
14322 | offsetof(union bpf_attr, line_info_rec_size), | |
14323 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
14324 | err = -EFAULT; |
14325 | } | |
14326 | goto err_free; | |
14327 | } | |
14328 | ||
af2ac3e1 | 14329 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
14330 | err = -EFAULT; |
14331 | goto err_free; | |
14332 | } | |
14333 | ||
14334 | /* | |
14335 | * Check insn_off to ensure | |
14336 | * 1) strictly increasing AND | |
14337 | * 2) bounded by prog->len | |
14338 | * | |
14339 | * The linfo[0].insn_off == 0 check logically falls into | |
14340 | * the later "missing bpf_line_info for func..." case | |
14341 | * because the first linfo[0].insn_off must be the | |
14342 | * first sub also and the first sub must have | |
14343 | * subprog_info[0].start == 0. | |
14344 | */ | |
14345 | if ((i && linfo[i].insn_off <= prev_offset) || | |
14346 | linfo[i].insn_off >= prog->len) { | |
14347 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
14348 | i, linfo[i].insn_off, prev_offset, | |
14349 | prog->len); | |
14350 | err = -EINVAL; | |
14351 | goto err_free; | |
14352 | } | |
14353 | ||
fdbaa0be MKL |
14354 | if (!prog->insnsi[linfo[i].insn_off].code) { |
14355 | verbose(env, | |
14356 | "Invalid insn code at line_info[%u].insn_off\n", | |
14357 | i); | |
14358 | err = -EINVAL; | |
14359 | goto err_free; | |
14360 | } | |
14361 | ||
23127b33 MKL |
14362 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
14363 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
14364 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
14365 | err = -EINVAL; | |
14366 | goto err_free; | |
14367 | } | |
14368 | ||
14369 | if (s != env->subprog_cnt) { | |
14370 | if (linfo[i].insn_off == sub[s].start) { | |
14371 | sub[s].linfo_idx = i; | |
14372 | s++; | |
14373 | } else if (sub[s].start < linfo[i].insn_off) { | |
14374 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
14375 | err = -EINVAL; | |
14376 | goto err_free; | |
14377 | } | |
14378 | } | |
14379 | ||
14380 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 14381 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
14382 | } |
14383 | ||
14384 | if (s != env->subprog_cnt) { | |
14385 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
14386 | env->subprog_cnt - s, s); | |
14387 | err = -EINVAL; | |
14388 | goto err_free; | |
14389 | } | |
14390 | ||
14391 | prog->aux->linfo = linfo; | |
14392 | prog->aux->nr_linfo = nr_linfo; | |
14393 | ||
14394 | return 0; | |
14395 | ||
14396 | err_free: | |
14397 | kvfree(linfo); | |
14398 | return err; | |
14399 | } | |
14400 | ||
fbd94c7a AS |
14401 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
14402 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
14403 | ||
14404 | static int check_core_relo(struct bpf_verifier_env *env, | |
14405 | const union bpf_attr *attr, | |
14406 | bpfptr_t uattr) | |
14407 | { | |
14408 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
14409 | struct bpf_core_relo core_relo = {}; | |
14410 | struct bpf_prog *prog = env->prog; | |
14411 | const struct btf *btf = prog->aux->btf; | |
14412 | struct bpf_core_ctx ctx = { | |
14413 | .log = &env->log, | |
14414 | .btf = btf, | |
14415 | }; | |
14416 | bpfptr_t u_core_relo; | |
14417 | int err; | |
14418 | ||
14419 | nr_core_relo = attr->core_relo_cnt; | |
14420 | if (!nr_core_relo) | |
14421 | return 0; | |
14422 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
14423 | return -EINVAL; | |
14424 | ||
14425 | rec_size = attr->core_relo_rec_size; | |
14426 | if (rec_size < MIN_CORE_RELO_SIZE || | |
14427 | rec_size > MAX_CORE_RELO_SIZE || | |
14428 | rec_size % sizeof(u32)) | |
14429 | return -EINVAL; | |
14430 | ||
14431 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
14432 | expected_size = sizeof(struct bpf_core_relo); | |
14433 | ncopy = min_t(u32, expected_size, rec_size); | |
14434 | ||
14435 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
14436 | * relocation record one at a time. | |
14437 | */ | |
14438 | for (i = 0; i < nr_core_relo; i++) { | |
14439 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
14440 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
14441 | if (err) { | |
14442 | if (err == -E2BIG) { | |
14443 | verbose(env, "nonzero tailing record in core_relo"); | |
14444 | if (copy_to_bpfptr_offset(uattr, | |
14445 | offsetof(union bpf_attr, core_relo_rec_size), | |
14446 | &expected_size, sizeof(expected_size))) | |
14447 | err = -EFAULT; | |
14448 | } | |
14449 | break; | |
14450 | } | |
14451 | ||
14452 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
14453 | err = -EFAULT; | |
14454 | break; | |
14455 | } | |
14456 | ||
14457 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
14458 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
14459 | i, core_relo.insn_off, prog->len); | |
14460 | err = -EINVAL; | |
14461 | break; | |
14462 | } | |
14463 | ||
14464 | err = bpf_core_apply(&ctx, &core_relo, i, | |
14465 | &prog->insnsi[core_relo.insn_off / 8]); | |
14466 | if (err) | |
14467 | break; | |
14468 | bpfptr_add(&u_core_relo, rec_size); | |
14469 | } | |
14470 | return err; | |
14471 | } | |
14472 | ||
c454a46b MKL |
14473 | static int check_btf_info(struct bpf_verifier_env *env, |
14474 | const union bpf_attr *attr, | |
af2ac3e1 | 14475 | bpfptr_t uattr) |
c454a46b MKL |
14476 | { |
14477 | struct btf *btf; | |
14478 | int err; | |
14479 | ||
09b28d76 AS |
14480 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
14481 | if (check_abnormal_return(env)) | |
14482 | return -EINVAL; | |
c454a46b | 14483 | return 0; |
09b28d76 | 14484 | } |
c454a46b MKL |
14485 | |
14486 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
14487 | if (IS_ERR(btf)) | |
14488 | return PTR_ERR(btf); | |
350a5c4d AS |
14489 | if (btf_is_kernel(btf)) { |
14490 | btf_put(btf); | |
14491 | return -EACCES; | |
14492 | } | |
c454a46b MKL |
14493 | env->prog->aux->btf = btf; |
14494 | ||
14495 | err = check_btf_func(env, attr, uattr); | |
14496 | if (err) | |
14497 | return err; | |
14498 | ||
14499 | err = check_btf_line(env, attr, uattr); | |
14500 | if (err) | |
14501 | return err; | |
14502 | ||
fbd94c7a AS |
14503 | err = check_core_relo(env, attr, uattr); |
14504 | if (err) | |
14505 | return err; | |
14506 | ||
c454a46b | 14507 | return 0; |
ba64e7d8 YS |
14508 | } |
14509 | ||
f1174f77 EC |
14510 | /* check %cur's range satisfies %old's */ |
14511 | static bool range_within(struct bpf_reg_state *old, | |
14512 | struct bpf_reg_state *cur) | |
14513 | { | |
b03c9f9f EC |
14514 | return old->umin_value <= cur->umin_value && |
14515 | old->umax_value >= cur->umax_value && | |
14516 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
14517 | old->smax_value >= cur->smax_value && |
14518 | old->u32_min_value <= cur->u32_min_value && | |
14519 | old->u32_max_value >= cur->u32_max_value && | |
14520 | old->s32_min_value <= cur->s32_min_value && | |
14521 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
14522 | } |
14523 | ||
f1174f77 EC |
14524 | /* If in the old state two registers had the same id, then they need to have |
14525 | * the same id in the new state as well. But that id could be different from | |
14526 | * the old state, so we need to track the mapping from old to new ids. | |
14527 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
14528 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
14529 | * regs with a different old id could still have new id 9, we don't care about | |
14530 | * that. | |
14531 | * So we look through our idmap to see if this old id has been seen before. If | |
14532 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 14533 | */ |
c9e73e3d | 14534 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 14535 | { |
f1174f77 | 14536 | unsigned int i; |
969bf05e | 14537 | |
4633a006 AN |
14538 | /* either both IDs should be set or both should be zero */ |
14539 | if (!!old_id != !!cur_id) | |
14540 | return false; | |
14541 | ||
14542 | if (old_id == 0) /* cur_id == 0 as well */ | |
14543 | return true; | |
14544 | ||
c9e73e3d | 14545 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
14546 | if (!idmap[i].old) { |
14547 | /* Reached an empty slot; haven't seen this id before */ | |
14548 | idmap[i].old = old_id; | |
14549 | idmap[i].cur = cur_id; | |
14550 | return true; | |
14551 | } | |
14552 | if (idmap[i].old == old_id) | |
14553 | return idmap[i].cur == cur_id; | |
14554 | } | |
14555 | /* We ran out of idmap slots, which should be impossible */ | |
14556 | WARN_ON_ONCE(1); | |
14557 | return false; | |
14558 | } | |
14559 | ||
9242b5f5 AS |
14560 | static void clean_func_state(struct bpf_verifier_env *env, |
14561 | struct bpf_func_state *st) | |
14562 | { | |
14563 | enum bpf_reg_liveness live; | |
14564 | int i, j; | |
14565 | ||
14566 | for (i = 0; i < BPF_REG_FP; i++) { | |
14567 | live = st->regs[i].live; | |
14568 | /* liveness must not touch this register anymore */ | |
14569 | st->regs[i].live |= REG_LIVE_DONE; | |
14570 | if (!(live & REG_LIVE_READ)) | |
14571 | /* since the register is unused, clear its state | |
14572 | * to make further comparison simpler | |
14573 | */ | |
f54c7898 | 14574 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
14575 | } |
14576 | ||
14577 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
14578 | live = st->stack[i].spilled_ptr.live; | |
14579 | /* liveness must not touch this stack slot anymore */ | |
14580 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
14581 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 14582 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
14583 | for (j = 0; j < BPF_REG_SIZE; j++) |
14584 | st->stack[i].slot_type[j] = STACK_INVALID; | |
14585 | } | |
14586 | } | |
14587 | } | |
14588 | ||
14589 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
14590 | struct bpf_verifier_state *st) | |
14591 | { | |
14592 | int i; | |
14593 | ||
14594 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
14595 | /* all regs in this state in all frames were already marked */ | |
14596 | return; | |
14597 | ||
14598 | for (i = 0; i <= st->curframe; i++) | |
14599 | clean_func_state(env, st->frame[i]); | |
14600 | } | |
14601 | ||
14602 | /* the parentage chains form a tree. | |
14603 | * the verifier states are added to state lists at given insn and | |
14604 | * pushed into state stack for future exploration. | |
14605 | * when the verifier reaches bpf_exit insn some of the verifer states | |
14606 | * stored in the state lists have their final liveness state already, | |
14607 | * but a lot of states will get revised from liveness point of view when | |
14608 | * the verifier explores other branches. | |
14609 | * Example: | |
14610 | * 1: r0 = 1 | |
14611 | * 2: if r1 == 100 goto pc+1 | |
14612 | * 3: r0 = 2 | |
14613 | * 4: exit | |
14614 | * when the verifier reaches exit insn the register r0 in the state list of | |
14615 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
14616 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
14617 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
14618 | * | |
14619 | * Since the verifier pushes the branch states as it sees them while exploring | |
14620 | * the program the condition of walking the branch instruction for the second | |
14621 | * time means that all states below this branch were already explored and | |
8fb33b60 | 14622 | * their final liveness marks are already propagated. |
9242b5f5 AS |
14623 | * Hence when the verifier completes the search of state list in is_state_visited() |
14624 | * we can call this clean_live_states() function to mark all liveness states | |
14625 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
14626 | * will not be used. | |
14627 | * This function also clears the registers and stack for states that !READ | |
14628 | * to simplify state merging. | |
14629 | * | |
14630 | * Important note here that walking the same branch instruction in the callee | |
14631 | * doesn't meant that the states are DONE. The verifier has to compare | |
14632 | * the callsites | |
14633 | */ | |
14634 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
14635 | struct bpf_verifier_state *cur) | |
14636 | { | |
14637 | struct bpf_verifier_state_list *sl; | |
14638 | int i; | |
14639 | ||
5d839021 | 14640 | sl = *explored_state(env, insn); |
a8f500af | 14641 | while (sl) { |
2589726d AS |
14642 | if (sl->state.branches) |
14643 | goto next; | |
dc2a4ebc AS |
14644 | if (sl->state.insn_idx != insn || |
14645 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
14646 | goto next; |
14647 | for (i = 0; i <= cur->curframe; i++) | |
14648 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
14649 | goto next; | |
14650 | clean_verifier_state(env, &sl->state); | |
14651 | next: | |
14652 | sl = sl->next; | |
14653 | } | |
14654 | } | |
14655 | ||
4a95c85c | 14656 | static bool regs_exact(const struct bpf_reg_state *rold, |
4633a006 AN |
14657 | const struct bpf_reg_state *rcur, |
14658 | struct bpf_id_pair *idmap) | |
4a95c85c | 14659 | { |
4633a006 AN |
14660 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
14661 | check_ids(rold->id, rcur->id, idmap) && | |
14662 | check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap); | |
4a95c85c AN |
14663 | } |
14664 | ||
f1174f77 | 14665 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
14666 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
14667 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 14668 | { |
dc503a8a EC |
14669 | if (!(rold->live & REG_LIVE_READ)) |
14670 | /* explored state didn't use this */ | |
14671 | return true; | |
f1174f77 EC |
14672 | if (rold->type == NOT_INIT) |
14673 | /* explored state can't have used this */ | |
969bf05e | 14674 | return true; |
f1174f77 EC |
14675 | if (rcur->type == NOT_INIT) |
14676 | return false; | |
7f4ce97c | 14677 | |
910f6999 AN |
14678 | /* Enforce that register types have to match exactly, including their |
14679 | * modifiers (like PTR_MAYBE_NULL, MEM_RDONLY, etc), as a general | |
14680 | * rule. | |
14681 | * | |
14682 | * One can make a point that using a pointer register as unbounded | |
14683 | * SCALAR would be technically acceptable, but this could lead to | |
14684 | * pointer leaks because scalars are allowed to leak while pointers | |
14685 | * are not. We could make this safe in special cases if root is | |
14686 | * calling us, but it's probably not worth the hassle. | |
14687 | * | |
14688 | * Also, register types that are *not* MAYBE_NULL could technically be | |
14689 | * safe to use as their MAYBE_NULL variants (e.g., PTR_TO_MAP_VALUE | |
14690 | * is safe to be used as PTR_TO_MAP_VALUE_OR_NULL, provided both point | |
14691 | * to the same map). | |
7f4ce97c AN |
14692 | * However, if the old MAYBE_NULL register then got NULL checked, |
14693 | * doing so could have affected others with the same id, and we can't | |
14694 | * check for that because we lost the id when we converted to | |
14695 | * a non-MAYBE_NULL variant. | |
14696 | * So, as a general rule we don't allow mixing MAYBE_NULL and | |
910f6999 | 14697 | * non-MAYBE_NULL registers as well. |
7f4ce97c | 14698 | */ |
910f6999 | 14699 | if (rold->type != rcur->type) |
7f4ce97c AN |
14700 | return false; |
14701 | ||
c25b2ae1 | 14702 | switch (base_type(rold->type)) { |
f1174f77 | 14703 | case SCALAR_VALUE: |
4633a006 | 14704 | if (regs_exact(rold, rcur, idmap)) |
7c884339 | 14705 | return true; |
e042aa53 DB |
14706 | if (env->explore_alu_limits) |
14707 | return false; | |
910f6999 AN |
14708 | if (!rold->precise) |
14709 | return true; | |
14710 | /* new val must satisfy old val knowledge */ | |
14711 | return range_within(rold, rcur) && | |
14712 | tnum_in(rold->var_off, rcur->var_off); | |
69c087ba | 14713 | case PTR_TO_MAP_KEY: |
f1174f77 | 14714 | case PTR_TO_MAP_VALUE: |
567da5d2 AN |
14715 | case PTR_TO_MEM: |
14716 | case PTR_TO_BUF: | |
14717 | case PTR_TO_TP_BUFFER: | |
1b688a19 EC |
14718 | /* If the new min/max/var_off satisfy the old ones and |
14719 | * everything else matches, we are OK. | |
1b688a19 | 14720 | */ |
a73bf9f2 | 14721 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, var_off)) == 0 && |
1b688a19 | 14722 | range_within(rold, rcur) && |
4ea2bb15 | 14723 | tnum_in(rold->var_off, rcur->var_off) && |
567da5d2 AN |
14724 | check_ids(rold->id, rcur->id, idmap) && |
14725 | check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap); | |
de8f3a83 | 14726 | case PTR_TO_PACKET_META: |
f1174f77 | 14727 | case PTR_TO_PACKET: |
f1174f77 EC |
14728 | /* We must have at least as much range as the old ptr |
14729 | * did, so that any accesses which were safe before are | |
14730 | * still safe. This is true even if old range < old off, | |
14731 | * since someone could have accessed through (ptr - k), or | |
14732 | * even done ptr -= k in a register, to get a safe access. | |
14733 | */ | |
14734 | if (rold->range > rcur->range) | |
14735 | return false; | |
14736 | /* If the offsets don't match, we can't trust our alignment; | |
14737 | * nor can we be sure that we won't fall out of range. | |
14738 | */ | |
14739 | if (rold->off != rcur->off) | |
14740 | return false; | |
14741 | /* id relations must be preserved */ | |
4633a006 | 14742 | if (!check_ids(rold->id, rcur->id, idmap)) |
f1174f77 EC |
14743 | return false; |
14744 | /* new val must satisfy old val knowledge */ | |
14745 | return range_within(rold, rcur) && | |
14746 | tnum_in(rold->var_off, rcur->var_off); | |
7c884339 EZ |
14747 | case PTR_TO_STACK: |
14748 | /* two stack pointers are equal only if they're pointing to | |
14749 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
f1174f77 | 14750 | */ |
4633a006 | 14751 | return regs_exact(rold, rcur, idmap) && rold->frameno == rcur->frameno; |
f1174f77 | 14752 | default: |
4633a006 | 14753 | return regs_exact(rold, rcur, idmap); |
f1174f77 | 14754 | } |
969bf05e AS |
14755 | } |
14756 | ||
e042aa53 DB |
14757 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
14758 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
14759 | { |
14760 | int i, spi; | |
14761 | ||
638f5b90 AS |
14762 | /* walk slots of the explored stack and ignore any additional |
14763 | * slots in the current stack, since explored(safe) state | |
14764 | * didn't use them | |
14765 | */ | |
14766 | for (i = 0; i < old->allocated_stack; i++) { | |
06accc87 AN |
14767 | struct bpf_reg_state *old_reg, *cur_reg; |
14768 | ||
638f5b90 AS |
14769 | spi = i / BPF_REG_SIZE; |
14770 | ||
b233920c AS |
14771 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
14772 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 14773 | /* explored state didn't use this */ |
fd05e57b | 14774 | continue; |
b233920c | 14775 | } |
cc2b14d5 | 14776 | |
638f5b90 AS |
14777 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
14778 | continue; | |
19e2dbb7 | 14779 | |
6715df8d EZ |
14780 | if (env->allow_uninit_stack && |
14781 | old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC) | |
14782 | continue; | |
14783 | ||
19e2dbb7 AS |
14784 | /* explored stack has more populated slots than current stack |
14785 | * and these slots were used | |
14786 | */ | |
14787 | if (i >= cur->allocated_stack) | |
14788 | return false; | |
14789 | ||
cc2b14d5 AS |
14790 | /* if old state was safe with misc data in the stack |
14791 | * it will be safe with zero-initialized stack. | |
14792 | * The opposite is not true | |
14793 | */ | |
14794 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
14795 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
14796 | continue; | |
638f5b90 AS |
14797 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
14798 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
14799 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 14800 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
14801 | * this verifier states are not equivalent, |
14802 | * return false to continue verification of this path | |
14803 | */ | |
14804 | return false; | |
27113c59 | 14805 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 14806 | continue; |
d6fefa11 KKD |
14807 | /* Both old and cur are having same slot_type */ |
14808 | switch (old->stack[spi].slot_type[BPF_REG_SIZE - 1]) { | |
14809 | case STACK_SPILL: | |
638f5b90 AS |
14810 | /* when explored and current stack slot are both storing |
14811 | * spilled registers, check that stored pointers types | |
14812 | * are the same as well. | |
14813 | * Ex: explored safe path could have stored | |
14814 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
14815 | * but current path has stored: | |
14816 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
14817 | * such verifier states are not equivalent. | |
14818 | * return false to continue verification of this path | |
14819 | */ | |
d6fefa11 KKD |
14820 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
14821 | &cur->stack[spi].spilled_ptr, idmap)) | |
14822 | return false; | |
14823 | break; | |
14824 | case STACK_DYNPTR: | |
d6fefa11 KKD |
14825 | old_reg = &old->stack[spi].spilled_ptr; |
14826 | cur_reg = &cur->stack[spi].spilled_ptr; | |
14827 | if (old_reg->dynptr.type != cur_reg->dynptr.type || | |
14828 | old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot || | |
14829 | !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap)) | |
14830 | return false; | |
14831 | break; | |
06accc87 AN |
14832 | case STACK_ITER: |
14833 | old_reg = &old->stack[spi].spilled_ptr; | |
14834 | cur_reg = &cur->stack[spi].spilled_ptr; | |
14835 | /* iter.depth is not compared between states as it | |
14836 | * doesn't matter for correctness and would otherwise | |
14837 | * prevent convergence; we maintain it only to prevent | |
14838 | * infinite loop check triggering, see | |
14839 | * iter_active_depths_differ() | |
14840 | */ | |
14841 | if (old_reg->iter.btf != cur_reg->iter.btf || | |
14842 | old_reg->iter.btf_id != cur_reg->iter.btf_id || | |
14843 | old_reg->iter.state != cur_reg->iter.state || | |
14844 | /* ignore {old_reg,cur_reg}->iter.depth, see above */ | |
14845 | !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap)) | |
14846 | return false; | |
14847 | break; | |
d6fefa11 KKD |
14848 | case STACK_MISC: |
14849 | case STACK_ZERO: | |
14850 | case STACK_INVALID: | |
14851 | continue; | |
14852 | /* Ensure that new unhandled slot types return false by default */ | |
14853 | default: | |
638f5b90 | 14854 | return false; |
d6fefa11 | 14855 | } |
638f5b90 AS |
14856 | } |
14857 | return true; | |
14858 | } | |
14859 | ||
e8f55fcf AN |
14860 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur, |
14861 | struct bpf_id_pair *idmap) | |
fd978bf7 | 14862 | { |
e8f55fcf AN |
14863 | int i; |
14864 | ||
fd978bf7 JS |
14865 | if (old->acquired_refs != cur->acquired_refs) |
14866 | return false; | |
e8f55fcf AN |
14867 | |
14868 | for (i = 0; i < old->acquired_refs; i++) { | |
14869 | if (!check_ids(old->refs[i].id, cur->refs[i].id, idmap)) | |
14870 | return false; | |
14871 | } | |
14872 | ||
14873 | return true; | |
fd978bf7 JS |
14874 | } |
14875 | ||
f1bca824 AS |
14876 | /* compare two verifier states |
14877 | * | |
14878 | * all states stored in state_list are known to be valid, since | |
14879 | * verifier reached 'bpf_exit' instruction through them | |
14880 | * | |
14881 | * this function is called when verifier exploring different branches of | |
14882 | * execution popped from the state stack. If it sees an old state that has | |
14883 | * more strict register state and more strict stack state then this execution | |
14884 | * branch doesn't need to be explored further, since verifier already | |
14885 | * concluded that more strict state leads to valid finish. | |
14886 | * | |
14887 | * Therefore two states are equivalent if register state is more conservative | |
14888 | * and explored stack state is more conservative than the current one. | |
14889 | * Example: | |
14890 | * explored current | |
14891 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
14892 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
14893 | * | |
14894 | * In other words if current stack state (one being explored) has more | |
14895 | * valid slots than old one that already passed validation, it means | |
14896 | * the verifier can stop exploring and conclude that current state is valid too | |
14897 | * | |
14898 | * Similarly with registers. If explored state has register type as invalid | |
14899 | * whereas register type in current state is meaningful, it means that | |
14900 | * the current state will reach 'bpf_exit' instruction safely | |
14901 | */ | |
c9e73e3d | 14902 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 14903 | struct bpf_func_state *cur) |
f1bca824 AS |
14904 | { |
14905 | int i; | |
14906 | ||
c9e73e3d | 14907 | for (i = 0; i < MAX_BPF_REG; i++) |
e042aa53 DB |
14908 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
14909 | env->idmap_scratch)) | |
c9e73e3d | 14910 | return false; |
f1bca824 | 14911 | |
e042aa53 | 14912 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 14913 | return false; |
fd978bf7 | 14914 | |
e8f55fcf | 14915 | if (!refsafe(old, cur, env->idmap_scratch)) |
c9e73e3d LB |
14916 | return false; |
14917 | ||
14918 | return true; | |
f1bca824 AS |
14919 | } |
14920 | ||
f4d7e40a AS |
14921 | static bool states_equal(struct bpf_verifier_env *env, |
14922 | struct bpf_verifier_state *old, | |
14923 | struct bpf_verifier_state *cur) | |
14924 | { | |
14925 | int i; | |
14926 | ||
14927 | if (old->curframe != cur->curframe) | |
14928 | return false; | |
14929 | ||
5dd9cdbc EZ |
14930 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
14931 | ||
979d63d5 DB |
14932 | /* Verification state from speculative execution simulation |
14933 | * must never prune a non-speculative execution one. | |
14934 | */ | |
14935 | if (old->speculative && !cur->speculative) | |
14936 | return false; | |
14937 | ||
4ea2bb15 EZ |
14938 | if (old->active_lock.ptr != cur->active_lock.ptr) |
14939 | return false; | |
14940 | ||
14941 | /* Old and cur active_lock's have to be either both present | |
14942 | * or both absent. | |
14943 | */ | |
14944 | if (!!old->active_lock.id != !!cur->active_lock.id) | |
14945 | return false; | |
14946 | ||
14947 | if (old->active_lock.id && | |
14948 | !check_ids(old->active_lock.id, cur->active_lock.id, env->idmap_scratch)) | |
d83525ca AS |
14949 | return false; |
14950 | ||
9bb00b28 | 14951 | if (old->active_rcu_lock != cur->active_rcu_lock) |
d83525ca AS |
14952 | return false; |
14953 | ||
f4d7e40a AS |
14954 | /* for states to be equal callsites have to be the same |
14955 | * and all frame states need to be equivalent | |
14956 | */ | |
14957 | for (i = 0; i <= old->curframe; i++) { | |
14958 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
14959 | return false; | |
c9e73e3d | 14960 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
14961 | return false; |
14962 | } | |
14963 | return true; | |
14964 | } | |
14965 | ||
5327ed3d JW |
14966 | /* Return 0 if no propagation happened. Return negative error code if error |
14967 | * happened. Otherwise, return the propagated bit. | |
14968 | */ | |
55e7f3b5 JW |
14969 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
14970 | struct bpf_reg_state *reg, | |
14971 | struct bpf_reg_state *parent_reg) | |
14972 | { | |
5327ed3d JW |
14973 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
14974 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
14975 | int err; |
14976 | ||
5327ed3d JW |
14977 | /* When comes here, read flags of PARENT_REG or REG could be any of |
14978 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
14979 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
14980 | */ | |
14981 | if (parent_flag == REG_LIVE_READ64 || | |
14982 | /* Or if there is no read flag from REG. */ | |
14983 | !flag || | |
14984 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
14985 | parent_flag == flag) | |
55e7f3b5 JW |
14986 | return 0; |
14987 | ||
5327ed3d | 14988 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
14989 | if (err) |
14990 | return err; | |
14991 | ||
5327ed3d | 14992 | return flag; |
55e7f3b5 JW |
14993 | } |
14994 | ||
8e9cd9ce | 14995 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
14996 | * straight-line code between a state and its parent. When we arrive at an |
14997 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
14998 | * code, so read marks in the state must propagate to the parent regardless | |
14999 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 15000 | * in mark_reg_read() is for. |
8e9cd9ce | 15001 | */ |
f4d7e40a AS |
15002 | static int propagate_liveness(struct bpf_verifier_env *env, |
15003 | const struct bpf_verifier_state *vstate, | |
15004 | struct bpf_verifier_state *vparent) | |
dc503a8a | 15005 | { |
3f8cafa4 | 15006 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 15007 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 15008 | int i, frame, err = 0; |
dc503a8a | 15009 | |
f4d7e40a AS |
15010 | if (vparent->curframe != vstate->curframe) { |
15011 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
15012 | vparent->curframe, vstate->curframe); | |
15013 | return -EFAULT; | |
15014 | } | |
dc503a8a EC |
15015 | /* Propagate read liveness of registers... */ |
15016 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 15017 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
15018 | parent = vparent->frame[frame]; |
15019 | state = vstate->frame[frame]; | |
15020 | parent_reg = parent->regs; | |
15021 | state_reg = state->regs; | |
83d16312 JK |
15022 | /* We don't need to worry about FP liveness, it's read-only */ |
15023 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
15024 | err = propagate_liveness_reg(env, &state_reg[i], |
15025 | &parent_reg[i]); | |
5327ed3d | 15026 | if (err < 0) |
3f8cafa4 | 15027 | return err; |
5327ed3d JW |
15028 | if (err == REG_LIVE_READ64) |
15029 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 15030 | } |
f4d7e40a | 15031 | |
1b04aee7 | 15032 | /* Propagate stack slots. */ |
f4d7e40a AS |
15033 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
15034 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
15035 | parent_reg = &parent->stack[i].spilled_ptr; |
15036 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
15037 | err = propagate_liveness_reg(env, state_reg, |
15038 | parent_reg); | |
5327ed3d | 15039 | if (err < 0) |
3f8cafa4 | 15040 | return err; |
dc503a8a EC |
15041 | } |
15042 | } | |
5327ed3d | 15043 | return 0; |
dc503a8a EC |
15044 | } |
15045 | ||
a3ce685d AS |
15046 | /* find precise scalars in the previous equivalent state and |
15047 | * propagate them into the current state | |
15048 | */ | |
15049 | static int propagate_precision(struct bpf_verifier_env *env, | |
15050 | const struct bpf_verifier_state *old) | |
15051 | { | |
15052 | struct bpf_reg_state *state_reg; | |
15053 | struct bpf_func_state *state; | |
529409ea | 15054 | int i, err = 0, fr; |
a3ce685d | 15055 | |
529409ea AN |
15056 | for (fr = old->curframe; fr >= 0; fr--) { |
15057 | state = old->frame[fr]; | |
15058 | state_reg = state->regs; | |
15059 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
15060 | if (state_reg->type != SCALAR_VALUE || | |
52c2b005 AN |
15061 | !state_reg->precise || |
15062 | !(state_reg->live & REG_LIVE_READ)) | |
529409ea AN |
15063 | continue; |
15064 | if (env->log.level & BPF_LOG_LEVEL2) | |
15065 | verbose(env, "frame %d: propagating r%d\n", i, fr); | |
15066 | err = mark_chain_precision_frame(env, fr, i); | |
15067 | if (err < 0) | |
15068 | return err; | |
15069 | } | |
a3ce685d | 15070 | |
529409ea AN |
15071 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
15072 | if (!is_spilled_reg(&state->stack[i])) | |
15073 | continue; | |
15074 | state_reg = &state->stack[i].spilled_ptr; | |
15075 | if (state_reg->type != SCALAR_VALUE || | |
52c2b005 AN |
15076 | !state_reg->precise || |
15077 | !(state_reg->live & REG_LIVE_READ)) | |
529409ea AN |
15078 | continue; |
15079 | if (env->log.level & BPF_LOG_LEVEL2) | |
15080 | verbose(env, "frame %d: propagating fp%d\n", | |
15081 | (-i - 1) * BPF_REG_SIZE, fr); | |
15082 | err = mark_chain_precision_stack_frame(env, fr, i); | |
15083 | if (err < 0) | |
15084 | return err; | |
15085 | } | |
a3ce685d AS |
15086 | } |
15087 | return 0; | |
15088 | } | |
15089 | ||
2589726d AS |
15090 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
15091 | struct bpf_verifier_state *cur) | |
15092 | { | |
15093 | struct bpf_func_state *fold, *fcur; | |
15094 | int i, fr = cur->curframe; | |
15095 | ||
15096 | if (old->curframe != fr) | |
15097 | return false; | |
15098 | ||
15099 | fold = old->frame[fr]; | |
15100 | fcur = cur->frame[fr]; | |
15101 | for (i = 0; i < MAX_BPF_REG; i++) | |
15102 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
15103 | offsetof(struct bpf_reg_state, parent))) | |
15104 | return false; | |
15105 | return true; | |
15106 | } | |
15107 | ||
06accc87 AN |
15108 | static bool is_iter_next_insn(struct bpf_verifier_env *env, int insn_idx) |
15109 | { | |
15110 | return env->insn_aux_data[insn_idx].is_iter_next; | |
15111 | } | |
15112 | ||
15113 | /* is_state_visited() handles iter_next() (see process_iter_next_call() for | |
15114 | * terminology) calls specially: as opposed to bounded BPF loops, it *expects* | |
15115 | * states to match, which otherwise would look like an infinite loop. So while | |
15116 | * iter_next() calls are taken care of, we still need to be careful and | |
15117 | * prevent erroneous and too eager declaration of "ininite loop", when | |
15118 | * iterators are involved. | |
15119 | * | |
15120 | * Here's a situation in pseudo-BPF assembly form: | |
15121 | * | |
15122 | * 0: again: ; set up iter_next() call args | |
15123 | * 1: r1 = &it ; <CHECKPOINT HERE> | |
15124 | * 2: call bpf_iter_num_next ; this is iter_next() call | |
15125 | * 3: if r0 == 0 goto done | |
15126 | * 4: ... something useful here ... | |
15127 | * 5: goto again ; another iteration | |
15128 | * 6: done: | |
15129 | * 7: r1 = &it | |
15130 | * 8: call bpf_iter_num_destroy ; clean up iter state | |
15131 | * 9: exit | |
15132 | * | |
15133 | * This is a typical loop. Let's assume that we have a prune point at 1:, | |
15134 | * before we get to `call bpf_iter_num_next` (e.g., because of that `goto | |
15135 | * again`, assuming other heuristics don't get in a way). | |
15136 | * | |
15137 | * When we first time come to 1:, let's say we have some state X. We proceed | |
15138 | * to 2:, fork states, enqueue ACTIVE, validate NULL case successfully, exit. | |
15139 | * Now we come back to validate that forked ACTIVE state. We proceed through | |
15140 | * 3-5, come to goto, jump to 1:. Let's assume our state didn't change, so we | |
15141 | * are converging. But the problem is that we don't know that yet, as this | |
15142 | * convergence has to happen at iter_next() call site only. So if nothing is | |
15143 | * done, at 1: verifier will use bounded loop logic and declare infinite | |
15144 | * looping (and would be *technically* correct, if not for iterator's | |
15145 | * "eventual sticky NULL" contract, see process_iter_next_call()). But we | |
15146 | * don't want that. So what we do in process_iter_next_call() when we go on | |
15147 | * another ACTIVE iteration, we bump slot->iter.depth, to mark that it's | |
15148 | * a different iteration. So when we suspect an infinite loop, we additionally | |
15149 | * check if any of the *ACTIVE* iterator states depths differ. If yes, we | |
15150 | * pretend we are not looping and wait for next iter_next() call. | |
15151 | * | |
15152 | * This only applies to ACTIVE state. In DRAINED state we don't expect to | |
15153 | * loop, because that would actually mean infinite loop, as DRAINED state is | |
15154 | * "sticky", and so we'll keep returning into the same instruction with the | |
15155 | * same state (at least in one of possible code paths). | |
15156 | * | |
15157 | * This approach allows to keep infinite loop heuristic even in the face of | |
15158 | * active iterator. E.g., C snippet below is and will be detected as | |
15159 | * inifintely looping: | |
15160 | * | |
15161 | * struct bpf_iter_num it; | |
15162 | * int *p, x; | |
15163 | * | |
15164 | * bpf_iter_num_new(&it, 0, 10); | |
15165 | * while ((p = bpf_iter_num_next(&t))) { | |
15166 | * x = p; | |
15167 | * while (x--) {} // <<-- infinite loop here | |
15168 | * } | |
15169 | * | |
15170 | */ | |
15171 | static bool iter_active_depths_differ(struct bpf_verifier_state *old, struct bpf_verifier_state *cur) | |
15172 | { | |
15173 | struct bpf_reg_state *slot, *cur_slot; | |
15174 | struct bpf_func_state *state; | |
15175 | int i, fr; | |
15176 | ||
15177 | for (fr = old->curframe; fr >= 0; fr--) { | |
15178 | state = old->frame[fr]; | |
15179 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
15180 | if (state->stack[i].slot_type[0] != STACK_ITER) | |
15181 | continue; | |
15182 | ||
15183 | slot = &state->stack[i].spilled_ptr; | |
15184 | if (slot->iter.state != BPF_ITER_STATE_ACTIVE) | |
15185 | continue; | |
15186 | ||
15187 | cur_slot = &cur->frame[fr]->stack[i].spilled_ptr; | |
15188 | if (cur_slot->iter.depth != slot->iter.depth) | |
15189 | return true; | |
15190 | } | |
15191 | } | |
15192 | return false; | |
15193 | } | |
2589726d | 15194 | |
58e2af8b | 15195 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 15196 | { |
58e2af8b | 15197 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 15198 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 15199 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 15200 | int i, j, err, states_cnt = 0; |
4b5ce570 AN |
15201 | bool force_new_state = env->test_state_freq || is_force_checkpoint(env, insn_idx); |
15202 | bool add_new_state = force_new_state; | |
f1bca824 | 15203 | |
2589726d AS |
15204 | /* bpf progs typically have pruning point every 4 instructions |
15205 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
15206 | * Do not add new state for future pruning if the verifier hasn't seen | |
15207 | * at least 2 jumps and at least 8 instructions. | |
15208 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
15209 | * In tests that amounts to up to 50% reduction into total verifier | |
15210 | * memory consumption and 20% verifier time speedup. | |
15211 | */ | |
15212 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
15213 | env->insn_processed - env->prev_insn_processed >= 8) | |
15214 | add_new_state = true; | |
15215 | ||
a8f500af AS |
15216 | pprev = explored_state(env, insn_idx); |
15217 | sl = *pprev; | |
15218 | ||
9242b5f5 AS |
15219 | clean_live_states(env, insn_idx, cur); |
15220 | ||
a8f500af | 15221 | while (sl) { |
dc2a4ebc AS |
15222 | states_cnt++; |
15223 | if (sl->state.insn_idx != insn_idx) | |
15224 | goto next; | |
bfc6bb74 | 15225 | |
2589726d | 15226 | if (sl->state.branches) { |
bfc6bb74 AS |
15227 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
15228 | ||
15229 | if (frame->in_async_callback_fn && | |
15230 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
15231 | /* Different async_entry_cnt means that the verifier is | |
15232 | * processing another entry into async callback. | |
15233 | * Seeing the same state is not an indication of infinite | |
15234 | * loop or infinite recursion. | |
15235 | * But finding the same state doesn't mean that it's safe | |
15236 | * to stop processing the current state. The previous state | |
15237 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
15238 | * Checking in_async_callback_fn alone is not enough either. | |
15239 | * Since the verifier still needs to catch infinite loops | |
15240 | * inside async callbacks. | |
15241 | */ | |
06accc87 AN |
15242 | goto skip_inf_loop_check; |
15243 | } | |
15244 | /* BPF open-coded iterators loop detection is special. | |
15245 | * states_maybe_looping() logic is too simplistic in detecting | |
15246 | * states that *might* be equivalent, because it doesn't know | |
15247 | * about ID remapping, so don't even perform it. | |
15248 | * See process_iter_next_call() and iter_active_depths_differ() | |
15249 | * for overview of the logic. When current and one of parent | |
15250 | * states are detected as equivalent, it's a good thing: we prove | |
15251 | * convergence and can stop simulating further iterations. | |
15252 | * It's safe to assume that iterator loop will finish, taking into | |
15253 | * account iter_next() contract of eventually returning | |
15254 | * sticky NULL result. | |
15255 | */ | |
15256 | if (is_iter_next_insn(env, insn_idx)) { | |
15257 | if (states_equal(env, &sl->state, cur)) { | |
15258 | struct bpf_func_state *cur_frame; | |
15259 | struct bpf_reg_state *iter_state, *iter_reg; | |
15260 | int spi; | |
15261 | ||
15262 | cur_frame = cur->frame[cur->curframe]; | |
15263 | /* btf_check_iter_kfuncs() enforces that | |
15264 | * iter state pointer is always the first arg | |
15265 | */ | |
15266 | iter_reg = &cur_frame->regs[BPF_REG_1]; | |
15267 | /* current state is valid due to states_equal(), | |
15268 | * so we can assume valid iter and reg state, | |
15269 | * no need for extra (re-)validations | |
15270 | */ | |
15271 | spi = __get_spi(iter_reg->off + iter_reg->var_off.value); | |
15272 | iter_state = &func(env, iter_reg)->stack[spi].spilled_ptr; | |
15273 | if (iter_state->iter.state == BPF_ITER_STATE_ACTIVE) | |
15274 | goto hit; | |
15275 | } | |
15276 | goto skip_inf_loop_check; | |
15277 | } | |
15278 | /* attempt to detect infinite loop to avoid unnecessary doomed work */ | |
15279 | if (states_maybe_looping(&sl->state, cur) && | |
15280 | states_equal(env, &sl->state, cur) && | |
15281 | !iter_active_depths_differ(&sl->state, cur)) { | |
2589726d AS |
15282 | verbose_linfo(env, insn_idx, "; "); |
15283 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
15284 | return -EINVAL; | |
15285 | } | |
15286 | /* if the verifier is processing a loop, avoid adding new state | |
15287 | * too often, since different loop iterations have distinct | |
15288 | * states and may not help future pruning. | |
15289 | * This threshold shouldn't be too low to make sure that | |
15290 | * a loop with large bound will be rejected quickly. | |
15291 | * The most abusive loop will be: | |
15292 | * r1 += 1 | |
15293 | * if r1 < 1000000 goto pc-2 | |
15294 | * 1M insn_procssed limit / 100 == 10k peak states. | |
15295 | * This threshold shouldn't be too high either, since states | |
15296 | * at the end of the loop are likely to be useful in pruning. | |
15297 | */ | |
06accc87 | 15298 | skip_inf_loop_check: |
4b5ce570 | 15299 | if (!force_new_state && |
98ddcf38 | 15300 | env->jmps_processed - env->prev_jmps_processed < 20 && |
2589726d AS |
15301 | env->insn_processed - env->prev_insn_processed < 100) |
15302 | add_new_state = false; | |
15303 | goto miss; | |
15304 | } | |
638f5b90 | 15305 | if (states_equal(env, &sl->state, cur)) { |
06accc87 | 15306 | hit: |
9f4686c4 | 15307 | sl->hit_cnt++; |
f1bca824 | 15308 | /* reached equivalent register/stack state, |
dc503a8a EC |
15309 | * prune the search. |
15310 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
15311 | * If we have any write marks in env->cur_state, they |
15312 | * will prevent corresponding reads in the continuation | |
15313 | * from reaching our parent (an explored_state). Our | |
15314 | * own state will get the read marks recorded, but | |
15315 | * they'll be immediately forgotten as we're pruning | |
15316 | * this state and will pop a new one. | |
f1bca824 | 15317 | */ |
f4d7e40a | 15318 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
15319 | |
15320 | /* if previous state reached the exit with precision and | |
15321 | * current state is equivalent to it (except precsion marks) | |
15322 | * the precision needs to be propagated back in | |
15323 | * the current state. | |
15324 | */ | |
15325 | err = err ? : push_jmp_history(env, cur); | |
15326 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
15327 | if (err) |
15328 | return err; | |
f1bca824 | 15329 | return 1; |
dc503a8a | 15330 | } |
2589726d AS |
15331 | miss: |
15332 | /* when new state is not going to be added do not increase miss count. | |
15333 | * Otherwise several loop iterations will remove the state | |
15334 | * recorded earlier. The goal of these heuristics is to have | |
15335 | * states from some iterations of the loop (some in the beginning | |
15336 | * and some at the end) to help pruning. | |
15337 | */ | |
15338 | if (add_new_state) | |
15339 | sl->miss_cnt++; | |
9f4686c4 AS |
15340 | /* heuristic to determine whether this state is beneficial |
15341 | * to keep checking from state equivalence point of view. | |
15342 | * Higher numbers increase max_states_per_insn and verification time, | |
15343 | * but do not meaningfully decrease insn_processed. | |
15344 | */ | |
15345 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
15346 | /* the state is unlikely to be useful. Remove it to | |
15347 | * speed up verification | |
15348 | */ | |
15349 | *pprev = sl->next; | |
15350 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
15351 | u32 br = sl->state.branches; |
15352 | ||
15353 | WARN_ONCE(br, | |
15354 | "BUG live_done but branches_to_explore %d\n", | |
15355 | br); | |
9f4686c4 AS |
15356 | free_verifier_state(&sl->state, false); |
15357 | kfree(sl); | |
15358 | env->peak_states--; | |
15359 | } else { | |
15360 | /* cannot free this state, since parentage chain may | |
15361 | * walk it later. Add it for free_list instead to | |
15362 | * be freed at the end of verification | |
15363 | */ | |
15364 | sl->next = env->free_list; | |
15365 | env->free_list = sl; | |
15366 | } | |
15367 | sl = *pprev; | |
15368 | continue; | |
15369 | } | |
dc2a4ebc | 15370 | next: |
9f4686c4 AS |
15371 | pprev = &sl->next; |
15372 | sl = *pprev; | |
f1bca824 AS |
15373 | } |
15374 | ||
06ee7115 AS |
15375 | if (env->max_states_per_insn < states_cnt) |
15376 | env->max_states_per_insn = states_cnt; | |
15377 | ||
2c78ee89 | 15378 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
a095f421 | 15379 | return 0; |
ceefbc96 | 15380 | |
2589726d | 15381 | if (!add_new_state) |
a095f421 | 15382 | return 0; |
ceefbc96 | 15383 | |
2589726d AS |
15384 | /* There were no equivalent states, remember the current one. |
15385 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 15386 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 15387 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 15388 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
15389 | * again on the way to bpf_exit. |
15390 | * When looping the sl->state.branches will be > 0 and this state | |
15391 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 15392 | */ |
638f5b90 | 15393 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
15394 | if (!new_sl) |
15395 | return -ENOMEM; | |
06ee7115 AS |
15396 | env->total_states++; |
15397 | env->peak_states++; | |
2589726d AS |
15398 | env->prev_jmps_processed = env->jmps_processed; |
15399 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 | 15400 | |
7a830b53 AN |
15401 | /* forget precise markings we inherited, see __mark_chain_precision */ |
15402 | if (env->bpf_capable) | |
15403 | mark_all_scalars_imprecise(env, cur); | |
15404 | ||
f1bca824 | 15405 | /* add new state to the head of linked list */ |
679c782d EC |
15406 | new = &new_sl->state; |
15407 | err = copy_verifier_state(new, cur); | |
1969db47 | 15408 | if (err) { |
679c782d | 15409 | free_verifier_state(new, false); |
1969db47 AS |
15410 | kfree(new_sl); |
15411 | return err; | |
15412 | } | |
dc2a4ebc | 15413 | new->insn_idx = insn_idx; |
2589726d AS |
15414 | WARN_ONCE(new->branches != 1, |
15415 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 15416 | |
2589726d | 15417 | cur->parent = new; |
b5dc0163 AS |
15418 | cur->first_insn_idx = insn_idx; |
15419 | clear_jmp_history(cur); | |
5d839021 AS |
15420 | new_sl->next = *explored_state(env, insn_idx); |
15421 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
15422 | /* connect new state to parentage chain. Current frame needs all |
15423 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
15424 | * to the stack implicitly by JITs) so in callers' frames connect just | |
15425 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
15426 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
15427 | * from callee with its full parentage chain, anyway. | |
15428 | */ | |
8e9cd9ce EC |
15429 | /* clear write marks in current state: the writes we did are not writes |
15430 | * our child did, so they don't screen off its reads from us. | |
15431 | * (There are no read marks in current state, because reads always mark | |
15432 | * their parent and current state never has children yet. Only | |
15433 | * explored_states can get read marks.) | |
15434 | */ | |
eea1c227 AS |
15435 | for (j = 0; j <= cur->curframe; j++) { |
15436 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
15437 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
15438 | for (i = 0; i < BPF_REG_FP; i++) | |
15439 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
15440 | } | |
f4d7e40a AS |
15441 | |
15442 | /* all stack frames are accessible from callee, clear them all */ | |
15443 | for (j = 0; j <= cur->curframe; j++) { | |
15444 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 15445 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 15446 | |
679c782d | 15447 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 15448 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
15449 | frame->stack[i].spilled_ptr.parent = |
15450 | &newframe->stack[i].spilled_ptr; | |
15451 | } | |
f4d7e40a | 15452 | } |
f1bca824 AS |
15453 | return 0; |
15454 | } | |
15455 | ||
c64b7983 JS |
15456 | /* Return true if it's OK to have the same insn return a different type. */ |
15457 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
15458 | { | |
c25b2ae1 | 15459 | switch (base_type(type)) { |
c64b7983 JS |
15460 | case PTR_TO_CTX: |
15461 | case PTR_TO_SOCKET: | |
46f8bc92 | 15462 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 15463 | case PTR_TO_TCP_SOCK: |
fada7fdc | 15464 | case PTR_TO_XDP_SOCK: |
2a02759e | 15465 | case PTR_TO_BTF_ID: |
c64b7983 JS |
15466 | return false; |
15467 | default: | |
15468 | return true; | |
15469 | } | |
15470 | } | |
15471 | ||
15472 | /* If an instruction was previously used with particular pointer types, then we | |
15473 | * need to be careful to avoid cases such as the below, where it may be ok | |
15474 | * for one branch accessing the pointer, but not ok for the other branch: | |
15475 | * | |
15476 | * R1 = sock_ptr | |
15477 | * goto X; | |
15478 | * ... | |
15479 | * R1 = some_other_valid_ptr; | |
15480 | * goto X; | |
15481 | * ... | |
15482 | * R2 = *(u32 *)(R1 + 0); | |
15483 | */ | |
15484 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
15485 | { | |
15486 | return src != prev && (!reg_type_mismatch_ok(src) || | |
15487 | !reg_type_mismatch_ok(prev)); | |
15488 | } | |
15489 | ||
0d80a619 EZ |
15490 | static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type, |
15491 | bool allow_trust_missmatch) | |
15492 | { | |
15493 | enum bpf_reg_type *prev_type = &env->insn_aux_data[env->insn_idx].ptr_type; | |
15494 | ||
15495 | if (*prev_type == NOT_INIT) { | |
15496 | /* Saw a valid insn | |
15497 | * dst_reg = *(u32 *)(src_reg + off) | |
15498 | * save type to validate intersecting paths | |
15499 | */ | |
15500 | *prev_type = type; | |
15501 | } else if (reg_type_mismatch(type, *prev_type)) { | |
15502 | /* Abuser program is trying to use the same insn | |
15503 | * dst_reg = *(u32*) (src_reg + off) | |
15504 | * with different pointer types: | |
15505 | * src_reg == ctx in one branch and | |
15506 | * src_reg == stack|map in some other branch. | |
15507 | * Reject it. | |
15508 | */ | |
15509 | if (allow_trust_missmatch && | |
15510 | base_type(type) == PTR_TO_BTF_ID && | |
15511 | base_type(*prev_type) == PTR_TO_BTF_ID) { | |
15512 | /* | |
15513 | * Have to support a use case when one path through | |
15514 | * the program yields TRUSTED pointer while another | |
15515 | * is UNTRUSTED. Fallback to UNTRUSTED to generate | |
15516 | * BPF_PROBE_MEM. | |
15517 | */ | |
15518 | *prev_type = PTR_TO_BTF_ID | PTR_UNTRUSTED; | |
15519 | } else { | |
15520 | verbose(env, "same insn cannot be used with different pointers\n"); | |
15521 | return -EINVAL; | |
15522 | } | |
15523 | } | |
15524 | ||
15525 | return 0; | |
15526 | } | |
15527 | ||
58e2af8b | 15528 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 15529 | { |
6f8a57cc | 15530 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 15531 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 15532 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 15533 | struct bpf_reg_state *regs; |
06ee7115 | 15534 | int insn_cnt = env->prog->len; |
17a52670 | 15535 | bool do_print_state = false; |
b5dc0163 | 15536 | int prev_insn_idx = -1; |
17a52670 | 15537 | |
17a52670 AS |
15538 | for (;;) { |
15539 | struct bpf_insn *insn; | |
15540 | u8 class; | |
15541 | int err; | |
15542 | ||
b5dc0163 | 15543 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 15544 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 15545 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 15546 | env->insn_idx, insn_cnt); |
17a52670 AS |
15547 | return -EFAULT; |
15548 | } | |
15549 | ||
c08435ec | 15550 | insn = &insns[env->insn_idx]; |
17a52670 AS |
15551 | class = BPF_CLASS(insn->code); |
15552 | ||
06ee7115 | 15553 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
15554 | verbose(env, |
15555 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 15556 | env->insn_processed); |
17a52670 AS |
15557 | return -E2BIG; |
15558 | } | |
15559 | ||
a095f421 AN |
15560 | state->last_insn_idx = env->prev_insn_idx; |
15561 | ||
15562 | if (is_prune_point(env, env->insn_idx)) { | |
15563 | err = is_state_visited(env, env->insn_idx); | |
15564 | if (err < 0) | |
15565 | return err; | |
15566 | if (err == 1) { | |
15567 | /* found equivalent state, can prune the search */ | |
15568 | if (env->log.level & BPF_LOG_LEVEL) { | |
15569 | if (do_print_state) | |
15570 | verbose(env, "\nfrom %d to %d%s: safe\n", | |
15571 | env->prev_insn_idx, env->insn_idx, | |
15572 | env->cur_state->speculative ? | |
15573 | " (speculative execution)" : ""); | |
15574 | else | |
15575 | verbose(env, "%d: safe\n", env->insn_idx); | |
15576 | } | |
15577 | goto process_bpf_exit; | |
f1bca824 | 15578 | } |
a095f421 AN |
15579 | } |
15580 | ||
15581 | if (is_jmp_point(env, env->insn_idx)) { | |
15582 | err = push_jmp_history(env, state); | |
15583 | if (err) | |
15584 | return err; | |
f1bca824 AS |
15585 | } |
15586 | ||
c3494801 AS |
15587 | if (signal_pending(current)) |
15588 | return -EAGAIN; | |
15589 | ||
3c2ce60b DB |
15590 | if (need_resched()) |
15591 | cond_resched(); | |
15592 | ||
2e576648 CL |
15593 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
15594 | verbose(env, "\nfrom %d to %d%s:", | |
15595 | env->prev_insn_idx, env->insn_idx, | |
15596 | env->cur_state->speculative ? | |
15597 | " (speculative execution)" : ""); | |
15598 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
15599 | do_print_state = false; |
15600 | } | |
15601 | ||
06ee7115 | 15602 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 15603 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 15604 | .cb_call = disasm_kfunc_name, |
7105e828 | 15605 | .cb_print = verbose, |
abe08840 | 15606 | .private_data = env, |
7105e828 DB |
15607 | }; |
15608 | ||
2e576648 CL |
15609 | if (verifier_state_scratched(env)) |
15610 | print_insn_state(env, state->frame[state->curframe]); | |
15611 | ||
c08435ec | 15612 | verbose_linfo(env, env->insn_idx, "; "); |
2e576648 | 15613 | env->prev_log_len = env->log.len_used; |
c08435ec | 15614 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 15615 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
2e576648 CL |
15616 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
15617 | env->prev_log_len = env->log.len_used; | |
17a52670 AS |
15618 | } |
15619 | ||
9d03ebc7 | 15620 | if (bpf_prog_is_offloaded(env->prog->aux)) { |
c08435ec DB |
15621 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
15622 | env->prev_insn_idx); | |
cae1927c JK |
15623 | if (err) |
15624 | return err; | |
15625 | } | |
13a27dfc | 15626 | |
638f5b90 | 15627 | regs = cur_regs(env); |
fe9a5ca7 | 15628 | sanitize_mark_insn_seen(env); |
b5dc0163 | 15629 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 15630 | |
17a52670 | 15631 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 15632 | err = check_alu_op(env, insn); |
17a52670 AS |
15633 | if (err) |
15634 | return err; | |
15635 | ||
15636 | } else if (class == BPF_LDX) { | |
0d80a619 | 15637 | enum bpf_reg_type src_reg_type; |
9bac3d6d AS |
15638 | |
15639 | /* check for reserved fields is already done */ | |
15640 | ||
17a52670 | 15641 | /* check src operand */ |
dc503a8a | 15642 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
15643 | if (err) |
15644 | return err; | |
15645 | ||
dc503a8a | 15646 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
15647 | if (err) |
15648 | return err; | |
15649 | ||
725f9dcd AS |
15650 | src_reg_type = regs[insn->src_reg].type; |
15651 | ||
17a52670 AS |
15652 | /* check that memory (src_reg + off) is readable, |
15653 | * the state of dst_reg will be updated by this func | |
15654 | */ | |
c08435ec DB |
15655 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
15656 | insn->off, BPF_SIZE(insn->code), | |
15657 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
15658 | if (err) |
15659 | return err; | |
15660 | ||
0d80a619 EZ |
15661 | err = save_aux_ptr_type(env, src_reg_type, true); |
15662 | if (err) | |
15663 | return err; | |
17a52670 | 15664 | } else if (class == BPF_STX) { |
0d80a619 | 15665 | enum bpf_reg_type dst_reg_type; |
d691f9e8 | 15666 | |
91c960b0 BJ |
15667 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
15668 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
15669 | if (err) |
15670 | return err; | |
c08435ec | 15671 | env->insn_idx++; |
17a52670 AS |
15672 | continue; |
15673 | } | |
15674 | ||
5ca419f2 BJ |
15675 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
15676 | verbose(env, "BPF_STX uses reserved fields\n"); | |
15677 | return -EINVAL; | |
15678 | } | |
15679 | ||
17a52670 | 15680 | /* check src1 operand */ |
dc503a8a | 15681 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
15682 | if (err) |
15683 | return err; | |
15684 | /* check src2 operand */ | |
dc503a8a | 15685 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
15686 | if (err) |
15687 | return err; | |
15688 | ||
d691f9e8 AS |
15689 | dst_reg_type = regs[insn->dst_reg].type; |
15690 | ||
17a52670 | 15691 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
15692 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
15693 | insn->off, BPF_SIZE(insn->code), | |
15694 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
15695 | if (err) |
15696 | return err; | |
15697 | ||
0d80a619 EZ |
15698 | err = save_aux_ptr_type(env, dst_reg_type, false); |
15699 | if (err) | |
15700 | return err; | |
17a52670 | 15701 | } else if (class == BPF_ST) { |
0d80a619 EZ |
15702 | enum bpf_reg_type dst_reg_type; |
15703 | ||
17a52670 AS |
15704 | if (BPF_MODE(insn->code) != BPF_MEM || |
15705 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 15706 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
15707 | return -EINVAL; |
15708 | } | |
15709 | /* check src operand */ | |
dc503a8a | 15710 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
15711 | if (err) |
15712 | return err; | |
15713 | ||
0d80a619 | 15714 | dst_reg_type = regs[insn->dst_reg].type; |
f37a8cb8 | 15715 | |
17a52670 | 15716 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
15717 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
15718 | insn->off, BPF_SIZE(insn->code), | |
15719 | BPF_WRITE, -1, false); | |
17a52670 AS |
15720 | if (err) |
15721 | return err; | |
15722 | ||
0d80a619 EZ |
15723 | err = save_aux_ptr_type(env, dst_reg_type, false); |
15724 | if (err) | |
15725 | return err; | |
092ed096 | 15726 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
15727 | u8 opcode = BPF_OP(insn->code); |
15728 | ||
2589726d | 15729 | env->jmps_processed++; |
17a52670 AS |
15730 | if (opcode == BPF_CALL) { |
15731 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
15732 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
15733 | && insn->off != 0) || | |
f4d7e40a | 15734 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
15735 | insn->src_reg != BPF_PSEUDO_CALL && |
15736 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
15737 | insn->dst_reg != BPF_REG_0 || |
15738 | class == BPF_JMP32) { | |
61bd5218 | 15739 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
15740 | return -EINVAL; |
15741 | } | |
15742 | ||
8cab76ec KKD |
15743 | if (env->cur_state->active_lock.ptr) { |
15744 | if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) || | |
15745 | (insn->src_reg == BPF_PSEUDO_CALL) || | |
15746 | (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && | |
cd6791b4 | 15747 | (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) { |
8cab76ec KKD |
15748 | verbose(env, "function calls are not allowed while holding a lock\n"); |
15749 | return -EINVAL; | |
15750 | } | |
d83525ca | 15751 | } |
f4d7e40a | 15752 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 15753 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 | 15754 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
5c073f26 | 15755 | err = check_kfunc_call(env, insn, &env->insn_idx); |
f4d7e40a | 15756 | else |
69c087ba | 15757 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
15758 | if (err) |
15759 | return err; | |
553a64a8 AN |
15760 | |
15761 | mark_reg_scratched(env, BPF_REG_0); | |
17a52670 AS |
15762 | } else if (opcode == BPF_JA) { |
15763 | if (BPF_SRC(insn->code) != BPF_K || | |
15764 | insn->imm != 0 || | |
15765 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
15766 | insn->dst_reg != BPF_REG_0 || |
15767 | class == BPF_JMP32) { | |
61bd5218 | 15768 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
15769 | return -EINVAL; |
15770 | } | |
15771 | ||
c08435ec | 15772 | env->insn_idx += insn->off + 1; |
17a52670 AS |
15773 | continue; |
15774 | ||
15775 | } else if (opcode == BPF_EXIT) { | |
15776 | if (BPF_SRC(insn->code) != BPF_K || | |
15777 | insn->imm != 0 || | |
15778 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
15779 | insn->dst_reg != BPF_REG_0 || |
15780 | class == BPF_JMP32) { | |
61bd5218 | 15781 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
15782 | return -EINVAL; |
15783 | } | |
15784 | ||
5d92ddc3 DM |
15785 | if (env->cur_state->active_lock.ptr && |
15786 | !in_rbtree_lock_required_cb(env)) { | |
d83525ca AS |
15787 | verbose(env, "bpf_spin_unlock is missing\n"); |
15788 | return -EINVAL; | |
15789 | } | |
15790 | ||
9bb00b28 YS |
15791 | if (env->cur_state->active_rcu_lock) { |
15792 | verbose(env, "bpf_rcu_read_unlock is missing\n"); | |
15793 | return -EINVAL; | |
15794 | } | |
15795 | ||
9d9d00ac KKD |
15796 | /* We must do check_reference_leak here before |
15797 | * prepare_func_exit to handle the case when | |
15798 | * state->curframe > 0, it may be a callback | |
15799 | * function, for which reference_state must | |
15800 | * match caller reference state when it exits. | |
15801 | */ | |
15802 | err = check_reference_leak(env); | |
15803 | if (err) | |
15804 | return err; | |
15805 | ||
f4d7e40a AS |
15806 | if (state->curframe) { |
15807 | /* exit from nested function */ | |
c08435ec | 15808 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
15809 | if (err) |
15810 | return err; | |
15811 | do_print_state = true; | |
15812 | continue; | |
15813 | } | |
15814 | ||
390ee7e2 AS |
15815 | err = check_return_code(env); |
15816 | if (err) | |
15817 | return err; | |
f1bca824 | 15818 | process_bpf_exit: |
0f55f9ed | 15819 | mark_verifier_state_scratched(env); |
2589726d | 15820 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 15821 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 15822 | &env->insn_idx, pop_log); |
638f5b90 AS |
15823 | if (err < 0) { |
15824 | if (err != -ENOENT) | |
15825 | return err; | |
17a52670 AS |
15826 | break; |
15827 | } else { | |
15828 | do_print_state = true; | |
15829 | continue; | |
15830 | } | |
15831 | } else { | |
c08435ec | 15832 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
15833 | if (err) |
15834 | return err; | |
15835 | } | |
15836 | } else if (class == BPF_LD) { | |
15837 | u8 mode = BPF_MODE(insn->code); | |
15838 | ||
15839 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
15840 | err = check_ld_abs(env, insn); |
15841 | if (err) | |
15842 | return err; | |
15843 | ||
17a52670 AS |
15844 | } else if (mode == BPF_IMM) { |
15845 | err = check_ld_imm(env, insn); | |
15846 | if (err) | |
15847 | return err; | |
15848 | ||
c08435ec | 15849 | env->insn_idx++; |
fe9a5ca7 | 15850 | sanitize_mark_insn_seen(env); |
17a52670 | 15851 | } else { |
61bd5218 | 15852 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
15853 | return -EINVAL; |
15854 | } | |
15855 | } else { | |
61bd5218 | 15856 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
15857 | return -EINVAL; |
15858 | } | |
15859 | ||
c08435ec | 15860 | env->insn_idx++; |
17a52670 AS |
15861 | } |
15862 | ||
15863 | return 0; | |
15864 | } | |
15865 | ||
541c3bad AN |
15866 | static int find_btf_percpu_datasec(struct btf *btf) |
15867 | { | |
15868 | const struct btf_type *t; | |
15869 | const char *tname; | |
15870 | int i, n; | |
15871 | ||
15872 | /* | |
15873 | * Both vmlinux and module each have their own ".data..percpu" | |
15874 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
15875 | * types to look at only module's own BTF types. | |
15876 | */ | |
15877 | n = btf_nr_types(btf); | |
15878 | if (btf_is_module(btf)) | |
15879 | i = btf_nr_types(btf_vmlinux); | |
15880 | else | |
15881 | i = 1; | |
15882 | ||
15883 | for(; i < n; i++) { | |
15884 | t = btf_type_by_id(btf, i); | |
15885 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
15886 | continue; | |
15887 | ||
15888 | tname = btf_name_by_offset(btf, t->name_off); | |
15889 | if (!strcmp(tname, ".data..percpu")) | |
15890 | return i; | |
15891 | } | |
15892 | ||
15893 | return -ENOENT; | |
15894 | } | |
15895 | ||
4976b718 HL |
15896 | /* replace pseudo btf_id with kernel symbol address */ |
15897 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
15898 | struct bpf_insn *insn, | |
15899 | struct bpf_insn_aux_data *aux) | |
15900 | { | |
eaa6bcb7 HL |
15901 | const struct btf_var_secinfo *vsi; |
15902 | const struct btf_type *datasec; | |
541c3bad | 15903 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
15904 | const struct btf_type *t; |
15905 | const char *sym_name; | |
eaa6bcb7 | 15906 | bool percpu = false; |
f16e6313 | 15907 | u32 type, id = insn->imm; |
541c3bad | 15908 | struct btf *btf; |
f16e6313 | 15909 | s32 datasec_id; |
4976b718 | 15910 | u64 addr; |
541c3bad | 15911 | int i, btf_fd, err; |
4976b718 | 15912 | |
541c3bad AN |
15913 | btf_fd = insn[1].imm; |
15914 | if (btf_fd) { | |
15915 | btf = btf_get_by_fd(btf_fd); | |
15916 | if (IS_ERR(btf)) { | |
15917 | verbose(env, "invalid module BTF object FD specified.\n"); | |
15918 | return -EINVAL; | |
15919 | } | |
15920 | } else { | |
15921 | if (!btf_vmlinux) { | |
15922 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
15923 | return -EINVAL; | |
15924 | } | |
15925 | btf = btf_vmlinux; | |
15926 | btf_get(btf); | |
4976b718 HL |
15927 | } |
15928 | ||
541c3bad | 15929 | t = btf_type_by_id(btf, id); |
4976b718 HL |
15930 | if (!t) { |
15931 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
15932 | err = -ENOENT; |
15933 | goto err_put; | |
4976b718 HL |
15934 | } |
15935 | ||
15936 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
15937 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
15938 | err = -EINVAL; | |
15939 | goto err_put; | |
4976b718 HL |
15940 | } |
15941 | ||
541c3bad | 15942 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
15943 | addr = kallsyms_lookup_name(sym_name); |
15944 | if (!addr) { | |
15945 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
15946 | sym_name); | |
541c3bad AN |
15947 | err = -ENOENT; |
15948 | goto err_put; | |
4976b718 HL |
15949 | } |
15950 | ||
541c3bad | 15951 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 15952 | if (datasec_id > 0) { |
541c3bad | 15953 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
15954 | for_each_vsi(i, datasec, vsi) { |
15955 | if (vsi->type == id) { | |
15956 | percpu = true; | |
15957 | break; | |
15958 | } | |
15959 | } | |
15960 | } | |
15961 | ||
4976b718 HL |
15962 | insn[0].imm = (u32)addr; |
15963 | insn[1].imm = addr >> 32; | |
15964 | ||
15965 | type = t->type; | |
541c3bad | 15966 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 | 15967 | if (percpu) { |
5844101a | 15968 | aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; |
541c3bad | 15969 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
15970 | aux->btf_var.btf_id = type; |
15971 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
15972 | const struct btf_type *ret; |
15973 | const char *tname; | |
15974 | u32 tsize; | |
15975 | ||
15976 | /* resolve the type size of ksym. */ | |
541c3bad | 15977 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 15978 | if (IS_ERR(ret)) { |
541c3bad | 15979 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
15980 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
15981 | tname, PTR_ERR(ret)); | |
541c3bad AN |
15982 | err = -EINVAL; |
15983 | goto err_put; | |
4976b718 | 15984 | } |
34d3a78c | 15985 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
4976b718 HL |
15986 | aux->btf_var.mem_size = tsize; |
15987 | } else { | |
15988 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 15989 | aux->btf_var.btf = btf; |
4976b718 HL |
15990 | aux->btf_var.btf_id = type; |
15991 | } | |
541c3bad AN |
15992 | |
15993 | /* check whether we recorded this BTF (and maybe module) already */ | |
15994 | for (i = 0; i < env->used_btf_cnt; i++) { | |
15995 | if (env->used_btfs[i].btf == btf) { | |
15996 | btf_put(btf); | |
15997 | return 0; | |
15998 | } | |
15999 | } | |
16000 | ||
16001 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
16002 | err = -E2BIG; | |
16003 | goto err_put; | |
16004 | } | |
16005 | ||
16006 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
16007 | btf_mod->btf = btf; | |
16008 | btf_mod->module = NULL; | |
16009 | ||
16010 | /* if we reference variables from kernel module, bump its refcount */ | |
16011 | if (btf_is_module(btf)) { | |
16012 | btf_mod->module = btf_try_get_module(btf); | |
16013 | if (!btf_mod->module) { | |
16014 | err = -ENXIO; | |
16015 | goto err_put; | |
16016 | } | |
16017 | } | |
16018 | ||
16019 | env->used_btf_cnt++; | |
16020 | ||
4976b718 | 16021 | return 0; |
541c3bad AN |
16022 | err_put: |
16023 | btf_put(btf); | |
16024 | return err; | |
4976b718 HL |
16025 | } |
16026 | ||
d83525ca AS |
16027 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
16028 | { | |
16029 | switch (type) { | |
16030 | case BPF_PROG_TYPE_KPROBE: | |
16031 | case BPF_PROG_TYPE_TRACEPOINT: | |
16032 | case BPF_PROG_TYPE_PERF_EVENT: | |
16033 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
5002615a | 16034 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
d83525ca AS |
16035 | return true; |
16036 | default: | |
16037 | return false; | |
16038 | } | |
16039 | } | |
16040 | ||
61bd5218 JK |
16041 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
16042 | struct bpf_map *map, | |
fdc15d38 AS |
16043 | struct bpf_prog *prog) |
16044 | ||
16045 | { | |
7e40781c | 16046 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
a3884572 | 16047 | |
9c395c1b DM |
16048 | if (btf_record_has_field(map->record, BPF_LIST_HEAD) || |
16049 | btf_record_has_field(map->record, BPF_RB_ROOT)) { | |
f0c5941f | 16050 | if (is_tracing_prog_type(prog_type)) { |
9c395c1b | 16051 | verbose(env, "tracing progs cannot use bpf_{list_head,rb_root} yet\n"); |
f0c5941f KKD |
16052 | return -EINVAL; |
16053 | } | |
16054 | } | |
16055 | ||
db559117 | 16056 | if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) { |
9e7a4d98 KS |
16057 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
16058 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
16059 | return -EINVAL; | |
16060 | } | |
16061 | ||
16062 | if (is_tracing_prog_type(prog_type)) { | |
16063 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
16064 | return -EINVAL; | |
16065 | } | |
16066 | ||
16067 | if (prog->aux->sleepable) { | |
16068 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
16069 | return -EINVAL; | |
16070 | } | |
d83525ca AS |
16071 | } |
16072 | ||
db559117 | 16073 | if (btf_record_has_field(map->record, BPF_TIMER)) { |
5e0bc308 DB |
16074 | if (is_tracing_prog_type(prog_type)) { |
16075 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
16076 | return -EINVAL; | |
16077 | } | |
16078 | } | |
16079 | ||
9d03ebc7 | 16080 | if ((bpf_prog_is_offloaded(prog->aux) || bpf_map_is_offloaded(map)) && |
09728266 | 16081 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
16082 | verbose(env, "offload device mismatch between prog and map\n"); |
16083 | return -EINVAL; | |
16084 | } | |
16085 | ||
85d33df3 MKL |
16086 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
16087 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
16088 | return -EINVAL; | |
16089 | } | |
16090 | ||
1e6c62a8 AS |
16091 | if (prog->aux->sleepable) |
16092 | switch (map->map_type) { | |
16093 | case BPF_MAP_TYPE_HASH: | |
16094 | case BPF_MAP_TYPE_LRU_HASH: | |
16095 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
16096 | case BPF_MAP_TYPE_PERCPU_HASH: |
16097 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
16098 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
16099 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
16100 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
ba90c2cc | 16101 | case BPF_MAP_TYPE_RINGBUF: |
583c1f42 | 16102 | case BPF_MAP_TYPE_USER_RINGBUF: |
0fe4b381 KS |
16103 | case BPF_MAP_TYPE_INODE_STORAGE: |
16104 | case BPF_MAP_TYPE_SK_STORAGE: | |
16105 | case BPF_MAP_TYPE_TASK_STORAGE: | |
2c40d97d | 16106 | case BPF_MAP_TYPE_CGRP_STORAGE: |
ba90c2cc | 16107 | break; |
1e6c62a8 AS |
16108 | default: |
16109 | verbose(env, | |
2c40d97d | 16110 | "Sleepable programs can only use array, hash, ringbuf and local storage maps\n"); |
1e6c62a8 AS |
16111 | return -EINVAL; |
16112 | } | |
16113 | ||
fdc15d38 AS |
16114 | return 0; |
16115 | } | |
16116 | ||
b741f163 RG |
16117 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
16118 | { | |
16119 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
16120 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
16121 | } | |
16122 | ||
4976b718 HL |
16123 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
16124 | * | |
16125 | * 1. if it accesses map FD, replace it with actual map pointer. | |
16126 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
16127 | * | |
16128 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 16129 | */ |
4976b718 | 16130 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
16131 | { |
16132 | struct bpf_insn *insn = env->prog->insnsi; | |
16133 | int insn_cnt = env->prog->len; | |
fdc15d38 | 16134 | int i, j, err; |
0246e64d | 16135 | |
f1f7714e | 16136 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
16137 | if (err) |
16138 | return err; | |
16139 | ||
0246e64d | 16140 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 16141 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 16142 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 16143 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
16144 | return -EINVAL; |
16145 | } | |
16146 | ||
0246e64d | 16147 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 16148 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
16149 | struct bpf_map *map; |
16150 | struct fd f; | |
d8eca5bb | 16151 | u64 addr; |
387544bf | 16152 | u32 fd; |
0246e64d AS |
16153 | |
16154 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
16155 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
16156 | insn[1].off != 0) { | |
61bd5218 | 16157 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
16158 | return -EINVAL; |
16159 | } | |
16160 | ||
d8eca5bb | 16161 | if (insn[0].src_reg == 0) |
0246e64d AS |
16162 | /* valid generic load 64-bit imm */ |
16163 | goto next_insn; | |
16164 | ||
4976b718 HL |
16165 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
16166 | aux = &env->insn_aux_data[i]; | |
16167 | err = check_pseudo_btf_id(env, insn, aux); | |
16168 | if (err) | |
16169 | return err; | |
16170 | goto next_insn; | |
16171 | } | |
16172 | ||
69c087ba YS |
16173 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
16174 | aux = &env->insn_aux_data[i]; | |
16175 | aux->ptr_type = PTR_TO_FUNC; | |
16176 | goto next_insn; | |
16177 | } | |
16178 | ||
d8eca5bb DB |
16179 | /* In final convert_pseudo_ld_imm64() step, this is |
16180 | * converted into regular 64-bit imm load insn. | |
16181 | */ | |
387544bf AS |
16182 | switch (insn[0].src_reg) { |
16183 | case BPF_PSEUDO_MAP_VALUE: | |
16184 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
16185 | break; | |
16186 | case BPF_PSEUDO_MAP_FD: | |
16187 | case BPF_PSEUDO_MAP_IDX: | |
16188 | if (insn[1].imm == 0) | |
16189 | break; | |
16190 | fallthrough; | |
16191 | default: | |
16192 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
16193 | return -EINVAL; |
16194 | } | |
16195 | ||
387544bf AS |
16196 | switch (insn[0].src_reg) { |
16197 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
16198 | case BPF_PSEUDO_MAP_IDX: | |
16199 | if (bpfptr_is_null(env->fd_array)) { | |
16200 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
16201 | return -EPROTO; | |
16202 | } | |
16203 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
16204 | insn[0].imm * sizeof(fd), | |
16205 | sizeof(fd))) | |
16206 | return -EFAULT; | |
16207 | break; | |
16208 | default: | |
16209 | fd = insn[0].imm; | |
16210 | break; | |
16211 | } | |
16212 | ||
16213 | f = fdget(fd); | |
c2101297 | 16214 | map = __bpf_map_get(f); |
0246e64d | 16215 | if (IS_ERR(map)) { |
61bd5218 | 16216 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 16217 | insn[0].imm); |
0246e64d AS |
16218 | return PTR_ERR(map); |
16219 | } | |
16220 | ||
61bd5218 | 16221 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
16222 | if (err) { |
16223 | fdput(f); | |
16224 | return err; | |
16225 | } | |
16226 | ||
d8eca5bb | 16227 | aux = &env->insn_aux_data[i]; |
387544bf AS |
16228 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
16229 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
16230 | addr = (unsigned long)map; |
16231 | } else { | |
16232 | u32 off = insn[1].imm; | |
16233 | ||
16234 | if (off >= BPF_MAX_VAR_OFF) { | |
16235 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
16236 | fdput(f); | |
16237 | return -EINVAL; | |
16238 | } | |
16239 | ||
16240 | if (!map->ops->map_direct_value_addr) { | |
16241 | verbose(env, "no direct value access support for this map type\n"); | |
16242 | fdput(f); | |
16243 | return -EINVAL; | |
16244 | } | |
16245 | ||
16246 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
16247 | if (err) { | |
16248 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
16249 | map->value_size, off); | |
16250 | fdput(f); | |
16251 | return err; | |
16252 | } | |
16253 | ||
16254 | aux->map_off = off; | |
16255 | addr += off; | |
16256 | } | |
16257 | ||
16258 | insn[0].imm = (u32)addr; | |
16259 | insn[1].imm = addr >> 32; | |
0246e64d AS |
16260 | |
16261 | /* check whether we recorded this map already */ | |
d8eca5bb | 16262 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 16263 | if (env->used_maps[j] == map) { |
d8eca5bb | 16264 | aux->map_index = j; |
0246e64d AS |
16265 | fdput(f); |
16266 | goto next_insn; | |
16267 | } | |
d8eca5bb | 16268 | } |
0246e64d AS |
16269 | |
16270 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
16271 | fdput(f); | |
16272 | return -E2BIG; | |
16273 | } | |
16274 | ||
0246e64d AS |
16275 | /* hold the map. If the program is rejected by verifier, |
16276 | * the map will be released by release_maps() or it | |
16277 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 16278 | * and all maps are released in free_used_maps() |
0246e64d | 16279 | */ |
1e0bd5a0 | 16280 | bpf_map_inc(map); |
d8eca5bb DB |
16281 | |
16282 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
16283 | env->used_maps[env->used_map_cnt++] = map; |
16284 | ||
b741f163 | 16285 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 16286 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 16287 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
16288 | fdput(f); |
16289 | return -EBUSY; | |
16290 | } | |
16291 | ||
0246e64d AS |
16292 | fdput(f); |
16293 | next_insn: | |
16294 | insn++; | |
16295 | i++; | |
5e581dad DB |
16296 | continue; |
16297 | } | |
16298 | ||
16299 | /* Basic sanity check before we invest more work here. */ | |
16300 | if (!bpf_opcode_in_insntable(insn->code)) { | |
16301 | verbose(env, "unknown opcode %02x\n", insn->code); | |
16302 | return -EINVAL; | |
0246e64d AS |
16303 | } |
16304 | } | |
16305 | ||
16306 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
16307 | * 'struct bpf_map *' into a register instead of user map_fd. | |
16308 | * These pointers will be used later by verifier to validate map access. | |
16309 | */ | |
16310 | return 0; | |
16311 | } | |
16312 | ||
16313 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 16314 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 16315 | { |
a2ea0746 DB |
16316 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
16317 | env->used_map_cnt); | |
0246e64d AS |
16318 | } |
16319 | ||
541c3bad AN |
16320 | /* drop refcnt of maps used by the rejected program */ |
16321 | static void release_btfs(struct bpf_verifier_env *env) | |
16322 | { | |
16323 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
16324 | env->used_btf_cnt); | |
16325 | } | |
16326 | ||
0246e64d | 16327 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 16328 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
16329 | { |
16330 | struct bpf_insn *insn = env->prog->insnsi; | |
16331 | int insn_cnt = env->prog->len; | |
16332 | int i; | |
16333 | ||
69c087ba YS |
16334 | for (i = 0; i < insn_cnt; i++, insn++) { |
16335 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
16336 | continue; | |
16337 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
16338 | continue; | |
16339 | insn->src_reg = 0; | |
16340 | } | |
0246e64d AS |
16341 | } |
16342 | ||
8041902d AS |
16343 | /* single env->prog->insni[off] instruction was replaced with the range |
16344 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
16345 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
16346 | */ | |
75f0fc7b HF |
16347 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
16348 | struct bpf_insn_aux_data *new_data, | |
16349 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 16350 | { |
75f0fc7b | 16351 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 16352 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 16353 | u32 old_seen = old_data[off].seen; |
b325fbca | 16354 | u32 prog_len; |
c131187d | 16355 | int i; |
8041902d | 16356 | |
b325fbca JW |
16357 | /* aux info at OFF always needs adjustment, no matter fast path |
16358 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
16359 | * original insn at old prog. | |
16360 | */ | |
16361 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
16362 | ||
8041902d | 16363 | if (cnt == 1) |
75f0fc7b | 16364 | return; |
b325fbca | 16365 | prog_len = new_prog->len; |
75f0fc7b | 16366 | |
8041902d AS |
16367 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
16368 | memcpy(new_data + off + cnt - 1, old_data + off, | |
16369 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 16370 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
16371 | /* Expand insni[off]'s seen count to the patched range. */ |
16372 | new_data[i].seen = old_seen; | |
b325fbca JW |
16373 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
16374 | } | |
8041902d AS |
16375 | env->insn_aux_data = new_data; |
16376 | vfree(old_data); | |
8041902d AS |
16377 | } |
16378 | ||
cc8b0b92 AS |
16379 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
16380 | { | |
16381 | int i; | |
16382 | ||
16383 | if (len == 1) | |
16384 | return; | |
4cb3d99c JW |
16385 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
16386 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 16387 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 16388 | continue; |
9c8105bd | 16389 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
16390 | } |
16391 | } | |
16392 | ||
7506d211 | 16393 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
16394 | { |
16395 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
16396 | int i, sz = prog->aux->size_poke_tab; | |
16397 | struct bpf_jit_poke_descriptor *desc; | |
16398 | ||
16399 | for (i = 0; i < sz; i++) { | |
16400 | desc = &tab[i]; | |
7506d211 JF |
16401 | if (desc->insn_idx <= off) |
16402 | continue; | |
a748c697 MF |
16403 | desc->insn_idx += len - 1; |
16404 | } | |
16405 | } | |
16406 | ||
8041902d AS |
16407 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
16408 | const struct bpf_insn *patch, u32 len) | |
16409 | { | |
16410 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
16411 | struct bpf_insn_aux_data *new_data = NULL; |
16412 | ||
16413 | if (len > 1) { | |
16414 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
16415 | sizeof(struct bpf_insn_aux_data))); | |
16416 | if (!new_data) | |
16417 | return NULL; | |
16418 | } | |
8041902d AS |
16419 | |
16420 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
16421 | if (IS_ERR(new_prog)) { |
16422 | if (PTR_ERR(new_prog) == -ERANGE) | |
16423 | verbose(env, | |
16424 | "insn %d cannot be patched due to 16-bit range\n", | |
16425 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 16426 | vfree(new_data); |
8041902d | 16427 | return NULL; |
4f73379e | 16428 | } |
75f0fc7b | 16429 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 16430 | adjust_subprog_starts(env, off, len); |
7506d211 | 16431 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
16432 | return new_prog; |
16433 | } | |
16434 | ||
52875a04 JK |
16435 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
16436 | u32 off, u32 cnt) | |
16437 | { | |
16438 | int i, j; | |
16439 | ||
16440 | /* find first prog starting at or after off (first to remove) */ | |
16441 | for (i = 0; i < env->subprog_cnt; i++) | |
16442 | if (env->subprog_info[i].start >= off) | |
16443 | break; | |
16444 | /* find first prog starting at or after off + cnt (first to stay) */ | |
16445 | for (j = i; j < env->subprog_cnt; j++) | |
16446 | if (env->subprog_info[j].start >= off + cnt) | |
16447 | break; | |
16448 | /* if j doesn't start exactly at off + cnt, we are just removing | |
16449 | * the front of previous prog | |
16450 | */ | |
16451 | if (env->subprog_info[j].start != off + cnt) | |
16452 | j--; | |
16453 | ||
16454 | if (j > i) { | |
16455 | struct bpf_prog_aux *aux = env->prog->aux; | |
16456 | int move; | |
16457 | ||
16458 | /* move fake 'exit' subprog as well */ | |
16459 | move = env->subprog_cnt + 1 - j; | |
16460 | ||
16461 | memmove(env->subprog_info + i, | |
16462 | env->subprog_info + j, | |
16463 | sizeof(*env->subprog_info) * move); | |
16464 | env->subprog_cnt -= j - i; | |
16465 | ||
16466 | /* remove func_info */ | |
16467 | if (aux->func_info) { | |
16468 | move = aux->func_info_cnt - j; | |
16469 | ||
16470 | memmove(aux->func_info + i, | |
16471 | aux->func_info + j, | |
16472 | sizeof(*aux->func_info) * move); | |
16473 | aux->func_info_cnt -= j - i; | |
16474 | /* func_info->insn_off is set after all code rewrites, | |
16475 | * in adjust_btf_func() - no need to adjust | |
16476 | */ | |
16477 | } | |
16478 | } else { | |
16479 | /* convert i from "first prog to remove" to "first to adjust" */ | |
16480 | if (env->subprog_info[i].start == off) | |
16481 | i++; | |
16482 | } | |
16483 | ||
16484 | /* update fake 'exit' subprog as well */ | |
16485 | for (; i <= env->subprog_cnt; i++) | |
16486 | env->subprog_info[i].start -= cnt; | |
16487 | ||
16488 | return 0; | |
16489 | } | |
16490 | ||
16491 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
16492 | u32 cnt) | |
16493 | { | |
16494 | struct bpf_prog *prog = env->prog; | |
16495 | u32 i, l_off, l_cnt, nr_linfo; | |
16496 | struct bpf_line_info *linfo; | |
16497 | ||
16498 | nr_linfo = prog->aux->nr_linfo; | |
16499 | if (!nr_linfo) | |
16500 | return 0; | |
16501 | ||
16502 | linfo = prog->aux->linfo; | |
16503 | ||
16504 | /* find first line info to remove, count lines to be removed */ | |
16505 | for (i = 0; i < nr_linfo; i++) | |
16506 | if (linfo[i].insn_off >= off) | |
16507 | break; | |
16508 | ||
16509 | l_off = i; | |
16510 | l_cnt = 0; | |
16511 | for (; i < nr_linfo; i++) | |
16512 | if (linfo[i].insn_off < off + cnt) | |
16513 | l_cnt++; | |
16514 | else | |
16515 | break; | |
16516 | ||
16517 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
16518 | * last removed linfo. prog is already modified, so prog->len == off | |
16519 | * means no live instructions after (tail of the program was removed). | |
16520 | */ | |
16521 | if (prog->len != off && l_cnt && | |
16522 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
16523 | l_cnt--; | |
16524 | linfo[--i].insn_off = off + cnt; | |
16525 | } | |
16526 | ||
16527 | /* remove the line info which refer to the removed instructions */ | |
16528 | if (l_cnt) { | |
16529 | memmove(linfo + l_off, linfo + i, | |
16530 | sizeof(*linfo) * (nr_linfo - i)); | |
16531 | ||
16532 | prog->aux->nr_linfo -= l_cnt; | |
16533 | nr_linfo = prog->aux->nr_linfo; | |
16534 | } | |
16535 | ||
16536 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
16537 | for (i = l_off; i < nr_linfo; i++) | |
16538 | linfo[i].insn_off -= cnt; | |
16539 | ||
16540 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
16541 | for (i = 0; i <= env->subprog_cnt; i++) | |
16542 | if (env->subprog_info[i].linfo_idx > l_off) { | |
16543 | /* program may have started in the removed region but | |
16544 | * may not be fully removed | |
16545 | */ | |
16546 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
16547 | env->subprog_info[i].linfo_idx -= l_cnt; | |
16548 | else | |
16549 | env->subprog_info[i].linfo_idx = l_off; | |
16550 | } | |
16551 | ||
16552 | return 0; | |
16553 | } | |
16554 | ||
16555 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
16556 | { | |
16557 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16558 | unsigned int orig_prog_len = env->prog->len; | |
16559 | int err; | |
16560 | ||
9d03ebc7 | 16561 | if (bpf_prog_is_offloaded(env->prog->aux)) |
08ca90af JK |
16562 | bpf_prog_offload_remove_insns(env, off, cnt); |
16563 | ||
52875a04 JK |
16564 | err = bpf_remove_insns(env->prog, off, cnt); |
16565 | if (err) | |
16566 | return err; | |
16567 | ||
16568 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
16569 | if (err) | |
16570 | return err; | |
16571 | ||
16572 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
16573 | if (err) | |
16574 | return err; | |
16575 | ||
16576 | memmove(aux_data + off, aux_data + off + cnt, | |
16577 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
16578 | ||
16579 | return 0; | |
16580 | } | |
16581 | ||
2a5418a1 DB |
16582 | /* The verifier does more data flow analysis than llvm and will not |
16583 | * explore branches that are dead at run time. Malicious programs can | |
16584 | * have dead code too. Therefore replace all dead at-run-time code | |
16585 | * with 'ja -1'. | |
16586 | * | |
16587 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
16588 | * program and through another bug we would manage to jump there, then | |
16589 | * we'd execute beyond program memory otherwise. Returning exception | |
16590 | * code also wouldn't work since we can have subprogs where the dead | |
16591 | * code could be located. | |
c131187d AS |
16592 | */ |
16593 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
16594 | { | |
16595 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 16596 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
16597 | struct bpf_insn *insn = env->prog->insnsi; |
16598 | const int insn_cnt = env->prog->len; | |
16599 | int i; | |
16600 | ||
16601 | for (i = 0; i < insn_cnt; i++) { | |
16602 | if (aux_data[i].seen) | |
16603 | continue; | |
2a5418a1 | 16604 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 16605 | aux_data[i].zext_dst = false; |
c131187d AS |
16606 | } |
16607 | } | |
16608 | ||
e2ae4ca2 JK |
16609 | static bool insn_is_cond_jump(u8 code) |
16610 | { | |
16611 | u8 op; | |
16612 | ||
092ed096 JW |
16613 | if (BPF_CLASS(code) == BPF_JMP32) |
16614 | return true; | |
16615 | ||
e2ae4ca2 JK |
16616 | if (BPF_CLASS(code) != BPF_JMP) |
16617 | return false; | |
16618 | ||
16619 | op = BPF_OP(code); | |
16620 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
16621 | } | |
16622 | ||
16623 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
16624 | { | |
16625 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16626 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
16627 | struct bpf_insn *insn = env->prog->insnsi; | |
16628 | const int insn_cnt = env->prog->len; | |
16629 | int i; | |
16630 | ||
16631 | for (i = 0; i < insn_cnt; i++, insn++) { | |
16632 | if (!insn_is_cond_jump(insn->code)) | |
16633 | continue; | |
16634 | ||
16635 | if (!aux_data[i + 1].seen) | |
16636 | ja.off = insn->off; | |
16637 | else if (!aux_data[i + 1 + insn->off].seen) | |
16638 | ja.off = 0; | |
16639 | else | |
16640 | continue; | |
16641 | ||
9d03ebc7 | 16642 | if (bpf_prog_is_offloaded(env->prog->aux)) |
08ca90af JK |
16643 | bpf_prog_offload_replace_insn(env, i, &ja); |
16644 | ||
e2ae4ca2 JK |
16645 | memcpy(insn, &ja, sizeof(ja)); |
16646 | } | |
16647 | } | |
16648 | ||
52875a04 JK |
16649 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
16650 | { | |
16651 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16652 | int insn_cnt = env->prog->len; | |
16653 | int i, err; | |
16654 | ||
16655 | for (i = 0; i < insn_cnt; i++) { | |
16656 | int j; | |
16657 | ||
16658 | j = 0; | |
16659 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
16660 | j++; | |
16661 | if (!j) | |
16662 | continue; | |
16663 | ||
16664 | err = verifier_remove_insns(env, i, j); | |
16665 | if (err) | |
16666 | return err; | |
16667 | insn_cnt = env->prog->len; | |
16668 | } | |
16669 | ||
16670 | return 0; | |
16671 | } | |
16672 | ||
a1b14abc JK |
16673 | static int opt_remove_nops(struct bpf_verifier_env *env) |
16674 | { | |
16675 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
16676 | struct bpf_insn *insn = env->prog->insnsi; | |
16677 | int insn_cnt = env->prog->len; | |
16678 | int i, err; | |
16679 | ||
16680 | for (i = 0; i < insn_cnt; i++) { | |
16681 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
16682 | continue; | |
16683 | ||
16684 | err = verifier_remove_insns(env, i, 1); | |
16685 | if (err) | |
16686 | return err; | |
16687 | insn_cnt--; | |
16688 | i--; | |
16689 | } | |
16690 | ||
16691 | return 0; | |
16692 | } | |
16693 | ||
d6c2308c JW |
16694 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
16695 | const union bpf_attr *attr) | |
a4b1d3c1 | 16696 | { |
d6c2308c | 16697 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 16698 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 16699 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 16700 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 16701 | struct bpf_prog *new_prog; |
d6c2308c | 16702 | bool rnd_hi32; |
a4b1d3c1 | 16703 | |
d6c2308c | 16704 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 16705 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
16706 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
16707 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
16708 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
16709 | for (i = 0; i < len; i++) { |
16710 | int adj_idx = i + delta; | |
16711 | struct bpf_insn insn; | |
83a28819 | 16712 | int load_reg; |
a4b1d3c1 | 16713 | |
d6c2308c | 16714 | insn = insns[adj_idx]; |
83a28819 | 16715 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
16716 | if (!aux[adj_idx].zext_dst) { |
16717 | u8 code, class; | |
16718 | u32 imm_rnd; | |
16719 | ||
16720 | if (!rnd_hi32) | |
16721 | continue; | |
16722 | ||
16723 | code = insn.code; | |
16724 | class = BPF_CLASS(code); | |
83a28819 | 16725 | if (load_reg == -1) |
d6c2308c JW |
16726 | continue; |
16727 | ||
16728 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
16729 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
16730 | * here. | |
d6c2308c | 16731 | */ |
83a28819 | 16732 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
16733 | if (class == BPF_LD && |
16734 | BPF_MODE(code) == BPF_IMM) | |
16735 | i++; | |
16736 | continue; | |
16737 | } | |
16738 | ||
16739 | /* ctx load could be transformed into wider load. */ | |
16740 | if (class == BPF_LDX && | |
16741 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
16742 | continue; | |
16743 | ||
a251c17a | 16744 | imm_rnd = get_random_u32(); |
d6c2308c JW |
16745 | rnd_hi32_patch[0] = insn; |
16746 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 16747 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
16748 | patch = rnd_hi32_patch; |
16749 | patch_len = 4; | |
16750 | goto apply_patch_buffer; | |
16751 | } | |
16752 | ||
39491867 BJ |
16753 | /* Add in an zero-extend instruction if a) the JIT has requested |
16754 | * it or b) it's a CMPXCHG. | |
16755 | * | |
16756 | * The latter is because: BPF_CMPXCHG always loads a value into | |
16757 | * R0, therefore always zero-extends. However some archs' | |
16758 | * equivalent instruction only does this load when the | |
16759 | * comparison is successful. This detail of CMPXCHG is | |
16760 | * orthogonal to the general zero-extension behaviour of the | |
16761 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
16762 | */ | |
16763 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
16764 | continue; |
16765 | ||
d35af0a7 BT |
16766 | /* Zero-extension is done by the caller. */ |
16767 | if (bpf_pseudo_kfunc_call(&insn)) | |
16768 | continue; | |
16769 | ||
83a28819 IL |
16770 | if (WARN_ON(load_reg == -1)) { |
16771 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
16772 | return -EFAULT; | |
b2e37a71 IL |
16773 | } |
16774 | ||
a4b1d3c1 | 16775 | zext_patch[0] = insn; |
b2e37a71 IL |
16776 | zext_patch[1].dst_reg = load_reg; |
16777 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
16778 | patch = zext_patch; |
16779 | patch_len = 2; | |
16780 | apply_patch_buffer: | |
16781 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
16782 | if (!new_prog) |
16783 | return -ENOMEM; | |
16784 | env->prog = new_prog; | |
16785 | insns = new_prog->insnsi; | |
16786 | aux = env->insn_aux_data; | |
d6c2308c | 16787 | delta += patch_len - 1; |
a4b1d3c1 JW |
16788 | } |
16789 | ||
16790 | return 0; | |
16791 | } | |
16792 | ||
c64b7983 JS |
16793 | /* convert load instructions that access fields of a context type into a |
16794 | * sequence of instructions that access fields of the underlying structure: | |
16795 | * struct __sk_buff -> struct sk_buff | |
16796 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 16797 | */ |
58e2af8b | 16798 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 16799 | { |
00176a34 | 16800 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 16801 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 16802 | const int insn_cnt = env->prog->len; |
36bbef52 | 16803 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 16804 | u32 target_size, size_default, off; |
9bac3d6d | 16805 | struct bpf_prog *new_prog; |
d691f9e8 | 16806 | enum bpf_access_type type; |
f96da094 | 16807 | bool is_narrower_load; |
9bac3d6d | 16808 | |
b09928b9 DB |
16809 | if (ops->gen_prologue || env->seen_direct_write) { |
16810 | if (!ops->gen_prologue) { | |
16811 | verbose(env, "bpf verifier is misconfigured\n"); | |
16812 | return -EINVAL; | |
16813 | } | |
36bbef52 DB |
16814 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
16815 | env->prog); | |
16816 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 16817 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
16818 | return -EINVAL; |
16819 | } else if (cnt) { | |
8041902d | 16820 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
16821 | if (!new_prog) |
16822 | return -ENOMEM; | |
8041902d | 16823 | |
36bbef52 | 16824 | env->prog = new_prog; |
3df126f3 | 16825 | delta += cnt - 1; |
36bbef52 DB |
16826 | } |
16827 | } | |
16828 | ||
9d03ebc7 | 16829 | if (bpf_prog_is_offloaded(env->prog->aux)) |
9bac3d6d AS |
16830 | return 0; |
16831 | ||
3df126f3 | 16832 | insn = env->prog->insnsi + delta; |
36bbef52 | 16833 | |
9bac3d6d | 16834 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
16835 | bpf_convert_ctx_access_t convert_ctx_access; |
16836 | ||
62c7989b DB |
16837 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
16838 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
16839 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 16840 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 16841 | type = BPF_READ; |
2039f26f DB |
16842 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
16843 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
16844 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
16845 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
16846 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
16847 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
16848 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
16849 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 16850 | type = BPF_WRITE; |
2039f26f | 16851 | } else { |
9bac3d6d | 16852 | continue; |
2039f26f | 16853 | } |
9bac3d6d | 16854 | |
af86ca4e | 16855 | if (type == BPF_WRITE && |
2039f26f | 16856 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 16857 | struct bpf_insn patch[] = { |
af86ca4e | 16858 | *insn, |
2039f26f | 16859 | BPF_ST_NOSPEC(), |
af86ca4e AS |
16860 | }; |
16861 | ||
16862 | cnt = ARRAY_SIZE(patch); | |
16863 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
16864 | if (!new_prog) | |
16865 | return -ENOMEM; | |
16866 | ||
16867 | delta += cnt - 1; | |
16868 | env->prog = new_prog; | |
16869 | insn = new_prog->insnsi + i + delta; | |
16870 | continue; | |
16871 | } | |
16872 | ||
6efe152d | 16873 | switch ((int)env->insn_aux_data[i + delta].ptr_type) { |
c64b7983 JS |
16874 | case PTR_TO_CTX: |
16875 | if (!ops->convert_ctx_access) | |
16876 | continue; | |
16877 | convert_ctx_access = ops->convert_ctx_access; | |
16878 | break; | |
16879 | case PTR_TO_SOCKET: | |
46f8bc92 | 16880 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
16881 | convert_ctx_access = bpf_sock_convert_ctx_access; |
16882 | break; | |
655a51e5 MKL |
16883 | case PTR_TO_TCP_SOCK: |
16884 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
16885 | break; | |
fada7fdc JL |
16886 | case PTR_TO_XDP_SOCK: |
16887 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
16888 | break; | |
2a02759e | 16889 | case PTR_TO_BTF_ID: |
6efe152d | 16890 | case PTR_TO_BTF_ID | PTR_UNTRUSTED: |
282de143 KKD |
16891 | /* PTR_TO_BTF_ID | MEM_ALLOC always has a valid lifetime, unlike |
16892 | * PTR_TO_BTF_ID, and an active ref_obj_id, but the same cannot | |
16893 | * be said once it is marked PTR_UNTRUSTED, hence we must handle | |
16894 | * any faults for loads into such types. BPF_WRITE is disallowed | |
16895 | * for this case. | |
16896 | */ | |
16897 | case PTR_TO_BTF_ID | MEM_ALLOC | PTR_UNTRUSTED: | |
27ae7997 MKL |
16898 | if (type == BPF_READ) { |
16899 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
16900 | BPF_SIZE((insn)->code); | |
16901 | env->prog->aux->num_exentries++; | |
2a02759e | 16902 | } |
2a02759e | 16903 | continue; |
c64b7983 | 16904 | default: |
9bac3d6d | 16905 | continue; |
c64b7983 | 16906 | } |
9bac3d6d | 16907 | |
31fd8581 | 16908 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 16909 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
16910 | |
16911 | /* If the read access is a narrower load of the field, | |
16912 | * convert to a 4/8-byte load, to minimum program type specific | |
16913 | * convert_ctx_access changes. If conversion is successful, | |
16914 | * we will apply proper mask to the result. | |
16915 | */ | |
f96da094 | 16916 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
16917 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
16918 | off = insn->off; | |
31fd8581 | 16919 | if (is_narrower_load) { |
f96da094 DB |
16920 | u8 size_code; |
16921 | ||
16922 | if (type == BPF_WRITE) { | |
61bd5218 | 16923 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
16924 | return -EINVAL; |
16925 | } | |
31fd8581 | 16926 | |
f96da094 | 16927 | size_code = BPF_H; |
31fd8581 YS |
16928 | if (ctx_field_size == 4) |
16929 | size_code = BPF_W; | |
16930 | else if (ctx_field_size == 8) | |
16931 | size_code = BPF_DW; | |
f96da094 | 16932 | |
bc23105c | 16933 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
16934 | insn->code = BPF_LDX | BPF_MEM | size_code; |
16935 | } | |
f96da094 DB |
16936 | |
16937 | target_size = 0; | |
c64b7983 JS |
16938 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
16939 | &target_size); | |
f96da094 DB |
16940 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
16941 | (ctx_field_size && !target_size)) { | |
61bd5218 | 16942 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
16943 | return -EINVAL; |
16944 | } | |
f96da094 DB |
16945 | |
16946 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
16947 | u8 shift = bpf_ctx_narrow_access_offset( |
16948 | off, size, size_default) * 8; | |
d7af7e49 AI |
16949 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
16950 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
16951 | return -EINVAL; | |
16952 | } | |
46f53a65 AI |
16953 | if (ctx_field_size <= 4) { |
16954 | if (shift) | |
16955 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
16956 | insn->dst_reg, | |
16957 | shift); | |
31fd8581 | 16958 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 16959 | (1 << size * 8) - 1); |
46f53a65 AI |
16960 | } else { |
16961 | if (shift) | |
16962 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
16963 | insn->dst_reg, | |
16964 | shift); | |
31fd8581 | 16965 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 16966 | (1ULL << size * 8) - 1); |
46f53a65 | 16967 | } |
31fd8581 | 16968 | } |
9bac3d6d | 16969 | |
8041902d | 16970 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
16971 | if (!new_prog) |
16972 | return -ENOMEM; | |
16973 | ||
3df126f3 | 16974 | delta += cnt - 1; |
9bac3d6d AS |
16975 | |
16976 | /* keep walking new program and skip insns we just inserted */ | |
16977 | env->prog = new_prog; | |
3df126f3 | 16978 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
16979 | } |
16980 | ||
16981 | return 0; | |
16982 | } | |
16983 | ||
1c2a088a AS |
16984 | static int jit_subprogs(struct bpf_verifier_env *env) |
16985 | { | |
16986 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
16987 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 16988 | struct bpf_map *map_ptr; |
7105e828 | 16989 | struct bpf_insn *insn; |
1c2a088a | 16990 | void *old_bpf_func; |
c4c0bdc0 | 16991 | int err, num_exentries; |
1c2a088a | 16992 | |
f910cefa | 16993 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
16994 | return 0; |
16995 | ||
7105e828 | 16996 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 16997 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 16998 | continue; |
69c087ba | 16999 | |
c7a89784 DB |
17000 | /* Upon error here we cannot fall back to interpreter but |
17001 | * need a hard reject of the program. Thus -EFAULT is | |
17002 | * propagated in any case. | |
17003 | */ | |
1c2a088a AS |
17004 | subprog = find_subprog(env, i + insn->imm + 1); |
17005 | if (subprog < 0) { | |
17006 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
17007 | i + insn->imm + 1); | |
17008 | return -EFAULT; | |
17009 | } | |
17010 | /* temporarily remember subprog id inside insn instead of | |
17011 | * aux_data, since next loop will split up all insns into funcs | |
17012 | */ | |
f910cefa | 17013 | insn->off = subprog; |
1c2a088a AS |
17014 | /* remember original imm in case JIT fails and fallback |
17015 | * to interpreter will be needed | |
17016 | */ | |
17017 | env->insn_aux_data[i].call_imm = insn->imm; | |
17018 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
17019 | insn->imm = 1; | |
3990ed4c MKL |
17020 | if (bpf_pseudo_func(insn)) |
17021 | /* jit (e.g. x86_64) may emit fewer instructions | |
17022 | * if it learns a u32 imm is the same as a u64 imm. | |
17023 | * Force a non zero here. | |
17024 | */ | |
17025 | insn[1].imm = 1; | |
1c2a088a AS |
17026 | } |
17027 | ||
c454a46b MKL |
17028 | err = bpf_prog_alloc_jited_linfo(prog); |
17029 | if (err) | |
17030 | goto out_undo_insn; | |
17031 | ||
17032 | err = -ENOMEM; | |
6396bb22 | 17033 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 17034 | if (!func) |
c7a89784 | 17035 | goto out_undo_insn; |
1c2a088a | 17036 | |
f910cefa | 17037 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 17038 | subprog_start = subprog_end; |
4cb3d99c | 17039 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
17040 | |
17041 | len = subprog_end - subprog_start; | |
fb7dd8bc | 17042 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
17043 | * hence main prog stats include the runtime of subprogs. |
17044 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 17045 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
17046 | */ |
17047 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
17048 | if (!func[i]) |
17049 | goto out_free; | |
17050 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
17051 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 17052 | func[i]->type = prog->type; |
1c2a088a | 17053 | func[i]->len = len; |
4f74d809 DB |
17054 | if (bpf_prog_calc_tag(func[i])) |
17055 | goto out_free; | |
1c2a088a | 17056 | func[i]->is_func = 1; |
ba64e7d8 | 17057 | func[i]->aux->func_idx = i; |
f263a814 | 17058 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
17059 | func[i]->aux->btf = prog->aux->btf; |
17060 | func[i]->aux->func_info = prog->aux->func_info; | |
9c7c48d6 | 17061 | func[i]->aux->func_info_cnt = prog->aux->func_info_cnt; |
f263a814 JF |
17062 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
17063 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 17064 | |
a748c697 | 17065 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 17066 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 17067 | |
f263a814 JF |
17068 | poke = &prog->aux->poke_tab[j]; |
17069 | if (poke->insn_idx < subprog_end && | |
17070 | poke->insn_idx >= subprog_start) | |
17071 | poke->aux = func[i]->aux; | |
a748c697 MF |
17072 | } |
17073 | ||
1c2a088a | 17074 | func[i]->aux->name[0] = 'F'; |
9c8105bd | 17075 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 17076 | func[i]->jit_requested = 1; |
d2a3b7c5 | 17077 | func[i]->blinding_requested = prog->blinding_requested; |
e6ac2450 | 17078 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 17079 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
17080 | func[i]->aux->linfo = prog->aux->linfo; |
17081 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
17082 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
17083 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
17084 | num_exentries = 0; |
17085 | insn = func[i]->insnsi; | |
17086 | for (j = 0; j < func[i]->len; j++, insn++) { | |
17087 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
17088 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
17089 | num_exentries++; | |
17090 | } | |
17091 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 17092 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
17093 | func[i] = bpf_int_jit_compile(func[i]); |
17094 | if (!func[i]->jited) { | |
17095 | err = -ENOTSUPP; | |
17096 | goto out_free; | |
17097 | } | |
17098 | cond_resched(); | |
17099 | } | |
a748c697 | 17100 | |
1c2a088a AS |
17101 | /* at this point all bpf functions were successfully JITed |
17102 | * now populate all bpf_calls with correct addresses and | |
17103 | * run last pass of JIT | |
17104 | */ | |
f910cefa | 17105 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17106 | insn = func[i]->insnsi; |
17107 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 17108 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 17109 | subprog = insn->off; |
69c087ba YS |
17110 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
17111 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
17112 | continue; | |
17113 | } | |
23a2d70c | 17114 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
17115 | continue; |
17116 | subprog = insn->off; | |
3d717fad | 17117 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 17118 | } |
2162fed4 SD |
17119 | |
17120 | /* we use the aux data to keep a list of the start addresses | |
17121 | * of the JITed images for each function in the program | |
17122 | * | |
17123 | * for some architectures, such as powerpc64, the imm field | |
17124 | * might not be large enough to hold the offset of the start | |
17125 | * address of the callee's JITed image from __bpf_call_base | |
17126 | * | |
17127 | * in such cases, we can lookup the start address of a callee | |
17128 | * by using its subprog id, available from the off field of | |
17129 | * the call instruction, as an index for this list | |
17130 | */ | |
17131 | func[i]->aux->func = func; | |
17132 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 17133 | } |
f910cefa | 17134 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17135 | old_bpf_func = func[i]->bpf_func; |
17136 | tmp = bpf_int_jit_compile(func[i]); | |
17137 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
17138 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 17139 | err = -ENOTSUPP; |
1c2a088a AS |
17140 | goto out_free; |
17141 | } | |
17142 | cond_resched(); | |
17143 | } | |
17144 | ||
17145 | /* finally lock prog and jit images for all functions and | |
17146 | * populate kallsysm | |
17147 | */ | |
f910cefa | 17148 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17149 | bpf_prog_lock_ro(func[i]); |
17150 | bpf_prog_kallsyms_add(func[i]); | |
17151 | } | |
7105e828 DB |
17152 | |
17153 | /* Last step: make now unused interpreter insns from main | |
17154 | * prog consistent for later dump requests, so they can | |
17155 | * later look the same as if they were interpreted only. | |
17156 | */ | |
17157 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
17158 | if (bpf_pseudo_func(insn)) { |
17159 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
17160 | insn[1].imm = insn->off; |
17161 | insn->off = 0; | |
69c087ba YS |
17162 | continue; |
17163 | } | |
23a2d70c | 17164 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
17165 | continue; |
17166 | insn->off = env->insn_aux_data[i].call_imm; | |
17167 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 17168 | insn->imm = subprog; |
7105e828 DB |
17169 | } |
17170 | ||
1c2a088a AS |
17171 | prog->jited = 1; |
17172 | prog->bpf_func = func[0]->bpf_func; | |
d00c6473 | 17173 | prog->jited_len = func[0]->jited_len; |
1c2a088a | 17174 | prog->aux->func = func; |
f910cefa | 17175 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 17176 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
17177 | return 0; |
17178 | out_free: | |
f263a814 JF |
17179 | /* We failed JIT'ing, so at this point we need to unregister poke |
17180 | * descriptors from subprogs, so that kernel is not attempting to | |
17181 | * patch it anymore as we're freeing the subprog JIT memory. | |
17182 | */ | |
17183 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
17184 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
17185 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
17186 | } | |
17187 | /* At this point we're guaranteed that poke descriptors are not | |
17188 | * live anymore. We can just unlink its descriptor table as it's | |
17189 | * released with the main prog. | |
17190 | */ | |
a748c697 MF |
17191 | for (i = 0; i < env->subprog_cnt; i++) { |
17192 | if (!func[i]) | |
17193 | continue; | |
f263a814 | 17194 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
17195 | bpf_jit_free(func[i]); |
17196 | } | |
1c2a088a | 17197 | kfree(func); |
c7a89784 | 17198 | out_undo_insn: |
1c2a088a AS |
17199 | /* cleanup main prog to be interpreted */ |
17200 | prog->jit_requested = 0; | |
d2a3b7c5 | 17201 | prog->blinding_requested = 0; |
1c2a088a | 17202 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
23a2d70c | 17203 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
17204 | continue; |
17205 | insn->off = 0; | |
17206 | insn->imm = env->insn_aux_data[i].call_imm; | |
17207 | } | |
e16301fb | 17208 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
17209 | return err; |
17210 | } | |
17211 | ||
1ea47e01 AS |
17212 | static int fixup_call_args(struct bpf_verifier_env *env) |
17213 | { | |
19d28fbd | 17214 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
17215 | struct bpf_prog *prog = env->prog; |
17216 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 17217 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 17218 | int i, depth; |
19d28fbd | 17219 | #endif |
e4052d06 | 17220 | int err = 0; |
1ea47e01 | 17221 | |
e4052d06 | 17222 | if (env->prog->jit_requested && |
9d03ebc7 | 17223 | !bpf_prog_is_offloaded(env->prog->aux)) { |
19d28fbd DM |
17224 | err = jit_subprogs(env); |
17225 | if (err == 0) | |
1c2a088a | 17226 | return 0; |
c7a89784 DB |
17227 | if (err == -EFAULT) |
17228 | return err; | |
19d28fbd DM |
17229 | } |
17230 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
17231 | if (has_kfunc_call) { |
17232 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
17233 | return -EINVAL; | |
17234 | } | |
e411901c MF |
17235 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
17236 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
17237 | * have to be rejected, since interpreter doesn't support them yet. | |
17238 | */ | |
17239 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
17240 | return -EINVAL; | |
17241 | } | |
1ea47e01 | 17242 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
17243 | if (bpf_pseudo_func(insn)) { |
17244 | /* When JIT fails the progs with callback calls | |
17245 | * have to be rejected, since interpreter doesn't support them yet. | |
17246 | */ | |
17247 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
17248 | return -EINVAL; | |
17249 | } | |
17250 | ||
23a2d70c | 17251 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
17252 | continue; |
17253 | depth = get_callee_stack_depth(env, insn, i); | |
17254 | if (depth < 0) | |
17255 | return depth; | |
17256 | bpf_patch_call_args(insn, depth); | |
17257 | } | |
19d28fbd DM |
17258 | err = 0; |
17259 | #endif | |
17260 | return err; | |
1ea47e01 AS |
17261 | } |
17262 | ||
958cf2e2 KKD |
17263 | static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
17264 | struct bpf_insn *insn_buf, int insn_idx, int *cnt) | |
e6ac2450 MKL |
17265 | { |
17266 | const struct bpf_kfunc_desc *desc; | |
3d76a4d3 | 17267 | void *xdp_kfunc; |
e6ac2450 | 17268 | |
a5d82727 KKD |
17269 | if (!insn->imm) { |
17270 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
17271 | return -EINVAL; | |
17272 | } | |
17273 | ||
3d76a4d3 SF |
17274 | *cnt = 0; |
17275 | ||
17276 | if (bpf_dev_bound_kfunc_id(insn->imm)) { | |
17277 | xdp_kfunc = bpf_dev_bound_resolve_kfunc(env->prog, insn->imm); | |
17278 | if (xdp_kfunc) { | |
17279 | insn->imm = BPF_CALL_IMM(xdp_kfunc); | |
17280 | return 0; | |
17281 | } | |
17282 | ||
17283 | /* fallback to default kfunc when not supported by netdev */ | |
17284 | } | |
17285 | ||
e6ac2450 | 17286 | /* insn->imm has the btf func_id. Replace it with |
c2cc0ce7 | 17287 | * an address (relative to __bpf_call_base). |
e6ac2450 | 17288 | */ |
2357672c | 17289 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
17290 | if (!desc) { |
17291 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
17292 | insn->imm); | |
17293 | return -EFAULT; | |
17294 | } | |
17295 | ||
17296 | insn->imm = desc->imm; | |
958cf2e2 KKD |
17297 | if (insn->off) |
17298 | return 0; | |
17299 | if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
17300 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
17301 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
17302 | u64 obj_new_size = env->insn_aux_data[insn_idx].obj_new_size; | |
e6ac2450 | 17303 | |
958cf2e2 KKD |
17304 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_1, obj_new_size); |
17305 | insn_buf[1] = addr[0]; | |
17306 | insn_buf[2] = addr[1]; | |
17307 | insn_buf[3] = *insn; | |
17308 | *cnt = 4; | |
ac9f0605 KKD |
17309 | } else if (desc->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { |
17310 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
17311 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
17312 | ||
17313 | insn_buf[0] = addr[0]; | |
17314 | insn_buf[1] = addr[1]; | |
17315 | insn_buf[2] = *insn; | |
17316 | *cnt = 3; | |
a35b9af4 YS |
17317 | } else if (desc->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] || |
17318 | desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { | |
fd264ca0 YS |
17319 | insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1); |
17320 | *cnt = 1; | |
b5964b96 JK |
17321 | } else if (desc->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { |
17322 | bool seen_direct_write = env->seen_direct_write; | |
17323 | bool is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE); | |
17324 | ||
17325 | if (is_rdonly) | |
17326 | insn->imm = BPF_CALL_IMM(bpf_dynptr_from_skb_rdonly); | |
17327 | ||
17328 | /* restore env->seen_direct_write to its original value, since | |
17329 | * may_access_direct_pkt_data mutates it | |
17330 | */ | |
17331 | env->seen_direct_write = seen_direct_write; | |
958cf2e2 | 17332 | } |
e6ac2450 MKL |
17333 | return 0; |
17334 | } | |
17335 | ||
e6ac5933 BJ |
17336 | /* Do various post-verification rewrites in a single program pass. |
17337 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 17338 | */ |
e6ac5933 | 17339 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 17340 | { |
79741b3b | 17341 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 17342 | enum bpf_attach_type eatype = prog->expected_attach_type; |
9b99edca | 17343 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 17344 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 17345 | const struct bpf_func_proto *fn; |
79741b3b | 17346 | const int insn_cnt = prog->len; |
09772d92 | 17347 | const struct bpf_map_ops *ops; |
c93552c4 | 17348 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
17349 | struct bpf_insn insn_buf[16]; |
17350 | struct bpf_prog *new_prog; | |
17351 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 17352 | int i, ret, cnt, delta = 0; |
e245c5c6 | 17353 | |
79741b3b | 17354 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 17355 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
17356 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
17357 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
17358 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 17359 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 17360 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
17361 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
17362 | struct bpf_insn *patchlet; | |
17363 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 17364 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
17365 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
17366 | BPF_JNE | BPF_K, insn->src_reg, | |
17367 | 0, 2, 0), | |
f6b1b3bf DB |
17368 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
17369 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
17370 | *insn, | |
17371 | }; | |
e88b2c6e | 17372 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 17373 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
17374 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
17375 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 17376 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 17377 | *insn, |
9b00f1b7 DB |
17378 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
17379 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 17380 | }; |
f6b1b3bf | 17381 | |
e88b2c6e DB |
17382 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
17383 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 17384 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
17385 | |
17386 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
17387 | if (!new_prog) |
17388 | return -ENOMEM; | |
17389 | ||
17390 | delta += cnt - 1; | |
17391 | env->prog = prog = new_prog; | |
17392 | insn = new_prog->insnsi + i + delta; | |
17393 | continue; | |
17394 | } | |
17395 | ||
e6ac5933 | 17396 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
17397 | if (BPF_CLASS(insn->code) == BPF_LD && |
17398 | (BPF_MODE(insn->code) == BPF_ABS || | |
17399 | BPF_MODE(insn->code) == BPF_IND)) { | |
17400 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
17401 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
17402 | verbose(env, "bpf verifier is misconfigured\n"); | |
17403 | return -EINVAL; | |
17404 | } | |
17405 | ||
17406 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17407 | if (!new_prog) | |
17408 | return -ENOMEM; | |
17409 | ||
17410 | delta += cnt - 1; | |
17411 | env->prog = prog = new_prog; | |
17412 | insn = new_prog->insnsi + i + delta; | |
17413 | continue; | |
17414 | } | |
17415 | ||
e6ac5933 | 17416 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
17417 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
17418 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
17419 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
17420 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 17421 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 17422 | bool issrc, isneg, isimm; |
979d63d5 DB |
17423 | u32 off_reg; |
17424 | ||
17425 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
17426 | if (!aux->alu_state || |
17427 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
17428 | continue; |
17429 | ||
17430 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
17431 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
17432 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 17433 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
17434 | |
17435 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
17436 | if (isimm) { |
17437 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
17438 | } else { | |
17439 | if (isneg) | |
17440 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
17441 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
17442 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
17443 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
17444 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
17445 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
17446 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
17447 | } | |
b9b34ddb DB |
17448 | if (!issrc) |
17449 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
17450 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
17451 | if (isneg) |
17452 | insn->code = insn->code == code_add ? | |
17453 | code_sub : code_add; | |
17454 | *patch++ = *insn; | |
801c6058 | 17455 | if (issrc && isneg && !isimm) |
979d63d5 DB |
17456 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
17457 | cnt = patch - insn_buf; | |
17458 | ||
17459 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17460 | if (!new_prog) | |
17461 | return -ENOMEM; | |
17462 | ||
17463 | delta += cnt - 1; | |
17464 | env->prog = prog = new_prog; | |
17465 | insn = new_prog->insnsi + i + delta; | |
17466 | continue; | |
17467 | } | |
17468 | ||
79741b3b AS |
17469 | if (insn->code != (BPF_JMP | BPF_CALL)) |
17470 | continue; | |
cc8b0b92 AS |
17471 | if (insn->src_reg == BPF_PSEUDO_CALL) |
17472 | continue; | |
e6ac2450 | 17473 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
958cf2e2 | 17474 | ret = fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt); |
e6ac2450 MKL |
17475 | if (ret) |
17476 | return ret; | |
958cf2e2 KKD |
17477 | if (cnt == 0) |
17478 | continue; | |
17479 | ||
17480 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17481 | if (!new_prog) | |
17482 | return -ENOMEM; | |
17483 | ||
17484 | delta += cnt - 1; | |
17485 | env->prog = prog = new_prog; | |
17486 | insn = new_prog->insnsi + i + delta; | |
e6ac2450 MKL |
17487 | continue; |
17488 | } | |
e245c5c6 | 17489 | |
79741b3b AS |
17490 | if (insn->imm == BPF_FUNC_get_route_realm) |
17491 | prog->dst_needed = 1; | |
17492 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
17493 | bpf_user_rnd_init_once(); | |
9802d865 JB |
17494 | if (insn->imm == BPF_FUNC_override_return) |
17495 | prog->kprobe_override = 1; | |
79741b3b | 17496 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
17497 | /* If we tail call into other programs, we |
17498 | * cannot make any assumptions since they can | |
17499 | * be replaced dynamically during runtime in | |
17500 | * the program array. | |
17501 | */ | |
17502 | prog->cb_access = 1; | |
e411901c MF |
17503 | if (!allow_tail_call_in_subprogs(env)) |
17504 | prog->aux->stack_depth = MAX_BPF_STACK; | |
17505 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 17506 | |
79741b3b | 17507 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 17508 | * conditional branch in the interpreter for every normal |
79741b3b AS |
17509 | * call and to prevent accidental JITing by JIT compiler |
17510 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 17511 | */ |
79741b3b | 17512 | insn->imm = 0; |
71189fa9 | 17513 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 17514 | |
c93552c4 | 17515 | aux = &env->insn_aux_data[i + delta]; |
d2a3b7c5 | 17516 | if (env->bpf_capable && !prog->blinding_requested && |
cc52d914 | 17517 | prog->jit_requested && |
d2e4c1e6 DB |
17518 | !bpf_map_key_poisoned(aux) && |
17519 | !bpf_map_ptr_poisoned(aux) && | |
17520 | !bpf_map_ptr_unpriv(aux)) { | |
17521 | struct bpf_jit_poke_descriptor desc = { | |
17522 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
17523 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
17524 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 17525 | .insn_idx = i + delta, |
d2e4c1e6 DB |
17526 | }; |
17527 | ||
17528 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
17529 | if (ret < 0) { | |
17530 | verbose(env, "adding tail call poke descriptor failed\n"); | |
17531 | return ret; | |
17532 | } | |
17533 | ||
17534 | insn->imm = ret + 1; | |
17535 | continue; | |
17536 | } | |
17537 | ||
c93552c4 DB |
17538 | if (!bpf_map_ptr_unpriv(aux)) |
17539 | continue; | |
17540 | ||
b2157399 AS |
17541 | /* instead of changing every JIT dealing with tail_call |
17542 | * emit two extra insns: | |
17543 | * if (index >= max_entries) goto out; | |
17544 | * index &= array->index_mask; | |
17545 | * to avoid out-of-bounds cpu speculation | |
17546 | */ | |
c93552c4 | 17547 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 17548 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
17549 | return -EINVAL; |
17550 | } | |
c93552c4 | 17551 | |
d2e4c1e6 | 17552 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
17553 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
17554 | map_ptr->max_entries, 2); | |
17555 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
17556 | container_of(map_ptr, | |
17557 | struct bpf_array, | |
17558 | map)->index_mask); | |
17559 | insn_buf[2] = *insn; | |
17560 | cnt = 3; | |
17561 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17562 | if (!new_prog) | |
17563 | return -ENOMEM; | |
17564 | ||
17565 | delta += cnt - 1; | |
17566 | env->prog = prog = new_prog; | |
17567 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
17568 | continue; |
17569 | } | |
e245c5c6 | 17570 | |
b00628b1 AS |
17571 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
17572 | /* The verifier will process callback_fn as many times as necessary | |
17573 | * with different maps and the register states prepared by | |
17574 | * set_timer_callback_state will be accurate. | |
17575 | * | |
17576 | * The following use case is valid: | |
17577 | * map1 is shared by prog1, prog2, prog3. | |
17578 | * prog1 calls bpf_timer_init for some map1 elements | |
17579 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
17580 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
17581 | * prog3 calls bpf_timer_start for some map1 elements. | |
17582 | * Those that were not both bpf_timer_init-ed and | |
17583 | * bpf_timer_set_callback-ed will return -EINVAL. | |
17584 | */ | |
17585 | struct bpf_insn ld_addrs[2] = { | |
17586 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
17587 | }; | |
17588 | ||
17589 | insn_buf[0] = ld_addrs[0]; | |
17590 | insn_buf[1] = ld_addrs[1]; | |
17591 | insn_buf[2] = *insn; | |
17592 | cnt = 3; | |
17593 | ||
17594 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17595 | if (!new_prog) | |
17596 | return -ENOMEM; | |
17597 | ||
17598 | delta += cnt - 1; | |
17599 | env->prog = prog = new_prog; | |
17600 | insn = new_prog->insnsi + i + delta; | |
17601 | goto patch_call_imm; | |
17602 | } | |
17603 | ||
9bb00b28 YS |
17604 | if (is_storage_get_function(insn->imm)) { |
17605 | if (!env->prog->aux->sleepable || | |
17606 | env->insn_aux_data[i + delta].storage_get_func_atomic) | |
d56c9fe6 | 17607 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); |
9bb00b28 YS |
17608 | else |
17609 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); | |
b00fa38a JK |
17610 | insn_buf[1] = *insn; |
17611 | cnt = 2; | |
17612 | ||
17613 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17614 | if (!new_prog) | |
17615 | return -ENOMEM; | |
17616 | ||
17617 | delta += cnt - 1; | |
17618 | env->prog = prog = new_prog; | |
17619 | insn = new_prog->insnsi + i + delta; | |
17620 | goto patch_call_imm; | |
17621 | } | |
17622 | ||
89c63074 | 17623 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
17624 | * and other inlining handlers are currently limited to 64 bit |
17625 | * only. | |
89c63074 | 17626 | */ |
60b58afc | 17627 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
17628 | (insn->imm == BPF_FUNC_map_lookup_elem || |
17629 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
17630 | insn->imm == BPF_FUNC_map_delete_elem || |
17631 | insn->imm == BPF_FUNC_map_push_elem || | |
17632 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 17633 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c | 17634 | insn->imm == BPF_FUNC_redirect_map || |
07343110 FZ |
17635 | insn->imm == BPF_FUNC_for_each_map_elem || |
17636 | insn->imm == BPF_FUNC_map_lookup_percpu_elem)) { | |
c93552c4 DB |
17637 | aux = &env->insn_aux_data[i + delta]; |
17638 | if (bpf_map_ptr_poisoned(aux)) | |
17639 | goto patch_call_imm; | |
17640 | ||
d2e4c1e6 | 17641 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
17642 | ops = map_ptr->ops; |
17643 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
17644 | ops->map_gen_lookup) { | |
17645 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
17646 | if (cnt == -EOPNOTSUPP) |
17647 | goto patch_map_ops_generic; | |
17648 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
17649 | verbose(env, "bpf verifier is misconfigured\n"); |
17650 | return -EINVAL; | |
17651 | } | |
81ed18ab | 17652 | |
09772d92 DB |
17653 | new_prog = bpf_patch_insn_data(env, i + delta, |
17654 | insn_buf, cnt); | |
17655 | if (!new_prog) | |
17656 | return -ENOMEM; | |
81ed18ab | 17657 | |
09772d92 DB |
17658 | delta += cnt - 1; |
17659 | env->prog = prog = new_prog; | |
17660 | insn = new_prog->insnsi + i + delta; | |
17661 | continue; | |
17662 | } | |
81ed18ab | 17663 | |
09772d92 DB |
17664 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
17665 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
17666 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
17667 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
17668 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
17669 | (int (*)(struct bpf_map *map, void *key, void *value, | |
17670 | u64 flags))NULL)); | |
84430d42 DB |
17671 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
17672 | (int (*)(struct bpf_map *map, void *value, | |
17673 | u64 flags))NULL)); | |
17674 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
17675 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
17676 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
17677 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
e6a4750f | 17678 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
32637e33 | 17679 | (int (*)(struct bpf_map *map, u64 index, u64 flags))NULL)); |
0640c77c AI |
17680 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
17681 | (int (*)(struct bpf_map *map, | |
17682 | bpf_callback_t callback_fn, | |
17683 | void *callback_ctx, | |
17684 | u64 flags))NULL)); | |
07343110 FZ |
17685 | BUILD_BUG_ON(!__same_type(ops->map_lookup_percpu_elem, |
17686 | (void *(*)(struct bpf_map *map, void *key, u32 cpu))NULL)); | |
e6a4750f | 17687 | |
4a8f87e6 | 17688 | patch_map_ops_generic: |
09772d92 DB |
17689 | switch (insn->imm) { |
17690 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 17691 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
17692 | continue; |
17693 | case BPF_FUNC_map_update_elem: | |
3d717fad | 17694 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
17695 | continue; |
17696 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 17697 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 17698 | continue; |
84430d42 | 17699 | case BPF_FUNC_map_push_elem: |
3d717fad | 17700 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
17701 | continue; |
17702 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 17703 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
17704 | continue; |
17705 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 17706 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 17707 | continue; |
e6a4750f | 17708 | case BPF_FUNC_redirect_map: |
3d717fad | 17709 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 17710 | continue; |
0640c77c AI |
17711 | case BPF_FUNC_for_each_map_elem: |
17712 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 17713 | continue; |
07343110 FZ |
17714 | case BPF_FUNC_map_lookup_percpu_elem: |
17715 | insn->imm = BPF_CALL_IMM(ops->map_lookup_percpu_elem); | |
17716 | continue; | |
09772d92 | 17717 | } |
81ed18ab | 17718 | |
09772d92 | 17719 | goto patch_call_imm; |
81ed18ab AS |
17720 | } |
17721 | ||
e6ac5933 | 17722 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
17723 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
17724 | insn->imm == BPF_FUNC_jiffies64) { | |
17725 | struct bpf_insn ld_jiffies_addr[2] = { | |
17726 | BPF_LD_IMM64(BPF_REG_0, | |
17727 | (unsigned long)&jiffies), | |
17728 | }; | |
17729 | ||
17730 | insn_buf[0] = ld_jiffies_addr[0]; | |
17731 | insn_buf[1] = ld_jiffies_addr[1]; | |
17732 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
17733 | BPF_REG_0, 0); | |
17734 | cnt = 3; | |
17735 | ||
17736 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
17737 | cnt); | |
17738 | if (!new_prog) | |
17739 | return -ENOMEM; | |
17740 | ||
17741 | delta += cnt - 1; | |
17742 | env->prog = prog = new_prog; | |
17743 | insn = new_prog->insnsi + i + delta; | |
17744 | continue; | |
17745 | } | |
17746 | ||
f92c1e18 JO |
17747 | /* Implement bpf_get_func_arg inline. */ |
17748 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17749 | insn->imm == BPF_FUNC_get_func_arg) { | |
17750 | /* Load nr_args from ctx - 8 */ | |
17751 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17752 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
17753 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
17754 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
17755 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
17756 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
17757 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
17758 | insn_buf[7] = BPF_JMP_A(1); | |
17759 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
17760 | cnt = 9; | |
17761 | ||
17762 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17763 | if (!new_prog) | |
17764 | return -ENOMEM; | |
17765 | ||
17766 | delta += cnt - 1; | |
17767 | env->prog = prog = new_prog; | |
17768 | insn = new_prog->insnsi + i + delta; | |
17769 | continue; | |
17770 | } | |
17771 | ||
17772 | /* Implement bpf_get_func_ret inline. */ | |
17773 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17774 | insn->imm == BPF_FUNC_get_func_ret) { | |
17775 | if (eatype == BPF_TRACE_FEXIT || | |
17776 | eatype == BPF_MODIFY_RETURN) { | |
17777 | /* Load nr_args from ctx - 8 */ | |
17778 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17779 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
17780 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
17781 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
17782 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
17783 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
17784 | cnt = 6; | |
17785 | } else { | |
17786 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
17787 | cnt = 1; | |
17788 | } | |
17789 | ||
17790 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17791 | if (!new_prog) | |
17792 | return -ENOMEM; | |
17793 | ||
17794 | delta += cnt - 1; | |
17795 | env->prog = prog = new_prog; | |
17796 | insn = new_prog->insnsi + i + delta; | |
17797 | continue; | |
17798 | } | |
17799 | ||
17800 | /* Implement get_func_arg_cnt inline. */ | |
17801 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17802 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
17803 | /* Load nr_args from ctx - 8 */ | |
17804 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17805 | ||
17806 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
17807 | if (!new_prog) | |
17808 | return -ENOMEM; | |
17809 | ||
17810 | env->prog = prog = new_prog; | |
17811 | insn = new_prog->insnsi + i + delta; | |
17812 | continue; | |
17813 | } | |
17814 | ||
f705ec76 | 17815 | /* Implement bpf_get_func_ip inline. */ |
9b99edca JO |
17816 | if (prog_type == BPF_PROG_TYPE_TRACING && |
17817 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
17818 | /* Load IP address from ctx - 16 */ |
17819 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
17820 | |
17821 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
17822 | if (!new_prog) | |
17823 | return -ENOMEM; | |
17824 | ||
17825 | env->prog = prog = new_prog; | |
17826 | insn = new_prog->insnsi + i + delta; | |
17827 | continue; | |
17828 | } | |
17829 | ||
81ed18ab | 17830 | patch_call_imm: |
5e43f899 | 17831 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
17832 | /* all functions that have prototype and verifier allowed |
17833 | * programs to call them, must be real in-kernel functions | |
17834 | */ | |
17835 | if (!fn->func) { | |
61bd5218 JK |
17836 | verbose(env, |
17837 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
17838 | func_id_name(insn->imm), insn->imm); |
17839 | return -EFAULT; | |
e245c5c6 | 17840 | } |
79741b3b | 17841 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 17842 | } |
e245c5c6 | 17843 | |
d2e4c1e6 DB |
17844 | /* Since poke tab is now finalized, publish aux to tracker. */ |
17845 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
17846 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
17847 | if (!map_ptr->ops->map_poke_track || | |
17848 | !map_ptr->ops->map_poke_untrack || | |
17849 | !map_ptr->ops->map_poke_run) { | |
17850 | verbose(env, "bpf verifier is misconfigured\n"); | |
17851 | return -EINVAL; | |
17852 | } | |
17853 | ||
17854 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
17855 | if (ret < 0) { | |
17856 | verbose(env, "tracking tail call prog failed\n"); | |
17857 | return ret; | |
17858 | } | |
17859 | } | |
17860 | ||
e6ac2450 MKL |
17861 | sort_kfunc_descs_by_imm(env->prog); |
17862 | ||
79741b3b AS |
17863 | return 0; |
17864 | } | |
e245c5c6 | 17865 | |
1ade2371 EZ |
17866 | static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env, |
17867 | int position, | |
17868 | s32 stack_base, | |
17869 | u32 callback_subprogno, | |
17870 | u32 *cnt) | |
17871 | { | |
17872 | s32 r6_offset = stack_base + 0 * BPF_REG_SIZE; | |
17873 | s32 r7_offset = stack_base + 1 * BPF_REG_SIZE; | |
17874 | s32 r8_offset = stack_base + 2 * BPF_REG_SIZE; | |
17875 | int reg_loop_max = BPF_REG_6; | |
17876 | int reg_loop_cnt = BPF_REG_7; | |
17877 | int reg_loop_ctx = BPF_REG_8; | |
17878 | ||
17879 | struct bpf_prog *new_prog; | |
17880 | u32 callback_start; | |
17881 | u32 call_insn_offset; | |
17882 | s32 callback_offset; | |
17883 | ||
17884 | /* This represents an inlined version of bpf_iter.c:bpf_loop, | |
17885 | * be careful to modify this code in sync. | |
17886 | */ | |
17887 | struct bpf_insn insn_buf[] = { | |
17888 | /* Return error and jump to the end of the patch if | |
17889 | * expected number of iterations is too big. | |
17890 | */ | |
17891 | BPF_JMP_IMM(BPF_JLE, BPF_REG_1, BPF_MAX_LOOPS, 2), | |
17892 | BPF_MOV32_IMM(BPF_REG_0, -E2BIG), | |
17893 | BPF_JMP_IMM(BPF_JA, 0, 0, 16), | |
17894 | /* spill R6, R7, R8 to use these as loop vars */ | |
17895 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, r6_offset), | |
17896 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_7, r7_offset), | |
17897 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_8, r8_offset), | |
17898 | /* initialize loop vars */ | |
17899 | BPF_MOV64_REG(reg_loop_max, BPF_REG_1), | |
17900 | BPF_MOV32_IMM(reg_loop_cnt, 0), | |
17901 | BPF_MOV64_REG(reg_loop_ctx, BPF_REG_3), | |
17902 | /* loop header, | |
17903 | * if reg_loop_cnt >= reg_loop_max skip the loop body | |
17904 | */ | |
17905 | BPF_JMP_REG(BPF_JGE, reg_loop_cnt, reg_loop_max, 5), | |
17906 | /* callback call, | |
17907 | * correct callback offset would be set after patching | |
17908 | */ | |
17909 | BPF_MOV64_REG(BPF_REG_1, reg_loop_cnt), | |
17910 | BPF_MOV64_REG(BPF_REG_2, reg_loop_ctx), | |
17911 | BPF_CALL_REL(0), | |
17912 | /* increment loop counter */ | |
17913 | BPF_ALU64_IMM(BPF_ADD, reg_loop_cnt, 1), | |
17914 | /* jump to loop header if callback returned 0 */ | |
17915 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -6), | |
17916 | /* return value of bpf_loop, | |
17917 | * set R0 to the number of iterations | |
17918 | */ | |
17919 | BPF_MOV64_REG(BPF_REG_0, reg_loop_cnt), | |
17920 | /* restore original values of R6, R7, R8 */ | |
17921 | BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, r6_offset), | |
17922 | BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, r7_offset), | |
17923 | BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_10, r8_offset), | |
17924 | }; | |
17925 | ||
17926 | *cnt = ARRAY_SIZE(insn_buf); | |
17927 | new_prog = bpf_patch_insn_data(env, position, insn_buf, *cnt); | |
17928 | if (!new_prog) | |
17929 | return new_prog; | |
17930 | ||
17931 | /* callback start is known only after patching */ | |
17932 | callback_start = env->subprog_info[callback_subprogno].start; | |
17933 | /* Note: insn_buf[12] is an offset of BPF_CALL_REL instruction */ | |
17934 | call_insn_offset = position + 12; | |
17935 | callback_offset = callback_start - call_insn_offset - 1; | |
fb4e3b33 | 17936 | new_prog->insnsi[call_insn_offset].imm = callback_offset; |
1ade2371 EZ |
17937 | |
17938 | return new_prog; | |
17939 | } | |
17940 | ||
17941 | static bool is_bpf_loop_call(struct bpf_insn *insn) | |
17942 | { | |
17943 | return insn->code == (BPF_JMP | BPF_CALL) && | |
17944 | insn->src_reg == 0 && | |
17945 | insn->imm == BPF_FUNC_loop; | |
17946 | } | |
17947 | ||
17948 | /* For all sub-programs in the program (including main) check | |
17949 | * insn_aux_data to see if there are bpf_loop calls that require | |
17950 | * inlining. If such calls are found the calls are replaced with a | |
17951 | * sequence of instructions produced by `inline_bpf_loop` function and | |
17952 | * subprog stack_depth is increased by the size of 3 registers. | |
17953 | * This stack space is used to spill values of the R6, R7, R8. These | |
17954 | * registers are used to store the loop bound, counter and context | |
17955 | * variables. | |
17956 | */ | |
17957 | static int optimize_bpf_loop(struct bpf_verifier_env *env) | |
17958 | { | |
17959 | struct bpf_subprog_info *subprogs = env->subprog_info; | |
17960 | int i, cur_subprog = 0, cnt, delta = 0; | |
17961 | struct bpf_insn *insn = env->prog->insnsi; | |
17962 | int insn_cnt = env->prog->len; | |
17963 | u16 stack_depth = subprogs[cur_subprog].stack_depth; | |
17964 | u16 stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
17965 | u16 stack_depth_extra = 0; | |
17966 | ||
17967 | for (i = 0; i < insn_cnt; i++, insn++) { | |
17968 | struct bpf_loop_inline_state *inline_state = | |
17969 | &env->insn_aux_data[i + delta].loop_inline_state; | |
17970 | ||
17971 | if (is_bpf_loop_call(insn) && inline_state->fit_for_inline) { | |
17972 | struct bpf_prog *new_prog; | |
17973 | ||
17974 | stack_depth_extra = BPF_REG_SIZE * 3 + stack_depth_roundup; | |
17975 | new_prog = inline_bpf_loop(env, | |
17976 | i + delta, | |
17977 | -(stack_depth + stack_depth_extra), | |
17978 | inline_state->callback_subprogno, | |
17979 | &cnt); | |
17980 | if (!new_prog) | |
17981 | return -ENOMEM; | |
17982 | ||
17983 | delta += cnt - 1; | |
17984 | env->prog = new_prog; | |
17985 | insn = new_prog->insnsi + i + delta; | |
17986 | } | |
17987 | ||
17988 | if (subprogs[cur_subprog + 1].start == i + delta + 1) { | |
17989 | subprogs[cur_subprog].stack_depth += stack_depth_extra; | |
17990 | cur_subprog++; | |
17991 | stack_depth = subprogs[cur_subprog].stack_depth; | |
17992 | stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
17993 | stack_depth_extra = 0; | |
17994 | } | |
17995 | } | |
17996 | ||
17997 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
17998 | ||
17999 | return 0; | |
18000 | } | |
18001 | ||
58e2af8b | 18002 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 18003 | { |
58e2af8b | 18004 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
18005 | int i; |
18006 | ||
9f4686c4 AS |
18007 | sl = env->free_list; |
18008 | while (sl) { | |
18009 | sln = sl->next; | |
18010 | free_verifier_state(&sl->state, false); | |
18011 | kfree(sl); | |
18012 | sl = sln; | |
18013 | } | |
51c39bb1 | 18014 | env->free_list = NULL; |
9f4686c4 | 18015 | |
f1bca824 AS |
18016 | if (!env->explored_states) |
18017 | return; | |
18018 | ||
dc2a4ebc | 18019 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
18020 | sl = env->explored_states[i]; |
18021 | ||
a8f500af AS |
18022 | while (sl) { |
18023 | sln = sl->next; | |
18024 | free_verifier_state(&sl->state, false); | |
18025 | kfree(sl); | |
18026 | sl = sln; | |
18027 | } | |
51c39bb1 | 18028 | env->explored_states[i] = NULL; |
f1bca824 | 18029 | } |
51c39bb1 | 18030 | } |
f1bca824 | 18031 | |
51c39bb1 AS |
18032 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
18033 | { | |
6f8a57cc | 18034 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
18035 | struct bpf_verifier_state *state; |
18036 | struct bpf_reg_state *regs; | |
18037 | int ret, i; | |
18038 | ||
18039 | env->prev_linfo = NULL; | |
18040 | env->pass_cnt++; | |
18041 | ||
18042 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
18043 | if (!state) | |
18044 | return -ENOMEM; | |
18045 | state->curframe = 0; | |
18046 | state->speculative = false; | |
18047 | state->branches = 1; | |
18048 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
18049 | if (!state->frame[0]) { | |
18050 | kfree(state); | |
18051 | return -ENOMEM; | |
18052 | } | |
18053 | env->cur_state = state; | |
18054 | init_func_state(env, state->frame[0], | |
18055 | BPF_MAIN_FUNC /* callsite */, | |
18056 | 0 /* frameno */, | |
18057 | subprog); | |
be2ef816 AN |
18058 | state->first_insn_idx = env->subprog_info[subprog].start; |
18059 | state->last_insn_idx = -1; | |
51c39bb1 AS |
18060 | |
18061 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 18062 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
18063 | ret = btf_prepare_func_args(env, subprog, regs); |
18064 | if (ret) | |
18065 | goto out; | |
18066 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
18067 | if (regs[i].type == PTR_TO_CTX) | |
18068 | mark_reg_known_zero(env, regs, i); | |
18069 | else if (regs[i].type == SCALAR_VALUE) | |
18070 | mark_reg_unknown(env, regs, i); | |
cf9f2f8d | 18071 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
e5069b9c DB |
18072 | const u32 mem_size = regs[i].mem_size; |
18073 | ||
18074 | mark_reg_known_zero(env, regs, i); | |
18075 | regs[i].mem_size = mem_size; | |
18076 | regs[i].id = ++env->id_gen; | |
18077 | } | |
51c39bb1 AS |
18078 | } |
18079 | } else { | |
18080 | /* 1st arg to a function */ | |
18081 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
18082 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 18083 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
18084 | if (ret == -EFAULT) |
18085 | /* unlikely verifier bug. abort. | |
18086 | * ret == 0 and ret < 0 are sadly acceptable for | |
18087 | * main() function due to backward compatibility. | |
18088 | * Like socket filter program may be written as: | |
18089 | * int bpf_prog(struct pt_regs *ctx) | |
18090 | * and never dereference that ctx in the program. | |
18091 | * 'struct pt_regs' is a type mismatch for socket | |
18092 | * filter that should be using 'struct __sk_buff'. | |
18093 | */ | |
18094 | goto out; | |
18095 | } | |
18096 | ||
18097 | ret = do_check(env); | |
18098 | out: | |
f59bbfc2 AS |
18099 | /* check for NULL is necessary, since cur_state can be freed inside |
18100 | * do_check() under memory pressure. | |
18101 | */ | |
18102 | if (env->cur_state) { | |
18103 | free_verifier_state(env->cur_state, true); | |
18104 | env->cur_state = NULL; | |
18105 | } | |
6f8a57cc AN |
18106 | while (!pop_stack(env, NULL, NULL, false)); |
18107 | if (!ret && pop_log) | |
18108 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 18109 | free_states(env); |
51c39bb1 AS |
18110 | return ret; |
18111 | } | |
18112 | ||
18113 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
18114 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
18115 | * Consider: | |
18116 | * int bar(int); | |
18117 | * int foo(int f) | |
18118 | * { | |
18119 | * return bar(f); | |
18120 | * } | |
18121 | * int bar(int b) | |
18122 | * { | |
18123 | * ... | |
18124 | * } | |
18125 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
18126 | * will be assumed that bar() already verified successfully and call to bar() | |
18127 | * from foo() will be checked for type match only. Later bar() will be verified | |
18128 | * independently to check that it's safe for R1=any_scalar_value. | |
18129 | */ | |
18130 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
18131 | { | |
18132 | struct bpf_prog_aux *aux = env->prog->aux; | |
18133 | int i, ret; | |
18134 | ||
18135 | if (!aux->func_info) | |
18136 | return 0; | |
18137 | ||
18138 | for (i = 1; i < env->subprog_cnt; i++) { | |
18139 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
18140 | continue; | |
18141 | env->insn_idx = env->subprog_info[i].start; | |
18142 | WARN_ON_ONCE(env->insn_idx == 0); | |
18143 | ret = do_check_common(env, i); | |
18144 | if (ret) { | |
18145 | return ret; | |
18146 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
18147 | verbose(env, | |
18148 | "Func#%d is safe for any args that match its prototype\n", | |
18149 | i); | |
18150 | } | |
18151 | } | |
18152 | return 0; | |
18153 | } | |
18154 | ||
18155 | static int do_check_main(struct bpf_verifier_env *env) | |
18156 | { | |
18157 | int ret; | |
18158 | ||
18159 | env->insn_idx = 0; | |
18160 | ret = do_check_common(env, 0); | |
18161 | if (!ret) | |
18162 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
18163 | return ret; | |
18164 | } | |
18165 | ||
18166 | ||
06ee7115 AS |
18167 | static void print_verification_stats(struct bpf_verifier_env *env) |
18168 | { | |
18169 | int i; | |
18170 | ||
18171 | if (env->log.level & BPF_LOG_STATS) { | |
18172 | verbose(env, "verification time %lld usec\n", | |
18173 | div_u64(env->verification_time, 1000)); | |
18174 | verbose(env, "stack depth "); | |
18175 | for (i = 0; i < env->subprog_cnt; i++) { | |
18176 | u32 depth = env->subprog_info[i].stack_depth; | |
18177 | ||
18178 | verbose(env, "%d", depth); | |
18179 | if (i + 1 < env->subprog_cnt) | |
18180 | verbose(env, "+"); | |
18181 | } | |
18182 | verbose(env, "\n"); | |
18183 | } | |
18184 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
18185 | "total_states %d peak_states %d mark_read %d\n", | |
18186 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
18187 | env->max_states_per_insn, env->total_states, | |
18188 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
18189 | } |
18190 | ||
27ae7997 MKL |
18191 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
18192 | { | |
18193 | const struct btf_type *t, *func_proto; | |
18194 | const struct bpf_struct_ops *st_ops; | |
18195 | const struct btf_member *member; | |
18196 | struct bpf_prog *prog = env->prog; | |
18197 | u32 btf_id, member_idx; | |
18198 | const char *mname; | |
18199 | ||
12aa8a94 THJ |
18200 | if (!prog->gpl_compatible) { |
18201 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
18202 | return -EINVAL; | |
18203 | } | |
18204 | ||
27ae7997 MKL |
18205 | btf_id = prog->aux->attach_btf_id; |
18206 | st_ops = bpf_struct_ops_find(btf_id); | |
18207 | if (!st_ops) { | |
18208 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
18209 | btf_id); | |
18210 | return -ENOTSUPP; | |
18211 | } | |
18212 | ||
18213 | t = st_ops->type; | |
18214 | member_idx = prog->expected_attach_type; | |
18215 | if (member_idx >= btf_type_vlen(t)) { | |
18216 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
18217 | member_idx, st_ops->name); | |
18218 | return -EINVAL; | |
18219 | } | |
18220 | ||
18221 | member = &btf_type_member(t)[member_idx]; | |
18222 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
18223 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
18224 | NULL); | |
18225 | if (!func_proto) { | |
18226 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
18227 | mname, member_idx, st_ops->name); | |
18228 | return -EINVAL; | |
18229 | } | |
18230 | ||
18231 | if (st_ops->check_member) { | |
51a52a29 | 18232 | int err = st_ops->check_member(t, member, prog); |
27ae7997 MKL |
18233 | |
18234 | if (err) { | |
18235 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
18236 | mname, st_ops->name); | |
18237 | return err; | |
18238 | } | |
18239 | } | |
18240 | ||
18241 | prog->aux->attach_func_proto = func_proto; | |
18242 | prog->aux->attach_func_name = mname; | |
18243 | env->ops = st_ops->verifier_ops; | |
18244 | ||
18245 | return 0; | |
18246 | } | |
6ba43b76 KS |
18247 | #define SECURITY_PREFIX "security_" |
18248 | ||
f7b12b6f | 18249 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 18250 | { |
69191754 | 18251 | if (within_error_injection_list(addr) || |
f7b12b6f | 18252 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 18253 | return 0; |
6ba43b76 | 18254 | |
6ba43b76 KS |
18255 | return -EINVAL; |
18256 | } | |
27ae7997 | 18257 | |
1e6c62a8 AS |
18258 | /* list of non-sleepable functions that are otherwise on |
18259 | * ALLOW_ERROR_INJECTION list | |
18260 | */ | |
18261 | BTF_SET_START(btf_non_sleepable_error_inject) | |
18262 | /* Three functions below can be called from sleepable and non-sleepable context. | |
18263 | * Assume non-sleepable from bpf safety point of view. | |
18264 | */ | |
9dd3d069 | 18265 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
18266 | BTF_ID(func, should_fail_alloc_page) |
18267 | BTF_ID(func, should_failslab) | |
18268 | BTF_SET_END(btf_non_sleepable_error_inject) | |
18269 | ||
18270 | static int check_non_sleepable_error_inject(u32 btf_id) | |
18271 | { | |
18272 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
18273 | } | |
18274 | ||
f7b12b6f THJ |
18275 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
18276 | const struct bpf_prog *prog, | |
18277 | const struct bpf_prog *tgt_prog, | |
18278 | u32 btf_id, | |
18279 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 18280 | { |
be8704ff | 18281 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 18282 | const char prefix[] = "btf_trace_"; |
5b92a28a | 18283 | int ret = 0, subprog = -1, i; |
38207291 | 18284 | const struct btf_type *t; |
5b92a28a | 18285 | bool conservative = true; |
38207291 | 18286 | const char *tname; |
5b92a28a | 18287 | struct btf *btf; |
f7b12b6f | 18288 | long addr = 0; |
38207291 | 18289 | |
f1b9509c | 18290 | if (!btf_id) { |
efc68158 | 18291 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
18292 | return -EINVAL; |
18293 | } | |
22dc4a0f | 18294 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 18295 | if (!btf) { |
efc68158 | 18296 | bpf_log(log, |
5b92a28a AS |
18297 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
18298 | return -EINVAL; | |
18299 | } | |
18300 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 18301 | if (!t) { |
efc68158 | 18302 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
18303 | return -EINVAL; |
18304 | } | |
5b92a28a | 18305 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 18306 | if (!tname) { |
efc68158 | 18307 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
18308 | return -EINVAL; |
18309 | } | |
5b92a28a AS |
18310 | if (tgt_prog) { |
18311 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
18312 | ||
fd7c211d THJ |
18313 | if (bpf_prog_is_dev_bound(prog->aux) && |
18314 | !bpf_prog_dev_bound_match(prog, tgt_prog)) { | |
18315 | bpf_log(log, "Target program bound device mismatch"); | |
3d76a4d3 SF |
18316 | return -EINVAL; |
18317 | } | |
18318 | ||
5b92a28a AS |
18319 | for (i = 0; i < aux->func_info_cnt; i++) |
18320 | if (aux->func_info[i].type_id == btf_id) { | |
18321 | subprog = i; | |
18322 | break; | |
18323 | } | |
18324 | if (subprog == -1) { | |
efc68158 | 18325 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
18326 | return -EINVAL; |
18327 | } | |
18328 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
18329 | if (prog_extension) { |
18330 | if (conservative) { | |
efc68158 | 18331 | bpf_log(log, |
be8704ff AS |
18332 | "Cannot replace static functions\n"); |
18333 | return -EINVAL; | |
18334 | } | |
18335 | if (!prog->jit_requested) { | |
efc68158 | 18336 | bpf_log(log, |
be8704ff AS |
18337 | "Extension programs should be JITed\n"); |
18338 | return -EINVAL; | |
18339 | } | |
be8704ff AS |
18340 | } |
18341 | if (!tgt_prog->jited) { | |
efc68158 | 18342 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
18343 | return -EINVAL; |
18344 | } | |
18345 | if (tgt_prog->type == prog->type) { | |
18346 | /* Cannot fentry/fexit another fentry/fexit program. | |
18347 | * Cannot attach program extension to another extension. | |
18348 | * It's ok to attach fentry/fexit to extension program. | |
18349 | */ | |
efc68158 | 18350 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
18351 | return -EINVAL; |
18352 | } | |
18353 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
18354 | prog_extension && | |
18355 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
18356 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
18357 | /* Program extensions can extend all program types | |
18358 | * except fentry/fexit. The reason is the following. | |
18359 | * The fentry/fexit programs are used for performance | |
18360 | * analysis, stats and can be attached to any program | |
18361 | * type except themselves. When extension program is | |
18362 | * replacing XDP function it is necessary to allow | |
18363 | * performance analysis of all functions. Both original | |
18364 | * XDP program and its program extension. Hence | |
18365 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
18366 | * allowed. If extending of fentry/fexit was allowed it | |
18367 | * would be possible to create long call chain | |
18368 | * fentry->extension->fentry->extension beyond | |
18369 | * reasonable stack size. Hence extending fentry is not | |
18370 | * allowed. | |
18371 | */ | |
efc68158 | 18372 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
18373 | return -EINVAL; |
18374 | } | |
5b92a28a | 18375 | } else { |
be8704ff | 18376 | if (prog_extension) { |
efc68158 | 18377 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
18378 | return -EINVAL; |
18379 | } | |
5b92a28a | 18380 | } |
f1b9509c AS |
18381 | |
18382 | switch (prog->expected_attach_type) { | |
18383 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 18384 | if (tgt_prog) { |
efc68158 | 18385 | bpf_log(log, |
5b92a28a AS |
18386 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
18387 | return -EINVAL; | |
18388 | } | |
38207291 | 18389 | if (!btf_type_is_typedef(t)) { |
efc68158 | 18390 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
18391 | btf_id); |
18392 | return -EINVAL; | |
18393 | } | |
f1b9509c | 18394 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 18395 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
18396 | btf_id, tname); |
18397 | return -EINVAL; | |
18398 | } | |
18399 | tname += sizeof(prefix) - 1; | |
5b92a28a | 18400 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
18401 | if (!btf_type_is_ptr(t)) |
18402 | /* should never happen in valid vmlinux build */ | |
18403 | return -EINVAL; | |
5b92a28a | 18404 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
18405 | if (!btf_type_is_func_proto(t)) |
18406 | /* should never happen in valid vmlinux build */ | |
18407 | return -EINVAL; | |
18408 | ||
f7b12b6f | 18409 | break; |
15d83c4d YS |
18410 | case BPF_TRACE_ITER: |
18411 | if (!btf_type_is_func(t)) { | |
efc68158 | 18412 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
18413 | btf_id); |
18414 | return -EINVAL; | |
18415 | } | |
18416 | t = btf_type_by_id(btf, t->type); | |
18417 | if (!btf_type_is_func_proto(t)) | |
18418 | return -EINVAL; | |
f7b12b6f THJ |
18419 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
18420 | if (ret) | |
18421 | return ret; | |
18422 | break; | |
be8704ff AS |
18423 | default: |
18424 | if (!prog_extension) | |
18425 | return -EINVAL; | |
df561f66 | 18426 | fallthrough; |
ae240823 | 18427 | case BPF_MODIFY_RETURN: |
9e4e01df | 18428 | case BPF_LSM_MAC: |
69fd337a | 18429 | case BPF_LSM_CGROUP: |
fec56f58 AS |
18430 | case BPF_TRACE_FENTRY: |
18431 | case BPF_TRACE_FEXIT: | |
18432 | if (!btf_type_is_func(t)) { | |
efc68158 | 18433 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
18434 | btf_id); |
18435 | return -EINVAL; | |
18436 | } | |
be8704ff | 18437 | if (prog_extension && |
efc68158 | 18438 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 18439 | return -EINVAL; |
5b92a28a | 18440 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
18441 | if (!btf_type_is_func_proto(t)) |
18442 | return -EINVAL; | |
f7b12b6f | 18443 | |
4a1e7c0c THJ |
18444 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
18445 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
18446 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
18447 | return -EINVAL; | |
18448 | ||
f7b12b6f | 18449 | if (tgt_prog && conservative) |
5b92a28a | 18450 | t = NULL; |
f7b12b6f THJ |
18451 | |
18452 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 18453 | if (ret < 0) |
f7b12b6f THJ |
18454 | return ret; |
18455 | ||
5b92a28a | 18456 | if (tgt_prog) { |
e9eeec58 YS |
18457 | if (subprog == 0) |
18458 | addr = (long) tgt_prog->bpf_func; | |
18459 | else | |
18460 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
18461 | } else { |
18462 | addr = kallsyms_lookup_name(tname); | |
18463 | if (!addr) { | |
efc68158 | 18464 | bpf_log(log, |
5b92a28a AS |
18465 | "The address of function %s cannot be found\n", |
18466 | tname); | |
f7b12b6f | 18467 | return -ENOENT; |
5b92a28a | 18468 | } |
fec56f58 | 18469 | } |
18644cec | 18470 | |
1e6c62a8 AS |
18471 | if (prog->aux->sleepable) { |
18472 | ret = -EINVAL; | |
18473 | switch (prog->type) { | |
18474 | case BPF_PROG_TYPE_TRACING: | |
5b481aca BT |
18475 | |
18476 | /* fentry/fexit/fmod_ret progs can be sleepable if they are | |
1e6c62a8 AS |
18477 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
18478 | */ | |
18479 | if (!check_non_sleepable_error_inject(btf_id) && | |
18480 | within_error_injection_list(addr)) | |
18481 | ret = 0; | |
5b481aca BT |
18482 | /* fentry/fexit/fmod_ret progs can also be sleepable if they are |
18483 | * in the fmodret id set with the KF_SLEEPABLE flag. | |
18484 | */ | |
18485 | else { | |
18486 | u32 *flags = btf_kfunc_is_modify_return(btf, btf_id); | |
18487 | ||
18488 | if (flags && (*flags & KF_SLEEPABLE)) | |
18489 | ret = 0; | |
18490 | } | |
1e6c62a8 AS |
18491 | break; |
18492 | case BPF_PROG_TYPE_LSM: | |
18493 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
18494 | * Only some of them are sleepable. | |
18495 | */ | |
423f1610 | 18496 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
18497 | ret = 0; |
18498 | break; | |
18499 | default: | |
18500 | break; | |
18501 | } | |
f7b12b6f THJ |
18502 | if (ret) { |
18503 | bpf_log(log, "%s is not sleepable\n", tname); | |
18504 | return ret; | |
18505 | } | |
1e6c62a8 | 18506 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 18507 | if (tgt_prog) { |
efc68158 | 18508 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
18509 | return -EINVAL; |
18510 | } | |
5b481aca BT |
18511 | ret = -EINVAL; |
18512 | if (btf_kfunc_is_modify_return(btf, btf_id) || | |
18513 | !check_attach_modify_return(addr, tname)) | |
18514 | ret = 0; | |
f7b12b6f THJ |
18515 | if (ret) { |
18516 | bpf_log(log, "%s() is not modifiable\n", tname); | |
18517 | return ret; | |
1af9270e | 18518 | } |
18644cec | 18519 | } |
f7b12b6f THJ |
18520 | |
18521 | break; | |
18522 | } | |
18523 | tgt_info->tgt_addr = addr; | |
18524 | tgt_info->tgt_name = tname; | |
18525 | tgt_info->tgt_type = t; | |
18526 | return 0; | |
18527 | } | |
18528 | ||
35e3815f JO |
18529 | BTF_SET_START(btf_id_deny) |
18530 | BTF_ID_UNUSED | |
18531 | #ifdef CONFIG_SMP | |
18532 | BTF_ID(func, migrate_disable) | |
18533 | BTF_ID(func, migrate_enable) | |
18534 | #endif | |
18535 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
18536 | BTF_ID(func, rcu_read_unlock_strict) | |
18537 | #endif | |
18538 | BTF_SET_END(btf_id_deny) | |
18539 | ||
700e6f85 JO |
18540 | static bool can_be_sleepable(struct bpf_prog *prog) |
18541 | { | |
18542 | if (prog->type == BPF_PROG_TYPE_TRACING) { | |
18543 | switch (prog->expected_attach_type) { | |
18544 | case BPF_TRACE_FENTRY: | |
18545 | case BPF_TRACE_FEXIT: | |
18546 | case BPF_MODIFY_RETURN: | |
18547 | case BPF_TRACE_ITER: | |
18548 | return true; | |
18549 | default: | |
18550 | return false; | |
18551 | } | |
18552 | } | |
18553 | return prog->type == BPF_PROG_TYPE_LSM || | |
1e12d3ef DV |
18554 | prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ || |
18555 | prog->type == BPF_PROG_TYPE_STRUCT_OPS; | |
700e6f85 JO |
18556 | } |
18557 | ||
f7b12b6f THJ |
18558 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
18559 | { | |
18560 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 18561 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
18562 | struct bpf_attach_target_info tgt_info = {}; |
18563 | u32 btf_id = prog->aux->attach_btf_id; | |
18564 | struct bpf_trampoline *tr; | |
18565 | int ret; | |
18566 | u64 key; | |
18567 | ||
79a7f8bd AS |
18568 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
18569 | if (prog->aux->sleepable) | |
18570 | /* attach_btf_id checked to be zero already */ | |
18571 | return 0; | |
18572 | verbose(env, "Syscall programs can only be sleepable\n"); | |
18573 | return -EINVAL; | |
18574 | } | |
18575 | ||
700e6f85 | 18576 | if (prog->aux->sleepable && !can_be_sleepable(prog)) { |
1e12d3ef | 18577 | verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n"); |
f7b12b6f THJ |
18578 | return -EINVAL; |
18579 | } | |
18580 | ||
18581 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
18582 | return check_struct_ops_btf_id(env); | |
18583 | ||
18584 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
18585 | prog->type != BPF_PROG_TYPE_LSM && | |
18586 | prog->type != BPF_PROG_TYPE_EXT) | |
18587 | return 0; | |
18588 | ||
18589 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
18590 | if (ret) | |
fec56f58 | 18591 | return ret; |
f7b12b6f THJ |
18592 | |
18593 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
18594 | /* to make freplace equivalent to their targets, they need to |
18595 | * inherit env->ops and expected_attach_type for the rest of the | |
18596 | * verification | |
18597 | */ | |
f7b12b6f THJ |
18598 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
18599 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
18600 | } | |
18601 | ||
18602 | /* store info about the attachment target that will be used later */ | |
18603 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
18604 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
18605 | ||
4a1e7c0c THJ |
18606 | if (tgt_prog) { |
18607 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
18608 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
18609 | } | |
18610 | ||
f7b12b6f THJ |
18611 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
18612 | prog->aux->attach_btf_trace = true; | |
18613 | return 0; | |
18614 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
18615 | if (!bpf_iter_prog_supported(prog)) | |
18616 | return -EINVAL; | |
18617 | return 0; | |
18618 | } | |
18619 | ||
18620 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
18621 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
18622 | if (ret < 0) | |
18623 | return ret; | |
35e3815f JO |
18624 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
18625 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
18626 | return -EINVAL; | |
38207291 | 18627 | } |
f7b12b6f | 18628 | |
22dc4a0f | 18629 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
18630 | tr = bpf_trampoline_get(key, &tgt_info); |
18631 | if (!tr) | |
18632 | return -ENOMEM; | |
18633 | ||
3aac1ead | 18634 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 18635 | return 0; |
38207291 MKL |
18636 | } |
18637 | ||
76654e67 AM |
18638 | struct btf *bpf_get_btf_vmlinux(void) |
18639 | { | |
18640 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
18641 | mutex_lock(&bpf_verifier_lock); | |
18642 | if (!btf_vmlinux) | |
18643 | btf_vmlinux = btf_parse_vmlinux(); | |
18644 | mutex_unlock(&bpf_verifier_lock); | |
18645 | } | |
18646 | return btf_vmlinux; | |
18647 | } | |
18648 | ||
af2ac3e1 | 18649 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 18650 | { |
06ee7115 | 18651 | u64 start_time = ktime_get_ns(); |
58e2af8b | 18652 | struct bpf_verifier_env *env; |
b9193c1b | 18653 | struct bpf_verifier_log *log; |
9e4c24e7 | 18654 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 18655 | bool is_priv; |
51580e79 | 18656 | |
eba0c929 AB |
18657 | /* no program is valid */ |
18658 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
18659 | return -EINVAL; | |
18660 | ||
58e2af8b | 18661 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
18662 | * allocate/free it every time bpf_check() is called |
18663 | */ | |
58e2af8b | 18664 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
18665 | if (!env) |
18666 | return -ENOMEM; | |
61bd5218 | 18667 | log = &env->log; |
cbd35700 | 18668 | |
9e4c24e7 | 18669 | len = (*prog)->len; |
fad953ce | 18670 | env->insn_aux_data = |
9e4c24e7 | 18671 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
18672 | ret = -ENOMEM; |
18673 | if (!env->insn_aux_data) | |
18674 | goto err_free_env; | |
9e4c24e7 JK |
18675 | for (i = 0; i < len; i++) |
18676 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 18677 | env->prog = *prog; |
00176a34 | 18678 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 18679 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 18680 | is_priv = bpf_capable(); |
0246e64d | 18681 | |
76654e67 | 18682 | bpf_get_btf_vmlinux(); |
8580ac94 | 18683 | |
cbd35700 | 18684 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
18685 | if (!is_priv) |
18686 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
18687 | |
18688 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
18689 | /* user requested verbose verifier output | |
18690 | * and supplied buffer to store the verification trace | |
18691 | */ | |
e7bf8249 JK |
18692 | log->level = attr->log_level; |
18693 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
18694 | log->len_total = attr->log_size; | |
cbd35700 | 18695 | |
e7bf8249 | 18696 | /* log attributes have to be sane */ |
866de407 HT |
18697 | if (!bpf_verifier_log_attr_valid(log)) { |
18698 | ret = -EINVAL; | |
3df126f3 | 18699 | goto err_unlock; |
866de407 | 18700 | } |
cbd35700 | 18701 | } |
1ad2f583 | 18702 | |
0f55f9ed CL |
18703 | mark_verifier_state_clean(env); |
18704 | ||
8580ac94 AS |
18705 | if (IS_ERR(btf_vmlinux)) { |
18706 | /* Either gcc or pahole or kernel are broken. */ | |
18707 | verbose(env, "in-kernel BTF is malformed\n"); | |
18708 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 18709 | goto skip_full_check; |
8580ac94 AS |
18710 | } |
18711 | ||
1ad2f583 DB |
18712 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
18713 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 18714 | env->strict_alignment = true; |
e9ee9efc DM |
18715 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
18716 | env->strict_alignment = false; | |
cbd35700 | 18717 | |
2c78ee89 | 18718 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 18719 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
2c78ee89 AS |
18720 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
18721 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
18722 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 18723 | |
10d274e8 AS |
18724 | if (is_priv) |
18725 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
18726 | ||
dc2a4ebc | 18727 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 18728 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
18729 | GFP_USER); |
18730 | ret = -ENOMEM; | |
18731 | if (!env->explored_states) | |
18732 | goto skip_full_check; | |
18733 | ||
e6ac2450 MKL |
18734 | ret = add_subprog_and_kfunc(env); |
18735 | if (ret < 0) | |
18736 | goto skip_full_check; | |
18737 | ||
d9762e84 | 18738 | ret = check_subprogs(env); |
475fb78f AS |
18739 | if (ret < 0) |
18740 | goto skip_full_check; | |
18741 | ||
c454a46b | 18742 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
18743 | if (ret < 0) |
18744 | goto skip_full_check; | |
18745 | ||
be8704ff AS |
18746 | ret = check_attach_btf_id(env); |
18747 | if (ret) | |
18748 | goto skip_full_check; | |
18749 | ||
4976b718 HL |
18750 | ret = resolve_pseudo_ldimm64(env); |
18751 | if (ret < 0) | |
18752 | goto skip_full_check; | |
18753 | ||
9d03ebc7 | 18754 | if (bpf_prog_is_offloaded(env->prog->aux)) { |
ceb11679 YZ |
18755 | ret = bpf_prog_offload_verifier_prep(env->prog); |
18756 | if (ret) | |
18757 | goto skip_full_check; | |
18758 | } | |
18759 | ||
d9762e84 MKL |
18760 | ret = check_cfg(env); |
18761 | if (ret < 0) | |
18762 | goto skip_full_check; | |
18763 | ||
51c39bb1 AS |
18764 | ret = do_check_subprogs(env); |
18765 | ret = ret ?: do_check_main(env); | |
cbd35700 | 18766 | |
9d03ebc7 | 18767 | if (ret == 0 && bpf_prog_is_offloaded(env->prog->aux)) |
c941ce9c QM |
18768 | ret = bpf_prog_offload_finalize(env); |
18769 | ||
0246e64d | 18770 | skip_full_check: |
51c39bb1 | 18771 | kvfree(env->explored_states); |
0246e64d | 18772 | |
c131187d | 18773 | if (ret == 0) |
9b38c405 | 18774 | ret = check_max_stack_depth(env); |
c131187d | 18775 | |
9b38c405 | 18776 | /* instruction rewrites happen after this point */ |
1ade2371 EZ |
18777 | if (ret == 0) |
18778 | ret = optimize_bpf_loop(env); | |
18779 | ||
e2ae4ca2 JK |
18780 | if (is_priv) { |
18781 | if (ret == 0) | |
18782 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
18783 | if (ret == 0) |
18784 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
18785 | if (ret == 0) |
18786 | ret = opt_remove_nops(env); | |
52875a04 JK |
18787 | } else { |
18788 | if (ret == 0) | |
18789 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
18790 | } |
18791 | ||
9bac3d6d AS |
18792 | if (ret == 0) |
18793 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
18794 | ret = convert_ctx_accesses(env); | |
18795 | ||
e245c5c6 | 18796 | if (ret == 0) |
e6ac5933 | 18797 | ret = do_misc_fixups(env); |
e245c5c6 | 18798 | |
a4b1d3c1 JW |
18799 | /* do 32-bit optimization after insn patching has done so those patched |
18800 | * insns could be handled correctly. | |
18801 | */ | |
9d03ebc7 | 18802 | if (ret == 0 && !bpf_prog_is_offloaded(env->prog->aux)) { |
d6c2308c JW |
18803 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
18804 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
18805 | : false; | |
a4b1d3c1 JW |
18806 | } |
18807 | ||
1ea47e01 AS |
18808 | if (ret == 0) |
18809 | ret = fixup_call_args(env); | |
18810 | ||
06ee7115 AS |
18811 | env->verification_time = ktime_get_ns() - start_time; |
18812 | print_verification_stats(env); | |
aba64c7d | 18813 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 18814 | |
a2a7d570 | 18815 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 18816 | ret = -ENOSPC; |
a2a7d570 | 18817 | if (log->level && !log->ubuf) { |
cbd35700 | 18818 | ret = -EFAULT; |
a2a7d570 | 18819 | goto err_release_maps; |
cbd35700 AS |
18820 | } |
18821 | ||
541c3bad AN |
18822 | if (ret) |
18823 | goto err_release_maps; | |
18824 | ||
18825 | if (env->used_map_cnt) { | |
0246e64d | 18826 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
18827 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
18828 | sizeof(env->used_maps[0]), | |
18829 | GFP_KERNEL); | |
0246e64d | 18830 | |
9bac3d6d | 18831 | if (!env->prog->aux->used_maps) { |
0246e64d | 18832 | ret = -ENOMEM; |
a2a7d570 | 18833 | goto err_release_maps; |
0246e64d AS |
18834 | } |
18835 | ||
9bac3d6d | 18836 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 18837 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 18838 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
18839 | } |
18840 | if (env->used_btf_cnt) { | |
18841 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
18842 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
18843 | sizeof(env->used_btfs[0]), | |
18844 | GFP_KERNEL); | |
18845 | if (!env->prog->aux->used_btfs) { | |
18846 | ret = -ENOMEM; | |
18847 | goto err_release_maps; | |
18848 | } | |
0246e64d | 18849 | |
541c3bad AN |
18850 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
18851 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
18852 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
18853 | } | |
18854 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
18855 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
18856 | * bpf_ld_imm64 instructions | |
18857 | */ | |
18858 | convert_pseudo_ld_imm64(env); | |
18859 | } | |
cbd35700 | 18860 | |
541c3bad | 18861 | adjust_btf_func(env); |
ba64e7d8 | 18862 | |
a2a7d570 | 18863 | err_release_maps: |
9bac3d6d | 18864 | if (!env->prog->aux->used_maps) |
0246e64d | 18865 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 18866 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
18867 | */ |
18868 | release_maps(env); | |
541c3bad AN |
18869 | if (!env->prog->aux->used_btfs) |
18870 | release_btfs(env); | |
03f87c0b THJ |
18871 | |
18872 | /* extension progs temporarily inherit the attach_type of their targets | |
18873 | for verification purposes, so set it back to zero before returning | |
18874 | */ | |
18875 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
18876 | env->prog->expected_attach_type = 0; | |
18877 | ||
9bac3d6d | 18878 | *prog = env->prog; |
3df126f3 | 18879 | err_unlock: |
45a73c17 AS |
18880 | if (!is_priv) |
18881 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
18882 | vfree(env->insn_aux_data); |
18883 | err_free_env: | |
18884 | kfree(env); | |
51580e79 AS |
18885 | return ret; |
18886 | } |