Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
51580e79 | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
969bf05e | 3 | * Copyright (c) 2016 Facebook |
fd978bf7 | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
51580e79 | 5 | */ |
838e9690 | 6 | #include <uapi/linux/btf.h> |
aef2feda | 7 | #include <linux/bpf-cgroup.h> |
51580e79 AS |
8 | #include <linux/kernel.h> |
9 | #include <linux/types.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/bpf.h> | |
838e9690 | 12 | #include <linux/btf.h> |
58e2af8b | 13 | #include <linux/bpf_verifier.h> |
51580e79 AS |
14 | #include <linux/filter.h> |
15 | #include <net/netlink.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/vmalloc.h> | |
ebb676da | 18 | #include <linux/stringify.h> |
cc8b0b92 AS |
19 | #include <linux/bsearch.h> |
20 | #include <linux/sort.h> | |
c195651e | 21 | #include <linux/perf_event.h> |
d9762e84 | 22 | #include <linux/ctype.h> |
6ba43b76 | 23 | #include <linux/error-injection.h> |
9e4e01df | 24 | #include <linux/bpf_lsm.h> |
1e6c62a8 | 25 | #include <linux/btf_ids.h> |
47e34cb7 | 26 | #include <linux/poison.h> |
bd5314f8 | 27 | #include <linux/module.h> |
51580e79 | 28 | |
f4ac7e0b JK |
29 | #include "disasm.h" |
30 | ||
00176a34 | 31 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 32 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
33 | [_id] = & _name ## _verifier_ops, |
34 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 35 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
36 | #include <linux/bpf_types.h> |
37 | #undef BPF_PROG_TYPE | |
38 | #undef BPF_MAP_TYPE | |
f2e10bff | 39 | #undef BPF_LINK_TYPE |
00176a34 JK |
40 | }; |
41 | ||
51580e79 AS |
42 | /* bpf_check() is a static code analyzer that walks eBPF program |
43 | * instruction by instruction and updates register/stack state. | |
44 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
45 | * | |
46 | * The first pass is depth-first-search to check that the program is a DAG. | |
47 | * It rejects the following programs: | |
48 | * - larger than BPF_MAXINSNS insns | |
49 | * - if loop is present (detected via back-edge) | |
50 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
51 | * - out of bounds or malformed jumps | |
52 | * The second pass is all possible path descent from the 1st insn. | |
8fb33b60 | 53 | * Since it's analyzing all paths through the program, the length of the |
eba38a96 | 54 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
55 | * insn is less then 4K, but there are too many branches that change stack/regs. |
56 | * Number of 'branches to be analyzed' is limited to 1k | |
57 | * | |
58 | * On entry to each instruction, each register has a type, and the instruction | |
59 | * changes the types of the registers depending on instruction semantics. | |
60 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
61 | * copied to R1. | |
62 | * | |
63 | * All registers are 64-bit. | |
64 | * R0 - return register | |
65 | * R1-R5 argument passing registers | |
66 | * R6-R9 callee saved registers | |
67 | * R10 - frame pointer read-only | |
68 | * | |
69 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
70 | * and has type PTR_TO_CTX. | |
71 | * | |
72 | * Verifier tracks arithmetic operations on pointers in case: | |
73 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
74 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
75 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
76 | * and 2nd arithmetic instruction is pattern matched to recognize | |
77 | * that it wants to construct a pointer to some element within stack. | |
78 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
79 | * (and -20 constant is saved for further stack bounds checking). | |
80 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
81 | * | |
f1174f77 | 82 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 83 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 84 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
85 | * |
86 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
87 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
88 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
89 | * |
90 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
91 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
92 | * | |
93 | * registers used to pass values to function calls are checked against | |
94 | * function argument constraints. | |
95 | * | |
96 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
97 | * It means that the register type passed to this function must be | |
98 | * PTR_TO_STACK and it will be used inside the function as | |
99 | * 'pointer to map element key' | |
100 | * | |
101 | * For example the argument constraints for bpf_map_lookup_elem(): | |
102 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
103 | * .arg1_type = ARG_CONST_MAP_PTR, | |
104 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
105 | * | |
106 | * ret_type says that this function returns 'pointer to map elem value or null' | |
107 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
108 | * 2nd argument should be a pointer to stack, which will be used inside | |
109 | * the helper function as a pointer to map element key. | |
110 | * | |
111 | * On the kernel side the helper function looks like: | |
112 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
113 | * { | |
114 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
115 | * void *key = (void *) (unsigned long) r2; | |
116 | * void *value; | |
117 | * | |
118 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
119 | * [key, key + map->key_size) bytes are valid and were initialized on | |
120 | * the stack of eBPF program. | |
121 | * } | |
122 | * | |
123 | * Corresponding eBPF program may look like: | |
124 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
125 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
126 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
127 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
128 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
129 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
130 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
131 | * | |
132 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
133 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
134 | * and were initialized prior to this call. | |
135 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
136 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
137 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
8fb33b60 | 138 | * returns either pointer to map value or NULL. |
51580e79 AS |
139 | * |
140 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
141 | * insn, the register holding that pointer in the true branch changes state to | |
142 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
143 | * branch. See check_cond_jmp_op(). | |
144 | * | |
145 | * After the call R0 is set to return type of the function and registers R1-R5 | |
146 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
147 | * |
148 | * The following reference types represent a potential reference to a kernel | |
149 | * resource which, after first being allocated, must be checked and freed by | |
150 | * the BPF program: | |
151 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
152 | * | |
153 | * When the verifier sees a helper call return a reference type, it allocates a | |
154 | * pointer id for the reference and stores it in the current function state. | |
155 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
156 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
157 | * passes through a NULL-check conditional. For the branch wherein the state is | |
158 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
159 | * |
160 | * For each helper function that allocates a reference, such as | |
161 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
162 | * bpf_sk_release(). When a reference type passes into the release function, | |
163 | * the verifier also releases the reference. If any unchecked or unreleased | |
164 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
165 | */ |
166 | ||
17a52670 | 167 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 168 | struct bpf_verifier_stack_elem { |
17a52670 AS |
169 | /* verifer state is 'st' |
170 | * before processing instruction 'insn_idx' | |
171 | * and after processing instruction 'prev_insn_idx' | |
172 | */ | |
58e2af8b | 173 | struct bpf_verifier_state st; |
17a52670 AS |
174 | int insn_idx; |
175 | int prev_insn_idx; | |
58e2af8b | 176 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
177 | /* length of verifier log at the time this state was pushed on stack */ |
178 | u32 log_pos; | |
cbd35700 AS |
179 | }; |
180 | ||
b285fcb7 | 181 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 182 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 183 | |
d2e4c1e6 DB |
184 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
185 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
186 | ||
c93552c4 DB |
187 | #define BPF_MAP_PTR_UNPRIV 1UL |
188 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
189 | POISON_POINTER_DELTA)) | |
190 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
191 | ||
bc34dee6 JK |
192 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx); |
193 | static int release_reference(struct bpf_verifier_env *env, int ref_obj_id); | |
6a3cd331 | 194 | static void invalidate_non_owning_refs(struct bpf_verifier_env *env); |
5d92ddc3 | 195 | static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env); |
6a3cd331 DM |
196 | static int ref_set_non_owning(struct bpf_verifier_env *env, |
197 | struct bpf_reg_state *reg); | |
1cf3bfc6 IL |
198 | static void specialize_kfunc(struct bpf_verifier_env *env, |
199 | u32 func_id, u16 offset, unsigned long *addr); | |
bc34dee6 | 200 | |
c93552c4 DB |
201 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
202 | { | |
d2e4c1e6 | 203 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
204 | } |
205 | ||
206 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
207 | { | |
d2e4c1e6 | 208 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
209 | } |
210 | ||
211 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
212 | const struct bpf_map *map, bool unpriv) | |
213 | { | |
214 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
215 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
216 | aux->map_ptr_state = (unsigned long)map | |
217 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
218 | } | |
219 | ||
220 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
221 | { | |
222 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
223 | } | |
224 | ||
225 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
226 | { | |
227 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
228 | } | |
229 | ||
230 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
231 | { | |
232 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
233 | } | |
234 | ||
235 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
236 | { | |
237 | bool poisoned = bpf_map_key_poisoned(aux); | |
238 | ||
239 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
240 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 241 | } |
fad73a1a | 242 | |
23a2d70c YS |
243 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
244 | { | |
245 | return insn->code == (BPF_JMP | BPF_CALL) && | |
246 | insn->src_reg == BPF_PSEUDO_CALL; | |
247 | } | |
248 | ||
e6ac2450 MKL |
249 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
250 | { | |
251 | return insn->code == (BPF_JMP | BPF_CALL) && | |
252 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; | |
253 | } | |
254 | ||
33ff9823 DB |
255 | struct bpf_call_arg_meta { |
256 | struct bpf_map *map_ptr; | |
435faee1 | 257 | bool raw_mode; |
36bbef52 | 258 | bool pkt_access; |
8f14852e | 259 | u8 release_regno; |
435faee1 DB |
260 | int regno; |
261 | int access_size; | |
457f4436 | 262 | int mem_size; |
10060503 | 263 | u64 msize_max_value; |
1b986589 | 264 | int ref_obj_id; |
f8064ab9 | 265 | int dynptr_id; |
3e8ce298 | 266 | int map_uid; |
d83525ca | 267 | int func_id; |
22dc4a0f | 268 | struct btf *btf; |
eaa6bcb7 | 269 | u32 btf_id; |
22dc4a0f | 270 | struct btf *ret_btf; |
eaa6bcb7 | 271 | u32 ret_btf_id; |
69c087ba | 272 | u32 subprogno; |
aa3496ac | 273 | struct btf_field *kptr_field; |
33ff9823 DB |
274 | }; |
275 | ||
7c50b1cb DM |
276 | struct btf_and_id { |
277 | struct btf *btf; | |
278 | u32 btf_id; | |
279 | }; | |
280 | ||
d0e1ac22 AN |
281 | struct bpf_kfunc_call_arg_meta { |
282 | /* In parameters */ | |
283 | struct btf *btf; | |
284 | u32 func_id; | |
285 | u32 kfunc_flags; | |
286 | const struct btf_type *func_proto; | |
287 | const char *func_name; | |
288 | /* Out parameters */ | |
289 | u32 ref_obj_id; | |
290 | u8 release_regno; | |
291 | bool r0_rdonly; | |
292 | u32 ret_btf_id; | |
293 | u64 r0_size; | |
294 | u32 subprogno; | |
295 | struct { | |
296 | u64 value; | |
297 | bool found; | |
298 | } arg_constant; | |
7c50b1cb DM |
299 | union { |
300 | struct btf_and_id arg_obj_drop; | |
301 | struct btf_and_id arg_refcount_acquire; | |
302 | }; | |
d0e1ac22 AN |
303 | struct { |
304 | struct btf_field *field; | |
305 | } arg_list_head; | |
306 | struct { | |
307 | struct btf_field *field; | |
308 | } arg_rbtree_root; | |
309 | struct { | |
310 | enum bpf_dynptr_type type; | |
311 | u32 id; | |
361f129f | 312 | u32 ref_obj_id; |
d0e1ac22 | 313 | } initialized_dynptr; |
06accc87 AN |
314 | struct { |
315 | u8 spi; | |
316 | u8 frameno; | |
317 | } iter; | |
d0e1ac22 AN |
318 | u64 mem_size; |
319 | }; | |
320 | ||
8580ac94 AS |
321 | struct btf *btf_vmlinux; |
322 | ||
cbd35700 AS |
323 | static DEFINE_MUTEX(bpf_verifier_lock); |
324 | ||
d9762e84 MKL |
325 | static const struct bpf_line_info * |
326 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
327 | { | |
328 | const struct bpf_line_info *linfo; | |
329 | const struct bpf_prog *prog; | |
330 | u32 i, nr_linfo; | |
331 | ||
332 | prog = env->prog; | |
333 | nr_linfo = prog->aux->nr_linfo; | |
334 | ||
335 | if (!nr_linfo || insn_off >= prog->len) | |
336 | return NULL; | |
337 | ||
338 | linfo = prog->aux->linfo; | |
339 | for (i = 1; i < nr_linfo; i++) | |
340 | if (insn_off < linfo[i].insn_off) | |
341 | break; | |
342 | ||
343 | return &linfo[i - 1]; | |
344 | } | |
345 | ||
abe08840 JO |
346 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
347 | { | |
77d2e05a | 348 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
349 | va_list args; |
350 | ||
77d2e05a MKL |
351 | if (!bpf_verifier_log_needed(&env->log)) |
352 | return; | |
353 | ||
abe08840 | 354 | va_start(args, fmt); |
77d2e05a | 355 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
356 | va_end(args); |
357 | } | |
cbd35700 | 358 | |
d9762e84 MKL |
359 | static const char *ltrim(const char *s) |
360 | { | |
361 | while (isspace(*s)) | |
362 | s++; | |
363 | ||
364 | return s; | |
365 | } | |
366 | ||
367 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
368 | u32 insn_off, | |
369 | const char *prefix_fmt, ...) | |
370 | { | |
371 | const struct bpf_line_info *linfo; | |
372 | ||
373 | if (!bpf_verifier_log_needed(&env->log)) | |
374 | return; | |
375 | ||
376 | linfo = find_linfo(env, insn_off); | |
377 | if (!linfo || linfo == env->prev_linfo) | |
378 | return; | |
379 | ||
380 | if (prefix_fmt) { | |
381 | va_list args; | |
382 | ||
383 | va_start(args, prefix_fmt); | |
384 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
385 | va_end(args); | |
386 | } | |
387 | ||
388 | verbose(env, "%s\n", | |
389 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
390 | linfo->line_off))); | |
391 | ||
392 | env->prev_linfo = linfo; | |
393 | } | |
394 | ||
bc2591d6 YS |
395 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
396 | struct bpf_reg_state *reg, | |
397 | struct tnum *range, const char *ctx, | |
398 | const char *reg_name) | |
399 | { | |
400 | char tn_buf[48]; | |
401 | ||
402 | verbose(env, "At %s the register %s ", ctx, reg_name); | |
403 | if (!tnum_is_unknown(reg->var_off)) { | |
404 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
405 | verbose(env, "has value %s", tn_buf); | |
406 | } else { | |
407 | verbose(env, "has unknown scalar value"); | |
408 | } | |
409 | tnum_strn(tn_buf, sizeof(tn_buf), *range); | |
410 | verbose(env, " should have been in %s\n", tn_buf); | |
411 | } | |
412 | ||
de8f3a83 DB |
413 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
414 | { | |
0c9a7a7e | 415 | type = base_type(type); |
de8f3a83 DB |
416 | return type == PTR_TO_PACKET || |
417 | type == PTR_TO_PACKET_META; | |
418 | } | |
419 | ||
46f8bc92 MKL |
420 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
421 | { | |
422 | return type == PTR_TO_SOCKET || | |
655a51e5 | 423 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
424 | type == PTR_TO_TCP_SOCK || |
425 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
426 | } |
427 | ||
1057d299 AS |
428 | static bool type_may_be_null(u32 type) |
429 | { | |
430 | return type & PTR_MAYBE_NULL; | |
431 | } | |
432 | ||
cac616db JF |
433 | static bool reg_type_not_null(enum bpf_reg_type type) |
434 | { | |
1057d299 AS |
435 | if (type_may_be_null(type)) |
436 | return false; | |
437 | ||
438 | type = base_type(type); | |
cac616db JF |
439 | return type == PTR_TO_SOCKET || |
440 | type == PTR_TO_TCP_SOCK || | |
441 | type == PTR_TO_MAP_VALUE || | |
69c087ba | 442 | type == PTR_TO_MAP_KEY || |
d5271c5b AN |
443 | type == PTR_TO_SOCK_COMMON || |
444 | type == PTR_TO_MEM; | |
cac616db JF |
445 | } |
446 | ||
d8939cb0 DM |
447 | static bool type_is_ptr_alloc_obj(u32 type) |
448 | { | |
449 | return base_type(type) == PTR_TO_BTF_ID && type_flag(type) & MEM_ALLOC; | |
450 | } | |
451 | ||
6a3cd331 DM |
452 | static bool type_is_non_owning_ref(u32 type) |
453 | { | |
454 | return type_is_ptr_alloc_obj(type) && type_flag(type) & NON_OWN_REF; | |
455 | } | |
456 | ||
4e814da0 KKD |
457 | static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg) |
458 | { | |
459 | struct btf_record *rec = NULL; | |
460 | struct btf_struct_meta *meta; | |
461 | ||
462 | if (reg->type == PTR_TO_MAP_VALUE) { | |
463 | rec = reg->map_ptr->record; | |
d8939cb0 | 464 | } else if (type_is_ptr_alloc_obj(reg->type)) { |
4e814da0 KKD |
465 | meta = btf_find_struct_meta(reg->btf, reg->btf_id); |
466 | if (meta) | |
467 | rec = meta->record; | |
468 | } | |
469 | return rec; | |
470 | } | |
471 | ||
d83525ca AS |
472 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
473 | { | |
4e814da0 | 474 | return btf_record_has_field(reg_btf_record(reg), BPF_SPIN_LOCK); |
cba368c1 MKL |
475 | } |
476 | ||
20b2aff4 HL |
477 | static bool type_is_rdonly_mem(u32 type) |
478 | { | |
479 | return type & MEM_RDONLY; | |
cba368c1 MKL |
480 | } |
481 | ||
64d85290 JS |
482 | static bool is_acquire_function(enum bpf_func_id func_id, |
483 | const struct bpf_map *map) | |
484 | { | |
485 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
486 | ||
487 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
488 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 | 489 | func_id == BPF_FUNC_skc_lookup_tcp || |
c0a5a21c KKD |
490 | func_id == BPF_FUNC_ringbuf_reserve || |
491 | func_id == BPF_FUNC_kptr_xchg) | |
64d85290 JS |
492 | return true; |
493 | ||
494 | if (func_id == BPF_FUNC_map_lookup_elem && | |
495 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
496 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
497 | return true; | |
498 | ||
499 | return false; | |
46f8bc92 MKL |
500 | } |
501 | ||
1b986589 MKL |
502 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
503 | { | |
504 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
505 | func_id == BPF_FUNC_sk_fullsock || |
506 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
507 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
508 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
3bc253c2 | 509 | func_id == BPF_FUNC_skc_to_mptcp_sock || |
1df8f55a MKL |
510 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
511 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
512 | } |
513 | ||
88374342 | 514 | static bool is_dynptr_ref_function(enum bpf_func_id func_id) |
b2d8ef19 DM |
515 | { |
516 | return func_id == BPF_FUNC_dynptr_data; | |
517 | } | |
518 | ||
be2ef816 AN |
519 | static bool is_callback_calling_function(enum bpf_func_id func_id) |
520 | { | |
521 | return func_id == BPF_FUNC_for_each_map_elem || | |
522 | func_id == BPF_FUNC_timer_set_callback || | |
523 | func_id == BPF_FUNC_find_vma || | |
524 | func_id == BPF_FUNC_loop || | |
525 | func_id == BPF_FUNC_user_ringbuf_drain; | |
526 | } | |
527 | ||
9bb00b28 YS |
528 | static bool is_storage_get_function(enum bpf_func_id func_id) |
529 | { | |
530 | return func_id == BPF_FUNC_sk_storage_get || | |
531 | func_id == BPF_FUNC_inode_storage_get || | |
532 | func_id == BPF_FUNC_task_storage_get || | |
533 | func_id == BPF_FUNC_cgrp_storage_get; | |
534 | } | |
535 | ||
b2d8ef19 DM |
536 | static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id, |
537 | const struct bpf_map *map) | |
538 | { | |
539 | int ref_obj_uses = 0; | |
540 | ||
541 | if (is_ptr_cast_function(func_id)) | |
542 | ref_obj_uses++; | |
543 | if (is_acquire_function(func_id, map)) | |
544 | ref_obj_uses++; | |
88374342 | 545 | if (is_dynptr_ref_function(func_id)) |
b2d8ef19 DM |
546 | ref_obj_uses++; |
547 | ||
548 | return ref_obj_uses > 1; | |
549 | } | |
550 | ||
39491867 BJ |
551 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
552 | { | |
553 | return BPF_CLASS(insn->code) == BPF_STX && | |
554 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
555 | insn->imm == BPF_CMPXCHG; | |
556 | } | |
557 | ||
c25b2ae1 HL |
558 | /* string representation of 'enum bpf_reg_type' |
559 | * | |
560 | * Note that reg_type_str() can not appear more than once in a single verbose() | |
561 | * statement. | |
562 | */ | |
563 | static const char *reg_type_str(struct bpf_verifier_env *env, | |
564 | enum bpf_reg_type type) | |
565 | { | |
ef66c547 | 566 | char postfix[16] = {0}, prefix[64] = {0}; |
c25b2ae1 HL |
567 | static const char * const str[] = { |
568 | [NOT_INIT] = "?", | |
7df5072c | 569 | [SCALAR_VALUE] = "scalar", |
c25b2ae1 HL |
570 | [PTR_TO_CTX] = "ctx", |
571 | [CONST_PTR_TO_MAP] = "map_ptr", | |
572 | [PTR_TO_MAP_VALUE] = "map_value", | |
573 | [PTR_TO_STACK] = "fp", | |
574 | [PTR_TO_PACKET] = "pkt", | |
575 | [PTR_TO_PACKET_META] = "pkt_meta", | |
576 | [PTR_TO_PACKET_END] = "pkt_end", | |
577 | [PTR_TO_FLOW_KEYS] = "flow_keys", | |
578 | [PTR_TO_SOCKET] = "sock", | |
579 | [PTR_TO_SOCK_COMMON] = "sock_common", | |
580 | [PTR_TO_TCP_SOCK] = "tcp_sock", | |
581 | [PTR_TO_TP_BUFFER] = "tp_buffer", | |
582 | [PTR_TO_XDP_SOCK] = "xdp_sock", | |
583 | [PTR_TO_BTF_ID] = "ptr_", | |
c25b2ae1 | 584 | [PTR_TO_MEM] = "mem", |
20b2aff4 | 585 | [PTR_TO_BUF] = "buf", |
c25b2ae1 HL |
586 | [PTR_TO_FUNC] = "func", |
587 | [PTR_TO_MAP_KEY] = "map_key", | |
27060531 | 588 | [CONST_PTR_TO_DYNPTR] = "dynptr_ptr", |
c25b2ae1 HL |
589 | }; |
590 | ||
591 | if (type & PTR_MAYBE_NULL) { | |
5844101a | 592 | if (base_type(type) == PTR_TO_BTF_ID) |
c25b2ae1 HL |
593 | strncpy(postfix, "or_null_", 16); |
594 | else | |
595 | strncpy(postfix, "_or_null", 16); | |
596 | } | |
597 | ||
9bb00b28 | 598 | snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s", |
ef66c547 DV |
599 | type & MEM_RDONLY ? "rdonly_" : "", |
600 | type & MEM_RINGBUF ? "ringbuf_" : "", | |
601 | type & MEM_USER ? "user_" : "", | |
602 | type & MEM_PERCPU ? "percpu_" : "", | |
9bb00b28 | 603 | type & MEM_RCU ? "rcu_" : "", |
3f00c523 DV |
604 | type & PTR_UNTRUSTED ? "untrusted_" : "", |
605 | type & PTR_TRUSTED ? "trusted_" : "" | |
ef66c547 | 606 | ); |
20b2aff4 HL |
607 | |
608 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", | |
609 | prefix, str[base_type(type)], postfix); | |
c25b2ae1 HL |
610 | return env->type_str_buf; |
611 | } | |
17a52670 | 612 | |
8efea21d EC |
613 | static char slot_type_char[] = { |
614 | [STACK_INVALID] = '?', | |
615 | [STACK_SPILL] = 'r', | |
616 | [STACK_MISC] = 'm', | |
617 | [STACK_ZERO] = '0', | |
97e03f52 | 618 | [STACK_DYNPTR] = 'd', |
06accc87 | 619 | [STACK_ITER] = 'i', |
8efea21d EC |
620 | }; |
621 | ||
4e92024a AS |
622 | static void print_liveness(struct bpf_verifier_env *env, |
623 | enum bpf_reg_liveness live) | |
624 | { | |
9242b5f5 | 625 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
626 | verbose(env, "_"); |
627 | if (live & REG_LIVE_READ) | |
628 | verbose(env, "r"); | |
629 | if (live & REG_LIVE_WRITTEN) | |
630 | verbose(env, "w"); | |
9242b5f5 AS |
631 | if (live & REG_LIVE_DONE) |
632 | verbose(env, "D"); | |
4e92024a AS |
633 | } |
634 | ||
79168a66 | 635 | static int __get_spi(s32 off) |
97e03f52 JK |
636 | { |
637 | return (-off - 1) / BPF_REG_SIZE; | |
638 | } | |
639 | ||
f5b625e5 KKD |
640 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
641 | const struct bpf_reg_state *reg) | |
642 | { | |
643 | struct bpf_verifier_state *cur = env->cur_state; | |
644 | ||
645 | return cur->frame[reg->frameno]; | |
646 | } | |
647 | ||
97e03f52 JK |
648 | static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots) |
649 | { | |
f5b625e5 | 650 | int allocated_slots = state->allocated_stack / BPF_REG_SIZE; |
97e03f52 | 651 | |
f5b625e5 KKD |
652 | /* We need to check that slots between [spi - nr_slots + 1, spi] are |
653 | * within [0, allocated_stack). | |
654 | * | |
655 | * Please note that the spi grows downwards. For example, a dynptr | |
656 | * takes the size of two stack slots; the first slot will be at | |
657 | * spi and the second slot will be at spi - 1. | |
658 | */ | |
659 | return spi - nr_slots + 1 >= 0 && spi < allocated_slots; | |
97e03f52 JK |
660 | } |
661 | ||
a461f5ad AN |
662 | static int stack_slot_obj_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
663 | const char *obj_kind, int nr_slots) | |
f4d7e40a | 664 | { |
79168a66 | 665 | int off, spi; |
f4d7e40a | 666 | |
79168a66 | 667 | if (!tnum_is_const(reg->var_off)) { |
a461f5ad | 668 | verbose(env, "%s has to be at a constant offset\n", obj_kind); |
79168a66 KKD |
669 | return -EINVAL; |
670 | } | |
671 | ||
672 | off = reg->off + reg->var_off.value; | |
673 | if (off % BPF_REG_SIZE) { | |
a461f5ad | 674 | verbose(env, "cannot pass in %s at an offset=%d\n", obj_kind, off); |
79168a66 KKD |
675 | return -EINVAL; |
676 | } | |
677 | ||
678 | spi = __get_spi(off); | |
a461f5ad AN |
679 | if (spi + 1 < nr_slots) { |
680 | verbose(env, "cannot pass in %s at an offset=%d\n", obj_kind, off); | |
79168a66 KKD |
681 | return -EINVAL; |
682 | } | |
97e03f52 | 683 | |
a461f5ad | 684 | if (!is_spi_bounds_valid(func(env, reg), spi, nr_slots)) |
f5b625e5 KKD |
685 | return -ERANGE; |
686 | return spi; | |
f4d7e40a AS |
687 | } |
688 | ||
a461f5ad AN |
689 | static int dynptr_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
690 | { | |
691 | return stack_slot_obj_get_spi(env, reg, "dynptr", BPF_DYNPTR_NR_SLOTS); | |
692 | } | |
693 | ||
06accc87 AN |
694 | static int iter_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int nr_slots) |
695 | { | |
696 | return stack_slot_obj_get_spi(env, reg, "iter", nr_slots); | |
697 | } | |
698 | ||
b32a5dae | 699 | static const char *btf_type_name(const struct btf *btf, u32 id) |
9e15db66 | 700 | { |
22dc4a0f | 701 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
702 | } |
703 | ||
d54e0f6c AN |
704 | static const char *dynptr_type_str(enum bpf_dynptr_type type) |
705 | { | |
706 | switch (type) { | |
707 | case BPF_DYNPTR_TYPE_LOCAL: | |
708 | return "local"; | |
709 | case BPF_DYNPTR_TYPE_RINGBUF: | |
710 | return "ringbuf"; | |
711 | case BPF_DYNPTR_TYPE_SKB: | |
712 | return "skb"; | |
713 | case BPF_DYNPTR_TYPE_XDP: | |
714 | return "xdp"; | |
715 | case BPF_DYNPTR_TYPE_INVALID: | |
716 | return "<invalid>"; | |
717 | default: | |
718 | WARN_ONCE(1, "unknown dynptr type %d\n", type); | |
719 | return "<unknown>"; | |
720 | } | |
721 | } | |
722 | ||
06accc87 AN |
723 | static const char *iter_type_str(const struct btf *btf, u32 btf_id) |
724 | { | |
725 | if (!btf || btf_id == 0) | |
726 | return "<invalid>"; | |
727 | ||
728 | /* we already validated that type is valid and has conforming name */ | |
b32a5dae | 729 | return btf_type_name(btf, btf_id) + sizeof(ITER_PREFIX) - 1; |
06accc87 AN |
730 | } |
731 | ||
732 | static const char *iter_state_str(enum bpf_iter_state state) | |
733 | { | |
734 | switch (state) { | |
735 | case BPF_ITER_STATE_ACTIVE: | |
736 | return "active"; | |
737 | case BPF_ITER_STATE_DRAINED: | |
738 | return "drained"; | |
739 | case BPF_ITER_STATE_INVALID: | |
740 | return "<invalid>"; | |
741 | default: | |
742 | WARN_ONCE(1, "unknown iter state %d\n", state); | |
743 | return "<unknown>"; | |
744 | } | |
745 | } | |
746 | ||
0f55f9ed CL |
747 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
748 | { | |
749 | env->scratched_regs |= 1U << regno; | |
750 | } | |
751 | ||
752 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
753 | { | |
343e5375 | 754 | env->scratched_stack_slots |= 1ULL << spi; |
0f55f9ed CL |
755 | } |
756 | ||
757 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
758 | { | |
759 | return (env->scratched_regs >> regno) & 1; | |
760 | } | |
761 | ||
762 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
763 | { | |
764 | return (env->scratched_stack_slots >> regno) & 1; | |
765 | } | |
766 | ||
767 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
768 | { | |
769 | return env->scratched_regs || env->scratched_stack_slots; | |
770 | } | |
771 | ||
772 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
773 | { | |
774 | env->scratched_regs = 0U; | |
343e5375 | 775 | env->scratched_stack_slots = 0ULL; |
0f55f9ed CL |
776 | } |
777 | ||
778 | /* Used for printing the entire verifier state. */ | |
779 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
780 | { | |
781 | env->scratched_regs = ~0U; | |
343e5375 | 782 | env->scratched_stack_slots = ~0ULL; |
0f55f9ed CL |
783 | } |
784 | ||
97e03f52 JK |
785 | static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type) |
786 | { | |
787 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
788 | case DYNPTR_TYPE_LOCAL: | |
789 | return BPF_DYNPTR_TYPE_LOCAL; | |
bc34dee6 JK |
790 | case DYNPTR_TYPE_RINGBUF: |
791 | return BPF_DYNPTR_TYPE_RINGBUF; | |
b5964b96 JK |
792 | case DYNPTR_TYPE_SKB: |
793 | return BPF_DYNPTR_TYPE_SKB; | |
05421aec JK |
794 | case DYNPTR_TYPE_XDP: |
795 | return BPF_DYNPTR_TYPE_XDP; | |
97e03f52 JK |
796 | default: |
797 | return BPF_DYNPTR_TYPE_INVALID; | |
798 | } | |
799 | } | |
800 | ||
66e3a13e JK |
801 | static enum bpf_type_flag get_dynptr_type_flag(enum bpf_dynptr_type type) |
802 | { | |
803 | switch (type) { | |
804 | case BPF_DYNPTR_TYPE_LOCAL: | |
805 | return DYNPTR_TYPE_LOCAL; | |
806 | case BPF_DYNPTR_TYPE_RINGBUF: | |
807 | return DYNPTR_TYPE_RINGBUF; | |
808 | case BPF_DYNPTR_TYPE_SKB: | |
809 | return DYNPTR_TYPE_SKB; | |
810 | case BPF_DYNPTR_TYPE_XDP: | |
811 | return DYNPTR_TYPE_XDP; | |
812 | default: | |
813 | return 0; | |
814 | } | |
815 | } | |
816 | ||
bc34dee6 JK |
817 | static bool dynptr_type_refcounted(enum bpf_dynptr_type type) |
818 | { | |
819 | return type == BPF_DYNPTR_TYPE_RINGBUF; | |
820 | } | |
821 | ||
27060531 KKD |
822 | static void __mark_dynptr_reg(struct bpf_reg_state *reg, |
823 | enum bpf_dynptr_type type, | |
f8064ab9 | 824 | bool first_slot, int dynptr_id); |
27060531 KKD |
825 | |
826 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, | |
827 | struct bpf_reg_state *reg); | |
828 | ||
f8064ab9 KKD |
829 | static void mark_dynptr_stack_regs(struct bpf_verifier_env *env, |
830 | struct bpf_reg_state *sreg1, | |
27060531 KKD |
831 | struct bpf_reg_state *sreg2, |
832 | enum bpf_dynptr_type type) | |
833 | { | |
f8064ab9 KKD |
834 | int id = ++env->id_gen; |
835 | ||
836 | __mark_dynptr_reg(sreg1, type, true, id); | |
837 | __mark_dynptr_reg(sreg2, type, false, id); | |
27060531 KKD |
838 | } |
839 | ||
f8064ab9 KKD |
840 | static void mark_dynptr_cb_reg(struct bpf_verifier_env *env, |
841 | struct bpf_reg_state *reg, | |
27060531 KKD |
842 | enum bpf_dynptr_type type) |
843 | { | |
f8064ab9 | 844 | __mark_dynptr_reg(reg, type, true, ++env->id_gen); |
27060531 KKD |
845 | } |
846 | ||
ef8fc7a0 KKD |
847 | static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env, |
848 | struct bpf_func_state *state, int spi); | |
27060531 | 849 | |
97e03f52 | 850 | static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
361f129f | 851 | enum bpf_arg_type arg_type, int insn_idx, int clone_ref_obj_id) |
97e03f52 JK |
852 | { |
853 | struct bpf_func_state *state = func(env, reg); | |
854 | enum bpf_dynptr_type type; | |
361f129f | 855 | int spi, i, err; |
97e03f52 | 856 | |
79168a66 KKD |
857 | spi = dynptr_get_spi(env, reg); |
858 | if (spi < 0) | |
859 | return spi; | |
97e03f52 | 860 | |
379d4ba8 KKD |
861 | /* We cannot assume both spi and spi - 1 belong to the same dynptr, |
862 | * hence we need to call destroy_if_dynptr_stack_slot twice for both, | |
863 | * to ensure that for the following example: | |
864 | * [d1][d1][d2][d2] | |
865 | * spi 3 2 1 0 | |
866 | * So marking spi = 2 should lead to destruction of both d1 and d2. In | |
867 | * case they do belong to same dynptr, second call won't see slot_type | |
868 | * as STACK_DYNPTR and will simply skip destruction. | |
869 | */ | |
870 | err = destroy_if_dynptr_stack_slot(env, state, spi); | |
871 | if (err) | |
872 | return err; | |
873 | err = destroy_if_dynptr_stack_slot(env, state, spi - 1); | |
874 | if (err) | |
875 | return err; | |
97e03f52 JK |
876 | |
877 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
878 | state->stack[spi].slot_type[i] = STACK_DYNPTR; | |
879 | state->stack[spi - 1].slot_type[i] = STACK_DYNPTR; | |
880 | } | |
881 | ||
882 | type = arg_to_dynptr_type(arg_type); | |
883 | if (type == BPF_DYNPTR_TYPE_INVALID) | |
884 | return -EINVAL; | |
885 | ||
f8064ab9 | 886 | mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr, |
27060531 | 887 | &state->stack[spi - 1].spilled_ptr, type); |
97e03f52 | 888 | |
bc34dee6 JK |
889 | if (dynptr_type_refcounted(type)) { |
890 | /* The id is used to track proper releasing */ | |
361f129f JK |
891 | int id; |
892 | ||
893 | if (clone_ref_obj_id) | |
894 | id = clone_ref_obj_id; | |
895 | else | |
896 | id = acquire_reference_state(env, insn_idx); | |
897 | ||
bc34dee6 JK |
898 | if (id < 0) |
899 | return id; | |
900 | ||
27060531 KKD |
901 | state->stack[spi].spilled_ptr.ref_obj_id = id; |
902 | state->stack[spi - 1].spilled_ptr.ref_obj_id = id; | |
bc34dee6 JK |
903 | } |
904 | ||
d6fefa11 KKD |
905 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
906 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
907 | ||
97e03f52 JK |
908 | return 0; |
909 | } | |
910 | ||
361f129f | 911 | static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state, int spi) |
97e03f52 | 912 | { |
361f129f | 913 | int i; |
97e03f52 JK |
914 | |
915 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
916 | state->stack[spi].slot_type[i] = STACK_INVALID; | |
917 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
918 | } | |
919 | ||
27060531 KKD |
920 | __mark_reg_not_init(env, &state->stack[spi].spilled_ptr); |
921 | __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr); | |
d6fefa11 KKD |
922 | |
923 | /* Why do we need to set REG_LIVE_WRITTEN for STACK_INVALID slot? | |
924 | * | |
925 | * While we don't allow reading STACK_INVALID, it is still possible to | |
926 | * do <8 byte writes marking some but not all slots as STACK_MISC. Then, | |
927 | * helpers or insns can do partial read of that part without failing, | |
928 | * but check_stack_range_initialized, check_stack_read_var_off, and | |
929 | * check_stack_read_fixed_off will do mark_reg_read for all 8-bytes of | |
930 | * the slot conservatively. Hence we need to prevent those liveness | |
931 | * marking walks. | |
932 | * | |
933 | * This was not a problem before because STACK_INVALID is only set by | |
934 | * default (where the default reg state has its reg->parent as NULL), or | |
935 | * in clean_live_states after REG_LIVE_DONE (at which point | |
936 | * mark_reg_read won't walk reg->parent chain), but not randomly during | |
937 | * verifier state exploration (like we did above). Hence, for our case | |
938 | * parentage chain will still be live (i.e. reg->parent may be | |
939 | * non-NULL), while earlier reg->parent was NULL, so we need | |
940 | * REG_LIVE_WRITTEN to screen off read marker propagation when it is | |
941 | * done later on reads or by mark_dynptr_read as well to unnecessary | |
942 | * mark registers in verifier state. | |
943 | */ | |
944 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
945 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
361f129f JK |
946 | } |
947 | ||
948 | static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
949 | { | |
950 | struct bpf_func_state *state = func(env, reg); | |
951 | int spi, ref_obj_id, i; | |
952 | ||
953 | spi = dynptr_get_spi(env, reg); | |
954 | if (spi < 0) | |
955 | return spi; | |
956 | ||
957 | if (!dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) { | |
958 | invalidate_dynptr(env, state, spi); | |
959 | return 0; | |
960 | } | |
961 | ||
962 | ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id; | |
963 | ||
964 | /* If the dynptr has a ref_obj_id, then we need to invalidate | |
965 | * two things: | |
966 | * | |
967 | * 1) Any dynptrs with a matching ref_obj_id (clones) | |
968 | * 2) Any slices derived from this dynptr. | |
969 | */ | |
970 | ||
971 | /* Invalidate any slices associated with this dynptr */ | |
972 | WARN_ON_ONCE(release_reference(env, ref_obj_id)); | |
973 | ||
974 | /* Invalidate any dynptr clones */ | |
975 | for (i = 1; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
976 | if (state->stack[i].spilled_ptr.ref_obj_id != ref_obj_id) | |
977 | continue; | |
978 | ||
979 | /* it should always be the case that if the ref obj id | |
980 | * matches then the stack slot also belongs to a | |
981 | * dynptr | |
982 | */ | |
983 | if (state->stack[i].slot_type[0] != STACK_DYNPTR) { | |
984 | verbose(env, "verifier internal error: misconfigured ref_obj_id\n"); | |
985 | return -EFAULT; | |
986 | } | |
987 | if (state->stack[i].spilled_ptr.dynptr.first_slot) | |
988 | invalidate_dynptr(env, state, i); | |
989 | } | |
d6fefa11 | 990 | |
97e03f52 JK |
991 | return 0; |
992 | } | |
993 | ||
ef8fc7a0 KKD |
994 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
995 | struct bpf_reg_state *reg); | |
996 | ||
dbd8d228 KKD |
997 | static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
998 | { | |
999 | if (!env->allow_ptr_leaks) | |
1000 | __mark_reg_not_init(env, reg); | |
1001 | else | |
1002 | __mark_reg_unknown(env, reg); | |
1003 | } | |
1004 | ||
ef8fc7a0 KKD |
1005 | static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env, |
1006 | struct bpf_func_state *state, int spi) | |
97e03f52 | 1007 | { |
f8064ab9 KKD |
1008 | struct bpf_func_state *fstate; |
1009 | struct bpf_reg_state *dreg; | |
1010 | int i, dynptr_id; | |
27060531 | 1011 | |
ef8fc7a0 KKD |
1012 | /* We always ensure that STACK_DYNPTR is never set partially, |
1013 | * hence just checking for slot_type[0] is enough. This is | |
1014 | * different for STACK_SPILL, where it may be only set for | |
1015 | * 1 byte, so code has to use is_spilled_reg. | |
1016 | */ | |
1017 | if (state->stack[spi].slot_type[0] != STACK_DYNPTR) | |
1018 | return 0; | |
97e03f52 | 1019 | |
ef8fc7a0 KKD |
1020 | /* Reposition spi to first slot */ |
1021 | if (!state->stack[spi].spilled_ptr.dynptr.first_slot) | |
1022 | spi = spi + 1; | |
1023 | ||
1024 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) { | |
1025 | verbose(env, "cannot overwrite referenced dynptr\n"); | |
1026 | return -EINVAL; | |
1027 | } | |
1028 | ||
1029 | mark_stack_slot_scratched(env, spi); | |
1030 | mark_stack_slot_scratched(env, spi - 1); | |
97e03f52 | 1031 | |
ef8fc7a0 | 1032 | /* Writing partially to one dynptr stack slot destroys both. */ |
97e03f52 | 1033 | for (i = 0; i < BPF_REG_SIZE; i++) { |
ef8fc7a0 KKD |
1034 | state->stack[spi].slot_type[i] = STACK_INVALID; |
1035 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
97e03f52 JK |
1036 | } |
1037 | ||
f8064ab9 KKD |
1038 | dynptr_id = state->stack[spi].spilled_ptr.id; |
1039 | /* Invalidate any slices associated with this dynptr */ | |
1040 | bpf_for_each_reg_in_vstate(env->cur_state, fstate, dreg, ({ | |
1041 | /* Dynptr slices are only PTR_TO_MEM_OR_NULL and PTR_TO_MEM */ | |
1042 | if (dreg->type != (PTR_TO_MEM | PTR_MAYBE_NULL) && dreg->type != PTR_TO_MEM) | |
1043 | continue; | |
dbd8d228 KKD |
1044 | if (dreg->dynptr_id == dynptr_id) |
1045 | mark_reg_invalid(env, dreg); | |
f8064ab9 | 1046 | })); |
ef8fc7a0 KKD |
1047 | |
1048 | /* Do not release reference state, we are destroying dynptr on stack, | |
1049 | * not using some helper to release it. Just reset register. | |
1050 | */ | |
1051 | __mark_reg_not_init(env, &state->stack[spi].spilled_ptr); | |
1052 | __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr); | |
1053 | ||
1054 | /* Same reason as unmark_stack_slots_dynptr above */ | |
1055 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1056 | state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1057 | ||
1058 | return 0; | |
1059 | } | |
1060 | ||
7e0dac28 | 1061 | static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
97e03f52 | 1062 | { |
7e0dac28 JK |
1063 | int spi; |
1064 | ||
27060531 KKD |
1065 | if (reg->type == CONST_PTR_TO_DYNPTR) |
1066 | return false; | |
97e03f52 | 1067 | |
7e0dac28 JK |
1068 | spi = dynptr_get_spi(env, reg); |
1069 | ||
1070 | /* -ERANGE (i.e. spi not falling into allocated stack slots) isn't an | |
1071 | * error because this just means the stack state hasn't been updated yet. | |
1072 | * We will do check_mem_access to check and update stack bounds later. | |
f5b625e5 | 1073 | */ |
7e0dac28 JK |
1074 | if (spi < 0 && spi != -ERANGE) |
1075 | return false; | |
1076 | ||
1077 | /* We don't need to check if the stack slots are marked by previous | |
1078 | * dynptr initializations because we allow overwriting existing unreferenced | |
1079 | * STACK_DYNPTR slots, see mark_stack_slots_dynptr which calls | |
1080 | * destroy_if_dynptr_stack_slot to ensure dynptr objects at the slots we are | |
1081 | * touching are completely destructed before we reinitialize them for a new | |
1082 | * one. For referenced ones, destroy_if_dynptr_stack_slot returns an error early | |
1083 | * instead of delaying it until the end where the user will get "Unreleased | |
379d4ba8 KKD |
1084 | * reference" error. |
1085 | */ | |
97e03f52 JK |
1086 | return true; |
1087 | } | |
1088 | ||
7e0dac28 | 1089 | static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
97e03f52 JK |
1090 | { |
1091 | struct bpf_func_state *state = func(env, reg); | |
7e0dac28 | 1092 | int i, spi; |
97e03f52 | 1093 | |
7e0dac28 JK |
1094 | /* This already represents first slot of initialized bpf_dynptr. |
1095 | * | |
1096 | * CONST_PTR_TO_DYNPTR already has fixed and var_off as 0 due to | |
1097 | * check_func_arg_reg_off's logic, so we don't need to check its | |
1098 | * offset and alignment. | |
1099 | */ | |
27060531 KKD |
1100 | if (reg->type == CONST_PTR_TO_DYNPTR) |
1101 | return true; | |
1102 | ||
7e0dac28 | 1103 | spi = dynptr_get_spi(env, reg); |
79168a66 KKD |
1104 | if (spi < 0) |
1105 | return false; | |
f5b625e5 | 1106 | if (!state->stack[spi].spilled_ptr.dynptr.first_slot) |
97e03f52 JK |
1107 | return false; |
1108 | ||
1109 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
1110 | if (state->stack[spi].slot_type[i] != STACK_DYNPTR || | |
1111 | state->stack[spi - 1].slot_type[i] != STACK_DYNPTR) | |
1112 | return false; | |
1113 | } | |
1114 | ||
e9e315b4 RS |
1115 | return true; |
1116 | } | |
1117 | ||
6b75bd3d KKD |
1118 | static bool is_dynptr_type_expected(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
1119 | enum bpf_arg_type arg_type) | |
e9e315b4 RS |
1120 | { |
1121 | struct bpf_func_state *state = func(env, reg); | |
1122 | enum bpf_dynptr_type dynptr_type; | |
27060531 | 1123 | int spi; |
e9e315b4 | 1124 | |
97e03f52 JK |
1125 | /* ARG_PTR_TO_DYNPTR takes any type of dynptr */ |
1126 | if (arg_type == ARG_PTR_TO_DYNPTR) | |
1127 | return true; | |
1128 | ||
e9e315b4 | 1129 | dynptr_type = arg_to_dynptr_type(arg_type); |
27060531 KKD |
1130 | if (reg->type == CONST_PTR_TO_DYNPTR) { |
1131 | return reg->dynptr.type == dynptr_type; | |
1132 | } else { | |
79168a66 KKD |
1133 | spi = dynptr_get_spi(env, reg); |
1134 | if (spi < 0) | |
1135 | return false; | |
27060531 KKD |
1136 | return state->stack[spi].spilled_ptr.dynptr.type == dynptr_type; |
1137 | } | |
97e03f52 JK |
1138 | } |
1139 | ||
06accc87 AN |
1140 | static void __mark_reg_known_zero(struct bpf_reg_state *reg); |
1141 | ||
1142 | static int mark_stack_slots_iter(struct bpf_verifier_env *env, | |
1143 | struct bpf_reg_state *reg, int insn_idx, | |
1144 | struct btf *btf, u32 btf_id, int nr_slots) | |
1145 | { | |
1146 | struct bpf_func_state *state = func(env, reg); | |
1147 | int spi, i, j, id; | |
1148 | ||
1149 | spi = iter_get_spi(env, reg, nr_slots); | |
1150 | if (spi < 0) | |
1151 | return spi; | |
1152 | ||
1153 | id = acquire_reference_state(env, insn_idx); | |
1154 | if (id < 0) | |
1155 | return id; | |
1156 | ||
1157 | for (i = 0; i < nr_slots; i++) { | |
1158 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1159 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1160 | ||
1161 | __mark_reg_known_zero(st); | |
1162 | st->type = PTR_TO_STACK; /* we don't have dedicated reg type */ | |
1163 | st->live |= REG_LIVE_WRITTEN; | |
1164 | st->ref_obj_id = i == 0 ? id : 0; | |
1165 | st->iter.btf = btf; | |
1166 | st->iter.btf_id = btf_id; | |
1167 | st->iter.state = BPF_ITER_STATE_ACTIVE; | |
1168 | st->iter.depth = 0; | |
1169 | ||
1170 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1171 | slot->slot_type[j] = STACK_ITER; | |
1172 | ||
1173 | mark_stack_slot_scratched(env, spi - i); | |
1174 | } | |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | static int unmark_stack_slots_iter(struct bpf_verifier_env *env, | |
1180 | struct bpf_reg_state *reg, int nr_slots) | |
1181 | { | |
1182 | struct bpf_func_state *state = func(env, reg); | |
1183 | int spi, i, j; | |
1184 | ||
1185 | spi = iter_get_spi(env, reg, nr_slots); | |
1186 | if (spi < 0) | |
1187 | return spi; | |
1188 | ||
1189 | for (i = 0; i < nr_slots; i++) { | |
1190 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1191 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1192 | ||
1193 | if (i == 0) | |
1194 | WARN_ON_ONCE(release_reference(env, st->ref_obj_id)); | |
1195 | ||
1196 | __mark_reg_not_init(env, st); | |
1197 | ||
1198 | /* see unmark_stack_slots_dynptr() for why we need to set REG_LIVE_WRITTEN */ | |
1199 | st->live |= REG_LIVE_WRITTEN; | |
1200 | ||
1201 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1202 | slot->slot_type[j] = STACK_INVALID; | |
1203 | ||
1204 | mark_stack_slot_scratched(env, spi - i); | |
1205 | } | |
1206 | ||
1207 | return 0; | |
1208 | } | |
1209 | ||
1210 | static bool is_iter_reg_valid_uninit(struct bpf_verifier_env *env, | |
1211 | struct bpf_reg_state *reg, int nr_slots) | |
1212 | { | |
1213 | struct bpf_func_state *state = func(env, reg); | |
1214 | int spi, i, j; | |
1215 | ||
1216 | /* For -ERANGE (i.e. spi not falling into allocated stack slots), we | |
1217 | * will do check_mem_access to check and update stack bounds later, so | |
1218 | * return true for that case. | |
1219 | */ | |
1220 | spi = iter_get_spi(env, reg, nr_slots); | |
1221 | if (spi == -ERANGE) | |
1222 | return true; | |
1223 | if (spi < 0) | |
1224 | return false; | |
1225 | ||
1226 | for (i = 0; i < nr_slots; i++) { | |
1227 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1228 | ||
1229 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1230 | if (slot->slot_type[j] == STACK_ITER) | |
1231 | return false; | |
1232 | } | |
1233 | ||
1234 | return true; | |
1235 | } | |
1236 | ||
1237 | static bool is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg, | |
1238 | struct btf *btf, u32 btf_id, int nr_slots) | |
1239 | { | |
1240 | struct bpf_func_state *state = func(env, reg); | |
1241 | int spi, i, j; | |
1242 | ||
1243 | spi = iter_get_spi(env, reg, nr_slots); | |
1244 | if (spi < 0) | |
1245 | return false; | |
1246 | ||
1247 | for (i = 0; i < nr_slots; i++) { | |
1248 | struct bpf_stack_state *slot = &state->stack[spi - i]; | |
1249 | struct bpf_reg_state *st = &slot->spilled_ptr; | |
1250 | ||
1251 | /* only main (first) slot has ref_obj_id set */ | |
1252 | if (i == 0 && !st->ref_obj_id) | |
1253 | return false; | |
1254 | if (i != 0 && st->ref_obj_id) | |
1255 | return false; | |
1256 | if (st->iter.btf != btf || st->iter.btf_id != btf_id) | |
1257 | return false; | |
1258 | ||
1259 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1260 | if (slot->slot_type[j] != STACK_ITER) | |
1261 | return false; | |
1262 | } | |
1263 | ||
1264 | return true; | |
1265 | } | |
1266 | ||
1267 | /* Check if given stack slot is "special": | |
1268 | * - spilled register state (STACK_SPILL); | |
1269 | * - dynptr state (STACK_DYNPTR); | |
1270 | * - iter state (STACK_ITER). | |
1271 | */ | |
1272 | static bool is_stack_slot_special(const struct bpf_stack_state *stack) | |
1273 | { | |
1274 | enum bpf_stack_slot_type type = stack->slot_type[BPF_REG_SIZE - 1]; | |
1275 | ||
1276 | switch (type) { | |
1277 | case STACK_SPILL: | |
1278 | case STACK_DYNPTR: | |
1279 | case STACK_ITER: | |
1280 | return true; | |
1281 | case STACK_INVALID: | |
1282 | case STACK_MISC: | |
1283 | case STACK_ZERO: | |
1284 | return false; | |
1285 | default: | |
1286 | WARN_ONCE(1, "unknown stack slot type %d\n", type); | |
1287 | return true; | |
1288 | } | |
1289 | } | |
1290 | ||
27113c59 MKL |
1291 | /* The reg state of a pointer or a bounded scalar was saved when |
1292 | * it was spilled to the stack. | |
1293 | */ | |
1294 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
1295 | { | |
1296 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
1297 | } | |
1298 | ||
354e8f19 MKL |
1299 | static void scrub_spilled_slot(u8 *stype) |
1300 | { | |
1301 | if (*stype != STACK_INVALID) | |
1302 | *stype = STACK_MISC; | |
1303 | } | |
1304 | ||
61bd5218 | 1305 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
1306 | const struct bpf_func_state *state, |
1307 | bool print_all) | |
17a52670 | 1308 | { |
f4d7e40a | 1309 | const struct bpf_reg_state *reg; |
17a52670 AS |
1310 | enum bpf_reg_type t; |
1311 | int i; | |
1312 | ||
f4d7e40a AS |
1313 | if (state->frameno) |
1314 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 1315 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
1316 | reg = &state->regs[i]; |
1317 | t = reg->type; | |
17a52670 AS |
1318 | if (t == NOT_INIT) |
1319 | continue; | |
0f55f9ed CL |
1320 | if (!print_all && !reg_scratched(env, i)) |
1321 | continue; | |
4e92024a AS |
1322 | verbose(env, " R%d", i); |
1323 | print_liveness(env, reg->live); | |
7df5072c | 1324 | verbose(env, "="); |
b5dc0163 AS |
1325 | if (t == SCALAR_VALUE && reg->precise) |
1326 | verbose(env, "P"); | |
f1174f77 EC |
1327 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
1328 | tnum_is_const(reg->var_off)) { | |
1329 | /* reg->off should be 0 for SCALAR_VALUE */ | |
7df5072c | 1330 | verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
61bd5218 | 1331 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 1332 | } else { |
7df5072c ML |
1333 | const char *sep = ""; |
1334 | ||
1335 | verbose(env, "%s", reg_type_str(env, t)); | |
5844101a | 1336 | if (base_type(t) == PTR_TO_BTF_ID) |
b32a5dae | 1337 | verbose(env, "%s", btf_type_name(reg->btf, reg->btf_id)); |
7df5072c ML |
1338 | verbose(env, "("); |
1339 | /* | |
1340 | * _a stands for append, was shortened to avoid multiline statements below. | |
1341 | * This macro is used to output a comma separated list of attributes. | |
1342 | */ | |
1343 | #define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) | |
1344 | ||
1345 | if (reg->id) | |
1346 | verbose_a("id=%d", reg->id); | |
a28ace78 | 1347 | if (reg->ref_obj_id) |
7df5072c | 1348 | verbose_a("ref_obj_id=%d", reg->ref_obj_id); |
6a3cd331 DM |
1349 | if (type_is_non_owning_ref(reg->type)) |
1350 | verbose_a("%s", "non_own_ref"); | |
f1174f77 | 1351 | if (t != SCALAR_VALUE) |
7df5072c | 1352 | verbose_a("off=%d", reg->off); |
de8f3a83 | 1353 | if (type_is_pkt_pointer(t)) |
7df5072c | 1354 | verbose_a("r=%d", reg->range); |
c25b2ae1 HL |
1355 | else if (base_type(t) == CONST_PTR_TO_MAP || |
1356 | base_type(t) == PTR_TO_MAP_KEY || | |
1357 | base_type(t) == PTR_TO_MAP_VALUE) | |
7df5072c ML |
1358 | verbose_a("ks=%d,vs=%d", |
1359 | reg->map_ptr->key_size, | |
1360 | reg->map_ptr->value_size); | |
7d1238f2 EC |
1361 | if (tnum_is_const(reg->var_off)) { |
1362 | /* Typically an immediate SCALAR_VALUE, but | |
1363 | * could be a pointer whose offset is too big | |
1364 | * for reg->off | |
1365 | */ | |
7df5072c | 1366 | verbose_a("imm=%llx", reg->var_off.value); |
7d1238f2 EC |
1367 | } else { |
1368 | if (reg->smin_value != reg->umin_value && | |
1369 | reg->smin_value != S64_MIN) | |
7df5072c | 1370 | verbose_a("smin=%lld", (long long)reg->smin_value); |
7d1238f2 EC |
1371 | if (reg->smax_value != reg->umax_value && |
1372 | reg->smax_value != S64_MAX) | |
7df5072c | 1373 | verbose_a("smax=%lld", (long long)reg->smax_value); |
7d1238f2 | 1374 | if (reg->umin_value != 0) |
7df5072c | 1375 | verbose_a("umin=%llu", (unsigned long long)reg->umin_value); |
7d1238f2 | 1376 | if (reg->umax_value != U64_MAX) |
7df5072c | 1377 | verbose_a("umax=%llu", (unsigned long long)reg->umax_value); |
7d1238f2 EC |
1378 | if (!tnum_is_unknown(reg->var_off)) { |
1379 | char tn_buf[48]; | |
f1174f77 | 1380 | |
7d1238f2 | 1381 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
7df5072c | 1382 | verbose_a("var_off=%s", tn_buf); |
7d1238f2 | 1383 | } |
3f50f132 JF |
1384 | if (reg->s32_min_value != reg->smin_value && |
1385 | reg->s32_min_value != S32_MIN) | |
7df5072c | 1386 | verbose_a("s32_min=%d", (int)(reg->s32_min_value)); |
3f50f132 JF |
1387 | if (reg->s32_max_value != reg->smax_value && |
1388 | reg->s32_max_value != S32_MAX) | |
7df5072c | 1389 | verbose_a("s32_max=%d", (int)(reg->s32_max_value)); |
3f50f132 JF |
1390 | if (reg->u32_min_value != reg->umin_value && |
1391 | reg->u32_min_value != U32_MIN) | |
7df5072c | 1392 | verbose_a("u32_min=%d", (int)(reg->u32_min_value)); |
3f50f132 JF |
1393 | if (reg->u32_max_value != reg->umax_value && |
1394 | reg->u32_max_value != U32_MAX) | |
7df5072c | 1395 | verbose_a("u32_max=%d", (int)(reg->u32_max_value)); |
f1174f77 | 1396 | } |
7df5072c ML |
1397 | #undef verbose_a |
1398 | ||
61bd5218 | 1399 | verbose(env, ")"); |
f1174f77 | 1400 | } |
17a52670 | 1401 | } |
638f5b90 | 1402 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
1403 | char types_buf[BPF_REG_SIZE + 1]; |
1404 | bool valid = false; | |
1405 | int j; | |
1406 | ||
1407 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
1408 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
1409 | valid = true; | |
d54e0f6c | 1410 | types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; |
8efea21d EC |
1411 | } |
1412 | types_buf[BPF_REG_SIZE] = 0; | |
1413 | if (!valid) | |
1414 | continue; | |
0f55f9ed CL |
1415 | if (!print_all && !stack_slot_scratched(env, i)) |
1416 | continue; | |
d54e0f6c AN |
1417 | switch (state->stack[i].slot_type[BPF_REG_SIZE - 1]) { |
1418 | case STACK_SPILL: | |
b5dc0163 AS |
1419 | reg = &state->stack[i].spilled_ptr; |
1420 | t = reg->type; | |
d54e0f6c AN |
1421 | |
1422 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1423 | print_liveness(env, reg->live); | |
7df5072c | 1424 | verbose(env, "=%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
b5dc0163 AS |
1425 | if (t == SCALAR_VALUE && reg->precise) |
1426 | verbose(env, "P"); | |
1427 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
1428 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
d54e0f6c AN |
1429 | break; |
1430 | case STACK_DYNPTR: | |
1431 | i += BPF_DYNPTR_NR_SLOTS - 1; | |
1432 | reg = &state->stack[i].spilled_ptr; | |
1433 | ||
1434 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1435 | print_liveness(env, reg->live); | |
1436 | verbose(env, "=dynptr_%s", dynptr_type_str(reg->dynptr.type)); | |
1437 | if (reg->ref_obj_id) | |
1438 | verbose(env, "(ref_id=%d)", reg->ref_obj_id); | |
1439 | break; | |
06accc87 AN |
1440 | case STACK_ITER: |
1441 | /* only main slot has ref_obj_id set; skip others */ | |
1442 | reg = &state->stack[i].spilled_ptr; | |
1443 | if (!reg->ref_obj_id) | |
1444 | continue; | |
1445 | ||
1446 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1447 | print_liveness(env, reg->live); | |
1448 | verbose(env, "=iter_%s(ref_id=%d,state=%s,depth=%u)", | |
1449 | iter_type_str(reg->iter.btf, reg->iter.btf_id), | |
1450 | reg->ref_obj_id, iter_state_str(reg->iter.state), | |
1451 | reg->iter.depth); | |
1452 | break; | |
d54e0f6c AN |
1453 | case STACK_MISC: |
1454 | case STACK_ZERO: | |
1455 | default: | |
1456 | reg = &state->stack[i].spilled_ptr; | |
1457 | ||
1458 | for (j = 0; j < BPF_REG_SIZE; j++) | |
1459 | types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; | |
1460 | types_buf[BPF_REG_SIZE] = 0; | |
1461 | ||
1462 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
1463 | print_liveness(env, reg->live); | |
8efea21d | 1464 | verbose(env, "=%s", types_buf); |
d54e0f6c | 1465 | break; |
b5dc0163 | 1466 | } |
17a52670 | 1467 | } |
fd978bf7 JS |
1468 | if (state->acquired_refs && state->refs[0].id) { |
1469 | verbose(env, " refs=%d", state->refs[0].id); | |
1470 | for (i = 1; i < state->acquired_refs; i++) | |
1471 | if (state->refs[i].id) | |
1472 | verbose(env, ",%d", state->refs[i].id); | |
1473 | } | |
bfc6bb74 AS |
1474 | if (state->in_callback_fn) |
1475 | verbose(env, " cb"); | |
1476 | if (state->in_async_callback_fn) | |
1477 | verbose(env, " async_cb"); | |
61bd5218 | 1478 | verbose(env, "\n"); |
0f55f9ed | 1479 | mark_verifier_state_clean(env); |
17a52670 AS |
1480 | } |
1481 | ||
2e576648 CL |
1482 | static inline u32 vlog_alignment(u32 pos) |
1483 | { | |
1484 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
1485 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
1486 | } | |
1487 | ||
1488 | static void print_insn_state(struct bpf_verifier_env *env, | |
1489 | const struct bpf_func_state *state) | |
1490 | { | |
12166409 | 1491 | if (env->prev_log_pos && env->prev_log_pos == env->log.end_pos) { |
2e576648 | 1492 | /* remove new line character */ |
12166409 AN |
1493 | bpf_vlog_reset(&env->log, env->prev_log_pos - 1); |
1494 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_pos), ' '); | |
2e576648 CL |
1495 | } else { |
1496 | verbose(env, "%d:", env->insn_idx); | |
1497 | } | |
1498 | print_verifier_state(env, state, false); | |
17a52670 AS |
1499 | } |
1500 | ||
c69431aa LB |
1501 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
1502 | * small to hold src. This is different from krealloc since we don't want to preserve | |
1503 | * the contents of dst. | |
1504 | * | |
1505 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
1506 | * not be allocated. | |
638f5b90 | 1507 | */ |
c69431aa | 1508 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 1509 | { |
45435d8d KC |
1510 | size_t alloc_bytes; |
1511 | void *orig = dst; | |
c69431aa LB |
1512 | size_t bytes; |
1513 | ||
1514 | if (ZERO_OR_NULL_PTR(src)) | |
1515 | goto out; | |
1516 | ||
1517 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
1518 | return NULL; | |
1519 | ||
45435d8d KC |
1520 | alloc_bytes = max(ksize(orig), kmalloc_size_roundup(bytes)); |
1521 | dst = krealloc(orig, alloc_bytes, flags); | |
1522 | if (!dst) { | |
1523 | kfree(orig); | |
1524 | return NULL; | |
c69431aa LB |
1525 | } |
1526 | ||
1527 | memcpy(dst, src, bytes); | |
1528 | out: | |
1529 | return dst ? dst : ZERO_SIZE_PTR; | |
1530 | } | |
1531 | ||
1532 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
1533 | * small to hold new_n items. new items are zeroed out if the array grows. | |
1534 | * | |
1535 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
1536 | */ | |
1537 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
1538 | { | |
ceb35b66 | 1539 | size_t alloc_size; |
42378a9c KC |
1540 | void *new_arr; |
1541 | ||
c69431aa LB |
1542 | if (!new_n || old_n == new_n) |
1543 | goto out; | |
1544 | ||
ceb35b66 KC |
1545 | alloc_size = kmalloc_size_roundup(size_mul(new_n, size)); |
1546 | new_arr = krealloc(arr, alloc_size, GFP_KERNEL); | |
42378a9c KC |
1547 | if (!new_arr) { |
1548 | kfree(arr); | |
c69431aa | 1549 | return NULL; |
42378a9c KC |
1550 | } |
1551 | arr = new_arr; | |
c69431aa LB |
1552 | |
1553 | if (new_n > old_n) | |
1554 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
1555 | ||
1556 | out: | |
1557 | return arr ? arr : ZERO_SIZE_PTR; | |
1558 | } | |
1559 | ||
1560 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1561 | { | |
1562 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
1563 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
1564 | if (!dst->refs) | |
1565 | return -ENOMEM; | |
1566 | ||
1567 | dst->acquired_refs = src->acquired_refs; | |
1568 | return 0; | |
1569 | } | |
1570 | ||
1571 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1572 | { | |
1573 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
1574 | ||
1575 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
1576 | GFP_KERNEL); | |
1577 | if (!dst->stack) | |
1578 | return -ENOMEM; | |
1579 | ||
1580 | dst->allocated_stack = src->allocated_stack; | |
1581 | return 0; | |
1582 | } | |
1583 | ||
1584 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
1585 | { | |
1586 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
1587 | sizeof(struct bpf_reference_state)); | |
1588 | if (!state->refs) | |
1589 | return -ENOMEM; | |
1590 | ||
1591 | state->acquired_refs = n; | |
1592 | return 0; | |
1593 | } | |
1594 | ||
1595 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
1596 | { | |
1597 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
1598 | ||
1599 | if (old_n >= n) | |
1600 | return 0; | |
1601 | ||
1602 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
1603 | if (!state->stack) | |
1604 | return -ENOMEM; | |
1605 | ||
1606 | state->allocated_stack = size; | |
1607 | return 0; | |
fd978bf7 JS |
1608 | } |
1609 | ||
1610 | /* Acquire a pointer id from the env and update the state->refs to include | |
1611 | * this new pointer reference. | |
1612 | * On success, returns a valid pointer id to associate with the register | |
1613 | * On failure, returns a negative errno. | |
638f5b90 | 1614 | */ |
fd978bf7 | 1615 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 1616 | { |
fd978bf7 JS |
1617 | struct bpf_func_state *state = cur_func(env); |
1618 | int new_ofs = state->acquired_refs; | |
1619 | int id, err; | |
1620 | ||
c69431aa | 1621 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
1622 | if (err) |
1623 | return err; | |
1624 | id = ++env->id_gen; | |
1625 | state->refs[new_ofs].id = id; | |
1626 | state->refs[new_ofs].insn_idx = insn_idx; | |
9d9d00ac | 1627 | state->refs[new_ofs].callback_ref = state->in_callback_fn ? state->frameno : 0; |
638f5b90 | 1628 | |
fd978bf7 JS |
1629 | return id; |
1630 | } | |
1631 | ||
1632 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 1633 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
1634 | { |
1635 | int i, last_idx; | |
1636 | ||
fd978bf7 JS |
1637 | last_idx = state->acquired_refs - 1; |
1638 | for (i = 0; i < state->acquired_refs; i++) { | |
1639 | if (state->refs[i].id == ptr_id) { | |
9d9d00ac KKD |
1640 | /* Cannot release caller references in callbacks */ |
1641 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) | |
1642 | return -EINVAL; | |
fd978bf7 JS |
1643 | if (last_idx && i != last_idx) |
1644 | memcpy(&state->refs[i], &state->refs[last_idx], | |
1645 | sizeof(*state->refs)); | |
1646 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
1647 | state->acquired_refs--; | |
638f5b90 | 1648 | return 0; |
638f5b90 | 1649 | } |
638f5b90 | 1650 | } |
46f8bc92 | 1651 | return -EINVAL; |
fd978bf7 JS |
1652 | } |
1653 | ||
f4d7e40a AS |
1654 | static void free_func_state(struct bpf_func_state *state) |
1655 | { | |
5896351e AS |
1656 | if (!state) |
1657 | return; | |
fd978bf7 | 1658 | kfree(state->refs); |
f4d7e40a AS |
1659 | kfree(state->stack); |
1660 | kfree(state); | |
1661 | } | |
1662 | ||
b5dc0163 AS |
1663 | static void clear_jmp_history(struct bpf_verifier_state *state) |
1664 | { | |
1665 | kfree(state->jmp_history); | |
1666 | state->jmp_history = NULL; | |
1667 | state->jmp_history_cnt = 0; | |
1668 | } | |
1669 | ||
1969db47 AS |
1670 | static void free_verifier_state(struct bpf_verifier_state *state, |
1671 | bool free_self) | |
638f5b90 | 1672 | { |
f4d7e40a AS |
1673 | int i; |
1674 | ||
1675 | for (i = 0; i <= state->curframe; i++) { | |
1676 | free_func_state(state->frame[i]); | |
1677 | state->frame[i] = NULL; | |
1678 | } | |
b5dc0163 | 1679 | clear_jmp_history(state); |
1969db47 AS |
1680 | if (free_self) |
1681 | kfree(state); | |
638f5b90 AS |
1682 | } |
1683 | ||
1684 | /* copy verifier state from src to dst growing dst stack space | |
1685 | * when necessary to accommodate larger src stack | |
1686 | */ | |
f4d7e40a AS |
1687 | static int copy_func_state(struct bpf_func_state *dst, |
1688 | const struct bpf_func_state *src) | |
638f5b90 AS |
1689 | { |
1690 | int err; | |
1691 | ||
fd978bf7 JS |
1692 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
1693 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
1694 | if (err) |
1695 | return err; | |
638f5b90 AS |
1696 | return copy_stack_state(dst, src); |
1697 | } | |
1698 | ||
f4d7e40a AS |
1699 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1700 | const struct bpf_verifier_state *src) | |
1701 | { | |
1702 | struct bpf_func_state *dst; | |
1703 | int i, err; | |
1704 | ||
06ab6a50 LB |
1705 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1706 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1707 | GFP_USER); | |
1708 | if (!dst_state->jmp_history) | |
1709 | return -ENOMEM; | |
b5dc0163 AS |
1710 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1711 | ||
f4d7e40a AS |
1712 | /* if dst has more stack frames then src frame, free them */ |
1713 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1714 | free_func_state(dst_state->frame[i]); | |
1715 | dst_state->frame[i] = NULL; | |
1716 | } | |
979d63d5 | 1717 | dst_state->speculative = src->speculative; |
9bb00b28 | 1718 | dst_state->active_rcu_lock = src->active_rcu_lock; |
f4d7e40a | 1719 | dst_state->curframe = src->curframe; |
d0d78c1d KKD |
1720 | dst_state->active_lock.ptr = src->active_lock.ptr; |
1721 | dst_state->active_lock.id = src->active_lock.id; | |
2589726d AS |
1722 | dst_state->branches = src->branches; |
1723 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1724 | dst_state->first_insn_idx = src->first_insn_idx; |
1725 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1726 | for (i = 0; i <= src->curframe; i++) { |
1727 | dst = dst_state->frame[i]; | |
1728 | if (!dst) { | |
1729 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1730 | if (!dst) | |
1731 | return -ENOMEM; | |
1732 | dst_state->frame[i] = dst; | |
1733 | } | |
1734 | err = copy_func_state(dst, src->frame[i]); | |
1735 | if (err) | |
1736 | return err; | |
1737 | } | |
1738 | return 0; | |
1739 | } | |
1740 | ||
2589726d AS |
1741 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1742 | { | |
1743 | while (st) { | |
1744 | u32 br = --st->branches; | |
1745 | ||
1746 | /* WARN_ON(br > 1) technically makes sense here, | |
1747 | * but see comment in push_stack(), hence: | |
1748 | */ | |
1749 | WARN_ONCE((int)br < 0, | |
1750 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1751 | br); | |
1752 | if (br) | |
1753 | break; | |
1754 | st = st->parent; | |
1755 | } | |
1756 | } | |
1757 | ||
638f5b90 | 1758 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1759 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1760 | { |
1761 | struct bpf_verifier_state *cur = env->cur_state; | |
1762 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1763 | int err; | |
17a52670 AS |
1764 | |
1765 | if (env->head == NULL) | |
638f5b90 | 1766 | return -ENOENT; |
17a52670 | 1767 | |
638f5b90 AS |
1768 | if (cur) { |
1769 | err = copy_verifier_state(cur, &head->st); | |
1770 | if (err) | |
1771 | return err; | |
1772 | } | |
6f8a57cc AN |
1773 | if (pop_log) |
1774 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1775 | if (insn_idx) |
1776 | *insn_idx = head->insn_idx; | |
17a52670 | 1777 | if (prev_insn_idx) |
638f5b90 AS |
1778 | *prev_insn_idx = head->prev_insn_idx; |
1779 | elem = head->next; | |
1969db47 | 1780 | free_verifier_state(&head->st, false); |
638f5b90 | 1781 | kfree(head); |
17a52670 AS |
1782 | env->head = elem; |
1783 | env->stack_size--; | |
638f5b90 | 1784 | return 0; |
17a52670 AS |
1785 | } |
1786 | ||
58e2af8b | 1787 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1788 | int insn_idx, int prev_insn_idx, |
1789 | bool speculative) | |
17a52670 | 1790 | { |
638f5b90 | 1791 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1792 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1793 | int err; |
17a52670 | 1794 | |
638f5b90 | 1795 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1796 | if (!elem) |
1797 | goto err; | |
1798 | ||
17a52670 AS |
1799 | elem->insn_idx = insn_idx; |
1800 | elem->prev_insn_idx = prev_insn_idx; | |
1801 | elem->next = env->head; | |
12166409 | 1802 | elem->log_pos = env->log.end_pos; |
17a52670 AS |
1803 | env->head = elem; |
1804 | env->stack_size++; | |
1969db47 AS |
1805 | err = copy_verifier_state(&elem->st, cur); |
1806 | if (err) | |
1807 | goto err; | |
979d63d5 | 1808 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1809 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1810 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1811 | env->stack_size); | |
17a52670 AS |
1812 | goto err; |
1813 | } | |
2589726d AS |
1814 | if (elem->st.parent) { |
1815 | ++elem->st.parent->branches; | |
1816 | /* WARN_ON(branches > 2) technically makes sense here, | |
1817 | * but | |
1818 | * 1. speculative states will bump 'branches' for non-branch | |
1819 | * instructions | |
1820 | * 2. is_state_visited() heuristics may decide not to create | |
1821 | * a new state for a sequence of branches and all such current | |
1822 | * and cloned states will be pointing to a single parent state | |
1823 | * which might have large 'branches' count. | |
1824 | */ | |
1825 | } | |
17a52670 AS |
1826 | return &elem->st; |
1827 | err: | |
5896351e AS |
1828 | free_verifier_state(env->cur_state, true); |
1829 | env->cur_state = NULL; | |
17a52670 | 1830 | /* pop all elements and return */ |
6f8a57cc | 1831 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1832 | return NULL; |
1833 | } | |
1834 | ||
1835 | #define CALLER_SAVED_REGS 6 | |
1836 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1837 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1838 | }; | |
1839 | ||
e688c3db AS |
1840 | /* This helper doesn't clear reg->id */ |
1841 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1842 | { |
b03c9f9f EC |
1843 | reg->var_off = tnum_const(imm); |
1844 | reg->smin_value = (s64)imm; | |
1845 | reg->smax_value = (s64)imm; | |
1846 | reg->umin_value = imm; | |
1847 | reg->umax_value = imm; | |
3f50f132 JF |
1848 | |
1849 | reg->s32_min_value = (s32)imm; | |
1850 | reg->s32_max_value = (s32)imm; | |
1851 | reg->u32_min_value = (u32)imm; | |
1852 | reg->u32_max_value = (u32)imm; | |
1853 | } | |
1854 | ||
e688c3db AS |
1855 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1856 | * known to have the value @imm. | |
1857 | */ | |
1858 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1859 | { | |
a73bf9f2 | 1860 | /* Clear off and union(map_ptr, range) */ |
e688c3db AS |
1861 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
1862 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
a73bf9f2 AN |
1863 | reg->id = 0; |
1864 | reg->ref_obj_id = 0; | |
e688c3db AS |
1865 | ___mark_reg_known(reg, imm); |
1866 | } | |
1867 | ||
3f50f132 JF |
1868 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1869 | { | |
1870 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1871 | reg->s32_min_value = (s32)imm; | |
1872 | reg->s32_max_value = (s32)imm; | |
1873 | reg->u32_min_value = (u32)imm; | |
1874 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1875 | } |
1876 | ||
f1174f77 EC |
1877 | /* Mark the 'variable offset' part of a register as zero. This should be |
1878 | * used only on registers holding a pointer type. | |
1879 | */ | |
1880 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1881 | { |
b03c9f9f | 1882 | __mark_reg_known(reg, 0); |
f1174f77 | 1883 | } |
a9789ef9 | 1884 | |
cc2b14d5 AS |
1885 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1886 | { | |
1887 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1888 | reg->type = SCALAR_VALUE; |
1889 | } | |
1890 | ||
61bd5218 JK |
1891 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1892 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1893 | { |
1894 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1895 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1896 | /* Something bad happened, let's kill all regs */ |
1897 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1898 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1899 | return; |
1900 | } | |
1901 | __mark_reg_known_zero(regs + regno); | |
1902 | } | |
1903 | ||
27060531 | 1904 | static void __mark_dynptr_reg(struct bpf_reg_state *reg, enum bpf_dynptr_type type, |
f8064ab9 | 1905 | bool first_slot, int dynptr_id) |
27060531 KKD |
1906 | { |
1907 | /* reg->type has no meaning for STACK_DYNPTR, but when we set reg for | |
1908 | * callback arguments, it does need to be CONST_PTR_TO_DYNPTR, so simply | |
1909 | * set it unconditionally as it is ignored for STACK_DYNPTR anyway. | |
1910 | */ | |
1911 | __mark_reg_known_zero(reg); | |
1912 | reg->type = CONST_PTR_TO_DYNPTR; | |
f8064ab9 KKD |
1913 | /* Give each dynptr a unique id to uniquely associate slices to it. */ |
1914 | reg->id = dynptr_id; | |
27060531 KKD |
1915 | reg->dynptr.type = type; |
1916 | reg->dynptr.first_slot = first_slot; | |
1917 | } | |
1918 | ||
4ddb7416 DB |
1919 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1920 | { | |
c25b2ae1 | 1921 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
4ddb7416 DB |
1922 | const struct bpf_map *map = reg->map_ptr; |
1923 | ||
1924 | if (map->inner_map_meta) { | |
1925 | reg->type = CONST_PTR_TO_MAP; | |
1926 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1927 | /* transfer reg's id which is unique for every map_lookup_elem |
1928 | * as UID of the inner map. | |
1929 | */ | |
db559117 | 1930 | if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER)) |
34d11a44 | 1931 | reg->map_uid = reg->id; |
4ddb7416 DB |
1932 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1933 | reg->type = PTR_TO_XDP_SOCK; | |
1934 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1935 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1936 | reg->type = PTR_TO_SOCKET; | |
1937 | } else { | |
1938 | reg->type = PTR_TO_MAP_VALUE; | |
1939 | } | |
c25b2ae1 | 1940 | return; |
4ddb7416 | 1941 | } |
c25b2ae1 HL |
1942 | |
1943 | reg->type &= ~PTR_MAYBE_NULL; | |
4ddb7416 DB |
1944 | } |
1945 | ||
5d92ddc3 DM |
1946 | static void mark_reg_graph_node(struct bpf_reg_state *regs, u32 regno, |
1947 | struct btf_field_graph_root *ds_head) | |
1948 | { | |
1949 | __mark_reg_known_zero(®s[regno]); | |
1950 | regs[regno].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
1951 | regs[regno].btf = ds_head->btf; | |
1952 | regs[regno].btf_id = ds_head->value_btf_id; | |
1953 | regs[regno].off = ds_head->node_offset; | |
1954 | } | |
1955 | ||
de8f3a83 DB |
1956 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1957 | { | |
1958 | return type_is_pkt_pointer(reg->type); | |
1959 | } | |
1960 | ||
1961 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1962 | { | |
1963 | return reg_is_pkt_pointer(reg) || | |
1964 | reg->type == PTR_TO_PACKET_END; | |
1965 | } | |
1966 | ||
66e3a13e JK |
1967 | static bool reg_is_dynptr_slice_pkt(const struct bpf_reg_state *reg) |
1968 | { | |
1969 | return base_type(reg->type) == PTR_TO_MEM && | |
1970 | (reg->type & DYNPTR_TYPE_SKB || reg->type & DYNPTR_TYPE_XDP); | |
1971 | } | |
1972 | ||
de8f3a83 DB |
1973 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
1974 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1975 | enum bpf_reg_type which) | |
1976 | { | |
1977 | /* The register can already have a range from prior markings. | |
1978 | * This is fine as long as it hasn't been advanced from its | |
1979 | * origin. | |
1980 | */ | |
1981 | return reg->type == which && | |
1982 | reg->id == 0 && | |
1983 | reg->off == 0 && | |
1984 | tnum_equals_const(reg->var_off, 0); | |
1985 | } | |
1986 | ||
3f50f132 JF |
1987 | /* Reset the min/max bounds of a register */ |
1988 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1989 | { | |
1990 | reg->smin_value = S64_MIN; | |
1991 | reg->smax_value = S64_MAX; | |
1992 | reg->umin_value = 0; | |
1993 | reg->umax_value = U64_MAX; | |
1994 | ||
1995 | reg->s32_min_value = S32_MIN; | |
1996 | reg->s32_max_value = S32_MAX; | |
1997 | reg->u32_min_value = 0; | |
1998 | reg->u32_max_value = U32_MAX; | |
1999 | } | |
2000 | ||
2001 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
2002 | { | |
2003 | reg->smin_value = S64_MIN; | |
2004 | reg->smax_value = S64_MAX; | |
2005 | reg->umin_value = 0; | |
2006 | reg->umax_value = U64_MAX; | |
2007 | } | |
2008 | ||
2009 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
2010 | { | |
2011 | reg->s32_min_value = S32_MIN; | |
2012 | reg->s32_max_value = S32_MAX; | |
2013 | reg->u32_min_value = 0; | |
2014 | reg->u32_max_value = U32_MAX; | |
2015 | } | |
2016 | ||
2017 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
2018 | { | |
2019 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
2020 | ||
2021 | /* min signed is max(sign bit) | min(other bits) */ | |
2022 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
2023 | var32_off.value | (var32_off.mask & S32_MIN)); | |
2024 | /* max signed is min(sign bit) | max(other bits) */ | |
2025 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
2026 | var32_off.value | (var32_off.mask & S32_MAX)); | |
2027 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
2028 | reg->u32_max_value = min(reg->u32_max_value, | |
2029 | (u32)(var32_off.value | var32_off.mask)); | |
2030 | } | |
2031 | ||
2032 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
2033 | { |
2034 | /* min signed is max(sign bit) | min(other bits) */ | |
2035 | reg->smin_value = max_t(s64, reg->smin_value, | |
2036 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
2037 | /* max signed is min(sign bit) | max(other bits) */ | |
2038 | reg->smax_value = min_t(s64, reg->smax_value, | |
2039 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
2040 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
2041 | reg->umax_value = min(reg->umax_value, | |
2042 | reg->var_off.value | reg->var_off.mask); | |
2043 | } | |
2044 | ||
3f50f132 JF |
2045 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
2046 | { | |
2047 | __update_reg32_bounds(reg); | |
2048 | __update_reg64_bounds(reg); | |
2049 | } | |
2050 | ||
b03c9f9f | 2051 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
2052 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
2053 | { | |
2054 | /* Learn sign from signed bounds. | |
2055 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
2056 | * are the same, so combine. This works even in the negative case, e.g. | |
2057 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
2058 | */ | |
2059 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
2060 | reg->s32_min_value = reg->u32_min_value = | |
2061 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
2062 | reg->s32_max_value = reg->u32_max_value = | |
2063 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
2064 | return; | |
2065 | } | |
2066 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
2067 | * boundary, so we must be careful. | |
2068 | */ | |
2069 | if ((s32)reg->u32_max_value >= 0) { | |
2070 | /* Positive. We can't learn anything from the smin, but smax | |
2071 | * is positive, hence safe. | |
2072 | */ | |
2073 | reg->s32_min_value = reg->u32_min_value; | |
2074 | reg->s32_max_value = reg->u32_max_value = | |
2075 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
2076 | } else if ((s32)reg->u32_min_value < 0) { | |
2077 | /* Negative. We can't learn anything from the smax, but smin | |
2078 | * is negative, hence safe. | |
2079 | */ | |
2080 | reg->s32_min_value = reg->u32_min_value = | |
2081 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
2082 | reg->s32_max_value = reg->u32_max_value; | |
2083 | } | |
2084 | } | |
2085 | ||
2086 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
2087 | { |
2088 | /* Learn sign from signed bounds. | |
2089 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
2090 | * are the same, so combine. This works even in the negative case, e.g. | |
2091 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
2092 | */ | |
2093 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
2094 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
2095 | reg->umin_value); | |
2096 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
2097 | reg->umax_value); | |
2098 | return; | |
2099 | } | |
2100 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
2101 | * boundary, so we must be careful. | |
2102 | */ | |
2103 | if ((s64)reg->umax_value >= 0) { | |
2104 | /* Positive. We can't learn anything from the smin, but smax | |
2105 | * is positive, hence safe. | |
2106 | */ | |
2107 | reg->smin_value = reg->umin_value; | |
2108 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
2109 | reg->umax_value); | |
2110 | } else if ((s64)reg->umin_value < 0) { | |
2111 | /* Negative. We can't learn anything from the smax, but smin | |
2112 | * is negative, hence safe. | |
2113 | */ | |
2114 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
2115 | reg->umin_value); | |
2116 | reg->smax_value = reg->umax_value; | |
2117 | } | |
2118 | } | |
2119 | ||
3f50f132 JF |
2120 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
2121 | { | |
2122 | __reg32_deduce_bounds(reg); | |
2123 | __reg64_deduce_bounds(reg); | |
2124 | } | |
2125 | ||
b03c9f9f EC |
2126 | /* Attempts to improve var_off based on unsigned min/max information */ |
2127 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
2128 | { | |
3f50f132 JF |
2129 | struct tnum var64_off = tnum_intersect(reg->var_off, |
2130 | tnum_range(reg->umin_value, | |
2131 | reg->umax_value)); | |
7be14c1c DB |
2132 | struct tnum var32_off = tnum_intersect(tnum_subreg(var64_off), |
2133 | tnum_range(reg->u32_min_value, | |
2134 | reg->u32_max_value)); | |
3f50f132 JF |
2135 | |
2136 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
2137 | } |
2138 | ||
3844d153 DB |
2139 | static void reg_bounds_sync(struct bpf_reg_state *reg) |
2140 | { | |
2141 | /* We might have learned new bounds from the var_off. */ | |
2142 | __update_reg_bounds(reg); | |
2143 | /* We might have learned something about the sign bit. */ | |
2144 | __reg_deduce_bounds(reg); | |
2145 | /* We might have learned some bits from the bounds. */ | |
2146 | __reg_bound_offset(reg); | |
2147 | /* Intersecting with the old var_off might have improved our bounds | |
2148 | * slightly, e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
2149 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
2150 | */ | |
2151 | __update_reg_bounds(reg); | |
2152 | } | |
2153 | ||
e572ff80 DB |
2154 | static bool __reg32_bound_s64(s32 a) |
2155 | { | |
2156 | return a >= 0 && a <= S32_MAX; | |
2157 | } | |
2158 | ||
3f50f132 | 2159 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 2160 | { |
3f50f132 JF |
2161 | reg->umin_value = reg->u32_min_value; |
2162 | reg->umax_value = reg->u32_max_value; | |
e572ff80 DB |
2163 | |
2164 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must | |
2165 | * be positive otherwise set to worse case bounds and refine later | |
2166 | * from tnum. | |
3f50f132 | 2167 | */ |
e572ff80 DB |
2168 | if (__reg32_bound_s64(reg->s32_min_value) && |
2169 | __reg32_bound_s64(reg->s32_max_value)) { | |
3a71dc36 | 2170 | reg->smin_value = reg->s32_min_value; |
e572ff80 DB |
2171 | reg->smax_value = reg->s32_max_value; |
2172 | } else { | |
3a71dc36 | 2173 | reg->smin_value = 0; |
e572ff80 DB |
2174 | reg->smax_value = U32_MAX; |
2175 | } | |
3f50f132 JF |
2176 | } |
2177 | ||
2178 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
2179 | { | |
2180 | /* special case when 64-bit register has upper 32-bit register | |
2181 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
2182 | * allowing us to use 32-bit bounds directly, | |
2183 | */ | |
2184 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
2185 | __reg_assign_32_into_64(reg); | |
2186 | } else { | |
2187 | /* Otherwise the best we can do is push lower 32bit known and | |
2188 | * unknown bits into register (var_off set from jmp logic) | |
2189 | * then learn as much as possible from the 64-bit tnum | |
2190 | * known and unknown bits. The previous smin/smax bounds are | |
2191 | * invalid here because of jmp32 compare so mark them unknown | |
2192 | * so they do not impact tnum bounds calculation. | |
2193 | */ | |
2194 | __mark_reg64_unbounded(reg); | |
3f50f132 | 2195 | } |
3844d153 | 2196 | reg_bounds_sync(reg); |
3f50f132 JF |
2197 | } |
2198 | ||
2199 | static bool __reg64_bound_s32(s64 a) | |
2200 | { | |
388e2c0b | 2201 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
2202 | } |
2203 | ||
2204 | static bool __reg64_bound_u32(u64 a) | |
2205 | { | |
b9979db8 | 2206 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
2207 | } |
2208 | ||
2209 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
2210 | { | |
2211 | __mark_reg32_unbounded(reg); | |
b0270958 | 2212 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 2213 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 2214 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 2215 | } |
10bf4e83 | 2216 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 2217 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 2218 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 2219 | } |
3844d153 | 2220 | reg_bounds_sync(reg); |
b03c9f9f EC |
2221 | } |
2222 | ||
f1174f77 | 2223 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
2224 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
2225 | struct bpf_reg_state *reg) | |
f1174f77 | 2226 | { |
a9c676bc | 2227 | /* |
a73bf9f2 | 2228 | * Clear type, off, and union(map_ptr, range) and |
a9c676bc AS |
2229 | * padding between 'type' and union |
2230 | */ | |
2231 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 2232 | reg->type = SCALAR_VALUE; |
a73bf9f2 AN |
2233 | reg->id = 0; |
2234 | reg->ref_obj_id = 0; | |
f1174f77 | 2235 | reg->var_off = tnum_unknown; |
f4d7e40a | 2236 | reg->frameno = 0; |
be2ef816 | 2237 | reg->precise = !env->bpf_capable; |
b03c9f9f | 2238 | __mark_reg_unbounded(reg); |
f1174f77 EC |
2239 | } |
2240 | ||
61bd5218 JK |
2241 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
2242 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
2243 | { |
2244 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 2245 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
2246 | /* Something bad happened, let's kill all regs except FP */ |
2247 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 2248 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
2249 | return; |
2250 | } | |
f54c7898 | 2251 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
2252 | } |
2253 | ||
f54c7898 DB |
2254 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
2255 | struct bpf_reg_state *reg) | |
f1174f77 | 2256 | { |
f54c7898 | 2257 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
2258 | reg->type = NOT_INIT; |
2259 | } | |
2260 | ||
61bd5218 JK |
2261 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
2262 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
2263 | { |
2264 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 2265 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
2266 | /* Something bad happened, let's kill all regs except FP */ |
2267 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 2268 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
2269 | return; |
2270 | } | |
f54c7898 | 2271 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
2272 | } |
2273 | ||
41c48f3a AI |
2274 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
2275 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f | 2276 | enum bpf_reg_type reg_type, |
c6f1bfe8 YS |
2277 | struct btf *btf, u32 btf_id, |
2278 | enum bpf_type_flag flag) | |
41c48f3a AI |
2279 | { |
2280 | if (reg_type == SCALAR_VALUE) { | |
2281 | mark_reg_unknown(env, regs, regno); | |
2282 | return; | |
2283 | } | |
2284 | mark_reg_known_zero(env, regs, regno); | |
c6f1bfe8 | 2285 | regs[regno].type = PTR_TO_BTF_ID | flag; |
22dc4a0f | 2286 | regs[regno].btf = btf; |
41c48f3a AI |
2287 | regs[regno].btf_id = btf_id; |
2288 | } | |
2289 | ||
5327ed3d | 2290 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 2291 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 2292 | struct bpf_func_state *state) |
17a52670 | 2293 | { |
f4d7e40a | 2294 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
2295 | int i; |
2296 | ||
dc503a8a | 2297 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 2298 | mark_reg_not_init(env, regs, i); |
dc503a8a | 2299 | regs[i].live = REG_LIVE_NONE; |
679c782d | 2300 | regs[i].parent = NULL; |
5327ed3d | 2301 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 2302 | } |
17a52670 AS |
2303 | |
2304 | /* frame pointer */ | |
f1174f77 | 2305 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 2306 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 2307 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
2308 | } |
2309 | ||
f4d7e40a AS |
2310 | #define BPF_MAIN_FUNC (-1) |
2311 | static void init_func_state(struct bpf_verifier_env *env, | |
2312 | struct bpf_func_state *state, | |
2313 | int callsite, int frameno, int subprogno) | |
2314 | { | |
2315 | state->callsite = callsite; | |
2316 | state->frameno = frameno; | |
2317 | state->subprogno = subprogno; | |
1bfe26fb | 2318 | state->callback_ret_range = tnum_range(0, 0); |
f4d7e40a | 2319 | init_reg_state(env, state); |
0f55f9ed | 2320 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
2321 | } |
2322 | ||
bfc6bb74 AS |
2323 | /* Similar to push_stack(), but for async callbacks */ |
2324 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
2325 | int insn_idx, int prev_insn_idx, | |
2326 | int subprog) | |
2327 | { | |
2328 | struct bpf_verifier_stack_elem *elem; | |
2329 | struct bpf_func_state *frame; | |
2330 | ||
2331 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
2332 | if (!elem) | |
2333 | goto err; | |
2334 | ||
2335 | elem->insn_idx = insn_idx; | |
2336 | elem->prev_insn_idx = prev_insn_idx; | |
2337 | elem->next = env->head; | |
12166409 | 2338 | elem->log_pos = env->log.end_pos; |
bfc6bb74 AS |
2339 | env->head = elem; |
2340 | env->stack_size++; | |
2341 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
2342 | verbose(env, | |
2343 | "The sequence of %d jumps is too complex for async cb.\n", | |
2344 | env->stack_size); | |
2345 | goto err; | |
2346 | } | |
2347 | /* Unlike push_stack() do not copy_verifier_state(). | |
2348 | * The caller state doesn't matter. | |
2349 | * This is async callback. It starts in a fresh stack. | |
2350 | * Initialize it similar to do_check_common(). | |
2351 | */ | |
2352 | elem->st.branches = 1; | |
2353 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
2354 | if (!frame) | |
2355 | goto err; | |
2356 | init_func_state(env, frame, | |
2357 | BPF_MAIN_FUNC /* callsite */, | |
2358 | 0 /* frameno within this callchain */, | |
2359 | subprog /* subprog number within this prog */); | |
2360 | elem->st.frame[0] = frame; | |
2361 | return &elem->st; | |
2362 | err: | |
2363 | free_verifier_state(env->cur_state, true); | |
2364 | env->cur_state = NULL; | |
2365 | /* pop all elements and return */ | |
2366 | while (!pop_stack(env, NULL, NULL, false)); | |
2367 | return NULL; | |
2368 | } | |
2369 | ||
2370 | ||
17a52670 AS |
2371 | enum reg_arg_type { |
2372 | SRC_OP, /* register is used as source operand */ | |
2373 | DST_OP, /* register is used as destination operand */ | |
2374 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
2375 | }; | |
2376 | ||
cc8b0b92 AS |
2377 | static int cmp_subprogs(const void *a, const void *b) |
2378 | { | |
9c8105bd JW |
2379 | return ((struct bpf_subprog_info *)a)->start - |
2380 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
2381 | } |
2382 | ||
2383 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
2384 | { | |
9c8105bd | 2385 | struct bpf_subprog_info *p; |
cc8b0b92 | 2386 | |
9c8105bd JW |
2387 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
2388 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
2389 | if (!p) |
2390 | return -ENOENT; | |
9c8105bd | 2391 | return p - env->subprog_info; |
cc8b0b92 AS |
2392 | |
2393 | } | |
2394 | ||
2395 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
2396 | { | |
2397 | int insn_cnt = env->prog->len; | |
2398 | int ret; | |
2399 | ||
2400 | if (off >= insn_cnt || off < 0) { | |
2401 | verbose(env, "call to invalid destination\n"); | |
2402 | return -EINVAL; | |
2403 | } | |
2404 | ret = find_subprog(env, off); | |
2405 | if (ret >= 0) | |
282a0f46 | 2406 | return ret; |
4cb3d99c | 2407 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
2408 | verbose(env, "too many subprograms\n"); |
2409 | return -E2BIG; | |
2410 | } | |
e6ac2450 | 2411 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
2412 | env->subprog_info[env->subprog_cnt++].start = off; |
2413 | sort(env->subprog_info, env->subprog_cnt, | |
2414 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 2415 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
2416 | } |
2417 | ||
2357672c KKD |
2418 | #define MAX_KFUNC_DESCS 256 |
2419 | #define MAX_KFUNC_BTFS 256 | |
2420 | ||
e6ac2450 MKL |
2421 | struct bpf_kfunc_desc { |
2422 | struct btf_func_model func_model; | |
2423 | u32 func_id; | |
2424 | s32 imm; | |
2357672c | 2425 | u16 offset; |
1cf3bfc6 | 2426 | unsigned long addr; |
2357672c KKD |
2427 | }; |
2428 | ||
2429 | struct bpf_kfunc_btf { | |
2430 | struct btf *btf; | |
2431 | struct module *module; | |
2432 | u16 offset; | |
e6ac2450 MKL |
2433 | }; |
2434 | ||
e6ac2450 | 2435 | struct bpf_kfunc_desc_tab { |
1cf3bfc6 IL |
2436 | /* Sorted by func_id (BTF ID) and offset (fd_array offset) during |
2437 | * verification. JITs do lookups by bpf_insn, where func_id may not be | |
2438 | * available, therefore at the end of verification do_misc_fixups() | |
2439 | * sorts this by imm and offset. | |
2440 | */ | |
e6ac2450 MKL |
2441 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; |
2442 | u32 nr_descs; | |
2443 | }; | |
2444 | ||
2357672c KKD |
2445 | struct bpf_kfunc_btf_tab { |
2446 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
2447 | u32 nr_descs; | |
2448 | }; | |
2449 | ||
2450 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
2451 | { |
2452 | const struct bpf_kfunc_desc *d0 = a; | |
2453 | const struct bpf_kfunc_desc *d1 = b; | |
2454 | ||
2455 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
2456 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
2457 | } | |
2458 | ||
2459 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
2460 | { | |
2461 | const struct bpf_kfunc_btf *d0 = a; | |
2462 | const struct bpf_kfunc_btf *d1 = b; | |
2463 | ||
2464 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
2465 | } |
2466 | ||
2467 | static const struct bpf_kfunc_desc * | |
2357672c | 2468 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
2469 | { |
2470 | struct bpf_kfunc_desc desc = { | |
2471 | .func_id = func_id, | |
2357672c | 2472 | .offset = offset, |
e6ac2450 MKL |
2473 | }; |
2474 | struct bpf_kfunc_desc_tab *tab; | |
2475 | ||
2476 | tab = prog->aux->kfunc_tab; | |
2477 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
2478 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
2479 | } | |
2480 | ||
1cf3bfc6 IL |
2481 | int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id, |
2482 | u16 btf_fd_idx, u8 **func_addr) | |
2483 | { | |
2484 | const struct bpf_kfunc_desc *desc; | |
2485 | ||
2486 | desc = find_kfunc_desc(prog, func_id, btf_fd_idx); | |
2487 | if (!desc) | |
2488 | return -EFAULT; | |
2489 | ||
2490 | *func_addr = (u8 *)desc->addr; | |
2491 | return 0; | |
2492 | } | |
2493 | ||
2357672c | 2494 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, |
b202d844 | 2495 | s16 offset) |
2357672c KKD |
2496 | { |
2497 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
2498 | struct bpf_kfunc_btf_tab *tab; | |
2499 | struct bpf_kfunc_btf *b; | |
2500 | struct module *mod; | |
2501 | struct btf *btf; | |
2502 | int btf_fd; | |
2503 | ||
2504 | tab = env->prog->aux->kfunc_btf_tab; | |
2505 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
2506 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
2507 | if (!b) { | |
2508 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
2509 | verbose(env, "too many different module BTFs\n"); | |
2510 | return ERR_PTR(-E2BIG); | |
2511 | } | |
2512 | ||
2513 | if (bpfptr_is_null(env->fd_array)) { | |
2514 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
2515 | return ERR_PTR(-EPROTO); | |
2516 | } | |
2517 | ||
2518 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
2519 | offset * sizeof(btf_fd), | |
2520 | sizeof(btf_fd))) | |
2521 | return ERR_PTR(-EFAULT); | |
2522 | ||
2523 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
2524 | if (IS_ERR(btf)) { |
2525 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 2526 | return btf; |
588cd7ef | 2527 | } |
2357672c KKD |
2528 | |
2529 | if (!btf_is_module(btf)) { | |
2530 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
2531 | btf_put(btf); | |
2532 | return ERR_PTR(-EINVAL); | |
2533 | } | |
2534 | ||
2535 | mod = btf_try_get_module(btf); | |
2536 | if (!mod) { | |
2537 | btf_put(btf); | |
2538 | return ERR_PTR(-ENXIO); | |
2539 | } | |
2540 | ||
2541 | b = &tab->descs[tab->nr_descs++]; | |
2542 | b->btf = btf; | |
2543 | b->module = mod; | |
2544 | b->offset = offset; | |
2545 | ||
2546 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2547 | kfunc_btf_cmp_by_off, NULL); | |
2548 | } | |
2357672c | 2549 | return b->btf; |
e6ac2450 MKL |
2550 | } |
2551 | ||
2357672c KKD |
2552 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
2553 | { | |
2554 | if (!tab) | |
2555 | return; | |
2556 | ||
2557 | while (tab->nr_descs--) { | |
2558 | module_put(tab->descs[tab->nr_descs].module); | |
2559 | btf_put(tab->descs[tab->nr_descs].btf); | |
2560 | } | |
2561 | kfree(tab); | |
2562 | } | |
2563 | ||
43bf0878 | 2564 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset) |
2357672c | 2565 | { |
2357672c KKD |
2566 | if (offset) { |
2567 | if (offset < 0) { | |
2568 | /* In the future, this can be allowed to increase limit | |
2569 | * of fd index into fd_array, interpreted as u16. | |
2570 | */ | |
2571 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
2572 | return ERR_PTR(-EINVAL); | |
2573 | } | |
2574 | ||
b202d844 | 2575 | return __find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2576 | } |
2577 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
2578 | } |
2579 | ||
2357672c | 2580 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
2581 | { |
2582 | const struct btf_type *func, *func_proto; | |
2357672c | 2583 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
2584 | struct bpf_kfunc_desc_tab *tab; |
2585 | struct bpf_prog_aux *prog_aux; | |
2586 | struct bpf_kfunc_desc *desc; | |
2587 | const char *func_name; | |
2357672c | 2588 | struct btf *desc_btf; |
8cbf062a | 2589 | unsigned long call_imm; |
e6ac2450 MKL |
2590 | unsigned long addr; |
2591 | int err; | |
2592 | ||
2593 | prog_aux = env->prog->aux; | |
2594 | tab = prog_aux->kfunc_tab; | |
2357672c | 2595 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
2596 | if (!tab) { |
2597 | if (!btf_vmlinux) { | |
2598 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
2599 | return -ENOTSUPP; | |
2600 | } | |
2601 | ||
2602 | if (!env->prog->jit_requested) { | |
2603 | verbose(env, "JIT is required for calling kernel function\n"); | |
2604 | return -ENOTSUPP; | |
2605 | } | |
2606 | ||
2607 | if (!bpf_jit_supports_kfunc_call()) { | |
2608 | verbose(env, "JIT does not support calling kernel function\n"); | |
2609 | return -ENOTSUPP; | |
2610 | } | |
2611 | ||
2612 | if (!env->prog->gpl_compatible) { | |
2613 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
2614 | return -EINVAL; | |
2615 | } | |
2616 | ||
2617 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
2618 | if (!tab) | |
2619 | return -ENOMEM; | |
2620 | prog_aux->kfunc_tab = tab; | |
2621 | } | |
2622 | ||
a5d82727 KKD |
2623 | /* func_id == 0 is always invalid, but instead of returning an error, be |
2624 | * conservative and wait until the code elimination pass before returning | |
2625 | * error, so that invalid calls that get pruned out can be in BPF programs | |
2626 | * loaded from userspace. It is also required that offset be untouched | |
2627 | * for such calls. | |
2628 | */ | |
2629 | if (!func_id && !offset) | |
2630 | return 0; | |
2631 | ||
2357672c KKD |
2632 | if (!btf_tab && offset) { |
2633 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
2634 | if (!btf_tab) | |
2635 | return -ENOMEM; | |
2636 | prog_aux->kfunc_btf_tab = btf_tab; | |
2637 | } | |
2638 | ||
43bf0878 | 2639 | desc_btf = find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2640 | if (IS_ERR(desc_btf)) { |
2641 | verbose(env, "failed to find BTF for kernel function\n"); | |
2642 | return PTR_ERR(desc_btf); | |
2643 | } | |
2644 | ||
2645 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
2646 | return 0; |
2647 | ||
2648 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
2649 | verbose(env, "too many different kernel function calls\n"); | |
2650 | return -E2BIG; | |
2651 | } | |
2652 | ||
2357672c | 2653 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
2654 | if (!func || !btf_type_is_func(func)) { |
2655 | verbose(env, "kernel btf_id %u is not a function\n", | |
2656 | func_id); | |
2657 | return -EINVAL; | |
2658 | } | |
2357672c | 2659 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
2660 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
2661 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
2662 | func_id); | |
2663 | return -EINVAL; | |
2664 | } | |
2665 | ||
2357672c | 2666 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
2667 | addr = kallsyms_lookup_name(func_name); |
2668 | if (!addr) { | |
2669 | verbose(env, "cannot find address for kernel function %s\n", | |
2670 | func_name); | |
2671 | return -EINVAL; | |
2672 | } | |
1cf3bfc6 | 2673 | specialize_kfunc(env, func_id, offset, &addr); |
e6ac2450 | 2674 | |
1cf3bfc6 IL |
2675 | if (bpf_jit_supports_far_kfunc_call()) { |
2676 | call_imm = func_id; | |
2677 | } else { | |
2678 | call_imm = BPF_CALL_IMM(addr); | |
2679 | /* Check whether the relative offset overflows desc->imm */ | |
2680 | if ((unsigned long)(s32)call_imm != call_imm) { | |
2681 | verbose(env, "address of kernel function %s is out of range\n", | |
2682 | func_name); | |
2683 | return -EINVAL; | |
2684 | } | |
8cbf062a HT |
2685 | } |
2686 | ||
3d76a4d3 SF |
2687 | if (bpf_dev_bound_kfunc_id(func_id)) { |
2688 | err = bpf_dev_bound_kfunc_check(&env->log, prog_aux); | |
2689 | if (err) | |
2690 | return err; | |
2691 | } | |
2692 | ||
e6ac2450 MKL |
2693 | desc = &tab->descs[tab->nr_descs++]; |
2694 | desc->func_id = func_id; | |
8cbf062a | 2695 | desc->imm = call_imm; |
2357672c | 2696 | desc->offset = offset; |
1cf3bfc6 | 2697 | desc->addr = addr; |
2357672c | 2698 | err = btf_distill_func_proto(&env->log, desc_btf, |
e6ac2450 MKL |
2699 | func_proto, func_name, |
2700 | &desc->func_model); | |
2701 | if (!err) | |
2702 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 2703 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
2704 | return err; |
2705 | } | |
2706 | ||
1cf3bfc6 | 2707 | static int kfunc_desc_cmp_by_imm_off(const void *a, const void *b) |
e6ac2450 MKL |
2708 | { |
2709 | const struct bpf_kfunc_desc *d0 = a; | |
2710 | const struct bpf_kfunc_desc *d1 = b; | |
2711 | ||
1cf3bfc6 IL |
2712 | if (d0->imm != d1->imm) |
2713 | return d0->imm < d1->imm ? -1 : 1; | |
2714 | if (d0->offset != d1->offset) | |
2715 | return d0->offset < d1->offset ? -1 : 1; | |
e6ac2450 MKL |
2716 | return 0; |
2717 | } | |
2718 | ||
1cf3bfc6 | 2719 | static void sort_kfunc_descs_by_imm_off(struct bpf_prog *prog) |
e6ac2450 MKL |
2720 | { |
2721 | struct bpf_kfunc_desc_tab *tab; | |
2722 | ||
2723 | tab = prog->aux->kfunc_tab; | |
2724 | if (!tab) | |
2725 | return; | |
2726 | ||
2727 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1cf3bfc6 | 2728 | kfunc_desc_cmp_by_imm_off, NULL); |
e6ac2450 MKL |
2729 | } |
2730 | ||
2731 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
2732 | { | |
2733 | return !!prog->aux->kfunc_tab; | |
2734 | } | |
2735 | ||
2736 | const struct btf_func_model * | |
2737 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2738 | const struct bpf_insn *insn) | |
2739 | { | |
2740 | const struct bpf_kfunc_desc desc = { | |
2741 | .imm = insn->imm, | |
1cf3bfc6 | 2742 | .offset = insn->off, |
e6ac2450 MKL |
2743 | }; |
2744 | const struct bpf_kfunc_desc *res; | |
2745 | struct bpf_kfunc_desc_tab *tab; | |
2746 | ||
2747 | tab = prog->aux->kfunc_tab; | |
2748 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
1cf3bfc6 | 2749 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm_off); |
e6ac2450 MKL |
2750 | |
2751 | return res ? &res->func_model : NULL; | |
2752 | } | |
2753 | ||
2754 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2755 | { |
9c8105bd | 2756 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2757 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2758 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2759 | |
f910cefa JW |
2760 | /* Add entry function. */ |
2761 | ret = add_subprog(env, 0); | |
e6ac2450 | 2762 | if (ret) |
f910cefa JW |
2763 | return ret; |
2764 | ||
e6ac2450 MKL |
2765 | for (i = 0; i < insn_cnt; i++, insn++) { |
2766 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2767 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2768 | continue; |
e6ac2450 | 2769 | |
2c78ee89 | 2770 | if (!env->bpf_capable) { |
e6ac2450 | 2771 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2772 | return -EPERM; |
2773 | } | |
e6ac2450 | 2774 | |
3990ed4c | 2775 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2776 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2777 | else |
2357672c | 2778 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2779 | |
cc8b0b92 AS |
2780 | if (ret < 0) |
2781 | return ret; | |
2782 | } | |
2783 | ||
4cb3d99c JW |
2784 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2785 | * logic. 'subprog_cnt' should not be increased. | |
2786 | */ | |
2787 | subprog[env->subprog_cnt].start = insn_cnt; | |
2788 | ||
06ee7115 | 2789 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2790 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2791 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2792 | |
e6ac2450 MKL |
2793 | return 0; |
2794 | } | |
2795 | ||
2796 | static int check_subprogs(struct bpf_verifier_env *env) | |
2797 | { | |
2798 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2799 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2800 | struct bpf_insn *insn = env->prog->insnsi; | |
2801 | int insn_cnt = env->prog->len; | |
2802 | ||
cc8b0b92 | 2803 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2804 | subprog_start = subprog[cur_subprog].start; |
2805 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2806 | for (i = 0; i < insn_cnt; i++) { |
2807 | u8 code = insn[i].code; | |
2808 | ||
7f6e4312 | 2809 | if (code == (BPF_JMP | BPF_CALL) && |
df2ccc18 IL |
2810 | insn[i].src_reg == 0 && |
2811 | insn[i].imm == BPF_FUNC_tail_call) | |
7f6e4312 | 2812 | subprog[cur_subprog].has_tail_call = true; |
09b28d76 AS |
2813 | if (BPF_CLASS(code) == BPF_LD && |
2814 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2815 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2816 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2817 | goto next; |
2818 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2819 | goto next; | |
2820 | off = i + insn[i].off + 1; | |
2821 | if (off < subprog_start || off >= subprog_end) { | |
2822 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2823 | return -EINVAL; | |
2824 | } | |
2825 | next: | |
2826 | if (i == subprog_end - 1) { | |
2827 | /* to avoid fall-through from one subprog into another | |
2828 | * the last insn of the subprog should be either exit | |
2829 | * or unconditional jump back | |
2830 | */ | |
2831 | if (code != (BPF_JMP | BPF_EXIT) && | |
2832 | code != (BPF_JMP | BPF_JA)) { | |
2833 | verbose(env, "last insn is not an exit or jmp\n"); | |
2834 | return -EINVAL; | |
2835 | } | |
2836 | subprog_start = subprog_end; | |
4cb3d99c JW |
2837 | cur_subprog++; |
2838 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2839 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2840 | } |
2841 | } | |
2842 | return 0; | |
2843 | } | |
2844 | ||
679c782d EC |
2845 | /* Parentage chain of this register (or stack slot) should take care of all |
2846 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2847 | */ | |
f4d7e40a | 2848 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2849 | const struct bpf_reg_state *state, |
5327ed3d | 2850 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2851 | { |
2852 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2853 | int cnt = 0; |
dc503a8a EC |
2854 | |
2855 | while (parent) { | |
2856 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2857 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2858 | break; |
9242b5f5 AS |
2859 | if (parent->live & REG_LIVE_DONE) { |
2860 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
c25b2ae1 | 2861 | reg_type_str(env, parent->type), |
9242b5f5 AS |
2862 | parent->var_off.value, parent->off); |
2863 | return -EFAULT; | |
2864 | } | |
5327ed3d JW |
2865 | /* The first condition is more likely to be true than the |
2866 | * second, checked it first. | |
2867 | */ | |
2868 | if ((parent->live & REG_LIVE_READ) == flag || | |
2869 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2870 | /* The parentage chain never changes and |
2871 | * this parent was already marked as LIVE_READ. | |
2872 | * There is no need to keep walking the chain again and | |
2873 | * keep re-marking all parents as LIVE_READ. | |
2874 | * This case happens when the same register is read | |
2875 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2876 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2877 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2878 | */ |
2879 | break; | |
dc503a8a | 2880 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2881 | parent->live |= flag; |
2882 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2883 | if (flag == REG_LIVE_READ64) | |
2884 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2885 | state = parent; |
2886 | parent = state->parent; | |
f4d7e40a | 2887 | writes = true; |
06ee7115 | 2888 | cnt++; |
dc503a8a | 2889 | } |
06ee7115 AS |
2890 | |
2891 | if (env->longest_mark_read_walk < cnt) | |
2892 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2893 | return 0; |
dc503a8a EC |
2894 | } |
2895 | ||
d6fefa11 KKD |
2896 | static int mark_dynptr_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
2897 | { | |
2898 | struct bpf_func_state *state = func(env, reg); | |
2899 | int spi, ret; | |
2900 | ||
2901 | /* For CONST_PTR_TO_DYNPTR, it must have already been done by | |
2902 | * check_reg_arg in check_helper_call and mark_btf_func_reg_size in | |
2903 | * check_kfunc_call. | |
2904 | */ | |
2905 | if (reg->type == CONST_PTR_TO_DYNPTR) | |
2906 | return 0; | |
79168a66 KKD |
2907 | spi = dynptr_get_spi(env, reg); |
2908 | if (spi < 0) | |
2909 | return spi; | |
d6fefa11 KKD |
2910 | /* Caller ensures dynptr is valid and initialized, which means spi is in |
2911 | * bounds and spi is the first dynptr slot. Simply mark stack slot as | |
2912 | * read. | |
2913 | */ | |
2914 | ret = mark_reg_read(env, &state->stack[spi].spilled_ptr, | |
2915 | state->stack[spi].spilled_ptr.parent, REG_LIVE_READ64); | |
2916 | if (ret) | |
2917 | return ret; | |
2918 | return mark_reg_read(env, &state->stack[spi - 1].spilled_ptr, | |
2919 | state->stack[spi - 1].spilled_ptr.parent, REG_LIVE_READ64); | |
2920 | } | |
2921 | ||
06accc87 AN |
2922 | static int mark_iter_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
2923 | int spi, int nr_slots) | |
2924 | { | |
2925 | struct bpf_func_state *state = func(env, reg); | |
2926 | int err, i; | |
2927 | ||
2928 | for (i = 0; i < nr_slots; i++) { | |
2929 | struct bpf_reg_state *st = &state->stack[spi - i].spilled_ptr; | |
2930 | ||
2931 | err = mark_reg_read(env, st, st->parent, REG_LIVE_READ64); | |
2932 | if (err) | |
2933 | return err; | |
2934 | ||
2935 | mark_stack_slot_scratched(env, spi - i); | |
2936 | } | |
2937 | ||
2938 | return 0; | |
2939 | } | |
2940 | ||
5327ed3d JW |
2941 | /* This function is supposed to be used by the following 32-bit optimization |
2942 | * code only. It returns TRUE if the source or destination register operates | |
2943 | * on 64-bit, otherwise return FALSE. | |
2944 | */ | |
2945 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2946 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2947 | { | |
2948 | u8 code, class, op; | |
2949 | ||
2950 | code = insn->code; | |
2951 | class = BPF_CLASS(code); | |
2952 | op = BPF_OP(code); | |
2953 | if (class == BPF_JMP) { | |
2954 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2955 | * conservatively. | |
2956 | */ | |
2957 | if (op == BPF_EXIT) | |
2958 | return true; | |
2959 | if (op == BPF_CALL) { | |
2960 | /* BPF to BPF call will reach here because of marking | |
2961 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2962 | * don't care the register def because they are anyway | |
2963 | * marked as NOT_INIT already. | |
2964 | */ | |
2965 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2966 | return false; | |
2967 | /* Helper call will reach here because of arg type | |
2968 | * check, conservatively return TRUE. | |
2969 | */ | |
2970 | if (t == SRC_OP) | |
2971 | return true; | |
2972 | ||
2973 | return false; | |
2974 | } | |
2975 | } | |
2976 | ||
2977 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2978 | /* BPF_END always use BPF_ALU class. */ | |
2979 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2980 | return true; | |
2981 | ||
2982 | if (class == BPF_ALU || class == BPF_JMP32) | |
2983 | return false; | |
2984 | ||
2985 | if (class == BPF_LDX) { | |
2986 | if (t != SRC_OP) | |
2987 | return BPF_SIZE(code) == BPF_DW; | |
2988 | /* LDX source must be ptr. */ | |
2989 | return true; | |
2990 | } | |
2991 | ||
2992 | if (class == BPF_STX) { | |
83a28819 IL |
2993 | /* BPF_STX (including atomic variants) has multiple source |
2994 | * operands, one of which is a ptr. Check whether the caller is | |
2995 | * asking about it. | |
2996 | */ | |
2997 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2998 | return true; |
2999 | return BPF_SIZE(code) == BPF_DW; | |
3000 | } | |
3001 | ||
3002 | if (class == BPF_LD) { | |
3003 | u8 mode = BPF_MODE(code); | |
3004 | ||
3005 | /* LD_IMM64 */ | |
3006 | if (mode == BPF_IMM) | |
3007 | return true; | |
3008 | ||
3009 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
3010 | if (t != SRC_OP) | |
3011 | return false; | |
3012 | ||
3013 | /* Implicit ctx ptr. */ | |
3014 | if (regno == BPF_REG_6) | |
3015 | return true; | |
3016 | ||
3017 | /* Explicit source could be any width. */ | |
3018 | return true; | |
3019 | } | |
3020 | ||
3021 | if (class == BPF_ST) | |
3022 | /* The only source register for BPF_ST is a ptr. */ | |
3023 | return true; | |
3024 | ||
3025 | /* Conservatively return true at default. */ | |
3026 | return true; | |
3027 | } | |
3028 | ||
83a28819 IL |
3029 | /* Return the regno defined by the insn, or -1. */ |
3030 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 3031 | { |
83a28819 IL |
3032 | switch (BPF_CLASS(insn->code)) { |
3033 | case BPF_JMP: | |
3034 | case BPF_JMP32: | |
3035 | case BPF_ST: | |
3036 | return -1; | |
3037 | case BPF_STX: | |
3038 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
3039 | (insn->imm & BPF_FETCH)) { | |
3040 | if (insn->imm == BPF_CMPXCHG) | |
3041 | return BPF_REG_0; | |
3042 | else | |
3043 | return insn->src_reg; | |
3044 | } else { | |
3045 | return -1; | |
3046 | } | |
3047 | default: | |
3048 | return insn->dst_reg; | |
3049 | } | |
b325fbca JW |
3050 | } |
3051 | ||
3052 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
3053 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
3054 | { | |
83a28819 IL |
3055 | int dst_reg = insn_def_regno(insn); |
3056 | ||
3057 | if (dst_reg == -1) | |
b325fbca JW |
3058 | return false; |
3059 | ||
83a28819 | 3060 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
3061 | } |
3062 | ||
5327ed3d JW |
3063 | static void mark_insn_zext(struct bpf_verifier_env *env, |
3064 | struct bpf_reg_state *reg) | |
3065 | { | |
3066 | s32 def_idx = reg->subreg_def; | |
3067 | ||
3068 | if (def_idx == DEF_NOT_SUBREG) | |
3069 | return; | |
3070 | ||
3071 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
3072 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
3073 | reg->subreg_def = DEF_NOT_SUBREG; | |
3074 | } | |
3075 | ||
dc503a8a | 3076 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
3077 | enum reg_arg_type t) |
3078 | { | |
f4d7e40a AS |
3079 | struct bpf_verifier_state *vstate = env->cur_state; |
3080 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 3081 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 3082 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 3083 | bool rw64; |
dc503a8a | 3084 | |
17a52670 | 3085 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 3086 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
3087 | return -EINVAL; |
3088 | } | |
3089 | ||
0f55f9ed CL |
3090 | mark_reg_scratched(env, regno); |
3091 | ||
c342dc10 | 3092 | reg = ®s[regno]; |
5327ed3d | 3093 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
3094 | if (t == SRC_OP) { |
3095 | /* check whether register used as source operand can be read */ | |
c342dc10 | 3096 | if (reg->type == NOT_INIT) { |
61bd5218 | 3097 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
3098 | return -EACCES; |
3099 | } | |
679c782d | 3100 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
3101 | if (regno == BPF_REG_FP) |
3102 | return 0; | |
3103 | ||
5327ed3d JW |
3104 | if (rw64) |
3105 | mark_insn_zext(env, reg); | |
3106 | ||
3107 | return mark_reg_read(env, reg, reg->parent, | |
3108 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
3109 | } else { |
3110 | /* check whether register used as dest operand can be written to */ | |
3111 | if (regno == BPF_REG_FP) { | |
61bd5218 | 3112 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
3113 | return -EACCES; |
3114 | } | |
c342dc10 | 3115 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 3116 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 3117 | if (t == DST_OP) |
61bd5218 | 3118 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
3119 | } |
3120 | return 0; | |
3121 | } | |
3122 | ||
bffdeaa8 AN |
3123 | static void mark_jmp_point(struct bpf_verifier_env *env, int idx) |
3124 | { | |
3125 | env->insn_aux_data[idx].jmp_point = true; | |
3126 | } | |
3127 | ||
3128 | static bool is_jmp_point(struct bpf_verifier_env *env, int insn_idx) | |
3129 | { | |
3130 | return env->insn_aux_data[insn_idx].jmp_point; | |
3131 | } | |
3132 | ||
b5dc0163 AS |
3133 | /* for any branch, call, exit record the history of jmps in the given state */ |
3134 | static int push_jmp_history(struct bpf_verifier_env *env, | |
3135 | struct bpf_verifier_state *cur) | |
3136 | { | |
3137 | u32 cnt = cur->jmp_history_cnt; | |
3138 | struct bpf_idx_pair *p; | |
ceb35b66 | 3139 | size_t alloc_size; |
b5dc0163 | 3140 | |
bffdeaa8 AN |
3141 | if (!is_jmp_point(env, env->insn_idx)) |
3142 | return 0; | |
3143 | ||
b5dc0163 | 3144 | cnt++; |
ceb35b66 KC |
3145 | alloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p))); |
3146 | p = krealloc(cur->jmp_history, alloc_size, GFP_USER); | |
b5dc0163 AS |
3147 | if (!p) |
3148 | return -ENOMEM; | |
3149 | p[cnt - 1].idx = env->insn_idx; | |
3150 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
3151 | cur->jmp_history = p; | |
3152 | cur->jmp_history_cnt = cnt; | |
3153 | return 0; | |
3154 | } | |
3155 | ||
3156 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
3157 | * history then previous instruction came from straight line execution. | |
3158 | */ | |
3159 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
3160 | u32 *history) | |
3161 | { | |
3162 | u32 cnt = *history; | |
3163 | ||
3164 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
3165 | i = st->jmp_history[cnt - 1].prev_idx; | |
3166 | (*history)--; | |
3167 | } else { | |
3168 | i--; | |
3169 | } | |
3170 | return i; | |
3171 | } | |
3172 | ||
e6ac2450 MKL |
3173 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
3174 | { | |
3175 | const struct btf_type *func; | |
2357672c | 3176 | struct btf *desc_btf; |
e6ac2450 MKL |
3177 | |
3178 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
3179 | return NULL; | |
3180 | ||
43bf0878 | 3181 | desc_btf = find_kfunc_desc_btf(data, insn->off); |
2357672c KKD |
3182 | if (IS_ERR(desc_btf)) |
3183 | return "<error>"; | |
3184 | ||
3185 | func = btf_type_by_id(desc_btf, insn->imm); | |
3186 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
3187 | } |
3188 | ||
b5dc0163 AS |
3189 | /* For given verifier state backtrack_insn() is called from the last insn to |
3190 | * the first insn. Its purpose is to compute a bitmask of registers and | |
3191 | * stack slots that needs precision in the parent verifier state. | |
3192 | */ | |
3193 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
3194 | u32 *reg_mask, u64 *stack_mask) | |
3195 | { | |
3196 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 3197 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
3198 | .cb_print = verbose, |
3199 | .private_data = env, | |
3200 | }; | |
3201 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
3202 | u8 class = BPF_CLASS(insn->code); | |
3203 | u8 opcode = BPF_OP(insn->code); | |
3204 | u8 mode = BPF_MODE(insn->code); | |
3205 | u32 dreg = 1u << insn->dst_reg; | |
3206 | u32 sreg = 1u << insn->src_reg; | |
3207 | u32 spi; | |
3208 | ||
3209 | if (insn->code == 0) | |
3210 | return 0; | |
496f3324 | 3211 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
3212 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
3213 | verbose(env, "%d: ", idx); | |
3214 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
3215 | } | |
3216 | ||
3217 | if (class == BPF_ALU || class == BPF_ALU64) { | |
3218 | if (!(*reg_mask & dreg)) | |
3219 | return 0; | |
3220 | if (opcode == BPF_MOV) { | |
3221 | if (BPF_SRC(insn->code) == BPF_X) { | |
3222 | /* dreg = sreg | |
3223 | * dreg needs precision after this insn | |
3224 | * sreg needs precision before this insn | |
3225 | */ | |
3226 | *reg_mask &= ~dreg; | |
3227 | *reg_mask |= sreg; | |
3228 | } else { | |
3229 | /* dreg = K | |
3230 | * dreg needs precision after this insn. | |
3231 | * Corresponding register is already marked | |
3232 | * as precise=true in this verifier state. | |
3233 | * No further markings in parent are necessary | |
3234 | */ | |
3235 | *reg_mask &= ~dreg; | |
3236 | } | |
3237 | } else { | |
3238 | if (BPF_SRC(insn->code) == BPF_X) { | |
3239 | /* dreg += sreg | |
3240 | * both dreg and sreg need precision | |
3241 | * before this insn | |
3242 | */ | |
3243 | *reg_mask |= sreg; | |
3244 | } /* else dreg += K | |
3245 | * dreg still needs precision before this insn | |
3246 | */ | |
3247 | } | |
3248 | } else if (class == BPF_LDX) { | |
3249 | if (!(*reg_mask & dreg)) | |
3250 | return 0; | |
3251 | *reg_mask &= ~dreg; | |
3252 | ||
3253 | /* scalars can only be spilled into stack w/o losing precision. | |
3254 | * Load from any other memory can be zero extended. | |
3255 | * The desire to keep that precision is already indicated | |
3256 | * by 'precise' mark in corresponding register of this state. | |
3257 | * No further tracking necessary. | |
3258 | */ | |
3259 | if (insn->src_reg != BPF_REG_FP) | |
3260 | return 0; | |
b5dc0163 AS |
3261 | |
3262 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
3263 | * that [fp - off] slot contains scalar that needs to be | |
3264 | * tracked with precision | |
3265 | */ | |
3266 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
3267 | if (spi >= 64) { | |
3268 | verbose(env, "BUG spi %d\n", spi); | |
3269 | WARN_ONCE(1, "verifier backtracking bug"); | |
3270 | return -EFAULT; | |
3271 | } | |
3272 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 3273 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 3274 | if (*reg_mask & dreg) |
b3b50f05 | 3275 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
3276 | * to access memory. It means backtracking |
3277 | * encountered a case of pointer subtraction. | |
3278 | */ | |
3279 | return -ENOTSUPP; | |
3280 | /* scalars can only be spilled into stack */ | |
3281 | if (insn->dst_reg != BPF_REG_FP) | |
3282 | return 0; | |
b5dc0163 AS |
3283 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
3284 | if (spi >= 64) { | |
3285 | verbose(env, "BUG spi %d\n", spi); | |
3286 | WARN_ONCE(1, "verifier backtracking bug"); | |
3287 | return -EFAULT; | |
3288 | } | |
3289 | if (!(*stack_mask & (1ull << spi))) | |
3290 | return 0; | |
3291 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
3292 | if (class == BPF_STX) |
3293 | *reg_mask |= sreg; | |
b5dc0163 AS |
3294 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
3295 | if (opcode == BPF_CALL) { | |
3296 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
3297 | return -ENOTSUPP; | |
be2ef816 AN |
3298 | /* BPF helpers that invoke callback subprogs are |
3299 | * equivalent to BPF_PSEUDO_CALL above | |
3300 | */ | |
3301 | if (insn->src_reg == 0 && is_callback_calling_function(insn->imm)) | |
3302 | return -ENOTSUPP; | |
d3178e8a HS |
3303 | /* kfunc with imm==0 is invalid and fixup_kfunc_call will |
3304 | * catch this error later. Make backtracking conservative | |
3305 | * with ENOTSUPP. | |
3306 | */ | |
3307 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && insn->imm == 0) | |
3308 | return -ENOTSUPP; | |
b5dc0163 AS |
3309 | /* regular helper call sets R0 */ |
3310 | *reg_mask &= ~1; | |
3311 | if (*reg_mask & 0x3f) { | |
3312 | /* if backtracing was looking for registers R1-R5 | |
3313 | * they should have been found already. | |
3314 | */ | |
3315 | verbose(env, "BUG regs %x\n", *reg_mask); | |
3316 | WARN_ONCE(1, "verifier backtracking bug"); | |
3317 | return -EFAULT; | |
3318 | } | |
3319 | } else if (opcode == BPF_EXIT) { | |
3320 | return -ENOTSUPP; | |
71b547f5 DB |
3321 | } else if (BPF_SRC(insn->code) == BPF_X) { |
3322 | if (!(*reg_mask & (dreg | sreg))) | |
3323 | return 0; | |
3324 | /* dreg <cond> sreg | |
3325 | * Both dreg and sreg need precision before | |
3326 | * this insn. If only sreg was marked precise | |
3327 | * before it would be equally necessary to | |
3328 | * propagate it to dreg. | |
3329 | */ | |
3330 | *reg_mask |= (sreg | dreg); | |
3331 | /* else dreg <cond> K | |
3332 | * Only dreg still needs precision before | |
3333 | * this insn, so for the K-based conditional | |
3334 | * there is nothing new to be marked. | |
3335 | */ | |
b5dc0163 AS |
3336 | } |
3337 | } else if (class == BPF_LD) { | |
3338 | if (!(*reg_mask & dreg)) | |
3339 | return 0; | |
3340 | *reg_mask &= ~dreg; | |
3341 | /* It's ld_imm64 or ld_abs or ld_ind. | |
3342 | * For ld_imm64 no further tracking of precision | |
3343 | * into parent is necessary | |
3344 | */ | |
3345 | if (mode == BPF_IND || mode == BPF_ABS) | |
3346 | /* to be analyzed */ | |
3347 | return -ENOTSUPP; | |
b5dc0163 AS |
3348 | } |
3349 | return 0; | |
3350 | } | |
3351 | ||
3352 | /* the scalar precision tracking algorithm: | |
3353 | * . at the start all registers have precise=false. | |
3354 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
3355 | * . once precise value of the scalar register is used in: | |
3356 | * . ptr + scalar alu | |
3357 | * . if (scalar cond K|scalar) | |
3358 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
3359 | * backtrack through the verifier states and mark all registers and | |
3360 | * stack slots with spilled constants that these scalar regisers | |
3361 | * should be precise. | |
3362 | * . during state pruning two registers (or spilled stack slots) | |
3363 | * are equivalent if both are not precise. | |
3364 | * | |
3365 | * Note the verifier cannot simply walk register parentage chain, | |
3366 | * since many different registers and stack slots could have been | |
3367 | * used to compute single precise scalar. | |
3368 | * | |
3369 | * The approach of starting with precise=true for all registers and then | |
3370 | * backtrack to mark a register as not precise when the verifier detects | |
3371 | * that program doesn't care about specific value (e.g., when helper | |
3372 | * takes register as ARG_ANYTHING parameter) is not safe. | |
3373 | * | |
3374 | * It's ok to walk single parentage chain of the verifier states. | |
3375 | * It's possible that this backtracking will go all the way till 1st insn. | |
3376 | * All other branches will be explored for needing precision later. | |
3377 | * | |
3378 | * The backtracking needs to deal with cases like: | |
3379 | * 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) | |
3380 | * r9 -= r8 | |
3381 | * r5 = r9 | |
3382 | * if r5 > 0x79f goto pc+7 | |
3383 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
3384 | * r5 += 1 | |
3385 | * ... | |
3386 | * call bpf_perf_event_output#25 | |
3387 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
3388 | * | |
3389 | * and this case: | |
3390 | * r6 = 1 | |
3391 | * call foo // uses callee's r6 inside to compute r0 | |
3392 | * r0 += r6 | |
3393 | * if r0 == 0 goto | |
3394 | * | |
3395 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
3396 | * | |
3397 | * Also if parent's curframe > frame where backtracking started, | |
3398 | * the verifier need to mark registers in both frames, otherwise callees | |
3399 | * may incorrectly prune callers. This is similar to | |
3400 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
3401 | * | |
3402 | * For now backtracking falls back into conservative marking. | |
3403 | */ | |
3404 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
3405 | struct bpf_verifier_state *st) | |
3406 | { | |
3407 | struct bpf_func_state *func; | |
3408 | struct bpf_reg_state *reg; | |
3409 | int i, j; | |
3410 | ||
3411 | /* big hammer: mark all scalars precise in this path. | |
3412 | * pop_stack may still get !precise scalars. | |
f63181b6 AN |
3413 | * We also skip current state and go straight to first parent state, |
3414 | * because precision markings in current non-checkpointed state are | |
3415 | * not needed. See why in the comment in __mark_chain_precision below. | |
b5dc0163 | 3416 | */ |
f63181b6 | 3417 | for (st = st->parent; st; st = st->parent) { |
b5dc0163 AS |
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 = true; | |
3425 | } | |
3426 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 3427 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
3428 | continue; |
3429 | reg = &func->stack[j].spilled_ptr; | |
3430 | if (reg->type != SCALAR_VALUE) | |
3431 | continue; | |
3432 | reg->precise = true; | |
3433 | } | |
3434 | } | |
f63181b6 | 3435 | } |
b5dc0163 AS |
3436 | } |
3437 | ||
7a830b53 AN |
3438 | static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
3439 | { | |
3440 | struct bpf_func_state *func; | |
3441 | struct bpf_reg_state *reg; | |
3442 | int i, j; | |
3443 | ||
3444 | for (i = 0; i <= st->curframe; i++) { | |
3445 | func = st->frame[i]; | |
3446 | for (j = 0; j < BPF_REG_FP; j++) { | |
3447 | reg = &func->regs[j]; | |
3448 | if (reg->type != SCALAR_VALUE) | |
3449 | continue; | |
3450 | reg->precise = false; | |
3451 | } | |
3452 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
3453 | if (!is_spilled_reg(&func->stack[j])) | |
3454 | continue; | |
3455 | reg = &func->stack[j].spilled_ptr; | |
3456 | if (reg->type != SCALAR_VALUE) | |
3457 | continue; | |
3458 | reg->precise = false; | |
3459 | } | |
3460 | } | |
3461 | } | |
3462 | ||
f63181b6 AN |
3463 | /* |
3464 | * __mark_chain_precision() backtracks BPF program instruction sequence and | |
3465 | * chain of verifier states making sure that register *regno* (if regno >= 0) | |
3466 | * and/or stack slot *spi* (if spi >= 0) are marked as precisely tracked | |
3467 | * SCALARS, as well as any other registers and slots that contribute to | |
3468 | * a tracked state of given registers/stack slots, depending on specific BPF | |
3469 | * assembly instructions (see backtrack_insns() for exact instruction handling | |
3470 | * logic). This backtracking relies on recorded jmp_history and is able to | |
3471 | * traverse entire chain of parent states. This process ends only when all the | |
3472 | * necessary registers/slots and their transitive dependencies are marked as | |
3473 | * precise. | |
3474 | * | |
3475 | * One important and subtle aspect is that precise marks *do not matter* in | |
3476 | * the currently verified state (current state). It is important to understand | |
3477 | * why this is the case. | |
3478 | * | |
3479 | * First, note that current state is the state that is not yet "checkpointed", | |
3480 | * i.e., it is not yet put into env->explored_states, and it has no children | |
3481 | * states as well. It's ephemeral, and can end up either a) being discarded if | |
3482 | * compatible explored state is found at some point or BPF_EXIT instruction is | |
3483 | * reached or b) checkpointed and put into env->explored_states, branching out | |
3484 | * into one or more children states. | |
3485 | * | |
3486 | * In the former case, precise markings in current state are completely | |
3487 | * ignored by state comparison code (see regsafe() for details). Only | |
3488 | * checkpointed ("old") state precise markings are important, and if old | |
3489 | * state's register/slot is precise, regsafe() assumes current state's | |
3490 | * register/slot as precise and checks value ranges exactly and precisely. If | |
3491 | * states turn out to be compatible, current state's necessary precise | |
3492 | * markings and any required parent states' precise markings are enforced | |
3493 | * after the fact with propagate_precision() logic, after the fact. But it's | |
3494 | * important to realize that in this case, even after marking current state | |
3495 | * registers/slots as precise, we immediately discard current state. So what | |
3496 | * actually matters is any of the precise markings propagated into current | |
3497 | * state's parent states, which are always checkpointed (due to b) case above). | |
3498 | * As such, for scenario a) it doesn't matter if current state has precise | |
3499 | * markings set or not. | |
3500 | * | |
3501 | * Now, for the scenario b), checkpointing and forking into child(ren) | |
3502 | * state(s). Note that before current state gets to checkpointing step, any | |
3503 | * processed instruction always assumes precise SCALAR register/slot | |
3504 | * knowledge: if precise value or range is useful to prune jump branch, BPF | |
3505 | * verifier takes this opportunity enthusiastically. Similarly, when | |
3506 | * register's value is used to calculate offset or memory address, exact | |
3507 | * knowledge of SCALAR range is assumed, checked, and enforced. So, similar to | |
3508 | * what we mentioned above about state comparison ignoring precise markings | |
3509 | * during state comparison, BPF verifier ignores and also assumes precise | |
3510 | * markings *at will* during instruction verification process. But as verifier | |
3511 | * assumes precision, it also propagates any precision dependencies across | |
3512 | * parent states, which are not yet finalized, so can be further restricted | |
3513 | * based on new knowledge gained from restrictions enforced by their children | |
3514 | * states. This is so that once those parent states are finalized, i.e., when | |
3515 | * they have no more active children state, state comparison logic in | |
3516 | * is_state_visited() would enforce strict and precise SCALAR ranges, if | |
3517 | * required for correctness. | |
3518 | * | |
3519 | * To build a bit more intuition, note also that once a state is checkpointed, | |
3520 | * the path we took to get to that state is not important. This is crucial | |
3521 | * property for state pruning. When state is checkpointed and finalized at | |
3522 | * some instruction index, it can be correctly and safely used to "short | |
3523 | * circuit" any *compatible* state that reaches exactly the same instruction | |
3524 | * index. I.e., if we jumped to that instruction from a completely different | |
3525 | * code path than original finalized state was derived from, it doesn't | |
3526 | * matter, current state can be discarded because from that instruction | |
3527 | * forward having a compatible state will ensure we will safely reach the | |
3528 | * exit. States describe preconditions for further exploration, but completely | |
3529 | * forget the history of how we got here. | |
3530 | * | |
3531 | * This also means that even if we needed precise SCALAR range to get to | |
3532 | * finalized state, but from that point forward *that same* SCALAR register is | |
3533 | * never used in a precise context (i.e., it's precise value is not needed for | |
3534 | * correctness), it's correct and safe to mark such register as "imprecise" | |
3535 | * (i.e., precise marking set to false). This is what we rely on when we do | |
3536 | * not set precise marking in current state. If no child state requires | |
3537 | * precision for any given SCALAR register, it's safe to dictate that it can | |
3538 | * be imprecise. If any child state does require this register to be precise, | |
3539 | * we'll mark it precise later retroactively during precise markings | |
3540 | * propagation from child state to parent states. | |
7a830b53 AN |
3541 | * |
3542 | * Skipping precise marking setting in current state is a mild version of | |
3543 | * relying on the above observation. But we can utilize this property even | |
3544 | * more aggressively by proactively forgetting any precise marking in the | |
3545 | * current state (which we inherited from the parent state), right before we | |
3546 | * checkpoint it and branch off into new child state. This is done by | |
3547 | * mark_all_scalars_imprecise() to hopefully get more permissive and generic | |
3548 | * finalized states which help in short circuiting more future states. | |
f63181b6 | 3549 | */ |
529409ea | 3550 | static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int regno, |
a3ce685d | 3551 | int spi) |
b5dc0163 AS |
3552 | { |
3553 | struct bpf_verifier_state *st = env->cur_state; | |
3554 | int first_idx = st->first_insn_idx; | |
3555 | int last_idx = env->insn_idx; | |
3556 | struct bpf_func_state *func; | |
3557 | struct bpf_reg_state *reg; | |
a3ce685d AS |
3558 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
3559 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 3560 | bool skip_first = true; |
a3ce685d | 3561 | bool new_marks = false; |
b5dc0163 AS |
3562 | int i, err; |
3563 | ||
2c78ee89 | 3564 | if (!env->bpf_capable) |
b5dc0163 AS |
3565 | return 0; |
3566 | ||
f63181b6 AN |
3567 | /* Do sanity checks against current state of register and/or stack |
3568 | * slot, but don't set precise flag in current state, as precision | |
3569 | * tracking in the current state is unnecessary. | |
3570 | */ | |
529409ea | 3571 | func = st->frame[frame]; |
a3ce685d AS |
3572 | if (regno >= 0) { |
3573 | reg = &func->regs[regno]; | |
3574 | if (reg->type != SCALAR_VALUE) { | |
3575 | WARN_ONCE(1, "backtracing misuse"); | |
3576 | return -EFAULT; | |
3577 | } | |
f63181b6 | 3578 | new_marks = true; |
b5dc0163 | 3579 | } |
b5dc0163 | 3580 | |
a3ce685d | 3581 | while (spi >= 0) { |
27113c59 | 3582 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
3583 | stack_mask = 0; |
3584 | break; | |
3585 | } | |
3586 | reg = &func->stack[spi].spilled_ptr; | |
3587 | if (reg->type != SCALAR_VALUE) { | |
3588 | stack_mask = 0; | |
3589 | break; | |
3590 | } | |
f63181b6 | 3591 | new_marks = true; |
a3ce685d AS |
3592 | break; |
3593 | } | |
3594 | ||
3595 | if (!new_marks) | |
3596 | return 0; | |
3597 | if (!reg_mask && !stack_mask) | |
3598 | return 0; | |
be2ef816 | 3599 | |
b5dc0163 AS |
3600 | for (;;) { |
3601 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
3602 | u32 history = st->jmp_history_cnt; |
3603 | ||
496f3324 | 3604 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 | 3605 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
be2ef816 AN |
3606 | |
3607 | if (last_idx < 0) { | |
3608 | /* we are at the entry into subprog, which | |
3609 | * is expected for global funcs, but only if | |
3610 | * requested precise registers are R1-R5 | |
3611 | * (which are global func's input arguments) | |
3612 | */ | |
3613 | if (st->curframe == 0 && | |
3614 | st->frame[0]->subprogno > 0 && | |
3615 | st->frame[0]->callsite == BPF_MAIN_FUNC && | |
3616 | stack_mask == 0 && (reg_mask & ~0x3e) == 0) { | |
3617 | bitmap_from_u64(mask, reg_mask); | |
3618 | for_each_set_bit(i, mask, 32) { | |
3619 | reg = &st->frame[0]->regs[i]; | |
3620 | if (reg->type != SCALAR_VALUE) { | |
3621 | reg_mask &= ~(1u << i); | |
3622 | continue; | |
3623 | } | |
3624 | reg->precise = true; | |
3625 | } | |
3626 | return 0; | |
3627 | } | |
3628 | ||
3629 | verbose(env, "BUG backtracing func entry subprog %d reg_mask %x stack_mask %llx\n", | |
3630 | st->frame[0]->subprogno, reg_mask, stack_mask); | |
3631 | WARN_ONCE(1, "verifier backtracking bug"); | |
3632 | return -EFAULT; | |
3633 | } | |
3634 | ||
b5dc0163 AS |
3635 | for (i = last_idx;;) { |
3636 | if (skip_first) { | |
3637 | err = 0; | |
3638 | skip_first = false; | |
3639 | } else { | |
3640 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
3641 | } | |
3642 | if (err == -ENOTSUPP) { | |
3643 | mark_all_scalars_precise(env, st); | |
3644 | return 0; | |
3645 | } else if (err) { | |
3646 | return err; | |
3647 | } | |
3648 | if (!reg_mask && !stack_mask) | |
3649 | /* Found assignment(s) into tracked register in this state. | |
3650 | * Since this state is already marked, just return. | |
3651 | * Nothing to be tracked further in the parent state. | |
3652 | */ | |
3653 | return 0; | |
3654 | if (i == first_idx) | |
3655 | break; | |
3656 | i = get_prev_insn_idx(st, i, &history); | |
3657 | if (i >= env->prog->len) { | |
3658 | /* This can happen if backtracking reached insn 0 | |
3659 | * and there are still reg_mask or stack_mask | |
3660 | * to backtrack. | |
3661 | * It means the backtracking missed the spot where | |
3662 | * particular register was initialized with a constant. | |
3663 | */ | |
3664 | verbose(env, "BUG backtracking idx %d\n", i); | |
3665 | WARN_ONCE(1, "verifier backtracking bug"); | |
3666 | return -EFAULT; | |
3667 | } | |
3668 | } | |
3669 | st = st->parent; | |
3670 | if (!st) | |
3671 | break; | |
3672 | ||
a3ce685d | 3673 | new_marks = false; |
529409ea | 3674 | func = st->frame[frame]; |
b5dc0163 AS |
3675 | bitmap_from_u64(mask, reg_mask); |
3676 | for_each_set_bit(i, mask, 32) { | |
3677 | reg = &func->regs[i]; | |
a3ce685d AS |
3678 | if (reg->type != SCALAR_VALUE) { |
3679 | reg_mask &= ~(1u << i); | |
b5dc0163 | 3680 | continue; |
a3ce685d | 3681 | } |
b5dc0163 AS |
3682 | if (!reg->precise) |
3683 | new_marks = true; | |
3684 | reg->precise = true; | |
3685 | } | |
3686 | ||
3687 | bitmap_from_u64(mask, stack_mask); | |
3688 | for_each_set_bit(i, mask, 64) { | |
3689 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
3690 | /* the sequence of instructions: |
3691 | * 2: (bf) r3 = r10 | |
3692 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
3693 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
3694 | * doesn't contain jmps. It's backtracked | |
3695 | * as a single block. | |
3696 | * During backtracking insn 3 is not recognized as | |
3697 | * stack access, so at the end of backtracking | |
3698 | * stack slot fp-8 is still marked in stack_mask. | |
3699 | * However the parent state may not have accessed | |
3700 | * fp-8 and it's "unallocated" stack space. | |
3701 | * In such case fallback to conservative. | |
b5dc0163 | 3702 | */ |
2339cd6c AS |
3703 | mark_all_scalars_precise(env, st); |
3704 | return 0; | |
b5dc0163 AS |
3705 | } |
3706 | ||
27113c59 | 3707 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 3708 | stack_mask &= ~(1ull << i); |
b5dc0163 | 3709 | continue; |
a3ce685d | 3710 | } |
b5dc0163 | 3711 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
3712 | if (reg->type != SCALAR_VALUE) { |
3713 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 3714 | continue; |
a3ce685d | 3715 | } |
b5dc0163 AS |
3716 | if (!reg->precise) |
3717 | new_marks = true; | |
3718 | reg->precise = true; | |
3719 | } | |
496f3324 | 3720 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 3721 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
3722 | new_marks ? "didn't have" : "already had", |
3723 | reg_mask, stack_mask); | |
2e576648 | 3724 | print_verifier_state(env, func, true); |
b5dc0163 AS |
3725 | } |
3726 | ||
a3ce685d AS |
3727 | if (!reg_mask && !stack_mask) |
3728 | break; | |
b5dc0163 AS |
3729 | if (!new_marks) |
3730 | break; | |
3731 | ||
3732 | last_idx = st->last_insn_idx; | |
3733 | first_idx = st->first_insn_idx; | |
3734 | } | |
3735 | return 0; | |
3736 | } | |
3737 | ||
eb1f7f71 | 3738 | int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
a3ce685d | 3739 | { |
529409ea | 3740 | return __mark_chain_precision(env, env->cur_state->curframe, regno, -1); |
a3ce685d AS |
3741 | } |
3742 | ||
529409ea | 3743 | static int mark_chain_precision_frame(struct bpf_verifier_env *env, int frame, int regno) |
a3ce685d | 3744 | { |
529409ea | 3745 | return __mark_chain_precision(env, frame, regno, -1); |
a3ce685d AS |
3746 | } |
3747 | ||
529409ea | 3748 | static int mark_chain_precision_stack_frame(struct bpf_verifier_env *env, int frame, int spi) |
a3ce685d | 3749 | { |
529409ea | 3750 | return __mark_chain_precision(env, frame, -1, spi); |
a3ce685d | 3751 | } |
b5dc0163 | 3752 | |
1be7f75d AS |
3753 | static bool is_spillable_regtype(enum bpf_reg_type type) |
3754 | { | |
c25b2ae1 | 3755 | switch (base_type(type)) { |
1be7f75d | 3756 | case PTR_TO_MAP_VALUE: |
1be7f75d AS |
3757 | case PTR_TO_STACK: |
3758 | case PTR_TO_CTX: | |
969bf05e | 3759 | case PTR_TO_PACKET: |
de8f3a83 | 3760 | case PTR_TO_PACKET_META: |
969bf05e | 3761 | case PTR_TO_PACKET_END: |
d58e468b | 3762 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 3763 | case CONST_PTR_TO_MAP: |
c64b7983 | 3764 | case PTR_TO_SOCKET: |
46f8bc92 | 3765 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 3766 | case PTR_TO_TCP_SOCK: |
fada7fdc | 3767 | case PTR_TO_XDP_SOCK: |
65726b5b | 3768 | case PTR_TO_BTF_ID: |
20b2aff4 | 3769 | case PTR_TO_BUF: |
744ea4e3 | 3770 | case PTR_TO_MEM: |
69c087ba YS |
3771 | case PTR_TO_FUNC: |
3772 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
3773 | return true; |
3774 | default: | |
3775 | return false; | |
3776 | } | |
3777 | } | |
3778 | ||
cc2b14d5 AS |
3779 | /* Does this register contain a constant zero? */ |
3780 | static bool register_is_null(struct bpf_reg_state *reg) | |
3781 | { | |
3782 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
3783 | } | |
3784 | ||
f7cf25b2 AS |
3785 | static bool register_is_const(struct bpf_reg_state *reg) |
3786 | { | |
3787 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
3788 | } | |
3789 | ||
5689d49b YS |
3790 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
3791 | { | |
3792 | return tnum_is_unknown(reg->var_off) && | |
3793 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
3794 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
3795 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
3796 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
3797 | } | |
3798 | ||
3799 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
3800 | { | |
3801 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
3802 | } | |
3803 | ||
6e7e63cb JH |
3804 | static bool __is_pointer_value(bool allow_ptr_leaks, |
3805 | const struct bpf_reg_state *reg) | |
3806 | { | |
3807 | if (allow_ptr_leaks) | |
3808 | return false; | |
3809 | ||
3810 | return reg->type != SCALAR_VALUE; | |
3811 | } | |
3812 | ||
71f656a5 EZ |
3813 | /* Copy src state preserving dst->parent and dst->live fields */ |
3814 | static void copy_register_state(struct bpf_reg_state *dst, const struct bpf_reg_state *src) | |
3815 | { | |
3816 | struct bpf_reg_state *parent = dst->parent; | |
3817 | enum bpf_reg_liveness live = dst->live; | |
3818 | ||
3819 | *dst = *src; | |
3820 | dst->parent = parent; | |
3821 | dst->live = live; | |
3822 | } | |
3823 | ||
f7cf25b2 | 3824 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
3825 | int spi, struct bpf_reg_state *reg, |
3826 | int size) | |
f7cf25b2 AS |
3827 | { |
3828 | int i; | |
3829 | ||
71f656a5 | 3830 | copy_register_state(&state->stack[spi].spilled_ptr, reg); |
354e8f19 MKL |
3831 | if (size == BPF_REG_SIZE) |
3832 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 3833 | |
354e8f19 MKL |
3834 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
3835 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 3836 | |
354e8f19 MKL |
3837 | /* size < 8 bytes spill */ |
3838 | for (; i; i--) | |
3839 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
3840 | } |
3841 | ||
ecdf985d EZ |
3842 | static bool is_bpf_st_mem(struct bpf_insn *insn) |
3843 | { | |
3844 | return BPF_CLASS(insn->code) == BPF_ST && BPF_MODE(insn->code) == BPF_MEM; | |
3845 | } | |
3846 | ||
01f810ac | 3847 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
3848 | * stack boundary and alignment are checked in check_mem_access() |
3849 | */ | |
01f810ac AM |
3850 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
3851 | /* stack frame we're writing to */ | |
3852 | struct bpf_func_state *state, | |
3853 | int off, int size, int value_regno, | |
3854 | int insn_idx) | |
17a52670 | 3855 | { |
f4d7e40a | 3856 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 3857 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
ecdf985d | 3858 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; |
f7cf25b2 | 3859 | struct bpf_reg_state *reg = NULL; |
ecdf985d | 3860 | u32 dst_reg = insn->dst_reg; |
638f5b90 | 3861 | |
c69431aa | 3862 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
3863 | if (err) |
3864 | return err; | |
9c399760 AS |
3865 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
3866 | * so it's aligned access and [off, off + size) are within stack limits | |
3867 | */ | |
638f5b90 AS |
3868 | if (!env->allow_ptr_leaks && |
3869 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
3870 | size != BPF_REG_SIZE) { | |
3871 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
3872 | return -EACCES; | |
3873 | } | |
17a52670 | 3874 | |
f4d7e40a | 3875 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
3876 | if (value_regno >= 0) |
3877 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
3878 | if (!env->bypass_spec_v4) { |
3879 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
3880 | ||
3881 | for (i = 0; i < size; i++) { | |
e4f4db47 LG |
3882 | u8 type = state->stack[spi].slot_type[i]; |
3883 | ||
3884 | if (type != STACK_MISC && type != STACK_ZERO) { | |
2039f26f DB |
3885 | sanitize = true; |
3886 | break; | |
3887 | } | |
3888 | } | |
3889 | ||
3890 | if (sanitize) | |
3891 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
3892 | } | |
17a52670 | 3893 | |
ef8fc7a0 KKD |
3894 | err = destroy_if_dynptr_stack_slot(env, state, spi); |
3895 | if (err) | |
3896 | return err; | |
3897 | ||
0f55f9ed | 3898 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 3899 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 3900 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
3901 | if (dst_reg != BPF_REG_FP) { |
3902 | /* The backtracking logic can only recognize explicit | |
3903 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 3904 | * scalar via different register has to be conservative. |
b5dc0163 AS |
3905 | * Backtrack from here and mark all registers as precise |
3906 | * that contributed into 'reg' being a constant. | |
3907 | */ | |
3908 | err = mark_chain_precision(env, value_regno); | |
3909 | if (err) | |
3910 | return err; | |
3911 | } | |
354e8f19 | 3912 | save_register_state(state, spi, reg, size); |
ecdf985d EZ |
3913 | } else if (!reg && !(off % BPF_REG_SIZE) && is_bpf_st_mem(insn) && |
3914 | insn->imm != 0 && env->bpf_capable) { | |
3915 | struct bpf_reg_state fake_reg = {}; | |
3916 | ||
3917 | __mark_reg_known(&fake_reg, (u32)insn->imm); | |
3918 | fake_reg.type = SCALAR_VALUE; | |
3919 | save_register_state(state, spi, &fake_reg, size); | |
f7cf25b2 | 3920 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 3921 | /* register containing pointer is being spilled into stack */ |
9c399760 | 3922 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 3923 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 3924 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
3925 | return -EACCES; |
3926 | } | |
f7cf25b2 | 3927 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
3928 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
3929 | return -EINVAL; | |
3930 | } | |
354e8f19 | 3931 | save_register_state(state, spi, reg, size); |
9c399760 | 3932 | } else { |
cc2b14d5 AS |
3933 | u8 type = STACK_MISC; |
3934 | ||
679c782d EC |
3935 | /* regular write of data into stack destroys any spilled ptr */ |
3936 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
06accc87 AN |
3937 | /* Mark slots as STACK_MISC if they belonged to spilled ptr/dynptr/iter. */ |
3938 | if (is_stack_slot_special(&state->stack[spi])) | |
0bae2d4d | 3939 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 3940 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 3941 | |
cc2b14d5 AS |
3942 | /* only mark the slot as written if all 8 bytes were written |
3943 | * otherwise read propagation may incorrectly stop too soon | |
3944 | * when stack slots are partially written. | |
3945 | * This heuristic means that read propagation will be | |
3946 | * conservative, since it will add reg_live_read marks | |
3947 | * to stack slots all the way to first state when programs | |
3948 | * writes+reads less than 8 bytes | |
3949 | */ | |
3950 | if (size == BPF_REG_SIZE) | |
3951 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
3952 | ||
3953 | /* when we zero initialize stack slots mark them as such */ | |
ecdf985d EZ |
3954 | if ((reg && register_is_null(reg)) || |
3955 | (!reg && is_bpf_st_mem(insn) && insn->imm == 0)) { | |
b5dc0163 AS |
3956 | /* backtracking doesn't work for STACK_ZERO yet. */ |
3957 | err = mark_chain_precision(env, value_regno); | |
3958 | if (err) | |
3959 | return err; | |
cc2b14d5 | 3960 | type = STACK_ZERO; |
b5dc0163 | 3961 | } |
cc2b14d5 | 3962 | |
0bae2d4d | 3963 | /* Mark slots affected by this stack write. */ |
9c399760 | 3964 | for (i = 0; i < size; i++) |
638f5b90 | 3965 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 3966 | type; |
17a52670 AS |
3967 | } |
3968 | return 0; | |
3969 | } | |
3970 | ||
01f810ac AM |
3971 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
3972 | * known to contain a variable offset. | |
3973 | * This function checks whether the write is permitted and conservatively | |
3974 | * tracks the effects of the write, considering that each stack slot in the | |
3975 | * dynamic range is potentially written to. | |
3976 | * | |
3977 | * 'off' includes 'regno->off'. | |
3978 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
3979 | * the stack. | |
3980 | * | |
3981 | * Spilled pointers in range are not marked as written because we don't know | |
3982 | * what's going to be actually written. This means that read propagation for | |
3983 | * future reads cannot be terminated by this write. | |
3984 | * | |
3985 | * For privileged programs, uninitialized stack slots are considered | |
3986 | * initialized by this write (even though we don't know exactly what offsets | |
3987 | * are going to be written to). The idea is that we don't want the verifier to | |
3988 | * reject future reads that access slots written to through variable offsets. | |
3989 | */ | |
3990 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
3991 | /* func where register points to */ | |
3992 | struct bpf_func_state *state, | |
3993 | int ptr_regno, int off, int size, | |
3994 | int value_regno, int insn_idx) | |
3995 | { | |
3996 | struct bpf_func_state *cur; /* state of the current function */ | |
3997 | int min_off, max_off; | |
3998 | int i, err; | |
3999 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
31ff2135 | 4000 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; |
01f810ac AM |
4001 | bool writing_zero = false; |
4002 | /* set if the fact that we're writing a zero is used to let any | |
4003 | * stack slots remain STACK_ZERO | |
4004 | */ | |
4005 | bool zero_used = false; | |
4006 | ||
4007 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
4008 | ptr_reg = &cur->regs[ptr_regno]; | |
4009 | min_off = ptr_reg->smin_value + off; | |
4010 | max_off = ptr_reg->smax_value + off + size; | |
4011 | if (value_regno >= 0) | |
4012 | value_reg = &cur->regs[value_regno]; | |
31ff2135 EZ |
4013 | if ((value_reg && register_is_null(value_reg)) || |
4014 | (!value_reg && is_bpf_st_mem(insn) && insn->imm == 0)) | |
01f810ac AM |
4015 | writing_zero = true; |
4016 | ||
c69431aa | 4017 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
4018 | if (err) |
4019 | return err; | |
4020 | ||
ef8fc7a0 KKD |
4021 | for (i = min_off; i < max_off; i++) { |
4022 | int spi; | |
4023 | ||
4024 | spi = __get_spi(i); | |
4025 | err = destroy_if_dynptr_stack_slot(env, state, spi); | |
4026 | if (err) | |
4027 | return err; | |
4028 | } | |
01f810ac AM |
4029 | |
4030 | /* Variable offset writes destroy any spilled pointers in range. */ | |
4031 | for (i = min_off; i < max_off; i++) { | |
4032 | u8 new_type, *stype; | |
4033 | int slot, spi; | |
4034 | ||
4035 | slot = -i - 1; | |
4036 | spi = slot / BPF_REG_SIZE; | |
4037 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 4038 | mark_stack_slot_scratched(env, spi); |
01f810ac | 4039 | |
f5e477a8 KKD |
4040 | if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) { |
4041 | /* Reject the write if range we may write to has not | |
4042 | * been initialized beforehand. If we didn't reject | |
4043 | * here, the ptr status would be erased below (even | |
4044 | * though not all slots are actually overwritten), | |
4045 | * possibly opening the door to leaks. | |
4046 | * | |
4047 | * We do however catch STACK_INVALID case below, and | |
4048 | * only allow reading possibly uninitialized memory | |
4049 | * later for CAP_PERFMON, as the write may not happen to | |
4050 | * that slot. | |
01f810ac AM |
4051 | */ |
4052 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
4053 | insn_idx, i); | |
4054 | return -EINVAL; | |
4055 | } | |
4056 | ||
4057 | /* Erase all spilled pointers. */ | |
4058 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
4059 | ||
4060 | /* Update the slot type. */ | |
4061 | new_type = STACK_MISC; | |
4062 | if (writing_zero && *stype == STACK_ZERO) { | |
4063 | new_type = STACK_ZERO; | |
4064 | zero_used = true; | |
4065 | } | |
4066 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
4067 | * pretend that it will be initialized by this write. The slot | |
4068 | * might not actually be written to, and so if we mark it as | |
4069 | * initialized future reads might leak uninitialized memory. | |
4070 | * For privileged programs, we will accept such reads to slots | |
4071 | * that may or may not be written because, if we're reject | |
4072 | * them, the error would be too confusing. | |
4073 | */ | |
4074 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
4075 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
4076 | insn_idx, i); | |
4077 | return -EINVAL; | |
4078 | } | |
4079 | *stype = new_type; | |
4080 | } | |
4081 | if (zero_used) { | |
4082 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
4083 | err = mark_chain_precision(env, value_regno); | |
4084 | if (err) | |
4085 | return err; | |
4086 | } | |
4087 | return 0; | |
4088 | } | |
4089 | ||
4090 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
4091 | * max_off), we set the register's type according to the types of the | |
4092 | * respective stack slots. If all the stack values are known to be zeros, then | |
4093 | * so is the destination reg. Otherwise, the register is considered to be | |
4094 | * SCALAR. This function does not deal with register filling; the caller must | |
4095 | * ensure that all spilled registers in the stack range have been marked as | |
4096 | * read. | |
4097 | */ | |
4098 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
4099 | /* func where src register points to */ | |
4100 | struct bpf_func_state *ptr_state, | |
4101 | int min_off, int max_off, int dst_regno) | |
4102 | { | |
4103 | struct bpf_verifier_state *vstate = env->cur_state; | |
4104 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4105 | int i, slot, spi; | |
4106 | u8 *stype; | |
4107 | int zeros = 0; | |
4108 | ||
4109 | for (i = min_off; i < max_off; i++) { | |
4110 | slot = -i - 1; | |
4111 | spi = slot / BPF_REG_SIZE; | |
4112 | stype = ptr_state->stack[spi].slot_type; | |
4113 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
4114 | break; | |
4115 | zeros++; | |
4116 | } | |
4117 | if (zeros == max_off - min_off) { | |
4118 | /* any access_size read into register is zero extended, | |
4119 | * so the whole register == const_zero | |
4120 | */ | |
4121 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
4122 | /* backtracking doesn't support STACK_ZERO yet, | |
4123 | * so mark it precise here, so that later | |
4124 | * backtracking can stop here. | |
4125 | * Backtracking may not need this if this register | |
4126 | * doesn't participate in pointer adjustment. | |
4127 | * Forward propagation of precise flag is not | |
4128 | * necessary either. This mark is only to stop | |
4129 | * backtracking. Any register that contributed | |
4130 | * to const 0 was marked precise before spill. | |
4131 | */ | |
4132 | state->regs[dst_regno].precise = true; | |
4133 | } else { | |
4134 | /* have read misc data from the stack */ | |
4135 | mark_reg_unknown(env, state->regs, dst_regno); | |
4136 | } | |
4137 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
4138 | } | |
4139 | ||
4140 | /* Read the stack at 'off' and put the results into the register indicated by | |
4141 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
4142 | * spilled reg. | |
4143 | * | |
4144 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
4145 | * register. | |
4146 | * | |
4147 | * The access is assumed to be within the current stack bounds. | |
4148 | */ | |
4149 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
4150 | /* func where src register points to */ | |
4151 | struct bpf_func_state *reg_state, | |
4152 | int off, int size, int dst_regno) | |
17a52670 | 4153 | { |
f4d7e40a AS |
4154 | struct bpf_verifier_state *vstate = env->cur_state; |
4155 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 4156 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 4157 | struct bpf_reg_state *reg; |
354e8f19 | 4158 | u8 *stype, type; |
17a52670 | 4159 | |
f4d7e40a | 4160 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 4161 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 4162 | |
27113c59 | 4163 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
4164 | u8 spill_size = 1; |
4165 | ||
4166 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
4167 | spill_size++; | |
354e8f19 | 4168 | |
f30d4968 | 4169 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
4170 | if (reg->type != SCALAR_VALUE) { |
4171 | verbose_linfo(env, env->insn_idx, "; "); | |
4172 | verbose(env, "invalid size of register fill\n"); | |
4173 | return -EACCES; | |
4174 | } | |
354e8f19 MKL |
4175 | |
4176 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
4177 | if (dst_regno < 0) | |
4178 | return 0; | |
4179 | ||
f30d4968 | 4180 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
4181 | /* The earlier check_reg_arg() has decided the |
4182 | * subreg_def for this insn. Save it first. | |
4183 | */ | |
4184 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
4185 | ||
71f656a5 | 4186 | copy_register_state(&state->regs[dst_regno], reg); |
354e8f19 MKL |
4187 | state->regs[dst_regno].subreg_def = subreg_def; |
4188 | } else { | |
4189 | for (i = 0; i < size; i++) { | |
4190 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
4191 | if (type == STACK_SPILL) | |
4192 | continue; | |
4193 | if (type == STACK_MISC) | |
4194 | continue; | |
6715df8d EZ |
4195 | if (type == STACK_INVALID && env->allow_uninit_stack) |
4196 | continue; | |
354e8f19 MKL |
4197 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
4198 | off, i, size); | |
4199 | return -EACCES; | |
4200 | } | |
01f810ac | 4201 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 4202 | } |
354e8f19 | 4203 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 4204 | return 0; |
17a52670 | 4205 | } |
17a52670 | 4206 | |
01f810ac | 4207 | if (dst_regno >= 0) { |
17a52670 | 4208 | /* restore register state from stack */ |
71f656a5 | 4209 | copy_register_state(&state->regs[dst_regno], reg); |
2f18f62e AS |
4210 | /* mark reg as written since spilled pointer state likely |
4211 | * has its liveness marks cleared by is_state_visited() | |
4212 | * which resets stack/reg liveness for state transitions | |
4213 | */ | |
01f810ac | 4214 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 4215 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 4216 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
4217 | * it is acceptable to use this value as a SCALAR_VALUE |
4218 | * (e.g. for XADD). | |
4219 | * We must not allow unprivileged callers to do that | |
4220 | * with spilled pointers. | |
4221 | */ | |
4222 | verbose(env, "leaking pointer from stack off %d\n", | |
4223 | off); | |
4224 | return -EACCES; | |
dc503a8a | 4225 | } |
f7cf25b2 | 4226 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
4227 | } else { |
4228 | for (i = 0; i < size; i++) { | |
01f810ac AM |
4229 | type = stype[(slot - i) % BPF_REG_SIZE]; |
4230 | if (type == STACK_MISC) | |
cc2b14d5 | 4231 | continue; |
01f810ac | 4232 | if (type == STACK_ZERO) |
cc2b14d5 | 4233 | continue; |
6715df8d EZ |
4234 | if (type == STACK_INVALID && env->allow_uninit_stack) |
4235 | continue; | |
cc2b14d5 AS |
4236 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
4237 | off, i, size); | |
4238 | return -EACCES; | |
4239 | } | |
f7cf25b2 | 4240 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
4241 | if (dst_regno >= 0) |
4242 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 4243 | } |
f7cf25b2 | 4244 | return 0; |
17a52670 AS |
4245 | } |
4246 | ||
61df10c7 | 4247 | enum bpf_access_src { |
01f810ac AM |
4248 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
4249 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
4250 | }; | |
4251 | ||
4252 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
4253 | int regno, int off, int access_size, | |
4254 | bool zero_size_allowed, | |
61df10c7 | 4255 | enum bpf_access_src type, |
01f810ac AM |
4256 | struct bpf_call_arg_meta *meta); |
4257 | ||
4258 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
4259 | { | |
4260 | return cur_regs(env) + regno; | |
4261 | } | |
4262 | ||
4263 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
4264 | * 'dst_regno'. | |
4265 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
4266 | * but not its variable offset. | |
4267 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
4268 | * | |
4269 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
4270 | * filling registers (i.e. reads of spilled register cannot be detected when | |
4271 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
4272 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
4273 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
4274 | * instead. | |
4275 | */ | |
4276 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
4277 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 4278 | { |
01f810ac AM |
4279 | /* The state of the source register. */ |
4280 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4281 | struct bpf_func_state *ptr_state = func(env, reg); | |
4282 | int err; | |
4283 | int min_off, max_off; | |
4284 | ||
4285 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 4286 | */ |
01f810ac AM |
4287 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
4288 | false, ACCESS_DIRECT, NULL); | |
4289 | if (err) | |
4290 | return err; | |
4291 | ||
4292 | min_off = reg->smin_value + off; | |
4293 | max_off = reg->smax_value + off; | |
4294 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
4295 | return 0; | |
4296 | } | |
4297 | ||
4298 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
4299 | * check_stack_read_var_off. | |
4300 | * | |
4301 | * The caller must ensure that the offset falls within the allocated stack | |
4302 | * bounds. | |
4303 | * | |
4304 | * 'dst_regno' is a register which will receive the value from the stack. It | |
4305 | * can be -1, meaning that the read value is not going to a register. | |
4306 | */ | |
4307 | static int check_stack_read(struct bpf_verifier_env *env, | |
4308 | int ptr_regno, int off, int size, | |
4309 | int dst_regno) | |
4310 | { | |
4311 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4312 | struct bpf_func_state *state = func(env, reg); | |
4313 | int err; | |
4314 | /* Some accesses are only permitted with a static offset. */ | |
4315 | bool var_off = !tnum_is_const(reg->var_off); | |
4316 | ||
4317 | /* The offset is required to be static when reads don't go to a | |
4318 | * register, in order to not leak pointers (see | |
4319 | * check_stack_read_fixed_off). | |
4320 | */ | |
4321 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
4322 | char tn_buf[48]; |
4323 | ||
4324 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 4325 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
4326 | tn_buf, off, size); |
4327 | return -EACCES; | |
4328 | } | |
01f810ac AM |
4329 | /* Variable offset is prohibited for unprivileged mode for simplicity |
4330 | * since it requires corresponding support in Spectre masking for stack | |
082cdc69 LG |
4331 | * ALU. See also retrieve_ptr_limit(). The check in |
4332 | * check_stack_access_for_ptr_arithmetic() called by | |
4333 | * adjust_ptr_min_max_vals() prevents users from creating stack pointers | |
4334 | * with variable offsets, therefore no check is required here. Further, | |
4335 | * just checking it here would be insufficient as speculative stack | |
4336 | * writes could still lead to unsafe speculative behaviour. | |
01f810ac | 4337 | */ |
01f810ac AM |
4338 | if (!var_off) { |
4339 | off += reg->var_off.value; | |
4340 | err = check_stack_read_fixed_off(env, state, off, size, | |
4341 | dst_regno); | |
4342 | } else { | |
4343 | /* Variable offset stack reads need more conservative handling | |
4344 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
4345 | * branch. | |
4346 | */ | |
4347 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
4348 | dst_regno); | |
4349 | } | |
4350 | return err; | |
4351 | } | |
4352 | ||
4353 | ||
4354 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
4355 | * check_stack_write_var_off. | |
4356 | * | |
4357 | * 'ptr_regno' is the register used as a pointer into the stack. | |
4358 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
4359 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
4360 | * be -1, meaning that we're not writing from a register. | |
4361 | * | |
4362 | * The caller must ensure that the offset falls within the maximum stack size. | |
4363 | */ | |
4364 | static int check_stack_write(struct bpf_verifier_env *env, | |
4365 | int ptr_regno, int off, int size, | |
4366 | int value_regno, int insn_idx) | |
4367 | { | |
4368 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
4369 | struct bpf_func_state *state = func(env, reg); | |
4370 | int err; | |
4371 | ||
4372 | if (tnum_is_const(reg->var_off)) { | |
4373 | off += reg->var_off.value; | |
4374 | err = check_stack_write_fixed_off(env, state, off, size, | |
4375 | value_regno, insn_idx); | |
4376 | } else { | |
4377 | /* Variable offset stack reads need more conservative handling | |
4378 | * than fixed offset ones. | |
4379 | */ | |
4380 | err = check_stack_write_var_off(env, state, | |
4381 | ptr_regno, off, size, | |
4382 | value_regno, insn_idx); | |
4383 | } | |
4384 | return err; | |
e4298d25 DB |
4385 | } |
4386 | ||
591fe988 DB |
4387 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
4388 | int off, int size, enum bpf_access_type type) | |
4389 | { | |
4390 | struct bpf_reg_state *regs = cur_regs(env); | |
4391 | struct bpf_map *map = regs[regno].map_ptr; | |
4392 | u32 cap = bpf_map_flags_to_cap(map); | |
4393 | ||
4394 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
4395 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
4396 | map->value_size, off, size); | |
4397 | return -EACCES; | |
4398 | } | |
4399 | ||
4400 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
4401 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
4402 | map->value_size, off, size); | |
4403 | return -EACCES; | |
4404 | } | |
4405 | ||
4406 | return 0; | |
4407 | } | |
4408 | ||
457f4436 AN |
4409 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
4410 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
4411 | int off, int size, u32 mem_size, | |
4412 | bool zero_size_allowed) | |
17a52670 | 4413 | { |
457f4436 AN |
4414 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
4415 | struct bpf_reg_state *reg; | |
4416 | ||
4417 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
4418 | return 0; | |
17a52670 | 4419 | |
457f4436 AN |
4420 | reg = &cur_regs(env)[regno]; |
4421 | switch (reg->type) { | |
69c087ba YS |
4422 | case PTR_TO_MAP_KEY: |
4423 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
4424 | mem_size, off, size); | |
4425 | break; | |
457f4436 | 4426 | case PTR_TO_MAP_VALUE: |
61bd5218 | 4427 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
4428 | mem_size, off, size); |
4429 | break; | |
4430 | case PTR_TO_PACKET: | |
4431 | case PTR_TO_PACKET_META: | |
4432 | case PTR_TO_PACKET_END: | |
4433 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
4434 | off, size, regno, reg->id, off, mem_size); | |
4435 | break; | |
4436 | case PTR_TO_MEM: | |
4437 | default: | |
4438 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
4439 | mem_size, off, size); | |
17a52670 | 4440 | } |
457f4436 AN |
4441 | |
4442 | return -EACCES; | |
17a52670 AS |
4443 | } |
4444 | ||
457f4436 AN |
4445 | /* check read/write into a memory region with possible variable offset */ |
4446 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
4447 | int off, int size, u32 mem_size, | |
4448 | bool zero_size_allowed) | |
dbcfe5f7 | 4449 | { |
f4d7e40a AS |
4450 | struct bpf_verifier_state *vstate = env->cur_state; |
4451 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
4452 | struct bpf_reg_state *reg = &state->regs[regno]; |
4453 | int err; | |
4454 | ||
457f4436 | 4455 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
4456 | * need to try adding each of min_value and max_value to off |
4457 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
4458 | * |
4459 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
4460 | * comparisons where we can't assume the floor of a |
4461 | * value is 0. If we are using signed variables for our | |
4462 | * index'es we need to make sure that whatever we use | |
4463 | * will have a set floor within our range. | |
4464 | */ | |
b7137c4e DB |
4465 | if (reg->smin_value < 0 && |
4466 | (reg->smin_value == S64_MIN || | |
4467 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
4468 | reg->smin_value + off < 0)) { | |
61bd5218 | 4469 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
4470 | regno); |
4471 | return -EACCES; | |
4472 | } | |
457f4436 AN |
4473 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
4474 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 4475 | if (err) { |
457f4436 | 4476 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 4477 | regno); |
dbcfe5f7 GB |
4478 | return err; |
4479 | } | |
4480 | ||
b03c9f9f EC |
4481 | /* If we haven't set a max value then we need to bail since we can't be |
4482 | * sure we won't do bad things. | |
4483 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 4484 | */ |
b03c9f9f | 4485 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 4486 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
4487 | regno); |
4488 | return -EACCES; | |
4489 | } | |
457f4436 AN |
4490 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
4491 | mem_size, zero_size_allowed); | |
4492 | if (err) { | |
4493 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 4494 | regno); |
457f4436 AN |
4495 | return err; |
4496 | } | |
4497 | ||
4498 | return 0; | |
4499 | } | |
d83525ca | 4500 | |
e9147b44 KKD |
4501 | static int __check_ptr_off_reg(struct bpf_verifier_env *env, |
4502 | const struct bpf_reg_state *reg, int regno, | |
4503 | bool fixed_off_ok) | |
4504 | { | |
4505 | /* Access to this pointer-typed register or passing it to a helper | |
4506 | * is only allowed in its original, unmodified form. | |
4507 | */ | |
4508 | ||
4509 | if (reg->off < 0) { | |
4510 | verbose(env, "negative offset %s ptr R%d off=%d disallowed\n", | |
4511 | reg_type_str(env, reg->type), regno, reg->off); | |
4512 | return -EACCES; | |
4513 | } | |
4514 | ||
4515 | if (!fixed_off_ok && reg->off) { | |
4516 | verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n", | |
4517 | reg_type_str(env, reg->type), regno, reg->off); | |
4518 | return -EACCES; | |
4519 | } | |
4520 | ||
4521 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4522 | char tn_buf[48]; | |
4523 | ||
4524 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4525 | verbose(env, "variable %s access var_off=%s disallowed\n", | |
4526 | reg_type_str(env, reg->type), tn_buf); | |
4527 | return -EACCES; | |
4528 | } | |
4529 | ||
4530 | return 0; | |
4531 | } | |
4532 | ||
4533 | int check_ptr_off_reg(struct bpf_verifier_env *env, | |
4534 | const struct bpf_reg_state *reg, int regno) | |
4535 | { | |
4536 | return __check_ptr_off_reg(env, reg, regno, false); | |
4537 | } | |
4538 | ||
61df10c7 | 4539 | static int map_kptr_match_type(struct bpf_verifier_env *env, |
aa3496ac | 4540 | struct btf_field *kptr_field, |
61df10c7 KKD |
4541 | struct bpf_reg_state *reg, u32 regno) |
4542 | { | |
b32a5dae | 4543 | const char *targ_name = btf_type_name(kptr_field->kptr.btf, kptr_field->kptr.btf_id); |
20c09d92 | 4544 | int perm_flags = PTR_MAYBE_NULL | PTR_TRUSTED | MEM_RCU; |
61df10c7 KKD |
4545 | const char *reg_name = ""; |
4546 | ||
6efe152d | 4547 | /* Only unreferenced case accepts untrusted pointers */ |
aa3496ac | 4548 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
4549 | perm_flags |= PTR_UNTRUSTED; |
4550 | ||
4551 | if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags)) | |
61df10c7 KKD |
4552 | goto bad_type; |
4553 | ||
4554 | if (!btf_is_kernel(reg->btf)) { | |
4555 | verbose(env, "R%d must point to kernel BTF\n", regno); | |
4556 | return -EINVAL; | |
4557 | } | |
4558 | /* We need to verify reg->type and reg->btf, before accessing reg->btf */ | |
b32a5dae | 4559 | reg_name = btf_type_name(reg->btf, reg->btf_id); |
61df10c7 | 4560 | |
c0a5a21c KKD |
4561 | /* For ref_ptr case, release function check should ensure we get one |
4562 | * referenced PTR_TO_BTF_ID, and that its fixed offset is 0. For the | |
4563 | * normal store of unreferenced kptr, we must ensure var_off is zero. | |
4564 | * Since ref_ptr cannot be accessed directly by BPF insns, checks for | |
4565 | * reg->off and reg->ref_obj_id are not needed here. | |
4566 | */ | |
61df10c7 KKD |
4567 | if (__check_ptr_off_reg(env, reg, regno, true)) |
4568 | return -EACCES; | |
4569 | ||
4570 | /* A full type match is needed, as BTF can be vmlinux or module BTF, and | |
4571 | * we also need to take into account the reg->off. | |
4572 | * | |
4573 | * We want to support cases like: | |
4574 | * | |
4575 | * struct foo { | |
4576 | * struct bar br; | |
4577 | * struct baz bz; | |
4578 | * }; | |
4579 | * | |
4580 | * struct foo *v; | |
4581 | * v = func(); // PTR_TO_BTF_ID | |
4582 | * val->foo = v; // reg->off is zero, btf and btf_id match type | |
4583 | * val->bar = &v->br; // reg->off is still zero, but we need to retry with | |
4584 | * // first member type of struct after comparison fails | |
4585 | * val->baz = &v->bz; // reg->off is non-zero, so struct needs to be walked | |
4586 | * // to match type | |
4587 | * | |
4588 | * In the kptr_ref case, check_func_arg_reg_off already ensures reg->off | |
2ab3b380 KKD |
4589 | * is zero. We must also ensure that btf_struct_ids_match does not walk |
4590 | * the struct to match type against first member of struct, i.e. reject | |
4591 | * second case from above. Hence, when type is BPF_KPTR_REF, we set | |
4592 | * strict mode to true for type match. | |
61df10c7 KKD |
4593 | */ |
4594 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
aa3496ac KKD |
4595 | kptr_field->kptr.btf, kptr_field->kptr.btf_id, |
4596 | kptr_field->type == BPF_KPTR_REF)) | |
61df10c7 KKD |
4597 | goto bad_type; |
4598 | return 0; | |
4599 | bad_type: | |
4600 | verbose(env, "invalid kptr access, R%d type=%s%s ", regno, | |
4601 | reg_type_str(env, reg->type), reg_name); | |
6efe152d | 4602 | verbose(env, "expected=%s%s", reg_type_str(env, PTR_TO_BTF_ID), targ_name); |
aa3496ac | 4603 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
4604 | verbose(env, " or %s%s\n", reg_type_str(env, PTR_TO_BTF_ID | PTR_UNTRUSTED), |
4605 | targ_name); | |
4606 | else | |
4607 | verbose(env, "\n"); | |
61df10c7 KKD |
4608 | return -EINVAL; |
4609 | } | |
4610 | ||
20c09d92 AS |
4611 | /* The non-sleepable programs and sleepable programs with explicit bpf_rcu_read_lock() |
4612 | * can dereference RCU protected pointers and result is PTR_TRUSTED. | |
4613 | */ | |
4614 | static bool in_rcu_cs(struct bpf_verifier_env *env) | |
4615 | { | |
4616 | return env->cur_state->active_rcu_lock || !env->prog->aux->sleepable; | |
4617 | } | |
4618 | ||
4619 | /* Once GCC supports btf_type_tag the following mechanism will be replaced with tag check */ | |
4620 | BTF_SET_START(rcu_protected_types) | |
4621 | BTF_ID(struct, prog_test_ref_kfunc) | |
4622 | BTF_ID(struct, cgroup) | |
63d2d83d | 4623 | BTF_ID(struct, bpf_cpumask) |
d02c48fa | 4624 | BTF_ID(struct, task_struct) |
20c09d92 AS |
4625 | BTF_SET_END(rcu_protected_types) |
4626 | ||
4627 | static bool rcu_protected_object(const struct btf *btf, u32 btf_id) | |
4628 | { | |
4629 | if (!btf_is_kernel(btf)) | |
4630 | return false; | |
4631 | return btf_id_set_contains(&rcu_protected_types, btf_id); | |
4632 | } | |
4633 | ||
4634 | static bool rcu_safe_kptr(const struct btf_field *field) | |
4635 | { | |
4636 | const struct btf_field_kptr *kptr = &field->kptr; | |
4637 | ||
4638 | return field->type == BPF_KPTR_REF && rcu_protected_object(kptr->btf, kptr->btf_id); | |
4639 | } | |
4640 | ||
61df10c7 KKD |
4641 | static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, |
4642 | int value_regno, int insn_idx, | |
aa3496ac | 4643 | struct btf_field *kptr_field) |
61df10c7 KKD |
4644 | { |
4645 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; | |
4646 | int class = BPF_CLASS(insn->code); | |
4647 | struct bpf_reg_state *val_reg; | |
4648 | ||
4649 | /* Things we already checked for in check_map_access and caller: | |
4650 | * - Reject cases where variable offset may touch kptr | |
4651 | * - size of access (must be BPF_DW) | |
4652 | * - tnum_is_const(reg->var_off) | |
aa3496ac | 4653 | * - kptr_field->offset == off + reg->var_off.value |
61df10c7 KKD |
4654 | */ |
4655 | /* Only BPF_[LDX,STX,ST] | BPF_MEM | BPF_DW is supported */ | |
4656 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
4657 | verbose(env, "kptr in map can only be accessed using BPF_MEM instruction mode\n"); | |
4658 | return -EACCES; | |
4659 | } | |
4660 | ||
6efe152d KKD |
4661 | /* We only allow loading referenced kptr, since it will be marked as |
4662 | * untrusted, similar to unreferenced kptr. | |
4663 | */ | |
aa3496ac | 4664 | if (class != BPF_LDX && kptr_field->type == BPF_KPTR_REF) { |
6efe152d | 4665 | verbose(env, "store to referenced kptr disallowed\n"); |
c0a5a21c KKD |
4666 | return -EACCES; |
4667 | } | |
4668 | ||
61df10c7 KKD |
4669 | if (class == BPF_LDX) { |
4670 | val_reg = reg_state(env, value_regno); | |
4671 | /* We can simply mark the value_regno receiving the pointer | |
4672 | * value from map as PTR_TO_BTF_ID, with the correct type. | |
4673 | */ | |
aa3496ac | 4674 | mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, kptr_field->kptr.btf, |
20c09d92 AS |
4675 | kptr_field->kptr.btf_id, |
4676 | rcu_safe_kptr(kptr_field) && in_rcu_cs(env) ? | |
4677 | PTR_MAYBE_NULL | MEM_RCU : | |
4678 | PTR_MAYBE_NULL | PTR_UNTRUSTED); | |
61df10c7 KKD |
4679 | /* For mark_ptr_or_null_reg */ |
4680 | val_reg->id = ++env->id_gen; | |
4681 | } else if (class == BPF_STX) { | |
4682 | val_reg = reg_state(env, value_regno); | |
4683 | if (!register_is_null(val_reg) && | |
aa3496ac | 4684 | map_kptr_match_type(env, kptr_field, val_reg, value_regno)) |
61df10c7 KKD |
4685 | return -EACCES; |
4686 | } else if (class == BPF_ST) { | |
4687 | if (insn->imm) { | |
4688 | verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%u\n", | |
aa3496ac | 4689 | kptr_field->offset); |
61df10c7 KKD |
4690 | return -EACCES; |
4691 | } | |
4692 | } else { | |
4693 | verbose(env, "kptr in map can only be accessed using BPF_LDX/BPF_STX/BPF_ST\n"); | |
4694 | return -EACCES; | |
4695 | } | |
4696 | return 0; | |
4697 | } | |
4698 | ||
457f4436 AN |
4699 | /* check read/write into a map element with possible variable offset */ |
4700 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
61df10c7 KKD |
4701 | int off, int size, bool zero_size_allowed, |
4702 | enum bpf_access_src src) | |
457f4436 AN |
4703 | { |
4704 | struct bpf_verifier_state *vstate = env->cur_state; | |
4705 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4706 | struct bpf_reg_state *reg = &state->regs[regno]; | |
4707 | struct bpf_map *map = reg->map_ptr; | |
aa3496ac KKD |
4708 | struct btf_record *rec; |
4709 | int err, i; | |
457f4436 AN |
4710 | |
4711 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
4712 | zero_size_allowed); | |
4713 | if (err) | |
4714 | return err; | |
4715 | ||
aa3496ac KKD |
4716 | if (IS_ERR_OR_NULL(map->record)) |
4717 | return 0; | |
4718 | rec = map->record; | |
4719 | for (i = 0; i < rec->cnt; i++) { | |
4720 | struct btf_field *field = &rec->fields[i]; | |
4721 | u32 p = field->offset; | |
d83525ca | 4722 | |
db559117 KKD |
4723 | /* If any part of a field can be touched by load/store, reject |
4724 | * this program. To check that [x1, x2) overlaps with [y1, y2), | |
d83525ca AS |
4725 | * it is sufficient to check x1 < y2 && y1 < x2. |
4726 | */ | |
aa3496ac KKD |
4727 | if (reg->smin_value + off < p + btf_field_type_size(field->type) && |
4728 | p < reg->umax_value + off + size) { | |
4729 | switch (field->type) { | |
4730 | case BPF_KPTR_UNREF: | |
4731 | case BPF_KPTR_REF: | |
61df10c7 KKD |
4732 | if (src != ACCESS_DIRECT) { |
4733 | verbose(env, "kptr cannot be accessed indirectly by helper\n"); | |
4734 | return -EACCES; | |
4735 | } | |
4736 | if (!tnum_is_const(reg->var_off)) { | |
4737 | verbose(env, "kptr access cannot have variable offset\n"); | |
4738 | return -EACCES; | |
4739 | } | |
4740 | if (p != off + reg->var_off.value) { | |
4741 | verbose(env, "kptr access misaligned expected=%u off=%llu\n", | |
4742 | p, off + reg->var_off.value); | |
4743 | return -EACCES; | |
4744 | } | |
4745 | if (size != bpf_size_to_bytes(BPF_DW)) { | |
4746 | verbose(env, "kptr access size must be BPF_DW\n"); | |
4747 | return -EACCES; | |
4748 | } | |
4749 | break; | |
aa3496ac | 4750 | default: |
db559117 KKD |
4751 | verbose(env, "%s cannot be accessed directly by load/store\n", |
4752 | btf_field_type_name(field->type)); | |
aa3496ac | 4753 | return -EACCES; |
61df10c7 KKD |
4754 | } |
4755 | } | |
4756 | } | |
aa3496ac | 4757 | return 0; |
dbcfe5f7 GB |
4758 | } |
4759 | ||
969bf05e AS |
4760 | #define MAX_PACKET_OFF 0xffff |
4761 | ||
58e2af8b | 4762 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
4763 | const struct bpf_call_arg_meta *meta, |
4764 | enum bpf_access_type t) | |
4acf6c0b | 4765 | { |
7e40781c UP |
4766 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
4767 | ||
4768 | switch (prog_type) { | |
5d66fa7d | 4769 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
4770 | case BPF_PROG_TYPE_LWT_IN: |
4771 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 4772 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 4773 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 4774 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 4775 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
4776 | if (t == BPF_WRITE) |
4777 | return false; | |
8731745e | 4778 | fallthrough; |
5d66fa7d DB |
4779 | |
4780 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
4781 | case BPF_PROG_TYPE_SCHED_CLS: |
4782 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 4783 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 4784 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 4785 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 4786 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
4787 | if (meta) |
4788 | return meta->pkt_access; | |
4789 | ||
4790 | env->seen_direct_write = true; | |
4acf6c0b | 4791 | return true; |
0d01da6a SF |
4792 | |
4793 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
4794 | if (t == BPF_WRITE) | |
4795 | env->seen_direct_write = true; | |
4796 | ||
4797 | return true; | |
4798 | ||
4acf6c0b BB |
4799 | default: |
4800 | return false; | |
4801 | } | |
4802 | } | |
4803 | ||
f1174f77 | 4804 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 4805 | int size, bool zero_size_allowed) |
f1174f77 | 4806 | { |
638f5b90 | 4807 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
4808 | struct bpf_reg_state *reg = ®s[regno]; |
4809 | int err; | |
4810 | ||
4811 | /* We may have added a variable offset to the packet pointer; but any | |
4812 | * reg->range we have comes after that. We are only checking the fixed | |
4813 | * offset. | |
4814 | */ | |
4815 | ||
4816 | /* We don't allow negative numbers, because we aren't tracking enough | |
4817 | * detail to prove they're safe. | |
4818 | */ | |
b03c9f9f | 4819 | if (reg->smin_value < 0) { |
61bd5218 | 4820 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
4821 | regno); |
4822 | return -EACCES; | |
4823 | } | |
6d94e741 AS |
4824 | |
4825 | err = reg->range < 0 ? -EINVAL : | |
4826 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 4827 | zero_size_allowed); |
f1174f77 | 4828 | if (err) { |
61bd5218 | 4829 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
4830 | return err; |
4831 | } | |
e647815a | 4832 | |
457f4436 | 4833 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
4834 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
4835 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 4836 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
4837 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
4838 | */ | |
4839 | env->prog->aux->max_pkt_offset = | |
4840 | max_t(u32, env->prog->aux->max_pkt_offset, | |
4841 | off + reg->umax_value + size - 1); | |
4842 | ||
f1174f77 EC |
4843 | return err; |
4844 | } | |
4845 | ||
4846 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 4847 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 4848 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 4849 | struct btf **btf, u32 *btf_id) |
17a52670 | 4850 | { |
f96da094 DB |
4851 | struct bpf_insn_access_aux info = { |
4852 | .reg_type = *reg_type, | |
9e15db66 | 4853 | .log = &env->log, |
f96da094 | 4854 | }; |
31fd8581 | 4855 | |
4f9218aa | 4856 | if (env->ops->is_valid_access && |
5e43f899 | 4857 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
4858 | /* A non zero info.ctx_field_size indicates that this field is a |
4859 | * candidate for later verifier transformation to load the whole | |
4860 | * field and then apply a mask when accessed with a narrower | |
4861 | * access than actual ctx access size. A zero info.ctx_field_size | |
4862 | * will only allow for whole field access and rejects any other | |
4863 | * type of narrower access. | |
31fd8581 | 4864 | */ |
23994631 | 4865 | *reg_type = info.reg_type; |
31fd8581 | 4866 | |
c25b2ae1 | 4867 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 4868 | *btf = info.btf; |
9e15db66 | 4869 | *btf_id = info.btf_id; |
22dc4a0f | 4870 | } else { |
9e15db66 | 4871 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 4872 | } |
32bbe007 AS |
4873 | /* remember the offset of last byte accessed in ctx */ |
4874 | if (env->prog->aux->max_ctx_offset < off + size) | |
4875 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 4876 | return 0; |
32bbe007 | 4877 | } |
17a52670 | 4878 | |
61bd5218 | 4879 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
4880 | return -EACCES; |
4881 | } | |
4882 | ||
d58e468b PP |
4883 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
4884 | int size) | |
4885 | { | |
4886 | if (size < 0 || off < 0 || | |
4887 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
4888 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
4889 | off, size); | |
4890 | return -EACCES; | |
4891 | } | |
4892 | return 0; | |
4893 | } | |
4894 | ||
5f456649 MKL |
4895 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
4896 | u32 regno, int off, int size, | |
4897 | enum bpf_access_type t) | |
c64b7983 JS |
4898 | { |
4899 | struct bpf_reg_state *regs = cur_regs(env); | |
4900 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 4901 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 4902 | bool valid; |
c64b7983 JS |
4903 | |
4904 | if (reg->smin_value < 0) { | |
4905 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
4906 | regno); | |
4907 | return -EACCES; | |
4908 | } | |
4909 | ||
46f8bc92 MKL |
4910 | switch (reg->type) { |
4911 | case PTR_TO_SOCK_COMMON: | |
4912 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
4913 | break; | |
4914 | case PTR_TO_SOCKET: | |
4915 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
4916 | break; | |
655a51e5 MKL |
4917 | case PTR_TO_TCP_SOCK: |
4918 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
4919 | break; | |
fada7fdc JL |
4920 | case PTR_TO_XDP_SOCK: |
4921 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
4922 | break; | |
46f8bc92 MKL |
4923 | default: |
4924 | valid = false; | |
c64b7983 JS |
4925 | } |
4926 | ||
5f456649 | 4927 | |
46f8bc92 MKL |
4928 | if (valid) { |
4929 | env->insn_aux_data[insn_idx].ctx_field_size = | |
4930 | info.ctx_field_size; | |
4931 | return 0; | |
4932 | } | |
4933 | ||
4934 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
c25b2ae1 | 4935 | regno, reg_type_str(env, reg->type), off, size); |
46f8bc92 MKL |
4936 | |
4937 | return -EACCES; | |
c64b7983 JS |
4938 | } |
4939 | ||
4cabc5b1 DB |
4940 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
4941 | { | |
2a159c6f | 4942 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
4943 | } |
4944 | ||
f37a8cb8 DB |
4945 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
4946 | { | |
2a159c6f | 4947 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 4948 | |
46f8bc92 MKL |
4949 | return reg->type == PTR_TO_CTX; |
4950 | } | |
4951 | ||
4952 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
4953 | { | |
4954 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4955 | ||
4956 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
4957 | } |
4958 | ||
ca369602 DB |
4959 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
4960 | { | |
2a159c6f | 4961 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
4962 | |
4963 | return type_is_pkt_pointer(reg->type); | |
4964 | } | |
4965 | ||
4b5defde DB |
4966 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
4967 | { | |
4968 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4969 | ||
4970 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
4971 | return reg->type == PTR_TO_FLOW_KEYS; | |
4972 | } | |
4973 | ||
9bb00b28 YS |
4974 | static bool is_trusted_reg(const struct bpf_reg_state *reg) |
4975 | { | |
4976 | /* A referenced register is always trusted. */ | |
4977 | if (reg->ref_obj_id) | |
4978 | return true; | |
4979 | ||
4980 | /* If a register is not referenced, it is trusted if it has the | |
fca1aa75 | 4981 | * MEM_ALLOC or PTR_TRUSTED type modifiers, and no others. Some of the |
9bb00b28 YS |
4982 | * other type modifiers may be safe, but we elect to take an opt-in |
4983 | * approach here as some (e.g. PTR_UNTRUSTED and PTR_MAYBE_NULL) are | |
4984 | * not. | |
4985 | * | |
4986 | * Eventually, we should make PTR_TRUSTED the single source of truth | |
4987 | * for whether a register is trusted. | |
4988 | */ | |
4989 | return type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS && | |
4990 | !bpf_type_has_unsafe_modifiers(reg->type); | |
4991 | } | |
4992 | ||
fca1aa75 YS |
4993 | static bool is_rcu_reg(const struct bpf_reg_state *reg) |
4994 | { | |
4995 | return reg->type & MEM_RCU; | |
4996 | } | |
4997 | ||
afeebf9f AS |
4998 | static void clear_trusted_flags(enum bpf_type_flag *flag) |
4999 | { | |
5000 | *flag &= ~(BPF_REG_TRUSTED_MODIFIERS | MEM_RCU); | |
5001 | } | |
5002 | ||
61bd5218 JK |
5003 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
5004 | const struct bpf_reg_state *reg, | |
d1174416 | 5005 | int off, int size, bool strict) |
969bf05e | 5006 | { |
f1174f77 | 5007 | struct tnum reg_off; |
e07b98d9 | 5008 | int ip_align; |
d1174416 DM |
5009 | |
5010 | /* Byte size accesses are always allowed. */ | |
5011 | if (!strict || size == 1) | |
5012 | return 0; | |
5013 | ||
e4eda884 DM |
5014 | /* For platforms that do not have a Kconfig enabling |
5015 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
5016 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
5017 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
5018 | * to this code only in strict mode where we want to emulate | |
5019 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
5020 | * unconditional IP align value of '2'. | |
e07b98d9 | 5021 | */ |
e4eda884 | 5022 | ip_align = 2; |
f1174f77 EC |
5023 | |
5024 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
5025 | if (!tnum_is_aligned(reg_off, size)) { | |
5026 | char tn_buf[48]; | |
5027 | ||
5028 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
5029 | verbose(env, |
5030 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 5031 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
5032 | return -EACCES; |
5033 | } | |
79adffcd | 5034 | |
969bf05e AS |
5035 | return 0; |
5036 | } | |
5037 | ||
61bd5218 JK |
5038 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
5039 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
5040 | const char *pointer_desc, |
5041 | int off, int size, bool strict) | |
79adffcd | 5042 | { |
f1174f77 EC |
5043 | struct tnum reg_off; |
5044 | ||
5045 | /* Byte size accesses are always allowed. */ | |
5046 | if (!strict || size == 1) | |
5047 | return 0; | |
5048 | ||
5049 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
5050 | if (!tnum_is_aligned(reg_off, size)) { | |
5051 | char tn_buf[48]; | |
5052 | ||
5053 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 5054 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 5055 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
5056 | return -EACCES; |
5057 | } | |
5058 | ||
969bf05e AS |
5059 | return 0; |
5060 | } | |
5061 | ||
e07b98d9 | 5062 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
5063 | const struct bpf_reg_state *reg, int off, |
5064 | int size, bool strict_alignment_once) | |
79adffcd | 5065 | { |
ca369602 | 5066 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 5067 | const char *pointer_desc = ""; |
d1174416 | 5068 | |
79adffcd DB |
5069 | switch (reg->type) { |
5070 | case PTR_TO_PACKET: | |
de8f3a83 DB |
5071 | case PTR_TO_PACKET_META: |
5072 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
5073 | * right in front, treat it the very same way. | |
5074 | */ | |
61bd5218 | 5075 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
5076 | case PTR_TO_FLOW_KEYS: |
5077 | pointer_desc = "flow keys "; | |
5078 | break; | |
69c087ba YS |
5079 | case PTR_TO_MAP_KEY: |
5080 | pointer_desc = "key "; | |
5081 | break; | |
f1174f77 EC |
5082 | case PTR_TO_MAP_VALUE: |
5083 | pointer_desc = "value "; | |
5084 | break; | |
5085 | case PTR_TO_CTX: | |
5086 | pointer_desc = "context "; | |
5087 | break; | |
5088 | case PTR_TO_STACK: | |
5089 | pointer_desc = "stack "; | |
01f810ac AM |
5090 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
5091 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
5092 | * aligned. |
5093 | */ | |
5094 | strict = true; | |
f1174f77 | 5095 | break; |
c64b7983 JS |
5096 | case PTR_TO_SOCKET: |
5097 | pointer_desc = "sock "; | |
5098 | break; | |
46f8bc92 MKL |
5099 | case PTR_TO_SOCK_COMMON: |
5100 | pointer_desc = "sock_common "; | |
5101 | break; | |
655a51e5 MKL |
5102 | case PTR_TO_TCP_SOCK: |
5103 | pointer_desc = "tcp_sock "; | |
5104 | break; | |
fada7fdc JL |
5105 | case PTR_TO_XDP_SOCK: |
5106 | pointer_desc = "xdp_sock "; | |
5107 | break; | |
79adffcd | 5108 | default: |
f1174f77 | 5109 | break; |
79adffcd | 5110 | } |
61bd5218 JK |
5111 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
5112 | strict); | |
79adffcd DB |
5113 | } |
5114 | ||
f4d7e40a AS |
5115 | static int update_stack_depth(struct bpf_verifier_env *env, |
5116 | const struct bpf_func_state *func, | |
5117 | int off) | |
5118 | { | |
9c8105bd | 5119 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
5120 | |
5121 | if (stack >= -off) | |
5122 | return 0; | |
5123 | ||
5124 | /* update known max for given subprogram */ | |
9c8105bd | 5125 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
5126 | return 0; |
5127 | } | |
f4d7e40a | 5128 | |
70a87ffe AS |
5129 | /* starting from main bpf function walk all instructions of the function |
5130 | * and recursively walk all callees that given function can call. | |
5131 | * Ignore jump and exit insns. | |
5132 | * Since recursion is prevented by check_cfg() this algorithm | |
5133 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
5134 | */ | |
5135 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
5136 | { | |
9c8105bd JW |
5137 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
5138 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 5139 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 5140 | bool tail_call_reachable = false; |
70a87ffe AS |
5141 | int ret_insn[MAX_CALL_FRAMES]; |
5142 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 5143 | int j; |
f4d7e40a | 5144 | |
70a87ffe | 5145 | process_func: |
7f6e4312 MF |
5146 | /* protect against potential stack overflow that might happen when |
5147 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
5148 | * depth for such case down to 256 so that the worst case scenario | |
5149 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
5150 | * 8k). | |
5151 | * | |
5152 | * To get the idea what might happen, see an example: | |
5153 | * func1 -> sub rsp, 128 | |
5154 | * subfunc1 -> sub rsp, 256 | |
5155 | * tailcall1 -> add rsp, 256 | |
5156 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
5157 | * subfunc2 -> sub rsp, 64 | |
5158 | * subfunc22 -> sub rsp, 128 | |
5159 | * tailcall2 -> add rsp, 128 | |
5160 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
5161 | * | |
5162 | * tailcall will unwind the current stack frame but it will not get rid | |
5163 | * of caller's stack as shown on the example above. | |
5164 | */ | |
5165 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
5166 | verbose(env, | |
5167 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
5168 | depth); | |
5169 | return -EACCES; | |
5170 | } | |
70a87ffe AS |
5171 | /* round up to 32-bytes, since this is granularity |
5172 | * of interpreter stack size | |
5173 | */ | |
9c8105bd | 5174 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 5175 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 5176 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 5177 | frame + 1, depth); |
f4d7e40a AS |
5178 | return -EACCES; |
5179 | } | |
70a87ffe | 5180 | continue_func: |
4cb3d99c | 5181 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 5182 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
5183 | int next_insn; |
5184 | ||
69c087ba | 5185 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
5186 | continue; |
5187 | /* remember insn and function to return to */ | |
5188 | ret_insn[frame] = i + 1; | |
9c8105bd | 5189 | ret_prog[frame] = idx; |
70a87ffe AS |
5190 | |
5191 | /* find the callee */ | |
7ddc80a4 AS |
5192 | next_insn = i + insn[i].imm + 1; |
5193 | idx = find_subprog(env, next_insn); | |
9c8105bd | 5194 | if (idx < 0) { |
70a87ffe | 5195 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 5196 | next_insn); |
70a87ffe AS |
5197 | return -EFAULT; |
5198 | } | |
7ddc80a4 AS |
5199 | if (subprog[idx].is_async_cb) { |
5200 | if (subprog[idx].has_tail_call) { | |
5201 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
5202 | return -EFAULT; | |
5203 | } | |
5204 | /* async callbacks don't increase bpf prog stack size */ | |
5205 | continue; | |
5206 | } | |
5207 | i = next_insn; | |
ebf7d1f5 MF |
5208 | |
5209 | if (subprog[idx].has_tail_call) | |
5210 | tail_call_reachable = true; | |
5211 | ||
70a87ffe AS |
5212 | frame++; |
5213 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
5214 | verbose(env, "the call stack of %d frames is too deep !\n", |
5215 | frame); | |
5216 | return -E2BIG; | |
70a87ffe AS |
5217 | } |
5218 | goto process_func; | |
5219 | } | |
ebf7d1f5 MF |
5220 | /* if tail call got detected across bpf2bpf calls then mark each of the |
5221 | * currently present subprog frames as tail call reachable subprogs; | |
5222 | * this info will be utilized by JIT so that we will be preserving the | |
5223 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
5224 | */ | |
5225 | if (tail_call_reachable) | |
5226 | for (j = 0; j < frame; j++) | |
5227 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
5228 | if (subprog[0].tail_call_reachable) |
5229 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 5230 | |
70a87ffe AS |
5231 | /* end of for() loop means the last insn of the 'subprog' |
5232 | * was reached. Doesn't matter whether it was JA or EXIT | |
5233 | */ | |
5234 | if (frame == 0) | |
5235 | return 0; | |
9c8105bd | 5236 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
5237 | frame--; |
5238 | i = ret_insn[frame]; | |
9c8105bd | 5239 | idx = ret_prog[frame]; |
70a87ffe | 5240 | goto continue_func; |
f4d7e40a AS |
5241 | } |
5242 | ||
19d28fbd | 5243 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
5244 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
5245 | const struct bpf_insn *insn, int idx) | |
5246 | { | |
5247 | int start = idx + insn->imm + 1, subprog; | |
5248 | ||
5249 | subprog = find_subprog(env, start); | |
5250 | if (subprog < 0) { | |
5251 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
5252 | start); | |
5253 | return -EFAULT; | |
5254 | } | |
9c8105bd | 5255 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 5256 | } |
19d28fbd | 5257 | #endif |
1ea47e01 | 5258 | |
afbf21dc YS |
5259 | static int __check_buffer_access(struct bpf_verifier_env *env, |
5260 | const char *buf_info, | |
5261 | const struct bpf_reg_state *reg, | |
5262 | int regno, int off, int size) | |
9df1c28b MM |
5263 | { |
5264 | if (off < 0) { | |
5265 | verbose(env, | |
4fc00b79 | 5266 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 5267 | regno, buf_info, off, size); |
9df1c28b MM |
5268 | return -EACCES; |
5269 | } | |
5270 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5271 | char tn_buf[48]; | |
5272 | ||
5273 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5274 | verbose(env, | |
4fc00b79 | 5275 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
5276 | regno, off, tn_buf); |
5277 | return -EACCES; | |
5278 | } | |
afbf21dc YS |
5279 | |
5280 | return 0; | |
5281 | } | |
5282 | ||
5283 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
5284 | const struct bpf_reg_state *reg, | |
5285 | int regno, int off, int size) | |
5286 | { | |
5287 | int err; | |
5288 | ||
5289 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
5290 | if (err) | |
5291 | return err; | |
5292 | ||
9df1c28b MM |
5293 | if (off + size > env->prog->aux->max_tp_access) |
5294 | env->prog->aux->max_tp_access = off + size; | |
5295 | ||
5296 | return 0; | |
5297 | } | |
5298 | ||
afbf21dc YS |
5299 | static int check_buffer_access(struct bpf_verifier_env *env, |
5300 | const struct bpf_reg_state *reg, | |
5301 | int regno, int off, int size, | |
5302 | bool zero_size_allowed, | |
afbf21dc YS |
5303 | u32 *max_access) |
5304 | { | |
44e9a741 | 5305 | const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; |
afbf21dc YS |
5306 | int err; |
5307 | ||
5308 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
5309 | if (err) | |
5310 | return err; | |
5311 | ||
5312 | if (off + size > *max_access) | |
5313 | *max_access = off + size; | |
5314 | ||
5315 | return 0; | |
5316 | } | |
5317 | ||
3f50f132 JF |
5318 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
5319 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
5320 | { | |
5321 | reg->var_off = tnum_subreg(reg->var_off); | |
5322 | __reg_assign_32_into_64(reg); | |
5323 | } | |
9df1c28b | 5324 | |
0c17d1d2 JH |
5325 | /* truncate register to smaller size (in bytes) |
5326 | * must be called with size < BPF_REG_SIZE | |
5327 | */ | |
5328 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
5329 | { | |
5330 | u64 mask; | |
5331 | ||
5332 | /* clear high bits in bit representation */ | |
5333 | reg->var_off = tnum_cast(reg->var_off, size); | |
5334 | ||
5335 | /* fix arithmetic bounds */ | |
5336 | mask = ((u64)1 << (size * 8)) - 1; | |
5337 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
5338 | reg->umin_value &= mask; | |
5339 | reg->umax_value &= mask; | |
5340 | } else { | |
5341 | reg->umin_value = 0; | |
5342 | reg->umax_value = mask; | |
5343 | } | |
5344 | reg->smin_value = reg->umin_value; | |
5345 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
5346 | |
5347 | /* If size is smaller than 32bit register the 32bit register | |
5348 | * values are also truncated so we push 64-bit bounds into | |
5349 | * 32-bit bounds. Above were truncated < 32-bits already. | |
5350 | */ | |
5351 | if (size >= 4) | |
5352 | return; | |
5353 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
5354 | } |
5355 | ||
a23740ec AN |
5356 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
5357 | { | |
353050be DB |
5358 | /* A map is considered read-only if the following condition are true: |
5359 | * | |
5360 | * 1) BPF program side cannot change any of the map content. The | |
5361 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
5362 | * and was set at map creation time. | |
5363 | * 2) The map value(s) have been initialized from user space by a | |
5364 | * loader and then "frozen", such that no new map update/delete | |
5365 | * operations from syscall side are possible for the rest of | |
5366 | * the map's lifetime from that point onwards. | |
5367 | * 3) Any parallel/pending map update/delete operations from syscall | |
5368 | * side have been completed. Only after that point, it's safe to | |
5369 | * assume that map value(s) are immutable. | |
5370 | */ | |
5371 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
5372 | READ_ONCE(map->frozen) && | |
5373 | !bpf_map_write_active(map); | |
a23740ec AN |
5374 | } |
5375 | ||
5376 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
5377 | { | |
5378 | void *ptr; | |
5379 | u64 addr; | |
5380 | int err; | |
5381 | ||
5382 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
5383 | if (err) | |
5384 | return err; | |
2dedd7d2 | 5385 | ptr = (void *)(long)addr + off; |
a23740ec AN |
5386 | |
5387 | switch (size) { | |
5388 | case sizeof(u8): | |
5389 | *val = (u64)*(u8 *)ptr; | |
5390 | break; | |
5391 | case sizeof(u16): | |
5392 | *val = (u64)*(u16 *)ptr; | |
5393 | break; | |
5394 | case sizeof(u32): | |
5395 | *val = (u64)*(u32 *)ptr; | |
5396 | break; | |
5397 | case sizeof(u64): | |
5398 | *val = *(u64 *)ptr; | |
5399 | break; | |
5400 | default: | |
5401 | return -EINVAL; | |
5402 | } | |
5403 | return 0; | |
5404 | } | |
5405 | ||
6fcd486b | 5406 | #define BTF_TYPE_SAFE_RCU(__type) __PASTE(__type, __safe_rcu) |
30ee9821 | 5407 | #define BTF_TYPE_SAFE_RCU_OR_NULL(__type) __PASTE(__type, __safe_rcu_or_null) |
6fcd486b | 5408 | #define BTF_TYPE_SAFE_TRUSTED(__type) __PASTE(__type, __safe_trusted) |
57539b1c | 5409 | |
6fcd486b AS |
5410 | /* |
5411 | * Allow list few fields as RCU trusted or full trusted. | |
5412 | * This logic doesn't allow mix tagging and will be removed once GCC supports | |
5413 | * btf_type_tag. | |
5414 | */ | |
5415 | ||
5416 | /* RCU trusted: these fields are trusted in RCU CS and never NULL */ | |
5417 | BTF_TYPE_SAFE_RCU(struct task_struct) { | |
57539b1c | 5418 | const cpumask_t *cpus_ptr; |
8d093b4e | 5419 | struct css_set __rcu *cgroups; |
6fcd486b AS |
5420 | struct task_struct __rcu *real_parent; |
5421 | struct task_struct *group_leader; | |
8d093b4e AS |
5422 | }; |
5423 | ||
30ee9821 AS |
5424 | BTF_TYPE_SAFE_RCU(struct cgroup) { |
5425 | /* cgrp->kn is always accessible as documented in kernel/cgroup/cgroup.c */ | |
5426 | struct kernfs_node *kn; | |
5427 | }; | |
5428 | ||
6fcd486b | 5429 | BTF_TYPE_SAFE_RCU(struct css_set) { |
8d093b4e | 5430 | struct cgroup *dfl_cgrp; |
57539b1c DV |
5431 | }; |
5432 | ||
30ee9821 AS |
5433 | /* RCU trusted: these fields are trusted in RCU CS and can be NULL */ |
5434 | BTF_TYPE_SAFE_RCU_OR_NULL(struct mm_struct) { | |
5435 | struct file __rcu *exe_file; | |
5436 | }; | |
5437 | ||
5438 | /* skb->sk, req->sk are not RCU protected, but we mark them as such | |
5439 | * because bpf prog accessible sockets are SOCK_RCU_FREE. | |
5440 | */ | |
5441 | BTF_TYPE_SAFE_RCU_OR_NULL(struct sk_buff) { | |
5442 | struct sock *sk; | |
5443 | }; | |
5444 | ||
5445 | BTF_TYPE_SAFE_RCU_OR_NULL(struct request_sock) { | |
5446 | struct sock *sk; | |
5447 | }; | |
5448 | ||
6fcd486b AS |
5449 | /* full trusted: these fields are trusted even outside of RCU CS and never NULL */ |
5450 | BTF_TYPE_SAFE_TRUSTED(struct bpf_iter_meta) { | |
63260df1 | 5451 | struct seq_file *seq; |
6fcd486b AS |
5452 | }; |
5453 | ||
5454 | BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task) { | |
63260df1 AS |
5455 | struct bpf_iter_meta *meta; |
5456 | struct task_struct *task; | |
6fcd486b AS |
5457 | }; |
5458 | ||
5459 | BTF_TYPE_SAFE_TRUSTED(struct linux_binprm) { | |
5460 | struct file *file; | |
5461 | }; | |
5462 | ||
5463 | BTF_TYPE_SAFE_TRUSTED(struct file) { | |
5464 | struct inode *f_inode; | |
5465 | }; | |
5466 | ||
5467 | BTF_TYPE_SAFE_TRUSTED(struct dentry) { | |
5468 | /* no negative dentry-s in places where bpf can see it */ | |
5469 | struct inode *d_inode; | |
5470 | }; | |
5471 | ||
5472 | BTF_TYPE_SAFE_TRUSTED(struct socket) { | |
5473 | struct sock *sk; | |
5474 | }; | |
5475 | ||
5476 | static bool type_is_rcu(struct bpf_verifier_env *env, | |
5477 | struct bpf_reg_state *reg, | |
63260df1 | 5478 | const char *field_name, u32 btf_id) |
57539b1c | 5479 | { |
6fcd486b | 5480 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU(struct task_struct)); |
30ee9821 | 5481 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU(struct cgroup)); |
6fcd486b | 5482 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU(struct css_set)); |
57539b1c | 5483 | |
63260df1 | 5484 | return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_rcu"); |
6fcd486b | 5485 | } |
57539b1c | 5486 | |
30ee9821 AS |
5487 | static bool type_is_rcu_or_null(struct bpf_verifier_env *env, |
5488 | struct bpf_reg_state *reg, | |
5489 | const char *field_name, u32 btf_id) | |
5490 | { | |
5491 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU_OR_NULL(struct mm_struct)); | |
5492 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU_OR_NULL(struct sk_buff)); | |
5493 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_RCU_OR_NULL(struct request_sock)); | |
5494 | ||
5495 | return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_rcu_or_null"); | |
5496 | } | |
5497 | ||
6fcd486b AS |
5498 | static bool type_is_trusted(struct bpf_verifier_env *env, |
5499 | struct bpf_reg_state *reg, | |
63260df1 | 5500 | const char *field_name, u32 btf_id) |
6fcd486b AS |
5501 | { |
5502 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter_meta)); | |
5503 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task)); | |
5504 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct linux_binprm)); | |
5505 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct file)); | |
5506 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct dentry)); | |
5507 | BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct socket)); | |
5508 | ||
63260df1 | 5509 | return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_trusted"); |
57539b1c DV |
5510 | } |
5511 | ||
9e15db66 AS |
5512 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
5513 | struct bpf_reg_state *regs, | |
5514 | int regno, int off, int size, | |
5515 | enum bpf_access_type atype, | |
5516 | int value_regno) | |
5517 | { | |
5518 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
5519 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
5520 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
63260df1 | 5521 | const char *field_name = NULL; |
c6f1bfe8 | 5522 | enum bpf_type_flag flag = 0; |
b7e852a9 | 5523 | u32 btf_id = 0; |
9e15db66 AS |
5524 | int ret; |
5525 | ||
c67cae55 AS |
5526 | if (!env->allow_ptr_leaks) { |
5527 | verbose(env, | |
5528 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
5529 | tname); | |
5530 | return -EPERM; | |
5531 | } | |
5532 | if (!env->prog->gpl_compatible && btf_is_kernel(reg->btf)) { | |
5533 | verbose(env, | |
5534 | "Cannot access kernel 'struct %s' from non-GPL compatible program\n", | |
5535 | tname); | |
5536 | return -EINVAL; | |
5537 | } | |
9e15db66 AS |
5538 | if (off < 0) { |
5539 | verbose(env, | |
5540 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
5541 | regno, tname, off); | |
5542 | return -EACCES; | |
5543 | } | |
5544 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5545 | char tn_buf[48]; | |
5546 | ||
5547 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5548 | verbose(env, | |
5549 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
5550 | regno, tname, off, tn_buf); | |
5551 | return -EACCES; | |
5552 | } | |
5553 | ||
c6f1bfe8 YS |
5554 | if (reg->type & MEM_USER) { |
5555 | verbose(env, | |
5556 | "R%d is ptr_%s access user memory: off=%d\n", | |
5557 | regno, tname, off); | |
5558 | return -EACCES; | |
5559 | } | |
5560 | ||
5844101a HL |
5561 | if (reg->type & MEM_PERCPU) { |
5562 | verbose(env, | |
5563 | "R%d is ptr_%s access percpu memory: off=%d\n", | |
5564 | regno, tname, off); | |
5565 | return -EACCES; | |
5566 | } | |
5567 | ||
7d64c513 | 5568 | if (env->ops->btf_struct_access && !type_is_alloc(reg->type) && atype == BPF_WRITE) { |
282de143 KKD |
5569 | if (!btf_is_kernel(reg->btf)) { |
5570 | verbose(env, "verifier internal error: reg->btf must be kernel btf\n"); | |
5571 | return -EFAULT; | |
5572 | } | |
b7e852a9 | 5573 | ret = env->ops->btf_struct_access(&env->log, reg, off, size); |
27ae7997 | 5574 | } else { |
282de143 KKD |
5575 | /* Writes are permitted with default btf_struct_access for |
5576 | * program allocated objects (which always have ref_obj_id > 0), | |
5577 | * but not for untrusted PTR_TO_BTF_ID | MEM_ALLOC. | |
5578 | */ | |
5579 | if (atype != BPF_READ && reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
27ae7997 MKL |
5580 | verbose(env, "only read is supported\n"); |
5581 | return -EACCES; | |
5582 | } | |
5583 | ||
6a3cd331 DM |
5584 | if (type_is_alloc(reg->type) && !type_is_non_owning_ref(reg->type) && |
5585 | !reg->ref_obj_id) { | |
282de143 KKD |
5586 | verbose(env, "verifier internal error: ref_obj_id for allocated object must be non-zero\n"); |
5587 | return -EFAULT; | |
5588 | } | |
5589 | ||
63260df1 | 5590 | ret = btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag, &field_name); |
27ae7997 MKL |
5591 | } |
5592 | ||
9e15db66 AS |
5593 | if (ret < 0) |
5594 | return ret; | |
5595 | ||
6fcd486b AS |
5596 | if (ret != PTR_TO_BTF_ID) { |
5597 | /* just mark; */ | |
6efe152d | 5598 | |
6fcd486b AS |
5599 | } else if (type_flag(reg->type) & PTR_UNTRUSTED) { |
5600 | /* If this is an untrusted pointer, all pointers formed by walking it | |
5601 | * also inherit the untrusted flag. | |
5602 | */ | |
5603 | flag = PTR_UNTRUSTED; | |
5604 | ||
5605 | } else if (is_trusted_reg(reg) || is_rcu_reg(reg)) { | |
5606 | /* By default any pointer obtained from walking a trusted pointer is no | |
5607 | * longer trusted, unless the field being accessed has explicitly been | |
5608 | * marked as inheriting its parent's state of trust (either full or RCU). | |
5609 | * For example: | |
5610 | * 'cgroups' pointer is untrusted if task->cgroups dereference | |
5611 | * happened in a sleepable program outside of bpf_rcu_read_lock() | |
5612 | * section. In a non-sleepable program it's trusted while in RCU CS (aka MEM_RCU). | |
5613 | * Note bpf_rcu_read_unlock() converts MEM_RCU pointers to PTR_UNTRUSTED. | |
5614 | * | |
5615 | * A regular RCU-protected pointer with __rcu tag can also be deemed | |
5616 | * trusted if we are in an RCU CS. Such pointer can be NULL. | |
20c09d92 | 5617 | */ |
63260df1 | 5618 | if (type_is_trusted(env, reg, field_name, btf_id)) { |
6fcd486b AS |
5619 | flag |= PTR_TRUSTED; |
5620 | } else if (in_rcu_cs(env) && !type_may_be_null(reg->type)) { | |
63260df1 | 5621 | if (type_is_rcu(env, reg, field_name, btf_id)) { |
6fcd486b AS |
5622 | /* ignore __rcu tag and mark it MEM_RCU */ |
5623 | flag |= MEM_RCU; | |
30ee9821 AS |
5624 | } else if (flag & MEM_RCU || |
5625 | type_is_rcu_or_null(env, reg, field_name, btf_id)) { | |
6fcd486b | 5626 | /* __rcu tagged pointers can be NULL */ |
30ee9821 | 5627 | flag |= MEM_RCU | PTR_MAYBE_NULL; |
6fcd486b AS |
5628 | } else if (flag & (MEM_PERCPU | MEM_USER)) { |
5629 | /* keep as-is */ | |
5630 | } else { | |
afeebf9f AS |
5631 | /* walking unknown pointers yields old deprecated PTR_TO_BTF_ID */ |
5632 | clear_trusted_flags(&flag); | |
6fcd486b AS |
5633 | } |
5634 | } else { | |
5635 | /* | |
5636 | * If not in RCU CS or MEM_RCU pointer can be NULL then | |
5637 | * aggressively mark as untrusted otherwise such | |
5638 | * pointers will be plain PTR_TO_BTF_ID without flags | |
5639 | * and will be allowed to be passed into helpers for | |
5640 | * compat reasons. | |
5641 | */ | |
5642 | flag = PTR_UNTRUSTED; | |
5643 | } | |
20c09d92 | 5644 | } else { |
6fcd486b | 5645 | /* Old compat. Deprecated */ |
afeebf9f | 5646 | clear_trusted_flags(&flag); |
20c09d92 | 5647 | } |
3f00c523 | 5648 | |
41c48f3a | 5649 | if (atype == BPF_READ && value_regno >= 0) |
c6f1bfe8 | 5650 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); |
41c48f3a AI |
5651 | |
5652 | return 0; | |
5653 | } | |
5654 | ||
5655 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
5656 | struct bpf_reg_state *regs, | |
5657 | int regno, int off, int size, | |
5658 | enum bpf_access_type atype, | |
5659 | int value_regno) | |
5660 | { | |
5661 | struct bpf_reg_state *reg = regs + regno; | |
5662 | struct bpf_map *map = reg->map_ptr; | |
6728aea7 | 5663 | struct bpf_reg_state map_reg; |
c6f1bfe8 | 5664 | enum bpf_type_flag flag = 0; |
41c48f3a AI |
5665 | const struct btf_type *t; |
5666 | const char *tname; | |
5667 | u32 btf_id; | |
5668 | int ret; | |
5669 | ||
5670 | if (!btf_vmlinux) { | |
5671 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
5672 | return -ENOTSUPP; | |
5673 | } | |
5674 | ||
5675 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
5676 | verbose(env, "map_ptr access not supported for map type %d\n", | |
5677 | map->map_type); | |
5678 | return -ENOTSUPP; | |
5679 | } | |
5680 | ||
5681 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
5682 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
5683 | ||
c67cae55 | 5684 | if (!env->allow_ptr_leaks) { |
41c48f3a | 5685 | verbose(env, |
c67cae55 | 5686 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
41c48f3a AI |
5687 | tname); |
5688 | return -EPERM; | |
9e15db66 | 5689 | } |
27ae7997 | 5690 | |
41c48f3a AI |
5691 | if (off < 0) { |
5692 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
5693 | regno, tname, off); | |
5694 | return -EACCES; | |
5695 | } | |
5696 | ||
5697 | if (atype != BPF_READ) { | |
5698 | verbose(env, "only read from %s is supported\n", tname); | |
5699 | return -EACCES; | |
5700 | } | |
5701 | ||
6728aea7 KKD |
5702 | /* Simulate access to a PTR_TO_BTF_ID */ |
5703 | memset(&map_reg, 0, sizeof(map_reg)); | |
5704 | mark_btf_ld_reg(env, &map_reg, 0, PTR_TO_BTF_ID, btf_vmlinux, *map->ops->map_btf_id, 0); | |
63260df1 | 5705 | ret = btf_struct_access(&env->log, &map_reg, off, size, atype, &btf_id, &flag, NULL); |
41c48f3a AI |
5706 | if (ret < 0) |
5707 | return ret; | |
5708 | ||
5709 | if (value_regno >= 0) | |
c6f1bfe8 | 5710 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id, flag); |
41c48f3a | 5711 | |
9e15db66 AS |
5712 | return 0; |
5713 | } | |
5714 | ||
01f810ac AM |
5715 | /* Check that the stack access at the given offset is within bounds. The |
5716 | * maximum valid offset is -1. | |
5717 | * | |
5718 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
5719 | * -state->allocated_stack for reads. | |
5720 | */ | |
5721 | static int check_stack_slot_within_bounds(int off, | |
5722 | struct bpf_func_state *state, | |
5723 | enum bpf_access_type t) | |
5724 | { | |
5725 | int min_valid_off; | |
5726 | ||
5727 | if (t == BPF_WRITE) | |
5728 | min_valid_off = -MAX_BPF_STACK; | |
5729 | else | |
5730 | min_valid_off = -state->allocated_stack; | |
5731 | ||
5732 | if (off < min_valid_off || off > -1) | |
5733 | return -EACCES; | |
5734 | return 0; | |
5735 | } | |
5736 | ||
5737 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
5738 | * bounds. | |
5739 | * | |
5740 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
5741 | */ | |
5742 | static int check_stack_access_within_bounds( | |
5743 | struct bpf_verifier_env *env, | |
5744 | int regno, int off, int access_size, | |
61df10c7 | 5745 | enum bpf_access_src src, enum bpf_access_type type) |
01f810ac AM |
5746 | { |
5747 | struct bpf_reg_state *regs = cur_regs(env); | |
5748 | struct bpf_reg_state *reg = regs + regno; | |
5749 | struct bpf_func_state *state = func(env, reg); | |
5750 | int min_off, max_off; | |
5751 | int err; | |
5752 | char *err_extra; | |
5753 | ||
5754 | if (src == ACCESS_HELPER) | |
5755 | /* We don't know if helpers are reading or writing (or both). */ | |
5756 | err_extra = " indirect access to"; | |
5757 | else if (type == BPF_READ) | |
5758 | err_extra = " read from"; | |
5759 | else | |
5760 | err_extra = " write to"; | |
5761 | ||
5762 | if (tnum_is_const(reg->var_off)) { | |
5763 | min_off = reg->var_off.value + off; | |
5764 | if (access_size > 0) | |
5765 | max_off = min_off + access_size - 1; | |
5766 | else | |
5767 | max_off = min_off; | |
5768 | } else { | |
5769 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
5770 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
5771 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
5772 | err_extra, regno); | |
5773 | return -EACCES; | |
5774 | } | |
5775 | min_off = reg->smin_value + off; | |
5776 | if (access_size > 0) | |
5777 | max_off = reg->smax_value + off + access_size - 1; | |
5778 | else | |
5779 | max_off = min_off; | |
5780 | } | |
5781 | ||
5782 | err = check_stack_slot_within_bounds(min_off, state, type); | |
5783 | if (!err) | |
5784 | err = check_stack_slot_within_bounds(max_off, state, type); | |
5785 | ||
5786 | if (err) { | |
5787 | if (tnum_is_const(reg->var_off)) { | |
5788 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
5789 | err_extra, regno, off, access_size); | |
5790 | } else { | |
5791 | char tn_buf[48]; | |
5792 | ||
5793 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
5794 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
5795 | err_extra, regno, tn_buf, access_size); | |
5796 | } | |
5797 | } | |
5798 | return err; | |
5799 | } | |
41c48f3a | 5800 | |
17a52670 AS |
5801 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
5802 | * if t==write, value_regno is a register which value is stored into memory | |
5803 | * if t==read, value_regno is a register which will receive the value from memory | |
5804 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
5805 | * if t==read && value_regno==-1, don't care what we read from memory | |
5806 | */ | |
ca369602 DB |
5807 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
5808 | int off, int bpf_size, enum bpf_access_type t, | |
5809 | int value_regno, bool strict_alignment_once) | |
17a52670 | 5810 | { |
638f5b90 AS |
5811 | struct bpf_reg_state *regs = cur_regs(env); |
5812 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 5813 | struct bpf_func_state *state; |
17a52670 AS |
5814 | int size, err = 0; |
5815 | ||
5816 | size = bpf_size_to_bytes(bpf_size); | |
5817 | if (size < 0) | |
5818 | return size; | |
5819 | ||
f1174f77 | 5820 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 5821 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
5822 | if (err) |
5823 | return err; | |
17a52670 | 5824 | |
f1174f77 EC |
5825 | /* for access checks, reg->off is just part of off */ |
5826 | off += reg->off; | |
5827 | ||
69c087ba YS |
5828 | if (reg->type == PTR_TO_MAP_KEY) { |
5829 | if (t == BPF_WRITE) { | |
5830 | verbose(env, "write to change key R%d not allowed\n", regno); | |
5831 | return -EACCES; | |
5832 | } | |
5833 | ||
5834 | err = check_mem_region_access(env, regno, off, size, | |
5835 | reg->map_ptr->key_size, false); | |
5836 | if (err) | |
5837 | return err; | |
5838 | if (value_regno >= 0) | |
5839 | mark_reg_unknown(env, regs, value_regno); | |
5840 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
aa3496ac | 5841 | struct btf_field *kptr_field = NULL; |
61df10c7 | 5842 | |
1be7f75d AS |
5843 | if (t == BPF_WRITE && value_regno >= 0 && |
5844 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5845 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
5846 | return -EACCES; |
5847 | } | |
591fe988 DB |
5848 | err = check_map_access_type(env, regno, off, size, t); |
5849 | if (err) | |
5850 | return err; | |
61df10c7 KKD |
5851 | err = check_map_access(env, regno, off, size, false, ACCESS_DIRECT); |
5852 | if (err) | |
5853 | return err; | |
5854 | if (tnum_is_const(reg->var_off)) | |
aa3496ac KKD |
5855 | kptr_field = btf_record_find(reg->map_ptr->record, |
5856 | off + reg->var_off.value, BPF_KPTR); | |
5857 | if (kptr_field) { | |
5858 | err = check_map_kptr_access(env, regno, value_regno, insn_idx, kptr_field); | |
61df10c7 | 5859 | } else if (t == BPF_READ && value_regno >= 0) { |
a23740ec AN |
5860 | struct bpf_map *map = reg->map_ptr; |
5861 | ||
5862 | /* if map is read-only, track its contents as scalars */ | |
5863 | if (tnum_is_const(reg->var_off) && | |
5864 | bpf_map_is_rdonly(map) && | |
5865 | map->ops->map_direct_value_addr) { | |
5866 | int map_off = off + reg->var_off.value; | |
5867 | u64 val = 0; | |
5868 | ||
5869 | err = bpf_map_direct_read(map, map_off, size, | |
5870 | &val); | |
5871 | if (err) | |
5872 | return err; | |
5873 | ||
5874 | regs[value_regno].type = SCALAR_VALUE; | |
5875 | __mark_reg_known(®s[value_regno], val); | |
5876 | } else { | |
5877 | mark_reg_unknown(env, regs, value_regno); | |
5878 | } | |
5879 | } | |
34d3a78c HL |
5880 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
5881 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
5882 | ||
5883 | if (type_may_be_null(reg->type)) { | |
5884 | verbose(env, "R%d invalid mem access '%s'\n", regno, | |
5885 | reg_type_str(env, reg->type)); | |
5886 | return -EACCES; | |
5887 | } | |
5888 | ||
5889 | if (t == BPF_WRITE && rdonly_mem) { | |
5890 | verbose(env, "R%d cannot write into %s\n", | |
5891 | regno, reg_type_str(env, reg->type)); | |
5892 | return -EACCES; | |
5893 | } | |
5894 | ||
457f4436 AN |
5895 | if (t == BPF_WRITE && value_regno >= 0 && |
5896 | is_pointer_value(env, value_regno)) { | |
5897 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
5898 | return -EACCES; | |
5899 | } | |
34d3a78c | 5900 | |
457f4436 AN |
5901 | err = check_mem_region_access(env, regno, off, size, |
5902 | reg->mem_size, false); | |
34d3a78c | 5903 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
457f4436 | 5904 | mark_reg_unknown(env, regs, value_regno); |
1a0dc1ac | 5905 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 5906 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 5907 | struct btf *btf = NULL; |
9e15db66 | 5908 | u32 btf_id = 0; |
19de99f7 | 5909 | |
1be7f75d AS |
5910 | if (t == BPF_WRITE && value_regno >= 0 && |
5911 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5912 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
5913 | return -EACCES; |
5914 | } | |
f1174f77 | 5915 | |
be80a1d3 | 5916 | err = check_ptr_off_reg(env, reg, regno); |
58990d1f DB |
5917 | if (err < 0) |
5918 | return err; | |
5919 | ||
c6f1bfe8 YS |
5920 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, |
5921 | &btf_id); | |
9e15db66 AS |
5922 | if (err) |
5923 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 5924 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 5925 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
5926 | * PTR_TO_PACKET[_META,_END]. In the latter |
5927 | * case, we know the offset is zero. | |
f1174f77 | 5928 | */ |
46f8bc92 | 5929 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 5930 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 5931 | } else { |
638f5b90 | 5932 | mark_reg_known_zero(env, regs, |
61bd5218 | 5933 | value_regno); |
c25b2ae1 | 5934 | if (type_may_be_null(reg_type)) |
46f8bc92 | 5935 | regs[value_regno].id = ++env->id_gen; |
5327ed3d JW |
5936 | /* A load of ctx field could have different |
5937 | * actual load size with the one encoded in the | |
5938 | * insn. When the dst is PTR, it is for sure not | |
5939 | * a sub-register. | |
5940 | */ | |
5941 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
c25b2ae1 | 5942 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 5943 | regs[value_regno].btf = btf; |
9e15db66 | 5944 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 5945 | } |
46f8bc92 | 5946 | } |
638f5b90 | 5947 | regs[value_regno].type = reg_type; |
969bf05e | 5948 | } |
17a52670 | 5949 | |
f1174f77 | 5950 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
5951 | /* Basic bounds checks. */ |
5952 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
5953 | if (err) |
5954 | return err; | |
8726679a | 5955 | |
f4d7e40a AS |
5956 | state = func(env, reg); |
5957 | err = update_stack_depth(env, state, off); | |
5958 | if (err) | |
5959 | return err; | |
8726679a | 5960 | |
01f810ac AM |
5961 | if (t == BPF_READ) |
5962 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 5963 | value_regno); |
01f810ac AM |
5964 | else |
5965 | err = check_stack_write(env, regno, off, size, | |
5966 | value_regno, insn_idx); | |
de8f3a83 | 5967 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 5968 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 5969 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
5970 | return -EACCES; |
5971 | } | |
4acf6c0b BB |
5972 | if (t == BPF_WRITE && value_regno >= 0 && |
5973 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
5974 | verbose(env, "R%d leaks addr into packet\n", |
5975 | value_regno); | |
4acf6c0b BB |
5976 | return -EACCES; |
5977 | } | |
9fd29c08 | 5978 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 5979 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 5980 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
5981 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
5982 | if (t == BPF_WRITE && value_regno >= 0 && | |
5983 | is_pointer_value(env, value_regno)) { | |
5984 | verbose(env, "R%d leaks addr into flow keys\n", | |
5985 | value_regno); | |
5986 | return -EACCES; | |
5987 | } | |
5988 | ||
5989 | err = check_flow_keys_access(env, off, size); | |
5990 | if (!err && t == BPF_READ && value_regno >= 0) | |
5991 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 5992 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 5993 | if (t == BPF_WRITE) { |
46f8bc92 | 5994 | verbose(env, "R%d cannot write into %s\n", |
c25b2ae1 | 5995 | regno, reg_type_str(env, reg->type)); |
c64b7983 JS |
5996 | return -EACCES; |
5997 | } | |
5f456649 | 5998 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
5999 | if (!err && value_regno >= 0) |
6000 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
6001 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
6002 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
6003 | if (!err && t == BPF_READ && value_regno >= 0) | |
6004 | mark_reg_unknown(env, regs, value_regno); | |
bff61f6f HL |
6005 | } else if (base_type(reg->type) == PTR_TO_BTF_ID && |
6006 | !type_may_be_null(reg->type)) { | |
9e15db66 AS |
6007 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
6008 | value_regno); | |
41c48f3a AI |
6009 | } else if (reg->type == CONST_PTR_TO_MAP) { |
6010 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
6011 | value_regno); | |
20b2aff4 HL |
6012 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
6013 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
20b2aff4 HL |
6014 | u32 *max_access; |
6015 | ||
6016 | if (rdonly_mem) { | |
6017 | if (t == BPF_WRITE) { | |
6018 | verbose(env, "R%d cannot write into %s\n", | |
6019 | regno, reg_type_str(env, reg->type)); | |
6020 | return -EACCES; | |
6021 | } | |
20b2aff4 HL |
6022 | max_access = &env->prog->aux->max_rdonly_access; |
6023 | } else { | |
20b2aff4 | 6024 | max_access = &env->prog->aux->max_rdwr_access; |
afbf21dc | 6025 | } |
20b2aff4 | 6026 | |
f6dfbe31 | 6027 | err = check_buffer_access(env, reg, regno, off, size, false, |
44e9a741 | 6028 | max_access); |
20b2aff4 HL |
6029 | |
6030 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) | |
afbf21dc | 6031 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 6032 | } else { |
61bd5218 | 6033 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
c25b2ae1 | 6034 | reg_type_str(env, reg->type)); |
17a52670 AS |
6035 | return -EACCES; |
6036 | } | |
969bf05e | 6037 | |
f1174f77 | 6038 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 6039 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 6040 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 6041 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 6042 | } |
17a52670 AS |
6043 | return err; |
6044 | } | |
6045 | ||
91c960b0 | 6046 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 6047 | { |
5ffa2550 | 6048 | int load_reg; |
17a52670 AS |
6049 | int err; |
6050 | ||
5ca419f2 BJ |
6051 | switch (insn->imm) { |
6052 | case BPF_ADD: | |
6053 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
6054 | case BPF_AND: |
6055 | case BPF_AND | BPF_FETCH: | |
6056 | case BPF_OR: | |
6057 | case BPF_OR | BPF_FETCH: | |
6058 | case BPF_XOR: | |
6059 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
6060 | case BPF_XCHG: |
6061 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
6062 | break; |
6063 | default: | |
91c960b0 BJ |
6064 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
6065 | return -EINVAL; | |
6066 | } | |
6067 | ||
6068 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
6069 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
6070 | return -EINVAL; |
6071 | } | |
6072 | ||
6073 | /* check src1 operand */ | |
dc503a8a | 6074 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6075 | if (err) |
6076 | return err; | |
6077 | ||
6078 | /* check src2 operand */ | |
dc503a8a | 6079 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6080 | if (err) |
6081 | return err; | |
6082 | ||
5ffa2550 BJ |
6083 | if (insn->imm == BPF_CMPXCHG) { |
6084 | /* Check comparison of R0 with memory location */ | |
a82fe085 DB |
6085 | const u32 aux_reg = BPF_REG_0; |
6086 | ||
6087 | err = check_reg_arg(env, aux_reg, SRC_OP); | |
5ffa2550 BJ |
6088 | if (err) |
6089 | return err; | |
a82fe085 DB |
6090 | |
6091 | if (is_pointer_value(env, aux_reg)) { | |
6092 | verbose(env, "R%d leaks addr into mem\n", aux_reg); | |
6093 | return -EACCES; | |
6094 | } | |
5ffa2550 BJ |
6095 | } |
6096 | ||
6bdf6abc | 6097 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 6098 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
6099 | return -EACCES; |
6100 | } | |
6101 | ||
ca369602 | 6102 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 6103 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
6104 | is_flow_key_reg(env, insn->dst_reg) || |
6105 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 6106 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f | 6107 | insn->dst_reg, |
c25b2ae1 | 6108 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
6109 | return -EACCES; |
6110 | } | |
6111 | ||
37086bfd BJ |
6112 | if (insn->imm & BPF_FETCH) { |
6113 | if (insn->imm == BPF_CMPXCHG) | |
6114 | load_reg = BPF_REG_0; | |
6115 | else | |
6116 | load_reg = insn->src_reg; | |
6117 | ||
6118 | /* check and record load of old value */ | |
6119 | err = check_reg_arg(env, load_reg, DST_OP); | |
6120 | if (err) | |
6121 | return err; | |
6122 | } else { | |
6123 | /* This instruction accesses a memory location but doesn't | |
6124 | * actually load it into a register. | |
6125 | */ | |
6126 | load_reg = -1; | |
6127 | } | |
6128 | ||
7d3baf0a DB |
6129 | /* Check whether we can read the memory, with second call for fetch |
6130 | * case to simulate the register fill. | |
6131 | */ | |
31fd8581 | 6132 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
7d3baf0a DB |
6133 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
6134 | if (!err && load_reg >= 0) | |
6135 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, | |
6136 | BPF_SIZE(insn->code), BPF_READ, load_reg, | |
6137 | true); | |
17a52670 AS |
6138 | if (err) |
6139 | return err; | |
6140 | ||
7d3baf0a | 6141 | /* Check whether we can write into the same memory. */ |
5ca419f2 BJ |
6142 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
6143 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
6144 | if (err) | |
6145 | return err; | |
6146 | ||
5ca419f2 | 6147 | return 0; |
17a52670 AS |
6148 | } |
6149 | ||
01f810ac AM |
6150 | /* When register 'regno' is used to read the stack (either directly or through |
6151 | * a helper function) make sure that it's within stack boundary and, depending | |
6152 | * on the access type, that all elements of the stack are initialized. | |
6153 | * | |
6154 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
6155 | * | |
6156 | * All registers that have been spilled on the stack in the slots within the | |
6157 | * read offsets are marked as read. | |
6158 | */ | |
6159 | static int check_stack_range_initialized( | |
6160 | struct bpf_verifier_env *env, int regno, int off, | |
6161 | int access_size, bool zero_size_allowed, | |
61df10c7 | 6162 | enum bpf_access_src type, struct bpf_call_arg_meta *meta) |
2011fccf AI |
6163 | { |
6164 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
6165 | struct bpf_func_state *state = func(env, reg); |
6166 | int err, min_off, max_off, i, j, slot, spi; | |
6167 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
6168 | enum bpf_access_type bounds_check_type; | |
6169 | /* Some accesses can write anything into the stack, others are | |
6170 | * read-only. | |
6171 | */ | |
6172 | bool clobber = false; | |
2011fccf | 6173 | |
01f810ac AM |
6174 | if (access_size == 0 && !zero_size_allowed) { |
6175 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
6176 | return -EACCES; |
6177 | } | |
2011fccf | 6178 | |
01f810ac AM |
6179 | if (type == ACCESS_HELPER) { |
6180 | /* The bounds checks for writes are more permissive than for | |
6181 | * reads. However, if raw_mode is not set, we'll do extra | |
6182 | * checks below. | |
6183 | */ | |
6184 | bounds_check_type = BPF_WRITE; | |
6185 | clobber = true; | |
6186 | } else { | |
6187 | bounds_check_type = BPF_READ; | |
6188 | } | |
6189 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
6190 | type, bounds_check_type); | |
6191 | if (err) | |
6192 | return err; | |
6193 | ||
17a52670 | 6194 | |
2011fccf | 6195 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 6196 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 6197 | } else { |
088ec26d AI |
6198 | /* Variable offset is prohibited for unprivileged mode for |
6199 | * simplicity since it requires corresponding support in | |
6200 | * Spectre masking for stack ALU. | |
6201 | * See also retrieve_ptr_limit(). | |
6202 | */ | |
2c78ee89 | 6203 | if (!env->bypass_spec_v1) { |
088ec26d | 6204 | char tn_buf[48]; |
f1174f77 | 6205 | |
088ec26d | 6206 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
6207 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
6208 | regno, err_extra, tn_buf); | |
088ec26d AI |
6209 | return -EACCES; |
6210 | } | |
f2bcd05e AI |
6211 | /* Only initialized buffer on stack is allowed to be accessed |
6212 | * with variable offset. With uninitialized buffer it's hard to | |
6213 | * guarantee that whole memory is marked as initialized on | |
6214 | * helper return since specific bounds are unknown what may | |
6215 | * cause uninitialized stack leaking. | |
6216 | */ | |
6217 | if (meta && meta->raw_mode) | |
6218 | meta = NULL; | |
6219 | ||
01f810ac AM |
6220 | min_off = reg->smin_value + off; |
6221 | max_off = reg->smax_value + off; | |
17a52670 AS |
6222 | } |
6223 | ||
435faee1 | 6224 | if (meta && meta->raw_mode) { |
ef8fc7a0 KKD |
6225 | /* Ensure we won't be overwriting dynptrs when simulating byte |
6226 | * by byte access in check_helper_call using meta.access_size. | |
6227 | * This would be a problem if we have a helper in the future | |
6228 | * which takes: | |
6229 | * | |
6230 | * helper(uninit_mem, len, dynptr) | |
6231 | * | |
6232 | * Now, uninint_mem may overlap with dynptr pointer. Hence, it | |
6233 | * may end up writing to dynptr itself when touching memory from | |
6234 | * arg 1. This can be relaxed on a case by case basis for known | |
6235 | * safe cases, but reject due to the possibilitiy of aliasing by | |
6236 | * default. | |
6237 | */ | |
6238 | for (i = min_off; i < max_off + access_size; i++) { | |
6239 | int stack_off = -i - 1; | |
6240 | ||
6241 | spi = __get_spi(i); | |
6242 | /* raw_mode may write past allocated_stack */ | |
6243 | if (state->allocated_stack <= stack_off) | |
6244 | continue; | |
6245 | if (state->stack[spi].slot_type[stack_off % BPF_REG_SIZE] == STACK_DYNPTR) { | |
6246 | verbose(env, "potential write to dynptr at off=%d disallowed\n", i); | |
6247 | return -EACCES; | |
6248 | } | |
6249 | } | |
435faee1 DB |
6250 | meta->access_size = access_size; |
6251 | meta->regno = regno; | |
6252 | return 0; | |
6253 | } | |
6254 | ||
2011fccf | 6255 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
6256 | u8 *stype; |
6257 | ||
2011fccf | 6258 | slot = -i - 1; |
638f5b90 | 6259 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
6260 | if (state->allocated_stack <= slot) |
6261 | goto err; | |
6262 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
6263 | if (*stype == STACK_MISC) | |
6264 | goto mark; | |
6715df8d EZ |
6265 | if ((*stype == STACK_ZERO) || |
6266 | (*stype == STACK_INVALID && env->allow_uninit_stack)) { | |
01f810ac AM |
6267 | if (clobber) { |
6268 | /* helper can write anything into the stack */ | |
6269 | *stype = STACK_MISC; | |
6270 | } | |
cc2b14d5 | 6271 | goto mark; |
17a52670 | 6272 | } |
1d68f22b | 6273 | |
27113c59 | 6274 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
6275 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
6276 | env->allow_ptr_leaks)) { | |
01f810ac AM |
6277 | if (clobber) { |
6278 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
6279 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 6280 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 6281 | } |
f7cf25b2 AS |
6282 | goto mark; |
6283 | } | |
6284 | ||
cc2b14d5 | 6285 | err: |
2011fccf | 6286 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
6287 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
6288 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
6289 | } else { |
6290 | char tn_buf[48]; | |
6291 | ||
6292 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
6293 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
6294 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 6295 | } |
cc2b14d5 AS |
6296 | return -EACCES; |
6297 | mark: | |
6298 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
6299 | * the whole slot to be marked as 'read' | |
6300 | */ | |
679c782d | 6301 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
6302 | state->stack[spi].spilled_ptr.parent, |
6303 | REG_LIVE_READ64); | |
261f4664 KKD |
6304 | /* We do not set REG_LIVE_WRITTEN for stack slot, as we can not |
6305 | * be sure that whether stack slot is written to or not. Hence, | |
6306 | * we must still conservatively propagate reads upwards even if | |
6307 | * helper may write to the entire memory range. | |
6308 | */ | |
17a52670 | 6309 | } |
2011fccf | 6310 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
6311 | } |
6312 | ||
06c1c049 GB |
6313 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
6314 | int access_size, bool zero_size_allowed, | |
6315 | struct bpf_call_arg_meta *meta) | |
6316 | { | |
638f5b90 | 6317 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
20b2aff4 | 6318 | u32 *max_access; |
06c1c049 | 6319 | |
20b2aff4 | 6320 | switch (base_type(reg->type)) { |
06c1c049 | 6321 | case PTR_TO_PACKET: |
de8f3a83 | 6322 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
6323 | return check_packet_access(env, regno, reg->off, access_size, |
6324 | zero_size_allowed); | |
69c087ba | 6325 | case PTR_TO_MAP_KEY: |
7b3552d3 KKD |
6326 | if (meta && meta->raw_mode) { |
6327 | verbose(env, "R%d cannot write into %s\n", regno, | |
6328 | reg_type_str(env, reg->type)); | |
6329 | return -EACCES; | |
6330 | } | |
69c087ba YS |
6331 | return check_mem_region_access(env, regno, reg->off, access_size, |
6332 | reg->map_ptr->key_size, false); | |
06c1c049 | 6333 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
6334 | if (check_map_access_type(env, regno, reg->off, access_size, |
6335 | meta && meta->raw_mode ? BPF_WRITE : | |
6336 | BPF_READ)) | |
6337 | return -EACCES; | |
9fd29c08 | 6338 | return check_map_access(env, regno, reg->off, access_size, |
61df10c7 | 6339 | zero_size_allowed, ACCESS_HELPER); |
457f4436 | 6340 | case PTR_TO_MEM: |
97e6d7da KKD |
6341 | if (type_is_rdonly_mem(reg->type)) { |
6342 | if (meta && meta->raw_mode) { | |
6343 | verbose(env, "R%d cannot write into %s\n", regno, | |
6344 | reg_type_str(env, reg->type)); | |
6345 | return -EACCES; | |
6346 | } | |
6347 | } | |
457f4436 AN |
6348 | return check_mem_region_access(env, regno, reg->off, |
6349 | access_size, reg->mem_size, | |
6350 | zero_size_allowed); | |
20b2aff4 HL |
6351 | case PTR_TO_BUF: |
6352 | if (type_is_rdonly_mem(reg->type)) { | |
97e6d7da KKD |
6353 | if (meta && meta->raw_mode) { |
6354 | verbose(env, "R%d cannot write into %s\n", regno, | |
6355 | reg_type_str(env, reg->type)); | |
20b2aff4 | 6356 | return -EACCES; |
97e6d7da | 6357 | } |
20b2aff4 | 6358 | |
20b2aff4 HL |
6359 | max_access = &env->prog->aux->max_rdonly_access; |
6360 | } else { | |
20b2aff4 HL |
6361 | max_access = &env->prog->aux->max_rdwr_access; |
6362 | } | |
afbf21dc YS |
6363 | return check_buffer_access(env, reg, regno, reg->off, |
6364 | access_size, zero_size_allowed, | |
44e9a741 | 6365 | max_access); |
0d004c02 | 6366 | case PTR_TO_STACK: |
01f810ac AM |
6367 | return check_stack_range_initialized( |
6368 | env, | |
6369 | regno, reg->off, access_size, | |
6370 | zero_size_allowed, ACCESS_HELPER, meta); | |
3e30be42 AS |
6371 | case PTR_TO_BTF_ID: |
6372 | return check_ptr_to_btf_access(env, regs, regno, reg->off, | |
6373 | access_size, BPF_READ, -1); | |
15baa55f BT |
6374 | case PTR_TO_CTX: |
6375 | /* in case the function doesn't know how to access the context, | |
6376 | * (because we are in a program of type SYSCALL for example), we | |
6377 | * can not statically check its size. | |
6378 | * Dynamically check it now. | |
6379 | */ | |
6380 | if (!env->ops->convert_ctx_access) { | |
6381 | enum bpf_access_type atype = meta && meta->raw_mode ? BPF_WRITE : BPF_READ; | |
6382 | int offset = access_size - 1; | |
6383 | ||
6384 | /* Allow zero-byte read from PTR_TO_CTX */ | |
6385 | if (access_size == 0) | |
6386 | return zero_size_allowed ? 0 : -EACCES; | |
6387 | ||
6388 | return check_mem_access(env, env->insn_idx, regno, offset, BPF_B, | |
6389 | atype, -1, false); | |
6390 | } | |
6391 | ||
6392 | fallthrough; | |
0d004c02 LB |
6393 | default: /* scalar_value or invalid ptr */ |
6394 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
6395 | if (zero_size_allowed && access_size == 0 && | |
6396 | register_is_null(reg)) | |
6397 | return 0; | |
6398 | ||
c25b2ae1 HL |
6399 | verbose(env, "R%d type=%s ", regno, |
6400 | reg_type_str(env, reg->type)); | |
6401 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); | |
0d004c02 | 6402 | return -EACCES; |
06c1c049 GB |
6403 | } |
6404 | } | |
6405 | ||
d583691c KKD |
6406 | static int check_mem_size_reg(struct bpf_verifier_env *env, |
6407 | struct bpf_reg_state *reg, u32 regno, | |
6408 | bool zero_size_allowed, | |
6409 | struct bpf_call_arg_meta *meta) | |
6410 | { | |
6411 | int err; | |
6412 | ||
6413 | /* This is used to refine r0 return value bounds for helpers | |
6414 | * that enforce this value as an upper bound on return values. | |
6415 | * See do_refine_retval_range() for helpers that can refine | |
6416 | * the return value. C type of helper is u32 so we pull register | |
6417 | * bound from umax_value however, if negative verifier errors | |
6418 | * out. Only upper bounds can be learned because retval is an | |
6419 | * int type and negative retvals are allowed. | |
6420 | */ | |
be77354a | 6421 | meta->msize_max_value = reg->umax_value; |
d583691c KKD |
6422 | |
6423 | /* The register is SCALAR_VALUE; the access check | |
6424 | * happens using its boundaries. | |
6425 | */ | |
6426 | if (!tnum_is_const(reg->var_off)) | |
6427 | /* For unprivileged variable accesses, disable raw | |
6428 | * mode so that the program is required to | |
6429 | * initialize all the memory that the helper could | |
6430 | * just partially fill up. | |
6431 | */ | |
6432 | meta = NULL; | |
6433 | ||
6434 | if (reg->smin_value < 0) { | |
6435 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", | |
6436 | regno); | |
6437 | return -EACCES; | |
6438 | } | |
6439 | ||
6440 | if (reg->umin_value == 0) { | |
6441 | err = check_helper_mem_access(env, regno - 1, 0, | |
6442 | zero_size_allowed, | |
6443 | meta); | |
6444 | if (err) | |
6445 | return err; | |
6446 | } | |
6447 | ||
6448 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { | |
6449 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
6450 | regno); | |
6451 | return -EACCES; | |
6452 | } | |
6453 | err = check_helper_mem_access(env, regno - 1, | |
6454 | reg->umax_value, | |
6455 | zero_size_allowed, meta); | |
6456 | if (!err) | |
6457 | err = mark_chain_precision(env, regno); | |
6458 | return err; | |
6459 | } | |
6460 | ||
e5069b9c DB |
6461 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
6462 | u32 regno, u32 mem_size) | |
6463 | { | |
be77354a KKD |
6464 | bool may_be_null = type_may_be_null(reg->type); |
6465 | struct bpf_reg_state saved_reg; | |
6466 | struct bpf_call_arg_meta meta; | |
6467 | int err; | |
6468 | ||
e5069b9c DB |
6469 | if (register_is_null(reg)) |
6470 | return 0; | |
6471 | ||
be77354a KKD |
6472 | memset(&meta, 0, sizeof(meta)); |
6473 | /* Assuming that the register contains a value check if the memory | |
6474 | * access is safe. Temporarily save and restore the register's state as | |
6475 | * the conversion shouldn't be visible to a caller. | |
6476 | */ | |
6477 | if (may_be_null) { | |
6478 | saved_reg = *reg; | |
e5069b9c | 6479 | mark_ptr_not_null_reg(reg); |
e5069b9c DB |
6480 | } |
6481 | ||
be77354a KKD |
6482 | err = check_helper_mem_access(env, regno, mem_size, true, &meta); |
6483 | /* Check access for BPF_WRITE */ | |
6484 | meta.raw_mode = true; | |
6485 | err = err ?: check_helper_mem_access(env, regno, mem_size, true, &meta); | |
6486 | ||
6487 | if (may_be_null) | |
6488 | *reg = saved_reg; | |
6489 | ||
6490 | return err; | |
e5069b9c DB |
6491 | } |
6492 | ||
00b85860 KKD |
6493 | static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
6494 | u32 regno) | |
d583691c KKD |
6495 | { |
6496 | struct bpf_reg_state *mem_reg = &cur_regs(env)[regno - 1]; | |
6497 | bool may_be_null = type_may_be_null(mem_reg->type); | |
6498 | struct bpf_reg_state saved_reg; | |
be77354a | 6499 | struct bpf_call_arg_meta meta; |
d583691c KKD |
6500 | int err; |
6501 | ||
6502 | WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); | |
6503 | ||
be77354a KKD |
6504 | memset(&meta, 0, sizeof(meta)); |
6505 | ||
d583691c KKD |
6506 | if (may_be_null) { |
6507 | saved_reg = *mem_reg; | |
6508 | mark_ptr_not_null_reg(mem_reg); | |
6509 | } | |
6510 | ||
be77354a KKD |
6511 | err = check_mem_size_reg(env, reg, regno, true, &meta); |
6512 | /* Check access for BPF_WRITE */ | |
6513 | meta.raw_mode = true; | |
6514 | err = err ?: check_mem_size_reg(env, reg, regno, true, &meta); | |
d583691c KKD |
6515 | |
6516 | if (may_be_null) | |
6517 | *mem_reg = saved_reg; | |
6518 | return err; | |
6519 | } | |
6520 | ||
d83525ca | 6521 | /* Implementation details: |
4e814da0 KKD |
6522 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL. |
6523 | * bpf_obj_new returns PTR_TO_BTF_ID | MEM_ALLOC | PTR_MAYBE_NULL. | |
d83525ca | 6524 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
4e814da0 KKD |
6525 | * Two separate bpf_obj_new will also have different reg->id. |
6526 | * For traditional PTR_TO_MAP_VALUE or PTR_TO_BTF_ID | MEM_ALLOC, the verifier | |
6527 | * clears reg->id after value_or_null->value transition, since the verifier only | |
6528 | * cares about the range of access to valid map value pointer and doesn't care | |
6529 | * about actual address of the map element. | |
d83525ca AS |
6530 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
6531 | * reg->id > 0 after value_or_null->value transition. By doing so | |
6532 | * two bpf_map_lookups will be considered two different pointers that | |
4e814da0 KKD |
6533 | * point to different bpf_spin_locks. Likewise for pointers to allocated objects |
6534 | * returned from bpf_obj_new. | |
d83525ca AS |
6535 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
6536 | * dead-locks. | |
6537 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
6538 | * reg_is_refcounted() logic. The verifier needs to remember only | |
6539 | * one spin_lock instead of array of acquired_refs. | |
d0d78c1d | 6540 | * cur_state->active_lock remembers which map value element or allocated |
4e814da0 | 6541 | * object got locked and clears it after bpf_spin_unlock. |
d83525ca AS |
6542 | */ |
6543 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
6544 | bool is_lock) | |
6545 | { | |
6546 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6547 | struct bpf_verifier_state *cur = env->cur_state; | |
6548 | bool is_const = tnum_is_const(reg->var_off); | |
d83525ca | 6549 | u64 val = reg->var_off.value; |
4e814da0 KKD |
6550 | struct bpf_map *map = NULL; |
6551 | struct btf *btf = NULL; | |
6552 | struct btf_record *rec; | |
d83525ca | 6553 | |
d83525ca AS |
6554 | if (!is_const) { |
6555 | verbose(env, | |
6556 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
6557 | regno); | |
6558 | return -EINVAL; | |
6559 | } | |
4e814da0 KKD |
6560 | if (reg->type == PTR_TO_MAP_VALUE) { |
6561 | map = reg->map_ptr; | |
6562 | if (!map->btf) { | |
6563 | verbose(env, | |
6564 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
6565 | map->name); | |
6566 | return -EINVAL; | |
6567 | } | |
6568 | } else { | |
6569 | btf = reg->btf; | |
d83525ca | 6570 | } |
4e814da0 KKD |
6571 | |
6572 | rec = reg_btf_record(reg); | |
6573 | if (!btf_record_has_field(rec, BPF_SPIN_LOCK)) { | |
6574 | verbose(env, "%s '%s' has no valid bpf_spin_lock\n", map ? "map" : "local", | |
6575 | map ? map->name : "kptr"); | |
d83525ca AS |
6576 | return -EINVAL; |
6577 | } | |
4e814da0 | 6578 | if (rec->spin_lock_off != val + reg->off) { |
db559117 | 6579 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock' that is at %d\n", |
4e814da0 | 6580 | val + reg->off, rec->spin_lock_off); |
d83525ca AS |
6581 | return -EINVAL; |
6582 | } | |
6583 | if (is_lock) { | |
d0d78c1d | 6584 | if (cur->active_lock.ptr) { |
d83525ca AS |
6585 | verbose(env, |
6586 | "Locking two bpf_spin_locks are not allowed\n"); | |
6587 | return -EINVAL; | |
6588 | } | |
d0d78c1d KKD |
6589 | if (map) |
6590 | cur->active_lock.ptr = map; | |
6591 | else | |
6592 | cur->active_lock.ptr = btf; | |
6593 | cur->active_lock.id = reg->id; | |
d83525ca | 6594 | } else { |
d0d78c1d KKD |
6595 | void *ptr; |
6596 | ||
6597 | if (map) | |
6598 | ptr = map; | |
6599 | else | |
6600 | ptr = btf; | |
6601 | ||
6602 | if (!cur->active_lock.ptr) { | |
d83525ca AS |
6603 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
6604 | return -EINVAL; | |
6605 | } | |
d0d78c1d KKD |
6606 | if (cur->active_lock.ptr != ptr || |
6607 | cur->active_lock.id != reg->id) { | |
d83525ca AS |
6608 | verbose(env, "bpf_spin_unlock of different lock\n"); |
6609 | return -EINVAL; | |
6610 | } | |
534e86bc | 6611 | |
6a3cd331 | 6612 | invalidate_non_owning_refs(env); |
534e86bc | 6613 | |
6a3cd331 DM |
6614 | cur->active_lock.ptr = NULL; |
6615 | cur->active_lock.id = 0; | |
d83525ca AS |
6616 | } |
6617 | return 0; | |
6618 | } | |
6619 | ||
b00628b1 AS |
6620 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
6621 | struct bpf_call_arg_meta *meta) | |
6622 | { | |
6623 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6624 | bool is_const = tnum_is_const(reg->var_off); | |
6625 | struct bpf_map *map = reg->map_ptr; | |
6626 | u64 val = reg->var_off.value; | |
6627 | ||
6628 | if (!is_const) { | |
6629 | verbose(env, | |
6630 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
6631 | regno); | |
6632 | return -EINVAL; | |
6633 | } | |
6634 | if (!map->btf) { | |
6635 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
6636 | map->name); | |
6637 | return -EINVAL; | |
6638 | } | |
db559117 KKD |
6639 | if (!btf_record_has_field(map->record, BPF_TIMER)) { |
6640 | verbose(env, "map '%s' has no valid bpf_timer\n", map->name); | |
68134668 AS |
6641 | return -EINVAL; |
6642 | } | |
db559117 | 6643 | if (map->record->timer_off != val + reg->off) { |
68134668 | 6644 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
db559117 | 6645 | val + reg->off, map->record->timer_off); |
b00628b1 AS |
6646 | return -EINVAL; |
6647 | } | |
6648 | if (meta->map_ptr) { | |
6649 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
6650 | return -EFAULT; | |
6651 | } | |
3e8ce298 | 6652 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
6653 | meta->map_ptr = map; |
6654 | return 0; | |
6655 | } | |
6656 | ||
c0a5a21c KKD |
6657 | static int process_kptr_func(struct bpf_verifier_env *env, int regno, |
6658 | struct bpf_call_arg_meta *meta) | |
6659 | { | |
6660 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
c0a5a21c | 6661 | struct bpf_map *map_ptr = reg->map_ptr; |
aa3496ac | 6662 | struct btf_field *kptr_field; |
c0a5a21c | 6663 | u32 kptr_off; |
c0a5a21c KKD |
6664 | |
6665 | if (!tnum_is_const(reg->var_off)) { | |
6666 | verbose(env, | |
6667 | "R%d doesn't have constant offset. kptr has to be at the constant offset\n", | |
6668 | regno); | |
6669 | return -EINVAL; | |
6670 | } | |
6671 | if (!map_ptr->btf) { | |
6672 | verbose(env, "map '%s' has to have BTF in order to use bpf_kptr_xchg\n", | |
6673 | map_ptr->name); | |
6674 | return -EINVAL; | |
6675 | } | |
aa3496ac KKD |
6676 | if (!btf_record_has_field(map_ptr->record, BPF_KPTR)) { |
6677 | verbose(env, "map '%s' has no valid kptr\n", map_ptr->name); | |
c0a5a21c KKD |
6678 | return -EINVAL; |
6679 | } | |
6680 | ||
6681 | meta->map_ptr = map_ptr; | |
6682 | kptr_off = reg->off + reg->var_off.value; | |
aa3496ac KKD |
6683 | kptr_field = btf_record_find(map_ptr->record, kptr_off, BPF_KPTR); |
6684 | if (!kptr_field) { | |
c0a5a21c KKD |
6685 | verbose(env, "off=%d doesn't point to kptr\n", kptr_off); |
6686 | return -EACCES; | |
6687 | } | |
aa3496ac | 6688 | if (kptr_field->type != BPF_KPTR_REF) { |
c0a5a21c KKD |
6689 | verbose(env, "off=%d kptr isn't referenced kptr\n", kptr_off); |
6690 | return -EACCES; | |
6691 | } | |
aa3496ac | 6692 | meta->kptr_field = kptr_field; |
c0a5a21c KKD |
6693 | return 0; |
6694 | } | |
6695 | ||
27060531 KKD |
6696 | /* There are two register types representing a bpf_dynptr, one is PTR_TO_STACK |
6697 | * which points to a stack slot, and the other is CONST_PTR_TO_DYNPTR. | |
6698 | * | |
6699 | * In both cases we deal with the first 8 bytes, but need to mark the next 8 | |
6700 | * bytes as STACK_DYNPTR in case of PTR_TO_STACK. In case of | |
6701 | * CONST_PTR_TO_DYNPTR, we are guaranteed to get the beginning of the object. | |
6702 | * | |
6703 | * Mutability of bpf_dynptr is at two levels, one is at the level of struct | |
6704 | * bpf_dynptr itself, i.e. whether the helper is receiving a pointer to struct | |
6705 | * bpf_dynptr or pointer to const struct bpf_dynptr. In the former case, it can | |
6706 | * mutate the view of the dynptr and also possibly destroy it. In the latter | |
6707 | * case, it cannot mutate the bpf_dynptr itself but it can still mutate the | |
6708 | * memory that dynptr points to. | |
6709 | * | |
6710 | * The verifier will keep track both levels of mutation (bpf_dynptr's in | |
6711 | * reg->type and the memory's in reg->dynptr.type), but there is no support for | |
6712 | * readonly dynptr view yet, hence only the first case is tracked and checked. | |
6713 | * | |
6714 | * This is consistent with how C applies the const modifier to a struct object, | |
6715 | * where the pointer itself inside bpf_dynptr becomes const but not what it | |
6716 | * points to. | |
6717 | * | |
6718 | * Helpers which do not mutate the bpf_dynptr set MEM_RDONLY in their argument | |
6719 | * type, and declare it as 'const struct bpf_dynptr *' in their prototype. | |
6720 | */ | |
1d18feb2 | 6721 | static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx, |
361f129f | 6722 | enum bpf_arg_type arg_type, int clone_ref_obj_id) |
6b75bd3d KKD |
6723 | { |
6724 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
1d18feb2 | 6725 | int err; |
6b75bd3d | 6726 | |
27060531 KKD |
6727 | /* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an |
6728 | * ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*): | |
6729 | */ | |
6730 | if ((arg_type & (MEM_UNINIT | MEM_RDONLY)) == (MEM_UNINIT | MEM_RDONLY)) { | |
6731 | verbose(env, "verifier internal error: misconfigured dynptr helper type flags\n"); | |
6732 | return -EFAULT; | |
6733 | } | |
79168a66 | 6734 | |
27060531 KKD |
6735 | /* MEM_UNINIT - Points to memory that is an appropriate candidate for |
6736 | * constructing a mutable bpf_dynptr object. | |
6737 | * | |
6738 | * Currently, this is only possible with PTR_TO_STACK | |
6739 | * pointing to a region of at least 16 bytes which doesn't | |
6740 | * contain an existing bpf_dynptr. | |
6741 | * | |
6742 | * MEM_RDONLY - Points to a initialized bpf_dynptr that will not be | |
6743 | * mutated or destroyed. However, the memory it points to | |
6744 | * may be mutated. | |
6745 | * | |
6746 | * None - Points to a initialized dynptr that can be mutated and | |
6747 | * destroyed, including mutation of the memory it points | |
6748 | * to. | |
6b75bd3d | 6749 | */ |
6b75bd3d | 6750 | if (arg_type & MEM_UNINIT) { |
1d18feb2 JK |
6751 | int i; |
6752 | ||
7e0dac28 | 6753 | if (!is_dynptr_reg_valid_uninit(env, reg)) { |
6b75bd3d KKD |
6754 | verbose(env, "Dynptr has to be an uninitialized dynptr\n"); |
6755 | return -EINVAL; | |
6756 | } | |
6757 | ||
1d18feb2 JK |
6758 | /* we write BPF_DW bits (8 bytes) at a time */ |
6759 | for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) { | |
6760 | err = check_mem_access(env, insn_idx, regno, | |
6761 | i, BPF_DW, BPF_WRITE, -1, false); | |
6762 | if (err) | |
6763 | return err; | |
6b75bd3d KKD |
6764 | } |
6765 | ||
361f129f | 6766 | err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx, clone_ref_obj_id); |
27060531 KKD |
6767 | } else /* MEM_RDONLY and None case from above */ { |
6768 | /* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */ | |
6769 | if (reg->type == CONST_PTR_TO_DYNPTR && !(arg_type & MEM_RDONLY)) { | |
6770 | verbose(env, "cannot pass pointer to const bpf_dynptr, the helper mutates it\n"); | |
6771 | return -EINVAL; | |
6772 | } | |
6773 | ||
7e0dac28 | 6774 | if (!is_dynptr_reg_valid_init(env, reg)) { |
6b75bd3d KKD |
6775 | verbose(env, |
6776 | "Expected an initialized dynptr as arg #%d\n", | |
6777 | regno); | |
6778 | return -EINVAL; | |
6779 | } | |
6780 | ||
27060531 KKD |
6781 | /* Fold modifiers (in this case, MEM_RDONLY) when checking expected type */ |
6782 | if (!is_dynptr_type_expected(env, reg, arg_type & ~MEM_RDONLY)) { | |
6b75bd3d KKD |
6783 | verbose(env, |
6784 | "Expected a dynptr of type %s as arg #%d\n", | |
d54e0f6c | 6785 | dynptr_type_str(arg_to_dynptr_type(arg_type)), regno); |
6b75bd3d KKD |
6786 | return -EINVAL; |
6787 | } | |
d6fefa11 KKD |
6788 | |
6789 | err = mark_dynptr_read(env, reg); | |
6b75bd3d | 6790 | } |
1d18feb2 | 6791 | return err; |
6b75bd3d KKD |
6792 | } |
6793 | ||
06accc87 AN |
6794 | static u32 iter_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int spi) |
6795 | { | |
6796 | struct bpf_func_state *state = func(env, reg); | |
6797 | ||
6798 | return state->stack[spi].spilled_ptr.ref_obj_id; | |
6799 | } | |
6800 | ||
6801 | static bool is_iter_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6802 | { | |
6803 | return meta->kfunc_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY); | |
6804 | } | |
6805 | ||
6806 | static bool is_iter_new_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6807 | { | |
6808 | return meta->kfunc_flags & KF_ITER_NEW; | |
6809 | } | |
6810 | ||
6811 | static bool is_iter_next_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6812 | { | |
6813 | return meta->kfunc_flags & KF_ITER_NEXT; | |
6814 | } | |
6815 | ||
6816 | static bool is_iter_destroy_kfunc(struct bpf_kfunc_call_arg_meta *meta) | |
6817 | { | |
6818 | return meta->kfunc_flags & KF_ITER_DESTROY; | |
6819 | } | |
6820 | ||
6821 | static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg) | |
6822 | { | |
6823 | /* btf_check_iter_kfuncs() guarantees that first argument of any iter | |
6824 | * kfunc is iter state pointer | |
6825 | */ | |
6826 | return arg == 0 && is_iter_kfunc(meta); | |
6827 | } | |
6828 | ||
6829 | static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idx, | |
6830 | struct bpf_kfunc_call_arg_meta *meta) | |
6831 | { | |
6832 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6833 | const struct btf_type *t; | |
6834 | const struct btf_param *arg; | |
6835 | int spi, err, i, nr_slots; | |
6836 | u32 btf_id; | |
6837 | ||
6838 | /* btf_check_iter_kfuncs() ensures we don't need to validate anything here */ | |
6839 | arg = &btf_params(meta->func_proto)[0]; | |
6840 | t = btf_type_skip_modifiers(meta->btf, arg->type, NULL); /* PTR */ | |
6841 | t = btf_type_skip_modifiers(meta->btf, t->type, &btf_id); /* STRUCT */ | |
6842 | nr_slots = t->size / BPF_REG_SIZE; | |
6843 | ||
06accc87 AN |
6844 | if (is_iter_new_kfunc(meta)) { |
6845 | /* bpf_iter_<type>_new() expects pointer to uninit iter state */ | |
6846 | if (!is_iter_reg_valid_uninit(env, reg, nr_slots)) { | |
6847 | verbose(env, "expected uninitialized iter_%s as arg #%d\n", | |
6848 | iter_type_str(meta->btf, btf_id), regno); | |
6849 | return -EINVAL; | |
6850 | } | |
6851 | ||
6852 | for (i = 0; i < nr_slots * 8; i += BPF_REG_SIZE) { | |
6853 | err = check_mem_access(env, insn_idx, regno, | |
6854 | i, BPF_DW, BPF_WRITE, -1, false); | |
6855 | if (err) | |
6856 | return err; | |
6857 | } | |
6858 | ||
6859 | err = mark_stack_slots_iter(env, reg, insn_idx, meta->btf, btf_id, nr_slots); | |
6860 | if (err) | |
6861 | return err; | |
6862 | } else { | |
6863 | /* iter_next() or iter_destroy() expect initialized iter state*/ | |
6864 | if (!is_iter_reg_valid_init(env, reg, meta->btf, btf_id, nr_slots)) { | |
6865 | verbose(env, "expected an initialized iter_%s as arg #%d\n", | |
6866 | iter_type_str(meta->btf, btf_id), regno); | |
6867 | return -EINVAL; | |
6868 | } | |
6869 | ||
b63cbc49 AN |
6870 | spi = iter_get_spi(env, reg, nr_slots); |
6871 | if (spi < 0) | |
6872 | return spi; | |
6873 | ||
06accc87 AN |
6874 | err = mark_iter_read(env, reg, spi, nr_slots); |
6875 | if (err) | |
6876 | return err; | |
6877 | ||
b63cbc49 AN |
6878 | /* remember meta->iter info for process_iter_next_call() */ |
6879 | meta->iter.spi = spi; | |
6880 | meta->iter.frameno = reg->frameno; | |
06accc87 AN |
6881 | meta->ref_obj_id = iter_ref_obj_id(env, reg, spi); |
6882 | ||
6883 | if (is_iter_destroy_kfunc(meta)) { | |
6884 | err = unmark_stack_slots_iter(env, reg, nr_slots); | |
6885 | if (err) | |
6886 | return err; | |
6887 | } | |
6888 | } | |
6889 | ||
6890 | return 0; | |
6891 | } | |
6892 | ||
6893 | /* process_iter_next_call() is called when verifier gets to iterator's next | |
6894 | * "method" (e.g., bpf_iter_num_next() for numbers iterator) call. We'll refer | |
6895 | * to it as just "iter_next()" in comments below. | |
6896 | * | |
6897 | * BPF verifier relies on a crucial contract for any iter_next() | |
6898 | * implementation: it should *eventually* return NULL, and once that happens | |
6899 | * it should keep returning NULL. That is, once iterator exhausts elements to | |
6900 | * iterate, it should never reset or spuriously return new elements. | |
6901 | * | |
6902 | * With the assumption of such contract, process_iter_next_call() simulates | |
6903 | * a fork in the verifier state to validate loop logic correctness and safety | |
6904 | * without having to simulate infinite amount of iterations. | |
6905 | * | |
6906 | * In current state, we first assume that iter_next() returned NULL and | |
6907 | * iterator state is set to DRAINED (BPF_ITER_STATE_DRAINED). In such | |
6908 | * conditions we should not form an infinite loop and should eventually reach | |
6909 | * exit. | |
6910 | * | |
6911 | * Besides that, we also fork current state and enqueue it for later | |
6912 | * verification. In a forked state we keep iterator state as ACTIVE | |
6913 | * (BPF_ITER_STATE_ACTIVE) and assume non-NULL return from iter_next(). We | |
6914 | * also bump iteration depth to prevent erroneous infinite loop detection | |
6915 | * later on (see iter_active_depths_differ() comment for details). In this | |
6916 | * state we assume that we'll eventually loop back to another iter_next() | |
6917 | * calls (it could be in exactly same location or in some other instruction, | |
6918 | * it doesn't matter, we don't make any unnecessary assumptions about this, | |
6919 | * everything revolves around iterator state in a stack slot, not which | |
6920 | * instruction is calling iter_next()). When that happens, we either will come | |
6921 | * to iter_next() with equivalent state and can conclude that next iteration | |
6922 | * will proceed in exactly the same way as we just verified, so it's safe to | |
6923 | * assume that loop converges. If not, we'll go on another iteration | |
6924 | * simulation with a different input state, until all possible starting states | |
6925 | * are validated or we reach maximum number of instructions limit. | |
6926 | * | |
6927 | * This way, we will either exhaustively discover all possible input states | |
6928 | * that iterator loop can start with and eventually will converge, or we'll | |
6929 | * effectively regress into bounded loop simulation logic and either reach | |
6930 | * maximum number of instructions if loop is not provably convergent, or there | |
6931 | * is some statically known limit on number of iterations (e.g., if there is | |
6932 | * an explicit `if n > 100 then break;` statement somewhere in the loop). | |
6933 | * | |
6934 | * One very subtle but very important aspect is that we *always* simulate NULL | |
6935 | * condition first (as the current state) before we simulate non-NULL case. | |
6936 | * This has to do with intricacies of scalar precision tracking. By simulating | |
6937 | * "exit condition" of iter_next() returning NULL first, we make sure all the | |
6938 | * relevant precision marks *that will be set **after** we exit iterator loop* | |
6939 | * are propagated backwards to common parent state of NULL and non-NULL | |
6940 | * branches. Thanks to that, state equivalence checks done later in forked | |
6941 | * state, when reaching iter_next() for ACTIVE iterator, can assume that | |
6942 | * precision marks are finalized and won't change. Because simulating another | |
6943 | * ACTIVE iterator iteration won't change them (because given same input | |
6944 | * states we'll end up with exactly same output states which we are currently | |
6945 | * comparing; and verification after the loop already propagated back what | |
6946 | * needs to be **additionally** tracked as precise). It's subtle, grok | |
6947 | * precision tracking for more intuitive understanding. | |
6948 | */ | |
6949 | static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, | |
6950 | struct bpf_kfunc_call_arg_meta *meta) | |
6951 | { | |
6952 | struct bpf_verifier_state *cur_st = env->cur_state, *queued_st; | |
6953 | struct bpf_func_state *cur_fr = cur_st->frame[cur_st->curframe], *queued_fr; | |
6954 | struct bpf_reg_state *cur_iter, *queued_iter; | |
6955 | int iter_frameno = meta->iter.frameno; | |
6956 | int iter_spi = meta->iter.spi; | |
6957 | ||
6958 | BTF_TYPE_EMIT(struct bpf_iter); | |
6959 | ||
6960 | cur_iter = &env->cur_state->frame[iter_frameno]->stack[iter_spi].spilled_ptr; | |
6961 | ||
6962 | if (cur_iter->iter.state != BPF_ITER_STATE_ACTIVE && | |
6963 | cur_iter->iter.state != BPF_ITER_STATE_DRAINED) { | |
6964 | verbose(env, "verifier internal error: unexpected iterator state %d (%s)\n", | |
6965 | cur_iter->iter.state, iter_state_str(cur_iter->iter.state)); | |
6966 | return -EFAULT; | |
6967 | } | |
6968 | ||
6969 | if (cur_iter->iter.state == BPF_ITER_STATE_ACTIVE) { | |
6970 | /* branch out active iter state */ | |
6971 | queued_st = push_stack(env, insn_idx + 1, insn_idx, false); | |
6972 | if (!queued_st) | |
6973 | return -ENOMEM; | |
6974 | ||
6975 | queued_iter = &queued_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr; | |
6976 | queued_iter->iter.state = BPF_ITER_STATE_ACTIVE; | |
6977 | queued_iter->iter.depth++; | |
6978 | ||
6979 | queued_fr = queued_st->frame[queued_st->curframe]; | |
6980 | mark_ptr_not_null_reg(&queued_fr->regs[BPF_REG_0]); | |
6981 | } | |
6982 | ||
6983 | /* switch to DRAINED state, but keep the depth unchanged */ | |
6984 | /* mark current iter state as drained and assume returned NULL */ | |
6985 | cur_iter->iter.state = BPF_ITER_STATE_DRAINED; | |
6986 | __mark_reg_const_zero(&cur_fr->regs[BPF_REG_0]); | |
6987 | ||
6988 | return 0; | |
6989 | } | |
6990 | ||
90133415 DB |
6991 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
6992 | { | |
6993 | return type == ARG_CONST_SIZE || | |
6994 | type == ARG_CONST_SIZE_OR_ZERO; | |
6995 | } | |
6996 | ||
8f14852e KKD |
6997 | static bool arg_type_is_release(enum bpf_arg_type type) |
6998 | { | |
6999 | return type & OBJ_RELEASE; | |
7000 | } | |
7001 | ||
97e03f52 JK |
7002 | static bool arg_type_is_dynptr(enum bpf_arg_type type) |
7003 | { | |
7004 | return base_type(type) == ARG_PTR_TO_DYNPTR; | |
7005 | } | |
7006 | ||
57c3bb72 AI |
7007 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
7008 | { | |
7009 | if (type == ARG_PTR_TO_INT) | |
7010 | return sizeof(u32); | |
7011 | else if (type == ARG_PTR_TO_LONG) | |
7012 | return sizeof(u64); | |
7013 | ||
7014 | return -EINVAL; | |
7015 | } | |
7016 | ||
912f442c LB |
7017 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
7018 | const struct bpf_call_arg_meta *meta, | |
7019 | enum bpf_arg_type *arg_type) | |
7020 | { | |
7021 | if (!meta->map_ptr) { | |
7022 | /* kernel subsystem misconfigured verifier */ | |
7023 | verbose(env, "invalid map_ptr to access map->type\n"); | |
7024 | return -EACCES; | |
7025 | } | |
7026 | ||
7027 | switch (meta->map_ptr->map_type) { | |
7028 | case BPF_MAP_TYPE_SOCKMAP: | |
7029 | case BPF_MAP_TYPE_SOCKHASH: | |
7030 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 7031 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
7032 | } else { |
7033 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
7034 | return -EINVAL; | |
7035 | } | |
7036 | break; | |
9330986c JK |
7037 | case BPF_MAP_TYPE_BLOOM_FILTER: |
7038 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
7039 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
7040 | break; | |
912f442c LB |
7041 | default: |
7042 | break; | |
7043 | } | |
7044 | return 0; | |
7045 | } | |
7046 | ||
f79e7ea5 LB |
7047 | struct bpf_reg_types { |
7048 | const enum bpf_reg_type types[10]; | |
1df8f55a | 7049 | u32 *btf_id; |
f79e7ea5 LB |
7050 | }; |
7051 | ||
f79e7ea5 LB |
7052 | static const struct bpf_reg_types sock_types = { |
7053 | .types = { | |
7054 | PTR_TO_SOCK_COMMON, | |
7055 | PTR_TO_SOCKET, | |
7056 | PTR_TO_TCP_SOCK, | |
7057 | PTR_TO_XDP_SOCK, | |
7058 | }, | |
7059 | }; | |
7060 | ||
49a2a4d4 | 7061 | #ifdef CONFIG_NET |
1df8f55a MKL |
7062 | static const struct bpf_reg_types btf_id_sock_common_types = { |
7063 | .types = { | |
7064 | PTR_TO_SOCK_COMMON, | |
7065 | PTR_TO_SOCKET, | |
7066 | PTR_TO_TCP_SOCK, | |
7067 | PTR_TO_XDP_SOCK, | |
7068 | PTR_TO_BTF_ID, | |
3f00c523 | 7069 | PTR_TO_BTF_ID | PTR_TRUSTED, |
1df8f55a MKL |
7070 | }, |
7071 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
7072 | }; | |
49a2a4d4 | 7073 | #endif |
1df8f55a | 7074 | |
f79e7ea5 LB |
7075 | static const struct bpf_reg_types mem_types = { |
7076 | .types = { | |
7077 | PTR_TO_STACK, | |
7078 | PTR_TO_PACKET, | |
7079 | PTR_TO_PACKET_META, | |
69c087ba | 7080 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
7081 | PTR_TO_MAP_VALUE, |
7082 | PTR_TO_MEM, | |
894f2a8b | 7083 | PTR_TO_MEM | MEM_RINGBUF, |
20b2aff4 | 7084 | PTR_TO_BUF, |
3e30be42 | 7085 | PTR_TO_BTF_ID | PTR_TRUSTED, |
f79e7ea5 LB |
7086 | }, |
7087 | }; | |
7088 | ||
7089 | static const struct bpf_reg_types int_ptr_types = { | |
7090 | .types = { | |
7091 | PTR_TO_STACK, | |
7092 | PTR_TO_PACKET, | |
7093 | PTR_TO_PACKET_META, | |
69c087ba | 7094 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
7095 | PTR_TO_MAP_VALUE, |
7096 | }, | |
7097 | }; | |
7098 | ||
4e814da0 KKD |
7099 | static const struct bpf_reg_types spin_lock_types = { |
7100 | .types = { | |
7101 | PTR_TO_MAP_VALUE, | |
7102 | PTR_TO_BTF_ID | MEM_ALLOC, | |
7103 | } | |
7104 | }; | |
7105 | ||
f79e7ea5 LB |
7106 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
7107 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
7108 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
894f2a8b | 7109 | static const struct bpf_reg_types ringbuf_mem_types = { .types = { PTR_TO_MEM | MEM_RINGBUF } }; |
f79e7ea5 | 7110 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
3f00c523 DV |
7111 | static const struct bpf_reg_types btf_ptr_types = { |
7112 | .types = { | |
7113 | PTR_TO_BTF_ID, | |
7114 | PTR_TO_BTF_ID | PTR_TRUSTED, | |
fca1aa75 | 7115 | PTR_TO_BTF_ID | MEM_RCU, |
3f00c523 DV |
7116 | }, |
7117 | }; | |
7118 | static const struct bpf_reg_types percpu_btf_ptr_types = { | |
7119 | .types = { | |
7120 | PTR_TO_BTF_ID | MEM_PERCPU, | |
7121 | PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED, | |
7122 | } | |
7123 | }; | |
69c087ba YS |
7124 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
7125 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 7126 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 7127 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
c0a5a21c | 7128 | static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
20571567 DV |
7129 | static const struct bpf_reg_types dynptr_types = { |
7130 | .types = { | |
7131 | PTR_TO_STACK, | |
27060531 | 7132 | CONST_PTR_TO_DYNPTR, |
20571567 DV |
7133 | } |
7134 | }; | |
f79e7ea5 | 7135 | |
0789e13b | 7136 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
d1673304 DM |
7137 | [ARG_PTR_TO_MAP_KEY] = &mem_types, |
7138 | [ARG_PTR_TO_MAP_VALUE] = &mem_types, | |
f79e7ea5 LB |
7139 | [ARG_CONST_SIZE] = &scalar_types, |
7140 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
7141 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
7142 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
7143 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 7144 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 7145 | #ifdef CONFIG_NET |
1df8f55a | 7146 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 7147 | #endif |
f79e7ea5 | 7148 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
7149 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
7150 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
7151 | [ARG_PTR_TO_MEM] = &mem_types, | |
894f2a8b | 7152 | [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, |
f79e7ea5 LB |
7153 | [ARG_PTR_TO_INT] = &int_ptr_types, |
7154 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 7155 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 7156 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 7157 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 7158 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 7159 | [ARG_PTR_TO_TIMER] = &timer_types, |
c0a5a21c | 7160 | [ARG_PTR_TO_KPTR] = &kptr_types, |
20571567 | 7161 | [ARG_PTR_TO_DYNPTR] = &dynptr_types, |
f79e7ea5 LB |
7162 | }; |
7163 | ||
7164 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 | 7165 | enum bpf_arg_type arg_type, |
c0a5a21c KKD |
7166 | const u32 *arg_btf_id, |
7167 | struct bpf_call_arg_meta *meta) | |
f79e7ea5 LB |
7168 | { |
7169 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
7170 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 7171 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
7172 | int i, j; |
7173 | ||
48946bd6 | 7174 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
7175 | if (!compatible) { |
7176 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
7177 | return -EFAULT; | |
7178 | } | |
7179 | ||
216e3cd2 HL |
7180 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
7181 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY | |
7182 | * | |
7183 | * Same for MAYBE_NULL: | |
7184 | * | |
7185 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, | |
7186 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL | |
7187 | * | |
7188 | * Therefore we fold these flags depending on the arg_type before comparison. | |
7189 | */ | |
7190 | if (arg_type & MEM_RDONLY) | |
7191 | type &= ~MEM_RDONLY; | |
7192 | if (arg_type & PTR_MAYBE_NULL) | |
7193 | type &= ~PTR_MAYBE_NULL; | |
7194 | ||
738c96d5 DM |
7195 | if (meta->func_id == BPF_FUNC_kptr_xchg && type & MEM_ALLOC) |
7196 | type &= ~MEM_ALLOC; | |
7197 | ||
f79e7ea5 LB |
7198 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
7199 | expected = compatible->types[i]; | |
7200 | if (expected == NOT_INIT) | |
7201 | break; | |
7202 | ||
7203 | if (type == expected) | |
a968d5e2 | 7204 | goto found; |
f79e7ea5 LB |
7205 | } |
7206 | ||
216e3cd2 | 7207 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
f79e7ea5 | 7208 | for (j = 0; j + 1 < i; j++) |
c25b2ae1 HL |
7209 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
7210 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); | |
f79e7ea5 | 7211 | return -EACCES; |
a968d5e2 MKL |
7212 | |
7213 | found: | |
da03e43a KKD |
7214 | if (base_type(reg->type) != PTR_TO_BTF_ID) |
7215 | return 0; | |
7216 | ||
3e30be42 AS |
7217 | if (compatible == &mem_types) { |
7218 | if (!(arg_type & MEM_RDONLY)) { | |
7219 | verbose(env, | |
7220 | "%s() may write into memory pointed by R%d type=%s\n", | |
7221 | func_id_name(meta->func_id), | |
7222 | regno, reg_type_str(env, reg->type)); | |
7223 | return -EACCES; | |
7224 | } | |
7225 | return 0; | |
7226 | } | |
7227 | ||
da03e43a KKD |
7228 | switch ((int)reg->type) { |
7229 | case PTR_TO_BTF_ID: | |
7230 | case PTR_TO_BTF_ID | PTR_TRUSTED: | |
7231 | case PTR_TO_BTF_ID | MEM_RCU: | |
add68b84 AS |
7232 | case PTR_TO_BTF_ID | PTR_MAYBE_NULL: |
7233 | case PTR_TO_BTF_ID | PTR_MAYBE_NULL | MEM_RCU: | |
da03e43a | 7234 | { |
2ab3b380 KKD |
7235 | /* For bpf_sk_release, it needs to match against first member |
7236 | * 'struct sock_common', hence make an exception for it. This | |
7237 | * allows bpf_sk_release to work for multiple socket types. | |
7238 | */ | |
7239 | bool strict_type_match = arg_type_is_release(arg_type) && | |
7240 | meta->func_id != BPF_FUNC_sk_release; | |
7241 | ||
add68b84 AS |
7242 | if (type_may_be_null(reg->type) && |
7243 | (!type_may_be_null(arg_type) || arg_type_is_release(arg_type))) { | |
7244 | verbose(env, "Possibly NULL pointer passed to helper arg%d\n", regno); | |
7245 | return -EACCES; | |
7246 | } | |
7247 | ||
1df8f55a MKL |
7248 | if (!arg_btf_id) { |
7249 | if (!compatible->btf_id) { | |
7250 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
7251 | return -EFAULT; | |
7252 | } | |
7253 | arg_btf_id = compatible->btf_id; | |
7254 | } | |
7255 | ||
c0a5a21c | 7256 | if (meta->func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac | 7257 | if (map_kptr_match_type(env, meta->kptr_field, reg, regno)) |
c0a5a21c | 7258 | return -EACCES; |
47e34cb7 DM |
7259 | } else { |
7260 | if (arg_btf_id == BPF_PTR_POISON) { | |
7261 | verbose(env, "verifier internal error:"); | |
7262 | verbose(env, "R%d has non-overwritten BPF_PTR_POISON type\n", | |
7263 | regno); | |
7264 | return -EACCES; | |
7265 | } | |
7266 | ||
7267 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
7268 | btf_vmlinux, *arg_btf_id, | |
7269 | strict_type_match)) { | |
7270 | verbose(env, "R%d is of type %s but %s is expected\n", | |
b32a5dae DM |
7271 | regno, btf_type_name(reg->btf, reg->btf_id), |
7272 | btf_type_name(btf_vmlinux, *arg_btf_id)); | |
47e34cb7 DM |
7273 | return -EACCES; |
7274 | } | |
a968d5e2 | 7275 | } |
da03e43a KKD |
7276 | break; |
7277 | } | |
7278 | case PTR_TO_BTF_ID | MEM_ALLOC: | |
738c96d5 DM |
7279 | if (meta->func_id != BPF_FUNC_spin_lock && meta->func_id != BPF_FUNC_spin_unlock && |
7280 | meta->func_id != BPF_FUNC_kptr_xchg) { | |
4e814da0 KKD |
7281 | verbose(env, "verifier internal error: unimplemented handling of MEM_ALLOC\n"); |
7282 | return -EFAULT; | |
7283 | } | |
da03e43a KKD |
7284 | /* Handled by helper specific checks */ |
7285 | break; | |
7286 | case PTR_TO_BTF_ID | MEM_PERCPU: | |
7287 | case PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED: | |
7288 | /* Handled by helper specific checks */ | |
7289 | break; | |
7290 | default: | |
7291 | verbose(env, "verifier internal error: invalid PTR_TO_BTF_ID register for type match\n"); | |
7292 | return -EFAULT; | |
a968d5e2 | 7293 | } |
a968d5e2 | 7294 | return 0; |
f79e7ea5 LB |
7295 | } |
7296 | ||
6a3cd331 DM |
7297 | static struct btf_field * |
7298 | reg_find_field_offset(const struct bpf_reg_state *reg, s32 off, u32 fields) | |
7299 | { | |
7300 | struct btf_field *field; | |
7301 | struct btf_record *rec; | |
7302 | ||
7303 | rec = reg_btf_record(reg); | |
7304 | if (!rec) | |
7305 | return NULL; | |
7306 | ||
7307 | field = btf_record_find(rec, off, fields); | |
7308 | if (!field) | |
7309 | return NULL; | |
7310 | ||
7311 | return field; | |
7312 | } | |
7313 | ||
25b35dd2 KKD |
7314 | int check_func_arg_reg_off(struct bpf_verifier_env *env, |
7315 | const struct bpf_reg_state *reg, int regno, | |
8f14852e | 7316 | enum bpf_arg_type arg_type) |
25b35dd2 | 7317 | { |
184c9bdb | 7318 | u32 type = reg->type; |
25b35dd2 | 7319 | |
184c9bdb KKD |
7320 | /* When referenced register is passed to release function, its fixed |
7321 | * offset must be 0. | |
7322 | * | |
7323 | * We will check arg_type_is_release reg has ref_obj_id when storing | |
7324 | * meta->release_regno. | |
7325 | */ | |
7326 | if (arg_type_is_release(arg_type)) { | |
7327 | /* ARG_PTR_TO_DYNPTR with OBJ_RELEASE is a bit special, as it | |
7328 | * may not directly point to the object being released, but to | |
7329 | * dynptr pointing to such object, which might be at some offset | |
7330 | * on the stack. In that case, we simply to fallback to the | |
7331 | * default handling. | |
7332 | */ | |
7333 | if (arg_type_is_dynptr(arg_type) && type == PTR_TO_STACK) | |
7334 | return 0; | |
6a3cd331 DM |
7335 | |
7336 | if ((type_is_ptr_alloc_obj(type) || type_is_non_owning_ref(type)) && reg->off) { | |
7337 | if (reg_find_field_offset(reg, reg->off, BPF_GRAPH_NODE_OR_ROOT)) | |
7338 | return __check_ptr_off_reg(env, reg, regno, true); | |
7339 | ||
7340 | verbose(env, "R%d must have zero offset when passed to release func\n", | |
7341 | regno); | |
7342 | verbose(env, "No graph node or root found at R%d type:%s off:%d\n", regno, | |
b32a5dae | 7343 | btf_type_name(reg->btf, reg->btf_id), reg->off); |
6a3cd331 DM |
7344 | return -EINVAL; |
7345 | } | |
7346 | ||
184c9bdb KKD |
7347 | /* Doing check_ptr_off_reg check for the offset will catch this |
7348 | * because fixed_off_ok is false, but checking here allows us | |
7349 | * to give the user a better error message. | |
7350 | */ | |
7351 | if (reg->off) { | |
7352 | verbose(env, "R%d must have zero offset when passed to release func or trusted arg to kfunc\n", | |
7353 | regno); | |
7354 | return -EINVAL; | |
7355 | } | |
7356 | return __check_ptr_off_reg(env, reg, regno, false); | |
7357 | } | |
7358 | ||
7359 | switch (type) { | |
7360 | /* Pointer types where both fixed and variable offset is explicitly allowed: */ | |
97e03f52 | 7361 | case PTR_TO_STACK: |
25b35dd2 KKD |
7362 | case PTR_TO_PACKET: |
7363 | case PTR_TO_PACKET_META: | |
7364 | case PTR_TO_MAP_KEY: | |
7365 | case PTR_TO_MAP_VALUE: | |
7366 | case PTR_TO_MEM: | |
7367 | case PTR_TO_MEM | MEM_RDONLY: | |
894f2a8b | 7368 | case PTR_TO_MEM | MEM_RINGBUF: |
25b35dd2 KKD |
7369 | case PTR_TO_BUF: |
7370 | case PTR_TO_BUF | MEM_RDONLY: | |
97e03f52 | 7371 | case SCALAR_VALUE: |
184c9bdb | 7372 | return 0; |
25b35dd2 KKD |
7373 | /* All the rest must be rejected, except PTR_TO_BTF_ID which allows |
7374 | * fixed offset. | |
7375 | */ | |
7376 | case PTR_TO_BTF_ID: | |
282de143 | 7377 | case PTR_TO_BTF_ID | MEM_ALLOC: |
3f00c523 | 7378 | case PTR_TO_BTF_ID | PTR_TRUSTED: |
fca1aa75 | 7379 | case PTR_TO_BTF_ID | MEM_RCU: |
6a3cd331 | 7380 | case PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF: |
24d5bb80 | 7381 | /* When referenced PTR_TO_BTF_ID is passed to release function, |
184c9bdb KKD |
7382 | * its fixed offset must be 0. In the other cases, fixed offset |
7383 | * can be non-zero. This was already checked above. So pass | |
7384 | * fixed_off_ok as true to allow fixed offset for all other | |
7385 | * cases. var_off always must be 0 for PTR_TO_BTF_ID, hence we | |
7386 | * still need to do checks instead of returning. | |
24d5bb80 | 7387 | */ |
184c9bdb | 7388 | return __check_ptr_off_reg(env, reg, regno, true); |
25b35dd2 | 7389 | default: |
184c9bdb | 7390 | return __check_ptr_off_reg(env, reg, regno, false); |
25b35dd2 | 7391 | } |
25b35dd2 KKD |
7392 | } |
7393 | ||
485ec51e JK |
7394 | static struct bpf_reg_state *get_dynptr_arg_reg(struct bpf_verifier_env *env, |
7395 | const struct bpf_func_proto *fn, | |
7396 | struct bpf_reg_state *regs) | |
7397 | { | |
7398 | struct bpf_reg_state *state = NULL; | |
7399 | int i; | |
7400 | ||
7401 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) | |
7402 | if (arg_type_is_dynptr(fn->arg_type[i])) { | |
7403 | if (state) { | |
7404 | verbose(env, "verifier internal error: multiple dynptr args\n"); | |
7405 | return NULL; | |
7406 | } | |
7407 | state = ®s[BPF_REG_1 + i]; | |
7408 | } | |
7409 | ||
7410 | if (!state) | |
7411 | verbose(env, "verifier internal error: no dynptr arg found\n"); | |
7412 | ||
7413 | return state; | |
7414 | } | |
7415 | ||
f8064ab9 | 7416 | static int dynptr_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
34d4ef57 JK |
7417 | { |
7418 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7419 | int spi; |
34d4ef57 | 7420 | |
27060531 | 7421 | if (reg->type == CONST_PTR_TO_DYNPTR) |
f8064ab9 KKD |
7422 | return reg->id; |
7423 | spi = dynptr_get_spi(env, reg); | |
7424 | if (spi < 0) | |
7425 | return spi; | |
7426 | return state->stack[spi].spilled_ptr.id; | |
7427 | } | |
7428 | ||
79168a66 | 7429 | static int dynptr_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
34d4ef57 JK |
7430 | { |
7431 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7432 | int spi; |
27060531 | 7433 | |
27060531 KKD |
7434 | if (reg->type == CONST_PTR_TO_DYNPTR) |
7435 | return reg->ref_obj_id; | |
79168a66 KKD |
7436 | spi = dynptr_get_spi(env, reg); |
7437 | if (spi < 0) | |
7438 | return spi; | |
27060531 | 7439 | return state->stack[spi].spilled_ptr.ref_obj_id; |
34d4ef57 JK |
7440 | } |
7441 | ||
b5964b96 JK |
7442 | static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env, |
7443 | struct bpf_reg_state *reg) | |
7444 | { | |
7445 | struct bpf_func_state *state = func(env, reg); | |
7446 | int spi; | |
7447 | ||
7448 | if (reg->type == CONST_PTR_TO_DYNPTR) | |
7449 | return reg->dynptr.type; | |
7450 | ||
7451 | spi = __get_spi(reg->off); | |
7452 | if (spi < 0) { | |
7453 | verbose(env, "verifier internal error: invalid spi when querying dynptr type\n"); | |
7454 | return BPF_DYNPTR_TYPE_INVALID; | |
7455 | } | |
7456 | ||
7457 | return state->stack[spi].spilled_ptr.dynptr.type; | |
7458 | } | |
7459 | ||
af7ec138 YS |
7460 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
7461 | struct bpf_call_arg_meta *meta, | |
1d18feb2 JK |
7462 | const struct bpf_func_proto *fn, |
7463 | int insn_idx) | |
17a52670 | 7464 | { |
af7ec138 | 7465 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 7466 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 7467 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 7468 | enum bpf_reg_type type = reg->type; |
508362ac | 7469 | u32 *arg_btf_id = NULL; |
17a52670 AS |
7470 | int err = 0; |
7471 | ||
80f1d68c | 7472 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
7473 | return 0; |
7474 | ||
dc503a8a EC |
7475 | err = check_reg_arg(env, regno, SRC_OP); |
7476 | if (err) | |
7477 | return err; | |
17a52670 | 7478 | |
1be7f75d AS |
7479 | if (arg_type == ARG_ANYTHING) { |
7480 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
7481 | verbose(env, "R%d leaks addr into helper function\n", |
7482 | regno); | |
1be7f75d AS |
7483 | return -EACCES; |
7484 | } | |
80f1d68c | 7485 | return 0; |
1be7f75d | 7486 | } |
80f1d68c | 7487 | |
de8f3a83 | 7488 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 7489 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 7490 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
7491 | return -EACCES; |
7492 | } | |
7493 | ||
16d1e00c | 7494 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE) { |
912f442c LB |
7495 | err = resolve_map_arg_type(env, meta, &arg_type); |
7496 | if (err) | |
7497 | return err; | |
7498 | } | |
7499 | ||
48946bd6 | 7500 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
7501 | /* A NULL register has a SCALAR_VALUE type, so skip |
7502 | * type checking. | |
7503 | */ | |
7504 | goto skip_type_check; | |
7505 | ||
508362ac | 7506 | /* arg_btf_id and arg_size are in a union. */ |
4e814da0 KKD |
7507 | if (base_type(arg_type) == ARG_PTR_TO_BTF_ID || |
7508 | base_type(arg_type) == ARG_PTR_TO_SPIN_LOCK) | |
508362ac MM |
7509 | arg_btf_id = fn->arg_btf_id[arg]; |
7510 | ||
7511 | err = check_reg_type(env, regno, arg_type, arg_btf_id, meta); | |
f79e7ea5 LB |
7512 | if (err) |
7513 | return err; | |
7514 | ||
8f14852e | 7515 | err = check_func_arg_reg_off(env, reg, regno, arg_type); |
25b35dd2 KKD |
7516 | if (err) |
7517 | return err; | |
d7b9454a | 7518 | |
fd1b0d60 | 7519 | skip_type_check: |
8f14852e | 7520 | if (arg_type_is_release(arg_type)) { |
bc34dee6 JK |
7521 | if (arg_type_is_dynptr(arg_type)) { |
7522 | struct bpf_func_state *state = func(env, reg); | |
27060531 | 7523 | int spi; |
bc34dee6 | 7524 | |
27060531 KKD |
7525 | /* Only dynptr created on stack can be released, thus |
7526 | * the get_spi and stack state checks for spilled_ptr | |
7527 | * should only be done before process_dynptr_func for | |
7528 | * PTR_TO_STACK. | |
7529 | */ | |
7530 | if (reg->type == PTR_TO_STACK) { | |
79168a66 | 7531 | spi = dynptr_get_spi(env, reg); |
f5b625e5 | 7532 | if (spi < 0 || !state->stack[spi].spilled_ptr.ref_obj_id) { |
27060531 KKD |
7533 | verbose(env, "arg %d is an unacquired reference\n", regno); |
7534 | return -EINVAL; | |
7535 | } | |
7536 | } else { | |
7537 | verbose(env, "cannot release unowned const bpf_dynptr\n"); | |
bc34dee6 JK |
7538 | return -EINVAL; |
7539 | } | |
7540 | } else if (!reg->ref_obj_id && !register_is_null(reg)) { | |
8f14852e KKD |
7541 | verbose(env, "R%d must be referenced when passed to release function\n", |
7542 | regno); | |
7543 | return -EINVAL; | |
7544 | } | |
7545 | if (meta->release_regno) { | |
7546 | verbose(env, "verifier internal error: more than one release argument\n"); | |
7547 | return -EFAULT; | |
7548 | } | |
7549 | meta->release_regno = regno; | |
7550 | } | |
7551 | ||
02f7c958 | 7552 | if (reg->ref_obj_id) { |
457f4436 AN |
7553 | if (meta->ref_obj_id) { |
7554 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
7555 | regno, reg->ref_obj_id, | |
7556 | meta->ref_obj_id); | |
7557 | return -EFAULT; | |
7558 | } | |
7559 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
7560 | } |
7561 | ||
8ab4cdcf JK |
7562 | switch (base_type(arg_type)) { |
7563 | case ARG_CONST_MAP_PTR: | |
17a52670 | 7564 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
3e8ce298 AS |
7565 | if (meta->map_ptr) { |
7566 | /* Use map_uid (which is unique id of inner map) to reject: | |
7567 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
7568 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
7569 | * if (inner_map1 && inner_map2) { | |
7570 | * timer = bpf_map_lookup_elem(inner_map1); | |
7571 | * if (timer) | |
7572 | * // mismatch would have been allowed | |
7573 | * bpf_timer_init(timer, inner_map2); | |
7574 | * } | |
7575 | * | |
7576 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
7577 | */ | |
7578 | if (meta->map_ptr != reg->map_ptr || | |
7579 | meta->map_uid != reg->map_uid) { | |
7580 | verbose(env, | |
7581 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
7582 | meta->map_uid, reg->map_uid); | |
7583 | return -EINVAL; | |
7584 | } | |
b00628b1 | 7585 | } |
33ff9823 | 7586 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 7587 | meta->map_uid = reg->map_uid; |
8ab4cdcf JK |
7588 | break; |
7589 | case ARG_PTR_TO_MAP_KEY: | |
17a52670 AS |
7590 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
7591 | * check that [key, key + map->key_size) are within | |
7592 | * stack limits and initialized | |
7593 | */ | |
33ff9823 | 7594 | if (!meta->map_ptr) { |
17a52670 AS |
7595 | /* in function declaration map_ptr must come before |
7596 | * map_key, so that it's verified and known before | |
7597 | * we have to check map_key here. Otherwise it means | |
7598 | * that kernel subsystem misconfigured verifier | |
7599 | */ | |
61bd5218 | 7600 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
7601 | return -EACCES; |
7602 | } | |
d71962f3 PC |
7603 | err = check_helper_mem_access(env, regno, |
7604 | meta->map_ptr->key_size, false, | |
7605 | NULL); | |
8ab4cdcf JK |
7606 | break; |
7607 | case ARG_PTR_TO_MAP_VALUE: | |
48946bd6 HL |
7608 | if (type_may_be_null(arg_type) && register_is_null(reg)) |
7609 | return 0; | |
7610 | ||
17a52670 AS |
7611 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
7612 | * check [value, value + map->value_size) validity | |
7613 | */ | |
33ff9823 | 7614 | if (!meta->map_ptr) { |
17a52670 | 7615 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 7616 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
7617 | return -EACCES; |
7618 | } | |
16d1e00c | 7619 | meta->raw_mode = arg_type & MEM_UNINIT; |
d71962f3 PC |
7620 | err = check_helper_mem_access(env, regno, |
7621 | meta->map_ptr->value_size, false, | |
2ea864c5 | 7622 | meta); |
8ab4cdcf JK |
7623 | break; |
7624 | case ARG_PTR_TO_PERCPU_BTF_ID: | |
eaa6bcb7 HL |
7625 | if (!reg->btf_id) { |
7626 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
7627 | return -EACCES; | |
7628 | } | |
22dc4a0f | 7629 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 7630 | meta->ret_btf_id = reg->btf_id; |
8ab4cdcf JK |
7631 | break; |
7632 | case ARG_PTR_TO_SPIN_LOCK: | |
5d92ddc3 DM |
7633 | if (in_rbtree_lock_required_cb(env)) { |
7634 | verbose(env, "can't spin_{lock,unlock} in rbtree cb\n"); | |
7635 | return -EACCES; | |
7636 | } | |
c18f0b6a | 7637 | if (meta->func_id == BPF_FUNC_spin_lock) { |
ac50fe51 KKD |
7638 | err = process_spin_lock(env, regno, true); |
7639 | if (err) | |
7640 | return err; | |
c18f0b6a | 7641 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
ac50fe51 KKD |
7642 | err = process_spin_lock(env, regno, false); |
7643 | if (err) | |
7644 | return err; | |
c18f0b6a LB |
7645 | } else { |
7646 | verbose(env, "verifier internal error\n"); | |
7647 | return -EFAULT; | |
7648 | } | |
8ab4cdcf JK |
7649 | break; |
7650 | case ARG_PTR_TO_TIMER: | |
ac50fe51 KKD |
7651 | err = process_timer_func(env, regno, meta); |
7652 | if (err) | |
7653 | return err; | |
8ab4cdcf JK |
7654 | break; |
7655 | case ARG_PTR_TO_FUNC: | |
69c087ba | 7656 | meta->subprogno = reg->subprogno; |
8ab4cdcf JK |
7657 | break; |
7658 | case ARG_PTR_TO_MEM: | |
a2bbe7cc LB |
7659 | /* The access to this pointer is only checked when we hit the |
7660 | * next is_mem_size argument below. | |
7661 | */ | |
16d1e00c | 7662 | meta->raw_mode = arg_type & MEM_UNINIT; |
508362ac MM |
7663 | if (arg_type & MEM_FIXED_SIZE) { |
7664 | err = check_helper_mem_access(env, regno, | |
7665 | fn->arg_size[arg], false, | |
7666 | meta); | |
7667 | } | |
8ab4cdcf JK |
7668 | break; |
7669 | case ARG_CONST_SIZE: | |
7670 | err = check_mem_size_reg(env, reg, regno, false, meta); | |
7671 | break; | |
7672 | case ARG_CONST_SIZE_OR_ZERO: | |
7673 | err = check_mem_size_reg(env, reg, regno, true, meta); | |
7674 | break; | |
7675 | case ARG_PTR_TO_DYNPTR: | |
361f129f | 7676 | err = process_dynptr_func(env, regno, insn_idx, arg_type, 0); |
ac50fe51 KKD |
7677 | if (err) |
7678 | return err; | |
8ab4cdcf JK |
7679 | break; |
7680 | case ARG_CONST_ALLOC_SIZE_OR_ZERO: | |
457f4436 | 7681 | if (!tnum_is_const(reg->var_off)) { |
28a8add6 | 7682 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
7683 | regno); |
7684 | return -EACCES; | |
7685 | } | |
7686 | meta->mem_size = reg->var_off.value; | |
2fc31465 KKD |
7687 | err = mark_chain_precision(env, regno); |
7688 | if (err) | |
7689 | return err; | |
8ab4cdcf JK |
7690 | break; |
7691 | case ARG_PTR_TO_INT: | |
7692 | case ARG_PTR_TO_LONG: | |
7693 | { | |
57c3bb72 AI |
7694 | int size = int_ptr_type_to_size(arg_type); |
7695 | ||
7696 | err = check_helper_mem_access(env, regno, size, false, meta); | |
7697 | if (err) | |
7698 | return err; | |
7699 | err = check_ptr_alignment(env, reg, 0, size, true); | |
8ab4cdcf JK |
7700 | break; |
7701 | } | |
7702 | case ARG_PTR_TO_CONST_STR: | |
7703 | { | |
fff13c4b FR |
7704 | struct bpf_map *map = reg->map_ptr; |
7705 | int map_off; | |
7706 | u64 map_addr; | |
7707 | char *str_ptr; | |
7708 | ||
a8fad73e | 7709 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
7710 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
7711 | return -EACCES; | |
7712 | } | |
7713 | ||
7714 | if (!tnum_is_const(reg->var_off)) { | |
7715 | verbose(env, "R%d is not a constant address'\n", regno); | |
7716 | return -EACCES; | |
7717 | } | |
7718 | ||
7719 | if (!map->ops->map_direct_value_addr) { | |
7720 | verbose(env, "no direct value access support for this map type\n"); | |
7721 | return -EACCES; | |
7722 | } | |
7723 | ||
7724 | err = check_map_access(env, regno, reg->off, | |
61df10c7 KKD |
7725 | map->value_size - reg->off, false, |
7726 | ACCESS_HELPER); | |
fff13c4b FR |
7727 | if (err) |
7728 | return err; | |
7729 | ||
7730 | map_off = reg->off + reg->var_off.value; | |
7731 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
7732 | if (err) { | |
7733 | verbose(env, "direct value access on string failed\n"); | |
7734 | return err; | |
7735 | } | |
7736 | ||
7737 | str_ptr = (char *)(long)(map_addr); | |
7738 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
7739 | verbose(env, "string is not zero-terminated\n"); | |
7740 | return -EINVAL; | |
7741 | } | |
8ab4cdcf JK |
7742 | break; |
7743 | } | |
7744 | case ARG_PTR_TO_KPTR: | |
ac50fe51 KKD |
7745 | err = process_kptr_func(env, regno, meta); |
7746 | if (err) | |
7747 | return err; | |
8ab4cdcf | 7748 | break; |
17a52670 AS |
7749 | } |
7750 | ||
7751 | return err; | |
7752 | } | |
7753 | ||
0126240f LB |
7754 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
7755 | { | |
7756 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 7757 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
7758 | |
7759 | if (func_id != BPF_FUNC_map_update_elem) | |
7760 | return false; | |
7761 | ||
7762 | /* It's not possible to get access to a locked struct sock in these | |
7763 | * contexts, so updating is safe. | |
7764 | */ | |
7765 | switch (type) { | |
7766 | case BPF_PROG_TYPE_TRACING: | |
7767 | if (eatype == BPF_TRACE_ITER) | |
7768 | return true; | |
7769 | break; | |
7770 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
7771 | case BPF_PROG_TYPE_SCHED_CLS: | |
7772 | case BPF_PROG_TYPE_SCHED_ACT: | |
7773 | case BPF_PROG_TYPE_XDP: | |
7774 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
7775 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
7776 | case BPF_PROG_TYPE_SK_LOOKUP: | |
7777 | return true; | |
7778 | default: | |
7779 | break; | |
7780 | } | |
7781 | ||
7782 | verbose(env, "cannot update sockmap in this context\n"); | |
7783 | return false; | |
7784 | } | |
7785 | ||
e411901c MF |
7786 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
7787 | { | |
95acd881 TA |
7788 | return env->prog->jit_requested && |
7789 | bpf_jit_supports_subprog_tailcalls(); | |
e411901c MF |
7790 | } |
7791 | ||
61bd5218 JK |
7792 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
7793 | struct bpf_map *map, int func_id) | |
35578d79 | 7794 | { |
35578d79 KX |
7795 | if (!map) |
7796 | return 0; | |
7797 | ||
6aff67c8 AS |
7798 | /* We need a two way check, first is from map perspective ... */ |
7799 | switch (map->map_type) { | |
7800 | case BPF_MAP_TYPE_PROG_ARRAY: | |
7801 | if (func_id != BPF_FUNC_tail_call) | |
7802 | goto error; | |
7803 | break; | |
7804 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
7805 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 7806 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 7807 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
7808 | func_id != BPF_FUNC_perf_event_read_value && |
7809 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
7810 | goto error; |
7811 | break; | |
457f4436 AN |
7812 | case BPF_MAP_TYPE_RINGBUF: |
7813 | if (func_id != BPF_FUNC_ringbuf_output && | |
7814 | func_id != BPF_FUNC_ringbuf_reserve && | |
bc34dee6 JK |
7815 | func_id != BPF_FUNC_ringbuf_query && |
7816 | func_id != BPF_FUNC_ringbuf_reserve_dynptr && | |
7817 | func_id != BPF_FUNC_ringbuf_submit_dynptr && | |
7818 | func_id != BPF_FUNC_ringbuf_discard_dynptr) | |
457f4436 AN |
7819 | goto error; |
7820 | break; | |
583c1f42 | 7821 | case BPF_MAP_TYPE_USER_RINGBUF: |
20571567 DV |
7822 | if (func_id != BPF_FUNC_user_ringbuf_drain) |
7823 | goto error; | |
7824 | break; | |
6aff67c8 AS |
7825 | case BPF_MAP_TYPE_STACK_TRACE: |
7826 | if (func_id != BPF_FUNC_get_stackid) | |
7827 | goto error; | |
7828 | break; | |
4ed8ec52 | 7829 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 7830 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 7831 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
7832 | goto error; |
7833 | break; | |
cd339431 | 7834 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 7835 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
7836 | if (func_id != BPF_FUNC_get_local_storage) |
7837 | goto error; | |
7838 | break; | |
546ac1ff | 7839 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 7840 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
7841 | if (func_id != BPF_FUNC_redirect_map && |
7842 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
7843 | goto error; |
7844 | break; | |
fbfc504a BT |
7845 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
7846 | * appear. | |
7847 | */ | |
6710e112 JDB |
7848 | case BPF_MAP_TYPE_CPUMAP: |
7849 | if (func_id != BPF_FUNC_redirect_map) | |
7850 | goto error; | |
7851 | break; | |
fada7fdc JL |
7852 | case BPF_MAP_TYPE_XSKMAP: |
7853 | if (func_id != BPF_FUNC_redirect_map && | |
7854 | func_id != BPF_FUNC_map_lookup_elem) | |
7855 | goto error; | |
7856 | break; | |
56f668df | 7857 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 7858 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
7859 | if (func_id != BPF_FUNC_map_lookup_elem) |
7860 | goto error; | |
16a43625 | 7861 | break; |
174a79ff JF |
7862 | case BPF_MAP_TYPE_SOCKMAP: |
7863 | if (func_id != BPF_FUNC_sk_redirect_map && | |
7864 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 7865 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 7866 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 7867 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
7868 | func_id != BPF_FUNC_map_lookup_elem && |
7869 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
7870 | goto error; |
7871 | break; | |
81110384 JF |
7872 | case BPF_MAP_TYPE_SOCKHASH: |
7873 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
7874 | func_id != BPF_FUNC_sock_hash_update && | |
7875 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 7876 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 7877 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
7878 | func_id != BPF_FUNC_map_lookup_elem && |
7879 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
7880 | goto error; |
7881 | break; | |
2dbb9b9e MKL |
7882 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
7883 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
7884 | goto error; | |
7885 | break; | |
f1a2e44a MV |
7886 | case BPF_MAP_TYPE_QUEUE: |
7887 | case BPF_MAP_TYPE_STACK: | |
7888 | if (func_id != BPF_FUNC_map_peek_elem && | |
7889 | func_id != BPF_FUNC_map_pop_elem && | |
7890 | func_id != BPF_FUNC_map_push_elem) | |
7891 | goto error; | |
7892 | break; | |
6ac99e8f MKL |
7893 | case BPF_MAP_TYPE_SK_STORAGE: |
7894 | if (func_id != BPF_FUNC_sk_storage_get && | |
9db44fdd KKD |
7895 | func_id != BPF_FUNC_sk_storage_delete && |
7896 | func_id != BPF_FUNC_kptr_xchg) | |
6ac99e8f MKL |
7897 | goto error; |
7898 | break; | |
8ea63684 KS |
7899 | case BPF_MAP_TYPE_INODE_STORAGE: |
7900 | if (func_id != BPF_FUNC_inode_storage_get && | |
9db44fdd KKD |
7901 | func_id != BPF_FUNC_inode_storage_delete && |
7902 | func_id != BPF_FUNC_kptr_xchg) | |
8ea63684 KS |
7903 | goto error; |
7904 | break; | |
4cf1bc1f KS |
7905 | case BPF_MAP_TYPE_TASK_STORAGE: |
7906 | if (func_id != BPF_FUNC_task_storage_get && | |
9db44fdd KKD |
7907 | func_id != BPF_FUNC_task_storage_delete && |
7908 | func_id != BPF_FUNC_kptr_xchg) | |
4cf1bc1f KS |
7909 | goto error; |
7910 | break; | |
c4bcfb38 YS |
7911 | case BPF_MAP_TYPE_CGRP_STORAGE: |
7912 | if (func_id != BPF_FUNC_cgrp_storage_get && | |
9db44fdd KKD |
7913 | func_id != BPF_FUNC_cgrp_storage_delete && |
7914 | func_id != BPF_FUNC_kptr_xchg) | |
c4bcfb38 YS |
7915 | goto error; |
7916 | break; | |
9330986c JK |
7917 | case BPF_MAP_TYPE_BLOOM_FILTER: |
7918 | if (func_id != BPF_FUNC_map_peek_elem && | |
7919 | func_id != BPF_FUNC_map_push_elem) | |
7920 | goto error; | |
7921 | break; | |
6aff67c8 AS |
7922 | default: |
7923 | break; | |
7924 | } | |
7925 | ||
7926 | /* ... and second from the function itself. */ | |
7927 | switch (func_id) { | |
7928 | case BPF_FUNC_tail_call: | |
7929 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
7930 | goto error; | |
e411901c MF |
7931 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
7932 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
7933 | return -EINVAL; |
7934 | } | |
6aff67c8 AS |
7935 | break; |
7936 | case BPF_FUNC_perf_event_read: | |
7937 | case BPF_FUNC_perf_event_output: | |
908432ca | 7938 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 7939 | case BPF_FUNC_skb_output: |
d831ee84 | 7940 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
7941 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
7942 | goto error; | |
7943 | break; | |
5b029a32 DB |
7944 | case BPF_FUNC_ringbuf_output: |
7945 | case BPF_FUNC_ringbuf_reserve: | |
7946 | case BPF_FUNC_ringbuf_query: | |
bc34dee6 JK |
7947 | case BPF_FUNC_ringbuf_reserve_dynptr: |
7948 | case BPF_FUNC_ringbuf_submit_dynptr: | |
7949 | case BPF_FUNC_ringbuf_discard_dynptr: | |
5b029a32 DB |
7950 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
7951 | goto error; | |
7952 | break; | |
20571567 DV |
7953 | case BPF_FUNC_user_ringbuf_drain: |
7954 | if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF) | |
7955 | goto error; | |
7956 | break; | |
6aff67c8 AS |
7957 | case BPF_FUNC_get_stackid: |
7958 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
7959 | goto error; | |
7960 | break; | |
60d20f91 | 7961 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 7962 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
7963 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
7964 | goto error; | |
7965 | break; | |
97f91a7c | 7966 | case BPF_FUNC_redirect_map: |
9c270af3 | 7967 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 7968 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
7969 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
7970 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
7971 | goto error; |
7972 | break; | |
174a79ff | 7973 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 7974 | case BPF_FUNC_msg_redirect_map: |
81110384 | 7975 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
7976 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
7977 | goto error; | |
7978 | break; | |
81110384 JF |
7979 | case BPF_FUNC_sk_redirect_hash: |
7980 | case BPF_FUNC_msg_redirect_hash: | |
7981 | case BPF_FUNC_sock_hash_update: | |
7982 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
7983 | goto error; |
7984 | break; | |
cd339431 | 7985 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
7986 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
7987 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
7988 | goto error; |
7989 | break; | |
2dbb9b9e | 7990 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
7991 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
7992 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
7993 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
7994 | goto error; |
7995 | break; | |
f1a2e44a | 7996 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
7997 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
7998 | map->map_type != BPF_MAP_TYPE_STACK) | |
7999 | goto error; | |
8000 | break; | |
9330986c JK |
8001 | case BPF_FUNC_map_peek_elem: |
8002 | case BPF_FUNC_map_push_elem: | |
8003 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
8004 | map->map_type != BPF_MAP_TYPE_STACK && | |
8005 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
8006 | goto error; | |
8007 | break; | |
07343110 FZ |
8008 | case BPF_FUNC_map_lookup_percpu_elem: |
8009 | if (map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && | |
8010 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && | |
8011 | map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH) | |
8012 | goto error; | |
8013 | break; | |
6ac99e8f MKL |
8014 | case BPF_FUNC_sk_storage_get: |
8015 | case BPF_FUNC_sk_storage_delete: | |
8016 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
8017 | goto error; | |
8018 | break; | |
8ea63684 KS |
8019 | case BPF_FUNC_inode_storage_get: |
8020 | case BPF_FUNC_inode_storage_delete: | |
8021 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
8022 | goto error; | |
8023 | break; | |
4cf1bc1f KS |
8024 | case BPF_FUNC_task_storage_get: |
8025 | case BPF_FUNC_task_storage_delete: | |
8026 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
8027 | goto error; | |
8028 | break; | |
c4bcfb38 YS |
8029 | case BPF_FUNC_cgrp_storage_get: |
8030 | case BPF_FUNC_cgrp_storage_delete: | |
8031 | if (map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) | |
8032 | goto error; | |
8033 | break; | |
6aff67c8 AS |
8034 | default: |
8035 | break; | |
35578d79 KX |
8036 | } |
8037 | ||
8038 | return 0; | |
6aff67c8 | 8039 | error: |
61bd5218 | 8040 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 8041 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 8042 | return -EINVAL; |
35578d79 KX |
8043 | } |
8044 | ||
90133415 | 8045 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
8046 | { |
8047 | int count = 0; | |
8048 | ||
39f19ebb | 8049 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 8050 | count++; |
39f19ebb | 8051 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 8052 | count++; |
39f19ebb | 8053 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 8054 | count++; |
39f19ebb | 8055 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 8056 | count++; |
39f19ebb | 8057 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
8058 | count++; |
8059 | ||
90133415 DB |
8060 | /* We only support one arg being in raw mode at the moment, |
8061 | * which is sufficient for the helper functions we have | |
8062 | * right now. | |
8063 | */ | |
8064 | return count <= 1; | |
8065 | } | |
8066 | ||
508362ac | 8067 | static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg) |
90133415 | 8068 | { |
508362ac MM |
8069 | bool is_fixed = fn->arg_type[arg] & MEM_FIXED_SIZE; |
8070 | bool has_size = fn->arg_size[arg] != 0; | |
8071 | bool is_next_size = false; | |
8072 | ||
8073 | if (arg + 1 < ARRAY_SIZE(fn->arg_type)) | |
8074 | is_next_size = arg_type_is_mem_size(fn->arg_type[arg + 1]); | |
8075 | ||
8076 | if (base_type(fn->arg_type[arg]) != ARG_PTR_TO_MEM) | |
8077 | return is_next_size; | |
8078 | ||
8079 | return has_size == is_next_size || is_next_size == is_fixed; | |
90133415 DB |
8080 | } |
8081 | ||
8082 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
8083 | { | |
8084 | /* bpf_xxx(..., buf, len) call will access 'len' | |
8085 | * bytes from memory 'buf'. Both arg types need | |
8086 | * to be paired, so make sure there's no buggy | |
8087 | * helper function specification. | |
8088 | */ | |
8089 | if (arg_type_is_mem_size(fn->arg1_type) || | |
508362ac MM |
8090 | check_args_pair_invalid(fn, 0) || |
8091 | check_args_pair_invalid(fn, 1) || | |
8092 | check_args_pair_invalid(fn, 2) || | |
8093 | check_args_pair_invalid(fn, 3) || | |
8094 | check_args_pair_invalid(fn, 4)) | |
90133415 DB |
8095 | return false; |
8096 | ||
8097 | return true; | |
8098 | } | |
8099 | ||
9436ef6e LB |
8100 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
8101 | { | |
8102 | int i; | |
8103 | ||
1df8f55a | 8104 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
4e814da0 KKD |
8105 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_BTF_ID) |
8106 | return !!fn->arg_btf_id[i]; | |
8107 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_SPIN_LOCK) | |
8108 | return fn->arg_btf_id[i] == BPF_PTR_POISON; | |
508362ac MM |
8109 | if (base_type(fn->arg_type[i]) != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i] && |
8110 | /* arg_btf_id and arg_size are in a union. */ | |
8111 | (base_type(fn->arg_type[i]) != ARG_PTR_TO_MEM || | |
8112 | !(fn->arg_type[i] & MEM_FIXED_SIZE))) | |
1df8f55a MKL |
8113 | return false; |
8114 | } | |
8115 | ||
9436ef6e LB |
8116 | return true; |
8117 | } | |
8118 | ||
0c9a7a7e | 8119 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
8120 | { |
8121 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 8122 | check_arg_pair_ok(fn) && |
b2d8ef19 | 8123 | check_btf_id_ok(fn) ? 0 : -EINVAL; |
435faee1 DB |
8124 | } |
8125 | ||
de8f3a83 DB |
8126 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
8127 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
66e3a13e JK |
8128 | * |
8129 | * This also applies to dynptr slices belonging to skb and xdp dynptrs, | |
8130 | * since these slices point to packet data. | |
f1174f77 | 8131 | */ |
b239da34 | 8132 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 8133 | { |
b239da34 KKD |
8134 | struct bpf_func_state *state; |
8135 | struct bpf_reg_state *reg; | |
969bf05e | 8136 | |
b239da34 | 8137 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
66e3a13e | 8138 | if (reg_is_pkt_pointer_any(reg) || reg_is_dynptr_slice_pkt(reg)) |
dbd8d228 | 8139 | mark_reg_invalid(env, reg); |
b239da34 | 8140 | })); |
f4d7e40a AS |
8141 | } |
8142 | ||
6d94e741 AS |
8143 | enum { |
8144 | AT_PKT_END = -1, | |
8145 | BEYOND_PKT_END = -2, | |
8146 | }; | |
8147 | ||
8148 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
8149 | { | |
8150 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
8151 | struct bpf_reg_state *reg = &state->regs[regn]; | |
8152 | ||
8153 | if (reg->type != PTR_TO_PACKET) | |
8154 | /* PTR_TO_PACKET_META is not supported yet */ | |
8155 | return; | |
8156 | ||
8157 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
8158 | * How far beyond pkt_end it goes is unknown. | |
8159 | * if (!range_open) it's the case of pkt >= pkt_end | |
8160 | * if (range_open) it's the case of pkt > pkt_end | |
8161 | * hence this pointer is at least 1 byte bigger than pkt_end | |
8162 | */ | |
8163 | if (range_open) | |
8164 | reg->range = BEYOND_PKT_END; | |
8165 | else | |
8166 | reg->range = AT_PKT_END; | |
8167 | } | |
8168 | ||
fd978bf7 JS |
8169 | /* The pointer with the specified id has released its reference to kernel |
8170 | * resources. Identify all copies of the same pointer and clear the reference. | |
8171 | */ | |
8172 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 8173 | int ref_obj_id) |
fd978bf7 | 8174 | { |
b239da34 KKD |
8175 | struct bpf_func_state *state; |
8176 | struct bpf_reg_state *reg; | |
1b986589 | 8177 | int err; |
fd978bf7 | 8178 | |
1b986589 MKL |
8179 | err = release_reference_state(cur_func(env), ref_obj_id); |
8180 | if (err) | |
8181 | return err; | |
8182 | ||
b239da34 | 8183 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
dbd8d228 KKD |
8184 | if (reg->ref_obj_id == ref_obj_id) |
8185 | mark_reg_invalid(env, reg); | |
b239da34 | 8186 | })); |
fd978bf7 | 8187 | |
1b986589 | 8188 | return 0; |
fd978bf7 JS |
8189 | } |
8190 | ||
6a3cd331 DM |
8191 | static void invalidate_non_owning_refs(struct bpf_verifier_env *env) |
8192 | { | |
8193 | struct bpf_func_state *unused; | |
8194 | struct bpf_reg_state *reg; | |
8195 | ||
8196 | bpf_for_each_reg_in_vstate(env->cur_state, unused, reg, ({ | |
8197 | if (type_is_non_owning_ref(reg->type)) | |
dbd8d228 | 8198 | mark_reg_invalid(env, reg); |
6a3cd331 DM |
8199 | })); |
8200 | } | |
8201 | ||
51c39bb1 AS |
8202 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
8203 | struct bpf_reg_state *regs) | |
8204 | { | |
8205 | int i; | |
8206 | ||
8207 | /* after the call registers r0 - r5 were scratched */ | |
8208 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
8209 | mark_reg_not_init(env, regs, caller_saved[i]); | |
8210 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
8211 | } | |
8212 | } | |
8213 | ||
14351375 YS |
8214 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
8215 | struct bpf_func_state *caller, | |
8216 | struct bpf_func_state *callee, | |
8217 | int insn_idx); | |
8218 | ||
be2ef816 AN |
8219 | static int set_callee_state(struct bpf_verifier_env *env, |
8220 | struct bpf_func_state *caller, | |
8221 | struct bpf_func_state *callee, int insn_idx); | |
8222 | ||
5d92ddc3 DM |
8223 | static bool is_callback_calling_kfunc(u32 btf_id); |
8224 | ||
14351375 YS |
8225 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8226 | int *insn_idx, int subprog, | |
8227 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
8228 | { |
8229 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 8230 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 8231 | struct bpf_func_state *caller, *callee; |
14351375 | 8232 | int err; |
51c39bb1 | 8233 | bool is_global = false; |
f4d7e40a | 8234 | |
aada9ce6 | 8235 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 8236 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 8237 | state->curframe + 2); |
f4d7e40a AS |
8238 | return -E2BIG; |
8239 | } | |
8240 | ||
f4d7e40a AS |
8241 | caller = state->frame[state->curframe]; |
8242 | if (state->frame[state->curframe + 1]) { | |
8243 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
8244 | state->curframe + 1); | |
8245 | return -EFAULT; | |
8246 | } | |
8247 | ||
51c39bb1 AS |
8248 | func_info_aux = env->prog->aux->func_info_aux; |
8249 | if (func_info_aux) | |
8250 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
95f2f26f | 8251 | err = btf_check_subprog_call(env, subprog, caller->regs); |
51c39bb1 AS |
8252 | if (err == -EFAULT) |
8253 | return err; | |
8254 | if (is_global) { | |
8255 | if (err) { | |
8256 | verbose(env, "Caller passes invalid args into func#%d\n", | |
8257 | subprog); | |
8258 | return err; | |
8259 | } else { | |
8260 | if (env->log.level & BPF_LOG_LEVEL) | |
8261 | verbose(env, | |
8262 | "Func#%d is global and valid. Skipping.\n", | |
8263 | subprog); | |
8264 | clear_caller_saved_regs(env, caller->regs); | |
8265 | ||
45159b27 | 8266 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 8267 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 8268 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
8269 | |
8270 | /* continue with next insn after call */ | |
8271 | return 0; | |
8272 | } | |
8273 | } | |
8274 | ||
be2ef816 AN |
8275 | /* set_callee_state is used for direct subprog calls, but we are |
8276 | * interested in validating only BPF helpers that can call subprogs as | |
8277 | * callbacks | |
8278 | */ | |
5d92ddc3 DM |
8279 | if (set_callee_state_cb != set_callee_state) { |
8280 | if (bpf_pseudo_kfunc_call(insn) && | |
8281 | !is_callback_calling_kfunc(insn->imm)) { | |
8282 | verbose(env, "verifier bug: kfunc %s#%d not marked as callback-calling\n", | |
8283 | func_id_name(insn->imm), insn->imm); | |
8284 | return -EFAULT; | |
8285 | } else if (!bpf_pseudo_kfunc_call(insn) && | |
8286 | !is_callback_calling_function(insn->imm)) { /* helper */ | |
8287 | verbose(env, "verifier bug: helper %s#%d not marked as callback-calling\n", | |
8288 | func_id_name(insn->imm), insn->imm); | |
8289 | return -EFAULT; | |
8290 | } | |
be2ef816 AN |
8291 | } |
8292 | ||
bfc6bb74 | 8293 | if (insn->code == (BPF_JMP | BPF_CALL) && |
a5bebc4f | 8294 | insn->src_reg == 0 && |
bfc6bb74 AS |
8295 | insn->imm == BPF_FUNC_timer_set_callback) { |
8296 | struct bpf_verifier_state *async_cb; | |
8297 | ||
8298 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 8299 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
8300 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
8301 | *insn_idx, subprog); | |
8302 | if (!async_cb) | |
8303 | return -EFAULT; | |
8304 | callee = async_cb->frame[0]; | |
8305 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
8306 | ||
8307 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
8308 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
8309 | if (err) | |
8310 | return err; | |
8311 | ||
8312 | clear_caller_saved_regs(env, caller->regs); | |
8313 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
8314 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
8315 | /* continue with next insn after call */ | |
8316 | return 0; | |
8317 | } | |
8318 | ||
f4d7e40a AS |
8319 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
8320 | if (!callee) | |
8321 | return -ENOMEM; | |
8322 | state->frame[state->curframe + 1] = callee; | |
8323 | ||
8324 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
8325 | * into its own stack before reading from it. | |
8326 | * callee can read/write into caller's stack | |
8327 | */ | |
8328 | init_func_state(env, callee, | |
8329 | /* remember the callsite, it will be used by bpf_exit */ | |
8330 | *insn_idx /* callsite */, | |
8331 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 8332 | subprog /* subprog number within this prog */); |
f4d7e40a | 8333 | |
fd978bf7 | 8334 | /* Transfer references to the callee */ |
c69431aa | 8335 | err = copy_reference_state(callee, caller); |
fd978bf7 | 8336 | if (err) |
eb86559a | 8337 | goto err_out; |
fd978bf7 | 8338 | |
14351375 YS |
8339 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
8340 | if (err) | |
eb86559a | 8341 | goto err_out; |
f4d7e40a | 8342 | |
51c39bb1 | 8343 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
8344 | |
8345 | /* only increment it after check_reg_arg() finished */ | |
8346 | state->curframe++; | |
8347 | ||
8348 | /* and go analyze first insn of the callee */ | |
14351375 | 8349 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 8350 | |
06ee7115 | 8351 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 8352 | verbose(env, "caller:\n"); |
0f55f9ed | 8353 | print_verifier_state(env, caller, true); |
f4d7e40a | 8354 | verbose(env, "callee:\n"); |
0f55f9ed | 8355 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
8356 | } |
8357 | return 0; | |
eb86559a WY |
8358 | |
8359 | err_out: | |
8360 | free_func_state(callee); | |
8361 | state->frame[state->curframe + 1] = NULL; | |
8362 | return err; | |
f4d7e40a AS |
8363 | } |
8364 | ||
314ee05e YS |
8365 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
8366 | struct bpf_func_state *caller, | |
8367 | struct bpf_func_state *callee) | |
8368 | { | |
8369 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
8370 | * void *callback_ctx, u64 flags); | |
8371 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
8372 | * void *callback_ctx); | |
8373 | */ | |
8374 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
8375 | ||
8376 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
8377 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8378 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8379 | ||
8380 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
8381 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
8382 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8383 | ||
8384 | /* pointer to stack or null */ | |
8385 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
8386 | ||
8387 | /* unused */ | |
8388 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8389 | return 0; | |
8390 | } | |
8391 | ||
14351375 YS |
8392 | static int set_callee_state(struct bpf_verifier_env *env, |
8393 | struct bpf_func_state *caller, | |
8394 | struct bpf_func_state *callee, int insn_idx) | |
8395 | { | |
8396 | int i; | |
8397 | ||
8398 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
8399 | * pointers, which connects us up to the liveness chain | |
8400 | */ | |
8401 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
8402 | callee->regs[i] = caller->regs[i]; | |
8403 | return 0; | |
8404 | } | |
8405 | ||
8406 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
8407 | int *insn_idx) | |
8408 | { | |
8409 | int subprog, target_insn; | |
8410 | ||
8411 | target_insn = *insn_idx + insn->imm + 1; | |
8412 | subprog = find_subprog(env, target_insn); | |
8413 | if (subprog < 0) { | |
8414 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
8415 | target_insn); | |
8416 | return -EFAULT; | |
8417 | } | |
8418 | ||
8419 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
8420 | } | |
8421 | ||
69c087ba YS |
8422 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
8423 | struct bpf_func_state *caller, | |
8424 | struct bpf_func_state *callee, | |
8425 | int insn_idx) | |
8426 | { | |
8427 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
8428 | struct bpf_map *map; | |
8429 | int err; | |
8430 | ||
8431 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
8432 | verbose(env, "tail_call abusing map_ptr\n"); | |
8433 | return -EINVAL; | |
8434 | } | |
8435 | ||
8436 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
8437 | if (!map->ops->map_set_for_each_callback_args || | |
8438 | !map->ops->map_for_each_callback) { | |
8439 | verbose(env, "callback function not allowed for map\n"); | |
8440 | return -ENOTSUPP; | |
8441 | } | |
8442 | ||
8443 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
8444 | if (err) | |
8445 | return err; | |
8446 | ||
8447 | callee->in_callback_fn = true; | |
1bfe26fb | 8448 | callee->callback_ret_range = tnum_range(0, 1); |
69c087ba YS |
8449 | return 0; |
8450 | } | |
8451 | ||
e6f2dd0f JK |
8452 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
8453 | struct bpf_func_state *caller, | |
8454 | struct bpf_func_state *callee, | |
8455 | int insn_idx) | |
8456 | { | |
8457 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
8458 | * u64 flags); | |
8459 | * callback_fn(u32 index, void *callback_ctx); | |
8460 | */ | |
8461 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
8462 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
8463 | ||
8464 | /* unused */ | |
8465 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8466 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8467 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8468 | ||
8469 | callee->in_callback_fn = true; | |
1bfe26fb | 8470 | callee->callback_ret_range = tnum_range(0, 1); |
e6f2dd0f JK |
8471 | return 0; |
8472 | } | |
8473 | ||
b00628b1 AS |
8474 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
8475 | struct bpf_func_state *caller, | |
8476 | struct bpf_func_state *callee, | |
8477 | int insn_idx) | |
8478 | { | |
8479 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
8480 | ||
8481 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
8482 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
8483 | */ | |
8484 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
8485 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
8486 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
8487 | ||
8488 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
8489 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8490 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
8491 | ||
8492 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
8493 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
8494 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
8495 | ||
8496 | /* unused */ | |
8497 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8498 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 8499 | callee->in_async_callback_fn = true; |
1bfe26fb | 8500 | callee->callback_ret_range = tnum_range(0, 1); |
b00628b1 AS |
8501 | return 0; |
8502 | } | |
8503 | ||
7c7e3d31 SL |
8504 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
8505 | struct bpf_func_state *caller, | |
8506 | struct bpf_func_state *callee, | |
8507 | int insn_idx) | |
8508 | { | |
8509 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
8510 | * void *callback_fn, void *callback_ctx, u64 flags) | |
8511 | * (callback_fn)(struct task_struct *task, | |
8512 | * struct vm_area_struct *vma, void *callback_ctx); | |
8513 | */ | |
8514 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
8515 | ||
8516 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
8517 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
8518 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 8519 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
8520 | |
8521 | /* pointer to stack or null */ | |
8522 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
8523 | ||
8524 | /* unused */ | |
8525 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8526 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8527 | callee->in_callback_fn = true; | |
1bfe26fb | 8528 | callee->callback_ret_range = tnum_range(0, 1); |
7c7e3d31 SL |
8529 | return 0; |
8530 | } | |
8531 | ||
20571567 DV |
8532 | static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env, |
8533 | struct bpf_func_state *caller, | |
8534 | struct bpf_func_state *callee, | |
8535 | int insn_idx) | |
8536 | { | |
8537 | /* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void | |
8538 | * callback_ctx, u64 flags); | |
27060531 | 8539 | * callback_fn(const struct bpf_dynptr_t* dynptr, void *callback_ctx); |
20571567 DV |
8540 | */ |
8541 | __mark_reg_not_init(env, &callee->regs[BPF_REG_0]); | |
f8064ab9 | 8542 | mark_dynptr_cb_reg(env, &callee->regs[BPF_REG_1], BPF_DYNPTR_TYPE_LOCAL); |
20571567 DV |
8543 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; |
8544 | ||
8545 | /* unused */ | |
8546 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8547 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8548 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8549 | ||
8550 | callee->in_callback_fn = true; | |
c92a7a52 | 8551 | callee->callback_ret_range = tnum_range(0, 1); |
20571567 DV |
8552 | return 0; |
8553 | } | |
8554 | ||
5d92ddc3 DM |
8555 | static int set_rbtree_add_callback_state(struct bpf_verifier_env *env, |
8556 | struct bpf_func_state *caller, | |
8557 | struct bpf_func_state *callee, | |
8558 | int insn_idx) | |
8559 | { | |
d2dcc67d | 8560 | /* void bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node, |
5d92ddc3 DM |
8561 | * bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b)); |
8562 | * | |
d2dcc67d | 8563 | * 'struct bpf_rb_node *node' arg to bpf_rbtree_add_impl is the same PTR_TO_BTF_ID w/ offset |
5d92ddc3 DM |
8564 | * that 'less' callback args will be receiving. However, 'node' arg was release_reference'd |
8565 | * by this point, so look at 'root' | |
8566 | */ | |
8567 | struct btf_field *field; | |
8568 | ||
8569 | field = reg_find_field_offset(&caller->regs[BPF_REG_1], caller->regs[BPF_REG_1].off, | |
8570 | BPF_RB_ROOT); | |
8571 | if (!field || !field->graph_root.value_btf_id) | |
8572 | return -EFAULT; | |
8573 | ||
8574 | mark_reg_graph_node(callee->regs, BPF_REG_1, &field->graph_root); | |
8575 | ref_set_non_owning(env, &callee->regs[BPF_REG_1]); | |
8576 | mark_reg_graph_node(callee->regs, BPF_REG_2, &field->graph_root); | |
8577 | ref_set_non_owning(env, &callee->regs[BPF_REG_2]); | |
8578 | ||
8579 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
8580 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
8581 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
8582 | callee->in_callback_fn = true; | |
8583 | callee->callback_ret_range = tnum_range(0, 1); | |
8584 | return 0; | |
8585 | } | |
8586 | ||
8587 | static bool is_rbtree_lock_required_kfunc(u32 btf_id); | |
8588 | ||
8589 | /* Are we currently verifying the callback for a rbtree helper that must | |
8590 | * be called with lock held? If so, no need to complain about unreleased | |
8591 | * lock | |
8592 | */ | |
8593 | static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env) | |
8594 | { | |
8595 | struct bpf_verifier_state *state = env->cur_state; | |
8596 | struct bpf_insn *insn = env->prog->insnsi; | |
8597 | struct bpf_func_state *callee; | |
8598 | int kfunc_btf_id; | |
8599 | ||
8600 | if (!state->curframe) | |
8601 | return false; | |
8602 | ||
8603 | callee = state->frame[state->curframe]; | |
8604 | ||
8605 | if (!callee->in_callback_fn) | |
8606 | return false; | |
8607 | ||
8608 | kfunc_btf_id = insn[callee->callsite].imm; | |
8609 | return is_rbtree_lock_required_kfunc(kfunc_btf_id); | |
8610 | } | |
8611 | ||
f4d7e40a AS |
8612 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
8613 | { | |
8614 | struct bpf_verifier_state *state = env->cur_state; | |
8615 | struct bpf_func_state *caller, *callee; | |
8616 | struct bpf_reg_state *r0; | |
fd978bf7 | 8617 | int err; |
f4d7e40a AS |
8618 | |
8619 | callee = state->frame[state->curframe]; | |
8620 | r0 = &callee->regs[BPF_REG_0]; | |
8621 | if (r0->type == PTR_TO_STACK) { | |
8622 | /* technically it's ok to return caller's stack pointer | |
8623 | * (or caller's caller's pointer) back to the caller, | |
8624 | * since these pointers are valid. Only current stack | |
8625 | * pointer will be invalid as soon as function exits, | |
8626 | * but let's be conservative | |
8627 | */ | |
8628 | verbose(env, "cannot return stack pointer to the caller\n"); | |
8629 | return -EINVAL; | |
8630 | } | |
8631 | ||
eb86559a | 8632 | caller = state->frame[state->curframe - 1]; |
69c087ba YS |
8633 | if (callee->in_callback_fn) { |
8634 | /* enforce R0 return value range [0, 1]. */ | |
1bfe26fb | 8635 | struct tnum range = callee->callback_ret_range; |
69c087ba YS |
8636 | |
8637 | if (r0->type != SCALAR_VALUE) { | |
8638 | verbose(env, "R0 not a scalar value\n"); | |
8639 | return -EACCES; | |
8640 | } | |
8641 | if (!tnum_in(range, r0->var_off)) { | |
8642 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
8643 | return -EINVAL; | |
8644 | } | |
8645 | } else { | |
8646 | /* return to the caller whatever r0 had in the callee */ | |
8647 | caller->regs[BPF_REG_0] = *r0; | |
8648 | } | |
f4d7e40a | 8649 | |
9d9d00ac KKD |
8650 | /* callback_fn frame should have released its own additions to parent's |
8651 | * reference state at this point, or check_reference_leak would | |
8652 | * complain, hence it must be the same as the caller. There is no need | |
8653 | * to copy it back. | |
8654 | */ | |
8655 | if (!callee->in_callback_fn) { | |
8656 | /* Transfer references to the caller */ | |
8657 | err = copy_reference_state(caller, callee); | |
8658 | if (err) | |
8659 | return err; | |
8660 | } | |
fd978bf7 | 8661 | |
f4d7e40a | 8662 | *insn_idx = callee->callsite + 1; |
06ee7115 | 8663 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 8664 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 8665 | print_verifier_state(env, callee, true); |
f4d7e40a | 8666 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 8667 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
8668 | } |
8669 | /* clear everything in the callee */ | |
8670 | free_func_state(callee); | |
eb86559a | 8671 | state->frame[state->curframe--] = NULL; |
f4d7e40a AS |
8672 | return 0; |
8673 | } | |
8674 | ||
849fa506 YS |
8675 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
8676 | int func_id, | |
8677 | struct bpf_call_arg_meta *meta) | |
8678 | { | |
8679 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
8680 | ||
8681 | if (ret_type != RET_INTEGER || | |
8682 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 8683 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
8684 | func_id != BPF_FUNC_probe_read_str && |
8685 | func_id != BPF_FUNC_probe_read_kernel_str && | |
8686 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
8687 | return; |
8688 | ||
10060503 | 8689 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 8690 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
8691 | ret_reg->smin_value = -MAX_ERRNO; |
8692 | ret_reg->s32_min_value = -MAX_ERRNO; | |
3844d153 | 8693 | reg_bounds_sync(ret_reg); |
849fa506 YS |
8694 | } |
8695 | ||
c93552c4 DB |
8696 | static int |
8697 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
8698 | int func_id, int insn_idx) | |
8699 | { | |
8700 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 8701 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
8702 | |
8703 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
8704 | func_id != BPF_FUNC_map_lookup_elem && |
8705 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
8706 | func_id != BPF_FUNC_map_delete_elem && |
8707 | func_id != BPF_FUNC_map_push_elem && | |
8708 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 8709 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f | 8710 | func_id != BPF_FUNC_for_each_map_elem && |
07343110 FZ |
8711 | func_id != BPF_FUNC_redirect_map && |
8712 | func_id != BPF_FUNC_map_lookup_percpu_elem) | |
c93552c4 | 8713 | return 0; |
09772d92 | 8714 | |
591fe988 | 8715 | if (map == NULL) { |
c93552c4 DB |
8716 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
8717 | return -EINVAL; | |
8718 | } | |
8719 | ||
591fe988 DB |
8720 | /* In case of read-only, some additional restrictions |
8721 | * need to be applied in order to prevent altering the | |
8722 | * state of the map from program side. | |
8723 | */ | |
8724 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
8725 | (func_id == BPF_FUNC_map_delete_elem || | |
8726 | func_id == BPF_FUNC_map_update_elem || | |
8727 | func_id == BPF_FUNC_map_push_elem || | |
8728 | func_id == BPF_FUNC_map_pop_elem)) { | |
8729 | verbose(env, "write into map forbidden\n"); | |
8730 | return -EACCES; | |
8731 | } | |
8732 | ||
d2e4c1e6 | 8733 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 8734 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 8735 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 8736 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 8737 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 8738 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
8739 | return 0; |
8740 | } | |
8741 | ||
d2e4c1e6 DB |
8742 | static int |
8743 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
8744 | int func_id, int insn_idx) | |
8745 | { | |
8746 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
8747 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
8748 | struct bpf_map *map = meta->map_ptr; | |
a657182a | 8749 | u64 val, max; |
cc52d914 | 8750 | int err; |
d2e4c1e6 DB |
8751 | |
8752 | if (func_id != BPF_FUNC_tail_call) | |
8753 | return 0; | |
8754 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
8755 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
8756 | return -EINVAL; | |
8757 | } | |
8758 | ||
d2e4c1e6 | 8759 | reg = ®s[BPF_REG_3]; |
a657182a DB |
8760 | val = reg->var_off.value; |
8761 | max = map->max_entries; | |
d2e4c1e6 | 8762 | |
a657182a | 8763 | if (!(register_is_const(reg) && val < max)) { |
d2e4c1e6 DB |
8764 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
8765 | return 0; | |
8766 | } | |
8767 | ||
cc52d914 DB |
8768 | err = mark_chain_precision(env, BPF_REG_3); |
8769 | if (err) | |
8770 | return err; | |
d2e4c1e6 DB |
8771 | if (bpf_map_key_unseen(aux)) |
8772 | bpf_map_key_store(aux, val); | |
8773 | else if (!bpf_map_key_poisoned(aux) && | |
8774 | bpf_map_key_immediate(aux) != val) | |
8775 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
8776 | return 0; | |
8777 | } | |
8778 | ||
fd978bf7 JS |
8779 | static int check_reference_leak(struct bpf_verifier_env *env) |
8780 | { | |
8781 | struct bpf_func_state *state = cur_func(env); | |
9d9d00ac | 8782 | bool refs_lingering = false; |
fd978bf7 JS |
8783 | int i; |
8784 | ||
9d9d00ac KKD |
8785 | if (state->frameno && !state->in_callback_fn) |
8786 | return 0; | |
8787 | ||
fd978bf7 | 8788 | for (i = 0; i < state->acquired_refs; i++) { |
9d9d00ac KKD |
8789 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) |
8790 | continue; | |
fd978bf7 JS |
8791 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
8792 | state->refs[i].id, state->refs[i].insn_idx); | |
9d9d00ac | 8793 | refs_lingering = true; |
fd978bf7 | 8794 | } |
9d9d00ac | 8795 | return refs_lingering ? -EINVAL : 0; |
fd978bf7 JS |
8796 | } |
8797 | ||
7b15523a FR |
8798 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
8799 | struct bpf_reg_state *regs) | |
8800 | { | |
8801 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
8802 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
8803 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
78aa1cc9 | 8804 | struct bpf_bprintf_data data = {}; |
7b15523a FR |
8805 | int err, fmt_map_off, num_args; |
8806 | u64 fmt_addr; | |
8807 | char *fmt; | |
8808 | ||
8809 | /* data must be an array of u64 */ | |
8810 | if (data_len_reg->var_off.value % 8) | |
8811 | return -EINVAL; | |
8812 | num_args = data_len_reg->var_off.value / 8; | |
8813 | ||
8814 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
8815 | * and map_direct_value_addr is set. | |
8816 | */ | |
8817 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
8818 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
8819 | fmt_map_off); | |
8e8ee109 FR |
8820 | if (err) { |
8821 | verbose(env, "verifier bug\n"); | |
8822 | return -EFAULT; | |
8823 | } | |
7b15523a FR |
8824 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
8825 | ||
8826 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
8827 | * can focus on validating the format specifiers. | |
8828 | */ | |
78aa1cc9 | 8829 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, num_args, &data); |
7b15523a FR |
8830 | if (err < 0) |
8831 | verbose(env, "Invalid format string\n"); | |
8832 | ||
8833 | return err; | |
8834 | } | |
8835 | ||
9b99edca JO |
8836 | static int check_get_func_ip(struct bpf_verifier_env *env) |
8837 | { | |
9b99edca JO |
8838 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
8839 | int func_id = BPF_FUNC_get_func_ip; | |
8840 | ||
8841 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 8842 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
8843 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
8844 | func_id_name(func_id), func_id); | |
8845 | return -ENOTSUPP; | |
8846 | } | |
8847 | return 0; | |
9ffd9f3f JO |
8848 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
8849 | return 0; | |
9b99edca JO |
8850 | } |
8851 | ||
8852 | verbose(env, "func %s#%d not supported for program type %d\n", | |
8853 | func_id_name(func_id), func_id, type); | |
8854 | return -ENOTSUPP; | |
8855 | } | |
8856 | ||
1ade2371 EZ |
8857 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
8858 | { | |
8859 | return &env->insn_aux_data[env->insn_idx]; | |
8860 | } | |
8861 | ||
8862 | static bool loop_flag_is_zero(struct bpf_verifier_env *env) | |
8863 | { | |
8864 | struct bpf_reg_state *regs = cur_regs(env); | |
8865 | struct bpf_reg_state *reg = ®s[BPF_REG_4]; | |
8866 | bool reg_is_null = register_is_null(reg); | |
8867 | ||
8868 | if (reg_is_null) | |
8869 | mark_chain_precision(env, BPF_REG_4); | |
8870 | ||
8871 | return reg_is_null; | |
8872 | } | |
8873 | ||
8874 | static void update_loop_inline_state(struct bpf_verifier_env *env, u32 subprogno) | |
8875 | { | |
8876 | struct bpf_loop_inline_state *state = &cur_aux(env)->loop_inline_state; | |
8877 | ||
8878 | if (!state->initialized) { | |
8879 | state->initialized = 1; | |
8880 | state->fit_for_inline = loop_flag_is_zero(env); | |
8881 | state->callback_subprogno = subprogno; | |
8882 | return; | |
8883 | } | |
8884 | ||
8885 | if (!state->fit_for_inline) | |
8886 | return; | |
8887 | ||
8888 | state->fit_for_inline = (loop_flag_is_zero(env) && | |
8889 | state->callback_subprogno == subprogno); | |
8890 | } | |
8891 | ||
69c087ba YS |
8892 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8893 | int *insn_idx_p) | |
17a52670 | 8894 | { |
aef9d4a3 | 8895 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
17a52670 | 8896 | const struct bpf_func_proto *fn = NULL; |
3c480732 | 8897 | enum bpf_return_type ret_type; |
c25b2ae1 | 8898 | enum bpf_type_flag ret_flag; |
638f5b90 | 8899 | struct bpf_reg_state *regs; |
33ff9823 | 8900 | struct bpf_call_arg_meta meta; |
69c087ba | 8901 | int insn_idx = *insn_idx_p; |
969bf05e | 8902 | bool changes_data; |
69c087ba | 8903 | int i, err, func_id; |
17a52670 AS |
8904 | |
8905 | /* find function prototype */ | |
69c087ba | 8906 | func_id = insn->imm; |
17a52670 | 8907 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
8908 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
8909 | func_id); | |
17a52670 AS |
8910 | return -EINVAL; |
8911 | } | |
8912 | ||
00176a34 | 8913 | if (env->ops->get_func_proto) |
5e43f899 | 8914 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 8915 | if (!fn) { |
61bd5218 JK |
8916 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
8917 | func_id); | |
17a52670 AS |
8918 | return -EINVAL; |
8919 | } | |
8920 | ||
8921 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 8922 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 8923 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
8924 | return -EINVAL; |
8925 | } | |
8926 | ||
eae2e83e JO |
8927 | if (fn->allowed && !fn->allowed(env->prog)) { |
8928 | verbose(env, "helper call is not allowed in probe\n"); | |
8929 | return -EINVAL; | |
8930 | } | |
8931 | ||
01685c5b YS |
8932 | if (!env->prog->aux->sleepable && fn->might_sleep) { |
8933 | verbose(env, "helper call might sleep in a non-sleepable prog\n"); | |
8934 | return -EINVAL; | |
8935 | } | |
8936 | ||
04514d13 | 8937 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 8938 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
8939 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
8940 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
8941 | func_id_name(func_id), func_id); | |
8942 | return -EINVAL; | |
8943 | } | |
969bf05e | 8944 | |
33ff9823 | 8945 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 8946 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 8947 | |
0c9a7a7e | 8948 | err = check_func_proto(fn, func_id); |
435faee1 | 8949 | if (err) { |
61bd5218 | 8950 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 8951 | func_id_name(func_id), func_id); |
435faee1 DB |
8952 | return err; |
8953 | } | |
8954 | ||
9bb00b28 YS |
8955 | if (env->cur_state->active_rcu_lock) { |
8956 | if (fn->might_sleep) { | |
8957 | verbose(env, "sleepable helper %s#%d in rcu_read_lock region\n", | |
8958 | func_id_name(func_id), func_id); | |
8959 | return -EINVAL; | |
8960 | } | |
8961 | ||
8962 | if (env->prog->aux->sleepable && is_storage_get_function(func_id)) | |
8963 | env->insn_aux_data[insn_idx].storage_get_func_atomic = true; | |
8964 | } | |
8965 | ||
d83525ca | 8966 | meta.func_id = func_id; |
17a52670 | 8967 | /* check args */ |
523a4cf4 | 8968 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
1d18feb2 | 8969 | err = check_func_arg(env, i, &meta, fn, insn_idx); |
a7658e1a AS |
8970 | if (err) |
8971 | return err; | |
8972 | } | |
17a52670 | 8973 | |
c93552c4 DB |
8974 | err = record_func_map(env, &meta, func_id, insn_idx); |
8975 | if (err) | |
8976 | return err; | |
8977 | ||
d2e4c1e6 DB |
8978 | err = record_func_key(env, &meta, func_id, insn_idx); |
8979 | if (err) | |
8980 | return err; | |
8981 | ||
435faee1 DB |
8982 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
8983 | * is inferred from register state. | |
8984 | */ | |
8985 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
8986 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
8987 | BPF_WRITE, -1, false); | |
435faee1 DB |
8988 | if (err) |
8989 | return err; | |
8990 | } | |
8991 | ||
8f14852e KKD |
8992 | regs = cur_regs(env); |
8993 | ||
8994 | if (meta.release_regno) { | |
8995 | err = -EINVAL; | |
27060531 KKD |
8996 | /* This can only be set for PTR_TO_STACK, as CONST_PTR_TO_DYNPTR cannot |
8997 | * be released by any dynptr helper. Hence, unmark_stack_slots_dynptr | |
8998 | * is safe to do directly. | |
8999 | */ | |
9000 | if (arg_type_is_dynptr(fn->arg_type[meta.release_regno - BPF_REG_1])) { | |
9001 | if (regs[meta.release_regno].type == CONST_PTR_TO_DYNPTR) { | |
9002 | verbose(env, "verifier internal error: CONST_PTR_TO_DYNPTR cannot be released\n"); | |
9003 | return -EFAULT; | |
9004 | } | |
97e03f52 | 9005 | err = unmark_stack_slots_dynptr(env, ®s[meta.release_regno]); |
27060531 | 9006 | } else if (meta.ref_obj_id) { |
8f14852e | 9007 | err = release_reference(env, meta.ref_obj_id); |
27060531 KKD |
9008 | } else if (register_is_null(®s[meta.release_regno])) { |
9009 | /* meta.ref_obj_id can only be 0 if register that is meant to be | |
9010 | * released is NULL, which must be > R0. | |
9011 | */ | |
8f14852e | 9012 | err = 0; |
27060531 | 9013 | } |
46f8bc92 MKL |
9014 | if (err) { |
9015 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
9016 | func_id_name(func_id), func_id); | |
fd978bf7 | 9017 | return err; |
46f8bc92 | 9018 | } |
fd978bf7 JS |
9019 | } |
9020 | ||
e6f2dd0f JK |
9021 | switch (func_id) { |
9022 | case BPF_FUNC_tail_call: | |
9023 | err = check_reference_leak(env); | |
9024 | if (err) { | |
9025 | verbose(env, "tail_call would lead to reference leak\n"); | |
9026 | return err; | |
9027 | } | |
9028 | break; | |
9029 | case BPF_FUNC_get_local_storage: | |
9030 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
9031 | * this is required because get_local_storage() can't return an error. | |
9032 | */ | |
9033 | if (!register_is_null(®s[BPF_REG_2])) { | |
9034 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
9035 | return -EINVAL; | |
9036 | } | |
9037 | break; | |
9038 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
9039 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
9040 | set_map_elem_callback_state); | |
e6f2dd0f JK |
9041 | break; |
9042 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
9043 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
9044 | set_timer_callback_state); | |
e6f2dd0f JK |
9045 | break; |
9046 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
9047 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
9048 | set_find_vma_callback_state); | |
e6f2dd0f JK |
9049 | break; |
9050 | case BPF_FUNC_snprintf: | |
7b15523a | 9051 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
9052 | break; |
9053 | case BPF_FUNC_loop: | |
1ade2371 | 9054 | update_loop_inline_state(env, meta.subprogno); |
e6f2dd0f JK |
9055 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
9056 | set_loop_callback_state); | |
9057 | break; | |
263ae152 JK |
9058 | case BPF_FUNC_dynptr_from_mem: |
9059 | if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) { | |
9060 | verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n", | |
9061 | reg_type_str(env, regs[BPF_REG_1].type)); | |
9062 | return -EACCES; | |
9063 | } | |
69fd337a SF |
9064 | break; |
9065 | case BPF_FUNC_set_retval: | |
aef9d4a3 SF |
9066 | if (prog_type == BPF_PROG_TYPE_LSM && |
9067 | env->prog->expected_attach_type == BPF_LSM_CGROUP) { | |
69fd337a SF |
9068 | if (!env->prog->aux->attach_func_proto->type) { |
9069 | /* Make sure programs that attach to void | |
9070 | * hooks don't try to modify return value. | |
9071 | */ | |
9072 | verbose(env, "BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
9073 | return -EINVAL; | |
9074 | } | |
9075 | } | |
9076 | break; | |
88374342 | 9077 | case BPF_FUNC_dynptr_data: |
485ec51e JK |
9078 | { |
9079 | struct bpf_reg_state *reg; | |
9080 | int id, ref_obj_id; | |
20571567 | 9081 | |
485ec51e JK |
9082 | reg = get_dynptr_arg_reg(env, fn, regs); |
9083 | if (!reg) | |
9084 | return -EFAULT; | |
f8064ab9 | 9085 | |
f8064ab9 | 9086 | |
485ec51e JK |
9087 | if (meta.dynptr_id) { |
9088 | verbose(env, "verifier internal error: meta.dynptr_id already set\n"); | |
9089 | return -EFAULT; | |
88374342 | 9090 | } |
485ec51e JK |
9091 | if (meta.ref_obj_id) { |
9092 | verbose(env, "verifier internal error: meta.ref_obj_id already set\n"); | |
88374342 JK |
9093 | return -EFAULT; |
9094 | } | |
485ec51e JK |
9095 | |
9096 | id = dynptr_id(env, reg); | |
9097 | if (id < 0) { | |
9098 | verbose(env, "verifier internal error: failed to obtain dynptr id\n"); | |
9099 | return id; | |
9100 | } | |
9101 | ||
9102 | ref_obj_id = dynptr_ref_obj_id(env, reg); | |
9103 | if (ref_obj_id < 0) { | |
9104 | verbose(env, "verifier internal error: failed to obtain dynptr ref_obj_id\n"); | |
9105 | return ref_obj_id; | |
9106 | } | |
9107 | ||
9108 | meta.dynptr_id = id; | |
9109 | meta.ref_obj_id = ref_obj_id; | |
9110 | ||
88374342 | 9111 | break; |
485ec51e | 9112 | } |
b5964b96 JK |
9113 | case BPF_FUNC_dynptr_write: |
9114 | { | |
9115 | enum bpf_dynptr_type dynptr_type; | |
9116 | struct bpf_reg_state *reg; | |
9117 | ||
9118 | reg = get_dynptr_arg_reg(env, fn, regs); | |
9119 | if (!reg) | |
9120 | return -EFAULT; | |
9121 | ||
9122 | dynptr_type = dynptr_get_type(env, reg); | |
9123 | if (dynptr_type == BPF_DYNPTR_TYPE_INVALID) | |
9124 | return -EFAULT; | |
9125 | ||
9126 | if (dynptr_type == BPF_DYNPTR_TYPE_SKB) | |
9127 | /* this will trigger clear_all_pkt_pointers(), which will | |
9128 | * invalidate all dynptr slices associated with the skb | |
9129 | */ | |
9130 | changes_data = true; | |
9131 | ||
9132 | break; | |
9133 | } | |
20571567 DV |
9134 | case BPF_FUNC_user_ringbuf_drain: |
9135 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
9136 | set_user_ringbuf_callback_state); | |
9137 | break; | |
7b15523a FR |
9138 | } |
9139 | ||
e6f2dd0f JK |
9140 | if (err) |
9141 | return err; | |
9142 | ||
17a52670 | 9143 | /* reset caller saved regs */ |
dc503a8a | 9144 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 9145 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
9146 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
9147 | } | |
17a52670 | 9148 | |
5327ed3d JW |
9149 | /* helper call returns 64-bit value. */ |
9150 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
9151 | ||
dc503a8a | 9152 | /* update return register (already marked as written above) */ |
3c480732 | 9153 | ret_type = fn->ret_type; |
0c9a7a7e JK |
9154 | ret_flag = type_flag(ret_type); |
9155 | ||
9156 | switch (base_type(ret_type)) { | |
9157 | case RET_INTEGER: | |
f1174f77 | 9158 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 9159 | mark_reg_unknown(env, regs, BPF_REG_0); |
0c9a7a7e JK |
9160 | break; |
9161 | case RET_VOID: | |
17a52670 | 9162 | regs[BPF_REG_0].type = NOT_INIT; |
0c9a7a7e JK |
9163 | break; |
9164 | case RET_PTR_TO_MAP_VALUE: | |
f1174f77 | 9165 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 9166 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
9167 | /* remember map_ptr, so that check_map_access() |
9168 | * can check 'value_size' boundary of memory access | |
9169 | * to map element returned from bpf_map_lookup_elem() | |
9170 | */ | |
33ff9823 | 9171 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
9172 | verbose(env, |
9173 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
9174 | return -EINVAL; |
9175 | } | |
33ff9823 | 9176 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 9177 | regs[BPF_REG_0].map_uid = meta.map_uid; |
c25b2ae1 HL |
9178 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
9179 | if (!type_may_be_null(ret_type) && | |
db559117 | 9180 | btf_record_has_field(meta.map_ptr->record, BPF_SPIN_LOCK)) { |
c25b2ae1 | 9181 | regs[BPF_REG_0].id = ++env->id_gen; |
4d31f301 | 9182 | } |
0c9a7a7e JK |
9183 | break; |
9184 | case RET_PTR_TO_SOCKET: | |
c64b7983 | 9185 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9186 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
0c9a7a7e JK |
9187 | break; |
9188 | case RET_PTR_TO_SOCK_COMMON: | |
85a51f8c | 9189 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9190 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
0c9a7a7e JK |
9191 | break; |
9192 | case RET_PTR_TO_TCP_SOCK: | |
655a51e5 | 9193 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9194 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
0c9a7a7e | 9195 | break; |
2de2669b | 9196 | case RET_PTR_TO_MEM: |
457f4436 | 9197 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 9198 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
457f4436 | 9199 | regs[BPF_REG_0].mem_size = meta.mem_size; |
0c9a7a7e JK |
9200 | break; |
9201 | case RET_PTR_TO_MEM_OR_BTF_ID: | |
9202 | { | |
eaa6bcb7 HL |
9203 | const struct btf_type *t; |
9204 | ||
9205 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 9206 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
9207 | if (!btf_type_is_struct(t)) { |
9208 | u32 tsize; | |
9209 | const struct btf_type *ret; | |
9210 | const char *tname; | |
9211 | ||
9212 | /* resolve the type size of ksym. */ | |
22dc4a0f | 9213 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 9214 | if (IS_ERR(ret)) { |
22dc4a0f | 9215 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
9216 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
9217 | tname, PTR_ERR(ret)); | |
9218 | return -EINVAL; | |
9219 | } | |
c25b2ae1 | 9220 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
eaa6bcb7 HL |
9221 | regs[BPF_REG_0].mem_size = tsize; |
9222 | } else { | |
34d3a78c HL |
9223 | /* MEM_RDONLY may be carried from ret_flag, but it |
9224 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise | |
9225 | * it will confuse the check of PTR_TO_BTF_ID in | |
9226 | * check_mem_access(). | |
9227 | */ | |
9228 | ret_flag &= ~MEM_RDONLY; | |
9229 | ||
c25b2ae1 | 9230 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
22dc4a0f | 9231 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
9232 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
9233 | } | |
0c9a7a7e JK |
9234 | break; |
9235 | } | |
9236 | case RET_PTR_TO_BTF_ID: | |
9237 | { | |
c0a5a21c | 9238 | struct btf *ret_btf; |
af7ec138 YS |
9239 | int ret_btf_id; |
9240 | ||
9241 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
c25b2ae1 | 9242 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
c0a5a21c | 9243 | if (func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac KKD |
9244 | ret_btf = meta.kptr_field->kptr.btf; |
9245 | ret_btf_id = meta.kptr_field->kptr.btf_id; | |
738c96d5 DM |
9246 | if (!btf_is_kernel(ret_btf)) |
9247 | regs[BPF_REG_0].type |= MEM_ALLOC; | |
c0a5a21c | 9248 | } else { |
47e34cb7 DM |
9249 | if (fn->ret_btf_id == BPF_PTR_POISON) { |
9250 | verbose(env, "verifier internal error:"); | |
9251 | verbose(env, "func %s has non-overwritten BPF_PTR_POISON return type\n", | |
9252 | func_id_name(func_id)); | |
9253 | return -EINVAL; | |
9254 | } | |
c0a5a21c KKD |
9255 | ret_btf = btf_vmlinux; |
9256 | ret_btf_id = *fn->ret_btf_id; | |
9257 | } | |
af7ec138 | 9258 | if (ret_btf_id == 0) { |
3c480732 HL |
9259 | verbose(env, "invalid return type %u of func %s#%d\n", |
9260 | base_type(ret_type), func_id_name(func_id), | |
9261 | func_id); | |
af7ec138 YS |
9262 | return -EINVAL; |
9263 | } | |
c0a5a21c | 9264 | regs[BPF_REG_0].btf = ret_btf; |
af7ec138 | 9265 | regs[BPF_REG_0].btf_id = ret_btf_id; |
0c9a7a7e JK |
9266 | break; |
9267 | } | |
9268 | default: | |
3c480732 HL |
9269 | verbose(env, "unknown return type %u of func %s#%d\n", |
9270 | base_type(ret_type), func_id_name(func_id), func_id); | |
17a52670 AS |
9271 | return -EINVAL; |
9272 | } | |
04fd61ab | 9273 | |
c25b2ae1 | 9274 | if (type_may_be_null(regs[BPF_REG_0].type)) |
93c230e3 MKL |
9275 | regs[BPF_REG_0].id = ++env->id_gen; |
9276 | ||
b2d8ef19 DM |
9277 | if (helper_multiple_ref_obj_use(func_id, meta.map_ptr)) { |
9278 | verbose(env, "verifier internal error: func %s#%d sets ref_obj_id more than once\n", | |
9279 | func_id_name(func_id), func_id); | |
9280 | return -EFAULT; | |
9281 | } | |
9282 | ||
f8064ab9 KKD |
9283 | if (is_dynptr_ref_function(func_id)) |
9284 | regs[BPF_REG_0].dynptr_id = meta.dynptr_id; | |
9285 | ||
88374342 | 9286 | if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) { |
1b986589 MKL |
9287 | /* For release_reference() */ |
9288 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 9289 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
9290 | int id = acquire_reference_state(env, insn_idx); |
9291 | ||
9292 | if (id < 0) | |
9293 | return id; | |
9294 | /* For mark_ptr_or_null_reg() */ | |
9295 | regs[BPF_REG_0].id = id; | |
9296 | /* For release_reference() */ | |
9297 | regs[BPF_REG_0].ref_obj_id = id; | |
9298 | } | |
1b986589 | 9299 | |
849fa506 YS |
9300 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
9301 | ||
61bd5218 | 9302 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
9303 | if (err) |
9304 | return err; | |
04fd61ab | 9305 | |
fa28dcb8 SL |
9306 | if ((func_id == BPF_FUNC_get_stack || |
9307 | func_id == BPF_FUNC_get_task_stack) && | |
9308 | !env->prog->has_callchain_buf) { | |
c195651e YS |
9309 | const char *err_str; |
9310 | ||
9311 | #ifdef CONFIG_PERF_EVENTS | |
9312 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
9313 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
9314 | #else | |
9315 | err = -ENOTSUPP; | |
9316 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
9317 | #endif | |
9318 | if (err) { | |
9319 | verbose(env, err_str, func_id_name(func_id), func_id); | |
9320 | return err; | |
9321 | } | |
9322 | ||
9323 | env->prog->has_callchain_buf = true; | |
9324 | } | |
9325 | ||
5d99cb2c SL |
9326 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
9327 | env->prog->call_get_stack = true; | |
9328 | ||
9b99edca JO |
9329 | if (func_id == BPF_FUNC_get_func_ip) { |
9330 | if (check_get_func_ip(env)) | |
9331 | return -ENOTSUPP; | |
9332 | env->prog->call_get_func_ip = true; | |
9333 | } | |
9334 | ||
969bf05e AS |
9335 | if (changes_data) |
9336 | clear_all_pkt_pointers(env); | |
9337 | return 0; | |
9338 | } | |
9339 | ||
e6ac2450 MKL |
9340 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
9341 | * the BTF func_proto's return value size and argument. | |
9342 | */ | |
9343 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
9344 | size_t reg_size) | |
9345 | { | |
9346 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
9347 | ||
9348 | if (regno == BPF_REG_0) { | |
9349 | /* Function return value */ | |
9350 | reg->live |= REG_LIVE_WRITTEN; | |
9351 | reg->subreg_def = reg_size == sizeof(u64) ? | |
9352 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
9353 | } else { | |
9354 | /* Function argument */ | |
9355 | if (reg_size == sizeof(u64)) { | |
9356 | mark_insn_zext(env, reg); | |
9357 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
9358 | } else { | |
9359 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
9360 | } | |
9361 | } | |
9362 | } | |
9363 | ||
00b85860 KKD |
9364 | static bool is_kfunc_acquire(struct bpf_kfunc_call_arg_meta *meta) |
9365 | { | |
9366 | return meta->kfunc_flags & KF_ACQUIRE; | |
9367 | } | |
a5d82727 | 9368 | |
00b85860 KKD |
9369 | static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) |
9370 | { | |
9371 | return meta->kfunc_flags & KF_RET_NULL; | |
9372 | } | |
2357672c | 9373 | |
00b85860 KKD |
9374 | static bool is_kfunc_release(struct bpf_kfunc_call_arg_meta *meta) |
9375 | { | |
9376 | return meta->kfunc_flags & KF_RELEASE; | |
9377 | } | |
e6ac2450 | 9378 | |
00b85860 KKD |
9379 | static bool is_kfunc_trusted_args(struct bpf_kfunc_call_arg_meta *meta) |
9380 | { | |
6c831c46 | 9381 | return (meta->kfunc_flags & KF_TRUSTED_ARGS) || is_kfunc_release(meta); |
00b85860 | 9382 | } |
4dd48c6f | 9383 | |
00b85860 KKD |
9384 | static bool is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta) |
9385 | { | |
9386 | return meta->kfunc_flags & KF_SLEEPABLE; | |
9387 | } | |
5c073f26 | 9388 | |
00b85860 KKD |
9389 | static bool is_kfunc_destructive(struct bpf_kfunc_call_arg_meta *meta) |
9390 | { | |
9391 | return meta->kfunc_flags & KF_DESTRUCTIVE; | |
9392 | } | |
eb1f7f71 | 9393 | |
fca1aa75 YS |
9394 | static bool is_kfunc_rcu(struct bpf_kfunc_call_arg_meta *meta) |
9395 | { | |
9396 | return meta->kfunc_flags & KF_RCU; | |
9397 | } | |
9398 | ||
a50388db KKD |
9399 | static bool __kfunc_param_match_suffix(const struct btf *btf, |
9400 | const struct btf_param *arg, | |
9401 | const char *suffix) | |
00b85860 | 9402 | { |
a50388db | 9403 | int suffix_len = strlen(suffix), len; |
00b85860 | 9404 | const char *param_name; |
e6ac2450 | 9405 | |
00b85860 KKD |
9406 | /* In the future, this can be ported to use BTF tagging */ |
9407 | param_name = btf_name_by_offset(btf, arg->name_off); | |
9408 | if (str_is_empty(param_name)) | |
9409 | return false; | |
9410 | len = strlen(param_name); | |
a50388db | 9411 | if (len < suffix_len) |
00b85860 | 9412 | return false; |
a50388db KKD |
9413 | param_name += len - suffix_len; |
9414 | return !strncmp(param_name, suffix, suffix_len); | |
9415 | } | |
5c073f26 | 9416 | |
a50388db KKD |
9417 | static bool is_kfunc_arg_mem_size(const struct btf *btf, |
9418 | const struct btf_param *arg, | |
9419 | const struct bpf_reg_state *reg) | |
9420 | { | |
9421 | const struct btf_type *t; | |
5c073f26 | 9422 | |
a50388db KKD |
9423 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
9424 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
00b85860 | 9425 | return false; |
eb1f7f71 | 9426 | |
a50388db KKD |
9427 | return __kfunc_param_match_suffix(btf, arg, "__sz"); |
9428 | } | |
eb1f7f71 | 9429 | |
66e3a13e JK |
9430 | static bool is_kfunc_arg_const_mem_size(const struct btf *btf, |
9431 | const struct btf_param *arg, | |
9432 | const struct bpf_reg_state *reg) | |
9433 | { | |
9434 | const struct btf_type *t; | |
9435 | ||
9436 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
9437 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
9438 | return false; | |
9439 | ||
9440 | return __kfunc_param_match_suffix(btf, arg, "__szk"); | |
9441 | } | |
9442 | ||
a50388db KKD |
9443 | static bool is_kfunc_arg_constant(const struct btf *btf, const struct btf_param *arg) |
9444 | { | |
9445 | return __kfunc_param_match_suffix(btf, arg, "__k"); | |
00b85860 | 9446 | } |
eb1f7f71 | 9447 | |
958cf2e2 KKD |
9448 | static bool is_kfunc_arg_ignore(const struct btf *btf, const struct btf_param *arg) |
9449 | { | |
9450 | return __kfunc_param_match_suffix(btf, arg, "__ign"); | |
9451 | } | |
5c073f26 | 9452 | |
ac9f0605 KKD |
9453 | static bool is_kfunc_arg_alloc_obj(const struct btf *btf, const struct btf_param *arg) |
9454 | { | |
9455 | return __kfunc_param_match_suffix(btf, arg, "__alloc"); | |
9456 | } | |
e6ac2450 | 9457 | |
d96d937d JK |
9458 | static bool is_kfunc_arg_uninit(const struct btf *btf, const struct btf_param *arg) |
9459 | { | |
9460 | return __kfunc_param_match_suffix(btf, arg, "__uninit"); | |
9461 | } | |
9462 | ||
7c50b1cb DM |
9463 | static bool is_kfunc_arg_refcounted_kptr(const struct btf *btf, const struct btf_param *arg) |
9464 | { | |
9465 | return __kfunc_param_match_suffix(btf, arg, "__refcounted_kptr"); | |
9466 | } | |
9467 | ||
00b85860 KKD |
9468 | static bool is_kfunc_arg_scalar_with_name(const struct btf *btf, |
9469 | const struct btf_param *arg, | |
9470 | const char *name) | |
9471 | { | |
9472 | int len, target_len = strlen(name); | |
9473 | const char *param_name; | |
e6ac2450 | 9474 | |
00b85860 KKD |
9475 | param_name = btf_name_by_offset(btf, arg->name_off); |
9476 | if (str_is_empty(param_name)) | |
9477 | return false; | |
9478 | len = strlen(param_name); | |
9479 | if (len != target_len) | |
9480 | return false; | |
9481 | if (strcmp(param_name, name)) | |
9482 | return false; | |
e6ac2450 | 9483 | |
00b85860 | 9484 | return true; |
e6ac2450 MKL |
9485 | } |
9486 | ||
00b85860 KKD |
9487 | enum { |
9488 | KF_ARG_DYNPTR_ID, | |
8cab76ec KKD |
9489 | KF_ARG_LIST_HEAD_ID, |
9490 | KF_ARG_LIST_NODE_ID, | |
cd6791b4 DM |
9491 | KF_ARG_RB_ROOT_ID, |
9492 | KF_ARG_RB_NODE_ID, | |
00b85860 | 9493 | }; |
b03c9f9f | 9494 | |
00b85860 KKD |
9495 | BTF_ID_LIST(kf_arg_btf_ids) |
9496 | BTF_ID(struct, bpf_dynptr_kern) | |
8cab76ec KKD |
9497 | BTF_ID(struct, bpf_list_head) |
9498 | BTF_ID(struct, bpf_list_node) | |
bd1279ae DM |
9499 | BTF_ID(struct, bpf_rb_root) |
9500 | BTF_ID(struct, bpf_rb_node) | |
b03c9f9f | 9501 | |
8cab76ec KKD |
9502 | static bool __is_kfunc_ptr_arg_type(const struct btf *btf, |
9503 | const struct btf_param *arg, int type) | |
3f50f132 | 9504 | { |
00b85860 KKD |
9505 | const struct btf_type *t; |
9506 | u32 res_id; | |
3f50f132 | 9507 | |
00b85860 KKD |
9508 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
9509 | if (!t) | |
9510 | return false; | |
9511 | if (!btf_type_is_ptr(t)) | |
9512 | return false; | |
9513 | t = btf_type_skip_modifiers(btf, t->type, &res_id); | |
9514 | if (!t) | |
9515 | return false; | |
8cab76ec | 9516 | return btf_types_are_same(btf, res_id, btf_vmlinux, kf_arg_btf_ids[type]); |
3f50f132 JF |
9517 | } |
9518 | ||
8cab76ec | 9519 | static bool is_kfunc_arg_dynptr(const struct btf *btf, const struct btf_param *arg) |
b03c9f9f | 9520 | { |
8cab76ec | 9521 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_DYNPTR_ID); |
969bf05e AS |
9522 | } |
9523 | ||
8cab76ec | 9524 | static bool is_kfunc_arg_list_head(const struct btf *btf, const struct btf_param *arg) |
3f50f132 | 9525 | { |
8cab76ec | 9526 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_HEAD_ID); |
3f50f132 JF |
9527 | } |
9528 | ||
8cab76ec | 9529 | static bool is_kfunc_arg_list_node(const struct btf *btf, const struct btf_param *arg) |
bb7f0f98 | 9530 | { |
8cab76ec | 9531 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_NODE_ID); |
00b85860 KKD |
9532 | } |
9533 | ||
cd6791b4 DM |
9534 | static bool is_kfunc_arg_rbtree_root(const struct btf *btf, const struct btf_param *arg) |
9535 | { | |
9536 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_ROOT_ID); | |
9537 | } | |
9538 | ||
9539 | static bool is_kfunc_arg_rbtree_node(const struct btf *btf, const struct btf_param *arg) | |
9540 | { | |
9541 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_NODE_ID); | |
9542 | } | |
9543 | ||
5d92ddc3 DM |
9544 | static bool is_kfunc_arg_callback(struct bpf_verifier_env *env, const struct btf *btf, |
9545 | const struct btf_param *arg) | |
9546 | { | |
9547 | const struct btf_type *t; | |
9548 | ||
9549 | t = btf_type_resolve_func_ptr(btf, arg->type, NULL); | |
9550 | if (!t) | |
9551 | return false; | |
9552 | ||
9553 | return true; | |
9554 | } | |
9555 | ||
00b85860 KKD |
9556 | /* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ |
9557 | static bool __btf_type_is_scalar_struct(struct bpf_verifier_env *env, | |
9558 | const struct btf *btf, | |
9559 | const struct btf_type *t, int rec) | |
9560 | { | |
9561 | const struct btf_type *member_type; | |
9562 | const struct btf_member *member; | |
9563 | u32 i; | |
9564 | ||
9565 | if (!btf_type_is_struct(t)) | |
9566 | return false; | |
9567 | ||
9568 | for_each_member(i, t, member) { | |
9569 | const struct btf_array *array; | |
9570 | ||
9571 | member_type = btf_type_skip_modifiers(btf, member->type, NULL); | |
9572 | if (btf_type_is_struct(member_type)) { | |
9573 | if (rec >= 3) { | |
9574 | verbose(env, "max struct nesting depth exceeded\n"); | |
9575 | return false; | |
9576 | } | |
9577 | if (!__btf_type_is_scalar_struct(env, btf, member_type, rec + 1)) | |
9578 | return false; | |
9579 | continue; | |
9580 | } | |
9581 | if (btf_type_is_array(member_type)) { | |
9582 | array = btf_array(member_type); | |
9583 | if (!array->nelems) | |
9584 | return false; | |
9585 | member_type = btf_type_skip_modifiers(btf, array->type, NULL); | |
9586 | if (!btf_type_is_scalar(member_type)) | |
9587 | return false; | |
9588 | continue; | |
9589 | } | |
9590 | if (!btf_type_is_scalar(member_type)) | |
9591 | return false; | |
9592 | } | |
9593 | return true; | |
9594 | } | |
9595 | ||
9596 | ||
9597 | static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { | |
9598 | #ifdef CONFIG_NET | |
9599 | [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], | |
9600 | [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
9601 | [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], | |
9602 | #endif | |
9603 | }; | |
9604 | ||
9605 | enum kfunc_ptr_arg_type { | |
9606 | KF_ARG_PTR_TO_CTX, | |
7c50b1cb DM |
9607 | KF_ARG_PTR_TO_ALLOC_BTF_ID, /* Allocated object */ |
9608 | KF_ARG_PTR_TO_REFCOUNTED_KPTR, /* Refcounted local kptr */ | |
00b85860 | 9609 | KF_ARG_PTR_TO_DYNPTR, |
06accc87 | 9610 | KF_ARG_PTR_TO_ITER, |
8cab76ec KKD |
9611 | KF_ARG_PTR_TO_LIST_HEAD, |
9612 | KF_ARG_PTR_TO_LIST_NODE, | |
7c50b1cb | 9613 | KF_ARG_PTR_TO_BTF_ID, /* Also covers reg2btf_ids conversions */ |
00b85860 | 9614 | KF_ARG_PTR_TO_MEM, |
7c50b1cb | 9615 | KF_ARG_PTR_TO_MEM_SIZE, /* Size derived from next argument, skip it */ |
5d92ddc3 | 9616 | KF_ARG_PTR_TO_CALLBACK, |
cd6791b4 DM |
9617 | KF_ARG_PTR_TO_RB_ROOT, |
9618 | KF_ARG_PTR_TO_RB_NODE, | |
00b85860 KKD |
9619 | }; |
9620 | ||
ac9f0605 KKD |
9621 | enum special_kfunc_type { |
9622 | KF_bpf_obj_new_impl, | |
9623 | KF_bpf_obj_drop_impl, | |
7c50b1cb | 9624 | KF_bpf_refcount_acquire_impl, |
d2dcc67d DM |
9625 | KF_bpf_list_push_front_impl, |
9626 | KF_bpf_list_push_back_impl, | |
8cab76ec KKD |
9627 | KF_bpf_list_pop_front, |
9628 | KF_bpf_list_pop_back, | |
fd264ca0 | 9629 | KF_bpf_cast_to_kern_ctx, |
a35b9af4 | 9630 | KF_bpf_rdonly_cast, |
9bb00b28 YS |
9631 | KF_bpf_rcu_read_lock, |
9632 | KF_bpf_rcu_read_unlock, | |
bd1279ae | 9633 | KF_bpf_rbtree_remove, |
d2dcc67d | 9634 | KF_bpf_rbtree_add_impl, |
bd1279ae | 9635 | KF_bpf_rbtree_first, |
b5964b96 | 9636 | KF_bpf_dynptr_from_skb, |
05421aec | 9637 | KF_bpf_dynptr_from_xdp, |
66e3a13e JK |
9638 | KF_bpf_dynptr_slice, |
9639 | KF_bpf_dynptr_slice_rdwr, | |
361f129f | 9640 | KF_bpf_dynptr_clone, |
ac9f0605 KKD |
9641 | }; |
9642 | ||
9643 | BTF_SET_START(special_kfunc_set) | |
9644 | BTF_ID(func, bpf_obj_new_impl) | |
9645 | BTF_ID(func, bpf_obj_drop_impl) | |
7c50b1cb | 9646 | BTF_ID(func, bpf_refcount_acquire_impl) |
d2dcc67d DM |
9647 | BTF_ID(func, bpf_list_push_front_impl) |
9648 | BTF_ID(func, bpf_list_push_back_impl) | |
8cab76ec KKD |
9649 | BTF_ID(func, bpf_list_pop_front) |
9650 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 9651 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 9652 | BTF_ID(func, bpf_rdonly_cast) |
bd1279ae | 9653 | BTF_ID(func, bpf_rbtree_remove) |
d2dcc67d | 9654 | BTF_ID(func, bpf_rbtree_add_impl) |
bd1279ae | 9655 | BTF_ID(func, bpf_rbtree_first) |
b5964b96 | 9656 | BTF_ID(func, bpf_dynptr_from_skb) |
05421aec | 9657 | BTF_ID(func, bpf_dynptr_from_xdp) |
66e3a13e JK |
9658 | BTF_ID(func, bpf_dynptr_slice) |
9659 | BTF_ID(func, bpf_dynptr_slice_rdwr) | |
361f129f | 9660 | BTF_ID(func, bpf_dynptr_clone) |
ac9f0605 KKD |
9661 | BTF_SET_END(special_kfunc_set) |
9662 | ||
9663 | BTF_ID_LIST(special_kfunc_list) | |
9664 | BTF_ID(func, bpf_obj_new_impl) | |
9665 | BTF_ID(func, bpf_obj_drop_impl) | |
7c50b1cb | 9666 | BTF_ID(func, bpf_refcount_acquire_impl) |
d2dcc67d DM |
9667 | BTF_ID(func, bpf_list_push_front_impl) |
9668 | BTF_ID(func, bpf_list_push_back_impl) | |
8cab76ec KKD |
9669 | BTF_ID(func, bpf_list_pop_front) |
9670 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 9671 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 9672 | BTF_ID(func, bpf_rdonly_cast) |
9bb00b28 YS |
9673 | BTF_ID(func, bpf_rcu_read_lock) |
9674 | BTF_ID(func, bpf_rcu_read_unlock) | |
bd1279ae | 9675 | BTF_ID(func, bpf_rbtree_remove) |
d2dcc67d | 9676 | BTF_ID(func, bpf_rbtree_add_impl) |
bd1279ae | 9677 | BTF_ID(func, bpf_rbtree_first) |
b5964b96 | 9678 | BTF_ID(func, bpf_dynptr_from_skb) |
05421aec | 9679 | BTF_ID(func, bpf_dynptr_from_xdp) |
66e3a13e JK |
9680 | BTF_ID(func, bpf_dynptr_slice) |
9681 | BTF_ID(func, bpf_dynptr_slice_rdwr) | |
361f129f | 9682 | BTF_ID(func, bpf_dynptr_clone) |
9bb00b28 YS |
9683 | |
9684 | static bool is_kfunc_bpf_rcu_read_lock(struct bpf_kfunc_call_arg_meta *meta) | |
9685 | { | |
9686 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_lock]; | |
9687 | } | |
9688 | ||
9689 | static bool is_kfunc_bpf_rcu_read_unlock(struct bpf_kfunc_call_arg_meta *meta) | |
9690 | { | |
9691 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_unlock]; | |
9692 | } | |
ac9f0605 | 9693 | |
00b85860 KKD |
9694 | static enum kfunc_ptr_arg_type |
9695 | get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, | |
9696 | struct bpf_kfunc_call_arg_meta *meta, | |
9697 | const struct btf_type *t, const struct btf_type *ref_t, | |
9698 | const char *ref_tname, const struct btf_param *args, | |
9699 | int argno, int nargs) | |
9700 | { | |
9701 | u32 regno = argno + 1; | |
9702 | struct bpf_reg_state *regs = cur_regs(env); | |
9703 | struct bpf_reg_state *reg = ®s[regno]; | |
9704 | bool arg_mem_size = false; | |
9705 | ||
fd264ca0 YS |
9706 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) |
9707 | return KF_ARG_PTR_TO_CTX; | |
9708 | ||
00b85860 KKD |
9709 | /* In this function, we verify the kfunc's BTF as per the argument type, |
9710 | * leaving the rest of the verification with respect to the register | |
9711 | * type to our caller. When a set of conditions hold in the BTF type of | |
9712 | * arguments, we resolve it to a known kfunc_ptr_arg_type. | |
9713 | */ | |
9714 | if (btf_get_prog_ctx_type(&env->log, meta->btf, t, resolve_prog_type(env->prog), argno)) | |
9715 | return KF_ARG_PTR_TO_CTX; | |
9716 | ||
ac9f0605 KKD |
9717 | if (is_kfunc_arg_alloc_obj(meta->btf, &args[argno])) |
9718 | return KF_ARG_PTR_TO_ALLOC_BTF_ID; | |
9719 | ||
7c50b1cb DM |
9720 | if (is_kfunc_arg_refcounted_kptr(meta->btf, &args[argno])) |
9721 | return KF_ARG_PTR_TO_REFCOUNTED_KPTR; | |
00b85860 KKD |
9722 | |
9723 | if (is_kfunc_arg_dynptr(meta->btf, &args[argno])) | |
9724 | return KF_ARG_PTR_TO_DYNPTR; | |
9725 | ||
06accc87 AN |
9726 | if (is_kfunc_arg_iter(meta, argno)) |
9727 | return KF_ARG_PTR_TO_ITER; | |
9728 | ||
8cab76ec KKD |
9729 | if (is_kfunc_arg_list_head(meta->btf, &args[argno])) |
9730 | return KF_ARG_PTR_TO_LIST_HEAD; | |
9731 | ||
9732 | if (is_kfunc_arg_list_node(meta->btf, &args[argno])) | |
9733 | return KF_ARG_PTR_TO_LIST_NODE; | |
9734 | ||
cd6791b4 DM |
9735 | if (is_kfunc_arg_rbtree_root(meta->btf, &args[argno])) |
9736 | return KF_ARG_PTR_TO_RB_ROOT; | |
9737 | ||
9738 | if (is_kfunc_arg_rbtree_node(meta->btf, &args[argno])) | |
9739 | return KF_ARG_PTR_TO_RB_NODE; | |
9740 | ||
00b85860 KKD |
9741 | if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { |
9742 | if (!btf_type_is_struct(ref_t)) { | |
9743 | verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n", | |
9744 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
9745 | return -EINVAL; | |
9746 | } | |
9747 | return KF_ARG_PTR_TO_BTF_ID; | |
9748 | } | |
9749 | ||
5d92ddc3 DM |
9750 | if (is_kfunc_arg_callback(env, meta->btf, &args[argno])) |
9751 | return KF_ARG_PTR_TO_CALLBACK; | |
9752 | ||
66e3a13e JK |
9753 | |
9754 | if (argno + 1 < nargs && | |
9755 | (is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1]) || | |
9756 | is_kfunc_arg_const_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1]))) | |
00b85860 KKD |
9757 | arg_mem_size = true; |
9758 | ||
9759 | /* This is the catch all argument type of register types supported by | |
9760 | * check_helper_mem_access. However, we only allow when argument type is | |
9761 | * pointer to scalar, or struct composed (recursively) of scalars. When | |
9762 | * arg_mem_size is true, the pointer can be void *. | |
9763 | */ | |
9764 | if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(env, meta->btf, ref_t, 0) && | |
9765 | (arg_mem_size ? !btf_type_is_void(ref_t) : 1)) { | |
9766 | verbose(env, "arg#%d pointer type %s %s must point to %sscalar, or struct with scalar\n", | |
9767 | argno, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : ""); | |
9768 | return -EINVAL; | |
9769 | } | |
9770 | return arg_mem_size ? KF_ARG_PTR_TO_MEM_SIZE : KF_ARG_PTR_TO_MEM; | |
9771 | } | |
9772 | ||
9773 | static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env, | |
9774 | struct bpf_reg_state *reg, | |
9775 | const struct btf_type *ref_t, | |
9776 | const char *ref_tname, u32 ref_id, | |
9777 | struct bpf_kfunc_call_arg_meta *meta, | |
9778 | int argno) | |
9779 | { | |
9780 | const struct btf_type *reg_ref_t; | |
9781 | bool strict_type_match = false; | |
9782 | const struct btf *reg_btf; | |
9783 | const char *reg_ref_tname; | |
9784 | u32 reg_ref_id; | |
9785 | ||
3f00c523 | 9786 | if (base_type(reg->type) == PTR_TO_BTF_ID) { |
00b85860 KKD |
9787 | reg_btf = reg->btf; |
9788 | reg_ref_id = reg->btf_id; | |
9789 | } else { | |
9790 | reg_btf = btf_vmlinux; | |
9791 | reg_ref_id = *reg2btf_ids[base_type(reg->type)]; | |
9792 | } | |
9793 | ||
b613d335 DV |
9794 | /* Enforce strict type matching for calls to kfuncs that are acquiring |
9795 | * or releasing a reference, or are no-cast aliases. We do _not_ | |
9796 | * enforce strict matching for plain KF_TRUSTED_ARGS kfuncs by default, | |
9797 | * as we want to enable BPF programs to pass types that are bitwise | |
9798 | * equivalent without forcing them to explicitly cast with something | |
9799 | * like bpf_cast_to_kern_ctx(). | |
9800 | * | |
9801 | * For example, say we had a type like the following: | |
9802 | * | |
9803 | * struct bpf_cpumask { | |
9804 | * cpumask_t cpumask; | |
9805 | * refcount_t usage; | |
9806 | * }; | |
9807 | * | |
9808 | * Note that as specified in <linux/cpumask.h>, cpumask_t is typedef'ed | |
9809 | * to a struct cpumask, so it would be safe to pass a struct | |
9810 | * bpf_cpumask * to a kfunc expecting a struct cpumask *. | |
9811 | * | |
9812 | * The philosophy here is similar to how we allow scalars of different | |
9813 | * types to be passed to kfuncs as long as the size is the same. The | |
9814 | * only difference here is that we're simply allowing | |
9815 | * btf_struct_ids_match() to walk the struct at the 0th offset, and | |
9816 | * resolve types. | |
9817 | */ | |
9818 | if (is_kfunc_acquire(meta) || | |
9819 | (is_kfunc_release(meta) && reg->ref_obj_id) || | |
9820 | btf_type_ids_nocast_alias(&env->log, reg_btf, reg_ref_id, meta->btf, ref_id)) | |
00b85860 KKD |
9821 | strict_type_match = true; |
9822 | ||
b613d335 DV |
9823 | WARN_ON_ONCE(is_kfunc_trusted_args(meta) && reg->off); |
9824 | ||
00b85860 KKD |
9825 | reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id, ®_ref_id); |
9826 | reg_ref_tname = btf_name_by_offset(reg_btf, reg_ref_t->name_off); | |
9827 | if (!btf_struct_ids_match(&env->log, reg_btf, reg_ref_id, reg->off, meta->btf, ref_id, strict_type_match)) { | |
9828 | verbose(env, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", | |
9829 | meta->func_name, argno, btf_type_str(ref_t), ref_tname, argno + 1, | |
9830 | btf_type_str(reg_ref_t), reg_ref_tname); | |
9831 | return -EINVAL; | |
9832 | } | |
9833 | return 0; | |
9834 | } | |
9835 | ||
6a3cd331 | 9836 | static int ref_set_non_owning(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
534e86bc | 9837 | { |
6a3cd331 DM |
9838 | struct bpf_verifier_state *state = env->cur_state; |
9839 | ||
9840 | if (!state->active_lock.ptr) { | |
9841 | verbose(env, "verifier internal error: ref_set_non_owning w/o active lock\n"); | |
9842 | return -EFAULT; | |
9843 | } | |
9844 | ||
9845 | if (type_flag(reg->type) & NON_OWN_REF) { | |
9846 | verbose(env, "verifier internal error: NON_OWN_REF already set\n"); | |
9847 | return -EFAULT; | |
9848 | } | |
9849 | ||
9850 | reg->type |= NON_OWN_REF; | |
9851 | return 0; | |
9852 | } | |
9853 | ||
9854 | static int ref_convert_owning_non_owning(struct bpf_verifier_env *env, u32 ref_obj_id) | |
9855 | { | |
9856 | struct bpf_func_state *state, *unused; | |
534e86bc KKD |
9857 | struct bpf_reg_state *reg; |
9858 | int i; | |
9859 | ||
6a3cd331 DM |
9860 | state = cur_func(env); |
9861 | ||
534e86bc | 9862 | if (!ref_obj_id) { |
6a3cd331 DM |
9863 | verbose(env, "verifier internal error: ref_obj_id is zero for " |
9864 | "owning -> non-owning conversion\n"); | |
534e86bc KKD |
9865 | return -EFAULT; |
9866 | } | |
6a3cd331 | 9867 | |
534e86bc | 9868 | for (i = 0; i < state->acquired_refs; i++) { |
6a3cd331 DM |
9869 | if (state->refs[i].id != ref_obj_id) |
9870 | continue; | |
9871 | ||
9872 | /* Clear ref_obj_id here so release_reference doesn't clobber | |
9873 | * the whole reg | |
9874 | */ | |
9875 | bpf_for_each_reg_in_vstate(env->cur_state, unused, reg, ({ | |
9876 | if (reg->ref_obj_id == ref_obj_id) { | |
9877 | reg->ref_obj_id = 0; | |
9878 | ref_set_non_owning(env, reg); | |
534e86bc | 9879 | } |
6a3cd331 DM |
9880 | })); |
9881 | return 0; | |
534e86bc | 9882 | } |
6a3cd331 | 9883 | |
534e86bc KKD |
9884 | verbose(env, "verifier internal error: ref state missing for ref_obj_id\n"); |
9885 | return -EFAULT; | |
9886 | } | |
9887 | ||
8cab76ec KKD |
9888 | /* Implementation details: |
9889 | * | |
9890 | * Each register points to some region of memory, which we define as an | |
9891 | * allocation. Each allocation may embed a bpf_spin_lock which protects any | |
9892 | * special BPF objects (bpf_list_head, bpf_rb_root, etc.) part of the same | |
9893 | * allocation. The lock and the data it protects are colocated in the same | |
9894 | * memory region. | |
9895 | * | |
9896 | * Hence, everytime a register holds a pointer value pointing to such | |
9897 | * allocation, the verifier preserves a unique reg->id for it. | |
9898 | * | |
9899 | * The verifier remembers the lock 'ptr' and the lock 'id' whenever | |
9900 | * bpf_spin_lock is called. | |
9901 | * | |
9902 | * To enable this, lock state in the verifier captures two values: | |
9903 | * active_lock.ptr = Register's type specific pointer | |
9904 | * active_lock.id = A unique ID for each register pointer value | |
9905 | * | |
9906 | * Currently, PTR_TO_MAP_VALUE and PTR_TO_BTF_ID | MEM_ALLOC are the two | |
9907 | * supported register types. | |
9908 | * | |
9909 | * The active_lock.ptr in case of map values is the reg->map_ptr, and in case of | |
9910 | * allocated objects is the reg->btf pointer. | |
9911 | * | |
9912 | * The active_lock.id is non-unique for maps supporting direct_value_addr, as we | |
9913 | * can establish the provenance of the map value statically for each distinct | |
9914 | * lookup into such maps. They always contain a single map value hence unique | |
9915 | * IDs for each pseudo load pessimizes the algorithm and rejects valid programs. | |
9916 | * | |
9917 | * So, in case of global variables, they use array maps with max_entries = 1, | |
9918 | * hence their active_lock.ptr becomes map_ptr and id = 0 (since they all point | |
9919 | * into the same map value as max_entries is 1, as described above). | |
9920 | * | |
9921 | * In case of inner map lookups, the inner map pointer has same map_ptr as the | |
9922 | * outer map pointer (in verifier context), but each lookup into an inner map | |
9923 | * assigns a fresh reg->id to the lookup, so while lookups into distinct inner | |
9924 | * maps from the same outer map share the same map_ptr as active_lock.ptr, they | |
9925 | * will get different reg->id assigned to each lookup, hence different | |
9926 | * active_lock.id. | |
9927 | * | |
9928 | * In case of allocated objects, active_lock.ptr is the reg->btf, and the | |
9929 | * reg->id is a unique ID preserved after the NULL pointer check on the pointer | |
9930 | * returned from bpf_obj_new. Each allocation receives a new reg->id. | |
9931 | */ | |
9932 | static int check_reg_allocation_locked(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
9933 | { | |
9934 | void *ptr; | |
9935 | u32 id; | |
9936 | ||
9937 | switch ((int)reg->type) { | |
9938 | case PTR_TO_MAP_VALUE: | |
9939 | ptr = reg->map_ptr; | |
9940 | break; | |
9941 | case PTR_TO_BTF_ID | MEM_ALLOC: | |
9942 | ptr = reg->btf; | |
9943 | break; | |
9944 | default: | |
9945 | verbose(env, "verifier internal error: unknown reg type for lock check\n"); | |
9946 | return -EFAULT; | |
9947 | } | |
9948 | id = reg->id; | |
9949 | ||
9950 | if (!env->cur_state->active_lock.ptr) | |
9951 | return -EINVAL; | |
9952 | if (env->cur_state->active_lock.ptr != ptr || | |
9953 | env->cur_state->active_lock.id != id) { | |
9954 | verbose(env, "held lock and object are not in the same allocation\n"); | |
9955 | return -EINVAL; | |
9956 | } | |
9957 | return 0; | |
9958 | } | |
9959 | ||
9960 | static bool is_bpf_list_api_kfunc(u32 btf_id) | |
9961 | { | |
d2dcc67d DM |
9962 | return btf_id == special_kfunc_list[KF_bpf_list_push_front_impl] || |
9963 | btf_id == special_kfunc_list[KF_bpf_list_push_back_impl] || | |
8cab76ec KKD |
9964 | btf_id == special_kfunc_list[KF_bpf_list_pop_front] || |
9965 | btf_id == special_kfunc_list[KF_bpf_list_pop_back]; | |
9966 | } | |
9967 | ||
cd6791b4 DM |
9968 | static bool is_bpf_rbtree_api_kfunc(u32 btf_id) |
9969 | { | |
d2dcc67d | 9970 | return btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl] || |
cd6791b4 DM |
9971 | btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || |
9972 | btf_id == special_kfunc_list[KF_bpf_rbtree_first]; | |
9973 | } | |
9974 | ||
9975 | static bool is_bpf_graph_api_kfunc(u32 btf_id) | |
9976 | { | |
7c50b1cb DM |
9977 | return is_bpf_list_api_kfunc(btf_id) || is_bpf_rbtree_api_kfunc(btf_id) || |
9978 | btf_id == special_kfunc_list[KF_bpf_refcount_acquire_impl]; | |
cd6791b4 DM |
9979 | } |
9980 | ||
5d92ddc3 DM |
9981 | static bool is_callback_calling_kfunc(u32 btf_id) |
9982 | { | |
d2dcc67d | 9983 | return btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl]; |
5d92ddc3 DM |
9984 | } |
9985 | ||
9986 | static bool is_rbtree_lock_required_kfunc(u32 btf_id) | |
9987 | { | |
9988 | return is_bpf_rbtree_api_kfunc(btf_id); | |
9989 | } | |
9990 | ||
cd6791b4 DM |
9991 | static bool check_kfunc_is_graph_root_api(struct bpf_verifier_env *env, |
9992 | enum btf_field_type head_field_type, | |
9993 | u32 kfunc_btf_id) | |
9994 | { | |
9995 | bool ret; | |
9996 | ||
9997 | switch (head_field_type) { | |
9998 | case BPF_LIST_HEAD: | |
9999 | ret = is_bpf_list_api_kfunc(kfunc_btf_id); | |
10000 | break; | |
10001 | case BPF_RB_ROOT: | |
10002 | ret = is_bpf_rbtree_api_kfunc(kfunc_btf_id); | |
10003 | break; | |
10004 | default: | |
10005 | verbose(env, "verifier internal error: unexpected graph root argument type %s\n", | |
10006 | btf_field_type_name(head_field_type)); | |
10007 | return false; | |
10008 | } | |
10009 | ||
10010 | if (!ret) | |
10011 | verbose(env, "verifier internal error: %s head arg for unknown kfunc\n", | |
10012 | btf_field_type_name(head_field_type)); | |
10013 | return ret; | |
10014 | } | |
10015 | ||
10016 | static bool check_kfunc_is_graph_node_api(struct bpf_verifier_env *env, | |
10017 | enum btf_field_type node_field_type, | |
10018 | u32 kfunc_btf_id) | |
8cab76ec | 10019 | { |
cd6791b4 DM |
10020 | bool ret; |
10021 | ||
10022 | switch (node_field_type) { | |
10023 | case BPF_LIST_NODE: | |
d2dcc67d DM |
10024 | ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_front_impl] || |
10025 | kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_back_impl]); | |
cd6791b4 DM |
10026 | break; |
10027 | case BPF_RB_NODE: | |
10028 | ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
d2dcc67d | 10029 | kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl]); |
cd6791b4 DM |
10030 | break; |
10031 | default: | |
10032 | verbose(env, "verifier internal error: unexpected graph node argument type %s\n", | |
10033 | btf_field_type_name(node_field_type)); | |
10034 | return false; | |
10035 | } | |
10036 | ||
10037 | if (!ret) | |
10038 | verbose(env, "verifier internal error: %s node arg for unknown kfunc\n", | |
10039 | btf_field_type_name(node_field_type)); | |
10040 | return ret; | |
10041 | } | |
10042 | ||
10043 | static int | |
10044 | __process_kf_arg_ptr_to_graph_root(struct bpf_verifier_env *env, | |
10045 | struct bpf_reg_state *reg, u32 regno, | |
10046 | struct bpf_kfunc_call_arg_meta *meta, | |
10047 | enum btf_field_type head_field_type, | |
10048 | struct btf_field **head_field) | |
10049 | { | |
10050 | const char *head_type_name; | |
8cab76ec KKD |
10051 | struct btf_field *field; |
10052 | struct btf_record *rec; | |
cd6791b4 | 10053 | u32 head_off; |
8cab76ec | 10054 | |
cd6791b4 DM |
10055 | if (meta->btf != btf_vmlinux) { |
10056 | verbose(env, "verifier internal error: unexpected btf mismatch in kfunc call\n"); | |
8cab76ec KKD |
10057 | return -EFAULT; |
10058 | } | |
10059 | ||
cd6791b4 DM |
10060 | if (!check_kfunc_is_graph_root_api(env, head_field_type, meta->func_id)) |
10061 | return -EFAULT; | |
10062 | ||
10063 | head_type_name = btf_field_type_name(head_field_type); | |
8cab76ec KKD |
10064 | if (!tnum_is_const(reg->var_off)) { |
10065 | verbose(env, | |
cd6791b4 DM |
10066 | "R%d doesn't have constant offset. %s has to be at the constant offset\n", |
10067 | regno, head_type_name); | |
8cab76ec KKD |
10068 | return -EINVAL; |
10069 | } | |
10070 | ||
10071 | rec = reg_btf_record(reg); | |
cd6791b4 DM |
10072 | head_off = reg->off + reg->var_off.value; |
10073 | field = btf_record_find(rec, head_off, head_field_type); | |
8cab76ec | 10074 | if (!field) { |
cd6791b4 | 10075 | verbose(env, "%s not found at offset=%u\n", head_type_name, head_off); |
8cab76ec KKD |
10076 | return -EINVAL; |
10077 | } | |
10078 | ||
10079 | /* All functions require bpf_list_head to be protected using a bpf_spin_lock */ | |
10080 | if (check_reg_allocation_locked(env, reg)) { | |
cd6791b4 DM |
10081 | verbose(env, "bpf_spin_lock at off=%d must be held for %s\n", |
10082 | rec->spin_lock_off, head_type_name); | |
8cab76ec KKD |
10083 | return -EINVAL; |
10084 | } | |
10085 | ||
cd6791b4 DM |
10086 | if (*head_field) { |
10087 | verbose(env, "verifier internal error: repeating %s arg\n", head_type_name); | |
8cab76ec KKD |
10088 | return -EFAULT; |
10089 | } | |
cd6791b4 | 10090 | *head_field = field; |
8cab76ec KKD |
10091 | return 0; |
10092 | } | |
10093 | ||
cd6791b4 | 10094 | static int process_kf_arg_ptr_to_list_head(struct bpf_verifier_env *env, |
8cab76ec KKD |
10095 | struct bpf_reg_state *reg, u32 regno, |
10096 | struct bpf_kfunc_call_arg_meta *meta) | |
10097 | { | |
cd6791b4 DM |
10098 | return __process_kf_arg_ptr_to_graph_root(env, reg, regno, meta, BPF_LIST_HEAD, |
10099 | &meta->arg_list_head.field); | |
10100 | } | |
10101 | ||
10102 | static int process_kf_arg_ptr_to_rbtree_root(struct bpf_verifier_env *env, | |
10103 | struct bpf_reg_state *reg, u32 regno, | |
10104 | struct bpf_kfunc_call_arg_meta *meta) | |
10105 | { | |
10106 | return __process_kf_arg_ptr_to_graph_root(env, reg, regno, meta, BPF_RB_ROOT, | |
10107 | &meta->arg_rbtree_root.field); | |
10108 | } | |
10109 | ||
10110 | static int | |
10111 | __process_kf_arg_ptr_to_graph_node(struct bpf_verifier_env *env, | |
10112 | struct bpf_reg_state *reg, u32 regno, | |
10113 | struct bpf_kfunc_call_arg_meta *meta, | |
10114 | enum btf_field_type head_field_type, | |
10115 | enum btf_field_type node_field_type, | |
10116 | struct btf_field **node_field) | |
10117 | { | |
10118 | const char *node_type_name; | |
8cab76ec KKD |
10119 | const struct btf_type *et, *t; |
10120 | struct btf_field *field; | |
cd6791b4 | 10121 | u32 node_off; |
8cab76ec | 10122 | |
cd6791b4 DM |
10123 | if (meta->btf != btf_vmlinux) { |
10124 | verbose(env, "verifier internal error: unexpected btf mismatch in kfunc call\n"); | |
8cab76ec KKD |
10125 | return -EFAULT; |
10126 | } | |
10127 | ||
cd6791b4 DM |
10128 | if (!check_kfunc_is_graph_node_api(env, node_field_type, meta->func_id)) |
10129 | return -EFAULT; | |
10130 | ||
10131 | node_type_name = btf_field_type_name(node_field_type); | |
8cab76ec KKD |
10132 | if (!tnum_is_const(reg->var_off)) { |
10133 | verbose(env, | |
cd6791b4 DM |
10134 | "R%d doesn't have constant offset. %s has to be at the constant offset\n", |
10135 | regno, node_type_name); | |
8cab76ec KKD |
10136 | return -EINVAL; |
10137 | } | |
10138 | ||
cd6791b4 DM |
10139 | node_off = reg->off + reg->var_off.value; |
10140 | field = reg_find_field_offset(reg, node_off, node_field_type); | |
10141 | if (!field || field->offset != node_off) { | |
10142 | verbose(env, "%s not found at offset=%u\n", node_type_name, node_off); | |
8cab76ec KKD |
10143 | return -EINVAL; |
10144 | } | |
10145 | ||
cd6791b4 | 10146 | field = *node_field; |
8cab76ec | 10147 | |
30465003 | 10148 | et = btf_type_by_id(field->graph_root.btf, field->graph_root.value_btf_id); |
8cab76ec | 10149 | t = btf_type_by_id(reg->btf, reg->btf_id); |
30465003 DM |
10150 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, 0, field->graph_root.btf, |
10151 | field->graph_root.value_btf_id, true)) { | |
cd6791b4 | 10152 | verbose(env, "operation on %s expects arg#1 %s at offset=%d " |
8cab76ec | 10153 | "in struct %s, but arg is at offset=%d in struct %s\n", |
cd6791b4 DM |
10154 | btf_field_type_name(head_field_type), |
10155 | btf_field_type_name(node_field_type), | |
30465003 DM |
10156 | field->graph_root.node_offset, |
10157 | btf_name_by_offset(field->graph_root.btf, et->name_off), | |
cd6791b4 | 10158 | node_off, btf_name_by_offset(reg->btf, t->name_off)); |
8cab76ec KKD |
10159 | return -EINVAL; |
10160 | } | |
10161 | ||
cd6791b4 DM |
10162 | if (node_off != field->graph_root.node_offset) { |
10163 | verbose(env, "arg#1 offset=%d, but expected %s at offset=%d in struct %s\n", | |
10164 | node_off, btf_field_type_name(node_field_type), | |
10165 | field->graph_root.node_offset, | |
30465003 | 10166 | btf_name_by_offset(field->graph_root.btf, et->name_off)); |
8cab76ec KKD |
10167 | return -EINVAL; |
10168 | } | |
6a3cd331 DM |
10169 | |
10170 | return 0; | |
8cab76ec KKD |
10171 | } |
10172 | ||
cd6791b4 DM |
10173 | static int process_kf_arg_ptr_to_list_node(struct bpf_verifier_env *env, |
10174 | struct bpf_reg_state *reg, u32 regno, | |
10175 | struct bpf_kfunc_call_arg_meta *meta) | |
10176 | { | |
10177 | return __process_kf_arg_ptr_to_graph_node(env, reg, regno, meta, | |
10178 | BPF_LIST_HEAD, BPF_LIST_NODE, | |
10179 | &meta->arg_list_head.field); | |
10180 | } | |
10181 | ||
10182 | static int process_kf_arg_ptr_to_rbtree_node(struct bpf_verifier_env *env, | |
10183 | struct bpf_reg_state *reg, u32 regno, | |
10184 | struct bpf_kfunc_call_arg_meta *meta) | |
10185 | { | |
10186 | return __process_kf_arg_ptr_to_graph_node(env, reg, regno, meta, | |
10187 | BPF_RB_ROOT, BPF_RB_NODE, | |
10188 | &meta->arg_rbtree_root.field); | |
10189 | } | |
10190 | ||
1d18feb2 JK |
10191 | static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta, |
10192 | int insn_idx) | |
00b85860 KKD |
10193 | { |
10194 | const char *func_name = meta->func_name, *ref_tname; | |
10195 | const struct btf *btf = meta->btf; | |
10196 | const struct btf_param *args; | |
7c50b1cb | 10197 | struct btf_record *rec; |
00b85860 KKD |
10198 | u32 i, nargs; |
10199 | int ret; | |
10200 | ||
10201 | args = (const struct btf_param *)(meta->func_proto + 1); | |
10202 | nargs = btf_type_vlen(meta->func_proto); | |
10203 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { | |
10204 | verbose(env, "Function %s has %d > %d args\n", func_name, nargs, | |
10205 | MAX_BPF_FUNC_REG_ARGS); | |
10206 | return -EINVAL; | |
10207 | } | |
10208 | ||
10209 | /* Check that BTF function arguments match actual types that the | |
10210 | * verifier sees. | |
10211 | */ | |
10212 | for (i = 0; i < nargs; i++) { | |
10213 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[i + 1]; | |
10214 | const struct btf_type *t, *ref_t, *resolve_ret; | |
10215 | enum bpf_arg_type arg_type = ARG_DONTCARE; | |
10216 | u32 regno = i + 1, ref_id, type_size; | |
10217 | bool is_ret_buf_sz = false; | |
10218 | int kf_arg_type; | |
10219 | ||
10220 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); | |
958cf2e2 KKD |
10221 | |
10222 | if (is_kfunc_arg_ignore(btf, &args[i])) | |
10223 | continue; | |
10224 | ||
00b85860 KKD |
10225 | if (btf_type_is_scalar(t)) { |
10226 | if (reg->type != SCALAR_VALUE) { | |
10227 | verbose(env, "R%d is not a scalar\n", regno); | |
10228 | return -EINVAL; | |
10229 | } | |
a50388db KKD |
10230 | |
10231 | if (is_kfunc_arg_constant(meta->btf, &args[i])) { | |
10232 | if (meta->arg_constant.found) { | |
10233 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
10234 | return -EFAULT; | |
10235 | } | |
10236 | if (!tnum_is_const(reg->var_off)) { | |
10237 | verbose(env, "R%d must be a known constant\n", regno); | |
10238 | return -EINVAL; | |
10239 | } | |
10240 | ret = mark_chain_precision(env, regno); | |
10241 | if (ret < 0) | |
10242 | return ret; | |
10243 | meta->arg_constant.found = true; | |
10244 | meta->arg_constant.value = reg->var_off.value; | |
10245 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) { | |
00b85860 KKD |
10246 | meta->r0_rdonly = true; |
10247 | is_ret_buf_sz = true; | |
10248 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) { | |
10249 | is_ret_buf_sz = true; | |
10250 | } | |
10251 | ||
10252 | if (is_ret_buf_sz) { | |
10253 | if (meta->r0_size) { | |
10254 | verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc"); | |
10255 | return -EINVAL; | |
10256 | } | |
10257 | ||
10258 | if (!tnum_is_const(reg->var_off)) { | |
10259 | verbose(env, "R%d is not a const\n", regno); | |
10260 | return -EINVAL; | |
10261 | } | |
10262 | ||
10263 | meta->r0_size = reg->var_off.value; | |
10264 | ret = mark_chain_precision(env, regno); | |
10265 | if (ret) | |
10266 | return ret; | |
10267 | } | |
10268 | continue; | |
10269 | } | |
10270 | ||
10271 | if (!btf_type_is_ptr(t)) { | |
10272 | verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t)); | |
10273 | return -EINVAL; | |
10274 | } | |
10275 | ||
20c09d92 | 10276 | if ((is_kfunc_trusted_args(meta) || is_kfunc_rcu(meta)) && |
caf713c3 DV |
10277 | (register_is_null(reg) || type_may_be_null(reg->type))) { |
10278 | verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i); | |
10279 | return -EACCES; | |
10280 | } | |
10281 | ||
00b85860 KKD |
10282 | if (reg->ref_obj_id) { |
10283 | if (is_kfunc_release(meta) && meta->ref_obj_id) { | |
10284 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
10285 | regno, reg->ref_obj_id, | |
10286 | meta->ref_obj_id); | |
10287 | return -EFAULT; | |
10288 | } | |
10289 | meta->ref_obj_id = reg->ref_obj_id; | |
10290 | if (is_kfunc_release(meta)) | |
10291 | meta->release_regno = regno; | |
10292 | } | |
10293 | ||
10294 | ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); | |
10295 | ref_tname = btf_name_by_offset(btf, ref_t->name_off); | |
10296 | ||
10297 | kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs); | |
10298 | if (kf_arg_type < 0) | |
10299 | return kf_arg_type; | |
10300 | ||
10301 | switch (kf_arg_type) { | |
ac9f0605 | 10302 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
00b85860 | 10303 | case KF_ARG_PTR_TO_BTF_ID: |
fca1aa75 | 10304 | if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta)) |
00b85860 | 10305 | break; |
3f00c523 DV |
10306 | |
10307 | if (!is_trusted_reg(reg)) { | |
fca1aa75 YS |
10308 | if (!is_kfunc_rcu(meta)) { |
10309 | verbose(env, "R%d must be referenced or trusted\n", regno); | |
10310 | return -EINVAL; | |
10311 | } | |
10312 | if (!is_rcu_reg(reg)) { | |
10313 | verbose(env, "R%d must be a rcu pointer\n", regno); | |
10314 | return -EINVAL; | |
10315 | } | |
00b85860 | 10316 | } |
fca1aa75 | 10317 | |
00b85860 KKD |
10318 | fallthrough; |
10319 | case KF_ARG_PTR_TO_CTX: | |
10320 | /* Trusted arguments have the same offset checks as release arguments */ | |
10321 | arg_type |= OBJ_RELEASE; | |
10322 | break; | |
00b85860 | 10323 | case KF_ARG_PTR_TO_DYNPTR: |
06accc87 | 10324 | case KF_ARG_PTR_TO_ITER: |
8cab76ec KKD |
10325 | case KF_ARG_PTR_TO_LIST_HEAD: |
10326 | case KF_ARG_PTR_TO_LIST_NODE: | |
cd6791b4 DM |
10327 | case KF_ARG_PTR_TO_RB_ROOT: |
10328 | case KF_ARG_PTR_TO_RB_NODE: | |
00b85860 KKD |
10329 | case KF_ARG_PTR_TO_MEM: |
10330 | case KF_ARG_PTR_TO_MEM_SIZE: | |
5d92ddc3 | 10331 | case KF_ARG_PTR_TO_CALLBACK: |
7c50b1cb | 10332 | case KF_ARG_PTR_TO_REFCOUNTED_KPTR: |
00b85860 KKD |
10333 | /* Trusted by default */ |
10334 | break; | |
10335 | default: | |
10336 | WARN_ON_ONCE(1); | |
10337 | return -EFAULT; | |
10338 | } | |
10339 | ||
10340 | if (is_kfunc_release(meta) && reg->ref_obj_id) | |
10341 | arg_type |= OBJ_RELEASE; | |
10342 | ret = check_func_arg_reg_off(env, reg, regno, arg_type); | |
10343 | if (ret < 0) | |
10344 | return ret; | |
10345 | ||
10346 | switch (kf_arg_type) { | |
10347 | case KF_ARG_PTR_TO_CTX: | |
10348 | if (reg->type != PTR_TO_CTX) { | |
10349 | verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t)); | |
10350 | return -EINVAL; | |
10351 | } | |
fd264ca0 YS |
10352 | |
10353 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { | |
10354 | ret = get_kern_ctx_btf_id(&env->log, resolve_prog_type(env->prog)); | |
10355 | if (ret < 0) | |
10356 | return -EINVAL; | |
10357 | meta->ret_btf_id = ret; | |
10358 | } | |
00b85860 | 10359 | break; |
ac9f0605 KKD |
10360 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
10361 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10362 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10363 | return -EINVAL; | |
10364 | } | |
10365 | if (!reg->ref_obj_id) { | |
10366 | verbose(env, "allocated object must be referenced\n"); | |
10367 | return -EINVAL; | |
10368 | } | |
10369 | if (meta->btf == btf_vmlinux && | |
10370 | meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { | |
10371 | meta->arg_obj_drop.btf = reg->btf; | |
10372 | meta->arg_obj_drop.btf_id = reg->btf_id; | |
10373 | } | |
10374 | break; | |
00b85860 | 10375 | case KF_ARG_PTR_TO_DYNPTR: |
d96d937d JK |
10376 | { |
10377 | enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR; | |
361f129f | 10378 | int clone_ref_obj_id = 0; |
d96d937d | 10379 | |
6b75bd3d | 10380 | if (reg->type != PTR_TO_STACK && |
27060531 | 10381 | reg->type != CONST_PTR_TO_DYNPTR) { |
6b75bd3d | 10382 | verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i); |
00b85860 KKD |
10383 | return -EINVAL; |
10384 | } | |
10385 | ||
d96d937d JK |
10386 | if (reg->type == CONST_PTR_TO_DYNPTR) |
10387 | dynptr_arg_type |= MEM_RDONLY; | |
10388 | ||
10389 | if (is_kfunc_arg_uninit(btf, &args[i])) | |
10390 | dynptr_arg_type |= MEM_UNINIT; | |
10391 | ||
361f129f | 10392 | if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { |
b5964b96 | 10393 | dynptr_arg_type |= DYNPTR_TYPE_SKB; |
361f129f | 10394 | } else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp]) { |
05421aec | 10395 | dynptr_arg_type |= DYNPTR_TYPE_XDP; |
361f129f JK |
10396 | } else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_clone] && |
10397 | (dynptr_arg_type & MEM_UNINIT)) { | |
10398 | enum bpf_dynptr_type parent_type = meta->initialized_dynptr.type; | |
10399 | ||
10400 | if (parent_type == BPF_DYNPTR_TYPE_INVALID) { | |
10401 | verbose(env, "verifier internal error: no dynptr type for parent of clone\n"); | |
10402 | return -EFAULT; | |
10403 | } | |
10404 | ||
10405 | dynptr_arg_type |= (unsigned int)get_dynptr_type_flag(parent_type); | |
10406 | clone_ref_obj_id = meta->initialized_dynptr.ref_obj_id; | |
10407 | if (dynptr_type_refcounted(parent_type) && !clone_ref_obj_id) { | |
10408 | verbose(env, "verifier internal error: missing ref obj id for parent of clone\n"); | |
10409 | return -EFAULT; | |
10410 | } | |
10411 | } | |
b5964b96 | 10412 | |
361f129f | 10413 | ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id); |
6b75bd3d KKD |
10414 | if (ret < 0) |
10415 | return ret; | |
66e3a13e JK |
10416 | |
10417 | if (!(dynptr_arg_type & MEM_UNINIT)) { | |
10418 | int id = dynptr_id(env, reg); | |
10419 | ||
10420 | if (id < 0) { | |
10421 | verbose(env, "verifier internal error: failed to obtain dynptr id\n"); | |
10422 | return id; | |
10423 | } | |
10424 | meta->initialized_dynptr.id = id; | |
10425 | meta->initialized_dynptr.type = dynptr_get_type(env, reg); | |
361f129f | 10426 | meta->initialized_dynptr.ref_obj_id = dynptr_ref_obj_id(env, reg); |
66e3a13e JK |
10427 | } |
10428 | ||
00b85860 | 10429 | break; |
d96d937d | 10430 | } |
06accc87 AN |
10431 | case KF_ARG_PTR_TO_ITER: |
10432 | ret = process_iter_arg(env, regno, insn_idx, meta); | |
10433 | if (ret < 0) | |
10434 | return ret; | |
10435 | break; | |
8cab76ec KKD |
10436 | case KF_ARG_PTR_TO_LIST_HEAD: |
10437 | if (reg->type != PTR_TO_MAP_VALUE && | |
10438 | reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10439 | verbose(env, "arg#%d expected pointer to map value or allocated object\n", i); | |
10440 | return -EINVAL; | |
10441 | } | |
10442 | if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) { | |
10443 | verbose(env, "allocated object must be referenced\n"); | |
10444 | return -EINVAL; | |
10445 | } | |
10446 | ret = process_kf_arg_ptr_to_list_head(env, reg, regno, meta); | |
10447 | if (ret < 0) | |
10448 | return ret; | |
10449 | break; | |
cd6791b4 DM |
10450 | case KF_ARG_PTR_TO_RB_ROOT: |
10451 | if (reg->type != PTR_TO_MAP_VALUE && | |
10452 | reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10453 | verbose(env, "arg#%d expected pointer to map value or allocated object\n", i); | |
10454 | return -EINVAL; | |
10455 | } | |
10456 | if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) { | |
10457 | verbose(env, "allocated object must be referenced\n"); | |
10458 | return -EINVAL; | |
10459 | } | |
10460 | ret = process_kf_arg_ptr_to_rbtree_root(env, reg, regno, meta); | |
10461 | if (ret < 0) | |
10462 | return ret; | |
10463 | break; | |
8cab76ec KKD |
10464 | case KF_ARG_PTR_TO_LIST_NODE: |
10465 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10466 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10467 | return -EINVAL; | |
10468 | } | |
10469 | if (!reg->ref_obj_id) { | |
10470 | verbose(env, "allocated object must be referenced\n"); | |
10471 | return -EINVAL; | |
10472 | } | |
10473 | ret = process_kf_arg_ptr_to_list_node(env, reg, regno, meta); | |
10474 | if (ret < 0) | |
10475 | return ret; | |
10476 | break; | |
cd6791b4 | 10477 | case KF_ARG_PTR_TO_RB_NODE: |
a40d3632 DM |
10478 | if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_remove]) { |
10479 | if (!type_is_non_owning_ref(reg->type) || reg->ref_obj_id) { | |
10480 | verbose(env, "rbtree_remove node input must be non-owning ref\n"); | |
10481 | return -EINVAL; | |
10482 | } | |
10483 | if (in_rbtree_lock_required_cb(env)) { | |
10484 | verbose(env, "rbtree_remove not allowed in rbtree cb\n"); | |
10485 | return -EINVAL; | |
10486 | } | |
10487 | } else { | |
10488 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
10489 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
10490 | return -EINVAL; | |
10491 | } | |
10492 | if (!reg->ref_obj_id) { | |
10493 | verbose(env, "allocated object must be referenced\n"); | |
10494 | return -EINVAL; | |
10495 | } | |
cd6791b4 | 10496 | } |
a40d3632 | 10497 | |
cd6791b4 DM |
10498 | ret = process_kf_arg_ptr_to_rbtree_node(env, reg, regno, meta); |
10499 | if (ret < 0) | |
10500 | return ret; | |
10501 | break; | |
00b85860 KKD |
10502 | case KF_ARG_PTR_TO_BTF_ID: |
10503 | /* Only base_type is checked, further checks are done here */ | |
3f00c523 | 10504 | if ((base_type(reg->type) != PTR_TO_BTF_ID || |
fca1aa75 | 10505 | (bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) && |
3f00c523 DV |
10506 | !reg2btf_ids[base_type(reg->type)]) { |
10507 | verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type)); | |
10508 | verbose(env, "expected %s or socket\n", | |
10509 | reg_type_str(env, base_type(reg->type) | | |
10510 | (type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS))); | |
00b85860 KKD |
10511 | return -EINVAL; |
10512 | } | |
10513 | ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i); | |
10514 | if (ret < 0) | |
10515 | return ret; | |
10516 | break; | |
10517 | case KF_ARG_PTR_TO_MEM: | |
10518 | resolve_ret = btf_resolve_size(btf, ref_t, &type_size); | |
10519 | if (IS_ERR(resolve_ret)) { | |
10520 | verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n", | |
10521 | i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret)); | |
10522 | return -EINVAL; | |
10523 | } | |
10524 | ret = check_mem_reg(env, reg, regno, type_size); | |
10525 | if (ret < 0) | |
10526 | return ret; | |
10527 | break; | |
10528 | case KF_ARG_PTR_TO_MEM_SIZE: | |
66e3a13e JK |
10529 | { |
10530 | struct bpf_reg_state *size_reg = ®s[regno + 1]; | |
10531 | const struct btf_param *size_arg = &args[i + 1]; | |
10532 | ||
10533 | ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1); | |
00b85860 KKD |
10534 | if (ret < 0) { |
10535 | verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1); | |
10536 | return ret; | |
10537 | } | |
66e3a13e JK |
10538 | |
10539 | if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) { | |
10540 | if (meta->arg_constant.found) { | |
10541 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
10542 | return -EFAULT; | |
10543 | } | |
10544 | if (!tnum_is_const(size_reg->var_off)) { | |
10545 | verbose(env, "R%d must be a known constant\n", regno + 1); | |
10546 | return -EINVAL; | |
10547 | } | |
10548 | meta->arg_constant.found = true; | |
10549 | meta->arg_constant.value = size_reg->var_off.value; | |
10550 | } | |
10551 | ||
10552 | /* Skip next '__sz' or '__szk' argument */ | |
00b85860 KKD |
10553 | i++; |
10554 | break; | |
66e3a13e | 10555 | } |
5d92ddc3 DM |
10556 | case KF_ARG_PTR_TO_CALLBACK: |
10557 | meta->subprogno = reg->subprogno; | |
10558 | break; | |
7c50b1cb DM |
10559 | case KF_ARG_PTR_TO_REFCOUNTED_KPTR: |
10560 | if (!type_is_ptr_alloc_obj(reg->type) && !type_is_non_owning_ref(reg->type)) { | |
10561 | verbose(env, "arg#%d is neither owning or non-owning ref\n", i); | |
10562 | return -EINVAL; | |
10563 | } | |
10564 | ||
10565 | rec = reg_btf_record(reg); | |
10566 | if (!rec) { | |
10567 | verbose(env, "verifier internal error: Couldn't find btf_record\n"); | |
10568 | return -EFAULT; | |
10569 | } | |
10570 | ||
10571 | if (rec->refcount_off < 0) { | |
10572 | verbose(env, "arg#%d doesn't point to a type with bpf_refcount field\n", i); | |
10573 | return -EINVAL; | |
10574 | } | |
7deca5ea DM |
10575 | if (rec->refcount_off >= 0) { |
10576 | verbose(env, "bpf_refcount_acquire calls are disabled for now\n"); | |
10577 | return -EINVAL; | |
10578 | } | |
7c50b1cb DM |
10579 | meta->arg_refcount_acquire.btf = reg->btf; |
10580 | meta->arg_refcount_acquire.btf_id = reg->btf_id; | |
10581 | break; | |
00b85860 KKD |
10582 | } |
10583 | } | |
10584 | ||
10585 | if (is_kfunc_release(meta) && !meta->release_regno) { | |
10586 | verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n", | |
10587 | func_name); | |
10588 | return -EINVAL; | |
10589 | } | |
10590 | ||
10591 | return 0; | |
10592 | } | |
10593 | ||
07236eab AN |
10594 | static int fetch_kfunc_meta(struct bpf_verifier_env *env, |
10595 | struct bpf_insn *insn, | |
10596 | struct bpf_kfunc_call_arg_meta *meta, | |
10597 | const char **kfunc_name) | |
e6ac2450 | 10598 | { |
07236eab AN |
10599 | const struct btf_type *func, *func_proto; |
10600 | u32 func_id, *kfunc_flags; | |
10601 | const char *func_name; | |
2357672c | 10602 | struct btf *desc_btf; |
e6ac2450 | 10603 | |
07236eab AN |
10604 | if (kfunc_name) |
10605 | *kfunc_name = NULL; | |
10606 | ||
a5d82727 | 10607 | if (!insn->imm) |
07236eab | 10608 | return -EINVAL; |
a5d82727 | 10609 | |
43bf0878 | 10610 | desc_btf = find_kfunc_desc_btf(env, insn->off); |
2357672c KKD |
10611 | if (IS_ERR(desc_btf)) |
10612 | return PTR_ERR(desc_btf); | |
10613 | ||
e6ac2450 | 10614 | func_id = insn->imm; |
2357672c KKD |
10615 | func = btf_type_by_id(desc_btf, func_id); |
10616 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
07236eab AN |
10617 | if (kfunc_name) |
10618 | *kfunc_name = func_name; | |
2357672c | 10619 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 | 10620 | |
a4703e31 KKD |
10621 | kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id); |
10622 | if (!kfunc_flags) { | |
e6ac2450 MKL |
10623 | return -EACCES; |
10624 | } | |
00b85860 | 10625 | |
07236eab AN |
10626 | memset(meta, 0, sizeof(*meta)); |
10627 | meta->btf = desc_btf; | |
10628 | meta->func_id = func_id; | |
10629 | meta->kfunc_flags = *kfunc_flags; | |
10630 | meta->func_proto = func_proto; | |
10631 | meta->func_name = func_name; | |
10632 | ||
10633 | return 0; | |
10634 | } | |
10635 | ||
10636 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
10637 | int *insn_idx_p) | |
10638 | { | |
10639 | const struct btf_type *t, *ptr_type; | |
10640 | u32 i, nargs, ptr_type_id, release_ref_obj_id; | |
10641 | struct bpf_reg_state *regs = cur_regs(env); | |
10642 | const char *func_name, *ptr_type_name; | |
10643 | bool sleepable, rcu_lock, rcu_unlock; | |
10644 | struct bpf_kfunc_call_arg_meta meta; | |
10645 | struct bpf_insn_aux_data *insn_aux; | |
10646 | int err, insn_idx = *insn_idx_p; | |
10647 | const struct btf_param *args; | |
10648 | const struct btf_type *ret_t; | |
10649 | struct btf *desc_btf; | |
10650 | ||
10651 | /* skip for now, but return error when we find this in fixup_kfunc_call */ | |
10652 | if (!insn->imm) | |
10653 | return 0; | |
10654 | ||
10655 | err = fetch_kfunc_meta(env, insn, &meta, &func_name); | |
10656 | if (err == -EACCES && func_name) | |
10657 | verbose(env, "calling kernel function %s is not allowed\n", func_name); | |
10658 | if (err) | |
10659 | return err; | |
10660 | desc_btf = meta.btf; | |
10661 | insn_aux = &env->insn_aux_data[insn_idx]; | |
00b85860 | 10662 | |
06accc87 AN |
10663 | insn_aux->is_iter_next = is_iter_next_kfunc(&meta); |
10664 | ||
00b85860 KKD |
10665 | if (is_kfunc_destructive(&meta) && !capable(CAP_SYS_BOOT)) { |
10666 | verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capability\n"); | |
4dd48c6f AS |
10667 | return -EACCES; |
10668 | } | |
10669 | ||
9bb00b28 YS |
10670 | sleepable = is_kfunc_sleepable(&meta); |
10671 | if (sleepable && !env->prog->aux->sleepable) { | |
00b85860 KKD |
10672 | verbose(env, "program must be sleepable to call sleepable kfunc %s\n", func_name); |
10673 | return -EACCES; | |
10674 | } | |
eb1f7f71 | 10675 | |
9bb00b28 YS |
10676 | rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta); |
10677 | rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta); | |
9bb00b28 YS |
10678 | |
10679 | if (env->cur_state->active_rcu_lock) { | |
10680 | struct bpf_func_state *state; | |
10681 | struct bpf_reg_state *reg; | |
10682 | ||
10683 | if (rcu_lock) { | |
10684 | verbose(env, "nested rcu read lock (kernel function %s)\n", func_name); | |
10685 | return -EINVAL; | |
10686 | } else if (rcu_unlock) { | |
10687 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ | |
10688 | if (reg->type & MEM_RCU) { | |
fca1aa75 | 10689 | reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL); |
9bb00b28 YS |
10690 | reg->type |= PTR_UNTRUSTED; |
10691 | } | |
10692 | })); | |
10693 | env->cur_state->active_rcu_lock = false; | |
10694 | } else if (sleepable) { | |
10695 | verbose(env, "kernel func %s is sleepable within rcu_read_lock region\n", func_name); | |
10696 | return -EACCES; | |
10697 | } | |
10698 | } else if (rcu_lock) { | |
10699 | env->cur_state->active_rcu_lock = true; | |
10700 | } else if (rcu_unlock) { | |
10701 | verbose(env, "unmatched rcu read unlock (kernel function %s)\n", func_name); | |
10702 | return -EINVAL; | |
10703 | } | |
10704 | ||
e6ac2450 | 10705 | /* Check the arguments */ |
1d18feb2 | 10706 | err = check_kfunc_args(env, &meta, insn_idx); |
5c073f26 | 10707 | if (err < 0) |
e6ac2450 | 10708 | return err; |
5c073f26 | 10709 | /* In case of release function, we get register number of refcounted |
00b85860 | 10710 | * PTR_TO_BTF_ID in bpf_kfunc_arg_meta, do the release now. |
5c073f26 | 10711 | */ |
00b85860 KKD |
10712 | if (meta.release_regno) { |
10713 | err = release_reference(env, regs[meta.release_regno].ref_obj_id); | |
5c073f26 KKD |
10714 | if (err) { |
10715 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
07236eab | 10716 | func_name, meta.func_id); |
5c073f26 KKD |
10717 | return err; |
10718 | } | |
10719 | } | |
e6ac2450 | 10720 | |
d2dcc67d DM |
10721 | if (meta.func_id == special_kfunc_list[KF_bpf_list_push_front_impl] || |
10722 | meta.func_id == special_kfunc_list[KF_bpf_list_push_back_impl] || | |
10723 | meta.func_id == special_kfunc_list[KF_bpf_rbtree_add_impl]) { | |
6a3cd331 | 10724 | release_ref_obj_id = regs[BPF_REG_2].ref_obj_id; |
d2dcc67d | 10725 | insn_aux->insert_off = regs[BPF_REG_2].off; |
6a3cd331 DM |
10726 | err = ref_convert_owning_non_owning(env, release_ref_obj_id); |
10727 | if (err) { | |
10728 | verbose(env, "kfunc %s#%d conversion of owning ref to non-owning failed\n", | |
07236eab | 10729 | func_name, meta.func_id); |
6a3cd331 DM |
10730 | return err; |
10731 | } | |
10732 | ||
10733 | err = release_reference(env, release_ref_obj_id); | |
10734 | if (err) { | |
10735 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
07236eab | 10736 | func_name, meta.func_id); |
6a3cd331 DM |
10737 | return err; |
10738 | } | |
10739 | } | |
10740 | ||
d2dcc67d | 10741 | if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_add_impl]) { |
5d92ddc3 DM |
10742 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
10743 | set_rbtree_add_callback_state); | |
10744 | if (err) { | |
10745 | verbose(env, "kfunc %s#%d failed callback verification\n", | |
07236eab | 10746 | func_name, meta.func_id); |
5d92ddc3 DM |
10747 | return err; |
10748 | } | |
10749 | } | |
10750 | ||
e6ac2450 MKL |
10751 | for (i = 0; i < CALLER_SAVED_REGS; i++) |
10752 | mark_reg_not_init(env, regs, caller_saved[i]); | |
10753 | ||
10754 | /* Check return type */ | |
07236eab | 10755 | t = btf_type_skip_modifiers(desc_btf, meta.func_proto->type, NULL); |
5c073f26 | 10756 | |
00b85860 | 10757 | if (is_kfunc_acquire(&meta) && !btf_type_is_struct_ptr(meta.btf, t)) { |
958cf2e2 | 10758 | /* Only exception is bpf_obj_new_impl */ |
7c50b1cb DM |
10759 | if (meta.btf != btf_vmlinux || |
10760 | (meta.func_id != special_kfunc_list[KF_bpf_obj_new_impl] && | |
10761 | meta.func_id != special_kfunc_list[KF_bpf_refcount_acquire_impl])) { | |
958cf2e2 KKD |
10762 | verbose(env, "acquire kernel function does not return PTR_TO_BTF_ID\n"); |
10763 | return -EINVAL; | |
10764 | } | |
5c073f26 KKD |
10765 | } |
10766 | ||
e6ac2450 MKL |
10767 | if (btf_type_is_scalar(t)) { |
10768 | mark_reg_unknown(env, regs, BPF_REG_0); | |
10769 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
10770 | } else if (btf_type_is_ptr(t)) { | |
958cf2e2 KKD |
10771 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, &ptr_type_id); |
10772 | ||
10773 | if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) { | |
10774 | if (meta.func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
958cf2e2 KKD |
10775 | struct btf *ret_btf; |
10776 | u32 ret_btf_id; | |
10777 | ||
e181d3f1 KKD |
10778 | if (unlikely(!bpf_global_ma_set)) |
10779 | return -ENOMEM; | |
10780 | ||
958cf2e2 KKD |
10781 | if (((u64)(u32)meta.arg_constant.value) != meta.arg_constant.value) { |
10782 | verbose(env, "local type ID argument must be in range [0, U32_MAX]\n"); | |
10783 | return -EINVAL; | |
10784 | } | |
10785 | ||
10786 | ret_btf = env->prog->aux->btf; | |
10787 | ret_btf_id = meta.arg_constant.value; | |
10788 | ||
10789 | /* This may be NULL due to user not supplying a BTF */ | |
10790 | if (!ret_btf) { | |
10791 | verbose(env, "bpf_obj_new requires prog BTF\n"); | |
10792 | return -EINVAL; | |
10793 | } | |
10794 | ||
10795 | ret_t = btf_type_by_id(ret_btf, ret_btf_id); | |
10796 | if (!ret_t || !__btf_type_is_struct(ret_t)) { | |
10797 | verbose(env, "bpf_obj_new type ID argument must be of a struct\n"); | |
10798 | return -EINVAL; | |
10799 | } | |
10800 | ||
10801 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10802 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
10803 | regs[BPF_REG_0].btf = ret_btf; | |
10804 | regs[BPF_REG_0].btf_id = ret_btf_id; | |
10805 | ||
07236eab AN |
10806 | insn_aux->obj_new_size = ret_t->size; |
10807 | insn_aux->kptr_struct_meta = | |
958cf2e2 | 10808 | btf_find_struct_meta(ret_btf, ret_btf_id); |
7c50b1cb DM |
10809 | } else if (meta.func_id == special_kfunc_list[KF_bpf_refcount_acquire_impl]) { |
10810 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10811 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
10812 | regs[BPF_REG_0].btf = meta.arg_refcount_acquire.btf; | |
10813 | regs[BPF_REG_0].btf_id = meta.arg_refcount_acquire.btf_id; | |
10814 | ||
10815 | insn_aux->kptr_struct_meta = | |
10816 | btf_find_struct_meta(meta.arg_refcount_acquire.btf, | |
10817 | meta.arg_refcount_acquire.btf_id); | |
8cab76ec KKD |
10818 | } else if (meta.func_id == special_kfunc_list[KF_bpf_list_pop_front] || |
10819 | meta.func_id == special_kfunc_list[KF_bpf_list_pop_back]) { | |
10820 | struct btf_field *field = meta.arg_list_head.field; | |
10821 | ||
a40d3632 DM |
10822 | mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root); |
10823 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_remove] || | |
10824 | meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { | |
10825 | struct btf_field *field = meta.arg_rbtree_root.field; | |
10826 | ||
10827 | mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root); | |
fd264ca0 YS |
10828 | } else if (meta.func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { |
10829 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10830 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED; | |
10831 | regs[BPF_REG_0].btf = desc_btf; | |
10832 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; | |
a35b9af4 YS |
10833 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { |
10834 | ret_t = btf_type_by_id(desc_btf, meta.arg_constant.value); | |
10835 | if (!ret_t || !btf_type_is_struct(ret_t)) { | |
10836 | verbose(env, | |
10837 | "kfunc bpf_rdonly_cast type ID argument must be of a struct\n"); | |
10838 | return -EINVAL; | |
10839 | } | |
10840 | ||
10841 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10842 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_UNTRUSTED; | |
10843 | regs[BPF_REG_0].btf = desc_btf; | |
10844 | regs[BPF_REG_0].btf_id = meta.arg_constant.value; | |
66e3a13e JK |
10845 | } else if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice] || |
10846 | meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) { | |
10847 | enum bpf_type_flag type_flag = get_dynptr_type_flag(meta.initialized_dynptr.type); | |
10848 | ||
10849 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10850 | ||
10851 | if (!meta.arg_constant.found) { | |
10852 | verbose(env, "verifier internal error: bpf_dynptr_slice(_rdwr) no constant size\n"); | |
10853 | return -EFAULT; | |
10854 | } | |
10855 | ||
10856 | regs[BPF_REG_0].mem_size = meta.arg_constant.value; | |
10857 | ||
10858 | /* PTR_MAYBE_NULL will be added when is_kfunc_ret_null is checked */ | |
10859 | regs[BPF_REG_0].type = PTR_TO_MEM | type_flag; | |
10860 | ||
10861 | if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice]) { | |
10862 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
10863 | } else { | |
10864 | /* this will set env->seen_direct_write to true */ | |
10865 | if (!may_access_direct_pkt_data(env, NULL, BPF_WRITE)) { | |
10866 | verbose(env, "the prog does not allow writes to packet data\n"); | |
10867 | return -EINVAL; | |
10868 | } | |
10869 | } | |
10870 | ||
10871 | if (!meta.initialized_dynptr.id) { | |
10872 | verbose(env, "verifier internal error: no dynptr id\n"); | |
10873 | return -EFAULT; | |
10874 | } | |
10875 | regs[BPF_REG_0].dynptr_id = meta.initialized_dynptr.id; | |
10876 | ||
10877 | /* we don't need to set BPF_REG_0's ref obj id | |
10878 | * because packet slices are not refcounted (see | |
10879 | * dynptr_type_refcounted) | |
10880 | */ | |
958cf2e2 KKD |
10881 | } else { |
10882 | verbose(env, "kernel function %s unhandled dynamic return type\n", | |
10883 | meta.func_name); | |
10884 | return -EFAULT; | |
10885 | } | |
10886 | } else if (!__btf_type_is_struct(ptr_type)) { | |
f4b4eee6 AN |
10887 | if (!meta.r0_size) { |
10888 | __u32 sz; | |
10889 | ||
10890 | if (!IS_ERR(btf_resolve_size(desc_btf, ptr_type, &sz))) { | |
10891 | meta.r0_size = sz; | |
10892 | meta.r0_rdonly = true; | |
10893 | } | |
10894 | } | |
eb1f7f71 BT |
10895 | if (!meta.r0_size) { |
10896 | ptr_type_name = btf_name_by_offset(desc_btf, | |
10897 | ptr_type->name_off); | |
10898 | verbose(env, | |
10899 | "kernel function %s returns pointer type %s %s is not supported\n", | |
10900 | func_name, | |
10901 | btf_type_str(ptr_type), | |
10902 | ptr_type_name); | |
10903 | return -EINVAL; | |
10904 | } | |
10905 | ||
10906 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10907 | regs[BPF_REG_0].type = PTR_TO_MEM; | |
10908 | regs[BPF_REG_0].mem_size = meta.r0_size; | |
10909 | ||
10910 | if (meta.r0_rdonly) | |
10911 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
10912 | ||
10913 | /* Ensures we don't access the memory after a release_reference() */ | |
10914 | if (meta.ref_obj_id) | |
10915 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
10916 | } else { | |
10917 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
10918 | regs[BPF_REG_0].btf = desc_btf; | |
10919 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; | |
10920 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
e6ac2450 | 10921 | } |
958cf2e2 | 10922 | |
00b85860 | 10923 | if (is_kfunc_ret_null(&meta)) { |
5c073f26 KKD |
10924 | regs[BPF_REG_0].type |= PTR_MAYBE_NULL; |
10925 | /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ | |
10926 | regs[BPF_REG_0].id = ++env->id_gen; | |
10927 | } | |
e6ac2450 | 10928 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
00b85860 | 10929 | if (is_kfunc_acquire(&meta)) { |
5c073f26 KKD |
10930 | int id = acquire_reference_state(env, insn_idx); |
10931 | ||
10932 | if (id < 0) | |
10933 | return id; | |
00b85860 KKD |
10934 | if (is_kfunc_ret_null(&meta)) |
10935 | regs[BPF_REG_0].id = id; | |
5c073f26 | 10936 | regs[BPF_REG_0].ref_obj_id = id; |
a40d3632 DM |
10937 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { |
10938 | ref_set_non_owning(env, ®s[BPF_REG_0]); | |
5c073f26 | 10939 | } |
a40d3632 | 10940 | |
00b85860 KKD |
10941 | if (reg_may_point_to_spin_lock(®s[BPF_REG_0]) && !regs[BPF_REG_0].id) |
10942 | regs[BPF_REG_0].id = ++env->id_gen; | |
f6a6a5a9 DM |
10943 | } else if (btf_type_is_void(t)) { |
10944 | if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) { | |
10945 | if (meta.func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { | |
10946 | insn_aux->kptr_struct_meta = | |
10947 | btf_find_struct_meta(meta.arg_obj_drop.btf, | |
10948 | meta.arg_obj_drop.btf_id); | |
10949 | } | |
10950 | } | |
10951 | } | |
e6ac2450 | 10952 | |
07236eab AN |
10953 | nargs = btf_type_vlen(meta.func_proto); |
10954 | args = (const struct btf_param *)(meta.func_proto + 1); | |
e6ac2450 MKL |
10955 | for (i = 0; i < nargs; i++) { |
10956 | u32 regno = i + 1; | |
10957 | ||
2357672c | 10958 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
10959 | if (btf_type_is_ptr(t)) |
10960 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
10961 | else | |
10962 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
10963 | mark_btf_func_reg_size(env, regno, t->size); | |
10964 | } | |
10965 | ||
06accc87 AN |
10966 | if (is_iter_next_kfunc(&meta)) { |
10967 | err = process_iter_next_call(env, insn_idx, &meta); | |
10968 | if (err) | |
10969 | return err; | |
10970 | } | |
10971 | ||
e6ac2450 MKL |
10972 | return 0; |
10973 | } | |
10974 | ||
b03c9f9f EC |
10975 | static bool signed_add_overflows(s64 a, s64 b) |
10976 | { | |
10977 | /* Do the add in u64, where overflow is well-defined */ | |
10978 | s64 res = (s64)((u64)a + (u64)b); | |
10979 | ||
10980 | if (b < 0) | |
10981 | return res > a; | |
10982 | return res < a; | |
10983 | } | |
10984 | ||
bc895e8b | 10985 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
10986 | { |
10987 | /* Do the add in u32, where overflow is well-defined */ | |
10988 | s32 res = (s32)((u32)a + (u32)b); | |
10989 | ||
10990 | if (b < 0) | |
10991 | return res > a; | |
10992 | return res < a; | |
10993 | } | |
10994 | ||
bc895e8b | 10995 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
10996 | { |
10997 | /* Do the sub in u64, where overflow is well-defined */ | |
10998 | s64 res = (s64)((u64)a - (u64)b); | |
10999 | ||
11000 | if (b < 0) | |
11001 | return res < a; | |
11002 | return res > a; | |
969bf05e AS |
11003 | } |
11004 | ||
3f50f132 JF |
11005 | static bool signed_sub32_overflows(s32 a, s32 b) |
11006 | { | |
bc895e8b | 11007 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
11008 | s32 res = (s32)((u32)a - (u32)b); |
11009 | ||
11010 | if (b < 0) | |
11011 | return res < a; | |
11012 | return res > a; | |
11013 | } | |
11014 | ||
bb7f0f98 AS |
11015 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
11016 | const struct bpf_reg_state *reg, | |
11017 | enum bpf_reg_type type) | |
11018 | { | |
11019 | bool known = tnum_is_const(reg->var_off); | |
11020 | s64 val = reg->var_off.value; | |
11021 | s64 smin = reg->smin_value; | |
11022 | ||
11023 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
11024 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
c25b2ae1 | 11025 | reg_type_str(env, type), val); |
bb7f0f98 AS |
11026 | return false; |
11027 | } | |
11028 | ||
11029 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
11030 | verbose(env, "%s pointer offset %d is not allowed\n", | |
c25b2ae1 | 11031 | reg_type_str(env, type), reg->off); |
bb7f0f98 AS |
11032 | return false; |
11033 | } | |
11034 | ||
11035 | if (smin == S64_MIN) { | |
11036 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
c25b2ae1 | 11037 | reg_type_str(env, type)); |
bb7f0f98 AS |
11038 | return false; |
11039 | } | |
11040 | ||
11041 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
11042 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
c25b2ae1 | 11043 | smin, reg_type_str(env, type)); |
bb7f0f98 AS |
11044 | return false; |
11045 | } | |
11046 | ||
11047 | return true; | |
11048 | } | |
11049 | ||
a6aaece0 DB |
11050 | enum { |
11051 | REASON_BOUNDS = -1, | |
11052 | REASON_TYPE = -2, | |
11053 | REASON_PATHS = -3, | |
11054 | REASON_LIMIT = -4, | |
11055 | REASON_STACK = -5, | |
11056 | }; | |
11057 | ||
979d63d5 | 11058 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 11059 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 11060 | { |
7fedb63a | 11061 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
11062 | |
11063 | switch (ptr_reg->type) { | |
11064 | case PTR_TO_STACK: | |
1b1597e6 | 11065 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
11066 | * left direction, see BPF_REG_FP. Also, unknown scalar |
11067 | * offset where we would need to deal with min/max bounds is | |
11068 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
11069 | */ |
11070 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 11071 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 11072 | break; |
979d63d5 | 11073 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 11074 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
11075 | ptr_limit = (mask_to_left ? |
11076 | ptr_reg->smin_value : | |
11077 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 11078 | break; |
979d63d5 | 11079 | default: |
a6aaece0 | 11080 | return REASON_TYPE; |
979d63d5 | 11081 | } |
b658bbb8 DB |
11082 | |
11083 | if (ptr_limit >= max) | |
a6aaece0 | 11084 | return REASON_LIMIT; |
b658bbb8 DB |
11085 | *alu_limit = ptr_limit; |
11086 | return 0; | |
979d63d5 DB |
11087 | } |
11088 | ||
d3bd7413 DB |
11089 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
11090 | const struct bpf_insn *insn) | |
11091 | { | |
2c78ee89 | 11092 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
11093 | } |
11094 | ||
11095 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
11096 | u32 alu_state, u32 alu_limit) | |
11097 | { | |
11098 | /* If we arrived here from different branches with different | |
11099 | * state or limits to sanitize, then this won't work. | |
11100 | */ | |
11101 | if (aux->alu_state && | |
11102 | (aux->alu_state != alu_state || | |
11103 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 11104 | return REASON_PATHS; |
d3bd7413 | 11105 | |
e6ac5933 | 11106 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
11107 | aux->alu_state = alu_state; |
11108 | aux->alu_limit = alu_limit; | |
11109 | return 0; | |
11110 | } | |
11111 | ||
11112 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
11113 | struct bpf_insn *insn) | |
11114 | { | |
11115 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
11116 | ||
11117 | if (can_skip_alu_sanitation(env, insn)) | |
11118 | return 0; | |
11119 | ||
11120 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
11121 | } | |
11122 | ||
f5288193 DB |
11123 | static bool sanitize_needed(u8 opcode) |
11124 | { | |
11125 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
11126 | } | |
11127 | ||
3d0220f6 DB |
11128 | struct bpf_sanitize_info { |
11129 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 11130 | bool mask_to_left; |
3d0220f6 DB |
11131 | }; |
11132 | ||
9183671a DB |
11133 | static struct bpf_verifier_state * |
11134 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
11135 | const struct bpf_insn *insn, | |
11136 | u32 next_idx, u32 curr_idx) | |
11137 | { | |
11138 | struct bpf_verifier_state *branch; | |
11139 | struct bpf_reg_state *regs; | |
11140 | ||
11141 | branch = push_stack(env, next_idx, curr_idx, true); | |
11142 | if (branch && insn) { | |
11143 | regs = branch->frame[branch->curframe]->regs; | |
11144 | if (BPF_SRC(insn->code) == BPF_K) { | |
11145 | mark_reg_unknown(env, regs, insn->dst_reg); | |
11146 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
11147 | mark_reg_unknown(env, regs, insn->dst_reg); | |
11148 | mark_reg_unknown(env, regs, insn->src_reg); | |
11149 | } | |
11150 | } | |
11151 | return branch; | |
11152 | } | |
11153 | ||
979d63d5 DB |
11154 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
11155 | struct bpf_insn *insn, | |
11156 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 11157 | const struct bpf_reg_state *off_reg, |
979d63d5 | 11158 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 11159 | struct bpf_sanitize_info *info, |
7fedb63a | 11160 | const bool commit_window) |
979d63d5 | 11161 | { |
3d0220f6 | 11162 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 11163 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 11164 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 11165 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
11166 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
11167 | u8 opcode = BPF_OP(insn->code); | |
11168 | u32 alu_state, alu_limit; | |
11169 | struct bpf_reg_state tmp; | |
11170 | bool ret; | |
f232326f | 11171 | int err; |
979d63d5 | 11172 | |
d3bd7413 | 11173 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
11174 | return 0; |
11175 | ||
11176 | /* We already marked aux for masking from non-speculative | |
11177 | * paths, thus we got here in the first place. We only care | |
11178 | * to explore bad access from here. | |
11179 | */ | |
11180 | if (vstate->speculative) | |
11181 | goto do_sim; | |
11182 | ||
bb01a1bb DB |
11183 | if (!commit_window) { |
11184 | if (!tnum_is_const(off_reg->var_off) && | |
11185 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
11186 | return REASON_BOUNDS; | |
11187 | ||
11188 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
11189 | (opcode == BPF_SUB && !off_is_neg); | |
11190 | } | |
11191 | ||
11192 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
11193 | if (err < 0) |
11194 | return err; | |
11195 | ||
7fedb63a DB |
11196 | if (commit_window) { |
11197 | /* In commit phase we narrow the masking window based on | |
11198 | * the observed pointer move after the simulated operation. | |
11199 | */ | |
3d0220f6 DB |
11200 | alu_state = info->aux.alu_state; |
11201 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
11202 | } else { |
11203 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 11204 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
11205 | alu_state |= ptr_is_dst_reg ? |
11206 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
11207 | |
11208 | /* Limit pruning on unknown scalars to enable deep search for | |
11209 | * potential masking differences from other program paths. | |
11210 | */ | |
11211 | if (!off_is_imm) | |
11212 | env->explore_alu_limits = true; | |
7fedb63a DB |
11213 | } |
11214 | ||
f232326f PK |
11215 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
11216 | if (err < 0) | |
11217 | return err; | |
979d63d5 | 11218 | do_sim: |
7fedb63a DB |
11219 | /* If we're in commit phase, we're done here given we already |
11220 | * pushed the truncated dst_reg into the speculative verification | |
11221 | * stack. | |
a7036191 DB |
11222 | * |
11223 | * Also, when register is a known constant, we rewrite register-based | |
11224 | * operation to immediate-based, and thus do not need masking (and as | |
11225 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 11226 | */ |
a7036191 | 11227 | if (commit_window || off_is_imm) |
7fedb63a DB |
11228 | return 0; |
11229 | ||
979d63d5 DB |
11230 | /* Simulate and find potential out-of-bounds access under |
11231 | * speculative execution from truncation as a result of | |
11232 | * masking when off was not within expected range. If off | |
11233 | * sits in dst, then we temporarily need to move ptr there | |
11234 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
11235 | * for cases where we use K-based arithmetic in one direction | |
11236 | * and truncated reg-based in the other in order to explore | |
11237 | * bad access. | |
11238 | */ | |
11239 | if (!ptr_is_dst_reg) { | |
11240 | tmp = *dst_reg; | |
71f656a5 | 11241 | copy_register_state(dst_reg, ptr_reg); |
979d63d5 | 11242 | } |
9183671a DB |
11243 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
11244 | env->insn_idx); | |
0803278b | 11245 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 11246 | *dst_reg = tmp; |
a6aaece0 DB |
11247 | return !ret ? REASON_STACK : 0; |
11248 | } | |
11249 | ||
fe9a5ca7 DB |
11250 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
11251 | { | |
11252 | struct bpf_verifier_state *vstate = env->cur_state; | |
11253 | ||
11254 | /* If we simulate paths under speculation, we don't update the | |
11255 | * insn as 'seen' such that when we verify unreachable paths in | |
11256 | * the non-speculative domain, sanitize_dead_code() can still | |
11257 | * rewrite/sanitize them. | |
11258 | */ | |
11259 | if (!vstate->speculative) | |
11260 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
11261 | } | |
11262 | ||
a6aaece0 DB |
11263 | static int sanitize_err(struct bpf_verifier_env *env, |
11264 | const struct bpf_insn *insn, int reason, | |
11265 | const struct bpf_reg_state *off_reg, | |
11266 | const struct bpf_reg_state *dst_reg) | |
11267 | { | |
11268 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
11269 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
11270 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
11271 | ||
11272 | switch (reason) { | |
11273 | case REASON_BOUNDS: | |
11274 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
11275 | off_reg == dst_reg ? dst : src, err); | |
11276 | break; | |
11277 | case REASON_TYPE: | |
11278 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
11279 | off_reg == dst_reg ? src : dst, err); | |
11280 | break; | |
11281 | case REASON_PATHS: | |
11282 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
11283 | dst, op, err); | |
11284 | break; | |
11285 | case REASON_LIMIT: | |
11286 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
11287 | dst, op, err); | |
11288 | break; | |
11289 | case REASON_STACK: | |
11290 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
11291 | dst, err); | |
11292 | break; | |
11293 | default: | |
11294 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
11295 | reason); | |
11296 | break; | |
11297 | } | |
11298 | ||
11299 | return -EACCES; | |
979d63d5 DB |
11300 | } |
11301 | ||
01f810ac AM |
11302 | /* check that stack access falls within stack limits and that 'reg' doesn't |
11303 | * have a variable offset. | |
11304 | * | |
11305 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
11306 | * requires corresponding support in Spectre masking for stack ALU. See also | |
11307 | * retrieve_ptr_limit(). | |
11308 | * | |
11309 | * | |
11310 | * 'off' includes 'reg->off'. | |
11311 | */ | |
11312 | static int check_stack_access_for_ptr_arithmetic( | |
11313 | struct bpf_verifier_env *env, | |
11314 | int regno, | |
11315 | const struct bpf_reg_state *reg, | |
11316 | int off) | |
11317 | { | |
11318 | if (!tnum_is_const(reg->var_off)) { | |
11319 | char tn_buf[48]; | |
11320 | ||
11321 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
11322 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
11323 | regno, tn_buf, off); | |
11324 | return -EACCES; | |
11325 | } | |
11326 | ||
11327 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
11328 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
11329 | "prohibited for !root; off=%d\n", regno, off); | |
11330 | return -EACCES; | |
11331 | } | |
11332 | ||
11333 | return 0; | |
11334 | } | |
11335 | ||
073815b7 DB |
11336 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
11337 | const struct bpf_insn *insn, | |
11338 | const struct bpf_reg_state *dst_reg) | |
11339 | { | |
11340 | u32 dst = insn->dst_reg; | |
11341 | ||
11342 | /* For unprivileged we require that resulting offset must be in bounds | |
11343 | * in order to be able to sanitize access later on. | |
11344 | */ | |
11345 | if (env->bypass_spec_v1) | |
11346 | return 0; | |
11347 | ||
11348 | switch (dst_reg->type) { | |
11349 | case PTR_TO_STACK: | |
11350 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
11351 | dst_reg->off + dst_reg->var_off.value)) | |
11352 | return -EACCES; | |
11353 | break; | |
11354 | case PTR_TO_MAP_VALUE: | |
61df10c7 | 11355 | if (check_map_access(env, dst, dst_reg->off, 1, false, ACCESS_HELPER)) { |
073815b7 DB |
11356 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
11357 | "prohibited for !root\n", dst); | |
11358 | return -EACCES; | |
11359 | } | |
11360 | break; | |
11361 | default: | |
11362 | break; | |
11363 | } | |
11364 | ||
11365 | return 0; | |
11366 | } | |
01f810ac | 11367 | |
f1174f77 | 11368 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
11369 | * Caller should also handle BPF_MOV case separately. |
11370 | * If we return -EACCES, caller may want to try again treating pointer as a | |
11371 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
11372 | */ | |
11373 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
11374 | struct bpf_insn *insn, | |
11375 | const struct bpf_reg_state *ptr_reg, | |
11376 | const struct bpf_reg_state *off_reg) | |
969bf05e | 11377 | { |
f4d7e40a AS |
11378 | struct bpf_verifier_state *vstate = env->cur_state; |
11379 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
11380 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 11381 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
11382 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
11383 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
11384 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
11385 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 11386 | struct bpf_sanitize_info info = {}; |
969bf05e | 11387 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 11388 | u32 dst = insn->dst_reg; |
979d63d5 | 11389 | int ret; |
969bf05e | 11390 | |
f1174f77 | 11391 | dst_reg = ®s[dst]; |
969bf05e | 11392 | |
6f16101e DB |
11393 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
11394 | smin_val > smax_val || umin_val > umax_val) { | |
11395 | /* Taint dst register if offset had invalid bounds derived from | |
11396 | * e.g. dead branches. | |
11397 | */ | |
f54c7898 | 11398 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 11399 | return 0; |
f1174f77 EC |
11400 | } |
11401 | ||
11402 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
11403 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
11404 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
11405 | __mark_reg_unknown(env, dst_reg); | |
11406 | return 0; | |
11407 | } | |
11408 | ||
82abbf8d AS |
11409 | verbose(env, |
11410 | "R%d 32-bit pointer arithmetic prohibited\n", | |
11411 | dst); | |
f1174f77 | 11412 | return -EACCES; |
969bf05e AS |
11413 | } |
11414 | ||
c25b2ae1 | 11415 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
aad2eeaf | 11416 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
c25b2ae1 | 11417 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 11418 | return -EACCES; |
c25b2ae1 HL |
11419 | } |
11420 | ||
11421 | switch (base_type(ptr_reg->type)) { | |
aad2eeaf | 11422 | case CONST_PTR_TO_MAP: |
7c696732 YS |
11423 | /* smin_val represents the known value */ |
11424 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
11425 | break; | |
8731745e | 11426 | fallthrough; |
aad2eeaf | 11427 | case PTR_TO_PACKET_END: |
c64b7983 | 11428 | case PTR_TO_SOCKET: |
46f8bc92 | 11429 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 11430 | case PTR_TO_TCP_SOCK: |
fada7fdc | 11431 | case PTR_TO_XDP_SOCK: |
aad2eeaf | 11432 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
c25b2ae1 | 11433 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 11434 | return -EACCES; |
aad2eeaf JS |
11435 | default: |
11436 | break; | |
f1174f77 EC |
11437 | } |
11438 | ||
11439 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
11440 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 11441 | */ |
f1174f77 EC |
11442 | dst_reg->type = ptr_reg->type; |
11443 | dst_reg->id = ptr_reg->id; | |
969bf05e | 11444 | |
bb7f0f98 AS |
11445 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
11446 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
11447 | return -EINVAL; | |
11448 | ||
3f50f132 JF |
11449 | /* pointer types do not carry 32-bit bounds at the moment. */ |
11450 | __mark_reg32_unbounded(dst_reg); | |
11451 | ||
7fedb63a DB |
11452 | if (sanitize_needed(opcode)) { |
11453 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 11454 | &info, false); |
a6aaece0 DB |
11455 | if (ret < 0) |
11456 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 11457 | } |
a6aaece0 | 11458 | |
f1174f77 EC |
11459 | switch (opcode) { |
11460 | case BPF_ADD: | |
11461 | /* We can take a fixed offset as long as it doesn't overflow | |
11462 | * the s32 'off' field | |
969bf05e | 11463 | */ |
b03c9f9f EC |
11464 | if (known && (ptr_reg->off + smin_val == |
11465 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 11466 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
11467 | dst_reg->smin_value = smin_ptr; |
11468 | dst_reg->smax_value = smax_ptr; | |
11469 | dst_reg->umin_value = umin_ptr; | |
11470 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 11471 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 11472 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 11473 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
11474 | break; |
11475 | } | |
f1174f77 EC |
11476 | /* A new variable offset is created. Note that off_reg->off |
11477 | * == 0, since it's a scalar. | |
11478 | * dst_reg gets the pointer type and since some positive | |
11479 | * integer value was added to the pointer, give it a new 'id' | |
11480 | * if it's a PTR_TO_PACKET. | |
11481 | * this creates a new 'base' pointer, off_reg (variable) gets | |
11482 | * added into the variable offset, and we copy the fixed offset | |
11483 | * from ptr_reg. | |
969bf05e | 11484 | */ |
b03c9f9f EC |
11485 | if (signed_add_overflows(smin_ptr, smin_val) || |
11486 | signed_add_overflows(smax_ptr, smax_val)) { | |
11487 | dst_reg->smin_value = S64_MIN; | |
11488 | dst_reg->smax_value = S64_MAX; | |
11489 | } else { | |
11490 | dst_reg->smin_value = smin_ptr + smin_val; | |
11491 | dst_reg->smax_value = smax_ptr + smax_val; | |
11492 | } | |
11493 | if (umin_ptr + umin_val < umin_ptr || | |
11494 | umax_ptr + umax_val < umax_ptr) { | |
11495 | dst_reg->umin_value = 0; | |
11496 | dst_reg->umax_value = U64_MAX; | |
11497 | } else { | |
11498 | dst_reg->umin_value = umin_ptr + umin_val; | |
11499 | dst_reg->umax_value = umax_ptr + umax_val; | |
11500 | } | |
f1174f77 EC |
11501 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
11502 | dst_reg->off = ptr_reg->off; | |
0962590e | 11503 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 11504 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
11505 | dst_reg->id = ++env->id_gen; |
11506 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 11507 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
11508 | } |
11509 | break; | |
11510 | case BPF_SUB: | |
11511 | if (dst_reg == off_reg) { | |
11512 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
11513 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
11514 | dst); | |
f1174f77 EC |
11515 | return -EACCES; |
11516 | } | |
11517 | /* We don't allow subtraction from FP, because (according to | |
11518 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
11519 | * be able to deal with it. | |
969bf05e | 11520 | */ |
f1174f77 | 11521 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
11522 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
11523 | dst); | |
f1174f77 EC |
11524 | return -EACCES; |
11525 | } | |
b03c9f9f EC |
11526 | if (known && (ptr_reg->off - smin_val == |
11527 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 11528 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
11529 | dst_reg->smin_value = smin_ptr; |
11530 | dst_reg->smax_value = smax_ptr; | |
11531 | dst_reg->umin_value = umin_ptr; | |
11532 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
11533 | dst_reg->var_off = ptr_reg->var_off; |
11534 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 11535 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 11536 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
11537 | break; |
11538 | } | |
f1174f77 EC |
11539 | /* A new variable offset is created. If the subtrahend is known |
11540 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 11541 | */ |
b03c9f9f EC |
11542 | if (signed_sub_overflows(smin_ptr, smax_val) || |
11543 | signed_sub_overflows(smax_ptr, smin_val)) { | |
11544 | /* Overflow possible, we know nothing */ | |
11545 | dst_reg->smin_value = S64_MIN; | |
11546 | dst_reg->smax_value = S64_MAX; | |
11547 | } else { | |
11548 | dst_reg->smin_value = smin_ptr - smax_val; | |
11549 | dst_reg->smax_value = smax_ptr - smin_val; | |
11550 | } | |
11551 | if (umin_ptr < umax_val) { | |
11552 | /* Overflow possible, we know nothing */ | |
11553 | dst_reg->umin_value = 0; | |
11554 | dst_reg->umax_value = U64_MAX; | |
11555 | } else { | |
11556 | /* Cannot overflow (as long as bounds are consistent) */ | |
11557 | dst_reg->umin_value = umin_ptr - umax_val; | |
11558 | dst_reg->umax_value = umax_ptr - umin_val; | |
11559 | } | |
f1174f77 EC |
11560 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
11561 | dst_reg->off = ptr_reg->off; | |
0962590e | 11562 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 11563 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
11564 | dst_reg->id = ++env->id_gen; |
11565 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 11566 | if (smin_val < 0) |
22dc4a0f | 11567 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 11568 | } |
f1174f77 EC |
11569 | break; |
11570 | case BPF_AND: | |
11571 | case BPF_OR: | |
11572 | case BPF_XOR: | |
82abbf8d AS |
11573 | /* bitwise ops on pointers are troublesome, prohibit. */ |
11574 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
11575 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
11576 | return -EACCES; |
11577 | default: | |
11578 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
11579 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
11580 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 11581 | return -EACCES; |
43188702 JF |
11582 | } |
11583 | ||
bb7f0f98 AS |
11584 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
11585 | return -EINVAL; | |
3844d153 | 11586 | reg_bounds_sync(dst_reg); |
073815b7 DB |
11587 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
11588 | return -EACCES; | |
7fedb63a DB |
11589 | if (sanitize_needed(opcode)) { |
11590 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 11591 | &info, true); |
7fedb63a DB |
11592 | if (ret < 0) |
11593 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
11594 | } |
11595 | ||
43188702 JF |
11596 | return 0; |
11597 | } | |
11598 | ||
3f50f132 JF |
11599 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
11600 | struct bpf_reg_state *src_reg) | |
11601 | { | |
11602 | s32 smin_val = src_reg->s32_min_value; | |
11603 | s32 smax_val = src_reg->s32_max_value; | |
11604 | u32 umin_val = src_reg->u32_min_value; | |
11605 | u32 umax_val = src_reg->u32_max_value; | |
11606 | ||
11607 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
11608 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
11609 | dst_reg->s32_min_value = S32_MIN; | |
11610 | dst_reg->s32_max_value = S32_MAX; | |
11611 | } else { | |
11612 | dst_reg->s32_min_value += smin_val; | |
11613 | dst_reg->s32_max_value += smax_val; | |
11614 | } | |
11615 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
11616 | dst_reg->u32_max_value + umax_val < umax_val) { | |
11617 | dst_reg->u32_min_value = 0; | |
11618 | dst_reg->u32_max_value = U32_MAX; | |
11619 | } else { | |
11620 | dst_reg->u32_min_value += umin_val; | |
11621 | dst_reg->u32_max_value += umax_val; | |
11622 | } | |
11623 | } | |
11624 | ||
07cd2631 JF |
11625 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
11626 | struct bpf_reg_state *src_reg) | |
11627 | { | |
11628 | s64 smin_val = src_reg->smin_value; | |
11629 | s64 smax_val = src_reg->smax_value; | |
11630 | u64 umin_val = src_reg->umin_value; | |
11631 | u64 umax_val = src_reg->umax_value; | |
11632 | ||
11633 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
11634 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
11635 | dst_reg->smin_value = S64_MIN; | |
11636 | dst_reg->smax_value = S64_MAX; | |
11637 | } else { | |
11638 | dst_reg->smin_value += smin_val; | |
11639 | dst_reg->smax_value += smax_val; | |
11640 | } | |
11641 | if (dst_reg->umin_value + umin_val < umin_val || | |
11642 | dst_reg->umax_value + umax_val < umax_val) { | |
11643 | dst_reg->umin_value = 0; | |
11644 | dst_reg->umax_value = U64_MAX; | |
11645 | } else { | |
11646 | dst_reg->umin_value += umin_val; | |
11647 | dst_reg->umax_value += umax_val; | |
11648 | } | |
3f50f132 JF |
11649 | } |
11650 | ||
11651 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
11652 | struct bpf_reg_state *src_reg) | |
11653 | { | |
11654 | s32 smin_val = src_reg->s32_min_value; | |
11655 | s32 smax_val = src_reg->s32_max_value; | |
11656 | u32 umin_val = src_reg->u32_min_value; | |
11657 | u32 umax_val = src_reg->u32_max_value; | |
11658 | ||
11659 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
11660 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
11661 | /* Overflow possible, we know nothing */ | |
11662 | dst_reg->s32_min_value = S32_MIN; | |
11663 | dst_reg->s32_max_value = S32_MAX; | |
11664 | } else { | |
11665 | dst_reg->s32_min_value -= smax_val; | |
11666 | dst_reg->s32_max_value -= smin_val; | |
11667 | } | |
11668 | if (dst_reg->u32_min_value < umax_val) { | |
11669 | /* Overflow possible, we know nothing */ | |
11670 | dst_reg->u32_min_value = 0; | |
11671 | dst_reg->u32_max_value = U32_MAX; | |
11672 | } else { | |
11673 | /* Cannot overflow (as long as bounds are consistent) */ | |
11674 | dst_reg->u32_min_value -= umax_val; | |
11675 | dst_reg->u32_max_value -= umin_val; | |
11676 | } | |
07cd2631 JF |
11677 | } |
11678 | ||
11679 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
11680 | struct bpf_reg_state *src_reg) | |
11681 | { | |
11682 | s64 smin_val = src_reg->smin_value; | |
11683 | s64 smax_val = src_reg->smax_value; | |
11684 | u64 umin_val = src_reg->umin_value; | |
11685 | u64 umax_val = src_reg->umax_value; | |
11686 | ||
11687 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
11688 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
11689 | /* Overflow possible, we know nothing */ | |
11690 | dst_reg->smin_value = S64_MIN; | |
11691 | dst_reg->smax_value = S64_MAX; | |
11692 | } else { | |
11693 | dst_reg->smin_value -= smax_val; | |
11694 | dst_reg->smax_value -= smin_val; | |
11695 | } | |
11696 | if (dst_reg->umin_value < umax_val) { | |
11697 | /* Overflow possible, we know nothing */ | |
11698 | dst_reg->umin_value = 0; | |
11699 | dst_reg->umax_value = U64_MAX; | |
11700 | } else { | |
11701 | /* Cannot overflow (as long as bounds are consistent) */ | |
11702 | dst_reg->umin_value -= umax_val; | |
11703 | dst_reg->umax_value -= umin_val; | |
11704 | } | |
3f50f132 JF |
11705 | } |
11706 | ||
11707 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
11708 | struct bpf_reg_state *src_reg) | |
11709 | { | |
11710 | s32 smin_val = src_reg->s32_min_value; | |
11711 | u32 umin_val = src_reg->u32_min_value; | |
11712 | u32 umax_val = src_reg->u32_max_value; | |
11713 | ||
11714 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
11715 | /* Ain't nobody got time to multiply that sign */ | |
11716 | __mark_reg32_unbounded(dst_reg); | |
11717 | return; | |
11718 | } | |
11719 | /* Both values are positive, so we can work with unsigned and | |
11720 | * copy the result to signed (unless it exceeds S32_MAX). | |
11721 | */ | |
11722 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
11723 | /* Potential overflow, we know nothing */ | |
11724 | __mark_reg32_unbounded(dst_reg); | |
11725 | return; | |
11726 | } | |
11727 | dst_reg->u32_min_value *= umin_val; | |
11728 | dst_reg->u32_max_value *= umax_val; | |
11729 | if (dst_reg->u32_max_value > S32_MAX) { | |
11730 | /* Overflow possible, we know nothing */ | |
11731 | dst_reg->s32_min_value = S32_MIN; | |
11732 | dst_reg->s32_max_value = S32_MAX; | |
11733 | } else { | |
11734 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11735 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11736 | } | |
07cd2631 JF |
11737 | } |
11738 | ||
11739 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
11740 | struct bpf_reg_state *src_reg) | |
11741 | { | |
11742 | s64 smin_val = src_reg->smin_value; | |
11743 | u64 umin_val = src_reg->umin_value; | |
11744 | u64 umax_val = src_reg->umax_value; | |
11745 | ||
07cd2631 JF |
11746 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
11747 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 11748 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
11749 | return; |
11750 | } | |
11751 | /* Both values are positive, so we can work with unsigned and | |
11752 | * copy the result to signed (unless it exceeds S64_MAX). | |
11753 | */ | |
11754 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
11755 | /* Potential overflow, we know nothing */ | |
3f50f132 | 11756 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
11757 | return; |
11758 | } | |
11759 | dst_reg->umin_value *= umin_val; | |
11760 | dst_reg->umax_value *= umax_val; | |
11761 | if (dst_reg->umax_value > S64_MAX) { | |
11762 | /* Overflow possible, we know nothing */ | |
11763 | dst_reg->smin_value = S64_MIN; | |
11764 | dst_reg->smax_value = S64_MAX; | |
11765 | } else { | |
11766 | dst_reg->smin_value = dst_reg->umin_value; | |
11767 | dst_reg->smax_value = dst_reg->umax_value; | |
11768 | } | |
11769 | } | |
11770 | ||
3f50f132 JF |
11771 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
11772 | struct bpf_reg_state *src_reg) | |
11773 | { | |
11774 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11775 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11776 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
11777 | s32 smin_val = src_reg->s32_min_value; | |
11778 | u32 umax_val = src_reg->u32_max_value; | |
11779 | ||
049c4e13 DB |
11780 | if (src_known && dst_known) { |
11781 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 11782 | return; |
049c4e13 | 11783 | } |
3f50f132 JF |
11784 | |
11785 | /* We get our minimum from the var_off, since that's inherently | |
11786 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
11787 | */ | |
11788 | dst_reg->u32_min_value = var32_off.value; | |
11789 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
11790 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
11791 | /* Lose signed bounds when ANDing negative numbers, | |
11792 | * ain't nobody got time for that. | |
11793 | */ | |
11794 | dst_reg->s32_min_value = S32_MIN; | |
11795 | dst_reg->s32_max_value = S32_MAX; | |
11796 | } else { | |
11797 | /* ANDing two positives gives a positive, so safe to | |
11798 | * cast result into s64. | |
11799 | */ | |
11800 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11801 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11802 | } | |
3f50f132 JF |
11803 | } |
11804 | ||
07cd2631 JF |
11805 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
11806 | struct bpf_reg_state *src_reg) | |
11807 | { | |
3f50f132 JF |
11808 | bool src_known = tnum_is_const(src_reg->var_off); |
11809 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
11810 | s64 smin_val = src_reg->smin_value; |
11811 | u64 umax_val = src_reg->umax_value; | |
11812 | ||
3f50f132 | 11813 | if (src_known && dst_known) { |
4fbb38a3 | 11814 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
11815 | return; |
11816 | } | |
11817 | ||
07cd2631 JF |
11818 | /* We get our minimum from the var_off, since that's inherently |
11819 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
11820 | */ | |
07cd2631 JF |
11821 | dst_reg->umin_value = dst_reg->var_off.value; |
11822 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
11823 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
11824 | /* Lose signed bounds when ANDing negative numbers, | |
11825 | * ain't nobody got time for that. | |
11826 | */ | |
11827 | dst_reg->smin_value = S64_MIN; | |
11828 | dst_reg->smax_value = S64_MAX; | |
11829 | } else { | |
11830 | /* ANDing two positives gives a positive, so safe to | |
11831 | * cast result into s64. | |
11832 | */ | |
11833 | dst_reg->smin_value = dst_reg->umin_value; | |
11834 | dst_reg->smax_value = dst_reg->umax_value; | |
11835 | } | |
11836 | /* We may learn something more from the var_off */ | |
11837 | __update_reg_bounds(dst_reg); | |
11838 | } | |
11839 | ||
3f50f132 JF |
11840 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
11841 | struct bpf_reg_state *src_reg) | |
11842 | { | |
11843 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11844 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11845 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
11846 | s32 smin_val = src_reg->s32_min_value; |
11847 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 11848 | |
049c4e13 DB |
11849 | if (src_known && dst_known) { |
11850 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 11851 | return; |
049c4e13 | 11852 | } |
3f50f132 JF |
11853 | |
11854 | /* We get our maximum from the var_off, and our minimum is the | |
11855 | * maximum of the operands' minima | |
11856 | */ | |
11857 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
11858 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
11859 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
11860 | /* Lose signed bounds when ORing negative numbers, | |
11861 | * ain't nobody got time for that. | |
11862 | */ | |
11863 | dst_reg->s32_min_value = S32_MIN; | |
11864 | dst_reg->s32_max_value = S32_MAX; | |
11865 | } else { | |
11866 | /* ORing two positives gives a positive, so safe to | |
11867 | * cast result into s64. | |
11868 | */ | |
5b9fbeb7 DB |
11869 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
11870 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
11871 | } |
11872 | } | |
11873 | ||
07cd2631 JF |
11874 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
11875 | struct bpf_reg_state *src_reg) | |
11876 | { | |
3f50f132 JF |
11877 | bool src_known = tnum_is_const(src_reg->var_off); |
11878 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
11879 | s64 smin_val = src_reg->smin_value; |
11880 | u64 umin_val = src_reg->umin_value; | |
11881 | ||
3f50f132 | 11882 | if (src_known && dst_known) { |
4fbb38a3 | 11883 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
11884 | return; |
11885 | } | |
11886 | ||
07cd2631 JF |
11887 | /* We get our maximum from the var_off, and our minimum is the |
11888 | * maximum of the operands' minima | |
11889 | */ | |
07cd2631 JF |
11890 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
11891 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
11892 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
11893 | /* Lose signed bounds when ORing negative numbers, | |
11894 | * ain't nobody got time for that. | |
11895 | */ | |
11896 | dst_reg->smin_value = S64_MIN; | |
11897 | dst_reg->smax_value = S64_MAX; | |
11898 | } else { | |
11899 | /* ORing two positives gives a positive, so safe to | |
11900 | * cast result into s64. | |
11901 | */ | |
11902 | dst_reg->smin_value = dst_reg->umin_value; | |
11903 | dst_reg->smax_value = dst_reg->umax_value; | |
11904 | } | |
11905 | /* We may learn something more from the var_off */ | |
11906 | __update_reg_bounds(dst_reg); | |
11907 | } | |
11908 | ||
2921c90d YS |
11909 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
11910 | struct bpf_reg_state *src_reg) | |
11911 | { | |
11912 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
11913 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
11914 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
11915 | s32 smin_val = src_reg->s32_min_value; | |
11916 | ||
049c4e13 DB |
11917 | if (src_known && dst_known) { |
11918 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 11919 | return; |
049c4e13 | 11920 | } |
2921c90d YS |
11921 | |
11922 | /* We get both minimum and maximum from the var32_off. */ | |
11923 | dst_reg->u32_min_value = var32_off.value; | |
11924 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
11925 | ||
11926 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
11927 | /* XORing two positive sign numbers gives a positive, | |
11928 | * so safe to cast u32 result into s32. | |
11929 | */ | |
11930 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
11931 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
11932 | } else { | |
11933 | dst_reg->s32_min_value = S32_MIN; | |
11934 | dst_reg->s32_max_value = S32_MAX; | |
11935 | } | |
11936 | } | |
11937 | ||
11938 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
11939 | struct bpf_reg_state *src_reg) | |
11940 | { | |
11941 | bool src_known = tnum_is_const(src_reg->var_off); | |
11942 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
11943 | s64 smin_val = src_reg->smin_value; | |
11944 | ||
11945 | if (src_known && dst_known) { | |
11946 | /* dst_reg->var_off.value has been updated earlier */ | |
11947 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
11948 | return; | |
11949 | } | |
11950 | ||
11951 | /* We get both minimum and maximum from the var_off. */ | |
11952 | dst_reg->umin_value = dst_reg->var_off.value; | |
11953 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
11954 | ||
11955 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
11956 | /* XORing two positive sign numbers gives a positive, | |
11957 | * so safe to cast u64 result into s64. | |
11958 | */ | |
11959 | dst_reg->smin_value = dst_reg->umin_value; | |
11960 | dst_reg->smax_value = dst_reg->umax_value; | |
11961 | } else { | |
11962 | dst_reg->smin_value = S64_MIN; | |
11963 | dst_reg->smax_value = S64_MAX; | |
11964 | } | |
11965 | ||
11966 | __update_reg_bounds(dst_reg); | |
11967 | } | |
11968 | ||
3f50f132 JF |
11969 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
11970 | u64 umin_val, u64 umax_val) | |
07cd2631 | 11971 | { |
07cd2631 JF |
11972 | /* We lose all sign bit information (except what we can pick |
11973 | * up from var_off) | |
11974 | */ | |
3f50f132 JF |
11975 | dst_reg->s32_min_value = S32_MIN; |
11976 | dst_reg->s32_max_value = S32_MAX; | |
11977 | /* If we might shift our top bit out, then we know nothing */ | |
11978 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
11979 | dst_reg->u32_min_value = 0; | |
11980 | dst_reg->u32_max_value = U32_MAX; | |
11981 | } else { | |
11982 | dst_reg->u32_min_value <<= umin_val; | |
11983 | dst_reg->u32_max_value <<= umax_val; | |
11984 | } | |
11985 | } | |
11986 | ||
11987 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
11988 | struct bpf_reg_state *src_reg) | |
11989 | { | |
11990 | u32 umax_val = src_reg->u32_max_value; | |
11991 | u32 umin_val = src_reg->u32_min_value; | |
11992 | /* u32 alu operation will zext upper bits */ | |
11993 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
11994 | ||
11995 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
11996 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
11997 | /* Not required but being careful mark reg64 bounds as unknown so | |
11998 | * that we are forced to pick them up from tnum and zext later and | |
11999 | * if some path skips this step we are still safe. | |
12000 | */ | |
12001 | __mark_reg64_unbounded(dst_reg); | |
12002 | __update_reg32_bounds(dst_reg); | |
12003 | } | |
12004 | ||
12005 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
12006 | u64 umin_val, u64 umax_val) | |
12007 | { | |
12008 | /* Special case <<32 because it is a common compiler pattern to sign | |
12009 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
12010 | * positive we know this shift will also be positive so we can track | |
12011 | * bounds correctly. Otherwise we lose all sign bit information except | |
12012 | * what we can pick up from var_off. Perhaps we can generalize this | |
12013 | * later to shifts of any length. | |
12014 | */ | |
12015 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
12016 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
12017 | else | |
12018 | dst_reg->smax_value = S64_MAX; | |
12019 | ||
12020 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
12021 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
12022 | else | |
12023 | dst_reg->smin_value = S64_MIN; | |
12024 | ||
07cd2631 JF |
12025 | /* If we might shift our top bit out, then we know nothing */ |
12026 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
12027 | dst_reg->umin_value = 0; | |
12028 | dst_reg->umax_value = U64_MAX; | |
12029 | } else { | |
12030 | dst_reg->umin_value <<= umin_val; | |
12031 | dst_reg->umax_value <<= umax_val; | |
12032 | } | |
3f50f132 JF |
12033 | } |
12034 | ||
12035 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
12036 | struct bpf_reg_state *src_reg) | |
12037 | { | |
12038 | u64 umax_val = src_reg->umax_value; | |
12039 | u64 umin_val = src_reg->umin_value; | |
12040 | ||
12041 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
12042 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
12043 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
12044 | ||
07cd2631 JF |
12045 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
12046 | /* We may learn something more from the var_off */ | |
12047 | __update_reg_bounds(dst_reg); | |
12048 | } | |
12049 | ||
3f50f132 JF |
12050 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
12051 | struct bpf_reg_state *src_reg) | |
12052 | { | |
12053 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
12054 | u32 umax_val = src_reg->u32_max_value; | |
12055 | u32 umin_val = src_reg->u32_min_value; | |
12056 | ||
12057 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
12058 | * be negative, then either: | |
12059 | * 1) src_reg might be zero, so the sign bit of the result is | |
12060 | * unknown, so we lose our signed bounds | |
12061 | * 2) it's known negative, thus the unsigned bounds capture the | |
12062 | * signed bounds | |
12063 | * 3) the signed bounds cross zero, so they tell us nothing | |
12064 | * about the result | |
12065 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 12066 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
12067 | * Thus, in all cases it suffices to blow away our signed bounds |
12068 | * and rely on inferring new ones from the unsigned bounds and | |
12069 | * var_off of the result. | |
12070 | */ | |
12071 | dst_reg->s32_min_value = S32_MIN; | |
12072 | dst_reg->s32_max_value = S32_MAX; | |
12073 | ||
12074 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
12075 | dst_reg->u32_min_value >>= umax_val; | |
12076 | dst_reg->u32_max_value >>= umin_val; | |
12077 | ||
12078 | __mark_reg64_unbounded(dst_reg); | |
12079 | __update_reg32_bounds(dst_reg); | |
12080 | } | |
12081 | ||
07cd2631 JF |
12082 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
12083 | struct bpf_reg_state *src_reg) | |
12084 | { | |
12085 | u64 umax_val = src_reg->umax_value; | |
12086 | u64 umin_val = src_reg->umin_value; | |
12087 | ||
12088 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
12089 | * be negative, then either: | |
12090 | * 1) src_reg might be zero, so the sign bit of the result is | |
12091 | * unknown, so we lose our signed bounds | |
12092 | * 2) it's known negative, thus the unsigned bounds capture the | |
12093 | * signed bounds | |
12094 | * 3) the signed bounds cross zero, so they tell us nothing | |
12095 | * about the result | |
12096 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 12097 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
12098 | * Thus, in all cases it suffices to blow away our signed bounds |
12099 | * and rely on inferring new ones from the unsigned bounds and | |
12100 | * var_off of the result. | |
12101 | */ | |
12102 | dst_reg->smin_value = S64_MIN; | |
12103 | dst_reg->smax_value = S64_MAX; | |
12104 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
12105 | dst_reg->umin_value >>= umax_val; | |
12106 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
12107 | |
12108 | /* Its not easy to operate on alu32 bounds here because it depends | |
12109 | * on bits being shifted in. Take easy way out and mark unbounded | |
12110 | * so we can recalculate later from tnum. | |
12111 | */ | |
12112 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
12113 | __update_reg_bounds(dst_reg); |
12114 | } | |
12115 | ||
3f50f132 JF |
12116 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
12117 | struct bpf_reg_state *src_reg) | |
07cd2631 | 12118 | { |
3f50f132 | 12119 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
12120 | |
12121 | /* Upon reaching here, src_known is true and | |
12122 | * umax_val is equal to umin_val. | |
12123 | */ | |
3f50f132 JF |
12124 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
12125 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 12126 | |
3f50f132 JF |
12127 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
12128 | ||
12129 | /* blow away the dst_reg umin_value/umax_value and rely on | |
12130 | * dst_reg var_off to refine the result. | |
12131 | */ | |
12132 | dst_reg->u32_min_value = 0; | |
12133 | dst_reg->u32_max_value = U32_MAX; | |
12134 | ||
12135 | __mark_reg64_unbounded(dst_reg); | |
12136 | __update_reg32_bounds(dst_reg); | |
12137 | } | |
12138 | ||
12139 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
12140 | struct bpf_reg_state *src_reg) | |
12141 | { | |
12142 | u64 umin_val = src_reg->umin_value; | |
12143 | ||
12144 | /* Upon reaching here, src_known is true and umax_val is equal | |
12145 | * to umin_val. | |
12146 | */ | |
12147 | dst_reg->smin_value >>= umin_val; | |
12148 | dst_reg->smax_value >>= umin_val; | |
12149 | ||
12150 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
12151 | |
12152 | /* blow away the dst_reg umin_value/umax_value and rely on | |
12153 | * dst_reg var_off to refine the result. | |
12154 | */ | |
12155 | dst_reg->umin_value = 0; | |
12156 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
12157 | |
12158 | /* Its not easy to operate on alu32 bounds here because it depends | |
12159 | * on bits being shifted in from upper 32-bits. Take easy way out | |
12160 | * and mark unbounded so we can recalculate later from tnum. | |
12161 | */ | |
12162 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
12163 | __update_reg_bounds(dst_reg); |
12164 | } | |
12165 | ||
468f6eaf JH |
12166 | /* WARNING: This function does calculations on 64-bit values, but the actual |
12167 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
12168 | * need extra checks in the 32-bit case. | |
12169 | */ | |
f1174f77 EC |
12170 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
12171 | struct bpf_insn *insn, | |
12172 | struct bpf_reg_state *dst_reg, | |
12173 | struct bpf_reg_state src_reg) | |
969bf05e | 12174 | { |
638f5b90 | 12175 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 12176 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 12177 | bool src_known; |
b03c9f9f EC |
12178 | s64 smin_val, smax_val; |
12179 | u64 umin_val, umax_val; | |
3f50f132 JF |
12180 | s32 s32_min_val, s32_max_val; |
12181 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 12182 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 12183 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 12184 | int ret; |
b799207e | 12185 | |
b03c9f9f EC |
12186 | smin_val = src_reg.smin_value; |
12187 | smax_val = src_reg.smax_value; | |
12188 | umin_val = src_reg.umin_value; | |
12189 | umax_val = src_reg.umax_value; | |
f23cc643 | 12190 | |
3f50f132 JF |
12191 | s32_min_val = src_reg.s32_min_value; |
12192 | s32_max_val = src_reg.s32_max_value; | |
12193 | u32_min_val = src_reg.u32_min_value; | |
12194 | u32_max_val = src_reg.u32_max_value; | |
12195 | ||
12196 | if (alu32) { | |
12197 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
12198 | if ((src_known && |
12199 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
12200 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
12201 | /* Taint dst register if offset had invalid bounds | |
12202 | * derived from e.g. dead branches. | |
12203 | */ | |
12204 | __mark_reg_unknown(env, dst_reg); | |
12205 | return 0; | |
12206 | } | |
12207 | } else { | |
12208 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
12209 | if ((src_known && |
12210 | (smin_val != smax_val || umin_val != umax_val)) || | |
12211 | smin_val > smax_val || umin_val > umax_val) { | |
12212 | /* Taint dst register if offset had invalid bounds | |
12213 | * derived from e.g. dead branches. | |
12214 | */ | |
12215 | __mark_reg_unknown(env, dst_reg); | |
12216 | return 0; | |
12217 | } | |
6f16101e DB |
12218 | } |
12219 | ||
bb7f0f98 AS |
12220 | if (!src_known && |
12221 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 12222 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
12223 | return 0; |
12224 | } | |
12225 | ||
f5288193 DB |
12226 | if (sanitize_needed(opcode)) { |
12227 | ret = sanitize_val_alu(env, insn); | |
12228 | if (ret < 0) | |
12229 | return sanitize_err(env, insn, ret, NULL, NULL); | |
12230 | } | |
12231 | ||
3f50f132 JF |
12232 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
12233 | * There are two classes of instructions: The first class we track both | |
12234 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
12235 | * greatest amount of precision when alu operations are mixed with jmp32 | |
12236 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
12237 | * and BPF_OR. This is possible because these ops have fairly easy to | |
12238 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
12239 | * See alu32 verifier tests for examples. The second class of | |
12240 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
12241 | * with regards to tracking sign/unsigned bounds because the bits may | |
12242 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
12243 | * the reg unbounded in the subreg bound space and use the resulting | |
12244 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
12245 | */ | |
48461135 JB |
12246 | switch (opcode) { |
12247 | case BPF_ADD: | |
3f50f132 | 12248 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 12249 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 12250 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
12251 | break; |
12252 | case BPF_SUB: | |
3f50f132 | 12253 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 12254 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 12255 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
12256 | break; |
12257 | case BPF_MUL: | |
3f50f132 JF |
12258 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
12259 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 12260 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
12261 | break; |
12262 | case BPF_AND: | |
3f50f132 JF |
12263 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
12264 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 12265 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
12266 | break; |
12267 | case BPF_OR: | |
3f50f132 JF |
12268 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
12269 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 12270 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 12271 | break; |
2921c90d YS |
12272 | case BPF_XOR: |
12273 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
12274 | scalar32_min_max_xor(dst_reg, &src_reg); | |
12275 | scalar_min_max_xor(dst_reg, &src_reg); | |
12276 | break; | |
48461135 | 12277 | case BPF_LSH: |
468f6eaf JH |
12278 | if (umax_val >= insn_bitness) { |
12279 | /* Shifts greater than 31 or 63 are undefined. | |
12280 | * This includes shifts by a negative number. | |
b03c9f9f | 12281 | */ |
61bd5218 | 12282 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
12283 | break; |
12284 | } | |
3f50f132 JF |
12285 | if (alu32) |
12286 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
12287 | else | |
12288 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
12289 | break; |
12290 | case BPF_RSH: | |
468f6eaf JH |
12291 | if (umax_val >= insn_bitness) { |
12292 | /* Shifts greater than 31 or 63 are undefined. | |
12293 | * This includes shifts by a negative number. | |
b03c9f9f | 12294 | */ |
61bd5218 | 12295 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
12296 | break; |
12297 | } | |
3f50f132 JF |
12298 | if (alu32) |
12299 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
12300 | else | |
12301 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 12302 | break; |
9cbe1f5a YS |
12303 | case BPF_ARSH: |
12304 | if (umax_val >= insn_bitness) { | |
12305 | /* Shifts greater than 31 or 63 are undefined. | |
12306 | * This includes shifts by a negative number. | |
12307 | */ | |
12308 | mark_reg_unknown(env, regs, insn->dst_reg); | |
12309 | break; | |
12310 | } | |
3f50f132 JF |
12311 | if (alu32) |
12312 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
12313 | else | |
12314 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 12315 | break; |
48461135 | 12316 | default: |
61bd5218 | 12317 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
12318 | break; |
12319 | } | |
12320 | ||
3f50f132 JF |
12321 | /* ALU32 ops are zero extended into 64bit register */ |
12322 | if (alu32) | |
12323 | zext_32_to_64(dst_reg); | |
3844d153 | 12324 | reg_bounds_sync(dst_reg); |
f1174f77 EC |
12325 | return 0; |
12326 | } | |
12327 | ||
12328 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
12329 | * and var_off. | |
12330 | */ | |
12331 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
12332 | struct bpf_insn *insn) | |
12333 | { | |
f4d7e40a AS |
12334 | struct bpf_verifier_state *vstate = env->cur_state; |
12335 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
12336 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
12337 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
12338 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 12339 | int err; |
f1174f77 EC |
12340 | |
12341 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
12342 | src_reg = NULL; |
12343 | if (dst_reg->type != SCALAR_VALUE) | |
12344 | ptr_reg = dst_reg; | |
75748837 AS |
12345 | else |
12346 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
12347 | * incorrectly propagated into other registers by find_equal_scalars() | |
12348 | */ | |
12349 | dst_reg->id = 0; | |
f1174f77 EC |
12350 | if (BPF_SRC(insn->code) == BPF_X) { |
12351 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
12352 | if (src_reg->type != SCALAR_VALUE) { |
12353 | if (dst_reg->type != SCALAR_VALUE) { | |
12354 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
12355 | * an arbitrary scalar. Disallow all math except |
12356 | * pointer subtraction | |
f1174f77 | 12357 | */ |
dd066823 | 12358 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
12359 | mark_reg_unknown(env, regs, insn->dst_reg); |
12360 | return 0; | |
f1174f77 | 12361 | } |
82abbf8d AS |
12362 | verbose(env, "R%d pointer %s pointer prohibited\n", |
12363 | insn->dst_reg, | |
12364 | bpf_alu_string[opcode >> 4]); | |
12365 | return -EACCES; | |
f1174f77 EC |
12366 | } else { |
12367 | /* scalar += pointer | |
12368 | * This is legal, but we have to reverse our | |
12369 | * src/dest handling in computing the range | |
12370 | */ | |
b5dc0163 AS |
12371 | err = mark_chain_precision(env, insn->dst_reg); |
12372 | if (err) | |
12373 | return err; | |
82abbf8d AS |
12374 | return adjust_ptr_min_max_vals(env, insn, |
12375 | src_reg, dst_reg); | |
f1174f77 EC |
12376 | } |
12377 | } else if (ptr_reg) { | |
12378 | /* pointer += scalar */ | |
b5dc0163 AS |
12379 | err = mark_chain_precision(env, insn->src_reg); |
12380 | if (err) | |
12381 | return err; | |
82abbf8d AS |
12382 | return adjust_ptr_min_max_vals(env, insn, |
12383 | dst_reg, src_reg); | |
a3b666bf AN |
12384 | } else if (dst_reg->precise) { |
12385 | /* if dst_reg is precise, src_reg should be precise as well */ | |
12386 | err = mark_chain_precision(env, insn->src_reg); | |
12387 | if (err) | |
12388 | return err; | |
f1174f77 EC |
12389 | } |
12390 | } else { | |
12391 | /* Pretend the src is a reg with a known value, since we only | |
12392 | * need to be able to read from this state. | |
12393 | */ | |
12394 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 12395 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 12396 | src_reg = &off_reg; |
82abbf8d AS |
12397 | if (ptr_reg) /* pointer += K */ |
12398 | return adjust_ptr_min_max_vals(env, insn, | |
12399 | ptr_reg, src_reg); | |
f1174f77 EC |
12400 | } |
12401 | ||
12402 | /* Got here implies adding two SCALAR_VALUEs */ | |
12403 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 12404 | print_verifier_state(env, state, true); |
61bd5218 | 12405 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
12406 | return -EINVAL; |
12407 | } | |
12408 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 12409 | print_verifier_state(env, state, true); |
61bd5218 | 12410 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
12411 | return -EINVAL; |
12412 | } | |
12413 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
12414 | } |
12415 | ||
17a52670 | 12416 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 12417 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 12418 | { |
638f5b90 | 12419 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
12420 | u8 opcode = BPF_OP(insn->code); |
12421 | int err; | |
12422 | ||
12423 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
12424 | if (opcode == BPF_NEG) { | |
395e942d | 12425 | if (BPF_SRC(insn->code) != BPF_K || |
17a52670 AS |
12426 | insn->src_reg != BPF_REG_0 || |
12427 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 12428 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
12429 | return -EINVAL; |
12430 | } | |
12431 | } else { | |
12432 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
12433 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
12434 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 12435 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
12436 | return -EINVAL; |
12437 | } | |
12438 | } | |
12439 | ||
12440 | /* check src operand */ | |
dc503a8a | 12441 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12442 | if (err) |
12443 | return err; | |
12444 | ||
1be7f75d | 12445 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 12446 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
12447 | insn->dst_reg); |
12448 | return -EACCES; | |
12449 | } | |
12450 | ||
17a52670 | 12451 | /* check dest operand */ |
dc503a8a | 12452 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
12453 | if (err) |
12454 | return err; | |
12455 | ||
12456 | } else if (opcode == BPF_MOV) { | |
12457 | ||
12458 | if (BPF_SRC(insn->code) == BPF_X) { | |
12459 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 12460 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
12461 | return -EINVAL; |
12462 | } | |
12463 | ||
12464 | /* check src operand */ | |
dc503a8a | 12465 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12466 | if (err) |
12467 | return err; | |
12468 | } else { | |
12469 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 12470 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
12471 | return -EINVAL; |
12472 | } | |
12473 | } | |
12474 | ||
fbeb1603 AF |
12475 | /* check dest operand, mark as required later */ |
12476 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
12477 | if (err) |
12478 | return err; | |
12479 | ||
12480 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
12481 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
12482 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
12483 | ||
17a52670 AS |
12484 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
12485 | /* case: R1 = R2 | |
12486 | * copy register state to dest reg | |
12487 | */ | |
75748837 AS |
12488 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
12489 | /* Assign src and dst registers the same ID | |
12490 | * that will be used by find_equal_scalars() | |
12491 | * to propagate min/max range. | |
12492 | */ | |
12493 | src_reg->id = ++env->id_gen; | |
71f656a5 | 12494 | copy_register_state(dst_reg, src_reg); |
e434b8cd | 12495 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 12496 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 12497 | } else { |
f1174f77 | 12498 | /* R1 = (u32) R2 */ |
1be7f75d | 12499 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
12500 | verbose(env, |
12501 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
12502 | insn->src_reg); |
12503 | return -EACCES; | |
e434b8cd | 12504 | } else if (src_reg->type == SCALAR_VALUE) { |
3be49f79 YS |
12505 | bool is_src_reg_u32 = src_reg->umax_value <= U32_MAX; |
12506 | ||
12507 | if (is_src_reg_u32 && !src_reg->id) | |
12508 | src_reg->id = ++env->id_gen; | |
71f656a5 | 12509 | copy_register_state(dst_reg, src_reg); |
3be49f79 | 12510 | /* Make sure ID is cleared if src_reg is not in u32 range otherwise |
75748837 AS |
12511 | * dst_reg min/max could be incorrectly |
12512 | * propagated into src_reg by find_equal_scalars() | |
12513 | */ | |
3be49f79 YS |
12514 | if (!is_src_reg_u32) |
12515 | dst_reg->id = 0; | |
e434b8cd | 12516 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 12517 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
12518 | } else { |
12519 | mark_reg_unknown(env, regs, | |
12520 | insn->dst_reg); | |
1be7f75d | 12521 | } |
3f50f132 | 12522 | zext_32_to_64(dst_reg); |
3844d153 | 12523 | reg_bounds_sync(dst_reg); |
17a52670 AS |
12524 | } |
12525 | } else { | |
12526 | /* case: R = imm | |
12527 | * remember the value we stored into this reg | |
12528 | */ | |
fbeb1603 AF |
12529 | /* clear any state __mark_reg_known doesn't set */ |
12530 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 12531 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
12532 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
12533 | __mark_reg_known(regs + insn->dst_reg, | |
12534 | insn->imm); | |
12535 | } else { | |
12536 | __mark_reg_known(regs + insn->dst_reg, | |
12537 | (u32)insn->imm); | |
12538 | } | |
17a52670 AS |
12539 | } |
12540 | ||
12541 | } else if (opcode > BPF_END) { | |
61bd5218 | 12542 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
12543 | return -EINVAL; |
12544 | ||
12545 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
12546 | ||
17a52670 AS |
12547 | if (BPF_SRC(insn->code) == BPF_X) { |
12548 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 12549 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
12550 | return -EINVAL; |
12551 | } | |
12552 | /* check src1 operand */ | |
dc503a8a | 12553 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12554 | if (err) |
12555 | return err; | |
12556 | } else { | |
12557 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 12558 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
12559 | return -EINVAL; |
12560 | } | |
12561 | } | |
12562 | ||
12563 | /* check src2 operand */ | |
dc503a8a | 12564 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12565 | if (err) |
12566 | return err; | |
12567 | ||
12568 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
12569 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 12570 | verbose(env, "div by zero\n"); |
17a52670 AS |
12571 | return -EINVAL; |
12572 | } | |
12573 | ||
229394e8 RV |
12574 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
12575 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
12576 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
12577 | ||
12578 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 12579 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
12580 | return -EINVAL; |
12581 | } | |
12582 | } | |
12583 | ||
1a0dc1ac | 12584 | /* check dest operand */ |
dc503a8a | 12585 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
12586 | if (err) |
12587 | return err; | |
12588 | ||
f1174f77 | 12589 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
12590 | } |
12591 | ||
12592 | return 0; | |
12593 | } | |
12594 | ||
f4d7e40a | 12595 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 12596 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 12597 | enum bpf_reg_type type, |
fb2a311a | 12598 | bool range_right_open) |
969bf05e | 12599 | { |
b239da34 KKD |
12600 | struct bpf_func_state *state; |
12601 | struct bpf_reg_state *reg; | |
12602 | int new_range; | |
2d2be8ca | 12603 | |
fb2a311a DB |
12604 | if (dst_reg->off < 0 || |
12605 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
12606 | /* This doesn't give us any range */ |
12607 | return; | |
12608 | ||
b03c9f9f EC |
12609 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
12610 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
12611 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
12612 | * than pkt_end, but that's because it's also less than pkt. | |
12613 | */ | |
12614 | return; | |
12615 | ||
fb2a311a DB |
12616 | new_range = dst_reg->off; |
12617 | if (range_right_open) | |
2fa7d94a | 12618 | new_range++; |
fb2a311a DB |
12619 | |
12620 | /* Examples for register markings: | |
2d2be8ca | 12621 | * |
fb2a311a | 12622 | * pkt_data in dst register: |
2d2be8ca DB |
12623 | * |
12624 | * r2 = r3; | |
12625 | * r2 += 8; | |
12626 | * if (r2 > pkt_end) goto <handle exception> | |
12627 | * <access okay> | |
12628 | * | |
b4e432f1 DB |
12629 | * r2 = r3; |
12630 | * r2 += 8; | |
12631 | * if (r2 < pkt_end) goto <access okay> | |
12632 | * <handle exception> | |
12633 | * | |
2d2be8ca DB |
12634 | * Where: |
12635 | * r2 == dst_reg, pkt_end == src_reg | |
12636 | * r2=pkt(id=n,off=8,r=0) | |
12637 | * r3=pkt(id=n,off=0,r=0) | |
12638 | * | |
fb2a311a | 12639 | * pkt_data in src register: |
2d2be8ca DB |
12640 | * |
12641 | * r2 = r3; | |
12642 | * r2 += 8; | |
12643 | * if (pkt_end >= r2) goto <access okay> | |
12644 | * <handle exception> | |
12645 | * | |
b4e432f1 DB |
12646 | * r2 = r3; |
12647 | * r2 += 8; | |
12648 | * if (pkt_end <= r2) goto <handle exception> | |
12649 | * <access okay> | |
12650 | * | |
2d2be8ca DB |
12651 | * Where: |
12652 | * pkt_end == dst_reg, r2 == src_reg | |
12653 | * r2=pkt(id=n,off=8,r=0) | |
12654 | * r3=pkt(id=n,off=0,r=0) | |
12655 | * | |
12656 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
12657 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
12658 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
12659 | * the check. | |
969bf05e | 12660 | */ |
2d2be8ca | 12661 | |
f1174f77 EC |
12662 | /* If our ids match, then we must have the same max_value. And we |
12663 | * don't care about the other reg's fixed offset, since if it's too big | |
12664 | * the range won't allow anything. | |
12665 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
12666 | */ | |
b239da34 KKD |
12667 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
12668 | if (reg->type == type && reg->id == dst_reg->id) | |
12669 | /* keep the maximum range already checked */ | |
12670 | reg->range = max(reg->range, new_range); | |
12671 | })); | |
969bf05e AS |
12672 | } |
12673 | ||
3f50f132 | 12674 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 12675 | { |
3f50f132 JF |
12676 | struct tnum subreg = tnum_subreg(reg->var_off); |
12677 | s32 sval = (s32)val; | |
a72dafaf | 12678 | |
3f50f132 JF |
12679 | switch (opcode) { |
12680 | case BPF_JEQ: | |
12681 | if (tnum_is_const(subreg)) | |
12682 | return !!tnum_equals_const(subreg, val); | |
13fbcee5 YS |
12683 | else if (val < reg->u32_min_value || val > reg->u32_max_value) |
12684 | return 0; | |
3f50f132 JF |
12685 | break; |
12686 | case BPF_JNE: | |
12687 | if (tnum_is_const(subreg)) | |
12688 | return !tnum_equals_const(subreg, val); | |
13fbcee5 YS |
12689 | else if (val < reg->u32_min_value || val > reg->u32_max_value) |
12690 | return 1; | |
3f50f132 JF |
12691 | break; |
12692 | case BPF_JSET: | |
12693 | if ((~subreg.mask & subreg.value) & val) | |
12694 | return 1; | |
12695 | if (!((subreg.mask | subreg.value) & val)) | |
12696 | return 0; | |
12697 | break; | |
12698 | case BPF_JGT: | |
12699 | if (reg->u32_min_value > val) | |
12700 | return 1; | |
12701 | else if (reg->u32_max_value <= val) | |
12702 | return 0; | |
12703 | break; | |
12704 | case BPF_JSGT: | |
12705 | if (reg->s32_min_value > sval) | |
12706 | return 1; | |
ee114dd6 | 12707 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
12708 | return 0; |
12709 | break; | |
12710 | case BPF_JLT: | |
12711 | if (reg->u32_max_value < val) | |
12712 | return 1; | |
12713 | else if (reg->u32_min_value >= val) | |
12714 | return 0; | |
12715 | break; | |
12716 | case BPF_JSLT: | |
12717 | if (reg->s32_max_value < sval) | |
12718 | return 1; | |
12719 | else if (reg->s32_min_value >= sval) | |
12720 | return 0; | |
12721 | break; | |
12722 | case BPF_JGE: | |
12723 | if (reg->u32_min_value >= val) | |
12724 | return 1; | |
12725 | else if (reg->u32_max_value < val) | |
12726 | return 0; | |
12727 | break; | |
12728 | case BPF_JSGE: | |
12729 | if (reg->s32_min_value >= sval) | |
12730 | return 1; | |
12731 | else if (reg->s32_max_value < sval) | |
12732 | return 0; | |
12733 | break; | |
12734 | case BPF_JLE: | |
12735 | if (reg->u32_max_value <= val) | |
12736 | return 1; | |
12737 | else if (reg->u32_min_value > val) | |
12738 | return 0; | |
12739 | break; | |
12740 | case BPF_JSLE: | |
12741 | if (reg->s32_max_value <= sval) | |
12742 | return 1; | |
12743 | else if (reg->s32_min_value > sval) | |
12744 | return 0; | |
12745 | break; | |
12746 | } | |
4f7b3e82 | 12747 | |
3f50f132 JF |
12748 | return -1; |
12749 | } | |
092ed096 | 12750 | |
3f50f132 JF |
12751 | |
12752 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
12753 | { | |
12754 | s64 sval = (s64)val; | |
a72dafaf | 12755 | |
4f7b3e82 AS |
12756 | switch (opcode) { |
12757 | case BPF_JEQ: | |
12758 | if (tnum_is_const(reg->var_off)) | |
12759 | return !!tnum_equals_const(reg->var_off, val); | |
13fbcee5 YS |
12760 | else if (val < reg->umin_value || val > reg->umax_value) |
12761 | return 0; | |
4f7b3e82 AS |
12762 | break; |
12763 | case BPF_JNE: | |
12764 | if (tnum_is_const(reg->var_off)) | |
12765 | return !tnum_equals_const(reg->var_off, val); | |
13fbcee5 YS |
12766 | else if (val < reg->umin_value || val > reg->umax_value) |
12767 | return 1; | |
4f7b3e82 | 12768 | break; |
960ea056 JK |
12769 | case BPF_JSET: |
12770 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
12771 | return 1; | |
12772 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
12773 | return 0; | |
12774 | break; | |
4f7b3e82 AS |
12775 | case BPF_JGT: |
12776 | if (reg->umin_value > val) | |
12777 | return 1; | |
12778 | else if (reg->umax_value <= val) | |
12779 | return 0; | |
12780 | break; | |
12781 | case BPF_JSGT: | |
a72dafaf | 12782 | if (reg->smin_value > sval) |
4f7b3e82 | 12783 | return 1; |
ee114dd6 | 12784 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
12785 | return 0; |
12786 | break; | |
12787 | case BPF_JLT: | |
12788 | if (reg->umax_value < val) | |
12789 | return 1; | |
12790 | else if (reg->umin_value >= val) | |
12791 | return 0; | |
12792 | break; | |
12793 | case BPF_JSLT: | |
a72dafaf | 12794 | if (reg->smax_value < sval) |
4f7b3e82 | 12795 | return 1; |
a72dafaf | 12796 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
12797 | return 0; |
12798 | break; | |
12799 | case BPF_JGE: | |
12800 | if (reg->umin_value >= val) | |
12801 | return 1; | |
12802 | else if (reg->umax_value < val) | |
12803 | return 0; | |
12804 | break; | |
12805 | case BPF_JSGE: | |
a72dafaf | 12806 | if (reg->smin_value >= sval) |
4f7b3e82 | 12807 | return 1; |
a72dafaf | 12808 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
12809 | return 0; |
12810 | break; | |
12811 | case BPF_JLE: | |
12812 | if (reg->umax_value <= val) | |
12813 | return 1; | |
12814 | else if (reg->umin_value > val) | |
12815 | return 0; | |
12816 | break; | |
12817 | case BPF_JSLE: | |
a72dafaf | 12818 | if (reg->smax_value <= sval) |
4f7b3e82 | 12819 | return 1; |
a72dafaf | 12820 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
12821 | return 0; |
12822 | break; | |
12823 | } | |
12824 | ||
12825 | return -1; | |
12826 | } | |
12827 | ||
3f50f132 JF |
12828 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
12829 | * and return: | |
12830 | * 1 - branch will be taken and "goto target" will be executed | |
12831 | * 0 - branch will not be taken and fall-through to next insn | |
12832 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
12833 | * range [0,10] | |
604dca5e | 12834 | */ |
3f50f132 JF |
12835 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
12836 | bool is_jmp32) | |
604dca5e | 12837 | { |
cac616db JF |
12838 | if (__is_pointer_value(false, reg)) { |
12839 | if (!reg_type_not_null(reg->type)) | |
12840 | return -1; | |
12841 | ||
12842 | /* If pointer is valid tests against zero will fail so we can | |
12843 | * use this to direct branch taken. | |
12844 | */ | |
12845 | if (val != 0) | |
12846 | return -1; | |
12847 | ||
12848 | switch (opcode) { | |
12849 | case BPF_JEQ: | |
12850 | return 0; | |
12851 | case BPF_JNE: | |
12852 | return 1; | |
12853 | default: | |
12854 | return -1; | |
12855 | } | |
12856 | } | |
604dca5e | 12857 | |
3f50f132 JF |
12858 | if (is_jmp32) |
12859 | return is_branch32_taken(reg, val, opcode); | |
12860 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
12861 | } |
12862 | ||
6d94e741 AS |
12863 | static int flip_opcode(u32 opcode) |
12864 | { | |
12865 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
12866 | static const u8 opcode_flip[16] = { | |
12867 | /* these stay the same */ | |
12868 | [BPF_JEQ >> 4] = BPF_JEQ, | |
12869 | [BPF_JNE >> 4] = BPF_JNE, | |
12870 | [BPF_JSET >> 4] = BPF_JSET, | |
12871 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
12872 | [BPF_JGE >> 4] = BPF_JLE, | |
12873 | [BPF_JGT >> 4] = BPF_JLT, | |
12874 | [BPF_JLE >> 4] = BPF_JGE, | |
12875 | [BPF_JLT >> 4] = BPF_JGT, | |
12876 | [BPF_JSGE >> 4] = BPF_JSLE, | |
12877 | [BPF_JSGT >> 4] = BPF_JSLT, | |
12878 | [BPF_JSLE >> 4] = BPF_JSGE, | |
12879 | [BPF_JSLT >> 4] = BPF_JSGT | |
12880 | }; | |
12881 | return opcode_flip[opcode >> 4]; | |
12882 | } | |
12883 | ||
12884 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
12885 | struct bpf_reg_state *src_reg, | |
12886 | u8 opcode) | |
12887 | { | |
12888 | struct bpf_reg_state *pkt; | |
12889 | ||
12890 | if (src_reg->type == PTR_TO_PACKET_END) { | |
12891 | pkt = dst_reg; | |
12892 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
12893 | pkt = src_reg; | |
12894 | opcode = flip_opcode(opcode); | |
12895 | } else { | |
12896 | return -1; | |
12897 | } | |
12898 | ||
12899 | if (pkt->range >= 0) | |
12900 | return -1; | |
12901 | ||
12902 | switch (opcode) { | |
12903 | case BPF_JLE: | |
12904 | /* pkt <= pkt_end */ | |
12905 | fallthrough; | |
12906 | case BPF_JGT: | |
12907 | /* pkt > pkt_end */ | |
12908 | if (pkt->range == BEYOND_PKT_END) | |
12909 | /* pkt has at last one extra byte beyond pkt_end */ | |
12910 | return opcode == BPF_JGT; | |
12911 | break; | |
12912 | case BPF_JLT: | |
12913 | /* pkt < pkt_end */ | |
12914 | fallthrough; | |
12915 | case BPF_JGE: | |
12916 | /* pkt >= pkt_end */ | |
12917 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
12918 | return opcode == BPF_JGE; | |
12919 | break; | |
12920 | } | |
12921 | return -1; | |
12922 | } | |
12923 | ||
48461135 JB |
12924 | /* Adjusts the register min/max values in the case that the dst_reg is the |
12925 | * variable register that we are working on, and src_reg is a constant or we're | |
12926 | * simply doing a BPF_K check. | |
f1174f77 | 12927 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
12928 | */ |
12929 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
12930 | struct bpf_reg_state *false_reg, |
12931 | u64 val, u32 val32, | |
092ed096 | 12932 | u8 opcode, bool is_jmp32) |
48461135 | 12933 | { |
3f50f132 JF |
12934 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
12935 | struct tnum false_64off = false_reg->var_off; | |
12936 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
12937 | struct tnum true_64off = true_reg->var_off; | |
12938 | s64 sval = (s64)val; | |
12939 | s32 sval32 = (s32)val32; | |
a72dafaf | 12940 | |
f1174f77 EC |
12941 | /* If the dst_reg is a pointer, we can't learn anything about its |
12942 | * variable offset from the compare (unless src_reg were a pointer into | |
12943 | * the same object, but we don't bother with that. | |
12944 | * Since false_reg and true_reg have the same type by construction, we | |
12945 | * only need to check one of them for pointerness. | |
12946 | */ | |
12947 | if (__is_pointer_value(false, false_reg)) | |
12948 | return; | |
4cabc5b1 | 12949 | |
48461135 | 12950 | switch (opcode) { |
a12ca627 DB |
12951 | /* JEQ/JNE comparison doesn't change the register equivalence. |
12952 | * | |
12953 | * r1 = r2; | |
12954 | * if (r1 == 42) goto label; | |
12955 | * ... | |
12956 | * label: // here both r1 and r2 are known to be 42. | |
12957 | * | |
12958 | * Hence when marking register as known preserve it's ID. | |
12959 | */ | |
48461135 | 12960 | case BPF_JEQ: |
a12ca627 DB |
12961 | if (is_jmp32) { |
12962 | __mark_reg32_known(true_reg, val32); | |
12963 | true_32off = tnum_subreg(true_reg->var_off); | |
12964 | } else { | |
12965 | ___mark_reg_known(true_reg, val); | |
12966 | true_64off = true_reg->var_off; | |
12967 | } | |
12968 | break; | |
48461135 | 12969 | case BPF_JNE: |
a12ca627 DB |
12970 | if (is_jmp32) { |
12971 | __mark_reg32_known(false_reg, val32); | |
12972 | false_32off = tnum_subreg(false_reg->var_off); | |
12973 | } else { | |
12974 | ___mark_reg_known(false_reg, val); | |
12975 | false_64off = false_reg->var_off; | |
12976 | } | |
48461135 | 12977 | break; |
960ea056 | 12978 | case BPF_JSET: |
3f50f132 JF |
12979 | if (is_jmp32) { |
12980 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
12981 | if (is_power_of_2(val32)) | |
12982 | true_32off = tnum_or(true_32off, | |
12983 | tnum_const(val32)); | |
12984 | } else { | |
12985 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
12986 | if (is_power_of_2(val)) | |
12987 | true_64off = tnum_or(true_64off, | |
12988 | tnum_const(val)); | |
12989 | } | |
960ea056 | 12990 | break; |
48461135 | 12991 | case BPF_JGE: |
a72dafaf JW |
12992 | case BPF_JGT: |
12993 | { | |
3f50f132 JF |
12994 | if (is_jmp32) { |
12995 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
12996 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
12997 | ||
12998 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
12999 | false_umax); | |
13000 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
13001 | true_umin); | |
13002 | } else { | |
13003 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
13004 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
13005 | ||
13006 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
13007 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
13008 | } | |
b03c9f9f | 13009 | break; |
a72dafaf | 13010 | } |
48461135 | 13011 | case BPF_JSGE: |
a72dafaf JW |
13012 | case BPF_JSGT: |
13013 | { | |
3f50f132 JF |
13014 | if (is_jmp32) { |
13015 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
13016 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 13017 | |
3f50f132 JF |
13018 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
13019 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
13020 | } else { | |
13021 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
13022 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
13023 | ||
13024 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
13025 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
13026 | } | |
48461135 | 13027 | break; |
a72dafaf | 13028 | } |
b4e432f1 | 13029 | case BPF_JLE: |
a72dafaf JW |
13030 | case BPF_JLT: |
13031 | { | |
3f50f132 JF |
13032 | if (is_jmp32) { |
13033 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
13034 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
13035 | ||
13036 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
13037 | false_umin); | |
13038 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
13039 | true_umax); | |
13040 | } else { | |
13041 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
13042 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
13043 | ||
13044 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
13045 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
13046 | } | |
b4e432f1 | 13047 | break; |
a72dafaf | 13048 | } |
b4e432f1 | 13049 | case BPF_JSLE: |
a72dafaf JW |
13050 | case BPF_JSLT: |
13051 | { | |
3f50f132 JF |
13052 | if (is_jmp32) { |
13053 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
13054 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 13055 | |
3f50f132 JF |
13056 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
13057 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
13058 | } else { | |
13059 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
13060 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
13061 | ||
13062 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
13063 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
13064 | } | |
b4e432f1 | 13065 | break; |
a72dafaf | 13066 | } |
48461135 | 13067 | default: |
0fc31b10 | 13068 | return; |
48461135 JB |
13069 | } |
13070 | ||
3f50f132 JF |
13071 | if (is_jmp32) { |
13072 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
13073 | tnum_subreg(false_32off)); | |
13074 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
13075 | tnum_subreg(true_32off)); | |
13076 | __reg_combine_32_into_64(false_reg); | |
13077 | __reg_combine_32_into_64(true_reg); | |
13078 | } else { | |
13079 | false_reg->var_off = false_64off; | |
13080 | true_reg->var_off = true_64off; | |
13081 | __reg_combine_64_into_32(false_reg); | |
13082 | __reg_combine_64_into_32(true_reg); | |
13083 | } | |
48461135 JB |
13084 | } |
13085 | ||
f1174f77 EC |
13086 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
13087 | * the variable reg. | |
48461135 JB |
13088 | */ |
13089 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
13090 | struct bpf_reg_state *false_reg, |
13091 | u64 val, u32 val32, | |
092ed096 | 13092 | u8 opcode, bool is_jmp32) |
48461135 | 13093 | { |
6d94e741 | 13094 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
13095 | /* This uses zero as "not present in table"; luckily the zero opcode, |
13096 | * BPF_JA, can't get here. | |
b03c9f9f | 13097 | */ |
0fc31b10 | 13098 | if (opcode) |
3f50f132 | 13099 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
13100 | } |
13101 | ||
13102 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
13103 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
13104 | struct bpf_reg_state *dst_reg) | |
13105 | { | |
b03c9f9f EC |
13106 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
13107 | dst_reg->umin_value); | |
13108 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
13109 | dst_reg->umax_value); | |
13110 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
13111 | dst_reg->smin_value); | |
13112 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
13113 | dst_reg->smax_value); | |
f1174f77 EC |
13114 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
13115 | dst_reg->var_off); | |
3844d153 DB |
13116 | reg_bounds_sync(src_reg); |
13117 | reg_bounds_sync(dst_reg); | |
f1174f77 EC |
13118 | } |
13119 | ||
13120 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
13121 | struct bpf_reg_state *true_dst, | |
13122 | struct bpf_reg_state *false_src, | |
13123 | struct bpf_reg_state *false_dst, | |
13124 | u8 opcode) | |
13125 | { | |
13126 | switch (opcode) { | |
13127 | case BPF_JEQ: | |
13128 | __reg_combine_min_max(true_src, true_dst); | |
13129 | break; | |
13130 | case BPF_JNE: | |
13131 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 13132 | break; |
4cabc5b1 | 13133 | } |
48461135 JB |
13134 | } |
13135 | ||
fd978bf7 JS |
13136 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
13137 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 13138 | bool is_null) |
57a09bf0 | 13139 | { |
c25b2ae1 | 13140 | if (type_may_be_null(reg->type) && reg->id == id && |
fca1aa75 | 13141 | (is_rcu_reg(reg) || !WARN_ON_ONCE(!reg->id))) { |
df57f38a KKD |
13142 | /* Old offset (both fixed and variable parts) should have been |
13143 | * known-zero, because we don't allow pointer arithmetic on | |
13144 | * pointers that might be NULL. If we see this happening, don't | |
13145 | * convert the register. | |
13146 | * | |
13147 | * But in some cases, some helpers that return local kptrs | |
13148 | * advance offset for the returned pointer. In those cases, it | |
13149 | * is fine to expect to see reg->off. | |
13150 | */ | |
13151 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || !tnum_equals_const(reg->var_off, 0))) | |
13152 | return; | |
6a3cd331 DM |
13153 | if (!(type_is_ptr_alloc_obj(reg->type) || type_is_non_owning_ref(reg->type)) && |
13154 | WARN_ON_ONCE(reg->off)) | |
e60b0d12 | 13155 | return; |
6a3cd331 | 13156 | |
f1174f77 EC |
13157 | if (is_null) { |
13158 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
13159 | /* We don't need id and ref_obj_id from this point |
13160 | * onwards anymore, thus we should better reset it, | |
13161 | * so that state pruning has chances to take effect. | |
13162 | */ | |
13163 | reg->id = 0; | |
13164 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
13165 | |
13166 | return; | |
13167 | } | |
13168 | ||
13169 | mark_ptr_not_null_reg(reg); | |
13170 | ||
13171 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 | 13172 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
b239da34 | 13173 | * in release_reference(). |
1b986589 MKL |
13174 | * |
13175 | * reg->id is still used by spin_lock ptr. Other | |
13176 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
13177 | */ |
13178 | reg->id = 0; | |
56f668df | 13179 | } |
57a09bf0 TG |
13180 | } |
13181 | } | |
13182 | ||
13183 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
13184 | * be folded together at some point. | |
13185 | */ | |
840b9615 JS |
13186 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
13187 | bool is_null) | |
57a09bf0 | 13188 | { |
f4d7e40a | 13189 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
b239da34 | 13190 | struct bpf_reg_state *regs = state->regs, *reg; |
1b986589 | 13191 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 13192 | u32 id = regs[regno].id; |
57a09bf0 | 13193 | |
1b986589 MKL |
13194 | if (ref_obj_id && ref_obj_id == id && is_null) |
13195 | /* regs[regno] is in the " == NULL" branch. | |
13196 | * No one could have freed the reference state before | |
13197 | * doing the NULL check. | |
13198 | */ | |
13199 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 13200 | |
b239da34 KKD |
13201 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
13202 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
13203 | })); | |
57a09bf0 TG |
13204 | } |
13205 | ||
5beca081 DB |
13206 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
13207 | struct bpf_reg_state *dst_reg, | |
13208 | struct bpf_reg_state *src_reg, | |
13209 | struct bpf_verifier_state *this_branch, | |
13210 | struct bpf_verifier_state *other_branch) | |
13211 | { | |
13212 | if (BPF_SRC(insn->code) != BPF_X) | |
13213 | return false; | |
13214 | ||
092ed096 JW |
13215 | /* Pointers are always 64-bit. */ |
13216 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
13217 | return false; | |
13218 | ||
5beca081 DB |
13219 | switch (BPF_OP(insn->code)) { |
13220 | case BPF_JGT: | |
13221 | if ((dst_reg->type == PTR_TO_PACKET && | |
13222 | src_reg->type == PTR_TO_PACKET_END) || | |
13223 | (dst_reg->type == PTR_TO_PACKET_META && | |
13224 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13225 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
13226 | find_good_pkt_pointers(this_branch, dst_reg, | |
13227 | dst_reg->type, false); | |
6d94e741 | 13228 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
13229 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13230 | src_reg->type == PTR_TO_PACKET) || | |
13231 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13232 | src_reg->type == PTR_TO_PACKET_META)) { | |
13233 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
13234 | find_good_pkt_pointers(other_branch, src_reg, | |
13235 | src_reg->type, true); | |
6d94e741 | 13236 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
13237 | } else { |
13238 | return false; | |
13239 | } | |
13240 | break; | |
13241 | case BPF_JLT: | |
13242 | if ((dst_reg->type == PTR_TO_PACKET && | |
13243 | src_reg->type == PTR_TO_PACKET_END) || | |
13244 | (dst_reg->type == PTR_TO_PACKET_META && | |
13245 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13246 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
13247 | find_good_pkt_pointers(other_branch, dst_reg, | |
13248 | dst_reg->type, true); | |
6d94e741 | 13249 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
13250 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13251 | src_reg->type == PTR_TO_PACKET) || | |
13252 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13253 | src_reg->type == PTR_TO_PACKET_META)) { | |
13254 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
13255 | find_good_pkt_pointers(this_branch, src_reg, | |
13256 | src_reg->type, false); | |
6d94e741 | 13257 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
13258 | } else { |
13259 | return false; | |
13260 | } | |
13261 | break; | |
13262 | case BPF_JGE: | |
13263 | if ((dst_reg->type == PTR_TO_PACKET && | |
13264 | src_reg->type == PTR_TO_PACKET_END) || | |
13265 | (dst_reg->type == PTR_TO_PACKET_META && | |
13266 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13267 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
13268 | find_good_pkt_pointers(this_branch, dst_reg, | |
13269 | dst_reg->type, true); | |
6d94e741 | 13270 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
13271 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13272 | src_reg->type == PTR_TO_PACKET) || | |
13273 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13274 | src_reg->type == PTR_TO_PACKET_META)) { | |
13275 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
13276 | find_good_pkt_pointers(other_branch, src_reg, | |
13277 | src_reg->type, false); | |
6d94e741 | 13278 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
13279 | } else { |
13280 | return false; | |
13281 | } | |
13282 | break; | |
13283 | case BPF_JLE: | |
13284 | if ((dst_reg->type == PTR_TO_PACKET && | |
13285 | src_reg->type == PTR_TO_PACKET_END) || | |
13286 | (dst_reg->type == PTR_TO_PACKET_META && | |
13287 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
13288 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
13289 | find_good_pkt_pointers(other_branch, dst_reg, | |
13290 | dst_reg->type, false); | |
6d94e741 | 13291 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
13292 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
13293 | src_reg->type == PTR_TO_PACKET) || | |
13294 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
13295 | src_reg->type == PTR_TO_PACKET_META)) { | |
13296 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
13297 | find_good_pkt_pointers(this_branch, src_reg, | |
13298 | src_reg->type, true); | |
6d94e741 | 13299 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
13300 | } else { |
13301 | return false; | |
13302 | } | |
13303 | break; | |
13304 | default: | |
13305 | return false; | |
13306 | } | |
13307 | ||
13308 | return true; | |
13309 | } | |
13310 | ||
75748837 AS |
13311 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
13312 | struct bpf_reg_state *known_reg) | |
13313 | { | |
13314 | struct bpf_func_state *state; | |
13315 | struct bpf_reg_state *reg; | |
75748837 | 13316 | |
b239da34 KKD |
13317 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
13318 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
71f656a5 | 13319 | copy_register_state(reg, known_reg); |
b239da34 | 13320 | })); |
75748837 AS |
13321 | } |
13322 | ||
58e2af8b | 13323 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
13324 | struct bpf_insn *insn, int *insn_idx) |
13325 | { | |
f4d7e40a AS |
13326 | struct bpf_verifier_state *this_branch = env->cur_state; |
13327 | struct bpf_verifier_state *other_branch; | |
13328 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 13329 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
befae758 | 13330 | struct bpf_reg_state *eq_branch_regs; |
17a52670 | 13331 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 13332 | bool is_jmp32; |
fb8d251e | 13333 | int pred = -1; |
17a52670 AS |
13334 | int err; |
13335 | ||
092ed096 JW |
13336 | /* Only conditional jumps are expected to reach here. */ |
13337 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
13338 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
13339 | return -EINVAL; |
13340 | } | |
13341 | ||
13342 | if (BPF_SRC(insn->code) == BPF_X) { | |
13343 | if (insn->imm != 0) { | |
092ed096 | 13344 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
13345 | return -EINVAL; |
13346 | } | |
13347 | ||
13348 | /* check src1 operand */ | |
dc503a8a | 13349 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
13350 | if (err) |
13351 | return err; | |
1be7f75d AS |
13352 | |
13353 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 13354 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
13355 | insn->src_reg); |
13356 | return -EACCES; | |
13357 | } | |
fb8d251e | 13358 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
13359 | } else { |
13360 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 13361 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
13362 | return -EINVAL; |
13363 | } | |
13364 | } | |
13365 | ||
13366 | /* check src2 operand */ | |
dc503a8a | 13367 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
13368 | if (err) |
13369 | return err; | |
13370 | ||
1a0dc1ac | 13371 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 13372 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 13373 | |
3f50f132 JF |
13374 | if (BPF_SRC(insn->code) == BPF_K) { |
13375 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
13376 | } else if (src_reg->type == SCALAR_VALUE && | |
13377 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
13378 | pred = is_branch_taken(dst_reg, | |
13379 | tnum_subreg(src_reg->var_off).value, | |
13380 | opcode, | |
13381 | is_jmp32); | |
13382 | } else if (src_reg->type == SCALAR_VALUE && | |
13383 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
13384 | pred = is_branch_taken(dst_reg, | |
13385 | src_reg->var_off.value, | |
13386 | opcode, | |
13387 | is_jmp32); | |
953d9f5b YS |
13388 | } else if (dst_reg->type == SCALAR_VALUE && |
13389 | is_jmp32 && tnum_is_const(tnum_subreg(dst_reg->var_off))) { | |
13390 | pred = is_branch_taken(src_reg, | |
13391 | tnum_subreg(dst_reg->var_off).value, | |
13392 | flip_opcode(opcode), | |
13393 | is_jmp32); | |
13394 | } else if (dst_reg->type == SCALAR_VALUE && | |
13395 | !is_jmp32 && tnum_is_const(dst_reg->var_off)) { | |
13396 | pred = is_branch_taken(src_reg, | |
13397 | dst_reg->var_off.value, | |
13398 | flip_opcode(opcode), | |
13399 | is_jmp32); | |
6d94e741 AS |
13400 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
13401 | reg_is_pkt_pointer_any(src_reg) && | |
13402 | !is_jmp32) { | |
13403 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
13404 | } |
13405 | ||
b5dc0163 | 13406 | if (pred >= 0) { |
cac616db JF |
13407 | /* If we get here with a dst_reg pointer type it is because |
13408 | * above is_branch_taken() special cased the 0 comparison. | |
13409 | */ | |
13410 | if (!__is_pointer_value(false, dst_reg)) | |
13411 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
13412 | if (BPF_SRC(insn->code) == BPF_X && !err && |
13413 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
13414 | err = mark_chain_precision(env, insn->src_reg); |
13415 | if (err) | |
13416 | return err; | |
13417 | } | |
9183671a | 13418 | |
fb8d251e | 13419 | if (pred == 1) { |
9183671a DB |
13420 | /* Only follow the goto, ignore fall-through. If needed, push |
13421 | * the fall-through branch for simulation under speculative | |
13422 | * execution. | |
13423 | */ | |
13424 | if (!env->bypass_spec_v1 && | |
13425 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
13426 | *insn_idx)) | |
13427 | return -EFAULT; | |
fb8d251e AS |
13428 | *insn_idx += insn->off; |
13429 | return 0; | |
13430 | } else if (pred == 0) { | |
9183671a DB |
13431 | /* Only follow the fall-through branch, since that's where the |
13432 | * program will go. If needed, push the goto branch for | |
13433 | * simulation under speculative execution. | |
fb8d251e | 13434 | */ |
9183671a DB |
13435 | if (!env->bypass_spec_v1 && |
13436 | !sanitize_speculative_path(env, insn, | |
13437 | *insn_idx + insn->off + 1, | |
13438 | *insn_idx)) | |
13439 | return -EFAULT; | |
fb8d251e | 13440 | return 0; |
17a52670 AS |
13441 | } |
13442 | ||
979d63d5 DB |
13443 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
13444 | false); | |
17a52670 AS |
13445 | if (!other_branch) |
13446 | return -EFAULT; | |
f4d7e40a | 13447 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 13448 | |
48461135 JB |
13449 | /* detect if we are comparing against a constant value so we can adjust |
13450 | * our min/max values for our dst register. | |
f1174f77 | 13451 | * this is only legit if both are scalars (or pointers to the same |
befae758 EZ |
13452 | * object, I suppose, see the PTR_MAYBE_NULL related if block below), |
13453 | * because otherwise the different base pointers mean the offsets aren't | |
f1174f77 | 13454 | * comparable. |
48461135 JB |
13455 | */ |
13456 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 13457 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 13458 | |
f1174f77 | 13459 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
13460 | src_reg->type == SCALAR_VALUE) { |
13461 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
13462 | (is_jmp32 && |
13463 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 13464 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 13465 | dst_reg, |
3f50f132 JF |
13466 | src_reg->var_off.value, |
13467 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
13468 | opcode, is_jmp32); |
13469 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
13470 | (is_jmp32 && |
13471 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 13472 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 13473 | src_reg, |
3f50f132 JF |
13474 | dst_reg->var_off.value, |
13475 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
13476 | opcode, is_jmp32); |
13477 | else if (!is_jmp32 && | |
13478 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 13479 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
13480 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
13481 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 13482 | src_reg, dst_reg, opcode); |
e688c3db AS |
13483 | if (src_reg->id && |
13484 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
13485 | find_equal_scalars(this_branch, src_reg); |
13486 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
13487 | } | |
13488 | ||
f1174f77 EC |
13489 | } |
13490 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 13491 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
13492 | dst_reg, insn->imm, (u32)insn->imm, |
13493 | opcode, is_jmp32); | |
48461135 JB |
13494 | } |
13495 | ||
e688c3db AS |
13496 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
13497 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
13498 | find_equal_scalars(this_branch, dst_reg); |
13499 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
13500 | } | |
13501 | ||
befae758 EZ |
13502 | /* if one pointer register is compared to another pointer |
13503 | * register check if PTR_MAYBE_NULL could be lifted. | |
13504 | * E.g. register A - maybe null | |
13505 | * register B - not null | |
13506 | * for JNE A, B, ... - A is not null in the false branch; | |
13507 | * for JEQ A, B, ... - A is not null in the true branch. | |
8374bfd5 HS |
13508 | * |
13509 | * Since PTR_TO_BTF_ID points to a kernel struct that does | |
13510 | * not need to be null checked by the BPF program, i.e., | |
13511 | * could be null even without PTR_MAYBE_NULL marking, so | |
13512 | * only propagate nullness when neither reg is that type. | |
befae758 EZ |
13513 | */ |
13514 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_X && | |
13515 | __is_pointer_value(false, src_reg) && __is_pointer_value(false, dst_reg) && | |
8374bfd5 HS |
13516 | type_may_be_null(src_reg->type) != type_may_be_null(dst_reg->type) && |
13517 | base_type(src_reg->type) != PTR_TO_BTF_ID && | |
13518 | base_type(dst_reg->type) != PTR_TO_BTF_ID) { | |
befae758 EZ |
13519 | eq_branch_regs = NULL; |
13520 | switch (opcode) { | |
13521 | case BPF_JEQ: | |
13522 | eq_branch_regs = other_branch_regs; | |
13523 | break; | |
13524 | case BPF_JNE: | |
13525 | eq_branch_regs = regs; | |
13526 | break; | |
13527 | default: | |
13528 | /* do nothing */ | |
13529 | break; | |
13530 | } | |
13531 | if (eq_branch_regs) { | |
13532 | if (type_may_be_null(src_reg->type)) | |
13533 | mark_ptr_not_null_reg(&eq_branch_regs[insn->src_reg]); | |
13534 | else | |
13535 | mark_ptr_not_null_reg(&eq_branch_regs[insn->dst_reg]); | |
13536 | } | |
13537 | } | |
13538 | ||
092ed096 JW |
13539 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
13540 | * NOTE: these optimizations below are related with pointer comparison | |
13541 | * which will never be JMP32. | |
13542 | */ | |
13543 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 13544 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
c25b2ae1 | 13545 | type_may_be_null(dst_reg->type)) { |
840b9615 | 13546 | /* Mark all identical registers in each branch as either |
57a09bf0 TG |
13547 | * safe or unknown depending R == 0 or R != 0 conditional. |
13548 | */ | |
840b9615 JS |
13549 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
13550 | opcode == BPF_JNE); | |
13551 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
13552 | opcode == BPF_JEQ); | |
5beca081 DB |
13553 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
13554 | this_branch, other_branch) && | |
13555 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
13556 | verbose(env, "R%d pointer comparison prohibited\n", |
13557 | insn->dst_reg); | |
1be7f75d | 13558 | return -EACCES; |
17a52670 | 13559 | } |
06ee7115 | 13560 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 13561 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
13562 | return 0; |
13563 | } | |
13564 | ||
17a52670 | 13565 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 13566 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 13567 | { |
d8eca5bb | 13568 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 13569 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 13570 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 13571 | struct bpf_map *map; |
17a52670 AS |
13572 | int err; |
13573 | ||
13574 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 13575 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
13576 | return -EINVAL; |
13577 | } | |
13578 | if (insn->off != 0) { | |
61bd5218 | 13579 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
13580 | return -EINVAL; |
13581 | } | |
13582 | ||
dc503a8a | 13583 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
13584 | if (err) |
13585 | return err; | |
13586 | ||
4976b718 | 13587 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 13588 | if (insn->src_reg == 0) { |
6b173873 JK |
13589 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
13590 | ||
4976b718 | 13591 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 13592 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 13593 | return 0; |
6b173873 | 13594 | } |
17a52670 | 13595 | |
d400a6cf DB |
13596 | /* All special src_reg cases are listed below. From this point onwards |
13597 | * we either succeed and assign a corresponding dst_reg->type after | |
13598 | * zeroing the offset, or fail and reject the program. | |
13599 | */ | |
13600 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 13601 | |
d400a6cf | 13602 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
4976b718 | 13603 | dst_reg->type = aux->btf_var.reg_type; |
34d3a78c | 13604 | switch (base_type(dst_reg->type)) { |
4976b718 HL |
13605 | case PTR_TO_MEM: |
13606 | dst_reg->mem_size = aux->btf_var.mem_size; | |
13607 | break; | |
13608 | case PTR_TO_BTF_ID: | |
22dc4a0f | 13609 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
13610 | dst_reg->btf_id = aux->btf_var.btf_id; |
13611 | break; | |
13612 | default: | |
13613 | verbose(env, "bpf verifier is misconfigured\n"); | |
13614 | return -EFAULT; | |
13615 | } | |
13616 | return 0; | |
13617 | } | |
13618 | ||
69c087ba YS |
13619 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
13620 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
13621 | u32 subprogno = find_subprog(env, |
13622 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
13623 | |
13624 | if (!aux->func_info) { | |
13625 | verbose(env, "missing btf func_info\n"); | |
13626 | return -EINVAL; | |
13627 | } | |
13628 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
13629 | verbose(env, "callback function not static\n"); | |
13630 | return -EINVAL; | |
13631 | } | |
13632 | ||
13633 | dst_reg->type = PTR_TO_FUNC; | |
13634 | dst_reg->subprogno = subprogno; | |
13635 | return 0; | |
13636 | } | |
13637 | ||
d8eca5bb | 13638 | map = env->used_maps[aux->map_index]; |
4976b718 | 13639 | dst_reg->map_ptr = map; |
d8eca5bb | 13640 | |
387544bf AS |
13641 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
13642 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
13643 | dst_reg->type = PTR_TO_MAP_VALUE; |
13644 | dst_reg->off = aux->map_off; | |
d0d78c1d KKD |
13645 | WARN_ON_ONCE(map->max_entries != 1); |
13646 | /* We want reg->id to be same (0) as map_value is not distinct */ | |
387544bf AS |
13647 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
13648 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 13649 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
13650 | } else { |
13651 | verbose(env, "bpf verifier is misconfigured\n"); | |
13652 | return -EINVAL; | |
13653 | } | |
17a52670 | 13654 | |
17a52670 AS |
13655 | return 0; |
13656 | } | |
13657 | ||
96be4325 DB |
13658 | static bool may_access_skb(enum bpf_prog_type type) |
13659 | { | |
13660 | switch (type) { | |
13661 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
13662 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 13663 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
13664 | return true; |
13665 | default: | |
13666 | return false; | |
13667 | } | |
13668 | } | |
13669 | ||
ddd872bc AS |
13670 | /* verify safety of LD_ABS|LD_IND instructions: |
13671 | * - they can only appear in the programs where ctx == skb | |
13672 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
13673 | * preserve R6-R9, and store return value into R0 | |
13674 | * | |
13675 | * Implicit input: | |
13676 | * ctx == skb == R6 == CTX | |
13677 | * | |
13678 | * Explicit input: | |
13679 | * SRC == any register | |
13680 | * IMM == 32-bit immediate | |
13681 | * | |
13682 | * Output: | |
13683 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
13684 | */ | |
58e2af8b | 13685 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 13686 | { |
638f5b90 | 13687 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 13688 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 13689 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
13690 | int i, err; |
13691 | ||
7e40781c | 13692 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 13693 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
13694 | return -EINVAL; |
13695 | } | |
13696 | ||
e0cea7ce DB |
13697 | if (!env->ops->gen_ld_abs) { |
13698 | verbose(env, "bpf verifier is misconfigured\n"); | |
13699 | return -EINVAL; | |
13700 | } | |
13701 | ||
ddd872bc | 13702 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 13703 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 13704 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 13705 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
13706 | return -EINVAL; |
13707 | } | |
13708 | ||
13709 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 13710 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
13711 | if (err) |
13712 | return err; | |
13713 | ||
fd978bf7 JS |
13714 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
13715 | * gen_ld_abs() may terminate the program at runtime, leading to | |
13716 | * reference leak. | |
13717 | */ | |
13718 | err = check_reference_leak(env); | |
13719 | if (err) { | |
13720 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
13721 | return err; | |
13722 | } | |
13723 | ||
d0d78c1d | 13724 | if (env->cur_state->active_lock.ptr) { |
d83525ca AS |
13725 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
13726 | return -EINVAL; | |
13727 | } | |
13728 | ||
9bb00b28 YS |
13729 | if (env->cur_state->active_rcu_lock) { |
13730 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_rcu_read_lock-ed region\n"); | |
13731 | return -EINVAL; | |
13732 | } | |
13733 | ||
6d4f151a | 13734 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
13735 | verbose(env, |
13736 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
13737 | return -EINVAL; |
13738 | } | |
13739 | ||
13740 | if (mode == BPF_IND) { | |
13741 | /* check explicit source operand */ | |
dc503a8a | 13742 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
13743 | if (err) |
13744 | return err; | |
13745 | } | |
13746 | ||
be80a1d3 | 13747 | err = check_ptr_off_reg(env, ®s[ctx_reg], ctx_reg); |
6d4f151a DB |
13748 | if (err < 0) |
13749 | return err; | |
13750 | ||
ddd872bc | 13751 | /* reset caller saved regs to unreadable */ |
dc503a8a | 13752 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 13753 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
13754 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
13755 | } | |
ddd872bc AS |
13756 | |
13757 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
13758 | * the value fetched from the packet. |
13759 | * Already marked as written above. | |
ddd872bc | 13760 | */ |
61bd5218 | 13761 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
13762 | /* ld_abs load up to 32-bit skb data. */ |
13763 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
13764 | return 0; |
13765 | } | |
13766 | ||
390ee7e2 AS |
13767 | static int check_return_code(struct bpf_verifier_env *env) |
13768 | { | |
5cf1e914 | 13769 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 13770 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
13771 | struct bpf_reg_state *reg; |
13772 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 13773 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 13774 | int err; |
bfc6bb74 AS |
13775 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
13776 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 13777 | |
9e4e01df | 13778 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
d1a6edec SF |
13779 | if (!is_subprog) { |
13780 | switch (prog_type) { | |
13781 | case BPF_PROG_TYPE_LSM: | |
13782 | if (prog->expected_attach_type == BPF_LSM_CGROUP) | |
13783 | /* See below, can be 0 or 0-1 depending on hook. */ | |
13784 | break; | |
13785 | fallthrough; | |
13786 | case BPF_PROG_TYPE_STRUCT_OPS: | |
13787 | if (!prog->aux->attach_func_proto->type) | |
13788 | return 0; | |
13789 | break; | |
13790 | default: | |
13791 | break; | |
13792 | } | |
13793 | } | |
27ae7997 | 13794 | |
8fb33b60 | 13795 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
13796 | * to return the value from eBPF program. |
13797 | * Make sure that it's readable at this time | |
13798 | * of bpf_exit, which means that program wrote | |
13799 | * something into it earlier | |
13800 | */ | |
13801 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
13802 | if (err) | |
13803 | return err; | |
13804 | ||
13805 | if (is_pointer_value(env, BPF_REG_0)) { | |
13806 | verbose(env, "R0 leaks addr as return value\n"); | |
13807 | return -EACCES; | |
13808 | } | |
390ee7e2 | 13809 | |
f782e2c3 | 13810 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
13811 | |
13812 | if (frame->in_async_callback_fn) { | |
13813 | /* enforce return zero from async callbacks like timer */ | |
13814 | if (reg->type != SCALAR_VALUE) { | |
13815 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
c25b2ae1 | 13816 | reg_type_str(env, reg->type)); |
bfc6bb74 AS |
13817 | return -EINVAL; |
13818 | } | |
13819 | ||
13820 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
13821 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
13822 | return -EINVAL; | |
13823 | } | |
13824 | return 0; | |
13825 | } | |
13826 | ||
f782e2c3 DB |
13827 | if (is_subprog) { |
13828 | if (reg->type != SCALAR_VALUE) { | |
13829 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
c25b2ae1 | 13830 | reg_type_str(env, reg->type)); |
f782e2c3 DB |
13831 | return -EINVAL; |
13832 | } | |
13833 | return 0; | |
13834 | } | |
13835 | ||
7e40781c | 13836 | switch (prog_type) { |
983695fa DB |
13837 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
13838 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
13839 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
13840 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
13841 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
13842 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
13843 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 13844 | range = tnum_range(1, 1); |
77241217 SF |
13845 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
13846 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
13847 | range = tnum_range(0, 3); | |
ed4ed404 | 13848 | break; |
390ee7e2 | 13849 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 13850 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
13851 | range = tnum_range(0, 3); | |
13852 | enforce_attach_type_range = tnum_range(2, 3); | |
13853 | } | |
ed4ed404 | 13854 | break; |
390ee7e2 AS |
13855 | case BPF_PROG_TYPE_CGROUP_SOCK: |
13856 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 13857 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 13858 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 13859 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 13860 | break; |
15ab09bd AS |
13861 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
13862 | if (!env->prog->aux->attach_btf_id) | |
13863 | return 0; | |
13864 | range = tnum_const(0); | |
13865 | break; | |
15d83c4d | 13866 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
13867 | switch (env->prog->expected_attach_type) { |
13868 | case BPF_TRACE_FENTRY: | |
13869 | case BPF_TRACE_FEXIT: | |
13870 | range = tnum_const(0); | |
13871 | break; | |
13872 | case BPF_TRACE_RAW_TP: | |
13873 | case BPF_MODIFY_RETURN: | |
15d83c4d | 13874 | return 0; |
2ec0616e DB |
13875 | case BPF_TRACE_ITER: |
13876 | break; | |
e92888c7 YS |
13877 | default: |
13878 | return -ENOTSUPP; | |
13879 | } | |
15d83c4d | 13880 | break; |
e9ddbb77 JS |
13881 | case BPF_PROG_TYPE_SK_LOOKUP: |
13882 | range = tnum_range(SK_DROP, SK_PASS); | |
13883 | break; | |
69fd337a SF |
13884 | |
13885 | case BPF_PROG_TYPE_LSM: | |
13886 | if (env->prog->expected_attach_type != BPF_LSM_CGROUP) { | |
13887 | /* Regular BPF_PROG_TYPE_LSM programs can return | |
13888 | * any value. | |
13889 | */ | |
13890 | return 0; | |
13891 | } | |
13892 | if (!env->prog->aux->attach_func_proto->type) { | |
13893 | /* Make sure programs that attach to void | |
13894 | * hooks don't try to modify return value. | |
13895 | */ | |
13896 | range = tnum_range(1, 1); | |
13897 | } | |
13898 | break; | |
13899 | ||
fd9c663b FW |
13900 | case BPF_PROG_TYPE_NETFILTER: |
13901 | range = tnum_range(NF_DROP, NF_ACCEPT); | |
13902 | break; | |
e92888c7 YS |
13903 | case BPF_PROG_TYPE_EXT: |
13904 | /* freplace program can return anything as its return value | |
13905 | * depends on the to-be-replaced kernel func or bpf program. | |
13906 | */ | |
390ee7e2 AS |
13907 | default: |
13908 | return 0; | |
13909 | } | |
13910 | ||
390ee7e2 | 13911 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 13912 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
c25b2ae1 | 13913 | reg_type_str(env, reg->type)); |
390ee7e2 AS |
13914 | return -EINVAL; |
13915 | } | |
13916 | ||
13917 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 13918 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
69fd337a | 13919 | if (prog->expected_attach_type == BPF_LSM_CGROUP && |
d1a6edec | 13920 | prog_type == BPF_PROG_TYPE_LSM && |
69fd337a SF |
13921 | !prog->aux->attach_func_proto->type) |
13922 | verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
390ee7e2 AS |
13923 | return -EINVAL; |
13924 | } | |
5cf1e914 | 13925 | |
13926 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
13927 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
13928 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
13929 | return 0; |
13930 | } | |
13931 | ||
475fb78f AS |
13932 | /* non-recursive DFS pseudo code |
13933 | * 1 procedure DFS-iterative(G,v): | |
13934 | * 2 label v as discovered | |
13935 | * 3 let S be a stack | |
13936 | * 4 S.push(v) | |
13937 | * 5 while S is not empty | |
b6d20799 | 13938 | * 6 t <- S.peek() |
475fb78f AS |
13939 | * 7 if t is what we're looking for: |
13940 | * 8 return t | |
13941 | * 9 for all edges e in G.adjacentEdges(t) do | |
13942 | * 10 if edge e is already labelled | |
13943 | * 11 continue with the next edge | |
13944 | * 12 w <- G.adjacentVertex(t,e) | |
13945 | * 13 if vertex w is not discovered and not explored | |
13946 | * 14 label e as tree-edge | |
13947 | * 15 label w as discovered | |
13948 | * 16 S.push(w) | |
13949 | * 17 continue at 5 | |
13950 | * 18 else if vertex w is discovered | |
13951 | * 19 label e as back-edge | |
13952 | * 20 else | |
13953 | * 21 // vertex w is explored | |
13954 | * 22 label e as forward- or cross-edge | |
13955 | * 23 label t as explored | |
13956 | * 24 S.pop() | |
13957 | * | |
13958 | * convention: | |
13959 | * 0x10 - discovered | |
13960 | * 0x11 - discovered and fall-through edge labelled | |
13961 | * 0x12 - discovered and fall-through and branch edges labelled | |
13962 | * 0x20 - explored | |
13963 | */ | |
13964 | ||
13965 | enum { | |
13966 | DISCOVERED = 0x10, | |
13967 | EXPLORED = 0x20, | |
13968 | FALLTHROUGH = 1, | |
13969 | BRANCH = 2, | |
13970 | }; | |
13971 | ||
dc2a4ebc AS |
13972 | static u32 state_htab_size(struct bpf_verifier_env *env) |
13973 | { | |
13974 | return env->prog->len; | |
13975 | } | |
13976 | ||
5d839021 AS |
13977 | static struct bpf_verifier_state_list **explored_state( |
13978 | struct bpf_verifier_env *env, | |
13979 | int idx) | |
13980 | { | |
dc2a4ebc AS |
13981 | struct bpf_verifier_state *cur = env->cur_state; |
13982 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
13983 | ||
13984 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
13985 | } |
13986 | ||
bffdeaa8 | 13987 | static void mark_prune_point(struct bpf_verifier_env *env, int idx) |
5d839021 | 13988 | { |
a8f500af | 13989 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 13990 | } |
f1bca824 | 13991 | |
bffdeaa8 AN |
13992 | static bool is_prune_point(struct bpf_verifier_env *env, int insn_idx) |
13993 | { | |
13994 | return env->insn_aux_data[insn_idx].prune_point; | |
13995 | } | |
13996 | ||
4b5ce570 AN |
13997 | static void mark_force_checkpoint(struct bpf_verifier_env *env, int idx) |
13998 | { | |
13999 | env->insn_aux_data[idx].force_checkpoint = true; | |
14000 | } | |
14001 | ||
14002 | static bool is_force_checkpoint(struct bpf_verifier_env *env, int insn_idx) | |
14003 | { | |
14004 | return env->insn_aux_data[insn_idx].force_checkpoint; | |
14005 | } | |
14006 | ||
14007 | ||
59e2e27d WAF |
14008 | enum { |
14009 | DONE_EXPLORING = 0, | |
14010 | KEEP_EXPLORING = 1, | |
14011 | }; | |
14012 | ||
475fb78f AS |
14013 | /* t, w, e - match pseudo-code above: |
14014 | * t - index of current instruction | |
14015 | * w - next instruction | |
14016 | * e - edge | |
14017 | */ | |
2589726d AS |
14018 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
14019 | bool loop_ok) | |
475fb78f | 14020 | { |
7df737e9 AS |
14021 | int *insn_stack = env->cfg.insn_stack; |
14022 | int *insn_state = env->cfg.insn_state; | |
14023 | ||
475fb78f | 14024 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 14025 | return DONE_EXPLORING; |
475fb78f AS |
14026 | |
14027 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 14028 | return DONE_EXPLORING; |
475fb78f AS |
14029 | |
14030 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 14031 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 14032 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
14033 | return -EINVAL; |
14034 | } | |
14035 | ||
bffdeaa8 | 14036 | if (e == BRANCH) { |
f1bca824 | 14037 | /* mark branch target for state pruning */ |
bffdeaa8 AN |
14038 | mark_prune_point(env, w); |
14039 | mark_jmp_point(env, w); | |
14040 | } | |
f1bca824 | 14041 | |
475fb78f AS |
14042 | if (insn_state[w] == 0) { |
14043 | /* tree-edge */ | |
14044 | insn_state[t] = DISCOVERED | e; | |
14045 | insn_state[w] = DISCOVERED; | |
7df737e9 | 14046 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 14047 | return -E2BIG; |
7df737e9 | 14048 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 14049 | return KEEP_EXPLORING; |
475fb78f | 14050 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 14051 | if (loop_ok && env->bpf_capable) |
59e2e27d | 14052 | return DONE_EXPLORING; |
d9762e84 MKL |
14053 | verbose_linfo(env, t, "%d: ", t); |
14054 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 14055 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
14056 | return -EINVAL; |
14057 | } else if (insn_state[w] == EXPLORED) { | |
14058 | /* forward- or cross-edge */ | |
14059 | insn_state[t] = DISCOVERED | e; | |
14060 | } else { | |
61bd5218 | 14061 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
14062 | return -EFAULT; |
14063 | } | |
59e2e27d WAF |
14064 | return DONE_EXPLORING; |
14065 | } | |
14066 | ||
dcb2288b | 14067 | static int visit_func_call_insn(int t, struct bpf_insn *insns, |
efdb22de YS |
14068 | struct bpf_verifier_env *env, |
14069 | bool visit_callee) | |
14070 | { | |
14071 | int ret; | |
14072 | ||
14073 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
14074 | if (ret) | |
14075 | return ret; | |
14076 | ||
618945fb AN |
14077 | mark_prune_point(env, t + 1); |
14078 | /* when we exit from subprog, we need to record non-linear history */ | |
14079 | mark_jmp_point(env, t + 1); | |
14080 | ||
efdb22de | 14081 | if (visit_callee) { |
bffdeaa8 | 14082 | mark_prune_point(env, t); |
86fc6ee6 AS |
14083 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
14084 | /* It's ok to allow recursion from CFG point of | |
14085 | * view. __check_func_call() will do the actual | |
14086 | * check. | |
14087 | */ | |
14088 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
14089 | } |
14090 | return ret; | |
14091 | } | |
14092 | ||
59e2e27d WAF |
14093 | /* Visits the instruction at index t and returns one of the following: |
14094 | * < 0 - an error occurred | |
14095 | * DONE_EXPLORING - the instruction was fully explored | |
14096 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
14097 | */ | |
dcb2288b | 14098 | static int visit_insn(int t, struct bpf_verifier_env *env) |
59e2e27d | 14099 | { |
653ae3a8 | 14100 | struct bpf_insn *insns = env->prog->insnsi, *insn = &insns[t]; |
59e2e27d WAF |
14101 | int ret; |
14102 | ||
653ae3a8 | 14103 | if (bpf_pseudo_func(insn)) |
dcb2288b | 14104 | return visit_func_call_insn(t, insns, env, true); |
69c087ba | 14105 | |
59e2e27d | 14106 | /* All non-branch instructions have a single fall-through edge. */ |
653ae3a8 AN |
14107 | if (BPF_CLASS(insn->code) != BPF_JMP && |
14108 | BPF_CLASS(insn->code) != BPF_JMP32) | |
59e2e27d WAF |
14109 | return push_insn(t, t + 1, FALLTHROUGH, env, false); |
14110 | ||
653ae3a8 | 14111 | switch (BPF_OP(insn->code)) { |
59e2e27d WAF |
14112 | case BPF_EXIT: |
14113 | return DONE_EXPLORING; | |
14114 | ||
14115 | case BPF_CALL: | |
c1ee85a9 | 14116 | if (insn->src_reg == 0 && insn->imm == BPF_FUNC_timer_set_callback) |
618945fb AN |
14117 | /* Mark this call insn as a prune point to trigger |
14118 | * is_state_visited() check before call itself is | |
14119 | * processed by __check_func_call(). Otherwise new | |
14120 | * async state will be pushed for further exploration. | |
bfc6bb74 | 14121 | */ |
bffdeaa8 | 14122 | mark_prune_point(env, t); |
06accc87 AN |
14123 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
14124 | struct bpf_kfunc_call_arg_meta meta; | |
14125 | ||
14126 | ret = fetch_kfunc_meta(env, insn, &meta, NULL); | |
4b5ce570 | 14127 | if (ret == 0 && is_iter_next_kfunc(&meta)) { |
06accc87 | 14128 | mark_prune_point(env, t); |
4b5ce570 AN |
14129 | /* Checking and saving state checkpoints at iter_next() call |
14130 | * is crucial for fast convergence of open-coded iterator loop | |
14131 | * logic, so we need to force it. If we don't do that, | |
14132 | * is_state_visited() might skip saving a checkpoint, causing | |
14133 | * unnecessarily long sequence of not checkpointed | |
14134 | * instructions and jumps, leading to exhaustion of jump | |
14135 | * history buffer, and potentially other undesired outcomes. | |
14136 | * It is expected that with correct open-coded iterators | |
14137 | * convergence will happen quickly, so we don't run a risk of | |
14138 | * exhausting memory. | |
14139 | */ | |
14140 | mark_force_checkpoint(env, t); | |
14141 | } | |
06accc87 | 14142 | } |
653ae3a8 | 14143 | return visit_func_call_insn(t, insns, env, insn->src_reg == BPF_PSEUDO_CALL); |
59e2e27d WAF |
14144 | |
14145 | case BPF_JA: | |
653ae3a8 | 14146 | if (BPF_SRC(insn->code) != BPF_K) |
59e2e27d WAF |
14147 | return -EINVAL; |
14148 | ||
14149 | /* unconditional jump with single edge */ | |
653ae3a8 | 14150 | ret = push_insn(t, t + insn->off + 1, FALLTHROUGH, env, |
59e2e27d WAF |
14151 | true); |
14152 | if (ret) | |
14153 | return ret; | |
14154 | ||
653ae3a8 AN |
14155 | mark_prune_point(env, t + insn->off + 1); |
14156 | mark_jmp_point(env, t + insn->off + 1); | |
59e2e27d WAF |
14157 | |
14158 | return ret; | |
14159 | ||
14160 | default: | |
14161 | /* conditional jump with two edges */ | |
bffdeaa8 | 14162 | mark_prune_point(env, t); |
618945fb | 14163 | |
59e2e27d WAF |
14164 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
14165 | if (ret) | |
14166 | return ret; | |
14167 | ||
653ae3a8 | 14168 | return push_insn(t, t + insn->off + 1, BRANCH, env, true); |
59e2e27d | 14169 | } |
475fb78f AS |
14170 | } |
14171 | ||
14172 | /* non-recursive depth-first-search to detect loops in BPF program | |
14173 | * loop == back-edge in directed graph | |
14174 | */ | |
58e2af8b | 14175 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 14176 | { |
475fb78f | 14177 | int insn_cnt = env->prog->len; |
7df737e9 | 14178 | int *insn_stack, *insn_state; |
475fb78f | 14179 | int ret = 0; |
59e2e27d | 14180 | int i; |
475fb78f | 14181 | |
7df737e9 | 14182 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
14183 | if (!insn_state) |
14184 | return -ENOMEM; | |
14185 | ||
7df737e9 | 14186 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 14187 | if (!insn_stack) { |
71dde681 | 14188 | kvfree(insn_state); |
475fb78f AS |
14189 | return -ENOMEM; |
14190 | } | |
14191 | ||
14192 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
14193 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 14194 | env->cfg.cur_stack = 1; |
475fb78f | 14195 | |
59e2e27d WAF |
14196 | while (env->cfg.cur_stack > 0) { |
14197 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 14198 | |
dcb2288b | 14199 | ret = visit_insn(t, env); |
59e2e27d WAF |
14200 | switch (ret) { |
14201 | case DONE_EXPLORING: | |
14202 | insn_state[t] = EXPLORED; | |
14203 | env->cfg.cur_stack--; | |
14204 | break; | |
14205 | case KEEP_EXPLORING: | |
14206 | break; | |
14207 | default: | |
14208 | if (ret > 0) { | |
14209 | verbose(env, "visit_insn internal bug\n"); | |
14210 | ret = -EFAULT; | |
475fb78f | 14211 | } |
475fb78f | 14212 | goto err_free; |
59e2e27d | 14213 | } |
475fb78f AS |
14214 | } |
14215 | ||
59e2e27d | 14216 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 14217 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
14218 | ret = -EFAULT; |
14219 | goto err_free; | |
14220 | } | |
475fb78f | 14221 | |
475fb78f AS |
14222 | for (i = 0; i < insn_cnt; i++) { |
14223 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 14224 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
14225 | ret = -EINVAL; |
14226 | goto err_free; | |
14227 | } | |
14228 | } | |
14229 | ret = 0; /* cfg looks good */ | |
14230 | ||
14231 | err_free: | |
71dde681 AS |
14232 | kvfree(insn_state); |
14233 | kvfree(insn_stack); | |
7df737e9 | 14234 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
14235 | return ret; |
14236 | } | |
14237 | ||
09b28d76 AS |
14238 | static int check_abnormal_return(struct bpf_verifier_env *env) |
14239 | { | |
14240 | int i; | |
14241 | ||
14242 | for (i = 1; i < env->subprog_cnt; i++) { | |
14243 | if (env->subprog_info[i].has_ld_abs) { | |
14244 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
14245 | return -EINVAL; | |
14246 | } | |
14247 | if (env->subprog_info[i].has_tail_call) { | |
14248 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
14249 | return -EINVAL; | |
14250 | } | |
14251 | } | |
14252 | return 0; | |
14253 | } | |
14254 | ||
838e9690 YS |
14255 | /* The minimum supported BTF func info size */ |
14256 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
14257 | #define MAX_FUNCINFO_REC_SIZE 252 | |
14258 | ||
c454a46b MKL |
14259 | static int check_btf_func(struct bpf_verifier_env *env, |
14260 | const union bpf_attr *attr, | |
af2ac3e1 | 14261 | bpfptr_t uattr) |
838e9690 | 14262 | { |
09b28d76 | 14263 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 14264 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 14265 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 14266 | struct bpf_func_info *krecord; |
8c1b6e69 | 14267 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
14268 | struct bpf_prog *prog; |
14269 | const struct btf *btf; | |
af2ac3e1 | 14270 | bpfptr_t urecord; |
d0b2818e | 14271 | u32 prev_offset = 0; |
09b28d76 | 14272 | bool scalar_return; |
e7ed83d6 | 14273 | int ret = -ENOMEM; |
838e9690 YS |
14274 | |
14275 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
14276 | if (!nfuncs) { |
14277 | if (check_abnormal_return(env)) | |
14278 | return -EINVAL; | |
838e9690 | 14279 | return 0; |
09b28d76 | 14280 | } |
838e9690 YS |
14281 | |
14282 | if (nfuncs != env->subprog_cnt) { | |
14283 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
14284 | return -EINVAL; | |
14285 | } | |
14286 | ||
14287 | urec_size = attr->func_info_rec_size; | |
14288 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
14289 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
14290 | urec_size % sizeof(u32)) { | |
14291 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
14292 | return -EINVAL; | |
14293 | } | |
14294 | ||
c454a46b MKL |
14295 | prog = env->prog; |
14296 | btf = prog->aux->btf; | |
838e9690 | 14297 | |
af2ac3e1 | 14298 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
14299 | min_size = min_t(u32, krec_size, urec_size); |
14300 | ||
ba64e7d8 | 14301 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
14302 | if (!krecord) |
14303 | return -ENOMEM; | |
8c1b6e69 AS |
14304 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
14305 | if (!info_aux) | |
14306 | goto err_free; | |
ba64e7d8 | 14307 | |
838e9690 YS |
14308 | for (i = 0; i < nfuncs; i++) { |
14309 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
14310 | if (ret) { | |
14311 | if (ret == -E2BIG) { | |
14312 | verbose(env, "nonzero tailing record in func info"); | |
14313 | /* set the size kernel expects so loader can zero | |
14314 | * out the rest of the record. | |
14315 | */ | |
af2ac3e1 AS |
14316 | if (copy_to_bpfptr_offset(uattr, |
14317 | offsetof(union bpf_attr, func_info_rec_size), | |
14318 | &min_size, sizeof(min_size))) | |
838e9690 YS |
14319 | ret = -EFAULT; |
14320 | } | |
c454a46b | 14321 | goto err_free; |
838e9690 YS |
14322 | } |
14323 | ||
af2ac3e1 | 14324 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 14325 | ret = -EFAULT; |
c454a46b | 14326 | goto err_free; |
838e9690 YS |
14327 | } |
14328 | ||
d30d42e0 | 14329 | /* check insn_off */ |
09b28d76 | 14330 | ret = -EINVAL; |
838e9690 | 14331 | if (i == 0) { |
d30d42e0 | 14332 | if (krecord[i].insn_off) { |
838e9690 | 14333 | verbose(env, |
d30d42e0 MKL |
14334 | "nonzero insn_off %u for the first func info record", |
14335 | krecord[i].insn_off); | |
c454a46b | 14336 | goto err_free; |
838e9690 | 14337 | } |
d30d42e0 | 14338 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
14339 | verbose(env, |
14340 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 14341 | krecord[i].insn_off, prev_offset); |
c454a46b | 14342 | goto err_free; |
838e9690 YS |
14343 | } |
14344 | ||
d30d42e0 | 14345 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 14346 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 14347 | goto err_free; |
838e9690 YS |
14348 | } |
14349 | ||
14350 | /* check type_id */ | |
ba64e7d8 | 14351 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 14352 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 14353 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 14354 | krecord[i].type_id); |
c454a46b | 14355 | goto err_free; |
838e9690 | 14356 | } |
51c39bb1 | 14357 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
14358 | |
14359 | func_proto = btf_type_by_id(btf, type->type); | |
14360 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
14361 | /* btf_func_check() already verified it during BTF load */ | |
14362 | goto err_free; | |
14363 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
14364 | scalar_return = | |
6089fb32 | 14365 | btf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type); |
09b28d76 AS |
14366 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
14367 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
14368 | goto err_free; | |
14369 | } | |
14370 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
14371 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
14372 | goto err_free; | |
14373 | } | |
14374 | ||
d30d42e0 | 14375 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 14376 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
14377 | } |
14378 | ||
ba64e7d8 YS |
14379 | prog->aux->func_info = krecord; |
14380 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 14381 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
14382 | return 0; |
14383 | ||
c454a46b | 14384 | err_free: |
ba64e7d8 | 14385 | kvfree(krecord); |
8c1b6e69 | 14386 | kfree(info_aux); |
838e9690 YS |
14387 | return ret; |
14388 | } | |
14389 | ||
ba64e7d8 YS |
14390 | static void adjust_btf_func(struct bpf_verifier_env *env) |
14391 | { | |
8c1b6e69 | 14392 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
14393 | int i; |
14394 | ||
8c1b6e69 | 14395 | if (!aux->func_info) |
ba64e7d8 YS |
14396 | return; |
14397 | ||
14398 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 14399 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
14400 | } |
14401 | ||
1b773d00 | 14402 | #define MIN_BPF_LINEINFO_SIZE offsetofend(struct bpf_line_info, line_col) |
c454a46b MKL |
14403 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
14404 | ||
14405 | static int check_btf_line(struct bpf_verifier_env *env, | |
14406 | const union bpf_attr *attr, | |
af2ac3e1 | 14407 | bpfptr_t uattr) |
c454a46b MKL |
14408 | { |
14409 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
14410 | struct bpf_subprog_info *sub; | |
14411 | struct bpf_line_info *linfo; | |
14412 | struct bpf_prog *prog; | |
14413 | const struct btf *btf; | |
af2ac3e1 | 14414 | bpfptr_t ulinfo; |
c454a46b MKL |
14415 | int err; |
14416 | ||
14417 | nr_linfo = attr->line_info_cnt; | |
14418 | if (!nr_linfo) | |
14419 | return 0; | |
0e6491b5 BC |
14420 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
14421 | return -EINVAL; | |
c454a46b MKL |
14422 | |
14423 | rec_size = attr->line_info_rec_size; | |
14424 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
14425 | rec_size > MAX_LINEINFO_REC_SIZE || | |
14426 | rec_size & (sizeof(u32) - 1)) | |
14427 | return -EINVAL; | |
14428 | ||
14429 | /* Need to zero it in case the userspace may | |
14430 | * pass in a smaller bpf_line_info object. | |
14431 | */ | |
14432 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
14433 | GFP_KERNEL | __GFP_NOWARN); | |
14434 | if (!linfo) | |
14435 | return -ENOMEM; | |
14436 | ||
14437 | prog = env->prog; | |
14438 | btf = prog->aux->btf; | |
14439 | ||
14440 | s = 0; | |
14441 | sub = env->subprog_info; | |
af2ac3e1 | 14442 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
14443 | expected_size = sizeof(struct bpf_line_info); |
14444 | ncopy = min_t(u32, expected_size, rec_size); | |
14445 | for (i = 0; i < nr_linfo; i++) { | |
14446 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
14447 | if (err) { | |
14448 | if (err == -E2BIG) { | |
14449 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
14450 | if (copy_to_bpfptr_offset(uattr, |
14451 | offsetof(union bpf_attr, line_info_rec_size), | |
14452 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
14453 | err = -EFAULT; |
14454 | } | |
14455 | goto err_free; | |
14456 | } | |
14457 | ||
af2ac3e1 | 14458 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
14459 | err = -EFAULT; |
14460 | goto err_free; | |
14461 | } | |
14462 | ||
14463 | /* | |
14464 | * Check insn_off to ensure | |
14465 | * 1) strictly increasing AND | |
14466 | * 2) bounded by prog->len | |
14467 | * | |
14468 | * The linfo[0].insn_off == 0 check logically falls into | |
14469 | * the later "missing bpf_line_info for func..." case | |
14470 | * because the first linfo[0].insn_off must be the | |
14471 | * first sub also and the first sub must have | |
14472 | * subprog_info[0].start == 0. | |
14473 | */ | |
14474 | if ((i && linfo[i].insn_off <= prev_offset) || | |
14475 | linfo[i].insn_off >= prog->len) { | |
14476 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
14477 | i, linfo[i].insn_off, prev_offset, | |
14478 | prog->len); | |
14479 | err = -EINVAL; | |
14480 | goto err_free; | |
14481 | } | |
14482 | ||
fdbaa0be MKL |
14483 | if (!prog->insnsi[linfo[i].insn_off].code) { |
14484 | verbose(env, | |
14485 | "Invalid insn code at line_info[%u].insn_off\n", | |
14486 | i); | |
14487 | err = -EINVAL; | |
14488 | goto err_free; | |
14489 | } | |
14490 | ||
23127b33 MKL |
14491 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
14492 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
14493 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
14494 | err = -EINVAL; | |
14495 | goto err_free; | |
14496 | } | |
14497 | ||
14498 | if (s != env->subprog_cnt) { | |
14499 | if (linfo[i].insn_off == sub[s].start) { | |
14500 | sub[s].linfo_idx = i; | |
14501 | s++; | |
14502 | } else if (sub[s].start < linfo[i].insn_off) { | |
14503 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
14504 | err = -EINVAL; | |
14505 | goto err_free; | |
14506 | } | |
14507 | } | |
14508 | ||
14509 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 14510 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
14511 | } |
14512 | ||
14513 | if (s != env->subprog_cnt) { | |
14514 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
14515 | env->subprog_cnt - s, s); | |
14516 | err = -EINVAL; | |
14517 | goto err_free; | |
14518 | } | |
14519 | ||
14520 | prog->aux->linfo = linfo; | |
14521 | prog->aux->nr_linfo = nr_linfo; | |
14522 | ||
14523 | return 0; | |
14524 | ||
14525 | err_free: | |
14526 | kvfree(linfo); | |
14527 | return err; | |
14528 | } | |
14529 | ||
fbd94c7a AS |
14530 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
14531 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
14532 | ||
14533 | static int check_core_relo(struct bpf_verifier_env *env, | |
14534 | const union bpf_attr *attr, | |
14535 | bpfptr_t uattr) | |
14536 | { | |
14537 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
14538 | struct bpf_core_relo core_relo = {}; | |
14539 | struct bpf_prog *prog = env->prog; | |
14540 | const struct btf *btf = prog->aux->btf; | |
14541 | struct bpf_core_ctx ctx = { | |
14542 | .log = &env->log, | |
14543 | .btf = btf, | |
14544 | }; | |
14545 | bpfptr_t u_core_relo; | |
14546 | int err; | |
14547 | ||
14548 | nr_core_relo = attr->core_relo_cnt; | |
14549 | if (!nr_core_relo) | |
14550 | return 0; | |
14551 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
14552 | return -EINVAL; | |
14553 | ||
14554 | rec_size = attr->core_relo_rec_size; | |
14555 | if (rec_size < MIN_CORE_RELO_SIZE || | |
14556 | rec_size > MAX_CORE_RELO_SIZE || | |
14557 | rec_size % sizeof(u32)) | |
14558 | return -EINVAL; | |
14559 | ||
14560 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
14561 | expected_size = sizeof(struct bpf_core_relo); | |
14562 | ncopy = min_t(u32, expected_size, rec_size); | |
14563 | ||
14564 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
14565 | * relocation record one at a time. | |
14566 | */ | |
14567 | for (i = 0; i < nr_core_relo; i++) { | |
14568 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
14569 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
14570 | if (err) { | |
14571 | if (err == -E2BIG) { | |
14572 | verbose(env, "nonzero tailing record in core_relo"); | |
14573 | if (copy_to_bpfptr_offset(uattr, | |
14574 | offsetof(union bpf_attr, core_relo_rec_size), | |
14575 | &expected_size, sizeof(expected_size))) | |
14576 | err = -EFAULT; | |
14577 | } | |
14578 | break; | |
14579 | } | |
14580 | ||
14581 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
14582 | err = -EFAULT; | |
14583 | break; | |
14584 | } | |
14585 | ||
14586 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
14587 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
14588 | i, core_relo.insn_off, prog->len); | |
14589 | err = -EINVAL; | |
14590 | break; | |
14591 | } | |
14592 | ||
14593 | err = bpf_core_apply(&ctx, &core_relo, i, | |
14594 | &prog->insnsi[core_relo.insn_off / 8]); | |
14595 | if (err) | |
14596 | break; | |
14597 | bpfptr_add(&u_core_relo, rec_size); | |
14598 | } | |
14599 | return err; | |
14600 | } | |
14601 | ||
c454a46b MKL |
14602 | static int check_btf_info(struct bpf_verifier_env *env, |
14603 | const union bpf_attr *attr, | |
af2ac3e1 | 14604 | bpfptr_t uattr) |
c454a46b MKL |
14605 | { |
14606 | struct btf *btf; | |
14607 | int err; | |
14608 | ||
09b28d76 AS |
14609 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
14610 | if (check_abnormal_return(env)) | |
14611 | return -EINVAL; | |
c454a46b | 14612 | return 0; |
09b28d76 | 14613 | } |
c454a46b MKL |
14614 | |
14615 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
14616 | if (IS_ERR(btf)) | |
14617 | return PTR_ERR(btf); | |
350a5c4d AS |
14618 | if (btf_is_kernel(btf)) { |
14619 | btf_put(btf); | |
14620 | return -EACCES; | |
14621 | } | |
c454a46b MKL |
14622 | env->prog->aux->btf = btf; |
14623 | ||
14624 | err = check_btf_func(env, attr, uattr); | |
14625 | if (err) | |
14626 | return err; | |
14627 | ||
14628 | err = check_btf_line(env, attr, uattr); | |
14629 | if (err) | |
14630 | return err; | |
14631 | ||
fbd94c7a AS |
14632 | err = check_core_relo(env, attr, uattr); |
14633 | if (err) | |
14634 | return err; | |
14635 | ||
c454a46b | 14636 | return 0; |
ba64e7d8 YS |
14637 | } |
14638 | ||
f1174f77 EC |
14639 | /* check %cur's range satisfies %old's */ |
14640 | static bool range_within(struct bpf_reg_state *old, | |
14641 | struct bpf_reg_state *cur) | |
14642 | { | |
b03c9f9f EC |
14643 | return old->umin_value <= cur->umin_value && |
14644 | old->umax_value >= cur->umax_value && | |
14645 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
14646 | old->smax_value >= cur->smax_value && |
14647 | old->u32_min_value <= cur->u32_min_value && | |
14648 | old->u32_max_value >= cur->u32_max_value && | |
14649 | old->s32_min_value <= cur->s32_min_value && | |
14650 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
14651 | } |
14652 | ||
f1174f77 EC |
14653 | /* If in the old state two registers had the same id, then they need to have |
14654 | * the same id in the new state as well. But that id could be different from | |
14655 | * the old state, so we need to track the mapping from old to new ids. | |
14656 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
14657 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
14658 | * regs with a different old id could still have new id 9, we don't care about | |
14659 | * that. | |
14660 | * So we look through our idmap to see if this old id has been seen before. If | |
14661 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 14662 | */ |
c9e73e3d | 14663 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 14664 | { |
f1174f77 | 14665 | unsigned int i; |
969bf05e | 14666 | |
4633a006 AN |
14667 | /* either both IDs should be set or both should be zero */ |
14668 | if (!!old_id != !!cur_id) | |
14669 | return false; | |
14670 | ||
14671 | if (old_id == 0) /* cur_id == 0 as well */ | |
14672 | return true; | |
14673 | ||
c9e73e3d | 14674 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
14675 | if (!idmap[i].old) { |
14676 | /* Reached an empty slot; haven't seen this id before */ | |
14677 | idmap[i].old = old_id; | |
14678 | idmap[i].cur = cur_id; | |
14679 | return true; | |
14680 | } | |
14681 | if (idmap[i].old == old_id) | |
14682 | return idmap[i].cur == cur_id; | |
14683 | } | |
14684 | /* We ran out of idmap slots, which should be impossible */ | |
14685 | WARN_ON_ONCE(1); | |
14686 | return false; | |
14687 | } | |
14688 | ||
9242b5f5 AS |
14689 | static void clean_func_state(struct bpf_verifier_env *env, |
14690 | struct bpf_func_state *st) | |
14691 | { | |
14692 | enum bpf_reg_liveness live; | |
14693 | int i, j; | |
14694 | ||
14695 | for (i = 0; i < BPF_REG_FP; i++) { | |
14696 | live = st->regs[i].live; | |
14697 | /* liveness must not touch this register anymore */ | |
14698 | st->regs[i].live |= REG_LIVE_DONE; | |
14699 | if (!(live & REG_LIVE_READ)) | |
14700 | /* since the register is unused, clear its state | |
14701 | * to make further comparison simpler | |
14702 | */ | |
f54c7898 | 14703 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
14704 | } |
14705 | ||
14706 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
14707 | live = st->stack[i].spilled_ptr.live; | |
14708 | /* liveness must not touch this stack slot anymore */ | |
14709 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
14710 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 14711 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
14712 | for (j = 0; j < BPF_REG_SIZE; j++) |
14713 | st->stack[i].slot_type[j] = STACK_INVALID; | |
14714 | } | |
14715 | } | |
14716 | } | |
14717 | ||
14718 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
14719 | struct bpf_verifier_state *st) | |
14720 | { | |
14721 | int i; | |
14722 | ||
14723 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
14724 | /* all regs in this state in all frames were already marked */ | |
14725 | return; | |
14726 | ||
14727 | for (i = 0; i <= st->curframe; i++) | |
14728 | clean_func_state(env, st->frame[i]); | |
14729 | } | |
14730 | ||
14731 | /* the parentage chains form a tree. | |
14732 | * the verifier states are added to state lists at given insn and | |
14733 | * pushed into state stack for future exploration. | |
14734 | * when the verifier reaches bpf_exit insn some of the verifer states | |
14735 | * stored in the state lists have their final liveness state already, | |
14736 | * but a lot of states will get revised from liveness point of view when | |
14737 | * the verifier explores other branches. | |
14738 | * Example: | |
14739 | * 1: r0 = 1 | |
14740 | * 2: if r1 == 100 goto pc+1 | |
14741 | * 3: r0 = 2 | |
14742 | * 4: exit | |
14743 | * when the verifier reaches exit insn the register r0 in the state list of | |
14744 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
14745 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
14746 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
14747 | * | |
14748 | * Since the verifier pushes the branch states as it sees them while exploring | |
14749 | * the program the condition of walking the branch instruction for the second | |
14750 | * time means that all states below this branch were already explored and | |
8fb33b60 | 14751 | * their final liveness marks are already propagated. |
9242b5f5 AS |
14752 | * Hence when the verifier completes the search of state list in is_state_visited() |
14753 | * we can call this clean_live_states() function to mark all liveness states | |
14754 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
14755 | * will not be used. | |
14756 | * This function also clears the registers and stack for states that !READ | |
14757 | * to simplify state merging. | |
14758 | * | |
14759 | * Important note here that walking the same branch instruction in the callee | |
14760 | * doesn't meant that the states are DONE. The verifier has to compare | |
14761 | * the callsites | |
14762 | */ | |
14763 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
14764 | struct bpf_verifier_state *cur) | |
14765 | { | |
14766 | struct bpf_verifier_state_list *sl; | |
14767 | int i; | |
14768 | ||
5d839021 | 14769 | sl = *explored_state(env, insn); |
a8f500af | 14770 | while (sl) { |
2589726d AS |
14771 | if (sl->state.branches) |
14772 | goto next; | |
dc2a4ebc AS |
14773 | if (sl->state.insn_idx != insn || |
14774 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
14775 | goto next; |
14776 | for (i = 0; i <= cur->curframe; i++) | |
14777 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
14778 | goto next; | |
14779 | clean_verifier_state(env, &sl->state); | |
14780 | next: | |
14781 | sl = sl->next; | |
14782 | } | |
14783 | } | |
14784 | ||
4a95c85c | 14785 | static bool regs_exact(const struct bpf_reg_state *rold, |
4633a006 AN |
14786 | const struct bpf_reg_state *rcur, |
14787 | struct bpf_id_pair *idmap) | |
4a95c85c | 14788 | { |
d2dcc67d | 14789 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
4633a006 AN |
14790 | check_ids(rold->id, rcur->id, idmap) && |
14791 | check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap); | |
4a95c85c AN |
14792 | } |
14793 | ||
f1174f77 | 14794 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
14795 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
14796 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 14797 | { |
dc503a8a EC |
14798 | if (!(rold->live & REG_LIVE_READ)) |
14799 | /* explored state didn't use this */ | |
14800 | return true; | |
f1174f77 EC |
14801 | if (rold->type == NOT_INIT) |
14802 | /* explored state can't have used this */ | |
969bf05e | 14803 | return true; |
f1174f77 EC |
14804 | if (rcur->type == NOT_INIT) |
14805 | return false; | |
7f4ce97c | 14806 | |
910f6999 AN |
14807 | /* Enforce that register types have to match exactly, including their |
14808 | * modifiers (like PTR_MAYBE_NULL, MEM_RDONLY, etc), as a general | |
14809 | * rule. | |
14810 | * | |
14811 | * One can make a point that using a pointer register as unbounded | |
14812 | * SCALAR would be technically acceptable, but this could lead to | |
14813 | * pointer leaks because scalars are allowed to leak while pointers | |
14814 | * are not. We could make this safe in special cases if root is | |
14815 | * calling us, but it's probably not worth the hassle. | |
14816 | * | |
14817 | * Also, register types that are *not* MAYBE_NULL could technically be | |
14818 | * safe to use as their MAYBE_NULL variants (e.g., PTR_TO_MAP_VALUE | |
14819 | * is safe to be used as PTR_TO_MAP_VALUE_OR_NULL, provided both point | |
14820 | * to the same map). | |
7f4ce97c AN |
14821 | * However, if the old MAYBE_NULL register then got NULL checked, |
14822 | * doing so could have affected others with the same id, and we can't | |
14823 | * check for that because we lost the id when we converted to | |
14824 | * a non-MAYBE_NULL variant. | |
14825 | * So, as a general rule we don't allow mixing MAYBE_NULL and | |
910f6999 | 14826 | * non-MAYBE_NULL registers as well. |
7f4ce97c | 14827 | */ |
910f6999 | 14828 | if (rold->type != rcur->type) |
7f4ce97c AN |
14829 | return false; |
14830 | ||
c25b2ae1 | 14831 | switch (base_type(rold->type)) { |
f1174f77 | 14832 | case SCALAR_VALUE: |
4633a006 | 14833 | if (regs_exact(rold, rcur, idmap)) |
7c884339 | 14834 | return true; |
e042aa53 DB |
14835 | if (env->explore_alu_limits) |
14836 | return false; | |
910f6999 AN |
14837 | if (!rold->precise) |
14838 | return true; | |
14839 | /* new val must satisfy old val knowledge */ | |
14840 | return range_within(rold, rcur) && | |
14841 | tnum_in(rold->var_off, rcur->var_off); | |
69c087ba | 14842 | case PTR_TO_MAP_KEY: |
f1174f77 | 14843 | case PTR_TO_MAP_VALUE: |
567da5d2 AN |
14844 | case PTR_TO_MEM: |
14845 | case PTR_TO_BUF: | |
14846 | case PTR_TO_TP_BUFFER: | |
1b688a19 EC |
14847 | /* If the new min/max/var_off satisfy the old ones and |
14848 | * everything else matches, we are OK. | |
1b688a19 | 14849 | */ |
a73bf9f2 | 14850 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, var_off)) == 0 && |
1b688a19 | 14851 | range_within(rold, rcur) && |
4ea2bb15 | 14852 | tnum_in(rold->var_off, rcur->var_off) && |
567da5d2 AN |
14853 | check_ids(rold->id, rcur->id, idmap) && |
14854 | check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap); | |
de8f3a83 | 14855 | case PTR_TO_PACKET_META: |
f1174f77 | 14856 | case PTR_TO_PACKET: |
f1174f77 EC |
14857 | /* We must have at least as much range as the old ptr |
14858 | * did, so that any accesses which were safe before are | |
14859 | * still safe. This is true even if old range < old off, | |
14860 | * since someone could have accessed through (ptr - k), or | |
14861 | * even done ptr -= k in a register, to get a safe access. | |
14862 | */ | |
14863 | if (rold->range > rcur->range) | |
14864 | return false; | |
14865 | /* If the offsets don't match, we can't trust our alignment; | |
14866 | * nor can we be sure that we won't fall out of range. | |
14867 | */ | |
14868 | if (rold->off != rcur->off) | |
14869 | return false; | |
14870 | /* id relations must be preserved */ | |
4633a006 | 14871 | if (!check_ids(rold->id, rcur->id, idmap)) |
f1174f77 EC |
14872 | return false; |
14873 | /* new val must satisfy old val knowledge */ | |
14874 | return range_within(rold, rcur) && | |
14875 | tnum_in(rold->var_off, rcur->var_off); | |
7c884339 EZ |
14876 | case PTR_TO_STACK: |
14877 | /* two stack pointers are equal only if they're pointing to | |
14878 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
f1174f77 | 14879 | */ |
4633a006 | 14880 | return regs_exact(rold, rcur, idmap) && rold->frameno == rcur->frameno; |
f1174f77 | 14881 | default: |
4633a006 | 14882 | return regs_exact(rold, rcur, idmap); |
f1174f77 | 14883 | } |
969bf05e AS |
14884 | } |
14885 | ||
e042aa53 DB |
14886 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
14887 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
14888 | { |
14889 | int i, spi; | |
14890 | ||
638f5b90 AS |
14891 | /* walk slots of the explored stack and ignore any additional |
14892 | * slots in the current stack, since explored(safe) state | |
14893 | * didn't use them | |
14894 | */ | |
14895 | for (i = 0; i < old->allocated_stack; i++) { | |
06accc87 AN |
14896 | struct bpf_reg_state *old_reg, *cur_reg; |
14897 | ||
638f5b90 AS |
14898 | spi = i / BPF_REG_SIZE; |
14899 | ||
b233920c AS |
14900 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
14901 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 14902 | /* explored state didn't use this */ |
fd05e57b | 14903 | continue; |
b233920c | 14904 | } |
cc2b14d5 | 14905 | |
638f5b90 AS |
14906 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
14907 | continue; | |
19e2dbb7 | 14908 | |
6715df8d EZ |
14909 | if (env->allow_uninit_stack && |
14910 | old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC) | |
14911 | continue; | |
14912 | ||
19e2dbb7 AS |
14913 | /* explored stack has more populated slots than current stack |
14914 | * and these slots were used | |
14915 | */ | |
14916 | if (i >= cur->allocated_stack) | |
14917 | return false; | |
14918 | ||
cc2b14d5 AS |
14919 | /* if old state was safe with misc data in the stack |
14920 | * it will be safe with zero-initialized stack. | |
14921 | * The opposite is not true | |
14922 | */ | |
14923 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
14924 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
14925 | continue; | |
638f5b90 AS |
14926 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
14927 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
14928 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 14929 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
14930 | * this verifier states are not equivalent, |
14931 | * return false to continue verification of this path | |
14932 | */ | |
14933 | return false; | |
27113c59 | 14934 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 14935 | continue; |
d6fefa11 KKD |
14936 | /* Both old and cur are having same slot_type */ |
14937 | switch (old->stack[spi].slot_type[BPF_REG_SIZE - 1]) { | |
14938 | case STACK_SPILL: | |
638f5b90 AS |
14939 | /* when explored and current stack slot are both storing |
14940 | * spilled registers, check that stored pointers types | |
14941 | * are the same as well. | |
14942 | * Ex: explored safe path could have stored | |
14943 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
14944 | * but current path has stored: | |
14945 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
14946 | * such verifier states are not equivalent. | |
14947 | * return false to continue verification of this path | |
14948 | */ | |
d6fefa11 KKD |
14949 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
14950 | &cur->stack[spi].spilled_ptr, idmap)) | |
14951 | return false; | |
14952 | break; | |
14953 | case STACK_DYNPTR: | |
d6fefa11 KKD |
14954 | old_reg = &old->stack[spi].spilled_ptr; |
14955 | cur_reg = &cur->stack[spi].spilled_ptr; | |
14956 | if (old_reg->dynptr.type != cur_reg->dynptr.type || | |
14957 | old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot || | |
14958 | !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap)) | |
14959 | return false; | |
14960 | break; | |
06accc87 AN |
14961 | case STACK_ITER: |
14962 | old_reg = &old->stack[spi].spilled_ptr; | |
14963 | cur_reg = &cur->stack[spi].spilled_ptr; | |
14964 | /* iter.depth is not compared between states as it | |
14965 | * doesn't matter for correctness and would otherwise | |
14966 | * prevent convergence; we maintain it only to prevent | |
14967 | * infinite loop check triggering, see | |
14968 | * iter_active_depths_differ() | |
14969 | */ | |
14970 | if (old_reg->iter.btf != cur_reg->iter.btf || | |
14971 | old_reg->iter.btf_id != cur_reg->iter.btf_id || | |
14972 | old_reg->iter.state != cur_reg->iter.state || | |
14973 | /* ignore {old_reg,cur_reg}->iter.depth, see above */ | |
14974 | !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap)) | |
14975 | return false; | |
14976 | break; | |
d6fefa11 KKD |
14977 | case STACK_MISC: |
14978 | case STACK_ZERO: | |
14979 | case STACK_INVALID: | |
14980 | continue; | |
14981 | /* Ensure that new unhandled slot types return false by default */ | |
14982 | default: | |
638f5b90 | 14983 | return false; |
d6fefa11 | 14984 | } |
638f5b90 AS |
14985 | } |
14986 | return true; | |
14987 | } | |
14988 | ||
e8f55fcf AN |
14989 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur, |
14990 | struct bpf_id_pair *idmap) | |
fd978bf7 | 14991 | { |
e8f55fcf AN |
14992 | int i; |
14993 | ||
fd978bf7 JS |
14994 | if (old->acquired_refs != cur->acquired_refs) |
14995 | return false; | |
e8f55fcf AN |
14996 | |
14997 | for (i = 0; i < old->acquired_refs; i++) { | |
14998 | if (!check_ids(old->refs[i].id, cur->refs[i].id, idmap)) | |
14999 | return false; | |
15000 | } | |
15001 | ||
15002 | return true; | |
fd978bf7 JS |
15003 | } |
15004 | ||
f1bca824 AS |
15005 | /* compare two verifier states |
15006 | * | |
15007 | * all states stored in state_list are known to be valid, since | |
15008 | * verifier reached 'bpf_exit' instruction through them | |
15009 | * | |
15010 | * this function is called when verifier exploring different branches of | |
15011 | * execution popped from the state stack. If it sees an old state that has | |
15012 | * more strict register state and more strict stack state then this execution | |
15013 | * branch doesn't need to be explored further, since verifier already | |
15014 | * concluded that more strict state leads to valid finish. | |
15015 | * | |
15016 | * Therefore two states are equivalent if register state is more conservative | |
15017 | * and explored stack state is more conservative than the current one. | |
15018 | * Example: | |
15019 | * explored current | |
15020 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
15021 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
15022 | * | |
15023 | * In other words if current stack state (one being explored) has more | |
15024 | * valid slots than old one that already passed validation, it means | |
15025 | * the verifier can stop exploring and conclude that current state is valid too | |
15026 | * | |
15027 | * Similarly with registers. If explored state has register type as invalid | |
15028 | * whereas register type in current state is meaningful, it means that | |
15029 | * the current state will reach 'bpf_exit' instruction safely | |
15030 | */ | |
c9e73e3d | 15031 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 15032 | struct bpf_func_state *cur) |
f1bca824 AS |
15033 | { |
15034 | int i; | |
15035 | ||
c9e73e3d | 15036 | for (i = 0; i < MAX_BPF_REG; i++) |
e042aa53 DB |
15037 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
15038 | env->idmap_scratch)) | |
c9e73e3d | 15039 | return false; |
f1bca824 | 15040 | |
e042aa53 | 15041 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 15042 | return false; |
fd978bf7 | 15043 | |
e8f55fcf | 15044 | if (!refsafe(old, cur, env->idmap_scratch)) |
c9e73e3d LB |
15045 | return false; |
15046 | ||
15047 | return true; | |
f1bca824 AS |
15048 | } |
15049 | ||
f4d7e40a AS |
15050 | static bool states_equal(struct bpf_verifier_env *env, |
15051 | struct bpf_verifier_state *old, | |
15052 | struct bpf_verifier_state *cur) | |
15053 | { | |
15054 | int i; | |
15055 | ||
15056 | if (old->curframe != cur->curframe) | |
15057 | return false; | |
15058 | ||
5dd9cdbc EZ |
15059 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
15060 | ||
979d63d5 DB |
15061 | /* Verification state from speculative execution simulation |
15062 | * must never prune a non-speculative execution one. | |
15063 | */ | |
15064 | if (old->speculative && !cur->speculative) | |
15065 | return false; | |
15066 | ||
4ea2bb15 EZ |
15067 | if (old->active_lock.ptr != cur->active_lock.ptr) |
15068 | return false; | |
15069 | ||
15070 | /* Old and cur active_lock's have to be either both present | |
15071 | * or both absent. | |
15072 | */ | |
15073 | if (!!old->active_lock.id != !!cur->active_lock.id) | |
15074 | return false; | |
15075 | ||
15076 | if (old->active_lock.id && | |
15077 | !check_ids(old->active_lock.id, cur->active_lock.id, env->idmap_scratch)) | |
d83525ca AS |
15078 | return false; |
15079 | ||
9bb00b28 | 15080 | if (old->active_rcu_lock != cur->active_rcu_lock) |
d83525ca AS |
15081 | return false; |
15082 | ||
f4d7e40a AS |
15083 | /* for states to be equal callsites have to be the same |
15084 | * and all frame states need to be equivalent | |
15085 | */ | |
15086 | for (i = 0; i <= old->curframe; i++) { | |
15087 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
15088 | return false; | |
c9e73e3d | 15089 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
15090 | return false; |
15091 | } | |
15092 | return true; | |
15093 | } | |
15094 | ||
5327ed3d JW |
15095 | /* Return 0 if no propagation happened. Return negative error code if error |
15096 | * happened. Otherwise, return the propagated bit. | |
15097 | */ | |
55e7f3b5 JW |
15098 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
15099 | struct bpf_reg_state *reg, | |
15100 | struct bpf_reg_state *parent_reg) | |
15101 | { | |
5327ed3d JW |
15102 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
15103 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
15104 | int err; |
15105 | ||
5327ed3d JW |
15106 | /* When comes here, read flags of PARENT_REG or REG could be any of |
15107 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
15108 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
15109 | */ | |
15110 | if (parent_flag == REG_LIVE_READ64 || | |
15111 | /* Or if there is no read flag from REG. */ | |
15112 | !flag || | |
15113 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
15114 | parent_flag == flag) | |
55e7f3b5 JW |
15115 | return 0; |
15116 | ||
5327ed3d | 15117 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
15118 | if (err) |
15119 | return err; | |
15120 | ||
5327ed3d | 15121 | return flag; |
55e7f3b5 JW |
15122 | } |
15123 | ||
8e9cd9ce | 15124 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
15125 | * straight-line code between a state and its parent. When we arrive at an |
15126 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
15127 | * code, so read marks in the state must propagate to the parent regardless | |
15128 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 15129 | * in mark_reg_read() is for. |
8e9cd9ce | 15130 | */ |
f4d7e40a AS |
15131 | static int propagate_liveness(struct bpf_verifier_env *env, |
15132 | const struct bpf_verifier_state *vstate, | |
15133 | struct bpf_verifier_state *vparent) | |
dc503a8a | 15134 | { |
3f8cafa4 | 15135 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 15136 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 15137 | int i, frame, err = 0; |
dc503a8a | 15138 | |
f4d7e40a AS |
15139 | if (vparent->curframe != vstate->curframe) { |
15140 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
15141 | vparent->curframe, vstate->curframe); | |
15142 | return -EFAULT; | |
15143 | } | |
dc503a8a EC |
15144 | /* Propagate read liveness of registers... */ |
15145 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 15146 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
15147 | parent = vparent->frame[frame]; |
15148 | state = vstate->frame[frame]; | |
15149 | parent_reg = parent->regs; | |
15150 | state_reg = state->regs; | |
83d16312 JK |
15151 | /* We don't need to worry about FP liveness, it's read-only */ |
15152 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
15153 | err = propagate_liveness_reg(env, &state_reg[i], |
15154 | &parent_reg[i]); | |
5327ed3d | 15155 | if (err < 0) |
3f8cafa4 | 15156 | return err; |
5327ed3d JW |
15157 | if (err == REG_LIVE_READ64) |
15158 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 15159 | } |
f4d7e40a | 15160 | |
1b04aee7 | 15161 | /* Propagate stack slots. */ |
f4d7e40a AS |
15162 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
15163 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
15164 | parent_reg = &parent->stack[i].spilled_ptr; |
15165 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
15166 | err = propagate_liveness_reg(env, state_reg, |
15167 | parent_reg); | |
5327ed3d | 15168 | if (err < 0) |
3f8cafa4 | 15169 | return err; |
dc503a8a EC |
15170 | } |
15171 | } | |
5327ed3d | 15172 | return 0; |
dc503a8a EC |
15173 | } |
15174 | ||
a3ce685d AS |
15175 | /* find precise scalars in the previous equivalent state and |
15176 | * propagate them into the current state | |
15177 | */ | |
15178 | static int propagate_precision(struct bpf_verifier_env *env, | |
15179 | const struct bpf_verifier_state *old) | |
15180 | { | |
15181 | struct bpf_reg_state *state_reg; | |
15182 | struct bpf_func_state *state; | |
529409ea | 15183 | int i, err = 0, fr; |
a3ce685d | 15184 | |
529409ea AN |
15185 | for (fr = old->curframe; fr >= 0; fr--) { |
15186 | state = old->frame[fr]; | |
15187 | state_reg = state->regs; | |
15188 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
15189 | if (state_reg->type != SCALAR_VALUE || | |
52c2b005 AN |
15190 | !state_reg->precise || |
15191 | !(state_reg->live & REG_LIVE_READ)) | |
529409ea AN |
15192 | continue; |
15193 | if (env->log.level & BPF_LOG_LEVEL2) | |
34f0677e | 15194 | verbose(env, "frame %d: propagating r%d\n", fr, i); |
529409ea AN |
15195 | err = mark_chain_precision_frame(env, fr, i); |
15196 | if (err < 0) | |
15197 | return err; | |
15198 | } | |
a3ce685d | 15199 | |
529409ea AN |
15200 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
15201 | if (!is_spilled_reg(&state->stack[i])) | |
15202 | continue; | |
15203 | state_reg = &state->stack[i].spilled_ptr; | |
15204 | if (state_reg->type != SCALAR_VALUE || | |
52c2b005 AN |
15205 | !state_reg->precise || |
15206 | !(state_reg->live & REG_LIVE_READ)) | |
529409ea AN |
15207 | continue; |
15208 | if (env->log.level & BPF_LOG_LEVEL2) | |
15209 | verbose(env, "frame %d: propagating fp%d\n", | |
34f0677e | 15210 | fr, (-i - 1) * BPF_REG_SIZE); |
529409ea AN |
15211 | err = mark_chain_precision_stack_frame(env, fr, i); |
15212 | if (err < 0) | |
15213 | return err; | |
15214 | } | |
a3ce685d AS |
15215 | } |
15216 | return 0; | |
15217 | } | |
15218 | ||
2589726d AS |
15219 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
15220 | struct bpf_verifier_state *cur) | |
15221 | { | |
15222 | struct bpf_func_state *fold, *fcur; | |
15223 | int i, fr = cur->curframe; | |
15224 | ||
15225 | if (old->curframe != fr) | |
15226 | return false; | |
15227 | ||
15228 | fold = old->frame[fr]; | |
15229 | fcur = cur->frame[fr]; | |
15230 | for (i = 0; i < MAX_BPF_REG; i++) | |
15231 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
15232 | offsetof(struct bpf_reg_state, parent))) | |
15233 | return false; | |
15234 | return true; | |
15235 | } | |
15236 | ||
06accc87 AN |
15237 | static bool is_iter_next_insn(struct bpf_verifier_env *env, int insn_idx) |
15238 | { | |
15239 | return env->insn_aux_data[insn_idx].is_iter_next; | |
15240 | } | |
15241 | ||
15242 | /* is_state_visited() handles iter_next() (see process_iter_next_call() for | |
15243 | * terminology) calls specially: as opposed to bounded BPF loops, it *expects* | |
15244 | * states to match, which otherwise would look like an infinite loop. So while | |
15245 | * iter_next() calls are taken care of, we still need to be careful and | |
15246 | * prevent erroneous and too eager declaration of "ininite loop", when | |
15247 | * iterators are involved. | |
15248 | * | |
15249 | * Here's a situation in pseudo-BPF assembly form: | |
15250 | * | |
15251 | * 0: again: ; set up iter_next() call args | |
15252 | * 1: r1 = &it ; <CHECKPOINT HERE> | |
15253 | * 2: call bpf_iter_num_next ; this is iter_next() call | |
15254 | * 3: if r0 == 0 goto done | |
15255 | * 4: ... something useful here ... | |
15256 | * 5: goto again ; another iteration | |
15257 | * 6: done: | |
15258 | * 7: r1 = &it | |
15259 | * 8: call bpf_iter_num_destroy ; clean up iter state | |
15260 | * 9: exit | |
15261 | * | |
15262 | * This is a typical loop. Let's assume that we have a prune point at 1:, | |
15263 | * before we get to `call bpf_iter_num_next` (e.g., because of that `goto | |
15264 | * again`, assuming other heuristics don't get in a way). | |
15265 | * | |
15266 | * When we first time come to 1:, let's say we have some state X. We proceed | |
15267 | * to 2:, fork states, enqueue ACTIVE, validate NULL case successfully, exit. | |
15268 | * Now we come back to validate that forked ACTIVE state. We proceed through | |
15269 | * 3-5, come to goto, jump to 1:. Let's assume our state didn't change, so we | |
15270 | * are converging. But the problem is that we don't know that yet, as this | |
15271 | * convergence has to happen at iter_next() call site only. So if nothing is | |
15272 | * done, at 1: verifier will use bounded loop logic and declare infinite | |
15273 | * looping (and would be *technically* correct, if not for iterator's | |
15274 | * "eventual sticky NULL" contract, see process_iter_next_call()). But we | |
15275 | * don't want that. So what we do in process_iter_next_call() when we go on | |
15276 | * another ACTIVE iteration, we bump slot->iter.depth, to mark that it's | |
15277 | * a different iteration. So when we suspect an infinite loop, we additionally | |
15278 | * check if any of the *ACTIVE* iterator states depths differ. If yes, we | |
15279 | * pretend we are not looping and wait for next iter_next() call. | |
15280 | * | |
15281 | * This only applies to ACTIVE state. In DRAINED state we don't expect to | |
15282 | * loop, because that would actually mean infinite loop, as DRAINED state is | |
15283 | * "sticky", and so we'll keep returning into the same instruction with the | |
15284 | * same state (at least in one of possible code paths). | |
15285 | * | |
15286 | * This approach allows to keep infinite loop heuristic even in the face of | |
15287 | * active iterator. E.g., C snippet below is and will be detected as | |
15288 | * inifintely looping: | |
15289 | * | |
15290 | * struct bpf_iter_num it; | |
15291 | * int *p, x; | |
15292 | * | |
15293 | * bpf_iter_num_new(&it, 0, 10); | |
15294 | * while ((p = bpf_iter_num_next(&t))) { | |
15295 | * x = p; | |
15296 | * while (x--) {} // <<-- infinite loop here | |
15297 | * } | |
15298 | * | |
15299 | */ | |
15300 | static bool iter_active_depths_differ(struct bpf_verifier_state *old, struct bpf_verifier_state *cur) | |
15301 | { | |
15302 | struct bpf_reg_state *slot, *cur_slot; | |
15303 | struct bpf_func_state *state; | |
15304 | int i, fr; | |
15305 | ||
15306 | for (fr = old->curframe; fr >= 0; fr--) { | |
15307 | state = old->frame[fr]; | |
15308 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
15309 | if (state->stack[i].slot_type[0] != STACK_ITER) | |
15310 | continue; | |
15311 | ||
15312 | slot = &state->stack[i].spilled_ptr; | |
15313 | if (slot->iter.state != BPF_ITER_STATE_ACTIVE) | |
15314 | continue; | |
15315 | ||
15316 | cur_slot = &cur->frame[fr]->stack[i].spilled_ptr; | |
15317 | if (cur_slot->iter.depth != slot->iter.depth) | |
15318 | return true; | |
15319 | } | |
15320 | } | |
15321 | return false; | |
15322 | } | |
2589726d | 15323 | |
58e2af8b | 15324 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 15325 | { |
58e2af8b | 15326 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 15327 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 15328 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 15329 | int i, j, err, states_cnt = 0; |
4b5ce570 AN |
15330 | bool force_new_state = env->test_state_freq || is_force_checkpoint(env, insn_idx); |
15331 | bool add_new_state = force_new_state; | |
f1bca824 | 15332 | |
2589726d AS |
15333 | /* bpf progs typically have pruning point every 4 instructions |
15334 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
15335 | * Do not add new state for future pruning if the verifier hasn't seen | |
15336 | * at least 2 jumps and at least 8 instructions. | |
15337 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
15338 | * In tests that amounts to up to 50% reduction into total verifier | |
15339 | * memory consumption and 20% verifier time speedup. | |
15340 | */ | |
15341 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
15342 | env->insn_processed - env->prev_insn_processed >= 8) | |
15343 | add_new_state = true; | |
15344 | ||
a8f500af AS |
15345 | pprev = explored_state(env, insn_idx); |
15346 | sl = *pprev; | |
15347 | ||
9242b5f5 AS |
15348 | clean_live_states(env, insn_idx, cur); |
15349 | ||
a8f500af | 15350 | while (sl) { |
dc2a4ebc AS |
15351 | states_cnt++; |
15352 | if (sl->state.insn_idx != insn_idx) | |
15353 | goto next; | |
bfc6bb74 | 15354 | |
2589726d | 15355 | if (sl->state.branches) { |
bfc6bb74 AS |
15356 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
15357 | ||
15358 | if (frame->in_async_callback_fn && | |
15359 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
15360 | /* Different async_entry_cnt means that the verifier is | |
15361 | * processing another entry into async callback. | |
15362 | * Seeing the same state is not an indication of infinite | |
15363 | * loop or infinite recursion. | |
15364 | * But finding the same state doesn't mean that it's safe | |
15365 | * to stop processing the current state. The previous state | |
15366 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
15367 | * Checking in_async_callback_fn alone is not enough either. | |
15368 | * Since the verifier still needs to catch infinite loops | |
15369 | * inside async callbacks. | |
15370 | */ | |
06accc87 AN |
15371 | goto skip_inf_loop_check; |
15372 | } | |
15373 | /* BPF open-coded iterators loop detection is special. | |
15374 | * states_maybe_looping() logic is too simplistic in detecting | |
15375 | * states that *might* be equivalent, because it doesn't know | |
15376 | * about ID remapping, so don't even perform it. | |
15377 | * See process_iter_next_call() and iter_active_depths_differ() | |
15378 | * for overview of the logic. When current and one of parent | |
15379 | * states are detected as equivalent, it's a good thing: we prove | |
15380 | * convergence and can stop simulating further iterations. | |
15381 | * It's safe to assume that iterator loop will finish, taking into | |
15382 | * account iter_next() contract of eventually returning | |
15383 | * sticky NULL result. | |
15384 | */ | |
15385 | if (is_iter_next_insn(env, insn_idx)) { | |
15386 | if (states_equal(env, &sl->state, cur)) { | |
15387 | struct bpf_func_state *cur_frame; | |
15388 | struct bpf_reg_state *iter_state, *iter_reg; | |
15389 | int spi; | |
15390 | ||
15391 | cur_frame = cur->frame[cur->curframe]; | |
15392 | /* btf_check_iter_kfuncs() enforces that | |
15393 | * iter state pointer is always the first arg | |
15394 | */ | |
15395 | iter_reg = &cur_frame->regs[BPF_REG_1]; | |
15396 | /* current state is valid due to states_equal(), | |
15397 | * so we can assume valid iter and reg state, | |
15398 | * no need for extra (re-)validations | |
15399 | */ | |
15400 | spi = __get_spi(iter_reg->off + iter_reg->var_off.value); | |
15401 | iter_state = &func(env, iter_reg)->stack[spi].spilled_ptr; | |
15402 | if (iter_state->iter.state == BPF_ITER_STATE_ACTIVE) | |
15403 | goto hit; | |
15404 | } | |
15405 | goto skip_inf_loop_check; | |
15406 | } | |
15407 | /* attempt to detect infinite loop to avoid unnecessary doomed work */ | |
15408 | if (states_maybe_looping(&sl->state, cur) && | |
15409 | states_equal(env, &sl->state, cur) && | |
15410 | !iter_active_depths_differ(&sl->state, cur)) { | |
2589726d AS |
15411 | verbose_linfo(env, insn_idx, "; "); |
15412 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
15413 | return -EINVAL; | |
15414 | } | |
15415 | /* if the verifier is processing a loop, avoid adding new state | |
15416 | * too often, since different loop iterations have distinct | |
15417 | * states and may not help future pruning. | |
15418 | * This threshold shouldn't be too low to make sure that | |
15419 | * a loop with large bound will be rejected quickly. | |
15420 | * The most abusive loop will be: | |
15421 | * r1 += 1 | |
15422 | * if r1 < 1000000 goto pc-2 | |
15423 | * 1M insn_procssed limit / 100 == 10k peak states. | |
15424 | * This threshold shouldn't be too high either, since states | |
15425 | * at the end of the loop are likely to be useful in pruning. | |
15426 | */ | |
06accc87 | 15427 | skip_inf_loop_check: |
4b5ce570 | 15428 | if (!force_new_state && |
98ddcf38 | 15429 | env->jmps_processed - env->prev_jmps_processed < 20 && |
2589726d AS |
15430 | env->insn_processed - env->prev_insn_processed < 100) |
15431 | add_new_state = false; | |
15432 | goto miss; | |
15433 | } | |
638f5b90 | 15434 | if (states_equal(env, &sl->state, cur)) { |
06accc87 | 15435 | hit: |
9f4686c4 | 15436 | sl->hit_cnt++; |
f1bca824 | 15437 | /* reached equivalent register/stack state, |
dc503a8a EC |
15438 | * prune the search. |
15439 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
15440 | * If we have any write marks in env->cur_state, they |
15441 | * will prevent corresponding reads in the continuation | |
15442 | * from reaching our parent (an explored_state). Our | |
15443 | * own state will get the read marks recorded, but | |
15444 | * they'll be immediately forgotten as we're pruning | |
15445 | * this state and will pop a new one. | |
f1bca824 | 15446 | */ |
f4d7e40a | 15447 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
15448 | |
15449 | /* if previous state reached the exit with precision and | |
15450 | * current state is equivalent to it (except precsion marks) | |
15451 | * the precision needs to be propagated back in | |
15452 | * the current state. | |
15453 | */ | |
15454 | err = err ? : push_jmp_history(env, cur); | |
15455 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
15456 | if (err) |
15457 | return err; | |
f1bca824 | 15458 | return 1; |
dc503a8a | 15459 | } |
2589726d AS |
15460 | miss: |
15461 | /* when new state is not going to be added do not increase miss count. | |
15462 | * Otherwise several loop iterations will remove the state | |
15463 | * recorded earlier. The goal of these heuristics is to have | |
15464 | * states from some iterations of the loop (some in the beginning | |
15465 | * and some at the end) to help pruning. | |
15466 | */ | |
15467 | if (add_new_state) | |
15468 | sl->miss_cnt++; | |
9f4686c4 AS |
15469 | /* heuristic to determine whether this state is beneficial |
15470 | * to keep checking from state equivalence point of view. | |
15471 | * Higher numbers increase max_states_per_insn and verification time, | |
15472 | * but do not meaningfully decrease insn_processed. | |
15473 | */ | |
15474 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
15475 | /* the state is unlikely to be useful. Remove it to | |
15476 | * speed up verification | |
15477 | */ | |
15478 | *pprev = sl->next; | |
15479 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
15480 | u32 br = sl->state.branches; |
15481 | ||
15482 | WARN_ONCE(br, | |
15483 | "BUG live_done but branches_to_explore %d\n", | |
15484 | br); | |
9f4686c4 AS |
15485 | free_verifier_state(&sl->state, false); |
15486 | kfree(sl); | |
15487 | env->peak_states--; | |
15488 | } else { | |
15489 | /* cannot free this state, since parentage chain may | |
15490 | * walk it later. Add it for free_list instead to | |
15491 | * be freed at the end of verification | |
15492 | */ | |
15493 | sl->next = env->free_list; | |
15494 | env->free_list = sl; | |
15495 | } | |
15496 | sl = *pprev; | |
15497 | continue; | |
15498 | } | |
dc2a4ebc | 15499 | next: |
9f4686c4 AS |
15500 | pprev = &sl->next; |
15501 | sl = *pprev; | |
f1bca824 AS |
15502 | } |
15503 | ||
06ee7115 AS |
15504 | if (env->max_states_per_insn < states_cnt) |
15505 | env->max_states_per_insn = states_cnt; | |
15506 | ||
2c78ee89 | 15507 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
a095f421 | 15508 | return 0; |
ceefbc96 | 15509 | |
2589726d | 15510 | if (!add_new_state) |
a095f421 | 15511 | return 0; |
ceefbc96 | 15512 | |
2589726d AS |
15513 | /* There were no equivalent states, remember the current one. |
15514 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 15515 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 15516 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 15517 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
15518 | * again on the way to bpf_exit. |
15519 | * When looping the sl->state.branches will be > 0 and this state | |
15520 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 15521 | */ |
638f5b90 | 15522 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
15523 | if (!new_sl) |
15524 | return -ENOMEM; | |
06ee7115 AS |
15525 | env->total_states++; |
15526 | env->peak_states++; | |
2589726d AS |
15527 | env->prev_jmps_processed = env->jmps_processed; |
15528 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 | 15529 | |
7a830b53 AN |
15530 | /* forget precise markings we inherited, see __mark_chain_precision */ |
15531 | if (env->bpf_capable) | |
15532 | mark_all_scalars_imprecise(env, cur); | |
15533 | ||
f1bca824 | 15534 | /* add new state to the head of linked list */ |
679c782d EC |
15535 | new = &new_sl->state; |
15536 | err = copy_verifier_state(new, cur); | |
1969db47 | 15537 | if (err) { |
679c782d | 15538 | free_verifier_state(new, false); |
1969db47 AS |
15539 | kfree(new_sl); |
15540 | return err; | |
15541 | } | |
dc2a4ebc | 15542 | new->insn_idx = insn_idx; |
2589726d AS |
15543 | WARN_ONCE(new->branches != 1, |
15544 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 15545 | |
2589726d | 15546 | cur->parent = new; |
b5dc0163 AS |
15547 | cur->first_insn_idx = insn_idx; |
15548 | clear_jmp_history(cur); | |
5d839021 AS |
15549 | new_sl->next = *explored_state(env, insn_idx); |
15550 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
15551 | /* connect new state to parentage chain. Current frame needs all |
15552 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
15553 | * to the stack implicitly by JITs) so in callers' frames connect just | |
15554 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
15555 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
15556 | * from callee with its full parentage chain, anyway. | |
15557 | */ | |
8e9cd9ce EC |
15558 | /* clear write marks in current state: the writes we did are not writes |
15559 | * our child did, so they don't screen off its reads from us. | |
15560 | * (There are no read marks in current state, because reads always mark | |
15561 | * their parent and current state never has children yet. Only | |
15562 | * explored_states can get read marks.) | |
15563 | */ | |
eea1c227 AS |
15564 | for (j = 0; j <= cur->curframe; j++) { |
15565 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
15566 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
15567 | for (i = 0; i < BPF_REG_FP; i++) | |
15568 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
15569 | } | |
f4d7e40a AS |
15570 | |
15571 | /* all stack frames are accessible from callee, clear them all */ | |
15572 | for (j = 0; j <= cur->curframe; j++) { | |
15573 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 15574 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 15575 | |
679c782d | 15576 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 15577 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
15578 | frame->stack[i].spilled_ptr.parent = |
15579 | &newframe->stack[i].spilled_ptr; | |
15580 | } | |
f4d7e40a | 15581 | } |
f1bca824 AS |
15582 | return 0; |
15583 | } | |
15584 | ||
c64b7983 JS |
15585 | /* Return true if it's OK to have the same insn return a different type. */ |
15586 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
15587 | { | |
c25b2ae1 | 15588 | switch (base_type(type)) { |
c64b7983 JS |
15589 | case PTR_TO_CTX: |
15590 | case PTR_TO_SOCKET: | |
46f8bc92 | 15591 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 15592 | case PTR_TO_TCP_SOCK: |
fada7fdc | 15593 | case PTR_TO_XDP_SOCK: |
2a02759e | 15594 | case PTR_TO_BTF_ID: |
c64b7983 JS |
15595 | return false; |
15596 | default: | |
15597 | return true; | |
15598 | } | |
15599 | } | |
15600 | ||
15601 | /* If an instruction was previously used with particular pointer types, then we | |
15602 | * need to be careful to avoid cases such as the below, where it may be ok | |
15603 | * for one branch accessing the pointer, but not ok for the other branch: | |
15604 | * | |
15605 | * R1 = sock_ptr | |
15606 | * goto X; | |
15607 | * ... | |
15608 | * R1 = some_other_valid_ptr; | |
15609 | * goto X; | |
15610 | * ... | |
15611 | * R2 = *(u32 *)(R1 + 0); | |
15612 | */ | |
15613 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
15614 | { | |
15615 | return src != prev && (!reg_type_mismatch_ok(src) || | |
15616 | !reg_type_mismatch_ok(prev)); | |
15617 | } | |
15618 | ||
0d80a619 EZ |
15619 | static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type, |
15620 | bool allow_trust_missmatch) | |
15621 | { | |
15622 | enum bpf_reg_type *prev_type = &env->insn_aux_data[env->insn_idx].ptr_type; | |
15623 | ||
15624 | if (*prev_type == NOT_INIT) { | |
15625 | /* Saw a valid insn | |
15626 | * dst_reg = *(u32 *)(src_reg + off) | |
15627 | * save type to validate intersecting paths | |
15628 | */ | |
15629 | *prev_type = type; | |
15630 | } else if (reg_type_mismatch(type, *prev_type)) { | |
15631 | /* Abuser program is trying to use the same insn | |
15632 | * dst_reg = *(u32*) (src_reg + off) | |
15633 | * with different pointer types: | |
15634 | * src_reg == ctx in one branch and | |
15635 | * src_reg == stack|map in some other branch. | |
15636 | * Reject it. | |
15637 | */ | |
15638 | if (allow_trust_missmatch && | |
15639 | base_type(type) == PTR_TO_BTF_ID && | |
15640 | base_type(*prev_type) == PTR_TO_BTF_ID) { | |
15641 | /* | |
15642 | * Have to support a use case when one path through | |
15643 | * the program yields TRUSTED pointer while another | |
15644 | * is UNTRUSTED. Fallback to UNTRUSTED to generate | |
15645 | * BPF_PROBE_MEM. | |
15646 | */ | |
15647 | *prev_type = PTR_TO_BTF_ID | PTR_UNTRUSTED; | |
15648 | } else { | |
15649 | verbose(env, "same insn cannot be used with different pointers\n"); | |
15650 | return -EINVAL; | |
15651 | } | |
15652 | } | |
15653 | ||
15654 | return 0; | |
15655 | } | |
15656 | ||
58e2af8b | 15657 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 15658 | { |
6f8a57cc | 15659 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 15660 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 15661 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 15662 | struct bpf_reg_state *regs; |
06ee7115 | 15663 | int insn_cnt = env->prog->len; |
17a52670 | 15664 | bool do_print_state = false; |
b5dc0163 | 15665 | int prev_insn_idx = -1; |
17a52670 | 15666 | |
17a52670 AS |
15667 | for (;;) { |
15668 | struct bpf_insn *insn; | |
15669 | u8 class; | |
15670 | int err; | |
15671 | ||
b5dc0163 | 15672 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 15673 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 15674 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 15675 | env->insn_idx, insn_cnt); |
17a52670 AS |
15676 | return -EFAULT; |
15677 | } | |
15678 | ||
c08435ec | 15679 | insn = &insns[env->insn_idx]; |
17a52670 AS |
15680 | class = BPF_CLASS(insn->code); |
15681 | ||
06ee7115 | 15682 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
15683 | verbose(env, |
15684 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 15685 | env->insn_processed); |
17a52670 AS |
15686 | return -E2BIG; |
15687 | } | |
15688 | ||
a095f421 AN |
15689 | state->last_insn_idx = env->prev_insn_idx; |
15690 | ||
15691 | if (is_prune_point(env, env->insn_idx)) { | |
15692 | err = is_state_visited(env, env->insn_idx); | |
15693 | if (err < 0) | |
15694 | return err; | |
15695 | if (err == 1) { | |
15696 | /* found equivalent state, can prune the search */ | |
15697 | if (env->log.level & BPF_LOG_LEVEL) { | |
15698 | if (do_print_state) | |
15699 | verbose(env, "\nfrom %d to %d%s: safe\n", | |
15700 | env->prev_insn_idx, env->insn_idx, | |
15701 | env->cur_state->speculative ? | |
15702 | " (speculative execution)" : ""); | |
15703 | else | |
15704 | verbose(env, "%d: safe\n", env->insn_idx); | |
15705 | } | |
15706 | goto process_bpf_exit; | |
f1bca824 | 15707 | } |
a095f421 AN |
15708 | } |
15709 | ||
15710 | if (is_jmp_point(env, env->insn_idx)) { | |
15711 | err = push_jmp_history(env, state); | |
15712 | if (err) | |
15713 | return err; | |
f1bca824 AS |
15714 | } |
15715 | ||
c3494801 AS |
15716 | if (signal_pending(current)) |
15717 | return -EAGAIN; | |
15718 | ||
3c2ce60b DB |
15719 | if (need_resched()) |
15720 | cond_resched(); | |
15721 | ||
2e576648 CL |
15722 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
15723 | verbose(env, "\nfrom %d to %d%s:", | |
15724 | env->prev_insn_idx, env->insn_idx, | |
15725 | env->cur_state->speculative ? | |
15726 | " (speculative execution)" : ""); | |
15727 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
15728 | do_print_state = false; |
15729 | } | |
15730 | ||
06ee7115 | 15731 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 15732 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 15733 | .cb_call = disasm_kfunc_name, |
7105e828 | 15734 | .cb_print = verbose, |
abe08840 | 15735 | .private_data = env, |
7105e828 DB |
15736 | }; |
15737 | ||
2e576648 CL |
15738 | if (verifier_state_scratched(env)) |
15739 | print_insn_state(env, state->frame[state->curframe]); | |
15740 | ||
c08435ec | 15741 | verbose_linfo(env, env->insn_idx, "; "); |
12166409 | 15742 | env->prev_log_pos = env->log.end_pos; |
c08435ec | 15743 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 15744 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
12166409 AN |
15745 | env->prev_insn_print_pos = env->log.end_pos - env->prev_log_pos; |
15746 | env->prev_log_pos = env->log.end_pos; | |
17a52670 AS |
15747 | } |
15748 | ||
9d03ebc7 | 15749 | if (bpf_prog_is_offloaded(env->prog->aux)) { |
c08435ec DB |
15750 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
15751 | env->prev_insn_idx); | |
cae1927c JK |
15752 | if (err) |
15753 | return err; | |
15754 | } | |
13a27dfc | 15755 | |
638f5b90 | 15756 | regs = cur_regs(env); |
fe9a5ca7 | 15757 | sanitize_mark_insn_seen(env); |
b5dc0163 | 15758 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 15759 | |
17a52670 | 15760 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 15761 | err = check_alu_op(env, insn); |
17a52670 AS |
15762 | if (err) |
15763 | return err; | |
15764 | ||
15765 | } else if (class == BPF_LDX) { | |
0d80a619 | 15766 | enum bpf_reg_type src_reg_type; |
9bac3d6d AS |
15767 | |
15768 | /* check for reserved fields is already done */ | |
15769 | ||
17a52670 | 15770 | /* check src operand */ |
dc503a8a | 15771 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
15772 | if (err) |
15773 | return err; | |
15774 | ||
dc503a8a | 15775 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
15776 | if (err) |
15777 | return err; | |
15778 | ||
725f9dcd AS |
15779 | src_reg_type = regs[insn->src_reg].type; |
15780 | ||
17a52670 AS |
15781 | /* check that memory (src_reg + off) is readable, |
15782 | * the state of dst_reg will be updated by this func | |
15783 | */ | |
c08435ec DB |
15784 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
15785 | insn->off, BPF_SIZE(insn->code), | |
15786 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
15787 | if (err) |
15788 | return err; | |
15789 | ||
0d80a619 EZ |
15790 | err = save_aux_ptr_type(env, src_reg_type, true); |
15791 | if (err) | |
15792 | return err; | |
17a52670 | 15793 | } else if (class == BPF_STX) { |
0d80a619 | 15794 | enum bpf_reg_type dst_reg_type; |
d691f9e8 | 15795 | |
91c960b0 BJ |
15796 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
15797 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
15798 | if (err) |
15799 | return err; | |
c08435ec | 15800 | env->insn_idx++; |
17a52670 AS |
15801 | continue; |
15802 | } | |
15803 | ||
5ca419f2 BJ |
15804 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
15805 | verbose(env, "BPF_STX uses reserved fields\n"); | |
15806 | return -EINVAL; | |
15807 | } | |
15808 | ||
17a52670 | 15809 | /* check src1 operand */ |
dc503a8a | 15810 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
15811 | if (err) |
15812 | return err; | |
15813 | /* check src2 operand */ | |
dc503a8a | 15814 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
15815 | if (err) |
15816 | return err; | |
15817 | ||
d691f9e8 AS |
15818 | dst_reg_type = regs[insn->dst_reg].type; |
15819 | ||
17a52670 | 15820 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
15821 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
15822 | insn->off, BPF_SIZE(insn->code), | |
15823 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
15824 | if (err) |
15825 | return err; | |
15826 | ||
0d80a619 EZ |
15827 | err = save_aux_ptr_type(env, dst_reg_type, false); |
15828 | if (err) | |
15829 | return err; | |
17a52670 | 15830 | } else if (class == BPF_ST) { |
0d80a619 EZ |
15831 | enum bpf_reg_type dst_reg_type; |
15832 | ||
17a52670 AS |
15833 | if (BPF_MODE(insn->code) != BPF_MEM || |
15834 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 15835 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
15836 | return -EINVAL; |
15837 | } | |
15838 | /* check src operand */ | |
dc503a8a | 15839 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
15840 | if (err) |
15841 | return err; | |
15842 | ||
0d80a619 | 15843 | dst_reg_type = regs[insn->dst_reg].type; |
f37a8cb8 | 15844 | |
17a52670 | 15845 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
15846 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
15847 | insn->off, BPF_SIZE(insn->code), | |
15848 | BPF_WRITE, -1, false); | |
17a52670 AS |
15849 | if (err) |
15850 | return err; | |
15851 | ||
0d80a619 EZ |
15852 | err = save_aux_ptr_type(env, dst_reg_type, false); |
15853 | if (err) | |
15854 | return err; | |
092ed096 | 15855 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
15856 | u8 opcode = BPF_OP(insn->code); |
15857 | ||
2589726d | 15858 | env->jmps_processed++; |
17a52670 AS |
15859 | if (opcode == BPF_CALL) { |
15860 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
15861 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
15862 | && insn->off != 0) || | |
f4d7e40a | 15863 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
15864 | insn->src_reg != BPF_PSEUDO_CALL && |
15865 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
15866 | insn->dst_reg != BPF_REG_0 || |
15867 | class == BPF_JMP32) { | |
61bd5218 | 15868 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
15869 | return -EINVAL; |
15870 | } | |
15871 | ||
8cab76ec KKD |
15872 | if (env->cur_state->active_lock.ptr) { |
15873 | if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) || | |
15874 | (insn->src_reg == BPF_PSEUDO_CALL) || | |
15875 | (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && | |
cd6791b4 | 15876 | (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) { |
8cab76ec KKD |
15877 | verbose(env, "function calls are not allowed while holding a lock\n"); |
15878 | return -EINVAL; | |
15879 | } | |
d83525ca | 15880 | } |
f4d7e40a | 15881 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 15882 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 | 15883 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
5c073f26 | 15884 | err = check_kfunc_call(env, insn, &env->insn_idx); |
f4d7e40a | 15885 | else |
69c087ba | 15886 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
15887 | if (err) |
15888 | return err; | |
553a64a8 AN |
15889 | |
15890 | mark_reg_scratched(env, BPF_REG_0); | |
17a52670 AS |
15891 | } else if (opcode == BPF_JA) { |
15892 | if (BPF_SRC(insn->code) != BPF_K || | |
15893 | insn->imm != 0 || | |
15894 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
15895 | insn->dst_reg != BPF_REG_0 || |
15896 | class == BPF_JMP32) { | |
61bd5218 | 15897 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
15898 | return -EINVAL; |
15899 | } | |
15900 | ||
c08435ec | 15901 | env->insn_idx += insn->off + 1; |
17a52670 AS |
15902 | continue; |
15903 | ||
15904 | } else if (opcode == BPF_EXIT) { | |
15905 | if (BPF_SRC(insn->code) != BPF_K || | |
15906 | insn->imm != 0 || | |
15907 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
15908 | insn->dst_reg != BPF_REG_0 || |
15909 | class == BPF_JMP32) { | |
61bd5218 | 15910 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
15911 | return -EINVAL; |
15912 | } | |
15913 | ||
5d92ddc3 DM |
15914 | if (env->cur_state->active_lock.ptr && |
15915 | !in_rbtree_lock_required_cb(env)) { | |
d83525ca AS |
15916 | verbose(env, "bpf_spin_unlock is missing\n"); |
15917 | return -EINVAL; | |
15918 | } | |
15919 | ||
9bb00b28 YS |
15920 | if (env->cur_state->active_rcu_lock) { |
15921 | verbose(env, "bpf_rcu_read_unlock is missing\n"); | |
15922 | return -EINVAL; | |
15923 | } | |
15924 | ||
9d9d00ac KKD |
15925 | /* We must do check_reference_leak here before |
15926 | * prepare_func_exit to handle the case when | |
15927 | * state->curframe > 0, it may be a callback | |
15928 | * function, for which reference_state must | |
15929 | * match caller reference state when it exits. | |
15930 | */ | |
15931 | err = check_reference_leak(env); | |
15932 | if (err) | |
15933 | return err; | |
15934 | ||
f4d7e40a AS |
15935 | if (state->curframe) { |
15936 | /* exit from nested function */ | |
c08435ec | 15937 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
15938 | if (err) |
15939 | return err; | |
15940 | do_print_state = true; | |
15941 | continue; | |
15942 | } | |
15943 | ||
390ee7e2 AS |
15944 | err = check_return_code(env); |
15945 | if (err) | |
15946 | return err; | |
f1bca824 | 15947 | process_bpf_exit: |
0f55f9ed | 15948 | mark_verifier_state_scratched(env); |
2589726d | 15949 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 15950 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 15951 | &env->insn_idx, pop_log); |
638f5b90 AS |
15952 | if (err < 0) { |
15953 | if (err != -ENOENT) | |
15954 | return err; | |
17a52670 AS |
15955 | break; |
15956 | } else { | |
15957 | do_print_state = true; | |
15958 | continue; | |
15959 | } | |
15960 | } else { | |
c08435ec | 15961 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
15962 | if (err) |
15963 | return err; | |
15964 | } | |
15965 | } else if (class == BPF_LD) { | |
15966 | u8 mode = BPF_MODE(insn->code); | |
15967 | ||
15968 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
15969 | err = check_ld_abs(env, insn); |
15970 | if (err) | |
15971 | return err; | |
15972 | ||
17a52670 AS |
15973 | } else if (mode == BPF_IMM) { |
15974 | err = check_ld_imm(env, insn); | |
15975 | if (err) | |
15976 | return err; | |
15977 | ||
c08435ec | 15978 | env->insn_idx++; |
fe9a5ca7 | 15979 | sanitize_mark_insn_seen(env); |
17a52670 | 15980 | } else { |
61bd5218 | 15981 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
15982 | return -EINVAL; |
15983 | } | |
15984 | } else { | |
61bd5218 | 15985 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
15986 | return -EINVAL; |
15987 | } | |
15988 | ||
c08435ec | 15989 | env->insn_idx++; |
17a52670 AS |
15990 | } |
15991 | ||
15992 | return 0; | |
15993 | } | |
15994 | ||
541c3bad AN |
15995 | static int find_btf_percpu_datasec(struct btf *btf) |
15996 | { | |
15997 | const struct btf_type *t; | |
15998 | const char *tname; | |
15999 | int i, n; | |
16000 | ||
16001 | /* | |
16002 | * Both vmlinux and module each have their own ".data..percpu" | |
16003 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
16004 | * types to look at only module's own BTF types. | |
16005 | */ | |
16006 | n = btf_nr_types(btf); | |
16007 | if (btf_is_module(btf)) | |
16008 | i = btf_nr_types(btf_vmlinux); | |
16009 | else | |
16010 | i = 1; | |
16011 | ||
16012 | for(; i < n; i++) { | |
16013 | t = btf_type_by_id(btf, i); | |
16014 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
16015 | continue; | |
16016 | ||
16017 | tname = btf_name_by_offset(btf, t->name_off); | |
16018 | if (!strcmp(tname, ".data..percpu")) | |
16019 | return i; | |
16020 | } | |
16021 | ||
16022 | return -ENOENT; | |
16023 | } | |
16024 | ||
4976b718 HL |
16025 | /* replace pseudo btf_id with kernel symbol address */ |
16026 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
16027 | struct bpf_insn *insn, | |
16028 | struct bpf_insn_aux_data *aux) | |
16029 | { | |
eaa6bcb7 HL |
16030 | const struct btf_var_secinfo *vsi; |
16031 | const struct btf_type *datasec; | |
541c3bad | 16032 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
16033 | const struct btf_type *t; |
16034 | const char *sym_name; | |
eaa6bcb7 | 16035 | bool percpu = false; |
f16e6313 | 16036 | u32 type, id = insn->imm; |
541c3bad | 16037 | struct btf *btf; |
f16e6313 | 16038 | s32 datasec_id; |
4976b718 | 16039 | u64 addr; |
541c3bad | 16040 | int i, btf_fd, err; |
4976b718 | 16041 | |
541c3bad AN |
16042 | btf_fd = insn[1].imm; |
16043 | if (btf_fd) { | |
16044 | btf = btf_get_by_fd(btf_fd); | |
16045 | if (IS_ERR(btf)) { | |
16046 | verbose(env, "invalid module BTF object FD specified.\n"); | |
16047 | return -EINVAL; | |
16048 | } | |
16049 | } else { | |
16050 | if (!btf_vmlinux) { | |
16051 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
16052 | return -EINVAL; | |
16053 | } | |
16054 | btf = btf_vmlinux; | |
16055 | btf_get(btf); | |
4976b718 HL |
16056 | } |
16057 | ||
541c3bad | 16058 | t = btf_type_by_id(btf, id); |
4976b718 HL |
16059 | if (!t) { |
16060 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
16061 | err = -ENOENT; |
16062 | goto err_put; | |
4976b718 HL |
16063 | } |
16064 | ||
58aa2afb AS |
16065 | if (!btf_type_is_var(t) && !btf_type_is_func(t)) { |
16066 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR or KIND_FUNC\n", id); | |
541c3bad AN |
16067 | err = -EINVAL; |
16068 | goto err_put; | |
4976b718 HL |
16069 | } |
16070 | ||
541c3bad | 16071 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
16072 | addr = kallsyms_lookup_name(sym_name); |
16073 | if (!addr) { | |
16074 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
16075 | sym_name); | |
541c3bad AN |
16076 | err = -ENOENT; |
16077 | goto err_put; | |
4976b718 | 16078 | } |
58aa2afb AS |
16079 | insn[0].imm = (u32)addr; |
16080 | insn[1].imm = addr >> 32; | |
16081 | ||
16082 | if (btf_type_is_func(t)) { | |
16083 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; | |
16084 | aux->btf_var.mem_size = 0; | |
16085 | goto check_btf; | |
16086 | } | |
4976b718 | 16087 | |
541c3bad | 16088 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 16089 | if (datasec_id > 0) { |
541c3bad | 16090 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
16091 | for_each_vsi(i, datasec, vsi) { |
16092 | if (vsi->type == id) { | |
16093 | percpu = true; | |
16094 | break; | |
16095 | } | |
16096 | } | |
16097 | } | |
16098 | ||
4976b718 | 16099 | type = t->type; |
541c3bad | 16100 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 | 16101 | if (percpu) { |
5844101a | 16102 | aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; |
541c3bad | 16103 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
16104 | aux->btf_var.btf_id = type; |
16105 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
16106 | const struct btf_type *ret; |
16107 | const char *tname; | |
16108 | u32 tsize; | |
16109 | ||
16110 | /* resolve the type size of ksym. */ | |
541c3bad | 16111 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 16112 | if (IS_ERR(ret)) { |
541c3bad | 16113 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
16114 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
16115 | tname, PTR_ERR(ret)); | |
541c3bad AN |
16116 | err = -EINVAL; |
16117 | goto err_put; | |
4976b718 | 16118 | } |
34d3a78c | 16119 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
4976b718 HL |
16120 | aux->btf_var.mem_size = tsize; |
16121 | } else { | |
16122 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 16123 | aux->btf_var.btf = btf; |
4976b718 HL |
16124 | aux->btf_var.btf_id = type; |
16125 | } | |
58aa2afb | 16126 | check_btf: |
541c3bad AN |
16127 | /* check whether we recorded this BTF (and maybe module) already */ |
16128 | for (i = 0; i < env->used_btf_cnt; i++) { | |
16129 | if (env->used_btfs[i].btf == btf) { | |
16130 | btf_put(btf); | |
16131 | return 0; | |
16132 | } | |
16133 | } | |
16134 | ||
16135 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
16136 | err = -E2BIG; | |
16137 | goto err_put; | |
16138 | } | |
16139 | ||
16140 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
16141 | btf_mod->btf = btf; | |
16142 | btf_mod->module = NULL; | |
16143 | ||
16144 | /* if we reference variables from kernel module, bump its refcount */ | |
16145 | if (btf_is_module(btf)) { | |
16146 | btf_mod->module = btf_try_get_module(btf); | |
16147 | if (!btf_mod->module) { | |
16148 | err = -ENXIO; | |
16149 | goto err_put; | |
16150 | } | |
16151 | } | |
16152 | ||
16153 | env->used_btf_cnt++; | |
16154 | ||
4976b718 | 16155 | return 0; |
541c3bad AN |
16156 | err_put: |
16157 | btf_put(btf); | |
16158 | return err; | |
4976b718 HL |
16159 | } |
16160 | ||
d83525ca AS |
16161 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
16162 | { | |
16163 | switch (type) { | |
16164 | case BPF_PROG_TYPE_KPROBE: | |
16165 | case BPF_PROG_TYPE_TRACEPOINT: | |
16166 | case BPF_PROG_TYPE_PERF_EVENT: | |
16167 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
5002615a | 16168 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
d83525ca AS |
16169 | return true; |
16170 | default: | |
16171 | return false; | |
16172 | } | |
16173 | } | |
16174 | ||
61bd5218 JK |
16175 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
16176 | struct bpf_map *map, | |
fdc15d38 AS |
16177 | struct bpf_prog *prog) |
16178 | ||
16179 | { | |
7e40781c | 16180 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
a3884572 | 16181 | |
9c395c1b DM |
16182 | if (btf_record_has_field(map->record, BPF_LIST_HEAD) || |
16183 | btf_record_has_field(map->record, BPF_RB_ROOT)) { | |
f0c5941f | 16184 | if (is_tracing_prog_type(prog_type)) { |
9c395c1b | 16185 | verbose(env, "tracing progs cannot use bpf_{list_head,rb_root} yet\n"); |
f0c5941f KKD |
16186 | return -EINVAL; |
16187 | } | |
16188 | } | |
16189 | ||
db559117 | 16190 | if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) { |
9e7a4d98 KS |
16191 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
16192 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
16193 | return -EINVAL; | |
16194 | } | |
16195 | ||
16196 | if (is_tracing_prog_type(prog_type)) { | |
16197 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
16198 | return -EINVAL; | |
16199 | } | |
16200 | ||
16201 | if (prog->aux->sleepable) { | |
16202 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
16203 | return -EINVAL; | |
16204 | } | |
d83525ca AS |
16205 | } |
16206 | ||
db559117 | 16207 | if (btf_record_has_field(map->record, BPF_TIMER)) { |
5e0bc308 DB |
16208 | if (is_tracing_prog_type(prog_type)) { |
16209 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
16210 | return -EINVAL; | |
16211 | } | |
16212 | } | |
16213 | ||
9d03ebc7 | 16214 | if ((bpf_prog_is_offloaded(prog->aux) || bpf_map_is_offloaded(map)) && |
09728266 | 16215 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
16216 | verbose(env, "offload device mismatch between prog and map\n"); |
16217 | return -EINVAL; | |
16218 | } | |
16219 | ||
85d33df3 MKL |
16220 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
16221 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
16222 | return -EINVAL; | |
16223 | } | |
16224 | ||
1e6c62a8 AS |
16225 | if (prog->aux->sleepable) |
16226 | switch (map->map_type) { | |
16227 | case BPF_MAP_TYPE_HASH: | |
16228 | case BPF_MAP_TYPE_LRU_HASH: | |
16229 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
16230 | case BPF_MAP_TYPE_PERCPU_HASH: |
16231 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
16232 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
16233 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
16234 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
ba90c2cc | 16235 | case BPF_MAP_TYPE_RINGBUF: |
583c1f42 | 16236 | case BPF_MAP_TYPE_USER_RINGBUF: |
0fe4b381 KS |
16237 | case BPF_MAP_TYPE_INODE_STORAGE: |
16238 | case BPF_MAP_TYPE_SK_STORAGE: | |
16239 | case BPF_MAP_TYPE_TASK_STORAGE: | |
2c40d97d | 16240 | case BPF_MAP_TYPE_CGRP_STORAGE: |
ba90c2cc | 16241 | break; |
1e6c62a8 AS |
16242 | default: |
16243 | verbose(env, | |
2c40d97d | 16244 | "Sleepable programs can only use array, hash, ringbuf and local storage maps\n"); |
1e6c62a8 AS |
16245 | return -EINVAL; |
16246 | } | |
16247 | ||
fdc15d38 AS |
16248 | return 0; |
16249 | } | |
16250 | ||
b741f163 RG |
16251 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
16252 | { | |
16253 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
16254 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
16255 | } | |
16256 | ||
4976b718 HL |
16257 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
16258 | * | |
16259 | * 1. if it accesses map FD, replace it with actual map pointer. | |
16260 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
16261 | * | |
16262 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 16263 | */ |
4976b718 | 16264 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
16265 | { |
16266 | struct bpf_insn *insn = env->prog->insnsi; | |
16267 | int insn_cnt = env->prog->len; | |
fdc15d38 | 16268 | int i, j, err; |
0246e64d | 16269 | |
f1f7714e | 16270 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
16271 | if (err) |
16272 | return err; | |
16273 | ||
0246e64d | 16274 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 16275 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 16276 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 16277 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
16278 | return -EINVAL; |
16279 | } | |
16280 | ||
0246e64d | 16281 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 16282 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
16283 | struct bpf_map *map; |
16284 | struct fd f; | |
d8eca5bb | 16285 | u64 addr; |
387544bf | 16286 | u32 fd; |
0246e64d AS |
16287 | |
16288 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
16289 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
16290 | insn[1].off != 0) { | |
61bd5218 | 16291 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
16292 | return -EINVAL; |
16293 | } | |
16294 | ||
d8eca5bb | 16295 | if (insn[0].src_reg == 0) |
0246e64d AS |
16296 | /* valid generic load 64-bit imm */ |
16297 | goto next_insn; | |
16298 | ||
4976b718 HL |
16299 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
16300 | aux = &env->insn_aux_data[i]; | |
16301 | err = check_pseudo_btf_id(env, insn, aux); | |
16302 | if (err) | |
16303 | return err; | |
16304 | goto next_insn; | |
16305 | } | |
16306 | ||
69c087ba YS |
16307 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
16308 | aux = &env->insn_aux_data[i]; | |
16309 | aux->ptr_type = PTR_TO_FUNC; | |
16310 | goto next_insn; | |
16311 | } | |
16312 | ||
d8eca5bb DB |
16313 | /* In final convert_pseudo_ld_imm64() step, this is |
16314 | * converted into regular 64-bit imm load insn. | |
16315 | */ | |
387544bf AS |
16316 | switch (insn[0].src_reg) { |
16317 | case BPF_PSEUDO_MAP_VALUE: | |
16318 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
16319 | break; | |
16320 | case BPF_PSEUDO_MAP_FD: | |
16321 | case BPF_PSEUDO_MAP_IDX: | |
16322 | if (insn[1].imm == 0) | |
16323 | break; | |
16324 | fallthrough; | |
16325 | default: | |
16326 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
16327 | return -EINVAL; |
16328 | } | |
16329 | ||
387544bf AS |
16330 | switch (insn[0].src_reg) { |
16331 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
16332 | case BPF_PSEUDO_MAP_IDX: | |
16333 | if (bpfptr_is_null(env->fd_array)) { | |
16334 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
16335 | return -EPROTO; | |
16336 | } | |
16337 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
16338 | insn[0].imm * sizeof(fd), | |
16339 | sizeof(fd))) | |
16340 | return -EFAULT; | |
16341 | break; | |
16342 | default: | |
16343 | fd = insn[0].imm; | |
16344 | break; | |
16345 | } | |
16346 | ||
16347 | f = fdget(fd); | |
c2101297 | 16348 | map = __bpf_map_get(f); |
0246e64d | 16349 | if (IS_ERR(map)) { |
61bd5218 | 16350 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 16351 | insn[0].imm); |
0246e64d AS |
16352 | return PTR_ERR(map); |
16353 | } | |
16354 | ||
61bd5218 | 16355 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
16356 | if (err) { |
16357 | fdput(f); | |
16358 | return err; | |
16359 | } | |
16360 | ||
d8eca5bb | 16361 | aux = &env->insn_aux_data[i]; |
387544bf AS |
16362 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
16363 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
16364 | addr = (unsigned long)map; |
16365 | } else { | |
16366 | u32 off = insn[1].imm; | |
16367 | ||
16368 | if (off >= BPF_MAX_VAR_OFF) { | |
16369 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
16370 | fdput(f); | |
16371 | return -EINVAL; | |
16372 | } | |
16373 | ||
16374 | if (!map->ops->map_direct_value_addr) { | |
16375 | verbose(env, "no direct value access support for this map type\n"); | |
16376 | fdput(f); | |
16377 | return -EINVAL; | |
16378 | } | |
16379 | ||
16380 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
16381 | if (err) { | |
16382 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
16383 | map->value_size, off); | |
16384 | fdput(f); | |
16385 | return err; | |
16386 | } | |
16387 | ||
16388 | aux->map_off = off; | |
16389 | addr += off; | |
16390 | } | |
16391 | ||
16392 | insn[0].imm = (u32)addr; | |
16393 | insn[1].imm = addr >> 32; | |
0246e64d AS |
16394 | |
16395 | /* check whether we recorded this map already */ | |
d8eca5bb | 16396 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 16397 | if (env->used_maps[j] == map) { |
d8eca5bb | 16398 | aux->map_index = j; |
0246e64d AS |
16399 | fdput(f); |
16400 | goto next_insn; | |
16401 | } | |
d8eca5bb | 16402 | } |
0246e64d AS |
16403 | |
16404 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
16405 | fdput(f); | |
16406 | return -E2BIG; | |
16407 | } | |
16408 | ||
0246e64d AS |
16409 | /* hold the map. If the program is rejected by verifier, |
16410 | * the map will be released by release_maps() or it | |
16411 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 16412 | * and all maps are released in free_used_maps() |
0246e64d | 16413 | */ |
1e0bd5a0 | 16414 | bpf_map_inc(map); |
d8eca5bb DB |
16415 | |
16416 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
16417 | env->used_maps[env->used_map_cnt++] = map; |
16418 | ||
b741f163 | 16419 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 16420 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 16421 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
16422 | fdput(f); |
16423 | return -EBUSY; | |
16424 | } | |
16425 | ||
0246e64d AS |
16426 | fdput(f); |
16427 | next_insn: | |
16428 | insn++; | |
16429 | i++; | |
5e581dad DB |
16430 | continue; |
16431 | } | |
16432 | ||
16433 | /* Basic sanity check before we invest more work here. */ | |
16434 | if (!bpf_opcode_in_insntable(insn->code)) { | |
16435 | verbose(env, "unknown opcode %02x\n", insn->code); | |
16436 | return -EINVAL; | |
0246e64d AS |
16437 | } |
16438 | } | |
16439 | ||
16440 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
16441 | * 'struct bpf_map *' into a register instead of user map_fd. | |
16442 | * These pointers will be used later by verifier to validate map access. | |
16443 | */ | |
16444 | return 0; | |
16445 | } | |
16446 | ||
16447 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 16448 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 16449 | { |
a2ea0746 DB |
16450 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
16451 | env->used_map_cnt); | |
0246e64d AS |
16452 | } |
16453 | ||
541c3bad AN |
16454 | /* drop refcnt of maps used by the rejected program */ |
16455 | static void release_btfs(struct bpf_verifier_env *env) | |
16456 | { | |
16457 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
16458 | env->used_btf_cnt); | |
16459 | } | |
16460 | ||
0246e64d | 16461 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 16462 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
16463 | { |
16464 | struct bpf_insn *insn = env->prog->insnsi; | |
16465 | int insn_cnt = env->prog->len; | |
16466 | int i; | |
16467 | ||
69c087ba YS |
16468 | for (i = 0; i < insn_cnt; i++, insn++) { |
16469 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
16470 | continue; | |
16471 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
16472 | continue; | |
16473 | insn->src_reg = 0; | |
16474 | } | |
0246e64d AS |
16475 | } |
16476 | ||
8041902d AS |
16477 | /* single env->prog->insni[off] instruction was replaced with the range |
16478 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
16479 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
16480 | */ | |
75f0fc7b HF |
16481 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
16482 | struct bpf_insn_aux_data *new_data, | |
16483 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 16484 | { |
75f0fc7b | 16485 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 16486 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 16487 | u32 old_seen = old_data[off].seen; |
b325fbca | 16488 | u32 prog_len; |
c131187d | 16489 | int i; |
8041902d | 16490 | |
b325fbca JW |
16491 | /* aux info at OFF always needs adjustment, no matter fast path |
16492 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
16493 | * original insn at old prog. | |
16494 | */ | |
16495 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
16496 | ||
8041902d | 16497 | if (cnt == 1) |
75f0fc7b | 16498 | return; |
b325fbca | 16499 | prog_len = new_prog->len; |
75f0fc7b | 16500 | |
8041902d AS |
16501 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
16502 | memcpy(new_data + off + cnt - 1, old_data + off, | |
16503 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 16504 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
16505 | /* Expand insni[off]'s seen count to the patched range. */ |
16506 | new_data[i].seen = old_seen; | |
b325fbca JW |
16507 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
16508 | } | |
8041902d AS |
16509 | env->insn_aux_data = new_data; |
16510 | vfree(old_data); | |
8041902d AS |
16511 | } |
16512 | ||
cc8b0b92 AS |
16513 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
16514 | { | |
16515 | int i; | |
16516 | ||
16517 | if (len == 1) | |
16518 | return; | |
4cb3d99c JW |
16519 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
16520 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 16521 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 16522 | continue; |
9c8105bd | 16523 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
16524 | } |
16525 | } | |
16526 | ||
7506d211 | 16527 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
16528 | { |
16529 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
16530 | int i, sz = prog->aux->size_poke_tab; | |
16531 | struct bpf_jit_poke_descriptor *desc; | |
16532 | ||
16533 | for (i = 0; i < sz; i++) { | |
16534 | desc = &tab[i]; | |
7506d211 JF |
16535 | if (desc->insn_idx <= off) |
16536 | continue; | |
a748c697 MF |
16537 | desc->insn_idx += len - 1; |
16538 | } | |
16539 | } | |
16540 | ||
8041902d AS |
16541 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
16542 | const struct bpf_insn *patch, u32 len) | |
16543 | { | |
16544 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
16545 | struct bpf_insn_aux_data *new_data = NULL; |
16546 | ||
16547 | if (len > 1) { | |
16548 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
16549 | sizeof(struct bpf_insn_aux_data))); | |
16550 | if (!new_data) | |
16551 | return NULL; | |
16552 | } | |
8041902d AS |
16553 | |
16554 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
16555 | if (IS_ERR(new_prog)) { |
16556 | if (PTR_ERR(new_prog) == -ERANGE) | |
16557 | verbose(env, | |
16558 | "insn %d cannot be patched due to 16-bit range\n", | |
16559 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 16560 | vfree(new_data); |
8041902d | 16561 | return NULL; |
4f73379e | 16562 | } |
75f0fc7b | 16563 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 16564 | adjust_subprog_starts(env, off, len); |
7506d211 | 16565 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
16566 | return new_prog; |
16567 | } | |
16568 | ||
52875a04 JK |
16569 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
16570 | u32 off, u32 cnt) | |
16571 | { | |
16572 | int i, j; | |
16573 | ||
16574 | /* find first prog starting at or after off (first to remove) */ | |
16575 | for (i = 0; i < env->subprog_cnt; i++) | |
16576 | if (env->subprog_info[i].start >= off) | |
16577 | break; | |
16578 | /* find first prog starting at or after off + cnt (first to stay) */ | |
16579 | for (j = i; j < env->subprog_cnt; j++) | |
16580 | if (env->subprog_info[j].start >= off + cnt) | |
16581 | break; | |
16582 | /* if j doesn't start exactly at off + cnt, we are just removing | |
16583 | * the front of previous prog | |
16584 | */ | |
16585 | if (env->subprog_info[j].start != off + cnt) | |
16586 | j--; | |
16587 | ||
16588 | if (j > i) { | |
16589 | struct bpf_prog_aux *aux = env->prog->aux; | |
16590 | int move; | |
16591 | ||
16592 | /* move fake 'exit' subprog as well */ | |
16593 | move = env->subprog_cnt + 1 - j; | |
16594 | ||
16595 | memmove(env->subprog_info + i, | |
16596 | env->subprog_info + j, | |
16597 | sizeof(*env->subprog_info) * move); | |
16598 | env->subprog_cnt -= j - i; | |
16599 | ||
16600 | /* remove func_info */ | |
16601 | if (aux->func_info) { | |
16602 | move = aux->func_info_cnt - j; | |
16603 | ||
16604 | memmove(aux->func_info + i, | |
16605 | aux->func_info + j, | |
16606 | sizeof(*aux->func_info) * move); | |
16607 | aux->func_info_cnt -= j - i; | |
16608 | /* func_info->insn_off is set after all code rewrites, | |
16609 | * in adjust_btf_func() - no need to adjust | |
16610 | */ | |
16611 | } | |
16612 | } else { | |
16613 | /* convert i from "first prog to remove" to "first to adjust" */ | |
16614 | if (env->subprog_info[i].start == off) | |
16615 | i++; | |
16616 | } | |
16617 | ||
16618 | /* update fake 'exit' subprog as well */ | |
16619 | for (; i <= env->subprog_cnt; i++) | |
16620 | env->subprog_info[i].start -= cnt; | |
16621 | ||
16622 | return 0; | |
16623 | } | |
16624 | ||
16625 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
16626 | u32 cnt) | |
16627 | { | |
16628 | struct bpf_prog *prog = env->prog; | |
16629 | u32 i, l_off, l_cnt, nr_linfo; | |
16630 | struct bpf_line_info *linfo; | |
16631 | ||
16632 | nr_linfo = prog->aux->nr_linfo; | |
16633 | if (!nr_linfo) | |
16634 | return 0; | |
16635 | ||
16636 | linfo = prog->aux->linfo; | |
16637 | ||
16638 | /* find first line info to remove, count lines to be removed */ | |
16639 | for (i = 0; i < nr_linfo; i++) | |
16640 | if (linfo[i].insn_off >= off) | |
16641 | break; | |
16642 | ||
16643 | l_off = i; | |
16644 | l_cnt = 0; | |
16645 | for (; i < nr_linfo; i++) | |
16646 | if (linfo[i].insn_off < off + cnt) | |
16647 | l_cnt++; | |
16648 | else | |
16649 | break; | |
16650 | ||
16651 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
16652 | * last removed linfo. prog is already modified, so prog->len == off | |
16653 | * means no live instructions after (tail of the program was removed). | |
16654 | */ | |
16655 | if (prog->len != off && l_cnt && | |
16656 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
16657 | l_cnt--; | |
16658 | linfo[--i].insn_off = off + cnt; | |
16659 | } | |
16660 | ||
16661 | /* remove the line info which refer to the removed instructions */ | |
16662 | if (l_cnt) { | |
16663 | memmove(linfo + l_off, linfo + i, | |
16664 | sizeof(*linfo) * (nr_linfo - i)); | |
16665 | ||
16666 | prog->aux->nr_linfo -= l_cnt; | |
16667 | nr_linfo = prog->aux->nr_linfo; | |
16668 | } | |
16669 | ||
16670 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
16671 | for (i = l_off; i < nr_linfo; i++) | |
16672 | linfo[i].insn_off -= cnt; | |
16673 | ||
16674 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
16675 | for (i = 0; i <= env->subprog_cnt; i++) | |
16676 | if (env->subprog_info[i].linfo_idx > l_off) { | |
16677 | /* program may have started in the removed region but | |
16678 | * may not be fully removed | |
16679 | */ | |
16680 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
16681 | env->subprog_info[i].linfo_idx -= l_cnt; | |
16682 | else | |
16683 | env->subprog_info[i].linfo_idx = l_off; | |
16684 | } | |
16685 | ||
16686 | return 0; | |
16687 | } | |
16688 | ||
16689 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
16690 | { | |
16691 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16692 | unsigned int orig_prog_len = env->prog->len; | |
16693 | int err; | |
16694 | ||
9d03ebc7 | 16695 | if (bpf_prog_is_offloaded(env->prog->aux)) |
08ca90af JK |
16696 | bpf_prog_offload_remove_insns(env, off, cnt); |
16697 | ||
52875a04 JK |
16698 | err = bpf_remove_insns(env->prog, off, cnt); |
16699 | if (err) | |
16700 | return err; | |
16701 | ||
16702 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
16703 | if (err) | |
16704 | return err; | |
16705 | ||
16706 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
16707 | if (err) | |
16708 | return err; | |
16709 | ||
16710 | memmove(aux_data + off, aux_data + off + cnt, | |
16711 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
16712 | ||
16713 | return 0; | |
16714 | } | |
16715 | ||
2a5418a1 DB |
16716 | /* The verifier does more data flow analysis than llvm and will not |
16717 | * explore branches that are dead at run time. Malicious programs can | |
16718 | * have dead code too. Therefore replace all dead at-run-time code | |
16719 | * with 'ja -1'. | |
16720 | * | |
16721 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
16722 | * program and through another bug we would manage to jump there, then | |
16723 | * we'd execute beyond program memory otherwise. Returning exception | |
16724 | * code also wouldn't work since we can have subprogs where the dead | |
16725 | * code could be located. | |
c131187d AS |
16726 | */ |
16727 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
16728 | { | |
16729 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 16730 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
16731 | struct bpf_insn *insn = env->prog->insnsi; |
16732 | const int insn_cnt = env->prog->len; | |
16733 | int i; | |
16734 | ||
16735 | for (i = 0; i < insn_cnt; i++) { | |
16736 | if (aux_data[i].seen) | |
16737 | continue; | |
2a5418a1 | 16738 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 16739 | aux_data[i].zext_dst = false; |
c131187d AS |
16740 | } |
16741 | } | |
16742 | ||
e2ae4ca2 JK |
16743 | static bool insn_is_cond_jump(u8 code) |
16744 | { | |
16745 | u8 op; | |
16746 | ||
092ed096 JW |
16747 | if (BPF_CLASS(code) == BPF_JMP32) |
16748 | return true; | |
16749 | ||
e2ae4ca2 JK |
16750 | if (BPF_CLASS(code) != BPF_JMP) |
16751 | return false; | |
16752 | ||
16753 | op = BPF_OP(code); | |
16754 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
16755 | } | |
16756 | ||
16757 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
16758 | { | |
16759 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16760 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
16761 | struct bpf_insn *insn = env->prog->insnsi; | |
16762 | const int insn_cnt = env->prog->len; | |
16763 | int i; | |
16764 | ||
16765 | for (i = 0; i < insn_cnt; i++, insn++) { | |
16766 | if (!insn_is_cond_jump(insn->code)) | |
16767 | continue; | |
16768 | ||
16769 | if (!aux_data[i + 1].seen) | |
16770 | ja.off = insn->off; | |
16771 | else if (!aux_data[i + 1 + insn->off].seen) | |
16772 | ja.off = 0; | |
16773 | else | |
16774 | continue; | |
16775 | ||
9d03ebc7 | 16776 | if (bpf_prog_is_offloaded(env->prog->aux)) |
08ca90af JK |
16777 | bpf_prog_offload_replace_insn(env, i, &ja); |
16778 | ||
e2ae4ca2 JK |
16779 | memcpy(insn, &ja, sizeof(ja)); |
16780 | } | |
16781 | } | |
16782 | ||
52875a04 JK |
16783 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
16784 | { | |
16785 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
16786 | int insn_cnt = env->prog->len; | |
16787 | int i, err; | |
16788 | ||
16789 | for (i = 0; i < insn_cnt; i++) { | |
16790 | int j; | |
16791 | ||
16792 | j = 0; | |
16793 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
16794 | j++; | |
16795 | if (!j) | |
16796 | continue; | |
16797 | ||
16798 | err = verifier_remove_insns(env, i, j); | |
16799 | if (err) | |
16800 | return err; | |
16801 | insn_cnt = env->prog->len; | |
16802 | } | |
16803 | ||
16804 | return 0; | |
16805 | } | |
16806 | ||
a1b14abc JK |
16807 | static int opt_remove_nops(struct bpf_verifier_env *env) |
16808 | { | |
16809 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
16810 | struct bpf_insn *insn = env->prog->insnsi; | |
16811 | int insn_cnt = env->prog->len; | |
16812 | int i, err; | |
16813 | ||
16814 | for (i = 0; i < insn_cnt; i++) { | |
16815 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
16816 | continue; | |
16817 | ||
16818 | err = verifier_remove_insns(env, i, 1); | |
16819 | if (err) | |
16820 | return err; | |
16821 | insn_cnt--; | |
16822 | i--; | |
16823 | } | |
16824 | ||
16825 | return 0; | |
16826 | } | |
16827 | ||
d6c2308c JW |
16828 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
16829 | const union bpf_attr *attr) | |
a4b1d3c1 | 16830 | { |
d6c2308c | 16831 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 16832 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 16833 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 16834 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 16835 | struct bpf_prog *new_prog; |
d6c2308c | 16836 | bool rnd_hi32; |
a4b1d3c1 | 16837 | |
d6c2308c | 16838 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 16839 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
16840 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
16841 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
16842 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
16843 | for (i = 0; i < len; i++) { |
16844 | int adj_idx = i + delta; | |
16845 | struct bpf_insn insn; | |
83a28819 | 16846 | int load_reg; |
a4b1d3c1 | 16847 | |
d6c2308c | 16848 | insn = insns[adj_idx]; |
83a28819 | 16849 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
16850 | if (!aux[adj_idx].zext_dst) { |
16851 | u8 code, class; | |
16852 | u32 imm_rnd; | |
16853 | ||
16854 | if (!rnd_hi32) | |
16855 | continue; | |
16856 | ||
16857 | code = insn.code; | |
16858 | class = BPF_CLASS(code); | |
83a28819 | 16859 | if (load_reg == -1) |
d6c2308c JW |
16860 | continue; |
16861 | ||
16862 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
16863 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
16864 | * here. | |
d6c2308c | 16865 | */ |
83a28819 | 16866 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
16867 | if (class == BPF_LD && |
16868 | BPF_MODE(code) == BPF_IMM) | |
16869 | i++; | |
16870 | continue; | |
16871 | } | |
16872 | ||
16873 | /* ctx load could be transformed into wider load. */ | |
16874 | if (class == BPF_LDX && | |
16875 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
16876 | continue; | |
16877 | ||
a251c17a | 16878 | imm_rnd = get_random_u32(); |
d6c2308c JW |
16879 | rnd_hi32_patch[0] = insn; |
16880 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 16881 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
16882 | patch = rnd_hi32_patch; |
16883 | patch_len = 4; | |
16884 | goto apply_patch_buffer; | |
16885 | } | |
16886 | ||
39491867 BJ |
16887 | /* Add in an zero-extend instruction if a) the JIT has requested |
16888 | * it or b) it's a CMPXCHG. | |
16889 | * | |
16890 | * The latter is because: BPF_CMPXCHG always loads a value into | |
16891 | * R0, therefore always zero-extends. However some archs' | |
16892 | * equivalent instruction only does this load when the | |
16893 | * comparison is successful. This detail of CMPXCHG is | |
16894 | * orthogonal to the general zero-extension behaviour of the | |
16895 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
16896 | */ | |
16897 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
16898 | continue; |
16899 | ||
d35af0a7 BT |
16900 | /* Zero-extension is done by the caller. */ |
16901 | if (bpf_pseudo_kfunc_call(&insn)) | |
16902 | continue; | |
16903 | ||
83a28819 IL |
16904 | if (WARN_ON(load_reg == -1)) { |
16905 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
16906 | return -EFAULT; | |
b2e37a71 IL |
16907 | } |
16908 | ||
a4b1d3c1 | 16909 | zext_patch[0] = insn; |
b2e37a71 IL |
16910 | zext_patch[1].dst_reg = load_reg; |
16911 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
16912 | patch = zext_patch; |
16913 | patch_len = 2; | |
16914 | apply_patch_buffer: | |
16915 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
16916 | if (!new_prog) |
16917 | return -ENOMEM; | |
16918 | env->prog = new_prog; | |
16919 | insns = new_prog->insnsi; | |
16920 | aux = env->insn_aux_data; | |
d6c2308c | 16921 | delta += patch_len - 1; |
a4b1d3c1 JW |
16922 | } |
16923 | ||
16924 | return 0; | |
16925 | } | |
16926 | ||
c64b7983 JS |
16927 | /* convert load instructions that access fields of a context type into a |
16928 | * sequence of instructions that access fields of the underlying structure: | |
16929 | * struct __sk_buff -> struct sk_buff | |
16930 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 16931 | */ |
58e2af8b | 16932 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 16933 | { |
00176a34 | 16934 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 16935 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 16936 | const int insn_cnt = env->prog->len; |
36bbef52 | 16937 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 16938 | u32 target_size, size_default, off; |
9bac3d6d | 16939 | struct bpf_prog *new_prog; |
d691f9e8 | 16940 | enum bpf_access_type type; |
f96da094 | 16941 | bool is_narrower_load; |
9bac3d6d | 16942 | |
b09928b9 DB |
16943 | if (ops->gen_prologue || env->seen_direct_write) { |
16944 | if (!ops->gen_prologue) { | |
16945 | verbose(env, "bpf verifier is misconfigured\n"); | |
16946 | return -EINVAL; | |
16947 | } | |
36bbef52 DB |
16948 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
16949 | env->prog); | |
16950 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 16951 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
16952 | return -EINVAL; |
16953 | } else if (cnt) { | |
8041902d | 16954 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
16955 | if (!new_prog) |
16956 | return -ENOMEM; | |
8041902d | 16957 | |
36bbef52 | 16958 | env->prog = new_prog; |
3df126f3 | 16959 | delta += cnt - 1; |
36bbef52 DB |
16960 | } |
16961 | } | |
16962 | ||
9d03ebc7 | 16963 | if (bpf_prog_is_offloaded(env->prog->aux)) |
9bac3d6d AS |
16964 | return 0; |
16965 | ||
3df126f3 | 16966 | insn = env->prog->insnsi + delta; |
36bbef52 | 16967 | |
9bac3d6d | 16968 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
16969 | bpf_convert_ctx_access_t convert_ctx_access; |
16970 | ||
62c7989b DB |
16971 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
16972 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
16973 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 16974 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 16975 | type = BPF_READ; |
2039f26f DB |
16976 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
16977 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
16978 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
16979 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
16980 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
16981 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
16982 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
16983 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 16984 | type = BPF_WRITE; |
2039f26f | 16985 | } else { |
9bac3d6d | 16986 | continue; |
2039f26f | 16987 | } |
9bac3d6d | 16988 | |
af86ca4e | 16989 | if (type == BPF_WRITE && |
2039f26f | 16990 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 16991 | struct bpf_insn patch[] = { |
af86ca4e | 16992 | *insn, |
2039f26f | 16993 | BPF_ST_NOSPEC(), |
af86ca4e AS |
16994 | }; |
16995 | ||
16996 | cnt = ARRAY_SIZE(patch); | |
16997 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
16998 | if (!new_prog) | |
16999 | return -ENOMEM; | |
17000 | ||
17001 | delta += cnt - 1; | |
17002 | env->prog = new_prog; | |
17003 | insn = new_prog->insnsi + i + delta; | |
17004 | continue; | |
17005 | } | |
17006 | ||
6efe152d | 17007 | switch ((int)env->insn_aux_data[i + delta].ptr_type) { |
c64b7983 JS |
17008 | case PTR_TO_CTX: |
17009 | if (!ops->convert_ctx_access) | |
17010 | continue; | |
17011 | convert_ctx_access = ops->convert_ctx_access; | |
17012 | break; | |
17013 | case PTR_TO_SOCKET: | |
46f8bc92 | 17014 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
17015 | convert_ctx_access = bpf_sock_convert_ctx_access; |
17016 | break; | |
655a51e5 MKL |
17017 | case PTR_TO_TCP_SOCK: |
17018 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
17019 | break; | |
fada7fdc JL |
17020 | case PTR_TO_XDP_SOCK: |
17021 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
17022 | break; | |
2a02759e | 17023 | case PTR_TO_BTF_ID: |
6efe152d | 17024 | case PTR_TO_BTF_ID | PTR_UNTRUSTED: |
282de143 KKD |
17025 | /* PTR_TO_BTF_ID | MEM_ALLOC always has a valid lifetime, unlike |
17026 | * PTR_TO_BTF_ID, and an active ref_obj_id, but the same cannot | |
17027 | * be said once it is marked PTR_UNTRUSTED, hence we must handle | |
17028 | * any faults for loads into such types. BPF_WRITE is disallowed | |
17029 | * for this case. | |
17030 | */ | |
17031 | case PTR_TO_BTF_ID | MEM_ALLOC | PTR_UNTRUSTED: | |
27ae7997 MKL |
17032 | if (type == BPF_READ) { |
17033 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
17034 | BPF_SIZE((insn)->code); | |
17035 | env->prog->aux->num_exentries++; | |
2a02759e | 17036 | } |
2a02759e | 17037 | continue; |
c64b7983 | 17038 | default: |
9bac3d6d | 17039 | continue; |
c64b7983 | 17040 | } |
9bac3d6d | 17041 | |
31fd8581 | 17042 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 17043 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
17044 | |
17045 | /* If the read access is a narrower load of the field, | |
17046 | * convert to a 4/8-byte load, to minimum program type specific | |
17047 | * convert_ctx_access changes. If conversion is successful, | |
17048 | * we will apply proper mask to the result. | |
17049 | */ | |
f96da094 | 17050 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
17051 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
17052 | off = insn->off; | |
31fd8581 | 17053 | if (is_narrower_load) { |
f96da094 DB |
17054 | u8 size_code; |
17055 | ||
17056 | if (type == BPF_WRITE) { | |
61bd5218 | 17057 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
17058 | return -EINVAL; |
17059 | } | |
31fd8581 | 17060 | |
f96da094 | 17061 | size_code = BPF_H; |
31fd8581 YS |
17062 | if (ctx_field_size == 4) |
17063 | size_code = BPF_W; | |
17064 | else if (ctx_field_size == 8) | |
17065 | size_code = BPF_DW; | |
f96da094 | 17066 | |
bc23105c | 17067 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
17068 | insn->code = BPF_LDX | BPF_MEM | size_code; |
17069 | } | |
f96da094 DB |
17070 | |
17071 | target_size = 0; | |
c64b7983 JS |
17072 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
17073 | &target_size); | |
f96da094 DB |
17074 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
17075 | (ctx_field_size && !target_size)) { | |
61bd5218 | 17076 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
17077 | return -EINVAL; |
17078 | } | |
f96da094 DB |
17079 | |
17080 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
17081 | u8 shift = bpf_ctx_narrow_access_offset( |
17082 | off, size, size_default) * 8; | |
d7af7e49 AI |
17083 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
17084 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
17085 | return -EINVAL; | |
17086 | } | |
46f53a65 AI |
17087 | if (ctx_field_size <= 4) { |
17088 | if (shift) | |
17089 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
17090 | insn->dst_reg, | |
17091 | shift); | |
31fd8581 | 17092 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 17093 | (1 << size * 8) - 1); |
46f53a65 AI |
17094 | } else { |
17095 | if (shift) | |
17096 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
17097 | insn->dst_reg, | |
17098 | shift); | |
31fd8581 | 17099 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 17100 | (1ULL << size * 8) - 1); |
46f53a65 | 17101 | } |
31fd8581 | 17102 | } |
9bac3d6d | 17103 | |
8041902d | 17104 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
17105 | if (!new_prog) |
17106 | return -ENOMEM; | |
17107 | ||
3df126f3 | 17108 | delta += cnt - 1; |
9bac3d6d AS |
17109 | |
17110 | /* keep walking new program and skip insns we just inserted */ | |
17111 | env->prog = new_prog; | |
3df126f3 | 17112 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
17113 | } |
17114 | ||
17115 | return 0; | |
17116 | } | |
17117 | ||
1c2a088a AS |
17118 | static int jit_subprogs(struct bpf_verifier_env *env) |
17119 | { | |
17120 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
17121 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 17122 | struct bpf_map *map_ptr; |
7105e828 | 17123 | struct bpf_insn *insn; |
1c2a088a | 17124 | void *old_bpf_func; |
c4c0bdc0 | 17125 | int err, num_exentries; |
1c2a088a | 17126 | |
f910cefa | 17127 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
17128 | return 0; |
17129 | ||
7105e828 | 17130 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 17131 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 17132 | continue; |
69c087ba | 17133 | |
c7a89784 DB |
17134 | /* Upon error here we cannot fall back to interpreter but |
17135 | * need a hard reject of the program. Thus -EFAULT is | |
17136 | * propagated in any case. | |
17137 | */ | |
1c2a088a AS |
17138 | subprog = find_subprog(env, i + insn->imm + 1); |
17139 | if (subprog < 0) { | |
17140 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
17141 | i + insn->imm + 1); | |
17142 | return -EFAULT; | |
17143 | } | |
17144 | /* temporarily remember subprog id inside insn instead of | |
17145 | * aux_data, since next loop will split up all insns into funcs | |
17146 | */ | |
f910cefa | 17147 | insn->off = subprog; |
1c2a088a AS |
17148 | /* remember original imm in case JIT fails and fallback |
17149 | * to interpreter will be needed | |
17150 | */ | |
17151 | env->insn_aux_data[i].call_imm = insn->imm; | |
17152 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
17153 | insn->imm = 1; | |
3990ed4c MKL |
17154 | if (bpf_pseudo_func(insn)) |
17155 | /* jit (e.g. x86_64) may emit fewer instructions | |
17156 | * if it learns a u32 imm is the same as a u64 imm. | |
17157 | * Force a non zero here. | |
17158 | */ | |
17159 | insn[1].imm = 1; | |
1c2a088a AS |
17160 | } |
17161 | ||
c454a46b MKL |
17162 | err = bpf_prog_alloc_jited_linfo(prog); |
17163 | if (err) | |
17164 | goto out_undo_insn; | |
17165 | ||
17166 | err = -ENOMEM; | |
6396bb22 | 17167 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 17168 | if (!func) |
c7a89784 | 17169 | goto out_undo_insn; |
1c2a088a | 17170 | |
f910cefa | 17171 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 17172 | subprog_start = subprog_end; |
4cb3d99c | 17173 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
17174 | |
17175 | len = subprog_end - subprog_start; | |
fb7dd8bc | 17176 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
17177 | * hence main prog stats include the runtime of subprogs. |
17178 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 17179 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
17180 | */ |
17181 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
17182 | if (!func[i]) |
17183 | goto out_free; | |
17184 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
17185 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 17186 | func[i]->type = prog->type; |
1c2a088a | 17187 | func[i]->len = len; |
4f74d809 DB |
17188 | if (bpf_prog_calc_tag(func[i])) |
17189 | goto out_free; | |
1c2a088a | 17190 | func[i]->is_func = 1; |
ba64e7d8 | 17191 | func[i]->aux->func_idx = i; |
f263a814 | 17192 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
17193 | func[i]->aux->btf = prog->aux->btf; |
17194 | func[i]->aux->func_info = prog->aux->func_info; | |
9c7c48d6 | 17195 | func[i]->aux->func_info_cnt = prog->aux->func_info_cnt; |
f263a814 JF |
17196 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
17197 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 17198 | |
a748c697 | 17199 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 17200 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 17201 | |
f263a814 JF |
17202 | poke = &prog->aux->poke_tab[j]; |
17203 | if (poke->insn_idx < subprog_end && | |
17204 | poke->insn_idx >= subprog_start) | |
17205 | poke->aux = func[i]->aux; | |
a748c697 MF |
17206 | } |
17207 | ||
1c2a088a | 17208 | func[i]->aux->name[0] = 'F'; |
9c8105bd | 17209 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 17210 | func[i]->jit_requested = 1; |
d2a3b7c5 | 17211 | func[i]->blinding_requested = prog->blinding_requested; |
e6ac2450 | 17212 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 17213 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
17214 | func[i]->aux->linfo = prog->aux->linfo; |
17215 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
17216 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
17217 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
17218 | num_exentries = 0; |
17219 | insn = func[i]->insnsi; | |
17220 | for (j = 0; j < func[i]->len; j++, insn++) { | |
17221 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
17222 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
17223 | num_exentries++; | |
17224 | } | |
17225 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 17226 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
17227 | func[i] = bpf_int_jit_compile(func[i]); |
17228 | if (!func[i]->jited) { | |
17229 | err = -ENOTSUPP; | |
17230 | goto out_free; | |
17231 | } | |
17232 | cond_resched(); | |
17233 | } | |
a748c697 | 17234 | |
1c2a088a AS |
17235 | /* at this point all bpf functions were successfully JITed |
17236 | * now populate all bpf_calls with correct addresses and | |
17237 | * run last pass of JIT | |
17238 | */ | |
f910cefa | 17239 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17240 | insn = func[i]->insnsi; |
17241 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 17242 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 17243 | subprog = insn->off; |
69c087ba YS |
17244 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
17245 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
17246 | continue; | |
17247 | } | |
23a2d70c | 17248 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
17249 | continue; |
17250 | subprog = insn->off; | |
3d717fad | 17251 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 17252 | } |
2162fed4 SD |
17253 | |
17254 | /* we use the aux data to keep a list of the start addresses | |
17255 | * of the JITed images for each function in the program | |
17256 | * | |
17257 | * for some architectures, such as powerpc64, the imm field | |
17258 | * might not be large enough to hold the offset of the start | |
17259 | * address of the callee's JITed image from __bpf_call_base | |
17260 | * | |
17261 | * in such cases, we can lookup the start address of a callee | |
17262 | * by using its subprog id, available from the off field of | |
17263 | * the call instruction, as an index for this list | |
17264 | */ | |
17265 | func[i]->aux->func = func; | |
17266 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 17267 | } |
f910cefa | 17268 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17269 | old_bpf_func = func[i]->bpf_func; |
17270 | tmp = bpf_int_jit_compile(func[i]); | |
17271 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
17272 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 17273 | err = -ENOTSUPP; |
1c2a088a AS |
17274 | goto out_free; |
17275 | } | |
17276 | cond_resched(); | |
17277 | } | |
17278 | ||
17279 | /* finally lock prog and jit images for all functions and | |
17280 | * populate kallsysm | |
17281 | */ | |
f910cefa | 17282 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
17283 | bpf_prog_lock_ro(func[i]); |
17284 | bpf_prog_kallsyms_add(func[i]); | |
17285 | } | |
7105e828 DB |
17286 | |
17287 | /* Last step: make now unused interpreter insns from main | |
17288 | * prog consistent for later dump requests, so they can | |
17289 | * later look the same as if they were interpreted only. | |
17290 | */ | |
17291 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
17292 | if (bpf_pseudo_func(insn)) { |
17293 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
17294 | insn[1].imm = insn->off; |
17295 | insn->off = 0; | |
69c087ba YS |
17296 | continue; |
17297 | } | |
23a2d70c | 17298 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
17299 | continue; |
17300 | insn->off = env->insn_aux_data[i].call_imm; | |
17301 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 17302 | insn->imm = subprog; |
7105e828 DB |
17303 | } |
17304 | ||
1c2a088a AS |
17305 | prog->jited = 1; |
17306 | prog->bpf_func = func[0]->bpf_func; | |
d00c6473 | 17307 | prog->jited_len = func[0]->jited_len; |
1c2a088a | 17308 | prog->aux->func = func; |
f910cefa | 17309 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 17310 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
17311 | return 0; |
17312 | out_free: | |
f263a814 JF |
17313 | /* We failed JIT'ing, so at this point we need to unregister poke |
17314 | * descriptors from subprogs, so that kernel is not attempting to | |
17315 | * patch it anymore as we're freeing the subprog JIT memory. | |
17316 | */ | |
17317 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
17318 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
17319 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
17320 | } | |
17321 | /* At this point we're guaranteed that poke descriptors are not | |
17322 | * live anymore. We can just unlink its descriptor table as it's | |
17323 | * released with the main prog. | |
17324 | */ | |
a748c697 MF |
17325 | for (i = 0; i < env->subprog_cnt; i++) { |
17326 | if (!func[i]) | |
17327 | continue; | |
f263a814 | 17328 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
17329 | bpf_jit_free(func[i]); |
17330 | } | |
1c2a088a | 17331 | kfree(func); |
c7a89784 | 17332 | out_undo_insn: |
1c2a088a AS |
17333 | /* cleanup main prog to be interpreted */ |
17334 | prog->jit_requested = 0; | |
d2a3b7c5 | 17335 | prog->blinding_requested = 0; |
1c2a088a | 17336 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
23a2d70c | 17337 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
17338 | continue; |
17339 | insn->off = 0; | |
17340 | insn->imm = env->insn_aux_data[i].call_imm; | |
17341 | } | |
e16301fb | 17342 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
17343 | return err; |
17344 | } | |
17345 | ||
1ea47e01 AS |
17346 | static int fixup_call_args(struct bpf_verifier_env *env) |
17347 | { | |
19d28fbd | 17348 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
17349 | struct bpf_prog *prog = env->prog; |
17350 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 17351 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 17352 | int i, depth; |
19d28fbd | 17353 | #endif |
e4052d06 | 17354 | int err = 0; |
1ea47e01 | 17355 | |
e4052d06 | 17356 | if (env->prog->jit_requested && |
9d03ebc7 | 17357 | !bpf_prog_is_offloaded(env->prog->aux)) { |
19d28fbd DM |
17358 | err = jit_subprogs(env); |
17359 | if (err == 0) | |
1c2a088a | 17360 | return 0; |
c7a89784 DB |
17361 | if (err == -EFAULT) |
17362 | return err; | |
19d28fbd DM |
17363 | } |
17364 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
17365 | if (has_kfunc_call) { |
17366 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
17367 | return -EINVAL; | |
17368 | } | |
e411901c MF |
17369 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
17370 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
17371 | * have to be rejected, since interpreter doesn't support them yet. | |
17372 | */ | |
17373 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
17374 | return -EINVAL; | |
17375 | } | |
1ea47e01 | 17376 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
17377 | if (bpf_pseudo_func(insn)) { |
17378 | /* When JIT fails the progs with callback calls | |
17379 | * have to be rejected, since interpreter doesn't support them yet. | |
17380 | */ | |
17381 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
17382 | return -EINVAL; | |
17383 | } | |
17384 | ||
23a2d70c | 17385 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
17386 | continue; |
17387 | depth = get_callee_stack_depth(env, insn, i); | |
17388 | if (depth < 0) | |
17389 | return depth; | |
17390 | bpf_patch_call_args(insn, depth); | |
17391 | } | |
19d28fbd DM |
17392 | err = 0; |
17393 | #endif | |
17394 | return err; | |
1ea47e01 AS |
17395 | } |
17396 | ||
1cf3bfc6 IL |
17397 | /* replace a generic kfunc with a specialized version if necessary */ |
17398 | static void specialize_kfunc(struct bpf_verifier_env *env, | |
17399 | u32 func_id, u16 offset, unsigned long *addr) | |
17400 | { | |
17401 | struct bpf_prog *prog = env->prog; | |
17402 | bool seen_direct_write; | |
17403 | void *xdp_kfunc; | |
17404 | bool is_rdonly; | |
17405 | ||
17406 | if (bpf_dev_bound_kfunc_id(func_id)) { | |
17407 | xdp_kfunc = bpf_dev_bound_resolve_kfunc(prog, func_id); | |
17408 | if (xdp_kfunc) { | |
17409 | *addr = (unsigned long)xdp_kfunc; | |
17410 | return; | |
17411 | } | |
17412 | /* fallback to default kfunc when not supported by netdev */ | |
17413 | } | |
17414 | ||
17415 | if (offset) | |
17416 | return; | |
17417 | ||
17418 | if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { | |
17419 | seen_direct_write = env->seen_direct_write; | |
17420 | is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE); | |
17421 | ||
17422 | if (is_rdonly) | |
17423 | *addr = (unsigned long)bpf_dynptr_from_skb_rdonly; | |
17424 | ||
17425 | /* restore env->seen_direct_write to its original value, since | |
17426 | * may_access_direct_pkt_data mutates it | |
17427 | */ | |
17428 | env->seen_direct_write = seen_direct_write; | |
17429 | } | |
17430 | } | |
17431 | ||
d2dcc67d DM |
17432 | static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux, |
17433 | u16 struct_meta_reg, | |
17434 | u16 node_offset_reg, | |
17435 | struct bpf_insn *insn, | |
17436 | struct bpf_insn *insn_buf, | |
17437 | int *cnt) | |
17438 | { | |
17439 | struct btf_struct_meta *kptr_struct_meta = insn_aux->kptr_struct_meta; | |
17440 | struct bpf_insn addr[2] = { BPF_LD_IMM64(struct_meta_reg, (long)kptr_struct_meta) }; | |
17441 | ||
17442 | insn_buf[0] = addr[0]; | |
17443 | insn_buf[1] = addr[1]; | |
17444 | insn_buf[2] = BPF_MOV64_IMM(node_offset_reg, insn_aux->insert_off); | |
17445 | insn_buf[3] = *insn; | |
17446 | *cnt = 4; | |
17447 | } | |
17448 | ||
958cf2e2 KKD |
17449 | static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
17450 | struct bpf_insn *insn_buf, int insn_idx, int *cnt) | |
e6ac2450 MKL |
17451 | { |
17452 | const struct bpf_kfunc_desc *desc; | |
17453 | ||
a5d82727 KKD |
17454 | if (!insn->imm) { |
17455 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
17456 | return -EINVAL; | |
17457 | } | |
17458 | ||
3d76a4d3 SF |
17459 | *cnt = 0; |
17460 | ||
1cf3bfc6 IL |
17461 | /* insn->imm has the btf func_id. Replace it with an offset relative to |
17462 | * __bpf_call_base, unless the JIT needs to call functions that are | |
17463 | * further than 32 bits away (bpf_jit_supports_far_kfunc_call()). | |
e6ac2450 | 17464 | */ |
2357672c | 17465 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
17466 | if (!desc) { |
17467 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
17468 | insn->imm); | |
17469 | return -EFAULT; | |
17470 | } | |
17471 | ||
1cf3bfc6 IL |
17472 | if (!bpf_jit_supports_far_kfunc_call()) |
17473 | insn->imm = BPF_CALL_IMM(desc->addr); | |
958cf2e2 KKD |
17474 | if (insn->off) |
17475 | return 0; | |
17476 | if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
17477 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
17478 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
17479 | u64 obj_new_size = env->insn_aux_data[insn_idx].obj_new_size; | |
e6ac2450 | 17480 | |
958cf2e2 KKD |
17481 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_1, obj_new_size); |
17482 | insn_buf[1] = addr[0]; | |
17483 | insn_buf[2] = addr[1]; | |
17484 | insn_buf[3] = *insn; | |
17485 | *cnt = 4; | |
7c50b1cb DM |
17486 | } else if (desc->func_id == special_kfunc_list[KF_bpf_obj_drop_impl] || |
17487 | desc->func_id == special_kfunc_list[KF_bpf_refcount_acquire_impl]) { | |
ac9f0605 KKD |
17488 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; |
17489 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
17490 | ||
17491 | insn_buf[0] = addr[0]; | |
17492 | insn_buf[1] = addr[1]; | |
17493 | insn_buf[2] = *insn; | |
17494 | *cnt = 3; | |
d2dcc67d DM |
17495 | } else if (desc->func_id == special_kfunc_list[KF_bpf_list_push_back_impl] || |
17496 | desc->func_id == special_kfunc_list[KF_bpf_list_push_front_impl] || | |
17497 | desc->func_id == special_kfunc_list[KF_bpf_rbtree_add_impl]) { | |
17498 | int struct_meta_reg = BPF_REG_3; | |
17499 | int node_offset_reg = BPF_REG_4; | |
17500 | ||
17501 | /* rbtree_add has extra 'less' arg, so args-to-fixup are in diff regs */ | |
17502 | if (desc->func_id == special_kfunc_list[KF_bpf_rbtree_add_impl]) { | |
17503 | struct_meta_reg = BPF_REG_4; | |
17504 | node_offset_reg = BPF_REG_5; | |
17505 | } | |
17506 | ||
17507 | __fixup_collection_insert_kfunc(&env->insn_aux_data[insn_idx], struct_meta_reg, | |
17508 | node_offset_reg, insn, insn_buf, cnt); | |
a35b9af4 YS |
17509 | } else if (desc->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] || |
17510 | desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { | |
fd264ca0 YS |
17511 | insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1); |
17512 | *cnt = 1; | |
958cf2e2 | 17513 | } |
e6ac2450 MKL |
17514 | return 0; |
17515 | } | |
17516 | ||
e6ac5933 BJ |
17517 | /* Do various post-verification rewrites in a single program pass. |
17518 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 17519 | */ |
e6ac5933 | 17520 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 17521 | { |
79741b3b | 17522 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 17523 | enum bpf_attach_type eatype = prog->expected_attach_type; |
9b99edca | 17524 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 17525 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 17526 | const struct bpf_func_proto *fn; |
79741b3b | 17527 | const int insn_cnt = prog->len; |
09772d92 | 17528 | const struct bpf_map_ops *ops; |
c93552c4 | 17529 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
17530 | struct bpf_insn insn_buf[16]; |
17531 | struct bpf_prog *new_prog; | |
17532 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 17533 | int i, ret, cnt, delta = 0; |
e245c5c6 | 17534 | |
79741b3b | 17535 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 17536 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
17537 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
17538 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
17539 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 17540 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 17541 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
17542 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
17543 | struct bpf_insn *patchlet; | |
17544 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 17545 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
17546 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
17547 | BPF_JNE | BPF_K, insn->src_reg, | |
17548 | 0, 2, 0), | |
f6b1b3bf DB |
17549 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
17550 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
17551 | *insn, | |
17552 | }; | |
e88b2c6e | 17553 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 17554 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
17555 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
17556 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 17557 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 17558 | *insn, |
9b00f1b7 DB |
17559 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
17560 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 17561 | }; |
f6b1b3bf | 17562 | |
e88b2c6e DB |
17563 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
17564 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 17565 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
17566 | |
17567 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
17568 | if (!new_prog) |
17569 | return -ENOMEM; | |
17570 | ||
17571 | delta += cnt - 1; | |
17572 | env->prog = prog = new_prog; | |
17573 | insn = new_prog->insnsi + i + delta; | |
17574 | continue; | |
17575 | } | |
17576 | ||
e6ac5933 | 17577 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
17578 | if (BPF_CLASS(insn->code) == BPF_LD && |
17579 | (BPF_MODE(insn->code) == BPF_ABS || | |
17580 | BPF_MODE(insn->code) == BPF_IND)) { | |
17581 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
17582 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
17583 | verbose(env, "bpf verifier is misconfigured\n"); | |
17584 | return -EINVAL; | |
17585 | } | |
17586 | ||
17587 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17588 | if (!new_prog) | |
17589 | return -ENOMEM; | |
17590 | ||
17591 | delta += cnt - 1; | |
17592 | env->prog = prog = new_prog; | |
17593 | insn = new_prog->insnsi + i + delta; | |
17594 | continue; | |
17595 | } | |
17596 | ||
e6ac5933 | 17597 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
17598 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
17599 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
17600 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
17601 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 17602 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 17603 | bool issrc, isneg, isimm; |
979d63d5 DB |
17604 | u32 off_reg; |
17605 | ||
17606 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
17607 | if (!aux->alu_state || |
17608 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
17609 | continue; |
17610 | ||
17611 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
17612 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
17613 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 17614 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
17615 | |
17616 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
17617 | if (isimm) { |
17618 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
17619 | } else { | |
17620 | if (isneg) | |
17621 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
17622 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
17623 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
17624 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
17625 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
17626 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
17627 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
17628 | } | |
b9b34ddb DB |
17629 | if (!issrc) |
17630 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
17631 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
17632 | if (isneg) |
17633 | insn->code = insn->code == code_add ? | |
17634 | code_sub : code_add; | |
17635 | *patch++ = *insn; | |
801c6058 | 17636 | if (issrc && isneg && !isimm) |
979d63d5 DB |
17637 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
17638 | cnt = patch - insn_buf; | |
17639 | ||
17640 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17641 | if (!new_prog) | |
17642 | return -ENOMEM; | |
17643 | ||
17644 | delta += cnt - 1; | |
17645 | env->prog = prog = new_prog; | |
17646 | insn = new_prog->insnsi + i + delta; | |
17647 | continue; | |
17648 | } | |
17649 | ||
79741b3b AS |
17650 | if (insn->code != (BPF_JMP | BPF_CALL)) |
17651 | continue; | |
cc8b0b92 AS |
17652 | if (insn->src_reg == BPF_PSEUDO_CALL) |
17653 | continue; | |
e6ac2450 | 17654 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
958cf2e2 | 17655 | ret = fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt); |
e6ac2450 MKL |
17656 | if (ret) |
17657 | return ret; | |
958cf2e2 KKD |
17658 | if (cnt == 0) |
17659 | continue; | |
17660 | ||
17661 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17662 | if (!new_prog) | |
17663 | return -ENOMEM; | |
17664 | ||
17665 | delta += cnt - 1; | |
17666 | env->prog = prog = new_prog; | |
17667 | insn = new_prog->insnsi + i + delta; | |
e6ac2450 MKL |
17668 | continue; |
17669 | } | |
e245c5c6 | 17670 | |
79741b3b AS |
17671 | if (insn->imm == BPF_FUNC_get_route_realm) |
17672 | prog->dst_needed = 1; | |
17673 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
17674 | bpf_user_rnd_init_once(); | |
9802d865 JB |
17675 | if (insn->imm == BPF_FUNC_override_return) |
17676 | prog->kprobe_override = 1; | |
79741b3b | 17677 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
17678 | /* If we tail call into other programs, we |
17679 | * cannot make any assumptions since they can | |
17680 | * be replaced dynamically during runtime in | |
17681 | * the program array. | |
17682 | */ | |
17683 | prog->cb_access = 1; | |
e411901c MF |
17684 | if (!allow_tail_call_in_subprogs(env)) |
17685 | prog->aux->stack_depth = MAX_BPF_STACK; | |
17686 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 17687 | |
79741b3b | 17688 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 17689 | * conditional branch in the interpreter for every normal |
79741b3b AS |
17690 | * call and to prevent accidental JITing by JIT compiler |
17691 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 17692 | */ |
79741b3b | 17693 | insn->imm = 0; |
71189fa9 | 17694 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 17695 | |
c93552c4 | 17696 | aux = &env->insn_aux_data[i + delta]; |
d2a3b7c5 | 17697 | if (env->bpf_capable && !prog->blinding_requested && |
cc52d914 | 17698 | prog->jit_requested && |
d2e4c1e6 DB |
17699 | !bpf_map_key_poisoned(aux) && |
17700 | !bpf_map_ptr_poisoned(aux) && | |
17701 | !bpf_map_ptr_unpriv(aux)) { | |
17702 | struct bpf_jit_poke_descriptor desc = { | |
17703 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
17704 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
17705 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 17706 | .insn_idx = i + delta, |
d2e4c1e6 DB |
17707 | }; |
17708 | ||
17709 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
17710 | if (ret < 0) { | |
17711 | verbose(env, "adding tail call poke descriptor failed\n"); | |
17712 | return ret; | |
17713 | } | |
17714 | ||
17715 | insn->imm = ret + 1; | |
17716 | continue; | |
17717 | } | |
17718 | ||
c93552c4 DB |
17719 | if (!bpf_map_ptr_unpriv(aux)) |
17720 | continue; | |
17721 | ||
b2157399 AS |
17722 | /* instead of changing every JIT dealing with tail_call |
17723 | * emit two extra insns: | |
17724 | * if (index >= max_entries) goto out; | |
17725 | * index &= array->index_mask; | |
17726 | * to avoid out-of-bounds cpu speculation | |
17727 | */ | |
c93552c4 | 17728 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 17729 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
17730 | return -EINVAL; |
17731 | } | |
c93552c4 | 17732 | |
d2e4c1e6 | 17733 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
17734 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
17735 | map_ptr->max_entries, 2); | |
17736 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
17737 | container_of(map_ptr, | |
17738 | struct bpf_array, | |
17739 | map)->index_mask); | |
17740 | insn_buf[2] = *insn; | |
17741 | cnt = 3; | |
17742 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17743 | if (!new_prog) | |
17744 | return -ENOMEM; | |
17745 | ||
17746 | delta += cnt - 1; | |
17747 | env->prog = prog = new_prog; | |
17748 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
17749 | continue; |
17750 | } | |
e245c5c6 | 17751 | |
b00628b1 AS |
17752 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
17753 | /* The verifier will process callback_fn as many times as necessary | |
17754 | * with different maps and the register states prepared by | |
17755 | * set_timer_callback_state will be accurate. | |
17756 | * | |
17757 | * The following use case is valid: | |
17758 | * map1 is shared by prog1, prog2, prog3. | |
17759 | * prog1 calls bpf_timer_init for some map1 elements | |
17760 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
17761 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
17762 | * prog3 calls bpf_timer_start for some map1 elements. | |
17763 | * Those that were not both bpf_timer_init-ed and | |
17764 | * bpf_timer_set_callback-ed will return -EINVAL. | |
17765 | */ | |
17766 | struct bpf_insn ld_addrs[2] = { | |
17767 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
17768 | }; | |
17769 | ||
17770 | insn_buf[0] = ld_addrs[0]; | |
17771 | insn_buf[1] = ld_addrs[1]; | |
17772 | insn_buf[2] = *insn; | |
17773 | cnt = 3; | |
17774 | ||
17775 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17776 | if (!new_prog) | |
17777 | return -ENOMEM; | |
17778 | ||
17779 | delta += cnt - 1; | |
17780 | env->prog = prog = new_prog; | |
17781 | insn = new_prog->insnsi + i + delta; | |
17782 | goto patch_call_imm; | |
17783 | } | |
17784 | ||
9bb00b28 YS |
17785 | if (is_storage_get_function(insn->imm)) { |
17786 | if (!env->prog->aux->sleepable || | |
17787 | env->insn_aux_data[i + delta].storage_get_func_atomic) | |
d56c9fe6 | 17788 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); |
9bb00b28 YS |
17789 | else |
17790 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); | |
b00fa38a JK |
17791 | insn_buf[1] = *insn; |
17792 | cnt = 2; | |
17793 | ||
17794 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17795 | if (!new_prog) | |
17796 | return -ENOMEM; | |
17797 | ||
17798 | delta += cnt - 1; | |
17799 | env->prog = prog = new_prog; | |
17800 | insn = new_prog->insnsi + i + delta; | |
17801 | goto patch_call_imm; | |
17802 | } | |
17803 | ||
89c63074 | 17804 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
17805 | * and other inlining handlers are currently limited to 64 bit |
17806 | * only. | |
89c63074 | 17807 | */ |
60b58afc | 17808 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
17809 | (insn->imm == BPF_FUNC_map_lookup_elem || |
17810 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
17811 | insn->imm == BPF_FUNC_map_delete_elem || |
17812 | insn->imm == BPF_FUNC_map_push_elem || | |
17813 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 17814 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c | 17815 | insn->imm == BPF_FUNC_redirect_map || |
07343110 FZ |
17816 | insn->imm == BPF_FUNC_for_each_map_elem || |
17817 | insn->imm == BPF_FUNC_map_lookup_percpu_elem)) { | |
c93552c4 DB |
17818 | aux = &env->insn_aux_data[i + delta]; |
17819 | if (bpf_map_ptr_poisoned(aux)) | |
17820 | goto patch_call_imm; | |
17821 | ||
d2e4c1e6 | 17822 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
17823 | ops = map_ptr->ops; |
17824 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
17825 | ops->map_gen_lookup) { | |
17826 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
17827 | if (cnt == -EOPNOTSUPP) |
17828 | goto patch_map_ops_generic; | |
17829 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
17830 | verbose(env, "bpf verifier is misconfigured\n"); |
17831 | return -EINVAL; | |
17832 | } | |
81ed18ab | 17833 | |
09772d92 DB |
17834 | new_prog = bpf_patch_insn_data(env, i + delta, |
17835 | insn_buf, cnt); | |
17836 | if (!new_prog) | |
17837 | return -ENOMEM; | |
81ed18ab | 17838 | |
09772d92 DB |
17839 | delta += cnt - 1; |
17840 | env->prog = prog = new_prog; | |
17841 | insn = new_prog->insnsi + i + delta; | |
17842 | continue; | |
17843 | } | |
81ed18ab | 17844 | |
09772d92 DB |
17845 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
17846 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
17847 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
d7ba4cc9 | 17848 | (long (*)(struct bpf_map *map, void *key))NULL)); |
09772d92 | 17849 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
d7ba4cc9 | 17850 | (long (*)(struct bpf_map *map, void *key, void *value, |
09772d92 | 17851 | u64 flags))NULL)); |
84430d42 | 17852 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
d7ba4cc9 | 17853 | (long (*)(struct bpf_map *map, void *value, |
84430d42 DB |
17854 | u64 flags))NULL)); |
17855 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
d7ba4cc9 | 17856 | (long (*)(struct bpf_map *map, void *value))NULL)); |
84430d42 | 17857 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
d7ba4cc9 | 17858 | (long (*)(struct bpf_map *map, void *value))NULL)); |
e6a4750f | 17859 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
d7ba4cc9 | 17860 | (long (*)(struct bpf_map *map, u64 index, u64 flags))NULL)); |
0640c77c | 17861 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
d7ba4cc9 | 17862 | (long (*)(struct bpf_map *map, |
0640c77c AI |
17863 | bpf_callback_t callback_fn, |
17864 | void *callback_ctx, | |
17865 | u64 flags))NULL)); | |
07343110 FZ |
17866 | BUILD_BUG_ON(!__same_type(ops->map_lookup_percpu_elem, |
17867 | (void *(*)(struct bpf_map *map, void *key, u32 cpu))NULL)); | |
e6a4750f | 17868 | |
4a8f87e6 | 17869 | patch_map_ops_generic: |
09772d92 DB |
17870 | switch (insn->imm) { |
17871 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 17872 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
17873 | continue; |
17874 | case BPF_FUNC_map_update_elem: | |
3d717fad | 17875 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
17876 | continue; |
17877 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 17878 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 17879 | continue; |
84430d42 | 17880 | case BPF_FUNC_map_push_elem: |
3d717fad | 17881 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
17882 | continue; |
17883 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 17884 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
17885 | continue; |
17886 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 17887 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 17888 | continue; |
e6a4750f | 17889 | case BPF_FUNC_redirect_map: |
3d717fad | 17890 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 17891 | continue; |
0640c77c AI |
17892 | case BPF_FUNC_for_each_map_elem: |
17893 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 17894 | continue; |
07343110 FZ |
17895 | case BPF_FUNC_map_lookup_percpu_elem: |
17896 | insn->imm = BPF_CALL_IMM(ops->map_lookup_percpu_elem); | |
17897 | continue; | |
09772d92 | 17898 | } |
81ed18ab | 17899 | |
09772d92 | 17900 | goto patch_call_imm; |
81ed18ab AS |
17901 | } |
17902 | ||
e6ac5933 | 17903 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
17904 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
17905 | insn->imm == BPF_FUNC_jiffies64) { | |
17906 | struct bpf_insn ld_jiffies_addr[2] = { | |
17907 | BPF_LD_IMM64(BPF_REG_0, | |
17908 | (unsigned long)&jiffies), | |
17909 | }; | |
17910 | ||
17911 | insn_buf[0] = ld_jiffies_addr[0]; | |
17912 | insn_buf[1] = ld_jiffies_addr[1]; | |
17913 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
17914 | BPF_REG_0, 0); | |
17915 | cnt = 3; | |
17916 | ||
17917 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
17918 | cnt); | |
17919 | if (!new_prog) | |
17920 | return -ENOMEM; | |
17921 | ||
17922 | delta += cnt - 1; | |
17923 | env->prog = prog = new_prog; | |
17924 | insn = new_prog->insnsi + i + delta; | |
17925 | continue; | |
17926 | } | |
17927 | ||
f92c1e18 JO |
17928 | /* Implement bpf_get_func_arg inline. */ |
17929 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17930 | insn->imm == BPF_FUNC_get_func_arg) { | |
17931 | /* Load nr_args from ctx - 8 */ | |
17932 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17933 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
17934 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
17935 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
17936 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
17937 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
17938 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
17939 | insn_buf[7] = BPF_JMP_A(1); | |
17940 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
17941 | cnt = 9; | |
17942 | ||
17943 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17944 | if (!new_prog) | |
17945 | return -ENOMEM; | |
17946 | ||
17947 | delta += cnt - 1; | |
17948 | env->prog = prog = new_prog; | |
17949 | insn = new_prog->insnsi + i + delta; | |
17950 | continue; | |
17951 | } | |
17952 | ||
17953 | /* Implement bpf_get_func_ret inline. */ | |
17954 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17955 | insn->imm == BPF_FUNC_get_func_ret) { | |
17956 | if (eatype == BPF_TRACE_FEXIT || | |
17957 | eatype == BPF_MODIFY_RETURN) { | |
17958 | /* Load nr_args from ctx - 8 */ | |
17959 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17960 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
17961 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
17962 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
17963 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
17964 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
17965 | cnt = 6; | |
17966 | } else { | |
17967 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
17968 | cnt = 1; | |
17969 | } | |
17970 | ||
17971 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
17972 | if (!new_prog) | |
17973 | return -ENOMEM; | |
17974 | ||
17975 | delta += cnt - 1; | |
17976 | env->prog = prog = new_prog; | |
17977 | insn = new_prog->insnsi + i + delta; | |
17978 | continue; | |
17979 | } | |
17980 | ||
17981 | /* Implement get_func_arg_cnt inline. */ | |
17982 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
17983 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
17984 | /* Load nr_args from ctx - 8 */ | |
17985 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
17986 | ||
17987 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
17988 | if (!new_prog) | |
17989 | return -ENOMEM; | |
17990 | ||
17991 | env->prog = prog = new_prog; | |
17992 | insn = new_prog->insnsi + i + delta; | |
17993 | continue; | |
17994 | } | |
17995 | ||
f705ec76 | 17996 | /* Implement bpf_get_func_ip inline. */ |
9b99edca JO |
17997 | if (prog_type == BPF_PROG_TYPE_TRACING && |
17998 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
17999 | /* Load IP address from ctx - 16 */ |
18000 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
18001 | |
18002 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
18003 | if (!new_prog) | |
18004 | return -ENOMEM; | |
18005 | ||
18006 | env->prog = prog = new_prog; | |
18007 | insn = new_prog->insnsi + i + delta; | |
18008 | continue; | |
18009 | } | |
18010 | ||
81ed18ab | 18011 | patch_call_imm: |
5e43f899 | 18012 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
18013 | /* all functions that have prototype and verifier allowed |
18014 | * programs to call them, must be real in-kernel functions | |
18015 | */ | |
18016 | if (!fn->func) { | |
61bd5218 JK |
18017 | verbose(env, |
18018 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
18019 | func_id_name(insn->imm), insn->imm); |
18020 | return -EFAULT; | |
e245c5c6 | 18021 | } |
79741b3b | 18022 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 18023 | } |
e245c5c6 | 18024 | |
d2e4c1e6 DB |
18025 | /* Since poke tab is now finalized, publish aux to tracker. */ |
18026 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
18027 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
18028 | if (!map_ptr->ops->map_poke_track || | |
18029 | !map_ptr->ops->map_poke_untrack || | |
18030 | !map_ptr->ops->map_poke_run) { | |
18031 | verbose(env, "bpf verifier is misconfigured\n"); | |
18032 | return -EINVAL; | |
18033 | } | |
18034 | ||
18035 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
18036 | if (ret < 0) { | |
18037 | verbose(env, "tracking tail call prog failed\n"); | |
18038 | return ret; | |
18039 | } | |
18040 | } | |
18041 | ||
1cf3bfc6 | 18042 | sort_kfunc_descs_by_imm_off(env->prog); |
e6ac2450 | 18043 | |
79741b3b AS |
18044 | return 0; |
18045 | } | |
e245c5c6 | 18046 | |
1ade2371 EZ |
18047 | static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env, |
18048 | int position, | |
18049 | s32 stack_base, | |
18050 | u32 callback_subprogno, | |
18051 | u32 *cnt) | |
18052 | { | |
18053 | s32 r6_offset = stack_base + 0 * BPF_REG_SIZE; | |
18054 | s32 r7_offset = stack_base + 1 * BPF_REG_SIZE; | |
18055 | s32 r8_offset = stack_base + 2 * BPF_REG_SIZE; | |
18056 | int reg_loop_max = BPF_REG_6; | |
18057 | int reg_loop_cnt = BPF_REG_7; | |
18058 | int reg_loop_ctx = BPF_REG_8; | |
18059 | ||
18060 | struct bpf_prog *new_prog; | |
18061 | u32 callback_start; | |
18062 | u32 call_insn_offset; | |
18063 | s32 callback_offset; | |
18064 | ||
18065 | /* This represents an inlined version of bpf_iter.c:bpf_loop, | |
18066 | * be careful to modify this code in sync. | |
18067 | */ | |
18068 | struct bpf_insn insn_buf[] = { | |
18069 | /* Return error and jump to the end of the patch if | |
18070 | * expected number of iterations is too big. | |
18071 | */ | |
18072 | BPF_JMP_IMM(BPF_JLE, BPF_REG_1, BPF_MAX_LOOPS, 2), | |
18073 | BPF_MOV32_IMM(BPF_REG_0, -E2BIG), | |
18074 | BPF_JMP_IMM(BPF_JA, 0, 0, 16), | |
18075 | /* spill R6, R7, R8 to use these as loop vars */ | |
18076 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, r6_offset), | |
18077 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_7, r7_offset), | |
18078 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_8, r8_offset), | |
18079 | /* initialize loop vars */ | |
18080 | BPF_MOV64_REG(reg_loop_max, BPF_REG_1), | |
18081 | BPF_MOV32_IMM(reg_loop_cnt, 0), | |
18082 | BPF_MOV64_REG(reg_loop_ctx, BPF_REG_3), | |
18083 | /* loop header, | |
18084 | * if reg_loop_cnt >= reg_loop_max skip the loop body | |
18085 | */ | |
18086 | BPF_JMP_REG(BPF_JGE, reg_loop_cnt, reg_loop_max, 5), | |
18087 | /* callback call, | |
18088 | * correct callback offset would be set after patching | |
18089 | */ | |
18090 | BPF_MOV64_REG(BPF_REG_1, reg_loop_cnt), | |
18091 | BPF_MOV64_REG(BPF_REG_2, reg_loop_ctx), | |
18092 | BPF_CALL_REL(0), | |
18093 | /* increment loop counter */ | |
18094 | BPF_ALU64_IMM(BPF_ADD, reg_loop_cnt, 1), | |
18095 | /* jump to loop header if callback returned 0 */ | |
18096 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -6), | |
18097 | /* return value of bpf_loop, | |
18098 | * set R0 to the number of iterations | |
18099 | */ | |
18100 | BPF_MOV64_REG(BPF_REG_0, reg_loop_cnt), | |
18101 | /* restore original values of R6, R7, R8 */ | |
18102 | BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, r6_offset), | |
18103 | BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, r7_offset), | |
18104 | BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_10, r8_offset), | |
18105 | }; | |
18106 | ||
18107 | *cnt = ARRAY_SIZE(insn_buf); | |
18108 | new_prog = bpf_patch_insn_data(env, position, insn_buf, *cnt); | |
18109 | if (!new_prog) | |
18110 | return new_prog; | |
18111 | ||
18112 | /* callback start is known only after patching */ | |
18113 | callback_start = env->subprog_info[callback_subprogno].start; | |
18114 | /* Note: insn_buf[12] is an offset of BPF_CALL_REL instruction */ | |
18115 | call_insn_offset = position + 12; | |
18116 | callback_offset = callback_start - call_insn_offset - 1; | |
fb4e3b33 | 18117 | new_prog->insnsi[call_insn_offset].imm = callback_offset; |
1ade2371 EZ |
18118 | |
18119 | return new_prog; | |
18120 | } | |
18121 | ||
18122 | static bool is_bpf_loop_call(struct bpf_insn *insn) | |
18123 | { | |
18124 | return insn->code == (BPF_JMP | BPF_CALL) && | |
18125 | insn->src_reg == 0 && | |
18126 | insn->imm == BPF_FUNC_loop; | |
18127 | } | |
18128 | ||
18129 | /* For all sub-programs in the program (including main) check | |
18130 | * insn_aux_data to see if there are bpf_loop calls that require | |
18131 | * inlining. If such calls are found the calls are replaced with a | |
18132 | * sequence of instructions produced by `inline_bpf_loop` function and | |
18133 | * subprog stack_depth is increased by the size of 3 registers. | |
18134 | * This stack space is used to spill values of the R6, R7, R8. These | |
18135 | * registers are used to store the loop bound, counter and context | |
18136 | * variables. | |
18137 | */ | |
18138 | static int optimize_bpf_loop(struct bpf_verifier_env *env) | |
18139 | { | |
18140 | struct bpf_subprog_info *subprogs = env->subprog_info; | |
18141 | int i, cur_subprog = 0, cnt, delta = 0; | |
18142 | struct bpf_insn *insn = env->prog->insnsi; | |
18143 | int insn_cnt = env->prog->len; | |
18144 | u16 stack_depth = subprogs[cur_subprog].stack_depth; | |
18145 | u16 stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
18146 | u16 stack_depth_extra = 0; | |
18147 | ||
18148 | for (i = 0; i < insn_cnt; i++, insn++) { | |
18149 | struct bpf_loop_inline_state *inline_state = | |
18150 | &env->insn_aux_data[i + delta].loop_inline_state; | |
18151 | ||
18152 | if (is_bpf_loop_call(insn) && inline_state->fit_for_inline) { | |
18153 | struct bpf_prog *new_prog; | |
18154 | ||
18155 | stack_depth_extra = BPF_REG_SIZE * 3 + stack_depth_roundup; | |
18156 | new_prog = inline_bpf_loop(env, | |
18157 | i + delta, | |
18158 | -(stack_depth + stack_depth_extra), | |
18159 | inline_state->callback_subprogno, | |
18160 | &cnt); | |
18161 | if (!new_prog) | |
18162 | return -ENOMEM; | |
18163 | ||
18164 | delta += cnt - 1; | |
18165 | env->prog = new_prog; | |
18166 | insn = new_prog->insnsi + i + delta; | |
18167 | } | |
18168 | ||
18169 | if (subprogs[cur_subprog + 1].start == i + delta + 1) { | |
18170 | subprogs[cur_subprog].stack_depth += stack_depth_extra; | |
18171 | cur_subprog++; | |
18172 | stack_depth = subprogs[cur_subprog].stack_depth; | |
18173 | stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
18174 | stack_depth_extra = 0; | |
18175 | } | |
18176 | } | |
18177 | ||
18178 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
18179 | ||
18180 | return 0; | |
18181 | } | |
18182 | ||
58e2af8b | 18183 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 18184 | { |
58e2af8b | 18185 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
18186 | int i; |
18187 | ||
9f4686c4 AS |
18188 | sl = env->free_list; |
18189 | while (sl) { | |
18190 | sln = sl->next; | |
18191 | free_verifier_state(&sl->state, false); | |
18192 | kfree(sl); | |
18193 | sl = sln; | |
18194 | } | |
51c39bb1 | 18195 | env->free_list = NULL; |
9f4686c4 | 18196 | |
f1bca824 AS |
18197 | if (!env->explored_states) |
18198 | return; | |
18199 | ||
dc2a4ebc | 18200 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
18201 | sl = env->explored_states[i]; |
18202 | ||
a8f500af AS |
18203 | while (sl) { |
18204 | sln = sl->next; | |
18205 | free_verifier_state(&sl->state, false); | |
18206 | kfree(sl); | |
18207 | sl = sln; | |
18208 | } | |
51c39bb1 | 18209 | env->explored_states[i] = NULL; |
f1bca824 | 18210 | } |
51c39bb1 | 18211 | } |
f1bca824 | 18212 | |
51c39bb1 AS |
18213 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
18214 | { | |
6f8a57cc | 18215 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
18216 | struct bpf_verifier_state *state; |
18217 | struct bpf_reg_state *regs; | |
18218 | int ret, i; | |
18219 | ||
18220 | env->prev_linfo = NULL; | |
18221 | env->pass_cnt++; | |
18222 | ||
18223 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
18224 | if (!state) | |
18225 | return -ENOMEM; | |
18226 | state->curframe = 0; | |
18227 | state->speculative = false; | |
18228 | state->branches = 1; | |
18229 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
18230 | if (!state->frame[0]) { | |
18231 | kfree(state); | |
18232 | return -ENOMEM; | |
18233 | } | |
18234 | env->cur_state = state; | |
18235 | init_func_state(env, state->frame[0], | |
18236 | BPF_MAIN_FUNC /* callsite */, | |
18237 | 0 /* frameno */, | |
18238 | subprog); | |
be2ef816 AN |
18239 | state->first_insn_idx = env->subprog_info[subprog].start; |
18240 | state->last_insn_idx = -1; | |
51c39bb1 AS |
18241 | |
18242 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 18243 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
18244 | ret = btf_prepare_func_args(env, subprog, regs); |
18245 | if (ret) | |
18246 | goto out; | |
18247 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
18248 | if (regs[i].type == PTR_TO_CTX) | |
18249 | mark_reg_known_zero(env, regs, i); | |
18250 | else if (regs[i].type == SCALAR_VALUE) | |
18251 | mark_reg_unknown(env, regs, i); | |
cf9f2f8d | 18252 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
e5069b9c DB |
18253 | const u32 mem_size = regs[i].mem_size; |
18254 | ||
18255 | mark_reg_known_zero(env, regs, i); | |
18256 | regs[i].mem_size = mem_size; | |
18257 | regs[i].id = ++env->id_gen; | |
18258 | } | |
51c39bb1 AS |
18259 | } |
18260 | } else { | |
18261 | /* 1st arg to a function */ | |
18262 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
18263 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 18264 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
18265 | if (ret == -EFAULT) |
18266 | /* unlikely verifier bug. abort. | |
18267 | * ret == 0 and ret < 0 are sadly acceptable for | |
18268 | * main() function due to backward compatibility. | |
18269 | * Like socket filter program may be written as: | |
18270 | * int bpf_prog(struct pt_regs *ctx) | |
18271 | * and never dereference that ctx in the program. | |
18272 | * 'struct pt_regs' is a type mismatch for socket | |
18273 | * filter that should be using 'struct __sk_buff'. | |
18274 | */ | |
18275 | goto out; | |
18276 | } | |
18277 | ||
18278 | ret = do_check(env); | |
18279 | out: | |
f59bbfc2 AS |
18280 | /* check for NULL is necessary, since cur_state can be freed inside |
18281 | * do_check() under memory pressure. | |
18282 | */ | |
18283 | if (env->cur_state) { | |
18284 | free_verifier_state(env->cur_state, true); | |
18285 | env->cur_state = NULL; | |
18286 | } | |
6f8a57cc AN |
18287 | while (!pop_stack(env, NULL, NULL, false)); |
18288 | if (!ret && pop_log) | |
18289 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 18290 | free_states(env); |
51c39bb1 AS |
18291 | return ret; |
18292 | } | |
18293 | ||
18294 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
18295 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
18296 | * Consider: | |
18297 | * int bar(int); | |
18298 | * int foo(int f) | |
18299 | * { | |
18300 | * return bar(f); | |
18301 | * } | |
18302 | * int bar(int b) | |
18303 | * { | |
18304 | * ... | |
18305 | * } | |
18306 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
18307 | * will be assumed that bar() already verified successfully and call to bar() | |
18308 | * from foo() will be checked for type match only. Later bar() will be verified | |
18309 | * independently to check that it's safe for R1=any_scalar_value. | |
18310 | */ | |
18311 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
18312 | { | |
18313 | struct bpf_prog_aux *aux = env->prog->aux; | |
18314 | int i, ret; | |
18315 | ||
18316 | if (!aux->func_info) | |
18317 | return 0; | |
18318 | ||
18319 | for (i = 1; i < env->subprog_cnt; i++) { | |
18320 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
18321 | continue; | |
18322 | env->insn_idx = env->subprog_info[i].start; | |
18323 | WARN_ON_ONCE(env->insn_idx == 0); | |
18324 | ret = do_check_common(env, i); | |
18325 | if (ret) { | |
18326 | return ret; | |
18327 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
18328 | verbose(env, | |
18329 | "Func#%d is safe for any args that match its prototype\n", | |
18330 | i); | |
18331 | } | |
18332 | } | |
18333 | return 0; | |
18334 | } | |
18335 | ||
18336 | static int do_check_main(struct bpf_verifier_env *env) | |
18337 | { | |
18338 | int ret; | |
18339 | ||
18340 | env->insn_idx = 0; | |
18341 | ret = do_check_common(env, 0); | |
18342 | if (!ret) | |
18343 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
18344 | return ret; | |
18345 | } | |
18346 | ||
18347 | ||
06ee7115 AS |
18348 | static void print_verification_stats(struct bpf_verifier_env *env) |
18349 | { | |
18350 | int i; | |
18351 | ||
18352 | if (env->log.level & BPF_LOG_STATS) { | |
18353 | verbose(env, "verification time %lld usec\n", | |
18354 | div_u64(env->verification_time, 1000)); | |
18355 | verbose(env, "stack depth "); | |
18356 | for (i = 0; i < env->subprog_cnt; i++) { | |
18357 | u32 depth = env->subprog_info[i].stack_depth; | |
18358 | ||
18359 | verbose(env, "%d", depth); | |
18360 | if (i + 1 < env->subprog_cnt) | |
18361 | verbose(env, "+"); | |
18362 | } | |
18363 | verbose(env, "\n"); | |
18364 | } | |
18365 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
18366 | "total_states %d peak_states %d mark_read %d\n", | |
18367 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
18368 | env->max_states_per_insn, env->total_states, | |
18369 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
18370 | } |
18371 | ||
27ae7997 MKL |
18372 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
18373 | { | |
18374 | const struct btf_type *t, *func_proto; | |
18375 | const struct bpf_struct_ops *st_ops; | |
18376 | const struct btf_member *member; | |
18377 | struct bpf_prog *prog = env->prog; | |
18378 | u32 btf_id, member_idx; | |
18379 | const char *mname; | |
18380 | ||
12aa8a94 THJ |
18381 | if (!prog->gpl_compatible) { |
18382 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
18383 | return -EINVAL; | |
18384 | } | |
18385 | ||
27ae7997 MKL |
18386 | btf_id = prog->aux->attach_btf_id; |
18387 | st_ops = bpf_struct_ops_find(btf_id); | |
18388 | if (!st_ops) { | |
18389 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
18390 | btf_id); | |
18391 | return -ENOTSUPP; | |
18392 | } | |
18393 | ||
18394 | t = st_ops->type; | |
18395 | member_idx = prog->expected_attach_type; | |
18396 | if (member_idx >= btf_type_vlen(t)) { | |
18397 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
18398 | member_idx, st_ops->name); | |
18399 | return -EINVAL; | |
18400 | } | |
18401 | ||
18402 | member = &btf_type_member(t)[member_idx]; | |
18403 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
18404 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
18405 | NULL); | |
18406 | if (!func_proto) { | |
18407 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
18408 | mname, member_idx, st_ops->name); | |
18409 | return -EINVAL; | |
18410 | } | |
18411 | ||
18412 | if (st_ops->check_member) { | |
51a52a29 | 18413 | int err = st_ops->check_member(t, member, prog); |
27ae7997 MKL |
18414 | |
18415 | if (err) { | |
18416 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
18417 | mname, st_ops->name); | |
18418 | return err; | |
18419 | } | |
18420 | } | |
18421 | ||
18422 | prog->aux->attach_func_proto = func_proto; | |
18423 | prog->aux->attach_func_name = mname; | |
18424 | env->ops = st_ops->verifier_ops; | |
18425 | ||
18426 | return 0; | |
18427 | } | |
6ba43b76 KS |
18428 | #define SECURITY_PREFIX "security_" |
18429 | ||
f7b12b6f | 18430 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 18431 | { |
69191754 | 18432 | if (within_error_injection_list(addr) || |
f7b12b6f | 18433 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 18434 | return 0; |
6ba43b76 | 18435 | |
6ba43b76 KS |
18436 | return -EINVAL; |
18437 | } | |
27ae7997 | 18438 | |
1e6c62a8 AS |
18439 | /* list of non-sleepable functions that are otherwise on |
18440 | * ALLOW_ERROR_INJECTION list | |
18441 | */ | |
18442 | BTF_SET_START(btf_non_sleepable_error_inject) | |
18443 | /* Three functions below can be called from sleepable and non-sleepable context. | |
18444 | * Assume non-sleepable from bpf safety point of view. | |
18445 | */ | |
9dd3d069 | 18446 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
18447 | BTF_ID(func, should_fail_alloc_page) |
18448 | BTF_ID(func, should_failslab) | |
18449 | BTF_SET_END(btf_non_sleepable_error_inject) | |
18450 | ||
18451 | static int check_non_sleepable_error_inject(u32 btf_id) | |
18452 | { | |
18453 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
18454 | } | |
18455 | ||
f7b12b6f THJ |
18456 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
18457 | const struct bpf_prog *prog, | |
18458 | const struct bpf_prog *tgt_prog, | |
18459 | u32 btf_id, | |
18460 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 18461 | { |
be8704ff | 18462 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 18463 | const char prefix[] = "btf_trace_"; |
5b92a28a | 18464 | int ret = 0, subprog = -1, i; |
38207291 | 18465 | const struct btf_type *t; |
5b92a28a | 18466 | bool conservative = true; |
38207291 | 18467 | const char *tname; |
5b92a28a | 18468 | struct btf *btf; |
f7b12b6f | 18469 | long addr = 0; |
31bf1dbc | 18470 | struct module *mod = NULL; |
38207291 | 18471 | |
f1b9509c | 18472 | if (!btf_id) { |
efc68158 | 18473 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
18474 | return -EINVAL; |
18475 | } | |
22dc4a0f | 18476 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 18477 | if (!btf) { |
efc68158 | 18478 | bpf_log(log, |
5b92a28a AS |
18479 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
18480 | return -EINVAL; | |
18481 | } | |
18482 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 18483 | if (!t) { |
efc68158 | 18484 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
18485 | return -EINVAL; |
18486 | } | |
5b92a28a | 18487 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 18488 | if (!tname) { |
efc68158 | 18489 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
18490 | return -EINVAL; |
18491 | } | |
5b92a28a AS |
18492 | if (tgt_prog) { |
18493 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
18494 | ||
fd7c211d THJ |
18495 | if (bpf_prog_is_dev_bound(prog->aux) && |
18496 | !bpf_prog_dev_bound_match(prog, tgt_prog)) { | |
18497 | bpf_log(log, "Target program bound device mismatch"); | |
3d76a4d3 SF |
18498 | return -EINVAL; |
18499 | } | |
18500 | ||
5b92a28a AS |
18501 | for (i = 0; i < aux->func_info_cnt; i++) |
18502 | if (aux->func_info[i].type_id == btf_id) { | |
18503 | subprog = i; | |
18504 | break; | |
18505 | } | |
18506 | if (subprog == -1) { | |
efc68158 | 18507 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
18508 | return -EINVAL; |
18509 | } | |
18510 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
18511 | if (prog_extension) { |
18512 | if (conservative) { | |
efc68158 | 18513 | bpf_log(log, |
be8704ff AS |
18514 | "Cannot replace static functions\n"); |
18515 | return -EINVAL; | |
18516 | } | |
18517 | if (!prog->jit_requested) { | |
efc68158 | 18518 | bpf_log(log, |
be8704ff AS |
18519 | "Extension programs should be JITed\n"); |
18520 | return -EINVAL; | |
18521 | } | |
be8704ff AS |
18522 | } |
18523 | if (!tgt_prog->jited) { | |
efc68158 | 18524 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
18525 | return -EINVAL; |
18526 | } | |
18527 | if (tgt_prog->type == prog->type) { | |
18528 | /* Cannot fentry/fexit another fentry/fexit program. | |
18529 | * Cannot attach program extension to another extension. | |
18530 | * It's ok to attach fentry/fexit to extension program. | |
18531 | */ | |
efc68158 | 18532 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
18533 | return -EINVAL; |
18534 | } | |
18535 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
18536 | prog_extension && | |
18537 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
18538 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
18539 | /* Program extensions can extend all program types | |
18540 | * except fentry/fexit. The reason is the following. | |
18541 | * The fentry/fexit programs are used for performance | |
18542 | * analysis, stats and can be attached to any program | |
18543 | * type except themselves. When extension program is | |
18544 | * replacing XDP function it is necessary to allow | |
18545 | * performance analysis of all functions. Both original | |
18546 | * XDP program and its program extension. Hence | |
18547 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
18548 | * allowed. If extending of fentry/fexit was allowed it | |
18549 | * would be possible to create long call chain | |
18550 | * fentry->extension->fentry->extension beyond | |
18551 | * reasonable stack size. Hence extending fentry is not | |
18552 | * allowed. | |
18553 | */ | |
efc68158 | 18554 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
18555 | return -EINVAL; |
18556 | } | |
5b92a28a | 18557 | } else { |
be8704ff | 18558 | if (prog_extension) { |
efc68158 | 18559 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
18560 | return -EINVAL; |
18561 | } | |
5b92a28a | 18562 | } |
f1b9509c AS |
18563 | |
18564 | switch (prog->expected_attach_type) { | |
18565 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 18566 | if (tgt_prog) { |
efc68158 | 18567 | bpf_log(log, |
5b92a28a AS |
18568 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
18569 | return -EINVAL; | |
18570 | } | |
38207291 | 18571 | if (!btf_type_is_typedef(t)) { |
efc68158 | 18572 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
18573 | btf_id); |
18574 | return -EINVAL; | |
18575 | } | |
f1b9509c | 18576 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 18577 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
18578 | btf_id, tname); |
18579 | return -EINVAL; | |
18580 | } | |
18581 | tname += sizeof(prefix) - 1; | |
5b92a28a | 18582 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
18583 | if (!btf_type_is_ptr(t)) |
18584 | /* should never happen in valid vmlinux build */ | |
18585 | return -EINVAL; | |
5b92a28a | 18586 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
18587 | if (!btf_type_is_func_proto(t)) |
18588 | /* should never happen in valid vmlinux build */ | |
18589 | return -EINVAL; | |
18590 | ||
f7b12b6f | 18591 | break; |
15d83c4d YS |
18592 | case BPF_TRACE_ITER: |
18593 | if (!btf_type_is_func(t)) { | |
efc68158 | 18594 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
18595 | btf_id); |
18596 | return -EINVAL; | |
18597 | } | |
18598 | t = btf_type_by_id(btf, t->type); | |
18599 | if (!btf_type_is_func_proto(t)) | |
18600 | return -EINVAL; | |
f7b12b6f THJ |
18601 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
18602 | if (ret) | |
18603 | return ret; | |
18604 | break; | |
be8704ff AS |
18605 | default: |
18606 | if (!prog_extension) | |
18607 | return -EINVAL; | |
df561f66 | 18608 | fallthrough; |
ae240823 | 18609 | case BPF_MODIFY_RETURN: |
9e4e01df | 18610 | case BPF_LSM_MAC: |
69fd337a | 18611 | case BPF_LSM_CGROUP: |
fec56f58 AS |
18612 | case BPF_TRACE_FENTRY: |
18613 | case BPF_TRACE_FEXIT: | |
18614 | if (!btf_type_is_func(t)) { | |
efc68158 | 18615 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
18616 | btf_id); |
18617 | return -EINVAL; | |
18618 | } | |
be8704ff | 18619 | if (prog_extension && |
efc68158 | 18620 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 18621 | return -EINVAL; |
5b92a28a | 18622 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
18623 | if (!btf_type_is_func_proto(t)) |
18624 | return -EINVAL; | |
f7b12b6f | 18625 | |
4a1e7c0c THJ |
18626 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
18627 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
18628 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
18629 | return -EINVAL; | |
18630 | ||
f7b12b6f | 18631 | if (tgt_prog && conservative) |
5b92a28a | 18632 | t = NULL; |
f7b12b6f THJ |
18633 | |
18634 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 18635 | if (ret < 0) |
f7b12b6f THJ |
18636 | return ret; |
18637 | ||
5b92a28a | 18638 | if (tgt_prog) { |
e9eeec58 YS |
18639 | if (subprog == 0) |
18640 | addr = (long) tgt_prog->bpf_func; | |
18641 | else | |
18642 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a | 18643 | } else { |
31bf1dbc VM |
18644 | if (btf_is_module(btf)) { |
18645 | mod = btf_try_get_module(btf); | |
18646 | if (mod) | |
18647 | addr = find_kallsyms_symbol_value(mod, tname); | |
18648 | else | |
18649 | addr = 0; | |
18650 | } else { | |
18651 | addr = kallsyms_lookup_name(tname); | |
18652 | } | |
5b92a28a | 18653 | if (!addr) { |
31bf1dbc | 18654 | module_put(mod); |
efc68158 | 18655 | bpf_log(log, |
5b92a28a AS |
18656 | "The address of function %s cannot be found\n", |
18657 | tname); | |
f7b12b6f | 18658 | return -ENOENT; |
5b92a28a | 18659 | } |
fec56f58 | 18660 | } |
18644cec | 18661 | |
1e6c62a8 AS |
18662 | if (prog->aux->sleepable) { |
18663 | ret = -EINVAL; | |
18664 | switch (prog->type) { | |
18665 | case BPF_PROG_TYPE_TRACING: | |
5b481aca BT |
18666 | |
18667 | /* fentry/fexit/fmod_ret progs can be sleepable if they are | |
1e6c62a8 AS |
18668 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
18669 | */ | |
18670 | if (!check_non_sleepable_error_inject(btf_id) && | |
18671 | within_error_injection_list(addr)) | |
18672 | ret = 0; | |
5b481aca BT |
18673 | /* fentry/fexit/fmod_ret progs can also be sleepable if they are |
18674 | * in the fmodret id set with the KF_SLEEPABLE flag. | |
18675 | */ | |
18676 | else { | |
18677 | u32 *flags = btf_kfunc_is_modify_return(btf, btf_id); | |
18678 | ||
18679 | if (flags && (*flags & KF_SLEEPABLE)) | |
18680 | ret = 0; | |
18681 | } | |
1e6c62a8 AS |
18682 | break; |
18683 | case BPF_PROG_TYPE_LSM: | |
18684 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
18685 | * Only some of them are sleepable. | |
18686 | */ | |
423f1610 | 18687 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
18688 | ret = 0; |
18689 | break; | |
18690 | default: | |
18691 | break; | |
18692 | } | |
f7b12b6f | 18693 | if (ret) { |
31bf1dbc | 18694 | module_put(mod); |
f7b12b6f THJ |
18695 | bpf_log(log, "%s is not sleepable\n", tname); |
18696 | return ret; | |
18697 | } | |
1e6c62a8 | 18698 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 18699 | if (tgt_prog) { |
31bf1dbc | 18700 | module_put(mod); |
efc68158 | 18701 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
18702 | return -EINVAL; |
18703 | } | |
5b481aca BT |
18704 | ret = -EINVAL; |
18705 | if (btf_kfunc_is_modify_return(btf, btf_id) || | |
18706 | !check_attach_modify_return(addr, tname)) | |
18707 | ret = 0; | |
f7b12b6f | 18708 | if (ret) { |
31bf1dbc | 18709 | module_put(mod); |
f7b12b6f THJ |
18710 | bpf_log(log, "%s() is not modifiable\n", tname); |
18711 | return ret; | |
1af9270e | 18712 | } |
18644cec | 18713 | } |
f7b12b6f THJ |
18714 | |
18715 | break; | |
18716 | } | |
18717 | tgt_info->tgt_addr = addr; | |
18718 | tgt_info->tgt_name = tname; | |
18719 | tgt_info->tgt_type = t; | |
31bf1dbc | 18720 | tgt_info->tgt_mod = mod; |
f7b12b6f THJ |
18721 | return 0; |
18722 | } | |
18723 | ||
35e3815f JO |
18724 | BTF_SET_START(btf_id_deny) |
18725 | BTF_ID_UNUSED | |
18726 | #ifdef CONFIG_SMP | |
18727 | BTF_ID(func, migrate_disable) | |
18728 | BTF_ID(func, migrate_enable) | |
18729 | #endif | |
18730 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
18731 | BTF_ID(func, rcu_read_unlock_strict) | |
18732 | #endif | |
c11bd046 Y |
18733 | #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE) |
18734 | BTF_ID(func, preempt_count_add) | |
18735 | BTF_ID(func, preempt_count_sub) | |
18736 | #endif | |
a0c109dc YS |
18737 | #ifdef CONFIG_PREEMPT_RCU |
18738 | BTF_ID(func, __rcu_read_lock) | |
18739 | BTF_ID(func, __rcu_read_unlock) | |
18740 | #endif | |
35e3815f JO |
18741 | BTF_SET_END(btf_id_deny) |
18742 | ||
700e6f85 JO |
18743 | static bool can_be_sleepable(struct bpf_prog *prog) |
18744 | { | |
18745 | if (prog->type == BPF_PROG_TYPE_TRACING) { | |
18746 | switch (prog->expected_attach_type) { | |
18747 | case BPF_TRACE_FENTRY: | |
18748 | case BPF_TRACE_FEXIT: | |
18749 | case BPF_MODIFY_RETURN: | |
18750 | case BPF_TRACE_ITER: | |
18751 | return true; | |
18752 | default: | |
18753 | return false; | |
18754 | } | |
18755 | } | |
18756 | return prog->type == BPF_PROG_TYPE_LSM || | |
1e12d3ef DV |
18757 | prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ || |
18758 | prog->type == BPF_PROG_TYPE_STRUCT_OPS; | |
700e6f85 JO |
18759 | } |
18760 | ||
f7b12b6f THJ |
18761 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
18762 | { | |
18763 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 18764 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
18765 | struct bpf_attach_target_info tgt_info = {}; |
18766 | u32 btf_id = prog->aux->attach_btf_id; | |
18767 | struct bpf_trampoline *tr; | |
18768 | int ret; | |
18769 | u64 key; | |
18770 | ||
79a7f8bd AS |
18771 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
18772 | if (prog->aux->sleepable) | |
18773 | /* attach_btf_id checked to be zero already */ | |
18774 | return 0; | |
18775 | verbose(env, "Syscall programs can only be sleepable\n"); | |
18776 | return -EINVAL; | |
18777 | } | |
18778 | ||
700e6f85 | 18779 | if (prog->aux->sleepable && !can_be_sleepable(prog)) { |
1e12d3ef | 18780 | verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n"); |
f7b12b6f THJ |
18781 | return -EINVAL; |
18782 | } | |
18783 | ||
18784 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
18785 | return check_struct_ops_btf_id(env); | |
18786 | ||
18787 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
18788 | prog->type != BPF_PROG_TYPE_LSM && | |
18789 | prog->type != BPF_PROG_TYPE_EXT) | |
18790 | return 0; | |
18791 | ||
18792 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
18793 | if (ret) | |
fec56f58 | 18794 | return ret; |
f7b12b6f THJ |
18795 | |
18796 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
18797 | /* to make freplace equivalent to their targets, they need to |
18798 | * inherit env->ops and expected_attach_type for the rest of the | |
18799 | * verification | |
18800 | */ | |
f7b12b6f THJ |
18801 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
18802 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
18803 | } | |
18804 | ||
18805 | /* store info about the attachment target that will be used later */ | |
18806 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
18807 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
31bf1dbc | 18808 | prog->aux->mod = tgt_info.tgt_mod; |
f7b12b6f | 18809 | |
4a1e7c0c THJ |
18810 | if (tgt_prog) { |
18811 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
18812 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
18813 | } | |
18814 | ||
f7b12b6f THJ |
18815 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
18816 | prog->aux->attach_btf_trace = true; | |
18817 | return 0; | |
18818 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
18819 | if (!bpf_iter_prog_supported(prog)) | |
18820 | return -EINVAL; | |
18821 | return 0; | |
18822 | } | |
18823 | ||
18824 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
18825 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
18826 | if (ret < 0) | |
18827 | return ret; | |
35e3815f JO |
18828 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
18829 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
18830 | return -EINVAL; | |
38207291 | 18831 | } |
f7b12b6f | 18832 | |
22dc4a0f | 18833 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
18834 | tr = bpf_trampoline_get(key, &tgt_info); |
18835 | if (!tr) | |
18836 | return -ENOMEM; | |
18837 | ||
3aac1ead | 18838 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 18839 | return 0; |
38207291 MKL |
18840 | } |
18841 | ||
76654e67 AM |
18842 | struct btf *bpf_get_btf_vmlinux(void) |
18843 | { | |
18844 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
18845 | mutex_lock(&bpf_verifier_lock); | |
18846 | if (!btf_vmlinux) | |
18847 | btf_vmlinux = btf_parse_vmlinux(); | |
18848 | mutex_unlock(&bpf_verifier_lock); | |
18849 | } | |
18850 | return btf_vmlinux; | |
18851 | } | |
18852 | ||
47a71c1f | 18853 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size) |
51580e79 | 18854 | { |
06ee7115 | 18855 | u64 start_time = ktime_get_ns(); |
58e2af8b | 18856 | struct bpf_verifier_env *env; |
bdcab414 AN |
18857 | int i, len, ret = -EINVAL, err; |
18858 | u32 log_true_size; | |
e2ae4ca2 | 18859 | bool is_priv; |
51580e79 | 18860 | |
eba0c929 AB |
18861 | /* no program is valid */ |
18862 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
18863 | return -EINVAL; | |
18864 | ||
58e2af8b | 18865 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
18866 | * allocate/free it every time bpf_check() is called |
18867 | */ | |
58e2af8b | 18868 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
18869 | if (!env) |
18870 | return -ENOMEM; | |
18871 | ||
9e4c24e7 | 18872 | len = (*prog)->len; |
fad953ce | 18873 | env->insn_aux_data = |
9e4c24e7 | 18874 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
18875 | ret = -ENOMEM; |
18876 | if (!env->insn_aux_data) | |
18877 | goto err_free_env; | |
9e4c24e7 JK |
18878 | for (i = 0; i < len; i++) |
18879 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 18880 | env->prog = *prog; |
00176a34 | 18881 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 18882 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 18883 | is_priv = bpf_capable(); |
0246e64d | 18884 | |
76654e67 | 18885 | bpf_get_btf_vmlinux(); |
8580ac94 | 18886 | |
cbd35700 | 18887 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
18888 | if (!is_priv) |
18889 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 | 18890 | |
bdcab414 AN |
18891 | /* user could have requested verbose verifier output |
18892 | * and supplied buffer to store the verification trace | |
18893 | */ | |
18894 | ret = bpf_vlog_init(&env->log, attr->log_level, | |
18895 | (char __user *) (unsigned long) attr->log_buf, | |
18896 | attr->log_size); | |
18897 | if (ret) | |
18898 | goto err_unlock; | |
1ad2f583 | 18899 | |
0f55f9ed CL |
18900 | mark_verifier_state_clean(env); |
18901 | ||
8580ac94 AS |
18902 | if (IS_ERR(btf_vmlinux)) { |
18903 | /* Either gcc or pahole or kernel are broken. */ | |
18904 | verbose(env, "in-kernel BTF is malformed\n"); | |
18905 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 18906 | goto skip_full_check; |
8580ac94 AS |
18907 | } |
18908 | ||
1ad2f583 DB |
18909 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
18910 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 18911 | env->strict_alignment = true; |
e9ee9efc DM |
18912 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
18913 | env->strict_alignment = false; | |
cbd35700 | 18914 | |
2c78ee89 | 18915 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 18916 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
2c78ee89 AS |
18917 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
18918 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
18919 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 18920 | |
10d274e8 AS |
18921 | if (is_priv) |
18922 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
18923 | ||
dc2a4ebc | 18924 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 18925 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
18926 | GFP_USER); |
18927 | ret = -ENOMEM; | |
18928 | if (!env->explored_states) | |
18929 | goto skip_full_check; | |
18930 | ||
e6ac2450 MKL |
18931 | ret = add_subprog_and_kfunc(env); |
18932 | if (ret < 0) | |
18933 | goto skip_full_check; | |
18934 | ||
d9762e84 | 18935 | ret = check_subprogs(env); |
475fb78f AS |
18936 | if (ret < 0) |
18937 | goto skip_full_check; | |
18938 | ||
c454a46b | 18939 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
18940 | if (ret < 0) |
18941 | goto skip_full_check; | |
18942 | ||
be8704ff AS |
18943 | ret = check_attach_btf_id(env); |
18944 | if (ret) | |
18945 | goto skip_full_check; | |
18946 | ||
4976b718 HL |
18947 | ret = resolve_pseudo_ldimm64(env); |
18948 | if (ret < 0) | |
18949 | goto skip_full_check; | |
18950 | ||
9d03ebc7 | 18951 | if (bpf_prog_is_offloaded(env->prog->aux)) { |
ceb11679 YZ |
18952 | ret = bpf_prog_offload_verifier_prep(env->prog); |
18953 | if (ret) | |
18954 | goto skip_full_check; | |
18955 | } | |
18956 | ||
d9762e84 MKL |
18957 | ret = check_cfg(env); |
18958 | if (ret < 0) | |
18959 | goto skip_full_check; | |
18960 | ||
51c39bb1 AS |
18961 | ret = do_check_subprogs(env); |
18962 | ret = ret ?: do_check_main(env); | |
cbd35700 | 18963 | |
9d03ebc7 | 18964 | if (ret == 0 && bpf_prog_is_offloaded(env->prog->aux)) |
c941ce9c QM |
18965 | ret = bpf_prog_offload_finalize(env); |
18966 | ||
0246e64d | 18967 | skip_full_check: |
51c39bb1 | 18968 | kvfree(env->explored_states); |
0246e64d | 18969 | |
c131187d | 18970 | if (ret == 0) |
9b38c405 | 18971 | ret = check_max_stack_depth(env); |
c131187d | 18972 | |
9b38c405 | 18973 | /* instruction rewrites happen after this point */ |
1ade2371 EZ |
18974 | if (ret == 0) |
18975 | ret = optimize_bpf_loop(env); | |
18976 | ||
e2ae4ca2 JK |
18977 | if (is_priv) { |
18978 | if (ret == 0) | |
18979 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
18980 | if (ret == 0) |
18981 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
18982 | if (ret == 0) |
18983 | ret = opt_remove_nops(env); | |
52875a04 JK |
18984 | } else { |
18985 | if (ret == 0) | |
18986 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
18987 | } |
18988 | ||
9bac3d6d AS |
18989 | if (ret == 0) |
18990 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
18991 | ret = convert_ctx_accesses(env); | |
18992 | ||
e245c5c6 | 18993 | if (ret == 0) |
e6ac5933 | 18994 | ret = do_misc_fixups(env); |
e245c5c6 | 18995 | |
a4b1d3c1 JW |
18996 | /* do 32-bit optimization after insn patching has done so those patched |
18997 | * insns could be handled correctly. | |
18998 | */ | |
9d03ebc7 | 18999 | if (ret == 0 && !bpf_prog_is_offloaded(env->prog->aux)) { |
d6c2308c JW |
19000 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
19001 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
19002 | : false; | |
a4b1d3c1 JW |
19003 | } |
19004 | ||
1ea47e01 AS |
19005 | if (ret == 0) |
19006 | ret = fixup_call_args(env); | |
19007 | ||
06ee7115 AS |
19008 | env->verification_time = ktime_get_ns() - start_time; |
19009 | print_verification_stats(env); | |
aba64c7d | 19010 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 19011 | |
bdcab414 AN |
19012 | /* preserve original error even if log finalization is successful */ |
19013 | err = bpf_vlog_finalize(&env->log, &log_true_size); | |
19014 | if (err) | |
19015 | ret = err; | |
19016 | ||
47a71c1f AN |
19017 | if (uattr_size >= offsetofend(union bpf_attr, log_true_size) && |
19018 | copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), | |
bdcab414 | 19019 | &log_true_size, sizeof(log_true_size))) { |
47a71c1f AN |
19020 | ret = -EFAULT; |
19021 | goto err_release_maps; | |
19022 | } | |
cbd35700 | 19023 | |
541c3bad AN |
19024 | if (ret) |
19025 | goto err_release_maps; | |
19026 | ||
19027 | if (env->used_map_cnt) { | |
0246e64d | 19028 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
19029 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
19030 | sizeof(env->used_maps[0]), | |
19031 | GFP_KERNEL); | |
0246e64d | 19032 | |
9bac3d6d | 19033 | if (!env->prog->aux->used_maps) { |
0246e64d | 19034 | ret = -ENOMEM; |
a2a7d570 | 19035 | goto err_release_maps; |
0246e64d AS |
19036 | } |
19037 | ||
9bac3d6d | 19038 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 19039 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 19040 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
19041 | } |
19042 | if (env->used_btf_cnt) { | |
19043 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
19044 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
19045 | sizeof(env->used_btfs[0]), | |
19046 | GFP_KERNEL); | |
19047 | if (!env->prog->aux->used_btfs) { | |
19048 | ret = -ENOMEM; | |
19049 | goto err_release_maps; | |
19050 | } | |
0246e64d | 19051 | |
541c3bad AN |
19052 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
19053 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
19054 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
19055 | } | |
19056 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
19057 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
19058 | * bpf_ld_imm64 instructions | |
19059 | */ | |
19060 | convert_pseudo_ld_imm64(env); | |
19061 | } | |
cbd35700 | 19062 | |
541c3bad | 19063 | adjust_btf_func(env); |
ba64e7d8 | 19064 | |
a2a7d570 | 19065 | err_release_maps: |
9bac3d6d | 19066 | if (!env->prog->aux->used_maps) |
0246e64d | 19067 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 19068 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
19069 | */ |
19070 | release_maps(env); | |
541c3bad AN |
19071 | if (!env->prog->aux->used_btfs) |
19072 | release_btfs(env); | |
03f87c0b THJ |
19073 | |
19074 | /* extension progs temporarily inherit the attach_type of their targets | |
19075 | for verification purposes, so set it back to zero before returning | |
19076 | */ | |
19077 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
19078 | env->prog->expected_attach_type = 0; | |
19079 | ||
9bac3d6d | 19080 | *prog = env->prog; |
3df126f3 | 19081 | err_unlock: |
45a73c17 AS |
19082 | if (!is_priv) |
19083 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
19084 | vfree(env->insn_aux_data); |
19085 | err_free_env: | |
19086 | kfree(env); | |
51580e79 AS |
19087 | return ret; |
19088 | } |