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 | ||
d83525ca AS |
454 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
455 | { | |
456 | return reg->type == PTR_TO_MAP_VALUE && | |
db559117 | 457 | btf_record_has_field(reg->map_ptr->record, BPF_SPIN_LOCK); |
d83525ca AS |
458 | } |
459 | ||
20b2aff4 HL |
460 | static bool type_is_rdonly_mem(u32 type) |
461 | { | |
462 | return type & MEM_RDONLY; | |
cba368c1 MKL |
463 | } |
464 | ||
48946bd6 | 465 | static bool type_may_be_null(u32 type) |
fd1b0d60 | 466 | { |
48946bd6 | 467 | return type & PTR_MAYBE_NULL; |
fd1b0d60 LB |
468 | } |
469 | ||
64d85290 JS |
470 | static bool is_acquire_function(enum bpf_func_id func_id, |
471 | const struct bpf_map *map) | |
472 | { | |
473 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
474 | ||
475 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
476 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 | 477 | func_id == BPF_FUNC_skc_lookup_tcp || |
c0a5a21c KKD |
478 | func_id == BPF_FUNC_ringbuf_reserve || |
479 | func_id == BPF_FUNC_kptr_xchg) | |
64d85290 JS |
480 | return true; |
481 | ||
482 | if (func_id == BPF_FUNC_map_lookup_elem && | |
483 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
484 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
485 | return true; | |
486 | ||
487 | return false; | |
46f8bc92 MKL |
488 | } |
489 | ||
1b986589 MKL |
490 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
491 | { | |
492 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
493 | func_id == BPF_FUNC_sk_fullsock || |
494 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
495 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
496 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
3bc253c2 | 497 | func_id == BPF_FUNC_skc_to_mptcp_sock || |
1df8f55a MKL |
498 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
499 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
500 | } |
501 | ||
88374342 | 502 | static bool is_dynptr_ref_function(enum bpf_func_id func_id) |
b2d8ef19 DM |
503 | { |
504 | return func_id == BPF_FUNC_dynptr_data; | |
505 | } | |
506 | ||
507 | static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id, | |
508 | const struct bpf_map *map) | |
509 | { | |
510 | int ref_obj_uses = 0; | |
511 | ||
512 | if (is_ptr_cast_function(func_id)) | |
513 | ref_obj_uses++; | |
514 | if (is_acquire_function(func_id, map)) | |
515 | ref_obj_uses++; | |
88374342 | 516 | if (is_dynptr_ref_function(func_id)) |
b2d8ef19 DM |
517 | ref_obj_uses++; |
518 | ||
519 | return ref_obj_uses > 1; | |
520 | } | |
521 | ||
39491867 BJ |
522 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
523 | { | |
524 | return BPF_CLASS(insn->code) == BPF_STX && | |
525 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
526 | insn->imm == BPF_CMPXCHG; | |
527 | } | |
528 | ||
c25b2ae1 HL |
529 | /* string representation of 'enum bpf_reg_type' |
530 | * | |
531 | * Note that reg_type_str() can not appear more than once in a single verbose() | |
532 | * statement. | |
533 | */ | |
534 | static const char *reg_type_str(struct bpf_verifier_env *env, | |
535 | enum bpf_reg_type type) | |
536 | { | |
c6f1bfe8 | 537 | char postfix[16] = {0}, prefix[32] = {0}; |
c25b2ae1 HL |
538 | static const char * const str[] = { |
539 | [NOT_INIT] = "?", | |
7df5072c | 540 | [SCALAR_VALUE] = "scalar", |
c25b2ae1 HL |
541 | [PTR_TO_CTX] = "ctx", |
542 | [CONST_PTR_TO_MAP] = "map_ptr", | |
543 | [PTR_TO_MAP_VALUE] = "map_value", | |
544 | [PTR_TO_STACK] = "fp", | |
545 | [PTR_TO_PACKET] = "pkt", | |
546 | [PTR_TO_PACKET_META] = "pkt_meta", | |
547 | [PTR_TO_PACKET_END] = "pkt_end", | |
548 | [PTR_TO_FLOW_KEYS] = "flow_keys", | |
549 | [PTR_TO_SOCKET] = "sock", | |
550 | [PTR_TO_SOCK_COMMON] = "sock_common", | |
551 | [PTR_TO_TCP_SOCK] = "tcp_sock", | |
552 | [PTR_TO_TP_BUFFER] = "tp_buffer", | |
553 | [PTR_TO_XDP_SOCK] = "xdp_sock", | |
554 | [PTR_TO_BTF_ID] = "ptr_", | |
c25b2ae1 | 555 | [PTR_TO_MEM] = "mem", |
20b2aff4 | 556 | [PTR_TO_BUF] = "buf", |
c25b2ae1 HL |
557 | [PTR_TO_FUNC] = "func", |
558 | [PTR_TO_MAP_KEY] = "map_key", | |
20571567 | 559 | [PTR_TO_DYNPTR] = "dynptr_ptr", |
c25b2ae1 HL |
560 | }; |
561 | ||
562 | if (type & PTR_MAYBE_NULL) { | |
5844101a | 563 | if (base_type(type) == PTR_TO_BTF_ID) |
c25b2ae1 HL |
564 | strncpy(postfix, "or_null_", 16); |
565 | else | |
566 | strncpy(postfix, "_or_null", 16); | |
567 | } | |
568 | ||
20b2aff4 | 569 | if (type & MEM_RDONLY) |
c6f1bfe8 | 570 | strncpy(prefix, "rdonly_", 32); |
a672b2e3 | 571 | if (type & MEM_ALLOC) |
c6f1bfe8 YS |
572 | strncpy(prefix, "alloc_", 32); |
573 | if (type & MEM_USER) | |
574 | strncpy(prefix, "user_", 32); | |
5844101a HL |
575 | if (type & MEM_PERCPU) |
576 | strncpy(prefix, "percpu_", 32); | |
6efe152d KKD |
577 | if (type & PTR_UNTRUSTED) |
578 | strncpy(prefix, "untrusted_", 32); | |
20b2aff4 HL |
579 | |
580 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", | |
581 | prefix, str[base_type(type)], postfix); | |
c25b2ae1 HL |
582 | return env->type_str_buf; |
583 | } | |
17a52670 | 584 | |
8efea21d EC |
585 | static char slot_type_char[] = { |
586 | [STACK_INVALID] = '?', | |
587 | [STACK_SPILL] = 'r', | |
588 | [STACK_MISC] = 'm', | |
589 | [STACK_ZERO] = '0', | |
97e03f52 | 590 | [STACK_DYNPTR] = 'd', |
8efea21d EC |
591 | }; |
592 | ||
4e92024a AS |
593 | static void print_liveness(struct bpf_verifier_env *env, |
594 | enum bpf_reg_liveness live) | |
595 | { | |
9242b5f5 | 596 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
597 | verbose(env, "_"); |
598 | if (live & REG_LIVE_READ) | |
599 | verbose(env, "r"); | |
600 | if (live & REG_LIVE_WRITTEN) | |
601 | verbose(env, "w"); | |
9242b5f5 AS |
602 | if (live & REG_LIVE_DONE) |
603 | verbose(env, "D"); | |
4e92024a AS |
604 | } |
605 | ||
97e03f52 JK |
606 | static int get_spi(s32 off) |
607 | { | |
608 | return (-off - 1) / BPF_REG_SIZE; | |
609 | } | |
610 | ||
611 | static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots) | |
612 | { | |
613 | int allocated_slots = state->allocated_stack / BPF_REG_SIZE; | |
614 | ||
615 | /* We need to check that slots between [spi - nr_slots + 1, spi] are | |
616 | * within [0, allocated_stack). | |
617 | * | |
618 | * Please note that the spi grows downwards. For example, a dynptr | |
619 | * takes the size of two stack slots; the first slot will be at | |
620 | * spi and the second slot will be at spi - 1. | |
621 | */ | |
622 | return spi - nr_slots + 1 >= 0 && spi < allocated_slots; | |
623 | } | |
624 | ||
f4d7e40a AS |
625 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
626 | const struct bpf_reg_state *reg) | |
627 | { | |
628 | struct bpf_verifier_state *cur = env->cur_state; | |
629 | ||
630 | return cur->frame[reg->frameno]; | |
631 | } | |
632 | ||
22dc4a0f | 633 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
9e15db66 | 634 | { |
22dc4a0f | 635 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
636 | } |
637 | ||
0f55f9ed CL |
638 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
639 | { | |
640 | env->scratched_regs |= 1U << regno; | |
641 | } | |
642 | ||
643 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
644 | { | |
343e5375 | 645 | env->scratched_stack_slots |= 1ULL << spi; |
0f55f9ed CL |
646 | } |
647 | ||
648 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
649 | { | |
650 | return (env->scratched_regs >> regno) & 1; | |
651 | } | |
652 | ||
653 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
654 | { | |
655 | return (env->scratched_stack_slots >> regno) & 1; | |
656 | } | |
657 | ||
658 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
659 | { | |
660 | return env->scratched_regs || env->scratched_stack_slots; | |
661 | } | |
662 | ||
663 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
664 | { | |
665 | env->scratched_regs = 0U; | |
343e5375 | 666 | env->scratched_stack_slots = 0ULL; |
0f55f9ed CL |
667 | } |
668 | ||
669 | /* Used for printing the entire verifier state. */ | |
670 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
671 | { | |
672 | env->scratched_regs = ~0U; | |
343e5375 | 673 | env->scratched_stack_slots = ~0ULL; |
0f55f9ed CL |
674 | } |
675 | ||
97e03f52 JK |
676 | static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type) |
677 | { | |
678 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
679 | case DYNPTR_TYPE_LOCAL: | |
680 | return BPF_DYNPTR_TYPE_LOCAL; | |
bc34dee6 JK |
681 | case DYNPTR_TYPE_RINGBUF: |
682 | return BPF_DYNPTR_TYPE_RINGBUF; | |
97e03f52 JK |
683 | default: |
684 | return BPF_DYNPTR_TYPE_INVALID; | |
685 | } | |
686 | } | |
687 | ||
bc34dee6 JK |
688 | static bool dynptr_type_refcounted(enum bpf_dynptr_type type) |
689 | { | |
690 | return type == BPF_DYNPTR_TYPE_RINGBUF; | |
691 | } | |
692 | ||
97e03f52 JK |
693 | static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
694 | enum bpf_arg_type arg_type, int insn_idx) | |
695 | { | |
696 | struct bpf_func_state *state = func(env, reg); | |
697 | enum bpf_dynptr_type type; | |
bc34dee6 | 698 | int spi, i, id; |
97e03f52 JK |
699 | |
700 | spi = get_spi(reg->off); | |
701 | ||
702 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
703 | return -EINVAL; | |
704 | ||
705 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
706 | state->stack[spi].slot_type[i] = STACK_DYNPTR; | |
707 | state->stack[spi - 1].slot_type[i] = STACK_DYNPTR; | |
708 | } | |
709 | ||
710 | type = arg_to_dynptr_type(arg_type); | |
711 | if (type == BPF_DYNPTR_TYPE_INVALID) | |
712 | return -EINVAL; | |
713 | ||
714 | state->stack[spi].spilled_ptr.dynptr.first_slot = true; | |
715 | state->stack[spi].spilled_ptr.dynptr.type = type; | |
716 | state->stack[spi - 1].spilled_ptr.dynptr.type = type; | |
717 | ||
bc34dee6 JK |
718 | if (dynptr_type_refcounted(type)) { |
719 | /* The id is used to track proper releasing */ | |
720 | id = acquire_reference_state(env, insn_idx); | |
721 | if (id < 0) | |
722 | return id; | |
723 | ||
724 | state->stack[spi].spilled_ptr.id = id; | |
725 | state->stack[spi - 1].spilled_ptr.id = id; | |
726 | } | |
727 | ||
97e03f52 JK |
728 | return 0; |
729 | } | |
730 | ||
731 | static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
732 | { | |
733 | struct bpf_func_state *state = func(env, reg); | |
734 | int spi, i; | |
735 | ||
736 | spi = get_spi(reg->off); | |
737 | ||
738 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
739 | return -EINVAL; | |
740 | ||
741 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
742 | state->stack[spi].slot_type[i] = STACK_INVALID; | |
743 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
744 | } | |
745 | ||
bc34dee6 JK |
746 | /* Invalidate any slices associated with this dynptr */ |
747 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) { | |
748 | release_reference(env, state->stack[spi].spilled_ptr.id); | |
749 | state->stack[spi].spilled_ptr.id = 0; | |
750 | state->stack[spi - 1].spilled_ptr.id = 0; | |
751 | } | |
752 | ||
97e03f52 JK |
753 | state->stack[spi].spilled_ptr.dynptr.first_slot = false; |
754 | state->stack[spi].spilled_ptr.dynptr.type = 0; | |
755 | state->stack[spi - 1].spilled_ptr.dynptr.type = 0; | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
761 | { | |
762 | struct bpf_func_state *state = func(env, reg); | |
763 | int spi = get_spi(reg->off); | |
764 | int i; | |
765 | ||
766 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
767 | return true; | |
768 | ||
769 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
770 | if (state->stack[spi].slot_type[i] == STACK_DYNPTR || | |
771 | state->stack[spi - 1].slot_type[i] == STACK_DYNPTR) | |
772 | return false; | |
773 | } | |
774 | ||
775 | return true; | |
776 | } | |
777 | ||
b8d31762 RS |
778 | bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, |
779 | struct bpf_reg_state *reg) | |
97e03f52 JK |
780 | { |
781 | struct bpf_func_state *state = func(env, reg); | |
782 | int spi = get_spi(reg->off); | |
783 | int i; | |
784 | ||
785 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS) || | |
786 | !state->stack[spi].spilled_ptr.dynptr.first_slot) | |
787 | return false; | |
788 | ||
789 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
790 | if (state->stack[spi].slot_type[i] != STACK_DYNPTR || | |
791 | state->stack[spi - 1].slot_type[i] != STACK_DYNPTR) | |
792 | return false; | |
793 | } | |
794 | ||
e9e315b4 RS |
795 | return true; |
796 | } | |
797 | ||
b8d31762 RS |
798 | bool is_dynptr_type_expected(struct bpf_verifier_env *env, |
799 | struct bpf_reg_state *reg, | |
800 | enum bpf_arg_type arg_type) | |
e9e315b4 RS |
801 | { |
802 | struct bpf_func_state *state = func(env, reg); | |
803 | enum bpf_dynptr_type dynptr_type; | |
804 | int spi = get_spi(reg->off); | |
805 | ||
97e03f52 JK |
806 | /* ARG_PTR_TO_DYNPTR takes any type of dynptr */ |
807 | if (arg_type == ARG_PTR_TO_DYNPTR) | |
808 | return true; | |
809 | ||
e9e315b4 RS |
810 | dynptr_type = arg_to_dynptr_type(arg_type); |
811 | ||
812 | return state->stack[spi].spilled_ptr.dynptr.type == dynptr_type; | |
97e03f52 JK |
813 | } |
814 | ||
27113c59 MKL |
815 | /* The reg state of a pointer or a bounded scalar was saved when |
816 | * it was spilled to the stack. | |
817 | */ | |
818 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
819 | { | |
820 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
821 | } | |
822 | ||
354e8f19 MKL |
823 | static void scrub_spilled_slot(u8 *stype) |
824 | { | |
825 | if (*stype != STACK_INVALID) | |
826 | *stype = STACK_MISC; | |
827 | } | |
828 | ||
61bd5218 | 829 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
830 | const struct bpf_func_state *state, |
831 | bool print_all) | |
17a52670 | 832 | { |
f4d7e40a | 833 | const struct bpf_reg_state *reg; |
17a52670 AS |
834 | enum bpf_reg_type t; |
835 | int i; | |
836 | ||
f4d7e40a AS |
837 | if (state->frameno) |
838 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 839 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
840 | reg = &state->regs[i]; |
841 | t = reg->type; | |
17a52670 AS |
842 | if (t == NOT_INIT) |
843 | continue; | |
0f55f9ed CL |
844 | if (!print_all && !reg_scratched(env, i)) |
845 | continue; | |
4e92024a AS |
846 | verbose(env, " R%d", i); |
847 | print_liveness(env, reg->live); | |
7df5072c | 848 | verbose(env, "="); |
b5dc0163 AS |
849 | if (t == SCALAR_VALUE && reg->precise) |
850 | verbose(env, "P"); | |
f1174f77 EC |
851 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
852 | tnum_is_const(reg->var_off)) { | |
853 | /* reg->off should be 0 for SCALAR_VALUE */ | |
7df5072c | 854 | verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
61bd5218 | 855 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 856 | } else { |
7df5072c ML |
857 | const char *sep = ""; |
858 | ||
859 | verbose(env, "%s", reg_type_str(env, t)); | |
5844101a | 860 | if (base_type(t) == PTR_TO_BTF_ID) |
22dc4a0f | 861 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
7df5072c ML |
862 | verbose(env, "("); |
863 | /* | |
864 | * _a stands for append, was shortened to avoid multiline statements below. | |
865 | * This macro is used to output a comma separated list of attributes. | |
866 | */ | |
867 | #define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) | |
868 | ||
869 | if (reg->id) | |
870 | verbose_a("id=%d", reg->id); | |
a28ace78 | 871 | if (reg->ref_obj_id) |
7df5072c | 872 | verbose_a("ref_obj_id=%d", reg->ref_obj_id); |
f1174f77 | 873 | if (t != SCALAR_VALUE) |
7df5072c | 874 | verbose_a("off=%d", reg->off); |
de8f3a83 | 875 | if (type_is_pkt_pointer(t)) |
7df5072c | 876 | verbose_a("r=%d", reg->range); |
c25b2ae1 HL |
877 | else if (base_type(t) == CONST_PTR_TO_MAP || |
878 | base_type(t) == PTR_TO_MAP_KEY || | |
879 | base_type(t) == PTR_TO_MAP_VALUE) | |
7df5072c ML |
880 | verbose_a("ks=%d,vs=%d", |
881 | reg->map_ptr->key_size, | |
882 | reg->map_ptr->value_size); | |
7d1238f2 EC |
883 | if (tnum_is_const(reg->var_off)) { |
884 | /* Typically an immediate SCALAR_VALUE, but | |
885 | * could be a pointer whose offset is too big | |
886 | * for reg->off | |
887 | */ | |
7df5072c | 888 | verbose_a("imm=%llx", reg->var_off.value); |
7d1238f2 EC |
889 | } else { |
890 | if (reg->smin_value != reg->umin_value && | |
891 | reg->smin_value != S64_MIN) | |
7df5072c | 892 | verbose_a("smin=%lld", (long long)reg->smin_value); |
7d1238f2 EC |
893 | if (reg->smax_value != reg->umax_value && |
894 | reg->smax_value != S64_MAX) | |
7df5072c | 895 | verbose_a("smax=%lld", (long long)reg->smax_value); |
7d1238f2 | 896 | if (reg->umin_value != 0) |
7df5072c | 897 | verbose_a("umin=%llu", (unsigned long long)reg->umin_value); |
7d1238f2 | 898 | if (reg->umax_value != U64_MAX) |
7df5072c | 899 | verbose_a("umax=%llu", (unsigned long long)reg->umax_value); |
7d1238f2 EC |
900 | if (!tnum_is_unknown(reg->var_off)) { |
901 | char tn_buf[48]; | |
f1174f77 | 902 | |
7d1238f2 | 903 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
7df5072c | 904 | verbose_a("var_off=%s", tn_buf); |
7d1238f2 | 905 | } |
3f50f132 JF |
906 | if (reg->s32_min_value != reg->smin_value && |
907 | reg->s32_min_value != S32_MIN) | |
7df5072c | 908 | verbose_a("s32_min=%d", (int)(reg->s32_min_value)); |
3f50f132 JF |
909 | if (reg->s32_max_value != reg->smax_value && |
910 | reg->s32_max_value != S32_MAX) | |
7df5072c | 911 | verbose_a("s32_max=%d", (int)(reg->s32_max_value)); |
3f50f132 JF |
912 | if (reg->u32_min_value != reg->umin_value && |
913 | reg->u32_min_value != U32_MIN) | |
7df5072c | 914 | verbose_a("u32_min=%d", (int)(reg->u32_min_value)); |
3f50f132 JF |
915 | if (reg->u32_max_value != reg->umax_value && |
916 | reg->u32_max_value != U32_MAX) | |
7df5072c | 917 | verbose_a("u32_max=%d", (int)(reg->u32_max_value)); |
f1174f77 | 918 | } |
7df5072c ML |
919 | #undef verbose_a |
920 | ||
61bd5218 | 921 | verbose(env, ")"); |
f1174f77 | 922 | } |
17a52670 | 923 | } |
638f5b90 | 924 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
925 | char types_buf[BPF_REG_SIZE + 1]; |
926 | bool valid = false; | |
927 | int j; | |
928 | ||
929 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
930 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
931 | valid = true; | |
932 | types_buf[j] = slot_type_char[ | |
933 | state->stack[i].slot_type[j]]; | |
934 | } | |
935 | types_buf[BPF_REG_SIZE] = 0; | |
936 | if (!valid) | |
937 | continue; | |
0f55f9ed CL |
938 | if (!print_all && !stack_slot_scratched(env, i)) |
939 | continue; | |
8efea21d EC |
940 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
941 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
27113c59 | 942 | if (is_spilled_reg(&state->stack[i])) { |
b5dc0163 AS |
943 | reg = &state->stack[i].spilled_ptr; |
944 | t = reg->type; | |
7df5072c | 945 | verbose(env, "=%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
b5dc0163 AS |
946 | if (t == SCALAR_VALUE && reg->precise) |
947 | verbose(env, "P"); | |
948 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
949 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
950 | } else { | |
8efea21d | 951 | verbose(env, "=%s", types_buf); |
b5dc0163 | 952 | } |
17a52670 | 953 | } |
fd978bf7 JS |
954 | if (state->acquired_refs && state->refs[0].id) { |
955 | verbose(env, " refs=%d", state->refs[0].id); | |
956 | for (i = 1; i < state->acquired_refs; i++) | |
957 | if (state->refs[i].id) | |
958 | verbose(env, ",%d", state->refs[i].id); | |
959 | } | |
bfc6bb74 AS |
960 | if (state->in_callback_fn) |
961 | verbose(env, " cb"); | |
962 | if (state->in_async_callback_fn) | |
963 | verbose(env, " async_cb"); | |
61bd5218 | 964 | verbose(env, "\n"); |
0f55f9ed | 965 | mark_verifier_state_clean(env); |
17a52670 AS |
966 | } |
967 | ||
2e576648 CL |
968 | static inline u32 vlog_alignment(u32 pos) |
969 | { | |
970 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
971 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
972 | } | |
973 | ||
974 | static void print_insn_state(struct bpf_verifier_env *env, | |
975 | const struct bpf_func_state *state) | |
976 | { | |
977 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { | |
978 | /* remove new line character */ | |
979 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); | |
980 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); | |
981 | } else { | |
982 | verbose(env, "%d:", env->insn_idx); | |
983 | } | |
984 | print_verifier_state(env, state, false); | |
17a52670 AS |
985 | } |
986 | ||
c69431aa LB |
987 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
988 | * small to hold src. This is different from krealloc since we don't want to preserve | |
989 | * the contents of dst. | |
990 | * | |
991 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
992 | * not be allocated. | |
638f5b90 | 993 | */ |
c69431aa | 994 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 995 | { |
c69431aa LB |
996 | size_t bytes; |
997 | ||
998 | if (ZERO_OR_NULL_PTR(src)) | |
999 | goto out; | |
1000 | ||
1001 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
1002 | return NULL; | |
1003 | ||
1004 | if (ksize(dst) < bytes) { | |
1005 | kfree(dst); | |
1006 | dst = kmalloc_track_caller(bytes, flags); | |
1007 | if (!dst) | |
1008 | return NULL; | |
1009 | } | |
1010 | ||
1011 | memcpy(dst, src, bytes); | |
1012 | out: | |
1013 | return dst ? dst : ZERO_SIZE_PTR; | |
1014 | } | |
1015 | ||
1016 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
1017 | * small to hold new_n items. new items are zeroed out if the array grows. | |
1018 | * | |
1019 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
1020 | */ | |
1021 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
1022 | { | |
1023 | if (!new_n || old_n == new_n) | |
1024 | goto out; | |
1025 | ||
1026 | arr = krealloc_array(arr, new_n, size, GFP_KERNEL); | |
1027 | if (!arr) | |
1028 | return NULL; | |
1029 | ||
1030 | if (new_n > old_n) | |
1031 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
1032 | ||
1033 | out: | |
1034 | return arr ? arr : ZERO_SIZE_PTR; | |
1035 | } | |
1036 | ||
1037 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1038 | { | |
1039 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
1040 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
1041 | if (!dst->refs) | |
1042 | return -ENOMEM; | |
1043 | ||
1044 | dst->acquired_refs = src->acquired_refs; | |
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1049 | { | |
1050 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
1051 | ||
1052 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
1053 | GFP_KERNEL); | |
1054 | if (!dst->stack) | |
1055 | return -ENOMEM; | |
1056 | ||
1057 | dst->allocated_stack = src->allocated_stack; | |
1058 | return 0; | |
1059 | } | |
1060 | ||
1061 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
1062 | { | |
1063 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
1064 | sizeof(struct bpf_reference_state)); | |
1065 | if (!state->refs) | |
1066 | return -ENOMEM; | |
1067 | ||
1068 | state->acquired_refs = n; | |
1069 | return 0; | |
1070 | } | |
1071 | ||
1072 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
1073 | { | |
1074 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
1075 | ||
1076 | if (old_n >= n) | |
1077 | return 0; | |
1078 | ||
1079 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
1080 | if (!state->stack) | |
1081 | return -ENOMEM; | |
1082 | ||
1083 | state->allocated_stack = size; | |
1084 | return 0; | |
fd978bf7 JS |
1085 | } |
1086 | ||
1087 | /* Acquire a pointer id from the env and update the state->refs to include | |
1088 | * this new pointer reference. | |
1089 | * On success, returns a valid pointer id to associate with the register | |
1090 | * On failure, returns a negative errno. | |
638f5b90 | 1091 | */ |
fd978bf7 | 1092 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 1093 | { |
fd978bf7 JS |
1094 | struct bpf_func_state *state = cur_func(env); |
1095 | int new_ofs = state->acquired_refs; | |
1096 | int id, err; | |
1097 | ||
c69431aa | 1098 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
1099 | if (err) |
1100 | return err; | |
1101 | id = ++env->id_gen; | |
1102 | state->refs[new_ofs].id = id; | |
1103 | state->refs[new_ofs].insn_idx = insn_idx; | |
9d9d00ac | 1104 | state->refs[new_ofs].callback_ref = state->in_callback_fn ? state->frameno : 0; |
638f5b90 | 1105 | |
fd978bf7 JS |
1106 | return id; |
1107 | } | |
1108 | ||
1109 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 1110 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
1111 | { |
1112 | int i, last_idx; | |
1113 | ||
fd978bf7 JS |
1114 | last_idx = state->acquired_refs - 1; |
1115 | for (i = 0; i < state->acquired_refs; i++) { | |
1116 | if (state->refs[i].id == ptr_id) { | |
9d9d00ac KKD |
1117 | /* Cannot release caller references in callbacks */ |
1118 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) | |
1119 | return -EINVAL; | |
fd978bf7 JS |
1120 | if (last_idx && i != last_idx) |
1121 | memcpy(&state->refs[i], &state->refs[last_idx], | |
1122 | sizeof(*state->refs)); | |
1123 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
1124 | state->acquired_refs--; | |
638f5b90 | 1125 | return 0; |
638f5b90 | 1126 | } |
638f5b90 | 1127 | } |
46f8bc92 | 1128 | return -EINVAL; |
fd978bf7 JS |
1129 | } |
1130 | ||
f4d7e40a AS |
1131 | static void free_func_state(struct bpf_func_state *state) |
1132 | { | |
5896351e AS |
1133 | if (!state) |
1134 | return; | |
fd978bf7 | 1135 | kfree(state->refs); |
f4d7e40a AS |
1136 | kfree(state->stack); |
1137 | kfree(state); | |
1138 | } | |
1139 | ||
b5dc0163 AS |
1140 | static void clear_jmp_history(struct bpf_verifier_state *state) |
1141 | { | |
1142 | kfree(state->jmp_history); | |
1143 | state->jmp_history = NULL; | |
1144 | state->jmp_history_cnt = 0; | |
1145 | } | |
1146 | ||
1969db47 AS |
1147 | static void free_verifier_state(struct bpf_verifier_state *state, |
1148 | bool free_self) | |
638f5b90 | 1149 | { |
f4d7e40a AS |
1150 | int i; |
1151 | ||
1152 | for (i = 0; i <= state->curframe; i++) { | |
1153 | free_func_state(state->frame[i]); | |
1154 | state->frame[i] = NULL; | |
1155 | } | |
b5dc0163 | 1156 | clear_jmp_history(state); |
1969db47 AS |
1157 | if (free_self) |
1158 | kfree(state); | |
638f5b90 AS |
1159 | } |
1160 | ||
1161 | /* copy verifier state from src to dst growing dst stack space | |
1162 | * when necessary to accommodate larger src stack | |
1163 | */ | |
f4d7e40a AS |
1164 | static int copy_func_state(struct bpf_func_state *dst, |
1165 | const struct bpf_func_state *src) | |
638f5b90 AS |
1166 | { |
1167 | int err; | |
1168 | ||
fd978bf7 JS |
1169 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
1170 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
1171 | if (err) |
1172 | return err; | |
638f5b90 AS |
1173 | return copy_stack_state(dst, src); |
1174 | } | |
1175 | ||
f4d7e40a AS |
1176 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1177 | const struct bpf_verifier_state *src) | |
1178 | { | |
1179 | struct bpf_func_state *dst; | |
1180 | int i, err; | |
1181 | ||
06ab6a50 LB |
1182 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1183 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1184 | GFP_USER); | |
1185 | if (!dst_state->jmp_history) | |
1186 | return -ENOMEM; | |
b5dc0163 AS |
1187 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1188 | ||
f4d7e40a AS |
1189 | /* if dst has more stack frames then src frame, free them */ |
1190 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1191 | free_func_state(dst_state->frame[i]); | |
1192 | dst_state->frame[i] = NULL; | |
1193 | } | |
979d63d5 | 1194 | dst_state->speculative = src->speculative; |
f4d7e40a | 1195 | dst_state->curframe = src->curframe; |
d83525ca | 1196 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
1197 | dst_state->branches = src->branches; |
1198 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1199 | dst_state->first_insn_idx = src->first_insn_idx; |
1200 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1201 | for (i = 0; i <= src->curframe; i++) { |
1202 | dst = dst_state->frame[i]; | |
1203 | if (!dst) { | |
1204 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1205 | if (!dst) | |
1206 | return -ENOMEM; | |
1207 | dst_state->frame[i] = dst; | |
1208 | } | |
1209 | err = copy_func_state(dst, src->frame[i]); | |
1210 | if (err) | |
1211 | return err; | |
1212 | } | |
1213 | return 0; | |
1214 | } | |
1215 | ||
2589726d AS |
1216 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1217 | { | |
1218 | while (st) { | |
1219 | u32 br = --st->branches; | |
1220 | ||
1221 | /* WARN_ON(br > 1) technically makes sense here, | |
1222 | * but see comment in push_stack(), hence: | |
1223 | */ | |
1224 | WARN_ONCE((int)br < 0, | |
1225 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1226 | br); | |
1227 | if (br) | |
1228 | break; | |
1229 | st = st->parent; | |
1230 | } | |
1231 | } | |
1232 | ||
638f5b90 | 1233 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1234 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1235 | { |
1236 | struct bpf_verifier_state *cur = env->cur_state; | |
1237 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1238 | int err; | |
17a52670 AS |
1239 | |
1240 | if (env->head == NULL) | |
638f5b90 | 1241 | return -ENOENT; |
17a52670 | 1242 | |
638f5b90 AS |
1243 | if (cur) { |
1244 | err = copy_verifier_state(cur, &head->st); | |
1245 | if (err) | |
1246 | return err; | |
1247 | } | |
6f8a57cc AN |
1248 | if (pop_log) |
1249 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1250 | if (insn_idx) |
1251 | *insn_idx = head->insn_idx; | |
17a52670 | 1252 | if (prev_insn_idx) |
638f5b90 AS |
1253 | *prev_insn_idx = head->prev_insn_idx; |
1254 | elem = head->next; | |
1969db47 | 1255 | free_verifier_state(&head->st, false); |
638f5b90 | 1256 | kfree(head); |
17a52670 AS |
1257 | env->head = elem; |
1258 | env->stack_size--; | |
638f5b90 | 1259 | return 0; |
17a52670 AS |
1260 | } |
1261 | ||
58e2af8b | 1262 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1263 | int insn_idx, int prev_insn_idx, |
1264 | bool speculative) | |
17a52670 | 1265 | { |
638f5b90 | 1266 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1267 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1268 | int err; |
17a52670 | 1269 | |
638f5b90 | 1270 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1271 | if (!elem) |
1272 | goto err; | |
1273 | ||
17a52670 AS |
1274 | elem->insn_idx = insn_idx; |
1275 | elem->prev_insn_idx = prev_insn_idx; | |
1276 | elem->next = env->head; | |
6f8a57cc | 1277 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1278 | env->head = elem; |
1279 | env->stack_size++; | |
1969db47 AS |
1280 | err = copy_verifier_state(&elem->st, cur); |
1281 | if (err) | |
1282 | goto err; | |
979d63d5 | 1283 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1284 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1285 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1286 | env->stack_size); | |
17a52670 AS |
1287 | goto err; |
1288 | } | |
2589726d AS |
1289 | if (elem->st.parent) { |
1290 | ++elem->st.parent->branches; | |
1291 | /* WARN_ON(branches > 2) technically makes sense here, | |
1292 | * but | |
1293 | * 1. speculative states will bump 'branches' for non-branch | |
1294 | * instructions | |
1295 | * 2. is_state_visited() heuristics may decide not to create | |
1296 | * a new state for a sequence of branches and all such current | |
1297 | * and cloned states will be pointing to a single parent state | |
1298 | * which might have large 'branches' count. | |
1299 | */ | |
1300 | } | |
17a52670 AS |
1301 | return &elem->st; |
1302 | err: | |
5896351e AS |
1303 | free_verifier_state(env->cur_state, true); |
1304 | env->cur_state = NULL; | |
17a52670 | 1305 | /* pop all elements and return */ |
6f8a57cc | 1306 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1307 | return NULL; |
1308 | } | |
1309 | ||
1310 | #define CALLER_SAVED_REGS 6 | |
1311 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1312 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1313 | }; | |
1314 | ||
f54c7898 DB |
1315 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1316 | struct bpf_reg_state *reg); | |
f1174f77 | 1317 | |
e688c3db AS |
1318 | /* This helper doesn't clear reg->id */ |
1319 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1320 | { |
b03c9f9f EC |
1321 | reg->var_off = tnum_const(imm); |
1322 | reg->smin_value = (s64)imm; | |
1323 | reg->smax_value = (s64)imm; | |
1324 | reg->umin_value = imm; | |
1325 | reg->umax_value = imm; | |
3f50f132 JF |
1326 | |
1327 | reg->s32_min_value = (s32)imm; | |
1328 | reg->s32_max_value = (s32)imm; | |
1329 | reg->u32_min_value = (u32)imm; | |
1330 | reg->u32_max_value = (u32)imm; | |
1331 | } | |
1332 | ||
e688c3db AS |
1333 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1334 | * known to have the value @imm. | |
1335 | */ | |
1336 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1337 | { | |
1338 | /* Clear id, off, and union(map_ptr, range) */ | |
1339 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1340 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
1341 | ___mark_reg_known(reg, imm); | |
1342 | } | |
1343 | ||
3f50f132 JF |
1344 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1345 | { | |
1346 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1347 | reg->s32_min_value = (s32)imm; | |
1348 | reg->s32_max_value = (s32)imm; | |
1349 | reg->u32_min_value = (u32)imm; | |
1350 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1351 | } |
1352 | ||
f1174f77 EC |
1353 | /* Mark the 'variable offset' part of a register as zero. This should be |
1354 | * used only on registers holding a pointer type. | |
1355 | */ | |
1356 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1357 | { |
b03c9f9f | 1358 | __mark_reg_known(reg, 0); |
f1174f77 | 1359 | } |
a9789ef9 | 1360 | |
cc2b14d5 AS |
1361 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1362 | { | |
1363 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1364 | reg->type = SCALAR_VALUE; |
1365 | } | |
1366 | ||
61bd5218 JK |
1367 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1368 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1369 | { |
1370 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1371 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1372 | /* Something bad happened, let's kill all regs */ |
1373 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1374 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1375 | return; |
1376 | } | |
1377 | __mark_reg_known_zero(regs + regno); | |
1378 | } | |
1379 | ||
4ddb7416 DB |
1380 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1381 | { | |
c25b2ae1 | 1382 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
4ddb7416 DB |
1383 | const struct bpf_map *map = reg->map_ptr; |
1384 | ||
1385 | if (map->inner_map_meta) { | |
1386 | reg->type = CONST_PTR_TO_MAP; | |
1387 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1388 | /* transfer reg's id which is unique for every map_lookup_elem |
1389 | * as UID of the inner map. | |
1390 | */ | |
db559117 | 1391 | if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER)) |
34d11a44 | 1392 | reg->map_uid = reg->id; |
4ddb7416 DB |
1393 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1394 | reg->type = PTR_TO_XDP_SOCK; | |
1395 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1396 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1397 | reg->type = PTR_TO_SOCKET; | |
1398 | } else { | |
1399 | reg->type = PTR_TO_MAP_VALUE; | |
1400 | } | |
c25b2ae1 | 1401 | return; |
4ddb7416 | 1402 | } |
c25b2ae1 HL |
1403 | |
1404 | reg->type &= ~PTR_MAYBE_NULL; | |
4ddb7416 DB |
1405 | } |
1406 | ||
de8f3a83 DB |
1407 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1408 | { | |
1409 | return type_is_pkt_pointer(reg->type); | |
1410 | } | |
1411 | ||
1412 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1413 | { | |
1414 | return reg_is_pkt_pointer(reg) || | |
1415 | reg->type == PTR_TO_PACKET_END; | |
1416 | } | |
1417 | ||
1418 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1419 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1420 | enum bpf_reg_type which) | |
1421 | { | |
1422 | /* The register can already have a range from prior markings. | |
1423 | * This is fine as long as it hasn't been advanced from its | |
1424 | * origin. | |
1425 | */ | |
1426 | return reg->type == which && | |
1427 | reg->id == 0 && | |
1428 | reg->off == 0 && | |
1429 | tnum_equals_const(reg->var_off, 0); | |
1430 | } | |
1431 | ||
3f50f132 JF |
1432 | /* Reset the min/max bounds of a register */ |
1433 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1434 | { | |
1435 | reg->smin_value = S64_MIN; | |
1436 | reg->smax_value = S64_MAX; | |
1437 | reg->umin_value = 0; | |
1438 | reg->umax_value = U64_MAX; | |
1439 | ||
1440 | reg->s32_min_value = S32_MIN; | |
1441 | reg->s32_max_value = S32_MAX; | |
1442 | reg->u32_min_value = 0; | |
1443 | reg->u32_max_value = U32_MAX; | |
1444 | } | |
1445 | ||
1446 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1447 | { | |
1448 | reg->smin_value = S64_MIN; | |
1449 | reg->smax_value = S64_MAX; | |
1450 | reg->umin_value = 0; | |
1451 | reg->umax_value = U64_MAX; | |
1452 | } | |
1453 | ||
1454 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1455 | { | |
1456 | reg->s32_min_value = S32_MIN; | |
1457 | reg->s32_max_value = S32_MAX; | |
1458 | reg->u32_min_value = 0; | |
1459 | reg->u32_max_value = U32_MAX; | |
1460 | } | |
1461 | ||
1462 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1463 | { | |
1464 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1465 | ||
1466 | /* min signed is max(sign bit) | min(other bits) */ | |
1467 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1468 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1469 | /* max signed is min(sign bit) | max(other bits) */ | |
1470 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1471 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1472 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1473 | reg->u32_max_value = min(reg->u32_max_value, | |
1474 | (u32)(var32_off.value | var32_off.mask)); | |
1475 | } | |
1476 | ||
1477 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1478 | { |
1479 | /* min signed is max(sign bit) | min(other bits) */ | |
1480 | reg->smin_value = max_t(s64, reg->smin_value, | |
1481 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1482 | /* max signed is min(sign bit) | max(other bits) */ | |
1483 | reg->smax_value = min_t(s64, reg->smax_value, | |
1484 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1485 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1486 | reg->umax_value = min(reg->umax_value, | |
1487 | reg->var_off.value | reg->var_off.mask); | |
1488 | } | |
1489 | ||
3f50f132 JF |
1490 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1491 | { | |
1492 | __update_reg32_bounds(reg); | |
1493 | __update_reg64_bounds(reg); | |
1494 | } | |
1495 | ||
b03c9f9f | 1496 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1497 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1498 | { | |
1499 | /* Learn sign from signed bounds. | |
1500 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1501 | * are the same, so combine. This works even in the negative case, e.g. | |
1502 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1503 | */ | |
1504 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1505 | reg->s32_min_value = reg->u32_min_value = | |
1506 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1507 | reg->s32_max_value = reg->u32_max_value = | |
1508 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1509 | return; | |
1510 | } | |
1511 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1512 | * boundary, so we must be careful. | |
1513 | */ | |
1514 | if ((s32)reg->u32_max_value >= 0) { | |
1515 | /* Positive. We can't learn anything from the smin, but smax | |
1516 | * is positive, hence safe. | |
1517 | */ | |
1518 | reg->s32_min_value = reg->u32_min_value; | |
1519 | reg->s32_max_value = reg->u32_max_value = | |
1520 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1521 | } else if ((s32)reg->u32_min_value < 0) { | |
1522 | /* Negative. We can't learn anything from the smax, but smin | |
1523 | * is negative, hence safe. | |
1524 | */ | |
1525 | reg->s32_min_value = reg->u32_min_value = | |
1526 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1527 | reg->s32_max_value = reg->u32_max_value; | |
1528 | } | |
1529 | } | |
1530 | ||
1531 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1532 | { |
1533 | /* Learn sign from signed bounds. | |
1534 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1535 | * are the same, so combine. This works even in the negative case, e.g. | |
1536 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1537 | */ | |
1538 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1539 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1540 | reg->umin_value); | |
1541 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1542 | reg->umax_value); | |
1543 | return; | |
1544 | } | |
1545 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1546 | * boundary, so we must be careful. | |
1547 | */ | |
1548 | if ((s64)reg->umax_value >= 0) { | |
1549 | /* Positive. We can't learn anything from the smin, but smax | |
1550 | * is positive, hence safe. | |
1551 | */ | |
1552 | reg->smin_value = reg->umin_value; | |
1553 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1554 | reg->umax_value); | |
1555 | } else if ((s64)reg->umin_value < 0) { | |
1556 | /* Negative. We can't learn anything from the smax, but smin | |
1557 | * is negative, hence safe. | |
1558 | */ | |
1559 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1560 | reg->umin_value); | |
1561 | reg->smax_value = reg->umax_value; | |
1562 | } | |
1563 | } | |
1564 | ||
3f50f132 JF |
1565 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1566 | { | |
1567 | __reg32_deduce_bounds(reg); | |
1568 | __reg64_deduce_bounds(reg); | |
1569 | } | |
1570 | ||
b03c9f9f EC |
1571 | /* Attempts to improve var_off based on unsigned min/max information */ |
1572 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1573 | { | |
3f50f132 JF |
1574 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1575 | tnum_range(reg->umin_value, | |
1576 | reg->umax_value)); | |
1577 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1578 | tnum_range(reg->u32_min_value, | |
1579 | reg->u32_max_value)); | |
1580 | ||
1581 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1582 | } |
1583 | ||
3844d153 DB |
1584 | static void reg_bounds_sync(struct bpf_reg_state *reg) |
1585 | { | |
1586 | /* We might have learned new bounds from the var_off. */ | |
1587 | __update_reg_bounds(reg); | |
1588 | /* We might have learned something about the sign bit. */ | |
1589 | __reg_deduce_bounds(reg); | |
1590 | /* We might have learned some bits from the bounds. */ | |
1591 | __reg_bound_offset(reg); | |
1592 | /* Intersecting with the old var_off might have improved our bounds | |
1593 | * slightly, e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1594 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1595 | */ | |
1596 | __update_reg_bounds(reg); | |
1597 | } | |
1598 | ||
e572ff80 DB |
1599 | static bool __reg32_bound_s64(s32 a) |
1600 | { | |
1601 | return a >= 0 && a <= S32_MAX; | |
1602 | } | |
1603 | ||
3f50f132 | 1604 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1605 | { |
3f50f132 JF |
1606 | reg->umin_value = reg->u32_min_value; |
1607 | reg->umax_value = reg->u32_max_value; | |
e572ff80 DB |
1608 | |
1609 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must | |
1610 | * be positive otherwise set to worse case bounds and refine later | |
1611 | * from tnum. | |
3f50f132 | 1612 | */ |
e572ff80 DB |
1613 | if (__reg32_bound_s64(reg->s32_min_value) && |
1614 | __reg32_bound_s64(reg->s32_max_value)) { | |
3a71dc36 | 1615 | reg->smin_value = reg->s32_min_value; |
e572ff80 DB |
1616 | reg->smax_value = reg->s32_max_value; |
1617 | } else { | |
3a71dc36 | 1618 | reg->smin_value = 0; |
e572ff80 DB |
1619 | reg->smax_value = U32_MAX; |
1620 | } | |
3f50f132 JF |
1621 | } |
1622 | ||
1623 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1624 | { | |
1625 | /* special case when 64-bit register has upper 32-bit register | |
1626 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1627 | * allowing us to use 32-bit bounds directly, | |
1628 | */ | |
1629 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1630 | __reg_assign_32_into_64(reg); | |
1631 | } else { | |
1632 | /* Otherwise the best we can do is push lower 32bit known and | |
1633 | * unknown bits into register (var_off set from jmp logic) | |
1634 | * then learn as much as possible from the 64-bit tnum | |
1635 | * known and unknown bits. The previous smin/smax bounds are | |
1636 | * invalid here because of jmp32 compare so mark them unknown | |
1637 | * so they do not impact tnum bounds calculation. | |
1638 | */ | |
1639 | __mark_reg64_unbounded(reg); | |
3f50f132 | 1640 | } |
3844d153 | 1641 | reg_bounds_sync(reg); |
3f50f132 JF |
1642 | } |
1643 | ||
1644 | static bool __reg64_bound_s32(s64 a) | |
1645 | { | |
388e2c0b | 1646 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
1647 | } |
1648 | ||
1649 | static bool __reg64_bound_u32(u64 a) | |
1650 | { | |
b9979db8 | 1651 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
1652 | } |
1653 | ||
1654 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1655 | { | |
1656 | __mark_reg32_unbounded(reg); | |
b0270958 | 1657 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 1658 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 1659 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 1660 | } |
10bf4e83 | 1661 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 1662 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 1663 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 1664 | } |
3844d153 | 1665 | reg_bounds_sync(reg); |
b03c9f9f EC |
1666 | } |
1667 | ||
f1174f77 | 1668 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1669 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1670 | struct bpf_reg_state *reg) | |
f1174f77 | 1671 | { |
a9c676bc AS |
1672 | /* |
1673 | * Clear type, id, off, and union(map_ptr, range) and | |
1674 | * padding between 'type' and union | |
1675 | */ | |
1676 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1677 | reg->type = SCALAR_VALUE; |
f1174f77 | 1678 | reg->var_off = tnum_unknown; |
f4d7e40a | 1679 | reg->frameno = 0; |
2c78ee89 | 1680 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1681 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1682 | } |
1683 | ||
61bd5218 JK |
1684 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1685 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1686 | { |
1687 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1688 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1689 | /* Something bad happened, let's kill all regs except FP */ |
1690 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1691 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1692 | return; |
1693 | } | |
f54c7898 | 1694 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1695 | } |
1696 | ||
f54c7898 DB |
1697 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1698 | struct bpf_reg_state *reg) | |
f1174f77 | 1699 | { |
f54c7898 | 1700 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1701 | reg->type = NOT_INIT; |
1702 | } | |
1703 | ||
61bd5218 JK |
1704 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1705 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1706 | { |
1707 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1708 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1709 | /* Something bad happened, let's kill all regs except FP */ |
1710 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1711 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1712 | return; |
1713 | } | |
f54c7898 | 1714 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1715 | } |
1716 | ||
41c48f3a AI |
1717 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1718 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f | 1719 | enum bpf_reg_type reg_type, |
c6f1bfe8 YS |
1720 | struct btf *btf, u32 btf_id, |
1721 | enum bpf_type_flag flag) | |
41c48f3a AI |
1722 | { |
1723 | if (reg_type == SCALAR_VALUE) { | |
1724 | mark_reg_unknown(env, regs, regno); | |
1725 | return; | |
1726 | } | |
1727 | mark_reg_known_zero(env, regs, regno); | |
c6f1bfe8 | 1728 | regs[regno].type = PTR_TO_BTF_ID | flag; |
22dc4a0f | 1729 | regs[regno].btf = btf; |
41c48f3a AI |
1730 | regs[regno].btf_id = btf_id; |
1731 | } | |
1732 | ||
5327ed3d | 1733 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1734 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1735 | struct bpf_func_state *state) |
17a52670 | 1736 | { |
f4d7e40a | 1737 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1738 | int i; |
1739 | ||
dc503a8a | 1740 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1741 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1742 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1743 | regs[i].parent = NULL; |
5327ed3d | 1744 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1745 | } |
17a52670 AS |
1746 | |
1747 | /* frame pointer */ | |
f1174f77 | 1748 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1749 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1750 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1751 | } |
1752 | ||
f4d7e40a AS |
1753 | #define BPF_MAIN_FUNC (-1) |
1754 | static void init_func_state(struct bpf_verifier_env *env, | |
1755 | struct bpf_func_state *state, | |
1756 | int callsite, int frameno, int subprogno) | |
1757 | { | |
1758 | state->callsite = callsite; | |
1759 | state->frameno = frameno; | |
1760 | state->subprogno = subprogno; | |
1bfe26fb | 1761 | state->callback_ret_range = tnum_range(0, 0); |
f4d7e40a | 1762 | init_reg_state(env, state); |
0f55f9ed | 1763 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
1764 | } |
1765 | ||
bfc6bb74 AS |
1766 | /* Similar to push_stack(), but for async callbacks */ |
1767 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
1768 | int insn_idx, int prev_insn_idx, | |
1769 | int subprog) | |
1770 | { | |
1771 | struct bpf_verifier_stack_elem *elem; | |
1772 | struct bpf_func_state *frame; | |
1773 | ||
1774 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
1775 | if (!elem) | |
1776 | goto err; | |
1777 | ||
1778 | elem->insn_idx = insn_idx; | |
1779 | elem->prev_insn_idx = prev_insn_idx; | |
1780 | elem->next = env->head; | |
1781 | elem->log_pos = env->log.len_used; | |
1782 | env->head = elem; | |
1783 | env->stack_size++; | |
1784 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
1785 | verbose(env, | |
1786 | "The sequence of %d jumps is too complex for async cb.\n", | |
1787 | env->stack_size); | |
1788 | goto err; | |
1789 | } | |
1790 | /* Unlike push_stack() do not copy_verifier_state(). | |
1791 | * The caller state doesn't matter. | |
1792 | * This is async callback. It starts in a fresh stack. | |
1793 | * Initialize it similar to do_check_common(). | |
1794 | */ | |
1795 | elem->st.branches = 1; | |
1796 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
1797 | if (!frame) | |
1798 | goto err; | |
1799 | init_func_state(env, frame, | |
1800 | BPF_MAIN_FUNC /* callsite */, | |
1801 | 0 /* frameno within this callchain */, | |
1802 | subprog /* subprog number within this prog */); | |
1803 | elem->st.frame[0] = frame; | |
1804 | return &elem->st; | |
1805 | err: | |
1806 | free_verifier_state(env->cur_state, true); | |
1807 | env->cur_state = NULL; | |
1808 | /* pop all elements and return */ | |
1809 | while (!pop_stack(env, NULL, NULL, false)); | |
1810 | return NULL; | |
1811 | } | |
1812 | ||
1813 | ||
17a52670 AS |
1814 | enum reg_arg_type { |
1815 | SRC_OP, /* register is used as source operand */ | |
1816 | DST_OP, /* register is used as destination operand */ | |
1817 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1818 | }; | |
1819 | ||
cc8b0b92 AS |
1820 | static int cmp_subprogs(const void *a, const void *b) |
1821 | { | |
9c8105bd JW |
1822 | return ((struct bpf_subprog_info *)a)->start - |
1823 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1824 | } |
1825 | ||
1826 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1827 | { | |
9c8105bd | 1828 | struct bpf_subprog_info *p; |
cc8b0b92 | 1829 | |
9c8105bd JW |
1830 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1831 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1832 | if (!p) |
1833 | return -ENOENT; | |
9c8105bd | 1834 | return p - env->subprog_info; |
cc8b0b92 AS |
1835 | |
1836 | } | |
1837 | ||
1838 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1839 | { | |
1840 | int insn_cnt = env->prog->len; | |
1841 | int ret; | |
1842 | ||
1843 | if (off >= insn_cnt || off < 0) { | |
1844 | verbose(env, "call to invalid destination\n"); | |
1845 | return -EINVAL; | |
1846 | } | |
1847 | ret = find_subprog(env, off); | |
1848 | if (ret >= 0) | |
282a0f46 | 1849 | return ret; |
4cb3d99c | 1850 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1851 | verbose(env, "too many subprograms\n"); |
1852 | return -E2BIG; | |
1853 | } | |
e6ac2450 | 1854 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
1855 | env->subprog_info[env->subprog_cnt++].start = off; |
1856 | sort(env->subprog_info, env->subprog_cnt, | |
1857 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 1858 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
1859 | } |
1860 | ||
2357672c KKD |
1861 | #define MAX_KFUNC_DESCS 256 |
1862 | #define MAX_KFUNC_BTFS 256 | |
1863 | ||
e6ac2450 MKL |
1864 | struct bpf_kfunc_desc { |
1865 | struct btf_func_model func_model; | |
1866 | u32 func_id; | |
1867 | s32 imm; | |
2357672c KKD |
1868 | u16 offset; |
1869 | }; | |
1870 | ||
1871 | struct bpf_kfunc_btf { | |
1872 | struct btf *btf; | |
1873 | struct module *module; | |
1874 | u16 offset; | |
e6ac2450 MKL |
1875 | }; |
1876 | ||
e6ac2450 MKL |
1877 | struct bpf_kfunc_desc_tab { |
1878 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
1879 | u32 nr_descs; | |
1880 | }; | |
1881 | ||
2357672c KKD |
1882 | struct bpf_kfunc_btf_tab { |
1883 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
1884 | u32 nr_descs; | |
1885 | }; | |
1886 | ||
1887 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
1888 | { |
1889 | const struct bpf_kfunc_desc *d0 = a; | |
1890 | const struct bpf_kfunc_desc *d1 = b; | |
1891 | ||
1892 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
1893 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
1894 | } | |
1895 | ||
1896 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
1897 | { | |
1898 | const struct bpf_kfunc_btf *d0 = a; | |
1899 | const struct bpf_kfunc_btf *d1 = b; | |
1900 | ||
1901 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
1902 | } |
1903 | ||
1904 | static const struct bpf_kfunc_desc * | |
2357672c | 1905 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
1906 | { |
1907 | struct bpf_kfunc_desc desc = { | |
1908 | .func_id = func_id, | |
2357672c | 1909 | .offset = offset, |
e6ac2450 MKL |
1910 | }; |
1911 | struct bpf_kfunc_desc_tab *tab; | |
1912 | ||
1913 | tab = prog->aux->kfunc_tab; | |
1914 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
1915 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
1916 | } | |
1917 | ||
1918 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
b202d844 | 1919 | s16 offset) |
2357672c KKD |
1920 | { |
1921 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
1922 | struct bpf_kfunc_btf_tab *tab; | |
1923 | struct bpf_kfunc_btf *b; | |
1924 | struct module *mod; | |
1925 | struct btf *btf; | |
1926 | int btf_fd; | |
1927 | ||
1928 | tab = env->prog->aux->kfunc_btf_tab; | |
1929 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
1930 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
1931 | if (!b) { | |
1932 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
1933 | verbose(env, "too many different module BTFs\n"); | |
1934 | return ERR_PTR(-E2BIG); | |
1935 | } | |
1936 | ||
1937 | if (bpfptr_is_null(env->fd_array)) { | |
1938 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
1939 | return ERR_PTR(-EPROTO); | |
1940 | } | |
1941 | ||
1942 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
1943 | offset * sizeof(btf_fd), | |
1944 | sizeof(btf_fd))) | |
1945 | return ERR_PTR(-EFAULT); | |
1946 | ||
1947 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
1948 | if (IS_ERR(btf)) { |
1949 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 1950 | return btf; |
588cd7ef | 1951 | } |
2357672c KKD |
1952 | |
1953 | if (!btf_is_module(btf)) { | |
1954 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
1955 | btf_put(btf); | |
1956 | return ERR_PTR(-EINVAL); | |
1957 | } | |
1958 | ||
1959 | mod = btf_try_get_module(btf); | |
1960 | if (!mod) { | |
1961 | btf_put(btf); | |
1962 | return ERR_PTR(-ENXIO); | |
1963 | } | |
1964 | ||
1965 | b = &tab->descs[tab->nr_descs++]; | |
1966 | b->btf = btf; | |
1967 | b->module = mod; | |
1968 | b->offset = offset; | |
1969 | ||
1970 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1971 | kfunc_btf_cmp_by_off, NULL); | |
1972 | } | |
2357672c | 1973 | return b->btf; |
e6ac2450 MKL |
1974 | } |
1975 | ||
2357672c KKD |
1976 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
1977 | { | |
1978 | if (!tab) | |
1979 | return; | |
1980 | ||
1981 | while (tab->nr_descs--) { | |
1982 | module_put(tab->descs[tab->nr_descs].module); | |
1983 | btf_put(tab->descs[tab->nr_descs].btf); | |
1984 | } | |
1985 | kfree(tab); | |
1986 | } | |
1987 | ||
43bf0878 | 1988 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset) |
2357672c | 1989 | { |
2357672c KKD |
1990 | if (offset) { |
1991 | if (offset < 0) { | |
1992 | /* In the future, this can be allowed to increase limit | |
1993 | * of fd index into fd_array, interpreted as u16. | |
1994 | */ | |
1995 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
1996 | return ERR_PTR(-EINVAL); | |
1997 | } | |
1998 | ||
b202d844 | 1999 | return __find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2000 | } |
2001 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
2002 | } |
2003 | ||
2357672c | 2004 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
2005 | { |
2006 | const struct btf_type *func, *func_proto; | |
2357672c | 2007 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
2008 | struct bpf_kfunc_desc_tab *tab; |
2009 | struct bpf_prog_aux *prog_aux; | |
2010 | struct bpf_kfunc_desc *desc; | |
2011 | const char *func_name; | |
2357672c | 2012 | struct btf *desc_btf; |
8cbf062a | 2013 | unsigned long call_imm; |
e6ac2450 MKL |
2014 | unsigned long addr; |
2015 | int err; | |
2016 | ||
2017 | prog_aux = env->prog->aux; | |
2018 | tab = prog_aux->kfunc_tab; | |
2357672c | 2019 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
2020 | if (!tab) { |
2021 | if (!btf_vmlinux) { | |
2022 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
2023 | return -ENOTSUPP; | |
2024 | } | |
2025 | ||
2026 | if (!env->prog->jit_requested) { | |
2027 | verbose(env, "JIT is required for calling kernel function\n"); | |
2028 | return -ENOTSUPP; | |
2029 | } | |
2030 | ||
2031 | if (!bpf_jit_supports_kfunc_call()) { | |
2032 | verbose(env, "JIT does not support calling kernel function\n"); | |
2033 | return -ENOTSUPP; | |
2034 | } | |
2035 | ||
2036 | if (!env->prog->gpl_compatible) { | |
2037 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
2038 | return -EINVAL; | |
2039 | } | |
2040 | ||
2041 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
2042 | if (!tab) | |
2043 | return -ENOMEM; | |
2044 | prog_aux->kfunc_tab = tab; | |
2045 | } | |
2046 | ||
a5d82727 KKD |
2047 | /* func_id == 0 is always invalid, but instead of returning an error, be |
2048 | * conservative and wait until the code elimination pass before returning | |
2049 | * error, so that invalid calls that get pruned out can be in BPF programs | |
2050 | * loaded from userspace. It is also required that offset be untouched | |
2051 | * for such calls. | |
2052 | */ | |
2053 | if (!func_id && !offset) | |
2054 | return 0; | |
2055 | ||
2357672c KKD |
2056 | if (!btf_tab && offset) { |
2057 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
2058 | if (!btf_tab) | |
2059 | return -ENOMEM; | |
2060 | prog_aux->kfunc_btf_tab = btf_tab; | |
2061 | } | |
2062 | ||
43bf0878 | 2063 | desc_btf = find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2064 | if (IS_ERR(desc_btf)) { |
2065 | verbose(env, "failed to find BTF for kernel function\n"); | |
2066 | return PTR_ERR(desc_btf); | |
2067 | } | |
2068 | ||
2069 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
2070 | return 0; |
2071 | ||
2072 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
2073 | verbose(env, "too many different kernel function calls\n"); | |
2074 | return -E2BIG; | |
2075 | } | |
2076 | ||
2357672c | 2077 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
2078 | if (!func || !btf_type_is_func(func)) { |
2079 | verbose(env, "kernel btf_id %u is not a function\n", | |
2080 | func_id); | |
2081 | return -EINVAL; | |
2082 | } | |
2357672c | 2083 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
2084 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
2085 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
2086 | func_id); | |
2087 | return -EINVAL; | |
2088 | } | |
2089 | ||
2357672c | 2090 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
2091 | addr = kallsyms_lookup_name(func_name); |
2092 | if (!addr) { | |
2093 | verbose(env, "cannot find address for kernel function %s\n", | |
2094 | func_name); | |
2095 | return -EINVAL; | |
2096 | } | |
2097 | ||
8cbf062a HT |
2098 | call_imm = BPF_CALL_IMM(addr); |
2099 | /* Check whether or not the relative offset overflows desc->imm */ | |
2100 | if ((unsigned long)(s32)call_imm != call_imm) { | |
2101 | verbose(env, "address of kernel function %s is out of range\n", | |
2102 | func_name); | |
2103 | return -EINVAL; | |
2104 | } | |
2105 | ||
e6ac2450 MKL |
2106 | desc = &tab->descs[tab->nr_descs++]; |
2107 | desc->func_id = func_id; | |
8cbf062a | 2108 | desc->imm = call_imm; |
2357672c KKD |
2109 | desc->offset = offset; |
2110 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
2111 | func_proto, func_name, |
2112 | &desc->func_model); | |
2113 | if (!err) | |
2114 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 2115 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
2116 | return err; |
2117 | } | |
2118 | ||
2119 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
2120 | { | |
2121 | const struct bpf_kfunc_desc *d0 = a; | |
2122 | const struct bpf_kfunc_desc *d1 = b; | |
2123 | ||
2124 | if (d0->imm > d1->imm) | |
2125 | return 1; | |
2126 | else if (d0->imm < d1->imm) | |
2127 | return -1; | |
2128 | return 0; | |
2129 | } | |
2130 | ||
2131 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
2132 | { | |
2133 | struct bpf_kfunc_desc_tab *tab; | |
2134 | ||
2135 | tab = prog->aux->kfunc_tab; | |
2136 | if (!tab) | |
2137 | return; | |
2138 | ||
2139 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2140 | kfunc_desc_cmp_by_imm, NULL); | |
2141 | } | |
2142 | ||
2143 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
2144 | { | |
2145 | return !!prog->aux->kfunc_tab; | |
2146 | } | |
2147 | ||
2148 | const struct btf_func_model * | |
2149 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2150 | const struct bpf_insn *insn) | |
2151 | { | |
2152 | const struct bpf_kfunc_desc desc = { | |
2153 | .imm = insn->imm, | |
2154 | }; | |
2155 | const struct bpf_kfunc_desc *res; | |
2156 | struct bpf_kfunc_desc_tab *tab; | |
2157 | ||
2158 | tab = prog->aux->kfunc_tab; | |
2159 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
2160 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
2161 | ||
2162 | return res ? &res->func_model : NULL; | |
2163 | } | |
2164 | ||
2165 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2166 | { |
9c8105bd | 2167 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2168 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2169 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2170 | |
f910cefa JW |
2171 | /* Add entry function. */ |
2172 | ret = add_subprog(env, 0); | |
e6ac2450 | 2173 | if (ret) |
f910cefa JW |
2174 | return ret; |
2175 | ||
e6ac2450 MKL |
2176 | for (i = 0; i < insn_cnt; i++, insn++) { |
2177 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2178 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2179 | continue; |
e6ac2450 | 2180 | |
2c78ee89 | 2181 | if (!env->bpf_capable) { |
e6ac2450 | 2182 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2183 | return -EPERM; |
2184 | } | |
e6ac2450 | 2185 | |
3990ed4c | 2186 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2187 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2188 | else |
2357672c | 2189 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2190 | |
cc8b0b92 AS |
2191 | if (ret < 0) |
2192 | return ret; | |
2193 | } | |
2194 | ||
4cb3d99c JW |
2195 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2196 | * logic. 'subprog_cnt' should not be increased. | |
2197 | */ | |
2198 | subprog[env->subprog_cnt].start = insn_cnt; | |
2199 | ||
06ee7115 | 2200 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2201 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2202 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2203 | |
e6ac2450 MKL |
2204 | return 0; |
2205 | } | |
2206 | ||
2207 | static int check_subprogs(struct bpf_verifier_env *env) | |
2208 | { | |
2209 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2210 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2211 | struct bpf_insn *insn = env->prog->insnsi; | |
2212 | int insn_cnt = env->prog->len; | |
2213 | ||
cc8b0b92 | 2214 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2215 | subprog_start = subprog[cur_subprog].start; |
2216 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2217 | for (i = 0; i < insn_cnt; i++) { |
2218 | u8 code = insn[i].code; | |
2219 | ||
7f6e4312 MF |
2220 | if (code == (BPF_JMP | BPF_CALL) && |
2221 | insn[i].imm == BPF_FUNC_tail_call && | |
2222 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
2223 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
2224 | if (BPF_CLASS(code) == BPF_LD && |
2225 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2226 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2227 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2228 | goto next; |
2229 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2230 | goto next; | |
2231 | off = i + insn[i].off + 1; | |
2232 | if (off < subprog_start || off >= subprog_end) { | |
2233 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2234 | return -EINVAL; | |
2235 | } | |
2236 | next: | |
2237 | if (i == subprog_end - 1) { | |
2238 | /* to avoid fall-through from one subprog into another | |
2239 | * the last insn of the subprog should be either exit | |
2240 | * or unconditional jump back | |
2241 | */ | |
2242 | if (code != (BPF_JMP | BPF_EXIT) && | |
2243 | code != (BPF_JMP | BPF_JA)) { | |
2244 | verbose(env, "last insn is not an exit or jmp\n"); | |
2245 | return -EINVAL; | |
2246 | } | |
2247 | subprog_start = subprog_end; | |
4cb3d99c JW |
2248 | cur_subprog++; |
2249 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2250 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2251 | } |
2252 | } | |
2253 | return 0; | |
2254 | } | |
2255 | ||
679c782d EC |
2256 | /* Parentage chain of this register (or stack slot) should take care of all |
2257 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2258 | */ | |
f4d7e40a | 2259 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2260 | const struct bpf_reg_state *state, |
5327ed3d | 2261 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2262 | { |
2263 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2264 | int cnt = 0; |
dc503a8a EC |
2265 | |
2266 | while (parent) { | |
2267 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2268 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2269 | break; |
9242b5f5 AS |
2270 | if (parent->live & REG_LIVE_DONE) { |
2271 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
c25b2ae1 | 2272 | reg_type_str(env, parent->type), |
9242b5f5 AS |
2273 | parent->var_off.value, parent->off); |
2274 | return -EFAULT; | |
2275 | } | |
5327ed3d JW |
2276 | /* The first condition is more likely to be true than the |
2277 | * second, checked it first. | |
2278 | */ | |
2279 | if ((parent->live & REG_LIVE_READ) == flag || | |
2280 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2281 | /* The parentage chain never changes and |
2282 | * this parent was already marked as LIVE_READ. | |
2283 | * There is no need to keep walking the chain again and | |
2284 | * keep re-marking all parents as LIVE_READ. | |
2285 | * This case happens when the same register is read | |
2286 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2287 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2288 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2289 | */ |
2290 | break; | |
dc503a8a | 2291 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2292 | parent->live |= flag; |
2293 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2294 | if (flag == REG_LIVE_READ64) | |
2295 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2296 | state = parent; |
2297 | parent = state->parent; | |
f4d7e40a | 2298 | writes = true; |
06ee7115 | 2299 | cnt++; |
dc503a8a | 2300 | } |
06ee7115 AS |
2301 | |
2302 | if (env->longest_mark_read_walk < cnt) | |
2303 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2304 | return 0; |
dc503a8a EC |
2305 | } |
2306 | ||
5327ed3d JW |
2307 | /* This function is supposed to be used by the following 32-bit optimization |
2308 | * code only. It returns TRUE if the source or destination register operates | |
2309 | * on 64-bit, otherwise return FALSE. | |
2310 | */ | |
2311 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2312 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2313 | { | |
2314 | u8 code, class, op; | |
2315 | ||
2316 | code = insn->code; | |
2317 | class = BPF_CLASS(code); | |
2318 | op = BPF_OP(code); | |
2319 | if (class == BPF_JMP) { | |
2320 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2321 | * conservatively. | |
2322 | */ | |
2323 | if (op == BPF_EXIT) | |
2324 | return true; | |
2325 | if (op == BPF_CALL) { | |
2326 | /* BPF to BPF call will reach here because of marking | |
2327 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2328 | * don't care the register def because they are anyway | |
2329 | * marked as NOT_INIT already. | |
2330 | */ | |
2331 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2332 | return false; | |
2333 | /* Helper call will reach here because of arg type | |
2334 | * check, conservatively return TRUE. | |
2335 | */ | |
2336 | if (t == SRC_OP) | |
2337 | return true; | |
2338 | ||
2339 | return false; | |
2340 | } | |
2341 | } | |
2342 | ||
2343 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2344 | /* BPF_END always use BPF_ALU class. */ | |
2345 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2346 | return true; | |
2347 | ||
2348 | if (class == BPF_ALU || class == BPF_JMP32) | |
2349 | return false; | |
2350 | ||
2351 | if (class == BPF_LDX) { | |
2352 | if (t != SRC_OP) | |
2353 | return BPF_SIZE(code) == BPF_DW; | |
2354 | /* LDX source must be ptr. */ | |
2355 | return true; | |
2356 | } | |
2357 | ||
2358 | if (class == BPF_STX) { | |
83a28819 IL |
2359 | /* BPF_STX (including atomic variants) has multiple source |
2360 | * operands, one of which is a ptr. Check whether the caller is | |
2361 | * asking about it. | |
2362 | */ | |
2363 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2364 | return true; |
2365 | return BPF_SIZE(code) == BPF_DW; | |
2366 | } | |
2367 | ||
2368 | if (class == BPF_LD) { | |
2369 | u8 mode = BPF_MODE(code); | |
2370 | ||
2371 | /* LD_IMM64 */ | |
2372 | if (mode == BPF_IMM) | |
2373 | return true; | |
2374 | ||
2375 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
2376 | if (t != SRC_OP) | |
2377 | return false; | |
2378 | ||
2379 | /* Implicit ctx ptr. */ | |
2380 | if (regno == BPF_REG_6) | |
2381 | return true; | |
2382 | ||
2383 | /* Explicit source could be any width. */ | |
2384 | return true; | |
2385 | } | |
2386 | ||
2387 | if (class == BPF_ST) | |
2388 | /* The only source register for BPF_ST is a ptr. */ | |
2389 | return true; | |
2390 | ||
2391 | /* Conservatively return true at default. */ | |
2392 | return true; | |
2393 | } | |
2394 | ||
83a28819 IL |
2395 | /* Return the regno defined by the insn, or -1. */ |
2396 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 2397 | { |
83a28819 IL |
2398 | switch (BPF_CLASS(insn->code)) { |
2399 | case BPF_JMP: | |
2400 | case BPF_JMP32: | |
2401 | case BPF_ST: | |
2402 | return -1; | |
2403 | case BPF_STX: | |
2404 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
2405 | (insn->imm & BPF_FETCH)) { | |
2406 | if (insn->imm == BPF_CMPXCHG) | |
2407 | return BPF_REG_0; | |
2408 | else | |
2409 | return insn->src_reg; | |
2410 | } else { | |
2411 | return -1; | |
2412 | } | |
2413 | default: | |
2414 | return insn->dst_reg; | |
2415 | } | |
b325fbca JW |
2416 | } |
2417 | ||
2418 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
2419 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
2420 | { | |
83a28819 IL |
2421 | int dst_reg = insn_def_regno(insn); |
2422 | ||
2423 | if (dst_reg == -1) | |
b325fbca JW |
2424 | return false; |
2425 | ||
83a28819 | 2426 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
2427 | } |
2428 | ||
5327ed3d JW |
2429 | static void mark_insn_zext(struct bpf_verifier_env *env, |
2430 | struct bpf_reg_state *reg) | |
2431 | { | |
2432 | s32 def_idx = reg->subreg_def; | |
2433 | ||
2434 | if (def_idx == DEF_NOT_SUBREG) | |
2435 | return; | |
2436 | ||
2437 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
2438 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
2439 | reg->subreg_def = DEF_NOT_SUBREG; | |
2440 | } | |
2441 | ||
dc503a8a | 2442 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
2443 | enum reg_arg_type t) |
2444 | { | |
f4d7e40a AS |
2445 | struct bpf_verifier_state *vstate = env->cur_state; |
2446 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 2447 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 2448 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 2449 | bool rw64; |
dc503a8a | 2450 | |
17a52670 | 2451 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 2452 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
2453 | return -EINVAL; |
2454 | } | |
2455 | ||
0f55f9ed CL |
2456 | mark_reg_scratched(env, regno); |
2457 | ||
c342dc10 | 2458 | reg = ®s[regno]; |
5327ed3d | 2459 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
2460 | if (t == SRC_OP) { |
2461 | /* check whether register used as source operand can be read */ | |
c342dc10 | 2462 | if (reg->type == NOT_INIT) { |
61bd5218 | 2463 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
2464 | return -EACCES; |
2465 | } | |
679c782d | 2466 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
2467 | if (regno == BPF_REG_FP) |
2468 | return 0; | |
2469 | ||
5327ed3d JW |
2470 | if (rw64) |
2471 | mark_insn_zext(env, reg); | |
2472 | ||
2473 | return mark_reg_read(env, reg, reg->parent, | |
2474 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
2475 | } else { |
2476 | /* check whether register used as dest operand can be written to */ | |
2477 | if (regno == BPF_REG_FP) { | |
61bd5218 | 2478 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
2479 | return -EACCES; |
2480 | } | |
c342dc10 | 2481 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 2482 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 2483 | if (t == DST_OP) |
61bd5218 | 2484 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
2485 | } |
2486 | return 0; | |
2487 | } | |
2488 | ||
b5dc0163 AS |
2489 | /* for any branch, call, exit record the history of jmps in the given state */ |
2490 | static int push_jmp_history(struct bpf_verifier_env *env, | |
2491 | struct bpf_verifier_state *cur) | |
2492 | { | |
2493 | u32 cnt = cur->jmp_history_cnt; | |
2494 | struct bpf_idx_pair *p; | |
2495 | ||
2496 | cnt++; | |
2497 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
2498 | if (!p) | |
2499 | return -ENOMEM; | |
2500 | p[cnt - 1].idx = env->insn_idx; | |
2501 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
2502 | cur->jmp_history = p; | |
2503 | cur->jmp_history_cnt = cnt; | |
2504 | return 0; | |
2505 | } | |
2506 | ||
2507 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
2508 | * history then previous instruction came from straight line execution. | |
2509 | */ | |
2510 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
2511 | u32 *history) | |
2512 | { | |
2513 | u32 cnt = *history; | |
2514 | ||
2515 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
2516 | i = st->jmp_history[cnt - 1].prev_idx; | |
2517 | (*history)--; | |
2518 | } else { | |
2519 | i--; | |
2520 | } | |
2521 | return i; | |
2522 | } | |
2523 | ||
e6ac2450 MKL |
2524 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
2525 | { | |
2526 | const struct btf_type *func; | |
2357672c | 2527 | struct btf *desc_btf; |
e6ac2450 MKL |
2528 | |
2529 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
2530 | return NULL; | |
2531 | ||
43bf0878 | 2532 | desc_btf = find_kfunc_desc_btf(data, insn->off); |
2357672c KKD |
2533 | if (IS_ERR(desc_btf)) |
2534 | return "<error>"; | |
2535 | ||
2536 | func = btf_type_by_id(desc_btf, insn->imm); | |
2537 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
2538 | } |
2539 | ||
b5dc0163 AS |
2540 | /* For given verifier state backtrack_insn() is called from the last insn to |
2541 | * the first insn. Its purpose is to compute a bitmask of registers and | |
2542 | * stack slots that needs precision in the parent verifier state. | |
2543 | */ | |
2544 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
2545 | u32 *reg_mask, u64 *stack_mask) | |
2546 | { | |
2547 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 2548 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
2549 | .cb_print = verbose, |
2550 | .private_data = env, | |
2551 | }; | |
2552 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
2553 | u8 class = BPF_CLASS(insn->code); | |
2554 | u8 opcode = BPF_OP(insn->code); | |
2555 | u8 mode = BPF_MODE(insn->code); | |
2556 | u32 dreg = 1u << insn->dst_reg; | |
2557 | u32 sreg = 1u << insn->src_reg; | |
2558 | u32 spi; | |
2559 | ||
2560 | if (insn->code == 0) | |
2561 | return 0; | |
496f3324 | 2562 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
2563 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
2564 | verbose(env, "%d: ", idx); | |
2565 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
2566 | } | |
2567 | ||
2568 | if (class == BPF_ALU || class == BPF_ALU64) { | |
2569 | if (!(*reg_mask & dreg)) | |
2570 | return 0; | |
2571 | if (opcode == BPF_MOV) { | |
2572 | if (BPF_SRC(insn->code) == BPF_X) { | |
2573 | /* dreg = sreg | |
2574 | * dreg needs precision after this insn | |
2575 | * sreg needs precision before this insn | |
2576 | */ | |
2577 | *reg_mask &= ~dreg; | |
2578 | *reg_mask |= sreg; | |
2579 | } else { | |
2580 | /* dreg = K | |
2581 | * dreg needs precision after this insn. | |
2582 | * Corresponding register is already marked | |
2583 | * as precise=true in this verifier state. | |
2584 | * No further markings in parent are necessary | |
2585 | */ | |
2586 | *reg_mask &= ~dreg; | |
2587 | } | |
2588 | } else { | |
2589 | if (BPF_SRC(insn->code) == BPF_X) { | |
2590 | /* dreg += sreg | |
2591 | * both dreg and sreg need precision | |
2592 | * before this insn | |
2593 | */ | |
2594 | *reg_mask |= sreg; | |
2595 | } /* else dreg += K | |
2596 | * dreg still needs precision before this insn | |
2597 | */ | |
2598 | } | |
2599 | } else if (class == BPF_LDX) { | |
2600 | if (!(*reg_mask & dreg)) | |
2601 | return 0; | |
2602 | *reg_mask &= ~dreg; | |
2603 | ||
2604 | /* scalars can only be spilled into stack w/o losing precision. | |
2605 | * Load from any other memory can be zero extended. | |
2606 | * The desire to keep that precision is already indicated | |
2607 | * by 'precise' mark in corresponding register of this state. | |
2608 | * No further tracking necessary. | |
2609 | */ | |
2610 | if (insn->src_reg != BPF_REG_FP) | |
2611 | return 0; | |
b5dc0163 AS |
2612 | |
2613 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
2614 | * that [fp - off] slot contains scalar that needs to be | |
2615 | * tracked with precision | |
2616 | */ | |
2617 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2618 | if (spi >= 64) { | |
2619 | verbose(env, "BUG spi %d\n", spi); | |
2620 | WARN_ONCE(1, "verifier backtracking bug"); | |
2621 | return -EFAULT; | |
2622 | } | |
2623 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 2624 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 2625 | if (*reg_mask & dreg) |
b3b50f05 | 2626 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
2627 | * to access memory. It means backtracking |
2628 | * encountered a case of pointer subtraction. | |
2629 | */ | |
2630 | return -ENOTSUPP; | |
2631 | /* scalars can only be spilled into stack */ | |
2632 | if (insn->dst_reg != BPF_REG_FP) | |
2633 | return 0; | |
b5dc0163 AS |
2634 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
2635 | if (spi >= 64) { | |
2636 | verbose(env, "BUG spi %d\n", spi); | |
2637 | WARN_ONCE(1, "verifier backtracking bug"); | |
2638 | return -EFAULT; | |
2639 | } | |
2640 | if (!(*stack_mask & (1ull << spi))) | |
2641 | return 0; | |
2642 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
2643 | if (class == BPF_STX) |
2644 | *reg_mask |= sreg; | |
b5dc0163 AS |
2645 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
2646 | if (opcode == BPF_CALL) { | |
2647 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2648 | return -ENOTSUPP; | |
2649 | /* regular helper call sets R0 */ | |
2650 | *reg_mask &= ~1; | |
2651 | if (*reg_mask & 0x3f) { | |
2652 | /* if backtracing was looking for registers R1-R5 | |
2653 | * they should have been found already. | |
2654 | */ | |
2655 | verbose(env, "BUG regs %x\n", *reg_mask); | |
2656 | WARN_ONCE(1, "verifier backtracking bug"); | |
2657 | return -EFAULT; | |
2658 | } | |
2659 | } else if (opcode == BPF_EXIT) { | |
2660 | return -ENOTSUPP; | |
2661 | } | |
2662 | } else if (class == BPF_LD) { | |
2663 | if (!(*reg_mask & dreg)) | |
2664 | return 0; | |
2665 | *reg_mask &= ~dreg; | |
2666 | /* It's ld_imm64 or ld_abs or ld_ind. | |
2667 | * For ld_imm64 no further tracking of precision | |
2668 | * into parent is necessary | |
2669 | */ | |
2670 | if (mode == BPF_IND || mode == BPF_ABS) | |
2671 | /* to be analyzed */ | |
2672 | return -ENOTSUPP; | |
b5dc0163 AS |
2673 | } |
2674 | return 0; | |
2675 | } | |
2676 | ||
2677 | /* the scalar precision tracking algorithm: | |
2678 | * . at the start all registers have precise=false. | |
2679 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
2680 | * . once precise value of the scalar register is used in: | |
2681 | * . ptr + scalar alu | |
2682 | * . if (scalar cond K|scalar) | |
2683 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
2684 | * backtrack through the verifier states and mark all registers and | |
2685 | * stack slots with spilled constants that these scalar regisers | |
2686 | * should be precise. | |
2687 | * . during state pruning two registers (or spilled stack slots) | |
2688 | * are equivalent if both are not precise. | |
2689 | * | |
2690 | * Note the verifier cannot simply walk register parentage chain, | |
2691 | * since many different registers and stack slots could have been | |
2692 | * used to compute single precise scalar. | |
2693 | * | |
2694 | * The approach of starting with precise=true for all registers and then | |
2695 | * backtrack to mark a register as not precise when the verifier detects | |
2696 | * that program doesn't care about specific value (e.g., when helper | |
2697 | * takes register as ARG_ANYTHING parameter) is not safe. | |
2698 | * | |
2699 | * It's ok to walk single parentage chain of the verifier states. | |
2700 | * It's possible that this backtracking will go all the way till 1st insn. | |
2701 | * All other branches will be explored for needing precision later. | |
2702 | * | |
2703 | * The backtracking needs to deal with cases like: | |
2704 | * 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) | |
2705 | * r9 -= r8 | |
2706 | * r5 = r9 | |
2707 | * if r5 > 0x79f goto pc+7 | |
2708 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
2709 | * r5 += 1 | |
2710 | * ... | |
2711 | * call bpf_perf_event_output#25 | |
2712 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
2713 | * | |
2714 | * and this case: | |
2715 | * r6 = 1 | |
2716 | * call foo // uses callee's r6 inside to compute r0 | |
2717 | * r0 += r6 | |
2718 | * if r0 == 0 goto | |
2719 | * | |
2720 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
2721 | * | |
2722 | * Also if parent's curframe > frame where backtracking started, | |
2723 | * the verifier need to mark registers in both frames, otherwise callees | |
2724 | * may incorrectly prune callers. This is similar to | |
2725 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
2726 | * | |
2727 | * For now backtracking falls back into conservative marking. | |
2728 | */ | |
2729 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
2730 | struct bpf_verifier_state *st) | |
2731 | { | |
2732 | struct bpf_func_state *func; | |
2733 | struct bpf_reg_state *reg; | |
2734 | int i, j; | |
2735 | ||
2736 | /* big hammer: mark all scalars precise in this path. | |
2737 | * pop_stack may still get !precise scalars. | |
2738 | */ | |
2739 | for (; st; st = st->parent) | |
2740 | for (i = 0; i <= st->curframe; i++) { | |
2741 | func = st->frame[i]; | |
2742 | for (j = 0; j < BPF_REG_FP; j++) { | |
2743 | reg = &func->regs[j]; | |
2744 | if (reg->type != SCALAR_VALUE) | |
2745 | continue; | |
2746 | reg->precise = true; | |
2747 | } | |
2748 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 2749 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
2750 | continue; |
2751 | reg = &func->stack[j].spilled_ptr; | |
2752 | if (reg->type != SCALAR_VALUE) | |
2753 | continue; | |
2754 | reg->precise = true; | |
2755 | } | |
2756 | } | |
2757 | } | |
2758 | ||
a3ce685d AS |
2759 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
2760 | int spi) | |
b5dc0163 AS |
2761 | { |
2762 | struct bpf_verifier_state *st = env->cur_state; | |
2763 | int first_idx = st->first_insn_idx; | |
2764 | int last_idx = env->insn_idx; | |
2765 | struct bpf_func_state *func; | |
2766 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2767 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2768 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2769 | bool skip_first = true; |
a3ce685d | 2770 | bool new_marks = false; |
b5dc0163 AS |
2771 | int i, err; |
2772 | ||
2c78ee89 | 2773 | if (!env->bpf_capable) |
b5dc0163 AS |
2774 | return 0; |
2775 | ||
2776 | func = st->frame[st->curframe]; | |
a3ce685d AS |
2777 | if (regno >= 0) { |
2778 | reg = &func->regs[regno]; | |
2779 | if (reg->type != SCALAR_VALUE) { | |
2780 | WARN_ONCE(1, "backtracing misuse"); | |
2781 | return -EFAULT; | |
2782 | } | |
2783 | if (!reg->precise) | |
2784 | new_marks = true; | |
2785 | else | |
2786 | reg_mask = 0; | |
2787 | reg->precise = true; | |
b5dc0163 | 2788 | } |
b5dc0163 | 2789 | |
a3ce685d | 2790 | while (spi >= 0) { |
27113c59 | 2791 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
2792 | stack_mask = 0; |
2793 | break; | |
2794 | } | |
2795 | reg = &func->stack[spi].spilled_ptr; | |
2796 | if (reg->type != SCALAR_VALUE) { | |
2797 | stack_mask = 0; | |
2798 | break; | |
2799 | } | |
2800 | if (!reg->precise) | |
2801 | new_marks = true; | |
2802 | else | |
2803 | stack_mask = 0; | |
2804 | reg->precise = true; | |
2805 | break; | |
2806 | } | |
2807 | ||
2808 | if (!new_marks) | |
2809 | return 0; | |
2810 | if (!reg_mask && !stack_mask) | |
2811 | return 0; | |
b5dc0163 AS |
2812 | for (;;) { |
2813 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2814 | u32 history = st->jmp_history_cnt; |
2815 | ||
496f3324 | 2816 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 AS |
2817 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
2818 | for (i = last_idx;;) { | |
2819 | if (skip_first) { | |
2820 | err = 0; | |
2821 | skip_first = false; | |
2822 | } else { | |
2823 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2824 | } | |
2825 | if (err == -ENOTSUPP) { | |
2826 | mark_all_scalars_precise(env, st); | |
2827 | return 0; | |
2828 | } else if (err) { | |
2829 | return err; | |
2830 | } | |
2831 | if (!reg_mask && !stack_mask) | |
2832 | /* Found assignment(s) into tracked register in this state. | |
2833 | * Since this state is already marked, just return. | |
2834 | * Nothing to be tracked further in the parent state. | |
2835 | */ | |
2836 | return 0; | |
2837 | if (i == first_idx) | |
2838 | break; | |
2839 | i = get_prev_insn_idx(st, i, &history); | |
2840 | if (i >= env->prog->len) { | |
2841 | /* This can happen if backtracking reached insn 0 | |
2842 | * and there are still reg_mask or stack_mask | |
2843 | * to backtrack. | |
2844 | * It means the backtracking missed the spot where | |
2845 | * particular register was initialized with a constant. | |
2846 | */ | |
2847 | verbose(env, "BUG backtracking idx %d\n", i); | |
2848 | WARN_ONCE(1, "verifier backtracking bug"); | |
2849 | return -EFAULT; | |
2850 | } | |
2851 | } | |
2852 | st = st->parent; | |
2853 | if (!st) | |
2854 | break; | |
2855 | ||
a3ce685d | 2856 | new_marks = false; |
b5dc0163 AS |
2857 | func = st->frame[st->curframe]; |
2858 | bitmap_from_u64(mask, reg_mask); | |
2859 | for_each_set_bit(i, mask, 32) { | |
2860 | reg = &func->regs[i]; | |
a3ce685d AS |
2861 | if (reg->type != SCALAR_VALUE) { |
2862 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2863 | continue; |
a3ce685d | 2864 | } |
b5dc0163 AS |
2865 | if (!reg->precise) |
2866 | new_marks = true; | |
2867 | reg->precise = true; | |
2868 | } | |
2869 | ||
2870 | bitmap_from_u64(mask, stack_mask); | |
2871 | for_each_set_bit(i, mask, 64) { | |
2872 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2873 | /* the sequence of instructions: |
2874 | * 2: (bf) r3 = r10 | |
2875 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2876 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2877 | * doesn't contain jmps. It's backtracked | |
2878 | * as a single block. | |
2879 | * During backtracking insn 3 is not recognized as | |
2880 | * stack access, so at the end of backtracking | |
2881 | * stack slot fp-8 is still marked in stack_mask. | |
2882 | * However the parent state may not have accessed | |
2883 | * fp-8 and it's "unallocated" stack space. | |
2884 | * In such case fallback to conservative. | |
b5dc0163 | 2885 | */ |
2339cd6c AS |
2886 | mark_all_scalars_precise(env, st); |
2887 | return 0; | |
b5dc0163 AS |
2888 | } |
2889 | ||
27113c59 | 2890 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 2891 | stack_mask &= ~(1ull << i); |
b5dc0163 | 2892 | continue; |
a3ce685d | 2893 | } |
b5dc0163 | 2894 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2895 | if (reg->type != SCALAR_VALUE) { |
2896 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2897 | continue; |
a3ce685d | 2898 | } |
b5dc0163 AS |
2899 | if (!reg->precise) |
2900 | new_marks = true; | |
2901 | reg->precise = true; | |
2902 | } | |
496f3324 | 2903 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 2904 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
2905 | new_marks ? "didn't have" : "already had", |
2906 | reg_mask, stack_mask); | |
2e576648 | 2907 | print_verifier_state(env, func, true); |
b5dc0163 AS |
2908 | } |
2909 | ||
a3ce685d AS |
2910 | if (!reg_mask && !stack_mask) |
2911 | break; | |
b5dc0163 AS |
2912 | if (!new_marks) |
2913 | break; | |
2914 | ||
2915 | last_idx = st->last_insn_idx; | |
2916 | first_idx = st->first_insn_idx; | |
2917 | } | |
2918 | return 0; | |
2919 | } | |
2920 | ||
eb1f7f71 | 2921 | int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
a3ce685d AS |
2922 | { |
2923 | return __mark_chain_precision(env, regno, -1); | |
2924 | } | |
2925 | ||
2926 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2927 | { | |
2928 | return __mark_chain_precision(env, -1, spi); | |
2929 | } | |
b5dc0163 | 2930 | |
1be7f75d AS |
2931 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2932 | { | |
c25b2ae1 | 2933 | switch (base_type(type)) { |
1be7f75d | 2934 | case PTR_TO_MAP_VALUE: |
1be7f75d AS |
2935 | case PTR_TO_STACK: |
2936 | case PTR_TO_CTX: | |
969bf05e | 2937 | case PTR_TO_PACKET: |
de8f3a83 | 2938 | case PTR_TO_PACKET_META: |
969bf05e | 2939 | case PTR_TO_PACKET_END: |
d58e468b | 2940 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2941 | case CONST_PTR_TO_MAP: |
c64b7983 | 2942 | case PTR_TO_SOCKET: |
46f8bc92 | 2943 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 2944 | case PTR_TO_TCP_SOCK: |
fada7fdc | 2945 | case PTR_TO_XDP_SOCK: |
65726b5b | 2946 | case PTR_TO_BTF_ID: |
20b2aff4 | 2947 | case PTR_TO_BUF: |
744ea4e3 | 2948 | case PTR_TO_MEM: |
69c087ba YS |
2949 | case PTR_TO_FUNC: |
2950 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
2951 | return true; |
2952 | default: | |
2953 | return false; | |
2954 | } | |
2955 | } | |
2956 | ||
cc2b14d5 AS |
2957 | /* Does this register contain a constant zero? */ |
2958 | static bool register_is_null(struct bpf_reg_state *reg) | |
2959 | { | |
2960 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2961 | } | |
2962 | ||
f7cf25b2 AS |
2963 | static bool register_is_const(struct bpf_reg_state *reg) |
2964 | { | |
2965 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2966 | } | |
2967 | ||
5689d49b YS |
2968 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
2969 | { | |
2970 | return tnum_is_unknown(reg->var_off) && | |
2971 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
2972 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
2973 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
2974 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
2975 | } | |
2976 | ||
2977 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
2978 | { | |
2979 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
2980 | } | |
2981 | ||
6e7e63cb JH |
2982 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2983 | const struct bpf_reg_state *reg) | |
2984 | { | |
2985 | if (allow_ptr_leaks) | |
2986 | return false; | |
2987 | ||
2988 | return reg->type != SCALAR_VALUE; | |
2989 | } | |
2990 | ||
f7cf25b2 | 2991 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
2992 | int spi, struct bpf_reg_state *reg, |
2993 | int size) | |
f7cf25b2 AS |
2994 | { |
2995 | int i; | |
2996 | ||
2997 | state->stack[spi].spilled_ptr = *reg; | |
354e8f19 MKL |
2998 | if (size == BPF_REG_SIZE) |
2999 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 3000 | |
354e8f19 MKL |
3001 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
3002 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 3003 | |
354e8f19 MKL |
3004 | /* size < 8 bytes spill */ |
3005 | for (; i; i--) | |
3006 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
3007 | } |
3008 | ||
01f810ac | 3009 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
3010 | * stack boundary and alignment are checked in check_mem_access() |
3011 | */ | |
01f810ac AM |
3012 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
3013 | /* stack frame we're writing to */ | |
3014 | struct bpf_func_state *state, | |
3015 | int off, int size, int value_regno, | |
3016 | int insn_idx) | |
17a52670 | 3017 | { |
f4d7e40a | 3018 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 3019 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 3020 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 3021 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 3022 | |
c69431aa | 3023 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
3024 | if (err) |
3025 | return err; | |
9c399760 AS |
3026 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
3027 | * so it's aligned access and [off, off + size) are within stack limits | |
3028 | */ | |
638f5b90 AS |
3029 | if (!env->allow_ptr_leaks && |
3030 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
3031 | size != BPF_REG_SIZE) { | |
3032 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
3033 | return -EACCES; | |
3034 | } | |
17a52670 | 3035 | |
f4d7e40a | 3036 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
3037 | if (value_regno >= 0) |
3038 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
3039 | if (!env->bypass_spec_v4) { |
3040 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
3041 | ||
3042 | for (i = 0; i < size; i++) { | |
3043 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { | |
3044 | sanitize = true; | |
3045 | break; | |
3046 | } | |
3047 | } | |
3048 | ||
3049 | if (sanitize) | |
3050 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
3051 | } | |
17a52670 | 3052 | |
0f55f9ed | 3053 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 3054 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 3055 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
3056 | if (dst_reg != BPF_REG_FP) { |
3057 | /* The backtracking logic can only recognize explicit | |
3058 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 3059 | * scalar via different register has to be conservative. |
b5dc0163 AS |
3060 | * Backtrack from here and mark all registers as precise |
3061 | * that contributed into 'reg' being a constant. | |
3062 | */ | |
3063 | err = mark_chain_precision(env, value_regno); | |
3064 | if (err) | |
3065 | return err; | |
3066 | } | |
354e8f19 | 3067 | save_register_state(state, spi, reg, size); |
f7cf25b2 | 3068 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 3069 | /* register containing pointer is being spilled into stack */ |
9c399760 | 3070 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 3071 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 3072 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
3073 | return -EACCES; |
3074 | } | |
f7cf25b2 | 3075 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
3076 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
3077 | return -EINVAL; | |
3078 | } | |
354e8f19 | 3079 | save_register_state(state, spi, reg, size); |
9c399760 | 3080 | } else { |
cc2b14d5 AS |
3081 | u8 type = STACK_MISC; |
3082 | ||
679c782d EC |
3083 | /* regular write of data into stack destroys any spilled ptr */ |
3084 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d | 3085 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
27113c59 | 3086 | if (is_spilled_reg(&state->stack[spi])) |
0bae2d4d | 3087 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 3088 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 3089 | |
cc2b14d5 AS |
3090 | /* only mark the slot as written if all 8 bytes were written |
3091 | * otherwise read propagation may incorrectly stop too soon | |
3092 | * when stack slots are partially written. | |
3093 | * This heuristic means that read propagation will be | |
3094 | * conservative, since it will add reg_live_read marks | |
3095 | * to stack slots all the way to first state when programs | |
3096 | * writes+reads less than 8 bytes | |
3097 | */ | |
3098 | if (size == BPF_REG_SIZE) | |
3099 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
3100 | ||
3101 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
3102 | if (reg && register_is_null(reg)) { |
3103 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3104 | err = mark_chain_precision(env, value_regno); | |
3105 | if (err) | |
3106 | return err; | |
cc2b14d5 | 3107 | type = STACK_ZERO; |
b5dc0163 | 3108 | } |
cc2b14d5 | 3109 | |
0bae2d4d | 3110 | /* Mark slots affected by this stack write. */ |
9c399760 | 3111 | for (i = 0; i < size; i++) |
638f5b90 | 3112 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 3113 | type; |
17a52670 AS |
3114 | } |
3115 | return 0; | |
3116 | } | |
3117 | ||
01f810ac AM |
3118 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
3119 | * known to contain a variable offset. | |
3120 | * This function checks whether the write is permitted and conservatively | |
3121 | * tracks the effects of the write, considering that each stack slot in the | |
3122 | * dynamic range is potentially written to. | |
3123 | * | |
3124 | * 'off' includes 'regno->off'. | |
3125 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
3126 | * the stack. | |
3127 | * | |
3128 | * Spilled pointers in range are not marked as written because we don't know | |
3129 | * what's going to be actually written. This means that read propagation for | |
3130 | * future reads cannot be terminated by this write. | |
3131 | * | |
3132 | * For privileged programs, uninitialized stack slots are considered | |
3133 | * initialized by this write (even though we don't know exactly what offsets | |
3134 | * are going to be written to). The idea is that we don't want the verifier to | |
3135 | * reject future reads that access slots written to through variable offsets. | |
3136 | */ | |
3137 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
3138 | /* func where register points to */ | |
3139 | struct bpf_func_state *state, | |
3140 | int ptr_regno, int off, int size, | |
3141 | int value_regno, int insn_idx) | |
3142 | { | |
3143 | struct bpf_func_state *cur; /* state of the current function */ | |
3144 | int min_off, max_off; | |
3145 | int i, err; | |
3146 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
3147 | bool writing_zero = false; | |
3148 | /* set if the fact that we're writing a zero is used to let any | |
3149 | * stack slots remain STACK_ZERO | |
3150 | */ | |
3151 | bool zero_used = false; | |
3152 | ||
3153 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
3154 | ptr_reg = &cur->regs[ptr_regno]; | |
3155 | min_off = ptr_reg->smin_value + off; | |
3156 | max_off = ptr_reg->smax_value + off + size; | |
3157 | if (value_regno >= 0) | |
3158 | value_reg = &cur->regs[value_regno]; | |
3159 | if (value_reg && register_is_null(value_reg)) | |
3160 | writing_zero = true; | |
3161 | ||
c69431aa | 3162 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
3163 | if (err) |
3164 | return err; | |
3165 | ||
3166 | ||
3167 | /* Variable offset writes destroy any spilled pointers in range. */ | |
3168 | for (i = min_off; i < max_off; i++) { | |
3169 | u8 new_type, *stype; | |
3170 | int slot, spi; | |
3171 | ||
3172 | slot = -i - 1; | |
3173 | spi = slot / BPF_REG_SIZE; | |
3174 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 3175 | mark_stack_slot_scratched(env, spi); |
01f810ac | 3176 | |
f5e477a8 KKD |
3177 | if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) { |
3178 | /* Reject the write if range we may write to has not | |
3179 | * been initialized beforehand. If we didn't reject | |
3180 | * here, the ptr status would be erased below (even | |
3181 | * though not all slots are actually overwritten), | |
3182 | * possibly opening the door to leaks. | |
3183 | * | |
3184 | * We do however catch STACK_INVALID case below, and | |
3185 | * only allow reading possibly uninitialized memory | |
3186 | * later for CAP_PERFMON, as the write may not happen to | |
3187 | * that slot. | |
01f810ac AM |
3188 | */ |
3189 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
3190 | insn_idx, i); | |
3191 | return -EINVAL; | |
3192 | } | |
3193 | ||
3194 | /* Erase all spilled pointers. */ | |
3195 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
3196 | ||
3197 | /* Update the slot type. */ | |
3198 | new_type = STACK_MISC; | |
3199 | if (writing_zero && *stype == STACK_ZERO) { | |
3200 | new_type = STACK_ZERO; | |
3201 | zero_used = true; | |
3202 | } | |
3203 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
3204 | * pretend that it will be initialized by this write. The slot | |
3205 | * might not actually be written to, and so if we mark it as | |
3206 | * initialized future reads might leak uninitialized memory. | |
3207 | * For privileged programs, we will accept such reads to slots | |
3208 | * that may or may not be written because, if we're reject | |
3209 | * them, the error would be too confusing. | |
3210 | */ | |
3211 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
3212 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
3213 | insn_idx, i); | |
3214 | return -EINVAL; | |
3215 | } | |
3216 | *stype = new_type; | |
3217 | } | |
3218 | if (zero_used) { | |
3219 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3220 | err = mark_chain_precision(env, value_regno); | |
3221 | if (err) | |
3222 | return err; | |
3223 | } | |
3224 | return 0; | |
3225 | } | |
3226 | ||
3227 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
3228 | * max_off), we set the register's type according to the types of the | |
3229 | * respective stack slots. If all the stack values are known to be zeros, then | |
3230 | * so is the destination reg. Otherwise, the register is considered to be | |
3231 | * SCALAR. This function does not deal with register filling; the caller must | |
3232 | * ensure that all spilled registers in the stack range have been marked as | |
3233 | * read. | |
3234 | */ | |
3235 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
3236 | /* func where src register points to */ | |
3237 | struct bpf_func_state *ptr_state, | |
3238 | int min_off, int max_off, int dst_regno) | |
3239 | { | |
3240 | struct bpf_verifier_state *vstate = env->cur_state; | |
3241 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3242 | int i, slot, spi; | |
3243 | u8 *stype; | |
3244 | int zeros = 0; | |
3245 | ||
3246 | for (i = min_off; i < max_off; i++) { | |
3247 | slot = -i - 1; | |
3248 | spi = slot / BPF_REG_SIZE; | |
3249 | stype = ptr_state->stack[spi].slot_type; | |
3250 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
3251 | break; | |
3252 | zeros++; | |
3253 | } | |
3254 | if (zeros == max_off - min_off) { | |
3255 | /* any access_size read into register is zero extended, | |
3256 | * so the whole register == const_zero | |
3257 | */ | |
3258 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
3259 | /* backtracking doesn't support STACK_ZERO yet, | |
3260 | * so mark it precise here, so that later | |
3261 | * backtracking can stop here. | |
3262 | * Backtracking may not need this if this register | |
3263 | * doesn't participate in pointer adjustment. | |
3264 | * Forward propagation of precise flag is not | |
3265 | * necessary either. This mark is only to stop | |
3266 | * backtracking. Any register that contributed | |
3267 | * to const 0 was marked precise before spill. | |
3268 | */ | |
3269 | state->regs[dst_regno].precise = true; | |
3270 | } else { | |
3271 | /* have read misc data from the stack */ | |
3272 | mark_reg_unknown(env, state->regs, dst_regno); | |
3273 | } | |
3274 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
3275 | } | |
3276 | ||
3277 | /* Read the stack at 'off' and put the results into the register indicated by | |
3278 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
3279 | * spilled reg. | |
3280 | * | |
3281 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
3282 | * register. | |
3283 | * | |
3284 | * The access is assumed to be within the current stack bounds. | |
3285 | */ | |
3286 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
3287 | /* func where src register points to */ | |
3288 | struct bpf_func_state *reg_state, | |
3289 | int off, int size, int dst_regno) | |
17a52670 | 3290 | { |
f4d7e40a AS |
3291 | struct bpf_verifier_state *vstate = env->cur_state; |
3292 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 3293 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 3294 | struct bpf_reg_state *reg; |
354e8f19 | 3295 | u8 *stype, type; |
17a52670 | 3296 | |
f4d7e40a | 3297 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 3298 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 3299 | |
27113c59 | 3300 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
3301 | u8 spill_size = 1; |
3302 | ||
3303 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
3304 | spill_size++; | |
354e8f19 | 3305 | |
f30d4968 | 3306 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
3307 | if (reg->type != SCALAR_VALUE) { |
3308 | verbose_linfo(env, env->insn_idx, "; "); | |
3309 | verbose(env, "invalid size of register fill\n"); | |
3310 | return -EACCES; | |
3311 | } | |
354e8f19 MKL |
3312 | |
3313 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
3314 | if (dst_regno < 0) | |
3315 | return 0; | |
3316 | ||
f30d4968 | 3317 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
3318 | /* The earlier check_reg_arg() has decided the |
3319 | * subreg_def for this insn. Save it first. | |
3320 | */ | |
3321 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
3322 | ||
3323 | state->regs[dst_regno] = *reg; | |
3324 | state->regs[dst_regno].subreg_def = subreg_def; | |
3325 | } else { | |
3326 | for (i = 0; i < size; i++) { | |
3327 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
3328 | if (type == STACK_SPILL) | |
3329 | continue; | |
3330 | if (type == STACK_MISC) | |
3331 | continue; | |
3332 | verbose(env, "invalid read from stack off %d+%d size %d\n", | |
3333 | off, i, size); | |
3334 | return -EACCES; | |
3335 | } | |
01f810ac | 3336 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 3337 | } |
354e8f19 | 3338 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 3339 | return 0; |
17a52670 | 3340 | } |
17a52670 | 3341 | |
01f810ac | 3342 | if (dst_regno >= 0) { |
17a52670 | 3343 | /* restore register state from stack */ |
01f810ac | 3344 | state->regs[dst_regno] = *reg; |
2f18f62e AS |
3345 | /* mark reg as written since spilled pointer state likely |
3346 | * has its liveness marks cleared by is_state_visited() | |
3347 | * which resets stack/reg liveness for state transitions | |
3348 | */ | |
01f810ac | 3349 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 3350 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 3351 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
3352 | * it is acceptable to use this value as a SCALAR_VALUE |
3353 | * (e.g. for XADD). | |
3354 | * We must not allow unprivileged callers to do that | |
3355 | * with spilled pointers. | |
3356 | */ | |
3357 | verbose(env, "leaking pointer from stack off %d\n", | |
3358 | off); | |
3359 | return -EACCES; | |
dc503a8a | 3360 | } |
f7cf25b2 | 3361 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
3362 | } else { |
3363 | for (i = 0; i < size; i++) { | |
01f810ac AM |
3364 | type = stype[(slot - i) % BPF_REG_SIZE]; |
3365 | if (type == STACK_MISC) | |
cc2b14d5 | 3366 | continue; |
01f810ac | 3367 | if (type == STACK_ZERO) |
cc2b14d5 | 3368 | continue; |
cc2b14d5 AS |
3369 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
3370 | off, i, size); | |
3371 | return -EACCES; | |
3372 | } | |
f7cf25b2 | 3373 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
3374 | if (dst_regno >= 0) |
3375 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 3376 | } |
f7cf25b2 | 3377 | return 0; |
17a52670 AS |
3378 | } |
3379 | ||
61df10c7 | 3380 | enum bpf_access_src { |
01f810ac AM |
3381 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
3382 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
3383 | }; | |
3384 | ||
3385 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
3386 | int regno, int off, int access_size, | |
3387 | bool zero_size_allowed, | |
61df10c7 | 3388 | enum bpf_access_src type, |
01f810ac AM |
3389 | struct bpf_call_arg_meta *meta); |
3390 | ||
3391 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
3392 | { | |
3393 | return cur_regs(env) + regno; | |
3394 | } | |
3395 | ||
3396 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
3397 | * 'dst_regno'. | |
3398 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
3399 | * but not its variable offset. | |
3400 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
3401 | * | |
3402 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
3403 | * filling registers (i.e. reads of spilled register cannot be detected when | |
3404 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
3405 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
3406 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
3407 | * instead. | |
3408 | */ | |
3409 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
3410 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 3411 | { |
01f810ac AM |
3412 | /* The state of the source register. */ |
3413 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3414 | struct bpf_func_state *ptr_state = func(env, reg); | |
3415 | int err; | |
3416 | int min_off, max_off; | |
3417 | ||
3418 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 3419 | */ |
01f810ac AM |
3420 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
3421 | false, ACCESS_DIRECT, NULL); | |
3422 | if (err) | |
3423 | return err; | |
3424 | ||
3425 | min_off = reg->smin_value + off; | |
3426 | max_off = reg->smax_value + off; | |
3427 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
3428 | return 0; | |
3429 | } | |
3430 | ||
3431 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
3432 | * check_stack_read_var_off. | |
3433 | * | |
3434 | * The caller must ensure that the offset falls within the allocated stack | |
3435 | * bounds. | |
3436 | * | |
3437 | * 'dst_regno' is a register which will receive the value from the stack. It | |
3438 | * can be -1, meaning that the read value is not going to a register. | |
3439 | */ | |
3440 | static int check_stack_read(struct bpf_verifier_env *env, | |
3441 | int ptr_regno, int off, int size, | |
3442 | int dst_regno) | |
3443 | { | |
3444 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3445 | struct bpf_func_state *state = func(env, reg); | |
3446 | int err; | |
3447 | /* Some accesses are only permitted with a static offset. */ | |
3448 | bool var_off = !tnum_is_const(reg->var_off); | |
3449 | ||
3450 | /* The offset is required to be static when reads don't go to a | |
3451 | * register, in order to not leak pointers (see | |
3452 | * check_stack_read_fixed_off). | |
3453 | */ | |
3454 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
3455 | char tn_buf[48]; |
3456 | ||
3457 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 3458 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
3459 | tn_buf, off, size); |
3460 | return -EACCES; | |
3461 | } | |
01f810ac AM |
3462 | /* Variable offset is prohibited for unprivileged mode for simplicity |
3463 | * since it requires corresponding support in Spectre masking for stack | |
3464 | * ALU. See also retrieve_ptr_limit(). | |
3465 | */ | |
3466 | if (!env->bypass_spec_v1 && var_off) { | |
3467 | char tn_buf[48]; | |
e4298d25 | 3468 | |
01f810ac AM |
3469 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3470 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", | |
3471 | ptr_regno, tn_buf); | |
e4298d25 DB |
3472 | return -EACCES; |
3473 | } | |
3474 | ||
01f810ac AM |
3475 | if (!var_off) { |
3476 | off += reg->var_off.value; | |
3477 | err = check_stack_read_fixed_off(env, state, off, size, | |
3478 | dst_regno); | |
3479 | } else { | |
3480 | /* Variable offset stack reads need more conservative handling | |
3481 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
3482 | * branch. | |
3483 | */ | |
3484 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
3485 | dst_regno); | |
3486 | } | |
3487 | return err; | |
3488 | } | |
3489 | ||
3490 | ||
3491 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
3492 | * check_stack_write_var_off. | |
3493 | * | |
3494 | * 'ptr_regno' is the register used as a pointer into the stack. | |
3495 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
3496 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
3497 | * be -1, meaning that we're not writing from a register. | |
3498 | * | |
3499 | * The caller must ensure that the offset falls within the maximum stack size. | |
3500 | */ | |
3501 | static int check_stack_write(struct bpf_verifier_env *env, | |
3502 | int ptr_regno, int off, int size, | |
3503 | int value_regno, int insn_idx) | |
3504 | { | |
3505 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3506 | struct bpf_func_state *state = func(env, reg); | |
3507 | int err; | |
3508 | ||
3509 | if (tnum_is_const(reg->var_off)) { | |
3510 | off += reg->var_off.value; | |
3511 | err = check_stack_write_fixed_off(env, state, off, size, | |
3512 | value_regno, insn_idx); | |
3513 | } else { | |
3514 | /* Variable offset stack reads need more conservative handling | |
3515 | * than fixed offset ones. | |
3516 | */ | |
3517 | err = check_stack_write_var_off(env, state, | |
3518 | ptr_regno, off, size, | |
3519 | value_regno, insn_idx); | |
3520 | } | |
3521 | return err; | |
e4298d25 DB |
3522 | } |
3523 | ||
591fe988 DB |
3524 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
3525 | int off, int size, enum bpf_access_type type) | |
3526 | { | |
3527 | struct bpf_reg_state *regs = cur_regs(env); | |
3528 | struct bpf_map *map = regs[regno].map_ptr; | |
3529 | u32 cap = bpf_map_flags_to_cap(map); | |
3530 | ||
3531 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
3532 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
3533 | map->value_size, off, size); | |
3534 | return -EACCES; | |
3535 | } | |
3536 | ||
3537 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
3538 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
3539 | map->value_size, off, size); | |
3540 | return -EACCES; | |
3541 | } | |
3542 | ||
3543 | return 0; | |
3544 | } | |
3545 | ||
457f4436 AN |
3546 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
3547 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
3548 | int off, int size, u32 mem_size, | |
3549 | bool zero_size_allowed) | |
17a52670 | 3550 | { |
457f4436 AN |
3551 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
3552 | struct bpf_reg_state *reg; | |
3553 | ||
3554 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
3555 | return 0; | |
17a52670 | 3556 | |
457f4436 AN |
3557 | reg = &cur_regs(env)[regno]; |
3558 | switch (reg->type) { | |
69c087ba YS |
3559 | case PTR_TO_MAP_KEY: |
3560 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
3561 | mem_size, off, size); | |
3562 | break; | |
457f4436 | 3563 | case PTR_TO_MAP_VALUE: |
61bd5218 | 3564 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
3565 | mem_size, off, size); |
3566 | break; | |
3567 | case PTR_TO_PACKET: | |
3568 | case PTR_TO_PACKET_META: | |
3569 | case PTR_TO_PACKET_END: | |
3570 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
3571 | off, size, regno, reg->id, off, mem_size); | |
3572 | break; | |
3573 | case PTR_TO_MEM: | |
3574 | default: | |
3575 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
3576 | mem_size, off, size); | |
17a52670 | 3577 | } |
457f4436 AN |
3578 | |
3579 | return -EACCES; | |
17a52670 AS |
3580 | } |
3581 | ||
457f4436 AN |
3582 | /* check read/write into a memory region with possible variable offset */ |
3583 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
3584 | int off, int size, u32 mem_size, | |
3585 | bool zero_size_allowed) | |
dbcfe5f7 | 3586 | { |
f4d7e40a AS |
3587 | struct bpf_verifier_state *vstate = env->cur_state; |
3588 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
3589 | struct bpf_reg_state *reg = &state->regs[regno]; |
3590 | int err; | |
3591 | ||
457f4436 | 3592 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
3593 | * need to try adding each of min_value and max_value to off |
3594 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
3595 | * |
3596 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
3597 | * comparisons where we can't assume the floor of a |
3598 | * value is 0. If we are using signed variables for our | |
3599 | * index'es we need to make sure that whatever we use | |
3600 | * will have a set floor within our range. | |
3601 | */ | |
b7137c4e DB |
3602 | if (reg->smin_value < 0 && |
3603 | (reg->smin_value == S64_MIN || | |
3604 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
3605 | reg->smin_value + off < 0)) { | |
61bd5218 | 3606 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
3607 | regno); |
3608 | return -EACCES; | |
3609 | } | |
457f4436 AN |
3610 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
3611 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 3612 | if (err) { |
457f4436 | 3613 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 3614 | regno); |
dbcfe5f7 GB |
3615 | return err; |
3616 | } | |
3617 | ||
b03c9f9f EC |
3618 | /* If we haven't set a max value then we need to bail since we can't be |
3619 | * sure we won't do bad things. | |
3620 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 3621 | */ |
b03c9f9f | 3622 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 3623 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
3624 | regno); |
3625 | return -EACCES; | |
3626 | } | |
457f4436 AN |
3627 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
3628 | mem_size, zero_size_allowed); | |
3629 | if (err) { | |
3630 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 3631 | regno); |
457f4436 AN |
3632 | return err; |
3633 | } | |
3634 | ||
3635 | return 0; | |
3636 | } | |
d83525ca | 3637 | |
e9147b44 KKD |
3638 | static int __check_ptr_off_reg(struct bpf_verifier_env *env, |
3639 | const struct bpf_reg_state *reg, int regno, | |
3640 | bool fixed_off_ok) | |
3641 | { | |
3642 | /* Access to this pointer-typed register or passing it to a helper | |
3643 | * is only allowed in its original, unmodified form. | |
3644 | */ | |
3645 | ||
3646 | if (reg->off < 0) { | |
3647 | verbose(env, "negative offset %s ptr R%d off=%d disallowed\n", | |
3648 | reg_type_str(env, reg->type), regno, reg->off); | |
3649 | return -EACCES; | |
3650 | } | |
3651 | ||
3652 | if (!fixed_off_ok && reg->off) { | |
3653 | verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n", | |
3654 | reg_type_str(env, reg->type), regno, reg->off); | |
3655 | return -EACCES; | |
3656 | } | |
3657 | ||
3658 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3659 | char tn_buf[48]; | |
3660 | ||
3661 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3662 | verbose(env, "variable %s access var_off=%s disallowed\n", | |
3663 | reg_type_str(env, reg->type), tn_buf); | |
3664 | return -EACCES; | |
3665 | } | |
3666 | ||
3667 | return 0; | |
3668 | } | |
3669 | ||
3670 | int check_ptr_off_reg(struct bpf_verifier_env *env, | |
3671 | const struct bpf_reg_state *reg, int regno) | |
3672 | { | |
3673 | return __check_ptr_off_reg(env, reg, regno, false); | |
3674 | } | |
3675 | ||
61df10c7 | 3676 | static int map_kptr_match_type(struct bpf_verifier_env *env, |
aa3496ac | 3677 | struct btf_field *kptr_field, |
61df10c7 KKD |
3678 | struct bpf_reg_state *reg, u32 regno) |
3679 | { | |
aa3496ac | 3680 | const char *targ_name = kernel_type_name(kptr_field->kptr.btf, kptr_field->kptr.btf_id); |
6efe152d | 3681 | int perm_flags = PTR_MAYBE_NULL; |
61df10c7 KKD |
3682 | const char *reg_name = ""; |
3683 | ||
6efe152d | 3684 | /* Only unreferenced case accepts untrusted pointers */ |
aa3496ac | 3685 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
3686 | perm_flags |= PTR_UNTRUSTED; |
3687 | ||
3688 | if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags)) | |
61df10c7 KKD |
3689 | goto bad_type; |
3690 | ||
3691 | if (!btf_is_kernel(reg->btf)) { | |
3692 | verbose(env, "R%d must point to kernel BTF\n", regno); | |
3693 | return -EINVAL; | |
3694 | } | |
3695 | /* We need to verify reg->type and reg->btf, before accessing reg->btf */ | |
3696 | reg_name = kernel_type_name(reg->btf, reg->btf_id); | |
3697 | ||
c0a5a21c KKD |
3698 | /* For ref_ptr case, release function check should ensure we get one |
3699 | * referenced PTR_TO_BTF_ID, and that its fixed offset is 0. For the | |
3700 | * normal store of unreferenced kptr, we must ensure var_off is zero. | |
3701 | * Since ref_ptr cannot be accessed directly by BPF insns, checks for | |
3702 | * reg->off and reg->ref_obj_id are not needed here. | |
3703 | */ | |
61df10c7 KKD |
3704 | if (__check_ptr_off_reg(env, reg, regno, true)) |
3705 | return -EACCES; | |
3706 | ||
3707 | /* A full type match is needed, as BTF can be vmlinux or module BTF, and | |
3708 | * we also need to take into account the reg->off. | |
3709 | * | |
3710 | * We want to support cases like: | |
3711 | * | |
3712 | * struct foo { | |
3713 | * struct bar br; | |
3714 | * struct baz bz; | |
3715 | * }; | |
3716 | * | |
3717 | * struct foo *v; | |
3718 | * v = func(); // PTR_TO_BTF_ID | |
3719 | * val->foo = v; // reg->off is zero, btf and btf_id match type | |
3720 | * val->bar = &v->br; // reg->off is still zero, but we need to retry with | |
3721 | * // first member type of struct after comparison fails | |
3722 | * val->baz = &v->bz; // reg->off is non-zero, so struct needs to be walked | |
3723 | * // to match type | |
3724 | * | |
3725 | * In the kptr_ref case, check_func_arg_reg_off already ensures reg->off | |
2ab3b380 KKD |
3726 | * is zero. We must also ensure that btf_struct_ids_match does not walk |
3727 | * the struct to match type against first member of struct, i.e. reject | |
3728 | * second case from above. Hence, when type is BPF_KPTR_REF, we set | |
3729 | * strict mode to true for type match. | |
61df10c7 KKD |
3730 | */ |
3731 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
aa3496ac KKD |
3732 | kptr_field->kptr.btf, kptr_field->kptr.btf_id, |
3733 | kptr_field->type == BPF_KPTR_REF)) | |
61df10c7 KKD |
3734 | goto bad_type; |
3735 | return 0; | |
3736 | bad_type: | |
3737 | verbose(env, "invalid kptr access, R%d type=%s%s ", regno, | |
3738 | reg_type_str(env, reg->type), reg_name); | |
6efe152d | 3739 | verbose(env, "expected=%s%s", reg_type_str(env, PTR_TO_BTF_ID), targ_name); |
aa3496ac | 3740 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
3741 | verbose(env, " or %s%s\n", reg_type_str(env, PTR_TO_BTF_ID | PTR_UNTRUSTED), |
3742 | targ_name); | |
3743 | else | |
3744 | verbose(env, "\n"); | |
61df10c7 KKD |
3745 | return -EINVAL; |
3746 | } | |
3747 | ||
3748 | static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, | |
3749 | int value_regno, int insn_idx, | |
aa3496ac | 3750 | struct btf_field *kptr_field) |
61df10c7 KKD |
3751 | { |
3752 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; | |
3753 | int class = BPF_CLASS(insn->code); | |
3754 | struct bpf_reg_state *val_reg; | |
3755 | ||
3756 | /* Things we already checked for in check_map_access and caller: | |
3757 | * - Reject cases where variable offset may touch kptr | |
3758 | * - size of access (must be BPF_DW) | |
3759 | * - tnum_is_const(reg->var_off) | |
aa3496ac | 3760 | * - kptr_field->offset == off + reg->var_off.value |
61df10c7 KKD |
3761 | */ |
3762 | /* Only BPF_[LDX,STX,ST] | BPF_MEM | BPF_DW is supported */ | |
3763 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
3764 | verbose(env, "kptr in map can only be accessed using BPF_MEM instruction mode\n"); | |
3765 | return -EACCES; | |
3766 | } | |
3767 | ||
6efe152d KKD |
3768 | /* We only allow loading referenced kptr, since it will be marked as |
3769 | * untrusted, similar to unreferenced kptr. | |
3770 | */ | |
aa3496ac | 3771 | if (class != BPF_LDX && kptr_field->type == BPF_KPTR_REF) { |
6efe152d | 3772 | verbose(env, "store to referenced kptr disallowed\n"); |
c0a5a21c KKD |
3773 | return -EACCES; |
3774 | } | |
3775 | ||
61df10c7 KKD |
3776 | if (class == BPF_LDX) { |
3777 | val_reg = reg_state(env, value_regno); | |
3778 | /* We can simply mark the value_regno receiving the pointer | |
3779 | * value from map as PTR_TO_BTF_ID, with the correct type. | |
3780 | */ | |
aa3496ac KKD |
3781 | mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, kptr_field->kptr.btf, |
3782 | kptr_field->kptr.btf_id, PTR_MAYBE_NULL | PTR_UNTRUSTED); | |
61df10c7 KKD |
3783 | /* For mark_ptr_or_null_reg */ |
3784 | val_reg->id = ++env->id_gen; | |
3785 | } else if (class == BPF_STX) { | |
3786 | val_reg = reg_state(env, value_regno); | |
3787 | if (!register_is_null(val_reg) && | |
aa3496ac | 3788 | map_kptr_match_type(env, kptr_field, val_reg, value_regno)) |
61df10c7 KKD |
3789 | return -EACCES; |
3790 | } else if (class == BPF_ST) { | |
3791 | if (insn->imm) { | |
3792 | verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%u\n", | |
aa3496ac | 3793 | kptr_field->offset); |
61df10c7 KKD |
3794 | return -EACCES; |
3795 | } | |
3796 | } else { | |
3797 | verbose(env, "kptr in map can only be accessed using BPF_LDX/BPF_STX/BPF_ST\n"); | |
3798 | return -EACCES; | |
3799 | } | |
3800 | return 0; | |
3801 | } | |
3802 | ||
457f4436 AN |
3803 | /* check read/write into a map element with possible variable offset */ |
3804 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
61df10c7 KKD |
3805 | int off, int size, bool zero_size_allowed, |
3806 | enum bpf_access_src src) | |
457f4436 AN |
3807 | { |
3808 | struct bpf_verifier_state *vstate = env->cur_state; | |
3809 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3810 | struct bpf_reg_state *reg = &state->regs[regno]; | |
3811 | struct bpf_map *map = reg->map_ptr; | |
aa3496ac KKD |
3812 | struct btf_record *rec; |
3813 | int err, i; | |
457f4436 AN |
3814 | |
3815 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
3816 | zero_size_allowed); | |
3817 | if (err) | |
3818 | return err; | |
3819 | ||
aa3496ac KKD |
3820 | if (IS_ERR_OR_NULL(map->record)) |
3821 | return 0; | |
3822 | rec = map->record; | |
3823 | for (i = 0; i < rec->cnt; i++) { | |
3824 | struct btf_field *field = &rec->fields[i]; | |
3825 | u32 p = field->offset; | |
3826 | ||
db559117 KKD |
3827 | /* If any part of a field can be touched by load/store, reject |
3828 | * this program. To check that [x1, x2) overlaps with [y1, y2), | |
3829 | * it is sufficient to check x1 < y2 && y1 < x2. | |
3830 | */ | |
aa3496ac KKD |
3831 | if (reg->smin_value + off < p + btf_field_type_size(field->type) && |
3832 | p < reg->umax_value + off + size) { | |
3833 | switch (field->type) { | |
3834 | case BPF_KPTR_UNREF: | |
3835 | case BPF_KPTR_REF: | |
61df10c7 KKD |
3836 | if (src != ACCESS_DIRECT) { |
3837 | verbose(env, "kptr cannot be accessed indirectly by helper\n"); | |
3838 | return -EACCES; | |
3839 | } | |
3840 | if (!tnum_is_const(reg->var_off)) { | |
3841 | verbose(env, "kptr access cannot have variable offset\n"); | |
3842 | return -EACCES; | |
3843 | } | |
3844 | if (p != off + reg->var_off.value) { | |
3845 | verbose(env, "kptr access misaligned expected=%u off=%llu\n", | |
3846 | p, off + reg->var_off.value); | |
3847 | return -EACCES; | |
3848 | } | |
3849 | if (size != bpf_size_to_bytes(BPF_DW)) { | |
3850 | verbose(env, "kptr access size must be BPF_DW\n"); | |
3851 | return -EACCES; | |
3852 | } | |
3853 | break; | |
aa3496ac | 3854 | default: |
db559117 KKD |
3855 | verbose(env, "%s cannot be accessed directly by load/store\n", |
3856 | btf_field_type_name(field->type)); | |
aa3496ac | 3857 | return -EACCES; |
61df10c7 KKD |
3858 | } |
3859 | } | |
3860 | } | |
aa3496ac | 3861 | return 0; |
dbcfe5f7 GB |
3862 | } |
3863 | ||
969bf05e AS |
3864 | #define MAX_PACKET_OFF 0xffff |
3865 | ||
58e2af8b | 3866 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
3867 | const struct bpf_call_arg_meta *meta, |
3868 | enum bpf_access_type t) | |
4acf6c0b | 3869 | { |
7e40781c UP |
3870 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
3871 | ||
3872 | switch (prog_type) { | |
5d66fa7d | 3873 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
3874 | case BPF_PROG_TYPE_LWT_IN: |
3875 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 3876 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 3877 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 3878 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 3879 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
3880 | if (t == BPF_WRITE) |
3881 | return false; | |
8731745e | 3882 | fallthrough; |
5d66fa7d DB |
3883 | |
3884 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
3885 | case BPF_PROG_TYPE_SCHED_CLS: |
3886 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 3887 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 3888 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 3889 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 3890 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
3891 | if (meta) |
3892 | return meta->pkt_access; | |
3893 | ||
3894 | env->seen_direct_write = true; | |
4acf6c0b | 3895 | return true; |
0d01da6a SF |
3896 | |
3897 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
3898 | if (t == BPF_WRITE) | |
3899 | env->seen_direct_write = true; | |
3900 | ||
3901 | return true; | |
3902 | ||
4acf6c0b BB |
3903 | default: |
3904 | return false; | |
3905 | } | |
3906 | } | |
3907 | ||
f1174f77 | 3908 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 3909 | int size, bool zero_size_allowed) |
f1174f77 | 3910 | { |
638f5b90 | 3911 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
3912 | struct bpf_reg_state *reg = ®s[regno]; |
3913 | int err; | |
3914 | ||
3915 | /* We may have added a variable offset to the packet pointer; but any | |
3916 | * reg->range we have comes after that. We are only checking the fixed | |
3917 | * offset. | |
3918 | */ | |
3919 | ||
3920 | /* We don't allow negative numbers, because we aren't tracking enough | |
3921 | * detail to prove they're safe. | |
3922 | */ | |
b03c9f9f | 3923 | if (reg->smin_value < 0) { |
61bd5218 | 3924 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
3925 | regno); |
3926 | return -EACCES; | |
3927 | } | |
6d94e741 AS |
3928 | |
3929 | err = reg->range < 0 ? -EINVAL : | |
3930 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 3931 | zero_size_allowed); |
f1174f77 | 3932 | if (err) { |
61bd5218 | 3933 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
3934 | return err; |
3935 | } | |
e647815a | 3936 | |
457f4436 | 3937 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
3938 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
3939 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 3940 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
3941 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
3942 | */ | |
3943 | env->prog->aux->max_pkt_offset = | |
3944 | max_t(u32, env->prog->aux->max_pkt_offset, | |
3945 | off + reg->umax_value + size - 1); | |
3946 | ||
f1174f77 EC |
3947 | return err; |
3948 | } | |
3949 | ||
3950 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 3951 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 3952 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 3953 | struct btf **btf, u32 *btf_id) |
17a52670 | 3954 | { |
f96da094 DB |
3955 | struct bpf_insn_access_aux info = { |
3956 | .reg_type = *reg_type, | |
9e15db66 | 3957 | .log = &env->log, |
f96da094 | 3958 | }; |
31fd8581 | 3959 | |
4f9218aa | 3960 | if (env->ops->is_valid_access && |
5e43f899 | 3961 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
3962 | /* A non zero info.ctx_field_size indicates that this field is a |
3963 | * candidate for later verifier transformation to load the whole | |
3964 | * field and then apply a mask when accessed with a narrower | |
3965 | * access than actual ctx access size. A zero info.ctx_field_size | |
3966 | * will only allow for whole field access and rejects any other | |
3967 | * type of narrower access. | |
31fd8581 | 3968 | */ |
23994631 | 3969 | *reg_type = info.reg_type; |
31fd8581 | 3970 | |
c25b2ae1 | 3971 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 3972 | *btf = info.btf; |
9e15db66 | 3973 | *btf_id = info.btf_id; |
22dc4a0f | 3974 | } else { |
9e15db66 | 3975 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 3976 | } |
32bbe007 AS |
3977 | /* remember the offset of last byte accessed in ctx */ |
3978 | if (env->prog->aux->max_ctx_offset < off + size) | |
3979 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 3980 | return 0; |
32bbe007 | 3981 | } |
17a52670 | 3982 | |
61bd5218 | 3983 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
3984 | return -EACCES; |
3985 | } | |
3986 | ||
d58e468b PP |
3987 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
3988 | int size) | |
3989 | { | |
3990 | if (size < 0 || off < 0 || | |
3991 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
3992 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
3993 | off, size); | |
3994 | return -EACCES; | |
3995 | } | |
3996 | return 0; | |
3997 | } | |
3998 | ||
5f456649 MKL |
3999 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
4000 | u32 regno, int off, int size, | |
4001 | enum bpf_access_type t) | |
c64b7983 JS |
4002 | { |
4003 | struct bpf_reg_state *regs = cur_regs(env); | |
4004 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 4005 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 4006 | bool valid; |
c64b7983 JS |
4007 | |
4008 | if (reg->smin_value < 0) { | |
4009 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
4010 | regno); | |
4011 | return -EACCES; | |
4012 | } | |
4013 | ||
46f8bc92 MKL |
4014 | switch (reg->type) { |
4015 | case PTR_TO_SOCK_COMMON: | |
4016 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
4017 | break; | |
4018 | case PTR_TO_SOCKET: | |
4019 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
4020 | break; | |
655a51e5 MKL |
4021 | case PTR_TO_TCP_SOCK: |
4022 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
4023 | break; | |
fada7fdc JL |
4024 | case PTR_TO_XDP_SOCK: |
4025 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
4026 | break; | |
46f8bc92 MKL |
4027 | default: |
4028 | valid = false; | |
c64b7983 JS |
4029 | } |
4030 | ||
5f456649 | 4031 | |
46f8bc92 MKL |
4032 | if (valid) { |
4033 | env->insn_aux_data[insn_idx].ctx_field_size = | |
4034 | info.ctx_field_size; | |
4035 | return 0; | |
4036 | } | |
4037 | ||
4038 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
c25b2ae1 | 4039 | regno, reg_type_str(env, reg->type), off, size); |
46f8bc92 MKL |
4040 | |
4041 | return -EACCES; | |
c64b7983 JS |
4042 | } |
4043 | ||
4cabc5b1 DB |
4044 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
4045 | { | |
2a159c6f | 4046 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
4047 | } |
4048 | ||
f37a8cb8 DB |
4049 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
4050 | { | |
2a159c6f | 4051 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 4052 | |
46f8bc92 MKL |
4053 | return reg->type == PTR_TO_CTX; |
4054 | } | |
4055 | ||
4056 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
4057 | { | |
4058 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4059 | ||
4060 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
4061 | } |
4062 | ||
ca369602 DB |
4063 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
4064 | { | |
2a159c6f | 4065 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
4066 | |
4067 | return type_is_pkt_pointer(reg->type); | |
4068 | } | |
4069 | ||
4b5defde DB |
4070 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
4071 | { | |
4072 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4073 | ||
4074 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
4075 | return reg->type == PTR_TO_FLOW_KEYS; | |
4076 | } | |
4077 | ||
61bd5218 JK |
4078 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
4079 | const struct bpf_reg_state *reg, | |
d1174416 | 4080 | int off, int size, bool strict) |
969bf05e | 4081 | { |
f1174f77 | 4082 | struct tnum reg_off; |
e07b98d9 | 4083 | int ip_align; |
d1174416 DM |
4084 | |
4085 | /* Byte size accesses are always allowed. */ | |
4086 | if (!strict || size == 1) | |
4087 | return 0; | |
4088 | ||
e4eda884 DM |
4089 | /* For platforms that do not have a Kconfig enabling |
4090 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
4091 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
4092 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
4093 | * to this code only in strict mode where we want to emulate | |
4094 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
4095 | * unconditional IP align value of '2'. | |
e07b98d9 | 4096 | */ |
e4eda884 | 4097 | ip_align = 2; |
f1174f77 EC |
4098 | |
4099 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
4100 | if (!tnum_is_aligned(reg_off, size)) { | |
4101 | char tn_buf[48]; | |
4102 | ||
4103 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
4104 | verbose(env, |
4105 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 4106 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
4107 | return -EACCES; |
4108 | } | |
79adffcd | 4109 | |
969bf05e AS |
4110 | return 0; |
4111 | } | |
4112 | ||
61bd5218 JK |
4113 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
4114 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
4115 | const char *pointer_desc, |
4116 | int off, int size, bool strict) | |
79adffcd | 4117 | { |
f1174f77 EC |
4118 | struct tnum reg_off; |
4119 | ||
4120 | /* Byte size accesses are always allowed. */ | |
4121 | if (!strict || size == 1) | |
4122 | return 0; | |
4123 | ||
4124 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
4125 | if (!tnum_is_aligned(reg_off, size)) { | |
4126 | char tn_buf[48]; | |
4127 | ||
4128 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 4129 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 4130 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
4131 | return -EACCES; |
4132 | } | |
4133 | ||
969bf05e AS |
4134 | return 0; |
4135 | } | |
4136 | ||
e07b98d9 | 4137 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
4138 | const struct bpf_reg_state *reg, int off, |
4139 | int size, bool strict_alignment_once) | |
79adffcd | 4140 | { |
ca369602 | 4141 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 4142 | const char *pointer_desc = ""; |
d1174416 | 4143 | |
79adffcd DB |
4144 | switch (reg->type) { |
4145 | case PTR_TO_PACKET: | |
de8f3a83 DB |
4146 | case PTR_TO_PACKET_META: |
4147 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
4148 | * right in front, treat it the very same way. | |
4149 | */ | |
61bd5218 | 4150 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
4151 | case PTR_TO_FLOW_KEYS: |
4152 | pointer_desc = "flow keys "; | |
4153 | break; | |
69c087ba YS |
4154 | case PTR_TO_MAP_KEY: |
4155 | pointer_desc = "key "; | |
4156 | break; | |
f1174f77 EC |
4157 | case PTR_TO_MAP_VALUE: |
4158 | pointer_desc = "value "; | |
4159 | break; | |
4160 | case PTR_TO_CTX: | |
4161 | pointer_desc = "context "; | |
4162 | break; | |
4163 | case PTR_TO_STACK: | |
4164 | pointer_desc = "stack "; | |
01f810ac AM |
4165 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
4166 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
4167 | * aligned. |
4168 | */ | |
4169 | strict = true; | |
f1174f77 | 4170 | break; |
c64b7983 JS |
4171 | case PTR_TO_SOCKET: |
4172 | pointer_desc = "sock "; | |
4173 | break; | |
46f8bc92 MKL |
4174 | case PTR_TO_SOCK_COMMON: |
4175 | pointer_desc = "sock_common "; | |
4176 | break; | |
655a51e5 MKL |
4177 | case PTR_TO_TCP_SOCK: |
4178 | pointer_desc = "tcp_sock "; | |
4179 | break; | |
fada7fdc JL |
4180 | case PTR_TO_XDP_SOCK: |
4181 | pointer_desc = "xdp_sock "; | |
4182 | break; | |
79adffcd | 4183 | default: |
f1174f77 | 4184 | break; |
79adffcd | 4185 | } |
61bd5218 JK |
4186 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
4187 | strict); | |
79adffcd DB |
4188 | } |
4189 | ||
f4d7e40a AS |
4190 | static int update_stack_depth(struct bpf_verifier_env *env, |
4191 | const struct bpf_func_state *func, | |
4192 | int off) | |
4193 | { | |
9c8105bd | 4194 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
4195 | |
4196 | if (stack >= -off) | |
4197 | return 0; | |
4198 | ||
4199 | /* update known max for given subprogram */ | |
9c8105bd | 4200 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
4201 | return 0; |
4202 | } | |
f4d7e40a | 4203 | |
70a87ffe AS |
4204 | /* starting from main bpf function walk all instructions of the function |
4205 | * and recursively walk all callees that given function can call. | |
4206 | * Ignore jump and exit insns. | |
4207 | * Since recursion is prevented by check_cfg() this algorithm | |
4208 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
4209 | */ | |
4210 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
4211 | { | |
9c8105bd JW |
4212 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
4213 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 4214 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 4215 | bool tail_call_reachable = false; |
70a87ffe AS |
4216 | int ret_insn[MAX_CALL_FRAMES]; |
4217 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 4218 | int j; |
f4d7e40a | 4219 | |
70a87ffe | 4220 | process_func: |
7f6e4312 MF |
4221 | /* protect against potential stack overflow that might happen when |
4222 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
4223 | * depth for such case down to 256 so that the worst case scenario | |
4224 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
4225 | * 8k). | |
4226 | * | |
4227 | * To get the idea what might happen, see an example: | |
4228 | * func1 -> sub rsp, 128 | |
4229 | * subfunc1 -> sub rsp, 256 | |
4230 | * tailcall1 -> add rsp, 256 | |
4231 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
4232 | * subfunc2 -> sub rsp, 64 | |
4233 | * subfunc22 -> sub rsp, 128 | |
4234 | * tailcall2 -> add rsp, 128 | |
4235 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
4236 | * | |
4237 | * tailcall will unwind the current stack frame but it will not get rid | |
4238 | * of caller's stack as shown on the example above. | |
4239 | */ | |
4240 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
4241 | verbose(env, | |
4242 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
4243 | depth); | |
4244 | return -EACCES; | |
4245 | } | |
70a87ffe AS |
4246 | /* round up to 32-bytes, since this is granularity |
4247 | * of interpreter stack size | |
4248 | */ | |
9c8105bd | 4249 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 4250 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 4251 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 4252 | frame + 1, depth); |
f4d7e40a AS |
4253 | return -EACCES; |
4254 | } | |
70a87ffe | 4255 | continue_func: |
4cb3d99c | 4256 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 4257 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
4258 | int next_insn; |
4259 | ||
69c087ba | 4260 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
4261 | continue; |
4262 | /* remember insn and function to return to */ | |
4263 | ret_insn[frame] = i + 1; | |
9c8105bd | 4264 | ret_prog[frame] = idx; |
70a87ffe AS |
4265 | |
4266 | /* find the callee */ | |
7ddc80a4 AS |
4267 | next_insn = i + insn[i].imm + 1; |
4268 | idx = find_subprog(env, next_insn); | |
9c8105bd | 4269 | if (idx < 0) { |
70a87ffe | 4270 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 4271 | next_insn); |
70a87ffe AS |
4272 | return -EFAULT; |
4273 | } | |
7ddc80a4 AS |
4274 | if (subprog[idx].is_async_cb) { |
4275 | if (subprog[idx].has_tail_call) { | |
4276 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
4277 | return -EFAULT; | |
4278 | } | |
4279 | /* async callbacks don't increase bpf prog stack size */ | |
4280 | continue; | |
4281 | } | |
4282 | i = next_insn; | |
ebf7d1f5 MF |
4283 | |
4284 | if (subprog[idx].has_tail_call) | |
4285 | tail_call_reachable = true; | |
4286 | ||
70a87ffe AS |
4287 | frame++; |
4288 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
4289 | verbose(env, "the call stack of %d frames is too deep !\n", |
4290 | frame); | |
4291 | return -E2BIG; | |
70a87ffe AS |
4292 | } |
4293 | goto process_func; | |
4294 | } | |
ebf7d1f5 MF |
4295 | /* if tail call got detected across bpf2bpf calls then mark each of the |
4296 | * currently present subprog frames as tail call reachable subprogs; | |
4297 | * this info will be utilized by JIT so that we will be preserving the | |
4298 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
4299 | */ | |
4300 | if (tail_call_reachable) | |
4301 | for (j = 0; j < frame; j++) | |
4302 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
4303 | if (subprog[0].tail_call_reachable) |
4304 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 4305 | |
70a87ffe AS |
4306 | /* end of for() loop means the last insn of the 'subprog' |
4307 | * was reached. Doesn't matter whether it was JA or EXIT | |
4308 | */ | |
4309 | if (frame == 0) | |
4310 | return 0; | |
9c8105bd | 4311 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
4312 | frame--; |
4313 | i = ret_insn[frame]; | |
9c8105bd | 4314 | idx = ret_prog[frame]; |
70a87ffe | 4315 | goto continue_func; |
f4d7e40a AS |
4316 | } |
4317 | ||
19d28fbd | 4318 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
4319 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
4320 | const struct bpf_insn *insn, int idx) | |
4321 | { | |
4322 | int start = idx + insn->imm + 1, subprog; | |
4323 | ||
4324 | subprog = find_subprog(env, start); | |
4325 | if (subprog < 0) { | |
4326 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
4327 | start); | |
4328 | return -EFAULT; | |
4329 | } | |
9c8105bd | 4330 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 4331 | } |
19d28fbd | 4332 | #endif |
1ea47e01 | 4333 | |
afbf21dc YS |
4334 | static int __check_buffer_access(struct bpf_verifier_env *env, |
4335 | const char *buf_info, | |
4336 | const struct bpf_reg_state *reg, | |
4337 | int regno, int off, int size) | |
9df1c28b MM |
4338 | { |
4339 | if (off < 0) { | |
4340 | verbose(env, | |
4fc00b79 | 4341 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 4342 | regno, buf_info, off, size); |
9df1c28b MM |
4343 | return -EACCES; |
4344 | } | |
4345 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4346 | char tn_buf[48]; | |
4347 | ||
4348 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4349 | verbose(env, | |
4fc00b79 | 4350 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
4351 | regno, off, tn_buf); |
4352 | return -EACCES; | |
4353 | } | |
afbf21dc YS |
4354 | |
4355 | return 0; | |
4356 | } | |
4357 | ||
4358 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
4359 | const struct bpf_reg_state *reg, | |
4360 | int regno, int off, int size) | |
4361 | { | |
4362 | int err; | |
4363 | ||
4364 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
4365 | if (err) | |
4366 | return err; | |
4367 | ||
9df1c28b MM |
4368 | if (off + size > env->prog->aux->max_tp_access) |
4369 | env->prog->aux->max_tp_access = off + size; | |
4370 | ||
4371 | return 0; | |
4372 | } | |
4373 | ||
afbf21dc YS |
4374 | static int check_buffer_access(struct bpf_verifier_env *env, |
4375 | const struct bpf_reg_state *reg, | |
4376 | int regno, int off, int size, | |
4377 | bool zero_size_allowed, | |
afbf21dc YS |
4378 | u32 *max_access) |
4379 | { | |
44e9a741 | 4380 | const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; |
afbf21dc YS |
4381 | int err; |
4382 | ||
4383 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
4384 | if (err) | |
4385 | return err; | |
4386 | ||
4387 | if (off + size > *max_access) | |
4388 | *max_access = off + size; | |
4389 | ||
4390 | return 0; | |
4391 | } | |
4392 | ||
3f50f132 JF |
4393 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
4394 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
4395 | { | |
4396 | reg->var_off = tnum_subreg(reg->var_off); | |
4397 | __reg_assign_32_into_64(reg); | |
4398 | } | |
9df1c28b | 4399 | |
0c17d1d2 JH |
4400 | /* truncate register to smaller size (in bytes) |
4401 | * must be called with size < BPF_REG_SIZE | |
4402 | */ | |
4403 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
4404 | { | |
4405 | u64 mask; | |
4406 | ||
4407 | /* clear high bits in bit representation */ | |
4408 | reg->var_off = tnum_cast(reg->var_off, size); | |
4409 | ||
4410 | /* fix arithmetic bounds */ | |
4411 | mask = ((u64)1 << (size * 8)) - 1; | |
4412 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
4413 | reg->umin_value &= mask; | |
4414 | reg->umax_value &= mask; | |
4415 | } else { | |
4416 | reg->umin_value = 0; | |
4417 | reg->umax_value = mask; | |
4418 | } | |
4419 | reg->smin_value = reg->umin_value; | |
4420 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
4421 | |
4422 | /* If size is smaller than 32bit register the 32bit register | |
4423 | * values are also truncated so we push 64-bit bounds into | |
4424 | * 32-bit bounds. Above were truncated < 32-bits already. | |
4425 | */ | |
4426 | if (size >= 4) | |
4427 | return; | |
4428 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
4429 | } |
4430 | ||
a23740ec AN |
4431 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
4432 | { | |
353050be DB |
4433 | /* A map is considered read-only if the following condition are true: |
4434 | * | |
4435 | * 1) BPF program side cannot change any of the map content. The | |
4436 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
4437 | * and was set at map creation time. | |
4438 | * 2) The map value(s) have been initialized from user space by a | |
4439 | * loader and then "frozen", such that no new map update/delete | |
4440 | * operations from syscall side are possible for the rest of | |
4441 | * the map's lifetime from that point onwards. | |
4442 | * 3) Any parallel/pending map update/delete operations from syscall | |
4443 | * side have been completed. Only after that point, it's safe to | |
4444 | * assume that map value(s) are immutable. | |
4445 | */ | |
4446 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
4447 | READ_ONCE(map->frozen) && | |
4448 | !bpf_map_write_active(map); | |
a23740ec AN |
4449 | } |
4450 | ||
4451 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
4452 | { | |
4453 | void *ptr; | |
4454 | u64 addr; | |
4455 | int err; | |
4456 | ||
4457 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
4458 | if (err) | |
4459 | return err; | |
2dedd7d2 | 4460 | ptr = (void *)(long)addr + off; |
a23740ec AN |
4461 | |
4462 | switch (size) { | |
4463 | case sizeof(u8): | |
4464 | *val = (u64)*(u8 *)ptr; | |
4465 | break; | |
4466 | case sizeof(u16): | |
4467 | *val = (u64)*(u16 *)ptr; | |
4468 | break; | |
4469 | case sizeof(u32): | |
4470 | *val = (u64)*(u32 *)ptr; | |
4471 | break; | |
4472 | case sizeof(u64): | |
4473 | *val = *(u64 *)ptr; | |
4474 | break; | |
4475 | default: | |
4476 | return -EINVAL; | |
4477 | } | |
4478 | return 0; | |
4479 | } | |
4480 | ||
9e15db66 AS |
4481 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
4482 | struct bpf_reg_state *regs, | |
4483 | int regno, int off, int size, | |
4484 | enum bpf_access_type atype, | |
4485 | int value_regno) | |
4486 | { | |
4487 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
4488 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
4489 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
c6f1bfe8 | 4490 | enum bpf_type_flag flag = 0; |
9e15db66 AS |
4491 | u32 btf_id; |
4492 | int ret; | |
4493 | ||
9e15db66 AS |
4494 | if (off < 0) { |
4495 | verbose(env, | |
4496 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
4497 | regno, tname, off); | |
4498 | return -EACCES; | |
4499 | } | |
4500 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4501 | char tn_buf[48]; | |
4502 | ||
4503 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4504 | verbose(env, | |
4505 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
4506 | regno, tname, off, tn_buf); | |
4507 | return -EACCES; | |
4508 | } | |
4509 | ||
c6f1bfe8 YS |
4510 | if (reg->type & MEM_USER) { |
4511 | verbose(env, | |
4512 | "R%d is ptr_%s access user memory: off=%d\n", | |
4513 | regno, tname, off); | |
4514 | return -EACCES; | |
4515 | } | |
4516 | ||
5844101a HL |
4517 | if (reg->type & MEM_PERCPU) { |
4518 | verbose(env, | |
4519 | "R%d is ptr_%s access percpu memory: off=%d\n", | |
4520 | regno, tname, off); | |
4521 | return -EACCES; | |
4522 | } | |
4523 | ||
27ae7997 | 4524 | if (env->ops->btf_struct_access) { |
22dc4a0f | 4525 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
c6f1bfe8 | 4526 | off, size, atype, &btf_id, &flag); |
27ae7997 MKL |
4527 | } else { |
4528 | if (atype != BPF_READ) { | |
4529 | verbose(env, "only read is supported\n"); | |
4530 | return -EACCES; | |
4531 | } | |
4532 | ||
22dc4a0f | 4533 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
c6f1bfe8 | 4534 | atype, &btf_id, &flag); |
27ae7997 MKL |
4535 | } |
4536 | ||
9e15db66 AS |
4537 | if (ret < 0) |
4538 | return ret; | |
4539 | ||
6efe152d KKD |
4540 | /* If this is an untrusted pointer, all pointers formed by walking it |
4541 | * also inherit the untrusted flag. | |
4542 | */ | |
4543 | if (type_flag(reg->type) & PTR_UNTRUSTED) | |
4544 | flag |= PTR_UNTRUSTED; | |
4545 | ||
41c48f3a | 4546 | if (atype == BPF_READ && value_regno >= 0) |
c6f1bfe8 | 4547 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); |
41c48f3a AI |
4548 | |
4549 | return 0; | |
4550 | } | |
4551 | ||
4552 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
4553 | struct bpf_reg_state *regs, | |
4554 | int regno, int off, int size, | |
4555 | enum bpf_access_type atype, | |
4556 | int value_regno) | |
4557 | { | |
4558 | struct bpf_reg_state *reg = regs + regno; | |
4559 | struct bpf_map *map = reg->map_ptr; | |
c6f1bfe8 | 4560 | enum bpf_type_flag flag = 0; |
41c48f3a AI |
4561 | const struct btf_type *t; |
4562 | const char *tname; | |
4563 | u32 btf_id; | |
4564 | int ret; | |
4565 | ||
4566 | if (!btf_vmlinux) { | |
4567 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
4568 | return -ENOTSUPP; | |
4569 | } | |
4570 | ||
4571 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
4572 | verbose(env, "map_ptr access not supported for map type %d\n", | |
4573 | map->map_type); | |
4574 | return -ENOTSUPP; | |
4575 | } | |
4576 | ||
4577 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
4578 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
4579 | ||
4580 | if (!env->allow_ptr_to_map_access) { | |
4581 | verbose(env, | |
4582 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
4583 | tname); | |
4584 | return -EPERM; | |
9e15db66 | 4585 | } |
27ae7997 | 4586 | |
41c48f3a AI |
4587 | if (off < 0) { |
4588 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
4589 | regno, tname, off); | |
4590 | return -EACCES; | |
4591 | } | |
4592 | ||
4593 | if (atype != BPF_READ) { | |
4594 | verbose(env, "only read from %s is supported\n", tname); | |
4595 | return -EACCES; | |
4596 | } | |
4597 | ||
c6f1bfe8 | 4598 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id, &flag); |
41c48f3a AI |
4599 | if (ret < 0) |
4600 | return ret; | |
4601 | ||
4602 | if (value_regno >= 0) | |
c6f1bfe8 | 4603 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id, flag); |
41c48f3a | 4604 | |
9e15db66 AS |
4605 | return 0; |
4606 | } | |
4607 | ||
01f810ac AM |
4608 | /* Check that the stack access at the given offset is within bounds. The |
4609 | * maximum valid offset is -1. | |
4610 | * | |
4611 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
4612 | * -state->allocated_stack for reads. | |
4613 | */ | |
4614 | static int check_stack_slot_within_bounds(int off, | |
4615 | struct bpf_func_state *state, | |
4616 | enum bpf_access_type t) | |
4617 | { | |
4618 | int min_valid_off; | |
4619 | ||
4620 | if (t == BPF_WRITE) | |
4621 | min_valid_off = -MAX_BPF_STACK; | |
4622 | else | |
4623 | min_valid_off = -state->allocated_stack; | |
4624 | ||
4625 | if (off < min_valid_off || off > -1) | |
4626 | return -EACCES; | |
4627 | return 0; | |
4628 | } | |
4629 | ||
4630 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
4631 | * bounds. | |
4632 | * | |
4633 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
4634 | */ | |
4635 | static int check_stack_access_within_bounds( | |
4636 | struct bpf_verifier_env *env, | |
4637 | int regno, int off, int access_size, | |
61df10c7 | 4638 | enum bpf_access_src src, enum bpf_access_type type) |
01f810ac AM |
4639 | { |
4640 | struct bpf_reg_state *regs = cur_regs(env); | |
4641 | struct bpf_reg_state *reg = regs + regno; | |
4642 | struct bpf_func_state *state = func(env, reg); | |
4643 | int min_off, max_off; | |
4644 | int err; | |
4645 | char *err_extra; | |
4646 | ||
4647 | if (src == ACCESS_HELPER) | |
4648 | /* We don't know if helpers are reading or writing (or both). */ | |
4649 | err_extra = " indirect access to"; | |
4650 | else if (type == BPF_READ) | |
4651 | err_extra = " read from"; | |
4652 | else | |
4653 | err_extra = " write to"; | |
4654 | ||
4655 | if (tnum_is_const(reg->var_off)) { | |
4656 | min_off = reg->var_off.value + off; | |
4657 | if (access_size > 0) | |
4658 | max_off = min_off + access_size - 1; | |
4659 | else | |
4660 | max_off = min_off; | |
4661 | } else { | |
4662 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
4663 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
4664 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
4665 | err_extra, regno); | |
4666 | return -EACCES; | |
4667 | } | |
4668 | min_off = reg->smin_value + off; | |
4669 | if (access_size > 0) | |
4670 | max_off = reg->smax_value + off + access_size - 1; | |
4671 | else | |
4672 | max_off = min_off; | |
4673 | } | |
4674 | ||
4675 | err = check_stack_slot_within_bounds(min_off, state, type); | |
4676 | if (!err) | |
4677 | err = check_stack_slot_within_bounds(max_off, state, type); | |
4678 | ||
4679 | if (err) { | |
4680 | if (tnum_is_const(reg->var_off)) { | |
4681 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
4682 | err_extra, regno, off, access_size); | |
4683 | } else { | |
4684 | char tn_buf[48]; | |
4685 | ||
4686 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4687 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
4688 | err_extra, regno, tn_buf, access_size); | |
4689 | } | |
4690 | } | |
4691 | return err; | |
4692 | } | |
41c48f3a | 4693 | |
17a52670 AS |
4694 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
4695 | * if t==write, value_regno is a register which value is stored into memory | |
4696 | * if t==read, value_regno is a register which will receive the value from memory | |
4697 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
4698 | * if t==read && value_regno==-1, don't care what we read from memory | |
4699 | */ | |
ca369602 DB |
4700 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
4701 | int off, int bpf_size, enum bpf_access_type t, | |
4702 | int value_regno, bool strict_alignment_once) | |
17a52670 | 4703 | { |
638f5b90 AS |
4704 | struct bpf_reg_state *regs = cur_regs(env); |
4705 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 4706 | struct bpf_func_state *state; |
17a52670 AS |
4707 | int size, err = 0; |
4708 | ||
4709 | size = bpf_size_to_bytes(bpf_size); | |
4710 | if (size < 0) | |
4711 | return size; | |
4712 | ||
f1174f77 | 4713 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 4714 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
4715 | if (err) |
4716 | return err; | |
17a52670 | 4717 | |
f1174f77 EC |
4718 | /* for access checks, reg->off is just part of off */ |
4719 | off += reg->off; | |
4720 | ||
69c087ba YS |
4721 | if (reg->type == PTR_TO_MAP_KEY) { |
4722 | if (t == BPF_WRITE) { | |
4723 | verbose(env, "write to change key R%d not allowed\n", regno); | |
4724 | return -EACCES; | |
4725 | } | |
4726 | ||
4727 | err = check_mem_region_access(env, regno, off, size, | |
4728 | reg->map_ptr->key_size, false); | |
4729 | if (err) | |
4730 | return err; | |
4731 | if (value_regno >= 0) | |
4732 | mark_reg_unknown(env, regs, value_regno); | |
4733 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
aa3496ac | 4734 | struct btf_field *kptr_field = NULL; |
61df10c7 | 4735 | |
1be7f75d AS |
4736 | if (t == BPF_WRITE && value_regno >= 0 && |
4737 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4738 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
4739 | return -EACCES; |
4740 | } | |
591fe988 DB |
4741 | err = check_map_access_type(env, regno, off, size, t); |
4742 | if (err) | |
4743 | return err; | |
61df10c7 KKD |
4744 | err = check_map_access(env, regno, off, size, false, ACCESS_DIRECT); |
4745 | if (err) | |
4746 | return err; | |
4747 | if (tnum_is_const(reg->var_off)) | |
aa3496ac KKD |
4748 | kptr_field = btf_record_find(reg->map_ptr->record, |
4749 | off + reg->var_off.value, BPF_KPTR); | |
4750 | if (kptr_field) { | |
4751 | err = check_map_kptr_access(env, regno, value_regno, insn_idx, kptr_field); | |
61df10c7 | 4752 | } else if (t == BPF_READ && value_regno >= 0) { |
a23740ec AN |
4753 | struct bpf_map *map = reg->map_ptr; |
4754 | ||
4755 | /* if map is read-only, track its contents as scalars */ | |
4756 | if (tnum_is_const(reg->var_off) && | |
4757 | bpf_map_is_rdonly(map) && | |
4758 | map->ops->map_direct_value_addr) { | |
4759 | int map_off = off + reg->var_off.value; | |
4760 | u64 val = 0; | |
4761 | ||
4762 | err = bpf_map_direct_read(map, map_off, size, | |
4763 | &val); | |
4764 | if (err) | |
4765 | return err; | |
4766 | ||
4767 | regs[value_regno].type = SCALAR_VALUE; | |
4768 | __mark_reg_known(®s[value_regno], val); | |
4769 | } else { | |
4770 | mark_reg_unknown(env, regs, value_regno); | |
4771 | } | |
4772 | } | |
34d3a78c HL |
4773 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
4774 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
4775 | ||
4776 | if (type_may_be_null(reg->type)) { | |
4777 | verbose(env, "R%d invalid mem access '%s'\n", regno, | |
4778 | reg_type_str(env, reg->type)); | |
4779 | return -EACCES; | |
4780 | } | |
4781 | ||
4782 | if (t == BPF_WRITE && rdonly_mem) { | |
4783 | verbose(env, "R%d cannot write into %s\n", | |
4784 | regno, reg_type_str(env, reg->type)); | |
4785 | return -EACCES; | |
4786 | } | |
4787 | ||
457f4436 AN |
4788 | if (t == BPF_WRITE && value_regno >= 0 && |
4789 | is_pointer_value(env, value_regno)) { | |
4790 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
4791 | return -EACCES; | |
4792 | } | |
34d3a78c | 4793 | |
457f4436 AN |
4794 | err = check_mem_region_access(env, regno, off, size, |
4795 | reg->mem_size, false); | |
34d3a78c | 4796 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
457f4436 | 4797 | mark_reg_unknown(env, regs, value_regno); |
1a0dc1ac | 4798 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 4799 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 4800 | struct btf *btf = NULL; |
9e15db66 | 4801 | u32 btf_id = 0; |
19de99f7 | 4802 | |
1be7f75d AS |
4803 | if (t == BPF_WRITE && value_regno >= 0 && |
4804 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4805 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
4806 | return -EACCES; |
4807 | } | |
f1174f77 | 4808 | |
be80a1d3 | 4809 | err = check_ptr_off_reg(env, reg, regno); |
58990d1f DB |
4810 | if (err < 0) |
4811 | return err; | |
4812 | ||
c6f1bfe8 YS |
4813 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, |
4814 | &btf_id); | |
9e15db66 AS |
4815 | if (err) |
4816 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 4817 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 4818 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
4819 | * PTR_TO_PACKET[_META,_END]. In the latter |
4820 | * case, we know the offset is zero. | |
f1174f77 | 4821 | */ |
46f8bc92 | 4822 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 4823 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 4824 | } else { |
638f5b90 | 4825 | mark_reg_known_zero(env, regs, |
61bd5218 | 4826 | value_regno); |
c25b2ae1 | 4827 | if (type_may_be_null(reg_type)) |
46f8bc92 | 4828 | regs[value_regno].id = ++env->id_gen; |
5327ed3d JW |
4829 | /* A load of ctx field could have different |
4830 | * actual load size with the one encoded in the | |
4831 | * insn. When the dst is PTR, it is for sure not | |
4832 | * a sub-register. | |
4833 | */ | |
4834 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
c25b2ae1 | 4835 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 4836 | regs[value_regno].btf = btf; |
9e15db66 | 4837 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 4838 | } |
46f8bc92 | 4839 | } |
638f5b90 | 4840 | regs[value_regno].type = reg_type; |
969bf05e | 4841 | } |
17a52670 | 4842 | |
f1174f77 | 4843 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
4844 | /* Basic bounds checks. */ |
4845 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
4846 | if (err) |
4847 | return err; | |
8726679a | 4848 | |
f4d7e40a AS |
4849 | state = func(env, reg); |
4850 | err = update_stack_depth(env, state, off); | |
4851 | if (err) | |
4852 | return err; | |
8726679a | 4853 | |
01f810ac AM |
4854 | if (t == BPF_READ) |
4855 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 4856 | value_regno); |
01f810ac AM |
4857 | else |
4858 | err = check_stack_write(env, regno, off, size, | |
4859 | value_regno, insn_idx); | |
de8f3a83 | 4860 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 4861 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 4862 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
4863 | return -EACCES; |
4864 | } | |
4acf6c0b BB |
4865 | if (t == BPF_WRITE && value_regno >= 0 && |
4866 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
4867 | verbose(env, "R%d leaks addr into packet\n", |
4868 | value_regno); | |
4acf6c0b BB |
4869 | return -EACCES; |
4870 | } | |
9fd29c08 | 4871 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 4872 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 4873 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
4874 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
4875 | if (t == BPF_WRITE && value_regno >= 0 && | |
4876 | is_pointer_value(env, value_regno)) { | |
4877 | verbose(env, "R%d leaks addr into flow keys\n", | |
4878 | value_regno); | |
4879 | return -EACCES; | |
4880 | } | |
4881 | ||
4882 | err = check_flow_keys_access(env, off, size); | |
4883 | if (!err && t == BPF_READ && value_regno >= 0) | |
4884 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 4885 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 4886 | if (t == BPF_WRITE) { |
46f8bc92 | 4887 | verbose(env, "R%d cannot write into %s\n", |
c25b2ae1 | 4888 | regno, reg_type_str(env, reg->type)); |
c64b7983 JS |
4889 | return -EACCES; |
4890 | } | |
5f456649 | 4891 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
4892 | if (!err && value_regno >= 0) |
4893 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
4894 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
4895 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
4896 | if (!err && t == BPF_READ && value_regno >= 0) | |
4897 | mark_reg_unknown(env, regs, value_regno); | |
bff61f6f HL |
4898 | } else if (base_type(reg->type) == PTR_TO_BTF_ID && |
4899 | !type_may_be_null(reg->type)) { | |
9e15db66 AS |
4900 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
4901 | value_regno); | |
41c48f3a AI |
4902 | } else if (reg->type == CONST_PTR_TO_MAP) { |
4903 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
4904 | value_regno); | |
20b2aff4 HL |
4905 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
4906 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
20b2aff4 HL |
4907 | u32 *max_access; |
4908 | ||
4909 | if (rdonly_mem) { | |
4910 | if (t == BPF_WRITE) { | |
4911 | verbose(env, "R%d cannot write into %s\n", | |
4912 | regno, reg_type_str(env, reg->type)); | |
4913 | return -EACCES; | |
4914 | } | |
20b2aff4 HL |
4915 | max_access = &env->prog->aux->max_rdonly_access; |
4916 | } else { | |
20b2aff4 | 4917 | max_access = &env->prog->aux->max_rdwr_access; |
afbf21dc | 4918 | } |
20b2aff4 | 4919 | |
f6dfbe31 | 4920 | err = check_buffer_access(env, reg, regno, off, size, false, |
44e9a741 | 4921 | max_access); |
20b2aff4 HL |
4922 | |
4923 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) | |
afbf21dc | 4924 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 4925 | } else { |
61bd5218 | 4926 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
c25b2ae1 | 4927 | reg_type_str(env, reg->type)); |
17a52670 AS |
4928 | return -EACCES; |
4929 | } | |
969bf05e | 4930 | |
f1174f77 | 4931 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 4932 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 4933 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 4934 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 4935 | } |
17a52670 AS |
4936 | return err; |
4937 | } | |
4938 | ||
91c960b0 | 4939 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 4940 | { |
5ffa2550 | 4941 | int load_reg; |
17a52670 AS |
4942 | int err; |
4943 | ||
5ca419f2 BJ |
4944 | switch (insn->imm) { |
4945 | case BPF_ADD: | |
4946 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
4947 | case BPF_AND: |
4948 | case BPF_AND | BPF_FETCH: | |
4949 | case BPF_OR: | |
4950 | case BPF_OR | BPF_FETCH: | |
4951 | case BPF_XOR: | |
4952 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
4953 | case BPF_XCHG: |
4954 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
4955 | break; |
4956 | default: | |
91c960b0 BJ |
4957 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
4958 | return -EINVAL; | |
4959 | } | |
4960 | ||
4961 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
4962 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
4963 | return -EINVAL; |
4964 | } | |
4965 | ||
4966 | /* check src1 operand */ | |
dc503a8a | 4967 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4968 | if (err) |
4969 | return err; | |
4970 | ||
4971 | /* check src2 operand */ | |
dc503a8a | 4972 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4973 | if (err) |
4974 | return err; | |
4975 | ||
5ffa2550 BJ |
4976 | if (insn->imm == BPF_CMPXCHG) { |
4977 | /* Check comparison of R0 with memory location */ | |
a82fe085 DB |
4978 | const u32 aux_reg = BPF_REG_0; |
4979 | ||
4980 | err = check_reg_arg(env, aux_reg, SRC_OP); | |
5ffa2550 BJ |
4981 | if (err) |
4982 | return err; | |
a82fe085 DB |
4983 | |
4984 | if (is_pointer_value(env, aux_reg)) { | |
4985 | verbose(env, "R%d leaks addr into mem\n", aux_reg); | |
4986 | return -EACCES; | |
4987 | } | |
5ffa2550 BJ |
4988 | } |
4989 | ||
6bdf6abc | 4990 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 4991 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
4992 | return -EACCES; |
4993 | } | |
4994 | ||
ca369602 | 4995 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 4996 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
4997 | is_flow_key_reg(env, insn->dst_reg) || |
4998 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 4999 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f | 5000 | insn->dst_reg, |
c25b2ae1 | 5001 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
5002 | return -EACCES; |
5003 | } | |
5004 | ||
37086bfd BJ |
5005 | if (insn->imm & BPF_FETCH) { |
5006 | if (insn->imm == BPF_CMPXCHG) | |
5007 | load_reg = BPF_REG_0; | |
5008 | else | |
5009 | load_reg = insn->src_reg; | |
5010 | ||
5011 | /* check and record load of old value */ | |
5012 | err = check_reg_arg(env, load_reg, DST_OP); | |
5013 | if (err) | |
5014 | return err; | |
5015 | } else { | |
5016 | /* This instruction accesses a memory location but doesn't | |
5017 | * actually load it into a register. | |
5018 | */ | |
5019 | load_reg = -1; | |
5020 | } | |
5021 | ||
7d3baf0a DB |
5022 | /* Check whether we can read the memory, with second call for fetch |
5023 | * case to simulate the register fill. | |
5024 | */ | |
31fd8581 | 5025 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
7d3baf0a DB |
5026 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
5027 | if (!err && load_reg >= 0) | |
5028 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, | |
5029 | BPF_SIZE(insn->code), BPF_READ, load_reg, | |
5030 | true); | |
17a52670 AS |
5031 | if (err) |
5032 | return err; | |
5033 | ||
7d3baf0a | 5034 | /* Check whether we can write into the same memory. */ |
5ca419f2 BJ |
5035 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
5036 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
5037 | if (err) | |
5038 | return err; | |
5039 | ||
5ca419f2 | 5040 | return 0; |
17a52670 AS |
5041 | } |
5042 | ||
01f810ac AM |
5043 | /* When register 'regno' is used to read the stack (either directly or through |
5044 | * a helper function) make sure that it's within stack boundary and, depending | |
5045 | * on the access type, that all elements of the stack are initialized. | |
5046 | * | |
5047 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
5048 | * | |
5049 | * All registers that have been spilled on the stack in the slots within the | |
5050 | * read offsets are marked as read. | |
5051 | */ | |
5052 | static int check_stack_range_initialized( | |
5053 | struct bpf_verifier_env *env, int regno, int off, | |
5054 | int access_size, bool zero_size_allowed, | |
61df10c7 | 5055 | enum bpf_access_src type, struct bpf_call_arg_meta *meta) |
2011fccf AI |
5056 | { |
5057 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
5058 | struct bpf_func_state *state = func(env, reg); |
5059 | int err, min_off, max_off, i, j, slot, spi; | |
5060 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
5061 | enum bpf_access_type bounds_check_type; | |
5062 | /* Some accesses can write anything into the stack, others are | |
5063 | * read-only. | |
5064 | */ | |
5065 | bool clobber = false; | |
2011fccf | 5066 | |
01f810ac AM |
5067 | if (access_size == 0 && !zero_size_allowed) { |
5068 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
5069 | return -EACCES; |
5070 | } | |
2011fccf | 5071 | |
01f810ac AM |
5072 | if (type == ACCESS_HELPER) { |
5073 | /* The bounds checks for writes are more permissive than for | |
5074 | * reads. However, if raw_mode is not set, we'll do extra | |
5075 | * checks below. | |
5076 | */ | |
5077 | bounds_check_type = BPF_WRITE; | |
5078 | clobber = true; | |
5079 | } else { | |
5080 | bounds_check_type = BPF_READ; | |
5081 | } | |
5082 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
5083 | type, bounds_check_type); | |
5084 | if (err) | |
5085 | return err; | |
5086 | ||
17a52670 | 5087 | |
2011fccf | 5088 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 5089 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 5090 | } else { |
088ec26d AI |
5091 | /* Variable offset is prohibited for unprivileged mode for |
5092 | * simplicity since it requires corresponding support in | |
5093 | * Spectre masking for stack ALU. | |
5094 | * See also retrieve_ptr_limit(). | |
5095 | */ | |
2c78ee89 | 5096 | if (!env->bypass_spec_v1) { |
088ec26d | 5097 | char tn_buf[48]; |
f1174f77 | 5098 | |
088ec26d | 5099 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
5100 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
5101 | regno, err_extra, tn_buf); | |
088ec26d AI |
5102 | return -EACCES; |
5103 | } | |
f2bcd05e AI |
5104 | /* Only initialized buffer on stack is allowed to be accessed |
5105 | * with variable offset. With uninitialized buffer it's hard to | |
5106 | * guarantee that whole memory is marked as initialized on | |
5107 | * helper return since specific bounds are unknown what may | |
5108 | * cause uninitialized stack leaking. | |
5109 | */ | |
5110 | if (meta && meta->raw_mode) | |
5111 | meta = NULL; | |
5112 | ||
01f810ac AM |
5113 | min_off = reg->smin_value + off; |
5114 | max_off = reg->smax_value + off; | |
17a52670 AS |
5115 | } |
5116 | ||
435faee1 DB |
5117 | if (meta && meta->raw_mode) { |
5118 | meta->access_size = access_size; | |
5119 | meta->regno = regno; | |
5120 | return 0; | |
5121 | } | |
5122 | ||
2011fccf | 5123 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
5124 | u8 *stype; |
5125 | ||
2011fccf | 5126 | slot = -i - 1; |
638f5b90 | 5127 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
5128 | if (state->allocated_stack <= slot) |
5129 | goto err; | |
5130 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
5131 | if (*stype == STACK_MISC) | |
5132 | goto mark; | |
5133 | if (*stype == STACK_ZERO) { | |
01f810ac AM |
5134 | if (clobber) { |
5135 | /* helper can write anything into the stack */ | |
5136 | *stype = STACK_MISC; | |
5137 | } | |
cc2b14d5 | 5138 | goto mark; |
17a52670 | 5139 | } |
1d68f22b | 5140 | |
27113c59 | 5141 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
5142 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
5143 | env->allow_ptr_leaks)) { | |
01f810ac AM |
5144 | if (clobber) { |
5145 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
5146 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 5147 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 5148 | } |
f7cf25b2 AS |
5149 | goto mark; |
5150 | } | |
5151 | ||
cc2b14d5 | 5152 | err: |
2011fccf | 5153 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
5154 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
5155 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
5156 | } else { |
5157 | char tn_buf[48]; | |
5158 | ||
5159 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
5160 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
5161 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 5162 | } |
cc2b14d5 AS |
5163 | return -EACCES; |
5164 | mark: | |
5165 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
5166 | * the whole slot to be marked as 'read' | |
5167 | */ | |
679c782d | 5168 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
5169 | state->stack[spi].spilled_ptr.parent, |
5170 | REG_LIVE_READ64); | |
261f4664 KKD |
5171 | /* We do not set REG_LIVE_WRITTEN for stack slot, as we can not |
5172 | * be sure that whether stack slot is written to or not. Hence, | |
5173 | * we must still conservatively propagate reads upwards even if | |
5174 | * helper may write to the entire memory range. | |
5175 | */ | |
17a52670 | 5176 | } |
2011fccf | 5177 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
5178 | } |
5179 | ||
06c1c049 GB |
5180 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
5181 | int access_size, bool zero_size_allowed, | |
5182 | struct bpf_call_arg_meta *meta) | |
5183 | { | |
638f5b90 | 5184 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
20b2aff4 | 5185 | u32 *max_access; |
06c1c049 | 5186 | |
20b2aff4 | 5187 | switch (base_type(reg->type)) { |
06c1c049 | 5188 | case PTR_TO_PACKET: |
de8f3a83 | 5189 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
5190 | return check_packet_access(env, regno, reg->off, access_size, |
5191 | zero_size_allowed); | |
69c087ba | 5192 | case PTR_TO_MAP_KEY: |
7b3552d3 KKD |
5193 | if (meta && meta->raw_mode) { |
5194 | verbose(env, "R%d cannot write into %s\n", regno, | |
5195 | reg_type_str(env, reg->type)); | |
5196 | return -EACCES; | |
5197 | } | |
69c087ba YS |
5198 | return check_mem_region_access(env, regno, reg->off, access_size, |
5199 | reg->map_ptr->key_size, false); | |
06c1c049 | 5200 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
5201 | if (check_map_access_type(env, regno, reg->off, access_size, |
5202 | meta && meta->raw_mode ? BPF_WRITE : | |
5203 | BPF_READ)) | |
5204 | return -EACCES; | |
9fd29c08 | 5205 | return check_map_access(env, regno, reg->off, access_size, |
61df10c7 | 5206 | zero_size_allowed, ACCESS_HELPER); |
457f4436 | 5207 | case PTR_TO_MEM: |
97e6d7da KKD |
5208 | if (type_is_rdonly_mem(reg->type)) { |
5209 | if (meta && meta->raw_mode) { | |
5210 | verbose(env, "R%d cannot write into %s\n", regno, | |
5211 | reg_type_str(env, reg->type)); | |
5212 | return -EACCES; | |
5213 | } | |
5214 | } | |
457f4436 AN |
5215 | return check_mem_region_access(env, regno, reg->off, |
5216 | access_size, reg->mem_size, | |
5217 | zero_size_allowed); | |
20b2aff4 HL |
5218 | case PTR_TO_BUF: |
5219 | if (type_is_rdonly_mem(reg->type)) { | |
97e6d7da KKD |
5220 | if (meta && meta->raw_mode) { |
5221 | verbose(env, "R%d cannot write into %s\n", regno, | |
5222 | reg_type_str(env, reg->type)); | |
20b2aff4 | 5223 | return -EACCES; |
97e6d7da | 5224 | } |
20b2aff4 | 5225 | |
20b2aff4 HL |
5226 | max_access = &env->prog->aux->max_rdonly_access; |
5227 | } else { | |
20b2aff4 HL |
5228 | max_access = &env->prog->aux->max_rdwr_access; |
5229 | } | |
afbf21dc YS |
5230 | return check_buffer_access(env, reg, regno, reg->off, |
5231 | access_size, zero_size_allowed, | |
44e9a741 | 5232 | max_access); |
0d004c02 | 5233 | case PTR_TO_STACK: |
01f810ac AM |
5234 | return check_stack_range_initialized( |
5235 | env, | |
5236 | regno, reg->off, access_size, | |
5237 | zero_size_allowed, ACCESS_HELPER, meta); | |
15baa55f BT |
5238 | case PTR_TO_CTX: |
5239 | /* in case the function doesn't know how to access the context, | |
5240 | * (because we are in a program of type SYSCALL for example), we | |
5241 | * can not statically check its size. | |
5242 | * Dynamically check it now. | |
5243 | */ | |
5244 | if (!env->ops->convert_ctx_access) { | |
5245 | enum bpf_access_type atype = meta && meta->raw_mode ? BPF_WRITE : BPF_READ; | |
5246 | int offset = access_size - 1; | |
5247 | ||
5248 | /* Allow zero-byte read from PTR_TO_CTX */ | |
5249 | if (access_size == 0) | |
5250 | return zero_size_allowed ? 0 : -EACCES; | |
5251 | ||
5252 | return check_mem_access(env, env->insn_idx, regno, offset, BPF_B, | |
5253 | atype, -1, false); | |
5254 | } | |
5255 | ||
5256 | fallthrough; | |
0d004c02 LB |
5257 | default: /* scalar_value or invalid ptr */ |
5258 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
5259 | if (zero_size_allowed && access_size == 0 && | |
5260 | register_is_null(reg)) | |
5261 | return 0; | |
5262 | ||
c25b2ae1 HL |
5263 | verbose(env, "R%d type=%s ", regno, |
5264 | reg_type_str(env, reg->type)); | |
5265 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); | |
0d004c02 | 5266 | return -EACCES; |
06c1c049 GB |
5267 | } |
5268 | } | |
5269 | ||
d583691c KKD |
5270 | static int check_mem_size_reg(struct bpf_verifier_env *env, |
5271 | struct bpf_reg_state *reg, u32 regno, | |
5272 | bool zero_size_allowed, | |
5273 | struct bpf_call_arg_meta *meta) | |
5274 | { | |
5275 | int err; | |
5276 | ||
5277 | /* This is used to refine r0 return value bounds for helpers | |
5278 | * that enforce this value as an upper bound on return values. | |
5279 | * See do_refine_retval_range() for helpers that can refine | |
5280 | * the return value. C type of helper is u32 so we pull register | |
5281 | * bound from umax_value however, if negative verifier errors | |
5282 | * out. Only upper bounds can be learned because retval is an | |
5283 | * int type and negative retvals are allowed. | |
5284 | */ | |
be77354a | 5285 | meta->msize_max_value = reg->umax_value; |
d583691c KKD |
5286 | |
5287 | /* The register is SCALAR_VALUE; the access check | |
5288 | * happens using its boundaries. | |
5289 | */ | |
5290 | if (!tnum_is_const(reg->var_off)) | |
5291 | /* For unprivileged variable accesses, disable raw | |
5292 | * mode so that the program is required to | |
5293 | * initialize all the memory that the helper could | |
5294 | * just partially fill up. | |
5295 | */ | |
5296 | meta = NULL; | |
5297 | ||
5298 | if (reg->smin_value < 0) { | |
5299 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", | |
5300 | regno); | |
5301 | return -EACCES; | |
5302 | } | |
5303 | ||
5304 | if (reg->umin_value == 0) { | |
5305 | err = check_helper_mem_access(env, regno - 1, 0, | |
5306 | zero_size_allowed, | |
5307 | meta); | |
5308 | if (err) | |
5309 | return err; | |
5310 | } | |
5311 | ||
5312 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { | |
5313 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
5314 | regno); | |
5315 | return -EACCES; | |
5316 | } | |
5317 | err = check_helper_mem_access(env, regno - 1, | |
5318 | reg->umax_value, | |
5319 | zero_size_allowed, meta); | |
5320 | if (!err) | |
5321 | err = mark_chain_precision(env, regno); | |
5322 | return err; | |
5323 | } | |
5324 | ||
e5069b9c DB |
5325 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
5326 | u32 regno, u32 mem_size) | |
5327 | { | |
be77354a KKD |
5328 | bool may_be_null = type_may_be_null(reg->type); |
5329 | struct bpf_reg_state saved_reg; | |
5330 | struct bpf_call_arg_meta meta; | |
5331 | int err; | |
5332 | ||
e5069b9c DB |
5333 | if (register_is_null(reg)) |
5334 | return 0; | |
5335 | ||
be77354a KKD |
5336 | memset(&meta, 0, sizeof(meta)); |
5337 | /* Assuming that the register contains a value check if the memory | |
5338 | * access is safe. Temporarily save and restore the register's state as | |
5339 | * the conversion shouldn't be visible to a caller. | |
5340 | */ | |
5341 | if (may_be_null) { | |
5342 | saved_reg = *reg; | |
e5069b9c | 5343 | mark_ptr_not_null_reg(reg); |
e5069b9c DB |
5344 | } |
5345 | ||
be77354a KKD |
5346 | err = check_helper_mem_access(env, regno, mem_size, true, &meta); |
5347 | /* Check access for BPF_WRITE */ | |
5348 | meta.raw_mode = true; | |
5349 | err = err ?: check_helper_mem_access(env, regno, mem_size, true, &meta); | |
5350 | ||
5351 | if (may_be_null) | |
5352 | *reg = saved_reg; | |
5353 | ||
5354 | return err; | |
e5069b9c DB |
5355 | } |
5356 | ||
d583691c KKD |
5357 | int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
5358 | u32 regno) | |
5359 | { | |
5360 | struct bpf_reg_state *mem_reg = &cur_regs(env)[regno - 1]; | |
5361 | bool may_be_null = type_may_be_null(mem_reg->type); | |
5362 | struct bpf_reg_state saved_reg; | |
be77354a | 5363 | struct bpf_call_arg_meta meta; |
d583691c KKD |
5364 | int err; |
5365 | ||
5366 | WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); | |
5367 | ||
be77354a KKD |
5368 | memset(&meta, 0, sizeof(meta)); |
5369 | ||
d583691c KKD |
5370 | if (may_be_null) { |
5371 | saved_reg = *mem_reg; | |
5372 | mark_ptr_not_null_reg(mem_reg); | |
5373 | } | |
5374 | ||
be77354a KKD |
5375 | err = check_mem_size_reg(env, reg, regno, true, &meta); |
5376 | /* Check access for BPF_WRITE */ | |
5377 | meta.raw_mode = true; | |
5378 | err = err ?: check_mem_size_reg(env, reg, regno, true, &meta); | |
d583691c KKD |
5379 | |
5380 | if (may_be_null) | |
5381 | *mem_reg = saved_reg; | |
5382 | return err; | |
5383 | } | |
5384 | ||
d83525ca AS |
5385 | /* Implementation details: |
5386 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
5387 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
5388 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
5389 | * value_or_null->value transition, since the verifier only cares about | |
5390 | * the range of access to valid map value pointer and doesn't care about actual | |
5391 | * address of the map element. | |
5392 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
5393 | * reg->id > 0 after value_or_null->value transition. By doing so | |
5394 | * two bpf_map_lookups will be considered two different pointers that | |
5395 | * point to different bpf_spin_locks. | |
5396 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
5397 | * dead-locks. | |
5398 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
5399 | * reg_is_refcounted() logic. The verifier needs to remember only | |
5400 | * one spin_lock instead of array of acquired_refs. | |
5401 | * cur_state->active_spin_lock remembers which map value element got locked | |
5402 | * and clears it after bpf_spin_unlock. | |
5403 | */ | |
5404 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
5405 | bool is_lock) | |
5406 | { | |
5407 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5408 | struct bpf_verifier_state *cur = env->cur_state; | |
5409 | bool is_const = tnum_is_const(reg->var_off); | |
5410 | struct bpf_map *map = reg->map_ptr; | |
5411 | u64 val = reg->var_off.value; | |
5412 | ||
d83525ca AS |
5413 | if (!is_const) { |
5414 | verbose(env, | |
5415 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
5416 | regno); | |
5417 | return -EINVAL; | |
5418 | } | |
5419 | if (!map->btf) { | |
5420 | verbose(env, | |
5421 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
5422 | map->name); | |
5423 | return -EINVAL; | |
5424 | } | |
db559117 KKD |
5425 | if (!btf_record_has_field(map->record, BPF_SPIN_LOCK)) { |
5426 | verbose(env, "map '%s' has no valid bpf_spin_lock\n", map->name); | |
d83525ca AS |
5427 | return -EINVAL; |
5428 | } | |
db559117 KKD |
5429 | if (map->record->spin_lock_off != val + reg->off) { |
5430 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock' that is at %d\n", | |
5431 | val + reg->off, map->record->spin_lock_off); | |
d83525ca AS |
5432 | return -EINVAL; |
5433 | } | |
5434 | if (is_lock) { | |
5435 | if (cur->active_spin_lock) { | |
5436 | verbose(env, | |
5437 | "Locking two bpf_spin_locks are not allowed\n"); | |
5438 | return -EINVAL; | |
5439 | } | |
5440 | cur->active_spin_lock = reg->id; | |
5441 | } else { | |
5442 | if (!cur->active_spin_lock) { | |
5443 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
5444 | return -EINVAL; | |
5445 | } | |
5446 | if (cur->active_spin_lock != reg->id) { | |
5447 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
5448 | return -EINVAL; | |
5449 | } | |
5450 | cur->active_spin_lock = 0; | |
5451 | } | |
5452 | return 0; | |
5453 | } | |
5454 | ||
b00628b1 AS |
5455 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
5456 | struct bpf_call_arg_meta *meta) | |
5457 | { | |
5458 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5459 | bool is_const = tnum_is_const(reg->var_off); | |
5460 | struct bpf_map *map = reg->map_ptr; | |
5461 | u64 val = reg->var_off.value; | |
5462 | ||
5463 | if (!is_const) { | |
5464 | verbose(env, | |
5465 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
5466 | regno); | |
5467 | return -EINVAL; | |
5468 | } | |
5469 | if (!map->btf) { | |
5470 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
5471 | map->name); | |
5472 | return -EINVAL; | |
5473 | } | |
db559117 KKD |
5474 | if (!btf_record_has_field(map->record, BPF_TIMER)) { |
5475 | verbose(env, "map '%s' has no valid bpf_timer\n", map->name); | |
68134668 AS |
5476 | return -EINVAL; |
5477 | } | |
db559117 | 5478 | if (map->record->timer_off != val + reg->off) { |
68134668 | 5479 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
db559117 | 5480 | val + reg->off, map->record->timer_off); |
b00628b1 AS |
5481 | return -EINVAL; |
5482 | } | |
5483 | if (meta->map_ptr) { | |
5484 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
5485 | return -EFAULT; | |
5486 | } | |
3e8ce298 | 5487 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
5488 | meta->map_ptr = map; |
5489 | return 0; | |
5490 | } | |
5491 | ||
c0a5a21c KKD |
5492 | static int process_kptr_func(struct bpf_verifier_env *env, int regno, |
5493 | struct bpf_call_arg_meta *meta) | |
5494 | { | |
5495 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
c0a5a21c | 5496 | struct bpf_map *map_ptr = reg->map_ptr; |
aa3496ac | 5497 | struct btf_field *kptr_field; |
c0a5a21c | 5498 | u32 kptr_off; |
c0a5a21c KKD |
5499 | |
5500 | if (!tnum_is_const(reg->var_off)) { | |
5501 | verbose(env, | |
5502 | "R%d doesn't have constant offset. kptr has to be at the constant offset\n", | |
5503 | regno); | |
5504 | return -EINVAL; | |
5505 | } | |
5506 | if (!map_ptr->btf) { | |
5507 | verbose(env, "map '%s' has to have BTF in order to use bpf_kptr_xchg\n", | |
5508 | map_ptr->name); | |
5509 | return -EINVAL; | |
5510 | } | |
aa3496ac KKD |
5511 | if (!btf_record_has_field(map_ptr->record, BPF_KPTR)) { |
5512 | verbose(env, "map '%s' has no valid kptr\n", map_ptr->name); | |
c0a5a21c KKD |
5513 | return -EINVAL; |
5514 | } | |
5515 | ||
5516 | meta->map_ptr = map_ptr; | |
5517 | kptr_off = reg->off + reg->var_off.value; | |
aa3496ac KKD |
5518 | kptr_field = btf_record_find(map_ptr->record, kptr_off, BPF_KPTR); |
5519 | if (!kptr_field) { | |
c0a5a21c KKD |
5520 | verbose(env, "off=%d doesn't point to kptr\n", kptr_off); |
5521 | return -EACCES; | |
5522 | } | |
aa3496ac | 5523 | if (kptr_field->type != BPF_KPTR_REF) { |
c0a5a21c KKD |
5524 | verbose(env, "off=%d kptr isn't referenced kptr\n", kptr_off); |
5525 | return -EACCES; | |
5526 | } | |
aa3496ac | 5527 | meta->kptr_field = kptr_field; |
c0a5a21c KKD |
5528 | return 0; |
5529 | } | |
5530 | ||
90133415 DB |
5531 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
5532 | { | |
5533 | return type == ARG_CONST_SIZE || | |
5534 | type == ARG_CONST_SIZE_OR_ZERO; | |
5535 | } | |
5536 | ||
8f14852e KKD |
5537 | static bool arg_type_is_release(enum bpf_arg_type type) |
5538 | { | |
5539 | return type & OBJ_RELEASE; | |
5540 | } | |
5541 | ||
97e03f52 JK |
5542 | static bool arg_type_is_dynptr(enum bpf_arg_type type) |
5543 | { | |
5544 | return base_type(type) == ARG_PTR_TO_DYNPTR; | |
5545 | } | |
5546 | ||
57c3bb72 AI |
5547 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
5548 | { | |
5549 | if (type == ARG_PTR_TO_INT) | |
5550 | return sizeof(u32); | |
5551 | else if (type == ARG_PTR_TO_LONG) | |
5552 | return sizeof(u64); | |
5553 | ||
5554 | return -EINVAL; | |
5555 | } | |
5556 | ||
912f442c LB |
5557 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
5558 | const struct bpf_call_arg_meta *meta, | |
5559 | enum bpf_arg_type *arg_type) | |
5560 | { | |
5561 | if (!meta->map_ptr) { | |
5562 | /* kernel subsystem misconfigured verifier */ | |
5563 | verbose(env, "invalid map_ptr to access map->type\n"); | |
5564 | return -EACCES; | |
5565 | } | |
5566 | ||
5567 | switch (meta->map_ptr->map_type) { | |
5568 | case BPF_MAP_TYPE_SOCKMAP: | |
5569 | case BPF_MAP_TYPE_SOCKHASH: | |
5570 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 5571 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
5572 | } else { |
5573 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
5574 | return -EINVAL; | |
5575 | } | |
5576 | break; | |
9330986c JK |
5577 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5578 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
5579 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
5580 | break; | |
912f442c LB |
5581 | default: |
5582 | break; | |
5583 | } | |
5584 | return 0; | |
5585 | } | |
5586 | ||
f79e7ea5 LB |
5587 | struct bpf_reg_types { |
5588 | const enum bpf_reg_type types[10]; | |
1df8f55a | 5589 | u32 *btf_id; |
f79e7ea5 LB |
5590 | }; |
5591 | ||
f79e7ea5 LB |
5592 | static const struct bpf_reg_types sock_types = { |
5593 | .types = { | |
5594 | PTR_TO_SOCK_COMMON, | |
5595 | PTR_TO_SOCKET, | |
5596 | PTR_TO_TCP_SOCK, | |
5597 | PTR_TO_XDP_SOCK, | |
5598 | }, | |
5599 | }; | |
5600 | ||
49a2a4d4 | 5601 | #ifdef CONFIG_NET |
1df8f55a MKL |
5602 | static const struct bpf_reg_types btf_id_sock_common_types = { |
5603 | .types = { | |
5604 | PTR_TO_SOCK_COMMON, | |
5605 | PTR_TO_SOCKET, | |
5606 | PTR_TO_TCP_SOCK, | |
5607 | PTR_TO_XDP_SOCK, | |
5608 | PTR_TO_BTF_ID, | |
5609 | }, | |
5610 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
5611 | }; | |
49a2a4d4 | 5612 | #endif |
1df8f55a | 5613 | |
f79e7ea5 LB |
5614 | static const struct bpf_reg_types mem_types = { |
5615 | .types = { | |
5616 | PTR_TO_STACK, | |
5617 | PTR_TO_PACKET, | |
5618 | PTR_TO_PACKET_META, | |
69c087ba | 5619 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5620 | PTR_TO_MAP_VALUE, |
5621 | PTR_TO_MEM, | |
a672b2e3 | 5622 | PTR_TO_MEM | MEM_ALLOC, |
20b2aff4 | 5623 | PTR_TO_BUF, |
f79e7ea5 LB |
5624 | }, |
5625 | }; | |
5626 | ||
5627 | static const struct bpf_reg_types int_ptr_types = { | |
5628 | .types = { | |
5629 | PTR_TO_STACK, | |
5630 | PTR_TO_PACKET, | |
5631 | PTR_TO_PACKET_META, | |
69c087ba | 5632 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5633 | PTR_TO_MAP_VALUE, |
5634 | }, | |
5635 | }; | |
5636 | ||
5637 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; | |
5638 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
5639 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
a672b2e3 | 5640 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM | MEM_ALLOC } }; |
f79e7ea5 LB |
5641 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
5642 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
5643 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; | |
5844101a | 5644 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_BTF_ID | MEM_PERCPU } }; |
69c087ba YS |
5645 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
5646 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 5647 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 5648 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
c0a5a21c | 5649 | static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
20571567 DV |
5650 | static const struct bpf_reg_types dynptr_types = { |
5651 | .types = { | |
5652 | PTR_TO_STACK, | |
5653 | PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL, | |
5654 | } | |
5655 | }; | |
f79e7ea5 | 5656 | |
0789e13b | 5657 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
d1673304 DM |
5658 | [ARG_PTR_TO_MAP_KEY] = &mem_types, |
5659 | [ARG_PTR_TO_MAP_VALUE] = &mem_types, | |
f79e7ea5 LB |
5660 | [ARG_CONST_SIZE] = &scalar_types, |
5661 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
5662 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
5663 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
5664 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 5665 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 5666 | #ifdef CONFIG_NET |
1df8f55a | 5667 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 5668 | #endif |
f79e7ea5 | 5669 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
5670 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
5671 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
5672 | [ARG_PTR_TO_MEM] = &mem_types, | |
f79e7ea5 | 5673 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, |
f79e7ea5 LB |
5674 | [ARG_PTR_TO_INT] = &int_ptr_types, |
5675 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 5676 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 5677 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 5678 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 5679 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 5680 | [ARG_PTR_TO_TIMER] = &timer_types, |
c0a5a21c | 5681 | [ARG_PTR_TO_KPTR] = &kptr_types, |
20571567 | 5682 | [ARG_PTR_TO_DYNPTR] = &dynptr_types, |
f79e7ea5 LB |
5683 | }; |
5684 | ||
5685 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 | 5686 | enum bpf_arg_type arg_type, |
c0a5a21c KKD |
5687 | const u32 *arg_btf_id, |
5688 | struct bpf_call_arg_meta *meta) | |
f79e7ea5 LB |
5689 | { |
5690 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5691 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 5692 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
5693 | int i, j; |
5694 | ||
48946bd6 | 5695 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
5696 | if (!compatible) { |
5697 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
5698 | return -EFAULT; | |
5699 | } | |
5700 | ||
216e3cd2 HL |
5701 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
5702 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY | |
5703 | * | |
5704 | * Same for MAYBE_NULL: | |
5705 | * | |
5706 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, | |
5707 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL | |
5708 | * | |
5709 | * Therefore we fold these flags depending on the arg_type before comparison. | |
5710 | */ | |
5711 | if (arg_type & MEM_RDONLY) | |
5712 | type &= ~MEM_RDONLY; | |
5713 | if (arg_type & PTR_MAYBE_NULL) | |
5714 | type &= ~PTR_MAYBE_NULL; | |
5715 | ||
f79e7ea5 LB |
5716 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
5717 | expected = compatible->types[i]; | |
5718 | if (expected == NOT_INIT) | |
5719 | break; | |
5720 | ||
5721 | if (type == expected) | |
a968d5e2 | 5722 | goto found; |
f79e7ea5 LB |
5723 | } |
5724 | ||
216e3cd2 | 5725 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
f79e7ea5 | 5726 | for (j = 0; j + 1 < i; j++) |
c25b2ae1 HL |
5727 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
5728 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); | |
f79e7ea5 | 5729 | return -EACCES; |
a968d5e2 MKL |
5730 | |
5731 | found: | |
216e3cd2 | 5732 | if (reg->type == PTR_TO_BTF_ID) { |
2ab3b380 KKD |
5733 | /* For bpf_sk_release, it needs to match against first member |
5734 | * 'struct sock_common', hence make an exception for it. This | |
5735 | * allows bpf_sk_release to work for multiple socket types. | |
5736 | */ | |
5737 | bool strict_type_match = arg_type_is_release(arg_type) && | |
5738 | meta->func_id != BPF_FUNC_sk_release; | |
5739 | ||
1df8f55a MKL |
5740 | if (!arg_btf_id) { |
5741 | if (!compatible->btf_id) { | |
5742 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
5743 | return -EFAULT; | |
5744 | } | |
5745 | arg_btf_id = compatible->btf_id; | |
5746 | } | |
5747 | ||
c0a5a21c | 5748 | if (meta->func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac | 5749 | if (map_kptr_match_type(env, meta->kptr_field, reg, regno)) |
c0a5a21c | 5750 | return -EACCES; |
47e34cb7 DM |
5751 | } else { |
5752 | if (arg_btf_id == BPF_PTR_POISON) { | |
5753 | verbose(env, "verifier internal error:"); | |
5754 | verbose(env, "R%d has non-overwritten BPF_PTR_POISON type\n", | |
5755 | regno); | |
5756 | return -EACCES; | |
5757 | } | |
5758 | ||
5759 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
5760 | btf_vmlinux, *arg_btf_id, | |
5761 | strict_type_match)) { | |
5762 | verbose(env, "R%d is of type %s but %s is expected\n", | |
5763 | regno, kernel_type_name(reg->btf, reg->btf_id), | |
5764 | kernel_type_name(btf_vmlinux, *arg_btf_id)); | |
5765 | return -EACCES; | |
5766 | } | |
a968d5e2 | 5767 | } |
a968d5e2 MKL |
5768 | } |
5769 | ||
5770 | return 0; | |
f79e7ea5 LB |
5771 | } |
5772 | ||
25b35dd2 KKD |
5773 | int check_func_arg_reg_off(struct bpf_verifier_env *env, |
5774 | const struct bpf_reg_state *reg, int regno, | |
8f14852e | 5775 | enum bpf_arg_type arg_type) |
25b35dd2 KKD |
5776 | { |
5777 | enum bpf_reg_type type = reg->type; | |
8f14852e | 5778 | bool fixed_off_ok = false; |
25b35dd2 KKD |
5779 | |
5780 | switch ((u32)type) { | |
25b35dd2 | 5781 | /* Pointer types where reg offset is explicitly allowed: */ |
97e03f52 JK |
5782 | case PTR_TO_STACK: |
5783 | if (arg_type_is_dynptr(arg_type) && reg->off % BPF_REG_SIZE) { | |
5784 | verbose(env, "cannot pass in dynptr at an offset\n"); | |
5785 | return -EINVAL; | |
5786 | } | |
5787 | fallthrough; | |
25b35dd2 KKD |
5788 | case PTR_TO_PACKET: |
5789 | case PTR_TO_PACKET_META: | |
5790 | case PTR_TO_MAP_KEY: | |
5791 | case PTR_TO_MAP_VALUE: | |
5792 | case PTR_TO_MEM: | |
5793 | case PTR_TO_MEM | MEM_RDONLY: | |
5794 | case PTR_TO_MEM | MEM_ALLOC: | |
5795 | case PTR_TO_BUF: | |
5796 | case PTR_TO_BUF | MEM_RDONLY: | |
97e03f52 | 5797 | case SCALAR_VALUE: |
25b35dd2 KKD |
5798 | /* Some of the argument types nevertheless require a |
5799 | * zero register offset. | |
5800 | */ | |
8f14852e | 5801 | if (base_type(arg_type) != ARG_PTR_TO_ALLOC_MEM) |
25b35dd2 KKD |
5802 | return 0; |
5803 | break; | |
5804 | /* All the rest must be rejected, except PTR_TO_BTF_ID which allows | |
5805 | * fixed offset. | |
5806 | */ | |
5807 | case PTR_TO_BTF_ID: | |
24d5bb80 | 5808 | /* When referenced PTR_TO_BTF_ID is passed to release function, |
8f14852e KKD |
5809 | * it's fixed offset must be 0. In the other cases, fixed offset |
5810 | * can be non-zero. | |
24d5bb80 | 5811 | */ |
8f14852e | 5812 | if (arg_type_is_release(arg_type) && reg->off) { |
24d5bb80 KKD |
5813 | verbose(env, "R%d must have zero offset when passed to release func\n", |
5814 | regno); | |
5815 | return -EINVAL; | |
5816 | } | |
8f14852e KKD |
5817 | /* For arg is release pointer, fixed_off_ok must be false, but |
5818 | * we already checked and rejected reg->off != 0 above, so set | |
5819 | * to true to allow fixed offset for all other cases. | |
24d5bb80 | 5820 | */ |
25b35dd2 KKD |
5821 | fixed_off_ok = true; |
5822 | break; | |
5823 | default: | |
5824 | break; | |
5825 | } | |
5826 | return __check_ptr_off_reg(env, reg, regno, fixed_off_ok); | |
5827 | } | |
5828 | ||
34d4ef57 JK |
5829 | static u32 stack_slot_get_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
5830 | { | |
5831 | struct bpf_func_state *state = func(env, reg); | |
5832 | int spi = get_spi(reg->off); | |
5833 | ||
5834 | return state->stack[spi].spilled_ptr.id; | |
5835 | } | |
5836 | ||
af7ec138 YS |
5837 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
5838 | struct bpf_call_arg_meta *meta, | |
5839 | const struct bpf_func_proto *fn) | |
17a52670 | 5840 | { |
af7ec138 | 5841 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 5842 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 5843 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 5844 | enum bpf_reg_type type = reg->type; |
508362ac | 5845 | u32 *arg_btf_id = NULL; |
17a52670 AS |
5846 | int err = 0; |
5847 | ||
80f1d68c | 5848 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
5849 | return 0; |
5850 | ||
dc503a8a EC |
5851 | err = check_reg_arg(env, regno, SRC_OP); |
5852 | if (err) | |
5853 | return err; | |
17a52670 | 5854 | |
1be7f75d AS |
5855 | if (arg_type == ARG_ANYTHING) { |
5856 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
5857 | verbose(env, "R%d leaks addr into helper function\n", |
5858 | regno); | |
1be7f75d AS |
5859 | return -EACCES; |
5860 | } | |
80f1d68c | 5861 | return 0; |
1be7f75d | 5862 | } |
80f1d68c | 5863 | |
de8f3a83 | 5864 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 5865 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 5866 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
5867 | return -EACCES; |
5868 | } | |
5869 | ||
16d1e00c | 5870 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE) { |
912f442c LB |
5871 | err = resolve_map_arg_type(env, meta, &arg_type); |
5872 | if (err) | |
5873 | return err; | |
5874 | } | |
5875 | ||
48946bd6 | 5876 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
5877 | /* A NULL register has a SCALAR_VALUE type, so skip |
5878 | * type checking. | |
5879 | */ | |
5880 | goto skip_type_check; | |
5881 | ||
508362ac MM |
5882 | /* arg_btf_id and arg_size are in a union. */ |
5883 | if (base_type(arg_type) == ARG_PTR_TO_BTF_ID) | |
5884 | arg_btf_id = fn->arg_btf_id[arg]; | |
5885 | ||
5886 | err = check_reg_type(env, regno, arg_type, arg_btf_id, meta); | |
f79e7ea5 LB |
5887 | if (err) |
5888 | return err; | |
5889 | ||
8f14852e | 5890 | err = check_func_arg_reg_off(env, reg, regno, arg_type); |
25b35dd2 KKD |
5891 | if (err) |
5892 | return err; | |
d7b9454a | 5893 | |
fd1b0d60 | 5894 | skip_type_check: |
8f14852e | 5895 | if (arg_type_is_release(arg_type)) { |
bc34dee6 JK |
5896 | if (arg_type_is_dynptr(arg_type)) { |
5897 | struct bpf_func_state *state = func(env, reg); | |
5898 | int spi = get_spi(reg->off); | |
5899 | ||
5900 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS) || | |
5901 | !state->stack[spi].spilled_ptr.id) { | |
5902 | verbose(env, "arg %d is an unacquired reference\n", regno); | |
5903 | return -EINVAL; | |
5904 | } | |
5905 | } else if (!reg->ref_obj_id && !register_is_null(reg)) { | |
8f14852e KKD |
5906 | verbose(env, "R%d must be referenced when passed to release function\n", |
5907 | regno); | |
5908 | return -EINVAL; | |
5909 | } | |
5910 | if (meta->release_regno) { | |
5911 | verbose(env, "verifier internal error: more than one release argument\n"); | |
5912 | return -EFAULT; | |
5913 | } | |
5914 | meta->release_regno = regno; | |
5915 | } | |
5916 | ||
02f7c958 | 5917 | if (reg->ref_obj_id) { |
457f4436 AN |
5918 | if (meta->ref_obj_id) { |
5919 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
5920 | regno, reg->ref_obj_id, | |
5921 | meta->ref_obj_id); | |
5922 | return -EFAULT; | |
5923 | } | |
5924 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
5925 | } |
5926 | ||
8ab4cdcf JK |
5927 | switch (base_type(arg_type)) { |
5928 | case ARG_CONST_MAP_PTR: | |
17a52670 | 5929 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
3e8ce298 AS |
5930 | if (meta->map_ptr) { |
5931 | /* Use map_uid (which is unique id of inner map) to reject: | |
5932 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
5933 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
5934 | * if (inner_map1 && inner_map2) { | |
5935 | * timer = bpf_map_lookup_elem(inner_map1); | |
5936 | * if (timer) | |
5937 | * // mismatch would have been allowed | |
5938 | * bpf_timer_init(timer, inner_map2); | |
5939 | * } | |
5940 | * | |
5941 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
5942 | */ | |
5943 | if (meta->map_ptr != reg->map_ptr || | |
5944 | meta->map_uid != reg->map_uid) { | |
5945 | verbose(env, | |
5946 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
5947 | meta->map_uid, reg->map_uid); | |
5948 | return -EINVAL; | |
5949 | } | |
b00628b1 | 5950 | } |
33ff9823 | 5951 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 5952 | meta->map_uid = reg->map_uid; |
8ab4cdcf JK |
5953 | break; |
5954 | case ARG_PTR_TO_MAP_KEY: | |
17a52670 AS |
5955 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
5956 | * check that [key, key + map->key_size) are within | |
5957 | * stack limits and initialized | |
5958 | */ | |
33ff9823 | 5959 | if (!meta->map_ptr) { |
17a52670 AS |
5960 | /* in function declaration map_ptr must come before |
5961 | * map_key, so that it's verified and known before | |
5962 | * we have to check map_key here. Otherwise it means | |
5963 | * that kernel subsystem misconfigured verifier | |
5964 | */ | |
61bd5218 | 5965 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
5966 | return -EACCES; |
5967 | } | |
d71962f3 PC |
5968 | err = check_helper_mem_access(env, regno, |
5969 | meta->map_ptr->key_size, false, | |
5970 | NULL); | |
8ab4cdcf JK |
5971 | break; |
5972 | case ARG_PTR_TO_MAP_VALUE: | |
48946bd6 HL |
5973 | if (type_may_be_null(arg_type) && register_is_null(reg)) |
5974 | return 0; | |
5975 | ||
17a52670 AS |
5976 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
5977 | * check [value, value + map->value_size) validity | |
5978 | */ | |
33ff9823 | 5979 | if (!meta->map_ptr) { |
17a52670 | 5980 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 5981 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
5982 | return -EACCES; |
5983 | } | |
16d1e00c | 5984 | meta->raw_mode = arg_type & MEM_UNINIT; |
d71962f3 PC |
5985 | err = check_helper_mem_access(env, regno, |
5986 | meta->map_ptr->value_size, false, | |
2ea864c5 | 5987 | meta); |
8ab4cdcf JK |
5988 | break; |
5989 | case ARG_PTR_TO_PERCPU_BTF_ID: | |
eaa6bcb7 HL |
5990 | if (!reg->btf_id) { |
5991 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
5992 | return -EACCES; | |
5993 | } | |
22dc4a0f | 5994 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 5995 | meta->ret_btf_id = reg->btf_id; |
8ab4cdcf JK |
5996 | break; |
5997 | case ARG_PTR_TO_SPIN_LOCK: | |
c18f0b6a LB |
5998 | if (meta->func_id == BPF_FUNC_spin_lock) { |
5999 | if (process_spin_lock(env, regno, true)) | |
6000 | return -EACCES; | |
6001 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
6002 | if (process_spin_lock(env, regno, false)) | |
6003 | return -EACCES; | |
6004 | } else { | |
6005 | verbose(env, "verifier internal error\n"); | |
6006 | return -EFAULT; | |
6007 | } | |
8ab4cdcf JK |
6008 | break; |
6009 | case ARG_PTR_TO_TIMER: | |
b00628b1 AS |
6010 | if (process_timer_func(env, regno, meta)) |
6011 | return -EACCES; | |
8ab4cdcf JK |
6012 | break; |
6013 | case ARG_PTR_TO_FUNC: | |
69c087ba | 6014 | meta->subprogno = reg->subprogno; |
8ab4cdcf JK |
6015 | break; |
6016 | case ARG_PTR_TO_MEM: | |
a2bbe7cc LB |
6017 | /* The access to this pointer is only checked when we hit the |
6018 | * next is_mem_size argument below. | |
6019 | */ | |
16d1e00c | 6020 | meta->raw_mode = arg_type & MEM_UNINIT; |
508362ac MM |
6021 | if (arg_type & MEM_FIXED_SIZE) { |
6022 | err = check_helper_mem_access(env, regno, | |
6023 | fn->arg_size[arg], false, | |
6024 | meta); | |
6025 | } | |
8ab4cdcf JK |
6026 | break; |
6027 | case ARG_CONST_SIZE: | |
6028 | err = check_mem_size_reg(env, reg, regno, false, meta); | |
6029 | break; | |
6030 | case ARG_CONST_SIZE_OR_ZERO: | |
6031 | err = check_mem_size_reg(env, reg, regno, true, meta); | |
6032 | break; | |
6033 | case ARG_PTR_TO_DYNPTR: | |
20571567 DV |
6034 | /* We only need to check for initialized / uninitialized helper |
6035 | * dynptr args if the dynptr is not PTR_TO_DYNPTR, as the | |
6036 | * assumption is that if it is, that a helper function | |
6037 | * initialized the dynptr on behalf of the BPF program. | |
6038 | */ | |
6039 | if (base_type(reg->type) == PTR_TO_DYNPTR) | |
6040 | break; | |
97e03f52 JK |
6041 | if (arg_type & MEM_UNINIT) { |
6042 | if (!is_dynptr_reg_valid_uninit(env, reg)) { | |
6043 | verbose(env, "Dynptr has to be an uninitialized dynptr\n"); | |
6044 | return -EINVAL; | |
6045 | } | |
6046 | ||
6047 | /* We only support one dynptr being uninitialized at the moment, | |
6048 | * which is sufficient for the helper functions we have right now. | |
6049 | */ | |
6050 | if (meta->uninit_dynptr_regno) { | |
6051 | verbose(env, "verifier internal error: multiple uninitialized dynptr args\n"); | |
6052 | return -EFAULT; | |
6053 | } | |
6054 | ||
6055 | meta->uninit_dynptr_regno = regno; | |
e9e315b4 RS |
6056 | } else if (!is_dynptr_reg_valid_init(env, reg)) { |
6057 | verbose(env, | |
6058 | "Expected an initialized dynptr as arg #%d\n", | |
6059 | arg + 1); | |
6060 | return -EINVAL; | |
6061 | } else if (!is_dynptr_type_expected(env, reg, arg_type)) { | |
97e03f52 JK |
6062 | const char *err_extra = ""; |
6063 | ||
6064 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
6065 | case DYNPTR_TYPE_LOCAL: | |
e9e315b4 | 6066 | err_extra = "local"; |
97e03f52 | 6067 | break; |
bc34dee6 | 6068 | case DYNPTR_TYPE_RINGBUF: |
e9e315b4 | 6069 | err_extra = "ringbuf"; |
bc34dee6 | 6070 | break; |
97e03f52 | 6071 | default: |
e9e315b4 | 6072 | err_extra = "<unknown>"; |
97e03f52 JK |
6073 | break; |
6074 | } | |
e9e315b4 RS |
6075 | verbose(env, |
6076 | "Expected a dynptr of type %s as arg #%d\n", | |
97e03f52 JK |
6077 | err_extra, arg + 1); |
6078 | return -EINVAL; | |
6079 | } | |
8ab4cdcf JK |
6080 | break; |
6081 | case ARG_CONST_ALLOC_SIZE_OR_ZERO: | |
457f4436 | 6082 | if (!tnum_is_const(reg->var_off)) { |
28a8add6 | 6083 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
6084 | regno); |
6085 | return -EACCES; | |
6086 | } | |
6087 | meta->mem_size = reg->var_off.value; | |
2fc31465 KKD |
6088 | err = mark_chain_precision(env, regno); |
6089 | if (err) | |
6090 | return err; | |
8ab4cdcf JK |
6091 | break; |
6092 | case ARG_PTR_TO_INT: | |
6093 | case ARG_PTR_TO_LONG: | |
6094 | { | |
57c3bb72 AI |
6095 | int size = int_ptr_type_to_size(arg_type); |
6096 | ||
6097 | err = check_helper_mem_access(env, regno, size, false, meta); | |
6098 | if (err) | |
6099 | return err; | |
6100 | err = check_ptr_alignment(env, reg, 0, size, true); | |
8ab4cdcf JK |
6101 | break; |
6102 | } | |
6103 | case ARG_PTR_TO_CONST_STR: | |
6104 | { | |
fff13c4b FR |
6105 | struct bpf_map *map = reg->map_ptr; |
6106 | int map_off; | |
6107 | u64 map_addr; | |
6108 | char *str_ptr; | |
6109 | ||
a8fad73e | 6110 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
6111 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
6112 | return -EACCES; | |
6113 | } | |
6114 | ||
6115 | if (!tnum_is_const(reg->var_off)) { | |
6116 | verbose(env, "R%d is not a constant address'\n", regno); | |
6117 | return -EACCES; | |
6118 | } | |
6119 | ||
6120 | if (!map->ops->map_direct_value_addr) { | |
6121 | verbose(env, "no direct value access support for this map type\n"); | |
6122 | return -EACCES; | |
6123 | } | |
6124 | ||
6125 | err = check_map_access(env, regno, reg->off, | |
61df10c7 KKD |
6126 | map->value_size - reg->off, false, |
6127 | ACCESS_HELPER); | |
fff13c4b FR |
6128 | if (err) |
6129 | return err; | |
6130 | ||
6131 | map_off = reg->off + reg->var_off.value; | |
6132 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
6133 | if (err) { | |
6134 | verbose(env, "direct value access on string failed\n"); | |
6135 | return err; | |
6136 | } | |
6137 | ||
6138 | str_ptr = (char *)(long)(map_addr); | |
6139 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
6140 | verbose(env, "string is not zero-terminated\n"); | |
6141 | return -EINVAL; | |
6142 | } | |
8ab4cdcf JK |
6143 | break; |
6144 | } | |
6145 | case ARG_PTR_TO_KPTR: | |
c0a5a21c KKD |
6146 | if (process_kptr_func(env, regno, meta)) |
6147 | return -EACCES; | |
8ab4cdcf | 6148 | break; |
17a52670 AS |
6149 | } |
6150 | ||
6151 | return err; | |
6152 | } | |
6153 | ||
0126240f LB |
6154 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
6155 | { | |
6156 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 6157 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
6158 | |
6159 | if (func_id != BPF_FUNC_map_update_elem) | |
6160 | return false; | |
6161 | ||
6162 | /* It's not possible to get access to a locked struct sock in these | |
6163 | * contexts, so updating is safe. | |
6164 | */ | |
6165 | switch (type) { | |
6166 | case BPF_PROG_TYPE_TRACING: | |
6167 | if (eatype == BPF_TRACE_ITER) | |
6168 | return true; | |
6169 | break; | |
6170 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
6171 | case BPF_PROG_TYPE_SCHED_CLS: | |
6172 | case BPF_PROG_TYPE_SCHED_ACT: | |
6173 | case BPF_PROG_TYPE_XDP: | |
6174 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
6175 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
6176 | case BPF_PROG_TYPE_SK_LOOKUP: | |
6177 | return true; | |
6178 | default: | |
6179 | break; | |
6180 | } | |
6181 | ||
6182 | verbose(env, "cannot update sockmap in this context\n"); | |
6183 | return false; | |
6184 | } | |
6185 | ||
e411901c MF |
6186 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
6187 | { | |
95acd881 TA |
6188 | return env->prog->jit_requested && |
6189 | bpf_jit_supports_subprog_tailcalls(); | |
e411901c MF |
6190 | } |
6191 | ||
61bd5218 JK |
6192 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
6193 | struct bpf_map *map, int func_id) | |
35578d79 | 6194 | { |
35578d79 KX |
6195 | if (!map) |
6196 | return 0; | |
6197 | ||
6aff67c8 AS |
6198 | /* We need a two way check, first is from map perspective ... */ |
6199 | switch (map->map_type) { | |
6200 | case BPF_MAP_TYPE_PROG_ARRAY: | |
6201 | if (func_id != BPF_FUNC_tail_call) | |
6202 | goto error; | |
6203 | break; | |
6204 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
6205 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 6206 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 6207 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
6208 | func_id != BPF_FUNC_perf_event_read_value && |
6209 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
6210 | goto error; |
6211 | break; | |
457f4436 AN |
6212 | case BPF_MAP_TYPE_RINGBUF: |
6213 | if (func_id != BPF_FUNC_ringbuf_output && | |
6214 | func_id != BPF_FUNC_ringbuf_reserve && | |
bc34dee6 JK |
6215 | func_id != BPF_FUNC_ringbuf_query && |
6216 | func_id != BPF_FUNC_ringbuf_reserve_dynptr && | |
6217 | func_id != BPF_FUNC_ringbuf_submit_dynptr && | |
6218 | func_id != BPF_FUNC_ringbuf_discard_dynptr) | |
457f4436 AN |
6219 | goto error; |
6220 | break; | |
583c1f42 | 6221 | case BPF_MAP_TYPE_USER_RINGBUF: |
20571567 DV |
6222 | if (func_id != BPF_FUNC_user_ringbuf_drain) |
6223 | goto error; | |
6224 | break; | |
6aff67c8 AS |
6225 | case BPF_MAP_TYPE_STACK_TRACE: |
6226 | if (func_id != BPF_FUNC_get_stackid) | |
6227 | goto error; | |
6228 | break; | |
4ed8ec52 | 6229 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 6230 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 6231 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
6232 | goto error; |
6233 | break; | |
cd339431 | 6234 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 6235 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
6236 | if (func_id != BPF_FUNC_get_local_storage) |
6237 | goto error; | |
6238 | break; | |
546ac1ff | 6239 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 6240 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
6241 | if (func_id != BPF_FUNC_redirect_map && |
6242 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
6243 | goto error; |
6244 | break; | |
fbfc504a BT |
6245 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
6246 | * appear. | |
6247 | */ | |
6710e112 JDB |
6248 | case BPF_MAP_TYPE_CPUMAP: |
6249 | if (func_id != BPF_FUNC_redirect_map) | |
6250 | goto error; | |
6251 | break; | |
fada7fdc JL |
6252 | case BPF_MAP_TYPE_XSKMAP: |
6253 | if (func_id != BPF_FUNC_redirect_map && | |
6254 | func_id != BPF_FUNC_map_lookup_elem) | |
6255 | goto error; | |
6256 | break; | |
56f668df | 6257 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 6258 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
6259 | if (func_id != BPF_FUNC_map_lookup_elem) |
6260 | goto error; | |
16a43625 | 6261 | break; |
174a79ff JF |
6262 | case BPF_MAP_TYPE_SOCKMAP: |
6263 | if (func_id != BPF_FUNC_sk_redirect_map && | |
6264 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 6265 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 6266 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 6267 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
6268 | func_id != BPF_FUNC_map_lookup_elem && |
6269 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
6270 | goto error; |
6271 | break; | |
81110384 JF |
6272 | case BPF_MAP_TYPE_SOCKHASH: |
6273 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
6274 | func_id != BPF_FUNC_sock_hash_update && | |
6275 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 6276 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 6277 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
6278 | func_id != BPF_FUNC_map_lookup_elem && |
6279 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
6280 | goto error; |
6281 | break; | |
2dbb9b9e MKL |
6282 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
6283 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
6284 | goto error; | |
6285 | break; | |
f1a2e44a MV |
6286 | case BPF_MAP_TYPE_QUEUE: |
6287 | case BPF_MAP_TYPE_STACK: | |
6288 | if (func_id != BPF_FUNC_map_peek_elem && | |
6289 | func_id != BPF_FUNC_map_pop_elem && | |
6290 | func_id != BPF_FUNC_map_push_elem) | |
6291 | goto error; | |
6292 | break; | |
6ac99e8f MKL |
6293 | case BPF_MAP_TYPE_SK_STORAGE: |
6294 | if (func_id != BPF_FUNC_sk_storage_get && | |
6295 | func_id != BPF_FUNC_sk_storage_delete) | |
6296 | goto error; | |
6297 | break; | |
8ea63684 KS |
6298 | case BPF_MAP_TYPE_INODE_STORAGE: |
6299 | if (func_id != BPF_FUNC_inode_storage_get && | |
6300 | func_id != BPF_FUNC_inode_storage_delete) | |
6301 | goto error; | |
6302 | break; | |
4cf1bc1f KS |
6303 | case BPF_MAP_TYPE_TASK_STORAGE: |
6304 | if (func_id != BPF_FUNC_task_storage_get && | |
6305 | func_id != BPF_FUNC_task_storage_delete) | |
6306 | goto error; | |
6307 | break; | |
c4bcfb38 YS |
6308 | case BPF_MAP_TYPE_CGRP_STORAGE: |
6309 | if (func_id != BPF_FUNC_cgrp_storage_get && | |
6310 | func_id != BPF_FUNC_cgrp_storage_delete) | |
6311 | goto error; | |
6312 | break; | |
9330986c JK |
6313 | case BPF_MAP_TYPE_BLOOM_FILTER: |
6314 | if (func_id != BPF_FUNC_map_peek_elem && | |
6315 | func_id != BPF_FUNC_map_push_elem) | |
6316 | goto error; | |
6317 | break; | |
6aff67c8 AS |
6318 | default: |
6319 | break; | |
6320 | } | |
6321 | ||
6322 | /* ... and second from the function itself. */ | |
6323 | switch (func_id) { | |
6324 | case BPF_FUNC_tail_call: | |
6325 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
6326 | goto error; | |
e411901c MF |
6327 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
6328 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
6329 | return -EINVAL; |
6330 | } | |
6aff67c8 AS |
6331 | break; |
6332 | case BPF_FUNC_perf_event_read: | |
6333 | case BPF_FUNC_perf_event_output: | |
908432ca | 6334 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 6335 | case BPF_FUNC_skb_output: |
d831ee84 | 6336 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
6337 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
6338 | goto error; | |
6339 | break; | |
5b029a32 DB |
6340 | case BPF_FUNC_ringbuf_output: |
6341 | case BPF_FUNC_ringbuf_reserve: | |
6342 | case BPF_FUNC_ringbuf_query: | |
bc34dee6 JK |
6343 | case BPF_FUNC_ringbuf_reserve_dynptr: |
6344 | case BPF_FUNC_ringbuf_submit_dynptr: | |
6345 | case BPF_FUNC_ringbuf_discard_dynptr: | |
5b029a32 DB |
6346 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
6347 | goto error; | |
6348 | break; | |
20571567 DV |
6349 | case BPF_FUNC_user_ringbuf_drain: |
6350 | if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF) | |
6351 | goto error; | |
6352 | break; | |
6aff67c8 AS |
6353 | case BPF_FUNC_get_stackid: |
6354 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
6355 | goto error; | |
6356 | break; | |
60d20f91 | 6357 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 6358 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
6359 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
6360 | goto error; | |
6361 | break; | |
97f91a7c | 6362 | case BPF_FUNC_redirect_map: |
9c270af3 | 6363 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 6364 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
6365 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
6366 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
6367 | goto error; |
6368 | break; | |
174a79ff | 6369 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 6370 | case BPF_FUNC_msg_redirect_map: |
81110384 | 6371 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
6372 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
6373 | goto error; | |
6374 | break; | |
81110384 JF |
6375 | case BPF_FUNC_sk_redirect_hash: |
6376 | case BPF_FUNC_msg_redirect_hash: | |
6377 | case BPF_FUNC_sock_hash_update: | |
6378 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
6379 | goto error; |
6380 | break; | |
cd339431 | 6381 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
6382 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
6383 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
6384 | goto error; |
6385 | break; | |
2dbb9b9e | 6386 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
6387 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
6388 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
6389 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
6390 | goto error; |
6391 | break; | |
f1a2e44a | 6392 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
6393 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
6394 | map->map_type != BPF_MAP_TYPE_STACK) | |
6395 | goto error; | |
6396 | break; | |
9330986c JK |
6397 | case BPF_FUNC_map_peek_elem: |
6398 | case BPF_FUNC_map_push_elem: | |
6399 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
6400 | map->map_type != BPF_MAP_TYPE_STACK && | |
6401 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
6402 | goto error; | |
6403 | break; | |
07343110 FZ |
6404 | case BPF_FUNC_map_lookup_percpu_elem: |
6405 | if (map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && | |
6406 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && | |
6407 | map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH) | |
6408 | goto error; | |
6409 | break; | |
6ac99e8f MKL |
6410 | case BPF_FUNC_sk_storage_get: |
6411 | case BPF_FUNC_sk_storage_delete: | |
6412 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
6413 | goto error; | |
6414 | break; | |
8ea63684 KS |
6415 | case BPF_FUNC_inode_storage_get: |
6416 | case BPF_FUNC_inode_storage_delete: | |
6417 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
6418 | goto error; | |
6419 | break; | |
4cf1bc1f KS |
6420 | case BPF_FUNC_task_storage_get: |
6421 | case BPF_FUNC_task_storage_delete: | |
6422 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
6423 | goto error; | |
6424 | break; | |
c4bcfb38 YS |
6425 | case BPF_FUNC_cgrp_storage_get: |
6426 | case BPF_FUNC_cgrp_storage_delete: | |
6427 | if (map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) | |
6428 | goto error; | |
6429 | break; | |
6aff67c8 AS |
6430 | default: |
6431 | break; | |
35578d79 KX |
6432 | } |
6433 | ||
6434 | return 0; | |
6aff67c8 | 6435 | error: |
61bd5218 | 6436 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 6437 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 6438 | return -EINVAL; |
35578d79 KX |
6439 | } |
6440 | ||
90133415 | 6441 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
6442 | { |
6443 | int count = 0; | |
6444 | ||
39f19ebb | 6445 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6446 | count++; |
39f19ebb | 6447 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6448 | count++; |
39f19ebb | 6449 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6450 | count++; |
39f19ebb | 6451 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6452 | count++; |
39f19ebb | 6453 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
6454 | count++; |
6455 | ||
90133415 DB |
6456 | /* We only support one arg being in raw mode at the moment, |
6457 | * which is sufficient for the helper functions we have | |
6458 | * right now. | |
6459 | */ | |
6460 | return count <= 1; | |
6461 | } | |
6462 | ||
508362ac | 6463 | static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg) |
90133415 | 6464 | { |
508362ac MM |
6465 | bool is_fixed = fn->arg_type[arg] & MEM_FIXED_SIZE; |
6466 | bool has_size = fn->arg_size[arg] != 0; | |
6467 | bool is_next_size = false; | |
6468 | ||
6469 | if (arg + 1 < ARRAY_SIZE(fn->arg_type)) | |
6470 | is_next_size = arg_type_is_mem_size(fn->arg_type[arg + 1]); | |
6471 | ||
6472 | if (base_type(fn->arg_type[arg]) != ARG_PTR_TO_MEM) | |
6473 | return is_next_size; | |
6474 | ||
6475 | return has_size == is_next_size || is_next_size == is_fixed; | |
90133415 DB |
6476 | } |
6477 | ||
6478 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
6479 | { | |
6480 | /* bpf_xxx(..., buf, len) call will access 'len' | |
6481 | * bytes from memory 'buf'. Both arg types need | |
6482 | * to be paired, so make sure there's no buggy | |
6483 | * helper function specification. | |
6484 | */ | |
6485 | if (arg_type_is_mem_size(fn->arg1_type) || | |
508362ac MM |
6486 | check_args_pair_invalid(fn, 0) || |
6487 | check_args_pair_invalid(fn, 1) || | |
6488 | check_args_pair_invalid(fn, 2) || | |
6489 | check_args_pair_invalid(fn, 3) || | |
6490 | check_args_pair_invalid(fn, 4)) | |
90133415 DB |
6491 | return false; |
6492 | ||
6493 | return true; | |
6494 | } | |
6495 | ||
9436ef6e LB |
6496 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
6497 | { | |
6498 | int i; | |
6499 | ||
1df8f55a | 6500 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
c0a5a21c | 6501 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
9436ef6e LB |
6502 | return false; |
6503 | ||
508362ac MM |
6504 | if (base_type(fn->arg_type[i]) != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i] && |
6505 | /* arg_btf_id and arg_size are in a union. */ | |
6506 | (base_type(fn->arg_type[i]) != ARG_PTR_TO_MEM || | |
6507 | !(fn->arg_type[i] & MEM_FIXED_SIZE))) | |
1df8f55a MKL |
6508 | return false; |
6509 | } | |
6510 | ||
9436ef6e LB |
6511 | return true; |
6512 | } | |
6513 | ||
0c9a7a7e | 6514 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
6515 | { |
6516 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 6517 | check_arg_pair_ok(fn) && |
b2d8ef19 | 6518 | check_btf_id_ok(fn) ? 0 : -EINVAL; |
435faee1 DB |
6519 | } |
6520 | ||
de8f3a83 DB |
6521 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
6522 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 6523 | */ |
b239da34 | 6524 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 6525 | { |
b239da34 KKD |
6526 | struct bpf_func_state *state; |
6527 | struct bpf_reg_state *reg; | |
969bf05e | 6528 | |
b239da34 | 6529 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
de8f3a83 | 6530 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 6531 | __mark_reg_unknown(env, reg); |
b239da34 | 6532 | })); |
f4d7e40a AS |
6533 | } |
6534 | ||
6d94e741 AS |
6535 | enum { |
6536 | AT_PKT_END = -1, | |
6537 | BEYOND_PKT_END = -2, | |
6538 | }; | |
6539 | ||
6540 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
6541 | { | |
6542 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
6543 | struct bpf_reg_state *reg = &state->regs[regn]; | |
6544 | ||
6545 | if (reg->type != PTR_TO_PACKET) | |
6546 | /* PTR_TO_PACKET_META is not supported yet */ | |
6547 | return; | |
6548 | ||
6549 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
6550 | * How far beyond pkt_end it goes is unknown. | |
6551 | * if (!range_open) it's the case of pkt >= pkt_end | |
6552 | * if (range_open) it's the case of pkt > pkt_end | |
6553 | * hence this pointer is at least 1 byte bigger than pkt_end | |
6554 | */ | |
6555 | if (range_open) | |
6556 | reg->range = BEYOND_PKT_END; | |
6557 | else | |
6558 | reg->range = AT_PKT_END; | |
6559 | } | |
6560 | ||
fd978bf7 JS |
6561 | /* The pointer with the specified id has released its reference to kernel |
6562 | * resources. Identify all copies of the same pointer and clear the reference. | |
6563 | */ | |
6564 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 6565 | int ref_obj_id) |
fd978bf7 | 6566 | { |
b239da34 KKD |
6567 | struct bpf_func_state *state; |
6568 | struct bpf_reg_state *reg; | |
1b986589 | 6569 | int err; |
fd978bf7 | 6570 | |
1b986589 MKL |
6571 | err = release_reference_state(cur_func(env), ref_obj_id); |
6572 | if (err) | |
6573 | return err; | |
6574 | ||
b239da34 KKD |
6575 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
6576 | if (reg->ref_obj_id == ref_obj_id) | |
6577 | __mark_reg_unknown(env, reg); | |
6578 | })); | |
fd978bf7 | 6579 | |
1b986589 | 6580 | return 0; |
fd978bf7 JS |
6581 | } |
6582 | ||
51c39bb1 AS |
6583 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
6584 | struct bpf_reg_state *regs) | |
6585 | { | |
6586 | int i; | |
6587 | ||
6588 | /* after the call registers r0 - r5 were scratched */ | |
6589 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
6590 | mark_reg_not_init(env, regs, caller_saved[i]); | |
6591 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
6592 | } | |
6593 | } | |
6594 | ||
14351375 YS |
6595 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
6596 | struct bpf_func_state *caller, | |
6597 | struct bpf_func_state *callee, | |
6598 | int insn_idx); | |
6599 | ||
6600 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
6601 | int *insn_idx, int subprog, | |
6602 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
6603 | { |
6604 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 6605 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 6606 | struct bpf_func_state *caller, *callee; |
14351375 | 6607 | int err; |
51c39bb1 | 6608 | bool is_global = false; |
f4d7e40a | 6609 | |
aada9ce6 | 6610 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 6611 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 6612 | state->curframe + 2); |
f4d7e40a AS |
6613 | return -E2BIG; |
6614 | } | |
6615 | ||
f4d7e40a AS |
6616 | caller = state->frame[state->curframe]; |
6617 | if (state->frame[state->curframe + 1]) { | |
6618 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
6619 | state->curframe + 1); | |
6620 | return -EFAULT; | |
6621 | } | |
6622 | ||
51c39bb1 AS |
6623 | func_info_aux = env->prog->aux->func_info_aux; |
6624 | if (func_info_aux) | |
6625 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
95f2f26f | 6626 | err = btf_check_subprog_call(env, subprog, caller->regs); |
51c39bb1 AS |
6627 | if (err == -EFAULT) |
6628 | return err; | |
6629 | if (is_global) { | |
6630 | if (err) { | |
6631 | verbose(env, "Caller passes invalid args into func#%d\n", | |
6632 | subprog); | |
6633 | return err; | |
6634 | } else { | |
6635 | if (env->log.level & BPF_LOG_LEVEL) | |
6636 | verbose(env, | |
6637 | "Func#%d is global and valid. Skipping.\n", | |
6638 | subprog); | |
6639 | clear_caller_saved_regs(env, caller->regs); | |
6640 | ||
45159b27 | 6641 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 6642 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 6643 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
6644 | |
6645 | /* continue with next insn after call */ | |
6646 | return 0; | |
6647 | } | |
6648 | } | |
6649 | ||
bfc6bb74 | 6650 | if (insn->code == (BPF_JMP | BPF_CALL) && |
a5bebc4f | 6651 | insn->src_reg == 0 && |
bfc6bb74 AS |
6652 | insn->imm == BPF_FUNC_timer_set_callback) { |
6653 | struct bpf_verifier_state *async_cb; | |
6654 | ||
6655 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 6656 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
6657 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
6658 | *insn_idx, subprog); | |
6659 | if (!async_cb) | |
6660 | return -EFAULT; | |
6661 | callee = async_cb->frame[0]; | |
6662 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
6663 | ||
6664 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
6665 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
6666 | if (err) | |
6667 | return err; | |
6668 | ||
6669 | clear_caller_saved_regs(env, caller->regs); | |
6670 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
6671 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
6672 | /* continue with next insn after call */ | |
6673 | return 0; | |
6674 | } | |
6675 | ||
f4d7e40a AS |
6676 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
6677 | if (!callee) | |
6678 | return -ENOMEM; | |
6679 | state->frame[state->curframe + 1] = callee; | |
6680 | ||
6681 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
6682 | * into its own stack before reading from it. | |
6683 | * callee can read/write into caller's stack | |
6684 | */ | |
6685 | init_func_state(env, callee, | |
6686 | /* remember the callsite, it will be used by bpf_exit */ | |
6687 | *insn_idx /* callsite */, | |
6688 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 6689 | subprog /* subprog number within this prog */); |
f4d7e40a | 6690 | |
fd978bf7 | 6691 | /* Transfer references to the callee */ |
c69431aa | 6692 | err = copy_reference_state(callee, caller); |
fd978bf7 JS |
6693 | if (err) |
6694 | return err; | |
6695 | ||
14351375 YS |
6696 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
6697 | if (err) | |
6698 | return err; | |
f4d7e40a | 6699 | |
51c39bb1 | 6700 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
6701 | |
6702 | /* only increment it after check_reg_arg() finished */ | |
6703 | state->curframe++; | |
6704 | ||
6705 | /* and go analyze first insn of the callee */ | |
14351375 | 6706 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 6707 | |
06ee7115 | 6708 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 6709 | verbose(env, "caller:\n"); |
0f55f9ed | 6710 | print_verifier_state(env, caller, true); |
f4d7e40a | 6711 | verbose(env, "callee:\n"); |
0f55f9ed | 6712 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
6713 | } |
6714 | return 0; | |
6715 | } | |
6716 | ||
314ee05e YS |
6717 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
6718 | struct bpf_func_state *caller, | |
6719 | struct bpf_func_state *callee) | |
6720 | { | |
6721 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
6722 | * void *callback_ctx, u64 flags); | |
6723 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
6724 | * void *callback_ctx); | |
6725 | */ | |
6726 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6727 | ||
6728 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6729 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6730 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6731 | ||
6732 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6733 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6734 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6735 | ||
6736 | /* pointer to stack or null */ | |
6737 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
6738 | ||
6739 | /* unused */ | |
6740 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6741 | return 0; | |
6742 | } | |
6743 | ||
14351375 YS |
6744 | static int set_callee_state(struct bpf_verifier_env *env, |
6745 | struct bpf_func_state *caller, | |
6746 | struct bpf_func_state *callee, int insn_idx) | |
6747 | { | |
6748 | int i; | |
6749 | ||
6750 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
6751 | * pointers, which connects us up to the liveness chain | |
6752 | */ | |
6753 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
6754 | callee->regs[i] = caller->regs[i]; | |
6755 | return 0; | |
6756 | } | |
6757 | ||
6758 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
6759 | int *insn_idx) | |
6760 | { | |
6761 | int subprog, target_insn; | |
6762 | ||
6763 | target_insn = *insn_idx + insn->imm + 1; | |
6764 | subprog = find_subprog(env, target_insn); | |
6765 | if (subprog < 0) { | |
6766 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
6767 | target_insn); | |
6768 | return -EFAULT; | |
6769 | } | |
6770 | ||
6771 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
6772 | } | |
6773 | ||
69c087ba YS |
6774 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
6775 | struct bpf_func_state *caller, | |
6776 | struct bpf_func_state *callee, | |
6777 | int insn_idx) | |
6778 | { | |
6779 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
6780 | struct bpf_map *map; | |
6781 | int err; | |
6782 | ||
6783 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
6784 | verbose(env, "tail_call abusing map_ptr\n"); | |
6785 | return -EINVAL; | |
6786 | } | |
6787 | ||
6788 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
6789 | if (!map->ops->map_set_for_each_callback_args || | |
6790 | !map->ops->map_for_each_callback) { | |
6791 | verbose(env, "callback function not allowed for map\n"); | |
6792 | return -ENOTSUPP; | |
6793 | } | |
6794 | ||
6795 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
6796 | if (err) | |
6797 | return err; | |
6798 | ||
6799 | callee->in_callback_fn = true; | |
1bfe26fb | 6800 | callee->callback_ret_range = tnum_range(0, 1); |
69c087ba YS |
6801 | return 0; |
6802 | } | |
6803 | ||
e6f2dd0f JK |
6804 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
6805 | struct bpf_func_state *caller, | |
6806 | struct bpf_func_state *callee, | |
6807 | int insn_idx) | |
6808 | { | |
6809 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
6810 | * u64 flags); | |
6811 | * callback_fn(u32 index, void *callback_ctx); | |
6812 | */ | |
6813 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
6814 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
6815 | ||
6816 | /* unused */ | |
6817 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
6818 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6819 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6820 | ||
6821 | callee->in_callback_fn = true; | |
1bfe26fb | 6822 | callee->callback_ret_range = tnum_range(0, 1); |
e6f2dd0f JK |
6823 | return 0; |
6824 | } | |
6825 | ||
b00628b1 AS |
6826 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
6827 | struct bpf_func_state *caller, | |
6828 | struct bpf_func_state *callee, | |
6829 | int insn_idx) | |
6830 | { | |
6831 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6832 | ||
6833 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
6834 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
6835 | */ | |
6836 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
6837 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
6838 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
6839 | ||
6840 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6841 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6842 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
6843 | ||
6844 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6845 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6846 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
6847 | ||
6848 | /* unused */ | |
6849 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6850 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 6851 | callee->in_async_callback_fn = true; |
1bfe26fb | 6852 | callee->callback_ret_range = tnum_range(0, 1); |
b00628b1 AS |
6853 | return 0; |
6854 | } | |
6855 | ||
7c7e3d31 SL |
6856 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
6857 | struct bpf_func_state *caller, | |
6858 | struct bpf_func_state *callee, | |
6859 | int insn_idx) | |
6860 | { | |
6861 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
6862 | * void *callback_fn, void *callback_ctx, u64 flags) | |
6863 | * (callback_fn)(struct task_struct *task, | |
6864 | * struct vm_area_struct *vma, void *callback_ctx); | |
6865 | */ | |
6866 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6867 | ||
6868 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
6869 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6870 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 6871 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
6872 | |
6873 | /* pointer to stack or null */ | |
6874 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
6875 | ||
6876 | /* unused */ | |
6877 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6878 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6879 | callee->in_callback_fn = true; | |
1bfe26fb | 6880 | callee->callback_ret_range = tnum_range(0, 1); |
7c7e3d31 SL |
6881 | return 0; |
6882 | } | |
6883 | ||
20571567 DV |
6884 | static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env, |
6885 | struct bpf_func_state *caller, | |
6886 | struct bpf_func_state *callee, | |
6887 | int insn_idx) | |
6888 | { | |
6889 | /* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void | |
6890 | * callback_ctx, u64 flags); | |
6891 | * callback_fn(struct bpf_dynptr_t* dynptr, void *callback_ctx); | |
6892 | */ | |
6893 | __mark_reg_not_init(env, &callee->regs[BPF_REG_0]); | |
6894 | callee->regs[BPF_REG_1].type = PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL; | |
6895 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
6896 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
6897 | ||
6898 | /* unused */ | |
6899 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
6900 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6901 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6902 | ||
6903 | callee->in_callback_fn = true; | |
c92a7a52 | 6904 | callee->callback_ret_range = tnum_range(0, 1); |
20571567 DV |
6905 | return 0; |
6906 | } | |
6907 | ||
f4d7e40a AS |
6908 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
6909 | { | |
6910 | struct bpf_verifier_state *state = env->cur_state; | |
6911 | struct bpf_func_state *caller, *callee; | |
6912 | struct bpf_reg_state *r0; | |
fd978bf7 | 6913 | int err; |
f4d7e40a AS |
6914 | |
6915 | callee = state->frame[state->curframe]; | |
6916 | r0 = &callee->regs[BPF_REG_0]; | |
6917 | if (r0->type == PTR_TO_STACK) { | |
6918 | /* technically it's ok to return caller's stack pointer | |
6919 | * (or caller's caller's pointer) back to the caller, | |
6920 | * since these pointers are valid. Only current stack | |
6921 | * pointer will be invalid as soon as function exits, | |
6922 | * but let's be conservative | |
6923 | */ | |
6924 | verbose(env, "cannot return stack pointer to the caller\n"); | |
6925 | return -EINVAL; | |
6926 | } | |
6927 | ||
6928 | state->curframe--; | |
6929 | caller = state->frame[state->curframe]; | |
69c087ba YS |
6930 | if (callee->in_callback_fn) { |
6931 | /* enforce R0 return value range [0, 1]. */ | |
1bfe26fb | 6932 | struct tnum range = callee->callback_ret_range; |
69c087ba YS |
6933 | |
6934 | if (r0->type != SCALAR_VALUE) { | |
6935 | verbose(env, "R0 not a scalar value\n"); | |
6936 | return -EACCES; | |
6937 | } | |
6938 | if (!tnum_in(range, r0->var_off)) { | |
6939 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
6940 | return -EINVAL; | |
6941 | } | |
6942 | } else { | |
6943 | /* return to the caller whatever r0 had in the callee */ | |
6944 | caller->regs[BPF_REG_0] = *r0; | |
6945 | } | |
f4d7e40a | 6946 | |
9d9d00ac KKD |
6947 | /* callback_fn frame should have released its own additions to parent's |
6948 | * reference state at this point, or check_reference_leak would | |
6949 | * complain, hence it must be the same as the caller. There is no need | |
6950 | * to copy it back. | |
6951 | */ | |
6952 | if (!callee->in_callback_fn) { | |
6953 | /* Transfer references to the caller */ | |
6954 | err = copy_reference_state(caller, callee); | |
6955 | if (err) | |
6956 | return err; | |
6957 | } | |
fd978bf7 | 6958 | |
f4d7e40a | 6959 | *insn_idx = callee->callsite + 1; |
06ee7115 | 6960 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 6961 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 6962 | print_verifier_state(env, callee, true); |
f4d7e40a | 6963 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 6964 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
6965 | } |
6966 | /* clear everything in the callee */ | |
6967 | free_func_state(callee); | |
6968 | state->frame[state->curframe + 1] = NULL; | |
6969 | return 0; | |
6970 | } | |
6971 | ||
849fa506 YS |
6972 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
6973 | int func_id, | |
6974 | struct bpf_call_arg_meta *meta) | |
6975 | { | |
6976 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
6977 | ||
6978 | if (ret_type != RET_INTEGER || | |
6979 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 6980 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
6981 | func_id != BPF_FUNC_probe_read_str && |
6982 | func_id != BPF_FUNC_probe_read_kernel_str && | |
6983 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
6984 | return; |
6985 | ||
10060503 | 6986 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 6987 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
6988 | ret_reg->smin_value = -MAX_ERRNO; |
6989 | ret_reg->s32_min_value = -MAX_ERRNO; | |
3844d153 | 6990 | reg_bounds_sync(ret_reg); |
849fa506 YS |
6991 | } |
6992 | ||
c93552c4 DB |
6993 | static int |
6994 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
6995 | int func_id, int insn_idx) | |
6996 | { | |
6997 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 6998 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
6999 | |
7000 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
7001 | func_id != BPF_FUNC_map_lookup_elem && |
7002 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
7003 | func_id != BPF_FUNC_map_delete_elem && |
7004 | func_id != BPF_FUNC_map_push_elem && | |
7005 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 7006 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f | 7007 | func_id != BPF_FUNC_for_each_map_elem && |
07343110 FZ |
7008 | func_id != BPF_FUNC_redirect_map && |
7009 | func_id != BPF_FUNC_map_lookup_percpu_elem) | |
c93552c4 | 7010 | return 0; |
09772d92 | 7011 | |
591fe988 | 7012 | if (map == NULL) { |
c93552c4 DB |
7013 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
7014 | return -EINVAL; | |
7015 | } | |
7016 | ||
591fe988 DB |
7017 | /* In case of read-only, some additional restrictions |
7018 | * need to be applied in order to prevent altering the | |
7019 | * state of the map from program side. | |
7020 | */ | |
7021 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
7022 | (func_id == BPF_FUNC_map_delete_elem || | |
7023 | func_id == BPF_FUNC_map_update_elem || | |
7024 | func_id == BPF_FUNC_map_push_elem || | |
7025 | func_id == BPF_FUNC_map_pop_elem)) { | |
7026 | verbose(env, "write into map forbidden\n"); | |
7027 | return -EACCES; | |
7028 | } | |
7029 | ||
d2e4c1e6 | 7030 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 7031 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 7032 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 7033 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 7034 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 7035 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
7036 | return 0; |
7037 | } | |
7038 | ||
d2e4c1e6 DB |
7039 | static int |
7040 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
7041 | int func_id, int insn_idx) | |
7042 | { | |
7043 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
7044 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
7045 | struct bpf_map *map = meta->map_ptr; | |
a657182a | 7046 | u64 val, max; |
cc52d914 | 7047 | int err; |
d2e4c1e6 DB |
7048 | |
7049 | if (func_id != BPF_FUNC_tail_call) | |
7050 | return 0; | |
7051 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
7052 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
7053 | return -EINVAL; | |
7054 | } | |
7055 | ||
d2e4c1e6 | 7056 | reg = ®s[BPF_REG_3]; |
a657182a DB |
7057 | val = reg->var_off.value; |
7058 | max = map->max_entries; | |
d2e4c1e6 | 7059 | |
a657182a | 7060 | if (!(register_is_const(reg) && val < max)) { |
d2e4c1e6 DB |
7061 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
7062 | return 0; | |
7063 | } | |
7064 | ||
cc52d914 DB |
7065 | err = mark_chain_precision(env, BPF_REG_3); |
7066 | if (err) | |
7067 | return err; | |
d2e4c1e6 DB |
7068 | if (bpf_map_key_unseen(aux)) |
7069 | bpf_map_key_store(aux, val); | |
7070 | else if (!bpf_map_key_poisoned(aux) && | |
7071 | bpf_map_key_immediate(aux) != val) | |
7072 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
7073 | return 0; | |
7074 | } | |
7075 | ||
fd978bf7 JS |
7076 | static int check_reference_leak(struct bpf_verifier_env *env) |
7077 | { | |
7078 | struct bpf_func_state *state = cur_func(env); | |
9d9d00ac | 7079 | bool refs_lingering = false; |
fd978bf7 JS |
7080 | int i; |
7081 | ||
9d9d00ac KKD |
7082 | if (state->frameno && !state->in_callback_fn) |
7083 | return 0; | |
7084 | ||
fd978bf7 | 7085 | for (i = 0; i < state->acquired_refs; i++) { |
9d9d00ac KKD |
7086 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) |
7087 | continue; | |
fd978bf7 JS |
7088 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
7089 | state->refs[i].id, state->refs[i].insn_idx); | |
9d9d00ac | 7090 | refs_lingering = true; |
fd978bf7 | 7091 | } |
9d9d00ac | 7092 | return refs_lingering ? -EINVAL : 0; |
fd978bf7 JS |
7093 | } |
7094 | ||
7b15523a FR |
7095 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
7096 | struct bpf_reg_state *regs) | |
7097 | { | |
7098 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
7099 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
7100 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
7101 | int err, fmt_map_off, num_args; | |
7102 | u64 fmt_addr; | |
7103 | char *fmt; | |
7104 | ||
7105 | /* data must be an array of u64 */ | |
7106 | if (data_len_reg->var_off.value % 8) | |
7107 | return -EINVAL; | |
7108 | num_args = data_len_reg->var_off.value / 8; | |
7109 | ||
7110 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
7111 | * and map_direct_value_addr is set. | |
7112 | */ | |
7113 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
7114 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
7115 | fmt_map_off); | |
8e8ee109 FR |
7116 | if (err) { |
7117 | verbose(env, "verifier bug\n"); | |
7118 | return -EFAULT; | |
7119 | } | |
7b15523a FR |
7120 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
7121 | ||
7122 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
7123 | * can focus on validating the format specifiers. | |
7124 | */ | |
48cac3f4 | 7125 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
7b15523a FR |
7126 | if (err < 0) |
7127 | verbose(env, "Invalid format string\n"); | |
7128 | ||
7129 | return err; | |
7130 | } | |
7131 | ||
9b99edca JO |
7132 | static int check_get_func_ip(struct bpf_verifier_env *env) |
7133 | { | |
9b99edca JO |
7134 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
7135 | int func_id = BPF_FUNC_get_func_ip; | |
7136 | ||
7137 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 7138 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
7139 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
7140 | func_id_name(func_id), func_id); | |
7141 | return -ENOTSUPP; | |
7142 | } | |
7143 | return 0; | |
9ffd9f3f JO |
7144 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
7145 | return 0; | |
9b99edca JO |
7146 | } |
7147 | ||
7148 | verbose(env, "func %s#%d not supported for program type %d\n", | |
7149 | func_id_name(func_id), func_id, type); | |
7150 | return -ENOTSUPP; | |
7151 | } | |
7152 | ||
1ade2371 EZ |
7153 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
7154 | { | |
7155 | return &env->insn_aux_data[env->insn_idx]; | |
7156 | } | |
7157 | ||
7158 | static bool loop_flag_is_zero(struct bpf_verifier_env *env) | |
7159 | { | |
7160 | struct bpf_reg_state *regs = cur_regs(env); | |
7161 | struct bpf_reg_state *reg = ®s[BPF_REG_4]; | |
7162 | bool reg_is_null = register_is_null(reg); | |
7163 | ||
7164 | if (reg_is_null) | |
7165 | mark_chain_precision(env, BPF_REG_4); | |
7166 | ||
7167 | return reg_is_null; | |
7168 | } | |
7169 | ||
7170 | static void update_loop_inline_state(struct bpf_verifier_env *env, u32 subprogno) | |
7171 | { | |
7172 | struct bpf_loop_inline_state *state = &cur_aux(env)->loop_inline_state; | |
7173 | ||
7174 | if (!state->initialized) { | |
7175 | state->initialized = 1; | |
7176 | state->fit_for_inline = loop_flag_is_zero(env); | |
7177 | state->callback_subprogno = subprogno; | |
7178 | return; | |
7179 | } | |
7180 | ||
7181 | if (!state->fit_for_inline) | |
7182 | return; | |
7183 | ||
7184 | state->fit_for_inline = (loop_flag_is_zero(env) && | |
7185 | state->callback_subprogno == subprogno); | |
7186 | } | |
7187 | ||
69c087ba YS |
7188 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
7189 | int *insn_idx_p) | |
17a52670 | 7190 | { |
aef9d4a3 | 7191 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
17a52670 | 7192 | const struct bpf_func_proto *fn = NULL; |
3c480732 | 7193 | enum bpf_return_type ret_type; |
c25b2ae1 | 7194 | enum bpf_type_flag ret_flag; |
638f5b90 | 7195 | struct bpf_reg_state *regs; |
33ff9823 | 7196 | struct bpf_call_arg_meta meta; |
69c087ba | 7197 | int insn_idx = *insn_idx_p; |
969bf05e | 7198 | bool changes_data; |
69c087ba | 7199 | int i, err, func_id; |
17a52670 AS |
7200 | |
7201 | /* find function prototype */ | |
69c087ba | 7202 | func_id = insn->imm; |
17a52670 | 7203 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
7204 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
7205 | func_id); | |
17a52670 AS |
7206 | return -EINVAL; |
7207 | } | |
7208 | ||
00176a34 | 7209 | if (env->ops->get_func_proto) |
5e43f899 | 7210 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 7211 | if (!fn) { |
61bd5218 JK |
7212 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
7213 | func_id); | |
17a52670 AS |
7214 | return -EINVAL; |
7215 | } | |
7216 | ||
7217 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 7218 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 7219 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
7220 | return -EINVAL; |
7221 | } | |
7222 | ||
eae2e83e JO |
7223 | if (fn->allowed && !fn->allowed(env->prog)) { |
7224 | verbose(env, "helper call is not allowed in probe\n"); | |
7225 | return -EINVAL; | |
7226 | } | |
7227 | ||
04514d13 | 7228 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 7229 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
7230 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
7231 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
7232 | func_id_name(func_id), func_id); | |
7233 | return -EINVAL; | |
7234 | } | |
969bf05e | 7235 | |
33ff9823 | 7236 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 7237 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 7238 | |
0c9a7a7e | 7239 | err = check_func_proto(fn, func_id); |
435faee1 | 7240 | if (err) { |
61bd5218 | 7241 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 7242 | func_id_name(func_id), func_id); |
435faee1 DB |
7243 | return err; |
7244 | } | |
7245 | ||
d83525ca | 7246 | meta.func_id = func_id; |
17a52670 | 7247 | /* check args */ |
523a4cf4 | 7248 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
af7ec138 | 7249 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
7250 | if (err) |
7251 | return err; | |
7252 | } | |
17a52670 | 7253 | |
c93552c4 DB |
7254 | err = record_func_map(env, &meta, func_id, insn_idx); |
7255 | if (err) | |
7256 | return err; | |
7257 | ||
d2e4c1e6 DB |
7258 | err = record_func_key(env, &meta, func_id, insn_idx); |
7259 | if (err) | |
7260 | return err; | |
7261 | ||
435faee1 DB |
7262 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
7263 | * is inferred from register state. | |
7264 | */ | |
7265 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
7266 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
7267 | BPF_WRITE, -1, false); | |
435faee1 DB |
7268 | if (err) |
7269 | return err; | |
7270 | } | |
7271 | ||
8f14852e KKD |
7272 | regs = cur_regs(env); |
7273 | ||
97e03f52 JK |
7274 | if (meta.uninit_dynptr_regno) { |
7275 | /* we write BPF_DW bits (8 bytes) at a time */ | |
7276 | for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) { | |
7277 | err = check_mem_access(env, insn_idx, meta.uninit_dynptr_regno, | |
7278 | i, BPF_DW, BPF_WRITE, -1, false); | |
7279 | if (err) | |
7280 | return err; | |
7281 | } | |
7282 | ||
7283 | err = mark_stack_slots_dynptr(env, ®s[meta.uninit_dynptr_regno], | |
7284 | fn->arg_type[meta.uninit_dynptr_regno - BPF_REG_1], | |
7285 | insn_idx); | |
7286 | if (err) | |
7287 | return err; | |
7288 | } | |
7289 | ||
8f14852e KKD |
7290 | if (meta.release_regno) { |
7291 | err = -EINVAL; | |
97e03f52 JK |
7292 | if (arg_type_is_dynptr(fn->arg_type[meta.release_regno - BPF_REG_1])) |
7293 | err = unmark_stack_slots_dynptr(env, ®s[meta.release_regno]); | |
7294 | else if (meta.ref_obj_id) | |
8f14852e KKD |
7295 | err = release_reference(env, meta.ref_obj_id); |
7296 | /* meta.ref_obj_id can only be 0 if register that is meant to be | |
7297 | * released is NULL, which must be > R0. | |
7298 | */ | |
7299 | else if (register_is_null(®s[meta.release_regno])) | |
7300 | err = 0; | |
46f8bc92 MKL |
7301 | if (err) { |
7302 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
7303 | func_id_name(func_id), func_id); | |
fd978bf7 | 7304 | return err; |
46f8bc92 | 7305 | } |
fd978bf7 JS |
7306 | } |
7307 | ||
e6f2dd0f JK |
7308 | switch (func_id) { |
7309 | case BPF_FUNC_tail_call: | |
7310 | err = check_reference_leak(env); | |
7311 | if (err) { | |
7312 | verbose(env, "tail_call would lead to reference leak\n"); | |
7313 | return err; | |
7314 | } | |
7315 | break; | |
7316 | case BPF_FUNC_get_local_storage: | |
7317 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
7318 | * this is required because get_local_storage() can't return an error. | |
7319 | */ | |
7320 | if (!register_is_null(®s[BPF_REG_2])) { | |
7321 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
7322 | return -EINVAL; | |
7323 | } | |
7324 | break; | |
7325 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
7326 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7327 | set_map_elem_callback_state); | |
e6f2dd0f JK |
7328 | break; |
7329 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
7330 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7331 | set_timer_callback_state); | |
e6f2dd0f JK |
7332 | break; |
7333 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
7334 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7335 | set_find_vma_callback_state); | |
e6f2dd0f JK |
7336 | break; |
7337 | case BPF_FUNC_snprintf: | |
7b15523a | 7338 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
7339 | break; |
7340 | case BPF_FUNC_loop: | |
1ade2371 | 7341 | update_loop_inline_state(env, meta.subprogno); |
e6f2dd0f JK |
7342 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7343 | set_loop_callback_state); | |
7344 | break; | |
263ae152 JK |
7345 | case BPF_FUNC_dynptr_from_mem: |
7346 | if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) { | |
7347 | verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n", | |
7348 | reg_type_str(env, regs[BPF_REG_1].type)); | |
7349 | return -EACCES; | |
7350 | } | |
69fd337a SF |
7351 | break; |
7352 | case BPF_FUNC_set_retval: | |
aef9d4a3 SF |
7353 | if (prog_type == BPF_PROG_TYPE_LSM && |
7354 | env->prog->expected_attach_type == BPF_LSM_CGROUP) { | |
69fd337a SF |
7355 | if (!env->prog->aux->attach_func_proto->type) { |
7356 | /* Make sure programs that attach to void | |
7357 | * hooks don't try to modify return value. | |
7358 | */ | |
7359 | verbose(env, "BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
7360 | return -EINVAL; | |
7361 | } | |
7362 | } | |
7363 | break; | |
88374342 JK |
7364 | case BPF_FUNC_dynptr_data: |
7365 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { | |
7366 | if (arg_type_is_dynptr(fn->arg_type[i])) { | |
20571567 DV |
7367 | struct bpf_reg_state *reg = ®s[BPF_REG_1 + i]; |
7368 | ||
88374342 JK |
7369 | if (meta.ref_obj_id) { |
7370 | verbose(env, "verifier internal error: meta.ref_obj_id already set\n"); | |
7371 | return -EFAULT; | |
7372 | } | |
20571567 DV |
7373 | |
7374 | if (base_type(reg->type) != PTR_TO_DYNPTR) | |
7375 | /* Find the id of the dynptr we're | |
7376 | * tracking the reference of | |
7377 | */ | |
7378 | meta.ref_obj_id = stack_slot_get_id(env, reg); | |
88374342 JK |
7379 | break; |
7380 | } | |
7381 | } | |
7382 | if (i == MAX_BPF_FUNC_REG_ARGS) { | |
7383 | verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n"); | |
7384 | return -EFAULT; | |
7385 | } | |
7386 | break; | |
20571567 DV |
7387 | case BPF_FUNC_user_ringbuf_drain: |
7388 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
7389 | set_user_ringbuf_callback_state); | |
7390 | break; | |
7b15523a FR |
7391 | } |
7392 | ||
e6f2dd0f JK |
7393 | if (err) |
7394 | return err; | |
7395 | ||
17a52670 | 7396 | /* reset caller saved regs */ |
dc503a8a | 7397 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 7398 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
7399 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
7400 | } | |
17a52670 | 7401 | |
5327ed3d JW |
7402 | /* helper call returns 64-bit value. */ |
7403 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
7404 | ||
dc503a8a | 7405 | /* update return register (already marked as written above) */ |
3c480732 | 7406 | ret_type = fn->ret_type; |
0c9a7a7e JK |
7407 | ret_flag = type_flag(ret_type); |
7408 | ||
7409 | switch (base_type(ret_type)) { | |
7410 | case RET_INTEGER: | |
f1174f77 | 7411 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 7412 | mark_reg_unknown(env, regs, BPF_REG_0); |
0c9a7a7e JK |
7413 | break; |
7414 | case RET_VOID: | |
17a52670 | 7415 | regs[BPF_REG_0].type = NOT_INIT; |
0c9a7a7e JK |
7416 | break; |
7417 | case RET_PTR_TO_MAP_VALUE: | |
f1174f77 | 7418 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 7419 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
7420 | /* remember map_ptr, so that check_map_access() |
7421 | * can check 'value_size' boundary of memory access | |
7422 | * to map element returned from bpf_map_lookup_elem() | |
7423 | */ | |
33ff9823 | 7424 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
7425 | verbose(env, |
7426 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
7427 | return -EINVAL; |
7428 | } | |
33ff9823 | 7429 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 7430 | regs[BPF_REG_0].map_uid = meta.map_uid; |
c25b2ae1 HL |
7431 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
7432 | if (!type_may_be_null(ret_type) && | |
db559117 | 7433 | btf_record_has_field(meta.map_ptr->record, BPF_SPIN_LOCK)) { |
c25b2ae1 | 7434 | regs[BPF_REG_0].id = ++env->id_gen; |
4d31f301 | 7435 | } |
0c9a7a7e JK |
7436 | break; |
7437 | case RET_PTR_TO_SOCKET: | |
c64b7983 | 7438 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7439 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
0c9a7a7e JK |
7440 | break; |
7441 | case RET_PTR_TO_SOCK_COMMON: | |
85a51f8c | 7442 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7443 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
0c9a7a7e JK |
7444 | break; |
7445 | case RET_PTR_TO_TCP_SOCK: | |
655a51e5 | 7446 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7447 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
0c9a7a7e JK |
7448 | break; |
7449 | case RET_PTR_TO_ALLOC_MEM: | |
457f4436 | 7450 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7451 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
457f4436 | 7452 | regs[BPF_REG_0].mem_size = meta.mem_size; |
0c9a7a7e JK |
7453 | break; |
7454 | case RET_PTR_TO_MEM_OR_BTF_ID: | |
7455 | { | |
eaa6bcb7 HL |
7456 | const struct btf_type *t; |
7457 | ||
7458 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 7459 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
7460 | if (!btf_type_is_struct(t)) { |
7461 | u32 tsize; | |
7462 | const struct btf_type *ret; | |
7463 | const char *tname; | |
7464 | ||
7465 | /* resolve the type size of ksym. */ | |
22dc4a0f | 7466 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 7467 | if (IS_ERR(ret)) { |
22dc4a0f | 7468 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
7469 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
7470 | tname, PTR_ERR(ret)); | |
7471 | return -EINVAL; | |
7472 | } | |
c25b2ae1 | 7473 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
eaa6bcb7 HL |
7474 | regs[BPF_REG_0].mem_size = tsize; |
7475 | } else { | |
34d3a78c HL |
7476 | /* MEM_RDONLY may be carried from ret_flag, but it |
7477 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise | |
7478 | * it will confuse the check of PTR_TO_BTF_ID in | |
7479 | * check_mem_access(). | |
7480 | */ | |
7481 | ret_flag &= ~MEM_RDONLY; | |
7482 | ||
c25b2ae1 | 7483 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
22dc4a0f | 7484 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
7485 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
7486 | } | |
0c9a7a7e JK |
7487 | break; |
7488 | } | |
7489 | case RET_PTR_TO_BTF_ID: | |
7490 | { | |
c0a5a21c | 7491 | struct btf *ret_btf; |
af7ec138 YS |
7492 | int ret_btf_id; |
7493 | ||
7494 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
c25b2ae1 | 7495 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
c0a5a21c | 7496 | if (func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac KKD |
7497 | ret_btf = meta.kptr_field->kptr.btf; |
7498 | ret_btf_id = meta.kptr_field->kptr.btf_id; | |
c0a5a21c | 7499 | } else { |
47e34cb7 DM |
7500 | if (fn->ret_btf_id == BPF_PTR_POISON) { |
7501 | verbose(env, "verifier internal error:"); | |
7502 | verbose(env, "func %s has non-overwritten BPF_PTR_POISON return type\n", | |
7503 | func_id_name(func_id)); | |
7504 | return -EINVAL; | |
7505 | } | |
c0a5a21c KKD |
7506 | ret_btf = btf_vmlinux; |
7507 | ret_btf_id = *fn->ret_btf_id; | |
7508 | } | |
af7ec138 | 7509 | if (ret_btf_id == 0) { |
3c480732 HL |
7510 | verbose(env, "invalid return type %u of func %s#%d\n", |
7511 | base_type(ret_type), func_id_name(func_id), | |
7512 | func_id); | |
af7ec138 YS |
7513 | return -EINVAL; |
7514 | } | |
c0a5a21c | 7515 | regs[BPF_REG_0].btf = ret_btf; |
af7ec138 | 7516 | regs[BPF_REG_0].btf_id = ret_btf_id; |
0c9a7a7e JK |
7517 | break; |
7518 | } | |
7519 | default: | |
3c480732 HL |
7520 | verbose(env, "unknown return type %u of func %s#%d\n", |
7521 | base_type(ret_type), func_id_name(func_id), func_id); | |
17a52670 AS |
7522 | return -EINVAL; |
7523 | } | |
04fd61ab | 7524 | |
c25b2ae1 | 7525 | if (type_may_be_null(regs[BPF_REG_0].type)) |
93c230e3 MKL |
7526 | regs[BPF_REG_0].id = ++env->id_gen; |
7527 | ||
b2d8ef19 DM |
7528 | if (helper_multiple_ref_obj_use(func_id, meta.map_ptr)) { |
7529 | verbose(env, "verifier internal error: func %s#%d sets ref_obj_id more than once\n", | |
7530 | func_id_name(func_id), func_id); | |
7531 | return -EFAULT; | |
7532 | } | |
7533 | ||
88374342 | 7534 | if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) { |
1b986589 MKL |
7535 | /* For release_reference() */ |
7536 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 7537 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
7538 | int id = acquire_reference_state(env, insn_idx); |
7539 | ||
7540 | if (id < 0) | |
7541 | return id; | |
7542 | /* For mark_ptr_or_null_reg() */ | |
7543 | regs[BPF_REG_0].id = id; | |
7544 | /* For release_reference() */ | |
7545 | regs[BPF_REG_0].ref_obj_id = id; | |
7546 | } | |
1b986589 | 7547 | |
849fa506 YS |
7548 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
7549 | ||
61bd5218 | 7550 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
7551 | if (err) |
7552 | return err; | |
04fd61ab | 7553 | |
fa28dcb8 SL |
7554 | if ((func_id == BPF_FUNC_get_stack || |
7555 | func_id == BPF_FUNC_get_task_stack) && | |
7556 | !env->prog->has_callchain_buf) { | |
c195651e YS |
7557 | const char *err_str; |
7558 | ||
7559 | #ifdef CONFIG_PERF_EVENTS | |
7560 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
7561 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
7562 | #else | |
7563 | err = -ENOTSUPP; | |
7564 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
7565 | #endif | |
7566 | if (err) { | |
7567 | verbose(env, err_str, func_id_name(func_id), func_id); | |
7568 | return err; | |
7569 | } | |
7570 | ||
7571 | env->prog->has_callchain_buf = true; | |
7572 | } | |
7573 | ||
5d99cb2c SL |
7574 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
7575 | env->prog->call_get_stack = true; | |
7576 | ||
9b99edca JO |
7577 | if (func_id == BPF_FUNC_get_func_ip) { |
7578 | if (check_get_func_ip(env)) | |
7579 | return -ENOTSUPP; | |
7580 | env->prog->call_get_func_ip = true; | |
7581 | } | |
7582 | ||
969bf05e AS |
7583 | if (changes_data) |
7584 | clear_all_pkt_pointers(env); | |
7585 | return 0; | |
7586 | } | |
7587 | ||
e6ac2450 MKL |
7588 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
7589 | * the BTF func_proto's return value size and argument. | |
7590 | */ | |
7591 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
7592 | size_t reg_size) | |
7593 | { | |
7594 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
7595 | ||
7596 | if (regno == BPF_REG_0) { | |
7597 | /* Function return value */ | |
7598 | reg->live |= REG_LIVE_WRITTEN; | |
7599 | reg->subreg_def = reg_size == sizeof(u64) ? | |
7600 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
7601 | } else { | |
7602 | /* Function argument */ | |
7603 | if (reg_size == sizeof(u64)) { | |
7604 | mark_insn_zext(env, reg); | |
7605 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
7606 | } else { | |
7607 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
7608 | } | |
7609 | } | |
7610 | } | |
7611 | ||
5c073f26 KKD |
7612 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
7613 | int *insn_idx_p) | |
e6ac2450 MKL |
7614 | { |
7615 | const struct btf_type *t, *func, *func_proto, *ptr_type; | |
7616 | struct bpf_reg_state *regs = cur_regs(env); | |
eb1f7f71 | 7617 | struct bpf_kfunc_arg_meta meta = { 0 }; |
e6ac2450 MKL |
7618 | const char *func_name, *ptr_type_name; |
7619 | u32 i, nargs, func_id, ptr_type_id; | |
5c073f26 | 7620 | int err, insn_idx = *insn_idx_p; |
e6ac2450 | 7621 | const struct btf_param *args; |
2357672c | 7622 | struct btf *desc_btf; |
a4703e31 | 7623 | u32 *kfunc_flags; |
5c073f26 | 7624 | bool acq; |
e6ac2450 | 7625 | |
a5d82727 KKD |
7626 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
7627 | if (!insn->imm) | |
7628 | return 0; | |
7629 | ||
43bf0878 | 7630 | desc_btf = find_kfunc_desc_btf(env, insn->off); |
2357672c KKD |
7631 | if (IS_ERR(desc_btf)) |
7632 | return PTR_ERR(desc_btf); | |
7633 | ||
e6ac2450 | 7634 | func_id = insn->imm; |
2357672c KKD |
7635 | func = btf_type_by_id(desc_btf, func_id); |
7636 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
7637 | func_proto = btf_type_by_id(desc_btf, func->type); | |
e6ac2450 | 7638 | |
a4703e31 KKD |
7639 | kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id); |
7640 | if (!kfunc_flags) { | |
e6ac2450 MKL |
7641 | verbose(env, "calling kernel function %s is not allowed\n", |
7642 | func_name); | |
7643 | return -EACCES; | |
7644 | } | |
4dd48c6f AS |
7645 | if (*kfunc_flags & KF_DESTRUCTIVE && !capable(CAP_SYS_BOOT)) { |
7646 | verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capabilities\n"); | |
7647 | return -EACCES; | |
7648 | } | |
7649 | ||
a4703e31 | 7650 | acq = *kfunc_flags & KF_ACQUIRE; |
5c073f26 | 7651 | |
eb1f7f71 BT |
7652 | meta.flags = *kfunc_flags; |
7653 | ||
e6ac2450 | 7654 | /* Check the arguments */ |
eb1f7f71 | 7655 | err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs, &meta); |
5c073f26 | 7656 | if (err < 0) |
e6ac2450 | 7657 | return err; |
5c073f26 KKD |
7658 | /* In case of release function, we get register number of refcounted |
7659 | * PTR_TO_BTF_ID back from btf_check_kfunc_arg_match, do the release now | |
7660 | */ | |
7661 | if (err) { | |
7662 | err = release_reference(env, regs[err].ref_obj_id); | |
7663 | if (err) { | |
7664 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
7665 | func_name, func_id); | |
7666 | return err; | |
7667 | } | |
7668 | } | |
e6ac2450 MKL |
7669 | |
7670 | for (i = 0; i < CALLER_SAVED_REGS; i++) | |
7671 | mark_reg_not_init(env, regs, caller_saved[i]); | |
7672 | ||
7673 | /* Check return type */ | |
2357672c | 7674 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
5c073f26 | 7675 | |
eb1f7f71 | 7676 | if (acq && !btf_type_is_struct_ptr(desc_btf, t)) { |
5c073f26 KKD |
7677 | verbose(env, "acquire kernel function does not return PTR_TO_BTF_ID\n"); |
7678 | return -EINVAL; | |
7679 | } | |
7680 | ||
e6ac2450 MKL |
7681 | if (btf_type_is_scalar(t)) { |
7682 | mark_reg_unknown(env, regs, BPF_REG_0); | |
7683 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
7684 | } else if (btf_type_is_ptr(t)) { | |
2357672c | 7685 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, |
e6ac2450 MKL |
7686 | &ptr_type_id); |
7687 | if (!btf_type_is_struct(ptr_type)) { | |
eb1f7f71 BT |
7688 | if (!meta.r0_size) { |
7689 | ptr_type_name = btf_name_by_offset(desc_btf, | |
7690 | ptr_type->name_off); | |
7691 | verbose(env, | |
7692 | "kernel function %s returns pointer type %s %s is not supported\n", | |
7693 | func_name, | |
7694 | btf_type_str(ptr_type), | |
7695 | ptr_type_name); | |
7696 | return -EINVAL; | |
7697 | } | |
7698 | ||
7699 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
7700 | regs[BPF_REG_0].type = PTR_TO_MEM; | |
7701 | regs[BPF_REG_0].mem_size = meta.r0_size; | |
7702 | ||
7703 | if (meta.r0_rdonly) | |
7704 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
7705 | ||
7706 | /* Ensures we don't access the memory after a release_reference() */ | |
7707 | if (meta.ref_obj_id) | |
7708 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
7709 | } else { | |
7710 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
7711 | regs[BPF_REG_0].btf = desc_btf; | |
7712 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; | |
7713 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
e6ac2450 | 7714 | } |
a4703e31 | 7715 | if (*kfunc_flags & KF_RET_NULL) { |
5c073f26 KKD |
7716 | regs[BPF_REG_0].type |= PTR_MAYBE_NULL; |
7717 | /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ | |
7718 | regs[BPF_REG_0].id = ++env->id_gen; | |
7719 | } | |
e6ac2450 | 7720 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
5c073f26 KKD |
7721 | if (acq) { |
7722 | int id = acquire_reference_state(env, insn_idx); | |
7723 | ||
7724 | if (id < 0) | |
7725 | return id; | |
7726 | regs[BPF_REG_0].id = id; | |
7727 | regs[BPF_REG_0].ref_obj_id = id; | |
7728 | } | |
e6ac2450 MKL |
7729 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ |
7730 | ||
7731 | nargs = btf_type_vlen(func_proto); | |
7732 | args = (const struct btf_param *)(func_proto + 1); | |
7733 | for (i = 0; i < nargs; i++) { | |
7734 | u32 regno = i + 1; | |
7735 | ||
2357672c | 7736 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
7737 | if (btf_type_is_ptr(t)) |
7738 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
7739 | else | |
7740 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
7741 | mark_btf_func_reg_size(env, regno, t->size); | |
7742 | } | |
7743 | ||
7744 | return 0; | |
7745 | } | |
7746 | ||
b03c9f9f EC |
7747 | static bool signed_add_overflows(s64 a, s64 b) |
7748 | { | |
7749 | /* Do the add in u64, where overflow is well-defined */ | |
7750 | s64 res = (s64)((u64)a + (u64)b); | |
7751 | ||
7752 | if (b < 0) | |
7753 | return res > a; | |
7754 | return res < a; | |
7755 | } | |
7756 | ||
bc895e8b | 7757 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
7758 | { |
7759 | /* Do the add in u32, where overflow is well-defined */ | |
7760 | s32 res = (s32)((u32)a + (u32)b); | |
7761 | ||
7762 | if (b < 0) | |
7763 | return res > a; | |
7764 | return res < a; | |
7765 | } | |
7766 | ||
bc895e8b | 7767 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
7768 | { |
7769 | /* Do the sub in u64, where overflow is well-defined */ | |
7770 | s64 res = (s64)((u64)a - (u64)b); | |
7771 | ||
7772 | if (b < 0) | |
7773 | return res < a; | |
7774 | return res > a; | |
969bf05e AS |
7775 | } |
7776 | ||
3f50f132 JF |
7777 | static bool signed_sub32_overflows(s32 a, s32 b) |
7778 | { | |
bc895e8b | 7779 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
7780 | s32 res = (s32)((u32)a - (u32)b); |
7781 | ||
7782 | if (b < 0) | |
7783 | return res < a; | |
7784 | return res > a; | |
7785 | } | |
7786 | ||
bb7f0f98 AS |
7787 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
7788 | const struct bpf_reg_state *reg, | |
7789 | enum bpf_reg_type type) | |
7790 | { | |
7791 | bool known = tnum_is_const(reg->var_off); | |
7792 | s64 val = reg->var_off.value; | |
7793 | s64 smin = reg->smin_value; | |
7794 | ||
7795 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
7796 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
c25b2ae1 | 7797 | reg_type_str(env, type), val); |
bb7f0f98 AS |
7798 | return false; |
7799 | } | |
7800 | ||
7801 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
7802 | verbose(env, "%s pointer offset %d is not allowed\n", | |
c25b2ae1 | 7803 | reg_type_str(env, type), reg->off); |
bb7f0f98 AS |
7804 | return false; |
7805 | } | |
7806 | ||
7807 | if (smin == S64_MIN) { | |
7808 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
c25b2ae1 | 7809 | reg_type_str(env, type)); |
bb7f0f98 AS |
7810 | return false; |
7811 | } | |
7812 | ||
7813 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
7814 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
c25b2ae1 | 7815 | smin, reg_type_str(env, type)); |
bb7f0f98 AS |
7816 | return false; |
7817 | } | |
7818 | ||
7819 | return true; | |
7820 | } | |
7821 | ||
a6aaece0 DB |
7822 | enum { |
7823 | REASON_BOUNDS = -1, | |
7824 | REASON_TYPE = -2, | |
7825 | REASON_PATHS = -3, | |
7826 | REASON_LIMIT = -4, | |
7827 | REASON_STACK = -5, | |
7828 | }; | |
7829 | ||
979d63d5 | 7830 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 7831 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 7832 | { |
7fedb63a | 7833 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
7834 | |
7835 | switch (ptr_reg->type) { | |
7836 | case PTR_TO_STACK: | |
1b1597e6 | 7837 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
7838 | * left direction, see BPF_REG_FP. Also, unknown scalar |
7839 | * offset where we would need to deal with min/max bounds is | |
7840 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
7841 | */ |
7842 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 7843 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 7844 | break; |
979d63d5 | 7845 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 7846 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
7847 | ptr_limit = (mask_to_left ? |
7848 | ptr_reg->smin_value : | |
7849 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 7850 | break; |
979d63d5 | 7851 | default: |
a6aaece0 | 7852 | return REASON_TYPE; |
979d63d5 | 7853 | } |
b658bbb8 DB |
7854 | |
7855 | if (ptr_limit >= max) | |
a6aaece0 | 7856 | return REASON_LIMIT; |
b658bbb8 DB |
7857 | *alu_limit = ptr_limit; |
7858 | return 0; | |
979d63d5 DB |
7859 | } |
7860 | ||
d3bd7413 DB |
7861 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
7862 | const struct bpf_insn *insn) | |
7863 | { | |
2c78ee89 | 7864 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
7865 | } |
7866 | ||
7867 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
7868 | u32 alu_state, u32 alu_limit) | |
7869 | { | |
7870 | /* If we arrived here from different branches with different | |
7871 | * state or limits to sanitize, then this won't work. | |
7872 | */ | |
7873 | if (aux->alu_state && | |
7874 | (aux->alu_state != alu_state || | |
7875 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 7876 | return REASON_PATHS; |
d3bd7413 | 7877 | |
e6ac5933 | 7878 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
7879 | aux->alu_state = alu_state; |
7880 | aux->alu_limit = alu_limit; | |
7881 | return 0; | |
7882 | } | |
7883 | ||
7884 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
7885 | struct bpf_insn *insn) | |
7886 | { | |
7887 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
7888 | ||
7889 | if (can_skip_alu_sanitation(env, insn)) | |
7890 | return 0; | |
7891 | ||
7892 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
7893 | } | |
7894 | ||
f5288193 DB |
7895 | static bool sanitize_needed(u8 opcode) |
7896 | { | |
7897 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
7898 | } | |
7899 | ||
3d0220f6 DB |
7900 | struct bpf_sanitize_info { |
7901 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 7902 | bool mask_to_left; |
3d0220f6 DB |
7903 | }; |
7904 | ||
9183671a DB |
7905 | static struct bpf_verifier_state * |
7906 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
7907 | const struct bpf_insn *insn, | |
7908 | u32 next_idx, u32 curr_idx) | |
7909 | { | |
7910 | struct bpf_verifier_state *branch; | |
7911 | struct bpf_reg_state *regs; | |
7912 | ||
7913 | branch = push_stack(env, next_idx, curr_idx, true); | |
7914 | if (branch && insn) { | |
7915 | regs = branch->frame[branch->curframe]->regs; | |
7916 | if (BPF_SRC(insn->code) == BPF_K) { | |
7917 | mark_reg_unknown(env, regs, insn->dst_reg); | |
7918 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
7919 | mark_reg_unknown(env, regs, insn->dst_reg); | |
7920 | mark_reg_unknown(env, regs, insn->src_reg); | |
7921 | } | |
7922 | } | |
7923 | return branch; | |
7924 | } | |
7925 | ||
979d63d5 DB |
7926 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
7927 | struct bpf_insn *insn, | |
7928 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 7929 | const struct bpf_reg_state *off_reg, |
979d63d5 | 7930 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 7931 | struct bpf_sanitize_info *info, |
7fedb63a | 7932 | const bool commit_window) |
979d63d5 | 7933 | { |
3d0220f6 | 7934 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 7935 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 7936 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 7937 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
7938 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
7939 | u8 opcode = BPF_OP(insn->code); | |
7940 | u32 alu_state, alu_limit; | |
7941 | struct bpf_reg_state tmp; | |
7942 | bool ret; | |
f232326f | 7943 | int err; |
979d63d5 | 7944 | |
d3bd7413 | 7945 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
7946 | return 0; |
7947 | ||
7948 | /* We already marked aux for masking from non-speculative | |
7949 | * paths, thus we got here in the first place. We only care | |
7950 | * to explore bad access from here. | |
7951 | */ | |
7952 | if (vstate->speculative) | |
7953 | goto do_sim; | |
7954 | ||
bb01a1bb DB |
7955 | if (!commit_window) { |
7956 | if (!tnum_is_const(off_reg->var_off) && | |
7957 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
7958 | return REASON_BOUNDS; | |
7959 | ||
7960 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
7961 | (opcode == BPF_SUB && !off_is_neg); | |
7962 | } | |
7963 | ||
7964 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
7965 | if (err < 0) |
7966 | return err; | |
7967 | ||
7fedb63a DB |
7968 | if (commit_window) { |
7969 | /* In commit phase we narrow the masking window based on | |
7970 | * the observed pointer move after the simulated operation. | |
7971 | */ | |
3d0220f6 DB |
7972 | alu_state = info->aux.alu_state; |
7973 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
7974 | } else { |
7975 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 7976 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
7977 | alu_state |= ptr_is_dst_reg ? |
7978 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
7979 | |
7980 | /* Limit pruning on unknown scalars to enable deep search for | |
7981 | * potential masking differences from other program paths. | |
7982 | */ | |
7983 | if (!off_is_imm) | |
7984 | env->explore_alu_limits = true; | |
7fedb63a DB |
7985 | } |
7986 | ||
f232326f PK |
7987 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
7988 | if (err < 0) | |
7989 | return err; | |
979d63d5 | 7990 | do_sim: |
7fedb63a DB |
7991 | /* If we're in commit phase, we're done here given we already |
7992 | * pushed the truncated dst_reg into the speculative verification | |
7993 | * stack. | |
a7036191 DB |
7994 | * |
7995 | * Also, when register is a known constant, we rewrite register-based | |
7996 | * operation to immediate-based, and thus do not need masking (and as | |
7997 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 7998 | */ |
a7036191 | 7999 | if (commit_window || off_is_imm) |
7fedb63a DB |
8000 | return 0; |
8001 | ||
979d63d5 DB |
8002 | /* Simulate and find potential out-of-bounds access under |
8003 | * speculative execution from truncation as a result of | |
8004 | * masking when off was not within expected range. If off | |
8005 | * sits in dst, then we temporarily need to move ptr there | |
8006 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
8007 | * for cases where we use K-based arithmetic in one direction | |
8008 | * and truncated reg-based in the other in order to explore | |
8009 | * bad access. | |
8010 | */ | |
8011 | if (!ptr_is_dst_reg) { | |
8012 | tmp = *dst_reg; | |
8013 | *dst_reg = *ptr_reg; | |
8014 | } | |
9183671a DB |
8015 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
8016 | env->insn_idx); | |
0803278b | 8017 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 8018 | *dst_reg = tmp; |
a6aaece0 DB |
8019 | return !ret ? REASON_STACK : 0; |
8020 | } | |
8021 | ||
fe9a5ca7 DB |
8022 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
8023 | { | |
8024 | struct bpf_verifier_state *vstate = env->cur_state; | |
8025 | ||
8026 | /* If we simulate paths under speculation, we don't update the | |
8027 | * insn as 'seen' such that when we verify unreachable paths in | |
8028 | * the non-speculative domain, sanitize_dead_code() can still | |
8029 | * rewrite/sanitize them. | |
8030 | */ | |
8031 | if (!vstate->speculative) | |
8032 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
8033 | } | |
8034 | ||
a6aaece0 DB |
8035 | static int sanitize_err(struct bpf_verifier_env *env, |
8036 | const struct bpf_insn *insn, int reason, | |
8037 | const struct bpf_reg_state *off_reg, | |
8038 | const struct bpf_reg_state *dst_reg) | |
8039 | { | |
8040 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
8041 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
8042 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
8043 | ||
8044 | switch (reason) { | |
8045 | case REASON_BOUNDS: | |
8046 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
8047 | off_reg == dst_reg ? dst : src, err); | |
8048 | break; | |
8049 | case REASON_TYPE: | |
8050 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
8051 | off_reg == dst_reg ? src : dst, err); | |
8052 | break; | |
8053 | case REASON_PATHS: | |
8054 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
8055 | dst, op, err); | |
8056 | break; | |
8057 | case REASON_LIMIT: | |
8058 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
8059 | dst, op, err); | |
8060 | break; | |
8061 | case REASON_STACK: | |
8062 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
8063 | dst, err); | |
8064 | break; | |
8065 | default: | |
8066 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
8067 | reason); | |
8068 | break; | |
8069 | } | |
8070 | ||
8071 | return -EACCES; | |
979d63d5 DB |
8072 | } |
8073 | ||
01f810ac AM |
8074 | /* check that stack access falls within stack limits and that 'reg' doesn't |
8075 | * have a variable offset. | |
8076 | * | |
8077 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
8078 | * requires corresponding support in Spectre masking for stack ALU. See also | |
8079 | * retrieve_ptr_limit(). | |
8080 | * | |
8081 | * | |
8082 | * 'off' includes 'reg->off'. | |
8083 | */ | |
8084 | static int check_stack_access_for_ptr_arithmetic( | |
8085 | struct bpf_verifier_env *env, | |
8086 | int regno, | |
8087 | const struct bpf_reg_state *reg, | |
8088 | int off) | |
8089 | { | |
8090 | if (!tnum_is_const(reg->var_off)) { | |
8091 | char tn_buf[48]; | |
8092 | ||
8093 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
8094 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
8095 | regno, tn_buf, off); | |
8096 | return -EACCES; | |
8097 | } | |
8098 | ||
8099 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
8100 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
8101 | "prohibited for !root; off=%d\n", regno, off); | |
8102 | return -EACCES; | |
8103 | } | |
8104 | ||
8105 | return 0; | |
8106 | } | |
8107 | ||
073815b7 DB |
8108 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
8109 | const struct bpf_insn *insn, | |
8110 | const struct bpf_reg_state *dst_reg) | |
8111 | { | |
8112 | u32 dst = insn->dst_reg; | |
8113 | ||
8114 | /* For unprivileged we require that resulting offset must be in bounds | |
8115 | * in order to be able to sanitize access later on. | |
8116 | */ | |
8117 | if (env->bypass_spec_v1) | |
8118 | return 0; | |
8119 | ||
8120 | switch (dst_reg->type) { | |
8121 | case PTR_TO_STACK: | |
8122 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
8123 | dst_reg->off + dst_reg->var_off.value)) | |
8124 | return -EACCES; | |
8125 | break; | |
8126 | case PTR_TO_MAP_VALUE: | |
61df10c7 | 8127 | if (check_map_access(env, dst, dst_reg->off, 1, false, ACCESS_HELPER)) { |
073815b7 DB |
8128 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
8129 | "prohibited for !root\n", dst); | |
8130 | return -EACCES; | |
8131 | } | |
8132 | break; | |
8133 | default: | |
8134 | break; | |
8135 | } | |
8136 | ||
8137 | return 0; | |
8138 | } | |
01f810ac | 8139 | |
f1174f77 | 8140 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
8141 | * Caller should also handle BPF_MOV case separately. |
8142 | * If we return -EACCES, caller may want to try again treating pointer as a | |
8143 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
8144 | */ | |
8145 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
8146 | struct bpf_insn *insn, | |
8147 | const struct bpf_reg_state *ptr_reg, | |
8148 | const struct bpf_reg_state *off_reg) | |
969bf05e | 8149 | { |
f4d7e40a AS |
8150 | struct bpf_verifier_state *vstate = env->cur_state; |
8151 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
8152 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 8153 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
8154 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
8155 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
8156 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
8157 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 8158 | struct bpf_sanitize_info info = {}; |
969bf05e | 8159 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 8160 | u32 dst = insn->dst_reg; |
979d63d5 | 8161 | int ret; |
969bf05e | 8162 | |
f1174f77 | 8163 | dst_reg = ®s[dst]; |
969bf05e | 8164 | |
6f16101e DB |
8165 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
8166 | smin_val > smax_val || umin_val > umax_val) { | |
8167 | /* Taint dst register if offset had invalid bounds derived from | |
8168 | * e.g. dead branches. | |
8169 | */ | |
f54c7898 | 8170 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 8171 | return 0; |
f1174f77 EC |
8172 | } |
8173 | ||
8174 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
8175 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
8176 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
8177 | __mark_reg_unknown(env, dst_reg); | |
8178 | return 0; | |
8179 | } | |
8180 | ||
82abbf8d AS |
8181 | verbose(env, |
8182 | "R%d 32-bit pointer arithmetic prohibited\n", | |
8183 | dst); | |
f1174f77 | 8184 | return -EACCES; |
969bf05e AS |
8185 | } |
8186 | ||
c25b2ae1 | 8187 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
aad2eeaf | 8188 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
c25b2ae1 | 8189 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 8190 | return -EACCES; |
c25b2ae1 HL |
8191 | } |
8192 | ||
8193 | switch (base_type(ptr_reg->type)) { | |
aad2eeaf | 8194 | case CONST_PTR_TO_MAP: |
7c696732 YS |
8195 | /* smin_val represents the known value */ |
8196 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
8197 | break; | |
8731745e | 8198 | fallthrough; |
aad2eeaf | 8199 | case PTR_TO_PACKET_END: |
c64b7983 | 8200 | case PTR_TO_SOCKET: |
46f8bc92 | 8201 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 8202 | case PTR_TO_TCP_SOCK: |
fada7fdc | 8203 | case PTR_TO_XDP_SOCK: |
aad2eeaf | 8204 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
c25b2ae1 | 8205 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 8206 | return -EACCES; |
aad2eeaf JS |
8207 | default: |
8208 | break; | |
f1174f77 EC |
8209 | } |
8210 | ||
8211 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
8212 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 8213 | */ |
f1174f77 EC |
8214 | dst_reg->type = ptr_reg->type; |
8215 | dst_reg->id = ptr_reg->id; | |
969bf05e | 8216 | |
bb7f0f98 AS |
8217 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
8218 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
8219 | return -EINVAL; | |
8220 | ||
3f50f132 JF |
8221 | /* pointer types do not carry 32-bit bounds at the moment. */ |
8222 | __mark_reg32_unbounded(dst_reg); | |
8223 | ||
7fedb63a DB |
8224 | if (sanitize_needed(opcode)) { |
8225 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 8226 | &info, false); |
a6aaece0 DB |
8227 | if (ret < 0) |
8228 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 8229 | } |
a6aaece0 | 8230 | |
f1174f77 EC |
8231 | switch (opcode) { |
8232 | case BPF_ADD: | |
8233 | /* We can take a fixed offset as long as it doesn't overflow | |
8234 | * the s32 'off' field | |
969bf05e | 8235 | */ |
b03c9f9f EC |
8236 | if (known && (ptr_reg->off + smin_val == |
8237 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 8238 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
8239 | dst_reg->smin_value = smin_ptr; |
8240 | dst_reg->smax_value = smax_ptr; | |
8241 | dst_reg->umin_value = umin_ptr; | |
8242 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 8243 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 8244 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 8245 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
8246 | break; |
8247 | } | |
f1174f77 EC |
8248 | /* A new variable offset is created. Note that off_reg->off |
8249 | * == 0, since it's a scalar. | |
8250 | * dst_reg gets the pointer type and since some positive | |
8251 | * integer value was added to the pointer, give it a new 'id' | |
8252 | * if it's a PTR_TO_PACKET. | |
8253 | * this creates a new 'base' pointer, off_reg (variable) gets | |
8254 | * added into the variable offset, and we copy the fixed offset | |
8255 | * from ptr_reg. | |
969bf05e | 8256 | */ |
b03c9f9f EC |
8257 | if (signed_add_overflows(smin_ptr, smin_val) || |
8258 | signed_add_overflows(smax_ptr, smax_val)) { | |
8259 | dst_reg->smin_value = S64_MIN; | |
8260 | dst_reg->smax_value = S64_MAX; | |
8261 | } else { | |
8262 | dst_reg->smin_value = smin_ptr + smin_val; | |
8263 | dst_reg->smax_value = smax_ptr + smax_val; | |
8264 | } | |
8265 | if (umin_ptr + umin_val < umin_ptr || | |
8266 | umax_ptr + umax_val < umax_ptr) { | |
8267 | dst_reg->umin_value = 0; | |
8268 | dst_reg->umax_value = U64_MAX; | |
8269 | } else { | |
8270 | dst_reg->umin_value = umin_ptr + umin_val; | |
8271 | dst_reg->umax_value = umax_ptr + umax_val; | |
8272 | } | |
f1174f77 EC |
8273 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
8274 | dst_reg->off = ptr_reg->off; | |
0962590e | 8275 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 8276 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
8277 | dst_reg->id = ++env->id_gen; |
8278 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 8279 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
8280 | } |
8281 | break; | |
8282 | case BPF_SUB: | |
8283 | if (dst_reg == off_reg) { | |
8284 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
8285 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
8286 | dst); | |
f1174f77 EC |
8287 | return -EACCES; |
8288 | } | |
8289 | /* We don't allow subtraction from FP, because (according to | |
8290 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
8291 | * be able to deal with it. | |
969bf05e | 8292 | */ |
f1174f77 | 8293 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
8294 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
8295 | dst); | |
f1174f77 EC |
8296 | return -EACCES; |
8297 | } | |
b03c9f9f EC |
8298 | if (known && (ptr_reg->off - smin_val == |
8299 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 8300 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
8301 | dst_reg->smin_value = smin_ptr; |
8302 | dst_reg->smax_value = smax_ptr; | |
8303 | dst_reg->umin_value = umin_ptr; | |
8304 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
8305 | dst_reg->var_off = ptr_reg->var_off; |
8306 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 8307 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 8308 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
8309 | break; |
8310 | } | |
f1174f77 EC |
8311 | /* A new variable offset is created. If the subtrahend is known |
8312 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 8313 | */ |
b03c9f9f EC |
8314 | if (signed_sub_overflows(smin_ptr, smax_val) || |
8315 | signed_sub_overflows(smax_ptr, smin_val)) { | |
8316 | /* Overflow possible, we know nothing */ | |
8317 | dst_reg->smin_value = S64_MIN; | |
8318 | dst_reg->smax_value = S64_MAX; | |
8319 | } else { | |
8320 | dst_reg->smin_value = smin_ptr - smax_val; | |
8321 | dst_reg->smax_value = smax_ptr - smin_val; | |
8322 | } | |
8323 | if (umin_ptr < umax_val) { | |
8324 | /* Overflow possible, we know nothing */ | |
8325 | dst_reg->umin_value = 0; | |
8326 | dst_reg->umax_value = U64_MAX; | |
8327 | } else { | |
8328 | /* Cannot overflow (as long as bounds are consistent) */ | |
8329 | dst_reg->umin_value = umin_ptr - umax_val; | |
8330 | dst_reg->umax_value = umax_ptr - umin_val; | |
8331 | } | |
f1174f77 EC |
8332 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
8333 | dst_reg->off = ptr_reg->off; | |
0962590e | 8334 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 8335 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
8336 | dst_reg->id = ++env->id_gen; |
8337 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 8338 | if (smin_val < 0) |
22dc4a0f | 8339 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 8340 | } |
f1174f77 EC |
8341 | break; |
8342 | case BPF_AND: | |
8343 | case BPF_OR: | |
8344 | case BPF_XOR: | |
82abbf8d AS |
8345 | /* bitwise ops on pointers are troublesome, prohibit. */ |
8346 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
8347 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
8348 | return -EACCES; |
8349 | default: | |
8350 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
8351 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
8352 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 8353 | return -EACCES; |
43188702 JF |
8354 | } |
8355 | ||
bb7f0f98 AS |
8356 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
8357 | return -EINVAL; | |
3844d153 | 8358 | reg_bounds_sync(dst_reg); |
073815b7 DB |
8359 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
8360 | return -EACCES; | |
7fedb63a DB |
8361 | if (sanitize_needed(opcode)) { |
8362 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 8363 | &info, true); |
7fedb63a DB |
8364 | if (ret < 0) |
8365 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
8366 | } |
8367 | ||
43188702 JF |
8368 | return 0; |
8369 | } | |
8370 | ||
3f50f132 JF |
8371 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
8372 | struct bpf_reg_state *src_reg) | |
8373 | { | |
8374 | s32 smin_val = src_reg->s32_min_value; | |
8375 | s32 smax_val = src_reg->s32_max_value; | |
8376 | u32 umin_val = src_reg->u32_min_value; | |
8377 | u32 umax_val = src_reg->u32_max_value; | |
8378 | ||
8379 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
8380 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
8381 | dst_reg->s32_min_value = S32_MIN; | |
8382 | dst_reg->s32_max_value = S32_MAX; | |
8383 | } else { | |
8384 | dst_reg->s32_min_value += smin_val; | |
8385 | dst_reg->s32_max_value += smax_val; | |
8386 | } | |
8387 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
8388 | dst_reg->u32_max_value + umax_val < umax_val) { | |
8389 | dst_reg->u32_min_value = 0; | |
8390 | dst_reg->u32_max_value = U32_MAX; | |
8391 | } else { | |
8392 | dst_reg->u32_min_value += umin_val; | |
8393 | dst_reg->u32_max_value += umax_val; | |
8394 | } | |
8395 | } | |
8396 | ||
07cd2631 JF |
8397 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
8398 | struct bpf_reg_state *src_reg) | |
8399 | { | |
8400 | s64 smin_val = src_reg->smin_value; | |
8401 | s64 smax_val = src_reg->smax_value; | |
8402 | u64 umin_val = src_reg->umin_value; | |
8403 | u64 umax_val = src_reg->umax_value; | |
8404 | ||
8405 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
8406 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
8407 | dst_reg->smin_value = S64_MIN; | |
8408 | dst_reg->smax_value = S64_MAX; | |
8409 | } else { | |
8410 | dst_reg->smin_value += smin_val; | |
8411 | dst_reg->smax_value += smax_val; | |
8412 | } | |
8413 | if (dst_reg->umin_value + umin_val < umin_val || | |
8414 | dst_reg->umax_value + umax_val < umax_val) { | |
8415 | dst_reg->umin_value = 0; | |
8416 | dst_reg->umax_value = U64_MAX; | |
8417 | } else { | |
8418 | dst_reg->umin_value += umin_val; | |
8419 | dst_reg->umax_value += umax_val; | |
8420 | } | |
3f50f132 JF |
8421 | } |
8422 | ||
8423 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
8424 | struct bpf_reg_state *src_reg) | |
8425 | { | |
8426 | s32 smin_val = src_reg->s32_min_value; | |
8427 | s32 smax_val = src_reg->s32_max_value; | |
8428 | u32 umin_val = src_reg->u32_min_value; | |
8429 | u32 umax_val = src_reg->u32_max_value; | |
8430 | ||
8431 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
8432 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
8433 | /* Overflow possible, we know nothing */ | |
8434 | dst_reg->s32_min_value = S32_MIN; | |
8435 | dst_reg->s32_max_value = S32_MAX; | |
8436 | } else { | |
8437 | dst_reg->s32_min_value -= smax_val; | |
8438 | dst_reg->s32_max_value -= smin_val; | |
8439 | } | |
8440 | if (dst_reg->u32_min_value < umax_val) { | |
8441 | /* Overflow possible, we know nothing */ | |
8442 | dst_reg->u32_min_value = 0; | |
8443 | dst_reg->u32_max_value = U32_MAX; | |
8444 | } else { | |
8445 | /* Cannot overflow (as long as bounds are consistent) */ | |
8446 | dst_reg->u32_min_value -= umax_val; | |
8447 | dst_reg->u32_max_value -= umin_val; | |
8448 | } | |
07cd2631 JF |
8449 | } |
8450 | ||
8451 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
8452 | struct bpf_reg_state *src_reg) | |
8453 | { | |
8454 | s64 smin_val = src_reg->smin_value; | |
8455 | s64 smax_val = src_reg->smax_value; | |
8456 | u64 umin_val = src_reg->umin_value; | |
8457 | u64 umax_val = src_reg->umax_value; | |
8458 | ||
8459 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
8460 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
8461 | /* Overflow possible, we know nothing */ | |
8462 | dst_reg->smin_value = S64_MIN; | |
8463 | dst_reg->smax_value = S64_MAX; | |
8464 | } else { | |
8465 | dst_reg->smin_value -= smax_val; | |
8466 | dst_reg->smax_value -= smin_val; | |
8467 | } | |
8468 | if (dst_reg->umin_value < umax_val) { | |
8469 | /* Overflow possible, we know nothing */ | |
8470 | dst_reg->umin_value = 0; | |
8471 | dst_reg->umax_value = U64_MAX; | |
8472 | } else { | |
8473 | /* Cannot overflow (as long as bounds are consistent) */ | |
8474 | dst_reg->umin_value -= umax_val; | |
8475 | dst_reg->umax_value -= umin_val; | |
8476 | } | |
3f50f132 JF |
8477 | } |
8478 | ||
8479 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
8480 | struct bpf_reg_state *src_reg) | |
8481 | { | |
8482 | s32 smin_val = src_reg->s32_min_value; | |
8483 | u32 umin_val = src_reg->u32_min_value; | |
8484 | u32 umax_val = src_reg->u32_max_value; | |
8485 | ||
8486 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
8487 | /* Ain't nobody got time to multiply that sign */ | |
8488 | __mark_reg32_unbounded(dst_reg); | |
8489 | return; | |
8490 | } | |
8491 | /* Both values are positive, so we can work with unsigned and | |
8492 | * copy the result to signed (unless it exceeds S32_MAX). | |
8493 | */ | |
8494 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
8495 | /* Potential overflow, we know nothing */ | |
8496 | __mark_reg32_unbounded(dst_reg); | |
8497 | return; | |
8498 | } | |
8499 | dst_reg->u32_min_value *= umin_val; | |
8500 | dst_reg->u32_max_value *= umax_val; | |
8501 | if (dst_reg->u32_max_value > S32_MAX) { | |
8502 | /* Overflow possible, we know nothing */ | |
8503 | dst_reg->s32_min_value = S32_MIN; | |
8504 | dst_reg->s32_max_value = S32_MAX; | |
8505 | } else { | |
8506 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
8507 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
8508 | } | |
07cd2631 JF |
8509 | } |
8510 | ||
8511 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
8512 | struct bpf_reg_state *src_reg) | |
8513 | { | |
8514 | s64 smin_val = src_reg->smin_value; | |
8515 | u64 umin_val = src_reg->umin_value; | |
8516 | u64 umax_val = src_reg->umax_value; | |
8517 | ||
07cd2631 JF |
8518 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
8519 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 8520 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
8521 | return; |
8522 | } | |
8523 | /* Both values are positive, so we can work with unsigned and | |
8524 | * copy the result to signed (unless it exceeds S64_MAX). | |
8525 | */ | |
8526 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
8527 | /* Potential overflow, we know nothing */ | |
3f50f132 | 8528 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
8529 | return; |
8530 | } | |
8531 | dst_reg->umin_value *= umin_val; | |
8532 | dst_reg->umax_value *= umax_val; | |
8533 | if (dst_reg->umax_value > S64_MAX) { | |
8534 | /* Overflow possible, we know nothing */ | |
8535 | dst_reg->smin_value = S64_MIN; | |
8536 | dst_reg->smax_value = S64_MAX; | |
8537 | } else { | |
8538 | dst_reg->smin_value = dst_reg->umin_value; | |
8539 | dst_reg->smax_value = dst_reg->umax_value; | |
8540 | } | |
8541 | } | |
8542 | ||
3f50f132 JF |
8543 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
8544 | struct bpf_reg_state *src_reg) | |
8545 | { | |
8546 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
8547 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
8548 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
8549 | s32 smin_val = src_reg->s32_min_value; | |
8550 | u32 umax_val = src_reg->u32_max_value; | |
8551 | ||
049c4e13 DB |
8552 | if (src_known && dst_known) { |
8553 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 8554 | return; |
049c4e13 | 8555 | } |
3f50f132 JF |
8556 | |
8557 | /* We get our minimum from the var_off, since that's inherently | |
8558 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
8559 | */ | |
8560 | dst_reg->u32_min_value = var32_off.value; | |
8561 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
8562 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
8563 | /* Lose signed bounds when ANDing negative numbers, | |
8564 | * ain't nobody got time for that. | |
8565 | */ | |
8566 | dst_reg->s32_min_value = S32_MIN; | |
8567 | dst_reg->s32_max_value = S32_MAX; | |
8568 | } else { | |
8569 | /* ANDing two positives gives a positive, so safe to | |
8570 | * cast result into s64. | |
8571 | */ | |
8572 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
8573 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
8574 | } | |
3f50f132 JF |
8575 | } |
8576 | ||
07cd2631 JF |
8577 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
8578 | struct bpf_reg_state *src_reg) | |
8579 | { | |
3f50f132 JF |
8580 | bool src_known = tnum_is_const(src_reg->var_off); |
8581 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
8582 | s64 smin_val = src_reg->smin_value; |
8583 | u64 umax_val = src_reg->umax_value; | |
8584 | ||
3f50f132 | 8585 | if (src_known && dst_known) { |
4fbb38a3 | 8586 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
8587 | return; |
8588 | } | |
8589 | ||
07cd2631 JF |
8590 | /* We get our minimum from the var_off, since that's inherently |
8591 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
8592 | */ | |
07cd2631 JF |
8593 | dst_reg->umin_value = dst_reg->var_off.value; |
8594 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
8595 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
8596 | /* Lose signed bounds when ANDing negative numbers, | |
8597 | * ain't nobody got time for that. | |
8598 | */ | |
8599 | dst_reg->smin_value = S64_MIN; | |
8600 | dst_reg->smax_value = S64_MAX; | |
8601 | } else { | |
8602 | /* ANDing two positives gives a positive, so safe to | |
8603 | * cast result into s64. | |
8604 | */ | |
8605 | dst_reg->smin_value = dst_reg->umin_value; | |
8606 | dst_reg->smax_value = dst_reg->umax_value; | |
8607 | } | |
8608 | /* We may learn something more from the var_off */ | |
8609 | __update_reg_bounds(dst_reg); | |
8610 | } | |
8611 | ||
3f50f132 JF |
8612 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
8613 | struct bpf_reg_state *src_reg) | |
8614 | { | |
8615 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
8616 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
8617 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
8618 | s32 smin_val = src_reg->s32_min_value; |
8619 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 8620 | |
049c4e13 DB |
8621 | if (src_known && dst_known) { |
8622 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 8623 | return; |
049c4e13 | 8624 | } |
3f50f132 JF |
8625 | |
8626 | /* We get our maximum from the var_off, and our minimum is the | |
8627 | * maximum of the operands' minima | |
8628 | */ | |
8629 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
8630 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
8631 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
8632 | /* Lose signed bounds when ORing negative numbers, | |
8633 | * ain't nobody got time for that. | |
8634 | */ | |
8635 | dst_reg->s32_min_value = S32_MIN; | |
8636 | dst_reg->s32_max_value = S32_MAX; | |
8637 | } else { | |
8638 | /* ORing two positives gives a positive, so safe to | |
8639 | * cast result into s64. | |
8640 | */ | |
5b9fbeb7 DB |
8641 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
8642 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
8643 | } |
8644 | } | |
8645 | ||
07cd2631 JF |
8646 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
8647 | struct bpf_reg_state *src_reg) | |
8648 | { | |
3f50f132 JF |
8649 | bool src_known = tnum_is_const(src_reg->var_off); |
8650 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
8651 | s64 smin_val = src_reg->smin_value; |
8652 | u64 umin_val = src_reg->umin_value; | |
8653 | ||
3f50f132 | 8654 | if (src_known && dst_known) { |
4fbb38a3 | 8655 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
8656 | return; |
8657 | } | |
8658 | ||
07cd2631 JF |
8659 | /* We get our maximum from the var_off, and our minimum is the |
8660 | * maximum of the operands' minima | |
8661 | */ | |
07cd2631 JF |
8662 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
8663 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
8664 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
8665 | /* Lose signed bounds when ORing negative numbers, | |
8666 | * ain't nobody got time for that. | |
8667 | */ | |
8668 | dst_reg->smin_value = S64_MIN; | |
8669 | dst_reg->smax_value = S64_MAX; | |
8670 | } else { | |
8671 | /* ORing two positives gives a positive, so safe to | |
8672 | * cast result into s64. | |
8673 | */ | |
8674 | dst_reg->smin_value = dst_reg->umin_value; | |
8675 | dst_reg->smax_value = dst_reg->umax_value; | |
8676 | } | |
8677 | /* We may learn something more from the var_off */ | |
8678 | __update_reg_bounds(dst_reg); | |
8679 | } | |
8680 | ||
2921c90d YS |
8681 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
8682 | struct bpf_reg_state *src_reg) | |
8683 | { | |
8684 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
8685 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
8686 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
8687 | s32 smin_val = src_reg->s32_min_value; | |
8688 | ||
049c4e13 DB |
8689 | if (src_known && dst_known) { |
8690 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 8691 | return; |
049c4e13 | 8692 | } |
2921c90d YS |
8693 | |
8694 | /* We get both minimum and maximum from the var32_off. */ | |
8695 | dst_reg->u32_min_value = var32_off.value; | |
8696 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
8697 | ||
8698 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
8699 | /* XORing two positive sign numbers gives a positive, | |
8700 | * so safe to cast u32 result into s32. | |
8701 | */ | |
8702 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
8703 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
8704 | } else { | |
8705 | dst_reg->s32_min_value = S32_MIN; | |
8706 | dst_reg->s32_max_value = S32_MAX; | |
8707 | } | |
8708 | } | |
8709 | ||
8710 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
8711 | struct bpf_reg_state *src_reg) | |
8712 | { | |
8713 | bool src_known = tnum_is_const(src_reg->var_off); | |
8714 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
8715 | s64 smin_val = src_reg->smin_value; | |
8716 | ||
8717 | if (src_known && dst_known) { | |
8718 | /* dst_reg->var_off.value has been updated earlier */ | |
8719 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
8720 | return; | |
8721 | } | |
8722 | ||
8723 | /* We get both minimum and maximum from the var_off. */ | |
8724 | dst_reg->umin_value = dst_reg->var_off.value; | |
8725 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
8726 | ||
8727 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
8728 | /* XORing two positive sign numbers gives a positive, | |
8729 | * so safe to cast u64 result into s64. | |
8730 | */ | |
8731 | dst_reg->smin_value = dst_reg->umin_value; | |
8732 | dst_reg->smax_value = dst_reg->umax_value; | |
8733 | } else { | |
8734 | dst_reg->smin_value = S64_MIN; | |
8735 | dst_reg->smax_value = S64_MAX; | |
8736 | } | |
8737 | ||
8738 | __update_reg_bounds(dst_reg); | |
8739 | } | |
8740 | ||
3f50f132 JF |
8741 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
8742 | u64 umin_val, u64 umax_val) | |
07cd2631 | 8743 | { |
07cd2631 JF |
8744 | /* We lose all sign bit information (except what we can pick |
8745 | * up from var_off) | |
8746 | */ | |
3f50f132 JF |
8747 | dst_reg->s32_min_value = S32_MIN; |
8748 | dst_reg->s32_max_value = S32_MAX; | |
8749 | /* If we might shift our top bit out, then we know nothing */ | |
8750 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
8751 | dst_reg->u32_min_value = 0; | |
8752 | dst_reg->u32_max_value = U32_MAX; | |
8753 | } else { | |
8754 | dst_reg->u32_min_value <<= umin_val; | |
8755 | dst_reg->u32_max_value <<= umax_val; | |
8756 | } | |
8757 | } | |
8758 | ||
8759 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
8760 | struct bpf_reg_state *src_reg) | |
8761 | { | |
8762 | u32 umax_val = src_reg->u32_max_value; | |
8763 | u32 umin_val = src_reg->u32_min_value; | |
8764 | /* u32 alu operation will zext upper bits */ | |
8765 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
8766 | ||
8767 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
8768 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
8769 | /* Not required but being careful mark reg64 bounds as unknown so | |
8770 | * that we are forced to pick them up from tnum and zext later and | |
8771 | * if some path skips this step we are still safe. | |
8772 | */ | |
8773 | __mark_reg64_unbounded(dst_reg); | |
8774 | __update_reg32_bounds(dst_reg); | |
8775 | } | |
8776 | ||
8777 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
8778 | u64 umin_val, u64 umax_val) | |
8779 | { | |
8780 | /* Special case <<32 because it is a common compiler pattern to sign | |
8781 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
8782 | * positive we know this shift will also be positive so we can track | |
8783 | * bounds correctly. Otherwise we lose all sign bit information except | |
8784 | * what we can pick up from var_off. Perhaps we can generalize this | |
8785 | * later to shifts of any length. | |
8786 | */ | |
8787 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
8788 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
8789 | else | |
8790 | dst_reg->smax_value = S64_MAX; | |
8791 | ||
8792 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
8793 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
8794 | else | |
8795 | dst_reg->smin_value = S64_MIN; | |
8796 | ||
07cd2631 JF |
8797 | /* If we might shift our top bit out, then we know nothing */ |
8798 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
8799 | dst_reg->umin_value = 0; | |
8800 | dst_reg->umax_value = U64_MAX; | |
8801 | } else { | |
8802 | dst_reg->umin_value <<= umin_val; | |
8803 | dst_reg->umax_value <<= umax_val; | |
8804 | } | |
3f50f132 JF |
8805 | } |
8806 | ||
8807 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
8808 | struct bpf_reg_state *src_reg) | |
8809 | { | |
8810 | u64 umax_val = src_reg->umax_value; | |
8811 | u64 umin_val = src_reg->umin_value; | |
8812 | ||
8813 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
8814 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
8815 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
8816 | ||
07cd2631 JF |
8817 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
8818 | /* We may learn something more from the var_off */ | |
8819 | __update_reg_bounds(dst_reg); | |
8820 | } | |
8821 | ||
3f50f132 JF |
8822 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
8823 | struct bpf_reg_state *src_reg) | |
8824 | { | |
8825 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
8826 | u32 umax_val = src_reg->u32_max_value; | |
8827 | u32 umin_val = src_reg->u32_min_value; | |
8828 | ||
8829 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
8830 | * be negative, then either: | |
8831 | * 1) src_reg might be zero, so the sign bit of the result is | |
8832 | * unknown, so we lose our signed bounds | |
8833 | * 2) it's known negative, thus the unsigned bounds capture the | |
8834 | * signed bounds | |
8835 | * 3) the signed bounds cross zero, so they tell us nothing | |
8836 | * about the result | |
8837 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 8838 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
8839 | * Thus, in all cases it suffices to blow away our signed bounds |
8840 | * and rely on inferring new ones from the unsigned bounds and | |
8841 | * var_off of the result. | |
8842 | */ | |
8843 | dst_reg->s32_min_value = S32_MIN; | |
8844 | dst_reg->s32_max_value = S32_MAX; | |
8845 | ||
8846 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
8847 | dst_reg->u32_min_value >>= umax_val; | |
8848 | dst_reg->u32_max_value >>= umin_val; | |
8849 | ||
8850 | __mark_reg64_unbounded(dst_reg); | |
8851 | __update_reg32_bounds(dst_reg); | |
8852 | } | |
8853 | ||
07cd2631 JF |
8854 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
8855 | struct bpf_reg_state *src_reg) | |
8856 | { | |
8857 | u64 umax_val = src_reg->umax_value; | |
8858 | u64 umin_val = src_reg->umin_value; | |
8859 | ||
8860 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
8861 | * be negative, then either: | |
8862 | * 1) src_reg might be zero, so the sign bit of the result is | |
8863 | * unknown, so we lose our signed bounds | |
8864 | * 2) it's known negative, thus the unsigned bounds capture the | |
8865 | * signed bounds | |
8866 | * 3) the signed bounds cross zero, so they tell us nothing | |
8867 | * about the result | |
8868 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 8869 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
8870 | * Thus, in all cases it suffices to blow away our signed bounds |
8871 | * and rely on inferring new ones from the unsigned bounds and | |
8872 | * var_off of the result. | |
8873 | */ | |
8874 | dst_reg->smin_value = S64_MIN; | |
8875 | dst_reg->smax_value = S64_MAX; | |
8876 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
8877 | dst_reg->umin_value >>= umax_val; | |
8878 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
8879 | |
8880 | /* Its not easy to operate on alu32 bounds here because it depends | |
8881 | * on bits being shifted in. Take easy way out and mark unbounded | |
8882 | * so we can recalculate later from tnum. | |
8883 | */ | |
8884 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
8885 | __update_reg_bounds(dst_reg); |
8886 | } | |
8887 | ||
3f50f132 JF |
8888 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
8889 | struct bpf_reg_state *src_reg) | |
07cd2631 | 8890 | { |
3f50f132 | 8891 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
8892 | |
8893 | /* Upon reaching here, src_known is true and | |
8894 | * umax_val is equal to umin_val. | |
8895 | */ | |
3f50f132 JF |
8896 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
8897 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 8898 | |
3f50f132 JF |
8899 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
8900 | ||
8901 | /* blow away the dst_reg umin_value/umax_value and rely on | |
8902 | * dst_reg var_off to refine the result. | |
8903 | */ | |
8904 | dst_reg->u32_min_value = 0; | |
8905 | dst_reg->u32_max_value = U32_MAX; | |
8906 | ||
8907 | __mark_reg64_unbounded(dst_reg); | |
8908 | __update_reg32_bounds(dst_reg); | |
8909 | } | |
8910 | ||
8911 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
8912 | struct bpf_reg_state *src_reg) | |
8913 | { | |
8914 | u64 umin_val = src_reg->umin_value; | |
8915 | ||
8916 | /* Upon reaching here, src_known is true and umax_val is equal | |
8917 | * to umin_val. | |
8918 | */ | |
8919 | dst_reg->smin_value >>= umin_val; | |
8920 | dst_reg->smax_value >>= umin_val; | |
8921 | ||
8922 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
8923 | |
8924 | /* blow away the dst_reg umin_value/umax_value and rely on | |
8925 | * dst_reg var_off to refine the result. | |
8926 | */ | |
8927 | dst_reg->umin_value = 0; | |
8928 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
8929 | |
8930 | /* Its not easy to operate on alu32 bounds here because it depends | |
8931 | * on bits being shifted in from upper 32-bits. Take easy way out | |
8932 | * and mark unbounded so we can recalculate later from tnum. | |
8933 | */ | |
8934 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
8935 | __update_reg_bounds(dst_reg); |
8936 | } | |
8937 | ||
468f6eaf JH |
8938 | /* WARNING: This function does calculations on 64-bit values, but the actual |
8939 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
8940 | * need extra checks in the 32-bit case. | |
8941 | */ | |
f1174f77 EC |
8942 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
8943 | struct bpf_insn *insn, | |
8944 | struct bpf_reg_state *dst_reg, | |
8945 | struct bpf_reg_state src_reg) | |
969bf05e | 8946 | { |
638f5b90 | 8947 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 8948 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 8949 | bool src_known; |
b03c9f9f EC |
8950 | s64 smin_val, smax_val; |
8951 | u64 umin_val, umax_val; | |
3f50f132 JF |
8952 | s32 s32_min_val, s32_max_val; |
8953 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 8954 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 8955 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 8956 | int ret; |
b799207e | 8957 | |
b03c9f9f EC |
8958 | smin_val = src_reg.smin_value; |
8959 | smax_val = src_reg.smax_value; | |
8960 | umin_val = src_reg.umin_value; | |
8961 | umax_val = src_reg.umax_value; | |
f23cc643 | 8962 | |
3f50f132 JF |
8963 | s32_min_val = src_reg.s32_min_value; |
8964 | s32_max_val = src_reg.s32_max_value; | |
8965 | u32_min_val = src_reg.u32_min_value; | |
8966 | u32_max_val = src_reg.u32_max_value; | |
8967 | ||
8968 | if (alu32) { | |
8969 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
8970 | if ((src_known && |
8971 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
8972 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
8973 | /* Taint dst register if offset had invalid bounds | |
8974 | * derived from e.g. dead branches. | |
8975 | */ | |
8976 | __mark_reg_unknown(env, dst_reg); | |
8977 | return 0; | |
8978 | } | |
8979 | } else { | |
8980 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
8981 | if ((src_known && |
8982 | (smin_val != smax_val || umin_val != umax_val)) || | |
8983 | smin_val > smax_val || umin_val > umax_val) { | |
8984 | /* Taint dst register if offset had invalid bounds | |
8985 | * derived from e.g. dead branches. | |
8986 | */ | |
8987 | __mark_reg_unknown(env, dst_reg); | |
8988 | return 0; | |
8989 | } | |
6f16101e DB |
8990 | } |
8991 | ||
bb7f0f98 AS |
8992 | if (!src_known && |
8993 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 8994 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
8995 | return 0; |
8996 | } | |
8997 | ||
f5288193 DB |
8998 | if (sanitize_needed(opcode)) { |
8999 | ret = sanitize_val_alu(env, insn); | |
9000 | if (ret < 0) | |
9001 | return sanitize_err(env, insn, ret, NULL, NULL); | |
9002 | } | |
9003 | ||
3f50f132 JF |
9004 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
9005 | * There are two classes of instructions: The first class we track both | |
9006 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
9007 | * greatest amount of precision when alu operations are mixed with jmp32 | |
9008 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
9009 | * and BPF_OR. This is possible because these ops have fairly easy to | |
9010 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
9011 | * See alu32 verifier tests for examples. The second class of | |
9012 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
9013 | * with regards to tracking sign/unsigned bounds because the bits may | |
9014 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
9015 | * the reg unbounded in the subreg bound space and use the resulting | |
9016 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
9017 | */ | |
48461135 JB |
9018 | switch (opcode) { |
9019 | case BPF_ADD: | |
3f50f132 | 9020 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 9021 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 9022 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
9023 | break; |
9024 | case BPF_SUB: | |
3f50f132 | 9025 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 9026 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 9027 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
9028 | break; |
9029 | case BPF_MUL: | |
3f50f132 JF |
9030 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
9031 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 9032 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
9033 | break; |
9034 | case BPF_AND: | |
3f50f132 JF |
9035 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
9036 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 9037 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
9038 | break; |
9039 | case BPF_OR: | |
3f50f132 JF |
9040 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
9041 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 9042 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 9043 | break; |
2921c90d YS |
9044 | case BPF_XOR: |
9045 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
9046 | scalar32_min_max_xor(dst_reg, &src_reg); | |
9047 | scalar_min_max_xor(dst_reg, &src_reg); | |
9048 | break; | |
48461135 | 9049 | case BPF_LSH: |
468f6eaf JH |
9050 | if (umax_val >= insn_bitness) { |
9051 | /* Shifts greater than 31 or 63 are undefined. | |
9052 | * This includes shifts by a negative number. | |
b03c9f9f | 9053 | */ |
61bd5218 | 9054 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
9055 | break; |
9056 | } | |
3f50f132 JF |
9057 | if (alu32) |
9058 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
9059 | else | |
9060 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
9061 | break; |
9062 | case BPF_RSH: | |
468f6eaf JH |
9063 | if (umax_val >= insn_bitness) { |
9064 | /* Shifts greater than 31 or 63 are undefined. | |
9065 | * This includes shifts by a negative number. | |
b03c9f9f | 9066 | */ |
61bd5218 | 9067 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
9068 | break; |
9069 | } | |
3f50f132 JF |
9070 | if (alu32) |
9071 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
9072 | else | |
9073 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 9074 | break; |
9cbe1f5a YS |
9075 | case BPF_ARSH: |
9076 | if (umax_val >= insn_bitness) { | |
9077 | /* Shifts greater than 31 or 63 are undefined. | |
9078 | * This includes shifts by a negative number. | |
9079 | */ | |
9080 | mark_reg_unknown(env, regs, insn->dst_reg); | |
9081 | break; | |
9082 | } | |
3f50f132 JF |
9083 | if (alu32) |
9084 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
9085 | else | |
9086 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 9087 | break; |
48461135 | 9088 | default: |
61bd5218 | 9089 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
9090 | break; |
9091 | } | |
9092 | ||
3f50f132 JF |
9093 | /* ALU32 ops are zero extended into 64bit register */ |
9094 | if (alu32) | |
9095 | zext_32_to_64(dst_reg); | |
3844d153 | 9096 | reg_bounds_sync(dst_reg); |
f1174f77 EC |
9097 | return 0; |
9098 | } | |
9099 | ||
9100 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
9101 | * and var_off. | |
9102 | */ | |
9103 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
9104 | struct bpf_insn *insn) | |
9105 | { | |
f4d7e40a AS |
9106 | struct bpf_verifier_state *vstate = env->cur_state; |
9107 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
9108 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
9109 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
9110 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 9111 | int err; |
f1174f77 EC |
9112 | |
9113 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
9114 | src_reg = NULL; |
9115 | if (dst_reg->type != SCALAR_VALUE) | |
9116 | ptr_reg = dst_reg; | |
75748837 AS |
9117 | else |
9118 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
9119 | * incorrectly propagated into other registers by find_equal_scalars() | |
9120 | */ | |
9121 | dst_reg->id = 0; | |
f1174f77 EC |
9122 | if (BPF_SRC(insn->code) == BPF_X) { |
9123 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
9124 | if (src_reg->type != SCALAR_VALUE) { |
9125 | if (dst_reg->type != SCALAR_VALUE) { | |
9126 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
9127 | * an arbitrary scalar. Disallow all math except |
9128 | * pointer subtraction | |
f1174f77 | 9129 | */ |
dd066823 | 9130 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
9131 | mark_reg_unknown(env, regs, insn->dst_reg); |
9132 | return 0; | |
f1174f77 | 9133 | } |
82abbf8d AS |
9134 | verbose(env, "R%d pointer %s pointer prohibited\n", |
9135 | insn->dst_reg, | |
9136 | bpf_alu_string[opcode >> 4]); | |
9137 | return -EACCES; | |
f1174f77 EC |
9138 | } else { |
9139 | /* scalar += pointer | |
9140 | * This is legal, but we have to reverse our | |
9141 | * src/dest handling in computing the range | |
9142 | */ | |
b5dc0163 AS |
9143 | err = mark_chain_precision(env, insn->dst_reg); |
9144 | if (err) | |
9145 | return err; | |
82abbf8d AS |
9146 | return adjust_ptr_min_max_vals(env, insn, |
9147 | src_reg, dst_reg); | |
f1174f77 EC |
9148 | } |
9149 | } else if (ptr_reg) { | |
9150 | /* pointer += scalar */ | |
b5dc0163 AS |
9151 | err = mark_chain_precision(env, insn->src_reg); |
9152 | if (err) | |
9153 | return err; | |
82abbf8d AS |
9154 | return adjust_ptr_min_max_vals(env, insn, |
9155 | dst_reg, src_reg); | |
a3b666bf AN |
9156 | } else if (dst_reg->precise) { |
9157 | /* if dst_reg is precise, src_reg should be precise as well */ | |
9158 | err = mark_chain_precision(env, insn->src_reg); | |
9159 | if (err) | |
9160 | return err; | |
f1174f77 EC |
9161 | } |
9162 | } else { | |
9163 | /* Pretend the src is a reg with a known value, since we only | |
9164 | * need to be able to read from this state. | |
9165 | */ | |
9166 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 9167 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 9168 | src_reg = &off_reg; |
82abbf8d AS |
9169 | if (ptr_reg) /* pointer += K */ |
9170 | return adjust_ptr_min_max_vals(env, insn, | |
9171 | ptr_reg, src_reg); | |
f1174f77 EC |
9172 | } |
9173 | ||
9174 | /* Got here implies adding two SCALAR_VALUEs */ | |
9175 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 9176 | print_verifier_state(env, state, true); |
61bd5218 | 9177 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
9178 | return -EINVAL; |
9179 | } | |
9180 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 9181 | print_verifier_state(env, state, true); |
61bd5218 | 9182 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
9183 | return -EINVAL; |
9184 | } | |
9185 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
9186 | } |
9187 | ||
17a52670 | 9188 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 9189 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 9190 | { |
638f5b90 | 9191 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
9192 | u8 opcode = BPF_OP(insn->code); |
9193 | int err; | |
9194 | ||
9195 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
9196 | if (opcode == BPF_NEG) { | |
395e942d | 9197 | if (BPF_SRC(insn->code) != BPF_K || |
17a52670 AS |
9198 | insn->src_reg != BPF_REG_0 || |
9199 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 9200 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
9201 | return -EINVAL; |
9202 | } | |
9203 | } else { | |
9204 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
9205 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
9206 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 9207 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
9208 | return -EINVAL; |
9209 | } | |
9210 | } | |
9211 | ||
9212 | /* check src operand */ | |
dc503a8a | 9213 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9214 | if (err) |
9215 | return err; | |
9216 | ||
1be7f75d | 9217 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 9218 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
9219 | insn->dst_reg); |
9220 | return -EACCES; | |
9221 | } | |
9222 | ||
17a52670 | 9223 | /* check dest operand */ |
dc503a8a | 9224 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
9225 | if (err) |
9226 | return err; | |
9227 | ||
9228 | } else if (opcode == BPF_MOV) { | |
9229 | ||
9230 | if (BPF_SRC(insn->code) == BPF_X) { | |
9231 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 9232 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
9233 | return -EINVAL; |
9234 | } | |
9235 | ||
9236 | /* check src operand */ | |
dc503a8a | 9237 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9238 | if (err) |
9239 | return err; | |
9240 | } else { | |
9241 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 9242 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
9243 | return -EINVAL; |
9244 | } | |
9245 | } | |
9246 | ||
fbeb1603 AF |
9247 | /* check dest operand, mark as required later */ |
9248 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
9249 | if (err) |
9250 | return err; | |
9251 | ||
9252 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
9253 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
9254 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
9255 | ||
17a52670 AS |
9256 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
9257 | /* case: R1 = R2 | |
9258 | * copy register state to dest reg | |
9259 | */ | |
75748837 AS |
9260 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
9261 | /* Assign src and dst registers the same ID | |
9262 | * that will be used by find_equal_scalars() | |
9263 | * to propagate min/max range. | |
9264 | */ | |
9265 | src_reg->id = ++env->id_gen; | |
e434b8cd JW |
9266 | *dst_reg = *src_reg; |
9267 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 9268 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 9269 | } else { |
f1174f77 | 9270 | /* R1 = (u32) R2 */ |
1be7f75d | 9271 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
9272 | verbose(env, |
9273 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
9274 | insn->src_reg); |
9275 | return -EACCES; | |
e434b8cd JW |
9276 | } else if (src_reg->type == SCALAR_VALUE) { |
9277 | *dst_reg = *src_reg; | |
75748837 AS |
9278 | /* Make sure ID is cleared otherwise |
9279 | * dst_reg min/max could be incorrectly | |
9280 | * propagated into src_reg by find_equal_scalars() | |
9281 | */ | |
9282 | dst_reg->id = 0; | |
e434b8cd | 9283 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 9284 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
9285 | } else { |
9286 | mark_reg_unknown(env, regs, | |
9287 | insn->dst_reg); | |
1be7f75d | 9288 | } |
3f50f132 | 9289 | zext_32_to_64(dst_reg); |
3844d153 | 9290 | reg_bounds_sync(dst_reg); |
17a52670 AS |
9291 | } |
9292 | } else { | |
9293 | /* case: R = imm | |
9294 | * remember the value we stored into this reg | |
9295 | */ | |
fbeb1603 AF |
9296 | /* clear any state __mark_reg_known doesn't set */ |
9297 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 9298 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
9299 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
9300 | __mark_reg_known(regs + insn->dst_reg, | |
9301 | insn->imm); | |
9302 | } else { | |
9303 | __mark_reg_known(regs + insn->dst_reg, | |
9304 | (u32)insn->imm); | |
9305 | } | |
17a52670 AS |
9306 | } |
9307 | ||
9308 | } else if (opcode > BPF_END) { | |
61bd5218 | 9309 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
9310 | return -EINVAL; |
9311 | ||
9312 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
9313 | ||
17a52670 AS |
9314 | if (BPF_SRC(insn->code) == BPF_X) { |
9315 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 9316 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
9317 | return -EINVAL; |
9318 | } | |
9319 | /* check src1 operand */ | |
dc503a8a | 9320 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9321 | if (err) |
9322 | return err; | |
9323 | } else { | |
9324 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 9325 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
9326 | return -EINVAL; |
9327 | } | |
9328 | } | |
9329 | ||
9330 | /* check src2 operand */ | |
dc503a8a | 9331 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9332 | if (err) |
9333 | return err; | |
9334 | ||
9335 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
9336 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 9337 | verbose(env, "div by zero\n"); |
17a52670 AS |
9338 | return -EINVAL; |
9339 | } | |
9340 | ||
229394e8 RV |
9341 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
9342 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
9343 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
9344 | ||
9345 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 9346 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
9347 | return -EINVAL; |
9348 | } | |
9349 | } | |
9350 | ||
1a0dc1ac | 9351 | /* check dest operand */ |
dc503a8a | 9352 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
9353 | if (err) |
9354 | return err; | |
9355 | ||
f1174f77 | 9356 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
9357 | } |
9358 | ||
9359 | return 0; | |
9360 | } | |
9361 | ||
f4d7e40a | 9362 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 9363 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 9364 | enum bpf_reg_type type, |
fb2a311a | 9365 | bool range_right_open) |
969bf05e | 9366 | { |
b239da34 KKD |
9367 | struct bpf_func_state *state; |
9368 | struct bpf_reg_state *reg; | |
9369 | int new_range; | |
2d2be8ca | 9370 | |
fb2a311a DB |
9371 | if (dst_reg->off < 0 || |
9372 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
9373 | /* This doesn't give us any range */ |
9374 | return; | |
9375 | ||
b03c9f9f EC |
9376 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
9377 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
9378 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
9379 | * than pkt_end, but that's because it's also less than pkt. | |
9380 | */ | |
9381 | return; | |
9382 | ||
fb2a311a DB |
9383 | new_range = dst_reg->off; |
9384 | if (range_right_open) | |
2fa7d94a | 9385 | new_range++; |
fb2a311a DB |
9386 | |
9387 | /* Examples for register markings: | |
2d2be8ca | 9388 | * |
fb2a311a | 9389 | * pkt_data in dst register: |
2d2be8ca DB |
9390 | * |
9391 | * r2 = r3; | |
9392 | * r2 += 8; | |
9393 | * if (r2 > pkt_end) goto <handle exception> | |
9394 | * <access okay> | |
9395 | * | |
b4e432f1 DB |
9396 | * r2 = r3; |
9397 | * r2 += 8; | |
9398 | * if (r2 < pkt_end) goto <access okay> | |
9399 | * <handle exception> | |
9400 | * | |
2d2be8ca DB |
9401 | * Where: |
9402 | * r2 == dst_reg, pkt_end == src_reg | |
9403 | * r2=pkt(id=n,off=8,r=0) | |
9404 | * r3=pkt(id=n,off=0,r=0) | |
9405 | * | |
fb2a311a | 9406 | * pkt_data in src register: |
2d2be8ca DB |
9407 | * |
9408 | * r2 = r3; | |
9409 | * r2 += 8; | |
9410 | * if (pkt_end >= r2) goto <access okay> | |
9411 | * <handle exception> | |
9412 | * | |
b4e432f1 DB |
9413 | * r2 = r3; |
9414 | * r2 += 8; | |
9415 | * if (pkt_end <= r2) goto <handle exception> | |
9416 | * <access okay> | |
9417 | * | |
2d2be8ca DB |
9418 | * Where: |
9419 | * pkt_end == dst_reg, r2 == src_reg | |
9420 | * r2=pkt(id=n,off=8,r=0) | |
9421 | * r3=pkt(id=n,off=0,r=0) | |
9422 | * | |
9423 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
9424 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
9425 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
9426 | * the check. | |
969bf05e | 9427 | */ |
2d2be8ca | 9428 | |
f1174f77 EC |
9429 | /* If our ids match, then we must have the same max_value. And we |
9430 | * don't care about the other reg's fixed offset, since if it's too big | |
9431 | * the range won't allow anything. | |
9432 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
9433 | */ | |
b239da34 KKD |
9434 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
9435 | if (reg->type == type && reg->id == dst_reg->id) | |
9436 | /* keep the maximum range already checked */ | |
9437 | reg->range = max(reg->range, new_range); | |
9438 | })); | |
969bf05e AS |
9439 | } |
9440 | ||
3f50f132 | 9441 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 9442 | { |
3f50f132 JF |
9443 | struct tnum subreg = tnum_subreg(reg->var_off); |
9444 | s32 sval = (s32)val; | |
a72dafaf | 9445 | |
3f50f132 JF |
9446 | switch (opcode) { |
9447 | case BPF_JEQ: | |
9448 | if (tnum_is_const(subreg)) | |
9449 | return !!tnum_equals_const(subreg, val); | |
9450 | break; | |
9451 | case BPF_JNE: | |
9452 | if (tnum_is_const(subreg)) | |
9453 | return !tnum_equals_const(subreg, val); | |
9454 | break; | |
9455 | case BPF_JSET: | |
9456 | if ((~subreg.mask & subreg.value) & val) | |
9457 | return 1; | |
9458 | if (!((subreg.mask | subreg.value) & val)) | |
9459 | return 0; | |
9460 | break; | |
9461 | case BPF_JGT: | |
9462 | if (reg->u32_min_value > val) | |
9463 | return 1; | |
9464 | else if (reg->u32_max_value <= val) | |
9465 | return 0; | |
9466 | break; | |
9467 | case BPF_JSGT: | |
9468 | if (reg->s32_min_value > sval) | |
9469 | return 1; | |
ee114dd6 | 9470 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
9471 | return 0; |
9472 | break; | |
9473 | case BPF_JLT: | |
9474 | if (reg->u32_max_value < val) | |
9475 | return 1; | |
9476 | else if (reg->u32_min_value >= val) | |
9477 | return 0; | |
9478 | break; | |
9479 | case BPF_JSLT: | |
9480 | if (reg->s32_max_value < sval) | |
9481 | return 1; | |
9482 | else if (reg->s32_min_value >= sval) | |
9483 | return 0; | |
9484 | break; | |
9485 | case BPF_JGE: | |
9486 | if (reg->u32_min_value >= val) | |
9487 | return 1; | |
9488 | else if (reg->u32_max_value < val) | |
9489 | return 0; | |
9490 | break; | |
9491 | case BPF_JSGE: | |
9492 | if (reg->s32_min_value >= sval) | |
9493 | return 1; | |
9494 | else if (reg->s32_max_value < sval) | |
9495 | return 0; | |
9496 | break; | |
9497 | case BPF_JLE: | |
9498 | if (reg->u32_max_value <= val) | |
9499 | return 1; | |
9500 | else if (reg->u32_min_value > val) | |
9501 | return 0; | |
9502 | break; | |
9503 | case BPF_JSLE: | |
9504 | if (reg->s32_max_value <= sval) | |
9505 | return 1; | |
9506 | else if (reg->s32_min_value > sval) | |
9507 | return 0; | |
9508 | break; | |
9509 | } | |
4f7b3e82 | 9510 | |
3f50f132 JF |
9511 | return -1; |
9512 | } | |
092ed096 | 9513 | |
3f50f132 JF |
9514 | |
9515 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
9516 | { | |
9517 | s64 sval = (s64)val; | |
a72dafaf | 9518 | |
4f7b3e82 AS |
9519 | switch (opcode) { |
9520 | case BPF_JEQ: | |
9521 | if (tnum_is_const(reg->var_off)) | |
9522 | return !!tnum_equals_const(reg->var_off, val); | |
9523 | break; | |
9524 | case BPF_JNE: | |
9525 | if (tnum_is_const(reg->var_off)) | |
9526 | return !tnum_equals_const(reg->var_off, val); | |
9527 | break; | |
960ea056 JK |
9528 | case BPF_JSET: |
9529 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
9530 | return 1; | |
9531 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
9532 | return 0; | |
9533 | break; | |
4f7b3e82 AS |
9534 | case BPF_JGT: |
9535 | if (reg->umin_value > val) | |
9536 | return 1; | |
9537 | else if (reg->umax_value <= val) | |
9538 | return 0; | |
9539 | break; | |
9540 | case BPF_JSGT: | |
a72dafaf | 9541 | if (reg->smin_value > sval) |
4f7b3e82 | 9542 | return 1; |
ee114dd6 | 9543 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
9544 | return 0; |
9545 | break; | |
9546 | case BPF_JLT: | |
9547 | if (reg->umax_value < val) | |
9548 | return 1; | |
9549 | else if (reg->umin_value >= val) | |
9550 | return 0; | |
9551 | break; | |
9552 | case BPF_JSLT: | |
a72dafaf | 9553 | if (reg->smax_value < sval) |
4f7b3e82 | 9554 | return 1; |
a72dafaf | 9555 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
9556 | return 0; |
9557 | break; | |
9558 | case BPF_JGE: | |
9559 | if (reg->umin_value >= val) | |
9560 | return 1; | |
9561 | else if (reg->umax_value < val) | |
9562 | return 0; | |
9563 | break; | |
9564 | case BPF_JSGE: | |
a72dafaf | 9565 | if (reg->smin_value >= sval) |
4f7b3e82 | 9566 | return 1; |
a72dafaf | 9567 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
9568 | return 0; |
9569 | break; | |
9570 | case BPF_JLE: | |
9571 | if (reg->umax_value <= val) | |
9572 | return 1; | |
9573 | else if (reg->umin_value > val) | |
9574 | return 0; | |
9575 | break; | |
9576 | case BPF_JSLE: | |
a72dafaf | 9577 | if (reg->smax_value <= sval) |
4f7b3e82 | 9578 | return 1; |
a72dafaf | 9579 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
9580 | return 0; |
9581 | break; | |
9582 | } | |
9583 | ||
9584 | return -1; | |
9585 | } | |
9586 | ||
3f50f132 JF |
9587 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
9588 | * and return: | |
9589 | * 1 - branch will be taken and "goto target" will be executed | |
9590 | * 0 - branch will not be taken and fall-through to next insn | |
9591 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
9592 | * range [0,10] | |
604dca5e | 9593 | */ |
3f50f132 JF |
9594 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
9595 | bool is_jmp32) | |
604dca5e | 9596 | { |
cac616db JF |
9597 | if (__is_pointer_value(false, reg)) { |
9598 | if (!reg_type_not_null(reg->type)) | |
9599 | return -1; | |
9600 | ||
9601 | /* If pointer is valid tests against zero will fail so we can | |
9602 | * use this to direct branch taken. | |
9603 | */ | |
9604 | if (val != 0) | |
9605 | return -1; | |
9606 | ||
9607 | switch (opcode) { | |
9608 | case BPF_JEQ: | |
9609 | return 0; | |
9610 | case BPF_JNE: | |
9611 | return 1; | |
9612 | default: | |
9613 | return -1; | |
9614 | } | |
9615 | } | |
604dca5e | 9616 | |
3f50f132 JF |
9617 | if (is_jmp32) |
9618 | return is_branch32_taken(reg, val, opcode); | |
9619 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
9620 | } |
9621 | ||
6d94e741 AS |
9622 | static int flip_opcode(u32 opcode) |
9623 | { | |
9624 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
9625 | static const u8 opcode_flip[16] = { | |
9626 | /* these stay the same */ | |
9627 | [BPF_JEQ >> 4] = BPF_JEQ, | |
9628 | [BPF_JNE >> 4] = BPF_JNE, | |
9629 | [BPF_JSET >> 4] = BPF_JSET, | |
9630 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
9631 | [BPF_JGE >> 4] = BPF_JLE, | |
9632 | [BPF_JGT >> 4] = BPF_JLT, | |
9633 | [BPF_JLE >> 4] = BPF_JGE, | |
9634 | [BPF_JLT >> 4] = BPF_JGT, | |
9635 | [BPF_JSGE >> 4] = BPF_JSLE, | |
9636 | [BPF_JSGT >> 4] = BPF_JSLT, | |
9637 | [BPF_JSLE >> 4] = BPF_JSGE, | |
9638 | [BPF_JSLT >> 4] = BPF_JSGT | |
9639 | }; | |
9640 | return opcode_flip[opcode >> 4]; | |
9641 | } | |
9642 | ||
9643 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
9644 | struct bpf_reg_state *src_reg, | |
9645 | u8 opcode) | |
9646 | { | |
9647 | struct bpf_reg_state *pkt; | |
9648 | ||
9649 | if (src_reg->type == PTR_TO_PACKET_END) { | |
9650 | pkt = dst_reg; | |
9651 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
9652 | pkt = src_reg; | |
9653 | opcode = flip_opcode(opcode); | |
9654 | } else { | |
9655 | return -1; | |
9656 | } | |
9657 | ||
9658 | if (pkt->range >= 0) | |
9659 | return -1; | |
9660 | ||
9661 | switch (opcode) { | |
9662 | case BPF_JLE: | |
9663 | /* pkt <= pkt_end */ | |
9664 | fallthrough; | |
9665 | case BPF_JGT: | |
9666 | /* pkt > pkt_end */ | |
9667 | if (pkt->range == BEYOND_PKT_END) | |
9668 | /* pkt has at last one extra byte beyond pkt_end */ | |
9669 | return opcode == BPF_JGT; | |
9670 | break; | |
9671 | case BPF_JLT: | |
9672 | /* pkt < pkt_end */ | |
9673 | fallthrough; | |
9674 | case BPF_JGE: | |
9675 | /* pkt >= pkt_end */ | |
9676 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
9677 | return opcode == BPF_JGE; | |
9678 | break; | |
9679 | } | |
9680 | return -1; | |
9681 | } | |
9682 | ||
48461135 JB |
9683 | /* Adjusts the register min/max values in the case that the dst_reg is the |
9684 | * variable register that we are working on, and src_reg is a constant or we're | |
9685 | * simply doing a BPF_K check. | |
f1174f77 | 9686 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
9687 | */ |
9688 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
9689 | struct bpf_reg_state *false_reg, |
9690 | u64 val, u32 val32, | |
092ed096 | 9691 | u8 opcode, bool is_jmp32) |
48461135 | 9692 | { |
3f50f132 JF |
9693 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
9694 | struct tnum false_64off = false_reg->var_off; | |
9695 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
9696 | struct tnum true_64off = true_reg->var_off; | |
9697 | s64 sval = (s64)val; | |
9698 | s32 sval32 = (s32)val32; | |
a72dafaf | 9699 | |
f1174f77 EC |
9700 | /* If the dst_reg is a pointer, we can't learn anything about its |
9701 | * variable offset from the compare (unless src_reg were a pointer into | |
9702 | * the same object, but we don't bother with that. | |
9703 | * Since false_reg and true_reg have the same type by construction, we | |
9704 | * only need to check one of them for pointerness. | |
9705 | */ | |
9706 | if (__is_pointer_value(false, false_reg)) | |
9707 | return; | |
4cabc5b1 | 9708 | |
48461135 | 9709 | switch (opcode) { |
a12ca627 DB |
9710 | /* JEQ/JNE comparison doesn't change the register equivalence. |
9711 | * | |
9712 | * r1 = r2; | |
9713 | * if (r1 == 42) goto label; | |
9714 | * ... | |
9715 | * label: // here both r1 and r2 are known to be 42. | |
9716 | * | |
9717 | * Hence when marking register as known preserve it's ID. | |
9718 | */ | |
48461135 | 9719 | case BPF_JEQ: |
a12ca627 DB |
9720 | if (is_jmp32) { |
9721 | __mark_reg32_known(true_reg, val32); | |
9722 | true_32off = tnum_subreg(true_reg->var_off); | |
9723 | } else { | |
9724 | ___mark_reg_known(true_reg, val); | |
9725 | true_64off = true_reg->var_off; | |
9726 | } | |
9727 | break; | |
48461135 | 9728 | case BPF_JNE: |
a12ca627 DB |
9729 | if (is_jmp32) { |
9730 | __mark_reg32_known(false_reg, val32); | |
9731 | false_32off = tnum_subreg(false_reg->var_off); | |
9732 | } else { | |
9733 | ___mark_reg_known(false_reg, val); | |
9734 | false_64off = false_reg->var_off; | |
9735 | } | |
48461135 | 9736 | break; |
960ea056 | 9737 | case BPF_JSET: |
3f50f132 JF |
9738 | if (is_jmp32) { |
9739 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
9740 | if (is_power_of_2(val32)) | |
9741 | true_32off = tnum_or(true_32off, | |
9742 | tnum_const(val32)); | |
9743 | } else { | |
9744 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
9745 | if (is_power_of_2(val)) | |
9746 | true_64off = tnum_or(true_64off, | |
9747 | tnum_const(val)); | |
9748 | } | |
960ea056 | 9749 | break; |
48461135 | 9750 | case BPF_JGE: |
a72dafaf JW |
9751 | case BPF_JGT: |
9752 | { | |
3f50f132 JF |
9753 | if (is_jmp32) { |
9754 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
9755 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
9756 | ||
9757 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
9758 | false_umax); | |
9759 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
9760 | true_umin); | |
9761 | } else { | |
9762 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
9763 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
9764 | ||
9765 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
9766 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
9767 | } | |
b03c9f9f | 9768 | break; |
a72dafaf | 9769 | } |
48461135 | 9770 | case BPF_JSGE: |
a72dafaf JW |
9771 | case BPF_JSGT: |
9772 | { | |
3f50f132 JF |
9773 | if (is_jmp32) { |
9774 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
9775 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 9776 | |
3f50f132 JF |
9777 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
9778 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
9779 | } else { | |
9780 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
9781 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
9782 | ||
9783 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
9784 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
9785 | } | |
48461135 | 9786 | break; |
a72dafaf | 9787 | } |
b4e432f1 | 9788 | case BPF_JLE: |
a72dafaf JW |
9789 | case BPF_JLT: |
9790 | { | |
3f50f132 JF |
9791 | if (is_jmp32) { |
9792 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
9793 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
9794 | ||
9795 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
9796 | false_umin); | |
9797 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
9798 | true_umax); | |
9799 | } else { | |
9800 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
9801 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
9802 | ||
9803 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
9804 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
9805 | } | |
b4e432f1 | 9806 | break; |
a72dafaf | 9807 | } |
b4e432f1 | 9808 | case BPF_JSLE: |
a72dafaf JW |
9809 | case BPF_JSLT: |
9810 | { | |
3f50f132 JF |
9811 | if (is_jmp32) { |
9812 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
9813 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 9814 | |
3f50f132 JF |
9815 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
9816 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
9817 | } else { | |
9818 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
9819 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
9820 | ||
9821 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
9822 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
9823 | } | |
b4e432f1 | 9824 | break; |
a72dafaf | 9825 | } |
48461135 | 9826 | default: |
0fc31b10 | 9827 | return; |
48461135 JB |
9828 | } |
9829 | ||
3f50f132 JF |
9830 | if (is_jmp32) { |
9831 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
9832 | tnum_subreg(false_32off)); | |
9833 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
9834 | tnum_subreg(true_32off)); | |
9835 | __reg_combine_32_into_64(false_reg); | |
9836 | __reg_combine_32_into_64(true_reg); | |
9837 | } else { | |
9838 | false_reg->var_off = false_64off; | |
9839 | true_reg->var_off = true_64off; | |
9840 | __reg_combine_64_into_32(false_reg); | |
9841 | __reg_combine_64_into_32(true_reg); | |
9842 | } | |
48461135 JB |
9843 | } |
9844 | ||
f1174f77 EC |
9845 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
9846 | * the variable reg. | |
48461135 JB |
9847 | */ |
9848 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
9849 | struct bpf_reg_state *false_reg, |
9850 | u64 val, u32 val32, | |
092ed096 | 9851 | u8 opcode, bool is_jmp32) |
48461135 | 9852 | { |
6d94e741 | 9853 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
9854 | /* This uses zero as "not present in table"; luckily the zero opcode, |
9855 | * BPF_JA, can't get here. | |
b03c9f9f | 9856 | */ |
0fc31b10 | 9857 | if (opcode) |
3f50f132 | 9858 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
9859 | } |
9860 | ||
9861 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
9862 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
9863 | struct bpf_reg_state *dst_reg) | |
9864 | { | |
b03c9f9f EC |
9865 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
9866 | dst_reg->umin_value); | |
9867 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
9868 | dst_reg->umax_value); | |
9869 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
9870 | dst_reg->smin_value); | |
9871 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
9872 | dst_reg->smax_value); | |
f1174f77 EC |
9873 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
9874 | dst_reg->var_off); | |
3844d153 DB |
9875 | reg_bounds_sync(src_reg); |
9876 | reg_bounds_sync(dst_reg); | |
f1174f77 EC |
9877 | } |
9878 | ||
9879 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
9880 | struct bpf_reg_state *true_dst, | |
9881 | struct bpf_reg_state *false_src, | |
9882 | struct bpf_reg_state *false_dst, | |
9883 | u8 opcode) | |
9884 | { | |
9885 | switch (opcode) { | |
9886 | case BPF_JEQ: | |
9887 | __reg_combine_min_max(true_src, true_dst); | |
9888 | break; | |
9889 | case BPF_JNE: | |
9890 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 9891 | break; |
4cabc5b1 | 9892 | } |
48461135 JB |
9893 | } |
9894 | ||
fd978bf7 JS |
9895 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
9896 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 9897 | bool is_null) |
57a09bf0 | 9898 | { |
c25b2ae1 | 9899 | if (type_may_be_null(reg->type) && reg->id == id && |
93c230e3 | 9900 | !WARN_ON_ONCE(!reg->id)) { |
b03c9f9f EC |
9901 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
9902 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 9903 | reg->off)) { |
e60b0d12 DB |
9904 | /* Old offset (both fixed and variable parts) should |
9905 | * have been known-zero, because we don't allow pointer | |
9906 | * arithmetic on pointers that might be NULL. If we | |
9907 | * see this happening, don't convert the register. | |
9908 | */ | |
9909 | return; | |
f1174f77 EC |
9910 | } |
9911 | if (is_null) { | |
9912 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
9913 | /* We don't need id and ref_obj_id from this point |
9914 | * onwards anymore, thus we should better reset it, | |
9915 | * so that state pruning has chances to take effect. | |
9916 | */ | |
9917 | reg->id = 0; | |
9918 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
9919 | |
9920 | return; | |
9921 | } | |
9922 | ||
9923 | mark_ptr_not_null_reg(reg); | |
9924 | ||
9925 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 | 9926 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
b239da34 | 9927 | * in release_reference(). |
1b986589 MKL |
9928 | * |
9929 | * reg->id is still used by spin_lock ptr. Other | |
9930 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
9931 | */ |
9932 | reg->id = 0; | |
56f668df | 9933 | } |
57a09bf0 TG |
9934 | } |
9935 | } | |
9936 | ||
9937 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
9938 | * be folded together at some point. | |
9939 | */ | |
840b9615 JS |
9940 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
9941 | bool is_null) | |
57a09bf0 | 9942 | { |
f4d7e40a | 9943 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
b239da34 | 9944 | struct bpf_reg_state *regs = state->regs, *reg; |
1b986589 | 9945 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 9946 | u32 id = regs[regno].id; |
57a09bf0 | 9947 | |
1b986589 MKL |
9948 | if (ref_obj_id && ref_obj_id == id && is_null) |
9949 | /* regs[regno] is in the " == NULL" branch. | |
9950 | * No one could have freed the reference state before | |
9951 | * doing the NULL check. | |
9952 | */ | |
9953 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 9954 | |
b239da34 KKD |
9955 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
9956 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
9957 | })); | |
57a09bf0 TG |
9958 | } |
9959 | ||
5beca081 DB |
9960 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
9961 | struct bpf_reg_state *dst_reg, | |
9962 | struct bpf_reg_state *src_reg, | |
9963 | struct bpf_verifier_state *this_branch, | |
9964 | struct bpf_verifier_state *other_branch) | |
9965 | { | |
9966 | if (BPF_SRC(insn->code) != BPF_X) | |
9967 | return false; | |
9968 | ||
092ed096 JW |
9969 | /* Pointers are always 64-bit. */ |
9970 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
9971 | return false; | |
9972 | ||
5beca081 DB |
9973 | switch (BPF_OP(insn->code)) { |
9974 | case BPF_JGT: | |
9975 | if ((dst_reg->type == PTR_TO_PACKET && | |
9976 | src_reg->type == PTR_TO_PACKET_END) || | |
9977 | (dst_reg->type == PTR_TO_PACKET_META && | |
9978 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9979 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
9980 | find_good_pkt_pointers(this_branch, dst_reg, | |
9981 | dst_reg->type, false); | |
6d94e741 | 9982 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
9983 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9984 | src_reg->type == PTR_TO_PACKET) || | |
9985 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9986 | src_reg->type == PTR_TO_PACKET_META)) { | |
9987 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
9988 | find_good_pkt_pointers(other_branch, src_reg, | |
9989 | src_reg->type, true); | |
6d94e741 | 9990 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
9991 | } else { |
9992 | return false; | |
9993 | } | |
9994 | break; | |
9995 | case BPF_JLT: | |
9996 | if ((dst_reg->type == PTR_TO_PACKET && | |
9997 | src_reg->type == PTR_TO_PACKET_END) || | |
9998 | (dst_reg->type == PTR_TO_PACKET_META && | |
9999 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
10000 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
10001 | find_good_pkt_pointers(other_branch, dst_reg, | |
10002 | dst_reg->type, true); | |
6d94e741 | 10003 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
10004 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
10005 | src_reg->type == PTR_TO_PACKET) || | |
10006 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
10007 | src_reg->type == PTR_TO_PACKET_META)) { | |
10008 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
10009 | find_good_pkt_pointers(this_branch, src_reg, | |
10010 | src_reg->type, false); | |
6d94e741 | 10011 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
10012 | } else { |
10013 | return false; | |
10014 | } | |
10015 | break; | |
10016 | case BPF_JGE: | |
10017 | if ((dst_reg->type == PTR_TO_PACKET && | |
10018 | src_reg->type == PTR_TO_PACKET_END) || | |
10019 | (dst_reg->type == PTR_TO_PACKET_META && | |
10020 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
10021 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
10022 | find_good_pkt_pointers(this_branch, dst_reg, | |
10023 | dst_reg->type, true); | |
6d94e741 | 10024 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
10025 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
10026 | src_reg->type == PTR_TO_PACKET) || | |
10027 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
10028 | src_reg->type == PTR_TO_PACKET_META)) { | |
10029 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
10030 | find_good_pkt_pointers(other_branch, src_reg, | |
10031 | src_reg->type, false); | |
6d94e741 | 10032 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
10033 | } else { |
10034 | return false; | |
10035 | } | |
10036 | break; | |
10037 | case BPF_JLE: | |
10038 | if ((dst_reg->type == PTR_TO_PACKET && | |
10039 | src_reg->type == PTR_TO_PACKET_END) || | |
10040 | (dst_reg->type == PTR_TO_PACKET_META && | |
10041 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
10042 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
10043 | find_good_pkt_pointers(other_branch, dst_reg, | |
10044 | dst_reg->type, false); | |
6d94e741 | 10045 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
10046 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
10047 | src_reg->type == PTR_TO_PACKET) || | |
10048 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
10049 | src_reg->type == PTR_TO_PACKET_META)) { | |
10050 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
10051 | find_good_pkt_pointers(this_branch, src_reg, | |
10052 | src_reg->type, true); | |
6d94e741 | 10053 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
10054 | } else { |
10055 | return false; | |
10056 | } | |
10057 | break; | |
10058 | default: | |
10059 | return false; | |
10060 | } | |
10061 | ||
10062 | return true; | |
10063 | } | |
10064 | ||
75748837 AS |
10065 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
10066 | struct bpf_reg_state *known_reg) | |
10067 | { | |
10068 | struct bpf_func_state *state; | |
10069 | struct bpf_reg_state *reg; | |
75748837 | 10070 | |
b239da34 KKD |
10071 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
10072 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
10073 | *reg = *known_reg; | |
10074 | })); | |
75748837 AS |
10075 | } |
10076 | ||
58e2af8b | 10077 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
10078 | struct bpf_insn *insn, int *insn_idx) |
10079 | { | |
f4d7e40a AS |
10080 | struct bpf_verifier_state *this_branch = env->cur_state; |
10081 | struct bpf_verifier_state *other_branch; | |
10082 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 10083 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 10084 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 10085 | bool is_jmp32; |
fb8d251e | 10086 | int pred = -1; |
17a52670 AS |
10087 | int err; |
10088 | ||
092ed096 JW |
10089 | /* Only conditional jumps are expected to reach here. */ |
10090 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
10091 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
10092 | return -EINVAL; |
10093 | } | |
10094 | ||
10095 | if (BPF_SRC(insn->code) == BPF_X) { | |
10096 | if (insn->imm != 0) { | |
092ed096 | 10097 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
10098 | return -EINVAL; |
10099 | } | |
10100 | ||
10101 | /* check src1 operand */ | |
dc503a8a | 10102 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
10103 | if (err) |
10104 | return err; | |
1be7f75d AS |
10105 | |
10106 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 10107 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
10108 | insn->src_reg); |
10109 | return -EACCES; | |
10110 | } | |
fb8d251e | 10111 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
10112 | } else { |
10113 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 10114 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
10115 | return -EINVAL; |
10116 | } | |
10117 | } | |
10118 | ||
10119 | /* check src2 operand */ | |
dc503a8a | 10120 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
10121 | if (err) |
10122 | return err; | |
10123 | ||
1a0dc1ac | 10124 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 10125 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 10126 | |
3f50f132 JF |
10127 | if (BPF_SRC(insn->code) == BPF_K) { |
10128 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
10129 | } else if (src_reg->type == SCALAR_VALUE && | |
10130 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
10131 | pred = is_branch_taken(dst_reg, | |
10132 | tnum_subreg(src_reg->var_off).value, | |
10133 | opcode, | |
10134 | is_jmp32); | |
10135 | } else if (src_reg->type == SCALAR_VALUE && | |
10136 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
10137 | pred = is_branch_taken(dst_reg, | |
10138 | src_reg->var_off.value, | |
10139 | opcode, | |
10140 | is_jmp32); | |
6d94e741 AS |
10141 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
10142 | reg_is_pkt_pointer_any(src_reg) && | |
10143 | !is_jmp32) { | |
10144 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
10145 | } |
10146 | ||
b5dc0163 | 10147 | if (pred >= 0) { |
cac616db JF |
10148 | /* If we get here with a dst_reg pointer type it is because |
10149 | * above is_branch_taken() special cased the 0 comparison. | |
10150 | */ | |
10151 | if (!__is_pointer_value(false, dst_reg)) | |
10152 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
10153 | if (BPF_SRC(insn->code) == BPF_X && !err && |
10154 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
10155 | err = mark_chain_precision(env, insn->src_reg); |
10156 | if (err) | |
10157 | return err; | |
10158 | } | |
9183671a | 10159 | |
fb8d251e | 10160 | if (pred == 1) { |
9183671a DB |
10161 | /* Only follow the goto, ignore fall-through. If needed, push |
10162 | * the fall-through branch for simulation under speculative | |
10163 | * execution. | |
10164 | */ | |
10165 | if (!env->bypass_spec_v1 && | |
10166 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
10167 | *insn_idx)) | |
10168 | return -EFAULT; | |
fb8d251e AS |
10169 | *insn_idx += insn->off; |
10170 | return 0; | |
10171 | } else if (pred == 0) { | |
9183671a DB |
10172 | /* Only follow the fall-through branch, since that's where the |
10173 | * program will go. If needed, push the goto branch for | |
10174 | * simulation under speculative execution. | |
fb8d251e | 10175 | */ |
9183671a DB |
10176 | if (!env->bypass_spec_v1 && |
10177 | !sanitize_speculative_path(env, insn, | |
10178 | *insn_idx + insn->off + 1, | |
10179 | *insn_idx)) | |
10180 | return -EFAULT; | |
fb8d251e | 10181 | return 0; |
17a52670 AS |
10182 | } |
10183 | ||
979d63d5 DB |
10184 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
10185 | false); | |
17a52670 AS |
10186 | if (!other_branch) |
10187 | return -EFAULT; | |
f4d7e40a | 10188 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 10189 | |
48461135 JB |
10190 | /* detect if we are comparing against a constant value so we can adjust |
10191 | * our min/max values for our dst register. | |
f1174f77 EC |
10192 | * this is only legit if both are scalars (or pointers to the same |
10193 | * object, I suppose, but we don't support that right now), because | |
10194 | * otherwise the different base pointers mean the offsets aren't | |
10195 | * comparable. | |
48461135 JB |
10196 | */ |
10197 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 10198 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 10199 | |
f1174f77 | 10200 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
10201 | src_reg->type == SCALAR_VALUE) { |
10202 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
10203 | (is_jmp32 && |
10204 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 10205 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 10206 | dst_reg, |
3f50f132 JF |
10207 | src_reg->var_off.value, |
10208 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
10209 | opcode, is_jmp32); |
10210 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
10211 | (is_jmp32 && |
10212 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 10213 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 10214 | src_reg, |
3f50f132 JF |
10215 | dst_reg->var_off.value, |
10216 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
10217 | opcode, is_jmp32); |
10218 | else if (!is_jmp32 && | |
10219 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 10220 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
10221 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
10222 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 10223 | src_reg, dst_reg, opcode); |
e688c3db AS |
10224 | if (src_reg->id && |
10225 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
10226 | find_equal_scalars(this_branch, src_reg); |
10227 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
10228 | } | |
10229 | ||
f1174f77 EC |
10230 | } |
10231 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 10232 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
10233 | dst_reg, insn->imm, (u32)insn->imm, |
10234 | opcode, is_jmp32); | |
48461135 JB |
10235 | } |
10236 | ||
e688c3db AS |
10237 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
10238 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
10239 | find_equal_scalars(this_branch, dst_reg); |
10240 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
10241 | } | |
10242 | ||
092ed096 JW |
10243 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
10244 | * NOTE: these optimizations below are related with pointer comparison | |
10245 | * which will never be JMP32. | |
10246 | */ | |
10247 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 10248 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
c25b2ae1 | 10249 | type_may_be_null(dst_reg->type)) { |
840b9615 | 10250 | /* Mark all identical registers in each branch as either |
57a09bf0 TG |
10251 | * safe or unknown depending R == 0 or R != 0 conditional. |
10252 | */ | |
840b9615 JS |
10253 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
10254 | opcode == BPF_JNE); | |
10255 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
10256 | opcode == BPF_JEQ); | |
5beca081 DB |
10257 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
10258 | this_branch, other_branch) && | |
10259 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
10260 | verbose(env, "R%d pointer comparison prohibited\n", |
10261 | insn->dst_reg); | |
1be7f75d | 10262 | return -EACCES; |
17a52670 | 10263 | } |
06ee7115 | 10264 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 10265 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
10266 | return 0; |
10267 | } | |
10268 | ||
17a52670 | 10269 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 10270 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 10271 | { |
d8eca5bb | 10272 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 10273 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 10274 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 10275 | struct bpf_map *map; |
17a52670 AS |
10276 | int err; |
10277 | ||
10278 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 10279 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
10280 | return -EINVAL; |
10281 | } | |
10282 | if (insn->off != 0) { | |
61bd5218 | 10283 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
10284 | return -EINVAL; |
10285 | } | |
10286 | ||
dc503a8a | 10287 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
10288 | if (err) |
10289 | return err; | |
10290 | ||
4976b718 | 10291 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 10292 | if (insn->src_reg == 0) { |
6b173873 JK |
10293 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
10294 | ||
4976b718 | 10295 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 10296 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 10297 | return 0; |
6b173873 | 10298 | } |
17a52670 | 10299 | |
d400a6cf DB |
10300 | /* All special src_reg cases are listed below. From this point onwards |
10301 | * we either succeed and assign a corresponding dst_reg->type after | |
10302 | * zeroing the offset, or fail and reject the program. | |
10303 | */ | |
10304 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 10305 | |
d400a6cf | 10306 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
4976b718 | 10307 | dst_reg->type = aux->btf_var.reg_type; |
34d3a78c | 10308 | switch (base_type(dst_reg->type)) { |
4976b718 HL |
10309 | case PTR_TO_MEM: |
10310 | dst_reg->mem_size = aux->btf_var.mem_size; | |
10311 | break; | |
10312 | case PTR_TO_BTF_ID: | |
22dc4a0f | 10313 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
10314 | dst_reg->btf_id = aux->btf_var.btf_id; |
10315 | break; | |
10316 | default: | |
10317 | verbose(env, "bpf verifier is misconfigured\n"); | |
10318 | return -EFAULT; | |
10319 | } | |
10320 | return 0; | |
10321 | } | |
10322 | ||
69c087ba YS |
10323 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
10324 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
10325 | u32 subprogno = find_subprog(env, |
10326 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
10327 | |
10328 | if (!aux->func_info) { | |
10329 | verbose(env, "missing btf func_info\n"); | |
10330 | return -EINVAL; | |
10331 | } | |
10332 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
10333 | verbose(env, "callback function not static\n"); | |
10334 | return -EINVAL; | |
10335 | } | |
10336 | ||
10337 | dst_reg->type = PTR_TO_FUNC; | |
10338 | dst_reg->subprogno = subprogno; | |
10339 | return 0; | |
10340 | } | |
10341 | ||
d8eca5bb | 10342 | map = env->used_maps[aux->map_index]; |
4976b718 | 10343 | dst_reg->map_ptr = map; |
d8eca5bb | 10344 | |
387544bf AS |
10345 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
10346 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
10347 | dst_reg->type = PTR_TO_MAP_VALUE; |
10348 | dst_reg->off = aux->map_off; | |
db559117 | 10349 | if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) |
4976b718 | 10350 | dst_reg->id = ++env->id_gen; |
387544bf AS |
10351 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
10352 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 10353 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
10354 | } else { |
10355 | verbose(env, "bpf verifier is misconfigured\n"); | |
10356 | return -EINVAL; | |
10357 | } | |
17a52670 | 10358 | |
17a52670 AS |
10359 | return 0; |
10360 | } | |
10361 | ||
96be4325 DB |
10362 | static bool may_access_skb(enum bpf_prog_type type) |
10363 | { | |
10364 | switch (type) { | |
10365 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
10366 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 10367 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
10368 | return true; |
10369 | default: | |
10370 | return false; | |
10371 | } | |
10372 | } | |
10373 | ||
ddd872bc AS |
10374 | /* verify safety of LD_ABS|LD_IND instructions: |
10375 | * - they can only appear in the programs where ctx == skb | |
10376 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
10377 | * preserve R6-R9, and store return value into R0 | |
10378 | * | |
10379 | * Implicit input: | |
10380 | * ctx == skb == R6 == CTX | |
10381 | * | |
10382 | * Explicit input: | |
10383 | * SRC == any register | |
10384 | * IMM == 32-bit immediate | |
10385 | * | |
10386 | * Output: | |
10387 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
10388 | */ | |
58e2af8b | 10389 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 10390 | { |
638f5b90 | 10391 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 10392 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 10393 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
10394 | int i, err; |
10395 | ||
7e40781c | 10396 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 10397 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
10398 | return -EINVAL; |
10399 | } | |
10400 | ||
e0cea7ce DB |
10401 | if (!env->ops->gen_ld_abs) { |
10402 | verbose(env, "bpf verifier is misconfigured\n"); | |
10403 | return -EINVAL; | |
10404 | } | |
10405 | ||
ddd872bc | 10406 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 10407 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 10408 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 10409 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
10410 | return -EINVAL; |
10411 | } | |
10412 | ||
10413 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 10414 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
10415 | if (err) |
10416 | return err; | |
10417 | ||
fd978bf7 JS |
10418 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
10419 | * gen_ld_abs() may terminate the program at runtime, leading to | |
10420 | * reference leak. | |
10421 | */ | |
10422 | err = check_reference_leak(env); | |
10423 | if (err) { | |
10424 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
10425 | return err; | |
10426 | } | |
10427 | ||
d83525ca AS |
10428 | if (env->cur_state->active_spin_lock) { |
10429 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
10430 | return -EINVAL; | |
10431 | } | |
10432 | ||
6d4f151a | 10433 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
10434 | verbose(env, |
10435 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
10436 | return -EINVAL; |
10437 | } | |
10438 | ||
10439 | if (mode == BPF_IND) { | |
10440 | /* check explicit source operand */ | |
dc503a8a | 10441 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
10442 | if (err) |
10443 | return err; | |
10444 | } | |
10445 | ||
be80a1d3 | 10446 | err = check_ptr_off_reg(env, ®s[ctx_reg], ctx_reg); |
6d4f151a DB |
10447 | if (err < 0) |
10448 | return err; | |
10449 | ||
ddd872bc | 10450 | /* reset caller saved regs to unreadable */ |
dc503a8a | 10451 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 10452 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
10453 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
10454 | } | |
ddd872bc AS |
10455 | |
10456 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
10457 | * the value fetched from the packet. |
10458 | * Already marked as written above. | |
ddd872bc | 10459 | */ |
61bd5218 | 10460 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
10461 | /* ld_abs load up to 32-bit skb data. */ |
10462 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
10463 | return 0; |
10464 | } | |
10465 | ||
390ee7e2 AS |
10466 | static int check_return_code(struct bpf_verifier_env *env) |
10467 | { | |
5cf1e914 | 10468 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 10469 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
10470 | struct bpf_reg_state *reg; |
10471 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 10472 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 10473 | int err; |
bfc6bb74 AS |
10474 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
10475 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 10476 | |
9e4e01df | 10477 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
d1a6edec SF |
10478 | if (!is_subprog) { |
10479 | switch (prog_type) { | |
10480 | case BPF_PROG_TYPE_LSM: | |
10481 | if (prog->expected_attach_type == BPF_LSM_CGROUP) | |
10482 | /* See below, can be 0 or 0-1 depending on hook. */ | |
10483 | break; | |
10484 | fallthrough; | |
10485 | case BPF_PROG_TYPE_STRUCT_OPS: | |
10486 | if (!prog->aux->attach_func_proto->type) | |
10487 | return 0; | |
10488 | break; | |
10489 | default: | |
10490 | break; | |
10491 | } | |
10492 | } | |
27ae7997 | 10493 | |
8fb33b60 | 10494 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
10495 | * to return the value from eBPF program. |
10496 | * Make sure that it's readable at this time | |
10497 | * of bpf_exit, which means that program wrote | |
10498 | * something into it earlier | |
10499 | */ | |
10500 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
10501 | if (err) | |
10502 | return err; | |
10503 | ||
10504 | if (is_pointer_value(env, BPF_REG_0)) { | |
10505 | verbose(env, "R0 leaks addr as return value\n"); | |
10506 | return -EACCES; | |
10507 | } | |
390ee7e2 | 10508 | |
f782e2c3 | 10509 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
10510 | |
10511 | if (frame->in_async_callback_fn) { | |
10512 | /* enforce return zero from async callbacks like timer */ | |
10513 | if (reg->type != SCALAR_VALUE) { | |
10514 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
c25b2ae1 | 10515 | reg_type_str(env, reg->type)); |
bfc6bb74 AS |
10516 | return -EINVAL; |
10517 | } | |
10518 | ||
10519 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
10520 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
10521 | return -EINVAL; | |
10522 | } | |
10523 | return 0; | |
10524 | } | |
10525 | ||
f782e2c3 DB |
10526 | if (is_subprog) { |
10527 | if (reg->type != SCALAR_VALUE) { | |
10528 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
c25b2ae1 | 10529 | reg_type_str(env, reg->type)); |
f782e2c3 DB |
10530 | return -EINVAL; |
10531 | } | |
10532 | return 0; | |
10533 | } | |
10534 | ||
7e40781c | 10535 | switch (prog_type) { |
983695fa DB |
10536 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
10537 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
10538 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
10539 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
10540 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
10541 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
10542 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 10543 | range = tnum_range(1, 1); |
77241217 SF |
10544 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
10545 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
10546 | range = tnum_range(0, 3); | |
ed4ed404 | 10547 | break; |
390ee7e2 | 10548 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 10549 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
10550 | range = tnum_range(0, 3); | |
10551 | enforce_attach_type_range = tnum_range(2, 3); | |
10552 | } | |
ed4ed404 | 10553 | break; |
390ee7e2 AS |
10554 | case BPF_PROG_TYPE_CGROUP_SOCK: |
10555 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 10556 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 10557 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 10558 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 10559 | break; |
15ab09bd AS |
10560 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
10561 | if (!env->prog->aux->attach_btf_id) | |
10562 | return 0; | |
10563 | range = tnum_const(0); | |
10564 | break; | |
15d83c4d | 10565 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
10566 | switch (env->prog->expected_attach_type) { |
10567 | case BPF_TRACE_FENTRY: | |
10568 | case BPF_TRACE_FEXIT: | |
10569 | range = tnum_const(0); | |
10570 | break; | |
10571 | case BPF_TRACE_RAW_TP: | |
10572 | case BPF_MODIFY_RETURN: | |
15d83c4d | 10573 | return 0; |
2ec0616e DB |
10574 | case BPF_TRACE_ITER: |
10575 | break; | |
e92888c7 YS |
10576 | default: |
10577 | return -ENOTSUPP; | |
10578 | } | |
15d83c4d | 10579 | break; |
e9ddbb77 JS |
10580 | case BPF_PROG_TYPE_SK_LOOKUP: |
10581 | range = tnum_range(SK_DROP, SK_PASS); | |
10582 | break; | |
69fd337a SF |
10583 | |
10584 | case BPF_PROG_TYPE_LSM: | |
10585 | if (env->prog->expected_attach_type != BPF_LSM_CGROUP) { | |
10586 | /* Regular BPF_PROG_TYPE_LSM programs can return | |
10587 | * any value. | |
10588 | */ | |
10589 | return 0; | |
10590 | } | |
10591 | if (!env->prog->aux->attach_func_proto->type) { | |
10592 | /* Make sure programs that attach to void | |
10593 | * hooks don't try to modify return value. | |
10594 | */ | |
10595 | range = tnum_range(1, 1); | |
10596 | } | |
10597 | break; | |
10598 | ||
e92888c7 YS |
10599 | case BPF_PROG_TYPE_EXT: |
10600 | /* freplace program can return anything as its return value | |
10601 | * depends on the to-be-replaced kernel func or bpf program. | |
10602 | */ | |
390ee7e2 AS |
10603 | default: |
10604 | return 0; | |
10605 | } | |
10606 | ||
390ee7e2 | 10607 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 10608 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
c25b2ae1 | 10609 | reg_type_str(env, reg->type)); |
390ee7e2 AS |
10610 | return -EINVAL; |
10611 | } | |
10612 | ||
10613 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 10614 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
69fd337a | 10615 | if (prog->expected_attach_type == BPF_LSM_CGROUP && |
d1a6edec | 10616 | prog_type == BPF_PROG_TYPE_LSM && |
69fd337a SF |
10617 | !prog->aux->attach_func_proto->type) |
10618 | verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
390ee7e2 AS |
10619 | return -EINVAL; |
10620 | } | |
5cf1e914 | 10621 | |
10622 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
10623 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
10624 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
10625 | return 0; |
10626 | } | |
10627 | ||
475fb78f AS |
10628 | /* non-recursive DFS pseudo code |
10629 | * 1 procedure DFS-iterative(G,v): | |
10630 | * 2 label v as discovered | |
10631 | * 3 let S be a stack | |
10632 | * 4 S.push(v) | |
10633 | * 5 while S is not empty | |
b6d20799 | 10634 | * 6 t <- S.peek() |
475fb78f AS |
10635 | * 7 if t is what we're looking for: |
10636 | * 8 return t | |
10637 | * 9 for all edges e in G.adjacentEdges(t) do | |
10638 | * 10 if edge e is already labelled | |
10639 | * 11 continue with the next edge | |
10640 | * 12 w <- G.adjacentVertex(t,e) | |
10641 | * 13 if vertex w is not discovered and not explored | |
10642 | * 14 label e as tree-edge | |
10643 | * 15 label w as discovered | |
10644 | * 16 S.push(w) | |
10645 | * 17 continue at 5 | |
10646 | * 18 else if vertex w is discovered | |
10647 | * 19 label e as back-edge | |
10648 | * 20 else | |
10649 | * 21 // vertex w is explored | |
10650 | * 22 label e as forward- or cross-edge | |
10651 | * 23 label t as explored | |
10652 | * 24 S.pop() | |
10653 | * | |
10654 | * convention: | |
10655 | * 0x10 - discovered | |
10656 | * 0x11 - discovered and fall-through edge labelled | |
10657 | * 0x12 - discovered and fall-through and branch edges labelled | |
10658 | * 0x20 - explored | |
10659 | */ | |
10660 | ||
10661 | enum { | |
10662 | DISCOVERED = 0x10, | |
10663 | EXPLORED = 0x20, | |
10664 | FALLTHROUGH = 1, | |
10665 | BRANCH = 2, | |
10666 | }; | |
10667 | ||
dc2a4ebc AS |
10668 | static u32 state_htab_size(struct bpf_verifier_env *env) |
10669 | { | |
10670 | return env->prog->len; | |
10671 | } | |
10672 | ||
5d839021 AS |
10673 | static struct bpf_verifier_state_list **explored_state( |
10674 | struct bpf_verifier_env *env, | |
10675 | int idx) | |
10676 | { | |
dc2a4ebc AS |
10677 | struct bpf_verifier_state *cur = env->cur_state; |
10678 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
10679 | ||
10680 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
10681 | } |
10682 | ||
10683 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
10684 | { | |
a8f500af | 10685 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 10686 | } |
f1bca824 | 10687 | |
59e2e27d WAF |
10688 | enum { |
10689 | DONE_EXPLORING = 0, | |
10690 | KEEP_EXPLORING = 1, | |
10691 | }; | |
10692 | ||
475fb78f AS |
10693 | /* t, w, e - match pseudo-code above: |
10694 | * t - index of current instruction | |
10695 | * w - next instruction | |
10696 | * e - edge | |
10697 | */ | |
2589726d AS |
10698 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
10699 | bool loop_ok) | |
475fb78f | 10700 | { |
7df737e9 AS |
10701 | int *insn_stack = env->cfg.insn_stack; |
10702 | int *insn_state = env->cfg.insn_state; | |
10703 | ||
475fb78f | 10704 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 10705 | return DONE_EXPLORING; |
475fb78f AS |
10706 | |
10707 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 10708 | return DONE_EXPLORING; |
475fb78f AS |
10709 | |
10710 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 10711 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 10712 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
10713 | return -EINVAL; |
10714 | } | |
10715 | ||
f1bca824 AS |
10716 | if (e == BRANCH) |
10717 | /* mark branch target for state pruning */ | |
5d839021 | 10718 | init_explored_state(env, w); |
f1bca824 | 10719 | |
475fb78f AS |
10720 | if (insn_state[w] == 0) { |
10721 | /* tree-edge */ | |
10722 | insn_state[t] = DISCOVERED | e; | |
10723 | insn_state[w] = DISCOVERED; | |
7df737e9 | 10724 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 10725 | return -E2BIG; |
7df737e9 | 10726 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 10727 | return KEEP_EXPLORING; |
475fb78f | 10728 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 10729 | if (loop_ok && env->bpf_capable) |
59e2e27d | 10730 | return DONE_EXPLORING; |
d9762e84 MKL |
10731 | verbose_linfo(env, t, "%d: ", t); |
10732 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 10733 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
10734 | return -EINVAL; |
10735 | } else if (insn_state[w] == EXPLORED) { | |
10736 | /* forward- or cross-edge */ | |
10737 | insn_state[t] = DISCOVERED | e; | |
10738 | } else { | |
61bd5218 | 10739 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
10740 | return -EFAULT; |
10741 | } | |
59e2e27d WAF |
10742 | return DONE_EXPLORING; |
10743 | } | |
10744 | ||
efdb22de YS |
10745 | static int visit_func_call_insn(int t, int insn_cnt, |
10746 | struct bpf_insn *insns, | |
10747 | struct bpf_verifier_env *env, | |
10748 | bool visit_callee) | |
10749 | { | |
10750 | int ret; | |
10751 | ||
10752 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
10753 | if (ret) | |
10754 | return ret; | |
10755 | ||
10756 | if (t + 1 < insn_cnt) | |
10757 | init_explored_state(env, t + 1); | |
10758 | if (visit_callee) { | |
10759 | init_explored_state(env, t); | |
86fc6ee6 AS |
10760 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
10761 | /* It's ok to allow recursion from CFG point of | |
10762 | * view. __check_func_call() will do the actual | |
10763 | * check. | |
10764 | */ | |
10765 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
10766 | } |
10767 | return ret; | |
10768 | } | |
10769 | ||
59e2e27d WAF |
10770 | /* Visits the instruction at index t and returns one of the following: |
10771 | * < 0 - an error occurred | |
10772 | * DONE_EXPLORING - the instruction was fully explored | |
10773 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
10774 | */ | |
10775 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) | |
10776 | { | |
10777 | struct bpf_insn *insns = env->prog->insnsi; | |
10778 | int ret; | |
10779 | ||
69c087ba YS |
10780 | if (bpf_pseudo_func(insns + t)) |
10781 | return visit_func_call_insn(t, insn_cnt, insns, env, true); | |
10782 | ||
59e2e27d WAF |
10783 | /* All non-branch instructions have a single fall-through edge. */ |
10784 | if (BPF_CLASS(insns[t].code) != BPF_JMP && | |
10785 | BPF_CLASS(insns[t].code) != BPF_JMP32) | |
10786 | return push_insn(t, t + 1, FALLTHROUGH, env, false); | |
10787 | ||
10788 | switch (BPF_OP(insns[t].code)) { | |
10789 | case BPF_EXIT: | |
10790 | return DONE_EXPLORING; | |
10791 | ||
10792 | case BPF_CALL: | |
bfc6bb74 AS |
10793 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
10794 | /* Mark this call insn to trigger is_state_visited() check | |
10795 | * before call itself is processed by __check_func_call(). | |
10796 | * Otherwise new async state will be pushed for further | |
10797 | * exploration. | |
10798 | */ | |
10799 | init_explored_state(env, t); | |
efdb22de YS |
10800 | return visit_func_call_insn(t, insn_cnt, insns, env, |
10801 | insns[t].src_reg == BPF_PSEUDO_CALL); | |
59e2e27d WAF |
10802 | |
10803 | case BPF_JA: | |
10804 | if (BPF_SRC(insns[t].code) != BPF_K) | |
10805 | return -EINVAL; | |
10806 | ||
10807 | /* unconditional jump with single edge */ | |
10808 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, | |
10809 | true); | |
10810 | if (ret) | |
10811 | return ret; | |
10812 | ||
10813 | /* unconditional jmp is not a good pruning point, | |
10814 | * but it's marked, since backtracking needs | |
10815 | * to record jmp history in is_state_visited(). | |
10816 | */ | |
10817 | init_explored_state(env, t + insns[t].off + 1); | |
10818 | /* tell verifier to check for equivalent states | |
10819 | * after every call and jump | |
10820 | */ | |
10821 | if (t + 1 < insn_cnt) | |
10822 | init_explored_state(env, t + 1); | |
10823 | ||
10824 | return ret; | |
10825 | ||
10826 | default: | |
10827 | /* conditional jump with two edges */ | |
10828 | init_explored_state(env, t); | |
10829 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); | |
10830 | if (ret) | |
10831 | return ret; | |
10832 | ||
10833 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); | |
10834 | } | |
475fb78f AS |
10835 | } |
10836 | ||
10837 | /* non-recursive depth-first-search to detect loops in BPF program | |
10838 | * loop == back-edge in directed graph | |
10839 | */ | |
58e2af8b | 10840 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 10841 | { |
475fb78f | 10842 | int insn_cnt = env->prog->len; |
7df737e9 | 10843 | int *insn_stack, *insn_state; |
475fb78f | 10844 | int ret = 0; |
59e2e27d | 10845 | int i; |
475fb78f | 10846 | |
7df737e9 | 10847 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
10848 | if (!insn_state) |
10849 | return -ENOMEM; | |
10850 | ||
7df737e9 | 10851 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 10852 | if (!insn_stack) { |
71dde681 | 10853 | kvfree(insn_state); |
475fb78f AS |
10854 | return -ENOMEM; |
10855 | } | |
10856 | ||
10857 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
10858 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 10859 | env->cfg.cur_stack = 1; |
475fb78f | 10860 | |
59e2e27d WAF |
10861 | while (env->cfg.cur_stack > 0) { |
10862 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 10863 | |
59e2e27d WAF |
10864 | ret = visit_insn(t, insn_cnt, env); |
10865 | switch (ret) { | |
10866 | case DONE_EXPLORING: | |
10867 | insn_state[t] = EXPLORED; | |
10868 | env->cfg.cur_stack--; | |
10869 | break; | |
10870 | case KEEP_EXPLORING: | |
10871 | break; | |
10872 | default: | |
10873 | if (ret > 0) { | |
10874 | verbose(env, "visit_insn internal bug\n"); | |
10875 | ret = -EFAULT; | |
475fb78f | 10876 | } |
475fb78f | 10877 | goto err_free; |
59e2e27d | 10878 | } |
475fb78f AS |
10879 | } |
10880 | ||
59e2e27d | 10881 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 10882 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
10883 | ret = -EFAULT; |
10884 | goto err_free; | |
10885 | } | |
475fb78f | 10886 | |
475fb78f AS |
10887 | for (i = 0; i < insn_cnt; i++) { |
10888 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 10889 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
10890 | ret = -EINVAL; |
10891 | goto err_free; | |
10892 | } | |
10893 | } | |
10894 | ret = 0; /* cfg looks good */ | |
10895 | ||
10896 | err_free: | |
71dde681 AS |
10897 | kvfree(insn_state); |
10898 | kvfree(insn_stack); | |
7df737e9 | 10899 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
10900 | return ret; |
10901 | } | |
10902 | ||
09b28d76 AS |
10903 | static int check_abnormal_return(struct bpf_verifier_env *env) |
10904 | { | |
10905 | int i; | |
10906 | ||
10907 | for (i = 1; i < env->subprog_cnt; i++) { | |
10908 | if (env->subprog_info[i].has_ld_abs) { | |
10909 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
10910 | return -EINVAL; | |
10911 | } | |
10912 | if (env->subprog_info[i].has_tail_call) { | |
10913 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
10914 | return -EINVAL; | |
10915 | } | |
10916 | } | |
10917 | return 0; | |
10918 | } | |
10919 | ||
838e9690 YS |
10920 | /* The minimum supported BTF func info size */ |
10921 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
10922 | #define MAX_FUNCINFO_REC_SIZE 252 | |
10923 | ||
c454a46b MKL |
10924 | static int check_btf_func(struct bpf_verifier_env *env, |
10925 | const union bpf_attr *attr, | |
af2ac3e1 | 10926 | bpfptr_t uattr) |
838e9690 | 10927 | { |
09b28d76 | 10928 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 10929 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 10930 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 10931 | struct bpf_func_info *krecord; |
8c1b6e69 | 10932 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
10933 | struct bpf_prog *prog; |
10934 | const struct btf *btf; | |
af2ac3e1 | 10935 | bpfptr_t urecord; |
d0b2818e | 10936 | u32 prev_offset = 0; |
09b28d76 | 10937 | bool scalar_return; |
e7ed83d6 | 10938 | int ret = -ENOMEM; |
838e9690 YS |
10939 | |
10940 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
10941 | if (!nfuncs) { |
10942 | if (check_abnormal_return(env)) | |
10943 | return -EINVAL; | |
838e9690 | 10944 | return 0; |
09b28d76 | 10945 | } |
838e9690 YS |
10946 | |
10947 | if (nfuncs != env->subprog_cnt) { | |
10948 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
10949 | return -EINVAL; | |
10950 | } | |
10951 | ||
10952 | urec_size = attr->func_info_rec_size; | |
10953 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
10954 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
10955 | urec_size % sizeof(u32)) { | |
10956 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
10957 | return -EINVAL; | |
10958 | } | |
10959 | ||
c454a46b MKL |
10960 | prog = env->prog; |
10961 | btf = prog->aux->btf; | |
838e9690 | 10962 | |
af2ac3e1 | 10963 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
10964 | min_size = min_t(u32, krec_size, urec_size); |
10965 | ||
ba64e7d8 | 10966 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
10967 | if (!krecord) |
10968 | return -ENOMEM; | |
8c1b6e69 AS |
10969 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
10970 | if (!info_aux) | |
10971 | goto err_free; | |
ba64e7d8 | 10972 | |
838e9690 YS |
10973 | for (i = 0; i < nfuncs; i++) { |
10974 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
10975 | if (ret) { | |
10976 | if (ret == -E2BIG) { | |
10977 | verbose(env, "nonzero tailing record in func info"); | |
10978 | /* set the size kernel expects so loader can zero | |
10979 | * out the rest of the record. | |
10980 | */ | |
af2ac3e1 AS |
10981 | if (copy_to_bpfptr_offset(uattr, |
10982 | offsetof(union bpf_attr, func_info_rec_size), | |
10983 | &min_size, sizeof(min_size))) | |
838e9690 YS |
10984 | ret = -EFAULT; |
10985 | } | |
c454a46b | 10986 | goto err_free; |
838e9690 YS |
10987 | } |
10988 | ||
af2ac3e1 | 10989 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 10990 | ret = -EFAULT; |
c454a46b | 10991 | goto err_free; |
838e9690 YS |
10992 | } |
10993 | ||
d30d42e0 | 10994 | /* check insn_off */ |
09b28d76 | 10995 | ret = -EINVAL; |
838e9690 | 10996 | if (i == 0) { |
d30d42e0 | 10997 | if (krecord[i].insn_off) { |
838e9690 | 10998 | verbose(env, |
d30d42e0 MKL |
10999 | "nonzero insn_off %u for the first func info record", |
11000 | krecord[i].insn_off); | |
c454a46b | 11001 | goto err_free; |
838e9690 | 11002 | } |
d30d42e0 | 11003 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
11004 | verbose(env, |
11005 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 11006 | krecord[i].insn_off, prev_offset); |
c454a46b | 11007 | goto err_free; |
838e9690 YS |
11008 | } |
11009 | ||
d30d42e0 | 11010 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 11011 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 11012 | goto err_free; |
838e9690 YS |
11013 | } |
11014 | ||
11015 | /* check type_id */ | |
ba64e7d8 | 11016 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 11017 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 11018 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 11019 | krecord[i].type_id); |
c454a46b | 11020 | goto err_free; |
838e9690 | 11021 | } |
51c39bb1 | 11022 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
11023 | |
11024 | func_proto = btf_type_by_id(btf, type->type); | |
11025 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
11026 | /* btf_func_check() already verified it during BTF load */ | |
11027 | goto err_free; | |
11028 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
11029 | scalar_return = | |
6089fb32 | 11030 | btf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type); |
09b28d76 AS |
11031 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
11032 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
11033 | goto err_free; | |
11034 | } | |
11035 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
11036 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
11037 | goto err_free; | |
11038 | } | |
11039 | ||
d30d42e0 | 11040 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 11041 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
11042 | } |
11043 | ||
ba64e7d8 YS |
11044 | prog->aux->func_info = krecord; |
11045 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 11046 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
11047 | return 0; |
11048 | ||
c454a46b | 11049 | err_free: |
ba64e7d8 | 11050 | kvfree(krecord); |
8c1b6e69 | 11051 | kfree(info_aux); |
838e9690 YS |
11052 | return ret; |
11053 | } | |
11054 | ||
ba64e7d8 YS |
11055 | static void adjust_btf_func(struct bpf_verifier_env *env) |
11056 | { | |
8c1b6e69 | 11057 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
11058 | int i; |
11059 | ||
8c1b6e69 | 11060 | if (!aux->func_info) |
ba64e7d8 YS |
11061 | return; |
11062 | ||
11063 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 11064 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
11065 | } |
11066 | ||
1b773d00 | 11067 | #define MIN_BPF_LINEINFO_SIZE offsetofend(struct bpf_line_info, line_col) |
c454a46b MKL |
11068 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
11069 | ||
11070 | static int check_btf_line(struct bpf_verifier_env *env, | |
11071 | const union bpf_attr *attr, | |
af2ac3e1 | 11072 | bpfptr_t uattr) |
c454a46b MKL |
11073 | { |
11074 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
11075 | struct bpf_subprog_info *sub; | |
11076 | struct bpf_line_info *linfo; | |
11077 | struct bpf_prog *prog; | |
11078 | const struct btf *btf; | |
af2ac3e1 | 11079 | bpfptr_t ulinfo; |
c454a46b MKL |
11080 | int err; |
11081 | ||
11082 | nr_linfo = attr->line_info_cnt; | |
11083 | if (!nr_linfo) | |
11084 | return 0; | |
0e6491b5 BC |
11085 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
11086 | return -EINVAL; | |
c454a46b MKL |
11087 | |
11088 | rec_size = attr->line_info_rec_size; | |
11089 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
11090 | rec_size > MAX_LINEINFO_REC_SIZE || | |
11091 | rec_size & (sizeof(u32) - 1)) | |
11092 | return -EINVAL; | |
11093 | ||
11094 | /* Need to zero it in case the userspace may | |
11095 | * pass in a smaller bpf_line_info object. | |
11096 | */ | |
11097 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
11098 | GFP_KERNEL | __GFP_NOWARN); | |
11099 | if (!linfo) | |
11100 | return -ENOMEM; | |
11101 | ||
11102 | prog = env->prog; | |
11103 | btf = prog->aux->btf; | |
11104 | ||
11105 | s = 0; | |
11106 | sub = env->subprog_info; | |
af2ac3e1 | 11107 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
11108 | expected_size = sizeof(struct bpf_line_info); |
11109 | ncopy = min_t(u32, expected_size, rec_size); | |
11110 | for (i = 0; i < nr_linfo; i++) { | |
11111 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
11112 | if (err) { | |
11113 | if (err == -E2BIG) { | |
11114 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
11115 | if (copy_to_bpfptr_offset(uattr, |
11116 | offsetof(union bpf_attr, line_info_rec_size), | |
11117 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
11118 | err = -EFAULT; |
11119 | } | |
11120 | goto err_free; | |
11121 | } | |
11122 | ||
af2ac3e1 | 11123 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
11124 | err = -EFAULT; |
11125 | goto err_free; | |
11126 | } | |
11127 | ||
11128 | /* | |
11129 | * Check insn_off to ensure | |
11130 | * 1) strictly increasing AND | |
11131 | * 2) bounded by prog->len | |
11132 | * | |
11133 | * The linfo[0].insn_off == 0 check logically falls into | |
11134 | * the later "missing bpf_line_info for func..." case | |
11135 | * because the first linfo[0].insn_off must be the | |
11136 | * first sub also and the first sub must have | |
11137 | * subprog_info[0].start == 0. | |
11138 | */ | |
11139 | if ((i && linfo[i].insn_off <= prev_offset) || | |
11140 | linfo[i].insn_off >= prog->len) { | |
11141 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
11142 | i, linfo[i].insn_off, prev_offset, | |
11143 | prog->len); | |
11144 | err = -EINVAL; | |
11145 | goto err_free; | |
11146 | } | |
11147 | ||
fdbaa0be MKL |
11148 | if (!prog->insnsi[linfo[i].insn_off].code) { |
11149 | verbose(env, | |
11150 | "Invalid insn code at line_info[%u].insn_off\n", | |
11151 | i); | |
11152 | err = -EINVAL; | |
11153 | goto err_free; | |
11154 | } | |
11155 | ||
23127b33 MKL |
11156 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
11157 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
11158 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
11159 | err = -EINVAL; | |
11160 | goto err_free; | |
11161 | } | |
11162 | ||
11163 | if (s != env->subprog_cnt) { | |
11164 | if (linfo[i].insn_off == sub[s].start) { | |
11165 | sub[s].linfo_idx = i; | |
11166 | s++; | |
11167 | } else if (sub[s].start < linfo[i].insn_off) { | |
11168 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
11169 | err = -EINVAL; | |
11170 | goto err_free; | |
11171 | } | |
11172 | } | |
11173 | ||
11174 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 11175 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
11176 | } |
11177 | ||
11178 | if (s != env->subprog_cnt) { | |
11179 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
11180 | env->subprog_cnt - s, s); | |
11181 | err = -EINVAL; | |
11182 | goto err_free; | |
11183 | } | |
11184 | ||
11185 | prog->aux->linfo = linfo; | |
11186 | prog->aux->nr_linfo = nr_linfo; | |
11187 | ||
11188 | return 0; | |
11189 | ||
11190 | err_free: | |
11191 | kvfree(linfo); | |
11192 | return err; | |
11193 | } | |
11194 | ||
fbd94c7a AS |
11195 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
11196 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
11197 | ||
11198 | static int check_core_relo(struct bpf_verifier_env *env, | |
11199 | const union bpf_attr *attr, | |
11200 | bpfptr_t uattr) | |
11201 | { | |
11202 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
11203 | struct bpf_core_relo core_relo = {}; | |
11204 | struct bpf_prog *prog = env->prog; | |
11205 | const struct btf *btf = prog->aux->btf; | |
11206 | struct bpf_core_ctx ctx = { | |
11207 | .log = &env->log, | |
11208 | .btf = btf, | |
11209 | }; | |
11210 | bpfptr_t u_core_relo; | |
11211 | int err; | |
11212 | ||
11213 | nr_core_relo = attr->core_relo_cnt; | |
11214 | if (!nr_core_relo) | |
11215 | return 0; | |
11216 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
11217 | return -EINVAL; | |
11218 | ||
11219 | rec_size = attr->core_relo_rec_size; | |
11220 | if (rec_size < MIN_CORE_RELO_SIZE || | |
11221 | rec_size > MAX_CORE_RELO_SIZE || | |
11222 | rec_size % sizeof(u32)) | |
11223 | return -EINVAL; | |
11224 | ||
11225 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
11226 | expected_size = sizeof(struct bpf_core_relo); | |
11227 | ncopy = min_t(u32, expected_size, rec_size); | |
11228 | ||
11229 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
11230 | * relocation record one at a time. | |
11231 | */ | |
11232 | for (i = 0; i < nr_core_relo; i++) { | |
11233 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
11234 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
11235 | if (err) { | |
11236 | if (err == -E2BIG) { | |
11237 | verbose(env, "nonzero tailing record in core_relo"); | |
11238 | if (copy_to_bpfptr_offset(uattr, | |
11239 | offsetof(union bpf_attr, core_relo_rec_size), | |
11240 | &expected_size, sizeof(expected_size))) | |
11241 | err = -EFAULT; | |
11242 | } | |
11243 | break; | |
11244 | } | |
11245 | ||
11246 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
11247 | err = -EFAULT; | |
11248 | break; | |
11249 | } | |
11250 | ||
11251 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
11252 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
11253 | i, core_relo.insn_off, prog->len); | |
11254 | err = -EINVAL; | |
11255 | break; | |
11256 | } | |
11257 | ||
11258 | err = bpf_core_apply(&ctx, &core_relo, i, | |
11259 | &prog->insnsi[core_relo.insn_off / 8]); | |
11260 | if (err) | |
11261 | break; | |
11262 | bpfptr_add(&u_core_relo, rec_size); | |
11263 | } | |
11264 | return err; | |
11265 | } | |
11266 | ||
c454a46b MKL |
11267 | static int check_btf_info(struct bpf_verifier_env *env, |
11268 | const union bpf_attr *attr, | |
af2ac3e1 | 11269 | bpfptr_t uattr) |
c454a46b MKL |
11270 | { |
11271 | struct btf *btf; | |
11272 | int err; | |
11273 | ||
09b28d76 AS |
11274 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
11275 | if (check_abnormal_return(env)) | |
11276 | return -EINVAL; | |
c454a46b | 11277 | return 0; |
09b28d76 | 11278 | } |
c454a46b MKL |
11279 | |
11280 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
11281 | if (IS_ERR(btf)) | |
11282 | return PTR_ERR(btf); | |
350a5c4d AS |
11283 | if (btf_is_kernel(btf)) { |
11284 | btf_put(btf); | |
11285 | return -EACCES; | |
11286 | } | |
c454a46b MKL |
11287 | env->prog->aux->btf = btf; |
11288 | ||
11289 | err = check_btf_func(env, attr, uattr); | |
11290 | if (err) | |
11291 | return err; | |
11292 | ||
11293 | err = check_btf_line(env, attr, uattr); | |
11294 | if (err) | |
11295 | return err; | |
11296 | ||
fbd94c7a AS |
11297 | err = check_core_relo(env, attr, uattr); |
11298 | if (err) | |
11299 | return err; | |
11300 | ||
c454a46b | 11301 | return 0; |
ba64e7d8 YS |
11302 | } |
11303 | ||
f1174f77 EC |
11304 | /* check %cur's range satisfies %old's */ |
11305 | static bool range_within(struct bpf_reg_state *old, | |
11306 | struct bpf_reg_state *cur) | |
11307 | { | |
b03c9f9f EC |
11308 | return old->umin_value <= cur->umin_value && |
11309 | old->umax_value >= cur->umax_value && | |
11310 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
11311 | old->smax_value >= cur->smax_value && |
11312 | old->u32_min_value <= cur->u32_min_value && | |
11313 | old->u32_max_value >= cur->u32_max_value && | |
11314 | old->s32_min_value <= cur->s32_min_value && | |
11315 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
11316 | } |
11317 | ||
f1174f77 EC |
11318 | /* If in the old state two registers had the same id, then they need to have |
11319 | * the same id in the new state as well. But that id could be different from | |
11320 | * the old state, so we need to track the mapping from old to new ids. | |
11321 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
11322 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
11323 | * regs with a different old id could still have new id 9, we don't care about | |
11324 | * that. | |
11325 | * So we look through our idmap to see if this old id has been seen before. If | |
11326 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 11327 | */ |
c9e73e3d | 11328 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 11329 | { |
f1174f77 | 11330 | unsigned int i; |
969bf05e | 11331 | |
c9e73e3d | 11332 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
11333 | if (!idmap[i].old) { |
11334 | /* Reached an empty slot; haven't seen this id before */ | |
11335 | idmap[i].old = old_id; | |
11336 | idmap[i].cur = cur_id; | |
11337 | return true; | |
11338 | } | |
11339 | if (idmap[i].old == old_id) | |
11340 | return idmap[i].cur == cur_id; | |
11341 | } | |
11342 | /* We ran out of idmap slots, which should be impossible */ | |
11343 | WARN_ON_ONCE(1); | |
11344 | return false; | |
11345 | } | |
11346 | ||
9242b5f5 AS |
11347 | static void clean_func_state(struct bpf_verifier_env *env, |
11348 | struct bpf_func_state *st) | |
11349 | { | |
11350 | enum bpf_reg_liveness live; | |
11351 | int i, j; | |
11352 | ||
11353 | for (i = 0; i < BPF_REG_FP; i++) { | |
11354 | live = st->regs[i].live; | |
11355 | /* liveness must not touch this register anymore */ | |
11356 | st->regs[i].live |= REG_LIVE_DONE; | |
11357 | if (!(live & REG_LIVE_READ)) | |
11358 | /* since the register is unused, clear its state | |
11359 | * to make further comparison simpler | |
11360 | */ | |
f54c7898 | 11361 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
11362 | } |
11363 | ||
11364 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
11365 | live = st->stack[i].spilled_ptr.live; | |
11366 | /* liveness must not touch this stack slot anymore */ | |
11367 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
11368 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 11369 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
11370 | for (j = 0; j < BPF_REG_SIZE; j++) |
11371 | st->stack[i].slot_type[j] = STACK_INVALID; | |
11372 | } | |
11373 | } | |
11374 | } | |
11375 | ||
11376 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
11377 | struct bpf_verifier_state *st) | |
11378 | { | |
11379 | int i; | |
11380 | ||
11381 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
11382 | /* all regs in this state in all frames were already marked */ | |
11383 | return; | |
11384 | ||
11385 | for (i = 0; i <= st->curframe; i++) | |
11386 | clean_func_state(env, st->frame[i]); | |
11387 | } | |
11388 | ||
11389 | /* the parentage chains form a tree. | |
11390 | * the verifier states are added to state lists at given insn and | |
11391 | * pushed into state stack for future exploration. | |
11392 | * when the verifier reaches bpf_exit insn some of the verifer states | |
11393 | * stored in the state lists have their final liveness state already, | |
11394 | * but a lot of states will get revised from liveness point of view when | |
11395 | * the verifier explores other branches. | |
11396 | * Example: | |
11397 | * 1: r0 = 1 | |
11398 | * 2: if r1 == 100 goto pc+1 | |
11399 | * 3: r0 = 2 | |
11400 | * 4: exit | |
11401 | * when the verifier reaches exit insn the register r0 in the state list of | |
11402 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
11403 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
11404 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
11405 | * | |
11406 | * Since the verifier pushes the branch states as it sees them while exploring | |
11407 | * the program the condition of walking the branch instruction for the second | |
11408 | * time means that all states below this branch were already explored and | |
8fb33b60 | 11409 | * their final liveness marks are already propagated. |
9242b5f5 AS |
11410 | * Hence when the verifier completes the search of state list in is_state_visited() |
11411 | * we can call this clean_live_states() function to mark all liveness states | |
11412 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
11413 | * will not be used. | |
11414 | * This function also clears the registers and stack for states that !READ | |
11415 | * to simplify state merging. | |
11416 | * | |
11417 | * Important note here that walking the same branch instruction in the callee | |
11418 | * doesn't meant that the states are DONE. The verifier has to compare | |
11419 | * the callsites | |
11420 | */ | |
11421 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
11422 | struct bpf_verifier_state *cur) | |
11423 | { | |
11424 | struct bpf_verifier_state_list *sl; | |
11425 | int i; | |
11426 | ||
5d839021 | 11427 | sl = *explored_state(env, insn); |
a8f500af | 11428 | while (sl) { |
2589726d AS |
11429 | if (sl->state.branches) |
11430 | goto next; | |
dc2a4ebc AS |
11431 | if (sl->state.insn_idx != insn || |
11432 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
11433 | goto next; |
11434 | for (i = 0; i <= cur->curframe; i++) | |
11435 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
11436 | goto next; | |
11437 | clean_verifier_state(env, &sl->state); | |
11438 | next: | |
11439 | sl = sl->next; | |
11440 | } | |
11441 | } | |
11442 | ||
f1174f77 | 11443 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
11444 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
11445 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 11446 | { |
f4d7e40a AS |
11447 | bool equal; |
11448 | ||
dc503a8a EC |
11449 | if (!(rold->live & REG_LIVE_READ)) |
11450 | /* explored state didn't use this */ | |
11451 | return true; | |
11452 | ||
679c782d | 11453 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
11454 | |
11455 | if (rold->type == PTR_TO_STACK) | |
11456 | /* two stack pointers are equal only if they're pointing to | |
11457 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
11458 | */ | |
11459 | return equal && rold->frameno == rcur->frameno; | |
11460 | ||
11461 | if (equal) | |
969bf05e AS |
11462 | return true; |
11463 | ||
f1174f77 EC |
11464 | if (rold->type == NOT_INIT) |
11465 | /* explored state can't have used this */ | |
969bf05e | 11466 | return true; |
f1174f77 EC |
11467 | if (rcur->type == NOT_INIT) |
11468 | return false; | |
c25b2ae1 | 11469 | switch (base_type(rold->type)) { |
f1174f77 | 11470 | case SCALAR_VALUE: |
e042aa53 DB |
11471 | if (env->explore_alu_limits) |
11472 | return false; | |
f1174f77 | 11473 | if (rcur->type == SCALAR_VALUE) { |
b5dc0163 AS |
11474 | if (!rold->precise && !rcur->precise) |
11475 | return true; | |
f1174f77 EC |
11476 | /* new val must satisfy old val knowledge */ |
11477 | return range_within(rold, rcur) && | |
11478 | tnum_in(rold->var_off, rcur->var_off); | |
11479 | } else { | |
179d1c56 JH |
11480 | /* We're trying to use a pointer in place of a scalar. |
11481 | * Even if the scalar was unbounded, this could lead to | |
11482 | * pointer leaks because scalars are allowed to leak | |
11483 | * while pointers are not. We could make this safe in | |
11484 | * special cases if root is calling us, but it's | |
11485 | * probably not worth the hassle. | |
f1174f77 | 11486 | */ |
179d1c56 | 11487 | return false; |
f1174f77 | 11488 | } |
69c087ba | 11489 | case PTR_TO_MAP_KEY: |
f1174f77 | 11490 | case PTR_TO_MAP_VALUE: |
c25b2ae1 HL |
11491 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
11492 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
11493 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
11494 | * checked, doing so could have affected others with the same | |
11495 | * id, and we can't check for that because we lost the id when | |
11496 | * we converted to a PTR_TO_MAP_VALUE. | |
11497 | */ | |
11498 | if (type_may_be_null(rold->type)) { | |
11499 | if (!type_may_be_null(rcur->type)) | |
11500 | return false; | |
11501 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
11502 | return false; | |
11503 | /* Check our ids match any regs they're supposed to */ | |
11504 | return check_ids(rold->id, rcur->id, idmap); | |
11505 | } | |
11506 | ||
1b688a19 EC |
11507 | /* If the new min/max/var_off satisfy the old ones and |
11508 | * everything else matches, we are OK. | |
d83525ca AS |
11509 | * 'id' is not compared, since it's only used for maps with |
11510 | * bpf_spin_lock inside map element and in such cases if | |
11511 | * the rest of the prog is valid for one map element then | |
11512 | * it's valid for all map elements regardless of the key | |
11513 | * used in bpf_map_lookup() | |
1b688a19 EC |
11514 | */ |
11515 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
11516 | range_within(rold, rcur) && | |
11517 | tnum_in(rold->var_off, rcur->var_off); | |
de8f3a83 | 11518 | case PTR_TO_PACKET_META: |
f1174f77 | 11519 | case PTR_TO_PACKET: |
de8f3a83 | 11520 | if (rcur->type != rold->type) |
f1174f77 EC |
11521 | return false; |
11522 | /* We must have at least as much range as the old ptr | |
11523 | * did, so that any accesses which were safe before are | |
11524 | * still safe. This is true even if old range < old off, | |
11525 | * since someone could have accessed through (ptr - k), or | |
11526 | * even done ptr -= k in a register, to get a safe access. | |
11527 | */ | |
11528 | if (rold->range > rcur->range) | |
11529 | return false; | |
11530 | /* If the offsets don't match, we can't trust our alignment; | |
11531 | * nor can we be sure that we won't fall out of range. | |
11532 | */ | |
11533 | if (rold->off != rcur->off) | |
11534 | return false; | |
11535 | /* id relations must be preserved */ | |
11536 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
11537 | return false; | |
11538 | /* new val must satisfy old val knowledge */ | |
11539 | return range_within(rold, rcur) && | |
11540 | tnum_in(rold->var_off, rcur->var_off); | |
11541 | case PTR_TO_CTX: | |
11542 | case CONST_PTR_TO_MAP: | |
f1174f77 | 11543 | case PTR_TO_PACKET_END: |
d58e468b | 11544 | case PTR_TO_FLOW_KEYS: |
c64b7983 | 11545 | case PTR_TO_SOCKET: |
46f8bc92 | 11546 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 11547 | case PTR_TO_TCP_SOCK: |
fada7fdc | 11548 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
11549 | /* Only valid matches are exact, which memcmp() above |
11550 | * would have accepted | |
11551 | */ | |
11552 | default: | |
11553 | /* Don't know what's going on, just say it's not safe */ | |
11554 | return false; | |
11555 | } | |
969bf05e | 11556 | |
f1174f77 EC |
11557 | /* Shouldn't get here; if we do, say it's not safe */ |
11558 | WARN_ON_ONCE(1); | |
969bf05e AS |
11559 | return false; |
11560 | } | |
11561 | ||
e042aa53 DB |
11562 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
11563 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
11564 | { |
11565 | int i, spi; | |
11566 | ||
638f5b90 AS |
11567 | /* walk slots of the explored stack and ignore any additional |
11568 | * slots in the current stack, since explored(safe) state | |
11569 | * didn't use them | |
11570 | */ | |
11571 | for (i = 0; i < old->allocated_stack; i++) { | |
11572 | spi = i / BPF_REG_SIZE; | |
11573 | ||
b233920c AS |
11574 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
11575 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 11576 | /* explored state didn't use this */ |
fd05e57b | 11577 | continue; |
b233920c | 11578 | } |
cc2b14d5 | 11579 | |
638f5b90 AS |
11580 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
11581 | continue; | |
19e2dbb7 AS |
11582 | |
11583 | /* explored stack has more populated slots than current stack | |
11584 | * and these slots were used | |
11585 | */ | |
11586 | if (i >= cur->allocated_stack) | |
11587 | return false; | |
11588 | ||
cc2b14d5 AS |
11589 | /* if old state was safe with misc data in the stack |
11590 | * it will be safe with zero-initialized stack. | |
11591 | * The opposite is not true | |
11592 | */ | |
11593 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
11594 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
11595 | continue; | |
638f5b90 AS |
11596 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
11597 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
11598 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 11599 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
11600 | * this verifier states are not equivalent, |
11601 | * return false to continue verification of this path | |
11602 | */ | |
11603 | return false; | |
27113c59 | 11604 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 11605 | continue; |
27113c59 | 11606 | if (!is_spilled_reg(&old->stack[spi])) |
638f5b90 | 11607 | continue; |
e042aa53 DB |
11608 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
11609 | &cur->stack[spi].spilled_ptr, idmap)) | |
638f5b90 AS |
11610 | /* when explored and current stack slot are both storing |
11611 | * spilled registers, check that stored pointers types | |
11612 | * are the same as well. | |
11613 | * Ex: explored safe path could have stored | |
11614 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
11615 | * but current path has stored: | |
11616 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
11617 | * such verifier states are not equivalent. | |
11618 | * return false to continue verification of this path | |
11619 | */ | |
11620 | return false; | |
11621 | } | |
11622 | return true; | |
11623 | } | |
11624 | ||
fd978bf7 JS |
11625 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
11626 | { | |
11627 | if (old->acquired_refs != cur->acquired_refs) | |
11628 | return false; | |
11629 | return !memcmp(old->refs, cur->refs, | |
11630 | sizeof(*old->refs) * old->acquired_refs); | |
11631 | } | |
11632 | ||
f1bca824 AS |
11633 | /* compare two verifier states |
11634 | * | |
11635 | * all states stored in state_list are known to be valid, since | |
11636 | * verifier reached 'bpf_exit' instruction through them | |
11637 | * | |
11638 | * this function is called when verifier exploring different branches of | |
11639 | * execution popped from the state stack. If it sees an old state that has | |
11640 | * more strict register state and more strict stack state then this execution | |
11641 | * branch doesn't need to be explored further, since verifier already | |
11642 | * concluded that more strict state leads to valid finish. | |
11643 | * | |
11644 | * Therefore two states are equivalent if register state is more conservative | |
11645 | * and explored stack state is more conservative than the current one. | |
11646 | * Example: | |
11647 | * explored current | |
11648 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
11649 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
11650 | * | |
11651 | * In other words if current stack state (one being explored) has more | |
11652 | * valid slots than old one that already passed validation, it means | |
11653 | * the verifier can stop exploring and conclude that current state is valid too | |
11654 | * | |
11655 | * Similarly with registers. If explored state has register type as invalid | |
11656 | * whereas register type in current state is meaningful, it means that | |
11657 | * the current state will reach 'bpf_exit' instruction safely | |
11658 | */ | |
c9e73e3d | 11659 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 11660 | struct bpf_func_state *cur) |
f1bca824 AS |
11661 | { |
11662 | int i; | |
11663 | ||
c9e73e3d LB |
11664 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
11665 | for (i = 0; i < MAX_BPF_REG; i++) | |
e042aa53 DB |
11666 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
11667 | env->idmap_scratch)) | |
c9e73e3d | 11668 | return false; |
f1bca824 | 11669 | |
e042aa53 | 11670 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 11671 | return false; |
fd978bf7 JS |
11672 | |
11673 | if (!refsafe(old, cur)) | |
c9e73e3d LB |
11674 | return false; |
11675 | ||
11676 | return true; | |
f1bca824 AS |
11677 | } |
11678 | ||
f4d7e40a AS |
11679 | static bool states_equal(struct bpf_verifier_env *env, |
11680 | struct bpf_verifier_state *old, | |
11681 | struct bpf_verifier_state *cur) | |
11682 | { | |
11683 | int i; | |
11684 | ||
11685 | if (old->curframe != cur->curframe) | |
11686 | return false; | |
11687 | ||
979d63d5 DB |
11688 | /* Verification state from speculative execution simulation |
11689 | * must never prune a non-speculative execution one. | |
11690 | */ | |
11691 | if (old->speculative && !cur->speculative) | |
11692 | return false; | |
11693 | ||
d83525ca AS |
11694 | if (old->active_spin_lock != cur->active_spin_lock) |
11695 | return false; | |
11696 | ||
f4d7e40a AS |
11697 | /* for states to be equal callsites have to be the same |
11698 | * and all frame states need to be equivalent | |
11699 | */ | |
11700 | for (i = 0; i <= old->curframe; i++) { | |
11701 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
11702 | return false; | |
c9e73e3d | 11703 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
11704 | return false; |
11705 | } | |
11706 | return true; | |
11707 | } | |
11708 | ||
5327ed3d JW |
11709 | /* Return 0 if no propagation happened. Return negative error code if error |
11710 | * happened. Otherwise, return the propagated bit. | |
11711 | */ | |
55e7f3b5 JW |
11712 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
11713 | struct bpf_reg_state *reg, | |
11714 | struct bpf_reg_state *parent_reg) | |
11715 | { | |
5327ed3d JW |
11716 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
11717 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
11718 | int err; |
11719 | ||
5327ed3d JW |
11720 | /* When comes here, read flags of PARENT_REG or REG could be any of |
11721 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
11722 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
11723 | */ | |
11724 | if (parent_flag == REG_LIVE_READ64 || | |
11725 | /* Or if there is no read flag from REG. */ | |
11726 | !flag || | |
11727 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
11728 | parent_flag == flag) | |
55e7f3b5 JW |
11729 | return 0; |
11730 | ||
5327ed3d | 11731 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
11732 | if (err) |
11733 | return err; | |
11734 | ||
5327ed3d | 11735 | return flag; |
55e7f3b5 JW |
11736 | } |
11737 | ||
8e9cd9ce | 11738 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
11739 | * straight-line code between a state and its parent. When we arrive at an |
11740 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
11741 | * code, so read marks in the state must propagate to the parent regardless | |
11742 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 11743 | * in mark_reg_read() is for. |
8e9cd9ce | 11744 | */ |
f4d7e40a AS |
11745 | static int propagate_liveness(struct bpf_verifier_env *env, |
11746 | const struct bpf_verifier_state *vstate, | |
11747 | struct bpf_verifier_state *vparent) | |
dc503a8a | 11748 | { |
3f8cafa4 | 11749 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 11750 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 11751 | int i, frame, err = 0; |
dc503a8a | 11752 | |
f4d7e40a AS |
11753 | if (vparent->curframe != vstate->curframe) { |
11754 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
11755 | vparent->curframe, vstate->curframe); | |
11756 | return -EFAULT; | |
11757 | } | |
dc503a8a EC |
11758 | /* Propagate read liveness of registers... */ |
11759 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 11760 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
11761 | parent = vparent->frame[frame]; |
11762 | state = vstate->frame[frame]; | |
11763 | parent_reg = parent->regs; | |
11764 | state_reg = state->regs; | |
83d16312 JK |
11765 | /* We don't need to worry about FP liveness, it's read-only */ |
11766 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
11767 | err = propagate_liveness_reg(env, &state_reg[i], |
11768 | &parent_reg[i]); | |
5327ed3d | 11769 | if (err < 0) |
3f8cafa4 | 11770 | return err; |
5327ed3d JW |
11771 | if (err == REG_LIVE_READ64) |
11772 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 11773 | } |
f4d7e40a | 11774 | |
1b04aee7 | 11775 | /* Propagate stack slots. */ |
f4d7e40a AS |
11776 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
11777 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
11778 | parent_reg = &parent->stack[i].spilled_ptr; |
11779 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
11780 | err = propagate_liveness_reg(env, state_reg, |
11781 | parent_reg); | |
5327ed3d | 11782 | if (err < 0) |
3f8cafa4 | 11783 | return err; |
dc503a8a EC |
11784 | } |
11785 | } | |
5327ed3d | 11786 | return 0; |
dc503a8a EC |
11787 | } |
11788 | ||
a3ce685d AS |
11789 | /* find precise scalars in the previous equivalent state and |
11790 | * propagate them into the current state | |
11791 | */ | |
11792 | static int propagate_precision(struct bpf_verifier_env *env, | |
11793 | const struct bpf_verifier_state *old) | |
11794 | { | |
11795 | struct bpf_reg_state *state_reg; | |
11796 | struct bpf_func_state *state; | |
11797 | int i, err = 0; | |
11798 | ||
11799 | state = old->frame[old->curframe]; | |
11800 | state_reg = state->regs; | |
11801 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
11802 | if (state_reg->type != SCALAR_VALUE || | |
11803 | !state_reg->precise) | |
11804 | continue; | |
11805 | if (env->log.level & BPF_LOG_LEVEL2) | |
11806 | verbose(env, "propagating r%d\n", i); | |
11807 | err = mark_chain_precision(env, i); | |
11808 | if (err < 0) | |
11809 | return err; | |
11810 | } | |
11811 | ||
11812 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
27113c59 | 11813 | if (!is_spilled_reg(&state->stack[i])) |
a3ce685d AS |
11814 | continue; |
11815 | state_reg = &state->stack[i].spilled_ptr; | |
11816 | if (state_reg->type != SCALAR_VALUE || | |
11817 | !state_reg->precise) | |
11818 | continue; | |
11819 | if (env->log.level & BPF_LOG_LEVEL2) | |
11820 | verbose(env, "propagating fp%d\n", | |
11821 | (-i - 1) * BPF_REG_SIZE); | |
11822 | err = mark_chain_precision_stack(env, i); | |
11823 | if (err < 0) | |
11824 | return err; | |
11825 | } | |
11826 | return 0; | |
11827 | } | |
11828 | ||
2589726d AS |
11829 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
11830 | struct bpf_verifier_state *cur) | |
11831 | { | |
11832 | struct bpf_func_state *fold, *fcur; | |
11833 | int i, fr = cur->curframe; | |
11834 | ||
11835 | if (old->curframe != fr) | |
11836 | return false; | |
11837 | ||
11838 | fold = old->frame[fr]; | |
11839 | fcur = cur->frame[fr]; | |
11840 | for (i = 0; i < MAX_BPF_REG; i++) | |
11841 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
11842 | offsetof(struct bpf_reg_state, parent))) | |
11843 | return false; | |
11844 | return true; | |
11845 | } | |
11846 | ||
11847 | ||
58e2af8b | 11848 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 11849 | { |
58e2af8b | 11850 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 11851 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 11852 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 11853 | int i, j, err, states_cnt = 0; |
10d274e8 | 11854 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 11855 | |
b5dc0163 | 11856 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 11857 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
11858 | /* this 'insn_idx' instruction wasn't marked, so we will not |
11859 | * be doing state search here | |
11860 | */ | |
11861 | return 0; | |
11862 | ||
2589726d AS |
11863 | /* bpf progs typically have pruning point every 4 instructions |
11864 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
11865 | * Do not add new state for future pruning if the verifier hasn't seen | |
11866 | * at least 2 jumps and at least 8 instructions. | |
11867 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
11868 | * In tests that amounts to up to 50% reduction into total verifier | |
11869 | * memory consumption and 20% verifier time speedup. | |
11870 | */ | |
11871 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
11872 | env->insn_processed - env->prev_insn_processed >= 8) | |
11873 | add_new_state = true; | |
11874 | ||
a8f500af AS |
11875 | pprev = explored_state(env, insn_idx); |
11876 | sl = *pprev; | |
11877 | ||
9242b5f5 AS |
11878 | clean_live_states(env, insn_idx, cur); |
11879 | ||
a8f500af | 11880 | while (sl) { |
dc2a4ebc AS |
11881 | states_cnt++; |
11882 | if (sl->state.insn_idx != insn_idx) | |
11883 | goto next; | |
bfc6bb74 | 11884 | |
2589726d | 11885 | if (sl->state.branches) { |
bfc6bb74 AS |
11886 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
11887 | ||
11888 | if (frame->in_async_callback_fn && | |
11889 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
11890 | /* Different async_entry_cnt means that the verifier is | |
11891 | * processing another entry into async callback. | |
11892 | * Seeing the same state is not an indication of infinite | |
11893 | * loop or infinite recursion. | |
11894 | * But finding the same state doesn't mean that it's safe | |
11895 | * to stop processing the current state. The previous state | |
11896 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
11897 | * Checking in_async_callback_fn alone is not enough either. | |
11898 | * Since the verifier still needs to catch infinite loops | |
11899 | * inside async callbacks. | |
11900 | */ | |
11901 | } else if (states_maybe_looping(&sl->state, cur) && | |
11902 | states_equal(env, &sl->state, cur)) { | |
2589726d AS |
11903 | verbose_linfo(env, insn_idx, "; "); |
11904 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
11905 | return -EINVAL; | |
11906 | } | |
11907 | /* if the verifier is processing a loop, avoid adding new state | |
11908 | * too often, since different loop iterations have distinct | |
11909 | * states and may not help future pruning. | |
11910 | * This threshold shouldn't be too low to make sure that | |
11911 | * a loop with large bound will be rejected quickly. | |
11912 | * The most abusive loop will be: | |
11913 | * r1 += 1 | |
11914 | * if r1 < 1000000 goto pc-2 | |
11915 | * 1M insn_procssed limit / 100 == 10k peak states. | |
11916 | * This threshold shouldn't be too high either, since states | |
11917 | * at the end of the loop are likely to be useful in pruning. | |
11918 | */ | |
11919 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
11920 | env->insn_processed - env->prev_insn_processed < 100) | |
11921 | add_new_state = false; | |
11922 | goto miss; | |
11923 | } | |
638f5b90 | 11924 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 11925 | sl->hit_cnt++; |
f1bca824 | 11926 | /* reached equivalent register/stack state, |
dc503a8a EC |
11927 | * prune the search. |
11928 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
11929 | * If we have any write marks in env->cur_state, they |
11930 | * will prevent corresponding reads in the continuation | |
11931 | * from reaching our parent (an explored_state). Our | |
11932 | * own state will get the read marks recorded, but | |
11933 | * they'll be immediately forgotten as we're pruning | |
11934 | * this state and will pop a new one. | |
f1bca824 | 11935 | */ |
f4d7e40a | 11936 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
11937 | |
11938 | /* if previous state reached the exit with precision and | |
11939 | * current state is equivalent to it (except precsion marks) | |
11940 | * the precision needs to be propagated back in | |
11941 | * the current state. | |
11942 | */ | |
11943 | err = err ? : push_jmp_history(env, cur); | |
11944 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
11945 | if (err) |
11946 | return err; | |
f1bca824 | 11947 | return 1; |
dc503a8a | 11948 | } |
2589726d AS |
11949 | miss: |
11950 | /* when new state is not going to be added do not increase miss count. | |
11951 | * Otherwise several loop iterations will remove the state | |
11952 | * recorded earlier. The goal of these heuristics is to have | |
11953 | * states from some iterations of the loop (some in the beginning | |
11954 | * and some at the end) to help pruning. | |
11955 | */ | |
11956 | if (add_new_state) | |
11957 | sl->miss_cnt++; | |
9f4686c4 AS |
11958 | /* heuristic to determine whether this state is beneficial |
11959 | * to keep checking from state equivalence point of view. | |
11960 | * Higher numbers increase max_states_per_insn and verification time, | |
11961 | * but do not meaningfully decrease insn_processed. | |
11962 | */ | |
11963 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
11964 | /* the state is unlikely to be useful. Remove it to | |
11965 | * speed up verification | |
11966 | */ | |
11967 | *pprev = sl->next; | |
11968 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
11969 | u32 br = sl->state.branches; |
11970 | ||
11971 | WARN_ONCE(br, | |
11972 | "BUG live_done but branches_to_explore %d\n", | |
11973 | br); | |
9f4686c4 AS |
11974 | free_verifier_state(&sl->state, false); |
11975 | kfree(sl); | |
11976 | env->peak_states--; | |
11977 | } else { | |
11978 | /* cannot free this state, since parentage chain may | |
11979 | * walk it later. Add it for free_list instead to | |
11980 | * be freed at the end of verification | |
11981 | */ | |
11982 | sl->next = env->free_list; | |
11983 | env->free_list = sl; | |
11984 | } | |
11985 | sl = *pprev; | |
11986 | continue; | |
11987 | } | |
dc2a4ebc | 11988 | next: |
9f4686c4 AS |
11989 | pprev = &sl->next; |
11990 | sl = *pprev; | |
f1bca824 AS |
11991 | } |
11992 | ||
06ee7115 AS |
11993 | if (env->max_states_per_insn < states_cnt) |
11994 | env->max_states_per_insn = states_cnt; | |
11995 | ||
2c78ee89 | 11996 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 11997 | return push_jmp_history(env, cur); |
ceefbc96 | 11998 | |
2589726d | 11999 | if (!add_new_state) |
b5dc0163 | 12000 | return push_jmp_history(env, cur); |
ceefbc96 | 12001 | |
2589726d AS |
12002 | /* There were no equivalent states, remember the current one. |
12003 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 12004 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 12005 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 12006 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
12007 | * again on the way to bpf_exit. |
12008 | * When looping the sl->state.branches will be > 0 and this state | |
12009 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 12010 | */ |
638f5b90 | 12011 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
12012 | if (!new_sl) |
12013 | return -ENOMEM; | |
06ee7115 AS |
12014 | env->total_states++; |
12015 | env->peak_states++; | |
2589726d AS |
12016 | env->prev_jmps_processed = env->jmps_processed; |
12017 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
12018 | |
12019 | /* add new state to the head of linked list */ | |
679c782d EC |
12020 | new = &new_sl->state; |
12021 | err = copy_verifier_state(new, cur); | |
1969db47 | 12022 | if (err) { |
679c782d | 12023 | free_verifier_state(new, false); |
1969db47 AS |
12024 | kfree(new_sl); |
12025 | return err; | |
12026 | } | |
dc2a4ebc | 12027 | new->insn_idx = insn_idx; |
2589726d AS |
12028 | WARN_ONCE(new->branches != 1, |
12029 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 12030 | |
2589726d | 12031 | cur->parent = new; |
b5dc0163 AS |
12032 | cur->first_insn_idx = insn_idx; |
12033 | clear_jmp_history(cur); | |
5d839021 AS |
12034 | new_sl->next = *explored_state(env, insn_idx); |
12035 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
12036 | /* connect new state to parentage chain. Current frame needs all |
12037 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
12038 | * to the stack implicitly by JITs) so in callers' frames connect just | |
12039 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
12040 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
12041 | * from callee with its full parentage chain, anyway. | |
12042 | */ | |
8e9cd9ce EC |
12043 | /* clear write marks in current state: the writes we did are not writes |
12044 | * our child did, so they don't screen off its reads from us. | |
12045 | * (There are no read marks in current state, because reads always mark | |
12046 | * their parent and current state never has children yet. Only | |
12047 | * explored_states can get read marks.) | |
12048 | */ | |
eea1c227 AS |
12049 | for (j = 0; j <= cur->curframe; j++) { |
12050 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
12051 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
12052 | for (i = 0; i < BPF_REG_FP; i++) | |
12053 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
12054 | } | |
f4d7e40a AS |
12055 | |
12056 | /* all stack frames are accessible from callee, clear them all */ | |
12057 | for (j = 0; j <= cur->curframe; j++) { | |
12058 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 12059 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 12060 | |
679c782d | 12061 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 12062 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
12063 | frame->stack[i].spilled_ptr.parent = |
12064 | &newframe->stack[i].spilled_ptr; | |
12065 | } | |
f4d7e40a | 12066 | } |
f1bca824 AS |
12067 | return 0; |
12068 | } | |
12069 | ||
c64b7983 JS |
12070 | /* Return true if it's OK to have the same insn return a different type. */ |
12071 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
12072 | { | |
c25b2ae1 | 12073 | switch (base_type(type)) { |
c64b7983 JS |
12074 | case PTR_TO_CTX: |
12075 | case PTR_TO_SOCKET: | |
46f8bc92 | 12076 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 12077 | case PTR_TO_TCP_SOCK: |
fada7fdc | 12078 | case PTR_TO_XDP_SOCK: |
2a02759e | 12079 | case PTR_TO_BTF_ID: |
c64b7983 JS |
12080 | return false; |
12081 | default: | |
12082 | return true; | |
12083 | } | |
12084 | } | |
12085 | ||
12086 | /* If an instruction was previously used with particular pointer types, then we | |
12087 | * need to be careful to avoid cases such as the below, where it may be ok | |
12088 | * for one branch accessing the pointer, but not ok for the other branch: | |
12089 | * | |
12090 | * R1 = sock_ptr | |
12091 | * goto X; | |
12092 | * ... | |
12093 | * R1 = some_other_valid_ptr; | |
12094 | * goto X; | |
12095 | * ... | |
12096 | * R2 = *(u32 *)(R1 + 0); | |
12097 | */ | |
12098 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
12099 | { | |
12100 | return src != prev && (!reg_type_mismatch_ok(src) || | |
12101 | !reg_type_mismatch_ok(prev)); | |
12102 | } | |
12103 | ||
58e2af8b | 12104 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 12105 | { |
6f8a57cc | 12106 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 12107 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 12108 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 12109 | struct bpf_reg_state *regs; |
06ee7115 | 12110 | int insn_cnt = env->prog->len; |
17a52670 | 12111 | bool do_print_state = false; |
b5dc0163 | 12112 | int prev_insn_idx = -1; |
17a52670 | 12113 | |
17a52670 AS |
12114 | for (;;) { |
12115 | struct bpf_insn *insn; | |
12116 | u8 class; | |
12117 | int err; | |
12118 | ||
b5dc0163 | 12119 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 12120 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 12121 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 12122 | env->insn_idx, insn_cnt); |
17a52670 AS |
12123 | return -EFAULT; |
12124 | } | |
12125 | ||
c08435ec | 12126 | insn = &insns[env->insn_idx]; |
17a52670 AS |
12127 | class = BPF_CLASS(insn->code); |
12128 | ||
06ee7115 | 12129 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
12130 | verbose(env, |
12131 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 12132 | env->insn_processed); |
17a52670 AS |
12133 | return -E2BIG; |
12134 | } | |
12135 | ||
c08435ec | 12136 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
12137 | if (err < 0) |
12138 | return err; | |
12139 | if (err == 1) { | |
12140 | /* found equivalent state, can prune the search */ | |
06ee7115 | 12141 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 12142 | if (do_print_state) |
979d63d5 DB |
12143 | verbose(env, "\nfrom %d to %d%s: safe\n", |
12144 | env->prev_insn_idx, env->insn_idx, | |
12145 | env->cur_state->speculative ? | |
12146 | " (speculative execution)" : ""); | |
f1bca824 | 12147 | else |
c08435ec | 12148 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
12149 | } |
12150 | goto process_bpf_exit; | |
12151 | } | |
12152 | ||
c3494801 AS |
12153 | if (signal_pending(current)) |
12154 | return -EAGAIN; | |
12155 | ||
3c2ce60b DB |
12156 | if (need_resched()) |
12157 | cond_resched(); | |
12158 | ||
2e576648 CL |
12159 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
12160 | verbose(env, "\nfrom %d to %d%s:", | |
12161 | env->prev_insn_idx, env->insn_idx, | |
12162 | env->cur_state->speculative ? | |
12163 | " (speculative execution)" : ""); | |
12164 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
12165 | do_print_state = false; |
12166 | } | |
12167 | ||
06ee7115 | 12168 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 12169 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 12170 | .cb_call = disasm_kfunc_name, |
7105e828 | 12171 | .cb_print = verbose, |
abe08840 | 12172 | .private_data = env, |
7105e828 DB |
12173 | }; |
12174 | ||
2e576648 CL |
12175 | if (verifier_state_scratched(env)) |
12176 | print_insn_state(env, state->frame[state->curframe]); | |
12177 | ||
c08435ec | 12178 | verbose_linfo(env, env->insn_idx, "; "); |
2e576648 | 12179 | env->prev_log_len = env->log.len_used; |
c08435ec | 12180 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 12181 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
2e576648 CL |
12182 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
12183 | env->prev_log_len = env->log.len_used; | |
17a52670 AS |
12184 | } |
12185 | ||
cae1927c | 12186 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
12187 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
12188 | env->prev_insn_idx); | |
cae1927c JK |
12189 | if (err) |
12190 | return err; | |
12191 | } | |
13a27dfc | 12192 | |
638f5b90 | 12193 | regs = cur_regs(env); |
fe9a5ca7 | 12194 | sanitize_mark_insn_seen(env); |
b5dc0163 | 12195 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 12196 | |
17a52670 | 12197 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 12198 | err = check_alu_op(env, insn); |
17a52670 AS |
12199 | if (err) |
12200 | return err; | |
12201 | ||
12202 | } else if (class == BPF_LDX) { | |
3df126f3 | 12203 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
12204 | |
12205 | /* check for reserved fields is already done */ | |
12206 | ||
17a52670 | 12207 | /* check src operand */ |
dc503a8a | 12208 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12209 | if (err) |
12210 | return err; | |
12211 | ||
dc503a8a | 12212 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
12213 | if (err) |
12214 | return err; | |
12215 | ||
725f9dcd AS |
12216 | src_reg_type = regs[insn->src_reg].type; |
12217 | ||
17a52670 AS |
12218 | /* check that memory (src_reg + off) is readable, |
12219 | * the state of dst_reg will be updated by this func | |
12220 | */ | |
c08435ec DB |
12221 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
12222 | insn->off, BPF_SIZE(insn->code), | |
12223 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
12224 | if (err) |
12225 | return err; | |
12226 | ||
c08435ec | 12227 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
12228 | |
12229 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
12230 | /* saw a valid insn |
12231 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 12232 | * save type to validate intersecting paths |
9bac3d6d | 12233 | */ |
3df126f3 | 12234 | *prev_src_type = src_reg_type; |
9bac3d6d | 12235 | |
c64b7983 | 12236 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
12237 | /* ABuser program is trying to use the same insn |
12238 | * dst_reg = *(u32*) (src_reg + off) | |
12239 | * with different pointer types: | |
12240 | * src_reg == ctx in one branch and | |
12241 | * src_reg == stack|map in some other branch. | |
12242 | * Reject it. | |
12243 | */ | |
61bd5218 | 12244 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
12245 | return -EINVAL; |
12246 | } | |
12247 | ||
17a52670 | 12248 | } else if (class == BPF_STX) { |
3df126f3 | 12249 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 12250 | |
91c960b0 BJ |
12251 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
12252 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
12253 | if (err) |
12254 | return err; | |
c08435ec | 12255 | env->insn_idx++; |
17a52670 AS |
12256 | continue; |
12257 | } | |
12258 | ||
5ca419f2 BJ |
12259 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
12260 | verbose(env, "BPF_STX uses reserved fields\n"); | |
12261 | return -EINVAL; | |
12262 | } | |
12263 | ||
17a52670 | 12264 | /* check src1 operand */ |
dc503a8a | 12265 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
12266 | if (err) |
12267 | return err; | |
12268 | /* check src2 operand */ | |
dc503a8a | 12269 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12270 | if (err) |
12271 | return err; | |
12272 | ||
d691f9e8 AS |
12273 | dst_reg_type = regs[insn->dst_reg].type; |
12274 | ||
17a52670 | 12275 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
12276 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
12277 | insn->off, BPF_SIZE(insn->code), | |
12278 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
12279 | if (err) |
12280 | return err; | |
12281 | ||
c08435ec | 12282 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
12283 | |
12284 | if (*prev_dst_type == NOT_INIT) { | |
12285 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 12286 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 12287 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
12288 | return -EINVAL; |
12289 | } | |
12290 | ||
17a52670 AS |
12291 | } else if (class == BPF_ST) { |
12292 | if (BPF_MODE(insn->code) != BPF_MEM || | |
12293 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 12294 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
12295 | return -EINVAL; |
12296 | } | |
12297 | /* check src operand */ | |
dc503a8a | 12298 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
12299 | if (err) |
12300 | return err; | |
12301 | ||
f37a8cb8 | 12302 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 12303 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f | 12304 | insn->dst_reg, |
c25b2ae1 | 12305 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
12306 | return -EACCES; |
12307 | } | |
12308 | ||
17a52670 | 12309 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
12310 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
12311 | insn->off, BPF_SIZE(insn->code), | |
12312 | BPF_WRITE, -1, false); | |
17a52670 AS |
12313 | if (err) |
12314 | return err; | |
12315 | ||
092ed096 | 12316 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
12317 | u8 opcode = BPF_OP(insn->code); |
12318 | ||
2589726d | 12319 | env->jmps_processed++; |
17a52670 AS |
12320 | if (opcode == BPF_CALL) { |
12321 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
12322 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
12323 | && insn->off != 0) || | |
f4d7e40a | 12324 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
12325 | insn->src_reg != BPF_PSEUDO_CALL && |
12326 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
12327 | insn->dst_reg != BPF_REG_0 || |
12328 | class == BPF_JMP32) { | |
61bd5218 | 12329 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
12330 | return -EINVAL; |
12331 | } | |
12332 | ||
d83525ca AS |
12333 | if (env->cur_state->active_spin_lock && |
12334 | (insn->src_reg == BPF_PSEUDO_CALL || | |
12335 | insn->imm != BPF_FUNC_spin_unlock)) { | |
12336 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
12337 | return -EINVAL; | |
12338 | } | |
f4d7e40a | 12339 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 12340 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 | 12341 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
5c073f26 | 12342 | err = check_kfunc_call(env, insn, &env->insn_idx); |
f4d7e40a | 12343 | else |
69c087ba | 12344 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
12345 | if (err) |
12346 | return err; | |
17a52670 AS |
12347 | } else if (opcode == BPF_JA) { |
12348 | if (BPF_SRC(insn->code) != BPF_K || | |
12349 | insn->imm != 0 || | |
12350 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
12351 | insn->dst_reg != BPF_REG_0 || |
12352 | class == BPF_JMP32) { | |
61bd5218 | 12353 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
12354 | return -EINVAL; |
12355 | } | |
12356 | ||
c08435ec | 12357 | env->insn_idx += insn->off + 1; |
17a52670 AS |
12358 | continue; |
12359 | ||
12360 | } else if (opcode == BPF_EXIT) { | |
12361 | if (BPF_SRC(insn->code) != BPF_K || | |
12362 | insn->imm != 0 || | |
12363 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
12364 | insn->dst_reg != BPF_REG_0 || |
12365 | class == BPF_JMP32) { | |
61bd5218 | 12366 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
12367 | return -EINVAL; |
12368 | } | |
12369 | ||
d83525ca AS |
12370 | if (env->cur_state->active_spin_lock) { |
12371 | verbose(env, "bpf_spin_unlock is missing\n"); | |
12372 | return -EINVAL; | |
12373 | } | |
12374 | ||
9d9d00ac KKD |
12375 | /* We must do check_reference_leak here before |
12376 | * prepare_func_exit to handle the case when | |
12377 | * state->curframe > 0, it may be a callback | |
12378 | * function, for which reference_state must | |
12379 | * match caller reference state when it exits. | |
12380 | */ | |
12381 | err = check_reference_leak(env); | |
12382 | if (err) | |
12383 | return err; | |
12384 | ||
f4d7e40a AS |
12385 | if (state->curframe) { |
12386 | /* exit from nested function */ | |
c08435ec | 12387 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
12388 | if (err) |
12389 | return err; | |
12390 | do_print_state = true; | |
12391 | continue; | |
12392 | } | |
12393 | ||
390ee7e2 AS |
12394 | err = check_return_code(env); |
12395 | if (err) | |
12396 | return err; | |
f1bca824 | 12397 | process_bpf_exit: |
0f55f9ed | 12398 | mark_verifier_state_scratched(env); |
2589726d | 12399 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 12400 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 12401 | &env->insn_idx, pop_log); |
638f5b90 AS |
12402 | if (err < 0) { |
12403 | if (err != -ENOENT) | |
12404 | return err; | |
17a52670 AS |
12405 | break; |
12406 | } else { | |
12407 | do_print_state = true; | |
12408 | continue; | |
12409 | } | |
12410 | } else { | |
c08435ec | 12411 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
12412 | if (err) |
12413 | return err; | |
12414 | } | |
12415 | } else if (class == BPF_LD) { | |
12416 | u8 mode = BPF_MODE(insn->code); | |
12417 | ||
12418 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
12419 | err = check_ld_abs(env, insn); |
12420 | if (err) | |
12421 | return err; | |
12422 | ||
17a52670 AS |
12423 | } else if (mode == BPF_IMM) { |
12424 | err = check_ld_imm(env, insn); | |
12425 | if (err) | |
12426 | return err; | |
12427 | ||
c08435ec | 12428 | env->insn_idx++; |
fe9a5ca7 | 12429 | sanitize_mark_insn_seen(env); |
17a52670 | 12430 | } else { |
61bd5218 | 12431 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
12432 | return -EINVAL; |
12433 | } | |
12434 | } else { | |
61bd5218 | 12435 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
12436 | return -EINVAL; |
12437 | } | |
12438 | ||
c08435ec | 12439 | env->insn_idx++; |
17a52670 AS |
12440 | } |
12441 | ||
12442 | return 0; | |
12443 | } | |
12444 | ||
541c3bad AN |
12445 | static int find_btf_percpu_datasec(struct btf *btf) |
12446 | { | |
12447 | const struct btf_type *t; | |
12448 | const char *tname; | |
12449 | int i, n; | |
12450 | ||
12451 | /* | |
12452 | * Both vmlinux and module each have their own ".data..percpu" | |
12453 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
12454 | * types to look at only module's own BTF types. | |
12455 | */ | |
12456 | n = btf_nr_types(btf); | |
12457 | if (btf_is_module(btf)) | |
12458 | i = btf_nr_types(btf_vmlinux); | |
12459 | else | |
12460 | i = 1; | |
12461 | ||
12462 | for(; i < n; i++) { | |
12463 | t = btf_type_by_id(btf, i); | |
12464 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
12465 | continue; | |
12466 | ||
12467 | tname = btf_name_by_offset(btf, t->name_off); | |
12468 | if (!strcmp(tname, ".data..percpu")) | |
12469 | return i; | |
12470 | } | |
12471 | ||
12472 | return -ENOENT; | |
12473 | } | |
12474 | ||
4976b718 HL |
12475 | /* replace pseudo btf_id with kernel symbol address */ |
12476 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
12477 | struct bpf_insn *insn, | |
12478 | struct bpf_insn_aux_data *aux) | |
12479 | { | |
eaa6bcb7 HL |
12480 | const struct btf_var_secinfo *vsi; |
12481 | const struct btf_type *datasec; | |
541c3bad | 12482 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
12483 | const struct btf_type *t; |
12484 | const char *sym_name; | |
eaa6bcb7 | 12485 | bool percpu = false; |
f16e6313 | 12486 | u32 type, id = insn->imm; |
541c3bad | 12487 | struct btf *btf; |
f16e6313 | 12488 | s32 datasec_id; |
4976b718 | 12489 | u64 addr; |
541c3bad | 12490 | int i, btf_fd, err; |
4976b718 | 12491 | |
541c3bad AN |
12492 | btf_fd = insn[1].imm; |
12493 | if (btf_fd) { | |
12494 | btf = btf_get_by_fd(btf_fd); | |
12495 | if (IS_ERR(btf)) { | |
12496 | verbose(env, "invalid module BTF object FD specified.\n"); | |
12497 | return -EINVAL; | |
12498 | } | |
12499 | } else { | |
12500 | if (!btf_vmlinux) { | |
12501 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
12502 | return -EINVAL; | |
12503 | } | |
12504 | btf = btf_vmlinux; | |
12505 | btf_get(btf); | |
4976b718 HL |
12506 | } |
12507 | ||
541c3bad | 12508 | t = btf_type_by_id(btf, id); |
4976b718 HL |
12509 | if (!t) { |
12510 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
12511 | err = -ENOENT; |
12512 | goto err_put; | |
4976b718 HL |
12513 | } |
12514 | ||
12515 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
12516 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
12517 | err = -EINVAL; | |
12518 | goto err_put; | |
4976b718 HL |
12519 | } |
12520 | ||
541c3bad | 12521 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
12522 | addr = kallsyms_lookup_name(sym_name); |
12523 | if (!addr) { | |
12524 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
12525 | sym_name); | |
541c3bad AN |
12526 | err = -ENOENT; |
12527 | goto err_put; | |
4976b718 HL |
12528 | } |
12529 | ||
541c3bad | 12530 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 12531 | if (datasec_id > 0) { |
541c3bad | 12532 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
12533 | for_each_vsi(i, datasec, vsi) { |
12534 | if (vsi->type == id) { | |
12535 | percpu = true; | |
12536 | break; | |
12537 | } | |
12538 | } | |
12539 | } | |
12540 | ||
4976b718 HL |
12541 | insn[0].imm = (u32)addr; |
12542 | insn[1].imm = addr >> 32; | |
12543 | ||
12544 | type = t->type; | |
541c3bad | 12545 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 | 12546 | if (percpu) { |
5844101a | 12547 | aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; |
541c3bad | 12548 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
12549 | aux->btf_var.btf_id = type; |
12550 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
12551 | const struct btf_type *ret; |
12552 | const char *tname; | |
12553 | u32 tsize; | |
12554 | ||
12555 | /* resolve the type size of ksym. */ | |
541c3bad | 12556 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 12557 | if (IS_ERR(ret)) { |
541c3bad | 12558 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
12559 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
12560 | tname, PTR_ERR(ret)); | |
541c3bad AN |
12561 | err = -EINVAL; |
12562 | goto err_put; | |
4976b718 | 12563 | } |
34d3a78c | 12564 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
4976b718 HL |
12565 | aux->btf_var.mem_size = tsize; |
12566 | } else { | |
12567 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 12568 | aux->btf_var.btf = btf; |
4976b718 HL |
12569 | aux->btf_var.btf_id = type; |
12570 | } | |
541c3bad AN |
12571 | |
12572 | /* check whether we recorded this BTF (and maybe module) already */ | |
12573 | for (i = 0; i < env->used_btf_cnt; i++) { | |
12574 | if (env->used_btfs[i].btf == btf) { | |
12575 | btf_put(btf); | |
12576 | return 0; | |
12577 | } | |
12578 | } | |
12579 | ||
12580 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
12581 | err = -E2BIG; | |
12582 | goto err_put; | |
12583 | } | |
12584 | ||
12585 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
12586 | btf_mod->btf = btf; | |
12587 | btf_mod->module = NULL; | |
12588 | ||
12589 | /* if we reference variables from kernel module, bump its refcount */ | |
12590 | if (btf_is_module(btf)) { | |
12591 | btf_mod->module = btf_try_get_module(btf); | |
12592 | if (!btf_mod->module) { | |
12593 | err = -ENXIO; | |
12594 | goto err_put; | |
12595 | } | |
12596 | } | |
12597 | ||
12598 | env->used_btf_cnt++; | |
12599 | ||
4976b718 | 12600 | return 0; |
541c3bad AN |
12601 | err_put: |
12602 | btf_put(btf); | |
12603 | return err; | |
4976b718 HL |
12604 | } |
12605 | ||
d83525ca AS |
12606 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
12607 | { | |
12608 | switch (type) { | |
12609 | case BPF_PROG_TYPE_KPROBE: | |
12610 | case BPF_PROG_TYPE_TRACEPOINT: | |
12611 | case BPF_PROG_TYPE_PERF_EVENT: | |
12612 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
5002615a | 12613 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
d83525ca AS |
12614 | return true; |
12615 | default: | |
12616 | return false; | |
12617 | } | |
12618 | } | |
12619 | ||
61bd5218 JK |
12620 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
12621 | struct bpf_map *map, | |
fdc15d38 AS |
12622 | struct bpf_prog *prog) |
12623 | ||
12624 | { | |
7e40781c | 12625 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
a3884572 | 12626 | |
db559117 | 12627 | if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) { |
9e7a4d98 KS |
12628 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
12629 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
12630 | return -EINVAL; | |
12631 | } | |
12632 | ||
12633 | if (is_tracing_prog_type(prog_type)) { | |
12634 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
12635 | return -EINVAL; | |
12636 | } | |
12637 | ||
12638 | if (prog->aux->sleepable) { | |
12639 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
12640 | return -EINVAL; | |
12641 | } | |
d83525ca AS |
12642 | } |
12643 | ||
db559117 | 12644 | if (btf_record_has_field(map->record, BPF_TIMER)) { |
5e0bc308 DB |
12645 | if (is_tracing_prog_type(prog_type)) { |
12646 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
12647 | return -EINVAL; | |
12648 | } | |
12649 | } | |
12650 | ||
a3884572 | 12651 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 12652 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
12653 | verbose(env, "offload device mismatch between prog and map\n"); |
12654 | return -EINVAL; | |
12655 | } | |
12656 | ||
85d33df3 MKL |
12657 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
12658 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
12659 | return -EINVAL; | |
12660 | } | |
12661 | ||
1e6c62a8 AS |
12662 | if (prog->aux->sleepable) |
12663 | switch (map->map_type) { | |
12664 | case BPF_MAP_TYPE_HASH: | |
12665 | case BPF_MAP_TYPE_LRU_HASH: | |
12666 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
12667 | case BPF_MAP_TYPE_PERCPU_HASH: |
12668 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
12669 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
12670 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
12671 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
ba90c2cc | 12672 | case BPF_MAP_TYPE_RINGBUF: |
583c1f42 | 12673 | case BPF_MAP_TYPE_USER_RINGBUF: |
0fe4b381 KS |
12674 | case BPF_MAP_TYPE_INODE_STORAGE: |
12675 | case BPF_MAP_TYPE_SK_STORAGE: | |
12676 | case BPF_MAP_TYPE_TASK_STORAGE: | |
ba90c2cc | 12677 | break; |
1e6c62a8 AS |
12678 | default: |
12679 | verbose(env, | |
ba90c2cc | 12680 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
1e6c62a8 AS |
12681 | return -EINVAL; |
12682 | } | |
12683 | ||
fdc15d38 AS |
12684 | return 0; |
12685 | } | |
12686 | ||
b741f163 RG |
12687 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
12688 | { | |
12689 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
12690 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
12691 | } | |
12692 | ||
4976b718 HL |
12693 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
12694 | * | |
12695 | * 1. if it accesses map FD, replace it with actual map pointer. | |
12696 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
12697 | * | |
12698 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 12699 | */ |
4976b718 | 12700 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
12701 | { |
12702 | struct bpf_insn *insn = env->prog->insnsi; | |
12703 | int insn_cnt = env->prog->len; | |
fdc15d38 | 12704 | int i, j, err; |
0246e64d | 12705 | |
f1f7714e | 12706 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
12707 | if (err) |
12708 | return err; | |
12709 | ||
0246e64d | 12710 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 12711 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 12712 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 12713 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
12714 | return -EINVAL; |
12715 | } | |
12716 | ||
0246e64d | 12717 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 12718 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
12719 | struct bpf_map *map; |
12720 | struct fd f; | |
d8eca5bb | 12721 | u64 addr; |
387544bf | 12722 | u32 fd; |
0246e64d AS |
12723 | |
12724 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
12725 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
12726 | insn[1].off != 0) { | |
61bd5218 | 12727 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
12728 | return -EINVAL; |
12729 | } | |
12730 | ||
d8eca5bb | 12731 | if (insn[0].src_reg == 0) |
0246e64d AS |
12732 | /* valid generic load 64-bit imm */ |
12733 | goto next_insn; | |
12734 | ||
4976b718 HL |
12735 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
12736 | aux = &env->insn_aux_data[i]; | |
12737 | err = check_pseudo_btf_id(env, insn, aux); | |
12738 | if (err) | |
12739 | return err; | |
12740 | goto next_insn; | |
12741 | } | |
12742 | ||
69c087ba YS |
12743 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
12744 | aux = &env->insn_aux_data[i]; | |
12745 | aux->ptr_type = PTR_TO_FUNC; | |
12746 | goto next_insn; | |
12747 | } | |
12748 | ||
d8eca5bb DB |
12749 | /* In final convert_pseudo_ld_imm64() step, this is |
12750 | * converted into regular 64-bit imm load insn. | |
12751 | */ | |
387544bf AS |
12752 | switch (insn[0].src_reg) { |
12753 | case BPF_PSEUDO_MAP_VALUE: | |
12754 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
12755 | break; | |
12756 | case BPF_PSEUDO_MAP_FD: | |
12757 | case BPF_PSEUDO_MAP_IDX: | |
12758 | if (insn[1].imm == 0) | |
12759 | break; | |
12760 | fallthrough; | |
12761 | default: | |
12762 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
12763 | return -EINVAL; |
12764 | } | |
12765 | ||
387544bf AS |
12766 | switch (insn[0].src_reg) { |
12767 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
12768 | case BPF_PSEUDO_MAP_IDX: | |
12769 | if (bpfptr_is_null(env->fd_array)) { | |
12770 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
12771 | return -EPROTO; | |
12772 | } | |
12773 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
12774 | insn[0].imm * sizeof(fd), | |
12775 | sizeof(fd))) | |
12776 | return -EFAULT; | |
12777 | break; | |
12778 | default: | |
12779 | fd = insn[0].imm; | |
12780 | break; | |
12781 | } | |
12782 | ||
12783 | f = fdget(fd); | |
c2101297 | 12784 | map = __bpf_map_get(f); |
0246e64d | 12785 | if (IS_ERR(map)) { |
61bd5218 | 12786 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 12787 | insn[0].imm); |
0246e64d AS |
12788 | return PTR_ERR(map); |
12789 | } | |
12790 | ||
61bd5218 | 12791 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
12792 | if (err) { |
12793 | fdput(f); | |
12794 | return err; | |
12795 | } | |
12796 | ||
d8eca5bb | 12797 | aux = &env->insn_aux_data[i]; |
387544bf AS |
12798 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
12799 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
12800 | addr = (unsigned long)map; |
12801 | } else { | |
12802 | u32 off = insn[1].imm; | |
12803 | ||
12804 | if (off >= BPF_MAX_VAR_OFF) { | |
12805 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
12806 | fdput(f); | |
12807 | return -EINVAL; | |
12808 | } | |
12809 | ||
12810 | if (!map->ops->map_direct_value_addr) { | |
12811 | verbose(env, "no direct value access support for this map type\n"); | |
12812 | fdput(f); | |
12813 | return -EINVAL; | |
12814 | } | |
12815 | ||
12816 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
12817 | if (err) { | |
12818 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
12819 | map->value_size, off); | |
12820 | fdput(f); | |
12821 | return err; | |
12822 | } | |
12823 | ||
12824 | aux->map_off = off; | |
12825 | addr += off; | |
12826 | } | |
12827 | ||
12828 | insn[0].imm = (u32)addr; | |
12829 | insn[1].imm = addr >> 32; | |
0246e64d AS |
12830 | |
12831 | /* check whether we recorded this map already */ | |
d8eca5bb | 12832 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 12833 | if (env->used_maps[j] == map) { |
d8eca5bb | 12834 | aux->map_index = j; |
0246e64d AS |
12835 | fdput(f); |
12836 | goto next_insn; | |
12837 | } | |
d8eca5bb | 12838 | } |
0246e64d AS |
12839 | |
12840 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
12841 | fdput(f); | |
12842 | return -E2BIG; | |
12843 | } | |
12844 | ||
0246e64d AS |
12845 | /* hold the map. If the program is rejected by verifier, |
12846 | * the map will be released by release_maps() or it | |
12847 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 12848 | * and all maps are released in free_used_maps() |
0246e64d | 12849 | */ |
1e0bd5a0 | 12850 | bpf_map_inc(map); |
d8eca5bb DB |
12851 | |
12852 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
12853 | env->used_maps[env->used_map_cnt++] = map; |
12854 | ||
b741f163 | 12855 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 12856 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 12857 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
12858 | fdput(f); |
12859 | return -EBUSY; | |
12860 | } | |
12861 | ||
0246e64d AS |
12862 | fdput(f); |
12863 | next_insn: | |
12864 | insn++; | |
12865 | i++; | |
5e581dad DB |
12866 | continue; |
12867 | } | |
12868 | ||
12869 | /* Basic sanity check before we invest more work here. */ | |
12870 | if (!bpf_opcode_in_insntable(insn->code)) { | |
12871 | verbose(env, "unknown opcode %02x\n", insn->code); | |
12872 | return -EINVAL; | |
0246e64d AS |
12873 | } |
12874 | } | |
12875 | ||
12876 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
12877 | * 'struct bpf_map *' into a register instead of user map_fd. | |
12878 | * These pointers will be used later by verifier to validate map access. | |
12879 | */ | |
12880 | return 0; | |
12881 | } | |
12882 | ||
12883 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 12884 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 12885 | { |
a2ea0746 DB |
12886 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
12887 | env->used_map_cnt); | |
0246e64d AS |
12888 | } |
12889 | ||
541c3bad AN |
12890 | /* drop refcnt of maps used by the rejected program */ |
12891 | static void release_btfs(struct bpf_verifier_env *env) | |
12892 | { | |
12893 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
12894 | env->used_btf_cnt); | |
12895 | } | |
12896 | ||
0246e64d | 12897 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 12898 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
12899 | { |
12900 | struct bpf_insn *insn = env->prog->insnsi; | |
12901 | int insn_cnt = env->prog->len; | |
12902 | int i; | |
12903 | ||
69c087ba YS |
12904 | for (i = 0; i < insn_cnt; i++, insn++) { |
12905 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
12906 | continue; | |
12907 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
12908 | continue; | |
12909 | insn->src_reg = 0; | |
12910 | } | |
0246e64d AS |
12911 | } |
12912 | ||
8041902d AS |
12913 | /* single env->prog->insni[off] instruction was replaced with the range |
12914 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
12915 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
12916 | */ | |
75f0fc7b HF |
12917 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
12918 | struct bpf_insn_aux_data *new_data, | |
12919 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 12920 | { |
75f0fc7b | 12921 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 12922 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 12923 | u32 old_seen = old_data[off].seen; |
b325fbca | 12924 | u32 prog_len; |
c131187d | 12925 | int i; |
8041902d | 12926 | |
b325fbca JW |
12927 | /* aux info at OFF always needs adjustment, no matter fast path |
12928 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
12929 | * original insn at old prog. | |
12930 | */ | |
12931 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
12932 | ||
8041902d | 12933 | if (cnt == 1) |
75f0fc7b | 12934 | return; |
b325fbca | 12935 | prog_len = new_prog->len; |
75f0fc7b | 12936 | |
8041902d AS |
12937 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
12938 | memcpy(new_data + off + cnt - 1, old_data + off, | |
12939 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 12940 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
12941 | /* Expand insni[off]'s seen count to the patched range. */ |
12942 | new_data[i].seen = old_seen; | |
b325fbca JW |
12943 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
12944 | } | |
8041902d AS |
12945 | env->insn_aux_data = new_data; |
12946 | vfree(old_data); | |
8041902d AS |
12947 | } |
12948 | ||
cc8b0b92 AS |
12949 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
12950 | { | |
12951 | int i; | |
12952 | ||
12953 | if (len == 1) | |
12954 | return; | |
4cb3d99c JW |
12955 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
12956 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 12957 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 12958 | continue; |
9c8105bd | 12959 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
12960 | } |
12961 | } | |
12962 | ||
7506d211 | 12963 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
12964 | { |
12965 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
12966 | int i, sz = prog->aux->size_poke_tab; | |
12967 | struct bpf_jit_poke_descriptor *desc; | |
12968 | ||
12969 | for (i = 0; i < sz; i++) { | |
12970 | desc = &tab[i]; | |
7506d211 JF |
12971 | if (desc->insn_idx <= off) |
12972 | continue; | |
a748c697 MF |
12973 | desc->insn_idx += len - 1; |
12974 | } | |
12975 | } | |
12976 | ||
8041902d AS |
12977 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
12978 | const struct bpf_insn *patch, u32 len) | |
12979 | { | |
12980 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
12981 | struct bpf_insn_aux_data *new_data = NULL; |
12982 | ||
12983 | if (len > 1) { | |
12984 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
12985 | sizeof(struct bpf_insn_aux_data))); | |
12986 | if (!new_data) | |
12987 | return NULL; | |
12988 | } | |
8041902d AS |
12989 | |
12990 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
12991 | if (IS_ERR(new_prog)) { |
12992 | if (PTR_ERR(new_prog) == -ERANGE) | |
12993 | verbose(env, | |
12994 | "insn %d cannot be patched due to 16-bit range\n", | |
12995 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 12996 | vfree(new_data); |
8041902d | 12997 | return NULL; |
4f73379e | 12998 | } |
75f0fc7b | 12999 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 13000 | adjust_subprog_starts(env, off, len); |
7506d211 | 13001 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
13002 | return new_prog; |
13003 | } | |
13004 | ||
52875a04 JK |
13005 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
13006 | u32 off, u32 cnt) | |
13007 | { | |
13008 | int i, j; | |
13009 | ||
13010 | /* find first prog starting at or after off (first to remove) */ | |
13011 | for (i = 0; i < env->subprog_cnt; i++) | |
13012 | if (env->subprog_info[i].start >= off) | |
13013 | break; | |
13014 | /* find first prog starting at or after off + cnt (first to stay) */ | |
13015 | for (j = i; j < env->subprog_cnt; j++) | |
13016 | if (env->subprog_info[j].start >= off + cnt) | |
13017 | break; | |
13018 | /* if j doesn't start exactly at off + cnt, we are just removing | |
13019 | * the front of previous prog | |
13020 | */ | |
13021 | if (env->subprog_info[j].start != off + cnt) | |
13022 | j--; | |
13023 | ||
13024 | if (j > i) { | |
13025 | struct bpf_prog_aux *aux = env->prog->aux; | |
13026 | int move; | |
13027 | ||
13028 | /* move fake 'exit' subprog as well */ | |
13029 | move = env->subprog_cnt + 1 - j; | |
13030 | ||
13031 | memmove(env->subprog_info + i, | |
13032 | env->subprog_info + j, | |
13033 | sizeof(*env->subprog_info) * move); | |
13034 | env->subprog_cnt -= j - i; | |
13035 | ||
13036 | /* remove func_info */ | |
13037 | if (aux->func_info) { | |
13038 | move = aux->func_info_cnt - j; | |
13039 | ||
13040 | memmove(aux->func_info + i, | |
13041 | aux->func_info + j, | |
13042 | sizeof(*aux->func_info) * move); | |
13043 | aux->func_info_cnt -= j - i; | |
13044 | /* func_info->insn_off is set after all code rewrites, | |
13045 | * in adjust_btf_func() - no need to adjust | |
13046 | */ | |
13047 | } | |
13048 | } else { | |
13049 | /* convert i from "first prog to remove" to "first to adjust" */ | |
13050 | if (env->subprog_info[i].start == off) | |
13051 | i++; | |
13052 | } | |
13053 | ||
13054 | /* update fake 'exit' subprog as well */ | |
13055 | for (; i <= env->subprog_cnt; i++) | |
13056 | env->subprog_info[i].start -= cnt; | |
13057 | ||
13058 | return 0; | |
13059 | } | |
13060 | ||
13061 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
13062 | u32 cnt) | |
13063 | { | |
13064 | struct bpf_prog *prog = env->prog; | |
13065 | u32 i, l_off, l_cnt, nr_linfo; | |
13066 | struct bpf_line_info *linfo; | |
13067 | ||
13068 | nr_linfo = prog->aux->nr_linfo; | |
13069 | if (!nr_linfo) | |
13070 | return 0; | |
13071 | ||
13072 | linfo = prog->aux->linfo; | |
13073 | ||
13074 | /* find first line info to remove, count lines to be removed */ | |
13075 | for (i = 0; i < nr_linfo; i++) | |
13076 | if (linfo[i].insn_off >= off) | |
13077 | break; | |
13078 | ||
13079 | l_off = i; | |
13080 | l_cnt = 0; | |
13081 | for (; i < nr_linfo; i++) | |
13082 | if (linfo[i].insn_off < off + cnt) | |
13083 | l_cnt++; | |
13084 | else | |
13085 | break; | |
13086 | ||
13087 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
13088 | * last removed linfo. prog is already modified, so prog->len == off | |
13089 | * means no live instructions after (tail of the program was removed). | |
13090 | */ | |
13091 | if (prog->len != off && l_cnt && | |
13092 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
13093 | l_cnt--; | |
13094 | linfo[--i].insn_off = off + cnt; | |
13095 | } | |
13096 | ||
13097 | /* remove the line info which refer to the removed instructions */ | |
13098 | if (l_cnt) { | |
13099 | memmove(linfo + l_off, linfo + i, | |
13100 | sizeof(*linfo) * (nr_linfo - i)); | |
13101 | ||
13102 | prog->aux->nr_linfo -= l_cnt; | |
13103 | nr_linfo = prog->aux->nr_linfo; | |
13104 | } | |
13105 | ||
13106 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
13107 | for (i = l_off; i < nr_linfo; i++) | |
13108 | linfo[i].insn_off -= cnt; | |
13109 | ||
13110 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
13111 | for (i = 0; i <= env->subprog_cnt; i++) | |
13112 | if (env->subprog_info[i].linfo_idx > l_off) { | |
13113 | /* program may have started in the removed region but | |
13114 | * may not be fully removed | |
13115 | */ | |
13116 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
13117 | env->subprog_info[i].linfo_idx -= l_cnt; | |
13118 | else | |
13119 | env->subprog_info[i].linfo_idx = l_off; | |
13120 | } | |
13121 | ||
13122 | return 0; | |
13123 | } | |
13124 | ||
13125 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
13126 | { | |
13127 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
13128 | unsigned int orig_prog_len = env->prog->len; | |
13129 | int err; | |
13130 | ||
08ca90af JK |
13131 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
13132 | bpf_prog_offload_remove_insns(env, off, cnt); | |
13133 | ||
52875a04 JK |
13134 | err = bpf_remove_insns(env->prog, off, cnt); |
13135 | if (err) | |
13136 | return err; | |
13137 | ||
13138 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
13139 | if (err) | |
13140 | return err; | |
13141 | ||
13142 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
13143 | if (err) | |
13144 | return err; | |
13145 | ||
13146 | memmove(aux_data + off, aux_data + off + cnt, | |
13147 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
13148 | ||
13149 | return 0; | |
13150 | } | |
13151 | ||
2a5418a1 DB |
13152 | /* The verifier does more data flow analysis than llvm and will not |
13153 | * explore branches that are dead at run time. Malicious programs can | |
13154 | * have dead code too. Therefore replace all dead at-run-time code | |
13155 | * with 'ja -1'. | |
13156 | * | |
13157 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
13158 | * program and through another bug we would manage to jump there, then | |
13159 | * we'd execute beyond program memory otherwise. Returning exception | |
13160 | * code also wouldn't work since we can have subprogs where the dead | |
13161 | * code could be located. | |
c131187d AS |
13162 | */ |
13163 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
13164 | { | |
13165 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 13166 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
13167 | struct bpf_insn *insn = env->prog->insnsi; |
13168 | const int insn_cnt = env->prog->len; | |
13169 | int i; | |
13170 | ||
13171 | for (i = 0; i < insn_cnt; i++) { | |
13172 | if (aux_data[i].seen) | |
13173 | continue; | |
2a5418a1 | 13174 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 13175 | aux_data[i].zext_dst = false; |
c131187d AS |
13176 | } |
13177 | } | |
13178 | ||
e2ae4ca2 JK |
13179 | static bool insn_is_cond_jump(u8 code) |
13180 | { | |
13181 | u8 op; | |
13182 | ||
092ed096 JW |
13183 | if (BPF_CLASS(code) == BPF_JMP32) |
13184 | return true; | |
13185 | ||
e2ae4ca2 JK |
13186 | if (BPF_CLASS(code) != BPF_JMP) |
13187 | return false; | |
13188 | ||
13189 | op = BPF_OP(code); | |
13190 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
13191 | } | |
13192 | ||
13193 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
13194 | { | |
13195 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
13196 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
13197 | struct bpf_insn *insn = env->prog->insnsi; | |
13198 | const int insn_cnt = env->prog->len; | |
13199 | int i; | |
13200 | ||
13201 | for (i = 0; i < insn_cnt; i++, insn++) { | |
13202 | if (!insn_is_cond_jump(insn->code)) | |
13203 | continue; | |
13204 | ||
13205 | if (!aux_data[i + 1].seen) | |
13206 | ja.off = insn->off; | |
13207 | else if (!aux_data[i + 1 + insn->off].seen) | |
13208 | ja.off = 0; | |
13209 | else | |
13210 | continue; | |
13211 | ||
08ca90af JK |
13212 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
13213 | bpf_prog_offload_replace_insn(env, i, &ja); | |
13214 | ||
e2ae4ca2 JK |
13215 | memcpy(insn, &ja, sizeof(ja)); |
13216 | } | |
13217 | } | |
13218 | ||
52875a04 JK |
13219 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
13220 | { | |
13221 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
13222 | int insn_cnt = env->prog->len; | |
13223 | int i, err; | |
13224 | ||
13225 | for (i = 0; i < insn_cnt; i++) { | |
13226 | int j; | |
13227 | ||
13228 | j = 0; | |
13229 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
13230 | j++; | |
13231 | if (!j) | |
13232 | continue; | |
13233 | ||
13234 | err = verifier_remove_insns(env, i, j); | |
13235 | if (err) | |
13236 | return err; | |
13237 | insn_cnt = env->prog->len; | |
13238 | } | |
13239 | ||
13240 | return 0; | |
13241 | } | |
13242 | ||
a1b14abc JK |
13243 | static int opt_remove_nops(struct bpf_verifier_env *env) |
13244 | { | |
13245 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
13246 | struct bpf_insn *insn = env->prog->insnsi; | |
13247 | int insn_cnt = env->prog->len; | |
13248 | int i, err; | |
13249 | ||
13250 | for (i = 0; i < insn_cnt; i++) { | |
13251 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
13252 | continue; | |
13253 | ||
13254 | err = verifier_remove_insns(env, i, 1); | |
13255 | if (err) | |
13256 | return err; | |
13257 | insn_cnt--; | |
13258 | i--; | |
13259 | } | |
13260 | ||
13261 | return 0; | |
13262 | } | |
13263 | ||
d6c2308c JW |
13264 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
13265 | const union bpf_attr *attr) | |
a4b1d3c1 | 13266 | { |
d6c2308c | 13267 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 13268 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 13269 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 13270 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 13271 | struct bpf_prog *new_prog; |
d6c2308c | 13272 | bool rnd_hi32; |
a4b1d3c1 | 13273 | |
d6c2308c | 13274 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 13275 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
13276 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
13277 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
13278 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
13279 | for (i = 0; i < len; i++) { |
13280 | int adj_idx = i + delta; | |
13281 | struct bpf_insn insn; | |
83a28819 | 13282 | int load_reg; |
a4b1d3c1 | 13283 | |
d6c2308c | 13284 | insn = insns[adj_idx]; |
83a28819 | 13285 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
13286 | if (!aux[adj_idx].zext_dst) { |
13287 | u8 code, class; | |
13288 | u32 imm_rnd; | |
13289 | ||
13290 | if (!rnd_hi32) | |
13291 | continue; | |
13292 | ||
13293 | code = insn.code; | |
13294 | class = BPF_CLASS(code); | |
83a28819 | 13295 | if (load_reg == -1) |
d6c2308c JW |
13296 | continue; |
13297 | ||
13298 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
13299 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
13300 | * here. | |
d6c2308c | 13301 | */ |
83a28819 | 13302 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
13303 | if (class == BPF_LD && |
13304 | BPF_MODE(code) == BPF_IMM) | |
13305 | i++; | |
13306 | continue; | |
13307 | } | |
13308 | ||
13309 | /* ctx load could be transformed into wider load. */ | |
13310 | if (class == BPF_LDX && | |
13311 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
13312 | continue; | |
13313 | ||
a251c17a | 13314 | imm_rnd = get_random_u32(); |
d6c2308c JW |
13315 | rnd_hi32_patch[0] = insn; |
13316 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 13317 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
13318 | patch = rnd_hi32_patch; |
13319 | patch_len = 4; | |
13320 | goto apply_patch_buffer; | |
13321 | } | |
13322 | ||
39491867 BJ |
13323 | /* Add in an zero-extend instruction if a) the JIT has requested |
13324 | * it or b) it's a CMPXCHG. | |
13325 | * | |
13326 | * The latter is because: BPF_CMPXCHG always loads a value into | |
13327 | * R0, therefore always zero-extends. However some archs' | |
13328 | * equivalent instruction only does this load when the | |
13329 | * comparison is successful. This detail of CMPXCHG is | |
13330 | * orthogonal to the general zero-extension behaviour of the | |
13331 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
13332 | */ | |
13333 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
13334 | continue; |
13335 | ||
83a28819 IL |
13336 | if (WARN_ON(load_reg == -1)) { |
13337 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
13338 | return -EFAULT; | |
b2e37a71 IL |
13339 | } |
13340 | ||
a4b1d3c1 | 13341 | zext_patch[0] = insn; |
b2e37a71 IL |
13342 | zext_patch[1].dst_reg = load_reg; |
13343 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
13344 | patch = zext_patch; |
13345 | patch_len = 2; | |
13346 | apply_patch_buffer: | |
13347 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
13348 | if (!new_prog) |
13349 | return -ENOMEM; | |
13350 | env->prog = new_prog; | |
13351 | insns = new_prog->insnsi; | |
13352 | aux = env->insn_aux_data; | |
d6c2308c | 13353 | delta += patch_len - 1; |
a4b1d3c1 JW |
13354 | } |
13355 | ||
13356 | return 0; | |
13357 | } | |
13358 | ||
c64b7983 JS |
13359 | /* convert load instructions that access fields of a context type into a |
13360 | * sequence of instructions that access fields of the underlying structure: | |
13361 | * struct __sk_buff -> struct sk_buff | |
13362 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 13363 | */ |
58e2af8b | 13364 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 13365 | { |
00176a34 | 13366 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 13367 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 13368 | const int insn_cnt = env->prog->len; |
36bbef52 | 13369 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 13370 | u32 target_size, size_default, off; |
9bac3d6d | 13371 | struct bpf_prog *new_prog; |
d691f9e8 | 13372 | enum bpf_access_type type; |
f96da094 | 13373 | bool is_narrower_load; |
9bac3d6d | 13374 | |
b09928b9 DB |
13375 | if (ops->gen_prologue || env->seen_direct_write) { |
13376 | if (!ops->gen_prologue) { | |
13377 | verbose(env, "bpf verifier is misconfigured\n"); | |
13378 | return -EINVAL; | |
13379 | } | |
36bbef52 DB |
13380 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
13381 | env->prog); | |
13382 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 13383 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
13384 | return -EINVAL; |
13385 | } else if (cnt) { | |
8041902d | 13386 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
13387 | if (!new_prog) |
13388 | return -ENOMEM; | |
8041902d | 13389 | |
36bbef52 | 13390 | env->prog = new_prog; |
3df126f3 | 13391 | delta += cnt - 1; |
36bbef52 DB |
13392 | } |
13393 | } | |
13394 | ||
c64b7983 | 13395 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
13396 | return 0; |
13397 | ||
3df126f3 | 13398 | insn = env->prog->insnsi + delta; |
36bbef52 | 13399 | |
9bac3d6d | 13400 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 | 13401 | bpf_convert_ctx_access_t convert_ctx_access; |
2039f26f | 13402 | bool ctx_access; |
c64b7983 | 13403 | |
62c7989b DB |
13404 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
13405 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
13406 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 13407 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 13408 | type = BPF_READ; |
2039f26f DB |
13409 | ctx_access = true; |
13410 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || | |
13411 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
13412 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
13413 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
13414 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
13415 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
13416 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
13417 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 13418 | type = BPF_WRITE; |
2039f26f DB |
13419 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
13420 | } else { | |
9bac3d6d | 13421 | continue; |
2039f26f | 13422 | } |
9bac3d6d | 13423 | |
af86ca4e | 13424 | if (type == BPF_WRITE && |
2039f26f | 13425 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 13426 | struct bpf_insn patch[] = { |
af86ca4e | 13427 | *insn, |
2039f26f | 13428 | BPF_ST_NOSPEC(), |
af86ca4e AS |
13429 | }; |
13430 | ||
13431 | cnt = ARRAY_SIZE(patch); | |
13432 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
13433 | if (!new_prog) | |
13434 | return -ENOMEM; | |
13435 | ||
13436 | delta += cnt - 1; | |
13437 | env->prog = new_prog; | |
13438 | insn = new_prog->insnsi + i + delta; | |
13439 | continue; | |
13440 | } | |
13441 | ||
2039f26f DB |
13442 | if (!ctx_access) |
13443 | continue; | |
13444 | ||
6efe152d | 13445 | switch ((int)env->insn_aux_data[i + delta].ptr_type) { |
c64b7983 JS |
13446 | case PTR_TO_CTX: |
13447 | if (!ops->convert_ctx_access) | |
13448 | continue; | |
13449 | convert_ctx_access = ops->convert_ctx_access; | |
13450 | break; | |
13451 | case PTR_TO_SOCKET: | |
46f8bc92 | 13452 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
13453 | convert_ctx_access = bpf_sock_convert_ctx_access; |
13454 | break; | |
655a51e5 MKL |
13455 | case PTR_TO_TCP_SOCK: |
13456 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
13457 | break; | |
fada7fdc JL |
13458 | case PTR_TO_XDP_SOCK: |
13459 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
13460 | break; | |
2a02759e | 13461 | case PTR_TO_BTF_ID: |
6efe152d | 13462 | case PTR_TO_BTF_ID | PTR_UNTRUSTED: |
27ae7997 MKL |
13463 | if (type == BPF_READ) { |
13464 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
13465 | BPF_SIZE((insn)->code); | |
13466 | env->prog->aux->num_exentries++; | |
2a02759e | 13467 | } |
2a02759e | 13468 | continue; |
c64b7983 | 13469 | default: |
9bac3d6d | 13470 | continue; |
c64b7983 | 13471 | } |
9bac3d6d | 13472 | |
31fd8581 | 13473 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 13474 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
13475 | |
13476 | /* If the read access is a narrower load of the field, | |
13477 | * convert to a 4/8-byte load, to minimum program type specific | |
13478 | * convert_ctx_access changes. If conversion is successful, | |
13479 | * we will apply proper mask to the result. | |
13480 | */ | |
f96da094 | 13481 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
13482 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
13483 | off = insn->off; | |
31fd8581 | 13484 | if (is_narrower_load) { |
f96da094 DB |
13485 | u8 size_code; |
13486 | ||
13487 | if (type == BPF_WRITE) { | |
61bd5218 | 13488 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
13489 | return -EINVAL; |
13490 | } | |
31fd8581 | 13491 | |
f96da094 | 13492 | size_code = BPF_H; |
31fd8581 YS |
13493 | if (ctx_field_size == 4) |
13494 | size_code = BPF_W; | |
13495 | else if (ctx_field_size == 8) | |
13496 | size_code = BPF_DW; | |
f96da094 | 13497 | |
bc23105c | 13498 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
13499 | insn->code = BPF_LDX | BPF_MEM | size_code; |
13500 | } | |
f96da094 DB |
13501 | |
13502 | target_size = 0; | |
c64b7983 JS |
13503 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
13504 | &target_size); | |
f96da094 DB |
13505 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
13506 | (ctx_field_size && !target_size)) { | |
61bd5218 | 13507 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
13508 | return -EINVAL; |
13509 | } | |
f96da094 DB |
13510 | |
13511 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
13512 | u8 shift = bpf_ctx_narrow_access_offset( |
13513 | off, size, size_default) * 8; | |
d7af7e49 AI |
13514 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
13515 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
13516 | return -EINVAL; | |
13517 | } | |
46f53a65 AI |
13518 | if (ctx_field_size <= 4) { |
13519 | if (shift) | |
13520 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
13521 | insn->dst_reg, | |
13522 | shift); | |
31fd8581 | 13523 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 13524 | (1 << size * 8) - 1); |
46f53a65 AI |
13525 | } else { |
13526 | if (shift) | |
13527 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
13528 | insn->dst_reg, | |
13529 | shift); | |
31fd8581 | 13530 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 13531 | (1ULL << size * 8) - 1); |
46f53a65 | 13532 | } |
31fd8581 | 13533 | } |
9bac3d6d | 13534 | |
8041902d | 13535 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
13536 | if (!new_prog) |
13537 | return -ENOMEM; | |
13538 | ||
3df126f3 | 13539 | delta += cnt - 1; |
9bac3d6d AS |
13540 | |
13541 | /* keep walking new program and skip insns we just inserted */ | |
13542 | env->prog = new_prog; | |
3df126f3 | 13543 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
13544 | } |
13545 | ||
13546 | return 0; | |
13547 | } | |
13548 | ||
1c2a088a AS |
13549 | static int jit_subprogs(struct bpf_verifier_env *env) |
13550 | { | |
13551 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
13552 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 13553 | struct bpf_map *map_ptr; |
7105e828 | 13554 | struct bpf_insn *insn; |
1c2a088a | 13555 | void *old_bpf_func; |
c4c0bdc0 | 13556 | int err, num_exentries; |
1c2a088a | 13557 | |
f910cefa | 13558 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
13559 | return 0; |
13560 | ||
7105e828 | 13561 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 13562 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 13563 | continue; |
69c087ba | 13564 | |
c7a89784 DB |
13565 | /* Upon error here we cannot fall back to interpreter but |
13566 | * need a hard reject of the program. Thus -EFAULT is | |
13567 | * propagated in any case. | |
13568 | */ | |
1c2a088a AS |
13569 | subprog = find_subprog(env, i + insn->imm + 1); |
13570 | if (subprog < 0) { | |
13571 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
13572 | i + insn->imm + 1); | |
13573 | return -EFAULT; | |
13574 | } | |
13575 | /* temporarily remember subprog id inside insn instead of | |
13576 | * aux_data, since next loop will split up all insns into funcs | |
13577 | */ | |
f910cefa | 13578 | insn->off = subprog; |
1c2a088a AS |
13579 | /* remember original imm in case JIT fails and fallback |
13580 | * to interpreter will be needed | |
13581 | */ | |
13582 | env->insn_aux_data[i].call_imm = insn->imm; | |
13583 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
13584 | insn->imm = 1; | |
3990ed4c MKL |
13585 | if (bpf_pseudo_func(insn)) |
13586 | /* jit (e.g. x86_64) may emit fewer instructions | |
13587 | * if it learns a u32 imm is the same as a u64 imm. | |
13588 | * Force a non zero here. | |
13589 | */ | |
13590 | insn[1].imm = 1; | |
1c2a088a AS |
13591 | } |
13592 | ||
c454a46b MKL |
13593 | err = bpf_prog_alloc_jited_linfo(prog); |
13594 | if (err) | |
13595 | goto out_undo_insn; | |
13596 | ||
13597 | err = -ENOMEM; | |
6396bb22 | 13598 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 13599 | if (!func) |
c7a89784 | 13600 | goto out_undo_insn; |
1c2a088a | 13601 | |
f910cefa | 13602 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 13603 | subprog_start = subprog_end; |
4cb3d99c | 13604 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
13605 | |
13606 | len = subprog_end - subprog_start; | |
fb7dd8bc | 13607 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
13608 | * hence main prog stats include the runtime of subprogs. |
13609 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 13610 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
13611 | */ |
13612 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
13613 | if (!func[i]) |
13614 | goto out_free; | |
13615 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
13616 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 13617 | func[i]->type = prog->type; |
1c2a088a | 13618 | func[i]->len = len; |
4f74d809 DB |
13619 | if (bpf_prog_calc_tag(func[i])) |
13620 | goto out_free; | |
1c2a088a | 13621 | func[i]->is_func = 1; |
ba64e7d8 | 13622 | func[i]->aux->func_idx = i; |
f263a814 | 13623 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
13624 | func[i]->aux->btf = prog->aux->btf; |
13625 | func[i]->aux->func_info = prog->aux->func_info; | |
9c7c48d6 | 13626 | func[i]->aux->func_info_cnt = prog->aux->func_info_cnt; |
f263a814 JF |
13627 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
13628 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 13629 | |
a748c697 | 13630 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 13631 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 13632 | |
f263a814 JF |
13633 | poke = &prog->aux->poke_tab[j]; |
13634 | if (poke->insn_idx < subprog_end && | |
13635 | poke->insn_idx >= subprog_start) | |
13636 | poke->aux = func[i]->aux; | |
a748c697 MF |
13637 | } |
13638 | ||
1c2a088a | 13639 | func[i]->aux->name[0] = 'F'; |
9c8105bd | 13640 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 13641 | func[i]->jit_requested = 1; |
d2a3b7c5 | 13642 | func[i]->blinding_requested = prog->blinding_requested; |
e6ac2450 | 13643 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 13644 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
13645 | func[i]->aux->linfo = prog->aux->linfo; |
13646 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
13647 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
13648 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
13649 | num_exentries = 0; |
13650 | insn = func[i]->insnsi; | |
13651 | for (j = 0; j < func[i]->len; j++, insn++) { | |
13652 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
13653 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
13654 | num_exentries++; | |
13655 | } | |
13656 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 13657 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
13658 | func[i] = bpf_int_jit_compile(func[i]); |
13659 | if (!func[i]->jited) { | |
13660 | err = -ENOTSUPP; | |
13661 | goto out_free; | |
13662 | } | |
13663 | cond_resched(); | |
13664 | } | |
a748c697 | 13665 | |
1c2a088a AS |
13666 | /* at this point all bpf functions were successfully JITed |
13667 | * now populate all bpf_calls with correct addresses and | |
13668 | * run last pass of JIT | |
13669 | */ | |
f910cefa | 13670 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
13671 | insn = func[i]->insnsi; |
13672 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 13673 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 13674 | subprog = insn->off; |
69c087ba YS |
13675 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
13676 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
13677 | continue; | |
13678 | } | |
23a2d70c | 13679 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
13680 | continue; |
13681 | subprog = insn->off; | |
3d717fad | 13682 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 13683 | } |
2162fed4 SD |
13684 | |
13685 | /* we use the aux data to keep a list of the start addresses | |
13686 | * of the JITed images for each function in the program | |
13687 | * | |
13688 | * for some architectures, such as powerpc64, the imm field | |
13689 | * might not be large enough to hold the offset of the start | |
13690 | * address of the callee's JITed image from __bpf_call_base | |
13691 | * | |
13692 | * in such cases, we can lookup the start address of a callee | |
13693 | * by using its subprog id, available from the off field of | |
13694 | * the call instruction, as an index for this list | |
13695 | */ | |
13696 | func[i]->aux->func = func; | |
13697 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 13698 | } |
f910cefa | 13699 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
13700 | old_bpf_func = func[i]->bpf_func; |
13701 | tmp = bpf_int_jit_compile(func[i]); | |
13702 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
13703 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 13704 | err = -ENOTSUPP; |
1c2a088a AS |
13705 | goto out_free; |
13706 | } | |
13707 | cond_resched(); | |
13708 | } | |
13709 | ||
13710 | /* finally lock prog and jit images for all functions and | |
13711 | * populate kallsysm | |
13712 | */ | |
f910cefa | 13713 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
13714 | bpf_prog_lock_ro(func[i]); |
13715 | bpf_prog_kallsyms_add(func[i]); | |
13716 | } | |
7105e828 DB |
13717 | |
13718 | /* Last step: make now unused interpreter insns from main | |
13719 | * prog consistent for later dump requests, so they can | |
13720 | * later look the same as if they were interpreted only. | |
13721 | */ | |
13722 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
13723 | if (bpf_pseudo_func(insn)) { |
13724 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
13725 | insn[1].imm = insn->off; |
13726 | insn->off = 0; | |
69c087ba YS |
13727 | continue; |
13728 | } | |
23a2d70c | 13729 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
13730 | continue; |
13731 | insn->off = env->insn_aux_data[i].call_imm; | |
13732 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 13733 | insn->imm = subprog; |
7105e828 DB |
13734 | } |
13735 | ||
1c2a088a AS |
13736 | prog->jited = 1; |
13737 | prog->bpf_func = func[0]->bpf_func; | |
d00c6473 | 13738 | prog->jited_len = func[0]->jited_len; |
1c2a088a | 13739 | prog->aux->func = func; |
f910cefa | 13740 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 13741 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
13742 | return 0; |
13743 | out_free: | |
f263a814 JF |
13744 | /* We failed JIT'ing, so at this point we need to unregister poke |
13745 | * descriptors from subprogs, so that kernel is not attempting to | |
13746 | * patch it anymore as we're freeing the subprog JIT memory. | |
13747 | */ | |
13748 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
13749 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
13750 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
13751 | } | |
13752 | /* At this point we're guaranteed that poke descriptors are not | |
13753 | * live anymore. We can just unlink its descriptor table as it's | |
13754 | * released with the main prog. | |
13755 | */ | |
a748c697 MF |
13756 | for (i = 0; i < env->subprog_cnt; i++) { |
13757 | if (!func[i]) | |
13758 | continue; | |
f263a814 | 13759 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
13760 | bpf_jit_free(func[i]); |
13761 | } | |
1c2a088a | 13762 | kfree(func); |
c7a89784 | 13763 | out_undo_insn: |
1c2a088a AS |
13764 | /* cleanup main prog to be interpreted */ |
13765 | prog->jit_requested = 0; | |
d2a3b7c5 | 13766 | prog->blinding_requested = 0; |
1c2a088a | 13767 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
23a2d70c | 13768 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
13769 | continue; |
13770 | insn->off = 0; | |
13771 | insn->imm = env->insn_aux_data[i].call_imm; | |
13772 | } | |
e16301fb | 13773 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
13774 | return err; |
13775 | } | |
13776 | ||
1ea47e01 AS |
13777 | static int fixup_call_args(struct bpf_verifier_env *env) |
13778 | { | |
19d28fbd | 13779 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
13780 | struct bpf_prog *prog = env->prog; |
13781 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 13782 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 13783 | int i, depth; |
19d28fbd | 13784 | #endif |
e4052d06 | 13785 | int err = 0; |
1ea47e01 | 13786 | |
e4052d06 QM |
13787 | if (env->prog->jit_requested && |
13788 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
13789 | err = jit_subprogs(env); |
13790 | if (err == 0) | |
1c2a088a | 13791 | return 0; |
c7a89784 DB |
13792 | if (err == -EFAULT) |
13793 | return err; | |
19d28fbd DM |
13794 | } |
13795 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
13796 | if (has_kfunc_call) { |
13797 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
13798 | return -EINVAL; | |
13799 | } | |
e411901c MF |
13800 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
13801 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
13802 | * have to be rejected, since interpreter doesn't support them yet. | |
13803 | */ | |
13804 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
13805 | return -EINVAL; | |
13806 | } | |
1ea47e01 | 13807 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
13808 | if (bpf_pseudo_func(insn)) { |
13809 | /* When JIT fails the progs with callback calls | |
13810 | * have to be rejected, since interpreter doesn't support them yet. | |
13811 | */ | |
13812 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
13813 | return -EINVAL; | |
13814 | } | |
13815 | ||
23a2d70c | 13816 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
13817 | continue; |
13818 | depth = get_callee_stack_depth(env, insn, i); | |
13819 | if (depth < 0) | |
13820 | return depth; | |
13821 | bpf_patch_call_args(insn, depth); | |
13822 | } | |
19d28fbd DM |
13823 | err = 0; |
13824 | #endif | |
13825 | return err; | |
1ea47e01 AS |
13826 | } |
13827 | ||
e6ac2450 MKL |
13828 | static int fixup_kfunc_call(struct bpf_verifier_env *env, |
13829 | struct bpf_insn *insn) | |
13830 | { | |
13831 | const struct bpf_kfunc_desc *desc; | |
13832 | ||
a5d82727 KKD |
13833 | if (!insn->imm) { |
13834 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
13835 | return -EINVAL; | |
13836 | } | |
13837 | ||
e6ac2450 MKL |
13838 | /* insn->imm has the btf func_id. Replace it with |
13839 | * an address (relative to __bpf_base_call). | |
13840 | */ | |
2357672c | 13841 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
13842 | if (!desc) { |
13843 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
13844 | insn->imm); | |
13845 | return -EFAULT; | |
13846 | } | |
13847 | ||
13848 | insn->imm = desc->imm; | |
13849 | ||
13850 | return 0; | |
13851 | } | |
13852 | ||
e6ac5933 BJ |
13853 | /* Do various post-verification rewrites in a single program pass. |
13854 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 13855 | */ |
e6ac5933 | 13856 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 13857 | { |
79741b3b | 13858 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 13859 | enum bpf_attach_type eatype = prog->expected_attach_type; |
9b99edca | 13860 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 13861 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 13862 | const struct bpf_func_proto *fn; |
79741b3b | 13863 | const int insn_cnt = prog->len; |
09772d92 | 13864 | const struct bpf_map_ops *ops; |
c93552c4 | 13865 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
13866 | struct bpf_insn insn_buf[16]; |
13867 | struct bpf_prog *new_prog; | |
13868 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 13869 | int i, ret, cnt, delta = 0; |
e245c5c6 | 13870 | |
79741b3b | 13871 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 13872 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
13873 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
13874 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
13875 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 13876 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 13877 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
13878 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
13879 | struct bpf_insn *patchlet; | |
13880 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 13881 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
13882 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
13883 | BPF_JNE | BPF_K, insn->src_reg, | |
13884 | 0, 2, 0), | |
f6b1b3bf DB |
13885 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
13886 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
13887 | *insn, | |
13888 | }; | |
e88b2c6e | 13889 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 13890 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
13891 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
13892 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 13893 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 13894 | *insn, |
9b00f1b7 DB |
13895 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
13896 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 13897 | }; |
f6b1b3bf | 13898 | |
e88b2c6e DB |
13899 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
13900 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 13901 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
13902 | |
13903 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
13904 | if (!new_prog) |
13905 | return -ENOMEM; | |
13906 | ||
13907 | delta += cnt - 1; | |
13908 | env->prog = prog = new_prog; | |
13909 | insn = new_prog->insnsi + i + delta; | |
13910 | continue; | |
13911 | } | |
13912 | ||
e6ac5933 | 13913 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
13914 | if (BPF_CLASS(insn->code) == BPF_LD && |
13915 | (BPF_MODE(insn->code) == BPF_ABS || | |
13916 | BPF_MODE(insn->code) == BPF_IND)) { | |
13917 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
13918 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
13919 | verbose(env, "bpf verifier is misconfigured\n"); | |
13920 | return -EINVAL; | |
13921 | } | |
13922 | ||
13923 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13924 | if (!new_prog) | |
13925 | return -ENOMEM; | |
13926 | ||
13927 | delta += cnt - 1; | |
13928 | env->prog = prog = new_prog; | |
13929 | insn = new_prog->insnsi + i + delta; | |
13930 | continue; | |
13931 | } | |
13932 | ||
e6ac5933 | 13933 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
13934 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
13935 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
13936 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
13937 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 13938 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 13939 | bool issrc, isneg, isimm; |
979d63d5 DB |
13940 | u32 off_reg; |
13941 | ||
13942 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
13943 | if (!aux->alu_state || |
13944 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
13945 | continue; |
13946 | ||
13947 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
13948 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
13949 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 13950 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
13951 | |
13952 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
13953 | if (isimm) { |
13954 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
13955 | } else { | |
13956 | if (isneg) | |
13957 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
13958 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
13959 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
13960 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
13961 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
13962 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
13963 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
13964 | } | |
b9b34ddb DB |
13965 | if (!issrc) |
13966 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
13967 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
13968 | if (isneg) |
13969 | insn->code = insn->code == code_add ? | |
13970 | code_sub : code_add; | |
13971 | *patch++ = *insn; | |
801c6058 | 13972 | if (issrc && isneg && !isimm) |
979d63d5 DB |
13973 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
13974 | cnt = patch - insn_buf; | |
13975 | ||
13976 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13977 | if (!new_prog) | |
13978 | return -ENOMEM; | |
13979 | ||
13980 | delta += cnt - 1; | |
13981 | env->prog = prog = new_prog; | |
13982 | insn = new_prog->insnsi + i + delta; | |
13983 | continue; | |
13984 | } | |
13985 | ||
79741b3b AS |
13986 | if (insn->code != (BPF_JMP | BPF_CALL)) |
13987 | continue; | |
cc8b0b92 AS |
13988 | if (insn->src_reg == BPF_PSEUDO_CALL) |
13989 | continue; | |
e6ac2450 MKL |
13990 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
13991 | ret = fixup_kfunc_call(env, insn); | |
13992 | if (ret) | |
13993 | return ret; | |
13994 | continue; | |
13995 | } | |
e245c5c6 | 13996 | |
79741b3b AS |
13997 | if (insn->imm == BPF_FUNC_get_route_realm) |
13998 | prog->dst_needed = 1; | |
13999 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
14000 | bpf_user_rnd_init_once(); | |
9802d865 JB |
14001 | if (insn->imm == BPF_FUNC_override_return) |
14002 | prog->kprobe_override = 1; | |
79741b3b | 14003 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
14004 | /* If we tail call into other programs, we |
14005 | * cannot make any assumptions since they can | |
14006 | * be replaced dynamically during runtime in | |
14007 | * the program array. | |
14008 | */ | |
14009 | prog->cb_access = 1; | |
e411901c MF |
14010 | if (!allow_tail_call_in_subprogs(env)) |
14011 | prog->aux->stack_depth = MAX_BPF_STACK; | |
14012 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 14013 | |
79741b3b | 14014 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 14015 | * conditional branch in the interpreter for every normal |
79741b3b AS |
14016 | * call and to prevent accidental JITing by JIT compiler |
14017 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 14018 | */ |
79741b3b | 14019 | insn->imm = 0; |
71189fa9 | 14020 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 14021 | |
c93552c4 | 14022 | aux = &env->insn_aux_data[i + delta]; |
d2a3b7c5 | 14023 | if (env->bpf_capable && !prog->blinding_requested && |
cc52d914 | 14024 | prog->jit_requested && |
d2e4c1e6 DB |
14025 | !bpf_map_key_poisoned(aux) && |
14026 | !bpf_map_ptr_poisoned(aux) && | |
14027 | !bpf_map_ptr_unpriv(aux)) { | |
14028 | struct bpf_jit_poke_descriptor desc = { | |
14029 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
14030 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
14031 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 14032 | .insn_idx = i + delta, |
d2e4c1e6 DB |
14033 | }; |
14034 | ||
14035 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
14036 | if (ret < 0) { | |
14037 | verbose(env, "adding tail call poke descriptor failed\n"); | |
14038 | return ret; | |
14039 | } | |
14040 | ||
14041 | insn->imm = ret + 1; | |
14042 | continue; | |
14043 | } | |
14044 | ||
c93552c4 DB |
14045 | if (!bpf_map_ptr_unpriv(aux)) |
14046 | continue; | |
14047 | ||
b2157399 AS |
14048 | /* instead of changing every JIT dealing with tail_call |
14049 | * emit two extra insns: | |
14050 | * if (index >= max_entries) goto out; | |
14051 | * index &= array->index_mask; | |
14052 | * to avoid out-of-bounds cpu speculation | |
14053 | */ | |
c93552c4 | 14054 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 14055 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
14056 | return -EINVAL; |
14057 | } | |
c93552c4 | 14058 | |
d2e4c1e6 | 14059 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
14060 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
14061 | map_ptr->max_entries, 2); | |
14062 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
14063 | container_of(map_ptr, | |
14064 | struct bpf_array, | |
14065 | map)->index_mask); | |
14066 | insn_buf[2] = *insn; | |
14067 | cnt = 3; | |
14068 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14069 | if (!new_prog) | |
14070 | return -ENOMEM; | |
14071 | ||
14072 | delta += cnt - 1; | |
14073 | env->prog = prog = new_prog; | |
14074 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
14075 | continue; |
14076 | } | |
e245c5c6 | 14077 | |
b00628b1 AS |
14078 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
14079 | /* The verifier will process callback_fn as many times as necessary | |
14080 | * with different maps and the register states prepared by | |
14081 | * set_timer_callback_state will be accurate. | |
14082 | * | |
14083 | * The following use case is valid: | |
14084 | * map1 is shared by prog1, prog2, prog3. | |
14085 | * prog1 calls bpf_timer_init for some map1 elements | |
14086 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
14087 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
14088 | * prog3 calls bpf_timer_start for some map1 elements. | |
14089 | * Those that were not both bpf_timer_init-ed and | |
14090 | * bpf_timer_set_callback-ed will return -EINVAL. | |
14091 | */ | |
14092 | struct bpf_insn ld_addrs[2] = { | |
14093 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
14094 | }; | |
14095 | ||
14096 | insn_buf[0] = ld_addrs[0]; | |
14097 | insn_buf[1] = ld_addrs[1]; | |
14098 | insn_buf[2] = *insn; | |
14099 | cnt = 3; | |
14100 | ||
14101 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14102 | if (!new_prog) | |
14103 | return -ENOMEM; | |
14104 | ||
14105 | delta += cnt - 1; | |
14106 | env->prog = prog = new_prog; | |
14107 | insn = new_prog->insnsi + i + delta; | |
14108 | goto patch_call_imm; | |
14109 | } | |
14110 | ||
b00fa38a JK |
14111 | if (insn->imm == BPF_FUNC_task_storage_get || |
14112 | insn->imm == BPF_FUNC_sk_storage_get || | |
c4bcfb38 YS |
14113 | insn->imm == BPF_FUNC_inode_storage_get || |
14114 | insn->imm == BPF_FUNC_cgrp_storage_get) { | |
b00fa38a | 14115 | if (env->prog->aux->sleepable) |
d56c9fe6 | 14116 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); |
b00fa38a | 14117 | else |
d56c9fe6 | 14118 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); |
b00fa38a JK |
14119 | insn_buf[1] = *insn; |
14120 | cnt = 2; | |
14121 | ||
14122 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14123 | if (!new_prog) | |
14124 | return -ENOMEM; | |
14125 | ||
14126 | delta += cnt - 1; | |
14127 | env->prog = prog = new_prog; | |
14128 | insn = new_prog->insnsi + i + delta; | |
14129 | goto patch_call_imm; | |
14130 | } | |
14131 | ||
89c63074 | 14132 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
14133 | * and other inlining handlers are currently limited to 64 bit |
14134 | * only. | |
89c63074 | 14135 | */ |
60b58afc | 14136 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
14137 | (insn->imm == BPF_FUNC_map_lookup_elem || |
14138 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
14139 | insn->imm == BPF_FUNC_map_delete_elem || |
14140 | insn->imm == BPF_FUNC_map_push_elem || | |
14141 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 14142 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c | 14143 | insn->imm == BPF_FUNC_redirect_map || |
07343110 FZ |
14144 | insn->imm == BPF_FUNC_for_each_map_elem || |
14145 | insn->imm == BPF_FUNC_map_lookup_percpu_elem)) { | |
c93552c4 DB |
14146 | aux = &env->insn_aux_data[i + delta]; |
14147 | if (bpf_map_ptr_poisoned(aux)) | |
14148 | goto patch_call_imm; | |
14149 | ||
d2e4c1e6 | 14150 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
14151 | ops = map_ptr->ops; |
14152 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
14153 | ops->map_gen_lookup) { | |
14154 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
14155 | if (cnt == -EOPNOTSUPP) |
14156 | goto patch_map_ops_generic; | |
14157 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
14158 | verbose(env, "bpf verifier is misconfigured\n"); |
14159 | return -EINVAL; | |
14160 | } | |
81ed18ab | 14161 | |
09772d92 DB |
14162 | new_prog = bpf_patch_insn_data(env, i + delta, |
14163 | insn_buf, cnt); | |
14164 | if (!new_prog) | |
14165 | return -ENOMEM; | |
81ed18ab | 14166 | |
09772d92 DB |
14167 | delta += cnt - 1; |
14168 | env->prog = prog = new_prog; | |
14169 | insn = new_prog->insnsi + i + delta; | |
14170 | continue; | |
14171 | } | |
81ed18ab | 14172 | |
09772d92 DB |
14173 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
14174 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
14175 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
14176 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
14177 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
14178 | (int (*)(struct bpf_map *map, void *key, void *value, | |
14179 | u64 flags))NULL)); | |
84430d42 DB |
14180 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
14181 | (int (*)(struct bpf_map *map, void *value, | |
14182 | u64 flags))NULL)); | |
14183 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
14184 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
14185 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
14186 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
e6a4750f BT |
14187 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
14188 | (int (*)(struct bpf_map *map, u32 ifindex, u64 flags))NULL)); | |
0640c77c AI |
14189 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
14190 | (int (*)(struct bpf_map *map, | |
14191 | bpf_callback_t callback_fn, | |
14192 | void *callback_ctx, | |
14193 | u64 flags))NULL)); | |
07343110 FZ |
14194 | BUILD_BUG_ON(!__same_type(ops->map_lookup_percpu_elem, |
14195 | (void *(*)(struct bpf_map *map, void *key, u32 cpu))NULL)); | |
e6a4750f | 14196 | |
4a8f87e6 | 14197 | patch_map_ops_generic: |
09772d92 DB |
14198 | switch (insn->imm) { |
14199 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 14200 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
14201 | continue; |
14202 | case BPF_FUNC_map_update_elem: | |
3d717fad | 14203 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
14204 | continue; |
14205 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 14206 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 14207 | continue; |
84430d42 | 14208 | case BPF_FUNC_map_push_elem: |
3d717fad | 14209 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
14210 | continue; |
14211 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 14212 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
14213 | continue; |
14214 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 14215 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 14216 | continue; |
e6a4750f | 14217 | case BPF_FUNC_redirect_map: |
3d717fad | 14218 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 14219 | continue; |
0640c77c AI |
14220 | case BPF_FUNC_for_each_map_elem: |
14221 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 14222 | continue; |
07343110 FZ |
14223 | case BPF_FUNC_map_lookup_percpu_elem: |
14224 | insn->imm = BPF_CALL_IMM(ops->map_lookup_percpu_elem); | |
14225 | continue; | |
09772d92 | 14226 | } |
81ed18ab | 14227 | |
09772d92 | 14228 | goto patch_call_imm; |
81ed18ab AS |
14229 | } |
14230 | ||
e6ac5933 | 14231 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
14232 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
14233 | insn->imm == BPF_FUNC_jiffies64) { | |
14234 | struct bpf_insn ld_jiffies_addr[2] = { | |
14235 | BPF_LD_IMM64(BPF_REG_0, | |
14236 | (unsigned long)&jiffies), | |
14237 | }; | |
14238 | ||
14239 | insn_buf[0] = ld_jiffies_addr[0]; | |
14240 | insn_buf[1] = ld_jiffies_addr[1]; | |
14241 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
14242 | BPF_REG_0, 0); | |
14243 | cnt = 3; | |
14244 | ||
14245 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
14246 | cnt); | |
14247 | if (!new_prog) | |
14248 | return -ENOMEM; | |
14249 | ||
14250 | delta += cnt - 1; | |
14251 | env->prog = prog = new_prog; | |
14252 | insn = new_prog->insnsi + i + delta; | |
14253 | continue; | |
14254 | } | |
14255 | ||
f92c1e18 JO |
14256 | /* Implement bpf_get_func_arg inline. */ |
14257 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
14258 | insn->imm == BPF_FUNC_get_func_arg) { | |
14259 | /* Load nr_args from ctx - 8 */ | |
14260 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
14261 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
14262 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
14263 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
14264 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
14265 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
14266 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
14267 | insn_buf[7] = BPF_JMP_A(1); | |
14268 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
14269 | cnt = 9; | |
14270 | ||
14271 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14272 | if (!new_prog) | |
14273 | return -ENOMEM; | |
14274 | ||
14275 | delta += cnt - 1; | |
14276 | env->prog = prog = new_prog; | |
14277 | insn = new_prog->insnsi + i + delta; | |
14278 | continue; | |
14279 | } | |
14280 | ||
14281 | /* Implement bpf_get_func_ret inline. */ | |
14282 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
14283 | insn->imm == BPF_FUNC_get_func_ret) { | |
14284 | if (eatype == BPF_TRACE_FEXIT || | |
14285 | eatype == BPF_MODIFY_RETURN) { | |
14286 | /* Load nr_args from ctx - 8 */ | |
14287 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
14288 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
14289 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
14290 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
14291 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
14292 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
14293 | cnt = 6; | |
14294 | } else { | |
14295 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
14296 | cnt = 1; | |
14297 | } | |
14298 | ||
14299 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14300 | if (!new_prog) | |
14301 | return -ENOMEM; | |
14302 | ||
14303 | delta += cnt - 1; | |
14304 | env->prog = prog = new_prog; | |
14305 | insn = new_prog->insnsi + i + delta; | |
14306 | continue; | |
14307 | } | |
14308 | ||
14309 | /* Implement get_func_arg_cnt inline. */ | |
14310 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
14311 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
14312 | /* Load nr_args from ctx - 8 */ | |
14313 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
14314 | ||
14315 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
14316 | if (!new_prog) | |
14317 | return -ENOMEM; | |
14318 | ||
14319 | env->prog = prog = new_prog; | |
14320 | insn = new_prog->insnsi + i + delta; | |
14321 | continue; | |
14322 | } | |
14323 | ||
f705ec76 | 14324 | /* Implement bpf_get_func_ip inline. */ |
9b99edca JO |
14325 | if (prog_type == BPF_PROG_TYPE_TRACING && |
14326 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
14327 | /* Load IP address from ctx - 16 */ |
14328 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
14329 | |
14330 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
14331 | if (!new_prog) | |
14332 | return -ENOMEM; | |
14333 | ||
14334 | env->prog = prog = new_prog; | |
14335 | insn = new_prog->insnsi + i + delta; | |
14336 | continue; | |
14337 | } | |
14338 | ||
81ed18ab | 14339 | patch_call_imm: |
5e43f899 | 14340 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
14341 | /* all functions that have prototype and verifier allowed |
14342 | * programs to call them, must be real in-kernel functions | |
14343 | */ | |
14344 | if (!fn->func) { | |
61bd5218 JK |
14345 | verbose(env, |
14346 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
14347 | func_id_name(insn->imm), insn->imm); |
14348 | return -EFAULT; | |
e245c5c6 | 14349 | } |
79741b3b | 14350 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 14351 | } |
e245c5c6 | 14352 | |
d2e4c1e6 DB |
14353 | /* Since poke tab is now finalized, publish aux to tracker. */ |
14354 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
14355 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
14356 | if (!map_ptr->ops->map_poke_track || | |
14357 | !map_ptr->ops->map_poke_untrack || | |
14358 | !map_ptr->ops->map_poke_run) { | |
14359 | verbose(env, "bpf verifier is misconfigured\n"); | |
14360 | return -EINVAL; | |
14361 | } | |
14362 | ||
14363 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
14364 | if (ret < 0) { | |
14365 | verbose(env, "tracking tail call prog failed\n"); | |
14366 | return ret; | |
14367 | } | |
14368 | } | |
14369 | ||
e6ac2450 MKL |
14370 | sort_kfunc_descs_by_imm(env->prog); |
14371 | ||
79741b3b AS |
14372 | return 0; |
14373 | } | |
e245c5c6 | 14374 | |
1ade2371 EZ |
14375 | static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env, |
14376 | int position, | |
14377 | s32 stack_base, | |
14378 | u32 callback_subprogno, | |
14379 | u32 *cnt) | |
14380 | { | |
14381 | s32 r6_offset = stack_base + 0 * BPF_REG_SIZE; | |
14382 | s32 r7_offset = stack_base + 1 * BPF_REG_SIZE; | |
14383 | s32 r8_offset = stack_base + 2 * BPF_REG_SIZE; | |
14384 | int reg_loop_max = BPF_REG_6; | |
14385 | int reg_loop_cnt = BPF_REG_7; | |
14386 | int reg_loop_ctx = BPF_REG_8; | |
14387 | ||
14388 | struct bpf_prog *new_prog; | |
14389 | u32 callback_start; | |
14390 | u32 call_insn_offset; | |
14391 | s32 callback_offset; | |
14392 | ||
14393 | /* This represents an inlined version of bpf_iter.c:bpf_loop, | |
14394 | * be careful to modify this code in sync. | |
14395 | */ | |
14396 | struct bpf_insn insn_buf[] = { | |
14397 | /* Return error and jump to the end of the patch if | |
14398 | * expected number of iterations is too big. | |
14399 | */ | |
14400 | BPF_JMP_IMM(BPF_JLE, BPF_REG_1, BPF_MAX_LOOPS, 2), | |
14401 | BPF_MOV32_IMM(BPF_REG_0, -E2BIG), | |
14402 | BPF_JMP_IMM(BPF_JA, 0, 0, 16), | |
14403 | /* spill R6, R7, R8 to use these as loop vars */ | |
14404 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, r6_offset), | |
14405 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_7, r7_offset), | |
14406 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_8, r8_offset), | |
14407 | /* initialize loop vars */ | |
14408 | BPF_MOV64_REG(reg_loop_max, BPF_REG_1), | |
14409 | BPF_MOV32_IMM(reg_loop_cnt, 0), | |
14410 | BPF_MOV64_REG(reg_loop_ctx, BPF_REG_3), | |
14411 | /* loop header, | |
14412 | * if reg_loop_cnt >= reg_loop_max skip the loop body | |
14413 | */ | |
14414 | BPF_JMP_REG(BPF_JGE, reg_loop_cnt, reg_loop_max, 5), | |
14415 | /* callback call, | |
14416 | * correct callback offset would be set after patching | |
14417 | */ | |
14418 | BPF_MOV64_REG(BPF_REG_1, reg_loop_cnt), | |
14419 | BPF_MOV64_REG(BPF_REG_2, reg_loop_ctx), | |
14420 | BPF_CALL_REL(0), | |
14421 | /* increment loop counter */ | |
14422 | BPF_ALU64_IMM(BPF_ADD, reg_loop_cnt, 1), | |
14423 | /* jump to loop header if callback returned 0 */ | |
14424 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -6), | |
14425 | /* return value of bpf_loop, | |
14426 | * set R0 to the number of iterations | |
14427 | */ | |
14428 | BPF_MOV64_REG(BPF_REG_0, reg_loop_cnt), | |
14429 | /* restore original values of R6, R7, R8 */ | |
14430 | BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, r6_offset), | |
14431 | BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, r7_offset), | |
14432 | BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_10, r8_offset), | |
14433 | }; | |
14434 | ||
14435 | *cnt = ARRAY_SIZE(insn_buf); | |
14436 | new_prog = bpf_patch_insn_data(env, position, insn_buf, *cnt); | |
14437 | if (!new_prog) | |
14438 | return new_prog; | |
14439 | ||
14440 | /* callback start is known only after patching */ | |
14441 | callback_start = env->subprog_info[callback_subprogno].start; | |
14442 | /* Note: insn_buf[12] is an offset of BPF_CALL_REL instruction */ | |
14443 | call_insn_offset = position + 12; | |
14444 | callback_offset = callback_start - call_insn_offset - 1; | |
fb4e3b33 | 14445 | new_prog->insnsi[call_insn_offset].imm = callback_offset; |
1ade2371 EZ |
14446 | |
14447 | return new_prog; | |
14448 | } | |
14449 | ||
14450 | static bool is_bpf_loop_call(struct bpf_insn *insn) | |
14451 | { | |
14452 | return insn->code == (BPF_JMP | BPF_CALL) && | |
14453 | insn->src_reg == 0 && | |
14454 | insn->imm == BPF_FUNC_loop; | |
14455 | } | |
14456 | ||
14457 | /* For all sub-programs in the program (including main) check | |
14458 | * insn_aux_data to see if there are bpf_loop calls that require | |
14459 | * inlining. If such calls are found the calls are replaced with a | |
14460 | * sequence of instructions produced by `inline_bpf_loop` function and | |
14461 | * subprog stack_depth is increased by the size of 3 registers. | |
14462 | * This stack space is used to spill values of the R6, R7, R8. These | |
14463 | * registers are used to store the loop bound, counter and context | |
14464 | * variables. | |
14465 | */ | |
14466 | static int optimize_bpf_loop(struct bpf_verifier_env *env) | |
14467 | { | |
14468 | struct bpf_subprog_info *subprogs = env->subprog_info; | |
14469 | int i, cur_subprog = 0, cnt, delta = 0; | |
14470 | struct bpf_insn *insn = env->prog->insnsi; | |
14471 | int insn_cnt = env->prog->len; | |
14472 | u16 stack_depth = subprogs[cur_subprog].stack_depth; | |
14473 | u16 stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
14474 | u16 stack_depth_extra = 0; | |
14475 | ||
14476 | for (i = 0; i < insn_cnt; i++, insn++) { | |
14477 | struct bpf_loop_inline_state *inline_state = | |
14478 | &env->insn_aux_data[i + delta].loop_inline_state; | |
14479 | ||
14480 | if (is_bpf_loop_call(insn) && inline_state->fit_for_inline) { | |
14481 | struct bpf_prog *new_prog; | |
14482 | ||
14483 | stack_depth_extra = BPF_REG_SIZE * 3 + stack_depth_roundup; | |
14484 | new_prog = inline_bpf_loop(env, | |
14485 | i + delta, | |
14486 | -(stack_depth + stack_depth_extra), | |
14487 | inline_state->callback_subprogno, | |
14488 | &cnt); | |
14489 | if (!new_prog) | |
14490 | return -ENOMEM; | |
14491 | ||
14492 | delta += cnt - 1; | |
14493 | env->prog = new_prog; | |
14494 | insn = new_prog->insnsi + i + delta; | |
14495 | } | |
14496 | ||
14497 | if (subprogs[cur_subprog + 1].start == i + delta + 1) { | |
14498 | subprogs[cur_subprog].stack_depth += stack_depth_extra; | |
14499 | cur_subprog++; | |
14500 | stack_depth = subprogs[cur_subprog].stack_depth; | |
14501 | stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
14502 | stack_depth_extra = 0; | |
14503 | } | |
14504 | } | |
14505 | ||
14506 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
14507 | ||
14508 | return 0; | |
14509 | } | |
14510 | ||
58e2af8b | 14511 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 14512 | { |
58e2af8b | 14513 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
14514 | int i; |
14515 | ||
9f4686c4 AS |
14516 | sl = env->free_list; |
14517 | while (sl) { | |
14518 | sln = sl->next; | |
14519 | free_verifier_state(&sl->state, false); | |
14520 | kfree(sl); | |
14521 | sl = sln; | |
14522 | } | |
51c39bb1 | 14523 | env->free_list = NULL; |
9f4686c4 | 14524 | |
f1bca824 AS |
14525 | if (!env->explored_states) |
14526 | return; | |
14527 | ||
dc2a4ebc | 14528 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
14529 | sl = env->explored_states[i]; |
14530 | ||
a8f500af AS |
14531 | while (sl) { |
14532 | sln = sl->next; | |
14533 | free_verifier_state(&sl->state, false); | |
14534 | kfree(sl); | |
14535 | sl = sln; | |
14536 | } | |
51c39bb1 | 14537 | env->explored_states[i] = NULL; |
f1bca824 | 14538 | } |
51c39bb1 | 14539 | } |
f1bca824 | 14540 | |
51c39bb1 AS |
14541 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
14542 | { | |
6f8a57cc | 14543 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
14544 | struct bpf_verifier_state *state; |
14545 | struct bpf_reg_state *regs; | |
14546 | int ret, i; | |
14547 | ||
14548 | env->prev_linfo = NULL; | |
14549 | env->pass_cnt++; | |
14550 | ||
14551 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
14552 | if (!state) | |
14553 | return -ENOMEM; | |
14554 | state->curframe = 0; | |
14555 | state->speculative = false; | |
14556 | state->branches = 1; | |
14557 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
14558 | if (!state->frame[0]) { | |
14559 | kfree(state); | |
14560 | return -ENOMEM; | |
14561 | } | |
14562 | env->cur_state = state; | |
14563 | init_func_state(env, state->frame[0], | |
14564 | BPF_MAIN_FUNC /* callsite */, | |
14565 | 0 /* frameno */, | |
14566 | subprog); | |
14567 | ||
14568 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 14569 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
14570 | ret = btf_prepare_func_args(env, subprog, regs); |
14571 | if (ret) | |
14572 | goto out; | |
14573 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
14574 | if (regs[i].type == PTR_TO_CTX) | |
14575 | mark_reg_known_zero(env, regs, i); | |
14576 | else if (regs[i].type == SCALAR_VALUE) | |
14577 | mark_reg_unknown(env, regs, i); | |
cf9f2f8d | 14578 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
e5069b9c DB |
14579 | const u32 mem_size = regs[i].mem_size; |
14580 | ||
14581 | mark_reg_known_zero(env, regs, i); | |
14582 | regs[i].mem_size = mem_size; | |
14583 | regs[i].id = ++env->id_gen; | |
14584 | } | |
51c39bb1 AS |
14585 | } |
14586 | } else { | |
14587 | /* 1st arg to a function */ | |
14588 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
14589 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 14590 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
14591 | if (ret == -EFAULT) |
14592 | /* unlikely verifier bug. abort. | |
14593 | * ret == 0 and ret < 0 are sadly acceptable for | |
14594 | * main() function due to backward compatibility. | |
14595 | * Like socket filter program may be written as: | |
14596 | * int bpf_prog(struct pt_regs *ctx) | |
14597 | * and never dereference that ctx in the program. | |
14598 | * 'struct pt_regs' is a type mismatch for socket | |
14599 | * filter that should be using 'struct __sk_buff'. | |
14600 | */ | |
14601 | goto out; | |
14602 | } | |
14603 | ||
14604 | ret = do_check(env); | |
14605 | out: | |
f59bbfc2 AS |
14606 | /* check for NULL is necessary, since cur_state can be freed inside |
14607 | * do_check() under memory pressure. | |
14608 | */ | |
14609 | if (env->cur_state) { | |
14610 | free_verifier_state(env->cur_state, true); | |
14611 | env->cur_state = NULL; | |
14612 | } | |
6f8a57cc AN |
14613 | while (!pop_stack(env, NULL, NULL, false)); |
14614 | if (!ret && pop_log) | |
14615 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 14616 | free_states(env); |
51c39bb1 AS |
14617 | return ret; |
14618 | } | |
14619 | ||
14620 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
14621 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
14622 | * Consider: | |
14623 | * int bar(int); | |
14624 | * int foo(int f) | |
14625 | * { | |
14626 | * return bar(f); | |
14627 | * } | |
14628 | * int bar(int b) | |
14629 | * { | |
14630 | * ... | |
14631 | * } | |
14632 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
14633 | * will be assumed that bar() already verified successfully and call to bar() | |
14634 | * from foo() will be checked for type match only. Later bar() will be verified | |
14635 | * independently to check that it's safe for R1=any_scalar_value. | |
14636 | */ | |
14637 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
14638 | { | |
14639 | struct bpf_prog_aux *aux = env->prog->aux; | |
14640 | int i, ret; | |
14641 | ||
14642 | if (!aux->func_info) | |
14643 | return 0; | |
14644 | ||
14645 | for (i = 1; i < env->subprog_cnt; i++) { | |
14646 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
14647 | continue; | |
14648 | env->insn_idx = env->subprog_info[i].start; | |
14649 | WARN_ON_ONCE(env->insn_idx == 0); | |
14650 | ret = do_check_common(env, i); | |
14651 | if (ret) { | |
14652 | return ret; | |
14653 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
14654 | verbose(env, | |
14655 | "Func#%d is safe for any args that match its prototype\n", | |
14656 | i); | |
14657 | } | |
14658 | } | |
14659 | return 0; | |
14660 | } | |
14661 | ||
14662 | static int do_check_main(struct bpf_verifier_env *env) | |
14663 | { | |
14664 | int ret; | |
14665 | ||
14666 | env->insn_idx = 0; | |
14667 | ret = do_check_common(env, 0); | |
14668 | if (!ret) | |
14669 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
14670 | return ret; | |
14671 | } | |
14672 | ||
14673 | ||
06ee7115 AS |
14674 | static void print_verification_stats(struct bpf_verifier_env *env) |
14675 | { | |
14676 | int i; | |
14677 | ||
14678 | if (env->log.level & BPF_LOG_STATS) { | |
14679 | verbose(env, "verification time %lld usec\n", | |
14680 | div_u64(env->verification_time, 1000)); | |
14681 | verbose(env, "stack depth "); | |
14682 | for (i = 0; i < env->subprog_cnt; i++) { | |
14683 | u32 depth = env->subprog_info[i].stack_depth; | |
14684 | ||
14685 | verbose(env, "%d", depth); | |
14686 | if (i + 1 < env->subprog_cnt) | |
14687 | verbose(env, "+"); | |
14688 | } | |
14689 | verbose(env, "\n"); | |
14690 | } | |
14691 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
14692 | "total_states %d peak_states %d mark_read %d\n", | |
14693 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
14694 | env->max_states_per_insn, env->total_states, | |
14695 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
14696 | } |
14697 | ||
27ae7997 MKL |
14698 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
14699 | { | |
14700 | const struct btf_type *t, *func_proto; | |
14701 | const struct bpf_struct_ops *st_ops; | |
14702 | const struct btf_member *member; | |
14703 | struct bpf_prog *prog = env->prog; | |
14704 | u32 btf_id, member_idx; | |
14705 | const char *mname; | |
14706 | ||
12aa8a94 THJ |
14707 | if (!prog->gpl_compatible) { |
14708 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
14709 | return -EINVAL; | |
14710 | } | |
14711 | ||
27ae7997 MKL |
14712 | btf_id = prog->aux->attach_btf_id; |
14713 | st_ops = bpf_struct_ops_find(btf_id); | |
14714 | if (!st_ops) { | |
14715 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
14716 | btf_id); | |
14717 | return -ENOTSUPP; | |
14718 | } | |
14719 | ||
14720 | t = st_ops->type; | |
14721 | member_idx = prog->expected_attach_type; | |
14722 | if (member_idx >= btf_type_vlen(t)) { | |
14723 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
14724 | member_idx, st_ops->name); | |
14725 | return -EINVAL; | |
14726 | } | |
14727 | ||
14728 | member = &btf_type_member(t)[member_idx]; | |
14729 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
14730 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
14731 | NULL); | |
14732 | if (!func_proto) { | |
14733 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
14734 | mname, member_idx, st_ops->name); | |
14735 | return -EINVAL; | |
14736 | } | |
14737 | ||
14738 | if (st_ops->check_member) { | |
14739 | int err = st_ops->check_member(t, member); | |
14740 | ||
14741 | if (err) { | |
14742 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
14743 | mname, st_ops->name); | |
14744 | return err; | |
14745 | } | |
14746 | } | |
14747 | ||
14748 | prog->aux->attach_func_proto = func_proto; | |
14749 | prog->aux->attach_func_name = mname; | |
14750 | env->ops = st_ops->verifier_ops; | |
14751 | ||
14752 | return 0; | |
14753 | } | |
6ba43b76 KS |
14754 | #define SECURITY_PREFIX "security_" |
14755 | ||
f7b12b6f | 14756 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 14757 | { |
69191754 | 14758 | if (within_error_injection_list(addr) || |
f7b12b6f | 14759 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 14760 | return 0; |
6ba43b76 | 14761 | |
6ba43b76 KS |
14762 | return -EINVAL; |
14763 | } | |
27ae7997 | 14764 | |
1e6c62a8 AS |
14765 | /* list of non-sleepable functions that are otherwise on |
14766 | * ALLOW_ERROR_INJECTION list | |
14767 | */ | |
14768 | BTF_SET_START(btf_non_sleepable_error_inject) | |
14769 | /* Three functions below can be called from sleepable and non-sleepable context. | |
14770 | * Assume non-sleepable from bpf safety point of view. | |
14771 | */ | |
9dd3d069 | 14772 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
14773 | BTF_ID(func, should_fail_alloc_page) |
14774 | BTF_ID(func, should_failslab) | |
14775 | BTF_SET_END(btf_non_sleepable_error_inject) | |
14776 | ||
14777 | static int check_non_sleepable_error_inject(u32 btf_id) | |
14778 | { | |
14779 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
14780 | } | |
14781 | ||
f7b12b6f THJ |
14782 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
14783 | const struct bpf_prog *prog, | |
14784 | const struct bpf_prog *tgt_prog, | |
14785 | u32 btf_id, | |
14786 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 14787 | { |
be8704ff | 14788 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 14789 | const char prefix[] = "btf_trace_"; |
5b92a28a | 14790 | int ret = 0, subprog = -1, i; |
38207291 | 14791 | const struct btf_type *t; |
5b92a28a | 14792 | bool conservative = true; |
38207291 | 14793 | const char *tname; |
5b92a28a | 14794 | struct btf *btf; |
f7b12b6f | 14795 | long addr = 0; |
38207291 | 14796 | |
f1b9509c | 14797 | if (!btf_id) { |
efc68158 | 14798 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
14799 | return -EINVAL; |
14800 | } | |
22dc4a0f | 14801 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 14802 | if (!btf) { |
efc68158 | 14803 | bpf_log(log, |
5b92a28a AS |
14804 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
14805 | return -EINVAL; | |
14806 | } | |
14807 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 14808 | if (!t) { |
efc68158 | 14809 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
14810 | return -EINVAL; |
14811 | } | |
5b92a28a | 14812 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 14813 | if (!tname) { |
efc68158 | 14814 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
14815 | return -EINVAL; |
14816 | } | |
5b92a28a AS |
14817 | if (tgt_prog) { |
14818 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
14819 | ||
14820 | for (i = 0; i < aux->func_info_cnt; i++) | |
14821 | if (aux->func_info[i].type_id == btf_id) { | |
14822 | subprog = i; | |
14823 | break; | |
14824 | } | |
14825 | if (subprog == -1) { | |
efc68158 | 14826 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
14827 | return -EINVAL; |
14828 | } | |
14829 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
14830 | if (prog_extension) { |
14831 | if (conservative) { | |
efc68158 | 14832 | bpf_log(log, |
be8704ff AS |
14833 | "Cannot replace static functions\n"); |
14834 | return -EINVAL; | |
14835 | } | |
14836 | if (!prog->jit_requested) { | |
efc68158 | 14837 | bpf_log(log, |
be8704ff AS |
14838 | "Extension programs should be JITed\n"); |
14839 | return -EINVAL; | |
14840 | } | |
be8704ff AS |
14841 | } |
14842 | if (!tgt_prog->jited) { | |
efc68158 | 14843 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
14844 | return -EINVAL; |
14845 | } | |
14846 | if (tgt_prog->type == prog->type) { | |
14847 | /* Cannot fentry/fexit another fentry/fexit program. | |
14848 | * Cannot attach program extension to another extension. | |
14849 | * It's ok to attach fentry/fexit to extension program. | |
14850 | */ | |
efc68158 | 14851 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
14852 | return -EINVAL; |
14853 | } | |
14854 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
14855 | prog_extension && | |
14856 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
14857 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
14858 | /* Program extensions can extend all program types | |
14859 | * except fentry/fexit. The reason is the following. | |
14860 | * The fentry/fexit programs are used for performance | |
14861 | * analysis, stats and can be attached to any program | |
14862 | * type except themselves. When extension program is | |
14863 | * replacing XDP function it is necessary to allow | |
14864 | * performance analysis of all functions. Both original | |
14865 | * XDP program and its program extension. Hence | |
14866 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
14867 | * allowed. If extending of fentry/fexit was allowed it | |
14868 | * would be possible to create long call chain | |
14869 | * fentry->extension->fentry->extension beyond | |
14870 | * reasonable stack size. Hence extending fentry is not | |
14871 | * allowed. | |
14872 | */ | |
efc68158 | 14873 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
14874 | return -EINVAL; |
14875 | } | |
5b92a28a | 14876 | } else { |
be8704ff | 14877 | if (prog_extension) { |
efc68158 | 14878 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
14879 | return -EINVAL; |
14880 | } | |
5b92a28a | 14881 | } |
f1b9509c AS |
14882 | |
14883 | switch (prog->expected_attach_type) { | |
14884 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 14885 | if (tgt_prog) { |
efc68158 | 14886 | bpf_log(log, |
5b92a28a AS |
14887 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
14888 | return -EINVAL; | |
14889 | } | |
38207291 | 14890 | if (!btf_type_is_typedef(t)) { |
efc68158 | 14891 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
14892 | btf_id); |
14893 | return -EINVAL; | |
14894 | } | |
f1b9509c | 14895 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 14896 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
14897 | btf_id, tname); |
14898 | return -EINVAL; | |
14899 | } | |
14900 | tname += sizeof(prefix) - 1; | |
5b92a28a | 14901 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
14902 | if (!btf_type_is_ptr(t)) |
14903 | /* should never happen in valid vmlinux build */ | |
14904 | return -EINVAL; | |
5b92a28a | 14905 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
14906 | if (!btf_type_is_func_proto(t)) |
14907 | /* should never happen in valid vmlinux build */ | |
14908 | return -EINVAL; | |
14909 | ||
f7b12b6f | 14910 | break; |
15d83c4d YS |
14911 | case BPF_TRACE_ITER: |
14912 | if (!btf_type_is_func(t)) { | |
efc68158 | 14913 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
14914 | btf_id); |
14915 | return -EINVAL; | |
14916 | } | |
14917 | t = btf_type_by_id(btf, t->type); | |
14918 | if (!btf_type_is_func_proto(t)) | |
14919 | return -EINVAL; | |
f7b12b6f THJ |
14920 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
14921 | if (ret) | |
14922 | return ret; | |
14923 | break; | |
be8704ff AS |
14924 | default: |
14925 | if (!prog_extension) | |
14926 | return -EINVAL; | |
df561f66 | 14927 | fallthrough; |
ae240823 | 14928 | case BPF_MODIFY_RETURN: |
9e4e01df | 14929 | case BPF_LSM_MAC: |
69fd337a | 14930 | case BPF_LSM_CGROUP: |
fec56f58 AS |
14931 | case BPF_TRACE_FENTRY: |
14932 | case BPF_TRACE_FEXIT: | |
14933 | if (!btf_type_is_func(t)) { | |
efc68158 | 14934 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
14935 | btf_id); |
14936 | return -EINVAL; | |
14937 | } | |
be8704ff | 14938 | if (prog_extension && |
efc68158 | 14939 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 14940 | return -EINVAL; |
5b92a28a | 14941 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
14942 | if (!btf_type_is_func_proto(t)) |
14943 | return -EINVAL; | |
f7b12b6f | 14944 | |
4a1e7c0c THJ |
14945 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
14946 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
14947 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
14948 | return -EINVAL; | |
14949 | ||
f7b12b6f | 14950 | if (tgt_prog && conservative) |
5b92a28a | 14951 | t = NULL; |
f7b12b6f THJ |
14952 | |
14953 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 14954 | if (ret < 0) |
f7b12b6f THJ |
14955 | return ret; |
14956 | ||
5b92a28a | 14957 | if (tgt_prog) { |
e9eeec58 YS |
14958 | if (subprog == 0) |
14959 | addr = (long) tgt_prog->bpf_func; | |
14960 | else | |
14961 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
14962 | } else { |
14963 | addr = kallsyms_lookup_name(tname); | |
14964 | if (!addr) { | |
efc68158 | 14965 | bpf_log(log, |
5b92a28a AS |
14966 | "The address of function %s cannot be found\n", |
14967 | tname); | |
f7b12b6f | 14968 | return -ENOENT; |
5b92a28a | 14969 | } |
fec56f58 | 14970 | } |
18644cec | 14971 | |
1e6c62a8 AS |
14972 | if (prog->aux->sleepable) { |
14973 | ret = -EINVAL; | |
14974 | switch (prog->type) { | |
14975 | case BPF_PROG_TYPE_TRACING: | |
14976 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
14977 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
14978 | */ | |
14979 | if (!check_non_sleepable_error_inject(btf_id) && | |
14980 | within_error_injection_list(addr)) | |
14981 | ret = 0; | |
14982 | break; | |
14983 | case BPF_PROG_TYPE_LSM: | |
14984 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
14985 | * Only some of them are sleepable. | |
14986 | */ | |
423f1610 | 14987 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
14988 | ret = 0; |
14989 | break; | |
14990 | default: | |
14991 | break; | |
14992 | } | |
f7b12b6f THJ |
14993 | if (ret) { |
14994 | bpf_log(log, "%s is not sleepable\n", tname); | |
14995 | return ret; | |
14996 | } | |
1e6c62a8 | 14997 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 14998 | if (tgt_prog) { |
efc68158 | 14999 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
15000 | return -EINVAL; |
15001 | } | |
15002 | ret = check_attach_modify_return(addr, tname); | |
15003 | if (ret) { | |
15004 | bpf_log(log, "%s() is not modifiable\n", tname); | |
15005 | return ret; | |
1af9270e | 15006 | } |
18644cec | 15007 | } |
f7b12b6f THJ |
15008 | |
15009 | break; | |
15010 | } | |
15011 | tgt_info->tgt_addr = addr; | |
15012 | tgt_info->tgt_name = tname; | |
15013 | tgt_info->tgt_type = t; | |
15014 | return 0; | |
15015 | } | |
15016 | ||
35e3815f JO |
15017 | BTF_SET_START(btf_id_deny) |
15018 | BTF_ID_UNUSED | |
15019 | #ifdef CONFIG_SMP | |
15020 | BTF_ID(func, migrate_disable) | |
15021 | BTF_ID(func, migrate_enable) | |
15022 | #endif | |
15023 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
15024 | BTF_ID(func, rcu_read_unlock_strict) | |
15025 | #endif | |
15026 | BTF_SET_END(btf_id_deny) | |
15027 | ||
f7b12b6f THJ |
15028 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
15029 | { | |
15030 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 15031 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
15032 | struct bpf_attach_target_info tgt_info = {}; |
15033 | u32 btf_id = prog->aux->attach_btf_id; | |
15034 | struct bpf_trampoline *tr; | |
15035 | int ret; | |
15036 | u64 key; | |
15037 | ||
79a7f8bd AS |
15038 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
15039 | if (prog->aux->sleepable) | |
15040 | /* attach_btf_id checked to be zero already */ | |
15041 | return 0; | |
15042 | verbose(env, "Syscall programs can only be sleepable\n"); | |
15043 | return -EINVAL; | |
15044 | } | |
15045 | ||
f7b12b6f | 15046 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
64ad7556 DK |
15047 | prog->type != BPF_PROG_TYPE_LSM && prog->type != BPF_PROG_TYPE_KPROBE) { |
15048 | verbose(env, "Only fentry/fexit/fmod_ret, lsm, and kprobe/uprobe programs can be sleepable\n"); | |
f7b12b6f THJ |
15049 | return -EINVAL; |
15050 | } | |
15051 | ||
15052 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
15053 | return check_struct_ops_btf_id(env); | |
15054 | ||
15055 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
15056 | prog->type != BPF_PROG_TYPE_LSM && | |
15057 | prog->type != BPF_PROG_TYPE_EXT) | |
15058 | return 0; | |
15059 | ||
15060 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
15061 | if (ret) | |
fec56f58 | 15062 | return ret; |
f7b12b6f THJ |
15063 | |
15064 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
15065 | /* to make freplace equivalent to their targets, they need to |
15066 | * inherit env->ops and expected_attach_type for the rest of the | |
15067 | * verification | |
15068 | */ | |
f7b12b6f THJ |
15069 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
15070 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
15071 | } | |
15072 | ||
15073 | /* store info about the attachment target that will be used later */ | |
15074 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
15075 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
15076 | ||
4a1e7c0c THJ |
15077 | if (tgt_prog) { |
15078 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
15079 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
15080 | } | |
15081 | ||
f7b12b6f THJ |
15082 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
15083 | prog->aux->attach_btf_trace = true; | |
15084 | return 0; | |
15085 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
15086 | if (!bpf_iter_prog_supported(prog)) | |
15087 | return -EINVAL; | |
15088 | return 0; | |
15089 | } | |
15090 | ||
15091 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
15092 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
15093 | if (ret < 0) | |
15094 | return ret; | |
35e3815f JO |
15095 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
15096 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
15097 | return -EINVAL; | |
38207291 | 15098 | } |
f7b12b6f | 15099 | |
22dc4a0f | 15100 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
15101 | tr = bpf_trampoline_get(key, &tgt_info); |
15102 | if (!tr) | |
15103 | return -ENOMEM; | |
15104 | ||
3aac1ead | 15105 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 15106 | return 0; |
38207291 MKL |
15107 | } |
15108 | ||
76654e67 AM |
15109 | struct btf *bpf_get_btf_vmlinux(void) |
15110 | { | |
15111 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
15112 | mutex_lock(&bpf_verifier_lock); | |
15113 | if (!btf_vmlinux) | |
15114 | btf_vmlinux = btf_parse_vmlinux(); | |
15115 | mutex_unlock(&bpf_verifier_lock); | |
15116 | } | |
15117 | return btf_vmlinux; | |
15118 | } | |
15119 | ||
af2ac3e1 | 15120 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 15121 | { |
06ee7115 | 15122 | u64 start_time = ktime_get_ns(); |
58e2af8b | 15123 | struct bpf_verifier_env *env; |
b9193c1b | 15124 | struct bpf_verifier_log *log; |
9e4c24e7 | 15125 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 15126 | bool is_priv; |
51580e79 | 15127 | |
eba0c929 AB |
15128 | /* no program is valid */ |
15129 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
15130 | return -EINVAL; | |
15131 | ||
58e2af8b | 15132 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
15133 | * allocate/free it every time bpf_check() is called |
15134 | */ | |
58e2af8b | 15135 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
15136 | if (!env) |
15137 | return -ENOMEM; | |
61bd5218 | 15138 | log = &env->log; |
cbd35700 | 15139 | |
9e4c24e7 | 15140 | len = (*prog)->len; |
fad953ce | 15141 | env->insn_aux_data = |
9e4c24e7 | 15142 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
15143 | ret = -ENOMEM; |
15144 | if (!env->insn_aux_data) | |
15145 | goto err_free_env; | |
9e4c24e7 JK |
15146 | for (i = 0; i < len; i++) |
15147 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 15148 | env->prog = *prog; |
00176a34 | 15149 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 15150 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 15151 | is_priv = bpf_capable(); |
0246e64d | 15152 | |
76654e67 | 15153 | bpf_get_btf_vmlinux(); |
8580ac94 | 15154 | |
cbd35700 | 15155 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
15156 | if (!is_priv) |
15157 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
15158 | |
15159 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
15160 | /* user requested verbose verifier output | |
15161 | * and supplied buffer to store the verification trace | |
15162 | */ | |
e7bf8249 JK |
15163 | log->level = attr->log_level; |
15164 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
15165 | log->len_total = attr->log_size; | |
cbd35700 | 15166 | |
e7bf8249 | 15167 | /* log attributes have to be sane */ |
866de407 HT |
15168 | if (!bpf_verifier_log_attr_valid(log)) { |
15169 | ret = -EINVAL; | |
3df126f3 | 15170 | goto err_unlock; |
866de407 | 15171 | } |
cbd35700 | 15172 | } |
1ad2f583 | 15173 | |
0f55f9ed CL |
15174 | mark_verifier_state_clean(env); |
15175 | ||
8580ac94 AS |
15176 | if (IS_ERR(btf_vmlinux)) { |
15177 | /* Either gcc or pahole or kernel are broken. */ | |
15178 | verbose(env, "in-kernel BTF is malformed\n"); | |
15179 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 15180 | goto skip_full_check; |
8580ac94 AS |
15181 | } |
15182 | ||
1ad2f583 DB |
15183 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
15184 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 15185 | env->strict_alignment = true; |
e9ee9efc DM |
15186 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
15187 | env->strict_alignment = false; | |
cbd35700 | 15188 | |
2c78ee89 | 15189 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 15190 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
41c48f3a | 15191 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
15192 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
15193 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
15194 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 15195 | |
10d274e8 AS |
15196 | if (is_priv) |
15197 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
15198 | ||
dc2a4ebc | 15199 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 15200 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
15201 | GFP_USER); |
15202 | ret = -ENOMEM; | |
15203 | if (!env->explored_states) | |
15204 | goto skip_full_check; | |
15205 | ||
e6ac2450 MKL |
15206 | ret = add_subprog_and_kfunc(env); |
15207 | if (ret < 0) | |
15208 | goto skip_full_check; | |
15209 | ||
d9762e84 | 15210 | ret = check_subprogs(env); |
475fb78f AS |
15211 | if (ret < 0) |
15212 | goto skip_full_check; | |
15213 | ||
c454a46b | 15214 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
15215 | if (ret < 0) |
15216 | goto skip_full_check; | |
15217 | ||
be8704ff AS |
15218 | ret = check_attach_btf_id(env); |
15219 | if (ret) | |
15220 | goto skip_full_check; | |
15221 | ||
4976b718 HL |
15222 | ret = resolve_pseudo_ldimm64(env); |
15223 | if (ret < 0) | |
15224 | goto skip_full_check; | |
15225 | ||
ceb11679 YZ |
15226 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
15227 | ret = bpf_prog_offload_verifier_prep(env->prog); | |
15228 | if (ret) | |
15229 | goto skip_full_check; | |
15230 | } | |
15231 | ||
d9762e84 MKL |
15232 | ret = check_cfg(env); |
15233 | if (ret < 0) | |
15234 | goto skip_full_check; | |
15235 | ||
51c39bb1 AS |
15236 | ret = do_check_subprogs(env); |
15237 | ret = ret ?: do_check_main(env); | |
cbd35700 | 15238 | |
c941ce9c QM |
15239 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
15240 | ret = bpf_prog_offload_finalize(env); | |
15241 | ||
0246e64d | 15242 | skip_full_check: |
51c39bb1 | 15243 | kvfree(env->explored_states); |
0246e64d | 15244 | |
c131187d | 15245 | if (ret == 0) |
9b38c405 | 15246 | ret = check_max_stack_depth(env); |
c131187d | 15247 | |
9b38c405 | 15248 | /* instruction rewrites happen after this point */ |
1ade2371 EZ |
15249 | if (ret == 0) |
15250 | ret = optimize_bpf_loop(env); | |
15251 | ||
e2ae4ca2 JK |
15252 | if (is_priv) { |
15253 | if (ret == 0) | |
15254 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
15255 | if (ret == 0) |
15256 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
15257 | if (ret == 0) |
15258 | ret = opt_remove_nops(env); | |
52875a04 JK |
15259 | } else { |
15260 | if (ret == 0) | |
15261 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
15262 | } |
15263 | ||
9bac3d6d AS |
15264 | if (ret == 0) |
15265 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
15266 | ret = convert_ctx_accesses(env); | |
15267 | ||
e245c5c6 | 15268 | if (ret == 0) |
e6ac5933 | 15269 | ret = do_misc_fixups(env); |
e245c5c6 | 15270 | |
a4b1d3c1 JW |
15271 | /* do 32-bit optimization after insn patching has done so those patched |
15272 | * insns could be handled correctly. | |
15273 | */ | |
d6c2308c JW |
15274 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
15275 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
15276 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
15277 | : false; | |
a4b1d3c1 JW |
15278 | } |
15279 | ||
1ea47e01 AS |
15280 | if (ret == 0) |
15281 | ret = fixup_call_args(env); | |
15282 | ||
06ee7115 AS |
15283 | env->verification_time = ktime_get_ns() - start_time; |
15284 | print_verification_stats(env); | |
aba64c7d | 15285 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 15286 | |
a2a7d570 | 15287 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 15288 | ret = -ENOSPC; |
a2a7d570 | 15289 | if (log->level && !log->ubuf) { |
cbd35700 | 15290 | ret = -EFAULT; |
a2a7d570 | 15291 | goto err_release_maps; |
cbd35700 AS |
15292 | } |
15293 | ||
541c3bad AN |
15294 | if (ret) |
15295 | goto err_release_maps; | |
15296 | ||
15297 | if (env->used_map_cnt) { | |
0246e64d | 15298 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
15299 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
15300 | sizeof(env->used_maps[0]), | |
15301 | GFP_KERNEL); | |
0246e64d | 15302 | |
9bac3d6d | 15303 | if (!env->prog->aux->used_maps) { |
0246e64d | 15304 | ret = -ENOMEM; |
a2a7d570 | 15305 | goto err_release_maps; |
0246e64d AS |
15306 | } |
15307 | ||
9bac3d6d | 15308 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 15309 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 15310 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
15311 | } |
15312 | if (env->used_btf_cnt) { | |
15313 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
15314 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
15315 | sizeof(env->used_btfs[0]), | |
15316 | GFP_KERNEL); | |
15317 | if (!env->prog->aux->used_btfs) { | |
15318 | ret = -ENOMEM; | |
15319 | goto err_release_maps; | |
15320 | } | |
0246e64d | 15321 | |
541c3bad AN |
15322 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
15323 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
15324 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
15325 | } | |
15326 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
15327 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
15328 | * bpf_ld_imm64 instructions | |
15329 | */ | |
15330 | convert_pseudo_ld_imm64(env); | |
15331 | } | |
cbd35700 | 15332 | |
541c3bad | 15333 | adjust_btf_func(env); |
ba64e7d8 | 15334 | |
a2a7d570 | 15335 | err_release_maps: |
9bac3d6d | 15336 | if (!env->prog->aux->used_maps) |
0246e64d | 15337 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 15338 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
15339 | */ |
15340 | release_maps(env); | |
541c3bad AN |
15341 | if (!env->prog->aux->used_btfs) |
15342 | release_btfs(env); | |
03f87c0b THJ |
15343 | |
15344 | /* extension progs temporarily inherit the attach_type of their targets | |
15345 | for verification purposes, so set it back to zero before returning | |
15346 | */ | |
15347 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
15348 | env->prog->expected_attach_type = 0; | |
15349 | ||
9bac3d6d | 15350 | *prog = env->prog; |
3df126f3 | 15351 | err_unlock: |
45a73c17 AS |
15352 | if (!is_priv) |
15353 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
15354 | vfree(env->insn_aux_data); |
15355 | err_free_env: | |
15356 | kfree(env); | |
51580e79 AS |
15357 | return ret; |
15358 | } |