Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
51580e79 | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
969bf05e | 3 | * Copyright (c) 2016 Facebook |
fd978bf7 | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
51580e79 | 5 | */ |
838e9690 | 6 | #include <uapi/linux/btf.h> |
aef2feda | 7 | #include <linux/bpf-cgroup.h> |
51580e79 AS |
8 | #include <linux/kernel.h> |
9 | #include <linux/types.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/bpf.h> | |
838e9690 | 12 | #include <linux/btf.h> |
58e2af8b | 13 | #include <linux/bpf_verifier.h> |
51580e79 AS |
14 | #include <linux/filter.h> |
15 | #include <net/netlink.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/vmalloc.h> | |
ebb676da | 18 | #include <linux/stringify.h> |
cc8b0b92 AS |
19 | #include <linux/bsearch.h> |
20 | #include <linux/sort.h> | |
c195651e | 21 | #include <linux/perf_event.h> |
d9762e84 | 22 | #include <linux/ctype.h> |
6ba43b76 | 23 | #include <linux/error-injection.h> |
9e4e01df | 24 | #include <linux/bpf_lsm.h> |
1e6c62a8 | 25 | #include <linux/btf_ids.h> |
47e34cb7 | 26 | #include <linux/poison.h> |
51580e79 | 27 | |
f4ac7e0b JK |
28 | #include "disasm.h" |
29 | ||
00176a34 | 30 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 31 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
32 | [_id] = & _name ## _verifier_ops, |
33 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 34 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
35 | #include <linux/bpf_types.h> |
36 | #undef BPF_PROG_TYPE | |
37 | #undef BPF_MAP_TYPE | |
f2e10bff | 38 | #undef BPF_LINK_TYPE |
00176a34 JK |
39 | }; |
40 | ||
51580e79 AS |
41 | /* bpf_check() is a static code analyzer that walks eBPF program |
42 | * instruction by instruction and updates register/stack state. | |
43 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
44 | * | |
45 | * The first pass is depth-first-search to check that the program is a DAG. | |
46 | * It rejects the following programs: | |
47 | * - larger than BPF_MAXINSNS insns | |
48 | * - if loop is present (detected via back-edge) | |
49 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
50 | * - out of bounds or malformed jumps | |
51 | * The second pass is all possible path descent from the 1st insn. | |
8fb33b60 | 52 | * Since it's analyzing all paths through the program, the length of the |
eba38a96 | 53 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
54 | * insn is less then 4K, but there are too many branches that change stack/regs. |
55 | * Number of 'branches to be analyzed' is limited to 1k | |
56 | * | |
57 | * On entry to each instruction, each register has a type, and the instruction | |
58 | * changes the types of the registers depending on instruction semantics. | |
59 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
60 | * copied to R1. | |
61 | * | |
62 | * All registers are 64-bit. | |
63 | * R0 - return register | |
64 | * R1-R5 argument passing registers | |
65 | * R6-R9 callee saved registers | |
66 | * R10 - frame pointer read-only | |
67 | * | |
68 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
69 | * and has type PTR_TO_CTX. | |
70 | * | |
71 | * Verifier tracks arithmetic operations on pointers in case: | |
72 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
73 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
74 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
75 | * and 2nd arithmetic instruction is pattern matched to recognize | |
76 | * that it wants to construct a pointer to some element within stack. | |
77 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
78 | * (and -20 constant is saved for further stack bounds checking). | |
79 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
80 | * | |
f1174f77 | 81 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 82 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 83 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
84 | * |
85 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
86 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
87 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
88 | * |
89 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
90 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
91 | * | |
92 | * registers used to pass values to function calls are checked against | |
93 | * function argument constraints. | |
94 | * | |
95 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
96 | * It means that the register type passed to this function must be | |
97 | * PTR_TO_STACK and it will be used inside the function as | |
98 | * 'pointer to map element key' | |
99 | * | |
100 | * For example the argument constraints for bpf_map_lookup_elem(): | |
101 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
102 | * .arg1_type = ARG_CONST_MAP_PTR, | |
103 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
104 | * | |
105 | * ret_type says that this function returns 'pointer to map elem value or null' | |
106 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
107 | * 2nd argument should be a pointer to stack, which will be used inside | |
108 | * the helper function as a pointer to map element key. | |
109 | * | |
110 | * On the kernel side the helper function looks like: | |
111 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
112 | * { | |
113 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
114 | * void *key = (void *) (unsigned long) r2; | |
115 | * void *value; | |
116 | * | |
117 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
118 | * [key, key + map->key_size) bytes are valid and were initialized on | |
119 | * the stack of eBPF program. | |
120 | * } | |
121 | * | |
122 | * Corresponding eBPF program may look like: | |
123 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
124 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
125 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
126 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
127 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
128 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
129 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
130 | * | |
131 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
132 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
133 | * and were initialized prior to this call. | |
134 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
135 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
136 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
8fb33b60 | 137 | * returns either pointer to map value or NULL. |
51580e79 AS |
138 | * |
139 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
140 | * insn, the register holding that pointer in the true branch changes state to | |
141 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
142 | * branch. See check_cond_jmp_op(). | |
143 | * | |
144 | * After the call R0 is set to return type of the function and registers R1-R5 | |
145 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
146 | * |
147 | * The following reference types represent a potential reference to a kernel | |
148 | * resource which, after first being allocated, must be checked and freed by | |
149 | * the BPF program: | |
150 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
151 | * | |
152 | * When the verifier sees a helper call return a reference type, it allocates a | |
153 | * pointer id for the reference and stores it in the current function state. | |
154 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
155 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
156 | * passes through a NULL-check conditional. For the branch wherein the state is | |
157 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
158 | * |
159 | * For each helper function that allocates a reference, such as | |
160 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
161 | * bpf_sk_release(). When a reference type passes into the release function, | |
162 | * the verifier also releases the reference. If any unchecked or unreleased | |
163 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
164 | */ |
165 | ||
17a52670 | 166 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 167 | struct bpf_verifier_stack_elem { |
17a52670 AS |
168 | /* verifer state is 'st' |
169 | * before processing instruction 'insn_idx' | |
170 | * and after processing instruction 'prev_insn_idx' | |
171 | */ | |
58e2af8b | 172 | struct bpf_verifier_state st; |
17a52670 AS |
173 | int insn_idx; |
174 | int prev_insn_idx; | |
58e2af8b | 175 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
176 | /* length of verifier log at the time this state was pushed on stack */ |
177 | u32 log_pos; | |
cbd35700 AS |
178 | }; |
179 | ||
b285fcb7 | 180 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 181 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 182 | |
d2e4c1e6 DB |
183 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
184 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
185 | ||
c93552c4 DB |
186 | #define BPF_MAP_PTR_UNPRIV 1UL |
187 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
188 | POISON_POINTER_DELTA)) | |
189 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
190 | ||
bc34dee6 JK |
191 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx); |
192 | static int release_reference(struct bpf_verifier_env *env, int ref_obj_id); | |
193 | ||
c93552c4 DB |
194 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
195 | { | |
d2e4c1e6 | 196 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
197 | } |
198 | ||
199 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
200 | { | |
d2e4c1e6 | 201 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
202 | } |
203 | ||
204 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
205 | const struct bpf_map *map, bool unpriv) | |
206 | { | |
207 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
208 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
209 | aux->map_ptr_state = (unsigned long)map | |
210 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
211 | } | |
212 | ||
213 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
214 | { | |
215 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
216 | } | |
217 | ||
218 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
219 | { | |
220 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
221 | } | |
222 | ||
223 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
224 | { | |
225 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
226 | } | |
227 | ||
228 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
229 | { | |
230 | bool poisoned = bpf_map_key_poisoned(aux); | |
231 | ||
232 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
233 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 234 | } |
fad73a1a | 235 | |
23a2d70c YS |
236 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
237 | { | |
238 | return insn->code == (BPF_JMP | BPF_CALL) && | |
239 | insn->src_reg == BPF_PSEUDO_CALL; | |
240 | } | |
241 | ||
e6ac2450 MKL |
242 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
243 | { | |
244 | return insn->code == (BPF_JMP | BPF_CALL) && | |
245 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; | |
246 | } | |
247 | ||
33ff9823 DB |
248 | struct bpf_call_arg_meta { |
249 | struct bpf_map *map_ptr; | |
435faee1 | 250 | bool raw_mode; |
36bbef52 | 251 | bool pkt_access; |
8f14852e | 252 | u8 release_regno; |
435faee1 DB |
253 | int regno; |
254 | int access_size; | |
457f4436 | 255 | int mem_size; |
10060503 | 256 | u64 msize_max_value; |
1b986589 | 257 | int ref_obj_id; |
3e8ce298 | 258 | int map_uid; |
d83525ca | 259 | int func_id; |
22dc4a0f | 260 | struct btf *btf; |
eaa6bcb7 | 261 | u32 btf_id; |
22dc4a0f | 262 | struct btf *ret_btf; |
eaa6bcb7 | 263 | u32 ret_btf_id; |
69c087ba | 264 | u32 subprogno; |
aa3496ac | 265 | struct btf_field *kptr_field; |
97e03f52 | 266 | u8 uninit_dynptr_regno; |
33ff9823 DB |
267 | }; |
268 | ||
8580ac94 AS |
269 | struct btf *btf_vmlinux; |
270 | ||
cbd35700 AS |
271 | static DEFINE_MUTEX(bpf_verifier_lock); |
272 | ||
d9762e84 MKL |
273 | static const struct bpf_line_info * |
274 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
275 | { | |
276 | const struct bpf_line_info *linfo; | |
277 | const struct bpf_prog *prog; | |
278 | u32 i, nr_linfo; | |
279 | ||
280 | prog = env->prog; | |
281 | nr_linfo = prog->aux->nr_linfo; | |
282 | ||
283 | if (!nr_linfo || insn_off >= prog->len) | |
284 | return NULL; | |
285 | ||
286 | linfo = prog->aux->linfo; | |
287 | for (i = 1; i < nr_linfo; i++) | |
288 | if (insn_off < linfo[i].insn_off) | |
289 | break; | |
290 | ||
291 | return &linfo[i - 1]; | |
292 | } | |
293 | ||
77d2e05a MKL |
294 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
295 | va_list args) | |
cbd35700 | 296 | { |
a2a7d570 | 297 | unsigned int n; |
cbd35700 | 298 | |
a2a7d570 | 299 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
300 | |
301 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
302 | "verifier log line truncated - local buffer too short\n"); | |
303 | ||
8580ac94 | 304 | if (log->level == BPF_LOG_KERNEL) { |
436d404c HT |
305 | bool newline = n > 0 && log->kbuf[n - 1] == '\n'; |
306 | ||
307 | pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); | |
8580ac94 AS |
308 | return; |
309 | } | |
436d404c HT |
310 | |
311 | n = min(log->len_total - log->len_used - 1, n); | |
312 | log->kbuf[n] = '\0'; | |
a2a7d570 JK |
313 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
314 | log->len_used += n; | |
315 | else | |
316 | log->ubuf = NULL; | |
cbd35700 | 317 | } |
abe08840 | 318 | |
6f8a57cc AN |
319 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
320 | { | |
321 | char zero = 0; | |
322 | ||
323 | if (!bpf_verifier_log_needed(log)) | |
324 | return; | |
325 | ||
326 | log->len_used = new_pos; | |
327 | if (put_user(zero, log->ubuf + new_pos)) | |
328 | log->ubuf = NULL; | |
329 | } | |
330 | ||
abe08840 JO |
331 | /* log_level controls verbosity level of eBPF verifier. |
332 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
333 | * so the user can figure out what's wrong with the program | |
430e68d1 | 334 | */ |
abe08840 JO |
335 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
336 | const char *fmt, ...) | |
337 | { | |
338 | va_list args; | |
339 | ||
77d2e05a MKL |
340 | if (!bpf_verifier_log_needed(&env->log)) |
341 | return; | |
342 | ||
abe08840 | 343 | va_start(args, fmt); |
77d2e05a | 344 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
345 | va_end(args); |
346 | } | |
347 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
348 | ||
349 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
350 | { | |
77d2e05a | 351 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
352 | va_list args; |
353 | ||
77d2e05a MKL |
354 | if (!bpf_verifier_log_needed(&env->log)) |
355 | return; | |
356 | ||
abe08840 | 357 | va_start(args, fmt); |
77d2e05a | 358 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
359 | va_end(args); |
360 | } | |
cbd35700 | 361 | |
9e15db66 AS |
362 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
363 | const char *fmt, ...) | |
364 | { | |
365 | va_list args; | |
366 | ||
367 | if (!bpf_verifier_log_needed(log)) | |
368 | return; | |
369 | ||
370 | va_start(args, fmt); | |
371 | bpf_verifier_vlog(log, fmt, args); | |
372 | va_end(args); | |
373 | } | |
84c6ac41 | 374 | EXPORT_SYMBOL_GPL(bpf_log); |
9e15db66 | 375 | |
d9762e84 MKL |
376 | static const char *ltrim(const char *s) |
377 | { | |
378 | while (isspace(*s)) | |
379 | s++; | |
380 | ||
381 | return s; | |
382 | } | |
383 | ||
384 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
385 | u32 insn_off, | |
386 | const char *prefix_fmt, ...) | |
387 | { | |
388 | const struct bpf_line_info *linfo; | |
389 | ||
390 | if (!bpf_verifier_log_needed(&env->log)) | |
391 | return; | |
392 | ||
393 | linfo = find_linfo(env, insn_off); | |
394 | if (!linfo || linfo == env->prev_linfo) | |
395 | return; | |
396 | ||
397 | if (prefix_fmt) { | |
398 | va_list args; | |
399 | ||
400 | va_start(args, prefix_fmt); | |
401 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
402 | va_end(args); | |
403 | } | |
404 | ||
405 | verbose(env, "%s\n", | |
406 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
407 | linfo->line_off))); | |
408 | ||
409 | env->prev_linfo = linfo; | |
410 | } | |
411 | ||
bc2591d6 YS |
412 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
413 | struct bpf_reg_state *reg, | |
414 | struct tnum *range, const char *ctx, | |
415 | const char *reg_name) | |
416 | { | |
417 | char tn_buf[48]; | |
418 | ||
419 | verbose(env, "At %s the register %s ", ctx, reg_name); | |
420 | if (!tnum_is_unknown(reg->var_off)) { | |
421 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
422 | verbose(env, "has value %s", tn_buf); | |
423 | } else { | |
424 | verbose(env, "has unknown scalar value"); | |
425 | } | |
426 | tnum_strn(tn_buf, sizeof(tn_buf), *range); | |
427 | verbose(env, " should have been in %s\n", tn_buf); | |
428 | } | |
429 | ||
de8f3a83 DB |
430 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
431 | { | |
0c9a7a7e | 432 | type = base_type(type); |
de8f3a83 DB |
433 | return type == PTR_TO_PACKET || |
434 | type == PTR_TO_PACKET_META; | |
435 | } | |
436 | ||
46f8bc92 MKL |
437 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
438 | { | |
439 | return type == PTR_TO_SOCKET || | |
655a51e5 | 440 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
441 | type == PTR_TO_TCP_SOCK || |
442 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
443 | } |
444 | ||
cac616db JF |
445 | static bool reg_type_not_null(enum bpf_reg_type type) |
446 | { | |
447 | return type == PTR_TO_SOCKET || | |
448 | type == PTR_TO_TCP_SOCK || | |
449 | type == PTR_TO_MAP_VALUE || | |
69c087ba | 450 | type == PTR_TO_MAP_KEY || |
01c66c48 | 451 | type == PTR_TO_SOCK_COMMON; |
cac616db JF |
452 | } |
453 | ||
4e814da0 KKD |
454 | static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg) |
455 | { | |
456 | struct btf_record *rec = NULL; | |
457 | struct btf_struct_meta *meta; | |
458 | ||
459 | if (reg->type == PTR_TO_MAP_VALUE) { | |
460 | rec = reg->map_ptr->record; | |
461 | } else if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
462 | meta = btf_find_struct_meta(reg->btf, reg->btf_id); | |
463 | if (meta) | |
464 | rec = meta->record; | |
465 | } | |
466 | return rec; | |
467 | } | |
468 | ||
d83525ca AS |
469 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
470 | { | |
4e814da0 | 471 | return btf_record_has_field(reg_btf_record(reg), BPF_SPIN_LOCK); |
cba368c1 MKL |
472 | } |
473 | ||
20b2aff4 HL |
474 | static bool type_is_rdonly_mem(u32 type) |
475 | { | |
476 | return type & MEM_RDONLY; | |
cba368c1 MKL |
477 | } |
478 | ||
48946bd6 | 479 | static bool type_may_be_null(u32 type) |
fd1b0d60 | 480 | { |
48946bd6 | 481 | return type & PTR_MAYBE_NULL; |
fd1b0d60 LB |
482 | } |
483 | ||
64d85290 JS |
484 | static bool is_acquire_function(enum bpf_func_id func_id, |
485 | const struct bpf_map *map) | |
486 | { | |
487 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
488 | ||
489 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
490 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 | 491 | func_id == BPF_FUNC_skc_lookup_tcp || |
c0a5a21c KKD |
492 | func_id == BPF_FUNC_ringbuf_reserve || |
493 | func_id == BPF_FUNC_kptr_xchg) | |
64d85290 JS |
494 | return true; |
495 | ||
496 | if (func_id == BPF_FUNC_map_lookup_elem && | |
497 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
498 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
499 | return true; | |
500 | ||
501 | return false; | |
46f8bc92 MKL |
502 | } |
503 | ||
1b986589 MKL |
504 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
505 | { | |
506 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
507 | func_id == BPF_FUNC_sk_fullsock || |
508 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
509 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
510 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
3bc253c2 | 511 | func_id == BPF_FUNC_skc_to_mptcp_sock || |
1df8f55a MKL |
512 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
513 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
514 | } |
515 | ||
88374342 | 516 | static bool is_dynptr_ref_function(enum bpf_func_id func_id) |
b2d8ef19 DM |
517 | { |
518 | return func_id == BPF_FUNC_dynptr_data; | |
519 | } | |
520 | ||
be2ef816 AN |
521 | static bool is_callback_calling_function(enum bpf_func_id func_id) |
522 | { | |
523 | return func_id == BPF_FUNC_for_each_map_elem || | |
524 | func_id == BPF_FUNC_timer_set_callback || | |
525 | func_id == BPF_FUNC_find_vma || | |
526 | func_id == BPF_FUNC_loop || | |
527 | func_id == BPF_FUNC_user_ringbuf_drain; | |
528 | } | |
529 | ||
b2d8ef19 DM |
530 | static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id, |
531 | const struct bpf_map *map) | |
532 | { | |
533 | int ref_obj_uses = 0; | |
534 | ||
535 | if (is_ptr_cast_function(func_id)) | |
536 | ref_obj_uses++; | |
537 | if (is_acquire_function(func_id, map)) | |
538 | ref_obj_uses++; | |
88374342 | 539 | if (is_dynptr_ref_function(func_id)) |
b2d8ef19 DM |
540 | ref_obj_uses++; |
541 | ||
542 | return ref_obj_uses > 1; | |
543 | } | |
544 | ||
39491867 BJ |
545 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
546 | { | |
547 | return BPF_CLASS(insn->code) == BPF_STX && | |
548 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
549 | insn->imm == BPF_CMPXCHG; | |
550 | } | |
551 | ||
c25b2ae1 HL |
552 | /* string representation of 'enum bpf_reg_type' |
553 | * | |
554 | * Note that reg_type_str() can not appear more than once in a single verbose() | |
555 | * statement. | |
556 | */ | |
557 | static const char *reg_type_str(struct bpf_verifier_env *env, | |
558 | enum bpf_reg_type type) | |
559 | { | |
c6f1bfe8 | 560 | char postfix[16] = {0}, prefix[32] = {0}; |
c25b2ae1 HL |
561 | static const char * const str[] = { |
562 | [NOT_INIT] = "?", | |
7df5072c | 563 | [SCALAR_VALUE] = "scalar", |
c25b2ae1 HL |
564 | [PTR_TO_CTX] = "ctx", |
565 | [CONST_PTR_TO_MAP] = "map_ptr", | |
566 | [PTR_TO_MAP_VALUE] = "map_value", | |
567 | [PTR_TO_STACK] = "fp", | |
568 | [PTR_TO_PACKET] = "pkt", | |
569 | [PTR_TO_PACKET_META] = "pkt_meta", | |
570 | [PTR_TO_PACKET_END] = "pkt_end", | |
571 | [PTR_TO_FLOW_KEYS] = "flow_keys", | |
572 | [PTR_TO_SOCKET] = "sock", | |
573 | [PTR_TO_SOCK_COMMON] = "sock_common", | |
574 | [PTR_TO_TCP_SOCK] = "tcp_sock", | |
575 | [PTR_TO_TP_BUFFER] = "tp_buffer", | |
576 | [PTR_TO_XDP_SOCK] = "xdp_sock", | |
577 | [PTR_TO_BTF_ID] = "ptr_", | |
c25b2ae1 | 578 | [PTR_TO_MEM] = "mem", |
20b2aff4 | 579 | [PTR_TO_BUF] = "buf", |
c25b2ae1 HL |
580 | [PTR_TO_FUNC] = "func", |
581 | [PTR_TO_MAP_KEY] = "map_key", | |
20571567 | 582 | [PTR_TO_DYNPTR] = "dynptr_ptr", |
c25b2ae1 HL |
583 | }; |
584 | ||
585 | if (type & PTR_MAYBE_NULL) { | |
5844101a | 586 | if (base_type(type) == PTR_TO_BTF_ID) |
c25b2ae1 HL |
587 | strncpy(postfix, "or_null_", 16); |
588 | else | |
589 | strncpy(postfix, "_or_null", 16); | |
590 | } | |
591 | ||
20b2aff4 | 592 | if (type & MEM_RDONLY) |
c6f1bfe8 | 593 | strncpy(prefix, "rdonly_", 32); |
894f2a8b KKD |
594 | if (type & MEM_RINGBUF) |
595 | strncpy(prefix, "ringbuf_", 32); | |
c6f1bfe8 YS |
596 | if (type & MEM_USER) |
597 | strncpy(prefix, "user_", 32); | |
5844101a HL |
598 | if (type & MEM_PERCPU) |
599 | strncpy(prefix, "percpu_", 32); | |
6efe152d KKD |
600 | if (type & PTR_UNTRUSTED) |
601 | strncpy(prefix, "untrusted_", 32); | |
20b2aff4 HL |
602 | |
603 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", | |
604 | prefix, str[base_type(type)], postfix); | |
c25b2ae1 HL |
605 | return env->type_str_buf; |
606 | } | |
17a52670 | 607 | |
8efea21d EC |
608 | static char slot_type_char[] = { |
609 | [STACK_INVALID] = '?', | |
610 | [STACK_SPILL] = 'r', | |
611 | [STACK_MISC] = 'm', | |
612 | [STACK_ZERO] = '0', | |
97e03f52 | 613 | [STACK_DYNPTR] = 'd', |
8efea21d EC |
614 | }; |
615 | ||
4e92024a AS |
616 | static void print_liveness(struct bpf_verifier_env *env, |
617 | enum bpf_reg_liveness live) | |
618 | { | |
9242b5f5 | 619 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
620 | verbose(env, "_"); |
621 | if (live & REG_LIVE_READ) | |
622 | verbose(env, "r"); | |
623 | if (live & REG_LIVE_WRITTEN) | |
624 | verbose(env, "w"); | |
9242b5f5 AS |
625 | if (live & REG_LIVE_DONE) |
626 | verbose(env, "D"); | |
4e92024a AS |
627 | } |
628 | ||
97e03f52 JK |
629 | static int get_spi(s32 off) |
630 | { | |
631 | return (-off - 1) / BPF_REG_SIZE; | |
632 | } | |
633 | ||
634 | static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots) | |
635 | { | |
636 | int allocated_slots = state->allocated_stack / BPF_REG_SIZE; | |
637 | ||
638 | /* We need to check that slots between [spi - nr_slots + 1, spi] are | |
639 | * within [0, allocated_stack). | |
640 | * | |
641 | * Please note that the spi grows downwards. For example, a dynptr | |
642 | * takes the size of two stack slots; the first slot will be at | |
643 | * spi and the second slot will be at spi - 1. | |
644 | */ | |
645 | return spi - nr_slots + 1 >= 0 && spi < allocated_slots; | |
646 | } | |
647 | ||
f4d7e40a AS |
648 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
649 | const struct bpf_reg_state *reg) | |
650 | { | |
651 | struct bpf_verifier_state *cur = env->cur_state; | |
652 | ||
653 | return cur->frame[reg->frameno]; | |
654 | } | |
655 | ||
22dc4a0f | 656 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
9e15db66 | 657 | { |
22dc4a0f | 658 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
659 | } |
660 | ||
0f55f9ed CL |
661 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
662 | { | |
663 | env->scratched_regs |= 1U << regno; | |
664 | } | |
665 | ||
666 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
667 | { | |
343e5375 | 668 | env->scratched_stack_slots |= 1ULL << spi; |
0f55f9ed CL |
669 | } |
670 | ||
671 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
672 | { | |
673 | return (env->scratched_regs >> regno) & 1; | |
674 | } | |
675 | ||
676 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
677 | { | |
678 | return (env->scratched_stack_slots >> regno) & 1; | |
679 | } | |
680 | ||
681 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
682 | { | |
683 | return env->scratched_regs || env->scratched_stack_slots; | |
684 | } | |
685 | ||
686 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
687 | { | |
688 | env->scratched_regs = 0U; | |
343e5375 | 689 | env->scratched_stack_slots = 0ULL; |
0f55f9ed CL |
690 | } |
691 | ||
692 | /* Used for printing the entire verifier state. */ | |
693 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
694 | { | |
695 | env->scratched_regs = ~0U; | |
343e5375 | 696 | env->scratched_stack_slots = ~0ULL; |
0f55f9ed CL |
697 | } |
698 | ||
97e03f52 JK |
699 | static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type) |
700 | { | |
701 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
702 | case DYNPTR_TYPE_LOCAL: | |
703 | return BPF_DYNPTR_TYPE_LOCAL; | |
bc34dee6 JK |
704 | case DYNPTR_TYPE_RINGBUF: |
705 | return BPF_DYNPTR_TYPE_RINGBUF; | |
97e03f52 JK |
706 | default: |
707 | return BPF_DYNPTR_TYPE_INVALID; | |
708 | } | |
709 | } | |
710 | ||
bc34dee6 JK |
711 | static bool dynptr_type_refcounted(enum bpf_dynptr_type type) |
712 | { | |
713 | return type == BPF_DYNPTR_TYPE_RINGBUF; | |
714 | } | |
715 | ||
97e03f52 JK |
716 | static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
717 | enum bpf_arg_type arg_type, int insn_idx) | |
718 | { | |
719 | struct bpf_func_state *state = func(env, reg); | |
720 | enum bpf_dynptr_type type; | |
bc34dee6 | 721 | int spi, i, id; |
97e03f52 JK |
722 | |
723 | spi = get_spi(reg->off); | |
724 | ||
725 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
726 | return -EINVAL; | |
727 | ||
728 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
729 | state->stack[spi].slot_type[i] = STACK_DYNPTR; | |
730 | state->stack[spi - 1].slot_type[i] = STACK_DYNPTR; | |
731 | } | |
732 | ||
733 | type = arg_to_dynptr_type(arg_type); | |
734 | if (type == BPF_DYNPTR_TYPE_INVALID) | |
735 | return -EINVAL; | |
736 | ||
737 | state->stack[spi].spilled_ptr.dynptr.first_slot = true; | |
738 | state->stack[spi].spilled_ptr.dynptr.type = type; | |
739 | state->stack[spi - 1].spilled_ptr.dynptr.type = type; | |
740 | ||
bc34dee6 JK |
741 | if (dynptr_type_refcounted(type)) { |
742 | /* The id is used to track proper releasing */ | |
743 | id = acquire_reference_state(env, insn_idx); | |
744 | if (id < 0) | |
745 | return id; | |
746 | ||
747 | state->stack[spi].spilled_ptr.id = id; | |
748 | state->stack[spi - 1].spilled_ptr.id = id; | |
749 | } | |
750 | ||
97e03f52 JK |
751 | return 0; |
752 | } | |
753 | ||
754 | static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
755 | { | |
756 | struct bpf_func_state *state = func(env, reg); | |
757 | int spi, i; | |
758 | ||
759 | spi = get_spi(reg->off); | |
760 | ||
761 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
762 | return -EINVAL; | |
763 | ||
764 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
765 | state->stack[spi].slot_type[i] = STACK_INVALID; | |
766 | state->stack[spi - 1].slot_type[i] = STACK_INVALID; | |
767 | } | |
768 | ||
bc34dee6 JK |
769 | /* Invalidate any slices associated with this dynptr */ |
770 | if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) { | |
771 | release_reference(env, state->stack[spi].spilled_ptr.id); | |
772 | state->stack[spi].spilled_ptr.id = 0; | |
773 | state->stack[spi - 1].spilled_ptr.id = 0; | |
774 | } | |
775 | ||
97e03f52 JK |
776 | state->stack[spi].spilled_ptr.dynptr.first_slot = false; |
777 | state->stack[spi].spilled_ptr.dynptr.type = 0; | |
778 | state->stack[spi - 1].spilled_ptr.dynptr.type = 0; | |
779 | ||
780 | return 0; | |
781 | } | |
782 | ||
783 | static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg) | |
784 | { | |
785 | struct bpf_func_state *state = func(env, reg); | |
786 | int spi = get_spi(reg->off); | |
787 | int i; | |
788 | ||
789 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS)) | |
790 | return true; | |
791 | ||
792 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
793 | if (state->stack[spi].slot_type[i] == STACK_DYNPTR || | |
794 | state->stack[spi - 1].slot_type[i] == STACK_DYNPTR) | |
795 | return false; | |
796 | } | |
797 | ||
798 | return true; | |
799 | } | |
800 | ||
b8d31762 RS |
801 | bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, |
802 | struct bpf_reg_state *reg) | |
97e03f52 JK |
803 | { |
804 | struct bpf_func_state *state = func(env, reg); | |
805 | int spi = get_spi(reg->off); | |
806 | int i; | |
807 | ||
808 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS) || | |
809 | !state->stack[spi].spilled_ptr.dynptr.first_slot) | |
810 | return false; | |
811 | ||
812 | for (i = 0; i < BPF_REG_SIZE; i++) { | |
813 | if (state->stack[spi].slot_type[i] != STACK_DYNPTR || | |
814 | state->stack[spi - 1].slot_type[i] != STACK_DYNPTR) | |
815 | return false; | |
816 | } | |
817 | ||
e9e315b4 RS |
818 | return true; |
819 | } | |
820 | ||
b8d31762 RS |
821 | bool is_dynptr_type_expected(struct bpf_verifier_env *env, |
822 | struct bpf_reg_state *reg, | |
823 | enum bpf_arg_type arg_type) | |
e9e315b4 RS |
824 | { |
825 | struct bpf_func_state *state = func(env, reg); | |
826 | enum bpf_dynptr_type dynptr_type; | |
827 | int spi = get_spi(reg->off); | |
828 | ||
97e03f52 JK |
829 | /* ARG_PTR_TO_DYNPTR takes any type of dynptr */ |
830 | if (arg_type == ARG_PTR_TO_DYNPTR) | |
831 | return true; | |
832 | ||
e9e315b4 RS |
833 | dynptr_type = arg_to_dynptr_type(arg_type); |
834 | ||
835 | return state->stack[spi].spilled_ptr.dynptr.type == dynptr_type; | |
97e03f52 JK |
836 | } |
837 | ||
27113c59 MKL |
838 | /* The reg state of a pointer or a bounded scalar was saved when |
839 | * it was spilled to the stack. | |
840 | */ | |
841 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
842 | { | |
843 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
844 | } | |
845 | ||
354e8f19 MKL |
846 | static void scrub_spilled_slot(u8 *stype) |
847 | { | |
848 | if (*stype != STACK_INVALID) | |
849 | *stype = STACK_MISC; | |
850 | } | |
851 | ||
61bd5218 | 852 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
853 | const struct bpf_func_state *state, |
854 | bool print_all) | |
17a52670 | 855 | { |
f4d7e40a | 856 | const struct bpf_reg_state *reg; |
17a52670 AS |
857 | enum bpf_reg_type t; |
858 | int i; | |
859 | ||
f4d7e40a AS |
860 | if (state->frameno) |
861 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 862 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
863 | reg = &state->regs[i]; |
864 | t = reg->type; | |
17a52670 AS |
865 | if (t == NOT_INIT) |
866 | continue; | |
0f55f9ed CL |
867 | if (!print_all && !reg_scratched(env, i)) |
868 | continue; | |
4e92024a AS |
869 | verbose(env, " R%d", i); |
870 | print_liveness(env, reg->live); | |
7df5072c | 871 | verbose(env, "="); |
b5dc0163 AS |
872 | if (t == SCALAR_VALUE && reg->precise) |
873 | verbose(env, "P"); | |
f1174f77 EC |
874 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
875 | tnum_is_const(reg->var_off)) { | |
876 | /* reg->off should be 0 for SCALAR_VALUE */ | |
7df5072c | 877 | verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
61bd5218 | 878 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 879 | } else { |
7df5072c ML |
880 | const char *sep = ""; |
881 | ||
882 | verbose(env, "%s", reg_type_str(env, t)); | |
5844101a | 883 | if (base_type(t) == PTR_TO_BTF_ID) |
22dc4a0f | 884 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
7df5072c ML |
885 | verbose(env, "("); |
886 | /* | |
887 | * _a stands for append, was shortened to avoid multiline statements below. | |
888 | * This macro is used to output a comma separated list of attributes. | |
889 | */ | |
890 | #define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) | |
891 | ||
892 | if (reg->id) | |
893 | verbose_a("id=%d", reg->id); | |
a28ace78 | 894 | if (reg->ref_obj_id) |
7df5072c | 895 | verbose_a("ref_obj_id=%d", reg->ref_obj_id); |
f1174f77 | 896 | if (t != SCALAR_VALUE) |
7df5072c | 897 | verbose_a("off=%d", reg->off); |
de8f3a83 | 898 | if (type_is_pkt_pointer(t)) |
7df5072c | 899 | verbose_a("r=%d", reg->range); |
c25b2ae1 HL |
900 | else if (base_type(t) == CONST_PTR_TO_MAP || |
901 | base_type(t) == PTR_TO_MAP_KEY || | |
902 | base_type(t) == PTR_TO_MAP_VALUE) | |
7df5072c ML |
903 | verbose_a("ks=%d,vs=%d", |
904 | reg->map_ptr->key_size, | |
905 | reg->map_ptr->value_size); | |
7d1238f2 EC |
906 | if (tnum_is_const(reg->var_off)) { |
907 | /* Typically an immediate SCALAR_VALUE, but | |
908 | * could be a pointer whose offset is too big | |
909 | * for reg->off | |
910 | */ | |
7df5072c | 911 | verbose_a("imm=%llx", reg->var_off.value); |
7d1238f2 EC |
912 | } else { |
913 | if (reg->smin_value != reg->umin_value && | |
914 | reg->smin_value != S64_MIN) | |
7df5072c | 915 | verbose_a("smin=%lld", (long long)reg->smin_value); |
7d1238f2 EC |
916 | if (reg->smax_value != reg->umax_value && |
917 | reg->smax_value != S64_MAX) | |
7df5072c | 918 | verbose_a("smax=%lld", (long long)reg->smax_value); |
7d1238f2 | 919 | if (reg->umin_value != 0) |
7df5072c | 920 | verbose_a("umin=%llu", (unsigned long long)reg->umin_value); |
7d1238f2 | 921 | if (reg->umax_value != U64_MAX) |
7df5072c | 922 | verbose_a("umax=%llu", (unsigned long long)reg->umax_value); |
7d1238f2 EC |
923 | if (!tnum_is_unknown(reg->var_off)) { |
924 | char tn_buf[48]; | |
f1174f77 | 925 | |
7d1238f2 | 926 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
7df5072c | 927 | verbose_a("var_off=%s", tn_buf); |
7d1238f2 | 928 | } |
3f50f132 JF |
929 | if (reg->s32_min_value != reg->smin_value && |
930 | reg->s32_min_value != S32_MIN) | |
7df5072c | 931 | verbose_a("s32_min=%d", (int)(reg->s32_min_value)); |
3f50f132 JF |
932 | if (reg->s32_max_value != reg->smax_value && |
933 | reg->s32_max_value != S32_MAX) | |
7df5072c | 934 | verbose_a("s32_max=%d", (int)(reg->s32_max_value)); |
3f50f132 JF |
935 | if (reg->u32_min_value != reg->umin_value && |
936 | reg->u32_min_value != U32_MIN) | |
7df5072c | 937 | verbose_a("u32_min=%d", (int)(reg->u32_min_value)); |
3f50f132 JF |
938 | if (reg->u32_max_value != reg->umax_value && |
939 | reg->u32_max_value != U32_MAX) | |
7df5072c | 940 | verbose_a("u32_max=%d", (int)(reg->u32_max_value)); |
f1174f77 | 941 | } |
7df5072c ML |
942 | #undef verbose_a |
943 | ||
61bd5218 | 944 | verbose(env, ")"); |
f1174f77 | 945 | } |
17a52670 | 946 | } |
638f5b90 | 947 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
948 | char types_buf[BPF_REG_SIZE + 1]; |
949 | bool valid = false; | |
950 | int j; | |
951 | ||
952 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
953 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
954 | valid = true; | |
955 | types_buf[j] = slot_type_char[ | |
956 | state->stack[i].slot_type[j]]; | |
957 | } | |
958 | types_buf[BPF_REG_SIZE] = 0; | |
959 | if (!valid) | |
960 | continue; | |
0f55f9ed CL |
961 | if (!print_all && !stack_slot_scratched(env, i)) |
962 | continue; | |
8efea21d EC |
963 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
964 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
27113c59 | 965 | if (is_spilled_reg(&state->stack[i])) { |
b5dc0163 AS |
966 | reg = &state->stack[i].spilled_ptr; |
967 | t = reg->type; | |
7df5072c | 968 | verbose(env, "=%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
b5dc0163 AS |
969 | if (t == SCALAR_VALUE && reg->precise) |
970 | verbose(env, "P"); | |
971 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
972 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
973 | } else { | |
8efea21d | 974 | verbose(env, "=%s", types_buf); |
b5dc0163 | 975 | } |
17a52670 | 976 | } |
fd978bf7 JS |
977 | if (state->acquired_refs && state->refs[0].id) { |
978 | verbose(env, " refs=%d", state->refs[0].id); | |
979 | for (i = 1; i < state->acquired_refs; i++) | |
980 | if (state->refs[i].id) | |
981 | verbose(env, ",%d", state->refs[i].id); | |
982 | } | |
bfc6bb74 AS |
983 | if (state->in_callback_fn) |
984 | verbose(env, " cb"); | |
985 | if (state->in_async_callback_fn) | |
986 | verbose(env, " async_cb"); | |
61bd5218 | 987 | verbose(env, "\n"); |
0f55f9ed | 988 | mark_verifier_state_clean(env); |
17a52670 AS |
989 | } |
990 | ||
2e576648 CL |
991 | static inline u32 vlog_alignment(u32 pos) |
992 | { | |
993 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
994 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
995 | } | |
996 | ||
997 | static void print_insn_state(struct bpf_verifier_env *env, | |
998 | const struct bpf_func_state *state) | |
999 | { | |
1000 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { | |
1001 | /* remove new line character */ | |
1002 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); | |
1003 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); | |
1004 | } else { | |
1005 | verbose(env, "%d:", env->insn_idx); | |
1006 | } | |
1007 | print_verifier_state(env, state, false); | |
17a52670 AS |
1008 | } |
1009 | ||
c69431aa LB |
1010 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
1011 | * small to hold src. This is different from krealloc since we don't want to preserve | |
1012 | * the contents of dst. | |
1013 | * | |
1014 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
1015 | * not be allocated. | |
638f5b90 | 1016 | */ |
c69431aa | 1017 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 1018 | { |
c69431aa LB |
1019 | size_t bytes; |
1020 | ||
1021 | if (ZERO_OR_NULL_PTR(src)) | |
1022 | goto out; | |
1023 | ||
1024 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
1025 | return NULL; | |
1026 | ||
1027 | if (ksize(dst) < bytes) { | |
1028 | kfree(dst); | |
1029 | dst = kmalloc_track_caller(bytes, flags); | |
1030 | if (!dst) | |
1031 | return NULL; | |
1032 | } | |
1033 | ||
1034 | memcpy(dst, src, bytes); | |
1035 | out: | |
1036 | return dst ? dst : ZERO_SIZE_PTR; | |
1037 | } | |
1038 | ||
1039 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
1040 | * small to hold new_n items. new items are zeroed out if the array grows. | |
1041 | * | |
1042 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
1043 | */ | |
1044 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
1045 | { | |
42378a9c KC |
1046 | void *new_arr; |
1047 | ||
c69431aa LB |
1048 | if (!new_n || old_n == new_n) |
1049 | goto out; | |
1050 | ||
42378a9c KC |
1051 | new_arr = krealloc_array(arr, new_n, size, GFP_KERNEL); |
1052 | if (!new_arr) { | |
1053 | kfree(arr); | |
c69431aa | 1054 | return NULL; |
42378a9c KC |
1055 | } |
1056 | arr = new_arr; | |
c69431aa LB |
1057 | |
1058 | if (new_n > old_n) | |
1059 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
1060 | ||
1061 | out: | |
1062 | return arr ? arr : ZERO_SIZE_PTR; | |
1063 | } | |
1064 | ||
1065 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1066 | { | |
1067 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
1068 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
1069 | if (!dst->refs) | |
1070 | return -ENOMEM; | |
1071 | ||
1072 | dst->acquired_refs = src->acquired_refs; | |
1073 | return 0; | |
1074 | } | |
1075 | ||
1076 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
1077 | { | |
1078 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
1079 | ||
1080 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
1081 | GFP_KERNEL); | |
1082 | if (!dst->stack) | |
1083 | return -ENOMEM; | |
1084 | ||
1085 | dst->allocated_stack = src->allocated_stack; | |
1086 | return 0; | |
1087 | } | |
1088 | ||
1089 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
1090 | { | |
1091 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
1092 | sizeof(struct bpf_reference_state)); | |
1093 | if (!state->refs) | |
1094 | return -ENOMEM; | |
1095 | ||
1096 | state->acquired_refs = n; | |
1097 | return 0; | |
1098 | } | |
1099 | ||
1100 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
1101 | { | |
1102 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
1103 | ||
1104 | if (old_n >= n) | |
1105 | return 0; | |
1106 | ||
1107 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
1108 | if (!state->stack) | |
1109 | return -ENOMEM; | |
1110 | ||
1111 | state->allocated_stack = size; | |
1112 | return 0; | |
fd978bf7 JS |
1113 | } |
1114 | ||
1115 | /* Acquire a pointer id from the env and update the state->refs to include | |
1116 | * this new pointer reference. | |
1117 | * On success, returns a valid pointer id to associate with the register | |
1118 | * On failure, returns a negative errno. | |
638f5b90 | 1119 | */ |
fd978bf7 | 1120 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 1121 | { |
fd978bf7 JS |
1122 | struct bpf_func_state *state = cur_func(env); |
1123 | int new_ofs = state->acquired_refs; | |
1124 | int id, err; | |
1125 | ||
c69431aa | 1126 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
1127 | if (err) |
1128 | return err; | |
1129 | id = ++env->id_gen; | |
1130 | state->refs[new_ofs].id = id; | |
1131 | state->refs[new_ofs].insn_idx = insn_idx; | |
9d9d00ac | 1132 | state->refs[new_ofs].callback_ref = state->in_callback_fn ? state->frameno : 0; |
638f5b90 | 1133 | |
fd978bf7 JS |
1134 | return id; |
1135 | } | |
1136 | ||
1137 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 1138 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
1139 | { |
1140 | int i, last_idx; | |
1141 | ||
fd978bf7 JS |
1142 | last_idx = state->acquired_refs - 1; |
1143 | for (i = 0; i < state->acquired_refs; i++) { | |
1144 | if (state->refs[i].id == ptr_id) { | |
9d9d00ac KKD |
1145 | /* Cannot release caller references in callbacks */ |
1146 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) | |
1147 | return -EINVAL; | |
fd978bf7 JS |
1148 | if (last_idx && i != last_idx) |
1149 | memcpy(&state->refs[i], &state->refs[last_idx], | |
1150 | sizeof(*state->refs)); | |
1151 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
1152 | state->acquired_refs--; | |
638f5b90 | 1153 | return 0; |
638f5b90 | 1154 | } |
638f5b90 | 1155 | } |
46f8bc92 | 1156 | return -EINVAL; |
fd978bf7 JS |
1157 | } |
1158 | ||
f4d7e40a AS |
1159 | static void free_func_state(struct bpf_func_state *state) |
1160 | { | |
5896351e AS |
1161 | if (!state) |
1162 | return; | |
fd978bf7 | 1163 | kfree(state->refs); |
f4d7e40a AS |
1164 | kfree(state->stack); |
1165 | kfree(state); | |
1166 | } | |
1167 | ||
b5dc0163 AS |
1168 | static void clear_jmp_history(struct bpf_verifier_state *state) |
1169 | { | |
1170 | kfree(state->jmp_history); | |
1171 | state->jmp_history = NULL; | |
1172 | state->jmp_history_cnt = 0; | |
1173 | } | |
1174 | ||
1969db47 AS |
1175 | static void free_verifier_state(struct bpf_verifier_state *state, |
1176 | bool free_self) | |
638f5b90 | 1177 | { |
f4d7e40a AS |
1178 | int i; |
1179 | ||
1180 | for (i = 0; i <= state->curframe; i++) { | |
1181 | free_func_state(state->frame[i]); | |
1182 | state->frame[i] = NULL; | |
1183 | } | |
b5dc0163 | 1184 | clear_jmp_history(state); |
1969db47 AS |
1185 | if (free_self) |
1186 | kfree(state); | |
638f5b90 AS |
1187 | } |
1188 | ||
1189 | /* copy verifier state from src to dst growing dst stack space | |
1190 | * when necessary to accommodate larger src stack | |
1191 | */ | |
f4d7e40a AS |
1192 | static int copy_func_state(struct bpf_func_state *dst, |
1193 | const struct bpf_func_state *src) | |
638f5b90 AS |
1194 | { |
1195 | int err; | |
1196 | ||
fd978bf7 JS |
1197 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
1198 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
1199 | if (err) |
1200 | return err; | |
638f5b90 AS |
1201 | return copy_stack_state(dst, src); |
1202 | } | |
1203 | ||
f4d7e40a AS |
1204 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1205 | const struct bpf_verifier_state *src) | |
1206 | { | |
1207 | struct bpf_func_state *dst; | |
1208 | int i, err; | |
1209 | ||
06ab6a50 LB |
1210 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1211 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1212 | GFP_USER); | |
1213 | if (!dst_state->jmp_history) | |
1214 | return -ENOMEM; | |
b5dc0163 AS |
1215 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1216 | ||
f4d7e40a AS |
1217 | /* if dst has more stack frames then src frame, free them */ |
1218 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1219 | free_func_state(dst_state->frame[i]); | |
1220 | dst_state->frame[i] = NULL; | |
1221 | } | |
979d63d5 | 1222 | dst_state->speculative = src->speculative; |
f4d7e40a | 1223 | dst_state->curframe = src->curframe; |
d0d78c1d KKD |
1224 | dst_state->active_lock.ptr = src->active_lock.ptr; |
1225 | dst_state->active_lock.id = src->active_lock.id; | |
2589726d AS |
1226 | dst_state->branches = src->branches; |
1227 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1228 | dst_state->first_insn_idx = src->first_insn_idx; |
1229 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1230 | for (i = 0; i <= src->curframe; i++) { |
1231 | dst = dst_state->frame[i]; | |
1232 | if (!dst) { | |
1233 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1234 | if (!dst) | |
1235 | return -ENOMEM; | |
1236 | dst_state->frame[i] = dst; | |
1237 | } | |
1238 | err = copy_func_state(dst, src->frame[i]); | |
1239 | if (err) | |
1240 | return err; | |
1241 | } | |
1242 | return 0; | |
1243 | } | |
1244 | ||
2589726d AS |
1245 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1246 | { | |
1247 | while (st) { | |
1248 | u32 br = --st->branches; | |
1249 | ||
1250 | /* WARN_ON(br > 1) technically makes sense here, | |
1251 | * but see comment in push_stack(), hence: | |
1252 | */ | |
1253 | WARN_ONCE((int)br < 0, | |
1254 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1255 | br); | |
1256 | if (br) | |
1257 | break; | |
1258 | st = st->parent; | |
1259 | } | |
1260 | } | |
1261 | ||
638f5b90 | 1262 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1263 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1264 | { |
1265 | struct bpf_verifier_state *cur = env->cur_state; | |
1266 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1267 | int err; | |
17a52670 AS |
1268 | |
1269 | if (env->head == NULL) | |
638f5b90 | 1270 | return -ENOENT; |
17a52670 | 1271 | |
638f5b90 AS |
1272 | if (cur) { |
1273 | err = copy_verifier_state(cur, &head->st); | |
1274 | if (err) | |
1275 | return err; | |
1276 | } | |
6f8a57cc AN |
1277 | if (pop_log) |
1278 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1279 | if (insn_idx) |
1280 | *insn_idx = head->insn_idx; | |
17a52670 | 1281 | if (prev_insn_idx) |
638f5b90 AS |
1282 | *prev_insn_idx = head->prev_insn_idx; |
1283 | elem = head->next; | |
1969db47 | 1284 | free_verifier_state(&head->st, false); |
638f5b90 | 1285 | kfree(head); |
17a52670 AS |
1286 | env->head = elem; |
1287 | env->stack_size--; | |
638f5b90 | 1288 | return 0; |
17a52670 AS |
1289 | } |
1290 | ||
58e2af8b | 1291 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1292 | int insn_idx, int prev_insn_idx, |
1293 | bool speculative) | |
17a52670 | 1294 | { |
638f5b90 | 1295 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1296 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1297 | int err; |
17a52670 | 1298 | |
638f5b90 | 1299 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1300 | if (!elem) |
1301 | goto err; | |
1302 | ||
17a52670 AS |
1303 | elem->insn_idx = insn_idx; |
1304 | elem->prev_insn_idx = prev_insn_idx; | |
1305 | elem->next = env->head; | |
6f8a57cc | 1306 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1307 | env->head = elem; |
1308 | env->stack_size++; | |
1969db47 AS |
1309 | err = copy_verifier_state(&elem->st, cur); |
1310 | if (err) | |
1311 | goto err; | |
979d63d5 | 1312 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1313 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1314 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1315 | env->stack_size); | |
17a52670 AS |
1316 | goto err; |
1317 | } | |
2589726d AS |
1318 | if (elem->st.parent) { |
1319 | ++elem->st.parent->branches; | |
1320 | /* WARN_ON(branches > 2) technically makes sense here, | |
1321 | * but | |
1322 | * 1. speculative states will bump 'branches' for non-branch | |
1323 | * instructions | |
1324 | * 2. is_state_visited() heuristics may decide not to create | |
1325 | * a new state for a sequence of branches and all such current | |
1326 | * and cloned states will be pointing to a single parent state | |
1327 | * which might have large 'branches' count. | |
1328 | */ | |
1329 | } | |
17a52670 AS |
1330 | return &elem->st; |
1331 | err: | |
5896351e AS |
1332 | free_verifier_state(env->cur_state, true); |
1333 | env->cur_state = NULL; | |
17a52670 | 1334 | /* pop all elements and return */ |
6f8a57cc | 1335 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1336 | return NULL; |
1337 | } | |
1338 | ||
1339 | #define CALLER_SAVED_REGS 6 | |
1340 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1341 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1342 | }; | |
1343 | ||
f54c7898 DB |
1344 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1345 | struct bpf_reg_state *reg); | |
f1174f77 | 1346 | |
e688c3db AS |
1347 | /* This helper doesn't clear reg->id */ |
1348 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1349 | { |
b03c9f9f EC |
1350 | reg->var_off = tnum_const(imm); |
1351 | reg->smin_value = (s64)imm; | |
1352 | reg->smax_value = (s64)imm; | |
1353 | reg->umin_value = imm; | |
1354 | reg->umax_value = imm; | |
3f50f132 JF |
1355 | |
1356 | reg->s32_min_value = (s32)imm; | |
1357 | reg->s32_max_value = (s32)imm; | |
1358 | reg->u32_min_value = (u32)imm; | |
1359 | reg->u32_max_value = (u32)imm; | |
1360 | } | |
1361 | ||
e688c3db AS |
1362 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1363 | * known to have the value @imm. | |
1364 | */ | |
1365 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1366 | { | |
1367 | /* Clear id, off, and union(map_ptr, range) */ | |
1368 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1369 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
1370 | ___mark_reg_known(reg, imm); | |
1371 | } | |
1372 | ||
3f50f132 JF |
1373 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1374 | { | |
1375 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1376 | reg->s32_min_value = (s32)imm; | |
1377 | reg->s32_max_value = (s32)imm; | |
1378 | reg->u32_min_value = (u32)imm; | |
1379 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1380 | } |
1381 | ||
f1174f77 EC |
1382 | /* Mark the 'variable offset' part of a register as zero. This should be |
1383 | * used only on registers holding a pointer type. | |
1384 | */ | |
1385 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1386 | { |
b03c9f9f | 1387 | __mark_reg_known(reg, 0); |
f1174f77 | 1388 | } |
a9789ef9 | 1389 | |
cc2b14d5 AS |
1390 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1391 | { | |
1392 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1393 | reg->type = SCALAR_VALUE; |
1394 | } | |
1395 | ||
61bd5218 JK |
1396 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1397 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1398 | { |
1399 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1400 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1401 | /* Something bad happened, let's kill all regs */ |
1402 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1403 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1404 | return; |
1405 | } | |
1406 | __mark_reg_known_zero(regs + regno); | |
1407 | } | |
1408 | ||
4ddb7416 DB |
1409 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1410 | { | |
c25b2ae1 | 1411 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
4ddb7416 DB |
1412 | const struct bpf_map *map = reg->map_ptr; |
1413 | ||
1414 | if (map->inner_map_meta) { | |
1415 | reg->type = CONST_PTR_TO_MAP; | |
1416 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1417 | /* transfer reg's id which is unique for every map_lookup_elem |
1418 | * as UID of the inner map. | |
1419 | */ | |
db559117 | 1420 | if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER)) |
34d11a44 | 1421 | reg->map_uid = reg->id; |
4ddb7416 DB |
1422 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1423 | reg->type = PTR_TO_XDP_SOCK; | |
1424 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1425 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1426 | reg->type = PTR_TO_SOCKET; | |
1427 | } else { | |
1428 | reg->type = PTR_TO_MAP_VALUE; | |
1429 | } | |
c25b2ae1 | 1430 | return; |
4ddb7416 | 1431 | } |
c25b2ae1 HL |
1432 | |
1433 | reg->type &= ~PTR_MAYBE_NULL; | |
4ddb7416 DB |
1434 | } |
1435 | ||
de8f3a83 DB |
1436 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1437 | { | |
1438 | return type_is_pkt_pointer(reg->type); | |
1439 | } | |
1440 | ||
1441 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1442 | { | |
1443 | return reg_is_pkt_pointer(reg) || | |
1444 | reg->type == PTR_TO_PACKET_END; | |
1445 | } | |
1446 | ||
1447 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1448 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1449 | enum bpf_reg_type which) | |
1450 | { | |
1451 | /* The register can already have a range from prior markings. | |
1452 | * This is fine as long as it hasn't been advanced from its | |
1453 | * origin. | |
1454 | */ | |
1455 | return reg->type == which && | |
1456 | reg->id == 0 && | |
1457 | reg->off == 0 && | |
1458 | tnum_equals_const(reg->var_off, 0); | |
1459 | } | |
1460 | ||
3f50f132 JF |
1461 | /* Reset the min/max bounds of a register */ |
1462 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1463 | { | |
1464 | reg->smin_value = S64_MIN; | |
1465 | reg->smax_value = S64_MAX; | |
1466 | reg->umin_value = 0; | |
1467 | reg->umax_value = U64_MAX; | |
1468 | ||
1469 | reg->s32_min_value = S32_MIN; | |
1470 | reg->s32_max_value = S32_MAX; | |
1471 | reg->u32_min_value = 0; | |
1472 | reg->u32_max_value = U32_MAX; | |
1473 | } | |
1474 | ||
1475 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1476 | { | |
1477 | reg->smin_value = S64_MIN; | |
1478 | reg->smax_value = S64_MAX; | |
1479 | reg->umin_value = 0; | |
1480 | reg->umax_value = U64_MAX; | |
1481 | } | |
1482 | ||
1483 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1484 | { | |
1485 | reg->s32_min_value = S32_MIN; | |
1486 | reg->s32_max_value = S32_MAX; | |
1487 | reg->u32_min_value = 0; | |
1488 | reg->u32_max_value = U32_MAX; | |
1489 | } | |
1490 | ||
1491 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1492 | { | |
1493 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1494 | ||
1495 | /* min signed is max(sign bit) | min(other bits) */ | |
1496 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1497 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1498 | /* max signed is min(sign bit) | max(other bits) */ | |
1499 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1500 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1501 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1502 | reg->u32_max_value = min(reg->u32_max_value, | |
1503 | (u32)(var32_off.value | var32_off.mask)); | |
1504 | } | |
1505 | ||
1506 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1507 | { |
1508 | /* min signed is max(sign bit) | min(other bits) */ | |
1509 | reg->smin_value = max_t(s64, reg->smin_value, | |
1510 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1511 | /* max signed is min(sign bit) | max(other bits) */ | |
1512 | reg->smax_value = min_t(s64, reg->smax_value, | |
1513 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1514 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1515 | reg->umax_value = min(reg->umax_value, | |
1516 | reg->var_off.value | reg->var_off.mask); | |
1517 | } | |
1518 | ||
3f50f132 JF |
1519 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1520 | { | |
1521 | __update_reg32_bounds(reg); | |
1522 | __update_reg64_bounds(reg); | |
1523 | } | |
1524 | ||
b03c9f9f | 1525 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1526 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1527 | { | |
1528 | /* Learn sign from signed bounds. | |
1529 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1530 | * are the same, so combine. This works even in the negative case, e.g. | |
1531 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1532 | */ | |
1533 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1534 | reg->s32_min_value = reg->u32_min_value = | |
1535 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1536 | reg->s32_max_value = reg->u32_max_value = | |
1537 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1538 | return; | |
1539 | } | |
1540 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1541 | * boundary, so we must be careful. | |
1542 | */ | |
1543 | if ((s32)reg->u32_max_value >= 0) { | |
1544 | /* Positive. We can't learn anything from the smin, but smax | |
1545 | * is positive, hence safe. | |
1546 | */ | |
1547 | reg->s32_min_value = reg->u32_min_value; | |
1548 | reg->s32_max_value = reg->u32_max_value = | |
1549 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1550 | } else if ((s32)reg->u32_min_value < 0) { | |
1551 | /* Negative. We can't learn anything from the smax, but smin | |
1552 | * is negative, hence safe. | |
1553 | */ | |
1554 | reg->s32_min_value = reg->u32_min_value = | |
1555 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1556 | reg->s32_max_value = reg->u32_max_value; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1561 | { |
1562 | /* Learn sign from signed bounds. | |
1563 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1564 | * are the same, so combine. This works even in the negative case, e.g. | |
1565 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1566 | */ | |
1567 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1568 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1569 | reg->umin_value); | |
1570 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1571 | reg->umax_value); | |
1572 | return; | |
1573 | } | |
1574 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1575 | * boundary, so we must be careful. | |
1576 | */ | |
1577 | if ((s64)reg->umax_value >= 0) { | |
1578 | /* Positive. We can't learn anything from the smin, but smax | |
1579 | * is positive, hence safe. | |
1580 | */ | |
1581 | reg->smin_value = reg->umin_value; | |
1582 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1583 | reg->umax_value); | |
1584 | } else if ((s64)reg->umin_value < 0) { | |
1585 | /* Negative. We can't learn anything from the smax, but smin | |
1586 | * is negative, hence safe. | |
1587 | */ | |
1588 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1589 | reg->umin_value); | |
1590 | reg->smax_value = reg->umax_value; | |
1591 | } | |
1592 | } | |
1593 | ||
3f50f132 JF |
1594 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1595 | { | |
1596 | __reg32_deduce_bounds(reg); | |
1597 | __reg64_deduce_bounds(reg); | |
1598 | } | |
1599 | ||
b03c9f9f EC |
1600 | /* Attempts to improve var_off based on unsigned min/max information */ |
1601 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1602 | { | |
3f50f132 JF |
1603 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1604 | tnum_range(reg->umin_value, | |
1605 | reg->umax_value)); | |
1606 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1607 | tnum_range(reg->u32_min_value, | |
1608 | reg->u32_max_value)); | |
1609 | ||
1610 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1611 | } |
1612 | ||
3844d153 DB |
1613 | static void reg_bounds_sync(struct bpf_reg_state *reg) |
1614 | { | |
1615 | /* We might have learned new bounds from the var_off. */ | |
1616 | __update_reg_bounds(reg); | |
1617 | /* We might have learned something about the sign bit. */ | |
1618 | __reg_deduce_bounds(reg); | |
1619 | /* We might have learned some bits from the bounds. */ | |
1620 | __reg_bound_offset(reg); | |
1621 | /* Intersecting with the old var_off might have improved our bounds | |
1622 | * slightly, e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1623 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1624 | */ | |
1625 | __update_reg_bounds(reg); | |
1626 | } | |
1627 | ||
e572ff80 DB |
1628 | static bool __reg32_bound_s64(s32 a) |
1629 | { | |
1630 | return a >= 0 && a <= S32_MAX; | |
1631 | } | |
1632 | ||
3f50f132 | 1633 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1634 | { |
3f50f132 JF |
1635 | reg->umin_value = reg->u32_min_value; |
1636 | reg->umax_value = reg->u32_max_value; | |
e572ff80 DB |
1637 | |
1638 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must | |
1639 | * be positive otherwise set to worse case bounds and refine later | |
1640 | * from tnum. | |
3f50f132 | 1641 | */ |
e572ff80 DB |
1642 | if (__reg32_bound_s64(reg->s32_min_value) && |
1643 | __reg32_bound_s64(reg->s32_max_value)) { | |
3a71dc36 | 1644 | reg->smin_value = reg->s32_min_value; |
e572ff80 DB |
1645 | reg->smax_value = reg->s32_max_value; |
1646 | } else { | |
3a71dc36 | 1647 | reg->smin_value = 0; |
e572ff80 DB |
1648 | reg->smax_value = U32_MAX; |
1649 | } | |
3f50f132 JF |
1650 | } |
1651 | ||
1652 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1653 | { | |
1654 | /* special case when 64-bit register has upper 32-bit register | |
1655 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1656 | * allowing us to use 32-bit bounds directly, | |
1657 | */ | |
1658 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1659 | __reg_assign_32_into_64(reg); | |
1660 | } else { | |
1661 | /* Otherwise the best we can do is push lower 32bit known and | |
1662 | * unknown bits into register (var_off set from jmp logic) | |
1663 | * then learn as much as possible from the 64-bit tnum | |
1664 | * known and unknown bits. The previous smin/smax bounds are | |
1665 | * invalid here because of jmp32 compare so mark them unknown | |
1666 | * so they do not impact tnum bounds calculation. | |
1667 | */ | |
1668 | __mark_reg64_unbounded(reg); | |
3f50f132 | 1669 | } |
3844d153 | 1670 | reg_bounds_sync(reg); |
3f50f132 JF |
1671 | } |
1672 | ||
1673 | static bool __reg64_bound_s32(s64 a) | |
1674 | { | |
388e2c0b | 1675 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
1676 | } |
1677 | ||
1678 | static bool __reg64_bound_u32(u64 a) | |
1679 | { | |
b9979db8 | 1680 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
1681 | } |
1682 | ||
1683 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1684 | { | |
1685 | __mark_reg32_unbounded(reg); | |
b0270958 | 1686 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 1687 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 1688 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 1689 | } |
10bf4e83 | 1690 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 1691 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 1692 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 1693 | } |
3844d153 | 1694 | reg_bounds_sync(reg); |
b03c9f9f EC |
1695 | } |
1696 | ||
f1174f77 | 1697 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1698 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1699 | struct bpf_reg_state *reg) | |
f1174f77 | 1700 | { |
a9c676bc AS |
1701 | /* |
1702 | * Clear type, id, off, and union(map_ptr, range) and | |
1703 | * padding between 'type' and union | |
1704 | */ | |
1705 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1706 | reg->type = SCALAR_VALUE; |
f1174f77 | 1707 | reg->var_off = tnum_unknown; |
f4d7e40a | 1708 | reg->frameno = 0; |
be2ef816 | 1709 | reg->precise = !env->bpf_capable; |
b03c9f9f | 1710 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1711 | } |
1712 | ||
61bd5218 JK |
1713 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1714 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1715 | { |
1716 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1717 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1718 | /* Something bad happened, let's kill all regs except FP */ |
1719 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1720 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1721 | return; |
1722 | } | |
f54c7898 | 1723 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1724 | } |
1725 | ||
f54c7898 DB |
1726 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1727 | struct bpf_reg_state *reg) | |
f1174f77 | 1728 | { |
f54c7898 | 1729 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1730 | reg->type = NOT_INIT; |
1731 | } | |
1732 | ||
61bd5218 JK |
1733 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1734 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1735 | { |
1736 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1737 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1738 | /* Something bad happened, let's kill all regs except FP */ |
1739 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1740 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1741 | return; |
1742 | } | |
f54c7898 | 1743 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1744 | } |
1745 | ||
41c48f3a AI |
1746 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1747 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f | 1748 | enum bpf_reg_type reg_type, |
c6f1bfe8 YS |
1749 | struct btf *btf, u32 btf_id, |
1750 | enum bpf_type_flag flag) | |
41c48f3a AI |
1751 | { |
1752 | if (reg_type == SCALAR_VALUE) { | |
1753 | mark_reg_unknown(env, regs, regno); | |
1754 | return; | |
1755 | } | |
1756 | mark_reg_known_zero(env, regs, regno); | |
c6f1bfe8 | 1757 | regs[regno].type = PTR_TO_BTF_ID | flag; |
22dc4a0f | 1758 | regs[regno].btf = btf; |
41c48f3a AI |
1759 | regs[regno].btf_id = btf_id; |
1760 | } | |
1761 | ||
5327ed3d | 1762 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1763 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1764 | struct bpf_func_state *state) |
17a52670 | 1765 | { |
f4d7e40a | 1766 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1767 | int i; |
1768 | ||
dc503a8a | 1769 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1770 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1771 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1772 | regs[i].parent = NULL; |
5327ed3d | 1773 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1774 | } |
17a52670 AS |
1775 | |
1776 | /* frame pointer */ | |
f1174f77 | 1777 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1778 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1779 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1780 | } |
1781 | ||
f4d7e40a AS |
1782 | #define BPF_MAIN_FUNC (-1) |
1783 | static void init_func_state(struct bpf_verifier_env *env, | |
1784 | struct bpf_func_state *state, | |
1785 | int callsite, int frameno, int subprogno) | |
1786 | { | |
1787 | state->callsite = callsite; | |
1788 | state->frameno = frameno; | |
1789 | state->subprogno = subprogno; | |
1bfe26fb | 1790 | state->callback_ret_range = tnum_range(0, 0); |
f4d7e40a | 1791 | init_reg_state(env, state); |
0f55f9ed | 1792 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
1793 | } |
1794 | ||
bfc6bb74 AS |
1795 | /* Similar to push_stack(), but for async callbacks */ |
1796 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
1797 | int insn_idx, int prev_insn_idx, | |
1798 | int subprog) | |
1799 | { | |
1800 | struct bpf_verifier_stack_elem *elem; | |
1801 | struct bpf_func_state *frame; | |
1802 | ||
1803 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
1804 | if (!elem) | |
1805 | goto err; | |
1806 | ||
1807 | elem->insn_idx = insn_idx; | |
1808 | elem->prev_insn_idx = prev_insn_idx; | |
1809 | elem->next = env->head; | |
1810 | elem->log_pos = env->log.len_used; | |
1811 | env->head = elem; | |
1812 | env->stack_size++; | |
1813 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
1814 | verbose(env, | |
1815 | "The sequence of %d jumps is too complex for async cb.\n", | |
1816 | env->stack_size); | |
1817 | goto err; | |
1818 | } | |
1819 | /* Unlike push_stack() do not copy_verifier_state(). | |
1820 | * The caller state doesn't matter. | |
1821 | * This is async callback. It starts in a fresh stack. | |
1822 | * Initialize it similar to do_check_common(). | |
1823 | */ | |
1824 | elem->st.branches = 1; | |
1825 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
1826 | if (!frame) | |
1827 | goto err; | |
1828 | init_func_state(env, frame, | |
1829 | BPF_MAIN_FUNC /* callsite */, | |
1830 | 0 /* frameno within this callchain */, | |
1831 | subprog /* subprog number within this prog */); | |
1832 | elem->st.frame[0] = frame; | |
1833 | return &elem->st; | |
1834 | err: | |
1835 | free_verifier_state(env->cur_state, true); | |
1836 | env->cur_state = NULL; | |
1837 | /* pop all elements and return */ | |
1838 | while (!pop_stack(env, NULL, NULL, false)); | |
1839 | return NULL; | |
1840 | } | |
1841 | ||
1842 | ||
17a52670 AS |
1843 | enum reg_arg_type { |
1844 | SRC_OP, /* register is used as source operand */ | |
1845 | DST_OP, /* register is used as destination operand */ | |
1846 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1847 | }; | |
1848 | ||
cc8b0b92 AS |
1849 | static int cmp_subprogs(const void *a, const void *b) |
1850 | { | |
9c8105bd JW |
1851 | return ((struct bpf_subprog_info *)a)->start - |
1852 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1853 | } |
1854 | ||
1855 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1856 | { | |
9c8105bd | 1857 | struct bpf_subprog_info *p; |
cc8b0b92 | 1858 | |
9c8105bd JW |
1859 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1860 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1861 | if (!p) |
1862 | return -ENOENT; | |
9c8105bd | 1863 | return p - env->subprog_info; |
cc8b0b92 AS |
1864 | |
1865 | } | |
1866 | ||
1867 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1868 | { | |
1869 | int insn_cnt = env->prog->len; | |
1870 | int ret; | |
1871 | ||
1872 | if (off >= insn_cnt || off < 0) { | |
1873 | verbose(env, "call to invalid destination\n"); | |
1874 | return -EINVAL; | |
1875 | } | |
1876 | ret = find_subprog(env, off); | |
1877 | if (ret >= 0) | |
282a0f46 | 1878 | return ret; |
4cb3d99c | 1879 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1880 | verbose(env, "too many subprograms\n"); |
1881 | return -E2BIG; | |
1882 | } | |
e6ac2450 | 1883 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
1884 | env->subprog_info[env->subprog_cnt++].start = off; |
1885 | sort(env->subprog_info, env->subprog_cnt, | |
1886 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 1887 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
1888 | } |
1889 | ||
2357672c KKD |
1890 | #define MAX_KFUNC_DESCS 256 |
1891 | #define MAX_KFUNC_BTFS 256 | |
1892 | ||
e6ac2450 MKL |
1893 | struct bpf_kfunc_desc { |
1894 | struct btf_func_model func_model; | |
1895 | u32 func_id; | |
1896 | s32 imm; | |
2357672c KKD |
1897 | u16 offset; |
1898 | }; | |
1899 | ||
1900 | struct bpf_kfunc_btf { | |
1901 | struct btf *btf; | |
1902 | struct module *module; | |
1903 | u16 offset; | |
e6ac2450 MKL |
1904 | }; |
1905 | ||
e6ac2450 MKL |
1906 | struct bpf_kfunc_desc_tab { |
1907 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
1908 | u32 nr_descs; | |
1909 | }; | |
1910 | ||
2357672c KKD |
1911 | struct bpf_kfunc_btf_tab { |
1912 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
1913 | u32 nr_descs; | |
1914 | }; | |
1915 | ||
1916 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
1917 | { |
1918 | const struct bpf_kfunc_desc *d0 = a; | |
1919 | const struct bpf_kfunc_desc *d1 = b; | |
1920 | ||
1921 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
1922 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
1923 | } | |
1924 | ||
1925 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
1926 | { | |
1927 | const struct bpf_kfunc_btf *d0 = a; | |
1928 | const struct bpf_kfunc_btf *d1 = b; | |
1929 | ||
1930 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
1931 | } |
1932 | ||
1933 | static const struct bpf_kfunc_desc * | |
2357672c | 1934 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
1935 | { |
1936 | struct bpf_kfunc_desc desc = { | |
1937 | .func_id = func_id, | |
2357672c | 1938 | .offset = offset, |
e6ac2450 MKL |
1939 | }; |
1940 | struct bpf_kfunc_desc_tab *tab; | |
1941 | ||
1942 | tab = prog->aux->kfunc_tab; | |
1943 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
1944 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
1945 | } | |
1946 | ||
1947 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
b202d844 | 1948 | s16 offset) |
2357672c KKD |
1949 | { |
1950 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
1951 | struct bpf_kfunc_btf_tab *tab; | |
1952 | struct bpf_kfunc_btf *b; | |
1953 | struct module *mod; | |
1954 | struct btf *btf; | |
1955 | int btf_fd; | |
1956 | ||
1957 | tab = env->prog->aux->kfunc_btf_tab; | |
1958 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
1959 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
1960 | if (!b) { | |
1961 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
1962 | verbose(env, "too many different module BTFs\n"); | |
1963 | return ERR_PTR(-E2BIG); | |
1964 | } | |
1965 | ||
1966 | if (bpfptr_is_null(env->fd_array)) { | |
1967 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
1968 | return ERR_PTR(-EPROTO); | |
1969 | } | |
1970 | ||
1971 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
1972 | offset * sizeof(btf_fd), | |
1973 | sizeof(btf_fd))) | |
1974 | return ERR_PTR(-EFAULT); | |
1975 | ||
1976 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
1977 | if (IS_ERR(btf)) { |
1978 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 1979 | return btf; |
588cd7ef | 1980 | } |
2357672c KKD |
1981 | |
1982 | if (!btf_is_module(btf)) { | |
1983 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
1984 | btf_put(btf); | |
1985 | return ERR_PTR(-EINVAL); | |
1986 | } | |
1987 | ||
1988 | mod = btf_try_get_module(btf); | |
1989 | if (!mod) { | |
1990 | btf_put(btf); | |
1991 | return ERR_PTR(-ENXIO); | |
1992 | } | |
1993 | ||
1994 | b = &tab->descs[tab->nr_descs++]; | |
1995 | b->btf = btf; | |
1996 | b->module = mod; | |
1997 | b->offset = offset; | |
1998 | ||
1999 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2000 | kfunc_btf_cmp_by_off, NULL); | |
2001 | } | |
2357672c | 2002 | return b->btf; |
e6ac2450 MKL |
2003 | } |
2004 | ||
2357672c KKD |
2005 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
2006 | { | |
2007 | if (!tab) | |
2008 | return; | |
2009 | ||
2010 | while (tab->nr_descs--) { | |
2011 | module_put(tab->descs[tab->nr_descs].module); | |
2012 | btf_put(tab->descs[tab->nr_descs].btf); | |
2013 | } | |
2014 | kfree(tab); | |
2015 | } | |
2016 | ||
43bf0878 | 2017 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset) |
2357672c | 2018 | { |
2357672c KKD |
2019 | if (offset) { |
2020 | if (offset < 0) { | |
2021 | /* In the future, this can be allowed to increase limit | |
2022 | * of fd index into fd_array, interpreted as u16. | |
2023 | */ | |
2024 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
2025 | return ERR_PTR(-EINVAL); | |
2026 | } | |
2027 | ||
b202d844 | 2028 | return __find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2029 | } |
2030 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
2031 | } |
2032 | ||
2357672c | 2033 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
2034 | { |
2035 | const struct btf_type *func, *func_proto; | |
2357672c | 2036 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
2037 | struct bpf_kfunc_desc_tab *tab; |
2038 | struct bpf_prog_aux *prog_aux; | |
2039 | struct bpf_kfunc_desc *desc; | |
2040 | const char *func_name; | |
2357672c | 2041 | struct btf *desc_btf; |
8cbf062a | 2042 | unsigned long call_imm; |
e6ac2450 MKL |
2043 | unsigned long addr; |
2044 | int err; | |
2045 | ||
2046 | prog_aux = env->prog->aux; | |
2047 | tab = prog_aux->kfunc_tab; | |
2357672c | 2048 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
2049 | if (!tab) { |
2050 | if (!btf_vmlinux) { | |
2051 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
2052 | return -ENOTSUPP; | |
2053 | } | |
2054 | ||
2055 | if (!env->prog->jit_requested) { | |
2056 | verbose(env, "JIT is required for calling kernel function\n"); | |
2057 | return -ENOTSUPP; | |
2058 | } | |
2059 | ||
2060 | if (!bpf_jit_supports_kfunc_call()) { | |
2061 | verbose(env, "JIT does not support calling kernel function\n"); | |
2062 | return -ENOTSUPP; | |
2063 | } | |
2064 | ||
2065 | if (!env->prog->gpl_compatible) { | |
2066 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
2067 | return -EINVAL; | |
2068 | } | |
2069 | ||
2070 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
2071 | if (!tab) | |
2072 | return -ENOMEM; | |
2073 | prog_aux->kfunc_tab = tab; | |
2074 | } | |
2075 | ||
a5d82727 KKD |
2076 | /* func_id == 0 is always invalid, but instead of returning an error, be |
2077 | * conservative and wait until the code elimination pass before returning | |
2078 | * error, so that invalid calls that get pruned out can be in BPF programs | |
2079 | * loaded from userspace. It is also required that offset be untouched | |
2080 | * for such calls. | |
2081 | */ | |
2082 | if (!func_id && !offset) | |
2083 | return 0; | |
2084 | ||
2357672c KKD |
2085 | if (!btf_tab && offset) { |
2086 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
2087 | if (!btf_tab) | |
2088 | return -ENOMEM; | |
2089 | prog_aux->kfunc_btf_tab = btf_tab; | |
2090 | } | |
2091 | ||
43bf0878 | 2092 | desc_btf = find_kfunc_desc_btf(env, offset); |
2357672c KKD |
2093 | if (IS_ERR(desc_btf)) { |
2094 | verbose(env, "failed to find BTF for kernel function\n"); | |
2095 | return PTR_ERR(desc_btf); | |
2096 | } | |
2097 | ||
2098 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
2099 | return 0; |
2100 | ||
2101 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
2102 | verbose(env, "too many different kernel function calls\n"); | |
2103 | return -E2BIG; | |
2104 | } | |
2105 | ||
2357672c | 2106 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
2107 | if (!func || !btf_type_is_func(func)) { |
2108 | verbose(env, "kernel btf_id %u is not a function\n", | |
2109 | func_id); | |
2110 | return -EINVAL; | |
2111 | } | |
2357672c | 2112 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
2113 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
2114 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
2115 | func_id); | |
2116 | return -EINVAL; | |
2117 | } | |
2118 | ||
2357672c | 2119 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
2120 | addr = kallsyms_lookup_name(func_name); |
2121 | if (!addr) { | |
2122 | verbose(env, "cannot find address for kernel function %s\n", | |
2123 | func_name); | |
2124 | return -EINVAL; | |
2125 | } | |
2126 | ||
8cbf062a HT |
2127 | call_imm = BPF_CALL_IMM(addr); |
2128 | /* Check whether or not the relative offset overflows desc->imm */ | |
2129 | if ((unsigned long)(s32)call_imm != call_imm) { | |
2130 | verbose(env, "address of kernel function %s is out of range\n", | |
2131 | func_name); | |
2132 | return -EINVAL; | |
2133 | } | |
2134 | ||
e6ac2450 MKL |
2135 | desc = &tab->descs[tab->nr_descs++]; |
2136 | desc->func_id = func_id; | |
8cbf062a | 2137 | desc->imm = call_imm; |
2357672c KKD |
2138 | desc->offset = offset; |
2139 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
2140 | func_proto, func_name, |
2141 | &desc->func_model); | |
2142 | if (!err) | |
2143 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 2144 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
2145 | return err; |
2146 | } | |
2147 | ||
2148 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
2149 | { | |
2150 | const struct bpf_kfunc_desc *d0 = a; | |
2151 | const struct bpf_kfunc_desc *d1 = b; | |
2152 | ||
2153 | if (d0->imm > d1->imm) | |
2154 | return 1; | |
2155 | else if (d0->imm < d1->imm) | |
2156 | return -1; | |
2157 | return 0; | |
2158 | } | |
2159 | ||
2160 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
2161 | { | |
2162 | struct bpf_kfunc_desc_tab *tab; | |
2163 | ||
2164 | tab = prog->aux->kfunc_tab; | |
2165 | if (!tab) | |
2166 | return; | |
2167 | ||
2168 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2169 | kfunc_desc_cmp_by_imm, NULL); | |
2170 | } | |
2171 | ||
2172 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
2173 | { | |
2174 | return !!prog->aux->kfunc_tab; | |
2175 | } | |
2176 | ||
2177 | const struct btf_func_model * | |
2178 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
2179 | const struct bpf_insn *insn) | |
2180 | { | |
2181 | const struct bpf_kfunc_desc desc = { | |
2182 | .imm = insn->imm, | |
2183 | }; | |
2184 | const struct bpf_kfunc_desc *res; | |
2185 | struct bpf_kfunc_desc_tab *tab; | |
2186 | ||
2187 | tab = prog->aux->kfunc_tab; | |
2188 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
2189 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
2190 | ||
2191 | return res ? &res->func_model : NULL; | |
2192 | } | |
2193 | ||
2194 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2195 | { |
9c8105bd | 2196 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2197 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2198 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2199 | |
f910cefa JW |
2200 | /* Add entry function. */ |
2201 | ret = add_subprog(env, 0); | |
e6ac2450 | 2202 | if (ret) |
f910cefa JW |
2203 | return ret; |
2204 | ||
e6ac2450 MKL |
2205 | for (i = 0; i < insn_cnt; i++, insn++) { |
2206 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2207 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2208 | continue; |
e6ac2450 | 2209 | |
2c78ee89 | 2210 | if (!env->bpf_capable) { |
e6ac2450 | 2211 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2212 | return -EPERM; |
2213 | } | |
e6ac2450 | 2214 | |
3990ed4c | 2215 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2216 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2217 | else |
2357672c | 2218 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2219 | |
cc8b0b92 AS |
2220 | if (ret < 0) |
2221 | return ret; | |
2222 | } | |
2223 | ||
4cb3d99c JW |
2224 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2225 | * logic. 'subprog_cnt' should not be increased. | |
2226 | */ | |
2227 | subprog[env->subprog_cnt].start = insn_cnt; | |
2228 | ||
06ee7115 | 2229 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2230 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2231 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2232 | |
e6ac2450 MKL |
2233 | return 0; |
2234 | } | |
2235 | ||
2236 | static int check_subprogs(struct bpf_verifier_env *env) | |
2237 | { | |
2238 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2239 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2240 | struct bpf_insn *insn = env->prog->insnsi; | |
2241 | int insn_cnt = env->prog->len; | |
2242 | ||
cc8b0b92 | 2243 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2244 | subprog_start = subprog[cur_subprog].start; |
2245 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2246 | for (i = 0; i < insn_cnt; i++) { |
2247 | u8 code = insn[i].code; | |
2248 | ||
7f6e4312 MF |
2249 | if (code == (BPF_JMP | BPF_CALL) && |
2250 | insn[i].imm == BPF_FUNC_tail_call && | |
2251 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
2252 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
2253 | if (BPF_CLASS(code) == BPF_LD && |
2254 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2255 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2256 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2257 | goto next; |
2258 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2259 | goto next; | |
2260 | off = i + insn[i].off + 1; | |
2261 | if (off < subprog_start || off >= subprog_end) { | |
2262 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2263 | return -EINVAL; | |
2264 | } | |
2265 | next: | |
2266 | if (i == subprog_end - 1) { | |
2267 | /* to avoid fall-through from one subprog into another | |
2268 | * the last insn of the subprog should be either exit | |
2269 | * or unconditional jump back | |
2270 | */ | |
2271 | if (code != (BPF_JMP | BPF_EXIT) && | |
2272 | code != (BPF_JMP | BPF_JA)) { | |
2273 | verbose(env, "last insn is not an exit or jmp\n"); | |
2274 | return -EINVAL; | |
2275 | } | |
2276 | subprog_start = subprog_end; | |
4cb3d99c JW |
2277 | cur_subprog++; |
2278 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2279 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2280 | } |
2281 | } | |
2282 | return 0; | |
2283 | } | |
2284 | ||
679c782d EC |
2285 | /* Parentage chain of this register (or stack slot) should take care of all |
2286 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2287 | */ | |
f4d7e40a | 2288 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2289 | const struct bpf_reg_state *state, |
5327ed3d | 2290 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2291 | { |
2292 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2293 | int cnt = 0; |
dc503a8a EC |
2294 | |
2295 | while (parent) { | |
2296 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2297 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2298 | break; |
9242b5f5 AS |
2299 | if (parent->live & REG_LIVE_DONE) { |
2300 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
c25b2ae1 | 2301 | reg_type_str(env, parent->type), |
9242b5f5 AS |
2302 | parent->var_off.value, parent->off); |
2303 | return -EFAULT; | |
2304 | } | |
5327ed3d JW |
2305 | /* The first condition is more likely to be true than the |
2306 | * second, checked it first. | |
2307 | */ | |
2308 | if ((parent->live & REG_LIVE_READ) == flag || | |
2309 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2310 | /* The parentage chain never changes and |
2311 | * this parent was already marked as LIVE_READ. | |
2312 | * There is no need to keep walking the chain again and | |
2313 | * keep re-marking all parents as LIVE_READ. | |
2314 | * This case happens when the same register is read | |
2315 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2316 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2317 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2318 | */ |
2319 | break; | |
dc503a8a | 2320 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2321 | parent->live |= flag; |
2322 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2323 | if (flag == REG_LIVE_READ64) | |
2324 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2325 | state = parent; |
2326 | parent = state->parent; | |
f4d7e40a | 2327 | writes = true; |
06ee7115 | 2328 | cnt++; |
dc503a8a | 2329 | } |
06ee7115 AS |
2330 | |
2331 | if (env->longest_mark_read_walk < cnt) | |
2332 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2333 | return 0; |
dc503a8a EC |
2334 | } |
2335 | ||
5327ed3d JW |
2336 | /* This function is supposed to be used by the following 32-bit optimization |
2337 | * code only. It returns TRUE if the source or destination register operates | |
2338 | * on 64-bit, otherwise return FALSE. | |
2339 | */ | |
2340 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2341 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2342 | { | |
2343 | u8 code, class, op; | |
2344 | ||
2345 | code = insn->code; | |
2346 | class = BPF_CLASS(code); | |
2347 | op = BPF_OP(code); | |
2348 | if (class == BPF_JMP) { | |
2349 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2350 | * conservatively. | |
2351 | */ | |
2352 | if (op == BPF_EXIT) | |
2353 | return true; | |
2354 | if (op == BPF_CALL) { | |
2355 | /* BPF to BPF call will reach here because of marking | |
2356 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2357 | * don't care the register def because they are anyway | |
2358 | * marked as NOT_INIT already. | |
2359 | */ | |
2360 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2361 | return false; | |
2362 | /* Helper call will reach here because of arg type | |
2363 | * check, conservatively return TRUE. | |
2364 | */ | |
2365 | if (t == SRC_OP) | |
2366 | return true; | |
2367 | ||
2368 | return false; | |
2369 | } | |
2370 | } | |
2371 | ||
2372 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2373 | /* BPF_END always use BPF_ALU class. */ | |
2374 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2375 | return true; | |
2376 | ||
2377 | if (class == BPF_ALU || class == BPF_JMP32) | |
2378 | return false; | |
2379 | ||
2380 | if (class == BPF_LDX) { | |
2381 | if (t != SRC_OP) | |
2382 | return BPF_SIZE(code) == BPF_DW; | |
2383 | /* LDX source must be ptr. */ | |
2384 | return true; | |
2385 | } | |
2386 | ||
2387 | if (class == BPF_STX) { | |
83a28819 IL |
2388 | /* BPF_STX (including atomic variants) has multiple source |
2389 | * operands, one of which is a ptr. Check whether the caller is | |
2390 | * asking about it. | |
2391 | */ | |
2392 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2393 | return true; |
2394 | return BPF_SIZE(code) == BPF_DW; | |
2395 | } | |
2396 | ||
2397 | if (class == BPF_LD) { | |
2398 | u8 mode = BPF_MODE(code); | |
2399 | ||
2400 | /* LD_IMM64 */ | |
2401 | if (mode == BPF_IMM) | |
2402 | return true; | |
2403 | ||
2404 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
2405 | if (t != SRC_OP) | |
2406 | return false; | |
2407 | ||
2408 | /* Implicit ctx ptr. */ | |
2409 | if (regno == BPF_REG_6) | |
2410 | return true; | |
2411 | ||
2412 | /* Explicit source could be any width. */ | |
2413 | return true; | |
2414 | } | |
2415 | ||
2416 | if (class == BPF_ST) | |
2417 | /* The only source register for BPF_ST is a ptr. */ | |
2418 | return true; | |
2419 | ||
2420 | /* Conservatively return true at default. */ | |
2421 | return true; | |
2422 | } | |
2423 | ||
83a28819 IL |
2424 | /* Return the regno defined by the insn, or -1. */ |
2425 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 2426 | { |
83a28819 IL |
2427 | switch (BPF_CLASS(insn->code)) { |
2428 | case BPF_JMP: | |
2429 | case BPF_JMP32: | |
2430 | case BPF_ST: | |
2431 | return -1; | |
2432 | case BPF_STX: | |
2433 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
2434 | (insn->imm & BPF_FETCH)) { | |
2435 | if (insn->imm == BPF_CMPXCHG) | |
2436 | return BPF_REG_0; | |
2437 | else | |
2438 | return insn->src_reg; | |
2439 | } else { | |
2440 | return -1; | |
2441 | } | |
2442 | default: | |
2443 | return insn->dst_reg; | |
2444 | } | |
b325fbca JW |
2445 | } |
2446 | ||
2447 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
2448 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
2449 | { | |
83a28819 IL |
2450 | int dst_reg = insn_def_regno(insn); |
2451 | ||
2452 | if (dst_reg == -1) | |
b325fbca JW |
2453 | return false; |
2454 | ||
83a28819 | 2455 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
2456 | } |
2457 | ||
5327ed3d JW |
2458 | static void mark_insn_zext(struct bpf_verifier_env *env, |
2459 | struct bpf_reg_state *reg) | |
2460 | { | |
2461 | s32 def_idx = reg->subreg_def; | |
2462 | ||
2463 | if (def_idx == DEF_NOT_SUBREG) | |
2464 | return; | |
2465 | ||
2466 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
2467 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
2468 | reg->subreg_def = DEF_NOT_SUBREG; | |
2469 | } | |
2470 | ||
dc503a8a | 2471 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
2472 | enum reg_arg_type t) |
2473 | { | |
f4d7e40a AS |
2474 | struct bpf_verifier_state *vstate = env->cur_state; |
2475 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 2476 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 2477 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 2478 | bool rw64; |
dc503a8a | 2479 | |
17a52670 | 2480 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 2481 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
2482 | return -EINVAL; |
2483 | } | |
2484 | ||
0f55f9ed CL |
2485 | mark_reg_scratched(env, regno); |
2486 | ||
c342dc10 | 2487 | reg = ®s[regno]; |
5327ed3d | 2488 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
2489 | if (t == SRC_OP) { |
2490 | /* check whether register used as source operand can be read */ | |
c342dc10 | 2491 | if (reg->type == NOT_INIT) { |
61bd5218 | 2492 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
2493 | return -EACCES; |
2494 | } | |
679c782d | 2495 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
2496 | if (regno == BPF_REG_FP) |
2497 | return 0; | |
2498 | ||
5327ed3d JW |
2499 | if (rw64) |
2500 | mark_insn_zext(env, reg); | |
2501 | ||
2502 | return mark_reg_read(env, reg, reg->parent, | |
2503 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
2504 | } else { |
2505 | /* check whether register used as dest operand can be written to */ | |
2506 | if (regno == BPF_REG_FP) { | |
61bd5218 | 2507 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
2508 | return -EACCES; |
2509 | } | |
c342dc10 | 2510 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 2511 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 2512 | if (t == DST_OP) |
61bd5218 | 2513 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
2514 | } |
2515 | return 0; | |
2516 | } | |
2517 | ||
b5dc0163 AS |
2518 | /* for any branch, call, exit record the history of jmps in the given state */ |
2519 | static int push_jmp_history(struct bpf_verifier_env *env, | |
2520 | struct bpf_verifier_state *cur) | |
2521 | { | |
2522 | u32 cnt = cur->jmp_history_cnt; | |
2523 | struct bpf_idx_pair *p; | |
2524 | ||
2525 | cnt++; | |
2526 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
2527 | if (!p) | |
2528 | return -ENOMEM; | |
2529 | p[cnt - 1].idx = env->insn_idx; | |
2530 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
2531 | cur->jmp_history = p; | |
2532 | cur->jmp_history_cnt = cnt; | |
2533 | return 0; | |
2534 | } | |
2535 | ||
2536 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
2537 | * history then previous instruction came from straight line execution. | |
2538 | */ | |
2539 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
2540 | u32 *history) | |
2541 | { | |
2542 | u32 cnt = *history; | |
2543 | ||
2544 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
2545 | i = st->jmp_history[cnt - 1].prev_idx; | |
2546 | (*history)--; | |
2547 | } else { | |
2548 | i--; | |
2549 | } | |
2550 | return i; | |
2551 | } | |
2552 | ||
e6ac2450 MKL |
2553 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
2554 | { | |
2555 | const struct btf_type *func; | |
2357672c | 2556 | struct btf *desc_btf; |
e6ac2450 MKL |
2557 | |
2558 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
2559 | return NULL; | |
2560 | ||
43bf0878 | 2561 | desc_btf = find_kfunc_desc_btf(data, insn->off); |
2357672c KKD |
2562 | if (IS_ERR(desc_btf)) |
2563 | return "<error>"; | |
2564 | ||
2565 | func = btf_type_by_id(desc_btf, insn->imm); | |
2566 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
2567 | } |
2568 | ||
b5dc0163 AS |
2569 | /* For given verifier state backtrack_insn() is called from the last insn to |
2570 | * the first insn. Its purpose is to compute a bitmask of registers and | |
2571 | * stack slots that needs precision in the parent verifier state. | |
2572 | */ | |
2573 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
2574 | u32 *reg_mask, u64 *stack_mask) | |
2575 | { | |
2576 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 2577 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
2578 | .cb_print = verbose, |
2579 | .private_data = env, | |
2580 | }; | |
2581 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
2582 | u8 class = BPF_CLASS(insn->code); | |
2583 | u8 opcode = BPF_OP(insn->code); | |
2584 | u8 mode = BPF_MODE(insn->code); | |
2585 | u32 dreg = 1u << insn->dst_reg; | |
2586 | u32 sreg = 1u << insn->src_reg; | |
2587 | u32 spi; | |
2588 | ||
2589 | if (insn->code == 0) | |
2590 | return 0; | |
496f3324 | 2591 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
2592 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
2593 | verbose(env, "%d: ", idx); | |
2594 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
2595 | } | |
2596 | ||
2597 | if (class == BPF_ALU || class == BPF_ALU64) { | |
2598 | if (!(*reg_mask & dreg)) | |
2599 | return 0; | |
2600 | if (opcode == BPF_MOV) { | |
2601 | if (BPF_SRC(insn->code) == BPF_X) { | |
2602 | /* dreg = sreg | |
2603 | * dreg needs precision after this insn | |
2604 | * sreg needs precision before this insn | |
2605 | */ | |
2606 | *reg_mask &= ~dreg; | |
2607 | *reg_mask |= sreg; | |
2608 | } else { | |
2609 | /* dreg = K | |
2610 | * dreg needs precision after this insn. | |
2611 | * Corresponding register is already marked | |
2612 | * as precise=true in this verifier state. | |
2613 | * No further markings in parent are necessary | |
2614 | */ | |
2615 | *reg_mask &= ~dreg; | |
2616 | } | |
2617 | } else { | |
2618 | if (BPF_SRC(insn->code) == BPF_X) { | |
2619 | /* dreg += sreg | |
2620 | * both dreg and sreg need precision | |
2621 | * before this insn | |
2622 | */ | |
2623 | *reg_mask |= sreg; | |
2624 | } /* else dreg += K | |
2625 | * dreg still needs precision before this insn | |
2626 | */ | |
2627 | } | |
2628 | } else if (class == BPF_LDX) { | |
2629 | if (!(*reg_mask & dreg)) | |
2630 | return 0; | |
2631 | *reg_mask &= ~dreg; | |
2632 | ||
2633 | /* scalars can only be spilled into stack w/o losing precision. | |
2634 | * Load from any other memory can be zero extended. | |
2635 | * The desire to keep that precision is already indicated | |
2636 | * by 'precise' mark in corresponding register of this state. | |
2637 | * No further tracking necessary. | |
2638 | */ | |
2639 | if (insn->src_reg != BPF_REG_FP) | |
2640 | return 0; | |
b5dc0163 AS |
2641 | |
2642 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
2643 | * that [fp - off] slot contains scalar that needs to be | |
2644 | * tracked with precision | |
2645 | */ | |
2646 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2647 | if (spi >= 64) { | |
2648 | verbose(env, "BUG spi %d\n", spi); | |
2649 | WARN_ONCE(1, "verifier backtracking bug"); | |
2650 | return -EFAULT; | |
2651 | } | |
2652 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 2653 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 2654 | if (*reg_mask & dreg) |
b3b50f05 | 2655 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
2656 | * to access memory. It means backtracking |
2657 | * encountered a case of pointer subtraction. | |
2658 | */ | |
2659 | return -ENOTSUPP; | |
2660 | /* scalars can only be spilled into stack */ | |
2661 | if (insn->dst_reg != BPF_REG_FP) | |
2662 | return 0; | |
b5dc0163 AS |
2663 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
2664 | if (spi >= 64) { | |
2665 | verbose(env, "BUG spi %d\n", spi); | |
2666 | WARN_ONCE(1, "verifier backtracking bug"); | |
2667 | return -EFAULT; | |
2668 | } | |
2669 | if (!(*stack_mask & (1ull << spi))) | |
2670 | return 0; | |
2671 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
2672 | if (class == BPF_STX) |
2673 | *reg_mask |= sreg; | |
b5dc0163 AS |
2674 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
2675 | if (opcode == BPF_CALL) { | |
2676 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2677 | return -ENOTSUPP; | |
be2ef816 AN |
2678 | /* BPF helpers that invoke callback subprogs are |
2679 | * equivalent to BPF_PSEUDO_CALL above | |
2680 | */ | |
2681 | if (insn->src_reg == 0 && is_callback_calling_function(insn->imm)) | |
2682 | return -ENOTSUPP; | |
b5dc0163 AS |
2683 | /* regular helper call sets R0 */ |
2684 | *reg_mask &= ~1; | |
2685 | if (*reg_mask & 0x3f) { | |
2686 | /* if backtracing was looking for registers R1-R5 | |
2687 | * they should have been found already. | |
2688 | */ | |
2689 | verbose(env, "BUG regs %x\n", *reg_mask); | |
2690 | WARN_ONCE(1, "verifier backtracking bug"); | |
2691 | return -EFAULT; | |
2692 | } | |
2693 | } else if (opcode == BPF_EXIT) { | |
2694 | return -ENOTSUPP; | |
2695 | } | |
2696 | } else if (class == BPF_LD) { | |
2697 | if (!(*reg_mask & dreg)) | |
2698 | return 0; | |
2699 | *reg_mask &= ~dreg; | |
2700 | /* It's ld_imm64 or ld_abs or ld_ind. | |
2701 | * For ld_imm64 no further tracking of precision | |
2702 | * into parent is necessary | |
2703 | */ | |
2704 | if (mode == BPF_IND || mode == BPF_ABS) | |
2705 | /* to be analyzed */ | |
2706 | return -ENOTSUPP; | |
b5dc0163 AS |
2707 | } |
2708 | return 0; | |
2709 | } | |
2710 | ||
2711 | /* the scalar precision tracking algorithm: | |
2712 | * . at the start all registers have precise=false. | |
2713 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
2714 | * . once precise value of the scalar register is used in: | |
2715 | * . ptr + scalar alu | |
2716 | * . if (scalar cond K|scalar) | |
2717 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
2718 | * backtrack through the verifier states and mark all registers and | |
2719 | * stack slots with spilled constants that these scalar regisers | |
2720 | * should be precise. | |
2721 | * . during state pruning two registers (or spilled stack slots) | |
2722 | * are equivalent if both are not precise. | |
2723 | * | |
2724 | * Note the verifier cannot simply walk register parentage chain, | |
2725 | * since many different registers and stack slots could have been | |
2726 | * used to compute single precise scalar. | |
2727 | * | |
2728 | * The approach of starting with precise=true for all registers and then | |
2729 | * backtrack to mark a register as not precise when the verifier detects | |
2730 | * that program doesn't care about specific value (e.g., when helper | |
2731 | * takes register as ARG_ANYTHING parameter) is not safe. | |
2732 | * | |
2733 | * It's ok to walk single parentage chain of the verifier states. | |
2734 | * It's possible that this backtracking will go all the way till 1st insn. | |
2735 | * All other branches will be explored for needing precision later. | |
2736 | * | |
2737 | * The backtracking needs to deal with cases like: | |
2738 | * 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) | |
2739 | * r9 -= r8 | |
2740 | * r5 = r9 | |
2741 | * if r5 > 0x79f goto pc+7 | |
2742 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
2743 | * r5 += 1 | |
2744 | * ... | |
2745 | * call bpf_perf_event_output#25 | |
2746 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
2747 | * | |
2748 | * and this case: | |
2749 | * r6 = 1 | |
2750 | * call foo // uses callee's r6 inside to compute r0 | |
2751 | * r0 += r6 | |
2752 | * if r0 == 0 goto | |
2753 | * | |
2754 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
2755 | * | |
2756 | * Also if parent's curframe > frame where backtracking started, | |
2757 | * the verifier need to mark registers in both frames, otherwise callees | |
2758 | * may incorrectly prune callers. This is similar to | |
2759 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
2760 | * | |
2761 | * For now backtracking falls back into conservative marking. | |
2762 | */ | |
2763 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
2764 | struct bpf_verifier_state *st) | |
2765 | { | |
2766 | struct bpf_func_state *func; | |
2767 | struct bpf_reg_state *reg; | |
2768 | int i, j; | |
2769 | ||
2770 | /* big hammer: mark all scalars precise in this path. | |
2771 | * pop_stack may still get !precise scalars. | |
f63181b6 AN |
2772 | * We also skip current state and go straight to first parent state, |
2773 | * because precision markings in current non-checkpointed state are | |
2774 | * not needed. See why in the comment in __mark_chain_precision below. | |
b5dc0163 | 2775 | */ |
f63181b6 | 2776 | for (st = st->parent; st; st = st->parent) { |
b5dc0163 AS |
2777 | for (i = 0; i <= st->curframe; i++) { |
2778 | func = st->frame[i]; | |
2779 | for (j = 0; j < BPF_REG_FP; j++) { | |
2780 | reg = &func->regs[j]; | |
2781 | if (reg->type != SCALAR_VALUE) | |
2782 | continue; | |
2783 | reg->precise = true; | |
2784 | } | |
2785 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 2786 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
2787 | continue; |
2788 | reg = &func->stack[j].spilled_ptr; | |
2789 | if (reg->type != SCALAR_VALUE) | |
2790 | continue; | |
2791 | reg->precise = true; | |
2792 | } | |
2793 | } | |
f63181b6 | 2794 | } |
b5dc0163 AS |
2795 | } |
2796 | ||
7a830b53 AN |
2797 | static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
2798 | { | |
2799 | struct bpf_func_state *func; | |
2800 | struct bpf_reg_state *reg; | |
2801 | int i, j; | |
2802 | ||
2803 | for (i = 0; i <= st->curframe; i++) { | |
2804 | func = st->frame[i]; | |
2805 | for (j = 0; j < BPF_REG_FP; j++) { | |
2806 | reg = &func->regs[j]; | |
2807 | if (reg->type != SCALAR_VALUE) | |
2808 | continue; | |
2809 | reg->precise = false; | |
2810 | } | |
2811 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
2812 | if (!is_spilled_reg(&func->stack[j])) | |
2813 | continue; | |
2814 | reg = &func->stack[j].spilled_ptr; | |
2815 | if (reg->type != SCALAR_VALUE) | |
2816 | continue; | |
2817 | reg->precise = false; | |
2818 | } | |
2819 | } | |
2820 | } | |
2821 | ||
f63181b6 AN |
2822 | /* |
2823 | * __mark_chain_precision() backtracks BPF program instruction sequence and | |
2824 | * chain of verifier states making sure that register *regno* (if regno >= 0) | |
2825 | * and/or stack slot *spi* (if spi >= 0) are marked as precisely tracked | |
2826 | * SCALARS, as well as any other registers and slots that contribute to | |
2827 | * a tracked state of given registers/stack slots, depending on specific BPF | |
2828 | * assembly instructions (see backtrack_insns() for exact instruction handling | |
2829 | * logic). This backtracking relies on recorded jmp_history and is able to | |
2830 | * traverse entire chain of parent states. This process ends only when all the | |
2831 | * necessary registers/slots and their transitive dependencies are marked as | |
2832 | * precise. | |
2833 | * | |
2834 | * One important and subtle aspect is that precise marks *do not matter* in | |
2835 | * the currently verified state (current state). It is important to understand | |
2836 | * why this is the case. | |
2837 | * | |
2838 | * First, note that current state is the state that is not yet "checkpointed", | |
2839 | * i.e., it is not yet put into env->explored_states, and it has no children | |
2840 | * states as well. It's ephemeral, and can end up either a) being discarded if | |
2841 | * compatible explored state is found at some point or BPF_EXIT instruction is | |
2842 | * reached or b) checkpointed and put into env->explored_states, branching out | |
2843 | * into one or more children states. | |
2844 | * | |
2845 | * In the former case, precise markings in current state are completely | |
2846 | * ignored by state comparison code (see regsafe() for details). Only | |
2847 | * checkpointed ("old") state precise markings are important, and if old | |
2848 | * state's register/slot is precise, regsafe() assumes current state's | |
2849 | * register/slot as precise and checks value ranges exactly and precisely. If | |
2850 | * states turn out to be compatible, current state's necessary precise | |
2851 | * markings and any required parent states' precise markings are enforced | |
2852 | * after the fact with propagate_precision() logic, after the fact. But it's | |
2853 | * important to realize that in this case, even after marking current state | |
2854 | * registers/slots as precise, we immediately discard current state. So what | |
2855 | * actually matters is any of the precise markings propagated into current | |
2856 | * state's parent states, which are always checkpointed (due to b) case above). | |
2857 | * As such, for scenario a) it doesn't matter if current state has precise | |
2858 | * markings set or not. | |
2859 | * | |
2860 | * Now, for the scenario b), checkpointing and forking into child(ren) | |
2861 | * state(s). Note that before current state gets to checkpointing step, any | |
2862 | * processed instruction always assumes precise SCALAR register/slot | |
2863 | * knowledge: if precise value or range is useful to prune jump branch, BPF | |
2864 | * verifier takes this opportunity enthusiastically. Similarly, when | |
2865 | * register's value is used to calculate offset or memory address, exact | |
2866 | * knowledge of SCALAR range is assumed, checked, and enforced. So, similar to | |
2867 | * what we mentioned above about state comparison ignoring precise markings | |
2868 | * during state comparison, BPF verifier ignores and also assumes precise | |
2869 | * markings *at will* during instruction verification process. But as verifier | |
2870 | * assumes precision, it also propagates any precision dependencies across | |
2871 | * parent states, which are not yet finalized, so can be further restricted | |
2872 | * based on new knowledge gained from restrictions enforced by their children | |
2873 | * states. This is so that once those parent states are finalized, i.e., when | |
2874 | * they have no more active children state, state comparison logic in | |
2875 | * is_state_visited() would enforce strict and precise SCALAR ranges, if | |
2876 | * required for correctness. | |
2877 | * | |
2878 | * To build a bit more intuition, note also that once a state is checkpointed, | |
2879 | * the path we took to get to that state is not important. This is crucial | |
2880 | * property for state pruning. When state is checkpointed and finalized at | |
2881 | * some instruction index, it can be correctly and safely used to "short | |
2882 | * circuit" any *compatible* state that reaches exactly the same instruction | |
2883 | * index. I.e., if we jumped to that instruction from a completely different | |
2884 | * code path than original finalized state was derived from, it doesn't | |
2885 | * matter, current state can be discarded because from that instruction | |
2886 | * forward having a compatible state will ensure we will safely reach the | |
2887 | * exit. States describe preconditions for further exploration, but completely | |
2888 | * forget the history of how we got here. | |
2889 | * | |
2890 | * This also means that even if we needed precise SCALAR range to get to | |
2891 | * finalized state, but from that point forward *that same* SCALAR register is | |
2892 | * never used in a precise context (i.e., it's precise value is not needed for | |
2893 | * correctness), it's correct and safe to mark such register as "imprecise" | |
2894 | * (i.e., precise marking set to false). This is what we rely on when we do | |
2895 | * not set precise marking in current state. If no child state requires | |
2896 | * precision for any given SCALAR register, it's safe to dictate that it can | |
2897 | * be imprecise. If any child state does require this register to be precise, | |
2898 | * we'll mark it precise later retroactively during precise markings | |
2899 | * propagation from child state to parent states. | |
7a830b53 AN |
2900 | * |
2901 | * Skipping precise marking setting in current state is a mild version of | |
2902 | * relying on the above observation. But we can utilize this property even | |
2903 | * more aggressively by proactively forgetting any precise marking in the | |
2904 | * current state (which we inherited from the parent state), right before we | |
2905 | * checkpoint it and branch off into new child state. This is done by | |
2906 | * mark_all_scalars_imprecise() to hopefully get more permissive and generic | |
2907 | * finalized states which help in short circuiting more future states. | |
f63181b6 | 2908 | */ |
529409ea | 2909 | static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int regno, |
a3ce685d | 2910 | int spi) |
b5dc0163 AS |
2911 | { |
2912 | struct bpf_verifier_state *st = env->cur_state; | |
2913 | int first_idx = st->first_insn_idx; | |
2914 | int last_idx = env->insn_idx; | |
2915 | struct bpf_func_state *func; | |
2916 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2917 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2918 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2919 | bool skip_first = true; |
a3ce685d | 2920 | bool new_marks = false; |
b5dc0163 AS |
2921 | int i, err; |
2922 | ||
2c78ee89 | 2923 | if (!env->bpf_capable) |
b5dc0163 AS |
2924 | return 0; |
2925 | ||
f63181b6 AN |
2926 | /* Do sanity checks against current state of register and/or stack |
2927 | * slot, but don't set precise flag in current state, as precision | |
2928 | * tracking in the current state is unnecessary. | |
2929 | */ | |
529409ea | 2930 | func = st->frame[frame]; |
a3ce685d AS |
2931 | if (regno >= 0) { |
2932 | reg = &func->regs[regno]; | |
2933 | if (reg->type != SCALAR_VALUE) { | |
2934 | WARN_ONCE(1, "backtracing misuse"); | |
2935 | return -EFAULT; | |
2936 | } | |
f63181b6 | 2937 | new_marks = true; |
b5dc0163 | 2938 | } |
b5dc0163 | 2939 | |
a3ce685d | 2940 | while (spi >= 0) { |
27113c59 | 2941 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
2942 | stack_mask = 0; |
2943 | break; | |
2944 | } | |
2945 | reg = &func->stack[spi].spilled_ptr; | |
2946 | if (reg->type != SCALAR_VALUE) { | |
2947 | stack_mask = 0; | |
2948 | break; | |
2949 | } | |
f63181b6 | 2950 | new_marks = true; |
a3ce685d AS |
2951 | break; |
2952 | } | |
2953 | ||
2954 | if (!new_marks) | |
2955 | return 0; | |
2956 | if (!reg_mask && !stack_mask) | |
2957 | return 0; | |
be2ef816 | 2958 | |
b5dc0163 AS |
2959 | for (;;) { |
2960 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2961 | u32 history = st->jmp_history_cnt; |
2962 | ||
496f3324 | 2963 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 | 2964 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
be2ef816 AN |
2965 | |
2966 | if (last_idx < 0) { | |
2967 | /* we are at the entry into subprog, which | |
2968 | * is expected for global funcs, but only if | |
2969 | * requested precise registers are R1-R5 | |
2970 | * (which are global func's input arguments) | |
2971 | */ | |
2972 | if (st->curframe == 0 && | |
2973 | st->frame[0]->subprogno > 0 && | |
2974 | st->frame[0]->callsite == BPF_MAIN_FUNC && | |
2975 | stack_mask == 0 && (reg_mask & ~0x3e) == 0) { | |
2976 | bitmap_from_u64(mask, reg_mask); | |
2977 | for_each_set_bit(i, mask, 32) { | |
2978 | reg = &st->frame[0]->regs[i]; | |
2979 | if (reg->type != SCALAR_VALUE) { | |
2980 | reg_mask &= ~(1u << i); | |
2981 | continue; | |
2982 | } | |
2983 | reg->precise = true; | |
2984 | } | |
2985 | return 0; | |
2986 | } | |
2987 | ||
2988 | verbose(env, "BUG backtracing func entry subprog %d reg_mask %x stack_mask %llx\n", | |
2989 | st->frame[0]->subprogno, reg_mask, stack_mask); | |
2990 | WARN_ONCE(1, "verifier backtracking bug"); | |
2991 | return -EFAULT; | |
2992 | } | |
2993 | ||
b5dc0163 AS |
2994 | for (i = last_idx;;) { |
2995 | if (skip_first) { | |
2996 | err = 0; | |
2997 | skip_first = false; | |
2998 | } else { | |
2999 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
3000 | } | |
3001 | if (err == -ENOTSUPP) { | |
3002 | mark_all_scalars_precise(env, st); | |
3003 | return 0; | |
3004 | } else if (err) { | |
3005 | return err; | |
3006 | } | |
3007 | if (!reg_mask && !stack_mask) | |
3008 | /* Found assignment(s) into tracked register in this state. | |
3009 | * Since this state is already marked, just return. | |
3010 | * Nothing to be tracked further in the parent state. | |
3011 | */ | |
3012 | return 0; | |
3013 | if (i == first_idx) | |
3014 | break; | |
3015 | i = get_prev_insn_idx(st, i, &history); | |
3016 | if (i >= env->prog->len) { | |
3017 | /* This can happen if backtracking reached insn 0 | |
3018 | * and there are still reg_mask or stack_mask | |
3019 | * to backtrack. | |
3020 | * It means the backtracking missed the spot where | |
3021 | * particular register was initialized with a constant. | |
3022 | */ | |
3023 | verbose(env, "BUG backtracking idx %d\n", i); | |
3024 | WARN_ONCE(1, "verifier backtracking bug"); | |
3025 | return -EFAULT; | |
3026 | } | |
3027 | } | |
3028 | st = st->parent; | |
3029 | if (!st) | |
3030 | break; | |
3031 | ||
a3ce685d | 3032 | new_marks = false; |
529409ea | 3033 | func = st->frame[frame]; |
b5dc0163 AS |
3034 | bitmap_from_u64(mask, reg_mask); |
3035 | for_each_set_bit(i, mask, 32) { | |
3036 | reg = &func->regs[i]; | |
a3ce685d AS |
3037 | if (reg->type != SCALAR_VALUE) { |
3038 | reg_mask &= ~(1u << i); | |
b5dc0163 | 3039 | continue; |
a3ce685d | 3040 | } |
b5dc0163 AS |
3041 | if (!reg->precise) |
3042 | new_marks = true; | |
3043 | reg->precise = true; | |
3044 | } | |
3045 | ||
3046 | bitmap_from_u64(mask, stack_mask); | |
3047 | for_each_set_bit(i, mask, 64) { | |
3048 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
3049 | /* the sequence of instructions: |
3050 | * 2: (bf) r3 = r10 | |
3051 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
3052 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
3053 | * doesn't contain jmps. It's backtracked | |
3054 | * as a single block. | |
3055 | * During backtracking insn 3 is not recognized as | |
3056 | * stack access, so at the end of backtracking | |
3057 | * stack slot fp-8 is still marked in stack_mask. | |
3058 | * However the parent state may not have accessed | |
3059 | * fp-8 and it's "unallocated" stack space. | |
3060 | * In such case fallback to conservative. | |
b5dc0163 | 3061 | */ |
2339cd6c AS |
3062 | mark_all_scalars_precise(env, st); |
3063 | return 0; | |
b5dc0163 AS |
3064 | } |
3065 | ||
27113c59 | 3066 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 3067 | stack_mask &= ~(1ull << i); |
b5dc0163 | 3068 | continue; |
a3ce685d | 3069 | } |
b5dc0163 | 3070 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
3071 | if (reg->type != SCALAR_VALUE) { |
3072 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 3073 | continue; |
a3ce685d | 3074 | } |
b5dc0163 AS |
3075 | if (!reg->precise) |
3076 | new_marks = true; | |
3077 | reg->precise = true; | |
3078 | } | |
496f3324 | 3079 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 3080 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
3081 | new_marks ? "didn't have" : "already had", |
3082 | reg_mask, stack_mask); | |
2e576648 | 3083 | print_verifier_state(env, func, true); |
b5dc0163 AS |
3084 | } |
3085 | ||
a3ce685d AS |
3086 | if (!reg_mask && !stack_mask) |
3087 | break; | |
b5dc0163 AS |
3088 | if (!new_marks) |
3089 | break; | |
3090 | ||
3091 | last_idx = st->last_insn_idx; | |
3092 | first_idx = st->first_insn_idx; | |
3093 | } | |
3094 | return 0; | |
3095 | } | |
3096 | ||
eb1f7f71 | 3097 | int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
a3ce685d | 3098 | { |
529409ea | 3099 | return __mark_chain_precision(env, env->cur_state->curframe, regno, -1); |
a3ce685d AS |
3100 | } |
3101 | ||
529409ea | 3102 | static int mark_chain_precision_frame(struct bpf_verifier_env *env, int frame, int regno) |
a3ce685d | 3103 | { |
529409ea | 3104 | return __mark_chain_precision(env, frame, regno, -1); |
a3ce685d AS |
3105 | } |
3106 | ||
529409ea | 3107 | static int mark_chain_precision_stack_frame(struct bpf_verifier_env *env, int frame, int spi) |
a3ce685d | 3108 | { |
529409ea | 3109 | return __mark_chain_precision(env, frame, -1, spi); |
a3ce685d | 3110 | } |
b5dc0163 | 3111 | |
1be7f75d AS |
3112 | static bool is_spillable_regtype(enum bpf_reg_type type) |
3113 | { | |
c25b2ae1 | 3114 | switch (base_type(type)) { |
1be7f75d | 3115 | case PTR_TO_MAP_VALUE: |
1be7f75d AS |
3116 | case PTR_TO_STACK: |
3117 | case PTR_TO_CTX: | |
969bf05e | 3118 | case PTR_TO_PACKET: |
de8f3a83 | 3119 | case PTR_TO_PACKET_META: |
969bf05e | 3120 | case PTR_TO_PACKET_END: |
d58e468b | 3121 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 3122 | case CONST_PTR_TO_MAP: |
c64b7983 | 3123 | case PTR_TO_SOCKET: |
46f8bc92 | 3124 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 3125 | case PTR_TO_TCP_SOCK: |
fada7fdc | 3126 | case PTR_TO_XDP_SOCK: |
65726b5b | 3127 | case PTR_TO_BTF_ID: |
20b2aff4 | 3128 | case PTR_TO_BUF: |
744ea4e3 | 3129 | case PTR_TO_MEM: |
69c087ba YS |
3130 | case PTR_TO_FUNC: |
3131 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
3132 | return true; |
3133 | default: | |
3134 | return false; | |
3135 | } | |
3136 | } | |
3137 | ||
cc2b14d5 AS |
3138 | /* Does this register contain a constant zero? */ |
3139 | static bool register_is_null(struct bpf_reg_state *reg) | |
3140 | { | |
3141 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
3142 | } | |
3143 | ||
f7cf25b2 AS |
3144 | static bool register_is_const(struct bpf_reg_state *reg) |
3145 | { | |
3146 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
3147 | } | |
3148 | ||
5689d49b YS |
3149 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
3150 | { | |
3151 | return tnum_is_unknown(reg->var_off) && | |
3152 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
3153 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
3154 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
3155 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
3156 | } | |
3157 | ||
3158 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
3159 | { | |
3160 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
3161 | } | |
3162 | ||
6e7e63cb JH |
3163 | static bool __is_pointer_value(bool allow_ptr_leaks, |
3164 | const struct bpf_reg_state *reg) | |
3165 | { | |
3166 | if (allow_ptr_leaks) | |
3167 | return false; | |
3168 | ||
3169 | return reg->type != SCALAR_VALUE; | |
3170 | } | |
3171 | ||
f7cf25b2 | 3172 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
3173 | int spi, struct bpf_reg_state *reg, |
3174 | int size) | |
f7cf25b2 AS |
3175 | { |
3176 | int i; | |
3177 | ||
3178 | state->stack[spi].spilled_ptr = *reg; | |
354e8f19 MKL |
3179 | if (size == BPF_REG_SIZE) |
3180 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 3181 | |
354e8f19 MKL |
3182 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
3183 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 3184 | |
354e8f19 MKL |
3185 | /* size < 8 bytes spill */ |
3186 | for (; i; i--) | |
3187 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
3188 | } |
3189 | ||
01f810ac | 3190 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
3191 | * stack boundary and alignment are checked in check_mem_access() |
3192 | */ | |
01f810ac AM |
3193 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
3194 | /* stack frame we're writing to */ | |
3195 | struct bpf_func_state *state, | |
3196 | int off, int size, int value_regno, | |
3197 | int insn_idx) | |
17a52670 | 3198 | { |
f4d7e40a | 3199 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 3200 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 3201 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 3202 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 3203 | |
c69431aa | 3204 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
3205 | if (err) |
3206 | return err; | |
9c399760 AS |
3207 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
3208 | * so it's aligned access and [off, off + size) are within stack limits | |
3209 | */ | |
638f5b90 AS |
3210 | if (!env->allow_ptr_leaks && |
3211 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
3212 | size != BPF_REG_SIZE) { | |
3213 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
3214 | return -EACCES; | |
3215 | } | |
17a52670 | 3216 | |
f4d7e40a | 3217 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
3218 | if (value_regno >= 0) |
3219 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
3220 | if (!env->bypass_spec_v4) { |
3221 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
3222 | ||
3223 | for (i = 0; i < size; i++) { | |
3224 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { | |
3225 | sanitize = true; | |
3226 | break; | |
3227 | } | |
3228 | } | |
3229 | ||
3230 | if (sanitize) | |
3231 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
3232 | } | |
17a52670 | 3233 | |
0f55f9ed | 3234 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 3235 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 3236 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
3237 | if (dst_reg != BPF_REG_FP) { |
3238 | /* The backtracking logic can only recognize explicit | |
3239 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 3240 | * scalar via different register has to be conservative. |
b5dc0163 AS |
3241 | * Backtrack from here and mark all registers as precise |
3242 | * that contributed into 'reg' being a constant. | |
3243 | */ | |
3244 | err = mark_chain_precision(env, value_regno); | |
3245 | if (err) | |
3246 | return err; | |
3247 | } | |
354e8f19 | 3248 | save_register_state(state, spi, reg, size); |
f7cf25b2 | 3249 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 3250 | /* register containing pointer is being spilled into stack */ |
9c399760 | 3251 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 3252 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 3253 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
3254 | return -EACCES; |
3255 | } | |
f7cf25b2 | 3256 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
3257 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
3258 | return -EINVAL; | |
3259 | } | |
354e8f19 | 3260 | save_register_state(state, spi, reg, size); |
9c399760 | 3261 | } else { |
cc2b14d5 AS |
3262 | u8 type = STACK_MISC; |
3263 | ||
679c782d EC |
3264 | /* regular write of data into stack destroys any spilled ptr */ |
3265 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d | 3266 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
27113c59 | 3267 | if (is_spilled_reg(&state->stack[spi])) |
0bae2d4d | 3268 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 3269 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 3270 | |
cc2b14d5 AS |
3271 | /* only mark the slot as written if all 8 bytes were written |
3272 | * otherwise read propagation may incorrectly stop too soon | |
3273 | * when stack slots are partially written. | |
3274 | * This heuristic means that read propagation will be | |
3275 | * conservative, since it will add reg_live_read marks | |
3276 | * to stack slots all the way to first state when programs | |
3277 | * writes+reads less than 8 bytes | |
3278 | */ | |
3279 | if (size == BPF_REG_SIZE) | |
3280 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
3281 | ||
3282 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
3283 | if (reg && register_is_null(reg)) { |
3284 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3285 | err = mark_chain_precision(env, value_regno); | |
3286 | if (err) | |
3287 | return err; | |
cc2b14d5 | 3288 | type = STACK_ZERO; |
b5dc0163 | 3289 | } |
cc2b14d5 | 3290 | |
0bae2d4d | 3291 | /* Mark slots affected by this stack write. */ |
9c399760 | 3292 | for (i = 0; i < size; i++) |
638f5b90 | 3293 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 3294 | type; |
17a52670 AS |
3295 | } |
3296 | return 0; | |
3297 | } | |
3298 | ||
01f810ac AM |
3299 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
3300 | * known to contain a variable offset. | |
3301 | * This function checks whether the write is permitted and conservatively | |
3302 | * tracks the effects of the write, considering that each stack slot in the | |
3303 | * dynamic range is potentially written to. | |
3304 | * | |
3305 | * 'off' includes 'regno->off'. | |
3306 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
3307 | * the stack. | |
3308 | * | |
3309 | * Spilled pointers in range are not marked as written because we don't know | |
3310 | * what's going to be actually written. This means that read propagation for | |
3311 | * future reads cannot be terminated by this write. | |
3312 | * | |
3313 | * For privileged programs, uninitialized stack slots are considered | |
3314 | * initialized by this write (even though we don't know exactly what offsets | |
3315 | * are going to be written to). The idea is that we don't want the verifier to | |
3316 | * reject future reads that access slots written to through variable offsets. | |
3317 | */ | |
3318 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
3319 | /* func where register points to */ | |
3320 | struct bpf_func_state *state, | |
3321 | int ptr_regno, int off, int size, | |
3322 | int value_regno, int insn_idx) | |
3323 | { | |
3324 | struct bpf_func_state *cur; /* state of the current function */ | |
3325 | int min_off, max_off; | |
3326 | int i, err; | |
3327 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
3328 | bool writing_zero = false; | |
3329 | /* set if the fact that we're writing a zero is used to let any | |
3330 | * stack slots remain STACK_ZERO | |
3331 | */ | |
3332 | bool zero_used = false; | |
3333 | ||
3334 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
3335 | ptr_reg = &cur->regs[ptr_regno]; | |
3336 | min_off = ptr_reg->smin_value + off; | |
3337 | max_off = ptr_reg->smax_value + off + size; | |
3338 | if (value_regno >= 0) | |
3339 | value_reg = &cur->regs[value_regno]; | |
3340 | if (value_reg && register_is_null(value_reg)) | |
3341 | writing_zero = true; | |
3342 | ||
c69431aa | 3343 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
3344 | if (err) |
3345 | return err; | |
3346 | ||
3347 | ||
3348 | /* Variable offset writes destroy any spilled pointers in range. */ | |
3349 | for (i = min_off; i < max_off; i++) { | |
3350 | u8 new_type, *stype; | |
3351 | int slot, spi; | |
3352 | ||
3353 | slot = -i - 1; | |
3354 | spi = slot / BPF_REG_SIZE; | |
3355 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 3356 | mark_stack_slot_scratched(env, spi); |
01f810ac | 3357 | |
f5e477a8 KKD |
3358 | if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) { |
3359 | /* Reject the write if range we may write to has not | |
3360 | * been initialized beforehand. If we didn't reject | |
3361 | * here, the ptr status would be erased below (even | |
3362 | * though not all slots are actually overwritten), | |
3363 | * possibly opening the door to leaks. | |
3364 | * | |
3365 | * We do however catch STACK_INVALID case below, and | |
3366 | * only allow reading possibly uninitialized memory | |
3367 | * later for CAP_PERFMON, as the write may not happen to | |
3368 | * that slot. | |
01f810ac AM |
3369 | */ |
3370 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
3371 | insn_idx, i); | |
3372 | return -EINVAL; | |
3373 | } | |
3374 | ||
3375 | /* Erase all spilled pointers. */ | |
3376 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
3377 | ||
3378 | /* Update the slot type. */ | |
3379 | new_type = STACK_MISC; | |
3380 | if (writing_zero && *stype == STACK_ZERO) { | |
3381 | new_type = STACK_ZERO; | |
3382 | zero_used = true; | |
3383 | } | |
3384 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
3385 | * pretend that it will be initialized by this write. The slot | |
3386 | * might not actually be written to, and so if we mark it as | |
3387 | * initialized future reads might leak uninitialized memory. | |
3388 | * For privileged programs, we will accept such reads to slots | |
3389 | * that may or may not be written because, if we're reject | |
3390 | * them, the error would be too confusing. | |
3391 | */ | |
3392 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
3393 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
3394 | insn_idx, i); | |
3395 | return -EINVAL; | |
3396 | } | |
3397 | *stype = new_type; | |
3398 | } | |
3399 | if (zero_used) { | |
3400 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3401 | err = mark_chain_precision(env, value_regno); | |
3402 | if (err) | |
3403 | return err; | |
3404 | } | |
3405 | return 0; | |
3406 | } | |
3407 | ||
3408 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
3409 | * max_off), we set the register's type according to the types of the | |
3410 | * respective stack slots. If all the stack values are known to be zeros, then | |
3411 | * so is the destination reg. Otherwise, the register is considered to be | |
3412 | * SCALAR. This function does not deal with register filling; the caller must | |
3413 | * ensure that all spilled registers in the stack range have been marked as | |
3414 | * read. | |
3415 | */ | |
3416 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
3417 | /* func where src register points to */ | |
3418 | struct bpf_func_state *ptr_state, | |
3419 | int min_off, int max_off, int dst_regno) | |
3420 | { | |
3421 | struct bpf_verifier_state *vstate = env->cur_state; | |
3422 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3423 | int i, slot, spi; | |
3424 | u8 *stype; | |
3425 | int zeros = 0; | |
3426 | ||
3427 | for (i = min_off; i < max_off; i++) { | |
3428 | slot = -i - 1; | |
3429 | spi = slot / BPF_REG_SIZE; | |
3430 | stype = ptr_state->stack[spi].slot_type; | |
3431 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
3432 | break; | |
3433 | zeros++; | |
3434 | } | |
3435 | if (zeros == max_off - min_off) { | |
3436 | /* any access_size read into register is zero extended, | |
3437 | * so the whole register == const_zero | |
3438 | */ | |
3439 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
3440 | /* backtracking doesn't support STACK_ZERO yet, | |
3441 | * so mark it precise here, so that later | |
3442 | * backtracking can stop here. | |
3443 | * Backtracking may not need this if this register | |
3444 | * doesn't participate in pointer adjustment. | |
3445 | * Forward propagation of precise flag is not | |
3446 | * necessary either. This mark is only to stop | |
3447 | * backtracking. Any register that contributed | |
3448 | * to const 0 was marked precise before spill. | |
3449 | */ | |
3450 | state->regs[dst_regno].precise = true; | |
3451 | } else { | |
3452 | /* have read misc data from the stack */ | |
3453 | mark_reg_unknown(env, state->regs, dst_regno); | |
3454 | } | |
3455 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
3456 | } | |
3457 | ||
3458 | /* Read the stack at 'off' and put the results into the register indicated by | |
3459 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
3460 | * spilled reg. | |
3461 | * | |
3462 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
3463 | * register. | |
3464 | * | |
3465 | * The access is assumed to be within the current stack bounds. | |
3466 | */ | |
3467 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
3468 | /* func where src register points to */ | |
3469 | struct bpf_func_state *reg_state, | |
3470 | int off, int size, int dst_regno) | |
17a52670 | 3471 | { |
f4d7e40a AS |
3472 | struct bpf_verifier_state *vstate = env->cur_state; |
3473 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 3474 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 3475 | struct bpf_reg_state *reg; |
354e8f19 | 3476 | u8 *stype, type; |
17a52670 | 3477 | |
f4d7e40a | 3478 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 3479 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 3480 | |
27113c59 | 3481 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
3482 | u8 spill_size = 1; |
3483 | ||
3484 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
3485 | spill_size++; | |
354e8f19 | 3486 | |
f30d4968 | 3487 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
3488 | if (reg->type != SCALAR_VALUE) { |
3489 | verbose_linfo(env, env->insn_idx, "; "); | |
3490 | verbose(env, "invalid size of register fill\n"); | |
3491 | return -EACCES; | |
3492 | } | |
354e8f19 MKL |
3493 | |
3494 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
3495 | if (dst_regno < 0) | |
3496 | return 0; | |
3497 | ||
f30d4968 | 3498 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
3499 | /* The earlier check_reg_arg() has decided the |
3500 | * subreg_def for this insn. Save it first. | |
3501 | */ | |
3502 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
3503 | ||
3504 | state->regs[dst_regno] = *reg; | |
3505 | state->regs[dst_regno].subreg_def = subreg_def; | |
3506 | } else { | |
3507 | for (i = 0; i < size; i++) { | |
3508 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
3509 | if (type == STACK_SPILL) | |
3510 | continue; | |
3511 | if (type == STACK_MISC) | |
3512 | continue; | |
3513 | verbose(env, "invalid read from stack off %d+%d size %d\n", | |
3514 | off, i, size); | |
3515 | return -EACCES; | |
3516 | } | |
01f810ac | 3517 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 3518 | } |
354e8f19 | 3519 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 3520 | return 0; |
17a52670 | 3521 | } |
17a52670 | 3522 | |
01f810ac | 3523 | if (dst_regno >= 0) { |
17a52670 | 3524 | /* restore register state from stack */ |
01f810ac | 3525 | state->regs[dst_regno] = *reg; |
2f18f62e AS |
3526 | /* mark reg as written since spilled pointer state likely |
3527 | * has its liveness marks cleared by is_state_visited() | |
3528 | * which resets stack/reg liveness for state transitions | |
3529 | */ | |
01f810ac | 3530 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 3531 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 3532 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
3533 | * it is acceptable to use this value as a SCALAR_VALUE |
3534 | * (e.g. for XADD). | |
3535 | * We must not allow unprivileged callers to do that | |
3536 | * with spilled pointers. | |
3537 | */ | |
3538 | verbose(env, "leaking pointer from stack off %d\n", | |
3539 | off); | |
3540 | return -EACCES; | |
dc503a8a | 3541 | } |
f7cf25b2 | 3542 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
3543 | } else { |
3544 | for (i = 0; i < size; i++) { | |
01f810ac AM |
3545 | type = stype[(slot - i) % BPF_REG_SIZE]; |
3546 | if (type == STACK_MISC) | |
cc2b14d5 | 3547 | continue; |
01f810ac | 3548 | if (type == STACK_ZERO) |
cc2b14d5 | 3549 | continue; |
cc2b14d5 AS |
3550 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
3551 | off, i, size); | |
3552 | return -EACCES; | |
3553 | } | |
f7cf25b2 | 3554 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
3555 | if (dst_regno >= 0) |
3556 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 3557 | } |
f7cf25b2 | 3558 | return 0; |
17a52670 AS |
3559 | } |
3560 | ||
61df10c7 | 3561 | enum bpf_access_src { |
01f810ac AM |
3562 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
3563 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
3564 | }; | |
3565 | ||
3566 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
3567 | int regno, int off, int access_size, | |
3568 | bool zero_size_allowed, | |
61df10c7 | 3569 | enum bpf_access_src type, |
01f810ac AM |
3570 | struct bpf_call_arg_meta *meta); |
3571 | ||
3572 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
3573 | { | |
3574 | return cur_regs(env) + regno; | |
3575 | } | |
3576 | ||
3577 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
3578 | * 'dst_regno'. | |
3579 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
3580 | * but not its variable offset. | |
3581 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
3582 | * | |
3583 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
3584 | * filling registers (i.e. reads of spilled register cannot be detected when | |
3585 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
3586 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
3587 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
3588 | * instead. | |
3589 | */ | |
3590 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
3591 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 3592 | { |
01f810ac AM |
3593 | /* The state of the source register. */ |
3594 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3595 | struct bpf_func_state *ptr_state = func(env, reg); | |
3596 | int err; | |
3597 | int min_off, max_off; | |
3598 | ||
3599 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 3600 | */ |
01f810ac AM |
3601 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
3602 | false, ACCESS_DIRECT, NULL); | |
3603 | if (err) | |
3604 | return err; | |
3605 | ||
3606 | min_off = reg->smin_value + off; | |
3607 | max_off = reg->smax_value + off; | |
3608 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
3609 | return 0; | |
3610 | } | |
3611 | ||
3612 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
3613 | * check_stack_read_var_off. | |
3614 | * | |
3615 | * The caller must ensure that the offset falls within the allocated stack | |
3616 | * bounds. | |
3617 | * | |
3618 | * 'dst_regno' is a register which will receive the value from the stack. It | |
3619 | * can be -1, meaning that the read value is not going to a register. | |
3620 | */ | |
3621 | static int check_stack_read(struct bpf_verifier_env *env, | |
3622 | int ptr_regno, int off, int size, | |
3623 | int dst_regno) | |
3624 | { | |
3625 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3626 | struct bpf_func_state *state = func(env, reg); | |
3627 | int err; | |
3628 | /* Some accesses are only permitted with a static offset. */ | |
3629 | bool var_off = !tnum_is_const(reg->var_off); | |
3630 | ||
3631 | /* The offset is required to be static when reads don't go to a | |
3632 | * register, in order to not leak pointers (see | |
3633 | * check_stack_read_fixed_off). | |
3634 | */ | |
3635 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
3636 | char tn_buf[48]; |
3637 | ||
3638 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 3639 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
3640 | tn_buf, off, size); |
3641 | return -EACCES; | |
3642 | } | |
01f810ac AM |
3643 | /* Variable offset is prohibited for unprivileged mode for simplicity |
3644 | * since it requires corresponding support in Spectre masking for stack | |
3645 | * ALU. See also retrieve_ptr_limit(). | |
3646 | */ | |
3647 | if (!env->bypass_spec_v1 && var_off) { | |
3648 | char tn_buf[48]; | |
e4298d25 | 3649 | |
01f810ac AM |
3650 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3651 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", | |
3652 | ptr_regno, tn_buf); | |
e4298d25 DB |
3653 | return -EACCES; |
3654 | } | |
3655 | ||
01f810ac AM |
3656 | if (!var_off) { |
3657 | off += reg->var_off.value; | |
3658 | err = check_stack_read_fixed_off(env, state, off, size, | |
3659 | dst_regno); | |
3660 | } else { | |
3661 | /* Variable offset stack reads need more conservative handling | |
3662 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
3663 | * branch. | |
3664 | */ | |
3665 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
3666 | dst_regno); | |
3667 | } | |
3668 | return err; | |
3669 | } | |
3670 | ||
3671 | ||
3672 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
3673 | * check_stack_write_var_off. | |
3674 | * | |
3675 | * 'ptr_regno' is the register used as a pointer into the stack. | |
3676 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
3677 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
3678 | * be -1, meaning that we're not writing from a register. | |
3679 | * | |
3680 | * The caller must ensure that the offset falls within the maximum stack size. | |
3681 | */ | |
3682 | static int check_stack_write(struct bpf_verifier_env *env, | |
3683 | int ptr_regno, int off, int size, | |
3684 | int value_regno, int insn_idx) | |
3685 | { | |
3686 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3687 | struct bpf_func_state *state = func(env, reg); | |
3688 | int err; | |
3689 | ||
3690 | if (tnum_is_const(reg->var_off)) { | |
3691 | off += reg->var_off.value; | |
3692 | err = check_stack_write_fixed_off(env, state, off, size, | |
3693 | value_regno, insn_idx); | |
3694 | } else { | |
3695 | /* Variable offset stack reads need more conservative handling | |
3696 | * than fixed offset ones. | |
3697 | */ | |
3698 | err = check_stack_write_var_off(env, state, | |
3699 | ptr_regno, off, size, | |
3700 | value_regno, insn_idx); | |
3701 | } | |
3702 | return err; | |
e4298d25 DB |
3703 | } |
3704 | ||
591fe988 DB |
3705 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
3706 | int off, int size, enum bpf_access_type type) | |
3707 | { | |
3708 | struct bpf_reg_state *regs = cur_regs(env); | |
3709 | struct bpf_map *map = regs[regno].map_ptr; | |
3710 | u32 cap = bpf_map_flags_to_cap(map); | |
3711 | ||
3712 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
3713 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
3714 | map->value_size, off, size); | |
3715 | return -EACCES; | |
3716 | } | |
3717 | ||
3718 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
3719 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
3720 | map->value_size, off, size); | |
3721 | return -EACCES; | |
3722 | } | |
3723 | ||
3724 | return 0; | |
3725 | } | |
3726 | ||
457f4436 AN |
3727 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
3728 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
3729 | int off, int size, u32 mem_size, | |
3730 | bool zero_size_allowed) | |
17a52670 | 3731 | { |
457f4436 AN |
3732 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
3733 | struct bpf_reg_state *reg; | |
3734 | ||
3735 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
3736 | return 0; | |
17a52670 | 3737 | |
457f4436 AN |
3738 | reg = &cur_regs(env)[regno]; |
3739 | switch (reg->type) { | |
69c087ba YS |
3740 | case PTR_TO_MAP_KEY: |
3741 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
3742 | mem_size, off, size); | |
3743 | break; | |
457f4436 | 3744 | case PTR_TO_MAP_VALUE: |
61bd5218 | 3745 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
3746 | mem_size, off, size); |
3747 | break; | |
3748 | case PTR_TO_PACKET: | |
3749 | case PTR_TO_PACKET_META: | |
3750 | case PTR_TO_PACKET_END: | |
3751 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
3752 | off, size, regno, reg->id, off, mem_size); | |
3753 | break; | |
3754 | case PTR_TO_MEM: | |
3755 | default: | |
3756 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
3757 | mem_size, off, size); | |
17a52670 | 3758 | } |
457f4436 AN |
3759 | |
3760 | return -EACCES; | |
17a52670 AS |
3761 | } |
3762 | ||
457f4436 AN |
3763 | /* check read/write into a memory region with possible variable offset */ |
3764 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
3765 | int off, int size, u32 mem_size, | |
3766 | bool zero_size_allowed) | |
dbcfe5f7 | 3767 | { |
f4d7e40a AS |
3768 | struct bpf_verifier_state *vstate = env->cur_state; |
3769 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
3770 | struct bpf_reg_state *reg = &state->regs[regno]; |
3771 | int err; | |
3772 | ||
457f4436 | 3773 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
3774 | * need to try adding each of min_value and max_value to off |
3775 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
3776 | * |
3777 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
3778 | * comparisons where we can't assume the floor of a |
3779 | * value is 0. If we are using signed variables for our | |
3780 | * index'es we need to make sure that whatever we use | |
3781 | * will have a set floor within our range. | |
3782 | */ | |
b7137c4e DB |
3783 | if (reg->smin_value < 0 && |
3784 | (reg->smin_value == S64_MIN || | |
3785 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
3786 | reg->smin_value + off < 0)) { | |
61bd5218 | 3787 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
3788 | regno); |
3789 | return -EACCES; | |
3790 | } | |
457f4436 AN |
3791 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
3792 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 3793 | if (err) { |
457f4436 | 3794 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 3795 | regno); |
dbcfe5f7 GB |
3796 | return err; |
3797 | } | |
3798 | ||
b03c9f9f EC |
3799 | /* If we haven't set a max value then we need to bail since we can't be |
3800 | * sure we won't do bad things. | |
3801 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 3802 | */ |
b03c9f9f | 3803 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 3804 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
3805 | regno); |
3806 | return -EACCES; | |
3807 | } | |
457f4436 AN |
3808 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
3809 | mem_size, zero_size_allowed); | |
3810 | if (err) { | |
3811 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 3812 | regno); |
457f4436 AN |
3813 | return err; |
3814 | } | |
3815 | ||
3816 | return 0; | |
3817 | } | |
d83525ca | 3818 | |
e9147b44 KKD |
3819 | static int __check_ptr_off_reg(struct bpf_verifier_env *env, |
3820 | const struct bpf_reg_state *reg, int regno, | |
3821 | bool fixed_off_ok) | |
3822 | { | |
3823 | /* Access to this pointer-typed register or passing it to a helper | |
3824 | * is only allowed in its original, unmodified form. | |
3825 | */ | |
3826 | ||
3827 | if (reg->off < 0) { | |
3828 | verbose(env, "negative offset %s ptr R%d off=%d disallowed\n", | |
3829 | reg_type_str(env, reg->type), regno, reg->off); | |
3830 | return -EACCES; | |
3831 | } | |
3832 | ||
3833 | if (!fixed_off_ok && reg->off) { | |
3834 | verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n", | |
3835 | reg_type_str(env, reg->type), regno, reg->off); | |
3836 | return -EACCES; | |
3837 | } | |
3838 | ||
3839 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3840 | char tn_buf[48]; | |
3841 | ||
3842 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3843 | verbose(env, "variable %s access var_off=%s disallowed\n", | |
3844 | reg_type_str(env, reg->type), tn_buf); | |
3845 | return -EACCES; | |
3846 | } | |
3847 | ||
3848 | return 0; | |
3849 | } | |
3850 | ||
3851 | int check_ptr_off_reg(struct bpf_verifier_env *env, | |
3852 | const struct bpf_reg_state *reg, int regno) | |
3853 | { | |
3854 | return __check_ptr_off_reg(env, reg, regno, false); | |
3855 | } | |
3856 | ||
61df10c7 | 3857 | static int map_kptr_match_type(struct bpf_verifier_env *env, |
aa3496ac | 3858 | struct btf_field *kptr_field, |
61df10c7 KKD |
3859 | struct bpf_reg_state *reg, u32 regno) |
3860 | { | |
aa3496ac | 3861 | const char *targ_name = kernel_type_name(kptr_field->kptr.btf, kptr_field->kptr.btf_id); |
6efe152d | 3862 | int perm_flags = PTR_MAYBE_NULL; |
61df10c7 KKD |
3863 | const char *reg_name = ""; |
3864 | ||
6efe152d | 3865 | /* Only unreferenced case accepts untrusted pointers */ |
aa3496ac | 3866 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
3867 | perm_flags |= PTR_UNTRUSTED; |
3868 | ||
3869 | if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags)) | |
61df10c7 KKD |
3870 | goto bad_type; |
3871 | ||
3872 | if (!btf_is_kernel(reg->btf)) { | |
3873 | verbose(env, "R%d must point to kernel BTF\n", regno); | |
3874 | return -EINVAL; | |
3875 | } | |
3876 | /* We need to verify reg->type and reg->btf, before accessing reg->btf */ | |
3877 | reg_name = kernel_type_name(reg->btf, reg->btf_id); | |
3878 | ||
c0a5a21c KKD |
3879 | /* For ref_ptr case, release function check should ensure we get one |
3880 | * referenced PTR_TO_BTF_ID, and that its fixed offset is 0. For the | |
3881 | * normal store of unreferenced kptr, we must ensure var_off is zero. | |
3882 | * Since ref_ptr cannot be accessed directly by BPF insns, checks for | |
3883 | * reg->off and reg->ref_obj_id are not needed here. | |
3884 | */ | |
61df10c7 KKD |
3885 | if (__check_ptr_off_reg(env, reg, regno, true)) |
3886 | return -EACCES; | |
3887 | ||
3888 | /* A full type match is needed, as BTF can be vmlinux or module BTF, and | |
3889 | * we also need to take into account the reg->off. | |
3890 | * | |
3891 | * We want to support cases like: | |
3892 | * | |
3893 | * struct foo { | |
3894 | * struct bar br; | |
3895 | * struct baz bz; | |
3896 | * }; | |
3897 | * | |
3898 | * struct foo *v; | |
3899 | * v = func(); // PTR_TO_BTF_ID | |
3900 | * val->foo = v; // reg->off is zero, btf and btf_id match type | |
3901 | * val->bar = &v->br; // reg->off is still zero, but we need to retry with | |
3902 | * // first member type of struct after comparison fails | |
3903 | * val->baz = &v->bz; // reg->off is non-zero, so struct needs to be walked | |
3904 | * // to match type | |
3905 | * | |
3906 | * In the kptr_ref case, check_func_arg_reg_off already ensures reg->off | |
2ab3b380 KKD |
3907 | * is zero. We must also ensure that btf_struct_ids_match does not walk |
3908 | * the struct to match type against first member of struct, i.e. reject | |
3909 | * second case from above. Hence, when type is BPF_KPTR_REF, we set | |
3910 | * strict mode to true for type match. | |
61df10c7 KKD |
3911 | */ |
3912 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
aa3496ac KKD |
3913 | kptr_field->kptr.btf, kptr_field->kptr.btf_id, |
3914 | kptr_field->type == BPF_KPTR_REF)) | |
61df10c7 KKD |
3915 | goto bad_type; |
3916 | return 0; | |
3917 | bad_type: | |
3918 | verbose(env, "invalid kptr access, R%d type=%s%s ", regno, | |
3919 | reg_type_str(env, reg->type), reg_name); | |
6efe152d | 3920 | verbose(env, "expected=%s%s", reg_type_str(env, PTR_TO_BTF_ID), targ_name); |
aa3496ac | 3921 | if (kptr_field->type == BPF_KPTR_UNREF) |
6efe152d KKD |
3922 | verbose(env, " or %s%s\n", reg_type_str(env, PTR_TO_BTF_ID | PTR_UNTRUSTED), |
3923 | targ_name); | |
3924 | else | |
3925 | verbose(env, "\n"); | |
61df10c7 KKD |
3926 | return -EINVAL; |
3927 | } | |
3928 | ||
3929 | static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, | |
3930 | int value_regno, int insn_idx, | |
aa3496ac | 3931 | struct btf_field *kptr_field) |
61df10c7 KKD |
3932 | { |
3933 | struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; | |
3934 | int class = BPF_CLASS(insn->code); | |
3935 | struct bpf_reg_state *val_reg; | |
3936 | ||
3937 | /* Things we already checked for in check_map_access and caller: | |
3938 | * - Reject cases where variable offset may touch kptr | |
3939 | * - size of access (must be BPF_DW) | |
3940 | * - tnum_is_const(reg->var_off) | |
aa3496ac | 3941 | * - kptr_field->offset == off + reg->var_off.value |
61df10c7 KKD |
3942 | */ |
3943 | /* Only BPF_[LDX,STX,ST] | BPF_MEM | BPF_DW is supported */ | |
3944 | if (BPF_MODE(insn->code) != BPF_MEM) { | |
3945 | verbose(env, "kptr in map can only be accessed using BPF_MEM instruction mode\n"); | |
3946 | return -EACCES; | |
3947 | } | |
3948 | ||
6efe152d KKD |
3949 | /* We only allow loading referenced kptr, since it will be marked as |
3950 | * untrusted, similar to unreferenced kptr. | |
3951 | */ | |
aa3496ac | 3952 | if (class != BPF_LDX && kptr_field->type == BPF_KPTR_REF) { |
6efe152d | 3953 | verbose(env, "store to referenced kptr disallowed\n"); |
c0a5a21c KKD |
3954 | return -EACCES; |
3955 | } | |
3956 | ||
61df10c7 KKD |
3957 | if (class == BPF_LDX) { |
3958 | val_reg = reg_state(env, value_regno); | |
3959 | /* We can simply mark the value_regno receiving the pointer | |
3960 | * value from map as PTR_TO_BTF_ID, with the correct type. | |
3961 | */ | |
aa3496ac KKD |
3962 | mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, kptr_field->kptr.btf, |
3963 | kptr_field->kptr.btf_id, PTR_MAYBE_NULL | PTR_UNTRUSTED); | |
61df10c7 KKD |
3964 | /* For mark_ptr_or_null_reg */ |
3965 | val_reg->id = ++env->id_gen; | |
3966 | } else if (class == BPF_STX) { | |
3967 | val_reg = reg_state(env, value_regno); | |
3968 | if (!register_is_null(val_reg) && | |
aa3496ac | 3969 | map_kptr_match_type(env, kptr_field, val_reg, value_regno)) |
61df10c7 KKD |
3970 | return -EACCES; |
3971 | } else if (class == BPF_ST) { | |
3972 | if (insn->imm) { | |
3973 | verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%u\n", | |
aa3496ac | 3974 | kptr_field->offset); |
61df10c7 KKD |
3975 | return -EACCES; |
3976 | } | |
3977 | } else { | |
3978 | verbose(env, "kptr in map can only be accessed using BPF_LDX/BPF_STX/BPF_ST\n"); | |
3979 | return -EACCES; | |
3980 | } | |
3981 | return 0; | |
3982 | } | |
3983 | ||
457f4436 AN |
3984 | /* check read/write into a map element with possible variable offset */ |
3985 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
61df10c7 KKD |
3986 | int off, int size, bool zero_size_allowed, |
3987 | enum bpf_access_src src) | |
457f4436 AN |
3988 | { |
3989 | struct bpf_verifier_state *vstate = env->cur_state; | |
3990 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3991 | struct bpf_reg_state *reg = &state->regs[regno]; | |
3992 | struct bpf_map *map = reg->map_ptr; | |
aa3496ac KKD |
3993 | struct btf_record *rec; |
3994 | int err, i; | |
457f4436 AN |
3995 | |
3996 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
3997 | zero_size_allowed); | |
3998 | if (err) | |
3999 | return err; | |
4000 | ||
aa3496ac KKD |
4001 | if (IS_ERR_OR_NULL(map->record)) |
4002 | return 0; | |
4003 | rec = map->record; | |
4004 | for (i = 0; i < rec->cnt; i++) { | |
4005 | struct btf_field *field = &rec->fields[i]; | |
4006 | u32 p = field->offset; | |
d83525ca | 4007 | |
db559117 KKD |
4008 | /* If any part of a field can be touched by load/store, reject |
4009 | * this program. To check that [x1, x2) overlaps with [y1, y2), | |
d83525ca AS |
4010 | * it is sufficient to check x1 < y2 && y1 < x2. |
4011 | */ | |
aa3496ac KKD |
4012 | if (reg->smin_value + off < p + btf_field_type_size(field->type) && |
4013 | p < reg->umax_value + off + size) { | |
4014 | switch (field->type) { | |
4015 | case BPF_KPTR_UNREF: | |
4016 | case BPF_KPTR_REF: | |
61df10c7 KKD |
4017 | if (src != ACCESS_DIRECT) { |
4018 | verbose(env, "kptr cannot be accessed indirectly by helper\n"); | |
4019 | return -EACCES; | |
4020 | } | |
4021 | if (!tnum_is_const(reg->var_off)) { | |
4022 | verbose(env, "kptr access cannot have variable offset\n"); | |
4023 | return -EACCES; | |
4024 | } | |
4025 | if (p != off + reg->var_off.value) { | |
4026 | verbose(env, "kptr access misaligned expected=%u off=%llu\n", | |
4027 | p, off + reg->var_off.value); | |
4028 | return -EACCES; | |
4029 | } | |
4030 | if (size != bpf_size_to_bytes(BPF_DW)) { | |
4031 | verbose(env, "kptr access size must be BPF_DW\n"); | |
4032 | return -EACCES; | |
4033 | } | |
4034 | break; | |
aa3496ac | 4035 | default: |
db559117 KKD |
4036 | verbose(env, "%s cannot be accessed directly by load/store\n", |
4037 | btf_field_type_name(field->type)); | |
aa3496ac | 4038 | return -EACCES; |
61df10c7 KKD |
4039 | } |
4040 | } | |
4041 | } | |
aa3496ac | 4042 | return 0; |
dbcfe5f7 GB |
4043 | } |
4044 | ||
969bf05e AS |
4045 | #define MAX_PACKET_OFF 0xffff |
4046 | ||
58e2af8b | 4047 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
4048 | const struct bpf_call_arg_meta *meta, |
4049 | enum bpf_access_type t) | |
4acf6c0b | 4050 | { |
7e40781c UP |
4051 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
4052 | ||
4053 | switch (prog_type) { | |
5d66fa7d | 4054 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
4055 | case BPF_PROG_TYPE_LWT_IN: |
4056 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 4057 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 4058 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 4059 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 4060 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
4061 | if (t == BPF_WRITE) |
4062 | return false; | |
8731745e | 4063 | fallthrough; |
5d66fa7d DB |
4064 | |
4065 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
4066 | case BPF_PROG_TYPE_SCHED_CLS: |
4067 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 4068 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 4069 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 4070 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 4071 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
4072 | if (meta) |
4073 | return meta->pkt_access; | |
4074 | ||
4075 | env->seen_direct_write = true; | |
4acf6c0b | 4076 | return true; |
0d01da6a SF |
4077 | |
4078 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
4079 | if (t == BPF_WRITE) | |
4080 | env->seen_direct_write = true; | |
4081 | ||
4082 | return true; | |
4083 | ||
4acf6c0b BB |
4084 | default: |
4085 | return false; | |
4086 | } | |
4087 | } | |
4088 | ||
f1174f77 | 4089 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 4090 | int size, bool zero_size_allowed) |
f1174f77 | 4091 | { |
638f5b90 | 4092 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
4093 | struct bpf_reg_state *reg = ®s[regno]; |
4094 | int err; | |
4095 | ||
4096 | /* We may have added a variable offset to the packet pointer; but any | |
4097 | * reg->range we have comes after that. We are only checking the fixed | |
4098 | * offset. | |
4099 | */ | |
4100 | ||
4101 | /* We don't allow negative numbers, because we aren't tracking enough | |
4102 | * detail to prove they're safe. | |
4103 | */ | |
b03c9f9f | 4104 | if (reg->smin_value < 0) { |
61bd5218 | 4105 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
4106 | regno); |
4107 | return -EACCES; | |
4108 | } | |
6d94e741 AS |
4109 | |
4110 | err = reg->range < 0 ? -EINVAL : | |
4111 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 4112 | zero_size_allowed); |
f1174f77 | 4113 | if (err) { |
61bd5218 | 4114 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
4115 | return err; |
4116 | } | |
e647815a | 4117 | |
457f4436 | 4118 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
4119 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
4120 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 4121 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
4122 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
4123 | */ | |
4124 | env->prog->aux->max_pkt_offset = | |
4125 | max_t(u32, env->prog->aux->max_pkt_offset, | |
4126 | off + reg->umax_value + size - 1); | |
4127 | ||
f1174f77 EC |
4128 | return err; |
4129 | } | |
4130 | ||
4131 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 4132 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 4133 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 4134 | struct btf **btf, u32 *btf_id) |
17a52670 | 4135 | { |
f96da094 DB |
4136 | struct bpf_insn_access_aux info = { |
4137 | .reg_type = *reg_type, | |
9e15db66 | 4138 | .log = &env->log, |
f96da094 | 4139 | }; |
31fd8581 | 4140 | |
4f9218aa | 4141 | if (env->ops->is_valid_access && |
5e43f899 | 4142 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
4143 | /* A non zero info.ctx_field_size indicates that this field is a |
4144 | * candidate for later verifier transformation to load the whole | |
4145 | * field and then apply a mask when accessed with a narrower | |
4146 | * access than actual ctx access size. A zero info.ctx_field_size | |
4147 | * will only allow for whole field access and rejects any other | |
4148 | * type of narrower access. | |
31fd8581 | 4149 | */ |
23994631 | 4150 | *reg_type = info.reg_type; |
31fd8581 | 4151 | |
c25b2ae1 | 4152 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 4153 | *btf = info.btf; |
9e15db66 | 4154 | *btf_id = info.btf_id; |
22dc4a0f | 4155 | } else { |
9e15db66 | 4156 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 4157 | } |
32bbe007 AS |
4158 | /* remember the offset of last byte accessed in ctx */ |
4159 | if (env->prog->aux->max_ctx_offset < off + size) | |
4160 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 4161 | return 0; |
32bbe007 | 4162 | } |
17a52670 | 4163 | |
61bd5218 | 4164 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
4165 | return -EACCES; |
4166 | } | |
4167 | ||
d58e468b PP |
4168 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
4169 | int size) | |
4170 | { | |
4171 | if (size < 0 || off < 0 || | |
4172 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
4173 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
4174 | off, size); | |
4175 | return -EACCES; | |
4176 | } | |
4177 | return 0; | |
4178 | } | |
4179 | ||
5f456649 MKL |
4180 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
4181 | u32 regno, int off, int size, | |
4182 | enum bpf_access_type t) | |
c64b7983 JS |
4183 | { |
4184 | struct bpf_reg_state *regs = cur_regs(env); | |
4185 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 4186 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 4187 | bool valid; |
c64b7983 JS |
4188 | |
4189 | if (reg->smin_value < 0) { | |
4190 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
4191 | regno); | |
4192 | return -EACCES; | |
4193 | } | |
4194 | ||
46f8bc92 MKL |
4195 | switch (reg->type) { |
4196 | case PTR_TO_SOCK_COMMON: | |
4197 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
4198 | break; | |
4199 | case PTR_TO_SOCKET: | |
4200 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
4201 | break; | |
655a51e5 MKL |
4202 | case PTR_TO_TCP_SOCK: |
4203 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
4204 | break; | |
fada7fdc JL |
4205 | case PTR_TO_XDP_SOCK: |
4206 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
4207 | break; | |
46f8bc92 MKL |
4208 | default: |
4209 | valid = false; | |
c64b7983 JS |
4210 | } |
4211 | ||
5f456649 | 4212 | |
46f8bc92 MKL |
4213 | if (valid) { |
4214 | env->insn_aux_data[insn_idx].ctx_field_size = | |
4215 | info.ctx_field_size; | |
4216 | return 0; | |
4217 | } | |
4218 | ||
4219 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
c25b2ae1 | 4220 | regno, reg_type_str(env, reg->type), off, size); |
46f8bc92 MKL |
4221 | |
4222 | return -EACCES; | |
c64b7983 JS |
4223 | } |
4224 | ||
4cabc5b1 DB |
4225 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
4226 | { | |
2a159c6f | 4227 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
4228 | } |
4229 | ||
f37a8cb8 DB |
4230 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
4231 | { | |
2a159c6f | 4232 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 4233 | |
46f8bc92 MKL |
4234 | return reg->type == PTR_TO_CTX; |
4235 | } | |
4236 | ||
4237 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
4238 | { | |
4239 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4240 | ||
4241 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
4242 | } |
4243 | ||
ca369602 DB |
4244 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
4245 | { | |
2a159c6f | 4246 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
4247 | |
4248 | return type_is_pkt_pointer(reg->type); | |
4249 | } | |
4250 | ||
4b5defde DB |
4251 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
4252 | { | |
4253 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
4254 | ||
4255 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
4256 | return reg->type == PTR_TO_FLOW_KEYS; | |
4257 | } | |
4258 | ||
61bd5218 JK |
4259 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
4260 | const struct bpf_reg_state *reg, | |
d1174416 | 4261 | int off, int size, bool strict) |
969bf05e | 4262 | { |
f1174f77 | 4263 | struct tnum reg_off; |
e07b98d9 | 4264 | int ip_align; |
d1174416 DM |
4265 | |
4266 | /* Byte size accesses are always allowed. */ | |
4267 | if (!strict || size == 1) | |
4268 | return 0; | |
4269 | ||
e4eda884 DM |
4270 | /* For platforms that do not have a Kconfig enabling |
4271 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
4272 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
4273 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
4274 | * to this code only in strict mode where we want to emulate | |
4275 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
4276 | * unconditional IP align value of '2'. | |
e07b98d9 | 4277 | */ |
e4eda884 | 4278 | ip_align = 2; |
f1174f77 EC |
4279 | |
4280 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
4281 | if (!tnum_is_aligned(reg_off, size)) { | |
4282 | char tn_buf[48]; | |
4283 | ||
4284 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
4285 | verbose(env, |
4286 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 4287 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
4288 | return -EACCES; |
4289 | } | |
79adffcd | 4290 | |
969bf05e AS |
4291 | return 0; |
4292 | } | |
4293 | ||
61bd5218 JK |
4294 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
4295 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
4296 | const char *pointer_desc, |
4297 | int off, int size, bool strict) | |
79adffcd | 4298 | { |
f1174f77 EC |
4299 | struct tnum reg_off; |
4300 | ||
4301 | /* Byte size accesses are always allowed. */ | |
4302 | if (!strict || size == 1) | |
4303 | return 0; | |
4304 | ||
4305 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
4306 | if (!tnum_is_aligned(reg_off, size)) { | |
4307 | char tn_buf[48]; | |
4308 | ||
4309 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 4310 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 4311 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
4312 | return -EACCES; |
4313 | } | |
4314 | ||
969bf05e AS |
4315 | return 0; |
4316 | } | |
4317 | ||
e07b98d9 | 4318 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
4319 | const struct bpf_reg_state *reg, int off, |
4320 | int size, bool strict_alignment_once) | |
79adffcd | 4321 | { |
ca369602 | 4322 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 4323 | const char *pointer_desc = ""; |
d1174416 | 4324 | |
79adffcd DB |
4325 | switch (reg->type) { |
4326 | case PTR_TO_PACKET: | |
de8f3a83 DB |
4327 | case PTR_TO_PACKET_META: |
4328 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
4329 | * right in front, treat it the very same way. | |
4330 | */ | |
61bd5218 | 4331 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
4332 | case PTR_TO_FLOW_KEYS: |
4333 | pointer_desc = "flow keys "; | |
4334 | break; | |
69c087ba YS |
4335 | case PTR_TO_MAP_KEY: |
4336 | pointer_desc = "key "; | |
4337 | break; | |
f1174f77 EC |
4338 | case PTR_TO_MAP_VALUE: |
4339 | pointer_desc = "value "; | |
4340 | break; | |
4341 | case PTR_TO_CTX: | |
4342 | pointer_desc = "context "; | |
4343 | break; | |
4344 | case PTR_TO_STACK: | |
4345 | pointer_desc = "stack "; | |
01f810ac AM |
4346 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
4347 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
4348 | * aligned. |
4349 | */ | |
4350 | strict = true; | |
f1174f77 | 4351 | break; |
c64b7983 JS |
4352 | case PTR_TO_SOCKET: |
4353 | pointer_desc = "sock "; | |
4354 | break; | |
46f8bc92 MKL |
4355 | case PTR_TO_SOCK_COMMON: |
4356 | pointer_desc = "sock_common "; | |
4357 | break; | |
655a51e5 MKL |
4358 | case PTR_TO_TCP_SOCK: |
4359 | pointer_desc = "tcp_sock "; | |
4360 | break; | |
fada7fdc JL |
4361 | case PTR_TO_XDP_SOCK: |
4362 | pointer_desc = "xdp_sock "; | |
4363 | break; | |
79adffcd | 4364 | default: |
f1174f77 | 4365 | break; |
79adffcd | 4366 | } |
61bd5218 JK |
4367 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
4368 | strict); | |
79adffcd DB |
4369 | } |
4370 | ||
f4d7e40a AS |
4371 | static int update_stack_depth(struct bpf_verifier_env *env, |
4372 | const struct bpf_func_state *func, | |
4373 | int off) | |
4374 | { | |
9c8105bd | 4375 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
4376 | |
4377 | if (stack >= -off) | |
4378 | return 0; | |
4379 | ||
4380 | /* update known max for given subprogram */ | |
9c8105bd | 4381 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
4382 | return 0; |
4383 | } | |
f4d7e40a | 4384 | |
70a87ffe AS |
4385 | /* starting from main bpf function walk all instructions of the function |
4386 | * and recursively walk all callees that given function can call. | |
4387 | * Ignore jump and exit insns. | |
4388 | * Since recursion is prevented by check_cfg() this algorithm | |
4389 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
4390 | */ | |
4391 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
4392 | { | |
9c8105bd JW |
4393 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
4394 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 4395 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 4396 | bool tail_call_reachable = false; |
70a87ffe AS |
4397 | int ret_insn[MAX_CALL_FRAMES]; |
4398 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 4399 | int j; |
f4d7e40a | 4400 | |
70a87ffe | 4401 | process_func: |
7f6e4312 MF |
4402 | /* protect against potential stack overflow that might happen when |
4403 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
4404 | * depth for such case down to 256 so that the worst case scenario | |
4405 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
4406 | * 8k). | |
4407 | * | |
4408 | * To get the idea what might happen, see an example: | |
4409 | * func1 -> sub rsp, 128 | |
4410 | * subfunc1 -> sub rsp, 256 | |
4411 | * tailcall1 -> add rsp, 256 | |
4412 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
4413 | * subfunc2 -> sub rsp, 64 | |
4414 | * subfunc22 -> sub rsp, 128 | |
4415 | * tailcall2 -> add rsp, 128 | |
4416 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
4417 | * | |
4418 | * tailcall will unwind the current stack frame but it will not get rid | |
4419 | * of caller's stack as shown on the example above. | |
4420 | */ | |
4421 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
4422 | verbose(env, | |
4423 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
4424 | depth); | |
4425 | return -EACCES; | |
4426 | } | |
70a87ffe AS |
4427 | /* round up to 32-bytes, since this is granularity |
4428 | * of interpreter stack size | |
4429 | */ | |
9c8105bd | 4430 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 4431 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 4432 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 4433 | frame + 1, depth); |
f4d7e40a AS |
4434 | return -EACCES; |
4435 | } | |
70a87ffe | 4436 | continue_func: |
4cb3d99c | 4437 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 4438 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
4439 | int next_insn; |
4440 | ||
69c087ba | 4441 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
4442 | continue; |
4443 | /* remember insn and function to return to */ | |
4444 | ret_insn[frame] = i + 1; | |
9c8105bd | 4445 | ret_prog[frame] = idx; |
70a87ffe AS |
4446 | |
4447 | /* find the callee */ | |
7ddc80a4 AS |
4448 | next_insn = i + insn[i].imm + 1; |
4449 | idx = find_subprog(env, next_insn); | |
9c8105bd | 4450 | if (idx < 0) { |
70a87ffe | 4451 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 4452 | next_insn); |
70a87ffe AS |
4453 | return -EFAULT; |
4454 | } | |
7ddc80a4 AS |
4455 | if (subprog[idx].is_async_cb) { |
4456 | if (subprog[idx].has_tail_call) { | |
4457 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
4458 | return -EFAULT; | |
4459 | } | |
4460 | /* async callbacks don't increase bpf prog stack size */ | |
4461 | continue; | |
4462 | } | |
4463 | i = next_insn; | |
ebf7d1f5 MF |
4464 | |
4465 | if (subprog[idx].has_tail_call) | |
4466 | tail_call_reachable = true; | |
4467 | ||
70a87ffe AS |
4468 | frame++; |
4469 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
4470 | verbose(env, "the call stack of %d frames is too deep !\n", |
4471 | frame); | |
4472 | return -E2BIG; | |
70a87ffe AS |
4473 | } |
4474 | goto process_func; | |
4475 | } | |
ebf7d1f5 MF |
4476 | /* if tail call got detected across bpf2bpf calls then mark each of the |
4477 | * currently present subprog frames as tail call reachable subprogs; | |
4478 | * this info will be utilized by JIT so that we will be preserving the | |
4479 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
4480 | */ | |
4481 | if (tail_call_reachable) | |
4482 | for (j = 0; j < frame; j++) | |
4483 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
4484 | if (subprog[0].tail_call_reachable) |
4485 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 4486 | |
70a87ffe AS |
4487 | /* end of for() loop means the last insn of the 'subprog' |
4488 | * was reached. Doesn't matter whether it was JA or EXIT | |
4489 | */ | |
4490 | if (frame == 0) | |
4491 | return 0; | |
9c8105bd | 4492 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
4493 | frame--; |
4494 | i = ret_insn[frame]; | |
9c8105bd | 4495 | idx = ret_prog[frame]; |
70a87ffe | 4496 | goto continue_func; |
f4d7e40a AS |
4497 | } |
4498 | ||
19d28fbd | 4499 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
4500 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
4501 | const struct bpf_insn *insn, int idx) | |
4502 | { | |
4503 | int start = idx + insn->imm + 1, subprog; | |
4504 | ||
4505 | subprog = find_subprog(env, start); | |
4506 | if (subprog < 0) { | |
4507 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
4508 | start); | |
4509 | return -EFAULT; | |
4510 | } | |
9c8105bd | 4511 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 4512 | } |
19d28fbd | 4513 | #endif |
1ea47e01 | 4514 | |
afbf21dc YS |
4515 | static int __check_buffer_access(struct bpf_verifier_env *env, |
4516 | const char *buf_info, | |
4517 | const struct bpf_reg_state *reg, | |
4518 | int regno, int off, int size) | |
9df1c28b MM |
4519 | { |
4520 | if (off < 0) { | |
4521 | verbose(env, | |
4fc00b79 | 4522 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 4523 | regno, buf_info, off, size); |
9df1c28b MM |
4524 | return -EACCES; |
4525 | } | |
4526 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4527 | char tn_buf[48]; | |
4528 | ||
4529 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4530 | verbose(env, | |
4fc00b79 | 4531 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
4532 | regno, off, tn_buf); |
4533 | return -EACCES; | |
4534 | } | |
afbf21dc YS |
4535 | |
4536 | return 0; | |
4537 | } | |
4538 | ||
4539 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
4540 | const struct bpf_reg_state *reg, | |
4541 | int regno, int off, int size) | |
4542 | { | |
4543 | int err; | |
4544 | ||
4545 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
4546 | if (err) | |
4547 | return err; | |
4548 | ||
9df1c28b MM |
4549 | if (off + size > env->prog->aux->max_tp_access) |
4550 | env->prog->aux->max_tp_access = off + size; | |
4551 | ||
4552 | return 0; | |
4553 | } | |
4554 | ||
afbf21dc YS |
4555 | static int check_buffer_access(struct bpf_verifier_env *env, |
4556 | const struct bpf_reg_state *reg, | |
4557 | int regno, int off, int size, | |
4558 | bool zero_size_allowed, | |
afbf21dc YS |
4559 | u32 *max_access) |
4560 | { | |
44e9a741 | 4561 | const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; |
afbf21dc YS |
4562 | int err; |
4563 | ||
4564 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
4565 | if (err) | |
4566 | return err; | |
4567 | ||
4568 | if (off + size > *max_access) | |
4569 | *max_access = off + size; | |
4570 | ||
4571 | return 0; | |
4572 | } | |
4573 | ||
3f50f132 JF |
4574 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
4575 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
4576 | { | |
4577 | reg->var_off = tnum_subreg(reg->var_off); | |
4578 | __reg_assign_32_into_64(reg); | |
4579 | } | |
9df1c28b | 4580 | |
0c17d1d2 JH |
4581 | /* truncate register to smaller size (in bytes) |
4582 | * must be called with size < BPF_REG_SIZE | |
4583 | */ | |
4584 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
4585 | { | |
4586 | u64 mask; | |
4587 | ||
4588 | /* clear high bits in bit representation */ | |
4589 | reg->var_off = tnum_cast(reg->var_off, size); | |
4590 | ||
4591 | /* fix arithmetic bounds */ | |
4592 | mask = ((u64)1 << (size * 8)) - 1; | |
4593 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
4594 | reg->umin_value &= mask; | |
4595 | reg->umax_value &= mask; | |
4596 | } else { | |
4597 | reg->umin_value = 0; | |
4598 | reg->umax_value = mask; | |
4599 | } | |
4600 | reg->smin_value = reg->umin_value; | |
4601 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
4602 | |
4603 | /* If size is smaller than 32bit register the 32bit register | |
4604 | * values are also truncated so we push 64-bit bounds into | |
4605 | * 32-bit bounds. Above were truncated < 32-bits already. | |
4606 | */ | |
4607 | if (size >= 4) | |
4608 | return; | |
4609 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
4610 | } |
4611 | ||
a23740ec AN |
4612 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
4613 | { | |
353050be DB |
4614 | /* A map is considered read-only if the following condition are true: |
4615 | * | |
4616 | * 1) BPF program side cannot change any of the map content. The | |
4617 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
4618 | * and was set at map creation time. | |
4619 | * 2) The map value(s) have been initialized from user space by a | |
4620 | * loader and then "frozen", such that no new map update/delete | |
4621 | * operations from syscall side are possible for the rest of | |
4622 | * the map's lifetime from that point onwards. | |
4623 | * 3) Any parallel/pending map update/delete operations from syscall | |
4624 | * side have been completed. Only after that point, it's safe to | |
4625 | * assume that map value(s) are immutable. | |
4626 | */ | |
4627 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
4628 | READ_ONCE(map->frozen) && | |
4629 | !bpf_map_write_active(map); | |
a23740ec AN |
4630 | } |
4631 | ||
4632 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
4633 | { | |
4634 | void *ptr; | |
4635 | u64 addr; | |
4636 | int err; | |
4637 | ||
4638 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
4639 | if (err) | |
4640 | return err; | |
2dedd7d2 | 4641 | ptr = (void *)(long)addr + off; |
a23740ec AN |
4642 | |
4643 | switch (size) { | |
4644 | case sizeof(u8): | |
4645 | *val = (u64)*(u8 *)ptr; | |
4646 | break; | |
4647 | case sizeof(u16): | |
4648 | *val = (u64)*(u16 *)ptr; | |
4649 | break; | |
4650 | case sizeof(u32): | |
4651 | *val = (u64)*(u32 *)ptr; | |
4652 | break; | |
4653 | case sizeof(u64): | |
4654 | *val = *(u64 *)ptr; | |
4655 | break; | |
4656 | default: | |
4657 | return -EINVAL; | |
4658 | } | |
4659 | return 0; | |
4660 | } | |
4661 | ||
9e15db66 AS |
4662 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
4663 | struct bpf_reg_state *regs, | |
4664 | int regno, int off, int size, | |
4665 | enum bpf_access_type atype, | |
4666 | int value_regno) | |
4667 | { | |
4668 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
4669 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
4670 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
c6f1bfe8 | 4671 | enum bpf_type_flag flag = 0; |
9e15db66 AS |
4672 | u32 btf_id; |
4673 | int ret; | |
4674 | ||
9e15db66 AS |
4675 | if (off < 0) { |
4676 | verbose(env, | |
4677 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
4678 | regno, tname, off); | |
4679 | return -EACCES; | |
4680 | } | |
4681 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4682 | char tn_buf[48]; | |
4683 | ||
4684 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4685 | verbose(env, | |
4686 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
4687 | regno, tname, off, tn_buf); | |
4688 | return -EACCES; | |
4689 | } | |
4690 | ||
c6f1bfe8 YS |
4691 | if (reg->type & MEM_USER) { |
4692 | verbose(env, | |
4693 | "R%d is ptr_%s access user memory: off=%d\n", | |
4694 | regno, tname, off); | |
4695 | return -EACCES; | |
4696 | } | |
4697 | ||
5844101a HL |
4698 | if (reg->type & MEM_PERCPU) { |
4699 | verbose(env, | |
4700 | "R%d is ptr_%s access percpu memory: off=%d\n", | |
4701 | regno, tname, off); | |
4702 | return -EACCES; | |
4703 | } | |
4704 | ||
282de143 KKD |
4705 | if (env->ops->btf_struct_access && !type_is_alloc(reg->type)) { |
4706 | if (!btf_is_kernel(reg->btf)) { | |
4707 | verbose(env, "verifier internal error: reg->btf must be kernel btf\n"); | |
4708 | return -EFAULT; | |
4709 | } | |
6728aea7 | 4710 | ret = env->ops->btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 | 4711 | } else { |
282de143 KKD |
4712 | /* Writes are permitted with default btf_struct_access for |
4713 | * program allocated objects (which always have ref_obj_id > 0), | |
4714 | * but not for untrusted PTR_TO_BTF_ID | MEM_ALLOC. | |
4715 | */ | |
4716 | if (atype != BPF_READ && reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
27ae7997 MKL |
4717 | verbose(env, "only read is supported\n"); |
4718 | return -EACCES; | |
4719 | } | |
4720 | ||
282de143 KKD |
4721 | if (type_is_alloc(reg->type) && !reg->ref_obj_id) { |
4722 | verbose(env, "verifier internal error: ref_obj_id for allocated object must be non-zero\n"); | |
4723 | return -EFAULT; | |
4724 | } | |
4725 | ||
6728aea7 | 4726 | ret = btf_struct_access(&env->log, reg, off, size, atype, &btf_id, &flag); |
27ae7997 MKL |
4727 | } |
4728 | ||
9e15db66 AS |
4729 | if (ret < 0) |
4730 | return ret; | |
4731 | ||
6efe152d KKD |
4732 | /* If this is an untrusted pointer, all pointers formed by walking it |
4733 | * also inherit the untrusted flag. | |
4734 | */ | |
4735 | if (type_flag(reg->type) & PTR_UNTRUSTED) | |
4736 | flag |= PTR_UNTRUSTED; | |
4737 | ||
41c48f3a | 4738 | if (atype == BPF_READ && value_regno >= 0) |
c6f1bfe8 | 4739 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); |
41c48f3a AI |
4740 | |
4741 | return 0; | |
4742 | } | |
4743 | ||
4744 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
4745 | struct bpf_reg_state *regs, | |
4746 | int regno, int off, int size, | |
4747 | enum bpf_access_type atype, | |
4748 | int value_regno) | |
4749 | { | |
4750 | struct bpf_reg_state *reg = regs + regno; | |
4751 | struct bpf_map *map = reg->map_ptr; | |
6728aea7 | 4752 | struct bpf_reg_state map_reg; |
c6f1bfe8 | 4753 | enum bpf_type_flag flag = 0; |
41c48f3a AI |
4754 | const struct btf_type *t; |
4755 | const char *tname; | |
4756 | u32 btf_id; | |
4757 | int ret; | |
4758 | ||
4759 | if (!btf_vmlinux) { | |
4760 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
4761 | return -ENOTSUPP; | |
4762 | } | |
4763 | ||
4764 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
4765 | verbose(env, "map_ptr access not supported for map type %d\n", | |
4766 | map->map_type); | |
4767 | return -ENOTSUPP; | |
4768 | } | |
4769 | ||
4770 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
4771 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
4772 | ||
4773 | if (!env->allow_ptr_to_map_access) { | |
4774 | verbose(env, | |
4775 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
4776 | tname); | |
4777 | return -EPERM; | |
9e15db66 | 4778 | } |
27ae7997 | 4779 | |
41c48f3a AI |
4780 | if (off < 0) { |
4781 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
4782 | regno, tname, off); | |
4783 | return -EACCES; | |
4784 | } | |
4785 | ||
4786 | if (atype != BPF_READ) { | |
4787 | verbose(env, "only read from %s is supported\n", tname); | |
4788 | return -EACCES; | |
4789 | } | |
4790 | ||
6728aea7 KKD |
4791 | /* Simulate access to a PTR_TO_BTF_ID */ |
4792 | memset(&map_reg, 0, sizeof(map_reg)); | |
4793 | mark_btf_ld_reg(env, &map_reg, 0, PTR_TO_BTF_ID, btf_vmlinux, *map->ops->map_btf_id, 0); | |
4794 | ret = btf_struct_access(&env->log, &map_reg, off, size, atype, &btf_id, &flag); | |
41c48f3a AI |
4795 | if (ret < 0) |
4796 | return ret; | |
4797 | ||
4798 | if (value_regno >= 0) | |
c6f1bfe8 | 4799 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id, flag); |
41c48f3a | 4800 | |
9e15db66 AS |
4801 | return 0; |
4802 | } | |
4803 | ||
01f810ac AM |
4804 | /* Check that the stack access at the given offset is within bounds. The |
4805 | * maximum valid offset is -1. | |
4806 | * | |
4807 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
4808 | * -state->allocated_stack for reads. | |
4809 | */ | |
4810 | static int check_stack_slot_within_bounds(int off, | |
4811 | struct bpf_func_state *state, | |
4812 | enum bpf_access_type t) | |
4813 | { | |
4814 | int min_valid_off; | |
4815 | ||
4816 | if (t == BPF_WRITE) | |
4817 | min_valid_off = -MAX_BPF_STACK; | |
4818 | else | |
4819 | min_valid_off = -state->allocated_stack; | |
4820 | ||
4821 | if (off < min_valid_off || off > -1) | |
4822 | return -EACCES; | |
4823 | return 0; | |
4824 | } | |
4825 | ||
4826 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
4827 | * bounds. | |
4828 | * | |
4829 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
4830 | */ | |
4831 | static int check_stack_access_within_bounds( | |
4832 | struct bpf_verifier_env *env, | |
4833 | int regno, int off, int access_size, | |
61df10c7 | 4834 | enum bpf_access_src src, enum bpf_access_type type) |
01f810ac AM |
4835 | { |
4836 | struct bpf_reg_state *regs = cur_regs(env); | |
4837 | struct bpf_reg_state *reg = regs + regno; | |
4838 | struct bpf_func_state *state = func(env, reg); | |
4839 | int min_off, max_off; | |
4840 | int err; | |
4841 | char *err_extra; | |
4842 | ||
4843 | if (src == ACCESS_HELPER) | |
4844 | /* We don't know if helpers are reading or writing (or both). */ | |
4845 | err_extra = " indirect access to"; | |
4846 | else if (type == BPF_READ) | |
4847 | err_extra = " read from"; | |
4848 | else | |
4849 | err_extra = " write to"; | |
4850 | ||
4851 | if (tnum_is_const(reg->var_off)) { | |
4852 | min_off = reg->var_off.value + off; | |
4853 | if (access_size > 0) | |
4854 | max_off = min_off + access_size - 1; | |
4855 | else | |
4856 | max_off = min_off; | |
4857 | } else { | |
4858 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
4859 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
4860 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
4861 | err_extra, regno); | |
4862 | return -EACCES; | |
4863 | } | |
4864 | min_off = reg->smin_value + off; | |
4865 | if (access_size > 0) | |
4866 | max_off = reg->smax_value + off + access_size - 1; | |
4867 | else | |
4868 | max_off = min_off; | |
4869 | } | |
4870 | ||
4871 | err = check_stack_slot_within_bounds(min_off, state, type); | |
4872 | if (!err) | |
4873 | err = check_stack_slot_within_bounds(max_off, state, type); | |
4874 | ||
4875 | if (err) { | |
4876 | if (tnum_is_const(reg->var_off)) { | |
4877 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
4878 | err_extra, regno, off, access_size); | |
4879 | } else { | |
4880 | char tn_buf[48]; | |
4881 | ||
4882 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4883 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
4884 | err_extra, regno, tn_buf, access_size); | |
4885 | } | |
4886 | } | |
4887 | return err; | |
4888 | } | |
41c48f3a | 4889 | |
17a52670 AS |
4890 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
4891 | * if t==write, value_regno is a register which value is stored into memory | |
4892 | * if t==read, value_regno is a register which will receive the value from memory | |
4893 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
4894 | * if t==read && value_regno==-1, don't care what we read from memory | |
4895 | */ | |
ca369602 DB |
4896 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
4897 | int off, int bpf_size, enum bpf_access_type t, | |
4898 | int value_regno, bool strict_alignment_once) | |
17a52670 | 4899 | { |
638f5b90 AS |
4900 | struct bpf_reg_state *regs = cur_regs(env); |
4901 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 4902 | struct bpf_func_state *state; |
17a52670 AS |
4903 | int size, err = 0; |
4904 | ||
4905 | size = bpf_size_to_bytes(bpf_size); | |
4906 | if (size < 0) | |
4907 | return size; | |
4908 | ||
f1174f77 | 4909 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 4910 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
4911 | if (err) |
4912 | return err; | |
17a52670 | 4913 | |
f1174f77 EC |
4914 | /* for access checks, reg->off is just part of off */ |
4915 | off += reg->off; | |
4916 | ||
69c087ba YS |
4917 | if (reg->type == PTR_TO_MAP_KEY) { |
4918 | if (t == BPF_WRITE) { | |
4919 | verbose(env, "write to change key R%d not allowed\n", regno); | |
4920 | return -EACCES; | |
4921 | } | |
4922 | ||
4923 | err = check_mem_region_access(env, regno, off, size, | |
4924 | reg->map_ptr->key_size, false); | |
4925 | if (err) | |
4926 | return err; | |
4927 | if (value_regno >= 0) | |
4928 | mark_reg_unknown(env, regs, value_regno); | |
4929 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
aa3496ac | 4930 | struct btf_field *kptr_field = NULL; |
61df10c7 | 4931 | |
1be7f75d AS |
4932 | if (t == BPF_WRITE && value_regno >= 0 && |
4933 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4934 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
4935 | return -EACCES; |
4936 | } | |
591fe988 DB |
4937 | err = check_map_access_type(env, regno, off, size, t); |
4938 | if (err) | |
4939 | return err; | |
61df10c7 KKD |
4940 | err = check_map_access(env, regno, off, size, false, ACCESS_DIRECT); |
4941 | if (err) | |
4942 | return err; | |
4943 | if (tnum_is_const(reg->var_off)) | |
aa3496ac KKD |
4944 | kptr_field = btf_record_find(reg->map_ptr->record, |
4945 | off + reg->var_off.value, BPF_KPTR); | |
4946 | if (kptr_field) { | |
4947 | err = check_map_kptr_access(env, regno, value_regno, insn_idx, kptr_field); | |
61df10c7 | 4948 | } else if (t == BPF_READ && value_regno >= 0) { |
a23740ec AN |
4949 | struct bpf_map *map = reg->map_ptr; |
4950 | ||
4951 | /* if map is read-only, track its contents as scalars */ | |
4952 | if (tnum_is_const(reg->var_off) && | |
4953 | bpf_map_is_rdonly(map) && | |
4954 | map->ops->map_direct_value_addr) { | |
4955 | int map_off = off + reg->var_off.value; | |
4956 | u64 val = 0; | |
4957 | ||
4958 | err = bpf_map_direct_read(map, map_off, size, | |
4959 | &val); | |
4960 | if (err) | |
4961 | return err; | |
4962 | ||
4963 | regs[value_regno].type = SCALAR_VALUE; | |
4964 | __mark_reg_known(®s[value_regno], val); | |
4965 | } else { | |
4966 | mark_reg_unknown(env, regs, value_regno); | |
4967 | } | |
4968 | } | |
34d3a78c HL |
4969 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
4970 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
4971 | ||
4972 | if (type_may_be_null(reg->type)) { | |
4973 | verbose(env, "R%d invalid mem access '%s'\n", regno, | |
4974 | reg_type_str(env, reg->type)); | |
4975 | return -EACCES; | |
4976 | } | |
4977 | ||
4978 | if (t == BPF_WRITE && rdonly_mem) { | |
4979 | verbose(env, "R%d cannot write into %s\n", | |
4980 | regno, reg_type_str(env, reg->type)); | |
4981 | return -EACCES; | |
4982 | } | |
4983 | ||
457f4436 AN |
4984 | if (t == BPF_WRITE && value_regno >= 0 && |
4985 | is_pointer_value(env, value_regno)) { | |
4986 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
4987 | return -EACCES; | |
4988 | } | |
34d3a78c | 4989 | |
457f4436 AN |
4990 | err = check_mem_region_access(env, regno, off, size, |
4991 | reg->mem_size, false); | |
34d3a78c | 4992 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
457f4436 | 4993 | mark_reg_unknown(env, regs, value_regno); |
1a0dc1ac | 4994 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 4995 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 4996 | struct btf *btf = NULL; |
9e15db66 | 4997 | u32 btf_id = 0; |
19de99f7 | 4998 | |
1be7f75d AS |
4999 | if (t == BPF_WRITE && value_regno >= 0 && |
5000 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 5001 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
5002 | return -EACCES; |
5003 | } | |
f1174f77 | 5004 | |
be80a1d3 | 5005 | err = check_ptr_off_reg(env, reg, regno); |
58990d1f DB |
5006 | if (err < 0) |
5007 | return err; | |
5008 | ||
c6f1bfe8 YS |
5009 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, |
5010 | &btf_id); | |
9e15db66 AS |
5011 | if (err) |
5012 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 5013 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 5014 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
5015 | * PTR_TO_PACKET[_META,_END]. In the latter |
5016 | * case, we know the offset is zero. | |
f1174f77 | 5017 | */ |
46f8bc92 | 5018 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 5019 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 5020 | } else { |
638f5b90 | 5021 | mark_reg_known_zero(env, regs, |
61bd5218 | 5022 | value_regno); |
c25b2ae1 | 5023 | if (type_may_be_null(reg_type)) |
46f8bc92 | 5024 | regs[value_regno].id = ++env->id_gen; |
5327ed3d JW |
5025 | /* A load of ctx field could have different |
5026 | * actual load size with the one encoded in the | |
5027 | * insn. When the dst is PTR, it is for sure not | |
5028 | * a sub-register. | |
5029 | */ | |
5030 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
c25b2ae1 | 5031 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 5032 | regs[value_regno].btf = btf; |
9e15db66 | 5033 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 5034 | } |
46f8bc92 | 5035 | } |
638f5b90 | 5036 | regs[value_regno].type = reg_type; |
969bf05e | 5037 | } |
17a52670 | 5038 | |
f1174f77 | 5039 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
5040 | /* Basic bounds checks. */ |
5041 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
5042 | if (err) |
5043 | return err; | |
8726679a | 5044 | |
f4d7e40a AS |
5045 | state = func(env, reg); |
5046 | err = update_stack_depth(env, state, off); | |
5047 | if (err) | |
5048 | return err; | |
8726679a | 5049 | |
01f810ac AM |
5050 | if (t == BPF_READ) |
5051 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 5052 | value_regno); |
01f810ac AM |
5053 | else |
5054 | err = check_stack_write(env, regno, off, size, | |
5055 | value_regno, insn_idx); | |
de8f3a83 | 5056 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 5057 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 5058 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
5059 | return -EACCES; |
5060 | } | |
4acf6c0b BB |
5061 | if (t == BPF_WRITE && value_regno >= 0 && |
5062 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
5063 | verbose(env, "R%d leaks addr into packet\n", |
5064 | value_regno); | |
4acf6c0b BB |
5065 | return -EACCES; |
5066 | } | |
9fd29c08 | 5067 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 5068 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 5069 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
5070 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
5071 | if (t == BPF_WRITE && value_regno >= 0 && | |
5072 | is_pointer_value(env, value_regno)) { | |
5073 | verbose(env, "R%d leaks addr into flow keys\n", | |
5074 | value_regno); | |
5075 | return -EACCES; | |
5076 | } | |
5077 | ||
5078 | err = check_flow_keys_access(env, off, size); | |
5079 | if (!err && t == BPF_READ && value_regno >= 0) | |
5080 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 5081 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 5082 | if (t == BPF_WRITE) { |
46f8bc92 | 5083 | verbose(env, "R%d cannot write into %s\n", |
c25b2ae1 | 5084 | regno, reg_type_str(env, reg->type)); |
c64b7983 JS |
5085 | return -EACCES; |
5086 | } | |
5f456649 | 5087 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
5088 | if (!err && value_regno >= 0) |
5089 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
5090 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
5091 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
5092 | if (!err && t == BPF_READ && value_regno >= 0) | |
5093 | mark_reg_unknown(env, regs, value_regno); | |
bff61f6f HL |
5094 | } else if (base_type(reg->type) == PTR_TO_BTF_ID && |
5095 | !type_may_be_null(reg->type)) { | |
9e15db66 AS |
5096 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
5097 | value_regno); | |
41c48f3a AI |
5098 | } else if (reg->type == CONST_PTR_TO_MAP) { |
5099 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
5100 | value_regno); | |
20b2aff4 HL |
5101 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
5102 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
20b2aff4 HL |
5103 | u32 *max_access; |
5104 | ||
5105 | if (rdonly_mem) { | |
5106 | if (t == BPF_WRITE) { | |
5107 | verbose(env, "R%d cannot write into %s\n", | |
5108 | regno, reg_type_str(env, reg->type)); | |
5109 | return -EACCES; | |
5110 | } | |
20b2aff4 HL |
5111 | max_access = &env->prog->aux->max_rdonly_access; |
5112 | } else { | |
20b2aff4 | 5113 | max_access = &env->prog->aux->max_rdwr_access; |
afbf21dc | 5114 | } |
20b2aff4 | 5115 | |
f6dfbe31 | 5116 | err = check_buffer_access(env, reg, regno, off, size, false, |
44e9a741 | 5117 | max_access); |
20b2aff4 HL |
5118 | |
5119 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) | |
afbf21dc | 5120 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 5121 | } else { |
61bd5218 | 5122 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
c25b2ae1 | 5123 | reg_type_str(env, reg->type)); |
17a52670 AS |
5124 | return -EACCES; |
5125 | } | |
969bf05e | 5126 | |
f1174f77 | 5127 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 5128 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 5129 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 5130 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 5131 | } |
17a52670 AS |
5132 | return err; |
5133 | } | |
5134 | ||
91c960b0 | 5135 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 5136 | { |
5ffa2550 | 5137 | int load_reg; |
17a52670 AS |
5138 | int err; |
5139 | ||
5ca419f2 BJ |
5140 | switch (insn->imm) { |
5141 | case BPF_ADD: | |
5142 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
5143 | case BPF_AND: |
5144 | case BPF_AND | BPF_FETCH: | |
5145 | case BPF_OR: | |
5146 | case BPF_OR | BPF_FETCH: | |
5147 | case BPF_XOR: | |
5148 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
5149 | case BPF_XCHG: |
5150 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
5151 | break; |
5152 | default: | |
91c960b0 BJ |
5153 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
5154 | return -EINVAL; | |
5155 | } | |
5156 | ||
5157 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
5158 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
5159 | return -EINVAL; |
5160 | } | |
5161 | ||
5162 | /* check src1 operand */ | |
dc503a8a | 5163 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5164 | if (err) |
5165 | return err; | |
5166 | ||
5167 | /* check src2 operand */ | |
dc503a8a | 5168 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5169 | if (err) |
5170 | return err; | |
5171 | ||
5ffa2550 BJ |
5172 | if (insn->imm == BPF_CMPXCHG) { |
5173 | /* Check comparison of R0 with memory location */ | |
a82fe085 DB |
5174 | const u32 aux_reg = BPF_REG_0; |
5175 | ||
5176 | err = check_reg_arg(env, aux_reg, SRC_OP); | |
5ffa2550 BJ |
5177 | if (err) |
5178 | return err; | |
a82fe085 DB |
5179 | |
5180 | if (is_pointer_value(env, aux_reg)) { | |
5181 | verbose(env, "R%d leaks addr into mem\n", aux_reg); | |
5182 | return -EACCES; | |
5183 | } | |
5ffa2550 BJ |
5184 | } |
5185 | ||
6bdf6abc | 5186 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 5187 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
5188 | return -EACCES; |
5189 | } | |
5190 | ||
ca369602 | 5191 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 5192 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
5193 | is_flow_key_reg(env, insn->dst_reg) || |
5194 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 5195 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f | 5196 | insn->dst_reg, |
c25b2ae1 | 5197 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
5198 | return -EACCES; |
5199 | } | |
5200 | ||
37086bfd BJ |
5201 | if (insn->imm & BPF_FETCH) { |
5202 | if (insn->imm == BPF_CMPXCHG) | |
5203 | load_reg = BPF_REG_0; | |
5204 | else | |
5205 | load_reg = insn->src_reg; | |
5206 | ||
5207 | /* check and record load of old value */ | |
5208 | err = check_reg_arg(env, load_reg, DST_OP); | |
5209 | if (err) | |
5210 | return err; | |
5211 | } else { | |
5212 | /* This instruction accesses a memory location but doesn't | |
5213 | * actually load it into a register. | |
5214 | */ | |
5215 | load_reg = -1; | |
5216 | } | |
5217 | ||
7d3baf0a DB |
5218 | /* Check whether we can read the memory, with second call for fetch |
5219 | * case to simulate the register fill. | |
5220 | */ | |
31fd8581 | 5221 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
7d3baf0a DB |
5222 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
5223 | if (!err && load_reg >= 0) | |
5224 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, | |
5225 | BPF_SIZE(insn->code), BPF_READ, load_reg, | |
5226 | true); | |
17a52670 AS |
5227 | if (err) |
5228 | return err; | |
5229 | ||
7d3baf0a | 5230 | /* Check whether we can write into the same memory. */ |
5ca419f2 BJ |
5231 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
5232 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
5233 | if (err) | |
5234 | return err; | |
5235 | ||
5ca419f2 | 5236 | return 0; |
17a52670 AS |
5237 | } |
5238 | ||
01f810ac AM |
5239 | /* When register 'regno' is used to read the stack (either directly or through |
5240 | * a helper function) make sure that it's within stack boundary and, depending | |
5241 | * on the access type, that all elements of the stack are initialized. | |
5242 | * | |
5243 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
5244 | * | |
5245 | * All registers that have been spilled on the stack in the slots within the | |
5246 | * read offsets are marked as read. | |
5247 | */ | |
5248 | static int check_stack_range_initialized( | |
5249 | struct bpf_verifier_env *env, int regno, int off, | |
5250 | int access_size, bool zero_size_allowed, | |
61df10c7 | 5251 | enum bpf_access_src type, struct bpf_call_arg_meta *meta) |
2011fccf AI |
5252 | { |
5253 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
5254 | struct bpf_func_state *state = func(env, reg); |
5255 | int err, min_off, max_off, i, j, slot, spi; | |
5256 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
5257 | enum bpf_access_type bounds_check_type; | |
5258 | /* Some accesses can write anything into the stack, others are | |
5259 | * read-only. | |
5260 | */ | |
5261 | bool clobber = false; | |
2011fccf | 5262 | |
01f810ac AM |
5263 | if (access_size == 0 && !zero_size_allowed) { |
5264 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
5265 | return -EACCES; |
5266 | } | |
2011fccf | 5267 | |
01f810ac AM |
5268 | if (type == ACCESS_HELPER) { |
5269 | /* The bounds checks for writes are more permissive than for | |
5270 | * reads. However, if raw_mode is not set, we'll do extra | |
5271 | * checks below. | |
5272 | */ | |
5273 | bounds_check_type = BPF_WRITE; | |
5274 | clobber = true; | |
5275 | } else { | |
5276 | bounds_check_type = BPF_READ; | |
5277 | } | |
5278 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
5279 | type, bounds_check_type); | |
5280 | if (err) | |
5281 | return err; | |
5282 | ||
17a52670 | 5283 | |
2011fccf | 5284 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 5285 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 5286 | } else { |
088ec26d AI |
5287 | /* Variable offset is prohibited for unprivileged mode for |
5288 | * simplicity since it requires corresponding support in | |
5289 | * Spectre masking for stack ALU. | |
5290 | * See also retrieve_ptr_limit(). | |
5291 | */ | |
2c78ee89 | 5292 | if (!env->bypass_spec_v1) { |
088ec26d | 5293 | char tn_buf[48]; |
f1174f77 | 5294 | |
088ec26d | 5295 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
5296 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
5297 | regno, err_extra, tn_buf); | |
088ec26d AI |
5298 | return -EACCES; |
5299 | } | |
f2bcd05e AI |
5300 | /* Only initialized buffer on stack is allowed to be accessed |
5301 | * with variable offset. With uninitialized buffer it's hard to | |
5302 | * guarantee that whole memory is marked as initialized on | |
5303 | * helper return since specific bounds are unknown what may | |
5304 | * cause uninitialized stack leaking. | |
5305 | */ | |
5306 | if (meta && meta->raw_mode) | |
5307 | meta = NULL; | |
5308 | ||
01f810ac AM |
5309 | min_off = reg->smin_value + off; |
5310 | max_off = reg->smax_value + off; | |
17a52670 AS |
5311 | } |
5312 | ||
435faee1 DB |
5313 | if (meta && meta->raw_mode) { |
5314 | meta->access_size = access_size; | |
5315 | meta->regno = regno; | |
5316 | return 0; | |
5317 | } | |
5318 | ||
2011fccf | 5319 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
5320 | u8 *stype; |
5321 | ||
2011fccf | 5322 | slot = -i - 1; |
638f5b90 | 5323 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
5324 | if (state->allocated_stack <= slot) |
5325 | goto err; | |
5326 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
5327 | if (*stype == STACK_MISC) | |
5328 | goto mark; | |
5329 | if (*stype == STACK_ZERO) { | |
01f810ac AM |
5330 | if (clobber) { |
5331 | /* helper can write anything into the stack */ | |
5332 | *stype = STACK_MISC; | |
5333 | } | |
cc2b14d5 | 5334 | goto mark; |
17a52670 | 5335 | } |
1d68f22b | 5336 | |
27113c59 | 5337 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
5338 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
5339 | env->allow_ptr_leaks)) { | |
01f810ac AM |
5340 | if (clobber) { |
5341 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
5342 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 5343 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 5344 | } |
f7cf25b2 AS |
5345 | goto mark; |
5346 | } | |
5347 | ||
cc2b14d5 | 5348 | err: |
2011fccf | 5349 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
5350 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
5351 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
5352 | } else { |
5353 | char tn_buf[48]; | |
5354 | ||
5355 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
5356 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
5357 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 5358 | } |
cc2b14d5 AS |
5359 | return -EACCES; |
5360 | mark: | |
5361 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
5362 | * the whole slot to be marked as 'read' | |
5363 | */ | |
679c782d | 5364 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
5365 | state->stack[spi].spilled_ptr.parent, |
5366 | REG_LIVE_READ64); | |
261f4664 KKD |
5367 | /* We do not set REG_LIVE_WRITTEN for stack slot, as we can not |
5368 | * be sure that whether stack slot is written to or not. Hence, | |
5369 | * we must still conservatively propagate reads upwards even if | |
5370 | * helper may write to the entire memory range. | |
5371 | */ | |
17a52670 | 5372 | } |
2011fccf | 5373 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
5374 | } |
5375 | ||
06c1c049 GB |
5376 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
5377 | int access_size, bool zero_size_allowed, | |
5378 | struct bpf_call_arg_meta *meta) | |
5379 | { | |
638f5b90 | 5380 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
20b2aff4 | 5381 | u32 *max_access; |
06c1c049 | 5382 | |
20b2aff4 | 5383 | switch (base_type(reg->type)) { |
06c1c049 | 5384 | case PTR_TO_PACKET: |
de8f3a83 | 5385 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
5386 | return check_packet_access(env, regno, reg->off, access_size, |
5387 | zero_size_allowed); | |
69c087ba | 5388 | case PTR_TO_MAP_KEY: |
7b3552d3 KKD |
5389 | if (meta && meta->raw_mode) { |
5390 | verbose(env, "R%d cannot write into %s\n", regno, | |
5391 | reg_type_str(env, reg->type)); | |
5392 | return -EACCES; | |
5393 | } | |
69c087ba YS |
5394 | return check_mem_region_access(env, regno, reg->off, access_size, |
5395 | reg->map_ptr->key_size, false); | |
06c1c049 | 5396 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
5397 | if (check_map_access_type(env, regno, reg->off, access_size, |
5398 | meta && meta->raw_mode ? BPF_WRITE : | |
5399 | BPF_READ)) | |
5400 | return -EACCES; | |
9fd29c08 | 5401 | return check_map_access(env, regno, reg->off, access_size, |
61df10c7 | 5402 | zero_size_allowed, ACCESS_HELPER); |
457f4436 | 5403 | case PTR_TO_MEM: |
97e6d7da KKD |
5404 | if (type_is_rdonly_mem(reg->type)) { |
5405 | if (meta && meta->raw_mode) { | |
5406 | verbose(env, "R%d cannot write into %s\n", regno, | |
5407 | reg_type_str(env, reg->type)); | |
5408 | return -EACCES; | |
5409 | } | |
5410 | } | |
457f4436 AN |
5411 | return check_mem_region_access(env, regno, reg->off, |
5412 | access_size, reg->mem_size, | |
5413 | zero_size_allowed); | |
20b2aff4 HL |
5414 | case PTR_TO_BUF: |
5415 | if (type_is_rdonly_mem(reg->type)) { | |
97e6d7da KKD |
5416 | if (meta && meta->raw_mode) { |
5417 | verbose(env, "R%d cannot write into %s\n", regno, | |
5418 | reg_type_str(env, reg->type)); | |
20b2aff4 | 5419 | return -EACCES; |
97e6d7da | 5420 | } |
20b2aff4 | 5421 | |
20b2aff4 HL |
5422 | max_access = &env->prog->aux->max_rdonly_access; |
5423 | } else { | |
20b2aff4 HL |
5424 | max_access = &env->prog->aux->max_rdwr_access; |
5425 | } | |
afbf21dc YS |
5426 | return check_buffer_access(env, reg, regno, reg->off, |
5427 | access_size, zero_size_allowed, | |
44e9a741 | 5428 | max_access); |
0d004c02 | 5429 | case PTR_TO_STACK: |
01f810ac AM |
5430 | return check_stack_range_initialized( |
5431 | env, | |
5432 | regno, reg->off, access_size, | |
5433 | zero_size_allowed, ACCESS_HELPER, meta); | |
15baa55f BT |
5434 | case PTR_TO_CTX: |
5435 | /* in case the function doesn't know how to access the context, | |
5436 | * (because we are in a program of type SYSCALL for example), we | |
5437 | * can not statically check its size. | |
5438 | * Dynamically check it now. | |
5439 | */ | |
5440 | if (!env->ops->convert_ctx_access) { | |
5441 | enum bpf_access_type atype = meta && meta->raw_mode ? BPF_WRITE : BPF_READ; | |
5442 | int offset = access_size - 1; | |
5443 | ||
5444 | /* Allow zero-byte read from PTR_TO_CTX */ | |
5445 | if (access_size == 0) | |
5446 | return zero_size_allowed ? 0 : -EACCES; | |
5447 | ||
5448 | return check_mem_access(env, env->insn_idx, regno, offset, BPF_B, | |
5449 | atype, -1, false); | |
5450 | } | |
5451 | ||
5452 | fallthrough; | |
0d004c02 LB |
5453 | default: /* scalar_value or invalid ptr */ |
5454 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
5455 | if (zero_size_allowed && access_size == 0 && | |
5456 | register_is_null(reg)) | |
5457 | return 0; | |
5458 | ||
c25b2ae1 HL |
5459 | verbose(env, "R%d type=%s ", regno, |
5460 | reg_type_str(env, reg->type)); | |
5461 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); | |
0d004c02 | 5462 | return -EACCES; |
06c1c049 GB |
5463 | } |
5464 | } | |
5465 | ||
d583691c KKD |
5466 | static int check_mem_size_reg(struct bpf_verifier_env *env, |
5467 | struct bpf_reg_state *reg, u32 regno, | |
5468 | bool zero_size_allowed, | |
5469 | struct bpf_call_arg_meta *meta) | |
5470 | { | |
5471 | int err; | |
5472 | ||
5473 | /* This is used to refine r0 return value bounds for helpers | |
5474 | * that enforce this value as an upper bound on return values. | |
5475 | * See do_refine_retval_range() for helpers that can refine | |
5476 | * the return value. C type of helper is u32 so we pull register | |
5477 | * bound from umax_value however, if negative verifier errors | |
5478 | * out. Only upper bounds can be learned because retval is an | |
5479 | * int type and negative retvals are allowed. | |
5480 | */ | |
be77354a | 5481 | meta->msize_max_value = reg->umax_value; |
d583691c KKD |
5482 | |
5483 | /* The register is SCALAR_VALUE; the access check | |
5484 | * happens using its boundaries. | |
5485 | */ | |
5486 | if (!tnum_is_const(reg->var_off)) | |
5487 | /* For unprivileged variable accesses, disable raw | |
5488 | * mode so that the program is required to | |
5489 | * initialize all the memory that the helper could | |
5490 | * just partially fill up. | |
5491 | */ | |
5492 | meta = NULL; | |
5493 | ||
5494 | if (reg->smin_value < 0) { | |
5495 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", | |
5496 | regno); | |
5497 | return -EACCES; | |
5498 | } | |
5499 | ||
5500 | if (reg->umin_value == 0) { | |
5501 | err = check_helper_mem_access(env, regno - 1, 0, | |
5502 | zero_size_allowed, | |
5503 | meta); | |
5504 | if (err) | |
5505 | return err; | |
5506 | } | |
5507 | ||
5508 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { | |
5509 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
5510 | regno); | |
5511 | return -EACCES; | |
5512 | } | |
5513 | err = check_helper_mem_access(env, regno - 1, | |
5514 | reg->umax_value, | |
5515 | zero_size_allowed, meta); | |
5516 | if (!err) | |
5517 | err = mark_chain_precision(env, regno); | |
5518 | return err; | |
5519 | } | |
5520 | ||
e5069b9c DB |
5521 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
5522 | u32 regno, u32 mem_size) | |
5523 | { | |
be77354a KKD |
5524 | bool may_be_null = type_may_be_null(reg->type); |
5525 | struct bpf_reg_state saved_reg; | |
5526 | struct bpf_call_arg_meta meta; | |
5527 | int err; | |
5528 | ||
e5069b9c DB |
5529 | if (register_is_null(reg)) |
5530 | return 0; | |
5531 | ||
be77354a KKD |
5532 | memset(&meta, 0, sizeof(meta)); |
5533 | /* Assuming that the register contains a value check if the memory | |
5534 | * access is safe. Temporarily save and restore the register's state as | |
5535 | * the conversion shouldn't be visible to a caller. | |
5536 | */ | |
5537 | if (may_be_null) { | |
5538 | saved_reg = *reg; | |
e5069b9c | 5539 | mark_ptr_not_null_reg(reg); |
e5069b9c DB |
5540 | } |
5541 | ||
be77354a KKD |
5542 | err = check_helper_mem_access(env, regno, mem_size, true, &meta); |
5543 | /* Check access for BPF_WRITE */ | |
5544 | meta.raw_mode = true; | |
5545 | err = err ?: check_helper_mem_access(env, regno, mem_size, true, &meta); | |
5546 | ||
5547 | if (may_be_null) | |
5548 | *reg = saved_reg; | |
5549 | ||
5550 | return err; | |
e5069b9c DB |
5551 | } |
5552 | ||
00b85860 KKD |
5553 | static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
5554 | u32 regno) | |
d583691c KKD |
5555 | { |
5556 | struct bpf_reg_state *mem_reg = &cur_regs(env)[regno - 1]; | |
5557 | bool may_be_null = type_may_be_null(mem_reg->type); | |
5558 | struct bpf_reg_state saved_reg; | |
be77354a | 5559 | struct bpf_call_arg_meta meta; |
d583691c KKD |
5560 | int err; |
5561 | ||
5562 | WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); | |
5563 | ||
be77354a KKD |
5564 | memset(&meta, 0, sizeof(meta)); |
5565 | ||
d583691c KKD |
5566 | if (may_be_null) { |
5567 | saved_reg = *mem_reg; | |
5568 | mark_ptr_not_null_reg(mem_reg); | |
5569 | } | |
5570 | ||
be77354a KKD |
5571 | err = check_mem_size_reg(env, reg, regno, true, &meta); |
5572 | /* Check access for BPF_WRITE */ | |
5573 | meta.raw_mode = true; | |
5574 | err = err ?: check_mem_size_reg(env, reg, regno, true, &meta); | |
d583691c KKD |
5575 | |
5576 | if (may_be_null) | |
5577 | *mem_reg = saved_reg; | |
5578 | return err; | |
5579 | } | |
5580 | ||
d83525ca | 5581 | /* Implementation details: |
4e814da0 KKD |
5582 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL. |
5583 | * bpf_obj_new returns PTR_TO_BTF_ID | MEM_ALLOC | PTR_MAYBE_NULL. | |
d83525ca | 5584 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
4e814da0 KKD |
5585 | * Two separate bpf_obj_new will also have different reg->id. |
5586 | * For traditional PTR_TO_MAP_VALUE or PTR_TO_BTF_ID | MEM_ALLOC, the verifier | |
5587 | * clears reg->id after value_or_null->value transition, since the verifier only | |
5588 | * cares about the range of access to valid map value pointer and doesn't care | |
5589 | * about actual address of the map element. | |
d83525ca AS |
5590 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
5591 | * reg->id > 0 after value_or_null->value transition. By doing so | |
5592 | * two bpf_map_lookups will be considered two different pointers that | |
4e814da0 KKD |
5593 | * point to different bpf_spin_locks. Likewise for pointers to allocated objects |
5594 | * returned from bpf_obj_new. | |
d83525ca AS |
5595 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
5596 | * dead-locks. | |
5597 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
5598 | * reg_is_refcounted() logic. The verifier needs to remember only | |
5599 | * one spin_lock instead of array of acquired_refs. | |
d0d78c1d | 5600 | * cur_state->active_lock remembers which map value element or allocated |
4e814da0 | 5601 | * object got locked and clears it after bpf_spin_unlock. |
d83525ca AS |
5602 | */ |
5603 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
5604 | bool is_lock) | |
5605 | { | |
5606 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5607 | struct bpf_verifier_state *cur = env->cur_state; | |
5608 | bool is_const = tnum_is_const(reg->var_off); | |
d83525ca | 5609 | u64 val = reg->var_off.value; |
4e814da0 KKD |
5610 | struct bpf_map *map = NULL; |
5611 | struct btf *btf = NULL; | |
5612 | struct btf_record *rec; | |
d83525ca | 5613 | |
d83525ca AS |
5614 | if (!is_const) { |
5615 | verbose(env, | |
5616 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
5617 | regno); | |
5618 | return -EINVAL; | |
5619 | } | |
4e814da0 KKD |
5620 | if (reg->type == PTR_TO_MAP_VALUE) { |
5621 | map = reg->map_ptr; | |
5622 | if (!map->btf) { | |
5623 | verbose(env, | |
5624 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
5625 | map->name); | |
5626 | return -EINVAL; | |
5627 | } | |
5628 | } else { | |
5629 | btf = reg->btf; | |
d83525ca | 5630 | } |
4e814da0 KKD |
5631 | |
5632 | rec = reg_btf_record(reg); | |
5633 | if (!btf_record_has_field(rec, BPF_SPIN_LOCK)) { | |
5634 | verbose(env, "%s '%s' has no valid bpf_spin_lock\n", map ? "map" : "local", | |
5635 | map ? map->name : "kptr"); | |
d83525ca AS |
5636 | return -EINVAL; |
5637 | } | |
4e814da0 | 5638 | if (rec->spin_lock_off != val + reg->off) { |
db559117 | 5639 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock' that is at %d\n", |
4e814da0 | 5640 | val + reg->off, rec->spin_lock_off); |
d83525ca AS |
5641 | return -EINVAL; |
5642 | } | |
5643 | if (is_lock) { | |
d0d78c1d | 5644 | if (cur->active_lock.ptr) { |
d83525ca AS |
5645 | verbose(env, |
5646 | "Locking two bpf_spin_locks are not allowed\n"); | |
5647 | return -EINVAL; | |
5648 | } | |
d0d78c1d KKD |
5649 | if (map) |
5650 | cur->active_lock.ptr = map; | |
5651 | else | |
5652 | cur->active_lock.ptr = btf; | |
5653 | cur->active_lock.id = reg->id; | |
d83525ca | 5654 | } else { |
d0d78c1d KKD |
5655 | void *ptr; |
5656 | ||
5657 | if (map) | |
5658 | ptr = map; | |
5659 | else | |
5660 | ptr = btf; | |
5661 | ||
5662 | if (!cur->active_lock.ptr) { | |
d83525ca AS |
5663 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
5664 | return -EINVAL; | |
5665 | } | |
d0d78c1d KKD |
5666 | if (cur->active_lock.ptr != ptr || |
5667 | cur->active_lock.id != reg->id) { | |
d83525ca AS |
5668 | verbose(env, "bpf_spin_unlock of different lock\n"); |
5669 | return -EINVAL; | |
5670 | } | |
d0d78c1d KKD |
5671 | cur->active_lock.ptr = NULL; |
5672 | cur->active_lock.id = 0; | |
d83525ca AS |
5673 | } |
5674 | return 0; | |
5675 | } | |
5676 | ||
b00628b1 AS |
5677 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
5678 | struct bpf_call_arg_meta *meta) | |
5679 | { | |
5680 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5681 | bool is_const = tnum_is_const(reg->var_off); | |
5682 | struct bpf_map *map = reg->map_ptr; | |
5683 | u64 val = reg->var_off.value; | |
5684 | ||
5685 | if (!is_const) { | |
5686 | verbose(env, | |
5687 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
5688 | regno); | |
5689 | return -EINVAL; | |
5690 | } | |
5691 | if (!map->btf) { | |
5692 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
5693 | map->name); | |
5694 | return -EINVAL; | |
5695 | } | |
db559117 KKD |
5696 | if (!btf_record_has_field(map->record, BPF_TIMER)) { |
5697 | verbose(env, "map '%s' has no valid bpf_timer\n", map->name); | |
68134668 AS |
5698 | return -EINVAL; |
5699 | } | |
db559117 | 5700 | if (map->record->timer_off != val + reg->off) { |
68134668 | 5701 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
db559117 | 5702 | val + reg->off, map->record->timer_off); |
b00628b1 AS |
5703 | return -EINVAL; |
5704 | } | |
5705 | if (meta->map_ptr) { | |
5706 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
5707 | return -EFAULT; | |
5708 | } | |
3e8ce298 | 5709 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
5710 | meta->map_ptr = map; |
5711 | return 0; | |
5712 | } | |
5713 | ||
c0a5a21c KKD |
5714 | static int process_kptr_func(struct bpf_verifier_env *env, int regno, |
5715 | struct bpf_call_arg_meta *meta) | |
5716 | { | |
5717 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
c0a5a21c | 5718 | struct bpf_map *map_ptr = reg->map_ptr; |
aa3496ac | 5719 | struct btf_field *kptr_field; |
c0a5a21c | 5720 | u32 kptr_off; |
c0a5a21c KKD |
5721 | |
5722 | if (!tnum_is_const(reg->var_off)) { | |
5723 | verbose(env, | |
5724 | "R%d doesn't have constant offset. kptr has to be at the constant offset\n", | |
5725 | regno); | |
5726 | return -EINVAL; | |
5727 | } | |
5728 | if (!map_ptr->btf) { | |
5729 | verbose(env, "map '%s' has to have BTF in order to use bpf_kptr_xchg\n", | |
5730 | map_ptr->name); | |
5731 | return -EINVAL; | |
5732 | } | |
aa3496ac KKD |
5733 | if (!btf_record_has_field(map_ptr->record, BPF_KPTR)) { |
5734 | verbose(env, "map '%s' has no valid kptr\n", map_ptr->name); | |
c0a5a21c KKD |
5735 | return -EINVAL; |
5736 | } | |
5737 | ||
5738 | meta->map_ptr = map_ptr; | |
5739 | kptr_off = reg->off + reg->var_off.value; | |
aa3496ac KKD |
5740 | kptr_field = btf_record_find(map_ptr->record, kptr_off, BPF_KPTR); |
5741 | if (!kptr_field) { | |
c0a5a21c KKD |
5742 | verbose(env, "off=%d doesn't point to kptr\n", kptr_off); |
5743 | return -EACCES; | |
5744 | } | |
aa3496ac | 5745 | if (kptr_field->type != BPF_KPTR_REF) { |
c0a5a21c KKD |
5746 | verbose(env, "off=%d kptr isn't referenced kptr\n", kptr_off); |
5747 | return -EACCES; | |
5748 | } | |
aa3496ac | 5749 | meta->kptr_field = kptr_field; |
c0a5a21c KKD |
5750 | return 0; |
5751 | } | |
5752 | ||
90133415 DB |
5753 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
5754 | { | |
5755 | return type == ARG_CONST_SIZE || | |
5756 | type == ARG_CONST_SIZE_OR_ZERO; | |
5757 | } | |
5758 | ||
8f14852e KKD |
5759 | static bool arg_type_is_release(enum bpf_arg_type type) |
5760 | { | |
5761 | return type & OBJ_RELEASE; | |
5762 | } | |
5763 | ||
97e03f52 JK |
5764 | static bool arg_type_is_dynptr(enum bpf_arg_type type) |
5765 | { | |
5766 | return base_type(type) == ARG_PTR_TO_DYNPTR; | |
5767 | } | |
5768 | ||
57c3bb72 AI |
5769 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
5770 | { | |
5771 | if (type == ARG_PTR_TO_INT) | |
5772 | return sizeof(u32); | |
5773 | else if (type == ARG_PTR_TO_LONG) | |
5774 | return sizeof(u64); | |
5775 | ||
5776 | return -EINVAL; | |
5777 | } | |
5778 | ||
912f442c LB |
5779 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
5780 | const struct bpf_call_arg_meta *meta, | |
5781 | enum bpf_arg_type *arg_type) | |
5782 | { | |
5783 | if (!meta->map_ptr) { | |
5784 | /* kernel subsystem misconfigured verifier */ | |
5785 | verbose(env, "invalid map_ptr to access map->type\n"); | |
5786 | return -EACCES; | |
5787 | } | |
5788 | ||
5789 | switch (meta->map_ptr->map_type) { | |
5790 | case BPF_MAP_TYPE_SOCKMAP: | |
5791 | case BPF_MAP_TYPE_SOCKHASH: | |
5792 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 5793 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
5794 | } else { |
5795 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
5796 | return -EINVAL; | |
5797 | } | |
5798 | break; | |
9330986c JK |
5799 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5800 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
5801 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
5802 | break; | |
912f442c LB |
5803 | default: |
5804 | break; | |
5805 | } | |
5806 | return 0; | |
5807 | } | |
5808 | ||
f79e7ea5 LB |
5809 | struct bpf_reg_types { |
5810 | const enum bpf_reg_type types[10]; | |
1df8f55a | 5811 | u32 *btf_id; |
f79e7ea5 LB |
5812 | }; |
5813 | ||
f79e7ea5 LB |
5814 | static const struct bpf_reg_types sock_types = { |
5815 | .types = { | |
5816 | PTR_TO_SOCK_COMMON, | |
5817 | PTR_TO_SOCKET, | |
5818 | PTR_TO_TCP_SOCK, | |
5819 | PTR_TO_XDP_SOCK, | |
5820 | }, | |
5821 | }; | |
5822 | ||
49a2a4d4 | 5823 | #ifdef CONFIG_NET |
1df8f55a MKL |
5824 | static const struct bpf_reg_types btf_id_sock_common_types = { |
5825 | .types = { | |
5826 | PTR_TO_SOCK_COMMON, | |
5827 | PTR_TO_SOCKET, | |
5828 | PTR_TO_TCP_SOCK, | |
5829 | PTR_TO_XDP_SOCK, | |
5830 | PTR_TO_BTF_ID, | |
5831 | }, | |
5832 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
5833 | }; | |
49a2a4d4 | 5834 | #endif |
1df8f55a | 5835 | |
f79e7ea5 LB |
5836 | static const struct bpf_reg_types mem_types = { |
5837 | .types = { | |
5838 | PTR_TO_STACK, | |
5839 | PTR_TO_PACKET, | |
5840 | PTR_TO_PACKET_META, | |
69c087ba | 5841 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5842 | PTR_TO_MAP_VALUE, |
5843 | PTR_TO_MEM, | |
894f2a8b | 5844 | PTR_TO_MEM | MEM_RINGBUF, |
20b2aff4 | 5845 | PTR_TO_BUF, |
f79e7ea5 LB |
5846 | }, |
5847 | }; | |
5848 | ||
5849 | static const struct bpf_reg_types int_ptr_types = { | |
5850 | .types = { | |
5851 | PTR_TO_STACK, | |
5852 | PTR_TO_PACKET, | |
5853 | PTR_TO_PACKET_META, | |
69c087ba | 5854 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5855 | PTR_TO_MAP_VALUE, |
5856 | }, | |
5857 | }; | |
5858 | ||
4e814da0 KKD |
5859 | static const struct bpf_reg_types spin_lock_types = { |
5860 | .types = { | |
5861 | PTR_TO_MAP_VALUE, | |
5862 | PTR_TO_BTF_ID | MEM_ALLOC, | |
5863 | } | |
5864 | }; | |
5865 | ||
f79e7ea5 LB |
5866 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
5867 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
5868 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
894f2a8b | 5869 | static const struct bpf_reg_types ringbuf_mem_types = { .types = { PTR_TO_MEM | MEM_RINGBUF } }; |
f79e7ea5 LB |
5870 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
5871 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
5844101a | 5872 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_BTF_ID | MEM_PERCPU } }; |
69c087ba YS |
5873 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
5874 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 5875 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 5876 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
c0a5a21c | 5877 | static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
20571567 DV |
5878 | static const struct bpf_reg_types dynptr_types = { |
5879 | .types = { | |
5880 | PTR_TO_STACK, | |
5881 | PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL, | |
5882 | } | |
5883 | }; | |
f79e7ea5 | 5884 | |
0789e13b | 5885 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
d1673304 DM |
5886 | [ARG_PTR_TO_MAP_KEY] = &mem_types, |
5887 | [ARG_PTR_TO_MAP_VALUE] = &mem_types, | |
f79e7ea5 LB |
5888 | [ARG_CONST_SIZE] = &scalar_types, |
5889 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
5890 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
5891 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
5892 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 5893 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 5894 | #ifdef CONFIG_NET |
1df8f55a | 5895 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 5896 | #endif |
f79e7ea5 | 5897 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
5898 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
5899 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
5900 | [ARG_PTR_TO_MEM] = &mem_types, | |
894f2a8b | 5901 | [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, |
f79e7ea5 LB |
5902 | [ARG_PTR_TO_INT] = &int_ptr_types, |
5903 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 5904 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 5905 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 5906 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 5907 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 5908 | [ARG_PTR_TO_TIMER] = &timer_types, |
c0a5a21c | 5909 | [ARG_PTR_TO_KPTR] = &kptr_types, |
20571567 | 5910 | [ARG_PTR_TO_DYNPTR] = &dynptr_types, |
f79e7ea5 LB |
5911 | }; |
5912 | ||
5913 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 | 5914 | enum bpf_arg_type arg_type, |
c0a5a21c KKD |
5915 | const u32 *arg_btf_id, |
5916 | struct bpf_call_arg_meta *meta) | |
f79e7ea5 LB |
5917 | { |
5918 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5919 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 5920 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
5921 | int i, j; |
5922 | ||
48946bd6 | 5923 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
5924 | if (!compatible) { |
5925 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
5926 | return -EFAULT; | |
5927 | } | |
5928 | ||
216e3cd2 HL |
5929 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
5930 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY | |
5931 | * | |
5932 | * Same for MAYBE_NULL: | |
5933 | * | |
5934 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, | |
5935 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL | |
5936 | * | |
5937 | * Therefore we fold these flags depending on the arg_type before comparison. | |
5938 | */ | |
5939 | if (arg_type & MEM_RDONLY) | |
5940 | type &= ~MEM_RDONLY; | |
5941 | if (arg_type & PTR_MAYBE_NULL) | |
5942 | type &= ~PTR_MAYBE_NULL; | |
5943 | ||
f79e7ea5 LB |
5944 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
5945 | expected = compatible->types[i]; | |
5946 | if (expected == NOT_INIT) | |
5947 | break; | |
5948 | ||
5949 | if (type == expected) | |
a968d5e2 | 5950 | goto found; |
f79e7ea5 LB |
5951 | } |
5952 | ||
216e3cd2 | 5953 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
f79e7ea5 | 5954 | for (j = 0; j + 1 < i; j++) |
c25b2ae1 HL |
5955 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
5956 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); | |
f79e7ea5 | 5957 | return -EACCES; |
a968d5e2 MKL |
5958 | |
5959 | found: | |
216e3cd2 | 5960 | if (reg->type == PTR_TO_BTF_ID) { |
2ab3b380 KKD |
5961 | /* For bpf_sk_release, it needs to match against first member |
5962 | * 'struct sock_common', hence make an exception for it. This | |
5963 | * allows bpf_sk_release to work for multiple socket types. | |
5964 | */ | |
5965 | bool strict_type_match = arg_type_is_release(arg_type) && | |
5966 | meta->func_id != BPF_FUNC_sk_release; | |
5967 | ||
1df8f55a MKL |
5968 | if (!arg_btf_id) { |
5969 | if (!compatible->btf_id) { | |
5970 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
5971 | return -EFAULT; | |
5972 | } | |
5973 | arg_btf_id = compatible->btf_id; | |
5974 | } | |
5975 | ||
c0a5a21c | 5976 | if (meta->func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac | 5977 | if (map_kptr_match_type(env, meta->kptr_field, reg, regno)) |
c0a5a21c | 5978 | return -EACCES; |
47e34cb7 DM |
5979 | } else { |
5980 | if (arg_btf_id == BPF_PTR_POISON) { | |
5981 | verbose(env, "verifier internal error:"); | |
5982 | verbose(env, "R%d has non-overwritten BPF_PTR_POISON type\n", | |
5983 | regno); | |
5984 | return -EACCES; | |
5985 | } | |
5986 | ||
5987 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, | |
5988 | btf_vmlinux, *arg_btf_id, | |
5989 | strict_type_match)) { | |
5990 | verbose(env, "R%d is of type %s but %s is expected\n", | |
5991 | regno, kernel_type_name(reg->btf, reg->btf_id), | |
5992 | kernel_type_name(btf_vmlinux, *arg_btf_id)); | |
5993 | return -EACCES; | |
5994 | } | |
a968d5e2 | 5995 | } |
4e814da0 KKD |
5996 | } else if (type_is_alloc(reg->type)) { |
5997 | if (meta->func_id != BPF_FUNC_spin_lock && meta->func_id != BPF_FUNC_spin_unlock) { | |
5998 | verbose(env, "verifier internal error: unimplemented handling of MEM_ALLOC\n"); | |
5999 | return -EFAULT; | |
6000 | } | |
a968d5e2 MKL |
6001 | } |
6002 | ||
6003 | return 0; | |
f79e7ea5 LB |
6004 | } |
6005 | ||
25b35dd2 KKD |
6006 | int check_func_arg_reg_off(struct bpf_verifier_env *env, |
6007 | const struct bpf_reg_state *reg, int regno, | |
8f14852e | 6008 | enum bpf_arg_type arg_type) |
25b35dd2 KKD |
6009 | { |
6010 | enum bpf_reg_type type = reg->type; | |
8f14852e | 6011 | bool fixed_off_ok = false; |
25b35dd2 KKD |
6012 | |
6013 | switch ((u32)type) { | |
25b35dd2 | 6014 | /* Pointer types where reg offset is explicitly allowed: */ |
97e03f52 JK |
6015 | case PTR_TO_STACK: |
6016 | if (arg_type_is_dynptr(arg_type) && reg->off % BPF_REG_SIZE) { | |
6017 | verbose(env, "cannot pass in dynptr at an offset\n"); | |
6018 | return -EINVAL; | |
6019 | } | |
6020 | fallthrough; | |
25b35dd2 KKD |
6021 | case PTR_TO_PACKET: |
6022 | case PTR_TO_PACKET_META: | |
6023 | case PTR_TO_MAP_KEY: | |
6024 | case PTR_TO_MAP_VALUE: | |
6025 | case PTR_TO_MEM: | |
6026 | case PTR_TO_MEM | MEM_RDONLY: | |
894f2a8b | 6027 | case PTR_TO_MEM | MEM_RINGBUF: |
25b35dd2 KKD |
6028 | case PTR_TO_BUF: |
6029 | case PTR_TO_BUF | MEM_RDONLY: | |
97e03f52 | 6030 | case SCALAR_VALUE: |
25b35dd2 KKD |
6031 | /* Some of the argument types nevertheless require a |
6032 | * zero register offset. | |
6033 | */ | |
894f2a8b | 6034 | if (base_type(arg_type) != ARG_PTR_TO_RINGBUF_MEM) |
25b35dd2 KKD |
6035 | return 0; |
6036 | break; | |
6037 | /* All the rest must be rejected, except PTR_TO_BTF_ID which allows | |
6038 | * fixed offset. | |
6039 | */ | |
6040 | case PTR_TO_BTF_ID: | |
282de143 | 6041 | case PTR_TO_BTF_ID | MEM_ALLOC: |
24d5bb80 | 6042 | /* When referenced PTR_TO_BTF_ID is passed to release function, |
8f14852e KKD |
6043 | * it's fixed offset must be 0. In the other cases, fixed offset |
6044 | * can be non-zero. | |
24d5bb80 | 6045 | */ |
8f14852e | 6046 | if (arg_type_is_release(arg_type) && reg->off) { |
24d5bb80 KKD |
6047 | verbose(env, "R%d must have zero offset when passed to release func\n", |
6048 | regno); | |
6049 | return -EINVAL; | |
6050 | } | |
8f14852e KKD |
6051 | /* For arg is release pointer, fixed_off_ok must be false, but |
6052 | * we already checked and rejected reg->off != 0 above, so set | |
6053 | * to true to allow fixed offset for all other cases. | |
24d5bb80 | 6054 | */ |
25b35dd2 KKD |
6055 | fixed_off_ok = true; |
6056 | break; | |
6057 | default: | |
6058 | break; | |
6059 | } | |
6060 | return __check_ptr_off_reg(env, reg, regno, fixed_off_ok); | |
6061 | } | |
6062 | ||
34d4ef57 JK |
6063 | static u32 stack_slot_get_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg) |
6064 | { | |
6065 | struct bpf_func_state *state = func(env, reg); | |
6066 | int spi = get_spi(reg->off); | |
6067 | ||
6068 | return state->stack[spi].spilled_ptr.id; | |
6069 | } | |
6070 | ||
af7ec138 YS |
6071 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
6072 | struct bpf_call_arg_meta *meta, | |
6073 | const struct bpf_func_proto *fn) | |
17a52670 | 6074 | { |
af7ec138 | 6075 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 6076 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 6077 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 6078 | enum bpf_reg_type type = reg->type; |
508362ac | 6079 | u32 *arg_btf_id = NULL; |
17a52670 AS |
6080 | int err = 0; |
6081 | ||
80f1d68c | 6082 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
6083 | return 0; |
6084 | ||
dc503a8a EC |
6085 | err = check_reg_arg(env, regno, SRC_OP); |
6086 | if (err) | |
6087 | return err; | |
17a52670 | 6088 | |
1be7f75d AS |
6089 | if (arg_type == ARG_ANYTHING) { |
6090 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
6091 | verbose(env, "R%d leaks addr into helper function\n", |
6092 | regno); | |
1be7f75d AS |
6093 | return -EACCES; |
6094 | } | |
80f1d68c | 6095 | return 0; |
1be7f75d | 6096 | } |
80f1d68c | 6097 | |
de8f3a83 | 6098 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 6099 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 6100 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
6101 | return -EACCES; |
6102 | } | |
6103 | ||
16d1e00c | 6104 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE) { |
912f442c LB |
6105 | err = resolve_map_arg_type(env, meta, &arg_type); |
6106 | if (err) | |
6107 | return err; | |
6108 | } | |
6109 | ||
48946bd6 | 6110 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
6111 | /* A NULL register has a SCALAR_VALUE type, so skip |
6112 | * type checking. | |
6113 | */ | |
6114 | goto skip_type_check; | |
6115 | ||
508362ac | 6116 | /* arg_btf_id and arg_size are in a union. */ |
4e814da0 KKD |
6117 | if (base_type(arg_type) == ARG_PTR_TO_BTF_ID || |
6118 | base_type(arg_type) == ARG_PTR_TO_SPIN_LOCK) | |
508362ac MM |
6119 | arg_btf_id = fn->arg_btf_id[arg]; |
6120 | ||
6121 | err = check_reg_type(env, regno, arg_type, arg_btf_id, meta); | |
f79e7ea5 LB |
6122 | if (err) |
6123 | return err; | |
6124 | ||
8f14852e | 6125 | err = check_func_arg_reg_off(env, reg, regno, arg_type); |
25b35dd2 KKD |
6126 | if (err) |
6127 | return err; | |
d7b9454a | 6128 | |
fd1b0d60 | 6129 | skip_type_check: |
8f14852e | 6130 | if (arg_type_is_release(arg_type)) { |
bc34dee6 JK |
6131 | if (arg_type_is_dynptr(arg_type)) { |
6132 | struct bpf_func_state *state = func(env, reg); | |
6133 | int spi = get_spi(reg->off); | |
6134 | ||
6135 | if (!is_spi_bounds_valid(state, spi, BPF_DYNPTR_NR_SLOTS) || | |
6136 | !state->stack[spi].spilled_ptr.id) { | |
6137 | verbose(env, "arg %d is an unacquired reference\n", regno); | |
6138 | return -EINVAL; | |
6139 | } | |
6140 | } else if (!reg->ref_obj_id && !register_is_null(reg)) { | |
8f14852e KKD |
6141 | verbose(env, "R%d must be referenced when passed to release function\n", |
6142 | regno); | |
6143 | return -EINVAL; | |
6144 | } | |
6145 | if (meta->release_regno) { | |
6146 | verbose(env, "verifier internal error: more than one release argument\n"); | |
6147 | return -EFAULT; | |
6148 | } | |
6149 | meta->release_regno = regno; | |
6150 | } | |
6151 | ||
02f7c958 | 6152 | if (reg->ref_obj_id) { |
457f4436 AN |
6153 | if (meta->ref_obj_id) { |
6154 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
6155 | regno, reg->ref_obj_id, | |
6156 | meta->ref_obj_id); | |
6157 | return -EFAULT; | |
6158 | } | |
6159 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
6160 | } |
6161 | ||
8ab4cdcf JK |
6162 | switch (base_type(arg_type)) { |
6163 | case ARG_CONST_MAP_PTR: | |
17a52670 | 6164 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
3e8ce298 AS |
6165 | if (meta->map_ptr) { |
6166 | /* Use map_uid (which is unique id of inner map) to reject: | |
6167 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
6168 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
6169 | * if (inner_map1 && inner_map2) { | |
6170 | * timer = bpf_map_lookup_elem(inner_map1); | |
6171 | * if (timer) | |
6172 | * // mismatch would have been allowed | |
6173 | * bpf_timer_init(timer, inner_map2); | |
6174 | * } | |
6175 | * | |
6176 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
6177 | */ | |
6178 | if (meta->map_ptr != reg->map_ptr || | |
6179 | meta->map_uid != reg->map_uid) { | |
6180 | verbose(env, | |
6181 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
6182 | meta->map_uid, reg->map_uid); | |
6183 | return -EINVAL; | |
6184 | } | |
b00628b1 | 6185 | } |
33ff9823 | 6186 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 6187 | meta->map_uid = reg->map_uid; |
8ab4cdcf JK |
6188 | break; |
6189 | case ARG_PTR_TO_MAP_KEY: | |
17a52670 AS |
6190 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
6191 | * check that [key, key + map->key_size) are within | |
6192 | * stack limits and initialized | |
6193 | */ | |
33ff9823 | 6194 | if (!meta->map_ptr) { |
17a52670 AS |
6195 | /* in function declaration map_ptr must come before |
6196 | * map_key, so that it's verified and known before | |
6197 | * we have to check map_key here. Otherwise it means | |
6198 | * that kernel subsystem misconfigured verifier | |
6199 | */ | |
61bd5218 | 6200 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
6201 | return -EACCES; |
6202 | } | |
d71962f3 PC |
6203 | err = check_helper_mem_access(env, regno, |
6204 | meta->map_ptr->key_size, false, | |
6205 | NULL); | |
8ab4cdcf JK |
6206 | break; |
6207 | case ARG_PTR_TO_MAP_VALUE: | |
48946bd6 HL |
6208 | if (type_may_be_null(arg_type) && register_is_null(reg)) |
6209 | return 0; | |
6210 | ||
17a52670 AS |
6211 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
6212 | * check [value, value + map->value_size) validity | |
6213 | */ | |
33ff9823 | 6214 | if (!meta->map_ptr) { |
17a52670 | 6215 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 6216 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
6217 | return -EACCES; |
6218 | } | |
16d1e00c | 6219 | meta->raw_mode = arg_type & MEM_UNINIT; |
d71962f3 PC |
6220 | err = check_helper_mem_access(env, regno, |
6221 | meta->map_ptr->value_size, false, | |
2ea864c5 | 6222 | meta); |
8ab4cdcf JK |
6223 | break; |
6224 | case ARG_PTR_TO_PERCPU_BTF_ID: | |
eaa6bcb7 HL |
6225 | if (!reg->btf_id) { |
6226 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
6227 | return -EACCES; | |
6228 | } | |
22dc4a0f | 6229 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 6230 | meta->ret_btf_id = reg->btf_id; |
8ab4cdcf JK |
6231 | break; |
6232 | case ARG_PTR_TO_SPIN_LOCK: | |
c18f0b6a LB |
6233 | if (meta->func_id == BPF_FUNC_spin_lock) { |
6234 | if (process_spin_lock(env, regno, true)) | |
6235 | return -EACCES; | |
6236 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
6237 | if (process_spin_lock(env, regno, false)) | |
6238 | return -EACCES; | |
6239 | } else { | |
6240 | verbose(env, "verifier internal error\n"); | |
6241 | return -EFAULT; | |
6242 | } | |
8ab4cdcf JK |
6243 | break; |
6244 | case ARG_PTR_TO_TIMER: | |
b00628b1 AS |
6245 | if (process_timer_func(env, regno, meta)) |
6246 | return -EACCES; | |
8ab4cdcf JK |
6247 | break; |
6248 | case ARG_PTR_TO_FUNC: | |
69c087ba | 6249 | meta->subprogno = reg->subprogno; |
8ab4cdcf JK |
6250 | break; |
6251 | case ARG_PTR_TO_MEM: | |
a2bbe7cc LB |
6252 | /* The access to this pointer is only checked when we hit the |
6253 | * next is_mem_size argument below. | |
6254 | */ | |
16d1e00c | 6255 | meta->raw_mode = arg_type & MEM_UNINIT; |
508362ac MM |
6256 | if (arg_type & MEM_FIXED_SIZE) { |
6257 | err = check_helper_mem_access(env, regno, | |
6258 | fn->arg_size[arg], false, | |
6259 | meta); | |
6260 | } | |
8ab4cdcf JK |
6261 | break; |
6262 | case ARG_CONST_SIZE: | |
6263 | err = check_mem_size_reg(env, reg, regno, false, meta); | |
6264 | break; | |
6265 | case ARG_CONST_SIZE_OR_ZERO: | |
6266 | err = check_mem_size_reg(env, reg, regno, true, meta); | |
6267 | break; | |
6268 | case ARG_PTR_TO_DYNPTR: | |
20571567 DV |
6269 | /* We only need to check for initialized / uninitialized helper |
6270 | * dynptr args if the dynptr is not PTR_TO_DYNPTR, as the | |
6271 | * assumption is that if it is, that a helper function | |
6272 | * initialized the dynptr on behalf of the BPF program. | |
6273 | */ | |
6274 | if (base_type(reg->type) == PTR_TO_DYNPTR) | |
6275 | break; | |
97e03f52 JK |
6276 | if (arg_type & MEM_UNINIT) { |
6277 | if (!is_dynptr_reg_valid_uninit(env, reg)) { | |
6278 | verbose(env, "Dynptr has to be an uninitialized dynptr\n"); | |
6279 | return -EINVAL; | |
6280 | } | |
6281 | ||
6282 | /* We only support one dynptr being uninitialized at the moment, | |
6283 | * which is sufficient for the helper functions we have right now. | |
6284 | */ | |
6285 | if (meta->uninit_dynptr_regno) { | |
6286 | verbose(env, "verifier internal error: multiple uninitialized dynptr args\n"); | |
6287 | return -EFAULT; | |
6288 | } | |
6289 | ||
6290 | meta->uninit_dynptr_regno = regno; | |
e9e315b4 RS |
6291 | } else if (!is_dynptr_reg_valid_init(env, reg)) { |
6292 | verbose(env, | |
6293 | "Expected an initialized dynptr as arg #%d\n", | |
6294 | arg + 1); | |
6295 | return -EINVAL; | |
6296 | } else if (!is_dynptr_type_expected(env, reg, arg_type)) { | |
97e03f52 JK |
6297 | const char *err_extra = ""; |
6298 | ||
6299 | switch (arg_type & DYNPTR_TYPE_FLAG_MASK) { | |
6300 | case DYNPTR_TYPE_LOCAL: | |
e9e315b4 | 6301 | err_extra = "local"; |
97e03f52 | 6302 | break; |
bc34dee6 | 6303 | case DYNPTR_TYPE_RINGBUF: |
e9e315b4 | 6304 | err_extra = "ringbuf"; |
bc34dee6 | 6305 | break; |
97e03f52 | 6306 | default: |
e9e315b4 | 6307 | err_extra = "<unknown>"; |
97e03f52 JK |
6308 | break; |
6309 | } | |
e9e315b4 RS |
6310 | verbose(env, |
6311 | "Expected a dynptr of type %s as arg #%d\n", | |
97e03f52 JK |
6312 | err_extra, arg + 1); |
6313 | return -EINVAL; | |
6314 | } | |
8ab4cdcf JK |
6315 | break; |
6316 | case ARG_CONST_ALLOC_SIZE_OR_ZERO: | |
457f4436 | 6317 | if (!tnum_is_const(reg->var_off)) { |
28a8add6 | 6318 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
6319 | regno); |
6320 | return -EACCES; | |
6321 | } | |
6322 | meta->mem_size = reg->var_off.value; | |
2fc31465 KKD |
6323 | err = mark_chain_precision(env, regno); |
6324 | if (err) | |
6325 | return err; | |
8ab4cdcf JK |
6326 | break; |
6327 | case ARG_PTR_TO_INT: | |
6328 | case ARG_PTR_TO_LONG: | |
6329 | { | |
57c3bb72 AI |
6330 | int size = int_ptr_type_to_size(arg_type); |
6331 | ||
6332 | err = check_helper_mem_access(env, regno, size, false, meta); | |
6333 | if (err) | |
6334 | return err; | |
6335 | err = check_ptr_alignment(env, reg, 0, size, true); | |
8ab4cdcf JK |
6336 | break; |
6337 | } | |
6338 | case ARG_PTR_TO_CONST_STR: | |
6339 | { | |
fff13c4b FR |
6340 | struct bpf_map *map = reg->map_ptr; |
6341 | int map_off; | |
6342 | u64 map_addr; | |
6343 | char *str_ptr; | |
6344 | ||
a8fad73e | 6345 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
6346 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
6347 | return -EACCES; | |
6348 | } | |
6349 | ||
6350 | if (!tnum_is_const(reg->var_off)) { | |
6351 | verbose(env, "R%d is not a constant address'\n", regno); | |
6352 | return -EACCES; | |
6353 | } | |
6354 | ||
6355 | if (!map->ops->map_direct_value_addr) { | |
6356 | verbose(env, "no direct value access support for this map type\n"); | |
6357 | return -EACCES; | |
6358 | } | |
6359 | ||
6360 | err = check_map_access(env, regno, reg->off, | |
61df10c7 KKD |
6361 | map->value_size - reg->off, false, |
6362 | ACCESS_HELPER); | |
fff13c4b FR |
6363 | if (err) |
6364 | return err; | |
6365 | ||
6366 | map_off = reg->off + reg->var_off.value; | |
6367 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
6368 | if (err) { | |
6369 | verbose(env, "direct value access on string failed\n"); | |
6370 | return err; | |
6371 | } | |
6372 | ||
6373 | str_ptr = (char *)(long)(map_addr); | |
6374 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
6375 | verbose(env, "string is not zero-terminated\n"); | |
6376 | return -EINVAL; | |
6377 | } | |
8ab4cdcf JK |
6378 | break; |
6379 | } | |
6380 | case ARG_PTR_TO_KPTR: | |
c0a5a21c KKD |
6381 | if (process_kptr_func(env, regno, meta)) |
6382 | return -EACCES; | |
8ab4cdcf | 6383 | break; |
17a52670 AS |
6384 | } |
6385 | ||
6386 | return err; | |
6387 | } | |
6388 | ||
0126240f LB |
6389 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
6390 | { | |
6391 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 6392 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
6393 | |
6394 | if (func_id != BPF_FUNC_map_update_elem) | |
6395 | return false; | |
6396 | ||
6397 | /* It's not possible to get access to a locked struct sock in these | |
6398 | * contexts, so updating is safe. | |
6399 | */ | |
6400 | switch (type) { | |
6401 | case BPF_PROG_TYPE_TRACING: | |
6402 | if (eatype == BPF_TRACE_ITER) | |
6403 | return true; | |
6404 | break; | |
6405 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
6406 | case BPF_PROG_TYPE_SCHED_CLS: | |
6407 | case BPF_PROG_TYPE_SCHED_ACT: | |
6408 | case BPF_PROG_TYPE_XDP: | |
6409 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
6410 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
6411 | case BPF_PROG_TYPE_SK_LOOKUP: | |
6412 | return true; | |
6413 | default: | |
6414 | break; | |
6415 | } | |
6416 | ||
6417 | verbose(env, "cannot update sockmap in this context\n"); | |
6418 | return false; | |
6419 | } | |
6420 | ||
e411901c MF |
6421 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
6422 | { | |
95acd881 TA |
6423 | return env->prog->jit_requested && |
6424 | bpf_jit_supports_subprog_tailcalls(); | |
e411901c MF |
6425 | } |
6426 | ||
61bd5218 JK |
6427 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
6428 | struct bpf_map *map, int func_id) | |
35578d79 | 6429 | { |
35578d79 KX |
6430 | if (!map) |
6431 | return 0; | |
6432 | ||
6aff67c8 AS |
6433 | /* We need a two way check, first is from map perspective ... */ |
6434 | switch (map->map_type) { | |
6435 | case BPF_MAP_TYPE_PROG_ARRAY: | |
6436 | if (func_id != BPF_FUNC_tail_call) | |
6437 | goto error; | |
6438 | break; | |
6439 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
6440 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 6441 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 6442 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
6443 | func_id != BPF_FUNC_perf_event_read_value && |
6444 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
6445 | goto error; |
6446 | break; | |
457f4436 AN |
6447 | case BPF_MAP_TYPE_RINGBUF: |
6448 | if (func_id != BPF_FUNC_ringbuf_output && | |
6449 | func_id != BPF_FUNC_ringbuf_reserve && | |
bc34dee6 JK |
6450 | func_id != BPF_FUNC_ringbuf_query && |
6451 | func_id != BPF_FUNC_ringbuf_reserve_dynptr && | |
6452 | func_id != BPF_FUNC_ringbuf_submit_dynptr && | |
6453 | func_id != BPF_FUNC_ringbuf_discard_dynptr) | |
457f4436 AN |
6454 | goto error; |
6455 | break; | |
583c1f42 | 6456 | case BPF_MAP_TYPE_USER_RINGBUF: |
20571567 DV |
6457 | if (func_id != BPF_FUNC_user_ringbuf_drain) |
6458 | goto error; | |
6459 | break; | |
6aff67c8 AS |
6460 | case BPF_MAP_TYPE_STACK_TRACE: |
6461 | if (func_id != BPF_FUNC_get_stackid) | |
6462 | goto error; | |
6463 | break; | |
4ed8ec52 | 6464 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 6465 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 6466 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
6467 | goto error; |
6468 | break; | |
cd339431 | 6469 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 6470 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
6471 | if (func_id != BPF_FUNC_get_local_storage) |
6472 | goto error; | |
6473 | break; | |
546ac1ff | 6474 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 6475 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
6476 | if (func_id != BPF_FUNC_redirect_map && |
6477 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
6478 | goto error; |
6479 | break; | |
fbfc504a BT |
6480 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
6481 | * appear. | |
6482 | */ | |
6710e112 JDB |
6483 | case BPF_MAP_TYPE_CPUMAP: |
6484 | if (func_id != BPF_FUNC_redirect_map) | |
6485 | goto error; | |
6486 | break; | |
fada7fdc JL |
6487 | case BPF_MAP_TYPE_XSKMAP: |
6488 | if (func_id != BPF_FUNC_redirect_map && | |
6489 | func_id != BPF_FUNC_map_lookup_elem) | |
6490 | goto error; | |
6491 | break; | |
56f668df | 6492 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 6493 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
6494 | if (func_id != BPF_FUNC_map_lookup_elem) |
6495 | goto error; | |
16a43625 | 6496 | break; |
174a79ff JF |
6497 | case BPF_MAP_TYPE_SOCKMAP: |
6498 | if (func_id != BPF_FUNC_sk_redirect_map && | |
6499 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 6500 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 6501 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 6502 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
6503 | func_id != BPF_FUNC_map_lookup_elem && |
6504 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
6505 | goto error; |
6506 | break; | |
81110384 JF |
6507 | case BPF_MAP_TYPE_SOCKHASH: |
6508 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
6509 | func_id != BPF_FUNC_sock_hash_update && | |
6510 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 6511 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 6512 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
6513 | func_id != BPF_FUNC_map_lookup_elem && |
6514 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
6515 | goto error; |
6516 | break; | |
2dbb9b9e MKL |
6517 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
6518 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
6519 | goto error; | |
6520 | break; | |
f1a2e44a MV |
6521 | case BPF_MAP_TYPE_QUEUE: |
6522 | case BPF_MAP_TYPE_STACK: | |
6523 | if (func_id != BPF_FUNC_map_peek_elem && | |
6524 | func_id != BPF_FUNC_map_pop_elem && | |
6525 | func_id != BPF_FUNC_map_push_elem) | |
6526 | goto error; | |
6527 | break; | |
6ac99e8f MKL |
6528 | case BPF_MAP_TYPE_SK_STORAGE: |
6529 | if (func_id != BPF_FUNC_sk_storage_get && | |
6530 | func_id != BPF_FUNC_sk_storage_delete) | |
6531 | goto error; | |
6532 | break; | |
8ea63684 KS |
6533 | case BPF_MAP_TYPE_INODE_STORAGE: |
6534 | if (func_id != BPF_FUNC_inode_storage_get && | |
6535 | func_id != BPF_FUNC_inode_storage_delete) | |
6536 | goto error; | |
6537 | break; | |
4cf1bc1f KS |
6538 | case BPF_MAP_TYPE_TASK_STORAGE: |
6539 | if (func_id != BPF_FUNC_task_storage_get && | |
6540 | func_id != BPF_FUNC_task_storage_delete) | |
6541 | goto error; | |
6542 | break; | |
c4bcfb38 YS |
6543 | case BPF_MAP_TYPE_CGRP_STORAGE: |
6544 | if (func_id != BPF_FUNC_cgrp_storage_get && | |
6545 | func_id != BPF_FUNC_cgrp_storage_delete) | |
6546 | goto error; | |
6547 | break; | |
9330986c JK |
6548 | case BPF_MAP_TYPE_BLOOM_FILTER: |
6549 | if (func_id != BPF_FUNC_map_peek_elem && | |
6550 | func_id != BPF_FUNC_map_push_elem) | |
6551 | goto error; | |
6552 | break; | |
6aff67c8 AS |
6553 | default: |
6554 | break; | |
6555 | } | |
6556 | ||
6557 | /* ... and second from the function itself. */ | |
6558 | switch (func_id) { | |
6559 | case BPF_FUNC_tail_call: | |
6560 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
6561 | goto error; | |
e411901c MF |
6562 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
6563 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
6564 | return -EINVAL; |
6565 | } | |
6aff67c8 AS |
6566 | break; |
6567 | case BPF_FUNC_perf_event_read: | |
6568 | case BPF_FUNC_perf_event_output: | |
908432ca | 6569 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 6570 | case BPF_FUNC_skb_output: |
d831ee84 | 6571 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
6572 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
6573 | goto error; | |
6574 | break; | |
5b029a32 DB |
6575 | case BPF_FUNC_ringbuf_output: |
6576 | case BPF_FUNC_ringbuf_reserve: | |
6577 | case BPF_FUNC_ringbuf_query: | |
bc34dee6 JK |
6578 | case BPF_FUNC_ringbuf_reserve_dynptr: |
6579 | case BPF_FUNC_ringbuf_submit_dynptr: | |
6580 | case BPF_FUNC_ringbuf_discard_dynptr: | |
5b029a32 DB |
6581 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
6582 | goto error; | |
6583 | break; | |
20571567 DV |
6584 | case BPF_FUNC_user_ringbuf_drain: |
6585 | if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF) | |
6586 | goto error; | |
6587 | break; | |
6aff67c8 AS |
6588 | case BPF_FUNC_get_stackid: |
6589 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
6590 | goto error; | |
6591 | break; | |
60d20f91 | 6592 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 6593 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
6594 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
6595 | goto error; | |
6596 | break; | |
97f91a7c | 6597 | case BPF_FUNC_redirect_map: |
9c270af3 | 6598 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 6599 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
6600 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
6601 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
6602 | goto error; |
6603 | break; | |
174a79ff | 6604 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 6605 | case BPF_FUNC_msg_redirect_map: |
81110384 | 6606 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
6607 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
6608 | goto error; | |
6609 | break; | |
81110384 JF |
6610 | case BPF_FUNC_sk_redirect_hash: |
6611 | case BPF_FUNC_msg_redirect_hash: | |
6612 | case BPF_FUNC_sock_hash_update: | |
6613 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
6614 | goto error; |
6615 | break; | |
cd339431 | 6616 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
6617 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
6618 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
6619 | goto error; |
6620 | break; | |
2dbb9b9e | 6621 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
6622 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
6623 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
6624 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
6625 | goto error; |
6626 | break; | |
f1a2e44a | 6627 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
6628 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
6629 | map->map_type != BPF_MAP_TYPE_STACK) | |
6630 | goto error; | |
6631 | break; | |
9330986c JK |
6632 | case BPF_FUNC_map_peek_elem: |
6633 | case BPF_FUNC_map_push_elem: | |
6634 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
6635 | map->map_type != BPF_MAP_TYPE_STACK && | |
6636 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
6637 | goto error; | |
6638 | break; | |
07343110 FZ |
6639 | case BPF_FUNC_map_lookup_percpu_elem: |
6640 | if (map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && | |
6641 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && | |
6642 | map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH) | |
6643 | goto error; | |
6644 | break; | |
6ac99e8f MKL |
6645 | case BPF_FUNC_sk_storage_get: |
6646 | case BPF_FUNC_sk_storage_delete: | |
6647 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
6648 | goto error; | |
6649 | break; | |
8ea63684 KS |
6650 | case BPF_FUNC_inode_storage_get: |
6651 | case BPF_FUNC_inode_storage_delete: | |
6652 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
6653 | goto error; | |
6654 | break; | |
4cf1bc1f KS |
6655 | case BPF_FUNC_task_storage_get: |
6656 | case BPF_FUNC_task_storage_delete: | |
6657 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
6658 | goto error; | |
6659 | break; | |
c4bcfb38 YS |
6660 | case BPF_FUNC_cgrp_storage_get: |
6661 | case BPF_FUNC_cgrp_storage_delete: | |
6662 | if (map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) | |
6663 | goto error; | |
6664 | break; | |
6aff67c8 AS |
6665 | default: |
6666 | break; | |
35578d79 KX |
6667 | } |
6668 | ||
6669 | return 0; | |
6aff67c8 | 6670 | error: |
61bd5218 | 6671 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 6672 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 6673 | return -EINVAL; |
35578d79 KX |
6674 | } |
6675 | ||
90133415 | 6676 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
6677 | { |
6678 | int count = 0; | |
6679 | ||
39f19ebb | 6680 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6681 | count++; |
39f19ebb | 6682 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6683 | count++; |
39f19ebb | 6684 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6685 | count++; |
39f19ebb | 6686 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 6687 | count++; |
39f19ebb | 6688 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
6689 | count++; |
6690 | ||
90133415 DB |
6691 | /* We only support one arg being in raw mode at the moment, |
6692 | * which is sufficient for the helper functions we have | |
6693 | * right now. | |
6694 | */ | |
6695 | return count <= 1; | |
6696 | } | |
6697 | ||
508362ac | 6698 | static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg) |
90133415 | 6699 | { |
508362ac MM |
6700 | bool is_fixed = fn->arg_type[arg] & MEM_FIXED_SIZE; |
6701 | bool has_size = fn->arg_size[arg] != 0; | |
6702 | bool is_next_size = false; | |
6703 | ||
6704 | if (arg + 1 < ARRAY_SIZE(fn->arg_type)) | |
6705 | is_next_size = arg_type_is_mem_size(fn->arg_type[arg + 1]); | |
6706 | ||
6707 | if (base_type(fn->arg_type[arg]) != ARG_PTR_TO_MEM) | |
6708 | return is_next_size; | |
6709 | ||
6710 | return has_size == is_next_size || is_next_size == is_fixed; | |
90133415 DB |
6711 | } |
6712 | ||
6713 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
6714 | { | |
6715 | /* bpf_xxx(..., buf, len) call will access 'len' | |
6716 | * bytes from memory 'buf'. Both arg types need | |
6717 | * to be paired, so make sure there's no buggy | |
6718 | * helper function specification. | |
6719 | */ | |
6720 | if (arg_type_is_mem_size(fn->arg1_type) || | |
508362ac MM |
6721 | check_args_pair_invalid(fn, 0) || |
6722 | check_args_pair_invalid(fn, 1) || | |
6723 | check_args_pair_invalid(fn, 2) || | |
6724 | check_args_pair_invalid(fn, 3) || | |
6725 | check_args_pair_invalid(fn, 4)) | |
90133415 DB |
6726 | return false; |
6727 | ||
6728 | return true; | |
6729 | } | |
6730 | ||
9436ef6e LB |
6731 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
6732 | { | |
6733 | int i; | |
6734 | ||
1df8f55a | 6735 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
4e814da0 KKD |
6736 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_BTF_ID) |
6737 | return !!fn->arg_btf_id[i]; | |
6738 | if (base_type(fn->arg_type[i]) == ARG_PTR_TO_SPIN_LOCK) | |
6739 | return fn->arg_btf_id[i] == BPF_PTR_POISON; | |
508362ac MM |
6740 | if (base_type(fn->arg_type[i]) != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i] && |
6741 | /* arg_btf_id and arg_size are in a union. */ | |
6742 | (base_type(fn->arg_type[i]) != ARG_PTR_TO_MEM || | |
6743 | !(fn->arg_type[i] & MEM_FIXED_SIZE))) | |
1df8f55a MKL |
6744 | return false; |
6745 | } | |
6746 | ||
9436ef6e LB |
6747 | return true; |
6748 | } | |
6749 | ||
0c9a7a7e | 6750 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
6751 | { |
6752 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 6753 | check_arg_pair_ok(fn) && |
b2d8ef19 | 6754 | check_btf_id_ok(fn) ? 0 : -EINVAL; |
435faee1 DB |
6755 | } |
6756 | ||
de8f3a83 DB |
6757 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
6758 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 6759 | */ |
b239da34 | 6760 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 6761 | { |
b239da34 KKD |
6762 | struct bpf_func_state *state; |
6763 | struct bpf_reg_state *reg; | |
969bf05e | 6764 | |
b239da34 | 6765 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
de8f3a83 | 6766 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 6767 | __mark_reg_unknown(env, reg); |
b239da34 | 6768 | })); |
f4d7e40a AS |
6769 | } |
6770 | ||
6d94e741 AS |
6771 | enum { |
6772 | AT_PKT_END = -1, | |
6773 | BEYOND_PKT_END = -2, | |
6774 | }; | |
6775 | ||
6776 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
6777 | { | |
6778 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
6779 | struct bpf_reg_state *reg = &state->regs[regn]; | |
6780 | ||
6781 | if (reg->type != PTR_TO_PACKET) | |
6782 | /* PTR_TO_PACKET_META is not supported yet */ | |
6783 | return; | |
6784 | ||
6785 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
6786 | * How far beyond pkt_end it goes is unknown. | |
6787 | * if (!range_open) it's the case of pkt >= pkt_end | |
6788 | * if (range_open) it's the case of pkt > pkt_end | |
6789 | * hence this pointer is at least 1 byte bigger than pkt_end | |
6790 | */ | |
6791 | if (range_open) | |
6792 | reg->range = BEYOND_PKT_END; | |
6793 | else | |
6794 | reg->range = AT_PKT_END; | |
6795 | } | |
6796 | ||
fd978bf7 JS |
6797 | /* The pointer with the specified id has released its reference to kernel |
6798 | * resources. Identify all copies of the same pointer and clear the reference. | |
6799 | */ | |
6800 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 6801 | int ref_obj_id) |
fd978bf7 | 6802 | { |
b239da34 KKD |
6803 | struct bpf_func_state *state; |
6804 | struct bpf_reg_state *reg; | |
1b986589 | 6805 | int err; |
fd978bf7 | 6806 | |
1b986589 MKL |
6807 | err = release_reference_state(cur_func(env), ref_obj_id); |
6808 | if (err) | |
6809 | return err; | |
6810 | ||
b239da34 | 6811 | bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ |
f1db2081 YL |
6812 | if (reg->ref_obj_id == ref_obj_id) { |
6813 | if (!env->allow_ptr_leaks) | |
6814 | __mark_reg_not_init(env, reg); | |
6815 | else | |
6816 | __mark_reg_unknown(env, reg); | |
6817 | } | |
b239da34 | 6818 | })); |
fd978bf7 | 6819 | |
1b986589 | 6820 | return 0; |
fd978bf7 JS |
6821 | } |
6822 | ||
51c39bb1 AS |
6823 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
6824 | struct bpf_reg_state *regs) | |
6825 | { | |
6826 | int i; | |
6827 | ||
6828 | /* after the call registers r0 - r5 were scratched */ | |
6829 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
6830 | mark_reg_not_init(env, regs, caller_saved[i]); | |
6831 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
6832 | } | |
6833 | } | |
6834 | ||
14351375 YS |
6835 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
6836 | struct bpf_func_state *caller, | |
6837 | struct bpf_func_state *callee, | |
6838 | int insn_idx); | |
6839 | ||
be2ef816 AN |
6840 | static int set_callee_state(struct bpf_verifier_env *env, |
6841 | struct bpf_func_state *caller, | |
6842 | struct bpf_func_state *callee, int insn_idx); | |
6843 | ||
14351375 YS |
6844 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
6845 | int *insn_idx, int subprog, | |
6846 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
6847 | { |
6848 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 6849 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 6850 | struct bpf_func_state *caller, *callee; |
14351375 | 6851 | int err; |
51c39bb1 | 6852 | bool is_global = false; |
f4d7e40a | 6853 | |
aada9ce6 | 6854 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 6855 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 6856 | state->curframe + 2); |
f4d7e40a AS |
6857 | return -E2BIG; |
6858 | } | |
6859 | ||
f4d7e40a AS |
6860 | caller = state->frame[state->curframe]; |
6861 | if (state->frame[state->curframe + 1]) { | |
6862 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
6863 | state->curframe + 1); | |
6864 | return -EFAULT; | |
6865 | } | |
6866 | ||
51c39bb1 AS |
6867 | func_info_aux = env->prog->aux->func_info_aux; |
6868 | if (func_info_aux) | |
6869 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
95f2f26f | 6870 | err = btf_check_subprog_call(env, subprog, caller->regs); |
51c39bb1 AS |
6871 | if (err == -EFAULT) |
6872 | return err; | |
6873 | if (is_global) { | |
6874 | if (err) { | |
6875 | verbose(env, "Caller passes invalid args into func#%d\n", | |
6876 | subprog); | |
6877 | return err; | |
6878 | } else { | |
6879 | if (env->log.level & BPF_LOG_LEVEL) | |
6880 | verbose(env, | |
6881 | "Func#%d is global and valid. Skipping.\n", | |
6882 | subprog); | |
6883 | clear_caller_saved_regs(env, caller->regs); | |
6884 | ||
45159b27 | 6885 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 6886 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 6887 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
6888 | |
6889 | /* continue with next insn after call */ | |
6890 | return 0; | |
6891 | } | |
6892 | } | |
6893 | ||
be2ef816 AN |
6894 | /* set_callee_state is used for direct subprog calls, but we are |
6895 | * interested in validating only BPF helpers that can call subprogs as | |
6896 | * callbacks | |
6897 | */ | |
6898 | if (set_callee_state_cb != set_callee_state && !is_callback_calling_function(insn->imm)) { | |
6899 | verbose(env, "verifier bug: helper %s#%d is not marked as callback-calling\n", | |
6900 | func_id_name(insn->imm), insn->imm); | |
6901 | return -EFAULT; | |
6902 | } | |
6903 | ||
bfc6bb74 | 6904 | if (insn->code == (BPF_JMP | BPF_CALL) && |
a5bebc4f | 6905 | insn->src_reg == 0 && |
bfc6bb74 AS |
6906 | insn->imm == BPF_FUNC_timer_set_callback) { |
6907 | struct bpf_verifier_state *async_cb; | |
6908 | ||
6909 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 6910 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
6911 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
6912 | *insn_idx, subprog); | |
6913 | if (!async_cb) | |
6914 | return -EFAULT; | |
6915 | callee = async_cb->frame[0]; | |
6916 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
6917 | ||
6918 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
6919 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
6920 | if (err) | |
6921 | return err; | |
6922 | ||
6923 | clear_caller_saved_regs(env, caller->regs); | |
6924 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
6925 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
6926 | /* continue with next insn after call */ | |
6927 | return 0; | |
6928 | } | |
6929 | ||
f4d7e40a AS |
6930 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
6931 | if (!callee) | |
6932 | return -ENOMEM; | |
6933 | state->frame[state->curframe + 1] = callee; | |
6934 | ||
6935 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
6936 | * into its own stack before reading from it. | |
6937 | * callee can read/write into caller's stack | |
6938 | */ | |
6939 | init_func_state(env, callee, | |
6940 | /* remember the callsite, it will be used by bpf_exit */ | |
6941 | *insn_idx /* callsite */, | |
6942 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 6943 | subprog /* subprog number within this prog */); |
f4d7e40a | 6944 | |
fd978bf7 | 6945 | /* Transfer references to the callee */ |
c69431aa | 6946 | err = copy_reference_state(callee, caller); |
fd978bf7 JS |
6947 | if (err) |
6948 | return err; | |
6949 | ||
14351375 YS |
6950 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
6951 | if (err) | |
6952 | return err; | |
f4d7e40a | 6953 | |
51c39bb1 | 6954 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
6955 | |
6956 | /* only increment it after check_reg_arg() finished */ | |
6957 | state->curframe++; | |
6958 | ||
6959 | /* and go analyze first insn of the callee */ | |
14351375 | 6960 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 6961 | |
06ee7115 | 6962 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 6963 | verbose(env, "caller:\n"); |
0f55f9ed | 6964 | print_verifier_state(env, caller, true); |
f4d7e40a | 6965 | verbose(env, "callee:\n"); |
0f55f9ed | 6966 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
6967 | } |
6968 | return 0; | |
6969 | } | |
6970 | ||
314ee05e YS |
6971 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
6972 | struct bpf_func_state *caller, | |
6973 | struct bpf_func_state *callee) | |
6974 | { | |
6975 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
6976 | * void *callback_ctx, u64 flags); | |
6977 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
6978 | * void *callback_ctx); | |
6979 | */ | |
6980 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6981 | ||
6982 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6983 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6984 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6985 | ||
6986 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6987 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6988 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6989 | ||
6990 | /* pointer to stack or null */ | |
6991 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
6992 | ||
6993 | /* unused */ | |
6994 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6995 | return 0; | |
6996 | } | |
6997 | ||
14351375 YS |
6998 | static int set_callee_state(struct bpf_verifier_env *env, |
6999 | struct bpf_func_state *caller, | |
7000 | struct bpf_func_state *callee, int insn_idx) | |
7001 | { | |
7002 | int i; | |
7003 | ||
7004 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
7005 | * pointers, which connects us up to the liveness chain | |
7006 | */ | |
7007 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
7008 | callee->regs[i] = caller->regs[i]; | |
7009 | return 0; | |
7010 | } | |
7011 | ||
7012 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
7013 | int *insn_idx) | |
7014 | { | |
7015 | int subprog, target_insn; | |
7016 | ||
7017 | target_insn = *insn_idx + insn->imm + 1; | |
7018 | subprog = find_subprog(env, target_insn); | |
7019 | if (subprog < 0) { | |
7020 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
7021 | target_insn); | |
7022 | return -EFAULT; | |
7023 | } | |
7024 | ||
7025 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
7026 | } | |
7027 | ||
69c087ba YS |
7028 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
7029 | struct bpf_func_state *caller, | |
7030 | struct bpf_func_state *callee, | |
7031 | int insn_idx) | |
7032 | { | |
7033 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
7034 | struct bpf_map *map; | |
7035 | int err; | |
7036 | ||
7037 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
7038 | verbose(env, "tail_call abusing map_ptr\n"); | |
7039 | return -EINVAL; | |
7040 | } | |
7041 | ||
7042 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
7043 | if (!map->ops->map_set_for_each_callback_args || | |
7044 | !map->ops->map_for_each_callback) { | |
7045 | verbose(env, "callback function not allowed for map\n"); | |
7046 | return -ENOTSUPP; | |
7047 | } | |
7048 | ||
7049 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
7050 | if (err) | |
7051 | return err; | |
7052 | ||
7053 | callee->in_callback_fn = true; | |
1bfe26fb | 7054 | callee->callback_ret_range = tnum_range(0, 1); |
69c087ba YS |
7055 | return 0; |
7056 | } | |
7057 | ||
e6f2dd0f JK |
7058 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
7059 | struct bpf_func_state *caller, | |
7060 | struct bpf_func_state *callee, | |
7061 | int insn_idx) | |
7062 | { | |
7063 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
7064 | * u64 flags); | |
7065 | * callback_fn(u32 index, void *callback_ctx); | |
7066 | */ | |
7067 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
7068 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
7069 | ||
7070 | /* unused */ | |
7071 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
7072 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7073 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
7074 | ||
7075 | callee->in_callback_fn = true; | |
1bfe26fb | 7076 | callee->callback_ret_range = tnum_range(0, 1); |
e6f2dd0f JK |
7077 | return 0; |
7078 | } | |
7079 | ||
b00628b1 AS |
7080 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
7081 | struct bpf_func_state *caller, | |
7082 | struct bpf_func_state *callee, | |
7083 | int insn_idx) | |
7084 | { | |
7085 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
7086 | ||
7087 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
7088 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
7089 | */ | |
7090 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
7091 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
7092 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
7093 | ||
7094 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
7095 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
7096 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
7097 | ||
7098 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
7099 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
7100 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
7101 | ||
7102 | /* unused */ | |
7103 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7104 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 7105 | callee->in_async_callback_fn = true; |
1bfe26fb | 7106 | callee->callback_ret_range = tnum_range(0, 1); |
b00628b1 AS |
7107 | return 0; |
7108 | } | |
7109 | ||
7c7e3d31 SL |
7110 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
7111 | struct bpf_func_state *caller, | |
7112 | struct bpf_func_state *callee, | |
7113 | int insn_idx) | |
7114 | { | |
7115 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
7116 | * void *callback_fn, void *callback_ctx, u64 flags) | |
7117 | * (callback_fn)(struct task_struct *task, | |
7118 | * struct vm_area_struct *vma, void *callback_ctx); | |
7119 | */ | |
7120 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
7121 | ||
7122 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
7123 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
7124 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 7125 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
7126 | |
7127 | /* pointer to stack or null */ | |
7128 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
7129 | ||
7130 | /* unused */ | |
7131 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7132 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
7133 | callee->in_callback_fn = true; | |
1bfe26fb | 7134 | callee->callback_ret_range = tnum_range(0, 1); |
7c7e3d31 SL |
7135 | return 0; |
7136 | } | |
7137 | ||
20571567 DV |
7138 | static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env, |
7139 | struct bpf_func_state *caller, | |
7140 | struct bpf_func_state *callee, | |
7141 | int insn_idx) | |
7142 | { | |
7143 | /* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void | |
7144 | * callback_ctx, u64 flags); | |
7145 | * callback_fn(struct bpf_dynptr_t* dynptr, void *callback_ctx); | |
7146 | */ | |
7147 | __mark_reg_not_init(env, &callee->regs[BPF_REG_0]); | |
7148 | callee->regs[BPF_REG_1].type = PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL; | |
7149 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
7150 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
7151 | ||
7152 | /* unused */ | |
7153 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
7154 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
7155 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
7156 | ||
7157 | callee->in_callback_fn = true; | |
c92a7a52 | 7158 | callee->callback_ret_range = tnum_range(0, 1); |
20571567 DV |
7159 | return 0; |
7160 | } | |
7161 | ||
f4d7e40a AS |
7162 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
7163 | { | |
7164 | struct bpf_verifier_state *state = env->cur_state; | |
7165 | struct bpf_func_state *caller, *callee; | |
7166 | struct bpf_reg_state *r0; | |
fd978bf7 | 7167 | int err; |
f4d7e40a AS |
7168 | |
7169 | callee = state->frame[state->curframe]; | |
7170 | r0 = &callee->regs[BPF_REG_0]; | |
7171 | if (r0->type == PTR_TO_STACK) { | |
7172 | /* technically it's ok to return caller's stack pointer | |
7173 | * (or caller's caller's pointer) back to the caller, | |
7174 | * since these pointers are valid. Only current stack | |
7175 | * pointer will be invalid as soon as function exits, | |
7176 | * but let's be conservative | |
7177 | */ | |
7178 | verbose(env, "cannot return stack pointer to the caller\n"); | |
7179 | return -EINVAL; | |
7180 | } | |
7181 | ||
7182 | state->curframe--; | |
7183 | caller = state->frame[state->curframe]; | |
69c087ba YS |
7184 | if (callee->in_callback_fn) { |
7185 | /* enforce R0 return value range [0, 1]. */ | |
1bfe26fb | 7186 | struct tnum range = callee->callback_ret_range; |
69c087ba YS |
7187 | |
7188 | if (r0->type != SCALAR_VALUE) { | |
7189 | verbose(env, "R0 not a scalar value\n"); | |
7190 | return -EACCES; | |
7191 | } | |
7192 | if (!tnum_in(range, r0->var_off)) { | |
7193 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
7194 | return -EINVAL; | |
7195 | } | |
7196 | } else { | |
7197 | /* return to the caller whatever r0 had in the callee */ | |
7198 | caller->regs[BPF_REG_0] = *r0; | |
7199 | } | |
f4d7e40a | 7200 | |
9d9d00ac KKD |
7201 | /* callback_fn frame should have released its own additions to parent's |
7202 | * reference state at this point, or check_reference_leak would | |
7203 | * complain, hence it must be the same as the caller. There is no need | |
7204 | * to copy it back. | |
7205 | */ | |
7206 | if (!callee->in_callback_fn) { | |
7207 | /* Transfer references to the caller */ | |
7208 | err = copy_reference_state(caller, callee); | |
7209 | if (err) | |
7210 | return err; | |
7211 | } | |
fd978bf7 | 7212 | |
f4d7e40a | 7213 | *insn_idx = callee->callsite + 1; |
06ee7115 | 7214 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 7215 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 7216 | print_verifier_state(env, callee, true); |
f4d7e40a | 7217 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 7218 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
7219 | } |
7220 | /* clear everything in the callee */ | |
7221 | free_func_state(callee); | |
7222 | state->frame[state->curframe + 1] = NULL; | |
7223 | return 0; | |
7224 | } | |
7225 | ||
849fa506 YS |
7226 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
7227 | int func_id, | |
7228 | struct bpf_call_arg_meta *meta) | |
7229 | { | |
7230 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
7231 | ||
7232 | if (ret_type != RET_INTEGER || | |
7233 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 7234 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
7235 | func_id != BPF_FUNC_probe_read_str && |
7236 | func_id != BPF_FUNC_probe_read_kernel_str && | |
7237 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
7238 | return; |
7239 | ||
10060503 | 7240 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 7241 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
7242 | ret_reg->smin_value = -MAX_ERRNO; |
7243 | ret_reg->s32_min_value = -MAX_ERRNO; | |
3844d153 | 7244 | reg_bounds_sync(ret_reg); |
849fa506 YS |
7245 | } |
7246 | ||
c93552c4 DB |
7247 | static int |
7248 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
7249 | int func_id, int insn_idx) | |
7250 | { | |
7251 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 7252 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
7253 | |
7254 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
7255 | func_id != BPF_FUNC_map_lookup_elem && |
7256 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
7257 | func_id != BPF_FUNC_map_delete_elem && |
7258 | func_id != BPF_FUNC_map_push_elem && | |
7259 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 7260 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f | 7261 | func_id != BPF_FUNC_for_each_map_elem && |
07343110 FZ |
7262 | func_id != BPF_FUNC_redirect_map && |
7263 | func_id != BPF_FUNC_map_lookup_percpu_elem) | |
c93552c4 | 7264 | return 0; |
09772d92 | 7265 | |
591fe988 | 7266 | if (map == NULL) { |
c93552c4 DB |
7267 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
7268 | return -EINVAL; | |
7269 | } | |
7270 | ||
591fe988 DB |
7271 | /* In case of read-only, some additional restrictions |
7272 | * need to be applied in order to prevent altering the | |
7273 | * state of the map from program side. | |
7274 | */ | |
7275 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
7276 | (func_id == BPF_FUNC_map_delete_elem || | |
7277 | func_id == BPF_FUNC_map_update_elem || | |
7278 | func_id == BPF_FUNC_map_push_elem || | |
7279 | func_id == BPF_FUNC_map_pop_elem)) { | |
7280 | verbose(env, "write into map forbidden\n"); | |
7281 | return -EACCES; | |
7282 | } | |
7283 | ||
d2e4c1e6 | 7284 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 7285 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 7286 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 7287 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 7288 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 7289 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
7290 | return 0; |
7291 | } | |
7292 | ||
d2e4c1e6 DB |
7293 | static int |
7294 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
7295 | int func_id, int insn_idx) | |
7296 | { | |
7297 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
7298 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
7299 | struct bpf_map *map = meta->map_ptr; | |
a657182a | 7300 | u64 val, max; |
cc52d914 | 7301 | int err; |
d2e4c1e6 DB |
7302 | |
7303 | if (func_id != BPF_FUNC_tail_call) | |
7304 | return 0; | |
7305 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
7306 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
7307 | return -EINVAL; | |
7308 | } | |
7309 | ||
d2e4c1e6 | 7310 | reg = ®s[BPF_REG_3]; |
a657182a DB |
7311 | val = reg->var_off.value; |
7312 | max = map->max_entries; | |
d2e4c1e6 | 7313 | |
a657182a | 7314 | if (!(register_is_const(reg) && val < max)) { |
d2e4c1e6 DB |
7315 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
7316 | return 0; | |
7317 | } | |
7318 | ||
cc52d914 DB |
7319 | err = mark_chain_precision(env, BPF_REG_3); |
7320 | if (err) | |
7321 | return err; | |
d2e4c1e6 DB |
7322 | if (bpf_map_key_unseen(aux)) |
7323 | bpf_map_key_store(aux, val); | |
7324 | else if (!bpf_map_key_poisoned(aux) && | |
7325 | bpf_map_key_immediate(aux) != val) | |
7326 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
7327 | return 0; | |
7328 | } | |
7329 | ||
fd978bf7 JS |
7330 | static int check_reference_leak(struct bpf_verifier_env *env) |
7331 | { | |
7332 | struct bpf_func_state *state = cur_func(env); | |
9d9d00ac | 7333 | bool refs_lingering = false; |
fd978bf7 JS |
7334 | int i; |
7335 | ||
9d9d00ac KKD |
7336 | if (state->frameno && !state->in_callback_fn) |
7337 | return 0; | |
7338 | ||
fd978bf7 | 7339 | for (i = 0; i < state->acquired_refs; i++) { |
9d9d00ac KKD |
7340 | if (state->in_callback_fn && state->refs[i].callback_ref != state->frameno) |
7341 | continue; | |
fd978bf7 JS |
7342 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
7343 | state->refs[i].id, state->refs[i].insn_idx); | |
9d9d00ac | 7344 | refs_lingering = true; |
fd978bf7 | 7345 | } |
9d9d00ac | 7346 | return refs_lingering ? -EINVAL : 0; |
fd978bf7 JS |
7347 | } |
7348 | ||
7b15523a FR |
7349 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
7350 | struct bpf_reg_state *regs) | |
7351 | { | |
7352 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
7353 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
7354 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
7355 | int err, fmt_map_off, num_args; | |
7356 | u64 fmt_addr; | |
7357 | char *fmt; | |
7358 | ||
7359 | /* data must be an array of u64 */ | |
7360 | if (data_len_reg->var_off.value % 8) | |
7361 | return -EINVAL; | |
7362 | num_args = data_len_reg->var_off.value / 8; | |
7363 | ||
7364 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
7365 | * and map_direct_value_addr is set. | |
7366 | */ | |
7367 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
7368 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
7369 | fmt_map_off); | |
8e8ee109 FR |
7370 | if (err) { |
7371 | verbose(env, "verifier bug\n"); | |
7372 | return -EFAULT; | |
7373 | } | |
7b15523a FR |
7374 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
7375 | ||
7376 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
7377 | * can focus on validating the format specifiers. | |
7378 | */ | |
48cac3f4 | 7379 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
7b15523a FR |
7380 | if (err < 0) |
7381 | verbose(env, "Invalid format string\n"); | |
7382 | ||
7383 | return err; | |
7384 | } | |
7385 | ||
9b99edca JO |
7386 | static int check_get_func_ip(struct bpf_verifier_env *env) |
7387 | { | |
9b99edca JO |
7388 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
7389 | int func_id = BPF_FUNC_get_func_ip; | |
7390 | ||
7391 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 7392 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
7393 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
7394 | func_id_name(func_id), func_id); | |
7395 | return -ENOTSUPP; | |
7396 | } | |
7397 | return 0; | |
9ffd9f3f JO |
7398 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
7399 | return 0; | |
9b99edca JO |
7400 | } |
7401 | ||
7402 | verbose(env, "func %s#%d not supported for program type %d\n", | |
7403 | func_id_name(func_id), func_id, type); | |
7404 | return -ENOTSUPP; | |
7405 | } | |
7406 | ||
1ade2371 EZ |
7407 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
7408 | { | |
7409 | return &env->insn_aux_data[env->insn_idx]; | |
7410 | } | |
7411 | ||
7412 | static bool loop_flag_is_zero(struct bpf_verifier_env *env) | |
7413 | { | |
7414 | struct bpf_reg_state *regs = cur_regs(env); | |
7415 | struct bpf_reg_state *reg = ®s[BPF_REG_4]; | |
7416 | bool reg_is_null = register_is_null(reg); | |
7417 | ||
7418 | if (reg_is_null) | |
7419 | mark_chain_precision(env, BPF_REG_4); | |
7420 | ||
7421 | return reg_is_null; | |
7422 | } | |
7423 | ||
7424 | static void update_loop_inline_state(struct bpf_verifier_env *env, u32 subprogno) | |
7425 | { | |
7426 | struct bpf_loop_inline_state *state = &cur_aux(env)->loop_inline_state; | |
7427 | ||
7428 | if (!state->initialized) { | |
7429 | state->initialized = 1; | |
7430 | state->fit_for_inline = loop_flag_is_zero(env); | |
7431 | state->callback_subprogno = subprogno; | |
7432 | return; | |
7433 | } | |
7434 | ||
7435 | if (!state->fit_for_inline) | |
7436 | return; | |
7437 | ||
7438 | state->fit_for_inline = (loop_flag_is_zero(env) && | |
7439 | state->callback_subprogno == subprogno); | |
7440 | } | |
7441 | ||
69c087ba YS |
7442 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
7443 | int *insn_idx_p) | |
17a52670 | 7444 | { |
aef9d4a3 | 7445 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
17a52670 | 7446 | const struct bpf_func_proto *fn = NULL; |
3c480732 | 7447 | enum bpf_return_type ret_type; |
c25b2ae1 | 7448 | enum bpf_type_flag ret_flag; |
638f5b90 | 7449 | struct bpf_reg_state *regs; |
33ff9823 | 7450 | struct bpf_call_arg_meta meta; |
69c087ba | 7451 | int insn_idx = *insn_idx_p; |
969bf05e | 7452 | bool changes_data; |
69c087ba | 7453 | int i, err, func_id; |
17a52670 AS |
7454 | |
7455 | /* find function prototype */ | |
69c087ba | 7456 | func_id = insn->imm; |
17a52670 | 7457 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
7458 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
7459 | func_id); | |
17a52670 AS |
7460 | return -EINVAL; |
7461 | } | |
7462 | ||
00176a34 | 7463 | if (env->ops->get_func_proto) |
5e43f899 | 7464 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 7465 | if (!fn) { |
61bd5218 JK |
7466 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
7467 | func_id); | |
17a52670 AS |
7468 | return -EINVAL; |
7469 | } | |
7470 | ||
7471 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 7472 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 7473 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
7474 | return -EINVAL; |
7475 | } | |
7476 | ||
eae2e83e JO |
7477 | if (fn->allowed && !fn->allowed(env->prog)) { |
7478 | verbose(env, "helper call is not allowed in probe\n"); | |
7479 | return -EINVAL; | |
7480 | } | |
7481 | ||
04514d13 | 7482 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 7483 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
7484 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
7485 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
7486 | func_id_name(func_id), func_id); | |
7487 | return -EINVAL; | |
7488 | } | |
969bf05e | 7489 | |
33ff9823 | 7490 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 7491 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 7492 | |
0c9a7a7e | 7493 | err = check_func_proto(fn, func_id); |
435faee1 | 7494 | if (err) { |
61bd5218 | 7495 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 7496 | func_id_name(func_id), func_id); |
435faee1 DB |
7497 | return err; |
7498 | } | |
7499 | ||
d83525ca | 7500 | meta.func_id = func_id; |
17a52670 | 7501 | /* check args */ |
523a4cf4 | 7502 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
af7ec138 | 7503 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
7504 | if (err) |
7505 | return err; | |
7506 | } | |
17a52670 | 7507 | |
c93552c4 DB |
7508 | err = record_func_map(env, &meta, func_id, insn_idx); |
7509 | if (err) | |
7510 | return err; | |
7511 | ||
d2e4c1e6 DB |
7512 | err = record_func_key(env, &meta, func_id, insn_idx); |
7513 | if (err) | |
7514 | return err; | |
7515 | ||
435faee1 DB |
7516 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
7517 | * is inferred from register state. | |
7518 | */ | |
7519 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
7520 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
7521 | BPF_WRITE, -1, false); | |
435faee1 DB |
7522 | if (err) |
7523 | return err; | |
7524 | } | |
7525 | ||
8f14852e KKD |
7526 | regs = cur_regs(env); |
7527 | ||
97e03f52 JK |
7528 | if (meta.uninit_dynptr_regno) { |
7529 | /* we write BPF_DW bits (8 bytes) at a time */ | |
7530 | for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) { | |
7531 | err = check_mem_access(env, insn_idx, meta.uninit_dynptr_regno, | |
7532 | i, BPF_DW, BPF_WRITE, -1, false); | |
7533 | if (err) | |
7534 | return err; | |
7535 | } | |
7536 | ||
7537 | err = mark_stack_slots_dynptr(env, ®s[meta.uninit_dynptr_regno], | |
7538 | fn->arg_type[meta.uninit_dynptr_regno - BPF_REG_1], | |
7539 | insn_idx); | |
7540 | if (err) | |
7541 | return err; | |
7542 | } | |
7543 | ||
8f14852e KKD |
7544 | if (meta.release_regno) { |
7545 | err = -EINVAL; | |
97e03f52 JK |
7546 | if (arg_type_is_dynptr(fn->arg_type[meta.release_regno - BPF_REG_1])) |
7547 | err = unmark_stack_slots_dynptr(env, ®s[meta.release_regno]); | |
7548 | else if (meta.ref_obj_id) | |
8f14852e KKD |
7549 | err = release_reference(env, meta.ref_obj_id); |
7550 | /* meta.ref_obj_id can only be 0 if register that is meant to be | |
7551 | * released is NULL, which must be > R0. | |
7552 | */ | |
7553 | else if (register_is_null(®s[meta.release_regno])) | |
7554 | err = 0; | |
46f8bc92 MKL |
7555 | if (err) { |
7556 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
7557 | func_id_name(func_id), func_id); | |
fd978bf7 | 7558 | return err; |
46f8bc92 | 7559 | } |
fd978bf7 JS |
7560 | } |
7561 | ||
e6f2dd0f JK |
7562 | switch (func_id) { |
7563 | case BPF_FUNC_tail_call: | |
7564 | err = check_reference_leak(env); | |
7565 | if (err) { | |
7566 | verbose(env, "tail_call would lead to reference leak\n"); | |
7567 | return err; | |
7568 | } | |
7569 | break; | |
7570 | case BPF_FUNC_get_local_storage: | |
7571 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
7572 | * this is required because get_local_storage() can't return an error. | |
7573 | */ | |
7574 | if (!register_is_null(®s[BPF_REG_2])) { | |
7575 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
7576 | return -EINVAL; | |
7577 | } | |
7578 | break; | |
7579 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
7580 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7581 | set_map_elem_callback_state); | |
e6f2dd0f JK |
7582 | break; |
7583 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
7584 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7585 | set_timer_callback_state); | |
e6f2dd0f JK |
7586 | break; |
7587 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
7588 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7589 | set_find_vma_callback_state); | |
e6f2dd0f JK |
7590 | break; |
7591 | case BPF_FUNC_snprintf: | |
7b15523a | 7592 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
7593 | break; |
7594 | case BPF_FUNC_loop: | |
1ade2371 | 7595 | update_loop_inline_state(env, meta.subprogno); |
e6f2dd0f JK |
7596 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
7597 | set_loop_callback_state); | |
7598 | break; | |
263ae152 JK |
7599 | case BPF_FUNC_dynptr_from_mem: |
7600 | if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) { | |
7601 | verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n", | |
7602 | reg_type_str(env, regs[BPF_REG_1].type)); | |
7603 | return -EACCES; | |
7604 | } | |
69fd337a SF |
7605 | break; |
7606 | case BPF_FUNC_set_retval: | |
aef9d4a3 SF |
7607 | if (prog_type == BPF_PROG_TYPE_LSM && |
7608 | env->prog->expected_attach_type == BPF_LSM_CGROUP) { | |
69fd337a SF |
7609 | if (!env->prog->aux->attach_func_proto->type) { |
7610 | /* Make sure programs that attach to void | |
7611 | * hooks don't try to modify return value. | |
7612 | */ | |
7613 | verbose(env, "BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
7614 | return -EINVAL; | |
7615 | } | |
7616 | } | |
7617 | break; | |
88374342 JK |
7618 | case BPF_FUNC_dynptr_data: |
7619 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { | |
7620 | if (arg_type_is_dynptr(fn->arg_type[i])) { | |
20571567 DV |
7621 | struct bpf_reg_state *reg = ®s[BPF_REG_1 + i]; |
7622 | ||
88374342 JK |
7623 | if (meta.ref_obj_id) { |
7624 | verbose(env, "verifier internal error: meta.ref_obj_id already set\n"); | |
7625 | return -EFAULT; | |
7626 | } | |
20571567 DV |
7627 | |
7628 | if (base_type(reg->type) != PTR_TO_DYNPTR) | |
7629 | /* Find the id of the dynptr we're | |
7630 | * tracking the reference of | |
7631 | */ | |
7632 | meta.ref_obj_id = stack_slot_get_id(env, reg); | |
88374342 JK |
7633 | break; |
7634 | } | |
7635 | } | |
7636 | if (i == MAX_BPF_FUNC_REG_ARGS) { | |
7637 | verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n"); | |
7638 | return -EFAULT; | |
7639 | } | |
7640 | break; | |
20571567 DV |
7641 | case BPF_FUNC_user_ringbuf_drain: |
7642 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
7643 | set_user_ringbuf_callback_state); | |
7644 | break; | |
7b15523a FR |
7645 | } |
7646 | ||
e6f2dd0f JK |
7647 | if (err) |
7648 | return err; | |
7649 | ||
17a52670 | 7650 | /* reset caller saved regs */ |
dc503a8a | 7651 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 7652 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
7653 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
7654 | } | |
17a52670 | 7655 | |
5327ed3d JW |
7656 | /* helper call returns 64-bit value. */ |
7657 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
7658 | ||
dc503a8a | 7659 | /* update return register (already marked as written above) */ |
3c480732 | 7660 | ret_type = fn->ret_type; |
0c9a7a7e JK |
7661 | ret_flag = type_flag(ret_type); |
7662 | ||
7663 | switch (base_type(ret_type)) { | |
7664 | case RET_INTEGER: | |
f1174f77 | 7665 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 7666 | mark_reg_unknown(env, regs, BPF_REG_0); |
0c9a7a7e JK |
7667 | break; |
7668 | case RET_VOID: | |
17a52670 | 7669 | regs[BPF_REG_0].type = NOT_INIT; |
0c9a7a7e JK |
7670 | break; |
7671 | case RET_PTR_TO_MAP_VALUE: | |
f1174f77 | 7672 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 7673 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
7674 | /* remember map_ptr, so that check_map_access() |
7675 | * can check 'value_size' boundary of memory access | |
7676 | * to map element returned from bpf_map_lookup_elem() | |
7677 | */ | |
33ff9823 | 7678 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
7679 | verbose(env, |
7680 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
7681 | return -EINVAL; |
7682 | } | |
33ff9823 | 7683 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 7684 | regs[BPF_REG_0].map_uid = meta.map_uid; |
c25b2ae1 HL |
7685 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
7686 | if (!type_may_be_null(ret_type) && | |
db559117 | 7687 | btf_record_has_field(meta.map_ptr->record, BPF_SPIN_LOCK)) { |
c25b2ae1 | 7688 | regs[BPF_REG_0].id = ++env->id_gen; |
4d31f301 | 7689 | } |
0c9a7a7e JK |
7690 | break; |
7691 | case RET_PTR_TO_SOCKET: | |
c64b7983 | 7692 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7693 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
0c9a7a7e JK |
7694 | break; |
7695 | case RET_PTR_TO_SOCK_COMMON: | |
85a51f8c | 7696 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7697 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
0c9a7a7e JK |
7698 | break; |
7699 | case RET_PTR_TO_TCP_SOCK: | |
655a51e5 | 7700 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7701 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
0c9a7a7e | 7702 | break; |
2de2669b | 7703 | case RET_PTR_TO_MEM: |
457f4436 | 7704 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 7705 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
457f4436 | 7706 | regs[BPF_REG_0].mem_size = meta.mem_size; |
0c9a7a7e JK |
7707 | break; |
7708 | case RET_PTR_TO_MEM_OR_BTF_ID: | |
7709 | { | |
eaa6bcb7 HL |
7710 | const struct btf_type *t; |
7711 | ||
7712 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 7713 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
7714 | if (!btf_type_is_struct(t)) { |
7715 | u32 tsize; | |
7716 | const struct btf_type *ret; | |
7717 | const char *tname; | |
7718 | ||
7719 | /* resolve the type size of ksym. */ | |
22dc4a0f | 7720 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 7721 | if (IS_ERR(ret)) { |
22dc4a0f | 7722 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
7723 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
7724 | tname, PTR_ERR(ret)); | |
7725 | return -EINVAL; | |
7726 | } | |
c25b2ae1 | 7727 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
eaa6bcb7 HL |
7728 | regs[BPF_REG_0].mem_size = tsize; |
7729 | } else { | |
34d3a78c HL |
7730 | /* MEM_RDONLY may be carried from ret_flag, but it |
7731 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise | |
7732 | * it will confuse the check of PTR_TO_BTF_ID in | |
7733 | * check_mem_access(). | |
7734 | */ | |
7735 | ret_flag &= ~MEM_RDONLY; | |
7736 | ||
c25b2ae1 | 7737 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
22dc4a0f | 7738 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
7739 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
7740 | } | |
0c9a7a7e JK |
7741 | break; |
7742 | } | |
7743 | case RET_PTR_TO_BTF_ID: | |
7744 | { | |
c0a5a21c | 7745 | struct btf *ret_btf; |
af7ec138 YS |
7746 | int ret_btf_id; |
7747 | ||
7748 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
c25b2ae1 | 7749 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
c0a5a21c | 7750 | if (func_id == BPF_FUNC_kptr_xchg) { |
aa3496ac KKD |
7751 | ret_btf = meta.kptr_field->kptr.btf; |
7752 | ret_btf_id = meta.kptr_field->kptr.btf_id; | |
c0a5a21c | 7753 | } else { |
47e34cb7 DM |
7754 | if (fn->ret_btf_id == BPF_PTR_POISON) { |
7755 | verbose(env, "verifier internal error:"); | |
7756 | verbose(env, "func %s has non-overwritten BPF_PTR_POISON return type\n", | |
7757 | func_id_name(func_id)); | |
7758 | return -EINVAL; | |
7759 | } | |
c0a5a21c KKD |
7760 | ret_btf = btf_vmlinux; |
7761 | ret_btf_id = *fn->ret_btf_id; | |
7762 | } | |
af7ec138 | 7763 | if (ret_btf_id == 0) { |
3c480732 HL |
7764 | verbose(env, "invalid return type %u of func %s#%d\n", |
7765 | base_type(ret_type), func_id_name(func_id), | |
7766 | func_id); | |
af7ec138 YS |
7767 | return -EINVAL; |
7768 | } | |
c0a5a21c | 7769 | regs[BPF_REG_0].btf = ret_btf; |
af7ec138 | 7770 | regs[BPF_REG_0].btf_id = ret_btf_id; |
0c9a7a7e JK |
7771 | break; |
7772 | } | |
7773 | default: | |
3c480732 HL |
7774 | verbose(env, "unknown return type %u of func %s#%d\n", |
7775 | base_type(ret_type), func_id_name(func_id), func_id); | |
17a52670 AS |
7776 | return -EINVAL; |
7777 | } | |
04fd61ab | 7778 | |
c25b2ae1 | 7779 | if (type_may_be_null(regs[BPF_REG_0].type)) |
93c230e3 MKL |
7780 | regs[BPF_REG_0].id = ++env->id_gen; |
7781 | ||
b2d8ef19 DM |
7782 | if (helper_multiple_ref_obj_use(func_id, meta.map_ptr)) { |
7783 | verbose(env, "verifier internal error: func %s#%d sets ref_obj_id more than once\n", | |
7784 | func_id_name(func_id), func_id); | |
7785 | return -EFAULT; | |
7786 | } | |
7787 | ||
88374342 | 7788 | if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) { |
1b986589 MKL |
7789 | /* For release_reference() */ |
7790 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 7791 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
7792 | int id = acquire_reference_state(env, insn_idx); |
7793 | ||
7794 | if (id < 0) | |
7795 | return id; | |
7796 | /* For mark_ptr_or_null_reg() */ | |
7797 | regs[BPF_REG_0].id = id; | |
7798 | /* For release_reference() */ | |
7799 | regs[BPF_REG_0].ref_obj_id = id; | |
7800 | } | |
1b986589 | 7801 | |
849fa506 YS |
7802 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
7803 | ||
61bd5218 | 7804 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
7805 | if (err) |
7806 | return err; | |
04fd61ab | 7807 | |
fa28dcb8 SL |
7808 | if ((func_id == BPF_FUNC_get_stack || |
7809 | func_id == BPF_FUNC_get_task_stack) && | |
7810 | !env->prog->has_callchain_buf) { | |
c195651e YS |
7811 | const char *err_str; |
7812 | ||
7813 | #ifdef CONFIG_PERF_EVENTS | |
7814 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
7815 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
7816 | #else | |
7817 | err = -ENOTSUPP; | |
7818 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
7819 | #endif | |
7820 | if (err) { | |
7821 | verbose(env, err_str, func_id_name(func_id), func_id); | |
7822 | return err; | |
7823 | } | |
7824 | ||
7825 | env->prog->has_callchain_buf = true; | |
7826 | } | |
7827 | ||
5d99cb2c SL |
7828 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
7829 | env->prog->call_get_stack = true; | |
7830 | ||
9b99edca JO |
7831 | if (func_id == BPF_FUNC_get_func_ip) { |
7832 | if (check_get_func_ip(env)) | |
7833 | return -ENOTSUPP; | |
7834 | env->prog->call_get_func_ip = true; | |
7835 | } | |
7836 | ||
969bf05e AS |
7837 | if (changes_data) |
7838 | clear_all_pkt_pointers(env); | |
7839 | return 0; | |
7840 | } | |
7841 | ||
e6ac2450 MKL |
7842 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
7843 | * the BTF func_proto's return value size and argument. | |
7844 | */ | |
7845 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
7846 | size_t reg_size) | |
7847 | { | |
7848 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
7849 | ||
7850 | if (regno == BPF_REG_0) { | |
7851 | /* Function return value */ | |
7852 | reg->live |= REG_LIVE_WRITTEN; | |
7853 | reg->subreg_def = reg_size == sizeof(u64) ? | |
7854 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
7855 | } else { | |
7856 | /* Function argument */ | |
7857 | if (reg_size == sizeof(u64)) { | |
7858 | mark_insn_zext(env, reg); | |
7859 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
7860 | } else { | |
7861 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
7862 | } | |
7863 | } | |
7864 | } | |
7865 | ||
00b85860 KKD |
7866 | struct bpf_kfunc_call_arg_meta { |
7867 | /* In parameters */ | |
7868 | struct btf *btf; | |
7869 | u32 func_id; | |
7870 | u32 kfunc_flags; | |
7871 | const struct btf_type *func_proto; | |
7872 | const char *func_name; | |
7873 | /* Out parameters */ | |
7874 | u32 ref_obj_id; | |
7875 | u8 release_regno; | |
7876 | bool r0_rdonly; | |
7877 | u64 r0_size; | |
a50388db KKD |
7878 | struct { |
7879 | u64 value; | |
7880 | bool found; | |
7881 | } arg_constant; | |
ac9f0605 KKD |
7882 | struct { |
7883 | struct btf *btf; | |
7884 | u32 btf_id; | |
7885 | } arg_obj_drop; | |
00b85860 KKD |
7886 | }; |
7887 | ||
7888 | static bool is_kfunc_acquire(struct bpf_kfunc_call_arg_meta *meta) | |
7889 | { | |
7890 | return meta->kfunc_flags & KF_ACQUIRE; | |
7891 | } | |
7892 | ||
7893 | static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) | |
7894 | { | |
7895 | return meta->kfunc_flags & KF_RET_NULL; | |
7896 | } | |
7897 | ||
7898 | static bool is_kfunc_release(struct bpf_kfunc_call_arg_meta *meta) | |
7899 | { | |
7900 | return meta->kfunc_flags & KF_RELEASE; | |
7901 | } | |
7902 | ||
7903 | static bool is_kfunc_trusted_args(struct bpf_kfunc_call_arg_meta *meta) | |
7904 | { | |
7905 | return meta->kfunc_flags & KF_TRUSTED_ARGS; | |
7906 | } | |
7907 | ||
7908 | static bool is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta) | |
7909 | { | |
7910 | return meta->kfunc_flags & KF_SLEEPABLE; | |
7911 | } | |
7912 | ||
7913 | static bool is_kfunc_destructive(struct bpf_kfunc_call_arg_meta *meta) | |
7914 | { | |
7915 | return meta->kfunc_flags & KF_DESTRUCTIVE; | |
7916 | } | |
7917 | ||
7918 | static bool is_kfunc_arg_kptr_get(struct bpf_kfunc_call_arg_meta *meta, int arg) | |
7919 | { | |
7920 | return arg == 0 && (meta->kfunc_flags & KF_KPTR_GET); | |
7921 | } | |
7922 | ||
a50388db KKD |
7923 | static bool __kfunc_param_match_suffix(const struct btf *btf, |
7924 | const struct btf_param *arg, | |
7925 | const char *suffix) | |
00b85860 | 7926 | { |
a50388db | 7927 | int suffix_len = strlen(suffix), len; |
00b85860 KKD |
7928 | const char *param_name; |
7929 | ||
00b85860 KKD |
7930 | /* In the future, this can be ported to use BTF tagging */ |
7931 | param_name = btf_name_by_offset(btf, arg->name_off); | |
7932 | if (str_is_empty(param_name)) | |
7933 | return false; | |
7934 | len = strlen(param_name); | |
a50388db | 7935 | if (len < suffix_len) |
00b85860 | 7936 | return false; |
a50388db KKD |
7937 | param_name += len - suffix_len; |
7938 | return !strncmp(param_name, suffix, suffix_len); | |
7939 | } | |
7940 | ||
7941 | static bool is_kfunc_arg_mem_size(const struct btf *btf, | |
7942 | const struct btf_param *arg, | |
7943 | const struct bpf_reg_state *reg) | |
7944 | { | |
7945 | const struct btf_type *t; | |
7946 | ||
7947 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
7948 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
00b85860 KKD |
7949 | return false; |
7950 | ||
a50388db KKD |
7951 | return __kfunc_param_match_suffix(btf, arg, "__sz"); |
7952 | } | |
7953 | ||
7954 | static bool is_kfunc_arg_constant(const struct btf *btf, const struct btf_param *arg) | |
7955 | { | |
7956 | return __kfunc_param_match_suffix(btf, arg, "__k"); | |
00b85860 KKD |
7957 | } |
7958 | ||
958cf2e2 KKD |
7959 | static bool is_kfunc_arg_ignore(const struct btf *btf, const struct btf_param *arg) |
7960 | { | |
7961 | return __kfunc_param_match_suffix(btf, arg, "__ign"); | |
7962 | } | |
7963 | ||
ac9f0605 KKD |
7964 | static bool is_kfunc_arg_alloc_obj(const struct btf *btf, const struct btf_param *arg) |
7965 | { | |
7966 | return __kfunc_param_match_suffix(btf, arg, "__alloc"); | |
7967 | } | |
7968 | ||
00b85860 KKD |
7969 | static bool is_kfunc_arg_scalar_with_name(const struct btf *btf, |
7970 | const struct btf_param *arg, | |
7971 | const char *name) | |
7972 | { | |
7973 | int len, target_len = strlen(name); | |
7974 | const char *param_name; | |
7975 | ||
7976 | param_name = btf_name_by_offset(btf, arg->name_off); | |
7977 | if (str_is_empty(param_name)) | |
7978 | return false; | |
7979 | len = strlen(param_name); | |
7980 | if (len != target_len) | |
7981 | return false; | |
7982 | if (strcmp(param_name, name)) | |
7983 | return false; | |
7984 | ||
7985 | return true; | |
7986 | } | |
7987 | ||
7988 | enum { | |
7989 | KF_ARG_DYNPTR_ID, | |
7990 | }; | |
7991 | ||
7992 | BTF_ID_LIST(kf_arg_btf_ids) | |
7993 | BTF_ID(struct, bpf_dynptr_kern) | |
7994 | ||
7995 | static bool is_kfunc_arg_dynptr(const struct btf *btf, | |
7996 | const struct btf_param *arg) | |
7997 | { | |
7998 | const struct btf_type *t; | |
7999 | u32 res_id; | |
8000 | ||
8001 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
8002 | if (!t) | |
8003 | return false; | |
8004 | if (!btf_type_is_ptr(t)) | |
8005 | return false; | |
8006 | t = btf_type_skip_modifiers(btf, t->type, &res_id); | |
8007 | if (!t) | |
8008 | return false; | |
8009 | return btf_types_are_same(btf, res_id, btf_vmlinux, kf_arg_btf_ids[KF_ARG_DYNPTR_ID]); | |
8010 | } | |
8011 | ||
8012 | /* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ | |
8013 | static bool __btf_type_is_scalar_struct(struct bpf_verifier_env *env, | |
8014 | const struct btf *btf, | |
8015 | const struct btf_type *t, int rec) | |
8016 | { | |
8017 | const struct btf_type *member_type; | |
8018 | const struct btf_member *member; | |
8019 | u32 i; | |
8020 | ||
8021 | if (!btf_type_is_struct(t)) | |
8022 | return false; | |
8023 | ||
8024 | for_each_member(i, t, member) { | |
8025 | const struct btf_array *array; | |
8026 | ||
8027 | member_type = btf_type_skip_modifiers(btf, member->type, NULL); | |
8028 | if (btf_type_is_struct(member_type)) { | |
8029 | if (rec >= 3) { | |
8030 | verbose(env, "max struct nesting depth exceeded\n"); | |
8031 | return false; | |
8032 | } | |
8033 | if (!__btf_type_is_scalar_struct(env, btf, member_type, rec + 1)) | |
8034 | return false; | |
8035 | continue; | |
8036 | } | |
8037 | if (btf_type_is_array(member_type)) { | |
8038 | array = btf_array(member_type); | |
8039 | if (!array->nelems) | |
8040 | return false; | |
8041 | member_type = btf_type_skip_modifiers(btf, array->type, NULL); | |
8042 | if (!btf_type_is_scalar(member_type)) | |
8043 | return false; | |
8044 | continue; | |
8045 | } | |
8046 | if (!btf_type_is_scalar(member_type)) | |
8047 | return false; | |
8048 | } | |
8049 | return true; | |
8050 | } | |
8051 | ||
8052 | ||
8053 | static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { | |
8054 | #ifdef CONFIG_NET | |
8055 | [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], | |
8056 | [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
8057 | [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], | |
8058 | #endif | |
8059 | }; | |
8060 | ||
8061 | enum kfunc_ptr_arg_type { | |
8062 | KF_ARG_PTR_TO_CTX, | |
ac9f0605 | 8063 | KF_ARG_PTR_TO_ALLOC_BTF_ID, /* Allocated object */ |
00b85860 KKD |
8064 | KF_ARG_PTR_TO_KPTR, /* PTR_TO_KPTR but type specific */ |
8065 | KF_ARG_PTR_TO_DYNPTR, | |
8066 | KF_ARG_PTR_TO_BTF_ID, /* Also covers reg2btf_ids conversions */ | |
8067 | KF_ARG_PTR_TO_MEM, | |
8068 | KF_ARG_PTR_TO_MEM_SIZE, /* Size derived from next argument, skip it */ | |
8069 | }; | |
8070 | ||
ac9f0605 KKD |
8071 | enum special_kfunc_type { |
8072 | KF_bpf_obj_new_impl, | |
8073 | KF_bpf_obj_drop_impl, | |
8074 | }; | |
8075 | ||
8076 | BTF_SET_START(special_kfunc_set) | |
8077 | BTF_ID(func, bpf_obj_new_impl) | |
8078 | BTF_ID(func, bpf_obj_drop_impl) | |
8079 | BTF_SET_END(special_kfunc_set) | |
8080 | ||
8081 | BTF_ID_LIST(special_kfunc_list) | |
8082 | BTF_ID(func, bpf_obj_new_impl) | |
8083 | BTF_ID(func, bpf_obj_drop_impl) | |
8084 | ||
00b85860 KKD |
8085 | static enum kfunc_ptr_arg_type |
8086 | get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, | |
8087 | struct bpf_kfunc_call_arg_meta *meta, | |
8088 | const struct btf_type *t, const struct btf_type *ref_t, | |
8089 | const char *ref_tname, const struct btf_param *args, | |
8090 | int argno, int nargs) | |
8091 | { | |
8092 | u32 regno = argno + 1; | |
8093 | struct bpf_reg_state *regs = cur_regs(env); | |
8094 | struct bpf_reg_state *reg = ®s[regno]; | |
8095 | bool arg_mem_size = false; | |
8096 | ||
8097 | /* In this function, we verify the kfunc's BTF as per the argument type, | |
8098 | * leaving the rest of the verification with respect to the register | |
8099 | * type to our caller. When a set of conditions hold in the BTF type of | |
8100 | * arguments, we resolve it to a known kfunc_ptr_arg_type. | |
8101 | */ | |
8102 | if (btf_get_prog_ctx_type(&env->log, meta->btf, t, resolve_prog_type(env->prog), argno)) | |
8103 | return KF_ARG_PTR_TO_CTX; | |
8104 | ||
ac9f0605 KKD |
8105 | if (is_kfunc_arg_alloc_obj(meta->btf, &args[argno])) |
8106 | return KF_ARG_PTR_TO_ALLOC_BTF_ID; | |
8107 | ||
00b85860 KKD |
8108 | if (is_kfunc_arg_kptr_get(meta, argno)) { |
8109 | if (!btf_type_is_ptr(ref_t)) { | |
8110 | verbose(env, "arg#0 BTF type must be a double pointer for kptr_get kfunc\n"); | |
8111 | return -EINVAL; | |
8112 | } | |
8113 | ref_t = btf_type_by_id(meta->btf, ref_t->type); | |
8114 | ref_tname = btf_name_by_offset(meta->btf, ref_t->name_off); | |
8115 | if (!btf_type_is_struct(ref_t)) { | |
8116 | verbose(env, "kernel function %s args#0 pointer type %s %s is not supported\n", | |
8117 | meta->func_name, btf_type_str(ref_t), ref_tname); | |
8118 | return -EINVAL; | |
8119 | } | |
8120 | return KF_ARG_PTR_TO_KPTR; | |
8121 | } | |
8122 | ||
8123 | if (is_kfunc_arg_dynptr(meta->btf, &args[argno])) | |
8124 | return KF_ARG_PTR_TO_DYNPTR; | |
8125 | ||
8126 | if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { | |
8127 | if (!btf_type_is_struct(ref_t)) { | |
8128 | verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n", | |
8129 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
8130 | return -EINVAL; | |
8131 | } | |
8132 | return KF_ARG_PTR_TO_BTF_ID; | |
8133 | } | |
8134 | ||
8135 | if (argno + 1 < nargs && is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1])) | |
8136 | arg_mem_size = true; | |
8137 | ||
8138 | /* This is the catch all argument type of register types supported by | |
8139 | * check_helper_mem_access. However, we only allow when argument type is | |
8140 | * pointer to scalar, or struct composed (recursively) of scalars. When | |
8141 | * arg_mem_size is true, the pointer can be void *. | |
8142 | */ | |
8143 | if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(env, meta->btf, ref_t, 0) && | |
8144 | (arg_mem_size ? !btf_type_is_void(ref_t) : 1)) { | |
8145 | verbose(env, "arg#%d pointer type %s %s must point to %sscalar, or struct with scalar\n", | |
8146 | argno, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : ""); | |
8147 | return -EINVAL; | |
8148 | } | |
8149 | return arg_mem_size ? KF_ARG_PTR_TO_MEM_SIZE : KF_ARG_PTR_TO_MEM; | |
8150 | } | |
8151 | ||
8152 | static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env, | |
8153 | struct bpf_reg_state *reg, | |
8154 | const struct btf_type *ref_t, | |
8155 | const char *ref_tname, u32 ref_id, | |
8156 | struct bpf_kfunc_call_arg_meta *meta, | |
8157 | int argno) | |
8158 | { | |
8159 | const struct btf_type *reg_ref_t; | |
8160 | bool strict_type_match = false; | |
8161 | const struct btf *reg_btf; | |
8162 | const char *reg_ref_tname; | |
8163 | u32 reg_ref_id; | |
8164 | ||
8165 | if (reg->type == PTR_TO_BTF_ID) { | |
8166 | reg_btf = reg->btf; | |
8167 | reg_ref_id = reg->btf_id; | |
8168 | } else { | |
8169 | reg_btf = btf_vmlinux; | |
8170 | reg_ref_id = *reg2btf_ids[base_type(reg->type)]; | |
8171 | } | |
8172 | ||
8173 | if (is_kfunc_trusted_args(meta) || (is_kfunc_release(meta) && reg->ref_obj_id)) | |
8174 | strict_type_match = true; | |
8175 | ||
8176 | reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id, ®_ref_id); | |
8177 | reg_ref_tname = btf_name_by_offset(reg_btf, reg_ref_t->name_off); | |
8178 | if (!btf_struct_ids_match(&env->log, reg_btf, reg_ref_id, reg->off, meta->btf, ref_id, strict_type_match)) { | |
8179 | verbose(env, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", | |
8180 | meta->func_name, argno, btf_type_str(ref_t), ref_tname, argno + 1, | |
8181 | btf_type_str(reg_ref_t), reg_ref_tname); | |
8182 | return -EINVAL; | |
8183 | } | |
8184 | return 0; | |
8185 | } | |
8186 | ||
8187 | static int process_kf_arg_ptr_to_kptr(struct bpf_verifier_env *env, | |
8188 | struct bpf_reg_state *reg, | |
8189 | const struct btf_type *ref_t, | |
8190 | const char *ref_tname, | |
8191 | struct bpf_kfunc_call_arg_meta *meta, | |
8192 | int argno) | |
8193 | { | |
8194 | struct btf_field *kptr_field; | |
8195 | ||
8196 | /* check_func_arg_reg_off allows var_off for | |
8197 | * PTR_TO_MAP_VALUE, but we need fixed offset to find | |
8198 | * off_desc. | |
8199 | */ | |
8200 | if (!tnum_is_const(reg->var_off)) { | |
8201 | verbose(env, "arg#0 must have constant offset\n"); | |
8202 | return -EINVAL; | |
8203 | } | |
8204 | ||
8205 | kptr_field = btf_record_find(reg->map_ptr->record, reg->off + reg->var_off.value, BPF_KPTR); | |
8206 | if (!kptr_field || kptr_field->type != BPF_KPTR_REF) { | |
8207 | verbose(env, "arg#0 no referenced kptr at map value offset=%llu\n", | |
8208 | reg->off + reg->var_off.value); | |
8209 | return -EINVAL; | |
8210 | } | |
8211 | ||
8212 | if (!btf_struct_ids_match(&env->log, meta->btf, ref_t->type, 0, kptr_field->kptr.btf, | |
8213 | kptr_field->kptr.btf_id, true)) { | |
8214 | verbose(env, "kernel function %s args#%d expected pointer to %s %s\n", | |
8215 | meta->func_name, argno, btf_type_str(ref_t), ref_tname); | |
8216 | return -EINVAL; | |
8217 | } | |
8218 | return 0; | |
8219 | } | |
8220 | ||
8221 | static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta) | |
8222 | { | |
8223 | const char *func_name = meta->func_name, *ref_tname; | |
8224 | const struct btf *btf = meta->btf; | |
8225 | const struct btf_param *args; | |
8226 | u32 i, nargs; | |
8227 | int ret; | |
8228 | ||
8229 | args = (const struct btf_param *)(meta->func_proto + 1); | |
8230 | nargs = btf_type_vlen(meta->func_proto); | |
8231 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { | |
8232 | verbose(env, "Function %s has %d > %d args\n", func_name, nargs, | |
8233 | MAX_BPF_FUNC_REG_ARGS); | |
8234 | return -EINVAL; | |
8235 | } | |
8236 | ||
8237 | /* Check that BTF function arguments match actual types that the | |
8238 | * verifier sees. | |
8239 | */ | |
8240 | for (i = 0; i < nargs; i++) { | |
8241 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[i + 1]; | |
8242 | const struct btf_type *t, *ref_t, *resolve_ret; | |
8243 | enum bpf_arg_type arg_type = ARG_DONTCARE; | |
8244 | u32 regno = i + 1, ref_id, type_size; | |
8245 | bool is_ret_buf_sz = false; | |
8246 | int kf_arg_type; | |
8247 | ||
8248 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); | |
958cf2e2 KKD |
8249 | |
8250 | if (is_kfunc_arg_ignore(btf, &args[i])) | |
8251 | continue; | |
8252 | ||
00b85860 KKD |
8253 | if (btf_type_is_scalar(t)) { |
8254 | if (reg->type != SCALAR_VALUE) { | |
8255 | verbose(env, "R%d is not a scalar\n", regno); | |
8256 | return -EINVAL; | |
8257 | } | |
a50388db KKD |
8258 | |
8259 | if (is_kfunc_arg_constant(meta->btf, &args[i])) { | |
8260 | if (meta->arg_constant.found) { | |
8261 | verbose(env, "verifier internal error: only one constant argument permitted\n"); | |
8262 | return -EFAULT; | |
8263 | } | |
8264 | if (!tnum_is_const(reg->var_off)) { | |
8265 | verbose(env, "R%d must be a known constant\n", regno); | |
8266 | return -EINVAL; | |
8267 | } | |
8268 | ret = mark_chain_precision(env, regno); | |
8269 | if (ret < 0) | |
8270 | return ret; | |
8271 | meta->arg_constant.found = true; | |
8272 | meta->arg_constant.value = reg->var_off.value; | |
8273 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) { | |
00b85860 KKD |
8274 | meta->r0_rdonly = true; |
8275 | is_ret_buf_sz = true; | |
8276 | } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) { | |
8277 | is_ret_buf_sz = true; | |
8278 | } | |
8279 | ||
8280 | if (is_ret_buf_sz) { | |
8281 | if (meta->r0_size) { | |
8282 | verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc"); | |
8283 | return -EINVAL; | |
8284 | } | |
8285 | ||
8286 | if (!tnum_is_const(reg->var_off)) { | |
8287 | verbose(env, "R%d is not a const\n", regno); | |
8288 | return -EINVAL; | |
8289 | } | |
8290 | ||
8291 | meta->r0_size = reg->var_off.value; | |
8292 | ret = mark_chain_precision(env, regno); | |
8293 | if (ret) | |
8294 | return ret; | |
8295 | } | |
8296 | continue; | |
8297 | } | |
8298 | ||
8299 | if (!btf_type_is_ptr(t)) { | |
8300 | verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t)); | |
8301 | return -EINVAL; | |
8302 | } | |
8303 | ||
8304 | if (reg->ref_obj_id) { | |
8305 | if (is_kfunc_release(meta) && meta->ref_obj_id) { | |
8306 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
8307 | regno, reg->ref_obj_id, | |
8308 | meta->ref_obj_id); | |
8309 | return -EFAULT; | |
8310 | } | |
8311 | meta->ref_obj_id = reg->ref_obj_id; | |
8312 | if (is_kfunc_release(meta)) | |
8313 | meta->release_regno = regno; | |
8314 | } | |
8315 | ||
8316 | ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); | |
8317 | ref_tname = btf_name_by_offset(btf, ref_t->name_off); | |
8318 | ||
8319 | kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs); | |
8320 | if (kf_arg_type < 0) | |
8321 | return kf_arg_type; | |
8322 | ||
8323 | switch (kf_arg_type) { | |
ac9f0605 | 8324 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
00b85860 KKD |
8325 | case KF_ARG_PTR_TO_BTF_ID: |
8326 | if (!is_kfunc_trusted_args(meta)) | |
8327 | break; | |
8328 | if (!reg->ref_obj_id) { | |
8329 | verbose(env, "R%d must be referenced\n", regno); | |
8330 | return -EINVAL; | |
8331 | } | |
8332 | fallthrough; | |
8333 | case KF_ARG_PTR_TO_CTX: | |
8334 | /* Trusted arguments have the same offset checks as release arguments */ | |
8335 | arg_type |= OBJ_RELEASE; | |
8336 | break; | |
8337 | case KF_ARG_PTR_TO_KPTR: | |
8338 | case KF_ARG_PTR_TO_DYNPTR: | |
8339 | case KF_ARG_PTR_TO_MEM: | |
8340 | case KF_ARG_PTR_TO_MEM_SIZE: | |
8341 | /* Trusted by default */ | |
8342 | break; | |
8343 | default: | |
8344 | WARN_ON_ONCE(1); | |
8345 | return -EFAULT; | |
8346 | } | |
8347 | ||
8348 | if (is_kfunc_release(meta) && reg->ref_obj_id) | |
8349 | arg_type |= OBJ_RELEASE; | |
8350 | ret = check_func_arg_reg_off(env, reg, regno, arg_type); | |
8351 | if (ret < 0) | |
8352 | return ret; | |
8353 | ||
8354 | switch (kf_arg_type) { | |
8355 | case KF_ARG_PTR_TO_CTX: | |
8356 | if (reg->type != PTR_TO_CTX) { | |
8357 | verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t)); | |
8358 | return -EINVAL; | |
8359 | } | |
8360 | break; | |
ac9f0605 KKD |
8361 | case KF_ARG_PTR_TO_ALLOC_BTF_ID: |
8362 | if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) { | |
8363 | verbose(env, "arg#%d expected pointer to allocated object\n", i); | |
8364 | return -EINVAL; | |
8365 | } | |
8366 | if (!reg->ref_obj_id) { | |
8367 | verbose(env, "allocated object must be referenced\n"); | |
8368 | return -EINVAL; | |
8369 | } | |
8370 | if (meta->btf == btf_vmlinux && | |
8371 | meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { | |
8372 | meta->arg_obj_drop.btf = reg->btf; | |
8373 | meta->arg_obj_drop.btf_id = reg->btf_id; | |
8374 | } | |
8375 | break; | |
00b85860 KKD |
8376 | case KF_ARG_PTR_TO_KPTR: |
8377 | if (reg->type != PTR_TO_MAP_VALUE) { | |
8378 | verbose(env, "arg#0 expected pointer to map value\n"); | |
8379 | return -EINVAL; | |
8380 | } | |
8381 | ret = process_kf_arg_ptr_to_kptr(env, reg, ref_t, ref_tname, meta, i); | |
8382 | if (ret < 0) | |
8383 | return ret; | |
8384 | break; | |
8385 | case KF_ARG_PTR_TO_DYNPTR: | |
8386 | if (reg->type != PTR_TO_STACK) { | |
8387 | verbose(env, "arg#%d expected pointer to stack\n", i); | |
8388 | return -EINVAL; | |
8389 | } | |
8390 | ||
8391 | if (!is_dynptr_reg_valid_init(env, reg)) { | |
8392 | verbose(env, "arg#%d pointer type %s %s must be valid and initialized\n", | |
8393 | i, btf_type_str(ref_t), ref_tname); | |
8394 | return -EINVAL; | |
8395 | } | |
8396 | ||
8397 | if (!is_dynptr_type_expected(env, reg, ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL)) { | |
8398 | verbose(env, "arg#%d pointer type %s %s points to unsupported dynamic pointer type\n", | |
8399 | i, btf_type_str(ref_t), ref_tname); | |
8400 | return -EINVAL; | |
8401 | } | |
8402 | break; | |
8403 | case KF_ARG_PTR_TO_BTF_ID: | |
8404 | /* Only base_type is checked, further checks are done here */ | |
8405 | if (reg->type != PTR_TO_BTF_ID && | |
8406 | (!reg2btf_ids[base_type(reg->type)] || type_flag(reg->type))) { | |
8407 | verbose(env, "arg#%d expected pointer to btf or socket\n", i); | |
8408 | return -EINVAL; | |
8409 | } | |
8410 | ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i); | |
8411 | if (ret < 0) | |
8412 | return ret; | |
8413 | break; | |
8414 | case KF_ARG_PTR_TO_MEM: | |
8415 | resolve_ret = btf_resolve_size(btf, ref_t, &type_size); | |
8416 | if (IS_ERR(resolve_ret)) { | |
8417 | verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n", | |
8418 | i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret)); | |
8419 | return -EINVAL; | |
8420 | } | |
8421 | ret = check_mem_reg(env, reg, regno, type_size); | |
8422 | if (ret < 0) | |
8423 | return ret; | |
8424 | break; | |
8425 | case KF_ARG_PTR_TO_MEM_SIZE: | |
8426 | ret = check_kfunc_mem_size_reg(env, ®s[regno + 1], regno + 1); | |
8427 | if (ret < 0) { | |
8428 | verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1); | |
8429 | return ret; | |
8430 | } | |
8431 | /* Skip next '__sz' argument */ | |
8432 | i++; | |
8433 | break; | |
8434 | } | |
8435 | } | |
8436 | ||
8437 | if (is_kfunc_release(meta) && !meta->release_regno) { | |
8438 | verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n", | |
8439 | func_name); | |
8440 | return -EINVAL; | |
8441 | } | |
8442 | ||
8443 | return 0; | |
8444 | } | |
8445 | ||
5c073f26 KKD |
8446 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
8447 | int *insn_idx_p) | |
e6ac2450 MKL |
8448 | { |
8449 | const struct btf_type *t, *func, *func_proto, *ptr_type; | |
8450 | struct bpf_reg_state *regs = cur_regs(env); | |
8451 | const char *func_name, *ptr_type_name; | |
00b85860 | 8452 | struct bpf_kfunc_call_arg_meta meta; |
e6ac2450 | 8453 | u32 i, nargs, func_id, ptr_type_id; |
5c073f26 | 8454 | int err, insn_idx = *insn_idx_p; |
e6ac2450 | 8455 | const struct btf_param *args; |
2357672c | 8456 | struct btf *desc_btf; |
a4703e31 | 8457 | u32 *kfunc_flags; |
e6ac2450 | 8458 | |
a5d82727 KKD |
8459 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
8460 | if (!insn->imm) | |
8461 | return 0; | |
8462 | ||
43bf0878 | 8463 | desc_btf = find_kfunc_desc_btf(env, insn->off); |
2357672c KKD |
8464 | if (IS_ERR(desc_btf)) |
8465 | return PTR_ERR(desc_btf); | |
8466 | ||
e6ac2450 | 8467 | func_id = insn->imm; |
2357672c KKD |
8468 | func = btf_type_by_id(desc_btf, func_id); |
8469 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
8470 | func_proto = btf_type_by_id(desc_btf, func->type); | |
e6ac2450 | 8471 | |
a4703e31 KKD |
8472 | kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id); |
8473 | if (!kfunc_flags) { | |
e6ac2450 MKL |
8474 | verbose(env, "calling kernel function %s is not allowed\n", |
8475 | func_name); | |
8476 | return -EACCES; | |
8477 | } | |
00b85860 KKD |
8478 | |
8479 | /* Prepare kfunc call metadata */ | |
8480 | memset(&meta, 0, sizeof(meta)); | |
8481 | meta.btf = desc_btf; | |
8482 | meta.func_id = func_id; | |
8483 | meta.kfunc_flags = *kfunc_flags; | |
8484 | meta.func_proto = func_proto; | |
8485 | meta.func_name = func_name; | |
8486 | ||
8487 | if (is_kfunc_destructive(&meta) && !capable(CAP_SYS_BOOT)) { | |
8488 | verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capability\n"); | |
4dd48c6f AS |
8489 | return -EACCES; |
8490 | } | |
8491 | ||
00b85860 KKD |
8492 | if (is_kfunc_sleepable(&meta) && !env->prog->aux->sleepable) { |
8493 | verbose(env, "program must be sleepable to call sleepable kfunc %s\n", func_name); | |
8494 | return -EACCES; | |
8495 | } | |
eb1f7f71 | 8496 | |
e6ac2450 | 8497 | /* Check the arguments */ |
00b85860 | 8498 | err = check_kfunc_args(env, &meta); |
5c073f26 | 8499 | if (err < 0) |
e6ac2450 | 8500 | return err; |
5c073f26 | 8501 | /* In case of release function, we get register number of refcounted |
00b85860 | 8502 | * PTR_TO_BTF_ID in bpf_kfunc_arg_meta, do the release now. |
5c073f26 | 8503 | */ |
00b85860 KKD |
8504 | if (meta.release_regno) { |
8505 | err = release_reference(env, regs[meta.release_regno].ref_obj_id); | |
5c073f26 KKD |
8506 | if (err) { |
8507 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
8508 | func_name, func_id); | |
8509 | return err; | |
8510 | } | |
8511 | } | |
e6ac2450 MKL |
8512 | |
8513 | for (i = 0; i < CALLER_SAVED_REGS; i++) | |
8514 | mark_reg_not_init(env, regs, caller_saved[i]); | |
8515 | ||
8516 | /* Check return type */ | |
2357672c | 8517 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
5c073f26 | 8518 | |
00b85860 | 8519 | if (is_kfunc_acquire(&meta) && !btf_type_is_struct_ptr(meta.btf, t)) { |
958cf2e2 KKD |
8520 | /* Only exception is bpf_obj_new_impl */ |
8521 | if (meta.btf != btf_vmlinux || meta.func_id != special_kfunc_list[KF_bpf_obj_new_impl]) { | |
8522 | verbose(env, "acquire kernel function does not return PTR_TO_BTF_ID\n"); | |
8523 | return -EINVAL; | |
8524 | } | |
5c073f26 KKD |
8525 | } |
8526 | ||
e6ac2450 MKL |
8527 | if (btf_type_is_scalar(t)) { |
8528 | mark_reg_unknown(env, regs, BPF_REG_0); | |
8529 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
8530 | } else if (btf_type_is_ptr(t)) { | |
958cf2e2 KKD |
8531 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, &ptr_type_id); |
8532 | ||
8533 | if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) { | |
8534 | if (meta.func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
8535 | const struct btf_type *ret_t; | |
8536 | struct btf *ret_btf; | |
8537 | u32 ret_btf_id; | |
8538 | ||
8539 | if (((u64)(u32)meta.arg_constant.value) != meta.arg_constant.value) { | |
8540 | verbose(env, "local type ID argument must be in range [0, U32_MAX]\n"); | |
8541 | return -EINVAL; | |
8542 | } | |
8543 | ||
8544 | ret_btf = env->prog->aux->btf; | |
8545 | ret_btf_id = meta.arg_constant.value; | |
8546 | ||
8547 | /* This may be NULL due to user not supplying a BTF */ | |
8548 | if (!ret_btf) { | |
8549 | verbose(env, "bpf_obj_new requires prog BTF\n"); | |
8550 | return -EINVAL; | |
8551 | } | |
8552 | ||
8553 | ret_t = btf_type_by_id(ret_btf, ret_btf_id); | |
8554 | if (!ret_t || !__btf_type_is_struct(ret_t)) { | |
8555 | verbose(env, "bpf_obj_new type ID argument must be of a struct\n"); | |
8556 | return -EINVAL; | |
8557 | } | |
8558 | ||
8559 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
8560 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | MEM_ALLOC; | |
8561 | regs[BPF_REG_0].btf = ret_btf; | |
8562 | regs[BPF_REG_0].btf_id = ret_btf_id; | |
8563 | ||
8564 | env->insn_aux_data[insn_idx].obj_new_size = ret_t->size; | |
8565 | env->insn_aux_data[insn_idx].kptr_struct_meta = | |
8566 | btf_find_struct_meta(ret_btf, ret_btf_id); | |
ac9f0605 KKD |
8567 | } else if (meta.func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { |
8568 | env->insn_aux_data[insn_idx].kptr_struct_meta = | |
8569 | btf_find_struct_meta(meta.arg_obj_drop.btf, | |
8570 | meta.arg_obj_drop.btf_id); | |
958cf2e2 KKD |
8571 | } else { |
8572 | verbose(env, "kernel function %s unhandled dynamic return type\n", | |
8573 | meta.func_name); | |
8574 | return -EFAULT; | |
8575 | } | |
8576 | } else if (!__btf_type_is_struct(ptr_type)) { | |
eb1f7f71 BT |
8577 | if (!meta.r0_size) { |
8578 | ptr_type_name = btf_name_by_offset(desc_btf, | |
8579 | ptr_type->name_off); | |
8580 | verbose(env, | |
8581 | "kernel function %s returns pointer type %s %s is not supported\n", | |
8582 | func_name, | |
8583 | btf_type_str(ptr_type), | |
8584 | ptr_type_name); | |
8585 | return -EINVAL; | |
8586 | } | |
8587 | ||
8588 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
8589 | regs[BPF_REG_0].type = PTR_TO_MEM; | |
8590 | regs[BPF_REG_0].mem_size = meta.r0_size; | |
8591 | ||
8592 | if (meta.r0_rdonly) | |
8593 | regs[BPF_REG_0].type |= MEM_RDONLY; | |
8594 | ||
8595 | /* Ensures we don't access the memory after a release_reference() */ | |
8596 | if (meta.ref_obj_id) | |
8597 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
8598 | } else { | |
8599 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
8600 | regs[BPF_REG_0].btf = desc_btf; | |
8601 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; | |
8602 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
e6ac2450 | 8603 | } |
958cf2e2 | 8604 | |
00b85860 | 8605 | if (is_kfunc_ret_null(&meta)) { |
5c073f26 KKD |
8606 | regs[BPF_REG_0].type |= PTR_MAYBE_NULL; |
8607 | /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ | |
8608 | regs[BPF_REG_0].id = ++env->id_gen; | |
8609 | } | |
e6ac2450 | 8610 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
00b85860 | 8611 | if (is_kfunc_acquire(&meta)) { |
5c073f26 KKD |
8612 | int id = acquire_reference_state(env, insn_idx); |
8613 | ||
8614 | if (id < 0) | |
8615 | return id; | |
00b85860 KKD |
8616 | if (is_kfunc_ret_null(&meta)) |
8617 | regs[BPF_REG_0].id = id; | |
5c073f26 KKD |
8618 | regs[BPF_REG_0].ref_obj_id = id; |
8619 | } | |
00b85860 KKD |
8620 | if (reg_may_point_to_spin_lock(®s[BPF_REG_0]) && !regs[BPF_REG_0].id) |
8621 | regs[BPF_REG_0].id = ++env->id_gen; | |
e6ac2450 MKL |
8622 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ |
8623 | ||
8624 | nargs = btf_type_vlen(func_proto); | |
8625 | args = (const struct btf_param *)(func_proto + 1); | |
8626 | for (i = 0; i < nargs; i++) { | |
8627 | u32 regno = i + 1; | |
8628 | ||
2357672c | 8629 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
8630 | if (btf_type_is_ptr(t)) |
8631 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
8632 | else | |
8633 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
8634 | mark_btf_func_reg_size(env, regno, t->size); | |
8635 | } | |
8636 | ||
8637 | return 0; | |
8638 | } | |
8639 | ||
b03c9f9f EC |
8640 | static bool signed_add_overflows(s64 a, s64 b) |
8641 | { | |
8642 | /* Do the add in u64, where overflow is well-defined */ | |
8643 | s64 res = (s64)((u64)a + (u64)b); | |
8644 | ||
8645 | if (b < 0) | |
8646 | return res > a; | |
8647 | return res < a; | |
8648 | } | |
8649 | ||
bc895e8b | 8650 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
8651 | { |
8652 | /* Do the add in u32, where overflow is well-defined */ | |
8653 | s32 res = (s32)((u32)a + (u32)b); | |
8654 | ||
8655 | if (b < 0) | |
8656 | return res > a; | |
8657 | return res < a; | |
8658 | } | |
8659 | ||
bc895e8b | 8660 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
8661 | { |
8662 | /* Do the sub in u64, where overflow is well-defined */ | |
8663 | s64 res = (s64)((u64)a - (u64)b); | |
8664 | ||
8665 | if (b < 0) | |
8666 | return res < a; | |
8667 | return res > a; | |
969bf05e AS |
8668 | } |
8669 | ||
3f50f132 JF |
8670 | static bool signed_sub32_overflows(s32 a, s32 b) |
8671 | { | |
bc895e8b | 8672 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
8673 | s32 res = (s32)((u32)a - (u32)b); |
8674 | ||
8675 | if (b < 0) | |
8676 | return res < a; | |
8677 | return res > a; | |
8678 | } | |
8679 | ||
bb7f0f98 AS |
8680 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
8681 | const struct bpf_reg_state *reg, | |
8682 | enum bpf_reg_type type) | |
8683 | { | |
8684 | bool known = tnum_is_const(reg->var_off); | |
8685 | s64 val = reg->var_off.value; | |
8686 | s64 smin = reg->smin_value; | |
8687 | ||
8688 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
8689 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
c25b2ae1 | 8690 | reg_type_str(env, type), val); |
bb7f0f98 AS |
8691 | return false; |
8692 | } | |
8693 | ||
8694 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
8695 | verbose(env, "%s pointer offset %d is not allowed\n", | |
c25b2ae1 | 8696 | reg_type_str(env, type), reg->off); |
bb7f0f98 AS |
8697 | return false; |
8698 | } | |
8699 | ||
8700 | if (smin == S64_MIN) { | |
8701 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
c25b2ae1 | 8702 | reg_type_str(env, type)); |
bb7f0f98 AS |
8703 | return false; |
8704 | } | |
8705 | ||
8706 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
8707 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
c25b2ae1 | 8708 | smin, reg_type_str(env, type)); |
bb7f0f98 AS |
8709 | return false; |
8710 | } | |
8711 | ||
8712 | return true; | |
8713 | } | |
8714 | ||
a6aaece0 DB |
8715 | enum { |
8716 | REASON_BOUNDS = -1, | |
8717 | REASON_TYPE = -2, | |
8718 | REASON_PATHS = -3, | |
8719 | REASON_LIMIT = -4, | |
8720 | REASON_STACK = -5, | |
8721 | }; | |
8722 | ||
979d63d5 | 8723 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 8724 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 8725 | { |
7fedb63a | 8726 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
8727 | |
8728 | switch (ptr_reg->type) { | |
8729 | case PTR_TO_STACK: | |
1b1597e6 | 8730 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
8731 | * left direction, see BPF_REG_FP. Also, unknown scalar |
8732 | * offset where we would need to deal with min/max bounds is | |
8733 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
8734 | */ |
8735 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 8736 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 8737 | break; |
979d63d5 | 8738 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 8739 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
8740 | ptr_limit = (mask_to_left ? |
8741 | ptr_reg->smin_value : | |
8742 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 8743 | break; |
979d63d5 | 8744 | default: |
a6aaece0 | 8745 | return REASON_TYPE; |
979d63d5 | 8746 | } |
b658bbb8 DB |
8747 | |
8748 | if (ptr_limit >= max) | |
a6aaece0 | 8749 | return REASON_LIMIT; |
b658bbb8 DB |
8750 | *alu_limit = ptr_limit; |
8751 | return 0; | |
979d63d5 DB |
8752 | } |
8753 | ||
d3bd7413 DB |
8754 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
8755 | const struct bpf_insn *insn) | |
8756 | { | |
2c78ee89 | 8757 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
8758 | } |
8759 | ||
8760 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
8761 | u32 alu_state, u32 alu_limit) | |
8762 | { | |
8763 | /* If we arrived here from different branches with different | |
8764 | * state or limits to sanitize, then this won't work. | |
8765 | */ | |
8766 | if (aux->alu_state && | |
8767 | (aux->alu_state != alu_state || | |
8768 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 8769 | return REASON_PATHS; |
d3bd7413 | 8770 | |
e6ac5933 | 8771 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
8772 | aux->alu_state = alu_state; |
8773 | aux->alu_limit = alu_limit; | |
8774 | return 0; | |
8775 | } | |
8776 | ||
8777 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
8778 | struct bpf_insn *insn) | |
8779 | { | |
8780 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
8781 | ||
8782 | if (can_skip_alu_sanitation(env, insn)) | |
8783 | return 0; | |
8784 | ||
8785 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
8786 | } | |
8787 | ||
f5288193 DB |
8788 | static bool sanitize_needed(u8 opcode) |
8789 | { | |
8790 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
8791 | } | |
8792 | ||
3d0220f6 DB |
8793 | struct bpf_sanitize_info { |
8794 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 8795 | bool mask_to_left; |
3d0220f6 DB |
8796 | }; |
8797 | ||
9183671a DB |
8798 | static struct bpf_verifier_state * |
8799 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
8800 | const struct bpf_insn *insn, | |
8801 | u32 next_idx, u32 curr_idx) | |
8802 | { | |
8803 | struct bpf_verifier_state *branch; | |
8804 | struct bpf_reg_state *regs; | |
8805 | ||
8806 | branch = push_stack(env, next_idx, curr_idx, true); | |
8807 | if (branch && insn) { | |
8808 | regs = branch->frame[branch->curframe]->regs; | |
8809 | if (BPF_SRC(insn->code) == BPF_K) { | |
8810 | mark_reg_unknown(env, regs, insn->dst_reg); | |
8811 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
8812 | mark_reg_unknown(env, regs, insn->dst_reg); | |
8813 | mark_reg_unknown(env, regs, insn->src_reg); | |
8814 | } | |
8815 | } | |
8816 | return branch; | |
8817 | } | |
8818 | ||
979d63d5 DB |
8819 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
8820 | struct bpf_insn *insn, | |
8821 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 8822 | const struct bpf_reg_state *off_reg, |
979d63d5 | 8823 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 8824 | struct bpf_sanitize_info *info, |
7fedb63a | 8825 | const bool commit_window) |
979d63d5 | 8826 | { |
3d0220f6 | 8827 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 8828 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 8829 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 8830 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
8831 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
8832 | u8 opcode = BPF_OP(insn->code); | |
8833 | u32 alu_state, alu_limit; | |
8834 | struct bpf_reg_state tmp; | |
8835 | bool ret; | |
f232326f | 8836 | int err; |
979d63d5 | 8837 | |
d3bd7413 | 8838 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
8839 | return 0; |
8840 | ||
8841 | /* We already marked aux for masking from non-speculative | |
8842 | * paths, thus we got here in the first place. We only care | |
8843 | * to explore bad access from here. | |
8844 | */ | |
8845 | if (vstate->speculative) | |
8846 | goto do_sim; | |
8847 | ||
bb01a1bb DB |
8848 | if (!commit_window) { |
8849 | if (!tnum_is_const(off_reg->var_off) && | |
8850 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
8851 | return REASON_BOUNDS; | |
8852 | ||
8853 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
8854 | (opcode == BPF_SUB && !off_is_neg); | |
8855 | } | |
8856 | ||
8857 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
8858 | if (err < 0) |
8859 | return err; | |
8860 | ||
7fedb63a DB |
8861 | if (commit_window) { |
8862 | /* In commit phase we narrow the masking window based on | |
8863 | * the observed pointer move after the simulated operation. | |
8864 | */ | |
3d0220f6 DB |
8865 | alu_state = info->aux.alu_state; |
8866 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
8867 | } else { |
8868 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 8869 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
8870 | alu_state |= ptr_is_dst_reg ? |
8871 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
8872 | |
8873 | /* Limit pruning on unknown scalars to enable deep search for | |
8874 | * potential masking differences from other program paths. | |
8875 | */ | |
8876 | if (!off_is_imm) | |
8877 | env->explore_alu_limits = true; | |
7fedb63a DB |
8878 | } |
8879 | ||
f232326f PK |
8880 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
8881 | if (err < 0) | |
8882 | return err; | |
979d63d5 | 8883 | do_sim: |
7fedb63a DB |
8884 | /* If we're in commit phase, we're done here given we already |
8885 | * pushed the truncated dst_reg into the speculative verification | |
8886 | * stack. | |
a7036191 DB |
8887 | * |
8888 | * Also, when register is a known constant, we rewrite register-based | |
8889 | * operation to immediate-based, and thus do not need masking (and as | |
8890 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 8891 | */ |
a7036191 | 8892 | if (commit_window || off_is_imm) |
7fedb63a DB |
8893 | return 0; |
8894 | ||
979d63d5 DB |
8895 | /* Simulate and find potential out-of-bounds access under |
8896 | * speculative execution from truncation as a result of | |
8897 | * masking when off was not within expected range. If off | |
8898 | * sits in dst, then we temporarily need to move ptr there | |
8899 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
8900 | * for cases where we use K-based arithmetic in one direction | |
8901 | * and truncated reg-based in the other in order to explore | |
8902 | * bad access. | |
8903 | */ | |
8904 | if (!ptr_is_dst_reg) { | |
8905 | tmp = *dst_reg; | |
8906 | *dst_reg = *ptr_reg; | |
8907 | } | |
9183671a DB |
8908 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
8909 | env->insn_idx); | |
0803278b | 8910 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 8911 | *dst_reg = tmp; |
a6aaece0 DB |
8912 | return !ret ? REASON_STACK : 0; |
8913 | } | |
8914 | ||
fe9a5ca7 DB |
8915 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
8916 | { | |
8917 | struct bpf_verifier_state *vstate = env->cur_state; | |
8918 | ||
8919 | /* If we simulate paths under speculation, we don't update the | |
8920 | * insn as 'seen' such that when we verify unreachable paths in | |
8921 | * the non-speculative domain, sanitize_dead_code() can still | |
8922 | * rewrite/sanitize them. | |
8923 | */ | |
8924 | if (!vstate->speculative) | |
8925 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
8926 | } | |
8927 | ||
a6aaece0 DB |
8928 | static int sanitize_err(struct bpf_verifier_env *env, |
8929 | const struct bpf_insn *insn, int reason, | |
8930 | const struct bpf_reg_state *off_reg, | |
8931 | const struct bpf_reg_state *dst_reg) | |
8932 | { | |
8933 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
8934 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
8935 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
8936 | ||
8937 | switch (reason) { | |
8938 | case REASON_BOUNDS: | |
8939 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
8940 | off_reg == dst_reg ? dst : src, err); | |
8941 | break; | |
8942 | case REASON_TYPE: | |
8943 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
8944 | off_reg == dst_reg ? src : dst, err); | |
8945 | break; | |
8946 | case REASON_PATHS: | |
8947 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
8948 | dst, op, err); | |
8949 | break; | |
8950 | case REASON_LIMIT: | |
8951 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
8952 | dst, op, err); | |
8953 | break; | |
8954 | case REASON_STACK: | |
8955 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
8956 | dst, err); | |
8957 | break; | |
8958 | default: | |
8959 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
8960 | reason); | |
8961 | break; | |
8962 | } | |
8963 | ||
8964 | return -EACCES; | |
979d63d5 DB |
8965 | } |
8966 | ||
01f810ac AM |
8967 | /* check that stack access falls within stack limits and that 'reg' doesn't |
8968 | * have a variable offset. | |
8969 | * | |
8970 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
8971 | * requires corresponding support in Spectre masking for stack ALU. See also | |
8972 | * retrieve_ptr_limit(). | |
8973 | * | |
8974 | * | |
8975 | * 'off' includes 'reg->off'. | |
8976 | */ | |
8977 | static int check_stack_access_for_ptr_arithmetic( | |
8978 | struct bpf_verifier_env *env, | |
8979 | int regno, | |
8980 | const struct bpf_reg_state *reg, | |
8981 | int off) | |
8982 | { | |
8983 | if (!tnum_is_const(reg->var_off)) { | |
8984 | char tn_buf[48]; | |
8985 | ||
8986 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
8987 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
8988 | regno, tn_buf, off); | |
8989 | return -EACCES; | |
8990 | } | |
8991 | ||
8992 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
8993 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
8994 | "prohibited for !root; off=%d\n", regno, off); | |
8995 | return -EACCES; | |
8996 | } | |
8997 | ||
8998 | return 0; | |
8999 | } | |
9000 | ||
073815b7 DB |
9001 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
9002 | const struct bpf_insn *insn, | |
9003 | const struct bpf_reg_state *dst_reg) | |
9004 | { | |
9005 | u32 dst = insn->dst_reg; | |
9006 | ||
9007 | /* For unprivileged we require that resulting offset must be in bounds | |
9008 | * in order to be able to sanitize access later on. | |
9009 | */ | |
9010 | if (env->bypass_spec_v1) | |
9011 | return 0; | |
9012 | ||
9013 | switch (dst_reg->type) { | |
9014 | case PTR_TO_STACK: | |
9015 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
9016 | dst_reg->off + dst_reg->var_off.value)) | |
9017 | return -EACCES; | |
9018 | break; | |
9019 | case PTR_TO_MAP_VALUE: | |
61df10c7 | 9020 | if (check_map_access(env, dst, dst_reg->off, 1, false, ACCESS_HELPER)) { |
073815b7 DB |
9021 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
9022 | "prohibited for !root\n", dst); | |
9023 | return -EACCES; | |
9024 | } | |
9025 | break; | |
9026 | default: | |
9027 | break; | |
9028 | } | |
9029 | ||
9030 | return 0; | |
9031 | } | |
01f810ac | 9032 | |
f1174f77 | 9033 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
9034 | * Caller should also handle BPF_MOV case separately. |
9035 | * If we return -EACCES, caller may want to try again treating pointer as a | |
9036 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
9037 | */ | |
9038 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
9039 | struct bpf_insn *insn, | |
9040 | const struct bpf_reg_state *ptr_reg, | |
9041 | const struct bpf_reg_state *off_reg) | |
969bf05e | 9042 | { |
f4d7e40a AS |
9043 | struct bpf_verifier_state *vstate = env->cur_state; |
9044 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
9045 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 9046 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
9047 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
9048 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
9049 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
9050 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 9051 | struct bpf_sanitize_info info = {}; |
969bf05e | 9052 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 9053 | u32 dst = insn->dst_reg; |
979d63d5 | 9054 | int ret; |
969bf05e | 9055 | |
f1174f77 | 9056 | dst_reg = ®s[dst]; |
969bf05e | 9057 | |
6f16101e DB |
9058 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
9059 | smin_val > smax_val || umin_val > umax_val) { | |
9060 | /* Taint dst register if offset had invalid bounds derived from | |
9061 | * e.g. dead branches. | |
9062 | */ | |
f54c7898 | 9063 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 9064 | return 0; |
f1174f77 EC |
9065 | } |
9066 | ||
9067 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
9068 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
9069 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
9070 | __mark_reg_unknown(env, dst_reg); | |
9071 | return 0; | |
9072 | } | |
9073 | ||
82abbf8d AS |
9074 | verbose(env, |
9075 | "R%d 32-bit pointer arithmetic prohibited\n", | |
9076 | dst); | |
f1174f77 | 9077 | return -EACCES; |
969bf05e AS |
9078 | } |
9079 | ||
c25b2ae1 | 9080 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
aad2eeaf | 9081 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
c25b2ae1 | 9082 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 9083 | return -EACCES; |
c25b2ae1 HL |
9084 | } |
9085 | ||
9086 | switch (base_type(ptr_reg->type)) { | |
aad2eeaf | 9087 | case CONST_PTR_TO_MAP: |
7c696732 YS |
9088 | /* smin_val represents the known value */ |
9089 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
9090 | break; | |
8731745e | 9091 | fallthrough; |
aad2eeaf | 9092 | case PTR_TO_PACKET_END: |
c64b7983 | 9093 | case PTR_TO_SOCKET: |
46f8bc92 | 9094 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 9095 | case PTR_TO_TCP_SOCK: |
fada7fdc | 9096 | case PTR_TO_XDP_SOCK: |
aad2eeaf | 9097 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
c25b2ae1 | 9098 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 9099 | return -EACCES; |
aad2eeaf JS |
9100 | default: |
9101 | break; | |
f1174f77 EC |
9102 | } |
9103 | ||
9104 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
9105 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 9106 | */ |
f1174f77 EC |
9107 | dst_reg->type = ptr_reg->type; |
9108 | dst_reg->id = ptr_reg->id; | |
969bf05e | 9109 | |
bb7f0f98 AS |
9110 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
9111 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
9112 | return -EINVAL; | |
9113 | ||
3f50f132 JF |
9114 | /* pointer types do not carry 32-bit bounds at the moment. */ |
9115 | __mark_reg32_unbounded(dst_reg); | |
9116 | ||
7fedb63a DB |
9117 | if (sanitize_needed(opcode)) { |
9118 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 9119 | &info, false); |
a6aaece0 DB |
9120 | if (ret < 0) |
9121 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 9122 | } |
a6aaece0 | 9123 | |
f1174f77 EC |
9124 | switch (opcode) { |
9125 | case BPF_ADD: | |
9126 | /* We can take a fixed offset as long as it doesn't overflow | |
9127 | * the s32 'off' field | |
969bf05e | 9128 | */ |
b03c9f9f EC |
9129 | if (known && (ptr_reg->off + smin_val == |
9130 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 9131 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
9132 | dst_reg->smin_value = smin_ptr; |
9133 | dst_reg->smax_value = smax_ptr; | |
9134 | dst_reg->umin_value = umin_ptr; | |
9135 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 9136 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 9137 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 9138 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
9139 | break; |
9140 | } | |
f1174f77 EC |
9141 | /* A new variable offset is created. Note that off_reg->off |
9142 | * == 0, since it's a scalar. | |
9143 | * dst_reg gets the pointer type and since some positive | |
9144 | * integer value was added to the pointer, give it a new 'id' | |
9145 | * if it's a PTR_TO_PACKET. | |
9146 | * this creates a new 'base' pointer, off_reg (variable) gets | |
9147 | * added into the variable offset, and we copy the fixed offset | |
9148 | * from ptr_reg. | |
969bf05e | 9149 | */ |
b03c9f9f EC |
9150 | if (signed_add_overflows(smin_ptr, smin_val) || |
9151 | signed_add_overflows(smax_ptr, smax_val)) { | |
9152 | dst_reg->smin_value = S64_MIN; | |
9153 | dst_reg->smax_value = S64_MAX; | |
9154 | } else { | |
9155 | dst_reg->smin_value = smin_ptr + smin_val; | |
9156 | dst_reg->smax_value = smax_ptr + smax_val; | |
9157 | } | |
9158 | if (umin_ptr + umin_val < umin_ptr || | |
9159 | umax_ptr + umax_val < umax_ptr) { | |
9160 | dst_reg->umin_value = 0; | |
9161 | dst_reg->umax_value = U64_MAX; | |
9162 | } else { | |
9163 | dst_reg->umin_value = umin_ptr + umin_val; | |
9164 | dst_reg->umax_value = umax_ptr + umax_val; | |
9165 | } | |
f1174f77 EC |
9166 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
9167 | dst_reg->off = ptr_reg->off; | |
0962590e | 9168 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 9169 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
9170 | dst_reg->id = ++env->id_gen; |
9171 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 9172 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
9173 | } |
9174 | break; | |
9175 | case BPF_SUB: | |
9176 | if (dst_reg == off_reg) { | |
9177 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
9178 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
9179 | dst); | |
f1174f77 EC |
9180 | return -EACCES; |
9181 | } | |
9182 | /* We don't allow subtraction from FP, because (according to | |
9183 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
9184 | * be able to deal with it. | |
969bf05e | 9185 | */ |
f1174f77 | 9186 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
9187 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
9188 | dst); | |
f1174f77 EC |
9189 | return -EACCES; |
9190 | } | |
b03c9f9f EC |
9191 | if (known && (ptr_reg->off - smin_val == |
9192 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 9193 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
9194 | dst_reg->smin_value = smin_ptr; |
9195 | dst_reg->smax_value = smax_ptr; | |
9196 | dst_reg->umin_value = umin_ptr; | |
9197 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
9198 | dst_reg->var_off = ptr_reg->var_off; |
9199 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 9200 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 9201 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
9202 | break; |
9203 | } | |
f1174f77 EC |
9204 | /* A new variable offset is created. If the subtrahend is known |
9205 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 9206 | */ |
b03c9f9f EC |
9207 | if (signed_sub_overflows(smin_ptr, smax_val) || |
9208 | signed_sub_overflows(smax_ptr, smin_val)) { | |
9209 | /* Overflow possible, we know nothing */ | |
9210 | dst_reg->smin_value = S64_MIN; | |
9211 | dst_reg->smax_value = S64_MAX; | |
9212 | } else { | |
9213 | dst_reg->smin_value = smin_ptr - smax_val; | |
9214 | dst_reg->smax_value = smax_ptr - smin_val; | |
9215 | } | |
9216 | if (umin_ptr < umax_val) { | |
9217 | /* Overflow possible, we know nothing */ | |
9218 | dst_reg->umin_value = 0; | |
9219 | dst_reg->umax_value = U64_MAX; | |
9220 | } else { | |
9221 | /* Cannot overflow (as long as bounds are consistent) */ | |
9222 | dst_reg->umin_value = umin_ptr - umax_val; | |
9223 | dst_reg->umax_value = umax_ptr - umin_val; | |
9224 | } | |
f1174f77 EC |
9225 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
9226 | dst_reg->off = ptr_reg->off; | |
0962590e | 9227 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 9228 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
9229 | dst_reg->id = ++env->id_gen; |
9230 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 9231 | if (smin_val < 0) |
22dc4a0f | 9232 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 9233 | } |
f1174f77 EC |
9234 | break; |
9235 | case BPF_AND: | |
9236 | case BPF_OR: | |
9237 | case BPF_XOR: | |
82abbf8d AS |
9238 | /* bitwise ops on pointers are troublesome, prohibit. */ |
9239 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
9240 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
9241 | return -EACCES; |
9242 | default: | |
9243 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
9244 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
9245 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 9246 | return -EACCES; |
43188702 JF |
9247 | } |
9248 | ||
bb7f0f98 AS |
9249 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
9250 | return -EINVAL; | |
3844d153 | 9251 | reg_bounds_sync(dst_reg); |
073815b7 DB |
9252 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
9253 | return -EACCES; | |
7fedb63a DB |
9254 | if (sanitize_needed(opcode)) { |
9255 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 9256 | &info, true); |
7fedb63a DB |
9257 | if (ret < 0) |
9258 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
9259 | } |
9260 | ||
43188702 JF |
9261 | return 0; |
9262 | } | |
9263 | ||
3f50f132 JF |
9264 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
9265 | struct bpf_reg_state *src_reg) | |
9266 | { | |
9267 | s32 smin_val = src_reg->s32_min_value; | |
9268 | s32 smax_val = src_reg->s32_max_value; | |
9269 | u32 umin_val = src_reg->u32_min_value; | |
9270 | u32 umax_val = src_reg->u32_max_value; | |
9271 | ||
9272 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
9273 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
9274 | dst_reg->s32_min_value = S32_MIN; | |
9275 | dst_reg->s32_max_value = S32_MAX; | |
9276 | } else { | |
9277 | dst_reg->s32_min_value += smin_val; | |
9278 | dst_reg->s32_max_value += smax_val; | |
9279 | } | |
9280 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
9281 | dst_reg->u32_max_value + umax_val < umax_val) { | |
9282 | dst_reg->u32_min_value = 0; | |
9283 | dst_reg->u32_max_value = U32_MAX; | |
9284 | } else { | |
9285 | dst_reg->u32_min_value += umin_val; | |
9286 | dst_reg->u32_max_value += umax_val; | |
9287 | } | |
9288 | } | |
9289 | ||
07cd2631 JF |
9290 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
9291 | struct bpf_reg_state *src_reg) | |
9292 | { | |
9293 | s64 smin_val = src_reg->smin_value; | |
9294 | s64 smax_val = src_reg->smax_value; | |
9295 | u64 umin_val = src_reg->umin_value; | |
9296 | u64 umax_val = src_reg->umax_value; | |
9297 | ||
9298 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
9299 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
9300 | dst_reg->smin_value = S64_MIN; | |
9301 | dst_reg->smax_value = S64_MAX; | |
9302 | } else { | |
9303 | dst_reg->smin_value += smin_val; | |
9304 | dst_reg->smax_value += smax_val; | |
9305 | } | |
9306 | if (dst_reg->umin_value + umin_val < umin_val || | |
9307 | dst_reg->umax_value + umax_val < umax_val) { | |
9308 | dst_reg->umin_value = 0; | |
9309 | dst_reg->umax_value = U64_MAX; | |
9310 | } else { | |
9311 | dst_reg->umin_value += umin_val; | |
9312 | dst_reg->umax_value += umax_val; | |
9313 | } | |
3f50f132 JF |
9314 | } |
9315 | ||
9316 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
9317 | struct bpf_reg_state *src_reg) | |
9318 | { | |
9319 | s32 smin_val = src_reg->s32_min_value; | |
9320 | s32 smax_val = src_reg->s32_max_value; | |
9321 | u32 umin_val = src_reg->u32_min_value; | |
9322 | u32 umax_val = src_reg->u32_max_value; | |
9323 | ||
9324 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
9325 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
9326 | /* Overflow possible, we know nothing */ | |
9327 | dst_reg->s32_min_value = S32_MIN; | |
9328 | dst_reg->s32_max_value = S32_MAX; | |
9329 | } else { | |
9330 | dst_reg->s32_min_value -= smax_val; | |
9331 | dst_reg->s32_max_value -= smin_val; | |
9332 | } | |
9333 | if (dst_reg->u32_min_value < umax_val) { | |
9334 | /* Overflow possible, we know nothing */ | |
9335 | dst_reg->u32_min_value = 0; | |
9336 | dst_reg->u32_max_value = U32_MAX; | |
9337 | } else { | |
9338 | /* Cannot overflow (as long as bounds are consistent) */ | |
9339 | dst_reg->u32_min_value -= umax_val; | |
9340 | dst_reg->u32_max_value -= umin_val; | |
9341 | } | |
07cd2631 JF |
9342 | } |
9343 | ||
9344 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
9345 | struct bpf_reg_state *src_reg) | |
9346 | { | |
9347 | s64 smin_val = src_reg->smin_value; | |
9348 | s64 smax_val = src_reg->smax_value; | |
9349 | u64 umin_val = src_reg->umin_value; | |
9350 | u64 umax_val = src_reg->umax_value; | |
9351 | ||
9352 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
9353 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
9354 | /* Overflow possible, we know nothing */ | |
9355 | dst_reg->smin_value = S64_MIN; | |
9356 | dst_reg->smax_value = S64_MAX; | |
9357 | } else { | |
9358 | dst_reg->smin_value -= smax_val; | |
9359 | dst_reg->smax_value -= smin_val; | |
9360 | } | |
9361 | if (dst_reg->umin_value < umax_val) { | |
9362 | /* Overflow possible, we know nothing */ | |
9363 | dst_reg->umin_value = 0; | |
9364 | dst_reg->umax_value = U64_MAX; | |
9365 | } else { | |
9366 | /* Cannot overflow (as long as bounds are consistent) */ | |
9367 | dst_reg->umin_value -= umax_val; | |
9368 | dst_reg->umax_value -= umin_val; | |
9369 | } | |
3f50f132 JF |
9370 | } |
9371 | ||
9372 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
9373 | struct bpf_reg_state *src_reg) | |
9374 | { | |
9375 | s32 smin_val = src_reg->s32_min_value; | |
9376 | u32 umin_val = src_reg->u32_min_value; | |
9377 | u32 umax_val = src_reg->u32_max_value; | |
9378 | ||
9379 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
9380 | /* Ain't nobody got time to multiply that sign */ | |
9381 | __mark_reg32_unbounded(dst_reg); | |
9382 | return; | |
9383 | } | |
9384 | /* Both values are positive, so we can work with unsigned and | |
9385 | * copy the result to signed (unless it exceeds S32_MAX). | |
9386 | */ | |
9387 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
9388 | /* Potential overflow, we know nothing */ | |
9389 | __mark_reg32_unbounded(dst_reg); | |
9390 | return; | |
9391 | } | |
9392 | dst_reg->u32_min_value *= umin_val; | |
9393 | dst_reg->u32_max_value *= umax_val; | |
9394 | if (dst_reg->u32_max_value > S32_MAX) { | |
9395 | /* Overflow possible, we know nothing */ | |
9396 | dst_reg->s32_min_value = S32_MIN; | |
9397 | dst_reg->s32_max_value = S32_MAX; | |
9398 | } else { | |
9399 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
9400 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
9401 | } | |
07cd2631 JF |
9402 | } |
9403 | ||
9404 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
9405 | struct bpf_reg_state *src_reg) | |
9406 | { | |
9407 | s64 smin_val = src_reg->smin_value; | |
9408 | u64 umin_val = src_reg->umin_value; | |
9409 | u64 umax_val = src_reg->umax_value; | |
9410 | ||
07cd2631 JF |
9411 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
9412 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 9413 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
9414 | return; |
9415 | } | |
9416 | /* Both values are positive, so we can work with unsigned and | |
9417 | * copy the result to signed (unless it exceeds S64_MAX). | |
9418 | */ | |
9419 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
9420 | /* Potential overflow, we know nothing */ | |
3f50f132 | 9421 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
9422 | return; |
9423 | } | |
9424 | dst_reg->umin_value *= umin_val; | |
9425 | dst_reg->umax_value *= umax_val; | |
9426 | if (dst_reg->umax_value > S64_MAX) { | |
9427 | /* Overflow possible, we know nothing */ | |
9428 | dst_reg->smin_value = S64_MIN; | |
9429 | dst_reg->smax_value = S64_MAX; | |
9430 | } else { | |
9431 | dst_reg->smin_value = dst_reg->umin_value; | |
9432 | dst_reg->smax_value = dst_reg->umax_value; | |
9433 | } | |
9434 | } | |
9435 | ||
3f50f132 JF |
9436 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
9437 | struct bpf_reg_state *src_reg) | |
9438 | { | |
9439 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
9440 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
9441 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
9442 | s32 smin_val = src_reg->s32_min_value; | |
9443 | u32 umax_val = src_reg->u32_max_value; | |
9444 | ||
049c4e13 DB |
9445 | if (src_known && dst_known) { |
9446 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 9447 | return; |
049c4e13 | 9448 | } |
3f50f132 JF |
9449 | |
9450 | /* We get our minimum from the var_off, since that's inherently | |
9451 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
9452 | */ | |
9453 | dst_reg->u32_min_value = var32_off.value; | |
9454 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
9455 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
9456 | /* Lose signed bounds when ANDing negative numbers, | |
9457 | * ain't nobody got time for that. | |
9458 | */ | |
9459 | dst_reg->s32_min_value = S32_MIN; | |
9460 | dst_reg->s32_max_value = S32_MAX; | |
9461 | } else { | |
9462 | /* ANDing two positives gives a positive, so safe to | |
9463 | * cast result into s64. | |
9464 | */ | |
9465 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
9466 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
9467 | } | |
3f50f132 JF |
9468 | } |
9469 | ||
07cd2631 JF |
9470 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
9471 | struct bpf_reg_state *src_reg) | |
9472 | { | |
3f50f132 JF |
9473 | bool src_known = tnum_is_const(src_reg->var_off); |
9474 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
9475 | s64 smin_val = src_reg->smin_value; |
9476 | u64 umax_val = src_reg->umax_value; | |
9477 | ||
3f50f132 | 9478 | if (src_known && dst_known) { |
4fbb38a3 | 9479 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
9480 | return; |
9481 | } | |
9482 | ||
07cd2631 JF |
9483 | /* We get our minimum from the var_off, since that's inherently |
9484 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
9485 | */ | |
07cd2631 JF |
9486 | dst_reg->umin_value = dst_reg->var_off.value; |
9487 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
9488 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
9489 | /* Lose signed bounds when ANDing negative numbers, | |
9490 | * ain't nobody got time for that. | |
9491 | */ | |
9492 | dst_reg->smin_value = S64_MIN; | |
9493 | dst_reg->smax_value = S64_MAX; | |
9494 | } else { | |
9495 | /* ANDing two positives gives a positive, so safe to | |
9496 | * cast result into s64. | |
9497 | */ | |
9498 | dst_reg->smin_value = dst_reg->umin_value; | |
9499 | dst_reg->smax_value = dst_reg->umax_value; | |
9500 | } | |
9501 | /* We may learn something more from the var_off */ | |
9502 | __update_reg_bounds(dst_reg); | |
9503 | } | |
9504 | ||
3f50f132 JF |
9505 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
9506 | struct bpf_reg_state *src_reg) | |
9507 | { | |
9508 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
9509 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
9510 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
9511 | s32 smin_val = src_reg->s32_min_value; |
9512 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 9513 | |
049c4e13 DB |
9514 | if (src_known && dst_known) { |
9515 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 9516 | return; |
049c4e13 | 9517 | } |
3f50f132 JF |
9518 | |
9519 | /* We get our maximum from the var_off, and our minimum is the | |
9520 | * maximum of the operands' minima | |
9521 | */ | |
9522 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
9523 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
9524 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
9525 | /* Lose signed bounds when ORing negative numbers, | |
9526 | * ain't nobody got time for that. | |
9527 | */ | |
9528 | dst_reg->s32_min_value = S32_MIN; | |
9529 | dst_reg->s32_max_value = S32_MAX; | |
9530 | } else { | |
9531 | /* ORing two positives gives a positive, so safe to | |
9532 | * cast result into s64. | |
9533 | */ | |
5b9fbeb7 DB |
9534 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
9535 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
9536 | } |
9537 | } | |
9538 | ||
07cd2631 JF |
9539 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
9540 | struct bpf_reg_state *src_reg) | |
9541 | { | |
3f50f132 JF |
9542 | bool src_known = tnum_is_const(src_reg->var_off); |
9543 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
9544 | s64 smin_val = src_reg->smin_value; |
9545 | u64 umin_val = src_reg->umin_value; | |
9546 | ||
3f50f132 | 9547 | if (src_known && dst_known) { |
4fbb38a3 | 9548 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
9549 | return; |
9550 | } | |
9551 | ||
07cd2631 JF |
9552 | /* We get our maximum from the var_off, and our minimum is the |
9553 | * maximum of the operands' minima | |
9554 | */ | |
07cd2631 JF |
9555 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
9556 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
9557 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
9558 | /* Lose signed bounds when ORing negative numbers, | |
9559 | * ain't nobody got time for that. | |
9560 | */ | |
9561 | dst_reg->smin_value = S64_MIN; | |
9562 | dst_reg->smax_value = S64_MAX; | |
9563 | } else { | |
9564 | /* ORing two positives gives a positive, so safe to | |
9565 | * cast result into s64. | |
9566 | */ | |
9567 | dst_reg->smin_value = dst_reg->umin_value; | |
9568 | dst_reg->smax_value = dst_reg->umax_value; | |
9569 | } | |
9570 | /* We may learn something more from the var_off */ | |
9571 | __update_reg_bounds(dst_reg); | |
9572 | } | |
9573 | ||
2921c90d YS |
9574 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
9575 | struct bpf_reg_state *src_reg) | |
9576 | { | |
9577 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
9578 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
9579 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
9580 | s32 smin_val = src_reg->s32_min_value; | |
9581 | ||
049c4e13 DB |
9582 | if (src_known && dst_known) { |
9583 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 9584 | return; |
049c4e13 | 9585 | } |
2921c90d YS |
9586 | |
9587 | /* We get both minimum and maximum from the var32_off. */ | |
9588 | dst_reg->u32_min_value = var32_off.value; | |
9589 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
9590 | ||
9591 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
9592 | /* XORing two positive sign numbers gives a positive, | |
9593 | * so safe to cast u32 result into s32. | |
9594 | */ | |
9595 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
9596 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
9597 | } else { | |
9598 | dst_reg->s32_min_value = S32_MIN; | |
9599 | dst_reg->s32_max_value = S32_MAX; | |
9600 | } | |
9601 | } | |
9602 | ||
9603 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
9604 | struct bpf_reg_state *src_reg) | |
9605 | { | |
9606 | bool src_known = tnum_is_const(src_reg->var_off); | |
9607 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
9608 | s64 smin_val = src_reg->smin_value; | |
9609 | ||
9610 | if (src_known && dst_known) { | |
9611 | /* dst_reg->var_off.value has been updated earlier */ | |
9612 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
9613 | return; | |
9614 | } | |
9615 | ||
9616 | /* We get both minimum and maximum from the var_off. */ | |
9617 | dst_reg->umin_value = dst_reg->var_off.value; | |
9618 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
9619 | ||
9620 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
9621 | /* XORing two positive sign numbers gives a positive, | |
9622 | * so safe to cast u64 result into s64. | |
9623 | */ | |
9624 | dst_reg->smin_value = dst_reg->umin_value; | |
9625 | dst_reg->smax_value = dst_reg->umax_value; | |
9626 | } else { | |
9627 | dst_reg->smin_value = S64_MIN; | |
9628 | dst_reg->smax_value = S64_MAX; | |
9629 | } | |
9630 | ||
9631 | __update_reg_bounds(dst_reg); | |
9632 | } | |
9633 | ||
3f50f132 JF |
9634 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
9635 | u64 umin_val, u64 umax_val) | |
07cd2631 | 9636 | { |
07cd2631 JF |
9637 | /* We lose all sign bit information (except what we can pick |
9638 | * up from var_off) | |
9639 | */ | |
3f50f132 JF |
9640 | dst_reg->s32_min_value = S32_MIN; |
9641 | dst_reg->s32_max_value = S32_MAX; | |
9642 | /* If we might shift our top bit out, then we know nothing */ | |
9643 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
9644 | dst_reg->u32_min_value = 0; | |
9645 | dst_reg->u32_max_value = U32_MAX; | |
9646 | } else { | |
9647 | dst_reg->u32_min_value <<= umin_val; | |
9648 | dst_reg->u32_max_value <<= umax_val; | |
9649 | } | |
9650 | } | |
9651 | ||
9652 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
9653 | struct bpf_reg_state *src_reg) | |
9654 | { | |
9655 | u32 umax_val = src_reg->u32_max_value; | |
9656 | u32 umin_val = src_reg->u32_min_value; | |
9657 | /* u32 alu operation will zext upper bits */ | |
9658 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
9659 | ||
9660 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
9661 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
9662 | /* Not required but being careful mark reg64 bounds as unknown so | |
9663 | * that we are forced to pick them up from tnum and zext later and | |
9664 | * if some path skips this step we are still safe. | |
9665 | */ | |
9666 | __mark_reg64_unbounded(dst_reg); | |
9667 | __update_reg32_bounds(dst_reg); | |
9668 | } | |
9669 | ||
9670 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
9671 | u64 umin_val, u64 umax_val) | |
9672 | { | |
9673 | /* Special case <<32 because it is a common compiler pattern to sign | |
9674 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
9675 | * positive we know this shift will also be positive so we can track | |
9676 | * bounds correctly. Otherwise we lose all sign bit information except | |
9677 | * what we can pick up from var_off. Perhaps we can generalize this | |
9678 | * later to shifts of any length. | |
9679 | */ | |
9680 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
9681 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
9682 | else | |
9683 | dst_reg->smax_value = S64_MAX; | |
9684 | ||
9685 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
9686 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
9687 | else | |
9688 | dst_reg->smin_value = S64_MIN; | |
9689 | ||
07cd2631 JF |
9690 | /* If we might shift our top bit out, then we know nothing */ |
9691 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
9692 | dst_reg->umin_value = 0; | |
9693 | dst_reg->umax_value = U64_MAX; | |
9694 | } else { | |
9695 | dst_reg->umin_value <<= umin_val; | |
9696 | dst_reg->umax_value <<= umax_val; | |
9697 | } | |
3f50f132 JF |
9698 | } |
9699 | ||
9700 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
9701 | struct bpf_reg_state *src_reg) | |
9702 | { | |
9703 | u64 umax_val = src_reg->umax_value; | |
9704 | u64 umin_val = src_reg->umin_value; | |
9705 | ||
9706 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
9707 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
9708 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
9709 | ||
07cd2631 JF |
9710 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
9711 | /* We may learn something more from the var_off */ | |
9712 | __update_reg_bounds(dst_reg); | |
9713 | } | |
9714 | ||
3f50f132 JF |
9715 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
9716 | struct bpf_reg_state *src_reg) | |
9717 | { | |
9718 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
9719 | u32 umax_val = src_reg->u32_max_value; | |
9720 | u32 umin_val = src_reg->u32_min_value; | |
9721 | ||
9722 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
9723 | * be negative, then either: | |
9724 | * 1) src_reg might be zero, so the sign bit of the result is | |
9725 | * unknown, so we lose our signed bounds | |
9726 | * 2) it's known negative, thus the unsigned bounds capture the | |
9727 | * signed bounds | |
9728 | * 3) the signed bounds cross zero, so they tell us nothing | |
9729 | * about the result | |
9730 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 9731 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
9732 | * Thus, in all cases it suffices to blow away our signed bounds |
9733 | * and rely on inferring new ones from the unsigned bounds and | |
9734 | * var_off of the result. | |
9735 | */ | |
9736 | dst_reg->s32_min_value = S32_MIN; | |
9737 | dst_reg->s32_max_value = S32_MAX; | |
9738 | ||
9739 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
9740 | dst_reg->u32_min_value >>= umax_val; | |
9741 | dst_reg->u32_max_value >>= umin_val; | |
9742 | ||
9743 | __mark_reg64_unbounded(dst_reg); | |
9744 | __update_reg32_bounds(dst_reg); | |
9745 | } | |
9746 | ||
07cd2631 JF |
9747 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
9748 | struct bpf_reg_state *src_reg) | |
9749 | { | |
9750 | u64 umax_val = src_reg->umax_value; | |
9751 | u64 umin_val = src_reg->umin_value; | |
9752 | ||
9753 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
9754 | * be negative, then either: | |
9755 | * 1) src_reg might be zero, so the sign bit of the result is | |
9756 | * unknown, so we lose our signed bounds | |
9757 | * 2) it's known negative, thus the unsigned bounds capture the | |
9758 | * signed bounds | |
9759 | * 3) the signed bounds cross zero, so they tell us nothing | |
9760 | * about the result | |
9761 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 9762 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
9763 | * Thus, in all cases it suffices to blow away our signed bounds |
9764 | * and rely on inferring new ones from the unsigned bounds and | |
9765 | * var_off of the result. | |
9766 | */ | |
9767 | dst_reg->smin_value = S64_MIN; | |
9768 | dst_reg->smax_value = S64_MAX; | |
9769 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
9770 | dst_reg->umin_value >>= umax_val; | |
9771 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
9772 | |
9773 | /* Its not easy to operate on alu32 bounds here because it depends | |
9774 | * on bits being shifted in. Take easy way out and mark unbounded | |
9775 | * so we can recalculate later from tnum. | |
9776 | */ | |
9777 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
9778 | __update_reg_bounds(dst_reg); |
9779 | } | |
9780 | ||
3f50f132 JF |
9781 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
9782 | struct bpf_reg_state *src_reg) | |
07cd2631 | 9783 | { |
3f50f132 | 9784 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
9785 | |
9786 | /* Upon reaching here, src_known is true and | |
9787 | * umax_val is equal to umin_val. | |
9788 | */ | |
3f50f132 JF |
9789 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
9790 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 9791 | |
3f50f132 JF |
9792 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
9793 | ||
9794 | /* blow away the dst_reg umin_value/umax_value and rely on | |
9795 | * dst_reg var_off to refine the result. | |
9796 | */ | |
9797 | dst_reg->u32_min_value = 0; | |
9798 | dst_reg->u32_max_value = U32_MAX; | |
9799 | ||
9800 | __mark_reg64_unbounded(dst_reg); | |
9801 | __update_reg32_bounds(dst_reg); | |
9802 | } | |
9803 | ||
9804 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
9805 | struct bpf_reg_state *src_reg) | |
9806 | { | |
9807 | u64 umin_val = src_reg->umin_value; | |
9808 | ||
9809 | /* Upon reaching here, src_known is true and umax_val is equal | |
9810 | * to umin_val. | |
9811 | */ | |
9812 | dst_reg->smin_value >>= umin_val; | |
9813 | dst_reg->smax_value >>= umin_val; | |
9814 | ||
9815 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
9816 | |
9817 | /* blow away the dst_reg umin_value/umax_value and rely on | |
9818 | * dst_reg var_off to refine the result. | |
9819 | */ | |
9820 | dst_reg->umin_value = 0; | |
9821 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
9822 | |
9823 | /* Its not easy to operate on alu32 bounds here because it depends | |
9824 | * on bits being shifted in from upper 32-bits. Take easy way out | |
9825 | * and mark unbounded so we can recalculate later from tnum. | |
9826 | */ | |
9827 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
9828 | __update_reg_bounds(dst_reg); |
9829 | } | |
9830 | ||
468f6eaf JH |
9831 | /* WARNING: This function does calculations on 64-bit values, but the actual |
9832 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
9833 | * need extra checks in the 32-bit case. | |
9834 | */ | |
f1174f77 EC |
9835 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
9836 | struct bpf_insn *insn, | |
9837 | struct bpf_reg_state *dst_reg, | |
9838 | struct bpf_reg_state src_reg) | |
969bf05e | 9839 | { |
638f5b90 | 9840 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 9841 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 9842 | bool src_known; |
b03c9f9f EC |
9843 | s64 smin_val, smax_val; |
9844 | u64 umin_val, umax_val; | |
3f50f132 JF |
9845 | s32 s32_min_val, s32_max_val; |
9846 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 9847 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 9848 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 9849 | int ret; |
b799207e | 9850 | |
b03c9f9f EC |
9851 | smin_val = src_reg.smin_value; |
9852 | smax_val = src_reg.smax_value; | |
9853 | umin_val = src_reg.umin_value; | |
9854 | umax_val = src_reg.umax_value; | |
f23cc643 | 9855 | |
3f50f132 JF |
9856 | s32_min_val = src_reg.s32_min_value; |
9857 | s32_max_val = src_reg.s32_max_value; | |
9858 | u32_min_val = src_reg.u32_min_value; | |
9859 | u32_max_val = src_reg.u32_max_value; | |
9860 | ||
9861 | if (alu32) { | |
9862 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
9863 | if ((src_known && |
9864 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
9865 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
9866 | /* Taint dst register if offset had invalid bounds | |
9867 | * derived from e.g. dead branches. | |
9868 | */ | |
9869 | __mark_reg_unknown(env, dst_reg); | |
9870 | return 0; | |
9871 | } | |
9872 | } else { | |
9873 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
9874 | if ((src_known && |
9875 | (smin_val != smax_val || umin_val != umax_val)) || | |
9876 | smin_val > smax_val || umin_val > umax_val) { | |
9877 | /* Taint dst register if offset had invalid bounds | |
9878 | * derived from e.g. dead branches. | |
9879 | */ | |
9880 | __mark_reg_unknown(env, dst_reg); | |
9881 | return 0; | |
9882 | } | |
6f16101e DB |
9883 | } |
9884 | ||
bb7f0f98 AS |
9885 | if (!src_known && |
9886 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 9887 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
9888 | return 0; |
9889 | } | |
9890 | ||
f5288193 DB |
9891 | if (sanitize_needed(opcode)) { |
9892 | ret = sanitize_val_alu(env, insn); | |
9893 | if (ret < 0) | |
9894 | return sanitize_err(env, insn, ret, NULL, NULL); | |
9895 | } | |
9896 | ||
3f50f132 JF |
9897 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
9898 | * There are two classes of instructions: The first class we track both | |
9899 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
9900 | * greatest amount of precision when alu operations are mixed with jmp32 | |
9901 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
9902 | * and BPF_OR. This is possible because these ops have fairly easy to | |
9903 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
9904 | * See alu32 verifier tests for examples. The second class of | |
9905 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
9906 | * with regards to tracking sign/unsigned bounds because the bits may | |
9907 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
9908 | * the reg unbounded in the subreg bound space and use the resulting | |
9909 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
9910 | */ | |
48461135 JB |
9911 | switch (opcode) { |
9912 | case BPF_ADD: | |
3f50f132 | 9913 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 9914 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 9915 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
9916 | break; |
9917 | case BPF_SUB: | |
3f50f132 | 9918 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 9919 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 9920 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
9921 | break; |
9922 | case BPF_MUL: | |
3f50f132 JF |
9923 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
9924 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 9925 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
9926 | break; |
9927 | case BPF_AND: | |
3f50f132 JF |
9928 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
9929 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 9930 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
9931 | break; |
9932 | case BPF_OR: | |
3f50f132 JF |
9933 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
9934 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 9935 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 9936 | break; |
2921c90d YS |
9937 | case BPF_XOR: |
9938 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
9939 | scalar32_min_max_xor(dst_reg, &src_reg); | |
9940 | scalar_min_max_xor(dst_reg, &src_reg); | |
9941 | break; | |
48461135 | 9942 | case BPF_LSH: |
468f6eaf JH |
9943 | if (umax_val >= insn_bitness) { |
9944 | /* Shifts greater than 31 or 63 are undefined. | |
9945 | * This includes shifts by a negative number. | |
b03c9f9f | 9946 | */ |
61bd5218 | 9947 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
9948 | break; |
9949 | } | |
3f50f132 JF |
9950 | if (alu32) |
9951 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
9952 | else | |
9953 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
9954 | break; |
9955 | case BPF_RSH: | |
468f6eaf JH |
9956 | if (umax_val >= insn_bitness) { |
9957 | /* Shifts greater than 31 or 63 are undefined. | |
9958 | * This includes shifts by a negative number. | |
b03c9f9f | 9959 | */ |
61bd5218 | 9960 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
9961 | break; |
9962 | } | |
3f50f132 JF |
9963 | if (alu32) |
9964 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
9965 | else | |
9966 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 9967 | break; |
9cbe1f5a YS |
9968 | case BPF_ARSH: |
9969 | if (umax_val >= insn_bitness) { | |
9970 | /* Shifts greater than 31 or 63 are undefined. | |
9971 | * This includes shifts by a negative number. | |
9972 | */ | |
9973 | mark_reg_unknown(env, regs, insn->dst_reg); | |
9974 | break; | |
9975 | } | |
3f50f132 JF |
9976 | if (alu32) |
9977 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
9978 | else | |
9979 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 9980 | break; |
48461135 | 9981 | default: |
61bd5218 | 9982 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
9983 | break; |
9984 | } | |
9985 | ||
3f50f132 JF |
9986 | /* ALU32 ops are zero extended into 64bit register */ |
9987 | if (alu32) | |
9988 | zext_32_to_64(dst_reg); | |
3844d153 | 9989 | reg_bounds_sync(dst_reg); |
f1174f77 EC |
9990 | return 0; |
9991 | } | |
9992 | ||
9993 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
9994 | * and var_off. | |
9995 | */ | |
9996 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
9997 | struct bpf_insn *insn) | |
9998 | { | |
f4d7e40a AS |
9999 | struct bpf_verifier_state *vstate = env->cur_state; |
10000 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
10001 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
10002 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
10003 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 10004 | int err; |
f1174f77 EC |
10005 | |
10006 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
10007 | src_reg = NULL; |
10008 | if (dst_reg->type != SCALAR_VALUE) | |
10009 | ptr_reg = dst_reg; | |
75748837 AS |
10010 | else |
10011 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
10012 | * incorrectly propagated into other registers by find_equal_scalars() | |
10013 | */ | |
10014 | dst_reg->id = 0; | |
f1174f77 EC |
10015 | if (BPF_SRC(insn->code) == BPF_X) { |
10016 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
10017 | if (src_reg->type != SCALAR_VALUE) { |
10018 | if (dst_reg->type != SCALAR_VALUE) { | |
10019 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
10020 | * an arbitrary scalar. Disallow all math except |
10021 | * pointer subtraction | |
f1174f77 | 10022 | */ |
dd066823 | 10023 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
10024 | mark_reg_unknown(env, regs, insn->dst_reg); |
10025 | return 0; | |
f1174f77 | 10026 | } |
82abbf8d AS |
10027 | verbose(env, "R%d pointer %s pointer prohibited\n", |
10028 | insn->dst_reg, | |
10029 | bpf_alu_string[opcode >> 4]); | |
10030 | return -EACCES; | |
f1174f77 EC |
10031 | } else { |
10032 | /* scalar += pointer | |
10033 | * This is legal, but we have to reverse our | |
10034 | * src/dest handling in computing the range | |
10035 | */ | |
b5dc0163 AS |
10036 | err = mark_chain_precision(env, insn->dst_reg); |
10037 | if (err) | |
10038 | return err; | |
82abbf8d AS |
10039 | return adjust_ptr_min_max_vals(env, insn, |
10040 | src_reg, dst_reg); | |
f1174f77 EC |
10041 | } |
10042 | } else if (ptr_reg) { | |
10043 | /* pointer += scalar */ | |
b5dc0163 AS |
10044 | err = mark_chain_precision(env, insn->src_reg); |
10045 | if (err) | |
10046 | return err; | |
82abbf8d AS |
10047 | return adjust_ptr_min_max_vals(env, insn, |
10048 | dst_reg, src_reg); | |
a3b666bf AN |
10049 | } else if (dst_reg->precise) { |
10050 | /* if dst_reg is precise, src_reg should be precise as well */ | |
10051 | err = mark_chain_precision(env, insn->src_reg); | |
10052 | if (err) | |
10053 | return err; | |
f1174f77 EC |
10054 | } |
10055 | } else { | |
10056 | /* Pretend the src is a reg with a known value, since we only | |
10057 | * need to be able to read from this state. | |
10058 | */ | |
10059 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 10060 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 10061 | src_reg = &off_reg; |
82abbf8d AS |
10062 | if (ptr_reg) /* pointer += K */ |
10063 | return adjust_ptr_min_max_vals(env, insn, | |
10064 | ptr_reg, src_reg); | |
f1174f77 EC |
10065 | } |
10066 | ||
10067 | /* Got here implies adding two SCALAR_VALUEs */ | |
10068 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 10069 | print_verifier_state(env, state, true); |
61bd5218 | 10070 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
10071 | return -EINVAL; |
10072 | } | |
10073 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 10074 | print_verifier_state(env, state, true); |
61bd5218 | 10075 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
10076 | return -EINVAL; |
10077 | } | |
10078 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
10079 | } |
10080 | ||
17a52670 | 10081 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 10082 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 10083 | { |
638f5b90 | 10084 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
10085 | u8 opcode = BPF_OP(insn->code); |
10086 | int err; | |
10087 | ||
10088 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
10089 | if (opcode == BPF_NEG) { | |
395e942d | 10090 | if (BPF_SRC(insn->code) != BPF_K || |
17a52670 AS |
10091 | insn->src_reg != BPF_REG_0 || |
10092 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 10093 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
10094 | return -EINVAL; |
10095 | } | |
10096 | } else { | |
10097 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
10098 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
10099 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 10100 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
10101 | return -EINVAL; |
10102 | } | |
10103 | } | |
10104 | ||
10105 | /* check src operand */ | |
dc503a8a | 10106 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
10107 | if (err) |
10108 | return err; | |
10109 | ||
1be7f75d | 10110 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 10111 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
10112 | insn->dst_reg); |
10113 | return -EACCES; | |
10114 | } | |
10115 | ||
17a52670 | 10116 | /* check dest operand */ |
dc503a8a | 10117 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
10118 | if (err) |
10119 | return err; | |
10120 | ||
10121 | } else if (opcode == BPF_MOV) { | |
10122 | ||
10123 | if (BPF_SRC(insn->code) == BPF_X) { | |
10124 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 10125 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
10126 | return -EINVAL; |
10127 | } | |
10128 | ||
10129 | /* check src operand */ | |
dc503a8a | 10130 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
10131 | if (err) |
10132 | return err; | |
10133 | } else { | |
10134 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 10135 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
10136 | return -EINVAL; |
10137 | } | |
10138 | } | |
10139 | ||
fbeb1603 AF |
10140 | /* check dest operand, mark as required later */ |
10141 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
10142 | if (err) |
10143 | return err; | |
10144 | ||
10145 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
10146 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
10147 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
10148 | ||
17a52670 AS |
10149 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
10150 | /* case: R1 = R2 | |
10151 | * copy register state to dest reg | |
10152 | */ | |
75748837 AS |
10153 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
10154 | /* Assign src and dst registers the same ID | |
10155 | * that will be used by find_equal_scalars() | |
10156 | * to propagate min/max range. | |
10157 | */ | |
10158 | src_reg->id = ++env->id_gen; | |
e434b8cd JW |
10159 | *dst_reg = *src_reg; |
10160 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 10161 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 10162 | } else { |
f1174f77 | 10163 | /* R1 = (u32) R2 */ |
1be7f75d | 10164 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
10165 | verbose(env, |
10166 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
10167 | insn->src_reg); |
10168 | return -EACCES; | |
e434b8cd JW |
10169 | } else if (src_reg->type == SCALAR_VALUE) { |
10170 | *dst_reg = *src_reg; | |
75748837 AS |
10171 | /* Make sure ID is cleared otherwise |
10172 | * dst_reg min/max could be incorrectly | |
10173 | * propagated into src_reg by find_equal_scalars() | |
10174 | */ | |
10175 | dst_reg->id = 0; | |
e434b8cd | 10176 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 10177 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
10178 | } else { |
10179 | mark_reg_unknown(env, regs, | |
10180 | insn->dst_reg); | |
1be7f75d | 10181 | } |
3f50f132 | 10182 | zext_32_to_64(dst_reg); |
3844d153 | 10183 | reg_bounds_sync(dst_reg); |
17a52670 AS |
10184 | } |
10185 | } else { | |
10186 | /* case: R = imm | |
10187 | * remember the value we stored into this reg | |
10188 | */ | |
fbeb1603 AF |
10189 | /* clear any state __mark_reg_known doesn't set */ |
10190 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 10191 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
10192 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
10193 | __mark_reg_known(regs + insn->dst_reg, | |
10194 | insn->imm); | |
10195 | } else { | |
10196 | __mark_reg_known(regs + insn->dst_reg, | |
10197 | (u32)insn->imm); | |
10198 | } | |
17a52670 AS |
10199 | } |
10200 | ||
10201 | } else if (opcode > BPF_END) { | |
61bd5218 | 10202 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
10203 | return -EINVAL; |
10204 | ||
10205 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
10206 | ||
17a52670 AS |
10207 | if (BPF_SRC(insn->code) == BPF_X) { |
10208 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 10209 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
10210 | return -EINVAL; |
10211 | } | |
10212 | /* check src1 operand */ | |
dc503a8a | 10213 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
10214 | if (err) |
10215 | return err; | |
10216 | } else { | |
10217 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 10218 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
10219 | return -EINVAL; |
10220 | } | |
10221 | } | |
10222 | ||
10223 | /* check src2 operand */ | |
dc503a8a | 10224 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
10225 | if (err) |
10226 | return err; | |
10227 | ||
10228 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
10229 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 10230 | verbose(env, "div by zero\n"); |
17a52670 AS |
10231 | return -EINVAL; |
10232 | } | |
10233 | ||
229394e8 RV |
10234 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
10235 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
10236 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
10237 | ||
10238 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 10239 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
10240 | return -EINVAL; |
10241 | } | |
10242 | } | |
10243 | ||
1a0dc1ac | 10244 | /* check dest operand */ |
dc503a8a | 10245 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
10246 | if (err) |
10247 | return err; | |
10248 | ||
f1174f77 | 10249 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
10250 | } |
10251 | ||
10252 | return 0; | |
10253 | } | |
10254 | ||
f4d7e40a | 10255 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 10256 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 10257 | enum bpf_reg_type type, |
fb2a311a | 10258 | bool range_right_open) |
969bf05e | 10259 | { |
b239da34 KKD |
10260 | struct bpf_func_state *state; |
10261 | struct bpf_reg_state *reg; | |
10262 | int new_range; | |
2d2be8ca | 10263 | |
fb2a311a DB |
10264 | if (dst_reg->off < 0 || |
10265 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
10266 | /* This doesn't give us any range */ |
10267 | return; | |
10268 | ||
b03c9f9f EC |
10269 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
10270 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
10271 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
10272 | * than pkt_end, but that's because it's also less than pkt. | |
10273 | */ | |
10274 | return; | |
10275 | ||
fb2a311a DB |
10276 | new_range = dst_reg->off; |
10277 | if (range_right_open) | |
2fa7d94a | 10278 | new_range++; |
fb2a311a DB |
10279 | |
10280 | /* Examples for register markings: | |
2d2be8ca | 10281 | * |
fb2a311a | 10282 | * pkt_data in dst register: |
2d2be8ca DB |
10283 | * |
10284 | * r2 = r3; | |
10285 | * r2 += 8; | |
10286 | * if (r2 > pkt_end) goto <handle exception> | |
10287 | * <access okay> | |
10288 | * | |
b4e432f1 DB |
10289 | * r2 = r3; |
10290 | * r2 += 8; | |
10291 | * if (r2 < pkt_end) goto <access okay> | |
10292 | * <handle exception> | |
10293 | * | |
2d2be8ca DB |
10294 | * Where: |
10295 | * r2 == dst_reg, pkt_end == src_reg | |
10296 | * r2=pkt(id=n,off=8,r=0) | |
10297 | * r3=pkt(id=n,off=0,r=0) | |
10298 | * | |
fb2a311a | 10299 | * pkt_data in src register: |
2d2be8ca DB |
10300 | * |
10301 | * r2 = r3; | |
10302 | * r2 += 8; | |
10303 | * if (pkt_end >= r2) goto <access okay> | |
10304 | * <handle exception> | |
10305 | * | |
b4e432f1 DB |
10306 | * r2 = r3; |
10307 | * r2 += 8; | |
10308 | * if (pkt_end <= r2) goto <handle exception> | |
10309 | * <access okay> | |
10310 | * | |
2d2be8ca DB |
10311 | * Where: |
10312 | * pkt_end == dst_reg, r2 == src_reg | |
10313 | * r2=pkt(id=n,off=8,r=0) | |
10314 | * r3=pkt(id=n,off=0,r=0) | |
10315 | * | |
10316 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
10317 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
10318 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
10319 | * the check. | |
969bf05e | 10320 | */ |
2d2be8ca | 10321 | |
f1174f77 EC |
10322 | /* If our ids match, then we must have the same max_value. And we |
10323 | * don't care about the other reg's fixed offset, since if it's too big | |
10324 | * the range won't allow anything. | |
10325 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
10326 | */ | |
b239da34 KKD |
10327 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
10328 | if (reg->type == type && reg->id == dst_reg->id) | |
10329 | /* keep the maximum range already checked */ | |
10330 | reg->range = max(reg->range, new_range); | |
10331 | })); | |
969bf05e AS |
10332 | } |
10333 | ||
3f50f132 | 10334 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 10335 | { |
3f50f132 JF |
10336 | struct tnum subreg = tnum_subreg(reg->var_off); |
10337 | s32 sval = (s32)val; | |
a72dafaf | 10338 | |
3f50f132 JF |
10339 | switch (opcode) { |
10340 | case BPF_JEQ: | |
10341 | if (tnum_is_const(subreg)) | |
10342 | return !!tnum_equals_const(subreg, val); | |
10343 | break; | |
10344 | case BPF_JNE: | |
10345 | if (tnum_is_const(subreg)) | |
10346 | return !tnum_equals_const(subreg, val); | |
10347 | break; | |
10348 | case BPF_JSET: | |
10349 | if ((~subreg.mask & subreg.value) & val) | |
10350 | return 1; | |
10351 | if (!((subreg.mask | subreg.value) & val)) | |
10352 | return 0; | |
10353 | break; | |
10354 | case BPF_JGT: | |
10355 | if (reg->u32_min_value > val) | |
10356 | return 1; | |
10357 | else if (reg->u32_max_value <= val) | |
10358 | return 0; | |
10359 | break; | |
10360 | case BPF_JSGT: | |
10361 | if (reg->s32_min_value > sval) | |
10362 | return 1; | |
ee114dd6 | 10363 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
10364 | return 0; |
10365 | break; | |
10366 | case BPF_JLT: | |
10367 | if (reg->u32_max_value < val) | |
10368 | return 1; | |
10369 | else if (reg->u32_min_value >= val) | |
10370 | return 0; | |
10371 | break; | |
10372 | case BPF_JSLT: | |
10373 | if (reg->s32_max_value < sval) | |
10374 | return 1; | |
10375 | else if (reg->s32_min_value >= sval) | |
10376 | return 0; | |
10377 | break; | |
10378 | case BPF_JGE: | |
10379 | if (reg->u32_min_value >= val) | |
10380 | return 1; | |
10381 | else if (reg->u32_max_value < val) | |
10382 | return 0; | |
10383 | break; | |
10384 | case BPF_JSGE: | |
10385 | if (reg->s32_min_value >= sval) | |
10386 | return 1; | |
10387 | else if (reg->s32_max_value < sval) | |
10388 | return 0; | |
10389 | break; | |
10390 | case BPF_JLE: | |
10391 | if (reg->u32_max_value <= val) | |
10392 | return 1; | |
10393 | else if (reg->u32_min_value > val) | |
10394 | return 0; | |
10395 | break; | |
10396 | case BPF_JSLE: | |
10397 | if (reg->s32_max_value <= sval) | |
10398 | return 1; | |
10399 | else if (reg->s32_min_value > sval) | |
10400 | return 0; | |
10401 | break; | |
10402 | } | |
4f7b3e82 | 10403 | |
3f50f132 JF |
10404 | return -1; |
10405 | } | |
092ed096 | 10406 | |
3f50f132 JF |
10407 | |
10408 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
10409 | { | |
10410 | s64 sval = (s64)val; | |
a72dafaf | 10411 | |
4f7b3e82 AS |
10412 | switch (opcode) { |
10413 | case BPF_JEQ: | |
10414 | if (tnum_is_const(reg->var_off)) | |
10415 | return !!tnum_equals_const(reg->var_off, val); | |
10416 | break; | |
10417 | case BPF_JNE: | |
10418 | if (tnum_is_const(reg->var_off)) | |
10419 | return !tnum_equals_const(reg->var_off, val); | |
10420 | break; | |
960ea056 JK |
10421 | case BPF_JSET: |
10422 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
10423 | return 1; | |
10424 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
10425 | return 0; | |
10426 | break; | |
4f7b3e82 AS |
10427 | case BPF_JGT: |
10428 | if (reg->umin_value > val) | |
10429 | return 1; | |
10430 | else if (reg->umax_value <= val) | |
10431 | return 0; | |
10432 | break; | |
10433 | case BPF_JSGT: | |
a72dafaf | 10434 | if (reg->smin_value > sval) |
4f7b3e82 | 10435 | return 1; |
ee114dd6 | 10436 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
10437 | return 0; |
10438 | break; | |
10439 | case BPF_JLT: | |
10440 | if (reg->umax_value < val) | |
10441 | return 1; | |
10442 | else if (reg->umin_value >= val) | |
10443 | return 0; | |
10444 | break; | |
10445 | case BPF_JSLT: | |
a72dafaf | 10446 | if (reg->smax_value < sval) |
4f7b3e82 | 10447 | return 1; |
a72dafaf | 10448 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
10449 | return 0; |
10450 | break; | |
10451 | case BPF_JGE: | |
10452 | if (reg->umin_value >= val) | |
10453 | return 1; | |
10454 | else if (reg->umax_value < val) | |
10455 | return 0; | |
10456 | break; | |
10457 | case BPF_JSGE: | |
a72dafaf | 10458 | if (reg->smin_value >= sval) |
4f7b3e82 | 10459 | return 1; |
a72dafaf | 10460 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
10461 | return 0; |
10462 | break; | |
10463 | case BPF_JLE: | |
10464 | if (reg->umax_value <= val) | |
10465 | return 1; | |
10466 | else if (reg->umin_value > val) | |
10467 | return 0; | |
10468 | break; | |
10469 | case BPF_JSLE: | |
a72dafaf | 10470 | if (reg->smax_value <= sval) |
4f7b3e82 | 10471 | return 1; |
a72dafaf | 10472 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
10473 | return 0; |
10474 | break; | |
10475 | } | |
10476 | ||
10477 | return -1; | |
10478 | } | |
10479 | ||
3f50f132 JF |
10480 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
10481 | * and return: | |
10482 | * 1 - branch will be taken and "goto target" will be executed | |
10483 | * 0 - branch will not be taken and fall-through to next insn | |
10484 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
10485 | * range [0,10] | |
604dca5e | 10486 | */ |
3f50f132 JF |
10487 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
10488 | bool is_jmp32) | |
604dca5e | 10489 | { |
cac616db JF |
10490 | if (__is_pointer_value(false, reg)) { |
10491 | if (!reg_type_not_null(reg->type)) | |
10492 | return -1; | |
10493 | ||
10494 | /* If pointer is valid tests against zero will fail so we can | |
10495 | * use this to direct branch taken. | |
10496 | */ | |
10497 | if (val != 0) | |
10498 | return -1; | |
10499 | ||
10500 | switch (opcode) { | |
10501 | case BPF_JEQ: | |
10502 | return 0; | |
10503 | case BPF_JNE: | |
10504 | return 1; | |
10505 | default: | |
10506 | return -1; | |
10507 | } | |
10508 | } | |
604dca5e | 10509 | |
3f50f132 JF |
10510 | if (is_jmp32) |
10511 | return is_branch32_taken(reg, val, opcode); | |
10512 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
10513 | } |
10514 | ||
6d94e741 AS |
10515 | static int flip_opcode(u32 opcode) |
10516 | { | |
10517 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
10518 | static const u8 opcode_flip[16] = { | |
10519 | /* these stay the same */ | |
10520 | [BPF_JEQ >> 4] = BPF_JEQ, | |
10521 | [BPF_JNE >> 4] = BPF_JNE, | |
10522 | [BPF_JSET >> 4] = BPF_JSET, | |
10523 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
10524 | [BPF_JGE >> 4] = BPF_JLE, | |
10525 | [BPF_JGT >> 4] = BPF_JLT, | |
10526 | [BPF_JLE >> 4] = BPF_JGE, | |
10527 | [BPF_JLT >> 4] = BPF_JGT, | |
10528 | [BPF_JSGE >> 4] = BPF_JSLE, | |
10529 | [BPF_JSGT >> 4] = BPF_JSLT, | |
10530 | [BPF_JSLE >> 4] = BPF_JSGE, | |
10531 | [BPF_JSLT >> 4] = BPF_JSGT | |
10532 | }; | |
10533 | return opcode_flip[opcode >> 4]; | |
10534 | } | |
10535 | ||
10536 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
10537 | struct bpf_reg_state *src_reg, | |
10538 | u8 opcode) | |
10539 | { | |
10540 | struct bpf_reg_state *pkt; | |
10541 | ||
10542 | if (src_reg->type == PTR_TO_PACKET_END) { | |
10543 | pkt = dst_reg; | |
10544 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
10545 | pkt = src_reg; | |
10546 | opcode = flip_opcode(opcode); | |
10547 | } else { | |
10548 | return -1; | |
10549 | } | |
10550 | ||
10551 | if (pkt->range >= 0) | |
10552 | return -1; | |
10553 | ||
10554 | switch (opcode) { | |
10555 | case BPF_JLE: | |
10556 | /* pkt <= pkt_end */ | |
10557 | fallthrough; | |
10558 | case BPF_JGT: | |
10559 | /* pkt > pkt_end */ | |
10560 | if (pkt->range == BEYOND_PKT_END) | |
10561 | /* pkt has at last one extra byte beyond pkt_end */ | |
10562 | return opcode == BPF_JGT; | |
10563 | break; | |
10564 | case BPF_JLT: | |
10565 | /* pkt < pkt_end */ | |
10566 | fallthrough; | |
10567 | case BPF_JGE: | |
10568 | /* pkt >= pkt_end */ | |
10569 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
10570 | return opcode == BPF_JGE; | |
10571 | break; | |
10572 | } | |
10573 | return -1; | |
10574 | } | |
10575 | ||
48461135 JB |
10576 | /* Adjusts the register min/max values in the case that the dst_reg is the |
10577 | * variable register that we are working on, and src_reg is a constant or we're | |
10578 | * simply doing a BPF_K check. | |
f1174f77 | 10579 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
10580 | */ |
10581 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
10582 | struct bpf_reg_state *false_reg, |
10583 | u64 val, u32 val32, | |
092ed096 | 10584 | u8 opcode, bool is_jmp32) |
48461135 | 10585 | { |
3f50f132 JF |
10586 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
10587 | struct tnum false_64off = false_reg->var_off; | |
10588 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
10589 | struct tnum true_64off = true_reg->var_off; | |
10590 | s64 sval = (s64)val; | |
10591 | s32 sval32 = (s32)val32; | |
a72dafaf | 10592 | |
f1174f77 EC |
10593 | /* If the dst_reg is a pointer, we can't learn anything about its |
10594 | * variable offset from the compare (unless src_reg were a pointer into | |
10595 | * the same object, but we don't bother with that. | |
10596 | * Since false_reg and true_reg have the same type by construction, we | |
10597 | * only need to check one of them for pointerness. | |
10598 | */ | |
10599 | if (__is_pointer_value(false, false_reg)) | |
10600 | return; | |
4cabc5b1 | 10601 | |
48461135 | 10602 | switch (opcode) { |
a12ca627 DB |
10603 | /* JEQ/JNE comparison doesn't change the register equivalence. |
10604 | * | |
10605 | * r1 = r2; | |
10606 | * if (r1 == 42) goto label; | |
10607 | * ... | |
10608 | * label: // here both r1 and r2 are known to be 42. | |
10609 | * | |
10610 | * Hence when marking register as known preserve it's ID. | |
10611 | */ | |
48461135 | 10612 | case BPF_JEQ: |
a12ca627 DB |
10613 | if (is_jmp32) { |
10614 | __mark_reg32_known(true_reg, val32); | |
10615 | true_32off = tnum_subreg(true_reg->var_off); | |
10616 | } else { | |
10617 | ___mark_reg_known(true_reg, val); | |
10618 | true_64off = true_reg->var_off; | |
10619 | } | |
10620 | break; | |
48461135 | 10621 | case BPF_JNE: |
a12ca627 DB |
10622 | if (is_jmp32) { |
10623 | __mark_reg32_known(false_reg, val32); | |
10624 | false_32off = tnum_subreg(false_reg->var_off); | |
10625 | } else { | |
10626 | ___mark_reg_known(false_reg, val); | |
10627 | false_64off = false_reg->var_off; | |
10628 | } | |
48461135 | 10629 | break; |
960ea056 | 10630 | case BPF_JSET: |
3f50f132 JF |
10631 | if (is_jmp32) { |
10632 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
10633 | if (is_power_of_2(val32)) | |
10634 | true_32off = tnum_or(true_32off, | |
10635 | tnum_const(val32)); | |
10636 | } else { | |
10637 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
10638 | if (is_power_of_2(val)) | |
10639 | true_64off = tnum_or(true_64off, | |
10640 | tnum_const(val)); | |
10641 | } | |
960ea056 | 10642 | break; |
48461135 | 10643 | case BPF_JGE: |
a72dafaf JW |
10644 | case BPF_JGT: |
10645 | { | |
3f50f132 JF |
10646 | if (is_jmp32) { |
10647 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
10648 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
10649 | ||
10650 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
10651 | false_umax); | |
10652 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
10653 | true_umin); | |
10654 | } else { | |
10655 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
10656 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
10657 | ||
10658 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
10659 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
10660 | } | |
b03c9f9f | 10661 | break; |
a72dafaf | 10662 | } |
48461135 | 10663 | case BPF_JSGE: |
a72dafaf JW |
10664 | case BPF_JSGT: |
10665 | { | |
3f50f132 JF |
10666 | if (is_jmp32) { |
10667 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
10668 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 10669 | |
3f50f132 JF |
10670 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
10671 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
10672 | } else { | |
10673 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
10674 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
10675 | ||
10676 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
10677 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
10678 | } | |
48461135 | 10679 | break; |
a72dafaf | 10680 | } |
b4e432f1 | 10681 | case BPF_JLE: |
a72dafaf JW |
10682 | case BPF_JLT: |
10683 | { | |
3f50f132 JF |
10684 | if (is_jmp32) { |
10685 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
10686 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
10687 | ||
10688 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
10689 | false_umin); | |
10690 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
10691 | true_umax); | |
10692 | } else { | |
10693 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
10694 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
10695 | ||
10696 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
10697 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
10698 | } | |
b4e432f1 | 10699 | break; |
a72dafaf | 10700 | } |
b4e432f1 | 10701 | case BPF_JSLE: |
a72dafaf JW |
10702 | case BPF_JSLT: |
10703 | { | |
3f50f132 JF |
10704 | if (is_jmp32) { |
10705 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
10706 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 10707 | |
3f50f132 JF |
10708 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
10709 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
10710 | } else { | |
10711 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
10712 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
10713 | ||
10714 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
10715 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
10716 | } | |
b4e432f1 | 10717 | break; |
a72dafaf | 10718 | } |
48461135 | 10719 | default: |
0fc31b10 | 10720 | return; |
48461135 JB |
10721 | } |
10722 | ||
3f50f132 JF |
10723 | if (is_jmp32) { |
10724 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
10725 | tnum_subreg(false_32off)); | |
10726 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
10727 | tnum_subreg(true_32off)); | |
10728 | __reg_combine_32_into_64(false_reg); | |
10729 | __reg_combine_32_into_64(true_reg); | |
10730 | } else { | |
10731 | false_reg->var_off = false_64off; | |
10732 | true_reg->var_off = true_64off; | |
10733 | __reg_combine_64_into_32(false_reg); | |
10734 | __reg_combine_64_into_32(true_reg); | |
10735 | } | |
48461135 JB |
10736 | } |
10737 | ||
f1174f77 EC |
10738 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
10739 | * the variable reg. | |
48461135 JB |
10740 | */ |
10741 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
10742 | struct bpf_reg_state *false_reg, |
10743 | u64 val, u32 val32, | |
092ed096 | 10744 | u8 opcode, bool is_jmp32) |
48461135 | 10745 | { |
6d94e741 | 10746 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
10747 | /* This uses zero as "not present in table"; luckily the zero opcode, |
10748 | * BPF_JA, can't get here. | |
b03c9f9f | 10749 | */ |
0fc31b10 | 10750 | if (opcode) |
3f50f132 | 10751 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
10752 | } |
10753 | ||
10754 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
10755 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
10756 | struct bpf_reg_state *dst_reg) | |
10757 | { | |
b03c9f9f EC |
10758 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
10759 | dst_reg->umin_value); | |
10760 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
10761 | dst_reg->umax_value); | |
10762 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
10763 | dst_reg->smin_value); | |
10764 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
10765 | dst_reg->smax_value); | |
f1174f77 EC |
10766 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
10767 | dst_reg->var_off); | |
3844d153 DB |
10768 | reg_bounds_sync(src_reg); |
10769 | reg_bounds_sync(dst_reg); | |
f1174f77 EC |
10770 | } |
10771 | ||
10772 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
10773 | struct bpf_reg_state *true_dst, | |
10774 | struct bpf_reg_state *false_src, | |
10775 | struct bpf_reg_state *false_dst, | |
10776 | u8 opcode) | |
10777 | { | |
10778 | switch (opcode) { | |
10779 | case BPF_JEQ: | |
10780 | __reg_combine_min_max(true_src, true_dst); | |
10781 | break; | |
10782 | case BPF_JNE: | |
10783 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 10784 | break; |
4cabc5b1 | 10785 | } |
48461135 JB |
10786 | } |
10787 | ||
fd978bf7 JS |
10788 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
10789 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 10790 | bool is_null) |
57a09bf0 | 10791 | { |
c25b2ae1 | 10792 | if (type_may_be_null(reg->type) && reg->id == id && |
93c230e3 | 10793 | !WARN_ON_ONCE(!reg->id)) { |
b03c9f9f EC |
10794 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
10795 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 10796 | reg->off)) { |
e60b0d12 DB |
10797 | /* Old offset (both fixed and variable parts) should |
10798 | * have been known-zero, because we don't allow pointer | |
10799 | * arithmetic on pointers that might be NULL. If we | |
10800 | * see this happening, don't convert the register. | |
10801 | */ | |
10802 | return; | |
f1174f77 EC |
10803 | } |
10804 | if (is_null) { | |
10805 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
10806 | /* We don't need id and ref_obj_id from this point |
10807 | * onwards anymore, thus we should better reset it, | |
10808 | * so that state pruning has chances to take effect. | |
10809 | */ | |
10810 | reg->id = 0; | |
10811 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
10812 | |
10813 | return; | |
10814 | } | |
10815 | ||
10816 | mark_ptr_not_null_reg(reg); | |
10817 | ||
10818 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 | 10819 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
b239da34 | 10820 | * in release_reference(). |
1b986589 MKL |
10821 | * |
10822 | * reg->id is still used by spin_lock ptr. Other | |
10823 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
10824 | */ |
10825 | reg->id = 0; | |
56f668df | 10826 | } |
57a09bf0 TG |
10827 | } |
10828 | } | |
10829 | ||
10830 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
10831 | * be folded together at some point. | |
10832 | */ | |
840b9615 JS |
10833 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
10834 | bool is_null) | |
57a09bf0 | 10835 | { |
f4d7e40a | 10836 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
b239da34 | 10837 | struct bpf_reg_state *regs = state->regs, *reg; |
1b986589 | 10838 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 10839 | u32 id = regs[regno].id; |
57a09bf0 | 10840 | |
1b986589 MKL |
10841 | if (ref_obj_id && ref_obj_id == id && is_null) |
10842 | /* regs[regno] is in the " == NULL" branch. | |
10843 | * No one could have freed the reference state before | |
10844 | * doing the NULL check. | |
10845 | */ | |
10846 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 10847 | |
b239da34 KKD |
10848 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
10849 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
10850 | })); | |
57a09bf0 TG |
10851 | } |
10852 | ||
5beca081 DB |
10853 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
10854 | struct bpf_reg_state *dst_reg, | |
10855 | struct bpf_reg_state *src_reg, | |
10856 | struct bpf_verifier_state *this_branch, | |
10857 | struct bpf_verifier_state *other_branch) | |
10858 | { | |
10859 | if (BPF_SRC(insn->code) != BPF_X) | |
10860 | return false; | |
10861 | ||
092ed096 JW |
10862 | /* Pointers are always 64-bit. */ |
10863 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
10864 | return false; | |
10865 | ||
5beca081 DB |
10866 | switch (BPF_OP(insn->code)) { |
10867 | case BPF_JGT: | |
10868 | if ((dst_reg->type == PTR_TO_PACKET && | |
10869 | src_reg->type == PTR_TO_PACKET_END) || | |
10870 | (dst_reg->type == PTR_TO_PACKET_META && | |
10871 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
10872 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
10873 | find_good_pkt_pointers(this_branch, dst_reg, | |
10874 | dst_reg->type, false); | |
6d94e741 | 10875 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
10876 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
10877 | src_reg->type == PTR_TO_PACKET) || | |
10878 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
10879 | src_reg->type == PTR_TO_PACKET_META)) { | |
10880 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
10881 | find_good_pkt_pointers(other_branch, src_reg, | |
10882 | src_reg->type, true); | |
6d94e741 | 10883 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
10884 | } else { |
10885 | return false; | |
10886 | } | |
10887 | break; | |
10888 | case BPF_JLT: | |
10889 | if ((dst_reg->type == PTR_TO_PACKET && | |
10890 | src_reg->type == PTR_TO_PACKET_END) || | |
10891 | (dst_reg->type == PTR_TO_PACKET_META && | |
10892 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
10893 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
10894 | find_good_pkt_pointers(other_branch, dst_reg, | |
10895 | dst_reg->type, true); | |
6d94e741 | 10896 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
10897 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
10898 | src_reg->type == PTR_TO_PACKET) || | |
10899 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
10900 | src_reg->type == PTR_TO_PACKET_META)) { | |
10901 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
10902 | find_good_pkt_pointers(this_branch, src_reg, | |
10903 | src_reg->type, false); | |
6d94e741 | 10904 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
10905 | } else { |
10906 | return false; | |
10907 | } | |
10908 | break; | |
10909 | case BPF_JGE: | |
10910 | if ((dst_reg->type == PTR_TO_PACKET && | |
10911 | src_reg->type == PTR_TO_PACKET_END) || | |
10912 | (dst_reg->type == PTR_TO_PACKET_META && | |
10913 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
10914 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
10915 | find_good_pkt_pointers(this_branch, dst_reg, | |
10916 | dst_reg->type, true); | |
6d94e741 | 10917 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
10918 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
10919 | src_reg->type == PTR_TO_PACKET) || | |
10920 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
10921 | src_reg->type == PTR_TO_PACKET_META)) { | |
10922 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
10923 | find_good_pkt_pointers(other_branch, src_reg, | |
10924 | src_reg->type, false); | |
6d94e741 | 10925 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
10926 | } else { |
10927 | return false; | |
10928 | } | |
10929 | break; | |
10930 | case BPF_JLE: | |
10931 | if ((dst_reg->type == PTR_TO_PACKET && | |
10932 | src_reg->type == PTR_TO_PACKET_END) || | |
10933 | (dst_reg->type == PTR_TO_PACKET_META && | |
10934 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
10935 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
10936 | find_good_pkt_pointers(other_branch, dst_reg, | |
10937 | dst_reg->type, false); | |
6d94e741 | 10938 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
10939 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
10940 | src_reg->type == PTR_TO_PACKET) || | |
10941 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
10942 | src_reg->type == PTR_TO_PACKET_META)) { | |
10943 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
10944 | find_good_pkt_pointers(this_branch, src_reg, | |
10945 | src_reg->type, true); | |
6d94e741 | 10946 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
10947 | } else { |
10948 | return false; | |
10949 | } | |
10950 | break; | |
10951 | default: | |
10952 | return false; | |
10953 | } | |
10954 | ||
10955 | return true; | |
10956 | } | |
10957 | ||
75748837 AS |
10958 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
10959 | struct bpf_reg_state *known_reg) | |
10960 | { | |
10961 | struct bpf_func_state *state; | |
10962 | struct bpf_reg_state *reg; | |
75748837 | 10963 | |
b239da34 KKD |
10964 | bpf_for_each_reg_in_vstate(vstate, state, reg, ({ |
10965 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
10966 | *reg = *known_reg; | |
10967 | })); | |
75748837 AS |
10968 | } |
10969 | ||
58e2af8b | 10970 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
10971 | struct bpf_insn *insn, int *insn_idx) |
10972 | { | |
f4d7e40a AS |
10973 | struct bpf_verifier_state *this_branch = env->cur_state; |
10974 | struct bpf_verifier_state *other_branch; | |
10975 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 10976 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
befae758 | 10977 | struct bpf_reg_state *eq_branch_regs; |
17a52670 | 10978 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 10979 | bool is_jmp32; |
fb8d251e | 10980 | int pred = -1; |
17a52670 AS |
10981 | int err; |
10982 | ||
092ed096 JW |
10983 | /* Only conditional jumps are expected to reach here. */ |
10984 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
10985 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
10986 | return -EINVAL; |
10987 | } | |
10988 | ||
10989 | if (BPF_SRC(insn->code) == BPF_X) { | |
10990 | if (insn->imm != 0) { | |
092ed096 | 10991 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
10992 | return -EINVAL; |
10993 | } | |
10994 | ||
10995 | /* check src1 operand */ | |
dc503a8a | 10996 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
10997 | if (err) |
10998 | return err; | |
1be7f75d AS |
10999 | |
11000 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 11001 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
11002 | insn->src_reg); |
11003 | return -EACCES; | |
11004 | } | |
fb8d251e | 11005 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
11006 | } else { |
11007 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 11008 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
11009 | return -EINVAL; |
11010 | } | |
11011 | } | |
11012 | ||
11013 | /* check src2 operand */ | |
dc503a8a | 11014 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11015 | if (err) |
11016 | return err; | |
11017 | ||
1a0dc1ac | 11018 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 11019 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 11020 | |
3f50f132 JF |
11021 | if (BPF_SRC(insn->code) == BPF_K) { |
11022 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
11023 | } else if (src_reg->type == SCALAR_VALUE && | |
11024 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
11025 | pred = is_branch_taken(dst_reg, | |
11026 | tnum_subreg(src_reg->var_off).value, | |
11027 | opcode, | |
11028 | is_jmp32); | |
11029 | } else if (src_reg->type == SCALAR_VALUE && | |
11030 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
11031 | pred = is_branch_taken(dst_reg, | |
11032 | src_reg->var_off.value, | |
11033 | opcode, | |
11034 | is_jmp32); | |
6d94e741 AS |
11035 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
11036 | reg_is_pkt_pointer_any(src_reg) && | |
11037 | !is_jmp32) { | |
11038 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
11039 | } |
11040 | ||
b5dc0163 | 11041 | if (pred >= 0) { |
cac616db JF |
11042 | /* If we get here with a dst_reg pointer type it is because |
11043 | * above is_branch_taken() special cased the 0 comparison. | |
11044 | */ | |
11045 | if (!__is_pointer_value(false, dst_reg)) | |
11046 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
11047 | if (BPF_SRC(insn->code) == BPF_X && !err && |
11048 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
11049 | err = mark_chain_precision(env, insn->src_reg); |
11050 | if (err) | |
11051 | return err; | |
11052 | } | |
9183671a | 11053 | |
fb8d251e | 11054 | if (pred == 1) { |
9183671a DB |
11055 | /* Only follow the goto, ignore fall-through. If needed, push |
11056 | * the fall-through branch for simulation under speculative | |
11057 | * execution. | |
11058 | */ | |
11059 | if (!env->bypass_spec_v1 && | |
11060 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
11061 | *insn_idx)) | |
11062 | return -EFAULT; | |
fb8d251e AS |
11063 | *insn_idx += insn->off; |
11064 | return 0; | |
11065 | } else if (pred == 0) { | |
9183671a DB |
11066 | /* Only follow the fall-through branch, since that's where the |
11067 | * program will go. If needed, push the goto branch for | |
11068 | * simulation under speculative execution. | |
fb8d251e | 11069 | */ |
9183671a DB |
11070 | if (!env->bypass_spec_v1 && |
11071 | !sanitize_speculative_path(env, insn, | |
11072 | *insn_idx + insn->off + 1, | |
11073 | *insn_idx)) | |
11074 | return -EFAULT; | |
fb8d251e | 11075 | return 0; |
17a52670 AS |
11076 | } |
11077 | ||
979d63d5 DB |
11078 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
11079 | false); | |
17a52670 AS |
11080 | if (!other_branch) |
11081 | return -EFAULT; | |
f4d7e40a | 11082 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 11083 | |
48461135 JB |
11084 | /* detect if we are comparing against a constant value so we can adjust |
11085 | * our min/max values for our dst register. | |
f1174f77 | 11086 | * this is only legit if both are scalars (or pointers to the same |
befae758 EZ |
11087 | * object, I suppose, see the PTR_MAYBE_NULL related if block below), |
11088 | * because otherwise the different base pointers mean the offsets aren't | |
f1174f77 | 11089 | * comparable. |
48461135 JB |
11090 | */ |
11091 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 11092 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 11093 | |
f1174f77 | 11094 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
11095 | src_reg->type == SCALAR_VALUE) { |
11096 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
11097 | (is_jmp32 && |
11098 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 11099 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 11100 | dst_reg, |
3f50f132 JF |
11101 | src_reg->var_off.value, |
11102 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
11103 | opcode, is_jmp32); |
11104 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
11105 | (is_jmp32 && |
11106 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 11107 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 11108 | src_reg, |
3f50f132 JF |
11109 | dst_reg->var_off.value, |
11110 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
11111 | opcode, is_jmp32); |
11112 | else if (!is_jmp32 && | |
11113 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 11114 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
11115 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
11116 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 11117 | src_reg, dst_reg, opcode); |
e688c3db AS |
11118 | if (src_reg->id && |
11119 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
11120 | find_equal_scalars(this_branch, src_reg); |
11121 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
11122 | } | |
11123 | ||
f1174f77 EC |
11124 | } |
11125 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 11126 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
11127 | dst_reg, insn->imm, (u32)insn->imm, |
11128 | opcode, is_jmp32); | |
48461135 JB |
11129 | } |
11130 | ||
e688c3db AS |
11131 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
11132 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
11133 | find_equal_scalars(this_branch, dst_reg); |
11134 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
11135 | } | |
11136 | ||
befae758 EZ |
11137 | /* if one pointer register is compared to another pointer |
11138 | * register check if PTR_MAYBE_NULL could be lifted. | |
11139 | * E.g. register A - maybe null | |
11140 | * register B - not null | |
11141 | * for JNE A, B, ... - A is not null in the false branch; | |
11142 | * for JEQ A, B, ... - A is not null in the true branch. | |
11143 | */ | |
11144 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_X && | |
11145 | __is_pointer_value(false, src_reg) && __is_pointer_value(false, dst_reg) && | |
11146 | type_may_be_null(src_reg->type) != type_may_be_null(dst_reg->type)) { | |
11147 | eq_branch_regs = NULL; | |
11148 | switch (opcode) { | |
11149 | case BPF_JEQ: | |
11150 | eq_branch_regs = other_branch_regs; | |
11151 | break; | |
11152 | case BPF_JNE: | |
11153 | eq_branch_regs = regs; | |
11154 | break; | |
11155 | default: | |
11156 | /* do nothing */ | |
11157 | break; | |
11158 | } | |
11159 | if (eq_branch_regs) { | |
11160 | if (type_may_be_null(src_reg->type)) | |
11161 | mark_ptr_not_null_reg(&eq_branch_regs[insn->src_reg]); | |
11162 | else | |
11163 | mark_ptr_not_null_reg(&eq_branch_regs[insn->dst_reg]); | |
11164 | } | |
11165 | } | |
11166 | ||
092ed096 JW |
11167 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
11168 | * NOTE: these optimizations below are related with pointer comparison | |
11169 | * which will never be JMP32. | |
11170 | */ | |
11171 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 11172 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
c25b2ae1 | 11173 | type_may_be_null(dst_reg->type)) { |
840b9615 | 11174 | /* Mark all identical registers in each branch as either |
57a09bf0 TG |
11175 | * safe or unknown depending R == 0 or R != 0 conditional. |
11176 | */ | |
840b9615 JS |
11177 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
11178 | opcode == BPF_JNE); | |
11179 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
11180 | opcode == BPF_JEQ); | |
5beca081 DB |
11181 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
11182 | this_branch, other_branch) && | |
11183 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
11184 | verbose(env, "R%d pointer comparison prohibited\n", |
11185 | insn->dst_reg); | |
1be7f75d | 11186 | return -EACCES; |
17a52670 | 11187 | } |
06ee7115 | 11188 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 11189 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
11190 | return 0; |
11191 | } | |
11192 | ||
17a52670 | 11193 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 11194 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 11195 | { |
d8eca5bb | 11196 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 11197 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 11198 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 11199 | struct bpf_map *map; |
17a52670 AS |
11200 | int err; |
11201 | ||
11202 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 11203 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
11204 | return -EINVAL; |
11205 | } | |
11206 | if (insn->off != 0) { | |
61bd5218 | 11207 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
11208 | return -EINVAL; |
11209 | } | |
11210 | ||
dc503a8a | 11211 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
11212 | if (err) |
11213 | return err; | |
11214 | ||
4976b718 | 11215 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 11216 | if (insn->src_reg == 0) { |
6b173873 JK |
11217 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
11218 | ||
4976b718 | 11219 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 11220 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 11221 | return 0; |
6b173873 | 11222 | } |
17a52670 | 11223 | |
d400a6cf DB |
11224 | /* All special src_reg cases are listed below. From this point onwards |
11225 | * we either succeed and assign a corresponding dst_reg->type after | |
11226 | * zeroing the offset, or fail and reject the program. | |
11227 | */ | |
11228 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 11229 | |
d400a6cf | 11230 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
4976b718 | 11231 | dst_reg->type = aux->btf_var.reg_type; |
34d3a78c | 11232 | switch (base_type(dst_reg->type)) { |
4976b718 HL |
11233 | case PTR_TO_MEM: |
11234 | dst_reg->mem_size = aux->btf_var.mem_size; | |
11235 | break; | |
11236 | case PTR_TO_BTF_ID: | |
22dc4a0f | 11237 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
11238 | dst_reg->btf_id = aux->btf_var.btf_id; |
11239 | break; | |
11240 | default: | |
11241 | verbose(env, "bpf verifier is misconfigured\n"); | |
11242 | return -EFAULT; | |
11243 | } | |
11244 | return 0; | |
11245 | } | |
11246 | ||
69c087ba YS |
11247 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
11248 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
11249 | u32 subprogno = find_subprog(env, |
11250 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
11251 | |
11252 | if (!aux->func_info) { | |
11253 | verbose(env, "missing btf func_info\n"); | |
11254 | return -EINVAL; | |
11255 | } | |
11256 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
11257 | verbose(env, "callback function not static\n"); | |
11258 | return -EINVAL; | |
11259 | } | |
11260 | ||
11261 | dst_reg->type = PTR_TO_FUNC; | |
11262 | dst_reg->subprogno = subprogno; | |
11263 | return 0; | |
11264 | } | |
11265 | ||
d8eca5bb | 11266 | map = env->used_maps[aux->map_index]; |
4976b718 | 11267 | dst_reg->map_ptr = map; |
d8eca5bb | 11268 | |
387544bf AS |
11269 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
11270 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
11271 | dst_reg->type = PTR_TO_MAP_VALUE; |
11272 | dst_reg->off = aux->map_off; | |
d0d78c1d KKD |
11273 | WARN_ON_ONCE(map->max_entries != 1); |
11274 | /* We want reg->id to be same (0) as map_value is not distinct */ | |
387544bf AS |
11275 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
11276 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 11277 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
11278 | } else { |
11279 | verbose(env, "bpf verifier is misconfigured\n"); | |
11280 | return -EINVAL; | |
11281 | } | |
17a52670 | 11282 | |
17a52670 AS |
11283 | return 0; |
11284 | } | |
11285 | ||
96be4325 DB |
11286 | static bool may_access_skb(enum bpf_prog_type type) |
11287 | { | |
11288 | switch (type) { | |
11289 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
11290 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 11291 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
11292 | return true; |
11293 | default: | |
11294 | return false; | |
11295 | } | |
11296 | } | |
11297 | ||
ddd872bc AS |
11298 | /* verify safety of LD_ABS|LD_IND instructions: |
11299 | * - they can only appear in the programs where ctx == skb | |
11300 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
11301 | * preserve R6-R9, and store return value into R0 | |
11302 | * | |
11303 | * Implicit input: | |
11304 | * ctx == skb == R6 == CTX | |
11305 | * | |
11306 | * Explicit input: | |
11307 | * SRC == any register | |
11308 | * IMM == 32-bit immediate | |
11309 | * | |
11310 | * Output: | |
11311 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
11312 | */ | |
58e2af8b | 11313 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 11314 | { |
638f5b90 | 11315 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 11316 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 11317 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
11318 | int i, err; |
11319 | ||
7e40781c | 11320 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 11321 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
11322 | return -EINVAL; |
11323 | } | |
11324 | ||
e0cea7ce DB |
11325 | if (!env->ops->gen_ld_abs) { |
11326 | verbose(env, "bpf verifier is misconfigured\n"); | |
11327 | return -EINVAL; | |
11328 | } | |
11329 | ||
ddd872bc | 11330 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 11331 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 11332 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 11333 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
11334 | return -EINVAL; |
11335 | } | |
11336 | ||
11337 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 11338 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
11339 | if (err) |
11340 | return err; | |
11341 | ||
fd978bf7 JS |
11342 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
11343 | * gen_ld_abs() may terminate the program at runtime, leading to | |
11344 | * reference leak. | |
11345 | */ | |
11346 | err = check_reference_leak(env); | |
11347 | if (err) { | |
11348 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
11349 | return err; | |
11350 | } | |
11351 | ||
d0d78c1d | 11352 | if (env->cur_state->active_lock.ptr) { |
d83525ca AS |
11353 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
11354 | return -EINVAL; | |
11355 | } | |
11356 | ||
6d4f151a | 11357 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
11358 | verbose(env, |
11359 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
11360 | return -EINVAL; |
11361 | } | |
11362 | ||
11363 | if (mode == BPF_IND) { | |
11364 | /* check explicit source operand */ | |
dc503a8a | 11365 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
11366 | if (err) |
11367 | return err; | |
11368 | } | |
11369 | ||
be80a1d3 | 11370 | err = check_ptr_off_reg(env, ®s[ctx_reg], ctx_reg); |
6d4f151a DB |
11371 | if (err < 0) |
11372 | return err; | |
11373 | ||
ddd872bc | 11374 | /* reset caller saved regs to unreadable */ |
dc503a8a | 11375 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 11376 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
11377 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
11378 | } | |
ddd872bc AS |
11379 | |
11380 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
11381 | * the value fetched from the packet. |
11382 | * Already marked as written above. | |
ddd872bc | 11383 | */ |
61bd5218 | 11384 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
11385 | /* ld_abs load up to 32-bit skb data. */ |
11386 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
11387 | return 0; |
11388 | } | |
11389 | ||
390ee7e2 AS |
11390 | static int check_return_code(struct bpf_verifier_env *env) |
11391 | { | |
5cf1e914 | 11392 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 11393 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
11394 | struct bpf_reg_state *reg; |
11395 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 11396 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 11397 | int err; |
bfc6bb74 AS |
11398 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
11399 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 11400 | |
9e4e01df | 11401 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
d1a6edec SF |
11402 | if (!is_subprog) { |
11403 | switch (prog_type) { | |
11404 | case BPF_PROG_TYPE_LSM: | |
11405 | if (prog->expected_attach_type == BPF_LSM_CGROUP) | |
11406 | /* See below, can be 0 or 0-1 depending on hook. */ | |
11407 | break; | |
11408 | fallthrough; | |
11409 | case BPF_PROG_TYPE_STRUCT_OPS: | |
11410 | if (!prog->aux->attach_func_proto->type) | |
11411 | return 0; | |
11412 | break; | |
11413 | default: | |
11414 | break; | |
11415 | } | |
11416 | } | |
27ae7997 | 11417 | |
8fb33b60 | 11418 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
11419 | * to return the value from eBPF program. |
11420 | * Make sure that it's readable at this time | |
11421 | * of bpf_exit, which means that program wrote | |
11422 | * something into it earlier | |
11423 | */ | |
11424 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
11425 | if (err) | |
11426 | return err; | |
11427 | ||
11428 | if (is_pointer_value(env, BPF_REG_0)) { | |
11429 | verbose(env, "R0 leaks addr as return value\n"); | |
11430 | return -EACCES; | |
11431 | } | |
390ee7e2 | 11432 | |
f782e2c3 | 11433 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
11434 | |
11435 | if (frame->in_async_callback_fn) { | |
11436 | /* enforce return zero from async callbacks like timer */ | |
11437 | if (reg->type != SCALAR_VALUE) { | |
11438 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
c25b2ae1 | 11439 | reg_type_str(env, reg->type)); |
bfc6bb74 AS |
11440 | return -EINVAL; |
11441 | } | |
11442 | ||
11443 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
11444 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
11445 | return -EINVAL; | |
11446 | } | |
11447 | return 0; | |
11448 | } | |
11449 | ||
f782e2c3 DB |
11450 | if (is_subprog) { |
11451 | if (reg->type != SCALAR_VALUE) { | |
11452 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
c25b2ae1 | 11453 | reg_type_str(env, reg->type)); |
f782e2c3 DB |
11454 | return -EINVAL; |
11455 | } | |
11456 | return 0; | |
11457 | } | |
11458 | ||
7e40781c | 11459 | switch (prog_type) { |
983695fa DB |
11460 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
11461 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
11462 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
11463 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
11464 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
11465 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
11466 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 11467 | range = tnum_range(1, 1); |
77241217 SF |
11468 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
11469 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
11470 | range = tnum_range(0, 3); | |
ed4ed404 | 11471 | break; |
390ee7e2 | 11472 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 11473 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
11474 | range = tnum_range(0, 3); | |
11475 | enforce_attach_type_range = tnum_range(2, 3); | |
11476 | } | |
ed4ed404 | 11477 | break; |
390ee7e2 AS |
11478 | case BPF_PROG_TYPE_CGROUP_SOCK: |
11479 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 11480 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 11481 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 11482 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 11483 | break; |
15ab09bd AS |
11484 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
11485 | if (!env->prog->aux->attach_btf_id) | |
11486 | return 0; | |
11487 | range = tnum_const(0); | |
11488 | break; | |
15d83c4d | 11489 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
11490 | switch (env->prog->expected_attach_type) { |
11491 | case BPF_TRACE_FENTRY: | |
11492 | case BPF_TRACE_FEXIT: | |
11493 | range = tnum_const(0); | |
11494 | break; | |
11495 | case BPF_TRACE_RAW_TP: | |
11496 | case BPF_MODIFY_RETURN: | |
15d83c4d | 11497 | return 0; |
2ec0616e DB |
11498 | case BPF_TRACE_ITER: |
11499 | break; | |
e92888c7 YS |
11500 | default: |
11501 | return -ENOTSUPP; | |
11502 | } | |
15d83c4d | 11503 | break; |
e9ddbb77 JS |
11504 | case BPF_PROG_TYPE_SK_LOOKUP: |
11505 | range = tnum_range(SK_DROP, SK_PASS); | |
11506 | break; | |
69fd337a SF |
11507 | |
11508 | case BPF_PROG_TYPE_LSM: | |
11509 | if (env->prog->expected_attach_type != BPF_LSM_CGROUP) { | |
11510 | /* Regular BPF_PROG_TYPE_LSM programs can return | |
11511 | * any value. | |
11512 | */ | |
11513 | return 0; | |
11514 | } | |
11515 | if (!env->prog->aux->attach_func_proto->type) { | |
11516 | /* Make sure programs that attach to void | |
11517 | * hooks don't try to modify return value. | |
11518 | */ | |
11519 | range = tnum_range(1, 1); | |
11520 | } | |
11521 | break; | |
11522 | ||
e92888c7 YS |
11523 | case BPF_PROG_TYPE_EXT: |
11524 | /* freplace program can return anything as its return value | |
11525 | * depends on the to-be-replaced kernel func or bpf program. | |
11526 | */ | |
390ee7e2 AS |
11527 | default: |
11528 | return 0; | |
11529 | } | |
11530 | ||
390ee7e2 | 11531 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 11532 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
c25b2ae1 | 11533 | reg_type_str(env, reg->type)); |
390ee7e2 AS |
11534 | return -EINVAL; |
11535 | } | |
11536 | ||
11537 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 11538 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
69fd337a | 11539 | if (prog->expected_attach_type == BPF_LSM_CGROUP && |
d1a6edec | 11540 | prog_type == BPF_PROG_TYPE_LSM && |
69fd337a SF |
11541 | !prog->aux->attach_func_proto->type) |
11542 | verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n"); | |
390ee7e2 AS |
11543 | return -EINVAL; |
11544 | } | |
5cf1e914 | 11545 | |
11546 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
11547 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
11548 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
11549 | return 0; |
11550 | } | |
11551 | ||
475fb78f AS |
11552 | /* non-recursive DFS pseudo code |
11553 | * 1 procedure DFS-iterative(G,v): | |
11554 | * 2 label v as discovered | |
11555 | * 3 let S be a stack | |
11556 | * 4 S.push(v) | |
11557 | * 5 while S is not empty | |
b6d20799 | 11558 | * 6 t <- S.peek() |
475fb78f AS |
11559 | * 7 if t is what we're looking for: |
11560 | * 8 return t | |
11561 | * 9 for all edges e in G.adjacentEdges(t) do | |
11562 | * 10 if edge e is already labelled | |
11563 | * 11 continue with the next edge | |
11564 | * 12 w <- G.adjacentVertex(t,e) | |
11565 | * 13 if vertex w is not discovered and not explored | |
11566 | * 14 label e as tree-edge | |
11567 | * 15 label w as discovered | |
11568 | * 16 S.push(w) | |
11569 | * 17 continue at 5 | |
11570 | * 18 else if vertex w is discovered | |
11571 | * 19 label e as back-edge | |
11572 | * 20 else | |
11573 | * 21 // vertex w is explored | |
11574 | * 22 label e as forward- or cross-edge | |
11575 | * 23 label t as explored | |
11576 | * 24 S.pop() | |
11577 | * | |
11578 | * convention: | |
11579 | * 0x10 - discovered | |
11580 | * 0x11 - discovered and fall-through edge labelled | |
11581 | * 0x12 - discovered and fall-through and branch edges labelled | |
11582 | * 0x20 - explored | |
11583 | */ | |
11584 | ||
11585 | enum { | |
11586 | DISCOVERED = 0x10, | |
11587 | EXPLORED = 0x20, | |
11588 | FALLTHROUGH = 1, | |
11589 | BRANCH = 2, | |
11590 | }; | |
11591 | ||
dc2a4ebc AS |
11592 | static u32 state_htab_size(struct bpf_verifier_env *env) |
11593 | { | |
11594 | return env->prog->len; | |
11595 | } | |
11596 | ||
5d839021 AS |
11597 | static struct bpf_verifier_state_list **explored_state( |
11598 | struct bpf_verifier_env *env, | |
11599 | int idx) | |
11600 | { | |
dc2a4ebc AS |
11601 | struct bpf_verifier_state *cur = env->cur_state; |
11602 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
11603 | ||
11604 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
11605 | } |
11606 | ||
11607 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
11608 | { | |
a8f500af | 11609 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 11610 | } |
f1bca824 | 11611 | |
59e2e27d WAF |
11612 | enum { |
11613 | DONE_EXPLORING = 0, | |
11614 | KEEP_EXPLORING = 1, | |
11615 | }; | |
11616 | ||
475fb78f AS |
11617 | /* t, w, e - match pseudo-code above: |
11618 | * t - index of current instruction | |
11619 | * w - next instruction | |
11620 | * e - edge | |
11621 | */ | |
2589726d AS |
11622 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
11623 | bool loop_ok) | |
475fb78f | 11624 | { |
7df737e9 AS |
11625 | int *insn_stack = env->cfg.insn_stack; |
11626 | int *insn_state = env->cfg.insn_state; | |
11627 | ||
475fb78f | 11628 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 11629 | return DONE_EXPLORING; |
475fb78f AS |
11630 | |
11631 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 11632 | return DONE_EXPLORING; |
475fb78f AS |
11633 | |
11634 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 11635 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 11636 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
11637 | return -EINVAL; |
11638 | } | |
11639 | ||
f1bca824 AS |
11640 | if (e == BRANCH) |
11641 | /* mark branch target for state pruning */ | |
5d839021 | 11642 | init_explored_state(env, w); |
f1bca824 | 11643 | |
475fb78f AS |
11644 | if (insn_state[w] == 0) { |
11645 | /* tree-edge */ | |
11646 | insn_state[t] = DISCOVERED | e; | |
11647 | insn_state[w] = DISCOVERED; | |
7df737e9 | 11648 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 11649 | return -E2BIG; |
7df737e9 | 11650 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 11651 | return KEEP_EXPLORING; |
475fb78f | 11652 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 11653 | if (loop_ok && env->bpf_capable) |
59e2e27d | 11654 | return DONE_EXPLORING; |
d9762e84 MKL |
11655 | verbose_linfo(env, t, "%d: ", t); |
11656 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 11657 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
11658 | return -EINVAL; |
11659 | } else if (insn_state[w] == EXPLORED) { | |
11660 | /* forward- or cross-edge */ | |
11661 | insn_state[t] = DISCOVERED | e; | |
11662 | } else { | |
61bd5218 | 11663 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
11664 | return -EFAULT; |
11665 | } | |
59e2e27d WAF |
11666 | return DONE_EXPLORING; |
11667 | } | |
11668 | ||
efdb22de YS |
11669 | static int visit_func_call_insn(int t, int insn_cnt, |
11670 | struct bpf_insn *insns, | |
11671 | struct bpf_verifier_env *env, | |
11672 | bool visit_callee) | |
11673 | { | |
11674 | int ret; | |
11675 | ||
11676 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
11677 | if (ret) | |
11678 | return ret; | |
11679 | ||
11680 | if (t + 1 < insn_cnt) | |
11681 | init_explored_state(env, t + 1); | |
11682 | if (visit_callee) { | |
11683 | init_explored_state(env, t); | |
86fc6ee6 AS |
11684 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
11685 | /* It's ok to allow recursion from CFG point of | |
11686 | * view. __check_func_call() will do the actual | |
11687 | * check. | |
11688 | */ | |
11689 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
11690 | } |
11691 | return ret; | |
11692 | } | |
11693 | ||
59e2e27d WAF |
11694 | /* Visits the instruction at index t and returns one of the following: |
11695 | * < 0 - an error occurred | |
11696 | * DONE_EXPLORING - the instruction was fully explored | |
11697 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
11698 | */ | |
11699 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) | |
11700 | { | |
11701 | struct bpf_insn *insns = env->prog->insnsi; | |
11702 | int ret; | |
11703 | ||
69c087ba YS |
11704 | if (bpf_pseudo_func(insns + t)) |
11705 | return visit_func_call_insn(t, insn_cnt, insns, env, true); | |
11706 | ||
59e2e27d WAF |
11707 | /* All non-branch instructions have a single fall-through edge. */ |
11708 | if (BPF_CLASS(insns[t].code) != BPF_JMP && | |
11709 | BPF_CLASS(insns[t].code) != BPF_JMP32) | |
11710 | return push_insn(t, t + 1, FALLTHROUGH, env, false); | |
11711 | ||
11712 | switch (BPF_OP(insns[t].code)) { | |
11713 | case BPF_EXIT: | |
11714 | return DONE_EXPLORING; | |
11715 | ||
11716 | case BPF_CALL: | |
bfc6bb74 AS |
11717 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
11718 | /* Mark this call insn to trigger is_state_visited() check | |
11719 | * before call itself is processed by __check_func_call(). | |
11720 | * Otherwise new async state will be pushed for further | |
11721 | * exploration. | |
11722 | */ | |
11723 | init_explored_state(env, t); | |
efdb22de YS |
11724 | return visit_func_call_insn(t, insn_cnt, insns, env, |
11725 | insns[t].src_reg == BPF_PSEUDO_CALL); | |
59e2e27d WAF |
11726 | |
11727 | case BPF_JA: | |
11728 | if (BPF_SRC(insns[t].code) != BPF_K) | |
11729 | return -EINVAL; | |
11730 | ||
11731 | /* unconditional jump with single edge */ | |
11732 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, | |
11733 | true); | |
11734 | if (ret) | |
11735 | return ret; | |
11736 | ||
11737 | /* unconditional jmp is not a good pruning point, | |
11738 | * but it's marked, since backtracking needs | |
11739 | * to record jmp history in is_state_visited(). | |
11740 | */ | |
11741 | init_explored_state(env, t + insns[t].off + 1); | |
11742 | /* tell verifier to check for equivalent states | |
11743 | * after every call and jump | |
11744 | */ | |
11745 | if (t + 1 < insn_cnt) | |
11746 | init_explored_state(env, t + 1); | |
11747 | ||
11748 | return ret; | |
11749 | ||
11750 | default: | |
11751 | /* conditional jump with two edges */ | |
11752 | init_explored_state(env, t); | |
11753 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); | |
11754 | if (ret) | |
11755 | return ret; | |
11756 | ||
11757 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); | |
11758 | } | |
475fb78f AS |
11759 | } |
11760 | ||
11761 | /* non-recursive depth-first-search to detect loops in BPF program | |
11762 | * loop == back-edge in directed graph | |
11763 | */ | |
58e2af8b | 11764 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 11765 | { |
475fb78f | 11766 | int insn_cnt = env->prog->len; |
7df737e9 | 11767 | int *insn_stack, *insn_state; |
475fb78f | 11768 | int ret = 0; |
59e2e27d | 11769 | int i; |
475fb78f | 11770 | |
7df737e9 | 11771 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
11772 | if (!insn_state) |
11773 | return -ENOMEM; | |
11774 | ||
7df737e9 | 11775 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 11776 | if (!insn_stack) { |
71dde681 | 11777 | kvfree(insn_state); |
475fb78f AS |
11778 | return -ENOMEM; |
11779 | } | |
11780 | ||
11781 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
11782 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 11783 | env->cfg.cur_stack = 1; |
475fb78f | 11784 | |
59e2e27d WAF |
11785 | while (env->cfg.cur_stack > 0) { |
11786 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 11787 | |
59e2e27d WAF |
11788 | ret = visit_insn(t, insn_cnt, env); |
11789 | switch (ret) { | |
11790 | case DONE_EXPLORING: | |
11791 | insn_state[t] = EXPLORED; | |
11792 | env->cfg.cur_stack--; | |
11793 | break; | |
11794 | case KEEP_EXPLORING: | |
11795 | break; | |
11796 | default: | |
11797 | if (ret > 0) { | |
11798 | verbose(env, "visit_insn internal bug\n"); | |
11799 | ret = -EFAULT; | |
475fb78f | 11800 | } |
475fb78f | 11801 | goto err_free; |
59e2e27d | 11802 | } |
475fb78f AS |
11803 | } |
11804 | ||
59e2e27d | 11805 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 11806 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
11807 | ret = -EFAULT; |
11808 | goto err_free; | |
11809 | } | |
475fb78f | 11810 | |
475fb78f AS |
11811 | for (i = 0; i < insn_cnt; i++) { |
11812 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 11813 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
11814 | ret = -EINVAL; |
11815 | goto err_free; | |
11816 | } | |
11817 | } | |
11818 | ret = 0; /* cfg looks good */ | |
11819 | ||
11820 | err_free: | |
71dde681 AS |
11821 | kvfree(insn_state); |
11822 | kvfree(insn_stack); | |
7df737e9 | 11823 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
11824 | return ret; |
11825 | } | |
11826 | ||
09b28d76 AS |
11827 | static int check_abnormal_return(struct bpf_verifier_env *env) |
11828 | { | |
11829 | int i; | |
11830 | ||
11831 | for (i = 1; i < env->subprog_cnt; i++) { | |
11832 | if (env->subprog_info[i].has_ld_abs) { | |
11833 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
11834 | return -EINVAL; | |
11835 | } | |
11836 | if (env->subprog_info[i].has_tail_call) { | |
11837 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
11838 | return -EINVAL; | |
11839 | } | |
11840 | } | |
11841 | return 0; | |
11842 | } | |
11843 | ||
838e9690 YS |
11844 | /* The minimum supported BTF func info size */ |
11845 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
11846 | #define MAX_FUNCINFO_REC_SIZE 252 | |
11847 | ||
c454a46b MKL |
11848 | static int check_btf_func(struct bpf_verifier_env *env, |
11849 | const union bpf_attr *attr, | |
af2ac3e1 | 11850 | bpfptr_t uattr) |
838e9690 | 11851 | { |
09b28d76 | 11852 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 11853 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 11854 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 11855 | struct bpf_func_info *krecord; |
8c1b6e69 | 11856 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
11857 | struct bpf_prog *prog; |
11858 | const struct btf *btf; | |
af2ac3e1 | 11859 | bpfptr_t urecord; |
d0b2818e | 11860 | u32 prev_offset = 0; |
09b28d76 | 11861 | bool scalar_return; |
e7ed83d6 | 11862 | int ret = -ENOMEM; |
838e9690 YS |
11863 | |
11864 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
11865 | if (!nfuncs) { |
11866 | if (check_abnormal_return(env)) | |
11867 | return -EINVAL; | |
838e9690 | 11868 | return 0; |
09b28d76 | 11869 | } |
838e9690 YS |
11870 | |
11871 | if (nfuncs != env->subprog_cnt) { | |
11872 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
11873 | return -EINVAL; | |
11874 | } | |
11875 | ||
11876 | urec_size = attr->func_info_rec_size; | |
11877 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
11878 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
11879 | urec_size % sizeof(u32)) { | |
11880 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
11881 | return -EINVAL; | |
11882 | } | |
11883 | ||
c454a46b MKL |
11884 | prog = env->prog; |
11885 | btf = prog->aux->btf; | |
838e9690 | 11886 | |
af2ac3e1 | 11887 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
11888 | min_size = min_t(u32, krec_size, urec_size); |
11889 | ||
ba64e7d8 | 11890 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
11891 | if (!krecord) |
11892 | return -ENOMEM; | |
8c1b6e69 AS |
11893 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
11894 | if (!info_aux) | |
11895 | goto err_free; | |
ba64e7d8 | 11896 | |
838e9690 YS |
11897 | for (i = 0; i < nfuncs; i++) { |
11898 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
11899 | if (ret) { | |
11900 | if (ret == -E2BIG) { | |
11901 | verbose(env, "nonzero tailing record in func info"); | |
11902 | /* set the size kernel expects so loader can zero | |
11903 | * out the rest of the record. | |
11904 | */ | |
af2ac3e1 AS |
11905 | if (copy_to_bpfptr_offset(uattr, |
11906 | offsetof(union bpf_attr, func_info_rec_size), | |
11907 | &min_size, sizeof(min_size))) | |
838e9690 YS |
11908 | ret = -EFAULT; |
11909 | } | |
c454a46b | 11910 | goto err_free; |
838e9690 YS |
11911 | } |
11912 | ||
af2ac3e1 | 11913 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 11914 | ret = -EFAULT; |
c454a46b | 11915 | goto err_free; |
838e9690 YS |
11916 | } |
11917 | ||
d30d42e0 | 11918 | /* check insn_off */ |
09b28d76 | 11919 | ret = -EINVAL; |
838e9690 | 11920 | if (i == 0) { |
d30d42e0 | 11921 | if (krecord[i].insn_off) { |
838e9690 | 11922 | verbose(env, |
d30d42e0 MKL |
11923 | "nonzero insn_off %u for the first func info record", |
11924 | krecord[i].insn_off); | |
c454a46b | 11925 | goto err_free; |
838e9690 | 11926 | } |
d30d42e0 | 11927 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
11928 | verbose(env, |
11929 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 11930 | krecord[i].insn_off, prev_offset); |
c454a46b | 11931 | goto err_free; |
838e9690 YS |
11932 | } |
11933 | ||
d30d42e0 | 11934 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 11935 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 11936 | goto err_free; |
838e9690 YS |
11937 | } |
11938 | ||
11939 | /* check type_id */ | |
ba64e7d8 | 11940 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 11941 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 11942 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 11943 | krecord[i].type_id); |
c454a46b | 11944 | goto err_free; |
838e9690 | 11945 | } |
51c39bb1 | 11946 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
11947 | |
11948 | func_proto = btf_type_by_id(btf, type->type); | |
11949 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
11950 | /* btf_func_check() already verified it during BTF load */ | |
11951 | goto err_free; | |
11952 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
11953 | scalar_return = | |
6089fb32 | 11954 | btf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type); |
09b28d76 AS |
11955 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
11956 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
11957 | goto err_free; | |
11958 | } | |
11959 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
11960 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
11961 | goto err_free; | |
11962 | } | |
11963 | ||
d30d42e0 | 11964 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 11965 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
11966 | } |
11967 | ||
ba64e7d8 YS |
11968 | prog->aux->func_info = krecord; |
11969 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 11970 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
11971 | return 0; |
11972 | ||
c454a46b | 11973 | err_free: |
ba64e7d8 | 11974 | kvfree(krecord); |
8c1b6e69 | 11975 | kfree(info_aux); |
838e9690 YS |
11976 | return ret; |
11977 | } | |
11978 | ||
ba64e7d8 YS |
11979 | static void adjust_btf_func(struct bpf_verifier_env *env) |
11980 | { | |
8c1b6e69 | 11981 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
11982 | int i; |
11983 | ||
8c1b6e69 | 11984 | if (!aux->func_info) |
ba64e7d8 YS |
11985 | return; |
11986 | ||
11987 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 11988 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
11989 | } |
11990 | ||
1b773d00 | 11991 | #define MIN_BPF_LINEINFO_SIZE offsetofend(struct bpf_line_info, line_col) |
c454a46b MKL |
11992 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
11993 | ||
11994 | static int check_btf_line(struct bpf_verifier_env *env, | |
11995 | const union bpf_attr *attr, | |
af2ac3e1 | 11996 | bpfptr_t uattr) |
c454a46b MKL |
11997 | { |
11998 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
11999 | struct bpf_subprog_info *sub; | |
12000 | struct bpf_line_info *linfo; | |
12001 | struct bpf_prog *prog; | |
12002 | const struct btf *btf; | |
af2ac3e1 | 12003 | bpfptr_t ulinfo; |
c454a46b MKL |
12004 | int err; |
12005 | ||
12006 | nr_linfo = attr->line_info_cnt; | |
12007 | if (!nr_linfo) | |
12008 | return 0; | |
0e6491b5 BC |
12009 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
12010 | return -EINVAL; | |
c454a46b MKL |
12011 | |
12012 | rec_size = attr->line_info_rec_size; | |
12013 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
12014 | rec_size > MAX_LINEINFO_REC_SIZE || | |
12015 | rec_size & (sizeof(u32) - 1)) | |
12016 | return -EINVAL; | |
12017 | ||
12018 | /* Need to zero it in case the userspace may | |
12019 | * pass in a smaller bpf_line_info object. | |
12020 | */ | |
12021 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
12022 | GFP_KERNEL | __GFP_NOWARN); | |
12023 | if (!linfo) | |
12024 | return -ENOMEM; | |
12025 | ||
12026 | prog = env->prog; | |
12027 | btf = prog->aux->btf; | |
12028 | ||
12029 | s = 0; | |
12030 | sub = env->subprog_info; | |
af2ac3e1 | 12031 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
12032 | expected_size = sizeof(struct bpf_line_info); |
12033 | ncopy = min_t(u32, expected_size, rec_size); | |
12034 | for (i = 0; i < nr_linfo; i++) { | |
12035 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
12036 | if (err) { | |
12037 | if (err == -E2BIG) { | |
12038 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
12039 | if (copy_to_bpfptr_offset(uattr, |
12040 | offsetof(union bpf_attr, line_info_rec_size), | |
12041 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
12042 | err = -EFAULT; |
12043 | } | |
12044 | goto err_free; | |
12045 | } | |
12046 | ||
af2ac3e1 | 12047 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
12048 | err = -EFAULT; |
12049 | goto err_free; | |
12050 | } | |
12051 | ||
12052 | /* | |
12053 | * Check insn_off to ensure | |
12054 | * 1) strictly increasing AND | |
12055 | * 2) bounded by prog->len | |
12056 | * | |
12057 | * The linfo[0].insn_off == 0 check logically falls into | |
12058 | * the later "missing bpf_line_info for func..." case | |
12059 | * because the first linfo[0].insn_off must be the | |
12060 | * first sub also and the first sub must have | |
12061 | * subprog_info[0].start == 0. | |
12062 | */ | |
12063 | if ((i && linfo[i].insn_off <= prev_offset) || | |
12064 | linfo[i].insn_off >= prog->len) { | |
12065 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
12066 | i, linfo[i].insn_off, prev_offset, | |
12067 | prog->len); | |
12068 | err = -EINVAL; | |
12069 | goto err_free; | |
12070 | } | |
12071 | ||
fdbaa0be MKL |
12072 | if (!prog->insnsi[linfo[i].insn_off].code) { |
12073 | verbose(env, | |
12074 | "Invalid insn code at line_info[%u].insn_off\n", | |
12075 | i); | |
12076 | err = -EINVAL; | |
12077 | goto err_free; | |
12078 | } | |
12079 | ||
23127b33 MKL |
12080 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
12081 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
12082 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
12083 | err = -EINVAL; | |
12084 | goto err_free; | |
12085 | } | |
12086 | ||
12087 | if (s != env->subprog_cnt) { | |
12088 | if (linfo[i].insn_off == sub[s].start) { | |
12089 | sub[s].linfo_idx = i; | |
12090 | s++; | |
12091 | } else if (sub[s].start < linfo[i].insn_off) { | |
12092 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
12093 | err = -EINVAL; | |
12094 | goto err_free; | |
12095 | } | |
12096 | } | |
12097 | ||
12098 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 12099 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
12100 | } |
12101 | ||
12102 | if (s != env->subprog_cnt) { | |
12103 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
12104 | env->subprog_cnt - s, s); | |
12105 | err = -EINVAL; | |
12106 | goto err_free; | |
12107 | } | |
12108 | ||
12109 | prog->aux->linfo = linfo; | |
12110 | prog->aux->nr_linfo = nr_linfo; | |
12111 | ||
12112 | return 0; | |
12113 | ||
12114 | err_free: | |
12115 | kvfree(linfo); | |
12116 | return err; | |
12117 | } | |
12118 | ||
fbd94c7a AS |
12119 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
12120 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
12121 | ||
12122 | static int check_core_relo(struct bpf_verifier_env *env, | |
12123 | const union bpf_attr *attr, | |
12124 | bpfptr_t uattr) | |
12125 | { | |
12126 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
12127 | struct bpf_core_relo core_relo = {}; | |
12128 | struct bpf_prog *prog = env->prog; | |
12129 | const struct btf *btf = prog->aux->btf; | |
12130 | struct bpf_core_ctx ctx = { | |
12131 | .log = &env->log, | |
12132 | .btf = btf, | |
12133 | }; | |
12134 | bpfptr_t u_core_relo; | |
12135 | int err; | |
12136 | ||
12137 | nr_core_relo = attr->core_relo_cnt; | |
12138 | if (!nr_core_relo) | |
12139 | return 0; | |
12140 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
12141 | return -EINVAL; | |
12142 | ||
12143 | rec_size = attr->core_relo_rec_size; | |
12144 | if (rec_size < MIN_CORE_RELO_SIZE || | |
12145 | rec_size > MAX_CORE_RELO_SIZE || | |
12146 | rec_size % sizeof(u32)) | |
12147 | return -EINVAL; | |
12148 | ||
12149 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
12150 | expected_size = sizeof(struct bpf_core_relo); | |
12151 | ncopy = min_t(u32, expected_size, rec_size); | |
12152 | ||
12153 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
12154 | * relocation record one at a time. | |
12155 | */ | |
12156 | for (i = 0; i < nr_core_relo; i++) { | |
12157 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
12158 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
12159 | if (err) { | |
12160 | if (err == -E2BIG) { | |
12161 | verbose(env, "nonzero tailing record in core_relo"); | |
12162 | if (copy_to_bpfptr_offset(uattr, | |
12163 | offsetof(union bpf_attr, core_relo_rec_size), | |
12164 | &expected_size, sizeof(expected_size))) | |
12165 | err = -EFAULT; | |
12166 | } | |
12167 | break; | |
12168 | } | |
12169 | ||
12170 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
12171 | err = -EFAULT; | |
12172 | break; | |
12173 | } | |
12174 | ||
12175 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
12176 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
12177 | i, core_relo.insn_off, prog->len); | |
12178 | err = -EINVAL; | |
12179 | break; | |
12180 | } | |
12181 | ||
12182 | err = bpf_core_apply(&ctx, &core_relo, i, | |
12183 | &prog->insnsi[core_relo.insn_off / 8]); | |
12184 | if (err) | |
12185 | break; | |
12186 | bpfptr_add(&u_core_relo, rec_size); | |
12187 | } | |
12188 | return err; | |
12189 | } | |
12190 | ||
c454a46b MKL |
12191 | static int check_btf_info(struct bpf_verifier_env *env, |
12192 | const union bpf_attr *attr, | |
af2ac3e1 | 12193 | bpfptr_t uattr) |
c454a46b MKL |
12194 | { |
12195 | struct btf *btf; | |
12196 | int err; | |
12197 | ||
09b28d76 AS |
12198 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
12199 | if (check_abnormal_return(env)) | |
12200 | return -EINVAL; | |
c454a46b | 12201 | return 0; |
09b28d76 | 12202 | } |
c454a46b MKL |
12203 | |
12204 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
12205 | if (IS_ERR(btf)) | |
12206 | return PTR_ERR(btf); | |
350a5c4d AS |
12207 | if (btf_is_kernel(btf)) { |
12208 | btf_put(btf); | |
12209 | return -EACCES; | |
12210 | } | |
c454a46b MKL |
12211 | env->prog->aux->btf = btf; |
12212 | ||
12213 | err = check_btf_func(env, attr, uattr); | |
12214 | if (err) | |
12215 | return err; | |
12216 | ||
12217 | err = check_btf_line(env, attr, uattr); | |
12218 | if (err) | |
12219 | return err; | |
12220 | ||
fbd94c7a AS |
12221 | err = check_core_relo(env, attr, uattr); |
12222 | if (err) | |
12223 | return err; | |
12224 | ||
c454a46b | 12225 | return 0; |
ba64e7d8 YS |
12226 | } |
12227 | ||
f1174f77 EC |
12228 | /* check %cur's range satisfies %old's */ |
12229 | static bool range_within(struct bpf_reg_state *old, | |
12230 | struct bpf_reg_state *cur) | |
12231 | { | |
b03c9f9f EC |
12232 | return old->umin_value <= cur->umin_value && |
12233 | old->umax_value >= cur->umax_value && | |
12234 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
12235 | old->smax_value >= cur->smax_value && |
12236 | old->u32_min_value <= cur->u32_min_value && | |
12237 | old->u32_max_value >= cur->u32_max_value && | |
12238 | old->s32_min_value <= cur->s32_min_value && | |
12239 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
12240 | } |
12241 | ||
f1174f77 EC |
12242 | /* If in the old state two registers had the same id, then they need to have |
12243 | * the same id in the new state as well. But that id could be different from | |
12244 | * the old state, so we need to track the mapping from old to new ids. | |
12245 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
12246 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
12247 | * regs with a different old id could still have new id 9, we don't care about | |
12248 | * that. | |
12249 | * So we look through our idmap to see if this old id has been seen before. If | |
12250 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 12251 | */ |
c9e73e3d | 12252 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 12253 | { |
f1174f77 | 12254 | unsigned int i; |
969bf05e | 12255 | |
c9e73e3d | 12256 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
12257 | if (!idmap[i].old) { |
12258 | /* Reached an empty slot; haven't seen this id before */ | |
12259 | idmap[i].old = old_id; | |
12260 | idmap[i].cur = cur_id; | |
12261 | return true; | |
12262 | } | |
12263 | if (idmap[i].old == old_id) | |
12264 | return idmap[i].cur == cur_id; | |
12265 | } | |
12266 | /* We ran out of idmap slots, which should be impossible */ | |
12267 | WARN_ON_ONCE(1); | |
12268 | return false; | |
12269 | } | |
12270 | ||
9242b5f5 AS |
12271 | static void clean_func_state(struct bpf_verifier_env *env, |
12272 | struct bpf_func_state *st) | |
12273 | { | |
12274 | enum bpf_reg_liveness live; | |
12275 | int i, j; | |
12276 | ||
12277 | for (i = 0; i < BPF_REG_FP; i++) { | |
12278 | live = st->regs[i].live; | |
12279 | /* liveness must not touch this register anymore */ | |
12280 | st->regs[i].live |= REG_LIVE_DONE; | |
12281 | if (!(live & REG_LIVE_READ)) | |
12282 | /* since the register is unused, clear its state | |
12283 | * to make further comparison simpler | |
12284 | */ | |
f54c7898 | 12285 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
12286 | } |
12287 | ||
12288 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
12289 | live = st->stack[i].spilled_ptr.live; | |
12290 | /* liveness must not touch this stack slot anymore */ | |
12291 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
12292 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 12293 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
12294 | for (j = 0; j < BPF_REG_SIZE; j++) |
12295 | st->stack[i].slot_type[j] = STACK_INVALID; | |
12296 | } | |
12297 | } | |
12298 | } | |
12299 | ||
12300 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
12301 | struct bpf_verifier_state *st) | |
12302 | { | |
12303 | int i; | |
12304 | ||
12305 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
12306 | /* all regs in this state in all frames were already marked */ | |
12307 | return; | |
12308 | ||
12309 | for (i = 0; i <= st->curframe; i++) | |
12310 | clean_func_state(env, st->frame[i]); | |
12311 | } | |
12312 | ||
12313 | /* the parentage chains form a tree. | |
12314 | * the verifier states are added to state lists at given insn and | |
12315 | * pushed into state stack for future exploration. | |
12316 | * when the verifier reaches bpf_exit insn some of the verifer states | |
12317 | * stored in the state lists have their final liveness state already, | |
12318 | * but a lot of states will get revised from liveness point of view when | |
12319 | * the verifier explores other branches. | |
12320 | * Example: | |
12321 | * 1: r0 = 1 | |
12322 | * 2: if r1 == 100 goto pc+1 | |
12323 | * 3: r0 = 2 | |
12324 | * 4: exit | |
12325 | * when the verifier reaches exit insn the register r0 in the state list of | |
12326 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
12327 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
12328 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
12329 | * | |
12330 | * Since the verifier pushes the branch states as it sees them while exploring | |
12331 | * the program the condition of walking the branch instruction for the second | |
12332 | * time means that all states below this branch were already explored and | |
8fb33b60 | 12333 | * their final liveness marks are already propagated. |
9242b5f5 AS |
12334 | * Hence when the verifier completes the search of state list in is_state_visited() |
12335 | * we can call this clean_live_states() function to mark all liveness states | |
12336 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
12337 | * will not be used. | |
12338 | * This function also clears the registers and stack for states that !READ | |
12339 | * to simplify state merging. | |
12340 | * | |
12341 | * Important note here that walking the same branch instruction in the callee | |
12342 | * doesn't meant that the states are DONE. The verifier has to compare | |
12343 | * the callsites | |
12344 | */ | |
12345 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
12346 | struct bpf_verifier_state *cur) | |
12347 | { | |
12348 | struct bpf_verifier_state_list *sl; | |
12349 | int i; | |
12350 | ||
5d839021 | 12351 | sl = *explored_state(env, insn); |
a8f500af | 12352 | while (sl) { |
2589726d AS |
12353 | if (sl->state.branches) |
12354 | goto next; | |
dc2a4ebc AS |
12355 | if (sl->state.insn_idx != insn || |
12356 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
12357 | goto next; |
12358 | for (i = 0; i <= cur->curframe; i++) | |
12359 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
12360 | goto next; | |
12361 | clean_verifier_state(env, &sl->state); | |
12362 | next: | |
12363 | sl = sl->next; | |
12364 | } | |
12365 | } | |
12366 | ||
f1174f77 | 12367 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
12368 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
12369 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 12370 | { |
f4d7e40a AS |
12371 | bool equal; |
12372 | ||
dc503a8a EC |
12373 | if (!(rold->live & REG_LIVE_READ)) |
12374 | /* explored state didn't use this */ | |
12375 | return true; | |
12376 | ||
679c782d | 12377 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
12378 | |
12379 | if (rold->type == PTR_TO_STACK) | |
12380 | /* two stack pointers are equal only if they're pointing to | |
12381 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
12382 | */ | |
12383 | return equal && rold->frameno == rcur->frameno; | |
12384 | ||
12385 | if (equal) | |
969bf05e AS |
12386 | return true; |
12387 | ||
f1174f77 EC |
12388 | if (rold->type == NOT_INIT) |
12389 | /* explored state can't have used this */ | |
969bf05e | 12390 | return true; |
f1174f77 EC |
12391 | if (rcur->type == NOT_INIT) |
12392 | return false; | |
c25b2ae1 | 12393 | switch (base_type(rold->type)) { |
f1174f77 | 12394 | case SCALAR_VALUE: |
e042aa53 DB |
12395 | if (env->explore_alu_limits) |
12396 | return false; | |
f1174f77 | 12397 | if (rcur->type == SCALAR_VALUE) { |
f63181b6 | 12398 | if (!rold->precise) |
b5dc0163 | 12399 | return true; |
f1174f77 EC |
12400 | /* new val must satisfy old val knowledge */ |
12401 | return range_within(rold, rcur) && | |
12402 | tnum_in(rold->var_off, rcur->var_off); | |
12403 | } else { | |
179d1c56 JH |
12404 | /* We're trying to use a pointer in place of a scalar. |
12405 | * Even if the scalar was unbounded, this could lead to | |
12406 | * pointer leaks because scalars are allowed to leak | |
12407 | * while pointers are not. We could make this safe in | |
12408 | * special cases if root is calling us, but it's | |
12409 | * probably not worth the hassle. | |
f1174f77 | 12410 | */ |
179d1c56 | 12411 | return false; |
f1174f77 | 12412 | } |
69c087ba | 12413 | case PTR_TO_MAP_KEY: |
f1174f77 | 12414 | case PTR_TO_MAP_VALUE: |
c25b2ae1 HL |
12415 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
12416 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
12417 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
12418 | * checked, doing so could have affected others with the same | |
12419 | * id, and we can't check for that because we lost the id when | |
12420 | * we converted to a PTR_TO_MAP_VALUE. | |
12421 | */ | |
12422 | if (type_may_be_null(rold->type)) { | |
12423 | if (!type_may_be_null(rcur->type)) | |
12424 | return false; | |
12425 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
12426 | return false; | |
12427 | /* Check our ids match any regs they're supposed to */ | |
12428 | return check_ids(rold->id, rcur->id, idmap); | |
12429 | } | |
12430 | ||
1b688a19 EC |
12431 | /* If the new min/max/var_off satisfy the old ones and |
12432 | * everything else matches, we are OK. | |
d83525ca AS |
12433 | * 'id' is not compared, since it's only used for maps with |
12434 | * bpf_spin_lock inside map element and in such cases if | |
12435 | * the rest of the prog is valid for one map element then | |
12436 | * it's valid for all map elements regardless of the key | |
12437 | * used in bpf_map_lookup() | |
1b688a19 EC |
12438 | */ |
12439 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
12440 | range_within(rold, rcur) && | |
12441 | tnum_in(rold->var_off, rcur->var_off); | |
de8f3a83 | 12442 | case PTR_TO_PACKET_META: |
f1174f77 | 12443 | case PTR_TO_PACKET: |
de8f3a83 | 12444 | if (rcur->type != rold->type) |
f1174f77 EC |
12445 | return false; |
12446 | /* We must have at least as much range as the old ptr | |
12447 | * did, so that any accesses which were safe before are | |
12448 | * still safe. This is true even if old range < old off, | |
12449 | * since someone could have accessed through (ptr - k), or | |
12450 | * even done ptr -= k in a register, to get a safe access. | |
12451 | */ | |
12452 | if (rold->range > rcur->range) | |
12453 | return false; | |
12454 | /* If the offsets don't match, we can't trust our alignment; | |
12455 | * nor can we be sure that we won't fall out of range. | |
12456 | */ | |
12457 | if (rold->off != rcur->off) | |
12458 | return false; | |
12459 | /* id relations must be preserved */ | |
12460 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
12461 | return false; | |
12462 | /* new val must satisfy old val knowledge */ | |
12463 | return range_within(rold, rcur) && | |
12464 | tnum_in(rold->var_off, rcur->var_off); | |
12465 | case PTR_TO_CTX: | |
12466 | case CONST_PTR_TO_MAP: | |
f1174f77 | 12467 | case PTR_TO_PACKET_END: |
d58e468b | 12468 | case PTR_TO_FLOW_KEYS: |
c64b7983 | 12469 | case PTR_TO_SOCKET: |
46f8bc92 | 12470 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 12471 | case PTR_TO_TCP_SOCK: |
fada7fdc | 12472 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
12473 | /* Only valid matches are exact, which memcmp() above |
12474 | * would have accepted | |
12475 | */ | |
12476 | default: | |
12477 | /* Don't know what's going on, just say it's not safe */ | |
12478 | return false; | |
12479 | } | |
969bf05e | 12480 | |
f1174f77 EC |
12481 | /* Shouldn't get here; if we do, say it's not safe */ |
12482 | WARN_ON_ONCE(1); | |
969bf05e AS |
12483 | return false; |
12484 | } | |
12485 | ||
e042aa53 DB |
12486 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
12487 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
12488 | { |
12489 | int i, spi; | |
12490 | ||
638f5b90 AS |
12491 | /* walk slots of the explored stack and ignore any additional |
12492 | * slots in the current stack, since explored(safe) state | |
12493 | * didn't use them | |
12494 | */ | |
12495 | for (i = 0; i < old->allocated_stack; i++) { | |
12496 | spi = i / BPF_REG_SIZE; | |
12497 | ||
b233920c AS |
12498 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
12499 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 12500 | /* explored state didn't use this */ |
fd05e57b | 12501 | continue; |
b233920c | 12502 | } |
cc2b14d5 | 12503 | |
638f5b90 AS |
12504 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
12505 | continue; | |
19e2dbb7 AS |
12506 | |
12507 | /* explored stack has more populated slots than current stack | |
12508 | * and these slots were used | |
12509 | */ | |
12510 | if (i >= cur->allocated_stack) | |
12511 | return false; | |
12512 | ||
cc2b14d5 AS |
12513 | /* if old state was safe with misc data in the stack |
12514 | * it will be safe with zero-initialized stack. | |
12515 | * The opposite is not true | |
12516 | */ | |
12517 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
12518 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
12519 | continue; | |
638f5b90 AS |
12520 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
12521 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
12522 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 12523 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
12524 | * this verifier states are not equivalent, |
12525 | * return false to continue verification of this path | |
12526 | */ | |
12527 | return false; | |
27113c59 | 12528 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 12529 | continue; |
27113c59 | 12530 | if (!is_spilled_reg(&old->stack[spi])) |
638f5b90 | 12531 | continue; |
e042aa53 DB |
12532 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
12533 | &cur->stack[spi].spilled_ptr, idmap)) | |
638f5b90 AS |
12534 | /* when explored and current stack slot are both storing |
12535 | * spilled registers, check that stored pointers types | |
12536 | * are the same as well. | |
12537 | * Ex: explored safe path could have stored | |
12538 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
12539 | * but current path has stored: | |
12540 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
12541 | * such verifier states are not equivalent. | |
12542 | * return false to continue verification of this path | |
12543 | */ | |
12544 | return false; | |
12545 | } | |
12546 | return true; | |
12547 | } | |
12548 | ||
fd978bf7 JS |
12549 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
12550 | { | |
12551 | if (old->acquired_refs != cur->acquired_refs) | |
12552 | return false; | |
12553 | return !memcmp(old->refs, cur->refs, | |
12554 | sizeof(*old->refs) * old->acquired_refs); | |
12555 | } | |
12556 | ||
f1bca824 AS |
12557 | /* compare two verifier states |
12558 | * | |
12559 | * all states stored in state_list are known to be valid, since | |
12560 | * verifier reached 'bpf_exit' instruction through them | |
12561 | * | |
12562 | * this function is called when verifier exploring different branches of | |
12563 | * execution popped from the state stack. If it sees an old state that has | |
12564 | * more strict register state and more strict stack state then this execution | |
12565 | * branch doesn't need to be explored further, since verifier already | |
12566 | * concluded that more strict state leads to valid finish. | |
12567 | * | |
12568 | * Therefore two states are equivalent if register state is more conservative | |
12569 | * and explored stack state is more conservative than the current one. | |
12570 | * Example: | |
12571 | * explored current | |
12572 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
12573 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
12574 | * | |
12575 | * In other words if current stack state (one being explored) has more | |
12576 | * valid slots than old one that already passed validation, it means | |
12577 | * the verifier can stop exploring and conclude that current state is valid too | |
12578 | * | |
12579 | * Similarly with registers. If explored state has register type as invalid | |
12580 | * whereas register type in current state is meaningful, it means that | |
12581 | * the current state will reach 'bpf_exit' instruction safely | |
12582 | */ | |
c9e73e3d | 12583 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 12584 | struct bpf_func_state *cur) |
f1bca824 AS |
12585 | { |
12586 | int i; | |
12587 | ||
c9e73e3d LB |
12588 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
12589 | for (i = 0; i < MAX_BPF_REG; i++) | |
e042aa53 DB |
12590 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
12591 | env->idmap_scratch)) | |
c9e73e3d | 12592 | return false; |
f1bca824 | 12593 | |
e042aa53 | 12594 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 12595 | return false; |
fd978bf7 JS |
12596 | |
12597 | if (!refsafe(old, cur)) | |
c9e73e3d LB |
12598 | return false; |
12599 | ||
12600 | return true; | |
f1bca824 AS |
12601 | } |
12602 | ||
f4d7e40a AS |
12603 | static bool states_equal(struct bpf_verifier_env *env, |
12604 | struct bpf_verifier_state *old, | |
12605 | struct bpf_verifier_state *cur) | |
12606 | { | |
12607 | int i; | |
12608 | ||
12609 | if (old->curframe != cur->curframe) | |
12610 | return false; | |
12611 | ||
979d63d5 DB |
12612 | /* Verification state from speculative execution simulation |
12613 | * must never prune a non-speculative execution one. | |
12614 | */ | |
12615 | if (old->speculative && !cur->speculative) | |
12616 | return false; | |
12617 | ||
d0d78c1d KKD |
12618 | if (old->active_lock.ptr != cur->active_lock.ptr || |
12619 | old->active_lock.id != cur->active_lock.id) | |
d83525ca AS |
12620 | return false; |
12621 | ||
f4d7e40a AS |
12622 | /* for states to be equal callsites have to be the same |
12623 | * and all frame states need to be equivalent | |
12624 | */ | |
12625 | for (i = 0; i <= old->curframe; i++) { | |
12626 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
12627 | return false; | |
c9e73e3d | 12628 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
12629 | return false; |
12630 | } | |
12631 | return true; | |
12632 | } | |
12633 | ||
5327ed3d JW |
12634 | /* Return 0 if no propagation happened. Return negative error code if error |
12635 | * happened. Otherwise, return the propagated bit. | |
12636 | */ | |
55e7f3b5 JW |
12637 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
12638 | struct bpf_reg_state *reg, | |
12639 | struct bpf_reg_state *parent_reg) | |
12640 | { | |
5327ed3d JW |
12641 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
12642 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
12643 | int err; |
12644 | ||
5327ed3d JW |
12645 | /* When comes here, read flags of PARENT_REG or REG could be any of |
12646 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
12647 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
12648 | */ | |
12649 | if (parent_flag == REG_LIVE_READ64 || | |
12650 | /* Or if there is no read flag from REG. */ | |
12651 | !flag || | |
12652 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
12653 | parent_flag == flag) | |
55e7f3b5 JW |
12654 | return 0; |
12655 | ||
5327ed3d | 12656 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
12657 | if (err) |
12658 | return err; | |
12659 | ||
5327ed3d | 12660 | return flag; |
55e7f3b5 JW |
12661 | } |
12662 | ||
8e9cd9ce | 12663 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
12664 | * straight-line code between a state and its parent. When we arrive at an |
12665 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
12666 | * code, so read marks in the state must propagate to the parent regardless | |
12667 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 12668 | * in mark_reg_read() is for. |
8e9cd9ce | 12669 | */ |
f4d7e40a AS |
12670 | static int propagate_liveness(struct bpf_verifier_env *env, |
12671 | const struct bpf_verifier_state *vstate, | |
12672 | struct bpf_verifier_state *vparent) | |
dc503a8a | 12673 | { |
3f8cafa4 | 12674 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 12675 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 12676 | int i, frame, err = 0; |
dc503a8a | 12677 | |
f4d7e40a AS |
12678 | if (vparent->curframe != vstate->curframe) { |
12679 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
12680 | vparent->curframe, vstate->curframe); | |
12681 | return -EFAULT; | |
12682 | } | |
dc503a8a EC |
12683 | /* Propagate read liveness of registers... */ |
12684 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 12685 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
12686 | parent = vparent->frame[frame]; |
12687 | state = vstate->frame[frame]; | |
12688 | parent_reg = parent->regs; | |
12689 | state_reg = state->regs; | |
83d16312 JK |
12690 | /* We don't need to worry about FP liveness, it's read-only */ |
12691 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
12692 | err = propagate_liveness_reg(env, &state_reg[i], |
12693 | &parent_reg[i]); | |
5327ed3d | 12694 | if (err < 0) |
3f8cafa4 | 12695 | return err; |
5327ed3d JW |
12696 | if (err == REG_LIVE_READ64) |
12697 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 12698 | } |
f4d7e40a | 12699 | |
1b04aee7 | 12700 | /* Propagate stack slots. */ |
f4d7e40a AS |
12701 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
12702 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
12703 | parent_reg = &parent->stack[i].spilled_ptr; |
12704 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
12705 | err = propagate_liveness_reg(env, state_reg, |
12706 | parent_reg); | |
5327ed3d | 12707 | if (err < 0) |
3f8cafa4 | 12708 | return err; |
dc503a8a EC |
12709 | } |
12710 | } | |
5327ed3d | 12711 | return 0; |
dc503a8a EC |
12712 | } |
12713 | ||
a3ce685d AS |
12714 | /* find precise scalars in the previous equivalent state and |
12715 | * propagate them into the current state | |
12716 | */ | |
12717 | static int propagate_precision(struct bpf_verifier_env *env, | |
12718 | const struct bpf_verifier_state *old) | |
12719 | { | |
12720 | struct bpf_reg_state *state_reg; | |
12721 | struct bpf_func_state *state; | |
529409ea | 12722 | int i, err = 0, fr; |
a3ce685d | 12723 | |
529409ea AN |
12724 | for (fr = old->curframe; fr >= 0; fr--) { |
12725 | state = old->frame[fr]; | |
12726 | state_reg = state->regs; | |
12727 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
12728 | if (state_reg->type != SCALAR_VALUE || | |
12729 | !state_reg->precise) | |
12730 | continue; | |
12731 | if (env->log.level & BPF_LOG_LEVEL2) | |
12732 | verbose(env, "frame %d: propagating r%d\n", i, fr); | |
12733 | err = mark_chain_precision_frame(env, fr, i); | |
12734 | if (err < 0) | |
12735 | return err; | |
12736 | } | |
a3ce685d | 12737 | |
529409ea AN |
12738 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
12739 | if (!is_spilled_reg(&state->stack[i])) | |
12740 | continue; | |
12741 | state_reg = &state->stack[i].spilled_ptr; | |
12742 | if (state_reg->type != SCALAR_VALUE || | |
12743 | !state_reg->precise) | |
12744 | continue; | |
12745 | if (env->log.level & BPF_LOG_LEVEL2) | |
12746 | verbose(env, "frame %d: propagating fp%d\n", | |
12747 | (-i - 1) * BPF_REG_SIZE, fr); | |
12748 | err = mark_chain_precision_stack_frame(env, fr, i); | |
12749 | if (err < 0) | |
12750 | return err; | |
12751 | } | |
a3ce685d AS |
12752 | } |
12753 | return 0; | |
12754 | } | |
12755 | ||
2589726d AS |
12756 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
12757 | struct bpf_verifier_state *cur) | |
12758 | { | |
12759 | struct bpf_func_state *fold, *fcur; | |
12760 | int i, fr = cur->curframe; | |
12761 | ||
12762 | if (old->curframe != fr) | |
12763 | return false; | |
12764 | ||
12765 | fold = old->frame[fr]; | |
12766 | fcur = cur->frame[fr]; | |
12767 | for (i = 0; i < MAX_BPF_REG; i++) | |
12768 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
12769 | offsetof(struct bpf_reg_state, parent))) | |
12770 | return false; | |
12771 | return true; | |
12772 | } | |
12773 | ||
12774 | ||
58e2af8b | 12775 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 12776 | { |
58e2af8b | 12777 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 12778 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 12779 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 12780 | int i, j, err, states_cnt = 0; |
10d274e8 | 12781 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 12782 | |
b5dc0163 | 12783 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 12784 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
12785 | /* this 'insn_idx' instruction wasn't marked, so we will not |
12786 | * be doing state search here | |
12787 | */ | |
12788 | return 0; | |
12789 | ||
2589726d AS |
12790 | /* bpf progs typically have pruning point every 4 instructions |
12791 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
12792 | * Do not add new state for future pruning if the verifier hasn't seen | |
12793 | * at least 2 jumps and at least 8 instructions. | |
12794 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
12795 | * In tests that amounts to up to 50% reduction into total verifier | |
12796 | * memory consumption and 20% verifier time speedup. | |
12797 | */ | |
12798 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
12799 | env->insn_processed - env->prev_insn_processed >= 8) | |
12800 | add_new_state = true; | |
12801 | ||
a8f500af AS |
12802 | pprev = explored_state(env, insn_idx); |
12803 | sl = *pprev; | |
12804 | ||
9242b5f5 AS |
12805 | clean_live_states(env, insn_idx, cur); |
12806 | ||
a8f500af | 12807 | while (sl) { |
dc2a4ebc AS |
12808 | states_cnt++; |
12809 | if (sl->state.insn_idx != insn_idx) | |
12810 | goto next; | |
bfc6bb74 | 12811 | |
2589726d | 12812 | if (sl->state.branches) { |
bfc6bb74 AS |
12813 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
12814 | ||
12815 | if (frame->in_async_callback_fn && | |
12816 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
12817 | /* Different async_entry_cnt means that the verifier is | |
12818 | * processing another entry into async callback. | |
12819 | * Seeing the same state is not an indication of infinite | |
12820 | * loop or infinite recursion. | |
12821 | * But finding the same state doesn't mean that it's safe | |
12822 | * to stop processing the current state. The previous state | |
12823 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
12824 | * Checking in_async_callback_fn alone is not enough either. | |
12825 | * Since the verifier still needs to catch infinite loops | |
12826 | * inside async callbacks. | |
12827 | */ | |
12828 | } else if (states_maybe_looping(&sl->state, cur) && | |
12829 | states_equal(env, &sl->state, cur)) { | |
2589726d AS |
12830 | verbose_linfo(env, insn_idx, "; "); |
12831 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
12832 | return -EINVAL; | |
12833 | } | |
12834 | /* if the verifier is processing a loop, avoid adding new state | |
12835 | * too often, since different loop iterations have distinct | |
12836 | * states and may not help future pruning. | |
12837 | * This threshold shouldn't be too low to make sure that | |
12838 | * a loop with large bound will be rejected quickly. | |
12839 | * The most abusive loop will be: | |
12840 | * r1 += 1 | |
12841 | * if r1 < 1000000 goto pc-2 | |
12842 | * 1M insn_procssed limit / 100 == 10k peak states. | |
12843 | * This threshold shouldn't be too high either, since states | |
12844 | * at the end of the loop are likely to be useful in pruning. | |
12845 | */ | |
12846 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
12847 | env->insn_processed - env->prev_insn_processed < 100) | |
12848 | add_new_state = false; | |
12849 | goto miss; | |
12850 | } | |
638f5b90 | 12851 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 12852 | sl->hit_cnt++; |
f1bca824 | 12853 | /* reached equivalent register/stack state, |
dc503a8a EC |
12854 | * prune the search. |
12855 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
12856 | * If we have any write marks in env->cur_state, they |
12857 | * will prevent corresponding reads in the continuation | |
12858 | * from reaching our parent (an explored_state). Our | |
12859 | * own state will get the read marks recorded, but | |
12860 | * they'll be immediately forgotten as we're pruning | |
12861 | * this state and will pop a new one. | |
f1bca824 | 12862 | */ |
f4d7e40a | 12863 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
12864 | |
12865 | /* if previous state reached the exit with precision and | |
12866 | * current state is equivalent to it (except precsion marks) | |
12867 | * the precision needs to be propagated back in | |
12868 | * the current state. | |
12869 | */ | |
12870 | err = err ? : push_jmp_history(env, cur); | |
12871 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
12872 | if (err) |
12873 | return err; | |
f1bca824 | 12874 | return 1; |
dc503a8a | 12875 | } |
2589726d AS |
12876 | miss: |
12877 | /* when new state is not going to be added do not increase miss count. | |
12878 | * Otherwise several loop iterations will remove the state | |
12879 | * recorded earlier. The goal of these heuristics is to have | |
12880 | * states from some iterations of the loop (some in the beginning | |
12881 | * and some at the end) to help pruning. | |
12882 | */ | |
12883 | if (add_new_state) | |
12884 | sl->miss_cnt++; | |
9f4686c4 AS |
12885 | /* heuristic to determine whether this state is beneficial |
12886 | * to keep checking from state equivalence point of view. | |
12887 | * Higher numbers increase max_states_per_insn and verification time, | |
12888 | * but do not meaningfully decrease insn_processed. | |
12889 | */ | |
12890 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
12891 | /* the state is unlikely to be useful. Remove it to | |
12892 | * speed up verification | |
12893 | */ | |
12894 | *pprev = sl->next; | |
12895 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
12896 | u32 br = sl->state.branches; |
12897 | ||
12898 | WARN_ONCE(br, | |
12899 | "BUG live_done but branches_to_explore %d\n", | |
12900 | br); | |
9f4686c4 AS |
12901 | free_verifier_state(&sl->state, false); |
12902 | kfree(sl); | |
12903 | env->peak_states--; | |
12904 | } else { | |
12905 | /* cannot free this state, since parentage chain may | |
12906 | * walk it later. Add it for free_list instead to | |
12907 | * be freed at the end of verification | |
12908 | */ | |
12909 | sl->next = env->free_list; | |
12910 | env->free_list = sl; | |
12911 | } | |
12912 | sl = *pprev; | |
12913 | continue; | |
12914 | } | |
dc2a4ebc | 12915 | next: |
9f4686c4 AS |
12916 | pprev = &sl->next; |
12917 | sl = *pprev; | |
f1bca824 AS |
12918 | } |
12919 | ||
06ee7115 AS |
12920 | if (env->max_states_per_insn < states_cnt) |
12921 | env->max_states_per_insn = states_cnt; | |
12922 | ||
2c78ee89 | 12923 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 12924 | return push_jmp_history(env, cur); |
ceefbc96 | 12925 | |
2589726d | 12926 | if (!add_new_state) |
b5dc0163 | 12927 | return push_jmp_history(env, cur); |
ceefbc96 | 12928 | |
2589726d AS |
12929 | /* There were no equivalent states, remember the current one. |
12930 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 12931 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 12932 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 12933 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
12934 | * again on the way to bpf_exit. |
12935 | * When looping the sl->state.branches will be > 0 and this state | |
12936 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 12937 | */ |
638f5b90 | 12938 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
12939 | if (!new_sl) |
12940 | return -ENOMEM; | |
06ee7115 AS |
12941 | env->total_states++; |
12942 | env->peak_states++; | |
2589726d AS |
12943 | env->prev_jmps_processed = env->jmps_processed; |
12944 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 | 12945 | |
7a830b53 AN |
12946 | /* forget precise markings we inherited, see __mark_chain_precision */ |
12947 | if (env->bpf_capable) | |
12948 | mark_all_scalars_imprecise(env, cur); | |
12949 | ||
f1bca824 | 12950 | /* add new state to the head of linked list */ |
679c782d EC |
12951 | new = &new_sl->state; |
12952 | err = copy_verifier_state(new, cur); | |
1969db47 | 12953 | if (err) { |
679c782d | 12954 | free_verifier_state(new, false); |
1969db47 AS |
12955 | kfree(new_sl); |
12956 | return err; | |
12957 | } | |
dc2a4ebc | 12958 | new->insn_idx = insn_idx; |
2589726d AS |
12959 | WARN_ONCE(new->branches != 1, |
12960 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 12961 | |
2589726d | 12962 | cur->parent = new; |
b5dc0163 AS |
12963 | cur->first_insn_idx = insn_idx; |
12964 | clear_jmp_history(cur); | |
5d839021 AS |
12965 | new_sl->next = *explored_state(env, insn_idx); |
12966 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
12967 | /* connect new state to parentage chain. Current frame needs all |
12968 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
12969 | * to the stack implicitly by JITs) so in callers' frames connect just | |
12970 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
12971 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
12972 | * from callee with its full parentage chain, anyway. | |
12973 | */ | |
8e9cd9ce EC |
12974 | /* clear write marks in current state: the writes we did are not writes |
12975 | * our child did, so they don't screen off its reads from us. | |
12976 | * (There are no read marks in current state, because reads always mark | |
12977 | * their parent and current state never has children yet. Only | |
12978 | * explored_states can get read marks.) | |
12979 | */ | |
eea1c227 AS |
12980 | for (j = 0; j <= cur->curframe; j++) { |
12981 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
12982 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
12983 | for (i = 0; i < BPF_REG_FP; i++) | |
12984 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
12985 | } | |
f4d7e40a AS |
12986 | |
12987 | /* all stack frames are accessible from callee, clear them all */ | |
12988 | for (j = 0; j <= cur->curframe; j++) { | |
12989 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 12990 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 12991 | |
679c782d | 12992 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 12993 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
12994 | frame->stack[i].spilled_ptr.parent = |
12995 | &newframe->stack[i].spilled_ptr; | |
12996 | } | |
f4d7e40a | 12997 | } |
f1bca824 AS |
12998 | return 0; |
12999 | } | |
13000 | ||
c64b7983 JS |
13001 | /* Return true if it's OK to have the same insn return a different type. */ |
13002 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
13003 | { | |
c25b2ae1 | 13004 | switch (base_type(type)) { |
c64b7983 JS |
13005 | case PTR_TO_CTX: |
13006 | case PTR_TO_SOCKET: | |
46f8bc92 | 13007 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 13008 | case PTR_TO_TCP_SOCK: |
fada7fdc | 13009 | case PTR_TO_XDP_SOCK: |
2a02759e | 13010 | case PTR_TO_BTF_ID: |
c64b7983 JS |
13011 | return false; |
13012 | default: | |
13013 | return true; | |
13014 | } | |
13015 | } | |
13016 | ||
13017 | /* If an instruction was previously used with particular pointer types, then we | |
13018 | * need to be careful to avoid cases such as the below, where it may be ok | |
13019 | * for one branch accessing the pointer, but not ok for the other branch: | |
13020 | * | |
13021 | * R1 = sock_ptr | |
13022 | * goto X; | |
13023 | * ... | |
13024 | * R1 = some_other_valid_ptr; | |
13025 | * goto X; | |
13026 | * ... | |
13027 | * R2 = *(u32 *)(R1 + 0); | |
13028 | */ | |
13029 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
13030 | { | |
13031 | return src != prev && (!reg_type_mismatch_ok(src) || | |
13032 | !reg_type_mismatch_ok(prev)); | |
13033 | } | |
13034 | ||
58e2af8b | 13035 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 13036 | { |
6f8a57cc | 13037 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 13038 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 13039 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 13040 | struct bpf_reg_state *regs; |
06ee7115 | 13041 | int insn_cnt = env->prog->len; |
17a52670 | 13042 | bool do_print_state = false; |
b5dc0163 | 13043 | int prev_insn_idx = -1; |
17a52670 | 13044 | |
17a52670 AS |
13045 | for (;;) { |
13046 | struct bpf_insn *insn; | |
13047 | u8 class; | |
13048 | int err; | |
13049 | ||
b5dc0163 | 13050 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 13051 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 13052 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 13053 | env->insn_idx, insn_cnt); |
17a52670 AS |
13054 | return -EFAULT; |
13055 | } | |
13056 | ||
c08435ec | 13057 | insn = &insns[env->insn_idx]; |
17a52670 AS |
13058 | class = BPF_CLASS(insn->code); |
13059 | ||
06ee7115 | 13060 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
13061 | verbose(env, |
13062 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 13063 | env->insn_processed); |
17a52670 AS |
13064 | return -E2BIG; |
13065 | } | |
13066 | ||
c08435ec | 13067 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
13068 | if (err < 0) |
13069 | return err; | |
13070 | if (err == 1) { | |
13071 | /* found equivalent state, can prune the search */ | |
06ee7115 | 13072 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 13073 | if (do_print_state) |
979d63d5 DB |
13074 | verbose(env, "\nfrom %d to %d%s: safe\n", |
13075 | env->prev_insn_idx, env->insn_idx, | |
13076 | env->cur_state->speculative ? | |
13077 | " (speculative execution)" : ""); | |
f1bca824 | 13078 | else |
c08435ec | 13079 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
13080 | } |
13081 | goto process_bpf_exit; | |
13082 | } | |
13083 | ||
c3494801 AS |
13084 | if (signal_pending(current)) |
13085 | return -EAGAIN; | |
13086 | ||
3c2ce60b DB |
13087 | if (need_resched()) |
13088 | cond_resched(); | |
13089 | ||
2e576648 CL |
13090 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
13091 | verbose(env, "\nfrom %d to %d%s:", | |
13092 | env->prev_insn_idx, env->insn_idx, | |
13093 | env->cur_state->speculative ? | |
13094 | " (speculative execution)" : ""); | |
13095 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
13096 | do_print_state = false; |
13097 | } | |
13098 | ||
06ee7115 | 13099 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 13100 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 13101 | .cb_call = disasm_kfunc_name, |
7105e828 | 13102 | .cb_print = verbose, |
abe08840 | 13103 | .private_data = env, |
7105e828 DB |
13104 | }; |
13105 | ||
2e576648 CL |
13106 | if (verifier_state_scratched(env)) |
13107 | print_insn_state(env, state->frame[state->curframe]); | |
13108 | ||
c08435ec | 13109 | verbose_linfo(env, env->insn_idx, "; "); |
2e576648 | 13110 | env->prev_log_len = env->log.len_used; |
c08435ec | 13111 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 13112 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
2e576648 CL |
13113 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
13114 | env->prev_log_len = env->log.len_used; | |
17a52670 AS |
13115 | } |
13116 | ||
cae1927c | 13117 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
13118 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
13119 | env->prev_insn_idx); | |
cae1927c JK |
13120 | if (err) |
13121 | return err; | |
13122 | } | |
13a27dfc | 13123 | |
638f5b90 | 13124 | regs = cur_regs(env); |
fe9a5ca7 | 13125 | sanitize_mark_insn_seen(env); |
b5dc0163 | 13126 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 13127 | |
17a52670 | 13128 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 13129 | err = check_alu_op(env, insn); |
17a52670 AS |
13130 | if (err) |
13131 | return err; | |
13132 | ||
13133 | } else if (class == BPF_LDX) { | |
3df126f3 | 13134 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
13135 | |
13136 | /* check for reserved fields is already done */ | |
13137 | ||
17a52670 | 13138 | /* check src operand */ |
dc503a8a | 13139 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
13140 | if (err) |
13141 | return err; | |
13142 | ||
dc503a8a | 13143 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
13144 | if (err) |
13145 | return err; | |
13146 | ||
725f9dcd AS |
13147 | src_reg_type = regs[insn->src_reg].type; |
13148 | ||
17a52670 AS |
13149 | /* check that memory (src_reg + off) is readable, |
13150 | * the state of dst_reg will be updated by this func | |
13151 | */ | |
c08435ec DB |
13152 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
13153 | insn->off, BPF_SIZE(insn->code), | |
13154 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
13155 | if (err) |
13156 | return err; | |
13157 | ||
c08435ec | 13158 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
13159 | |
13160 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
13161 | /* saw a valid insn |
13162 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 13163 | * save type to validate intersecting paths |
9bac3d6d | 13164 | */ |
3df126f3 | 13165 | *prev_src_type = src_reg_type; |
9bac3d6d | 13166 | |
c64b7983 | 13167 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
13168 | /* ABuser program is trying to use the same insn |
13169 | * dst_reg = *(u32*) (src_reg + off) | |
13170 | * with different pointer types: | |
13171 | * src_reg == ctx in one branch and | |
13172 | * src_reg == stack|map in some other branch. | |
13173 | * Reject it. | |
13174 | */ | |
61bd5218 | 13175 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
13176 | return -EINVAL; |
13177 | } | |
13178 | ||
17a52670 | 13179 | } else if (class == BPF_STX) { |
3df126f3 | 13180 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 13181 | |
91c960b0 BJ |
13182 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
13183 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
13184 | if (err) |
13185 | return err; | |
c08435ec | 13186 | env->insn_idx++; |
17a52670 AS |
13187 | continue; |
13188 | } | |
13189 | ||
5ca419f2 BJ |
13190 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
13191 | verbose(env, "BPF_STX uses reserved fields\n"); | |
13192 | return -EINVAL; | |
13193 | } | |
13194 | ||
17a52670 | 13195 | /* check src1 operand */ |
dc503a8a | 13196 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
13197 | if (err) |
13198 | return err; | |
13199 | /* check src2 operand */ | |
dc503a8a | 13200 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
13201 | if (err) |
13202 | return err; | |
13203 | ||
d691f9e8 AS |
13204 | dst_reg_type = regs[insn->dst_reg].type; |
13205 | ||
17a52670 | 13206 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
13207 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
13208 | insn->off, BPF_SIZE(insn->code), | |
13209 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
13210 | if (err) |
13211 | return err; | |
13212 | ||
c08435ec | 13213 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
13214 | |
13215 | if (*prev_dst_type == NOT_INIT) { | |
13216 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 13217 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 13218 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
13219 | return -EINVAL; |
13220 | } | |
13221 | ||
17a52670 AS |
13222 | } else if (class == BPF_ST) { |
13223 | if (BPF_MODE(insn->code) != BPF_MEM || | |
13224 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 13225 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
13226 | return -EINVAL; |
13227 | } | |
13228 | /* check src operand */ | |
dc503a8a | 13229 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
13230 | if (err) |
13231 | return err; | |
13232 | ||
f37a8cb8 | 13233 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 13234 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f | 13235 | insn->dst_reg, |
c25b2ae1 | 13236 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
13237 | return -EACCES; |
13238 | } | |
13239 | ||
17a52670 | 13240 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
13241 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
13242 | insn->off, BPF_SIZE(insn->code), | |
13243 | BPF_WRITE, -1, false); | |
17a52670 AS |
13244 | if (err) |
13245 | return err; | |
13246 | ||
092ed096 | 13247 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
13248 | u8 opcode = BPF_OP(insn->code); |
13249 | ||
2589726d | 13250 | env->jmps_processed++; |
17a52670 AS |
13251 | if (opcode == BPF_CALL) { |
13252 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
13253 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
13254 | && insn->off != 0) || | |
f4d7e40a | 13255 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
13256 | insn->src_reg != BPF_PSEUDO_CALL && |
13257 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
13258 | insn->dst_reg != BPF_REG_0 || |
13259 | class == BPF_JMP32) { | |
61bd5218 | 13260 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
13261 | return -EINVAL; |
13262 | } | |
13263 | ||
d0d78c1d | 13264 | if (env->cur_state->active_lock.ptr && |
d83525ca AS |
13265 | (insn->src_reg == BPF_PSEUDO_CALL || |
13266 | insn->imm != BPF_FUNC_spin_unlock)) { | |
13267 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
13268 | return -EINVAL; | |
13269 | } | |
f4d7e40a | 13270 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 13271 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 | 13272 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
5c073f26 | 13273 | err = check_kfunc_call(env, insn, &env->insn_idx); |
f4d7e40a | 13274 | else |
69c087ba | 13275 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
13276 | if (err) |
13277 | return err; | |
17a52670 AS |
13278 | } else if (opcode == BPF_JA) { |
13279 | if (BPF_SRC(insn->code) != BPF_K || | |
13280 | insn->imm != 0 || | |
13281 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
13282 | insn->dst_reg != BPF_REG_0 || |
13283 | class == BPF_JMP32) { | |
61bd5218 | 13284 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
13285 | return -EINVAL; |
13286 | } | |
13287 | ||
c08435ec | 13288 | env->insn_idx += insn->off + 1; |
17a52670 AS |
13289 | continue; |
13290 | ||
13291 | } else if (opcode == BPF_EXIT) { | |
13292 | if (BPF_SRC(insn->code) != BPF_K || | |
13293 | insn->imm != 0 || | |
13294 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
13295 | insn->dst_reg != BPF_REG_0 || |
13296 | class == BPF_JMP32) { | |
61bd5218 | 13297 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
13298 | return -EINVAL; |
13299 | } | |
13300 | ||
d0d78c1d | 13301 | if (env->cur_state->active_lock.ptr) { |
d83525ca AS |
13302 | verbose(env, "bpf_spin_unlock is missing\n"); |
13303 | return -EINVAL; | |
13304 | } | |
13305 | ||
9d9d00ac KKD |
13306 | /* We must do check_reference_leak here before |
13307 | * prepare_func_exit to handle the case when | |
13308 | * state->curframe > 0, it may be a callback | |
13309 | * function, for which reference_state must | |
13310 | * match caller reference state when it exits. | |
13311 | */ | |
13312 | err = check_reference_leak(env); | |
13313 | if (err) | |
13314 | return err; | |
13315 | ||
f4d7e40a AS |
13316 | if (state->curframe) { |
13317 | /* exit from nested function */ | |
c08435ec | 13318 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
13319 | if (err) |
13320 | return err; | |
13321 | do_print_state = true; | |
13322 | continue; | |
13323 | } | |
13324 | ||
390ee7e2 AS |
13325 | err = check_return_code(env); |
13326 | if (err) | |
13327 | return err; | |
f1bca824 | 13328 | process_bpf_exit: |
0f55f9ed | 13329 | mark_verifier_state_scratched(env); |
2589726d | 13330 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 13331 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 13332 | &env->insn_idx, pop_log); |
638f5b90 AS |
13333 | if (err < 0) { |
13334 | if (err != -ENOENT) | |
13335 | return err; | |
17a52670 AS |
13336 | break; |
13337 | } else { | |
13338 | do_print_state = true; | |
13339 | continue; | |
13340 | } | |
13341 | } else { | |
c08435ec | 13342 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
13343 | if (err) |
13344 | return err; | |
13345 | } | |
13346 | } else if (class == BPF_LD) { | |
13347 | u8 mode = BPF_MODE(insn->code); | |
13348 | ||
13349 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
13350 | err = check_ld_abs(env, insn); |
13351 | if (err) | |
13352 | return err; | |
13353 | ||
17a52670 AS |
13354 | } else if (mode == BPF_IMM) { |
13355 | err = check_ld_imm(env, insn); | |
13356 | if (err) | |
13357 | return err; | |
13358 | ||
c08435ec | 13359 | env->insn_idx++; |
fe9a5ca7 | 13360 | sanitize_mark_insn_seen(env); |
17a52670 | 13361 | } else { |
61bd5218 | 13362 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
13363 | return -EINVAL; |
13364 | } | |
13365 | } else { | |
61bd5218 | 13366 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
13367 | return -EINVAL; |
13368 | } | |
13369 | ||
c08435ec | 13370 | env->insn_idx++; |
17a52670 AS |
13371 | } |
13372 | ||
13373 | return 0; | |
13374 | } | |
13375 | ||
541c3bad AN |
13376 | static int find_btf_percpu_datasec(struct btf *btf) |
13377 | { | |
13378 | const struct btf_type *t; | |
13379 | const char *tname; | |
13380 | int i, n; | |
13381 | ||
13382 | /* | |
13383 | * Both vmlinux and module each have their own ".data..percpu" | |
13384 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
13385 | * types to look at only module's own BTF types. | |
13386 | */ | |
13387 | n = btf_nr_types(btf); | |
13388 | if (btf_is_module(btf)) | |
13389 | i = btf_nr_types(btf_vmlinux); | |
13390 | else | |
13391 | i = 1; | |
13392 | ||
13393 | for(; i < n; i++) { | |
13394 | t = btf_type_by_id(btf, i); | |
13395 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
13396 | continue; | |
13397 | ||
13398 | tname = btf_name_by_offset(btf, t->name_off); | |
13399 | if (!strcmp(tname, ".data..percpu")) | |
13400 | return i; | |
13401 | } | |
13402 | ||
13403 | return -ENOENT; | |
13404 | } | |
13405 | ||
4976b718 HL |
13406 | /* replace pseudo btf_id with kernel symbol address */ |
13407 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
13408 | struct bpf_insn *insn, | |
13409 | struct bpf_insn_aux_data *aux) | |
13410 | { | |
eaa6bcb7 HL |
13411 | const struct btf_var_secinfo *vsi; |
13412 | const struct btf_type *datasec; | |
541c3bad | 13413 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
13414 | const struct btf_type *t; |
13415 | const char *sym_name; | |
eaa6bcb7 | 13416 | bool percpu = false; |
f16e6313 | 13417 | u32 type, id = insn->imm; |
541c3bad | 13418 | struct btf *btf; |
f16e6313 | 13419 | s32 datasec_id; |
4976b718 | 13420 | u64 addr; |
541c3bad | 13421 | int i, btf_fd, err; |
4976b718 | 13422 | |
541c3bad AN |
13423 | btf_fd = insn[1].imm; |
13424 | if (btf_fd) { | |
13425 | btf = btf_get_by_fd(btf_fd); | |
13426 | if (IS_ERR(btf)) { | |
13427 | verbose(env, "invalid module BTF object FD specified.\n"); | |
13428 | return -EINVAL; | |
13429 | } | |
13430 | } else { | |
13431 | if (!btf_vmlinux) { | |
13432 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
13433 | return -EINVAL; | |
13434 | } | |
13435 | btf = btf_vmlinux; | |
13436 | btf_get(btf); | |
4976b718 HL |
13437 | } |
13438 | ||
541c3bad | 13439 | t = btf_type_by_id(btf, id); |
4976b718 HL |
13440 | if (!t) { |
13441 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
13442 | err = -ENOENT; |
13443 | goto err_put; | |
4976b718 HL |
13444 | } |
13445 | ||
13446 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
13447 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
13448 | err = -EINVAL; | |
13449 | goto err_put; | |
4976b718 HL |
13450 | } |
13451 | ||
541c3bad | 13452 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
13453 | addr = kallsyms_lookup_name(sym_name); |
13454 | if (!addr) { | |
13455 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
13456 | sym_name); | |
541c3bad AN |
13457 | err = -ENOENT; |
13458 | goto err_put; | |
4976b718 HL |
13459 | } |
13460 | ||
541c3bad | 13461 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 13462 | if (datasec_id > 0) { |
541c3bad | 13463 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
13464 | for_each_vsi(i, datasec, vsi) { |
13465 | if (vsi->type == id) { | |
13466 | percpu = true; | |
13467 | break; | |
13468 | } | |
13469 | } | |
13470 | } | |
13471 | ||
4976b718 HL |
13472 | insn[0].imm = (u32)addr; |
13473 | insn[1].imm = addr >> 32; | |
13474 | ||
13475 | type = t->type; | |
541c3bad | 13476 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 | 13477 | if (percpu) { |
5844101a | 13478 | aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; |
541c3bad | 13479 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
13480 | aux->btf_var.btf_id = type; |
13481 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
13482 | const struct btf_type *ret; |
13483 | const char *tname; | |
13484 | u32 tsize; | |
13485 | ||
13486 | /* resolve the type size of ksym. */ | |
541c3bad | 13487 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 13488 | if (IS_ERR(ret)) { |
541c3bad | 13489 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
13490 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
13491 | tname, PTR_ERR(ret)); | |
541c3bad AN |
13492 | err = -EINVAL; |
13493 | goto err_put; | |
4976b718 | 13494 | } |
34d3a78c | 13495 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
4976b718 HL |
13496 | aux->btf_var.mem_size = tsize; |
13497 | } else { | |
13498 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 13499 | aux->btf_var.btf = btf; |
4976b718 HL |
13500 | aux->btf_var.btf_id = type; |
13501 | } | |
541c3bad AN |
13502 | |
13503 | /* check whether we recorded this BTF (and maybe module) already */ | |
13504 | for (i = 0; i < env->used_btf_cnt; i++) { | |
13505 | if (env->used_btfs[i].btf == btf) { | |
13506 | btf_put(btf); | |
13507 | return 0; | |
13508 | } | |
13509 | } | |
13510 | ||
13511 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
13512 | err = -E2BIG; | |
13513 | goto err_put; | |
13514 | } | |
13515 | ||
13516 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
13517 | btf_mod->btf = btf; | |
13518 | btf_mod->module = NULL; | |
13519 | ||
13520 | /* if we reference variables from kernel module, bump its refcount */ | |
13521 | if (btf_is_module(btf)) { | |
13522 | btf_mod->module = btf_try_get_module(btf); | |
13523 | if (!btf_mod->module) { | |
13524 | err = -ENXIO; | |
13525 | goto err_put; | |
13526 | } | |
13527 | } | |
13528 | ||
13529 | env->used_btf_cnt++; | |
13530 | ||
4976b718 | 13531 | return 0; |
541c3bad AN |
13532 | err_put: |
13533 | btf_put(btf); | |
13534 | return err; | |
4976b718 HL |
13535 | } |
13536 | ||
d83525ca AS |
13537 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
13538 | { | |
13539 | switch (type) { | |
13540 | case BPF_PROG_TYPE_KPROBE: | |
13541 | case BPF_PROG_TYPE_TRACEPOINT: | |
13542 | case BPF_PROG_TYPE_PERF_EVENT: | |
13543 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
5002615a | 13544 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
d83525ca AS |
13545 | return true; |
13546 | default: | |
13547 | return false; | |
13548 | } | |
13549 | } | |
13550 | ||
61bd5218 JK |
13551 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
13552 | struct bpf_map *map, | |
fdc15d38 AS |
13553 | struct bpf_prog *prog) |
13554 | ||
13555 | { | |
7e40781c | 13556 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
a3884572 | 13557 | |
f0c5941f KKD |
13558 | if (btf_record_has_field(map->record, BPF_LIST_HEAD)) { |
13559 | if (is_tracing_prog_type(prog_type)) { | |
13560 | verbose(env, "tracing progs cannot use bpf_list_head yet\n"); | |
13561 | return -EINVAL; | |
13562 | } | |
13563 | } | |
13564 | ||
db559117 | 13565 | if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) { |
9e7a4d98 KS |
13566 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
13567 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
13568 | return -EINVAL; | |
13569 | } | |
13570 | ||
13571 | if (is_tracing_prog_type(prog_type)) { | |
13572 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
13573 | return -EINVAL; | |
13574 | } | |
13575 | ||
13576 | if (prog->aux->sleepable) { | |
13577 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
13578 | return -EINVAL; | |
13579 | } | |
d83525ca AS |
13580 | } |
13581 | ||
db559117 | 13582 | if (btf_record_has_field(map->record, BPF_TIMER)) { |
5e0bc308 DB |
13583 | if (is_tracing_prog_type(prog_type)) { |
13584 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
13585 | return -EINVAL; | |
13586 | } | |
13587 | } | |
13588 | ||
a3884572 | 13589 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 13590 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
13591 | verbose(env, "offload device mismatch between prog and map\n"); |
13592 | return -EINVAL; | |
13593 | } | |
13594 | ||
85d33df3 MKL |
13595 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
13596 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
13597 | return -EINVAL; | |
13598 | } | |
13599 | ||
1e6c62a8 AS |
13600 | if (prog->aux->sleepable) |
13601 | switch (map->map_type) { | |
13602 | case BPF_MAP_TYPE_HASH: | |
13603 | case BPF_MAP_TYPE_LRU_HASH: | |
13604 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
13605 | case BPF_MAP_TYPE_PERCPU_HASH: |
13606 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
13607 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
13608 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
13609 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
ba90c2cc | 13610 | case BPF_MAP_TYPE_RINGBUF: |
583c1f42 | 13611 | case BPF_MAP_TYPE_USER_RINGBUF: |
0fe4b381 KS |
13612 | case BPF_MAP_TYPE_INODE_STORAGE: |
13613 | case BPF_MAP_TYPE_SK_STORAGE: | |
13614 | case BPF_MAP_TYPE_TASK_STORAGE: | |
ba90c2cc | 13615 | break; |
1e6c62a8 AS |
13616 | default: |
13617 | verbose(env, | |
ba90c2cc | 13618 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
1e6c62a8 AS |
13619 | return -EINVAL; |
13620 | } | |
13621 | ||
fdc15d38 AS |
13622 | return 0; |
13623 | } | |
13624 | ||
b741f163 RG |
13625 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
13626 | { | |
13627 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
13628 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
13629 | } | |
13630 | ||
4976b718 HL |
13631 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
13632 | * | |
13633 | * 1. if it accesses map FD, replace it with actual map pointer. | |
13634 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
13635 | * | |
13636 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 13637 | */ |
4976b718 | 13638 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
13639 | { |
13640 | struct bpf_insn *insn = env->prog->insnsi; | |
13641 | int insn_cnt = env->prog->len; | |
fdc15d38 | 13642 | int i, j, err; |
0246e64d | 13643 | |
f1f7714e | 13644 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
13645 | if (err) |
13646 | return err; | |
13647 | ||
0246e64d | 13648 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 13649 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 13650 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 13651 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
13652 | return -EINVAL; |
13653 | } | |
13654 | ||
0246e64d | 13655 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 13656 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
13657 | struct bpf_map *map; |
13658 | struct fd f; | |
d8eca5bb | 13659 | u64 addr; |
387544bf | 13660 | u32 fd; |
0246e64d AS |
13661 | |
13662 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
13663 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
13664 | insn[1].off != 0) { | |
61bd5218 | 13665 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
13666 | return -EINVAL; |
13667 | } | |
13668 | ||
d8eca5bb | 13669 | if (insn[0].src_reg == 0) |
0246e64d AS |
13670 | /* valid generic load 64-bit imm */ |
13671 | goto next_insn; | |
13672 | ||
4976b718 HL |
13673 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
13674 | aux = &env->insn_aux_data[i]; | |
13675 | err = check_pseudo_btf_id(env, insn, aux); | |
13676 | if (err) | |
13677 | return err; | |
13678 | goto next_insn; | |
13679 | } | |
13680 | ||
69c087ba YS |
13681 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
13682 | aux = &env->insn_aux_data[i]; | |
13683 | aux->ptr_type = PTR_TO_FUNC; | |
13684 | goto next_insn; | |
13685 | } | |
13686 | ||
d8eca5bb DB |
13687 | /* In final convert_pseudo_ld_imm64() step, this is |
13688 | * converted into regular 64-bit imm load insn. | |
13689 | */ | |
387544bf AS |
13690 | switch (insn[0].src_reg) { |
13691 | case BPF_PSEUDO_MAP_VALUE: | |
13692 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
13693 | break; | |
13694 | case BPF_PSEUDO_MAP_FD: | |
13695 | case BPF_PSEUDO_MAP_IDX: | |
13696 | if (insn[1].imm == 0) | |
13697 | break; | |
13698 | fallthrough; | |
13699 | default: | |
13700 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
13701 | return -EINVAL; |
13702 | } | |
13703 | ||
387544bf AS |
13704 | switch (insn[0].src_reg) { |
13705 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
13706 | case BPF_PSEUDO_MAP_IDX: | |
13707 | if (bpfptr_is_null(env->fd_array)) { | |
13708 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
13709 | return -EPROTO; | |
13710 | } | |
13711 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
13712 | insn[0].imm * sizeof(fd), | |
13713 | sizeof(fd))) | |
13714 | return -EFAULT; | |
13715 | break; | |
13716 | default: | |
13717 | fd = insn[0].imm; | |
13718 | break; | |
13719 | } | |
13720 | ||
13721 | f = fdget(fd); | |
c2101297 | 13722 | map = __bpf_map_get(f); |
0246e64d | 13723 | if (IS_ERR(map)) { |
61bd5218 | 13724 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 13725 | insn[0].imm); |
0246e64d AS |
13726 | return PTR_ERR(map); |
13727 | } | |
13728 | ||
61bd5218 | 13729 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
13730 | if (err) { |
13731 | fdput(f); | |
13732 | return err; | |
13733 | } | |
13734 | ||
d8eca5bb | 13735 | aux = &env->insn_aux_data[i]; |
387544bf AS |
13736 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
13737 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
13738 | addr = (unsigned long)map; |
13739 | } else { | |
13740 | u32 off = insn[1].imm; | |
13741 | ||
13742 | if (off >= BPF_MAX_VAR_OFF) { | |
13743 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
13744 | fdput(f); | |
13745 | return -EINVAL; | |
13746 | } | |
13747 | ||
13748 | if (!map->ops->map_direct_value_addr) { | |
13749 | verbose(env, "no direct value access support for this map type\n"); | |
13750 | fdput(f); | |
13751 | return -EINVAL; | |
13752 | } | |
13753 | ||
13754 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
13755 | if (err) { | |
13756 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
13757 | map->value_size, off); | |
13758 | fdput(f); | |
13759 | return err; | |
13760 | } | |
13761 | ||
13762 | aux->map_off = off; | |
13763 | addr += off; | |
13764 | } | |
13765 | ||
13766 | insn[0].imm = (u32)addr; | |
13767 | insn[1].imm = addr >> 32; | |
0246e64d AS |
13768 | |
13769 | /* check whether we recorded this map already */ | |
d8eca5bb | 13770 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 13771 | if (env->used_maps[j] == map) { |
d8eca5bb | 13772 | aux->map_index = j; |
0246e64d AS |
13773 | fdput(f); |
13774 | goto next_insn; | |
13775 | } | |
d8eca5bb | 13776 | } |
0246e64d AS |
13777 | |
13778 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
13779 | fdput(f); | |
13780 | return -E2BIG; | |
13781 | } | |
13782 | ||
0246e64d AS |
13783 | /* hold the map. If the program is rejected by verifier, |
13784 | * the map will be released by release_maps() or it | |
13785 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 13786 | * and all maps are released in free_used_maps() |
0246e64d | 13787 | */ |
1e0bd5a0 | 13788 | bpf_map_inc(map); |
d8eca5bb DB |
13789 | |
13790 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
13791 | env->used_maps[env->used_map_cnt++] = map; |
13792 | ||
b741f163 | 13793 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 13794 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 13795 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
13796 | fdput(f); |
13797 | return -EBUSY; | |
13798 | } | |
13799 | ||
0246e64d AS |
13800 | fdput(f); |
13801 | next_insn: | |
13802 | insn++; | |
13803 | i++; | |
5e581dad DB |
13804 | continue; |
13805 | } | |
13806 | ||
13807 | /* Basic sanity check before we invest more work here. */ | |
13808 | if (!bpf_opcode_in_insntable(insn->code)) { | |
13809 | verbose(env, "unknown opcode %02x\n", insn->code); | |
13810 | return -EINVAL; | |
0246e64d AS |
13811 | } |
13812 | } | |
13813 | ||
13814 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
13815 | * 'struct bpf_map *' into a register instead of user map_fd. | |
13816 | * These pointers will be used later by verifier to validate map access. | |
13817 | */ | |
13818 | return 0; | |
13819 | } | |
13820 | ||
13821 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 13822 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 13823 | { |
a2ea0746 DB |
13824 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
13825 | env->used_map_cnt); | |
0246e64d AS |
13826 | } |
13827 | ||
541c3bad AN |
13828 | /* drop refcnt of maps used by the rejected program */ |
13829 | static void release_btfs(struct bpf_verifier_env *env) | |
13830 | { | |
13831 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
13832 | env->used_btf_cnt); | |
13833 | } | |
13834 | ||
0246e64d | 13835 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 13836 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
13837 | { |
13838 | struct bpf_insn *insn = env->prog->insnsi; | |
13839 | int insn_cnt = env->prog->len; | |
13840 | int i; | |
13841 | ||
69c087ba YS |
13842 | for (i = 0; i < insn_cnt; i++, insn++) { |
13843 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
13844 | continue; | |
13845 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
13846 | continue; | |
13847 | insn->src_reg = 0; | |
13848 | } | |
0246e64d AS |
13849 | } |
13850 | ||
8041902d AS |
13851 | /* single env->prog->insni[off] instruction was replaced with the range |
13852 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
13853 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
13854 | */ | |
75f0fc7b HF |
13855 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
13856 | struct bpf_insn_aux_data *new_data, | |
13857 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 13858 | { |
75f0fc7b | 13859 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 13860 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 13861 | u32 old_seen = old_data[off].seen; |
b325fbca | 13862 | u32 prog_len; |
c131187d | 13863 | int i; |
8041902d | 13864 | |
b325fbca JW |
13865 | /* aux info at OFF always needs adjustment, no matter fast path |
13866 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
13867 | * original insn at old prog. | |
13868 | */ | |
13869 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
13870 | ||
8041902d | 13871 | if (cnt == 1) |
75f0fc7b | 13872 | return; |
b325fbca | 13873 | prog_len = new_prog->len; |
75f0fc7b | 13874 | |
8041902d AS |
13875 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
13876 | memcpy(new_data + off + cnt - 1, old_data + off, | |
13877 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 13878 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
13879 | /* Expand insni[off]'s seen count to the patched range. */ |
13880 | new_data[i].seen = old_seen; | |
b325fbca JW |
13881 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
13882 | } | |
8041902d AS |
13883 | env->insn_aux_data = new_data; |
13884 | vfree(old_data); | |
8041902d AS |
13885 | } |
13886 | ||
cc8b0b92 AS |
13887 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
13888 | { | |
13889 | int i; | |
13890 | ||
13891 | if (len == 1) | |
13892 | return; | |
4cb3d99c JW |
13893 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
13894 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 13895 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 13896 | continue; |
9c8105bd | 13897 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
13898 | } |
13899 | } | |
13900 | ||
7506d211 | 13901 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
13902 | { |
13903 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
13904 | int i, sz = prog->aux->size_poke_tab; | |
13905 | struct bpf_jit_poke_descriptor *desc; | |
13906 | ||
13907 | for (i = 0; i < sz; i++) { | |
13908 | desc = &tab[i]; | |
7506d211 JF |
13909 | if (desc->insn_idx <= off) |
13910 | continue; | |
a748c697 MF |
13911 | desc->insn_idx += len - 1; |
13912 | } | |
13913 | } | |
13914 | ||
8041902d AS |
13915 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
13916 | const struct bpf_insn *patch, u32 len) | |
13917 | { | |
13918 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
13919 | struct bpf_insn_aux_data *new_data = NULL; |
13920 | ||
13921 | if (len > 1) { | |
13922 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
13923 | sizeof(struct bpf_insn_aux_data))); | |
13924 | if (!new_data) | |
13925 | return NULL; | |
13926 | } | |
8041902d AS |
13927 | |
13928 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
13929 | if (IS_ERR(new_prog)) { |
13930 | if (PTR_ERR(new_prog) == -ERANGE) | |
13931 | verbose(env, | |
13932 | "insn %d cannot be patched due to 16-bit range\n", | |
13933 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 13934 | vfree(new_data); |
8041902d | 13935 | return NULL; |
4f73379e | 13936 | } |
75f0fc7b | 13937 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 13938 | adjust_subprog_starts(env, off, len); |
7506d211 | 13939 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
13940 | return new_prog; |
13941 | } | |
13942 | ||
52875a04 JK |
13943 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
13944 | u32 off, u32 cnt) | |
13945 | { | |
13946 | int i, j; | |
13947 | ||
13948 | /* find first prog starting at or after off (first to remove) */ | |
13949 | for (i = 0; i < env->subprog_cnt; i++) | |
13950 | if (env->subprog_info[i].start >= off) | |
13951 | break; | |
13952 | /* find first prog starting at or after off + cnt (first to stay) */ | |
13953 | for (j = i; j < env->subprog_cnt; j++) | |
13954 | if (env->subprog_info[j].start >= off + cnt) | |
13955 | break; | |
13956 | /* if j doesn't start exactly at off + cnt, we are just removing | |
13957 | * the front of previous prog | |
13958 | */ | |
13959 | if (env->subprog_info[j].start != off + cnt) | |
13960 | j--; | |
13961 | ||
13962 | if (j > i) { | |
13963 | struct bpf_prog_aux *aux = env->prog->aux; | |
13964 | int move; | |
13965 | ||
13966 | /* move fake 'exit' subprog as well */ | |
13967 | move = env->subprog_cnt + 1 - j; | |
13968 | ||
13969 | memmove(env->subprog_info + i, | |
13970 | env->subprog_info + j, | |
13971 | sizeof(*env->subprog_info) * move); | |
13972 | env->subprog_cnt -= j - i; | |
13973 | ||
13974 | /* remove func_info */ | |
13975 | if (aux->func_info) { | |
13976 | move = aux->func_info_cnt - j; | |
13977 | ||
13978 | memmove(aux->func_info + i, | |
13979 | aux->func_info + j, | |
13980 | sizeof(*aux->func_info) * move); | |
13981 | aux->func_info_cnt -= j - i; | |
13982 | /* func_info->insn_off is set after all code rewrites, | |
13983 | * in adjust_btf_func() - no need to adjust | |
13984 | */ | |
13985 | } | |
13986 | } else { | |
13987 | /* convert i from "first prog to remove" to "first to adjust" */ | |
13988 | if (env->subprog_info[i].start == off) | |
13989 | i++; | |
13990 | } | |
13991 | ||
13992 | /* update fake 'exit' subprog as well */ | |
13993 | for (; i <= env->subprog_cnt; i++) | |
13994 | env->subprog_info[i].start -= cnt; | |
13995 | ||
13996 | return 0; | |
13997 | } | |
13998 | ||
13999 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
14000 | u32 cnt) | |
14001 | { | |
14002 | struct bpf_prog *prog = env->prog; | |
14003 | u32 i, l_off, l_cnt, nr_linfo; | |
14004 | struct bpf_line_info *linfo; | |
14005 | ||
14006 | nr_linfo = prog->aux->nr_linfo; | |
14007 | if (!nr_linfo) | |
14008 | return 0; | |
14009 | ||
14010 | linfo = prog->aux->linfo; | |
14011 | ||
14012 | /* find first line info to remove, count lines to be removed */ | |
14013 | for (i = 0; i < nr_linfo; i++) | |
14014 | if (linfo[i].insn_off >= off) | |
14015 | break; | |
14016 | ||
14017 | l_off = i; | |
14018 | l_cnt = 0; | |
14019 | for (; i < nr_linfo; i++) | |
14020 | if (linfo[i].insn_off < off + cnt) | |
14021 | l_cnt++; | |
14022 | else | |
14023 | break; | |
14024 | ||
14025 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
14026 | * last removed linfo. prog is already modified, so prog->len == off | |
14027 | * means no live instructions after (tail of the program was removed). | |
14028 | */ | |
14029 | if (prog->len != off && l_cnt && | |
14030 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
14031 | l_cnt--; | |
14032 | linfo[--i].insn_off = off + cnt; | |
14033 | } | |
14034 | ||
14035 | /* remove the line info which refer to the removed instructions */ | |
14036 | if (l_cnt) { | |
14037 | memmove(linfo + l_off, linfo + i, | |
14038 | sizeof(*linfo) * (nr_linfo - i)); | |
14039 | ||
14040 | prog->aux->nr_linfo -= l_cnt; | |
14041 | nr_linfo = prog->aux->nr_linfo; | |
14042 | } | |
14043 | ||
14044 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
14045 | for (i = l_off; i < nr_linfo; i++) | |
14046 | linfo[i].insn_off -= cnt; | |
14047 | ||
14048 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
14049 | for (i = 0; i <= env->subprog_cnt; i++) | |
14050 | if (env->subprog_info[i].linfo_idx > l_off) { | |
14051 | /* program may have started in the removed region but | |
14052 | * may not be fully removed | |
14053 | */ | |
14054 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
14055 | env->subprog_info[i].linfo_idx -= l_cnt; | |
14056 | else | |
14057 | env->subprog_info[i].linfo_idx = l_off; | |
14058 | } | |
14059 | ||
14060 | return 0; | |
14061 | } | |
14062 | ||
14063 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
14064 | { | |
14065 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
14066 | unsigned int orig_prog_len = env->prog->len; | |
14067 | int err; | |
14068 | ||
08ca90af JK |
14069 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
14070 | bpf_prog_offload_remove_insns(env, off, cnt); | |
14071 | ||
52875a04 JK |
14072 | err = bpf_remove_insns(env->prog, off, cnt); |
14073 | if (err) | |
14074 | return err; | |
14075 | ||
14076 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
14077 | if (err) | |
14078 | return err; | |
14079 | ||
14080 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
14081 | if (err) | |
14082 | return err; | |
14083 | ||
14084 | memmove(aux_data + off, aux_data + off + cnt, | |
14085 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
14086 | ||
14087 | return 0; | |
14088 | } | |
14089 | ||
2a5418a1 DB |
14090 | /* The verifier does more data flow analysis than llvm and will not |
14091 | * explore branches that are dead at run time. Malicious programs can | |
14092 | * have dead code too. Therefore replace all dead at-run-time code | |
14093 | * with 'ja -1'. | |
14094 | * | |
14095 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
14096 | * program and through another bug we would manage to jump there, then | |
14097 | * we'd execute beyond program memory otherwise. Returning exception | |
14098 | * code also wouldn't work since we can have subprogs where the dead | |
14099 | * code could be located. | |
c131187d AS |
14100 | */ |
14101 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
14102 | { | |
14103 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 14104 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
14105 | struct bpf_insn *insn = env->prog->insnsi; |
14106 | const int insn_cnt = env->prog->len; | |
14107 | int i; | |
14108 | ||
14109 | for (i = 0; i < insn_cnt; i++) { | |
14110 | if (aux_data[i].seen) | |
14111 | continue; | |
2a5418a1 | 14112 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 14113 | aux_data[i].zext_dst = false; |
c131187d AS |
14114 | } |
14115 | } | |
14116 | ||
e2ae4ca2 JK |
14117 | static bool insn_is_cond_jump(u8 code) |
14118 | { | |
14119 | u8 op; | |
14120 | ||
092ed096 JW |
14121 | if (BPF_CLASS(code) == BPF_JMP32) |
14122 | return true; | |
14123 | ||
e2ae4ca2 JK |
14124 | if (BPF_CLASS(code) != BPF_JMP) |
14125 | return false; | |
14126 | ||
14127 | op = BPF_OP(code); | |
14128 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
14129 | } | |
14130 | ||
14131 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
14132 | { | |
14133 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
14134 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
14135 | struct bpf_insn *insn = env->prog->insnsi; | |
14136 | const int insn_cnt = env->prog->len; | |
14137 | int i; | |
14138 | ||
14139 | for (i = 0; i < insn_cnt; i++, insn++) { | |
14140 | if (!insn_is_cond_jump(insn->code)) | |
14141 | continue; | |
14142 | ||
14143 | if (!aux_data[i + 1].seen) | |
14144 | ja.off = insn->off; | |
14145 | else if (!aux_data[i + 1 + insn->off].seen) | |
14146 | ja.off = 0; | |
14147 | else | |
14148 | continue; | |
14149 | ||
08ca90af JK |
14150 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
14151 | bpf_prog_offload_replace_insn(env, i, &ja); | |
14152 | ||
e2ae4ca2 JK |
14153 | memcpy(insn, &ja, sizeof(ja)); |
14154 | } | |
14155 | } | |
14156 | ||
52875a04 JK |
14157 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
14158 | { | |
14159 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
14160 | int insn_cnt = env->prog->len; | |
14161 | int i, err; | |
14162 | ||
14163 | for (i = 0; i < insn_cnt; i++) { | |
14164 | int j; | |
14165 | ||
14166 | j = 0; | |
14167 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
14168 | j++; | |
14169 | if (!j) | |
14170 | continue; | |
14171 | ||
14172 | err = verifier_remove_insns(env, i, j); | |
14173 | if (err) | |
14174 | return err; | |
14175 | insn_cnt = env->prog->len; | |
14176 | } | |
14177 | ||
14178 | return 0; | |
14179 | } | |
14180 | ||
a1b14abc JK |
14181 | static int opt_remove_nops(struct bpf_verifier_env *env) |
14182 | { | |
14183 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
14184 | struct bpf_insn *insn = env->prog->insnsi; | |
14185 | int insn_cnt = env->prog->len; | |
14186 | int i, err; | |
14187 | ||
14188 | for (i = 0; i < insn_cnt; i++) { | |
14189 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
14190 | continue; | |
14191 | ||
14192 | err = verifier_remove_insns(env, i, 1); | |
14193 | if (err) | |
14194 | return err; | |
14195 | insn_cnt--; | |
14196 | i--; | |
14197 | } | |
14198 | ||
14199 | return 0; | |
14200 | } | |
14201 | ||
d6c2308c JW |
14202 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
14203 | const union bpf_attr *attr) | |
a4b1d3c1 | 14204 | { |
d6c2308c | 14205 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 14206 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 14207 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 14208 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 14209 | struct bpf_prog *new_prog; |
d6c2308c | 14210 | bool rnd_hi32; |
a4b1d3c1 | 14211 | |
d6c2308c | 14212 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 14213 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
14214 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
14215 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
14216 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
14217 | for (i = 0; i < len; i++) { |
14218 | int adj_idx = i + delta; | |
14219 | struct bpf_insn insn; | |
83a28819 | 14220 | int load_reg; |
a4b1d3c1 | 14221 | |
d6c2308c | 14222 | insn = insns[adj_idx]; |
83a28819 | 14223 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
14224 | if (!aux[adj_idx].zext_dst) { |
14225 | u8 code, class; | |
14226 | u32 imm_rnd; | |
14227 | ||
14228 | if (!rnd_hi32) | |
14229 | continue; | |
14230 | ||
14231 | code = insn.code; | |
14232 | class = BPF_CLASS(code); | |
83a28819 | 14233 | if (load_reg == -1) |
d6c2308c JW |
14234 | continue; |
14235 | ||
14236 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
14237 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
14238 | * here. | |
d6c2308c | 14239 | */ |
83a28819 | 14240 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
14241 | if (class == BPF_LD && |
14242 | BPF_MODE(code) == BPF_IMM) | |
14243 | i++; | |
14244 | continue; | |
14245 | } | |
14246 | ||
14247 | /* ctx load could be transformed into wider load. */ | |
14248 | if (class == BPF_LDX && | |
14249 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
14250 | continue; | |
14251 | ||
a251c17a | 14252 | imm_rnd = get_random_u32(); |
d6c2308c JW |
14253 | rnd_hi32_patch[0] = insn; |
14254 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 14255 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
14256 | patch = rnd_hi32_patch; |
14257 | patch_len = 4; | |
14258 | goto apply_patch_buffer; | |
14259 | } | |
14260 | ||
39491867 BJ |
14261 | /* Add in an zero-extend instruction if a) the JIT has requested |
14262 | * it or b) it's a CMPXCHG. | |
14263 | * | |
14264 | * The latter is because: BPF_CMPXCHG always loads a value into | |
14265 | * R0, therefore always zero-extends. However some archs' | |
14266 | * equivalent instruction only does this load when the | |
14267 | * comparison is successful. This detail of CMPXCHG is | |
14268 | * orthogonal to the general zero-extension behaviour of the | |
14269 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
14270 | */ | |
14271 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
14272 | continue; |
14273 | ||
83a28819 IL |
14274 | if (WARN_ON(load_reg == -1)) { |
14275 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
14276 | return -EFAULT; | |
b2e37a71 IL |
14277 | } |
14278 | ||
a4b1d3c1 | 14279 | zext_patch[0] = insn; |
b2e37a71 IL |
14280 | zext_patch[1].dst_reg = load_reg; |
14281 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
14282 | patch = zext_patch; |
14283 | patch_len = 2; | |
14284 | apply_patch_buffer: | |
14285 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
14286 | if (!new_prog) |
14287 | return -ENOMEM; | |
14288 | env->prog = new_prog; | |
14289 | insns = new_prog->insnsi; | |
14290 | aux = env->insn_aux_data; | |
d6c2308c | 14291 | delta += patch_len - 1; |
a4b1d3c1 JW |
14292 | } |
14293 | ||
14294 | return 0; | |
14295 | } | |
14296 | ||
c64b7983 JS |
14297 | /* convert load instructions that access fields of a context type into a |
14298 | * sequence of instructions that access fields of the underlying structure: | |
14299 | * struct __sk_buff -> struct sk_buff | |
14300 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 14301 | */ |
58e2af8b | 14302 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 14303 | { |
00176a34 | 14304 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 14305 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 14306 | const int insn_cnt = env->prog->len; |
36bbef52 | 14307 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 14308 | u32 target_size, size_default, off; |
9bac3d6d | 14309 | struct bpf_prog *new_prog; |
d691f9e8 | 14310 | enum bpf_access_type type; |
f96da094 | 14311 | bool is_narrower_load; |
9bac3d6d | 14312 | |
b09928b9 DB |
14313 | if (ops->gen_prologue || env->seen_direct_write) { |
14314 | if (!ops->gen_prologue) { | |
14315 | verbose(env, "bpf verifier is misconfigured\n"); | |
14316 | return -EINVAL; | |
14317 | } | |
36bbef52 DB |
14318 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
14319 | env->prog); | |
14320 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 14321 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
14322 | return -EINVAL; |
14323 | } else if (cnt) { | |
8041902d | 14324 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
14325 | if (!new_prog) |
14326 | return -ENOMEM; | |
8041902d | 14327 | |
36bbef52 | 14328 | env->prog = new_prog; |
3df126f3 | 14329 | delta += cnt - 1; |
36bbef52 DB |
14330 | } |
14331 | } | |
14332 | ||
c64b7983 | 14333 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
14334 | return 0; |
14335 | ||
3df126f3 | 14336 | insn = env->prog->insnsi + delta; |
36bbef52 | 14337 | |
9bac3d6d | 14338 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 | 14339 | bpf_convert_ctx_access_t convert_ctx_access; |
2039f26f | 14340 | bool ctx_access; |
c64b7983 | 14341 | |
62c7989b DB |
14342 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
14343 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
14344 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 14345 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 14346 | type = BPF_READ; |
2039f26f DB |
14347 | ctx_access = true; |
14348 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || | |
14349 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
14350 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
14351 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
14352 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
14353 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
14354 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
14355 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 14356 | type = BPF_WRITE; |
2039f26f DB |
14357 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
14358 | } else { | |
9bac3d6d | 14359 | continue; |
2039f26f | 14360 | } |
9bac3d6d | 14361 | |
af86ca4e | 14362 | if (type == BPF_WRITE && |
2039f26f | 14363 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 14364 | struct bpf_insn patch[] = { |
af86ca4e | 14365 | *insn, |
2039f26f | 14366 | BPF_ST_NOSPEC(), |
af86ca4e AS |
14367 | }; |
14368 | ||
14369 | cnt = ARRAY_SIZE(patch); | |
14370 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
14371 | if (!new_prog) | |
14372 | return -ENOMEM; | |
14373 | ||
14374 | delta += cnt - 1; | |
14375 | env->prog = new_prog; | |
14376 | insn = new_prog->insnsi + i + delta; | |
14377 | continue; | |
14378 | } | |
14379 | ||
2039f26f DB |
14380 | if (!ctx_access) |
14381 | continue; | |
14382 | ||
6efe152d | 14383 | switch ((int)env->insn_aux_data[i + delta].ptr_type) { |
c64b7983 JS |
14384 | case PTR_TO_CTX: |
14385 | if (!ops->convert_ctx_access) | |
14386 | continue; | |
14387 | convert_ctx_access = ops->convert_ctx_access; | |
14388 | break; | |
14389 | case PTR_TO_SOCKET: | |
46f8bc92 | 14390 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
14391 | convert_ctx_access = bpf_sock_convert_ctx_access; |
14392 | break; | |
655a51e5 MKL |
14393 | case PTR_TO_TCP_SOCK: |
14394 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
14395 | break; | |
fada7fdc JL |
14396 | case PTR_TO_XDP_SOCK: |
14397 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
14398 | break; | |
2a02759e | 14399 | case PTR_TO_BTF_ID: |
6efe152d | 14400 | case PTR_TO_BTF_ID | PTR_UNTRUSTED: |
282de143 KKD |
14401 | /* PTR_TO_BTF_ID | MEM_ALLOC always has a valid lifetime, unlike |
14402 | * PTR_TO_BTF_ID, and an active ref_obj_id, but the same cannot | |
14403 | * be said once it is marked PTR_UNTRUSTED, hence we must handle | |
14404 | * any faults for loads into such types. BPF_WRITE is disallowed | |
14405 | * for this case. | |
14406 | */ | |
14407 | case PTR_TO_BTF_ID | MEM_ALLOC | PTR_UNTRUSTED: | |
27ae7997 MKL |
14408 | if (type == BPF_READ) { |
14409 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
14410 | BPF_SIZE((insn)->code); | |
14411 | env->prog->aux->num_exentries++; | |
2a02759e | 14412 | } |
2a02759e | 14413 | continue; |
c64b7983 | 14414 | default: |
9bac3d6d | 14415 | continue; |
c64b7983 | 14416 | } |
9bac3d6d | 14417 | |
31fd8581 | 14418 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 14419 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
14420 | |
14421 | /* If the read access is a narrower load of the field, | |
14422 | * convert to a 4/8-byte load, to minimum program type specific | |
14423 | * convert_ctx_access changes. If conversion is successful, | |
14424 | * we will apply proper mask to the result. | |
14425 | */ | |
f96da094 | 14426 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
14427 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
14428 | off = insn->off; | |
31fd8581 | 14429 | if (is_narrower_load) { |
f96da094 DB |
14430 | u8 size_code; |
14431 | ||
14432 | if (type == BPF_WRITE) { | |
61bd5218 | 14433 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
14434 | return -EINVAL; |
14435 | } | |
31fd8581 | 14436 | |
f96da094 | 14437 | size_code = BPF_H; |
31fd8581 YS |
14438 | if (ctx_field_size == 4) |
14439 | size_code = BPF_W; | |
14440 | else if (ctx_field_size == 8) | |
14441 | size_code = BPF_DW; | |
f96da094 | 14442 | |
bc23105c | 14443 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
14444 | insn->code = BPF_LDX | BPF_MEM | size_code; |
14445 | } | |
f96da094 DB |
14446 | |
14447 | target_size = 0; | |
c64b7983 JS |
14448 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
14449 | &target_size); | |
f96da094 DB |
14450 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
14451 | (ctx_field_size && !target_size)) { | |
61bd5218 | 14452 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
14453 | return -EINVAL; |
14454 | } | |
f96da094 DB |
14455 | |
14456 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
14457 | u8 shift = bpf_ctx_narrow_access_offset( |
14458 | off, size, size_default) * 8; | |
d7af7e49 AI |
14459 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
14460 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
14461 | return -EINVAL; | |
14462 | } | |
46f53a65 AI |
14463 | if (ctx_field_size <= 4) { |
14464 | if (shift) | |
14465 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
14466 | insn->dst_reg, | |
14467 | shift); | |
31fd8581 | 14468 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 14469 | (1 << size * 8) - 1); |
46f53a65 AI |
14470 | } else { |
14471 | if (shift) | |
14472 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
14473 | insn->dst_reg, | |
14474 | shift); | |
31fd8581 | 14475 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 14476 | (1ULL << size * 8) - 1); |
46f53a65 | 14477 | } |
31fd8581 | 14478 | } |
9bac3d6d | 14479 | |
8041902d | 14480 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
14481 | if (!new_prog) |
14482 | return -ENOMEM; | |
14483 | ||
3df126f3 | 14484 | delta += cnt - 1; |
9bac3d6d AS |
14485 | |
14486 | /* keep walking new program and skip insns we just inserted */ | |
14487 | env->prog = new_prog; | |
3df126f3 | 14488 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
14489 | } |
14490 | ||
14491 | return 0; | |
14492 | } | |
14493 | ||
1c2a088a AS |
14494 | static int jit_subprogs(struct bpf_verifier_env *env) |
14495 | { | |
14496 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
14497 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 14498 | struct bpf_map *map_ptr; |
7105e828 | 14499 | struct bpf_insn *insn; |
1c2a088a | 14500 | void *old_bpf_func; |
c4c0bdc0 | 14501 | int err, num_exentries; |
1c2a088a | 14502 | |
f910cefa | 14503 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
14504 | return 0; |
14505 | ||
7105e828 | 14506 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 14507 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 14508 | continue; |
69c087ba | 14509 | |
c7a89784 DB |
14510 | /* Upon error here we cannot fall back to interpreter but |
14511 | * need a hard reject of the program. Thus -EFAULT is | |
14512 | * propagated in any case. | |
14513 | */ | |
1c2a088a AS |
14514 | subprog = find_subprog(env, i + insn->imm + 1); |
14515 | if (subprog < 0) { | |
14516 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
14517 | i + insn->imm + 1); | |
14518 | return -EFAULT; | |
14519 | } | |
14520 | /* temporarily remember subprog id inside insn instead of | |
14521 | * aux_data, since next loop will split up all insns into funcs | |
14522 | */ | |
f910cefa | 14523 | insn->off = subprog; |
1c2a088a AS |
14524 | /* remember original imm in case JIT fails and fallback |
14525 | * to interpreter will be needed | |
14526 | */ | |
14527 | env->insn_aux_data[i].call_imm = insn->imm; | |
14528 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
14529 | insn->imm = 1; | |
3990ed4c MKL |
14530 | if (bpf_pseudo_func(insn)) |
14531 | /* jit (e.g. x86_64) may emit fewer instructions | |
14532 | * if it learns a u32 imm is the same as a u64 imm. | |
14533 | * Force a non zero here. | |
14534 | */ | |
14535 | insn[1].imm = 1; | |
1c2a088a AS |
14536 | } |
14537 | ||
c454a46b MKL |
14538 | err = bpf_prog_alloc_jited_linfo(prog); |
14539 | if (err) | |
14540 | goto out_undo_insn; | |
14541 | ||
14542 | err = -ENOMEM; | |
6396bb22 | 14543 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 14544 | if (!func) |
c7a89784 | 14545 | goto out_undo_insn; |
1c2a088a | 14546 | |
f910cefa | 14547 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 14548 | subprog_start = subprog_end; |
4cb3d99c | 14549 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
14550 | |
14551 | len = subprog_end - subprog_start; | |
fb7dd8bc | 14552 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
14553 | * hence main prog stats include the runtime of subprogs. |
14554 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 14555 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
14556 | */ |
14557 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
14558 | if (!func[i]) |
14559 | goto out_free; | |
14560 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
14561 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 14562 | func[i]->type = prog->type; |
1c2a088a | 14563 | func[i]->len = len; |
4f74d809 DB |
14564 | if (bpf_prog_calc_tag(func[i])) |
14565 | goto out_free; | |
1c2a088a | 14566 | func[i]->is_func = 1; |
ba64e7d8 | 14567 | func[i]->aux->func_idx = i; |
f263a814 | 14568 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
14569 | func[i]->aux->btf = prog->aux->btf; |
14570 | func[i]->aux->func_info = prog->aux->func_info; | |
9c7c48d6 | 14571 | func[i]->aux->func_info_cnt = prog->aux->func_info_cnt; |
f263a814 JF |
14572 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
14573 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 14574 | |
a748c697 | 14575 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 14576 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 14577 | |
f263a814 JF |
14578 | poke = &prog->aux->poke_tab[j]; |
14579 | if (poke->insn_idx < subprog_end && | |
14580 | poke->insn_idx >= subprog_start) | |
14581 | poke->aux = func[i]->aux; | |
a748c697 MF |
14582 | } |
14583 | ||
1c2a088a | 14584 | func[i]->aux->name[0] = 'F'; |
9c8105bd | 14585 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 14586 | func[i]->jit_requested = 1; |
d2a3b7c5 | 14587 | func[i]->blinding_requested = prog->blinding_requested; |
e6ac2450 | 14588 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 14589 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
14590 | func[i]->aux->linfo = prog->aux->linfo; |
14591 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
14592 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
14593 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
14594 | num_exentries = 0; |
14595 | insn = func[i]->insnsi; | |
14596 | for (j = 0; j < func[i]->len; j++, insn++) { | |
14597 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
14598 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
14599 | num_exentries++; | |
14600 | } | |
14601 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 14602 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
14603 | func[i] = bpf_int_jit_compile(func[i]); |
14604 | if (!func[i]->jited) { | |
14605 | err = -ENOTSUPP; | |
14606 | goto out_free; | |
14607 | } | |
14608 | cond_resched(); | |
14609 | } | |
a748c697 | 14610 | |
1c2a088a AS |
14611 | /* at this point all bpf functions were successfully JITed |
14612 | * now populate all bpf_calls with correct addresses and | |
14613 | * run last pass of JIT | |
14614 | */ | |
f910cefa | 14615 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
14616 | insn = func[i]->insnsi; |
14617 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 14618 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 14619 | subprog = insn->off; |
69c087ba YS |
14620 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
14621 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
14622 | continue; | |
14623 | } | |
23a2d70c | 14624 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
14625 | continue; |
14626 | subprog = insn->off; | |
3d717fad | 14627 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 14628 | } |
2162fed4 SD |
14629 | |
14630 | /* we use the aux data to keep a list of the start addresses | |
14631 | * of the JITed images for each function in the program | |
14632 | * | |
14633 | * for some architectures, such as powerpc64, the imm field | |
14634 | * might not be large enough to hold the offset of the start | |
14635 | * address of the callee's JITed image from __bpf_call_base | |
14636 | * | |
14637 | * in such cases, we can lookup the start address of a callee | |
14638 | * by using its subprog id, available from the off field of | |
14639 | * the call instruction, as an index for this list | |
14640 | */ | |
14641 | func[i]->aux->func = func; | |
14642 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 14643 | } |
f910cefa | 14644 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
14645 | old_bpf_func = func[i]->bpf_func; |
14646 | tmp = bpf_int_jit_compile(func[i]); | |
14647 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
14648 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 14649 | err = -ENOTSUPP; |
1c2a088a AS |
14650 | goto out_free; |
14651 | } | |
14652 | cond_resched(); | |
14653 | } | |
14654 | ||
14655 | /* finally lock prog and jit images for all functions and | |
14656 | * populate kallsysm | |
14657 | */ | |
f910cefa | 14658 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
14659 | bpf_prog_lock_ro(func[i]); |
14660 | bpf_prog_kallsyms_add(func[i]); | |
14661 | } | |
7105e828 DB |
14662 | |
14663 | /* Last step: make now unused interpreter insns from main | |
14664 | * prog consistent for later dump requests, so they can | |
14665 | * later look the same as if they were interpreted only. | |
14666 | */ | |
14667 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
14668 | if (bpf_pseudo_func(insn)) { |
14669 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
14670 | insn[1].imm = insn->off; |
14671 | insn->off = 0; | |
69c087ba YS |
14672 | continue; |
14673 | } | |
23a2d70c | 14674 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
14675 | continue; |
14676 | insn->off = env->insn_aux_data[i].call_imm; | |
14677 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 14678 | insn->imm = subprog; |
7105e828 DB |
14679 | } |
14680 | ||
1c2a088a AS |
14681 | prog->jited = 1; |
14682 | prog->bpf_func = func[0]->bpf_func; | |
d00c6473 | 14683 | prog->jited_len = func[0]->jited_len; |
1c2a088a | 14684 | prog->aux->func = func; |
f910cefa | 14685 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 14686 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
14687 | return 0; |
14688 | out_free: | |
f263a814 JF |
14689 | /* We failed JIT'ing, so at this point we need to unregister poke |
14690 | * descriptors from subprogs, so that kernel is not attempting to | |
14691 | * patch it anymore as we're freeing the subprog JIT memory. | |
14692 | */ | |
14693 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
14694 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
14695 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
14696 | } | |
14697 | /* At this point we're guaranteed that poke descriptors are not | |
14698 | * live anymore. We can just unlink its descriptor table as it's | |
14699 | * released with the main prog. | |
14700 | */ | |
a748c697 MF |
14701 | for (i = 0; i < env->subprog_cnt; i++) { |
14702 | if (!func[i]) | |
14703 | continue; | |
f263a814 | 14704 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
14705 | bpf_jit_free(func[i]); |
14706 | } | |
1c2a088a | 14707 | kfree(func); |
c7a89784 | 14708 | out_undo_insn: |
1c2a088a AS |
14709 | /* cleanup main prog to be interpreted */ |
14710 | prog->jit_requested = 0; | |
d2a3b7c5 | 14711 | prog->blinding_requested = 0; |
1c2a088a | 14712 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
23a2d70c | 14713 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
14714 | continue; |
14715 | insn->off = 0; | |
14716 | insn->imm = env->insn_aux_data[i].call_imm; | |
14717 | } | |
e16301fb | 14718 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
14719 | return err; |
14720 | } | |
14721 | ||
1ea47e01 AS |
14722 | static int fixup_call_args(struct bpf_verifier_env *env) |
14723 | { | |
19d28fbd | 14724 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
14725 | struct bpf_prog *prog = env->prog; |
14726 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 14727 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 14728 | int i, depth; |
19d28fbd | 14729 | #endif |
e4052d06 | 14730 | int err = 0; |
1ea47e01 | 14731 | |
e4052d06 QM |
14732 | if (env->prog->jit_requested && |
14733 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
14734 | err = jit_subprogs(env); |
14735 | if (err == 0) | |
1c2a088a | 14736 | return 0; |
c7a89784 DB |
14737 | if (err == -EFAULT) |
14738 | return err; | |
19d28fbd DM |
14739 | } |
14740 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
14741 | if (has_kfunc_call) { |
14742 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
14743 | return -EINVAL; | |
14744 | } | |
e411901c MF |
14745 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
14746 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
14747 | * have to be rejected, since interpreter doesn't support them yet. | |
14748 | */ | |
14749 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
14750 | return -EINVAL; | |
14751 | } | |
1ea47e01 | 14752 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
14753 | if (bpf_pseudo_func(insn)) { |
14754 | /* When JIT fails the progs with callback calls | |
14755 | * have to be rejected, since interpreter doesn't support them yet. | |
14756 | */ | |
14757 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
14758 | return -EINVAL; | |
14759 | } | |
14760 | ||
23a2d70c | 14761 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
14762 | continue; |
14763 | depth = get_callee_stack_depth(env, insn, i); | |
14764 | if (depth < 0) | |
14765 | return depth; | |
14766 | bpf_patch_call_args(insn, depth); | |
14767 | } | |
19d28fbd DM |
14768 | err = 0; |
14769 | #endif | |
14770 | return err; | |
1ea47e01 AS |
14771 | } |
14772 | ||
958cf2e2 KKD |
14773 | static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
14774 | struct bpf_insn *insn_buf, int insn_idx, int *cnt) | |
e6ac2450 MKL |
14775 | { |
14776 | const struct bpf_kfunc_desc *desc; | |
14777 | ||
a5d82727 KKD |
14778 | if (!insn->imm) { |
14779 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
14780 | return -EINVAL; | |
14781 | } | |
14782 | ||
e6ac2450 MKL |
14783 | /* insn->imm has the btf func_id. Replace it with |
14784 | * an address (relative to __bpf_base_call). | |
14785 | */ | |
2357672c | 14786 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
14787 | if (!desc) { |
14788 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
14789 | insn->imm); | |
14790 | return -EFAULT; | |
14791 | } | |
14792 | ||
958cf2e2 | 14793 | *cnt = 0; |
e6ac2450 | 14794 | insn->imm = desc->imm; |
958cf2e2 KKD |
14795 | if (insn->off) |
14796 | return 0; | |
14797 | if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { | |
14798 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
14799 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
14800 | u64 obj_new_size = env->insn_aux_data[insn_idx].obj_new_size; | |
e6ac2450 | 14801 | |
958cf2e2 KKD |
14802 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_1, obj_new_size); |
14803 | insn_buf[1] = addr[0]; | |
14804 | insn_buf[2] = addr[1]; | |
14805 | insn_buf[3] = *insn; | |
14806 | *cnt = 4; | |
ac9f0605 KKD |
14807 | } else if (desc->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) { |
14808 | struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; | |
14809 | struct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) }; | |
14810 | ||
14811 | insn_buf[0] = addr[0]; | |
14812 | insn_buf[1] = addr[1]; | |
14813 | insn_buf[2] = *insn; | |
14814 | *cnt = 3; | |
958cf2e2 | 14815 | } |
e6ac2450 MKL |
14816 | return 0; |
14817 | } | |
14818 | ||
e6ac5933 BJ |
14819 | /* Do various post-verification rewrites in a single program pass. |
14820 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 14821 | */ |
e6ac5933 | 14822 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 14823 | { |
79741b3b | 14824 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 14825 | enum bpf_attach_type eatype = prog->expected_attach_type; |
9b99edca | 14826 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 14827 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 14828 | const struct bpf_func_proto *fn; |
79741b3b | 14829 | const int insn_cnt = prog->len; |
09772d92 | 14830 | const struct bpf_map_ops *ops; |
c93552c4 | 14831 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
14832 | struct bpf_insn insn_buf[16]; |
14833 | struct bpf_prog *new_prog; | |
14834 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 14835 | int i, ret, cnt, delta = 0; |
e245c5c6 | 14836 | |
79741b3b | 14837 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 14838 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
14839 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
14840 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
14841 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 14842 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 14843 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
14844 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
14845 | struct bpf_insn *patchlet; | |
14846 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 14847 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
14848 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
14849 | BPF_JNE | BPF_K, insn->src_reg, | |
14850 | 0, 2, 0), | |
f6b1b3bf DB |
14851 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
14852 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
14853 | *insn, | |
14854 | }; | |
e88b2c6e | 14855 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 14856 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
14857 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
14858 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 14859 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 14860 | *insn, |
9b00f1b7 DB |
14861 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
14862 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 14863 | }; |
f6b1b3bf | 14864 | |
e88b2c6e DB |
14865 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
14866 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 14867 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
14868 | |
14869 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
14870 | if (!new_prog) |
14871 | return -ENOMEM; | |
14872 | ||
14873 | delta += cnt - 1; | |
14874 | env->prog = prog = new_prog; | |
14875 | insn = new_prog->insnsi + i + delta; | |
14876 | continue; | |
14877 | } | |
14878 | ||
e6ac5933 | 14879 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
14880 | if (BPF_CLASS(insn->code) == BPF_LD && |
14881 | (BPF_MODE(insn->code) == BPF_ABS || | |
14882 | BPF_MODE(insn->code) == BPF_IND)) { | |
14883 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
14884 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
14885 | verbose(env, "bpf verifier is misconfigured\n"); | |
14886 | return -EINVAL; | |
14887 | } | |
14888 | ||
14889 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14890 | if (!new_prog) | |
14891 | return -ENOMEM; | |
14892 | ||
14893 | delta += cnt - 1; | |
14894 | env->prog = prog = new_prog; | |
14895 | insn = new_prog->insnsi + i + delta; | |
14896 | continue; | |
14897 | } | |
14898 | ||
e6ac5933 | 14899 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
14900 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
14901 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
14902 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
14903 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 14904 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 14905 | bool issrc, isneg, isimm; |
979d63d5 DB |
14906 | u32 off_reg; |
14907 | ||
14908 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
14909 | if (!aux->alu_state || |
14910 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
14911 | continue; |
14912 | ||
14913 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
14914 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
14915 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 14916 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
14917 | |
14918 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
14919 | if (isimm) { |
14920 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
14921 | } else { | |
14922 | if (isneg) | |
14923 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
14924 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
14925 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
14926 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
14927 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
14928 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
14929 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
14930 | } | |
b9b34ddb DB |
14931 | if (!issrc) |
14932 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
14933 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
14934 | if (isneg) |
14935 | insn->code = insn->code == code_add ? | |
14936 | code_sub : code_add; | |
14937 | *patch++ = *insn; | |
801c6058 | 14938 | if (issrc && isneg && !isimm) |
979d63d5 DB |
14939 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
14940 | cnt = patch - insn_buf; | |
14941 | ||
14942 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14943 | if (!new_prog) | |
14944 | return -ENOMEM; | |
14945 | ||
14946 | delta += cnt - 1; | |
14947 | env->prog = prog = new_prog; | |
14948 | insn = new_prog->insnsi + i + delta; | |
14949 | continue; | |
14950 | } | |
14951 | ||
79741b3b AS |
14952 | if (insn->code != (BPF_JMP | BPF_CALL)) |
14953 | continue; | |
cc8b0b92 AS |
14954 | if (insn->src_reg == BPF_PSEUDO_CALL) |
14955 | continue; | |
e6ac2450 | 14956 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
958cf2e2 | 14957 | ret = fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt); |
e6ac2450 MKL |
14958 | if (ret) |
14959 | return ret; | |
958cf2e2 KKD |
14960 | if (cnt == 0) |
14961 | continue; | |
14962 | ||
14963 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
14964 | if (!new_prog) | |
14965 | return -ENOMEM; | |
14966 | ||
14967 | delta += cnt - 1; | |
14968 | env->prog = prog = new_prog; | |
14969 | insn = new_prog->insnsi + i + delta; | |
e6ac2450 MKL |
14970 | continue; |
14971 | } | |
e245c5c6 | 14972 | |
79741b3b AS |
14973 | if (insn->imm == BPF_FUNC_get_route_realm) |
14974 | prog->dst_needed = 1; | |
14975 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
14976 | bpf_user_rnd_init_once(); | |
9802d865 JB |
14977 | if (insn->imm == BPF_FUNC_override_return) |
14978 | prog->kprobe_override = 1; | |
79741b3b | 14979 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
14980 | /* If we tail call into other programs, we |
14981 | * cannot make any assumptions since they can | |
14982 | * be replaced dynamically during runtime in | |
14983 | * the program array. | |
14984 | */ | |
14985 | prog->cb_access = 1; | |
e411901c MF |
14986 | if (!allow_tail_call_in_subprogs(env)) |
14987 | prog->aux->stack_depth = MAX_BPF_STACK; | |
14988 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 14989 | |
79741b3b | 14990 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 14991 | * conditional branch in the interpreter for every normal |
79741b3b AS |
14992 | * call and to prevent accidental JITing by JIT compiler |
14993 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 14994 | */ |
79741b3b | 14995 | insn->imm = 0; |
71189fa9 | 14996 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 14997 | |
c93552c4 | 14998 | aux = &env->insn_aux_data[i + delta]; |
d2a3b7c5 | 14999 | if (env->bpf_capable && !prog->blinding_requested && |
cc52d914 | 15000 | prog->jit_requested && |
d2e4c1e6 DB |
15001 | !bpf_map_key_poisoned(aux) && |
15002 | !bpf_map_ptr_poisoned(aux) && | |
15003 | !bpf_map_ptr_unpriv(aux)) { | |
15004 | struct bpf_jit_poke_descriptor desc = { | |
15005 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
15006 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
15007 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 15008 | .insn_idx = i + delta, |
d2e4c1e6 DB |
15009 | }; |
15010 | ||
15011 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
15012 | if (ret < 0) { | |
15013 | verbose(env, "adding tail call poke descriptor failed\n"); | |
15014 | return ret; | |
15015 | } | |
15016 | ||
15017 | insn->imm = ret + 1; | |
15018 | continue; | |
15019 | } | |
15020 | ||
c93552c4 DB |
15021 | if (!bpf_map_ptr_unpriv(aux)) |
15022 | continue; | |
15023 | ||
b2157399 AS |
15024 | /* instead of changing every JIT dealing with tail_call |
15025 | * emit two extra insns: | |
15026 | * if (index >= max_entries) goto out; | |
15027 | * index &= array->index_mask; | |
15028 | * to avoid out-of-bounds cpu speculation | |
15029 | */ | |
c93552c4 | 15030 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 15031 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
15032 | return -EINVAL; |
15033 | } | |
c93552c4 | 15034 | |
d2e4c1e6 | 15035 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
15036 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
15037 | map_ptr->max_entries, 2); | |
15038 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
15039 | container_of(map_ptr, | |
15040 | struct bpf_array, | |
15041 | map)->index_mask); | |
15042 | insn_buf[2] = *insn; | |
15043 | cnt = 3; | |
15044 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15045 | if (!new_prog) | |
15046 | return -ENOMEM; | |
15047 | ||
15048 | delta += cnt - 1; | |
15049 | env->prog = prog = new_prog; | |
15050 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
15051 | continue; |
15052 | } | |
e245c5c6 | 15053 | |
b00628b1 AS |
15054 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
15055 | /* The verifier will process callback_fn as many times as necessary | |
15056 | * with different maps and the register states prepared by | |
15057 | * set_timer_callback_state will be accurate. | |
15058 | * | |
15059 | * The following use case is valid: | |
15060 | * map1 is shared by prog1, prog2, prog3. | |
15061 | * prog1 calls bpf_timer_init for some map1 elements | |
15062 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
15063 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
15064 | * prog3 calls bpf_timer_start for some map1 elements. | |
15065 | * Those that were not both bpf_timer_init-ed and | |
15066 | * bpf_timer_set_callback-ed will return -EINVAL. | |
15067 | */ | |
15068 | struct bpf_insn ld_addrs[2] = { | |
15069 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
15070 | }; | |
15071 | ||
15072 | insn_buf[0] = ld_addrs[0]; | |
15073 | insn_buf[1] = ld_addrs[1]; | |
15074 | insn_buf[2] = *insn; | |
15075 | cnt = 3; | |
15076 | ||
15077 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15078 | if (!new_prog) | |
15079 | return -ENOMEM; | |
15080 | ||
15081 | delta += cnt - 1; | |
15082 | env->prog = prog = new_prog; | |
15083 | insn = new_prog->insnsi + i + delta; | |
15084 | goto patch_call_imm; | |
15085 | } | |
15086 | ||
b00fa38a JK |
15087 | if (insn->imm == BPF_FUNC_task_storage_get || |
15088 | insn->imm == BPF_FUNC_sk_storage_get || | |
c4bcfb38 YS |
15089 | insn->imm == BPF_FUNC_inode_storage_get || |
15090 | insn->imm == BPF_FUNC_cgrp_storage_get) { | |
b00fa38a | 15091 | if (env->prog->aux->sleepable) |
d56c9fe6 | 15092 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); |
b00fa38a | 15093 | else |
d56c9fe6 | 15094 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); |
b00fa38a JK |
15095 | insn_buf[1] = *insn; |
15096 | cnt = 2; | |
15097 | ||
15098 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15099 | if (!new_prog) | |
15100 | return -ENOMEM; | |
15101 | ||
15102 | delta += cnt - 1; | |
15103 | env->prog = prog = new_prog; | |
15104 | insn = new_prog->insnsi + i + delta; | |
15105 | goto patch_call_imm; | |
15106 | } | |
15107 | ||
89c63074 | 15108 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
15109 | * and other inlining handlers are currently limited to 64 bit |
15110 | * only. | |
89c63074 | 15111 | */ |
60b58afc | 15112 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
15113 | (insn->imm == BPF_FUNC_map_lookup_elem || |
15114 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
15115 | insn->imm == BPF_FUNC_map_delete_elem || |
15116 | insn->imm == BPF_FUNC_map_push_elem || | |
15117 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 15118 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c | 15119 | insn->imm == BPF_FUNC_redirect_map || |
07343110 FZ |
15120 | insn->imm == BPF_FUNC_for_each_map_elem || |
15121 | insn->imm == BPF_FUNC_map_lookup_percpu_elem)) { | |
c93552c4 DB |
15122 | aux = &env->insn_aux_data[i + delta]; |
15123 | if (bpf_map_ptr_poisoned(aux)) | |
15124 | goto patch_call_imm; | |
15125 | ||
d2e4c1e6 | 15126 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
15127 | ops = map_ptr->ops; |
15128 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
15129 | ops->map_gen_lookup) { | |
15130 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
15131 | if (cnt == -EOPNOTSUPP) |
15132 | goto patch_map_ops_generic; | |
15133 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
15134 | verbose(env, "bpf verifier is misconfigured\n"); |
15135 | return -EINVAL; | |
15136 | } | |
81ed18ab | 15137 | |
09772d92 DB |
15138 | new_prog = bpf_patch_insn_data(env, i + delta, |
15139 | insn_buf, cnt); | |
15140 | if (!new_prog) | |
15141 | return -ENOMEM; | |
81ed18ab | 15142 | |
09772d92 DB |
15143 | delta += cnt - 1; |
15144 | env->prog = prog = new_prog; | |
15145 | insn = new_prog->insnsi + i + delta; | |
15146 | continue; | |
15147 | } | |
81ed18ab | 15148 | |
09772d92 DB |
15149 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
15150 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
15151 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
15152 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
15153 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
15154 | (int (*)(struct bpf_map *map, void *key, void *value, | |
15155 | u64 flags))NULL)); | |
84430d42 DB |
15156 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
15157 | (int (*)(struct bpf_map *map, void *value, | |
15158 | u64 flags))NULL)); | |
15159 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
15160 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
15161 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
15162 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
e6a4750f | 15163 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
32637e33 | 15164 | (int (*)(struct bpf_map *map, u64 index, u64 flags))NULL)); |
0640c77c AI |
15165 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
15166 | (int (*)(struct bpf_map *map, | |
15167 | bpf_callback_t callback_fn, | |
15168 | void *callback_ctx, | |
15169 | u64 flags))NULL)); | |
07343110 FZ |
15170 | BUILD_BUG_ON(!__same_type(ops->map_lookup_percpu_elem, |
15171 | (void *(*)(struct bpf_map *map, void *key, u32 cpu))NULL)); | |
e6a4750f | 15172 | |
4a8f87e6 | 15173 | patch_map_ops_generic: |
09772d92 DB |
15174 | switch (insn->imm) { |
15175 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 15176 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
15177 | continue; |
15178 | case BPF_FUNC_map_update_elem: | |
3d717fad | 15179 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
15180 | continue; |
15181 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 15182 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 15183 | continue; |
84430d42 | 15184 | case BPF_FUNC_map_push_elem: |
3d717fad | 15185 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
15186 | continue; |
15187 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 15188 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
15189 | continue; |
15190 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 15191 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 15192 | continue; |
e6a4750f | 15193 | case BPF_FUNC_redirect_map: |
3d717fad | 15194 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 15195 | continue; |
0640c77c AI |
15196 | case BPF_FUNC_for_each_map_elem: |
15197 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 15198 | continue; |
07343110 FZ |
15199 | case BPF_FUNC_map_lookup_percpu_elem: |
15200 | insn->imm = BPF_CALL_IMM(ops->map_lookup_percpu_elem); | |
15201 | continue; | |
09772d92 | 15202 | } |
81ed18ab | 15203 | |
09772d92 | 15204 | goto patch_call_imm; |
81ed18ab AS |
15205 | } |
15206 | ||
e6ac5933 | 15207 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
15208 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
15209 | insn->imm == BPF_FUNC_jiffies64) { | |
15210 | struct bpf_insn ld_jiffies_addr[2] = { | |
15211 | BPF_LD_IMM64(BPF_REG_0, | |
15212 | (unsigned long)&jiffies), | |
15213 | }; | |
15214 | ||
15215 | insn_buf[0] = ld_jiffies_addr[0]; | |
15216 | insn_buf[1] = ld_jiffies_addr[1]; | |
15217 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
15218 | BPF_REG_0, 0); | |
15219 | cnt = 3; | |
15220 | ||
15221 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
15222 | cnt); | |
15223 | if (!new_prog) | |
15224 | return -ENOMEM; | |
15225 | ||
15226 | delta += cnt - 1; | |
15227 | env->prog = prog = new_prog; | |
15228 | insn = new_prog->insnsi + i + delta; | |
15229 | continue; | |
15230 | } | |
15231 | ||
f92c1e18 JO |
15232 | /* Implement bpf_get_func_arg inline. */ |
15233 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
15234 | insn->imm == BPF_FUNC_get_func_arg) { | |
15235 | /* Load nr_args from ctx - 8 */ | |
15236 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
15237 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
15238 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
15239 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
15240 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
15241 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
15242 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
15243 | insn_buf[7] = BPF_JMP_A(1); | |
15244 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
15245 | cnt = 9; | |
15246 | ||
15247 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15248 | if (!new_prog) | |
15249 | return -ENOMEM; | |
15250 | ||
15251 | delta += cnt - 1; | |
15252 | env->prog = prog = new_prog; | |
15253 | insn = new_prog->insnsi + i + delta; | |
15254 | continue; | |
15255 | } | |
15256 | ||
15257 | /* Implement bpf_get_func_ret inline. */ | |
15258 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
15259 | insn->imm == BPF_FUNC_get_func_ret) { | |
15260 | if (eatype == BPF_TRACE_FEXIT || | |
15261 | eatype == BPF_MODIFY_RETURN) { | |
15262 | /* Load nr_args from ctx - 8 */ | |
15263 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
15264 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
15265 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
15266 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
15267 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
15268 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
15269 | cnt = 6; | |
15270 | } else { | |
15271 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
15272 | cnt = 1; | |
15273 | } | |
15274 | ||
15275 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
15276 | if (!new_prog) | |
15277 | return -ENOMEM; | |
15278 | ||
15279 | delta += cnt - 1; | |
15280 | env->prog = prog = new_prog; | |
15281 | insn = new_prog->insnsi + i + delta; | |
15282 | continue; | |
15283 | } | |
15284 | ||
15285 | /* Implement get_func_arg_cnt inline. */ | |
15286 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
15287 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
15288 | /* Load nr_args from ctx - 8 */ | |
15289 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
15290 | ||
15291 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
15292 | if (!new_prog) | |
15293 | return -ENOMEM; | |
15294 | ||
15295 | env->prog = prog = new_prog; | |
15296 | insn = new_prog->insnsi + i + delta; | |
15297 | continue; | |
15298 | } | |
15299 | ||
f705ec76 | 15300 | /* Implement bpf_get_func_ip inline. */ |
9b99edca JO |
15301 | if (prog_type == BPF_PROG_TYPE_TRACING && |
15302 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
15303 | /* Load IP address from ctx - 16 */ |
15304 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
15305 | |
15306 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
15307 | if (!new_prog) | |
15308 | return -ENOMEM; | |
15309 | ||
15310 | env->prog = prog = new_prog; | |
15311 | insn = new_prog->insnsi + i + delta; | |
15312 | continue; | |
15313 | } | |
15314 | ||
81ed18ab | 15315 | patch_call_imm: |
5e43f899 | 15316 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
15317 | /* all functions that have prototype and verifier allowed |
15318 | * programs to call them, must be real in-kernel functions | |
15319 | */ | |
15320 | if (!fn->func) { | |
61bd5218 JK |
15321 | verbose(env, |
15322 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
15323 | func_id_name(insn->imm), insn->imm); |
15324 | return -EFAULT; | |
e245c5c6 | 15325 | } |
79741b3b | 15326 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 15327 | } |
e245c5c6 | 15328 | |
d2e4c1e6 DB |
15329 | /* Since poke tab is now finalized, publish aux to tracker. */ |
15330 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
15331 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
15332 | if (!map_ptr->ops->map_poke_track || | |
15333 | !map_ptr->ops->map_poke_untrack || | |
15334 | !map_ptr->ops->map_poke_run) { | |
15335 | verbose(env, "bpf verifier is misconfigured\n"); | |
15336 | return -EINVAL; | |
15337 | } | |
15338 | ||
15339 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
15340 | if (ret < 0) { | |
15341 | verbose(env, "tracking tail call prog failed\n"); | |
15342 | return ret; | |
15343 | } | |
15344 | } | |
15345 | ||
e6ac2450 MKL |
15346 | sort_kfunc_descs_by_imm(env->prog); |
15347 | ||
79741b3b AS |
15348 | return 0; |
15349 | } | |
e245c5c6 | 15350 | |
1ade2371 EZ |
15351 | static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env, |
15352 | int position, | |
15353 | s32 stack_base, | |
15354 | u32 callback_subprogno, | |
15355 | u32 *cnt) | |
15356 | { | |
15357 | s32 r6_offset = stack_base + 0 * BPF_REG_SIZE; | |
15358 | s32 r7_offset = stack_base + 1 * BPF_REG_SIZE; | |
15359 | s32 r8_offset = stack_base + 2 * BPF_REG_SIZE; | |
15360 | int reg_loop_max = BPF_REG_6; | |
15361 | int reg_loop_cnt = BPF_REG_7; | |
15362 | int reg_loop_ctx = BPF_REG_8; | |
15363 | ||
15364 | struct bpf_prog *new_prog; | |
15365 | u32 callback_start; | |
15366 | u32 call_insn_offset; | |
15367 | s32 callback_offset; | |
15368 | ||
15369 | /* This represents an inlined version of bpf_iter.c:bpf_loop, | |
15370 | * be careful to modify this code in sync. | |
15371 | */ | |
15372 | struct bpf_insn insn_buf[] = { | |
15373 | /* Return error and jump to the end of the patch if | |
15374 | * expected number of iterations is too big. | |
15375 | */ | |
15376 | BPF_JMP_IMM(BPF_JLE, BPF_REG_1, BPF_MAX_LOOPS, 2), | |
15377 | BPF_MOV32_IMM(BPF_REG_0, -E2BIG), | |
15378 | BPF_JMP_IMM(BPF_JA, 0, 0, 16), | |
15379 | /* spill R6, R7, R8 to use these as loop vars */ | |
15380 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, r6_offset), | |
15381 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_7, r7_offset), | |
15382 | BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_8, r8_offset), | |
15383 | /* initialize loop vars */ | |
15384 | BPF_MOV64_REG(reg_loop_max, BPF_REG_1), | |
15385 | BPF_MOV32_IMM(reg_loop_cnt, 0), | |
15386 | BPF_MOV64_REG(reg_loop_ctx, BPF_REG_3), | |
15387 | /* loop header, | |
15388 | * if reg_loop_cnt >= reg_loop_max skip the loop body | |
15389 | */ | |
15390 | BPF_JMP_REG(BPF_JGE, reg_loop_cnt, reg_loop_max, 5), | |
15391 | /* callback call, | |
15392 | * correct callback offset would be set after patching | |
15393 | */ | |
15394 | BPF_MOV64_REG(BPF_REG_1, reg_loop_cnt), | |
15395 | BPF_MOV64_REG(BPF_REG_2, reg_loop_ctx), | |
15396 | BPF_CALL_REL(0), | |
15397 | /* increment loop counter */ | |
15398 | BPF_ALU64_IMM(BPF_ADD, reg_loop_cnt, 1), | |
15399 | /* jump to loop header if callback returned 0 */ | |
15400 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -6), | |
15401 | /* return value of bpf_loop, | |
15402 | * set R0 to the number of iterations | |
15403 | */ | |
15404 | BPF_MOV64_REG(BPF_REG_0, reg_loop_cnt), | |
15405 | /* restore original values of R6, R7, R8 */ | |
15406 | BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, r6_offset), | |
15407 | BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, r7_offset), | |
15408 | BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_10, r8_offset), | |
15409 | }; | |
15410 | ||
15411 | *cnt = ARRAY_SIZE(insn_buf); | |
15412 | new_prog = bpf_patch_insn_data(env, position, insn_buf, *cnt); | |
15413 | if (!new_prog) | |
15414 | return new_prog; | |
15415 | ||
15416 | /* callback start is known only after patching */ | |
15417 | callback_start = env->subprog_info[callback_subprogno].start; | |
15418 | /* Note: insn_buf[12] is an offset of BPF_CALL_REL instruction */ | |
15419 | call_insn_offset = position + 12; | |
15420 | callback_offset = callback_start - call_insn_offset - 1; | |
fb4e3b33 | 15421 | new_prog->insnsi[call_insn_offset].imm = callback_offset; |
1ade2371 EZ |
15422 | |
15423 | return new_prog; | |
15424 | } | |
15425 | ||
15426 | static bool is_bpf_loop_call(struct bpf_insn *insn) | |
15427 | { | |
15428 | return insn->code == (BPF_JMP | BPF_CALL) && | |
15429 | insn->src_reg == 0 && | |
15430 | insn->imm == BPF_FUNC_loop; | |
15431 | } | |
15432 | ||
15433 | /* For all sub-programs in the program (including main) check | |
15434 | * insn_aux_data to see if there are bpf_loop calls that require | |
15435 | * inlining. If such calls are found the calls are replaced with a | |
15436 | * sequence of instructions produced by `inline_bpf_loop` function and | |
15437 | * subprog stack_depth is increased by the size of 3 registers. | |
15438 | * This stack space is used to spill values of the R6, R7, R8. These | |
15439 | * registers are used to store the loop bound, counter and context | |
15440 | * variables. | |
15441 | */ | |
15442 | static int optimize_bpf_loop(struct bpf_verifier_env *env) | |
15443 | { | |
15444 | struct bpf_subprog_info *subprogs = env->subprog_info; | |
15445 | int i, cur_subprog = 0, cnt, delta = 0; | |
15446 | struct bpf_insn *insn = env->prog->insnsi; | |
15447 | int insn_cnt = env->prog->len; | |
15448 | u16 stack_depth = subprogs[cur_subprog].stack_depth; | |
15449 | u16 stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
15450 | u16 stack_depth_extra = 0; | |
15451 | ||
15452 | for (i = 0; i < insn_cnt; i++, insn++) { | |
15453 | struct bpf_loop_inline_state *inline_state = | |
15454 | &env->insn_aux_data[i + delta].loop_inline_state; | |
15455 | ||
15456 | if (is_bpf_loop_call(insn) && inline_state->fit_for_inline) { | |
15457 | struct bpf_prog *new_prog; | |
15458 | ||
15459 | stack_depth_extra = BPF_REG_SIZE * 3 + stack_depth_roundup; | |
15460 | new_prog = inline_bpf_loop(env, | |
15461 | i + delta, | |
15462 | -(stack_depth + stack_depth_extra), | |
15463 | inline_state->callback_subprogno, | |
15464 | &cnt); | |
15465 | if (!new_prog) | |
15466 | return -ENOMEM; | |
15467 | ||
15468 | delta += cnt - 1; | |
15469 | env->prog = new_prog; | |
15470 | insn = new_prog->insnsi + i + delta; | |
15471 | } | |
15472 | ||
15473 | if (subprogs[cur_subprog + 1].start == i + delta + 1) { | |
15474 | subprogs[cur_subprog].stack_depth += stack_depth_extra; | |
15475 | cur_subprog++; | |
15476 | stack_depth = subprogs[cur_subprog].stack_depth; | |
15477 | stack_depth_roundup = round_up(stack_depth, 8) - stack_depth; | |
15478 | stack_depth_extra = 0; | |
15479 | } | |
15480 | } | |
15481 | ||
15482 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
15483 | ||
15484 | return 0; | |
15485 | } | |
15486 | ||
58e2af8b | 15487 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 15488 | { |
58e2af8b | 15489 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
15490 | int i; |
15491 | ||
9f4686c4 AS |
15492 | sl = env->free_list; |
15493 | while (sl) { | |
15494 | sln = sl->next; | |
15495 | free_verifier_state(&sl->state, false); | |
15496 | kfree(sl); | |
15497 | sl = sln; | |
15498 | } | |
51c39bb1 | 15499 | env->free_list = NULL; |
9f4686c4 | 15500 | |
f1bca824 AS |
15501 | if (!env->explored_states) |
15502 | return; | |
15503 | ||
dc2a4ebc | 15504 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
15505 | sl = env->explored_states[i]; |
15506 | ||
a8f500af AS |
15507 | while (sl) { |
15508 | sln = sl->next; | |
15509 | free_verifier_state(&sl->state, false); | |
15510 | kfree(sl); | |
15511 | sl = sln; | |
15512 | } | |
51c39bb1 | 15513 | env->explored_states[i] = NULL; |
f1bca824 | 15514 | } |
51c39bb1 | 15515 | } |
f1bca824 | 15516 | |
51c39bb1 AS |
15517 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
15518 | { | |
6f8a57cc | 15519 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
15520 | struct bpf_verifier_state *state; |
15521 | struct bpf_reg_state *regs; | |
15522 | int ret, i; | |
15523 | ||
15524 | env->prev_linfo = NULL; | |
15525 | env->pass_cnt++; | |
15526 | ||
15527 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
15528 | if (!state) | |
15529 | return -ENOMEM; | |
15530 | state->curframe = 0; | |
15531 | state->speculative = false; | |
15532 | state->branches = 1; | |
15533 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
15534 | if (!state->frame[0]) { | |
15535 | kfree(state); | |
15536 | return -ENOMEM; | |
15537 | } | |
15538 | env->cur_state = state; | |
15539 | init_func_state(env, state->frame[0], | |
15540 | BPF_MAIN_FUNC /* callsite */, | |
15541 | 0 /* frameno */, | |
15542 | subprog); | |
be2ef816 AN |
15543 | state->first_insn_idx = env->subprog_info[subprog].start; |
15544 | state->last_insn_idx = -1; | |
51c39bb1 AS |
15545 | |
15546 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 15547 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
15548 | ret = btf_prepare_func_args(env, subprog, regs); |
15549 | if (ret) | |
15550 | goto out; | |
15551 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
15552 | if (regs[i].type == PTR_TO_CTX) | |
15553 | mark_reg_known_zero(env, regs, i); | |
15554 | else if (regs[i].type == SCALAR_VALUE) | |
15555 | mark_reg_unknown(env, regs, i); | |
cf9f2f8d | 15556 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
e5069b9c DB |
15557 | const u32 mem_size = regs[i].mem_size; |
15558 | ||
15559 | mark_reg_known_zero(env, regs, i); | |
15560 | regs[i].mem_size = mem_size; | |
15561 | regs[i].id = ++env->id_gen; | |
15562 | } | |
51c39bb1 AS |
15563 | } |
15564 | } else { | |
15565 | /* 1st arg to a function */ | |
15566 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
15567 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 15568 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
15569 | if (ret == -EFAULT) |
15570 | /* unlikely verifier bug. abort. | |
15571 | * ret == 0 and ret < 0 are sadly acceptable for | |
15572 | * main() function due to backward compatibility. | |
15573 | * Like socket filter program may be written as: | |
15574 | * int bpf_prog(struct pt_regs *ctx) | |
15575 | * and never dereference that ctx in the program. | |
15576 | * 'struct pt_regs' is a type mismatch for socket | |
15577 | * filter that should be using 'struct __sk_buff'. | |
15578 | */ | |
15579 | goto out; | |
15580 | } | |
15581 | ||
15582 | ret = do_check(env); | |
15583 | out: | |
f59bbfc2 AS |
15584 | /* check for NULL is necessary, since cur_state can be freed inside |
15585 | * do_check() under memory pressure. | |
15586 | */ | |
15587 | if (env->cur_state) { | |
15588 | free_verifier_state(env->cur_state, true); | |
15589 | env->cur_state = NULL; | |
15590 | } | |
6f8a57cc AN |
15591 | while (!pop_stack(env, NULL, NULL, false)); |
15592 | if (!ret && pop_log) | |
15593 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 15594 | free_states(env); |
51c39bb1 AS |
15595 | return ret; |
15596 | } | |
15597 | ||
15598 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
15599 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
15600 | * Consider: | |
15601 | * int bar(int); | |
15602 | * int foo(int f) | |
15603 | * { | |
15604 | * return bar(f); | |
15605 | * } | |
15606 | * int bar(int b) | |
15607 | * { | |
15608 | * ... | |
15609 | * } | |
15610 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
15611 | * will be assumed that bar() already verified successfully and call to bar() | |
15612 | * from foo() will be checked for type match only. Later bar() will be verified | |
15613 | * independently to check that it's safe for R1=any_scalar_value. | |
15614 | */ | |
15615 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
15616 | { | |
15617 | struct bpf_prog_aux *aux = env->prog->aux; | |
15618 | int i, ret; | |
15619 | ||
15620 | if (!aux->func_info) | |
15621 | return 0; | |
15622 | ||
15623 | for (i = 1; i < env->subprog_cnt; i++) { | |
15624 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
15625 | continue; | |
15626 | env->insn_idx = env->subprog_info[i].start; | |
15627 | WARN_ON_ONCE(env->insn_idx == 0); | |
15628 | ret = do_check_common(env, i); | |
15629 | if (ret) { | |
15630 | return ret; | |
15631 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
15632 | verbose(env, | |
15633 | "Func#%d is safe for any args that match its prototype\n", | |
15634 | i); | |
15635 | } | |
15636 | } | |
15637 | return 0; | |
15638 | } | |
15639 | ||
15640 | static int do_check_main(struct bpf_verifier_env *env) | |
15641 | { | |
15642 | int ret; | |
15643 | ||
15644 | env->insn_idx = 0; | |
15645 | ret = do_check_common(env, 0); | |
15646 | if (!ret) | |
15647 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
15648 | return ret; | |
15649 | } | |
15650 | ||
15651 | ||
06ee7115 AS |
15652 | static void print_verification_stats(struct bpf_verifier_env *env) |
15653 | { | |
15654 | int i; | |
15655 | ||
15656 | if (env->log.level & BPF_LOG_STATS) { | |
15657 | verbose(env, "verification time %lld usec\n", | |
15658 | div_u64(env->verification_time, 1000)); | |
15659 | verbose(env, "stack depth "); | |
15660 | for (i = 0; i < env->subprog_cnt; i++) { | |
15661 | u32 depth = env->subprog_info[i].stack_depth; | |
15662 | ||
15663 | verbose(env, "%d", depth); | |
15664 | if (i + 1 < env->subprog_cnt) | |
15665 | verbose(env, "+"); | |
15666 | } | |
15667 | verbose(env, "\n"); | |
15668 | } | |
15669 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
15670 | "total_states %d peak_states %d mark_read %d\n", | |
15671 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
15672 | env->max_states_per_insn, env->total_states, | |
15673 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
15674 | } |
15675 | ||
27ae7997 MKL |
15676 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
15677 | { | |
15678 | const struct btf_type *t, *func_proto; | |
15679 | const struct bpf_struct_ops *st_ops; | |
15680 | const struct btf_member *member; | |
15681 | struct bpf_prog *prog = env->prog; | |
15682 | u32 btf_id, member_idx; | |
15683 | const char *mname; | |
15684 | ||
12aa8a94 THJ |
15685 | if (!prog->gpl_compatible) { |
15686 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
15687 | return -EINVAL; | |
15688 | } | |
15689 | ||
27ae7997 MKL |
15690 | btf_id = prog->aux->attach_btf_id; |
15691 | st_ops = bpf_struct_ops_find(btf_id); | |
15692 | if (!st_ops) { | |
15693 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
15694 | btf_id); | |
15695 | return -ENOTSUPP; | |
15696 | } | |
15697 | ||
15698 | t = st_ops->type; | |
15699 | member_idx = prog->expected_attach_type; | |
15700 | if (member_idx >= btf_type_vlen(t)) { | |
15701 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
15702 | member_idx, st_ops->name); | |
15703 | return -EINVAL; | |
15704 | } | |
15705 | ||
15706 | member = &btf_type_member(t)[member_idx]; | |
15707 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
15708 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
15709 | NULL); | |
15710 | if (!func_proto) { | |
15711 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
15712 | mname, member_idx, st_ops->name); | |
15713 | return -EINVAL; | |
15714 | } | |
15715 | ||
15716 | if (st_ops->check_member) { | |
15717 | int err = st_ops->check_member(t, member); | |
15718 | ||
15719 | if (err) { | |
15720 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
15721 | mname, st_ops->name); | |
15722 | return err; | |
15723 | } | |
15724 | } | |
15725 | ||
15726 | prog->aux->attach_func_proto = func_proto; | |
15727 | prog->aux->attach_func_name = mname; | |
15728 | env->ops = st_ops->verifier_ops; | |
15729 | ||
15730 | return 0; | |
15731 | } | |
6ba43b76 KS |
15732 | #define SECURITY_PREFIX "security_" |
15733 | ||
f7b12b6f | 15734 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 15735 | { |
69191754 | 15736 | if (within_error_injection_list(addr) || |
f7b12b6f | 15737 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 15738 | return 0; |
6ba43b76 | 15739 | |
6ba43b76 KS |
15740 | return -EINVAL; |
15741 | } | |
27ae7997 | 15742 | |
1e6c62a8 AS |
15743 | /* list of non-sleepable functions that are otherwise on |
15744 | * ALLOW_ERROR_INJECTION list | |
15745 | */ | |
15746 | BTF_SET_START(btf_non_sleepable_error_inject) | |
15747 | /* Three functions below can be called from sleepable and non-sleepable context. | |
15748 | * Assume non-sleepable from bpf safety point of view. | |
15749 | */ | |
9dd3d069 | 15750 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
15751 | BTF_ID(func, should_fail_alloc_page) |
15752 | BTF_ID(func, should_failslab) | |
15753 | BTF_SET_END(btf_non_sleepable_error_inject) | |
15754 | ||
15755 | static int check_non_sleepable_error_inject(u32 btf_id) | |
15756 | { | |
15757 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
15758 | } | |
15759 | ||
f7b12b6f THJ |
15760 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
15761 | const struct bpf_prog *prog, | |
15762 | const struct bpf_prog *tgt_prog, | |
15763 | u32 btf_id, | |
15764 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 15765 | { |
be8704ff | 15766 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 15767 | const char prefix[] = "btf_trace_"; |
5b92a28a | 15768 | int ret = 0, subprog = -1, i; |
38207291 | 15769 | const struct btf_type *t; |
5b92a28a | 15770 | bool conservative = true; |
38207291 | 15771 | const char *tname; |
5b92a28a | 15772 | struct btf *btf; |
f7b12b6f | 15773 | long addr = 0; |
38207291 | 15774 | |
f1b9509c | 15775 | if (!btf_id) { |
efc68158 | 15776 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
15777 | return -EINVAL; |
15778 | } | |
22dc4a0f | 15779 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 15780 | if (!btf) { |
efc68158 | 15781 | bpf_log(log, |
5b92a28a AS |
15782 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
15783 | return -EINVAL; | |
15784 | } | |
15785 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 15786 | if (!t) { |
efc68158 | 15787 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
15788 | return -EINVAL; |
15789 | } | |
5b92a28a | 15790 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 15791 | if (!tname) { |
efc68158 | 15792 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
15793 | return -EINVAL; |
15794 | } | |
5b92a28a AS |
15795 | if (tgt_prog) { |
15796 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
15797 | ||
15798 | for (i = 0; i < aux->func_info_cnt; i++) | |
15799 | if (aux->func_info[i].type_id == btf_id) { | |
15800 | subprog = i; | |
15801 | break; | |
15802 | } | |
15803 | if (subprog == -1) { | |
efc68158 | 15804 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
15805 | return -EINVAL; |
15806 | } | |
15807 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
15808 | if (prog_extension) { |
15809 | if (conservative) { | |
efc68158 | 15810 | bpf_log(log, |
be8704ff AS |
15811 | "Cannot replace static functions\n"); |
15812 | return -EINVAL; | |
15813 | } | |
15814 | if (!prog->jit_requested) { | |
efc68158 | 15815 | bpf_log(log, |
be8704ff AS |
15816 | "Extension programs should be JITed\n"); |
15817 | return -EINVAL; | |
15818 | } | |
be8704ff AS |
15819 | } |
15820 | if (!tgt_prog->jited) { | |
efc68158 | 15821 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
15822 | return -EINVAL; |
15823 | } | |
15824 | if (tgt_prog->type == prog->type) { | |
15825 | /* Cannot fentry/fexit another fentry/fexit program. | |
15826 | * Cannot attach program extension to another extension. | |
15827 | * It's ok to attach fentry/fexit to extension program. | |
15828 | */ | |
efc68158 | 15829 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
15830 | return -EINVAL; |
15831 | } | |
15832 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
15833 | prog_extension && | |
15834 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
15835 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
15836 | /* Program extensions can extend all program types | |
15837 | * except fentry/fexit. The reason is the following. | |
15838 | * The fentry/fexit programs are used for performance | |
15839 | * analysis, stats and can be attached to any program | |
15840 | * type except themselves. When extension program is | |
15841 | * replacing XDP function it is necessary to allow | |
15842 | * performance analysis of all functions. Both original | |
15843 | * XDP program and its program extension. Hence | |
15844 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
15845 | * allowed. If extending of fentry/fexit was allowed it | |
15846 | * would be possible to create long call chain | |
15847 | * fentry->extension->fentry->extension beyond | |
15848 | * reasonable stack size. Hence extending fentry is not | |
15849 | * allowed. | |
15850 | */ | |
efc68158 | 15851 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
15852 | return -EINVAL; |
15853 | } | |
5b92a28a | 15854 | } else { |
be8704ff | 15855 | if (prog_extension) { |
efc68158 | 15856 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
15857 | return -EINVAL; |
15858 | } | |
5b92a28a | 15859 | } |
f1b9509c AS |
15860 | |
15861 | switch (prog->expected_attach_type) { | |
15862 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 15863 | if (tgt_prog) { |
efc68158 | 15864 | bpf_log(log, |
5b92a28a AS |
15865 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
15866 | return -EINVAL; | |
15867 | } | |
38207291 | 15868 | if (!btf_type_is_typedef(t)) { |
efc68158 | 15869 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
15870 | btf_id); |
15871 | return -EINVAL; | |
15872 | } | |
f1b9509c | 15873 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 15874 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
15875 | btf_id, tname); |
15876 | return -EINVAL; | |
15877 | } | |
15878 | tname += sizeof(prefix) - 1; | |
5b92a28a | 15879 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
15880 | if (!btf_type_is_ptr(t)) |
15881 | /* should never happen in valid vmlinux build */ | |
15882 | return -EINVAL; | |
5b92a28a | 15883 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
15884 | if (!btf_type_is_func_proto(t)) |
15885 | /* should never happen in valid vmlinux build */ | |
15886 | return -EINVAL; | |
15887 | ||
f7b12b6f | 15888 | break; |
15d83c4d YS |
15889 | case BPF_TRACE_ITER: |
15890 | if (!btf_type_is_func(t)) { | |
efc68158 | 15891 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
15892 | btf_id); |
15893 | return -EINVAL; | |
15894 | } | |
15895 | t = btf_type_by_id(btf, t->type); | |
15896 | if (!btf_type_is_func_proto(t)) | |
15897 | return -EINVAL; | |
f7b12b6f THJ |
15898 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
15899 | if (ret) | |
15900 | return ret; | |
15901 | break; | |
be8704ff AS |
15902 | default: |
15903 | if (!prog_extension) | |
15904 | return -EINVAL; | |
df561f66 | 15905 | fallthrough; |
ae240823 | 15906 | case BPF_MODIFY_RETURN: |
9e4e01df | 15907 | case BPF_LSM_MAC: |
69fd337a | 15908 | case BPF_LSM_CGROUP: |
fec56f58 AS |
15909 | case BPF_TRACE_FENTRY: |
15910 | case BPF_TRACE_FEXIT: | |
15911 | if (!btf_type_is_func(t)) { | |
efc68158 | 15912 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
15913 | btf_id); |
15914 | return -EINVAL; | |
15915 | } | |
be8704ff | 15916 | if (prog_extension && |
efc68158 | 15917 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 15918 | return -EINVAL; |
5b92a28a | 15919 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
15920 | if (!btf_type_is_func_proto(t)) |
15921 | return -EINVAL; | |
f7b12b6f | 15922 | |
4a1e7c0c THJ |
15923 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
15924 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
15925 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
15926 | return -EINVAL; | |
15927 | ||
f7b12b6f | 15928 | if (tgt_prog && conservative) |
5b92a28a | 15929 | t = NULL; |
f7b12b6f THJ |
15930 | |
15931 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 15932 | if (ret < 0) |
f7b12b6f THJ |
15933 | return ret; |
15934 | ||
5b92a28a | 15935 | if (tgt_prog) { |
e9eeec58 YS |
15936 | if (subprog == 0) |
15937 | addr = (long) tgt_prog->bpf_func; | |
15938 | else | |
15939 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
15940 | } else { |
15941 | addr = kallsyms_lookup_name(tname); | |
15942 | if (!addr) { | |
efc68158 | 15943 | bpf_log(log, |
5b92a28a AS |
15944 | "The address of function %s cannot be found\n", |
15945 | tname); | |
f7b12b6f | 15946 | return -ENOENT; |
5b92a28a | 15947 | } |
fec56f58 | 15948 | } |
18644cec | 15949 | |
1e6c62a8 AS |
15950 | if (prog->aux->sleepable) { |
15951 | ret = -EINVAL; | |
15952 | switch (prog->type) { | |
15953 | case BPF_PROG_TYPE_TRACING: | |
15954 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
15955 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
15956 | */ | |
15957 | if (!check_non_sleepable_error_inject(btf_id) && | |
15958 | within_error_injection_list(addr)) | |
15959 | ret = 0; | |
15960 | break; | |
15961 | case BPF_PROG_TYPE_LSM: | |
15962 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
15963 | * Only some of them are sleepable. | |
15964 | */ | |
423f1610 | 15965 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
15966 | ret = 0; |
15967 | break; | |
15968 | default: | |
15969 | break; | |
15970 | } | |
f7b12b6f THJ |
15971 | if (ret) { |
15972 | bpf_log(log, "%s is not sleepable\n", tname); | |
15973 | return ret; | |
15974 | } | |
1e6c62a8 | 15975 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 15976 | if (tgt_prog) { |
efc68158 | 15977 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
15978 | return -EINVAL; |
15979 | } | |
15980 | ret = check_attach_modify_return(addr, tname); | |
15981 | if (ret) { | |
15982 | bpf_log(log, "%s() is not modifiable\n", tname); | |
15983 | return ret; | |
1af9270e | 15984 | } |
18644cec | 15985 | } |
f7b12b6f THJ |
15986 | |
15987 | break; | |
15988 | } | |
15989 | tgt_info->tgt_addr = addr; | |
15990 | tgt_info->tgt_name = tname; | |
15991 | tgt_info->tgt_type = t; | |
15992 | return 0; | |
15993 | } | |
15994 | ||
35e3815f JO |
15995 | BTF_SET_START(btf_id_deny) |
15996 | BTF_ID_UNUSED | |
15997 | #ifdef CONFIG_SMP | |
15998 | BTF_ID(func, migrate_disable) | |
15999 | BTF_ID(func, migrate_enable) | |
16000 | #endif | |
16001 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
16002 | BTF_ID(func, rcu_read_unlock_strict) | |
16003 | #endif | |
16004 | BTF_SET_END(btf_id_deny) | |
16005 | ||
f7b12b6f THJ |
16006 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
16007 | { | |
16008 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 16009 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
16010 | struct bpf_attach_target_info tgt_info = {}; |
16011 | u32 btf_id = prog->aux->attach_btf_id; | |
16012 | struct bpf_trampoline *tr; | |
16013 | int ret; | |
16014 | u64 key; | |
16015 | ||
79a7f8bd AS |
16016 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
16017 | if (prog->aux->sleepable) | |
16018 | /* attach_btf_id checked to be zero already */ | |
16019 | return 0; | |
16020 | verbose(env, "Syscall programs can only be sleepable\n"); | |
16021 | return -EINVAL; | |
16022 | } | |
16023 | ||
f7b12b6f | 16024 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
64ad7556 DK |
16025 | prog->type != BPF_PROG_TYPE_LSM && prog->type != BPF_PROG_TYPE_KPROBE) { |
16026 | verbose(env, "Only fentry/fexit/fmod_ret, lsm, and kprobe/uprobe programs can be sleepable\n"); | |
f7b12b6f THJ |
16027 | return -EINVAL; |
16028 | } | |
16029 | ||
16030 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
16031 | return check_struct_ops_btf_id(env); | |
16032 | ||
16033 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
16034 | prog->type != BPF_PROG_TYPE_LSM && | |
16035 | prog->type != BPF_PROG_TYPE_EXT) | |
16036 | return 0; | |
16037 | ||
16038 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
16039 | if (ret) | |
fec56f58 | 16040 | return ret; |
f7b12b6f THJ |
16041 | |
16042 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
16043 | /* to make freplace equivalent to their targets, they need to |
16044 | * inherit env->ops and expected_attach_type for the rest of the | |
16045 | * verification | |
16046 | */ | |
f7b12b6f THJ |
16047 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
16048 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
16049 | } | |
16050 | ||
16051 | /* store info about the attachment target that will be used later */ | |
16052 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
16053 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
16054 | ||
4a1e7c0c THJ |
16055 | if (tgt_prog) { |
16056 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
16057 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
16058 | } | |
16059 | ||
f7b12b6f THJ |
16060 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
16061 | prog->aux->attach_btf_trace = true; | |
16062 | return 0; | |
16063 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
16064 | if (!bpf_iter_prog_supported(prog)) | |
16065 | return -EINVAL; | |
16066 | return 0; | |
16067 | } | |
16068 | ||
16069 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
16070 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
16071 | if (ret < 0) | |
16072 | return ret; | |
35e3815f JO |
16073 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
16074 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
16075 | return -EINVAL; | |
38207291 | 16076 | } |
f7b12b6f | 16077 | |
22dc4a0f | 16078 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
16079 | tr = bpf_trampoline_get(key, &tgt_info); |
16080 | if (!tr) | |
16081 | return -ENOMEM; | |
16082 | ||
3aac1ead | 16083 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 16084 | return 0; |
38207291 MKL |
16085 | } |
16086 | ||
76654e67 AM |
16087 | struct btf *bpf_get_btf_vmlinux(void) |
16088 | { | |
16089 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
16090 | mutex_lock(&bpf_verifier_lock); | |
16091 | if (!btf_vmlinux) | |
16092 | btf_vmlinux = btf_parse_vmlinux(); | |
16093 | mutex_unlock(&bpf_verifier_lock); | |
16094 | } | |
16095 | return btf_vmlinux; | |
16096 | } | |
16097 | ||
af2ac3e1 | 16098 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 16099 | { |
06ee7115 | 16100 | u64 start_time = ktime_get_ns(); |
58e2af8b | 16101 | struct bpf_verifier_env *env; |
b9193c1b | 16102 | struct bpf_verifier_log *log; |
9e4c24e7 | 16103 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 16104 | bool is_priv; |
51580e79 | 16105 | |
eba0c929 AB |
16106 | /* no program is valid */ |
16107 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
16108 | return -EINVAL; | |
16109 | ||
58e2af8b | 16110 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
16111 | * allocate/free it every time bpf_check() is called |
16112 | */ | |
58e2af8b | 16113 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
16114 | if (!env) |
16115 | return -ENOMEM; | |
61bd5218 | 16116 | log = &env->log; |
cbd35700 | 16117 | |
9e4c24e7 | 16118 | len = (*prog)->len; |
fad953ce | 16119 | env->insn_aux_data = |
9e4c24e7 | 16120 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
16121 | ret = -ENOMEM; |
16122 | if (!env->insn_aux_data) | |
16123 | goto err_free_env; | |
9e4c24e7 JK |
16124 | for (i = 0; i < len; i++) |
16125 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 16126 | env->prog = *prog; |
00176a34 | 16127 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 16128 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 16129 | is_priv = bpf_capable(); |
0246e64d | 16130 | |
76654e67 | 16131 | bpf_get_btf_vmlinux(); |
8580ac94 | 16132 | |
cbd35700 | 16133 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
16134 | if (!is_priv) |
16135 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
16136 | |
16137 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
16138 | /* user requested verbose verifier output | |
16139 | * and supplied buffer to store the verification trace | |
16140 | */ | |
e7bf8249 JK |
16141 | log->level = attr->log_level; |
16142 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
16143 | log->len_total = attr->log_size; | |
cbd35700 | 16144 | |
e7bf8249 | 16145 | /* log attributes have to be sane */ |
866de407 HT |
16146 | if (!bpf_verifier_log_attr_valid(log)) { |
16147 | ret = -EINVAL; | |
3df126f3 | 16148 | goto err_unlock; |
866de407 | 16149 | } |
cbd35700 | 16150 | } |
1ad2f583 | 16151 | |
0f55f9ed CL |
16152 | mark_verifier_state_clean(env); |
16153 | ||
8580ac94 AS |
16154 | if (IS_ERR(btf_vmlinux)) { |
16155 | /* Either gcc or pahole or kernel are broken. */ | |
16156 | verbose(env, "in-kernel BTF is malformed\n"); | |
16157 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 16158 | goto skip_full_check; |
8580ac94 AS |
16159 | } |
16160 | ||
1ad2f583 DB |
16161 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
16162 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 16163 | env->strict_alignment = true; |
e9ee9efc DM |
16164 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
16165 | env->strict_alignment = false; | |
cbd35700 | 16166 | |
2c78ee89 | 16167 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 16168 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
41c48f3a | 16169 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
16170 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
16171 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
16172 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 16173 | |
10d274e8 AS |
16174 | if (is_priv) |
16175 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
16176 | ||
dc2a4ebc | 16177 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 16178 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
16179 | GFP_USER); |
16180 | ret = -ENOMEM; | |
16181 | if (!env->explored_states) | |
16182 | goto skip_full_check; | |
16183 | ||
e6ac2450 MKL |
16184 | ret = add_subprog_and_kfunc(env); |
16185 | if (ret < 0) | |
16186 | goto skip_full_check; | |
16187 | ||
d9762e84 | 16188 | ret = check_subprogs(env); |
475fb78f AS |
16189 | if (ret < 0) |
16190 | goto skip_full_check; | |
16191 | ||
c454a46b | 16192 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
16193 | if (ret < 0) |
16194 | goto skip_full_check; | |
16195 | ||
be8704ff AS |
16196 | ret = check_attach_btf_id(env); |
16197 | if (ret) | |
16198 | goto skip_full_check; | |
16199 | ||
4976b718 HL |
16200 | ret = resolve_pseudo_ldimm64(env); |
16201 | if (ret < 0) | |
16202 | goto skip_full_check; | |
16203 | ||
ceb11679 YZ |
16204 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
16205 | ret = bpf_prog_offload_verifier_prep(env->prog); | |
16206 | if (ret) | |
16207 | goto skip_full_check; | |
16208 | } | |
16209 | ||
d9762e84 MKL |
16210 | ret = check_cfg(env); |
16211 | if (ret < 0) | |
16212 | goto skip_full_check; | |
16213 | ||
51c39bb1 AS |
16214 | ret = do_check_subprogs(env); |
16215 | ret = ret ?: do_check_main(env); | |
cbd35700 | 16216 | |
c941ce9c QM |
16217 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
16218 | ret = bpf_prog_offload_finalize(env); | |
16219 | ||
0246e64d | 16220 | skip_full_check: |
51c39bb1 | 16221 | kvfree(env->explored_states); |
0246e64d | 16222 | |
c131187d | 16223 | if (ret == 0) |
9b38c405 | 16224 | ret = check_max_stack_depth(env); |
c131187d | 16225 | |
9b38c405 | 16226 | /* instruction rewrites happen after this point */ |
1ade2371 EZ |
16227 | if (ret == 0) |
16228 | ret = optimize_bpf_loop(env); | |
16229 | ||
e2ae4ca2 JK |
16230 | if (is_priv) { |
16231 | if (ret == 0) | |
16232 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
16233 | if (ret == 0) |
16234 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
16235 | if (ret == 0) |
16236 | ret = opt_remove_nops(env); | |
52875a04 JK |
16237 | } else { |
16238 | if (ret == 0) | |
16239 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
16240 | } |
16241 | ||
9bac3d6d AS |
16242 | if (ret == 0) |
16243 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
16244 | ret = convert_ctx_accesses(env); | |
16245 | ||
e245c5c6 | 16246 | if (ret == 0) |
e6ac5933 | 16247 | ret = do_misc_fixups(env); |
e245c5c6 | 16248 | |
a4b1d3c1 JW |
16249 | /* do 32-bit optimization after insn patching has done so those patched |
16250 | * insns could be handled correctly. | |
16251 | */ | |
d6c2308c JW |
16252 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
16253 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
16254 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
16255 | : false; | |
a4b1d3c1 JW |
16256 | } |
16257 | ||
1ea47e01 AS |
16258 | if (ret == 0) |
16259 | ret = fixup_call_args(env); | |
16260 | ||
06ee7115 AS |
16261 | env->verification_time = ktime_get_ns() - start_time; |
16262 | print_verification_stats(env); | |
aba64c7d | 16263 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 16264 | |
a2a7d570 | 16265 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 16266 | ret = -ENOSPC; |
a2a7d570 | 16267 | if (log->level && !log->ubuf) { |
cbd35700 | 16268 | ret = -EFAULT; |
a2a7d570 | 16269 | goto err_release_maps; |
cbd35700 AS |
16270 | } |
16271 | ||
541c3bad AN |
16272 | if (ret) |
16273 | goto err_release_maps; | |
16274 | ||
16275 | if (env->used_map_cnt) { | |
0246e64d | 16276 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
16277 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
16278 | sizeof(env->used_maps[0]), | |
16279 | GFP_KERNEL); | |
0246e64d | 16280 | |
9bac3d6d | 16281 | if (!env->prog->aux->used_maps) { |
0246e64d | 16282 | ret = -ENOMEM; |
a2a7d570 | 16283 | goto err_release_maps; |
0246e64d AS |
16284 | } |
16285 | ||
9bac3d6d | 16286 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 16287 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 16288 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
16289 | } |
16290 | if (env->used_btf_cnt) { | |
16291 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
16292 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
16293 | sizeof(env->used_btfs[0]), | |
16294 | GFP_KERNEL); | |
16295 | if (!env->prog->aux->used_btfs) { | |
16296 | ret = -ENOMEM; | |
16297 | goto err_release_maps; | |
16298 | } | |
0246e64d | 16299 | |
541c3bad AN |
16300 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
16301 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
16302 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
16303 | } | |
16304 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
16305 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
16306 | * bpf_ld_imm64 instructions | |
16307 | */ | |
16308 | convert_pseudo_ld_imm64(env); | |
16309 | } | |
cbd35700 | 16310 | |
541c3bad | 16311 | adjust_btf_func(env); |
ba64e7d8 | 16312 | |
a2a7d570 | 16313 | err_release_maps: |
9bac3d6d | 16314 | if (!env->prog->aux->used_maps) |
0246e64d | 16315 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 16316 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
16317 | */ |
16318 | release_maps(env); | |
541c3bad AN |
16319 | if (!env->prog->aux->used_btfs) |
16320 | release_btfs(env); | |
03f87c0b THJ |
16321 | |
16322 | /* extension progs temporarily inherit the attach_type of their targets | |
16323 | for verification purposes, so set it back to zero before returning | |
16324 | */ | |
16325 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
16326 | env->prog->expected_attach_type = 0; | |
16327 | ||
9bac3d6d | 16328 | *prog = env->prog; |
3df126f3 | 16329 | err_unlock: |
45a73c17 AS |
16330 | if (!is_priv) |
16331 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
16332 | vfree(env->insn_aux_data); |
16333 | err_free_env: | |
16334 | kfree(env); | |
51580e79 AS |
16335 | return ret; |
16336 | } |