Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
51580e79 | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
969bf05e | 3 | * Copyright (c) 2016 Facebook |
fd978bf7 | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
51580e79 | 5 | */ |
838e9690 | 6 | #include <uapi/linux/btf.h> |
aef2feda | 7 | #include <linux/bpf-cgroup.h> |
51580e79 AS |
8 | #include <linux/kernel.h> |
9 | #include <linux/types.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/bpf.h> | |
838e9690 | 12 | #include <linux/btf.h> |
58e2af8b | 13 | #include <linux/bpf_verifier.h> |
51580e79 AS |
14 | #include <linux/filter.h> |
15 | #include <net/netlink.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/vmalloc.h> | |
ebb676da | 18 | #include <linux/stringify.h> |
cc8b0b92 AS |
19 | #include <linux/bsearch.h> |
20 | #include <linux/sort.h> | |
c195651e | 21 | #include <linux/perf_event.h> |
d9762e84 | 22 | #include <linux/ctype.h> |
6ba43b76 | 23 | #include <linux/error-injection.h> |
9e4e01df | 24 | #include <linux/bpf_lsm.h> |
1e6c62a8 | 25 | #include <linux/btf_ids.h> |
47e34cb7 | 26 | #include <linux/poison.h> |
51580e79 | 27 | |
f4ac7e0b JK |
28 | #include "disasm.h" |
29 | ||
00176a34 | 30 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 31 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
32 | [_id] = & _name ## _verifier_ops, |
33 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 34 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
35 | #include <linux/bpf_types.h> |
36 | #undef BPF_PROG_TYPE | |
37 | #undef BPF_MAP_TYPE | |
f2e10bff | 38 | #undef BPF_LINK_TYPE |
00176a34 JK |
39 | }; |
40 | ||
51580e79 AS |
41 | /* bpf_check() is a static code analyzer that walks eBPF program |
42 | * instruction by instruction and updates register/stack state. | |
43 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
44 | * | |
45 | * The first pass is depth-first-search to check that the program is a DAG. | |
46 | * It rejects the following programs: | |
47 | * - larger than BPF_MAXINSNS insns | |
48 | * - if loop is present (detected via back-edge) | |
49 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
50 | * - out of bounds or malformed jumps | |
51 | * The second pass is all possible path descent from the 1st insn. | |
8fb33b60 | 52 | * Since it's analyzing all paths through the program, the length of the |
eba38a96 | 53 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
54 | * insn is less then 4K, but there are too many branches that change stack/regs. |
55 | * Number of 'branches to be analyzed' is limited to 1k | |
56 | * | |
57 | * On entry to each instruction, each register has a type, and the instruction | |
58 | * changes the types of the registers depending on instruction semantics. | |
59 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
60 | * copied to R1. | |
61 | * | |
62 | * All registers are 64-bit. | |
63 | * R0 - return register | |
64 | * R1-R5 argument passing registers | |
65 | * R6-R9 callee saved registers | |
66 | * R10 - frame pointer read-only | |
67 | * | |
68 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
69 | * and has type PTR_TO_CTX. | |
70 | * | |
71 | * Verifier tracks arithmetic operations on pointers in case: | |
72 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
73 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
74 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
75 | * and 2nd arithmetic instruction is pattern matched to recognize | |
76 | * that it wants to construct a pointer to some element within stack. | |
77 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
78 | * (and -20 constant is saved for further stack bounds checking). | |
79 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
80 | * | |
f1174f77 | 81 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 82 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 83 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
84 | * |
85 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
86 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
87 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
88 | * |
89 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
90 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
91 | * | |
92 | * registers used to pass values to function calls are checked against | |
93 | * function argument constraints. | |
94 | * | |
95 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
96 | * It means that the register type passed to this function must be | |
97 | * PTR_TO_STACK and it will be used inside the function as | |
98 | * 'pointer to map element key' | |
99 | * | |
100 | * For example the argument constraints for bpf_map_lookup_elem(): | |
101 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
102 | * .arg1_type = ARG_CONST_MAP_PTR, | |
103 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
104 | * | |
105 | * ret_type says that this function returns 'pointer to map elem value or null' | |
106 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
107 | * 2nd argument should be a pointer to stack, which will be used inside | |
108 | * the helper function as a pointer to map element key. | |
109 | * | |
110 | * On the kernel side the helper function looks like: | |
111 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
112 | * { | |
113 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
114 | * void *key = (void *) (unsigned long) r2; | |
115 | * void *value; | |
116 | * | |
117 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
118 | * [key, key + map->key_size) bytes are valid and were initialized on | |
119 | * the stack of eBPF program. | |
120 | * } | |
121 | * | |
122 | * Corresponding eBPF program may look like: | |
123 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
124 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
125 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
126 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
127 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
128 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
129 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
130 | * | |
131 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
132 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
133 | * and were initialized prior to this call. | |
134 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
135 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
136 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
8fb33b60 | 137 | * returns either pointer to map value or NULL. |
51580e79 AS |
138 | * |
139 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
140 | * insn, the register holding that pointer in the true branch changes state to | |
141 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
142 | * branch. See check_cond_jmp_op(). | |
143 | * | |
144 | * After the call R0 is set to return type of the function and registers R1-R5 | |
145 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
146 | * |
147 | * The following reference types represent a potential reference to a kernel | |
148 | * resource which, after first being allocated, must be checked and freed by | |
149 | * the BPF program: | |
150 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
151 | * | |
152 | * When the verifier sees a helper call return a reference type, it allocates a | |
153 | * pointer id for the reference and stores it in the current function state. | |
154 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
155 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
156 | * passes through a NULL-check conditional. For the branch wherein the state is | |
157 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
158 | * |
159 | * For each helper function that allocates a reference, such as | |
160 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
161 | * bpf_sk_release(). When a reference type passes into the release function, | |
162 | * the verifier also releases the reference. If any unchecked or unreleased | |
163 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
164 | */ |
165 | ||
17a52670 | 166 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 167 | struct bpf_verifier_stack_elem { |
17a52670 AS |
168 | /* verifer state is 'st' |
169 | * before processing instruction 'insn_idx' | |
170 | * and after processing instruction 'prev_insn_idx' | |
171 | */ | |
58e2af8b | 172 | struct bpf_verifier_state st; |
17a52670 AS |
173 | int insn_idx; |
174 | int prev_insn_idx; | |
58e2af8b | 175 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
176 | /* length of verifier log at the time this state was pushed on stack */ |
177 | u32 log_pos; | |
cbd35700 AS |
178 | }; |
179 | ||
b285fcb7 | 180 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 181 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 182 | |
d2e4c1e6 DB |
183 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
184 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
185 | ||
c93552c4 DB |
186 | #define BPF_MAP_PTR_UNPRIV 1UL |
187 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
188 | POISON_POINTER_DELTA)) | |
189 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
190 | ||
bc34dee6 JK |
191 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx); |
192 | static int release_reference(struct bpf_verifier_env *env, int ref_obj_id); | |
193 | ||
c93552c4 DB |
194 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
195 | { | |
d2e4c1e6 | 196 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
197 | } |
198 | ||
199 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
200 | { | |
d2e4c1e6 | 201 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
202 | } |
203 | ||
204 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
205 | const struct bpf_map *map, bool unpriv) | |
206 | { | |
207 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
208 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
209 | aux->map_ptr_state = (unsigned long)map | |
210 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
211 | } | |
212 | ||
213 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
214 | { | |
215 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
216 | } | |
217 | ||
218 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
219 | { | |
220 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
221 | } | |
222 | ||
223 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
224 | { | |
225 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
226 | } | |
227 | ||
228 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
229 | { | |
230 | bool poisoned = bpf_map_key_poisoned(aux); | |
231 | ||
232 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
233 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 234 | } |
fad73a1a | 235 | |
23a2d70c YS |
236 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
237 | { | |
238 | return insn->code == (BPF_JMP | BPF_CALL) && | |
239 | insn->src_reg == BPF_PSEUDO_CALL; | |
240 | } | |
241 | ||
e6ac2450 MKL |
242 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
243 | { | |
244 | return insn->code == (BPF_JMP | BPF_CALL) && | |
245 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; | |
246 | } | |
247 | ||
33ff9823 DB |
248 | struct bpf_call_arg_meta { |
249 | struct bpf_map *map_ptr; | |
435faee1 | 250 | bool raw_mode; |
36bbef52 | 251 | bool pkt_access; |
8f14852e | 252 | u8 release_regno; |
435faee1 DB |
253 | int regno; |
254 | int access_size; | |
457f4436 | 255 | int mem_size; |
10060503 | 256 | u64 msize_max_value; |
1b986589 | 257 | int ref_obj_id; |
3e8ce298 | 258 | int map_uid; |
d83525ca | 259 | int func_id; |
22dc4a0f | 260 | struct btf *btf; |
eaa6bcb7 | 261 | u32 btf_id; |
22dc4a0f | 262 | struct btf *ret_btf; |
eaa6bcb7 | 263 | u32 ret_btf_id; |
69c087ba | 264 | u32 subprogno; |
aa3496ac | 265 | struct btf_field *kptr_field; |
97e03f52 | 266 | u8 uninit_dynptr_regno; |
33ff9823 DB |
267 | }; |
268 | ||
8580ac94 AS |
269 | struct btf *btf_vmlinux; |
270 | ||
cbd35700 AS |
271 | static DEFINE_MUTEX(bpf_verifier_lock); |
272 | ||
d9762e84 MKL |
273 | static const struct bpf_line_info * |
274 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
275 | { | |
276 | const struct bpf_line_info *linfo; | |
277 | const struct bpf_prog *prog; | |
278 | u32 i, nr_linfo; | |
279 | ||
280 | prog = env->prog; | |
281 | nr_linfo = prog->aux->nr_linfo; | |
282 | ||
283 | if (!nr_linfo || insn_off >= prog->len) | |
284 | return NULL; | |
285 | ||
286 | linfo = prog->aux->linfo; | |
287 | for (i = 1; i < nr_linfo; i++) | |
288 | if (insn_off < linfo[i].insn_off) | |
289 | break; | |
290 | ||
291 | return &linfo[i - 1]; | |
292 | } | |
293 | ||
77d2e05a MKL |
294 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
295 | va_list args) | |
cbd35700 | 296 | { |
a2a7d570 | 297 | unsigned int n; |
cbd35700 | 298 | |
a2a7d570 | 299 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
300 | |
301 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
302 | "verifier log line truncated - local buffer too short\n"); | |
303 | ||
8580ac94 | 304 | if (log->level == BPF_LOG_KERNEL) { |
436d404c HT |
305 | bool newline = n > 0 && log->kbuf[n - 1] == '\n'; |
306 | ||
307 | pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); | |
8580ac94 AS |
308 | return; |
309 | } | |
436d404c HT |
310 | |
311 | n = min(log->len_total - log->len_used - 1, n); | |
312 | log->kbuf[n] = '\0'; | |
a2a7d570 JK |
313 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
314 | log->len_used += n; | |
315 | else | |
316 | log->ubuf = NULL; | |
cbd35700 | 317 | } |
abe08840 | 318 | |
6f8a57cc AN |
319 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
320 | { | |
321 | char zero = 0; | |
322 | ||
323 | if (!bpf_verifier_log_needed(log)) | |
324 | return; | |
325 | ||
326 | log->len_used = new_pos; | |
327 | if (put_user(zero, log->ubuf + new_pos)) | |
328 | log->ubuf = NULL; | |
329 | } | |
330 | ||
abe08840 JO |
331 | /* log_level controls verbosity level of eBPF verifier. |
332 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
333 | * so the user can figure out what's wrong with the program | |
430e68d1 | 334 | */ |
abe08840 JO |
335 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
336 | const char *fmt, ...) | |
337 | { | |
338 | va_list args; | |
339 | ||
77d2e05a MKL |
340 | if (!bpf_verifier_log_needed(&env->log)) |
341 | return; | |
342 | ||
abe08840 | 343 | va_start(args, fmt); |
77d2e05a | 344 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
345 | va_end(args); |
346 | } | |
347 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
348 | ||
349 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
350 | { | |
77d2e05a | 351 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
352 | va_list args; |
353 | ||
77d2e05a MKL |
354 | if (!bpf_verifier_log_needed(&env->log)) |
355 | return; | |
356 | ||
abe08840 | 357 | va_start(args, fmt); |
77d2e05a | 358 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
359 | va_end(args); |
360 | } | |
cbd35700 | 361 | |
9e15db66 AS |
362 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
363 | const char *fmt, ...) | |
364 | { | |
365 | va_list args; | |
366 | ||
367 | if (!bpf_verifier_log_needed(log)) | |
368 | return; | |
369 | ||
370 | va_start(args, fmt); | |
371 | bpf_verifier_vlog(log, fmt, args); | |
372 | va_end(args); | |
373 | } | |
84c6ac41 | 374 | EXPORT_SYMBOL_GPL(bpf_log); |
9e15db66 | 375 | |
d9762e84 MKL |
376 | static const char *ltrim(const char *s) |
377 | { | |
378 | while (isspace(*s)) | |
379 | s++; | |
380 | ||
381 | return s; | |
382 | } | |
383 | ||
384 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
385 | u32 insn_off, | |
386 | const char *prefix_fmt, ...) | |
387 | { | |
388 | const struct bpf_line_info *linfo; | |
389 | ||
390 | if (!bpf_verifier_log_needed(&env->log)) | |
391 | return; | |
392 | ||
393 | linfo = find_linfo(env, insn_off); | |
394 | if (!linfo || linfo == env->prev_linfo) | |
395 | return; | |
396 | ||
397 | if (prefix_fmt) { | |
398 | va_list args; | |
399 | ||
400 | va_start(args, prefix_fmt); | |
401 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
402 | va_end(args); | |
403 | } | |
404 | ||
405 | verbose(env, "%s\n", | |
406 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
407 | linfo->line_off))); | |
408 | ||
409 | env->prev_linfo = linfo; | |
410 | } | |
411 | ||
bc2591d6 YS |
412 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
413 | struct bpf_reg_state *reg, | |
414 | struct tnum *range, const char *ctx, | |
415 | const char *reg_name) | |
416 | { | |
417 | char tn_buf[48]; | |
418 | ||
419 | verbose(env, "At %s the register %s ", ctx, reg_name); | |
420 | if (!tnum_is_unknown(reg->var_off)) { | |
421 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
422 | verbose(env, "has value %s", tn_buf); | |
423 | } else { | |
424 | verbose(env, "has unknown scalar value"); | |
425 | } | |
426 | tnum_strn(tn_buf, sizeof(tn_buf), *range); | |
427 | verbose(env, " should have been in %s\n", tn_buf); | |
428 | } | |
429 | ||
de8f3a83 DB |
430 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
431 | { | |
0c9a7a7e | 432 | type = base_type(type); |
de8f3a83 DB |
433 | return type == PTR_TO_PACKET || |
434 | type == PTR_TO_PACKET_META; | |
435 | } | |
436 | ||
46f8bc92 MKL |
437 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
438 | { | |
439 | return type == PTR_TO_SOCKET || | |
655a51e5 | 440 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
441 | type == PTR_TO_TCP_SOCK || |
442 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
443 | } |
444 | ||
cac616db JF |
445 | static bool reg_type_not_null(enum bpf_reg_type type) |
446 | { | |
447 | return type == PTR_TO_SOCKET || | |
448 | type == PTR_TO_TCP_SOCK || | |
449 | type == PTR_TO_MAP_VALUE || | |
69c087ba | 450 | type == PTR_TO_MAP_KEY || |
01c66c48 | 451 | type == PTR_TO_SOCK_COMMON; |
cac616db JF |
452 | } |
453 | ||
4e814da0 KKD |
454 | static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg) |
455 | { | |
456 | struct btf_record *rec = NULL; | |
457 | struct btf_struct_meta *meta; | |
458 | ||
459 | if (reg->type == PTR_TO_MAP_VALUE) { | |
460 | rec = reg->map_ptr->record; | |
461 | } else if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
462 | meta = btf_find_struct_meta(reg->btf, reg->btf_id); | |
463 | if (meta) | |
464 | rec = meta->record; | |
465 | } | |
466 | return rec; | |
467 | } | |
468 | ||
d83525ca AS |
469 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
470 | { | |
4e814da0 | 471 | return btf_record_has_field(reg_btf_record(reg), BPF_SPIN_LOCK); |
cba368c1 MKL |
472 | } |
473 | ||
20b2aff4 HL |
474 | static bool type_is_rdonly_mem(u32 type) |
475 | { | |
476 | return type & MEM_RDONLY; | |
cba368c1 MKL |
477 | } |
478 | ||
48946bd6 | 479 | static bool type_may_be_null(u32 type) |
fd1b0d60 | 480 | { |
48946bd6 | 481 | return type & PTR_MAYBE_NULL; |
fd1b0d60 LB |
482 | } |
483 | ||
64d85290 JS |
484 | static bool is_acquire_function(enum bpf_func_id func_id, |
485 | const struct bpf_map *map) | |
486 | { | |
487 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
488 | ||
489 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
490 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 | 491 | func_id == BPF_FUNC_skc_lookup_tcp || |
c0a5a21c KKD |
492 | func_id == BPF_FUNC_ringbuf_reserve || |
493 | func_id == BPF_FUNC_kptr_xchg) | |
64d85290 JS |
494 | return true; |
495 | ||
496 | if (func_id == BPF_FUNC_map_lookup_elem && | |
497 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
498 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
499 | return true; | |
500 | ||
501 | return false; | |
46f8bc92 MKL |
502 | } |
503 | ||
1b986589 MKL |
504 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
505 | { | |
506 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
507 | func_id == BPF_FUNC_sk_fullsock || |
508 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
509 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
510 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
3bc253c2 | 511 | func_id == BPF_FUNC_skc_to_mptcp_sock || |
1df8f55a MKL |
512 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
513 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
514 | } |
515 | ||
88374342 | 516 | static bool is_dynptr_ref_function(enum bpf_func_id func_id) |
b2d8ef19 DM |
517 | { |
518 | return func_id == BPF_FUNC_dynptr_data; | |
519 | } | |
520 | ||
be2ef816 AN |
521 | static bool is_callback_calling_function(enum bpf_func_id func_id) |
522 | { | |
523 | return func_id == BPF_FUNC_for_each_map_elem || | |
524 | func_id == BPF_FUNC_timer_set_callback || | |
525 | func_id == BPF_FUNC_find_vma || | |
526 | func_id == BPF_FUNC_loop || | |
527 | func_id == BPF_FUNC_user_ringbuf_drain; | |
528 | } | |
529 | ||
9bb00b28 YS |
530 | static bool is_storage_get_function(enum bpf_func_id func_id) |
531 | { | |
532 | return func_id == BPF_FUNC_sk_storage_get || | |
533 | func_id == BPF_FUNC_inode_storage_get || | |
534 | func_id == BPF_FUNC_task_storage_get || | |
535 | func_id == BPF_FUNC_cgrp_storage_get; | |
536 | } | |
537 | ||
b2d8ef19 DM |
538 | static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id, |
539 | const struct bpf_map *map) | |
540 | { | |
541 | int ref_obj_uses = 0; | |
542 | ||
543 | if (is_ptr_cast_function(func_id)) | |
544 | ref_obj_uses++; | |
545 | if (is_acquire_function(func_id, map)) | |
546 | ref_obj_uses++; | |
88374342 | 547 | if (is_dynptr_ref_function(func_id)) |
b2d8ef19 DM |
548 | ref_obj_uses++; |
549 | ||
550 | return ref_obj_uses > 1; | |
551 | } | |
552 | ||
39491867 BJ |
553 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
554 | { | |
555 | return BPF_CLASS(insn->code) == BPF_STX && | |
556 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
557 | insn->imm == BPF_CMPXCHG; | |
558 | } | |
559 | ||
c25b2ae1 HL |
560 | /* string representation of 'enum bpf_reg_type' |
561 | * | |
562 | * Note that reg_type_str() can not appear more than once in a single verbose() | |
563 | * statement. | |
564 | */ | |
565 | static const char *reg_type_str(struct bpf_verifier_env *env, | |
566 | enum bpf_reg_type type) | |
567 | { | |
ef66c547 | 568 | char postfix[16] = {0}, prefix[64] = {0}; |
c25b2ae1 HL |
569 | static const char * const str[] = { |
570 | [NOT_INIT] = "?", | |
7df5072c | 571 | [SCALAR_VALUE] = "scalar", |
c25b2ae1 HL |
572 | [PTR_TO_CTX] = "ctx", |
573 | [CONST_PTR_TO_MAP] = "map_ptr", | |
574 | [PTR_TO_MAP_VALUE] = "map_value", | |
575 | [PTR_TO_STACK] = "fp", | |
576 | [PTR_TO_PACKET] = "pkt", | |
577 | [PTR_TO_PACKET_META] = "pkt_meta", | |
578 | [PTR_TO_PACKET_END] = "pkt_end", | |
579 | [PTR_TO_FLOW_KEYS] = "flow_keys", | |
580 | [PTR_TO_SOCKET] = "sock", | |
581 | [PTR_TO_SOCK_COMMON] = "sock_common", | |
582 | [PTR_TO_TCP_SOCK] = "tcp_sock", | |
583 | [PTR_TO_TP_BUFFER] = "tp_buffer", | |
584 | [PTR_TO_XDP_SOCK] = "xdp_sock", | |
585 | [PTR_TO_BTF_ID] = "ptr_", | |
c25b2ae1 | 586 | [PTR_TO_MEM] = "mem", |
20b2aff4 | 587 | [PTR_TO_BUF] = "buf", |
c25b2ae1 HL |
588 | [PTR_TO_FUNC] = "func", |
589 | [PTR_TO_MAP_KEY] = "map_key", | |
20571567 | 590 | [PTR_TO_DYNPTR] = "dynptr_ptr", |
c25b2ae1 HL |
591 | }; |
592 | ||
593 | if (type & PTR_MAYBE_NULL) { | |
5844101a | 594 | if (base_type(type) == PTR_TO_BTF_ID) |
c25b2ae1 HL |
595 | strncpy(postfix, "or_null_", 16); |
596 | else | |
597 | strncpy(postfix, "_or_null", 16); | |
598 | } | |
599 | ||
9bb00b28 | 600 | snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s", |
ef66c547 DV |
601 | type & MEM_RDONLY ? "rdonly_" : "", |
602 | type & MEM_RINGBUF ? "ringbuf_" : "", | |
603 | type & MEM_USER ? "user_" : "", | |
604 | type & MEM_PERCPU ? "percpu_" : "", | |
9bb00b28 | 605 | type & MEM_RCU ? "rcu_" : "", |
3f00c523 DV |
606 | type & PTR_UNTRUSTED ? "untrusted_" : "", |
607 | type & PTR_TRUSTED ? "trusted_" : "" | |
ef66c547 | 608 | ); |
20b2aff4 HL |
609 | |
610 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", | |
611 | prefix, str[base_type(type)], postfix); | |
c25b2ae1 HL |
612 | return env->type_str_buf; |
613 | } | |
17a52670 | 614 | |
8efea21d EC |
615 | static char slot_type_char[] = { |
616 | [STACK_INVALID] = '?', | |
617 | [STACK_SPILL] = 'r', | |
618 | [STACK_MISC] = 'm', | |
619 | [STACK_ZERO] = '0', | |
97e03f52 | 620 | [STACK_DYNPTR] = 'd', |
8efea21d EC |
621 | }; |
622 | ||
4e92024a AS |
623 | static void print_liveness(struct bpf_verifier_env *env, |
624 | enum bpf_reg_liveness live) | |
625 | { | |
9242b5f5 | 626 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
627 | verbose(env, "_"); |
628 | if (live & REG_LIVE_READ) | |
629 | verbose(env, "r"); | |
630 | if (live & REG_LIVE_WRITTEN) | |
631 | verbose(env, "w"); | |
9242b5f5 AS |
632 | if (live & REG_LIVE_DONE) |
633 | verbose(env, "D"); | |
4e92024a AS |
634 | } |
635 | ||
97e03f52 JK |
636 | static int get_spi(s32 off) |
637 | { | |
638 | return (-off - 1) / BPF_REG_SIZE; | |
639 | } | |
640 | ||
641 | static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots) | |
642 | { | |
643 | int allocated_slots = state->allocated_stack / BPF_REG_SIZE; | |
644 | ||
645 | /* We need to check that slots between [spi - nr_slots + 1, spi] are | |
646 | * within [0, allocated_stack). | |
647 | * | |
648 | * Please note that the spi grows downwards. For example, a dynptr | |
649 | * takes the size of two stack slots; the first slot will be at | |
650 | * spi and the second slot will be at spi - 1. | |
651 | */ | |
652 | return spi - nr_slots + 1 >= 0 && spi < allocated_slots; | |
653 | } | |
654 | ||
f4d7e40a AS |
655 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
656 | const struct bpf_reg_state *reg) | |
657 | { | |
658 | struct bpf_verifier_state *cur = env->cur_state; | |
659 | ||
660 | return cur->frame[reg->frameno]; | |
661 | } | |
662 | ||
22dc4a0f | 663 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
9e15db66 | 664 | { |
22dc4a0f | 665 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
666 | } |
667 | ||
0f55f9ed CL |
668 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
669 | { | |
670 | env->scratched_regs |= 1U << regno; | |
671 | } | |
672 | ||
673 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
674 | { | |
343e5375 | 675 | env->scratched_stack_slots |= 1ULL << spi; |
0f55f9ed CL |
676 | } |
677 | ||
678 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
679 | { | |
680 | return (env->scratched_regs >> regno) & 1; | |
681 | } | |
682 | ||
683 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
684 | { | |
685 | return (env->scratched_stack_slots >> regno) & 1; | |
686 | } | |
687 | ||
688 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
689 | { | |
690 | return env->scratched_regs || env->scratched_stack_slots; | |
691 | } | |
692 | ||
693 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
694 | { | |
695 | env->scratched_regs = 0U; | |
343e5375 | 696 | env->scratched_stack_slots = 0ULL; |
0f55f9ed CL |
697 | } |
698 | ||
699 | /* Used for printing the entire verifier state. */ | |
700 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
701 | { | |
702 | env->scratched_regs = ~0U; | |
343e5375 | 703 | env->scratched_stack_slots = ~0ULL; |
0f55f9ed CL |
704 | } |
705 | ||
97e03f52 JK |
706 | static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type) |
707 | { | |
708 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
709 | case DYNPTR_TYPE_LOCAL: | |
710 | return BPF_DYNPTR_TYPE_LOCAL; | |
bc34dee6 JK |
711 | case DYNPTR_TYPE_RINGBUF: |
712 | return BPF_DYNPTR_TYPE_RINGBUF; | |
97e03f52 JK |
713 | default: |
714 | return BPF_DYNPTR_TYPE_INVALID; | |
715 | } | |
716 | } | |
717 | ||
bc34dee6 JK |
718 | static bool dynptr_type_refcounted(enum bpf_dynptr_type type) |
719 | { | |
720 | return type == BPF_DYNPTR_TYPE_RINGBUF; | |
721 | } | |
722 | ||
97e03f52 JK |
723 | static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
724 | enum bpf_arg_type arg_type, int insn_idx) | |
725 | { | |
726 | struct bpf_func_state *state = func(env, reg); | |
727 | enum bpf_dynptr_type type; | |
bc34dee6 | 728 | int spi, i, id; |
97e03f52 JK |
729 | |
730 | spi = get_spi(reg->off); | |
731 | ||
732 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
733 | return -EINVAL; | |
734 | ||
735 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
736 | state->stack[spi].slot_type[i] = STACK_DYNPTR; | |
737 | state->stack[spi - 1].slot_type[i] = STACK_DYNPTR; | |
738 | } | |
739 | ||
740 | type = arg_to_dynptr_type(arg_type); | |
741 | if (type == BPF_DYNPTR_TYPE_INVALID) | |
742 | return -EINVAL; | |
743 | ||
744 | state->stack[spi].spilled_ptr.dynptr.first_slot = true; | |
745 | state->stack[spi].spilled_ptr.dynptr.type = type; | |
746 | state->stack[spi - 1].spilled_ptr.dynptr.type = type; | |
747 | ||
bc34dee6 JK |
748 | if (dynptr_type_refcounted(type)) { |
749 | /* The id is used to track proper releasing */ | |
750 | id = acquire_reference_state(env, insn_idx); | |
751 | if (id < 0) | |
752 | return id; | |
753 | ||
754 | state->stack[spi].spilled_ptr.id = id; | |
755 | state->stack[spi - 1].spilled_ptr.id = id; | |
756 | } | |
757 | ||
97e03f52 JK |
758 | return 0; |
759 | } | |
760 | ||
761 | static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
762 | { | |
763 | struct bpf_func_state *state = func(env, reg); | |
764 | int spi, i; | |
765 | ||
766 | spi = get_spi(reg->off); | |
767 | ||
768 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
769 | return -EINVAL; | |
770 | ||
771 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
772 | state->stack[spi].slot_type[i] = STACK_INVALID; | |
773 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
774 | } | |
775 | ||
bc34dee6 JK |
776 | /* Invalidate any slices associated with this dynptr */ |
777 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) { | |
778 | release_reference(env, state->stack[spi].spilled_ptr.id); | |
779 | state->stack[spi].spilled_ptr.id = 0; | |
780 | state->stack[spi - 1].spilled_ptr.id = 0; | |
781 | } | |
782 | ||
97e03f52 JK |
783 | state->stack[spi].spilled_ptr.dynptr.first_slot = false; |
784 | state->stack[spi].spilled_ptr.dynptr.type = 0; | |
785 | state->stack[spi - 1].spilled_ptr.dynptr.type = 0; | |
786 | ||
787 | return 0; | |
788 | } | |
789 | ||
790 | static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
791 | { | |
792 | struct bpf_func_state *state = func(env, reg); | |
793 | int spi = get_spi(reg->off); | |
794 | int i; | |
795 | ||
796 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
797 | return true; | |
798 | ||
799 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
800 | if (state->stack[spi].slot_type[i] == STACK_DYNPTR || | |
801 | state->stack[spi - 1].slot_type[i] == STACK_DYNPTR) | |
802 | return false; | |
803 | } | |
804 | ||
805 | return true; | |
806 | } | |
807 | ||
b8d31762 RS |
808 | bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, |
809 | struct bpf_reg_state *reg) | |
97e03f52 JK |
810 | { |
811 | struct bpf_func_state *state = func(env, reg); | |
812 | int spi = get_spi(reg->off); | |
813 | int i; | |
814 | ||
815 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS) || | |
816 | !state->stack[spi].spilled_ptr.dynptr.first_slot) | |
817 | return false; | |
818 | ||
819 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
820 | if (state->stack[spi].slot_type[i] != STACK_DYNPTR || | |
821 | state->stack[spi - 1].slot_type[i] != STACK_DYNPTR) | |
822 | return false; | |
823 | } | |
824 | ||
e9e315b4 RS |
825 | return true; |
826 | } | |
827 | ||
b8d31762 RS |
828 | bool is_dynptr_type_expected(struct bpf_verifier_env *env, |
829 | struct bpf_reg_state *reg, | |
830 | enum bpf_arg_type arg_type) | |
e9e315b4 RS |
831 | { |
832 | struct bpf_func_state *state = func(env, reg); | |
833 | enum bpf_dynptr_type dynptr_type; | |
834 | int spi = get_spi(reg->off); | |
835 | ||
97e03f52 JK |
836 | /* ARG_PTR_TO_DYNPTR takes any type of dynptr */ |
837 | if (arg_type == ARG_PTR_TO_DYNPTR) | |
838 | return true; | |
839 | ||
e9e315b4 RS |
840 | dynptr_type = arg_to_dynptr_type(arg_type); |
841 | ||
842 | return state->stack[spi].spilled_ptr.dynptr.type == dynptr_type; | |
97e03f52 JK |
843 | } |
844 | ||
27113c59 MKL |
845 | /* The reg state of a pointer or a bounded scalar was saved when |
846 | * it was spilled to the stack. | |
847 | */ | |
848 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
849 | { | |
850 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
851 | } | |
852 | ||
354e8f19 MKL |
853 | static void scrub_spilled_slot(u8 *stype) |
854 | { | |
855 | if (*stype != STACK_INVALID) | |
856 | *stype = STACK_MISC; | |
857 | } | |
858 | ||
61bd5218 | 859 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
860 | const struct bpf_func_state *state, |
861 | bool print_all) | |
17a52670 | 862 | { |
f4d7e40a | 863 | const struct bpf_reg_state *reg; |
17a52670 AS |
864 | enum bpf_reg_type t; |
865 | int i; | |
866 | ||
f4d7e40a AS |
867 | if (state->frameno) |
868 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 869 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
870 | reg = &state->regs[i]; |
871 | t = reg->type; | |
17a52670 AS |
872 | if (t == NOT_INIT) |
873 | continue; | |
0f55f9ed CL |
874 | if (!print_all && !reg_scratched(env, i)) |
875 | continue; | |
4e92024a AS |
876 | verbose(env, " R%d", i); |
877 | print_liveness(env, reg->live); | |
7df5072c | 878 | verbose(env, "="); |
b5dc0163 AS |
879 | if (t == SCALAR_VALUE && reg->precise) |
880 | verbose(env, "P"); | |
f1174f77 EC |
881 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
882 | tnum_is_const(reg->var_off)) { | |
883 | /* reg->off should be 0 for SCALAR_VALUE */ | |
7df5072c | 884 | verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
61bd5218 | 885 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 886 | } else { |
7df5072c ML |
887 | const char *sep = ""; |
888 | ||
889 | verbose(env, "%s", reg_type_str(env, t)); | |
5844101a | 890 | if (base_type(t) == PTR_TO_BTF_ID) |
22dc4a0f | 891 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
7df5072c ML |
892 | verbose(env, "("); |
893 | /* | |
894 | * _a stands for append, was shortened to avoid multiline statements below. | |
895 | * This macro is used to output a comma separated list of attributes. | |
896 | */ | |
897 | #define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) | |
898 | ||
899 | if (reg->id) | |
900 | verbose_a("id=%d", reg->id); | |
a28ace78 | 901 | if (reg->ref_obj_id) |
7df5072c | 902 | verbose_a("ref_obj_id=%d", reg->ref_obj_id); |
f1174f77 | 903 | if (t != SCALAR_VALUE) |
7df5072c | 904 | verbose_a("off=%d", reg->off); |
de8f3a83 | 905 | if (type_is_pkt_pointer(t)) |
7df5072c | 906 | verbose_a("r=%d", reg->range); |
c25b2ae1 HL |
907 | else if (base_type(t) == CONST_PTR_TO_MAP || |
908 | base_type(t) == PTR_TO_MAP_KEY || | |
909 | base_type(t) == PTR_TO_MAP_VALUE) | |
7df5072c ML |
910 | verbose_a("ks=%d,vs=%d", |
911 | reg->map_ptr->key_size, | |
912 | reg->map_ptr->value_size); | |
7d1238f2 EC |
913 | if (tnum_is_const(reg->var_off)) { |
914 | /* Typically an immediate SCALAR_VALUE, but | |
915 | * could be a pointer whose offset is too big | |
916 | * for reg->off | |
917 | */ | |
7df5072c | 918 | verbose_a("imm=%llx", reg->var_off.value); |
7d1238f2 EC |
919 | } else { |
920 | if (reg->smin_value != reg->umin_value && | |
921 | reg->smin_value != S64_MIN) | |
7df5072c | 922 | verbose_a("smin=%lld", (long long)reg->smin_value); |
7d1238f2 EC |
923 | if (reg->smax_value != reg->umax_value && |
924 | reg->smax_value != S64_MAX) | |
7df5072c | 925 | verbose_a("smax=%lld", (long long)reg->smax_value); |
7d1238f2 | 926 | if (reg->umin_value != 0) |
7df5072c | 927 | verbose_a("umin=%llu", (unsigned long long)reg->umin_value); |
7d1238f2 | 928 | if (reg->umax_value != U64_MAX) |
7df5072c | 929 | verbose_a("umax=%llu", (unsigned long long)reg->umax_value); |
7d1238f2 EC |
930 | if (!tnum_is_unknown(reg->var_off)) { |
931 | char tn_buf[48]; | |
f1174f77 | 932 | |
7d1238f2 | 933 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
7df5072c | 934 | verbose_a("var_off=%s", tn_buf); |
7d1238f2 | 935 | } |
3f50f132 JF |
936 | if (reg->s32_min_value != reg->smin_value && |
937 | reg->s32_min_value != S32_MIN) | |
7df5072c | 938 | verbose_a("s32_min=%d", (int)(reg->s32_min_value)); |
3f50f132 JF |
939 | if (reg->s32_max_value != reg->smax_value && |
940 | reg->s32_max_value != S32_MAX) | |
7df5072c | 941 | verbose_a("s32_max=%d", (int)(reg->s32_max_value)); |
3f50f132 JF |
942 | if (reg->u32_min_value != reg->umin_value && |
943 | reg->u32_min_value != U32_MIN) | |
7df5072c | 944 | verbose_a("u32_min=%d", (int)(reg->u32_min_value)); |
3f50f132 JF |
945 | if (reg->u32_max_value != reg->umax_value && |
946 | reg->u32_max_value != U32_MAX) | |
7df5072c | 947 | verbose_a("u32_max=%d", (int)(reg->u32_max_value)); |
f1174f77 | 948 | } |
7df5072c ML |
949 | #undef verbose_a |
950 | ||
61bd5218 | 951 | verbose(env, ")"); |
f1174f77 | 952 | } |
17a52670 | 953 | } |
638f5b90 | 954 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
955 | char types_buf[BPF_REG_SIZE + 1]; |
956 | bool valid = false; | |
957 | int j; | |
958 | ||
959 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
960 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
961 | valid = true; | |
962 | types_buf[j] = slot_type_char[ | |
963 | state->stack[i].slot_type[j]]; | |
964 | } | |
965 | types_buf[BPF_REG_SIZE] = 0; | |
966 | if (!valid) | |
967 | continue; | |
0f55f9ed CL |
968 | if (!print_all && !stack_slot_scratched(env, i)) |
969 | continue; | |
8efea21d EC |
970 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
971 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
27113c59 | 972 | if (is_spilled_reg(&state->stack[i])) { |
b5dc0163 AS |
973 | reg = &state->stack[i].spilled_ptr; |
974 | t = reg->type; | |
7df5072c | 975 | verbose(env, "=%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
b5dc0163 AS |
976 | if (t == SCALAR_VALUE && reg->precise) |
977 | verbose(env, "P"); | |
978 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
979 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
980 | } else { | |
8efea21d | 981 | verbose(env, "=%s", types_buf); |
b5dc0163 | 982 | } |
17a52670 | 983 | } |
fd978bf7 JS |
984 | if (state->acquired_refs && state->refs[0].id) { |
985 | verbose(env, " refs=%d", state->refs[0].id); | |
986 | for (i = 1; i < state->acquired_refs; i++) | |
987 | if (state->refs[i].id) | |
988 | verbose(env, ",%d", state->refs[i].id); | |
989 | } | |
bfc6bb74 AS |
990 | if (state->in_callback_fn) |
991 | verbose(env, " cb"); | |
992 | if (state->in_async_callback_fn) | |
993 | verbose(env, " async_cb"); | |
61bd5218 | 994 | verbose(env, "\n"); |
0f55f9ed | 995 | mark_verifier_state_clean(env); |
17a52670 AS |
996 | } |
997 | ||
2e576648 CL |
998 | static inline u32 vlog_alignment(u32 pos) |
999 | { | |
1000 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
1001 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
1002 | } | |
1003 | ||
1004 | static void print_insn_state(struct bpf_verifier_env *env, | |
1005 | const struct bpf_func_state *state) | |
1006 | { | |
1007 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { | |
1008 | /* remove new line character */ | |
1009 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); | |
1010 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); | |
1011 | } else { | |
1012 | verbose(env, "%d:", env->insn_idx); | |
1013 | } | |
1014 | print_verifier_state(env, state, false); | |
17a52670 AS |
1015 | } |
1016 | ||
c69431aa LB |
1017 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
1018 | * small to hold src. This is different from krealloc since we don't want to preserve | |
1019 | * the contents of dst. | |
1020 | * | |
1021 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
1022 | * not be allocated. | |
638f5b90 | 1023 | */ |
c69431aa | 1024 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 1025 | { |
c69431aa LB |
1026 | size_t bytes; |
1027 | ||
1028 | if (ZERO_OR_NULL_PTR(src)) | |
1029 | goto out; | |
1030 | ||
1031 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
1032 | return NULL; | |
1033 | ||
ceb35b66 | 1034 | if (ksize(dst) < ksize(src)) { |
c69431aa | 1035 | kfree(dst); |
ceb35b66 | 1036 | dst = kmalloc_track_caller(kmalloc_size_roundup(bytes), flags); |
c69431aa LB |
1037 | if (!dst) |
1038 | return NULL; | |
1039 | } | |
1040 | ||
1041 | memcpy(dst, src, bytes); | |
1042 | out: | |
1043 | return dst ? dst : ZERO_SIZE_PTR; | |
1044 | } | |
1045 | ||
1046 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
1047 | * small to hold new_n items. new items are zeroed out if the array grows. | |
1048 | * | |
1049 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
1050 | */ | |
1051 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
1052 | { | |
ceb35b66 | 1053 | size_t alloc_size; |
42378a9c KC |
1054 | void *new_arr; |
1055 | ||
c69431aa LB |
1056 | if (!new_n || old_n == new_n) |
1057 | goto out; | |
1058 | ||
ceb35b66 KC |
1059 | alloc_size = kmalloc_size_roundup(size_mul(new_n, size)); |
1060 | new_arr = krealloc(arr, alloc_size, GFP_KERNEL); | |
42378a9c KC |
1061 | if (!new_arr) { |
1062 | kfree(arr); | |
c69431aa | 1063 | return NULL; |
42378a9c KC |
1064 | } |
1065 | arr = new_arr; | |
c69431aa LB |
1066 | |
1067 | if (new_n > old_n) | |
1068 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
1069 | ||
1070 | out: | |
1071 | return arr ? arr : ZERO_SIZE_PTR; | |
1072 | } | |
1073 | ||
1074 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1075 | { | |
1076 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
1077 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
1078 | if (!dst->refs) | |
1079 | return -ENOMEM; | |
1080 | ||
1081 | dst->acquired_refs = src->acquired_refs; | |
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1086 | { | |
1087 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
1088 | ||
1089 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
1090 | GFP_KERNEL); | |
1091 | if (!dst->stack) | |
1092 | return -ENOMEM; | |
1093 | ||
1094 | dst->allocated_stack = src->allocated_stack; | |
1095 | return 0; | |
1096 | } | |
1097 | ||
1098 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
1099 | { | |
1100 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
1101 | sizeof(struct bpf_reference_state)); | |
1102 | if (!state->refs) | |
1103 | return -ENOMEM; | |
1104 | ||
1105 | state->acquired_refs = n; | |
1106 | return 0; | |
1107 | } | |
1108 | ||
1109 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
1110 | { | |
1111 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
1112 | ||
1113 | if (old_n >= n) | |
1114 | return 0; | |
1115 | ||
1116 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
1117 | if (!state->stack) | |
1118 | return -ENOMEM; | |
1119 | ||
1120 | state->allocated_stack = size; | |
1121 | return 0; | |
fd978bf7 JS |
1122 | } |
1123 | ||
1124 | /* Acquire a pointer id from the env and update the state->refs to include | |
1125 | * this new pointer reference. | |
1126 | * On success, returns a valid pointer id to associate with the register | |
1127 | * On failure, returns a negative errno. | |
638f5b90 | 1128 | */ |
fd978bf7 | 1129 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 1130 | { |
fd978bf7 JS |
1131 | struct bpf_func_state *state = cur_func(env); |
1132 | int new_ofs = state->acquired_refs; | |
1133 | int id, err; | |
1134 | ||
c69431aa | 1135 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
1136 | if (err) |
1137 | return err; | |
1138 | id = ++env->id_gen; | |
1139 | state->refs[new_ofs].id = id; | |
1140 | state->refs[new_ofs].insn_idx = insn_idx; | |
9d9d00ac | 1141 | state->refs[new_ofs].callback_ref = state->in_callback_fn ? state->frameno : 0; |
638f5b90 | 1142 | |
fd978bf7 JS |
1143 | return id; |
1144 | } | |
1145 | ||
1146 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 1147 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
1148 | { |
1149 | int i, last_idx; | |
1150 | ||
fd978bf7 JS |
1151 | last_idx = state->acquired_refs - 1; |
1152 | for (i = 0; i < state->acquired_refs; i++) { | |
1153 | if (state->refs[i].id == ptr_id) { | |
9d9d00ac KKD |
1154 | /* Cannot release caller references in callbacks */ |
1155 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) | |
1156 | return -EINVAL; | |
fd978bf7 JS |
1157 | if (last_idx && i != last_idx) |
1158 | memcpy(&state->refs[i], &state->refs[last_idx], | |
1159 | sizeof(*state->refs)); | |
1160 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
1161 | state->acquired_refs--; | |
638f5b90 | 1162 | return 0; |
638f5b90 | 1163 | } |
638f5b90 | 1164 | } |
46f8bc92 | 1165 | return -EINVAL; |
fd978bf7 JS |
1166 | } |
1167 | ||
f4d7e40a AS |
1168 | static void free_func_state(struct bpf_func_state *state) |
1169 | { | |
5896351e AS |
1170 | if (!state) |
1171 | return; | |
fd978bf7 | 1172 | kfree(state->refs); |
f4d7e40a AS |
1173 | kfree(state->stack); |
1174 | kfree(state); | |
1175 | } | |
1176 | ||
b5dc0163 AS |
1177 | static void clear_jmp_history(struct bpf_verifier_state *state) |
1178 | { | |
1179 | kfree(state->jmp_history); | |
1180 | state->jmp_history = NULL; | |
1181 | state->jmp_history_cnt = 0; | |
1182 | } | |
1183 | ||
1969db47 AS |
1184 | static void free_verifier_state(struct bpf_verifier_state *state, |
1185 | bool free_self) | |
638f5b90 | 1186 | { |
f4d7e40a AS |
1187 | int i; |
1188 | ||
1189 | for (i = 0; i <= state->curframe; i++) { | |
1190 | free_func_state(state->frame[i]); | |
1191 | state->frame[i] = NULL; | |
1192 | } | |
b5dc0163 | 1193 | clear_jmp_history(state); |
1969db47 AS |
1194 | if (free_self) |
1195 | kfree(state); | |
638f5b90 AS |
1196 | } |
1197 | ||
1198 | /* copy verifier state from src to dst growing dst stack space | |
1199 | * when necessary to accommodate larger src stack | |
1200 | */ | |
f4d7e40a AS |
1201 | static int copy_func_state(struct bpf_func_state *dst, |
1202 | const struct bpf_func_state *src) | |
638f5b90 AS |
1203 | { |
1204 | int err; | |
1205 | ||
fd978bf7 JS |
1206 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
1207 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
1208 | if (err) |
1209 | return err; | |
638f5b90 AS |
1210 | return copy_stack_state(dst, src); |
1211 | } | |
1212 | ||
f4d7e40a AS |
1213 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1214 | const struct bpf_verifier_state *src) | |
1215 | { | |
1216 | struct bpf_func_state *dst; | |
1217 | int i, err; | |
1218 | ||
06ab6a50 LB |
1219 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1220 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1221 | GFP_USER); | |
1222 | if (!dst_state->jmp_history) | |
1223 | return -ENOMEM; | |
b5dc0163 AS |
1224 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1225 | ||
f4d7e40a AS |
1226 | /* if dst has more stack frames then src frame, free them */ |
1227 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1228 | free_func_state(dst_state->frame[i]); | |
1229 | dst_state->frame[i] = NULL; | |
1230 | } | |
979d63d5 | 1231 | dst_state->speculative = src->speculative; |
9bb00b28 | 1232 | dst_state->active_rcu_lock = src->active_rcu_lock; |
f4d7e40a | 1233 | dst_state->curframe = src->curframe; |
d0d78c1d KKD |
1234 | dst_state->active_lock.ptr = src->active_lock.ptr; |
1235 | dst_state->active_lock.id = src->active_lock.id; | |
2589726d AS |
1236 | dst_state->branches = src->branches; |
1237 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1238 | dst_state->first_insn_idx = src->first_insn_idx; |
1239 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1240 | for (i = 0; i <= src->curframe; i++) { |
1241 | dst = dst_state->frame[i]; | |
1242 | if (!dst) { | |
1243 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1244 | if (!dst) | |
1245 | return -ENOMEM; | |
1246 | dst_state->frame[i] = dst; | |
1247 | } | |
1248 | err = copy_func_state(dst, src->frame[i]); | |
1249 | if (err) | |
1250 | return err; | |
1251 | } | |
1252 | return 0; | |
1253 | } | |
1254 | ||
2589726d AS |
1255 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1256 | { | |
1257 | while (st) { | |
1258 | u32 br = --st->branches; | |
1259 | ||
1260 | /* WARN_ON(br > 1) technically makes sense here, | |
1261 | * but see comment in push_stack(), hence: | |
1262 | */ | |
1263 | WARN_ONCE((int)br < 0, | |
1264 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1265 | br); | |
1266 | if (br) | |
1267 | break; | |
1268 | st = st->parent; | |
1269 | } | |
1270 | } | |
1271 | ||
638f5b90 | 1272 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1273 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1274 | { |
1275 | struct bpf_verifier_state *cur = env->cur_state; | |
1276 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1277 | int err; | |
17a52670 AS |
1278 | |
1279 | if (env->head == NULL) | |
638f5b90 | 1280 | return -ENOENT; |
17a52670 | 1281 | |
638f5b90 AS |
1282 | if (cur) { |
1283 | err = copy_verifier_state(cur, &head->st); | |
1284 | if (err) | |
1285 | return err; | |
1286 | } | |
6f8a57cc AN |
1287 | if (pop_log) |
1288 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1289 | if (insn_idx) |
1290 | *insn_idx = head->insn_idx; | |
17a52670 | 1291 | if (prev_insn_idx) |
638f5b90 AS |
1292 | *prev_insn_idx = head->prev_insn_idx; |
1293 | elem = head->next; | |
1969db47 | 1294 | free_verifier_state(&head->st, false); |
638f5b90 | 1295 | kfree(head); |
17a52670 AS |
1296 | env->head = elem; |
1297 | env->stack_size--; | |
638f5b90 | 1298 | return 0; |
17a52670 AS |
1299 | } |
1300 | ||
58e2af8b | 1301 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1302 | int insn_idx, int prev_insn_idx, |
1303 | bool speculative) | |
17a52670 | 1304 | { |
638f5b90 | 1305 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1306 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1307 | int err; |
17a52670 | 1308 | |
638f5b90 | 1309 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1310 | if (!elem) |
1311 | goto err; | |
1312 | ||
17a52670 AS |
1313 | elem->insn_idx = insn_idx; |
1314 | elem->prev_insn_idx = prev_insn_idx; | |
1315 | elem->next = env->head; | |
6f8a57cc | 1316 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1317 | env->head = elem; |
1318 | env->stack_size++; | |
1969db47 AS |
1319 | err = copy_verifier_state(&elem->st, cur); |
1320 | if (err) | |
1321 | goto err; | |
979d63d5 | 1322 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1323 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1324 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1325 | env->stack_size); | |
17a52670 AS |
1326 | goto err; |
1327 | } | |
2589726d AS |
1328 | if (elem->st.parent) { |
1329 | ++elem->st.parent->branches; | |
1330 | /* WARN_ON(branches > 2) technically makes sense here, | |
1331 | * but | |
1332 | * 1. speculative states will bump 'branches' for non-branch | |
1333 | * instructions | |
1334 | * 2. is_state_visited() heuristics may decide not to create | |
1335 | * a new state for a sequence of branches and all such current | |
1336 | * and cloned states will be pointing to a single parent state | |
1337 | * which might have large 'branches' count. | |
1338 | */ | |
1339 | } | |
17a52670 AS |
1340 | return &elem->st; |
1341 | err: | |
5896351e AS |
1342 | free_verifier_state(env->cur_state, true); |
1343 | env->cur_state = NULL; | |
17a52670 | 1344 | /* pop all elements and return */ |
6f8a57cc | 1345 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1346 | return NULL; |
1347 | } | |
1348 | ||
1349 | #define CALLER_SAVED_REGS 6 | |
1350 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1351 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1352 | }; | |
1353 | ||
f54c7898 DB |
1354 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1355 | struct bpf_reg_state *reg); | |
f1174f77 | 1356 | |
e688c3db AS |
1357 | /* This helper doesn't clear reg->id */ |
1358 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1359 | { |
b03c9f9f EC |
1360 | reg->var_off = tnum_const(imm); |
1361 | reg->smin_value = (s64)imm; | |
1362 | reg->smax_value = (s64)imm; | |
1363 | reg->umin_value = imm; | |
1364 | reg->umax_value = imm; | |
3f50f132 JF |
1365 | |
1366 | reg->s32_min_value = (s32)imm; | |
1367 | reg->s32_max_value = (s32)imm; | |
1368 | reg->u32_min_value = (u32)imm; | |
1369 | reg->u32_max_value = (u32)imm; | |
1370 | } | |
1371 | ||
e688c3db AS |
1372 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1373 | * known to have the value @imm. | |
1374 | */ | |
1375 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1376 | { | |
1377 | /* Clear id, off, and union(map_ptr, range) */ | |
1378 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1379 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
1380 | ___mark_reg_known(reg, imm); | |
1381 | } | |
1382 | ||
3f50f132 JF |
1383 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1384 | { | |
1385 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1386 | reg->s32_min_value = (s32)imm; | |
1387 | reg->s32_max_value = (s32)imm; | |
1388 | reg->u32_min_value = (u32)imm; | |
1389 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1390 | } |
1391 | ||
f1174f77 EC |
1392 | /* Mark the 'variable offset' part of a register as zero. This should be |
1393 | * used only on registers holding a pointer type. | |
1394 | */ | |
1395 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1396 | { |
b03c9f9f | 1397 | __mark_reg_known(reg, 0); |
f1174f77 | 1398 | } |
a9789ef9 | 1399 | |
cc2b14d5 AS |
1400 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1401 | { | |
1402 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1403 | reg->type = SCALAR_VALUE; |
1404 | } | |
1405 | ||
61bd5218 JK |
1406 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1407 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1408 | { |
1409 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1410 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1411 | /* Something bad happened, let's kill all regs */ |
1412 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1413 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1414 | return; |
1415 | } | |
1416 | __mark_reg_known_zero(regs + regno); | |
1417 | } | |
1418 | ||
4ddb7416 DB |
1419 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1420 | { | |
c25b2ae1 | 1421 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
4ddb7416 DB |
1422 | const struct bpf_map *map = reg->map_ptr; |
1423 | ||
1424 | if (map->inner_map_meta) { | |
1425 | reg->type = CONST_PTR_TO_MAP; | |
1426 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1427 | /* transfer reg's id which is unique for every map_lookup_elem |
1428 | * as UID of the inner map. | |
1429 | */ | |
db559117 | 1430 | if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER)) |
34d11a44 | 1431 | reg->map_uid = reg->id; |
4ddb7416 DB |
1432 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1433 | reg->type = PTR_TO_XDP_SOCK; | |
1434 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1435 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1436 | reg->type = PTR_TO_SOCKET; | |
1437 | } else { | |
1438 | reg->type = PTR_TO_MAP_VALUE; | |
1439 | } | |
c25b2ae1 | 1440 | return; |
4ddb7416 | 1441 | } |
c25b2ae1 HL |
1442 | |
1443 | reg->type &= ~PTR_MAYBE_NULL; | |
4ddb7416 DB |
1444 | } |
1445 | ||
de8f3a83 DB |
1446 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1447 | { | |
1448 | return type_is_pkt_pointer(reg->type); | |
1449 | } | |
1450 | ||
1451 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1452 | { | |
1453 | return reg_is_pkt_pointer(reg) || | |
1454 | reg->type == PTR_TO_PACKET_END; | |
1455 | } | |
1456 | ||
1457 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1458 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1459 | enum bpf_reg_type which) | |
1460 | { | |
1461 | /* The register can already have a range from prior markings. | |
1462 | * This is fine as long as it hasn't been advanced from its | |
1463 | * origin. | |
1464 | */ | |
1465 | return reg->type == which && | |
1466 | reg->id == 0 && | |
1467 | reg->off == 0 && | |
1468 | tnum_equals_const(reg->var_off, 0); | |
1469 | } | |
1470 | ||
3f50f132 JF |
1471 | /* Reset the min/max bounds of a register */ |
1472 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1473 | { | |
1474 | reg->smin_value = S64_MIN; | |
1475 | reg->smax_value = S64_MAX; | |
1476 | reg->umin_value = 0; | |
1477 | reg->umax_value = U64_MAX; | |
1478 | ||
1479 | reg->s32_min_value = S32_MIN; | |
1480 | reg->s32_max_value = S32_MAX; | |
1481 | reg->u32_min_value = 0; | |
1482 | reg->u32_max_value = U32_MAX; | |
1483 | } | |
1484 | ||
1485 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1486 | { | |
1487 | reg->smin_value = S64_MIN; | |
1488 | reg->smax_value = S64_MAX; | |
1489 | reg->umin_value = 0; | |
1490 | reg->umax_value = U64_MAX; | |
1491 | } | |
1492 | ||
1493 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1494 | { | |
1495 | reg->s32_min_value = S32_MIN; | |
1496 | reg->s32_max_value = S32_MAX; | |
1497 | reg->u32_min_value = 0; | |
1498 | reg->u32_max_value = U32_MAX; | |
1499 | } | |
1500 | ||
1501 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1502 | { | |
1503 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1504 | ||
1505 | /* min signed is max(sign bit) | min(other bits) */ | |
1506 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1507 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1508 | /* max signed is min(sign bit) | max(other bits) */ | |
1509 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1510 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1511 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1512 | reg->u32_max_value = min(reg->u32_max_value, | |
1513 | (u32)(var32_off.value | var32_off.mask)); | |
1514 | } | |
1515 | ||
1516 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1517 | { |
1518 | /* min signed is max(sign bit) | min(other bits) */ | |
1519 | reg->smin_value = max_t(s64, reg->smin_value, | |
1520 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1521 | /* max signed is min(sign bit) | max(other bits) */ | |
1522 | reg->smax_value = min_t(s64, reg->smax_value, | |
1523 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1524 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1525 | reg->umax_value = min(reg->umax_value, | |
1526 | reg->var_off.value | reg->var_off.mask); | |
1527 | } | |
1528 | ||
3f50f132 JF |
1529 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1530 | { | |
1531 | __update_reg32_bounds(reg); | |
1532 | __update_reg64_bounds(reg); | |
1533 | } | |
1534 | ||
b03c9f9f | 1535 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1536 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1537 | { | |
1538 | /* Learn sign from signed bounds. | |
1539 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1540 | * are the same, so combine. This works even in the negative case, e.g. | |
1541 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1542 | */ | |
1543 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1544 | reg->s32_min_value = reg->u32_min_value = | |
1545 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1546 | reg->s32_max_value = reg->u32_max_value = | |
1547 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1548 | return; | |
1549 | } | |
1550 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1551 | * boundary, so we must be careful. | |
1552 | */ | |
1553 | if ((s32)reg->u32_max_value >= 0) { | |
1554 | /* Positive. We can't learn anything from the smin, but smax | |
1555 | * is positive, hence safe. | |
1556 | */ | |
1557 | reg->s32_min_value = reg->u32_min_value; | |
1558 | reg->s32_max_value = reg->u32_max_value = | |
1559 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1560 | } else if ((s32)reg->u32_min_value < 0) { | |
1561 | /* Negative. We can't learn anything from the smax, but smin | |
1562 | * is negative, hence safe. | |
1563 | */ | |
1564 | reg->s32_min_value = reg->u32_min_value = | |
1565 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1566 | reg->s32_max_value = reg->u32_max_value; | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1571 | { |
1572 | /* Learn sign from signed bounds. | |
1573 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1574 | * are the same, so combine. This works even in the negative case, e.g. | |
1575 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1576 | */ | |
1577 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1578 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1579 | reg->umin_value); | |
1580 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1581 | reg->umax_value); | |
1582 | return; | |
1583 | } | |
1584 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1585 | * boundary, so we must be careful. | |
1586 | */ | |
1587 | if ((s64)reg->umax_value >= 0) { | |
1588 | /* Positive. We can't learn anything from the smin, but smax | |
1589 | * is positive, hence safe. | |
1590 | */ | |
1591 | reg->smin_value = reg->umin_value; | |
1592 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1593 | reg->umax_value); | |
1594 | } else if ((s64)reg->umin_value < 0) { | |
1595 | /* Negative. We can't learn anything from the smax, but smin | |
1596 | * is negative, hence safe. | |
1597 | */ | |
1598 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1599 | reg->umin_value); | |
1600 | reg->smax_value = reg->umax_value; | |
1601 | } | |
1602 | } | |
1603 | ||
3f50f132 JF |
1604 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1605 | { | |
1606 | __reg32_deduce_bounds(reg); | |
1607 | __reg64_deduce_bounds(reg); | |
1608 | } | |
1609 | ||
b03c9f9f EC |
1610 | /* Attempts to improve var_off based on unsigned min/max information */ |
1611 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1612 | { | |
3f50f132 JF |
1613 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1614 | tnum_range(reg->umin_value, | |
1615 | reg->umax_value)); | |
1616 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1617 | tnum_range(reg->u32_min_value, | |
1618 | reg->u32_max_value)); | |
1619 | ||
1620 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1621 | } |
1622 | ||
3844d153 DB |
1623 | static void reg_bounds_sync(struct bpf_reg_state *reg) |
1624 | { | |
1625 | /* We might have learned new bounds from the var_off. */ | |
1626 | __update_reg_bounds(reg); | |
1627 | /* We might have learned something about the sign bit. */ | |
1628 | __reg_deduce_bounds(reg); | |
1629 | /* We might have learned some bits from the bounds. */ | |
1630 | __reg_bound_offset(reg); | |
1631 | /* Intersecting with the old var_off might have improved our bounds | |
1632 | * slightly, e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1633 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1634 | */ | |
1635 | __update_reg_bounds(reg); | |
1636 | } | |
1637 | ||
e572ff80 DB |
1638 | static bool __reg32_bound_s64(s32 a) |
1639 | { | |
1640 | return a >= 0 && a <= S32_MAX; | |
1641 | } | |
1642 | ||
3f50f132 | 1643 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1644 | { |
3f50f132 JF |
1645 | reg->umin_value = reg->u32_min_value; |
1646 | reg->umax_value = reg->u32_max_value; | |
e572ff80 DB |
1647 | |
1648 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must | |
1649 | * be positive otherwise set to worse case bounds and refine later | |
1650 | * from tnum. | |
3f50f132 | 1651 | */ |
e572ff80 DB |
1652 | if (__reg32_bound_s64(reg->s32_min_value) && |
1653 | __reg32_bound_s64(reg->s32_max_value)) { | |
3a71dc36 | 1654 | reg->smin_value = reg->s32_min_value; |
e572ff80 DB |
1655 | reg->smax_value = reg->s32_max_value; |
1656 | } else { | |
3a71dc36 | 1657 | reg->smin_value = 0; |
e572ff80 DB |
1658 | reg->smax_value = U32_MAX; |
1659 | } | |
3f50f132 JF |
1660 | } |
1661 | ||
1662 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1663 | { | |
1664 | /* special case when 64-bit register has upper 32-bit register | |
1665 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1666 | * allowing us to use 32-bit bounds directly, | |
1667 | */ | |
1668 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1669 | __reg_assign_32_into_64(reg); | |
1670 | } else { | |
1671 | /* Otherwise the best we can do is push lower 32bit known and | |
1672 | * unknown bits into register (var_off set from jmp logic) | |
1673 | * then learn as much as possible from the 64-bit tnum | |
1674 | * known and unknown bits. The previous smin/smax bounds are | |
1675 | * invalid here because of jmp32 compare so mark them unknown | |
1676 | * so they do not impact tnum bounds calculation. | |
1677 | */ | |
1678 | __mark_reg64_unbounded(reg); | |
3f50f132 | 1679 | } |
3844d153 | 1680 | reg_bounds_sync(reg); |
3f50f132 JF |
1681 | } |
1682 | ||
1683 | static bool __reg64_bound_s32(s64 a) | |
1684 | { | |
388e2c0b | 1685 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
1686 | } |
1687 | ||
1688 | static bool __reg64_bound_u32(u64 a) | |
1689 | { | |
b9979db8 | 1690 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
1691 | } |
1692 | ||
1693 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1694 | { | |
1695 | __mark_reg32_unbounded(reg); | |
b0270958 | 1696 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 1697 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 1698 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 1699 | } |
10bf4e83 | 1700 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 1701 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 1702 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 1703 | } |
3844d153 | 1704 | reg_bounds_sync(reg); |
b03c9f9f EC |
1705 | } |
1706 | ||
f1174f77 | 1707 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1708 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1709 | struct bpf_reg_state *reg) | |
f1174f77 | 1710 | { |
a9c676bc AS |
1711 | /* |
1712 | * Clear type, id, off, and union(map_ptr, range) and | |
1713 | * padding between 'type' and union | |
1714 | */ | |
1715 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1716 | reg->type = SCALAR_VALUE; |
f1174f77 | 1717 | reg->var_off = tnum_unknown; |
f4d7e40a | 1718 | reg->frameno = 0; |
be2ef816 | 1719 | reg->precise = !env->bpf_capable; |
b03c9f9f | 1720 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1721 | } |
1722 | ||
61bd5218 JK |
1723 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1724 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1725 | { |
1726 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1727 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1728 | /* Something bad happened, let's kill all regs except FP */ |
1729 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1730 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1731 | return; |
1732 | } | |
f54c7898 | 1733 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1734 | } |
1735 | ||
f54c7898 DB |
1736 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1737 | struct bpf_reg_state *reg) | |
f1174f77 | 1738 | { |
f54c7898 | 1739 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1740 | reg->type = NOT_INIT; |
1741 | } | |
1742 | ||
61bd5218 JK |
1743 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1744 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1745 | { |
1746 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1747 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1748 | /* Something bad happened, let's kill all regs except FP */ |
1749 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1750 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1751 | return; |
1752 | } | |
f54c7898 | 1753 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1754 | } |
1755 | ||
41c48f3a AI |
1756 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1757 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f | 1758 | enum bpf_reg_type reg_type, |
c6f1bfe8 YS |
1759 | struct btf *btf, u32 btf_id, |
1760 | enum bpf_type_flag flag) | |
41c48f3a AI |
1761 | { |
1762 | if (reg_type == SCALAR_VALUE) { | |
1763 | mark_reg_unknown(env, regs, regno); | |
1764 | return; | |
1765 | } | |
1766 | mark_reg_known_zero(env, regs, regno); | |
c6f1bfe8 | 1767 | regs[regno].type = PTR_TO_BTF_ID | flag; |
22dc4a0f | 1768 | regs[regno].btf = btf; |
41c48f3a AI |
1769 | regs[regno].btf_id = btf_id; |
1770 | } | |
1771 | ||
5327ed3d | 1772 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1773 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1774 | struct bpf_func_state *state) |
17a52670 | 1775 | { |
f4d7e40a | 1776 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1777 | int i; |
1778 | ||
dc503a8a | 1779 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1780 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1781 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1782 | regs[i].parent = NULL; |
5327ed3d | 1783 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1784 | } |
17a52670 AS |
1785 | |
1786 | /* frame pointer */ | |
f1174f77 | 1787 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1788 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1789 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1790 | } |
1791 | ||
f4d7e40a AS |
1792 | #define BPF_MAIN_FUNC (-1) |
1793 | static void init_func_state(struct bpf_verifier_env *env, | |
1794 | struct bpf_func_state *state, | |
1795 | int callsite, int frameno, int subprogno) | |
1796 | { | |
1797 | state->callsite = callsite; | |
1798 | state->frameno = frameno; | |
1799 | state->subprogno = subprogno; | |
1bfe26fb | 1800 | state->callback_ret_range = tnum_range(0, 0); |
f4d7e40a | 1801 | init_reg_state(env, state); |
0f55f9ed | 1802 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
1803 | } |
1804 | ||
bfc6bb74 AS |
1805 | /* Similar to push_stack(), but for async callbacks */ |
1806 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
1807 | int insn_idx, int prev_insn_idx, | |
1808 | int subprog) | |
1809 | { | |
1810 | struct bpf_verifier_stack_elem *elem; | |
1811 | struct bpf_func_state *frame; | |
1812 | ||
1813 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
1814 | if (!elem) | |
1815 | goto err; | |
1816 | ||
1817 | elem->insn_idx = insn_idx; | |
1818 | elem->prev_insn_idx = prev_insn_idx; | |
1819 | elem->next = env->head; | |
1820 | elem->log_pos = env->log.len_used; | |
1821 | env->head = elem; | |
1822 | env->stack_size++; | |
1823 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
1824 | verbose(env, | |
1825 | "The sequence of %d jumps is too complex for async cb.\n", | |
1826 | env->stack_size); | |
1827 | goto err; | |
1828 | } | |
1829 | /* Unlike push_stack() do not copy_verifier_state(). | |
1830 | * The caller state doesn't matter. | |
1831 | * This is async callback. It starts in a fresh stack. | |
1832 | * Initialize it similar to do_check_common(). | |
1833 | */ | |
1834 | elem->st.branches = 1; | |
1835 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
1836 | if (!frame) | |
1837 | goto err; | |
1838 | init_func_state(env, frame, | |
1839 | BPF_MAIN_FUNC /* callsite */, | |
1840 | 0 /* frameno within this callchain */, | |
1841 | subprog /* subprog number within this prog */); | |
1842 | elem->st.frame[0] = frame; | |
1843 | return &elem->st; | |
1844 | err: | |
1845 | free_verifier_state(env->cur_state, true); | |
1846 | env->cur_state = NULL; | |
1847 | /* pop all elements and return */ | |
1848 | while (!pop_stack(env, NULL, NULL, false)); | |
1849 | return NULL; | |
1850 | } | |
1851 | ||
1852 | ||
17a52670 AS |
1853 | enum reg_arg_type { |
1854 | SRC_OP, /* register is used as source operand */ | |
1855 | DST_OP, /* register is used as destination operand */ | |
1856 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1857 | }; | |
1858 | ||
cc8b0b92 AS |
1859 | static int cmp_subprogs(const void *a, const void *b) |
1860 | { | |
9c8105bd JW |
1861 | return ((struct bpf_subprog_info *)a)->start - |
1862 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1863 | } |
1864 | ||
1865 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1866 | { | |
9c8105bd | 1867 | struct bpf_subprog_info *p; |
cc8b0b92 | 1868 | |
9c8105bd JW |
1869 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1870 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1871 | if (!p) |
1872 | return -ENOENT; | |
9c8105bd | 1873 | return p - env->subprog_info; |
cc8b0b92 AS |
1874 | |
1875 | } | |
1876 | ||
1877 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1878 | { | |
1879 | int insn_cnt = env->prog->len; | |
1880 | int ret; | |
1881 | ||
1882 | if (off >= insn_cnt || off < 0) { | |
1883 | verbose(env, "call to invalid destination\n"); | |
1884 | return -EINVAL; | |
1885 | } | |
1886 | ret = find_subprog(env, off); | |
1887 | if (ret >= 0) | |
282a0f46 | 1888 | return ret; |
4cb3d99c | 1889 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1890 | verbose(env, "too many subprograms\n"); |
1891 | return -E2BIG; | |
1892 | } | |
e6ac2450 | 1893 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
1894 | env->subprog_info[env->subprog_cnt++].start = off; |
1895 | sort(env->subprog_info, env->subprog_cnt, | |
1896 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 1897 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
1898 | } |
1899 | ||
2357672c KKD |
1900 | #define MAX_KFUNC_DESCS 256 |
1901 | #define MAX_KFUNC_BTFS 256 | |
1902 | ||
e6ac2450 MKL |
1903 | struct bpf_kfunc_desc { |
1904 | struct btf_func_model func_model; | |
1905 | u32 func_id; | |
1906 | s32 imm; | |
2357672c KKD |
1907 | u16 offset; |
1908 | }; | |
1909 | ||
1910 | struct bpf_kfunc_btf { | |
1911 | struct btf *btf; | |
1912 | struct module *module; | |
1913 | u16 offset; | |
e6ac2450 MKL |
1914 | }; |
1915 | ||
e6ac2450 MKL |
1916 | struct bpf_kfunc_desc_tab { |
1917 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
1918 | u32 nr_descs; | |
1919 | }; | |
1920 | ||
2357672c KKD |
1921 | struct bpf_kfunc_btf_tab { |
1922 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
1923 | u32 nr_descs; | |
1924 | }; | |
1925 | ||
1926 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
1927 | { |
1928 | const struct bpf_kfunc_desc *d0 = a; | |
1929 | const struct bpf_kfunc_desc *d1 = b; | |
1930 | ||
1931 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
1932 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
1933 | } | |
1934 | ||
1935 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
1936 | { | |
1937 | const struct bpf_kfunc_btf *d0 = a; | |
1938 | const struct bpf_kfunc_btf *d1 = b; | |
1939 | ||
1940 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
1941 | } |
1942 | ||
1943 | static const struct bpf_kfunc_desc * | |
2357672c | 1944 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
1945 | { |
1946 | struct bpf_kfunc_desc desc = { | |
1947 | .func_id = func_id, | |
2357672c | 1948 | .offset = offset, |
e6ac2450 MKL |
1949 | }; |
1950 | struct bpf_kfunc_desc_tab *tab; | |
1951 | ||
1952 | tab = prog->aux->kfunc_tab; | |
1953 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
1954 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
1955 | } | |
1956 | ||
1957 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
b202d844 | 1958 | s16 offset) |
2357672c KKD |
1959 | { |
1960 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
1961 | struct bpf_kfunc_btf_tab *tab; | |
1962 | struct bpf_kfunc_btf *b; | |
1963 | struct module *mod; | |
1964 | struct btf *btf; | |
1965 | int btf_fd; | |
1966 | ||
1967 | tab = env->prog->aux->kfunc_btf_tab; | |
1968 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
1969 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
1970 | if (!b) { | |
1971 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
1972 | verbose(env, "too many different module BTFs\n"); | |
1973 | return ERR_PTR(-E2BIG); | |
1974 | } | |
1975 | ||
1976 | if (bpfptr_is_null(env->fd_array)) { | |
1977 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
1978 | return ERR_PTR(-EPROTO); | |
1979 | } | |
1980 | ||
1981 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
1982 | offset * sizeof(btf_fd), | |
1983 | sizeof(btf_fd))) | |
1984 | return ERR_PTR(-EFAULT); | |
1985 | ||
1986 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
1987 | if (IS_ERR(btf)) { |
1988 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 1989 | return btf; |
588cd7ef | 1990 | } |
2357672c KKD |
1991 | |
1992 | if (!btf_is_module(btf)) { | |
1993 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
1994 | btf_put(btf); | |
1995 | return ERR_PTR(-EINVAL); | |
1996 | } | |
1997 | ||
1998 | mod = btf_try_get_module(btf); | |
1999 | if (!mod) { | |
2000 | btf_put(btf); | |
2001 | return ERR_PTR(-ENXIO); | |
2002 | } | |
2003 | ||
2004 | b = &tab->descs[tab->nr_descs++]; | |
2005 | b->btf = btf; | |
2006 | b->module = mod; | |
2007 | b->offset = offset; | |
2008 | ||
2009 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2010 | kfunc_btf_cmp_by_off, NULL); | |
2011 | } | |
2357672c | 2012 | return b->btf; |
e6ac2450 MKL |
2013 | } |
2014 | ||
2357672c KKD |
2015 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
2016 | { | |
2017 | if (!tab) | |
2018 | return; | |
2019 | ||
2020 | while (tab->nr_descs--) { | |
2021 | module_put(tab->descs[tab->nr_descs].module); | |
2022 | btf_put(tab->descs[tab->nr_descs].btf); | |
2023 | } | |
2024 | kfree(tab); | |
2025 | } | |
2026 | ||
43bf0878 | 2027 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset) |
2357672c | 2028 | { |
2357672c KKD |
2029 | if (offset) { |
2030 | if (offset < 0) { | |
2031 | /* In the future, this can be allowed to increase limit | |
2032 | * of fd index into fd_array, interpreted as u16. | |
2033 | */ | |
2034 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
2035 | return ERR_PTR(-EINVAL); | |
2036 | } | |
2037 | ||
b202d844 | 2038 | return __find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2039 | } |
2040 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
2041 | } |
2042 | ||
2357672c | 2043 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
2044 | { |
2045 | const struct btf_type *func, *func_proto; | |
2357672c | 2046 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
2047 | struct bpf_kfunc_desc_tab *tab; |
2048 | struct bpf_prog_aux *prog_aux; | |
2049 | struct bpf_kfunc_desc *desc; | |
2050 | const char *func_name; | |
2357672c | 2051 | struct btf *desc_btf; |
8cbf062a | 2052 | unsigned long call_imm; |
e6ac2450 MKL |
2053 | unsigned long addr; |
2054 | int err; | |
2055 | ||
2056 | prog_aux = env->prog->aux; | |
2057 | tab = prog_aux->kfunc_tab; | |
2357672c | 2058 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
2059 | if (!tab) { |
2060 | if (!btf_vmlinux) { | |
2061 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
2062 | return -ENOTSUPP; | |
2063 | } | |
2064 | ||
2065 | if (!env->prog->jit_requested) { | |
2066 | verbose(env, "JIT is required for calling kernel function\n"); | |
2067 | return -ENOTSUPP; | |
2068 | } | |
2069 | ||
2070 | if (!bpf_jit_supports_kfunc_call()) { | |
2071 | verbose(env, "JIT does not support calling kernel function\n"); | |
2072 | return -ENOTSUPP; | |
2073 | } | |
2074 | ||
2075 | if (!env->prog->gpl_compatible) { | |
2076 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
2077 | return -EINVAL; | |
2078 | } | |
2079 | ||
2080 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
2081 | if (!tab) | |
2082 | return -ENOMEM; | |
2083 | prog_aux->kfunc_tab = tab; | |
2084 | } | |
2085 | ||
a5d82727 KKD |
2086 | /* func_id == 0 is always invalid, but instead of returning an error, be |
2087 | * conservative and wait until the code elimination pass before returning | |
2088 | * error, so that invalid calls that get pruned out can be in BPF programs | |
2089 | * loaded from userspace. It is also required that offset be untouched | |
2090 | * for such calls. | |
2091 | */ | |
2092 | if (!func_id && !offset) | |
2093 | return 0; | |
2094 | ||
2357672c KKD |
2095 | if (!btf_tab && offset) { |
2096 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
2097 | if (!btf_tab) | |
2098 | return -ENOMEM; | |
2099 | prog_aux->kfunc_btf_tab = btf_tab; | |
2100 | } | |
2101 | ||
43bf0878 | 2102 | desc_btf = find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2103 | if (IS_ERR(desc_btf)) { |
2104 | verbose(env, "failed to find BTF for kernel function\n"); | |
2105 | return PTR_ERR(desc_btf); | |
2106 | } | |
2107 | ||
2108 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
2109 | return 0; |
2110 | ||
2111 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
2112 | verbose(env, "too many different kernel function calls\n"); | |
2113 | return -E2BIG; | |
2114 | } | |
2115 | ||
2357672c | 2116 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
2117 | if (!func || !btf_type_is_func(func)) { |
2118 | verbose(env, "kernel btf_id %u is not a function\n", | |
2119 | func_id); | |
2120 | return -EINVAL; | |
2121 | } | |
2357672c | 2122 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
2123 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
2124 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
2125 | func_id); | |
2126 | return -EINVAL; | |
2127 | } | |
2128 | ||
2357672c | 2129 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
2130 | addr = kallsyms_lookup_name(func_name); |
2131 | if (!addr) { | |
2132 | verbose(env, "cannot find address for kernel function %s\n", | |
2133 | func_name); | |
2134 | return -EINVAL; | |
2135 | } | |
2136 | ||
8cbf062a HT |
2137 | call_imm = BPF_CALL_IMM(addr); |
2138 | /* Check whether or not the relative offset overflows desc->imm */ | |
2139 | if ((unsigned long)(s32)call_imm != call_imm) { | |
2140 | verbose(env, "address of kernel function %s is out of range\n", | |
2141 | func_name); | |
2142 | return -EINVAL; | |
2143 | } | |
2144 | ||
e6ac2450 MKL |
2145 | desc = &tab->descs[tab->nr_descs++]; |
2146 | desc->func_id = func_id; | |
8cbf062a | 2147 | desc->imm = call_imm; |
2357672c KKD |
2148 | desc->offset = offset; |
2149 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
2150 | func_proto, func_name, |
2151 | &desc->func_model); | |
2152 | if (!err) | |
2153 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 2154 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
2155 | return err; |
2156 | } | |
2157 | ||
2158 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
2159 | { | |
2160 | const struct bpf_kfunc_desc *d0 = a; | |
2161 | const struct bpf_kfunc_desc *d1 = b; | |
2162 | ||
2163 | if (d0->imm > d1->imm) | |
2164 | return 1; | |
2165 | else if (d0->imm < d1->imm) | |
2166 | return -1; | |
2167 | return 0; | |
2168 | } | |
2169 | ||
2170 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
2171 | { | |
2172 | struct bpf_kfunc_desc_tab *tab; | |
2173 | ||
2174 | tab = prog->aux->kfunc_tab; | |
2175 | if (!tab) | |
2176 | return; | |
2177 | ||
2178 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2179 | kfunc_desc_cmp_by_imm, NULL); | |
2180 | } | |
2181 | ||
2182 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
2183 | { | |
2184 | return !!prog->aux->kfunc_tab; | |
2185 | } | |
2186 | ||
2187 | const struct btf_func_model * | |
2188 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2189 | const struct bpf_insn *insn) | |
2190 | { | |
2191 | const struct bpf_kfunc_desc desc = { | |
2192 | .imm = insn->imm, | |
2193 | }; | |
2194 | const struct bpf_kfunc_desc *res; | |
2195 | struct bpf_kfunc_desc_tab *tab; | |
2196 | ||
2197 | tab = prog->aux->kfunc_tab; | |
2198 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
2199 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
2200 | ||
2201 | return res ? &res->func_model : NULL; | |
2202 | } | |
2203 | ||
2204 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2205 | { |
9c8105bd | 2206 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2207 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2208 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2209 | |
f910cefa JW |
2210 | /* Add entry function. */ |
2211 | ret = add_subprog(env, 0); | |
e6ac2450 | 2212 | if (ret) |
f910cefa JW |
2213 | return ret; |
2214 | ||
e6ac2450 MKL |
2215 | for (i = 0; i < insn_cnt; i++, insn++) { |
2216 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2217 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2218 | continue; |
e6ac2450 | 2219 | |
2c78ee89 | 2220 | if (!env->bpf_capable) { |
e6ac2450 | 2221 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2222 | return -EPERM; |
2223 | } | |
e6ac2450 | 2224 | |
3990ed4c | 2225 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2226 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2227 | else |
2357672c | 2228 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2229 | |
cc8b0b92 AS |
2230 | if (ret < 0) |
2231 | return ret; | |
2232 | } | |
2233 | ||
4cb3d99c JW |
2234 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2235 | * logic. 'subprog_cnt' should not be increased. | |
2236 | */ | |
2237 | subprog[env->subprog_cnt].start = insn_cnt; | |
2238 | ||
06ee7115 | 2239 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2240 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2241 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2242 | |
e6ac2450 MKL |
2243 | return 0; |
2244 | } | |
2245 | ||
2246 | static int check_subprogs(struct bpf_verifier_env *env) | |
2247 | { | |
2248 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2249 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2250 | struct bpf_insn *insn = env->prog->insnsi; | |
2251 | int insn_cnt = env->prog->len; | |
2252 | ||
cc8b0b92 | 2253 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2254 | subprog_start = subprog[cur_subprog].start; |
2255 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2256 | for (i = 0; i < insn_cnt; i++) { |
2257 | u8 code = insn[i].code; | |
2258 | ||
7f6e4312 MF |
2259 | if (code == (BPF_JMP | BPF_CALL) && |
2260 | insn[i].imm == BPF_FUNC_tail_call && | |
2261 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
2262 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
2263 | if (BPF_CLASS(code) == BPF_LD && |
2264 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2265 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2266 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2267 | goto next; |
2268 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2269 | goto next; | |
2270 | off = i + insn[i].off + 1; | |
2271 | if (off < subprog_start || off >= subprog_end) { | |
2272 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2273 | return -EINVAL; | |
2274 | } | |
2275 | next: | |
2276 | if (i == subprog_end - 1) { | |
2277 | /* to avoid fall-through from one subprog into another | |
2278 | * the last insn of the subprog should be either exit | |
2279 | * or unconditional jump back | |
2280 | */ | |
2281 | if (code != (BPF_JMP | BPF_EXIT) && | |
2282 | code != (BPF_JMP | BPF_JA)) { | |
2283 | verbose(env, "last insn is not an exit or jmp\n"); | |
2284 | return -EINVAL; | |
2285 | } | |
2286 | subprog_start = subprog_end; | |
4cb3d99c JW |
2287 | cur_subprog++; |
2288 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2289 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2290 | } |
2291 | } | |
2292 | return 0; | |
2293 | } | |
2294 | ||
679c782d EC |
2295 | /* Parentage chain of this register (or stack slot) should take care of all |
2296 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2297 | */ | |
f4d7e40a | 2298 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2299 | const struct bpf_reg_state *state, |
5327ed3d | 2300 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2301 | { |
2302 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2303 | int cnt = 0; |
dc503a8a EC |
2304 | |
2305 | while (parent) { | |
2306 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2307 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2308 | break; |
9242b5f5 AS |
2309 | if (parent->live & REG_LIVE_DONE) { |
2310 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
c25b2ae1 | 2311 | reg_type_str(env, parent->type), |
9242b5f5 AS |
2312 | parent->var_off.value, parent->off); |
2313 | return -EFAULT; | |
2314 | } | |
5327ed3d JW |
2315 | /* The first condition is more likely to be true than the |
2316 | * second, checked it first. | |
2317 | */ | |
2318 | if ((parent->live & REG_LIVE_READ) == flag || | |
2319 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2320 | /* The parentage chain never changes and |
2321 | * this parent was already marked as LIVE_READ. | |
2322 | * There is no need to keep walking the chain again and | |
2323 | * keep re-marking all parents as LIVE_READ. | |
2324 | * This case happens when the same register is read | |
2325 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2326 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2327 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2328 | */ |
2329 | break; | |
dc503a8a | 2330 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2331 | parent->live |= flag; |
2332 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2333 | if (flag == REG_LIVE_READ64) | |
2334 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2335 | state = parent; |
2336 | parent = state->parent; | |
f4d7e40a | 2337 | writes = true; |
06ee7115 | 2338 | cnt++; |
dc503a8a | 2339 | } |
06ee7115 AS |
2340 | |
2341 | if (env->longest_mark_read_walk < cnt) | |
2342 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2343 | return 0; |
dc503a8a EC |
2344 | } |
2345 | ||
5327ed3d JW |
2346 | /* This function is supposed to be used by the following 32-bit optimization |
2347 | * code only. It returns TRUE if the source or destination register operates | |
2348 | * on 64-bit, otherwise return FALSE. | |
2349 | */ | |
2350 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2351 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2352 | { | |
2353 | u8 code, class, op; | |
2354 | ||
2355 | code = insn->code; | |
2356 | class = BPF_CLASS(code); | |
2357 | op = BPF_OP(code); | |
2358 | if (class == BPF_JMP) { | |
2359 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2360 | * conservatively. | |
2361 | */ | |
2362 | if (op == BPF_EXIT) | |
2363 | return true; | |
2364 | if (op == BPF_CALL) { | |
2365 | /* BPF to BPF call will reach here because of marking | |
2366 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2367 | * don't care the register def because they are anyway | |
2368 | * marked as NOT_INIT already. | |
2369 | */ | |
2370 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2371 | return false; | |
2372 | /* Helper call will reach here because of arg type | |
2373 | * check, conservatively return TRUE. | |
2374 | */ | |
2375 | if (t == SRC_OP) | |
2376 | return true; | |
2377 | ||
2378 | return false; | |
2379 | } | |
2380 | } | |
2381 | ||
2382 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2383 | /* BPF_END always use BPF_ALU class. */ | |
2384 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2385 | return true; | |
2386 | ||
2387 | if (class == BPF_ALU || class == BPF_JMP32) | |
2388 | return false; | |
2389 | ||
2390 | if (class == BPF_LDX) { | |
2391 | if (t != SRC_OP) | |
2392 | return BPF_SIZE(code) == BPF_DW; | |
2393 | /* LDX source must be ptr. */ | |
2394 | return true; | |
2395 | } | |
2396 | ||
2397 | if (class == BPF_STX) { | |
83a28819 IL |
2398 | /* BPF_STX (including atomic variants) has multiple source |
2399 | * operands, one of which is a ptr. Check whether the caller is | |
2400 | * asking about it. | |
2401 | */ | |
2402 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2403 | return true; |
2404 | return BPF_SIZE(code) == BPF_DW; | |
2405 | } | |
2406 | ||
2407 | if (class == BPF_LD) { | |
2408 | u8 mode = BPF_MODE(code); | |
2409 | ||
2410 | /* LD_IMM64 */ | |
2411 | if (mode == BPF_IMM) | |
2412 | return true; | |
2413 | ||
2414 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
2415 | if (t != SRC_OP) | |
2416 | return false; | |
2417 | ||
2418 | /* Implicit ctx ptr. */ | |
2419 | if (regno == BPF_REG_6) | |
2420 | return true; | |
2421 | ||
2422 | /* Explicit source could be any width. */ | |
2423 | return true; | |
2424 | } | |
2425 | ||
2426 | if (class == BPF_ST) | |
2427 | /* The only source register for BPF_ST is a ptr. */ | |
2428 | return true; | |
2429 | ||
2430 | /* Conservatively return true at default. */ | |
2431 | return true; | |
2432 | } | |
2433 | ||
83a28819 IL |
2434 | /* Return the regno defined by the insn, or -1. */ |
2435 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 2436 | { |
83a28819 IL |
2437 | switch (BPF_CLASS(insn->code)) { |
2438 | case BPF_JMP: | |
2439 | case BPF_JMP32: | |
2440 | case BPF_ST: | |
2441 | return -1; | |
2442 | case BPF_STX: | |
2443 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
2444 | (insn->imm & BPF_FETCH)) { | |
2445 | if (insn->imm == BPF_CMPXCHG) | |
2446 | return BPF_REG_0; | |
2447 | else | |
2448 | return insn->src_reg; | |
2449 | } else { | |
2450 | return -1; | |
2451 | } | |
2452 | default: | |
2453 | return insn->dst_reg; | |
2454 | } | |
b325fbca JW |
2455 | } |
2456 | ||
2457 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
2458 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
2459 | { | |
83a28819 IL |
2460 | int dst_reg = insn_def_regno(insn); |
2461 | ||
2462 | if (dst_reg == -1) | |
b325fbca JW |
2463 | return false; |
2464 | ||
83a28819 | 2465 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
2466 | } |
2467 | ||
5327ed3d JW |
2468 | static void mark_insn_zext(struct bpf_verifier_env *env, |
2469 | struct bpf_reg_state *reg) | |
2470 | { | |
2471 | s32 def_idx = reg->subreg_def; | |
2472 | ||
2473 | if (def_idx == DEF_NOT_SUBREG) | |
2474 | return; | |
2475 | ||
2476 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
2477 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
2478 | reg->subreg_def = DEF_NOT_SUBREG; | |
2479 | } | |
2480 | ||
dc503a8a | 2481 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
2482 | enum reg_arg_type t) |
2483 | { | |
f4d7e40a AS |
2484 | struct bpf_verifier_state *vstate = env->cur_state; |
2485 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 2486 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 2487 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 2488 | bool rw64; |
dc503a8a | 2489 | |
17a52670 | 2490 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 2491 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
2492 | return -EINVAL; |
2493 | } | |
2494 | ||
0f55f9ed CL |
2495 | mark_reg_scratched(env, regno); |
2496 | ||
c342dc10 | 2497 | reg = ®s[regno]; |
5327ed3d | 2498 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
2499 | if (t == SRC_OP) { |
2500 | /* check whether register used as source operand can be read */ | |
c342dc10 | 2501 | if (reg->type == NOT_INIT) { |
61bd5218 | 2502 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
2503 | return -EACCES; |
2504 | } | |
679c782d | 2505 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
2506 | if (regno == BPF_REG_FP) |
2507 | return 0; | |
2508 | ||
5327ed3d JW |
2509 | if (rw64) |
2510 | mark_insn_zext(env, reg); | |
2511 | ||
2512 | return mark_reg_read(env, reg, reg->parent, | |
2513 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
2514 | } else { |
2515 | /* check whether register used as dest operand can be written to */ | |
2516 | if (regno == BPF_REG_FP) { | |
61bd5218 | 2517 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
2518 | return -EACCES; |
2519 | } | |
c342dc10 | 2520 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 2521 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 2522 | if (t == DST_OP) |
61bd5218 | 2523 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
2524 | } |
2525 | return 0; | |
2526 | } | |
2527 | ||
b5dc0163 AS |
2528 | /* for any branch, call, exit record the history of jmps in the given state */ |
2529 | static int push_jmp_history(struct bpf_verifier_env *env, | |
2530 | struct bpf_verifier_state *cur) | |
2531 | { | |
2532 | u32 cnt = cur->jmp_history_cnt; | |
2533 | struct bpf_idx_pair *p; | |
ceb35b66 | 2534 | size_t alloc_size; |
b5dc0163 AS |
2535 | |
2536 | cnt++; | |
ceb35b66 KC |
2537 | alloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p))); |
2538 | p = krealloc(cur->jmp_history, alloc_size, GFP_USER); | |
b5dc0163 AS |
2539 | if (!p) |
2540 | return -ENOMEM; | |
2541 | p[cnt - 1].idx = env->insn_idx; | |
2542 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
2543 | cur->jmp_history = p; | |
2544 | cur->jmp_history_cnt = cnt; | |
2545 | return 0; | |
2546 | } | |
2547 | ||
2548 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
2549 | * history then previous instruction came from straight line execution. | |
2550 | */ | |
2551 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
2552 | u32 *history) | |
2553 | { | |
2554 | u32 cnt = *history; | |
2555 | ||
2556 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
2557 | i = st->jmp_history[cnt - 1].prev_idx; | |
2558 | (*history)--; | |
2559 | } else { | |
2560 | i--; | |
2561 | } | |
2562 | return i; | |
2563 | } | |
2564 | ||
e6ac2450 MKL |
2565 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
2566 | { | |
2567 | const struct btf_type *func; | |
2357672c | 2568 | struct btf *desc_btf; |
e6ac2450 MKL |
2569 | |
2570 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
2571 | return NULL; | |
2572 | ||
43bf0878 | 2573 | desc_btf = find_kfunc_desc_btf(data, insn->off); |
2357672c KKD |
2574 | if (IS_ERR(desc_btf)) |
2575 | return "<error>"; | |
2576 | ||
2577 | func = btf_type_by_id(desc_btf, insn->imm); | |
2578 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
2579 | } |
2580 | ||
b5dc0163 AS |
2581 | /* For given verifier state backtrack_insn() is called from the last insn to |
2582 | * the first insn. Its purpose is to compute a bitmask of registers and | |
2583 | * stack slots that needs precision in the parent verifier state. | |
2584 | */ | |
2585 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
2586 | u32 *reg_mask, u64 *stack_mask) | |
2587 | { | |
2588 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 2589 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
2590 | .cb_print = verbose, |
2591 | .private_data = env, | |
2592 | }; | |
2593 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
2594 | u8 class = BPF_CLASS(insn->code); | |
2595 | u8 opcode = BPF_OP(insn->code); | |
2596 | u8 mode = BPF_MODE(insn->code); | |
2597 | u32 dreg = 1u << insn->dst_reg; | |
2598 | u32 sreg = 1u << insn->src_reg; | |
2599 | u32 spi; | |
2600 | ||
2601 | if (insn->code == 0) | |
2602 | return 0; | |
496f3324 | 2603 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
2604 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
2605 | verbose(env, "%d: ", idx); | |
2606 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
2607 | } | |
2608 | ||
2609 | if (class == BPF_ALU || class == BPF_ALU64) { | |
2610 | if (!(*reg_mask & dreg)) | |
2611 | return 0; | |
2612 | if (opcode == BPF_MOV) { | |
2613 | if (BPF_SRC(insn->code) == BPF_X) { | |
2614 | /* dreg = sreg | |
2615 | * dreg needs precision after this insn | |
2616 | * sreg needs precision before this insn | |
2617 | */ | |
2618 | *reg_mask &= ~dreg; | |
2619 | *reg_mask |= sreg; | |
2620 | } else { | |
2621 | /* dreg = K | |
2622 | * dreg needs precision after this insn. | |
2623 | * Corresponding register is already marked | |
2624 | * as precise=true in this verifier state. | |
2625 | * No further markings in parent are necessary | |
2626 | */ | |
2627 | *reg_mask &= ~dreg; | |
2628 | } | |
2629 | } else { | |
2630 | if (BPF_SRC(insn->code) == BPF_X) { | |
2631 | /* dreg += sreg | |
2632 | * both dreg and sreg need precision | |
2633 | * before this insn | |
2634 | */ | |
2635 | *reg_mask |= sreg; | |
2636 | } /* else dreg += K | |
2637 | * dreg still needs precision before this insn | |
2638 | */ | |
2639 | } | |
2640 | } else if (class == BPF_LDX) { | |
2641 | if (!(*reg_mask & dreg)) | |
2642 | return 0; | |
2643 | *reg_mask &= ~dreg; | |
2644 | ||
2645 | /* scalars can only be spilled into stack w/o losing precision. | |
2646 | * Load from any other memory can be zero extended. | |
2647 | * The desire to keep that precision is already indicated | |
2648 | * by 'precise' mark in corresponding register of this state. | |
2649 | * No further tracking necessary. | |
2650 | */ | |
2651 | if (insn->src_reg != BPF_REG_FP) | |
2652 | return 0; | |
b5dc0163 AS |
2653 | |
2654 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
2655 | * that [fp - off] slot contains scalar that needs to be | |
2656 | * tracked with precision | |
2657 | */ | |
2658 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2659 | if (spi >= 64) { | |
2660 | verbose(env, "BUG spi %d\n", spi); | |
2661 | WARN_ONCE(1, "verifier backtracking bug"); | |
2662 | return -EFAULT; | |
2663 | } | |
2664 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 2665 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 2666 | if (*reg_mask & dreg) |
b3b50f05 | 2667 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
2668 | * to access memory. It means backtracking |
2669 | * encountered a case of pointer subtraction. | |
2670 | */ | |
2671 | return -ENOTSUPP; | |
2672 | /* scalars can only be spilled into stack */ | |
2673 | if (insn->dst_reg != BPF_REG_FP) | |
2674 | return 0; | |
b5dc0163 AS |
2675 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
2676 | if (spi >= 64) { | |
2677 | verbose(env, "BUG spi %d\n", spi); | |
2678 | WARN_ONCE(1, "verifier backtracking bug"); | |
2679 | return -EFAULT; | |
2680 | } | |
2681 | if (!(*stack_mask & (1ull << spi))) | |
2682 | return 0; | |
2683 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
2684 | if (class == BPF_STX) |
2685 | *reg_mask |= sreg; | |
b5dc0163 AS |
2686 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
2687 | if (opcode == BPF_CALL) { | |
2688 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2689 | return -ENOTSUPP; | |
be2ef816 AN |
2690 | /* BPF helpers that invoke callback subprogs are |
2691 | * equivalent to BPF_PSEUDO_CALL above | |
2692 | */ | |
2693 | if (insn->src_reg == 0 && is_callback_calling_function(insn->imm)) | |
2694 | return -ENOTSUPP; | |
b5dc0163 AS |
2695 | /* regular helper call sets R0 */ |
2696 | *reg_mask &= ~1; | |
2697 | if (*reg_mask & 0x3f) { | |
2698 | /* if backtracing was looking for registers R1-R5 | |
2699 | * they should have been found already. | |
2700 | */ | |
2701 | verbose(env, "BUG regs %x\n", *reg_mask); | |
2702 | WARN_ONCE(1, "verifier backtracking bug"); | |
2703 | return -EFAULT; | |
2704 | } | |
2705 | } else if (opcode == BPF_EXIT) { | |
2706 | return -ENOTSUPP; | |
2707 | } | |
2708 | } else if (class == BPF_LD) { | |
2709 | if (!(*reg_mask & dreg)) | |
2710 | return 0; | |
2711 | *reg_mask &= ~dreg; | |
2712 | /* It's ld_imm64 or ld_abs or ld_ind. | |
2713 | * For ld_imm64 no further tracking of precision | |
2714 | * into parent is necessary | |
2715 | */ | |
2716 | if (mode == BPF_IND || mode == BPF_ABS) | |
2717 | /* to be analyzed */ | |
2718 | return -ENOTSUPP; | |
b5dc0163 AS |
2719 | } |
2720 | return 0; | |
2721 | } | |
2722 | ||
2723 | /* the scalar precision tracking algorithm: | |
2724 | * . at the start all registers have precise=false. | |
2725 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
2726 | * . once precise value of the scalar register is used in: | |
2727 | * . ptr + scalar alu | |
2728 | * . if (scalar cond K|scalar) | |
2729 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
2730 | * backtrack through the verifier states and mark all registers and | |
2731 | * stack slots with spilled constants that these scalar regisers | |
2732 | * should be precise. | |
2733 | * . during state pruning two registers (or spilled stack slots) | |
2734 | * are equivalent if both are not precise. | |
2735 | * | |
2736 | * Note the verifier cannot simply walk register parentage chain, | |
2737 | * since many different registers and stack slots could have been | |
2738 | * used to compute single precise scalar. | |
2739 | * | |
2740 | * The approach of starting with precise=true for all registers and then | |
2741 | * backtrack to mark a register as not precise when the verifier detects | |
2742 | * that program doesn't care about specific value (e.g., when helper | |
2743 | * takes register as ARG_ANYTHING parameter) is not safe. | |
2744 | * | |
2745 | * It's ok to walk single parentage chain of the verifier states. | |
2746 | * It's possible that this backtracking will go all the way till 1st insn. | |
2747 | * All other branches will be explored for needing precision later. | |
2748 | * | |
2749 | * The backtracking needs to deal with cases like: | |
2750 | * 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) | |
2751 | * r9 -= r8 | |
2752 | * r5 = r9 | |
2753 | * if r5 > 0x79f goto pc+7 | |
2754 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
2755 | * r5 += 1 | |
2756 | * ... | |
2757 | * call bpf_perf_event_output#25 | |
2758 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
2759 | * | |
2760 | * and this case: | |
2761 | * r6 = 1 | |
2762 | * call foo // uses callee's r6 inside to compute r0 | |
2763 | * r0 += r6 | |
2764 | * if r0 == 0 goto | |
2765 | * | |
2766 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
2767 | * | |
2768 | * Also if parent's curframe > frame where backtracking started, | |
2769 | * the verifier need to mark registers in both frames, otherwise callees | |
2770 | * may incorrectly prune callers. This is similar to | |
2771 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
2772 | * | |
2773 | * For now backtracking falls back into conservative marking. | |
2774 | */ | |
2775 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
2776 | struct bpf_verifier_state *st) | |
2777 | { | |
2778 | struct bpf_func_state *func; | |
2779 | struct bpf_reg_state *reg; | |
2780 | int i, j; | |
2781 | ||
2782 | /* big hammer: mark all scalars precise in this path. | |
2783 | * pop_stack may still get !precise scalars. | |
f63181b6 AN |
2784 | * We also skip current state and go straight to first parent state, |
2785 | * because precision markings in current non-checkpointed state are | |
2786 | * not needed. See why in the comment in __mark_chain_precision below. | |
b5dc0163 | 2787 | */ |
f63181b6 | 2788 | for (st = st->parent; st; st = st->parent) { |
b5dc0163 AS |
2789 | for (i = 0; i <= st->curframe; i++) { |
2790 | func = st->frame[i]; | |
2791 | for (j = 0; j < BPF_REG_FP; j++) { | |
2792 | reg = &func->regs[j]; | |
2793 | if (reg->type != SCALAR_VALUE) | |
2794 | continue; | |
2795 | reg->precise = true; | |
2796 | } | |
2797 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 2798 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
2799 | continue; |
2800 | reg = &func->stack[j].spilled_ptr; | |
2801 | if (reg->type != SCALAR_VALUE) | |
2802 | continue; | |
2803 | reg->precise = true; | |
2804 | } | |
2805 | } | |
f63181b6 | 2806 | } |
b5dc0163 AS |
2807 | } |
2808 | ||
7a830b53 AN |
2809 | static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
2810 | { | |
2811 | struct bpf_func_state *func; | |
2812 | struct bpf_reg_state *reg; | |
2813 | int i, j; | |
2814 | ||
2815 | for (i = 0; i <= st->curframe; i++) { | |
2816 | func = st->frame[i]; | |
2817 | for (j = 0; j < BPF_REG_FP; j++) { | |
2818 | reg = &func->regs[j]; | |
2819 | if (reg->type != SCALAR_VALUE) | |
2820 | continue; | |
2821 | reg->precise = false; | |
2822 | } | |
2823 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
2824 | if (!is_spilled_reg(&func->stack[j])) | |
2825 | continue; | |
2826 | reg = &func->stack[j].spilled_ptr; | |
2827 | if (reg->type != SCALAR_VALUE) | |
2828 | continue; | |
2829 | reg->precise = false; | |
2830 | } | |
2831 | } | |
2832 | } | |
2833 | ||
f63181b6 AN |
2834 | /* |
2835 | * __mark_chain_precision() backtracks BPF program instruction sequence and | |
2836 | * chain of verifier states making sure that register *regno* (if regno >= 0) | |
2837 | * and/or stack slot *spi* (if spi >= 0) are marked as precisely tracked | |
2838 | * SCALARS, as well as any other registers and slots that contribute to | |
2839 | * a tracked state of given registers/stack slots, depending on specific BPF | |
2840 | * assembly instructions (see backtrack_insns() for exact instruction handling | |
2841 | * logic). This backtracking relies on recorded jmp_history and is able to | |
2842 | * traverse entire chain of parent states. This process ends only when all the | |
2843 | * necessary registers/slots and their transitive dependencies are marked as | |
2844 | * precise. | |
2845 | * | |
2846 | * One important and subtle aspect is that precise marks *do not matter* in | |
2847 | * the currently verified state (current state). It is important to understand | |
2848 | * why this is the case. | |
2849 | * | |
2850 | * First, note that current state is the state that is not yet "checkpointed", | |
2851 | * i.e., it is not yet put into env->explored_states, and it has no children | |
2852 | * states as well. It's ephemeral, and can end up either a) being discarded if | |
2853 | * compatible explored state is found at some point or BPF_EXIT instruction is | |
2854 | * reached or b) checkpointed and put into env->explored_states, branching out | |
2855 | * into one or more children states. | |
2856 | * | |
2857 | * In the former case, precise markings in current state are completely | |
2858 | * ignored by state comparison code (see regsafe() for details). Only | |
2859 | * checkpointed ("old") state precise markings are important, and if old | |
2860 | * state's register/slot is precise, regsafe() assumes current state's | |
2861 | * register/slot as precise and checks value ranges exactly and precisely. If | |
2862 | * states turn out to be compatible, current state's necessary precise | |
2863 | * markings and any required parent states' precise markings are enforced | |
2864 | * after the fact with propagate_precision() logic, after the fact. But it's | |
2865 | * important to realize that in this case, even after marking current state | |
2866 | * registers/slots as precise, we immediately discard current state. So what | |
2867 | * actually matters is any of the precise markings propagated into current | |
2868 | * state's parent states, which are always checkpointed (due to b) case above). | |
2869 | * As such, for scenario a) it doesn't matter if current state has precise | |
2870 | * markings set or not. | |
2871 | * | |
2872 | * Now, for the scenario b), checkpointing and forking into child(ren) | |
2873 | * state(s). Note that before current state gets to checkpointing step, any | |
2874 | * processed instruction always assumes precise SCALAR register/slot | |
2875 | * knowledge: if precise value or range is useful to prune jump branch, BPF | |
2876 | * verifier takes this opportunity enthusiastically. Similarly, when | |
2877 | * register's value is used to calculate offset or memory address, exact | |
2878 | * knowledge of SCALAR range is assumed, checked, and enforced. So, similar to | |
2879 | * what we mentioned above about state comparison ignoring precise markings | |
2880 | * during state comparison, BPF verifier ignores and also assumes precise | |
2881 | * markings *at will* during instruction verification process. But as verifier | |
2882 | * assumes precision, it also propagates any precision dependencies across | |
2883 | * parent states, which are not yet finalized, so can be further restricted | |
2884 | * based on new knowledge gained from restrictions enforced by their children | |
2885 | * states. This is so that once those parent states are finalized, i.e., when | |
2886 | * they have no more active children state, state comparison logic in | |
2887 | * is_state_visited() would enforce strict and precise SCALAR ranges, if | |
2888 | * required for correctness. | |
2889 | * | |
2890 | * To build a bit more intuition, note also that once a state is checkpointed, | |
2891 | * the path we took to get to that state is not important. This is crucial | |
2892 | * property for state pruning. When state is checkpointed and finalized at | |
2893 | * some instruction index, it can be correctly and safely used to "short | |
2894 | * circuit" any *compatible* state that reaches exactly the same instruction | |
2895 | * index. I.e., if we jumped to that instruction from a completely different | |
2896 | * code path than original finalized state was derived from, it doesn't | |
2897 | * matter, current state can be discarded because from that instruction | |
2898 | * forward having a compatible state will ensure we will safely reach the | |
2899 | * exit. States describe preconditions for further exploration, but completely | |
2900 | * forget the history of how we got here. | |
2901 | * | |
2902 | * This also means that even if we needed precise SCALAR range to get to | |
2903 | * finalized state, but from that point forward *that same* SCALAR register is | |
2904 | * never used in a precise context (i.e., it's precise value is not needed for | |
2905 | * correctness), it's correct and safe to mark such register as "imprecise" | |
2906 | * (i.e., precise marking set to false). This is what we rely on when we do | |
2907 | * not set precise marking in current state. If no child state requires | |
2908 | * precision for any given SCALAR register, it's safe to dictate that it can | |
2909 | * be imprecise. If any child state does require this register to be precise, | |
2910 | * we'll mark it precise later retroactively during precise markings | |
2911 | * propagation from child state to parent states. | |
7a830b53 AN |
2912 | * |
2913 | * Skipping precise marking setting in current state is a mild version of | |
2914 | * relying on the above observation. But we can utilize this property even | |
2915 | * more aggressively by proactively forgetting any precise marking in the | |
2916 | * current state (which we inherited from the parent state), right before we | |
2917 | * checkpoint it and branch off into new child state. This is done by | |
2918 | * mark_all_scalars_imprecise() to hopefully get more permissive and generic | |
2919 | * finalized states which help in short circuiting more future states. | |
f63181b6 | 2920 | */ |
529409ea | 2921 | static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int regno, |
a3ce685d | 2922 | int spi) |
b5dc0163 AS |
2923 | { |
2924 | struct bpf_verifier_state *st = env->cur_state; | |
2925 | int first_idx = st->first_insn_idx; | |
2926 | int last_idx = env->insn_idx; | |
2927 | struct bpf_func_state *func; | |
2928 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2929 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2930 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2931 | bool skip_first = true; |
a3ce685d | 2932 | bool new_marks = false; |
b5dc0163 AS |
2933 | int i, err; |
2934 | ||
2c78ee89 | 2935 | if (!env->bpf_capable) |
b5dc0163 AS |
2936 | return 0; |
2937 | ||
f63181b6 AN |
2938 | /* Do sanity checks against current state of register and/or stack |
2939 | * slot, but don't set precise flag in current state, as precision | |
2940 | * tracking in the current state is unnecessary. | |
2941 | */ | |
529409ea | 2942 | func = st->frame[frame]; |
a3ce685d AS |
2943 | if (regno >= 0) { |
2944 | reg = &func->regs[regno]; | |
2945 | if (reg->type != SCALAR_VALUE) { | |
2946 | WARN_ONCE(1, "backtracing misuse"); | |
2947 | return -EFAULT; | |
2948 | } | |
f63181b6 | 2949 | new_marks = true; |
b5dc0163 | 2950 | } |
b5dc0163 | 2951 | |
a3ce685d | 2952 | while (spi >= 0) { |
27113c59 | 2953 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
2954 | stack_mask = 0; |
2955 | break; | |
2956 | } | |
2957 | reg = &func->stack[spi].spilled_ptr; | |
2958 | if (reg->type != SCALAR_VALUE) { | |
2959 | stack_mask = 0; | |
2960 | break; | |
2961 | } | |
f63181b6 | 2962 | new_marks = true; |
a3ce685d AS |
2963 | break; |
2964 | } | |
2965 | ||
2966 | if (!new_marks) | |
2967 | return 0; | |
2968 | if (!reg_mask && !stack_mask) | |
2969 | return 0; | |
be2ef816 | 2970 | |
b5dc0163 AS |
2971 | for (;;) { |
2972 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2973 | u32 history = st->jmp_history_cnt; |
2974 | ||
496f3324 | 2975 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 | 2976 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
be2ef816 AN |
2977 | |
2978 | if (last_idx < 0) { | |
2979 | /* we are at the entry into subprog, which | |
2980 | * is expected for global funcs, but only if | |
2981 | * requested precise registers are R1-R5 | |
2982 | * (which are global func's input arguments) | |
2983 | */ | |
2984 | if (st->curframe == 0 && | |
2985 | st->frame[0]->subprogno > 0 && | |
2986 | st->frame[0]->callsite == BPF_MAIN_FUNC && | |
2987 | stack_mask == 0 && (reg_mask & ~0x3e) == 0) { | |
2988 | bitmap_from_u64(mask, reg_mask); | |
2989 | for_each_set_bit(i, mask, 32) { | |
2990 | reg = &st->frame[0]->regs[i]; | |
2991 | if (reg->type != SCALAR_VALUE) { | |
2992 | reg_mask &= ~(1u << i); | |
2993 | continue; | |
2994 | } | |
2995 | reg->precise = true; | |
2996 | } | |
2997 | return 0; | |
2998 | } | |
2999 | ||
3000 | verbose(env, "BUG backtracing func entry subprog %d reg_mask %x stack_mask %llx\n", | |
3001 | st->frame[0]->subprogno, reg_mask, stack_mask); | |
3002 | WARN_ONCE(1, "verifier backtracking bug"); | |
3003 | return -EFAULT; | |
3004 | } | |
3005 | ||
b5dc0163 AS |
3006 | for (i = last_idx;;) { |
3007 | if (skip_first) { | |
3008 | err = 0; | |
3009 | skip_first = false; | |
3010 | } else { | |
3011 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
3012 | } | |
3013 | if (err == -ENOTSUPP) { | |
3014 | mark_all_scalars_precise(env, st); | |
3015 | return 0; | |
3016 | } else if (err) { | |
3017 | return err; | |
3018 | } | |
3019 | if (!reg_mask && !stack_mask) | |
3020 | /* Found assignment(s) into tracked register in this state. | |
3021 | * Since this state is already marked, just return. | |
3022 | * Nothing to be tracked further in the parent state. | |
3023 | */ | |
3024 | return 0; | |
3025 | if (i == first_idx) | |
3026 | break; | |
3027 | i = get_prev_insn_idx(st, i, &history); | |
3028 | if (i >= env->prog->len) { | |
3029 | /* This can happen if backtracking reached insn 0 | |
3030 | * and there are still reg_mask or stack_mask | |
3031 | * to backtrack. | |
3032 | * It means the backtracking missed the spot where | |
3033 | * particular register was initialized with a constant. | |
3034 | */ | |
3035 | verbose(env, "BUG backtracking idx %d\n", i); | |
3036 | WARN_ONCE(1, "verifier backtracking bug"); | |
3037 | return -EFAULT; | |
3038 | } | |
3039 | } | |
3040 | st = st->parent; | |
3041 | if (!st) | |
3042 | break; | |
3043 | ||
a3ce685d | 3044 | new_marks = false; |
529409ea | 3045 | func = st->frame[frame]; |
b5dc0163 AS |
3046 | bitmap_from_u64(mask, reg_mask); |
3047 | for_each_set_bit(i, mask, 32) { | |
3048 | reg = &func->regs[i]; | |
a3ce685d AS |
3049 | if (reg->type != SCALAR_VALUE) { |
3050 | reg_mask &= ~(1u << i); | |
b5dc0163 | 3051 | continue; |
a3ce685d | 3052 | } |
b5dc0163 AS |
3053 | if (!reg->precise) |
3054 | new_marks = true; | |
3055 | reg->precise = true; | |
3056 | } | |
3057 | ||
3058 | bitmap_from_u64(mask, stack_mask); | |
3059 | for_each_set_bit(i, mask, 64) { | |
3060 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
3061 | /* the sequence of instructions: |
3062 | * 2: (bf) r3 = r10 | |
3063 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
3064 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
3065 | * doesn't contain jmps. It's backtracked | |
3066 | * as a single block. | |
3067 | * During backtracking insn 3 is not recognized as | |
3068 | * stack access, so at the end of backtracking | |
3069 | * stack slot fp-8 is still marked in stack_mask. | |
3070 | * However the parent state may not have accessed | |
3071 | * fp-8 and it's "unallocated" stack space. | |
3072 | * In such case fallback to conservative. | |
b5dc0163 | 3073 | */ |
2339cd6c AS |
3074 | mark_all_scalars_precise(env, st); |
3075 | return 0; | |
b5dc0163 AS |
3076 | } |
3077 | ||
27113c59 | 3078 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 3079 | stack_mask &= ~(1ull << i); |
b5dc0163 | 3080 | continue; |
a3ce685d | 3081 | } |
b5dc0163 | 3082 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
3083 | if (reg->type != SCALAR_VALUE) { |
3084 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 3085 | continue; |
a3ce685d | 3086 | } |
b5dc0163 AS |
3087 | if (!reg->precise) |
3088 | new_marks = true; | |
3089 | reg->precise = true; | |
3090 | } | |
496f3324 | 3091 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 3092 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
3093 | new_marks ? "didn't have" : "already had", |
3094 | reg_mask, stack_mask); | |
2e576648 | 3095 | print_verifier_state(env, func, true); |
b5dc0163 AS |
3096 | } |
3097 | ||
a3ce685d AS |
3098 | if (!reg_mask && !stack_mask) |
3099 | break; | |
b5dc0163 AS |
3100 | if (!new_marks) |
3101 | break; | |
3102 | ||
3103 | last_idx = st->last_insn_idx; | |
3104 | first_idx = st->first_insn_idx; | |
3105 | } | |
3106 | return 0; | |
3107 | } | |
3108 | ||
eb1f7f71 | 3109 | int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
a3ce685d | 3110 | { |
529409ea | 3111 | return __mark_chain_precision(env, env->cur_state->curframe, regno, -1); |
a3ce685d AS |
3112 | } |
3113 | ||
529409ea | 3114 | static int mark_chain_precision_frame(struct bpf_verifier_env *env, int frame, int regno) |
a3ce685d | 3115 | { |
529409ea | 3116 | return __mark_chain_precision(env, frame, regno, -1); |
a3ce685d AS |
3117 | } |
3118 | ||
529409ea | 3119 | static int mark_chain_precision_stack_frame(struct bpf_verifier_env *env, int frame, int spi) |
a3ce685d | 3120 | { |
529409ea | 3121 | return __mark_chain_precision(env, frame, -1, spi); |
a3ce685d | 3122 | } |
b5dc0163 | 3123 | |
1be7f75d AS |
3124 | static bool is_spillable_regtype(enum bpf_reg_type type) |
3125 | { | |
c25b2ae1 | 3126 | switch (base_type(type)) { |
1be7f75d | 3127 | case PTR_TO_MAP_VALUE: |
1be7f75d AS |
3128 | case PTR_TO_STACK: |
3129 | case PTR_TO_CTX: | |
969bf05e | 3130 | case PTR_TO_PACKET: |
de8f3a83 | 3131 | case PTR_TO_PACKET_META: |
969bf05e | 3132 | case PTR_TO_PACKET_END: |
d58e468b | 3133 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 3134 | case CONST_PTR_TO_MAP: |
c64b7983 | 3135 | case PTR_TO_SOCKET: |
46f8bc92 | 3136 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 3137 | case PTR_TO_TCP_SOCK: |
fada7fdc | 3138 | case PTR_TO_XDP_SOCK: |
65726b5b | 3139 | case PTR_TO_BTF_ID: |
20b2aff4 | 3140 | case PTR_TO_BUF: |
744ea4e3 | 3141 | case PTR_TO_MEM: |
69c087ba YS |
3142 | case PTR_TO_FUNC: |
3143 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
3144 | return true; |
3145 | default: | |
3146 | return false; | |
3147 | } | |
3148 | } | |
3149 | ||
cc2b14d5 AS |
3150 | /* Does this register contain a constant zero? */ |
3151 | static bool register_is_null(struct bpf_reg_state *reg) | |
3152 | { | |
3153 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
3154 | } | |
3155 | ||
f7cf25b2 AS |
3156 | static bool register_is_const(struct bpf_reg_state *reg) |
3157 | { | |
3158 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
3159 | } | |
3160 | ||
5689d49b YS |
3161 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
3162 | { | |
3163 | return tnum_is_unknown(reg->var_off) && | |
3164 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
3165 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
3166 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
3167 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
3168 | } | |
3169 | ||
3170 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
3171 | { | |
3172 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
3173 | } | |
3174 | ||
6e7e63cb JH |
3175 | static bool __is_pointer_value(bool allow_ptr_leaks, |
3176 | const struct bpf_reg_state *reg) | |
3177 | { | |
3178 | if (allow_ptr_leaks) | |
3179 | return false; | |
3180 | ||
3181 | return reg->type != SCALAR_VALUE; | |
3182 | } | |
3183 | ||
f7cf25b2 | 3184 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
3185 | int spi, struct bpf_reg_state *reg, |
3186 | int size) | |
f7cf25b2 AS |
3187 | { |
3188 | int i; | |
3189 | ||
3190 | state->stack[spi].spilled_ptr = *reg; | |
354e8f19 MKL |
3191 | if (size == BPF_REG_SIZE) |
3192 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 3193 | |
354e8f19 MKL |
3194 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
3195 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 3196 | |
354e8f19 MKL |
3197 | /* size < 8 bytes spill */ |
3198 | for (; i; i--) | |
3199 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
3200 | } |
3201 | ||
01f810ac | 3202 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
3203 | * stack boundary and alignment are checked in check_mem_access() |
3204 | */ | |
01f810ac AM |
3205 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
3206 | /* stack frame we're writing to */ | |
3207 | struct bpf_func_state *state, | |
3208 | int off, int size, int value_regno, | |
3209 | int insn_idx) | |
17a52670 | 3210 | { |
f4d7e40a | 3211 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 3212 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 3213 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 3214 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 3215 | |
c69431aa | 3216 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
3217 | if (err) |
3218 | return err; | |
9c399760 AS |
3219 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
3220 | * so it's aligned access and [off, off + size) are within stack limits | |
3221 | */ | |
638f5b90 AS |
3222 | if (!env->allow_ptr_leaks && |
3223 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
3224 | size != BPF_REG_SIZE) { | |
3225 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
3226 | return -EACCES; | |
3227 | } | |
17a52670 | 3228 | |
f4d7e40a | 3229 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
3230 | if (value_regno >= 0) |
3231 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
3232 | if (!env->bypass_spec_v4) { |
3233 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
3234 | ||
3235 | for (i = 0; i < size; i++) { | |
3236 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { | |
3237 | sanitize = true; | |
3238 | break; | |
3239 | } | |
3240 | } | |
3241 | ||
3242 | if (sanitize) | |
3243 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
3244 | } | |
17a52670 | 3245 | |
0f55f9ed | 3246 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 3247 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 3248 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
3249 | if (dst_reg != BPF_REG_FP) { |
3250 | /* The backtracking logic can only recognize explicit | |
3251 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 3252 | * scalar via different register has to be conservative. |
b5dc0163 AS |
3253 | * Backtrack from here and mark all registers as precise |
3254 | * that contributed into 'reg' being a constant. | |
3255 | */ | |
3256 | err = mark_chain_precision(env, value_regno); | |
3257 | if (err) | |
3258 | return err; | |
3259 | } | |
354e8f19 | 3260 | save_register_state(state, spi, reg, size); |
f7cf25b2 | 3261 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 3262 | /* register containing pointer is being spilled into stack */ |
9c399760 | 3263 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 3264 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 3265 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
3266 | return -EACCES; |
3267 | } | |
f7cf25b2 | 3268 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
3269 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
3270 | return -EINVAL; | |
3271 | } | |
354e8f19 | 3272 | save_register_state(state, spi, reg, size); |
9c399760 | 3273 | } else { |
cc2b14d5 AS |
3274 | u8 type = STACK_MISC; |
3275 | ||
679c782d EC |
3276 | /* regular write of data into stack destroys any spilled ptr */ |
3277 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d | 3278 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
27113c59 | 3279 | if (is_spilled_reg(&state->stack[spi])) |
0bae2d4d | 3280 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 3281 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 3282 | |
cc2b14d5 AS |
3283 | /* only mark the slot as written if all 8 bytes were written |
3284 | * otherwise read propagation may incorrectly stop too soon | |
3285 | * when stack slots are partially written. | |
3286 | * This heuristic means that read propagation will be | |
3287 | * conservative, since it will add reg_live_read marks | |
3288 | * to stack slots all the way to first state when programs | |
3289 | * writes+reads less than 8 bytes | |
3290 | */ | |
3291 | if (size == BPF_REG_SIZE) | |
3292 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
3293 | ||
3294 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
3295 | if (reg && register_is_null(reg)) { |
3296 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3297 | err = mark_chain_precision(env, value_regno); | |
3298 | if (err) | |
3299 | return err; | |
cc2b14d5 | 3300 | type = STACK_ZERO; |
b5dc0163 | 3301 | } |
cc2b14d5 | 3302 | |
0bae2d4d | 3303 | /* Mark slots affected by this stack write. */ |
9c399760 | 3304 | for (i = 0; i < size; i++) |
638f5b90 | 3305 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 3306 | type; |
17a52670 AS |
3307 | } |
3308 | return 0; | |
3309 | } | |
3310 | ||
01f810ac AM |
3311 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
3312 | * known to contain a variable offset. | |
3313 | * This function checks whether the write is permitted and conservatively | |
3314 | * tracks the effects of the write, considering that each stack slot in the | |
3315 | * dynamic range is potentially written to. | |
3316 | * | |
3317 | * 'off' includes 'regno->off'. | |
3318 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
3319 | * the stack. | |
3320 | * | |
3321 | * Spilled pointers in range are not marked as written because we don't know | |
3322 | * what's going to be actually written. This means that read propagation for | |
3323 | * future reads cannot be terminated by this write. | |
3324 | * | |
3325 | * For privileged programs, uninitialized stack slots are considered | |
3326 | * initialized by this write (even though we don't know exactly what offsets | |
3327 | * are going to be written to). The idea is that we don't want the verifier to | |
3328 | * reject future reads that access slots written to through variable offsets. | |
3329 | */ | |
3330 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
3331 | /* func where register points to */ | |
3332 | struct bpf_func_state *state, | |
3333 | int ptr_regno, int off, int size, | |
3334 | int value_regno, int insn_idx) | |
3335 | { | |
3336 | struct bpf_func_state *cur; /* state of the current function */ | |
3337 | int min_off, max_off; | |
3338 | int i, err; | |
3339 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
3340 | bool writing_zero = false; | |
3341 | /* set if the fact that we're writing a zero is used to let any | |
3342 | * stack slots remain STACK_ZERO | |
3343 | */ | |
3344 | bool zero_used = false; | |
3345 | ||
3346 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
3347 | ptr_reg = &cur->regs[ptr_regno]; | |
3348 | min_off = ptr_reg->smin_value + off; | |
3349 | max_off = ptr_reg->smax_value + off + size; | |
3350 | if (value_regno >= 0) | |
3351 | value_reg = &cur->regs[value_regno]; | |
3352 | if (value_reg && register_is_null(value_reg)) | |
3353 | writing_zero = true; | |
3354 | ||
c69431aa | 3355 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
3356 | if (err) |
3357 | return err; | |
3358 | ||
3359 | ||
3360 | /* Variable offset writes destroy any spilled pointers in range. */ | |
3361 | for (i = min_off; i < max_off; i++) { | |
3362 | u8 new_type, *stype; | |
3363 | int slot, spi; | |
3364 | ||
3365 | slot = -i - 1; | |
3366 | spi = slot / BPF_REG_SIZE; | |
3367 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 3368 | mark_stack_slot_scratched(env, spi); |
01f810ac | 3369 | |
f5e477a8 KKD |
3370 | if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) { |
3371 | /* Reject the write if range we may write to has not | |
3372 | * been initialized beforehand. If we didn't reject | |
3373 | * here, the ptr status would be erased below (even | |
3374 | * though not all slots are actually overwritten), | |
3375 | * possibly opening the door to leaks. | |
3376 | * | |
3377 | * We do however catch STACK_INVALID case below, and | |
3378 | * only allow reading possibly uninitialized memory | |
3379 | * later for CAP_PERFMON, as the write may not happen to | |
3380 | * that slot. | |
01f810ac AM |
3381 | */ |
3382 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
3383 | insn_idx, i); | |
3384 | return -EINVAL; | |
3385 | } | |
3386 | ||
3387 | /* Erase all spilled pointers. */ | |
3388 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
3389 | ||
3390 | /* Update the slot type. */ | |
3391 | new_type = STACK_MISC; | |
3392 | if (writing_zero && *stype == STACK_ZERO) { | |
3393 | new_type = STACK_ZERO; | |
3394 | zero_used = true; | |
3395 | } | |
3396 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
3397 | * pretend that it will be initialized by this write. The slot | |
3398 | * might not actually be written to, and so if we mark it as | |
3399 | * initialized future reads might leak uninitialized memory. | |
3400 | * For privileged programs, we will accept such reads to slots | |
3401 | * that may or may not be written because, if we're reject | |
3402 | * them, the error would be too confusing. | |
3403 | */ | |
3404 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
3405 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
3406 | insn_idx, i); | |
3407 | return -EINVAL; | |
3408 | } | |
3409 | *stype = new_type; | |
3410 | } | |
3411 | if (zero_used) { | |
3412 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3413 | err = mark_chain_precision(env, value_regno); | |
3414 | if (err) | |
3415 | return err; | |
3416 | } | |
3417 | return 0; | |
3418 | } | |
3419 | ||
3420 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
3421 | * max_off), we set the register's type according to the types of the | |
3422 | * respective stack slots. If all the stack values are known to be zeros, then | |
3423 | * so is the destination reg. Otherwise, the register is considered to be | |
3424 | * SCALAR. This function does not deal with register filling; the caller must | |
3425 | * ensure that all spilled registers in the stack range have been marked as | |
3426 | * read. | |
3427 | */ | |
3428 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
3429 | /* func where src register points to */ | |
3430 | struct bpf_func_state *ptr_state, | |
3431 | int min_off, int max_off, int dst_regno) | |
3432 | { | |
3433 | struct bpf_verifier_state *vstate = env->cur_state; | |
3434 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3435 | int i, slot, spi; | |
3436 | u8 *stype; | |
3437 | int zeros = 0; | |
3438 | ||
3439 | for (i = min_off; i < max_off; i++) { | |
3440 | slot = -i - 1; | |
3441 | spi = slot / BPF_REG_SIZE; | |
3442 | stype = ptr_state->stack[spi].slot_type; | |
3443 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
3444 | break; | |
3445 | zeros++; | |
3446 | } | |
3447 | if (zeros == max_off - min_off) { | |
3448 | /* any access_size read into register is zero extended, | |
3449 | * so the whole register == const_zero | |
3450 | */ | |
3451 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
3452 | /* backtracking doesn't support STACK_ZERO yet, | |
3453 | * so mark it precise here, so that later | |
3454 | * backtracking can stop here. | |
3455 | * Backtracking may not need this if this register | |
3456 | * doesn't participate in pointer adjustment. | |
3457 | * Forward propagation of precise flag is not | |
3458 | * necessary either. This mark is only to stop | |
3459 | * backtracking. Any register that contributed | |
3460 | * to const 0 was marked precise before spill. | |
3461 | */ | |
3462 | state->regs[dst_regno].precise = true; | |
3463 | } else { | |
3464 | /* have read misc data from the stack */ | |
3465 | mark_reg_unknown(env, state->regs, dst_regno); | |
3466 | } | |
3467 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
3468 | } | |
3469 | ||
3470 | /* Read the stack at 'off' and put the results into the register indicated by | |
3471 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
3472 | * spilled reg. | |
3473 | * | |
3474 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
3475 | * register. | |
3476 | * | |
3477 | * The access is assumed to be within the current stack bounds. | |
3478 | */ | |
3479 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
3480 | /* func where src register points to */ | |
3481 | struct bpf_func_state *reg_state, | |
3482 | int off, int size, int dst_regno) | |
17a52670 | 3483 | { |
f4d7e40a AS |
3484 | struct bpf_verifier_state *vstate = env->cur_state; |
3485 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 3486 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 3487 | struct bpf_reg_state *reg; |
354e8f19 | 3488 | u8 *stype, type; |
17a52670 | 3489 | |
f4d7e40a | 3490 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 3491 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 3492 | |
27113c59 | 3493 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
3494 | u8 spill_size = 1; |
3495 | ||
3496 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
3497 | spill_size++; | |
354e8f19 | 3498 | |
f30d4968 | 3499 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
3500 | if (reg->type != SCALAR_VALUE) { |
3501 | verbose_linfo(env, env->insn_idx, "; "); | |
3502 | verbose(env, "invalid size of register fill\n"); | |
3503 | return -EACCES; | |
3504 | } | |
354e8f19 MKL |
3505 | |
3506 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
3507 | if (dst_regno < 0) | |
3508 | return 0; | |
3509 | ||
f30d4968 | 3510 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
3511 | /* The earlier check_reg_arg() has decided the |
3512 | * subreg_def for this insn. Save it first. | |
3513 | */ | |
3514 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
3515 | ||
3516 | state->regs[dst_regno] = *reg; | |
3517 | state->regs[dst_regno].subreg_def = subreg_def; | |
3518 | } else { | |
3519 | for (i = 0; i < size; i++) { | |
3520 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
3521 | if (type == STACK_SPILL) | |
3522 | continue; | |
3523 | if (type == STACK_MISC) | |
3524 | continue; | |
3525 | verbose(env, "invalid read from stack off %d+%d size %d\n", | |
3526 | off, i, size); | |
3527 | return -EACCES; | |
3528 | } | |
01f810ac | 3529 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 3530 | } |
354e8f19 | 3531 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 3532 | return 0; |
17a52670 | 3533 | } |
17a52670 | 3534 | |
01f810ac | 3535 | if (dst_regno >= 0) { |
17a52670 | 3536 | /* restore register state from stack */ |
01f810ac | 3537 | state->regs[dst_regno] = *reg; |
2f18f62e AS |
3538 | /* mark reg as written since spilled pointer state likely |
3539 | * has its liveness marks cleared by is_state_visited() | |
3540 | * which resets stack/reg liveness for state transitions | |
3541 | */ | |
01f810ac | 3542 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 3543 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 3544 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
3545 | * it is acceptable to use this value as a SCALAR_VALUE |
3546 | * (e.g. for XADD). | |
3547 | * We must not allow unprivileged callers to do that | |
3548 | * with spilled pointers. | |
3549 | */ | |
3550 | verbose(env, "leaking pointer from stack off %d\n", | |
3551 | off); | |
3552 | return -EACCES; | |
dc503a8a | 3553 | } |
f7cf25b2 | 3554 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
3555 | } else { |
3556 | for (i = 0; i < size; i++) { | |
01f810ac AM |
3557 | type = stype[(slot - i) % BPF_REG_SIZE]; |
3558 | if (type == STACK_MISC) | |
cc2b14d5 | 3559 | continue; |
01f810ac | 3560 | if (type == STACK_ZERO) |
cc2b14d5 | 3561 | continue; |
cc2b14d5 AS |
3562 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
3563 | off, i, size); | |
3564 | return -EACCES; | |
3565 | } | |
f7cf25b2 | 3566 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
3567 | if (dst_regno >= 0) |
3568 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 3569 | } |
f7cf25b2 | 3570 | return 0; |
17a52670 AS |
3571 | } |
3572 | ||
61df10c7 | 3573 | enum bpf_access_src { |
01f810ac AM |
3574 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
3575 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
3576 | }; | |
3577 | ||
3578 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
3579 | int regno, int off, int access_size, | |
3580 | bool zero_size_allowed, | |
61df10c7 | 3581 | enum bpf_access_src type, |
01f810ac AM |
3582 | struct bpf_call_arg_meta *meta); |
3583 | ||
3584 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
3585 | { | |
3586 | return cur_regs(env) + regno; | |
3587 | } | |
3588 | ||
3589 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
3590 | * 'dst_regno'. | |
3591 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
3592 | * but not its variable offset. | |
3593 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
3594 | * | |
3595 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
3596 | * filling registers (i.e. reads of spilled register cannot be detected when | |
3597 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
3598 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
3599 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
3600 | * instead. | |
3601 | */ | |
3602 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
3603 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 3604 | { |
01f810ac AM |
3605 | /* The state of the source register. */ |
3606 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3607 | struct bpf_func_state *ptr_state = func(env, reg); | |
3608 | int err; | |
3609 | int min_off, max_off; | |
3610 | ||
3611 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 3612 | */ |
01f810ac AM |
3613 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
3614 | false, ACCESS_DIRECT, NULL); | |
3615 | if (err) | |
3616 | return err; | |
3617 | ||
3618 | min_off = reg->smin_value + off; | |
3619 | max_off = reg->smax_value + off; | |
3620 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
3621 | return 0; | |
3622 | } | |
3623 | ||
3624 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
3625 | * check_stack_read_var_off. | |
3626 | * | |
3627 | * The caller must ensure that the offset falls within the allocated stack | |
3628 | * bounds. | |
3629 | * | |
3630 | * 'dst_regno' is a register which will receive the value from the stack. It | |
3631 | * can be -1, meaning that the read value is not going to a register. | |
3632 | */ | |
3633 | static int check_stack_read(struct bpf_verifier_env *env, | |
3634 | int ptr_regno, int off, int size, | |
3635 | int dst_regno) | |
3636 | { | |
3637 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3638 | struct bpf_func_state *state = func(env, reg); | |
3639 | int err; | |
3640 | /* Some accesses are only permitted with a static offset. */ | |
3641 | bool var_off = !tnum_is_const(reg->var_off); | |
3642 | ||
3643 | /* The offset is required to be static when reads don't go to a | |
3644 | * register, in order to not leak pointers (see | |
3645 | * check_stack_read_fixed_off). | |
3646 | */ | |
3647 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
3648 | char tn_buf[48]; |
3649 | ||
3650 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 3651 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
3652 | tn_buf, off, size); |
3653 | return -EACCES; | |
3654 | } | |
01f810ac AM |
3655 | /* Variable offset is prohibited for unprivileged mode for simplicity |
3656 | * since it requires corresponding support in Spectre masking for stack | |
3657 | * ALU. See also retrieve_ptr_limit(). | |
3658 | */ | |
3659 | if (!env->bypass_spec_v1 && var_off) { | |
3660 | char tn_buf[48]; | |
e4298d25 | 3661 | |
01f810ac AM |
3662 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3663 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", | |
3664 | ptr_regno, tn_buf); | |
e4298d25 DB |
3665 | return -EACCES; |
3666 | } | |
3667 | ||
01f810ac AM |
3668 | if (!var_off) { |
3669 | off += reg->var_off.value; | |
3670 | err = check_stack_read_fixed_off(env, state, off, size, | |
3671 | dst_regno); | |
3672 | } else { | |
3673 | /* Variable offset stack reads need more conservative handling | |
3674 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
3675 | * branch. | |
3676 | */ | |
3677 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
3678 | dst_regno); | |
3679 | } | |
3680 | return err; | |
3681 | } | |
3682 | ||
3683 | ||
3684 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
3685 | * check_stack_write_var_off. | |
3686 | * | |
3687 | * 'ptr_regno' is the register used as a pointer into the stack. | |
3688 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
3689 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
3690 | * be -1, meaning that we're not writing from a register. | |
3691 | * | |
3692 | * The caller must ensure that the offset falls within the maximum stack size. | |
3693 | */ | |
3694 | static int check_stack_write(struct bpf_verifier_env *env, | |
3695 | int ptr_regno, int off, int size, | |
3696 | int value_regno, int insn_idx) | |
3697 | { | |
3698 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3699 | struct bpf_func_state *state = func(env, reg); | |
3700 | int err; | |
3701 | ||
3702 | if (tnum_is_const(reg->var_off)) { | |
3703 | off += reg->var_off.value; | |
3704 | err = check_stack_write_fixed_off(env, state, off, size, | |
3705 | value_regno, insn_idx); | |
3706 | } else { | |
3707 | /* Variable offset stack reads need more conservative handling | |
3708 | * than fixed offset ones. | |
3709 | */ | |
3710 | err = check_stack_write_var_off(env, state, | |
3711 | ptr_regno, off, size, | |
3712 | value_regno, insn_idx); | |
3713 | } | |
3714 | return err; | |
e4298d25 DB |
3715 | } |
3716 | ||
591fe988 DB |
3717 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
3718 | int off, int size, enum bpf_access_type type) | |
3719 | { | |
3720 | struct bpf_reg_state *regs = cur_regs(env); | |
3721 | struct bpf_map *map = regs[regno].map_ptr; | |
3722 | u32 cap = bpf_map_flags_to_cap(map); | |
3723 | ||
3724 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
3725 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
3726 | map->value_size, off, size); | |
3727 | return -EACCES; | |
3728 | } | |
3729 | ||
3730 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
3731 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
3732 | map->value_size, off, size); | |
3733 | return -EACCES; | |
3734 | } | |
3735 | ||
3736 | return 0; | |
3737 | } | |
3738 | ||
457f4436 AN |
3739 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
3740 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
3741 | int off, int size, u32 mem_size, | |
3742 | bool zero_size_allowed) | |
17a52670 | 3743 | { |
457f4436 AN |
3744 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
3745 | struct bpf_reg_state *reg; | |
3746 | ||
3747 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
3748 | return 0; | |
17a52670 | 3749 | |
457f4436 AN |
3750 | reg = &cur_regs(env)[regno]; |
3751 | switch (reg->type) { | |
69c087ba YS |
3752 | case PTR_TO_MAP_KEY: |
3753 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
3754 | mem_size, off, size); | |
3755 | break; | |
457f4436 | 3756 | case PTR_TO_MAP_VALUE: |
61bd5218 | 3757 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
3758 | mem_size, off, size); |
3759 | break; | |
3760 | case PTR_TO_PACKET: | |
3761 | case PTR_TO_PACKET_META: | |
3762 | case PTR_TO_PACKET_END: | |
3763 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
3764 | off, size, regno, reg->id, off, mem_size); | |
3765 | break; | |
3766 | case PTR_TO_MEM: | |
3767 | default: | |
3768 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
3769 | mem_size, off, size); | |
17a52670 | 3770 | } |
457f4436 AN |
3771 | |
3772 | return -EACCES; | |
17a52670 AS |
3773 | } |
3774 | ||
457f4436 AN |
3775 | /* check read/write into a memory region with possible variable offset */ |
3776 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
3777 | int off, int size, u32 mem_size, | |
3778 | bool zero_size_allowed) | |
dbcfe5f7 | 3779 | { |
f4d7e40a AS |
3780 | struct bpf_verifier_state *vstate = env->cur_state; |
3781 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
3782 | struct bpf_reg_state *reg = &state->regs[regno]; |
3783 | int err; | |
3784 | ||
457f4436 | 3785 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
3786 | * need to try adding each of min_value and max_value to off |
3787 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
3788 | * |
3789 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
3790 | * comparisons where we can't assume the floor of a |
3791 | * value is 0. If we are using signed variables for our | |
3792 | * index'es we need to make sure that whatever we use | |
3793 | * will have a set floor within our range. | |
3794 | */ | |
b7137c4e DB |
3795 | if (reg->smin_value < 0 && |
3796 | (reg->smin_value == S64_MIN || | |
3797 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
3798 | reg->smin_value + off < 0)) { | |
61bd5218 | 3799 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
3800 | regno); |
3801 | return -EACCES; | |
3802 | } | |
457f4436 AN |
3803 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
3804 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 3805 | if (err) { |
457f4436 | 3806 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 3807 | regno); |
dbcfe5f7 GB |
3808 | return err; |
3809 | } | |
3810 | ||
b03c9f9f EC |
3811 | /* If we haven't set a max value then we need to bail since we can't be |
3812 | * sure we won't do bad things. | |
3813 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 3814 | */ |
b03c9f9f | 3815 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 3816 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
3817 | regno); |
3818 | return -EACCES; | |
3819 | } | |
457f4436 AN |
3820 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
3821 | mem_size, zero_size_allowed); | |
3822 | if (err) { | |
3823 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 3824 | regno); |
457f4436 AN |
3825 | return err; |
3826 | } | |
3827 | ||
3828 | return 0; | |
3829 | } | |
d83525ca | 3830 | |
e9147b44 KKD |
3831 | static int __check_ptr_off_reg(struct bpf_verifier_env *env, |
3832 | const struct bpf_reg_state *reg, int regno, | |
3833 | bool fixed_off_ok) | |
3834 | { | |
3835 | /* Access to this pointer-typed register or passing it to a helper | |
3836 | * is only allowed in its original, unmodified form. | |
3837 | */ | |
3838 | ||
3839 | if (reg->off < 0) { | |
3840 | verbose(env, "negative offset %s ptr R%d off=%d disallowed\n", | |
3841 | reg_type_str(env, reg->type), regno, reg->off); | |
3842 | return -EACCES; | |
3843 | } | |
3844 | ||
3845 | if (!fixed_off_ok && reg->off) { | |
3846 | verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n", | |
3847 | reg_type_str(env, reg->type), regno, reg->off); | |
3848 | return -EACCES; | |
3849 | } | |
3850 | ||
3851 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3852 | char tn_buf[48]; | |
3853 | ||
3854 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3855 | verbose(env, "variable %s access var_off=%s disallowed\n", | |
3856 | reg_type_str(env, reg->type), tn_buf); | |
3857 | return -EACCES; | |
3858 | } | |
3859 | ||
3860 | return 0; | |
3861 | } | |
3862 | ||
3863 | int check_ptr_off_reg(struct bpf_verifier_env *env, | |
3864 | const struct bpf_reg_state *reg, int regno) | |
3865 | { | |
3866 | return __check_ptr_off_reg(env, reg, regno, false); | |
3867 | } | |
3868 | ||
61df10c7 | 3869 | static int map_kptr_match_type(struct bpf_verifier_env *env, |
aa3496ac | 3870 | struct btf_field *kptr_field, |
61df10c7 KKD |
3871 | struct bpf_reg_state *reg, u32 regno) |
3872 | { | |
aa3496ac | 3873 | const char *targ_name = kernel_type_name(kptr_field->kptr.btf, kptr_field->kptr.btf_id); |
3f00c523 | 3874 | int perm_flags = PTR_MAYBE_NULL | PTR_TRUSTED; |
61df10c7 KKD |
3875 | const char *reg_name = ""; |
3876 | ||
6efe152d | 3877 | /* Only unreferenced case accepts untrusted pointers */ |
aa3496ac | 3878 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
3879 | perm_flags |= PTR_UNTRUSTED; |
3880 | ||
3881 | if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags)) | |
61df10c7 KKD |
3882 | goto bad_type; |
3883 | ||
3884 | if (!btf_is_kernel(reg->btf)) { | |
3885 | verbose(env, "R%d must point to kernel BTF\n", regno); | |
3886 | return -EINVAL; | |
3887 | } | |
3888 | /* We need to verify reg->type and reg->btf, before accessing reg->btf */ | |
3889 | reg_name = kernel_type_name(reg->btf, reg->btf_id); | |
3890 | ||
c0a5a21c KKD |
3891 | /* For ref_ptr case, release function check should ensure we get one |
3892 | * referenced PTR_TO_BTF_ID, and that its fixed offset is 0. For the | |
3893 | * normal store of unreferenced kptr, we must ensure var_off is zero. | |
3894 | * Since ref_ptr cannot be accessed directly by BPF insns, checks for | |
3895 | * reg->off and reg->ref_obj_id are not needed here. | |
3896 | */ | |
61df10c7 KKD |
3897 | if (__check_ptr_off_reg(env, reg, regno, true)) |
3898 | return -EACCES; | |
3899 | ||
3900 | /* A full type match is needed, as BTF can be vmlinux or module BTF, and | |
3901 | * we also need to take into account the reg->off. | |
3902 | * | |
3903 | * We want to support cases like: | |
3904 | * | |
3905 | * struct foo { | |
3906 | * struct bar br; | |
3907 | * struct baz bz; | |
3908 | * }; | |
3909 | * | |
3910 | * struct foo *v; | |
3911 | * v = func(); // PTR_TO_BTF_ID | |
3912 | * val->foo = v; // reg->off is zero, btf and btf_id match type | |
3913 | * val->bar = &v->br; // reg->off is still zero, but we need to retry with | |
3914 | * // first member type of struct after comparison fails | |
3915 | * val->baz = &v->bz; // reg->off is non-zero, so struct needs to be walked | |
3916 | * // to match type | |
3917 | * | |
3918 | * In the kptr_ref case, check_func_arg_reg_off already ensures reg->off | |
2ab3b380 KKD |
3919 | * is zero. We must also ensure that btf_struct_ids_match does not walk |
3920 | * the struct to match type against first member of struct, i.e. reject | |
3921 | * second case from above. Hence, when type is BPF_KPTR_REF, we set | |
3922 | * strict mode to true for type match. | |
61df10c7 KKD |
3923 | */ |
3924 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
aa3496ac KKD |
3925 | kptr_field->kptr.btf, kptr_field->kptr.btf_id, |
3926 | kptr_field->type == BPF_KPTR_REF)) | |
61df10c7 KKD |
3927 | goto bad_type; |
3928 | return 0; | |
3929 | bad_type: | |
3930 | verbose(env, "invalid kptr access, R%d type=%s%s ", regno, | |
3931 | reg_type_str(env, reg->type), reg_name); | |
6efe152d | 3932 | verbose(env, "expected=%s%s", reg_type_str(env, PTR_TO_BTF_ID), targ_name); |
aa3496ac | 3933 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
3934 | verbose(env, " or %s%s\n", reg_type_str(env, PTR_TO_BTF_ID | PTR_UNTRUSTED), |
3935 | targ_name); | |
3936 | else | |
3937 | verbose(env, "\n"); | |
61df10c7 KKD |
3938 | return -EINVAL; |
3939 | } | |
3940 | ||
3941 | static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, | |
3942 | int value_regno, int insn_idx, | |
aa3496ac | 3943 | struct btf_field *kptr_field) |
61df10c7 KKD |
3944 | { |
3945 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; | |
3946 | int class = BPF_CLASS(insn->code); | |
3947 | struct bpf_reg_state *val_reg; | |
3948 | ||
3949 | /* Things we already checked for in check_map_access and caller: | |
3950 | * - Reject cases where variable offset may touch kptr | |
3951 | * - size of access (must be BPF_DW) | |
3952 | * - tnum_is_const(reg->var_off) | |
aa3496ac | 3953 | * - kptr_field->offset == off + reg->var_off.value |
61df10c7 KKD |
3954 | */ |
3955 | /* Only BPF_[LDX,STX,ST] | BPF_MEM | BPF_DW is supported */ | |
3956 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
3957 | verbose(env, "kptr in map can only be accessed using BPF_MEM instruction mode\n"); | |
3958 | return -EACCES; | |
3959 | } | |
3960 | ||
6efe152d KKD |
3961 | /* We only allow loading referenced kptr, since it will be marked as |
3962 | * untrusted, similar to unreferenced kptr. | |
3963 | */ | |
aa3496ac | 3964 | if (class != BPF_LDX && kptr_field->type == BPF_KPTR_REF) { |
6efe152d | 3965 | verbose(env, "store to referenced kptr disallowed\n"); |
c0a5a21c KKD |
3966 | return -EACCES; |
3967 | } | |
3968 | ||
61df10c7 KKD |
3969 | if (class == BPF_LDX) { |
3970 | val_reg = reg_state(env, value_regno); | |
3971 | /* We can simply mark the value_regno receiving the pointer | |
3972 | * value from map as PTR_TO_BTF_ID, with the correct type. | |
3973 | */ | |
aa3496ac KKD |
3974 | mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, kptr_field->kptr.btf, |
3975 | kptr_field->kptr.btf_id, PTR_MAYBE_NULL | PTR_UNTRUSTED); | |
61df10c7 KKD |
3976 | /* For mark_ptr_or_null_reg */ |
3977 | val_reg->id = ++env->id_gen; | |
3978 | } else if (class == BPF_STX) { | |
3979 | val_reg = reg_state(env, value_regno); | |
3980 | if (!register_is_null(val_reg) && | |
aa3496ac | 3981 | map_kptr_match_type(env, kptr_field, val_reg, value_regno)) |
61df10c7 KKD |
3982 | return -EACCES; |
3983 | } else if (class == BPF_ST) { | |
3984 | if (insn->imm) { | |
3985 | verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%u\n", | |
aa3496ac | 3986 | kptr_field->offset); |
61df10c7 KKD |
3987 | return -EACCES; |
3988 | } | |
3989 | } else { | |
3990 | verbose(env, "kptr in map can only be accessed using BPF_LDX/BPF_STX/BPF_ST\n"); | |
3991 | return -EACCES; | |
3992 | } | |
3993 | return 0; | |
3994 | } | |
3995 | ||
457f4436 AN |
3996 | /* check read/write into a map element with possible variable offset */ |
3997 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
61df10c7 KKD |
3998 | int off, int size, bool zero_size_allowed, |
3999 | enum bpf_access_src src) | |
457f4436 AN |
4000 | { |
4001 | struct bpf_verifier_state *vstate = env->cur_state; | |
4002 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4003 | struct bpf_reg_state *reg = &state->regs[regno]; | |
4004 | struct bpf_map *map = reg->map_ptr; | |
aa3496ac KKD |
4005 | struct btf_record *rec; |
4006 | int err, i; | |
457f4436 AN |
4007 | |
4008 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
4009 | zero_size_allowed); | |
4010 | if (err) | |
4011 | return err; | |
4012 | ||
aa3496ac KKD |
4013 | if (IS_ERR_OR_NULL(map->record)) |
4014 | return 0; | |
4015 | rec = map->record; | |
4016 | for (i = 0; i < rec->cnt; i++) { | |
4017 | struct btf_field *field = &rec->fields[i]; | |
4018 | u32 p = field->offset; | |
d83525ca | 4019 | |
db559117 KKD |
4020 | /* If any part of a field can be touched by load/store, reject |
4021 | * this program. To check that [x1, x2) overlaps with [y1, y2), | |
d83525ca AS |
4022 | * it is sufficient to check x1 < y2 && y1 < x2. |
4023 | */ | |
aa3496ac KKD |
4024 | if (reg->smin_value + off < p + btf_field_type_size(field->type) && |
4025 | p < reg->umax_value + off + size) { | |
4026 | switch (field->type) { | |
4027 | case BPF_KPTR_UNREF: | |
4028 | case BPF_KPTR_REF: | |
61df10c7 KKD |
4029 | if (src != ACCESS_DIRECT) { |
4030 | verbose(env, "kptr cannot be accessed indirectly by helper\n"); | |
4031 | return -EACCES; | |
4032 | } | |
4033 | if (!tnum_is_const(reg->var_off)) { | |
4034 | verbose(env, "kptr access cannot have variable offset\n"); | |
4035 | return -EACCES; | |
4036 | } | |
4037 | if (p != off + reg->var_off.value) { | |
4038 | verbose(env, "kptr access misaligned expected=%u off=%llu\n", | |
4039 | p, off + reg->var_off.value); | |
4040 | return -EACCES; | |
4041 | } | |
4042 | if (size != bpf_size_to_bytes(BPF_DW)) { | |
4043 | verbose(env, "kptr access size must be BPF_DW\n"); | |
4044 | return -EACCES; | |
4045 | } | |
4046 | break; | |
aa3496ac | 4047 | default: |
db559117 KKD |
4048 | verbose(env, "%s cannot be accessed directly by load/store\n", |
4049 | btf_field_type_name(field->type)); | |
aa3496ac | 4050 | return -EACCES; |
61df10c7 KKD |
4051 | } |
4052 | } | |
4053 | } | |
aa3496ac | 4054 | return 0; |
dbcfe5f7 GB |
4055 | } |
4056 | ||
969bf05e AS |
4057 | #define MAX_PACKET_OFF 0xffff |
4058 | ||
58e2af8b | 4059 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
4060 | const struct bpf_call_arg_meta *meta, |
4061 | enum bpf_access_type t) | |
4acf6c0b | 4062 | { |
7e40781c UP |
4063 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
4064 | ||
4065 | switch (prog_type) { | |
5d66fa7d | 4066 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
4067 | case BPF_PROG_TYPE_LWT_IN: |
4068 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 4069 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 4070 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 4071 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 4072 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
4073 | if (t == BPF_WRITE) |
4074 | return false; | |
8731745e | 4075 | fallthrough; |
5d66fa7d DB |
4076 | |
4077 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
4078 | case BPF_PROG_TYPE_SCHED_CLS: |
4079 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 4080 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 4081 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 4082 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 4083 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
4084 | if (meta) |
4085 | return meta->pkt_access; | |
4086 | ||
4087 | env->seen_direct_write = true; | |
4acf6c0b | 4088 | return true; |
0d01da6a SF |
4089 | |
4090 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
4091 | if (t == BPF_WRITE) | |
4092 | env->seen_direct_write = true; | |
4093 | ||
4094 | return true; | |
4095 | ||
4acf6c0b BB |
4096 | default: |
4097 | return false; | |
4098 | } | |
4099 | } | |
4100 | ||
f1174f77 | 4101 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 4102 | int size, bool zero_size_allowed) |
f1174f77 | 4103 | { |
638f5b90 | 4104 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
4105 | struct bpf_reg_state *reg = ®s[regno]; |
4106 | int err; | |
4107 | ||
4108 | /* We may have added a variable offset to the packet pointer; but any | |
4109 | * reg->range we have comes after that. We are only checking the fixed | |
4110 | * offset. | |
4111 | */ | |
4112 | ||
4113 | /* We don't allow negative numbers, because we aren't tracking enough | |
4114 | * detail to prove they're safe. | |
4115 | */ | |
b03c9f9f | 4116 | if (reg->smin_value < 0) { |
61bd5218 | 4117 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
4118 | regno); |
4119 | return -EACCES; | |
4120 | } | |
6d94e741 AS |
4121 | |
4122 | err = reg->range < 0 ? -EINVAL : | |
4123 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 4124 | zero_size_allowed); |
f1174f77 | 4125 | if (err) { |
61bd5218 | 4126 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
4127 | return err; |
4128 | } | |
e647815a | 4129 | |
457f4436 | 4130 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
4131 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
4132 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 4133 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
4134 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
4135 | */ | |
4136 | env->prog->aux->max_pkt_offset = | |
4137 | max_t(u32, env->prog->aux->max_pkt_offset, | |
4138 | off + reg->umax_value + size - 1); | |
4139 | ||
f1174f77 EC |
4140 | return err; |
4141 | } | |
4142 | ||
4143 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 4144 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 4145 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 4146 | struct btf **btf, u32 *btf_id) |
17a52670 | 4147 | { |
f96da094 DB |
4148 | struct bpf_insn_access_aux info = { |
4149 | .reg_type = *reg_type, | |
9e15db66 | 4150 | .log = &env->log, |
f96da094 | 4151 | }; |
31fd8581 | 4152 | |
4f9218aa | 4153 | if (env->ops->is_valid_access && |
5e43f899 | 4154 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
4155 | /* A non zero info.ctx_field_size indicates that this field is a |
4156 | * candidate for later verifier transformation to load the whole | |
4157 | * field and then apply a mask when accessed with a narrower | |
4158 | * access than actual ctx access size. A zero info.ctx_field_size | |
4159 | * will only allow for whole field access and rejects any other | |
4160 | * type of narrower access. | |
31fd8581 | 4161 | */ |
23994631 | 4162 | *reg_type = info.reg_type; |
31fd8581 | 4163 | |
c25b2ae1 | 4164 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 4165 | *btf = info.btf; |
9e15db66 | 4166 | *btf_id = info.btf_id; |
22dc4a0f | 4167 | } else { |
9e15db66 | 4168 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 4169 | } |
32bbe007 AS |
4170 | /* remember the offset of last byte accessed in ctx */ |
4171 | if (env->prog->aux->max_ctx_offset < off + size) | |
4172 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 4173 | return 0; |
32bbe007 | 4174 | } |
17a52670 | 4175 | |
61bd5218 | 4176 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
4177 | return -EACCES; |
4178 | } | |
4179 | ||
d58e468b PP |
4180 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
4181 | int size) | |
4182 | { | |
4183 | if (size < 0 || off < 0 || | |
4184 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
4185 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
4186 | off, size); | |
4187 | return -EACCES; | |
4188 | } | |
4189 | return 0; | |
4190 | } | |
4191 | ||
5f456649 MKL |
4192 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
4193 | u32 regno, int off, int size, | |
4194 | enum bpf_access_type t) | |
c64b7983 JS |
4195 | { |
4196 | struct bpf_reg_state *regs = cur_regs(env); | |
4197 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 4198 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 4199 | bool valid; |
c64b7983 JS |
4200 | |
4201 | if (reg->smin_value < 0) { | |
4202 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
4203 | regno); | |
4204 | return -EACCES; | |
4205 | } | |
4206 | ||
46f8bc92 MKL |
4207 | switch (reg->type) { |
4208 | case PTR_TO_SOCK_COMMON: | |
4209 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
4210 | break; | |
4211 | case PTR_TO_SOCKET: | |
4212 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
4213 | break; | |
655a51e5 MKL |
4214 | case PTR_TO_TCP_SOCK: |
4215 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
4216 | break; | |
fada7fdc JL |
4217 | case PTR_TO_XDP_SOCK: |
4218 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
4219 | break; | |
46f8bc92 MKL |
4220 | default: |
4221 | valid = false; | |
c64b7983 JS |
4222 | } |
4223 | ||
5f456649 | 4224 | |
46f8bc92 MKL |
4225 | if (valid) { |
4226 | env->insn_aux_data[insn_idx].ctx_field_size = | |
4227 | info.ctx_field_size; | |
4228 | return 0; | |
4229 | } | |
4230 | ||
4231 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
c25b2ae1 | 4232 | regno, reg_type_str(env, reg->type), off, size); |
46f8bc92 MKL |
4233 | |
4234 | return -EACCES; | |
c64b7983 JS |
4235 | } |
4236 | ||
4cabc5b1 DB |
4237 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
4238 | { | |
2a159c6f | 4239 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
4240 | } |
4241 | ||
f37a8cb8 DB |
4242 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
4243 | { | |
2a159c6f | 4244 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 4245 | |
46f8bc92 MKL |
4246 | return reg->type == PTR_TO_CTX; |
4247 | } | |
4248 | ||
4249 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
4250 | { | |
4251 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4252 | ||
4253 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
4254 | } |
4255 | ||
ca369602 DB |
4256 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
4257 | { | |
2a159c6f | 4258 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
4259 | |
4260 | return type_is_pkt_pointer(reg->type); | |
4261 | } | |
4262 | ||
4b5defde DB |
4263 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
4264 | { | |
4265 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4266 | ||
4267 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
4268 | return reg->type == PTR_TO_FLOW_KEYS; | |
4269 | } | |
4270 | ||
9bb00b28 YS |
4271 | static bool is_trusted_reg(const struct bpf_reg_state *reg) |
4272 | { | |
4273 | /* A referenced register is always trusted. */ | |
4274 | if (reg->ref_obj_id) | |
4275 | return true; | |
4276 | ||
4277 | /* If a register is not referenced, it is trusted if it has the | |
fca1aa75 | 4278 | * MEM_ALLOC or PTR_TRUSTED type modifiers, and no others. Some of the |
9bb00b28 YS |
4279 | * other type modifiers may be safe, but we elect to take an opt-in |
4280 | * approach here as some (e.g. PTR_UNTRUSTED and PTR_MAYBE_NULL) are | |
4281 | * not. | |
4282 | * | |
4283 | * Eventually, we should make PTR_TRUSTED the single source of truth | |
4284 | * for whether a register is trusted. | |
4285 | */ | |
4286 | return type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS && | |
4287 | !bpf_type_has_unsafe_modifiers(reg->type); | |
4288 | } | |
4289 | ||
fca1aa75 YS |
4290 | static bool is_rcu_reg(const struct bpf_reg_state *reg) |
4291 | { | |
4292 | return reg->type & MEM_RCU; | |
4293 | } | |
4294 | ||
61bd5218 JK |
4295 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
4296 | const struct bpf_reg_state *reg, | |
d1174416 | 4297 | int off, int size, bool strict) |
969bf05e | 4298 | { |
f1174f77 | 4299 | struct tnum reg_off; |
e07b98d9 | 4300 | int ip_align; |
d1174416 DM |
4301 | |
4302 | /* Byte size accesses are always allowed. */ | |
4303 | if (!strict || size == 1) | |
4304 | return 0; | |
4305 | ||
e4eda884 DM |
4306 | /* For platforms that do not have a Kconfig enabling |
4307 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
4308 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
4309 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
4310 | * to this code only in strict mode where we want to emulate | |
4311 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
4312 | * unconditional IP align value of '2'. | |
e07b98d9 | 4313 | */ |
e4eda884 | 4314 | ip_align = 2; |
f1174f77 EC |
4315 | |
4316 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
4317 | if (!tnum_is_aligned(reg_off, size)) { | |
4318 | char tn_buf[48]; | |
4319 | ||
4320 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
4321 | verbose(env, |
4322 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 4323 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
4324 | return -EACCES; |
4325 | } | |
79adffcd | 4326 | |
969bf05e AS |
4327 | return 0; |
4328 | } | |
4329 | ||
61bd5218 JK |
4330 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
4331 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
4332 | const char *pointer_desc, |
4333 | int off, int size, bool strict) | |
79adffcd | 4334 | { |
f1174f77 EC |
4335 | struct tnum reg_off; |
4336 | ||
4337 | /* Byte size accesses are always allowed. */ | |
4338 | if (!strict || size == 1) | |
4339 | return 0; | |
4340 | ||
4341 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
4342 | if (!tnum_is_aligned(reg_off, size)) { | |
4343 | char tn_buf[48]; | |
4344 | ||
4345 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 4346 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 4347 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
4348 | return -EACCES; |
4349 | } | |
4350 | ||
969bf05e AS |
4351 | return 0; |
4352 | } | |
4353 | ||
e07b98d9 | 4354 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
4355 | const struct bpf_reg_state *reg, int off, |
4356 | int size, bool strict_alignment_once) | |
79adffcd | 4357 | { |
ca369602 | 4358 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 4359 | const char *pointer_desc = ""; |
d1174416 | 4360 | |
79adffcd DB |
4361 | switch (reg->type) { |
4362 | case PTR_TO_PACKET: | |
de8f3a83 DB |
4363 | case PTR_TO_PACKET_META: |
4364 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
4365 | * right in front, treat it the very same way. | |
4366 | */ | |
61bd5218 | 4367 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
4368 | case PTR_TO_FLOW_KEYS: |
4369 | pointer_desc = "flow keys "; | |
4370 | break; | |
69c087ba YS |
4371 | case PTR_TO_MAP_KEY: |
4372 | pointer_desc = "key "; | |
4373 | break; | |
f1174f77 EC |
4374 | case PTR_TO_MAP_VALUE: |
4375 | pointer_desc = "value "; | |
4376 | break; | |
4377 | case PTR_TO_CTX: | |
4378 | pointer_desc = "context "; | |
4379 | break; | |
4380 | case PTR_TO_STACK: | |
4381 | pointer_desc = "stack "; | |
01f810ac AM |
4382 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
4383 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
4384 | * aligned. |
4385 | */ | |
4386 | strict = true; | |
f1174f77 | 4387 | break; |
c64b7983 JS |
4388 | case PTR_TO_SOCKET: |
4389 | pointer_desc = "sock "; | |
4390 | break; | |
46f8bc92 MKL |
4391 | case PTR_TO_SOCK_COMMON: |
4392 | pointer_desc = "sock_common "; | |
4393 | break; | |
655a51e5 MKL |
4394 | case PTR_TO_TCP_SOCK: |
4395 | pointer_desc = "tcp_sock "; | |
4396 | break; | |
fada7fdc JL |
4397 | case PTR_TO_XDP_SOCK: |
4398 | pointer_desc = "xdp_sock "; | |
4399 | break; | |
79adffcd | 4400 | default: |
f1174f77 | 4401 | break; |
79adffcd | 4402 | } |
61bd5218 JK |
4403 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
4404 | strict); | |
79adffcd DB |
4405 | } |
4406 | ||
f4d7e40a AS |
4407 | static int update_stack_depth(struct bpf_verifier_env *env, |
4408 | const struct bpf_func_state *func, | |
4409 | int off) | |
4410 | { | |
9c8105bd | 4411 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
4412 | |
4413 | if (stack >= -off) | |
4414 | return 0; | |
4415 | ||
4416 | /* update known max for given subprogram */ | |
9c8105bd | 4417 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
4418 | return 0; |
4419 | } | |
f4d7e40a | 4420 | |
70a87ffe AS |
4421 | /* starting from main bpf function walk all instructions of the function |
4422 | * and recursively walk all callees that given function can call. | |
4423 | * Ignore jump and exit insns. | |
4424 | * Since recursion is prevented by check_cfg() this algorithm | |
4425 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
4426 | */ | |
4427 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
4428 | { | |
9c8105bd JW |
4429 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
4430 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 4431 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 4432 | bool tail_call_reachable = false; |
70a87ffe AS |
4433 | int ret_insn[MAX_CALL_FRAMES]; |
4434 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 4435 | int j; |
f4d7e40a | 4436 | |
70a87ffe | 4437 | process_func: |
7f6e4312 MF |
4438 | /* protect against potential stack overflow that might happen when |
4439 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
4440 | * depth for such case down to 256 so that the worst case scenario | |
4441 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
4442 | * 8k). | |
4443 | * | |
4444 | * To get the idea what might happen, see an example: | |
4445 | * func1 -> sub rsp, 128 | |
4446 | * subfunc1 -> sub rsp, 256 | |
4447 | * tailcall1 -> add rsp, 256 | |
4448 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
4449 | * subfunc2 -> sub rsp, 64 | |
4450 | * subfunc22 -> sub rsp, 128 | |
4451 | * tailcall2 -> add rsp, 128 | |
4452 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
4453 | * | |
4454 | * tailcall will unwind the current stack frame but it will not get rid | |
4455 | * of caller's stack as shown on the example above. | |
4456 | */ | |
4457 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
4458 | verbose(env, | |
4459 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
4460 | depth); | |
4461 | return -EACCES; | |
4462 | } | |
70a87ffe AS |
4463 | /* round up to 32-bytes, since this is granularity |
4464 | * of interpreter stack size | |
4465 | */ | |
9c8105bd | 4466 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 4467 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 4468 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 4469 | frame + 1, depth); |
f4d7e40a AS |
4470 | return -EACCES; |
4471 | } | |
70a87ffe | 4472 | continue_func: |
4cb3d99c | 4473 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 4474 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
4475 | int next_insn; |
4476 | ||
69c087ba | 4477 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
4478 | continue; |
4479 | /* remember insn and function to return to */ | |
4480 | ret_insn[frame] = i + 1; | |
9c8105bd | 4481 | ret_prog[frame] = idx; |
70a87ffe AS |
4482 | |
4483 | /* find the callee */ | |
7ddc80a4 AS |
4484 | next_insn = i + insn[i].imm + 1; |
4485 | idx = find_subprog(env, next_insn); | |
9c8105bd | 4486 | if (idx < 0) { |
70a87ffe | 4487 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 4488 | next_insn); |
70a87ffe AS |
4489 | return -EFAULT; |
4490 | } | |
7ddc80a4 AS |
4491 | if (subprog[idx].is_async_cb) { |
4492 | if (subprog[idx].has_tail_call) { | |
4493 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
4494 | return -EFAULT; | |
4495 | } | |
4496 | /* async callbacks don't increase bpf prog stack size */ | |
4497 | continue; | |
4498 | } | |
4499 | i = next_insn; | |
ebf7d1f5 MF |
4500 | |
4501 | if (subprog[idx].has_tail_call) | |
4502 | tail_call_reachable = true; | |
4503 | ||
70a87ffe AS |
4504 | frame++; |
4505 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
4506 | verbose(env, "the call stack of %d frames is too deep !\n", |
4507 | frame); | |
4508 | return -E2BIG; | |
70a87ffe AS |
4509 | } |
4510 | goto process_func; | |
4511 | } | |
ebf7d1f5 MF |
4512 | /* if tail call got detected across bpf2bpf calls then mark each of the |
4513 | * currently present subprog frames as tail call reachable subprogs; | |
4514 | * this info will be utilized by JIT so that we will be preserving the | |
4515 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
4516 | */ | |
4517 | if (tail_call_reachable) | |
4518 | for (j = 0; j < frame; j++) | |
4519 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
4520 | if (subprog[0].tail_call_reachable) |
4521 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 4522 | |
70a87ffe AS |
4523 | /* end of for() loop means the last insn of the 'subprog' |
4524 | * was reached. Doesn't matter whether it was JA or EXIT | |
4525 | */ | |
4526 | if (frame == 0) | |
4527 | return 0; | |
9c8105bd | 4528 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
4529 | frame--; |
4530 | i = ret_insn[frame]; | |
9c8105bd | 4531 | idx = ret_prog[frame]; |
70a87ffe | 4532 | goto continue_func; |
f4d7e40a AS |
4533 | } |
4534 | ||
19d28fbd | 4535 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
4536 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
4537 | const struct bpf_insn *insn, int idx) | |
4538 | { | |
4539 | int start = idx + insn->imm + 1, subprog; | |
4540 | ||
4541 | subprog = find_subprog(env, start); | |
4542 | if (subprog < 0) { | |
4543 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
4544 | start); | |
4545 | return -EFAULT; | |
4546 | } | |
9c8105bd | 4547 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 4548 | } |
19d28fbd | 4549 | #endif |
1ea47e01 | 4550 | |
afbf21dc YS |
4551 | static int __check_buffer_access(struct bpf_verifier_env *env, |
4552 | const char *buf_info, | |
4553 | const struct bpf_reg_state *reg, | |
4554 | int regno, int off, int size) | |
9df1c28b MM |
4555 | { |
4556 | if (off < 0) { | |
4557 | verbose(env, | |
4fc00b79 | 4558 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 4559 | regno, buf_info, off, size); |
9df1c28b MM |
4560 | return -EACCES; |
4561 | } | |
4562 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4563 | char tn_buf[48]; | |
4564 | ||
4565 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4566 | verbose(env, | |
4fc00b79 | 4567 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
4568 | regno, off, tn_buf); |
4569 | return -EACCES; | |
4570 | } | |
afbf21dc YS |
4571 | |
4572 | return 0; | |
4573 | } | |
4574 | ||
4575 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
4576 | const struct bpf_reg_state *reg, | |
4577 | int regno, int off, int size) | |
4578 | { | |
4579 | int err; | |
4580 | ||
4581 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
4582 | if (err) | |
4583 | return err; | |
4584 | ||
9df1c28b MM |
4585 | if (off + size > env->prog->aux->max_tp_access) |
4586 | env->prog->aux->max_tp_access = off + size; | |
4587 | ||
4588 | return 0; | |
4589 | } | |
4590 | ||
afbf21dc YS |
4591 | static int check_buffer_access(struct bpf_verifier_env *env, |
4592 | const struct bpf_reg_state *reg, | |
4593 | int regno, int off, int size, | |
4594 | bool zero_size_allowed, | |
afbf21dc YS |
4595 | u32 *max_access) |
4596 | { | |
44e9a741 | 4597 | const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; |
afbf21dc YS |
4598 | int err; |
4599 | ||
4600 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
4601 | if (err) | |
4602 | return err; | |
4603 | ||
4604 | if (off + size > *max_access) | |
4605 | *max_access = off + size; | |
4606 | ||
4607 | return 0; | |
4608 | } | |
4609 | ||
3f50f132 JF |
4610 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
4611 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
4612 | { | |
4613 | reg->var_off = tnum_subreg(reg->var_off); | |
4614 | __reg_assign_32_into_64(reg); | |
4615 | } | |
9df1c28b | 4616 | |
0c17d1d2 JH |
4617 | /* truncate register to smaller size (in bytes) |
4618 | * must be called with size < BPF_REG_SIZE | |
4619 | */ | |
4620 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
4621 | { | |
4622 | u64 mask; | |
4623 | ||
4624 | /* clear high bits in bit representation */ | |
4625 | reg->var_off = tnum_cast(reg->var_off, size); | |
4626 | ||
4627 | /* fix arithmetic bounds */ | |
4628 | mask = ((u64)1 << (size * 8)) - 1; | |
4629 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
4630 | reg->umin_value &= mask; | |
4631 | reg->umax_value &= mask; | |
4632 | } else { | |
4633 | reg->umin_value = 0; | |
4634 | reg->umax_value = mask; | |
4635 | } | |
4636 | reg->smin_value = reg->umin_value; | |
4637 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
4638 | |
4639 | /* If size is smaller than 32bit register the 32bit register | |
4640 | * values are also truncated so we push 64-bit bounds into | |
4641 | * 32-bit bounds. Above were truncated < 32-bits already. | |
4642 | */ | |
4643 | if (size >= 4) | |
4644 | return; | |
4645 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
4646 | } |
4647 | ||
a23740ec AN |
4648 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
4649 | { | |
353050be DB |
4650 | /* A map is considered read-only if the following condition are true: |
4651 | * | |
4652 | * 1) BPF program side cannot change any of the map content. The | |
4653 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
4654 | * and was set at map creation time. | |
4655 | * 2) The map value(s) have been initialized from user space by a | |
4656 | * loader and then "frozen", such that no new map update/delete | |
4657 | * operations from syscall side are possible for the rest of | |
4658 | * the map's lifetime from that point onwards. | |
4659 | * 3) Any parallel/pending map update/delete operations from syscall | |
4660 | * side have been completed. Only after that point, it's safe to | |
4661 | * assume that map value(s) are immutable. | |
4662 | */ | |
4663 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
4664 | READ_ONCE(map->frozen) && | |
4665 | !bpf_map_write_active(map); | |
a23740ec AN |
4666 | } |
4667 | ||
4668 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
4669 | { | |
4670 | void *ptr; | |
4671 | u64 addr; | |
4672 | int err; | |
4673 | ||
4674 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
4675 | if (err) | |
4676 | return err; | |
2dedd7d2 | 4677 | ptr = (void *)(long)addr + off; |
a23740ec AN |
4678 | |
4679 | switch (size) { | |
4680 | case sizeof(u8): | |
4681 | *val = (u64)*(u8 *)ptr; | |
4682 | break; | |
4683 | case sizeof(u16): | |
4684 | *val = (u64)*(u16 *)ptr; | |
4685 | break; | |
4686 | case sizeof(u32): | |
4687 | *val = (u64)*(u32 *)ptr; | |
4688 | break; | |
4689 | case sizeof(u64): | |
4690 | *val = *(u64 *)ptr; | |
4691 | break; | |
4692 | default: | |
4693 | return -EINVAL; | |
4694 | } | |
4695 | return 0; | |
4696 | } | |
4697 | ||
9e15db66 AS |
4698 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
4699 | struct bpf_reg_state *regs, | |
4700 | int regno, int off, int size, | |
4701 | enum bpf_access_type atype, | |
4702 | int value_regno) | |
4703 | { | |
4704 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
4705 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
4706 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
c6f1bfe8 | 4707 | enum bpf_type_flag flag = 0; |
9e15db66 AS |
4708 | u32 btf_id; |
4709 | int ret; | |
4710 | ||
c67cae55 AS |
4711 | if (!env->allow_ptr_leaks) { |
4712 | verbose(env, | |
4713 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
4714 | tname); | |
4715 | return -EPERM; | |
4716 | } | |
4717 | if (!env->prog->gpl_compatible && btf_is_kernel(reg->btf)) { | |
4718 | verbose(env, | |
4719 | "Cannot access kernel 'struct %s' from non-GPL compatible program\n", | |
4720 | tname); | |
4721 | return -EINVAL; | |
4722 | } | |
9e15db66 AS |
4723 | if (off < 0) { |
4724 | verbose(env, | |
4725 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
4726 | regno, tname, off); | |
4727 | return -EACCES; | |
4728 | } | |
4729 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4730 | char tn_buf[48]; | |
4731 | ||
4732 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4733 | verbose(env, | |
4734 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
4735 | regno, tname, off, tn_buf); | |
4736 | return -EACCES; | |
4737 | } | |
4738 | ||
c6f1bfe8 YS |
4739 | if (reg->type & MEM_USER) { |
4740 | verbose(env, | |
4741 | "R%d is ptr_%s access user memory: off=%d\n", | |
4742 | regno, tname, off); | |
4743 | return -EACCES; | |
4744 | } | |
4745 | ||
5844101a HL |
4746 | if (reg->type & MEM_PERCPU) { |
4747 | verbose(env, | |
4748 | "R%d is ptr_%s access percpu memory: off=%d\n", | |
4749 | regno, tname, off); | |
4750 | return -EACCES; | |
4751 | } | |
4752 | ||
282de143 KKD |
4753 | if (env->ops->btf_struct_access && !type_is_alloc(reg->type)) { |
4754 | if (!btf_is_kernel(reg->btf)) { | |
4755 | verbose(env, "verifier internal error: reg->btf must be kernel btf\n"); | |
4756 | return -EFAULT; | |
4757 | } | |
6728aea7 | 4758 | ret = env->ops->btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 | 4759 | } else { |
282de143 KKD |
4760 | /* Writes are permitted with default btf_struct_access for |
4761 | * program allocated objects (which always have ref_obj_id > 0), | |
4762 | * but not for untrusted PTR_TO_BTF_ID | MEM_ALLOC. | |
4763 | */ | |
4764 | if (atype != BPF_READ && reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
27ae7997 MKL |
4765 | verbose(env, "only read is supported\n"); |
4766 | return -EACCES; | |
4767 | } | |
4768 | ||
282de143 KKD |
4769 | if (type_is_alloc(reg->type) && !reg->ref_obj_id) { |
4770 | verbose(env, "verifier internal error: ref_obj_id for allocated object must be non-zero\n"); | |
4771 | return -EFAULT; | |
4772 | } | |
4773 | ||
6728aea7 | 4774 | ret = btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 MKL |
4775 | } |
4776 | ||
9e15db66 AS |
4777 | if (ret < 0) |
4778 | return ret; | |
4779 | ||
6efe152d KKD |
4780 | /* If this is an untrusted pointer, all pointers formed by walking it |
4781 | * also inherit the untrusted flag. | |
4782 | */ | |
4783 | if (type_flag(reg->type) & PTR_UNTRUSTED) | |
4784 | flag |= PTR_UNTRUSTED; | |
4785 | ||
9bb00b28 YS |
4786 | /* By default any pointer obtained from walking a trusted pointer is |
4787 | * no longer trusted except the rcu case below. | |
4788 | */ | |
3f00c523 DV |
4789 | flag &= ~PTR_TRUSTED; |
4790 | ||
9bb00b28 YS |
4791 | if (flag & MEM_RCU) { |
4792 | /* Mark value register as MEM_RCU only if it is protected by | |
fca1aa75 | 4793 | * bpf_rcu_read_lock() and the ptr reg is rcu or trusted. MEM_RCU |
9bb00b28 | 4794 | * itself can already indicate trustedness inside the rcu |
fca1aa75 YS |
4795 | * read lock region. Also mark rcu pointer as PTR_MAYBE_NULL since |
4796 | * it could be null in some cases. | |
9bb00b28 | 4797 | */ |
fca1aa75 YS |
4798 | if (!env->cur_state->active_rcu_lock || |
4799 | !(is_trusted_reg(reg) || is_rcu_reg(reg))) | |
9bb00b28 YS |
4800 | flag &= ~MEM_RCU; |
4801 | else | |
fca1aa75 | 4802 | flag |= PTR_MAYBE_NULL; |
9bb00b28 YS |
4803 | } else if (reg->type & MEM_RCU) { |
4804 | /* ptr (reg) is marked as MEM_RCU, but the struct field is not tagged | |
4805 | * with __rcu. Mark the flag as PTR_UNTRUSTED conservatively. | |
4806 | */ | |
4807 | flag |= PTR_UNTRUSTED; | |
4808 | } | |
4809 | ||
41c48f3a | 4810 | if (atype == BPF_READ && value_regno >= 0) |
c6f1bfe8 | 4811 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); |
41c48f3a AI |
4812 | |
4813 | return 0; | |
4814 | } | |
4815 | ||
4816 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
4817 | struct bpf_reg_state *regs, | |
4818 | int regno, int off, int size, | |
4819 | enum bpf_access_type atype, | |
4820 | int value_regno) | |
4821 | { | |
4822 | struct bpf_reg_state *reg = regs + regno; | |
4823 | struct bpf_map *map = reg->map_ptr; | |
6728aea7 | 4824 | struct bpf_reg_state map_reg; |
c6f1bfe8 | 4825 | enum bpf_type_flag flag = 0; |
41c48f3a AI |
4826 | const struct btf_type *t; |
4827 | const char *tname; | |
4828 | u32 btf_id; | |
4829 | int ret; | |
4830 | ||
4831 | if (!btf_vmlinux) { | |
4832 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
4833 | return -ENOTSUPP; | |
4834 | } | |
4835 | ||
4836 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
4837 | verbose(env, "map_ptr access not supported for map type %d\n", | |
4838 | map->map_type); | |
4839 | return -ENOTSUPP; | |
4840 | } | |
4841 | ||
4842 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
4843 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
4844 | ||
c67cae55 | 4845 | if (!env->allow_ptr_leaks) { |
41c48f3a | 4846 | verbose(env, |
c67cae55 | 4847 | "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
41c48f3a AI |
4848 | tname); |
4849 | return -EPERM; | |
9e15db66 | 4850 | } |
27ae7997 | 4851 | |
41c48f3a AI |
4852 | if (off < 0) { |
4853 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
4854 | regno, tname, off); | |
4855 | return -EACCES; | |
4856 | } | |
4857 | ||
4858 | if (atype != BPF_READ) { | |
4859 | verbose(env, "only read from %s is supported\n", tname); | |
4860 | return -EACCES; | |
4861 | } | |
4862 | ||
6728aea7 KKD |
4863 | /* Simulate access to a PTR_TO_BTF_ID */ |
4864 | memset(&map_reg, 0, sizeof(map_reg)); | |
4865 | mark_btf_ld_reg(env, &map_reg, 0, PTR_TO_BTF_ID, btf_vmlinux, *map->ops->map_btf_id, 0); | |
4866 | ret = btf_struct_access(&env->log, &map_reg, off, size, atype, &btf_id, &flag); | |
41c48f3a AI |
4867 | if (ret < 0) |
4868 | return ret; | |
4869 | ||
4870 | if (value_regno >= 0) | |
c6f1bfe8 | 4871 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id, flag); |
41c48f3a | 4872 | |
9e15db66 AS |
4873 | return 0; |
4874 | } | |
4875 | ||
01f810ac AM |
4876 | /* Check that the stack access at the given offset is within bounds. The |
4877 | * maximum valid offset is -1. | |
4878 | * | |
4879 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
4880 | * -state->allocated_stack for reads. | |
4881 | */ | |
4882 | static int check_stack_slot_within_bounds(int off, | |
4883 | struct bpf_func_state *state, | |
4884 | enum bpf_access_type t) | |
4885 | { | |
4886 | int min_valid_off; | |
4887 | ||
4888 | if (t == BPF_WRITE) | |
4889 | min_valid_off = -MAX_BPF_STACK; | |
4890 | else | |
4891 | min_valid_off = -state->allocated_stack; | |
4892 | ||
4893 | if (off < min_valid_off || off > -1) | |
4894 | return -EACCES; | |
4895 | return 0; | |
4896 | } | |
4897 | ||
4898 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
4899 | * bounds. | |
4900 | * | |
4901 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
4902 | */ | |
4903 | static int check_stack_access_within_bounds( | |
4904 | struct bpf_verifier_env *env, | |
4905 | int regno, int off, int access_size, | |
61df10c7 | 4906 | enum bpf_access_src src, enum bpf_access_type type) |
01f810ac AM |
4907 | { |
4908 | struct bpf_reg_state *regs = cur_regs(env); | |
4909 | struct bpf_reg_state *reg = regs + regno; | |
4910 | struct bpf_func_state *state = func(env, reg); | |
4911 | int min_off, max_off; | |
4912 | int err; | |
4913 | char *err_extra; | |
4914 | ||
4915 | if (src == ACCESS_HELPER) | |
4916 | /* We don't know if helpers are reading or writing (or both). */ | |
4917 | err_extra = " indirect access to"; | |
4918 | else if (type == BPF_READ) | |
4919 | err_extra = " read from"; | |
4920 | else | |
4921 | err_extra = " write to"; | |
4922 | ||
4923 | if (tnum_is_const(reg->var_off)) { | |
4924 | min_off = reg->var_off.value + off; | |
4925 | if (access_size > 0) | |
4926 | max_off = min_off + access_size - 1; | |
4927 | else | |
4928 | max_off = min_off; | |
4929 | } else { | |
4930 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
4931 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
4932 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
4933 | err_extra, regno); | |
4934 | return -EACCES; | |
4935 | } | |
4936 | min_off = reg->smin_value + off; | |
4937 | if (access_size > 0) | |
4938 | max_off = reg->smax_value + off + access_size - 1; | |
4939 | else | |
4940 | max_off = min_off; | |
4941 | } | |
4942 | ||
4943 | err = check_stack_slot_within_bounds(min_off, state, type); | |
4944 | if (!err) | |
4945 | err = check_stack_slot_within_bounds(max_off, state, type); | |
4946 | ||
4947 | if (err) { | |
4948 | if (tnum_is_const(reg->var_off)) { | |
4949 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
4950 | err_extra, regno, off, access_size); | |
4951 | } else { | |
4952 | char tn_buf[48]; | |
4953 | ||
4954 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4955 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
4956 | err_extra, regno, tn_buf, access_size); | |
4957 | } | |
4958 | } | |
4959 | return err; | |
4960 | } | |
41c48f3a | 4961 | |
17a52670 AS |
4962 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
4963 | * if t==write, value_regno is a register which value is stored into memory | |
4964 | * if t==read, value_regno is a register which will receive the value from memory | |
4965 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
4966 | * if t==read && value_regno==-1, don't care what we read from memory | |
4967 | */ | |
ca369602 DB |
4968 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
4969 | int off, int bpf_size, enum bpf_access_type t, | |
4970 | int value_regno, bool strict_alignment_once) | |
17a52670 | 4971 | { |
638f5b90 AS |
4972 | struct bpf_reg_state *regs = cur_regs(env); |
4973 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 4974 | struct bpf_func_state *state; |
17a52670 AS |
4975 | int size, err = 0; |
4976 | ||
4977 | size = bpf_size_to_bytes(bpf_size); | |
4978 | if (size < 0) | |
4979 | return size; | |
4980 | ||
f1174f77 | 4981 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 4982 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
4983 | if (err) |
4984 | return err; | |
17a52670 | 4985 | |
f1174f77 EC |
4986 | /* for access checks, reg->off is just part of off */ |
4987 | off += reg->off; | |
4988 | ||
69c087ba YS |
4989 | if (reg->type == PTR_TO_MAP_KEY) { |
4990 | if (t == BPF_WRITE) { | |
4991 | verbose(env, "write to change key R%d not allowed\n", regno); | |
4992 | return -EACCES; | |
4993 | } | |
4994 | ||
4995 | err = check_mem_region_access(env, regno, off, size, | |
4996 | reg->map_ptr->key_size, false); | |
4997 | if (err) | |
4998 | return err; | |
4999 | if (value_regno >= 0) | |
5000 | mark_reg_unknown(env, regs, value_regno); | |
5001 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
aa3496ac | 5002 | struct btf_field *kptr_field = NULL; |
61df10c7 | 5003 | |
1be7f75d AS |
5004 | if (t == BPF_WRITE && value_regno >= 0 && |
5005 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5006 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
5007 | return -EACCES; |
5008 | } | |
591fe988 DB |
5009 | err = check_map_access_type(env, regno, off, size, t); |
5010 | if (err) | |
5011 | return err; | |
61df10c7 KKD |
5012 | err = check_map_access(env, regno, off, size, false, ACCESS_DIRECT); |
5013 | if (err) | |
5014 | return err; | |
5015 | if (tnum_is_const(reg->var_off)) | |
aa3496ac KKD |
5016 | kptr_field = btf_record_find(reg->map_ptr->record, |
5017 | off + reg->var_off.value, BPF_KPTR); | |
5018 | if (kptr_field) { | |
5019 | err = check_map_kptr_access(env, regno, value_regno, insn_idx, kptr_field); | |
61df10c7 | 5020 | } else if (t == BPF_READ && value_regno >= 0) { |
a23740ec AN |
5021 | struct bpf_map *map = reg->map_ptr; |
5022 | ||
5023 | /* if map is read-only, track its contents as scalars */ | |
5024 | if (tnum_is_const(reg->var_off) && | |
5025 | bpf_map_is_rdonly(map) && | |
5026 | map->ops->map_direct_value_addr) { | |
5027 | int map_off = off + reg->var_off.value; | |
5028 | u64 val = 0; | |
5029 | ||
5030 | err = bpf_map_direct_read(map, map_off, size, | |
5031 | &val); | |
5032 | if (err) | |
5033 | return err; | |
5034 | ||
5035 | regs[value_regno].type = SCALAR_VALUE; | |
5036 | __mark_reg_known(®s[value_regno], val); | |
5037 | } else { | |
5038 | mark_reg_unknown(env, regs, value_regno); | |
5039 | } | |
5040 | } | |
34d3a78c HL |
5041 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
5042 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
5043 | ||
5044 | if (type_may_be_null(reg->type)) { | |
5045 | verbose(env, "R%d invalid mem access '%s'\n", regno, | |
5046 | reg_type_str(env, reg->type)); | |
5047 | return -EACCES; | |
5048 | } | |
5049 | ||
5050 | if (t == BPF_WRITE && rdonly_mem) { | |
5051 | verbose(env, "R%d cannot write into %s\n", | |
5052 | regno, reg_type_str(env, reg->type)); | |
5053 | return -EACCES; | |
5054 | } | |
5055 | ||
457f4436 AN |
5056 | if (t == BPF_WRITE && value_regno >= 0 && |
5057 | is_pointer_value(env, value_regno)) { | |
5058 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
5059 | return -EACCES; | |
5060 | } | |
34d3a78c | 5061 | |
457f4436 AN |
5062 | err = check_mem_region_access(env, regno, off, size, |
5063 | reg->mem_size, false); | |
34d3a78c | 5064 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
457f4436 | 5065 | mark_reg_unknown(env, regs, value_regno); |
1a0dc1ac | 5066 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 5067 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 5068 | struct btf *btf = NULL; |
9e15db66 | 5069 | u32 btf_id = 0; |
19de99f7 | 5070 | |
1be7f75d AS |
5071 | if (t == BPF_WRITE && value_regno >= 0 && |
5072 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5073 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
5074 | return -EACCES; |
5075 | } | |
f1174f77 | 5076 | |
be80a1d3 | 5077 | err = check_ptr_off_reg(env, reg, regno); |
58990d1f DB |
5078 | if (err < 0) |
5079 | return err; | |
5080 | ||
c6f1bfe8 YS |
5081 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, |
5082 | &btf_id); | |
9e15db66 AS |
5083 | if (err) |
5084 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 5085 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 5086 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
5087 | * PTR_TO_PACKET[_META,_END]. In the latter |
5088 | * case, we know the offset is zero. | |
f1174f77 | 5089 | */ |
46f8bc92 | 5090 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 5091 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 5092 | } else { |
638f5b90 | 5093 | mark_reg_known_zero(env, regs, |
61bd5218 | 5094 | value_regno); |
c25b2ae1 | 5095 | if (type_may_be_null(reg_type)) |
46f8bc92 | 5096 | regs[value_regno].id = ++env->id_gen; |
5327ed3d JW |
5097 | /* A load of ctx field could have different |
5098 | * actual load size with the one encoded in the | |
5099 | * insn. When the dst is PTR, it is for sure not | |
5100 | * a sub-register. | |
5101 | */ | |
5102 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
c25b2ae1 | 5103 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 5104 | regs[value_regno].btf = btf; |
9e15db66 | 5105 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 5106 | } |
46f8bc92 | 5107 | } |
638f5b90 | 5108 | regs[value_regno].type = reg_type; |
969bf05e | 5109 | } |
17a52670 | 5110 | |
f1174f77 | 5111 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
5112 | /* Basic bounds checks. */ |
5113 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
5114 | if (err) |
5115 | return err; | |
8726679a | 5116 | |
f4d7e40a AS |
5117 | state = func(env, reg); |
5118 | err = update_stack_depth(env, state, off); | |
5119 | if (err) | |
5120 | return err; | |
8726679a | 5121 | |
01f810ac AM |
5122 | if (t == BPF_READ) |
5123 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 5124 | value_regno); |
01f810ac AM |
5125 | else |
5126 | err = check_stack_write(env, regno, off, size, | |
5127 | value_regno, insn_idx); | |
de8f3a83 | 5128 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 5129 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 5130 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
5131 | return -EACCES; |
5132 | } | |
4acf6c0b BB |
5133 | if (t == BPF_WRITE && value_regno >= 0 && |
5134 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
5135 | verbose(env, "R%d leaks addr into packet\n", |
5136 | value_regno); | |
4acf6c0b BB |
5137 | return -EACCES; |
5138 | } | |
9fd29c08 | 5139 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 5140 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 5141 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
5142 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
5143 | if (t == BPF_WRITE && value_regno >= 0 && | |
5144 | is_pointer_value(env, value_regno)) { | |
5145 | verbose(env, "R%d leaks addr into flow keys\n", | |
5146 | value_regno); | |
5147 | return -EACCES; | |
5148 | } | |
5149 | ||
5150 | err = check_flow_keys_access(env, off, size); | |
5151 | if (!err && t == BPF_READ && value_regno >= 0) | |
5152 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 5153 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 5154 | if (t == BPF_WRITE) { |
46f8bc92 | 5155 | verbose(env, "R%d cannot write into %s\n", |
c25b2ae1 | 5156 | regno, reg_type_str(env, reg->type)); |
c64b7983 JS |
5157 | return -EACCES; |
5158 | } | |
5f456649 | 5159 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
5160 | if (!err && value_regno >= 0) |
5161 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
5162 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
5163 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
5164 | if (!err && t == BPF_READ && value_regno >= 0) | |
5165 | mark_reg_unknown(env, regs, value_regno); | |
bff61f6f HL |
5166 | } else if (base_type(reg->type) == PTR_TO_BTF_ID && |
5167 | !type_may_be_null(reg->type)) { | |
9e15db66 AS |
5168 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
5169 | value_regno); | |
41c48f3a AI |
5170 | } else if (reg->type == CONST_PTR_TO_MAP) { |
5171 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
5172 | value_regno); | |
20b2aff4 HL |
5173 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
5174 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
20b2aff4 HL |
5175 | u32 *max_access; |
5176 | ||
5177 | if (rdonly_mem) { | |
5178 | if (t == BPF_WRITE) { | |
5179 | verbose(env, "R%d cannot write into %s\n", | |
5180 | regno, reg_type_str(env, reg->type)); | |
5181 | return -EACCES; | |
5182 | } | |
20b2aff4 HL |
5183 | max_access = &env->prog->aux->max_rdonly_access; |
5184 | } else { | |
20b2aff4 | 5185 | max_access = &env->prog->aux->max_rdwr_access; |
afbf21dc | 5186 | } |
20b2aff4 | 5187 | |
f6dfbe31 | 5188 | err = check_buffer_access(env, reg, regno, off, size, false, |
44e9a741 | 5189 | max_access); |
20b2aff4 HL |
5190 | |
5191 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) | |
afbf21dc | 5192 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 5193 | } else { |
61bd5218 | 5194 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
c25b2ae1 | 5195 | reg_type_str(env, reg->type)); |
17a52670 AS |
5196 | return -EACCES; |
5197 | } | |
969bf05e | 5198 | |
f1174f77 | 5199 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 5200 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 5201 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 5202 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 5203 | } |
17a52670 AS |
5204 | return err; |
5205 | } | |
5206 | ||
91c960b0 | 5207 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 5208 | { |
5ffa2550 | 5209 | int load_reg; |
17a52670 AS |
5210 | int err; |
5211 | ||
5ca419f2 BJ |
5212 | switch (insn->imm) { |
5213 | case BPF_ADD: | |
5214 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
5215 | case BPF_AND: |
5216 | case BPF_AND | BPF_FETCH: | |
5217 | case BPF_OR: | |
5218 | case BPF_OR | BPF_FETCH: | |
5219 | case BPF_XOR: | |
5220 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
5221 | case BPF_XCHG: |
5222 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
5223 | break; |
5224 | default: | |
91c960b0 BJ |
5225 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
5226 | return -EINVAL; | |
5227 | } | |
5228 | ||
5229 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
5230 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
5231 | return -EINVAL; |
5232 | } | |
5233 | ||
5234 | /* check src1 operand */ | |
dc503a8a | 5235 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5236 | if (err) |
5237 | return err; | |
5238 | ||
5239 | /* check src2 operand */ | |
dc503a8a | 5240 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5241 | if (err) |
5242 | return err; | |
5243 | ||
5ffa2550 BJ |
5244 | if (insn->imm == BPF_CMPXCHG) { |
5245 | /* Check comparison of R0 with memory location */ | |
a82fe085 DB |
5246 | const u32 aux_reg = BPF_REG_0; |
5247 | ||
5248 | err = check_reg_arg(env, aux_reg, SRC_OP); | |
5ffa2550 BJ |
5249 | if (err) |
5250 | return err; | |
a82fe085 DB |
5251 | |
5252 | if (is_pointer_value(env, aux_reg)) { | |
5253 | verbose(env, "R%d leaks addr into mem\n", aux_reg); | |
5254 | return -EACCES; | |
5255 | } | |
5ffa2550 BJ |
5256 | } |
5257 | ||
6bdf6abc | 5258 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 5259 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
5260 | return -EACCES; |
5261 | } | |
5262 | ||
ca369602 | 5263 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 5264 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
5265 | is_flow_key_reg(env, insn->dst_reg) || |
5266 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 5267 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f | 5268 | insn->dst_reg, |
c25b2ae1 | 5269 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
5270 | return -EACCES; |
5271 | } | |
5272 | ||
37086bfd BJ |
5273 | if (insn->imm & BPF_FETCH) { |
5274 | if (insn->imm == BPF_CMPXCHG) | |
5275 | load_reg = BPF_REG_0; | |
5276 | else | |
5277 | load_reg = insn->src_reg; | |
5278 | ||
5279 | /* check and record load of old value */ | |
5280 | err = check_reg_arg(env, load_reg, DST_OP); | |
5281 | if (err) | |
5282 | return err; | |
5283 | } else { | |
5284 | /* This instruction accesses a memory location but doesn't | |
5285 | * actually load it into a register. | |
5286 | */ | |
5287 | load_reg = -1; | |
5288 | } | |
5289 | ||
7d3baf0a DB |
5290 | /* Check whether we can read the memory, with second call for fetch |
5291 | * case to simulate the register fill. | |
5292 | */ | |
31fd8581 | 5293 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
7d3baf0a DB |
5294 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
5295 | if (!err && load_reg >= 0) | |
5296 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, | |
5297 | BPF_SIZE(insn->code), BPF_READ, load_reg, | |
5298 | true); | |
17a52670 AS |
5299 | if (err) |
5300 | return err; | |
5301 | ||
7d3baf0a | 5302 | /* Check whether we can write into the same memory. */ |
5ca419f2 BJ |
5303 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
5304 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
5305 | if (err) | |
5306 | return err; | |
5307 | ||
5ca419f2 | 5308 | return 0; |
17a52670 AS |
5309 | } |
5310 | ||
01f810ac AM |
5311 | /* When register 'regno' is used to read the stack (either directly or through |
5312 | * a helper function) make sure that it's within stack boundary and, depending | |
5313 | * on the access type, that all elements of the stack are initialized. | |
5314 | * | |
5315 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
5316 | * | |
5317 | * All registers that have been spilled on the stack in the slots within the | |
5318 | * read offsets are marked as read. | |
5319 | */ | |
5320 | static int check_stack_range_initialized( | |
5321 | struct bpf_verifier_env *env, int regno, int off, | |
5322 | int access_size, bool zero_size_allowed, | |
61df10c7 | 5323 | enum bpf_access_src type, struct bpf_call_arg_meta *meta) |
2011fccf AI |
5324 | { |
5325 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
5326 | struct bpf_func_state *state = func(env, reg); |
5327 | int err, min_off, max_off, i, j, slot, spi; | |
5328 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
5329 | enum bpf_access_type bounds_check_type; | |
5330 | /* Some accesses can write anything into the stack, others are | |
5331 | * read-only. | |
5332 | */ | |
5333 | bool clobber = false; | |
2011fccf | 5334 | |
01f810ac AM |
5335 | if (access_size == 0 && !zero_size_allowed) { |
5336 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
5337 | return -EACCES; |
5338 | } | |
2011fccf | 5339 | |
01f810ac AM |
5340 | if (type == ACCESS_HELPER) { |
5341 | /* The bounds checks for writes are more permissive than for | |
5342 | * reads. However, if raw_mode is not set, we'll do extra | |
5343 | * checks below. | |
5344 | */ | |
5345 | bounds_check_type = BPF_WRITE; | |
5346 | clobber = true; | |
5347 | } else { | |
5348 | bounds_check_type = BPF_READ; | |
5349 | } | |
5350 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
5351 | type, bounds_check_type); | |
5352 | if (err) | |
5353 | return err; | |
5354 | ||
17a52670 | 5355 | |
2011fccf | 5356 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 5357 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 5358 | } else { |
088ec26d AI |
5359 | /* Variable offset is prohibited for unprivileged mode for |
5360 | * simplicity since it requires corresponding support in | |
5361 | * Spectre masking for stack ALU. | |
5362 | * See also retrieve_ptr_limit(). | |
5363 | */ | |
2c78ee89 | 5364 | if (!env->bypass_spec_v1) { |
088ec26d | 5365 | char tn_buf[48]; |
f1174f77 | 5366 | |
088ec26d | 5367 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
5368 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
5369 | regno, err_extra, tn_buf); | |
088ec26d AI |
5370 | return -EACCES; |
5371 | } | |
f2bcd05e AI |
5372 | /* Only initialized buffer on stack is allowed to be accessed |
5373 | * with variable offset. With uninitialized buffer it's hard to | |
5374 | * guarantee that whole memory is marked as initialized on | |
5375 | * helper return since specific bounds are unknown what may | |
5376 | * cause uninitialized stack leaking. | |
5377 | */ | |
5378 | if (meta && meta->raw_mode) | |
5379 | meta = NULL; | |
5380 | ||
01f810ac AM |
5381 | min_off = reg->smin_value + off; |
5382 | max_off = reg->smax_value + off; | |
17a52670 AS |
5383 | } |
5384 | ||
435faee1 DB |
5385 | if (meta && meta->raw_mode) { |
5386 | meta->access_size = access_size; | |
5387 | meta->regno = regno; | |
5388 | return 0; | |
5389 | } | |
5390 | ||
2011fccf | 5391 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
5392 | u8 *stype; |
5393 | ||
2011fccf | 5394 | slot = -i - 1; |
638f5b90 | 5395 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
5396 | if (state->allocated_stack <= slot) |
5397 | goto err; | |
5398 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
5399 | if (*stype == STACK_MISC) | |
5400 | goto mark; | |
5401 | if (*stype == STACK_ZERO) { | |
01f810ac AM |
5402 | if (clobber) { |
5403 | /* helper can write anything into the stack */ | |
5404 | *stype = STACK_MISC; | |
5405 | } | |
cc2b14d5 | 5406 | goto mark; |
17a52670 | 5407 | } |
1d68f22b | 5408 | |
27113c59 | 5409 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
5410 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
5411 | env->allow_ptr_leaks)) { | |
01f810ac AM |
5412 | if (clobber) { |
5413 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
5414 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 5415 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 5416 | } |
f7cf25b2 AS |
5417 | goto mark; |
5418 | } | |
5419 | ||
cc2b14d5 | 5420 | err: |
2011fccf | 5421 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
5422 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
5423 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
5424 | } else { |
5425 | char tn_buf[48]; | |
5426 | ||
5427 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
5428 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
5429 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 5430 | } |
cc2b14d5 AS |
5431 | return -EACCES; |
5432 | mark: | |
5433 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
5434 | * the whole slot to be marked as 'read' | |
5435 | */ | |
679c782d | 5436 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
5437 | state->stack[spi].spilled_ptr.parent, |
5438 | REG_LIVE_READ64); | |
261f4664 KKD |
5439 | /* We do not set REG_LIVE_WRITTEN for stack slot, as we can not |
5440 | * be sure that whether stack slot is written to or not. Hence, | |
5441 | * we must still conservatively propagate reads upwards even if | |
5442 | * helper may write to the entire memory range. | |
5443 | */ | |
17a52670 | 5444 | } |
2011fccf | 5445 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
5446 | } |
5447 | ||
06c1c049 GB |
5448 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
5449 | int access_size, bool zero_size_allowed, | |
5450 | struct bpf_call_arg_meta *meta) | |
5451 | { | |
638f5b90 | 5452 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
20b2aff4 | 5453 | u32 *max_access; |
06c1c049 | 5454 | |
20b2aff4 | 5455 | switch (base_type(reg->type)) { |
06c1c049 | 5456 | case PTR_TO_PACKET: |
de8f3a83 | 5457 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
5458 | return check_packet_access(env, regno, reg->off, access_size, |
5459 | zero_size_allowed); | |
69c087ba | 5460 | case PTR_TO_MAP_KEY: |
7b3552d3 KKD |
5461 | if (meta && meta->raw_mode) { |
5462 | verbose(env, "R%d cannot write into %s\n", regno, | |
5463 | reg_type_str(env, reg->type)); | |
5464 | return -EACCES; | |
5465 | } | |
69c087ba YS |
5466 | return check_mem_region_access(env, regno, reg->off, access_size, |
5467 | reg->map_ptr->key_size, false); | |
06c1c049 | 5468 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
5469 | if (check_map_access_type(env, regno, reg->off, access_size, |
5470 | meta && meta->raw_mode ? BPF_WRITE : | |
5471 | BPF_READ)) | |
5472 | return -EACCES; | |
9fd29c08 | 5473 | return check_map_access(env, regno, reg->off, access_size, |
61df10c7 | 5474 | zero_size_allowed, ACCESS_HELPER); |
457f4436 | 5475 | case PTR_TO_MEM: |
97e6d7da KKD |
5476 | if (type_is_rdonly_mem(reg->type)) { |
5477 | if (meta && meta->raw_mode) { | |
5478 | verbose(env, "R%d cannot write into %s\n", regno, | |
5479 | reg_type_str(env, reg->type)); | |
5480 | return -EACCES; | |
5481 | } | |
5482 | } | |
457f4436 AN |
5483 | return check_mem_region_access(env, regno, reg->off, |
5484 | access_size, reg->mem_size, | |
5485 | zero_size_allowed); | |
20b2aff4 HL |
5486 | case PTR_TO_BUF: |
5487 | if (type_is_rdonly_mem(reg->type)) { | |
97e6d7da KKD |
5488 | if (meta && meta->raw_mode) { |
5489 | verbose(env, "R%d cannot write into %s\n", regno, | |
5490 | reg_type_str(env, reg->type)); | |
20b2aff4 | 5491 | return -EACCES; |
97e6d7da | 5492 | } |
20b2aff4 | 5493 | |
20b2aff4 HL |
5494 | max_access = &env->prog->aux->max_rdonly_access; |
5495 | } else { | |
20b2aff4 HL |
5496 | max_access = &env->prog->aux->max_rdwr_access; |
5497 | } | |
afbf21dc YS |
5498 | return check_buffer_access(env, reg, regno, reg->off, |
5499 | access_size, zero_size_allowed, | |
44e9a741 | 5500 | max_access); |
0d004c02 | 5501 | case PTR_TO_STACK: |
01f810ac AM |
5502 | return check_stack_range_initialized( |
5503 | env, | |
5504 | regno, reg->off, access_size, | |
5505 | zero_size_allowed, ACCESS_HELPER, meta); | |
15baa55f BT |
5506 | case PTR_TO_CTX: |
5507 | /* in case the function doesn't know how to access the context, | |
5508 | * (because we are in a program of type SYSCALL for example), we | |
5509 | * can not statically check its size. | |
5510 | * Dynamically check it now. | |
5511 | */ | |
5512 | if (!env->ops->convert_ctx_access) { | |
5513 | enum bpf_access_type atype = meta && meta->raw_mode ? BPF_WRITE : BPF_READ; | |
5514 | int offset = access_size - 1; | |
5515 | ||
5516 | /* Allow zero-byte read from PTR_TO_CTX */ | |
5517 | if (access_size == 0) | |
5518 | return zero_size_allowed ? 0 : -EACCES; | |
5519 | ||
5520 | return check_mem_access(env, env->insn_idx, regno, offset, BPF_B, | |
5521 | atype, -1, false); | |
5522 | } | |
5523 | ||
5524 | fallthrough; | |
0d004c02 LB |
5525 | default: /* scalar_value or invalid ptr */ |
5526 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
5527 | if (zero_size_allowed && access_size == 0 && | |
5528 | register_is_null(reg)) | |
5529 | return 0; | |
5530 | ||
c25b2ae1 HL |
5531 | verbose(env, "R%d type=%s ", regno, |
5532 | reg_type_str(env, reg->type)); | |
5533 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); | |
0d004c02 | 5534 | return -EACCES; |
06c1c049 GB |
5535 | } |
5536 | } | |
5537 | ||
d583691c KKD |
5538 | static int check_mem_size_reg(struct bpf_verifier_env *env, |
5539 | struct bpf_reg_state *reg, u32 regno, | |
5540 | bool zero_size_allowed, | |
5541 | struct bpf_call_arg_meta *meta) | |
5542 | { | |
5543 | int err; | |
5544 | ||
5545 | /* This is used to refine r0 return value bounds for helpers | |
5546 | * that enforce this value as an upper bound on return values. | |
5547 | * See do_refine_retval_range() for helpers that can refine | |
5548 | * the return value. C type of helper is u32 so we pull register | |
5549 | * bound from umax_value however, if negative verifier errors | |
5550 | * out. Only upper bounds can be learned because retval is an | |
5551 | * int type and negative retvals are allowed. | |
5552 | */ | |
be77354a | 5553 | meta->msize_max_value = reg->umax_value; |
d583691c KKD |
5554 | |
5555 | /* The register is SCALAR_VALUE; the access check | |
5556 | * happens using its boundaries. | |
5557 | */ | |
5558 | if (!tnum_is_const(reg->var_off)) | |
5559 | /* For unprivileged variable accesses, disable raw | |
5560 | * mode so that the program is required to | |
5561 | * initialize all the memory that the helper could | |
5562 | * just partially fill up. | |
5563 | */ | |
5564 | meta = NULL; | |
5565 | ||
5566 | if (reg->smin_value < 0) { | |
5567 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", | |
5568 | regno); | |
5569 | return -EACCES; | |
5570 | } | |
5571 | ||
5572 | if (reg->umin_value == 0) { | |
5573 | err = check_helper_mem_access(env, regno - 1, 0, | |
5574 | zero_size_allowed, | |
5575 | meta); | |
5576 | if (err) | |
5577 | return err; | |
5578 | } | |
5579 | ||
5580 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { | |
5581 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
5582 | regno); | |
5583 | return -EACCES; | |
5584 | } | |
5585 | err = check_helper_mem_access(env, regno - 1, | |
5586 | reg->umax_value, | |
5587 | zero_size_allowed, meta); | |
5588 | if (!err) | |
5589 | err = mark_chain_precision(env, regno); | |
5590 | return err; | |
5591 | } | |
5592 | ||
e5069b9c DB |
5593 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
5594 | u32 regno, u32 mem_size) | |
5595 | { | |
be77354a KKD |
5596 | bool may_be_null = type_may_be_null(reg->type); |
5597 | struct bpf_reg_state saved_reg; | |
5598 | struct bpf_call_arg_meta meta; | |
5599 | int err; | |
5600 | ||
e5069b9c DB |
5601 | if (register_is_null(reg)) |
5602 | return 0; | |
5603 | ||
be77354a KKD |
5604 | memset(&meta, 0, sizeof(meta)); |
5605 | /* Assuming that the register contains a value check if the memory | |
5606 | * access is safe. Temporarily save and restore the register's state as | |
5607 | * the conversion shouldn't be visible to a caller. | |
5608 | */ | |
5609 | if (may_be_null) { | |
5610 | saved_reg = *reg; | |
e5069b9c | 5611 | mark_ptr_not_null_reg(reg); |
e5069b9c DB |
5612 | } |
5613 | ||
be77354a KKD |
5614 | err = check_helper_mem_access(env, regno, mem_size, true, &meta); |
5615 | /* Check access for BPF_WRITE */ | |
5616 | meta.raw_mode = true; | |
5617 | err = err ?: check_helper_mem_access(env, regno, mem_size, true, &meta); | |
5618 | ||
5619 | if (may_be_null) | |
5620 | *reg = saved_reg; | |
5621 | ||
5622 | return err; | |
e5069b9c DB |
5623 | } |
5624 | ||
00b85860 KKD |
5625 | static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
5626 | u32 regno) | |
d583691c KKD |
5627 | { |
5628 | struct bpf_reg_state *mem_reg = &cur_regs(env)[regno - 1]; | |
5629 | bool may_be_null = type_may_be_null(mem_reg->type); | |
5630 | struct bpf_reg_state saved_reg; | |
be77354a | 5631 | struct bpf_call_arg_meta meta; |
d583691c KKD |
5632 | int err; |
5633 | ||
5634 | WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); | |
5635 | ||
be77354a KKD |
5636 | memset(&meta, 0, sizeof(meta)); |
5637 | ||
d583691c KKD |
5638 | if (may_be_null) { |
5639 | saved_reg = *mem_reg; | |
5640 | mark_ptr_not_null_reg(mem_reg); | |
5641 | } | |
5642 | ||
be77354a KKD |
5643 | err = check_mem_size_reg(env, reg, regno, true, &meta); |
5644 | /* Check access for BPF_WRITE */ | |
5645 | meta.raw_mode = true; | |
5646 | err = err ?: check_mem_size_reg(env, reg, regno, true, &meta); | |
d583691c KKD |
5647 | |
5648 | if (may_be_null) | |
5649 | *mem_reg = saved_reg; | |
5650 | return err; | |
5651 | } | |
5652 | ||
d83525ca | 5653 | /* Implementation details: |
4e814da0 KKD |
5654 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL. |
5655 | * bpf_obj_new returns PTR_TO_BTF_ID | MEM_ALLOC | PTR_MAYBE_NULL. | |
d83525ca | 5656 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
4e814da0 KKD |
5657 | * Two separate bpf_obj_new will also have different reg->id. |
5658 | * For traditional PTR_TO_MAP_VALUE or PTR_TO_BTF_ID | MEM_ALLOC, the verifier | |
5659 | * clears reg->id after value_or_null->value transition, since the verifier only | |
5660 | * cares about the range of access to valid map value pointer and doesn't care | |
5661 | * about actual address of the map element. | |
d83525ca AS |
5662 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
5663 | * reg->id > 0 after value_or_null->value transition. By doing so | |
5664 | * two bpf_map_lookups will be considered two different pointers that | |
4e814da0 KKD |
5665 | * point to different bpf_spin_locks. Likewise for pointers to allocated objects |
5666 | * returned from bpf_obj_new. | |
d83525ca AS |
5667 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
5668 | * dead-locks. | |
5669 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
5670 | * reg_is_refcounted() logic. The verifier needs to remember only | |
5671 | * one spin_lock instead of array of acquired_refs. | |
d0d78c1d | 5672 | * cur_state->active_lock remembers which map value element or allocated |
4e814da0 | 5673 | * object got locked and clears it after bpf_spin_unlock. |
d83525ca AS |
5674 | */ |
5675 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
5676 | bool is_lock) | |
5677 | { | |
5678 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5679 | struct bpf_verifier_state *cur = env->cur_state; | |
5680 | bool is_const = tnum_is_const(reg->var_off); | |
d83525ca | 5681 | u64 val = reg->var_off.value; |
4e814da0 KKD |
5682 | struct bpf_map *map = NULL; |
5683 | struct btf *btf = NULL; | |
5684 | struct btf_record *rec; | |
d83525ca | 5685 | |
d83525ca AS |
5686 | if (!is_const) { |
5687 | verbose(env, | |
5688 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
5689 | regno); | |
5690 | return -EINVAL; | |
5691 | } | |
4e814da0 KKD |
5692 | if (reg->type == PTR_TO_MAP_VALUE) { |
5693 | map = reg->map_ptr; | |
5694 | if (!map->btf) { | |
5695 | verbose(env, | |
5696 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
5697 | map->name); | |
5698 | return -EINVAL; | |
5699 | } | |
5700 | } else { | |
5701 | btf = reg->btf; | |
d83525ca | 5702 | } |
4e814da0 KKD |
5703 | |
5704 | rec = reg_btf_record(reg); | |
5705 | if (!btf_record_has_field(rec, BPF_SPIN_LOCK)) { | |
5706 | verbose(env, "%s '%s' has no valid bpf_spin_lock\n", map ? "map" : "local", | |
5707 | map ? map->name : "kptr"); | |
d83525ca AS |
5708 | return -EINVAL; |
5709 | } | |
4e814da0 | 5710 | if (rec->spin_lock_off != val + reg->off) { |
db559117 | 5711 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock' that is at %d\n", |
4e814da0 | 5712 | val + reg->off, rec->spin_lock_off); |
d83525ca AS |
5713 | return -EINVAL; |
5714 | } | |
5715 | if (is_lock) { | |
d0d78c1d | 5716 | if (cur->active_lock.ptr) { |
d83525ca AS |
5717 | verbose(env, |
5718 | "Locking two bpf_spin_locks are not allowed\n"); | |
5719 | return -EINVAL; | |
5720 | } | |
d0d78c1d KKD |
5721 | if (map) |
5722 | cur->active_lock.ptr = map; | |
5723 | else | |
5724 | cur->active_lock.ptr = btf; | |
5725 | cur->active_lock.id = reg->id; | |
d83525ca | 5726 | } else { |
534e86bc | 5727 | struct bpf_func_state *fstate = cur_func(env); |
d0d78c1d | 5728 | void *ptr; |
534e86bc | 5729 | int i; |
d0d78c1d KKD |
5730 | |
5731 | if (map) | |
5732 | ptr = map; | |
5733 | else | |
5734 | ptr = btf; | |
5735 | ||
5736 | if (!cur->active_lock.ptr) { | |
d83525ca AS |
5737 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
5738 | return -EINVAL; | |
5739 | } | |
d0d78c1d KKD |
5740 | if (cur->active_lock.ptr != ptr || |
5741 | cur->active_lock.id != reg->id) { | |
d83525ca AS |
5742 | verbose(env, "bpf_spin_unlock of different lock\n"); |
5743 | return -EINVAL; | |
5744 | } | |
d0d78c1d KKD |
5745 | cur->active_lock.ptr = NULL; |
5746 | cur->active_lock.id = 0; | |
534e86bc | 5747 | |
1f82dffc | 5748 | for (i = fstate->acquired_refs - 1; i >= 0; i--) { |
534e86bc KKD |
5749 | int err; |
5750 | ||
5751 | /* Complain on error because this reference state cannot | |
5752 | * be freed before this point, as bpf_spin_lock critical | |
5753 | * section does not allow functions that release the | |
5754 | * allocated object immediately. | |
5755 | */ | |
5756 | if (!fstate->refs[i].release_on_unlock) | |
5757 | continue; | |
5758 | err = release_reference(env, fstate->refs[i].id); | |
5759 | if (err) { | |
5760 | verbose(env, "failed to release release_on_unlock reference"); | |
5761 | return err; | |
5762 | } | |
5763 | } | |
d83525ca AS |
5764 | } |
5765 | return 0; | |
5766 | } | |
5767 | ||
b00628b1 AS |
5768 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
5769 | struct bpf_call_arg_meta *meta) | |
5770 | { | |
5771 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5772 | bool is_const = tnum_is_const(reg->var_off); | |
5773 | struct bpf_map *map = reg->map_ptr; | |
5774 | u64 val = reg->var_off.value; | |
5775 | ||
5776 | if (!is_const) { | |
5777 | verbose(env, | |
5778 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
5779 | regno); | |
5780 | return -EINVAL; | |
5781 | } | |
5782 | if (!map->btf) { | |
5783 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
5784 | map->name); | |
5785 | return -EINVAL; | |
5786 | } | |
db559117 KKD |
5787 | if (!btf_record_has_field(map->record, BPF_TIMER)) { |
5788 | verbose(env, "map '%s' has no valid bpf_timer\n", map->name); | |
68134668 AS |
5789 | return -EINVAL; |
5790 | } | |
db559117 | 5791 | if (map->record->timer_off != val + reg->off) { |
68134668 | 5792 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
db559117 | 5793 | val + reg->off, map->record->timer_off); |
b00628b1 AS |
5794 | return -EINVAL; |
5795 | } | |
5796 | if (meta->map_ptr) { | |
5797 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
5798 | return -EFAULT; | |
5799 | } | |
3e8ce298 | 5800 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
5801 | meta->map_ptr = map; |
5802 | return 0; | |
5803 | } | |
5804 | ||
c0a5a21c KKD |
5805 | static int process_kptr_func(struct bpf_verifier_env *env, int regno, |
5806 | struct bpf_call_arg_meta *meta) | |
5807 | { | |
5808 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
c0a5a21c | 5809 | struct bpf_map *map_ptr = reg->map_ptr; |
aa3496ac | 5810 | struct btf_field *kptr_field; |
c0a5a21c | 5811 | u32 kptr_off; |
c0a5a21c KKD |
5812 | |
5813 | if (!tnum_is_const(reg->var_off)) { | |
5814 | verbose(env, | |
5815 | "R%d doesn't have constant offset. kptr has to be at the constant offset\n", | |
5816 | regno); | |
5817 | return -EINVAL; | |
5818 | } | |
5819 | if (!map_ptr->btf) { | |
5820 | verbose(env, "map '%s' has to have BTF in order to use bpf_kptr_xchg\n", | |
5821 | map_ptr->name); | |
5822 | return -EINVAL; | |
5823 | } | |
aa3496ac KKD |
5824 | if (!btf_record_has_field(map_ptr->record, BPF_KPTR)) { |
5825 | verbose(env, "map '%s' has no valid kptr\n", map_ptr->name); | |
c0a5a21c KKD |
5826 | return -EINVAL; |
5827 | } | |
5828 | ||
5829 | meta->map_ptr = map_ptr; | |
5830 | kptr_off = reg->off + reg->var_off.value; | |
aa3496ac KKD |
5831 | kptr_field = btf_record_find(map_ptr->record, kptr_off, BPF_KPTR); |
5832 | if (!kptr_field) { | |
c0a5a21c KKD |
5833 | verbose(env, "off=%d doesn't point to kptr\n", kptr_off); |
5834 | return -EACCES; | |
5835 | } | |
aa3496ac | 5836 | if (kptr_field->type != BPF_KPTR_REF) { |
c0a5a21c KKD |
5837 | verbose(env, "off=%d kptr isn't referenced kptr\n", kptr_off); |
5838 | return -EACCES; | |
5839 | } | |
aa3496ac | 5840 | meta->kptr_field = kptr_field; |
c0a5a21c KKD |
5841 | return 0; |
5842 | } | |
5843 | ||
90133415 DB |
5844 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
5845 | { | |
5846 | return type == ARG_CONST_SIZE || | |
5847 | type == ARG_CONST_SIZE_OR_ZERO; | |
5848 | } | |
5849 | ||
8f14852e KKD |
5850 | static bool arg_type_is_release(enum bpf_arg_type type) |
5851 | { | |
5852 | return type & OBJ_RELEASE; | |
5853 | } | |
5854 | ||
97e03f52 JK |
5855 | static bool arg_type_is_dynptr(enum bpf_arg_type type) |
5856 | { | |
5857 | return base_type(type) == ARG_PTR_TO_DYNPTR; | |
5858 | } | |
5859 | ||
57c3bb72 AI |
5860 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
5861 | { | |
5862 | if (type == ARG_PTR_TO_INT) | |
5863 | return sizeof(u32); | |
5864 | else if (type == ARG_PTR_TO_LONG) | |
5865 | return sizeof(u64); | |
5866 | ||
5867 | return -EINVAL; | |
5868 | } | |
5869 | ||
912f442c LB |
5870 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
5871 | const struct bpf_call_arg_meta *meta, | |
5872 | enum bpf_arg_type *arg_type) | |
5873 | { | |
5874 | if (!meta->map_ptr) { | |
5875 | /* kernel subsystem misconfigured verifier */ | |
5876 | verbose(env, "invalid map_ptr to access map->type\n"); | |
5877 | return -EACCES; | |
5878 | } | |
5879 | ||
5880 | switch (meta->map_ptr->map_type) { | |
5881 | case BPF_MAP_TYPE_SOCKMAP: | |
5882 | case BPF_MAP_TYPE_SOCKHASH: | |
5883 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 5884 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
5885 | } else { |
5886 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
5887 | return -EINVAL; | |
5888 | } | |
5889 | break; | |
9330986c JK |
5890 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5891 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
5892 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
5893 | break; | |
912f442c LB |
5894 | default: |
5895 | break; | |
5896 | } | |
5897 | return 0; | |
5898 | } | |
5899 | ||
f79e7ea5 LB |
5900 | struct bpf_reg_types { |
5901 | const enum bpf_reg_type types[10]; | |
1df8f55a | 5902 | u32 *btf_id; |
f79e7ea5 LB |
5903 | }; |
5904 | ||
f79e7ea5 LB |
5905 | static const struct bpf_reg_types sock_types = { |
5906 | .types = { | |
5907 | PTR_TO_SOCK_COMMON, | |
5908 | PTR_TO_SOCKET, | |
5909 | PTR_TO_TCP_SOCK, | |
5910 | PTR_TO_XDP_SOCK, | |
5911 | }, | |
5912 | }; | |
5913 | ||
49a2a4d4 | 5914 | #ifdef CONFIG_NET |
1df8f55a MKL |
5915 | static const struct bpf_reg_types btf_id_sock_common_types = { |
5916 | .types = { | |
5917 | PTR_TO_SOCK_COMMON, | |
5918 | PTR_TO_SOCKET, | |
5919 | PTR_TO_TCP_SOCK, | |
5920 | PTR_TO_XDP_SOCK, | |
5921 | PTR_TO_BTF_ID, | |
3f00c523 | 5922 | PTR_TO_BTF_ID | PTR_TRUSTED, |
1df8f55a MKL |
5923 | }, |
5924 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
5925 | }; | |
49a2a4d4 | 5926 | #endif |
1df8f55a | 5927 | |
f79e7ea5 LB |
5928 | static const struct bpf_reg_types mem_types = { |
5929 | .types = { | |
5930 | PTR_TO_STACK, | |
5931 | PTR_TO_PACKET, | |
5932 | PTR_TO_PACKET_META, | |
69c087ba | 5933 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5934 | PTR_TO_MAP_VALUE, |
5935 | PTR_TO_MEM, | |
894f2a8b | 5936 | PTR_TO_MEM | MEM_RINGBUF, |
20b2aff4 | 5937 | PTR_TO_BUF, |
f79e7ea5 LB |
5938 | }, |
5939 | }; | |
5940 | ||
5941 | static const struct bpf_reg_types int_ptr_types = { | |
5942 | .types = { | |
5943 | PTR_TO_STACK, | |
5944 | PTR_TO_PACKET, | |
5945 | PTR_TO_PACKET_META, | |
69c087ba | 5946 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5947 | PTR_TO_MAP_VALUE, |
5948 | }, | |
5949 | }; | |
5950 | ||
4e814da0 KKD |
5951 | static const struct bpf_reg_types spin_lock_types = { |
5952 | .types = { | |
5953 | PTR_TO_MAP_VALUE, | |
5954 | PTR_TO_BTF_ID | MEM_ALLOC, | |
5955 | } | |
5956 | }; | |
5957 | ||
f79e7ea5 LB |
5958 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
5959 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
5960 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
894f2a8b | 5961 | static const struct bpf_reg_types ringbuf_mem_types = { .types = { PTR_TO_MEM | MEM_RINGBUF } }; |
f79e7ea5 | 5962 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
3f00c523 DV |
5963 | static const struct bpf_reg_types btf_ptr_types = { |
5964 | .types = { | |
5965 | PTR_TO_BTF_ID, | |
5966 | PTR_TO_BTF_ID | PTR_TRUSTED, | |
fca1aa75 | 5967 | PTR_TO_BTF_ID | MEM_RCU, |
3f00c523 DV |
5968 | }, |
5969 | }; | |
5970 | static const struct bpf_reg_types percpu_btf_ptr_types = { | |
5971 | .types = { | |
5972 | PTR_TO_BTF_ID | MEM_PERCPU, | |
5973 | PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED, | |
5974 | } | |
5975 | }; | |
69c087ba YS |
5976 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
5977 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 5978 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 5979 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
c0a5a21c | 5980 | static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
20571567 DV |
5981 | static const struct bpf_reg_types dynptr_types = { |
5982 | .types = { | |
5983 | PTR_TO_STACK, | |
5984 | PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL, | |
5985 | } | |
5986 | }; | |
f79e7ea5 | 5987 | |
0789e13b | 5988 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
d1673304 DM |
5989 | [ARG_PTR_TO_MAP_KEY] = &mem_types, |
5990 | [ARG_PTR_TO_MAP_VALUE] = &mem_types, | |
f79e7ea5 LB |
5991 | [ARG_CONST_SIZE] = &scalar_types, |
5992 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
5993 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
5994 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
5995 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 5996 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 5997 | #ifdef CONFIG_NET |
1df8f55a | 5998 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 5999 | #endif |
f79e7ea5 | 6000 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
6001 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
6002 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
6003 | [ARG_PTR_TO_MEM] = &mem_types, | |
894f2a8b | 6004 | [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, |
f79e7ea5 LB |
6005 | [ARG_PTR_TO_INT] = &int_ptr_types, |
6006 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 6007 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 6008 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 6009 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 6010 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 6011 | [ARG_PTR_TO_TIMER] = &timer_types, |
c0a5a21c | 6012 | [ARG_PTR_TO_KPTR] = &kptr_types, |
20571567 | 6013 | [ARG_PTR_TO_DYNPTR] = &dynptr_types, |
f79e7ea5 LB |
6014 | }; |
6015 | ||
6016 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 | 6017 | enum bpf_arg_type arg_type, |
c0a5a21c KKD |
6018 | const u32 *arg_btf_id, |
6019 | struct bpf_call_arg_meta *meta) | |
f79e7ea5 LB |
6020 | { |
6021 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
6022 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 6023 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
6024 | int i, j; |
6025 | ||
48946bd6 | 6026 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
6027 | if (!compatible) { |
6028 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
6029 | return -EFAULT; | |
6030 | } | |
6031 | ||
216e3cd2 HL |
6032 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
6033 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY | |
6034 | * | |
6035 | * Same for MAYBE_NULL: | |
6036 | * | |
6037 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, | |
6038 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL | |
6039 | * | |
6040 | * Therefore we fold these flags depending on the arg_type before comparison. | |
6041 | */ | |
6042 | if (arg_type & MEM_RDONLY) | |
6043 | type &= ~MEM_RDONLY; | |
6044 | if (arg_type & PTR_MAYBE_NULL) | |
6045 | type &= ~PTR_MAYBE_NULL; | |
6046 | ||
f79e7ea5 LB |
6047 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
6048 | expected = compatible->types[i]; | |
6049 | if (expected == NOT_INIT) | |
6050 | break; | |
6051 | ||
6052 | if (type == expected) | |
a968d5e2 | 6053 | goto found; |
f79e7ea5 LB |
6054 | } |
6055 | ||
216e3cd2 | 6056 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
f79e7ea5 | 6057 | for (j = 0; j + 1 < i; j++) |
c25b2ae1 HL |
6058 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
6059 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); | |
f79e7ea5 | 6060 | return -EACCES; |
a968d5e2 MKL |
6061 | |
6062 | found: | |
3f00c523 | 6063 | if (reg->type == PTR_TO_BTF_ID || reg->type & PTR_TRUSTED) { |
2ab3b380 KKD |
6064 | /* For bpf_sk_release, it needs to match against first member |
6065 | * 'struct sock_common', hence make an exception for it. This | |
6066 | * allows bpf_sk_release to work for multiple socket types. | |
6067 | */ | |
6068 | bool strict_type_match = arg_type_is_release(arg_type) && | |
6069 | meta->func_id != BPF_FUNC_sk_release; | |
6070 | ||
1df8f55a MKL |
6071 | if (!arg_btf_id) { |
6072 | if (!compatible->btf_id) { | |
6073 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
6074 | return -EFAULT; | |
6075 | } | |
6076 | arg_btf_id = compatible->btf_id; | |
6077 | } | |
6078 | ||
c0a5a21c | 6079 | if (meta->func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac | 6080 | if (map_kptr_match_type(env, meta->kptr_field, reg, regno)) |
c0a5a21c | 6081 | return -EACCES; |
47e34cb7 DM |
6082 | } else { |
6083 | if (arg_btf_id == BPF_PTR_POISON) { | |
6084 | verbose(env, "verifier internal error:"); | |
6085 | verbose(env, "R%d has non-overwritten BPF_PTR_POISON type\n", | |
6086 | regno); | |
6087 | return -EACCES; | |
6088 | } | |
6089 | ||
6090 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
6091 | btf_vmlinux, *arg_btf_id, | |
6092 | strict_type_match)) { | |
6093 | verbose(env, "R%d is of type %s but %s is expected\n", | |
6094 | regno, kernel_type_name(reg->btf, reg->btf_id), | |
6095 | kernel_type_name(btf_vmlinux, *arg_btf_id)); | |
6096 | return -EACCES; | |
6097 | } | |
a968d5e2 | 6098 | } |
4e814da0 KKD |
6099 | } else if (type_is_alloc(reg->type)) { |
6100 | if (meta->func_id != BPF_FUNC_spin_lock && meta->func_id != BPF_FUNC_spin_unlock) { | |
6101 | verbose(env, "verifier internal error: unimplemented handling of MEM_ALLOC\n"); | |
6102 | return -EFAULT; | |
6103 | } | |
a968d5e2 MKL |
6104 | } |
6105 | ||
6106 | return 0; | |
f79e7ea5 LB |
6107 | } |
6108 | ||
25b35dd2 KKD |
6109 | int check_func_arg_reg_off(struct bpf_verifier_env *env, |
6110 | const struct bpf_reg_state *reg, int regno, | |
8f14852e | 6111 | enum bpf_arg_type arg_type) |
25b35dd2 KKD |
6112 | { |
6113 | enum bpf_reg_type type = reg->type; | |
8f14852e | 6114 | bool fixed_off_ok = false; |
25b35dd2 KKD |
6115 | |
6116 | switch ((u32)type) { | |
25b35dd2 | 6117 | /* Pointer types where reg offset is explicitly allowed: */ |
97e03f52 JK |
6118 | case PTR_TO_STACK: |
6119 | if (arg_type_is_dynptr(arg_type) && reg->off % BPF_REG_SIZE) { | |
6120 | verbose(env, "cannot pass in dynptr at an offset\n"); | |
6121 | return -EINVAL; | |
6122 | } | |
6123 | fallthrough; | |
25b35dd2 KKD |
6124 | case PTR_TO_PACKET: |
6125 | case PTR_TO_PACKET_META: | |
6126 | case PTR_TO_MAP_KEY: | |
6127 | case PTR_TO_MAP_VALUE: | |
6128 | case PTR_TO_MEM: | |
6129 | case PTR_TO_MEM | MEM_RDONLY: | |
894f2a8b | 6130 | case PTR_TO_MEM | MEM_RINGBUF: |
25b35dd2 KKD |
6131 | case PTR_TO_BUF: |
6132 | case PTR_TO_BUF | MEM_RDONLY: | |
97e03f52 | 6133 | case SCALAR_VALUE: |
25b35dd2 KKD |
6134 | /* Some of the argument types nevertheless require a |
6135 | * zero register offset. | |
6136 | */ | |
894f2a8b | 6137 | if (base_type(arg_type) != ARG_PTR_TO_RINGBUF_MEM) |
25b35dd2 KKD |
6138 | return 0; |
6139 | break; | |
6140 | /* All the rest must be rejected, except PTR_TO_BTF_ID which allows | |
6141 | * fixed offset. | |
6142 | */ | |
6143 | case PTR_TO_BTF_ID: | |
282de143 | 6144 | case PTR_TO_BTF_ID | MEM_ALLOC: |
3f00c523 | 6145 | case PTR_TO_BTF_ID | PTR_TRUSTED: |
fca1aa75 | 6146 | case PTR_TO_BTF_ID | MEM_RCU: |
3f00c523 | 6147 | case PTR_TO_BTF_ID | MEM_ALLOC | PTR_TRUSTED: |
24d5bb80 | 6148 | /* When referenced PTR_TO_BTF_ID is passed to release function, |
8f14852e KKD |
6149 | * it's fixed offset must be 0. In the other cases, fixed offset |
6150 | * can be non-zero. | |
24d5bb80 | 6151 | */ |
8f14852e | 6152 | if (arg_type_is_release(arg_type) && reg->off) { |
24d5bb80 KKD |
6153 | verbose(env, "R%d must have zero offset when passed to release func\n", |
6154 | regno); | |
6155 | return -EINVAL; | |
6156 | } | |
8f14852e KKD |
6157 | /* For arg is release pointer, fixed_off_ok must be false, but |
6158 | * we already checked and rejected reg->off != 0 above, so set | |
6159 | * to true to allow fixed offset for all other cases. | |
24d5bb80 | 6160 | */ |
25b35dd2 KKD |
6161 | fixed_off_ok = true; |
6162 | break; | |
6163 | default: | |
6164 | break; | |
6165 | } | |
6166 | return __check_ptr_off_reg(env, reg, regno, fixed_off_ok); | |
6167 | } | |
6168 | ||
34d4ef57 JK |
6169 | static u32 stack_slot_get_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
6170 | { | |
6171 | struct bpf_func_state *state = func(env, reg); | |
6172 | int spi = get_spi(reg->off); | |
6173 | ||
6174 | return state->stack[spi].spilled_ptr.id; | |
6175 | } | |
6176 | ||
af7ec138 YS |
6177 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
6178 | struct bpf_call_arg_meta *meta, | |
6179 | const struct bpf_func_proto *fn) | |
17a52670 | 6180 | { |
af7ec138 | 6181 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 6182 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 6183 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 6184 | enum bpf_reg_type type = reg->type; |
508362ac | 6185 | u32 *arg_btf_id = NULL; |
17a52670 AS |
6186 | int err = 0; |
6187 | ||
80f1d68c | 6188 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
6189 | return 0; |
6190 | ||
dc503a8a EC |
6191 | err = check_reg_arg(env, regno, SRC_OP); |
6192 | if (err) | |
6193 | return err; | |
17a52670 | 6194 | |
1be7f75d AS |
6195 | if (arg_type == ARG_ANYTHING) { |
6196 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
6197 | verbose(env, "R%d leaks addr into helper function\n", |
6198 | regno); | |
1be7f75d AS |
6199 | return -EACCES; |
6200 | } | |
80f1d68c | 6201 | return 0; |
1be7f75d | 6202 | } |
80f1d68c | 6203 | |
de8f3a83 | 6204 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 6205 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 6206 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
6207 | return -EACCES; |
6208 | } | |
6209 | ||
16d1e00c | 6210 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE) { |
912f442c LB |
6211 | err = resolve_map_arg_type(env, meta, &arg_type); |
6212 | if (err) | |
6213 | return err; | |
6214 | } | |
6215 | ||
48946bd6 | 6216 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
6217 | /* A NULL register has a SCALAR_VALUE type, so skip |
6218 | * type checking. | |
6219 | */ | |
6220 | goto skip_type_check; | |
6221 | ||
508362ac | 6222 | /* arg_btf_id and arg_size are in a union. */ |
4e814da0 KKD |
6223 | if (base_type(arg_type) == ARG_PTR_TO_BTF_ID || |
6224 | base_type(arg_type) == ARG_PTR_TO_SPIN_LOCK) | |
508362ac MM |
6225 | arg_btf_id = fn->arg_btf_id[arg]; |
6226 | ||
6227 | err = check_reg_type(env, regno, arg_type, arg_btf_id, meta); | |
f79e7ea5 LB |
6228 | if (err) |
6229 | return err; | |
6230 | ||
8f14852e | 6231 | err = check_func_arg_reg_off(env, reg, regno, arg_type); |
25b35dd2 KKD |
6232 | if (err) |
6233 | return err; | |
d7b9454a | 6234 | |
fd1b0d60 | 6235 | skip_type_check: |
8f14852e | 6236 | if (arg_type_is_release(arg_type)) { |
bc34dee6 JK |
6237 | if (arg_type_is_dynptr(arg_type)) { |
6238 | struct bpf_func_state *state = func(env, reg); | |
6239 | int spi = get_spi(reg->off); | |
6240 | ||
6241 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS) || | |
6242 | !state->stack[spi].spilled_ptr.id) { | |
6243 | verbose(env, "arg %d is an unacquired reference\n", regno); | |
6244 | return -EINVAL; | |
6245 | } | |
6246 | } else if (!reg->ref_obj_id && !register_is_null(reg)) { | |
8f14852e KKD |
6247 | verbose(env, "R%d must be referenced when passed to release function\n", |
6248 | regno); | |
6249 | return -EINVAL; | |
6250 | } | |
6251 | if (meta->release_regno) { | |
6252 | verbose(env, "verifier internal error: more than one release argument\n"); | |
6253 | return -EFAULT; | |
6254 | } | |
6255 | meta->release_regno = regno; | |
6256 | } | |
6257 | ||
02f7c958 | 6258 | if (reg->ref_obj_id) { |
457f4436 AN |
6259 | if (meta->ref_obj_id) { |
6260 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
6261 | regno, reg->ref_obj_id, | |
6262 | meta->ref_obj_id); | |
6263 | return -EFAULT; | |
6264 | } | |
6265 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
6266 | } |
6267 | ||
8ab4cdcf JK |
6268 | switch (base_type(arg_type)) { |
6269 | case ARG_CONST_MAP_PTR: | |
17a52670 | 6270 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
3e8ce298 AS |
6271 | if (meta->map_ptr) { |
6272 | /* Use map_uid (which is unique id of inner map) to reject: | |
6273 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
6274 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
6275 | * if (inner_map1 && inner_map2) { | |
6276 | * timer = bpf_map_lookup_elem(inner_map1); | |
6277 | * if (timer) | |
6278 | * // mismatch would have been allowed | |
6279 | * bpf_timer_init(timer, inner_map2); | |
6280 | * } | |
6281 | * | |
6282 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
6283 | */ | |
6284 | if (meta->map_ptr != reg->map_ptr || | |
6285 | meta->map_uid != reg->map_uid) { | |
6286 | verbose(env, | |
6287 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
6288 | meta->map_uid, reg->map_uid); | |
6289 | return -EINVAL; | |
6290 | } | |
b00628b1 | 6291 | } |
33ff9823 | 6292 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 6293 | meta->map_uid = reg->map_uid; |
8ab4cdcf JK |
6294 | break; |
6295 | case ARG_PTR_TO_MAP_KEY: | |
17a52670 AS |
6296 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
6297 | * check that [key, key + map->key_size) are within | |
6298 | * stack limits and initialized | |
6299 | */ | |
33ff9823 | 6300 | if (!meta->map_ptr) { |
17a52670 AS |
6301 | /* in function declaration map_ptr must come before |
6302 | * map_key, so that it's verified and known before | |
6303 | * we have to check map_key here. Otherwise it means | |
6304 | * that kernel subsystem misconfigured verifier | |
6305 | */ | |
61bd5218 | 6306 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
6307 | return -EACCES; |
6308 | } | |
d71962f3 PC |
6309 | err = check_helper_mem_access(env, regno, |
6310 | meta->map_ptr->key_size, false, | |
6311 | NULL); | |
8ab4cdcf JK |
6312 | break; |
6313 | case ARG_PTR_TO_MAP_VALUE: | |
48946bd6 HL |
6314 | if (type_may_be_null(arg_type) && register_is_null(reg)) |
6315 | return 0; | |
6316 | ||
17a52670 AS |
6317 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
6318 | * check [value, value + map->value_size) validity | |
6319 | */ | |
33ff9823 | 6320 | if (!meta->map_ptr) { |
17a52670 | 6321 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 6322 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
6323 | return -EACCES; |
6324 | } | |
16d1e00c | 6325 | meta->raw_mode = arg_type & MEM_UNINIT; |
d71962f3 PC |
6326 | err = check_helper_mem_access(env, regno, |
6327 | meta->map_ptr->value_size, false, | |
2ea864c5 | 6328 | meta); |
8ab4cdcf JK |
6329 | break; |
6330 | case ARG_PTR_TO_PERCPU_BTF_ID: | |
eaa6bcb7 HL |
6331 | if (!reg->btf_id) { |
6332 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
6333 | return -EACCES; | |
6334 | } | |
22dc4a0f | 6335 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 6336 | meta->ret_btf_id = reg->btf_id; |
8ab4cdcf JK |
6337 | break; |
6338 | case ARG_PTR_TO_SPIN_LOCK: | |
c18f0b6a LB |
6339 | if (meta->func_id == BPF_FUNC_spin_lock) { |
6340 | if (process_spin_lock(env, regno, true)) | |
6341 | return -EACCES; | |
6342 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
6343 | if (process_spin_lock(env, regno, false)) | |
6344 | return -EACCES; | |
6345 | } else { | |
6346 | verbose(env, "verifier internal error\n"); | |
6347 | return -EFAULT; | |
6348 | } | |
8ab4cdcf JK |
6349 | break; |
6350 | case ARG_PTR_TO_TIMER: | |
b00628b1 AS |
6351 | if (process_timer_func(env, regno, meta)) |
6352 | return -EACCES; | |
8ab4cdcf JK |
6353 | break; |
6354 | case ARG_PTR_TO_FUNC: | |
69c087ba | 6355 | meta->subprogno = reg->subprogno; |
8ab4cdcf JK |
6356 | break; |
6357 | case ARG_PTR_TO_MEM: | |
a2bbe7cc LB |
6358 | /* The access to this pointer is only checked when we hit the |
6359 | * next is_mem_size argument below. | |
6360 | */ | |
16d1e00c | 6361 | meta->raw_mode = arg_type & MEM_UNINIT; |
508362ac MM |
6362 | if (arg_type & MEM_FIXED_SIZE) { |
6363 | err = check_helper_mem_access(env, regno, | |
6364 | fn->arg_size[arg], false, | |
6365 | meta); | |
6366 | } | |
8ab4cdcf JK |
6367 | break; |
6368 | case ARG_CONST_SIZE: | |
6369 | err = check_mem_size_reg(env, reg, regno, false, meta); | |
6370 | break; | |
6371 | case ARG_CONST_SIZE_OR_ZERO: | |
6372 | err = check_mem_size_reg(env, reg, regno, true, meta); | |
6373 | break; | |
6374 | case ARG_PTR_TO_DYNPTR: | |
20571567 DV |
6375 | /* We only need to check for initialized / uninitialized helper |
6376 | * dynptr args if the dynptr is not PTR_TO_DYNPTR, as the | |
6377 | * assumption is that if it is, that a helper function | |
6378 | * initialized the dynptr on behalf of the BPF program. | |
6379 | */ | |
6380 | if (base_type(reg->type) == PTR_TO_DYNPTR) | |
6381 | break; | |
97e03f52 JK |
6382 | if (arg_type & MEM_UNINIT) { |
6383 | if (!is_dynptr_reg_valid_uninit(env, reg)) { | |
6384 | verbose(env, "Dynptr has to be an uninitialized dynptr\n"); | |
6385 | return -EINVAL; | |
6386 | } | |
6387 | ||
6388 | /* We only support one dynptr being uninitialized at the moment, | |
6389 | * which is sufficient for the helper functions we have right now. | |
6390 | */ | |
6391 | if (meta->uninit_dynptr_regno) { | |
6392 | verbose(env, "verifier internal error: multiple uninitialized dynptr args\n"); | |
6393 | return -EFAULT; | |
6394 | } | |
6395 | ||
6396 | meta->uninit_dynptr_regno = regno; | |
e9e315b4 RS |
6397 | } else if (!is_dynptr_reg_valid_init(env, reg)) { |
6398 | verbose(env, | |
6399 | "Expected an initialized dynptr as arg #%d\n", | |
6400 | arg + 1); | |
6401 | return -EINVAL; | |
6402 | } else if (!is_dynptr_type_expected(env, reg, arg_type)) { | |
97e03f52 JK |
6403 | const char *err_extra = ""; |
6404 | ||
6405 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
6406 | case DYNPTR_TYPE_LOCAL: | |
e9e315b4 | 6407 | err_extra = "local"; |
97e03f52 | 6408 | break; |
bc34dee6 | 6409 | case DYNPTR_TYPE_RINGBUF: |
e9e315b4 | 6410 | err_extra = "ringbuf"; |
bc34dee6 | 6411 | break; |
97e03f52 | 6412 | default: |
e9e315b4 | 6413 | err_extra = "<unknown>"; |
97e03f52 JK |
6414 | break; |
6415 | } | |
e9e315b4 RS |
6416 | verbose(env, |
6417 | "Expected a dynptr of type %s as arg #%d\n", | |
97e03f52 JK |
6418 | err_extra, arg + 1); |
6419 | return -EINVAL; | |
6420 | } | |
8ab4cdcf JK |
6421 | break; |
6422 | case ARG_CONST_ALLOC_SIZE_OR_ZERO: | |
457f4436 | 6423 | if (!tnum_is_const(reg->var_off)) { |
28a8add6 | 6424 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
6425 | regno); |
6426 | return -EACCES; | |
6427 | } | |
6428 | meta->mem_size = reg->var_off.value; | |
2fc31465 KKD |
6429 | err = mark_chain_precision(env, regno); |
6430 | if (err) | |
6431 | return err; | |
8ab4cdcf JK |
6432 | break; |
6433 | case ARG_PTR_TO_INT: | |
6434 | case ARG_PTR_TO_LONG: | |
6435 | { | |
57c3bb72 AI |
6436 | int size = int_ptr_type_to_size(arg_type); |
6437 | ||
6438 | err = check_helper_mem_access(env, regno, size, false, meta); | |
6439 | if (err) | |
6440 | return err; | |
6441 | err = check_ptr_alignment(env, reg, 0, size, true); | |
8ab4cdcf JK |
6442 | break; |
6443 | } | |
6444 | case ARG_PTR_TO_CONST_STR: | |
6445 | { | |
fff13c4b FR |
6446 | struct bpf_map *map = reg->map_ptr; |
6447 | int map_off; | |
6448 | u64 map_addr; | |
6449 | char *str_ptr; | |
6450 | ||
a8fad73e | 6451 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
6452 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
6453 | return -EACCES; | |
6454 | } | |
6455 | ||
6456 | if (!tnum_is_const(reg->var_off)) { | |
6457 | verbose(env, "R%d is not a constant address'\n", regno); | |
6458 | return -EACCES; | |
6459 | } | |
6460 | ||
6461 | if (!map->ops->map_direct_value_addr) { | |
6462 | verbose(env, "no direct value access support for this map type\n"); | |
6463 | return -EACCES; | |
6464 | } | |
6465 | ||
6466 | err = check_map_access(env, regno, reg->off, | |
61df10c7 KKD |
6467 | map->value_size - reg->off, false, |
6468 | ACCESS_HELPER); | |
fff13c4b FR |
6469 | if (err) |
6470 | return err; | |
6471 | ||
6472 | map_off = reg->off + reg->var_off.value; | |
6473 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
6474 | if (err) { | |
6475 | verbose(env, "direct value access on string failed\n"); | |
6476 | return err; | |
6477 | } | |
6478 | ||
6479 | str_ptr = (char *)(long)(map_addr); | |
6480 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
6481 | verbose(env, "string is not zero-terminated\n"); | |
6482 | return -EINVAL; | |
6483 | } | |
8ab4cdcf JK |
6484 | break; |
6485 | } | |
6486 | case ARG_PTR_TO_KPTR: | |
c0a5a21c KKD |
6487 | if (process_kptr_func(env, regno, meta)) |
6488 | return -EACCES; | |
8ab4cdcf | 6489 | break; |
17a52670 AS |
6490 | } |
6491 | ||
6492 | return err; | |
6493 | } | |
6494 | ||
0126240f LB |
6495 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
6496 | { | |
6497 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 6498 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
6499 | |
6500 | if (func_id != BPF_FUNC_map_update_elem) | |
6501 | return false; | |
6502 | ||
6503 | /* It's not possible to get access to a locked struct sock in these | |
6504 | * contexts, so updating is safe. | |
6505 | */ | |
6506 | switch (type) { | |
6507 | case BPF_PROG_TYPE_TRACING: | |
6508 | if (eatype == BPF_TRACE_ITER) | |
6509 | return true; | |
6510 | break; | |
6511 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
6512 | case BPF_PROG_TYPE_SCHED_CLS: | |
6513 | case BPF_PROG_TYPE_SCHED_ACT: | |
6514 | case BPF_PROG_TYPE_XDP: | |
6515 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
6516 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
6517 | case BPF_PROG_TYPE_SK_LOOKUP: | |
6518 | return true; | |
6519 | default: | |
6520 | break; | |
6521 | } | |
6522 | ||
6523 | verbose(env, "cannot update sockmap in this context\n"); | |
6524 | return false; | |
6525 | } | |
6526 | ||
e411901c MF |
6527 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
6528 | { | |
95acd881 TA |
6529 | return env->prog->jit_requested && |
6530 | bpf_jit_supports_subprog_tailcalls(); | |
e411901c MF |
6531 | } |
6532 | ||
61bd5218 JK |
6533 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
6534 | struct bpf_map *map, int func_id) | |
35578d79 | 6535 | { |
35578d79 KX |
6536 | if (!map) |
6537 | return 0; | |
6538 | ||
6aff67c8 AS |
6539 | /* We need a two way check, first is from map perspective ... */ |
6540 | switch (map->map_type) { | |
6541 | case BPF_MAP_TYPE_PROG_ARRAY: | |
6542 | if (func_id != BPF_FUNC_tail_call) | |
6543 | goto error; | |
6544 | break; | |
6545 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
6546 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 6547 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 6548 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
6549 | func_id != BPF_FUNC_perf_event_read_value && |
6550 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
6551 | goto error; |
6552 | break; | |
457f4436 AN |
6553 | case BPF_MAP_TYPE_RINGBUF: |
6554 | if (func_id != BPF_FUNC_ringbuf_output && | |
6555 | func_id != BPF_FUNC_ringbuf_reserve && | |
bc34dee6 JK |
6556 | func_id != BPF_FUNC_ringbuf_query && |
6557 | func_id != BPF_FUNC_ringbuf_reserve_dynptr && | |
6558 | func_id != BPF_FUNC_ringbuf_submit_dynptr && | |
6559 | func_id != BPF_FUNC_ringbuf_discard_dynptr) | |
457f4436 AN |
6560 | goto error; |
6561 | break; | |
583c1f42 | 6562 | case BPF_MAP_TYPE_USER_RINGBUF: |
20571567 DV |
6563 | if (func_id != BPF_FUNC_user_ringbuf_drain) |
6564 | goto error; | |
6565 | break; | |
6aff67c8 AS |
6566 | case BPF_MAP_TYPE_STACK_TRACE: |
6567 | if (func_id != BPF_FUNC_get_stackid) | |
6568 | goto error; | |
6569 | break; | |
4ed8ec52 | 6570 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 6571 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 6572 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
6573 | goto error; |
6574 | break; | |
cd339431 | 6575 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 6576 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
6577 | if (func_id != BPF_FUNC_get_local_storage) |
6578 | goto error; | |
6579 | break; | |
546ac1ff | 6580 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 6581 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
6582 | if (func_id != BPF_FUNC_redirect_map && |
6583 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
6584 | goto error; |
6585 | break; | |
fbfc504a BT |
6586 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
6587 | * appear. | |
6588 | */ | |
6710e112 JDB |
6589 | case BPF_MAP_TYPE_CPUMAP: |
6590 | if (func_id != BPF_FUNC_redirect_map) | |
6591 | goto error; | |
6592 | break; | |
fada7fdc JL |
6593 | case BPF_MAP_TYPE_XSKMAP: |
6594 | if (func_id != BPF_FUNC_redirect_map && | |
6595 | func_id != BPF_FUNC_map_lookup_elem) | |
6596 | goto error; | |
6597 | break; | |
56f668df | 6598 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 6599 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
6600 | if (func_id != BPF_FUNC_map_lookup_elem) |
6601 | goto error; | |
16a43625 | 6602 | break; |
174a79ff JF |
6603 | case BPF_MAP_TYPE_SOCKMAP: |
6604 | if (func_id != BPF_FUNC_sk_redirect_map && | |
6605 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 6606 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 6607 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 6608 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
6609 | func_id != BPF_FUNC_map_lookup_elem && |
6610 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
6611 | goto error; |
6612 | break; | |
81110384 JF |
6613 | case BPF_MAP_TYPE_SOCKHASH: |
6614 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
6615 | func_id != BPF_FUNC_sock_hash_update && | |
6616 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 6617 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 6618 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
6619 | func_id != BPF_FUNC_map_lookup_elem && |
6620 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
6621 | goto error; |
6622 | break; | |
2dbb9b9e MKL |
6623 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
6624 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
6625 | goto error; | |
6626 | break; | |
f1a2e44a MV |
6627 | case BPF_MAP_TYPE_QUEUE: |
6628 | case BPF_MAP_TYPE_STACK: | |
6629 | if (func_id != BPF_FUNC_map_peek_elem && | |
6630 | func_id != BPF_FUNC_map_pop_elem && | |
6631 | func_id != BPF_FUNC_map_push_elem) | |
6632 | goto error; | |
6633 | break; | |
6ac99e8f MKL |
6634 | case BPF_MAP_TYPE_SK_STORAGE: |
6635 | if (func_id != BPF_FUNC_sk_storage_get && | |
6636 | func_id != BPF_FUNC_sk_storage_delete) | |
6637 | goto error; | |
6638 | break; | |
8ea63684 KS |
6639 | case BPF_MAP_TYPE_INODE_STORAGE: |
6640 | if (func_id != BPF_FUNC_inode_storage_get && | |
6641 | func_id != BPF_FUNC_inode_storage_delete) | |
6642 | goto error; | |
6643 | break; | |
4cf1bc1f KS |
6644 | case BPF_MAP_TYPE_TASK_STORAGE: |
6645 | if (func_id != BPF_FUNC_task_storage_get && | |
6646 | func_id != BPF_FUNC_task_storage_delete) | |
6647 | goto error; | |
6648 | break; | |
c4bcfb38 YS |
6649 | case BPF_MAP_TYPE_CGRP_STORAGE: |
6650 | if (func_id != BPF_FUNC_cgrp_storage_get && | |
6651 | func_id != BPF_FUNC_cgrp_storage_delete) | |
6652 | goto error; | |
6653 | break; | |
9330986c JK |
6654 | case BPF_MAP_TYPE_BLOOM_FILTER: |
6655 | if (func_id != BPF_FUNC_map_peek_elem && | |
6656 | func_id != BPF_FUNC_map_push_elem) | |
6657 | goto error; | |
6658 | break; | |
6aff67c8 AS |
6659 | default: |
6660 | break; | |
6661 | } | |
6662 | ||
6663 | /* ... and second from the function itself. */ | |
6664 | switch (func_id) { | |
6665 | case BPF_FUNC_tail_call: | |
6666 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
6667 | goto error; | |
e411901c MF |
6668 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
6669 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
6670 | return -EINVAL; |
6671 | } | |
6aff67c8 AS |
6672 | break; |
6673 | case BPF_FUNC_perf_event_read: | |
6674 | case BPF_FUNC_perf_event_output: | |
908432ca | 6675 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 6676 | case BPF_FUNC_skb_output: |
d831ee84 | 6677 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
6678 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
6679 | goto error; | |
6680 | break; | |
5b029a32 DB |
6681 | case BPF_FUNC_ringbuf_output: |
6682 | case BPF_FUNC_ringbuf_reserve: | |
6683 | case BPF_FUNC_ringbuf_query: | |
bc34dee6 JK |
6684 | case BPF_FUNC_ringbuf_reserve_dynptr: |
6685 | case BPF_FUNC_ringbuf_submit_dynptr: | |
6686 | case BPF_FUNC_ringbuf_discard_dynptr: | |
5b029a32 DB |
6687 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
6688 | goto error; | |
6689 | break; | |
20571567 DV |
6690 | case BPF_FUNC_user_ringbuf_drain: |
6691 | if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF) | |
6692 | goto error; | |
6693 | break; | |
6aff67c8 AS |
6694 | case BPF_FUNC_get_stackid: |
6695 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
6696 | goto error; | |
6697 | break; | |
60d20f91 | 6698 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 6699 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
6700 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
6701 | goto error; | |
6702 | break; | |
97f91a7c | 6703 | case BPF_FUNC_redirect_map: |
9c270af3 | 6704 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 6705 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
6706 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
6707 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
6708 | goto error; |
6709 | break; | |
174a79ff | 6710 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 6711 | case BPF_FUNC_msg_redirect_map: |
81110384 | 6712 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
6713 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
6714 | goto error; | |
6715 | break; | |
81110384 JF |
6716 | case BPF_FUNC_sk_redirect_hash: |
6717 | case BPF_FUNC_msg_redirect_hash: | |
6718 | case BPF_FUNC_sock_hash_update: | |
6719 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
6720 | goto error; |
6721 | break; | |
cd339431 | 6722 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
6723 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
6724 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
6725 | goto error; |
6726 | break; | |
2dbb9b9e | 6727 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
6728 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
6729 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
6730 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
6731 | goto error; |
6732 | break; | |
f1a2e44a | 6733 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
6734 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
6735 | map->map_type != BPF_MAP_TYPE_STACK) | |
6736 | goto error; | |
6737 | break; | |
9330986c JK |
6738 | case BPF_FUNC_map_peek_elem: |
6739 | case BPF_FUNC_map_push_elem: | |
6740 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
6741 | map->map_type != BPF_MAP_TYPE_STACK && | |
6742 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
6743 | goto error; | |
6744 | break; | |
07343110 FZ |
6745 | case BPF_FUNC_map_lookup_percpu_elem: |
6746 | if (map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && | |
6747 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && | |
6748 | map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH) | |
6749 | goto error; | |
6750 | break; | |
6ac99e8f MKL |
6751 | case BPF_FUNC_sk_storage_get: |
6752 | case BPF_FUNC_sk_storage_delete: | |
6753 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
6754 | goto error; | |
6755 | break; | |
8ea63684 KS |
6756 | case BPF_FUNC_inode_storage_get: |
6757 | case BPF_FUNC_inode_storage_delete: | |
6758 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
6759 | goto error; | |
6760 | break; | |
4cf1bc1f KS |
6761 | case BPF_FUNC_task_storage_get: |
6762 | case BPF_FUNC_task_storage_delete: | |
6763 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
6764 | goto error; | |
6765 | break; | |
c4bcfb38 YS |
6766 | case BPF_FUNC_cgrp_storage_get: |
6767 | case BPF_FUNC_cgrp_storage_delete: | |
6768 | if (map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) | |
6769 | goto error; | |
6770 | break; | |
6aff67c8 AS |
6771 | default: |
6772 | break; | |
35578d79 KX |
6773 | } |
6774 | ||
6775 | return 0; | |
6aff67c8 | 6776 | error: |
61bd5218 | 6777 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 6778 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 6779 | return -EINVAL; |
35578d79 KX |
6780 | } |
6781 | ||
90133415 | 6782 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
6783 | { |
6784 | int count = 0; | |
6785 | ||
39f19ebb | 6786 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6787 | count++; |
39f19ebb | 6788 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6789 | count++; |
39f19ebb | 6790 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6791 | count++; |
39f19ebb | 6792 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6793 | count++; |
39f19ebb | 6794 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
6795 | count++; |
6796 | ||
90133415 DB |
6797 | /* We only support one arg being in raw mode at the moment, |
6798 | * which is sufficient for the helper functions we have | |
6799 | * right now. | |
6800 | */ | |
6801 | return count <= 1; | |
6802 | } | |
6803 | ||
508362ac | 6804 | static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg) |
90133415 | 6805 | { |
508362ac MM |
6806 | bool is_fixed = fn->arg_type[arg] & MEM_FIXED_SIZE; |
6807 | bool has_size = fn->arg_size[arg] != 0; | |
6808 | bool is_next_size = false; | |
6809 | ||
6810 | if (arg + 1 < ARRAY_SIZE(fn->arg_type)) | |
6811 | is_next_size = arg_type_is_mem_size(fn->arg_type[arg + 1]); | |
6812 | ||
6813 | if (base_type(fn->arg_type[arg]) != ARG_PTR_TO_MEM) | |
6814 | return is_next_size; | |
6815 | ||
6816 | return has_size == is_next_size || is_next_size == is_fixed; | |
90133415 DB |
6817 | } |
6818 | ||
6819 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
6820 | { | |
6821 | /* bpf_xxx(..., buf, len) call will access 'len' | |
6822 | * bytes from memory 'buf'. Both arg types need | |
6823 | * to be paired, so make sure there's no buggy | |
6824 | * helper function specification. | |
6825 | */ | |
6826 | if (arg_type_is_mem_size(fn->arg1_type) || | |
508362ac MM |
6827 | check_args_pair_invalid(fn, 0) || |
6828 | check_args_pair_invalid(fn, 1) || | |
6829 | check_args_pair_invalid(fn, 2) || | |
6830 | check_args_pair_invalid(fn, 3) || | |
6831 | check_args_pair_invalid(fn, 4)) | |
90133415 DB |
6832 | return false; |
6833 | ||
6834 | return true; | |
6835 | } | |
6836 | ||
9436ef6e LB |
6837 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
6838 | { | |
6839 | int i; | |
6840 | ||
1df8f55a | 6841 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
4e814da0 KKD |
6842 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_BTF_ID) |
6843 | return !!fn->arg_btf_id[i]; | |
6844 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_SPIN_LOCK) | |
6845 | return fn->arg_btf_id[i] == BPF_PTR_POISON; | |
508362ac MM |
6846 | if (base_type(fn->arg_type[i]) != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i] && |
6847 | /* arg_btf_id and arg_size are in a union. */ | |
6848 | (base_type(fn->arg_type[i]) != ARG_PTR_TO_MEM || | |
6849 | !(fn->arg_type[i] & MEM_FIXED_SIZE))) | |
1df8f55a MKL |
6850 | return false; |
6851 | } | |
6852 | ||
9436ef6e LB |
6853 | return true; |
6854 | } | |
6855 | ||
0c9a7a7e | 6856 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
6857 | { |
6858 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 6859 | check_arg_pair_ok(fn) && |
b2d8ef19 | 6860 | check_btf_id_ok(fn) ? 0 : -EINVAL; |
435faee1 DB |
6861 | } |
6862 | ||
de8f3a83 DB |
6863 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
6864 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 6865 | */ |
b239da34 | 6866 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 6867 | { |
b239da34 KKD |
6868 | struct bpf_func_state *state; |
6869 | struct bpf_reg_state *reg; | |
969bf05e | 6870 | |
b239da34 | 6871 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
de8f3a83 | 6872 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 6873 | __mark_reg_unknown(env, reg); |
b239da34 | 6874 | })); |
f4d7e40a AS |
6875 | } |
6876 | ||
6d94e741 AS |
6877 | enum { |
6878 | AT_PKT_END = -1, | |
6879 | BEYOND_PKT_END = -2, | |
6880 | }; | |
6881 | ||
6882 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
6883 | { | |
6884 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
6885 | struct bpf_reg_state *reg = &state->regs[regn]; | |
6886 | ||
6887 | if (reg->type != PTR_TO_PACKET) | |
6888 | /* PTR_TO_PACKET_META is not supported yet */ | |
6889 | return; | |
6890 | ||
6891 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
6892 | * How far beyond pkt_end it goes is unknown. | |
6893 | * if (!range_open) it's the case of pkt >= pkt_end | |
6894 | * if (range_open) it's the case of pkt > pkt_end | |
6895 | * hence this pointer is at least 1 byte bigger than pkt_end | |
6896 | */ | |
6897 | if (range_open) | |
6898 | reg->range = BEYOND_PKT_END; | |
6899 | else | |
6900 | reg->range = AT_PKT_END; | |
6901 | } | |
6902 | ||
fd978bf7 JS |
6903 | /* The pointer with the specified id has released its reference to kernel |
6904 | * resources. Identify all copies of the same pointer and clear the reference. | |
6905 | */ | |
6906 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 6907 | int ref_obj_id) |
fd978bf7 | 6908 | { |
b239da34 KKD |
6909 | struct bpf_func_state *state; |
6910 | struct bpf_reg_state *reg; | |
1b986589 | 6911 | int err; |
fd978bf7 | 6912 | |
1b986589 MKL |
6913 | err = release_reference_state(cur_func(env), ref_obj_id); |
6914 | if (err) | |
6915 | return err; | |
6916 | ||
b239da34 | 6917 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
f1db2081 YL |
6918 | if (reg->ref_obj_id == ref_obj_id) { |
6919 | if (!env->allow_ptr_leaks) | |
6920 | __mark_reg_not_init(env, reg); | |
6921 | else | |
6922 | __mark_reg_unknown(env, reg); | |
6923 | } | |
b239da34 | 6924 | })); |
fd978bf7 | 6925 | |
1b986589 | 6926 | return 0; |
fd978bf7 JS |
6927 | } |
6928 | ||
51c39bb1 AS |
6929 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
6930 | struct bpf_reg_state *regs) | |
6931 | { | |
6932 | int i; | |
6933 | ||
6934 | /* after the call registers r0 - r5 were scratched */ | |
6935 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
6936 | mark_reg_not_init(env, regs, caller_saved[i]); | |
6937 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
6938 | } | |
6939 | } | |
6940 | ||
14351375 YS |
6941 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
6942 | struct bpf_func_state *caller, | |
6943 | struct bpf_func_state *callee, | |
6944 | int insn_idx); | |
6945 | ||
be2ef816 AN |
6946 | static int set_callee_state(struct bpf_verifier_env *env, |
6947 | struct bpf_func_state *caller, | |
6948 | struct bpf_func_state *callee, int insn_idx); | |
6949 | ||
14351375 YS |
6950 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
6951 | int *insn_idx, int subprog, | |
6952 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
6953 | { |
6954 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 6955 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 6956 | struct bpf_func_state *caller, *callee; |
14351375 | 6957 | int err; |
51c39bb1 | 6958 | bool is_global = false; |
f4d7e40a | 6959 | |
aada9ce6 | 6960 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 6961 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 6962 | state->curframe + 2); |
f4d7e40a AS |
6963 | return -E2BIG; |
6964 | } | |
6965 | ||
f4d7e40a AS |
6966 | caller = state->frame[state->curframe]; |
6967 | if (state->frame[state->curframe + 1]) { | |
6968 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
6969 | state->curframe + 1); | |
6970 | return -EFAULT; | |
6971 | } | |
6972 | ||
51c39bb1 AS |
6973 | func_info_aux = env->prog->aux->func_info_aux; |
6974 | if (func_info_aux) | |
6975 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
95f2f26f | 6976 | err = btf_check_subprog_call(env, subprog, caller->regs); |
51c39bb1 AS |
6977 | if (err == -EFAULT) |
6978 | return err; | |
6979 | if (is_global) { | |
6980 | if (err) { | |
6981 | verbose(env, "Caller passes invalid args into func#%d\n", | |
6982 | subprog); | |
6983 | return err; | |
6984 | } else { | |
6985 | if (env->log.level & BPF_LOG_LEVEL) | |
6986 | verbose(env, | |
6987 | "Func#%d is global and valid. Skipping.\n", | |
6988 | subprog); | |
6989 | clear_caller_saved_regs(env, caller->regs); | |
6990 | ||
45159b27 | 6991 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 6992 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 6993 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
6994 | |
6995 | /* continue with next insn after call */ | |
6996 | return 0; | |
6997 | } | |
6998 | } | |
6999 | ||
be2ef816 AN |
7000 | /* set_callee_state is used for direct subprog calls, but we are |
7001 | * interested in validating only BPF helpers that can call subprogs as | |
7002 | * callbacks | |
7003 | */ | |
7004 | if (set_callee_state_cb != set_callee_state && !is_callback_calling_function(insn->imm)) { | |
7005 | verbose(env, "verifier bug: helper %s#%d is not marked as callback-calling\n", | |
7006 | func_id_name(insn->imm), insn->imm); | |
7007 | return -EFAULT; | |
7008 | } | |
7009 | ||
bfc6bb74 | 7010 | if (insn->code == (BPF_JMP | BPF_CALL) && |
a5bebc4f | 7011 | insn->src_reg == 0 && |
bfc6bb74 AS |
7012 | insn->imm == BPF_FUNC_timer_set_callback) { |
7013 | struct bpf_verifier_state *async_cb; | |
7014 | ||
7015 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 7016 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
7017 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
7018 | *insn_idx, subprog); | |
7019 | if (!async_cb) | |
7020 | return -EFAULT; | |
7021 | callee = async_cb->frame[0]; | |
7022 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
7023 | ||
7024 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
7025 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
7026 | if (err) | |
7027 | return err; | |
7028 | ||
7029 | clear_caller_saved_regs(env, caller->regs); | |
7030 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
7031 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
7032 | /* continue with next insn after call */ | |
7033 | return 0; | |
7034 | } | |
7035 | ||
f4d7e40a AS |
7036 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
7037 | if (!callee) | |
7038 | return -ENOMEM; | |
7039 | state->frame[state->curframe + 1] = callee; | |
7040 | ||
7041 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
7042 | * into its own stack before reading from it. | |
7043 | * callee can read/write into caller's stack | |
7044 | */ | |
7045 | init_func_state(env, callee, | |
7046 | /* remember the callsite, it will be used by bpf_exit */ | |
7047 | *insn_idx /* callsite */, | |
7048 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 7049 | subprog /* subprog number within this prog */); |
f4d7e40a | 7050 | |
fd978bf7 | 7051 | /* Transfer references to the callee */ |
c69431aa | 7052 | err = copy_reference_state(callee, caller); |
fd978bf7 | 7053 | if (err) |
eb86559a | 7054 | goto err_out; |
fd978bf7 | 7055 | |
14351375 YS |
7056 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
7057 | if (err) | |
eb86559a | 7058 | goto err_out; |
f4d7e40a | 7059 | |
51c39bb1 | 7060 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
7061 | |
7062 | /* only increment it after check_reg_arg() finished */ | |
7063 | state->curframe++; | |
7064 | ||
7065 | /* and go analyze first insn of the callee */ | |
14351375 | 7066 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 7067 | |
06ee7115 | 7068 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 7069 | verbose(env, "caller:\n"); |
0f55f9ed | 7070 | print_verifier_state(env, caller, true); |
f4d7e40a | 7071 | verbose(env, "callee:\n"); |
0f55f9ed | 7072 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
7073 | } |
7074 | return 0; | |
eb86559a WY |
7075 | |
7076 | err_out: | |
7077 | free_func_state(callee); | |
7078 | state->frame[state->curframe + 1] = NULL; | |
7079 | return err; | |
f4d7e40a AS |
7080 | } |
7081 | ||
314ee05e YS |
7082 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
7083 | struct bpf_func_state *caller, | |
7084 | struct bpf_func_state *callee) | |
7085 | { | |
7086 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
7087 | * void *callback_ctx, u64 flags); | |
7088 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
7089 | * void *callback_ctx); | |
7090 | */ | |
7091 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
7092 | ||
7093 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
7094 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
7095 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
7096 | ||
7097 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
7098 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
7099 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
7100 | ||
7101 | /* pointer to stack or null */ | |
7102 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
7103 | ||
7104 | /* unused */ | |
7105 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
7106 | return 0; | |
7107 | } | |
7108 | ||
14351375 YS |
7109 | static int set_callee_state(struct bpf_verifier_env *env, |
7110 | struct bpf_func_state *caller, | |
7111 | struct bpf_func_state *callee, int insn_idx) | |
7112 | { | |
7113 | int i; | |
7114 | ||
7115 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
7116 | * pointers, which connects us up to the liveness chain | |
7117 | */ | |
7118 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
7119 | callee->regs[i] = caller->regs[i]; | |
7120 | return 0; | |
7121 | } | |
7122 | ||
7123 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
7124 | int *insn_idx) | |
7125 | { | |
7126 | int subprog, target_insn; | |
7127 | ||
7128 | target_insn = *insn_idx + insn->imm + 1; | |
7129 | subprog = find_subprog(env, target_insn); | |
7130 | if (subprog < 0) { | |
7131 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
7132 | target_insn); | |
7133 | return -EFAULT; | |
7134 | } | |
7135 | ||
7136 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
7137 | } | |
7138 | ||
69c087ba YS |
7139 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
7140 | struct bpf_func_state *caller, | |
7141 | struct bpf_func_state *callee, | |
7142 | int insn_idx) | |
7143 | { | |
7144 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
7145 | struct bpf_map *map; | |
7146 | int err; | |
7147 | ||
7148 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
7149 | verbose(env, "tail_call abusing map_ptr\n"); | |
7150 | return -EINVAL; | |
7151 | } | |
7152 | ||
7153 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
7154 | if (!map->ops->map_set_for_each_callback_args || | |
7155 | !map->ops->map_for_each_callback) { | |
7156 | verbose(env, "callback function not allowed for map\n"); | |
7157 | return -ENOTSUPP; | |
7158 | } | |
7159 | ||
7160 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
7161 | if (err) | |
7162 | return err; | |
7163 | ||
7164 | callee->in_callback_fn = true; | |
1bfe26fb | 7165 | callee->callback_ret_range = tnum_range(0, 1); |
69c087ba YS |
7166 | return 0; |
7167 | } | |
7168 | ||
e6f2dd0f JK |
7169 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
7170 | struct bpf_func_state *caller, | |
7171 | struct bpf_func_state *callee, | |
7172 | int insn_idx) | |
7173 | { | |
7174 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
7175 | * u64 flags); | |
7176 | * callback_fn(u32 index, void *callback_ctx); | |
7177 | */ | |
7178 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
7179 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
7180 | ||
7181 | /* unused */ | |
7182 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
7183 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7184 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
7185 | ||
7186 | callee->in_callback_fn = true; | |
1bfe26fb | 7187 | callee->callback_ret_range = tnum_range(0, 1); |
e6f2dd0f JK |
7188 | return 0; |
7189 | } | |
7190 | ||
b00628b1 AS |
7191 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
7192 | struct bpf_func_state *caller, | |
7193 | struct bpf_func_state *callee, | |
7194 | int insn_idx) | |
7195 | { | |
7196 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
7197 | ||
7198 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
7199 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
7200 | */ | |
7201 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
7202 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
7203 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
7204 | ||
7205 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
7206 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
7207 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
7208 | ||
7209 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
7210 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
7211 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
7212 | ||
7213 | /* unused */ | |
7214 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7215 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 7216 | callee->in_async_callback_fn = true; |
1bfe26fb | 7217 | callee->callback_ret_range = tnum_range(0, 1); |
b00628b1 AS |
7218 | return 0; |
7219 | } | |
7220 | ||
7c7e3d31 SL |
7221 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
7222 | struct bpf_func_state *caller, | |
7223 | struct bpf_func_state *callee, | |
7224 | int insn_idx) | |
7225 | { | |
7226 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
7227 | * void *callback_fn, void *callback_ctx, u64 flags) | |
7228 | * (callback_fn)(struct task_struct *task, | |
7229 | * struct vm_area_struct *vma, void *callback_ctx); | |
7230 | */ | |
7231 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
7232 | ||
7233 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
7234 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
7235 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 7236 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
7237 | |
7238 | /* pointer to stack or null */ | |
7239 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
7240 | ||
7241 | /* unused */ | |
7242 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7243 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
7244 | callee->in_callback_fn = true; | |
1bfe26fb | 7245 | callee->callback_ret_range = tnum_range(0, 1); |
7c7e3d31 SL |
7246 | return 0; |
7247 | } | |
7248 | ||
20571567 DV |
7249 | static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env, |
7250 | struct bpf_func_state *caller, | |
7251 | struct bpf_func_state *callee, | |
7252 | int insn_idx) | |
7253 | { | |
7254 | /* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void | |
7255 | * callback_ctx, u64 flags); | |
7256 | * callback_fn(struct bpf_dynptr_t* dynptr, void *callback_ctx); | |
7257 | */ | |
7258 | __mark_reg_not_init(env, &callee->regs[BPF_REG_0]); | |
7259 | callee->regs[BPF_REG_1].type = PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL; | |
7260 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
7261 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
7262 | ||
7263 | /* unused */ | |
7264 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
7265 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7266 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
7267 | ||
7268 | callee->in_callback_fn = true; | |
c92a7a52 | 7269 | callee->callback_ret_range = tnum_range(0, 1); |
20571567 DV |
7270 | return 0; |
7271 | } | |
7272 | ||
f4d7e40a AS |
7273 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
7274 | { | |
7275 | struct bpf_verifier_state *state = env->cur_state; | |
7276 | struct bpf_func_state *caller, *callee; | |
7277 | struct bpf_reg_state *r0; | |
fd978bf7 | 7278 | int err; |
f4d7e40a AS |
7279 | |
7280 | callee = state->frame[state->curframe]; | |
7281 | r0 = &callee->regs[BPF_REG_0]; | |
7282 | if (r0->type == PTR_TO_STACK) { | |
7283 | /* technically it's ok to return caller's stack pointer | |
7284 | * (or caller's caller's pointer) back to the caller, | |
7285 | * since these pointers are valid. Only current stack | |
7286 | * pointer will be invalid as soon as function exits, | |
7287 | * but let's be conservative | |
7288 | */ | |
7289 | verbose(env, "cannot return stack pointer to the caller\n"); | |
7290 | return -EINVAL; | |
7291 | } | |
7292 | ||
eb86559a | 7293 | caller = state->frame[state->curframe - 1]; |
69c087ba YS |
7294 | if (callee->in_callback_fn) { |
7295 | /* enforce R0 return value range [0, 1]. */ | |
1bfe26fb | 7296 | struct tnum range = callee->callback_ret_range; |
69c087ba YS |
7297 | |
7298 | if (r0->type != SCALAR_VALUE) { | |
7299 | verbose(env, "R0 not a scalar value\n"); | |
7300 | return -EACCES; | |
7301 | } | |
7302 | if (!tnum_in(range, r0->var_off)) { | |
7303 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
7304 | return -EINVAL; | |
7305 | } | |
7306 | } else { | |
7307 | /* return to the caller whatever r0 had in the callee */ | |
7308 | caller->regs[BPF_REG_0] = *r0; | |
7309 | } | |
f4d7e40a | 7310 | |
9d9d00ac KKD |
7311 | /* callback_fn frame should have released its own additions to parent's |
7312 | * reference state at this point, or check_reference_leak would | |
7313 | * complain, hence it must be the same as the caller. There is no need | |
7314 | * to copy it back. | |
7315 | */ | |
7316 | if (!callee->in_callback_fn) { | |
7317 | /* Transfer references to the caller */ | |
7318 | err = copy_reference_state(caller, callee); | |
7319 | if (err) | |
7320 | return err; | |
7321 | } | |
fd978bf7 | 7322 | |
f4d7e40a | 7323 | *insn_idx = callee->callsite + 1; |
06ee7115 | 7324 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 7325 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 7326 | print_verifier_state(env, callee, true); |
f4d7e40a | 7327 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 7328 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
7329 | } |
7330 | /* clear everything in the callee */ | |
7331 | free_func_state(callee); | |
eb86559a | 7332 | state->frame[state->curframe--] = NULL; |
f4d7e40a AS |
7333 | return 0; |
7334 | } | |
7335 | ||
849fa506 YS |
7336 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
7337 | int func_id, | |
7338 | struct bpf_call_arg_meta *meta) | |
7339 | { | |
7340 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
7341 | ||
7342 | if (ret_type != RET_INTEGER || | |
7343 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 7344 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
7345 | func_id != BPF_FUNC_probe_read_str && |
7346 | func_id != BPF_FUNC_probe_read_kernel_str && | |
7347 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
7348 | return; |
7349 | ||
10060503 | 7350 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 7351 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
7352 | ret_reg->smin_value = -MAX_ERRNO; |
7353 | ret_reg->s32_min_value = -MAX_ERRNO; | |
3844d153 | 7354 | reg_bounds_sync(ret_reg); |
849fa506 YS |
7355 | } |
7356 | ||
c93552c4 DB |
7357 | static int |
7358 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
7359 | int func_id, int insn_idx) | |
7360 | { | |
7361 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 7362 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
7363 | |
7364 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
7365 | func_id != BPF_FUNC_map_lookup_elem && |
7366 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
7367 | func_id != BPF_FUNC_map_delete_elem && |
7368 | func_id != BPF_FUNC_map_push_elem && | |
7369 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 7370 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f | 7371 | func_id != BPF_FUNC_for_each_map_elem && |
07343110 FZ |
7372 | func_id != BPF_FUNC_redirect_map && |
7373 | func_id != BPF_FUNC_map_lookup_percpu_elem) | |
c93552c4 | 7374 | return 0; |
09772d92 | 7375 | |
591fe988 | 7376 | if (map == NULL) { |
c93552c4 DB |
7377 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
7378 | return -EINVAL; | |
7379 | } | |
7380 | ||
591fe988 DB |
7381 | /* In case of read-only, some additional restrictions |
7382 | * need to be applied in order to prevent altering the | |
7383 | * state of the map from program side. | |
7384 | */ | |
7385 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
7386 | (func_id == BPF_FUNC_map_delete_elem || | |
7387 | func_id == BPF_FUNC_map_update_elem || | |
7388 | func_id == BPF_FUNC_map_push_elem || | |
7389 | func_id == BPF_FUNC_map_pop_elem)) { | |
7390 | verbose(env, "write into map forbidden\n"); | |
7391 | return -EACCES; | |
7392 | } | |
7393 | ||
d2e4c1e6 | 7394 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 7395 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 7396 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 7397 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 7398 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 7399 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
7400 | return 0; |
7401 | } | |
7402 | ||
d2e4c1e6 DB |
7403 | static int |
7404 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
7405 | int func_id, int insn_idx) | |
7406 | { | |
7407 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
7408 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
7409 | struct bpf_map *map = meta->map_ptr; | |
a657182a | 7410 | u64 val, max; |
cc52d914 | 7411 | int err; |
d2e4c1e6 DB |
7412 | |
7413 | if (func_id != BPF_FUNC_tail_call) | |
7414 | return 0; | |
7415 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
7416 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
7417 | return -EINVAL; | |
7418 | } | |
7419 | ||
d2e4c1e6 | 7420 | reg = ®s[BPF_REG_3]; |
a657182a DB |
7421 | val = reg->var_off.value; |
7422 | max = map->max_entries; | |
d2e4c1e6 | 7423 | |
a657182a | 7424 | if (!(register_is_const(reg) && val < max)) { |
d2e4c1e6 DB |
7425 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
7426 | return 0; | |
7427 | } | |
7428 | ||
cc52d914 DB |
7429 | err = mark_chain_precision(env, BPF_REG_3); |
7430 | if (err) | |
7431 | return err; | |
d2e4c1e6 DB |
7432 | if (bpf_map_key_unseen(aux)) |
7433 | bpf_map_key_store(aux, val); | |
7434 | else if (!bpf_map_key_poisoned(aux) && | |
7435 | bpf_map_key_immediate(aux) != val) | |
7436 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
7437 | return 0; | |
7438 | } | |
7439 | ||
fd978bf7 JS |
7440 | static int check_reference_leak(struct bpf_verifier_env *env) |
7441 | { | |
7442 | struct bpf_func_state *state = cur_func(env); | |
9d9d00ac | 7443 | bool refs_lingering = false; |
fd978bf7 JS |
7444 | int i; |
7445 | ||
9d9d00ac KKD |
7446 | if (state->frameno && !state->in_callback_fn) |
7447 | return 0; | |
7448 | ||
fd978bf7 | 7449 | for (i = 0; i < state->acquired_refs; i++) { |
9d9d00ac KKD |
7450 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) |
7451 | continue; | |
fd978bf7 JS |
7452 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
7453 | state->refs[i].id, state->refs[i].insn_idx); | |
9d9d00ac | 7454 | refs_lingering = true; |
fd978bf7 | 7455 | } |
9d9d00ac | 7456 | return refs_lingering ? -EINVAL : 0; |
fd978bf7 JS |
7457 | } |
7458 | ||
7b15523a FR |
7459 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
7460 | struct bpf_reg_state *regs) | |
7461 | { | |
7462 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
7463 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
7464 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
7465 | int err, fmt_map_off, num_args; | |
7466 | u64 fmt_addr; | |
7467 | char *fmt; | |
7468 | ||
7469 | /* data must be an array of u64 */ | |
7470 | if (data_len_reg->var_off.value % 8) | |
7471 | return -EINVAL; | |
7472 | num_args = data_len_reg->var_off.value / 8; | |
7473 | ||
7474 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
7475 | * and map_direct_value_addr is set. | |
7476 | */ | |
7477 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
7478 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
7479 | fmt_map_off); | |
8e8ee109 FR |
7480 | if (err) { |
7481 | verbose(env, "verifier bug\n"); | |
7482 | return -EFAULT; | |
7483 | } | |
7b15523a FR |
7484 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
7485 | ||
7486 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
7487 | * can focus on validating the format specifiers. | |
7488 | */ | |
48cac3f4 | 7489 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
7b15523a FR |
7490 | if (err < 0) |
7491 | verbose(env, "Invalid format string\n"); | |
7492 | ||
7493 | return err; | |
7494 | } | |
7495 | ||
9b99edca JO |
7496 | static int check_get_func_ip(struct bpf_verifier_env *env) |
7497 | { | |
9b99edca JO |
7498 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
7499 | int func_id = BPF_FUNC_get_func_ip; | |
7500 | ||
7501 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 7502 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
7503 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
7504 | func_id_name(func_id), func_id); | |
7505 | return -ENOTSUPP; | |
7506 | } | |
7507 | return 0; | |
9ffd9f3f JO |
7508 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
7509 | return 0; | |
9b99edca JO |
7510 | } |
7511 | ||
7512 | verbose(env, "func %s#%d not supported for program type %d\n", | |
7513 | func_id_name(func_id), func_id, type); | |
7514 | return -ENOTSUPP; | |
7515 | } | |
7516 | ||
1ade2371 EZ |
7517 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
7518 | { | |
7519 | return &env->insn_aux_data[env->insn_idx]; | |
7520 | } | |
7521 | ||
7522 | static bool loop_flag_is_zero(struct bpf_verifier_env *env) | |
7523 | { | |
7524 | struct bpf_reg_state *regs = cur_regs(env); | |
7525 | struct bpf_reg_state *reg = ®s[BPF_REG_4]; | |
7526 | bool reg_is_null = register_is_null(reg); | |
7527 | ||
7528 | if (reg_is_null) | |
7529 | mark_chain_precision(env, BPF_REG_4); | |
7530 | ||
7531 | return reg_is_null; | |
7532 | } | |
7533 | ||
7534 | static void update_loop_inline_state(struct bpf_verifier_env *env, u32 subprogno) | |
7535 | { | |
7536 | struct bpf_loop_inline_state *state = &cur_aux(env)->loop_inline_state; | |
7537 | ||
7538 | if (!state->initialized) { | |
7539 | state->initialized = 1; | |
7540 | state->fit_for_inline = loop_flag_is_zero(env); | |
7541 | state->callback_subprogno = subprogno; | |
7542 | return; | |
7543 | } | |
7544 | ||
7545 | if (!state->fit_for_inline) | |
7546 | return; | |
7547 | ||
7548 | state->fit_for_inline = (loop_flag_is_zero(env) && | |
7549 | state->callback_subprogno == subprogno); | |
7550 | } | |
7551 | ||
69c087ba YS |
7552 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
7553 | int *insn_idx_p) | |
17a52670 | 7554 | { |
aef9d4a3 | 7555 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
17a52670 | 7556 | const struct bpf_func_proto *fn = NULL; |
3c480732 | 7557 | enum bpf_return_type ret_type; |
c25b2ae1 | 7558 | enum bpf_type_flag ret_flag; |
638f5b90 | 7559 | struct bpf_reg_state *regs; |
33ff9823 | 7560 | struct bpf_call_arg_meta meta; |
69c087ba | 7561 | int insn_idx = *insn_idx_p; |
969bf05e | 7562 | bool changes_data; |
69c087ba | 7563 | int i, err, func_id; |
17a52670 AS |
7564 | |
7565 | /* find function prototype */ | |
69c087ba | 7566 | func_id = insn->imm; |
17a52670 | 7567 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
7568 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
7569 | func_id); | |
17a52670 AS |
7570 | return -EINVAL; |
7571 | } | |
7572 | ||
00176a34 | 7573 | if (env->ops->get_func_proto) |
5e43f899 | 7574 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 7575 | if (!fn) { |
61bd5218 JK |
7576 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
7577 | func_id); | |
17a52670 AS |
7578 | return -EINVAL; |
7579 | } | |
7580 | ||
7581 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 7582 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 7583 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
7584 | return -EINVAL; |
7585 | } | |
7586 | ||
eae2e83e JO |
7587 | if (fn->allowed && !fn->allowed(env->prog)) { |
7588 | verbose(env, "helper call is not allowed in probe\n"); | |
7589 | return -EINVAL; | |
7590 | } | |
7591 | ||
01685c5b YS |
7592 | if (!env->prog->aux->sleepable && fn->might_sleep) { |
7593 | verbose(env, "helper call might sleep in a non-sleepable prog\n"); | |
7594 | return -EINVAL; | |
7595 | } | |
7596 | ||
04514d13 | 7597 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 7598 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
7599 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
7600 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
7601 | func_id_name(func_id), func_id); | |
7602 | return -EINVAL; | |
7603 | } | |
969bf05e | 7604 | |
33ff9823 | 7605 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 7606 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 7607 | |
0c9a7a7e | 7608 | err = check_func_proto(fn, func_id); |
435faee1 | 7609 | if (err) { |
61bd5218 | 7610 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 7611 | func_id_name(func_id), func_id); |
435faee1 DB |
7612 | return err; |
7613 | } | |
7614 | ||
9bb00b28 YS |
7615 | if (env->cur_state->active_rcu_lock) { |
7616 | if (fn->might_sleep) { | |
7617 | verbose(env, "sleepable helper %s#%d in rcu_read_lock region\n", | |
7618 | func_id_name(func_id), func_id); | |
7619 | return -EINVAL; | |
7620 | } | |
7621 | ||
7622 | if (env->prog->aux->sleepable && is_storage_get_function(func_id)) | |
7623 | env->insn_aux_data[insn_idx].storage_get_func_atomic = true; | |
7624 | } | |
7625 | ||
d83525ca | 7626 | meta.func_id = func_id; |
17a52670 | 7627 | /* check args */ |
523a4cf4 | 7628 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
af7ec138 | 7629 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
7630 | if (err) |
7631 | return err; | |
7632 | } | |
17a52670 | 7633 | |
c93552c4 DB |
7634 | err = record_func_map(env, &meta, func_id, insn_idx); |
7635 | if (err) | |
7636 | return err; | |
7637 | ||
d2e4c1e6 DB |
7638 | err = record_func_key(env, &meta, func_id, insn_idx); |
7639 | if (err) | |
7640 | return err; | |
7641 | ||
435faee1 DB |
7642 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
7643 | * is inferred from register state. | |
7644 | */ | |
7645 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
7646 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
7647 | BPF_WRITE, -1, false); | |
435faee1 DB |
7648 | if (err) |
7649 | return err; | |
7650 | } | |
7651 | ||
8f14852e KKD |
7652 | regs = cur_regs(env); |
7653 | ||
97e03f52 JK |
7654 | if (meta.uninit_dynptr_regno) { |
7655 | /* we write BPF_DW bits (8 bytes) at a time */ | |
7656 | for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) { | |
7657 | err = check_mem_access(env, insn_idx, meta.uninit_dynptr_regno, | |
7658 | i, BPF_DW, BPF_WRITE, -1, false); | |
7659 | if (err) | |
7660 | return err; | |
7661 | } | |
7662 | ||
7663 | err = mark_stack_slots_dynptr(env, ®s[meta.uninit_dynptr_regno], | |
7664 | fn->arg_type[meta.uninit_dynptr_regno - BPF_REG_1], | |
7665 | insn_idx); | |
7666 | if (err) | |
7667 | return err; | |
7668 | } | |
7669 | ||
8f14852e KKD |
7670 | if (meta.release_regno) { |
7671 | err = -EINVAL; | |
97e03f52 JK |
7672 | if (arg_type_is_dynptr(fn->arg_type[meta.release_regno - BPF_REG_1])) |
7673 | err = unmark_stack_slots_dynptr(env, ®s[meta.release_regno]); | |
7674 | else if (meta.ref_obj_id) | |
8f14852e KKD |
7675 | err = release_reference(env, meta.ref_obj_id); |
7676 | /* meta.ref_obj_id can only be 0 if register that is meant to be | |
7677 | * released is NULL, which must be > R0. | |
7678 | */ | |
7679 | else if (register_is_null(®s[meta.release_regno])) | |
7680 | err = 0; | |
46f8bc92 MKL |
7681 | if (err) { |
7682 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
7683 | func_id_name(func_id), func_id); | |
fd978bf7 | 7684 | return err; |
46f8bc92 | 7685 | } |
fd978bf7 JS |
7686 | } |
7687 | ||
e6f2dd0f JK |
7688 | switch (func_id) { |
7689 | case BPF_FUNC_tail_call: | |
7690 | err = check_reference_leak(env); | |
7691 | if (err) { | |
7692 | verbose(env, "tail_call would lead to reference leak\n"); | |
7693 | return err; | |
7694 | } | |
7695 | break; | |
7696 | case BPF_FUNC_get_local_storage: | |
7697 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
7698 | * this is required because get_local_storage() can't return an error. | |
7699 | */ | |
7700 | if (!register_is_null(®s[BPF_REG_2])) { | |
7701 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
7702 | return -EINVAL; | |
7703 | } | |
7704 | break; | |
7705 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
7706 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7707 | set_map_elem_callback_state); | |
e6f2dd0f JK |
7708 | break; |
7709 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
7710 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7711 | set_timer_callback_state); | |
e6f2dd0f JK |
7712 | break; |
7713 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
7714 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7715 | set_find_vma_callback_state); | |
e6f2dd0f JK |
7716 | break; |
7717 | case BPF_FUNC_snprintf: | |
7b15523a | 7718 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
7719 | break; |
7720 | case BPF_FUNC_loop: | |
1ade2371 | 7721 | update_loop_inline_state(env, meta.subprogno); |
e6f2dd0f JK |
7722 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7723 | set_loop_callback_state); | |
7724 | break; | |
263ae152 JK |
7725 | case BPF_FUNC_dynptr_from_mem: |
7726 | if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) { | |
7727 | verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n", | |
7728 | reg_type_str(env, regs[BPF_REG_1].type)); | |
7729 | return -EACCES; | |
7730 | } | |
69fd337a SF |
7731 | break; |
7732 | case BPF_FUNC_set_retval: | |
aef9d4a3 SF |
7733 | if (prog_type == BPF_PROG_TYPE_LSM && |
7734 | env->prog->expected_attach_type == BPF_LSM_CGROUP) { | |
69fd337a SF |
7735 | if (!env->prog->aux->attach_func_proto->type) { |
7736 | /* Make sure programs that attach to void | |
7737 | * hooks don't try to modify return value. | |
7738 | */ | |
7739 | verbose(env, "BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
7740 | return -EINVAL; | |
7741 | } | |
7742 | } | |
7743 | break; | |
88374342 JK |
7744 | case BPF_FUNC_dynptr_data: |
7745 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { | |
7746 | if (arg_type_is_dynptr(fn->arg_type[i])) { | |
20571567 DV |
7747 | struct bpf_reg_state *reg = ®s[BPF_REG_1 + i]; |
7748 | ||
88374342 JK |
7749 | if (meta.ref_obj_id) { |
7750 | verbose(env, "verifier internal error: meta.ref_obj_id already set\n"); | |
7751 | return -EFAULT; | |
7752 | } | |
20571567 DV |
7753 | |
7754 | if (base_type(reg->type) != PTR_TO_DYNPTR) | |
7755 | /* Find the id of the dynptr we're | |
7756 | * tracking the reference of | |
7757 | */ | |
7758 | meta.ref_obj_id = stack_slot_get_id(env, reg); | |
88374342 JK |
7759 | break; |
7760 | } | |
7761 | } | |
7762 | if (i == MAX_BPF_FUNC_REG_ARGS) { | |
7763 | verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n"); | |
7764 | return -EFAULT; | |
7765 | } | |
7766 | break; | |
20571567 DV |
7767 | case BPF_FUNC_user_ringbuf_drain: |
7768 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
7769 | set_user_ringbuf_callback_state); | |
7770 | break; | |
7b15523a FR |
7771 | } |
7772 | ||
e6f2dd0f JK |
7773 | if (err) |
7774 | return err; | |
7775 | ||
17a52670 | 7776 | /* reset caller saved regs */ |
dc503a8a | 7777 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 7778 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
7779 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
7780 | } | |
17a52670 | 7781 | |
5327ed3d JW |
7782 | /* helper call returns 64-bit value. */ |
7783 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
7784 | ||
dc503a8a | 7785 | /* update return register (already marked as written above) */ |
3c480732 | 7786 | ret_type = fn->ret_type; |
0c9a7a7e JK |
7787 | ret_flag = type_flag(ret_type); |
7788 | ||
7789 | switch (base_type(ret_type)) { | |
7790 | case RET_INTEGER: | |
f1174f77 | 7791 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 7792 | mark_reg_unknown(env, regs, BPF_REG_0); |
0c9a7a7e JK |
7793 | break; |
7794 | case RET_VOID: | |
17a52670 | 7795 | regs[BPF_REG_0].type = NOT_INIT; |
0c9a7a7e JK |
7796 | break; |
7797 | case RET_PTR_TO_MAP_VALUE: | |
f1174f77 | 7798 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 7799 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
7800 | /* remember map_ptr, so that check_map_access() |
7801 | * can check 'value_size' boundary of memory access | |
7802 | * to map element returned from bpf_map_lookup_elem() | |
7803 | */ | |
33ff9823 | 7804 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
7805 | verbose(env, |
7806 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
7807 | return -EINVAL; |
7808 | } | |
33ff9823 | 7809 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 7810 | regs[BPF_REG_0].map_uid = meta.map_uid; |
c25b2ae1 HL |
7811 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
7812 | if (!type_may_be_null(ret_type) && | |
db559117 | 7813 | btf_record_has_field(meta.map_ptr->record, BPF_SPIN_LOCK)) { |
c25b2ae1 | 7814 | regs[BPF_REG_0].id = ++env->id_gen; |
4d31f301 | 7815 | } |
0c9a7a7e JK |
7816 | break; |
7817 | case RET_PTR_TO_SOCKET: | |
c64b7983 | 7818 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7819 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
0c9a7a7e JK |
7820 | break; |
7821 | case RET_PTR_TO_SOCK_COMMON: | |
85a51f8c | 7822 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7823 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
0c9a7a7e JK |
7824 | break; |
7825 | case RET_PTR_TO_TCP_SOCK: | |
655a51e5 | 7826 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7827 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
0c9a7a7e | 7828 | break; |
2de2669b | 7829 | case RET_PTR_TO_MEM: |
457f4436 | 7830 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7831 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
457f4436 | 7832 | regs[BPF_REG_0].mem_size = meta.mem_size; |
0c9a7a7e JK |
7833 | break; |
7834 | case RET_PTR_TO_MEM_OR_BTF_ID: | |
7835 | { | |
eaa6bcb7 HL |
7836 | const struct btf_type *t; |
7837 | ||
7838 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 7839 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
7840 | if (!btf_type_is_struct(t)) { |
7841 | u32 tsize; | |
7842 | const struct btf_type *ret; | |
7843 | const char *tname; | |
7844 | ||
7845 | /* resolve the type size of ksym. */ | |
22dc4a0f | 7846 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 7847 | if (IS_ERR(ret)) { |
22dc4a0f | 7848 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
7849 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
7850 | tname, PTR_ERR(ret)); | |
7851 | return -EINVAL; | |
7852 | } | |
c25b2ae1 | 7853 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
eaa6bcb7 HL |
7854 | regs[BPF_REG_0].mem_size = tsize; |
7855 | } else { | |
34d3a78c HL |
7856 | /* MEM_RDONLY may be carried from ret_flag, but it |
7857 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise | |
7858 | * it will confuse the check of PTR_TO_BTF_ID in | |
7859 | * check_mem_access(). | |
7860 | */ | |
7861 | ret_flag &= ~MEM_RDONLY; | |
7862 | ||
c25b2ae1 | 7863 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
22dc4a0f | 7864 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
7865 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
7866 | } | |
0c9a7a7e JK |
7867 | break; |
7868 | } | |
7869 | case RET_PTR_TO_BTF_ID: | |
7870 | { | |
c0a5a21c | 7871 | struct btf *ret_btf; |
af7ec138 YS |
7872 | int ret_btf_id; |
7873 | ||
7874 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
c25b2ae1 | 7875 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
c0a5a21c | 7876 | if (func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac KKD |
7877 | ret_btf = meta.kptr_field->kptr.btf; |
7878 | ret_btf_id = meta.kptr_field->kptr.btf_id; | |
c0a5a21c | 7879 | } else { |
47e34cb7 DM |
7880 | if (fn->ret_btf_id == BPF_PTR_POISON) { |
7881 | verbose(env, "verifier internal error:"); | |
7882 | verbose(env, "func %s has non-overwritten BPF_PTR_POISON return type\n", | |
7883 | func_id_name(func_id)); | |
7884 | return -EINVAL; | |
7885 | } | |
c0a5a21c KKD |
7886 | ret_btf = btf_vmlinux; |
7887 | ret_btf_id = *fn->ret_btf_id; | |
7888 | } | |
af7ec138 | 7889 | if (ret_btf_id == 0) { |
3c480732 HL |
7890 | verbose(env, "invalid return type %u of func %s#%d\n", |
7891 | base_type(ret_type), func_id_name(func_id), | |
7892 | func_id); | |
af7ec138 YS |
7893 | return -EINVAL; |
7894 | } | |
c0a5a21c | 7895 | regs[BPF_REG_0].btf = ret_btf; |
af7ec138 | 7896 | regs[BPF_REG_0].btf_id = ret_btf_id; |
0c9a7a7e JK |
7897 | break; |
7898 | } | |
7899 | default: | |
3c480732 HL |
7900 | verbose(env, "unknown return type %u of func %s#%d\n", |
7901 | base_type(ret_type), func_id_name(func_id), func_id); | |
17a52670 AS |
7902 | return -EINVAL; |
7903 | } | |
04fd61ab | 7904 | |
c25b2ae1 | 7905 | if (type_may_be_null(regs[BPF_REG_0].type)) |
93c230e3 MKL |
7906 | regs[BPF_REG_0].id = ++env->id_gen; |
7907 | ||
b2d8ef19 DM |
7908 | if (helper_multiple_ref_obj_use(func_id, meta.map_ptr)) { |
7909 | verbose(env, "verifier internal error: func %s#%d sets ref_obj_id more than once\n", | |
7910 | func_id_name(func_id), func_id); | |
7911 | return -EFAULT; | |
7912 | } | |
7913 | ||
88374342 | 7914 | if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) { |
1b986589 MKL |
7915 | /* For release_reference() */ |
7916 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 7917 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
7918 | int id = acquire_reference_state(env, insn_idx); |
7919 | ||
7920 | if (id < 0) | |
7921 | return id; | |
7922 | /* For mark_ptr_or_null_reg() */ | |
7923 | regs[BPF_REG_0].id = id; | |
7924 | /* For release_reference() */ | |
7925 | regs[BPF_REG_0].ref_obj_id = id; | |
7926 | } | |
1b986589 | 7927 | |
849fa506 YS |
7928 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
7929 | ||
61bd5218 | 7930 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
7931 | if (err) |
7932 | return err; | |
04fd61ab | 7933 | |
fa28dcb8 SL |
7934 | if ((func_id == BPF_FUNC_get_stack || |
7935 | func_id == BPF_FUNC_get_task_stack) && | |
7936 | !env->prog->has_callchain_buf) { | |
c195651e YS |
7937 | const char *err_str; |
7938 | ||
7939 | #ifdef CONFIG_PERF_EVENTS | |
7940 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
7941 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
7942 | #else | |
7943 | err = -ENOTSUPP; | |
7944 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
7945 | #endif | |
7946 | if (err) { | |
7947 | verbose(env, err_str, func_id_name(func_id), func_id); | |
7948 | return err; | |
7949 | } | |
7950 | ||
7951 | env->prog->has_callchain_buf = true; | |
7952 | } | |
7953 | ||
5d99cb2c SL |
7954 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
7955 | env->prog->call_get_stack = true; | |
7956 | ||
9b99edca JO |
7957 | if (func_id == BPF_FUNC_get_func_ip) { |
7958 | if (check_get_func_ip(env)) | |
7959 | return -ENOTSUPP; | |
7960 | env->prog->call_get_func_ip = true; | |
7961 | } | |
7962 | ||
969bf05e AS |
7963 | if (changes_data) |
7964 | clear_all_pkt_pointers(env); | |
7965 | return 0; | |
7966 | } | |
7967 | ||
e6ac2450 MKL |
7968 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
7969 | * the BTF func_proto's return value size and argument. | |
7970 | */ | |
7971 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
7972 | size_t reg_size) | |
7973 | { | |
7974 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
7975 | ||
7976 | if (regno == BPF_REG_0) { | |
7977 | /* Function return value */ | |
7978 | reg->live |= REG_LIVE_WRITTEN; | |
7979 | reg->subreg_def = reg_size == sizeof(u64) ? | |
7980 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
7981 | } else { | |
7982 | /* Function argument */ | |
7983 | if (reg_size == sizeof(u64)) { | |
7984 | mark_insn_zext(env, reg); | |
7985 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
7986 | } else { | |
7987 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
7988 | } | |
7989 | } | |
7990 | } | |
7991 | ||
00b85860 KKD |
7992 | struct bpf_kfunc_call_arg_meta { |
7993 | /* In parameters */ | |
7994 | struct btf *btf; | |
7995 | u32 func_id; | |
7996 | u32 kfunc_flags; | |
7997 | const struct btf_type *func_proto; | |
7998 | const char *func_name; | |
7999 | /* Out parameters */ | |
8000 | u32 ref_obj_id; | |
8001 | u8 release_regno; | |
8002 | bool r0_rdonly; | |
fd264ca0 | 8003 | u32 ret_btf_id; |
00b85860 | 8004 | u64 r0_size; |
a50388db KKD |
8005 | struct { |
8006 | u64 value; | |
8007 | bool found; | |
8008 | } arg_constant; | |
ac9f0605 KKD |
8009 | struct { |
8010 | struct btf *btf; | |
8011 | u32 btf_id; | |
8012 | } arg_obj_drop; | |
8cab76ec KKD |
8013 | struct { |
8014 | struct btf_field *field; | |
8015 | } arg_list_head; | |
00b85860 | 8016 | }; |
e6ac2450 | 8017 | |
00b85860 KKD |
8018 | static bool is_kfunc_acquire(struct bpf_kfunc_call_arg_meta *meta) |
8019 | { | |
8020 | return meta->kfunc_flags & KF_ACQUIRE; | |
8021 | } | |
a5d82727 | 8022 | |
00b85860 KKD |
8023 | static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) |
8024 | { | |
8025 | return meta->kfunc_flags & KF_RET_NULL; | |
8026 | } | |
2357672c | 8027 | |
00b85860 KKD |
8028 | static bool is_kfunc_release(struct bpf_kfunc_call_arg_meta *meta) |
8029 | { | |
8030 | return meta->kfunc_flags & KF_RELEASE; | |
8031 | } | |
e6ac2450 | 8032 | |
00b85860 KKD |
8033 | static bool is_kfunc_trusted_args(struct bpf_kfunc_call_arg_meta *meta) |
8034 | { | |
8035 | return meta->kfunc_flags & KF_TRUSTED_ARGS; | |
8036 | } | |
4dd48c6f | 8037 | |
00b85860 KKD |
8038 | static bool is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta) |
8039 | { | |
8040 | return meta->kfunc_flags & KF_SLEEPABLE; | |
8041 | } | |
5c073f26 | 8042 | |
00b85860 KKD |
8043 | static bool is_kfunc_destructive(struct bpf_kfunc_call_arg_meta *meta) |
8044 | { | |
8045 | return meta->kfunc_flags & KF_DESTRUCTIVE; | |
8046 | } | |
eb1f7f71 | 8047 | |
fca1aa75 YS |
8048 | static bool is_kfunc_rcu(struct bpf_kfunc_call_arg_meta *meta) |
8049 | { | |
8050 | return meta->kfunc_flags & KF_RCU; | |
8051 | } | |
8052 | ||
00b85860 KKD |
8053 | static bool is_kfunc_arg_kptr_get(struct bpf_kfunc_call_arg_meta *meta, int arg) |
8054 | { | |
8055 | return arg == 0 && (meta->kfunc_flags & KF_KPTR_GET); | |
8056 | } | |
e6ac2450 | 8057 | |
a50388db KKD |
8058 | static bool __kfunc_param_match_suffix(const struct btf *btf, |
8059 | const struct btf_param *arg, | |
8060 | const char *suffix) | |
00b85860 | 8061 | { |
a50388db | 8062 | int suffix_len = strlen(suffix), len; |
00b85860 | 8063 | const char *param_name; |
e6ac2450 | 8064 | |
00b85860 KKD |
8065 | /* In the future, this can be ported to use BTF tagging */ |
8066 | param_name = btf_name_by_offset(btf, arg->name_off); | |
8067 | if (str_is_empty(param_name)) | |
8068 | return false; | |
8069 | len = strlen(param_name); | |
a50388db | 8070 | if (len < suffix_len) |
00b85860 | 8071 | return false; |
a50388db KKD |
8072 | param_name += len - suffix_len; |
8073 | return !strncmp(param_name, suffix, suffix_len); | |
8074 | } | |
5c073f26 | 8075 | |
a50388db KKD |
8076 | static bool is_kfunc_arg_mem_size(const struct btf *btf, |
8077 | const struct btf_param *arg, | |
8078 | const struct bpf_reg_state *reg) | |
8079 | { | |
8080 | const struct btf_type *t; | |
5c073f26 | 8081 | |
a50388db KKD |
8082 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
8083 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
00b85860 | 8084 | return false; |
eb1f7f71 | 8085 | |
a50388db KKD |
8086 | return __kfunc_param_match_suffix(btf, arg, "__sz"); |
8087 | } | |
eb1f7f71 | 8088 | |
a50388db KKD |
8089 | static bool is_kfunc_arg_constant(const struct btf *btf, const struct btf_param *arg) |
8090 | { | |
8091 | return __kfunc_param_match_suffix(btf, arg, "__k"); | |
00b85860 | 8092 | } |
eb1f7f71 | 8093 | |
958cf2e2 KKD |
8094 | static bool is_kfunc_arg_ignore(const struct btf *btf, const struct btf_param *arg) |
8095 | { | |
8096 | return __kfunc_param_match_suffix(btf, arg, "__ign"); | |
8097 | } | |
5c073f26 | 8098 | |
ac9f0605 KKD |
8099 | static bool is_kfunc_arg_alloc_obj(const struct btf *btf, const struct btf_param *arg) |
8100 | { | |
8101 | return __kfunc_param_match_suffix(btf, arg, "__alloc"); | |
8102 | } | |
e6ac2450 | 8103 | |
00b85860 KKD |
8104 | static bool is_kfunc_arg_scalar_with_name(const struct btf *btf, |
8105 | const struct btf_param *arg, | |
8106 | const char *name) | |
8107 | { | |
8108 | int len, target_len = strlen(name); | |
8109 | const char *param_name; | |
e6ac2450 | 8110 | |
00b85860 KKD |
8111 | param_name = btf_name_by_offset(btf, arg->name_off); |
8112 | if (str_is_empty(param_name)) | |
8113 | return false; | |
8114 | len = strlen(param_name); | |
8115 | if (len != target_len) | |
8116 | return false; | |
8117 | if (strcmp(param_name, name)) | |
8118 | return false; | |
e6ac2450 | 8119 | |
00b85860 | 8120 | return true; |
e6ac2450 MKL |
8121 | } |
8122 | ||
00b85860 KKD |
8123 | enum { |
8124 | KF_ARG_DYNPTR_ID, | |
8cab76ec KKD |
8125 | KF_ARG_LIST_HEAD_ID, |
8126 | KF_ARG_LIST_NODE_ID, | |
00b85860 | 8127 | }; |
b03c9f9f | 8128 | |
00b85860 KKD |
8129 | BTF_ID_LIST(kf_arg_btf_ids) |
8130 | BTF_ID(struct, bpf_dynptr_kern) | |
8cab76ec KKD |
8131 | BTF_ID(struct, bpf_list_head) |
8132 | BTF_ID(struct, bpf_list_node) | |
b03c9f9f | 8133 | |
8cab76ec KKD |
8134 | static bool __is_kfunc_ptr_arg_type(const struct btf *btf, |
8135 | const struct btf_param *arg, int type) | |
3f50f132 | 8136 | { |
00b85860 KKD |
8137 | const struct btf_type *t; |
8138 | u32 res_id; | |
3f50f132 | 8139 | |
00b85860 KKD |
8140 | t = btf_type_skip_modifiers(btf, arg->type, NULL); |
8141 | if (!t) | |
8142 | return false; | |
8143 | if (!btf_type_is_ptr(t)) | |
8144 | return false; | |
8145 | t = btf_type_skip_modifiers(btf, t->type, &res_id); | |
8146 | if (!t) | |
8147 | return false; | |
8cab76ec | 8148 | return btf_types_are_same(btf, res_id, btf_vmlinux, kf_arg_btf_ids[type]); |
3f50f132 JF |
8149 | } |
8150 | ||
8cab76ec | 8151 | static bool is_kfunc_arg_dynptr(const struct btf *btf, const struct btf_param *arg) |
b03c9f9f | 8152 | { |
8cab76ec | 8153 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_DYNPTR_ID); |
969bf05e AS |
8154 | } |
8155 | ||
8cab76ec | 8156 | static bool is_kfunc_arg_list_head(const struct btf *btf, const struct btf_param *arg) |
3f50f132 | 8157 | { |
8cab76ec | 8158 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_HEAD_ID); |
3f50f132 JF |
8159 | } |
8160 | ||
8cab76ec | 8161 | static bool is_kfunc_arg_list_node(const struct btf *btf, const struct btf_param *arg) |
bb7f0f98 | 8162 | { |
8cab76ec | 8163 | return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_LIST_NODE_ID); |
00b85860 KKD |
8164 | } |
8165 | ||
8166 | /* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ | |
8167 | static bool __btf_type_is_scalar_struct(struct bpf_verifier_env *env, | |
8168 | const struct btf *btf, | |
8169 | const struct btf_type *t, int rec) | |
8170 | { | |
8171 | const struct btf_type *member_type; | |
8172 | const struct btf_member *member; | |
8173 | u32 i; | |
8174 | ||
8175 | if (!btf_type_is_struct(t)) | |
8176 | return false; | |
8177 | ||
8178 | for_each_member(i, t, member) { | |
8179 | const struct btf_array *array; | |
8180 | ||
8181 | member_type = btf_type_skip_modifiers(btf, member->type, NULL); | |
8182 | if (btf_type_is_struct(member_type)) { | |
8183 | if (rec >= 3) { | |
8184 | verbose(env, "max struct nesting depth exceeded\n"); | |
8185 | return false; | |
8186 | } | |
8187 | if (!__btf_type_is_scalar_struct(env, btf, member_type, rec + 1)) | |
8188 | return false; | |
8189 | continue; | |
8190 | } | |
8191 | if (btf_type_is_array(member_type)) { | |
8192 | array = btf_array(member_type); | |
8193 | if (!array->nelems) | |
8194 | return false; | |
8195 | member_type = btf_type_skip_modifiers(btf, array->type, NULL); | |
8196 | if (!btf_type_is_scalar(member_type)) | |
8197 | return false; | |
8198 | continue; | |
8199 | } | |
8200 | if (!btf_type_is_scalar(member_type)) | |
8201 | return false; | |
8202 | } | |
8203 | return true; | |
8204 | } | |
8205 | ||
8206 | ||
8207 | static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { | |
8208 | #ifdef CONFIG_NET | |
8209 | [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], | |
8210 | [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
8211 | [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], | |
8212 | #endif | |
8213 | }; | |
8214 | ||
8215 | enum kfunc_ptr_arg_type { | |
8216 | KF_ARG_PTR_TO_CTX, | |
ac9f0605 | 8217 | KF_ARG_PTR_TO_ALLOC_BTF_ID, /* Allocated object */ |
00b85860 KKD |
8218 | KF_ARG_PTR_TO_KPTR, /* PTR_TO_KPTR but type specific */ |
8219 | KF_ARG_PTR_TO_DYNPTR, | |
8cab76ec KKD |
8220 | KF_ARG_PTR_TO_LIST_HEAD, |
8221 | KF_ARG_PTR_TO_LIST_NODE, | |
00b85860 KKD |
8222 | KF_ARG_PTR_TO_BTF_ID, /* Also covers reg2btf_ids conversions */ |
8223 | KF_ARG_PTR_TO_MEM, | |
8224 | KF_ARG_PTR_TO_MEM_SIZE, /* Size derived from next argument, skip it */ | |
8225 | }; | |
8226 | ||
ac9f0605 KKD |
8227 | enum special_kfunc_type { |
8228 | KF_bpf_obj_new_impl, | |
8229 | KF_bpf_obj_drop_impl, | |
8cab76ec KKD |
8230 | KF_bpf_list_push_front, |
8231 | KF_bpf_list_push_back, | |
8232 | KF_bpf_list_pop_front, | |
8233 | KF_bpf_list_pop_back, | |
fd264ca0 | 8234 | KF_bpf_cast_to_kern_ctx, |
a35b9af4 | 8235 | KF_bpf_rdonly_cast, |
9bb00b28 YS |
8236 | KF_bpf_rcu_read_lock, |
8237 | KF_bpf_rcu_read_unlock, | |
ac9f0605 KKD |
8238 | }; |
8239 | ||
8240 | BTF_SET_START(special_kfunc_set) | |
8241 | BTF_ID(func, bpf_obj_new_impl) | |
8242 | BTF_ID(func, bpf_obj_drop_impl) | |
8cab76ec KKD |
8243 | BTF_ID(func, bpf_list_push_front) |
8244 | BTF_ID(func, bpf_list_push_back) | |
8245 | BTF_ID(func, bpf_list_pop_front) | |
8246 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 8247 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 8248 | BTF_ID(func, bpf_rdonly_cast) |
ac9f0605 KKD |
8249 | BTF_SET_END(special_kfunc_set) |
8250 | ||
8251 | BTF_ID_LIST(special_kfunc_list) | |
8252 | BTF_ID(func, bpf_obj_new_impl) | |
8253 | BTF_ID(func, bpf_obj_drop_impl) | |
8cab76ec KKD |
8254 | BTF_ID(func, bpf_list_push_front) |
8255 | BTF_ID(func, bpf_list_push_back) | |
8256 | BTF_ID(func, bpf_list_pop_front) | |
8257 | BTF_ID(func, bpf_list_pop_back) | |
fd264ca0 | 8258 | BTF_ID(func, bpf_cast_to_kern_ctx) |
a35b9af4 | 8259 | BTF_ID(func, bpf_rdonly_cast) |
9bb00b28 YS |
8260 | BTF_ID(func, bpf_rcu_read_lock) |
8261 | BTF_ID(func, bpf_rcu_read_unlock) | |
8262 | ||
8263 | static bool is_kfunc_bpf_rcu_read_lock(struct bpf_kfunc_call_arg_meta *meta) | |
8264 | { | |
8265 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_lock]; | |
8266 | } | |
8267 | ||
8268 | static bool is_kfunc_bpf_rcu_read_unlock(struct bpf_kfunc_call_arg_meta *meta) | |
8269 | { | |
8270 | return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_unlock]; | |
8271 | } | |
ac9f0605 | 8272 | |
00b85860 KKD |
8273 | static enum kfunc_ptr_arg_type |
8274 | get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, | |
8275 | struct bpf_kfunc_call_arg_meta *meta, | |
8276 | const struct btf_type *t, const struct btf_type *ref_t, | |
8277 | const char *ref_tname, const struct btf_param *args, | |
8278 | int argno, int nargs) | |
8279 | { | |
8280 | u32 regno = argno + 1; | |
8281 | struct bpf_reg_state *regs = cur_regs(env); | |
8282 | struct bpf_reg_state *reg = ®s[regno]; | |
8283 | bool arg_mem_size = false; | |
8284 | ||
fd264ca0 YS |
8285 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) |
8286 | return KF_ARG_PTR_TO_CTX; | |
8287 | ||
00b85860 KKD |
8288 | /* In this function, we verify the kfunc's BTF as per the argument type, |
8289 | * leaving the rest of the verification with respect to the register | |
8290 | * type to our caller. When a set of conditions hold in the BTF type of | |
8291 | * arguments, we resolve it to a known kfunc_ptr_arg_type. | |
8292 | */ | |
8293 | if (btf_get_prog_ctx_type(&env->log, meta->btf, t, resolve_prog_type(env->prog), argno)) | |
8294 | return KF_ARG_PTR_TO_CTX; | |
8295 | ||
ac9f0605 KKD |
8296 | if (is_kfunc_arg_alloc_obj(meta->btf, &args[argno])) |
8297 | return KF_ARG_PTR_TO_ALLOC_BTF_ID; | |
8298 | ||
00b85860 KKD |
8299 | if (is_kfunc_arg_kptr_get(meta, argno)) { |
8300 | if (!btf_type_is_ptr(ref_t)) { | |
8301 | verbose(env, "arg#0 BTF type must be a double pointer for kptr_get kfunc\n"); | |
8302 | return -EINVAL; | |
8303 | } | |
8304 | ref_t = btf_type_by_id(meta->btf, ref_t->type); | |
8305 | ref_tname = btf_name_by_offset(meta->btf, ref_t->name_off); | |
8306 | if (!btf_type_is_struct(ref_t)) { | |
8307 | verbose(env, "kernel function %s args#0 pointer type %s %s is not supported\n", | |
8308 | meta->func_name, btf_type_str(ref_t), ref_tname); | |
8309 | return -EINVAL; | |
8310 | } | |
8311 | return KF_ARG_PTR_TO_KPTR; | |
8312 | } | |
8313 | ||
8314 | if (is_kfunc_arg_dynptr(meta->btf, &args[argno])) | |
8315 | return KF_ARG_PTR_TO_DYNPTR; | |
8316 | ||
8cab76ec KKD |
8317 | if (is_kfunc_arg_list_head(meta->btf, &args[argno])) |
8318 | return KF_ARG_PTR_TO_LIST_HEAD; | |
8319 | ||
8320 | if (is_kfunc_arg_list_node(meta->btf, &args[argno])) | |
8321 | return KF_ARG_PTR_TO_LIST_NODE; | |
8322 | ||
00b85860 KKD |
8323 | if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { |
8324 | if (!btf_type_is_struct(ref_t)) { | |
8325 | verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n", | |
8326 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
8327 | return -EINVAL; | |
8328 | } | |
8329 | return KF_ARG_PTR_TO_BTF_ID; | |
8330 | } | |
8331 | ||
8332 | if (argno + 1 < nargs && is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1])) | |
8333 | arg_mem_size = true; | |
8334 | ||
8335 | /* This is the catch all argument type of register types supported by | |
8336 | * check_helper_mem_access. However, we only allow when argument type is | |
8337 | * pointer to scalar, or struct composed (recursively) of scalars. When | |
8338 | * arg_mem_size is true, the pointer can be void *. | |
8339 | */ | |
8340 | if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(env, meta->btf, ref_t, 0) && | |
8341 | (arg_mem_size ? !btf_type_is_void(ref_t) : 1)) { | |
8342 | verbose(env, "arg#%d pointer type %s %s must point to %sscalar, or struct with scalar\n", | |
8343 | argno, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : ""); | |
8344 | return -EINVAL; | |
8345 | } | |
8346 | return arg_mem_size ? KF_ARG_PTR_TO_MEM_SIZE : KF_ARG_PTR_TO_MEM; | |
8347 | } | |
8348 | ||
8349 | static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env, | |
8350 | struct bpf_reg_state *reg, | |
8351 | const struct btf_type *ref_t, | |
8352 | const char *ref_tname, u32 ref_id, | |
8353 | struct bpf_kfunc_call_arg_meta *meta, | |
8354 | int argno) | |
8355 | { | |
8356 | const struct btf_type *reg_ref_t; | |
8357 | bool strict_type_match = false; | |
8358 | const struct btf *reg_btf; | |
8359 | const char *reg_ref_tname; | |
8360 | u32 reg_ref_id; | |
8361 | ||
3f00c523 | 8362 | if (base_type(reg->type) == PTR_TO_BTF_ID) { |
00b85860 KKD |
8363 | reg_btf = reg->btf; |
8364 | reg_ref_id = reg->btf_id; | |
8365 | } else { | |
8366 | reg_btf = btf_vmlinux; | |
8367 | reg_ref_id = *reg2btf_ids[base_type(reg->type)]; | |
8368 | } | |
8369 | ||
8370 | if (is_kfunc_trusted_args(meta) || (is_kfunc_release(meta) && reg->ref_obj_id)) | |
8371 | strict_type_match = true; | |
8372 | ||
8373 | reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id, ®_ref_id); | |
8374 | reg_ref_tname = btf_name_by_offset(reg_btf, reg_ref_t->name_off); | |
8375 | if (!btf_struct_ids_match(&env->log, reg_btf, reg_ref_id, reg->off, meta->btf, ref_id, strict_type_match)) { | |
8376 | verbose(env, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", | |
8377 | meta->func_name, argno, btf_type_str(ref_t), ref_tname, argno + 1, | |
8378 | btf_type_str(reg_ref_t), reg_ref_tname); | |
8379 | return -EINVAL; | |
8380 | } | |
8381 | return 0; | |
8382 | } | |
8383 | ||
8384 | static int process_kf_arg_ptr_to_kptr(struct bpf_verifier_env *env, | |
8385 | struct bpf_reg_state *reg, | |
8386 | const struct btf_type *ref_t, | |
8387 | const char *ref_tname, | |
8388 | struct bpf_kfunc_call_arg_meta *meta, | |
8389 | int argno) | |
8390 | { | |
8391 | struct btf_field *kptr_field; | |
8392 | ||
8393 | /* check_func_arg_reg_off allows var_off for | |
8394 | * PTR_TO_MAP_VALUE, but we need fixed offset to find | |
8395 | * off_desc. | |
8396 | */ | |
8397 | if (!tnum_is_const(reg->var_off)) { | |
8398 | verbose(env, "arg#0 must have constant offset\n"); | |
8399 | return -EINVAL; | |
8400 | } | |
8401 | ||
8402 | kptr_field = btf_record_find(reg->map_ptr->record, reg->off + reg->var_off.value, BPF_KPTR); | |
8403 | if (!kptr_field || kptr_field->type != BPF_KPTR_REF) { | |
8404 | verbose(env, "arg#0 no referenced kptr at map value offset=%llu\n", | |
8405 | reg->off + reg->var_off.value); | |
8406 | return -EINVAL; | |
8407 | } | |
8408 | ||
8409 | if (!btf_struct_ids_match(&env->log, meta->btf, ref_t->type, 0, kptr_field->kptr.btf, | |
8410 | kptr_field->kptr.btf_id, true)) { | |
8411 | verbose(env, "kernel function %s args#%d expected pointer to %s %s\n", | |
8412 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
8413 | return -EINVAL; | |
8414 | } | |
8415 | return 0; | |
8416 | } | |
8417 | ||
534e86bc KKD |
8418 | static int ref_set_release_on_unlock(struct bpf_verifier_env *env, u32 ref_obj_id) |
8419 | { | |
8420 | struct bpf_func_state *state = cur_func(env); | |
8421 | struct bpf_reg_state *reg; | |
8422 | int i; | |
8423 | ||
8424 | /* bpf_spin_lock only allows calling list_push and list_pop, no BPF | |
8425 | * subprogs, no global functions. This means that the references would | |
8426 | * not be released inside the critical section but they may be added to | |
8427 | * the reference state, and the acquired_refs are never copied out for a | |
8428 | * different frame as BPF to BPF calls don't work in bpf_spin_lock | |
8429 | * critical sections. | |
8430 | */ | |
8431 | if (!ref_obj_id) { | |
8432 | verbose(env, "verifier internal error: ref_obj_id is zero for release_on_unlock\n"); | |
8433 | return -EFAULT; | |
8434 | } | |
8435 | for (i = 0; i < state->acquired_refs; i++) { | |
8436 | if (state->refs[i].id == ref_obj_id) { | |
8437 | if (state->refs[i].release_on_unlock) { | |
8438 | verbose(env, "verifier internal error: expected false release_on_unlock"); | |
8439 | return -EFAULT; | |
8440 | } | |
8441 | state->refs[i].release_on_unlock = true; | |
8442 | /* Now mark everyone sharing same ref_obj_id as untrusted */ | |
8443 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ | |
8444 | if (reg->ref_obj_id == ref_obj_id) | |
8445 | reg->type |= PTR_UNTRUSTED; | |
8446 | })); | |
8447 | return 0; | |
8448 | } | |
8449 | } | |
8450 | verbose(env, "verifier internal error: ref state missing for ref_obj_id\n"); | |
8451 | return -EFAULT; | |
8452 | } | |
8453 | ||
8cab76ec KKD |
8454 | /* Implementation details: |
8455 | * | |
8456 | * Each register points to some region of memory, which we define as an | |
8457 | * allocation. Each allocation may embed a bpf_spin_lock which protects any | |
8458 | * special BPF objects (bpf_list_head, bpf_rb_root, etc.) part of the same | |
8459 | * allocation. The lock and the data it protects are colocated in the same | |
8460 | * memory region. | |
8461 | * | |
8462 | * Hence, everytime a register holds a pointer value pointing to such | |
8463 | * allocation, the verifier preserves a unique reg->id for it. | |
8464 | * | |
8465 | * The verifier remembers the lock 'ptr' and the lock 'id' whenever | |
8466 | * bpf_spin_lock is called. | |
8467 | * | |
8468 | * To enable this, lock state in the verifier captures two values: | |
8469 | * active_lock.ptr = Register's type specific pointer | |
8470 | * active_lock.id = A unique ID for each register pointer value | |
8471 | * | |
8472 | * Currently, PTR_TO_MAP_VALUE and PTR_TO_BTF_ID | MEM_ALLOC are the two | |
8473 | * supported register types. | |
8474 | * | |
8475 | * The active_lock.ptr in case of map values is the reg->map_ptr, and in case of | |
8476 | * allocated objects is the reg->btf pointer. | |
8477 | * | |
8478 | * The active_lock.id is non-unique for maps supporting direct_value_addr, as we | |
8479 | * can establish the provenance of the map value statically for each distinct | |
8480 | * lookup into such maps. They always contain a single map value hence unique | |
8481 | * IDs for each pseudo load pessimizes the algorithm and rejects valid programs. | |
8482 | * | |
8483 | * So, in case of global variables, they use array maps with max_entries = 1, | |
8484 | * hence their active_lock.ptr becomes map_ptr and id = 0 (since they all point | |
8485 | * into the same map value as max_entries is 1, as described above). | |
8486 | * | |
8487 | * In case of inner map lookups, the inner map pointer has same map_ptr as the | |
8488 | * outer map pointer (in verifier context), but each lookup into an inner map | |
8489 | * assigns a fresh reg->id to the lookup, so while lookups into distinct inner | |
8490 | * maps from the same outer map share the same map_ptr as active_lock.ptr, they | |
8491 | * will get different reg->id assigned to each lookup, hence different | |
8492 | * active_lock.id. | |
8493 | * | |
8494 | * In case of allocated objects, active_lock.ptr is the reg->btf, and the | |
8495 | * reg->id is a unique ID preserved after the NULL pointer check on the pointer | |
8496 | * returned from bpf_obj_new. Each allocation receives a new reg->id. | |
8497 | */ | |
8498 | static int check_reg_allocation_locked(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
8499 | { | |
8500 | void *ptr; | |
8501 | u32 id; | |
8502 | ||
8503 | switch ((int)reg->type) { | |
8504 | case PTR_TO_MAP_VALUE: | |
8505 | ptr = reg->map_ptr; | |
8506 | break; | |
8507 | case PTR_TO_BTF_ID | MEM_ALLOC: | |
3f00c523 | 8508 | case PTR_TO_BTF_ID | MEM_ALLOC | PTR_TRUSTED: |
8cab76ec KKD |
8509 | ptr = reg->btf; |
8510 | break; | |
8511 | default: | |
8512 | verbose(env, "verifier internal error: unknown reg type for lock check\n"); | |
8513 | return -EFAULT; | |
8514 | } | |
8515 | id = reg->id; | |
8516 | ||
8517 | if (!env->cur_state->active_lock.ptr) | |
8518 | return -EINVAL; | |
8519 | if (env->cur_state->active_lock.ptr != ptr || | |
8520 | env->cur_state->active_lock.id != id) { | |
8521 | verbose(env, "held lock and object are not in the same allocation\n"); | |
8522 | return -EINVAL; | |
8523 | } | |
8524 | return 0; | |
8525 | } | |
8526 | ||
8527 | static bool is_bpf_list_api_kfunc(u32 btf_id) | |
8528 | { | |
8529 | return btf_id == special_kfunc_list[KF_bpf_list_push_front] || | |
8530 | btf_id == special_kfunc_list[KF_bpf_list_push_back] || | |
8531 | btf_id == special_kfunc_list[KF_bpf_list_pop_front] || | |
8532 | btf_id == special_kfunc_list[KF_bpf_list_pop_back]; | |
8533 | } | |
8534 | ||
8535 | static int process_kf_arg_ptr_to_list_head(struct bpf_verifier_env *env, | |
8536 | struct bpf_reg_state *reg, u32 regno, | |
8537 | struct bpf_kfunc_call_arg_meta *meta) | |
8538 | { | |
8539 | struct btf_field *field; | |
8540 | struct btf_record *rec; | |
8541 | u32 list_head_off; | |
8542 | ||
8543 | if (meta->btf != btf_vmlinux || !is_bpf_list_api_kfunc(meta->func_id)) { | |
8544 | verbose(env, "verifier internal error: bpf_list_head argument for unknown kfunc\n"); | |
8545 | return -EFAULT; | |
8546 | } | |
8547 | ||
8548 | if (!tnum_is_const(reg->var_off)) { | |
8549 | verbose(env, | |
8550 | "R%d doesn't have constant offset. bpf_list_head has to be at the constant offset\n", | |
8551 | regno); | |
8552 | return -EINVAL; | |
8553 | } | |
8554 | ||
8555 | rec = reg_btf_record(reg); | |
8556 | list_head_off = reg->off + reg->var_off.value; | |
8557 | field = btf_record_find(rec, list_head_off, BPF_LIST_HEAD); | |
8558 | if (!field) { | |
8559 | verbose(env, "bpf_list_head not found at offset=%u\n", list_head_off); | |
8560 | return -EINVAL; | |
8561 | } | |
8562 | ||
8563 | /* All functions require bpf_list_head to be protected using a bpf_spin_lock */ | |
8564 | if (check_reg_allocation_locked(env, reg)) { | |
8565 | verbose(env, "bpf_spin_lock at off=%d must be held for bpf_list_head\n", | |
8566 | rec->spin_lock_off); | |
8567 | return -EINVAL; | |
8568 | } | |
8569 | ||
8570 | if (meta->arg_list_head.field) { | |
8571 | verbose(env, "verifier internal error: repeating bpf_list_head arg\n"); | |
8572 | return -EFAULT; | |
8573 | } | |
8574 | meta->arg_list_head.field = field; | |
8575 | return 0; | |
8576 | } | |
8577 | ||
8578 | static int process_kf_arg_ptr_to_list_node(struct bpf_verifier_env *env, | |
8579 | struct bpf_reg_state *reg, u32 regno, | |
8580 | struct bpf_kfunc_call_arg_meta *meta) | |
8581 | { | |
8582 | const struct btf_type *et, *t; | |
8583 | struct btf_field *field; | |
8584 | struct btf_record *rec; | |
8585 | u32 list_node_off; | |
8586 | ||
8587 | if (meta->btf != btf_vmlinux || | |
8588 | (meta->func_id != special_kfunc_list[KF_bpf_list_push_front] && | |
8589 | meta->func_id != special_kfunc_list[KF_bpf_list_push_back])) { | |
8590 | verbose(env, "verifier internal error: bpf_list_node argument for unknown kfunc\n"); | |
8591 | return -EFAULT; | |
8592 | } | |
8593 | ||
8594 | if (!tnum_is_const(reg->var_off)) { | |
8595 | verbose(env, | |
8596 | "R%d doesn't have constant offset. bpf_list_node has to be at the constant offset\n", | |
8597 | regno); | |
8598 | return -EINVAL; | |
8599 | } | |
8600 | ||
8601 | rec = reg_btf_record(reg); | |
8602 | list_node_off = reg->off + reg->var_off.value; | |
8603 | field = btf_record_find(rec, list_node_off, BPF_LIST_NODE); | |
8604 | if (!field || field->offset != list_node_off) { | |
8605 | verbose(env, "bpf_list_node not found at offset=%u\n", list_node_off); | |
8606 | return -EINVAL; | |
8607 | } | |
8608 | ||
8609 | field = meta->arg_list_head.field; | |
8610 | ||
8611 | et = btf_type_by_id(field->list_head.btf, field->list_head.value_btf_id); | |
8612 | t = btf_type_by_id(reg->btf, reg->btf_id); | |
8613 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, 0, field->list_head.btf, | |
8614 | field->list_head.value_btf_id, true)) { | |
8615 | verbose(env, "operation on bpf_list_head expects arg#1 bpf_list_node at offset=%d " | |
8616 | "in struct %s, but arg is at offset=%d in struct %s\n", | |
8617 | field->list_head.node_offset, btf_name_by_offset(field->list_head.btf, et->name_off), | |
8618 | list_node_off, btf_name_by_offset(reg->btf, t->name_off)); | |
8619 | return -EINVAL; | |
8620 | } | |
8621 | ||
8622 | if (list_node_off != field->list_head.node_offset) { | |
8623 | verbose(env, "arg#1 offset=%d, but expected bpf_list_node at offset=%d in struct %s\n", | |
8624 | list_node_off, field->list_head.node_offset, | |
8625 | btf_name_by_offset(field->list_head.btf, et->name_off)); | |
8626 | return -EINVAL; | |
8627 | } | |
534e86bc KKD |
8628 | /* Set arg#1 for expiration after unlock */ |
8629 | return ref_set_release_on_unlock(env, reg->ref_obj_id); | |
8cab76ec KKD |
8630 | } |
8631 | ||
00b85860 KKD |
8632 | static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta) |
8633 | { | |
8634 | const char *func_name = meta->func_name, *ref_tname; | |
8635 | const struct btf *btf = meta->btf; | |
8636 | const struct btf_param *args; | |
8637 | u32 i, nargs; | |
8638 | int ret; | |
8639 | ||
8640 | args = (const struct btf_param *)(meta->func_proto + 1); | |
8641 | nargs = btf_type_vlen(meta->func_proto); | |
8642 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { | |
8643 | verbose(env, "Function %s has %d > %d args\n", func_name, nargs, | |
8644 | MAX_BPF_FUNC_REG_ARGS); | |
8645 | return -EINVAL; | |
8646 | } | |
8647 | ||
8648 | /* Check that BTF function arguments match actual types that the | |
8649 | * verifier sees. | |
8650 | */ | |
8651 | for (i = 0; i < nargs; i++) { | |
8652 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[i + 1]; | |
8653 | const struct btf_type *t, *ref_t, *resolve_ret; | |
8654 | enum bpf_arg_type arg_type = ARG_DONTCARE; | |
8655 | u32 regno = i + 1, ref_id, type_size; | |
8656 | bool is_ret_buf_sz = false; | |
8657 | int kf_arg_type; | |
8658 | ||
8659 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); | |
958cf2e2 KKD |
8660 | |
8661 | if (is_kfunc_arg_ignore(btf, &args[i])) | |
8662 | continue; | |
8663 | ||
00b85860 KKD |
8664 | if (btf_type_is_scalar(t)) { |
8665 | if (reg->type != SCALAR_VALUE) { | |
8666 | verbose(env, "R%d is not a scalar\n", regno); | |
8667 | return -EINVAL; | |
8668 | } | |
a50388db KKD |
8669 | |
8670 | if (is_kfunc_arg_constant(meta->btf, &args[i])) { | |
8671 | if (meta->arg_constant.found) { | |
8672 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
8673 | return -EFAULT; | |
8674 | } | |
8675 | if (!tnum_is_const(reg->var_off)) { | |
8676 | verbose(env, "R%d must be a known constant\n", regno); | |
8677 | return -EINVAL; | |
8678 | } | |
8679 | ret = mark_chain_precision(env, regno); | |
8680 | if (ret < 0) | |
8681 | return ret; | |
8682 | meta->arg_constant.found = true; | |
8683 | meta->arg_constant.value = reg->var_off.value; | |
8684 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) { | |
00b85860 KKD |
8685 | meta->r0_rdonly = true; |
8686 | is_ret_buf_sz = true; | |
8687 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) { | |
8688 | is_ret_buf_sz = true; | |
8689 | } | |
8690 | ||
8691 | if (is_ret_buf_sz) { | |
8692 | if (meta->r0_size) { | |
8693 | verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc"); | |
8694 | return -EINVAL; | |
8695 | } | |
8696 | ||
8697 | if (!tnum_is_const(reg->var_off)) { | |
8698 | verbose(env, "R%d is not a const\n", regno); | |
8699 | return -EINVAL; | |
8700 | } | |
8701 | ||
8702 | meta->r0_size = reg->var_off.value; | |
8703 | ret = mark_chain_precision(env, regno); | |
8704 | if (ret) | |
8705 | return ret; | |
8706 | } | |
8707 | continue; | |
8708 | } | |
8709 | ||
8710 | if (!btf_type_is_ptr(t)) { | |
8711 | verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t)); | |
8712 | return -EINVAL; | |
8713 | } | |
8714 | ||
8715 | if (reg->ref_obj_id) { | |
8716 | if (is_kfunc_release(meta) && meta->ref_obj_id) { | |
8717 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
8718 | regno, reg->ref_obj_id, | |
8719 | meta->ref_obj_id); | |
8720 | return -EFAULT; | |
8721 | } | |
8722 | meta->ref_obj_id = reg->ref_obj_id; | |
8723 | if (is_kfunc_release(meta)) | |
8724 | meta->release_regno = regno; | |
8725 | } | |
8726 | ||
8727 | ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); | |
8728 | ref_tname = btf_name_by_offset(btf, ref_t->name_off); | |
8729 | ||
8730 | kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs); | |
8731 | if (kf_arg_type < 0) | |
8732 | return kf_arg_type; | |
8733 | ||
8734 | switch (kf_arg_type) { | |
ac9f0605 | 8735 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
00b85860 | 8736 | case KF_ARG_PTR_TO_BTF_ID: |
fca1aa75 | 8737 | if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta)) |
00b85860 | 8738 | break; |
3f00c523 DV |
8739 | |
8740 | if (!is_trusted_reg(reg)) { | |
fca1aa75 YS |
8741 | if (!is_kfunc_rcu(meta)) { |
8742 | verbose(env, "R%d must be referenced or trusted\n", regno); | |
8743 | return -EINVAL; | |
8744 | } | |
8745 | if (!is_rcu_reg(reg)) { | |
8746 | verbose(env, "R%d must be a rcu pointer\n", regno); | |
8747 | return -EINVAL; | |
8748 | } | |
00b85860 | 8749 | } |
fca1aa75 | 8750 | |
00b85860 KKD |
8751 | fallthrough; |
8752 | case KF_ARG_PTR_TO_CTX: | |
8753 | /* Trusted arguments have the same offset checks as release arguments */ | |
8754 | arg_type |= OBJ_RELEASE; | |
8755 | break; | |
8756 | case KF_ARG_PTR_TO_KPTR: | |
8757 | case KF_ARG_PTR_TO_DYNPTR: | |
8cab76ec KKD |
8758 | case KF_ARG_PTR_TO_LIST_HEAD: |
8759 | case KF_ARG_PTR_TO_LIST_NODE: | |
00b85860 KKD |
8760 | case KF_ARG_PTR_TO_MEM: |
8761 | case KF_ARG_PTR_TO_MEM_SIZE: | |
8762 | /* Trusted by default */ | |
8763 | break; | |
8764 | default: | |
8765 | WARN_ON_ONCE(1); | |
8766 | return -EFAULT; | |
8767 | } | |
8768 | ||
8769 | if (is_kfunc_release(meta) && reg->ref_obj_id) | |
8770 | arg_type |= OBJ_RELEASE; | |
8771 | ret = check_func_arg_reg_off(env, reg, regno, arg_type); | |
8772 | if (ret < 0) | |
8773 | return ret; | |
8774 | ||
8775 | switch (kf_arg_type) { | |
8776 | case KF_ARG_PTR_TO_CTX: | |
8777 | if (reg->type != PTR_TO_CTX) { | |
8778 | verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t)); | |
8779 | return -EINVAL; | |
8780 | } | |
fd264ca0 YS |
8781 | |
8782 | if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { | |
8783 | ret = get_kern_ctx_btf_id(&env->log, resolve_prog_type(env->prog)); | |
8784 | if (ret < 0) | |
8785 | return -EINVAL; | |
8786 | meta->ret_btf_id = ret; | |
8787 | } | |
00b85860 | 8788 | break; |
ac9f0605 KKD |
8789 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
8790 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
8791 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
8792 | return -EINVAL; | |
8793 | } | |
8794 | if (!reg->ref_obj_id) { | |
8795 | verbose(env, "allocated object must be referenced\n"); | |
8796 | return -EINVAL; | |
8797 | } | |
8798 | if (meta->btf == btf_vmlinux && | |
8799 | meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { | |
8800 | meta->arg_obj_drop.btf = reg->btf; | |
8801 | meta->arg_obj_drop.btf_id = reg->btf_id; | |
8802 | } | |
8803 | break; | |
00b85860 KKD |
8804 | case KF_ARG_PTR_TO_KPTR: |
8805 | if (reg->type != PTR_TO_MAP_VALUE) { | |
8806 | verbose(env, "arg#0 expected pointer to map value\n"); | |
8807 | return -EINVAL; | |
8808 | } | |
8809 | ret = process_kf_arg_ptr_to_kptr(env, reg, ref_t, ref_tname, meta, i); | |
8810 | if (ret < 0) | |
8811 | return ret; | |
8812 | break; | |
8813 | case KF_ARG_PTR_TO_DYNPTR: | |
8814 | if (reg->type != PTR_TO_STACK) { | |
8815 | verbose(env, "arg#%d expected pointer to stack\n", i); | |
8816 | return -EINVAL; | |
8817 | } | |
8818 | ||
8819 | if (!is_dynptr_reg_valid_init(env, reg)) { | |
8820 | verbose(env, "arg#%d pointer type %s %s must be valid and initialized\n", | |
8821 | i, btf_type_str(ref_t), ref_tname); | |
8822 | return -EINVAL; | |
8823 | } | |
8824 | ||
8825 | if (!is_dynptr_type_expected(env, reg, ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL)) { | |
8826 | verbose(env, "arg#%d pointer type %s %s points to unsupported dynamic pointer type\n", | |
8827 | i, btf_type_str(ref_t), ref_tname); | |
8828 | return -EINVAL; | |
8829 | } | |
8830 | break; | |
8cab76ec KKD |
8831 | case KF_ARG_PTR_TO_LIST_HEAD: |
8832 | if (reg->type != PTR_TO_MAP_VALUE && | |
8833 | reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
8834 | verbose(env, "arg#%d expected pointer to map value or allocated object\n", i); | |
8835 | return -EINVAL; | |
8836 | } | |
8837 | if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) { | |
8838 | verbose(env, "allocated object must be referenced\n"); | |
8839 | return -EINVAL; | |
8840 | } | |
8841 | ret = process_kf_arg_ptr_to_list_head(env, reg, regno, meta); | |
8842 | if (ret < 0) | |
8843 | return ret; | |
8844 | break; | |
8845 | case KF_ARG_PTR_TO_LIST_NODE: | |
8846 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
8847 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
8848 | return -EINVAL; | |
8849 | } | |
8850 | if (!reg->ref_obj_id) { | |
8851 | verbose(env, "allocated object must be referenced\n"); | |
8852 | return -EINVAL; | |
8853 | } | |
8854 | ret = process_kf_arg_ptr_to_list_node(env, reg, regno, meta); | |
8855 | if (ret < 0) | |
8856 | return ret; | |
8857 | break; | |
00b85860 KKD |
8858 | case KF_ARG_PTR_TO_BTF_ID: |
8859 | /* Only base_type is checked, further checks are done here */ | |
3f00c523 | 8860 | if ((base_type(reg->type) != PTR_TO_BTF_ID || |
fca1aa75 | 8861 | (bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) && |
3f00c523 DV |
8862 | !reg2btf_ids[base_type(reg->type)]) { |
8863 | verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type)); | |
8864 | verbose(env, "expected %s or socket\n", | |
8865 | reg_type_str(env, base_type(reg->type) | | |
8866 | (type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS))); | |
00b85860 KKD |
8867 | return -EINVAL; |
8868 | } | |
8869 | ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i); | |
8870 | if (ret < 0) | |
8871 | return ret; | |
8872 | break; | |
8873 | case KF_ARG_PTR_TO_MEM: | |
8874 | resolve_ret = btf_resolve_size(btf, ref_t, &type_size); | |
8875 | if (IS_ERR(resolve_ret)) { | |
8876 | verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n", | |
8877 | i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret)); | |
8878 | return -EINVAL; | |
8879 | } | |
8880 | ret = check_mem_reg(env, reg, regno, type_size); | |
8881 | if (ret < 0) | |
8882 | return ret; | |
8883 | break; | |
8884 | case KF_ARG_PTR_TO_MEM_SIZE: | |
8885 | ret = check_kfunc_mem_size_reg(env, ®s[regno + 1], regno + 1); | |
8886 | if (ret < 0) { | |
8887 | verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1); | |
8888 | return ret; | |
8889 | } | |
8890 | /* Skip next '__sz' argument */ | |
8891 | i++; | |
8892 | break; | |
8893 | } | |
8894 | } | |
8895 | ||
8896 | if (is_kfunc_release(meta) && !meta->release_regno) { | |
8897 | verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n", | |
8898 | func_name); | |
8899 | return -EINVAL; | |
8900 | } | |
8901 | ||
8902 | return 0; | |
8903 | } | |
8904 | ||
5c073f26 KKD |
8905 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8906 | int *insn_idx_p) | |
e6ac2450 MKL |
8907 | { |
8908 | const struct btf_type *t, *func, *func_proto, *ptr_type; | |
8909 | struct bpf_reg_state *regs = cur_regs(env); | |
8910 | const char *func_name, *ptr_type_name; | |
9bb00b28 | 8911 | bool sleepable, rcu_lock, rcu_unlock; |
00b85860 | 8912 | struct bpf_kfunc_call_arg_meta meta; |
e6ac2450 | 8913 | u32 i, nargs, func_id, ptr_type_id; |
5c073f26 | 8914 | int err, insn_idx = *insn_idx_p; |
e6ac2450 | 8915 | const struct btf_param *args; |
a35b9af4 | 8916 | const struct btf_type *ret_t; |
2357672c | 8917 | struct btf *desc_btf; |
a4703e31 | 8918 | u32 *kfunc_flags; |
e6ac2450 | 8919 | |
a5d82727 KKD |
8920 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
8921 | if (!insn->imm) | |
8922 | return 0; | |
8923 | ||
43bf0878 | 8924 | desc_btf = find_kfunc_desc_btf(env, insn->off); |
2357672c KKD |
8925 | if (IS_ERR(desc_btf)) |
8926 | return PTR_ERR(desc_btf); | |
8927 | ||
e6ac2450 | 8928 | func_id = insn->imm; |
2357672c KKD |
8929 | func = btf_type_by_id(desc_btf, func_id); |
8930 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
8931 | func_proto = btf_type_by_id(desc_btf, func->type); | |
e6ac2450 | 8932 | |
a4703e31 KKD |
8933 | kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id); |
8934 | if (!kfunc_flags) { | |
e6ac2450 MKL |
8935 | verbose(env, "calling kernel function %s is not allowed\n", |
8936 | func_name); | |
8937 | return -EACCES; | |
8938 | } | |
00b85860 KKD |
8939 | |
8940 | /* Prepare kfunc call metadata */ | |
8941 | memset(&meta, 0, sizeof(meta)); | |
8942 | meta.btf = desc_btf; | |
8943 | meta.func_id = func_id; | |
8944 | meta.kfunc_flags = *kfunc_flags; | |
8945 | meta.func_proto = func_proto; | |
8946 | meta.func_name = func_name; | |
8947 | ||
8948 | if (is_kfunc_destructive(&meta) && !capable(CAP_SYS_BOOT)) { | |
8949 | verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capability\n"); | |
4dd48c6f AS |
8950 | return -EACCES; |
8951 | } | |
8952 | ||
9bb00b28 YS |
8953 | sleepable = is_kfunc_sleepable(&meta); |
8954 | if (sleepable && !env->prog->aux->sleepable) { | |
00b85860 KKD |
8955 | verbose(env, "program must be sleepable to call sleepable kfunc %s\n", func_name); |
8956 | return -EACCES; | |
8957 | } | |
eb1f7f71 | 8958 | |
9bb00b28 YS |
8959 | rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta); |
8960 | rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta); | |
8961 | if ((rcu_lock || rcu_unlock) && !env->rcu_tag_supported) { | |
8962 | verbose(env, "no vmlinux btf rcu tag support for kfunc %s\n", func_name); | |
8963 | return -EACCES; | |
8964 | } | |
8965 | ||
8966 | if (env->cur_state->active_rcu_lock) { | |
8967 | struct bpf_func_state *state; | |
8968 | struct bpf_reg_state *reg; | |
8969 | ||
8970 | if (rcu_lock) { | |
8971 | verbose(env, "nested rcu read lock (kernel function %s)\n", func_name); | |
8972 | return -EINVAL; | |
8973 | } else if (rcu_unlock) { | |
8974 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ | |
8975 | if (reg->type & MEM_RCU) { | |
fca1aa75 | 8976 | reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL); |
9bb00b28 YS |
8977 | reg->type |= PTR_UNTRUSTED; |
8978 | } | |
8979 | })); | |
8980 | env->cur_state->active_rcu_lock = false; | |
8981 | } else if (sleepable) { | |
8982 | verbose(env, "kernel func %s is sleepable within rcu_read_lock region\n", func_name); | |
8983 | return -EACCES; | |
8984 | } | |
8985 | } else if (rcu_lock) { | |
8986 | env->cur_state->active_rcu_lock = true; | |
8987 | } else if (rcu_unlock) { | |
8988 | verbose(env, "unmatched rcu read unlock (kernel function %s)\n", func_name); | |
8989 | return -EINVAL; | |
8990 | } | |
8991 | ||
e6ac2450 | 8992 | /* Check the arguments */ |
00b85860 | 8993 | err = check_kfunc_args(env, &meta); |
5c073f26 | 8994 | if (err < 0) |
e6ac2450 | 8995 | return err; |
5c073f26 | 8996 | /* In case of release function, we get register number of refcounted |
00b85860 | 8997 | * PTR_TO_BTF_ID in bpf_kfunc_arg_meta, do the release now. |
5c073f26 | 8998 | */ |
00b85860 KKD |
8999 | if (meta.release_regno) { |
9000 | err = release_reference(env, regs[meta.release_regno].ref_obj_id); | |
5c073f26 KKD |
9001 | if (err) { |
9002 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
9003 | func_name, func_id); | |
9004 | return err; | |
9005 | } | |
9006 | } | |
e6ac2450 MKL |
9007 | |
9008 | for (i = 0; i < CALLER_SAVED_REGS; i++) | |
9009 | mark_reg_not_init(env, regs, caller_saved[i]); | |
9010 | ||
9011 | /* Check return type */ | |
2357672c | 9012 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
5c073f26 | 9013 | |
00b85860 | 9014 | if (is_kfunc_acquire(&meta) && !btf_type_is_struct_ptr(meta.btf, t)) { |
958cf2e2 KKD |
9015 | /* Only exception is bpf_obj_new_impl */ |
9016 | if (meta.btf != btf_vmlinux || meta.func_id != special_kfunc_list[KF_bpf_obj_new_impl]) { | |
9017 | verbose(env, "acquire kernel function does not return PTR_TO_BTF_ID\n"); | |
9018 | return -EINVAL; | |
9019 | } | |
5c073f26 KKD |
9020 | } |
9021 | ||
e6ac2450 MKL |
9022 | if (btf_type_is_scalar(t)) { |
9023 | mark_reg_unknown(env, regs, BPF_REG_0); | |
9024 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
9025 | } else if (btf_type_is_ptr(t)) { | |
958cf2e2 KKD |
9026 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, &ptr_type_id); |
9027 | ||
9028 | if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) { | |
9029 | if (meta.func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
958cf2e2 KKD |
9030 | struct btf *ret_btf; |
9031 | u32 ret_btf_id; | |
9032 | ||
e181d3f1 KKD |
9033 | if (unlikely(!bpf_global_ma_set)) |
9034 | return -ENOMEM; | |
9035 | ||
958cf2e2 KKD |
9036 | if (((u64)(u32)meta.arg_constant.value) != meta.arg_constant.value) { |
9037 | verbose(env, "local type ID argument must be in range [0, U32_MAX]\n"); | |
9038 | return -EINVAL; | |
9039 | } | |
9040 | ||
9041 | ret_btf = env->prog->aux->btf; | |
9042 | ret_btf_id = meta.arg_constant.value; | |
9043 | ||
9044 | /* This may be NULL due to user not supplying a BTF */ | |
9045 | if (!ret_btf) { | |
9046 | verbose(env, "bpf_obj_new requires prog BTF\n"); | |
9047 | return -EINVAL; | |
9048 | } | |
9049 | ||
9050 | ret_t = btf_type_by_id(ret_btf, ret_btf_id); | |
9051 | if (!ret_t || !__btf_type_is_struct(ret_t)) { | |
9052 | verbose(env, "bpf_obj_new type ID argument must be of a struct\n"); | |
9053 | return -EINVAL; | |
9054 | } | |
9055 | ||
9056 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
9057 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
9058 | regs[BPF_REG_0].btf = ret_btf; | |
9059 | regs[BPF_REG_0].btf_id = ret_btf_id; | |
9060 | ||
9061 | env->insn_aux_data[insn_idx].obj_new_size = ret_t->size; | |
9062 | env->insn_aux_data[insn_idx].kptr_struct_meta = | |
9063 | btf_find_struct_meta(ret_btf, ret_btf_id); | |
ac9f0605 KKD |
9064 | } else if (meta.func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { |
9065 | env->insn_aux_data[insn_idx].kptr_struct_meta = | |
9066 | btf_find_struct_meta(meta.arg_obj_drop.btf, | |
9067 | meta.arg_obj_drop.btf_id); | |
8cab76ec KKD |
9068 | } else if (meta.func_id == special_kfunc_list[KF_bpf_list_pop_front] || |
9069 | meta.func_id == special_kfunc_list[KF_bpf_list_pop_back]) { | |
9070 | struct btf_field *field = meta.arg_list_head.field; | |
9071 | ||
9072 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
9073 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
9074 | regs[BPF_REG_0].btf = field->list_head.btf; | |
9075 | regs[BPF_REG_0].btf_id = field->list_head.value_btf_id; | |
9076 | regs[BPF_REG_0].off = field->list_head.node_offset; | |
fd264ca0 YS |
9077 | } else if (meta.func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) { |
9078 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
9079 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED; | |
9080 | regs[BPF_REG_0].btf = desc_btf; | |
9081 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; | |
a35b9af4 YS |
9082 | } else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { |
9083 | ret_t = btf_type_by_id(desc_btf, meta.arg_constant.value); | |
9084 | if (!ret_t || !btf_type_is_struct(ret_t)) { | |
9085 | verbose(env, | |
9086 | "kfunc bpf_rdonly_cast type ID argument must be of a struct\n"); | |
9087 | return -EINVAL; | |
9088 | } | |
9089 | ||
9090 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
9091 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_UNTRUSTED; | |
9092 | regs[BPF_REG_0].btf = desc_btf; | |
9093 | regs[BPF_REG_0].btf_id = meta.arg_constant.value; | |
958cf2e2 KKD |
9094 | } else { |
9095 | verbose(env, "kernel function %s unhandled dynamic return type\n", | |
9096 | meta.func_name); | |
9097 | return -EFAULT; | |
9098 | } | |
9099 | } else if (!__btf_type_is_struct(ptr_type)) { | |
eb1f7f71 BT |
9100 | if (!meta.r0_size) { |
9101 | ptr_type_name = btf_name_by_offset(desc_btf, | |
9102 | ptr_type->name_off); | |
9103 | verbose(env, | |
9104 | "kernel function %s returns pointer type %s %s is not supported\n", | |
9105 | func_name, | |
9106 | btf_type_str(ptr_type), | |
9107 | ptr_type_name); | |
9108 | return -EINVAL; | |
9109 | } | |
9110 | ||
9111 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
9112 | regs[BPF_REG_0].type = PTR_TO_MEM; | |
9113 | regs[BPF_REG_0].mem_size = meta.r0_size; | |
9114 | ||
9115 | if (meta.r0_rdonly) | |
9116 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
9117 | ||
9118 | /* Ensures we don't access the memory after a release_reference() */ | |
9119 | if (meta.ref_obj_id) | |
9120 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
9121 | } else { | |
9122 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
9123 | regs[BPF_REG_0].btf = desc_btf; | |
9124 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; | |
9125 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
e6ac2450 | 9126 | } |
958cf2e2 | 9127 | |
00b85860 | 9128 | if (is_kfunc_ret_null(&meta)) { |
5c073f26 KKD |
9129 | regs[BPF_REG_0].type |= PTR_MAYBE_NULL; |
9130 | /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ | |
9131 | regs[BPF_REG_0].id = ++env->id_gen; | |
9132 | } | |
e6ac2450 | 9133 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
00b85860 | 9134 | if (is_kfunc_acquire(&meta)) { |
5c073f26 KKD |
9135 | int id = acquire_reference_state(env, insn_idx); |
9136 | ||
9137 | if (id < 0) | |
9138 | return id; | |
00b85860 KKD |
9139 | if (is_kfunc_ret_null(&meta)) |
9140 | regs[BPF_REG_0].id = id; | |
5c073f26 KKD |
9141 | regs[BPF_REG_0].ref_obj_id = id; |
9142 | } | |
00b85860 KKD |
9143 | if (reg_may_point_to_spin_lock(®s[BPF_REG_0]) && !regs[BPF_REG_0].id) |
9144 | regs[BPF_REG_0].id = ++env->id_gen; | |
e6ac2450 MKL |
9145 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ |
9146 | ||
9147 | nargs = btf_type_vlen(func_proto); | |
9148 | args = (const struct btf_param *)(func_proto + 1); | |
9149 | for (i = 0; i < nargs; i++) { | |
9150 | u32 regno = i + 1; | |
9151 | ||
2357672c | 9152 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
9153 | if (btf_type_is_ptr(t)) |
9154 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
9155 | else | |
9156 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
9157 | mark_btf_func_reg_size(env, regno, t->size); | |
9158 | } | |
9159 | ||
9160 | return 0; | |
9161 | } | |
9162 | ||
b03c9f9f EC |
9163 | static bool signed_add_overflows(s64 a, s64 b) |
9164 | { | |
9165 | /* Do the add in u64, where overflow is well-defined */ | |
9166 | s64 res = (s64)((u64)a + (u64)b); | |
9167 | ||
9168 | if (b < 0) | |
9169 | return res > a; | |
9170 | return res < a; | |
9171 | } | |
9172 | ||
bc895e8b | 9173 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
9174 | { |
9175 | /* Do the add in u32, where overflow is well-defined */ | |
9176 | s32 res = (s32)((u32)a + (u32)b); | |
9177 | ||
9178 | if (b < 0) | |
9179 | return res > a; | |
9180 | return res < a; | |
9181 | } | |
9182 | ||
bc895e8b | 9183 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
9184 | { |
9185 | /* Do the sub in u64, where overflow is well-defined */ | |
9186 | s64 res = (s64)((u64)a - (u64)b); | |
9187 | ||
9188 | if (b < 0) | |
9189 | return res < a; | |
9190 | return res > a; | |
969bf05e AS |
9191 | } |
9192 | ||
3f50f132 JF |
9193 | static bool signed_sub32_overflows(s32 a, s32 b) |
9194 | { | |
bc895e8b | 9195 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
9196 | s32 res = (s32)((u32)a - (u32)b); |
9197 | ||
9198 | if (b < 0) | |
9199 | return res < a; | |
9200 | return res > a; | |
9201 | } | |
9202 | ||
bb7f0f98 AS |
9203 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
9204 | const struct bpf_reg_state *reg, | |
9205 | enum bpf_reg_type type) | |
9206 | { | |
9207 | bool known = tnum_is_const(reg->var_off); | |
9208 | s64 val = reg->var_off.value; | |
9209 | s64 smin = reg->smin_value; | |
9210 | ||
9211 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
9212 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
c25b2ae1 | 9213 | reg_type_str(env, type), val); |
bb7f0f98 AS |
9214 | return false; |
9215 | } | |
9216 | ||
9217 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
9218 | verbose(env, "%s pointer offset %d is not allowed\n", | |
c25b2ae1 | 9219 | reg_type_str(env, type), reg->off); |
bb7f0f98 AS |
9220 | return false; |
9221 | } | |
9222 | ||
9223 | if (smin == S64_MIN) { | |
9224 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
c25b2ae1 | 9225 | reg_type_str(env, type)); |
bb7f0f98 AS |
9226 | return false; |
9227 | } | |
9228 | ||
9229 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
9230 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
c25b2ae1 | 9231 | smin, reg_type_str(env, type)); |
bb7f0f98 AS |
9232 | return false; |
9233 | } | |
9234 | ||
9235 | return true; | |
9236 | } | |
9237 | ||
a6aaece0 DB |
9238 | enum { |
9239 | REASON_BOUNDS = -1, | |
9240 | REASON_TYPE = -2, | |
9241 | REASON_PATHS = -3, | |
9242 | REASON_LIMIT = -4, | |
9243 | REASON_STACK = -5, | |
9244 | }; | |
9245 | ||
979d63d5 | 9246 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 9247 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 9248 | { |
7fedb63a | 9249 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
9250 | |
9251 | switch (ptr_reg->type) { | |
9252 | case PTR_TO_STACK: | |
1b1597e6 | 9253 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
9254 | * left direction, see BPF_REG_FP. Also, unknown scalar |
9255 | * offset where we would need to deal with min/max bounds is | |
9256 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
9257 | */ |
9258 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 9259 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 9260 | break; |
979d63d5 | 9261 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 9262 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
9263 | ptr_limit = (mask_to_left ? |
9264 | ptr_reg->smin_value : | |
9265 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 9266 | break; |
979d63d5 | 9267 | default: |
a6aaece0 | 9268 | return REASON_TYPE; |
979d63d5 | 9269 | } |
b658bbb8 DB |
9270 | |
9271 | if (ptr_limit >= max) | |
a6aaece0 | 9272 | return REASON_LIMIT; |
b658bbb8 DB |
9273 | *alu_limit = ptr_limit; |
9274 | return 0; | |
979d63d5 DB |
9275 | } |
9276 | ||
d3bd7413 DB |
9277 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
9278 | const struct bpf_insn *insn) | |
9279 | { | |
2c78ee89 | 9280 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
9281 | } |
9282 | ||
9283 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
9284 | u32 alu_state, u32 alu_limit) | |
9285 | { | |
9286 | /* If we arrived here from different branches with different | |
9287 | * state or limits to sanitize, then this won't work. | |
9288 | */ | |
9289 | if (aux->alu_state && | |
9290 | (aux->alu_state != alu_state || | |
9291 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 9292 | return REASON_PATHS; |
d3bd7413 | 9293 | |
e6ac5933 | 9294 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
9295 | aux->alu_state = alu_state; |
9296 | aux->alu_limit = alu_limit; | |
9297 | return 0; | |
9298 | } | |
9299 | ||
9300 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
9301 | struct bpf_insn *insn) | |
9302 | { | |
9303 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
9304 | ||
9305 | if (can_skip_alu_sanitation(env, insn)) | |
9306 | return 0; | |
9307 | ||
9308 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
9309 | } | |
9310 | ||
f5288193 DB |
9311 | static bool sanitize_needed(u8 opcode) |
9312 | { | |
9313 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
9314 | } | |
9315 | ||
3d0220f6 DB |
9316 | struct bpf_sanitize_info { |
9317 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 9318 | bool mask_to_left; |
3d0220f6 DB |
9319 | }; |
9320 | ||
9183671a DB |
9321 | static struct bpf_verifier_state * |
9322 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
9323 | const struct bpf_insn *insn, | |
9324 | u32 next_idx, u32 curr_idx) | |
9325 | { | |
9326 | struct bpf_verifier_state *branch; | |
9327 | struct bpf_reg_state *regs; | |
9328 | ||
9329 | branch = push_stack(env, next_idx, curr_idx, true); | |
9330 | if (branch && insn) { | |
9331 | regs = branch->frame[branch->curframe]->regs; | |
9332 | if (BPF_SRC(insn->code) == BPF_K) { | |
9333 | mark_reg_unknown(env, regs, insn->dst_reg); | |
9334 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
9335 | mark_reg_unknown(env, regs, insn->dst_reg); | |
9336 | mark_reg_unknown(env, regs, insn->src_reg); | |
9337 | } | |
9338 | } | |
9339 | return branch; | |
9340 | } | |
9341 | ||
979d63d5 DB |
9342 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
9343 | struct bpf_insn *insn, | |
9344 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 9345 | const struct bpf_reg_state *off_reg, |
979d63d5 | 9346 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 9347 | struct bpf_sanitize_info *info, |
7fedb63a | 9348 | const bool commit_window) |
979d63d5 | 9349 | { |
3d0220f6 | 9350 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 9351 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 9352 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 9353 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
9354 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
9355 | u8 opcode = BPF_OP(insn->code); | |
9356 | u32 alu_state, alu_limit; | |
9357 | struct bpf_reg_state tmp; | |
9358 | bool ret; | |
f232326f | 9359 | int err; |
979d63d5 | 9360 | |
d3bd7413 | 9361 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
9362 | return 0; |
9363 | ||
9364 | /* We already marked aux for masking from non-speculative | |
9365 | * paths, thus we got here in the first place. We only care | |
9366 | * to explore bad access from here. | |
9367 | */ | |
9368 | if (vstate->speculative) | |
9369 | goto do_sim; | |
9370 | ||
bb01a1bb DB |
9371 | if (!commit_window) { |
9372 | if (!tnum_is_const(off_reg->var_off) && | |
9373 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
9374 | return REASON_BOUNDS; | |
9375 | ||
9376 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
9377 | (opcode == BPF_SUB && !off_is_neg); | |
9378 | } | |
9379 | ||
9380 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
9381 | if (err < 0) |
9382 | return err; | |
9383 | ||
7fedb63a DB |
9384 | if (commit_window) { |
9385 | /* In commit phase we narrow the masking window based on | |
9386 | * the observed pointer move after the simulated operation. | |
9387 | */ | |
3d0220f6 DB |
9388 | alu_state = info->aux.alu_state; |
9389 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
9390 | } else { |
9391 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 9392 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
9393 | alu_state |= ptr_is_dst_reg ? |
9394 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
9395 | |
9396 | /* Limit pruning on unknown scalars to enable deep search for | |
9397 | * potential masking differences from other program paths. | |
9398 | */ | |
9399 | if (!off_is_imm) | |
9400 | env->explore_alu_limits = true; | |
7fedb63a DB |
9401 | } |
9402 | ||
f232326f PK |
9403 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
9404 | if (err < 0) | |
9405 | return err; | |
979d63d5 | 9406 | do_sim: |
7fedb63a DB |
9407 | /* If we're in commit phase, we're done here given we already |
9408 | * pushed the truncated dst_reg into the speculative verification | |
9409 | * stack. | |
a7036191 DB |
9410 | * |
9411 | * Also, when register is a known constant, we rewrite register-based | |
9412 | * operation to immediate-based, and thus do not need masking (and as | |
9413 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 9414 | */ |
a7036191 | 9415 | if (commit_window || off_is_imm) |
7fedb63a DB |
9416 | return 0; |
9417 | ||
979d63d5 DB |
9418 | /* Simulate and find potential out-of-bounds access under |
9419 | * speculative execution from truncation as a result of | |
9420 | * masking when off was not within expected range. If off | |
9421 | * sits in dst, then we temporarily need to move ptr there | |
9422 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
9423 | * for cases where we use K-based arithmetic in one direction | |
9424 | * and truncated reg-based in the other in order to explore | |
9425 | * bad access. | |
9426 | */ | |
9427 | if (!ptr_is_dst_reg) { | |
9428 | tmp = *dst_reg; | |
9429 | *dst_reg = *ptr_reg; | |
9430 | } | |
9183671a DB |
9431 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
9432 | env->insn_idx); | |
0803278b | 9433 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 9434 | *dst_reg = tmp; |
a6aaece0 DB |
9435 | return !ret ? REASON_STACK : 0; |
9436 | } | |
9437 | ||
fe9a5ca7 DB |
9438 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
9439 | { | |
9440 | struct bpf_verifier_state *vstate = env->cur_state; | |
9441 | ||
9442 | /* If we simulate paths under speculation, we don't update the | |
9443 | * insn as 'seen' such that when we verify unreachable paths in | |
9444 | * the non-speculative domain, sanitize_dead_code() can still | |
9445 | * rewrite/sanitize them. | |
9446 | */ | |
9447 | if (!vstate->speculative) | |
9448 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
9449 | } | |
9450 | ||
a6aaece0 DB |
9451 | static int sanitize_err(struct bpf_verifier_env *env, |
9452 | const struct bpf_insn *insn, int reason, | |
9453 | const struct bpf_reg_state *off_reg, | |
9454 | const struct bpf_reg_state *dst_reg) | |
9455 | { | |
9456 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
9457 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
9458 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
9459 | ||
9460 | switch (reason) { | |
9461 | case REASON_BOUNDS: | |
9462 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
9463 | off_reg == dst_reg ? dst : src, err); | |
9464 | break; | |
9465 | case REASON_TYPE: | |
9466 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
9467 | off_reg == dst_reg ? src : dst, err); | |
9468 | break; | |
9469 | case REASON_PATHS: | |
9470 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
9471 | dst, op, err); | |
9472 | break; | |
9473 | case REASON_LIMIT: | |
9474 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
9475 | dst, op, err); | |
9476 | break; | |
9477 | case REASON_STACK: | |
9478 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
9479 | dst, err); | |
9480 | break; | |
9481 | default: | |
9482 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
9483 | reason); | |
9484 | break; | |
9485 | } | |
9486 | ||
9487 | return -EACCES; | |
979d63d5 DB |
9488 | } |
9489 | ||
01f810ac AM |
9490 | /* check that stack access falls within stack limits and that 'reg' doesn't |
9491 | * have a variable offset. | |
9492 | * | |
9493 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
9494 | * requires corresponding support in Spectre masking for stack ALU. See also | |
9495 | * retrieve_ptr_limit(). | |
9496 | * | |
9497 | * | |
9498 | * 'off' includes 'reg->off'. | |
9499 | */ | |
9500 | static int check_stack_access_for_ptr_arithmetic( | |
9501 | struct bpf_verifier_env *env, | |
9502 | int regno, | |
9503 | const struct bpf_reg_state *reg, | |
9504 | int off) | |
9505 | { | |
9506 | if (!tnum_is_const(reg->var_off)) { | |
9507 | char tn_buf[48]; | |
9508 | ||
9509 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
9510 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
9511 | regno, tn_buf, off); | |
9512 | return -EACCES; | |
9513 | } | |
9514 | ||
9515 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
9516 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
9517 | "prohibited for !root; off=%d\n", regno, off); | |
9518 | return -EACCES; | |
9519 | } | |
9520 | ||
9521 | return 0; | |
9522 | } | |
9523 | ||
073815b7 DB |
9524 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
9525 | const struct bpf_insn *insn, | |
9526 | const struct bpf_reg_state *dst_reg) | |
9527 | { | |
9528 | u32 dst = insn->dst_reg; | |
9529 | ||
9530 | /* For unprivileged we require that resulting offset must be in bounds | |
9531 | * in order to be able to sanitize access later on. | |
9532 | */ | |
9533 | if (env->bypass_spec_v1) | |
9534 | return 0; | |
9535 | ||
9536 | switch (dst_reg->type) { | |
9537 | case PTR_TO_STACK: | |
9538 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
9539 | dst_reg->off + dst_reg->var_off.value)) | |
9540 | return -EACCES; | |
9541 | break; | |
9542 | case PTR_TO_MAP_VALUE: | |
61df10c7 | 9543 | if (check_map_access(env, dst, dst_reg->off, 1, false, ACCESS_HELPER)) { |
073815b7 DB |
9544 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
9545 | "prohibited for !root\n", dst); | |
9546 | return -EACCES; | |
9547 | } | |
9548 | break; | |
9549 | default: | |
9550 | break; | |
9551 | } | |
9552 | ||
9553 | return 0; | |
9554 | } | |
01f810ac | 9555 | |
f1174f77 | 9556 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
9557 | * Caller should also handle BPF_MOV case separately. |
9558 | * If we return -EACCES, caller may want to try again treating pointer as a | |
9559 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
9560 | */ | |
9561 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
9562 | struct bpf_insn *insn, | |
9563 | const struct bpf_reg_state *ptr_reg, | |
9564 | const struct bpf_reg_state *off_reg) | |
969bf05e | 9565 | { |
f4d7e40a AS |
9566 | struct bpf_verifier_state *vstate = env->cur_state; |
9567 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
9568 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 9569 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
9570 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
9571 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
9572 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
9573 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 9574 | struct bpf_sanitize_info info = {}; |
969bf05e | 9575 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 9576 | u32 dst = insn->dst_reg; |
979d63d5 | 9577 | int ret; |
969bf05e | 9578 | |
f1174f77 | 9579 | dst_reg = ®s[dst]; |
969bf05e | 9580 | |
6f16101e DB |
9581 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
9582 | smin_val > smax_val || umin_val > umax_val) { | |
9583 | /* Taint dst register if offset had invalid bounds derived from | |
9584 | * e.g. dead branches. | |
9585 | */ | |
f54c7898 | 9586 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 9587 | return 0; |
f1174f77 EC |
9588 | } |
9589 | ||
9590 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
9591 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
9592 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
9593 | __mark_reg_unknown(env, dst_reg); | |
9594 | return 0; | |
9595 | } | |
9596 | ||
82abbf8d AS |
9597 | verbose(env, |
9598 | "R%d 32-bit pointer arithmetic prohibited\n", | |
9599 | dst); | |
f1174f77 | 9600 | return -EACCES; |
969bf05e AS |
9601 | } |
9602 | ||
c25b2ae1 | 9603 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
aad2eeaf | 9604 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
c25b2ae1 | 9605 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 9606 | return -EACCES; |
c25b2ae1 HL |
9607 | } |
9608 | ||
9609 | switch (base_type(ptr_reg->type)) { | |
aad2eeaf | 9610 | case CONST_PTR_TO_MAP: |
7c696732 YS |
9611 | /* smin_val represents the known value */ |
9612 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
9613 | break; | |
8731745e | 9614 | fallthrough; |
aad2eeaf | 9615 | case PTR_TO_PACKET_END: |
c64b7983 | 9616 | case PTR_TO_SOCKET: |
46f8bc92 | 9617 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 9618 | case PTR_TO_TCP_SOCK: |
fada7fdc | 9619 | case PTR_TO_XDP_SOCK: |
aad2eeaf | 9620 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
c25b2ae1 | 9621 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 9622 | return -EACCES; |
aad2eeaf JS |
9623 | default: |
9624 | break; | |
f1174f77 EC |
9625 | } |
9626 | ||
9627 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
9628 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 9629 | */ |
f1174f77 EC |
9630 | dst_reg->type = ptr_reg->type; |
9631 | dst_reg->id = ptr_reg->id; | |
969bf05e | 9632 | |
bb7f0f98 AS |
9633 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
9634 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
9635 | return -EINVAL; | |
9636 | ||
3f50f132 JF |
9637 | /* pointer types do not carry 32-bit bounds at the moment. */ |
9638 | __mark_reg32_unbounded(dst_reg); | |
9639 | ||
7fedb63a DB |
9640 | if (sanitize_needed(opcode)) { |
9641 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 9642 | &info, false); |
a6aaece0 DB |
9643 | if (ret < 0) |
9644 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 9645 | } |
a6aaece0 | 9646 | |
f1174f77 EC |
9647 | switch (opcode) { |
9648 | case BPF_ADD: | |
9649 | /* We can take a fixed offset as long as it doesn't overflow | |
9650 | * the s32 'off' field | |
969bf05e | 9651 | */ |
b03c9f9f EC |
9652 | if (known && (ptr_reg->off + smin_val == |
9653 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 9654 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
9655 | dst_reg->smin_value = smin_ptr; |
9656 | dst_reg->smax_value = smax_ptr; | |
9657 | dst_reg->umin_value = umin_ptr; | |
9658 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 9659 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 9660 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 9661 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
9662 | break; |
9663 | } | |
f1174f77 EC |
9664 | /* A new variable offset is created. Note that off_reg->off |
9665 | * == 0, since it's a scalar. | |
9666 | * dst_reg gets the pointer type and since some positive | |
9667 | * integer value was added to the pointer, give it a new 'id' | |
9668 | * if it's a PTR_TO_PACKET. | |
9669 | * this creates a new 'base' pointer, off_reg (variable) gets | |
9670 | * added into the variable offset, and we copy the fixed offset | |
9671 | * from ptr_reg. | |
969bf05e | 9672 | */ |
b03c9f9f EC |
9673 | if (signed_add_overflows(smin_ptr, smin_val) || |
9674 | signed_add_overflows(smax_ptr, smax_val)) { | |
9675 | dst_reg->smin_value = S64_MIN; | |
9676 | dst_reg->smax_value = S64_MAX; | |
9677 | } else { | |
9678 | dst_reg->smin_value = smin_ptr + smin_val; | |
9679 | dst_reg->smax_value = smax_ptr + smax_val; | |
9680 | } | |
9681 | if (umin_ptr + umin_val < umin_ptr || | |
9682 | umax_ptr + umax_val < umax_ptr) { | |
9683 | dst_reg->umin_value = 0; | |
9684 | dst_reg->umax_value = U64_MAX; | |
9685 | } else { | |
9686 | dst_reg->umin_value = umin_ptr + umin_val; | |
9687 | dst_reg->umax_value = umax_ptr + umax_val; | |
9688 | } | |
f1174f77 EC |
9689 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
9690 | dst_reg->off = ptr_reg->off; | |
0962590e | 9691 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 9692 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
9693 | dst_reg->id = ++env->id_gen; |
9694 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 9695 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
9696 | } |
9697 | break; | |
9698 | case BPF_SUB: | |
9699 | if (dst_reg == off_reg) { | |
9700 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
9701 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
9702 | dst); | |
f1174f77 EC |
9703 | return -EACCES; |
9704 | } | |
9705 | /* We don't allow subtraction from FP, because (according to | |
9706 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
9707 | * be able to deal with it. | |
969bf05e | 9708 | */ |
f1174f77 | 9709 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
9710 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
9711 | dst); | |
f1174f77 EC |
9712 | return -EACCES; |
9713 | } | |
b03c9f9f EC |
9714 | if (known && (ptr_reg->off - smin_val == |
9715 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 9716 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
9717 | dst_reg->smin_value = smin_ptr; |
9718 | dst_reg->smax_value = smax_ptr; | |
9719 | dst_reg->umin_value = umin_ptr; | |
9720 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
9721 | dst_reg->var_off = ptr_reg->var_off; |
9722 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 9723 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 9724 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
9725 | break; |
9726 | } | |
f1174f77 EC |
9727 | /* A new variable offset is created. If the subtrahend is known |
9728 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 9729 | */ |
b03c9f9f EC |
9730 | if (signed_sub_overflows(smin_ptr, smax_val) || |
9731 | signed_sub_overflows(smax_ptr, smin_val)) { | |
9732 | /* Overflow possible, we know nothing */ | |
9733 | dst_reg->smin_value = S64_MIN; | |
9734 | dst_reg->smax_value = S64_MAX; | |
9735 | } else { | |
9736 | dst_reg->smin_value = smin_ptr - smax_val; | |
9737 | dst_reg->smax_value = smax_ptr - smin_val; | |
9738 | } | |
9739 | if (umin_ptr < umax_val) { | |
9740 | /* Overflow possible, we know nothing */ | |
9741 | dst_reg->umin_value = 0; | |
9742 | dst_reg->umax_value = U64_MAX; | |
9743 | } else { | |
9744 | /* Cannot overflow (as long as bounds are consistent) */ | |
9745 | dst_reg->umin_value = umin_ptr - umax_val; | |
9746 | dst_reg->umax_value = umax_ptr - umin_val; | |
9747 | } | |
f1174f77 EC |
9748 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
9749 | dst_reg->off = ptr_reg->off; | |
0962590e | 9750 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 9751 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
9752 | dst_reg->id = ++env->id_gen; |
9753 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 9754 | if (smin_val < 0) |
22dc4a0f | 9755 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 9756 | } |
f1174f77 EC |
9757 | break; |
9758 | case BPF_AND: | |
9759 | case BPF_OR: | |
9760 | case BPF_XOR: | |
82abbf8d AS |
9761 | /* bitwise ops on pointers are troublesome, prohibit. */ |
9762 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
9763 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
9764 | return -EACCES; |
9765 | default: | |
9766 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
9767 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
9768 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 9769 | return -EACCES; |
43188702 JF |
9770 | } |
9771 | ||
bb7f0f98 AS |
9772 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
9773 | return -EINVAL; | |
3844d153 | 9774 | reg_bounds_sync(dst_reg); |
073815b7 DB |
9775 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
9776 | return -EACCES; | |
7fedb63a DB |
9777 | if (sanitize_needed(opcode)) { |
9778 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 9779 | &info, true); |
7fedb63a DB |
9780 | if (ret < 0) |
9781 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
9782 | } |
9783 | ||
43188702 JF |
9784 | return 0; |
9785 | } | |
9786 | ||
3f50f132 JF |
9787 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
9788 | struct bpf_reg_state *src_reg) | |
9789 | { | |
9790 | s32 smin_val = src_reg->s32_min_value; | |
9791 | s32 smax_val = src_reg->s32_max_value; | |
9792 | u32 umin_val = src_reg->u32_min_value; | |
9793 | u32 umax_val = src_reg->u32_max_value; | |
9794 | ||
9795 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
9796 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
9797 | dst_reg->s32_min_value = S32_MIN; | |
9798 | dst_reg->s32_max_value = S32_MAX; | |
9799 | } else { | |
9800 | dst_reg->s32_min_value += smin_val; | |
9801 | dst_reg->s32_max_value += smax_val; | |
9802 | } | |
9803 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
9804 | dst_reg->u32_max_value + umax_val < umax_val) { | |
9805 | dst_reg->u32_min_value = 0; | |
9806 | dst_reg->u32_max_value = U32_MAX; | |
9807 | } else { | |
9808 | dst_reg->u32_min_value += umin_val; | |
9809 | dst_reg->u32_max_value += umax_val; | |
9810 | } | |
9811 | } | |
9812 | ||
07cd2631 JF |
9813 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
9814 | struct bpf_reg_state *src_reg) | |
9815 | { | |
9816 | s64 smin_val = src_reg->smin_value; | |
9817 | s64 smax_val = src_reg->smax_value; | |
9818 | u64 umin_val = src_reg->umin_value; | |
9819 | u64 umax_val = src_reg->umax_value; | |
9820 | ||
9821 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
9822 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
9823 | dst_reg->smin_value = S64_MIN; | |
9824 | dst_reg->smax_value = S64_MAX; | |
9825 | } else { | |
9826 | dst_reg->smin_value += smin_val; | |
9827 | dst_reg->smax_value += smax_val; | |
9828 | } | |
9829 | if (dst_reg->umin_value + umin_val < umin_val || | |
9830 | dst_reg->umax_value + umax_val < umax_val) { | |
9831 | dst_reg->umin_value = 0; | |
9832 | dst_reg->umax_value = U64_MAX; | |
9833 | } else { | |
9834 | dst_reg->umin_value += umin_val; | |
9835 | dst_reg->umax_value += umax_val; | |
9836 | } | |
3f50f132 JF |
9837 | } |
9838 | ||
9839 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
9840 | struct bpf_reg_state *src_reg) | |
9841 | { | |
9842 | s32 smin_val = src_reg->s32_min_value; | |
9843 | s32 smax_val = src_reg->s32_max_value; | |
9844 | u32 umin_val = src_reg->u32_min_value; | |
9845 | u32 umax_val = src_reg->u32_max_value; | |
9846 | ||
9847 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
9848 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
9849 | /* Overflow possible, we know nothing */ | |
9850 | dst_reg->s32_min_value = S32_MIN; | |
9851 | dst_reg->s32_max_value = S32_MAX; | |
9852 | } else { | |
9853 | dst_reg->s32_min_value -= smax_val; | |
9854 | dst_reg->s32_max_value -= smin_val; | |
9855 | } | |
9856 | if (dst_reg->u32_min_value < umax_val) { | |
9857 | /* Overflow possible, we know nothing */ | |
9858 | dst_reg->u32_min_value = 0; | |
9859 | dst_reg->u32_max_value = U32_MAX; | |
9860 | } else { | |
9861 | /* Cannot overflow (as long as bounds are consistent) */ | |
9862 | dst_reg->u32_min_value -= umax_val; | |
9863 | dst_reg->u32_max_value -= umin_val; | |
9864 | } | |
07cd2631 JF |
9865 | } |
9866 | ||
9867 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
9868 | struct bpf_reg_state *src_reg) | |
9869 | { | |
9870 | s64 smin_val = src_reg->smin_value; | |
9871 | s64 smax_val = src_reg->smax_value; | |
9872 | u64 umin_val = src_reg->umin_value; | |
9873 | u64 umax_val = src_reg->umax_value; | |
9874 | ||
9875 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
9876 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
9877 | /* Overflow possible, we know nothing */ | |
9878 | dst_reg->smin_value = S64_MIN; | |
9879 | dst_reg->smax_value = S64_MAX; | |
9880 | } else { | |
9881 | dst_reg->smin_value -= smax_val; | |
9882 | dst_reg->smax_value -= smin_val; | |
9883 | } | |
9884 | if (dst_reg->umin_value < umax_val) { | |
9885 | /* Overflow possible, we know nothing */ | |
9886 | dst_reg->umin_value = 0; | |
9887 | dst_reg->umax_value = U64_MAX; | |
9888 | } else { | |
9889 | /* Cannot overflow (as long as bounds are consistent) */ | |
9890 | dst_reg->umin_value -= umax_val; | |
9891 | dst_reg->umax_value -= umin_val; | |
9892 | } | |
3f50f132 JF |
9893 | } |
9894 | ||
9895 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
9896 | struct bpf_reg_state *src_reg) | |
9897 | { | |
9898 | s32 smin_val = src_reg->s32_min_value; | |
9899 | u32 umin_val = src_reg->u32_min_value; | |
9900 | u32 umax_val = src_reg->u32_max_value; | |
9901 | ||
9902 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
9903 | /* Ain't nobody got time to multiply that sign */ | |
9904 | __mark_reg32_unbounded(dst_reg); | |
9905 | return; | |
9906 | } | |
9907 | /* Both values are positive, so we can work with unsigned and | |
9908 | * copy the result to signed (unless it exceeds S32_MAX). | |
9909 | */ | |
9910 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
9911 | /* Potential overflow, we know nothing */ | |
9912 | __mark_reg32_unbounded(dst_reg); | |
9913 | return; | |
9914 | } | |
9915 | dst_reg->u32_min_value *= umin_val; | |
9916 | dst_reg->u32_max_value *= umax_val; | |
9917 | if (dst_reg->u32_max_value > S32_MAX) { | |
9918 | /* Overflow possible, we know nothing */ | |
9919 | dst_reg->s32_min_value = S32_MIN; | |
9920 | dst_reg->s32_max_value = S32_MAX; | |
9921 | } else { | |
9922 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
9923 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
9924 | } | |
07cd2631 JF |
9925 | } |
9926 | ||
9927 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
9928 | struct bpf_reg_state *src_reg) | |
9929 | { | |
9930 | s64 smin_val = src_reg->smin_value; | |
9931 | u64 umin_val = src_reg->umin_value; | |
9932 | u64 umax_val = src_reg->umax_value; | |
9933 | ||
07cd2631 JF |
9934 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
9935 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 9936 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
9937 | return; |
9938 | } | |
9939 | /* Both values are positive, so we can work with unsigned and | |
9940 | * copy the result to signed (unless it exceeds S64_MAX). | |
9941 | */ | |
9942 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
9943 | /* Potential overflow, we know nothing */ | |
3f50f132 | 9944 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
9945 | return; |
9946 | } | |
9947 | dst_reg->umin_value *= umin_val; | |
9948 | dst_reg->umax_value *= umax_val; | |
9949 | if (dst_reg->umax_value > S64_MAX) { | |
9950 | /* Overflow possible, we know nothing */ | |
9951 | dst_reg->smin_value = S64_MIN; | |
9952 | dst_reg->smax_value = S64_MAX; | |
9953 | } else { | |
9954 | dst_reg->smin_value = dst_reg->umin_value; | |
9955 | dst_reg->smax_value = dst_reg->umax_value; | |
9956 | } | |
9957 | } | |
9958 | ||
3f50f132 JF |
9959 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
9960 | struct bpf_reg_state *src_reg) | |
9961 | { | |
9962 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
9963 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
9964 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
9965 | s32 smin_val = src_reg->s32_min_value; | |
9966 | u32 umax_val = src_reg->u32_max_value; | |
9967 | ||
049c4e13 DB |
9968 | if (src_known && dst_known) { |
9969 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 9970 | return; |
049c4e13 | 9971 | } |
3f50f132 JF |
9972 | |
9973 | /* We get our minimum from the var_off, since that's inherently | |
9974 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
9975 | */ | |
9976 | dst_reg->u32_min_value = var32_off.value; | |
9977 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
9978 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
9979 | /* Lose signed bounds when ANDing negative numbers, | |
9980 | * ain't nobody got time for that. | |
9981 | */ | |
9982 | dst_reg->s32_min_value = S32_MIN; | |
9983 | dst_reg->s32_max_value = S32_MAX; | |
9984 | } else { | |
9985 | /* ANDing two positives gives a positive, so safe to | |
9986 | * cast result into s64. | |
9987 | */ | |
9988 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
9989 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
9990 | } | |
3f50f132 JF |
9991 | } |
9992 | ||
07cd2631 JF |
9993 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
9994 | struct bpf_reg_state *src_reg) | |
9995 | { | |
3f50f132 JF |
9996 | bool src_known = tnum_is_const(src_reg->var_off); |
9997 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
9998 | s64 smin_val = src_reg->smin_value; |
9999 | u64 umax_val = src_reg->umax_value; | |
10000 | ||
3f50f132 | 10001 | if (src_known && dst_known) { |
4fbb38a3 | 10002 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
10003 | return; |
10004 | } | |
10005 | ||
07cd2631 JF |
10006 | /* We get our minimum from the var_off, since that's inherently |
10007 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
10008 | */ | |
07cd2631 JF |
10009 | dst_reg->umin_value = dst_reg->var_off.value; |
10010 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
10011 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
10012 | /* Lose signed bounds when ANDing negative numbers, | |
10013 | * ain't nobody got time for that. | |
10014 | */ | |
10015 | dst_reg->smin_value = S64_MIN; | |
10016 | dst_reg->smax_value = S64_MAX; | |
10017 | } else { | |
10018 | /* ANDing two positives gives a positive, so safe to | |
10019 | * cast result into s64. | |
10020 | */ | |
10021 | dst_reg->smin_value = dst_reg->umin_value; | |
10022 | dst_reg->smax_value = dst_reg->umax_value; | |
10023 | } | |
10024 | /* We may learn something more from the var_off */ | |
10025 | __update_reg_bounds(dst_reg); | |
10026 | } | |
10027 | ||
3f50f132 JF |
10028 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
10029 | struct bpf_reg_state *src_reg) | |
10030 | { | |
10031 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
10032 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
10033 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
10034 | s32 smin_val = src_reg->s32_min_value; |
10035 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 10036 | |
049c4e13 DB |
10037 | if (src_known && dst_known) { |
10038 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 10039 | return; |
049c4e13 | 10040 | } |
3f50f132 JF |
10041 | |
10042 | /* We get our maximum from the var_off, and our minimum is the | |
10043 | * maximum of the operands' minima | |
10044 | */ | |
10045 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
10046 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
10047 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
10048 | /* Lose signed bounds when ORing negative numbers, | |
10049 | * ain't nobody got time for that. | |
10050 | */ | |
10051 | dst_reg->s32_min_value = S32_MIN; | |
10052 | dst_reg->s32_max_value = S32_MAX; | |
10053 | } else { | |
10054 | /* ORing two positives gives a positive, so safe to | |
10055 | * cast result into s64. | |
10056 | */ | |
5b9fbeb7 DB |
10057 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
10058 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
10059 | } |
10060 | } | |
10061 | ||
07cd2631 JF |
10062 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
10063 | struct bpf_reg_state *src_reg) | |
10064 | { | |
3f50f132 JF |
10065 | bool src_known = tnum_is_const(src_reg->var_off); |
10066 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
10067 | s64 smin_val = src_reg->smin_value; |
10068 | u64 umin_val = src_reg->umin_value; | |
10069 | ||
3f50f132 | 10070 | if (src_known && dst_known) { |
4fbb38a3 | 10071 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
10072 | return; |
10073 | } | |
10074 | ||
07cd2631 JF |
10075 | /* We get our maximum from the var_off, and our minimum is the |
10076 | * maximum of the operands' minima | |
10077 | */ | |
07cd2631 JF |
10078 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
10079 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
10080 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
10081 | /* Lose signed bounds when ORing negative numbers, | |
10082 | * ain't nobody got time for that. | |
10083 | */ | |
10084 | dst_reg->smin_value = S64_MIN; | |
10085 | dst_reg->smax_value = S64_MAX; | |
10086 | } else { | |
10087 | /* ORing two positives gives a positive, so safe to | |
10088 | * cast result into s64. | |
10089 | */ | |
10090 | dst_reg->smin_value = dst_reg->umin_value; | |
10091 | dst_reg->smax_value = dst_reg->umax_value; | |
10092 | } | |
10093 | /* We may learn something more from the var_off */ | |
10094 | __update_reg_bounds(dst_reg); | |
10095 | } | |
10096 | ||
2921c90d YS |
10097 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
10098 | struct bpf_reg_state *src_reg) | |
10099 | { | |
10100 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
10101 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
10102 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
10103 | s32 smin_val = src_reg->s32_min_value; | |
10104 | ||
049c4e13 DB |
10105 | if (src_known && dst_known) { |
10106 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 10107 | return; |
049c4e13 | 10108 | } |
2921c90d YS |
10109 | |
10110 | /* We get both minimum and maximum from the var32_off. */ | |
10111 | dst_reg->u32_min_value = var32_off.value; | |
10112 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
10113 | ||
10114 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
10115 | /* XORing two positive sign numbers gives a positive, | |
10116 | * so safe to cast u32 result into s32. | |
10117 | */ | |
10118 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
10119 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
10120 | } else { | |
10121 | dst_reg->s32_min_value = S32_MIN; | |
10122 | dst_reg->s32_max_value = S32_MAX; | |
10123 | } | |
10124 | } | |
10125 | ||
10126 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
10127 | struct bpf_reg_state *src_reg) | |
10128 | { | |
10129 | bool src_known = tnum_is_const(src_reg->var_off); | |
10130 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
10131 | s64 smin_val = src_reg->smin_value; | |
10132 | ||
10133 | if (src_known && dst_known) { | |
10134 | /* dst_reg->var_off.value has been updated earlier */ | |
10135 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
10136 | return; | |
10137 | } | |
10138 | ||
10139 | /* We get both minimum and maximum from the var_off. */ | |
10140 | dst_reg->umin_value = dst_reg->var_off.value; | |
10141 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
10142 | ||
10143 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
10144 | /* XORing two positive sign numbers gives a positive, | |
10145 | * so safe to cast u64 result into s64. | |
10146 | */ | |
10147 | dst_reg->smin_value = dst_reg->umin_value; | |
10148 | dst_reg->smax_value = dst_reg->umax_value; | |
10149 | } else { | |
10150 | dst_reg->smin_value = S64_MIN; | |
10151 | dst_reg->smax_value = S64_MAX; | |
10152 | } | |
10153 | ||
10154 | __update_reg_bounds(dst_reg); | |
10155 | } | |
10156 | ||
3f50f132 JF |
10157 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
10158 | u64 umin_val, u64 umax_val) | |
07cd2631 | 10159 | { |
07cd2631 JF |
10160 | /* We lose all sign bit information (except what we can pick |
10161 | * up from var_off) | |
10162 | */ | |
3f50f132 JF |
10163 | dst_reg->s32_min_value = S32_MIN; |
10164 | dst_reg->s32_max_value = S32_MAX; | |
10165 | /* If we might shift our top bit out, then we know nothing */ | |
10166 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
10167 | dst_reg->u32_min_value = 0; | |
10168 | dst_reg->u32_max_value = U32_MAX; | |
10169 | } else { | |
10170 | dst_reg->u32_min_value <<= umin_val; | |
10171 | dst_reg->u32_max_value <<= umax_val; | |
10172 | } | |
10173 | } | |
10174 | ||
10175 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
10176 | struct bpf_reg_state *src_reg) | |
10177 | { | |
10178 | u32 umax_val = src_reg->u32_max_value; | |
10179 | u32 umin_val = src_reg->u32_min_value; | |
10180 | /* u32 alu operation will zext upper bits */ | |
10181 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
10182 | ||
10183 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
10184 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
10185 | /* Not required but being careful mark reg64 bounds as unknown so | |
10186 | * that we are forced to pick them up from tnum and zext later and | |
10187 | * if some path skips this step we are still safe. | |
10188 | */ | |
10189 | __mark_reg64_unbounded(dst_reg); | |
10190 | __update_reg32_bounds(dst_reg); | |
10191 | } | |
10192 | ||
10193 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
10194 | u64 umin_val, u64 umax_val) | |
10195 | { | |
10196 | /* Special case <<32 because it is a common compiler pattern to sign | |
10197 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
10198 | * positive we know this shift will also be positive so we can track | |
10199 | * bounds correctly. Otherwise we lose all sign bit information except | |
10200 | * what we can pick up from var_off. Perhaps we can generalize this | |
10201 | * later to shifts of any length. | |
10202 | */ | |
10203 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
10204 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
10205 | else | |
10206 | dst_reg->smax_value = S64_MAX; | |
10207 | ||
10208 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
10209 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
10210 | else | |
10211 | dst_reg->smin_value = S64_MIN; | |
10212 | ||
07cd2631 JF |
10213 | /* If we might shift our top bit out, then we know nothing */ |
10214 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
10215 | dst_reg->umin_value = 0; | |
10216 | dst_reg->umax_value = U64_MAX; | |
10217 | } else { | |
10218 | dst_reg->umin_value <<= umin_val; | |
10219 | dst_reg->umax_value <<= umax_val; | |
10220 | } | |
3f50f132 JF |
10221 | } |
10222 | ||
10223 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
10224 | struct bpf_reg_state *src_reg) | |
10225 | { | |
10226 | u64 umax_val = src_reg->umax_value; | |
10227 | u64 umin_val = src_reg->umin_value; | |
10228 | ||
10229 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
10230 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
10231 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
10232 | ||
07cd2631 JF |
10233 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
10234 | /* We may learn something more from the var_off */ | |
10235 | __update_reg_bounds(dst_reg); | |
10236 | } | |
10237 | ||
3f50f132 JF |
10238 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
10239 | struct bpf_reg_state *src_reg) | |
10240 | { | |
10241 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
10242 | u32 umax_val = src_reg->u32_max_value; | |
10243 | u32 umin_val = src_reg->u32_min_value; | |
10244 | ||
10245 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
10246 | * be negative, then either: | |
10247 | * 1) src_reg might be zero, so the sign bit of the result is | |
10248 | * unknown, so we lose our signed bounds | |
10249 | * 2) it's known negative, thus the unsigned bounds capture the | |
10250 | * signed bounds | |
10251 | * 3) the signed bounds cross zero, so they tell us nothing | |
10252 | * about the result | |
10253 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 10254 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
10255 | * Thus, in all cases it suffices to blow away our signed bounds |
10256 | * and rely on inferring new ones from the unsigned bounds and | |
10257 | * var_off of the result. | |
10258 | */ | |
10259 | dst_reg->s32_min_value = S32_MIN; | |
10260 | dst_reg->s32_max_value = S32_MAX; | |
10261 | ||
10262 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
10263 | dst_reg->u32_min_value >>= umax_val; | |
10264 | dst_reg->u32_max_value >>= umin_val; | |
10265 | ||
10266 | __mark_reg64_unbounded(dst_reg); | |
10267 | __update_reg32_bounds(dst_reg); | |
10268 | } | |
10269 | ||
07cd2631 JF |
10270 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
10271 | struct bpf_reg_state *src_reg) | |
10272 | { | |
10273 | u64 umax_val = src_reg->umax_value; | |
10274 | u64 umin_val = src_reg->umin_value; | |
10275 | ||
10276 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
10277 | * be negative, then either: | |
10278 | * 1) src_reg might be zero, so the sign bit of the result is | |
10279 | * unknown, so we lose our signed bounds | |
10280 | * 2) it's known negative, thus the unsigned bounds capture the | |
10281 | * signed bounds | |
10282 | * 3) the signed bounds cross zero, so they tell us nothing | |
10283 | * about the result | |
10284 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 10285 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
10286 | * Thus, in all cases it suffices to blow away our signed bounds |
10287 | * and rely on inferring new ones from the unsigned bounds and | |
10288 | * var_off of the result. | |
10289 | */ | |
10290 | dst_reg->smin_value = S64_MIN; | |
10291 | dst_reg->smax_value = S64_MAX; | |
10292 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
10293 | dst_reg->umin_value >>= umax_val; | |
10294 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
10295 | |
10296 | /* Its not easy to operate on alu32 bounds here because it depends | |
10297 | * on bits being shifted in. Take easy way out and mark unbounded | |
10298 | * so we can recalculate later from tnum. | |
10299 | */ | |
10300 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
10301 | __update_reg_bounds(dst_reg); |
10302 | } | |
10303 | ||
3f50f132 JF |
10304 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
10305 | struct bpf_reg_state *src_reg) | |
07cd2631 | 10306 | { |
3f50f132 | 10307 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
10308 | |
10309 | /* Upon reaching here, src_known is true and | |
10310 | * umax_val is equal to umin_val. | |
10311 | */ | |
3f50f132 JF |
10312 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
10313 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 10314 | |
3f50f132 JF |
10315 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
10316 | ||
10317 | /* blow away the dst_reg umin_value/umax_value and rely on | |
10318 | * dst_reg var_off to refine the result. | |
10319 | */ | |
10320 | dst_reg->u32_min_value = 0; | |
10321 | dst_reg->u32_max_value = U32_MAX; | |
10322 | ||
10323 | __mark_reg64_unbounded(dst_reg); | |
10324 | __update_reg32_bounds(dst_reg); | |
10325 | } | |
10326 | ||
10327 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
10328 | struct bpf_reg_state *src_reg) | |
10329 | { | |
10330 | u64 umin_val = src_reg->umin_value; | |
10331 | ||
10332 | /* Upon reaching here, src_known is true and umax_val is equal | |
10333 | * to umin_val. | |
10334 | */ | |
10335 | dst_reg->smin_value >>= umin_val; | |
10336 | dst_reg->smax_value >>= umin_val; | |
10337 | ||
10338 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
10339 | |
10340 | /* blow away the dst_reg umin_value/umax_value and rely on | |
10341 | * dst_reg var_off to refine the result. | |
10342 | */ | |
10343 | dst_reg->umin_value = 0; | |
10344 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
10345 | |
10346 | /* Its not easy to operate on alu32 bounds here because it depends | |
10347 | * on bits being shifted in from upper 32-bits. Take easy way out | |
10348 | * and mark unbounded so we can recalculate later from tnum. | |
10349 | */ | |
10350 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
10351 | __update_reg_bounds(dst_reg); |
10352 | } | |
10353 | ||
468f6eaf JH |
10354 | /* WARNING: This function does calculations on 64-bit values, but the actual |
10355 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
10356 | * need extra checks in the 32-bit case. | |
10357 | */ | |
f1174f77 EC |
10358 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
10359 | struct bpf_insn *insn, | |
10360 | struct bpf_reg_state *dst_reg, | |
10361 | struct bpf_reg_state src_reg) | |
969bf05e | 10362 | { |
638f5b90 | 10363 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 10364 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 10365 | bool src_known; |
b03c9f9f EC |
10366 | s64 smin_val, smax_val; |
10367 | u64 umin_val, umax_val; | |
3f50f132 JF |
10368 | s32 s32_min_val, s32_max_val; |
10369 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 10370 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 10371 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 10372 | int ret; |
b799207e | 10373 | |
b03c9f9f EC |
10374 | smin_val = src_reg.smin_value; |
10375 | smax_val = src_reg.smax_value; | |
10376 | umin_val = src_reg.umin_value; | |
10377 | umax_val = src_reg.umax_value; | |
f23cc643 | 10378 | |
3f50f132 JF |
10379 | s32_min_val = src_reg.s32_min_value; |
10380 | s32_max_val = src_reg.s32_max_value; | |
10381 | u32_min_val = src_reg.u32_min_value; | |
10382 | u32_max_val = src_reg.u32_max_value; | |
10383 | ||
10384 | if (alu32) { | |
10385 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
10386 | if ((src_known && |
10387 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
10388 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
10389 | /* Taint dst register if offset had invalid bounds | |
10390 | * derived from e.g. dead branches. | |
10391 | */ | |
10392 | __mark_reg_unknown(env, dst_reg); | |
10393 | return 0; | |
10394 | } | |
10395 | } else { | |
10396 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
10397 | if ((src_known && |
10398 | (smin_val != smax_val || umin_val != umax_val)) || | |
10399 | smin_val > smax_val || umin_val > umax_val) { | |
10400 | /* Taint dst register if offset had invalid bounds | |
10401 | * derived from e.g. dead branches. | |
10402 | */ | |
10403 | __mark_reg_unknown(env, dst_reg); | |
10404 | return 0; | |
10405 | } | |
6f16101e DB |
10406 | } |
10407 | ||
bb7f0f98 AS |
10408 | if (!src_known && |
10409 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 10410 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
10411 | return 0; |
10412 | } | |
10413 | ||
f5288193 DB |
10414 | if (sanitize_needed(opcode)) { |
10415 | ret = sanitize_val_alu(env, insn); | |
10416 | if (ret < 0) | |
10417 | return sanitize_err(env, insn, ret, NULL, NULL); | |
10418 | } | |
10419 | ||
3f50f132 JF |
10420 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
10421 | * There are two classes of instructions: The first class we track both | |
10422 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
10423 | * greatest amount of precision when alu operations are mixed with jmp32 | |
10424 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
10425 | * and BPF_OR. This is possible because these ops have fairly easy to | |
10426 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
10427 | * See alu32 verifier tests for examples. The second class of | |
10428 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
10429 | * with regards to tracking sign/unsigned bounds because the bits may | |
10430 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
10431 | * the reg unbounded in the subreg bound space and use the resulting | |
10432 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
10433 | */ | |
48461135 JB |
10434 | switch (opcode) { |
10435 | case BPF_ADD: | |
3f50f132 | 10436 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 10437 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 10438 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
10439 | break; |
10440 | case BPF_SUB: | |
3f50f132 | 10441 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 10442 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 10443 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
10444 | break; |
10445 | case BPF_MUL: | |
3f50f132 JF |
10446 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
10447 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 10448 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
10449 | break; |
10450 | case BPF_AND: | |
3f50f132 JF |
10451 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
10452 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 10453 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
10454 | break; |
10455 | case BPF_OR: | |
3f50f132 JF |
10456 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
10457 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 10458 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 10459 | break; |
2921c90d YS |
10460 | case BPF_XOR: |
10461 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
10462 | scalar32_min_max_xor(dst_reg, &src_reg); | |
10463 | scalar_min_max_xor(dst_reg, &src_reg); | |
10464 | break; | |
48461135 | 10465 | case BPF_LSH: |
468f6eaf JH |
10466 | if (umax_val >= insn_bitness) { |
10467 | /* Shifts greater than 31 or 63 are undefined. | |
10468 | * This includes shifts by a negative number. | |
b03c9f9f | 10469 | */ |
61bd5218 | 10470 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
10471 | break; |
10472 | } | |
3f50f132 JF |
10473 | if (alu32) |
10474 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
10475 | else | |
10476 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
10477 | break; |
10478 | case BPF_RSH: | |
468f6eaf JH |
10479 | if (umax_val >= insn_bitness) { |
10480 | /* Shifts greater than 31 or 63 are undefined. | |
10481 | * This includes shifts by a negative number. | |
b03c9f9f | 10482 | */ |
61bd5218 | 10483 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
10484 | break; |
10485 | } | |
3f50f132 JF |
10486 | if (alu32) |
10487 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
10488 | else | |
10489 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 10490 | break; |
9cbe1f5a YS |
10491 | case BPF_ARSH: |
10492 | if (umax_val >= insn_bitness) { | |
10493 | /* Shifts greater than 31 or 63 are undefined. | |
10494 | * This includes shifts by a negative number. | |
10495 | */ | |
10496 | mark_reg_unknown(env, regs, insn->dst_reg); | |
10497 | break; | |
10498 | } | |
3f50f132 JF |
10499 | if (alu32) |
10500 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
10501 | else | |
10502 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 10503 | break; |
48461135 | 10504 | default: |
61bd5218 | 10505 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
10506 | break; |
10507 | } | |
10508 | ||
3f50f132 JF |
10509 | /* ALU32 ops are zero extended into 64bit register */ |
10510 | if (alu32) | |
10511 | zext_32_to_64(dst_reg); | |
3844d153 | 10512 | reg_bounds_sync(dst_reg); |
f1174f77 EC |
10513 | return 0; |
10514 | } | |
10515 | ||
10516 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
10517 | * and var_off. | |
10518 | */ | |
10519 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
10520 | struct bpf_insn *insn) | |
10521 | { | |
f4d7e40a AS |
10522 | struct bpf_verifier_state *vstate = env->cur_state; |
10523 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
10524 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
10525 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
10526 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 10527 | int err; |
f1174f77 EC |
10528 | |
10529 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
10530 | src_reg = NULL; |
10531 | if (dst_reg->type != SCALAR_VALUE) | |
10532 | ptr_reg = dst_reg; | |
75748837 AS |
10533 | else |
10534 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
10535 | * incorrectly propagated into other registers by find_equal_scalars() | |
10536 | */ | |
10537 | dst_reg->id = 0; | |
f1174f77 EC |
10538 | if (BPF_SRC(insn->code) == BPF_X) { |
10539 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
10540 | if (src_reg->type != SCALAR_VALUE) { |
10541 | if (dst_reg->type != SCALAR_VALUE) { | |
10542 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
10543 | * an arbitrary scalar. Disallow all math except |
10544 | * pointer subtraction | |
f1174f77 | 10545 | */ |
dd066823 | 10546 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
10547 | mark_reg_unknown(env, regs, insn->dst_reg); |
10548 | return 0; | |
f1174f77 | 10549 | } |
82abbf8d AS |
10550 | verbose(env, "R%d pointer %s pointer prohibited\n", |
10551 | insn->dst_reg, | |
10552 | bpf_alu_string[opcode >> 4]); | |
10553 | return -EACCES; | |
f1174f77 EC |
10554 | } else { |
10555 | /* scalar += pointer | |
10556 | * This is legal, but we have to reverse our | |
10557 | * src/dest handling in computing the range | |
10558 | */ | |
b5dc0163 AS |
10559 | err = mark_chain_precision(env, insn->dst_reg); |
10560 | if (err) | |
10561 | return err; | |
82abbf8d AS |
10562 | return adjust_ptr_min_max_vals(env, insn, |
10563 | src_reg, dst_reg); | |
f1174f77 EC |
10564 | } |
10565 | } else if (ptr_reg) { | |
10566 | /* pointer += scalar */ | |
b5dc0163 AS |
10567 | err = mark_chain_precision(env, insn->src_reg); |
10568 | if (err) | |
10569 | return err; | |
82abbf8d AS |
10570 | return adjust_ptr_min_max_vals(env, insn, |
10571 | dst_reg, src_reg); | |
a3b666bf AN |
10572 | } else if (dst_reg->precise) { |
10573 | /* if dst_reg is precise, src_reg should be precise as well */ | |
10574 | err = mark_chain_precision(env, insn->src_reg); | |
10575 | if (err) | |
10576 | return err; | |
f1174f77 EC |
10577 | } |
10578 | } else { | |
10579 | /* Pretend the src is a reg with a known value, since we only | |
10580 | * need to be able to read from this state. | |
10581 | */ | |
10582 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 10583 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 10584 | src_reg = &off_reg; |
82abbf8d AS |
10585 | if (ptr_reg) /* pointer += K */ |
10586 | return adjust_ptr_min_max_vals(env, insn, | |
10587 | ptr_reg, src_reg); | |
f1174f77 EC |
10588 | } |
10589 | ||
10590 | /* Got here implies adding two SCALAR_VALUEs */ | |
10591 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 10592 | print_verifier_state(env, state, true); |
61bd5218 | 10593 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
10594 | return -EINVAL; |
10595 | } | |
10596 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 10597 | print_verifier_state(env, state, true); |
61bd5218 | 10598 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
10599 | return -EINVAL; |
10600 | } | |
10601 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
10602 | } |
10603 | ||
17a52670 | 10604 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 10605 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 10606 | { |
638f5b90 | 10607 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
10608 | u8 opcode = BPF_OP(insn->code); |
10609 | int err; | |
10610 | ||
10611 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
10612 | if (opcode == BPF_NEG) { | |
395e942d | 10613 | if (BPF_SRC(insn->code) != BPF_K || |
17a52670 AS |
10614 | insn->src_reg != BPF_REG_0 || |
10615 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 10616 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
10617 | return -EINVAL; |
10618 | } | |
10619 | } else { | |
10620 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
10621 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
10622 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 10623 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
10624 | return -EINVAL; |
10625 | } | |
10626 | } | |
10627 | ||
10628 | /* check src operand */ | |
dc503a8a | 10629 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
10630 | if (err) |
10631 | return err; | |
10632 | ||
1be7f75d | 10633 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 10634 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
10635 | insn->dst_reg); |
10636 | return -EACCES; | |
10637 | } | |
10638 | ||
17a52670 | 10639 | /* check dest operand */ |
dc503a8a | 10640 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
10641 | if (err) |
10642 | return err; | |
10643 | ||
10644 | } else if (opcode == BPF_MOV) { | |
10645 | ||
10646 | if (BPF_SRC(insn->code) == BPF_X) { | |
10647 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 10648 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
10649 | return -EINVAL; |
10650 | } | |
10651 | ||
10652 | /* check src operand */ | |
dc503a8a | 10653 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
10654 | if (err) |
10655 | return err; | |
10656 | } else { | |
10657 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 10658 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
10659 | return -EINVAL; |
10660 | } | |
10661 | } | |
10662 | ||
fbeb1603 AF |
10663 | /* check dest operand, mark as required later */ |
10664 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
10665 | if (err) |
10666 | return err; | |
10667 | ||
10668 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
10669 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
10670 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
10671 | ||
17a52670 AS |
10672 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
10673 | /* case: R1 = R2 | |
10674 | * copy register state to dest reg | |
10675 | */ | |
75748837 AS |
10676 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
10677 | /* Assign src and dst registers the same ID | |
10678 | * that will be used by find_equal_scalars() | |
10679 | * to propagate min/max range. | |
10680 | */ | |
10681 | src_reg->id = ++env->id_gen; | |
e434b8cd JW |
10682 | *dst_reg = *src_reg; |
10683 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 10684 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 10685 | } else { |
f1174f77 | 10686 | /* R1 = (u32) R2 */ |
1be7f75d | 10687 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
10688 | verbose(env, |
10689 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
10690 | insn->src_reg); |
10691 | return -EACCES; | |
e434b8cd JW |
10692 | } else if (src_reg->type == SCALAR_VALUE) { |
10693 | *dst_reg = *src_reg; | |
75748837 AS |
10694 | /* Make sure ID is cleared otherwise |
10695 | * dst_reg min/max could be incorrectly | |
10696 | * propagated into src_reg by find_equal_scalars() | |
10697 | */ | |
10698 | dst_reg->id = 0; | |
e434b8cd | 10699 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 10700 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
10701 | } else { |
10702 | mark_reg_unknown(env, regs, | |
10703 | insn->dst_reg); | |
1be7f75d | 10704 | } |
3f50f132 | 10705 | zext_32_to_64(dst_reg); |
3844d153 | 10706 | reg_bounds_sync(dst_reg); |
17a52670 AS |
10707 | } |
10708 | } else { | |
10709 | /* case: R = imm | |
10710 | * remember the value we stored into this reg | |
10711 | */ | |
fbeb1603 AF |
10712 | /* clear any state __mark_reg_known doesn't set */ |
10713 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 10714 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
10715 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
10716 | __mark_reg_known(regs + insn->dst_reg, | |
10717 | insn->imm); | |
10718 | } else { | |
10719 | __mark_reg_known(regs + insn->dst_reg, | |
10720 | (u32)insn->imm); | |
10721 | } | |
17a52670 AS |
10722 | } |
10723 | ||
10724 | } else if (opcode > BPF_END) { | |
61bd5218 | 10725 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
10726 | return -EINVAL; |
10727 | ||
10728 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
10729 | ||
17a52670 AS |
10730 | if (BPF_SRC(insn->code) == BPF_X) { |
10731 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 10732 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
10733 | return -EINVAL; |
10734 | } | |
10735 | /* check src1 operand */ | |
dc503a8a | 10736 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
10737 | if (err) |
10738 | return err; | |
10739 | } else { | |
10740 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 10741 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
10742 | return -EINVAL; |
10743 | } | |
10744 | } | |
10745 | ||
10746 | /* check src2 operand */ | |
dc503a8a | 10747 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
10748 | if (err) |
10749 | return err; | |
10750 | ||
10751 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
10752 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 10753 | verbose(env, "div by zero\n"); |
17a52670 AS |
10754 | return -EINVAL; |
10755 | } | |
10756 | ||
229394e8 RV |
10757 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
10758 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
10759 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
10760 | ||
10761 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 10762 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
10763 | return -EINVAL; |
10764 | } | |
10765 | } | |
10766 | ||
1a0dc1ac | 10767 | /* check dest operand */ |
dc503a8a | 10768 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
10769 | if (err) |
10770 | return err; | |
10771 | ||
f1174f77 | 10772 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
10773 | } |
10774 | ||
10775 | return 0; | |
10776 | } | |
10777 | ||
f4d7e40a | 10778 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 10779 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 10780 | enum bpf_reg_type type, |
fb2a311a | 10781 | bool range_right_open) |
969bf05e | 10782 | { |
b239da34 KKD |
10783 | struct bpf_func_state *state; |
10784 | struct bpf_reg_state *reg; | |
10785 | int new_range; | |
2d2be8ca | 10786 | |
fb2a311a DB |
10787 | if (dst_reg->off < 0 || |
10788 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
10789 | /* This doesn't give us any range */ |
10790 | return; | |
10791 | ||
b03c9f9f EC |
10792 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
10793 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
10794 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
10795 | * than pkt_end, but that's because it's also less than pkt. | |
10796 | */ | |
10797 | return; | |
10798 | ||
fb2a311a DB |
10799 | new_range = dst_reg->off; |
10800 | if (range_right_open) | |
2fa7d94a | 10801 | new_range++; |
fb2a311a DB |
10802 | |
10803 | /* Examples for register markings: | |
2d2be8ca | 10804 | * |
fb2a311a | 10805 | * pkt_data in dst register: |
2d2be8ca DB |
10806 | * |
10807 | * r2 = r3; | |
10808 | * r2 += 8; | |
10809 | * if (r2 > pkt_end) goto <handle exception> | |
10810 | * <access okay> | |
10811 | * | |
b4e432f1 DB |
10812 | * r2 = r3; |
10813 | * r2 += 8; | |
10814 | * if (r2 < pkt_end) goto <access okay> | |
10815 | * <handle exception> | |
10816 | * | |
2d2be8ca DB |
10817 | * Where: |
10818 | * r2 == dst_reg, pkt_end == src_reg | |
10819 | * r2=pkt(id=n,off=8,r=0) | |
10820 | * r3=pkt(id=n,off=0,r=0) | |
10821 | * | |
fb2a311a | 10822 | * pkt_data in src register: |
2d2be8ca DB |
10823 | * |
10824 | * r2 = r3; | |
10825 | * r2 += 8; | |
10826 | * if (pkt_end >= r2) goto <access okay> | |
10827 | * <handle exception> | |
10828 | * | |
b4e432f1 DB |
10829 | * r2 = r3; |
10830 | * r2 += 8; | |
10831 | * if (pkt_end <= r2) goto <handle exception> | |
10832 | * <access okay> | |
10833 | * | |
2d2be8ca DB |
10834 | * Where: |
10835 | * pkt_end == dst_reg, r2 == src_reg | |
10836 | * r2=pkt(id=n,off=8,r=0) | |
10837 | * r3=pkt(id=n,off=0,r=0) | |
10838 | * | |
10839 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
10840 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
10841 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
10842 | * the check. | |
969bf05e | 10843 | */ |
2d2be8ca | 10844 | |
f1174f77 EC |
10845 | /* If our ids match, then we must have the same max_value. And we |
10846 | * don't care about the other reg's fixed offset, since if it's too big | |
10847 | * the range won't allow anything. | |
10848 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
10849 | */ | |
b239da34 KKD |
10850 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
10851 | if (reg->type == type && reg->id == dst_reg->id) | |
10852 | /* keep the maximum range already checked */ | |
10853 | reg->range = max(reg->range, new_range); | |
10854 | })); | |
969bf05e AS |
10855 | } |
10856 | ||
3f50f132 | 10857 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 10858 | { |
3f50f132 JF |
10859 | struct tnum subreg = tnum_subreg(reg->var_off); |
10860 | s32 sval = (s32)val; | |
a72dafaf | 10861 | |
3f50f132 JF |
10862 | switch (opcode) { |
10863 | case BPF_JEQ: | |
10864 | if (tnum_is_const(subreg)) | |
10865 | return !!tnum_equals_const(subreg, val); | |
10866 | break; | |
10867 | case BPF_JNE: | |
10868 | if (tnum_is_const(subreg)) | |
10869 | return !tnum_equals_const(subreg, val); | |
10870 | break; | |
10871 | case BPF_JSET: | |
10872 | if ((~subreg.mask & subreg.value) & val) | |
10873 | return 1; | |
10874 | if (!((subreg.mask | subreg.value) & val)) | |
10875 | return 0; | |
10876 | break; | |
10877 | case BPF_JGT: | |
10878 | if (reg->u32_min_value > val) | |
10879 | return 1; | |
10880 | else if (reg->u32_max_value <= val) | |
10881 | return 0; | |
10882 | break; | |
10883 | case BPF_JSGT: | |
10884 | if (reg->s32_min_value > sval) | |
10885 | return 1; | |
ee114dd6 | 10886 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
10887 | return 0; |
10888 | break; | |
10889 | case BPF_JLT: | |
10890 | if (reg->u32_max_value < val) | |
10891 | return 1; | |
10892 | else if (reg->u32_min_value >= val) | |
10893 | return 0; | |
10894 | break; | |
10895 | case BPF_JSLT: | |
10896 | if (reg->s32_max_value < sval) | |
10897 | return 1; | |
10898 | else if (reg->s32_min_value >= sval) | |
10899 | return 0; | |
10900 | break; | |
10901 | case BPF_JGE: | |
10902 | if (reg->u32_min_value >= val) | |
10903 | return 1; | |
10904 | else if (reg->u32_max_value < val) | |
10905 | return 0; | |
10906 | break; | |
10907 | case BPF_JSGE: | |
10908 | if (reg->s32_min_value >= sval) | |
10909 | return 1; | |
10910 | else if (reg->s32_max_value < sval) | |
10911 | return 0; | |
10912 | break; | |
10913 | case BPF_JLE: | |
10914 | if (reg->u32_max_value <= val) | |
10915 | return 1; | |
10916 | else if (reg->u32_min_value > val) | |
10917 | return 0; | |
10918 | break; | |
10919 | case BPF_JSLE: | |
10920 | if (reg->s32_max_value <= sval) | |
10921 | return 1; | |
10922 | else if (reg->s32_min_value > sval) | |
10923 | return 0; | |
10924 | break; | |
10925 | } | |
4f7b3e82 | 10926 | |
3f50f132 JF |
10927 | return -1; |
10928 | } | |
092ed096 | 10929 | |
3f50f132 JF |
10930 | |
10931 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
10932 | { | |
10933 | s64 sval = (s64)val; | |
a72dafaf | 10934 | |
4f7b3e82 AS |
10935 | switch (opcode) { |
10936 | case BPF_JEQ: | |
10937 | if (tnum_is_const(reg->var_off)) | |
10938 | return !!tnum_equals_const(reg->var_off, val); | |
10939 | break; | |
10940 | case BPF_JNE: | |
10941 | if (tnum_is_const(reg->var_off)) | |
10942 | return !tnum_equals_const(reg->var_off, val); | |
10943 | break; | |
960ea056 JK |
10944 | case BPF_JSET: |
10945 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
10946 | return 1; | |
10947 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
10948 | return 0; | |
10949 | break; | |
4f7b3e82 AS |
10950 | case BPF_JGT: |
10951 | if (reg->umin_value > val) | |
10952 | return 1; | |
10953 | else if (reg->umax_value <= val) | |
10954 | return 0; | |
10955 | break; | |
10956 | case BPF_JSGT: | |
a72dafaf | 10957 | if (reg->smin_value > sval) |
4f7b3e82 | 10958 | return 1; |
ee114dd6 | 10959 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
10960 | return 0; |
10961 | break; | |
10962 | case BPF_JLT: | |
10963 | if (reg->umax_value < val) | |
10964 | return 1; | |
10965 | else if (reg->umin_value >= val) | |
10966 | return 0; | |
10967 | break; | |
10968 | case BPF_JSLT: | |
a72dafaf | 10969 | if (reg->smax_value < sval) |
4f7b3e82 | 10970 | return 1; |
a72dafaf | 10971 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
10972 | return 0; |
10973 | break; | |
10974 | case BPF_JGE: | |
10975 | if (reg->umin_value >= val) | |
10976 | return 1; | |
10977 | else if (reg->umax_value < val) | |
10978 | return 0; | |
10979 | break; | |
10980 | case BPF_JSGE: | |
a72dafaf | 10981 | if (reg->smin_value >= sval) |
4f7b3e82 | 10982 | return 1; |
a72dafaf | 10983 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
10984 | return 0; |
10985 | break; | |
10986 | case BPF_JLE: | |
10987 | if (reg->umax_value <= val) | |
10988 | return 1; | |
10989 | else if (reg->umin_value > val) | |
10990 | return 0; | |
10991 | break; | |
10992 | case BPF_JSLE: | |
a72dafaf | 10993 | if (reg->smax_value <= sval) |
4f7b3e82 | 10994 | return 1; |
a72dafaf | 10995 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
10996 | return 0; |
10997 | break; | |
10998 | } | |
10999 | ||
11000 | return -1; | |
11001 | } | |
11002 | ||
3f50f132 JF |
11003 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
11004 | * and return: | |
11005 | * 1 - branch will be taken and "goto target" will be executed | |
11006 | * 0 - branch will not be taken and fall-through to next insn | |
11007 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
11008 | * range [0,10] | |
604dca5e | 11009 | */ |
3f50f132 JF |
11010 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
11011 | bool is_jmp32) | |
604dca5e | 11012 | { |
cac616db JF |
11013 | if (__is_pointer_value(false, reg)) { |
11014 | if (!reg_type_not_null(reg->type)) | |
11015 | return -1; | |
11016 | ||
11017 | /* If pointer is valid tests against zero will fail so we can | |
11018 | * use this to direct branch taken. | |
11019 | */ | |
11020 | if (val != 0) | |
11021 | return -1; | |
11022 | ||
11023 | switch (opcode) { | |
11024 | case BPF_JEQ: | |
11025 | return 0; | |
11026 | case BPF_JNE: | |
11027 | return 1; | |
11028 | default: | |
11029 | return -1; | |
11030 | } | |
11031 | } | |
604dca5e | 11032 | |
3f50f132 JF |
11033 | if (is_jmp32) |
11034 | return is_branch32_taken(reg, val, opcode); | |
11035 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
11036 | } |
11037 | ||
6d94e741 AS |
11038 | static int flip_opcode(u32 opcode) |
11039 | { | |
11040 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
11041 | static const u8 opcode_flip[16] = { | |
11042 | /* these stay the same */ | |
11043 | [BPF_JEQ >> 4] = BPF_JEQ, | |
11044 | [BPF_JNE >> 4] = BPF_JNE, | |
11045 | [BPF_JSET >> 4] = BPF_JSET, | |
11046 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
11047 | [BPF_JGE >> 4] = BPF_JLE, | |
11048 | [BPF_JGT >> 4] = BPF_JLT, | |
11049 | [BPF_JLE >> 4] = BPF_JGE, | |
11050 | [BPF_JLT >> 4] = BPF_JGT, | |
11051 | [BPF_JSGE >> 4] = BPF_JSLE, | |
11052 | [BPF_JSGT >> 4] = BPF_JSLT, | |
11053 | [BPF_JSLE >> 4] = BPF_JSGE, | |
11054 | [BPF_JSLT >> 4] = BPF_JSGT | |
11055 | }; | |
11056 | return opcode_flip[opcode >> 4]; | |
11057 | } | |
11058 | ||
11059 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
11060 | struct bpf_reg_state *src_reg, | |
11061 | u8 opcode) | |
11062 | { | |
11063 | struct bpf_reg_state *pkt; | |
11064 | ||
11065 | if (src_reg->type == PTR_TO_PACKET_END) { | |
11066 | pkt = dst_reg; | |
11067 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
11068 | pkt = src_reg; | |
11069 | opcode = flip_opcode(opcode); | |
11070 | } else { | |
11071 | return -1; | |
11072 | } | |
11073 | ||
11074 | if (pkt->range >= 0) | |
11075 | return -1; | |
11076 | ||
11077 | switch (opcode) { | |
11078 | case BPF_JLE: | |
11079 | /* pkt <= pkt_end */ | |
11080 | fallthrough; | |
11081 | case BPF_JGT: | |
11082 | /* pkt > pkt_end */ | |
11083 | if (pkt->range == BEYOND_PKT_END) | |
11084 | /* pkt has at last one extra byte beyond pkt_end */ | |
11085 | return opcode == BPF_JGT; | |
11086 | break; | |
11087 | case BPF_JLT: | |
11088 | /* pkt < pkt_end */ | |
11089 | fallthrough; | |
11090 | case BPF_JGE: | |
11091 | /* pkt >= pkt_end */ | |
11092 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
11093 | return opcode == BPF_JGE; | |
11094 | break; | |
11095 | } | |
11096 | return -1; | |
11097 | } | |
11098 | ||
48461135 JB |
11099 | /* Adjusts the register min/max values in the case that the dst_reg is the |
11100 | * variable register that we are working on, and src_reg is a constant or we're | |
11101 | * simply doing a BPF_K check. | |
f1174f77 | 11102 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
11103 | */ |
11104 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
11105 | struct bpf_reg_state *false_reg, |
11106 | u64 val, u32 val32, | |
092ed096 | 11107 | u8 opcode, bool is_jmp32) |
48461135 | 11108 | { |
3f50f132 JF |
11109 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
11110 | struct tnum false_64off = false_reg->var_off; | |
11111 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
11112 | struct tnum true_64off = true_reg->var_off; | |
11113 | s64 sval = (s64)val; | |
11114 | s32 sval32 = (s32)val32; | |
a72dafaf | 11115 | |
f1174f77 EC |
11116 | /* If the dst_reg is a pointer, we can't learn anything about its |
11117 | * variable offset from the compare (unless src_reg were a pointer into | |
11118 | * the same object, but we don't bother with that. | |
11119 | * Since false_reg and true_reg have the same type by construction, we | |
11120 | * only need to check one of them for pointerness. | |
11121 | */ | |
11122 | if (__is_pointer_value(false, false_reg)) | |
11123 | return; | |
4cabc5b1 | 11124 | |
48461135 | 11125 | switch (opcode) { |
a12ca627 DB |
11126 | /* JEQ/JNE comparison doesn't change the register equivalence. |
11127 | * | |
11128 | * r1 = r2; | |
11129 | * if (r1 == 42) goto label; | |
11130 | * ... | |
11131 | * label: // here both r1 and r2 are known to be 42. | |
11132 | * | |
11133 | * Hence when marking register as known preserve it's ID. | |
11134 | */ | |
48461135 | 11135 | case BPF_JEQ: |
a12ca627 DB |
11136 | if (is_jmp32) { |
11137 | __mark_reg32_known(true_reg, val32); | |
11138 | true_32off = tnum_subreg(true_reg->var_off); | |
11139 | } else { | |
11140 | ___mark_reg_known(true_reg, val); | |
11141 | true_64off = true_reg->var_off; | |
11142 | } | |
11143 | break; | |
48461135 | 11144 | case BPF_JNE: |
a12ca627 DB |
11145 | if (is_jmp32) { |
11146 | __mark_reg32_known(false_reg, val32); | |
11147 | false_32off = tnum_subreg(false_reg->var_off); | |
11148 | } else { | |
11149 | ___mark_reg_known(false_reg, val); | |
11150 | false_64off = false_reg->var_off; | |
11151 | } | |
48461135 | 11152 | break; |
960ea056 | 11153 | case BPF_JSET: |
3f50f132 JF |
11154 | if (is_jmp32) { |
11155 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
11156 | if (is_power_of_2(val32)) | |
11157 | true_32off = tnum_or(true_32off, | |
11158 | tnum_const(val32)); | |
11159 | } else { | |
11160 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
11161 | if (is_power_of_2(val)) | |
11162 | true_64off = tnum_or(true_64off, | |
11163 | tnum_const(val)); | |
11164 | } | |
960ea056 | 11165 | break; |
48461135 | 11166 | case BPF_JGE: |
a72dafaf JW |
11167 | case BPF_JGT: |
11168 | { | |
3f50f132 JF |
11169 | if (is_jmp32) { |
11170 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
11171 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
11172 | ||
11173 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
11174 | false_umax); | |
11175 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
11176 | true_umin); | |
11177 | } else { | |
11178 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
11179 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
11180 | ||
11181 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
11182 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
11183 | } | |
b03c9f9f | 11184 | break; |
a72dafaf | 11185 | } |
48461135 | 11186 | case BPF_JSGE: |
a72dafaf JW |
11187 | case BPF_JSGT: |
11188 | { | |
3f50f132 JF |
11189 | if (is_jmp32) { |
11190 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
11191 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 11192 | |
3f50f132 JF |
11193 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
11194 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
11195 | } else { | |
11196 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
11197 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
11198 | ||
11199 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
11200 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
11201 | } | |
48461135 | 11202 | break; |
a72dafaf | 11203 | } |
b4e432f1 | 11204 | case BPF_JLE: |
a72dafaf JW |
11205 | case BPF_JLT: |
11206 | { | |
3f50f132 JF |
11207 | if (is_jmp32) { |
11208 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
11209 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
11210 | ||
11211 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
11212 | false_umin); | |
11213 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
11214 | true_umax); | |
11215 | } else { | |
11216 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
11217 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
11218 | ||
11219 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
11220 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
11221 | } | |
b4e432f1 | 11222 | break; |
a72dafaf | 11223 | } |
b4e432f1 | 11224 | case BPF_JSLE: |
a72dafaf JW |
11225 | case BPF_JSLT: |
11226 | { | |
3f50f132 JF |
11227 | if (is_jmp32) { |
11228 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
11229 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 11230 | |
3f50f132 JF |
11231 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
11232 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
11233 | } else { | |
11234 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
11235 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
11236 | ||
11237 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
11238 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
11239 | } | |
b4e432f1 | 11240 | break; |
a72dafaf | 11241 | } |
48461135 | 11242 | default: |
0fc31b10 | 11243 | return; |
48461135 JB |
11244 | } |
11245 | ||
3f50f132 JF |
11246 | if (is_jmp32) { |
11247 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
11248 | tnum_subreg(false_32off)); | |
11249 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
11250 | tnum_subreg(true_32off)); | |
11251 | __reg_combine_32_into_64(false_reg); | |
11252 | __reg_combine_32_into_64(true_reg); | |
11253 | } else { | |
11254 | false_reg->var_off = false_64off; | |
11255 | true_reg->var_off = true_64off; | |
11256 | __reg_combine_64_into_32(false_reg); | |
11257 | __reg_combine_64_into_32(true_reg); | |
11258 | } | |
48461135 JB |
11259 | } |
11260 | ||
f1174f77 EC |
11261 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
11262 | * the variable reg. | |
48461135 JB |
11263 | */ |
11264 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
11265 | struct bpf_reg_state *false_reg, |
11266 | u64 val, u32 val32, | |
092ed096 | 11267 | u8 opcode, bool is_jmp32) |
48461135 | 11268 | { |
6d94e741 | 11269 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
11270 | /* This uses zero as "not present in table"; luckily the zero opcode, |
11271 | * BPF_JA, can't get here. | |
b03c9f9f | 11272 | */ |
0fc31b10 | 11273 | if (opcode) |
3f50f132 | 11274 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
11275 | } |
11276 | ||
11277 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
11278 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
11279 | struct bpf_reg_state *dst_reg) | |
11280 | { | |
b03c9f9f EC |
11281 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
11282 | dst_reg->umin_value); | |
11283 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
11284 | dst_reg->umax_value); | |
11285 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
11286 | dst_reg->smin_value); | |
11287 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
11288 | dst_reg->smax_value); | |
f1174f77 EC |
11289 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
11290 | dst_reg->var_off); | |
3844d153 DB |
11291 | reg_bounds_sync(src_reg); |
11292 | reg_bounds_sync(dst_reg); | |
f1174f77 EC |
11293 | } |
11294 | ||
11295 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
11296 | struct bpf_reg_state *true_dst, | |
11297 | struct bpf_reg_state *false_src, | |
11298 | struct bpf_reg_state *false_dst, | |
11299 | u8 opcode) | |
11300 | { | |
11301 | switch (opcode) { | |
11302 | case BPF_JEQ: | |
11303 | __reg_combine_min_max(true_src, true_dst); | |
11304 | break; | |
11305 | case BPF_JNE: | |
11306 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 11307 | break; |
4cabc5b1 | 11308 | } |
48461135 JB |
11309 | } |
11310 | ||
fd978bf7 JS |
11311 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
11312 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 11313 | bool is_null) |
57a09bf0 | 11314 | { |
c25b2ae1 | 11315 | if (type_may_be_null(reg->type) && reg->id == id && |
fca1aa75 | 11316 | (is_rcu_reg(reg) || !WARN_ON_ONCE(!reg->id))) { |
df57f38a KKD |
11317 | /* Old offset (both fixed and variable parts) should have been |
11318 | * known-zero, because we don't allow pointer arithmetic on | |
11319 | * pointers that might be NULL. If we see this happening, don't | |
11320 | * convert the register. | |
11321 | * | |
11322 | * But in some cases, some helpers that return local kptrs | |
11323 | * advance offset for the returned pointer. In those cases, it | |
11324 | * is fine to expect to see reg->off. | |
11325 | */ | |
11326 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || !tnum_equals_const(reg->var_off, 0))) | |
11327 | return; | |
11328 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC | PTR_MAYBE_NULL) && WARN_ON_ONCE(reg->off)) | |
e60b0d12 | 11329 | return; |
f1174f77 EC |
11330 | if (is_null) { |
11331 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
11332 | /* We don't need id and ref_obj_id from this point |
11333 | * onwards anymore, thus we should better reset it, | |
11334 | * so that state pruning has chances to take effect. | |
11335 | */ | |
11336 | reg->id = 0; | |
11337 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
11338 | |
11339 | return; | |
11340 | } | |
11341 | ||
11342 | mark_ptr_not_null_reg(reg); | |
11343 | ||
11344 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 | 11345 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
b239da34 | 11346 | * in release_reference(). |
1b986589 MKL |
11347 | * |
11348 | * reg->id is still used by spin_lock ptr. Other | |
11349 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
11350 | */ |
11351 | reg->id = 0; | |
56f668df | 11352 | } |
57a09bf0 TG |
11353 | } |
11354 | } | |
11355 | ||
11356 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
11357 | * be folded together at some point. | |
11358 | */ | |
840b9615 JS |
11359 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
11360 | bool is_null) | |
57a09bf0 | 11361 | { |
f4d7e40a | 11362 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
b239da34 | 11363 | struct bpf_reg_state *regs = state->regs, *reg; |
1b986589 | 11364 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 11365 | u32 id = regs[regno].id; |
57a09bf0 | 11366 | |
1b986589 MKL |
11367 | if (ref_obj_id && ref_obj_id == id && is_null) |
11368 | /* regs[regno] is in the " == NULL" branch. | |
11369 | * No one could have freed the reference state before | |
11370 | * doing the NULL check. | |
11371 | */ | |
11372 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 11373 | |
b239da34 KKD |
11374 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
11375 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
11376 | })); | |
57a09bf0 TG |
11377 | } |
11378 | ||
5beca081 DB |
11379 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
11380 | struct bpf_reg_state *dst_reg, | |
11381 | struct bpf_reg_state *src_reg, | |
11382 | struct bpf_verifier_state *this_branch, | |
11383 | struct bpf_verifier_state *other_branch) | |
11384 | { | |
11385 | if (BPF_SRC(insn->code) != BPF_X) | |
11386 | return false; | |
11387 | ||
092ed096 JW |
11388 | /* Pointers are always 64-bit. */ |
11389 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
11390 | return false; | |
11391 | ||
5beca081 DB |
11392 | switch (BPF_OP(insn->code)) { |
11393 | case BPF_JGT: | |
11394 | if ((dst_reg->type == PTR_TO_PACKET && | |
11395 | src_reg->type == PTR_TO_PACKET_END) || | |
11396 | (dst_reg->type == PTR_TO_PACKET_META && | |
11397 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
11398 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
11399 | find_good_pkt_pointers(this_branch, dst_reg, | |
11400 | dst_reg->type, false); | |
6d94e741 | 11401 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
11402 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
11403 | src_reg->type == PTR_TO_PACKET) || | |
11404 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
11405 | src_reg->type == PTR_TO_PACKET_META)) { | |
11406 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
11407 | find_good_pkt_pointers(other_branch, src_reg, | |
11408 | src_reg->type, true); | |
6d94e741 | 11409 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
11410 | } else { |
11411 | return false; | |
11412 | } | |
11413 | break; | |
11414 | case BPF_JLT: | |
11415 | if ((dst_reg->type == PTR_TO_PACKET && | |
11416 | src_reg->type == PTR_TO_PACKET_END) || | |
11417 | (dst_reg->type == PTR_TO_PACKET_META && | |
11418 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
11419 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
11420 | find_good_pkt_pointers(other_branch, dst_reg, | |
11421 | dst_reg->type, true); | |
6d94e741 | 11422 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
11423 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
11424 | src_reg->type == PTR_TO_PACKET) || | |
11425 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
11426 | src_reg->type == PTR_TO_PACKET_META)) { | |
11427 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
11428 | find_good_pkt_pointers(this_branch, src_reg, | |
11429 | src_reg->type, false); | |
6d94e741 | 11430 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
11431 | } else { |
11432 | return false; | |
11433 | } | |
11434 | break; | |
11435 | case BPF_JGE: | |
11436 | if ((dst_reg->type == PTR_TO_PACKET && | |
11437 | src_reg->type == PTR_TO_PACKET_END) || | |
11438 | (dst_reg->type == PTR_TO_PACKET_META && | |
11439 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
11440 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
11441 | find_good_pkt_pointers(this_branch, dst_reg, | |
11442 | dst_reg->type, true); | |
6d94e741 | 11443 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
11444 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
11445 | src_reg->type == PTR_TO_PACKET) || | |
11446 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
11447 | src_reg->type == PTR_TO_PACKET_META)) { | |
11448 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
11449 | find_good_pkt_pointers(other_branch, src_reg, | |
11450 | src_reg->type, false); | |
6d94e741 | 11451 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
11452 | } else { |
11453 | return false; | |
11454 | } | |
11455 | break; | |
11456 | case BPF_JLE: | |
11457 | if ((dst_reg->type == PTR_TO_PACKET && | |
11458 | src_reg->type == PTR_TO_PACKET_END) || | |
11459 | (dst_reg->type == PTR_TO_PACKET_META && | |
11460 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
11461 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
11462 | find_good_pkt_pointers(other_branch, dst_reg, | |
11463 | dst_reg->type, false); | |
6d94e741 | 11464 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
11465 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
11466 | src_reg->type == PTR_TO_PACKET) || | |
11467 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
11468 | src_reg->type == PTR_TO_PACKET_META)) { | |
11469 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
11470 | find_good_pkt_pointers(this_branch, src_reg, | |
11471 | src_reg->type, true); | |
6d94e741 | 11472 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
11473 | } else { |
11474 | return false; | |
11475 | } | |
11476 | break; | |
11477 | default: | |
11478 | return false; | |
11479 | } | |
11480 | ||
11481 | return true; | |
11482 | } | |
11483 | ||
75748837 AS |
11484 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
11485 | struct bpf_reg_state *known_reg) | |
11486 | { | |
11487 | struct bpf_func_state *state; | |
11488 | struct bpf_reg_state *reg; | |
75748837 | 11489 | |
b239da34 KKD |
11490 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
11491 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
11492 | *reg = *known_reg; | |
11493 | })); | |
75748837 AS |
11494 | } |
11495 | ||
58e2af8b | 11496 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
11497 | struct bpf_insn *insn, int *insn_idx) |
11498 | { | |
f4d7e40a AS |
11499 | struct bpf_verifier_state *this_branch = env->cur_state; |
11500 | struct bpf_verifier_state *other_branch; | |
11501 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 11502 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
befae758 | 11503 | struct bpf_reg_state *eq_branch_regs; |
17a52670 | 11504 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 11505 | bool is_jmp32; |
fb8d251e | 11506 | int pred = -1; |
17a52670 AS |
11507 | int err; |
11508 | ||
092ed096 JW |
11509 | /* Only conditional jumps are expected to reach here. */ |
11510 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
11511 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
11512 | return -EINVAL; |
11513 | } | |
11514 | ||
11515 | if (BPF_SRC(insn->code) == BPF_X) { | |
11516 | if (insn->imm != 0) { | |
092ed096 | 11517 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
11518 | return -EINVAL; |
11519 | } | |
11520 | ||
11521 | /* check src1 operand */ | |
dc503a8a | 11522 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
11523 | if (err) |
11524 | return err; | |
1be7f75d AS |
11525 | |
11526 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 11527 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
11528 | insn->src_reg); |
11529 | return -EACCES; | |
11530 | } | |
fb8d251e | 11531 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
11532 | } else { |
11533 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 11534 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
11535 | return -EINVAL; |
11536 | } | |
11537 | } | |
11538 | ||
11539 | /* check src2 operand */ | |
dc503a8a | 11540 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11541 | if (err) |
11542 | return err; | |
11543 | ||
1a0dc1ac | 11544 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 11545 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 11546 | |
3f50f132 JF |
11547 | if (BPF_SRC(insn->code) == BPF_K) { |
11548 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
11549 | } else if (src_reg->type == SCALAR_VALUE && | |
11550 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
11551 | pred = is_branch_taken(dst_reg, | |
11552 | tnum_subreg(src_reg->var_off).value, | |
11553 | opcode, | |
11554 | is_jmp32); | |
11555 | } else if (src_reg->type == SCALAR_VALUE && | |
11556 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
11557 | pred = is_branch_taken(dst_reg, | |
11558 | src_reg->var_off.value, | |
11559 | opcode, | |
11560 | is_jmp32); | |
6d94e741 AS |
11561 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
11562 | reg_is_pkt_pointer_any(src_reg) && | |
11563 | !is_jmp32) { | |
11564 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
11565 | } |
11566 | ||
b5dc0163 | 11567 | if (pred >= 0) { |
cac616db JF |
11568 | /* If we get here with a dst_reg pointer type it is because |
11569 | * above is_branch_taken() special cased the 0 comparison. | |
11570 | */ | |
11571 | if (!__is_pointer_value(false, dst_reg)) | |
11572 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
11573 | if (BPF_SRC(insn->code) == BPF_X && !err && |
11574 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
11575 | err = mark_chain_precision(env, insn->src_reg); |
11576 | if (err) | |
11577 | return err; | |
11578 | } | |
9183671a | 11579 | |
fb8d251e | 11580 | if (pred == 1) { |
9183671a DB |
11581 | /* Only follow the goto, ignore fall-through. If needed, push |
11582 | * the fall-through branch for simulation under speculative | |
11583 | * execution. | |
11584 | */ | |
11585 | if (!env->bypass_spec_v1 && | |
11586 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
11587 | *insn_idx)) | |
11588 | return -EFAULT; | |
fb8d251e AS |
11589 | *insn_idx += insn->off; |
11590 | return 0; | |
11591 | } else if (pred == 0) { | |
9183671a DB |
11592 | /* Only follow the fall-through branch, since that's where the |
11593 | * program will go. If needed, push the goto branch for | |
11594 | * simulation under speculative execution. | |
fb8d251e | 11595 | */ |
9183671a DB |
11596 | if (!env->bypass_spec_v1 && |
11597 | !sanitize_speculative_path(env, insn, | |
11598 | *insn_idx + insn->off + 1, | |
11599 | *insn_idx)) | |
11600 | return -EFAULT; | |
fb8d251e | 11601 | return 0; |
17a52670 AS |
11602 | } |
11603 | ||
979d63d5 DB |
11604 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
11605 | false); | |
17a52670 AS |
11606 | if (!other_branch) |
11607 | return -EFAULT; | |
f4d7e40a | 11608 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 11609 | |
48461135 JB |
11610 | /* detect if we are comparing against a constant value so we can adjust |
11611 | * our min/max values for our dst register. | |
f1174f77 | 11612 | * this is only legit if both are scalars (or pointers to the same |
befae758 EZ |
11613 | * object, I suppose, see the PTR_MAYBE_NULL related if block below), |
11614 | * because otherwise the different base pointers mean the offsets aren't | |
f1174f77 | 11615 | * comparable. |
48461135 JB |
11616 | */ |
11617 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 11618 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 11619 | |
f1174f77 | 11620 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
11621 | src_reg->type == SCALAR_VALUE) { |
11622 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
11623 | (is_jmp32 && |
11624 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 11625 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 11626 | dst_reg, |
3f50f132 JF |
11627 | src_reg->var_off.value, |
11628 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
11629 | opcode, is_jmp32); |
11630 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
11631 | (is_jmp32 && |
11632 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 11633 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 11634 | src_reg, |
3f50f132 JF |
11635 | dst_reg->var_off.value, |
11636 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
11637 | opcode, is_jmp32); |
11638 | else if (!is_jmp32 && | |
11639 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 11640 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
11641 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
11642 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 11643 | src_reg, dst_reg, opcode); |
e688c3db AS |
11644 | if (src_reg->id && |
11645 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
11646 | find_equal_scalars(this_branch, src_reg); |
11647 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
11648 | } | |
11649 | ||
f1174f77 EC |
11650 | } |
11651 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 11652 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
11653 | dst_reg, insn->imm, (u32)insn->imm, |
11654 | opcode, is_jmp32); | |
48461135 JB |
11655 | } |
11656 | ||
e688c3db AS |
11657 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
11658 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
11659 | find_equal_scalars(this_branch, dst_reg); |
11660 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
11661 | } | |
11662 | ||
befae758 EZ |
11663 | /* if one pointer register is compared to another pointer |
11664 | * register check if PTR_MAYBE_NULL could be lifted. | |
11665 | * E.g. register A - maybe null | |
11666 | * register B - not null | |
11667 | * for JNE A, B, ... - A is not null in the false branch; | |
11668 | * for JEQ A, B, ... - A is not null in the true branch. | |
11669 | */ | |
11670 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_X && | |
11671 | __is_pointer_value(false, src_reg) && __is_pointer_value(false, dst_reg) && | |
11672 | type_may_be_null(src_reg->type) != type_may_be_null(dst_reg->type)) { | |
11673 | eq_branch_regs = NULL; | |
11674 | switch (opcode) { | |
11675 | case BPF_JEQ: | |
11676 | eq_branch_regs = other_branch_regs; | |
11677 | break; | |
11678 | case BPF_JNE: | |
11679 | eq_branch_regs = regs; | |
11680 | break; | |
11681 | default: | |
11682 | /* do nothing */ | |
11683 | break; | |
11684 | } | |
11685 | if (eq_branch_regs) { | |
11686 | if (type_may_be_null(src_reg->type)) | |
11687 | mark_ptr_not_null_reg(&eq_branch_regs[insn->src_reg]); | |
11688 | else | |
11689 | mark_ptr_not_null_reg(&eq_branch_regs[insn->dst_reg]); | |
11690 | } | |
11691 | } | |
11692 | ||
092ed096 JW |
11693 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
11694 | * NOTE: these optimizations below are related with pointer comparison | |
11695 | * which will never be JMP32. | |
11696 | */ | |
11697 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 11698 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
c25b2ae1 | 11699 | type_may_be_null(dst_reg->type)) { |
840b9615 | 11700 | /* Mark all identical registers in each branch as either |
57a09bf0 TG |
11701 | * safe or unknown depending R == 0 or R != 0 conditional. |
11702 | */ | |
840b9615 JS |
11703 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
11704 | opcode == BPF_JNE); | |
11705 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
11706 | opcode == BPF_JEQ); | |
5beca081 DB |
11707 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
11708 | this_branch, other_branch) && | |
11709 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
11710 | verbose(env, "R%d pointer comparison prohibited\n", |
11711 | insn->dst_reg); | |
1be7f75d | 11712 | return -EACCES; |
17a52670 | 11713 | } |
06ee7115 | 11714 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 11715 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
11716 | return 0; |
11717 | } | |
11718 | ||
17a52670 | 11719 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 11720 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 11721 | { |
d8eca5bb | 11722 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 11723 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 11724 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 11725 | struct bpf_map *map; |
17a52670 AS |
11726 | int err; |
11727 | ||
11728 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 11729 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
11730 | return -EINVAL; |
11731 | } | |
11732 | if (insn->off != 0) { | |
61bd5218 | 11733 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
11734 | return -EINVAL; |
11735 | } | |
11736 | ||
dc503a8a | 11737 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
11738 | if (err) |
11739 | return err; | |
11740 | ||
4976b718 | 11741 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 11742 | if (insn->src_reg == 0) { |
6b173873 JK |
11743 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
11744 | ||
4976b718 | 11745 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 11746 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 11747 | return 0; |
6b173873 | 11748 | } |
17a52670 | 11749 | |
d400a6cf DB |
11750 | /* All special src_reg cases are listed below. From this point onwards |
11751 | * we either succeed and assign a corresponding dst_reg->type after | |
11752 | * zeroing the offset, or fail and reject the program. | |
11753 | */ | |
11754 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 11755 | |
d400a6cf | 11756 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
4976b718 | 11757 | dst_reg->type = aux->btf_var.reg_type; |
34d3a78c | 11758 | switch (base_type(dst_reg->type)) { |
4976b718 HL |
11759 | case PTR_TO_MEM: |
11760 | dst_reg->mem_size = aux->btf_var.mem_size; | |
11761 | break; | |
11762 | case PTR_TO_BTF_ID: | |
22dc4a0f | 11763 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
11764 | dst_reg->btf_id = aux->btf_var.btf_id; |
11765 | break; | |
11766 | default: | |
11767 | verbose(env, "bpf verifier is misconfigured\n"); | |
11768 | return -EFAULT; | |
11769 | } | |
11770 | return 0; | |
11771 | } | |
11772 | ||
69c087ba YS |
11773 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
11774 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
11775 | u32 subprogno = find_subprog(env, |
11776 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
11777 | |
11778 | if (!aux->func_info) { | |
11779 | verbose(env, "missing btf func_info\n"); | |
11780 | return -EINVAL; | |
11781 | } | |
11782 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
11783 | verbose(env, "callback function not static\n"); | |
11784 | return -EINVAL; | |
11785 | } | |
11786 | ||
11787 | dst_reg->type = PTR_TO_FUNC; | |
11788 | dst_reg->subprogno = subprogno; | |
11789 | return 0; | |
11790 | } | |
11791 | ||
d8eca5bb | 11792 | map = env->used_maps[aux->map_index]; |
4976b718 | 11793 | dst_reg->map_ptr = map; |
d8eca5bb | 11794 | |
387544bf AS |
11795 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
11796 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
11797 | dst_reg->type = PTR_TO_MAP_VALUE; |
11798 | dst_reg->off = aux->map_off; | |
d0d78c1d KKD |
11799 | WARN_ON_ONCE(map->max_entries != 1); |
11800 | /* We want reg->id to be same (0) as map_value is not distinct */ | |
387544bf AS |
11801 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
11802 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 11803 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
11804 | } else { |
11805 | verbose(env, "bpf verifier is misconfigured\n"); | |
11806 | return -EINVAL; | |
11807 | } | |
17a52670 | 11808 | |
17a52670 AS |
11809 | return 0; |
11810 | } | |
11811 | ||
96be4325 DB |
11812 | static bool may_access_skb(enum bpf_prog_type type) |
11813 | { | |
11814 | switch (type) { | |
11815 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
11816 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 11817 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
11818 | return true; |
11819 | default: | |
11820 | return false; | |
11821 | } | |
11822 | } | |
11823 | ||
ddd872bc AS |
11824 | /* verify safety of LD_ABS|LD_IND instructions: |
11825 | * - they can only appear in the programs where ctx == skb | |
11826 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
11827 | * preserve R6-R9, and store return value into R0 | |
11828 | * | |
11829 | * Implicit input: | |
11830 | * ctx == skb == R6 == CTX | |
11831 | * | |
11832 | * Explicit input: | |
11833 | * SRC == any register | |
11834 | * IMM == 32-bit immediate | |
11835 | * | |
11836 | * Output: | |
11837 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
11838 | */ | |
58e2af8b | 11839 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 11840 | { |
638f5b90 | 11841 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 11842 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 11843 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
11844 | int i, err; |
11845 | ||
7e40781c | 11846 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 11847 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
11848 | return -EINVAL; |
11849 | } | |
11850 | ||
e0cea7ce DB |
11851 | if (!env->ops->gen_ld_abs) { |
11852 | verbose(env, "bpf verifier is misconfigured\n"); | |
11853 | return -EINVAL; | |
11854 | } | |
11855 | ||
ddd872bc | 11856 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 11857 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 11858 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 11859 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
11860 | return -EINVAL; |
11861 | } | |
11862 | ||
11863 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 11864 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
11865 | if (err) |
11866 | return err; | |
11867 | ||
fd978bf7 JS |
11868 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
11869 | * gen_ld_abs() may terminate the program at runtime, leading to | |
11870 | * reference leak. | |
11871 | */ | |
11872 | err = check_reference_leak(env); | |
11873 | if (err) { | |
11874 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
11875 | return err; | |
11876 | } | |
11877 | ||
d0d78c1d | 11878 | if (env->cur_state->active_lock.ptr) { |
d83525ca AS |
11879 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
11880 | return -EINVAL; | |
11881 | } | |
11882 | ||
9bb00b28 YS |
11883 | if (env->cur_state->active_rcu_lock) { |
11884 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_rcu_read_lock-ed region\n"); | |
11885 | return -EINVAL; | |
11886 | } | |
11887 | ||
6d4f151a | 11888 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
11889 | verbose(env, |
11890 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
11891 | return -EINVAL; |
11892 | } | |
11893 | ||
11894 | if (mode == BPF_IND) { | |
11895 | /* check explicit source operand */ | |
dc503a8a | 11896 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
11897 | if (err) |
11898 | return err; | |
11899 | } | |
11900 | ||
be80a1d3 | 11901 | err = check_ptr_off_reg(env, ®s[ctx_reg], ctx_reg); |
6d4f151a DB |
11902 | if (err < 0) |
11903 | return err; | |
11904 | ||
ddd872bc | 11905 | /* reset caller saved regs to unreadable */ |
dc503a8a | 11906 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 11907 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
11908 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
11909 | } | |
ddd872bc AS |
11910 | |
11911 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
11912 | * the value fetched from the packet. |
11913 | * Already marked as written above. | |
ddd872bc | 11914 | */ |
61bd5218 | 11915 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
11916 | /* ld_abs load up to 32-bit skb data. */ |
11917 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
11918 | return 0; |
11919 | } | |
11920 | ||
390ee7e2 AS |
11921 | static int check_return_code(struct bpf_verifier_env *env) |
11922 | { | |
5cf1e914 | 11923 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 11924 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
11925 | struct bpf_reg_state *reg; |
11926 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 11927 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 11928 | int err; |
bfc6bb74 AS |
11929 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
11930 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 11931 | |
9e4e01df | 11932 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
d1a6edec SF |
11933 | if (!is_subprog) { |
11934 | switch (prog_type) { | |
11935 | case BPF_PROG_TYPE_LSM: | |
11936 | if (prog->expected_attach_type == BPF_LSM_CGROUP) | |
11937 | /* See below, can be 0 or 0-1 depending on hook. */ | |
11938 | break; | |
11939 | fallthrough; | |
11940 | case BPF_PROG_TYPE_STRUCT_OPS: | |
11941 | if (!prog->aux->attach_func_proto->type) | |
11942 | return 0; | |
11943 | break; | |
11944 | default: | |
11945 | break; | |
11946 | } | |
11947 | } | |
27ae7997 | 11948 | |
8fb33b60 | 11949 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
11950 | * to return the value from eBPF program. |
11951 | * Make sure that it's readable at this time | |
11952 | * of bpf_exit, which means that program wrote | |
11953 | * something into it earlier | |
11954 | */ | |
11955 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
11956 | if (err) | |
11957 | return err; | |
11958 | ||
11959 | if (is_pointer_value(env, BPF_REG_0)) { | |
11960 | verbose(env, "R0 leaks addr as return value\n"); | |
11961 | return -EACCES; | |
11962 | } | |
390ee7e2 | 11963 | |
f782e2c3 | 11964 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
11965 | |
11966 | if (frame->in_async_callback_fn) { | |
11967 | /* enforce return zero from async callbacks like timer */ | |
11968 | if (reg->type != SCALAR_VALUE) { | |
11969 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
c25b2ae1 | 11970 | reg_type_str(env, reg->type)); |
bfc6bb74 AS |
11971 | return -EINVAL; |
11972 | } | |
11973 | ||
11974 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
11975 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
11976 | return -EINVAL; | |
11977 | } | |
11978 | return 0; | |
11979 | } | |
11980 | ||
f782e2c3 DB |
11981 | if (is_subprog) { |
11982 | if (reg->type != SCALAR_VALUE) { | |
11983 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
c25b2ae1 | 11984 | reg_type_str(env, reg->type)); |
f782e2c3 DB |
11985 | return -EINVAL; |
11986 | } | |
11987 | return 0; | |
11988 | } | |
11989 | ||
7e40781c | 11990 | switch (prog_type) { |
983695fa DB |
11991 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
11992 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
11993 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
11994 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
11995 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
11996 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
11997 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 11998 | range = tnum_range(1, 1); |
77241217 SF |
11999 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
12000 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
12001 | range = tnum_range(0, 3); | |
ed4ed404 | 12002 | break; |
390ee7e2 | 12003 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 12004 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
12005 | range = tnum_range(0, 3); | |
12006 | enforce_attach_type_range = tnum_range(2, 3); | |
12007 | } | |
ed4ed404 | 12008 | break; |
390ee7e2 AS |
12009 | case BPF_PROG_TYPE_CGROUP_SOCK: |
12010 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 12011 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 12012 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 12013 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 12014 | break; |
15ab09bd AS |
12015 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
12016 | if (!env->prog->aux->attach_btf_id) | |
12017 | return 0; | |
12018 | range = tnum_const(0); | |
12019 | break; | |
15d83c4d | 12020 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
12021 | switch (env->prog->expected_attach_type) { |
12022 | case BPF_TRACE_FENTRY: | |
12023 | case BPF_TRACE_FEXIT: | |
12024 | range = tnum_const(0); | |
12025 | break; | |
12026 | case BPF_TRACE_RAW_TP: | |
12027 | case BPF_MODIFY_RETURN: | |
15d83c4d | 12028 | return 0; |
2ec0616e DB |
12029 | case BPF_TRACE_ITER: |
12030 | break; | |
e92888c7 YS |
12031 | default: |
12032 | return -ENOTSUPP; | |
12033 | } | |
15d83c4d | 12034 | break; |
e9ddbb77 JS |
12035 | case BPF_PROG_TYPE_SK_LOOKUP: |
12036 | range = tnum_range(SK_DROP, SK_PASS); | |
12037 | break; | |
69fd337a SF |
12038 | |
12039 | case BPF_PROG_TYPE_LSM: | |
12040 | if (env->prog->expected_attach_type != BPF_LSM_CGROUP) { | |
12041 | /* Regular BPF_PROG_TYPE_LSM programs can return | |
12042 | * any value. | |
12043 | */ | |
12044 | return 0; | |
12045 | } | |
12046 | if (!env->prog->aux->attach_func_proto->type) { | |
12047 | /* Make sure programs that attach to void | |
12048 | * hooks don't try to modify return value. | |
12049 | */ | |
12050 | range = tnum_range(1, 1); | |
12051 | } | |
12052 | break; | |
12053 | ||
e92888c7 YS |
12054 | case BPF_PROG_TYPE_EXT: |
12055 | /* freplace program can return anything as its return value | |
12056 | * depends on the to-be-replaced kernel func or bpf program. | |
12057 | */ | |
390ee7e2 AS |
12058 | default: |
12059 | return 0; | |
12060 | } | |
12061 | ||
390ee7e2 | 12062 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 12063 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
c25b2ae1 | 12064 | reg_type_str(env, reg->type)); |
390ee7e2 AS |
12065 | return -EINVAL; |
12066 | } | |
12067 | ||
12068 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 12069 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
69fd337a | 12070 | if (prog->expected_attach_type == BPF_LSM_CGROUP && |
d1a6edec | 12071 | prog_type == BPF_PROG_TYPE_LSM && |
69fd337a SF |
12072 | !prog->aux->attach_func_proto->type) |
12073 | verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
390ee7e2 AS |
12074 | return -EINVAL; |
12075 | } | |
5cf1e914 | 12076 | |
12077 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
12078 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
12079 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
12080 | return 0; |
12081 | } | |
12082 | ||
475fb78f AS |
12083 | /* non-recursive DFS pseudo code |
12084 | * 1 procedure DFS-iterative(G,v): | |
12085 | * 2 label v as discovered | |
12086 | * 3 let S be a stack | |
12087 | * 4 S.push(v) | |
12088 | * 5 while S is not empty | |
b6d20799 | 12089 | * 6 t <- S.peek() |
475fb78f AS |
12090 | * 7 if t is what we're looking for: |
12091 | * 8 return t | |
12092 | * 9 for all edges e in G.adjacentEdges(t) do | |
12093 | * 10 if edge e is already labelled | |
12094 | * 11 continue with the next edge | |
12095 | * 12 w <- G.adjacentVertex(t,e) | |
12096 | * 13 if vertex w is not discovered and not explored | |
12097 | * 14 label e as tree-edge | |
12098 | * 15 label w as discovered | |
12099 | * 16 S.push(w) | |
12100 | * 17 continue at 5 | |
12101 | * 18 else if vertex w is discovered | |
12102 | * 19 label e as back-edge | |
12103 | * 20 else | |
12104 | * 21 // vertex w is explored | |
12105 | * 22 label e as forward- or cross-edge | |
12106 | * 23 label t as explored | |
12107 | * 24 S.pop() | |
12108 | * | |
12109 | * convention: | |
12110 | * 0x10 - discovered | |
12111 | * 0x11 - discovered and fall-through edge labelled | |
12112 | * 0x12 - discovered and fall-through and branch edges labelled | |
12113 | * 0x20 - explored | |
12114 | */ | |
12115 | ||
12116 | enum { | |
12117 | DISCOVERED = 0x10, | |
12118 | EXPLORED = 0x20, | |
12119 | FALLTHROUGH = 1, | |
12120 | BRANCH = 2, | |
12121 | }; | |
12122 | ||
dc2a4ebc AS |
12123 | static u32 state_htab_size(struct bpf_verifier_env *env) |
12124 | { | |
12125 | return env->prog->len; | |
12126 | } | |
12127 | ||
5d839021 AS |
12128 | static struct bpf_verifier_state_list **explored_state( |
12129 | struct bpf_verifier_env *env, | |
12130 | int idx) | |
12131 | { | |
dc2a4ebc AS |
12132 | struct bpf_verifier_state *cur = env->cur_state; |
12133 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
12134 | ||
12135 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
12136 | } |
12137 | ||
12138 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
12139 | { | |
a8f500af | 12140 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 12141 | } |
f1bca824 | 12142 | |
59e2e27d WAF |
12143 | enum { |
12144 | DONE_EXPLORING = 0, | |
12145 | KEEP_EXPLORING = 1, | |
12146 | }; | |
12147 | ||
475fb78f AS |
12148 | /* t, w, e - match pseudo-code above: |
12149 | * t - index of current instruction | |
12150 | * w - next instruction | |
12151 | * e - edge | |
12152 | */ | |
2589726d AS |
12153 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
12154 | bool loop_ok) | |
475fb78f | 12155 | { |
7df737e9 AS |
12156 | int *insn_stack = env->cfg.insn_stack; |
12157 | int *insn_state = env->cfg.insn_state; | |
12158 | ||
475fb78f | 12159 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 12160 | return DONE_EXPLORING; |
475fb78f AS |
12161 | |
12162 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 12163 | return DONE_EXPLORING; |
475fb78f AS |
12164 | |
12165 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 12166 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 12167 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
12168 | return -EINVAL; |
12169 | } | |
12170 | ||
f1bca824 AS |
12171 | if (e == BRANCH) |
12172 | /* mark branch target for state pruning */ | |
5d839021 | 12173 | init_explored_state(env, w); |
f1bca824 | 12174 | |
475fb78f AS |
12175 | if (insn_state[w] == 0) { |
12176 | /* tree-edge */ | |
12177 | insn_state[t] = DISCOVERED | e; | |
12178 | insn_state[w] = DISCOVERED; | |
7df737e9 | 12179 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 12180 | return -E2BIG; |
7df737e9 | 12181 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 12182 | return KEEP_EXPLORING; |
475fb78f | 12183 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 12184 | if (loop_ok && env->bpf_capable) |
59e2e27d | 12185 | return DONE_EXPLORING; |
d9762e84 MKL |
12186 | verbose_linfo(env, t, "%d: ", t); |
12187 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 12188 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
12189 | return -EINVAL; |
12190 | } else if (insn_state[w] == EXPLORED) { | |
12191 | /* forward- or cross-edge */ | |
12192 | insn_state[t] = DISCOVERED | e; | |
12193 | } else { | |
61bd5218 | 12194 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
12195 | return -EFAULT; |
12196 | } | |
59e2e27d WAF |
12197 | return DONE_EXPLORING; |
12198 | } | |
12199 | ||
efdb22de YS |
12200 | static int visit_func_call_insn(int t, int insn_cnt, |
12201 | struct bpf_insn *insns, | |
12202 | struct bpf_verifier_env *env, | |
12203 | bool visit_callee) | |
12204 | { | |
12205 | int ret; | |
12206 | ||
12207 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
12208 | if (ret) | |
12209 | return ret; | |
12210 | ||
12211 | if (t + 1 < insn_cnt) | |
12212 | init_explored_state(env, t + 1); | |
12213 | if (visit_callee) { | |
12214 | init_explored_state(env, t); | |
86fc6ee6 AS |
12215 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
12216 | /* It's ok to allow recursion from CFG point of | |
12217 | * view. __check_func_call() will do the actual | |
12218 | * check. | |
12219 | */ | |
12220 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
12221 | } |
12222 | return ret; | |
12223 | } | |
12224 | ||
59e2e27d WAF |
12225 | /* Visits the instruction at index t and returns one of the following: |
12226 | * < 0 - an error occurred | |
12227 | * DONE_EXPLORING - the instruction was fully explored | |
12228 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
12229 | */ | |
12230 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) | |
12231 | { | |
12232 | struct bpf_insn *insns = env->prog->insnsi; | |
12233 | int ret; | |
12234 | ||
69c087ba YS |
12235 | if (bpf_pseudo_func(insns + t)) |
12236 | return visit_func_call_insn(t, insn_cnt, insns, env, true); | |
12237 | ||
59e2e27d WAF |
12238 | /* All non-branch instructions have a single fall-through edge. */ |
12239 | if (BPF_CLASS(insns[t].code) != BPF_JMP && | |
12240 | BPF_CLASS(insns[t].code) != BPF_JMP32) | |
12241 | return push_insn(t, t + 1, FALLTHROUGH, env, false); | |
12242 | ||
12243 | switch (BPF_OP(insns[t].code)) { | |
12244 | case BPF_EXIT: | |
12245 | return DONE_EXPLORING; | |
12246 | ||
12247 | case BPF_CALL: | |
bfc6bb74 AS |
12248 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
12249 | /* Mark this call insn to trigger is_state_visited() check | |
12250 | * before call itself is processed by __check_func_call(). | |
12251 | * Otherwise new async state will be pushed for further | |
12252 | * exploration. | |
12253 | */ | |
12254 | init_explored_state(env, t); | |
efdb22de YS |
12255 | return visit_func_call_insn(t, insn_cnt, insns, env, |
12256 | insns[t].src_reg == BPF_PSEUDO_CALL); | |
59e2e27d WAF |
12257 | |
12258 | case BPF_JA: | |
12259 | if (BPF_SRC(insns[t].code) != BPF_K) | |
12260 | return -EINVAL; | |
12261 | ||
12262 | /* unconditional jump with single edge */ | |
12263 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, | |
12264 | true); | |
12265 | if (ret) | |
12266 | return ret; | |
12267 | ||
12268 | /* unconditional jmp is not a good pruning point, | |
12269 | * but it's marked, since backtracking needs | |
12270 | * to record jmp history in is_state_visited(). | |
12271 | */ | |
12272 | init_explored_state(env, t + insns[t].off + 1); | |
12273 | /* tell verifier to check for equivalent states | |
12274 | * after every call and jump | |
12275 | */ | |
12276 | if (t + 1 < insn_cnt) | |
12277 | init_explored_state(env, t + 1); | |
12278 | ||
12279 | return ret; | |
12280 | ||
12281 | default: | |
12282 | /* conditional jump with two edges */ | |
12283 | init_explored_state(env, t); | |
12284 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); | |
12285 | if (ret) | |
12286 | return ret; | |
12287 | ||
12288 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); | |
12289 | } | |
475fb78f AS |
12290 | } |
12291 | ||
12292 | /* non-recursive depth-first-search to detect loops in BPF program | |
12293 | * loop == back-edge in directed graph | |
12294 | */ | |
58e2af8b | 12295 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 12296 | { |
475fb78f | 12297 | int insn_cnt = env->prog->len; |
7df737e9 | 12298 | int *insn_stack, *insn_state; |
475fb78f | 12299 | int ret = 0; |
59e2e27d | 12300 | int i; |
475fb78f | 12301 | |
7df737e9 | 12302 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
12303 | if (!insn_state) |
12304 | return -ENOMEM; | |
12305 | ||
7df737e9 | 12306 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 12307 | if (!insn_stack) { |
71dde681 | 12308 | kvfree(insn_state); |
475fb78f AS |
12309 | return -ENOMEM; |
12310 | } | |
12311 | ||
12312 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
12313 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 12314 | env->cfg.cur_stack = 1; |
475fb78f | 12315 | |
59e2e27d WAF |
12316 | while (env->cfg.cur_stack > 0) { |
12317 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 12318 | |
59e2e27d WAF |
12319 | ret = visit_insn(t, insn_cnt, env); |
12320 | switch (ret) { | |
12321 | case DONE_EXPLORING: | |
12322 | insn_state[t] = EXPLORED; | |
12323 | env->cfg.cur_stack--; | |
12324 | break; | |
12325 | case KEEP_EXPLORING: | |
12326 | break; | |
12327 | default: | |
12328 | if (ret > 0) { | |
12329 | verbose(env, "visit_insn internal bug\n"); | |
12330 | ret = -EFAULT; | |
475fb78f | 12331 | } |
475fb78f | 12332 | goto err_free; |
59e2e27d | 12333 | } |
475fb78f AS |
12334 | } |
12335 | ||
59e2e27d | 12336 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 12337 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
12338 | ret = -EFAULT; |
12339 | goto err_free; | |
12340 | } | |
475fb78f | 12341 | |
475fb78f AS |
12342 | for (i = 0; i < insn_cnt; i++) { |
12343 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 12344 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
12345 | ret = -EINVAL; |
12346 | goto err_free; | |
12347 | } | |
12348 | } | |
12349 | ret = 0; /* cfg looks good */ | |
12350 | ||
12351 | err_free: | |
71dde681 AS |
12352 | kvfree(insn_state); |
12353 | kvfree(insn_stack); | |
7df737e9 | 12354 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
12355 | return ret; |
12356 | } | |
12357 | ||
09b28d76 AS |
12358 | static int check_abnormal_return(struct bpf_verifier_env *env) |
12359 | { | |
12360 | int i; | |
12361 | ||
12362 | for (i = 1; i < env->subprog_cnt; i++) { | |
12363 | if (env->subprog_info[i].has_ld_abs) { | |
12364 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
12365 | return -EINVAL; | |
12366 | } | |
12367 | if (env->subprog_info[i].has_tail_call) { | |
12368 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
12369 | return -EINVAL; | |
12370 | } | |
12371 | } | |
12372 | return 0; | |
12373 | } | |
12374 | ||
838e9690 YS |
12375 | /* The minimum supported BTF func info size */ |
12376 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
12377 | #define MAX_FUNCINFO_REC_SIZE 252 | |
12378 | ||
c454a46b MKL |
12379 | static int check_btf_func(struct bpf_verifier_env *env, |
12380 | const union bpf_attr *attr, | |
af2ac3e1 | 12381 | bpfptr_t uattr) |
838e9690 | 12382 | { |
09b28d76 | 12383 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 12384 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 12385 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 12386 | struct bpf_func_info *krecord; |
8c1b6e69 | 12387 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
12388 | struct bpf_prog *prog; |
12389 | const struct btf *btf; | |
af2ac3e1 | 12390 | bpfptr_t urecord; |
d0b2818e | 12391 | u32 prev_offset = 0; |
09b28d76 | 12392 | bool scalar_return; |
e7ed83d6 | 12393 | int ret = -ENOMEM; |
838e9690 YS |
12394 | |
12395 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
12396 | if (!nfuncs) { |
12397 | if (check_abnormal_return(env)) | |
12398 | return -EINVAL; | |
838e9690 | 12399 | return 0; |
09b28d76 | 12400 | } |
838e9690 YS |
12401 | |
12402 | if (nfuncs != env->subprog_cnt) { | |
12403 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
12404 | return -EINVAL; | |
12405 | } | |
12406 | ||
12407 | urec_size = attr->func_info_rec_size; | |
12408 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
12409 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
12410 | urec_size % sizeof(u32)) { | |
12411 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
12412 | return -EINVAL; | |
12413 | } | |
12414 | ||
c454a46b MKL |
12415 | prog = env->prog; |
12416 | btf = prog->aux->btf; | |
838e9690 | 12417 | |
af2ac3e1 | 12418 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
12419 | min_size = min_t(u32, krec_size, urec_size); |
12420 | ||
ba64e7d8 | 12421 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
12422 | if (!krecord) |
12423 | return -ENOMEM; | |
8c1b6e69 AS |
12424 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
12425 | if (!info_aux) | |
12426 | goto err_free; | |
ba64e7d8 | 12427 | |
838e9690 YS |
12428 | for (i = 0; i < nfuncs; i++) { |
12429 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
12430 | if (ret) { | |
12431 | if (ret == -E2BIG) { | |
12432 | verbose(env, "nonzero tailing record in func info"); | |
12433 | /* set the size kernel expects so loader can zero | |
12434 | * out the rest of the record. | |
12435 | */ | |
af2ac3e1 AS |
12436 | if (copy_to_bpfptr_offset(uattr, |
12437 | offsetof(union bpf_attr, func_info_rec_size), | |
12438 | &min_size, sizeof(min_size))) | |
838e9690 YS |
12439 | ret = -EFAULT; |
12440 | } | |
c454a46b | 12441 | goto err_free; |
838e9690 YS |
12442 | } |
12443 | ||
af2ac3e1 | 12444 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 12445 | ret = -EFAULT; |
c454a46b | 12446 | goto err_free; |
838e9690 YS |
12447 | } |
12448 | ||
d30d42e0 | 12449 | /* check insn_off */ |
09b28d76 | 12450 | ret = -EINVAL; |
838e9690 | 12451 | if (i == 0) { |
d30d42e0 | 12452 | if (krecord[i].insn_off) { |
838e9690 | 12453 | verbose(env, |
d30d42e0 MKL |
12454 | "nonzero insn_off %u for the first func info record", |
12455 | krecord[i].insn_off); | |
c454a46b | 12456 | goto err_free; |
838e9690 | 12457 | } |
d30d42e0 | 12458 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
12459 | verbose(env, |
12460 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 12461 | krecord[i].insn_off, prev_offset); |
c454a46b | 12462 | goto err_free; |
838e9690 YS |
12463 | } |
12464 | ||
d30d42e0 | 12465 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 12466 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 12467 | goto err_free; |
838e9690 YS |
12468 | } |
12469 | ||
12470 | /* check type_id */ | |
ba64e7d8 | 12471 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 12472 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 12473 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 12474 | krecord[i].type_id); |
c454a46b | 12475 | goto err_free; |
838e9690 | 12476 | } |
51c39bb1 | 12477 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
12478 | |
12479 | func_proto = btf_type_by_id(btf, type->type); | |
12480 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
12481 | /* btf_func_check() already verified it during BTF load */ | |
12482 | goto err_free; | |
12483 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
12484 | scalar_return = | |
6089fb32 | 12485 | btf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type); |
09b28d76 AS |
12486 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
12487 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
12488 | goto err_free; | |
12489 | } | |
12490 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
12491 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
12492 | goto err_free; | |
12493 | } | |
12494 | ||
d30d42e0 | 12495 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 12496 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
12497 | } |
12498 | ||
ba64e7d8 YS |
12499 | prog->aux->func_info = krecord; |
12500 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 12501 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
12502 | return 0; |
12503 | ||
c454a46b | 12504 | err_free: |
ba64e7d8 | 12505 | kvfree(krecord); |
8c1b6e69 | 12506 | kfree(info_aux); |
838e9690 YS |
12507 | return ret; |
12508 | } | |
12509 | ||
ba64e7d8 YS |
12510 | static void adjust_btf_func(struct bpf_verifier_env *env) |
12511 | { | |
8c1b6e69 | 12512 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
12513 | int i; |
12514 | ||
8c1b6e69 | 12515 | if (!aux->func_info) |
ba64e7d8 YS |
12516 | return; |
12517 | ||
12518 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 12519 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
12520 | } |
12521 | ||
1b773d00 | 12522 | #define MIN_BPF_LINEINFO_SIZE offsetofend(struct bpf_line_info, line_col) |
c454a46b MKL |
12523 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
12524 | ||
12525 | static int check_btf_line(struct bpf_verifier_env *env, | |
12526 | const union bpf_attr *attr, | |
af2ac3e1 | 12527 | bpfptr_t uattr) |
c454a46b MKL |
12528 | { |
12529 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
12530 | struct bpf_subprog_info *sub; | |
12531 | struct bpf_line_info *linfo; | |
12532 | struct bpf_prog *prog; | |
12533 | const struct btf *btf; | |
af2ac3e1 | 12534 | bpfptr_t ulinfo; |
c454a46b MKL |
12535 | int err; |
12536 | ||
12537 | nr_linfo = attr->line_info_cnt; | |
12538 | if (!nr_linfo) | |
12539 | return 0; | |
0e6491b5 BC |
12540 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
12541 | return -EINVAL; | |
c454a46b MKL |
12542 | |
12543 | rec_size = attr->line_info_rec_size; | |
12544 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
12545 | rec_size > MAX_LINEINFO_REC_SIZE || | |
12546 | rec_size & (sizeof(u32) - 1)) | |
12547 | return -EINVAL; | |
12548 | ||
12549 | /* Need to zero it in case the userspace may | |
12550 | * pass in a smaller bpf_line_info object. | |
12551 | */ | |
12552 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
12553 | GFP_KERNEL | __GFP_NOWARN); | |
12554 | if (!linfo) | |
12555 | return -ENOMEM; | |
12556 | ||
12557 | prog = env->prog; | |
12558 | btf = prog->aux->btf; | |
12559 | ||
12560 | s = 0; | |
12561 | sub = env->subprog_info; | |
af2ac3e1 | 12562 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
12563 | expected_size = sizeof(struct bpf_line_info); |
12564 | ncopy = min_t(u32, expected_size, rec_size); | |
12565 | for (i = 0; i < nr_linfo; i++) { | |
12566 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
12567 | if (err) { | |
12568 | if (err == -E2BIG) { | |
12569 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
12570 | if (copy_to_bpfptr_offset(uattr, |
12571 | offsetof(union bpf_attr, line_info_rec_size), | |
12572 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
12573 | err = -EFAULT; |
12574 | } | |
12575 | goto err_free; | |
12576 | } | |
12577 | ||
af2ac3e1 | 12578 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
12579 | err = -EFAULT; |
12580 | goto err_free; | |
12581 | } | |
12582 | ||
12583 | /* | |
12584 | * Check insn_off to ensure | |
12585 | * 1) strictly increasing AND | |
12586 | * 2) bounded by prog->len | |
12587 | * | |
12588 | * The linfo[0].insn_off == 0 check logically falls into | |
12589 | * the later "missing bpf_line_info for func..." case | |
12590 | * because the first linfo[0].insn_off must be the | |
12591 | * first sub also and the first sub must have | |
12592 | * subprog_info[0].start == 0. | |
12593 | */ | |
12594 | if ((i && linfo[i].insn_off <= prev_offset) || | |
12595 | linfo[i].insn_off >= prog->len) { | |
12596 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
12597 | i, linfo[i].insn_off, prev_offset, | |
12598 | prog->len); | |
12599 | err = -EINVAL; | |
12600 | goto err_free; | |
12601 | } | |
12602 | ||
fdbaa0be MKL |
12603 | if (!prog->insnsi[linfo[i].insn_off].code) { |
12604 | verbose(env, | |
12605 | "Invalid insn code at line_info[%u].insn_off\n", | |
12606 | i); | |
12607 | err = -EINVAL; | |
12608 | goto err_free; | |
12609 | } | |
12610 | ||
23127b33 MKL |
12611 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
12612 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
12613 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
12614 | err = -EINVAL; | |
12615 | goto err_free; | |
12616 | } | |
12617 | ||
12618 | if (s != env->subprog_cnt) { | |
12619 | if (linfo[i].insn_off == sub[s].start) { | |
12620 | sub[s].linfo_idx = i; | |
12621 | s++; | |
12622 | } else if (sub[s].start < linfo[i].insn_off) { | |
12623 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
12624 | err = -EINVAL; | |
12625 | goto err_free; | |
12626 | } | |
12627 | } | |
12628 | ||
12629 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 12630 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
12631 | } |
12632 | ||
12633 | if (s != env->subprog_cnt) { | |
12634 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
12635 | env->subprog_cnt - s, s); | |
12636 | err = -EINVAL; | |
12637 | goto err_free; | |
12638 | } | |
12639 | ||
12640 | prog->aux->linfo = linfo; | |
12641 | prog->aux->nr_linfo = nr_linfo; | |
12642 | ||
12643 | return 0; | |
12644 | ||
12645 | err_free: | |
12646 | kvfree(linfo); | |
12647 | return err; | |
12648 | } | |
12649 | ||
fbd94c7a AS |
12650 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
12651 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
12652 | ||
12653 | static int check_core_relo(struct bpf_verifier_env *env, | |
12654 | const union bpf_attr *attr, | |
12655 | bpfptr_t uattr) | |
12656 | { | |
12657 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
12658 | struct bpf_core_relo core_relo = {}; | |
12659 | struct bpf_prog *prog = env->prog; | |
12660 | const struct btf *btf = prog->aux->btf; | |
12661 | struct bpf_core_ctx ctx = { | |
12662 | .log = &env->log, | |
12663 | .btf = btf, | |
12664 | }; | |
12665 | bpfptr_t u_core_relo; | |
12666 | int err; | |
12667 | ||
12668 | nr_core_relo = attr->core_relo_cnt; | |
12669 | if (!nr_core_relo) | |
12670 | return 0; | |
12671 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
12672 | return -EINVAL; | |
12673 | ||
12674 | rec_size = attr->core_relo_rec_size; | |
12675 | if (rec_size < MIN_CORE_RELO_SIZE || | |
12676 | rec_size > MAX_CORE_RELO_SIZE || | |
12677 | rec_size % sizeof(u32)) | |
12678 | return -EINVAL; | |
12679 | ||
12680 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
12681 | expected_size = sizeof(struct bpf_core_relo); | |
12682 | ncopy = min_t(u32, expected_size, rec_size); | |
12683 | ||
12684 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
12685 | * relocation record one at a time. | |
12686 | */ | |
12687 | for (i = 0; i < nr_core_relo; i++) { | |
12688 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
12689 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
12690 | if (err) { | |
12691 | if (err == -E2BIG) { | |
12692 | verbose(env, "nonzero tailing record in core_relo"); | |
12693 | if (copy_to_bpfptr_offset(uattr, | |
12694 | offsetof(union bpf_attr, core_relo_rec_size), | |
12695 | &expected_size, sizeof(expected_size))) | |
12696 | err = -EFAULT; | |
12697 | } | |
12698 | break; | |
12699 | } | |
12700 | ||
12701 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
12702 | err = -EFAULT; | |
12703 | break; | |
12704 | } | |
12705 | ||
12706 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
12707 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
12708 | i, core_relo.insn_off, prog->len); | |
12709 | err = -EINVAL; | |
12710 | break; | |
12711 | } | |
12712 | ||
12713 | err = bpf_core_apply(&ctx, &core_relo, i, | |
12714 | &prog->insnsi[core_relo.insn_off / 8]); | |
12715 | if (err) | |
12716 | break; | |
12717 | bpfptr_add(&u_core_relo, rec_size); | |
12718 | } | |
12719 | return err; | |
12720 | } | |
12721 | ||
c454a46b MKL |
12722 | static int check_btf_info(struct bpf_verifier_env *env, |
12723 | const union bpf_attr *attr, | |
af2ac3e1 | 12724 | bpfptr_t uattr) |
c454a46b MKL |
12725 | { |
12726 | struct btf *btf; | |
12727 | int err; | |
12728 | ||
09b28d76 AS |
12729 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
12730 | if (check_abnormal_return(env)) | |
12731 | return -EINVAL; | |
c454a46b | 12732 | return 0; |
09b28d76 | 12733 | } |
c454a46b MKL |
12734 | |
12735 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
12736 | if (IS_ERR(btf)) | |
12737 | return PTR_ERR(btf); | |
350a5c4d AS |
12738 | if (btf_is_kernel(btf)) { |
12739 | btf_put(btf); | |
12740 | return -EACCES; | |
12741 | } | |
c454a46b MKL |
12742 | env->prog->aux->btf = btf; |
12743 | ||
12744 | err = check_btf_func(env, attr, uattr); | |
12745 | if (err) | |
12746 | return err; | |
12747 | ||
12748 | err = check_btf_line(env, attr, uattr); | |
12749 | if (err) | |
12750 | return err; | |
12751 | ||
fbd94c7a AS |
12752 | err = check_core_relo(env, attr, uattr); |
12753 | if (err) | |
12754 | return err; | |
12755 | ||
c454a46b | 12756 | return 0; |
ba64e7d8 YS |
12757 | } |
12758 | ||
f1174f77 EC |
12759 | /* check %cur's range satisfies %old's */ |
12760 | static bool range_within(struct bpf_reg_state *old, | |
12761 | struct bpf_reg_state *cur) | |
12762 | { | |
b03c9f9f EC |
12763 | return old->umin_value <= cur->umin_value && |
12764 | old->umax_value >= cur->umax_value && | |
12765 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
12766 | old->smax_value >= cur->smax_value && |
12767 | old->u32_min_value <= cur->u32_min_value && | |
12768 | old->u32_max_value >= cur->u32_max_value && | |
12769 | old->s32_min_value <= cur->s32_min_value && | |
12770 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
12771 | } |
12772 | ||
f1174f77 EC |
12773 | /* If in the old state two registers had the same id, then they need to have |
12774 | * the same id in the new state as well. But that id could be different from | |
12775 | * the old state, so we need to track the mapping from old to new ids. | |
12776 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
12777 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
12778 | * regs with a different old id could still have new id 9, we don't care about | |
12779 | * that. | |
12780 | * So we look through our idmap to see if this old id has been seen before. If | |
12781 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 12782 | */ |
c9e73e3d | 12783 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 12784 | { |
f1174f77 | 12785 | unsigned int i; |
969bf05e | 12786 | |
c9e73e3d | 12787 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
12788 | if (!idmap[i].old) { |
12789 | /* Reached an empty slot; haven't seen this id before */ | |
12790 | idmap[i].old = old_id; | |
12791 | idmap[i].cur = cur_id; | |
12792 | return true; | |
12793 | } | |
12794 | if (idmap[i].old == old_id) | |
12795 | return idmap[i].cur == cur_id; | |
12796 | } | |
12797 | /* We ran out of idmap slots, which should be impossible */ | |
12798 | WARN_ON_ONCE(1); | |
12799 | return false; | |
12800 | } | |
12801 | ||
9242b5f5 AS |
12802 | static void clean_func_state(struct bpf_verifier_env *env, |
12803 | struct bpf_func_state *st) | |
12804 | { | |
12805 | enum bpf_reg_liveness live; | |
12806 | int i, j; | |
12807 | ||
12808 | for (i = 0; i < BPF_REG_FP; i++) { | |
12809 | live = st->regs[i].live; | |
12810 | /* liveness must not touch this register anymore */ | |
12811 | st->regs[i].live |= REG_LIVE_DONE; | |
12812 | if (!(live & REG_LIVE_READ)) | |
12813 | /* since the register is unused, clear its state | |
12814 | * to make further comparison simpler | |
12815 | */ | |
f54c7898 | 12816 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
12817 | } |
12818 | ||
12819 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
12820 | live = st->stack[i].spilled_ptr.live; | |
12821 | /* liveness must not touch this stack slot anymore */ | |
12822 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
12823 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 12824 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
12825 | for (j = 0; j < BPF_REG_SIZE; j++) |
12826 | st->stack[i].slot_type[j] = STACK_INVALID; | |
12827 | } | |
12828 | } | |
12829 | } | |
12830 | ||
12831 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
12832 | struct bpf_verifier_state *st) | |
12833 | { | |
12834 | int i; | |
12835 | ||
12836 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
12837 | /* all regs in this state in all frames were already marked */ | |
12838 | return; | |
12839 | ||
12840 | for (i = 0; i <= st->curframe; i++) | |
12841 | clean_func_state(env, st->frame[i]); | |
12842 | } | |
12843 | ||
12844 | /* the parentage chains form a tree. | |
12845 | * the verifier states are added to state lists at given insn and | |
12846 | * pushed into state stack for future exploration. | |
12847 | * when the verifier reaches bpf_exit insn some of the verifer states | |
12848 | * stored in the state lists have their final liveness state already, | |
12849 | * but a lot of states will get revised from liveness point of view when | |
12850 | * the verifier explores other branches. | |
12851 | * Example: | |
12852 | * 1: r0 = 1 | |
12853 | * 2: if r1 == 100 goto pc+1 | |
12854 | * 3: r0 = 2 | |
12855 | * 4: exit | |
12856 | * when the verifier reaches exit insn the register r0 in the state list of | |
12857 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
12858 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
12859 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
12860 | * | |
12861 | * Since the verifier pushes the branch states as it sees them while exploring | |
12862 | * the program the condition of walking the branch instruction for the second | |
12863 | * time means that all states below this branch were already explored and | |
8fb33b60 | 12864 | * their final liveness marks are already propagated. |
9242b5f5 AS |
12865 | * Hence when the verifier completes the search of state list in is_state_visited() |
12866 | * we can call this clean_live_states() function to mark all liveness states | |
12867 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
12868 | * will not be used. | |
12869 | * This function also clears the registers and stack for states that !READ | |
12870 | * to simplify state merging. | |
12871 | * | |
12872 | * Important note here that walking the same branch instruction in the callee | |
12873 | * doesn't meant that the states are DONE. The verifier has to compare | |
12874 | * the callsites | |
12875 | */ | |
12876 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
12877 | struct bpf_verifier_state *cur) | |
12878 | { | |
12879 | struct bpf_verifier_state_list *sl; | |
12880 | int i; | |
12881 | ||
5d839021 | 12882 | sl = *explored_state(env, insn); |
a8f500af | 12883 | while (sl) { |
2589726d AS |
12884 | if (sl->state.branches) |
12885 | goto next; | |
dc2a4ebc AS |
12886 | if (sl->state.insn_idx != insn || |
12887 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
12888 | goto next; |
12889 | for (i = 0; i <= cur->curframe; i++) | |
12890 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
12891 | goto next; | |
12892 | clean_verifier_state(env, &sl->state); | |
12893 | next: | |
12894 | sl = sl->next; | |
12895 | } | |
12896 | } | |
12897 | ||
f1174f77 | 12898 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
12899 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
12900 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 12901 | { |
f4d7e40a AS |
12902 | bool equal; |
12903 | ||
dc503a8a EC |
12904 | if (!(rold->live & REG_LIVE_READ)) |
12905 | /* explored state didn't use this */ | |
12906 | return true; | |
12907 | ||
679c782d | 12908 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
12909 | |
12910 | if (rold->type == PTR_TO_STACK) | |
12911 | /* two stack pointers are equal only if they're pointing to | |
12912 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
12913 | */ | |
12914 | return equal && rold->frameno == rcur->frameno; | |
12915 | ||
12916 | if (equal) | |
969bf05e AS |
12917 | return true; |
12918 | ||
f1174f77 EC |
12919 | if (rold->type == NOT_INIT) |
12920 | /* explored state can't have used this */ | |
969bf05e | 12921 | return true; |
f1174f77 EC |
12922 | if (rcur->type == NOT_INIT) |
12923 | return false; | |
c25b2ae1 | 12924 | switch (base_type(rold->type)) { |
f1174f77 | 12925 | case SCALAR_VALUE: |
e042aa53 DB |
12926 | if (env->explore_alu_limits) |
12927 | return false; | |
f1174f77 | 12928 | if (rcur->type == SCALAR_VALUE) { |
f63181b6 | 12929 | if (!rold->precise) |
b5dc0163 | 12930 | return true; |
f1174f77 EC |
12931 | /* new val must satisfy old val knowledge */ |
12932 | return range_within(rold, rcur) && | |
12933 | tnum_in(rold->var_off, rcur->var_off); | |
12934 | } else { | |
179d1c56 JH |
12935 | /* We're trying to use a pointer in place of a scalar. |
12936 | * Even if the scalar was unbounded, this could lead to | |
12937 | * pointer leaks because scalars are allowed to leak | |
12938 | * while pointers are not. We could make this safe in | |
12939 | * special cases if root is calling us, but it's | |
12940 | * probably not worth the hassle. | |
f1174f77 | 12941 | */ |
179d1c56 | 12942 | return false; |
f1174f77 | 12943 | } |
69c087ba | 12944 | case PTR_TO_MAP_KEY: |
f1174f77 | 12945 | case PTR_TO_MAP_VALUE: |
c25b2ae1 HL |
12946 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
12947 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
12948 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
12949 | * checked, doing so could have affected others with the same | |
12950 | * id, and we can't check for that because we lost the id when | |
12951 | * we converted to a PTR_TO_MAP_VALUE. | |
12952 | */ | |
12953 | if (type_may_be_null(rold->type)) { | |
12954 | if (!type_may_be_null(rcur->type)) | |
12955 | return false; | |
12956 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
12957 | return false; | |
12958 | /* Check our ids match any regs they're supposed to */ | |
12959 | return check_ids(rold->id, rcur->id, idmap); | |
12960 | } | |
12961 | ||
1b688a19 EC |
12962 | /* If the new min/max/var_off satisfy the old ones and |
12963 | * everything else matches, we are OK. | |
d83525ca AS |
12964 | * 'id' is not compared, since it's only used for maps with |
12965 | * bpf_spin_lock inside map element and in such cases if | |
12966 | * the rest of the prog is valid for one map element then | |
12967 | * it's valid for all map elements regardless of the key | |
12968 | * used in bpf_map_lookup() | |
1b688a19 EC |
12969 | */ |
12970 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
12971 | range_within(rold, rcur) && | |
12972 | tnum_in(rold->var_off, rcur->var_off); | |
de8f3a83 | 12973 | case PTR_TO_PACKET_META: |
f1174f77 | 12974 | case PTR_TO_PACKET: |
de8f3a83 | 12975 | if (rcur->type != rold->type) |
f1174f77 EC |
12976 | return false; |
12977 | /* We must have at least as much range as the old ptr | |
12978 | * did, so that any accesses which were safe before are | |
12979 | * still safe. This is true even if old range < old off, | |
12980 | * since someone could have accessed through (ptr - k), or | |
12981 | * even done ptr -= k in a register, to get a safe access. | |
12982 | */ | |
12983 | if (rold->range > rcur->range) | |
12984 | return false; | |
12985 | /* If the offsets don't match, we can't trust our alignment; | |
12986 | * nor can we be sure that we won't fall out of range. | |
12987 | */ | |
12988 | if (rold->off != rcur->off) | |
12989 | return false; | |
12990 | /* id relations must be preserved */ | |
12991 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
12992 | return false; | |
12993 | /* new val must satisfy old val knowledge */ | |
12994 | return range_within(rold, rcur) && | |
12995 | tnum_in(rold->var_off, rcur->var_off); | |
12996 | case PTR_TO_CTX: | |
12997 | case CONST_PTR_TO_MAP: | |
f1174f77 | 12998 | case PTR_TO_PACKET_END: |
d58e468b | 12999 | case PTR_TO_FLOW_KEYS: |
c64b7983 | 13000 | case PTR_TO_SOCKET: |
46f8bc92 | 13001 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 13002 | case PTR_TO_TCP_SOCK: |
fada7fdc | 13003 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
13004 | /* Only valid matches are exact, which memcmp() above |
13005 | * would have accepted | |
13006 | */ | |
13007 | default: | |
13008 | /* Don't know what's going on, just say it's not safe */ | |
13009 | return false; | |
13010 | } | |
969bf05e | 13011 | |
f1174f77 EC |
13012 | /* Shouldn't get here; if we do, say it's not safe */ |
13013 | WARN_ON_ONCE(1); | |
969bf05e AS |
13014 | return false; |
13015 | } | |
13016 | ||
e042aa53 DB |
13017 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
13018 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
13019 | { |
13020 | int i, spi; | |
13021 | ||
638f5b90 AS |
13022 | /* walk slots of the explored stack and ignore any additional |
13023 | * slots in the current stack, since explored(safe) state | |
13024 | * didn't use them | |
13025 | */ | |
13026 | for (i = 0; i < old->allocated_stack; i++) { | |
13027 | spi = i / BPF_REG_SIZE; | |
13028 | ||
b233920c AS |
13029 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
13030 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 13031 | /* explored state didn't use this */ |
fd05e57b | 13032 | continue; |
b233920c | 13033 | } |
cc2b14d5 | 13034 | |
638f5b90 AS |
13035 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
13036 | continue; | |
19e2dbb7 AS |
13037 | |
13038 | /* explored stack has more populated slots than current stack | |
13039 | * and these slots were used | |
13040 | */ | |
13041 | if (i >= cur->allocated_stack) | |
13042 | return false; | |
13043 | ||
cc2b14d5 AS |
13044 | /* if old state was safe with misc data in the stack |
13045 | * it will be safe with zero-initialized stack. | |
13046 | * The opposite is not true | |
13047 | */ | |
13048 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
13049 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
13050 | continue; | |
638f5b90 AS |
13051 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
13052 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
13053 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 13054 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
13055 | * this verifier states are not equivalent, |
13056 | * return false to continue verification of this path | |
13057 | */ | |
13058 | return false; | |
27113c59 | 13059 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 13060 | continue; |
27113c59 | 13061 | if (!is_spilled_reg(&old->stack[spi])) |
638f5b90 | 13062 | continue; |
e042aa53 DB |
13063 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
13064 | &cur->stack[spi].spilled_ptr, idmap)) | |
638f5b90 AS |
13065 | /* when explored and current stack slot are both storing |
13066 | * spilled registers, check that stored pointers types | |
13067 | * are the same as well. | |
13068 | * Ex: explored safe path could have stored | |
13069 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
13070 | * but current path has stored: | |
13071 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
13072 | * such verifier states are not equivalent. | |
13073 | * return false to continue verification of this path | |
13074 | */ | |
13075 | return false; | |
13076 | } | |
13077 | return true; | |
13078 | } | |
13079 | ||
fd978bf7 JS |
13080 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
13081 | { | |
13082 | if (old->acquired_refs != cur->acquired_refs) | |
13083 | return false; | |
13084 | return !memcmp(old->refs, cur->refs, | |
13085 | sizeof(*old->refs) * old->acquired_refs); | |
13086 | } | |
13087 | ||
f1bca824 AS |
13088 | /* compare two verifier states |
13089 | * | |
13090 | * all states stored in state_list are known to be valid, since | |
13091 | * verifier reached 'bpf_exit' instruction through them | |
13092 | * | |
13093 | * this function is called when verifier exploring different branches of | |
13094 | * execution popped from the state stack. If it sees an old state that has | |
13095 | * more strict register state and more strict stack state then this execution | |
13096 | * branch doesn't need to be explored further, since verifier already | |
13097 | * concluded that more strict state leads to valid finish. | |
13098 | * | |
13099 | * Therefore two states are equivalent if register state is more conservative | |
13100 | * and explored stack state is more conservative than the current one. | |
13101 | * Example: | |
13102 | * explored current | |
13103 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
13104 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
13105 | * | |
13106 | * In other words if current stack state (one being explored) has more | |
13107 | * valid slots than old one that already passed validation, it means | |
13108 | * the verifier can stop exploring and conclude that current state is valid too | |
13109 | * | |
13110 | * Similarly with registers. If explored state has register type as invalid | |
13111 | * whereas register type in current state is meaningful, it means that | |
13112 | * the current state will reach 'bpf_exit' instruction safely | |
13113 | */ | |
c9e73e3d | 13114 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 13115 | struct bpf_func_state *cur) |
f1bca824 AS |
13116 | { |
13117 | int i; | |
13118 | ||
c9e73e3d LB |
13119 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
13120 | for (i = 0; i < MAX_BPF_REG; i++) | |
e042aa53 DB |
13121 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
13122 | env->idmap_scratch)) | |
c9e73e3d | 13123 | return false; |
f1bca824 | 13124 | |
e042aa53 | 13125 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 13126 | return false; |
fd978bf7 JS |
13127 | |
13128 | if (!refsafe(old, cur)) | |
c9e73e3d LB |
13129 | return false; |
13130 | ||
13131 | return true; | |
f1bca824 AS |
13132 | } |
13133 | ||
f4d7e40a AS |
13134 | static bool states_equal(struct bpf_verifier_env *env, |
13135 | struct bpf_verifier_state *old, | |
13136 | struct bpf_verifier_state *cur) | |
13137 | { | |
13138 | int i; | |
13139 | ||
13140 | if (old->curframe != cur->curframe) | |
13141 | return false; | |
13142 | ||
979d63d5 DB |
13143 | /* Verification state from speculative execution simulation |
13144 | * must never prune a non-speculative execution one. | |
13145 | */ | |
13146 | if (old->speculative && !cur->speculative) | |
13147 | return false; | |
13148 | ||
d0d78c1d KKD |
13149 | if (old->active_lock.ptr != cur->active_lock.ptr || |
13150 | old->active_lock.id != cur->active_lock.id) | |
d83525ca AS |
13151 | return false; |
13152 | ||
9bb00b28 | 13153 | if (old->active_rcu_lock != cur->active_rcu_lock) |
d83525ca AS |
13154 | return false; |
13155 | ||
f4d7e40a AS |
13156 | /* for states to be equal callsites have to be the same |
13157 | * and all frame states need to be equivalent | |
13158 | */ | |
13159 | for (i = 0; i <= old->curframe; i++) { | |
13160 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
13161 | return false; | |
c9e73e3d | 13162 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
13163 | return false; |
13164 | } | |
13165 | return true; | |
13166 | } | |
13167 | ||
5327ed3d JW |
13168 | /* Return 0 if no propagation happened. Return negative error code if error |
13169 | * happened. Otherwise, return the propagated bit. | |
13170 | */ | |
55e7f3b5 JW |
13171 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
13172 | struct bpf_reg_state *reg, | |
13173 | struct bpf_reg_state *parent_reg) | |
13174 | { | |
5327ed3d JW |
13175 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
13176 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
13177 | int err; |
13178 | ||
5327ed3d JW |
13179 | /* When comes here, read flags of PARENT_REG or REG could be any of |
13180 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
13181 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
13182 | */ | |
13183 | if (parent_flag == REG_LIVE_READ64 || | |
13184 | /* Or if there is no read flag from REG. */ | |
13185 | !flag || | |
13186 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
13187 | parent_flag == flag) | |
55e7f3b5 JW |
13188 | return 0; |
13189 | ||
5327ed3d | 13190 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
13191 | if (err) |
13192 | return err; | |
13193 | ||
5327ed3d | 13194 | return flag; |
55e7f3b5 JW |
13195 | } |
13196 | ||
8e9cd9ce | 13197 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
13198 | * straight-line code between a state and its parent. When we arrive at an |
13199 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
13200 | * code, so read marks in the state must propagate to the parent regardless | |
13201 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 13202 | * in mark_reg_read() is for. |
8e9cd9ce | 13203 | */ |
f4d7e40a AS |
13204 | static int propagate_liveness(struct bpf_verifier_env *env, |
13205 | const struct bpf_verifier_state *vstate, | |
13206 | struct bpf_verifier_state *vparent) | |
dc503a8a | 13207 | { |
3f8cafa4 | 13208 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 13209 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 13210 | int i, frame, err = 0; |
dc503a8a | 13211 | |
f4d7e40a AS |
13212 | if (vparent->curframe != vstate->curframe) { |
13213 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
13214 | vparent->curframe, vstate->curframe); | |
13215 | return -EFAULT; | |
13216 | } | |
dc503a8a EC |
13217 | /* Propagate read liveness of registers... */ |
13218 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 13219 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
13220 | parent = vparent->frame[frame]; |
13221 | state = vstate->frame[frame]; | |
13222 | parent_reg = parent->regs; | |
13223 | state_reg = state->regs; | |
83d16312 JK |
13224 | /* We don't need to worry about FP liveness, it's read-only */ |
13225 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
13226 | err = propagate_liveness_reg(env, &state_reg[i], |
13227 | &parent_reg[i]); | |
5327ed3d | 13228 | if (err < 0) |
3f8cafa4 | 13229 | return err; |
5327ed3d JW |
13230 | if (err == REG_LIVE_READ64) |
13231 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 13232 | } |
f4d7e40a | 13233 | |
1b04aee7 | 13234 | /* Propagate stack slots. */ |
f4d7e40a AS |
13235 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
13236 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
13237 | parent_reg = &parent->stack[i].spilled_ptr; |
13238 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
13239 | err = propagate_liveness_reg(env, state_reg, |
13240 | parent_reg); | |
5327ed3d | 13241 | if (err < 0) |
3f8cafa4 | 13242 | return err; |
dc503a8a EC |
13243 | } |
13244 | } | |
5327ed3d | 13245 | return 0; |
dc503a8a EC |
13246 | } |
13247 | ||
a3ce685d AS |
13248 | /* find precise scalars in the previous equivalent state and |
13249 | * propagate them into the current state | |
13250 | */ | |
13251 | static int propagate_precision(struct bpf_verifier_env *env, | |
13252 | const struct bpf_verifier_state *old) | |
13253 | { | |
13254 | struct bpf_reg_state *state_reg; | |
13255 | struct bpf_func_state *state; | |
529409ea | 13256 | int i, err = 0, fr; |
a3ce685d | 13257 | |
529409ea AN |
13258 | for (fr = old->curframe; fr >= 0; fr--) { |
13259 | state = old->frame[fr]; | |
13260 | state_reg = state->regs; | |
13261 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
13262 | if (state_reg->type != SCALAR_VALUE || | |
13263 | !state_reg->precise) | |
13264 | continue; | |
13265 | if (env->log.level & BPF_LOG_LEVEL2) | |
13266 | verbose(env, "frame %d: propagating r%d\n", i, fr); | |
13267 | err = mark_chain_precision_frame(env, fr, i); | |
13268 | if (err < 0) | |
13269 | return err; | |
13270 | } | |
a3ce685d | 13271 | |
529409ea AN |
13272 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
13273 | if (!is_spilled_reg(&state->stack[i])) | |
13274 | continue; | |
13275 | state_reg = &state->stack[i].spilled_ptr; | |
13276 | if (state_reg->type != SCALAR_VALUE || | |
13277 | !state_reg->precise) | |
13278 | continue; | |
13279 | if (env->log.level & BPF_LOG_LEVEL2) | |
13280 | verbose(env, "frame %d: propagating fp%d\n", | |
13281 | (-i - 1) * BPF_REG_SIZE, fr); | |
13282 | err = mark_chain_precision_stack_frame(env, fr, i); | |
13283 | if (err < 0) | |
13284 | return err; | |
13285 | } | |
a3ce685d AS |
13286 | } |
13287 | return 0; | |
13288 | } | |
13289 | ||
2589726d AS |
13290 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
13291 | struct bpf_verifier_state *cur) | |
13292 | { | |
13293 | struct bpf_func_state *fold, *fcur; | |
13294 | int i, fr = cur->curframe; | |
13295 | ||
13296 | if (old->curframe != fr) | |
13297 | return false; | |
13298 | ||
13299 | fold = old->frame[fr]; | |
13300 | fcur = cur->frame[fr]; | |
13301 | for (i = 0; i < MAX_BPF_REG; i++) | |
13302 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
13303 | offsetof(struct bpf_reg_state, parent))) | |
13304 | return false; | |
13305 | return true; | |
13306 | } | |
13307 | ||
13308 | ||
58e2af8b | 13309 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 13310 | { |
58e2af8b | 13311 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 13312 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 13313 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 13314 | int i, j, err, states_cnt = 0; |
10d274e8 | 13315 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 13316 | |
b5dc0163 | 13317 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 13318 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
13319 | /* this 'insn_idx' instruction wasn't marked, so we will not |
13320 | * be doing state search here | |
13321 | */ | |
13322 | return 0; | |
13323 | ||
2589726d AS |
13324 | /* bpf progs typically have pruning point every 4 instructions |
13325 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
13326 | * Do not add new state for future pruning if the verifier hasn't seen | |
13327 | * at least 2 jumps and at least 8 instructions. | |
13328 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
13329 | * In tests that amounts to up to 50% reduction into total verifier | |
13330 | * memory consumption and 20% verifier time speedup. | |
13331 | */ | |
13332 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
13333 | env->insn_processed - env->prev_insn_processed >= 8) | |
13334 | add_new_state = true; | |
13335 | ||
a8f500af AS |
13336 | pprev = explored_state(env, insn_idx); |
13337 | sl = *pprev; | |
13338 | ||
9242b5f5 AS |
13339 | clean_live_states(env, insn_idx, cur); |
13340 | ||
a8f500af | 13341 | while (sl) { |
dc2a4ebc AS |
13342 | states_cnt++; |
13343 | if (sl->state.insn_idx != insn_idx) | |
13344 | goto next; | |
bfc6bb74 | 13345 | |
2589726d | 13346 | if (sl->state.branches) { |
bfc6bb74 AS |
13347 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
13348 | ||
13349 | if (frame->in_async_callback_fn && | |
13350 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
13351 | /* Different async_entry_cnt means that the verifier is | |
13352 | * processing another entry into async callback. | |
13353 | * Seeing the same state is not an indication of infinite | |
13354 | * loop or infinite recursion. | |
13355 | * But finding the same state doesn't mean that it's safe | |
13356 | * to stop processing the current state. The previous state | |
13357 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
13358 | * Checking in_async_callback_fn alone is not enough either. | |
13359 | * Since the verifier still needs to catch infinite loops | |
13360 | * inside async callbacks. | |
13361 | */ | |
13362 | } else if (states_maybe_looping(&sl->state, cur) && | |
13363 | states_equal(env, &sl->state, cur)) { | |
2589726d AS |
13364 | verbose_linfo(env, insn_idx, "; "); |
13365 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
13366 | return -EINVAL; | |
13367 | } | |
13368 | /* if the verifier is processing a loop, avoid adding new state | |
13369 | * too often, since different loop iterations have distinct | |
13370 | * states and may not help future pruning. | |
13371 | * This threshold shouldn't be too low to make sure that | |
13372 | * a loop with large bound will be rejected quickly. | |
13373 | * The most abusive loop will be: | |
13374 | * r1 += 1 | |
13375 | * if r1 < 1000000 goto pc-2 | |
13376 | * 1M insn_procssed limit / 100 == 10k peak states. | |
13377 | * This threshold shouldn't be too high either, since states | |
13378 | * at the end of the loop are likely to be useful in pruning. | |
13379 | */ | |
13380 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
13381 | env->insn_processed - env->prev_insn_processed < 100) | |
13382 | add_new_state = false; | |
13383 | goto miss; | |
13384 | } | |
638f5b90 | 13385 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 13386 | sl->hit_cnt++; |
f1bca824 | 13387 | /* reached equivalent register/stack state, |
dc503a8a EC |
13388 | * prune the search. |
13389 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
13390 | * If we have any write marks in env->cur_state, they |
13391 | * will prevent corresponding reads in the continuation | |
13392 | * from reaching our parent (an explored_state). Our | |
13393 | * own state will get the read marks recorded, but | |
13394 | * they'll be immediately forgotten as we're pruning | |
13395 | * this state and will pop a new one. | |
f1bca824 | 13396 | */ |
f4d7e40a | 13397 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
13398 | |
13399 | /* if previous state reached the exit with precision and | |
13400 | * current state is equivalent to it (except precsion marks) | |
13401 | * the precision needs to be propagated back in | |
13402 | * the current state. | |
13403 | */ | |
13404 | err = err ? : push_jmp_history(env, cur); | |
13405 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
13406 | if (err) |
13407 | return err; | |
f1bca824 | 13408 | return 1; |
dc503a8a | 13409 | } |
2589726d AS |
13410 | miss: |
13411 | /* when new state is not going to be added do not increase miss count. | |
13412 | * Otherwise several loop iterations will remove the state | |
13413 | * recorded earlier. The goal of these heuristics is to have | |
13414 | * states from some iterations of the loop (some in the beginning | |
13415 | * and some at the end) to help pruning. | |
13416 | */ | |
13417 | if (add_new_state) | |
13418 | sl->miss_cnt++; | |
9f4686c4 AS |
13419 | /* heuristic to determine whether this state is beneficial |
13420 | * to keep checking from state equivalence point of view. | |
13421 | * Higher numbers increase max_states_per_insn and verification time, | |
13422 | * but do not meaningfully decrease insn_processed. | |
13423 | */ | |
13424 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
13425 | /* the state is unlikely to be useful. Remove it to | |
13426 | * speed up verification | |
13427 | */ | |
13428 | *pprev = sl->next; | |
13429 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
13430 | u32 br = sl->state.branches; |
13431 | ||
13432 | WARN_ONCE(br, | |
13433 | "BUG live_done but branches_to_explore %d\n", | |
13434 | br); | |
9f4686c4 AS |
13435 | free_verifier_state(&sl->state, false); |
13436 | kfree(sl); | |
13437 | env->peak_states--; | |
13438 | } else { | |
13439 | /* cannot free this state, since parentage chain may | |
13440 | * walk it later. Add it for free_list instead to | |
13441 | * be freed at the end of verification | |
13442 | */ | |
13443 | sl->next = env->free_list; | |
13444 | env->free_list = sl; | |
13445 | } | |
13446 | sl = *pprev; | |
13447 | continue; | |
13448 | } | |
dc2a4ebc | 13449 | next: |
9f4686c4 AS |
13450 | pprev = &sl->next; |
13451 | sl = *pprev; | |
f1bca824 AS |
13452 | } |
13453 | ||
06ee7115 AS |
13454 | if (env->max_states_per_insn < states_cnt) |
13455 | env->max_states_per_insn = states_cnt; | |
13456 | ||
2c78ee89 | 13457 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 13458 | return push_jmp_history(env, cur); |
ceefbc96 | 13459 | |
2589726d | 13460 | if (!add_new_state) |
b5dc0163 | 13461 | return push_jmp_history(env, cur); |
ceefbc96 | 13462 | |
2589726d AS |
13463 | /* There were no equivalent states, remember the current one. |
13464 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 13465 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 13466 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 13467 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
13468 | * again on the way to bpf_exit. |
13469 | * When looping the sl->state.branches will be > 0 and this state | |
13470 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 13471 | */ |
638f5b90 | 13472 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
13473 | if (!new_sl) |
13474 | return -ENOMEM; | |
06ee7115 AS |
13475 | env->total_states++; |
13476 | env->peak_states++; | |
2589726d AS |
13477 | env->prev_jmps_processed = env->jmps_processed; |
13478 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 | 13479 | |
7a830b53 AN |
13480 | /* forget precise markings we inherited, see __mark_chain_precision */ |
13481 | if (env->bpf_capable) | |
13482 | mark_all_scalars_imprecise(env, cur); | |
13483 | ||
f1bca824 | 13484 | /* add new state to the head of linked list */ |
679c782d EC |
13485 | new = &new_sl->state; |
13486 | err = copy_verifier_state(new, cur); | |
1969db47 | 13487 | if (err) { |
679c782d | 13488 | free_verifier_state(new, false); |
1969db47 AS |
13489 | kfree(new_sl); |
13490 | return err; | |
13491 | } | |
dc2a4ebc | 13492 | new->insn_idx = insn_idx; |
2589726d AS |
13493 | WARN_ONCE(new->branches != 1, |
13494 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 13495 | |
2589726d | 13496 | cur->parent = new; |
b5dc0163 AS |
13497 | cur->first_insn_idx = insn_idx; |
13498 | clear_jmp_history(cur); | |
5d839021 AS |
13499 | new_sl->next = *explored_state(env, insn_idx); |
13500 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
13501 | /* connect new state to parentage chain. Current frame needs all |
13502 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
13503 | * to the stack implicitly by JITs) so in callers' frames connect just | |
13504 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
13505 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
13506 | * from callee with its full parentage chain, anyway. | |
13507 | */ | |
8e9cd9ce EC |
13508 | /* clear write marks in current state: the writes we did are not writes |
13509 | * our child did, so they don't screen off its reads from us. | |
13510 | * (There are no read marks in current state, because reads always mark | |
13511 | * their parent and current state never has children yet. Only | |
13512 | * explored_states can get read marks.) | |
13513 | */ | |
eea1c227 AS |
13514 | for (j = 0; j <= cur->curframe; j++) { |
13515 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
13516 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
13517 | for (i = 0; i < BPF_REG_FP; i++) | |
13518 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
13519 | } | |
f4d7e40a AS |
13520 | |
13521 | /* all stack frames are accessible from callee, clear them all */ | |
13522 | for (j = 0; j <= cur->curframe; j++) { | |
13523 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 13524 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 13525 | |
679c782d | 13526 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 13527 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
13528 | frame->stack[i].spilled_ptr.parent = |
13529 | &newframe->stack[i].spilled_ptr; | |
13530 | } | |
f4d7e40a | 13531 | } |
f1bca824 AS |
13532 | return 0; |
13533 | } | |
13534 | ||
c64b7983 JS |
13535 | /* Return true if it's OK to have the same insn return a different type. */ |
13536 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
13537 | { | |
c25b2ae1 | 13538 | switch (base_type(type)) { |
c64b7983 JS |
13539 | case PTR_TO_CTX: |
13540 | case PTR_TO_SOCKET: | |
46f8bc92 | 13541 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 13542 | case PTR_TO_TCP_SOCK: |
fada7fdc | 13543 | case PTR_TO_XDP_SOCK: |
2a02759e | 13544 | case PTR_TO_BTF_ID: |
c64b7983 JS |
13545 | return false; |
13546 | default: | |
13547 | return true; | |
13548 | } | |
13549 | } | |
13550 | ||
13551 | /* If an instruction was previously used with particular pointer types, then we | |
13552 | * need to be careful to avoid cases such as the below, where it may be ok | |
13553 | * for one branch accessing the pointer, but not ok for the other branch: | |
13554 | * | |
13555 | * R1 = sock_ptr | |
13556 | * goto X; | |
13557 | * ... | |
13558 | * R1 = some_other_valid_ptr; | |
13559 | * goto X; | |
13560 | * ... | |
13561 | * R2 = *(u32 *)(R1 + 0); | |
13562 | */ | |
13563 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
13564 | { | |
13565 | return src != prev && (!reg_type_mismatch_ok(src) || | |
13566 | !reg_type_mismatch_ok(prev)); | |
13567 | } | |
13568 | ||
58e2af8b | 13569 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 13570 | { |
6f8a57cc | 13571 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 13572 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 13573 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 13574 | struct bpf_reg_state *regs; |
06ee7115 | 13575 | int insn_cnt = env->prog->len; |
17a52670 | 13576 | bool do_print_state = false; |
b5dc0163 | 13577 | int prev_insn_idx = -1; |
17a52670 | 13578 | |
17a52670 AS |
13579 | for (;;) { |
13580 | struct bpf_insn *insn; | |
13581 | u8 class; | |
13582 | int err; | |
13583 | ||
b5dc0163 | 13584 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 13585 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 13586 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 13587 | env->insn_idx, insn_cnt); |
17a52670 AS |
13588 | return -EFAULT; |
13589 | } | |
13590 | ||
c08435ec | 13591 | insn = &insns[env->insn_idx]; |
17a52670 AS |
13592 | class = BPF_CLASS(insn->code); |
13593 | ||
06ee7115 | 13594 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
13595 | verbose(env, |
13596 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 13597 | env->insn_processed); |
17a52670 AS |
13598 | return -E2BIG; |
13599 | } | |
13600 | ||
c08435ec | 13601 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
13602 | if (err < 0) |
13603 | return err; | |
13604 | if (err == 1) { | |
13605 | /* found equivalent state, can prune the search */ | |
06ee7115 | 13606 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 13607 | if (do_print_state) |
979d63d5 DB |
13608 | verbose(env, "\nfrom %d to %d%s: safe\n", |
13609 | env->prev_insn_idx, env->insn_idx, | |
13610 | env->cur_state->speculative ? | |
13611 | " (speculative execution)" : ""); | |
f1bca824 | 13612 | else |
c08435ec | 13613 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
13614 | } |
13615 | goto process_bpf_exit; | |
13616 | } | |
13617 | ||
c3494801 AS |
13618 | if (signal_pending(current)) |
13619 | return -EAGAIN; | |
13620 | ||
3c2ce60b DB |
13621 | if (need_resched()) |
13622 | cond_resched(); | |
13623 | ||
2e576648 CL |
13624 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
13625 | verbose(env, "\nfrom %d to %d%s:", | |
13626 | env->prev_insn_idx, env->insn_idx, | |
13627 | env->cur_state->speculative ? | |
13628 | " (speculative execution)" : ""); | |
13629 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
13630 | do_print_state = false; |
13631 | } | |
13632 | ||
06ee7115 | 13633 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 13634 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 13635 | .cb_call = disasm_kfunc_name, |
7105e828 | 13636 | .cb_print = verbose, |
abe08840 | 13637 | .private_data = env, |
7105e828 DB |
13638 | }; |
13639 | ||
2e576648 CL |
13640 | if (verifier_state_scratched(env)) |
13641 | print_insn_state(env, state->frame[state->curframe]); | |
13642 | ||
c08435ec | 13643 | verbose_linfo(env, env->insn_idx, "; "); |
2e576648 | 13644 | env->prev_log_len = env->log.len_used; |
c08435ec | 13645 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 13646 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
2e576648 CL |
13647 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
13648 | env->prev_log_len = env->log.len_used; | |
17a52670 AS |
13649 | } |
13650 | ||
cae1927c | 13651 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
13652 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
13653 | env->prev_insn_idx); | |
cae1927c JK |
13654 | if (err) |
13655 | return err; | |
13656 | } | |
13a27dfc | 13657 | |
638f5b90 | 13658 | regs = cur_regs(env); |
fe9a5ca7 | 13659 | sanitize_mark_insn_seen(env); |
b5dc0163 | 13660 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 13661 | |
17a52670 | 13662 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 13663 | err = check_alu_op(env, insn); |
17a52670 AS |
13664 | if (err) |
13665 | return err; | |
13666 | ||
13667 | } else if (class == BPF_LDX) { | |
3df126f3 | 13668 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
13669 | |
13670 | /* check for reserved fields is already done */ | |
13671 | ||
17a52670 | 13672 | /* check src operand */ |
dc503a8a | 13673 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
13674 | if (err) |
13675 | return err; | |
13676 | ||
dc503a8a | 13677 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
13678 | if (err) |
13679 | return err; | |
13680 | ||
725f9dcd AS |
13681 | src_reg_type = regs[insn->src_reg].type; |
13682 | ||
17a52670 AS |
13683 | /* check that memory (src_reg + off) is readable, |
13684 | * the state of dst_reg will be updated by this func | |
13685 | */ | |
c08435ec DB |
13686 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
13687 | insn->off, BPF_SIZE(insn->code), | |
13688 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
13689 | if (err) |
13690 | return err; | |
13691 | ||
c08435ec | 13692 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
13693 | |
13694 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
13695 | /* saw a valid insn |
13696 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 13697 | * save type to validate intersecting paths |
9bac3d6d | 13698 | */ |
3df126f3 | 13699 | *prev_src_type = src_reg_type; |
9bac3d6d | 13700 | |
c64b7983 | 13701 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
13702 | /* ABuser program is trying to use the same insn |
13703 | * dst_reg = *(u32*) (src_reg + off) | |
13704 | * with different pointer types: | |
13705 | * src_reg == ctx in one branch and | |
13706 | * src_reg == stack|map in some other branch. | |
13707 | * Reject it. | |
13708 | */ | |
61bd5218 | 13709 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
13710 | return -EINVAL; |
13711 | } | |
13712 | ||
17a52670 | 13713 | } else if (class == BPF_STX) { |
3df126f3 | 13714 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 13715 | |
91c960b0 BJ |
13716 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
13717 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
13718 | if (err) |
13719 | return err; | |
c08435ec | 13720 | env->insn_idx++; |
17a52670 AS |
13721 | continue; |
13722 | } | |
13723 | ||
5ca419f2 BJ |
13724 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
13725 | verbose(env, "BPF_STX uses reserved fields\n"); | |
13726 | return -EINVAL; | |
13727 | } | |
13728 | ||
17a52670 | 13729 | /* check src1 operand */ |
dc503a8a | 13730 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
13731 | if (err) |
13732 | return err; | |
13733 | /* check src2 operand */ | |
dc503a8a | 13734 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
13735 | if (err) |
13736 | return err; | |
13737 | ||
d691f9e8 AS |
13738 | dst_reg_type = regs[insn->dst_reg].type; |
13739 | ||
17a52670 | 13740 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
13741 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
13742 | insn->off, BPF_SIZE(insn->code), | |
13743 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
13744 | if (err) |
13745 | return err; | |
13746 | ||
c08435ec | 13747 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
13748 | |
13749 | if (*prev_dst_type == NOT_INIT) { | |
13750 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 13751 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 13752 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
13753 | return -EINVAL; |
13754 | } | |
13755 | ||
17a52670 AS |
13756 | } else if (class == BPF_ST) { |
13757 | if (BPF_MODE(insn->code) != BPF_MEM || | |
13758 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 13759 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
13760 | return -EINVAL; |
13761 | } | |
13762 | /* check src operand */ | |
dc503a8a | 13763 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
13764 | if (err) |
13765 | return err; | |
13766 | ||
f37a8cb8 | 13767 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 13768 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f | 13769 | insn->dst_reg, |
c25b2ae1 | 13770 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
13771 | return -EACCES; |
13772 | } | |
13773 | ||
17a52670 | 13774 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
13775 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
13776 | insn->off, BPF_SIZE(insn->code), | |
13777 | BPF_WRITE, -1, false); | |
17a52670 AS |
13778 | if (err) |
13779 | return err; | |
13780 | ||
092ed096 | 13781 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
13782 | u8 opcode = BPF_OP(insn->code); |
13783 | ||
2589726d | 13784 | env->jmps_processed++; |
17a52670 AS |
13785 | if (opcode == BPF_CALL) { |
13786 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
13787 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
13788 | && insn->off != 0) || | |
f4d7e40a | 13789 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
13790 | insn->src_reg != BPF_PSEUDO_CALL && |
13791 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
13792 | insn->dst_reg != BPF_REG_0 || |
13793 | class == BPF_JMP32) { | |
61bd5218 | 13794 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
13795 | return -EINVAL; |
13796 | } | |
13797 | ||
8cab76ec KKD |
13798 | if (env->cur_state->active_lock.ptr) { |
13799 | if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) || | |
13800 | (insn->src_reg == BPF_PSEUDO_CALL) || | |
13801 | (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && | |
13802 | (insn->off != 0 || !is_bpf_list_api_kfunc(insn->imm)))) { | |
13803 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
13804 | return -EINVAL; | |
13805 | } | |
d83525ca | 13806 | } |
f4d7e40a | 13807 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 13808 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 | 13809 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
5c073f26 | 13810 | err = check_kfunc_call(env, insn, &env->insn_idx); |
f4d7e40a | 13811 | else |
69c087ba | 13812 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
13813 | if (err) |
13814 | return err; | |
17a52670 AS |
13815 | } else if (opcode == BPF_JA) { |
13816 | if (BPF_SRC(insn->code) != BPF_K || | |
13817 | insn->imm != 0 || | |
13818 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
13819 | insn->dst_reg != BPF_REG_0 || |
13820 | class == BPF_JMP32) { | |
61bd5218 | 13821 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
13822 | return -EINVAL; |
13823 | } | |
13824 | ||
c08435ec | 13825 | env->insn_idx += insn->off + 1; |
17a52670 AS |
13826 | continue; |
13827 | ||
13828 | } else if (opcode == BPF_EXIT) { | |
13829 | if (BPF_SRC(insn->code) != BPF_K || | |
13830 | insn->imm != 0 || | |
13831 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
13832 | insn->dst_reg != BPF_REG_0 || |
13833 | class == BPF_JMP32) { | |
61bd5218 | 13834 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
13835 | return -EINVAL; |
13836 | } | |
13837 | ||
d0d78c1d | 13838 | if (env->cur_state->active_lock.ptr) { |
d83525ca AS |
13839 | verbose(env, "bpf_spin_unlock is missing\n"); |
13840 | return -EINVAL; | |
13841 | } | |
13842 | ||
9bb00b28 YS |
13843 | if (env->cur_state->active_rcu_lock) { |
13844 | verbose(env, "bpf_rcu_read_unlock is missing\n"); | |
13845 | return -EINVAL; | |
13846 | } | |
13847 | ||
9d9d00ac KKD |
13848 | /* We must do check_reference_leak here before |
13849 | * prepare_func_exit to handle the case when | |
13850 | * state->curframe > 0, it may be a callback | |
13851 | * function, for which reference_state must | |
13852 | * match caller reference state when it exits. | |
13853 | */ | |
13854 | err = check_reference_leak(env); | |
13855 | if (err) | |
13856 | return err; | |
13857 | ||
f4d7e40a AS |
13858 | if (state->curframe) { |
13859 | /* exit from nested function */ | |
c08435ec | 13860 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
13861 | if (err) |
13862 | return err; | |
13863 | do_print_state = true; | |
13864 | continue; | |
13865 | } | |
13866 | ||
390ee7e2 AS |
13867 | err = check_return_code(env); |
13868 | if (err) | |
13869 | return err; | |
f1bca824 | 13870 | process_bpf_exit: |
0f55f9ed | 13871 | mark_verifier_state_scratched(env); |
2589726d | 13872 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 13873 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 13874 | &env->insn_idx, pop_log); |
638f5b90 AS |
13875 | if (err < 0) { |
13876 | if (err != -ENOENT) | |
13877 | return err; | |
17a52670 AS |
13878 | break; |
13879 | } else { | |
13880 | do_print_state = true; | |
13881 | continue; | |
13882 | } | |
13883 | } else { | |
c08435ec | 13884 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
13885 | if (err) |
13886 | return err; | |
13887 | } | |
13888 | } else if (class == BPF_LD) { | |
13889 | u8 mode = BPF_MODE(insn->code); | |
13890 | ||
13891 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
13892 | err = check_ld_abs(env, insn); |
13893 | if (err) | |
13894 | return err; | |
13895 | ||
17a52670 AS |
13896 | } else if (mode == BPF_IMM) { |
13897 | err = check_ld_imm(env, insn); | |
13898 | if (err) | |
13899 | return err; | |
13900 | ||
c08435ec | 13901 | env->insn_idx++; |
fe9a5ca7 | 13902 | sanitize_mark_insn_seen(env); |
17a52670 | 13903 | } else { |
61bd5218 | 13904 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
13905 | return -EINVAL; |
13906 | } | |
13907 | } else { | |
61bd5218 | 13908 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
13909 | return -EINVAL; |
13910 | } | |
13911 | ||
c08435ec | 13912 | env->insn_idx++; |
17a52670 AS |
13913 | } |
13914 | ||
13915 | return 0; | |
13916 | } | |
13917 | ||
541c3bad AN |
13918 | static int find_btf_percpu_datasec(struct btf *btf) |
13919 | { | |
13920 | const struct btf_type *t; | |
13921 | const char *tname; | |
13922 | int i, n; | |
13923 | ||
13924 | /* | |
13925 | * Both vmlinux and module each have their own ".data..percpu" | |
13926 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
13927 | * types to look at only module's own BTF types. | |
13928 | */ | |
13929 | n = btf_nr_types(btf); | |
13930 | if (btf_is_module(btf)) | |
13931 | i = btf_nr_types(btf_vmlinux); | |
13932 | else | |
13933 | i = 1; | |
13934 | ||
13935 | for(; i < n; i++) { | |
13936 | t = btf_type_by_id(btf, i); | |
13937 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
13938 | continue; | |
13939 | ||
13940 | tname = btf_name_by_offset(btf, t->name_off); | |
13941 | if (!strcmp(tname, ".data..percpu")) | |
13942 | return i; | |
13943 | } | |
13944 | ||
13945 | return -ENOENT; | |
13946 | } | |
13947 | ||
4976b718 HL |
13948 | /* replace pseudo btf_id with kernel symbol address */ |
13949 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
13950 | struct bpf_insn *insn, | |
13951 | struct bpf_insn_aux_data *aux) | |
13952 | { | |
eaa6bcb7 HL |
13953 | const struct btf_var_secinfo *vsi; |
13954 | const struct btf_type *datasec; | |
541c3bad | 13955 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
13956 | const struct btf_type *t; |
13957 | const char *sym_name; | |
eaa6bcb7 | 13958 | bool percpu = false; |
f16e6313 | 13959 | u32 type, id = insn->imm; |
541c3bad | 13960 | struct btf *btf; |
f16e6313 | 13961 | s32 datasec_id; |
4976b718 | 13962 | u64 addr; |
541c3bad | 13963 | int i, btf_fd, err; |
4976b718 | 13964 | |
541c3bad AN |
13965 | btf_fd = insn[1].imm; |
13966 | if (btf_fd) { | |
13967 | btf = btf_get_by_fd(btf_fd); | |
13968 | if (IS_ERR(btf)) { | |
13969 | verbose(env, "invalid module BTF object FD specified.\n"); | |
13970 | return -EINVAL; | |
13971 | } | |
13972 | } else { | |
13973 | if (!btf_vmlinux) { | |
13974 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
13975 | return -EINVAL; | |
13976 | } | |
13977 | btf = btf_vmlinux; | |
13978 | btf_get(btf); | |
4976b718 HL |
13979 | } |
13980 | ||
541c3bad | 13981 | t = btf_type_by_id(btf, id); |
4976b718 HL |
13982 | if (!t) { |
13983 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
13984 | err = -ENOENT; |
13985 | goto err_put; | |
4976b718 HL |
13986 | } |
13987 | ||
13988 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
13989 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
13990 | err = -EINVAL; | |
13991 | goto err_put; | |
4976b718 HL |
13992 | } |
13993 | ||
541c3bad | 13994 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
13995 | addr = kallsyms_lookup_name(sym_name); |
13996 | if (!addr) { | |
13997 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
13998 | sym_name); | |
541c3bad AN |
13999 | err = -ENOENT; |
14000 | goto err_put; | |
4976b718 HL |
14001 | } |
14002 | ||
541c3bad | 14003 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 14004 | if (datasec_id > 0) { |
541c3bad | 14005 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
14006 | for_each_vsi(i, datasec, vsi) { |
14007 | if (vsi->type == id) { | |
14008 | percpu = true; | |
14009 | break; | |
14010 | } | |
14011 | } | |
14012 | } | |
14013 | ||
4976b718 HL |
14014 | insn[0].imm = (u32)addr; |
14015 | insn[1].imm = addr >> 32; | |
14016 | ||
14017 | type = t->type; | |
541c3bad | 14018 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 | 14019 | if (percpu) { |
5844101a | 14020 | aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; |
541c3bad | 14021 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
14022 | aux->btf_var.btf_id = type; |
14023 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
14024 | const struct btf_type *ret; |
14025 | const char *tname; | |
14026 | u32 tsize; | |
14027 | ||
14028 | /* resolve the type size of ksym. */ | |
541c3bad | 14029 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 14030 | if (IS_ERR(ret)) { |
541c3bad | 14031 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
14032 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
14033 | tname, PTR_ERR(ret)); | |
541c3bad AN |
14034 | err = -EINVAL; |
14035 | goto err_put; | |
4976b718 | 14036 | } |
34d3a78c | 14037 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
4976b718 HL |
14038 | aux->btf_var.mem_size = tsize; |
14039 | } else { | |
14040 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 14041 | aux->btf_var.btf = btf; |
4976b718 HL |
14042 | aux->btf_var.btf_id = type; |
14043 | } | |
541c3bad AN |
14044 | |
14045 | /* check whether we recorded this BTF (and maybe module) already */ | |
14046 | for (i = 0; i < env->used_btf_cnt; i++) { | |
14047 | if (env->used_btfs[i].btf == btf) { | |
14048 | btf_put(btf); | |
14049 | return 0; | |
14050 | } | |
14051 | } | |
14052 | ||
14053 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
14054 | err = -E2BIG; | |
14055 | goto err_put; | |
14056 | } | |
14057 | ||
14058 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
14059 | btf_mod->btf = btf; | |
14060 | btf_mod->module = NULL; | |
14061 | ||
14062 | /* if we reference variables from kernel module, bump its refcount */ | |
14063 | if (btf_is_module(btf)) { | |
14064 | btf_mod->module = btf_try_get_module(btf); | |
14065 | if (!btf_mod->module) { | |
14066 | err = -ENXIO; | |
14067 | goto err_put; | |
14068 | } | |
14069 | } | |
14070 | ||
14071 | env->used_btf_cnt++; | |
14072 | ||
4976b718 | 14073 | return 0; |
541c3bad AN |
14074 | err_put: |
14075 | btf_put(btf); | |
14076 | return err; | |
4976b718 HL |
14077 | } |
14078 | ||
d83525ca AS |
14079 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
14080 | { | |
14081 | switch (type) { | |
14082 | case BPF_PROG_TYPE_KPROBE: | |
14083 | case BPF_PROG_TYPE_TRACEPOINT: | |
14084 | case BPF_PROG_TYPE_PERF_EVENT: | |
14085 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
5002615a | 14086 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
d83525ca AS |
14087 | return true; |
14088 | default: | |
14089 | return false; | |
14090 | } | |
14091 | } | |
14092 | ||
61bd5218 JK |
14093 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
14094 | struct bpf_map *map, | |
fdc15d38 AS |
14095 | struct bpf_prog *prog) |
14096 | ||
14097 | { | |
7e40781c | 14098 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
a3884572 | 14099 | |
f0c5941f KKD |
14100 | if (btf_record_has_field(map->record, BPF_LIST_HEAD)) { |
14101 | if (is_tracing_prog_type(prog_type)) { | |
14102 | verbose(env, "tracing progs cannot use bpf_list_head yet\n"); | |
14103 | return -EINVAL; | |
14104 | } | |
14105 | } | |
14106 | ||
db559117 | 14107 | if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) { |
9e7a4d98 KS |
14108 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
14109 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
14110 | return -EINVAL; | |
14111 | } | |
14112 | ||
14113 | if (is_tracing_prog_type(prog_type)) { | |
14114 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
14115 | return -EINVAL; | |
14116 | } | |
14117 | ||
14118 | if (prog->aux->sleepable) { | |
14119 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
14120 | return -EINVAL; | |
14121 | } | |
d83525ca AS |
14122 | } |
14123 | ||
db559117 | 14124 | if (btf_record_has_field(map->record, BPF_TIMER)) { |
5e0bc308 DB |
14125 | if (is_tracing_prog_type(prog_type)) { |
14126 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
14127 | return -EINVAL; | |
14128 | } | |
14129 | } | |
14130 | ||
a3884572 | 14131 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 14132 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
14133 | verbose(env, "offload device mismatch between prog and map\n"); |
14134 | return -EINVAL; | |
14135 | } | |
14136 | ||
85d33df3 MKL |
14137 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
14138 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
14139 | return -EINVAL; | |
14140 | } | |
14141 | ||
1e6c62a8 AS |
14142 | if (prog->aux->sleepable) |
14143 | switch (map->map_type) { | |
14144 | case BPF_MAP_TYPE_HASH: | |
14145 | case BPF_MAP_TYPE_LRU_HASH: | |
14146 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
14147 | case BPF_MAP_TYPE_PERCPU_HASH: |
14148 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
14149 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
14150 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
14151 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
ba90c2cc | 14152 | case BPF_MAP_TYPE_RINGBUF: |
583c1f42 | 14153 | case BPF_MAP_TYPE_USER_RINGBUF: |
0fe4b381 KS |
14154 | case BPF_MAP_TYPE_INODE_STORAGE: |
14155 | case BPF_MAP_TYPE_SK_STORAGE: | |
14156 | case BPF_MAP_TYPE_TASK_STORAGE: | |
ba90c2cc | 14157 | break; |
1e6c62a8 AS |
14158 | default: |
14159 | verbose(env, | |
ba90c2cc | 14160 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
1e6c62a8 AS |
14161 | return -EINVAL; |
14162 | } | |
14163 | ||
fdc15d38 AS |
14164 | return 0; |
14165 | } | |
14166 | ||
b741f163 RG |
14167 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
14168 | { | |
14169 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
14170 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
14171 | } | |
14172 | ||
4976b718 HL |
14173 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
14174 | * | |
14175 | * 1. if it accesses map FD, replace it with actual map pointer. | |
14176 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
14177 | * | |
14178 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 14179 | */ |
4976b718 | 14180 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
14181 | { |
14182 | struct bpf_insn *insn = env->prog->insnsi; | |
14183 | int insn_cnt = env->prog->len; | |
fdc15d38 | 14184 | int i, j, err; |
0246e64d | 14185 | |
f1f7714e | 14186 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
14187 | if (err) |
14188 | return err; | |
14189 | ||
0246e64d | 14190 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 14191 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 14192 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 14193 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
14194 | return -EINVAL; |
14195 | } | |
14196 | ||
0246e64d | 14197 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 14198 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
14199 | struct bpf_map *map; |
14200 | struct fd f; | |
d8eca5bb | 14201 | u64 addr; |
387544bf | 14202 | u32 fd; |
0246e64d AS |
14203 | |
14204 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
14205 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
14206 | insn[1].off != 0) { | |
61bd5218 | 14207 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
14208 | return -EINVAL; |
14209 | } | |
14210 | ||
d8eca5bb | 14211 | if (insn[0].src_reg == 0) |
0246e64d AS |
14212 | /* valid generic load 64-bit imm */ |
14213 | goto next_insn; | |
14214 | ||
4976b718 HL |
14215 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
14216 | aux = &env->insn_aux_data[i]; | |
14217 | err = check_pseudo_btf_id(env, insn, aux); | |
14218 | if (err) | |
14219 | return err; | |
14220 | goto next_insn; | |
14221 | } | |
14222 | ||
69c087ba YS |
14223 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
14224 | aux = &env->insn_aux_data[i]; | |
14225 | aux->ptr_type = PTR_TO_FUNC; | |
14226 | goto next_insn; | |
14227 | } | |
14228 | ||
d8eca5bb DB |
14229 | /* In final convert_pseudo_ld_imm64() step, this is |
14230 | * converted into regular 64-bit imm load insn. | |
14231 | */ | |
387544bf AS |
14232 | switch (insn[0].src_reg) { |
14233 | case BPF_PSEUDO_MAP_VALUE: | |
14234 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
14235 | break; | |
14236 | case BPF_PSEUDO_MAP_FD: | |
14237 | case BPF_PSEUDO_MAP_IDX: | |
14238 | if (insn[1].imm == 0) | |
14239 | break; | |
14240 | fallthrough; | |
14241 | default: | |
14242 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
14243 | return -EINVAL; |
14244 | } | |
14245 | ||
387544bf AS |
14246 | switch (insn[0].src_reg) { |
14247 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
14248 | case BPF_PSEUDO_MAP_IDX: | |
14249 | if (bpfptr_is_null(env->fd_array)) { | |
14250 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
14251 | return -EPROTO; | |
14252 | } | |
14253 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
14254 | insn[0].imm * sizeof(fd), | |
14255 | sizeof(fd))) | |
14256 | return -EFAULT; | |
14257 | break; | |
14258 | default: | |
14259 | fd = insn[0].imm; | |
14260 | break; | |
14261 | } | |
14262 | ||
14263 | f = fdget(fd); | |
c2101297 | 14264 | map = __bpf_map_get(f); |
0246e64d | 14265 | if (IS_ERR(map)) { |
61bd5218 | 14266 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 14267 | insn[0].imm); |
0246e64d AS |
14268 | return PTR_ERR(map); |
14269 | } | |
14270 | ||
61bd5218 | 14271 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
14272 | if (err) { |
14273 | fdput(f); | |
14274 | return err; | |
14275 | } | |
14276 | ||
d8eca5bb | 14277 | aux = &env->insn_aux_data[i]; |
387544bf AS |
14278 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
14279 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
14280 | addr = (unsigned long)map; |
14281 | } else { | |
14282 | u32 off = insn[1].imm; | |
14283 | ||
14284 | if (off >= BPF_MAX_VAR_OFF) { | |
14285 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
14286 | fdput(f); | |
14287 | return -EINVAL; | |
14288 | } | |
14289 | ||
14290 | if (!map->ops->map_direct_value_addr) { | |
14291 | verbose(env, "no direct value access support for this map type\n"); | |
14292 | fdput(f); | |
14293 | return -EINVAL; | |
14294 | } | |
14295 | ||
14296 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
14297 | if (err) { | |
14298 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
14299 | map->value_size, off); | |
14300 | fdput(f); | |
14301 | return err; | |
14302 | } | |
14303 | ||
14304 | aux->map_off = off; | |
14305 | addr += off; | |
14306 | } | |
14307 | ||
14308 | insn[0].imm = (u32)addr; | |
14309 | insn[1].imm = addr >> 32; | |
0246e64d AS |
14310 | |
14311 | /* check whether we recorded this map already */ | |
d8eca5bb | 14312 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 14313 | if (env->used_maps[j] == map) { |
d8eca5bb | 14314 | aux->map_index = j; |
0246e64d AS |
14315 | fdput(f); |
14316 | goto next_insn; | |
14317 | } | |
d8eca5bb | 14318 | } |
0246e64d AS |
14319 | |
14320 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
14321 | fdput(f); | |
14322 | return -E2BIG; | |
14323 | } | |
14324 | ||
0246e64d AS |
14325 | /* hold the map. If the program is rejected by verifier, |
14326 | * the map will be released by release_maps() or it | |
14327 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 14328 | * and all maps are released in free_used_maps() |
0246e64d | 14329 | */ |
1e0bd5a0 | 14330 | bpf_map_inc(map); |
d8eca5bb DB |
14331 | |
14332 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
14333 | env->used_maps[env->used_map_cnt++] = map; |
14334 | ||
b741f163 | 14335 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 14336 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 14337 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
14338 | fdput(f); |
14339 | return -EBUSY; | |
14340 | } | |
14341 | ||
0246e64d AS |
14342 | fdput(f); |
14343 | next_insn: | |
14344 | insn++; | |
14345 | i++; | |
5e581dad DB |
14346 | continue; |
14347 | } | |
14348 | ||
14349 | /* Basic sanity check before we invest more work here. */ | |
14350 | if (!bpf_opcode_in_insntable(insn->code)) { | |
14351 | verbose(env, "unknown opcode %02x\n", insn->code); | |
14352 | return -EINVAL; | |
0246e64d AS |
14353 | } |
14354 | } | |
14355 | ||
14356 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
14357 | * 'struct bpf_map *' into a register instead of user map_fd. | |
14358 | * These pointers will be used later by verifier to validate map access. | |
14359 | */ | |
14360 | return 0; | |
14361 | } | |
14362 | ||
14363 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 14364 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 14365 | { |
a2ea0746 DB |
14366 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
14367 | env->used_map_cnt); | |
0246e64d AS |
14368 | } |
14369 | ||
541c3bad AN |
14370 | /* drop refcnt of maps used by the rejected program */ |
14371 | static void release_btfs(struct bpf_verifier_env *env) | |
14372 | { | |
14373 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
14374 | env->used_btf_cnt); | |
14375 | } | |
14376 | ||
0246e64d | 14377 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 14378 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
14379 | { |
14380 | struct bpf_insn *insn = env->prog->insnsi; | |
14381 | int insn_cnt = env->prog->len; | |
14382 | int i; | |
14383 | ||
69c087ba YS |
14384 | for (i = 0; i < insn_cnt; i++, insn++) { |
14385 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
14386 | continue; | |
14387 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
14388 | continue; | |
14389 | insn->src_reg = 0; | |
14390 | } | |
0246e64d AS |
14391 | } |
14392 | ||
8041902d AS |
14393 | /* single env->prog->insni[off] instruction was replaced with the range |
14394 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
14395 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
14396 | */ | |
75f0fc7b HF |
14397 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
14398 | struct bpf_insn_aux_data *new_data, | |
14399 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 14400 | { |
75f0fc7b | 14401 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 14402 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 14403 | u32 old_seen = old_data[off].seen; |
b325fbca | 14404 | u32 prog_len; |
c131187d | 14405 | int i; |
8041902d | 14406 | |
b325fbca JW |
14407 | /* aux info at OFF always needs adjustment, no matter fast path |
14408 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
14409 | * original insn at old prog. | |
14410 | */ | |
14411 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
14412 | ||
8041902d | 14413 | if (cnt == 1) |
75f0fc7b | 14414 | return; |
b325fbca | 14415 | prog_len = new_prog->len; |
75f0fc7b | 14416 | |
8041902d AS |
14417 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
14418 | memcpy(new_data + off + cnt - 1, old_data + off, | |
14419 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 14420 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
14421 | /* Expand insni[off]'s seen count to the patched range. */ |
14422 | new_data[i].seen = old_seen; | |
b325fbca JW |
14423 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
14424 | } | |
8041902d AS |
14425 | env->insn_aux_data = new_data; |
14426 | vfree(old_data); | |
8041902d AS |
14427 | } |
14428 | ||
cc8b0b92 AS |
14429 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
14430 | { | |
14431 | int i; | |
14432 | ||
14433 | if (len == 1) | |
14434 | return; | |
4cb3d99c JW |
14435 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
14436 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 14437 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 14438 | continue; |
9c8105bd | 14439 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
14440 | } |
14441 | } | |
14442 | ||
7506d211 | 14443 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
14444 | { |
14445 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
14446 | int i, sz = prog->aux->size_poke_tab; | |
14447 | struct bpf_jit_poke_descriptor *desc; | |
14448 | ||
14449 | for (i = 0; i < sz; i++) { | |
14450 | desc = &tab[i]; | |
7506d211 JF |
14451 | if (desc->insn_idx <= off) |
14452 | continue; | |
a748c697 MF |
14453 | desc->insn_idx += len - 1; |
14454 | } | |
14455 | } | |
14456 | ||
8041902d AS |
14457 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
14458 | const struct bpf_insn *patch, u32 len) | |
14459 | { | |
14460 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
14461 | struct bpf_insn_aux_data *new_data = NULL; |
14462 | ||
14463 | if (len > 1) { | |
14464 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
14465 | sizeof(struct bpf_insn_aux_data))); | |
14466 | if (!new_data) | |
14467 | return NULL; | |
14468 | } | |
8041902d AS |
14469 | |
14470 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
14471 | if (IS_ERR(new_prog)) { |
14472 | if (PTR_ERR(new_prog) == -ERANGE) | |
14473 | verbose(env, | |
14474 | "insn %d cannot be patched due to 16-bit range\n", | |
14475 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 14476 | vfree(new_data); |
8041902d | 14477 | return NULL; |
4f73379e | 14478 | } |
75f0fc7b | 14479 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 14480 | adjust_subprog_starts(env, off, len); |
7506d211 | 14481 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
14482 | return new_prog; |
14483 | } | |
14484 | ||
52875a04 JK |
14485 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
14486 | u32 off, u32 cnt) | |
14487 | { | |
14488 | int i, j; | |
14489 | ||
14490 | /* find first prog starting at or after off (first to remove) */ | |
14491 | for (i = 0; i < env->subprog_cnt; i++) | |
14492 | if (env->subprog_info[i].start >= off) | |
14493 | break; | |
14494 | /* find first prog starting at or after off + cnt (first to stay) */ | |
14495 | for (j = i; j < env->subprog_cnt; j++) | |
14496 | if (env->subprog_info[j].start >= off + cnt) | |
14497 | break; | |
14498 | /* if j doesn't start exactly at off + cnt, we are just removing | |
14499 | * the front of previous prog | |
14500 | */ | |
14501 | if (env->subprog_info[j].start != off + cnt) | |
14502 | j--; | |
14503 | ||
14504 | if (j > i) { | |
14505 | struct bpf_prog_aux *aux = env->prog->aux; | |
14506 | int move; | |
14507 | ||
14508 | /* move fake 'exit' subprog as well */ | |
14509 | move = env->subprog_cnt + 1 - j; | |
14510 | ||
14511 | memmove(env->subprog_info + i, | |
14512 | env->subprog_info + j, | |
14513 | sizeof(*env->subprog_info) * move); | |
14514 | env->subprog_cnt -= j - i; | |
14515 | ||
14516 | /* remove func_info */ | |
14517 | if (aux->func_info) { | |
14518 | move = aux->func_info_cnt - j; | |
14519 | ||
14520 | memmove(aux->func_info + i, | |
14521 | aux->func_info + j, | |
14522 | sizeof(*aux->func_info) * move); | |
14523 | aux->func_info_cnt -= j - i; | |
14524 | /* func_info->insn_off is set after all code rewrites, | |
14525 | * in adjust_btf_func() - no need to adjust | |
14526 | */ | |
14527 | } | |
14528 | } else { | |
14529 | /* convert i from "first prog to remove" to "first to adjust" */ | |
14530 | if (env->subprog_info[i].start == off) | |
14531 | i++; | |
14532 | } | |
14533 | ||
14534 | /* update fake 'exit' subprog as well */ | |
14535 | for (; i <= env->subprog_cnt; i++) | |
14536 | env->subprog_info[i].start -= cnt; | |
14537 | ||
14538 | return 0; | |
14539 | } | |
14540 | ||
14541 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
14542 | u32 cnt) | |
14543 | { | |
14544 | struct bpf_prog *prog = env->prog; | |
14545 | u32 i, l_off, l_cnt, nr_linfo; | |
14546 | struct bpf_line_info *linfo; | |
14547 | ||
14548 | nr_linfo = prog->aux->nr_linfo; | |
14549 | if (!nr_linfo) | |
14550 | return 0; | |
14551 | ||
14552 | linfo = prog->aux->linfo; | |
14553 | ||
14554 | /* find first line info to remove, count lines to be removed */ | |
14555 | for (i = 0; i < nr_linfo; i++) | |
14556 | if (linfo[i].insn_off >= off) | |
14557 | break; | |
14558 | ||
14559 | l_off = i; | |
14560 | l_cnt = 0; | |
14561 | for (; i < nr_linfo; i++) | |
14562 | if (linfo[i].insn_off < off + cnt) | |
14563 | l_cnt++; | |
14564 | else | |
14565 | break; | |
14566 | ||
14567 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
14568 | * last removed linfo. prog is already modified, so prog->len == off | |
14569 | * means no live instructions after (tail of the program was removed). | |
14570 | */ | |
14571 | if (prog->len != off && l_cnt && | |
14572 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
14573 | l_cnt--; | |
14574 | linfo[--i].insn_off = off + cnt; | |
14575 | } | |
14576 | ||
14577 | /* remove the line info which refer to the removed instructions */ | |
14578 | if (l_cnt) { | |
14579 | memmove(linfo + l_off, linfo + i, | |
14580 | sizeof(*linfo) * (nr_linfo - i)); | |
14581 | ||
14582 | prog->aux->nr_linfo -= l_cnt; | |
14583 | nr_linfo = prog->aux->nr_linfo; | |
14584 | } | |
14585 | ||
14586 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
14587 | for (i = l_off; i < nr_linfo; i++) | |
14588 | linfo[i].insn_off -= cnt; | |
14589 | ||
14590 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
14591 | for (i = 0; i <= env->subprog_cnt; i++) | |
14592 | if (env->subprog_info[i].linfo_idx > l_off) { | |
14593 | /* program may have started in the removed region but | |
14594 | * may not be fully removed | |
14595 | */ | |
14596 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
14597 | env->subprog_info[i].linfo_idx -= l_cnt; | |
14598 | else | |
14599 | env->subprog_info[i].linfo_idx = l_off; | |
14600 | } | |
14601 | ||
14602 | return 0; | |
14603 | } | |
14604 | ||
14605 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
14606 | { | |
14607 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
14608 | unsigned int orig_prog_len = env->prog->len; | |
14609 | int err; | |
14610 | ||
08ca90af JK |
14611 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
14612 | bpf_prog_offload_remove_insns(env, off, cnt); | |
14613 | ||
52875a04 JK |
14614 | err = bpf_remove_insns(env->prog, off, cnt); |
14615 | if (err) | |
14616 | return err; | |
14617 | ||
14618 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
14619 | if (err) | |
14620 | return err; | |
14621 | ||
14622 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
14623 | if (err) | |
14624 | return err; | |
14625 | ||
14626 | memmove(aux_data + off, aux_data + off + cnt, | |
14627 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
14628 | ||
14629 | return 0; | |
14630 | } | |
14631 | ||
2a5418a1 DB |
14632 | /* The verifier does more data flow analysis than llvm and will not |
14633 | * explore branches that are dead at run time. Malicious programs can | |
14634 | * have dead code too. Therefore replace all dead at-run-time code | |
14635 | * with 'ja -1'. | |
14636 | * | |
14637 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
14638 | * program and through another bug we would manage to jump there, then | |
14639 | * we'd execute beyond program memory otherwise. Returning exception | |
14640 | * code also wouldn't work since we can have subprogs where the dead | |
14641 | * code could be located. | |
c131187d AS |
14642 | */ |
14643 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
14644 | { | |
14645 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 14646 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
14647 | struct bpf_insn *insn = env->prog->insnsi; |
14648 | const int insn_cnt = env->prog->len; | |
14649 | int i; | |
14650 | ||
14651 | for (i = 0; i < insn_cnt; i++) { | |
14652 | if (aux_data[i].seen) | |
14653 | continue; | |
2a5418a1 | 14654 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 14655 | aux_data[i].zext_dst = false; |
c131187d AS |
14656 | } |
14657 | } | |
14658 | ||
e2ae4ca2 JK |
14659 | static bool insn_is_cond_jump(u8 code) |
14660 | { | |
14661 | u8 op; | |
14662 | ||
092ed096 JW |
14663 | if (BPF_CLASS(code) == BPF_JMP32) |
14664 | return true; | |
14665 | ||
e2ae4ca2 JK |
14666 | if (BPF_CLASS(code) != BPF_JMP) |
14667 | return false; | |
14668 | ||
14669 | op = BPF_OP(code); | |
14670 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
14671 | } | |
14672 | ||
14673 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
14674 | { | |
14675 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
14676 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
14677 | struct bpf_insn *insn = env->prog->insnsi; | |
14678 | const int insn_cnt = env->prog->len; | |
14679 | int i; | |
14680 | ||
14681 | for (i = 0; i < insn_cnt; i++, insn++) { | |
14682 | if (!insn_is_cond_jump(insn->code)) | |
14683 | continue; | |
14684 | ||
14685 | if (!aux_data[i + 1].seen) | |
14686 | ja.off = insn->off; | |
14687 | else if (!aux_data[i + 1 + insn->off].seen) | |
14688 | ja.off = 0; | |
14689 | else | |
14690 | continue; | |
14691 | ||
08ca90af JK |
14692 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
14693 | bpf_prog_offload_replace_insn(env, i, &ja); | |
14694 | ||
e2ae4ca2 JK |
14695 | memcpy(insn, &ja, sizeof(ja)); |
14696 | } | |
14697 | } | |
14698 | ||
52875a04 JK |
14699 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
14700 | { | |
14701 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
14702 | int insn_cnt = env->prog->len; | |
14703 | int i, err; | |
14704 | ||
14705 | for (i = 0; i < insn_cnt; i++) { | |
14706 | int j; | |
14707 | ||
14708 | j = 0; | |
14709 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
14710 | j++; | |
14711 | if (!j) | |
14712 | continue; | |
14713 | ||
14714 | err = verifier_remove_insns(env, i, j); | |
14715 | if (err) | |
14716 | return err; | |
14717 | insn_cnt = env->prog->len; | |
14718 | } | |
14719 | ||
14720 | return 0; | |
14721 | } | |
14722 | ||
a1b14abc JK |
14723 | static int opt_remove_nops(struct bpf_verifier_env *env) |
14724 | { | |
14725 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
14726 | struct bpf_insn *insn = env->prog->insnsi; | |
14727 | int insn_cnt = env->prog->len; | |
14728 | int i, err; | |
14729 | ||
14730 | for (i = 0; i < insn_cnt; i++) { | |
14731 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
14732 | continue; | |
14733 | ||
14734 | err = verifier_remove_insns(env, i, 1); | |
14735 | if (err) | |
14736 | return err; | |
14737 | insn_cnt--; | |
14738 | i--; | |
14739 | } | |
14740 | ||
14741 | return 0; | |
14742 | } | |
14743 | ||
d6c2308c JW |
14744 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
14745 | const union bpf_attr *attr) | |
a4b1d3c1 | 14746 | { |
d6c2308c | 14747 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 14748 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 14749 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 14750 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 14751 | struct bpf_prog *new_prog; |
d6c2308c | 14752 | bool rnd_hi32; |
a4b1d3c1 | 14753 | |
d6c2308c | 14754 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 14755 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
14756 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
14757 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
14758 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
14759 | for (i = 0; i < len; i++) { |
14760 | int adj_idx = i + delta; | |
14761 | struct bpf_insn insn; | |
83a28819 | 14762 | int load_reg; |
a4b1d3c1 | 14763 | |
d6c2308c | 14764 | insn = insns[adj_idx]; |
83a28819 | 14765 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
14766 | if (!aux[adj_idx].zext_dst) { |
14767 | u8 code, class; | |
14768 | u32 imm_rnd; | |
14769 | ||
14770 | if (!rnd_hi32) | |
14771 | continue; | |
14772 | ||
14773 | code = insn.code; | |
14774 | class = BPF_CLASS(code); | |
83a28819 | 14775 | if (load_reg == -1) |
d6c2308c JW |
14776 | continue; |
14777 | ||
14778 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
14779 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
14780 | * here. | |
d6c2308c | 14781 | */ |
83a28819 | 14782 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
14783 | if (class == BPF_LD && |
14784 | BPF_MODE(code) == BPF_IMM) | |
14785 | i++; | |
14786 | continue; | |
14787 | } | |
14788 | ||
14789 | /* ctx load could be transformed into wider load. */ | |
14790 | if (class == BPF_LDX && | |
14791 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
14792 | continue; | |
14793 | ||
a251c17a | 14794 | imm_rnd = get_random_u32(); |
d6c2308c JW |
14795 | rnd_hi32_patch[0] = insn; |
14796 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 14797 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
14798 | patch = rnd_hi32_patch; |
14799 | patch_len = 4; | |
14800 | goto apply_patch_buffer; | |
14801 | } | |
14802 | ||
39491867 BJ |
14803 | /* Add in an zero-extend instruction if a) the JIT has requested |
14804 | * it or b) it's a CMPXCHG. | |
14805 | * | |
14806 | * The latter is because: BPF_CMPXCHG always loads a value into | |
14807 | * R0, therefore always zero-extends. However some archs' | |
14808 | * equivalent instruction only does this load when the | |
14809 | * comparison is successful. This detail of CMPXCHG is | |
14810 | * orthogonal to the general zero-extension behaviour of the | |
14811 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
14812 | */ | |
14813 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
14814 | continue; |
14815 | ||
83a28819 IL |
14816 | if (WARN_ON(load_reg == -1)) { |
14817 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
14818 | return -EFAULT; | |
b2e37a71 IL |
14819 | } |
14820 | ||
a4b1d3c1 | 14821 | zext_patch[0] = insn; |
b2e37a71 IL |
14822 | zext_patch[1].dst_reg = load_reg; |
14823 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
14824 | patch = zext_patch; |
14825 | patch_len = 2; | |
14826 | apply_patch_buffer: | |
14827 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
14828 | if (!new_prog) |
14829 | return -ENOMEM; | |
14830 | env->prog = new_prog; | |
14831 | insns = new_prog->insnsi; | |
14832 | aux = env->insn_aux_data; | |
d6c2308c | 14833 | delta += patch_len - 1; |
a4b1d3c1 JW |
14834 | } |
14835 | ||
14836 | return 0; | |
14837 | } | |
14838 | ||
c64b7983 JS |
14839 | /* convert load instructions that access fields of a context type into a |
14840 | * sequence of instructions that access fields of the underlying structure: | |
14841 | * struct __sk_buff -> struct sk_buff | |
14842 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 14843 | */ |
58e2af8b | 14844 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 14845 | { |
00176a34 | 14846 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 14847 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 14848 | const int insn_cnt = env->prog->len; |
36bbef52 | 14849 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 14850 | u32 target_size, size_default, off; |
9bac3d6d | 14851 | struct bpf_prog *new_prog; |
d691f9e8 | 14852 | enum bpf_access_type type; |
f96da094 | 14853 | bool is_narrower_load; |
9bac3d6d | 14854 | |
b09928b9 DB |
14855 | if (ops->gen_prologue || env->seen_direct_write) { |
14856 | if (!ops->gen_prologue) { | |
14857 | verbose(env, "bpf verifier is misconfigured\n"); | |
14858 | return -EINVAL; | |
14859 | } | |
36bbef52 DB |
14860 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
14861 | env->prog); | |
14862 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 14863 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
14864 | return -EINVAL; |
14865 | } else if (cnt) { | |
8041902d | 14866 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
14867 | if (!new_prog) |
14868 | return -ENOMEM; | |
8041902d | 14869 | |
36bbef52 | 14870 | env->prog = new_prog; |
3df126f3 | 14871 | delta += cnt - 1; |
36bbef52 DB |
14872 | } |
14873 | } | |
14874 | ||
c64b7983 | 14875 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
14876 | return 0; |
14877 | ||
3df126f3 | 14878 | insn = env->prog->insnsi + delta; |
36bbef52 | 14879 | |
9bac3d6d | 14880 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 | 14881 | bpf_convert_ctx_access_t convert_ctx_access; |
2039f26f | 14882 | bool ctx_access; |
c64b7983 | 14883 | |
62c7989b DB |
14884 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
14885 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
14886 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 14887 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 14888 | type = BPF_READ; |
2039f26f DB |
14889 | ctx_access = true; |
14890 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || | |
14891 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
14892 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
14893 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
14894 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
14895 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
14896 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
14897 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 14898 | type = BPF_WRITE; |
2039f26f DB |
14899 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
14900 | } else { | |
9bac3d6d | 14901 | continue; |
2039f26f | 14902 | } |
9bac3d6d | 14903 | |
af86ca4e | 14904 | if (type == BPF_WRITE && |
2039f26f | 14905 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 14906 | struct bpf_insn patch[] = { |
af86ca4e | 14907 | *insn, |
2039f26f | 14908 | BPF_ST_NOSPEC(), |
af86ca4e AS |
14909 | }; |
14910 | ||
14911 | cnt = ARRAY_SIZE(patch); | |
14912 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
14913 | if (!new_prog) | |
14914 | return -ENOMEM; | |
14915 | ||
14916 | delta += cnt - 1; | |
14917 | env->prog = new_prog; | |
14918 | insn = new_prog->insnsi + i + delta; | |
14919 | continue; | |
14920 | } | |
14921 | ||
2039f26f DB |
14922 | if (!ctx_access) |
14923 | continue; | |
14924 | ||
6efe152d | 14925 | switch ((int)env->insn_aux_data[i + delta].ptr_type) { |
c64b7983 JS |
14926 | case PTR_TO_CTX: |
14927 | if (!ops->convert_ctx_access) | |
14928 | continue; | |
14929 | convert_ctx_access = ops->convert_ctx_access; | |
14930 | break; | |
14931 | case PTR_TO_SOCKET: | |
46f8bc92 | 14932 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
14933 | convert_ctx_access = bpf_sock_convert_ctx_access; |
14934 | break; | |
655a51e5 MKL |
14935 | case PTR_TO_TCP_SOCK: |
14936 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
14937 | break; | |
fada7fdc JL |
14938 | case PTR_TO_XDP_SOCK: |
14939 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
14940 | break; | |
2a02759e | 14941 | case PTR_TO_BTF_ID: |
6efe152d | 14942 | case PTR_TO_BTF_ID | PTR_UNTRUSTED: |
282de143 KKD |
14943 | /* PTR_TO_BTF_ID | MEM_ALLOC always has a valid lifetime, unlike |
14944 | * PTR_TO_BTF_ID, and an active ref_obj_id, but the same cannot | |
14945 | * be said once it is marked PTR_UNTRUSTED, hence we must handle | |
14946 | * any faults for loads into such types. BPF_WRITE is disallowed | |
14947 | * for this case. | |
14948 | */ | |
14949 | case PTR_TO_BTF_ID | MEM_ALLOC | PTR_UNTRUSTED: | |
27ae7997 MKL |
14950 | if (type == BPF_READ) { |
14951 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
14952 | BPF_SIZE((insn)->code); | |
14953 | env->prog->aux->num_exentries++; | |
2a02759e | 14954 | } |
2a02759e | 14955 | continue; |
c64b7983 | 14956 | default: |
9bac3d6d | 14957 | continue; |
c64b7983 | 14958 | } |
9bac3d6d | 14959 | |
31fd8581 | 14960 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 14961 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
14962 | |
14963 | /* If the read access is a narrower load of the field, | |
14964 | * convert to a 4/8-byte load, to minimum program type specific | |
14965 | * convert_ctx_access changes. If conversion is successful, | |
14966 | * we will apply proper mask to the result. | |
14967 | */ | |
f96da094 | 14968 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
14969 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
14970 | off = insn->off; | |
31fd8581 | 14971 | if (is_narrower_load) { |
f96da094 DB |
14972 | u8 size_code; |
14973 | ||
14974 | if (type == BPF_WRITE) { | |
61bd5218 | 14975 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
14976 | return -EINVAL; |
14977 | } | |
31fd8581 | 14978 | |
f96da094 | 14979 | size_code = BPF_H; |
31fd8581 YS |
14980 | if (ctx_field_size == 4) |
14981 | size_code = BPF_W; | |
14982 | else if (ctx_field_size == 8) | |
14983 | size_code = BPF_DW; | |
f96da094 | 14984 | |
bc23105c | 14985 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
14986 | insn->code = BPF_LDX | BPF_MEM | size_code; |
14987 | } | |
f96da094 DB |
14988 | |
14989 | target_size = 0; | |
c64b7983 JS |
14990 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
14991 | &target_size); | |
f96da094 DB |
14992 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
14993 | (ctx_field_size && !target_size)) { | |
61bd5218 | 14994 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
14995 | return -EINVAL; |
14996 | } | |
f96da094 DB |
14997 | |
14998 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
14999 | u8 shift = bpf_ctx_narrow_access_offset( |
15000 | off, size, size_default) * 8; | |
d7af7e49 AI |
15001 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
15002 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
15003 | return -EINVAL; | |
15004 | } | |
46f53a65 AI |
15005 | if (ctx_field_size <= 4) { |
15006 | if (shift) | |
15007 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
15008 | insn->dst_reg, | |
15009 | shift); | |
31fd8581 | 15010 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 15011 | (1 << size * 8) - 1); |
46f53a65 AI |
15012 | } else { |
15013 | if (shift) | |
15014 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
15015 | insn->dst_reg, | |
15016 | shift); | |
31fd8581 | 15017 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 15018 | (1ULL << size * 8) - 1); |
46f53a65 | 15019 | } |
31fd8581 | 15020 | } |
9bac3d6d | 15021 | |
8041902d | 15022 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
15023 | if (!new_prog) |
15024 | return -ENOMEM; | |
15025 | ||
3df126f3 | 15026 | delta += cnt - 1; |
9bac3d6d AS |
15027 | |
15028 | /* keep walking new program and skip insns we just inserted */ | |
15029 | env->prog = new_prog; | |
3df126f3 | 15030 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
15031 | } |
15032 | ||
15033 | return 0; | |
15034 | } | |
15035 | ||
1c2a088a AS |
15036 | static int jit_subprogs(struct bpf_verifier_env *env) |
15037 | { | |
15038 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
15039 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 15040 | struct bpf_map *map_ptr; |
7105e828 | 15041 | struct bpf_insn *insn; |
1c2a088a | 15042 | void *old_bpf_func; |
c4c0bdc0 | 15043 | int err, num_exentries; |
1c2a088a | 15044 | |
f910cefa | 15045 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
15046 | return 0; |
15047 | ||
7105e828 | 15048 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 15049 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 15050 | continue; |
69c087ba | 15051 | |
c7a89784 DB |
15052 | /* Upon error here we cannot fall back to interpreter but |
15053 | * need a hard reject of the program. Thus -EFAULT is | |
15054 | * propagated in any case. | |
15055 | */ | |
1c2a088a AS |
15056 | subprog = find_subprog(env, i + insn->imm + 1); |
15057 | if (subprog < 0) { | |
15058 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
15059 | i + insn->imm + 1); | |
15060 | return -EFAULT; | |
15061 | } | |
15062 | /* temporarily remember subprog id inside insn instead of | |
15063 | * aux_data, since next loop will split up all insns into funcs | |
15064 | */ | |
f910cefa | 15065 | insn->off = subprog; |
1c2a088a AS |
15066 | /* remember original imm in case JIT fails and fallback |
15067 | * to interpreter will be needed | |
15068 | */ | |
15069 | env->insn_aux_data[i].call_imm = insn->imm; | |
15070 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
15071 | insn->imm = 1; | |
3990ed4c MKL |
15072 | if (bpf_pseudo_func(insn)) |
15073 | /* jit (e.g. x86_64) may emit fewer instructions | |
15074 | * if it learns a u32 imm is the same as a u64 imm. | |
15075 | * Force a non zero here. | |
15076 | */ | |
15077 | insn[1].imm = 1; | |
1c2a088a AS |
15078 | } |
15079 | ||
c454a46b MKL |
15080 | err = bpf_prog_alloc_jited_linfo(prog); |
15081 | if (err) | |
15082 | goto out_undo_insn; | |
15083 | ||
15084 | err = -ENOMEM; | |
6396bb22 | 15085 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 15086 | if (!func) |
c7a89784 | 15087 | goto out_undo_insn; |
1c2a088a | 15088 | |
f910cefa | 15089 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 15090 | subprog_start = subprog_end; |
4cb3d99c | 15091 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
15092 | |
15093 | len = subprog_end - subprog_start; | |
fb7dd8bc | 15094 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
15095 | * hence main prog stats include the runtime of subprogs. |
15096 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 15097 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
15098 | */ |
15099 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
15100 | if (!func[i]) |
15101 | goto out_free; | |
15102 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
15103 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 15104 | func[i]->type = prog->type; |
1c2a088a | 15105 | func[i]->len = len; |
4f74d809 DB |
15106 | if (bpf_prog_calc_tag(func[i])) |
15107 | goto out_free; | |
1c2a088a | 15108 | func[i]->is_func = 1; |
ba64e7d8 | 15109 | func[i]->aux->func_idx = i; |
f263a814 | 15110 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
15111 | func[i]->aux->btf = prog->aux->btf; |
15112 | func[i]->aux->func_info = prog->aux->func_info; | |
9c7c48d6 | 15113 | func[i]->aux->func_info_cnt = prog->aux->func_info_cnt; |
f263a814 JF |
15114 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
15115 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 15116 | |
a748c697 | 15117 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 15118 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 15119 | |
f263a814 JF |
15120 | poke = &prog->aux->poke_tab[j]; |
15121 | if (poke->insn_idx < subprog_end && | |
15122 | poke->insn_idx >= subprog_start) | |
15123 | poke->aux = func[i]->aux; | |
a748c697 MF |
15124 | } |
15125 | ||
1c2a088a | 15126 | func[i]->aux->name[0] = 'F'; |
9c8105bd | 15127 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 15128 | func[i]->jit_requested = 1; |
d2a3b7c5 | 15129 | func[i]->blinding_requested = prog->blinding_requested; |
e6ac2450 | 15130 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 15131 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
15132 | func[i]->aux->linfo = prog->aux->linfo; |
15133 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
15134 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
15135 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
15136 | num_exentries = 0; |
15137 | insn = func[i]->insnsi; | |
15138 | for (j = 0; j < func[i]->len; j++, insn++) { | |
15139 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
15140 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
15141 | num_exentries++; | |
15142 | } | |
15143 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 15144 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
15145 | func[i] = bpf_int_jit_compile(func[i]); |
15146 | if (!func[i]->jited) { | |
15147 | err = -ENOTSUPP; | |
15148 | goto out_free; | |
15149 | } | |
15150 | cond_resched(); | |
15151 | } | |
a748c697 | 15152 | |
1c2a088a AS |
15153 | /* at this point all bpf functions were successfully JITed |
15154 | * now populate all bpf_calls with correct addresses and | |
15155 | * run last pass of JIT | |
15156 | */ | |
f910cefa | 15157 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
15158 | insn = func[i]->insnsi; |
15159 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 15160 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 15161 | subprog = insn->off; |
69c087ba YS |
15162 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
15163 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
15164 | continue; | |
15165 | } | |
23a2d70c | 15166 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
15167 | continue; |
15168 | subprog = insn->off; | |
3d717fad | 15169 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 15170 | } |
2162fed4 SD |
15171 | |
15172 | /* we use the aux data to keep a list of the start addresses | |
15173 | * of the JITed images for each function in the program | |
15174 | * | |
15175 | * for some architectures, such as powerpc64, the imm field | |
15176 | * might not be large enough to hold the offset of the start | |
15177 | * address of the callee's JITed image from __bpf_call_base | |
15178 | * | |
15179 | * in such cases, we can lookup the start address of a callee | |
15180 | * by using its subprog id, available from the off field of | |
15181 | * the call instruction, as an index for this list | |
15182 | */ | |
15183 | func[i]->aux->func = func; | |
15184 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 15185 | } |
f910cefa | 15186 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
15187 | old_bpf_func = func[i]->bpf_func; |
15188 | tmp = bpf_int_jit_compile(func[i]); | |
15189 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
15190 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 15191 | err = -ENOTSUPP; |
1c2a088a AS |
15192 | goto out_free; |
15193 | } | |
15194 | cond_resched(); | |
15195 | } | |
15196 | ||
15197 | /* finally lock prog and jit images for all functions and | |
15198 | * populate kallsysm | |
15199 | */ | |
f910cefa | 15200 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
15201 | bpf_prog_lock_ro(func[i]); |
15202 | bpf_prog_kallsyms_add(func[i]); | |
15203 | } | |
7105e828 DB |
15204 | |
15205 | /* Last step: make now unused interpreter insns from main | |
15206 | * prog consistent for later dump requests, so they can | |
15207 | * later look the same as if they were interpreted only. | |
15208 | */ | |
15209 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
15210 | if (bpf_pseudo_func(insn)) { |
15211 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
15212 | insn[1].imm = insn->off; |
15213 | insn->off = 0; | |
69c087ba YS |
15214 | continue; |
15215 | } | |
23a2d70c | 15216 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
15217 | continue; |
15218 | insn->off = env->insn_aux_data[i].call_imm; | |
15219 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 15220 | insn->imm = subprog; |
7105e828 DB |
15221 | } |
15222 | ||
1c2a088a AS |
15223 | prog->jited = 1; |
15224 | prog->bpf_func = func[0]->bpf_func; | |
d00c6473 | 15225 | prog->jited_len = func[0]->jited_len; |
1c2a088a | 15226 | prog->aux->func = func; |
f910cefa | 15227 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 15228 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
15229 | return 0; |
15230 | out_free: | |
f263a814 JF |
15231 | /* We failed JIT'ing, so at this point we need to unregister poke |
15232 | * descriptors from subprogs, so that kernel is not attempting to | |
15233 | * patch it anymore as we're freeing the subprog JIT memory. | |
15234 | */ | |
15235 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
15236 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
15237 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
15238 | } | |
15239 | /* At this point we're guaranteed that poke descriptors are not | |
15240 | * live anymore. We can just unlink its descriptor table as it's | |
15241 | * released with the main prog. | |
15242 | */ | |
a748c697 MF |
15243 | for (i = 0; i < env->subprog_cnt; i++) { |
15244 | if (!func[i]) | |
15245 | continue; | |
f263a814 | 15246 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
15247 | bpf_jit_free(func[i]); |
15248 | } | |
1c2a088a | 15249 | kfree(func); |
c7a89784 | 15250 | out_undo_insn: |
1c2a088a AS |
15251 | /* cleanup main prog to be interpreted */ |
15252 | prog->jit_requested = 0; | |
d2a3b7c5 | 15253 | prog->blinding_requested = 0; |
1c2a088a | 15254 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
23a2d70c | 15255 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
15256 | continue; |
15257 | insn->off = 0; | |
15258 | insn->imm = env->insn_aux_data[i].call_imm; | |
15259 | } | |
e16301fb | 15260 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
15261 | return err; |
15262 | } | |
15263 | ||
1ea47e01 AS |
15264 | static int fixup_call_args(struct bpf_verifier_env *env) |
15265 | { | |
19d28fbd | 15266 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
15267 | struct bpf_prog *prog = env->prog; |
15268 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 15269 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 15270 | int i, depth; |
19d28fbd | 15271 | #endif |
e4052d06 | 15272 | int err = 0; |
1ea47e01 | 15273 | |
e4052d06 QM |
15274 | if (env->prog->jit_requested && |
15275 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
15276 | err = jit_subprogs(env); |
15277 | if (err == 0) | |
1c2a088a | 15278 | return 0; |
c7a89784 DB |
15279 | if (err == -EFAULT) |
15280 | return err; | |
19d28fbd DM |
15281 | } |
15282 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
15283 | if (has_kfunc_call) { |
15284 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
15285 | return -EINVAL; | |
15286 | } | |
e411901c MF |
15287 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
15288 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
15289 | * have to be rejected, since interpreter doesn't support them yet. | |
15290 | */ | |
15291 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
15292 | return -EINVAL; | |
15293 | } | |
1ea47e01 | 15294 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
15295 | if (bpf_pseudo_func(insn)) { |
15296 | /* When JIT fails the progs with callback calls | |
15297 | * have to be rejected, since interpreter doesn't support them yet. | |
15298 | */ | |
15299 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
15300 | return -EINVAL; | |
15301 | } | |
15302 | ||
23a2d70c | 15303 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
15304 | continue; |
15305 | depth = get_callee_stack_depth(env, insn, i); | |
15306 | if (depth < 0) | |
15307 | return depth; | |
15308 | bpf_patch_call_args(insn, depth); | |
15309 | } | |
19d28fbd DM |
15310 | err = 0; |
15311 | #endif | |
15312 | return err; | |
1ea47e01 AS |
15313 | } |
15314 | ||
958cf2e2 KKD |
15315 | static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
15316 | struct bpf_insn *insn_buf, int insn_idx, int *cnt) | |
e6ac2450 MKL |
15317 | { |
15318 | const struct bpf_kfunc_desc *desc; | |
15319 | ||
a5d82727 KKD |
15320 | if (!insn->imm) { |
15321 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
15322 | return -EINVAL; | |
15323 | } | |
15324 | ||
e6ac2450 MKL |
15325 | /* insn->imm has the btf func_id. Replace it with |
15326 | * an address (relative to __bpf_base_call). | |
15327 | */ | |
2357672c | 15328 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
15329 | if (!desc) { |
15330 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
15331 | insn->imm); | |
15332 | return -EFAULT; | |
15333 | } | |
15334 | ||
958cf2e2 | 15335 | *cnt = 0; |
e6ac2450 | 15336 | insn->imm = desc->imm; |
958cf2e2 KKD |
15337 | if (insn->off) |
15338 | return 0; | |
15339 | if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
15340 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
15341 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
15342 | u64 obj_new_size = env->insn_aux_data[insn_idx].obj_new_size; | |
e6ac2450 | 15343 | |
958cf2e2 KKD |
15344 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_1, obj_new_size); |
15345 | insn_buf[1] = addr[0]; | |
15346 | insn_buf[2] = addr[1]; | |
15347 | insn_buf[3] = *insn; | |
15348 | *cnt = 4; | |
ac9f0605 KKD |
15349 | } else if (desc->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { |
15350 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
15351 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
15352 | ||
15353 | insn_buf[0] = addr[0]; | |
15354 | insn_buf[1] = addr[1]; | |
15355 | insn_buf[2] = *insn; | |
15356 | *cnt = 3; | |
a35b9af4 YS |
15357 | } else if (desc->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] || |
15358 | desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { | |
fd264ca0 YS |
15359 | insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1); |
15360 | *cnt = 1; | |
958cf2e2 | 15361 | } |
e6ac2450 MKL |
15362 | return 0; |
15363 | } | |
15364 | ||
e6ac5933 BJ |
15365 | /* Do various post-verification rewrites in a single program pass. |
15366 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 15367 | */ |
e6ac5933 | 15368 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 15369 | { |
79741b3b | 15370 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 15371 | enum bpf_attach_type eatype = prog->expected_attach_type; |
9b99edca | 15372 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 15373 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 15374 | const struct bpf_func_proto *fn; |
79741b3b | 15375 | const int insn_cnt = prog->len; |
09772d92 | 15376 | const struct bpf_map_ops *ops; |
c93552c4 | 15377 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
15378 | struct bpf_insn insn_buf[16]; |
15379 | struct bpf_prog *new_prog; | |
15380 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 15381 | int i, ret, cnt, delta = 0; |
e245c5c6 | 15382 | |
79741b3b | 15383 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 15384 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
15385 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
15386 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
15387 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 15388 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 15389 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
15390 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
15391 | struct bpf_insn *patchlet; | |
15392 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 15393 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
15394 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
15395 | BPF_JNE | BPF_K, insn->src_reg, | |
15396 | 0, 2, 0), | |
f6b1b3bf DB |
15397 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
15398 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
15399 | *insn, | |
15400 | }; | |
e88b2c6e | 15401 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 15402 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
15403 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
15404 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 15405 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 15406 | *insn, |
9b00f1b7 DB |
15407 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
15408 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 15409 | }; |
f6b1b3bf | 15410 | |
e88b2c6e DB |
15411 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
15412 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 15413 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
15414 | |
15415 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
15416 | if (!new_prog) |
15417 | return -ENOMEM; | |
15418 | ||
15419 | delta += cnt - 1; | |
15420 | env->prog = prog = new_prog; | |
15421 | insn = new_prog->insnsi + i + delta; | |
15422 | continue; | |
15423 | } | |
15424 | ||
e6ac5933 | 15425 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
15426 | if (BPF_CLASS(insn->code) == BPF_LD && |
15427 | (BPF_MODE(insn->code) == BPF_ABS || | |
15428 | BPF_MODE(insn->code) == BPF_IND)) { | |
15429 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
15430 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
15431 | verbose(env, "bpf verifier is misconfigured\n"); | |
15432 | return -EINVAL; | |
15433 | } | |
15434 | ||
15435 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15436 | if (!new_prog) | |
15437 | return -ENOMEM; | |
15438 | ||
15439 | delta += cnt - 1; | |
15440 | env->prog = prog = new_prog; | |
15441 | insn = new_prog->insnsi + i + delta; | |
15442 | continue; | |
15443 | } | |
15444 | ||
e6ac5933 | 15445 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
15446 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
15447 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
15448 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
15449 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 15450 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 15451 | bool issrc, isneg, isimm; |
979d63d5 DB |
15452 | u32 off_reg; |
15453 | ||
15454 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
15455 | if (!aux->alu_state || |
15456 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
15457 | continue; |
15458 | ||
15459 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
15460 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
15461 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 15462 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
15463 | |
15464 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
15465 | if (isimm) { |
15466 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
15467 | } else { | |
15468 | if (isneg) | |
15469 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
15470 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
15471 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
15472 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
15473 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
15474 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
15475 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
15476 | } | |
b9b34ddb DB |
15477 | if (!issrc) |
15478 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
15479 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
15480 | if (isneg) |
15481 | insn->code = insn->code == code_add ? | |
15482 | code_sub : code_add; | |
15483 | *patch++ = *insn; | |
801c6058 | 15484 | if (issrc && isneg && !isimm) |
979d63d5 DB |
15485 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
15486 | cnt = patch - insn_buf; | |
15487 | ||
15488 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15489 | if (!new_prog) | |
15490 | return -ENOMEM; | |
15491 | ||
15492 | delta += cnt - 1; | |
15493 | env->prog = prog = new_prog; | |
15494 | insn = new_prog->insnsi + i + delta; | |
15495 | continue; | |
15496 | } | |
15497 | ||
79741b3b AS |
15498 | if (insn->code != (BPF_JMP | BPF_CALL)) |
15499 | continue; | |
cc8b0b92 AS |
15500 | if (insn->src_reg == BPF_PSEUDO_CALL) |
15501 | continue; | |
e6ac2450 | 15502 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
958cf2e2 | 15503 | ret = fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt); |
e6ac2450 MKL |
15504 | if (ret) |
15505 | return ret; | |
958cf2e2 KKD |
15506 | if (cnt == 0) |
15507 | continue; | |
15508 | ||
15509 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15510 | if (!new_prog) | |
15511 | return -ENOMEM; | |
15512 | ||
15513 | delta += cnt - 1; | |
15514 | env->prog = prog = new_prog; | |
15515 | insn = new_prog->insnsi + i + delta; | |
e6ac2450 MKL |
15516 | continue; |
15517 | } | |
e245c5c6 | 15518 | |
79741b3b AS |
15519 | if (insn->imm == BPF_FUNC_get_route_realm) |
15520 | prog->dst_needed = 1; | |
15521 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
15522 | bpf_user_rnd_init_once(); | |
9802d865 JB |
15523 | if (insn->imm == BPF_FUNC_override_return) |
15524 | prog->kprobe_override = 1; | |
79741b3b | 15525 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
15526 | /* If we tail call into other programs, we |
15527 | * cannot make any assumptions since they can | |
15528 | * be replaced dynamically during runtime in | |
15529 | * the program array. | |
15530 | */ | |
15531 | prog->cb_access = 1; | |
e411901c MF |
15532 | if (!allow_tail_call_in_subprogs(env)) |
15533 | prog->aux->stack_depth = MAX_BPF_STACK; | |
15534 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 15535 | |
79741b3b | 15536 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 15537 | * conditional branch in the interpreter for every normal |
79741b3b AS |
15538 | * call and to prevent accidental JITing by JIT compiler |
15539 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 15540 | */ |
79741b3b | 15541 | insn->imm = 0; |
71189fa9 | 15542 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 15543 | |
c93552c4 | 15544 | aux = &env->insn_aux_data[i + delta]; |
d2a3b7c5 | 15545 | if (env->bpf_capable && !prog->blinding_requested && |
cc52d914 | 15546 | prog->jit_requested && |
d2e4c1e6 DB |
15547 | !bpf_map_key_poisoned(aux) && |
15548 | !bpf_map_ptr_poisoned(aux) && | |
15549 | !bpf_map_ptr_unpriv(aux)) { | |
15550 | struct bpf_jit_poke_descriptor desc = { | |
15551 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
15552 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
15553 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 15554 | .insn_idx = i + delta, |
d2e4c1e6 DB |
15555 | }; |
15556 | ||
15557 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
15558 | if (ret < 0) { | |
15559 | verbose(env, "adding tail call poke descriptor failed\n"); | |
15560 | return ret; | |
15561 | } | |
15562 | ||
15563 | insn->imm = ret + 1; | |
15564 | continue; | |
15565 | } | |
15566 | ||
c93552c4 DB |
15567 | if (!bpf_map_ptr_unpriv(aux)) |
15568 | continue; | |
15569 | ||
b2157399 AS |
15570 | /* instead of changing every JIT dealing with tail_call |
15571 | * emit two extra insns: | |
15572 | * if (index >= max_entries) goto out; | |
15573 | * index &= array->index_mask; | |
15574 | * to avoid out-of-bounds cpu speculation | |
15575 | */ | |
c93552c4 | 15576 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 15577 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
15578 | return -EINVAL; |
15579 | } | |
c93552c4 | 15580 | |
d2e4c1e6 | 15581 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
15582 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
15583 | map_ptr->max_entries, 2); | |
15584 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
15585 | container_of(map_ptr, | |
15586 | struct bpf_array, | |
15587 | map)->index_mask); | |
15588 | insn_buf[2] = *insn; | |
15589 | cnt = 3; | |
15590 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15591 | if (!new_prog) | |
15592 | return -ENOMEM; | |
15593 | ||
15594 | delta += cnt - 1; | |
15595 | env->prog = prog = new_prog; | |
15596 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
15597 | continue; |
15598 | } | |
e245c5c6 | 15599 | |
b00628b1 AS |
15600 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
15601 | /* The verifier will process callback_fn as many times as necessary | |
15602 | * with different maps and the register states prepared by | |
15603 | * set_timer_callback_state will be accurate. | |
15604 | * | |
15605 | * The following use case is valid: | |
15606 | * map1 is shared by prog1, prog2, prog3. | |
15607 | * prog1 calls bpf_timer_init for some map1 elements | |
15608 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
15609 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
15610 | * prog3 calls bpf_timer_start for some map1 elements. | |
15611 | * Those that were not both bpf_timer_init-ed and | |
15612 | * bpf_timer_set_callback-ed will return -EINVAL. | |
15613 | */ | |
15614 | struct bpf_insn ld_addrs[2] = { | |
15615 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
15616 | }; | |
15617 | ||
15618 | insn_buf[0] = ld_addrs[0]; | |
15619 | insn_buf[1] = ld_addrs[1]; | |
15620 | insn_buf[2] = *insn; | |
15621 | cnt = 3; | |
15622 | ||
15623 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15624 | if (!new_prog) | |
15625 | return -ENOMEM; | |
15626 | ||
15627 | delta += cnt - 1; | |
15628 | env->prog = prog = new_prog; | |
15629 | insn = new_prog->insnsi + i + delta; | |
15630 | goto patch_call_imm; | |
15631 | } | |
15632 | ||
9bb00b28 YS |
15633 | if (is_storage_get_function(insn->imm)) { |
15634 | if (!env->prog->aux->sleepable || | |
15635 | env->insn_aux_data[i + delta].storage_get_func_atomic) | |
d56c9fe6 | 15636 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); |
9bb00b28 YS |
15637 | else |
15638 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); | |
b00fa38a JK |
15639 | insn_buf[1] = *insn; |
15640 | cnt = 2; | |
15641 | ||
15642 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15643 | if (!new_prog) | |
15644 | return -ENOMEM; | |
15645 | ||
15646 | delta += cnt - 1; | |
15647 | env->prog = prog = new_prog; | |
15648 | insn = new_prog->insnsi + i + delta; | |
15649 | goto patch_call_imm; | |
15650 | } | |
15651 | ||
89c63074 | 15652 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
15653 | * and other inlining handlers are currently limited to 64 bit |
15654 | * only. | |
89c63074 | 15655 | */ |
60b58afc | 15656 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
15657 | (insn->imm == BPF_FUNC_map_lookup_elem || |
15658 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
15659 | insn->imm == BPF_FUNC_map_delete_elem || |
15660 | insn->imm == BPF_FUNC_map_push_elem || | |
15661 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 15662 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c | 15663 | insn->imm == BPF_FUNC_redirect_map || |
07343110 FZ |
15664 | insn->imm == BPF_FUNC_for_each_map_elem || |
15665 | insn->imm == BPF_FUNC_map_lookup_percpu_elem)) { | |
c93552c4 DB |
15666 | aux = &env->insn_aux_data[i + delta]; |
15667 | if (bpf_map_ptr_poisoned(aux)) | |
15668 | goto patch_call_imm; | |
15669 | ||
d2e4c1e6 | 15670 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
15671 | ops = map_ptr->ops; |
15672 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
15673 | ops->map_gen_lookup) { | |
15674 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
15675 | if (cnt == -EOPNOTSUPP) |
15676 | goto patch_map_ops_generic; | |
15677 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
15678 | verbose(env, "bpf verifier is misconfigured\n"); |
15679 | return -EINVAL; | |
15680 | } | |
81ed18ab | 15681 | |
09772d92 DB |
15682 | new_prog = bpf_patch_insn_data(env, i + delta, |
15683 | insn_buf, cnt); | |
15684 | if (!new_prog) | |
15685 | return -ENOMEM; | |
81ed18ab | 15686 | |
09772d92 DB |
15687 | delta += cnt - 1; |
15688 | env->prog = prog = new_prog; | |
15689 | insn = new_prog->insnsi + i + delta; | |
15690 | continue; | |
15691 | } | |
81ed18ab | 15692 | |
09772d92 DB |
15693 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
15694 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
15695 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
15696 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
15697 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
15698 | (int (*)(struct bpf_map *map, void *key, void *value, | |
15699 | u64 flags))NULL)); | |
84430d42 DB |
15700 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
15701 | (int (*)(struct bpf_map *map, void *value, | |
15702 | u64 flags))NULL)); | |
15703 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
15704 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
15705 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
15706 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
e6a4750f | 15707 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
32637e33 | 15708 | (int (*)(struct bpf_map *map, u64 index, u64 flags))NULL)); |
0640c77c AI |
15709 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
15710 | (int (*)(struct bpf_map *map, | |
15711 | bpf_callback_t callback_fn, | |
15712 | void *callback_ctx, | |
15713 | u64 flags))NULL)); | |
07343110 FZ |
15714 | BUILD_BUG_ON(!__same_type(ops->map_lookup_percpu_elem, |
15715 | (void *(*)(struct bpf_map *map, void *key, u32 cpu))NULL)); | |
e6a4750f | 15716 | |
4a8f87e6 | 15717 | patch_map_ops_generic: |
09772d92 DB |
15718 | switch (insn->imm) { |
15719 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 15720 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
15721 | continue; |
15722 | case BPF_FUNC_map_update_elem: | |
3d717fad | 15723 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
15724 | continue; |
15725 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 15726 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 15727 | continue; |
84430d42 | 15728 | case BPF_FUNC_map_push_elem: |
3d717fad | 15729 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
15730 | continue; |
15731 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 15732 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
15733 | continue; |
15734 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 15735 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 15736 | continue; |
e6a4750f | 15737 | case BPF_FUNC_redirect_map: |
3d717fad | 15738 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 15739 | continue; |
0640c77c AI |
15740 | case BPF_FUNC_for_each_map_elem: |
15741 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 15742 | continue; |
07343110 FZ |
15743 | case BPF_FUNC_map_lookup_percpu_elem: |
15744 | insn->imm = BPF_CALL_IMM(ops->map_lookup_percpu_elem); | |
15745 | continue; | |
09772d92 | 15746 | } |
81ed18ab | 15747 | |
09772d92 | 15748 | goto patch_call_imm; |
81ed18ab AS |
15749 | } |
15750 | ||
e6ac5933 | 15751 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
15752 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
15753 | insn->imm == BPF_FUNC_jiffies64) { | |
15754 | struct bpf_insn ld_jiffies_addr[2] = { | |
15755 | BPF_LD_IMM64(BPF_REG_0, | |
15756 | (unsigned long)&jiffies), | |
15757 | }; | |
15758 | ||
15759 | insn_buf[0] = ld_jiffies_addr[0]; | |
15760 | insn_buf[1] = ld_jiffies_addr[1]; | |
15761 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
15762 | BPF_REG_0, 0); | |
15763 | cnt = 3; | |
15764 | ||
15765 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
15766 | cnt); | |
15767 | if (!new_prog) | |
15768 | return -ENOMEM; | |
15769 | ||
15770 | delta += cnt - 1; | |
15771 | env->prog = prog = new_prog; | |
15772 | insn = new_prog->insnsi + i + delta; | |
15773 | continue; | |
15774 | } | |
15775 | ||
f92c1e18 JO |
15776 | /* Implement bpf_get_func_arg inline. */ |
15777 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
15778 | insn->imm == BPF_FUNC_get_func_arg) { | |
15779 | /* Load nr_args from ctx - 8 */ | |
15780 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
15781 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
15782 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
15783 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
15784 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
15785 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
15786 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
15787 | insn_buf[7] = BPF_JMP_A(1); | |
15788 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
15789 | cnt = 9; | |
15790 | ||
15791 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15792 | if (!new_prog) | |
15793 | return -ENOMEM; | |
15794 | ||
15795 | delta += cnt - 1; | |
15796 | env->prog = prog = new_prog; | |
15797 | insn = new_prog->insnsi + i + delta; | |
15798 | continue; | |
15799 | } | |
15800 | ||
15801 | /* Implement bpf_get_func_ret inline. */ | |
15802 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
15803 | insn->imm == BPF_FUNC_get_func_ret) { | |
15804 | if (eatype == BPF_TRACE_FEXIT || | |
15805 | eatype == BPF_MODIFY_RETURN) { | |
15806 | /* Load nr_args from ctx - 8 */ | |
15807 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
15808 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
15809 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
15810 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
15811 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
15812 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
15813 | cnt = 6; | |
15814 | } else { | |
15815 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
15816 | cnt = 1; | |
15817 | } | |
15818 | ||
15819 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15820 | if (!new_prog) | |
15821 | return -ENOMEM; | |
15822 | ||
15823 | delta += cnt - 1; | |
15824 | env->prog = prog = new_prog; | |
15825 | insn = new_prog->insnsi + i + delta; | |
15826 | continue; | |
15827 | } | |
15828 | ||
15829 | /* Implement get_func_arg_cnt inline. */ | |
15830 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
15831 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
15832 | /* Load nr_args from ctx - 8 */ | |
15833 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
15834 | ||
15835 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
15836 | if (!new_prog) | |
15837 | return -ENOMEM; | |
15838 | ||
15839 | env->prog = prog = new_prog; | |
15840 | insn = new_prog->insnsi + i + delta; | |
15841 | continue; | |
15842 | } | |
15843 | ||
f705ec76 | 15844 | /* Implement bpf_get_func_ip inline. */ |
9b99edca JO |
15845 | if (prog_type == BPF_PROG_TYPE_TRACING && |
15846 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
15847 | /* Load IP address from ctx - 16 */ |
15848 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
15849 | |
15850 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
15851 | if (!new_prog) | |
15852 | return -ENOMEM; | |
15853 | ||
15854 | env->prog = prog = new_prog; | |
15855 | insn = new_prog->insnsi + i + delta; | |
15856 | continue; | |
15857 | } | |
15858 | ||
81ed18ab | 15859 | patch_call_imm: |
5e43f899 | 15860 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
15861 | /* all functions that have prototype and verifier allowed |
15862 | * programs to call them, must be real in-kernel functions | |
15863 | */ | |
15864 | if (!fn->func) { | |
61bd5218 JK |
15865 | verbose(env, |
15866 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
15867 | func_id_name(insn->imm), insn->imm); |
15868 | return -EFAULT; | |
e245c5c6 | 15869 | } |
79741b3b | 15870 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 15871 | } |
e245c5c6 | 15872 | |
d2e4c1e6 DB |
15873 | /* Since poke tab is now finalized, publish aux to tracker. */ |
15874 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
15875 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
15876 | if (!map_ptr->ops->map_poke_track || | |
15877 | !map_ptr->ops->map_poke_untrack || | |
15878 | !map_ptr->ops->map_poke_run) { | |
15879 | verbose(env, "bpf verifier is misconfigured\n"); | |
15880 | return -EINVAL; | |
15881 | } | |
15882 | ||
15883 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
15884 | if (ret < 0) { | |
15885 | verbose(env, "tracking tail call prog failed\n"); | |
15886 | return ret; | |
15887 | } | |
15888 | } | |
15889 | ||
e6ac2450 MKL |
15890 | sort_kfunc_descs_by_imm(env->prog); |
15891 | ||
79741b3b AS |
15892 | return 0; |
15893 | } | |
e245c5c6 | 15894 | |
1ade2371 EZ |
15895 | static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env, |
15896 | int position, | |
15897 | s32 stack_base, | |
15898 | u32 callback_subprogno, | |
15899 | u32 *cnt) | |
15900 | { | |
15901 | s32 r6_offset = stack_base + 0 * BPF_REG_SIZE; | |
15902 | s32 r7_offset = stack_base + 1 * BPF_REG_SIZE; | |
15903 | s32 r8_offset = stack_base + 2 * BPF_REG_SIZE; | |
15904 | int reg_loop_max = BPF_REG_6; | |
15905 | int reg_loop_cnt = BPF_REG_7; | |
15906 | int reg_loop_ctx = BPF_REG_8; | |
15907 | ||
15908 | struct bpf_prog *new_prog; | |
15909 | u32 callback_start; | |
15910 | u32 call_insn_offset; | |
15911 | s32 callback_offset; | |
15912 | ||
15913 | /* This represents an inlined version of bpf_iter.c:bpf_loop, | |
15914 | * be careful to modify this code in sync. | |
15915 | */ | |
15916 | struct bpf_insn insn_buf[] = { | |
15917 | /* Return error and jump to the end of the patch if | |
15918 | * expected number of iterations is too big. | |
15919 | */ | |
15920 | BPF_JMP_IMM(BPF_JLE, BPF_REG_1, BPF_MAX_LOOPS, 2), | |
15921 | BPF_MOV32_IMM(BPF_REG_0, -E2BIG), | |
15922 | BPF_JMP_IMM(BPF_JA, 0, 0, 16), | |
15923 | /* spill R6, R7, R8 to use these as loop vars */ | |
15924 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, r6_offset), | |
15925 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_7, r7_offset), | |
15926 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_8, r8_offset), | |
15927 | /* initialize loop vars */ | |
15928 | BPF_MOV64_REG(reg_loop_max, BPF_REG_1), | |
15929 | BPF_MOV32_IMM(reg_loop_cnt, 0), | |
15930 | BPF_MOV64_REG(reg_loop_ctx, BPF_REG_3), | |
15931 | /* loop header, | |
15932 | * if reg_loop_cnt >= reg_loop_max skip the loop body | |
15933 | */ | |
15934 | BPF_JMP_REG(BPF_JGE, reg_loop_cnt, reg_loop_max, 5), | |
15935 | /* callback call, | |
15936 | * correct callback offset would be set after patching | |
15937 | */ | |
15938 | BPF_MOV64_REG(BPF_REG_1, reg_loop_cnt), | |
15939 | BPF_MOV64_REG(BPF_REG_2, reg_loop_ctx), | |
15940 | BPF_CALL_REL(0), | |
15941 | /* increment loop counter */ | |
15942 | BPF_ALU64_IMM(BPF_ADD, reg_loop_cnt, 1), | |
15943 | /* jump to loop header if callback returned 0 */ | |
15944 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -6), | |
15945 | /* return value of bpf_loop, | |
15946 | * set R0 to the number of iterations | |
15947 | */ | |
15948 | BPF_MOV64_REG(BPF_REG_0, reg_loop_cnt), | |
15949 | /* restore original values of R6, R7, R8 */ | |
15950 | BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, r6_offset), | |
15951 | BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, r7_offset), | |
15952 | BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_10, r8_offset), | |
15953 | }; | |
15954 | ||
15955 | *cnt = ARRAY_SIZE(insn_buf); | |
15956 | new_prog = bpf_patch_insn_data(env, position, insn_buf, *cnt); | |
15957 | if (!new_prog) | |
15958 | return new_prog; | |
15959 | ||
15960 | /* callback start is known only after patching */ | |
15961 | callback_start = env->subprog_info[callback_subprogno].start; | |
15962 | /* Note: insn_buf[12] is an offset of BPF_CALL_REL instruction */ | |
15963 | call_insn_offset = position + 12; | |
15964 | callback_offset = callback_start - call_insn_offset - 1; | |
fb4e3b33 | 15965 | new_prog->insnsi[call_insn_offset].imm = callback_offset; |
1ade2371 EZ |
15966 | |
15967 | return new_prog; | |
15968 | } | |
15969 | ||
15970 | static bool is_bpf_loop_call(struct bpf_insn *insn) | |
15971 | { | |
15972 | return insn->code == (BPF_JMP | BPF_CALL) && | |
15973 | insn->src_reg == 0 && | |
15974 | insn->imm == BPF_FUNC_loop; | |
15975 | } | |
15976 | ||
15977 | /* For all sub-programs in the program (including main) check | |
15978 | * insn_aux_data to see if there are bpf_loop calls that require | |
15979 | * inlining. If such calls are found the calls are replaced with a | |
15980 | * sequence of instructions produced by `inline_bpf_loop` function and | |
15981 | * subprog stack_depth is increased by the size of 3 registers. | |
15982 | * This stack space is used to spill values of the R6, R7, R8. These | |
15983 | * registers are used to store the loop bound, counter and context | |
15984 | * variables. | |
15985 | */ | |
15986 | static int optimize_bpf_loop(struct bpf_verifier_env *env) | |
15987 | { | |
15988 | struct bpf_subprog_info *subprogs = env->subprog_info; | |
15989 | int i, cur_subprog = 0, cnt, delta = 0; | |
15990 | struct bpf_insn *insn = env->prog->insnsi; | |
15991 | int insn_cnt = env->prog->len; | |
15992 | u16 stack_depth = subprogs[cur_subprog].stack_depth; | |
15993 | u16 stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
15994 | u16 stack_depth_extra = 0; | |
15995 | ||
15996 | for (i = 0; i < insn_cnt; i++, insn++) { | |
15997 | struct bpf_loop_inline_state *inline_state = | |
15998 | &env->insn_aux_data[i + delta].loop_inline_state; | |
15999 | ||
16000 | if (is_bpf_loop_call(insn) && inline_state->fit_for_inline) { | |
16001 | struct bpf_prog *new_prog; | |
16002 | ||
16003 | stack_depth_extra = BPF_REG_SIZE * 3 + stack_depth_roundup; | |
16004 | new_prog = inline_bpf_loop(env, | |
16005 | i + delta, | |
16006 | -(stack_depth + stack_depth_extra), | |
16007 | inline_state->callback_subprogno, | |
16008 | &cnt); | |
16009 | if (!new_prog) | |
16010 | return -ENOMEM; | |
16011 | ||
16012 | delta += cnt - 1; | |
16013 | env->prog = new_prog; | |
16014 | insn = new_prog->insnsi + i + delta; | |
16015 | } | |
16016 | ||
16017 | if (subprogs[cur_subprog + 1].start == i + delta + 1) { | |
16018 | subprogs[cur_subprog].stack_depth += stack_depth_extra; | |
16019 | cur_subprog++; | |
16020 | stack_depth = subprogs[cur_subprog].stack_depth; | |
16021 | stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
16022 | stack_depth_extra = 0; | |
16023 | } | |
16024 | } | |
16025 | ||
16026 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
16027 | ||
16028 | return 0; | |
16029 | } | |
16030 | ||
58e2af8b | 16031 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 16032 | { |
58e2af8b | 16033 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
16034 | int i; |
16035 | ||
9f4686c4 AS |
16036 | sl = env->free_list; |
16037 | while (sl) { | |
16038 | sln = sl->next; | |
16039 | free_verifier_state(&sl->state, false); | |
16040 | kfree(sl); | |
16041 | sl = sln; | |
16042 | } | |
51c39bb1 | 16043 | env->free_list = NULL; |
9f4686c4 | 16044 | |
f1bca824 AS |
16045 | if (!env->explored_states) |
16046 | return; | |
16047 | ||
dc2a4ebc | 16048 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
16049 | sl = env->explored_states[i]; |
16050 | ||
a8f500af AS |
16051 | while (sl) { |
16052 | sln = sl->next; | |
16053 | free_verifier_state(&sl->state, false); | |
16054 | kfree(sl); | |
16055 | sl = sln; | |
16056 | } | |
51c39bb1 | 16057 | env->explored_states[i] = NULL; |
f1bca824 | 16058 | } |
51c39bb1 | 16059 | } |
f1bca824 | 16060 | |
51c39bb1 AS |
16061 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
16062 | { | |
6f8a57cc | 16063 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
16064 | struct bpf_verifier_state *state; |
16065 | struct bpf_reg_state *regs; | |
16066 | int ret, i; | |
16067 | ||
16068 | env->prev_linfo = NULL; | |
16069 | env->pass_cnt++; | |
16070 | ||
16071 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
16072 | if (!state) | |
16073 | return -ENOMEM; | |
16074 | state->curframe = 0; | |
16075 | state->speculative = false; | |
16076 | state->branches = 1; | |
16077 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
16078 | if (!state->frame[0]) { | |
16079 | kfree(state); | |
16080 | return -ENOMEM; | |
16081 | } | |
16082 | env->cur_state = state; | |
16083 | init_func_state(env, state->frame[0], | |
16084 | BPF_MAIN_FUNC /* callsite */, | |
16085 | 0 /* frameno */, | |
16086 | subprog); | |
be2ef816 AN |
16087 | state->first_insn_idx = env->subprog_info[subprog].start; |
16088 | state->last_insn_idx = -1; | |
51c39bb1 AS |
16089 | |
16090 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 16091 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
16092 | ret = btf_prepare_func_args(env, subprog, regs); |
16093 | if (ret) | |
16094 | goto out; | |
16095 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
16096 | if (regs[i].type == PTR_TO_CTX) | |
16097 | mark_reg_known_zero(env, regs, i); | |
16098 | else if (regs[i].type == SCALAR_VALUE) | |
16099 | mark_reg_unknown(env, regs, i); | |
cf9f2f8d | 16100 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
e5069b9c DB |
16101 | const u32 mem_size = regs[i].mem_size; |
16102 | ||
16103 | mark_reg_known_zero(env, regs, i); | |
16104 | regs[i].mem_size = mem_size; | |
16105 | regs[i].id = ++env->id_gen; | |
16106 | } | |
51c39bb1 AS |
16107 | } |
16108 | } else { | |
16109 | /* 1st arg to a function */ | |
16110 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
16111 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 16112 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
16113 | if (ret == -EFAULT) |
16114 | /* unlikely verifier bug. abort. | |
16115 | * ret == 0 and ret < 0 are sadly acceptable for | |
16116 | * main() function due to backward compatibility. | |
16117 | * Like socket filter program may be written as: | |
16118 | * int bpf_prog(struct pt_regs *ctx) | |
16119 | * and never dereference that ctx in the program. | |
16120 | * 'struct pt_regs' is a type mismatch for socket | |
16121 | * filter that should be using 'struct __sk_buff'. | |
16122 | */ | |
16123 | goto out; | |
16124 | } | |
16125 | ||
16126 | ret = do_check(env); | |
16127 | out: | |
f59bbfc2 AS |
16128 | /* check for NULL is necessary, since cur_state can be freed inside |
16129 | * do_check() under memory pressure. | |
16130 | */ | |
16131 | if (env->cur_state) { | |
16132 | free_verifier_state(env->cur_state, true); | |
16133 | env->cur_state = NULL; | |
16134 | } | |
6f8a57cc AN |
16135 | while (!pop_stack(env, NULL, NULL, false)); |
16136 | if (!ret && pop_log) | |
16137 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 16138 | free_states(env); |
51c39bb1 AS |
16139 | return ret; |
16140 | } | |
16141 | ||
16142 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
16143 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
16144 | * Consider: | |
16145 | * int bar(int); | |
16146 | * int foo(int f) | |
16147 | * { | |
16148 | * return bar(f); | |
16149 | * } | |
16150 | * int bar(int b) | |
16151 | * { | |
16152 | * ... | |
16153 | * } | |
16154 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
16155 | * will be assumed that bar() already verified successfully and call to bar() | |
16156 | * from foo() will be checked for type match only. Later bar() will be verified | |
16157 | * independently to check that it's safe for R1=any_scalar_value. | |
16158 | */ | |
16159 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
16160 | { | |
16161 | struct bpf_prog_aux *aux = env->prog->aux; | |
16162 | int i, ret; | |
16163 | ||
16164 | if (!aux->func_info) | |
16165 | return 0; | |
16166 | ||
16167 | for (i = 1; i < env->subprog_cnt; i++) { | |
16168 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
16169 | continue; | |
16170 | env->insn_idx = env->subprog_info[i].start; | |
16171 | WARN_ON_ONCE(env->insn_idx == 0); | |
16172 | ret = do_check_common(env, i); | |
16173 | if (ret) { | |
16174 | return ret; | |
16175 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
16176 | verbose(env, | |
16177 | "Func#%d is safe for any args that match its prototype\n", | |
16178 | i); | |
16179 | } | |
16180 | } | |
16181 | return 0; | |
16182 | } | |
16183 | ||
16184 | static int do_check_main(struct bpf_verifier_env *env) | |
16185 | { | |
16186 | int ret; | |
16187 | ||
16188 | env->insn_idx = 0; | |
16189 | ret = do_check_common(env, 0); | |
16190 | if (!ret) | |
16191 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
16192 | return ret; | |
16193 | } | |
16194 | ||
16195 | ||
06ee7115 AS |
16196 | static void print_verification_stats(struct bpf_verifier_env *env) |
16197 | { | |
16198 | int i; | |
16199 | ||
16200 | if (env->log.level & BPF_LOG_STATS) { | |
16201 | verbose(env, "verification time %lld usec\n", | |
16202 | div_u64(env->verification_time, 1000)); | |
16203 | verbose(env, "stack depth "); | |
16204 | for (i = 0; i < env->subprog_cnt; i++) { | |
16205 | u32 depth = env->subprog_info[i].stack_depth; | |
16206 | ||
16207 | verbose(env, "%d", depth); | |
16208 | if (i + 1 < env->subprog_cnt) | |
16209 | verbose(env, "+"); | |
16210 | } | |
16211 | verbose(env, "\n"); | |
16212 | } | |
16213 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
16214 | "total_states %d peak_states %d mark_read %d\n", | |
16215 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
16216 | env->max_states_per_insn, env->total_states, | |
16217 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
16218 | } |
16219 | ||
27ae7997 MKL |
16220 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
16221 | { | |
16222 | const struct btf_type *t, *func_proto; | |
16223 | const struct bpf_struct_ops *st_ops; | |
16224 | const struct btf_member *member; | |
16225 | struct bpf_prog *prog = env->prog; | |
16226 | u32 btf_id, member_idx; | |
16227 | const char *mname; | |
16228 | ||
12aa8a94 THJ |
16229 | if (!prog->gpl_compatible) { |
16230 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
16231 | return -EINVAL; | |
16232 | } | |
16233 | ||
27ae7997 MKL |
16234 | btf_id = prog->aux->attach_btf_id; |
16235 | st_ops = bpf_struct_ops_find(btf_id); | |
16236 | if (!st_ops) { | |
16237 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
16238 | btf_id); | |
16239 | return -ENOTSUPP; | |
16240 | } | |
16241 | ||
16242 | t = st_ops->type; | |
16243 | member_idx = prog->expected_attach_type; | |
16244 | if (member_idx >= btf_type_vlen(t)) { | |
16245 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
16246 | member_idx, st_ops->name); | |
16247 | return -EINVAL; | |
16248 | } | |
16249 | ||
16250 | member = &btf_type_member(t)[member_idx]; | |
16251 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
16252 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
16253 | NULL); | |
16254 | if (!func_proto) { | |
16255 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
16256 | mname, member_idx, st_ops->name); | |
16257 | return -EINVAL; | |
16258 | } | |
16259 | ||
16260 | if (st_ops->check_member) { | |
16261 | int err = st_ops->check_member(t, member); | |
16262 | ||
16263 | if (err) { | |
16264 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
16265 | mname, st_ops->name); | |
16266 | return err; | |
16267 | } | |
16268 | } | |
16269 | ||
16270 | prog->aux->attach_func_proto = func_proto; | |
16271 | prog->aux->attach_func_name = mname; | |
16272 | env->ops = st_ops->verifier_ops; | |
16273 | ||
16274 | return 0; | |
16275 | } | |
6ba43b76 KS |
16276 | #define SECURITY_PREFIX "security_" |
16277 | ||
f7b12b6f | 16278 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 16279 | { |
69191754 | 16280 | if (within_error_injection_list(addr) || |
f7b12b6f | 16281 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 16282 | return 0; |
6ba43b76 | 16283 | |
6ba43b76 KS |
16284 | return -EINVAL; |
16285 | } | |
27ae7997 | 16286 | |
1e6c62a8 AS |
16287 | /* list of non-sleepable functions that are otherwise on |
16288 | * ALLOW_ERROR_INJECTION list | |
16289 | */ | |
16290 | BTF_SET_START(btf_non_sleepable_error_inject) | |
16291 | /* Three functions below can be called from sleepable and non-sleepable context. | |
16292 | * Assume non-sleepable from bpf safety point of view. | |
16293 | */ | |
9dd3d069 | 16294 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
16295 | BTF_ID(func, should_fail_alloc_page) |
16296 | BTF_ID(func, should_failslab) | |
16297 | BTF_SET_END(btf_non_sleepable_error_inject) | |
16298 | ||
16299 | static int check_non_sleepable_error_inject(u32 btf_id) | |
16300 | { | |
16301 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
16302 | } | |
16303 | ||
f7b12b6f THJ |
16304 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
16305 | const struct bpf_prog *prog, | |
16306 | const struct bpf_prog *tgt_prog, | |
16307 | u32 btf_id, | |
16308 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 16309 | { |
be8704ff | 16310 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 16311 | const char prefix[] = "btf_trace_"; |
5b92a28a | 16312 | int ret = 0, subprog = -1, i; |
38207291 | 16313 | const struct btf_type *t; |
5b92a28a | 16314 | bool conservative = true; |
38207291 | 16315 | const char *tname; |
5b92a28a | 16316 | struct btf *btf; |
f7b12b6f | 16317 | long addr = 0; |
38207291 | 16318 | |
f1b9509c | 16319 | if (!btf_id) { |
efc68158 | 16320 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
16321 | return -EINVAL; |
16322 | } | |
22dc4a0f | 16323 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 16324 | if (!btf) { |
efc68158 | 16325 | bpf_log(log, |
5b92a28a AS |
16326 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
16327 | return -EINVAL; | |
16328 | } | |
16329 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 16330 | if (!t) { |
efc68158 | 16331 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
16332 | return -EINVAL; |
16333 | } | |
5b92a28a | 16334 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 16335 | if (!tname) { |
efc68158 | 16336 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
16337 | return -EINVAL; |
16338 | } | |
5b92a28a AS |
16339 | if (tgt_prog) { |
16340 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
16341 | ||
16342 | for (i = 0; i < aux->func_info_cnt; i++) | |
16343 | if (aux->func_info[i].type_id == btf_id) { | |
16344 | subprog = i; | |
16345 | break; | |
16346 | } | |
16347 | if (subprog == -1) { | |
efc68158 | 16348 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
16349 | return -EINVAL; |
16350 | } | |
16351 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
16352 | if (prog_extension) { |
16353 | if (conservative) { | |
efc68158 | 16354 | bpf_log(log, |
be8704ff AS |
16355 | "Cannot replace static functions\n"); |
16356 | return -EINVAL; | |
16357 | } | |
16358 | if (!prog->jit_requested) { | |
efc68158 | 16359 | bpf_log(log, |
be8704ff AS |
16360 | "Extension programs should be JITed\n"); |
16361 | return -EINVAL; | |
16362 | } | |
be8704ff AS |
16363 | } |
16364 | if (!tgt_prog->jited) { | |
efc68158 | 16365 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
16366 | return -EINVAL; |
16367 | } | |
16368 | if (tgt_prog->type == prog->type) { | |
16369 | /* Cannot fentry/fexit another fentry/fexit program. | |
16370 | * Cannot attach program extension to another extension. | |
16371 | * It's ok to attach fentry/fexit to extension program. | |
16372 | */ | |
efc68158 | 16373 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
16374 | return -EINVAL; |
16375 | } | |
16376 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
16377 | prog_extension && | |
16378 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
16379 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
16380 | /* Program extensions can extend all program types | |
16381 | * except fentry/fexit. The reason is the following. | |
16382 | * The fentry/fexit programs are used for performance | |
16383 | * analysis, stats and can be attached to any program | |
16384 | * type except themselves. When extension program is | |
16385 | * replacing XDP function it is necessary to allow | |
16386 | * performance analysis of all functions. Both original | |
16387 | * XDP program and its program extension. Hence | |
16388 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
16389 | * allowed. If extending of fentry/fexit was allowed it | |
16390 | * would be possible to create long call chain | |
16391 | * fentry->extension->fentry->extension beyond | |
16392 | * reasonable stack size. Hence extending fentry is not | |
16393 | * allowed. | |
16394 | */ | |
efc68158 | 16395 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
16396 | return -EINVAL; |
16397 | } | |
5b92a28a | 16398 | } else { |
be8704ff | 16399 | if (prog_extension) { |
efc68158 | 16400 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
16401 | return -EINVAL; |
16402 | } | |
5b92a28a | 16403 | } |
f1b9509c AS |
16404 | |
16405 | switch (prog->expected_attach_type) { | |
16406 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 16407 | if (tgt_prog) { |
efc68158 | 16408 | bpf_log(log, |
5b92a28a AS |
16409 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
16410 | return -EINVAL; | |
16411 | } | |
38207291 | 16412 | if (!btf_type_is_typedef(t)) { |
efc68158 | 16413 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
16414 | btf_id); |
16415 | return -EINVAL; | |
16416 | } | |
f1b9509c | 16417 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 16418 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
16419 | btf_id, tname); |
16420 | return -EINVAL; | |
16421 | } | |
16422 | tname += sizeof(prefix) - 1; | |
5b92a28a | 16423 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
16424 | if (!btf_type_is_ptr(t)) |
16425 | /* should never happen in valid vmlinux build */ | |
16426 | return -EINVAL; | |
5b92a28a | 16427 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
16428 | if (!btf_type_is_func_proto(t)) |
16429 | /* should never happen in valid vmlinux build */ | |
16430 | return -EINVAL; | |
16431 | ||
f7b12b6f | 16432 | break; |
15d83c4d YS |
16433 | case BPF_TRACE_ITER: |
16434 | if (!btf_type_is_func(t)) { | |
efc68158 | 16435 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
16436 | btf_id); |
16437 | return -EINVAL; | |
16438 | } | |
16439 | t = btf_type_by_id(btf, t->type); | |
16440 | if (!btf_type_is_func_proto(t)) | |
16441 | return -EINVAL; | |
f7b12b6f THJ |
16442 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
16443 | if (ret) | |
16444 | return ret; | |
16445 | break; | |
be8704ff AS |
16446 | default: |
16447 | if (!prog_extension) | |
16448 | return -EINVAL; | |
df561f66 | 16449 | fallthrough; |
ae240823 | 16450 | case BPF_MODIFY_RETURN: |
9e4e01df | 16451 | case BPF_LSM_MAC: |
69fd337a | 16452 | case BPF_LSM_CGROUP: |
fec56f58 AS |
16453 | case BPF_TRACE_FENTRY: |
16454 | case BPF_TRACE_FEXIT: | |
16455 | if (!btf_type_is_func(t)) { | |
efc68158 | 16456 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
16457 | btf_id); |
16458 | return -EINVAL; | |
16459 | } | |
be8704ff | 16460 | if (prog_extension && |
efc68158 | 16461 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 16462 | return -EINVAL; |
5b92a28a | 16463 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
16464 | if (!btf_type_is_func_proto(t)) |
16465 | return -EINVAL; | |
f7b12b6f | 16466 | |
4a1e7c0c THJ |
16467 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
16468 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
16469 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
16470 | return -EINVAL; | |
16471 | ||
f7b12b6f | 16472 | if (tgt_prog && conservative) |
5b92a28a | 16473 | t = NULL; |
f7b12b6f THJ |
16474 | |
16475 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 16476 | if (ret < 0) |
f7b12b6f THJ |
16477 | return ret; |
16478 | ||
5b92a28a | 16479 | if (tgt_prog) { |
e9eeec58 YS |
16480 | if (subprog == 0) |
16481 | addr = (long) tgt_prog->bpf_func; | |
16482 | else | |
16483 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
16484 | } else { |
16485 | addr = kallsyms_lookup_name(tname); | |
16486 | if (!addr) { | |
efc68158 | 16487 | bpf_log(log, |
5b92a28a AS |
16488 | "The address of function %s cannot be found\n", |
16489 | tname); | |
f7b12b6f | 16490 | return -ENOENT; |
5b92a28a | 16491 | } |
fec56f58 | 16492 | } |
18644cec | 16493 | |
1e6c62a8 AS |
16494 | if (prog->aux->sleepable) { |
16495 | ret = -EINVAL; | |
16496 | switch (prog->type) { | |
16497 | case BPF_PROG_TYPE_TRACING: | |
16498 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
16499 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
16500 | */ | |
16501 | if (!check_non_sleepable_error_inject(btf_id) && | |
16502 | within_error_injection_list(addr)) | |
16503 | ret = 0; | |
16504 | break; | |
16505 | case BPF_PROG_TYPE_LSM: | |
16506 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
16507 | * Only some of them are sleepable. | |
16508 | */ | |
423f1610 | 16509 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
16510 | ret = 0; |
16511 | break; | |
16512 | default: | |
16513 | break; | |
16514 | } | |
f7b12b6f THJ |
16515 | if (ret) { |
16516 | bpf_log(log, "%s is not sleepable\n", tname); | |
16517 | return ret; | |
16518 | } | |
1e6c62a8 | 16519 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 16520 | if (tgt_prog) { |
efc68158 | 16521 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
16522 | return -EINVAL; |
16523 | } | |
16524 | ret = check_attach_modify_return(addr, tname); | |
16525 | if (ret) { | |
16526 | bpf_log(log, "%s() is not modifiable\n", tname); | |
16527 | return ret; | |
1af9270e | 16528 | } |
18644cec | 16529 | } |
f7b12b6f THJ |
16530 | |
16531 | break; | |
16532 | } | |
16533 | tgt_info->tgt_addr = addr; | |
16534 | tgt_info->tgt_name = tname; | |
16535 | tgt_info->tgt_type = t; | |
16536 | return 0; | |
16537 | } | |
16538 | ||
35e3815f JO |
16539 | BTF_SET_START(btf_id_deny) |
16540 | BTF_ID_UNUSED | |
16541 | #ifdef CONFIG_SMP | |
16542 | BTF_ID(func, migrate_disable) | |
16543 | BTF_ID(func, migrate_enable) | |
16544 | #endif | |
16545 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
16546 | BTF_ID(func, rcu_read_unlock_strict) | |
16547 | #endif | |
16548 | BTF_SET_END(btf_id_deny) | |
16549 | ||
f7b12b6f THJ |
16550 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
16551 | { | |
16552 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 16553 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
16554 | struct bpf_attach_target_info tgt_info = {}; |
16555 | u32 btf_id = prog->aux->attach_btf_id; | |
16556 | struct bpf_trampoline *tr; | |
16557 | int ret; | |
16558 | u64 key; | |
16559 | ||
79a7f8bd AS |
16560 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
16561 | if (prog->aux->sleepable) | |
16562 | /* attach_btf_id checked to be zero already */ | |
16563 | return 0; | |
16564 | verbose(env, "Syscall programs can only be sleepable\n"); | |
16565 | return -EINVAL; | |
16566 | } | |
16567 | ||
f7b12b6f | 16568 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
64ad7556 DK |
16569 | prog->type != BPF_PROG_TYPE_LSM && prog->type != BPF_PROG_TYPE_KPROBE) { |
16570 | verbose(env, "Only fentry/fexit/fmod_ret, lsm, and kprobe/uprobe programs can be sleepable\n"); | |
f7b12b6f THJ |
16571 | return -EINVAL; |
16572 | } | |
16573 | ||
16574 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
16575 | return check_struct_ops_btf_id(env); | |
16576 | ||
16577 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
16578 | prog->type != BPF_PROG_TYPE_LSM && | |
16579 | prog->type != BPF_PROG_TYPE_EXT) | |
16580 | return 0; | |
16581 | ||
16582 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
16583 | if (ret) | |
fec56f58 | 16584 | return ret; |
f7b12b6f THJ |
16585 | |
16586 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
16587 | /* to make freplace equivalent to their targets, they need to |
16588 | * inherit env->ops and expected_attach_type for the rest of the | |
16589 | * verification | |
16590 | */ | |
f7b12b6f THJ |
16591 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
16592 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
16593 | } | |
16594 | ||
16595 | /* store info about the attachment target that will be used later */ | |
16596 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
16597 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
16598 | ||
4a1e7c0c THJ |
16599 | if (tgt_prog) { |
16600 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
16601 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
16602 | } | |
16603 | ||
f7b12b6f THJ |
16604 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
16605 | prog->aux->attach_btf_trace = true; | |
16606 | return 0; | |
16607 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
16608 | if (!bpf_iter_prog_supported(prog)) | |
16609 | return -EINVAL; | |
16610 | return 0; | |
16611 | } | |
16612 | ||
16613 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
16614 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
16615 | if (ret < 0) | |
16616 | return ret; | |
35e3815f JO |
16617 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
16618 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
16619 | return -EINVAL; | |
38207291 | 16620 | } |
f7b12b6f | 16621 | |
22dc4a0f | 16622 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
16623 | tr = bpf_trampoline_get(key, &tgt_info); |
16624 | if (!tr) | |
16625 | return -ENOMEM; | |
16626 | ||
3aac1ead | 16627 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 16628 | return 0; |
38207291 MKL |
16629 | } |
16630 | ||
76654e67 AM |
16631 | struct btf *bpf_get_btf_vmlinux(void) |
16632 | { | |
16633 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
16634 | mutex_lock(&bpf_verifier_lock); | |
16635 | if (!btf_vmlinux) | |
16636 | btf_vmlinux = btf_parse_vmlinux(); | |
16637 | mutex_unlock(&bpf_verifier_lock); | |
16638 | } | |
16639 | return btf_vmlinux; | |
16640 | } | |
16641 | ||
af2ac3e1 | 16642 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 16643 | { |
06ee7115 | 16644 | u64 start_time = ktime_get_ns(); |
58e2af8b | 16645 | struct bpf_verifier_env *env; |
b9193c1b | 16646 | struct bpf_verifier_log *log; |
9e4c24e7 | 16647 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 16648 | bool is_priv; |
51580e79 | 16649 | |
eba0c929 AB |
16650 | /* no program is valid */ |
16651 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
16652 | return -EINVAL; | |
16653 | ||
58e2af8b | 16654 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
16655 | * allocate/free it every time bpf_check() is called |
16656 | */ | |
58e2af8b | 16657 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
16658 | if (!env) |
16659 | return -ENOMEM; | |
61bd5218 | 16660 | log = &env->log; |
cbd35700 | 16661 | |
9e4c24e7 | 16662 | len = (*prog)->len; |
fad953ce | 16663 | env->insn_aux_data = |
9e4c24e7 | 16664 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
16665 | ret = -ENOMEM; |
16666 | if (!env->insn_aux_data) | |
16667 | goto err_free_env; | |
9e4c24e7 JK |
16668 | for (i = 0; i < len; i++) |
16669 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 16670 | env->prog = *prog; |
00176a34 | 16671 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 16672 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 16673 | is_priv = bpf_capable(); |
0246e64d | 16674 | |
76654e67 | 16675 | bpf_get_btf_vmlinux(); |
8580ac94 | 16676 | |
cbd35700 | 16677 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
16678 | if (!is_priv) |
16679 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
16680 | |
16681 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
16682 | /* user requested verbose verifier output | |
16683 | * and supplied buffer to store the verification trace | |
16684 | */ | |
e7bf8249 JK |
16685 | log->level = attr->log_level; |
16686 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
16687 | log->len_total = attr->log_size; | |
cbd35700 | 16688 | |
e7bf8249 | 16689 | /* log attributes have to be sane */ |
866de407 HT |
16690 | if (!bpf_verifier_log_attr_valid(log)) { |
16691 | ret = -EINVAL; | |
3df126f3 | 16692 | goto err_unlock; |
866de407 | 16693 | } |
cbd35700 | 16694 | } |
1ad2f583 | 16695 | |
0f55f9ed CL |
16696 | mark_verifier_state_clean(env); |
16697 | ||
8580ac94 AS |
16698 | if (IS_ERR(btf_vmlinux)) { |
16699 | /* Either gcc or pahole or kernel are broken. */ | |
16700 | verbose(env, "in-kernel BTF is malformed\n"); | |
16701 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 16702 | goto skip_full_check; |
8580ac94 AS |
16703 | } |
16704 | ||
1ad2f583 DB |
16705 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
16706 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 16707 | env->strict_alignment = true; |
e9ee9efc DM |
16708 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
16709 | env->strict_alignment = false; | |
cbd35700 | 16710 | |
2c78ee89 | 16711 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 16712 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
2c78ee89 AS |
16713 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
16714 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
16715 | env->bpf_capable = bpf_capable(); | |
9bb00b28 YS |
16716 | env->rcu_tag_supported = btf_vmlinux && |
16717 | btf_find_by_name_kind(btf_vmlinux, "rcu", BTF_KIND_TYPE_TAG) > 0; | |
e2ae4ca2 | 16718 | |
10d274e8 AS |
16719 | if (is_priv) |
16720 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
16721 | ||
dc2a4ebc | 16722 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 16723 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
16724 | GFP_USER); |
16725 | ret = -ENOMEM; | |
16726 | if (!env->explored_states) | |
16727 | goto skip_full_check; | |
16728 | ||
e6ac2450 MKL |
16729 | ret = add_subprog_and_kfunc(env); |
16730 | if (ret < 0) | |
16731 | goto skip_full_check; | |
16732 | ||
d9762e84 | 16733 | ret = check_subprogs(env); |
475fb78f AS |
16734 | if (ret < 0) |
16735 | goto skip_full_check; | |
16736 | ||
c454a46b | 16737 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
16738 | if (ret < 0) |
16739 | goto skip_full_check; | |
16740 | ||
be8704ff AS |
16741 | ret = check_attach_btf_id(env); |
16742 | if (ret) | |
16743 | goto skip_full_check; | |
16744 | ||
4976b718 HL |
16745 | ret = resolve_pseudo_ldimm64(env); |
16746 | if (ret < 0) | |
16747 | goto skip_full_check; | |
16748 | ||
ceb11679 YZ |
16749 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
16750 | ret = bpf_prog_offload_verifier_prep(env->prog); | |
16751 | if (ret) | |
16752 | goto skip_full_check; | |
16753 | } | |
16754 | ||
d9762e84 MKL |
16755 | ret = check_cfg(env); |
16756 | if (ret < 0) | |
16757 | goto skip_full_check; | |
16758 | ||
51c39bb1 AS |
16759 | ret = do_check_subprogs(env); |
16760 | ret = ret ?: do_check_main(env); | |
cbd35700 | 16761 | |
c941ce9c QM |
16762 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
16763 | ret = bpf_prog_offload_finalize(env); | |
16764 | ||
0246e64d | 16765 | skip_full_check: |
51c39bb1 | 16766 | kvfree(env->explored_states); |
0246e64d | 16767 | |
c131187d | 16768 | if (ret == 0) |
9b38c405 | 16769 | ret = check_max_stack_depth(env); |
c131187d | 16770 | |
9b38c405 | 16771 | /* instruction rewrites happen after this point */ |
1ade2371 EZ |
16772 | if (ret == 0) |
16773 | ret = optimize_bpf_loop(env); | |
16774 | ||
e2ae4ca2 JK |
16775 | if (is_priv) { |
16776 | if (ret == 0) | |
16777 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
16778 | if (ret == 0) |
16779 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
16780 | if (ret == 0) |
16781 | ret = opt_remove_nops(env); | |
52875a04 JK |
16782 | } else { |
16783 | if (ret == 0) | |
16784 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
16785 | } |
16786 | ||
9bac3d6d AS |
16787 | if (ret == 0) |
16788 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
16789 | ret = convert_ctx_accesses(env); | |
16790 | ||
e245c5c6 | 16791 | if (ret == 0) |
e6ac5933 | 16792 | ret = do_misc_fixups(env); |
e245c5c6 | 16793 | |
a4b1d3c1 JW |
16794 | /* do 32-bit optimization after insn patching has done so those patched |
16795 | * insns could be handled correctly. | |
16796 | */ | |
d6c2308c JW |
16797 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
16798 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
16799 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
16800 | : false; | |
a4b1d3c1 JW |
16801 | } |
16802 | ||
1ea47e01 AS |
16803 | if (ret == 0) |
16804 | ret = fixup_call_args(env); | |
16805 | ||
06ee7115 AS |
16806 | env->verification_time = ktime_get_ns() - start_time; |
16807 | print_verification_stats(env); | |
aba64c7d | 16808 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 16809 | |
a2a7d570 | 16810 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 16811 | ret = -ENOSPC; |
a2a7d570 | 16812 | if (log->level && !log->ubuf) { |
cbd35700 | 16813 | ret = -EFAULT; |
a2a7d570 | 16814 | goto err_release_maps; |
cbd35700 AS |
16815 | } |
16816 | ||
541c3bad AN |
16817 | if (ret) |
16818 | goto err_release_maps; | |
16819 | ||
16820 | if (env->used_map_cnt) { | |
0246e64d | 16821 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
16822 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
16823 | sizeof(env->used_maps[0]), | |
16824 | GFP_KERNEL); | |
0246e64d | 16825 | |
9bac3d6d | 16826 | if (!env->prog->aux->used_maps) { |
0246e64d | 16827 | ret = -ENOMEM; |
a2a7d570 | 16828 | goto err_release_maps; |
0246e64d AS |
16829 | } |
16830 | ||
9bac3d6d | 16831 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 16832 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 16833 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
16834 | } |
16835 | if (env->used_btf_cnt) { | |
16836 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
16837 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
16838 | sizeof(env->used_btfs[0]), | |
16839 | GFP_KERNEL); | |
16840 | if (!env->prog->aux->used_btfs) { | |
16841 | ret = -ENOMEM; | |
16842 | goto err_release_maps; | |
16843 | } | |
0246e64d | 16844 | |
541c3bad AN |
16845 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
16846 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
16847 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
16848 | } | |
16849 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
16850 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
16851 | * bpf_ld_imm64 instructions | |
16852 | */ | |
16853 | convert_pseudo_ld_imm64(env); | |
16854 | } | |
cbd35700 | 16855 | |
541c3bad | 16856 | adjust_btf_func(env); |
ba64e7d8 | 16857 | |
a2a7d570 | 16858 | err_release_maps: |
9bac3d6d | 16859 | if (!env->prog->aux->used_maps) |
0246e64d | 16860 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 16861 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
16862 | */ |
16863 | release_maps(env); | |
541c3bad AN |
16864 | if (!env->prog->aux->used_btfs) |
16865 | release_btfs(env); | |
03f87c0b THJ |
16866 | |
16867 | /* extension progs temporarily inherit the attach_type of their targets | |
16868 | for verification purposes, so set it back to zero before returning | |
16869 | */ | |
16870 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
16871 | env->prog->expected_attach_type = 0; | |
16872 | ||
9bac3d6d | 16873 | *prog = env->prog; |
3df126f3 | 16874 | err_unlock: |
45a73c17 AS |
16875 | if (!is_priv) |
16876 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
16877 | vfree(env->insn_aux_data); |
16878 | err_free_env: | |
16879 | kfree(env); | |
51580e79 AS |
16880 | return ret; |
16881 | } |