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> |
51580e79 | 26 | |
f4ac7e0b JK |
27 | #include "disasm.h" |
28 | ||
00176a34 | 29 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 30 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
31 | [_id] = & _name ## _verifier_ops, |
32 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 33 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
34 | #include <linux/bpf_types.h> |
35 | #undef BPF_PROG_TYPE | |
36 | #undef BPF_MAP_TYPE | |
f2e10bff | 37 | #undef BPF_LINK_TYPE |
00176a34 JK |
38 | }; |
39 | ||
51580e79 AS |
40 | /* bpf_check() is a static code analyzer that walks eBPF program |
41 | * instruction by instruction and updates register/stack state. | |
42 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
43 | * | |
44 | * The first pass is depth-first-search to check that the program is a DAG. | |
45 | * It rejects the following programs: | |
46 | * - larger than BPF_MAXINSNS insns | |
47 | * - if loop is present (detected via back-edge) | |
48 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
49 | * - out of bounds or malformed jumps | |
50 | * The second pass is all possible path descent from the 1st insn. | |
8fb33b60 | 51 | * Since it's analyzing all paths through the program, the length of the |
eba38a96 | 52 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
53 | * insn is less then 4K, but there are too many branches that change stack/regs. |
54 | * Number of 'branches to be analyzed' is limited to 1k | |
55 | * | |
56 | * On entry to each instruction, each register has a type, and the instruction | |
57 | * changes the types of the registers depending on instruction semantics. | |
58 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
59 | * copied to R1. | |
60 | * | |
61 | * All registers are 64-bit. | |
62 | * R0 - return register | |
63 | * R1-R5 argument passing registers | |
64 | * R6-R9 callee saved registers | |
65 | * R10 - frame pointer read-only | |
66 | * | |
67 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
68 | * and has type PTR_TO_CTX. | |
69 | * | |
70 | * Verifier tracks arithmetic operations on pointers in case: | |
71 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
72 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
73 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
74 | * and 2nd arithmetic instruction is pattern matched to recognize | |
75 | * that it wants to construct a pointer to some element within stack. | |
76 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
77 | * (and -20 constant is saved for further stack bounds checking). | |
78 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
79 | * | |
f1174f77 | 80 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 81 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 82 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
83 | * |
84 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
85 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
86 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
87 | * |
88 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
89 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
90 | * | |
91 | * registers used to pass values to function calls are checked against | |
92 | * function argument constraints. | |
93 | * | |
94 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
95 | * It means that the register type passed to this function must be | |
96 | * PTR_TO_STACK and it will be used inside the function as | |
97 | * 'pointer to map element key' | |
98 | * | |
99 | * For example the argument constraints for bpf_map_lookup_elem(): | |
100 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
101 | * .arg1_type = ARG_CONST_MAP_PTR, | |
102 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
103 | * | |
104 | * ret_type says that this function returns 'pointer to map elem value or null' | |
105 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
106 | * 2nd argument should be a pointer to stack, which will be used inside | |
107 | * the helper function as a pointer to map element key. | |
108 | * | |
109 | * On the kernel side the helper function looks like: | |
110 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
111 | * { | |
112 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
113 | * void *key = (void *) (unsigned long) r2; | |
114 | * void *value; | |
115 | * | |
116 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
117 | * [key, key + map->key_size) bytes are valid and were initialized on | |
118 | * the stack of eBPF program. | |
119 | * } | |
120 | * | |
121 | * Corresponding eBPF program may look like: | |
122 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
123 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
124 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
125 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
126 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
127 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
128 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
129 | * | |
130 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
131 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
132 | * and were initialized prior to this call. | |
133 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
134 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
135 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
8fb33b60 | 136 | * returns either pointer to map value or NULL. |
51580e79 AS |
137 | * |
138 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
139 | * insn, the register holding that pointer in the true branch changes state to | |
140 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
141 | * branch. See check_cond_jmp_op(). | |
142 | * | |
143 | * After the call R0 is set to return type of the function and registers R1-R5 | |
144 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
145 | * |
146 | * The following reference types represent a potential reference to a kernel | |
147 | * resource which, after first being allocated, must be checked and freed by | |
148 | * the BPF program: | |
149 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
150 | * | |
151 | * When the verifier sees a helper call return a reference type, it allocates a | |
152 | * pointer id for the reference and stores it in the current function state. | |
153 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
154 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
155 | * passes through a NULL-check conditional. For the branch wherein the state is | |
156 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
157 | * |
158 | * For each helper function that allocates a reference, such as | |
159 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
160 | * bpf_sk_release(). When a reference type passes into the release function, | |
161 | * the verifier also releases the reference. If any unchecked or unreleased | |
162 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
163 | */ |
164 | ||
17a52670 | 165 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 166 | struct bpf_verifier_stack_elem { |
17a52670 AS |
167 | /* verifer state is 'st' |
168 | * before processing instruction 'insn_idx' | |
169 | * and after processing instruction 'prev_insn_idx' | |
170 | */ | |
58e2af8b | 171 | struct bpf_verifier_state st; |
17a52670 AS |
172 | int insn_idx; |
173 | int prev_insn_idx; | |
58e2af8b | 174 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
175 | /* length of verifier log at the time this state was pushed on stack */ |
176 | u32 log_pos; | |
cbd35700 AS |
177 | }; |
178 | ||
b285fcb7 | 179 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 180 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 181 | |
d2e4c1e6 DB |
182 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
183 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
184 | ||
c93552c4 DB |
185 | #define BPF_MAP_PTR_UNPRIV 1UL |
186 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
187 | POISON_POINTER_DELTA)) | |
188 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
189 | ||
190 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) | |
191 | { | |
d2e4c1e6 | 192 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
193 | } |
194 | ||
195 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
196 | { | |
d2e4c1e6 | 197 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
198 | } |
199 | ||
200 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
201 | const struct bpf_map *map, bool unpriv) | |
202 | { | |
203 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
204 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
205 | aux->map_ptr_state = (unsigned long)map | |
206 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
207 | } | |
208 | ||
209 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
210 | { | |
211 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
212 | } | |
213 | ||
214 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
215 | { | |
216 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
217 | } | |
218 | ||
219 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
220 | { | |
221 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
222 | } | |
223 | ||
224 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
225 | { | |
226 | bool poisoned = bpf_map_key_poisoned(aux); | |
227 | ||
228 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
229 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 230 | } |
fad73a1a | 231 | |
23a2d70c YS |
232 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
233 | { | |
234 | return insn->code == (BPF_JMP | BPF_CALL) && | |
235 | insn->src_reg == BPF_PSEUDO_CALL; | |
236 | } | |
237 | ||
e6ac2450 MKL |
238 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
239 | { | |
240 | return insn->code == (BPF_JMP | BPF_CALL) && | |
241 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; | |
242 | } | |
243 | ||
33ff9823 DB |
244 | struct bpf_call_arg_meta { |
245 | struct bpf_map *map_ptr; | |
435faee1 | 246 | bool raw_mode; |
36bbef52 | 247 | bool pkt_access; |
435faee1 DB |
248 | int regno; |
249 | int access_size; | |
457f4436 | 250 | int mem_size; |
10060503 | 251 | u64 msize_max_value; |
1b986589 | 252 | int ref_obj_id; |
3e8ce298 | 253 | int map_uid; |
d83525ca | 254 | int func_id; |
22dc4a0f | 255 | struct btf *btf; |
eaa6bcb7 | 256 | u32 btf_id; |
22dc4a0f | 257 | struct btf *ret_btf; |
eaa6bcb7 | 258 | u32 ret_btf_id; |
69c087ba | 259 | u32 subprogno; |
33ff9823 DB |
260 | }; |
261 | ||
8580ac94 AS |
262 | struct btf *btf_vmlinux; |
263 | ||
cbd35700 AS |
264 | static DEFINE_MUTEX(bpf_verifier_lock); |
265 | ||
d9762e84 MKL |
266 | static const struct bpf_line_info * |
267 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
268 | { | |
269 | const struct bpf_line_info *linfo; | |
270 | const struct bpf_prog *prog; | |
271 | u32 i, nr_linfo; | |
272 | ||
273 | prog = env->prog; | |
274 | nr_linfo = prog->aux->nr_linfo; | |
275 | ||
276 | if (!nr_linfo || insn_off >= prog->len) | |
277 | return NULL; | |
278 | ||
279 | linfo = prog->aux->linfo; | |
280 | for (i = 1; i < nr_linfo; i++) | |
281 | if (insn_off < linfo[i].insn_off) | |
282 | break; | |
283 | ||
284 | return &linfo[i - 1]; | |
285 | } | |
286 | ||
77d2e05a MKL |
287 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
288 | va_list args) | |
cbd35700 | 289 | { |
a2a7d570 | 290 | unsigned int n; |
cbd35700 | 291 | |
a2a7d570 | 292 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
293 | |
294 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
295 | "verifier log line truncated - local buffer too short\n"); | |
296 | ||
8580ac94 | 297 | if (log->level == BPF_LOG_KERNEL) { |
436d404c HT |
298 | bool newline = n > 0 && log->kbuf[n - 1] == '\n'; |
299 | ||
300 | pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); | |
8580ac94 AS |
301 | return; |
302 | } | |
436d404c HT |
303 | |
304 | n = min(log->len_total - log->len_used - 1, n); | |
305 | log->kbuf[n] = '\0'; | |
a2a7d570 JK |
306 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
307 | log->len_used += n; | |
308 | else | |
309 | log->ubuf = NULL; | |
cbd35700 | 310 | } |
abe08840 | 311 | |
6f8a57cc AN |
312 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
313 | { | |
314 | char zero = 0; | |
315 | ||
316 | if (!bpf_verifier_log_needed(log)) | |
317 | return; | |
318 | ||
319 | log->len_used = new_pos; | |
320 | if (put_user(zero, log->ubuf + new_pos)) | |
321 | log->ubuf = NULL; | |
322 | } | |
323 | ||
abe08840 JO |
324 | /* log_level controls verbosity level of eBPF verifier. |
325 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
326 | * so the user can figure out what's wrong with the program | |
430e68d1 | 327 | */ |
abe08840 JO |
328 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
329 | const char *fmt, ...) | |
330 | { | |
331 | va_list args; | |
332 | ||
77d2e05a MKL |
333 | if (!bpf_verifier_log_needed(&env->log)) |
334 | return; | |
335 | ||
abe08840 | 336 | va_start(args, fmt); |
77d2e05a | 337 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
338 | va_end(args); |
339 | } | |
340 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
341 | ||
342 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
343 | { | |
77d2e05a | 344 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
345 | va_list args; |
346 | ||
77d2e05a MKL |
347 | if (!bpf_verifier_log_needed(&env->log)) |
348 | return; | |
349 | ||
abe08840 | 350 | va_start(args, fmt); |
77d2e05a | 351 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
352 | va_end(args); |
353 | } | |
cbd35700 | 354 | |
9e15db66 AS |
355 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
356 | const char *fmt, ...) | |
357 | { | |
358 | va_list args; | |
359 | ||
360 | if (!bpf_verifier_log_needed(log)) | |
361 | return; | |
362 | ||
363 | va_start(args, fmt); | |
364 | bpf_verifier_vlog(log, fmt, args); | |
365 | va_end(args); | |
366 | } | |
367 | ||
d9762e84 MKL |
368 | static const char *ltrim(const char *s) |
369 | { | |
370 | while (isspace(*s)) | |
371 | s++; | |
372 | ||
373 | return s; | |
374 | } | |
375 | ||
376 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
377 | u32 insn_off, | |
378 | const char *prefix_fmt, ...) | |
379 | { | |
380 | const struct bpf_line_info *linfo; | |
381 | ||
382 | if (!bpf_verifier_log_needed(&env->log)) | |
383 | return; | |
384 | ||
385 | linfo = find_linfo(env, insn_off); | |
386 | if (!linfo || linfo == env->prev_linfo) | |
387 | return; | |
388 | ||
389 | if (prefix_fmt) { | |
390 | va_list args; | |
391 | ||
392 | va_start(args, prefix_fmt); | |
393 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
394 | va_end(args); | |
395 | } | |
396 | ||
397 | verbose(env, "%s\n", | |
398 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
399 | linfo->line_off))); | |
400 | ||
401 | env->prev_linfo = linfo; | |
402 | } | |
403 | ||
bc2591d6 YS |
404 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
405 | struct bpf_reg_state *reg, | |
406 | struct tnum *range, const char *ctx, | |
407 | const char *reg_name) | |
408 | { | |
409 | char tn_buf[48]; | |
410 | ||
411 | verbose(env, "At %s the register %s ", ctx, reg_name); | |
412 | if (!tnum_is_unknown(reg->var_off)) { | |
413 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
414 | verbose(env, "has value %s", tn_buf); | |
415 | } else { | |
416 | verbose(env, "has unknown scalar value"); | |
417 | } | |
418 | tnum_strn(tn_buf, sizeof(tn_buf), *range); | |
419 | verbose(env, " should have been in %s\n", tn_buf); | |
420 | } | |
421 | ||
de8f3a83 DB |
422 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
423 | { | |
424 | return type == PTR_TO_PACKET || | |
425 | type == PTR_TO_PACKET_META; | |
426 | } | |
427 | ||
46f8bc92 MKL |
428 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
429 | { | |
430 | return type == PTR_TO_SOCKET || | |
655a51e5 | 431 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
432 | type == PTR_TO_TCP_SOCK || |
433 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
434 | } |
435 | ||
cac616db JF |
436 | static bool reg_type_not_null(enum bpf_reg_type type) |
437 | { | |
438 | return type == PTR_TO_SOCKET || | |
439 | type == PTR_TO_TCP_SOCK || | |
440 | type == PTR_TO_MAP_VALUE || | |
69c087ba | 441 | type == PTR_TO_MAP_KEY || |
01c66c48 | 442 | type == PTR_TO_SOCK_COMMON; |
cac616db JF |
443 | } |
444 | ||
840b9615 JS |
445 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
446 | { | |
fd978bf7 | 447 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 448 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 | 449 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
b121b341 | 450 | type == PTR_TO_TCP_SOCK_OR_NULL || |
457f4436 | 451 | type == PTR_TO_BTF_ID_OR_NULL || |
afbf21dc YS |
452 | type == PTR_TO_MEM_OR_NULL || |
453 | type == PTR_TO_RDONLY_BUF_OR_NULL || | |
454 | type == PTR_TO_RDWR_BUF_OR_NULL; | |
fd978bf7 JS |
455 | } |
456 | ||
d83525ca AS |
457 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
458 | { | |
459 | return reg->type == PTR_TO_MAP_VALUE && | |
460 | map_value_has_spin_lock(reg->map_ptr); | |
461 | } | |
462 | ||
cba368c1 MKL |
463 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
464 | { | |
465 | return type == PTR_TO_SOCKET || | |
466 | type == PTR_TO_SOCKET_OR_NULL || | |
467 | type == PTR_TO_TCP_SOCK || | |
457f4436 AN |
468 | type == PTR_TO_TCP_SOCK_OR_NULL || |
469 | type == PTR_TO_MEM || | |
470 | type == PTR_TO_MEM_OR_NULL; | |
cba368c1 MKL |
471 | } |
472 | ||
1b986589 | 473 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 474 | { |
1b986589 | 475 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
476 | } |
477 | ||
48946bd6 | 478 | static bool type_may_be_null(u32 type) |
fd1b0d60 | 479 | { |
48946bd6 | 480 | return type & PTR_MAYBE_NULL; |
fd1b0d60 LB |
481 | } |
482 | ||
fd978bf7 JS |
483 | /* Determine whether the function releases some resources allocated by another |
484 | * function call. The first reference type argument will be assumed to be | |
485 | * released by release_reference(). | |
486 | */ | |
487 | static bool is_release_function(enum bpf_func_id func_id) | |
488 | { | |
457f4436 AN |
489 | return func_id == BPF_FUNC_sk_release || |
490 | func_id == BPF_FUNC_ringbuf_submit || | |
491 | func_id == BPF_FUNC_ringbuf_discard; | |
840b9615 JS |
492 | } |
493 | ||
64d85290 | 494 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
46f8bc92 MKL |
495 | { |
496 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 | 497 | func_id == BPF_FUNC_sk_lookup_udp || |
64d85290 | 498 | func_id == BPF_FUNC_skc_lookup_tcp || |
457f4436 AN |
499 | func_id == BPF_FUNC_map_lookup_elem || |
500 | func_id == BPF_FUNC_ringbuf_reserve; | |
64d85290 JS |
501 | } |
502 | ||
503 | static bool is_acquire_function(enum bpf_func_id func_id, | |
504 | const struct bpf_map *map) | |
505 | { | |
506 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
507 | ||
508 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
509 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 AN |
510 | func_id == BPF_FUNC_skc_lookup_tcp || |
511 | func_id == BPF_FUNC_ringbuf_reserve) | |
64d85290 JS |
512 | return true; |
513 | ||
514 | if (func_id == BPF_FUNC_map_lookup_elem && | |
515 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
516 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
517 | return true; | |
518 | ||
519 | return false; | |
46f8bc92 MKL |
520 | } |
521 | ||
1b986589 MKL |
522 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
523 | { | |
524 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
525 | func_id == BPF_FUNC_sk_fullsock || |
526 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
527 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
528 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
529 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || | |
530 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
531 | } |
532 | ||
39491867 BJ |
533 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
534 | { | |
535 | return BPF_CLASS(insn->code) == BPF_STX && | |
536 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
537 | insn->imm == BPF_CMPXCHG; | |
538 | } | |
539 | ||
17a52670 AS |
540 | /* string representation of 'enum bpf_reg_type' */ |
541 | static const char * const reg_type_str[] = { | |
542 | [NOT_INIT] = "?", | |
f1174f77 | 543 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
544 | [PTR_TO_CTX] = "ctx", |
545 | [CONST_PTR_TO_MAP] = "map_ptr", | |
546 | [PTR_TO_MAP_VALUE] = "map_value", | |
547 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 548 | [PTR_TO_STACK] = "fp", |
969bf05e | 549 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 550 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 551 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 552 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
553 | [PTR_TO_SOCKET] = "sock", |
554 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
555 | [PTR_TO_SOCK_COMMON] = "sock_common", |
556 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
557 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
558 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 559 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 560 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
9e15db66 | 561 | [PTR_TO_BTF_ID] = "ptr_", |
b121b341 | 562 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
eaa6bcb7 | 563 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
457f4436 AN |
564 | [PTR_TO_MEM] = "mem", |
565 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", | |
afbf21dc YS |
566 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
567 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", | |
568 | [PTR_TO_RDWR_BUF] = "rdwr_buf", | |
569 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", | |
69c087ba YS |
570 | [PTR_TO_FUNC] = "func", |
571 | [PTR_TO_MAP_KEY] = "map_key", | |
17a52670 AS |
572 | }; |
573 | ||
8efea21d EC |
574 | static char slot_type_char[] = { |
575 | [STACK_INVALID] = '?', | |
576 | [STACK_SPILL] = 'r', | |
577 | [STACK_MISC] = 'm', | |
578 | [STACK_ZERO] = '0', | |
579 | }; | |
580 | ||
4e92024a AS |
581 | static void print_liveness(struct bpf_verifier_env *env, |
582 | enum bpf_reg_liveness live) | |
583 | { | |
9242b5f5 | 584 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
585 | verbose(env, "_"); |
586 | if (live & REG_LIVE_READ) | |
587 | verbose(env, "r"); | |
588 | if (live & REG_LIVE_WRITTEN) | |
589 | verbose(env, "w"); | |
9242b5f5 AS |
590 | if (live & REG_LIVE_DONE) |
591 | verbose(env, "D"); | |
4e92024a AS |
592 | } |
593 | ||
f4d7e40a AS |
594 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
595 | const struct bpf_reg_state *reg) | |
596 | { | |
597 | struct bpf_verifier_state *cur = env->cur_state; | |
598 | ||
599 | return cur->frame[reg->frameno]; | |
600 | } | |
601 | ||
22dc4a0f | 602 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
9e15db66 | 603 | { |
22dc4a0f | 604 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
605 | } |
606 | ||
0f55f9ed CL |
607 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
608 | { | |
609 | env->scratched_regs |= 1U << regno; | |
610 | } | |
611 | ||
612 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
613 | { | |
614 | env->scratched_stack_slots |= 1UL << spi; | |
615 | } | |
616 | ||
617 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
618 | { | |
619 | return (env->scratched_regs >> regno) & 1; | |
620 | } | |
621 | ||
622 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
623 | { | |
624 | return (env->scratched_stack_slots >> regno) & 1; | |
625 | } | |
626 | ||
627 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
628 | { | |
629 | return env->scratched_regs || env->scratched_stack_slots; | |
630 | } | |
631 | ||
632 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
633 | { | |
634 | env->scratched_regs = 0U; | |
635 | env->scratched_stack_slots = 0UL; | |
636 | } | |
637 | ||
638 | /* Used for printing the entire verifier state. */ | |
639 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
640 | { | |
641 | env->scratched_regs = ~0U; | |
642 | env->scratched_stack_slots = ~0UL; | |
643 | } | |
644 | ||
27113c59 MKL |
645 | /* The reg state of a pointer or a bounded scalar was saved when |
646 | * it was spilled to the stack. | |
647 | */ | |
648 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
649 | { | |
650 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
651 | } | |
652 | ||
354e8f19 MKL |
653 | static void scrub_spilled_slot(u8 *stype) |
654 | { | |
655 | if (*stype != STACK_INVALID) | |
656 | *stype = STACK_MISC; | |
657 | } | |
658 | ||
61bd5218 | 659 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
660 | const struct bpf_func_state *state, |
661 | bool print_all) | |
17a52670 | 662 | { |
f4d7e40a | 663 | const struct bpf_reg_state *reg; |
17a52670 AS |
664 | enum bpf_reg_type t; |
665 | int i; | |
666 | ||
f4d7e40a AS |
667 | if (state->frameno) |
668 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 669 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
670 | reg = &state->regs[i]; |
671 | t = reg->type; | |
17a52670 AS |
672 | if (t == NOT_INIT) |
673 | continue; | |
0f55f9ed CL |
674 | if (!print_all && !reg_scratched(env, i)) |
675 | continue; | |
4e92024a AS |
676 | verbose(env, " R%d", i); |
677 | print_liveness(env, reg->live); | |
678 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
679 | if (t == SCALAR_VALUE && reg->precise) |
680 | verbose(env, "P"); | |
f1174f77 EC |
681 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
682 | tnum_is_const(reg->var_off)) { | |
683 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 684 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 685 | } else { |
eaa6bcb7 HL |
686 | if (t == PTR_TO_BTF_ID || |
687 | t == PTR_TO_BTF_ID_OR_NULL || | |
688 | t == PTR_TO_PERCPU_BTF_ID) | |
22dc4a0f | 689 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
cba368c1 MKL |
690 | verbose(env, "(id=%d", reg->id); |
691 | if (reg_type_may_be_refcounted_or_null(t)) | |
692 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 693 | if (t != SCALAR_VALUE) |
61bd5218 | 694 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 695 | if (type_is_pkt_pointer(t)) |
61bd5218 | 696 | verbose(env, ",r=%d", reg->range); |
f1174f77 | 697 | else if (t == CONST_PTR_TO_MAP || |
69c087ba | 698 | t == PTR_TO_MAP_KEY || |
f1174f77 EC |
699 | t == PTR_TO_MAP_VALUE || |
700 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 701 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
702 | reg->map_ptr->key_size, |
703 | reg->map_ptr->value_size); | |
7d1238f2 EC |
704 | if (tnum_is_const(reg->var_off)) { |
705 | /* Typically an immediate SCALAR_VALUE, but | |
706 | * could be a pointer whose offset is too big | |
707 | * for reg->off | |
708 | */ | |
61bd5218 | 709 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
710 | } else { |
711 | if (reg->smin_value != reg->umin_value && | |
712 | reg->smin_value != S64_MIN) | |
61bd5218 | 713 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
714 | (long long)reg->smin_value); |
715 | if (reg->smax_value != reg->umax_value && | |
716 | reg->smax_value != S64_MAX) | |
61bd5218 | 717 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
718 | (long long)reg->smax_value); |
719 | if (reg->umin_value != 0) | |
61bd5218 | 720 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
721 | (unsigned long long)reg->umin_value); |
722 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 723 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
724 | (unsigned long long)reg->umax_value); |
725 | if (!tnum_is_unknown(reg->var_off)) { | |
726 | char tn_buf[48]; | |
f1174f77 | 727 | |
7d1238f2 | 728 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 729 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 730 | } |
3f50f132 JF |
731 | if (reg->s32_min_value != reg->smin_value && |
732 | reg->s32_min_value != S32_MIN) | |
733 | verbose(env, ",s32_min_value=%d", | |
734 | (int)(reg->s32_min_value)); | |
735 | if (reg->s32_max_value != reg->smax_value && | |
736 | reg->s32_max_value != S32_MAX) | |
737 | verbose(env, ",s32_max_value=%d", | |
738 | (int)(reg->s32_max_value)); | |
739 | if (reg->u32_min_value != reg->umin_value && | |
740 | reg->u32_min_value != U32_MIN) | |
741 | verbose(env, ",u32_min_value=%d", | |
742 | (int)(reg->u32_min_value)); | |
743 | if (reg->u32_max_value != reg->umax_value && | |
744 | reg->u32_max_value != U32_MAX) | |
745 | verbose(env, ",u32_max_value=%d", | |
746 | (int)(reg->u32_max_value)); | |
f1174f77 | 747 | } |
61bd5218 | 748 | verbose(env, ")"); |
f1174f77 | 749 | } |
17a52670 | 750 | } |
638f5b90 | 751 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
752 | char types_buf[BPF_REG_SIZE + 1]; |
753 | bool valid = false; | |
754 | int j; | |
755 | ||
756 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
757 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
758 | valid = true; | |
759 | types_buf[j] = slot_type_char[ | |
760 | state->stack[i].slot_type[j]]; | |
761 | } | |
762 | types_buf[BPF_REG_SIZE] = 0; | |
763 | if (!valid) | |
764 | continue; | |
0f55f9ed CL |
765 | if (!print_all && !stack_slot_scratched(env, i)) |
766 | continue; | |
8efea21d EC |
767 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
768 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
27113c59 | 769 | if (is_spilled_reg(&state->stack[i])) { |
b5dc0163 AS |
770 | reg = &state->stack[i].spilled_ptr; |
771 | t = reg->type; | |
772 | verbose(env, "=%s", reg_type_str[t]); | |
773 | if (t == SCALAR_VALUE && reg->precise) | |
774 | verbose(env, "P"); | |
775 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
776 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
777 | } else { | |
8efea21d | 778 | verbose(env, "=%s", types_buf); |
b5dc0163 | 779 | } |
17a52670 | 780 | } |
fd978bf7 JS |
781 | if (state->acquired_refs && state->refs[0].id) { |
782 | verbose(env, " refs=%d", state->refs[0].id); | |
783 | for (i = 1; i < state->acquired_refs; i++) | |
784 | if (state->refs[i].id) | |
785 | verbose(env, ",%d", state->refs[i].id); | |
786 | } | |
bfc6bb74 AS |
787 | if (state->in_callback_fn) |
788 | verbose(env, " cb"); | |
789 | if (state->in_async_callback_fn) | |
790 | verbose(env, " async_cb"); | |
61bd5218 | 791 | verbose(env, "\n"); |
0f55f9ed | 792 | mark_verifier_state_clean(env); |
17a52670 AS |
793 | } |
794 | ||
2e576648 CL |
795 | static inline u32 vlog_alignment(u32 pos) |
796 | { | |
797 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
798 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
799 | } | |
800 | ||
801 | static void print_insn_state(struct bpf_verifier_env *env, | |
802 | const struct bpf_func_state *state) | |
803 | { | |
804 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { | |
805 | /* remove new line character */ | |
806 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); | |
807 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); | |
808 | } else { | |
809 | verbose(env, "%d:", env->insn_idx); | |
810 | } | |
811 | print_verifier_state(env, state, false); | |
812 | } | |
813 | ||
c69431aa LB |
814 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
815 | * small to hold src. This is different from krealloc since we don't want to preserve | |
816 | * the contents of dst. | |
817 | * | |
818 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
819 | * not be allocated. | |
638f5b90 | 820 | */ |
c69431aa | 821 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 822 | { |
c69431aa LB |
823 | size_t bytes; |
824 | ||
825 | if (ZERO_OR_NULL_PTR(src)) | |
826 | goto out; | |
827 | ||
828 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
829 | return NULL; | |
830 | ||
831 | if (ksize(dst) < bytes) { | |
832 | kfree(dst); | |
833 | dst = kmalloc_track_caller(bytes, flags); | |
834 | if (!dst) | |
835 | return NULL; | |
836 | } | |
837 | ||
838 | memcpy(dst, src, bytes); | |
839 | out: | |
840 | return dst ? dst : ZERO_SIZE_PTR; | |
841 | } | |
842 | ||
843 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
844 | * small to hold new_n items. new items are zeroed out if the array grows. | |
845 | * | |
846 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
847 | */ | |
848 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
849 | { | |
850 | if (!new_n || old_n == new_n) | |
851 | goto out; | |
852 | ||
853 | arr = krealloc_array(arr, new_n, size, GFP_KERNEL); | |
854 | if (!arr) | |
855 | return NULL; | |
856 | ||
857 | if (new_n > old_n) | |
858 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
859 | ||
860 | out: | |
861 | return arr ? arr : ZERO_SIZE_PTR; | |
862 | } | |
863 | ||
864 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
865 | { | |
866 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
867 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
868 | if (!dst->refs) | |
869 | return -ENOMEM; | |
870 | ||
871 | dst->acquired_refs = src->acquired_refs; | |
872 | return 0; | |
873 | } | |
874 | ||
875 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
876 | { | |
877 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
878 | ||
879 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
880 | GFP_KERNEL); | |
881 | if (!dst->stack) | |
882 | return -ENOMEM; | |
883 | ||
884 | dst->allocated_stack = src->allocated_stack; | |
885 | return 0; | |
886 | } | |
887 | ||
888 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
889 | { | |
890 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
891 | sizeof(struct bpf_reference_state)); | |
892 | if (!state->refs) | |
893 | return -ENOMEM; | |
894 | ||
895 | state->acquired_refs = n; | |
896 | return 0; | |
897 | } | |
898 | ||
899 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
900 | { | |
901 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
902 | ||
903 | if (old_n >= n) | |
904 | return 0; | |
905 | ||
906 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
907 | if (!state->stack) | |
908 | return -ENOMEM; | |
909 | ||
910 | state->allocated_stack = size; | |
911 | return 0; | |
fd978bf7 JS |
912 | } |
913 | ||
914 | /* Acquire a pointer id from the env and update the state->refs to include | |
915 | * this new pointer reference. | |
916 | * On success, returns a valid pointer id to associate with the register | |
917 | * On failure, returns a negative errno. | |
638f5b90 | 918 | */ |
fd978bf7 | 919 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 920 | { |
fd978bf7 JS |
921 | struct bpf_func_state *state = cur_func(env); |
922 | int new_ofs = state->acquired_refs; | |
923 | int id, err; | |
924 | ||
c69431aa | 925 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
926 | if (err) |
927 | return err; | |
928 | id = ++env->id_gen; | |
929 | state->refs[new_ofs].id = id; | |
930 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 931 | |
fd978bf7 JS |
932 | return id; |
933 | } | |
934 | ||
935 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 936 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
937 | { |
938 | int i, last_idx; | |
939 | ||
fd978bf7 JS |
940 | last_idx = state->acquired_refs - 1; |
941 | for (i = 0; i < state->acquired_refs; i++) { | |
942 | if (state->refs[i].id == ptr_id) { | |
943 | if (last_idx && i != last_idx) | |
944 | memcpy(&state->refs[i], &state->refs[last_idx], | |
945 | sizeof(*state->refs)); | |
946 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
947 | state->acquired_refs--; | |
638f5b90 | 948 | return 0; |
638f5b90 | 949 | } |
638f5b90 | 950 | } |
46f8bc92 | 951 | return -EINVAL; |
fd978bf7 JS |
952 | } |
953 | ||
f4d7e40a AS |
954 | static void free_func_state(struct bpf_func_state *state) |
955 | { | |
5896351e AS |
956 | if (!state) |
957 | return; | |
fd978bf7 | 958 | kfree(state->refs); |
f4d7e40a AS |
959 | kfree(state->stack); |
960 | kfree(state); | |
961 | } | |
962 | ||
b5dc0163 AS |
963 | static void clear_jmp_history(struct bpf_verifier_state *state) |
964 | { | |
965 | kfree(state->jmp_history); | |
966 | state->jmp_history = NULL; | |
967 | state->jmp_history_cnt = 0; | |
968 | } | |
969 | ||
1969db47 AS |
970 | static void free_verifier_state(struct bpf_verifier_state *state, |
971 | bool free_self) | |
638f5b90 | 972 | { |
f4d7e40a AS |
973 | int i; |
974 | ||
975 | for (i = 0; i <= state->curframe; i++) { | |
976 | free_func_state(state->frame[i]); | |
977 | state->frame[i] = NULL; | |
978 | } | |
b5dc0163 | 979 | clear_jmp_history(state); |
1969db47 AS |
980 | if (free_self) |
981 | kfree(state); | |
638f5b90 AS |
982 | } |
983 | ||
984 | /* copy verifier state from src to dst growing dst stack space | |
985 | * when necessary to accommodate larger src stack | |
986 | */ | |
f4d7e40a AS |
987 | static int copy_func_state(struct bpf_func_state *dst, |
988 | const struct bpf_func_state *src) | |
638f5b90 AS |
989 | { |
990 | int err; | |
991 | ||
fd978bf7 JS |
992 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
993 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
994 | if (err) |
995 | return err; | |
638f5b90 AS |
996 | return copy_stack_state(dst, src); |
997 | } | |
998 | ||
f4d7e40a AS |
999 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1000 | const struct bpf_verifier_state *src) | |
1001 | { | |
1002 | struct bpf_func_state *dst; | |
1003 | int i, err; | |
1004 | ||
06ab6a50 LB |
1005 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1006 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1007 | GFP_USER); | |
1008 | if (!dst_state->jmp_history) | |
1009 | return -ENOMEM; | |
b5dc0163 AS |
1010 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1011 | ||
f4d7e40a AS |
1012 | /* if dst has more stack frames then src frame, free them */ |
1013 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1014 | free_func_state(dst_state->frame[i]); | |
1015 | dst_state->frame[i] = NULL; | |
1016 | } | |
979d63d5 | 1017 | dst_state->speculative = src->speculative; |
f4d7e40a | 1018 | dst_state->curframe = src->curframe; |
d83525ca | 1019 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
1020 | dst_state->branches = src->branches; |
1021 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1022 | dst_state->first_insn_idx = src->first_insn_idx; |
1023 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1024 | for (i = 0; i <= src->curframe; i++) { |
1025 | dst = dst_state->frame[i]; | |
1026 | if (!dst) { | |
1027 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1028 | if (!dst) | |
1029 | return -ENOMEM; | |
1030 | dst_state->frame[i] = dst; | |
1031 | } | |
1032 | err = copy_func_state(dst, src->frame[i]); | |
1033 | if (err) | |
1034 | return err; | |
1035 | } | |
1036 | return 0; | |
1037 | } | |
1038 | ||
2589726d AS |
1039 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1040 | { | |
1041 | while (st) { | |
1042 | u32 br = --st->branches; | |
1043 | ||
1044 | /* WARN_ON(br > 1) technically makes sense here, | |
1045 | * but see comment in push_stack(), hence: | |
1046 | */ | |
1047 | WARN_ONCE((int)br < 0, | |
1048 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1049 | br); | |
1050 | if (br) | |
1051 | break; | |
1052 | st = st->parent; | |
1053 | } | |
1054 | } | |
1055 | ||
638f5b90 | 1056 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1057 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1058 | { |
1059 | struct bpf_verifier_state *cur = env->cur_state; | |
1060 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1061 | int err; | |
17a52670 AS |
1062 | |
1063 | if (env->head == NULL) | |
638f5b90 | 1064 | return -ENOENT; |
17a52670 | 1065 | |
638f5b90 AS |
1066 | if (cur) { |
1067 | err = copy_verifier_state(cur, &head->st); | |
1068 | if (err) | |
1069 | return err; | |
1070 | } | |
6f8a57cc AN |
1071 | if (pop_log) |
1072 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1073 | if (insn_idx) |
1074 | *insn_idx = head->insn_idx; | |
17a52670 | 1075 | if (prev_insn_idx) |
638f5b90 AS |
1076 | *prev_insn_idx = head->prev_insn_idx; |
1077 | elem = head->next; | |
1969db47 | 1078 | free_verifier_state(&head->st, false); |
638f5b90 | 1079 | kfree(head); |
17a52670 AS |
1080 | env->head = elem; |
1081 | env->stack_size--; | |
638f5b90 | 1082 | return 0; |
17a52670 AS |
1083 | } |
1084 | ||
58e2af8b | 1085 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1086 | int insn_idx, int prev_insn_idx, |
1087 | bool speculative) | |
17a52670 | 1088 | { |
638f5b90 | 1089 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1090 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1091 | int err; |
17a52670 | 1092 | |
638f5b90 | 1093 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1094 | if (!elem) |
1095 | goto err; | |
1096 | ||
17a52670 AS |
1097 | elem->insn_idx = insn_idx; |
1098 | elem->prev_insn_idx = prev_insn_idx; | |
1099 | elem->next = env->head; | |
6f8a57cc | 1100 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1101 | env->head = elem; |
1102 | env->stack_size++; | |
1969db47 AS |
1103 | err = copy_verifier_state(&elem->st, cur); |
1104 | if (err) | |
1105 | goto err; | |
979d63d5 | 1106 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1107 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1108 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1109 | env->stack_size); | |
17a52670 AS |
1110 | goto err; |
1111 | } | |
2589726d AS |
1112 | if (elem->st.parent) { |
1113 | ++elem->st.parent->branches; | |
1114 | /* WARN_ON(branches > 2) technically makes sense here, | |
1115 | * but | |
1116 | * 1. speculative states will bump 'branches' for non-branch | |
1117 | * instructions | |
1118 | * 2. is_state_visited() heuristics may decide not to create | |
1119 | * a new state for a sequence of branches and all such current | |
1120 | * and cloned states will be pointing to a single parent state | |
1121 | * which might have large 'branches' count. | |
1122 | */ | |
1123 | } | |
17a52670 AS |
1124 | return &elem->st; |
1125 | err: | |
5896351e AS |
1126 | free_verifier_state(env->cur_state, true); |
1127 | env->cur_state = NULL; | |
17a52670 | 1128 | /* pop all elements and return */ |
6f8a57cc | 1129 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1130 | return NULL; |
1131 | } | |
1132 | ||
1133 | #define CALLER_SAVED_REGS 6 | |
1134 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1135 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1136 | }; | |
1137 | ||
f54c7898 DB |
1138 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1139 | struct bpf_reg_state *reg); | |
f1174f77 | 1140 | |
e688c3db AS |
1141 | /* This helper doesn't clear reg->id */ |
1142 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1143 | { |
b03c9f9f EC |
1144 | reg->var_off = tnum_const(imm); |
1145 | reg->smin_value = (s64)imm; | |
1146 | reg->smax_value = (s64)imm; | |
1147 | reg->umin_value = imm; | |
1148 | reg->umax_value = imm; | |
3f50f132 JF |
1149 | |
1150 | reg->s32_min_value = (s32)imm; | |
1151 | reg->s32_max_value = (s32)imm; | |
1152 | reg->u32_min_value = (u32)imm; | |
1153 | reg->u32_max_value = (u32)imm; | |
1154 | } | |
1155 | ||
e688c3db AS |
1156 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1157 | * known to have the value @imm. | |
1158 | */ | |
1159 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1160 | { | |
1161 | /* Clear id, off, and union(map_ptr, range) */ | |
1162 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1163 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
1164 | ___mark_reg_known(reg, imm); | |
1165 | } | |
1166 | ||
3f50f132 JF |
1167 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1168 | { | |
1169 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1170 | reg->s32_min_value = (s32)imm; | |
1171 | reg->s32_max_value = (s32)imm; | |
1172 | reg->u32_min_value = (u32)imm; | |
1173 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1174 | } |
1175 | ||
f1174f77 EC |
1176 | /* Mark the 'variable offset' part of a register as zero. This should be |
1177 | * used only on registers holding a pointer type. | |
1178 | */ | |
1179 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1180 | { |
b03c9f9f | 1181 | __mark_reg_known(reg, 0); |
f1174f77 | 1182 | } |
a9789ef9 | 1183 | |
cc2b14d5 AS |
1184 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1185 | { | |
1186 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1187 | reg->type = SCALAR_VALUE; |
1188 | } | |
1189 | ||
61bd5218 JK |
1190 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1191 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1192 | { |
1193 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1194 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1195 | /* Something bad happened, let's kill all regs */ |
1196 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1197 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1198 | return; |
1199 | } | |
1200 | __mark_reg_known_zero(regs + regno); | |
1201 | } | |
1202 | ||
4ddb7416 DB |
1203 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1204 | { | |
1205 | switch (reg->type) { | |
1206 | case PTR_TO_MAP_VALUE_OR_NULL: { | |
1207 | const struct bpf_map *map = reg->map_ptr; | |
1208 | ||
1209 | if (map->inner_map_meta) { | |
1210 | reg->type = CONST_PTR_TO_MAP; | |
1211 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1212 | /* transfer reg's id which is unique for every map_lookup_elem |
1213 | * as UID of the inner map. | |
1214 | */ | |
34d11a44 AS |
1215 | if (map_value_has_timer(map->inner_map_meta)) |
1216 | reg->map_uid = reg->id; | |
4ddb7416 DB |
1217 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1218 | reg->type = PTR_TO_XDP_SOCK; | |
1219 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1220 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1221 | reg->type = PTR_TO_SOCKET; | |
1222 | } else { | |
1223 | reg->type = PTR_TO_MAP_VALUE; | |
1224 | } | |
1225 | break; | |
1226 | } | |
1227 | case PTR_TO_SOCKET_OR_NULL: | |
1228 | reg->type = PTR_TO_SOCKET; | |
1229 | break; | |
1230 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
1231 | reg->type = PTR_TO_SOCK_COMMON; | |
1232 | break; | |
1233 | case PTR_TO_TCP_SOCK_OR_NULL: | |
1234 | reg->type = PTR_TO_TCP_SOCK; | |
1235 | break; | |
1236 | case PTR_TO_BTF_ID_OR_NULL: | |
1237 | reg->type = PTR_TO_BTF_ID; | |
1238 | break; | |
1239 | case PTR_TO_MEM_OR_NULL: | |
1240 | reg->type = PTR_TO_MEM; | |
1241 | break; | |
1242 | case PTR_TO_RDONLY_BUF_OR_NULL: | |
1243 | reg->type = PTR_TO_RDONLY_BUF; | |
1244 | break; | |
1245 | case PTR_TO_RDWR_BUF_OR_NULL: | |
1246 | reg->type = PTR_TO_RDWR_BUF; | |
1247 | break; | |
1248 | default: | |
33ccec5f | 1249 | WARN_ONCE(1, "unknown nullable register type"); |
4ddb7416 DB |
1250 | } |
1251 | } | |
1252 | ||
de8f3a83 DB |
1253 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1254 | { | |
1255 | return type_is_pkt_pointer(reg->type); | |
1256 | } | |
1257 | ||
1258 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1259 | { | |
1260 | return reg_is_pkt_pointer(reg) || | |
1261 | reg->type == PTR_TO_PACKET_END; | |
1262 | } | |
1263 | ||
1264 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1265 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1266 | enum bpf_reg_type which) | |
1267 | { | |
1268 | /* The register can already have a range from prior markings. | |
1269 | * This is fine as long as it hasn't been advanced from its | |
1270 | * origin. | |
1271 | */ | |
1272 | return reg->type == which && | |
1273 | reg->id == 0 && | |
1274 | reg->off == 0 && | |
1275 | tnum_equals_const(reg->var_off, 0); | |
1276 | } | |
1277 | ||
3f50f132 JF |
1278 | /* Reset the min/max bounds of a register */ |
1279 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1280 | { | |
1281 | reg->smin_value = S64_MIN; | |
1282 | reg->smax_value = S64_MAX; | |
1283 | reg->umin_value = 0; | |
1284 | reg->umax_value = U64_MAX; | |
1285 | ||
1286 | reg->s32_min_value = S32_MIN; | |
1287 | reg->s32_max_value = S32_MAX; | |
1288 | reg->u32_min_value = 0; | |
1289 | reg->u32_max_value = U32_MAX; | |
1290 | } | |
1291 | ||
1292 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1293 | { | |
1294 | reg->smin_value = S64_MIN; | |
1295 | reg->smax_value = S64_MAX; | |
1296 | reg->umin_value = 0; | |
1297 | reg->umax_value = U64_MAX; | |
1298 | } | |
1299 | ||
1300 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1301 | { | |
1302 | reg->s32_min_value = S32_MIN; | |
1303 | reg->s32_max_value = S32_MAX; | |
1304 | reg->u32_min_value = 0; | |
1305 | reg->u32_max_value = U32_MAX; | |
1306 | } | |
1307 | ||
1308 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1309 | { | |
1310 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1311 | ||
1312 | /* min signed is max(sign bit) | min(other bits) */ | |
1313 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1314 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1315 | /* max signed is min(sign bit) | max(other bits) */ | |
1316 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1317 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1318 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1319 | reg->u32_max_value = min(reg->u32_max_value, | |
1320 | (u32)(var32_off.value | var32_off.mask)); | |
1321 | } | |
1322 | ||
1323 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1324 | { |
1325 | /* min signed is max(sign bit) | min(other bits) */ | |
1326 | reg->smin_value = max_t(s64, reg->smin_value, | |
1327 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1328 | /* max signed is min(sign bit) | max(other bits) */ | |
1329 | reg->smax_value = min_t(s64, reg->smax_value, | |
1330 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1331 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1332 | reg->umax_value = min(reg->umax_value, | |
1333 | reg->var_off.value | reg->var_off.mask); | |
1334 | } | |
1335 | ||
3f50f132 JF |
1336 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1337 | { | |
1338 | __update_reg32_bounds(reg); | |
1339 | __update_reg64_bounds(reg); | |
1340 | } | |
1341 | ||
b03c9f9f | 1342 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1343 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1344 | { | |
1345 | /* Learn sign from signed bounds. | |
1346 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1347 | * are the same, so combine. This works even in the negative case, e.g. | |
1348 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1349 | */ | |
1350 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1351 | reg->s32_min_value = reg->u32_min_value = | |
1352 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1353 | reg->s32_max_value = reg->u32_max_value = | |
1354 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1355 | return; | |
1356 | } | |
1357 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1358 | * boundary, so we must be careful. | |
1359 | */ | |
1360 | if ((s32)reg->u32_max_value >= 0) { | |
1361 | /* Positive. We can't learn anything from the smin, but smax | |
1362 | * is positive, hence safe. | |
1363 | */ | |
1364 | reg->s32_min_value = reg->u32_min_value; | |
1365 | reg->s32_max_value = reg->u32_max_value = | |
1366 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1367 | } else if ((s32)reg->u32_min_value < 0) { | |
1368 | /* Negative. We can't learn anything from the smax, but smin | |
1369 | * is negative, hence safe. | |
1370 | */ | |
1371 | reg->s32_min_value = reg->u32_min_value = | |
1372 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1373 | reg->s32_max_value = reg->u32_max_value; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1378 | { |
1379 | /* Learn sign from signed bounds. | |
1380 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1381 | * are the same, so combine. This works even in the negative case, e.g. | |
1382 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1383 | */ | |
1384 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1385 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1386 | reg->umin_value); | |
1387 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1388 | reg->umax_value); | |
1389 | return; | |
1390 | } | |
1391 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1392 | * boundary, so we must be careful. | |
1393 | */ | |
1394 | if ((s64)reg->umax_value >= 0) { | |
1395 | /* Positive. We can't learn anything from the smin, but smax | |
1396 | * is positive, hence safe. | |
1397 | */ | |
1398 | reg->smin_value = reg->umin_value; | |
1399 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1400 | reg->umax_value); | |
1401 | } else if ((s64)reg->umin_value < 0) { | |
1402 | /* Negative. We can't learn anything from the smax, but smin | |
1403 | * is negative, hence safe. | |
1404 | */ | |
1405 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1406 | reg->umin_value); | |
1407 | reg->smax_value = reg->umax_value; | |
1408 | } | |
1409 | } | |
1410 | ||
3f50f132 JF |
1411 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1412 | { | |
1413 | __reg32_deduce_bounds(reg); | |
1414 | __reg64_deduce_bounds(reg); | |
1415 | } | |
1416 | ||
b03c9f9f EC |
1417 | /* Attempts to improve var_off based on unsigned min/max information */ |
1418 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1419 | { | |
3f50f132 JF |
1420 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1421 | tnum_range(reg->umin_value, | |
1422 | reg->umax_value)); | |
1423 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1424 | tnum_range(reg->u32_min_value, | |
1425 | reg->u32_max_value)); | |
1426 | ||
1427 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1428 | } |
1429 | ||
3f50f132 | 1430 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1431 | { |
3f50f132 JF |
1432 | reg->umin_value = reg->u32_min_value; |
1433 | reg->umax_value = reg->u32_max_value; | |
1434 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds | |
1435 | * but must be positive otherwise set to worse case bounds | |
1436 | * and refine later from tnum. | |
1437 | */ | |
3a71dc36 | 1438 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
3f50f132 JF |
1439 | reg->smax_value = reg->s32_max_value; |
1440 | else | |
1441 | reg->smax_value = U32_MAX; | |
3a71dc36 JF |
1442 | if (reg->s32_min_value >= 0) |
1443 | reg->smin_value = reg->s32_min_value; | |
1444 | else | |
1445 | reg->smin_value = 0; | |
3f50f132 JF |
1446 | } |
1447 | ||
1448 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1449 | { | |
1450 | /* special case when 64-bit register has upper 32-bit register | |
1451 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1452 | * allowing us to use 32-bit bounds directly, | |
1453 | */ | |
1454 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1455 | __reg_assign_32_into_64(reg); | |
1456 | } else { | |
1457 | /* Otherwise the best we can do is push lower 32bit known and | |
1458 | * unknown bits into register (var_off set from jmp logic) | |
1459 | * then learn as much as possible from the 64-bit tnum | |
1460 | * known and unknown bits. The previous smin/smax bounds are | |
1461 | * invalid here because of jmp32 compare so mark them unknown | |
1462 | * so they do not impact tnum bounds calculation. | |
1463 | */ | |
1464 | __mark_reg64_unbounded(reg); | |
1465 | __update_reg_bounds(reg); | |
1466 | } | |
1467 | ||
1468 | /* Intersecting with the old var_off might have improved our bounds | |
1469 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1470 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1471 | */ | |
1472 | __reg_deduce_bounds(reg); | |
1473 | __reg_bound_offset(reg); | |
1474 | __update_reg_bounds(reg); | |
1475 | } | |
1476 | ||
1477 | static bool __reg64_bound_s32(s64 a) | |
1478 | { | |
388e2c0b | 1479 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
1480 | } |
1481 | ||
1482 | static bool __reg64_bound_u32(u64 a) | |
1483 | { | |
b9979db8 | 1484 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
1485 | } |
1486 | ||
1487 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1488 | { | |
1489 | __mark_reg32_unbounded(reg); | |
1490 | ||
b0270958 | 1491 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 1492 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 1493 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 1494 | } |
10bf4e83 | 1495 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 1496 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 1497 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 1498 | } |
3f50f132 JF |
1499 | |
1500 | /* Intersecting with the old var_off might have improved our bounds | |
1501 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1502 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1503 | */ | |
1504 | __reg_deduce_bounds(reg); | |
1505 | __reg_bound_offset(reg); | |
1506 | __update_reg_bounds(reg); | |
b03c9f9f EC |
1507 | } |
1508 | ||
f1174f77 | 1509 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1510 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1511 | struct bpf_reg_state *reg) | |
f1174f77 | 1512 | { |
a9c676bc AS |
1513 | /* |
1514 | * Clear type, id, off, and union(map_ptr, range) and | |
1515 | * padding between 'type' and union | |
1516 | */ | |
1517 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1518 | reg->type = SCALAR_VALUE; |
f1174f77 | 1519 | reg->var_off = tnum_unknown; |
f4d7e40a | 1520 | reg->frameno = 0; |
2c78ee89 | 1521 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1522 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1523 | } |
1524 | ||
61bd5218 JK |
1525 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1526 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1527 | { |
1528 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1529 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1530 | /* Something bad happened, let's kill all regs except FP */ |
1531 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1532 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1533 | return; |
1534 | } | |
f54c7898 | 1535 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1536 | } |
1537 | ||
f54c7898 DB |
1538 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1539 | struct bpf_reg_state *reg) | |
f1174f77 | 1540 | { |
f54c7898 | 1541 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1542 | reg->type = NOT_INIT; |
1543 | } | |
1544 | ||
61bd5218 JK |
1545 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1546 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1547 | { |
1548 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1549 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1550 | /* Something bad happened, let's kill all regs except FP */ |
1551 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1552 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1553 | return; |
1554 | } | |
f54c7898 | 1555 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1556 | } |
1557 | ||
41c48f3a AI |
1558 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1559 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f AN |
1560 | enum bpf_reg_type reg_type, |
1561 | struct btf *btf, u32 btf_id) | |
41c48f3a AI |
1562 | { |
1563 | if (reg_type == SCALAR_VALUE) { | |
1564 | mark_reg_unknown(env, regs, regno); | |
1565 | return; | |
1566 | } | |
1567 | mark_reg_known_zero(env, regs, regno); | |
1568 | regs[regno].type = PTR_TO_BTF_ID; | |
22dc4a0f | 1569 | regs[regno].btf = btf; |
41c48f3a AI |
1570 | regs[regno].btf_id = btf_id; |
1571 | } | |
1572 | ||
5327ed3d | 1573 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1574 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1575 | struct bpf_func_state *state) |
17a52670 | 1576 | { |
f4d7e40a | 1577 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1578 | int i; |
1579 | ||
dc503a8a | 1580 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1581 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1582 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1583 | regs[i].parent = NULL; |
5327ed3d | 1584 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1585 | } |
17a52670 AS |
1586 | |
1587 | /* frame pointer */ | |
f1174f77 | 1588 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1589 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1590 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1591 | } |
1592 | ||
f4d7e40a AS |
1593 | #define BPF_MAIN_FUNC (-1) |
1594 | static void init_func_state(struct bpf_verifier_env *env, | |
1595 | struct bpf_func_state *state, | |
1596 | int callsite, int frameno, int subprogno) | |
1597 | { | |
1598 | state->callsite = callsite; | |
1599 | state->frameno = frameno; | |
1600 | state->subprogno = subprogno; | |
1601 | init_reg_state(env, state); | |
0f55f9ed | 1602 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
1603 | } |
1604 | ||
bfc6bb74 AS |
1605 | /* Similar to push_stack(), but for async callbacks */ |
1606 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
1607 | int insn_idx, int prev_insn_idx, | |
1608 | int subprog) | |
1609 | { | |
1610 | struct bpf_verifier_stack_elem *elem; | |
1611 | struct bpf_func_state *frame; | |
1612 | ||
1613 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
1614 | if (!elem) | |
1615 | goto err; | |
1616 | ||
1617 | elem->insn_idx = insn_idx; | |
1618 | elem->prev_insn_idx = prev_insn_idx; | |
1619 | elem->next = env->head; | |
1620 | elem->log_pos = env->log.len_used; | |
1621 | env->head = elem; | |
1622 | env->stack_size++; | |
1623 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
1624 | verbose(env, | |
1625 | "The sequence of %d jumps is too complex for async cb.\n", | |
1626 | env->stack_size); | |
1627 | goto err; | |
1628 | } | |
1629 | /* Unlike push_stack() do not copy_verifier_state(). | |
1630 | * The caller state doesn't matter. | |
1631 | * This is async callback. It starts in a fresh stack. | |
1632 | * Initialize it similar to do_check_common(). | |
1633 | */ | |
1634 | elem->st.branches = 1; | |
1635 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
1636 | if (!frame) | |
1637 | goto err; | |
1638 | init_func_state(env, frame, | |
1639 | BPF_MAIN_FUNC /* callsite */, | |
1640 | 0 /* frameno within this callchain */, | |
1641 | subprog /* subprog number within this prog */); | |
1642 | elem->st.frame[0] = frame; | |
1643 | return &elem->st; | |
1644 | err: | |
1645 | free_verifier_state(env->cur_state, true); | |
1646 | env->cur_state = NULL; | |
1647 | /* pop all elements and return */ | |
1648 | while (!pop_stack(env, NULL, NULL, false)); | |
1649 | return NULL; | |
1650 | } | |
1651 | ||
1652 | ||
17a52670 AS |
1653 | enum reg_arg_type { |
1654 | SRC_OP, /* register is used as source operand */ | |
1655 | DST_OP, /* register is used as destination operand */ | |
1656 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1657 | }; | |
1658 | ||
cc8b0b92 AS |
1659 | static int cmp_subprogs(const void *a, const void *b) |
1660 | { | |
9c8105bd JW |
1661 | return ((struct bpf_subprog_info *)a)->start - |
1662 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1663 | } |
1664 | ||
1665 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1666 | { | |
9c8105bd | 1667 | struct bpf_subprog_info *p; |
cc8b0b92 | 1668 | |
9c8105bd JW |
1669 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1670 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1671 | if (!p) |
1672 | return -ENOENT; | |
9c8105bd | 1673 | return p - env->subprog_info; |
cc8b0b92 AS |
1674 | |
1675 | } | |
1676 | ||
1677 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1678 | { | |
1679 | int insn_cnt = env->prog->len; | |
1680 | int ret; | |
1681 | ||
1682 | if (off >= insn_cnt || off < 0) { | |
1683 | verbose(env, "call to invalid destination\n"); | |
1684 | return -EINVAL; | |
1685 | } | |
1686 | ret = find_subprog(env, off); | |
1687 | if (ret >= 0) | |
282a0f46 | 1688 | return ret; |
4cb3d99c | 1689 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1690 | verbose(env, "too many subprograms\n"); |
1691 | return -E2BIG; | |
1692 | } | |
e6ac2450 | 1693 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
1694 | env->subprog_info[env->subprog_cnt++].start = off; |
1695 | sort(env->subprog_info, env->subprog_cnt, | |
1696 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 1697 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
1698 | } |
1699 | ||
2357672c KKD |
1700 | #define MAX_KFUNC_DESCS 256 |
1701 | #define MAX_KFUNC_BTFS 256 | |
1702 | ||
e6ac2450 MKL |
1703 | struct bpf_kfunc_desc { |
1704 | struct btf_func_model func_model; | |
1705 | u32 func_id; | |
1706 | s32 imm; | |
2357672c KKD |
1707 | u16 offset; |
1708 | }; | |
1709 | ||
1710 | struct bpf_kfunc_btf { | |
1711 | struct btf *btf; | |
1712 | struct module *module; | |
1713 | u16 offset; | |
e6ac2450 MKL |
1714 | }; |
1715 | ||
e6ac2450 MKL |
1716 | struct bpf_kfunc_desc_tab { |
1717 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
1718 | u32 nr_descs; | |
1719 | }; | |
1720 | ||
2357672c KKD |
1721 | struct bpf_kfunc_btf_tab { |
1722 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
1723 | u32 nr_descs; | |
1724 | }; | |
1725 | ||
1726 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
1727 | { |
1728 | const struct bpf_kfunc_desc *d0 = a; | |
1729 | const struct bpf_kfunc_desc *d1 = b; | |
1730 | ||
1731 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
1732 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
1733 | } | |
1734 | ||
1735 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
1736 | { | |
1737 | const struct bpf_kfunc_btf *d0 = a; | |
1738 | const struct bpf_kfunc_btf *d1 = b; | |
1739 | ||
1740 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
1741 | } |
1742 | ||
1743 | static const struct bpf_kfunc_desc * | |
2357672c | 1744 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
1745 | { |
1746 | struct bpf_kfunc_desc desc = { | |
1747 | .func_id = func_id, | |
2357672c | 1748 | .offset = offset, |
e6ac2450 MKL |
1749 | }; |
1750 | struct bpf_kfunc_desc_tab *tab; | |
1751 | ||
1752 | tab = prog->aux->kfunc_tab; | |
1753 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
1754 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
1755 | } | |
1756 | ||
1757 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
1758 | s16 offset, struct module **btf_modp) | |
1759 | { | |
1760 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
1761 | struct bpf_kfunc_btf_tab *tab; | |
1762 | struct bpf_kfunc_btf *b; | |
1763 | struct module *mod; | |
1764 | struct btf *btf; | |
1765 | int btf_fd; | |
1766 | ||
1767 | tab = env->prog->aux->kfunc_btf_tab; | |
1768 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
1769 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
1770 | if (!b) { | |
1771 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
1772 | verbose(env, "too many different module BTFs\n"); | |
1773 | return ERR_PTR(-E2BIG); | |
1774 | } | |
1775 | ||
1776 | if (bpfptr_is_null(env->fd_array)) { | |
1777 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
1778 | return ERR_PTR(-EPROTO); | |
1779 | } | |
1780 | ||
1781 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
1782 | offset * sizeof(btf_fd), | |
1783 | sizeof(btf_fd))) | |
1784 | return ERR_PTR(-EFAULT); | |
1785 | ||
1786 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
1787 | if (IS_ERR(btf)) { |
1788 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 1789 | return btf; |
588cd7ef | 1790 | } |
2357672c KKD |
1791 | |
1792 | if (!btf_is_module(btf)) { | |
1793 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
1794 | btf_put(btf); | |
1795 | return ERR_PTR(-EINVAL); | |
1796 | } | |
1797 | ||
1798 | mod = btf_try_get_module(btf); | |
1799 | if (!mod) { | |
1800 | btf_put(btf); | |
1801 | return ERR_PTR(-ENXIO); | |
1802 | } | |
1803 | ||
1804 | b = &tab->descs[tab->nr_descs++]; | |
1805 | b->btf = btf; | |
1806 | b->module = mod; | |
1807 | b->offset = offset; | |
1808 | ||
1809 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1810 | kfunc_btf_cmp_by_off, NULL); | |
1811 | } | |
1812 | if (btf_modp) | |
1813 | *btf_modp = b->module; | |
1814 | return b->btf; | |
e6ac2450 MKL |
1815 | } |
1816 | ||
2357672c KKD |
1817 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
1818 | { | |
1819 | if (!tab) | |
1820 | return; | |
1821 | ||
1822 | while (tab->nr_descs--) { | |
1823 | module_put(tab->descs[tab->nr_descs].module); | |
1824 | btf_put(tab->descs[tab->nr_descs].btf); | |
1825 | } | |
1826 | kfree(tab); | |
1827 | } | |
1828 | ||
1829 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
1830 | u32 func_id, s16 offset, | |
1831 | struct module **btf_modp) | |
1832 | { | |
2357672c KKD |
1833 | if (offset) { |
1834 | if (offset < 0) { | |
1835 | /* In the future, this can be allowed to increase limit | |
1836 | * of fd index into fd_array, interpreted as u16. | |
1837 | */ | |
1838 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
1839 | return ERR_PTR(-EINVAL); | |
1840 | } | |
1841 | ||
588cd7ef | 1842 | return __find_kfunc_desc_btf(env, offset, btf_modp); |
2357672c KKD |
1843 | } |
1844 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
1845 | } |
1846 | ||
2357672c | 1847 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
1848 | { |
1849 | const struct btf_type *func, *func_proto; | |
2357672c | 1850 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
1851 | struct bpf_kfunc_desc_tab *tab; |
1852 | struct bpf_prog_aux *prog_aux; | |
1853 | struct bpf_kfunc_desc *desc; | |
1854 | const char *func_name; | |
2357672c | 1855 | struct btf *desc_btf; |
e6ac2450 MKL |
1856 | unsigned long addr; |
1857 | int err; | |
1858 | ||
1859 | prog_aux = env->prog->aux; | |
1860 | tab = prog_aux->kfunc_tab; | |
2357672c | 1861 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
1862 | if (!tab) { |
1863 | if (!btf_vmlinux) { | |
1864 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
1865 | return -ENOTSUPP; | |
1866 | } | |
1867 | ||
1868 | if (!env->prog->jit_requested) { | |
1869 | verbose(env, "JIT is required for calling kernel function\n"); | |
1870 | return -ENOTSUPP; | |
1871 | } | |
1872 | ||
1873 | if (!bpf_jit_supports_kfunc_call()) { | |
1874 | verbose(env, "JIT does not support calling kernel function\n"); | |
1875 | return -ENOTSUPP; | |
1876 | } | |
1877 | ||
1878 | if (!env->prog->gpl_compatible) { | |
1879 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
1880 | return -EINVAL; | |
1881 | } | |
1882 | ||
1883 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
1884 | if (!tab) | |
1885 | return -ENOMEM; | |
1886 | prog_aux->kfunc_tab = tab; | |
1887 | } | |
1888 | ||
a5d82727 KKD |
1889 | /* func_id == 0 is always invalid, but instead of returning an error, be |
1890 | * conservative and wait until the code elimination pass before returning | |
1891 | * error, so that invalid calls that get pruned out can be in BPF programs | |
1892 | * loaded from userspace. It is also required that offset be untouched | |
1893 | * for such calls. | |
1894 | */ | |
1895 | if (!func_id && !offset) | |
1896 | return 0; | |
1897 | ||
2357672c KKD |
1898 | if (!btf_tab && offset) { |
1899 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
1900 | if (!btf_tab) | |
1901 | return -ENOMEM; | |
1902 | prog_aux->kfunc_btf_tab = btf_tab; | |
1903 | } | |
1904 | ||
1905 | desc_btf = find_kfunc_desc_btf(env, func_id, offset, NULL); | |
1906 | if (IS_ERR(desc_btf)) { | |
1907 | verbose(env, "failed to find BTF for kernel function\n"); | |
1908 | return PTR_ERR(desc_btf); | |
1909 | } | |
1910 | ||
1911 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
1912 | return 0; |
1913 | ||
1914 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
1915 | verbose(env, "too many different kernel function calls\n"); | |
1916 | return -E2BIG; | |
1917 | } | |
1918 | ||
2357672c | 1919 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
1920 | if (!func || !btf_type_is_func(func)) { |
1921 | verbose(env, "kernel btf_id %u is not a function\n", | |
1922 | func_id); | |
1923 | return -EINVAL; | |
1924 | } | |
2357672c | 1925 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
1926 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
1927 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
1928 | func_id); | |
1929 | return -EINVAL; | |
1930 | } | |
1931 | ||
2357672c | 1932 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
1933 | addr = kallsyms_lookup_name(func_name); |
1934 | if (!addr) { | |
1935 | verbose(env, "cannot find address for kernel function %s\n", | |
1936 | func_name); | |
1937 | return -EINVAL; | |
1938 | } | |
1939 | ||
1940 | desc = &tab->descs[tab->nr_descs++]; | |
1941 | desc->func_id = func_id; | |
3d717fad | 1942 | desc->imm = BPF_CALL_IMM(addr); |
2357672c KKD |
1943 | desc->offset = offset; |
1944 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
1945 | func_proto, func_name, |
1946 | &desc->func_model); | |
1947 | if (!err) | |
1948 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 1949 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
1950 | return err; |
1951 | } | |
1952 | ||
1953 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
1954 | { | |
1955 | const struct bpf_kfunc_desc *d0 = a; | |
1956 | const struct bpf_kfunc_desc *d1 = b; | |
1957 | ||
1958 | if (d0->imm > d1->imm) | |
1959 | return 1; | |
1960 | else if (d0->imm < d1->imm) | |
1961 | return -1; | |
1962 | return 0; | |
1963 | } | |
1964 | ||
1965 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
1966 | { | |
1967 | struct bpf_kfunc_desc_tab *tab; | |
1968 | ||
1969 | tab = prog->aux->kfunc_tab; | |
1970 | if (!tab) | |
1971 | return; | |
1972 | ||
1973 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1974 | kfunc_desc_cmp_by_imm, NULL); | |
1975 | } | |
1976 | ||
1977 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
1978 | { | |
1979 | return !!prog->aux->kfunc_tab; | |
1980 | } | |
1981 | ||
1982 | const struct btf_func_model * | |
1983 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
1984 | const struct bpf_insn *insn) | |
1985 | { | |
1986 | const struct bpf_kfunc_desc desc = { | |
1987 | .imm = insn->imm, | |
1988 | }; | |
1989 | const struct bpf_kfunc_desc *res; | |
1990 | struct bpf_kfunc_desc_tab *tab; | |
1991 | ||
1992 | tab = prog->aux->kfunc_tab; | |
1993 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
1994 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
1995 | ||
1996 | return res ? &res->func_model : NULL; | |
1997 | } | |
1998 | ||
1999 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2000 | { |
9c8105bd | 2001 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2002 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2003 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2004 | |
f910cefa JW |
2005 | /* Add entry function. */ |
2006 | ret = add_subprog(env, 0); | |
e6ac2450 | 2007 | if (ret) |
f910cefa JW |
2008 | return ret; |
2009 | ||
e6ac2450 MKL |
2010 | for (i = 0; i < insn_cnt; i++, insn++) { |
2011 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2012 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2013 | continue; |
e6ac2450 | 2014 | |
2c78ee89 | 2015 | if (!env->bpf_capable) { |
e6ac2450 | 2016 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2017 | return -EPERM; |
2018 | } | |
e6ac2450 | 2019 | |
3990ed4c | 2020 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2021 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2022 | else |
2357672c | 2023 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2024 | |
cc8b0b92 AS |
2025 | if (ret < 0) |
2026 | return ret; | |
2027 | } | |
2028 | ||
4cb3d99c JW |
2029 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2030 | * logic. 'subprog_cnt' should not be increased. | |
2031 | */ | |
2032 | subprog[env->subprog_cnt].start = insn_cnt; | |
2033 | ||
06ee7115 | 2034 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2035 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2036 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2037 | |
e6ac2450 MKL |
2038 | return 0; |
2039 | } | |
2040 | ||
2041 | static int check_subprogs(struct bpf_verifier_env *env) | |
2042 | { | |
2043 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2044 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2045 | struct bpf_insn *insn = env->prog->insnsi; | |
2046 | int insn_cnt = env->prog->len; | |
2047 | ||
cc8b0b92 | 2048 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2049 | subprog_start = subprog[cur_subprog].start; |
2050 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2051 | for (i = 0; i < insn_cnt; i++) { |
2052 | u8 code = insn[i].code; | |
2053 | ||
7f6e4312 MF |
2054 | if (code == (BPF_JMP | BPF_CALL) && |
2055 | insn[i].imm == BPF_FUNC_tail_call && | |
2056 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
2057 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
2058 | if (BPF_CLASS(code) == BPF_LD && |
2059 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2060 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2061 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2062 | goto next; |
2063 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2064 | goto next; | |
2065 | off = i + insn[i].off + 1; | |
2066 | if (off < subprog_start || off >= subprog_end) { | |
2067 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2068 | return -EINVAL; | |
2069 | } | |
2070 | next: | |
2071 | if (i == subprog_end - 1) { | |
2072 | /* to avoid fall-through from one subprog into another | |
2073 | * the last insn of the subprog should be either exit | |
2074 | * or unconditional jump back | |
2075 | */ | |
2076 | if (code != (BPF_JMP | BPF_EXIT) && | |
2077 | code != (BPF_JMP | BPF_JA)) { | |
2078 | verbose(env, "last insn is not an exit or jmp\n"); | |
2079 | return -EINVAL; | |
2080 | } | |
2081 | subprog_start = subprog_end; | |
4cb3d99c JW |
2082 | cur_subprog++; |
2083 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2084 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2085 | } |
2086 | } | |
2087 | return 0; | |
2088 | } | |
2089 | ||
679c782d EC |
2090 | /* Parentage chain of this register (or stack slot) should take care of all |
2091 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2092 | */ | |
f4d7e40a | 2093 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2094 | const struct bpf_reg_state *state, |
5327ed3d | 2095 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2096 | { |
2097 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2098 | int cnt = 0; |
dc503a8a EC |
2099 | |
2100 | while (parent) { | |
2101 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2102 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2103 | break; |
9242b5f5 AS |
2104 | if (parent->live & REG_LIVE_DONE) { |
2105 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
2106 | reg_type_str[parent->type], | |
2107 | parent->var_off.value, parent->off); | |
2108 | return -EFAULT; | |
2109 | } | |
5327ed3d JW |
2110 | /* The first condition is more likely to be true than the |
2111 | * second, checked it first. | |
2112 | */ | |
2113 | if ((parent->live & REG_LIVE_READ) == flag || | |
2114 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2115 | /* The parentage chain never changes and |
2116 | * this parent was already marked as LIVE_READ. | |
2117 | * There is no need to keep walking the chain again and | |
2118 | * keep re-marking all parents as LIVE_READ. | |
2119 | * This case happens when the same register is read | |
2120 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2121 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2122 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2123 | */ |
2124 | break; | |
dc503a8a | 2125 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2126 | parent->live |= flag; |
2127 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2128 | if (flag == REG_LIVE_READ64) | |
2129 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2130 | state = parent; |
2131 | parent = state->parent; | |
f4d7e40a | 2132 | writes = true; |
06ee7115 | 2133 | cnt++; |
dc503a8a | 2134 | } |
06ee7115 AS |
2135 | |
2136 | if (env->longest_mark_read_walk < cnt) | |
2137 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2138 | return 0; |
dc503a8a EC |
2139 | } |
2140 | ||
5327ed3d JW |
2141 | /* This function is supposed to be used by the following 32-bit optimization |
2142 | * code only. It returns TRUE if the source or destination register operates | |
2143 | * on 64-bit, otherwise return FALSE. | |
2144 | */ | |
2145 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2146 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2147 | { | |
2148 | u8 code, class, op; | |
2149 | ||
2150 | code = insn->code; | |
2151 | class = BPF_CLASS(code); | |
2152 | op = BPF_OP(code); | |
2153 | if (class == BPF_JMP) { | |
2154 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2155 | * conservatively. | |
2156 | */ | |
2157 | if (op == BPF_EXIT) | |
2158 | return true; | |
2159 | if (op == BPF_CALL) { | |
2160 | /* BPF to BPF call will reach here because of marking | |
2161 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2162 | * don't care the register def because they are anyway | |
2163 | * marked as NOT_INIT already. | |
2164 | */ | |
2165 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2166 | return false; | |
2167 | /* Helper call will reach here because of arg type | |
2168 | * check, conservatively return TRUE. | |
2169 | */ | |
2170 | if (t == SRC_OP) | |
2171 | return true; | |
2172 | ||
2173 | return false; | |
2174 | } | |
2175 | } | |
2176 | ||
2177 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2178 | /* BPF_END always use BPF_ALU class. */ | |
2179 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2180 | return true; | |
2181 | ||
2182 | if (class == BPF_ALU || class == BPF_JMP32) | |
2183 | return false; | |
2184 | ||
2185 | if (class == BPF_LDX) { | |
2186 | if (t != SRC_OP) | |
2187 | return BPF_SIZE(code) == BPF_DW; | |
2188 | /* LDX source must be ptr. */ | |
2189 | return true; | |
2190 | } | |
2191 | ||
2192 | if (class == BPF_STX) { | |
83a28819 IL |
2193 | /* BPF_STX (including atomic variants) has multiple source |
2194 | * operands, one of which is a ptr. Check whether the caller is | |
2195 | * asking about it. | |
2196 | */ | |
2197 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2198 | return true; |
2199 | return BPF_SIZE(code) == BPF_DW; | |
2200 | } | |
2201 | ||
2202 | if (class == BPF_LD) { | |
2203 | u8 mode = BPF_MODE(code); | |
2204 | ||
2205 | /* LD_IMM64 */ | |
2206 | if (mode == BPF_IMM) | |
2207 | return true; | |
2208 | ||
2209 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
2210 | if (t != SRC_OP) | |
2211 | return false; | |
2212 | ||
2213 | /* Implicit ctx ptr. */ | |
2214 | if (regno == BPF_REG_6) | |
2215 | return true; | |
2216 | ||
2217 | /* Explicit source could be any width. */ | |
2218 | return true; | |
2219 | } | |
2220 | ||
2221 | if (class == BPF_ST) | |
2222 | /* The only source register for BPF_ST is a ptr. */ | |
2223 | return true; | |
2224 | ||
2225 | /* Conservatively return true at default. */ | |
2226 | return true; | |
2227 | } | |
2228 | ||
83a28819 IL |
2229 | /* Return the regno defined by the insn, or -1. */ |
2230 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 2231 | { |
83a28819 IL |
2232 | switch (BPF_CLASS(insn->code)) { |
2233 | case BPF_JMP: | |
2234 | case BPF_JMP32: | |
2235 | case BPF_ST: | |
2236 | return -1; | |
2237 | case BPF_STX: | |
2238 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
2239 | (insn->imm & BPF_FETCH)) { | |
2240 | if (insn->imm == BPF_CMPXCHG) | |
2241 | return BPF_REG_0; | |
2242 | else | |
2243 | return insn->src_reg; | |
2244 | } else { | |
2245 | return -1; | |
2246 | } | |
2247 | default: | |
2248 | return insn->dst_reg; | |
2249 | } | |
b325fbca JW |
2250 | } |
2251 | ||
2252 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
2253 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
2254 | { | |
83a28819 IL |
2255 | int dst_reg = insn_def_regno(insn); |
2256 | ||
2257 | if (dst_reg == -1) | |
b325fbca JW |
2258 | return false; |
2259 | ||
83a28819 | 2260 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
2261 | } |
2262 | ||
5327ed3d JW |
2263 | static void mark_insn_zext(struct bpf_verifier_env *env, |
2264 | struct bpf_reg_state *reg) | |
2265 | { | |
2266 | s32 def_idx = reg->subreg_def; | |
2267 | ||
2268 | if (def_idx == DEF_NOT_SUBREG) | |
2269 | return; | |
2270 | ||
2271 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
2272 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
2273 | reg->subreg_def = DEF_NOT_SUBREG; | |
2274 | } | |
2275 | ||
dc503a8a | 2276 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
2277 | enum reg_arg_type t) |
2278 | { | |
f4d7e40a AS |
2279 | struct bpf_verifier_state *vstate = env->cur_state; |
2280 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 2281 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 2282 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 2283 | bool rw64; |
dc503a8a | 2284 | |
17a52670 | 2285 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 2286 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
2287 | return -EINVAL; |
2288 | } | |
2289 | ||
0f55f9ed CL |
2290 | mark_reg_scratched(env, regno); |
2291 | ||
c342dc10 | 2292 | reg = ®s[regno]; |
5327ed3d | 2293 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
2294 | if (t == SRC_OP) { |
2295 | /* check whether register used as source operand can be read */ | |
c342dc10 | 2296 | if (reg->type == NOT_INIT) { |
61bd5218 | 2297 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
2298 | return -EACCES; |
2299 | } | |
679c782d | 2300 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
2301 | if (regno == BPF_REG_FP) |
2302 | return 0; | |
2303 | ||
5327ed3d JW |
2304 | if (rw64) |
2305 | mark_insn_zext(env, reg); | |
2306 | ||
2307 | return mark_reg_read(env, reg, reg->parent, | |
2308 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
2309 | } else { |
2310 | /* check whether register used as dest operand can be written to */ | |
2311 | if (regno == BPF_REG_FP) { | |
61bd5218 | 2312 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
2313 | return -EACCES; |
2314 | } | |
c342dc10 | 2315 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 2316 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 2317 | if (t == DST_OP) |
61bd5218 | 2318 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
2319 | } |
2320 | return 0; | |
2321 | } | |
2322 | ||
b5dc0163 AS |
2323 | /* for any branch, call, exit record the history of jmps in the given state */ |
2324 | static int push_jmp_history(struct bpf_verifier_env *env, | |
2325 | struct bpf_verifier_state *cur) | |
2326 | { | |
2327 | u32 cnt = cur->jmp_history_cnt; | |
2328 | struct bpf_idx_pair *p; | |
2329 | ||
2330 | cnt++; | |
2331 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
2332 | if (!p) | |
2333 | return -ENOMEM; | |
2334 | p[cnt - 1].idx = env->insn_idx; | |
2335 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
2336 | cur->jmp_history = p; | |
2337 | cur->jmp_history_cnt = cnt; | |
2338 | return 0; | |
2339 | } | |
2340 | ||
2341 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
2342 | * history then previous instruction came from straight line execution. | |
2343 | */ | |
2344 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
2345 | u32 *history) | |
2346 | { | |
2347 | u32 cnt = *history; | |
2348 | ||
2349 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
2350 | i = st->jmp_history[cnt - 1].prev_idx; | |
2351 | (*history)--; | |
2352 | } else { | |
2353 | i--; | |
2354 | } | |
2355 | return i; | |
2356 | } | |
2357 | ||
e6ac2450 MKL |
2358 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
2359 | { | |
2360 | const struct btf_type *func; | |
2357672c | 2361 | struct btf *desc_btf; |
e6ac2450 MKL |
2362 | |
2363 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
2364 | return NULL; | |
2365 | ||
2357672c KKD |
2366 | desc_btf = find_kfunc_desc_btf(data, insn->imm, insn->off, NULL); |
2367 | if (IS_ERR(desc_btf)) | |
2368 | return "<error>"; | |
2369 | ||
2370 | func = btf_type_by_id(desc_btf, insn->imm); | |
2371 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
2372 | } |
2373 | ||
b5dc0163 AS |
2374 | /* For given verifier state backtrack_insn() is called from the last insn to |
2375 | * the first insn. Its purpose is to compute a bitmask of registers and | |
2376 | * stack slots that needs precision in the parent verifier state. | |
2377 | */ | |
2378 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
2379 | u32 *reg_mask, u64 *stack_mask) | |
2380 | { | |
2381 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 2382 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
2383 | .cb_print = verbose, |
2384 | .private_data = env, | |
2385 | }; | |
2386 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
2387 | u8 class = BPF_CLASS(insn->code); | |
2388 | u8 opcode = BPF_OP(insn->code); | |
2389 | u8 mode = BPF_MODE(insn->code); | |
2390 | u32 dreg = 1u << insn->dst_reg; | |
2391 | u32 sreg = 1u << insn->src_reg; | |
2392 | u32 spi; | |
2393 | ||
2394 | if (insn->code == 0) | |
2395 | return 0; | |
496f3324 | 2396 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
2397 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
2398 | verbose(env, "%d: ", idx); | |
2399 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
2400 | } | |
2401 | ||
2402 | if (class == BPF_ALU || class == BPF_ALU64) { | |
2403 | if (!(*reg_mask & dreg)) | |
2404 | return 0; | |
2405 | if (opcode == BPF_MOV) { | |
2406 | if (BPF_SRC(insn->code) == BPF_X) { | |
2407 | /* dreg = sreg | |
2408 | * dreg needs precision after this insn | |
2409 | * sreg needs precision before this insn | |
2410 | */ | |
2411 | *reg_mask &= ~dreg; | |
2412 | *reg_mask |= sreg; | |
2413 | } else { | |
2414 | /* dreg = K | |
2415 | * dreg needs precision after this insn. | |
2416 | * Corresponding register is already marked | |
2417 | * as precise=true in this verifier state. | |
2418 | * No further markings in parent are necessary | |
2419 | */ | |
2420 | *reg_mask &= ~dreg; | |
2421 | } | |
2422 | } else { | |
2423 | if (BPF_SRC(insn->code) == BPF_X) { | |
2424 | /* dreg += sreg | |
2425 | * both dreg and sreg need precision | |
2426 | * before this insn | |
2427 | */ | |
2428 | *reg_mask |= sreg; | |
2429 | } /* else dreg += K | |
2430 | * dreg still needs precision before this insn | |
2431 | */ | |
2432 | } | |
2433 | } else if (class == BPF_LDX) { | |
2434 | if (!(*reg_mask & dreg)) | |
2435 | return 0; | |
2436 | *reg_mask &= ~dreg; | |
2437 | ||
2438 | /* scalars can only be spilled into stack w/o losing precision. | |
2439 | * Load from any other memory can be zero extended. | |
2440 | * The desire to keep that precision is already indicated | |
2441 | * by 'precise' mark in corresponding register of this state. | |
2442 | * No further tracking necessary. | |
2443 | */ | |
2444 | if (insn->src_reg != BPF_REG_FP) | |
2445 | return 0; | |
2446 | if (BPF_SIZE(insn->code) != BPF_DW) | |
2447 | return 0; | |
2448 | ||
2449 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
2450 | * that [fp - off] slot contains scalar that needs to be | |
2451 | * tracked with precision | |
2452 | */ | |
2453 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2454 | if (spi >= 64) { | |
2455 | verbose(env, "BUG spi %d\n", spi); | |
2456 | WARN_ONCE(1, "verifier backtracking bug"); | |
2457 | return -EFAULT; | |
2458 | } | |
2459 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 2460 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 2461 | if (*reg_mask & dreg) |
b3b50f05 | 2462 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
2463 | * to access memory. It means backtracking |
2464 | * encountered a case of pointer subtraction. | |
2465 | */ | |
2466 | return -ENOTSUPP; | |
2467 | /* scalars can only be spilled into stack */ | |
2468 | if (insn->dst_reg != BPF_REG_FP) | |
2469 | return 0; | |
2470 | if (BPF_SIZE(insn->code) != BPF_DW) | |
2471 | return 0; | |
2472 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2473 | if (spi >= 64) { | |
2474 | verbose(env, "BUG spi %d\n", spi); | |
2475 | WARN_ONCE(1, "verifier backtracking bug"); | |
2476 | return -EFAULT; | |
2477 | } | |
2478 | if (!(*stack_mask & (1ull << spi))) | |
2479 | return 0; | |
2480 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
2481 | if (class == BPF_STX) |
2482 | *reg_mask |= sreg; | |
b5dc0163 AS |
2483 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
2484 | if (opcode == BPF_CALL) { | |
2485 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2486 | return -ENOTSUPP; | |
2487 | /* regular helper call sets R0 */ | |
2488 | *reg_mask &= ~1; | |
2489 | if (*reg_mask & 0x3f) { | |
2490 | /* if backtracing was looking for registers R1-R5 | |
2491 | * they should have been found already. | |
2492 | */ | |
2493 | verbose(env, "BUG regs %x\n", *reg_mask); | |
2494 | WARN_ONCE(1, "verifier backtracking bug"); | |
2495 | return -EFAULT; | |
2496 | } | |
2497 | } else if (opcode == BPF_EXIT) { | |
2498 | return -ENOTSUPP; | |
2499 | } | |
2500 | } else if (class == BPF_LD) { | |
2501 | if (!(*reg_mask & dreg)) | |
2502 | return 0; | |
2503 | *reg_mask &= ~dreg; | |
2504 | /* It's ld_imm64 or ld_abs or ld_ind. | |
2505 | * For ld_imm64 no further tracking of precision | |
2506 | * into parent is necessary | |
2507 | */ | |
2508 | if (mode == BPF_IND || mode == BPF_ABS) | |
2509 | /* to be analyzed */ | |
2510 | return -ENOTSUPP; | |
b5dc0163 AS |
2511 | } |
2512 | return 0; | |
2513 | } | |
2514 | ||
2515 | /* the scalar precision tracking algorithm: | |
2516 | * . at the start all registers have precise=false. | |
2517 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
2518 | * . once precise value of the scalar register is used in: | |
2519 | * . ptr + scalar alu | |
2520 | * . if (scalar cond K|scalar) | |
2521 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
2522 | * backtrack through the verifier states and mark all registers and | |
2523 | * stack slots with spilled constants that these scalar regisers | |
2524 | * should be precise. | |
2525 | * . during state pruning two registers (or spilled stack slots) | |
2526 | * are equivalent if both are not precise. | |
2527 | * | |
2528 | * Note the verifier cannot simply walk register parentage chain, | |
2529 | * since many different registers and stack slots could have been | |
2530 | * used to compute single precise scalar. | |
2531 | * | |
2532 | * The approach of starting with precise=true for all registers and then | |
2533 | * backtrack to mark a register as not precise when the verifier detects | |
2534 | * that program doesn't care about specific value (e.g., when helper | |
2535 | * takes register as ARG_ANYTHING parameter) is not safe. | |
2536 | * | |
2537 | * It's ok to walk single parentage chain of the verifier states. | |
2538 | * It's possible that this backtracking will go all the way till 1st insn. | |
2539 | * All other branches will be explored for needing precision later. | |
2540 | * | |
2541 | * The backtracking needs to deal with cases like: | |
2542 | * 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) | |
2543 | * r9 -= r8 | |
2544 | * r5 = r9 | |
2545 | * if r5 > 0x79f goto pc+7 | |
2546 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
2547 | * r5 += 1 | |
2548 | * ... | |
2549 | * call bpf_perf_event_output#25 | |
2550 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
2551 | * | |
2552 | * and this case: | |
2553 | * r6 = 1 | |
2554 | * call foo // uses callee's r6 inside to compute r0 | |
2555 | * r0 += r6 | |
2556 | * if r0 == 0 goto | |
2557 | * | |
2558 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
2559 | * | |
2560 | * Also if parent's curframe > frame where backtracking started, | |
2561 | * the verifier need to mark registers in both frames, otherwise callees | |
2562 | * may incorrectly prune callers. This is similar to | |
2563 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
2564 | * | |
2565 | * For now backtracking falls back into conservative marking. | |
2566 | */ | |
2567 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
2568 | struct bpf_verifier_state *st) | |
2569 | { | |
2570 | struct bpf_func_state *func; | |
2571 | struct bpf_reg_state *reg; | |
2572 | int i, j; | |
2573 | ||
2574 | /* big hammer: mark all scalars precise in this path. | |
2575 | * pop_stack may still get !precise scalars. | |
2576 | */ | |
2577 | for (; st; st = st->parent) | |
2578 | for (i = 0; i <= st->curframe; i++) { | |
2579 | func = st->frame[i]; | |
2580 | for (j = 0; j < BPF_REG_FP; j++) { | |
2581 | reg = &func->regs[j]; | |
2582 | if (reg->type != SCALAR_VALUE) | |
2583 | continue; | |
2584 | reg->precise = true; | |
2585 | } | |
2586 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 2587 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
2588 | continue; |
2589 | reg = &func->stack[j].spilled_ptr; | |
2590 | if (reg->type != SCALAR_VALUE) | |
2591 | continue; | |
2592 | reg->precise = true; | |
2593 | } | |
2594 | } | |
2595 | } | |
2596 | ||
a3ce685d AS |
2597 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
2598 | int spi) | |
b5dc0163 AS |
2599 | { |
2600 | struct bpf_verifier_state *st = env->cur_state; | |
2601 | int first_idx = st->first_insn_idx; | |
2602 | int last_idx = env->insn_idx; | |
2603 | struct bpf_func_state *func; | |
2604 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2605 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2606 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2607 | bool skip_first = true; |
a3ce685d | 2608 | bool new_marks = false; |
b5dc0163 AS |
2609 | int i, err; |
2610 | ||
2c78ee89 | 2611 | if (!env->bpf_capable) |
b5dc0163 AS |
2612 | return 0; |
2613 | ||
2614 | func = st->frame[st->curframe]; | |
a3ce685d AS |
2615 | if (regno >= 0) { |
2616 | reg = &func->regs[regno]; | |
2617 | if (reg->type != SCALAR_VALUE) { | |
2618 | WARN_ONCE(1, "backtracing misuse"); | |
2619 | return -EFAULT; | |
2620 | } | |
2621 | if (!reg->precise) | |
2622 | new_marks = true; | |
2623 | else | |
2624 | reg_mask = 0; | |
2625 | reg->precise = true; | |
b5dc0163 | 2626 | } |
b5dc0163 | 2627 | |
a3ce685d | 2628 | while (spi >= 0) { |
27113c59 | 2629 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
2630 | stack_mask = 0; |
2631 | break; | |
2632 | } | |
2633 | reg = &func->stack[spi].spilled_ptr; | |
2634 | if (reg->type != SCALAR_VALUE) { | |
2635 | stack_mask = 0; | |
2636 | break; | |
2637 | } | |
2638 | if (!reg->precise) | |
2639 | new_marks = true; | |
2640 | else | |
2641 | stack_mask = 0; | |
2642 | reg->precise = true; | |
2643 | break; | |
2644 | } | |
2645 | ||
2646 | if (!new_marks) | |
2647 | return 0; | |
2648 | if (!reg_mask && !stack_mask) | |
2649 | return 0; | |
b5dc0163 AS |
2650 | for (;;) { |
2651 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2652 | u32 history = st->jmp_history_cnt; |
2653 | ||
496f3324 | 2654 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 AS |
2655 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
2656 | for (i = last_idx;;) { | |
2657 | if (skip_first) { | |
2658 | err = 0; | |
2659 | skip_first = false; | |
2660 | } else { | |
2661 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2662 | } | |
2663 | if (err == -ENOTSUPP) { | |
2664 | mark_all_scalars_precise(env, st); | |
2665 | return 0; | |
2666 | } else if (err) { | |
2667 | return err; | |
2668 | } | |
2669 | if (!reg_mask && !stack_mask) | |
2670 | /* Found assignment(s) into tracked register in this state. | |
2671 | * Since this state is already marked, just return. | |
2672 | * Nothing to be tracked further in the parent state. | |
2673 | */ | |
2674 | return 0; | |
2675 | if (i == first_idx) | |
2676 | break; | |
2677 | i = get_prev_insn_idx(st, i, &history); | |
2678 | if (i >= env->prog->len) { | |
2679 | /* This can happen if backtracking reached insn 0 | |
2680 | * and there are still reg_mask or stack_mask | |
2681 | * to backtrack. | |
2682 | * It means the backtracking missed the spot where | |
2683 | * particular register was initialized with a constant. | |
2684 | */ | |
2685 | verbose(env, "BUG backtracking idx %d\n", i); | |
2686 | WARN_ONCE(1, "verifier backtracking bug"); | |
2687 | return -EFAULT; | |
2688 | } | |
2689 | } | |
2690 | st = st->parent; | |
2691 | if (!st) | |
2692 | break; | |
2693 | ||
a3ce685d | 2694 | new_marks = false; |
b5dc0163 AS |
2695 | func = st->frame[st->curframe]; |
2696 | bitmap_from_u64(mask, reg_mask); | |
2697 | for_each_set_bit(i, mask, 32) { | |
2698 | reg = &func->regs[i]; | |
a3ce685d AS |
2699 | if (reg->type != SCALAR_VALUE) { |
2700 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2701 | continue; |
a3ce685d | 2702 | } |
b5dc0163 AS |
2703 | if (!reg->precise) |
2704 | new_marks = true; | |
2705 | reg->precise = true; | |
2706 | } | |
2707 | ||
2708 | bitmap_from_u64(mask, stack_mask); | |
2709 | for_each_set_bit(i, mask, 64) { | |
2710 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2711 | /* the sequence of instructions: |
2712 | * 2: (bf) r3 = r10 | |
2713 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2714 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2715 | * doesn't contain jmps. It's backtracked | |
2716 | * as a single block. | |
2717 | * During backtracking insn 3 is not recognized as | |
2718 | * stack access, so at the end of backtracking | |
2719 | * stack slot fp-8 is still marked in stack_mask. | |
2720 | * However the parent state may not have accessed | |
2721 | * fp-8 and it's "unallocated" stack space. | |
2722 | * In such case fallback to conservative. | |
b5dc0163 | 2723 | */ |
2339cd6c AS |
2724 | mark_all_scalars_precise(env, st); |
2725 | return 0; | |
b5dc0163 AS |
2726 | } |
2727 | ||
27113c59 | 2728 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 2729 | stack_mask &= ~(1ull << i); |
b5dc0163 | 2730 | continue; |
a3ce685d | 2731 | } |
b5dc0163 | 2732 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2733 | if (reg->type != SCALAR_VALUE) { |
2734 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2735 | continue; |
a3ce685d | 2736 | } |
b5dc0163 AS |
2737 | if (!reg->precise) |
2738 | new_marks = true; | |
2739 | reg->precise = true; | |
2740 | } | |
496f3324 | 2741 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 2742 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
2743 | new_marks ? "didn't have" : "already had", |
2744 | reg_mask, stack_mask); | |
2e576648 | 2745 | print_verifier_state(env, func, true); |
b5dc0163 AS |
2746 | } |
2747 | ||
a3ce685d AS |
2748 | if (!reg_mask && !stack_mask) |
2749 | break; | |
b5dc0163 AS |
2750 | if (!new_marks) |
2751 | break; | |
2752 | ||
2753 | last_idx = st->last_insn_idx; | |
2754 | first_idx = st->first_insn_idx; | |
2755 | } | |
2756 | return 0; | |
2757 | } | |
2758 | ||
a3ce685d AS |
2759 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
2760 | { | |
2761 | return __mark_chain_precision(env, regno, -1); | |
2762 | } | |
2763 | ||
2764 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2765 | { | |
2766 | return __mark_chain_precision(env, -1, spi); | |
2767 | } | |
b5dc0163 | 2768 | |
1be7f75d AS |
2769 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2770 | { | |
2771 | switch (type) { | |
2772 | case PTR_TO_MAP_VALUE: | |
2773 | case PTR_TO_MAP_VALUE_OR_NULL: | |
2774 | case PTR_TO_STACK: | |
2775 | case PTR_TO_CTX: | |
969bf05e | 2776 | case PTR_TO_PACKET: |
de8f3a83 | 2777 | case PTR_TO_PACKET_META: |
969bf05e | 2778 | case PTR_TO_PACKET_END: |
d58e468b | 2779 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2780 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
2781 | case PTR_TO_SOCKET: |
2782 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
2783 | case PTR_TO_SOCK_COMMON: |
2784 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
2785 | case PTR_TO_TCP_SOCK: |
2786 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 2787 | case PTR_TO_XDP_SOCK: |
65726b5b | 2788 | case PTR_TO_BTF_ID: |
b121b341 | 2789 | case PTR_TO_BTF_ID_OR_NULL: |
afbf21dc YS |
2790 | case PTR_TO_RDONLY_BUF: |
2791 | case PTR_TO_RDONLY_BUF_OR_NULL: | |
2792 | case PTR_TO_RDWR_BUF: | |
2793 | case PTR_TO_RDWR_BUF_OR_NULL: | |
eaa6bcb7 | 2794 | case PTR_TO_PERCPU_BTF_ID: |
744ea4e3 GR |
2795 | case PTR_TO_MEM: |
2796 | case PTR_TO_MEM_OR_NULL: | |
69c087ba YS |
2797 | case PTR_TO_FUNC: |
2798 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
2799 | return true; |
2800 | default: | |
2801 | return false; | |
2802 | } | |
2803 | } | |
2804 | ||
cc2b14d5 AS |
2805 | /* Does this register contain a constant zero? */ |
2806 | static bool register_is_null(struct bpf_reg_state *reg) | |
2807 | { | |
2808 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2809 | } | |
2810 | ||
f7cf25b2 AS |
2811 | static bool register_is_const(struct bpf_reg_state *reg) |
2812 | { | |
2813 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2814 | } | |
2815 | ||
5689d49b YS |
2816 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
2817 | { | |
2818 | return tnum_is_unknown(reg->var_off) && | |
2819 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
2820 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
2821 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
2822 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
2823 | } | |
2824 | ||
2825 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
2826 | { | |
2827 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
2828 | } | |
2829 | ||
6e7e63cb JH |
2830 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2831 | const struct bpf_reg_state *reg) | |
2832 | { | |
2833 | if (allow_ptr_leaks) | |
2834 | return false; | |
2835 | ||
2836 | return reg->type != SCALAR_VALUE; | |
2837 | } | |
2838 | ||
f7cf25b2 | 2839 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
2840 | int spi, struct bpf_reg_state *reg, |
2841 | int size) | |
f7cf25b2 AS |
2842 | { |
2843 | int i; | |
2844 | ||
2845 | state->stack[spi].spilled_ptr = *reg; | |
354e8f19 MKL |
2846 | if (size == BPF_REG_SIZE) |
2847 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 2848 | |
354e8f19 MKL |
2849 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
2850 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 2851 | |
354e8f19 MKL |
2852 | /* size < 8 bytes spill */ |
2853 | for (; i; i--) | |
2854 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
2855 | } |
2856 | ||
01f810ac | 2857 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
2858 | * stack boundary and alignment are checked in check_mem_access() |
2859 | */ | |
01f810ac AM |
2860 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
2861 | /* stack frame we're writing to */ | |
2862 | struct bpf_func_state *state, | |
2863 | int off, int size, int value_regno, | |
2864 | int insn_idx) | |
17a52670 | 2865 | { |
f4d7e40a | 2866 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 2867 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 2868 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 2869 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 2870 | |
c69431aa | 2871 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
2872 | if (err) |
2873 | return err; | |
9c399760 AS |
2874 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
2875 | * so it's aligned access and [off, off + size) are within stack limits | |
2876 | */ | |
638f5b90 AS |
2877 | if (!env->allow_ptr_leaks && |
2878 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
2879 | size != BPF_REG_SIZE) { | |
2880 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
2881 | return -EACCES; | |
2882 | } | |
17a52670 | 2883 | |
f4d7e40a | 2884 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
2885 | if (value_regno >= 0) |
2886 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
2887 | if (!env->bypass_spec_v4) { |
2888 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
2889 | ||
2890 | for (i = 0; i < size; i++) { | |
2891 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { | |
2892 | sanitize = true; | |
2893 | break; | |
2894 | } | |
2895 | } | |
2896 | ||
2897 | if (sanitize) | |
2898 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
2899 | } | |
17a52670 | 2900 | |
0f55f9ed | 2901 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 2902 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 2903 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
2904 | if (dst_reg != BPF_REG_FP) { |
2905 | /* The backtracking logic can only recognize explicit | |
2906 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 2907 | * scalar via different register has to be conservative. |
b5dc0163 AS |
2908 | * Backtrack from here and mark all registers as precise |
2909 | * that contributed into 'reg' being a constant. | |
2910 | */ | |
2911 | err = mark_chain_precision(env, value_regno); | |
2912 | if (err) | |
2913 | return err; | |
2914 | } | |
354e8f19 | 2915 | save_register_state(state, spi, reg, size); |
f7cf25b2 | 2916 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 2917 | /* register containing pointer is being spilled into stack */ |
9c399760 | 2918 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 2919 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 2920 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
2921 | return -EACCES; |
2922 | } | |
f7cf25b2 | 2923 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2924 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2925 | return -EINVAL; | |
2926 | } | |
354e8f19 | 2927 | save_register_state(state, spi, reg, size); |
9c399760 | 2928 | } else { |
cc2b14d5 AS |
2929 | u8 type = STACK_MISC; |
2930 | ||
679c782d EC |
2931 | /* regular write of data into stack destroys any spilled ptr */ |
2932 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d | 2933 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
27113c59 | 2934 | if (is_spilled_reg(&state->stack[spi])) |
0bae2d4d | 2935 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 2936 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 2937 | |
cc2b14d5 AS |
2938 | /* only mark the slot as written if all 8 bytes were written |
2939 | * otherwise read propagation may incorrectly stop too soon | |
2940 | * when stack slots are partially written. | |
2941 | * This heuristic means that read propagation will be | |
2942 | * conservative, since it will add reg_live_read marks | |
2943 | * to stack slots all the way to first state when programs | |
2944 | * writes+reads less than 8 bytes | |
2945 | */ | |
2946 | if (size == BPF_REG_SIZE) | |
2947 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2948 | ||
2949 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2950 | if (reg && register_is_null(reg)) { |
2951 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2952 | err = mark_chain_precision(env, value_regno); | |
2953 | if (err) | |
2954 | return err; | |
cc2b14d5 | 2955 | type = STACK_ZERO; |
b5dc0163 | 2956 | } |
cc2b14d5 | 2957 | |
0bae2d4d | 2958 | /* Mark slots affected by this stack write. */ |
9c399760 | 2959 | for (i = 0; i < size; i++) |
638f5b90 | 2960 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2961 | type; |
17a52670 AS |
2962 | } |
2963 | return 0; | |
2964 | } | |
2965 | ||
01f810ac AM |
2966 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
2967 | * known to contain a variable offset. | |
2968 | * This function checks whether the write is permitted and conservatively | |
2969 | * tracks the effects of the write, considering that each stack slot in the | |
2970 | * dynamic range is potentially written to. | |
2971 | * | |
2972 | * 'off' includes 'regno->off'. | |
2973 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
2974 | * the stack. | |
2975 | * | |
2976 | * Spilled pointers in range are not marked as written because we don't know | |
2977 | * what's going to be actually written. This means that read propagation for | |
2978 | * future reads cannot be terminated by this write. | |
2979 | * | |
2980 | * For privileged programs, uninitialized stack slots are considered | |
2981 | * initialized by this write (even though we don't know exactly what offsets | |
2982 | * are going to be written to). The idea is that we don't want the verifier to | |
2983 | * reject future reads that access slots written to through variable offsets. | |
2984 | */ | |
2985 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
2986 | /* func where register points to */ | |
2987 | struct bpf_func_state *state, | |
2988 | int ptr_regno, int off, int size, | |
2989 | int value_regno, int insn_idx) | |
2990 | { | |
2991 | struct bpf_func_state *cur; /* state of the current function */ | |
2992 | int min_off, max_off; | |
2993 | int i, err; | |
2994 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
2995 | bool writing_zero = false; | |
2996 | /* set if the fact that we're writing a zero is used to let any | |
2997 | * stack slots remain STACK_ZERO | |
2998 | */ | |
2999 | bool zero_used = false; | |
3000 | ||
3001 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
3002 | ptr_reg = &cur->regs[ptr_regno]; | |
3003 | min_off = ptr_reg->smin_value + off; | |
3004 | max_off = ptr_reg->smax_value + off + size; | |
3005 | if (value_regno >= 0) | |
3006 | value_reg = &cur->regs[value_regno]; | |
3007 | if (value_reg && register_is_null(value_reg)) | |
3008 | writing_zero = true; | |
3009 | ||
c69431aa | 3010 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
3011 | if (err) |
3012 | return err; | |
3013 | ||
3014 | ||
3015 | /* Variable offset writes destroy any spilled pointers in range. */ | |
3016 | for (i = min_off; i < max_off; i++) { | |
3017 | u8 new_type, *stype; | |
3018 | int slot, spi; | |
3019 | ||
3020 | slot = -i - 1; | |
3021 | spi = slot / BPF_REG_SIZE; | |
3022 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 3023 | mark_stack_slot_scratched(env, spi); |
01f810ac AM |
3024 | |
3025 | if (!env->allow_ptr_leaks | |
3026 | && *stype != NOT_INIT | |
3027 | && *stype != SCALAR_VALUE) { | |
3028 | /* Reject the write if there's are spilled pointers in | |
3029 | * range. If we didn't reject here, the ptr status | |
3030 | * would be erased below (even though not all slots are | |
3031 | * actually overwritten), possibly opening the door to | |
3032 | * leaks. | |
3033 | */ | |
3034 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
3035 | insn_idx, i); | |
3036 | return -EINVAL; | |
3037 | } | |
3038 | ||
3039 | /* Erase all spilled pointers. */ | |
3040 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
3041 | ||
3042 | /* Update the slot type. */ | |
3043 | new_type = STACK_MISC; | |
3044 | if (writing_zero && *stype == STACK_ZERO) { | |
3045 | new_type = STACK_ZERO; | |
3046 | zero_used = true; | |
3047 | } | |
3048 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
3049 | * pretend that it will be initialized by this write. The slot | |
3050 | * might not actually be written to, and so if we mark it as | |
3051 | * initialized future reads might leak uninitialized memory. | |
3052 | * For privileged programs, we will accept such reads to slots | |
3053 | * that may or may not be written because, if we're reject | |
3054 | * them, the error would be too confusing. | |
3055 | */ | |
3056 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
3057 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
3058 | insn_idx, i); | |
3059 | return -EINVAL; | |
3060 | } | |
3061 | *stype = new_type; | |
3062 | } | |
3063 | if (zero_used) { | |
3064 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3065 | err = mark_chain_precision(env, value_regno); | |
3066 | if (err) | |
3067 | return err; | |
3068 | } | |
3069 | return 0; | |
3070 | } | |
3071 | ||
3072 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
3073 | * max_off), we set the register's type according to the types of the | |
3074 | * respective stack slots. If all the stack values are known to be zeros, then | |
3075 | * so is the destination reg. Otherwise, the register is considered to be | |
3076 | * SCALAR. This function does not deal with register filling; the caller must | |
3077 | * ensure that all spilled registers in the stack range have been marked as | |
3078 | * read. | |
3079 | */ | |
3080 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
3081 | /* func where src register points to */ | |
3082 | struct bpf_func_state *ptr_state, | |
3083 | int min_off, int max_off, int dst_regno) | |
3084 | { | |
3085 | struct bpf_verifier_state *vstate = env->cur_state; | |
3086 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3087 | int i, slot, spi; | |
3088 | u8 *stype; | |
3089 | int zeros = 0; | |
3090 | ||
3091 | for (i = min_off; i < max_off; i++) { | |
3092 | slot = -i - 1; | |
3093 | spi = slot / BPF_REG_SIZE; | |
3094 | stype = ptr_state->stack[spi].slot_type; | |
3095 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
3096 | break; | |
3097 | zeros++; | |
3098 | } | |
3099 | if (zeros == max_off - min_off) { | |
3100 | /* any access_size read into register is zero extended, | |
3101 | * so the whole register == const_zero | |
3102 | */ | |
3103 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
3104 | /* backtracking doesn't support STACK_ZERO yet, | |
3105 | * so mark it precise here, so that later | |
3106 | * backtracking can stop here. | |
3107 | * Backtracking may not need this if this register | |
3108 | * doesn't participate in pointer adjustment. | |
3109 | * Forward propagation of precise flag is not | |
3110 | * necessary either. This mark is only to stop | |
3111 | * backtracking. Any register that contributed | |
3112 | * to const 0 was marked precise before spill. | |
3113 | */ | |
3114 | state->regs[dst_regno].precise = true; | |
3115 | } else { | |
3116 | /* have read misc data from the stack */ | |
3117 | mark_reg_unknown(env, state->regs, dst_regno); | |
3118 | } | |
3119 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
3120 | } | |
3121 | ||
3122 | /* Read the stack at 'off' and put the results into the register indicated by | |
3123 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
3124 | * spilled reg. | |
3125 | * | |
3126 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
3127 | * register. | |
3128 | * | |
3129 | * The access is assumed to be within the current stack bounds. | |
3130 | */ | |
3131 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
3132 | /* func where src register points to */ | |
3133 | struct bpf_func_state *reg_state, | |
3134 | int off, int size, int dst_regno) | |
17a52670 | 3135 | { |
f4d7e40a AS |
3136 | struct bpf_verifier_state *vstate = env->cur_state; |
3137 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 3138 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 3139 | struct bpf_reg_state *reg; |
354e8f19 | 3140 | u8 *stype, type; |
17a52670 | 3141 | |
f4d7e40a | 3142 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 3143 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 3144 | |
27113c59 | 3145 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
3146 | u8 spill_size = 1; |
3147 | ||
3148 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
3149 | spill_size++; | |
354e8f19 | 3150 | |
f30d4968 | 3151 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
3152 | if (reg->type != SCALAR_VALUE) { |
3153 | verbose_linfo(env, env->insn_idx, "; "); | |
3154 | verbose(env, "invalid size of register fill\n"); | |
3155 | return -EACCES; | |
3156 | } | |
354e8f19 MKL |
3157 | |
3158 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
3159 | if (dst_regno < 0) | |
3160 | return 0; | |
3161 | ||
f30d4968 | 3162 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
3163 | /* The earlier check_reg_arg() has decided the |
3164 | * subreg_def for this insn. Save it first. | |
3165 | */ | |
3166 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
3167 | ||
3168 | state->regs[dst_regno] = *reg; | |
3169 | state->regs[dst_regno].subreg_def = subreg_def; | |
3170 | } else { | |
3171 | for (i = 0; i < size; i++) { | |
3172 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
3173 | if (type == STACK_SPILL) | |
3174 | continue; | |
3175 | if (type == STACK_MISC) | |
3176 | continue; | |
3177 | verbose(env, "invalid read from stack off %d+%d size %d\n", | |
3178 | off, i, size); | |
3179 | return -EACCES; | |
3180 | } | |
01f810ac | 3181 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 3182 | } |
354e8f19 | 3183 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 3184 | return 0; |
17a52670 | 3185 | } |
17a52670 | 3186 | |
01f810ac | 3187 | if (dst_regno >= 0) { |
17a52670 | 3188 | /* restore register state from stack */ |
01f810ac | 3189 | state->regs[dst_regno] = *reg; |
2f18f62e AS |
3190 | /* mark reg as written since spilled pointer state likely |
3191 | * has its liveness marks cleared by is_state_visited() | |
3192 | * which resets stack/reg liveness for state transitions | |
3193 | */ | |
01f810ac | 3194 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 3195 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 3196 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
3197 | * it is acceptable to use this value as a SCALAR_VALUE |
3198 | * (e.g. for XADD). | |
3199 | * We must not allow unprivileged callers to do that | |
3200 | * with spilled pointers. | |
3201 | */ | |
3202 | verbose(env, "leaking pointer from stack off %d\n", | |
3203 | off); | |
3204 | return -EACCES; | |
dc503a8a | 3205 | } |
f7cf25b2 | 3206 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
3207 | } else { |
3208 | for (i = 0; i < size; i++) { | |
01f810ac AM |
3209 | type = stype[(slot - i) % BPF_REG_SIZE]; |
3210 | if (type == STACK_MISC) | |
cc2b14d5 | 3211 | continue; |
01f810ac | 3212 | if (type == STACK_ZERO) |
cc2b14d5 | 3213 | continue; |
cc2b14d5 AS |
3214 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
3215 | off, i, size); | |
3216 | return -EACCES; | |
3217 | } | |
f7cf25b2 | 3218 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
3219 | if (dst_regno >= 0) |
3220 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 3221 | } |
f7cf25b2 | 3222 | return 0; |
17a52670 AS |
3223 | } |
3224 | ||
01f810ac AM |
3225 | enum stack_access_src { |
3226 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ | |
3227 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
3228 | }; | |
3229 | ||
3230 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
3231 | int regno, int off, int access_size, | |
3232 | bool zero_size_allowed, | |
3233 | enum stack_access_src type, | |
3234 | struct bpf_call_arg_meta *meta); | |
3235 | ||
3236 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
3237 | { | |
3238 | return cur_regs(env) + regno; | |
3239 | } | |
3240 | ||
3241 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
3242 | * 'dst_regno'. | |
3243 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
3244 | * but not its variable offset. | |
3245 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
3246 | * | |
3247 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
3248 | * filling registers (i.e. reads of spilled register cannot be detected when | |
3249 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
3250 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
3251 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
3252 | * instead. | |
3253 | */ | |
3254 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
3255 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 3256 | { |
01f810ac AM |
3257 | /* The state of the source register. */ |
3258 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3259 | struct bpf_func_state *ptr_state = func(env, reg); | |
3260 | int err; | |
3261 | int min_off, max_off; | |
3262 | ||
3263 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 3264 | */ |
01f810ac AM |
3265 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
3266 | false, ACCESS_DIRECT, NULL); | |
3267 | if (err) | |
3268 | return err; | |
3269 | ||
3270 | min_off = reg->smin_value + off; | |
3271 | max_off = reg->smax_value + off; | |
3272 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
3273 | return 0; | |
3274 | } | |
3275 | ||
3276 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
3277 | * check_stack_read_var_off. | |
3278 | * | |
3279 | * The caller must ensure that the offset falls within the allocated stack | |
3280 | * bounds. | |
3281 | * | |
3282 | * 'dst_regno' is a register which will receive the value from the stack. It | |
3283 | * can be -1, meaning that the read value is not going to a register. | |
3284 | */ | |
3285 | static int check_stack_read(struct bpf_verifier_env *env, | |
3286 | int ptr_regno, int off, int size, | |
3287 | int dst_regno) | |
3288 | { | |
3289 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3290 | struct bpf_func_state *state = func(env, reg); | |
3291 | int err; | |
3292 | /* Some accesses are only permitted with a static offset. */ | |
3293 | bool var_off = !tnum_is_const(reg->var_off); | |
3294 | ||
3295 | /* The offset is required to be static when reads don't go to a | |
3296 | * register, in order to not leak pointers (see | |
3297 | * check_stack_read_fixed_off). | |
3298 | */ | |
3299 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
3300 | char tn_buf[48]; |
3301 | ||
3302 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 3303 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
3304 | tn_buf, off, size); |
3305 | return -EACCES; | |
3306 | } | |
01f810ac AM |
3307 | /* Variable offset is prohibited for unprivileged mode for simplicity |
3308 | * since it requires corresponding support in Spectre masking for stack | |
3309 | * ALU. See also retrieve_ptr_limit(). | |
3310 | */ | |
3311 | if (!env->bypass_spec_v1 && var_off) { | |
3312 | char tn_buf[48]; | |
e4298d25 | 3313 | |
01f810ac AM |
3314 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3315 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", | |
3316 | ptr_regno, tn_buf); | |
e4298d25 DB |
3317 | return -EACCES; |
3318 | } | |
3319 | ||
01f810ac AM |
3320 | if (!var_off) { |
3321 | off += reg->var_off.value; | |
3322 | err = check_stack_read_fixed_off(env, state, off, size, | |
3323 | dst_regno); | |
3324 | } else { | |
3325 | /* Variable offset stack reads need more conservative handling | |
3326 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
3327 | * branch. | |
3328 | */ | |
3329 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
3330 | dst_regno); | |
3331 | } | |
3332 | return err; | |
3333 | } | |
3334 | ||
3335 | ||
3336 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
3337 | * check_stack_write_var_off. | |
3338 | * | |
3339 | * 'ptr_regno' is the register used as a pointer into the stack. | |
3340 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
3341 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
3342 | * be -1, meaning that we're not writing from a register. | |
3343 | * | |
3344 | * The caller must ensure that the offset falls within the maximum stack size. | |
3345 | */ | |
3346 | static int check_stack_write(struct bpf_verifier_env *env, | |
3347 | int ptr_regno, int off, int size, | |
3348 | int value_regno, int insn_idx) | |
3349 | { | |
3350 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3351 | struct bpf_func_state *state = func(env, reg); | |
3352 | int err; | |
3353 | ||
3354 | if (tnum_is_const(reg->var_off)) { | |
3355 | off += reg->var_off.value; | |
3356 | err = check_stack_write_fixed_off(env, state, off, size, | |
3357 | value_regno, insn_idx); | |
3358 | } else { | |
3359 | /* Variable offset stack reads need more conservative handling | |
3360 | * than fixed offset ones. | |
3361 | */ | |
3362 | err = check_stack_write_var_off(env, state, | |
3363 | ptr_regno, off, size, | |
3364 | value_regno, insn_idx); | |
3365 | } | |
3366 | return err; | |
e4298d25 DB |
3367 | } |
3368 | ||
591fe988 DB |
3369 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
3370 | int off, int size, enum bpf_access_type type) | |
3371 | { | |
3372 | struct bpf_reg_state *regs = cur_regs(env); | |
3373 | struct bpf_map *map = regs[regno].map_ptr; | |
3374 | u32 cap = bpf_map_flags_to_cap(map); | |
3375 | ||
3376 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
3377 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
3378 | map->value_size, off, size); | |
3379 | return -EACCES; | |
3380 | } | |
3381 | ||
3382 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
3383 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
3384 | map->value_size, off, size); | |
3385 | return -EACCES; | |
3386 | } | |
3387 | ||
3388 | return 0; | |
3389 | } | |
3390 | ||
457f4436 AN |
3391 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
3392 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
3393 | int off, int size, u32 mem_size, | |
3394 | bool zero_size_allowed) | |
17a52670 | 3395 | { |
457f4436 AN |
3396 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
3397 | struct bpf_reg_state *reg; | |
3398 | ||
3399 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
3400 | return 0; | |
17a52670 | 3401 | |
457f4436 AN |
3402 | reg = &cur_regs(env)[regno]; |
3403 | switch (reg->type) { | |
69c087ba YS |
3404 | case PTR_TO_MAP_KEY: |
3405 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
3406 | mem_size, off, size); | |
3407 | break; | |
457f4436 | 3408 | case PTR_TO_MAP_VALUE: |
61bd5218 | 3409 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
3410 | mem_size, off, size); |
3411 | break; | |
3412 | case PTR_TO_PACKET: | |
3413 | case PTR_TO_PACKET_META: | |
3414 | case PTR_TO_PACKET_END: | |
3415 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
3416 | off, size, regno, reg->id, off, mem_size); | |
3417 | break; | |
3418 | case PTR_TO_MEM: | |
3419 | default: | |
3420 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
3421 | mem_size, off, size); | |
17a52670 | 3422 | } |
457f4436 AN |
3423 | |
3424 | return -EACCES; | |
17a52670 AS |
3425 | } |
3426 | ||
457f4436 AN |
3427 | /* check read/write into a memory region with possible variable offset */ |
3428 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
3429 | int off, int size, u32 mem_size, | |
3430 | bool zero_size_allowed) | |
dbcfe5f7 | 3431 | { |
f4d7e40a AS |
3432 | struct bpf_verifier_state *vstate = env->cur_state; |
3433 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
3434 | struct bpf_reg_state *reg = &state->regs[regno]; |
3435 | int err; | |
3436 | ||
457f4436 | 3437 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
3438 | * need to try adding each of min_value and max_value to off |
3439 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
3440 | * |
3441 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
3442 | * comparisons where we can't assume the floor of a |
3443 | * value is 0. If we are using signed variables for our | |
3444 | * index'es we need to make sure that whatever we use | |
3445 | * will have a set floor within our range. | |
3446 | */ | |
b7137c4e DB |
3447 | if (reg->smin_value < 0 && |
3448 | (reg->smin_value == S64_MIN || | |
3449 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
3450 | reg->smin_value + off < 0)) { | |
61bd5218 | 3451 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
3452 | regno); |
3453 | return -EACCES; | |
3454 | } | |
457f4436 AN |
3455 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
3456 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 3457 | if (err) { |
457f4436 | 3458 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 3459 | regno); |
dbcfe5f7 GB |
3460 | return err; |
3461 | } | |
3462 | ||
b03c9f9f EC |
3463 | /* If we haven't set a max value then we need to bail since we can't be |
3464 | * sure we won't do bad things. | |
3465 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 3466 | */ |
b03c9f9f | 3467 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 3468 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
3469 | regno); |
3470 | return -EACCES; | |
3471 | } | |
457f4436 AN |
3472 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
3473 | mem_size, zero_size_allowed); | |
3474 | if (err) { | |
3475 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 3476 | regno); |
457f4436 AN |
3477 | return err; |
3478 | } | |
3479 | ||
3480 | return 0; | |
3481 | } | |
d83525ca | 3482 | |
457f4436 AN |
3483 | /* check read/write into a map element with possible variable offset */ |
3484 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
3485 | int off, int size, bool zero_size_allowed) | |
3486 | { | |
3487 | struct bpf_verifier_state *vstate = env->cur_state; | |
3488 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3489 | struct bpf_reg_state *reg = &state->regs[regno]; | |
3490 | struct bpf_map *map = reg->map_ptr; | |
3491 | int err; | |
3492 | ||
3493 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
3494 | zero_size_allowed); | |
3495 | if (err) | |
3496 | return err; | |
3497 | ||
3498 | if (map_value_has_spin_lock(map)) { | |
3499 | u32 lock = map->spin_lock_off; | |
d83525ca AS |
3500 | |
3501 | /* if any part of struct bpf_spin_lock can be touched by | |
3502 | * load/store reject this program. | |
3503 | * To check that [x1, x2) overlaps with [y1, y2) | |
3504 | * it is sufficient to check x1 < y2 && y1 < x2. | |
3505 | */ | |
3506 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
3507 | lock < reg->umax_value + off + size) { | |
3508 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
3509 | return -EACCES; | |
3510 | } | |
3511 | } | |
68134668 AS |
3512 | if (map_value_has_timer(map)) { |
3513 | u32 t = map->timer_off; | |
3514 | ||
3515 | if (reg->smin_value + off < t + sizeof(struct bpf_timer) && | |
3516 | t < reg->umax_value + off + size) { | |
3517 | verbose(env, "bpf_timer cannot be accessed directly by load/store\n"); | |
3518 | return -EACCES; | |
3519 | } | |
3520 | } | |
f1174f77 | 3521 | return err; |
dbcfe5f7 GB |
3522 | } |
3523 | ||
969bf05e AS |
3524 | #define MAX_PACKET_OFF 0xffff |
3525 | ||
7e40781c UP |
3526 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
3527 | { | |
3aac1ead | 3528 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
7e40781c UP |
3529 | } |
3530 | ||
58e2af8b | 3531 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
3532 | const struct bpf_call_arg_meta *meta, |
3533 | enum bpf_access_type t) | |
4acf6c0b | 3534 | { |
7e40781c UP |
3535 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
3536 | ||
3537 | switch (prog_type) { | |
5d66fa7d | 3538 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
3539 | case BPF_PROG_TYPE_LWT_IN: |
3540 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 3541 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 3542 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 3543 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 3544 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
3545 | if (t == BPF_WRITE) |
3546 | return false; | |
8731745e | 3547 | fallthrough; |
5d66fa7d DB |
3548 | |
3549 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
3550 | case BPF_PROG_TYPE_SCHED_CLS: |
3551 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 3552 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 3553 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 3554 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 3555 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
3556 | if (meta) |
3557 | return meta->pkt_access; | |
3558 | ||
3559 | env->seen_direct_write = true; | |
4acf6c0b | 3560 | return true; |
0d01da6a SF |
3561 | |
3562 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
3563 | if (t == BPF_WRITE) | |
3564 | env->seen_direct_write = true; | |
3565 | ||
3566 | return true; | |
3567 | ||
4acf6c0b BB |
3568 | default: |
3569 | return false; | |
3570 | } | |
3571 | } | |
3572 | ||
f1174f77 | 3573 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 3574 | int size, bool zero_size_allowed) |
f1174f77 | 3575 | { |
638f5b90 | 3576 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
3577 | struct bpf_reg_state *reg = ®s[regno]; |
3578 | int err; | |
3579 | ||
3580 | /* We may have added a variable offset to the packet pointer; but any | |
3581 | * reg->range we have comes after that. We are only checking the fixed | |
3582 | * offset. | |
3583 | */ | |
3584 | ||
3585 | /* We don't allow negative numbers, because we aren't tracking enough | |
3586 | * detail to prove they're safe. | |
3587 | */ | |
b03c9f9f | 3588 | if (reg->smin_value < 0) { |
61bd5218 | 3589 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
3590 | regno); |
3591 | return -EACCES; | |
3592 | } | |
6d94e741 AS |
3593 | |
3594 | err = reg->range < 0 ? -EINVAL : | |
3595 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 3596 | zero_size_allowed); |
f1174f77 | 3597 | if (err) { |
61bd5218 | 3598 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
3599 | return err; |
3600 | } | |
e647815a | 3601 | |
457f4436 | 3602 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
3603 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
3604 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 3605 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
3606 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
3607 | */ | |
3608 | env->prog->aux->max_pkt_offset = | |
3609 | max_t(u32, env->prog->aux->max_pkt_offset, | |
3610 | off + reg->umax_value + size - 1); | |
3611 | ||
f1174f77 EC |
3612 | return err; |
3613 | } | |
3614 | ||
3615 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 3616 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 3617 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 3618 | struct btf **btf, u32 *btf_id) |
17a52670 | 3619 | { |
f96da094 DB |
3620 | struct bpf_insn_access_aux info = { |
3621 | .reg_type = *reg_type, | |
9e15db66 | 3622 | .log = &env->log, |
f96da094 | 3623 | }; |
31fd8581 | 3624 | |
4f9218aa | 3625 | if (env->ops->is_valid_access && |
5e43f899 | 3626 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
3627 | /* A non zero info.ctx_field_size indicates that this field is a |
3628 | * candidate for later verifier transformation to load the whole | |
3629 | * field and then apply a mask when accessed with a narrower | |
3630 | * access than actual ctx access size. A zero info.ctx_field_size | |
3631 | * will only allow for whole field access and rejects any other | |
3632 | * type of narrower access. | |
31fd8581 | 3633 | */ |
23994631 | 3634 | *reg_type = info.reg_type; |
31fd8581 | 3635 | |
22dc4a0f AN |
3636 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) { |
3637 | *btf = info.btf; | |
9e15db66 | 3638 | *btf_id = info.btf_id; |
22dc4a0f | 3639 | } else { |
9e15db66 | 3640 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 3641 | } |
32bbe007 AS |
3642 | /* remember the offset of last byte accessed in ctx */ |
3643 | if (env->prog->aux->max_ctx_offset < off + size) | |
3644 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 3645 | return 0; |
32bbe007 | 3646 | } |
17a52670 | 3647 | |
61bd5218 | 3648 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
3649 | return -EACCES; |
3650 | } | |
3651 | ||
d58e468b PP |
3652 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
3653 | int size) | |
3654 | { | |
3655 | if (size < 0 || off < 0 || | |
3656 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
3657 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
3658 | off, size); | |
3659 | return -EACCES; | |
3660 | } | |
3661 | return 0; | |
3662 | } | |
3663 | ||
5f456649 MKL |
3664 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
3665 | u32 regno, int off, int size, | |
3666 | enum bpf_access_type t) | |
c64b7983 JS |
3667 | { |
3668 | struct bpf_reg_state *regs = cur_regs(env); | |
3669 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 3670 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 3671 | bool valid; |
c64b7983 JS |
3672 | |
3673 | if (reg->smin_value < 0) { | |
3674 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
3675 | regno); | |
3676 | return -EACCES; | |
3677 | } | |
3678 | ||
46f8bc92 MKL |
3679 | switch (reg->type) { |
3680 | case PTR_TO_SOCK_COMMON: | |
3681 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
3682 | break; | |
3683 | case PTR_TO_SOCKET: | |
3684 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
3685 | break; | |
655a51e5 MKL |
3686 | case PTR_TO_TCP_SOCK: |
3687 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
3688 | break; | |
fada7fdc JL |
3689 | case PTR_TO_XDP_SOCK: |
3690 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
3691 | break; | |
46f8bc92 MKL |
3692 | default: |
3693 | valid = false; | |
c64b7983 JS |
3694 | } |
3695 | ||
5f456649 | 3696 | |
46f8bc92 MKL |
3697 | if (valid) { |
3698 | env->insn_aux_data[insn_idx].ctx_field_size = | |
3699 | info.ctx_field_size; | |
3700 | return 0; | |
3701 | } | |
3702 | ||
3703 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
3704 | regno, reg_type_str[reg->type], off, size); | |
3705 | ||
3706 | return -EACCES; | |
c64b7983 JS |
3707 | } |
3708 | ||
4cabc5b1 DB |
3709 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
3710 | { | |
2a159c6f | 3711 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
3712 | } |
3713 | ||
f37a8cb8 DB |
3714 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
3715 | { | |
2a159c6f | 3716 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 3717 | |
46f8bc92 MKL |
3718 | return reg->type == PTR_TO_CTX; |
3719 | } | |
3720 | ||
3721 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
3722 | { | |
3723 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
3724 | ||
3725 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
3726 | } |
3727 | ||
ca369602 DB |
3728 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
3729 | { | |
2a159c6f | 3730 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
3731 | |
3732 | return type_is_pkt_pointer(reg->type); | |
3733 | } | |
3734 | ||
4b5defde DB |
3735 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
3736 | { | |
3737 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
3738 | ||
3739 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
3740 | return reg->type == PTR_TO_FLOW_KEYS; | |
3741 | } | |
3742 | ||
61bd5218 JK |
3743 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
3744 | const struct bpf_reg_state *reg, | |
d1174416 | 3745 | int off, int size, bool strict) |
969bf05e | 3746 | { |
f1174f77 | 3747 | struct tnum reg_off; |
e07b98d9 | 3748 | int ip_align; |
d1174416 DM |
3749 | |
3750 | /* Byte size accesses are always allowed. */ | |
3751 | if (!strict || size == 1) | |
3752 | return 0; | |
3753 | ||
e4eda884 DM |
3754 | /* For platforms that do not have a Kconfig enabling |
3755 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
3756 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
3757 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
3758 | * to this code only in strict mode where we want to emulate | |
3759 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
3760 | * unconditional IP align value of '2'. | |
e07b98d9 | 3761 | */ |
e4eda884 | 3762 | ip_align = 2; |
f1174f77 EC |
3763 | |
3764 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
3765 | if (!tnum_is_aligned(reg_off, size)) { | |
3766 | char tn_buf[48]; | |
3767 | ||
3768 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
3769 | verbose(env, |
3770 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 3771 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
3772 | return -EACCES; |
3773 | } | |
79adffcd | 3774 | |
969bf05e AS |
3775 | return 0; |
3776 | } | |
3777 | ||
61bd5218 JK |
3778 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
3779 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
3780 | const char *pointer_desc, |
3781 | int off, int size, bool strict) | |
79adffcd | 3782 | { |
f1174f77 EC |
3783 | struct tnum reg_off; |
3784 | ||
3785 | /* Byte size accesses are always allowed. */ | |
3786 | if (!strict || size == 1) | |
3787 | return 0; | |
3788 | ||
3789 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
3790 | if (!tnum_is_aligned(reg_off, size)) { | |
3791 | char tn_buf[48]; | |
3792 | ||
3793 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 3794 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 3795 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
3796 | return -EACCES; |
3797 | } | |
3798 | ||
969bf05e AS |
3799 | return 0; |
3800 | } | |
3801 | ||
e07b98d9 | 3802 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
3803 | const struct bpf_reg_state *reg, int off, |
3804 | int size, bool strict_alignment_once) | |
79adffcd | 3805 | { |
ca369602 | 3806 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 3807 | const char *pointer_desc = ""; |
d1174416 | 3808 | |
79adffcd DB |
3809 | switch (reg->type) { |
3810 | case PTR_TO_PACKET: | |
de8f3a83 DB |
3811 | case PTR_TO_PACKET_META: |
3812 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
3813 | * right in front, treat it the very same way. | |
3814 | */ | |
61bd5218 | 3815 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
3816 | case PTR_TO_FLOW_KEYS: |
3817 | pointer_desc = "flow keys "; | |
3818 | break; | |
69c087ba YS |
3819 | case PTR_TO_MAP_KEY: |
3820 | pointer_desc = "key "; | |
3821 | break; | |
f1174f77 EC |
3822 | case PTR_TO_MAP_VALUE: |
3823 | pointer_desc = "value "; | |
3824 | break; | |
3825 | case PTR_TO_CTX: | |
3826 | pointer_desc = "context "; | |
3827 | break; | |
3828 | case PTR_TO_STACK: | |
3829 | pointer_desc = "stack "; | |
01f810ac AM |
3830 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
3831 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
3832 | * aligned. |
3833 | */ | |
3834 | strict = true; | |
f1174f77 | 3835 | break; |
c64b7983 JS |
3836 | case PTR_TO_SOCKET: |
3837 | pointer_desc = "sock "; | |
3838 | break; | |
46f8bc92 MKL |
3839 | case PTR_TO_SOCK_COMMON: |
3840 | pointer_desc = "sock_common "; | |
3841 | break; | |
655a51e5 MKL |
3842 | case PTR_TO_TCP_SOCK: |
3843 | pointer_desc = "tcp_sock "; | |
3844 | break; | |
fada7fdc JL |
3845 | case PTR_TO_XDP_SOCK: |
3846 | pointer_desc = "xdp_sock "; | |
3847 | break; | |
79adffcd | 3848 | default: |
f1174f77 | 3849 | break; |
79adffcd | 3850 | } |
61bd5218 JK |
3851 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
3852 | strict); | |
79adffcd DB |
3853 | } |
3854 | ||
f4d7e40a AS |
3855 | static int update_stack_depth(struct bpf_verifier_env *env, |
3856 | const struct bpf_func_state *func, | |
3857 | int off) | |
3858 | { | |
9c8105bd | 3859 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
3860 | |
3861 | if (stack >= -off) | |
3862 | return 0; | |
3863 | ||
3864 | /* update known max for given subprogram */ | |
9c8105bd | 3865 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
3866 | return 0; |
3867 | } | |
f4d7e40a | 3868 | |
70a87ffe AS |
3869 | /* starting from main bpf function walk all instructions of the function |
3870 | * and recursively walk all callees that given function can call. | |
3871 | * Ignore jump and exit insns. | |
3872 | * Since recursion is prevented by check_cfg() this algorithm | |
3873 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
3874 | */ | |
3875 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
3876 | { | |
9c8105bd JW |
3877 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
3878 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 3879 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 3880 | bool tail_call_reachable = false; |
70a87ffe AS |
3881 | int ret_insn[MAX_CALL_FRAMES]; |
3882 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 3883 | int j; |
f4d7e40a | 3884 | |
70a87ffe | 3885 | process_func: |
7f6e4312 MF |
3886 | /* protect against potential stack overflow that might happen when |
3887 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
3888 | * depth for such case down to 256 so that the worst case scenario | |
3889 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
3890 | * 8k). | |
3891 | * | |
3892 | * To get the idea what might happen, see an example: | |
3893 | * func1 -> sub rsp, 128 | |
3894 | * subfunc1 -> sub rsp, 256 | |
3895 | * tailcall1 -> add rsp, 256 | |
3896 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
3897 | * subfunc2 -> sub rsp, 64 | |
3898 | * subfunc22 -> sub rsp, 128 | |
3899 | * tailcall2 -> add rsp, 128 | |
3900 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
3901 | * | |
3902 | * tailcall will unwind the current stack frame but it will not get rid | |
3903 | * of caller's stack as shown on the example above. | |
3904 | */ | |
3905 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
3906 | verbose(env, | |
3907 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
3908 | depth); | |
3909 | return -EACCES; | |
3910 | } | |
70a87ffe AS |
3911 | /* round up to 32-bytes, since this is granularity |
3912 | * of interpreter stack size | |
3913 | */ | |
9c8105bd | 3914 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 3915 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 3916 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 3917 | frame + 1, depth); |
f4d7e40a AS |
3918 | return -EACCES; |
3919 | } | |
70a87ffe | 3920 | continue_func: |
4cb3d99c | 3921 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 3922 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
3923 | int next_insn; |
3924 | ||
69c087ba | 3925 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
3926 | continue; |
3927 | /* remember insn and function to return to */ | |
3928 | ret_insn[frame] = i + 1; | |
9c8105bd | 3929 | ret_prog[frame] = idx; |
70a87ffe AS |
3930 | |
3931 | /* find the callee */ | |
7ddc80a4 AS |
3932 | next_insn = i + insn[i].imm + 1; |
3933 | idx = find_subprog(env, next_insn); | |
9c8105bd | 3934 | if (idx < 0) { |
70a87ffe | 3935 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 3936 | next_insn); |
70a87ffe AS |
3937 | return -EFAULT; |
3938 | } | |
7ddc80a4 AS |
3939 | if (subprog[idx].is_async_cb) { |
3940 | if (subprog[idx].has_tail_call) { | |
3941 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
3942 | return -EFAULT; | |
3943 | } | |
3944 | /* async callbacks don't increase bpf prog stack size */ | |
3945 | continue; | |
3946 | } | |
3947 | i = next_insn; | |
ebf7d1f5 MF |
3948 | |
3949 | if (subprog[idx].has_tail_call) | |
3950 | tail_call_reachable = true; | |
3951 | ||
70a87ffe AS |
3952 | frame++; |
3953 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
3954 | verbose(env, "the call stack of %d frames is too deep !\n", |
3955 | frame); | |
3956 | return -E2BIG; | |
70a87ffe AS |
3957 | } |
3958 | goto process_func; | |
3959 | } | |
ebf7d1f5 MF |
3960 | /* if tail call got detected across bpf2bpf calls then mark each of the |
3961 | * currently present subprog frames as tail call reachable subprogs; | |
3962 | * this info will be utilized by JIT so that we will be preserving the | |
3963 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
3964 | */ | |
3965 | if (tail_call_reachable) | |
3966 | for (j = 0; j < frame; j++) | |
3967 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
3968 | if (subprog[0].tail_call_reachable) |
3969 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 3970 | |
70a87ffe AS |
3971 | /* end of for() loop means the last insn of the 'subprog' |
3972 | * was reached. Doesn't matter whether it was JA or EXIT | |
3973 | */ | |
3974 | if (frame == 0) | |
3975 | return 0; | |
9c8105bd | 3976 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
3977 | frame--; |
3978 | i = ret_insn[frame]; | |
9c8105bd | 3979 | idx = ret_prog[frame]; |
70a87ffe | 3980 | goto continue_func; |
f4d7e40a AS |
3981 | } |
3982 | ||
19d28fbd | 3983 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
3984 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
3985 | const struct bpf_insn *insn, int idx) | |
3986 | { | |
3987 | int start = idx + insn->imm + 1, subprog; | |
3988 | ||
3989 | subprog = find_subprog(env, start); | |
3990 | if (subprog < 0) { | |
3991 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
3992 | start); | |
3993 | return -EFAULT; | |
3994 | } | |
9c8105bd | 3995 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 3996 | } |
19d28fbd | 3997 | #endif |
1ea47e01 | 3998 | |
51c39bb1 AS |
3999 | int check_ctx_reg(struct bpf_verifier_env *env, |
4000 | const struct bpf_reg_state *reg, int regno) | |
58990d1f DB |
4001 | { |
4002 | /* Access to ctx or passing it to a helper is only allowed in | |
4003 | * its original, unmodified form. | |
4004 | */ | |
4005 | ||
4006 | if (reg->off) { | |
4007 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
4008 | regno, reg->off); | |
4009 | return -EACCES; | |
4010 | } | |
4011 | ||
4012 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4013 | char tn_buf[48]; | |
4014 | ||
4015 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4016 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
4017 | return -EACCES; | |
4018 | } | |
4019 | ||
4020 | return 0; | |
4021 | } | |
4022 | ||
afbf21dc YS |
4023 | static int __check_buffer_access(struct bpf_verifier_env *env, |
4024 | const char *buf_info, | |
4025 | const struct bpf_reg_state *reg, | |
4026 | int regno, int off, int size) | |
9df1c28b MM |
4027 | { |
4028 | if (off < 0) { | |
4029 | verbose(env, | |
4fc00b79 | 4030 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 4031 | regno, buf_info, off, size); |
9df1c28b MM |
4032 | return -EACCES; |
4033 | } | |
4034 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4035 | char tn_buf[48]; | |
4036 | ||
4037 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4038 | verbose(env, | |
4fc00b79 | 4039 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
4040 | regno, off, tn_buf); |
4041 | return -EACCES; | |
4042 | } | |
afbf21dc YS |
4043 | |
4044 | return 0; | |
4045 | } | |
4046 | ||
4047 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
4048 | const struct bpf_reg_state *reg, | |
4049 | int regno, int off, int size) | |
4050 | { | |
4051 | int err; | |
4052 | ||
4053 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
4054 | if (err) | |
4055 | return err; | |
4056 | ||
9df1c28b MM |
4057 | if (off + size > env->prog->aux->max_tp_access) |
4058 | env->prog->aux->max_tp_access = off + size; | |
4059 | ||
4060 | return 0; | |
4061 | } | |
4062 | ||
afbf21dc YS |
4063 | static int check_buffer_access(struct bpf_verifier_env *env, |
4064 | const struct bpf_reg_state *reg, | |
4065 | int regno, int off, int size, | |
4066 | bool zero_size_allowed, | |
4067 | const char *buf_info, | |
4068 | u32 *max_access) | |
4069 | { | |
4070 | int err; | |
4071 | ||
4072 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
4073 | if (err) | |
4074 | return err; | |
4075 | ||
4076 | if (off + size > *max_access) | |
4077 | *max_access = off + size; | |
4078 | ||
4079 | return 0; | |
4080 | } | |
4081 | ||
3f50f132 JF |
4082 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
4083 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
4084 | { | |
4085 | reg->var_off = tnum_subreg(reg->var_off); | |
4086 | __reg_assign_32_into_64(reg); | |
4087 | } | |
9df1c28b | 4088 | |
0c17d1d2 JH |
4089 | /* truncate register to smaller size (in bytes) |
4090 | * must be called with size < BPF_REG_SIZE | |
4091 | */ | |
4092 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
4093 | { | |
4094 | u64 mask; | |
4095 | ||
4096 | /* clear high bits in bit representation */ | |
4097 | reg->var_off = tnum_cast(reg->var_off, size); | |
4098 | ||
4099 | /* fix arithmetic bounds */ | |
4100 | mask = ((u64)1 << (size * 8)) - 1; | |
4101 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
4102 | reg->umin_value &= mask; | |
4103 | reg->umax_value &= mask; | |
4104 | } else { | |
4105 | reg->umin_value = 0; | |
4106 | reg->umax_value = mask; | |
4107 | } | |
4108 | reg->smin_value = reg->umin_value; | |
4109 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
4110 | |
4111 | /* If size is smaller than 32bit register the 32bit register | |
4112 | * values are also truncated so we push 64-bit bounds into | |
4113 | * 32-bit bounds. Above were truncated < 32-bits already. | |
4114 | */ | |
4115 | if (size >= 4) | |
4116 | return; | |
4117 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
4118 | } |
4119 | ||
a23740ec AN |
4120 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
4121 | { | |
353050be DB |
4122 | /* A map is considered read-only if the following condition are true: |
4123 | * | |
4124 | * 1) BPF program side cannot change any of the map content. The | |
4125 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
4126 | * and was set at map creation time. | |
4127 | * 2) The map value(s) have been initialized from user space by a | |
4128 | * loader and then "frozen", such that no new map update/delete | |
4129 | * operations from syscall side are possible for the rest of | |
4130 | * the map's lifetime from that point onwards. | |
4131 | * 3) Any parallel/pending map update/delete operations from syscall | |
4132 | * side have been completed. Only after that point, it's safe to | |
4133 | * assume that map value(s) are immutable. | |
4134 | */ | |
4135 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
4136 | READ_ONCE(map->frozen) && | |
4137 | !bpf_map_write_active(map); | |
a23740ec AN |
4138 | } |
4139 | ||
4140 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
4141 | { | |
4142 | void *ptr; | |
4143 | u64 addr; | |
4144 | int err; | |
4145 | ||
4146 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
4147 | if (err) | |
4148 | return err; | |
2dedd7d2 | 4149 | ptr = (void *)(long)addr + off; |
a23740ec AN |
4150 | |
4151 | switch (size) { | |
4152 | case sizeof(u8): | |
4153 | *val = (u64)*(u8 *)ptr; | |
4154 | break; | |
4155 | case sizeof(u16): | |
4156 | *val = (u64)*(u16 *)ptr; | |
4157 | break; | |
4158 | case sizeof(u32): | |
4159 | *val = (u64)*(u32 *)ptr; | |
4160 | break; | |
4161 | case sizeof(u64): | |
4162 | *val = *(u64 *)ptr; | |
4163 | break; | |
4164 | default: | |
4165 | return -EINVAL; | |
4166 | } | |
4167 | return 0; | |
4168 | } | |
4169 | ||
9e15db66 AS |
4170 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
4171 | struct bpf_reg_state *regs, | |
4172 | int regno, int off, int size, | |
4173 | enum bpf_access_type atype, | |
4174 | int value_regno) | |
4175 | { | |
4176 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
4177 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
4178 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
9e15db66 AS |
4179 | u32 btf_id; |
4180 | int ret; | |
4181 | ||
9e15db66 AS |
4182 | if (off < 0) { |
4183 | verbose(env, | |
4184 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
4185 | regno, tname, off); | |
4186 | return -EACCES; | |
4187 | } | |
4188 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4189 | char tn_buf[48]; | |
4190 | ||
4191 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4192 | verbose(env, | |
4193 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
4194 | regno, tname, off, tn_buf); | |
4195 | return -EACCES; | |
4196 | } | |
4197 | ||
27ae7997 | 4198 | if (env->ops->btf_struct_access) { |
22dc4a0f AN |
4199 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
4200 | off, size, atype, &btf_id); | |
27ae7997 MKL |
4201 | } else { |
4202 | if (atype != BPF_READ) { | |
4203 | verbose(env, "only read is supported\n"); | |
4204 | return -EACCES; | |
4205 | } | |
4206 | ||
22dc4a0f AN |
4207 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
4208 | atype, &btf_id); | |
27ae7997 MKL |
4209 | } |
4210 | ||
9e15db66 AS |
4211 | if (ret < 0) |
4212 | return ret; | |
4213 | ||
41c48f3a | 4214 | if (atype == BPF_READ && value_regno >= 0) |
22dc4a0f | 4215 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id); |
41c48f3a AI |
4216 | |
4217 | return 0; | |
4218 | } | |
4219 | ||
4220 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
4221 | struct bpf_reg_state *regs, | |
4222 | int regno, int off, int size, | |
4223 | enum bpf_access_type atype, | |
4224 | int value_regno) | |
4225 | { | |
4226 | struct bpf_reg_state *reg = regs + regno; | |
4227 | struct bpf_map *map = reg->map_ptr; | |
4228 | const struct btf_type *t; | |
4229 | const char *tname; | |
4230 | u32 btf_id; | |
4231 | int ret; | |
4232 | ||
4233 | if (!btf_vmlinux) { | |
4234 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
4235 | return -ENOTSUPP; | |
4236 | } | |
4237 | ||
4238 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
4239 | verbose(env, "map_ptr access not supported for map type %d\n", | |
4240 | map->map_type); | |
4241 | return -ENOTSUPP; | |
4242 | } | |
4243 | ||
4244 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
4245 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
4246 | ||
4247 | if (!env->allow_ptr_to_map_access) { | |
4248 | verbose(env, | |
4249 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
4250 | tname); | |
4251 | return -EPERM; | |
9e15db66 | 4252 | } |
27ae7997 | 4253 | |
41c48f3a AI |
4254 | if (off < 0) { |
4255 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
4256 | regno, tname, off); | |
4257 | return -EACCES; | |
4258 | } | |
4259 | ||
4260 | if (atype != BPF_READ) { | |
4261 | verbose(env, "only read from %s is supported\n", tname); | |
4262 | return -EACCES; | |
4263 | } | |
4264 | ||
22dc4a0f | 4265 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id); |
41c48f3a AI |
4266 | if (ret < 0) |
4267 | return ret; | |
4268 | ||
4269 | if (value_regno >= 0) | |
22dc4a0f | 4270 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id); |
41c48f3a | 4271 | |
9e15db66 AS |
4272 | return 0; |
4273 | } | |
4274 | ||
01f810ac AM |
4275 | /* Check that the stack access at the given offset is within bounds. The |
4276 | * maximum valid offset is -1. | |
4277 | * | |
4278 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
4279 | * -state->allocated_stack for reads. | |
4280 | */ | |
4281 | static int check_stack_slot_within_bounds(int off, | |
4282 | struct bpf_func_state *state, | |
4283 | enum bpf_access_type t) | |
4284 | { | |
4285 | int min_valid_off; | |
4286 | ||
4287 | if (t == BPF_WRITE) | |
4288 | min_valid_off = -MAX_BPF_STACK; | |
4289 | else | |
4290 | min_valid_off = -state->allocated_stack; | |
4291 | ||
4292 | if (off < min_valid_off || off > -1) | |
4293 | return -EACCES; | |
4294 | return 0; | |
4295 | } | |
4296 | ||
4297 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
4298 | * bounds. | |
4299 | * | |
4300 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
4301 | */ | |
4302 | static int check_stack_access_within_bounds( | |
4303 | struct bpf_verifier_env *env, | |
4304 | int regno, int off, int access_size, | |
4305 | enum stack_access_src src, enum bpf_access_type type) | |
4306 | { | |
4307 | struct bpf_reg_state *regs = cur_regs(env); | |
4308 | struct bpf_reg_state *reg = regs + regno; | |
4309 | struct bpf_func_state *state = func(env, reg); | |
4310 | int min_off, max_off; | |
4311 | int err; | |
4312 | char *err_extra; | |
4313 | ||
4314 | if (src == ACCESS_HELPER) | |
4315 | /* We don't know if helpers are reading or writing (or both). */ | |
4316 | err_extra = " indirect access to"; | |
4317 | else if (type == BPF_READ) | |
4318 | err_extra = " read from"; | |
4319 | else | |
4320 | err_extra = " write to"; | |
4321 | ||
4322 | if (tnum_is_const(reg->var_off)) { | |
4323 | min_off = reg->var_off.value + off; | |
4324 | if (access_size > 0) | |
4325 | max_off = min_off + access_size - 1; | |
4326 | else | |
4327 | max_off = min_off; | |
4328 | } else { | |
4329 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
4330 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
4331 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
4332 | err_extra, regno); | |
4333 | return -EACCES; | |
4334 | } | |
4335 | min_off = reg->smin_value + off; | |
4336 | if (access_size > 0) | |
4337 | max_off = reg->smax_value + off + access_size - 1; | |
4338 | else | |
4339 | max_off = min_off; | |
4340 | } | |
4341 | ||
4342 | err = check_stack_slot_within_bounds(min_off, state, type); | |
4343 | if (!err) | |
4344 | err = check_stack_slot_within_bounds(max_off, state, type); | |
4345 | ||
4346 | if (err) { | |
4347 | if (tnum_is_const(reg->var_off)) { | |
4348 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
4349 | err_extra, regno, off, access_size); | |
4350 | } else { | |
4351 | char tn_buf[48]; | |
4352 | ||
4353 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4354 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
4355 | err_extra, regno, tn_buf, access_size); | |
4356 | } | |
4357 | } | |
4358 | return err; | |
4359 | } | |
41c48f3a | 4360 | |
17a52670 AS |
4361 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
4362 | * if t==write, value_regno is a register which value is stored into memory | |
4363 | * if t==read, value_regno is a register which will receive the value from memory | |
4364 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
4365 | * if t==read && value_regno==-1, don't care what we read from memory | |
4366 | */ | |
ca369602 DB |
4367 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
4368 | int off, int bpf_size, enum bpf_access_type t, | |
4369 | int value_regno, bool strict_alignment_once) | |
17a52670 | 4370 | { |
638f5b90 AS |
4371 | struct bpf_reg_state *regs = cur_regs(env); |
4372 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 4373 | struct bpf_func_state *state; |
17a52670 AS |
4374 | int size, err = 0; |
4375 | ||
4376 | size = bpf_size_to_bytes(bpf_size); | |
4377 | if (size < 0) | |
4378 | return size; | |
4379 | ||
f1174f77 | 4380 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 4381 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
4382 | if (err) |
4383 | return err; | |
17a52670 | 4384 | |
f1174f77 EC |
4385 | /* for access checks, reg->off is just part of off */ |
4386 | off += reg->off; | |
4387 | ||
69c087ba YS |
4388 | if (reg->type == PTR_TO_MAP_KEY) { |
4389 | if (t == BPF_WRITE) { | |
4390 | verbose(env, "write to change key R%d not allowed\n", regno); | |
4391 | return -EACCES; | |
4392 | } | |
4393 | ||
4394 | err = check_mem_region_access(env, regno, off, size, | |
4395 | reg->map_ptr->key_size, false); | |
4396 | if (err) | |
4397 | return err; | |
4398 | if (value_regno >= 0) | |
4399 | mark_reg_unknown(env, regs, value_regno); | |
4400 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
4401 | if (t == BPF_WRITE && value_regno >= 0 && |
4402 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4403 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
4404 | return -EACCES; |
4405 | } | |
591fe988 DB |
4406 | err = check_map_access_type(env, regno, off, size, t); |
4407 | if (err) | |
4408 | return err; | |
9fd29c08 | 4409 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
4410 | if (!err && t == BPF_READ && value_regno >= 0) { |
4411 | struct bpf_map *map = reg->map_ptr; | |
4412 | ||
4413 | /* if map is read-only, track its contents as scalars */ | |
4414 | if (tnum_is_const(reg->var_off) && | |
4415 | bpf_map_is_rdonly(map) && | |
4416 | map->ops->map_direct_value_addr) { | |
4417 | int map_off = off + reg->var_off.value; | |
4418 | u64 val = 0; | |
4419 | ||
4420 | err = bpf_map_direct_read(map, map_off, size, | |
4421 | &val); | |
4422 | if (err) | |
4423 | return err; | |
4424 | ||
4425 | regs[value_regno].type = SCALAR_VALUE; | |
4426 | __mark_reg_known(®s[value_regno], val); | |
4427 | } else { | |
4428 | mark_reg_unknown(env, regs, value_regno); | |
4429 | } | |
4430 | } | |
457f4436 AN |
4431 | } else if (reg->type == PTR_TO_MEM) { |
4432 | if (t == BPF_WRITE && value_regno >= 0 && | |
4433 | is_pointer_value(env, value_regno)) { | |
4434 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
4435 | return -EACCES; | |
4436 | } | |
4437 | err = check_mem_region_access(env, regno, off, size, | |
4438 | reg->mem_size, false); | |
4439 | if (!err && t == BPF_READ && value_regno >= 0) | |
4440 | mark_reg_unknown(env, regs, value_regno); | |
1a0dc1ac | 4441 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 4442 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 4443 | struct btf *btf = NULL; |
9e15db66 | 4444 | u32 btf_id = 0; |
19de99f7 | 4445 | |
1be7f75d AS |
4446 | if (t == BPF_WRITE && value_regno >= 0 && |
4447 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4448 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
4449 | return -EACCES; |
4450 | } | |
f1174f77 | 4451 | |
58990d1f DB |
4452 | err = check_ctx_reg(env, reg, regno); |
4453 | if (err < 0) | |
4454 | return err; | |
4455 | ||
22dc4a0f | 4456 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id); |
9e15db66 AS |
4457 | if (err) |
4458 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 4459 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 4460 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
4461 | * PTR_TO_PACKET[_META,_END]. In the latter |
4462 | * case, we know the offset is zero. | |
f1174f77 | 4463 | */ |
46f8bc92 | 4464 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 4465 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 4466 | } else { |
638f5b90 | 4467 | mark_reg_known_zero(env, regs, |
61bd5218 | 4468 | value_regno); |
46f8bc92 MKL |
4469 | if (reg_type_may_be_null(reg_type)) |
4470 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
4471 | /* A load of ctx field could have different |
4472 | * actual load size with the one encoded in the | |
4473 | * insn. When the dst is PTR, it is for sure not | |
4474 | * a sub-register. | |
4475 | */ | |
4476 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
b121b341 | 4477 | if (reg_type == PTR_TO_BTF_ID || |
22dc4a0f AN |
4478 | reg_type == PTR_TO_BTF_ID_OR_NULL) { |
4479 | regs[value_regno].btf = btf; | |
9e15db66 | 4480 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 4481 | } |
46f8bc92 | 4482 | } |
638f5b90 | 4483 | regs[value_regno].type = reg_type; |
969bf05e | 4484 | } |
17a52670 | 4485 | |
f1174f77 | 4486 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
4487 | /* Basic bounds checks. */ |
4488 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
4489 | if (err) |
4490 | return err; | |
8726679a | 4491 | |
f4d7e40a AS |
4492 | state = func(env, reg); |
4493 | err = update_stack_depth(env, state, off); | |
4494 | if (err) | |
4495 | return err; | |
8726679a | 4496 | |
01f810ac AM |
4497 | if (t == BPF_READ) |
4498 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 4499 | value_regno); |
01f810ac AM |
4500 | else |
4501 | err = check_stack_write(env, regno, off, size, | |
4502 | value_regno, insn_idx); | |
de8f3a83 | 4503 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 4504 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 4505 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
4506 | return -EACCES; |
4507 | } | |
4acf6c0b BB |
4508 | if (t == BPF_WRITE && value_regno >= 0 && |
4509 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
4510 | verbose(env, "R%d leaks addr into packet\n", |
4511 | value_regno); | |
4acf6c0b BB |
4512 | return -EACCES; |
4513 | } | |
9fd29c08 | 4514 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 4515 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 4516 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
4517 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
4518 | if (t == BPF_WRITE && value_regno >= 0 && | |
4519 | is_pointer_value(env, value_regno)) { | |
4520 | verbose(env, "R%d leaks addr into flow keys\n", | |
4521 | value_regno); | |
4522 | return -EACCES; | |
4523 | } | |
4524 | ||
4525 | err = check_flow_keys_access(env, off, size); | |
4526 | if (!err && t == BPF_READ && value_regno >= 0) | |
4527 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 4528 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 4529 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
4530 | verbose(env, "R%d cannot write into %s\n", |
4531 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
4532 | return -EACCES; |
4533 | } | |
5f456649 | 4534 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
4535 | if (!err && value_regno >= 0) |
4536 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
4537 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
4538 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
4539 | if (!err && t == BPF_READ && value_regno >= 0) | |
4540 | mark_reg_unknown(env, regs, value_regno); | |
9e15db66 AS |
4541 | } else if (reg->type == PTR_TO_BTF_ID) { |
4542 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, | |
4543 | value_regno); | |
41c48f3a AI |
4544 | } else if (reg->type == CONST_PTR_TO_MAP) { |
4545 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
4546 | value_regno); | |
afbf21dc YS |
4547 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
4548 | if (t == BPF_WRITE) { | |
4549 | verbose(env, "R%d cannot write into %s\n", | |
4550 | regno, reg_type_str[reg->type]); | |
4551 | return -EACCES; | |
4552 | } | |
f6dfbe31 CIK |
4553 | err = check_buffer_access(env, reg, regno, off, size, false, |
4554 | "rdonly", | |
afbf21dc YS |
4555 | &env->prog->aux->max_rdonly_access); |
4556 | if (!err && value_regno >= 0) | |
4557 | mark_reg_unknown(env, regs, value_regno); | |
4558 | } else if (reg->type == PTR_TO_RDWR_BUF) { | |
f6dfbe31 CIK |
4559 | err = check_buffer_access(env, reg, regno, off, size, false, |
4560 | "rdwr", | |
afbf21dc YS |
4561 | &env->prog->aux->max_rdwr_access); |
4562 | if (!err && t == BPF_READ && value_regno >= 0) | |
4563 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 4564 | } else { |
61bd5218 JK |
4565 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
4566 | reg_type_str[reg->type]); | |
17a52670 AS |
4567 | return -EACCES; |
4568 | } | |
969bf05e | 4569 | |
f1174f77 | 4570 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 4571 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 4572 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 4573 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 4574 | } |
17a52670 AS |
4575 | return err; |
4576 | } | |
4577 | ||
91c960b0 | 4578 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 4579 | { |
5ffa2550 | 4580 | int load_reg; |
17a52670 AS |
4581 | int err; |
4582 | ||
5ca419f2 BJ |
4583 | switch (insn->imm) { |
4584 | case BPF_ADD: | |
4585 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
4586 | case BPF_AND: |
4587 | case BPF_AND | BPF_FETCH: | |
4588 | case BPF_OR: | |
4589 | case BPF_OR | BPF_FETCH: | |
4590 | case BPF_XOR: | |
4591 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
4592 | case BPF_XCHG: |
4593 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
4594 | break; |
4595 | default: | |
91c960b0 BJ |
4596 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
4597 | return -EINVAL; | |
4598 | } | |
4599 | ||
4600 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
4601 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
4602 | return -EINVAL; |
4603 | } | |
4604 | ||
4605 | /* check src1 operand */ | |
dc503a8a | 4606 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4607 | if (err) |
4608 | return err; | |
4609 | ||
4610 | /* check src2 operand */ | |
dc503a8a | 4611 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4612 | if (err) |
4613 | return err; | |
4614 | ||
5ffa2550 BJ |
4615 | if (insn->imm == BPF_CMPXCHG) { |
4616 | /* Check comparison of R0 with memory location */ | |
4617 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
4618 | if (err) | |
4619 | return err; | |
4620 | } | |
4621 | ||
6bdf6abc | 4622 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 4623 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
4624 | return -EACCES; |
4625 | } | |
4626 | ||
ca369602 | 4627 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 4628 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
4629 | is_flow_key_reg(env, insn->dst_reg) || |
4630 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 4631 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f DB |
4632 | insn->dst_reg, |
4633 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
4634 | return -EACCES; |
4635 | } | |
4636 | ||
37086bfd BJ |
4637 | if (insn->imm & BPF_FETCH) { |
4638 | if (insn->imm == BPF_CMPXCHG) | |
4639 | load_reg = BPF_REG_0; | |
4640 | else | |
4641 | load_reg = insn->src_reg; | |
4642 | ||
4643 | /* check and record load of old value */ | |
4644 | err = check_reg_arg(env, load_reg, DST_OP); | |
4645 | if (err) | |
4646 | return err; | |
4647 | } else { | |
4648 | /* This instruction accesses a memory location but doesn't | |
4649 | * actually load it into a register. | |
4650 | */ | |
4651 | load_reg = -1; | |
4652 | } | |
4653 | ||
91c960b0 | 4654 | /* check whether we can read the memory */ |
31fd8581 | 4655 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
37086bfd | 4656 | BPF_SIZE(insn->code), BPF_READ, load_reg, true); |
17a52670 AS |
4657 | if (err) |
4658 | return err; | |
4659 | ||
91c960b0 | 4660 | /* check whether we can write into the same memory */ |
5ca419f2 BJ |
4661 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
4662 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
4663 | if (err) | |
4664 | return err; | |
4665 | ||
5ca419f2 | 4666 | return 0; |
17a52670 AS |
4667 | } |
4668 | ||
01f810ac AM |
4669 | /* When register 'regno' is used to read the stack (either directly or through |
4670 | * a helper function) make sure that it's within stack boundary and, depending | |
4671 | * on the access type, that all elements of the stack are initialized. | |
4672 | * | |
4673 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
4674 | * | |
4675 | * All registers that have been spilled on the stack in the slots within the | |
4676 | * read offsets are marked as read. | |
4677 | */ | |
4678 | static int check_stack_range_initialized( | |
4679 | struct bpf_verifier_env *env, int regno, int off, | |
4680 | int access_size, bool zero_size_allowed, | |
4681 | enum stack_access_src type, struct bpf_call_arg_meta *meta) | |
2011fccf AI |
4682 | { |
4683 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
4684 | struct bpf_func_state *state = func(env, reg); |
4685 | int err, min_off, max_off, i, j, slot, spi; | |
4686 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
4687 | enum bpf_access_type bounds_check_type; | |
4688 | /* Some accesses can write anything into the stack, others are | |
4689 | * read-only. | |
4690 | */ | |
4691 | bool clobber = false; | |
2011fccf | 4692 | |
01f810ac AM |
4693 | if (access_size == 0 && !zero_size_allowed) { |
4694 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
4695 | return -EACCES; |
4696 | } | |
2011fccf | 4697 | |
01f810ac AM |
4698 | if (type == ACCESS_HELPER) { |
4699 | /* The bounds checks for writes are more permissive than for | |
4700 | * reads. However, if raw_mode is not set, we'll do extra | |
4701 | * checks below. | |
4702 | */ | |
4703 | bounds_check_type = BPF_WRITE; | |
4704 | clobber = true; | |
4705 | } else { | |
4706 | bounds_check_type = BPF_READ; | |
4707 | } | |
4708 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
4709 | type, bounds_check_type); | |
4710 | if (err) | |
4711 | return err; | |
4712 | ||
17a52670 | 4713 | |
2011fccf | 4714 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 4715 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 4716 | } else { |
088ec26d AI |
4717 | /* Variable offset is prohibited for unprivileged mode for |
4718 | * simplicity since it requires corresponding support in | |
4719 | * Spectre masking for stack ALU. | |
4720 | * See also retrieve_ptr_limit(). | |
4721 | */ | |
2c78ee89 | 4722 | if (!env->bypass_spec_v1) { |
088ec26d | 4723 | char tn_buf[48]; |
f1174f77 | 4724 | |
088ec26d | 4725 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
4726 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
4727 | regno, err_extra, tn_buf); | |
088ec26d AI |
4728 | return -EACCES; |
4729 | } | |
f2bcd05e AI |
4730 | /* Only initialized buffer on stack is allowed to be accessed |
4731 | * with variable offset. With uninitialized buffer it's hard to | |
4732 | * guarantee that whole memory is marked as initialized on | |
4733 | * helper return since specific bounds are unknown what may | |
4734 | * cause uninitialized stack leaking. | |
4735 | */ | |
4736 | if (meta && meta->raw_mode) | |
4737 | meta = NULL; | |
4738 | ||
01f810ac AM |
4739 | min_off = reg->smin_value + off; |
4740 | max_off = reg->smax_value + off; | |
17a52670 AS |
4741 | } |
4742 | ||
435faee1 DB |
4743 | if (meta && meta->raw_mode) { |
4744 | meta->access_size = access_size; | |
4745 | meta->regno = regno; | |
4746 | return 0; | |
4747 | } | |
4748 | ||
2011fccf | 4749 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
4750 | u8 *stype; |
4751 | ||
2011fccf | 4752 | slot = -i - 1; |
638f5b90 | 4753 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
4754 | if (state->allocated_stack <= slot) |
4755 | goto err; | |
4756 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
4757 | if (*stype == STACK_MISC) | |
4758 | goto mark; | |
4759 | if (*stype == STACK_ZERO) { | |
01f810ac AM |
4760 | if (clobber) { |
4761 | /* helper can write anything into the stack */ | |
4762 | *stype = STACK_MISC; | |
4763 | } | |
cc2b14d5 | 4764 | goto mark; |
17a52670 | 4765 | } |
1d68f22b | 4766 | |
27113c59 | 4767 | if (is_spilled_reg(&state->stack[spi]) && |
1d68f22b YS |
4768 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
4769 | goto mark; | |
4770 | ||
27113c59 | 4771 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
4772 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
4773 | env->allow_ptr_leaks)) { | |
01f810ac AM |
4774 | if (clobber) { |
4775 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
4776 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 4777 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 4778 | } |
f7cf25b2 AS |
4779 | goto mark; |
4780 | } | |
4781 | ||
cc2b14d5 | 4782 | err: |
2011fccf | 4783 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
4784 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
4785 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
4786 | } else { |
4787 | char tn_buf[48]; | |
4788 | ||
4789 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
4790 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
4791 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 4792 | } |
cc2b14d5 AS |
4793 | return -EACCES; |
4794 | mark: | |
4795 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
4796 | * the whole slot to be marked as 'read' | |
4797 | */ | |
679c782d | 4798 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
4799 | state->stack[spi].spilled_ptr.parent, |
4800 | REG_LIVE_READ64); | |
17a52670 | 4801 | } |
2011fccf | 4802 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
4803 | } |
4804 | ||
06c1c049 GB |
4805 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
4806 | int access_size, bool zero_size_allowed, | |
4807 | struct bpf_call_arg_meta *meta) | |
4808 | { | |
638f5b90 | 4809 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 4810 | |
f1174f77 | 4811 | switch (reg->type) { |
06c1c049 | 4812 | case PTR_TO_PACKET: |
de8f3a83 | 4813 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
4814 | return check_packet_access(env, regno, reg->off, access_size, |
4815 | zero_size_allowed); | |
69c087ba YS |
4816 | case PTR_TO_MAP_KEY: |
4817 | return check_mem_region_access(env, regno, reg->off, access_size, | |
4818 | reg->map_ptr->key_size, false); | |
06c1c049 | 4819 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
4820 | if (check_map_access_type(env, regno, reg->off, access_size, |
4821 | meta && meta->raw_mode ? BPF_WRITE : | |
4822 | BPF_READ)) | |
4823 | return -EACCES; | |
9fd29c08 YS |
4824 | return check_map_access(env, regno, reg->off, access_size, |
4825 | zero_size_allowed); | |
457f4436 AN |
4826 | case PTR_TO_MEM: |
4827 | return check_mem_region_access(env, regno, reg->off, | |
4828 | access_size, reg->mem_size, | |
4829 | zero_size_allowed); | |
afbf21dc YS |
4830 | case PTR_TO_RDONLY_BUF: |
4831 | if (meta && meta->raw_mode) | |
4832 | return -EACCES; | |
4833 | return check_buffer_access(env, reg, regno, reg->off, | |
4834 | access_size, zero_size_allowed, | |
4835 | "rdonly", | |
4836 | &env->prog->aux->max_rdonly_access); | |
4837 | case PTR_TO_RDWR_BUF: | |
4838 | return check_buffer_access(env, reg, regno, reg->off, | |
4839 | access_size, zero_size_allowed, | |
4840 | "rdwr", | |
4841 | &env->prog->aux->max_rdwr_access); | |
0d004c02 | 4842 | case PTR_TO_STACK: |
01f810ac AM |
4843 | return check_stack_range_initialized( |
4844 | env, | |
4845 | regno, reg->off, access_size, | |
4846 | zero_size_allowed, ACCESS_HELPER, meta); | |
0d004c02 LB |
4847 | default: /* scalar_value or invalid ptr */ |
4848 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
4849 | if (zero_size_allowed && access_size == 0 && | |
4850 | register_is_null(reg)) | |
4851 | return 0; | |
4852 | ||
4853 | verbose(env, "R%d type=%s expected=%s\n", regno, | |
4854 | reg_type_str[reg->type], | |
4855 | reg_type_str[PTR_TO_STACK]); | |
4856 | return -EACCES; | |
06c1c049 GB |
4857 | } |
4858 | } | |
4859 | ||
e5069b9c DB |
4860 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
4861 | u32 regno, u32 mem_size) | |
4862 | { | |
4863 | if (register_is_null(reg)) | |
4864 | return 0; | |
4865 | ||
4866 | if (reg_type_may_be_null(reg->type)) { | |
4867 | /* Assuming that the register contains a value check if the memory | |
4868 | * access is safe. Temporarily save and restore the register's state as | |
4869 | * the conversion shouldn't be visible to a caller. | |
4870 | */ | |
4871 | const struct bpf_reg_state saved_reg = *reg; | |
4872 | int rv; | |
4873 | ||
4874 | mark_ptr_not_null_reg(reg); | |
4875 | rv = check_helper_mem_access(env, regno, mem_size, true, NULL); | |
4876 | *reg = saved_reg; | |
4877 | return rv; | |
4878 | } | |
4879 | ||
4880 | return check_helper_mem_access(env, regno, mem_size, true, NULL); | |
4881 | } | |
4882 | ||
d83525ca AS |
4883 | /* Implementation details: |
4884 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
4885 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
4886 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
4887 | * value_or_null->value transition, since the verifier only cares about | |
4888 | * the range of access to valid map value pointer and doesn't care about actual | |
4889 | * address of the map element. | |
4890 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
4891 | * reg->id > 0 after value_or_null->value transition. By doing so | |
4892 | * two bpf_map_lookups will be considered two different pointers that | |
4893 | * point to different bpf_spin_locks. | |
4894 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
4895 | * dead-locks. | |
4896 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
4897 | * reg_is_refcounted() logic. The verifier needs to remember only | |
4898 | * one spin_lock instead of array of acquired_refs. | |
4899 | * cur_state->active_spin_lock remembers which map value element got locked | |
4900 | * and clears it after bpf_spin_unlock. | |
4901 | */ | |
4902 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
4903 | bool is_lock) | |
4904 | { | |
4905 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
4906 | struct bpf_verifier_state *cur = env->cur_state; | |
4907 | bool is_const = tnum_is_const(reg->var_off); | |
4908 | struct bpf_map *map = reg->map_ptr; | |
4909 | u64 val = reg->var_off.value; | |
4910 | ||
d83525ca AS |
4911 | if (!is_const) { |
4912 | verbose(env, | |
4913 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
4914 | regno); | |
4915 | return -EINVAL; | |
4916 | } | |
4917 | if (!map->btf) { | |
4918 | verbose(env, | |
4919 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
4920 | map->name); | |
4921 | return -EINVAL; | |
4922 | } | |
4923 | if (!map_value_has_spin_lock(map)) { | |
4924 | if (map->spin_lock_off == -E2BIG) | |
4925 | verbose(env, | |
4926 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
4927 | map->name); | |
4928 | else if (map->spin_lock_off == -ENOENT) | |
4929 | verbose(env, | |
4930 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
4931 | map->name); | |
4932 | else | |
4933 | verbose(env, | |
4934 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
4935 | map->name); | |
4936 | return -EINVAL; | |
4937 | } | |
4938 | if (map->spin_lock_off != val + reg->off) { | |
4939 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
4940 | val + reg->off); | |
4941 | return -EINVAL; | |
4942 | } | |
4943 | if (is_lock) { | |
4944 | if (cur->active_spin_lock) { | |
4945 | verbose(env, | |
4946 | "Locking two bpf_spin_locks are not allowed\n"); | |
4947 | return -EINVAL; | |
4948 | } | |
4949 | cur->active_spin_lock = reg->id; | |
4950 | } else { | |
4951 | if (!cur->active_spin_lock) { | |
4952 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
4953 | return -EINVAL; | |
4954 | } | |
4955 | if (cur->active_spin_lock != reg->id) { | |
4956 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
4957 | return -EINVAL; | |
4958 | } | |
4959 | cur->active_spin_lock = 0; | |
4960 | } | |
4961 | return 0; | |
4962 | } | |
4963 | ||
b00628b1 AS |
4964 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
4965 | struct bpf_call_arg_meta *meta) | |
4966 | { | |
4967 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
4968 | bool is_const = tnum_is_const(reg->var_off); | |
4969 | struct bpf_map *map = reg->map_ptr; | |
4970 | u64 val = reg->var_off.value; | |
4971 | ||
4972 | if (!is_const) { | |
4973 | verbose(env, | |
4974 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
4975 | regno); | |
4976 | return -EINVAL; | |
4977 | } | |
4978 | if (!map->btf) { | |
4979 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
4980 | map->name); | |
4981 | return -EINVAL; | |
4982 | } | |
68134668 AS |
4983 | if (!map_value_has_timer(map)) { |
4984 | if (map->timer_off == -E2BIG) | |
4985 | verbose(env, | |
4986 | "map '%s' has more than one 'struct bpf_timer'\n", | |
4987 | map->name); | |
4988 | else if (map->timer_off == -ENOENT) | |
4989 | verbose(env, | |
4990 | "map '%s' doesn't have 'struct bpf_timer'\n", | |
4991 | map->name); | |
4992 | else | |
4993 | verbose(env, | |
4994 | "map '%s' is not a struct type or bpf_timer is mangled\n", | |
4995 | map->name); | |
4996 | return -EINVAL; | |
4997 | } | |
4998 | if (map->timer_off != val + reg->off) { | |
4999 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", | |
5000 | val + reg->off, map->timer_off); | |
b00628b1 AS |
5001 | return -EINVAL; |
5002 | } | |
5003 | if (meta->map_ptr) { | |
5004 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
5005 | return -EFAULT; | |
5006 | } | |
3e8ce298 | 5007 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
5008 | meta->map_ptr = map; |
5009 | return 0; | |
5010 | } | |
5011 | ||
90133415 DB |
5012 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
5013 | { | |
48946bd6 HL |
5014 | return base_type(type) == ARG_PTR_TO_MEM || |
5015 | base_type(type) == ARG_PTR_TO_UNINIT_MEM; | |
90133415 DB |
5016 | } |
5017 | ||
5018 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
5019 | { | |
5020 | return type == ARG_CONST_SIZE || | |
5021 | type == ARG_CONST_SIZE_OR_ZERO; | |
5022 | } | |
5023 | ||
457f4436 AN |
5024 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
5025 | { | |
5026 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; | |
5027 | } | |
5028 | ||
57c3bb72 AI |
5029 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
5030 | { | |
5031 | return type == ARG_PTR_TO_INT || | |
5032 | type == ARG_PTR_TO_LONG; | |
5033 | } | |
5034 | ||
5035 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
5036 | { | |
5037 | if (type == ARG_PTR_TO_INT) | |
5038 | return sizeof(u32); | |
5039 | else if (type == ARG_PTR_TO_LONG) | |
5040 | return sizeof(u64); | |
5041 | ||
5042 | return -EINVAL; | |
5043 | } | |
5044 | ||
912f442c LB |
5045 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
5046 | const struct bpf_call_arg_meta *meta, | |
5047 | enum bpf_arg_type *arg_type) | |
5048 | { | |
5049 | if (!meta->map_ptr) { | |
5050 | /* kernel subsystem misconfigured verifier */ | |
5051 | verbose(env, "invalid map_ptr to access map->type\n"); | |
5052 | return -EACCES; | |
5053 | } | |
5054 | ||
5055 | switch (meta->map_ptr->map_type) { | |
5056 | case BPF_MAP_TYPE_SOCKMAP: | |
5057 | case BPF_MAP_TYPE_SOCKHASH: | |
5058 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 5059 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
5060 | } else { |
5061 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
5062 | return -EINVAL; | |
5063 | } | |
5064 | break; | |
9330986c JK |
5065 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5066 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
5067 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
5068 | break; | |
912f442c LB |
5069 | default: |
5070 | break; | |
5071 | } | |
5072 | return 0; | |
5073 | } | |
5074 | ||
f79e7ea5 LB |
5075 | struct bpf_reg_types { |
5076 | const enum bpf_reg_type types[10]; | |
1df8f55a | 5077 | u32 *btf_id; |
f79e7ea5 LB |
5078 | }; |
5079 | ||
5080 | static const struct bpf_reg_types map_key_value_types = { | |
5081 | .types = { | |
5082 | PTR_TO_STACK, | |
5083 | PTR_TO_PACKET, | |
5084 | PTR_TO_PACKET_META, | |
69c087ba | 5085 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5086 | PTR_TO_MAP_VALUE, |
5087 | }, | |
5088 | }; | |
5089 | ||
5090 | static const struct bpf_reg_types sock_types = { | |
5091 | .types = { | |
5092 | PTR_TO_SOCK_COMMON, | |
5093 | PTR_TO_SOCKET, | |
5094 | PTR_TO_TCP_SOCK, | |
5095 | PTR_TO_XDP_SOCK, | |
5096 | }, | |
5097 | }; | |
5098 | ||
49a2a4d4 | 5099 | #ifdef CONFIG_NET |
1df8f55a MKL |
5100 | static const struct bpf_reg_types btf_id_sock_common_types = { |
5101 | .types = { | |
5102 | PTR_TO_SOCK_COMMON, | |
5103 | PTR_TO_SOCKET, | |
5104 | PTR_TO_TCP_SOCK, | |
5105 | PTR_TO_XDP_SOCK, | |
5106 | PTR_TO_BTF_ID, | |
5107 | }, | |
5108 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
5109 | }; | |
49a2a4d4 | 5110 | #endif |
1df8f55a | 5111 | |
f79e7ea5 LB |
5112 | static const struct bpf_reg_types mem_types = { |
5113 | .types = { | |
5114 | PTR_TO_STACK, | |
5115 | PTR_TO_PACKET, | |
5116 | PTR_TO_PACKET_META, | |
69c087ba | 5117 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5118 | PTR_TO_MAP_VALUE, |
5119 | PTR_TO_MEM, | |
5120 | PTR_TO_RDONLY_BUF, | |
5121 | PTR_TO_RDWR_BUF, | |
5122 | }, | |
5123 | }; | |
5124 | ||
5125 | static const struct bpf_reg_types int_ptr_types = { | |
5126 | .types = { | |
5127 | PTR_TO_STACK, | |
5128 | PTR_TO_PACKET, | |
5129 | PTR_TO_PACKET_META, | |
69c087ba | 5130 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5131 | PTR_TO_MAP_VALUE, |
5132 | }, | |
5133 | }; | |
5134 | ||
5135 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; | |
5136 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
5137 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
5138 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; | |
5139 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; | |
5140 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
5141 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; | |
eaa6bcb7 | 5142 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
69c087ba YS |
5143 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
5144 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 5145 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 5146 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
f79e7ea5 | 5147 | |
0789e13b | 5148 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
f79e7ea5 LB |
5149 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
5150 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, | |
5151 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, | |
f79e7ea5 LB |
5152 | [ARG_CONST_SIZE] = &scalar_types, |
5153 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
5154 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
5155 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
5156 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 5157 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 5158 | #ifdef CONFIG_NET |
1df8f55a | 5159 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 5160 | #endif |
f79e7ea5 | 5161 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
5162 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
5163 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
5164 | [ARG_PTR_TO_MEM] = &mem_types, | |
f79e7ea5 LB |
5165 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
5166 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, | |
f79e7ea5 LB |
5167 | [ARG_PTR_TO_INT] = &int_ptr_types, |
5168 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 5169 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 5170 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 5171 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 5172 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 5173 | [ARG_PTR_TO_TIMER] = &timer_types, |
f79e7ea5 LB |
5174 | }; |
5175 | ||
5176 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 MKL |
5177 | enum bpf_arg_type arg_type, |
5178 | const u32 *arg_btf_id) | |
f79e7ea5 LB |
5179 | { |
5180 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5181 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 5182 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
5183 | int i, j; |
5184 | ||
48946bd6 | 5185 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
5186 | if (!compatible) { |
5187 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
5188 | return -EFAULT; | |
5189 | } | |
5190 | ||
f79e7ea5 LB |
5191 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
5192 | expected = compatible->types[i]; | |
5193 | if (expected == NOT_INIT) | |
5194 | break; | |
5195 | ||
5196 | if (type == expected) | |
a968d5e2 | 5197 | goto found; |
f79e7ea5 LB |
5198 | } |
5199 | ||
5200 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); | |
5201 | for (j = 0; j + 1 < i; j++) | |
5202 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); | |
5203 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); | |
5204 | return -EACCES; | |
a968d5e2 MKL |
5205 | |
5206 | found: | |
5207 | if (type == PTR_TO_BTF_ID) { | |
1df8f55a MKL |
5208 | if (!arg_btf_id) { |
5209 | if (!compatible->btf_id) { | |
5210 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
5211 | return -EFAULT; | |
5212 | } | |
5213 | arg_btf_id = compatible->btf_id; | |
5214 | } | |
5215 | ||
22dc4a0f AN |
5216 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, |
5217 | btf_vmlinux, *arg_btf_id)) { | |
a968d5e2 | 5218 | verbose(env, "R%d is of type %s but %s is expected\n", |
22dc4a0f AN |
5219 | regno, kernel_type_name(reg->btf, reg->btf_id), |
5220 | kernel_type_name(btf_vmlinux, *arg_btf_id)); | |
a968d5e2 MKL |
5221 | return -EACCES; |
5222 | } | |
5223 | ||
5224 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
5225 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", | |
5226 | regno); | |
5227 | return -EACCES; | |
5228 | } | |
5229 | } | |
5230 | ||
5231 | return 0; | |
f79e7ea5 LB |
5232 | } |
5233 | ||
af7ec138 YS |
5234 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
5235 | struct bpf_call_arg_meta *meta, | |
5236 | const struct bpf_func_proto *fn) | |
17a52670 | 5237 | { |
af7ec138 | 5238 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 5239 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 5240 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 5241 | enum bpf_reg_type type = reg->type; |
17a52670 AS |
5242 | int err = 0; |
5243 | ||
80f1d68c | 5244 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
5245 | return 0; |
5246 | ||
dc503a8a EC |
5247 | err = check_reg_arg(env, regno, SRC_OP); |
5248 | if (err) | |
5249 | return err; | |
17a52670 | 5250 | |
1be7f75d AS |
5251 | if (arg_type == ARG_ANYTHING) { |
5252 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
5253 | verbose(env, "R%d leaks addr into helper function\n", |
5254 | regno); | |
1be7f75d AS |
5255 | return -EACCES; |
5256 | } | |
80f1d68c | 5257 | return 0; |
1be7f75d | 5258 | } |
80f1d68c | 5259 | |
de8f3a83 | 5260 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 5261 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 5262 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
5263 | return -EACCES; |
5264 | } | |
5265 | ||
48946bd6 HL |
5266 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE || |
5267 | base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) { | |
912f442c LB |
5268 | err = resolve_map_arg_type(env, meta, &arg_type); |
5269 | if (err) | |
5270 | return err; | |
5271 | } | |
5272 | ||
48946bd6 | 5273 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
5274 | /* A NULL register has a SCALAR_VALUE type, so skip |
5275 | * type checking. | |
5276 | */ | |
5277 | goto skip_type_check; | |
5278 | ||
a968d5e2 | 5279 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
f79e7ea5 LB |
5280 | if (err) |
5281 | return err; | |
5282 | ||
a968d5e2 | 5283 | if (type == PTR_TO_CTX) { |
feec7040 LB |
5284 | err = check_ctx_reg(env, reg, regno); |
5285 | if (err < 0) | |
5286 | return err; | |
d7b9454a LB |
5287 | } |
5288 | ||
fd1b0d60 | 5289 | skip_type_check: |
02f7c958 | 5290 | if (reg->ref_obj_id) { |
457f4436 AN |
5291 | if (meta->ref_obj_id) { |
5292 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
5293 | regno, reg->ref_obj_id, | |
5294 | meta->ref_obj_id); | |
5295 | return -EFAULT; | |
5296 | } | |
5297 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
5298 | } |
5299 | ||
17a52670 AS |
5300 | if (arg_type == ARG_CONST_MAP_PTR) { |
5301 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
3e8ce298 AS |
5302 | if (meta->map_ptr) { |
5303 | /* Use map_uid (which is unique id of inner map) to reject: | |
5304 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
5305 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
5306 | * if (inner_map1 && inner_map2) { | |
5307 | * timer = bpf_map_lookup_elem(inner_map1); | |
5308 | * if (timer) | |
5309 | * // mismatch would have been allowed | |
5310 | * bpf_timer_init(timer, inner_map2); | |
5311 | * } | |
5312 | * | |
5313 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
5314 | */ | |
5315 | if (meta->map_ptr != reg->map_ptr || | |
5316 | meta->map_uid != reg->map_uid) { | |
5317 | verbose(env, | |
5318 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
5319 | meta->map_uid, reg->map_uid); | |
5320 | return -EINVAL; | |
5321 | } | |
b00628b1 | 5322 | } |
33ff9823 | 5323 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 5324 | meta->map_uid = reg->map_uid; |
17a52670 AS |
5325 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
5326 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
5327 | * check that [key, key + map->key_size) are within | |
5328 | * stack limits and initialized | |
5329 | */ | |
33ff9823 | 5330 | if (!meta->map_ptr) { |
17a52670 AS |
5331 | /* in function declaration map_ptr must come before |
5332 | * map_key, so that it's verified and known before | |
5333 | * we have to check map_key here. Otherwise it means | |
5334 | * that kernel subsystem misconfigured verifier | |
5335 | */ | |
61bd5218 | 5336 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
5337 | return -EACCES; |
5338 | } | |
d71962f3 PC |
5339 | err = check_helper_mem_access(env, regno, |
5340 | meta->map_ptr->key_size, false, | |
5341 | NULL); | |
48946bd6 HL |
5342 | } else if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE || |
5343 | base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) { | |
5344 | if (type_may_be_null(arg_type) && register_is_null(reg)) | |
5345 | return 0; | |
5346 | ||
17a52670 AS |
5347 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
5348 | * check [value, value + map->value_size) validity | |
5349 | */ | |
33ff9823 | 5350 | if (!meta->map_ptr) { |
17a52670 | 5351 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 5352 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
5353 | return -EACCES; |
5354 | } | |
2ea864c5 | 5355 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
5356 | err = check_helper_mem_access(env, regno, |
5357 | meta->map_ptr->value_size, false, | |
2ea864c5 | 5358 | meta); |
eaa6bcb7 HL |
5359 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
5360 | if (!reg->btf_id) { | |
5361 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
5362 | return -EACCES; | |
5363 | } | |
22dc4a0f | 5364 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 5365 | meta->ret_btf_id = reg->btf_id; |
c18f0b6a LB |
5366 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
5367 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
5368 | if (process_spin_lock(env, regno, true)) | |
5369 | return -EACCES; | |
5370 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
5371 | if (process_spin_lock(env, regno, false)) | |
5372 | return -EACCES; | |
5373 | } else { | |
5374 | verbose(env, "verifier internal error\n"); | |
5375 | return -EFAULT; | |
5376 | } | |
b00628b1 AS |
5377 | } else if (arg_type == ARG_PTR_TO_TIMER) { |
5378 | if (process_timer_func(env, regno, meta)) | |
5379 | return -EACCES; | |
69c087ba YS |
5380 | } else if (arg_type == ARG_PTR_TO_FUNC) { |
5381 | meta->subprogno = reg->subprogno; | |
a2bbe7cc LB |
5382 | } else if (arg_type_is_mem_ptr(arg_type)) { |
5383 | /* The access to this pointer is only checked when we hit the | |
5384 | * next is_mem_size argument below. | |
5385 | */ | |
5386 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); | |
90133415 | 5387 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 5388 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 5389 | |
10060503 JF |
5390 | /* This is used to refine r0 return value bounds for helpers |
5391 | * that enforce this value as an upper bound on return values. | |
5392 | * See do_refine_retval_range() for helpers that can refine | |
5393 | * the return value. C type of helper is u32 so we pull register | |
5394 | * bound from umax_value however, if negative verifier errors | |
5395 | * out. Only upper bounds can be learned because retval is an | |
5396 | * int type and negative retvals are allowed. | |
849fa506 | 5397 | */ |
10060503 | 5398 | meta->msize_max_value = reg->umax_value; |
849fa506 | 5399 | |
f1174f77 EC |
5400 | /* The register is SCALAR_VALUE; the access check |
5401 | * happens using its boundaries. | |
06c1c049 | 5402 | */ |
f1174f77 | 5403 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
5404 | /* For unprivileged variable accesses, disable raw |
5405 | * mode so that the program is required to | |
5406 | * initialize all the memory that the helper could | |
5407 | * just partially fill up. | |
5408 | */ | |
5409 | meta = NULL; | |
5410 | ||
b03c9f9f | 5411 | if (reg->smin_value < 0) { |
61bd5218 | 5412 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
5413 | regno); |
5414 | return -EACCES; | |
5415 | } | |
06c1c049 | 5416 | |
b03c9f9f | 5417 | if (reg->umin_value == 0) { |
f1174f77 EC |
5418 | err = check_helper_mem_access(env, regno - 1, 0, |
5419 | zero_size_allowed, | |
5420 | meta); | |
06c1c049 GB |
5421 | if (err) |
5422 | return err; | |
06c1c049 | 5423 | } |
f1174f77 | 5424 | |
b03c9f9f | 5425 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 5426 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
5427 | regno); |
5428 | return -EACCES; | |
5429 | } | |
5430 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 5431 | reg->umax_value, |
f1174f77 | 5432 | zero_size_allowed, meta); |
b5dc0163 AS |
5433 | if (!err) |
5434 | err = mark_chain_precision(env, regno); | |
457f4436 AN |
5435 | } else if (arg_type_is_alloc_size(arg_type)) { |
5436 | if (!tnum_is_const(reg->var_off)) { | |
28a8add6 | 5437 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
5438 | regno); |
5439 | return -EACCES; | |
5440 | } | |
5441 | meta->mem_size = reg->var_off.value; | |
57c3bb72 AI |
5442 | } else if (arg_type_is_int_ptr(arg_type)) { |
5443 | int size = int_ptr_type_to_size(arg_type); | |
5444 | ||
5445 | err = check_helper_mem_access(env, regno, size, false, meta); | |
5446 | if (err) | |
5447 | return err; | |
5448 | err = check_ptr_alignment(env, reg, 0, size, true); | |
fff13c4b FR |
5449 | } else if (arg_type == ARG_PTR_TO_CONST_STR) { |
5450 | struct bpf_map *map = reg->map_ptr; | |
5451 | int map_off; | |
5452 | u64 map_addr; | |
5453 | char *str_ptr; | |
5454 | ||
a8fad73e | 5455 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
5456 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
5457 | return -EACCES; | |
5458 | } | |
5459 | ||
5460 | if (!tnum_is_const(reg->var_off)) { | |
5461 | verbose(env, "R%d is not a constant address'\n", regno); | |
5462 | return -EACCES; | |
5463 | } | |
5464 | ||
5465 | if (!map->ops->map_direct_value_addr) { | |
5466 | verbose(env, "no direct value access support for this map type\n"); | |
5467 | return -EACCES; | |
5468 | } | |
5469 | ||
5470 | err = check_map_access(env, regno, reg->off, | |
5471 | map->value_size - reg->off, false); | |
5472 | if (err) | |
5473 | return err; | |
5474 | ||
5475 | map_off = reg->off + reg->var_off.value; | |
5476 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
5477 | if (err) { | |
5478 | verbose(env, "direct value access on string failed\n"); | |
5479 | return err; | |
5480 | } | |
5481 | ||
5482 | str_ptr = (char *)(long)(map_addr); | |
5483 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
5484 | verbose(env, "string is not zero-terminated\n"); | |
5485 | return -EINVAL; | |
5486 | } | |
17a52670 AS |
5487 | } |
5488 | ||
5489 | return err; | |
5490 | } | |
5491 | ||
0126240f LB |
5492 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
5493 | { | |
5494 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 5495 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
5496 | |
5497 | if (func_id != BPF_FUNC_map_update_elem) | |
5498 | return false; | |
5499 | ||
5500 | /* It's not possible to get access to a locked struct sock in these | |
5501 | * contexts, so updating is safe. | |
5502 | */ | |
5503 | switch (type) { | |
5504 | case BPF_PROG_TYPE_TRACING: | |
5505 | if (eatype == BPF_TRACE_ITER) | |
5506 | return true; | |
5507 | break; | |
5508 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
5509 | case BPF_PROG_TYPE_SCHED_CLS: | |
5510 | case BPF_PROG_TYPE_SCHED_ACT: | |
5511 | case BPF_PROG_TYPE_XDP: | |
5512 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
5513 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
5514 | case BPF_PROG_TYPE_SK_LOOKUP: | |
5515 | return true; | |
5516 | default: | |
5517 | break; | |
5518 | } | |
5519 | ||
5520 | verbose(env, "cannot update sockmap in this context\n"); | |
5521 | return false; | |
5522 | } | |
5523 | ||
e411901c MF |
5524 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
5525 | { | |
5526 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); | |
5527 | } | |
5528 | ||
61bd5218 JK |
5529 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
5530 | struct bpf_map *map, int func_id) | |
35578d79 | 5531 | { |
35578d79 KX |
5532 | if (!map) |
5533 | return 0; | |
5534 | ||
6aff67c8 AS |
5535 | /* We need a two way check, first is from map perspective ... */ |
5536 | switch (map->map_type) { | |
5537 | case BPF_MAP_TYPE_PROG_ARRAY: | |
5538 | if (func_id != BPF_FUNC_tail_call) | |
5539 | goto error; | |
5540 | break; | |
5541 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
5542 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 5543 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 5544 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
5545 | func_id != BPF_FUNC_perf_event_read_value && |
5546 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
5547 | goto error; |
5548 | break; | |
457f4436 AN |
5549 | case BPF_MAP_TYPE_RINGBUF: |
5550 | if (func_id != BPF_FUNC_ringbuf_output && | |
5551 | func_id != BPF_FUNC_ringbuf_reserve && | |
457f4436 AN |
5552 | func_id != BPF_FUNC_ringbuf_query) |
5553 | goto error; | |
5554 | break; | |
6aff67c8 AS |
5555 | case BPF_MAP_TYPE_STACK_TRACE: |
5556 | if (func_id != BPF_FUNC_get_stackid) | |
5557 | goto error; | |
5558 | break; | |
4ed8ec52 | 5559 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 5560 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 5561 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
5562 | goto error; |
5563 | break; | |
cd339431 | 5564 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 5565 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
5566 | if (func_id != BPF_FUNC_get_local_storage) |
5567 | goto error; | |
5568 | break; | |
546ac1ff | 5569 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 5570 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
5571 | if (func_id != BPF_FUNC_redirect_map && |
5572 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
5573 | goto error; |
5574 | break; | |
fbfc504a BT |
5575 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
5576 | * appear. | |
5577 | */ | |
6710e112 JDB |
5578 | case BPF_MAP_TYPE_CPUMAP: |
5579 | if (func_id != BPF_FUNC_redirect_map) | |
5580 | goto error; | |
5581 | break; | |
fada7fdc JL |
5582 | case BPF_MAP_TYPE_XSKMAP: |
5583 | if (func_id != BPF_FUNC_redirect_map && | |
5584 | func_id != BPF_FUNC_map_lookup_elem) | |
5585 | goto error; | |
5586 | break; | |
56f668df | 5587 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 5588 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
5589 | if (func_id != BPF_FUNC_map_lookup_elem) |
5590 | goto error; | |
16a43625 | 5591 | break; |
174a79ff JF |
5592 | case BPF_MAP_TYPE_SOCKMAP: |
5593 | if (func_id != BPF_FUNC_sk_redirect_map && | |
5594 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 5595 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 5596 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 5597 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
5598 | func_id != BPF_FUNC_map_lookup_elem && |
5599 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
5600 | goto error; |
5601 | break; | |
81110384 JF |
5602 | case BPF_MAP_TYPE_SOCKHASH: |
5603 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
5604 | func_id != BPF_FUNC_sock_hash_update && | |
5605 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 5606 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 5607 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
5608 | func_id != BPF_FUNC_map_lookup_elem && |
5609 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
5610 | goto error; |
5611 | break; | |
2dbb9b9e MKL |
5612 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
5613 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
5614 | goto error; | |
5615 | break; | |
f1a2e44a MV |
5616 | case BPF_MAP_TYPE_QUEUE: |
5617 | case BPF_MAP_TYPE_STACK: | |
5618 | if (func_id != BPF_FUNC_map_peek_elem && | |
5619 | func_id != BPF_FUNC_map_pop_elem && | |
5620 | func_id != BPF_FUNC_map_push_elem) | |
5621 | goto error; | |
5622 | break; | |
6ac99e8f MKL |
5623 | case BPF_MAP_TYPE_SK_STORAGE: |
5624 | if (func_id != BPF_FUNC_sk_storage_get && | |
5625 | func_id != BPF_FUNC_sk_storage_delete) | |
5626 | goto error; | |
5627 | break; | |
8ea63684 KS |
5628 | case BPF_MAP_TYPE_INODE_STORAGE: |
5629 | if (func_id != BPF_FUNC_inode_storage_get && | |
5630 | func_id != BPF_FUNC_inode_storage_delete) | |
5631 | goto error; | |
5632 | break; | |
4cf1bc1f KS |
5633 | case BPF_MAP_TYPE_TASK_STORAGE: |
5634 | if (func_id != BPF_FUNC_task_storage_get && | |
5635 | func_id != BPF_FUNC_task_storage_delete) | |
5636 | goto error; | |
5637 | break; | |
9330986c JK |
5638 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5639 | if (func_id != BPF_FUNC_map_peek_elem && | |
5640 | func_id != BPF_FUNC_map_push_elem) | |
5641 | goto error; | |
5642 | break; | |
6aff67c8 AS |
5643 | default: |
5644 | break; | |
5645 | } | |
5646 | ||
5647 | /* ... and second from the function itself. */ | |
5648 | switch (func_id) { | |
5649 | case BPF_FUNC_tail_call: | |
5650 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
5651 | goto error; | |
e411901c MF |
5652 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
5653 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
5654 | return -EINVAL; |
5655 | } | |
6aff67c8 AS |
5656 | break; |
5657 | case BPF_FUNC_perf_event_read: | |
5658 | case BPF_FUNC_perf_event_output: | |
908432ca | 5659 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 5660 | case BPF_FUNC_skb_output: |
d831ee84 | 5661 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
5662 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
5663 | goto error; | |
5664 | break; | |
5b029a32 DB |
5665 | case BPF_FUNC_ringbuf_output: |
5666 | case BPF_FUNC_ringbuf_reserve: | |
5667 | case BPF_FUNC_ringbuf_query: | |
5668 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) | |
5669 | goto error; | |
5670 | break; | |
6aff67c8 AS |
5671 | case BPF_FUNC_get_stackid: |
5672 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
5673 | goto error; | |
5674 | break; | |
60d20f91 | 5675 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 5676 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
5677 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
5678 | goto error; | |
5679 | break; | |
97f91a7c | 5680 | case BPF_FUNC_redirect_map: |
9c270af3 | 5681 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 5682 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
5683 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
5684 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
5685 | goto error; |
5686 | break; | |
174a79ff | 5687 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 5688 | case BPF_FUNC_msg_redirect_map: |
81110384 | 5689 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
5690 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
5691 | goto error; | |
5692 | break; | |
81110384 JF |
5693 | case BPF_FUNC_sk_redirect_hash: |
5694 | case BPF_FUNC_msg_redirect_hash: | |
5695 | case BPF_FUNC_sock_hash_update: | |
5696 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
5697 | goto error; |
5698 | break; | |
cd339431 | 5699 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
5700 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
5701 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
5702 | goto error; |
5703 | break; | |
2dbb9b9e | 5704 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
5705 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
5706 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
5707 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
5708 | goto error; |
5709 | break; | |
f1a2e44a | 5710 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
5711 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
5712 | map->map_type != BPF_MAP_TYPE_STACK) | |
5713 | goto error; | |
5714 | break; | |
9330986c JK |
5715 | case BPF_FUNC_map_peek_elem: |
5716 | case BPF_FUNC_map_push_elem: | |
5717 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
5718 | map->map_type != BPF_MAP_TYPE_STACK && | |
5719 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
5720 | goto error; | |
5721 | break; | |
6ac99e8f MKL |
5722 | case BPF_FUNC_sk_storage_get: |
5723 | case BPF_FUNC_sk_storage_delete: | |
5724 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
5725 | goto error; | |
5726 | break; | |
8ea63684 KS |
5727 | case BPF_FUNC_inode_storage_get: |
5728 | case BPF_FUNC_inode_storage_delete: | |
5729 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
5730 | goto error; | |
5731 | break; | |
4cf1bc1f KS |
5732 | case BPF_FUNC_task_storage_get: |
5733 | case BPF_FUNC_task_storage_delete: | |
5734 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
5735 | goto error; | |
5736 | break; | |
6aff67c8 AS |
5737 | default: |
5738 | break; | |
35578d79 KX |
5739 | } |
5740 | ||
5741 | return 0; | |
6aff67c8 | 5742 | error: |
61bd5218 | 5743 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 5744 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 5745 | return -EINVAL; |
35578d79 KX |
5746 | } |
5747 | ||
90133415 | 5748 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
5749 | { |
5750 | int count = 0; | |
5751 | ||
39f19ebb | 5752 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5753 | count++; |
39f19ebb | 5754 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5755 | count++; |
39f19ebb | 5756 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5757 | count++; |
39f19ebb | 5758 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5759 | count++; |
39f19ebb | 5760 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
5761 | count++; |
5762 | ||
90133415 DB |
5763 | /* We only support one arg being in raw mode at the moment, |
5764 | * which is sufficient for the helper functions we have | |
5765 | * right now. | |
5766 | */ | |
5767 | return count <= 1; | |
5768 | } | |
5769 | ||
5770 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
5771 | enum bpf_arg_type arg_next) | |
5772 | { | |
5773 | return (arg_type_is_mem_ptr(arg_curr) && | |
5774 | !arg_type_is_mem_size(arg_next)) || | |
5775 | (!arg_type_is_mem_ptr(arg_curr) && | |
5776 | arg_type_is_mem_size(arg_next)); | |
5777 | } | |
5778 | ||
5779 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
5780 | { | |
5781 | /* bpf_xxx(..., buf, len) call will access 'len' | |
5782 | * bytes from memory 'buf'. Both arg types need | |
5783 | * to be paired, so make sure there's no buggy | |
5784 | * helper function specification. | |
5785 | */ | |
5786 | if (arg_type_is_mem_size(fn->arg1_type) || | |
5787 | arg_type_is_mem_ptr(fn->arg5_type) || | |
5788 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
5789 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
5790 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
5791 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
5792 | return false; | |
5793 | ||
5794 | return true; | |
5795 | } | |
5796 | ||
1b986589 | 5797 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
5798 | { |
5799 | int count = 0; | |
5800 | ||
1b986589 | 5801 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 5802 | count++; |
1b986589 | 5803 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 5804 | count++; |
1b986589 | 5805 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 5806 | count++; |
1b986589 | 5807 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 5808 | count++; |
1b986589 | 5809 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
5810 | count++; |
5811 | ||
1b986589 MKL |
5812 | /* A reference acquiring function cannot acquire |
5813 | * another refcounted ptr. | |
5814 | */ | |
64d85290 | 5815 | if (may_be_acquire_function(func_id) && count) |
1b986589 MKL |
5816 | return false; |
5817 | ||
fd978bf7 JS |
5818 | /* We only support one arg being unreferenced at the moment, |
5819 | * which is sufficient for the helper functions we have right now. | |
5820 | */ | |
5821 | return count <= 1; | |
5822 | } | |
5823 | ||
9436ef6e LB |
5824 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
5825 | { | |
5826 | int i; | |
5827 | ||
1df8f55a | 5828 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
9436ef6e LB |
5829 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
5830 | return false; | |
5831 | ||
1df8f55a MKL |
5832 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
5833 | return false; | |
5834 | } | |
5835 | ||
9436ef6e LB |
5836 | return true; |
5837 | } | |
5838 | ||
1b986589 | 5839 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
5840 | { |
5841 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 5842 | check_arg_pair_ok(fn) && |
9436ef6e | 5843 | check_btf_id_ok(fn) && |
1b986589 | 5844 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
5845 | } |
5846 | ||
de8f3a83 DB |
5847 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
5848 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 5849 | */ |
f4d7e40a AS |
5850 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
5851 | struct bpf_func_state *state) | |
969bf05e | 5852 | { |
58e2af8b | 5853 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
5854 | int i; |
5855 | ||
5856 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 5857 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 5858 | mark_reg_unknown(env, regs, i); |
969bf05e | 5859 | |
f3709f69 JS |
5860 | bpf_for_each_spilled_reg(i, state, reg) { |
5861 | if (!reg) | |
969bf05e | 5862 | continue; |
de8f3a83 | 5863 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 5864 | __mark_reg_unknown(env, reg); |
969bf05e AS |
5865 | } |
5866 | } | |
5867 | ||
f4d7e40a AS |
5868 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
5869 | { | |
5870 | struct bpf_verifier_state *vstate = env->cur_state; | |
5871 | int i; | |
5872 | ||
5873 | for (i = 0; i <= vstate->curframe; i++) | |
5874 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
5875 | } | |
5876 | ||
6d94e741 AS |
5877 | enum { |
5878 | AT_PKT_END = -1, | |
5879 | BEYOND_PKT_END = -2, | |
5880 | }; | |
5881 | ||
5882 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
5883 | { | |
5884 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5885 | struct bpf_reg_state *reg = &state->regs[regn]; | |
5886 | ||
5887 | if (reg->type != PTR_TO_PACKET) | |
5888 | /* PTR_TO_PACKET_META is not supported yet */ | |
5889 | return; | |
5890 | ||
5891 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
5892 | * How far beyond pkt_end it goes is unknown. | |
5893 | * if (!range_open) it's the case of pkt >= pkt_end | |
5894 | * if (range_open) it's the case of pkt > pkt_end | |
5895 | * hence this pointer is at least 1 byte bigger than pkt_end | |
5896 | */ | |
5897 | if (range_open) | |
5898 | reg->range = BEYOND_PKT_END; | |
5899 | else | |
5900 | reg->range = AT_PKT_END; | |
5901 | } | |
5902 | ||
fd978bf7 | 5903 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
5904 | struct bpf_func_state *state, |
5905 | int ref_obj_id) | |
fd978bf7 JS |
5906 | { |
5907 | struct bpf_reg_state *regs = state->regs, *reg; | |
5908 | int i; | |
5909 | ||
5910 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 5911 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
5912 | mark_reg_unknown(env, regs, i); |
5913 | ||
5914 | bpf_for_each_spilled_reg(i, state, reg) { | |
5915 | if (!reg) | |
5916 | continue; | |
1b986589 | 5917 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 5918 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
5919 | } |
5920 | } | |
5921 | ||
5922 | /* The pointer with the specified id has released its reference to kernel | |
5923 | * resources. Identify all copies of the same pointer and clear the reference. | |
5924 | */ | |
5925 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 5926 | int ref_obj_id) |
fd978bf7 JS |
5927 | { |
5928 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 5929 | int err; |
fd978bf7 JS |
5930 | int i; |
5931 | ||
1b986589 MKL |
5932 | err = release_reference_state(cur_func(env), ref_obj_id); |
5933 | if (err) | |
5934 | return err; | |
5935 | ||
fd978bf7 | 5936 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 5937 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 5938 | |
1b986589 | 5939 | return 0; |
fd978bf7 JS |
5940 | } |
5941 | ||
51c39bb1 AS |
5942 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
5943 | struct bpf_reg_state *regs) | |
5944 | { | |
5945 | int i; | |
5946 | ||
5947 | /* after the call registers r0 - r5 were scratched */ | |
5948 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
5949 | mark_reg_not_init(env, regs, caller_saved[i]); | |
5950 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
5951 | } | |
5952 | } | |
5953 | ||
14351375 YS |
5954 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
5955 | struct bpf_func_state *caller, | |
5956 | struct bpf_func_state *callee, | |
5957 | int insn_idx); | |
5958 | ||
5959 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
5960 | int *insn_idx, int subprog, | |
5961 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
5962 | { |
5963 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 5964 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 5965 | struct bpf_func_state *caller, *callee; |
14351375 | 5966 | int err; |
51c39bb1 | 5967 | bool is_global = false; |
f4d7e40a | 5968 | |
aada9ce6 | 5969 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 5970 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 5971 | state->curframe + 2); |
f4d7e40a AS |
5972 | return -E2BIG; |
5973 | } | |
5974 | ||
f4d7e40a AS |
5975 | caller = state->frame[state->curframe]; |
5976 | if (state->frame[state->curframe + 1]) { | |
5977 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
5978 | state->curframe + 1); | |
5979 | return -EFAULT; | |
5980 | } | |
5981 | ||
51c39bb1 AS |
5982 | func_info_aux = env->prog->aux->func_info_aux; |
5983 | if (func_info_aux) | |
5984 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
34747c41 | 5985 | err = btf_check_subprog_arg_match(env, subprog, caller->regs); |
51c39bb1 AS |
5986 | if (err == -EFAULT) |
5987 | return err; | |
5988 | if (is_global) { | |
5989 | if (err) { | |
5990 | verbose(env, "Caller passes invalid args into func#%d\n", | |
5991 | subprog); | |
5992 | return err; | |
5993 | } else { | |
5994 | if (env->log.level & BPF_LOG_LEVEL) | |
5995 | verbose(env, | |
5996 | "Func#%d is global and valid. Skipping.\n", | |
5997 | subprog); | |
5998 | clear_caller_saved_regs(env, caller->regs); | |
5999 | ||
45159b27 | 6000 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 6001 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 6002 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
6003 | |
6004 | /* continue with next insn after call */ | |
6005 | return 0; | |
6006 | } | |
6007 | } | |
6008 | ||
bfc6bb74 AS |
6009 | if (insn->code == (BPF_JMP | BPF_CALL) && |
6010 | insn->imm == BPF_FUNC_timer_set_callback) { | |
6011 | struct bpf_verifier_state *async_cb; | |
6012 | ||
6013 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 6014 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
6015 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
6016 | *insn_idx, subprog); | |
6017 | if (!async_cb) | |
6018 | return -EFAULT; | |
6019 | callee = async_cb->frame[0]; | |
6020 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
6021 | ||
6022 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
6023 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
6024 | if (err) | |
6025 | return err; | |
6026 | ||
6027 | clear_caller_saved_regs(env, caller->regs); | |
6028 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
6029 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
6030 | /* continue with next insn after call */ | |
6031 | return 0; | |
6032 | } | |
6033 | ||
f4d7e40a AS |
6034 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
6035 | if (!callee) | |
6036 | return -ENOMEM; | |
6037 | state->frame[state->curframe + 1] = callee; | |
6038 | ||
6039 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
6040 | * into its own stack before reading from it. | |
6041 | * callee can read/write into caller's stack | |
6042 | */ | |
6043 | init_func_state(env, callee, | |
6044 | /* remember the callsite, it will be used by bpf_exit */ | |
6045 | *insn_idx /* callsite */, | |
6046 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 6047 | subprog /* subprog number within this prog */); |
f4d7e40a | 6048 | |
fd978bf7 | 6049 | /* Transfer references to the callee */ |
c69431aa | 6050 | err = copy_reference_state(callee, caller); |
fd978bf7 JS |
6051 | if (err) |
6052 | return err; | |
6053 | ||
14351375 YS |
6054 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
6055 | if (err) | |
6056 | return err; | |
f4d7e40a | 6057 | |
51c39bb1 | 6058 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
6059 | |
6060 | /* only increment it after check_reg_arg() finished */ | |
6061 | state->curframe++; | |
6062 | ||
6063 | /* and go analyze first insn of the callee */ | |
14351375 | 6064 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 6065 | |
06ee7115 | 6066 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 6067 | verbose(env, "caller:\n"); |
0f55f9ed | 6068 | print_verifier_state(env, caller, true); |
f4d7e40a | 6069 | verbose(env, "callee:\n"); |
0f55f9ed | 6070 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
6071 | } |
6072 | return 0; | |
6073 | } | |
6074 | ||
314ee05e YS |
6075 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
6076 | struct bpf_func_state *caller, | |
6077 | struct bpf_func_state *callee) | |
6078 | { | |
6079 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
6080 | * void *callback_ctx, u64 flags); | |
6081 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
6082 | * void *callback_ctx); | |
6083 | */ | |
6084 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6085 | ||
6086 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6087 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6088 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6089 | ||
6090 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6091 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6092 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6093 | ||
6094 | /* pointer to stack or null */ | |
6095 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
6096 | ||
6097 | /* unused */ | |
6098 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6099 | return 0; | |
6100 | } | |
6101 | ||
14351375 YS |
6102 | static int set_callee_state(struct bpf_verifier_env *env, |
6103 | struct bpf_func_state *caller, | |
6104 | struct bpf_func_state *callee, int insn_idx) | |
6105 | { | |
6106 | int i; | |
6107 | ||
6108 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
6109 | * pointers, which connects us up to the liveness chain | |
6110 | */ | |
6111 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
6112 | callee->regs[i] = caller->regs[i]; | |
6113 | return 0; | |
6114 | } | |
6115 | ||
6116 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
6117 | int *insn_idx) | |
6118 | { | |
6119 | int subprog, target_insn; | |
6120 | ||
6121 | target_insn = *insn_idx + insn->imm + 1; | |
6122 | subprog = find_subprog(env, target_insn); | |
6123 | if (subprog < 0) { | |
6124 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
6125 | target_insn); | |
6126 | return -EFAULT; | |
6127 | } | |
6128 | ||
6129 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
6130 | } | |
6131 | ||
69c087ba YS |
6132 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
6133 | struct bpf_func_state *caller, | |
6134 | struct bpf_func_state *callee, | |
6135 | int insn_idx) | |
6136 | { | |
6137 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
6138 | struct bpf_map *map; | |
6139 | int err; | |
6140 | ||
6141 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
6142 | verbose(env, "tail_call abusing map_ptr\n"); | |
6143 | return -EINVAL; | |
6144 | } | |
6145 | ||
6146 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
6147 | if (!map->ops->map_set_for_each_callback_args || | |
6148 | !map->ops->map_for_each_callback) { | |
6149 | verbose(env, "callback function not allowed for map\n"); | |
6150 | return -ENOTSUPP; | |
6151 | } | |
6152 | ||
6153 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
6154 | if (err) | |
6155 | return err; | |
6156 | ||
6157 | callee->in_callback_fn = true; | |
6158 | return 0; | |
6159 | } | |
6160 | ||
e6f2dd0f JK |
6161 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
6162 | struct bpf_func_state *caller, | |
6163 | struct bpf_func_state *callee, | |
6164 | int insn_idx) | |
6165 | { | |
6166 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
6167 | * u64 flags); | |
6168 | * callback_fn(u32 index, void *callback_ctx); | |
6169 | */ | |
6170 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
6171 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
6172 | ||
6173 | /* unused */ | |
6174 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
6175 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6176 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6177 | ||
6178 | callee->in_callback_fn = true; | |
6179 | return 0; | |
6180 | } | |
6181 | ||
b00628b1 AS |
6182 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
6183 | struct bpf_func_state *caller, | |
6184 | struct bpf_func_state *callee, | |
6185 | int insn_idx) | |
6186 | { | |
6187 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6188 | ||
6189 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
6190 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
6191 | */ | |
6192 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
6193 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
6194 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
6195 | ||
6196 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6197 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6198 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
6199 | ||
6200 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6201 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6202 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
6203 | ||
6204 | /* unused */ | |
6205 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6206 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 6207 | callee->in_async_callback_fn = true; |
b00628b1 AS |
6208 | return 0; |
6209 | } | |
6210 | ||
7c7e3d31 SL |
6211 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
6212 | struct bpf_func_state *caller, | |
6213 | struct bpf_func_state *callee, | |
6214 | int insn_idx) | |
6215 | { | |
6216 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
6217 | * void *callback_fn, void *callback_ctx, u64 flags) | |
6218 | * (callback_fn)(struct task_struct *task, | |
6219 | * struct vm_area_struct *vma, void *callback_ctx); | |
6220 | */ | |
6221 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6222 | ||
6223 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
6224 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6225 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 6226 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
6227 | |
6228 | /* pointer to stack or null */ | |
6229 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
6230 | ||
6231 | /* unused */ | |
6232 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6233 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6234 | callee->in_callback_fn = true; | |
6235 | return 0; | |
6236 | } | |
6237 | ||
f4d7e40a AS |
6238 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
6239 | { | |
6240 | struct bpf_verifier_state *state = env->cur_state; | |
6241 | struct bpf_func_state *caller, *callee; | |
6242 | struct bpf_reg_state *r0; | |
fd978bf7 | 6243 | int err; |
f4d7e40a AS |
6244 | |
6245 | callee = state->frame[state->curframe]; | |
6246 | r0 = &callee->regs[BPF_REG_0]; | |
6247 | if (r0->type == PTR_TO_STACK) { | |
6248 | /* technically it's ok to return caller's stack pointer | |
6249 | * (or caller's caller's pointer) back to the caller, | |
6250 | * since these pointers are valid. Only current stack | |
6251 | * pointer will be invalid as soon as function exits, | |
6252 | * but let's be conservative | |
6253 | */ | |
6254 | verbose(env, "cannot return stack pointer to the caller\n"); | |
6255 | return -EINVAL; | |
6256 | } | |
6257 | ||
6258 | state->curframe--; | |
6259 | caller = state->frame[state->curframe]; | |
69c087ba YS |
6260 | if (callee->in_callback_fn) { |
6261 | /* enforce R0 return value range [0, 1]. */ | |
6262 | struct tnum range = tnum_range(0, 1); | |
6263 | ||
6264 | if (r0->type != SCALAR_VALUE) { | |
6265 | verbose(env, "R0 not a scalar value\n"); | |
6266 | return -EACCES; | |
6267 | } | |
6268 | if (!tnum_in(range, r0->var_off)) { | |
6269 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
6270 | return -EINVAL; | |
6271 | } | |
6272 | } else { | |
6273 | /* return to the caller whatever r0 had in the callee */ | |
6274 | caller->regs[BPF_REG_0] = *r0; | |
6275 | } | |
f4d7e40a | 6276 | |
fd978bf7 | 6277 | /* Transfer references to the caller */ |
c69431aa | 6278 | err = copy_reference_state(caller, callee); |
fd978bf7 JS |
6279 | if (err) |
6280 | return err; | |
6281 | ||
f4d7e40a | 6282 | *insn_idx = callee->callsite + 1; |
06ee7115 | 6283 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 6284 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 6285 | print_verifier_state(env, callee, true); |
f4d7e40a | 6286 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 6287 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
6288 | } |
6289 | /* clear everything in the callee */ | |
6290 | free_func_state(callee); | |
6291 | state->frame[state->curframe + 1] = NULL; | |
6292 | return 0; | |
6293 | } | |
6294 | ||
849fa506 YS |
6295 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
6296 | int func_id, | |
6297 | struct bpf_call_arg_meta *meta) | |
6298 | { | |
6299 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
6300 | ||
6301 | if (ret_type != RET_INTEGER || | |
6302 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 6303 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
6304 | func_id != BPF_FUNC_probe_read_str && |
6305 | func_id != BPF_FUNC_probe_read_kernel_str && | |
6306 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
6307 | return; |
6308 | ||
10060503 | 6309 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 6310 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
6311 | ret_reg->smin_value = -MAX_ERRNO; |
6312 | ret_reg->s32_min_value = -MAX_ERRNO; | |
849fa506 YS |
6313 | __reg_deduce_bounds(ret_reg); |
6314 | __reg_bound_offset(ret_reg); | |
10060503 | 6315 | __update_reg_bounds(ret_reg); |
849fa506 YS |
6316 | } |
6317 | ||
c93552c4 DB |
6318 | static int |
6319 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
6320 | int func_id, int insn_idx) | |
6321 | { | |
6322 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 6323 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
6324 | |
6325 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
6326 | func_id != BPF_FUNC_map_lookup_elem && |
6327 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
6328 | func_id != BPF_FUNC_map_delete_elem && |
6329 | func_id != BPF_FUNC_map_push_elem && | |
6330 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 6331 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f BT |
6332 | func_id != BPF_FUNC_for_each_map_elem && |
6333 | func_id != BPF_FUNC_redirect_map) | |
c93552c4 | 6334 | return 0; |
09772d92 | 6335 | |
591fe988 | 6336 | if (map == NULL) { |
c93552c4 DB |
6337 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
6338 | return -EINVAL; | |
6339 | } | |
6340 | ||
591fe988 DB |
6341 | /* In case of read-only, some additional restrictions |
6342 | * need to be applied in order to prevent altering the | |
6343 | * state of the map from program side. | |
6344 | */ | |
6345 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
6346 | (func_id == BPF_FUNC_map_delete_elem || | |
6347 | func_id == BPF_FUNC_map_update_elem || | |
6348 | func_id == BPF_FUNC_map_push_elem || | |
6349 | func_id == BPF_FUNC_map_pop_elem)) { | |
6350 | verbose(env, "write into map forbidden\n"); | |
6351 | return -EACCES; | |
6352 | } | |
6353 | ||
d2e4c1e6 | 6354 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 6355 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 6356 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 6357 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 6358 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 6359 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
6360 | return 0; |
6361 | } | |
6362 | ||
d2e4c1e6 DB |
6363 | static int |
6364 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
6365 | int func_id, int insn_idx) | |
6366 | { | |
6367 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
6368 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
6369 | struct bpf_map *map = meta->map_ptr; | |
6370 | struct tnum range; | |
6371 | u64 val; | |
cc52d914 | 6372 | int err; |
d2e4c1e6 DB |
6373 | |
6374 | if (func_id != BPF_FUNC_tail_call) | |
6375 | return 0; | |
6376 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
6377 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
6378 | return -EINVAL; | |
6379 | } | |
6380 | ||
6381 | range = tnum_range(0, map->max_entries - 1); | |
6382 | reg = ®s[BPF_REG_3]; | |
6383 | ||
6384 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
6385 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
6386 | return 0; | |
6387 | } | |
6388 | ||
cc52d914 DB |
6389 | err = mark_chain_precision(env, BPF_REG_3); |
6390 | if (err) | |
6391 | return err; | |
6392 | ||
d2e4c1e6 DB |
6393 | val = reg->var_off.value; |
6394 | if (bpf_map_key_unseen(aux)) | |
6395 | bpf_map_key_store(aux, val); | |
6396 | else if (!bpf_map_key_poisoned(aux) && | |
6397 | bpf_map_key_immediate(aux) != val) | |
6398 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
6399 | return 0; | |
6400 | } | |
6401 | ||
fd978bf7 JS |
6402 | static int check_reference_leak(struct bpf_verifier_env *env) |
6403 | { | |
6404 | struct bpf_func_state *state = cur_func(env); | |
6405 | int i; | |
6406 | ||
6407 | for (i = 0; i < state->acquired_refs; i++) { | |
6408 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
6409 | state->refs[i].id, state->refs[i].insn_idx); | |
6410 | } | |
6411 | return state->acquired_refs ? -EINVAL : 0; | |
6412 | } | |
6413 | ||
7b15523a FR |
6414 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
6415 | struct bpf_reg_state *regs) | |
6416 | { | |
6417 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
6418 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
6419 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
6420 | int err, fmt_map_off, num_args; | |
6421 | u64 fmt_addr; | |
6422 | char *fmt; | |
6423 | ||
6424 | /* data must be an array of u64 */ | |
6425 | if (data_len_reg->var_off.value % 8) | |
6426 | return -EINVAL; | |
6427 | num_args = data_len_reg->var_off.value / 8; | |
6428 | ||
6429 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
6430 | * and map_direct_value_addr is set. | |
6431 | */ | |
6432 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
6433 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
6434 | fmt_map_off); | |
8e8ee109 FR |
6435 | if (err) { |
6436 | verbose(env, "verifier bug\n"); | |
6437 | return -EFAULT; | |
6438 | } | |
7b15523a FR |
6439 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
6440 | ||
6441 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
6442 | * can focus on validating the format specifiers. | |
6443 | */ | |
48cac3f4 | 6444 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
7b15523a FR |
6445 | if (err < 0) |
6446 | verbose(env, "Invalid format string\n"); | |
6447 | ||
6448 | return err; | |
6449 | } | |
6450 | ||
9b99edca JO |
6451 | static int check_get_func_ip(struct bpf_verifier_env *env) |
6452 | { | |
9b99edca JO |
6453 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
6454 | int func_id = BPF_FUNC_get_func_ip; | |
6455 | ||
6456 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 6457 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
6458 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
6459 | func_id_name(func_id), func_id); | |
6460 | return -ENOTSUPP; | |
6461 | } | |
6462 | return 0; | |
9ffd9f3f JO |
6463 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
6464 | return 0; | |
9b99edca JO |
6465 | } |
6466 | ||
6467 | verbose(env, "func %s#%d not supported for program type %d\n", | |
6468 | func_id_name(func_id), func_id, type); | |
6469 | return -ENOTSUPP; | |
6470 | } | |
6471 | ||
69c087ba YS |
6472 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
6473 | int *insn_idx_p) | |
17a52670 | 6474 | { |
17a52670 | 6475 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 6476 | struct bpf_reg_state *regs; |
33ff9823 | 6477 | struct bpf_call_arg_meta meta; |
69c087ba | 6478 | int insn_idx = *insn_idx_p; |
969bf05e | 6479 | bool changes_data; |
69c087ba | 6480 | int i, err, func_id; |
17a52670 AS |
6481 | |
6482 | /* find function prototype */ | |
69c087ba | 6483 | func_id = insn->imm; |
17a52670 | 6484 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
6485 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
6486 | func_id); | |
17a52670 AS |
6487 | return -EINVAL; |
6488 | } | |
6489 | ||
00176a34 | 6490 | if (env->ops->get_func_proto) |
5e43f899 | 6491 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 6492 | if (!fn) { |
61bd5218 JK |
6493 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
6494 | func_id); | |
17a52670 AS |
6495 | return -EINVAL; |
6496 | } | |
6497 | ||
6498 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 6499 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 6500 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
6501 | return -EINVAL; |
6502 | } | |
6503 | ||
eae2e83e JO |
6504 | if (fn->allowed && !fn->allowed(env->prog)) { |
6505 | verbose(env, "helper call is not allowed in probe\n"); | |
6506 | return -EINVAL; | |
6507 | } | |
6508 | ||
04514d13 | 6509 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 6510 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
6511 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
6512 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
6513 | func_id_name(func_id), func_id); | |
6514 | return -EINVAL; | |
6515 | } | |
969bf05e | 6516 | |
33ff9823 | 6517 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 6518 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 6519 | |
1b986589 | 6520 | err = check_func_proto(fn, func_id); |
435faee1 | 6521 | if (err) { |
61bd5218 | 6522 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 6523 | func_id_name(func_id), func_id); |
435faee1 DB |
6524 | return err; |
6525 | } | |
6526 | ||
d83525ca | 6527 | meta.func_id = func_id; |
17a52670 | 6528 | /* check args */ |
523a4cf4 | 6529 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
af7ec138 | 6530 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
6531 | if (err) |
6532 | return err; | |
6533 | } | |
17a52670 | 6534 | |
c93552c4 DB |
6535 | err = record_func_map(env, &meta, func_id, insn_idx); |
6536 | if (err) | |
6537 | return err; | |
6538 | ||
d2e4c1e6 DB |
6539 | err = record_func_key(env, &meta, func_id, insn_idx); |
6540 | if (err) | |
6541 | return err; | |
6542 | ||
435faee1 DB |
6543 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
6544 | * is inferred from register state. | |
6545 | */ | |
6546 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
6547 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
6548 | BPF_WRITE, -1, false); | |
435faee1 DB |
6549 | if (err) |
6550 | return err; | |
6551 | } | |
6552 | ||
e6f2dd0f | 6553 | if (is_release_function(func_id)) { |
1b986589 | 6554 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
6555 | if (err) { |
6556 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
6557 | func_id_name(func_id), func_id); | |
fd978bf7 | 6558 | return err; |
46f8bc92 | 6559 | } |
fd978bf7 JS |
6560 | } |
6561 | ||
638f5b90 | 6562 | regs = cur_regs(env); |
cd339431 | 6563 | |
e6f2dd0f JK |
6564 | switch (func_id) { |
6565 | case BPF_FUNC_tail_call: | |
6566 | err = check_reference_leak(env); | |
6567 | if (err) { | |
6568 | verbose(env, "tail_call would lead to reference leak\n"); | |
6569 | return err; | |
6570 | } | |
6571 | break; | |
6572 | case BPF_FUNC_get_local_storage: | |
6573 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
6574 | * this is required because get_local_storage() can't return an error. | |
6575 | */ | |
6576 | if (!register_is_null(®s[BPF_REG_2])) { | |
6577 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
6578 | return -EINVAL; | |
6579 | } | |
6580 | break; | |
6581 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
6582 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
6583 | set_map_elem_callback_state); | |
e6f2dd0f JK |
6584 | break; |
6585 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
6586 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
6587 | set_timer_callback_state); | |
e6f2dd0f JK |
6588 | break; |
6589 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
6590 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
6591 | set_find_vma_callback_state); | |
e6f2dd0f JK |
6592 | break; |
6593 | case BPF_FUNC_snprintf: | |
7b15523a | 6594 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
6595 | break; |
6596 | case BPF_FUNC_loop: | |
6597 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
6598 | set_loop_callback_state); | |
6599 | break; | |
7b15523a FR |
6600 | } |
6601 | ||
e6f2dd0f JK |
6602 | if (err) |
6603 | return err; | |
6604 | ||
17a52670 | 6605 | /* reset caller saved regs */ |
dc503a8a | 6606 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 6607 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
6608 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
6609 | } | |
17a52670 | 6610 | |
5327ed3d JW |
6611 | /* helper call returns 64-bit value. */ |
6612 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
6613 | ||
dc503a8a | 6614 | /* update return register (already marked as written above) */ |
17a52670 | 6615 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 6616 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 6617 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
6618 | } else if (fn->ret_type == RET_VOID) { |
6619 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
6620 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
6621 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 6622 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 6623 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
6624 | /* remember map_ptr, so that check_map_access() |
6625 | * can check 'value_size' boundary of memory access | |
6626 | * to map element returned from bpf_map_lookup_elem() | |
6627 | */ | |
33ff9823 | 6628 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
6629 | verbose(env, |
6630 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
6631 | return -EINVAL; |
6632 | } | |
33ff9823 | 6633 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 6634 | regs[BPF_REG_0].map_uid = meta.map_uid; |
4d31f301 DB |
6635 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
6636 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
6637 | if (map_value_has_spin_lock(meta.map_ptr)) |
6638 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
6639 | } else { |
6640 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
4d31f301 | 6641 | } |
c64b7983 JS |
6642 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
6643 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6644 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
85a51f8c LB |
6645 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
6646 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6647 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
655a51e5 MKL |
6648 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
6649 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6650 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
457f4436 AN |
6651 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
6652 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
6653 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; | |
457f4436 | 6654 | regs[BPF_REG_0].mem_size = meta.mem_size; |
63d9b80d HL |
6655 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
6656 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { | |
eaa6bcb7 HL |
6657 | const struct btf_type *t; |
6658 | ||
6659 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 6660 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
6661 | if (!btf_type_is_struct(t)) { |
6662 | u32 tsize; | |
6663 | const struct btf_type *ret; | |
6664 | const char *tname; | |
6665 | ||
6666 | /* resolve the type size of ksym. */ | |
22dc4a0f | 6667 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 6668 | if (IS_ERR(ret)) { |
22dc4a0f | 6669 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
6670 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
6671 | tname, PTR_ERR(ret)); | |
6672 | return -EINVAL; | |
6673 | } | |
63d9b80d HL |
6674 | regs[BPF_REG_0].type = |
6675 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
6676 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; | |
eaa6bcb7 HL |
6677 | regs[BPF_REG_0].mem_size = tsize; |
6678 | } else { | |
63d9b80d HL |
6679 | regs[BPF_REG_0].type = |
6680 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
6681 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; | |
22dc4a0f | 6682 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
6683 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
6684 | } | |
3ca1032a KS |
6685 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL || |
6686 | fn->ret_type == RET_PTR_TO_BTF_ID) { | |
af7ec138 YS |
6687 | int ret_btf_id; |
6688 | ||
6689 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
3ca1032a KS |
6690 | regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ? |
6691 | PTR_TO_BTF_ID : | |
6692 | PTR_TO_BTF_ID_OR_NULL; | |
af7ec138 YS |
6693 | ret_btf_id = *fn->ret_btf_id; |
6694 | if (ret_btf_id == 0) { | |
6695 | verbose(env, "invalid return type %d of func %s#%d\n", | |
6696 | fn->ret_type, func_id_name(func_id), func_id); | |
6697 | return -EINVAL; | |
6698 | } | |
22dc4a0f AN |
6699 | /* current BPF helper definitions are only coming from |
6700 | * built-in code with type IDs from vmlinux BTF | |
6701 | */ | |
6702 | regs[BPF_REG_0].btf = btf_vmlinux; | |
af7ec138 | 6703 | regs[BPF_REG_0].btf_id = ret_btf_id; |
17a52670 | 6704 | } else { |
61bd5218 | 6705 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 6706 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
6707 | return -EINVAL; |
6708 | } | |
04fd61ab | 6709 | |
93c230e3 MKL |
6710 | if (reg_type_may_be_null(regs[BPF_REG_0].type)) |
6711 | regs[BPF_REG_0].id = ++env->id_gen; | |
6712 | ||
0f3adc28 | 6713 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
6714 | /* For release_reference() */ |
6715 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 6716 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
6717 | int id = acquire_reference_state(env, insn_idx); |
6718 | ||
6719 | if (id < 0) | |
6720 | return id; | |
6721 | /* For mark_ptr_or_null_reg() */ | |
6722 | regs[BPF_REG_0].id = id; | |
6723 | /* For release_reference() */ | |
6724 | regs[BPF_REG_0].ref_obj_id = id; | |
6725 | } | |
1b986589 | 6726 | |
849fa506 YS |
6727 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
6728 | ||
61bd5218 | 6729 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
6730 | if (err) |
6731 | return err; | |
04fd61ab | 6732 | |
fa28dcb8 SL |
6733 | if ((func_id == BPF_FUNC_get_stack || |
6734 | func_id == BPF_FUNC_get_task_stack) && | |
6735 | !env->prog->has_callchain_buf) { | |
c195651e YS |
6736 | const char *err_str; |
6737 | ||
6738 | #ifdef CONFIG_PERF_EVENTS | |
6739 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
6740 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
6741 | #else | |
6742 | err = -ENOTSUPP; | |
6743 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
6744 | #endif | |
6745 | if (err) { | |
6746 | verbose(env, err_str, func_id_name(func_id), func_id); | |
6747 | return err; | |
6748 | } | |
6749 | ||
6750 | env->prog->has_callchain_buf = true; | |
6751 | } | |
6752 | ||
5d99cb2c SL |
6753 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
6754 | env->prog->call_get_stack = true; | |
6755 | ||
9b99edca JO |
6756 | if (func_id == BPF_FUNC_get_func_ip) { |
6757 | if (check_get_func_ip(env)) | |
6758 | return -ENOTSUPP; | |
6759 | env->prog->call_get_func_ip = true; | |
6760 | } | |
6761 | ||
969bf05e AS |
6762 | if (changes_data) |
6763 | clear_all_pkt_pointers(env); | |
6764 | return 0; | |
6765 | } | |
6766 | ||
e6ac2450 MKL |
6767 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
6768 | * the BTF func_proto's return value size and argument. | |
6769 | */ | |
6770 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
6771 | size_t reg_size) | |
6772 | { | |
6773 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
6774 | ||
6775 | if (regno == BPF_REG_0) { | |
6776 | /* Function return value */ | |
6777 | reg->live |= REG_LIVE_WRITTEN; | |
6778 | reg->subreg_def = reg_size == sizeof(u64) ? | |
6779 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
6780 | } else { | |
6781 | /* Function argument */ | |
6782 | if (reg_size == sizeof(u64)) { | |
6783 | mark_insn_zext(env, reg); | |
6784 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
6785 | } else { | |
6786 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
6787 | } | |
6788 | } | |
6789 | } | |
6790 | ||
6791 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
6792 | { | |
6793 | const struct btf_type *t, *func, *func_proto, *ptr_type; | |
6794 | struct bpf_reg_state *regs = cur_regs(env); | |
6795 | const char *func_name, *ptr_type_name; | |
6796 | u32 i, nargs, func_id, ptr_type_id; | |
2357672c | 6797 | struct module *btf_mod = NULL; |
e6ac2450 | 6798 | const struct btf_param *args; |
2357672c | 6799 | struct btf *desc_btf; |
e6ac2450 MKL |
6800 | int err; |
6801 | ||
a5d82727 KKD |
6802 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
6803 | if (!insn->imm) | |
6804 | return 0; | |
6805 | ||
2357672c KKD |
6806 | desc_btf = find_kfunc_desc_btf(env, insn->imm, insn->off, &btf_mod); |
6807 | if (IS_ERR(desc_btf)) | |
6808 | return PTR_ERR(desc_btf); | |
6809 | ||
e6ac2450 | 6810 | func_id = insn->imm; |
2357672c KKD |
6811 | func = btf_type_by_id(desc_btf, func_id); |
6812 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
6813 | func_proto = btf_type_by_id(desc_btf, func->type); | |
e6ac2450 MKL |
6814 | |
6815 | if (!env->ops->check_kfunc_call || | |
2357672c | 6816 | !env->ops->check_kfunc_call(func_id, btf_mod)) { |
e6ac2450 MKL |
6817 | verbose(env, "calling kernel function %s is not allowed\n", |
6818 | func_name); | |
6819 | return -EACCES; | |
6820 | } | |
6821 | ||
6822 | /* Check the arguments */ | |
2357672c | 6823 | err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs); |
e6ac2450 MKL |
6824 | if (err) |
6825 | return err; | |
6826 | ||
6827 | for (i = 0; i < CALLER_SAVED_REGS; i++) | |
6828 | mark_reg_not_init(env, regs, caller_saved[i]); | |
6829 | ||
6830 | /* Check return type */ | |
2357672c | 6831 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
e6ac2450 MKL |
6832 | if (btf_type_is_scalar(t)) { |
6833 | mark_reg_unknown(env, regs, BPF_REG_0); | |
6834 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
6835 | } else if (btf_type_is_ptr(t)) { | |
2357672c | 6836 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, |
e6ac2450 MKL |
6837 | &ptr_type_id); |
6838 | if (!btf_type_is_struct(ptr_type)) { | |
2357672c | 6839 | ptr_type_name = btf_name_by_offset(desc_btf, |
e6ac2450 MKL |
6840 | ptr_type->name_off); |
6841 | verbose(env, "kernel function %s returns pointer type %s %s is not supported\n", | |
6842 | func_name, btf_type_str(ptr_type), | |
6843 | ptr_type_name); | |
6844 | return -EINVAL; | |
6845 | } | |
6846 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
2357672c | 6847 | regs[BPF_REG_0].btf = desc_btf; |
e6ac2450 MKL |
6848 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; |
6849 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
6850 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); | |
6851 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ | |
6852 | ||
6853 | nargs = btf_type_vlen(func_proto); | |
6854 | args = (const struct btf_param *)(func_proto + 1); | |
6855 | for (i = 0; i < nargs; i++) { | |
6856 | u32 regno = i + 1; | |
6857 | ||
2357672c | 6858 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
6859 | if (btf_type_is_ptr(t)) |
6860 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
6861 | else | |
6862 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
6863 | mark_btf_func_reg_size(env, regno, t->size); | |
6864 | } | |
6865 | ||
6866 | return 0; | |
6867 | } | |
6868 | ||
b03c9f9f EC |
6869 | static bool signed_add_overflows(s64 a, s64 b) |
6870 | { | |
6871 | /* Do the add in u64, where overflow is well-defined */ | |
6872 | s64 res = (s64)((u64)a + (u64)b); | |
6873 | ||
6874 | if (b < 0) | |
6875 | return res > a; | |
6876 | return res < a; | |
6877 | } | |
6878 | ||
bc895e8b | 6879 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
6880 | { |
6881 | /* Do the add in u32, where overflow is well-defined */ | |
6882 | s32 res = (s32)((u32)a + (u32)b); | |
6883 | ||
6884 | if (b < 0) | |
6885 | return res > a; | |
6886 | return res < a; | |
6887 | } | |
6888 | ||
bc895e8b | 6889 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
6890 | { |
6891 | /* Do the sub in u64, where overflow is well-defined */ | |
6892 | s64 res = (s64)((u64)a - (u64)b); | |
6893 | ||
6894 | if (b < 0) | |
6895 | return res < a; | |
6896 | return res > a; | |
969bf05e AS |
6897 | } |
6898 | ||
3f50f132 JF |
6899 | static bool signed_sub32_overflows(s32 a, s32 b) |
6900 | { | |
bc895e8b | 6901 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
6902 | s32 res = (s32)((u32)a - (u32)b); |
6903 | ||
6904 | if (b < 0) | |
6905 | return res < a; | |
6906 | return res > a; | |
6907 | } | |
6908 | ||
bb7f0f98 AS |
6909 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
6910 | const struct bpf_reg_state *reg, | |
6911 | enum bpf_reg_type type) | |
6912 | { | |
6913 | bool known = tnum_is_const(reg->var_off); | |
6914 | s64 val = reg->var_off.value; | |
6915 | s64 smin = reg->smin_value; | |
6916 | ||
6917 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
6918 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
6919 | reg_type_str[type], val); | |
6920 | return false; | |
6921 | } | |
6922 | ||
6923 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
6924 | verbose(env, "%s pointer offset %d is not allowed\n", | |
6925 | reg_type_str[type], reg->off); | |
6926 | return false; | |
6927 | } | |
6928 | ||
6929 | if (smin == S64_MIN) { | |
6930 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
6931 | reg_type_str[type]); | |
6932 | return false; | |
6933 | } | |
6934 | ||
6935 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
6936 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
6937 | smin, reg_type_str[type]); | |
6938 | return false; | |
6939 | } | |
6940 | ||
6941 | return true; | |
6942 | } | |
6943 | ||
979d63d5 DB |
6944 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
6945 | { | |
6946 | return &env->insn_aux_data[env->insn_idx]; | |
6947 | } | |
6948 | ||
a6aaece0 DB |
6949 | enum { |
6950 | REASON_BOUNDS = -1, | |
6951 | REASON_TYPE = -2, | |
6952 | REASON_PATHS = -3, | |
6953 | REASON_LIMIT = -4, | |
6954 | REASON_STACK = -5, | |
6955 | }; | |
6956 | ||
979d63d5 | 6957 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 6958 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 6959 | { |
7fedb63a | 6960 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
6961 | |
6962 | switch (ptr_reg->type) { | |
6963 | case PTR_TO_STACK: | |
1b1597e6 | 6964 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
6965 | * left direction, see BPF_REG_FP. Also, unknown scalar |
6966 | * offset where we would need to deal with min/max bounds is | |
6967 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
6968 | */ |
6969 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 6970 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 6971 | break; |
979d63d5 | 6972 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 6973 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
6974 | ptr_limit = (mask_to_left ? |
6975 | ptr_reg->smin_value : | |
6976 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 6977 | break; |
979d63d5 | 6978 | default: |
a6aaece0 | 6979 | return REASON_TYPE; |
979d63d5 | 6980 | } |
b658bbb8 DB |
6981 | |
6982 | if (ptr_limit >= max) | |
a6aaece0 | 6983 | return REASON_LIMIT; |
b658bbb8 DB |
6984 | *alu_limit = ptr_limit; |
6985 | return 0; | |
979d63d5 DB |
6986 | } |
6987 | ||
d3bd7413 DB |
6988 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
6989 | const struct bpf_insn *insn) | |
6990 | { | |
2c78ee89 | 6991 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
6992 | } |
6993 | ||
6994 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
6995 | u32 alu_state, u32 alu_limit) | |
6996 | { | |
6997 | /* If we arrived here from different branches with different | |
6998 | * state or limits to sanitize, then this won't work. | |
6999 | */ | |
7000 | if (aux->alu_state && | |
7001 | (aux->alu_state != alu_state || | |
7002 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 7003 | return REASON_PATHS; |
d3bd7413 | 7004 | |
e6ac5933 | 7005 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
7006 | aux->alu_state = alu_state; |
7007 | aux->alu_limit = alu_limit; | |
7008 | return 0; | |
7009 | } | |
7010 | ||
7011 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
7012 | struct bpf_insn *insn) | |
7013 | { | |
7014 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
7015 | ||
7016 | if (can_skip_alu_sanitation(env, insn)) | |
7017 | return 0; | |
7018 | ||
7019 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
7020 | } | |
7021 | ||
f5288193 DB |
7022 | static bool sanitize_needed(u8 opcode) |
7023 | { | |
7024 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
7025 | } | |
7026 | ||
3d0220f6 DB |
7027 | struct bpf_sanitize_info { |
7028 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 7029 | bool mask_to_left; |
3d0220f6 DB |
7030 | }; |
7031 | ||
9183671a DB |
7032 | static struct bpf_verifier_state * |
7033 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
7034 | const struct bpf_insn *insn, | |
7035 | u32 next_idx, u32 curr_idx) | |
7036 | { | |
7037 | struct bpf_verifier_state *branch; | |
7038 | struct bpf_reg_state *regs; | |
7039 | ||
7040 | branch = push_stack(env, next_idx, curr_idx, true); | |
7041 | if (branch && insn) { | |
7042 | regs = branch->frame[branch->curframe]->regs; | |
7043 | if (BPF_SRC(insn->code) == BPF_K) { | |
7044 | mark_reg_unknown(env, regs, insn->dst_reg); | |
7045 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
7046 | mark_reg_unknown(env, regs, insn->dst_reg); | |
7047 | mark_reg_unknown(env, regs, insn->src_reg); | |
7048 | } | |
7049 | } | |
7050 | return branch; | |
7051 | } | |
7052 | ||
979d63d5 DB |
7053 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
7054 | struct bpf_insn *insn, | |
7055 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 7056 | const struct bpf_reg_state *off_reg, |
979d63d5 | 7057 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 7058 | struct bpf_sanitize_info *info, |
7fedb63a | 7059 | const bool commit_window) |
979d63d5 | 7060 | { |
3d0220f6 | 7061 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 7062 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 7063 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 7064 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
7065 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
7066 | u8 opcode = BPF_OP(insn->code); | |
7067 | u32 alu_state, alu_limit; | |
7068 | struct bpf_reg_state tmp; | |
7069 | bool ret; | |
f232326f | 7070 | int err; |
979d63d5 | 7071 | |
d3bd7413 | 7072 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
7073 | return 0; |
7074 | ||
7075 | /* We already marked aux for masking from non-speculative | |
7076 | * paths, thus we got here in the first place. We only care | |
7077 | * to explore bad access from here. | |
7078 | */ | |
7079 | if (vstate->speculative) | |
7080 | goto do_sim; | |
7081 | ||
bb01a1bb DB |
7082 | if (!commit_window) { |
7083 | if (!tnum_is_const(off_reg->var_off) && | |
7084 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
7085 | return REASON_BOUNDS; | |
7086 | ||
7087 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
7088 | (opcode == BPF_SUB && !off_is_neg); | |
7089 | } | |
7090 | ||
7091 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
7092 | if (err < 0) |
7093 | return err; | |
7094 | ||
7fedb63a DB |
7095 | if (commit_window) { |
7096 | /* In commit phase we narrow the masking window based on | |
7097 | * the observed pointer move after the simulated operation. | |
7098 | */ | |
3d0220f6 DB |
7099 | alu_state = info->aux.alu_state; |
7100 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
7101 | } else { |
7102 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 7103 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
7104 | alu_state |= ptr_is_dst_reg ? |
7105 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
7106 | |
7107 | /* Limit pruning on unknown scalars to enable deep search for | |
7108 | * potential masking differences from other program paths. | |
7109 | */ | |
7110 | if (!off_is_imm) | |
7111 | env->explore_alu_limits = true; | |
7fedb63a DB |
7112 | } |
7113 | ||
f232326f PK |
7114 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
7115 | if (err < 0) | |
7116 | return err; | |
979d63d5 | 7117 | do_sim: |
7fedb63a DB |
7118 | /* If we're in commit phase, we're done here given we already |
7119 | * pushed the truncated dst_reg into the speculative verification | |
7120 | * stack. | |
a7036191 DB |
7121 | * |
7122 | * Also, when register is a known constant, we rewrite register-based | |
7123 | * operation to immediate-based, and thus do not need masking (and as | |
7124 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 7125 | */ |
a7036191 | 7126 | if (commit_window || off_is_imm) |
7fedb63a DB |
7127 | return 0; |
7128 | ||
979d63d5 DB |
7129 | /* Simulate and find potential out-of-bounds access under |
7130 | * speculative execution from truncation as a result of | |
7131 | * masking when off was not within expected range. If off | |
7132 | * sits in dst, then we temporarily need to move ptr there | |
7133 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
7134 | * for cases where we use K-based arithmetic in one direction | |
7135 | * and truncated reg-based in the other in order to explore | |
7136 | * bad access. | |
7137 | */ | |
7138 | if (!ptr_is_dst_reg) { | |
7139 | tmp = *dst_reg; | |
7140 | *dst_reg = *ptr_reg; | |
7141 | } | |
9183671a DB |
7142 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
7143 | env->insn_idx); | |
0803278b | 7144 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 7145 | *dst_reg = tmp; |
a6aaece0 DB |
7146 | return !ret ? REASON_STACK : 0; |
7147 | } | |
7148 | ||
fe9a5ca7 DB |
7149 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
7150 | { | |
7151 | struct bpf_verifier_state *vstate = env->cur_state; | |
7152 | ||
7153 | /* If we simulate paths under speculation, we don't update the | |
7154 | * insn as 'seen' such that when we verify unreachable paths in | |
7155 | * the non-speculative domain, sanitize_dead_code() can still | |
7156 | * rewrite/sanitize them. | |
7157 | */ | |
7158 | if (!vstate->speculative) | |
7159 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
7160 | } | |
7161 | ||
a6aaece0 DB |
7162 | static int sanitize_err(struct bpf_verifier_env *env, |
7163 | const struct bpf_insn *insn, int reason, | |
7164 | const struct bpf_reg_state *off_reg, | |
7165 | const struct bpf_reg_state *dst_reg) | |
7166 | { | |
7167 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
7168 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
7169 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
7170 | ||
7171 | switch (reason) { | |
7172 | case REASON_BOUNDS: | |
7173 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
7174 | off_reg == dst_reg ? dst : src, err); | |
7175 | break; | |
7176 | case REASON_TYPE: | |
7177 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
7178 | off_reg == dst_reg ? src : dst, err); | |
7179 | break; | |
7180 | case REASON_PATHS: | |
7181 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
7182 | dst, op, err); | |
7183 | break; | |
7184 | case REASON_LIMIT: | |
7185 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
7186 | dst, op, err); | |
7187 | break; | |
7188 | case REASON_STACK: | |
7189 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
7190 | dst, err); | |
7191 | break; | |
7192 | default: | |
7193 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
7194 | reason); | |
7195 | break; | |
7196 | } | |
7197 | ||
7198 | return -EACCES; | |
979d63d5 DB |
7199 | } |
7200 | ||
01f810ac AM |
7201 | /* check that stack access falls within stack limits and that 'reg' doesn't |
7202 | * have a variable offset. | |
7203 | * | |
7204 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
7205 | * requires corresponding support in Spectre masking for stack ALU. See also | |
7206 | * retrieve_ptr_limit(). | |
7207 | * | |
7208 | * | |
7209 | * 'off' includes 'reg->off'. | |
7210 | */ | |
7211 | static int check_stack_access_for_ptr_arithmetic( | |
7212 | struct bpf_verifier_env *env, | |
7213 | int regno, | |
7214 | const struct bpf_reg_state *reg, | |
7215 | int off) | |
7216 | { | |
7217 | if (!tnum_is_const(reg->var_off)) { | |
7218 | char tn_buf[48]; | |
7219 | ||
7220 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
7221 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
7222 | regno, tn_buf, off); | |
7223 | return -EACCES; | |
7224 | } | |
7225 | ||
7226 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
7227 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
7228 | "prohibited for !root; off=%d\n", regno, off); | |
7229 | return -EACCES; | |
7230 | } | |
7231 | ||
7232 | return 0; | |
7233 | } | |
7234 | ||
073815b7 DB |
7235 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
7236 | const struct bpf_insn *insn, | |
7237 | const struct bpf_reg_state *dst_reg) | |
7238 | { | |
7239 | u32 dst = insn->dst_reg; | |
7240 | ||
7241 | /* For unprivileged we require that resulting offset must be in bounds | |
7242 | * in order to be able to sanitize access later on. | |
7243 | */ | |
7244 | if (env->bypass_spec_v1) | |
7245 | return 0; | |
7246 | ||
7247 | switch (dst_reg->type) { | |
7248 | case PTR_TO_STACK: | |
7249 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
7250 | dst_reg->off + dst_reg->var_off.value)) | |
7251 | return -EACCES; | |
7252 | break; | |
7253 | case PTR_TO_MAP_VALUE: | |
7254 | if (check_map_access(env, dst, dst_reg->off, 1, false)) { | |
7255 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
7256 | "prohibited for !root\n", dst); | |
7257 | return -EACCES; | |
7258 | } | |
7259 | break; | |
7260 | default: | |
7261 | break; | |
7262 | } | |
7263 | ||
7264 | return 0; | |
7265 | } | |
01f810ac | 7266 | |
f1174f77 | 7267 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
7268 | * Caller should also handle BPF_MOV case separately. |
7269 | * If we return -EACCES, caller may want to try again treating pointer as a | |
7270 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
7271 | */ | |
7272 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
7273 | struct bpf_insn *insn, | |
7274 | const struct bpf_reg_state *ptr_reg, | |
7275 | const struct bpf_reg_state *off_reg) | |
969bf05e | 7276 | { |
f4d7e40a AS |
7277 | struct bpf_verifier_state *vstate = env->cur_state; |
7278 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
7279 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 7280 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
7281 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
7282 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
7283 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
7284 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 7285 | struct bpf_sanitize_info info = {}; |
969bf05e | 7286 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 7287 | u32 dst = insn->dst_reg; |
979d63d5 | 7288 | int ret; |
969bf05e | 7289 | |
f1174f77 | 7290 | dst_reg = ®s[dst]; |
969bf05e | 7291 | |
6f16101e DB |
7292 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
7293 | smin_val > smax_val || umin_val > umax_val) { | |
7294 | /* Taint dst register if offset had invalid bounds derived from | |
7295 | * e.g. dead branches. | |
7296 | */ | |
f54c7898 | 7297 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 7298 | return 0; |
f1174f77 EC |
7299 | } |
7300 | ||
7301 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
7302 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
7303 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
7304 | __mark_reg_unknown(env, dst_reg); | |
7305 | return 0; | |
7306 | } | |
7307 | ||
82abbf8d AS |
7308 | verbose(env, |
7309 | "R%d 32-bit pointer arithmetic prohibited\n", | |
7310 | dst); | |
f1174f77 | 7311 | return -EACCES; |
969bf05e AS |
7312 | } |
7313 | ||
aad2eeaf JS |
7314 | switch (ptr_reg->type) { |
7315 | case PTR_TO_MAP_VALUE_OR_NULL: | |
7316 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
7317 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 7318 | return -EACCES; |
aad2eeaf | 7319 | case CONST_PTR_TO_MAP: |
7c696732 YS |
7320 | /* smin_val represents the known value */ |
7321 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
7322 | break; | |
8731745e | 7323 | fallthrough; |
aad2eeaf | 7324 | case PTR_TO_PACKET_END: |
c64b7983 JS |
7325 | case PTR_TO_SOCKET: |
7326 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
7327 | case PTR_TO_SOCK_COMMON: |
7328 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
7329 | case PTR_TO_TCP_SOCK: |
7330 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 7331 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
7332 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
7333 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 7334 | return -EACCES; |
aad2eeaf JS |
7335 | default: |
7336 | break; | |
f1174f77 EC |
7337 | } |
7338 | ||
7339 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
7340 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 7341 | */ |
f1174f77 EC |
7342 | dst_reg->type = ptr_reg->type; |
7343 | dst_reg->id = ptr_reg->id; | |
969bf05e | 7344 | |
bb7f0f98 AS |
7345 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
7346 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
7347 | return -EINVAL; | |
7348 | ||
3f50f132 JF |
7349 | /* pointer types do not carry 32-bit bounds at the moment. */ |
7350 | __mark_reg32_unbounded(dst_reg); | |
7351 | ||
7fedb63a DB |
7352 | if (sanitize_needed(opcode)) { |
7353 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 7354 | &info, false); |
a6aaece0 DB |
7355 | if (ret < 0) |
7356 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 7357 | } |
a6aaece0 | 7358 | |
f1174f77 EC |
7359 | switch (opcode) { |
7360 | case BPF_ADD: | |
7361 | /* We can take a fixed offset as long as it doesn't overflow | |
7362 | * the s32 'off' field | |
969bf05e | 7363 | */ |
b03c9f9f EC |
7364 | if (known && (ptr_reg->off + smin_val == |
7365 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 7366 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
7367 | dst_reg->smin_value = smin_ptr; |
7368 | dst_reg->smax_value = smax_ptr; | |
7369 | dst_reg->umin_value = umin_ptr; | |
7370 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 7371 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 7372 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 7373 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
7374 | break; |
7375 | } | |
f1174f77 EC |
7376 | /* A new variable offset is created. Note that off_reg->off |
7377 | * == 0, since it's a scalar. | |
7378 | * dst_reg gets the pointer type and since some positive | |
7379 | * integer value was added to the pointer, give it a new 'id' | |
7380 | * if it's a PTR_TO_PACKET. | |
7381 | * this creates a new 'base' pointer, off_reg (variable) gets | |
7382 | * added into the variable offset, and we copy the fixed offset | |
7383 | * from ptr_reg. | |
969bf05e | 7384 | */ |
b03c9f9f EC |
7385 | if (signed_add_overflows(smin_ptr, smin_val) || |
7386 | signed_add_overflows(smax_ptr, smax_val)) { | |
7387 | dst_reg->smin_value = S64_MIN; | |
7388 | dst_reg->smax_value = S64_MAX; | |
7389 | } else { | |
7390 | dst_reg->smin_value = smin_ptr + smin_val; | |
7391 | dst_reg->smax_value = smax_ptr + smax_val; | |
7392 | } | |
7393 | if (umin_ptr + umin_val < umin_ptr || | |
7394 | umax_ptr + umax_val < umax_ptr) { | |
7395 | dst_reg->umin_value = 0; | |
7396 | dst_reg->umax_value = U64_MAX; | |
7397 | } else { | |
7398 | dst_reg->umin_value = umin_ptr + umin_val; | |
7399 | dst_reg->umax_value = umax_ptr + umax_val; | |
7400 | } | |
f1174f77 EC |
7401 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
7402 | dst_reg->off = ptr_reg->off; | |
0962590e | 7403 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 7404 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
7405 | dst_reg->id = ++env->id_gen; |
7406 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 7407 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
7408 | } |
7409 | break; | |
7410 | case BPF_SUB: | |
7411 | if (dst_reg == off_reg) { | |
7412 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
7413 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
7414 | dst); | |
f1174f77 EC |
7415 | return -EACCES; |
7416 | } | |
7417 | /* We don't allow subtraction from FP, because (according to | |
7418 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
7419 | * be able to deal with it. | |
969bf05e | 7420 | */ |
f1174f77 | 7421 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
7422 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
7423 | dst); | |
f1174f77 EC |
7424 | return -EACCES; |
7425 | } | |
b03c9f9f EC |
7426 | if (known && (ptr_reg->off - smin_val == |
7427 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 7428 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
7429 | dst_reg->smin_value = smin_ptr; |
7430 | dst_reg->smax_value = smax_ptr; | |
7431 | dst_reg->umin_value = umin_ptr; | |
7432 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
7433 | dst_reg->var_off = ptr_reg->var_off; |
7434 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 7435 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 7436 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
7437 | break; |
7438 | } | |
f1174f77 EC |
7439 | /* A new variable offset is created. If the subtrahend is known |
7440 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 7441 | */ |
b03c9f9f EC |
7442 | if (signed_sub_overflows(smin_ptr, smax_val) || |
7443 | signed_sub_overflows(smax_ptr, smin_val)) { | |
7444 | /* Overflow possible, we know nothing */ | |
7445 | dst_reg->smin_value = S64_MIN; | |
7446 | dst_reg->smax_value = S64_MAX; | |
7447 | } else { | |
7448 | dst_reg->smin_value = smin_ptr - smax_val; | |
7449 | dst_reg->smax_value = smax_ptr - smin_val; | |
7450 | } | |
7451 | if (umin_ptr < umax_val) { | |
7452 | /* Overflow possible, we know nothing */ | |
7453 | dst_reg->umin_value = 0; | |
7454 | dst_reg->umax_value = U64_MAX; | |
7455 | } else { | |
7456 | /* Cannot overflow (as long as bounds are consistent) */ | |
7457 | dst_reg->umin_value = umin_ptr - umax_val; | |
7458 | dst_reg->umax_value = umax_ptr - umin_val; | |
7459 | } | |
f1174f77 EC |
7460 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
7461 | dst_reg->off = ptr_reg->off; | |
0962590e | 7462 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 7463 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
7464 | dst_reg->id = ++env->id_gen; |
7465 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 7466 | if (smin_val < 0) |
22dc4a0f | 7467 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 7468 | } |
f1174f77 EC |
7469 | break; |
7470 | case BPF_AND: | |
7471 | case BPF_OR: | |
7472 | case BPF_XOR: | |
82abbf8d AS |
7473 | /* bitwise ops on pointers are troublesome, prohibit. */ |
7474 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
7475 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
7476 | return -EACCES; |
7477 | default: | |
7478 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
7479 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
7480 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 7481 | return -EACCES; |
43188702 JF |
7482 | } |
7483 | ||
bb7f0f98 AS |
7484 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
7485 | return -EINVAL; | |
7486 | ||
b03c9f9f EC |
7487 | __update_reg_bounds(dst_reg); |
7488 | __reg_deduce_bounds(dst_reg); | |
7489 | __reg_bound_offset(dst_reg); | |
0d6303db | 7490 | |
073815b7 DB |
7491 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
7492 | return -EACCES; | |
7fedb63a DB |
7493 | if (sanitize_needed(opcode)) { |
7494 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 7495 | &info, true); |
7fedb63a DB |
7496 | if (ret < 0) |
7497 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
7498 | } |
7499 | ||
43188702 JF |
7500 | return 0; |
7501 | } | |
7502 | ||
3f50f132 JF |
7503 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
7504 | struct bpf_reg_state *src_reg) | |
7505 | { | |
7506 | s32 smin_val = src_reg->s32_min_value; | |
7507 | s32 smax_val = src_reg->s32_max_value; | |
7508 | u32 umin_val = src_reg->u32_min_value; | |
7509 | u32 umax_val = src_reg->u32_max_value; | |
7510 | ||
7511 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
7512 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
7513 | dst_reg->s32_min_value = S32_MIN; | |
7514 | dst_reg->s32_max_value = S32_MAX; | |
7515 | } else { | |
7516 | dst_reg->s32_min_value += smin_val; | |
7517 | dst_reg->s32_max_value += smax_val; | |
7518 | } | |
7519 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
7520 | dst_reg->u32_max_value + umax_val < umax_val) { | |
7521 | dst_reg->u32_min_value = 0; | |
7522 | dst_reg->u32_max_value = U32_MAX; | |
7523 | } else { | |
7524 | dst_reg->u32_min_value += umin_val; | |
7525 | dst_reg->u32_max_value += umax_val; | |
7526 | } | |
7527 | } | |
7528 | ||
07cd2631 JF |
7529 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
7530 | struct bpf_reg_state *src_reg) | |
7531 | { | |
7532 | s64 smin_val = src_reg->smin_value; | |
7533 | s64 smax_val = src_reg->smax_value; | |
7534 | u64 umin_val = src_reg->umin_value; | |
7535 | u64 umax_val = src_reg->umax_value; | |
7536 | ||
7537 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
7538 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
7539 | dst_reg->smin_value = S64_MIN; | |
7540 | dst_reg->smax_value = S64_MAX; | |
7541 | } else { | |
7542 | dst_reg->smin_value += smin_val; | |
7543 | dst_reg->smax_value += smax_val; | |
7544 | } | |
7545 | if (dst_reg->umin_value + umin_val < umin_val || | |
7546 | dst_reg->umax_value + umax_val < umax_val) { | |
7547 | dst_reg->umin_value = 0; | |
7548 | dst_reg->umax_value = U64_MAX; | |
7549 | } else { | |
7550 | dst_reg->umin_value += umin_val; | |
7551 | dst_reg->umax_value += umax_val; | |
7552 | } | |
3f50f132 JF |
7553 | } |
7554 | ||
7555 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
7556 | struct bpf_reg_state *src_reg) | |
7557 | { | |
7558 | s32 smin_val = src_reg->s32_min_value; | |
7559 | s32 smax_val = src_reg->s32_max_value; | |
7560 | u32 umin_val = src_reg->u32_min_value; | |
7561 | u32 umax_val = src_reg->u32_max_value; | |
7562 | ||
7563 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
7564 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
7565 | /* Overflow possible, we know nothing */ | |
7566 | dst_reg->s32_min_value = S32_MIN; | |
7567 | dst_reg->s32_max_value = S32_MAX; | |
7568 | } else { | |
7569 | dst_reg->s32_min_value -= smax_val; | |
7570 | dst_reg->s32_max_value -= smin_val; | |
7571 | } | |
7572 | if (dst_reg->u32_min_value < umax_val) { | |
7573 | /* Overflow possible, we know nothing */ | |
7574 | dst_reg->u32_min_value = 0; | |
7575 | dst_reg->u32_max_value = U32_MAX; | |
7576 | } else { | |
7577 | /* Cannot overflow (as long as bounds are consistent) */ | |
7578 | dst_reg->u32_min_value -= umax_val; | |
7579 | dst_reg->u32_max_value -= umin_val; | |
7580 | } | |
07cd2631 JF |
7581 | } |
7582 | ||
7583 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
7584 | struct bpf_reg_state *src_reg) | |
7585 | { | |
7586 | s64 smin_val = src_reg->smin_value; | |
7587 | s64 smax_val = src_reg->smax_value; | |
7588 | u64 umin_val = src_reg->umin_value; | |
7589 | u64 umax_val = src_reg->umax_value; | |
7590 | ||
7591 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
7592 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
7593 | /* Overflow possible, we know nothing */ | |
7594 | dst_reg->smin_value = S64_MIN; | |
7595 | dst_reg->smax_value = S64_MAX; | |
7596 | } else { | |
7597 | dst_reg->smin_value -= smax_val; | |
7598 | dst_reg->smax_value -= smin_val; | |
7599 | } | |
7600 | if (dst_reg->umin_value < umax_val) { | |
7601 | /* Overflow possible, we know nothing */ | |
7602 | dst_reg->umin_value = 0; | |
7603 | dst_reg->umax_value = U64_MAX; | |
7604 | } else { | |
7605 | /* Cannot overflow (as long as bounds are consistent) */ | |
7606 | dst_reg->umin_value -= umax_val; | |
7607 | dst_reg->umax_value -= umin_val; | |
7608 | } | |
3f50f132 JF |
7609 | } |
7610 | ||
7611 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
7612 | struct bpf_reg_state *src_reg) | |
7613 | { | |
7614 | s32 smin_val = src_reg->s32_min_value; | |
7615 | u32 umin_val = src_reg->u32_min_value; | |
7616 | u32 umax_val = src_reg->u32_max_value; | |
7617 | ||
7618 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
7619 | /* Ain't nobody got time to multiply that sign */ | |
7620 | __mark_reg32_unbounded(dst_reg); | |
7621 | return; | |
7622 | } | |
7623 | /* Both values are positive, so we can work with unsigned and | |
7624 | * copy the result to signed (unless it exceeds S32_MAX). | |
7625 | */ | |
7626 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
7627 | /* Potential overflow, we know nothing */ | |
7628 | __mark_reg32_unbounded(dst_reg); | |
7629 | return; | |
7630 | } | |
7631 | dst_reg->u32_min_value *= umin_val; | |
7632 | dst_reg->u32_max_value *= umax_val; | |
7633 | if (dst_reg->u32_max_value > S32_MAX) { | |
7634 | /* Overflow possible, we know nothing */ | |
7635 | dst_reg->s32_min_value = S32_MIN; | |
7636 | dst_reg->s32_max_value = S32_MAX; | |
7637 | } else { | |
7638 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7639 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7640 | } | |
07cd2631 JF |
7641 | } |
7642 | ||
7643 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
7644 | struct bpf_reg_state *src_reg) | |
7645 | { | |
7646 | s64 smin_val = src_reg->smin_value; | |
7647 | u64 umin_val = src_reg->umin_value; | |
7648 | u64 umax_val = src_reg->umax_value; | |
7649 | ||
07cd2631 JF |
7650 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
7651 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 7652 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
7653 | return; |
7654 | } | |
7655 | /* Both values are positive, so we can work with unsigned and | |
7656 | * copy the result to signed (unless it exceeds S64_MAX). | |
7657 | */ | |
7658 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
7659 | /* Potential overflow, we know nothing */ | |
3f50f132 | 7660 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
7661 | return; |
7662 | } | |
7663 | dst_reg->umin_value *= umin_val; | |
7664 | dst_reg->umax_value *= umax_val; | |
7665 | if (dst_reg->umax_value > S64_MAX) { | |
7666 | /* Overflow possible, we know nothing */ | |
7667 | dst_reg->smin_value = S64_MIN; | |
7668 | dst_reg->smax_value = S64_MAX; | |
7669 | } else { | |
7670 | dst_reg->smin_value = dst_reg->umin_value; | |
7671 | dst_reg->smax_value = dst_reg->umax_value; | |
7672 | } | |
7673 | } | |
7674 | ||
3f50f132 JF |
7675 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
7676 | struct bpf_reg_state *src_reg) | |
7677 | { | |
7678 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7679 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7680 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
7681 | s32 smin_val = src_reg->s32_min_value; | |
7682 | u32 umax_val = src_reg->u32_max_value; | |
7683 | ||
049c4e13 DB |
7684 | if (src_known && dst_known) { |
7685 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 7686 | return; |
049c4e13 | 7687 | } |
3f50f132 JF |
7688 | |
7689 | /* We get our minimum from the var_off, since that's inherently | |
7690 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
7691 | */ | |
7692 | dst_reg->u32_min_value = var32_off.value; | |
7693 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
7694 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
7695 | /* Lose signed bounds when ANDing negative numbers, | |
7696 | * ain't nobody got time for that. | |
7697 | */ | |
7698 | dst_reg->s32_min_value = S32_MIN; | |
7699 | dst_reg->s32_max_value = S32_MAX; | |
7700 | } else { | |
7701 | /* ANDing two positives gives a positive, so safe to | |
7702 | * cast result into s64. | |
7703 | */ | |
7704 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7705 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7706 | } | |
3f50f132 JF |
7707 | } |
7708 | ||
07cd2631 JF |
7709 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
7710 | struct bpf_reg_state *src_reg) | |
7711 | { | |
3f50f132 JF |
7712 | bool src_known = tnum_is_const(src_reg->var_off); |
7713 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
7714 | s64 smin_val = src_reg->smin_value; |
7715 | u64 umax_val = src_reg->umax_value; | |
7716 | ||
3f50f132 | 7717 | if (src_known && dst_known) { |
4fbb38a3 | 7718 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
7719 | return; |
7720 | } | |
7721 | ||
07cd2631 JF |
7722 | /* We get our minimum from the var_off, since that's inherently |
7723 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
7724 | */ | |
07cd2631 JF |
7725 | dst_reg->umin_value = dst_reg->var_off.value; |
7726 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
7727 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
7728 | /* Lose signed bounds when ANDing negative numbers, | |
7729 | * ain't nobody got time for that. | |
7730 | */ | |
7731 | dst_reg->smin_value = S64_MIN; | |
7732 | dst_reg->smax_value = S64_MAX; | |
7733 | } else { | |
7734 | /* ANDing two positives gives a positive, so safe to | |
7735 | * cast result into s64. | |
7736 | */ | |
7737 | dst_reg->smin_value = dst_reg->umin_value; | |
7738 | dst_reg->smax_value = dst_reg->umax_value; | |
7739 | } | |
7740 | /* We may learn something more from the var_off */ | |
7741 | __update_reg_bounds(dst_reg); | |
7742 | } | |
7743 | ||
3f50f132 JF |
7744 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
7745 | struct bpf_reg_state *src_reg) | |
7746 | { | |
7747 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7748 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7749 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
7750 | s32 smin_val = src_reg->s32_min_value; |
7751 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 7752 | |
049c4e13 DB |
7753 | if (src_known && dst_known) { |
7754 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 7755 | return; |
049c4e13 | 7756 | } |
3f50f132 JF |
7757 | |
7758 | /* We get our maximum from the var_off, and our minimum is the | |
7759 | * maximum of the operands' minima | |
7760 | */ | |
7761 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
7762 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
7763 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
7764 | /* Lose signed bounds when ORing negative numbers, | |
7765 | * ain't nobody got time for that. | |
7766 | */ | |
7767 | dst_reg->s32_min_value = S32_MIN; | |
7768 | dst_reg->s32_max_value = S32_MAX; | |
7769 | } else { | |
7770 | /* ORing two positives gives a positive, so safe to | |
7771 | * cast result into s64. | |
7772 | */ | |
5b9fbeb7 DB |
7773 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
7774 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
7775 | } |
7776 | } | |
7777 | ||
07cd2631 JF |
7778 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
7779 | struct bpf_reg_state *src_reg) | |
7780 | { | |
3f50f132 JF |
7781 | bool src_known = tnum_is_const(src_reg->var_off); |
7782 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
7783 | s64 smin_val = src_reg->smin_value; |
7784 | u64 umin_val = src_reg->umin_value; | |
7785 | ||
3f50f132 | 7786 | if (src_known && dst_known) { |
4fbb38a3 | 7787 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
7788 | return; |
7789 | } | |
7790 | ||
07cd2631 JF |
7791 | /* We get our maximum from the var_off, and our minimum is the |
7792 | * maximum of the operands' minima | |
7793 | */ | |
07cd2631 JF |
7794 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
7795 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
7796 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
7797 | /* Lose signed bounds when ORing negative numbers, | |
7798 | * ain't nobody got time for that. | |
7799 | */ | |
7800 | dst_reg->smin_value = S64_MIN; | |
7801 | dst_reg->smax_value = S64_MAX; | |
7802 | } else { | |
7803 | /* ORing two positives gives a positive, so safe to | |
7804 | * cast result into s64. | |
7805 | */ | |
7806 | dst_reg->smin_value = dst_reg->umin_value; | |
7807 | dst_reg->smax_value = dst_reg->umax_value; | |
7808 | } | |
7809 | /* We may learn something more from the var_off */ | |
7810 | __update_reg_bounds(dst_reg); | |
7811 | } | |
7812 | ||
2921c90d YS |
7813 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
7814 | struct bpf_reg_state *src_reg) | |
7815 | { | |
7816 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7817 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7818 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
7819 | s32 smin_val = src_reg->s32_min_value; | |
7820 | ||
049c4e13 DB |
7821 | if (src_known && dst_known) { |
7822 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 7823 | return; |
049c4e13 | 7824 | } |
2921c90d YS |
7825 | |
7826 | /* We get both minimum and maximum from the var32_off. */ | |
7827 | dst_reg->u32_min_value = var32_off.value; | |
7828 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
7829 | ||
7830 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
7831 | /* XORing two positive sign numbers gives a positive, | |
7832 | * so safe to cast u32 result into s32. | |
7833 | */ | |
7834 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7835 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7836 | } else { | |
7837 | dst_reg->s32_min_value = S32_MIN; | |
7838 | dst_reg->s32_max_value = S32_MAX; | |
7839 | } | |
7840 | } | |
7841 | ||
7842 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
7843 | struct bpf_reg_state *src_reg) | |
7844 | { | |
7845 | bool src_known = tnum_is_const(src_reg->var_off); | |
7846 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
7847 | s64 smin_val = src_reg->smin_value; | |
7848 | ||
7849 | if (src_known && dst_known) { | |
7850 | /* dst_reg->var_off.value has been updated earlier */ | |
7851 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
7852 | return; | |
7853 | } | |
7854 | ||
7855 | /* We get both minimum and maximum from the var_off. */ | |
7856 | dst_reg->umin_value = dst_reg->var_off.value; | |
7857 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
7858 | ||
7859 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
7860 | /* XORing two positive sign numbers gives a positive, | |
7861 | * so safe to cast u64 result into s64. | |
7862 | */ | |
7863 | dst_reg->smin_value = dst_reg->umin_value; | |
7864 | dst_reg->smax_value = dst_reg->umax_value; | |
7865 | } else { | |
7866 | dst_reg->smin_value = S64_MIN; | |
7867 | dst_reg->smax_value = S64_MAX; | |
7868 | } | |
7869 | ||
7870 | __update_reg_bounds(dst_reg); | |
7871 | } | |
7872 | ||
3f50f132 JF |
7873 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
7874 | u64 umin_val, u64 umax_val) | |
07cd2631 | 7875 | { |
07cd2631 JF |
7876 | /* We lose all sign bit information (except what we can pick |
7877 | * up from var_off) | |
7878 | */ | |
3f50f132 JF |
7879 | dst_reg->s32_min_value = S32_MIN; |
7880 | dst_reg->s32_max_value = S32_MAX; | |
7881 | /* If we might shift our top bit out, then we know nothing */ | |
7882 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
7883 | dst_reg->u32_min_value = 0; | |
7884 | dst_reg->u32_max_value = U32_MAX; | |
7885 | } else { | |
7886 | dst_reg->u32_min_value <<= umin_val; | |
7887 | dst_reg->u32_max_value <<= umax_val; | |
7888 | } | |
7889 | } | |
7890 | ||
7891 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
7892 | struct bpf_reg_state *src_reg) | |
7893 | { | |
7894 | u32 umax_val = src_reg->u32_max_value; | |
7895 | u32 umin_val = src_reg->u32_min_value; | |
7896 | /* u32 alu operation will zext upper bits */ | |
7897 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
7898 | ||
7899 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
7900 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
7901 | /* Not required but being careful mark reg64 bounds as unknown so | |
7902 | * that we are forced to pick them up from tnum and zext later and | |
7903 | * if some path skips this step we are still safe. | |
7904 | */ | |
7905 | __mark_reg64_unbounded(dst_reg); | |
7906 | __update_reg32_bounds(dst_reg); | |
7907 | } | |
7908 | ||
7909 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
7910 | u64 umin_val, u64 umax_val) | |
7911 | { | |
7912 | /* Special case <<32 because it is a common compiler pattern to sign | |
7913 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
7914 | * positive we know this shift will also be positive so we can track | |
7915 | * bounds correctly. Otherwise we lose all sign bit information except | |
7916 | * what we can pick up from var_off. Perhaps we can generalize this | |
7917 | * later to shifts of any length. | |
7918 | */ | |
7919 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
7920 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
7921 | else | |
7922 | dst_reg->smax_value = S64_MAX; | |
7923 | ||
7924 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
7925 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
7926 | else | |
7927 | dst_reg->smin_value = S64_MIN; | |
7928 | ||
07cd2631 JF |
7929 | /* If we might shift our top bit out, then we know nothing */ |
7930 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
7931 | dst_reg->umin_value = 0; | |
7932 | dst_reg->umax_value = U64_MAX; | |
7933 | } else { | |
7934 | dst_reg->umin_value <<= umin_val; | |
7935 | dst_reg->umax_value <<= umax_val; | |
7936 | } | |
3f50f132 JF |
7937 | } |
7938 | ||
7939 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
7940 | struct bpf_reg_state *src_reg) | |
7941 | { | |
7942 | u64 umax_val = src_reg->umax_value; | |
7943 | u64 umin_val = src_reg->umin_value; | |
7944 | ||
7945 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
7946 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
7947 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
7948 | ||
07cd2631 JF |
7949 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
7950 | /* We may learn something more from the var_off */ | |
7951 | __update_reg_bounds(dst_reg); | |
7952 | } | |
7953 | ||
3f50f132 JF |
7954 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
7955 | struct bpf_reg_state *src_reg) | |
7956 | { | |
7957 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
7958 | u32 umax_val = src_reg->u32_max_value; | |
7959 | u32 umin_val = src_reg->u32_min_value; | |
7960 | ||
7961 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
7962 | * be negative, then either: | |
7963 | * 1) src_reg might be zero, so the sign bit of the result is | |
7964 | * unknown, so we lose our signed bounds | |
7965 | * 2) it's known negative, thus the unsigned bounds capture the | |
7966 | * signed bounds | |
7967 | * 3) the signed bounds cross zero, so they tell us nothing | |
7968 | * about the result | |
7969 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 7970 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
7971 | * Thus, in all cases it suffices to blow away our signed bounds |
7972 | * and rely on inferring new ones from the unsigned bounds and | |
7973 | * var_off of the result. | |
7974 | */ | |
7975 | dst_reg->s32_min_value = S32_MIN; | |
7976 | dst_reg->s32_max_value = S32_MAX; | |
7977 | ||
7978 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
7979 | dst_reg->u32_min_value >>= umax_val; | |
7980 | dst_reg->u32_max_value >>= umin_val; | |
7981 | ||
7982 | __mark_reg64_unbounded(dst_reg); | |
7983 | __update_reg32_bounds(dst_reg); | |
7984 | } | |
7985 | ||
07cd2631 JF |
7986 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
7987 | struct bpf_reg_state *src_reg) | |
7988 | { | |
7989 | u64 umax_val = src_reg->umax_value; | |
7990 | u64 umin_val = src_reg->umin_value; | |
7991 | ||
7992 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
7993 | * be negative, then either: | |
7994 | * 1) src_reg might be zero, so the sign bit of the result is | |
7995 | * unknown, so we lose our signed bounds | |
7996 | * 2) it's known negative, thus the unsigned bounds capture the | |
7997 | * signed bounds | |
7998 | * 3) the signed bounds cross zero, so they tell us nothing | |
7999 | * about the result | |
8000 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 8001 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
8002 | * Thus, in all cases it suffices to blow away our signed bounds |
8003 | * and rely on inferring new ones from the unsigned bounds and | |
8004 | * var_off of the result. | |
8005 | */ | |
8006 | dst_reg->smin_value = S64_MIN; | |
8007 | dst_reg->smax_value = S64_MAX; | |
8008 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
8009 | dst_reg->umin_value >>= umax_val; | |
8010 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
8011 | |
8012 | /* Its not easy to operate on alu32 bounds here because it depends | |
8013 | * on bits being shifted in. Take easy way out and mark unbounded | |
8014 | * so we can recalculate later from tnum. | |
8015 | */ | |
8016 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
8017 | __update_reg_bounds(dst_reg); |
8018 | } | |
8019 | ||
3f50f132 JF |
8020 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
8021 | struct bpf_reg_state *src_reg) | |
07cd2631 | 8022 | { |
3f50f132 | 8023 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
8024 | |
8025 | /* Upon reaching here, src_known is true and | |
8026 | * umax_val is equal to umin_val. | |
8027 | */ | |
3f50f132 JF |
8028 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
8029 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 8030 | |
3f50f132 JF |
8031 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
8032 | ||
8033 | /* blow away the dst_reg umin_value/umax_value and rely on | |
8034 | * dst_reg var_off to refine the result. | |
8035 | */ | |
8036 | dst_reg->u32_min_value = 0; | |
8037 | dst_reg->u32_max_value = U32_MAX; | |
8038 | ||
8039 | __mark_reg64_unbounded(dst_reg); | |
8040 | __update_reg32_bounds(dst_reg); | |
8041 | } | |
8042 | ||
8043 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
8044 | struct bpf_reg_state *src_reg) | |
8045 | { | |
8046 | u64 umin_val = src_reg->umin_value; | |
8047 | ||
8048 | /* Upon reaching here, src_known is true and umax_val is equal | |
8049 | * to umin_val. | |
8050 | */ | |
8051 | dst_reg->smin_value >>= umin_val; | |
8052 | dst_reg->smax_value >>= umin_val; | |
8053 | ||
8054 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
8055 | |
8056 | /* blow away the dst_reg umin_value/umax_value and rely on | |
8057 | * dst_reg var_off to refine the result. | |
8058 | */ | |
8059 | dst_reg->umin_value = 0; | |
8060 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
8061 | |
8062 | /* Its not easy to operate on alu32 bounds here because it depends | |
8063 | * on bits being shifted in from upper 32-bits. Take easy way out | |
8064 | * and mark unbounded so we can recalculate later from tnum. | |
8065 | */ | |
8066 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
8067 | __update_reg_bounds(dst_reg); |
8068 | } | |
8069 | ||
468f6eaf JH |
8070 | /* WARNING: This function does calculations on 64-bit values, but the actual |
8071 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
8072 | * need extra checks in the 32-bit case. | |
8073 | */ | |
f1174f77 EC |
8074 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
8075 | struct bpf_insn *insn, | |
8076 | struct bpf_reg_state *dst_reg, | |
8077 | struct bpf_reg_state src_reg) | |
969bf05e | 8078 | { |
638f5b90 | 8079 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 8080 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 8081 | bool src_known; |
b03c9f9f EC |
8082 | s64 smin_val, smax_val; |
8083 | u64 umin_val, umax_val; | |
3f50f132 JF |
8084 | s32 s32_min_val, s32_max_val; |
8085 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 8086 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 8087 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 8088 | int ret; |
b799207e | 8089 | |
b03c9f9f EC |
8090 | smin_val = src_reg.smin_value; |
8091 | smax_val = src_reg.smax_value; | |
8092 | umin_val = src_reg.umin_value; | |
8093 | umax_val = src_reg.umax_value; | |
f23cc643 | 8094 | |
3f50f132 JF |
8095 | s32_min_val = src_reg.s32_min_value; |
8096 | s32_max_val = src_reg.s32_max_value; | |
8097 | u32_min_val = src_reg.u32_min_value; | |
8098 | u32_max_val = src_reg.u32_max_value; | |
8099 | ||
8100 | if (alu32) { | |
8101 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
8102 | if ((src_known && |
8103 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
8104 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
8105 | /* Taint dst register if offset had invalid bounds | |
8106 | * derived from e.g. dead branches. | |
8107 | */ | |
8108 | __mark_reg_unknown(env, dst_reg); | |
8109 | return 0; | |
8110 | } | |
8111 | } else { | |
8112 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
8113 | if ((src_known && |
8114 | (smin_val != smax_val || umin_val != umax_val)) || | |
8115 | smin_val > smax_val || umin_val > umax_val) { | |
8116 | /* Taint dst register if offset had invalid bounds | |
8117 | * derived from e.g. dead branches. | |
8118 | */ | |
8119 | __mark_reg_unknown(env, dst_reg); | |
8120 | return 0; | |
8121 | } | |
6f16101e DB |
8122 | } |
8123 | ||
bb7f0f98 AS |
8124 | if (!src_known && |
8125 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 8126 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
8127 | return 0; |
8128 | } | |
8129 | ||
f5288193 DB |
8130 | if (sanitize_needed(opcode)) { |
8131 | ret = sanitize_val_alu(env, insn); | |
8132 | if (ret < 0) | |
8133 | return sanitize_err(env, insn, ret, NULL, NULL); | |
8134 | } | |
8135 | ||
3f50f132 JF |
8136 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
8137 | * There are two classes of instructions: The first class we track both | |
8138 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
8139 | * greatest amount of precision when alu operations are mixed with jmp32 | |
8140 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
8141 | * and BPF_OR. This is possible because these ops have fairly easy to | |
8142 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
8143 | * See alu32 verifier tests for examples. The second class of | |
8144 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
8145 | * with regards to tracking sign/unsigned bounds because the bits may | |
8146 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
8147 | * the reg unbounded in the subreg bound space and use the resulting | |
8148 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
8149 | */ | |
48461135 JB |
8150 | switch (opcode) { |
8151 | case BPF_ADD: | |
3f50f132 | 8152 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 8153 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 8154 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
8155 | break; |
8156 | case BPF_SUB: | |
3f50f132 | 8157 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 8158 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 8159 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
8160 | break; |
8161 | case BPF_MUL: | |
3f50f132 JF |
8162 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
8163 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 8164 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
8165 | break; |
8166 | case BPF_AND: | |
3f50f132 JF |
8167 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
8168 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 8169 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
8170 | break; |
8171 | case BPF_OR: | |
3f50f132 JF |
8172 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
8173 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 8174 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 8175 | break; |
2921c90d YS |
8176 | case BPF_XOR: |
8177 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
8178 | scalar32_min_max_xor(dst_reg, &src_reg); | |
8179 | scalar_min_max_xor(dst_reg, &src_reg); | |
8180 | break; | |
48461135 | 8181 | case BPF_LSH: |
468f6eaf JH |
8182 | if (umax_val >= insn_bitness) { |
8183 | /* Shifts greater than 31 or 63 are undefined. | |
8184 | * This includes shifts by a negative number. | |
b03c9f9f | 8185 | */ |
61bd5218 | 8186 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
8187 | break; |
8188 | } | |
3f50f132 JF |
8189 | if (alu32) |
8190 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
8191 | else | |
8192 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
8193 | break; |
8194 | case BPF_RSH: | |
468f6eaf JH |
8195 | if (umax_val >= insn_bitness) { |
8196 | /* Shifts greater than 31 or 63 are undefined. | |
8197 | * This includes shifts by a negative number. | |
b03c9f9f | 8198 | */ |
61bd5218 | 8199 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
8200 | break; |
8201 | } | |
3f50f132 JF |
8202 | if (alu32) |
8203 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
8204 | else | |
8205 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 8206 | break; |
9cbe1f5a YS |
8207 | case BPF_ARSH: |
8208 | if (umax_val >= insn_bitness) { | |
8209 | /* Shifts greater than 31 or 63 are undefined. | |
8210 | * This includes shifts by a negative number. | |
8211 | */ | |
8212 | mark_reg_unknown(env, regs, insn->dst_reg); | |
8213 | break; | |
8214 | } | |
3f50f132 JF |
8215 | if (alu32) |
8216 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
8217 | else | |
8218 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 8219 | break; |
48461135 | 8220 | default: |
61bd5218 | 8221 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
8222 | break; |
8223 | } | |
8224 | ||
3f50f132 JF |
8225 | /* ALU32 ops are zero extended into 64bit register */ |
8226 | if (alu32) | |
8227 | zext_32_to_64(dst_reg); | |
468f6eaf | 8228 | |
294f2fc6 | 8229 | __update_reg_bounds(dst_reg); |
b03c9f9f EC |
8230 | __reg_deduce_bounds(dst_reg); |
8231 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
8232 | return 0; |
8233 | } | |
8234 | ||
8235 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
8236 | * and var_off. | |
8237 | */ | |
8238 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
8239 | struct bpf_insn *insn) | |
8240 | { | |
f4d7e40a AS |
8241 | struct bpf_verifier_state *vstate = env->cur_state; |
8242 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
8243 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
8244 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
8245 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 8246 | int err; |
f1174f77 EC |
8247 | |
8248 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
8249 | src_reg = NULL; |
8250 | if (dst_reg->type != SCALAR_VALUE) | |
8251 | ptr_reg = dst_reg; | |
75748837 AS |
8252 | else |
8253 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
8254 | * incorrectly propagated into other registers by find_equal_scalars() | |
8255 | */ | |
8256 | dst_reg->id = 0; | |
f1174f77 EC |
8257 | if (BPF_SRC(insn->code) == BPF_X) { |
8258 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
8259 | if (src_reg->type != SCALAR_VALUE) { |
8260 | if (dst_reg->type != SCALAR_VALUE) { | |
8261 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
8262 | * an arbitrary scalar. Disallow all math except |
8263 | * pointer subtraction | |
f1174f77 | 8264 | */ |
dd066823 | 8265 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
8266 | mark_reg_unknown(env, regs, insn->dst_reg); |
8267 | return 0; | |
f1174f77 | 8268 | } |
82abbf8d AS |
8269 | verbose(env, "R%d pointer %s pointer prohibited\n", |
8270 | insn->dst_reg, | |
8271 | bpf_alu_string[opcode >> 4]); | |
8272 | return -EACCES; | |
f1174f77 EC |
8273 | } else { |
8274 | /* scalar += pointer | |
8275 | * This is legal, but we have to reverse our | |
8276 | * src/dest handling in computing the range | |
8277 | */ | |
b5dc0163 AS |
8278 | err = mark_chain_precision(env, insn->dst_reg); |
8279 | if (err) | |
8280 | return err; | |
82abbf8d AS |
8281 | return adjust_ptr_min_max_vals(env, insn, |
8282 | src_reg, dst_reg); | |
f1174f77 EC |
8283 | } |
8284 | } else if (ptr_reg) { | |
8285 | /* pointer += scalar */ | |
b5dc0163 AS |
8286 | err = mark_chain_precision(env, insn->src_reg); |
8287 | if (err) | |
8288 | return err; | |
82abbf8d AS |
8289 | return adjust_ptr_min_max_vals(env, insn, |
8290 | dst_reg, src_reg); | |
f1174f77 EC |
8291 | } |
8292 | } else { | |
8293 | /* Pretend the src is a reg with a known value, since we only | |
8294 | * need to be able to read from this state. | |
8295 | */ | |
8296 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 8297 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 8298 | src_reg = &off_reg; |
82abbf8d AS |
8299 | if (ptr_reg) /* pointer += K */ |
8300 | return adjust_ptr_min_max_vals(env, insn, | |
8301 | ptr_reg, src_reg); | |
f1174f77 EC |
8302 | } |
8303 | ||
8304 | /* Got here implies adding two SCALAR_VALUEs */ | |
8305 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 8306 | print_verifier_state(env, state, true); |
61bd5218 | 8307 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
8308 | return -EINVAL; |
8309 | } | |
8310 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 8311 | print_verifier_state(env, state, true); |
61bd5218 | 8312 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
8313 | return -EINVAL; |
8314 | } | |
8315 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
8316 | } |
8317 | ||
17a52670 | 8318 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 8319 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 8320 | { |
638f5b90 | 8321 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
8322 | u8 opcode = BPF_OP(insn->code); |
8323 | int err; | |
8324 | ||
8325 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
8326 | if (opcode == BPF_NEG) { | |
8327 | if (BPF_SRC(insn->code) != 0 || | |
8328 | insn->src_reg != BPF_REG_0 || | |
8329 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 8330 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
8331 | return -EINVAL; |
8332 | } | |
8333 | } else { | |
8334 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
8335 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
8336 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 8337 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
8338 | return -EINVAL; |
8339 | } | |
8340 | } | |
8341 | ||
8342 | /* check src operand */ | |
dc503a8a | 8343 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8344 | if (err) |
8345 | return err; | |
8346 | ||
1be7f75d | 8347 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 8348 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
8349 | insn->dst_reg); |
8350 | return -EACCES; | |
8351 | } | |
8352 | ||
17a52670 | 8353 | /* check dest operand */ |
dc503a8a | 8354 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
8355 | if (err) |
8356 | return err; | |
8357 | ||
8358 | } else if (opcode == BPF_MOV) { | |
8359 | ||
8360 | if (BPF_SRC(insn->code) == BPF_X) { | |
8361 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 8362 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
8363 | return -EINVAL; |
8364 | } | |
8365 | ||
8366 | /* check src operand */ | |
dc503a8a | 8367 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8368 | if (err) |
8369 | return err; | |
8370 | } else { | |
8371 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 8372 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
8373 | return -EINVAL; |
8374 | } | |
8375 | } | |
8376 | ||
fbeb1603 AF |
8377 | /* check dest operand, mark as required later */ |
8378 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
8379 | if (err) |
8380 | return err; | |
8381 | ||
8382 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
8383 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
8384 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
8385 | ||
17a52670 AS |
8386 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
8387 | /* case: R1 = R2 | |
8388 | * copy register state to dest reg | |
8389 | */ | |
75748837 AS |
8390 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
8391 | /* Assign src and dst registers the same ID | |
8392 | * that will be used by find_equal_scalars() | |
8393 | * to propagate min/max range. | |
8394 | */ | |
8395 | src_reg->id = ++env->id_gen; | |
e434b8cd JW |
8396 | *dst_reg = *src_reg; |
8397 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 8398 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 8399 | } else { |
f1174f77 | 8400 | /* R1 = (u32) R2 */ |
1be7f75d | 8401 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
8402 | verbose(env, |
8403 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
8404 | insn->src_reg); |
8405 | return -EACCES; | |
e434b8cd JW |
8406 | } else if (src_reg->type == SCALAR_VALUE) { |
8407 | *dst_reg = *src_reg; | |
75748837 AS |
8408 | /* Make sure ID is cleared otherwise |
8409 | * dst_reg min/max could be incorrectly | |
8410 | * propagated into src_reg by find_equal_scalars() | |
8411 | */ | |
8412 | dst_reg->id = 0; | |
e434b8cd | 8413 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 8414 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
8415 | } else { |
8416 | mark_reg_unknown(env, regs, | |
8417 | insn->dst_reg); | |
1be7f75d | 8418 | } |
3f50f132 | 8419 | zext_32_to_64(dst_reg); |
17a52670 AS |
8420 | } |
8421 | } else { | |
8422 | /* case: R = imm | |
8423 | * remember the value we stored into this reg | |
8424 | */ | |
fbeb1603 AF |
8425 | /* clear any state __mark_reg_known doesn't set */ |
8426 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 8427 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
8428 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
8429 | __mark_reg_known(regs + insn->dst_reg, | |
8430 | insn->imm); | |
8431 | } else { | |
8432 | __mark_reg_known(regs + insn->dst_reg, | |
8433 | (u32)insn->imm); | |
8434 | } | |
17a52670 AS |
8435 | } |
8436 | ||
8437 | } else if (opcode > BPF_END) { | |
61bd5218 | 8438 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
8439 | return -EINVAL; |
8440 | ||
8441 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
8442 | ||
17a52670 AS |
8443 | if (BPF_SRC(insn->code) == BPF_X) { |
8444 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 8445 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
8446 | return -EINVAL; |
8447 | } | |
8448 | /* check src1 operand */ | |
dc503a8a | 8449 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8450 | if (err) |
8451 | return err; | |
8452 | } else { | |
8453 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 8454 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
8455 | return -EINVAL; |
8456 | } | |
8457 | } | |
8458 | ||
8459 | /* check src2 operand */ | |
dc503a8a | 8460 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8461 | if (err) |
8462 | return err; | |
8463 | ||
8464 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
8465 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 8466 | verbose(env, "div by zero\n"); |
17a52670 AS |
8467 | return -EINVAL; |
8468 | } | |
8469 | ||
229394e8 RV |
8470 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
8471 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
8472 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
8473 | ||
8474 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 8475 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
8476 | return -EINVAL; |
8477 | } | |
8478 | } | |
8479 | ||
1a0dc1ac | 8480 | /* check dest operand */ |
dc503a8a | 8481 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
8482 | if (err) |
8483 | return err; | |
8484 | ||
f1174f77 | 8485 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
8486 | } |
8487 | ||
8488 | return 0; | |
8489 | } | |
8490 | ||
c6a9efa1 PC |
8491 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
8492 | struct bpf_reg_state *dst_reg, | |
6d94e741 | 8493 | enum bpf_reg_type type, int new_range) |
c6a9efa1 PC |
8494 | { |
8495 | struct bpf_reg_state *reg; | |
8496 | int i; | |
8497 | ||
8498 | for (i = 0; i < MAX_BPF_REG; i++) { | |
8499 | reg = &state->regs[i]; | |
8500 | if (reg->type == type && reg->id == dst_reg->id) | |
8501 | /* keep the maximum range already checked */ | |
8502 | reg->range = max(reg->range, new_range); | |
8503 | } | |
8504 | ||
8505 | bpf_for_each_spilled_reg(i, state, reg) { | |
8506 | if (!reg) | |
8507 | continue; | |
8508 | if (reg->type == type && reg->id == dst_reg->id) | |
8509 | reg->range = max(reg->range, new_range); | |
8510 | } | |
8511 | } | |
8512 | ||
f4d7e40a | 8513 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 8514 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 8515 | enum bpf_reg_type type, |
fb2a311a | 8516 | bool range_right_open) |
969bf05e | 8517 | { |
6d94e741 | 8518 | int new_range, i; |
2d2be8ca | 8519 | |
fb2a311a DB |
8520 | if (dst_reg->off < 0 || |
8521 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
8522 | /* This doesn't give us any range */ |
8523 | return; | |
8524 | ||
b03c9f9f EC |
8525 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
8526 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
8527 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
8528 | * than pkt_end, but that's because it's also less than pkt. | |
8529 | */ | |
8530 | return; | |
8531 | ||
fb2a311a DB |
8532 | new_range = dst_reg->off; |
8533 | if (range_right_open) | |
2fa7d94a | 8534 | new_range++; |
fb2a311a DB |
8535 | |
8536 | /* Examples for register markings: | |
2d2be8ca | 8537 | * |
fb2a311a | 8538 | * pkt_data in dst register: |
2d2be8ca DB |
8539 | * |
8540 | * r2 = r3; | |
8541 | * r2 += 8; | |
8542 | * if (r2 > pkt_end) goto <handle exception> | |
8543 | * <access okay> | |
8544 | * | |
b4e432f1 DB |
8545 | * r2 = r3; |
8546 | * r2 += 8; | |
8547 | * if (r2 < pkt_end) goto <access okay> | |
8548 | * <handle exception> | |
8549 | * | |
2d2be8ca DB |
8550 | * Where: |
8551 | * r2 == dst_reg, pkt_end == src_reg | |
8552 | * r2=pkt(id=n,off=8,r=0) | |
8553 | * r3=pkt(id=n,off=0,r=0) | |
8554 | * | |
fb2a311a | 8555 | * pkt_data in src register: |
2d2be8ca DB |
8556 | * |
8557 | * r2 = r3; | |
8558 | * r2 += 8; | |
8559 | * if (pkt_end >= r2) goto <access okay> | |
8560 | * <handle exception> | |
8561 | * | |
b4e432f1 DB |
8562 | * r2 = r3; |
8563 | * r2 += 8; | |
8564 | * if (pkt_end <= r2) goto <handle exception> | |
8565 | * <access okay> | |
8566 | * | |
2d2be8ca DB |
8567 | * Where: |
8568 | * pkt_end == dst_reg, r2 == src_reg | |
8569 | * r2=pkt(id=n,off=8,r=0) | |
8570 | * r3=pkt(id=n,off=0,r=0) | |
8571 | * | |
8572 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
8573 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
8574 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
8575 | * the check. | |
969bf05e | 8576 | */ |
2d2be8ca | 8577 | |
f1174f77 EC |
8578 | /* If our ids match, then we must have the same max_value. And we |
8579 | * don't care about the other reg's fixed offset, since if it's too big | |
8580 | * the range won't allow anything. | |
8581 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
8582 | */ | |
c6a9efa1 PC |
8583 | for (i = 0; i <= vstate->curframe; i++) |
8584 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
8585 | new_range); | |
969bf05e AS |
8586 | } |
8587 | ||
3f50f132 | 8588 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 8589 | { |
3f50f132 JF |
8590 | struct tnum subreg = tnum_subreg(reg->var_off); |
8591 | s32 sval = (s32)val; | |
a72dafaf | 8592 | |
3f50f132 JF |
8593 | switch (opcode) { |
8594 | case BPF_JEQ: | |
8595 | if (tnum_is_const(subreg)) | |
8596 | return !!tnum_equals_const(subreg, val); | |
8597 | break; | |
8598 | case BPF_JNE: | |
8599 | if (tnum_is_const(subreg)) | |
8600 | return !tnum_equals_const(subreg, val); | |
8601 | break; | |
8602 | case BPF_JSET: | |
8603 | if ((~subreg.mask & subreg.value) & val) | |
8604 | return 1; | |
8605 | if (!((subreg.mask | subreg.value) & val)) | |
8606 | return 0; | |
8607 | break; | |
8608 | case BPF_JGT: | |
8609 | if (reg->u32_min_value > val) | |
8610 | return 1; | |
8611 | else if (reg->u32_max_value <= val) | |
8612 | return 0; | |
8613 | break; | |
8614 | case BPF_JSGT: | |
8615 | if (reg->s32_min_value > sval) | |
8616 | return 1; | |
ee114dd6 | 8617 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
8618 | return 0; |
8619 | break; | |
8620 | case BPF_JLT: | |
8621 | if (reg->u32_max_value < val) | |
8622 | return 1; | |
8623 | else if (reg->u32_min_value >= val) | |
8624 | return 0; | |
8625 | break; | |
8626 | case BPF_JSLT: | |
8627 | if (reg->s32_max_value < sval) | |
8628 | return 1; | |
8629 | else if (reg->s32_min_value >= sval) | |
8630 | return 0; | |
8631 | break; | |
8632 | case BPF_JGE: | |
8633 | if (reg->u32_min_value >= val) | |
8634 | return 1; | |
8635 | else if (reg->u32_max_value < val) | |
8636 | return 0; | |
8637 | break; | |
8638 | case BPF_JSGE: | |
8639 | if (reg->s32_min_value >= sval) | |
8640 | return 1; | |
8641 | else if (reg->s32_max_value < sval) | |
8642 | return 0; | |
8643 | break; | |
8644 | case BPF_JLE: | |
8645 | if (reg->u32_max_value <= val) | |
8646 | return 1; | |
8647 | else if (reg->u32_min_value > val) | |
8648 | return 0; | |
8649 | break; | |
8650 | case BPF_JSLE: | |
8651 | if (reg->s32_max_value <= sval) | |
8652 | return 1; | |
8653 | else if (reg->s32_min_value > sval) | |
8654 | return 0; | |
8655 | break; | |
8656 | } | |
4f7b3e82 | 8657 | |
3f50f132 JF |
8658 | return -1; |
8659 | } | |
092ed096 | 8660 | |
3f50f132 JF |
8661 | |
8662 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
8663 | { | |
8664 | s64 sval = (s64)val; | |
a72dafaf | 8665 | |
4f7b3e82 AS |
8666 | switch (opcode) { |
8667 | case BPF_JEQ: | |
8668 | if (tnum_is_const(reg->var_off)) | |
8669 | return !!tnum_equals_const(reg->var_off, val); | |
8670 | break; | |
8671 | case BPF_JNE: | |
8672 | if (tnum_is_const(reg->var_off)) | |
8673 | return !tnum_equals_const(reg->var_off, val); | |
8674 | break; | |
960ea056 JK |
8675 | case BPF_JSET: |
8676 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
8677 | return 1; | |
8678 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
8679 | return 0; | |
8680 | break; | |
4f7b3e82 AS |
8681 | case BPF_JGT: |
8682 | if (reg->umin_value > val) | |
8683 | return 1; | |
8684 | else if (reg->umax_value <= val) | |
8685 | return 0; | |
8686 | break; | |
8687 | case BPF_JSGT: | |
a72dafaf | 8688 | if (reg->smin_value > sval) |
4f7b3e82 | 8689 | return 1; |
ee114dd6 | 8690 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
8691 | return 0; |
8692 | break; | |
8693 | case BPF_JLT: | |
8694 | if (reg->umax_value < val) | |
8695 | return 1; | |
8696 | else if (reg->umin_value >= val) | |
8697 | return 0; | |
8698 | break; | |
8699 | case BPF_JSLT: | |
a72dafaf | 8700 | if (reg->smax_value < sval) |
4f7b3e82 | 8701 | return 1; |
a72dafaf | 8702 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
8703 | return 0; |
8704 | break; | |
8705 | case BPF_JGE: | |
8706 | if (reg->umin_value >= val) | |
8707 | return 1; | |
8708 | else if (reg->umax_value < val) | |
8709 | return 0; | |
8710 | break; | |
8711 | case BPF_JSGE: | |
a72dafaf | 8712 | if (reg->smin_value >= sval) |
4f7b3e82 | 8713 | return 1; |
a72dafaf | 8714 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
8715 | return 0; |
8716 | break; | |
8717 | case BPF_JLE: | |
8718 | if (reg->umax_value <= val) | |
8719 | return 1; | |
8720 | else if (reg->umin_value > val) | |
8721 | return 0; | |
8722 | break; | |
8723 | case BPF_JSLE: | |
a72dafaf | 8724 | if (reg->smax_value <= sval) |
4f7b3e82 | 8725 | return 1; |
a72dafaf | 8726 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
8727 | return 0; |
8728 | break; | |
8729 | } | |
8730 | ||
8731 | return -1; | |
8732 | } | |
8733 | ||
3f50f132 JF |
8734 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
8735 | * and return: | |
8736 | * 1 - branch will be taken and "goto target" will be executed | |
8737 | * 0 - branch will not be taken and fall-through to next insn | |
8738 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
8739 | * range [0,10] | |
604dca5e | 8740 | */ |
3f50f132 JF |
8741 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
8742 | bool is_jmp32) | |
604dca5e | 8743 | { |
cac616db JF |
8744 | if (__is_pointer_value(false, reg)) { |
8745 | if (!reg_type_not_null(reg->type)) | |
8746 | return -1; | |
8747 | ||
8748 | /* If pointer is valid tests against zero will fail so we can | |
8749 | * use this to direct branch taken. | |
8750 | */ | |
8751 | if (val != 0) | |
8752 | return -1; | |
8753 | ||
8754 | switch (opcode) { | |
8755 | case BPF_JEQ: | |
8756 | return 0; | |
8757 | case BPF_JNE: | |
8758 | return 1; | |
8759 | default: | |
8760 | return -1; | |
8761 | } | |
8762 | } | |
604dca5e | 8763 | |
3f50f132 JF |
8764 | if (is_jmp32) |
8765 | return is_branch32_taken(reg, val, opcode); | |
8766 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
8767 | } |
8768 | ||
6d94e741 AS |
8769 | static int flip_opcode(u32 opcode) |
8770 | { | |
8771 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
8772 | static const u8 opcode_flip[16] = { | |
8773 | /* these stay the same */ | |
8774 | [BPF_JEQ >> 4] = BPF_JEQ, | |
8775 | [BPF_JNE >> 4] = BPF_JNE, | |
8776 | [BPF_JSET >> 4] = BPF_JSET, | |
8777 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
8778 | [BPF_JGE >> 4] = BPF_JLE, | |
8779 | [BPF_JGT >> 4] = BPF_JLT, | |
8780 | [BPF_JLE >> 4] = BPF_JGE, | |
8781 | [BPF_JLT >> 4] = BPF_JGT, | |
8782 | [BPF_JSGE >> 4] = BPF_JSLE, | |
8783 | [BPF_JSGT >> 4] = BPF_JSLT, | |
8784 | [BPF_JSLE >> 4] = BPF_JSGE, | |
8785 | [BPF_JSLT >> 4] = BPF_JSGT | |
8786 | }; | |
8787 | return opcode_flip[opcode >> 4]; | |
8788 | } | |
8789 | ||
8790 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
8791 | struct bpf_reg_state *src_reg, | |
8792 | u8 opcode) | |
8793 | { | |
8794 | struct bpf_reg_state *pkt; | |
8795 | ||
8796 | if (src_reg->type == PTR_TO_PACKET_END) { | |
8797 | pkt = dst_reg; | |
8798 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
8799 | pkt = src_reg; | |
8800 | opcode = flip_opcode(opcode); | |
8801 | } else { | |
8802 | return -1; | |
8803 | } | |
8804 | ||
8805 | if (pkt->range >= 0) | |
8806 | return -1; | |
8807 | ||
8808 | switch (opcode) { | |
8809 | case BPF_JLE: | |
8810 | /* pkt <= pkt_end */ | |
8811 | fallthrough; | |
8812 | case BPF_JGT: | |
8813 | /* pkt > pkt_end */ | |
8814 | if (pkt->range == BEYOND_PKT_END) | |
8815 | /* pkt has at last one extra byte beyond pkt_end */ | |
8816 | return opcode == BPF_JGT; | |
8817 | break; | |
8818 | case BPF_JLT: | |
8819 | /* pkt < pkt_end */ | |
8820 | fallthrough; | |
8821 | case BPF_JGE: | |
8822 | /* pkt >= pkt_end */ | |
8823 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
8824 | return opcode == BPF_JGE; | |
8825 | break; | |
8826 | } | |
8827 | return -1; | |
8828 | } | |
8829 | ||
48461135 JB |
8830 | /* Adjusts the register min/max values in the case that the dst_reg is the |
8831 | * variable register that we are working on, and src_reg is a constant or we're | |
8832 | * simply doing a BPF_K check. | |
f1174f77 | 8833 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
8834 | */ |
8835 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
8836 | struct bpf_reg_state *false_reg, |
8837 | u64 val, u32 val32, | |
092ed096 | 8838 | u8 opcode, bool is_jmp32) |
48461135 | 8839 | { |
3f50f132 JF |
8840 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
8841 | struct tnum false_64off = false_reg->var_off; | |
8842 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
8843 | struct tnum true_64off = true_reg->var_off; | |
8844 | s64 sval = (s64)val; | |
8845 | s32 sval32 = (s32)val32; | |
a72dafaf | 8846 | |
f1174f77 EC |
8847 | /* If the dst_reg is a pointer, we can't learn anything about its |
8848 | * variable offset from the compare (unless src_reg were a pointer into | |
8849 | * the same object, but we don't bother with that. | |
8850 | * Since false_reg and true_reg have the same type by construction, we | |
8851 | * only need to check one of them for pointerness. | |
8852 | */ | |
8853 | if (__is_pointer_value(false, false_reg)) | |
8854 | return; | |
4cabc5b1 | 8855 | |
48461135 JB |
8856 | switch (opcode) { |
8857 | case BPF_JEQ: | |
48461135 | 8858 | case BPF_JNE: |
a72dafaf JW |
8859 | { |
8860 | struct bpf_reg_state *reg = | |
8861 | opcode == BPF_JEQ ? true_reg : false_reg; | |
8862 | ||
e688c3db AS |
8863 | /* JEQ/JNE comparison doesn't change the register equivalence. |
8864 | * r1 = r2; | |
8865 | * if (r1 == 42) goto label; | |
8866 | * ... | |
8867 | * label: // here both r1 and r2 are known to be 42. | |
8868 | * | |
8869 | * Hence when marking register as known preserve it's ID. | |
48461135 | 8870 | */ |
3f50f132 JF |
8871 | if (is_jmp32) |
8872 | __mark_reg32_known(reg, val32); | |
8873 | else | |
e688c3db | 8874 | ___mark_reg_known(reg, val); |
48461135 | 8875 | break; |
a72dafaf | 8876 | } |
960ea056 | 8877 | case BPF_JSET: |
3f50f132 JF |
8878 | if (is_jmp32) { |
8879 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
8880 | if (is_power_of_2(val32)) | |
8881 | true_32off = tnum_or(true_32off, | |
8882 | tnum_const(val32)); | |
8883 | } else { | |
8884 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
8885 | if (is_power_of_2(val)) | |
8886 | true_64off = tnum_or(true_64off, | |
8887 | tnum_const(val)); | |
8888 | } | |
960ea056 | 8889 | break; |
48461135 | 8890 | case BPF_JGE: |
a72dafaf JW |
8891 | case BPF_JGT: |
8892 | { | |
3f50f132 JF |
8893 | if (is_jmp32) { |
8894 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
8895 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
8896 | ||
8897 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
8898 | false_umax); | |
8899 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
8900 | true_umin); | |
8901 | } else { | |
8902 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
8903 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
8904 | ||
8905 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
8906 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
8907 | } | |
b03c9f9f | 8908 | break; |
a72dafaf | 8909 | } |
48461135 | 8910 | case BPF_JSGE: |
a72dafaf JW |
8911 | case BPF_JSGT: |
8912 | { | |
3f50f132 JF |
8913 | if (is_jmp32) { |
8914 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
8915 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 8916 | |
3f50f132 JF |
8917 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
8918 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
8919 | } else { | |
8920 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
8921 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
8922 | ||
8923 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
8924 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
8925 | } | |
48461135 | 8926 | break; |
a72dafaf | 8927 | } |
b4e432f1 | 8928 | case BPF_JLE: |
a72dafaf JW |
8929 | case BPF_JLT: |
8930 | { | |
3f50f132 JF |
8931 | if (is_jmp32) { |
8932 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
8933 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
8934 | ||
8935 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
8936 | false_umin); | |
8937 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
8938 | true_umax); | |
8939 | } else { | |
8940 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
8941 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
8942 | ||
8943 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
8944 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
8945 | } | |
b4e432f1 | 8946 | break; |
a72dafaf | 8947 | } |
b4e432f1 | 8948 | case BPF_JSLE: |
a72dafaf JW |
8949 | case BPF_JSLT: |
8950 | { | |
3f50f132 JF |
8951 | if (is_jmp32) { |
8952 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
8953 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 8954 | |
3f50f132 JF |
8955 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
8956 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
8957 | } else { | |
8958 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
8959 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
8960 | ||
8961 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
8962 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
8963 | } | |
b4e432f1 | 8964 | break; |
a72dafaf | 8965 | } |
48461135 | 8966 | default: |
0fc31b10 | 8967 | return; |
48461135 JB |
8968 | } |
8969 | ||
3f50f132 JF |
8970 | if (is_jmp32) { |
8971 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
8972 | tnum_subreg(false_32off)); | |
8973 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
8974 | tnum_subreg(true_32off)); | |
8975 | __reg_combine_32_into_64(false_reg); | |
8976 | __reg_combine_32_into_64(true_reg); | |
8977 | } else { | |
8978 | false_reg->var_off = false_64off; | |
8979 | true_reg->var_off = true_64off; | |
8980 | __reg_combine_64_into_32(false_reg); | |
8981 | __reg_combine_64_into_32(true_reg); | |
8982 | } | |
48461135 JB |
8983 | } |
8984 | ||
f1174f77 EC |
8985 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
8986 | * the variable reg. | |
48461135 JB |
8987 | */ |
8988 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
8989 | struct bpf_reg_state *false_reg, |
8990 | u64 val, u32 val32, | |
092ed096 | 8991 | u8 opcode, bool is_jmp32) |
48461135 | 8992 | { |
6d94e741 | 8993 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
8994 | /* This uses zero as "not present in table"; luckily the zero opcode, |
8995 | * BPF_JA, can't get here. | |
b03c9f9f | 8996 | */ |
0fc31b10 | 8997 | if (opcode) |
3f50f132 | 8998 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
8999 | } |
9000 | ||
9001 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
9002 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
9003 | struct bpf_reg_state *dst_reg) | |
9004 | { | |
b03c9f9f EC |
9005 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
9006 | dst_reg->umin_value); | |
9007 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
9008 | dst_reg->umax_value); | |
9009 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
9010 | dst_reg->smin_value); | |
9011 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
9012 | dst_reg->smax_value); | |
f1174f77 EC |
9013 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
9014 | dst_reg->var_off); | |
b03c9f9f EC |
9015 | /* We might have learned new bounds from the var_off. */ |
9016 | __update_reg_bounds(src_reg); | |
9017 | __update_reg_bounds(dst_reg); | |
9018 | /* We might have learned something about the sign bit. */ | |
9019 | __reg_deduce_bounds(src_reg); | |
9020 | __reg_deduce_bounds(dst_reg); | |
9021 | /* We might have learned some bits from the bounds. */ | |
9022 | __reg_bound_offset(src_reg); | |
9023 | __reg_bound_offset(dst_reg); | |
9024 | /* Intersecting with the old var_off might have improved our bounds | |
9025 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
9026 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
9027 | */ | |
9028 | __update_reg_bounds(src_reg); | |
9029 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
9030 | } |
9031 | ||
9032 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
9033 | struct bpf_reg_state *true_dst, | |
9034 | struct bpf_reg_state *false_src, | |
9035 | struct bpf_reg_state *false_dst, | |
9036 | u8 opcode) | |
9037 | { | |
9038 | switch (opcode) { | |
9039 | case BPF_JEQ: | |
9040 | __reg_combine_min_max(true_src, true_dst); | |
9041 | break; | |
9042 | case BPF_JNE: | |
9043 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 9044 | break; |
4cabc5b1 | 9045 | } |
48461135 JB |
9046 | } |
9047 | ||
fd978bf7 JS |
9048 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
9049 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 9050 | bool is_null) |
57a09bf0 | 9051 | { |
93c230e3 MKL |
9052 | if (reg_type_may_be_null(reg->type) && reg->id == id && |
9053 | !WARN_ON_ONCE(!reg->id)) { | |
f1174f77 EC |
9054 | /* Old offset (both fixed and variable parts) should |
9055 | * have been known-zero, because we don't allow pointer | |
9056 | * arithmetic on pointers that might be NULL. | |
9057 | */ | |
b03c9f9f EC |
9058 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
9059 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 9060 | reg->off)) { |
b03c9f9f EC |
9061 | __mark_reg_known_zero(reg); |
9062 | reg->off = 0; | |
f1174f77 EC |
9063 | } |
9064 | if (is_null) { | |
9065 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
9066 | /* We don't need id and ref_obj_id from this point |
9067 | * onwards anymore, thus we should better reset it, | |
9068 | * so that state pruning has chances to take effect. | |
9069 | */ | |
9070 | reg->id = 0; | |
9071 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
9072 | |
9073 | return; | |
9074 | } | |
9075 | ||
9076 | mark_ptr_not_null_reg(reg); | |
9077 | ||
9078 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 MKL |
9079 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
9080 | * in release_reg_references(). | |
9081 | * | |
9082 | * reg->id is still used by spin_lock ptr. Other | |
9083 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
9084 | */ |
9085 | reg->id = 0; | |
56f668df | 9086 | } |
57a09bf0 TG |
9087 | } |
9088 | } | |
9089 | ||
c6a9efa1 PC |
9090 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
9091 | bool is_null) | |
9092 | { | |
9093 | struct bpf_reg_state *reg; | |
9094 | int i; | |
9095 | ||
9096 | for (i = 0; i < MAX_BPF_REG; i++) | |
9097 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
9098 | ||
9099 | bpf_for_each_spilled_reg(i, state, reg) { | |
9100 | if (!reg) | |
9101 | continue; | |
9102 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
9103 | } | |
9104 | } | |
9105 | ||
57a09bf0 TG |
9106 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
9107 | * be folded together at some point. | |
9108 | */ | |
840b9615 JS |
9109 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
9110 | bool is_null) | |
57a09bf0 | 9111 | { |
f4d7e40a | 9112 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 9113 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 9114 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 9115 | u32 id = regs[regno].id; |
c6a9efa1 | 9116 | int i; |
57a09bf0 | 9117 | |
1b986589 MKL |
9118 | if (ref_obj_id && ref_obj_id == id && is_null) |
9119 | /* regs[regno] is in the " == NULL" branch. | |
9120 | * No one could have freed the reference state before | |
9121 | * doing the NULL check. | |
9122 | */ | |
9123 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 9124 | |
c6a9efa1 PC |
9125 | for (i = 0; i <= vstate->curframe; i++) |
9126 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
9127 | } |
9128 | ||
5beca081 DB |
9129 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
9130 | struct bpf_reg_state *dst_reg, | |
9131 | struct bpf_reg_state *src_reg, | |
9132 | struct bpf_verifier_state *this_branch, | |
9133 | struct bpf_verifier_state *other_branch) | |
9134 | { | |
9135 | if (BPF_SRC(insn->code) != BPF_X) | |
9136 | return false; | |
9137 | ||
092ed096 JW |
9138 | /* Pointers are always 64-bit. */ |
9139 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
9140 | return false; | |
9141 | ||
5beca081 DB |
9142 | switch (BPF_OP(insn->code)) { |
9143 | case BPF_JGT: | |
9144 | if ((dst_reg->type == PTR_TO_PACKET && | |
9145 | src_reg->type == PTR_TO_PACKET_END) || | |
9146 | (dst_reg->type == PTR_TO_PACKET_META && | |
9147 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9148 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
9149 | find_good_pkt_pointers(this_branch, dst_reg, | |
9150 | dst_reg->type, false); | |
6d94e741 | 9151 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
9152 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9153 | src_reg->type == PTR_TO_PACKET) || | |
9154 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9155 | src_reg->type == PTR_TO_PACKET_META)) { | |
9156 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
9157 | find_good_pkt_pointers(other_branch, src_reg, | |
9158 | src_reg->type, true); | |
6d94e741 | 9159 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
9160 | } else { |
9161 | return false; | |
9162 | } | |
9163 | break; | |
9164 | case BPF_JLT: | |
9165 | if ((dst_reg->type == PTR_TO_PACKET && | |
9166 | src_reg->type == PTR_TO_PACKET_END) || | |
9167 | (dst_reg->type == PTR_TO_PACKET_META && | |
9168 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9169 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
9170 | find_good_pkt_pointers(other_branch, dst_reg, | |
9171 | dst_reg->type, true); | |
6d94e741 | 9172 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
9173 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9174 | src_reg->type == PTR_TO_PACKET) || | |
9175 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9176 | src_reg->type == PTR_TO_PACKET_META)) { | |
9177 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
9178 | find_good_pkt_pointers(this_branch, src_reg, | |
9179 | src_reg->type, false); | |
6d94e741 | 9180 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
9181 | } else { |
9182 | return false; | |
9183 | } | |
9184 | break; | |
9185 | case BPF_JGE: | |
9186 | if ((dst_reg->type == PTR_TO_PACKET && | |
9187 | src_reg->type == PTR_TO_PACKET_END) || | |
9188 | (dst_reg->type == PTR_TO_PACKET_META && | |
9189 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9190 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
9191 | find_good_pkt_pointers(this_branch, dst_reg, | |
9192 | dst_reg->type, true); | |
6d94e741 | 9193 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
9194 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9195 | src_reg->type == PTR_TO_PACKET) || | |
9196 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9197 | src_reg->type == PTR_TO_PACKET_META)) { | |
9198 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
9199 | find_good_pkt_pointers(other_branch, src_reg, | |
9200 | src_reg->type, false); | |
6d94e741 | 9201 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
9202 | } else { |
9203 | return false; | |
9204 | } | |
9205 | break; | |
9206 | case BPF_JLE: | |
9207 | if ((dst_reg->type == PTR_TO_PACKET && | |
9208 | src_reg->type == PTR_TO_PACKET_END) || | |
9209 | (dst_reg->type == PTR_TO_PACKET_META && | |
9210 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9211 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
9212 | find_good_pkt_pointers(other_branch, dst_reg, | |
9213 | dst_reg->type, false); | |
6d94e741 | 9214 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
9215 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9216 | src_reg->type == PTR_TO_PACKET) || | |
9217 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9218 | src_reg->type == PTR_TO_PACKET_META)) { | |
9219 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
9220 | find_good_pkt_pointers(this_branch, src_reg, | |
9221 | src_reg->type, true); | |
6d94e741 | 9222 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
9223 | } else { |
9224 | return false; | |
9225 | } | |
9226 | break; | |
9227 | default: | |
9228 | return false; | |
9229 | } | |
9230 | ||
9231 | return true; | |
9232 | } | |
9233 | ||
75748837 AS |
9234 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
9235 | struct bpf_reg_state *known_reg) | |
9236 | { | |
9237 | struct bpf_func_state *state; | |
9238 | struct bpf_reg_state *reg; | |
9239 | int i, j; | |
9240 | ||
9241 | for (i = 0; i <= vstate->curframe; i++) { | |
9242 | state = vstate->frame[i]; | |
9243 | for (j = 0; j < MAX_BPF_REG; j++) { | |
9244 | reg = &state->regs[j]; | |
9245 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
9246 | *reg = *known_reg; | |
9247 | } | |
9248 | ||
9249 | bpf_for_each_spilled_reg(j, state, reg) { | |
9250 | if (!reg) | |
9251 | continue; | |
9252 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
9253 | *reg = *known_reg; | |
9254 | } | |
9255 | } | |
9256 | } | |
9257 | ||
58e2af8b | 9258 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
9259 | struct bpf_insn *insn, int *insn_idx) |
9260 | { | |
f4d7e40a AS |
9261 | struct bpf_verifier_state *this_branch = env->cur_state; |
9262 | struct bpf_verifier_state *other_branch; | |
9263 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 9264 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 9265 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 9266 | bool is_jmp32; |
fb8d251e | 9267 | int pred = -1; |
17a52670 AS |
9268 | int err; |
9269 | ||
092ed096 JW |
9270 | /* Only conditional jumps are expected to reach here. */ |
9271 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
9272 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
9273 | return -EINVAL; |
9274 | } | |
9275 | ||
9276 | if (BPF_SRC(insn->code) == BPF_X) { | |
9277 | if (insn->imm != 0) { | |
092ed096 | 9278 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
9279 | return -EINVAL; |
9280 | } | |
9281 | ||
9282 | /* check src1 operand */ | |
dc503a8a | 9283 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9284 | if (err) |
9285 | return err; | |
1be7f75d AS |
9286 | |
9287 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 9288 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
9289 | insn->src_reg); |
9290 | return -EACCES; | |
9291 | } | |
fb8d251e | 9292 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
9293 | } else { |
9294 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 9295 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
9296 | return -EINVAL; |
9297 | } | |
9298 | } | |
9299 | ||
9300 | /* check src2 operand */ | |
dc503a8a | 9301 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9302 | if (err) |
9303 | return err; | |
9304 | ||
1a0dc1ac | 9305 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 9306 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 9307 | |
3f50f132 JF |
9308 | if (BPF_SRC(insn->code) == BPF_K) { |
9309 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
9310 | } else if (src_reg->type == SCALAR_VALUE && | |
9311 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
9312 | pred = is_branch_taken(dst_reg, | |
9313 | tnum_subreg(src_reg->var_off).value, | |
9314 | opcode, | |
9315 | is_jmp32); | |
9316 | } else if (src_reg->type == SCALAR_VALUE && | |
9317 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
9318 | pred = is_branch_taken(dst_reg, | |
9319 | src_reg->var_off.value, | |
9320 | opcode, | |
9321 | is_jmp32); | |
6d94e741 AS |
9322 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
9323 | reg_is_pkt_pointer_any(src_reg) && | |
9324 | !is_jmp32) { | |
9325 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
9326 | } |
9327 | ||
b5dc0163 | 9328 | if (pred >= 0) { |
cac616db JF |
9329 | /* If we get here with a dst_reg pointer type it is because |
9330 | * above is_branch_taken() special cased the 0 comparison. | |
9331 | */ | |
9332 | if (!__is_pointer_value(false, dst_reg)) | |
9333 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
9334 | if (BPF_SRC(insn->code) == BPF_X && !err && |
9335 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
9336 | err = mark_chain_precision(env, insn->src_reg); |
9337 | if (err) | |
9338 | return err; | |
9339 | } | |
9183671a | 9340 | |
fb8d251e | 9341 | if (pred == 1) { |
9183671a DB |
9342 | /* Only follow the goto, ignore fall-through. If needed, push |
9343 | * the fall-through branch for simulation under speculative | |
9344 | * execution. | |
9345 | */ | |
9346 | if (!env->bypass_spec_v1 && | |
9347 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
9348 | *insn_idx)) | |
9349 | return -EFAULT; | |
fb8d251e AS |
9350 | *insn_idx += insn->off; |
9351 | return 0; | |
9352 | } else if (pred == 0) { | |
9183671a DB |
9353 | /* Only follow the fall-through branch, since that's where the |
9354 | * program will go. If needed, push the goto branch for | |
9355 | * simulation under speculative execution. | |
fb8d251e | 9356 | */ |
9183671a DB |
9357 | if (!env->bypass_spec_v1 && |
9358 | !sanitize_speculative_path(env, insn, | |
9359 | *insn_idx + insn->off + 1, | |
9360 | *insn_idx)) | |
9361 | return -EFAULT; | |
fb8d251e | 9362 | return 0; |
17a52670 AS |
9363 | } |
9364 | ||
979d63d5 DB |
9365 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
9366 | false); | |
17a52670 AS |
9367 | if (!other_branch) |
9368 | return -EFAULT; | |
f4d7e40a | 9369 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 9370 | |
48461135 JB |
9371 | /* detect if we are comparing against a constant value so we can adjust |
9372 | * our min/max values for our dst register. | |
f1174f77 EC |
9373 | * this is only legit if both are scalars (or pointers to the same |
9374 | * object, I suppose, but we don't support that right now), because | |
9375 | * otherwise the different base pointers mean the offsets aren't | |
9376 | * comparable. | |
48461135 JB |
9377 | */ |
9378 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 9379 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 9380 | |
f1174f77 | 9381 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
9382 | src_reg->type == SCALAR_VALUE) { |
9383 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
9384 | (is_jmp32 && |
9385 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 9386 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 9387 | dst_reg, |
3f50f132 JF |
9388 | src_reg->var_off.value, |
9389 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
9390 | opcode, is_jmp32); |
9391 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
9392 | (is_jmp32 && |
9393 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 9394 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 9395 | src_reg, |
3f50f132 JF |
9396 | dst_reg->var_off.value, |
9397 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
9398 | opcode, is_jmp32); |
9399 | else if (!is_jmp32 && | |
9400 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 9401 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
9402 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
9403 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 9404 | src_reg, dst_reg, opcode); |
e688c3db AS |
9405 | if (src_reg->id && |
9406 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
9407 | find_equal_scalars(this_branch, src_reg); |
9408 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
9409 | } | |
9410 | ||
f1174f77 EC |
9411 | } |
9412 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 9413 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
9414 | dst_reg, insn->imm, (u32)insn->imm, |
9415 | opcode, is_jmp32); | |
48461135 JB |
9416 | } |
9417 | ||
e688c3db AS |
9418 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
9419 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
9420 | find_equal_scalars(this_branch, dst_reg); |
9421 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
9422 | } | |
9423 | ||
092ed096 JW |
9424 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
9425 | * NOTE: these optimizations below are related with pointer comparison | |
9426 | * which will never be JMP32. | |
9427 | */ | |
9428 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 9429 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
9430 | reg_type_may_be_null(dst_reg->type)) { |
9431 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
9432 | * safe or unknown depending R == 0 or R != 0 conditional. |
9433 | */ | |
840b9615 JS |
9434 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
9435 | opcode == BPF_JNE); | |
9436 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
9437 | opcode == BPF_JEQ); | |
5beca081 DB |
9438 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
9439 | this_branch, other_branch) && | |
9440 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
9441 | verbose(env, "R%d pointer comparison prohibited\n", |
9442 | insn->dst_reg); | |
1be7f75d | 9443 | return -EACCES; |
17a52670 | 9444 | } |
06ee7115 | 9445 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 9446 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
9447 | return 0; |
9448 | } | |
9449 | ||
17a52670 | 9450 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 9451 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 9452 | { |
d8eca5bb | 9453 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 9454 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 9455 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 9456 | struct bpf_map *map; |
17a52670 AS |
9457 | int err; |
9458 | ||
9459 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 9460 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
9461 | return -EINVAL; |
9462 | } | |
9463 | if (insn->off != 0) { | |
61bd5218 | 9464 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
9465 | return -EINVAL; |
9466 | } | |
9467 | ||
dc503a8a | 9468 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
9469 | if (err) |
9470 | return err; | |
9471 | ||
4976b718 | 9472 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 9473 | if (insn->src_reg == 0) { |
6b173873 JK |
9474 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
9475 | ||
4976b718 | 9476 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 9477 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 9478 | return 0; |
6b173873 | 9479 | } |
17a52670 | 9480 | |
4976b718 HL |
9481 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
9482 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
9483 | ||
9484 | dst_reg->type = aux->btf_var.reg_type; | |
9485 | switch (dst_reg->type) { | |
9486 | case PTR_TO_MEM: | |
9487 | dst_reg->mem_size = aux->btf_var.mem_size; | |
9488 | break; | |
9489 | case PTR_TO_BTF_ID: | |
eaa6bcb7 | 9490 | case PTR_TO_PERCPU_BTF_ID: |
22dc4a0f | 9491 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
9492 | dst_reg->btf_id = aux->btf_var.btf_id; |
9493 | break; | |
9494 | default: | |
9495 | verbose(env, "bpf verifier is misconfigured\n"); | |
9496 | return -EFAULT; | |
9497 | } | |
9498 | return 0; | |
9499 | } | |
9500 | ||
69c087ba YS |
9501 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
9502 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
9503 | u32 subprogno = find_subprog(env, |
9504 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
9505 | |
9506 | if (!aux->func_info) { | |
9507 | verbose(env, "missing btf func_info\n"); | |
9508 | return -EINVAL; | |
9509 | } | |
9510 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
9511 | verbose(env, "callback function not static\n"); | |
9512 | return -EINVAL; | |
9513 | } | |
9514 | ||
9515 | dst_reg->type = PTR_TO_FUNC; | |
9516 | dst_reg->subprogno = subprogno; | |
9517 | return 0; | |
9518 | } | |
9519 | ||
d8eca5bb DB |
9520 | map = env->used_maps[aux->map_index]; |
9521 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 9522 | dst_reg->map_ptr = map; |
d8eca5bb | 9523 | |
387544bf AS |
9524 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
9525 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
9526 | dst_reg->type = PTR_TO_MAP_VALUE; |
9527 | dst_reg->off = aux->map_off; | |
d8eca5bb | 9528 | if (map_value_has_spin_lock(map)) |
4976b718 | 9529 | dst_reg->id = ++env->id_gen; |
387544bf AS |
9530 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
9531 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 9532 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
9533 | } else { |
9534 | verbose(env, "bpf verifier is misconfigured\n"); | |
9535 | return -EINVAL; | |
9536 | } | |
17a52670 | 9537 | |
17a52670 AS |
9538 | return 0; |
9539 | } | |
9540 | ||
96be4325 DB |
9541 | static bool may_access_skb(enum bpf_prog_type type) |
9542 | { | |
9543 | switch (type) { | |
9544 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
9545 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 9546 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
9547 | return true; |
9548 | default: | |
9549 | return false; | |
9550 | } | |
9551 | } | |
9552 | ||
ddd872bc AS |
9553 | /* verify safety of LD_ABS|LD_IND instructions: |
9554 | * - they can only appear in the programs where ctx == skb | |
9555 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
9556 | * preserve R6-R9, and store return value into R0 | |
9557 | * | |
9558 | * Implicit input: | |
9559 | * ctx == skb == R6 == CTX | |
9560 | * | |
9561 | * Explicit input: | |
9562 | * SRC == any register | |
9563 | * IMM == 32-bit immediate | |
9564 | * | |
9565 | * Output: | |
9566 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
9567 | */ | |
58e2af8b | 9568 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 9569 | { |
638f5b90 | 9570 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 9571 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 9572 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
9573 | int i, err; |
9574 | ||
7e40781c | 9575 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 9576 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
9577 | return -EINVAL; |
9578 | } | |
9579 | ||
e0cea7ce DB |
9580 | if (!env->ops->gen_ld_abs) { |
9581 | verbose(env, "bpf verifier is misconfigured\n"); | |
9582 | return -EINVAL; | |
9583 | } | |
9584 | ||
ddd872bc | 9585 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 9586 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 9587 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 9588 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
9589 | return -EINVAL; |
9590 | } | |
9591 | ||
9592 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 9593 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
9594 | if (err) |
9595 | return err; | |
9596 | ||
fd978bf7 JS |
9597 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
9598 | * gen_ld_abs() may terminate the program at runtime, leading to | |
9599 | * reference leak. | |
9600 | */ | |
9601 | err = check_reference_leak(env); | |
9602 | if (err) { | |
9603 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
9604 | return err; | |
9605 | } | |
9606 | ||
d83525ca AS |
9607 | if (env->cur_state->active_spin_lock) { |
9608 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
9609 | return -EINVAL; | |
9610 | } | |
9611 | ||
6d4f151a | 9612 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
9613 | verbose(env, |
9614 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
9615 | return -EINVAL; |
9616 | } | |
9617 | ||
9618 | if (mode == BPF_IND) { | |
9619 | /* check explicit source operand */ | |
dc503a8a | 9620 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
9621 | if (err) |
9622 | return err; | |
9623 | } | |
9624 | ||
6d4f151a DB |
9625 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
9626 | if (err < 0) | |
9627 | return err; | |
9628 | ||
ddd872bc | 9629 | /* reset caller saved regs to unreadable */ |
dc503a8a | 9630 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 9631 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
9632 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
9633 | } | |
ddd872bc AS |
9634 | |
9635 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
9636 | * the value fetched from the packet. |
9637 | * Already marked as written above. | |
ddd872bc | 9638 | */ |
61bd5218 | 9639 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
9640 | /* ld_abs load up to 32-bit skb data. */ |
9641 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
9642 | return 0; |
9643 | } | |
9644 | ||
390ee7e2 AS |
9645 | static int check_return_code(struct bpf_verifier_env *env) |
9646 | { | |
5cf1e914 | 9647 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 9648 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
9649 | struct bpf_reg_state *reg; |
9650 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 9651 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 9652 | int err; |
bfc6bb74 AS |
9653 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
9654 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 9655 | |
9e4e01df | 9656 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
f782e2c3 DB |
9657 | if (!is_subprog && |
9658 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || | |
7e40781c | 9659 | prog_type == BPF_PROG_TYPE_LSM) && |
27ae7997 MKL |
9660 | !prog->aux->attach_func_proto->type) |
9661 | return 0; | |
9662 | ||
8fb33b60 | 9663 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
9664 | * to return the value from eBPF program. |
9665 | * Make sure that it's readable at this time | |
9666 | * of bpf_exit, which means that program wrote | |
9667 | * something into it earlier | |
9668 | */ | |
9669 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
9670 | if (err) | |
9671 | return err; | |
9672 | ||
9673 | if (is_pointer_value(env, BPF_REG_0)) { | |
9674 | verbose(env, "R0 leaks addr as return value\n"); | |
9675 | return -EACCES; | |
9676 | } | |
390ee7e2 | 9677 | |
f782e2c3 | 9678 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
9679 | |
9680 | if (frame->in_async_callback_fn) { | |
9681 | /* enforce return zero from async callbacks like timer */ | |
9682 | if (reg->type != SCALAR_VALUE) { | |
9683 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
9684 | reg_type_str[reg->type]); | |
9685 | return -EINVAL; | |
9686 | } | |
9687 | ||
9688 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
9689 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
9690 | return -EINVAL; | |
9691 | } | |
9692 | return 0; | |
9693 | } | |
9694 | ||
f782e2c3 DB |
9695 | if (is_subprog) { |
9696 | if (reg->type != SCALAR_VALUE) { | |
9697 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
9698 | reg_type_str[reg->type]); | |
9699 | return -EINVAL; | |
9700 | } | |
9701 | return 0; | |
9702 | } | |
9703 | ||
7e40781c | 9704 | switch (prog_type) { |
983695fa DB |
9705 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
9706 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
9707 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
9708 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
9709 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
9710 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
9711 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 9712 | range = tnum_range(1, 1); |
77241217 SF |
9713 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
9714 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
9715 | range = tnum_range(0, 3); | |
ed4ed404 | 9716 | break; |
390ee7e2 | 9717 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 9718 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
9719 | range = tnum_range(0, 3); | |
9720 | enforce_attach_type_range = tnum_range(2, 3); | |
9721 | } | |
ed4ed404 | 9722 | break; |
390ee7e2 AS |
9723 | case BPF_PROG_TYPE_CGROUP_SOCK: |
9724 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 9725 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 9726 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 9727 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 9728 | break; |
15ab09bd AS |
9729 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
9730 | if (!env->prog->aux->attach_btf_id) | |
9731 | return 0; | |
9732 | range = tnum_const(0); | |
9733 | break; | |
15d83c4d | 9734 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
9735 | switch (env->prog->expected_attach_type) { |
9736 | case BPF_TRACE_FENTRY: | |
9737 | case BPF_TRACE_FEXIT: | |
9738 | range = tnum_const(0); | |
9739 | break; | |
9740 | case BPF_TRACE_RAW_TP: | |
9741 | case BPF_MODIFY_RETURN: | |
15d83c4d | 9742 | return 0; |
2ec0616e DB |
9743 | case BPF_TRACE_ITER: |
9744 | break; | |
e92888c7 YS |
9745 | default: |
9746 | return -ENOTSUPP; | |
9747 | } | |
15d83c4d | 9748 | break; |
e9ddbb77 JS |
9749 | case BPF_PROG_TYPE_SK_LOOKUP: |
9750 | range = tnum_range(SK_DROP, SK_PASS); | |
9751 | break; | |
e92888c7 YS |
9752 | case BPF_PROG_TYPE_EXT: |
9753 | /* freplace program can return anything as its return value | |
9754 | * depends on the to-be-replaced kernel func or bpf program. | |
9755 | */ | |
390ee7e2 AS |
9756 | default: |
9757 | return 0; | |
9758 | } | |
9759 | ||
390ee7e2 | 9760 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 9761 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
9762 | reg_type_str[reg->type]); |
9763 | return -EINVAL; | |
9764 | } | |
9765 | ||
9766 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 9767 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
390ee7e2 AS |
9768 | return -EINVAL; |
9769 | } | |
5cf1e914 | 9770 | |
9771 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
9772 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
9773 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
9774 | return 0; |
9775 | } | |
9776 | ||
475fb78f AS |
9777 | /* non-recursive DFS pseudo code |
9778 | * 1 procedure DFS-iterative(G,v): | |
9779 | * 2 label v as discovered | |
9780 | * 3 let S be a stack | |
9781 | * 4 S.push(v) | |
9782 | * 5 while S is not empty | |
9783 | * 6 t <- S.pop() | |
9784 | * 7 if t is what we're looking for: | |
9785 | * 8 return t | |
9786 | * 9 for all edges e in G.adjacentEdges(t) do | |
9787 | * 10 if edge e is already labelled | |
9788 | * 11 continue with the next edge | |
9789 | * 12 w <- G.adjacentVertex(t,e) | |
9790 | * 13 if vertex w is not discovered and not explored | |
9791 | * 14 label e as tree-edge | |
9792 | * 15 label w as discovered | |
9793 | * 16 S.push(w) | |
9794 | * 17 continue at 5 | |
9795 | * 18 else if vertex w is discovered | |
9796 | * 19 label e as back-edge | |
9797 | * 20 else | |
9798 | * 21 // vertex w is explored | |
9799 | * 22 label e as forward- or cross-edge | |
9800 | * 23 label t as explored | |
9801 | * 24 S.pop() | |
9802 | * | |
9803 | * convention: | |
9804 | * 0x10 - discovered | |
9805 | * 0x11 - discovered and fall-through edge labelled | |
9806 | * 0x12 - discovered and fall-through and branch edges labelled | |
9807 | * 0x20 - explored | |
9808 | */ | |
9809 | ||
9810 | enum { | |
9811 | DISCOVERED = 0x10, | |
9812 | EXPLORED = 0x20, | |
9813 | FALLTHROUGH = 1, | |
9814 | BRANCH = 2, | |
9815 | }; | |
9816 | ||
dc2a4ebc AS |
9817 | static u32 state_htab_size(struct bpf_verifier_env *env) |
9818 | { | |
9819 | return env->prog->len; | |
9820 | } | |
9821 | ||
5d839021 AS |
9822 | static struct bpf_verifier_state_list **explored_state( |
9823 | struct bpf_verifier_env *env, | |
9824 | int idx) | |
9825 | { | |
dc2a4ebc AS |
9826 | struct bpf_verifier_state *cur = env->cur_state; |
9827 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
9828 | ||
9829 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
9830 | } |
9831 | ||
9832 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
9833 | { | |
a8f500af | 9834 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 9835 | } |
f1bca824 | 9836 | |
59e2e27d WAF |
9837 | enum { |
9838 | DONE_EXPLORING = 0, | |
9839 | KEEP_EXPLORING = 1, | |
9840 | }; | |
9841 | ||
475fb78f AS |
9842 | /* t, w, e - match pseudo-code above: |
9843 | * t - index of current instruction | |
9844 | * w - next instruction | |
9845 | * e - edge | |
9846 | */ | |
2589726d AS |
9847 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
9848 | bool loop_ok) | |
475fb78f | 9849 | { |
7df737e9 AS |
9850 | int *insn_stack = env->cfg.insn_stack; |
9851 | int *insn_state = env->cfg.insn_state; | |
9852 | ||
475fb78f | 9853 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 9854 | return DONE_EXPLORING; |
475fb78f AS |
9855 | |
9856 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 9857 | return DONE_EXPLORING; |
475fb78f AS |
9858 | |
9859 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 9860 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 9861 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
9862 | return -EINVAL; |
9863 | } | |
9864 | ||
f1bca824 AS |
9865 | if (e == BRANCH) |
9866 | /* mark branch target for state pruning */ | |
5d839021 | 9867 | init_explored_state(env, w); |
f1bca824 | 9868 | |
475fb78f AS |
9869 | if (insn_state[w] == 0) { |
9870 | /* tree-edge */ | |
9871 | insn_state[t] = DISCOVERED | e; | |
9872 | insn_state[w] = DISCOVERED; | |
7df737e9 | 9873 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 9874 | return -E2BIG; |
7df737e9 | 9875 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 9876 | return KEEP_EXPLORING; |
475fb78f | 9877 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 9878 | if (loop_ok && env->bpf_capable) |
59e2e27d | 9879 | return DONE_EXPLORING; |
d9762e84 MKL |
9880 | verbose_linfo(env, t, "%d: ", t); |
9881 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 9882 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
9883 | return -EINVAL; |
9884 | } else if (insn_state[w] == EXPLORED) { | |
9885 | /* forward- or cross-edge */ | |
9886 | insn_state[t] = DISCOVERED | e; | |
9887 | } else { | |
61bd5218 | 9888 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
9889 | return -EFAULT; |
9890 | } | |
59e2e27d WAF |
9891 | return DONE_EXPLORING; |
9892 | } | |
9893 | ||
efdb22de YS |
9894 | static int visit_func_call_insn(int t, int insn_cnt, |
9895 | struct bpf_insn *insns, | |
9896 | struct bpf_verifier_env *env, | |
9897 | bool visit_callee) | |
9898 | { | |
9899 | int ret; | |
9900 | ||
9901 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
9902 | if (ret) | |
9903 | return ret; | |
9904 | ||
9905 | if (t + 1 < insn_cnt) | |
9906 | init_explored_state(env, t + 1); | |
9907 | if (visit_callee) { | |
9908 | init_explored_state(env, t); | |
86fc6ee6 AS |
9909 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
9910 | /* It's ok to allow recursion from CFG point of | |
9911 | * view. __check_func_call() will do the actual | |
9912 | * check. | |
9913 | */ | |
9914 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
9915 | } |
9916 | return ret; | |
9917 | } | |
9918 | ||
59e2e27d WAF |
9919 | /* Visits the instruction at index t and returns one of the following: |
9920 | * < 0 - an error occurred | |
9921 | * DONE_EXPLORING - the instruction was fully explored | |
9922 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
9923 | */ | |
9924 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) | |
9925 | { | |
9926 | struct bpf_insn *insns = env->prog->insnsi; | |
9927 | int ret; | |
9928 | ||
69c087ba YS |
9929 | if (bpf_pseudo_func(insns + t)) |
9930 | return visit_func_call_insn(t, insn_cnt, insns, env, true); | |
9931 | ||
59e2e27d WAF |
9932 | /* All non-branch instructions have a single fall-through edge. */ |
9933 | if (BPF_CLASS(insns[t].code) != BPF_JMP && | |
9934 | BPF_CLASS(insns[t].code) != BPF_JMP32) | |
9935 | return push_insn(t, t + 1, FALLTHROUGH, env, false); | |
9936 | ||
9937 | switch (BPF_OP(insns[t].code)) { | |
9938 | case BPF_EXIT: | |
9939 | return DONE_EXPLORING; | |
9940 | ||
9941 | case BPF_CALL: | |
bfc6bb74 AS |
9942 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
9943 | /* Mark this call insn to trigger is_state_visited() check | |
9944 | * before call itself is processed by __check_func_call(). | |
9945 | * Otherwise new async state will be pushed for further | |
9946 | * exploration. | |
9947 | */ | |
9948 | init_explored_state(env, t); | |
efdb22de YS |
9949 | return visit_func_call_insn(t, insn_cnt, insns, env, |
9950 | insns[t].src_reg == BPF_PSEUDO_CALL); | |
59e2e27d WAF |
9951 | |
9952 | case BPF_JA: | |
9953 | if (BPF_SRC(insns[t].code) != BPF_K) | |
9954 | return -EINVAL; | |
9955 | ||
9956 | /* unconditional jump with single edge */ | |
9957 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, | |
9958 | true); | |
9959 | if (ret) | |
9960 | return ret; | |
9961 | ||
9962 | /* unconditional jmp is not a good pruning point, | |
9963 | * but it's marked, since backtracking needs | |
9964 | * to record jmp history in is_state_visited(). | |
9965 | */ | |
9966 | init_explored_state(env, t + insns[t].off + 1); | |
9967 | /* tell verifier to check for equivalent states | |
9968 | * after every call and jump | |
9969 | */ | |
9970 | if (t + 1 < insn_cnt) | |
9971 | init_explored_state(env, t + 1); | |
9972 | ||
9973 | return ret; | |
9974 | ||
9975 | default: | |
9976 | /* conditional jump with two edges */ | |
9977 | init_explored_state(env, t); | |
9978 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); | |
9979 | if (ret) | |
9980 | return ret; | |
9981 | ||
9982 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); | |
9983 | } | |
475fb78f AS |
9984 | } |
9985 | ||
9986 | /* non-recursive depth-first-search to detect loops in BPF program | |
9987 | * loop == back-edge in directed graph | |
9988 | */ | |
58e2af8b | 9989 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 9990 | { |
475fb78f | 9991 | int insn_cnt = env->prog->len; |
7df737e9 | 9992 | int *insn_stack, *insn_state; |
475fb78f | 9993 | int ret = 0; |
59e2e27d | 9994 | int i; |
475fb78f | 9995 | |
7df737e9 | 9996 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
9997 | if (!insn_state) |
9998 | return -ENOMEM; | |
9999 | ||
7df737e9 | 10000 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 10001 | if (!insn_stack) { |
71dde681 | 10002 | kvfree(insn_state); |
475fb78f AS |
10003 | return -ENOMEM; |
10004 | } | |
10005 | ||
10006 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
10007 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 10008 | env->cfg.cur_stack = 1; |
475fb78f | 10009 | |
59e2e27d WAF |
10010 | while (env->cfg.cur_stack > 0) { |
10011 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 10012 | |
59e2e27d WAF |
10013 | ret = visit_insn(t, insn_cnt, env); |
10014 | switch (ret) { | |
10015 | case DONE_EXPLORING: | |
10016 | insn_state[t] = EXPLORED; | |
10017 | env->cfg.cur_stack--; | |
10018 | break; | |
10019 | case KEEP_EXPLORING: | |
10020 | break; | |
10021 | default: | |
10022 | if (ret > 0) { | |
10023 | verbose(env, "visit_insn internal bug\n"); | |
10024 | ret = -EFAULT; | |
475fb78f | 10025 | } |
475fb78f | 10026 | goto err_free; |
59e2e27d | 10027 | } |
475fb78f AS |
10028 | } |
10029 | ||
59e2e27d | 10030 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 10031 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
10032 | ret = -EFAULT; |
10033 | goto err_free; | |
10034 | } | |
475fb78f | 10035 | |
475fb78f AS |
10036 | for (i = 0; i < insn_cnt; i++) { |
10037 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 10038 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
10039 | ret = -EINVAL; |
10040 | goto err_free; | |
10041 | } | |
10042 | } | |
10043 | ret = 0; /* cfg looks good */ | |
10044 | ||
10045 | err_free: | |
71dde681 AS |
10046 | kvfree(insn_state); |
10047 | kvfree(insn_stack); | |
7df737e9 | 10048 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
10049 | return ret; |
10050 | } | |
10051 | ||
09b28d76 AS |
10052 | static int check_abnormal_return(struct bpf_verifier_env *env) |
10053 | { | |
10054 | int i; | |
10055 | ||
10056 | for (i = 1; i < env->subprog_cnt; i++) { | |
10057 | if (env->subprog_info[i].has_ld_abs) { | |
10058 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
10059 | return -EINVAL; | |
10060 | } | |
10061 | if (env->subprog_info[i].has_tail_call) { | |
10062 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
10063 | return -EINVAL; | |
10064 | } | |
10065 | } | |
10066 | return 0; | |
10067 | } | |
10068 | ||
838e9690 YS |
10069 | /* The minimum supported BTF func info size */ |
10070 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
10071 | #define MAX_FUNCINFO_REC_SIZE 252 | |
10072 | ||
c454a46b MKL |
10073 | static int check_btf_func(struct bpf_verifier_env *env, |
10074 | const union bpf_attr *attr, | |
af2ac3e1 | 10075 | bpfptr_t uattr) |
838e9690 | 10076 | { |
09b28d76 | 10077 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 10078 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 10079 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 10080 | struct bpf_func_info *krecord; |
8c1b6e69 | 10081 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
10082 | struct bpf_prog *prog; |
10083 | const struct btf *btf; | |
af2ac3e1 | 10084 | bpfptr_t urecord; |
d0b2818e | 10085 | u32 prev_offset = 0; |
09b28d76 | 10086 | bool scalar_return; |
e7ed83d6 | 10087 | int ret = -ENOMEM; |
838e9690 YS |
10088 | |
10089 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
10090 | if (!nfuncs) { |
10091 | if (check_abnormal_return(env)) | |
10092 | return -EINVAL; | |
838e9690 | 10093 | return 0; |
09b28d76 | 10094 | } |
838e9690 YS |
10095 | |
10096 | if (nfuncs != env->subprog_cnt) { | |
10097 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
10098 | return -EINVAL; | |
10099 | } | |
10100 | ||
10101 | urec_size = attr->func_info_rec_size; | |
10102 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
10103 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
10104 | urec_size % sizeof(u32)) { | |
10105 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
10106 | return -EINVAL; | |
10107 | } | |
10108 | ||
c454a46b MKL |
10109 | prog = env->prog; |
10110 | btf = prog->aux->btf; | |
838e9690 | 10111 | |
af2ac3e1 | 10112 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
10113 | min_size = min_t(u32, krec_size, urec_size); |
10114 | ||
ba64e7d8 | 10115 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
10116 | if (!krecord) |
10117 | return -ENOMEM; | |
8c1b6e69 AS |
10118 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
10119 | if (!info_aux) | |
10120 | goto err_free; | |
ba64e7d8 | 10121 | |
838e9690 YS |
10122 | for (i = 0; i < nfuncs; i++) { |
10123 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
10124 | if (ret) { | |
10125 | if (ret == -E2BIG) { | |
10126 | verbose(env, "nonzero tailing record in func info"); | |
10127 | /* set the size kernel expects so loader can zero | |
10128 | * out the rest of the record. | |
10129 | */ | |
af2ac3e1 AS |
10130 | if (copy_to_bpfptr_offset(uattr, |
10131 | offsetof(union bpf_attr, func_info_rec_size), | |
10132 | &min_size, sizeof(min_size))) | |
838e9690 YS |
10133 | ret = -EFAULT; |
10134 | } | |
c454a46b | 10135 | goto err_free; |
838e9690 YS |
10136 | } |
10137 | ||
af2ac3e1 | 10138 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 10139 | ret = -EFAULT; |
c454a46b | 10140 | goto err_free; |
838e9690 YS |
10141 | } |
10142 | ||
d30d42e0 | 10143 | /* check insn_off */ |
09b28d76 | 10144 | ret = -EINVAL; |
838e9690 | 10145 | if (i == 0) { |
d30d42e0 | 10146 | if (krecord[i].insn_off) { |
838e9690 | 10147 | verbose(env, |
d30d42e0 MKL |
10148 | "nonzero insn_off %u for the first func info record", |
10149 | krecord[i].insn_off); | |
c454a46b | 10150 | goto err_free; |
838e9690 | 10151 | } |
d30d42e0 | 10152 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
10153 | verbose(env, |
10154 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 10155 | krecord[i].insn_off, prev_offset); |
c454a46b | 10156 | goto err_free; |
838e9690 YS |
10157 | } |
10158 | ||
d30d42e0 | 10159 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 10160 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 10161 | goto err_free; |
838e9690 YS |
10162 | } |
10163 | ||
10164 | /* check type_id */ | |
ba64e7d8 | 10165 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 10166 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 10167 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 10168 | krecord[i].type_id); |
c454a46b | 10169 | goto err_free; |
838e9690 | 10170 | } |
51c39bb1 | 10171 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
10172 | |
10173 | func_proto = btf_type_by_id(btf, type->type); | |
10174 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
10175 | /* btf_func_check() already verified it during BTF load */ | |
10176 | goto err_free; | |
10177 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
10178 | scalar_return = | |
10179 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); | |
10180 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { | |
10181 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
10182 | goto err_free; | |
10183 | } | |
10184 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
10185 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
10186 | goto err_free; | |
10187 | } | |
10188 | ||
d30d42e0 | 10189 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 10190 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
10191 | } |
10192 | ||
ba64e7d8 YS |
10193 | prog->aux->func_info = krecord; |
10194 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 10195 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
10196 | return 0; |
10197 | ||
c454a46b | 10198 | err_free: |
ba64e7d8 | 10199 | kvfree(krecord); |
8c1b6e69 | 10200 | kfree(info_aux); |
838e9690 YS |
10201 | return ret; |
10202 | } | |
10203 | ||
ba64e7d8 YS |
10204 | static void adjust_btf_func(struct bpf_verifier_env *env) |
10205 | { | |
8c1b6e69 | 10206 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
10207 | int i; |
10208 | ||
8c1b6e69 | 10209 | if (!aux->func_info) |
ba64e7d8 YS |
10210 | return; |
10211 | ||
10212 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 10213 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
10214 | } |
10215 | ||
c454a46b MKL |
10216 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
10217 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
10218 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
10219 | ||
10220 | static int check_btf_line(struct bpf_verifier_env *env, | |
10221 | const union bpf_attr *attr, | |
af2ac3e1 | 10222 | bpfptr_t uattr) |
c454a46b MKL |
10223 | { |
10224 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
10225 | struct bpf_subprog_info *sub; | |
10226 | struct bpf_line_info *linfo; | |
10227 | struct bpf_prog *prog; | |
10228 | const struct btf *btf; | |
af2ac3e1 | 10229 | bpfptr_t ulinfo; |
c454a46b MKL |
10230 | int err; |
10231 | ||
10232 | nr_linfo = attr->line_info_cnt; | |
10233 | if (!nr_linfo) | |
10234 | return 0; | |
0e6491b5 BC |
10235 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
10236 | return -EINVAL; | |
c454a46b MKL |
10237 | |
10238 | rec_size = attr->line_info_rec_size; | |
10239 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
10240 | rec_size > MAX_LINEINFO_REC_SIZE || | |
10241 | rec_size & (sizeof(u32) - 1)) | |
10242 | return -EINVAL; | |
10243 | ||
10244 | /* Need to zero it in case the userspace may | |
10245 | * pass in a smaller bpf_line_info object. | |
10246 | */ | |
10247 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
10248 | GFP_KERNEL | __GFP_NOWARN); | |
10249 | if (!linfo) | |
10250 | return -ENOMEM; | |
10251 | ||
10252 | prog = env->prog; | |
10253 | btf = prog->aux->btf; | |
10254 | ||
10255 | s = 0; | |
10256 | sub = env->subprog_info; | |
af2ac3e1 | 10257 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
10258 | expected_size = sizeof(struct bpf_line_info); |
10259 | ncopy = min_t(u32, expected_size, rec_size); | |
10260 | for (i = 0; i < nr_linfo; i++) { | |
10261 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
10262 | if (err) { | |
10263 | if (err == -E2BIG) { | |
10264 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
10265 | if (copy_to_bpfptr_offset(uattr, |
10266 | offsetof(union bpf_attr, line_info_rec_size), | |
10267 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
10268 | err = -EFAULT; |
10269 | } | |
10270 | goto err_free; | |
10271 | } | |
10272 | ||
af2ac3e1 | 10273 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
10274 | err = -EFAULT; |
10275 | goto err_free; | |
10276 | } | |
10277 | ||
10278 | /* | |
10279 | * Check insn_off to ensure | |
10280 | * 1) strictly increasing AND | |
10281 | * 2) bounded by prog->len | |
10282 | * | |
10283 | * The linfo[0].insn_off == 0 check logically falls into | |
10284 | * the later "missing bpf_line_info for func..." case | |
10285 | * because the first linfo[0].insn_off must be the | |
10286 | * first sub also and the first sub must have | |
10287 | * subprog_info[0].start == 0. | |
10288 | */ | |
10289 | if ((i && linfo[i].insn_off <= prev_offset) || | |
10290 | linfo[i].insn_off >= prog->len) { | |
10291 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
10292 | i, linfo[i].insn_off, prev_offset, | |
10293 | prog->len); | |
10294 | err = -EINVAL; | |
10295 | goto err_free; | |
10296 | } | |
10297 | ||
fdbaa0be MKL |
10298 | if (!prog->insnsi[linfo[i].insn_off].code) { |
10299 | verbose(env, | |
10300 | "Invalid insn code at line_info[%u].insn_off\n", | |
10301 | i); | |
10302 | err = -EINVAL; | |
10303 | goto err_free; | |
10304 | } | |
10305 | ||
23127b33 MKL |
10306 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
10307 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
10308 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
10309 | err = -EINVAL; | |
10310 | goto err_free; | |
10311 | } | |
10312 | ||
10313 | if (s != env->subprog_cnt) { | |
10314 | if (linfo[i].insn_off == sub[s].start) { | |
10315 | sub[s].linfo_idx = i; | |
10316 | s++; | |
10317 | } else if (sub[s].start < linfo[i].insn_off) { | |
10318 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
10319 | err = -EINVAL; | |
10320 | goto err_free; | |
10321 | } | |
10322 | } | |
10323 | ||
10324 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 10325 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
10326 | } |
10327 | ||
10328 | if (s != env->subprog_cnt) { | |
10329 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
10330 | env->subprog_cnt - s, s); | |
10331 | err = -EINVAL; | |
10332 | goto err_free; | |
10333 | } | |
10334 | ||
10335 | prog->aux->linfo = linfo; | |
10336 | prog->aux->nr_linfo = nr_linfo; | |
10337 | ||
10338 | return 0; | |
10339 | ||
10340 | err_free: | |
10341 | kvfree(linfo); | |
10342 | return err; | |
10343 | } | |
10344 | ||
fbd94c7a AS |
10345 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
10346 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
10347 | ||
10348 | static int check_core_relo(struct bpf_verifier_env *env, | |
10349 | const union bpf_attr *attr, | |
10350 | bpfptr_t uattr) | |
10351 | { | |
10352 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
10353 | struct bpf_core_relo core_relo = {}; | |
10354 | struct bpf_prog *prog = env->prog; | |
10355 | const struct btf *btf = prog->aux->btf; | |
10356 | struct bpf_core_ctx ctx = { | |
10357 | .log = &env->log, | |
10358 | .btf = btf, | |
10359 | }; | |
10360 | bpfptr_t u_core_relo; | |
10361 | int err; | |
10362 | ||
10363 | nr_core_relo = attr->core_relo_cnt; | |
10364 | if (!nr_core_relo) | |
10365 | return 0; | |
10366 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
10367 | return -EINVAL; | |
10368 | ||
10369 | rec_size = attr->core_relo_rec_size; | |
10370 | if (rec_size < MIN_CORE_RELO_SIZE || | |
10371 | rec_size > MAX_CORE_RELO_SIZE || | |
10372 | rec_size % sizeof(u32)) | |
10373 | return -EINVAL; | |
10374 | ||
10375 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
10376 | expected_size = sizeof(struct bpf_core_relo); | |
10377 | ncopy = min_t(u32, expected_size, rec_size); | |
10378 | ||
10379 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
10380 | * relocation record one at a time. | |
10381 | */ | |
10382 | for (i = 0; i < nr_core_relo; i++) { | |
10383 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
10384 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
10385 | if (err) { | |
10386 | if (err == -E2BIG) { | |
10387 | verbose(env, "nonzero tailing record in core_relo"); | |
10388 | if (copy_to_bpfptr_offset(uattr, | |
10389 | offsetof(union bpf_attr, core_relo_rec_size), | |
10390 | &expected_size, sizeof(expected_size))) | |
10391 | err = -EFAULT; | |
10392 | } | |
10393 | break; | |
10394 | } | |
10395 | ||
10396 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
10397 | err = -EFAULT; | |
10398 | break; | |
10399 | } | |
10400 | ||
10401 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
10402 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
10403 | i, core_relo.insn_off, prog->len); | |
10404 | err = -EINVAL; | |
10405 | break; | |
10406 | } | |
10407 | ||
10408 | err = bpf_core_apply(&ctx, &core_relo, i, | |
10409 | &prog->insnsi[core_relo.insn_off / 8]); | |
10410 | if (err) | |
10411 | break; | |
10412 | bpfptr_add(&u_core_relo, rec_size); | |
10413 | } | |
10414 | return err; | |
10415 | } | |
10416 | ||
c454a46b MKL |
10417 | static int check_btf_info(struct bpf_verifier_env *env, |
10418 | const union bpf_attr *attr, | |
af2ac3e1 | 10419 | bpfptr_t uattr) |
c454a46b MKL |
10420 | { |
10421 | struct btf *btf; | |
10422 | int err; | |
10423 | ||
09b28d76 AS |
10424 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
10425 | if (check_abnormal_return(env)) | |
10426 | return -EINVAL; | |
c454a46b | 10427 | return 0; |
09b28d76 | 10428 | } |
c454a46b MKL |
10429 | |
10430 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
10431 | if (IS_ERR(btf)) | |
10432 | return PTR_ERR(btf); | |
350a5c4d AS |
10433 | if (btf_is_kernel(btf)) { |
10434 | btf_put(btf); | |
10435 | return -EACCES; | |
10436 | } | |
c454a46b MKL |
10437 | env->prog->aux->btf = btf; |
10438 | ||
10439 | err = check_btf_func(env, attr, uattr); | |
10440 | if (err) | |
10441 | return err; | |
10442 | ||
10443 | err = check_btf_line(env, attr, uattr); | |
10444 | if (err) | |
10445 | return err; | |
10446 | ||
fbd94c7a AS |
10447 | err = check_core_relo(env, attr, uattr); |
10448 | if (err) | |
10449 | return err; | |
10450 | ||
c454a46b | 10451 | return 0; |
ba64e7d8 YS |
10452 | } |
10453 | ||
f1174f77 EC |
10454 | /* check %cur's range satisfies %old's */ |
10455 | static bool range_within(struct bpf_reg_state *old, | |
10456 | struct bpf_reg_state *cur) | |
10457 | { | |
b03c9f9f EC |
10458 | return old->umin_value <= cur->umin_value && |
10459 | old->umax_value >= cur->umax_value && | |
10460 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
10461 | old->smax_value >= cur->smax_value && |
10462 | old->u32_min_value <= cur->u32_min_value && | |
10463 | old->u32_max_value >= cur->u32_max_value && | |
10464 | old->s32_min_value <= cur->s32_min_value && | |
10465 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
10466 | } |
10467 | ||
f1174f77 EC |
10468 | /* If in the old state two registers had the same id, then they need to have |
10469 | * the same id in the new state as well. But that id could be different from | |
10470 | * the old state, so we need to track the mapping from old to new ids. | |
10471 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
10472 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
10473 | * regs with a different old id could still have new id 9, we don't care about | |
10474 | * that. | |
10475 | * So we look through our idmap to see if this old id has been seen before. If | |
10476 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 10477 | */ |
c9e73e3d | 10478 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 10479 | { |
f1174f77 | 10480 | unsigned int i; |
969bf05e | 10481 | |
c9e73e3d | 10482 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
10483 | if (!idmap[i].old) { |
10484 | /* Reached an empty slot; haven't seen this id before */ | |
10485 | idmap[i].old = old_id; | |
10486 | idmap[i].cur = cur_id; | |
10487 | return true; | |
10488 | } | |
10489 | if (idmap[i].old == old_id) | |
10490 | return idmap[i].cur == cur_id; | |
10491 | } | |
10492 | /* We ran out of idmap slots, which should be impossible */ | |
10493 | WARN_ON_ONCE(1); | |
10494 | return false; | |
10495 | } | |
10496 | ||
9242b5f5 AS |
10497 | static void clean_func_state(struct bpf_verifier_env *env, |
10498 | struct bpf_func_state *st) | |
10499 | { | |
10500 | enum bpf_reg_liveness live; | |
10501 | int i, j; | |
10502 | ||
10503 | for (i = 0; i < BPF_REG_FP; i++) { | |
10504 | live = st->regs[i].live; | |
10505 | /* liveness must not touch this register anymore */ | |
10506 | st->regs[i].live |= REG_LIVE_DONE; | |
10507 | if (!(live & REG_LIVE_READ)) | |
10508 | /* since the register is unused, clear its state | |
10509 | * to make further comparison simpler | |
10510 | */ | |
f54c7898 | 10511 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
10512 | } |
10513 | ||
10514 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
10515 | live = st->stack[i].spilled_ptr.live; | |
10516 | /* liveness must not touch this stack slot anymore */ | |
10517 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
10518 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 10519 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
10520 | for (j = 0; j < BPF_REG_SIZE; j++) |
10521 | st->stack[i].slot_type[j] = STACK_INVALID; | |
10522 | } | |
10523 | } | |
10524 | } | |
10525 | ||
10526 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
10527 | struct bpf_verifier_state *st) | |
10528 | { | |
10529 | int i; | |
10530 | ||
10531 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
10532 | /* all regs in this state in all frames were already marked */ | |
10533 | return; | |
10534 | ||
10535 | for (i = 0; i <= st->curframe; i++) | |
10536 | clean_func_state(env, st->frame[i]); | |
10537 | } | |
10538 | ||
10539 | /* the parentage chains form a tree. | |
10540 | * the verifier states are added to state lists at given insn and | |
10541 | * pushed into state stack for future exploration. | |
10542 | * when the verifier reaches bpf_exit insn some of the verifer states | |
10543 | * stored in the state lists have their final liveness state already, | |
10544 | * but a lot of states will get revised from liveness point of view when | |
10545 | * the verifier explores other branches. | |
10546 | * Example: | |
10547 | * 1: r0 = 1 | |
10548 | * 2: if r1 == 100 goto pc+1 | |
10549 | * 3: r0 = 2 | |
10550 | * 4: exit | |
10551 | * when the verifier reaches exit insn the register r0 in the state list of | |
10552 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
10553 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
10554 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
10555 | * | |
10556 | * Since the verifier pushes the branch states as it sees them while exploring | |
10557 | * the program the condition of walking the branch instruction for the second | |
10558 | * time means that all states below this branch were already explored and | |
8fb33b60 | 10559 | * their final liveness marks are already propagated. |
9242b5f5 AS |
10560 | * Hence when the verifier completes the search of state list in is_state_visited() |
10561 | * we can call this clean_live_states() function to mark all liveness states | |
10562 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
10563 | * will not be used. | |
10564 | * This function also clears the registers and stack for states that !READ | |
10565 | * to simplify state merging. | |
10566 | * | |
10567 | * Important note here that walking the same branch instruction in the callee | |
10568 | * doesn't meant that the states are DONE. The verifier has to compare | |
10569 | * the callsites | |
10570 | */ | |
10571 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
10572 | struct bpf_verifier_state *cur) | |
10573 | { | |
10574 | struct bpf_verifier_state_list *sl; | |
10575 | int i; | |
10576 | ||
5d839021 | 10577 | sl = *explored_state(env, insn); |
a8f500af | 10578 | while (sl) { |
2589726d AS |
10579 | if (sl->state.branches) |
10580 | goto next; | |
dc2a4ebc AS |
10581 | if (sl->state.insn_idx != insn || |
10582 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
10583 | goto next; |
10584 | for (i = 0; i <= cur->curframe; i++) | |
10585 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
10586 | goto next; | |
10587 | clean_verifier_state(env, &sl->state); | |
10588 | next: | |
10589 | sl = sl->next; | |
10590 | } | |
10591 | } | |
10592 | ||
f1174f77 | 10593 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
10594 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
10595 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 10596 | { |
f4d7e40a AS |
10597 | bool equal; |
10598 | ||
dc503a8a EC |
10599 | if (!(rold->live & REG_LIVE_READ)) |
10600 | /* explored state didn't use this */ | |
10601 | return true; | |
10602 | ||
679c782d | 10603 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
10604 | |
10605 | if (rold->type == PTR_TO_STACK) | |
10606 | /* two stack pointers are equal only if they're pointing to | |
10607 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
10608 | */ | |
10609 | return equal && rold->frameno == rcur->frameno; | |
10610 | ||
10611 | if (equal) | |
969bf05e AS |
10612 | return true; |
10613 | ||
f1174f77 EC |
10614 | if (rold->type == NOT_INIT) |
10615 | /* explored state can't have used this */ | |
969bf05e | 10616 | return true; |
f1174f77 EC |
10617 | if (rcur->type == NOT_INIT) |
10618 | return false; | |
10619 | switch (rold->type) { | |
10620 | case SCALAR_VALUE: | |
e042aa53 DB |
10621 | if (env->explore_alu_limits) |
10622 | return false; | |
f1174f77 | 10623 | if (rcur->type == SCALAR_VALUE) { |
b5dc0163 AS |
10624 | if (!rold->precise && !rcur->precise) |
10625 | return true; | |
f1174f77 EC |
10626 | /* new val must satisfy old val knowledge */ |
10627 | return range_within(rold, rcur) && | |
10628 | tnum_in(rold->var_off, rcur->var_off); | |
10629 | } else { | |
179d1c56 JH |
10630 | /* We're trying to use a pointer in place of a scalar. |
10631 | * Even if the scalar was unbounded, this could lead to | |
10632 | * pointer leaks because scalars are allowed to leak | |
10633 | * while pointers are not. We could make this safe in | |
10634 | * special cases if root is calling us, but it's | |
10635 | * probably not worth the hassle. | |
f1174f77 | 10636 | */ |
179d1c56 | 10637 | return false; |
f1174f77 | 10638 | } |
69c087ba | 10639 | case PTR_TO_MAP_KEY: |
f1174f77 | 10640 | case PTR_TO_MAP_VALUE: |
1b688a19 EC |
10641 | /* If the new min/max/var_off satisfy the old ones and |
10642 | * everything else matches, we are OK. | |
d83525ca AS |
10643 | * 'id' is not compared, since it's only used for maps with |
10644 | * bpf_spin_lock inside map element and in such cases if | |
10645 | * the rest of the prog is valid for one map element then | |
10646 | * it's valid for all map elements regardless of the key | |
10647 | * used in bpf_map_lookup() | |
1b688a19 EC |
10648 | */ |
10649 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
10650 | range_within(rold, rcur) && | |
10651 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
10652 | case PTR_TO_MAP_VALUE_OR_NULL: |
10653 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
10654 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
10655 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
10656 | * checked, doing so could have affected others with the same | |
10657 | * id, and we can't check for that because we lost the id when | |
10658 | * we converted to a PTR_TO_MAP_VALUE. | |
10659 | */ | |
10660 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
10661 | return false; | |
10662 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
10663 | return false; | |
10664 | /* Check our ids match any regs they're supposed to */ | |
10665 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 10666 | case PTR_TO_PACKET_META: |
f1174f77 | 10667 | case PTR_TO_PACKET: |
de8f3a83 | 10668 | if (rcur->type != rold->type) |
f1174f77 EC |
10669 | return false; |
10670 | /* We must have at least as much range as the old ptr | |
10671 | * did, so that any accesses which were safe before are | |
10672 | * still safe. This is true even if old range < old off, | |
10673 | * since someone could have accessed through (ptr - k), or | |
10674 | * even done ptr -= k in a register, to get a safe access. | |
10675 | */ | |
10676 | if (rold->range > rcur->range) | |
10677 | return false; | |
10678 | /* If the offsets don't match, we can't trust our alignment; | |
10679 | * nor can we be sure that we won't fall out of range. | |
10680 | */ | |
10681 | if (rold->off != rcur->off) | |
10682 | return false; | |
10683 | /* id relations must be preserved */ | |
10684 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
10685 | return false; | |
10686 | /* new val must satisfy old val knowledge */ | |
10687 | return range_within(rold, rcur) && | |
10688 | tnum_in(rold->var_off, rcur->var_off); | |
10689 | case PTR_TO_CTX: | |
10690 | case CONST_PTR_TO_MAP: | |
f1174f77 | 10691 | case PTR_TO_PACKET_END: |
d58e468b | 10692 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
10693 | case PTR_TO_SOCKET: |
10694 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
10695 | case PTR_TO_SOCK_COMMON: |
10696 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
10697 | case PTR_TO_TCP_SOCK: |
10698 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 10699 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
10700 | /* Only valid matches are exact, which memcmp() above |
10701 | * would have accepted | |
10702 | */ | |
10703 | default: | |
10704 | /* Don't know what's going on, just say it's not safe */ | |
10705 | return false; | |
10706 | } | |
969bf05e | 10707 | |
f1174f77 EC |
10708 | /* Shouldn't get here; if we do, say it's not safe */ |
10709 | WARN_ON_ONCE(1); | |
969bf05e AS |
10710 | return false; |
10711 | } | |
10712 | ||
e042aa53 DB |
10713 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
10714 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
10715 | { |
10716 | int i, spi; | |
10717 | ||
638f5b90 AS |
10718 | /* walk slots of the explored stack and ignore any additional |
10719 | * slots in the current stack, since explored(safe) state | |
10720 | * didn't use them | |
10721 | */ | |
10722 | for (i = 0; i < old->allocated_stack; i++) { | |
10723 | spi = i / BPF_REG_SIZE; | |
10724 | ||
b233920c AS |
10725 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
10726 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 10727 | /* explored state didn't use this */ |
fd05e57b | 10728 | continue; |
b233920c | 10729 | } |
cc2b14d5 | 10730 | |
638f5b90 AS |
10731 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
10732 | continue; | |
19e2dbb7 AS |
10733 | |
10734 | /* explored stack has more populated slots than current stack | |
10735 | * and these slots were used | |
10736 | */ | |
10737 | if (i >= cur->allocated_stack) | |
10738 | return false; | |
10739 | ||
cc2b14d5 AS |
10740 | /* if old state was safe with misc data in the stack |
10741 | * it will be safe with zero-initialized stack. | |
10742 | * The opposite is not true | |
10743 | */ | |
10744 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
10745 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
10746 | continue; | |
638f5b90 AS |
10747 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
10748 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
10749 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 10750 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
10751 | * this verifier states are not equivalent, |
10752 | * return false to continue verification of this path | |
10753 | */ | |
10754 | return false; | |
27113c59 | 10755 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 10756 | continue; |
27113c59 | 10757 | if (!is_spilled_reg(&old->stack[spi])) |
638f5b90 | 10758 | continue; |
e042aa53 DB |
10759 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
10760 | &cur->stack[spi].spilled_ptr, idmap)) | |
638f5b90 AS |
10761 | /* when explored and current stack slot are both storing |
10762 | * spilled registers, check that stored pointers types | |
10763 | * are the same as well. | |
10764 | * Ex: explored safe path could have stored | |
10765 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
10766 | * but current path has stored: | |
10767 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
10768 | * such verifier states are not equivalent. | |
10769 | * return false to continue verification of this path | |
10770 | */ | |
10771 | return false; | |
10772 | } | |
10773 | return true; | |
10774 | } | |
10775 | ||
fd978bf7 JS |
10776 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
10777 | { | |
10778 | if (old->acquired_refs != cur->acquired_refs) | |
10779 | return false; | |
10780 | return !memcmp(old->refs, cur->refs, | |
10781 | sizeof(*old->refs) * old->acquired_refs); | |
10782 | } | |
10783 | ||
f1bca824 AS |
10784 | /* compare two verifier states |
10785 | * | |
10786 | * all states stored in state_list are known to be valid, since | |
10787 | * verifier reached 'bpf_exit' instruction through them | |
10788 | * | |
10789 | * this function is called when verifier exploring different branches of | |
10790 | * execution popped from the state stack. If it sees an old state that has | |
10791 | * more strict register state and more strict stack state then this execution | |
10792 | * branch doesn't need to be explored further, since verifier already | |
10793 | * concluded that more strict state leads to valid finish. | |
10794 | * | |
10795 | * Therefore two states are equivalent if register state is more conservative | |
10796 | * and explored stack state is more conservative than the current one. | |
10797 | * Example: | |
10798 | * explored current | |
10799 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
10800 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
10801 | * | |
10802 | * In other words if current stack state (one being explored) has more | |
10803 | * valid slots than old one that already passed validation, it means | |
10804 | * the verifier can stop exploring and conclude that current state is valid too | |
10805 | * | |
10806 | * Similarly with registers. If explored state has register type as invalid | |
10807 | * whereas register type in current state is meaningful, it means that | |
10808 | * the current state will reach 'bpf_exit' instruction safely | |
10809 | */ | |
c9e73e3d | 10810 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 10811 | struct bpf_func_state *cur) |
f1bca824 AS |
10812 | { |
10813 | int i; | |
10814 | ||
c9e73e3d LB |
10815 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
10816 | for (i = 0; i < MAX_BPF_REG; i++) | |
e042aa53 DB |
10817 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
10818 | env->idmap_scratch)) | |
c9e73e3d | 10819 | return false; |
f1bca824 | 10820 | |
e042aa53 | 10821 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 10822 | return false; |
fd978bf7 JS |
10823 | |
10824 | if (!refsafe(old, cur)) | |
c9e73e3d LB |
10825 | return false; |
10826 | ||
10827 | return true; | |
f1bca824 AS |
10828 | } |
10829 | ||
f4d7e40a AS |
10830 | static bool states_equal(struct bpf_verifier_env *env, |
10831 | struct bpf_verifier_state *old, | |
10832 | struct bpf_verifier_state *cur) | |
10833 | { | |
10834 | int i; | |
10835 | ||
10836 | if (old->curframe != cur->curframe) | |
10837 | return false; | |
10838 | ||
979d63d5 DB |
10839 | /* Verification state from speculative execution simulation |
10840 | * must never prune a non-speculative execution one. | |
10841 | */ | |
10842 | if (old->speculative && !cur->speculative) | |
10843 | return false; | |
10844 | ||
d83525ca AS |
10845 | if (old->active_spin_lock != cur->active_spin_lock) |
10846 | return false; | |
10847 | ||
f4d7e40a AS |
10848 | /* for states to be equal callsites have to be the same |
10849 | * and all frame states need to be equivalent | |
10850 | */ | |
10851 | for (i = 0; i <= old->curframe; i++) { | |
10852 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
10853 | return false; | |
c9e73e3d | 10854 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
10855 | return false; |
10856 | } | |
10857 | return true; | |
10858 | } | |
10859 | ||
5327ed3d JW |
10860 | /* Return 0 if no propagation happened. Return negative error code if error |
10861 | * happened. Otherwise, return the propagated bit. | |
10862 | */ | |
55e7f3b5 JW |
10863 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
10864 | struct bpf_reg_state *reg, | |
10865 | struct bpf_reg_state *parent_reg) | |
10866 | { | |
5327ed3d JW |
10867 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
10868 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
10869 | int err; |
10870 | ||
5327ed3d JW |
10871 | /* When comes here, read flags of PARENT_REG or REG could be any of |
10872 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
10873 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
10874 | */ | |
10875 | if (parent_flag == REG_LIVE_READ64 || | |
10876 | /* Or if there is no read flag from REG. */ | |
10877 | !flag || | |
10878 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
10879 | parent_flag == flag) | |
55e7f3b5 JW |
10880 | return 0; |
10881 | ||
5327ed3d | 10882 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
10883 | if (err) |
10884 | return err; | |
10885 | ||
5327ed3d | 10886 | return flag; |
55e7f3b5 JW |
10887 | } |
10888 | ||
8e9cd9ce | 10889 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
10890 | * straight-line code between a state and its parent. When we arrive at an |
10891 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
10892 | * code, so read marks in the state must propagate to the parent regardless | |
10893 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 10894 | * in mark_reg_read() is for. |
8e9cd9ce | 10895 | */ |
f4d7e40a AS |
10896 | static int propagate_liveness(struct bpf_verifier_env *env, |
10897 | const struct bpf_verifier_state *vstate, | |
10898 | struct bpf_verifier_state *vparent) | |
dc503a8a | 10899 | { |
3f8cafa4 | 10900 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 10901 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 10902 | int i, frame, err = 0; |
dc503a8a | 10903 | |
f4d7e40a AS |
10904 | if (vparent->curframe != vstate->curframe) { |
10905 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
10906 | vparent->curframe, vstate->curframe); | |
10907 | return -EFAULT; | |
10908 | } | |
dc503a8a EC |
10909 | /* Propagate read liveness of registers... */ |
10910 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 10911 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
10912 | parent = vparent->frame[frame]; |
10913 | state = vstate->frame[frame]; | |
10914 | parent_reg = parent->regs; | |
10915 | state_reg = state->regs; | |
83d16312 JK |
10916 | /* We don't need to worry about FP liveness, it's read-only */ |
10917 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
10918 | err = propagate_liveness_reg(env, &state_reg[i], |
10919 | &parent_reg[i]); | |
5327ed3d | 10920 | if (err < 0) |
3f8cafa4 | 10921 | return err; |
5327ed3d JW |
10922 | if (err == REG_LIVE_READ64) |
10923 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 10924 | } |
f4d7e40a | 10925 | |
1b04aee7 | 10926 | /* Propagate stack slots. */ |
f4d7e40a AS |
10927 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
10928 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
10929 | parent_reg = &parent->stack[i].spilled_ptr; |
10930 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
10931 | err = propagate_liveness_reg(env, state_reg, |
10932 | parent_reg); | |
5327ed3d | 10933 | if (err < 0) |
3f8cafa4 | 10934 | return err; |
dc503a8a EC |
10935 | } |
10936 | } | |
5327ed3d | 10937 | return 0; |
dc503a8a EC |
10938 | } |
10939 | ||
a3ce685d AS |
10940 | /* find precise scalars in the previous equivalent state and |
10941 | * propagate them into the current state | |
10942 | */ | |
10943 | static int propagate_precision(struct bpf_verifier_env *env, | |
10944 | const struct bpf_verifier_state *old) | |
10945 | { | |
10946 | struct bpf_reg_state *state_reg; | |
10947 | struct bpf_func_state *state; | |
10948 | int i, err = 0; | |
10949 | ||
10950 | state = old->frame[old->curframe]; | |
10951 | state_reg = state->regs; | |
10952 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
10953 | if (state_reg->type != SCALAR_VALUE || | |
10954 | !state_reg->precise) | |
10955 | continue; | |
10956 | if (env->log.level & BPF_LOG_LEVEL2) | |
10957 | verbose(env, "propagating r%d\n", i); | |
10958 | err = mark_chain_precision(env, i); | |
10959 | if (err < 0) | |
10960 | return err; | |
10961 | } | |
10962 | ||
10963 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
27113c59 | 10964 | if (!is_spilled_reg(&state->stack[i])) |
a3ce685d AS |
10965 | continue; |
10966 | state_reg = &state->stack[i].spilled_ptr; | |
10967 | if (state_reg->type != SCALAR_VALUE || | |
10968 | !state_reg->precise) | |
10969 | continue; | |
10970 | if (env->log.level & BPF_LOG_LEVEL2) | |
10971 | verbose(env, "propagating fp%d\n", | |
10972 | (-i - 1) * BPF_REG_SIZE); | |
10973 | err = mark_chain_precision_stack(env, i); | |
10974 | if (err < 0) | |
10975 | return err; | |
10976 | } | |
10977 | return 0; | |
10978 | } | |
10979 | ||
2589726d AS |
10980 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
10981 | struct bpf_verifier_state *cur) | |
10982 | { | |
10983 | struct bpf_func_state *fold, *fcur; | |
10984 | int i, fr = cur->curframe; | |
10985 | ||
10986 | if (old->curframe != fr) | |
10987 | return false; | |
10988 | ||
10989 | fold = old->frame[fr]; | |
10990 | fcur = cur->frame[fr]; | |
10991 | for (i = 0; i < MAX_BPF_REG; i++) | |
10992 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
10993 | offsetof(struct bpf_reg_state, parent))) | |
10994 | return false; | |
10995 | return true; | |
10996 | } | |
10997 | ||
10998 | ||
58e2af8b | 10999 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 11000 | { |
58e2af8b | 11001 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 11002 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 11003 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 11004 | int i, j, err, states_cnt = 0; |
10d274e8 | 11005 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 11006 | |
b5dc0163 | 11007 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 11008 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
11009 | /* this 'insn_idx' instruction wasn't marked, so we will not |
11010 | * be doing state search here | |
11011 | */ | |
11012 | return 0; | |
11013 | ||
2589726d AS |
11014 | /* bpf progs typically have pruning point every 4 instructions |
11015 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
11016 | * Do not add new state for future pruning if the verifier hasn't seen | |
11017 | * at least 2 jumps and at least 8 instructions. | |
11018 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
11019 | * In tests that amounts to up to 50% reduction into total verifier | |
11020 | * memory consumption and 20% verifier time speedup. | |
11021 | */ | |
11022 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
11023 | env->insn_processed - env->prev_insn_processed >= 8) | |
11024 | add_new_state = true; | |
11025 | ||
a8f500af AS |
11026 | pprev = explored_state(env, insn_idx); |
11027 | sl = *pprev; | |
11028 | ||
9242b5f5 AS |
11029 | clean_live_states(env, insn_idx, cur); |
11030 | ||
a8f500af | 11031 | while (sl) { |
dc2a4ebc AS |
11032 | states_cnt++; |
11033 | if (sl->state.insn_idx != insn_idx) | |
11034 | goto next; | |
bfc6bb74 | 11035 | |
2589726d | 11036 | if (sl->state.branches) { |
bfc6bb74 AS |
11037 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
11038 | ||
11039 | if (frame->in_async_callback_fn && | |
11040 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
11041 | /* Different async_entry_cnt means that the verifier is | |
11042 | * processing another entry into async callback. | |
11043 | * Seeing the same state is not an indication of infinite | |
11044 | * loop or infinite recursion. | |
11045 | * But finding the same state doesn't mean that it's safe | |
11046 | * to stop processing the current state. The previous state | |
11047 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
11048 | * Checking in_async_callback_fn alone is not enough either. | |
11049 | * Since the verifier still needs to catch infinite loops | |
11050 | * inside async callbacks. | |
11051 | */ | |
11052 | } else if (states_maybe_looping(&sl->state, cur) && | |
11053 | states_equal(env, &sl->state, cur)) { | |
2589726d AS |
11054 | verbose_linfo(env, insn_idx, "; "); |
11055 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
11056 | return -EINVAL; | |
11057 | } | |
11058 | /* if the verifier is processing a loop, avoid adding new state | |
11059 | * too often, since different loop iterations have distinct | |
11060 | * states and may not help future pruning. | |
11061 | * This threshold shouldn't be too low to make sure that | |
11062 | * a loop with large bound will be rejected quickly. | |
11063 | * The most abusive loop will be: | |
11064 | * r1 += 1 | |
11065 | * if r1 < 1000000 goto pc-2 | |
11066 | * 1M insn_procssed limit / 100 == 10k peak states. | |
11067 | * This threshold shouldn't be too high either, since states | |
11068 | * at the end of the loop are likely to be useful in pruning. | |
11069 | */ | |
11070 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
11071 | env->insn_processed - env->prev_insn_processed < 100) | |
11072 | add_new_state = false; | |
11073 | goto miss; | |
11074 | } | |
638f5b90 | 11075 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 11076 | sl->hit_cnt++; |
f1bca824 | 11077 | /* reached equivalent register/stack state, |
dc503a8a EC |
11078 | * prune the search. |
11079 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
11080 | * If we have any write marks in env->cur_state, they |
11081 | * will prevent corresponding reads in the continuation | |
11082 | * from reaching our parent (an explored_state). Our | |
11083 | * own state will get the read marks recorded, but | |
11084 | * they'll be immediately forgotten as we're pruning | |
11085 | * this state and will pop a new one. | |
f1bca824 | 11086 | */ |
f4d7e40a | 11087 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
11088 | |
11089 | /* if previous state reached the exit with precision and | |
11090 | * current state is equivalent to it (except precsion marks) | |
11091 | * the precision needs to be propagated back in | |
11092 | * the current state. | |
11093 | */ | |
11094 | err = err ? : push_jmp_history(env, cur); | |
11095 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
11096 | if (err) |
11097 | return err; | |
f1bca824 | 11098 | return 1; |
dc503a8a | 11099 | } |
2589726d AS |
11100 | miss: |
11101 | /* when new state is not going to be added do not increase miss count. | |
11102 | * Otherwise several loop iterations will remove the state | |
11103 | * recorded earlier. The goal of these heuristics is to have | |
11104 | * states from some iterations of the loop (some in the beginning | |
11105 | * and some at the end) to help pruning. | |
11106 | */ | |
11107 | if (add_new_state) | |
11108 | sl->miss_cnt++; | |
9f4686c4 AS |
11109 | /* heuristic to determine whether this state is beneficial |
11110 | * to keep checking from state equivalence point of view. | |
11111 | * Higher numbers increase max_states_per_insn and verification time, | |
11112 | * but do not meaningfully decrease insn_processed. | |
11113 | */ | |
11114 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
11115 | /* the state is unlikely to be useful. Remove it to | |
11116 | * speed up verification | |
11117 | */ | |
11118 | *pprev = sl->next; | |
11119 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
11120 | u32 br = sl->state.branches; |
11121 | ||
11122 | WARN_ONCE(br, | |
11123 | "BUG live_done but branches_to_explore %d\n", | |
11124 | br); | |
9f4686c4 AS |
11125 | free_verifier_state(&sl->state, false); |
11126 | kfree(sl); | |
11127 | env->peak_states--; | |
11128 | } else { | |
11129 | /* cannot free this state, since parentage chain may | |
11130 | * walk it later. Add it for free_list instead to | |
11131 | * be freed at the end of verification | |
11132 | */ | |
11133 | sl->next = env->free_list; | |
11134 | env->free_list = sl; | |
11135 | } | |
11136 | sl = *pprev; | |
11137 | continue; | |
11138 | } | |
dc2a4ebc | 11139 | next: |
9f4686c4 AS |
11140 | pprev = &sl->next; |
11141 | sl = *pprev; | |
f1bca824 AS |
11142 | } |
11143 | ||
06ee7115 AS |
11144 | if (env->max_states_per_insn < states_cnt) |
11145 | env->max_states_per_insn = states_cnt; | |
11146 | ||
2c78ee89 | 11147 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 11148 | return push_jmp_history(env, cur); |
ceefbc96 | 11149 | |
2589726d | 11150 | if (!add_new_state) |
b5dc0163 | 11151 | return push_jmp_history(env, cur); |
ceefbc96 | 11152 | |
2589726d AS |
11153 | /* There were no equivalent states, remember the current one. |
11154 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 11155 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 11156 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 11157 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
11158 | * again on the way to bpf_exit. |
11159 | * When looping the sl->state.branches will be > 0 and this state | |
11160 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 11161 | */ |
638f5b90 | 11162 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
11163 | if (!new_sl) |
11164 | return -ENOMEM; | |
06ee7115 AS |
11165 | env->total_states++; |
11166 | env->peak_states++; | |
2589726d AS |
11167 | env->prev_jmps_processed = env->jmps_processed; |
11168 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
11169 | |
11170 | /* add new state to the head of linked list */ | |
679c782d EC |
11171 | new = &new_sl->state; |
11172 | err = copy_verifier_state(new, cur); | |
1969db47 | 11173 | if (err) { |
679c782d | 11174 | free_verifier_state(new, false); |
1969db47 AS |
11175 | kfree(new_sl); |
11176 | return err; | |
11177 | } | |
dc2a4ebc | 11178 | new->insn_idx = insn_idx; |
2589726d AS |
11179 | WARN_ONCE(new->branches != 1, |
11180 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 11181 | |
2589726d | 11182 | cur->parent = new; |
b5dc0163 AS |
11183 | cur->first_insn_idx = insn_idx; |
11184 | clear_jmp_history(cur); | |
5d839021 AS |
11185 | new_sl->next = *explored_state(env, insn_idx); |
11186 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
11187 | /* connect new state to parentage chain. Current frame needs all |
11188 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
11189 | * to the stack implicitly by JITs) so in callers' frames connect just | |
11190 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
11191 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
11192 | * from callee with its full parentage chain, anyway. | |
11193 | */ | |
8e9cd9ce EC |
11194 | /* clear write marks in current state: the writes we did are not writes |
11195 | * our child did, so they don't screen off its reads from us. | |
11196 | * (There are no read marks in current state, because reads always mark | |
11197 | * their parent and current state never has children yet. Only | |
11198 | * explored_states can get read marks.) | |
11199 | */ | |
eea1c227 AS |
11200 | for (j = 0; j <= cur->curframe; j++) { |
11201 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
11202 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
11203 | for (i = 0; i < BPF_REG_FP; i++) | |
11204 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
11205 | } | |
f4d7e40a AS |
11206 | |
11207 | /* all stack frames are accessible from callee, clear them all */ | |
11208 | for (j = 0; j <= cur->curframe; j++) { | |
11209 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 11210 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 11211 | |
679c782d | 11212 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 11213 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
11214 | frame->stack[i].spilled_ptr.parent = |
11215 | &newframe->stack[i].spilled_ptr; | |
11216 | } | |
f4d7e40a | 11217 | } |
f1bca824 AS |
11218 | return 0; |
11219 | } | |
11220 | ||
c64b7983 JS |
11221 | /* Return true if it's OK to have the same insn return a different type. */ |
11222 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
11223 | { | |
11224 | switch (type) { | |
11225 | case PTR_TO_CTX: | |
11226 | case PTR_TO_SOCKET: | |
11227 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
11228 | case PTR_TO_SOCK_COMMON: |
11229 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
11230 | case PTR_TO_TCP_SOCK: |
11231 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 11232 | case PTR_TO_XDP_SOCK: |
2a02759e | 11233 | case PTR_TO_BTF_ID: |
b121b341 | 11234 | case PTR_TO_BTF_ID_OR_NULL: |
c64b7983 JS |
11235 | return false; |
11236 | default: | |
11237 | return true; | |
11238 | } | |
11239 | } | |
11240 | ||
11241 | /* If an instruction was previously used with particular pointer types, then we | |
11242 | * need to be careful to avoid cases such as the below, where it may be ok | |
11243 | * for one branch accessing the pointer, but not ok for the other branch: | |
11244 | * | |
11245 | * R1 = sock_ptr | |
11246 | * goto X; | |
11247 | * ... | |
11248 | * R1 = some_other_valid_ptr; | |
11249 | * goto X; | |
11250 | * ... | |
11251 | * R2 = *(u32 *)(R1 + 0); | |
11252 | */ | |
11253 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
11254 | { | |
11255 | return src != prev && (!reg_type_mismatch_ok(src) || | |
11256 | !reg_type_mismatch_ok(prev)); | |
11257 | } | |
11258 | ||
58e2af8b | 11259 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 11260 | { |
6f8a57cc | 11261 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 11262 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 11263 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 11264 | struct bpf_reg_state *regs; |
06ee7115 | 11265 | int insn_cnt = env->prog->len; |
17a52670 | 11266 | bool do_print_state = false; |
b5dc0163 | 11267 | int prev_insn_idx = -1; |
17a52670 | 11268 | |
17a52670 AS |
11269 | for (;;) { |
11270 | struct bpf_insn *insn; | |
11271 | u8 class; | |
11272 | int err; | |
11273 | ||
b5dc0163 | 11274 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 11275 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 11276 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 11277 | env->insn_idx, insn_cnt); |
17a52670 AS |
11278 | return -EFAULT; |
11279 | } | |
11280 | ||
c08435ec | 11281 | insn = &insns[env->insn_idx]; |
17a52670 AS |
11282 | class = BPF_CLASS(insn->code); |
11283 | ||
06ee7115 | 11284 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
11285 | verbose(env, |
11286 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 11287 | env->insn_processed); |
17a52670 AS |
11288 | return -E2BIG; |
11289 | } | |
11290 | ||
c08435ec | 11291 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
11292 | if (err < 0) |
11293 | return err; | |
11294 | if (err == 1) { | |
11295 | /* found equivalent state, can prune the search */ | |
06ee7115 | 11296 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 11297 | if (do_print_state) |
979d63d5 DB |
11298 | verbose(env, "\nfrom %d to %d%s: safe\n", |
11299 | env->prev_insn_idx, env->insn_idx, | |
11300 | env->cur_state->speculative ? | |
11301 | " (speculative execution)" : ""); | |
f1bca824 | 11302 | else |
c08435ec | 11303 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
11304 | } |
11305 | goto process_bpf_exit; | |
11306 | } | |
11307 | ||
c3494801 AS |
11308 | if (signal_pending(current)) |
11309 | return -EAGAIN; | |
11310 | ||
3c2ce60b DB |
11311 | if (need_resched()) |
11312 | cond_resched(); | |
11313 | ||
2e576648 CL |
11314 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
11315 | verbose(env, "\nfrom %d to %d%s:", | |
11316 | env->prev_insn_idx, env->insn_idx, | |
11317 | env->cur_state->speculative ? | |
11318 | " (speculative execution)" : ""); | |
11319 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
11320 | do_print_state = false; |
11321 | } | |
11322 | ||
06ee7115 | 11323 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 11324 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 11325 | .cb_call = disasm_kfunc_name, |
7105e828 | 11326 | .cb_print = verbose, |
abe08840 | 11327 | .private_data = env, |
7105e828 DB |
11328 | }; |
11329 | ||
2e576648 CL |
11330 | if (verifier_state_scratched(env)) |
11331 | print_insn_state(env, state->frame[state->curframe]); | |
11332 | ||
c08435ec | 11333 | verbose_linfo(env, env->insn_idx, "; "); |
2e576648 | 11334 | env->prev_log_len = env->log.len_used; |
c08435ec | 11335 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 11336 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
2e576648 CL |
11337 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
11338 | env->prev_log_len = env->log.len_used; | |
17a52670 AS |
11339 | } |
11340 | ||
cae1927c | 11341 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
11342 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
11343 | env->prev_insn_idx); | |
cae1927c JK |
11344 | if (err) |
11345 | return err; | |
11346 | } | |
13a27dfc | 11347 | |
638f5b90 | 11348 | regs = cur_regs(env); |
fe9a5ca7 | 11349 | sanitize_mark_insn_seen(env); |
b5dc0163 | 11350 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 11351 | |
17a52670 | 11352 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 11353 | err = check_alu_op(env, insn); |
17a52670 AS |
11354 | if (err) |
11355 | return err; | |
11356 | ||
11357 | } else if (class == BPF_LDX) { | |
3df126f3 | 11358 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
11359 | |
11360 | /* check for reserved fields is already done */ | |
11361 | ||
17a52670 | 11362 | /* check src operand */ |
dc503a8a | 11363 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
11364 | if (err) |
11365 | return err; | |
11366 | ||
dc503a8a | 11367 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
11368 | if (err) |
11369 | return err; | |
11370 | ||
725f9dcd AS |
11371 | src_reg_type = regs[insn->src_reg].type; |
11372 | ||
17a52670 AS |
11373 | /* check that memory (src_reg + off) is readable, |
11374 | * the state of dst_reg will be updated by this func | |
11375 | */ | |
c08435ec DB |
11376 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
11377 | insn->off, BPF_SIZE(insn->code), | |
11378 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
11379 | if (err) |
11380 | return err; | |
11381 | ||
c08435ec | 11382 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
11383 | |
11384 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
11385 | /* saw a valid insn |
11386 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 11387 | * save type to validate intersecting paths |
9bac3d6d | 11388 | */ |
3df126f3 | 11389 | *prev_src_type = src_reg_type; |
9bac3d6d | 11390 | |
c64b7983 | 11391 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
11392 | /* ABuser program is trying to use the same insn |
11393 | * dst_reg = *(u32*) (src_reg + off) | |
11394 | * with different pointer types: | |
11395 | * src_reg == ctx in one branch and | |
11396 | * src_reg == stack|map in some other branch. | |
11397 | * Reject it. | |
11398 | */ | |
61bd5218 | 11399 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
11400 | return -EINVAL; |
11401 | } | |
11402 | ||
17a52670 | 11403 | } else if (class == BPF_STX) { |
3df126f3 | 11404 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 11405 | |
91c960b0 BJ |
11406 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
11407 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
11408 | if (err) |
11409 | return err; | |
c08435ec | 11410 | env->insn_idx++; |
17a52670 AS |
11411 | continue; |
11412 | } | |
11413 | ||
5ca419f2 BJ |
11414 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
11415 | verbose(env, "BPF_STX uses reserved fields\n"); | |
11416 | return -EINVAL; | |
11417 | } | |
11418 | ||
17a52670 | 11419 | /* check src1 operand */ |
dc503a8a | 11420 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
11421 | if (err) |
11422 | return err; | |
11423 | /* check src2 operand */ | |
dc503a8a | 11424 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11425 | if (err) |
11426 | return err; | |
11427 | ||
d691f9e8 AS |
11428 | dst_reg_type = regs[insn->dst_reg].type; |
11429 | ||
17a52670 | 11430 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
11431 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
11432 | insn->off, BPF_SIZE(insn->code), | |
11433 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
11434 | if (err) |
11435 | return err; | |
11436 | ||
c08435ec | 11437 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
11438 | |
11439 | if (*prev_dst_type == NOT_INIT) { | |
11440 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 11441 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 11442 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
11443 | return -EINVAL; |
11444 | } | |
11445 | ||
17a52670 AS |
11446 | } else if (class == BPF_ST) { |
11447 | if (BPF_MODE(insn->code) != BPF_MEM || | |
11448 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 11449 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
11450 | return -EINVAL; |
11451 | } | |
11452 | /* check src operand */ | |
dc503a8a | 11453 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11454 | if (err) |
11455 | return err; | |
11456 | ||
f37a8cb8 | 11457 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 11458 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
11459 | insn->dst_reg, |
11460 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
11461 | return -EACCES; |
11462 | } | |
11463 | ||
17a52670 | 11464 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
11465 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
11466 | insn->off, BPF_SIZE(insn->code), | |
11467 | BPF_WRITE, -1, false); | |
17a52670 AS |
11468 | if (err) |
11469 | return err; | |
11470 | ||
092ed096 | 11471 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
11472 | u8 opcode = BPF_OP(insn->code); |
11473 | ||
2589726d | 11474 | env->jmps_processed++; |
17a52670 AS |
11475 | if (opcode == BPF_CALL) { |
11476 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
11477 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
11478 | && insn->off != 0) || | |
f4d7e40a | 11479 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
11480 | insn->src_reg != BPF_PSEUDO_CALL && |
11481 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
11482 | insn->dst_reg != BPF_REG_0 || |
11483 | class == BPF_JMP32) { | |
61bd5218 | 11484 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
11485 | return -EINVAL; |
11486 | } | |
11487 | ||
d83525ca AS |
11488 | if (env->cur_state->active_spin_lock && |
11489 | (insn->src_reg == BPF_PSEUDO_CALL || | |
11490 | insn->imm != BPF_FUNC_spin_unlock)) { | |
11491 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
11492 | return -EINVAL; | |
11493 | } | |
f4d7e40a | 11494 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 11495 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 MKL |
11496 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
11497 | err = check_kfunc_call(env, insn); | |
f4d7e40a | 11498 | else |
69c087ba | 11499 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
11500 | if (err) |
11501 | return err; | |
17a52670 AS |
11502 | } else if (opcode == BPF_JA) { |
11503 | if (BPF_SRC(insn->code) != BPF_K || | |
11504 | insn->imm != 0 || | |
11505 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
11506 | insn->dst_reg != BPF_REG_0 || |
11507 | class == BPF_JMP32) { | |
61bd5218 | 11508 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
11509 | return -EINVAL; |
11510 | } | |
11511 | ||
c08435ec | 11512 | env->insn_idx += insn->off + 1; |
17a52670 AS |
11513 | continue; |
11514 | ||
11515 | } else if (opcode == BPF_EXIT) { | |
11516 | if (BPF_SRC(insn->code) != BPF_K || | |
11517 | insn->imm != 0 || | |
11518 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
11519 | insn->dst_reg != BPF_REG_0 || |
11520 | class == BPF_JMP32) { | |
61bd5218 | 11521 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
11522 | return -EINVAL; |
11523 | } | |
11524 | ||
d83525ca AS |
11525 | if (env->cur_state->active_spin_lock) { |
11526 | verbose(env, "bpf_spin_unlock is missing\n"); | |
11527 | return -EINVAL; | |
11528 | } | |
11529 | ||
f4d7e40a AS |
11530 | if (state->curframe) { |
11531 | /* exit from nested function */ | |
c08435ec | 11532 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
11533 | if (err) |
11534 | return err; | |
11535 | do_print_state = true; | |
11536 | continue; | |
11537 | } | |
11538 | ||
fd978bf7 JS |
11539 | err = check_reference_leak(env); |
11540 | if (err) | |
11541 | return err; | |
11542 | ||
390ee7e2 AS |
11543 | err = check_return_code(env); |
11544 | if (err) | |
11545 | return err; | |
f1bca824 | 11546 | process_bpf_exit: |
0f55f9ed | 11547 | mark_verifier_state_scratched(env); |
2589726d | 11548 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 11549 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 11550 | &env->insn_idx, pop_log); |
638f5b90 AS |
11551 | if (err < 0) { |
11552 | if (err != -ENOENT) | |
11553 | return err; | |
17a52670 AS |
11554 | break; |
11555 | } else { | |
11556 | do_print_state = true; | |
11557 | continue; | |
11558 | } | |
11559 | } else { | |
c08435ec | 11560 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
11561 | if (err) |
11562 | return err; | |
11563 | } | |
11564 | } else if (class == BPF_LD) { | |
11565 | u8 mode = BPF_MODE(insn->code); | |
11566 | ||
11567 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
11568 | err = check_ld_abs(env, insn); |
11569 | if (err) | |
11570 | return err; | |
11571 | ||
17a52670 AS |
11572 | } else if (mode == BPF_IMM) { |
11573 | err = check_ld_imm(env, insn); | |
11574 | if (err) | |
11575 | return err; | |
11576 | ||
c08435ec | 11577 | env->insn_idx++; |
fe9a5ca7 | 11578 | sanitize_mark_insn_seen(env); |
17a52670 | 11579 | } else { |
61bd5218 | 11580 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
11581 | return -EINVAL; |
11582 | } | |
11583 | } else { | |
61bd5218 | 11584 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
11585 | return -EINVAL; |
11586 | } | |
11587 | ||
c08435ec | 11588 | env->insn_idx++; |
17a52670 AS |
11589 | } |
11590 | ||
11591 | return 0; | |
11592 | } | |
11593 | ||
541c3bad AN |
11594 | static int find_btf_percpu_datasec(struct btf *btf) |
11595 | { | |
11596 | const struct btf_type *t; | |
11597 | const char *tname; | |
11598 | int i, n; | |
11599 | ||
11600 | /* | |
11601 | * Both vmlinux and module each have their own ".data..percpu" | |
11602 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
11603 | * types to look at only module's own BTF types. | |
11604 | */ | |
11605 | n = btf_nr_types(btf); | |
11606 | if (btf_is_module(btf)) | |
11607 | i = btf_nr_types(btf_vmlinux); | |
11608 | else | |
11609 | i = 1; | |
11610 | ||
11611 | for(; i < n; i++) { | |
11612 | t = btf_type_by_id(btf, i); | |
11613 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
11614 | continue; | |
11615 | ||
11616 | tname = btf_name_by_offset(btf, t->name_off); | |
11617 | if (!strcmp(tname, ".data..percpu")) | |
11618 | return i; | |
11619 | } | |
11620 | ||
11621 | return -ENOENT; | |
11622 | } | |
11623 | ||
4976b718 HL |
11624 | /* replace pseudo btf_id with kernel symbol address */ |
11625 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
11626 | struct bpf_insn *insn, | |
11627 | struct bpf_insn_aux_data *aux) | |
11628 | { | |
eaa6bcb7 HL |
11629 | const struct btf_var_secinfo *vsi; |
11630 | const struct btf_type *datasec; | |
541c3bad | 11631 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
11632 | const struct btf_type *t; |
11633 | const char *sym_name; | |
eaa6bcb7 | 11634 | bool percpu = false; |
f16e6313 | 11635 | u32 type, id = insn->imm; |
541c3bad | 11636 | struct btf *btf; |
f16e6313 | 11637 | s32 datasec_id; |
4976b718 | 11638 | u64 addr; |
541c3bad | 11639 | int i, btf_fd, err; |
4976b718 | 11640 | |
541c3bad AN |
11641 | btf_fd = insn[1].imm; |
11642 | if (btf_fd) { | |
11643 | btf = btf_get_by_fd(btf_fd); | |
11644 | if (IS_ERR(btf)) { | |
11645 | verbose(env, "invalid module BTF object FD specified.\n"); | |
11646 | return -EINVAL; | |
11647 | } | |
11648 | } else { | |
11649 | if (!btf_vmlinux) { | |
11650 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
11651 | return -EINVAL; | |
11652 | } | |
11653 | btf = btf_vmlinux; | |
11654 | btf_get(btf); | |
4976b718 HL |
11655 | } |
11656 | ||
541c3bad | 11657 | t = btf_type_by_id(btf, id); |
4976b718 HL |
11658 | if (!t) { |
11659 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
11660 | err = -ENOENT; |
11661 | goto err_put; | |
4976b718 HL |
11662 | } |
11663 | ||
11664 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
11665 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
11666 | err = -EINVAL; | |
11667 | goto err_put; | |
4976b718 HL |
11668 | } |
11669 | ||
541c3bad | 11670 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
11671 | addr = kallsyms_lookup_name(sym_name); |
11672 | if (!addr) { | |
11673 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
11674 | sym_name); | |
541c3bad AN |
11675 | err = -ENOENT; |
11676 | goto err_put; | |
4976b718 HL |
11677 | } |
11678 | ||
541c3bad | 11679 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 11680 | if (datasec_id > 0) { |
541c3bad | 11681 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
11682 | for_each_vsi(i, datasec, vsi) { |
11683 | if (vsi->type == id) { | |
11684 | percpu = true; | |
11685 | break; | |
11686 | } | |
11687 | } | |
11688 | } | |
11689 | ||
4976b718 HL |
11690 | insn[0].imm = (u32)addr; |
11691 | insn[1].imm = addr >> 32; | |
11692 | ||
11693 | type = t->type; | |
541c3bad | 11694 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 HL |
11695 | if (percpu) { |
11696 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; | |
541c3bad | 11697 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
11698 | aux->btf_var.btf_id = type; |
11699 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
11700 | const struct btf_type *ret; |
11701 | const char *tname; | |
11702 | u32 tsize; | |
11703 | ||
11704 | /* resolve the type size of ksym. */ | |
541c3bad | 11705 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 11706 | if (IS_ERR(ret)) { |
541c3bad | 11707 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
11708 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
11709 | tname, PTR_ERR(ret)); | |
541c3bad AN |
11710 | err = -EINVAL; |
11711 | goto err_put; | |
4976b718 HL |
11712 | } |
11713 | aux->btf_var.reg_type = PTR_TO_MEM; | |
11714 | aux->btf_var.mem_size = tsize; | |
11715 | } else { | |
11716 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 11717 | aux->btf_var.btf = btf; |
4976b718 HL |
11718 | aux->btf_var.btf_id = type; |
11719 | } | |
541c3bad AN |
11720 | |
11721 | /* check whether we recorded this BTF (and maybe module) already */ | |
11722 | for (i = 0; i < env->used_btf_cnt; i++) { | |
11723 | if (env->used_btfs[i].btf == btf) { | |
11724 | btf_put(btf); | |
11725 | return 0; | |
11726 | } | |
11727 | } | |
11728 | ||
11729 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
11730 | err = -E2BIG; | |
11731 | goto err_put; | |
11732 | } | |
11733 | ||
11734 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
11735 | btf_mod->btf = btf; | |
11736 | btf_mod->module = NULL; | |
11737 | ||
11738 | /* if we reference variables from kernel module, bump its refcount */ | |
11739 | if (btf_is_module(btf)) { | |
11740 | btf_mod->module = btf_try_get_module(btf); | |
11741 | if (!btf_mod->module) { | |
11742 | err = -ENXIO; | |
11743 | goto err_put; | |
11744 | } | |
11745 | } | |
11746 | ||
11747 | env->used_btf_cnt++; | |
11748 | ||
4976b718 | 11749 | return 0; |
541c3bad AN |
11750 | err_put: |
11751 | btf_put(btf); | |
11752 | return err; | |
4976b718 HL |
11753 | } |
11754 | ||
56f668df MKL |
11755 | static int check_map_prealloc(struct bpf_map *map) |
11756 | { | |
11757 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
11758 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
11759 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
11760 | !(map->map_flags & BPF_F_NO_PREALLOC); |
11761 | } | |
11762 | ||
d83525ca AS |
11763 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
11764 | { | |
11765 | switch (type) { | |
11766 | case BPF_PROG_TYPE_KPROBE: | |
11767 | case BPF_PROG_TYPE_TRACEPOINT: | |
11768 | case BPF_PROG_TYPE_PERF_EVENT: | |
11769 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
11770 | return true; | |
11771 | default: | |
11772 | return false; | |
11773 | } | |
11774 | } | |
11775 | ||
94dacdbd TG |
11776 | static bool is_preallocated_map(struct bpf_map *map) |
11777 | { | |
11778 | if (!check_map_prealloc(map)) | |
11779 | return false; | |
11780 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
11781 | return false; | |
11782 | return true; | |
11783 | } | |
11784 | ||
61bd5218 JK |
11785 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
11786 | struct bpf_map *map, | |
fdc15d38 AS |
11787 | struct bpf_prog *prog) |
11788 | ||
11789 | { | |
7e40781c | 11790 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
94dacdbd TG |
11791 | /* |
11792 | * Validate that trace type programs use preallocated hash maps. | |
11793 | * | |
11794 | * For programs attached to PERF events this is mandatory as the | |
11795 | * perf NMI can hit any arbitrary code sequence. | |
11796 | * | |
11797 | * All other trace types using preallocated hash maps are unsafe as | |
11798 | * well because tracepoint or kprobes can be inside locked regions | |
11799 | * of the memory allocator or at a place where a recursion into the | |
11800 | * memory allocator would see inconsistent state. | |
11801 | * | |
2ed905c5 TG |
11802 | * On RT enabled kernels run-time allocation of all trace type |
11803 | * programs is strictly prohibited due to lock type constraints. On | |
11804 | * !RT kernels it is allowed for backwards compatibility reasons for | |
11805 | * now, but warnings are emitted so developers are made aware of | |
11806 | * the unsafety and can fix their programs before this is enforced. | |
56f668df | 11807 | */ |
7e40781c UP |
11808 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
11809 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { | |
61bd5218 | 11810 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
11811 | return -EINVAL; |
11812 | } | |
2ed905c5 TG |
11813 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
11814 | verbose(env, "trace type programs can only use preallocated hash map\n"); | |
11815 | return -EINVAL; | |
11816 | } | |
94dacdbd TG |
11817 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
11818 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
fdc15d38 | 11819 | } |
a3884572 | 11820 | |
9e7a4d98 KS |
11821 | if (map_value_has_spin_lock(map)) { |
11822 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { | |
11823 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
11824 | return -EINVAL; | |
11825 | } | |
11826 | ||
11827 | if (is_tracing_prog_type(prog_type)) { | |
11828 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
11829 | return -EINVAL; | |
11830 | } | |
11831 | ||
11832 | if (prog->aux->sleepable) { | |
11833 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
11834 | return -EINVAL; | |
11835 | } | |
d83525ca AS |
11836 | } |
11837 | ||
5e0bc308 DB |
11838 | if (map_value_has_timer(map)) { |
11839 | if (is_tracing_prog_type(prog_type)) { | |
11840 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
11841 | return -EINVAL; | |
11842 | } | |
11843 | } | |
11844 | ||
a3884572 | 11845 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 11846 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
11847 | verbose(env, "offload device mismatch between prog and map\n"); |
11848 | return -EINVAL; | |
11849 | } | |
11850 | ||
85d33df3 MKL |
11851 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
11852 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
11853 | return -EINVAL; | |
11854 | } | |
11855 | ||
1e6c62a8 AS |
11856 | if (prog->aux->sleepable) |
11857 | switch (map->map_type) { | |
11858 | case BPF_MAP_TYPE_HASH: | |
11859 | case BPF_MAP_TYPE_LRU_HASH: | |
11860 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
11861 | case BPF_MAP_TYPE_PERCPU_HASH: |
11862 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
11863 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
11864 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
11865 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
1e6c62a8 AS |
11866 | if (!is_preallocated_map(map)) { |
11867 | verbose(env, | |
638e4b82 | 11868 | "Sleepable programs can only use preallocated maps\n"); |
1e6c62a8 AS |
11869 | return -EINVAL; |
11870 | } | |
11871 | break; | |
ba90c2cc KS |
11872 | case BPF_MAP_TYPE_RINGBUF: |
11873 | break; | |
1e6c62a8 AS |
11874 | default: |
11875 | verbose(env, | |
ba90c2cc | 11876 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
1e6c62a8 AS |
11877 | return -EINVAL; |
11878 | } | |
11879 | ||
fdc15d38 AS |
11880 | return 0; |
11881 | } | |
11882 | ||
b741f163 RG |
11883 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
11884 | { | |
11885 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
11886 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
11887 | } | |
11888 | ||
4976b718 HL |
11889 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
11890 | * | |
11891 | * 1. if it accesses map FD, replace it with actual map pointer. | |
11892 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
11893 | * | |
11894 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 11895 | */ |
4976b718 | 11896 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
11897 | { |
11898 | struct bpf_insn *insn = env->prog->insnsi; | |
11899 | int insn_cnt = env->prog->len; | |
fdc15d38 | 11900 | int i, j, err; |
0246e64d | 11901 | |
f1f7714e | 11902 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
11903 | if (err) |
11904 | return err; | |
11905 | ||
0246e64d | 11906 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 11907 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 11908 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 11909 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
11910 | return -EINVAL; |
11911 | } | |
11912 | ||
0246e64d | 11913 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 11914 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
11915 | struct bpf_map *map; |
11916 | struct fd f; | |
d8eca5bb | 11917 | u64 addr; |
387544bf | 11918 | u32 fd; |
0246e64d AS |
11919 | |
11920 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
11921 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
11922 | insn[1].off != 0) { | |
61bd5218 | 11923 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
11924 | return -EINVAL; |
11925 | } | |
11926 | ||
d8eca5bb | 11927 | if (insn[0].src_reg == 0) |
0246e64d AS |
11928 | /* valid generic load 64-bit imm */ |
11929 | goto next_insn; | |
11930 | ||
4976b718 HL |
11931 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
11932 | aux = &env->insn_aux_data[i]; | |
11933 | err = check_pseudo_btf_id(env, insn, aux); | |
11934 | if (err) | |
11935 | return err; | |
11936 | goto next_insn; | |
11937 | } | |
11938 | ||
69c087ba YS |
11939 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
11940 | aux = &env->insn_aux_data[i]; | |
11941 | aux->ptr_type = PTR_TO_FUNC; | |
11942 | goto next_insn; | |
11943 | } | |
11944 | ||
d8eca5bb DB |
11945 | /* In final convert_pseudo_ld_imm64() step, this is |
11946 | * converted into regular 64-bit imm load insn. | |
11947 | */ | |
387544bf AS |
11948 | switch (insn[0].src_reg) { |
11949 | case BPF_PSEUDO_MAP_VALUE: | |
11950 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
11951 | break; | |
11952 | case BPF_PSEUDO_MAP_FD: | |
11953 | case BPF_PSEUDO_MAP_IDX: | |
11954 | if (insn[1].imm == 0) | |
11955 | break; | |
11956 | fallthrough; | |
11957 | default: | |
11958 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
11959 | return -EINVAL; |
11960 | } | |
11961 | ||
387544bf AS |
11962 | switch (insn[0].src_reg) { |
11963 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
11964 | case BPF_PSEUDO_MAP_IDX: | |
11965 | if (bpfptr_is_null(env->fd_array)) { | |
11966 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
11967 | return -EPROTO; | |
11968 | } | |
11969 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
11970 | insn[0].imm * sizeof(fd), | |
11971 | sizeof(fd))) | |
11972 | return -EFAULT; | |
11973 | break; | |
11974 | default: | |
11975 | fd = insn[0].imm; | |
11976 | break; | |
11977 | } | |
11978 | ||
11979 | f = fdget(fd); | |
c2101297 | 11980 | map = __bpf_map_get(f); |
0246e64d | 11981 | if (IS_ERR(map)) { |
61bd5218 | 11982 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 11983 | insn[0].imm); |
0246e64d AS |
11984 | return PTR_ERR(map); |
11985 | } | |
11986 | ||
61bd5218 | 11987 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
11988 | if (err) { |
11989 | fdput(f); | |
11990 | return err; | |
11991 | } | |
11992 | ||
d8eca5bb | 11993 | aux = &env->insn_aux_data[i]; |
387544bf AS |
11994 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
11995 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
11996 | addr = (unsigned long)map; |
11997 | } else { | |
11998 | u32 off = insn[1].imm; | |
11999 | ||
12000 | if (off >= BPF_MAX_VAR_OFF) { | |
12001 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
12002 | fdput(f); | |
12003 | return -EINVAL; | |
12004 | } | |
12005 | ||
12006 | if (!map->ops->map_direct_value_addr) { | |
12007 | verbose(env, "no direct value access support for this map type\n"); | |
12008 | fdput(f); | |
12009 | return -EINVAL; | |
12010 | } | |
12011 | ||
12012 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
12013 | if (err) { | |
12014 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
12015 | map->value_size, off); | |
12016 | fdput(f); | |
12017 | return err; | |
12018 | } | |
12019 | ||
12020 | aux->map_off = off; | |
12021 | addr += off; | |
12022 | } | |
12023 | ||
12024 | insn[0].imm = (u32)addr; | |
12025 | insn[1].imm = addr >> 32; | |
0246e64d AS |
12026 | |
12027 | /* check whether we recorded this map already */ | |
d8eca5bb | 12028 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 12029 | if (env->used_maps[j] == map) { |
d8eca5bb | 12030 | aux->map_index = j; |
0246e64d AS |
12031 | fdput(f); |
12032 | goto next_insn; | |
12033 | } | |
d8eca5bb | 12034 | } |
0246e64d AS |
12035 | |
12036 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
12037 | fdput(f); | |
12038 | return -E2BIG; | |
12039 | } | |
12040 | ||
0246e64d AS |
12041 | /* hold the map. If the program is rejected by verifier, |
12042 | * the map will be released by release_maps() or it | |
12043 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 12044 | * and all maps are released in free_used_maps() |
0246e64d | 12045 | */ |
1e0bd5a0 | 12046 | bpf_map_inc(map); |
d8eca5bb DB |
12047 | |
12048 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
12049 | env->used_maps[env->used_map_cnt++] = map; |
12050 | ||
b741f163 | 12051 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 12052 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 12053 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
12054 | fdput(f); |
12055 | return -EBUSY; | |
12056 | } | |
12057 | ||
0246e64d AS |
12058 | fdput(f); |
12059 | next_insn: | |
12060 | insn++; | |
12061 | i++; | |
5e581dad DB |
12062 | continue; |
12063 | } | |
12064 | ||
12065 | /* Basic sanity check before we invest more work here. */ | |
12066 | if (!bpf_opcode_in_insntable(insn->code)) { | |
12067 | verbose(env, "unknown opcode %02x\n", insn->code); | |
12068 | return -EINVAL; | |
0246e64d AS |
12069 | } |
12070 | } | |
12071 | ||
12072 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
12073 | * 'struct bpf_map *' into a register instead of user map_fd. | |
12074 | * These pointers will be used later by verifier to validate map access. | |
12075 | */ | |
12076 | return 0; | |
12077 | } | |
12078 | ||
12079 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 12080 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 12081 | { |
a2ea0746 DB |
12082 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
12083 | env->used_map_cnt); | |
0246e64d AS |
12084 | } |
12085 | ||
541c3bad AN |
12086 | /* drop refcnt of maps used by the rejected program */ |
12087 | static void release_btfs(struct bpf_verifier_env *env) | |
12088 | { | |
12089 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
12090 | env->used_btf_cnt); | |
12091 | } | |
12092 | ||
0246e64d | 12093 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 12094 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
12095 | { |
12096 | struct bpf_insn *insn = env->prog->insnsi; | |
12097 | int insn_cnt = env->prog->len; | |
12098 | int i; | |
12099 | ||
69c087ba YS |
12100 | for (i = 0; i < insn_cnt; i++, insn++) { |
12101 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
12102 | continue; | |
12103 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
12104 | continue; | |
12105 | insn->src_reg = 0; | |
12106 | } | |
0246e64d AS |
12107 | } |
12108 | ||
8041902d AS |
12109 | /* single env->prog->insni[off] instruction was replaced with the range |
12110 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
12111 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
12112 | */ | |
75f0fc7b HF |
12113 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
12114 | struct bpf_insn_aux_data *new_data, | |
12115 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 12116 | { |
75f0fc7b | 12117 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 12118 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 12119 | u32 old_seen = old_data[off].seen; |
b325fbca | 12120 | u32 prog_len; |
c131187d | 12121 | int i; |
8041902d | 12122 | |
b325fbca JW |
12123 | /* aux info at OFF always needs adjustment, no matter fast path |
12124 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
12125 | * original insn at old prog. | |
12126 | */ | |
12127 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
12128 | ||
8041902d | 12129 | if (cnt == 1) |
75f0fc7b | 12130 | return; |
b325fbca | 12131 | prog_len = new_prog->len; |
75f0fc7b | 12132 | |
8041902d AS |
12133 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
12134 | memcpy(new_data + off + cnt - 1, old_data + off, | |
12135 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 12136 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
12137 | /* Expand insni[off]'s seen count to the patched range. */ |
12138 | new_data[i].seen = old_seen; | |
b325fbca JW |
12139 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
12140 | } | |
8041902d AS |
12141 | env->insn_aux_data = new_data; |
12142 | vfree(old_data); | |
8041902d AS |
12143 | } |
12144 | ||
cc8b0b92 AS |
12145 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
12146 | { | |
12147 | int i; | |
12148 | ||
12149 | if (len == 1) | |
12150 | return; | |
4cb3d99c JW |
12151 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
12152 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 12153 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 12154 | continue; |
9c8105bd | 12155 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
12156 | } |
12157 | } | |
12158 | ||
7506d211 | 12159 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
12160 | { |
12161 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
12162 | int i, sz = prog->aux->size_poke_tab; | |
12163 | struct bpf_jit_poke_descriptor *desc; | |
12164 | ||
12165 | for (i = 0; i < sz; i++) { | |
12166 | desc = &tab[i]; | |
7506d211 JF |
12167 | if (desc->insn_idx <= off) |
12168 | continue; | |
a748c697 MF |
12169 | desc->insn_idx += len - 1; |
12170 | } | |
12171 | } | |
12172 | ||
8041902d AS |
12173 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
12174 | const struct bpf_insn *patch, u32 len) | |
12175 | { | |
12176 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
12177 | struct bpf_insn_aux_data *new_data = NULL; |
12178 | ||
12179 | if (len > 1) { | |
12180 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
12181 | sizeof(struct bpf_insn_aux_data))); | |
12182 | if (!new_data) | |
12183 | return NULL; | |
12184 | } | |
8041902d AS |
12185 | |
12186 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
12187 | if (IS_ERR(new_prog)) { |
12188 | if (PTR_ERR(new_prog) == -ERANGE) | |
12189 | verbose(env, | |
12190 | "insn %d cannot be patched due to 16-bit range\n", | |
12191 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 12192 | vfree(new_data); |
8041902d | 12193 | return NULL; |
4f73379e | 12194 | } |
75f0fc7b | 12195 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 12196 | adjust_subprog_starts(env, off, len); |
7506d211 | 12197 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
12198 | return new_prog; |
12199 | } | |
12200 | ||
52875a04 JK |
12201 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
12202 | u32 off, u32 cnt) | |
12203 | { | |
12204 | int i, j; | |
12205 | ||
12206 | /* find first prog starting at or after off (first to remove) */ | |
12207 | for (i = 0; i < env->subprog_cnt; i++) | |
12208 | if (env->subprog_info[i].start >= off) | |
12209 | break; | |
12210 | /* find first prog starting at or after off + cnt (first to stay) */ | |
12211 | for (j = i; j < env->subprog_cnt; j++) | |
12212 | if (env->subprog_info[j].start >= off + cnt) | |
12213 | break; | |
12214 | /* if j doesn't start exactly at off + cnt, we are just removing | |
12215 | * the front of previous prog | |
12216 | */ | |
12217 | if (env->subprog_info[j].start != off + cnt) | |
12218 | j--; | |
12219 | ||
12220 | if (j > i) { | |
12221 | struct bpf_prog_aux *aux = env->prog->aux; | |
12222 | int move; | |
12223 | ||
12224 | /* move fake 'exit' subprog as well */ | |
12225 | move = env->subprog_cnt + 1 - j; | |
12226 | ||
12227 | memmove(env->subprog_info + i, | |
12228 | env->subprog_info + j, | |
12229 | sizeof(*env->subprog_info) * move); | |
12230 | env->subprog_cnt -= j - i; | |
12231 | ||
12232 | /* remove func_info */ | |
12233 | if (aux->func_info) { | |
12234 | move = aux->func_info_cnt - j; | |
12235 | ||
12236 | memmove(aux->func_info + i, | |
12237 | aux->func_info + j, | |
12238 | sizeof(*aux->func_info) * move); | |
12239 | aux->func_info_cnt -= j - i; | |
12240 | /* func_info->insn_off is set after all code rewrites, | |
12241 | * in adjust_btf_func() - no need to adjust | |
12242 | */ | |
12243 | } | |
12244 | } else { | |
12245 | /* convert i from "first prog to remove" to "first to adjust" */ | |
12246 | if (env->subprog_info[i].start == off) | |
12247 | i++; | |
12248 | } | |
12249 | ||
12250 | /* update fake 'exit' subprog as well */ | |
12251 | for (; i <= env->subprog_cnt; i++) | |
12252 | env->subprog_info[i].start -= cnt; | |
12253 | ||
12254 | return 0; | |
12255 | } | |
12256 | ||
12257 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
12258 | u32 cnt) | |
12259 | { | |
12260 | struct bpf_prog *prog = env->prog; | |
12261 | u32 i, l_off, l_cnt, nr_linfo; | |
12262 | struct bpf_line_info *linfo; | |
12263 | ||
12264 | nr_linfo = prog->aux->nr_linfo; | |
12265 | if (!nr_linfo) | |
12266 | return 0; | |
12267 | ||
12268 | linfo = prog->aux->linfo; | |
12269 | ||
12270 | /* find first line info to remove, count lines to be removed */ | |
12271 | for (i = 0; i < nr_linfo; i++) | |
12272 | if (linfo[i].insn_off >= off) | |
12273 | break; | |
12274 | ||
12275 | l_off = i; | |
12276 | l_cnt = 0; | |
12277 | for (; i < nr_linfo; i++) | |
12278 | if (linfo[i].insn_off < off + cnt) | |
12279 | l_cnt++; | |
12280 | else | |
12281 | break; | |
12282 | ||
12283 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
12284 | * last removed linfo. prog is already modified, so prog->len == off | |
12285 | * means no live instructions after (tail of the program was removed). | |
12286 | */ | |
12287 | if (prog->len != off && l_cnt && | |
12288 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
12289 | l_cnt--; | |
12290 | linfo[--i].insn_off = off + cnt; | |
12291 | } | |
12292 | ||
12293 | /* remove the line info which refer to the removed instructions */ | |
12294 | if (l_cnt) { | |
12295 | memmove(linfo + l_off, linfo + i, | |
12296 | sizeof(*linfo) * (nr_linfo - i)); | |
12297 | ||
12298 | prog->aux->nr_linfo -= l_cnt; | |
12299 | nr_linfo = prog->aux->nr_linfo; | |
12300 | } | |
12301 | ||
12302 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
12303 | for (i = l_off; i < nr_linfo; i++) | |
12304 | linfo[i].insn_off -= cnt; | |
12305 | ||
12306 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
12307 | for (i = 0; i <= env->subprog_cnt; i++) | |
12308 | if (env->subprog_info[i].linfo_idx > l_off) { | |
12309 | /* program may have started in the removed region but | |
12310 | * may not be fully removed | |
12311 | */ | |
12312 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
12313 | env->subprog_info[i].linfo_idx -= l_cnt; | |
12314 | else | |
12315 | env->subprog_info[i].linfo_idx = l_off; | |
12316 | } | |
12317 | ||
12318 | return 0; | |
12319 | } | |
12320 | ||
12321 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
12322 | { | |
12323 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12324 | unsigned int orig_prog_len = env->prog->len; | |
12325 | int err; | |
12326 | ||
08ca90af JK |
12327 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
12328 | bpf_prog_offload_remove_insns(env, off, cnt); | |
12329 | ||
52875a04 JK |
12330 | err = bpf_remove_insns(env->prog, off, cnt); |
12331 | if (err) | |
12332 | return err; | |
12333 | ||
12334 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
12335 | if (err) | |
12336 | return err; | |
12337 | ||
12338 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
12339 | if (err) | |
12340 | return err; | |
12341 | ||
12342 | memmove(aux_data + off, aux_data + off + cnt, | |
12343 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
12344 | ||
12345 | return 0; | |
12346 | } | |
12347 | ||
2a5418a1 DB |
12348 | /* The verifier does more data flow analysis than llvm and will not |
12349 | * explore branches that are dead at run time. Malicious programs can | |
12350 | * have dead code too. Therefore replace all dead at-run-time code | |
12351 | * with 'ja -1'. | |
12352 | * | |
12353 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
12354 | * program and through another bug we would manage to jump there, then | |
12355 | * we'd execute beyond program memory otherwise. Returning exception | |
12356 | * code also wouldn't work since we can have subprogs where the dead | |
12357 | * code could be located. | |
c131187d AS |
12358 | */ |
12359 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
12360 | { | |
12361 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 12362 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
12363 | struct bpf_insn *insn = env->prog->insnsi; |
12364 | const int insn_cnt = env->prog->len; | |
12365 | int i; | |
12366 | ||
12367 | for (i = 0; i < insn_cnt; i++) { | |
12368 | if (aux_data[i].seen) | |
12369 | continue; | |
2a5418a1 | 12370 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 12371 | aux_data[i].zext_dst = false; |
c131187d AS |
12372 | } |
12373 | } | |
12374 | ||
e2ae4ca2 JK |
12375 | static bool insn_is_cond_jump(u8 code) |
12376 | { | |
12377 | u8 op; | |
12378 | ||
092ed096 JW |
12379 | if (BPF_CLASS(code) == BPF_JMP32) |
12380 | return true; | |
12381 | ||
e2ae4ca2 JK |
12382 | if (BPF_CLASS(code) != BPF_JMP) |
12383 | return false; | |
12384 | ||
12385 | op = BPF_OP(code); | |
12386 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
12387 | } | |
12388 | ||
12389 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
12390 | { | |
12391 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12392 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
12393 | struct bpf_insn *insn = env->prog->insnsi; | |
12394 | const int insn_cnt = env->prog->len; | |
12395 | int i; | |
12396 | ||
12397 | for (i = 0; i < insn_cnt; i++, insn++) { | |
12398 | if (!insn_is_cond_jump(insn->code)) | |
12399 | continue; | |
12400 | ||
12401 | if (!aux_data[i + 1].seen) | |
12402 | ja.off = insn->off; | |
12403 | else if (!aux_data[i + 1 + insn->off].seen) | |
12404 | ja.off = 0; | |
12405 | else | |
12406 | continue; | |
12407 | ||
08ca90af JK |
12408 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
12409 | bpf_prog_offload_replace_insn(env, i, &ja); | |
12410 | ||
e2ae4ca2 JK |
12411 | memcpy(insn, &ja, sizeof(ja)); |
12412 | } | |
12413 | } | |
12414 | ||
52875a04 JK |
12415 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
12416 | { | |
12417 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12418 | int insn_cnt = env->prog->len; | |
12419 | int i, err; | |
12420 | ||
12421 | for (i = 0; i < insn_cnt; i++) { | |
12422 | int j; | |
12423 | ||
12424 | j = 0; | |
12425 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
12426 | j++; | |
12427 | if (!j) | |
12428 | continue; | |
12429 | ||
12430 | err = verifier_remove_insns(env, i, j); | |
12431 | if (err) | |
12432 | return err; | |
12433 | insn_cnt = env->prog->len; | |
12434 | } | |
12435 | ||
12436 | return 0; | |
12437 | } | |
12438 | ||
a1b14abc JK |
12439 | static int opt_remove_nops(struct bpf_verifier_env *env) |
12440 | { | |
12441 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
12442 | struct bpf_insn *insn = env->prog->insnsi; | |
12443 | int insn_cnt = env->prog->len; | |
12444 | int i, err; | |
12445 | ||
12446 | for (i = 0; i < insn_cnt; i++) { | |
12447 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
12448 | continue; | |
12449 | ||
12450 | err = verifier_remove_insns(env, i, 1); | |
12451 | if (err) | |
12452 | return err; | |
12453 | insn_cnt--; | |
12454 | i--; | |
12455 | } | |
12456 | ||
12457 | return 0; | |
12458 | } | |
12459 | ||
d6c2308c JW |
12460 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
12461 | const union bpf_attr *attr) | |
a4b1d3c1 | 12462 | { |
d6c2308c | 12463 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 12464 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 12465 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 12466 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 12467 | struct bpf_prog *new_prog; |
d6c2308c | 12468 | bool rnd_hi32; |
a4b1d3c1 | 12469 | |
d6c2308c | 12470 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 12471 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
12472 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
12473 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
12474 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
12475 | for (i = 0; i < len; i++) { |
12476 | int adj_idx = i + delta; | |
12477 | struct bpf_insn insn; | |
83a28819 | 12478 | int load_reg; |
a4b1d3c1 | 12479 | |
d6c2308c | 12480 | insn = insns[adj_idx]; |
83a28819 | 12481 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
12482 | if (!aux[adj_idx].zext_dst) { |
12483 | u8 code, class; | |
12484 | u32 imm_rnd; | |
12485 | ||
12486 | if (!rnd_hi32) | |
12487 | continue; | |
12488 | ||
12489 | code = insn.code; | |
12490 | class = BPF_CLASS(code); | |
83a28819 | 12491 | if (load_reg == -1) |
d6c2308c JW |
12492 | continue; |
12493 | ||
12494 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
12495 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
12496 | * here. | |
d6c2308c | 12497 | */ |
83a28819 | 12498 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
12499 | if (class == BPF_LD && |
12500 | BPF_MODE(code) == BPF_IMM) | |
12501 | i++; | |
12502 | continue; | |
12503 | } | |
12504 | ||
12505 | /* ctx load could be transformed into wider load. */ | |
12506 | if (class == BPF_LDX && | |
12507 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
12508 | continue; | |
12509 | ||
12510 | imm_rnd = get_random_int(); | |
12511 | rnd_hi32_patch[0] = insn; | |
12512 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 12513 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
12514 | patch = rnd_hi32_patch; |
12515 | patch_len = 4; | |
12516 | goto apply_patch_buffer; | |
12517 | } | |
12518 | ||
39491867 BJ |
12519 | /* Add in an zero-extend instruction if a) the JIT has requested |
12520 | * it or b) it's a CMPXCHG. | |
12521 | * | |
12522 | * The latter is because: BPF_CMPXCHG always loads a value into | |
12523 | * R0, therefore always zero-extends. However some archs' | |
12524 | * equivalent instruction only does this load when the | |
12525 | * comparison is successful. This detail of CMPXCHG is | |
12526 | * orthogonal to the general zero-extension behaviour of the | |
12527 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
12528 | */ | |
12529 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
12530 | continue; |
12531 | ||
83a28819 IL |
12532 | if (WARN_ON(load_reg == -1)) { |
12533 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
12534 | return -EFAULT; | |
b2e37a71 IL |
12535 | } |
12536 | ||
a4b1d3c1 | 12537 | zext_patch[0] = insn; |
b2e37a71 IL |
12538 | zext_patch[1].dst_reg = load_reg; |
12539 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
12540 | patch = zext_patch; |
12541 | patch_len = 2; | |
12542 | apply_patch_buffer: | |
12543 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
12544 | if (!new_prog) |
12545 | return -ENOMEM; | |
12546 | env->prog = new_prog; | |
12547 | insns = new_prog->insnsi; | |
12548 | aux = env->insn_aux_data; | |
d6c2308c | 12549 | delta += patch_len - 1; |
a4b1d3c1 JW |
12550 | } |
12551 | ||
12552 | return 0; | |
12553 | } | |
12554 | ||
c64b7983 JS |
12555 | /* convert load instructions that access fields of a context type into a |
12556 | * sequence of instructions that access fields of the underlying structure: | |
12557 | * struct __sk_buff -> struct sk_buff | |
12558 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 12559 | */ |
58e2af8b | 12560 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 12561 | { |
00176a34 | 12562 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 12563 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 12564 | const int insn_cnt = env->prog->len; |
36bbef52 | 12565 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 12566 | u32 target_size, size_default, off; |
9bac3d6d | 12567 | struct bpf_prog *new_prog; |
d691f9e8 | 12568 | enum bpf_access_type type; |
f96da094 | 12569 | bool is_narrower_load; |
9bac3d6d | 12570 | |
b09928b9 DB |
12571 | if (ops->gen_prologue || env->seen_direct_write) { |
12572 | if (!ops->gen_prologue) { | |
12573 | verbose(env, "bpf verifier is misconfigured\n"); | |
12574 | return -EINVAL; | |
12575 | } | |
36bbef52 DB |
12576 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
12577 | env->prog); | |
12578 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 12579 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
12580 | return -EINVAL; |
12581 | } else if (cnt) { | |
8041902d | 12582 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
12583 | if (!new_prog) |
12584 | return -ENOMEM; | |
8041902d | 12585 | |
36bbef52 | 12586 | env->prog = new_prog; |
3df126f3 | 12587 | delta += cnt - 1; |
36bbef52 DB |
12588 | } |
12589 | } | |
12590 | ||
c64b7983 | 12591 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
12592 | return 0; |
12593 | ||
3df126f3 | 12594 | insn = env->prog->insnsi + delta; |
36bbef52 | 12595 | |
9bac3d6d | 12596 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 | 12597 | bpf_convert_ctx_access_t convert_ctx_access; |
2039f26f | 12598 | bool ctx_access; |
c64b7983 | 12599 | |
62c7989b DB |
12600 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
12601 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
12602 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 12603 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 12604 | type = BPF_READ; |
2039f26f DB |
12605 | ctx_access = true; |
12606 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || | |
12607 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
12608 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
12609 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
12610 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
12611 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
12612 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
12613 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 12614 | type = BPF_WRITE; |
2039f26f DB |
12615 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
12616 | } else { | |
9bac3d6d | 12617 | continue; |
2039f26f | 12618 | } |
9bac3d6d | 12619 | |
af86ca4e | 12620 | if (type == BPF_WRITE && |
2039f26f | 12621 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 12622 | struct bpf_insn patch[] = { |
af86ca4e | 12623 | *insn, |
2039f26f | 12624 | BPF_ST_NOSPEC(), |
af86ca4e AS |
12625 | }; |
12626 | ||
12627 | cnt = ARRAY_SIZE(patch); | |
12628 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
12629 | if (!new_prog) | |
12630 | return -ENOMEM; | |
12631 | ||
12632 | delta += cnt - 1; | |
12633 | env->prog = new_prog; | |
12634 | insn = new_prog->insnsi + i + delta; | |
12635 | continue; | |
12636 | } | |
12637 | ||
2039f26f DB |
12638 | if (!ctx_access) |
12639 | continue; | |
12640 | ||
c64b7983 JS |
12641 | switch (env->insn_aux_data[i + delta].ptr_type) { |
12642 | case PTR_TO_CTX: | |
12643 | if (!ops->convert_ctx_access) | |
12644 | continue; | |
12645 | convert_ctx_access = ops->convert_ctx_access; | |
12646 | break; | |
12647 | case PTR_TO_SOCKET: | |
46f8bc92 | 12648 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
12649 | convert_ctx_access = bpf_sock_convert_ctx_access; |
12650 | break; | |
655a51e5 MKL |
12651 | case PTR_TO_TCP_SOCK: |
12652 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
12653 | break; | |
fada7fdc JL |
12654 | case PTR_TO_XDP_SOCK: |
12655 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
12656 | break; | |
2a02759e | 12657 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
12658 | if (type == BPF_READ) { |
12659 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
12660 | BPF_SIZE((insn)->code); | |
12661 | env->prog->aux->num_exentries++; | |
7e40781c | 12662 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
2a02759e AS |
12663 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
12664 | return -EINVAL; | |
12665 | } | |
2a02759e | 12666 | continue; |
c64b7983 | 12667 | default: |
9bac3d6d | 12668 | continue; |
c64b7983 | 12669 | } |
9bac3d6d | 12670 | |
31fd8581 | 12671 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 12672 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
12673 | |
12674 | /* If the read access is a narrower load of the field, | |
12675 | * convert to a 4/8-byte load, to minimum program type specific | |
12676 | * convert_ctx_access changes. If conversion is successful, | |
12677 | * we will apply proper mask to the result. | |
12678 | */ | |
f96da094 | 12679 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
12680 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
12681 | off = insn->off; | |
31fd8581 | 12682 | if (is_narrower_load) { |
f96da094 DB |
12683 | u8 size_code; |
12684 | ||
12685 | if (type == BPF_WRITE) { | |
61bd5218 | 12686 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
12687 | return -EINVAL; |
12688 | } | |
31fd8581 | 12689 | |
f96da094 | 12690 | size_code = BPF_H; |
31fd8581 YS |
12691 | if (ctx_field_size == 4) |
12692 | size_code = BPF_W; | |
12693 | else if (ctx_field_size == 8) | |
12694 | size_code = BPF_DW; | |
f96da094 | 12695 | |
bc23105c | 12696 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
12697 | insn->code = BPF_LDX | BPF_MEM | size_code; |
12698 | } | |
f96da094 DB |
12699 | |
12700 | target_size = 0; | |
c64b7983 JS |
12701 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
12702 | &target_size); | |
f96da094 DB |
12703 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
12704 | (ctx_field_size && !target_size)) { | |
61bd5218 | 12705 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
12706 | return -EINVAL; |
12707 | } | |
f96da094 DB |
12708 | |
12709 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
12710 | u8 shift = bpf_ctx_narrow_access_offset( |
12711 | off, size, size_default) * 8; | |
d7af7e49 AI |
12712 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
12713 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
12714 | return -EINVAL; | |
12715 | } | |
46f53a65 AI |
12716 | if (ctx_field_size <= 4) { |
12717 | if (shift) | |
12718 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
12719 | insn->dst_reg, | |
12720 | shift); | |
31fd8581 | 12721 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 12722 | (1 << size * 8) - 1); |
46f53a65 AI |
12723 | } else { |
12724 | if (shift) | |
12725 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
12726 | insn->dst_reg, | |
12727 | shift); | |
31fd8581 | 12728 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 12729 | (1ULL << size * 8) - 1); |
46f53a65 | 12730 | } |
31fd8581 | 12731 | } |
9bac3d6d | 12732 | |
8041902d | 12733 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
12734 | if (!new_prog) |
12735 | return -ENOMEM; | |
12736 | ||
3df126f3 | 12737 | delta += cnt - 1; |
9bac3d6d AS |
12738 | |
12739 | /* keep walking new program and skip insns we just inserted */ | |
12740 | env->prog = new_prog; | |
3df126f3 | 12741 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
12742 | } |
12743 | ||
12744 | return 0; | |
12745 | } | |
12746 | ||
1c2a088a AS |
12747 | static int jit_subprogs(struct bpf_verifier_env *env) |
12748 | { | |
12749 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
12750 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 12751 | struct bpf_map *map_ptr; |
7105e828 | 12752 | struct bpf_insn *insn; |
1c2a088a | 12753 | void *old_bpf_func; |
c4c0bdc0 | 12754 | int err, num_exentries; |
1c2a088a | 12755 | |
f910cefa | 12756 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
12757 | return 0; |
12758 | ||
7105e828 | 12759 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 12760 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 12761 | continue; |
69c087ba | 12762 | |
c7a89784 DB |
12763 | /* Upon error here we cannot fall back to interpreter but |
12764 | * need a hard reject of the program. Thus -EFAULT is | |
12765 | * propagated in any case. | |
12766 | */ | |
1c2a088a AS |
12767 | subprog = find_subprog(env, i + insn->imm + 1); |
12768 | if (subprog < 0) { | |
12769 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
12770 | i + insn->imm + 1); | |
12771 | return -EFAULT; | |
12772 | } | |
12773 | /* temporarily remember subprog id inside insn instead of | |
12774 | * aux_data, since next loop will split up all insns into funcs | |
12775 | */ | |
f910cefa | 12776 | insn->off = subprog; |
1c2a088a AS |
12777 | /* remember original imm in case JIT fails and fallback |
12778 | * to interpreter will be needed | |
12779 | */ | |
12780 | env->insn_aux_data[i].call_imm = insn->imm; | |
12781 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
12782 | insn->imm = 1; | |
3990ed4c MKL |
12783 | if (bpf_pseudo_func(insn)) |
12784 | /* jit (e.g. x86_64) may emit fewer instructions | |
12785 | * if it learns a u32 imm is the same as a u64 imm. | |
12786 | * Force a non zero here. | |
12787 | */ | |
12788 | insn[1].imm = 1; | |
1c2a088a AS |
12789 | } |
12790 | ||
c454a46b MKL |
12791 | err = bpf_prog_alloc_jited_linfo(prog); |
12792 | if (err) | |
12793 | goto out_undo_insn; | |
12794 | ||
12795 | err = -ENOMEM; | |
6396bb22 | 12796 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 12797 | if (!func) |
c7a89784 | 12798 | goto out_undo_insn; |
1c2a088a | 12799 | |
f910cefa | 12800 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 12801 | subprog_start = subprog_end; |
4cb3d99c | 12802 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
12803 | |
12804 | len = subprog_end - subprog_start; | |
fb7dd8bc | 12805 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
12806 | * hence main prog stats include the runtime of subprogs. |
12807 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 12808 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
12809 | */ |
12810 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
12811 | if (!func[i]) |
12812 | goto out_free; | |
12813 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
12814 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 12815 | func[i]->type = prog->type; |
1c2a088a | 12816 | func[i]->len = len; |
4f74d809 DB |
12817 | if (bpf_prog_calc_tag(func[i])) |
12818 | goto out_free; | |
1c2a088a | 12819 | func[i]->is_func = 1; |
ba64e7d8 | 12820 | func[i]->aux->func_idx = i; |
f263a814 | 12821 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
12822 | func[i]->aux->btf = prog->aux->btf; |
12823 | func[i]->aux->func_info = prog->aux->func_info; | |
f263a814 JF |
12824 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
12825 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 12826 | |
a748c697 | 12827 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 12828 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 12829 | |
f263a814 JF |
12830 | poke = &prog->aux->poke_tab[j]; |
12831 | if (poke->insn_idx < subprog_end && | |
12832 | poke->insn_idx >= subprog_start) | |
12833 | poke->aux = func[i]->aux; | |
a748c697 MF |
12834 | } |
12835 | ||
1c2a088a AS |
12836 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
12837 | * Long term would need debug info to populate names | |
12838 | */ | |
12839 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 12840 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 12841 | func[i]->jit_requested = 1; |
e6ac2450 | 12842 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 12843 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
12844 | func[i]->aux->linfo = prog->aux->linfo; |
12845 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
12846 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
12847 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
12848 | num_exentries = 0; |
12849 | insn = func[i]->insnsi; | |
12850 | for (j = 0; j < func[i]->len; j++, insn++) { | |
12851 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
12852 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
12853 | num_exentries++; | |
12854 | } | |
12855 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 12856 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
12857 | func[i] = bpf_int_jit_compile(func[i]); |
12858 | if (!func[i]->jited) { | |
12859 | err = -ENOTSUPP; | |
12860 | goto out_free; | |
12861 | } | |
12862 | cond_resched(); | |
12863 | } | |
a748c697 | 12864 | |
1c2a088a AS |
12865 | /* at this point all bpf functions were successfully JITed |
12866 | * now populate all bpf_calls with correct addresses and | |
12867 | * run last pass of JIT | |
12868 | */ | |
f910cefa | 12869 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
12870 | insn = func[i]->insnsi; |
12871 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 12872 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 12873 | subprog = insn->off; |
69c087ba YS |
12874 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
12875 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
12876 | continue; | |
12877 | } | |
23a2d70c | 12878 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
12879 | continue; |
12880 | subprog = insn->off; | |
3d717fad | 12881 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 12882 | } |
2162fed4 SD |
12883 | |
12884 | /* we use the aux data to keep a list of the start addresses | |
12885 | * of the JITed images for each function in the program | |
12886 | * | |
12887 | * for some architectures, such as powerpc64, the imm field | |
12888 | * might not be large enough to hold the offset of the start | |
12889 | * address of the callee's JITed image from __bpf_call_base | |
12890 | * | |
12891 | * in such cases, we can lookup the start address of a callee | |
12892 | * by using its subprog id, available from the off field of | |
12893 | * the call instruction, as an index for this list | |
12894 | */ | |
12895 | func[i]->aux->func = func; | |
12896 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 12897 | } |
f910cefa | 12898 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
12899 | old_bpf_func = func[i]->bpf_func; |
12900 | tmp = bpf_int_jit_compile(func[i]); | |
12901 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
12902 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 12903 | err = -ENOTSUPP; |
1c2a088a AS |
12904 | goto out_free; |
12905 | } | |
12906 | cond_resched(); | |
12907 | } | |
12908 | ||
12909 | /* finally lock prog and jit images for all functions and | |
12910 | * populate kallsysm | |
12911 | */ | |
f910cefa | 12912 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
12913 | bpf_prog_lock_ro(func[i]); |
12914 | bpf_prog_kallsyms_add(func[i]); | |
12915 | } | |
7105e828 DB |
12916 | |
12917 | /* Last step: make now unused interpreter insns from main | |
12918 | * prog consistent for later dump requests, so they can | |
12919 | * later look the same as if they were interpreted only. | |
12920 | */ | |
12921 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
12922 | if (bpf_pseudo_func(insn)) { |
12923 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
12924 | insn[1].imm = insn->off; |
12925 | insn->off = 0; | |
69c087ba YS |
12926 | continue; |
12927 | } | |
23a2d70c | 12928 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
12929 | continue; |
12930 | insn->off = env->insn_aux_data[i].call_imm; | |
12931 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 12932 | insn->imm = subprog; |
7105e828 DB |
12933 | } |
12934 | ||
1c2a088a AS |
12935 | prog->jited = 1; |
12936 | prog->bpf_func = func[0]->bpf_func; | |
12937 | prog->aux->func = func; | |
f910cefa | 12938 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 12939 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
12940 | return 0; |
12941 | out_free: | |
f263a814 JF |
12942 | /* We failed JIT'ing, so at this point we need to unregister poke |
12943 | * descriptors from subprogs, so that kernel is not attempting to | |
12944 | * patch it anymore as we're freeing the subprog JIT memory. | |
12945 | */ | |
12946 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
12947 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
12948 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
12949 | } | |
12950 | /* At this point we're guaranteed that poke descriptors are not | |
12951 | * live anymore. We can just unlink its descriptor table as it's | |
12952 | * released with the main prog. | |
12953 | */ | |
a748c697 MF |
12954 | for (i = 0; i < env->subprog_cnt; i++) { |
12955 | if (!func[i]) | |
12956 | continue; | |
f263a814 | 12957 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
12958 | bpf_jit_free(func[i]); |
12959 | } | |
1c2a088a | 12960 | kfree(func); |
c7a89784 | 12961 | out_undo_insn: |
1c2a088a AS |
12962 | /* cleanup main prog to be interpreted */ |
12963 | prog->jit_requested = 0; | |
12964 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
23a2d70c | 12965 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
12966 | continue; |
12967 | insn->off = 0; | |
12968 | insn->imm = env->insn_aux_data[i].call_imm; | |
12969 | } | |
e16301fb | 12970 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
12971 | return err; |
12972 | } | |
12973 | ||
1ea47e01 AS |
12974 | static int fixup_call_args(struct bpf_verifier_env *env) |
12975 | { | |
19d28fbd | 12976 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
12977 | struct bpf_prog *prog = env->prog; |
12978 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 12979 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 12980 | int i, depth; |
19d28fbd | 12981 | #endif |
e4052d06 | 12982 | int err = 0; |
1ea47e01 | 12983 | |
e4052d06 QM |
12984 | if (env->prog->jit_requested && |
12985 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
12986 | err = jit_subprogs(env); |
12987 | if (err == 0) | |
1c2a088a | 12988 | return 0; |
c7a89784 DB |
12989 | if (err == -EFAULT) |
12990 | return err; | |
19d28fbd DM |
12991 | } |
12992 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
12993 | if (has_kfunc_call) { |
12994 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
12995 | return -EINVAL; | |
12996 | } | |
e411901c MF |
12997 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
12998 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
12999 | * have to be rejected, since interpreter doesn't support them yet. | |
13000 | */ | |
13001 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
13002 | return -EINVAL; | |
13003 | } | |
1ea47e01 | 13004 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
13005 | if (bpf_pseudo_func(insn)) { |
13006 | /* When JIT fails the progs with callback calls | |
13007 | * have to be rejected, since interpreter doesn't support them yet. | |
13008 | */ | |
13009 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
13010 | return -EINVAL; | |
13011 | } | |
13012 | ||
23a2d70c | 13013 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
13014 | continue; |
13015 | depth = get_callee_stack_depth(env, insn, i); | |
13016 | if (depth < 0) | |
13017 | return depth; | |
13018 | bpf_patch_call_args(insn, depth); | |
13019 | } | |
19d28fbd DM |
13020 | err = 0; |
13021 | #endif | |
13022 | return err; | |
1ea47e01 AS |
13023 | } |
13024 | ||
e6ac2450 MKL |
13025 | static int fixup_kfunc_call(struct bpf_verifier_env *env, |
13026 | struct bpf_insn *insn) | |
13027 | { | |
13028 | const struct bpf_kfunc_desc *desc; | |
13029 | ||
a5d82727 KKD |
13030 | if (!insn->imm) { |
13031 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
13032 | return -EINVAL; | |
13033 | } | |
13034 | ||
e6ac2450 MKL |
13035 | /* insn->imm has the btf func_id. Replace it with |
13036 | * an address (relative to __bpf_base_call). | |
13037 | */ | |
2357672c | 13038 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
13039 | if (!desc) { |
13040 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
13041 | insn->imm); | |
13042 | return -EFAULT; | |
13043 | } | |
13044 | ||
13045 | insn->imm = desc->imm; | |
13046 | ||
13047 | return 0; | |
13048 | } | |
13049 | ||
e6ac5933 BJ |
13050 | /* Do various post-verification rewrites in a single program pass. |
13051 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 13052 | */ |
e6ac5933 | 13053 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 13054 | { |
79741b3b | 13055 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 13056 | enum bpf_attach_type eatype = prog->expected_attach_type; |
d2e4c1e6 | 13057 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
9b99edca | 13058 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 13059 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 13060 | const struct bpf_func_proto *fn; |
79741b3b | 13061 | const int insn_cnt = prog->len; |
09772d92 | 13062 | const struct bpf_map_ops *ops; |
c93552c4 | 13063 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
13064 | struct bpf_insn insn_buf[16]; |
13065 | struct bpf_prog *new_prog; | |
13066 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 13067 | int i, ret, cnt, delta = 0; |
e245c5c6 | 13068 | |
79741b3b | 13069 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 13070 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
13071 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
13072 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
13073 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 13074 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 13075 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
13076 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
13077 | struct bpf_insn *patchlet; | |
13078 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 13079 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
13080 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
13081 | BPF_JNE | BPF_K, insn->src_reg, | |
13082 | 0, 2, 0), | |
f6b1b3bf DB |
13083 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
13084 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
13085 | *insn, | |
13086 | }; | |
e88b2c6e | 13087 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 13088 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
13089 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
13090 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 13091 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 13092 | *insn, |
9b00f1b7 DB |
13093 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
13094 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 13095 | }; |
f6b1b3bf | 13096 | |
e88b2c6e DB |
13097 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
13098 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 13099 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
13100 | |
13101 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
13102 | if (!new_prog) |
13103 | return -ENOMEM; | |
13104 | ||
13105 | delta += cnt - 1; | |
13106 | env->prog = prog = new_prog; | |
13107 | insn = new_prog->insnsi + i + delta; | |
13108 | continue; | |
13109 | } | |
13110 | ||
e6ac5933 | 13111 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
13112 | if (BPF_CLASS(insn->code) == BPF_LD && |
13113 | (BPF_MODE(insn->code) == BPF_ABS || | |
13114 | BPF_MODE(insn->code) == BPF_IND)) { | |
13115 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
13116 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
13117 | verbose(env, "bpf verifier is misconfigured\n"); | |
13118 | return -EINVAL; | |
13119 | } | |
13120 | ||
13121 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13122 | if (!new_prog) | |
13123 | return -ENOMEM; | |
13124 | ||
13125 | delta += cnt - 1; | |
13126 | env->prog = prog = new_prog; | |
13127 | insn = new_prog->insnsi + i + delta; | |
13128 | continue; | |
13129 | } | |
13130 | ||
e6ac5933 | 13131 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
13132 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
13133 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
13134 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
13135 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 13136 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 13137 | bool issrc, isneg, isimm; |
979d63d5 DB |
13138 | u32 off_reg; |
13139 | ||
13140 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
13141 | if (!aux->alu_state || |
13142 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
13143 | continue; |
13144 | ||
13145 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
13146 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
13147 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 13148 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
13149 | |
13150 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
13151 | if (isimm) { |
13152 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
13153 | } else { | |
13154 | if (isneg) | |
13155 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
13156 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
13157 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
13158 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
13159 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
13160 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
13161 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
13162 | } | |
b9b34ddb DB |
13163 | if (!issrc) |
13164 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
13165 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
13166 | if (isneg) |
13167 | insn->code = insn->code == code_add ? | |
13168 | code_sub : code_add; | |
13169 | *patch++ = *insn; | |
801c6058 | 13170 | if (issrc && isneg && !isimm) |
979d63d5 DB |
13171 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
13172 | cnt = patch - insn_buf; | |
13173 | ||
13174 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13175 | if (!new_prog) | |
13176 | return -ENOMEM; | |
13177 | ||
13178 | delta += cnt - 1; | |
13179 | env->prog = prog = new_prog; | |
13180 | insn = new_prog->insnsi + i + delta; | |
13181 | continue; | |
13182 | } | |
13183 | ||
79741b3b AS |
13184 | if (insn->code != (BPF_JMP | BPF_CALL)) |
13185 | continue; | |
cc8b0b92 AS |
13186 | if (insn->src_reg == BPF_PSEUDO_CALL) |
13187 | continue; | |
e6ac2450 MKL |
13188 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
13189 | ret = fixup_kfunc_call(env, insn); | |
13190 | if (ret) | |
13191 | return ret; | |
13192 | continue; | |
13193 | } | |
e245c5c6 | 13194 | |
79741b3b AS |
13195 | if (insn->imm == BPF_FUNC_get_route_realm) |
13196 | prog->dst_needed = 1; | |
13197 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
13198 | bpf_user_rnd_init_once(); | |
9802d865 JB |
13199 | if (insn->imm == BPF_FUNC_override_return) |
13200 | prog->kprobe_override = 1; | |
79741b3b | 13201 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
13202 | /* If we tail call into other programs, we |
13203 | * cannot make any assumptions since they can | |
13204 | * be replaced dynamically during runtime in | |
13205 | * the program array. | |
13206 | */ | |
13207 | prog->cb_access = 1; | |
e411901c MF |
13208 | if (!allow_tail_call_in_subprogs(env)) |
13209 | prog->aux->stack_depth = MAX_BPF_STACK; | |
13210 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 13211 | |
79741b3b | 13212 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 13213 | * conditional branch in the interpreter for every normal |
79741b3b AS |
13214 | * call and to prevent accidental JITing by JIT compiler |
13215 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 13216 | */ |
79741b3b | 13217 | insn->imm = 0; |
71189fa9 | 13218 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 13219 | |
c93552c4 | 13220 | aux = &env->insn_aux_data[i + delta]; |
2c78ee89 | 13221 | if (env->bpf_capable && !expect_blinding && |
cc52d914 | 13222 | prog->jit_requested && |
d2e4c1e6 DB |
13223 | !bpf_map_key_poisoned(aux) && |
13224 | !bpf_map_ptr_poisoned(aux) && | |
13225 | !bpf_map_ptr_unpriv(aux)) { | |
13226 | struct bpf_jit_poke_descriptor desc = { | |
13227 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
13228 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
13229 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 13230 | .insn_idx = i + delta, |
d2e4c1e6 DB |
13231 | }; |
13232 | ||
13233 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
13234 | if (ret < 0) { | |
13235 | verbose(env, "adding tail call poke descriptor failed\n"); | |
13236 | return ret; | |
13237 | } | |
13238 | ||
13239 | insn->imm = ret + 1; | |
13240 | continue; | |
13241 | } | |
13242 | ||
c93552c4 DB |
13243 | if (!bpf_map_ptr_unpriv(aux)) |
13244 | continue; | |
13245 | ||
b2157399 AS |
13246 | /* instead of changing every JIT dealing with tail_call |
13247 | * emit two extra insns: | |
13248 | * if (index >= max_entries) goto out; | |
13249 | * index &= array->index_mask; | |
13250 | * to avoid out-of-bounds cpu speculation | |
13251 | */ | |
c93552c4 | 13252 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 13253 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
13254 | return -EINVAL; |
13255 | } | |
c93552c4 | 13256 | |
d2e4c1e6 | 13257 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
13258 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
13259 | map_ptr->max_entries, 2); | |
13260 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
13261 | container_of(map_ptr, | |
13262 | struct bpf_array, | |
13263 | map)->index_mask); | |
13264 | insn_buf[2] = *insn; | |
13265 | cnt = 3; | |
13266 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13267 | if (!new_prog) | |
13268 | return -ENOMEM; | |
13269 | ||
13270 | delta += cnt - 1; | |
13271 | env->prog = prog = new_prog; | |
13272 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
13273 | continue; |
13274 | } | |
e245c5c6 | 13275 | |
b00628b1 AS |
13276 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
13277 | /* The verifier will process callback_fn as many times as necessary | |
13278 | * with different maps and the register states prepared by | |
13279 | * set_timer_callback_state will be accurate. | |
13280 | * | |
13281 | * The following use case is valid: | |
13282 | * map1 is shared by prog1, prog2, prog3. | |
13283 | * prog1 calls bpf_timer_init for some map1 elements | |
13284 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
13285 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
13286 | * prog3 calls bpf_timer_start for some map1 elements. | |
13287 | * Those that were not both bpf_timer_init-ed and | |
13288 | * bpf_timer_set_callback-ed will return -EINVAL. | |
13289 | */ | |
13290 | struct bpf_insn ld_addrs[2] = { | |
13291 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
13292 | }; | |
13293 | ||
13294 | insn_buf[0] = ld_addrs[0]; | |
13295 | insn_buf[1] = ld_addrs[1]; | |
13296 | insn_buf[2] = *insn; | |
13297 | cnt = 3; | |
13298 | ||
13299 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13300 | if (!new_prog) | |
13301 | return -ENOMEM; | |
13302 | ||
13303 | delta += cnt - 1; | |
13304 | env->prog = prog = new_prog; | |
13305 | insn = new_prog->insnsi + i + delta; | |
13306 | goto patch_call_imm; | |
13307 | } | |
13308 | ||
89c63074 | 13309 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
13310 | * and other inlining handlers are currently limited to 64 bit |
13311 | * only. | |
89c63074 | 13312 | */ |
60b58afc | 13313 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
13314 | (insn->imm == BPF_FUNC_map_lookup_elem || |
13315 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
13316 | insn->imm == BPF_FUNC_map_delete_elem || |
13317 | insn->imm == BPF_FUNC_map_push_elem || | |
13318 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 13319 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c AI |
13320 | insn->imm == BPF_FUNC_redirect_map || |
13321 | insn->imm == BPF_FUNC_for_each_map_elem)) { | |
c93552c4 DB |
13322 | aux = &env->insn_aux_data[i + delta]; |
13323 | if (bpf_map_ptr_poisoned(aux)) | |
13324 | goto patch_call_imm; | |
13325 | ||
d2e4c1e6 | 13326 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
13327 | ops = map_ptr->ops; |
13328 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
13329 | ops->map_gen_lookup) { | |
13330 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
13331 | if (cnt == -EOPNOTSUPP) |
13332 | goto patch_map_ops_generic; | |
13333 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
13334 | verbose(env, "bpf verifier is misconfigured\n"); |
13335 | return -EINVAL; | |
13336 | } | |
81ed18ab | 13337 | |
09772d92 DB |
13338 | new_prog = bpf_patch_insn_data(env, i + delta, |
13339 | insn_buf, cnt); | |
13340 | if (!new_prog) | |
13341 | return -ENOMEM; | |
81ed18ab | 13342 | |
09772d92 DB |
13343 | delta += cnt - 1; |
13344 | env->prog = prog = new_prog; | |
13345 | insn = new_prog->insnsi + i + delta; | |
13346 | continue; | |
13347 | } | |
81ed18ab | 13348 | |
09772d92 DB |
13349 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
13350 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
13351 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
13352 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
13353 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
13354 | (int (*)(struct bpf_map *map, void *key, void *value, | |
13355 | u64 flags))NULL)); | |
84430d42 DB |
13356 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
13357 | (int (*)(struct bpf_map *map, void *value, | |
13358 | u64 flags))NULL)); | |
13359 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
13360 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
13361 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
13362 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
e6a4750f BT |
13363 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
13364 | (int (*)(struct bpf_map *map, u32 ifindex, u64 flags))NULL)); | |
0640c77c AI |
13365 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
13366 | (int (*)(struct bpf_map *map, | |
13367 | bpf_callback_t callback_fn, | |
13368 | void *callback_ctx, | |
13369 | u64 flags))NULL)); | |
e6a4750f | 13370 | |
4a8f87e6 | 13371 | patch_map_ops_generic: |
09772d92 DB |
13372 | switch (insn->imm) { |
13373 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 13374 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
13375 | continue; |
13376 | case BPF_FUNC_map_update_elem: | |
3d717fad | 13377 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
13378 | continue; |
13379 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 13380 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 13381 | continue; |
84430d42 | 13382 | case BPF_FUNC_map_push_elem: |
3d717fad | 13383 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
13384 | continue; |
13385 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 13386 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
13387 | continue; |
13388 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 13389 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 13390 | continue; |
e6a4750f | 13391 | case BPF_FUNC_redirect_map: |
3d717fad | 13392 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 13393 | continue; |
0640c77c AI |
13394 | case BPF_FUNC_for_each_map_elem: |
13395 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 13396 | continue; |
09772d92 | 13397 | } |
81ed18ab | 13398 | |
09772d92 | 13399 | goto patch_call_imm; |
81ed18ab AS |
13400 | } |
13401 | ||
e6ac5933 | 13402 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
13403 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
13404 | insn->imm == BPF_FUNC_jiffies64) { | |
13405 | struct bpf_insn ld_jiffies_addr[2] = { | |
13406 | BPF_LD_IMM64(BPF_REG_0, | |
13407 | (unsigned long)&jiffies), | |
13408 | }; | |
13409 | ||
13410 | insn_buf[0] = ld_jiffies_addr[0]; | |
13411 | insn_buf[1] = ld_jiffies_addr[1]; | |
13412 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
13413 | BPF_REG_0, 0); | |
13414 | cnt = 3; | |
13415 | ||
13416 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
13417 | cnt); | |
13418 | if (!new_prog) | |
13419 | return -ENOMEM; | |
13420 | ||
13421 | delta += cnt - 1; | |
13422 | env->prog = prog = new_prog; | |
13423 | insn = new_prog->insnsi + i + delta; | |
13424 | continue; | |
13425 | } | |
13426 | ||
f92c1e18 JO |
13427 | /* Implement bpf_get_func_arg inline. */ |
13428 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13429 | insn->imm == BPF_FUNC_get_func_arg) { | |
13430 | /* Load nr_args from ctx - 8 */ | |
13431 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
13432 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
13433 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
13434 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
13435 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
13436 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
13437 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
13438 | insn_buf[7] = BPF_JMP_A(1); | |
13439 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
13440 | cnt = 9; | |
13441 | ||
13442 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13443 | if (!new_prog) | |
13444 | return -ENOMEM; | |
13445 | ||
13446 | delta += cnt - 1; | |
13447 | env->prog = prog = new_prog; | |
13448 | insn = new_prog->insnsi + i + delta; | |
13449 | continue; | |
13450 | } | |
13451 | ||
13452 | /* Implement bpf_get_func_ret inline. */ | |
13453 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13454 | insn->imm == BPF_FUNC_get_func_ret) { | |
13455 | if (eatype == BPF_TRACE_FEXIT || | |
13456 | eatype == BPF_MODIFY_RETURN) { | |
13457 | /* Load nr_args from ctx - 8 */ | |
13458 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
13459 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
13460 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
13461 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
13462 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
13463 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
13464 | cnt = 6; | |
13465 | } else { | |
13466 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
13467 | cnt = 1; | |
13468 | } | |
13469 | ||
13470 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13471 | if (!new_prog) | |
13472 | return -ENOMEM; | |
13473 | ||
13474 | delta += cnt - 1; | |
13475 | env->prog = prog = new_prog; | |
13476 | insn = new_prog->insnsi + i + delta; | |
13477 | continue; | |
13478 | } | |
13479 | ||
13480 | /* Implement get_func_arg_cnt inline. */ | |
13481 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13482 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
13483 | /* Load nr_args from ctx - 8 */ | |
13484 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
13485 | ||
13486 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
13487 | if (!new_prog) | |
13488 | return -ENOMEM; | |
13489 | ||
13490 | env->prog = prog = new_prog; | |
13491 | insn = new_prog->insnsi + i + delta; | |
13492 | continue; | |
13493 | } | |
13494 | ||
9b99edca JO |
13495 | /* Implement bpf_get_func_ip inline. */ |
13496 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13497 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
13498 | /* Load IP address from ctx - 16 */ |
13499 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
13500 | |
13501 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
13502 | if (!new_prog) | |
13503 | return -ENOMEM; | |
13504 | ||
13505 | env->prog = prog = new_prog; | |
13506 | insn = new_prog->insnsi + i + delta; | |
13507 | continue; | |
13508 | } | |
13509 | ||
81ed18ab | 13510 | patch_call_imm: |
5e43f899 | 13511 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
13512 | /* all functions that have prototype and verifier allowed |
13513 | * programs to call them, must be real in-kernel functions | |
13514 | */ | |
13515 | if (!fn->func) { | |
61bd5218 JK |
13516 | verbose(env, |
13517 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
13518 | func_id_name(insn->imm), insn->imm); |
13519 | return -EFAULT; | |
e245c5c6 | 13520 | } |
79741b3b | 13521 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 13522 | } |
e245c5c6 | 13523 | |
d2e4c1e6 DB |
13524 | /* Since poke tab is now finalized, publish aux to tracker. */ |
13525 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
13526 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
13527 | if (!map_ptr->ops->map_poke_track || | |
13528 | !map_ptr->ops->map_poke_untrack || | |
13529 | !map_ptr->ops->map_poke_run) { | |
13530 | verbose(env, "bpf verifier is misconfigured\n"); | |
13531 | return -EINVAL; | |
13532 | } | |
13533 | ||
13534 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
13535 | if (ret < 0) { | |
13536 | verbose(env, "tracking tail call prog failed\n"); | |
13537 | return ret; | |
13538 | } | |
13539 | } | |
13540 | ||
e6ac2450 MKL |
13541 | sort_kfunc_descs_by_imm(env->prog); |
13542 | ||
79741b3b AS |
13543 | return 0; |
13544 | } | |
e245c5c6 | 13545 | |
58e2af8b | 13546 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 13547 | { |
58e2af8b | 13548 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
13549 | int i; |
13550 | ||
9f4686c4 AS |
13551 | sl = env->free_list; |
13552 | while (sl) { | |
13553 | sln = sl->next; | |
13554 | free_verifier_state(&sl->state, false); | |
13555 | kfree(sl); | |
13556 | sl = sln; | |
13557 | } | |
51c39bb1 | 13558 | env->free_list = NULL; |
9f4686c4 | 13559 | |
f1bca824 AS |
13560 | if (!env->explored_states) |
13561 | return; | |
13562 | ||
dc2a4ebc | 13563 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
13564 | sl = env->explored_states[i]; |
13565 | ||
a8f500af AS |
13566 | while (sl) { |
13567 | sln = sl->next; | |
13568 | free_verifier_state(&sl->state, false); | |
13569 | kfree(sl); | |
13570 | sl = sln; | |
13571 | } | |
51c39bb1 | 13572 | env->explored_states[i] = NULL; |
f1bca824 | 13573 | } |
51c39bb1 | 13574 | } |
f1bca824 | 13575 | |
51c39bb1 AS |
13576 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
13577 | { | |
6f8a57cc | 13578 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
13579 | struct bpf_verifier_state *state; |
13580 | struct bpf_reg_state *regs; | |
13581 | int ret, i; | |
13582 | ||
13583 | env->prev_linfo = NULL; | |
13584 | env->pass_cnt++; | |
13585 | ||
13586 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
13587 | if (!state) | |
13588 | return -ENOMEM; | |
13589 | state->curframe = 0; | |
13590 | state->speculative = false; | |
13591 | state->branches = 1; | |
13592 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
13593 | if (!state->frame[0]) { | |
13594 | kfree(state); | |
13595 | return -ENOMEM; | |
13596 | } | |
13597 | env->cur_state = state; | |
13598 | init_func_state(env, state->frame[0], | |
13599 | BPF_MAIN_FUNC /* callsite */, | |
13600 | 0 /* frameno */, | |
13601 | subprog); | |
13602 | ||
13603 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 13604 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
13605 | ret = btf_prepare_func_args(env, subprog, regs); |
13606 | if (ret) | |
13607 | goto out; | |
13608 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
13609 | if (regs[i].type == PTR_TO_CTX) | |
13610 | mark_reg_known_zero(env, regs, i); | |
13611 | else if (regs[i].type == SCALAR_VALUE) | |
13612 | mark_reg_unknown(env, regs, i); | |
e5069b9c DB |
13613 | else if (regs[i].type == PTR_TO_MEM_OR_NULL) { |
13614 | const u32 mem_size = regs[i].mem_size; | |
13615 | ||
13616 | mark_reg_known_zero(env, regs, i); | |
13617 | regs[i].mem_size = mem_size; | |
13618 | regs[i].id = ++env->id_gen; | |
13619 | } | |
51c39bb1 AS |
13620 | } |
13621 | } else { | |
13622 | /* 1st arg to a function */ | |
13623 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
13624 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 13625 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
13626 | if (ret == -EFAULT) |
13627 | /* unlikely verifier bug. abort. | |
13628 | * ret == 0 and ret < 0 are sadly acceptable for | |
13629 | * main() function due to backward compatibility. | |
13630 | * Like socket filter program may be written as: | |
13631 | * int bpf_prog(struct pt_regs *ctx) | |
13632 | * and never dereference that ctx in the program. | |
13633 | * 'struct pt_regs' is a type mismatch for socket | |
13634 | * filter that should be using 'struct __sk_buff'. | |
13635 | */ | |
13636 | goto out; | |
13637 | } | |
13638 | ||
13639 | ret = do_check(env); | |
13640 | out: | |
f59bbfc2 AS |
13641 | /* check for NULL is necessary, since cur_state can be freed inside |
13642 | * do_check() under memory pressure. | |
13643 | */ | |
13644 | if (env->cur_state) { | |
13645 | free_verifier_state(env->cur_state, true); | |
13646 | env->cur_state = NULL; | |
13647 | } | |
6f8a57cc AN |
13648 | while (!pop_stack(env, NULL, NULL, false)); |
13649 | if (!ret && pop_log) | |
13650 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 13651 | free_states(env); |
51c39bb1 AS |
13652 | return ret; |
13653 | } | |
13654 | ||
13655 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
13656 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
13657 | * Consider: | |
13658 | * int bar(int); | |
13659 | * int foo(int f) | |
13660 | * { | |
13661 | * return bar(f); | |
13662 | * } | |
13663 | * int bar(int b) | |
13664 | * { | |
13665 | * ... | |
13666 | * } | |
13667 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
13668 | * will be assumed that bar() already verified successfully and call to bar() | |
13669 | * from foo() will be checked for type match only. Later bar() will be verified | |
13670 | * independently to check that it's safe for R1=any_scalar_value. | |
13671 | */ | |
13672 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
13673 | { | |
13674 | struct bpf_prog_aux *aux = env->prog->aux; | |
13675 | int i, ret; | |
13676 | ||
13677 | if (!aux->func_info) | |
13678 | return 0; | |
13679 | ||
13680 | for (i = 1; i < env->subprog_cnt; i++) { | |
13681 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
13682 | continue; | |
13683 | env->insn_idx = env->subprog_info[i].start; | |
13684 | WARN_ON_ONCE(env->insn_idx == 0); | |
13685 | ret = do_check_common(env, i); | |
13686 | if (ret) { | |
13687 | return ret; | |
13688 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
13689 | verbose(env, | |
13690 | "Func#%d is safe for any args that match its prototype\n", | |
13691 | i); | |
13692 | } | |
13693 | } | |
13694 | return 0; | |
13695 | } | |
13696 | ||
13697 | static int do_check_main(struct bpf_verifier_env *env) | |
13698 | { | |
13699 | int ret; | |
13700 | ||
13701 | env->insn_idx = 0; | |
13702 | ret = do_check_common(env, 0); | |
13703 | if (!ret) | |
13704 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
13705 | return ret; | |
13706 | } | |
13707 | ||
13708 | ||
06ee7115 AS |
13709 | static void print_verification_stats(struct bpf_verifier_env *env) |
13710 | { | |
13711 | int i; | |
13712 | ||
13713 | if (env->log.level & BPF_LOG_STATS) { | |
13714 | verbose(env, "verification time %lld usec\n", | |
13715 | div_u64(env->verification_time, 1000)); | |
13716 | verbose(env, "stack depth "); | |
13717 | for (i = 0; i < env->subprog_cnt; i++) { | |
13718 | u32 depth = env->subprog_info[i].stack_depth; | |
13719 | ||
13720 | verbose(env, "%d", depth); | |
13721 | if (i + 1 < env->subprog_cnt) | |
13722 | verbose(env, "+"); | |
13723 | } | |
13724 | verbose(env, "\n"); | |
13725 | } | |
13726 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
13727 | "total_states %d peak_states %d mark_read %d\n", | |
13728 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
13729 | env->max_states_per_insn, env->total_states, | |
13730 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
13731 | } |
13732 | ||
27ae7997 MKL |
13733 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
13734 | { | |
13735 | const struct btf_type *t, *func_proto; | |
13736 | const struct bpf_struct_ops *st_ops; | |
13737 | const struct btf_member *member; | |
13738 | struct bpf_prog *prog = env->prog; | |
13739 | u32 btf_id, member_idx; | |
13740 | const char *mname; | |
13741 | ||
12aa8a94 THJ |
13742 | if (!prog->gpl_compatible) { |
13743 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
13744 | return -EINVAL; | |
13745 | } | |
13746 | ||
27ae7997 MKL |
13747 | btf_id = prog->aux->attach_btf_id; |
13748 | st_ops = bpf_struct_ops_find(btf_id); | |
13749 | if (!st_ops) { | |
13750 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
13751 | btf_id); | |
13752 | return -ENOTSUPP; | |
13753 | } | |
13754 | ||
13755 | t = st_ops->type; | |
13756 | member_idx = prog->expected_attach_type; | |
13757 | if (member_idx >= btf_type_vlen(t)) { | |
13758 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
13759 | member_idx, st_ops->name); | |
13760 | return -EINVAL; | |
13761 | } | |
13762 | ||
13763 | member = &btf_type_member(t)[member_idx]; | |
13764 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
13765 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
13766 | NULL); | |
13767 | if (!func_proto) { | |
13768 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
13769 | mname, member_idx, st_ops->name); | |
13770 | return -EINVAL; | |
13771 | } | |
13772 | ||
13773 | if (st_ops->check_member) { | |
13774 | int err = st_ops->check_member(t, member); | |
13775 | ||
13776 | if (err) { | |
13777 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
13778 | mname, st_ops->name); | |
13779 | return err; | |
13780 | } | |
13781 | } | |
13782 | ||
13783 | prog->aux->attach_func_proto = func_proto; | |
13784 | prog->aux->attach_func_name = mname; | |
13785 | env->ops = st_ops->verifier_ops; | |
13786 | ||
13787 | return 0; | |
13788 | } | |
6ba43b76 KS |
13789 | #define SECURITY_PREFIX "security_" |
13790 | ||
f7b12b6f | 13791 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 13792 | { |
69191754 | 13793 | if (within_error_injection_list(addr) || |
f7b12b6f | 13794 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 13795 | return 0; |
6ba43b76 | 13796 | |
6ba43b76 KS |
13797 | return -EINVAL; |
13798 | } | |
27ae7997 | 13799 | |
1e6c62a8 AS |
13800 | /* list of non-sleepable functions that are otherwise on |
13801 | * ALLOW_ERROR_INJECTION list | |
13802 | */ | |
13803 | BTF_SET_START(btf_non_sleepable_error_inject) | |
13804 | /* Three functions below can be called from sleepable and non-sleepable context. | |
13805 | * Assume non-sleepable from bpf safety point of view. | |
13806 | */ | |
9dd3d069 | 13807 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
13808 | BTF_ID(func, should_fail_alloc_page) |
13809 | BTF_ID(func, should_failslab) | |
13810 | BTF_SET_END(btf_non_sleepable_error_inject) | |
13811 | ||
13812 | static int check_non_sleepable_error_inject(u32 btf_id) | |
13813 | { | |
13814 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
13815 | } | |
13816 | ||
f7b12b6f THJ |
13817 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
13818 | const struct bpf_prog *prog, | |
13819 | const struct bpf_prog *tgt_prog, | |
13820 | u32 btf_id, | |
13821 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 13822 | { |
be8704ff | 13823 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 13824 | const char prefix[] = "btf_trace_"; |
5b92a28a | 13825 | int ret = 0, subprog = -1, i; |
38207291 | 13826 | const struct btf_type *t; |
5b92a28a | 13827 | bool conservative = true; |
38207291 | 13828 | const char *tname; |
5b92a28a | 13829 | struct btf *btf; |
f7b12b6f | 13830 | long addr = 0; |
38207291 | 13831 | |
f1b9509c | 13832 | if (!btf_id) { |
efc68158 | 13833 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
13834 | return -EINVAL; |
13835 | } | |
22dc4a0f | 13836 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 13837 | if (!btf) { |
efc68158 | 13838 | bpf_log(log, |
5b92a28a AS |
13839 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
13840 | return -EINVAL; | |
13841 | } | |
13842 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 13843 | if (!t) { |
efc68158 | 13844 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
13845 | return -EINVAL; |
13846 | } | |
5b92a28a | 13847 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 13848 | if (!tname) { |
efc68158 | 13849 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
13850 | return -EINVAL; |
13851 | } | |
5b92a28a AS |
13852 | if (tgt_prog) { |
13853 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
13854 | ||
13855 | for (i = 0; i < aux->func_info_cnt; i++) | |
13856 | if (aux->func_info[i].type_id == btf_id) { | |
13857 | subprog = i; | |
13858 | break; | |
13859 | } | |
13860 | if (subprog == -1) { | |
efc68158 | 13861 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
13862 | return -EINVAL; |
13863 | } | |
13864 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
13865 | if (prog_extension) { |
13866 | if (conservative) { | |
efc68158 | 13867 | bpf_log(log, |
be8704ff AS |
13868 | "Cannot replace static functions\n"); |
13869 | return -EINVAL; | |
13870 | } | |
13871 | if (!prog->jit_requested) { | |
efc68158 | 13872 | bpf_log(log, |
be8704ff AS |
13873 | "Extension programs should be JITed\n"); |
13874 | return -EINVAL; | |
13875 | } | |
be8704ff AS |
13876 | } |
13877 | if (!tgt_prog->jited) { | |
efc68158 | 13878 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
13879 | return -EINVAL; |
13880 | } | |
13881 | if (tgt_prog->type == prog->type) { | |
13882 | /* Cannot fentry/fexit another fentry/fexit program. | |
13883 | * Cannot attach program extension to another extension. | |
13884 | * It's ok to attach fentry/fexit to extension program. | |
13885 | */ | |
efc68158 | 13886 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
13887 | return -EINVAL; |
13888 | } | |
13889 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
13890 | prog_extension && | |
13891 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
13892 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
13893 | /* Program extensions can extend all program types | |
13894 | * except fentry/fexit. The reason is the following. | |
13895 | * The fentry/fexit programs are used for performance | |
13896 | * analysis, stats and can be attached to any program | |
13897 | * type except themselves. When extension program is | |
13898 | * replacing XDP function it is necessary to allow | |
13899 | * performance analysis of all functions. Both original | |
13900 | * XDP program and its program extension. Hence | |
13901 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
13902 | * allowed. If extending of fentry/fexit was allowed it | |
13903 | * would be possible to create long call chain | |
13904 | * fentry->extension->fentry->extension beyond | |
13905 | * reasonable stack size. Hence extending fentry is not | |
13906 | * allowed. | |
13907 | */ | |
efc68158 | 13908 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
13909 | return -EINVAL; |
13910 | } | |
5b92a28a | 13911 | } else { |
be8704ff | 13912 | if (prog_extension) { |
efc68158 | 13913 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
13914 | return -EINVAL; |
13915 | } | |
5b92a28a | 13916 | } |
f1b9509c AS |
13917 | |
13918 | switch (prog->expected_attach_type) { | |
13919 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 13920 | if (tgt_prog) { |
efc68158 | 13921 | bpf_log(log, |
5b92a28a AS |
13922 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
13923 | return -EINVAL; | |
13924 | } | |
38207291 | 13925 | if (!btf_type_is_typedef(t)) { |
efc68158 | 13926 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
13927 | btf_id); |
13928 | return -EINVAL; | |
13929 | } | |
f1b9509c | 13930 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 13931 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
13932 | btf_id, tname); |
13933 | return -EINVAL; | |
13934 | } | |
13935 | tname += sizeof(prefix) - 1; | |
5b92a28a | 13936 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
13937 | if (!btf_type_is_ptr(t)) |
13938 | /* should never happen in valid vmlinux build */ | |
13939 | return -EINVAL; | |
5b92a28a | 13940 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
13941 | if (!btf_type_is_func_proto(t)) |
13942 | /* should never happen in valid vmlinux build */ | |
13943 | return -EINVAL; | |
13944 | ||
f7b12b6f | 13945 | break; |
15d83c4d YS |
13946 | case BPF_TRACE_ITER: |
13947 | if (!btf_type_is_func(t)) { | |
efc68158 | 13948 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
13949 | btf_id); |
13950 | return -EINVAL; | |
13951 | } | |
13952 | t = btf_type_by_id(btf, t->type); | |
13953 | if (!btf_type_is_func_proto(t)) | |
13954 | return -EINVAL; | |
f7b12b6f THJ |
13955 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
13956 | if (ret) | |
13957 | return ret; | |
13958 | break; | |
be8704ff AS |
13959 | default: |
13960 | if (!prog_extension) | |
13961 | return -EINVAL; | |
df561f66 | 13962 | fallthrough; |
ae240823 | 13963 | case BPF_MODIFY_RETURN: |
9e4e01df | 13964 | case BPF_LSM_MAC: |
fec56f58 AS |
13965 | case BPF_TRACE_FENTRY: |
13966 | case BPF_TRACE_FEXIT: | |
13967 | if (!btf_type_is_func(t)) { | |
efc68158 | 13968 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
13969 | btf_id); |
13970 | return -EINVAL; | |
13971 | } | |
be8704ff | 13972 | if (prog_extension && |
efc68158 | 13973 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 13974 | return -EINVAL; |
5b92a28a | 13975 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
13976 | if (!btf_type_is_func_proto(t)) |
13977 | return -EINVAL; | |
f7b12b6f | 13978 | |
4a1e7c0c THJ |
13979 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
13980 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
13981 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
13982 | return -EINVAL; | |
13983 | ||
f7b12b6f | 13984 | if (tgt_prog && conservative) |
5b92a28a | 13985 | t = NULL; |
f7b12b6f THJ |
13986 | |
13987 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 13988 | if (ret < 0) |
f7b12b6f THJ |
13989 | return ret; |
13990 | ||
5b92a28a | 13991 | if (tgt_prog) { |
e9eeec58 YS |
13992 | if (subprog == 0) |
13993 | addr = (long) tgt_prog->bpf_func; | |
13994 | else | |
13995 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
13996 | } else { |
13997 | addr = kallsyms_lookup_name(tname); | |
13998 | if (!addr) { | |
efc68158 | 13999 | bpf_log(log, |
5b92a28a AS |
14000 | "The address of function %s cannot be found\n", |
14001 | tname); | |
f7b12b6f | 14002 | return -ENOENT; |
5b92a28a | 14003 | } |
fec56f58 | 14004 | } |
18644cec | 14005 | |
1e6c62a8 AS |
14006 | if (prog->aux->sleepable) { |
14007 | ret = -EINVAL; | |
14008 | switch (prog->type) { | |
14009 | case BPF_PROG_TYPE_TRACING: | |
14010 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
14011 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
14012 | */ | |
14013 | if (!check_non_sleepable_error_inject(btf_id) && | |
14014 | within_error_injection_list(addr)) | |
14015 | ret = 0; | |
14016 | break; | |
14017 | case BPF_PROG_TYPE_LSM: | |
14018 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
14019 | * Only some of them are sleepable. | |
14020 | */ | |
423f1610 | 14021 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
14022 | ret = 0; |
14023 | break; | |
14024 | default: | |
14025 | break; | |
14026 | } | |
f7b12b6f THJ |
14027 | if (ret) { |
14028 | bpf_log(log, "%s is not sleepable\n", tname); | |
14029 | return ret; | |
14030 | } | |
1e6c62a8 | 14031 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 14032 | if (tgt_prog) { |
efc68158 | 14033 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
14034 | return -EINVAL; |
14035 | } | |
14036 | ret = check_attach_modify_return(addr, tname); | |
14037 | if (ret) { | |
14038 | bpf_log(log, "%s() is not modifiable\n", tname); | |
14039 | return ret; | |
1af9270e | 14040 | } |
18644cec | 14041 | } |
f7b12b6f THJ |
14042 | |
14043 | break; | |
14044 | } | |
14045 | tgt_info->tgt_addr = addr; | |
14046 | tgt_info->tgt_name = tname; | |
14047 | tgt_info->tgt_type = t; | |
14048 | return 0; | |
14049 | } | |
14050 | ||
35e3815f JO |
14051 | BTF_SET_START(btf_id_deny) |
14052 | BTF_ID_UNUSED | |
14053 | #ifdef CONFIG_SMP | |
14054 | BTF_ID(func, migrate_disable) | |
14055 | BTF_ID(func, migrate_enable) | |
14056 | #endif | |
14057 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
14058 | BTF_ID(func, rcu_read_unlock_strict) | |
14059 | #endif | |
14060 | BTF_SET_END(btf_id_deny) | |
14061 | ||
f7b12b6f THJ |
14062 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
14063 | { | |
14064 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 14065 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
14066 | struct bpf_attach_target_info tgt_info = {}; |
14067 | u32 btf_id = prog->aux->attach_btf_id; | |
14068 | struct bpf_trampoline *tr; | |
14069 | int ret; | |
14070 | u64 key; | |
14071 | ||
79a7f8bd AS |
14072 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
14073 | if (prog->aux->sleepable) | |
14074 | /* attach_btf_id checked to be zero already */ | |
14075 | return 0; | |
14076 | verbose(env, "Syscall programs can only be sleepable\n"); | |
14077 | return -EINVAL; | |
14078 | } | |
14079 | ||
f7b12b6f THJ |
14080 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
14081 | prog->type != BPF_PROG_TYPE_LSM) { | |
14082 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); | |
14083 | return -EINVAL; | |
14084 | } | |
14085 | ||
14086 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
14087 | return check_struct_ops_btf_id(env); | |
14088 | ||
14089 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
14090 | prog->type != BPF_PROG_TYPE_LSM && | |
14091 | prog->type != BPF_PROG_TYPE_EXT) | |
14092 | return 0; | |
14093 | ||
14094 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
14095 | if (ret) | |
fec56f58 | 14096 | return ret; |
f7b12b6f THJ |
14097 | |
14098 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
14099 | /* to make freplace equivalent to their targets, they need to |
14100 | * inherit env->ops and expected_attach_type for the rest of the | |
14101 | * verification | |
14102 | */ | |
f7b12b6f THJ |
14103 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
14104 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
14105 | } | |
14106 | ||
14107 | /* store info about the attachment target that will be used later */ | |
14108 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
14109 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
14110 | ||
4a1e7c0c THJ |
14111 | if (tgt_prog) { |
14112 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
14113 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
14114 | } | |
14115 | ||
f7b12b6f THJ |
14116 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
14117 | prog->aux->attach_btf_trace = true; | |
14118 | return 0; | |
14119 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
14120 | if (!bpf_iter_prog_supported(prog)) | |
14121 | return -EINVAL; | |
14122 | return 0; | |
14123 | } | |
14124 | ||
14125 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
14126 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
14127 | if (ret < 0) | |
14128 | return ret; | |
35e3815f JO |
14129 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
14130 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
14131 | return -EINVAL; | |
38207291 | 14132 | } |
f7b12b6f | 14133 | |
22dc4a0f | 14134 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
14135 | tr = bpf_trampoline_get(key, &tgt_info); |
14136 | if (!tr) | |
14137 | return -ENOMEM; | |
14138 | ||
3aac1ead | 14139 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 14140 | return 0; |
38207291 MKL |
14141 | } |
14142 | ||
76654e67 AM |
14143 | struct btf *bpf_get_btf_vmlinux(void) |
14144 | { | |
14145 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
14146 | mutex_lock(&bpf_verifier_lock); | |
14147 | if (!btf_vmlinux) | |
14148 | btf_vmlinux = btf_parse_vmlinux(); | |
14149 | mutex_unlock(&bpf_verifier_lock); | |
14150 | } | |
14151 | return btf_vmlinux; | |
14152 | } | |
14153 | ||
af2ac3e1 | 14154 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 14155 | { |
06ee7115 | 14156 | u64 start_time = ktime_get_ns(); |
58e2af8b | 14157 | struct bpf_verifier_env *env; |
b9193c1b | 14158 | struct bpf_verifier_log *log; |
9e4c24e7 | 14159 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 14160 | bool is_priv; |
51580e79 | 14161 | |
eba0c929 AB |
14162 | /* no program is valid */ |
14163 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
14164 | return -EINVAL; | |
14165 | ||
58e2af8b | 14166 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
14167 | * allocate/free it every time bpf_check() is called |
14168 | */ | |
58e2af8b | 14169 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
14170 | if (!env) |
14171 | return -ENOMEM; | |
61bd5218 | 14172 | log = &env->log; |
cbd35700 | 14173 | |
9e4c24e7 | 14174 | len = (*prog)->len; |
fad953ce | 14175 | env->insn_aux_data = |
9e4c24e7 | 14176 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
14177 | ret = -ENOMEM; |
14178 | if (!env->insn_aux_data) | |
14179 | goto err_free_env; | |
9e4c24e7 JK |
14180 | for (i = 0; i < len; i++) |
14181 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 14182 | env->prog = *prog; |
00176a34 | 14183 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 14184 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 14185 | is_priv = bpf_capable(); |
0246e64d | 14186 | |
76654e67 | 14187 | bpf_get_btf_vmlinux(); |
8580ac94 | 14188 | |
cbd35700 | 14189 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
14190 | if (!is_priv) |
14191 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
14192 | |
14193 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
14194 | /* user requested verbose verifier output | |
14195 | * and supplied buffer to store the verification trace | |
14196 | */ | |
e7bf8249 JK |
14197 | log->level = attr->log_level; |
14198 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
14199 | log->len_total = attr->log_size; | |
cbd35700 | 14200 | |
e7bf8249 | 14201 | /* log attributes have to be sane */ |
866de407 HT |
14202 | if (!bpf_verifier_log_attr_valid(log)) { |
14203 | ret = -EINVAL; | |
3df126f3 | 14204 | goto err_unlock; |
866de407 | 14205 | } |
cbd35700 | 14206 | } |
1ad2f583 | 14207 | |
0f55f9ed CL |
14208 | mark_verifier_state_clean(env); |
14209 | ||
8580ac94 AS |
14210 | if (IS_ERR(btf_vmlinux)) { |
14211 | /* Either gcc or pahole or kernel are broken. */ | |
14212 | verbose(env, "in-kernel BTF is malformed\n"); | |
14213 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 14214 | goto skip_full_check; |
8580ac94 AS |
14215 | } |
14216 | ||
1ad2f583 DB |
14217 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
14218 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 14219 | env->strict_alignment = true; |
e9ee9efc DM |
14220 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
14221 | env->strict_alignment = false; | |
cbd35700 | 14222 | |
2c78ee89 | 14223 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 14224 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
41c48f3a | 14225 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
14226 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
14227 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
14228 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 14229 | |
10d274e8 AS |
14230 | if (is_priv) |
14231 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
14232 | ||
dc2a4ebc | 14233 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 14234 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
14235 | GFP_USER); |
14236 | ret = -ENOMEM; | |
14237 | if (!env->explored_states) | |
14238 | goto skip_full_check; | |
14239 | ||
e6ac2450 MKL |
14240 | ret = add_subprog_and_kfunc(env); |
14241 | if (ret < 0) | |
14242 | goto skip_full_check; | |
14243 | ||
d9762e84 | 14244 | ret = check_subprogs(env); |
475fb78f AS |
14245 | if (ret < 0) |
14246 | goto skip_full_check; | |
14247 | ||
c454a46b | 14248 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
14249 | if (ret < 0) |
14250 | goto skip_full_check; | |
14251 | ||
be8704ff AS |
14252 | ret = check_attach_btf_id(env); |
14253 | if (ret) | |
14254 | goto skip_full_check; | |
14255 | ||
4976b718 HL |
14256 | ret = resolve_pseudo_ldimm64(env); |
14257 | if (ret < 0) | |
14258 | goto skip_full_check; | |
14259 | ||
ceb11679 YZ |
14260 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
14261 | ret = bpf_prog_offload_verifier_prep(env->prog); | |
14262 | if (ret) | |
14263 | goto skip_full_check; | |
14264 | } | |
14265 | ||
d9762e84 MKL |
14266 | ret = check_cfg(env); |
14267 | if (ret < 0) | |
14268 | goto skip_full_check; | |
14269 | ||
51c39bb1 AS |
14270 | ret = do_check_subprogs(env); |
14271 | ret = ret ?: do_check_main(env); | |
cbd35700 | 14272 | |
c941ce9c QM |
14273 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
14274 | ret = bpf_prog_offload_finalize(env); | |
14275 | ||
0246e64d | 14276 | skip_full_check: |
51c39bb1 | 14277 | kvfree(env->explored_states); |
0246e64d | 14278 | |
c131187d | 14279 | if (ret == 0) |
9b38c405 | 14280 | ret = check_max_stack_depth(env); |
c131187d | 14281 | |
9b38c405 | 14282 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
14283 | if (is_priv) { |
14284 | if (ret == 0) | |
14285 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
14286 | if (ret == 0) |
14287 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
14288 | if (ret == 0) |
14289 | ret = opt_remove_nops(env); | |
52875a04 JK |
14290 | } else { |
14291 | if (ret == 0) | |
14292 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
14293 | } |
14294 | ||
9bac3d6d AS |
14295 | if (ret == 0) |
14296 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
14297 | ret = convert_ctx_accesses(env); | |
14298 | ||
e245c5c6 | 14299 | if (ret == 0) |
e6ac5933 | 14300 | ret = do_misc_fixups(env); |
e245c5c6 | 14301 | |
a4b1d3c1 JW |
14302 | /* do 32-bit optimization after insn patching has done so those patched |
14303 | * insns could be handled correctly. | |
14304 | */ | |
d6c2308c JW |
14305 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
14306 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
14307 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
14308 | : false; | |
a4b1d3c1 JW |
14309 | } |
14310 | ||
1ea47e01 AS |
14311 | if (ret == 0) |
14312 | ret = fixup_call_args(env); | |
14313 | ||
06ee7115 AS |
14314 | env->verification_time = ktime_get_ns() - start_time; |
14315 | print_verification_stats(env); | |
aba64c7d | 14316 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 14317 | |
a2a7d570 | 14318 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 14319 | ret = -ENOSPC; |
a2a7d570 | 14320 | if (log->level && !log->ubuf) { |
cbd35700 | 14321 | ret = -EFAULT; |
a2a7d570 | 14322 | goto err_release_maps; |
cbd35700 AS |
14323 | } |
14324 | ||
541c3bad AN |
14325 | if (ret) |
14326 | goto err_release_maps; | |
14327 | ||
14328 | if (env->used_map_cnt) { | |
0246e64d | 14329 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
14330 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
14331 | sizeof(env->used_maps[0]), | |
14332 | GFP_KERNEL); | |
0246e64d | 14333 | |
9bac3d6d | 14334 | if (!env->prog->aux->used_maps) { |
0246e64d | 14335 | ret = -ENOMEM; |
a2a7d570 | 14336 | goto err_release_maps; |
0246e64d AS |
14337 | } |
14338 | ||
9bac3d6d | 14339 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 14340 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 14341 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
14342 | } |
14343 | if (env->used_btf_cnt) { | |
14344 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
14345 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
14346 | sizeof(env->used_btfs[0]), | |
14347 | GFP_KERNEL); | |
14348 | if (!env->prog->aux->used_btfs) { | |
14349 | ret = -ENOMEM; | |
14350 | goto err_release_maps; | |
14351 | } | |
0246e64d | 14352 | |
541c3bad AN |
14353 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
14354 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
14355 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
14356 | } | |
14357 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
14358 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
14359 | * bpf_ld_imm64 instructions | |
14360 | */ | |
14361 | convert_pseudo_ld_imm64(env); | |
14362 | } | |
cbd35700 | 14363 | |
541c3bad | 14364 | adjust_btf_func(env); |
ba64e7d8 | 14365 | |
a2a7d570 | 14366 | err_release_maps: |
9bac3d6d | 14367 | if (!env->prog->aux->used_maps) |
0246e64d | 14368 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 14369 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
14370 | */ |
14371 | release_maps(env); | |
541c3bad AN |
14372 | if (!env->prog->aux->used_btfs) |
14373 | release_btfs(env); | |
03f87c0b THJ |
14374 | |
14375 | /* extension progs temporarily inherit the attach_type of their targets | |
14376 | for verification purposes, so set it back to zero before returning | |
14377 | */ | |
14378 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
14379 | env->prog->expected_attach_type = 0; | |
14380 | ||
9bac3d6d | 14381 | *prog = env->prog; |
3df126f3 | 14382 | err_unlock: |
45a73c17 AS |
14383 | if (!is_priv) |
14384 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
14385 | vfree(env->insn_aux_data); |
14386 | err_free_env: | |
14387 | kfree(env); | |
51580e79 AS |
14388 | return ret; |
14389 | } |