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 | ||
d83525ca AS |
445 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
446 | { | |
447 | return reg->type == PTR_TO_MAP_VALUE && | |
448 | map_value_has_spin_lock(reg->map_ptr); | |
449 | } | |
450 | ||
cba368c1 MKL |
451 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
452 | { | |
c25b2ae1 HL |
453 | return base_type(type) == PTR_TO_SOCKET || |
454 | base_type(type) == PTR_TO_TCP_SOCK || | |
5c073f26 KKD |
455 | base_type(type) == PTR_TO_MEM || |
456 | base_type(type) == PTR_TO_BTF_ID; | |
cba368c1 MKL |
457 | } |
458 | ||
20b2aff4 HL |
459 | static bool type_is_rdonly_mem(u32 type) |
460 | { | |
461 | return type & MEM_RDONLY; | |
cba368c1 MKL |
462 | } |
463 | ||
1b986589 | 464 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 465 | { |
1b986589 | 466 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
467 | } |
468 | ||
48946bd6 | 469 | static bool type_may_be_null(u32 type) |
fd1b0d60 | 470 | { |
48946bd6 | 471 | return type & PTR_MAYBE_NULL; |
fd1b0d60 LB |
472 | } |
473 | ||
fd978bf7 JS |
474 | /* Determine whether the function releases some resources allocated by another |
475 | * function call. The first reference type argument will be assumed to be | |
476 | * released by release_reference(). | |
477 | */ | |
478 | static bool is_release_function(enum bpf_func_id func_id) | |
479 | { | |
457f4436 AN |
480 | return func_id == BPF_FUNC_sk_release || |
481 | func_id == BPF_FUNC_ringbuf_submit || | |
482 | func_id == BPF_FUNC_ringbuf_discard; | |
840b9615 JS |
483 | } |
484 | ||
64d85290 | 485 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
46f8bc92 MKL |
486 | { |
487 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 | 488 | func_id == BPF_FUNC_sk_lookup_udp || |
64d85290 | 489 | func_id == BPF_FUNC_skc_lookup_tcp || |
457f4436 AN |
490 | func_id == BPF_FUNC_map_lookup_elem || |
491 | func_id == BPF_FUNC_ringbuf_reserve; | |
64d85290 JS |
492 | } |
493 | ||
494 | static bool is_acquire_function(enum bpf_func_id func_id, | |
495 | const struct bpf_map *map) | |
496 | { | |
497 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
498 | ||
499 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
500 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 AN |
501 | func_id == BPF_FUNC_skc_lookup_tcp || |
502 | func_id == BPF_FUNC_ringbuf_reserve) | |
64d85290 JS |
503 | return true; |
504 | ||
505 | if (func_id == BPF_FUNC_map_lookup_elem && | |
506 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
507 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
508 | return true; | |
509 | ||
510 | return false; | |
46f8bc92 MKL |
511 | } |
512 | ||
1b986589 MKL |
513 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
514 | { | |
515 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
516 | func_id == BPF_FUNC_sk_fullsock || |
517 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
518 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
519 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
520 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || | |
521 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
522 | } |
523 | ||
39491867 BJ |
524 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
525 | { | |
526 | return BPF_CLASS(insn->code) == BPF_STX && | |
527 | BPF_MODE(insn->code) == BPF_ATOMIC && | |
528 | insn->imm == BPF_CMPXCHG; | |
529 | } | |
530 | ||
c25b2ae1 HL |
531 | /* string representation of 'enum bpf_reg_type' |
532 | * | |
533 | * Note that reg_type_str() can not appear more than once in a single verbose() | |
534 | * statement. | |
535 | */ | |
536 | static const char *reg_type_str(struct bpf_verifier_env *env, | |
537 | enum bpf_reg_type type) | |
538 | { | |
c6f1bfe8 | 539 | char postfix[16] = {0}, prefix[32] = {0}; |
c25b2ae1 HL |
540 | static const char * const str[] = { |
541 | [NOT_INIT] = "?", | |
7df5072c | 542 | [SCALAR_VALUE] = "scalar", |
c25b2ae1 HL |
543 | [PTR_TO_CTX] = "ctx", |
544 | [CONST_PTR_TO_MAP] = "map_ptr", | |
545 | [PTR_TO_MAP_VALUE] = "map_value", | |
546 | [PTR_TO_STACK] = "fp", | |
547 | [PTR_TO_PACKET] = "pkt", | |
548 | [PTR_TO_PACKET_META] = "pkt_meta", | |
549 | [PTR_TO_PACKET_END] = "pkt_end", | |
550 | [PTR_TO_FLOW_KEYS] = "flow_keys", | |
551 | [PTR_TO_SOCKET] = "sock", | |
552 | [PTR_TO_SOCK_COMMON] = "sock_common", | |
553 | [PTR_TO_TCP_SOCK] = "tcp_sock", | |
554 | [PTR_TO_TP_BUFFER] = "tp_buffer", | |
555 | [PTR_TO_XDP_SOCK] = "xdp_sock", | |
556 | [PTR_TO_BTF_ID] = "ptr_", | |
c25b2ae1 | 557 | [PTR_TO_MEM] = "mem", |
20b2aff4 | 558 | [PTR_TO_BUF] = "buf", |
c25b2ae1 HL |
559 | [PTR_TO_FUNC] = "func", |
560 | [PTR_TO_MAP_KEY] = "map_key", | |
561 | }; | |
562 | ||
563 | if (type & PTR_MAYBE_NULL) { | |
5844101a | 564 | if (base_type(type) == PTR_TO_BTF_ID) |
c25b2ae1 HL |
565 | strncpy(postfix, "or_null_", 16); |
566 | else | |
567 | strncpy(postfix, "_or_null", 16); | |
568 | } | |
569 | ||
20b2aff4 | 570 | if (type & MEM_RDONLY) |
c6f1bfe8 | 571 | strncpy(prefix, "rdonly_", 32); |
a672b2e3 | 572 | if (type & MEM_ALLOC) |
c6f1bfe8 YS |
573 | strncpy(prefix, "alloc_", 32); |
574 | if (type & MEM_USER) | |
575 | strncpy(prefix, "user_", 32); | |
5844101a HL |
576 | if (type & MEM_PERCPU) |
577 | strncpy(prefix, "percpu_", 32); | |
20b2aff4 HL |
578 | |
579 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", | |
580 | prefix, str[base_type(type)], postfix); | |
c25b2ae1 HL |
581 | return env->type_str_buf; |
582 | } | |
17a52670 | 583 | |
8efea21d EC |
584 | static char slot_type_char[] = { |
585 | [STACK_INVALID] = '?', | |
586 | [STACK_SPILL] = 'r', | |
587 | [STACK_MISC] = 'm', | |
588 | [STACK_ZERO] = '0', | |
589 | }; | |
590 | ||
4e92024a AS |
591 | static void print_liveness(struct bpf_verifier_env *env, |
592 | enum bpf_reg_liveness live) | |
593 | { | |
9242b5f5 | 594 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
595 | verbose(env, "_"); |
596 | if (live & REG_LIVE_READ) | |
597 | verbose(env, "r"); | |
598 | if (live & REG_LIVE_WRITTEN) | |
599 | verbose(env, "w"); | |
9242b5f5 AS |
600 | if (live & REG_LIVE_DONE) |
601 | verbose(env, "D"); | |
4e92024a AS |
602 | } |
603 | ||
f4d7e40a AS |
604 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
605 | const struct bpf_reg_state *reg) | |
606 | { | |
607 | struct bpf_verifier_state *cur = env->cur_state; | |
608 | ||
609 | return cur->frame[reg->frameno]; | |
610 | } | |
611 | ||
22dc4a0f | 612 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
9e15db66 | 613 | { |
22dc4a0f | 614 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
615 | } |
616 | ||
0f55f9ed CL |
617 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
618 | { | |
619 | env->scratched_regs |= 1U << regno; | |
620 | } | |
621 | ||
622 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
623 | { | |
343e5375 | 624 | env->scratched_stack_slots |= 1ULL << spi; |
0f55f9ed CL |
625 | } |
626 | ||
627 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
628 | { | |
629 | return (env->scratched_regs >> regno) & 1; | |
630 | } | |
631 | ||
632 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
633 | { | |
634 | return (env->scratched_stack_slots >> regno) & 1; | |
635 | } | |
636 | ||
637 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
638 | { | |
639 | return env->scratched_regs || env->scratched_stack_slots; | |
640 | } | |
641 | ||
642 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
643 | { | |
644 | env->scratched_regs = 0U; | |
343e5375 | 645 | env->scratched_stack_slots = 0ULL; |
0f55f9ed CL |
646 | } |
647 | ||
648 | /* Used for printing the entire verifier state. */ | |
649 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
650 | { | |
651 | env->scratched_regs = ~0U; | |
343e5375 | 652 | env->scratched_stack_slots = ~0ULL; |
0f55f9ed CL |
653 | } |
654 | ||
27113c59 MKL |
655 | /* The reg state of a pointer or a bounded scalar was saved when |
656 | * it was spilled to the stack. | |
657 | */ | |
658 | static bool is_spilled_reg(const struct bpf_stack_state *stack) | |
659 | { | |
660 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; | |
661 | } | |
662 | ||
354e8f19 MKL |
663 | static void scrub_spilled_slot(u8 *stype) |
664 | { | |
665 | if (*stype != STACK_INVALID) | |
666 | *stype = STACK_MISC; | |
667 | } | |
668 | ||
61bd5218 | 669 | static void print_verifier_state(struct bpf_verifier_env *env, |
0f55f9ed CL |
670 | const struct bpf_func_state *state, |
671 | bool print_all) | |
17a52670 | 672 | { |
f4d7e40a | 673 | const struct bpf_reg_state *reg; |
17a52670 AS |
674 | enum bpf_reg_type t; |
675 | int i; | |
676 | ||
f4d7e40a AS |
677 | if (state->frameno) |
678 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 679 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
680 | reg = &state->regs[i]; |
681 | t = reg->type; | |
17a52670 AS |
682 | if (t == NOT_INIT) |
683 | continue; | |
0f55f9ed CL |
684 | if (!print_all && !reg_scratched(env, i)) |
685 | continue; | |
4e92024a AS |
686 | verbose(env, " R%d", i); |
687 | print_liveness(env, reg->live); | |
7df5072c | 688 | verbose(env, "="); |
b5dc0163 AS |
689 | if (t == SCALAR_VALUE && reg->precise) |
690 | verbose(env, "P"); | |
f1174f77 EC |
691 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
692 | tnum_is_const(reg->var_off)) { | |
693 | /* reg->off should be 0 for SCALAR_VALUE */ | |
7df5072c | 694 | verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
61bd5218 | 695 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 696 | } else { |
7df5072c ML |
697 | const char *sep = ""; |
698 | ||
699 | verbose(env, "%s", reg_type_str(env, t)); | |
5844101a | 700 | if (base_type(t) == PTR_TO_BTF_ID) |
22dc4a0f | 701 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
7df5072c ML |
702 | verbose(env, "("); |
703 | /* | |
704 | * _a stands for append, was shortened to avoid multiline statements below. | |
705 | * This macro is used to output a comma separated list of attributes. | |
706 | */ | |
707 | #define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) | |
708 | ||
709 | if (reg->id) | |
710 | verbose_a("id=%d", reg->id); | |
711 | if (reg_type_may_be_refcounted_or_null(t) && reg->ref_obj_id) | |
712 | verbose_a("ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 713 | if (t != SCALAR_VALUE) |
7df5072c | 714 | verbose_a("off=%d", reg->off); |
de8f3a83 | 715 | if (type_is_pkt_pointer(t)) |
7df5072c | 716 | verbose_a("r=%d", reg->range); |
c25b2ae1 HL |
717 | else if (base_type(t) == CONST_PTR_TO_MAP || |
718 | base_type(t) == PTR_TO_MAP_KEY || | |
719 | base_type(t) == PTR_TO_MAP_VALUE) | |
7df5072c ML |
720 | verbose_a("ks=%d,vs=%d", |
721 | reg->map_ptr->key_size, | |
722 | reg->map_ptr->value_size); | |
7d1238f2 EC |
723 | if (tnum_is_const(reg->var_off)) { |
724 | /* Typically an immediate SCALAR_VALUE, but | |
725 | * could be a pointer whose offset is too big | |
726 | * for reg->off | |
727 | */ | |
7df5072c | 728 | verbose_a("imm=%llx", reg->var_off.value); |
7d1238f2 EC |
729 | } else { |
730 | if (reg->smin_value != reg->umin_value && | |
731 | reg->smin_value != S64_MIN) | |
7df5072c | 732 | verbose_a("smin=%lld", (long long)reg->smin_value); |
7d1238f2 EC |
733 | if (reg->smax_value != reg->umax_value && |
734 | reg->smax_value != S64_MAX) | |
7df5072c | 735 | verbose_a("smax=%lld", (long long)reg->smax_value); |
7d1238f2 | 736 | if (reg->umin_value != 0) |
7df5072c | 737 | verbose_a("umin=%llu", (unsigned long long)reg->umin_value); |
7d1238f2 | 738 | if (reg->umax_value != U64_MAX) |
7df5072c | 739 | verbose_a("umax=%llu", (unsigned long long)reg->umax_value); |
7d1238f2 EC |
740 | if (!tnum_is_unknown(reg->var_off)) { |
741 | char tn_buf[48]; | |
f1174f77 | 742 | |
7d1238f2 | 743 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
7df5072c | 744 | verbose_a("var_off=%s", tn_buf); |
7d1238f2 | 745 | } |
3f50f132 JF |
746 | if (reg->s32_min_value != reg->smin_value && |
747 | reg->s32_min_value != S32_MIN) | |
7df5072c | 748 | verbose_a("s32_min=%d", (int)(reg->s32_min_value)); |
3f50f132 JF |
749 | if (reg->s32_max_value != reg->smax_value && |
750 | reg->s32_max_value != S32_MAX) | |
7df5072c | 751 | verbose_a("s32_max=%d", (int)(reg->s32_max_value)); |
3f50f132 JF |
752 | if (reg->u32_min_value != reg->umin_value && |
753 | reg->u32_min_value != U32_MIN) | |
7df5072c | 754 | verbose_a("u32_min=%d", (int)(reg->u32_min_value)); |
3f50f132 JF |
755 | if (reg->u32_max_value != reg->umax_value && |
756 | reg->u32_max_value != U32_MAX) | |
7df5072c | 757 | verbose_a("u32_max=%d", (int)(reg->u32_max_value)); |
f1174f77 | 758 | } |
7df5072c ML |
759 | #undef verbose_a |
760 | ||
61bd5218 | 761 | verbose(env, ")"); |
f1174f77 | 762 | } |
17a52670 | 763 | } |
638f5b90 | 764 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
765 | char types_buf[BPF_REG_SIZE + 1]; |
766 | bool valid = false; | |
767 | int j; | |
768 | ||
769 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
770 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
771 | valid = true; | |
772 | types_buf[j] = slot_type_char[ | |
773 | state->stack[i].slot_type[j]]; | |
774 | } | |
775 | types_buf[BPF_REG_SIZE] = 0; | |
776 | if (!valid) | |
777 | continue; | |
0f55f9ed CL |
778 | if (!print_all && !stack_slot_scratched(env, i)) |
779 | continue; | |
8efea21d EC |
780 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
781 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
27113c59 | 782 | if (is_spilled_reg(&state->stack[i])) { |
b5dc0163 AS |
783 | reg = &state->stack[i].spilled_ptr; |
784 | t = reg->type; | |
7df5072c | 785 | verbose(env, "=%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t)); |
b5dc0163 AS |
786 | if (t == SCALAR_VALUE && reg->precise) |
787 | verbose(env, "P"); | |
788 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
789 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
790 | } else { | |
8efea21d | 791 | verbose(env, "=%s", types_buf); |
b5dc0163 | 792 | } |
17a52670 | 793 | } |
fd978bf7 JS |
794 | if (state->acquired_refs && state->refs[0].id) { |
795 | verbose(env, " refs=%d", state->refs[0].id); | |
796 | for (i = 1; i < state->acquired_refs; i++) | |
797 | if (state->refs[i].id) | |
798 | verbose(env, ",%d", state->refs[i].id); | |
799 | } | |
bfc6bb74 AS |
800 | if (state->in_callback_fn) |
801 | verbose(env, " cb"); | |
802 | if (state->in_async_callback_fn) | |
803 | verbose(env, " async_cb"); | |
61bd5218 | 804 | verbose(env, "\n"); |
0f55f9ed | 805 | mark_verifier_state_clean(env); |
17a52670 AS |
806 | } |
807 | ||
2e576648 CL |
808 | static inline u32 vlog_alignment(u32 pos) |
809 | { | |
810 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), | |
811 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; | |
812 | } | |
813 | ||
814 | static void print_insn_state(struct bpf_verifier_env *env, | |
815 | const struct bpf_func_state *state) | |
816 | { | |
817 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { | |
818 | /* remove new line character */ | |
819 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); | |
820 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); | |
821 | } else { | |
822 | verbose(env, "%d:", env->insn_idx); | |
823 | } | |
824 | print_verifier_state(env, state, false); | |
17a52670 AS |
825 | } |
826 | ||
c69431aa LB |
827 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
828 | * small to hold src. This is different from krealloc since we don't want to preserve | |
829 | * the contents of dst. | |
830 | * | |
831 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could | |
832 | * not be allocated. | |
638f5b90 | 833 | */ |
c69431aa | 834 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
638f5b90 | 835 | { |
c69431aa LB |
836 | size_t bytes; |
837 | ||
838 | if (ZERO_OR_NULL_PTR(src)) | |
839 | goto out; | |
840 | ||
841 | if (unlikely(check_mul_overflow(n, size, &bytes))) | |
842 | return NULL; | |
843 | ||
844 | if (ksize(dst) < bytes) { | |
845 | kfree(dst); | |
846 | dst = kmalloc_track_caller(bytes, flags); | |
847 | if (!dst) | |
848 | return NULL; | |
849 | } | |
850 | ||
851 | memcpy(dst, src, bytes); | |
852 | out: | |
853 | return dst ? dst : ZERO_SIZE_PTR; | |
854 | } | |
855 | ||
856 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too | |
857 | * small to hold new_n items. new items are zeroed out if the array grows. | |
858 | * | |
859 | * Contrary to krealloc_array, does not free arr if new_n is zero. | |
860 | */ | |
861 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) | |
862 | { | |
863 | if (!new_n || old_n == new_n) | |
864 | goto out; | |
865 | ||
866 | arr = krealloc_array(arr, new_n, size, GFP_KERNEL); | |
867 | if (!arr) | |
868 | return NULL; | |
869 | ||
870 | if (new_n > old_n) | |
871 | memset(arr + old_n * size, 0, (new_n - old_n) * size); | |
872 | ||
873 | out: | |
874 | return arr ? arr : ZERO_SIZE_PTR; | |
875 | } | |
876 | ||
877 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
878 | { | |
879 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, | |
880 | sizeof(struct bpf_reference_state), GFP_KERNEL); | |
881 | if (!dst->refs) | |
882 | return -ENOMEM; | |
883 | ||
884 | dst->acquired_refs = src->acquired_refs; | |
885 | return 0; | |
886 | } | |
887 | ||
888 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) | |
889 | { | |
890 | size_t n = src->allocated_stack / BPF_REG_SIZE; | |
891 | ||
892 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), | |
893 | GFP_KERNEL); | |
894 | if (!dst->stack) | |
895 | return -ENOMEM; | |
896 | ||
897 | dst->allocated_stack = src->allocated_stack; | |
898 | return 0; | |
899 | } | |
900 | ||
901 | static int resize_reference_state(struct bpf_func_state *state, size_t n) | |
902 | { | |
903 | state->refs = realloc_array(state->refs, state->acquired_refs, n, | |
904 | sizeof(struct bpf_reference_state)); | |
905 | if (!state->refs) | |
906 | return -ENOMEM; | |
907 | ||
908 | state->acquired_refs = n; | |
909 | return 0; | |
910 | } | |
911 | ||
912 | static int grow_stack_state(struct bpf_func_state *state, int size) | |
913 | { | |
914 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; | |
915 | ||
916 | if (old_n >= n) | |
917 | return 0; | |
918 | ||
919 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); | |
920 | if (!state->stack) | |
921 | return -ENOMEM; | |
922 | ||
923 | state->allocated_stack = size; | |
924 | return 0; | |
fd978bf7 JS |
925 | } |
926 | ||
927 | /* Acquire a pointer id from the env and update the state->refs to include | |
928 | * this new pointer reference. | |
929 | * On success, returns a valid pointer id to associate with the register | |
930 | * On failure, returns a negative errno. | |
638f5b90 | 931 | */ |
fd978bf7 | 932 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 933 | { |
fd978bf7 JS |
934 | struct bpf_func_state *state = cur_func(env); |
935 | int new_ofs = state->acquired_refs; | |
936 | int id, err; | |
937 | ||
c69431aa | 938 | err = resize_reference_state(state, state->acquired_refs + 1); |
fd978bf7 JS |
939 | if (err) |
940 | return err; | |
941 | id = ++env->id_gen; | |
942 | state->refs[new_ofs].id = id; | |
943 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 944 | |
fd978bf7 JS |
945 | return id; |
946 | } | |
947 | ||
948 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 949 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
950 | { |
951 | int i, last_idx; | |
952 | ||
fd978bf7 JS |
953 | last_idx = state->acquired_refs - 1; |
954 | for (i = 0; i < state->acquired_refs; i++) { | |
955 | if (state->refs[i].id == ptr_id) { | |
956 | if (last_idx && i != last_idx) | |
957 | memcpy(&state->refs[i], &state->refs[last_idx], | |
958 | sizeof(*state->refs)); | |
959 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
960 | state->acquired_refs--; | |
638f5b90 | 961 | return 0; |
638f5b90 | 962 | } |
638f5b90 | 963 | } |
46f8bc92 | 964 | return -EINVAL; |
fd978bf7 JS |
965 | } |
966 | ||
f4d7e40a AS |
967 | static void free_func_state(struct bpf_func_state *state) |
968 | { | |
5896351e AS |
969 | if (!state) |
970 | return; | |
fd978bf7 | 971 | kfree(state->refs); |
f4d7e40a AS |
972 | kfree(state->stack); |
973 | kfree(state); | |
974 | } | |
975 | ||
b5dc0163 AS |
976 | static void clear_jmp_history(struct bpf_verifier_state *state) |
977 | { | |
978 | kfree(state->jmp_history); | |
979 | state->jmp_history = NULL; | |
980 | state->jmp_history_cnt = 0; | |
981 | } | |
982 | ||
1969db47 AS |
983 | static void free_verifier_state(struct bpf_verifier_state *state, |
984 | bool free_self) | |
638f5b90 | 985 | { |
f4d7e40a AS |
986 | int i; |
987 | ||
988 | for (i = 0; i <= state->curframe; i++) { | |
989 | free_func_state(state->frame[i]); | |
990 | state->frame[i] = NULL; | |
991 | } | |
b5dc0163 | 992 | clear_jmp_history(state); |
1969db47 AS |
993 | if (free_self) |
994 | kfree(state); | |
638f5b90 AS |
995 | } |
996 | ||
997 | /* copy verifier state from src to dst growing dst stack space | |
998 | * when necessary to accommodate larger src stack | |
999 | */ | |
f4d7e40a AS |
1000 | static int copy_func_state(struct bpf_func_state *dst, |
1001 | const struct bpf_func_state *src) | |
638f5b90 AS |
1002 | { |
1003 | int err; | |
1004 | ||
fd978bf7 JS |
1005 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
1006 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
1007 | if (err) |
1008 | return err; | |
638f5b90 AS |
1009 | return copy_stack_state(dst, src); |
1010 | } | |
1011 | ||
f4d7e40a AS |
1012 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
1013 | const struct bpf_verifier_state *src) | |
1014 | { | |
1015 | struct bpf_func_state *dst; | |
1016 | int i, err; | |
1017 | ||
06ab6a50 LB |
1018 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
1019 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), | |
1020 | GFP_USER); | |
1021 | if (!dst_state->jmp_history) | |
1022 | return -ENOMEM; | |
b5dc0163 AS |
1023 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
1024 | ||
f4d7e40a AS |
1025 | /* if dst has more stack frames then src frame, free them */ |
1026 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
1027 | free_func_state(dst_state->frame[i]); | |
1028 | dst_state->frame[i] = NULL; | |
1029 | } | |
979d63d5 | 1030 | dst_state->speculative = src->speculative; |
f4d7e40a | 1031 | dst_state->curframe = src->curframe; |
d83525ca | 1032 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
1033 | dst_state->branches = src->branches; |
1034 | dst_state->parent = src->parent; | |
b5dc0163 AS |
1035 | dst_state->first_insn_idx = src->first_insn_idx; |
1036 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
1037 | for (i = 0; i <= src->curframe; i++) { |
1038 | dst = dst_state->frame[i]; | |
1039 | if (!dst) { | |
1040 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
1041 | if (!dst) | |
1042 | return -ENOMEM; | |
1043 | dst_state->frame[i] = dst; | |
1044 | } | |
1045 | err = copy_func_state(dst, src->frame[i]); | |
1046 | if (err) | |
1047 | return err; | |
1048 | } | |
1049 | return 0; | |
1050 | } | |
1051 | ||
2589726d AS |
1052 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
1053 | { | |
1054 | while (st) { | |
1055 | u32 br = --st->branches; | |
1056 | ||
1057 | /* WARN_ON(br > 1) technically makes sense here, | |
1058 | * but see comment in push_stack(), hence: | |
1059 | */ | |
1060 | WARN_ONCE((int)br < 0, | |
1061 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
1062 | br); | |
1063 | if (br) | |
1064 | break; | |
1065 | st = st->parent; | |
1066 | } | |
1067 | } | |
1068 | ||
638f5b90 | 1069 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 1070 | int *insn_idx, bool pop_log) |
638f5b90 AS |
1071 | { |
1072 | struct bpf_verifier_state *cur = env->cur_state; | |
1073 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
1074 | int err; | |
17a52670 AS |
1075 | |
1076 | if (env->head == NULL) | |
638f5b90 | 1077 | return -ENOENT; |
17a52670 | 1078 | |
638f5b90 AS |
1079 | if (cur) { |
1080 | err = copy_verifier_state(cur, &head->st); | |
1081 | if (err) | |
1082 | return err; | |
1083 | } | |
6f8a57cc AN |
1084 | if (pop_log) |
1085 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
1086 | if (insn_idx) |
1087 | *insn_idx = head->insn_idx; | |
17a52670 | 1088 | if (prev_insn_idx) |
638f5b90 AS |
1089 | *prev_insn_idx = head->prev_insn_idx; |
1090 | elem = head->next; | |
1969db47 | 1091 | free_verifier_state(&head->st, false); |
638f5b90 | 1092 | kfree(head); |
17a52670 AS |
1093 | env->head = elem; |
1094 | env->stack_size--; | |
638f5b90 | 1095 | return 0; |
17a52670 AS |
1096 | } |
1097 | ||
58e2af8b | 1098 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
1099 | int insn_idx, int prev_insn_idx, |
1100 | bool speculative) | |
17a52670 | 1101 | { |
638f5b90 | 1102 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 1103 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 1104 | int err; |
17a52670 | 1105 | |
638f5b90 | 1106 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
1107 | if (!elem) |
1108 | goto err; | |
1109 | ||
17a52670 AS |
1110 | elem->insn_idx = insn_idx; |
1111 | elem->prev_insn_idx = prev_insn_idx; | |
1112 | elem->next = env->head; | |
6f8a57cc | 1113 | elem->log_pos = env->log.len_used; |
17a52670 AS |
1114 | env->head = elem; |
1115 | env->stack_size++; | |
1969db47 AS |
1116 | err = copy_verifier_state(&elem->st, cur); |
1117 | if (err) | |
1118 | goto err; | |
979d63d5 | 1119 | elem->st.speculative |= speculative; |
b285fcb7 AS |
1120 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
1121 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
1122 | env->stack_size); | |
17a52670 AS |
1123 | goto err; |
1124 | } | |
2589726d AS |
1125 | if (elem->st.parent) { |
1126 | ++elem->st.parent->branches; | |
1127 | /* WARN_ON(branches > 2) technically makes sense here, | |
1128 | * but | |
1129 | * 1. speculative states will bump 'branches' for non-branch | |
1130 | * instructions | |
1131 | * 2. is_state_visited() heuristics may decide not to create | |
1132 | * a new state for a sequence of branches and all such current | |
1133 | * and cloned states will be pointing to a single parent state | |
1134 | * which might have large 'branches' count. | |
1135 | */ | |
1136 | } | |
17a52670 AS |
1137 | return &elem->st; |
1138 | err: | |
5896351e AS |
1139 | free_verifier_state(env->cur_state, true); |
1140 | env->cur_state = NULL; | |
17a52670 | 1141 | /* pop all elements and return */ |
6f8a57cc | 1142 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1143 | return NULL; |
1144 | } | |
1145 | ||
1146 | #define CALLER_SAVED_REGS 6 | |
1147 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1148 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1149 | }; | |
1150 | ||
f54c7898 DB |
1151 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1152 | struct bpf_reg_state *reg); | |
f1174f77 | 1153 | |
e688c3db AS |
1154 | /* This helper doesn't clear reg->id */ |
1155 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1156 | { |
b03c9f9f EC |
1157 | reg->var_off = tnum_const(imm); |
1158 | reg->smin_value = (s64)imm; | |
1159 | reg->smax_value = (s64)imm; | |
1160 | reg->umin_value = imm; | |
1161 | reg->umax_value = imm; | |
3f50f132 JF |
1162 | |
1163 | reg->s32_min_value = (s32)imm; | |
1164 | reg->s32_max_value = (s32)imm; | |
1165 | reg->u32_min_value = (u32)imm; | |
1166 | reg->u32_max_value = (u32)imm; | |
1167 | } | |
1168 | ||
e688c3db AS |
1169 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1170 | * known to have the value @imm. | |
1171 | */ | |
1172 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1173 | { | |
1174 | /* Clear id, off, and union(map_ptr, range) */ | |
1175 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1176 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
1177 | ___mark_reg_known(reg, imm); | |
1178 | } | |
1179 | ||
3f50f132 JF |
1180 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1181 | { | |
1182 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1183 | reg->s32_min_value = (s32)imm; | |
1184 | reg->s32_max_value = (s32)imm; | |
1185 | reg->u32_min_value = (u32)imm; | |
1186 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1187 | } |
1188 | ||
f1174f77 EC |
1189 | /* Mark the 'variable offset' part of a register as zero. This should be |
1190 | * used only on registers holding a pointer type. | |
1191 | */ | |
1192 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1193 | { |
b03c9f9f | 1194 | __mark_reg_known(reg, 0); |
f1174f77 | 1195 | } |
a9789ef9 | 1196 | |
cc2b14d5 AS |
1197 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1198 | { | |
1199 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1200 | reg->type = SCALAR_VALUE; |
1201 | } | |
1202 | ||
61bd5218 JK |
1203 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1204 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1205 | { |
1206 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1207 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1208 | /* Something bad happened, let's kill all regs */ |
1209 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1210 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1211 | return; |
1212 | } | |
1213 | __mark_reg_known_zero(regs + regno); | |
1214 | } | |
1215 | ||
4ddb7416 DB |
1216 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
1217 | { | |
c25b2ae1 | 1218 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
4ddb7416 DB |
1219 | const struct bpf_map *map = reg->map_ptr; |
1220 | ||
1221 | if (map->inner_map_meta) { | |
1222 | reg->type = CONST_PTR_TO_MAP; | |
1223 | reg->map_ptr = map->inner_map_meta; | |
3e8ce298 AS |
1224 | /* transfer reg's id which is unique for every map_lookup_elem |
1225 | * as UID of the inner map. | |
1226 | */ | |
34d11a44 AS |
1227 | if (map_value_has_timer(map->inner_map_meta)) |
1228 | reg->map_uid = reg->id; | |
4ddb7416 DB |
1229 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
1230 | reg->type = PTR_TO_XDP_SOCK; | |
1231 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || | |
1232 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
1233 | reg->type = PTR_TO_SOCKET; | |
1234 | } else { | |
1235 | reg->type = PTR_TO_MAP_VALUE; | |
1236 | } | |
c25b2ae1 | 1237 | return; |
4ddb7416 | 1238 | } |
c25b2ae1 HL |
1239 | |
1240 | reg->type &= ~PTR_MAYBE_NULL; | |
4ddb7416 DB |
1241 | } |
1242 | ||
de8f3a83 DB |
1243 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1244 | { | |
1245 | return type_is_pkt_pointer(reg->type); | |
1246 | } | |
1247 | ||
1248 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1249 | { | |
1250 | return reg_is_pkt_pointer(reg) || | |
1251 | reg->type == PTR_TO_PACKET_END; | |
1252 | } | |
1253 | ||
1254 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1255 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1256 | enum bpf_reg_type which) | |
1257 | { | |
1258 | /* The register can already have a range from prior markings. | |
1259 | * This is fine as long as it hasn't been advanced from its | |
1260 | * origin. | |
1261 | */ | |
1262 | return reg->type == which && | |
1263 | reg->id == 0 && | |
1264 | reg->off == 0 && | |
1265 | tnum_equals_const(reg->var_off, 0); | |
1266 | } | |
1267 | ||
3f50f132 JF |
1268 | /* Reset the min/max bounds of a register */ |
1269 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1270 | { | |
1271 | reg->smin_value = S64_MIN; | |
1272 | reg->smax_value = S64_MAX; | |
1273 | reg->umin_value = 0; | |
1274 | reg->umax_value = U64_MAX; | |
1275 | ||
1276 | reg->s32_min_value = S32_MIN; | |
1277 | reg->s32_max_value = S32_MAX; | |
1278 | reg->u32_min_value = 0; | |
1279 | reg->u32_max_value = U32_MAX; | |
1280 | } | |
1281 | ||
1282 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1283 | { | |
1284 | reg->smin_value = S64_MIN; | |
1285 | reg->smax_value = S64_MAX; | |
1286 | reg->umin_value = 0; | |
1287 | reg->umax_value = U64_MAX; | |
1288 | } | |
1289 | ||
1290 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1291 | { | |
1292 | reg->s32_min_value = S32_MIN; | |
1293 | reg->s32_max_value = S32_MAX; | |
1294 | reg->u32_min_value = 0; | |
1295 | reg->u32_max_value = U32_MAX; | |
1296 | } | |
1297 | ||
1298 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1299 | { | |
1300 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1301 | ||
1302 | /* min signed is max(sign bit) | min(other bits) */ | |
1303 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1304 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1305 | /* max signed is min(sign bit) | max(other bits) */ | |
1306 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1307 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1308 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1309 | reg->u32_max_value = min(reg->u32_max_value, | |
1310 | (u32)(var32_off.value | var32_off.mask)); | |
1311 | } | |
1312 | ||
1313 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1314 | { |
1315 | /* min signed is max(sign bit) | min(other bits) */ | |
1316 | reg->smin_value = max_t(s64, reg->smin_value, | |
1317 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1318 | /* max signed is min(sign bit) | max(other bits) */ | |
1319 | reg->smax_value = min_t(s64, reg->smax_value, | |
1320 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1321 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1322 | reg->umax_value = min(reg->umax_value, | |
1323 | reg->var_off.value | reg->var_off.mask); | |
1324 | } | |
1325 | ||
3f50f132 JF |
1326 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1327 | { | |
1328 | __update_reg32_bounds(reg); | |
1329 | __update_reg64_bounds(reg); | |
1330 | } | |
1331 | ||
b03c9f9f | 1332 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1333 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1334 | { | |
1335 | /* Learn sign from signed bounds. | |
1336 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1337 | * are the same, so combine. This works even in the negative case, e.g. | |
1338 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1339 | */ | |
1340 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1341 | reg->s32_min_value = reg->u32_min_value = | |
1342 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1343 | reg->s32_max_value = reg->u32_max_value = | |
1344 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1345 | return; | |
1346 | } | |
1347 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1348 | * boundary, so we must be careful. | |
1349 | */ | |
1350 | if ((s32)reg->u32_max_value >= 0) { | |
1351 | /* Positive. We can't learn anything from the smin, but smax | |
1352 | * is positive, hence safe. | |
1353 | */ | |
1354 | reg->s32_min_value = reg->u32_min_value; | |
1355 | reg->s32_max_value = reg->u32_max_value = | |
1356 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1357 | } else if ((s32)reg->u32_min_value < 0) { | |
1358 | /* Negative. We can't learn anything from the smax, but smin | |
1359 | * is negative, hence safe. | |
1360 | */ | |
1361 | reg->s32_min_value = reg->u32_min_value = | |
1362 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1363 | reg->s32_max_value = reg->u32_max_value; | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1368 | { |
1369 | /* Learn sign from signed bounds. | |
1370 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1371 | * are the same, so combine. This works even in the negative case, e.g. | |
1372 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1373 | */ | |
1374 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1375 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1376 | reg->umin_value); | |
1377 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1378 | reg->umax_value); | |
1379 | return; | |
1380 | } | |
1381 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1382 | * boundary, so we must be careful. | |
1383 | */ | |
1384 | if ((s64)reg->umax_value >= 0) { | |
1385 | /* Positive. We can't learn anything from the smin, but smax | |
1386 | * is positive, hence safe. | |
1387 | */ | |
1388 | reg->smin_value = reg->umin_value; | |
1389 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1390 | reg->umax_value); | |
1391 | } else if ((s64)reg->umin_value < 0) { | |
1392 | /* Negative. We can't learn anything from the smax, but smin | |
1393 | * is negative, hence safe. | |
1394 | */ | |
1395 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1396 | reg->umin_value); | |
1397 | reg->smax_value = reg->umax_value; | |
1398 | } | |
1399 | } | |
1400 | ||
3f50f132 JF |
1401 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1402 | { | |
1403 | __reg32_deduce_bounds(reg); | |
1404 | __reg64_deduce_bounds(reg); | |
1405 | } | |
1406 | ||
b03c9f9f EC |
1407 | /* Attempts to improve var_off based on unsigned min/max information */ |
1408 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1409 | { | |
3f50f132 JF |
1410 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1411 | tnum_range(reg->umin_value, | |
1412 | reg->umax_value)); | |
1413 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1414 | tnum_range(reg->u32_min_value, | |
1415 | reg->u32_max_value)); | |
1416 | ||
1417 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1418 | } |
1419 | ||
e572ff80 DB |
1420 | static bool __reg32_bound_s64(s32 a) |
1421 | { | |
1422 | return a >= 0 && a <= S32_MAX; | |
1423 | } | |
1424 | ||
3f50f132 | 1425 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1426 | { |
3f50f132 JF |
1427 | reg->umin_value = reg->u32_min_value; |
1428 | reg->umax_value = reg->u32_max_value; | |
e572ff80 DB |
1429 | |
1430 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must | |
1431 | * be positive otherwise set to worse case bounds and refine later | |
1432 | * from tnum. | |
3f50f132 | 1433 | */ |
e572ff80 DB |
1434 | if (__reg32_bound_s64(reg->s32_min_value) && |
1435 | __reg32_bound_s64(reg->s32_max_value)) { | |
3a71dc36 | 1436 | reg->smin_value = reg->s32_min_value; |
e572ff80 DB |
1437 | reg->smax_value = reg->s32_max_value; |
1438 | } else { | |
3a71dc36 | 1439 | reg->smin_value = 0; |
e572ff80 DB |
1440 | reg->smax_value = U32_MAX; |
1441 | } | |
3f50f132 JF |
1442 | } |
1443 | ||
1444 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1445 | { | |
1446 | /* special case when 64-bit register has upper 32-bit register | |
1447 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1448 | * allowing us to use 32-bit bounds directly, | |
1449 | */ | |
1450 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1451 | __reg_assign_32_into_64(reg); | |
1452 | } else { | |
1453 | /* Otherwise the best we can do is push lower 32bit known and | |
1454 | * unknown bits into register (var_off set from jmp logic) | |
1455 | * then learn as much as possible from the 64-bit tnum | |
1456 | * known and unknown bits. The previous smin/smax bounds are | |
1457 | * invalid here because of jmp32 compare so mark them unknown | |
1458 | * so they do not impact tnum bounds calculation. | |
1459 | */ | |
1460 | __mark_reg64_unbounded(reg); | |
1461 | __update_reg_bounds(reg); | |
1462 | } | |
1463 | ||
1464 | /* Intersecting with the old var_off might have improved our bounds | |
1465 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1466 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1467 | */ | |
1468 | __reg_deduce_bounds(reg); | |
1469 | __reg_bound_offset(reg); | |
1470 | __update_reg_bounds(reg); | |
1471 | } | |
1472 | ||
1473 | static bool __reg64_bound_s32(s64 a) | |
1474 | { | |
388e2c0b | 1475 | return a >= S32_MIN && a <= S32_MAX; |
3f50f132 JF |
1476 | } |
1477 | ||
1478 | static bool __reg64_bound_u32(u64 a) | |
1479 | { | |
b9979db8 | 1480 | return a >= U32_MIN && a <= U32_MAX; |
3f50f132 JF |
1481 | } |
1482 | ||
1483 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1484 | { | |
1485 | __mark_reg32_unbounded(reg); | |
1486 | ||
b0270958 | 1487 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 1488 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 1489 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 1490 | } |
10bf4e83 | 1491 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
3f50f132 | 1492 | reg->u32_min_value = (u32)reg->umin_value; |
3f50f132 | 1493 | reg->u32_max_value = (u32)reg->umax_value; |
10bf4e83 | 1494 | } |
3f50f132 JF |
1495 | |
1496 | /* Intersecting with the old var_off might have improved our bounds | |
1497 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1498 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1499 | */ | |
1500 | __reg_deduce_bounds(reg); | |
1501 | __reg_bound_offset(reg); | |
1502 | __update_reg_bounds(reg); | |
b03c9f9f EC |
1503 | } |
1504 | ||
f1174f77 | 1505 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1506 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1507 | struct bpf_reg_state *reg) | |
f1174f77 | 1508 | { |
a9c676bc AS |
1509 | /* |
1510 | * Clear type, id, off, and union(map_ptr, range) and | |
1511 | * padding between 'type' and union | |
1512 | */ | |
1513 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1514 | reg->type = SCALAR_VALUE; |
f1174f77 | 1515 | reg->var_off = tnum_unknown; |
f4d7e40a | 1516 | reg->frameno = 0; |
2c78ee89 | 1517 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1518 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1519 | } |
1520 | ||
61bd5218 JK |
1521 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1522 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1523 | { |
1524 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1525 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1526 | /* Something bad happened, let's kill all regs except FP */ |
1527 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1528 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1529 | return; |
1530 | } | |
f54c7898 | 1531 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1532 | } |
1533 | ||
f54c7898 DB |
1534 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1535 | struct bpf_reg_state *reg) | |
f1174f77 | 1536 | { |
f54c7898 | 1537 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1538 | reg->type = NOT_INIT; |
1539 | } | |
1540 | ||
61bd5218 JK |
1541 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1542 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1543 | { |
1544 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1545 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1546 | /* Something bad happened, let's kill all regs except FP */ |
1547 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1548 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1549 | return; |
1550 | } | |
f54c7898 | 1551 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1552 | } |
1553 | ||
41c48f3a AI |
1554 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1555 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f | 1556 | enum bpf_reg_type reg_type, |
c6f1bfe8 YS |
1557 | struct btf *btf, u32 btf_id, |
1558 | enum bpf_type_flag flag) | |
41c48f3a AI |
1559 | { |
1560 | if (reg_type == SCALAR_VALUE) { | |
1561 | mark_reg_unknown(env, regs, regno); | |
1562 | return; | |
1563 | } | |
1564 | mark_reg_known_zero(env, regs, regno); | |
c6f1bfe8 | 1565 | regs[regno].type = PTR_TO_BTF_ID | flag; |
22dc4a0f | 1566 | regs[regno].btf = btf; |
41c48f3a AI |
1567 | regs[regno].btf_id = btf_id; |
1568 | } | |
1569 | ||
5327ed3d | 1570 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1571 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1572 | struct bpf_func_state *state) |
17a52670 | 1573 | { |
f4d7e40a | 1574 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1575 | int i; |
1576 | ||
dc503a8a | 1577 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1578 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1579 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1580 | regs[i].parent = NULL; |
5327ed3d | 1581 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1582 | } |
17a52670 AS |
1583 | |
1584 | /* frame pointer */ | |
f1174f77 | 1585 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1586 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1587 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1588 | } |
1589 | ||
f4d7e40a AS |
1590 | #define BPF_MAIN_FUNC (-1) |
1591 | static void init_func_state(struct bpf_verifier_env *env, | |
1592 | struct bpf_func_state *state, | |
1593 | int callsite, int frameno, int subprogno) | |
1594 | { | |
1595 | state->callsite = callsite; | |
1596 | state->frameno = frameno; | |
1597 | state->subprogno = subprogno; | |
1598 | init_reg_state(env, state); | |
0f55f9ed | 1599 | mark_verifier_state_scratched(env); |
f4d7e40a AS |
1600 | } |
1601 | ||
bfc6bb74 AS |
1602 | /* Similar to push_stack(), but for async callbacks */ |
1603 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, | |
1604 | int insn_idx, int prev_insn_idx, | |
1605 | int subprog) | |
1606 | { | |
1607 | struct bpf_verifier_stack_elem *elem; | |
1608 | struct bpf_func_state *frame; | |
1609 | ||
1610 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); | |
1611 | if (!elem) | |
1612 | goto err; | |
1613 | ||
1614 | elem->insn_idx = insn_idx; | |
1615 | elem->prev_insn_idx = prev_insn_idx; | |
1616 | elem->next = env->head; | |
1617 | elem->log_pos = env->log.len_used; | |
1618 | env->head = elem; | |
1619 | env->stack_size++; | |
1620 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { | |
1621 | verbose(env, | |
1622 | "The sequence of %d jumps is too complex for async cb.\n", | |
1623 | env->stack_size); | |
1624 | goto err; | |
1625 | } | |
1626 | /* Unlike push_stack() do not copy_verifier_state(). | |
1627 | * The caller state doesn't matter. | |
1628 | * This is async callback. It starts in a fresh stack. | |
1629 | * Initialize it similar to do_check_common(). | |
1630 | */ | |
1631 | elem->st.branches = 1; | |
1632 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); | |
1633 | if (!frame) | |
1634 | goto err; | |
1635 | init_func_state(env, frame, | |
1636 | BPF_MAIN_FUNC /* callsite */, | |
1637 | 0 /* frameno within this callchain */, | |
1638 | subprog /* subprog number within this prog */); | |
1639 | elem->st.frame[0] = frame; | |
1640 | return &elem->st; | |
1641 | err: | |
1642 | free_verifier_state(env->cur_state, true); | |
1643 | env->cur_state = NULL; | |
1644 | /* pop all elements and return */ | |
1645 | while (!pop_stack(env, NULL, NULL, false)); | |
1646 | return NULL; | |
1647 | } | |
1648 | ||
1649 | ||
17a52670 AS |
1650 | enum reg_arg_type { |
1651 | SRC_OP, /* register is used as source operand */ | |
1652 | DST_OP, /* register is used as destination operand */ | |
1653 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1654 | }; | |
1655 | ||
cc8b0b92 AS |
1656 | static int cmp_subprogs(const void *a, const void *b) |
1657 | { | |
9c8105bd JW |
1658 | return ((struct bpf_subprog_info *)a)->start - |
1659 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1660 | } |
1661 | ||
1662 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1663 | { | |
9c8105bd | 1664 | struct bpf_subprog_info *p; |
cc8b0b92 | 1665 | |
9c8105bd JW |
1666 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1667 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1668 | if (!p) |
1669 | return -ENOENT; | |
9c8105bd | 1670 | return p - env->subprog_info; |
cc8b0b92 AS |
1671 | |
1672 | } | |
1673 | ||
1674 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1675 | { | |
1676 | int insn_cnt = env->prog->len; | |
1677 | int ret; | |
1678 | ||
1679 | if (off >= insn_cnt || off < 0) { | |
1680 | verbose(env, "call to invalid destination\n"); | |
1681 | return -EINVAL; | |
1682 | } | |
1683 | ret = find_subprog(env, off); | |
1684 | if (ret >= 0) | |
282a0f46 | 1685 | return ret; |
4cb3d99c | 1686 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1687 | verbose(env, "too many subprograms\n"); |
1688 | return -E2BIG; | |
1689 | } | |
e6ac2450 | 1690 | /* determine subprog starts. The end is one before the next starts */ |
9c8105bd JW |
1691 | env->subprog_info[env->subprog_cnt++].start = off; |
1692 | sort(env->subprog_info, env->subprog_cnt, | |
1693 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
282a0f46 | 1694 | return env->subprog_cnt - 1; |
cc8b0b92 AS |
1695 | } |
1696 | ||
2357672c KKD |
1697 | #define MAX_KFUNC_DESCS 256 |
1698 | #define MAX_KFUNC_BTFS 256 | |
1699 | ||
e6ac2450 MKL |
1700 | struct bpf_kfunc_desc { |
1701 | struct btf_func_model func_model; | |
1702 | u32 func_id; | |
1703 | s32 imm; | |
2357672c KKD |
1704 | u16 offset; |
1705 | }; | |
1706 | ||
1707 | struct bpf_kfunc_btf { | |
1708 | struct btf *btf; | |
1709 | struct module *module; | |
1710 | u16 offset; | |
e6ac2450 MKL |
1711 | }; |
1712 | ||
e6ac2450 MKL |
1713 | struct bpf_kfunc_desc_tab { |
1714 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; | |
1715 | u32 nr_descs; | |
1716 | }; | |
1717 | ||
2357672c KKD |
1718 | struct bpf_kfunc_btf_tab { |
1719 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; | |
1720 | u32 nr_descs; | |
1721 | }; | |
1722 | ||
1723 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) | |
e6ac2450 MKL |
1724 | { |
1725 | const struct bpf_kfunc_desc *d0 = a; | |
1726 | const struct bpf_kfunc_desc *d1 = b; | |
1727 | ||
1728 | /* func_id is not greater than BTF_MAX_TYPE */ | |
2357672c KKD |
1729 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
1730 | } | |
1731 | ||
1732 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) | |
1733 | { | |
1734 | const struct bpf_kfunc_btf *d0 = a; | |
1735 | const struct bpf_kfunc_btf *d1 = b; | |
1736 | ||
1737 | return d0->offset - d1->offset; | |
e6ac2450 MKL |
1738 | } |
1739 | ||
1740 | static const struct bpf_kfunc_desc * | |
2357672c | 1741 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
e6ac2450 MKL |
1742 | { |
1743 | struct bpf_kfunc_desc desc = { | |
1744 | .func_id = func_id, | |
2357672c | 1745 | .offset = offset, |
e6ac2450 MKL |
1746 | }; |
1747 | struct bpf_kfunc_desc_tab *tab; | |
1748 | ||
1749 | tab = prog->aux->kfunc_tab; | |
1750 | return bsearch(&desc, tab->descs, tab->nr_descs, | |
2357672c KKD |
1751 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
1752 | } | |
1753 | ||
1754 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
b202d844 | 1755 | s16 offset) |
2357672c KKD |
1756 | { |
1757 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; | |
1758 | struct bpf_kfunc_btf_tab *tab; | |
1759 | struct bpf_kfunc_btf *b; | |
1760 | struct module *mod; | |
1761 | struct btf *btf; | |
1762 | int btf_fd; | |
1763 | ||
1764 | tab = env->prog->aux->kfunc_btf_tab; | |
1765 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, | |
1766 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); | |
1767 | if (!b) { | |
1768 | if (tab->nr_descs == MAX_KFUNC_BTFS) { | |
1769 | verbose(env, "too many different module BTFs\n"); | |
1770 | return ERR_PTR(-E2BIG); | |
1771 | } | |
1772 | ||
1773 | if (bpfptr_is_null(env->fd_array)) { | |
1774 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); | |
1775 | return ERR_PTR(-EPROTO); | |
1776 | } | |
1777 | ||
1778 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, | |
1779 | offset * sizeof(btf_fd), | |
1780 | sizeof(btf_fd))) | |
1781 | return ERR_PTR(-EFAULT); | |
1782 | ||
1783 | btf = btf_get_by_fd(btf_fd); | |
588cd7ef KKD |
1784 | if (IS_ERR(btf)) { |
1785 | verbose(env, "invalid module BTF fd specified\n"); | |
2357672c | 1786 | return btf; |
588cd7ef | 1787 | } |
2357672c KKD |
1788 | |
1789 | if (!btf_is_module(btf)) { | |
1790 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); | |
1791 | btf_put(btf); | |
1792 | return ERR_PTR(-EINVAL); | |
1793 | } | |
1794 | ||
1795 | mod = btf_try_get_module(btf); | |
1796 | if (!mod) { | |
1797 | btf_put(btf); | |
1798 | return ERR_PTR(-ENXIO); | |
1799 | } | |
1800 | ||
1801 | b = &tab->descs[tab->nr_descs++]; | |
1802 | b->btf = btf; | |
1803 | b->module = mod; | |
1804 | b->offset = offset; | |
1805 | ||
1806 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1807 | kfunc_btf_cmp_by_off, NULL); | |
1808 | } | |
2357672c | 1809 | return b->btf; |
e6ac2450 MKL |
1810 | } |
1811 | ||
2357672c KKD |
1812 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
1813 | { | |
1814 | if (!tab) | |
1815 | return; | |
1816 | ||
1817 | while (tab->nr_descs--) { | |
1818 | module_put(tab->descs[tab->nr_descs].module); | |
1819 | btf_put(tab->descs[tab->nr_descs].btf); | |
1820 | } | |
1821 | kfree(tab); | |
1822 | } | |
1823 | ||
1824 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, | |
b202d844 | 1825 | u32 func_id, s16 offset) |
2357672c | 1826 | { |
2357672c KKD |
1827 | if (offset) { |
1828 | if (offset < 0) { | |
1829 | /* In the future, this can be allowed to increase limit | |
1830 | * of fd index into fd_array, interpreted as u16. | |
1831 | */ | |
1832 | verbose(env, "negative offset disallowed for kernel module function call\n"); | |
1833 | return ERR_PTR(-EINVAL); | |
1834 | } | |
1835 | ||
b202d844 | 1836 | return __find_kfunc_desc_btf(env, offset); |
2357672c KKD |
1837 | } |
1838 | return btf_vmlinux ?: ERR_PTR(-ENOENT); | |
e6ac2450 MKL |
1839 | } |
1840 | ||
2357672c | 1841 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
e6ac2450 MKL |
1842 | { |
1843 | const struct btf_type *func, *func_proto; | |
2357672c | 1844 | struct bpf_kfunc_btf_tab *btf_tab; |
e6ac2450 MKL |
1845 | struct bpf_kfunc_desc_tab *tab; |
1846 | struct bpf_prog_aux *prog_aux; | |
1847 | struct bpf_kfunc_desc *desc; | |
1848 | const char *func_name; | |
2357672c | 1849 | struct btf *desc_btf; |
8cbf062a | 1850 | unsigned long call_imm; |
e6ac2450 MKL |
1851 | unsigned long addr; |
1852 | int err; | |
1853 | ||
1854 | prog_aux = env->prog->aux; | |
1855 | tab = prog_aux->kfunc_tab; | |
2357672c | 1856 | btf_tab = prog_aux->kfunc_btf_tab; |
e6ac2450 MKL |
1857 | if (!tab) { |
1858 | if (!btf_vmlinux) { | |
1859 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
1860 | return -ENOTSUPP; | |
1861 | } | |
1862 | ||
1863 | if (!env->prog->jit_requested) { | |
1864 | verbose(env, "JIT is required for calling kernel function\n"); | |
1865 | return -ENOTSUPP; | |
1866 | } | |
1867 | ||
1868 | if (!bpf_jit_supports_kfunc_call()) { | |
1869 | verbose(env, "JIT does not support calling kernel function\n"); | |
1870 | return -ENOTSUPP; | |
1871 | } | |
1872 | ||
1873 | if (!env->prog->gpl_compatible) { | |
1874 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); | |
1875 | return -EINVAL; | |
1876 | } | |
1877 | ||
1878 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); | |
1879 | if (!tab) | |
1880 | return -ENOMEM; | |
1881 | prog_aux->kfunc_tab = tab; | |
1882 | } | |
1883 | ||
a5d82727 KKD |
1884 | /* func_id == 0 is always invalid, but instead of returning an error, be |
1885 | * conservative and wait until the code elimination pass before returning | |
1886 | * error, so that invalid calls that get pruned out can be in BPF programs | |
1887 | * loaded from userspace. It is also required that offset be untouched | |
1888 | * for such calls. | |
1889 | */ | |
1890 | if (!func_id && !offset) | |
1891 | return 0; | |
1892 | ||
2357672c KKD |
1893 | if (!btf_tab && offset) { |
1894 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); | |
1895 | if (!btf_tab) | |
1896 | return -ENOMEM; | |
1897 | prog_aux->kfunc_btf_tab = btf_tab; | |
1898 | } | |
1899 | ||
b202d844 | 1900 | desc_btf = find_kfunc_desc_btf(env, func_id, offset); |
2357672c KKD |
1901 | if (IS_ERR(desc_btf)) { |
1902 | verbose(env, "failed to find BTF for kernel function\n"); | |
1903 | return PTR_ERR(desc_btf); | |
1904 | } | |
1905 | ||
1906 | if (find_kfunc_desc(env->prog, func_id, offset)) | |
e6ac2450 MKL |
1907 | return 0; |
1908 | ||
1909 | if (tab->nr_descs == MAX_KFUNC_DESCS) { | |
1910 | verbose(env, "too many different kernel function calls\n"); | |
1911 | return -E2BIG; | |
1912 | } | |
1913 | ||
2357672c | 1914 | func = btf_type_by_id(desc_btf, func_id); |
e6ac2450 MKL |
1915 | if (!func || !btf_type_is_func(func)) { |
1916 | verbose(env, "kernel btf_id %u is not a function\n", | |
1917 | func_id); | |
1918 | return -EINVAL; | |
1919 | } | |
2357672c | 1920 | func_proto = btf_type_by_id(desc_btf, func->type); |
e6ac2450 MKL |
1921 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
1922 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", | |
1923 | func_id); | |
1924 | return -EINVAL; | |
1925 | } | |
1926 | ||
2357672c | 1927 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
e6ac2450 MKL |
1928 | addr = kallsyms_lookup_name(func_name); |
1929 | if (!addr) { | |
1930 | verbose(env, "cannot find address for kernel function %s\n", | |
1931 | func_name); | |
1932 | return -EINVAL; | |
1933 | } | |
1934 | ||
8cbf062a HT |
1935 | call_imm = BPF_CALL_IMM(addr); |
1936 | /* Check whether or not the relative offset overflows desc->imm */ | |
1937 | if ((unsigned long)(s32)call_imm != call_imm) { | |
1938 | verbose(env, "address of kernel function %s is out of range\n", | |
1939 | func_name); | |
1940 | return -EINVAL; | |
1941 | } | |
1942 | ||
e6ac2450 MKL |
1943 | desc = &tab->descs[tab->nr_descs++]; |
1944 | desc->func_id = func_id; | |
8cbf062a | 1945 | desc->imm = call_imm; |
2357672c KKD |
1946 | desc->offset = offset; |
1947 | err = btf_distill_func_proto(&env->log, desc_btf, | |
e6ac2450 MKL |
1948 | func_proto, func_name, |
1949 | &desc->func_model); | |
1950 | if (!err) | |
1951 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
2357672c | 1952 | kfunc_desc_cmp_by_id_off, NULL); |
e6ac2450 MKL |
1953 | return err; |
1954 | } | |
1955 | ||
1956 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) | |
1957 | { | |
1958 | const struct bpf_kfunc_desc *d0 = a; | |
1959 | const struct bpf_kfunc_desc *d1 = b; | |
1960 | ||
1961 | if (d0->imm > d1->imm) | |
1962 | return 1; | |
1963 | else if (d0->imm < d1->imm) | |
1964 | return -1; | |
1965 | return 0; | |
1966 | } | |
1967 | ||
1968 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) | |
1969 | { | |
1970 | struct bpf_kfunc_desc_tab *tab; | |
1971 | ||
1972 | tab = prog->aux->kfunc_tab; | |
1973 | if (!tab) | |
1974 | return; | |
1975 | ||
1976 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), | |
1977 | kfunc_desc_cmp_by_imm, NULL); | |
1978 | } | |
1979 | ||
1980 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) | |
1981 | { | |
1982 | return !!prog->aux->kfunc_tab; | |
1983 | } | |
1984 | ||
1985 | const struct btf_func_model * | |
1986 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, | |
1987 | const struct bpf_insn *insn) | |
1988 | { | |
1989 | const struct bpf_kfunc_desc desc = { | |
1990 | .imm = insn->imm, | |
1991 | }; | |
1992 | const struct bpf_kfunc_desc *res; | |
1993 | struct bpf_kfunc_desc_tab *tab; | |
1994 | ||
1995 | tab = prog->aux->kfunc_tab; | |
1996 | res = bsearch(&desc, tab->descs, tab->nr_descs, | |
1997 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); | |
1998 | ||
1999 | return res ? &res->func_model : NULL; | |
2000 | } | |
2001 | ||
2002 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) | |
cc8b0b92 | 2003 | { |
9c8105bd | 2004 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 | 2005 | struct bpf_insn *insn = env->prog->insnsi; |
e6ac2450 | 2006 | int i, ret, insn_cnt = env->prog->len; |
cc8b0b92 | 2007 | |
f910cefa JW |
2008 | /* Add entry function. */ |
2009 | ret = add_subprog(env, 0); | |
e6ac2450 | 2010 | if (ret) |
f910cefa JW |
2011 | return ret; |
2012 | ||
e6ac2450 MKL |
2013 | for (i = 0; i < insn_cnt; i++, insn++) { |
2014 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && | |
2015 | !bpf_pseudo_kfunc_call(insn)) | |
cc8b0b92 | 2016 | continue; |
e6ac2450 | 2017 | |
2c78ee89 | 2018 | if (!env->bpf_capable) { |
e6ac2450 | 2019 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
cc8b0b92 AS |
2020 | return -EPERM; |
2021 | } | |
e6ac2450 | 2022 | |
3990ed4c | 2023 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
e6ac2450 | 2024 | ret = add_subprog(env, i + insn->imm + 1); |
3990ed4c | 2025 | else |
2357672c | 2026 | ret = add_kfunc_call(env, insn->imm, insn->off); |
e6ac2450 | 2027 | |
cc8b0b92 AS |
2028 | if (ret < 0) |
2029 | return ret; | |
2030 | } | |
2031 | ||
4cb3d99c JW |
2032 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
2033 | * logic. 'subprog_cnt' should not be increased. | |
2034 | */ | |
2035 | subprog[env->subprog_cnt].start = insn_cnt; | |
2036 | ||
06ee7115 | 2037 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 2038 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 2039 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 | 2040 | |
e6ac2450 MKL |
2041 | return 0; |
2042 | } | |
2043 | ||
2044 | static int check_subprogs(struct bpf_verifier_env *env) | |
2045 | { | |
2046 | int i, subprog_start, subprog_end, off, cur_subprog = 0; | |
2047 | struct bpf_subprog_info *subprog = env->subprog_info; | |
2048 | struct bpf_insn *insn = env->prog->insnsi; | |
2049 | int insn_cnt = env->prog->len; | |
2050 | ||
cc8b0b92 | 2051 | /* now check that all jumps are within the same subprog */ |
4cb3d99c JW |
2052 | subprog_start = subprog[cur_subprog].start; |
2053 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
2054 | for (i = 0; i < insn_cnt; i++) { |
2055 | u8 code = insn[i].code; | |
2056 | ||
7f6e4312 MF |
2057 | if (code == (BPF_JMP | BPF_CALL) && |
2058 | insn[i].imm == BPF_FUNC_tail_call && | |
2059 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
2060 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
2061 | if (BPF_CLASS(code) == BPF_LD && |
2062 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
2063 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 2064 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
2065 | goto next; |
2066 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
2067 | goto next; | |
2068 | off = i + insn[i].off + 1; | |
2069 | if (off < subprog_start || off >= subprog_end) { | |
2070 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
2071 | return -EINVAL; | |
2072 | } | |
2073 | next: | |
2074 | if (i == subprog_end - 1) { | |
2075 | /* to avoid fall-through from one subprog into another | |
2076 | * the last insn of the subprog should be either exit | |
2077 | * or unconditional jump back | |
2078 | */ | |
2079 | if (code != (BPF_JMP | BPF_EXIT) && | |
2080 | code != (BPF_JMP | BPF_JA)) { | |
2081 | verbose(env, "last insn is not an exit or jmp\n"); | |
2082 | return -EINVAL; | |
2083 | } | |
2084 | subprog_start = subprog_end; | |
4cb3d99c JW |
2085 | cur_subprog++; |
2086 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 2087 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
2088 | } |
2089 | } | |
2090 | return 0; | |
2091 | } | |
2092 | ||
679c782d EC |
2093 | /* Parentage chain of this register (or stack slot) should take care of all |
2094 | * issues like callee-saved registers, stack slot allocation time, etc. | |
2095 | */ | |
f4d7e40a | 2096 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 2097 | const struct bpf_reg_state *state, |
5327ed3d | 2098 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
2099 | { |
2100 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 2101 | int cnt = 0; |
dc503a8a EC |
2102 | |
2103 | while (parent) { | |
2104 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 2105 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 2106 | break; |
9242b5f5 AS |
2107 | if (parent->live & REG_LIVE_DONE) { |
2108 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
c25b2ae1 | 2109 | reg_type_str(env, parent->type), |
9242b5f5 AS |
2110 | parent->var_off.value, parent->off); |
2111 | return -EFAULT; | |
2112 | } | |
5327ed3d JW |
2113 | /* The first condition is more likely to be true than the |
2114 | * second, checked it first. | |
2115 | */ | |
2116 | if ((parent->live & REG_LIVE_READ) == flag || | |
2117 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
2118 | /* The parentage chain never changes and |
2119 | * this parent was already marked as LIVE_READ. | |
2120 | * There is no need to keep walking the chain again and | |
2121 | * keep re-marking all parents as LIVE_READ. | |
2122 | * This case happens when the same register is read | |
2123 | * multiple times without writes into it in-between. | |
5327ed3d JW |
2124 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
2125 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
2126 | */ |
2127 | break; | |
dc503a8a | 2128 | /* ... then we depend on parent's value */ |
5327ed3d JW |
2129 | parent->live |= flag; |
2130 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
2131 | if (flag == REG_LIVE_READ64) | |
2132 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
2133 | state = parent; |
2134 | parent = state->parent; | |
f4d7e40a | 2135 | writes = true; |
06ee7115 | 2136 | cnt++; |
dc503a8a | 2137 | } |
06ee7115 AS |
2138 | |
2139 | if (env->longest_mark_read_walk < cnt) | |
2140 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 2141 | return 0; |
dc503a8a EC |
2142 | } |
2143 | ||
5327ed3d JW |
2144 | /* This function is supposed to be used by the following 32-bit optimization |
2145 | * code only. It returns TRUE if the source or destination register operates | |
2146 | * on 64-bit, otherwise return FALSE. | |
2147 | */ | |
2148 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
2149 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
2150 | { | |
2151 | u8 code, class, op; | |
2152 | ||
2153 | code = insn->code; | |
2154 | class = BPF_CLASS(code); | |
2155 | op = BPF_OP(code); | |
2156 | if (class == BPF_JMP) { | |
2157 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
2158 | * conservatively. | |
2159 | */ | |
2160 | if (op == BPF_EXIT) | |
2161 | return true; | |
2162 | if (op == BPF_CALL) { | |
2163 | /* BPF to BPF call will reach here because of marking | |
2164 | * caller saved clobber with DST_OP_NO_MARK for which we | |
2165 | * don't care the register def because they are anyway | |
2166 | * marked as NOT_INIT already. | |
2167 | */ | |
2168 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2169 | return false; | |
2170 | /* Helper call will reach here because of arg type | |
2171 | * check, conservatively return TRUE. | |
2172 | */ | |
2173 | if (t == SRC_OP) | |
2174 | return true; | |
2175 | ||
2176 | return false; | |
2177 | } | |
2178 | } | |
2179 | ||
2180 | if (class == BPF_ALU64 || class == BPF_JMP || | |
2181 | /* BPF_END always use BPF_ALU class. */ | |
2182 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
2183 | return true; | |
2184 | ||
2185 | if (class == BPF_ALU || class == BPF_JMP32) | |
2186 | return false; | |
2187 | ||
2188 | if (class == BPF_LDX) { | |
2189 | if (t != SRC_OP) | |
2190 | return BPF_SIZE(code) == BPF_DW; | |
2191 | /* LDX source must be ptr. */ | |
2192 | return true; | |
2193 | } | |
2194 | ||
2195 | if (class == BPF_STX) { | |
83a28819 IL |
2196 | /* BPF_STX (including atomic variants) has multiple source |
2197 | * operands, one of which is a ptr. Check whether the caller is | |
2198 | * asking about it. | |
2199 | */ | |
2200 | if (t == SRC_OP && reg->type != SCALAR_VALUE) | |
5327ed3d JW |
2201 | return true; |
2202 | return BPF_SIZE(code) == BPF_DW; | |
2203 | } | |
2204 | ||
2205 | if (class == BPF_LD) { | |
2206 | u8 mode = BPF_MODE(code); | |
2207 | ||
2208 | /* LD_IMM64 */ | |
2209 | if (mode == BPF_IMM) | |
2210 | return true; | |
2211 | ||
2212 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
2213 | if (t != SRC_OP) | |
2214 | return false; | |
2215 | ||
2216 | /* Implicit ctx ptr. */ | |
2217 | if (regno == BPF_REG_6) | |
2218 | return true; | |
2219 | ||
2220 | /* Explicit source could be any width. */ | |
2221 | return true; | |
2222 | } | |
2223 | ||
2224 | if (class == BPF_ST) | |
2225 | /* The only source register for BPF_ST is a ptr. */ | |
2226 | return true; | |
2227 | ||
2228 | /* Conservatively return true at default. */ | |
2229 | return true; | |
2230 | } | |
2231 | ||
83a28819 IL |
2232 | /* Return the regno defined by the insn, or -1. */ |
2233 | static int insn_def_regno(const struct bpf_insn *insn) | |
b325fbca | 2234 | { |
83a28819 IL |
2235 | switch (BPF_CLASS(insn->code)) { |
2236 | case BPF_JMP: | |
2237 | case BPF_JMP32: | |
2238 | case BPF_ST: | |
2239 | return -1; | |
2240 | case BPF_STX: | |
2241 | if (BPF_MODE(insn->code) == BPF_ATOMIC && | |
2242 | (insn->imm & BPF_FETCH)) { | |
2243 | if (insn->imm == BPF_CMPXCHG) | |
2244 | return BPF_REG_0; | |
2245 | else | |
2246 | return insn->src_reg; | |
2247 | } else { | |
2248 | return -1; | |
2249 | } | |
2250 | default: | |
2251 | return insn->dst_reg; | |
2252 | } | |
b325fbca JW |
2253 | } |
2254 | ||
2255 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
2256 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
2257 | { | |
83a28819 IL |
2258 | int dst_reg = insn_def_regno(insn); |
2259 | ||
2260 | if (dst_reg == -1) | |
b325fbca JW |
2261 | return false; |
2262 | ||
83a28819 | 2263 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
b325fbca JW |
2264 | } |
2265 | ||
5327ed3d JW |
2266 | static void mark_insn_zext(struct bpf_verifier_env *env, |
2267 | struct bpf_reg_state *reg) | |
2268 | { | |
2269 | s32 def_idx = reg->subreg_def; | |
2270 | ||
2271 | if (def_idx == DEF_NOT_SUBREG) | |
2272 | return; | |
2273 | ||
2274 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
2275 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
2276 | reg->subreg_def = DEF_NOT_SUBREG; | |
2277 | } | |
2278 | ||
dc503a8a | 2279 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
2280 | enum reg_arg_type t) |
2281 | { | |
f4d7e40a AS |
2282 | struct bpf_verifier_state *vstate = env->cur_state; |
2283 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 2284 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 2285 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 2286 | bool rw64; |
dc503a8a | 2287 | |
17a52670 | 2288 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 2289 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
2290 | return -EINVAL; |
2291 | } | |
2292 | ||
0f55f9ed CL |
2293 | mark_reg_scratched(env, regno); |
2294 | ||
c342dc10 | 2295 | reg = ®s[regno]; |
5327ed3d | 2296 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
2297 | if (t == SRC_OP) { |
2298 | /* check whether register used as source operand can be read */ | |
c342dc10 | 2299 | if (reg->type == NOT_INIT) { |
61bd5218 | 2300 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
2301 | return -EACCES; |
2302 | } | |
679c782d | 2303 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
2304 | if (regno == BPF_REG_FP) |
2305 | return 0; | |
2306 | ||
5327ed3d JW |
2307 | if (rw64) |
2308 | mark_insn_zext(env, reg); | |
2309 | ||
2310 | return mark_reg_read(env, reg, reg->parent, | |
2311 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
2312 | } else { |
2313 | /* check whether register used as dest operand can be written to */ | |
2314 | if (regno == BPF_REG_FP) { | |
61bd5218 | 2315 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
2316 | return -EACCES; |
2317 | } | |
c342dc10 | 2318 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 2319 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 2320 | if (t == DST_OP) |
61bd5218 | 2321 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
2322 | } |
2323 | return 0; | |
2324 | } | |
2325 | ||
b5dc0163 AS |
2326 | /* for any branch, call, exit record the history of jmps in the given state */ |
2327 | static int push_jmp_history(struct bpf_verifier_env *env, | |
2328 | struct bpf_verifier_state *cur) | |
2329 | { | |
2330 | u32 cnt = cur->jmp_history_cnt; | |
2331 | struct bpf_idx_pair *p; | |
2332 | ||
2333 | cnt++; | |
2334 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
2335 | if (!p) | |
2336 | return -ENOMEM; | |
2337 | p[cnt - 1].idx = env->insn_idx; | |
2338 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
2339 | cur->jmp_history = p; | |
2340 | cur->jmp_history_cnt = cnt; | |
2341 | return 0; | |
2342 | } | |
2343 | ||
2344 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
2345 | * history then previous instruction came from straight line execution. | |
2346 | */ | |
2347 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
2348 | u32 *history) | |
2349 | { | |
2350 | u32 cnt = *history; | |
2351 | ||
2352 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
2353 | i = st->jmp_history[cnt - 1].prev_idx; | |
2354 | (*history)--; | |
2355 | } else { | |
2356 | i--; | |
2357 | } | |
2358 | return i; | |
2359 | } | |
2360 | ||
e6ac2450 MKL |
2361 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
2362 | { | |
2363 | const struct btf_type *func; | |
2357672c | 2364 | struct btf *desc_btf; |
e6ac2450 MKL |
2365 | |
2366 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) | |
2367 | return NULL; | |
2368 | ||
b202d844 | 2369 | desc_btf = find_kfunc_desc_btf(data, insn->imm, insn->off); |
2357672c KKD |
2370 | if (IS_ERR(desc_btf)) |
2371 | return "<error>"; | |
2372 | ||
2373 | func = btf_type_by_id(desc_btf, insn->imm); | |
2374 | return btf_name_by_offset(desc_btf, func->name_off); | |
e6ac2450 MKL |
2375 | } |
2376 | ||
b5dc0163 AS |
2377 | /* For given verifier state backtrack_insn() is called from the last insn to |
2378 | * the first insn. Its purpose is to compute a bitmask of registers and | |
2379 | * stack slots that needs precision in the parent verifier state. | |
2380 | */ | |
2381 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
2382 | u32 *reg_mask, u64 *stack_mask) | |
2383 | { | |
2384 | const struct bpf_insn_cbs cbs = { | |
e6ac2450 | 2385 | .cb_call = disasm_kfunc_name, |
b5dc0163 AS |
2386 | .cb_print = verbose, |
2387 | .private_data = env, | |
2388 | }; | |
2389 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
2390 | u8 class = BPF_CLASS(insn->code); | |
2391 | u8 opcode = BPF_OP(insn->code); | |
2392 | u8 mode = BPF_MODE(insn->code); | |
2393 | u32 dreg = 1u << insn->dst_reg; | |
2394 | u32 sreg = 1u << insn->src_reg; | |
2395 | u32 spi; | |
2396 | ||
2397 | if (insn->code == 0) | |
2398 | return 0; | |
496f3324 | 2399 | if (env->log.level & BPF_LOG_LEVEL2) { |
b5dc0163 AS |
2400 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
2401 | verbose(env, "%d: ", idx); | |
2402 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
2403 | } | |
2404 | ||
2405 | if (class == BPF_ALU || class == BPF_ALU64) { | |
2406 | if (!(*reg_mask & dreg)) | |
2407 | return 0; | |
2408 | if (opcode == BPF_MOV) { | |
2409 | if (BPF_SRC(insn->code) == BPF_X) { | |
2410 | /* dreg = sreg | |
2411 | * dreg needs precision after this insn | |
2412 | * sreg needs precision before this insn | |
2413 | */ | |
2414 | *reg_mask &= ~dreg; | |
2415 | *reg_mask |= sreg; | |
2416 | } else { | |
2417 | /* dreg = K | |
2418 | * dreg needs precision after this insn. | |
2419 | * Corresponding register is already marked | |
2420 | * as precise=true in this verifier state. | |
2421 | * No further markings in parent are necessary | |
2422 | */ | |
2423 | *reg_mask &= ~dreg; | |
2424 | } | |
2425 | } else { | |
2426 | if (BPF_SRC(insn->code) == BPF_X) { | |
2427 | /* dreg += sreg | |
2428 | * both dreg and sreg need precision | |
2429 | * before this insn | |
2430 | */ | |
2431 | *reg_mask |= sreg; | |
2432 | } /* else dreg += K | |
2433 | * dreg still needs precision before this insn | |
2434 | */ | |
2435 | } | |
2436 | } else if (class == BPF_LDX) { | |
2437 | if (!(*reg_mask & dreg)) | |
2438 | return 0; | |
2439 | *reg_mask &= ~dreg; | |
2440 | ||
2441 | /* scalars can only be spilled into stack w/o losing precision. | |
2442 | * Load from any other memory can be zero extended. | |
2443 | * The desire to keep that precision is already indicated | |
2444 | * by 'precise' mark in corresponding register of this state. | |
2445 | * No further tracking necessary. | |
2446 | */ | |
2447 | if (insn->src_reg != BPF_REG_FP) | |
2448 | return 0; | |
b5dc0163 AS |
2449 | |
2450 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
2451 | * that [fp - off] slot contains scalar that needs to be | |
2452 | * tracked with precision | |
2453 | */ | |
2454 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
2455 | if (spi >= 64) { | |
2456 | verbose(env, "BUG spi %d\n", spi); | |
2457 | WARN_ONCE(1, "verifier backtracking bug"); | |
2458 | return -EFAULT; | |
2459 | } | |
2460 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 2461 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 2462 | if (*reg_mask & dreg) |
b3b50f05 | 2463 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
2464 | * to access memory. It means backtracking |
2465 | * encountered a case of pointer subtraction. | |
2466 | */ | |
2467 | return -ENOTSUPP; | |
2468 | /* scalars can only be spilled into stack */ | |
2469 | if (insn->dst_reg != BPF_REG_FP) | |
2470 | return 0; | |
b5dc0163 AS |
2471 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
2472 | if (spi >= 64) { | |
2473 | verbose(env, "BUG spi %d\n", spi); | |
2474 | WARN_ONCE(1, "verifier backtracking bug"); | |
2475 | return -EFAULT; | |
2476 | } | |
2477 | if (!(*stack_mask & (1ull << spi))) | |
2478 | return 0; | |
2479 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
2480 | if (class == BPF_STX) |
2481 | *reg_mask |= sreg; | |
b5dc0163 AS |
2482 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
2483 | if (opcode == BPF_CALL) { | |
2484 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
2485 | return -ENOTSUPP; | |
2486 | /* regular helper call sets R0 */ | |
2487 | *reg_mask &= ~1; | |
2488 | if (*reg_mask & 0x3f) { | |
2489 | /* if backtracing was looking for registers R1-R5 | |
2490 | * they should have been found already. | |
2491 | */ | |
2492 | verbose(env, "BUG regs %x\n", *reg_mask); | |
2493 | WARN_ONCE(1, "verifier backtracking bug"); | |
2494 | return -EFAULT; | |
2495 | } | |
2496 | } else if (opcode == BPF_EXIT) { | |
2497 | return -ENOTSUPP; | |
2498 | } | |
2499 | } else if (class == BPF_LD) { | |
2500 | if (!(*reg_mask & dreg)) | |
2501 | return 0; | |
2502 | *reg_mask &= ~dreg; | |
2503 | /* It's ld_imm64 or ld_abs or ld_ind. | |
2504 | * For ld_imm64 no further tracking of precision | |
2505 | * into parent is necessary | |
2506 | */ | |
2507 | if (mode == BPF_IND || mode == BPF_ABS) | |
2508 | /* to be analyzed */ | |
2509 | return -ENOTSUPP; | |
b5dc0163 AS |
2510 | } |
2511 | return 0; | |
2512 | } | |
2513 | ||
2514 | /* the scalar precision tracking algorithm: | |
2515 | * . at the start all registers have precise=false. | |
2516 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
2517 | * . once precise value of the scalar register is used in: | |
2518 | * . ptr + scalar alu | |
2519 | * . if (scalar cond K|scalar) | |
2520 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
2521 | * backtrack through the verifier states and mark all registers and | |
2522 | * stack slots with spilled constants that these scalar regisers | |
2523 | * should be precise. | |
2524 | * . during state pruning two registers (or spilled stack slots) | |
2525 | * are equivalent if both are not precise. | |
2526 | * | |
2527 | * Note the verifier cannot simply walk register parentage chain, | |
2528 | * since many different registers and stack slots could have been | |
2529 | * used to compute single precise scalar. | |
2530 | * | |
2531 | * The approach of starting with precise=true for all registers and then | |
2532 | * backtrack to mark a register as not precise when the verifier detects | |
2533 | * that program doesn't care about specific value (e.g., when helper | |
2534 | * takes register as ARG_ANYTHING parameter) is not safe. | |
2535 | * | |
2536 | * It's ok to walk single parentage chain of the verifier states. | |
2537 | * It's possible that this backtracking will go all the way till 1st insn. | |
2538 | * All other branches will be explored for needing precision later. | |
2539 | * | |
2540 | * The backtracking needs to deal with cases like: | |
2541 | * 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) | |
2542 | * r9 -= r8 | |
2543 | * r5 = r9 | |
2544 | * if r5 > 0x79f goto pc+7 | |
2545 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
2546 | * r5 += 1 | |
2547 | * ... | |
2548 | * call bpf_perf_event_output#25 | |
2549 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
2550 | * | |
2551 | * and this case: | |
2552 | * r6 = 1 | |
2553 | * call foo // uses callee's r6 inside to compute r0 | |
2554 | * r0 += r6 | |
2555 | * if r0 == 0 goto | |
2556 | * | |
2557 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
2558 | * | |
2559 | * Also if parent's curframe > frame where backtracking started, | |
2560 | * the verifier need to mark registers in both frames, otherwise callees | |
2561 | * may incorrectly prune callers. This is similar to | |
2562 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
2563 | * | |
2564 | * For now backtracking falls back into conservative marking. | |
2565 | */ | |
2566 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
2567 | struct bpf_verifier_state *st) | |
2568 | { | |
2569 | struct bpf_func_state *func; | |
2570 | struct bpf_reg_state *reg; | |
2571 | int i, j; | |
2572 | ||
2573 | /* big hammer: mark all scalars precise in this path. | |
2574 | * pop_stack may still get !precise scalars. | |
2575 | */ | |
2576 | for (; st; st = st->parent) | |
2577 | for (i = 0; i <= st->curframe; i++) { | |
2578 | func = st->frame[i]; | |
2579 | for (j = 0; j < BPF_REG_FP; j++) { | |
2580 | reg = &func->regs[j]; | |
2581 | if (reg->type != SCALAR_VALUE) | |
2582 | continue; | |
2583 | reg->precise = true; | |
2584 | } | |
2585 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
27113c59 | 2586 | if (!is_spilled_reg(&func->stack[j])) |
b5dc0163 AS |
2587 | continue; |
2588 | reg = &func->stack[j].spilled_ptr; | |
2589 | if (reg->type != SCALAR_VALUE) | |
2590 | continue; | |
2591 | reg->precise = true; | |
2592 | } | |
2593 | } | |
2594 | } | |
2595 | ||
a3ce685d AS |
2596 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
2597 | int spi) | |
b5dc0163 AS |
2598 | { |
2599 | struct bpf_verifier_state *st = env->cur_state; | |
2600 | int first_idx = st->first_insn_idx; | |
2601 | int last_idx = env->insn_idx; | |
2602 | struct bpf_func_state *func; | |
2603 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2604 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2605 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2606 | bool skip_first = true; |
a3ce685d | 2607 | bool new_marks = false; |
b5dc0163 AS |
2608 | int i, err; |
2609 | ||
2c78ee89 | 2610 | if (!env->bpf_capable) |
b5dc0163 AS |
2611 | return 0; |
2612 | ||
2613 | func = st->frame[st->curframe]; | |
a3ce685d AS |
2614 | if (regno >= 0) { |
2615 | reg = &func->regs[regno]; | |
2616 | if (reg->type != SCALAR_VALUE) { | |
2617 | WARN_ONCE(1, "backtracing misuse"); | |
2618 | return -EFAULT; | |
2619 | } | |
2620 | if (!reg->precise) | |
2621 | new_marks = true; | |
2622 | else | |
2623 | reg_mask = 0; | |
2624 | reg->precise = true; | |
b5dc0163 | 2625 | } |
b5dc0163 | 2626 | |
a3ce685d | 2627 | while (spi >= 0) { |
27113c59 | 2628 | if (!is_spilled_reg(&func->stack[spi])) { |
a3ce685d AS |
2629 | stack_mask = 0; |
2630 | break; | |
2631 | } | |
2632 | reg = &func->stack[spi].spilled_ptr; | |
2633 | if (reg->type != SCALAR_VALUE) { | |
2634 | stack_mask = 0; | |
2635 | break; | |
2636 | } | |
2637 | if (!reg->precise) | |
2638 | new_marks = true; | |
2639 | else | |
2640 | stack_mask = 0; | |
2641 | reg->precise = true; | |
2642 | break; | |
2643 | } | |
2644 | ||
2645 | if (!new_marks) | |
2646 | return 0; | |
2647 | if (!reg_mask && !stack_mask) | |
2648 | return 0; | |
b5dc0163 AS |
2649 | for (;;) { |
2650 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2651 | u32 history = st->jmp_history_cnt; |
2652 | ||
496f3324 | 2653 | if (env->log.level & BPF_LOG_LEVEL2) |
b5dc0163 AS |
2654 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
2655 | for (i = last_idx;;) { | |
2656 | if (skip_first) { | |
2657 | err = 0; | |
2658 | skip_first = false; | |
2659 | } else { | |
2660 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2661 | } | |
2662 | if (err == -ENOTSUPP) { | |
2663 | mark_all_scalars_precise(env, st); | |
2664 | return 0; | |
2665 | } else if (err) { | |
2666 | return err; | |
2667 | } | |
2668 | if (!reg_mask && !stack_mask) | |
2669 | /* Found assignment(s) into tracked register in this state. | |
2670 | * Since this state is already marked, just return. | |
2671 | * Nothing to be tracked further in the parent state. | |
2672 | */ | |
2673 | return 0; | |
2674 | if (i == first_idx) | |
2675 | break; | |
2676 | i = get_prev_insn_idx(st, i, &history); | |
2677 | if (i >= env->prog->len) { | |
2678 | /* This can happen if backtracking reached insn 0 | |
2679 | * and there are still reg_mask or stack_mask | |
2680 | * to backtrack. | |
2681 | * It means the backtracking missed the spot where | |
2682 | * particular register was initialized with a constant. | |
2683 | */ | |
2684 | verbose(env, "BUG backtracking idx %d\n", i); | |
2685 | WARN_ONCE(1, "verifier backtracking bug"); | |
2686 | return -EFAULT; | |
2687 | } | |
2688 | } | |
2689 | st = st->parent; | |
2690 | if (!st) | |
2691 | break; | |
2692 | ||
a3ce685d | 2693 | new_marks = false; |
b5dc0163 AS |
2694 | func = st->frame[st->curframe]; |
2695 | bitmap_from_u64(mask, reg_mask); | |
2696 | for_each_set_bit(i, mask, 32) { | |
2697 | reg = &func->regs[i]; | |
a3ce685d AS |
2698 | if (reg->type != SCALAR_VALUE) { |
2699 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2700 | continue; |
a3ce685d | 2701 | } |
b5dc0163 AS |
2702 | if (!reg->precise) |
2703 | new_marks = true; | |
2704 | reg->precise = true; | |
2705 | } | |
2706 | ||
2707 | bitmap_from_u64(mask, stack_mask); | |
2708 | for_each_set_bit(i, mask, 64) { | |
2709 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2710 | /* the sequence of instructions: |
2711 | * 2: (bf) r3 = r10 | |
2712 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2713 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2714 | * doesn't contain jmps. It's backtracked | |
2715 | * as a single block. | |
2716 | * During backtracking insn 3 is not recognized as | |
2717 | * stack access, so at the end of backtracking | |
2718 | * stack slot fp-8 is still marked in stack_mask. | |
2719 | * However the parent state may not have accessed | |
2720 | * fp-8 and it's "unallocated" stack space. | |
2721 | * In such case fallback to conservative. | |
b5dc0163 | 2722 | */ |
2339cd6c AS |
2723 | mark_all_scalars_precise(env, st); |
2724 | return 0; | |
b5dc0163 AS |
2725 | } |
2726 | ||
27113c59 | 2727 | if (!is_spilled_reg(&func->stack[i])) { |
a3ce685d | 2728 | stack_mask &= ~(1ull << i); |
b5dc0163 | 2729 | continue; |
a3ce685d | 2730 | } |
b5dc0163 | 2731 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2732 | if (reg->type != SCALAR_VALUE) { |
2733 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2734 | continue; |
a3ce685d | 2735 | } |
b5dc0163 AS |
2736 | if (!reg->precise) |
2737 | new_marks = true; | |
2738 | reg->precise = true; | |
2739 | } | |
496f3324 | 2740 | if (env->log.level & BPF_LOG_LEVEL2) { |
2e576648 | 2741 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
b5dc0163 AS |
2742 | new_marks ? "didn't have" : "already had", |
2743 | reg_mask, stack_mask); | |
2e576648 | 2744 | print_verifier_state(env, func, true); |
b5dc0163 AS |
2745 | } |
2746 | ||
a3ce685d AS |
2747 | if (!reg_mask && !stack_mask) |
2748 | break; | |
b5dc0163 AS |
2749 | if (!new_marks) |
2750 | break; | |
2751 | ||
2752 | last_idx = st->last_insn_idx; | |
2753 | first_idx = st->first_insn_idx; | |
2754 | } | |
2755 | return 0; | |
2756 | } | |
2757 | ||
a3ce685d AS |
2758 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
2759 | { | |
2760 | return __mark_chain_precision(env, regno, -1); | |
2761 | } | |
2762 | ||
2763 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2764 | { | |
2765 | return __mark_chain_precision(env, -1, spi); | |
2766 | } | |
b5dc0163 | 2767 | |
1be7f75d AS |
2768 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2769 | { | |
c25b2ae1 | 2770 | switch (base_type(type)) { |
1be7f75d | 2771 | case PTR_TO_MAP_VALUE: |
1be7f75d AS |
2772 | case PTR_TO_STACK: |
2773 | case PTR_TO_CTX: | |
969bf05e | 2774 | case PTR_TO_PACKET: |
de8f3a83 | 2775 | case PTR_TO_PACKET_META: |
969bf05e | 2776 | case PTR_TO_PACKET_END: |
d58e468b | 2777 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2778 | case CONST_PTR_TO_MAP: |
c64b7983 | 2779 | case PTR_TO_SOCKET: |
46f8bc92 | 2780 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 2781 | case PTR_TO_TCP_SOCK: |
fada7fdc | 2782 | case PTR_TO_XDP_SOCK: |
65726b5b | 2783 | case PTR_TO_BTF_ID: |
20b2aff4 | 2784 | case PTR_TO_BUF: |
744ea4e3 | 2785 | case PTR_TO_MEM: |
69c087ba YS |
2786 | case PTR_TO_FUNC: |
2787 | case PTR_TO_MAP_KEY: | |
1be7f75d AS |
2788 | return true; |
2789 | default: | |
2790 | return false; | |
2791 | } | |
2792 | } | |
2793 | ||
cc2b14d5 AS |
2794 | /* Does this register contain a constant zero? */ |
2795 | static bool register_is_null(struct bpf_reg_state *reg) | |
2796 | { | |
2797 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2798 | } | |
2799 | ||
f7cf25b2 AS |
2800 | static bool register_is_const(struct bpf_reg_state *reg) |
2801 | { | |
2802 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2803 | } | |
2804 | ||
5689d49b YS |
2805 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
2806 | { | |
2807 | return tnum_is_unknown(reg->var_off) && | |
2808 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
2809 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
2810 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
2811 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
2812 | } | |
2813 | ||
2814 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
2815 | { | |
2816 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
2817 | } | |
2818 | ||
6e7e63cb JH |
2819 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2820 | const struct bpf_reg_state *reg) | |
2821 | { | |
2822 | if (allow_ptr_leaks) | |
2823 | return false; | |
2824 | ||
2825 | return reg->type != SCALAR_VALUE; | |
2826 | } | |
2827 | ||
f7cf25b2 | 2828 | static void save_register_state(struct bpf_func_state *state, |
354e8f19 MKL |
2829 | int spi, struct bpf_reg_state *reg, |
2830 | int size) | |
f7cf25b2 AS |
2831 | { |
2832 | int i; | |
2833 | ||
2834 | state->stack[spi].spilled_ptr = *reg; | |
354e8f19 MKL |
2835 | if (size == BPF_REG_SIZE) |
2836 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
f7cf25b2 | 2837 | |
354e8f19 MKL |
2838 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
2839 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; | |
f7cf25b2 | 2840 | |
354e8f19 MKL |
2841 | /* size < 8 bytes spill */ |
2842 | for (; i; i--) | |
2843 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); | |
f7cf25b2 AS |
2844 | } |
2845 | ||
01f810ac | 2846 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
17a52670 AS |
2847 | * stack boundary and alignment are checked in check_mem_access() |
2848 | */ | |
01f810ac AM |
2849 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
2850 | /* stack frame we're writing to */ | |
2851 | struct bpf_func_state *state, | |
2852 | int off, int size, int value_regno, | |
2853 | int insn_idx) | |
17a52670 | 2854 | { |
f4d7e40a | 2855 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 2856 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 2857 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 2858 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 2859 | |
c69431aa | 2860 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
638f5b90 AS |
2861 | if (err) |
2862 | return err; | |
9c399760 AS |
2863 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
2864 | * so it's aligned access and [off, off + size) are within stack limits | |
2865 | */ | |
638f5b90 AS |
2866 | if (!env->allow_ptr_leaks && |
2867 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
2868 | size != BPF_REG_SIZE) { | |
2869 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
2870 | return -EACCES; | |
2871 | } | |
17a52670 | 2872 | |
f4d7e40a | 2873 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
2874 | if (value_regno >= 0) |
2875 | reg = &cur->regs[value_regno]; | |
2039f26f DB |
2876 | if (!env->bypass_spec_v4) { |
2877 | bool sanitize = reg && is_spillable_regtype(reg->type); | |
2878 | ||
2879 | for (i = 0; i < size; i++) { | |
2880 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { | |
2881 | sanitize = true; | |
2882 | break; | |
2883 | } | |
2884 | } | |
2885 | ||
2886 | if (sanitize) | |
2887 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; | |
2888 | } | |
17a52670 | 2889 | |
0f55f9ed | 2890 | mark_stack_slot_scratched(env, spi); |
354e8f19 | 2891 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
2c78ee89 | 2892 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
2893 | if (dst_reg != BPF_REG_FP) { |
2894 | /* The backtracking logic can only recognize explicit | |
2895 | * stack slot address like [fp - 8]. Other spill of | |
8fb33b60 | 2896 | * scalar via different register has to be conservative. |
b5dc0163 AS |
2897 | * Backtrack from here and mark all registers as precise |
2898 | * that contributed into 'reg' being a constant. | |
2899 | */ | |
2900 | err = mark_chain_precision(env, value_regno); | |
2901 | if (err) | |
2902 | return err; | |
2903 | } | |
354e8f19 | 2904 | save_register_state(state, spi, reg, size); |
f7cf25b2 | 2905 | } else if (reg && is_spillable_regtype(reg->type)) { |
17a52670 | 2906 | /* register containing pointer is being spilled into stack */ |
9c399760 | 2907 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 2908 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 2909 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
2910 | return -EACCES; |
2911 | } | |
f7cf25b2 | 2912 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2913 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2914 | return -EINVAL; | |
2915 | } | |
354e8f19 | 2916 | save_register_state(state, spi, reg, size); |
9c399760 | 2917 | } else { |
cc2b14d5 AS |
2918 | u8 type = STACK_MISC; |
2919 | ||
679c782d EC |
2920 | /* regular write of data into stack destroys any spilled ptr */ |
2921 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d | 2922 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
27113c59 | 2923 | if (is_spilled_reg(&state->stack[spi])) |
0bae2d4d | 2924 | for (i = 0; i < BPF_REG_SIZE; i++) |
354e8f19 | 2925 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
9c399760 | 2926 | |
cc2b14d5 AS |
2927 | /* only mark the slot as written if all 8 bytes were written |
2928 | * otherwise read propagation may incorrectly stop too soon | |
2929 | * when stack slots are partially written. | |
2930 | * This heuristic means that read propagation will be | |
2931 | * conservative, since it will add reg_live_read marks | |
2932 | * to stack slots all the way to first state when programs | |
2933 | * writes+reads less than 8 bytes | |
2934 | */ | |
2935 | if (size == BPF_REG_SIZE) | |
2936 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2937 | ||
2938 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2939 | if (reg && register_is_null(reg)) { |
2940 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2941 | err = mark_chain_precision(env, value_regno); | |
2942 | if (err) | |
2943 | return err; | |
cc2b14d5 | 2944 | type = STACK_ZERO; |
b5dc0163 | 2945 | } |
cc2b14d5 | 2946 | |
0bae2d4d | 2947 | /* Mark slots affected by this stack write. */ |
9c399760 | 2948 | for (i = 0; i < size; i++) |
638f5b90 | 2949 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2950 | type; |
17a52670 AS |
2951 | } |
2952 | return 0; | |
2953 | } | |
2954 | ||
01f810ac AM |
2955 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
2956 | * known to contain a variable offset. | |
2957 | * This function checks whether the write is permitted and conservatively | |
2958 | * tracks the effects of the write, considering that each stack slot in the | |
2959 | * dynamic range is potentially written to. | |
2960 | * | |
2961 | * 'off' includes 'regno->off'. | |
2962 | * 'value_regno' can be -1, meaning that an unknown value is being written to | |
2963 | * the stack. | |
2964 | * | |
2965 | * Spilled pointers in range are not marked as written because we don't know | |
2966 | * what's going to be actually written. This means that read propagation for | |
2967 | * future reads cannot be terminated by this write. | |
2968 | * | |
2969 | * For privileged programs, uninitialized stack slots are considered | |
2970 | * initialized by this write (even though we don't know exactly what offsets | |
2971 | * are going to be written to). The idea is that we don't want the verifier to | |
2972 | * reject future reads that access slots written to through variable offsets. | |
2973 | */ | |
2974 | static int check_stack_write_var_off(struct bpf_verifier_env *env, | |
2975 | /* func where register points to */ | |
2976 | struct bpf_func_state *state, | |
2977 | int ptr_regno, int off, int size, | |
2978 | int value_regno, int insn_idx) | |
2979 | { | |
2980 | struct bpf_func_state *cur; /* state of the current function */ | |
2981 | int min_off, max_off; | |
2982 | int i, err; | |
2983 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; | |
2984 | bool writing_zero = false; | |
2985 | /* set if the fact that we're writing a zero is used to let any | |
2986 | * stack slots remain STACK_ZERO | |
2987 | */ | |
2988 | bool zero_used = false; | |
2989 | ||
2990 | cur = env->cur_state->frame[env->cur_state->curframe]; | |
2991 | ptr_reg = &cur->regs[ptr_regno]; | |
2992 | min_off = ptr_reg->smin_value + off; | |
2993 | max_off = ptr_reg->smax_value + off + size; | |
2994 | if (value_regno >= 0) | |
2995 | value_reg = &cur->regs[value_regno]; | |
2996 | if (value_reg && register_is_null(value_reg)) | |
2997 | writing_zero = true; | |
2998 | ||
c69431aa | 2999 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
01f810ac AM |
3000 | if (err) |
3001 | return err; | |
3002 | ||
3003 | ||
3004 | /* Variable offset writes destroy any spilled pointers in range. */ | |
3005 | for (i = min_off; i < max_off; i++) { | |
3006 | u8 new_type, *stype; | |
3007 | int slot, spi; | |
3008 | ||
3009 | slot = -i - 1; | |
3010 | spi = slot / BPF_REG_SIZE; | |
3011 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
0f55f9ed | 3012 | mark_stack_slot_scratched(env, spi); |
01f810ac AM |
3013 | |
3014 | if (!env->allow_ptr_leaks | |
3015 | && *stype != NOT_INIT | |
3016 | && *stype != SCALAR_VALUE) { | |
3017 | /* Reject the write if there's are spilled pointers in | |
3018 | * range. If we didn't reject here, the ptr status | |
3019 | * would be erased below (even though not all slots are | |
3020 | * actually overwritten), possibly opening the door to | |
3021 | * leaks. | |
3022 | */ | |
3023 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", | |
3024 | insn_idx, i); | |
3025 | return -EINVAL; | |
3026 | } | |
3027 | ||
3028 | /* Erase all spilled pointers. */ | |
3029 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
3030 | ||
3031 | /* Update the slot type. */ | |
3032 | new_type = STACK_MISC; | |
3033 | if (writing_zero && *stype == STACK_ZERO) { | |
3034 | new_type = STACK_ZERO; | |
3035 | zero_used = true; | |
3036 | } | |
3037 | /* If the slot is STACK_INVALID, we check whether it's OK to | |
3038 | * pretend that it will be initialized by this write. The slot | |
3039 | * might not actually be written to, and so if we mark it as | |
3040 | * initialized future reads might leak uninitialized memory. | |
3041 | * For privileged programs, we will accept such reads to slots | |
3042 | * that may or may not be written because, if we're reject | |
3043 | * them, the error would be too confusing. | |
3044 | */ | |
3045 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { | |
3046 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", | |
3047 | insn_idx, i); | |
3048 | return -EINVAL; | |
3049 | } | |
3050 | *stype = new_type; | |
3051 | } | |
3052 | if (zero_used) { | |
3053 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
3054 | err = mark_chain_precision(env, value_regno); | |
3055 | if (err) | |
3056 | return err; | |
3057 | } | |
3058 | return 0; | |
3059 | } | |
3060 | ||
3061 | /* When register 'dst_regno' is assigned some values from stack[min_off, | |
3062 | * max_off), we set the register's type according to the types of the | |
3063 | * respective stack slots. If all the stack values are known to be zeros, then | |
3064 | * so is the destination reg. Otherwise, the register is considered to be | |
3065 | * SCALAR. This function does not deal with register filling; the caller must | |
3066 | * ensure that all spilled registers in the stack range have been marked as | |
3067 | * read. | |
3068 | */ | |
3069 | static void mark_reg_stack_read(struct bpf_verifier_env *env, | |
3070 | /* func where src register points to */ | |
3071 | struct bpf_func_state *ptr_state, | |
3072 | int min_off, int max_off, int dst_regno) | |
3073 | { | |
3074 | struct bpf_verifier_state *vstate = env->cur_state; | |
3075 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3076 | int i, slot, spi; | |
3077 | u8 *stype; | |
3078 | int zeros = 0; | |
3079 | ||
3080 | for (i = min_off; i < max_off; i++) { | |
3081 | slot = -i - 1; | |
3082 | spi = slot / BPF_REG_SIZE; | |
3083 | stype = ptr_state->stack[spi].slot_type; | |
3084 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) | |
3085 | break; | |
3086 | zeros++; | |
3087 | } | |
3088 | if (zeros == max_off - min_off) { | |
3089 | /* any access_size read into register is zero extended, | |
3090 | * so the whole register == const_zero | |
3091 | */ | |
3092 | __mark_reg_const_zero(&state->regs[dst_regno]); | |
3093 | /* backtracking doesn't support STACK_ZERO yet, | |
3094 | * so mark it precise here, so that later | |
3095 | * backtracking can stop here. | |
3096 | * Backtracking may not need this if this register | |
3097 | * doesn't participate in pointer adjustment. | |
3098 | * Forward propagation of precise flag is not | |
3099 | * necessary either. This mark is only to stop | |
3100 | * backtracking. Any register that contributed | |
3101 | * to const 0 was marked precise before spill. | |
3102 | */ | |
3103 | state->regs[dst_regno].precise = true; | |
3104 | } else { | |
3105 | /* have read misc data from the stack */ | |
3106 | mark_reg_unknown(env, state->regs, dst_regno); | |
3107 | } | |
3108 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; | |
3109 | } | |
3110 | ||
3111 | /* Read the stack at 'off' and put the results into the register indicated by | |
3112 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a | |
3113 | * spilled reg. | |
3114 | * | |
3115 | * 'dst_regno' can be -1, meaning that the read value is not going to a | |
3116 | * register. | |
3117 | * | |
3118 | * The access is assumed to be within the current stack bounds. | |
3119 | */ | |
3120 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, | |
3121 | /* func where src register points to */ | |
3122 | struct bpf_func_state *reg_state, | |
3123 | int off, int size, int dst_regno) | |
17a52670 | 3124 | { |
f4d7e40a AS |
3125 | struct bpf_verifier_state *vstate = env->cur_state; |
3126 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 3127 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 3128 | struct bpf_reg_state *reg; |
354e8f19 | 3129 | u8 *stype, type; |
17a52670 | 3130 | |
f4d7e40a | 3131 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 3132 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 3133 | |
27113c59 | 3134 | if (is_spilled_reg(®_state->stack[spi])) { |
f30d4968 MKL |
3135 | u8 spill_size = 1; |
3136 | ||
3137 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) | |
3138 | spill_size++; | |
354e8f19 | 3139 | |
f30d4968 | 3140 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
f7cf25b2 AS |
3141 | if (reg->type != SCALAR_VALUE) { |
3142 | verbose_linfo(env, env->insn_idx, "; "); | |
3143 | verbose(env, "invalid size of register fill\n"); | |
3144 | return -EACCES; | |
3145 | } | |
354e8f19 MKL |
3146 | |
3147 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
3148 | if (dst_regno < 0) | |
3149 | return 0; | |
3150 | ||
f30d4968 | 3151 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
354e8f19 MKL |
3152 | /* The earlier check_reg_arg() has decided the |
3153 | * subreg_def for this insn. Save it first. | |
3154 | */ | |
3155 | s32 subreg_def = state->regs[dst_regno].subreg_def; | |
3156 | ||
3157 | state->regs[dst_regno] = *reg; | |
3158 | state->regs[dst_regno].subreg_def = subreg_def; | |
3159 | } else { | |
3160 | for (i = 0; i < size; i++) { | |
3161 | type = stype[(slot - i) % BPF_REG_SIZE]; | |
3162 | if (type == STACK_SPILL) | |
3163 | continue; | |
3164 | if (type == STACK_MISC) | |
3165 | continue; | |
3166 | verbose(env, "invalid read from stack off %d+%d size %d\n", | |
3167 | off, i, size); | |
3168 | return -EACCES; | |
3169 | } | |
01f810ac | 3170 | mark_reg_unknown(env, state->regs, dst_regno); |
f7cf25b2 | 3171 | } |
354e8f19 | 3172 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
f7cf25b2 | 3173 | return 0; |
17a52670 | 3174 | } |
17a52670 | 3175 | |
01f810ac | 3176 | if (dst_regno >= 0) { |
17a52670 | 3177 | /* restore register state from stack */ |
01f810ac | 3178 | state->regs[dst_regno] = *reg; |
2f18f62e AS |
3179 | /* mark reg as written since spilled pointer state likely |
3180 | * has its liveness marks cleared by is_state_visited() | |
3181 | * which resets stack/reg liveness for state transitions | |
3182 | */ | |
01f810ac | 3183 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
6e7e63cb | 3184 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
01f810ac | 3185 | /* If dst_regno==-1, the caller is asking us whether |
6e7e63cb JH |
3186 | * it is acceptable to use this value as a SCALAR_VALUE |
3187 | * (e.g. for XADD). | |
3188 | * We must not allow unprivileged callers to do that | |
3189 | * with spilled pointers. | |
3190 | */ | |
3191 | verbose(env, "leaking pointer from stack off %d\n", | |
3192 | off); | |
3193 | return -EACCES; | |
dc503a8a | 3194 | } |
f7cf25b2 | 3195 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 AS |
3196 | } else { |
3197 | for (i = 0; i < size; i++) { | |
01f810ac AM |
3198 | type = stype[(slot - i) % BPF_REG_SIZE]; |
3199 | if (type == STACK_MISC) | |
cc2b14d5 | 3200 | continue; |
01f810ac | 3201 | if (type == STACK_ZERO) |
cc2b14d5 | 3202 | continue; |
cc2b14d5 AS |
3203 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
3204 | off, i, size); | |
3205 | return -EACCES; | |
3206 | } | |
f7cf25b2 | 3207 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
01f810ac AM |
3208 | if (dst_regno >= 0) |
3209 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); | |
17a52670 | 3210 | } |
f7cf25b2 | 3211 | return 0; |
17a52670 AS |
3212 | } |
3213 | ||
01f810ac AM |
3214 | enum stack_access_src { |
3215 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ | |
3216 | ACCESS_HELPER = 2, /* the access is performed by a helper */ | |
3217 | }; | |
3218 | ||
3219 | static int check_stack_range_initialized(struct bpf_verifier_env *env, | |
3220 | int regno, int off, int access_size, | |
3221 | bool zero_size_allowed, | |
3222 | enum stack_access_src type, | |
3223 | struct bpf_call_arg_meta *meta); | |
3224 | ||
3225 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) | |
3226 | { | |
3227 | return cur_regs(env) + regno; | |
3228 | } | |
3229 | ||
3230 | /* Read the stack at 'ptr_regno + off' and put the result into the register | |
3231 | * 'dst_regno'. | |
3232 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), | |
3233 | * but not its variable offset. | |
3234 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. | |
3235 | * | |
3236 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with | |
3237 | * filling registers (i.e. reads of spilled register cannot be detected when | |
3238 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing | |
3239 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable | |
3240 | * offset; for a fixed offset check_stack_read_fixed_off should be used | |
3241 | * instead. | |
3242 | */ | |
3243 | static int check_stack_read_var_off(struct bpf_verifier_env *env, | |
3244 | int ptr_regno, int off, int size, int dst_regno) | |
e4298d25 | 3245 | { |
01f810ac AM |
3246 | /* The state of the source register. */ |
3247 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3248 | struct bpf_func_state *ptr_state = func(env, reg); | |
3249 | int err; | |
3250 | int min_off, max_off; | |
3251 | ||
3252 | /* Note that we pass a NULL meta, so raw access will not be permitted. | |
e4298d25 | 3253 | */ |
01f810ac AM |
3254 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
3255 | false, ACCESS_DIRECT, NULL); | |
3256 | if (err) | |
3257 | return err; | |
3258 | ||
3259 | min_off = reg->smin_value + off; | |
3260 | max_off = reg->smax_value + off; | |
3261 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); | |
3262 | return 0; | |
3263 | } | |
3264 | ||
3265 | /* check_stack_read dispatches to check_stack_read_fixed_off or | |
3266 | * check_stack_read_var_off. | |
3267 | * | |
3268 | * The caller must ensure that the offset falls within the allocated stack | |
3269 | * bounds. | |
3270 | * | |
3271 | * 'dst_regno' is a register which will receive the value from the stack. It | |
3272 | * can be -1, meaning that the read value is not going to a register. | |
3273 | */ | |
3274 | static int check_stack_read(struct bpf_verifier_env *env, | |
3275 | int ptr_regno, int off, int size, | |
3276 | int dst_regno) | |
3277 | { | |
3278 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3279 | struct bpf_func_state *state = func(env, reg); | |
3280 | int err; | |
3281 | /* Some accesses are only permitted with a static offset. */ | |
3282 | bool var_off = !tnum_is_const(reg->var_off); | |
3283 | ||
3284 | /* The offset is required to be static when reads don't go to a | |
3285 | * register, in order to not leak pointers (see | |
3286 | * check_stack_read_fixed_off). | |
3287 | */ | |
3288 | if (dst_regno < 0 && var_off) { | |
e4298d25 DB |
3289 | char tn_buf[48]; |
3290 | ||
3291 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac | 3292 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
e4298d25 DB |
3293 | tn_buf, off, size); |
3294 | return -EACCES; | |
3295 | } | |
01f810ac AM |
3296 | /* Variable offset is prohibited for unprivileged mode for simplicity |
3297 | * since it requires corresponding support in Spectre masking for stack | |
3298 | * ALU. See also retrieve_ptr_limit(). | |
3299 | */ | |
3300 | if (!env->bypass_spec_v1 && var_off) { | |
3301 | char tn_buf[48]; | |
e4298d25 | 3302 | |
01f810ac AM |
3303 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3304 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", | |
3305 | ptr_regno, tn_buf); | |
e4298d25 DB |
3306 | return -EACCES; |
3307 | } | |
3308 | ||
01f810ac AM |
3309 | if (!var_off) { |
3310 | off += reg->var_off.value; | |
3311 | err = check_stack_read_fixed_off(env, state, off, size, | |
3312 | dst_regno); | |
3313 | } else { | |
3314 | /* Variable offset stack reads need more conservative handling | |
3315 | * than fixed offset ones. Note that dst_regno >= 0 on this | |
3316 | * branch. | |
3317 | */ | |
3318 | err = check_stack_read_var_off(env, ptr_regno, off, size, | |
3319 | dst_regno); | |
3320 | } | |
3321 | return err; | |
3322 | } | |
3323 | ||
3324 | ||
3325 | /* check_stack_write dispatches to check_stack_write_fixed_off or | |
3326 | * check_stack_write_var_off. | |
3327 | * | |
3328 | * 'ptr_regno' is the register used as a pointer into the stack. | |
3329 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). | |
3330 | * 'value_regno' is the register whose value we're writing to the stack. It can | |
3331 | * be -1, meaning that we're not writing from a register. | |
3332 | * | |
3333 | * The caller must ensure that the offset falls within the maximum stack size. | |
3334 | */ | |
3335 | static int check_stack_write(struct bpf_verifier_env *env, | |
3336 | int ptr_regno, int off, int size, | |
3337 | int value_regno, int insn_idx) | |
3338 | { | |
3339 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); | |
3340 | struct bpf_func_state *state = func(env, reg); | |
3341 | int err; | |
3342 | ||
3343 | if (tnum_is_const(reg->var_off)) { | |
3344 | off += reg->var_off.value; | |
3345 | err = check_stack_write_fixed_off(env, state, off, size, | |
3346 | value_regno, insn_idx); | |
3347 | } else { | |
3348 | /* Variable offset stack reads need more conservative handling | |
3349 | * than fixed offset ones. | |
3350 | */ | |
3351 | err = check_stack_write_var_off(env, state, | |
3352 | ptr_regno, off, size, | |
3353 | value_regno, insn_idx); | |
3354 | } | |
3355 | return err; | |
e4298d25 DB |
3356 | } |
3357 | ||
591fe988 DB |
3358 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
3359 | int off, int size, enum bpf_access_type type) | |
3360 | { | |
3361 | struct bpf_reg_state *regs = cur_regs(env); | |
3362 | struct bpf_map *map = regs[regno].map_ptr; | |
3363 | u32 cap = bpf_map_flags_to_cap(map); | |
3364 | ||
3365 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
3366 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
3367 | map->value_size, off, size); | |
3368 | return -EACCES; | |
3369 | } | |
3370 | ||
3371 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
3372 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
3373 | map->value_size, off, size); | |
3374 | return -EACCES; | |
3375 | } | |
3376 | ||
3377 | return 0; | |
3378 | } | |
3379 | ||
457f4436 AN |
3380 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
3381 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
3382 | int off, int size, u32 mem_size, | |
3383 | bool zero_size_allowed) | |
17a52670 | 3384 | { |
457f4436 AN |
3385 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
3386 | struct bpf_reg_state *reg; | |
3387 | ||
3388 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
3389 | return 0; | |
17a52670 | 3390 | |
457f4436 AN |
3391 | reg = &cur_regs(env)[regno]; |
3392 | switch (reg->type) { | |
69c087ba YS |
3393 | case PTR_TO_MAP_KEY: |
3394 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", | |
3395 | mem_size, off, size); | |
3396 | break; | |
457f4436 | 3397 | case PTR_TO_MAP_VALUE: |
61bd5218 | 3398 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
3399 | mem_size, off, size); |
3400 | break; | |
3401 | case PTR_TO_PACKET: | |
3402 | case PTR_TO_PACKET_META: | |
3403 | case PTR_TO_PACKET_END: | |
3404 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
3405 | off, size, regno, reg->id, off, mem_size); | |
3406 | break; | |
3407 | case PTR_TO_MEM: | |
3408 | default: | |
3409 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
3410 | mem_size, off, size); | |
17a52670 | 3411 | } |
457f4436 AN |
3412 | |
3413 | return -EACCES; | |
17a52670 AS |
3414 | } |
3415 | ||
457f4436 AN |
3416 | /* check read/write into a memory region with possible variable offset */ |
3417 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
3418 | int off, int size, u32 mem_size, | |
3419 | bool zero_size_allowed) | |
dbcfe5f7 | 3420 | { |
f4d7e40a AS |
3421 | struct bpf_verifier_state *vstate = env->cur_state; |
3422 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
3423 | struct bpf_reg_state *reg = &state->regs[regno]; |
3424 | int err; | |
3425 | ||
457f4436 | 3426 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
3427 | * need to try adding each of min_value and max_value to off |
3428 | * to make sure our theoretical access will be safe. | |
2e576648 CL |
3429 | * |
3430 | * The minimum value is only important with signed | |
dbcfe5f7 GB |
3431 | * comparisons where we can't assume the floor of a |
3432 | * value is 0. If we are using signed variables for our | |
3433 | * index'es we need to make sure that whatever we use | |
3434 | * will have a set floor within our range. | |
3435 | */ | |
b7137c4e DB |
3436 | if (reg->smin_value < 0 && |
3437 | (reg->smin_value == S64_MIN || | |
3438 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
3439 | reg->smin_value + off < 0)) { | |
61bd5218 | 3440 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
3441 | regno); |
3442 | return -EACCES; | |
3443 | } | |
457f4436 AN |
3444 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
3445 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 3446 | if (err) { |
457f4436 | 3447 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 3448 | regno); |
dbcfe5f7 GB |
3449 | return err; |
3450 | } | |
3451 | ||
b03c9f9f EC |
3452 | /* If we haven't set a max value then we need to bail since we can't be |
3453 | * sure we won't do bad things. | |
3454 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 3455 | */ |
b03c9f9f | 3456 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 3457 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
3458 | regno); |
3459 | return -EACCES; | |
3460 | } | |
457f4436 AN |
3461 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
3462 | mem_size, zero_size_allowed); | |
3463 | if (err) { | |
3464 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 3465 | regno); |
457f4436 AN |
3466 | return err; |
3467 | } | |
3468 | ||
3469 | return 0; | |
3470 | } | |
d83525ca | 3471 | |
e9147b44 KKD |
3472 | static int __check_ptr_off_reg(struct bpf_verifier_env *env, |
3473 | const struct bpf_reg_state *reg, int regno, | |
3474 | bool fixed_off_ok) | |
3475 | { | |
3476 | /* Access to this pointer-typed register or passing it to a helper | |
3477 | * is only allowed in its original, unmodified form. | |
3478 | */ | |
3479 | ||
3480 | if (reg->off < 0) { | |
3481 | verbose(env, "negative offset %s ptr R%d off=%d disallowed\n", | |
3482 | reg_type_str(env, reg->type), regno, reg->off); | |
3483 | return -EACCES; | |
3484 | } | |
3485 | ||
3486 | if (!fixed_off_ok && reg->off) { | |
3487 | verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n", | |
3488 | reg_type_str(env, reg->type), regno, reg->off); | |
3489 | return -EACCES; | |
3490 | } | |
3491 | ||
3492 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3493 | char tn_buf[48]; | |
3494 | ||
3495 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3496 | verbose(env, "variable %s access var_off=%s disallowed\n", | |
3497 | reg_type_str(env, reg->type), tn_buf); | |
3498 | return -EACCES; | |
3499 | } | |
3500 | ||
3501 | return 0; | |
3502 | } | |
3503 | ||
3504 | int check_ptr_off_reg(struct bpf_verifier_env *env, | |
3505 | const struct bpf_reg_state *reg, int regno) | |
3506 | { | |
3507 | return __check_ptr_off_reg(env, reg, regno, false); | |
3508 | } | |
3509 | ||
457f4436 AN |
3510 | /* check read/write into a map element with possible variable offset */ |
3511 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
3512 | int off, int size, bool zero_size_allowed) | |
3513 | { | |
3514 | struct bpf_verifier_state *vstate = env->cur_state; | |
3515 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3516 | struct bpf_reg_state *reg = &state->regs[regno]; | |
3517 | struct bpf_map *map = reg->map_ptr; | |
3518 | int err; | |
3519 | ||
3520 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
3521 | zero_size_allowed); | |
3522 | if (err) | |
3523 | return err; | |
3524 | ||
3525 | if (map_value_has_spin_lock(map)) { | |
3526 | u32 lock = map->spin_lock_off; | |
d83525ca AS |
3527 | |
3528 | /* if any part of struct bpf_spin_lock can be touched by | |
3529 | * load/store reject this program. | |
3530 | * To check that [x1, x2) overlaps with [y1, y2) | |
3531 | * it is sufficient to check x1 < y2 && y1 < x2. | |
3532 | */ | |
3533 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
3534 | lock < reg->umax_value + off + size) { | |
3535 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
3536 | return -EACCES; | |
3537 | } | |
3538 | } | |
68134668 AS |
3539 | if (map_value_has_timer(map)) { |
3540 | u32 t = map->timer_off; | |
3541 | ||
3542 | if (reg->smin_value + off < t + sizeof(struct bpf_timer) && | |
3543 | t < reg->umax_value + off + size) { | |
3544 | verbose(env, "bpf_timer cannot be accessed directly by load/store\n"); | |
3545 | return -EACCES; | |
3546 | } | |
3547 | } | |
f1174f77 | 3548 | return err; |
dbcfe5f7 GB |
3549 | } |
3550 | ||
969bf05e AS |
3551 | #define MAX_PACKET_OFF 0xffff |
3552 | ||
58e2af8b | 3553 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
3554 | const struct bpf_call_arg_meta *meta, |
3555 | enum bpf_access_type t) | |
4acf6c0b | 3556 | { |
7e40781c UP |
3557 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
3558 | ||
3559 | switch (prog_type) { | |
5d66fa7d | 3560 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
3561 | case BPF_PROG_TYPE_LWT_IN: |
3562 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 3563 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 3564 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 3565 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 3566 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
3567 | if (t == BPF_WRITE) |
3568 | return false; | |
8731745e | 3569 | fallthrough; |
5d66fa7d DB |
3570 | |
3571 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
3572 | case BPF_PROG_TYPE_SCHED_CLS: |
3573 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 3574 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 3575 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 3576 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 3577 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
3578 | if (meta) |
3579 | return meta->pkt_access; | |
3580 | ||
3581 | env->seen_direct_write = true; | |
4acf6c0b | 3582 | return true; |
0d01da6a SF |
3583 | |
3584 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
3585 | if (t == BPF_WRITE) | |
3586 | env->seen_direct_write = true; | |
3587 | ||
3588 | return true; | |
3589 | ||
4acf6c0b BB |
3590 | default: |
3591 | return false; | |
3592 | } | |
3593 | } | |
3594 | ||
f1174f77 | 3595 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 3596 | int size, bool zero_size_allowed) |
f1174f77 | 3597 | { |
638f5b90 | 3598 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
3599 | struct bpf_reg_state *reg = ®s[regno]; |
3600 | int err; | |
3601 | ||
3602 | /* We may have added a variable offset to the packet pointer; but any | |
3603 | * reg->range we have comes after that. We are only checking the fixed | |
3604 | * offset. | |
3605 | */ | |
3606 | ||
3607 | /* We don't allow negative numbers, because we aren't tracking enough | |
3608 | * detail to prove they're safe. | |
3609 | */ | |
b03c9f9f | 3610 | if (reg->smin_value < 0) { |
61bd5218 | 3611 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
3612 | regno); |
3613 | return -EACCES; | |
3614 | } | |
6d94e741 AS |
3615 | |
3616 | err = reg->range < 0 ? -EINVAL : | |
3617 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 3618 | zero_size_allowed); |
f1174f77 | 3619 | if (err) { |
61bd5218 | 3620 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
3621 | return err; |
3622 | } | |
e647815a | 3623 | |
457f4436 | 3624 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
3625 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
3626 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 3627 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
3628 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
3629 | */ | |
3630 | env->prog->aux->max_pkt_offset = | |
3631 | max_t(u32, env->prog->aux->max_pkt_offset, | |
3632 | off + reg->umax_value + size - 1); | |
3633 | ||
f1174f77 EC |
3634 | return err; |
3635 | } | |
3636 | ||
3637 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 3638 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 3639 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 3640 | struct btf **btf, u32 *btf_id) |
17a52670 | 3641 | { |
f96da094 DB |
3642 | struct bpf_insn_access_aux info = { |
3643 | .reg_type = *reg_type, | |
9e15db66 | 3644 | .log = &env->log, |
f96da094 | 3645 | }; |
31fd8581 | 3646 | |
4f9218aa | 3647 | if (env->ops->is_valid_access && |
5e43f899 | 3648 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
3649 | /* A non zero info.ctx_field_size indicates that this field is a |
3650 | * candidate for later verifier transformation to load the whole | |
3651 | * field and then apply a mask when accessed with a narrower | |
3652 | * access than actual ctx access size. A zero info.ctx_field_size | |
3653 | * will only allow for whole field access and rejects any other | |
3654 | * type of narrower access. | |
31fd8581 | 3655 | */ |
23994631 | 3656 | *reg_type = info.reg_type; |
31fd8581 | 3657 | |
c25b2ae1 | 3658 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 3659 | *btf = info.btf; |
9e15db66 | 3660 | *btf_id = info.btf_id; |
22dc4a0f | 3661 | } else { |
9e15db66 | 3662 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 3663 | } |
32bbe007 AS |
3664 | /* remember the offset of last byte accessed in ctx */ |
3665 | if (env->prog->aux->max_ctx_offset < off + size) | |
3666 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 3667 | return 0; |
32bbe007 | 3668 | } |
17a52670 | 3669 | |
61bd5218 | 3670 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
3671 | return -EACCES; |
3672 | } | |
3673 | ||
d58e468b PP |
3674 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
3675 | int size) | |
3676 | { | |
3677 | if (size < 0 || off < 0 || | |
3678 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
3679 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
3680 | off, size); | |
3681 | return -EACCES; | |
3682 | } | |
3683 | return 0; | |
3684 | } | |
3685 | ||
5f456649 MKL |
3686 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
3687 | u32 regno, int off, int size, | |
3688 | enum bpf_access_type t) | |
c64b7983 JS |
3689 | { |
3690 | struct bpf_reg_state *regs = cur_regs(env); | |
3691 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 3692 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 3693 | bool valid; |
c64b7983 JS |
3694 | |
3695 | if (reg->smin_value < 0) { | |
3696 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
3697 | regno); | |
3698 | return -EACCES; | |
3699 | } | |
3700 | ||
46f8bc92 MKL |
3701 | switch (reg->type) { |
3702 | case PTR_TO_SOCK_COMMON: | |
3703 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
3704 | break; | |
3705 | case PTR_TO_SOCKET: | |
3706 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
3707 | break; | |
655a51e5 MKL |
3708 | case PTR_TO_TCP_SOCK: |
3709 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
3710 | break; | |
fada7fdc JL |
3711 | case PTR_TO_XDP_SOCK: |
3712 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
3713 | break; | |
46f8bc92 MKL |
3714 | default: |
3715 | valid = false; | |
c64b7983 JS |
3716 | } |
3717 | ||
5f456649 | 3718 | |
46f8bc92 MKL |
3719 | if (valid) { |
3720 | env->insn_aux_data[insn_idx].ctx_field_size = | |
3721 | info.ctx_field_size; | |
3722 | return 0; | |
3723 | } | |
3724 | ||
3725 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
c25b2ae1 | 3726 | regno, reg_type_str(env, reg->type), off, size); |
46f8bc92 MKL |
3727 | |
3728 | return -EACCES; | |
c64b7983 JS |
3729 | } |
3730 | ||
4cabc5b1 DB |
3731 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
3732 | { | |
2a159c6f | 3733 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
3734 | } |
3735 | ||
f37a8cb8 DB |
3736 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
3737 | { | |
2a159c6f | 3738 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 3739 | |
46f8bc92 MKL |
3740 | return reg->type == PTR_TO_CTX; |
3741 | } | |
3742 | ||
3743 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
3744 | { | |
3745 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
3746 | ||
3747 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
3748 | } |
3749 | ||
ca369602 DB |
3750 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
3751 | { | |
2a159c6f | 3752 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
3753 | |
3754 | return type_is_pkt_pointer(reg->type); | |
3755 | } | |
3756 | ||
4b5defde DB |
3757 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
3758 | { | |
3759 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
3760 | ||
3761 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
3762 | return reg->type == PTR_TO_FLOW_KEYS; | |
3763 | } | |
3764 | ||
61bd5218 JK |
3765 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
3766 | const struct bpf_reg_state *reg, | |
d1174416 | 3767 | int off, int size, bool strict) |
969bf05e | 3768 | { |
f1174f77 | 3769 | struct tnum reg_off; |
e07b98d9 | 3770 | int ip_align; |
d1174416 DM |
3771 | |
3772 | /* Byte size accesses are always allowed. */ | |
3773 | if (!strict || size == 1) | |
3774 | return 0; | |
3775 | ||
e4eda884 DM |
3776 | /* For platforms that do not have a Kconfig enabling |
3777 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
3778 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
3779 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
3780 | * to this code only in strict mode where we want to emulate | |
3781 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
3782 | * unconditional IP align value of '2'. | |
e07b98d9 | 3783 | */ |
e4eda884 | 3784 | ip_align = 2; |
f1174f77 EC |
3785 | |
3786 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
3787 | if (!tnum_is_aligned(reg_off, size)) { | |
3788 | char tn_buf[48]; | |
3789 | ||
3790 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
3791 | verbose(env, |
3792 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 3793 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
3794 | return -EACCES; |
3795 | } | |
79adffcd | 3796 | |
969bf05e AS |
3797 | return 0; |
3798 | } | |
3799 | ||
61bd5218 JK |
3800 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
3801 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
3802 | const char *pointer_desc, |
3803 | int off, int size, bool strict) | |
79adffcd | 3804 | { |
f1174f77 EC |
3805 | struct tnum reg_off; |
3806 | ||
3807 | /* Byte size accesses are always allowed. */ | |
3808 | if (!strict || size == 1) | |
3809 | return 0; | |
3810 | ||
3811 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
3812 | if (!tnum_is_aligned(reg_off, size)) { | |
3813 | char tn_buf[48]; | |
3814 | ||
3815 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 3816 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 3817 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
3818 | return -EACCES; |
3819 | } | |
3820 | ||
969bf05e AS |
3821 | return 0; |
3822 | } | |
3823 | ||
e07b98d9 | 3824 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
3825 | const struct bpf_reg_state *reg, int off, |
3826 | int size, bool strict_alignment_once) | |
79adffcd | 3827 | { |
ca369602 | 3828 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 3829 | const char *pointer_desc = ""; |
d1174416 | 3830 | |
79adffcd DB |
3831 | switch (reg->type) { |
3832 | case PTR_TO_PACKET: | |
de8f3a83 DB |
3833 | case PTR_TO_PACKET_META: |
3834 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
3835 | * right in front, treat it the very same way. | |
3836 | */ | |
61bd5218 | 3837 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
3838 | case PTR_TO_FLOW_KEYS: |
3839 | pointer_desc = "flow keys "; | |
3840 | break; | |
69c087ba YS |
3841 | case PTR_TO_MAP_KEY: |
3842 | pointer_desc = "key "; | |
3843 | break; | |
f1174f77 EC |
3844 | case PTR_TO_MAP_VALUE: |
3845 | pointer_desc = "value "; | |
3846 | break; | |
3847 | case PTR_TO_CTX: | |
3848 | pointer_desc = "context "; | |
3849 | break; | |
3850 | case PTR_TO_STACK: | |
3851 | pointer_desc = "stack "; | |
01f810ac AM |
3852 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
3853 | * and check_stack_read_fixed_off() relies on stack accesses being | |
a5ec6ae1 JH |
3854 | * aligned. |
3855 | */ | |
3856 | strict = true; | |
f1174f77 | 3857 | break; |
c64b7983 JS |
3858 | case PTR_TO_SOCKET: |
3859 | pointer_desc = "sock "; | |
3860 | break; | |
46f8bc92 MKL |
3861 | case PTR_TO_SOCK_COMMON: |
3862 | pointer_desc = "sock_common "; | |
3863 | break; | |
655a51e5 MKL |
3864 | case PTR_TO_TCP_SOCK: |
3865 | pointer_desc = "tcp_sock "; | |
3866 | break; | |
fada7fdc JL |
3867 | case PTR_TO_XDP_SOCK: |
3868 | pointer_desc = "xdp_sock "; | |
3869 | break; | |
79adffcd | 3870 | default: |
f1174f77 | 3871 | break; |
79adffcd | 3872 | } |
61bd5218 JK |
3873 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
3874 | strict); | |
79adffcd DB |
3875 | } |
3876 | ||
f4d7e40a AS |
3877 | static int update_stack_depth(struct bpf_verifier_env *env, |
3878 | const struct bpf_func_state *func, | |
3879 | int off) | |
3880 | { | |
9c8105bd | 3881 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
3882 | |
3883 | if (stack >= -off) | |
3884 | return 0; | |
3885 | ||
3886 | /* update known max for given subprogram */ | |
9c8105bd | 3887 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
3888 | return 0; |
3889 | } | |
f4d7e40a | 3890 | |
70a87ffe AS |
3891 | /* starting from main bpf function walk all instructions of the function |
3892 | * and recursively walk all callees that given function can call. | |
3893 | * Ignore jump and exit insns. | |
3894 | * Since recursion is prevented by check_cfg() this algorithm | |
3895 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
3896 | */ | |
3897 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
3898 | { | |
9c8105bd JW |
3899 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
3900 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 3901 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 3902 | bool tail_call_reachable = false; |
70a87ffe AS |
3903 | int ret_insn[MAX_CALL_FRAMES]; |
3904 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 3905 | int j; |
f4d7e40a | 3906 | |
70a87ffe | 3907 | process_func: |
7f6e4312 MF |
3908 | /* protect against potential stack overflow that might happen when |
3909 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
3910 | * depth for such case down to 256 so that the worst case scenario | |
3911 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
3912 | * 8k). | |
3913 | * | |
3914 | * To get the idea what might happen, see an example: | |
3915 | * func1 -> sub rsp, 128 | |
3916 | * subfunc1 -> sub rsp, 256 | |
3917 | * tailcall1 -> add rsp, 256 | |
3918 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
3919 | * subfunc2 -> sub rsp, 64 | |
3920 | * subfunc22 -> sub rsp, 128 | |
3921 | * tailcall2 -> add rsp, 128 | |
3922 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
3923 | * | |
3924 | * tailcall will unwind the current stack frame but it will not get rid | |
3925 | * of caller's stack as shown on the example above. | |
3926 | */ | |
3927 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
3928 | verbose(env, | |
3929 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
3930 | depth); | |
3931 | return -EACCES; | |
3932 | } | |
70a87ffe AS |
3933 | /* round up to 32-bytes, since this is granularity |
3934 | * of interpreter stack size | |
3935 | */ | |
9c8105bd | 3936 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 3937 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 3938 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 3939 | frame + 1, depth); |
f4d7e40a AS |
3940 | return -EACCES; |
3941 | } | |
70a87ffe | 3942 | continue_func: |
4cb3d99c | 3943 | subprog_end = subprog[idx + 1].start; |
70a87ffe | 3944 | for (; i < subprog_end; i++) { |
7ddc80a4 AS |
3945 | int next_insn; |
3946 | ||
69c087ba | 3947 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
70a87ffe AS |
3948 | continue; |
3949 | /* remember insn and function to return to */ | |
3950 | ret_insn[frame] = i + 1; | |
9c8105bd | 3951 | ret_prog[frame] = idx; |
70a87ffe AS |
3952 | |
3953 | /* find the callee */ | |
7ddc80a4 AS |
3954 | next_insn = i + insn[i].imm + 1; |
3955 | idx = find_subprog(env, next_insn); | |
9c8105bd | 3956 | if (idx < 0) { |
70a87ffe | 3957 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
7ddc80a4 | 3958 | next_insn); |
70a87ffe AS |
3959 | return -EFAULT; |
3960 | } | |
7ddc80a4 AS |
3961 | if (subprog[idx].is_async_cb) { |
3962 | if (subprog[idx].has_tail_call) { | |
3963 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); | |
3964 | return -EFAULT; | |
3965 | } | |
3966 | /* async callbacks don't increase bpf prog stack size */ | |
3967 | continue; | |
3968 | } | |
3969 | i = next_insn; | |
ebf7d1f5 MF |
3970 | |
3971 | if (subprog[idx].has_tail_call) | |
3972 | tail_call_reachable = true; | |
3973 | ||
70a87ffe AS |
3974 | frame++; |
3975 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
3976 | verbose(env, "the call stack of %d frames is too deep !\n", |
3977 | frame); | |
3978 | return -E2BIG; | |
70a87ffe AS |
3979 | } |
3980 | goto process_func; | |
3981 | } | |
ebf7d1f5 MF |
3982 | /* if tail call got detected across bpf2bpf calls then mark each of the |
3983 | * currently present subprog frames as tail call reachable subprogs; | |
3984 | * this info will be utilized by JIT so that we will be preserving the | |
3985 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
3986 | */ | |
3987 | if (tail_call_reachable) | |
3988 | for (j = 0; j < frame; j++) | |
3989 | subprog[ret_prog[j]].tail_call_reachable = true; | |
5dd0a6b8 DB |
3990 | if (subprog[0].tail_call_reachable) |
3991 | env->prog->aux->tail_call_reachable = true; | |
ebf7d1f5 | 3992 | |
70a87ffe AS |
3993 | /* end of for() loop means the last insn of the 'subprog' |
3994 | * was reached. Doesn't matter whether it was JA or EXIT | |
3995 | */ | |
3996 | if (frame == 0) | |
3997 | return 0; | |
9c8105bd | 3998 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
3999 | frame--; |
4000 | i = ret_insn[frame]; | |
9c8105bd | 4001 | idx = ret_prog[frame]; |
70a87ffe | 4002 | goto continue_func; |
f4d7e40a AS |
4003 | } |
4004 | ||
19d28fbd | 4005 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
4006 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
4007 | const struct bpf_insn *insn, int idx) | |
4008 | { | |
4009 | int start = idx + insn->imm + 1, subprog; | |
4010 | ||
4011 | subprog = find_subprog(env, start); | |
4012 | if (subprog < 0) { | |
4013 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
4014 | start); | |
4015 | return -EFAULT; | |
4016 | } | |
9c8105bd | 4017 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 4018 | } |
19d28fbd | 4019 | #endif |
1ea47e01 | 4020 | |
afbf21dc YS |
4021 | static int __check_buffer_access(struct bpf_verifier_env *env, |
4022 | const char *buf_info, | |
4023 | const struct bpf_reg_state *reg, | |
4024 | int regno, int off, int size) | |
9df1c28b MM |
4025 | { |
4026 | if (off < 0) { | |
4027 | verbose(env, | |
4fc00b79 | 4028 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 4029 | regno, buf_info, off, size); |
9df1c28b MM |
4030 | return -EACCES; |
4031 | } | |
4032 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4033 | char tn_buf[48]; | |
4034 | ||
4035 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4036 | verbose(env, | |
4fc00b79 | 4037 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
4038 | regno, off, tn_buf); |
4039 | return -EACCES; | |
4040 | } | |
afbf21dc YS |
4041 | |
4042 | return 0; | |
4043 | } | |
4044 | ||
4045 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
4046 | const struct bpf_reg_state *reg, | |
4047 | int regno, int off, int size) | |
4048 | { | |
4049 | int err; | |
4050 | ||
4051 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
4052 | if (err) | |
4053 | return err; | |
4054 | ||
9df1c28b MM |
4055 | if (off + size > env->prog->aux->max_tp_access) |
4056 | env->prog->aux->max_tp_access = off + size; | |
4057 | ||
4058 | return 0; | |
4059 | } | |
4060 | ||
afbf21dc YS |
4061 | static int check_buffer_access(struct bpf_verifier_env *env, |
4062 | const struct bpf_reg_state *reg, | |
4063 | int regno, int off, int size, | |
4064 | bool zero_size_allowed, | |
afbf21dc YS |
4065 | u32 *max_access) |
4066 | { | |
44e9a741 | 4067 | const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; |
afbf21dc YS |
4068 | int err; |
4069 | ||
4070 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
4071 | if (err) | |
4072 | return err; | |
4073 | ||
4074 | if (off + size > *max_access) | |
4075 | *max_access = off + size; | |
4076 | ||
4077 | return 0; | |
4078 | } | |
4079 | ||
3f50f132 JF |
4080 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
4081 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
4082 | { | |
4083 | reg->var_off = tnum_subreg(reg->var_off); | |
4084 | __reg_assign_32_into_64(reg); | |
4085 | } | |
9df1c28b | 4086 | |
0c17d1d2 JH |
4087 | /* truncate register to smaller size (in bytes) |
4088 | * must be called with size < BPF_REG_SIZE | |
4089 | */ | |
4090 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
4091 | { | |
4092 | u64 mask; | |
4093 | ||
4094 | /* clear high bits in bit representation */ | |
4095 | reg->var_off = tnum_cast(reg->var_off, size); | |
4096 | ||
4097 | /* fix arithmetic bounds */ | |
4098 | mask = ((u64)1 << (size * 8)) - 1; | |
4099 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
4100 | reg->umin_value &= mask; | |
4101 | reg->umax_value &= mask; | |
4102 | } else { | |
4103 | reg->umin_value = 0; | |
4104 | reg->umax_value = mask; | |
4105 | } | |
4106 | reg->smin_value = reg->umin_value; | |
4107 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
4108 | |
4109 | /* If size is smaller than 32bit register the 32bit register | |
4110 | * values are also truncated so we push 64-bit bounds into | |
4111 | * 32-bit bounds. Above were truncated < 32-bits already. | |
4112 | */ | |
4113 | if (size >= 4) | |
4114 | return; | |
4115 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
4116 | } |
4117 | ||
a23740ec AN |
4118 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
4119 | { | |
353050be DB |
4120 | /* A map is considered read-only if the following condition are true: |
4121 | * | |
4122 | * 1) BPF program side cannot change any of the map content. The | |
4123 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map | |
4124 | * and was set at map creation time. | |
4125 | * 2) The map value(s) have been initialized from user space by a | |
4126 | * loader and then "frozen", such that no new map update/delete | |
4127 | * operations from syscall side are possible for the rest of | |
4128 | * the map's lifetime from that point onwards. | |
4129 | * 3) Any parallel/pending map update/delete operations from syscall | |
4130 | * side have been completed. Only after that point, it's safe to | |
4131 | * assume that map value(s) are immutable. | |
4132 | */ | |
4133 | return (map->map_flags & BPF_F_RDONLY_PROG) && | |
4134 | READ_ONCE(map->frozen) && | |
4135 | !bpf_map_write_active(map); | |
a23740ec AN |
4136 | } |
4137 | ||
4138 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
4139 | { | |
4140 | void *ptr; | |
4141 | u64 addr; | |
4142 | int err; | |
4143 | ||
4144 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
4145 | if (err) | |
4146 | return err; | |
2dedd7d2 | 4147 | ptr = (void *)(long)addr + off; |
a23740ec AN |
4148 | |
4149 | switch (size) { | |
4150 | case sizeof(u8): | |
4151 | *val = (u64)*(u8 *)ptr; | |
4152 | break; | |
4153 | case sizeof(u16): | |
4154 | *val = (u64)*(u16 *)ptr; | |
4155 | break; | |
4156 | case sizeof(u32): | |
4157 | *val = (u64)*(u32 *)ptr; | |
4158 | break; | |
4159 | case sizeof(u64): | |
4160 | *val = *(u64 *)ptr; | |
4161 | break; | |
4162 | default: | |
4163 | return -EINVAL; | |
4164 | } | |
4165 | return 0; | |
4166 | } | |
4167 | ||
9e15db66 AS |
4168 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
4169 | struct bpf_reg_state *regs, | |
4170 | int regno, int off, int size, | |
4171 | enum bpf_access_type atype, | |
4172 | int value_regno) | |
4173 | { | |
4174 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
4175 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
4176 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
c6f1bfe8 | 4177 | enum bpf_type_flag flag = 0; |
9e15db66 AS |
4178 | u32 btf_id; |
4179 | int ret; | |
4180 | ||
9e15db66 AS |
4181 | if (off < 0) { |
4182 | verbose(env, | |
4183 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
4184 | regno, tname, off); | |
4185 | return -EACCES; | |
4186 | } | |
4187 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4188 | char tn_buf[48]; | |
4189 | ||
4190 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4191 | verbose(env, | |
4192 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
4193 | regno, tname, off, tn_buf); | |
4194 | return -EACCES; | |
4195 | } | |
4196 | ||
c6f1bfe8 YS |
4197 | if (reg->type & MEM_USER) { |
4198 | verbose(env, | |
4199 | "R%d is ptr_%s access user memory: off=%d\n", | |
4200 | regno, tname, off); | |
4201 | return -EACCES; | |
4202 | } | |
4203 | ||
5844101a HL |
4204 | if (reg->type & MEM_PERCPU) { |
4205 | verbose(env, | |
4206 | "R%d is ptr_%s access percpu memory: off=%d\n", | |
4207 | regno, tname, off); | |
4208 | return -EACCES; | |
4209 | } | |
4210 | ||
27ae7997 | 4211 | if (env->ops->btf_struct_access) { |
22dc4a0f | 4212 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
c6f1bfe8 | 4213 | off, size, atype, &btf_id, &flag); |
27ae7997 MKL |
4214 | } else { |
4215 | if (atype != BPF_READ) { | |
4216 | verbose(env, "only read is supported\n"); | |
4217 | return -EACCES; | |
4218 | } | |
4219 | ||
22dc4a0f | 4220 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
c6f1bfe8 | 4221 | atype, &btf_id, &flag); |
27ae7997 MKL |
4222 | } |
4223 | ||
9e15db66 AS |
4224 | if (ret < 0) |
4225 | return ret; | |
4226 | ||
41c48f3a | 4227 | if (atype == BPF_READ && value_regno >= 0) |
c6f1bfe8 | 4228 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); |
41c48f3a AI |
4229 | |
4230 | return 0; | |
4231 | } | |
4232 | ||
4233 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
4234 | struct bpf_reg_state *regs, | |
4235 | int regno, int off, int size, | |
4236 | enum bpf_access_type atype, | |
4237 | int value_regno) | |
4238 | { | |
4239 | struct bpf_reg_state *reg = regs + regno; | |
4240 | struct bpf_map *map = reg->map_ptr; | |
c6f1bfe8 | 4241 | enum bpf_type_flag flag = 0; |
41c48f3a AI |
4242 | const struct btf_type *t; |
4243 | const char *tname; | |
4244 | u32 btf_id; | |
4245 | int ret; | |
4246 | ||
4247 | if (!btf_vmlinux) { | |
4248 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
4249 | return -ENOTSUPP; | |
4250 | } | |
4251 | ||
4252 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
4253 | verbose(env, "map_ptr access not supported for map type %d\n", | |
4254 | map->map_type); | |
4255 | return -ENOTSUPP; | |
4256 | } | |
4257 | ||
4258 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
4259 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
4260 | ||
4261 | if (!env->allow_ptr_to_map_access) { | |
4262 | verbose(env, | |
4263 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
4264 | tname); | |
4265 | return -EPERM; | |
9e15db66 | 4266 | } |
27ae7997 | 4267 | |
41c48f3a AI |
4268 | if (off < 0) { |
4269 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
4270 | regno, tname, off); | |
4271 | return -EACCES; | |
4272 | } | |
4273 | ||
4274 | if (atype != BPF_READ) { | |
4275 | verbose(env, "only read from %s is supported\n", tname); | |
4276 | return -EACCES; | |
4277 | } | |
4278 | ||
c6f1bfe8 | 4279 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id, &flag); |
41c48f3a AI |
4280 | if (ret < 0) |
4281 | return ret; | |
4282 | ||
4283 | if (value_regno >= 0) | |
c6f1bfe8 | 4284 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id, flag); |
41c48f3a | 4285 | |
9e15db66 AS |
4286 | return 0; |
4287 | } | |
4288 | ||
01f810ac AM |
4289 | /* Check that the stack access at the given offset is within bounds. The |
4290 | * maximum valid offset is -1. | |
4291 | * | |
4292 | * The minimum valid offset is -MAX_BPF_STACK for writes, and | |
4293 | * -state->allocated_stack for reads. | |
4294 | */ | |
4295 | static int check_stack_slot_within_bounds(int off, | |
4296 | struct bpf_func_state *state, | |
4297 | enum bpf_access_type t) | |
4298 | { | |
4299 | int min_valid_off; | |
4300 | ||
4301 | if (t == BPF_WRITE) | |
4302 | min_valid_off = -MAX_BPF_STACK; | |
4303 | else | |
4304 | min_valid_off = -state->allocated_stack; | |
4305 | ||
4306 | if (off < min_valid_off || off > -1) | |
4307 | return -EACCES; | |
4308 | return 0; | |
4309 | } | |
4310 | ||
4311 | /* Check that the stack access at 'regno + off' falls within the maximum stack | |
4312 | * bounds. | |
4313 | * | |
4314 | * 'off' includes `regno->offset`, but not its dynamic part (if any). | |
4315 | */ | |
4316 | static int check_stack_access_within_bounds( | |
4317 | struct bpf_verifier_env *env, | |
4318 | int regno, int off, int access_size, | |
4319 | enum stack_access_src src, enum bpf_access_type type) | |
4320 | { | |
4321 | struct bpf_reg_state *regs = cur_regs(env); | |
4322 | struct bpf_reg_state *reg = regs + regno; | |
4323 | struct bpf_func_state *state = func(env, reg); | |
4324 | int min_off, max_off; | |
4325 | int err; | |
4326 | char *err_extra; | |
4327 | ||
4328 | if (src == ACCESS_HELPER) | |
4329 | /* We don't know if helpers are reading or writing (or both). */ | |
4330 | err_extra = " indirect access to"; | |
4331 | else if (type == BPF_READ) | |
4332 | err_extra = " read from"; | |
4333 | else | |
4334 | err_extra = " write to"; | |
4335 | ||
4336 | if (tnum_is_const(reg->var_off)) { | |
4337 | min_off = reg->var_off.value + off; | |
4338 | if (access_size > 0) | |
4339 | max_off = min_off + access_size - 1; | |
4340 | else | |
4341 | max_off = min_off; | |
4342 | } else { | |
4343 | if (reg->smax_value >= BPF_MAX_VAR_OFF || | |
4344 | reg->smin_value <= -BPF_MAX_VAR_OFF) { | |
4345 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", | |
4346 | err_extra, regno); | |
4347 | return -EACCES; | |
4348 | } | |
4349 | min_off = reg->smin_value + off; | |
4350 | if (access_size > 0) | |
4351 | max_off = reg->smax_value + off + access_size - 1; | |
4352 | else | |
4353 | max_off = min_off; | |
4354 | } | |
4355 | ||
4356 | err = check_stack_slot_within_bounds(min_off, state, type); | |
4357 | if (!err) | |
4358 | err = check_stack_slot_within_bounds(max_off, state, type); | |
4359 | ||
4360 | if (err) { | |
4361 | if (tnum_is_const(reg->var_off)) { | |
4362 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", | |
4363 | err_extra, regno, off, access_size); | |
4364 | } else { | |
4365 | char tn_buf[48]; | |
4366 | ||
4367 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
4368 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", | |
4369 | err_extra, regno, tn_buf, access_size); | |
4370 | } | |
4371 | } | |
4372 | return err; | |
4373 | } | |
41c48f3a | 4374 | |
17a52670 AS |
4375 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
4376 | * if t==write, value_regno is a register which value is stored into memory | |
4377 | * if t==read, value_regno is a register which will receive the value from memory | |
4378 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
4379 | * if t==read && value_regno==-1, don't care what we read from memory | |
4380 | */ | |
ca369602 DB |
4381 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
4382 | int off, int bpf_size, enum bpf_access_type t, | |
4383 | int value_regno, bool strict_alignment_once) | |
17a52670 | 4384 | { |
638f5b90 AS |
4385 | struct bpf_reg_state *regs = cur_regs(env); |
4386 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 4387 | struct bpf_func_state *state; |
17a52670 AS |
4388 | int size, err = 0; |
4389 | ||
4390 | size = bpf_size_to_bytes(bpf_size); | |
4391 | if (size < 0) | |
4392 | return size; | |
4393 | ||
f1174f77 | 4394 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 4395 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
4396 | if (err) |
4397 | return err; | |
17a52670 | 4398 | |
f1174f77 EC |
4399 | /* for access checks, reg->off is just part of off */ |
4400 | off += reg->off; | |
4401 | ||
69c087ba YS |
4402 | if (reg->type == PTR_TO_MAP_KEY) { |
4403 | if (t == BPF_WRITE) { | |
4404 | verbose(env, "write to change key R%d not allowed\n", regno); | |
4405 | return -EACCES; | |
4406 | } | |
4407 | ||
4408 | err = check_mem_region_access(env, regno, off, size, | |
4409 | reg->map_ptr->key_size, false); | |
4410 | if (err) | |
4411 | return err; | |
4412 | if (value_regno >= 0) | |
4413 | mark_reg_unknown(env, regs, value_regno); | |
4414 | } else if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
4415 | if (t == BPF_WRITE && value_regno >= 0 && |
4416 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4417 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
4418 | return -EACCES; |
4419 | } | |
591fe988 DB |
4420 | err = check_map_access_type(env, regno, off, size, t); |
4421 | if (err) | |
4422 | return err; | |
9fd29c08 | 4423 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
4424 | if (!err && t == BPF_READ && value_regno >= 0) { |
4425 | struct bpf_map *map = reg->map_ptr; | |
4426 | ||
4427 | /* if map is read-only, track its contents as scalars */ | |
4428 | if (tnum_is_const(reg->var_off) && | |
4429 | bpf_map_is_rdonly(map) && | |
4430 | map->ops->map_direct_value_addr) { | |
4431 | int map_off = off + reg->var_off.value; | |
4432 | u64 val = 0; | |
4433 | ||
4434 | err = bpf_map_direct_read(map, map_off, size, | |
4435 | &val); | |
4436 | if (err) | |
4437 | return err; | |
4438 | ||
4439 | regs[value_regno].type = SCALAR_VALUE; | |
4440 | __mark_reg_known(®s[value_regno], val); | |
4441 | } else { | |
4442 | mark_reg_unknown(env, regs, value_regno); | |
4443 | } | |
4444 | } | |
34d3a78c HL |
4445 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
4446 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
4447 | ||
4448 | if (type_may_be_null(reg->type)) { | |
4449 | verbose(env, "R%d invalid mem access '%s'\n", regno, | |
4450 | reg_type_str(env, reg->type)); | |
4451 | return -EACCES; | |
4452 | } | |
4453 | ||
4454 | if (t == BPF_WRITE && rdonly_mem) { | |
4455 | verbose(env, "R%d cannot write into %s\n", | |
4456 | regno, reg_type_str(env, reg->type)); | |
4457 | return -EACCES; | |
4458 | } | |
4459 | ||
457f4436 AN |
4460 | if (t == BPF_WRITE && value_regno >= 0 && |
4461 | is_pointer_value(env, value_regno)) { | |
4462 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
4463 | return -EACCES; | |
4464 | } | |
34d3a78c | 4465 | |
457f4436 AN |
4466 | err = check_mem_region_access(env, regno, off, size, |
4467 | reg->mem_size, false); | |
34d3a78c | 4468 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
457f4436 | 4469 | mark_reg_unknown(env, regs, value_regno); |
1a0dc1ac | 4470 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 4471 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 4472 | struct btf *btf = NULL; |
9e15db66 | 4473 | u32 btf_id = 0; |
19de99f7 | 4474 | |
1be7f75d AS |
4475 | if (t == BPF_WRITE && value_regno >= 0 && |
4476 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 4477 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
4478 | return -EACCES; |
4479 | } | |
f1174f77 | 4480 | |
be80a1d3 | 4481 | err = check_ptr_off_reg(env, reg, regno); |
58990d1f DB |
4482 | if (err < 0) |
4483 | return err; | |
4484 | ||
c6f1bfe8 YS |
4485 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, |
4486 | &btf_id); | |
9e15db66 AS |
4487 | if (err) |
4488 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 4489 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 4490 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
4491 | * PTR_TO_PACKET[_META,_END]. In the latter |
4492 | * case, we know the offset is zero. | |
f1174f77 | 4493 | */ |
46f8bc92 | 4494 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 4495 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 4496 | } else { |
638f5b90 | 4497 | mark_reg_known_zero(env, regs, |
61bd5218 | 4498 | value_regno); |
c25b2ae1 | 4499 | if (type_may_be_null(reg_type)) |
46f8bc92 | 4500 | regs[value_regno].id = ++env->id_gen; |
5327ed3d JW |
4501 | /* A load of ctx field could have different |
4502 | * actual load size with the one encoded in the | |
4503 | * insn. When the dst is PTR, it is for sure not | |
4504 | * a sub-register. | |
4505 | */ | |
4506 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
c25b2ae1 | 4507 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
22dc4a0f | 4508 | regs[value_regno].btf = btf; |
9e15db66 | 4509 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 4510 | } |
46f8bc92 | 4511 | } |
638f5b90 | 4512 | regs[value_regno].type = reg_type; |
969bf05e | 4513 | } |
17a52670 | 4514 | |
f1174f77 | 4515 | } else if (reg->type == PTR_TO_STACK) { |
01f810ac AM |
4516 | /* Basic bounds checks. */ |
4517 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); | |
e4298d25 DB |
4518 | if (err) |
4519 | return err; | |
8726679a | 4520 | |
f4d7e40a AS |
4521 | state = func(env, reg); |
4522 | err = update_stack_depth(env, state, off); | |
4523 | if (err) | |
4524 | return err; | |
8726679a | 4525 | |
01f810ac AM |
4526 | if (t == BPF_READ) |
4527 | err = check_stack_read(env, regno, off, size, | |
61bd5218 | 4528 | value_regno); |
01f810ac AM |
4529 | else |
4530 | err = check_stack_write(env, regno, off, size, | |
4531 | value_regno, insn_idx); | |
de8f3a83 | 4532 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 4533 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 4534 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
4535 | return -EACCES; |
4536 | } | |
4acf6c0b BB |
4537 | if (t == BPF_WRITE && value_regno >= 0 && |
4538 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
4539 | verbose(env, "R%d leaks addr into packet\n", |
4540 | value_regno); | |
4acf6c0b BB |
4541 | return -EACCES; |
4542 | } | |
9fd29c08 | 4543 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 4544 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 4545 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
4546 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
4547 | if (t == BPF_WRITE && value_regno >= 0 && | |
4548 | is_pointer_value(env, value_regno)) { | |
4549 | verbose(env, "R%d leaks addr into flow keys\n", | |
4550 | value_regno); | |
4551 | return -EACCES; | |
4552 | } | |
4553 | ||
4554 | err = check_flow_keys_access(env, off, size); | |
4555 | if (!err && t == BPF_READ && value_regno >= 0) | |
4556 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 4557 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 4558 | if (t == BPF_WRITE) { |
46f8bc92 | 4559 | verbose(env, "R%d cannot write into %s\n", |
c25b2ae1 | 4560 | regno, reg_type_str(env, reg->type)); |
c64b7983 JS |
4561 | return -EACCES; |
4562 | } | |
5f456649 | 4563 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
4564 | if (!err && value_regno >= 0) |
4565 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
4566 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
4567 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
4568 | if (!err && t == BPF_READ && value_regno >= 0) | |
4569 | mark_reg_unknown(env, regs, value_regno); | |
bff61f6f HL |
4570 | } else if (base_type(reg->type) == PTR_TO_BTF_ID && |
4571 | !type_may_be_null(reg->type)) { | |
9e15db66 AS |
4572 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
4573 | value_regno); | |
41c48f3a AI |
4574 | } else if (reg->type == CONST_PTR_TO_MAP) { |
4575 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
4576 | value_regno); | |
20b2aff4 HL |
4577 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
4578 | bool rdonly_mem = type_is_rdonly_mem(reg->type); | |
20b2aff4 HL |
4579 | u32 *max_access; |
4580 | ||
4581 | if (rdonly_mem) { | |
4582 | if (t == BPF_WRITE) { | |
4583 | verbose(env, "R%d cannot write into %s\n", | |
4584 | regno, reg_type_str(env, reg->type)); | |
4585 | return -EACCES; | |
4586 | } | |
20b2aff4 HL |
4587 | max_access = &env->prog->aux->max_rdonly_access; |
4588 | } else { | |
20b2aff4 | 4589 | max_access = &env->prog->aux->max_rdwr_access; |
afbf21dc | 4590 | } |
20b2aff4 | 4591 | |
f6dfbe31 | 4592 | err = check_buffer_access(env, reg, regno, off, size, false, |
44e9a741 | 4593 | max_access); |
20b2aff4 HL |
4594 | |
4595 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) | |
afbf21dc | 4596 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 4597 | } else { |
61bd5218 | 4598 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
c25b2ae1 | 4599 | reg_type_str(env, reg->type)); |
17a52670 AS |
4600 | return -EACCES; |
4601 | } | |
969bf05e | 4602 | |
f1174f77 | 4603 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 4604 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 4605 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 4606 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 4607 | } |
17a52670 AS |
4608 | return err; |
4609 | } | |
4610 | ||
91c960b0 | 4611 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 4612 | { |
5ffa2550 | 4613 | int load_reg; |
17a52670 AS |
4614 | int err; |
4615 | ||
5ca419f2 BJ |
4616 | switch (insn->imm) { |
4617 | case BPF_ADD: | |
4618 | case BPF_ADD | BPF_FETCH: | |
981f94c3 BJ |
4619 | case BPF_AND: |
4620 | case BPF_AND | BPF_FETCH: | |
4621 | case BPF_OR: | |
4622 | case BPF_OR | BPF_FETCH: | |
4623 | case BPF_XOR: | |
4624 | case BPF_XOR | BPF_FETCH: | |
5ffa2550 BJ |
4625 | case BPF_XCHG: |
4626 | case BPF_CMPXCHG: | |
5ca419f2 BJ |
4627 | break; |
4628 | default: | |
91c960b0 BJ |
4629 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
4630 | return -EINVAL; | |
4631 | } | |
4632 | ||
4633 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
4634 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
4635 | return -EINVAL; |
4636 | } | |
4637 | ||
4638 | /* check src1 operand */ | |
dc503a8a | 4639 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4640 | if (err) |
4641 | return err; | |
4642 | ||
4643 | /* check src2 operand */ | |
dc503a8a | 4644 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4645 | if (err) |
4646 | return err; | |
4647 | ||
5ffa2550 BJ |
4648 | if (insn->imm == BPF_CMPXCHG) { |
4649 | /* Check comparison of R0 with memory location */ | |
a82fe085 DB |
4650 | const u32 aux_reg = BPF_REG_0; |
4651 | ||
4652 | err = check_reg_arg(env, aux_reg, SRC_OP); | |
5ffa2550 BJ |
4653 | if (err) |
4654 | return err; | |
a82fe085 DB |
4655 | |
4656 | if (is_pointer_value(env, aux_reg)) { | |
4657 | verbose(env, "R%d leaks addr into mem\n", aux_reg); | |
4658 | return -EACCES; | |
4659 | } | |
5ffa2550 BJ |
4660 | } |
4661 | ||
6bdf6abc | 4662 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 4663 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
4664 | return -EACCES; |
4665 | } | |
4666 | ||
ca369602 | 4667 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 4668 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
4669 | is_flow_key_reg(env, insn->dst_reg) || |
4670 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 4671 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f | 4672 | insn->dst_reg, |
c25b2ae1 | 4673 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
4674 | return -EACCES; |
4675 | } | |
4676 | ||
37086bfd BJ |
4677 | if (insn->imm & BPF_FETCH) { |
4678 | if (insn->imm == BPF_CMPXCHG) | |
4679 | load_reg = BPF_REG_0; | |
4680 | else | |
4681 | load_reg = insn->src_reg; | |
4682 | ||
4683 | /* check and record load of old value */ | |
4684 | err = check_reg_arg(env, load_reg, DST_OP); | |
4685 | if (err) | |
4686 | return err; | |
4687 | } else { | |
4688 | /* This instruction accesses a memory location but doesn't | |
4689 | * actually load it into a register. | |
4690 | */ | |
4691 | load_reg = -1; | |
4692 | } | |
4693 | ||
7d3baf0a DB |
4694 | /* Check whether we can read the memory, with second call for fetch |
4695 | * case to simulate the register fill. | |
4696 | */ | |
31fd8581 | 4697 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
7d3baf0a DB |
4698 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
4699 | if (!err && load_reg >= 0) | |
4700 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, | |
4701 | BPF_SIZE(insn->code), BPF_READ, load_reg, | |
4702 | true); | |
17a52670 AS |
4703 | if (err) |
4704 | return err; | |
4705 | ||
7d3baf0a | 4706 | /* Check whether we can write into the same memory. */ |
5ca419f2 BJ |
4707 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
4708 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); | |
4709 | if (err) | |
4710 | return err; | |
4711 | ||
5ca419f2 | 4712 | return 0; |
17a52670 AS |
4713 | } |
4714 | ||
01f810ac AM |
4715 | /* When register 'regno' is used to read the stack (either directly or through |
4716 | * a helper function) make sure that it's within stack boundary and, depending | |
4717 | * on the access type, that all elements of the stack are initialized. | |
4718 | * | |
4719 | * 'off' includes 'regno->off', but not its dynamic part (if any). | |
4720 | * | |
4721 | * All registers that have been spilled on the stack in the slots within the | |
4722 | * read offsets are marked as read. | |
4723 | */ | |
4724 | static int check_stack_range_initialized( | |
4725 | struct bpf_verifier_env *env, int regno, int off, | |
4726 | int access_size, bool zero_size_allowed, | |
4727 | enum stack_access_src type, struct bpf_call_arg_meta *meta) | |
2011fccf AI |
4728 | { |
4729 | struct bpf_reg_state *reg = reg_state(env, regno); | |
01f810ac AM |
4730 | struct bpf_func_state *state = func(env, reg); |
4731 | int err, min_off, max_off, i, j, slot, spi; | |
4732 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; | |
4733 | enum bpf_access_type bounds_check_type; | |
4734 | /* Some accesses can write anything into the stack, others are | |
4735 | * read-only. | |
4736 | */ | |
4737 | bool clobber = false; | |
2011fccf | 4738 | |
01f810ac AM |
4739 | if (access_size == 0 && !zero_size_allowed) { |
4740 | verbose(env, "invalid zero-sized read\n"); | |
2011fccf AI |
4741 | return -EACCES; |
4742 | } | |
2011fccf | 4743 | |
01f810ac AM |
4744 | if (type == ACCESS_HELPER) { |
4745 | /* The bounds checks for writes are more permissive than for | |
4746 | * reads. However, if raw_mode is not set, we'll do extra | |
4747 | * checks below. | |
4748 | */ | |
4749 | bounds_check_type = BPF_WRITE; | |
4750 | clobber = true; | |
4751 | } else { | |
4752 | bounds_check_type = BPF_READ; | |
4753 | } | |
4754 | err = check_stack_access_within_bounds(env, regno, off, access_size, | |
4755 | type, bounds_check_type); | |
4756 | if (err) | |
4757 | return err; | |
4758 | ||
17a52670 | 4759 | |
2011fccf | 4760 | if (tnum_is_const(reg->var_off)) { |
01f810ac | 4761 | min_off = max_off = reg->var_off.value + off; |
2011fccf | 4762 | } else { |
088ec26d AI |
4763 | /* Variable offset is prohibited for unprivileged mode for |
4764 | * simplicity since it requires corresponding support in | |
4765 | * Spectre masking for stack ALU. | |
4766 | * See also retrieve_ptr_limit(). | |
4767 | */ | |
2c78ee89 | 4768 | if (!env->bypass_spec_v1) { |
088ec26d | 4769 | char tn_buf[48]; |
f1174f77 | 4770 | |
088ec26d | 4771 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
01f810ac AM |
4772 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
4773 | regno, err_extra, tn_buf); | |
088ec26d AI |
4774 | return -EACCES; |
4775 | } | |
f2bcd05e AI |
4776 | /* Only initialized buffer on stack is allowed to be accessed |
4777 | * with variable offset. With uninitialized buffer it's hard to | |
4778 | * guarantee that whole memory is marked as initialized on | |
4779 | * helper return since specific bounds are unknown what may | |
4780 | * cause uninitialized stack leaking. | |
4781 | */ | |
4782 | if (meta && meta->raw_mode) | |
4783 | meta = NULL; | |
4784 | ||
01f810ac AM |
4785 | min_off = reg->smin_value + off; |
4786 | max_off = reg->smax_value + off; | |
17a52670 AS |
4787 | } |
4788 | ||
435faee1 DB |
4789 | if (meta && meta->raw_mode) { |
4790 | meta->access_size = access_size; | |
4791 | meta->regno = regno; | |
4792 | return 0; | |
4793 | } | |
4794 | ||
2011fccf | 4795 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
4796 | u8 *stype; |
4797 | ||
2011fccf | 4798 | slot = -i - 1; |
638f5b90 | 4799 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
4800 | if (state->allocated_stack <= slot) |
4801 | goto err; | |
4802 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
4803 | if (*stype == STACK_MISC) | |
4804 | goto mark; | |
4805 | if (*stype == STACK_ZERO) { | |
01f810ac AM |
4806 | if (clobber) { |
4807 | /* helper can write anything into the stack */ | |
4808 | *stype = STACK_MISC; | |
4809 | } | |
cc2b14d5 | 4810 | goto mark; |
17a52670 | 4811 | } |
1d68f22b | 4812 | |
27113c59 | 4813 | if (is_spilled_reg(&state->stack[spi]) && |
5844101a | 4814 | base_type(state->stack[spi].spilled_ptr.type) == PTR_TO_BTF_ID) |
1d68f22b YS |
4815 | goto mark; |
4816 | ||
27113c59 | 4817 | if (is_spilled_reg(&state->stack[spi]) && |
cd17d38f YS |
4818 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
4819 | env->allow_ptr_leaks)) { | |
01f810ac AM |
4820 | if (clobber) { |
4821 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); | |
4822 | for (j = 0; j < BPF_REG_SIZE; j++) | |
354e8f19 | 4823 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
01f810ac | 4824 | } |
f7cf25b2 AS |
4825 | goto mark; |
4826 | } | |
4827 | ||
cc2b14d5 | 4828 | err: |
2011fccf | 4829 | if (tnum_is_const(reg->var_off)) { |
01f810ac AM |
4830 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
4831 | err_extra, regno, min_off, i - min_off, access_size); | |
2011fccf AI |
4832 | } else { |
4833 | char tn_buf[48]; | |
4834 | ||
4835 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
01f810ac AM |
4836 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
4837 | err_extra, regno, tn_buf, i - min_off, access_size); | |
2011fccf | 4838 | } |
cc2b14d5 AS |
4839 | return -EACCES; |
4840 | mark: | |
4841 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
4842 | * the whole slot to be marked as 'read' | |
4843 | */ | |
679c782d | 4844 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
4845 | state->stack[spi].spilled_ptr.parent, |
4846 | REG_LIVE_READ64); | |
17a52670 | 4847 | } |
2011fccf | 4848 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
4849 | } |
4850 | ||
06c1c049 GB |
4851 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
4852 | int access_size, bool zero_size_allowed, | |
4853 | struct bpf_call_arg_meta *meta) | |
4854 | { | |
638f5b90 | 4855 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
20b2aff4 | 4856 | u32 *max_access; |
06c1c049 | 4857 | |
20b2aff4 | 4858 | switch (base_type(reg->type)) { |
06c1c049 | 4859 | case PTR_TO_PACKET: |
de8f3a83 | 4860 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
4861 | return check_packet_access(env, regno, reg->off, access_size, |
4862 | zero_size_allowed); | |
69c087ba | 4863 | case PTR_TO_MAP_KEY: |
7b3552d3 KKD |
4864 | if (meta && meta->raw_mode) { |
4865 | verbose(env, "R%d cannot write into %s\n", regno, | |
4866 | reg_type_str(env, reg->type)); | |
4867 | return -EACCES; | |
4868 | } | |
69c087ba YS |
4869 | return check_mem_region_access(env, regno, reg->off, access_size, |
4870 | reg->map_ptr->key_size, false); | |
06c1c049 | 4871 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
4872 | if (check_map_access_type(env, regno, reg->off, access_size, |
4873 | meta && meta->raw_mode ? BPF_WRITE : | |
4874 | BPF_READ)) | |
4875 | return -EACCES; | |
9fd29c08 YS |
4876 | return check_map_access(env, regno, reg->off, access_size, |
4877 | zero_size_allowed); | |
457f4436 | 4878 | case PTR_TO_MEM: |
97e6d7da KKD |
4879 | if (type_is_rdonly_mem(reg->type)) { |
4880 | if (meta && meta->raw_mode) { | |
4881 | verbose(env, "R%d cannot write into %s\n", regno, | |
4882 | reg_type_str(env, reg->type)); | |
4883 | return -EACCES; | |
4884 | } | |
4885 | } | |
457f4436 AN |
4886 | return check_mem_region_access(env, regno, reg->off, |
4887 | access_size, reg->mem_size, | |
4888 | zero_size_allowed); | |
20b2aff4 HL |
4889 | case PTR_TO_BUF: |
4890 | if (type_is_rdonly_mem(reg->type)) { | |
97e6d7da KKD |
4891 | if (meta && meta->raw_mode) { |
4892 | verbose(env, "R%d cannot write into %s\n", regno, | |
4893 | reg_type_str(env, reg->type)); | |
20b2aff4 | 4894 | return -EACCES; |
97e6d7da | 4895 | } |
20b2aff4 | 4896 | |
20b2aff4 HL |
4897 | max_access = &env->prog->aux->max_rdonly_access; |
4898 | } else { | |
20b2aff4 HL |
4899 | max_access = &env->prog->aux->max_rdwr_access; |
4900 | } | |
afbf21dc YS |
4901 | return check_buffer_access(env, reg, regno, reg->off, |
4902 | access_size, zero_size_allowed, | |
44e9a741 | 4903 | max_access); |
0d004c02 | 4904 | case PTR_TO_STACK: |
01f810ac AM |
4905 | return check_stack_range_initialized( |
4906 | env, | |
4907 | regno, reg->off, access_size, | |
4908 | zero_size_allowed, ACCESS_HELPER, meta); | |
0d004c02 LB |
4909 | default: /* scalar_value or invalid ptr */ |
4910 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
4911 | if (zero_size_allowed && access_size == 0 && | |
4912 | register_is_null(reg)) | |
4913 | return 0; | |
4914 | ||
c25b2ae1 HL |
4915 | verbose(env, "R%d type=%s ", regno, |
4916 | reg_type_str(env, reg->type)); | |
4917 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); | |
0d004c02 | 4918 | return -EACCES; |
06c1c049 GB |
4919 | } |
4920 | } | |
4921 | ||
d583691c KKD |
4922 | static int check_mem_size_reg(struct bpf_verifier_env *env, |
4923 | struct bpf_reg_state *reg, u32 regno, | |
4924 | bool zero_size_allowed, | |
4925 | struct bpf_call_arg_meta *meta) | |
4926 | { | |
4927 | int err; | |
4928 | ||
4929 | /* This is used to refine r0 return value bounds for helpers | |
4930 | * that enforce this value as an upper bound on return values. | |
4931 | * See do_refine_retval_range() for helpers that can refine | |
4932 | * the return value. C type of helper is u32 so we pull register | |
4933 | * bound from umax_value however, if negative verifier errors | |
4934 | * out. Only upper bounds can be learned because retval is an | |
4935 | * int type and negative retvals are allowed. | |
4936 | */ | |
be77354a | 4937 | meta->msize_max_value = reg->umax_value; |
d583691c KKD |
4938 | |
4939 | /* The register is SCALAR_VALUE; the access check | |
4940 | * happens using its boundaries. | |
4941 | */ | |
4942 | if (!tnum_is_const(reg->var_off)) | |
4943 | /* For unprivileged variable accesses, disable raw | |
4944 | * mode so that the program is required to | |
4945 | * initialize all the memory that the helper could | |
4946 | * just partially fill up. | |
4947 | */ | |
4948 | meta = NULL; | |
4949 | ||
4950 | if (reg->smin_value < 0) { | |
4951 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", | |
4952 | regno); | |
4953 | return -EACCES; | |
4954 | } | |
4955 | ||
4956 | if (reg->umin_value == 0) { | |
4957 | err = check_helper_mem_access(env, regno - 1, 0, | |
4958 | zero_size_allowed, | |
4959 | meta); | |
4960 | if (err) | |
4961 | return err; | |
4962 | } | |
4963 | ||
4964 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { | |
4965 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", | |
4966 | regno); | |
4967 | return -EACCES; | |
4968 | } | |
4969 | err = check_helper_mem_access(env, regno - 1, | |
4970 | reg->umax_value, | |
4971 | zero_size_allowed, meta); | |
4972 | if (!err) | |
4973 | err = mark_chain_precision(env, regno); | |
4974 | return err; | |
4975 | } | |
4976 | ||
e5069b9c DB |
4977 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
4978 | u32 regno, u32 mem_size) | |
4979 | { | |
be77354a KKD |
4980 | bool may_be_null = type_may_be_null(reg->type); |
4981 | struct bpf_reg_state saved_reg; | |
4982 | struct bpf_call_arg_meta meta; | |
4983 | int err; | |
4984 | ||
e5069b9c DB |
4985 | if (register_is_null(reg)) |
4986 | return 0; | |
4987 | ||
be77354a KKD |
4988 | memset(&meta, 0, sizeof(meta)); |
4989 | /* Assuming that the register contains a value check if the memory | |
4990 | * access is safe. Temporarily save and restore the register's state as | |
4991 | * the conversion shouldn't be visible to a caller. | |
4992 | */ | |
4993 | if (may_be_null) { | |
4994 | saved_reg = *reg; | |
e5069b9c | 4995 | mark_ptr_not_null_reg(reg); |
e5069b9c DB |
4996 | } |
4997 | ||
be77354a KKD |
4998 | err = check_helper_mem_access(env, regno, mem_size, true, &meta); |
4999 | /* Check access for BPF_WRITE */ | |
5000 | meta.raw_mode = true; | |
5001 | err = err ?: check_helper_mem_access(env, regno, mem_size, true, &meta); | |
5002 | ||
5003 | if (may_be_null) | |
5004 | *reg = saved_reg; | |
5005 | ||
5006 | return err; | |
e5069b9c DB |
5007 | } |
5008 | ||
d583691c KKD |
5009 | int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
5010 | u32 regno) | |
5011 | { | |
5012 | struct bpf_reg_state *mem_reg = &cur_regs(env)[regno - 1]; | |
5013 | bool may_be_null = type_may_be_null(mem_reg->type); | |
5014 | struct bpf_reg_state saved_reg; | |
be77354a | 5015 | struct bpf_call_arg_meta meta; |
d583691c KKD |
5016 | int err; |
5017 | ||
5018 | WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); | |
5019 | ||
be77354a KKD |
5020 | memset(&meta, 0, sizeof(meta)); |
5021 | ||
d583691c KKD |
5022 | if (may_be_null) { |
5023 | saved_reg = *mem_reg; | |
5024 | mark_ptr_not_null_reg(mem_reg); | |
5025 | } | |
5026 | ||
be77354a KKD |
5027 | err = check_mem_size_reg(env, reg, regno, true, &meta); |
5028 | /* Check access for BPF_WRITE */ | |
5029 | meta.raw_mode = true; | |
5030 | err = err ?: check_mem_size_reg(env, reg, regno, true, &meta); | |
d583691c KKD |
5031 | |
5032 | if (may_be_null) | |
5033 | *mem_reg = saved_reg; | |
5034 | return err; | |
5035 | } | |
5036 | ||
d83525ca AS |
5037 | /* Implementation details: |
5038 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
5039 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
5040 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
5041 | * value_or_null->value transition, since the verifier only cares about | |
5042 | * the range of access to valid map value pointer and doesn't care about actual | |
5043 | * address of the map element. | |
5044 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
5045 | * reg->id > 0 after value_or_null->value transition. By doing so | |
5046 | * two bpf_map_lookups will be considered two different pointers that | |
5047 | * point to different bpf_spin_locks. | |
5048 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
5049 | * dead-locks. | |
5050 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
5051 | * reg_is_refcounted() logic. The verifier needs to remember only | |
5052 | * one spin_lock instead of array of acquired_refs. | |
5053 | * cur_state->active_spin_lock remembers which map value element got locked | |
5054 | * and clears it after bpf_spin_unlock. | |
5055 | */ | |
5056 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
5057 | bool is_lock) | |
5058 | { | |
5059 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5060 | struct bpf_verifier_state *cur = env->cur_state; | |
5061 | bool is_const = tnum_is_const(reg->var_off); | |
5062 | struct bpf_map *map = reg->map_ptr; | |
5063 | u64 val = reg->var_off.value; | |
5064 | ||
d83525ca AS |
5065 | if (!is_const) { |
5066 | verbose(env, | |
5067 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
5068 | regno); | |
5069 | return -EINVAL; | |
5070 | } | |
5071 | if (!map->btf) { | |
5072 | verbose(env, | |
5073 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
5074 | map->name); | |
5075 | return -EINVAL; | |
5076 | } | |
5077 | if (!map_value_has_spin_lock(map)) { | |
5078 | if (map->spin_lock_off == -E2BIG) | |
5079 | verbose(env, | |
5080 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
5081 | map->name); | |
5082 | else if (map->spin_lock_off == -ENOENT) | |
5083 | verbose(env, | |
5084 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
5085 | map->name); | |
5086 | else | |
5087 | verbose(env, | |
5088 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
5089 | map->name); | |
5090 | return -EINVAL; | |
5091 | } | |
5092 | if (map->spin_lock_off != val + reg->off) { | |
5093 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
5094 | val + reg->off); | |
5095 | return -EINVAL; | |
5096 | } | |
5097 | if (is_lock) { | |
5098 | if (cur->active_spin_lock) { | |
5099 | verbose(env, | |
5100 | "Locking two bpf_spin_locks are not allowed\n"); | |
5101 | return -EINVAL; | |
5102 | } | |
5103 | cur->active_spin_lock = reg->id; | |
5104 | } else { | |
5105 | if (!cur->active_spin_lock) { | |
5106 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
5107 | return -EINVAL; | |
5108 | } | |
5109 | if (cur->active_spin_lock != reg->id) { | |
5110 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
5111 | return -EINVAL; | |
5112 | } | |
5113 | cur->active_spin_lock = 0; | |
5114 | } | |
5115 | return 0; | |
5116 | } | |
5117 | ||
b00628b1 AS |
5118 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
5119 | struct bpf_call_arg_meta *meta) | |
5120 | { | |
5121 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5122 | bool is_const = tnum_is_const(reg->var_off); | |
5123 | struct bpf_map *map = reg->map_ptr; | |
5124 | u64 val = reg->var_off.value; | |
5125 | ||
5126 | if (!is_const) { | |
5127 | verbose(env, | |
5128 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", | |
5129 | regno); | |
5130 | return -EINVAL; | |
5131 | } | |
5132 | if (!map->btf) { | |
5133 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", | |
5134 | map->name); | |
5135 | return -EINVAL; | |
5136 | } | |
68134668 AS |
5137 | if (!map_value_has_timer(map)) { |
5138 | if (map->timer_off == -E2BIG) | |
5139 | verbose(env, | |
5140 | "map '%s' has more than one 'struct bpf_timer'\n", | |
5141 | map->name); | |
5142 | else if (map->timer_off == -ENOENT) | |
5143 | verbose(env, | |
5144 | "map '%s' doesn't have 'struct bpf_timer'\n", | |
5145 | map->name); | |
5146 | else | |
5147 | verbose(env, | |
5148 | "map '%s' is not a struct type or bpf_timer is mangled\n", | |
5149 | map->name); | |
5150 | return -EINVAL; | |
5151 | } | |
5152 | if (map->timer_off != val + reg->off) { | |
5153 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", | |
5154 | val + reg->off, map->timer_off); | |
b00628b1 AS |
5155 | return -EINVAL; |
5156 | } | |
5157 | if (meta->map_ptr) { | |
5158 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); | |
5159 | return -EFAULT; | |
5160 | } | |
3e8ce298 | 5161 | meta->map_uid = reg->map_uid; |
b00628b1 AS |
5162 | meta->map_ptr = map; |
5163 | return 0; | |
5164 | } | |
5165 | ||
90133415 DB |
5166 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
5167 | { | |
48946bd6 HL |
5168 | return base_type(type) == ARG_PTR_TO_MEM || |
5169 | base_type(type) == ARG_PTR_TO_UNINIT_MEM; | |
90133415 DB |
5170 | } |
5171 | ||
5172 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
5173 | { | |
5174 | return type == ARG_CONST_SIZE || | |
5175 | type == ARG_CONST_SIZE_OR_ZERO; | |
5176 | } | |
5177 | ||
457f4436 AN |
5178 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
5179 | { | |
5180 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; | |
5181 | } | |
5182 | ||
57c3bb72 AI |
5183 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
5184 | { | |
5185 | return type == ARG_PTR_TO_INT || | |
5186 | type == ARG_PTR_TO_LONG; | |
5187 | } | |
5188 | ||
5189 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
5190 | { | |
5191 | if (type == ARG_PTR_TO_INT) | |
5192 | return sizeof(u32); | |
5193 | else if (type == ARG_PTR_TO_LONG) | |
5194 | return sizeof(u64); | |
5195 | ||
5196 | return -EINVAL; | |
5197 | } | |
5198 | ||
912f442c LB |
5199 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
5200 | const struct bpf_call_arg_meta *meta, | |
5201 | enum bpf_arg_type *arg_type) | |
5202 | { | |
5203 | if (!meta->map_ptr) { | |
5204 | /* kernel subsystem misconfigured verifier */ | |
5205 | verbose(env, "invalid map_ptr to access map->type\n"); | |
5206 | return -EACCES; | |
5207 | } | |
5208 | ||
5209 | switch (meta->map_ptr->map_type) { | |
5210 | case BPF_MAP_TYPE_SOCKMAP: | |
5211 | case BPF_MAP_TYPE_SOCKHASH: | |
5212 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 5213 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
5214 | } else { |
5215 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
5216 | return -EINVAL; | |
5217 | } | |
5218 | break; | |
9330986c JK |
5219 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5220 | if (meta->func_id == BPF_FUNC_map_peek_elem) | |
5221 | *arg_type = ARG_PTR_TO_MAP_VALUE; | |
5222 | break; | |
912f442c LB |
5223 | default: |
5224 | break; | |
5225 | } | |
5226 | return 0; | |
5227 | } | |
5228 | ||
f79e7ea5 LB |
5229 | struct bpf_reg_types { |
5230 | const enum bpf_reg_type types[10]; | |
1df8f55a | 5231 | u32 *btf_id; |
f79e7ea5 LB |
5232 | }; |
5233 | ||
5234 | static const struct bpf_reg_types map_key_value_types = { | |
5235 | .types = { | |
5236 | PTR_TO_STACK, | |
5237 | PTR_TO_PACKET, | |
5238 | PTR_TO_PACKET_META, | |
69c087ba | 5239 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5240 | PTR_TO_MAP_VALUE, |
5241 | }, | |
5242 | }; | |
5243 | ||
5244 | static const struct bpf_reg_types sock_types = { | |
5245 | .types = { | |
5246 | PTR_TO_SOCK_COMMON, | |
5247 | PTR_TO_SOCKET, | |
5248 | PTR_TO_TCP_SOCK, | |
5249 | PTR_TO_XDP_SOCK, | |
5250 | }, | |
5251 | }; | |
5252 | ||
49a2a4d4 | 5253 | #ifdef CONFIG_NET |
1df8f55a MKL |
5254 | static const struct bpf_reg_types btf_id_sock_common_types = { |
5255 | .types = { | |
5256 | PTR_TO_SOCK_COMMON, | |
5257 | PTR_TO_SOCKET, | |
5258 | PTR_TO_TCP_SOCK, | |
5259 | PTR_TO_XDP_SOCK, | |
5260 | PTR_TO_BTF_ID, | |
5261 | }, | |
5262 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
5263 | }; | |
49a2a4d4 | 5264 | #endif |
1df8f55a | 5265 | |
f79e7ea5 LB |
5266 | static const struct bpf_reg_types mem_types = { |
5267 | .types = { | |
5268 | PTR_TO_STACK, | |
5269 | PTR_TO_PACKET, | |
5270 | PTR_TO_PACKET_META, | |
69c087ba | 5271 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5272 | PTR_TO_MAP_VALUE, |
5273 | PTR_TO_MEM, | |
a672b2e3 | 5274 | PTR_TO_MEM | MEM_ALLOC, |
20b2aff4 | 5275 | PTR_TO_BUF, |
f79e7ea5 LB |
5276 | }, |
5277 | }; | |
5278 | ||
5279 | static const struct bpf_reg_types int_ptr_types = { | |
5280 | .types = { | |
5281 | PTR_TO_STACK, | |
5282 | PTR_TO_PACKET, | |
5283 | PTR_TO_PACKET_META, | |
69c087ba | 5284 | PTR_TO_MAP_KEY, |
f79e7ea5 LB |
5285 | PTR_TO_MAP_VALUE, |
5286 | }, | |
5287 | }; | |
5288 | ||
5289 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; | |
5290 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
5291 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
a672b2e3 | 5292 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM | MEM_ALLOC } }; |
f79e7ea5 LB |
5293 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
5294 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
5295 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; | |
5844101a | 5296 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_BTF_ID | MEM_PERCPU } }; |
69c087ba YS |
5297 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
5298 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; | |
fff13c4b | 5299 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
b00628b1 | 5300 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
f79e7ea5 | 5301 | |
0789e13b | 5302 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
f79e7ea5 LB |
5303 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
5304 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, | |
5305 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, | |
f79e7ea5 LB |
5306 | [ARG_CONST_SIZE] = &scalar_types, |
5307 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
5308 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
5309 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
5310 | [ARG_PTR_TO_CTX] = &context_types, | |
f79e7ea5 | 5311 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
49a2a4d4 | 5312 | #ifdef CONFIG_NET |
1df8f55a | 5313 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 5314 | #endif |
f79e7ea5 | 5315 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
f79e7ea5 LB |
5316 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
5317 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
5318 | [ARG_PTR_TO_MEM] = &mem_types, | |
f79e7ea5 LB |
5319 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
5320 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, | |
f79e7ea5 LB |
5321 | [ARG_PTR_TO_INT] = &int_ptr_types, |
5322 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 5323 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
69c087ba | 5324 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
48946bd6 | 5325 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
fff13c4b | 5326 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
b00628b1 | 5327 | [ARG_PTR_TO_TIMER] = &timer_types, |
f79e7ea5 LB |
5328 | }; |
5329 | ||
5330 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 MKL |
5331 | enum bpf_arg_type arg_type, |
5332 | const u32 *arg_btf_id) | |
f79e7ea5 LB |
5333 | { |
5334 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
5335 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 5336 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
5337 | int i, j; |
5338 | ||
48946bd6 | 5339 | compatible = compatible_reg_types[base_type(arg_type)]; |
a968d5e2 MKL |
5340 | if (!compatible) { |
5341 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
5342 | return -EFAULT; | |
5343 | } | |
5344 | ||
216e3cd2 HL |
5345 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
5346 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY | |
5347 | * | |
5348 | * Same for MAYBE_NULL: | |
5349 | * | |
5350 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, | |
5351 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL | |
5352 | * | |
5353 | * Therefore we fold these flags depending on the arg_type before comparison. | |
5354 | */ | |
5355 | if (arg_type & MEM_RDONLY) | |
5356 | type &= ~MEM_RDONLY; | |
5357 | if (arg_type & PTR_MAYBE_NULL) | |
5358 | type &= ~PTR_MAYBE_NULL; | |
5359 | ||
f79e7ea5 LB |
5360 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
5361 | expected = compatible->types[i]; | |
5362 | if (expected == NOT_INIT) | |
5363 | break; | |
5364 | ||
5365 | if (type == expected) | |
a968d5e2 | 5366 | goto found; |
f79e7ea5 LB |
5367 | } |
5368 | ||
216e3cd2 | 5369 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
f79e7ea5 | 5370 | for (j = 0; j + 1 < i; j++) |
c25b2ae1 HL |
5371 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
5372 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); | |
f79e7ea5 | 5373 | return -EACCES; |
a968d5e2 MKL |
5374 | |
5375 | found: | |
216e3cd2 | 5376 | if (reg->type == PTR_TO_BTF_ID) { |
1df8f55a MKL |
5377 | if (!arg_btf_id) { |
5378 | if (!compatible->btf_id) { | |
5379 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
5380 | return -EFAULT; | |
5381 | } | |
5382 | arg_btf_id = compatible->btf_id; | |
5383 | } | |
5384 | ||
22dc4a0f AN |
5385 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, |
5386 | btf_vmlinux, *arg_btf_id)) { | |
a968d5e2 | 5387 | verbose(env, "R%d is of type %s but %s is expected\n", |
22dc4a0f AN |
5388 | regno, kernel_type_name(reg->btf, reg->btf_id), |
5389 | kernel_type_name(btf_vmlinux, *arg_btf_id)); | |
a968d5e2 MKL |
5390 | return -EACCES; |
5391 | } | |
a968d5e2 MKL |
5392 | } |
5393 | ||
5394 | return 0; | |
f79e7ea5 LB |
5395 | } |
5396 | ||
25b35dd2 KKD |
5397 | int check_func_arg_reg_off(struct bpf_verifier_env *env, |
5398 | const struct bpf_reg_state *reg, int regno, | |
24d5bb80 KKD |
5399 | enum bpf_arg_type arg_type, |
5400 | bool is_release_func) | |
25b35dd2 | 5401 | { |
24d5bb80 | 5402 | bool fixed_off_ok = false, release_reg; |
25b35dd2 | 5403 | enum bpf_reg_type type = reg->type; |
25b35dd2 KKD |
5404 | |
5405 | switch ((u32)type) { | |
5406 | case SCALAR_VALUE: | |
5407 | /* Pointer types where reg offset is explicitly allowed: */ | |
5408 | case PTR_TO_PACKET: | |
5409 | case PTR_TO_PACKET_META: | |
5410 | case PTR_TO_MAP_KEY: | |
5411 | case PTR_TO_MAP_VALUE: | |
5412 | case PTR_TO_MEM: | |
5413 | case PTR_TO_MEM | MEM_RDONLY: | |
5414 | case PTR_TO_MEM | MEM_ALLOC: | |
5415 | case PTR_TO_BUF: | |
5416 | case PTR_TO_BUF | MEM_RDONLY: | |
5417 | case PTR_TO_STACK: | |
5418 | /* Some of the argument types nevertheless require a | |
5419 | * zero register offset. | |
5420 | */ | |
5421 | if (arg_type != ARG_PTR_TO_ALLOC_MEM) | |
5422 | return 0; | |
5423 | break; | |
5424 | /* All the rest must be rejected, except PTR_TO_BTF_ID which allows | |
5425 | * fixed offset. | |
5426 | */ | |
5427 | case PTR_TO_BTF_ID: | |
24d5bb80 KKD |
5428 | /* When referenced PTR_TO_BTF_ID is passed to release function, |
5429 | * it's fixed offset must be 0. We rely on the property that | |
5430 | * only one referenced register can be passed to BPF helpers and | |
5431 | * kfuncs. In the other cases, fixed offset can be non-zero. | |
5432 | */ | |
5433 | release_reg = is_release_func && reg->ref_obj_id; | |
5434 | if (release_reg && reg->off) { | |
5435 | verbose(env, "R%d must have zero offset when passed to release func\n", | |
5436 | regno); | |
5437 | return -EINVAL; | |
5438 | } | |
5439 | /* For release_reg == true, fixed_off_ok must be false, but we | |
5440 | * already checked and rejected reg->off != 0 above, so set to | |
5441 | * true to allow fixed offset for all other cases. | |
5442 | */ | |
25b35dd2 KKD |
5443 | fixed_off_ok = true; |
5444 | break; | |
5445 | default: | |
5446 | break; | |
5447 | } | |
5448 | return __check_ptr_off_reg(env, reg, regno, fixed_off_ok); | |
5449 | } | |
5450 | ||
af7ec138 YS |
5451 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
5452 | struct bpf_call_arg_meta *meta, | |
5453 | const struct bpf_func_proto *fn) | |
17a52670 | 5454 | { |
af7ec138 | 5455 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 5456 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 5457 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 5458 | enum bpf_reg_type type = reg->type; |
17a52670 AS |
5459 | int err = 0; |
5460 | ||
80f1d68c | 5461 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
5462 | return 0; |
5463 | ||
dc503a8a EC |
5464 | err = check_reg_arg(env, regno, SRC_OP); |
5465 | if (err) | |
5466 | return err; | |
17a52670 | 5467 | |
1be7f75d AS |
5468 | if (arg_type == ARG_ANYTHING) { |
5469 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
5470 | verbose(env, "R%d leaks addr into helper function\n", |
5471 | regno); | |
1be7f75d AS |
5472 | return -EACCES; |
5473 | } | |
80f1d68c | 5474 | return 0; |
1be7f75d | 5475 | } |
80f1d68c | 5476 | |
de8f3a83 | 5477 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 5478 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 5479 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
5480 | return -EACCES; |
5481 | } | |
5482 | ||
48946bd6 HL |
5483 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE || |
5484 | base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) { | |
912f442c LB |
5485 | err = resolve_map_arg_type(env, meta, &arg_type); |
5486 | if (err) | |
5487 | return err; | |
5488 | } | |
5489 | ||
48946bd6 | 5490 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
fd1b0d60 LB |
5491 | /* A NULL register has a SCALAR_VALUE type, so skip |
5492 | * type checking. | |
5493 | */ | |
5494 | goto skip_type_check; | |
5495 | ||
a968d5e2 | 5496 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
f79e7ea5 LB |
5497 | if (err) |
5498 | return err; | |
5499 | ||
24d5bb80 | 5500 | err = check_func_arg_reg_off(env, reg, regno, arg_type, is_release_function(meta->func_id)); |
25b35dd2 KKD |
5501 | if (err) |
5502 | return err; | |
d7b9454a | 5503 | |
fd1b0d60 | 5504 | skip_type_check: |
24d5bb80 KKD |
5505 | /* check_func_arg_reg_off relies on only one referenced register being |
5506 | * allowed for BPF helpers. | |
5507 | */ | |
02f7c958 | 5508 | if (reg->ref_obj_id) { |
457f4436 AN |
5509 | if (meta->ref_obj_id) { |
5510 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
5511 | regno, reg->ref_obj_id, | |
5512 | meta->ref_obj_id); | |
5513 | return -EFAULT; | |
5514 | } | |
5515 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
5516 | } |
5517 | ||
17a52670 AS |
5518 | if (arg_type == ARG_CONST_MAP_PTR) { |
5519 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
3e8ce298 AS |
5520 | if (meta->map_ptr) { |
5521 | /* Use map_uid (which is unique id of inner map) to reject: | |
5522 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) | |
5523 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) | |
5524 | * if (inner_map1 && inner_map2) { | |
5525 | * timer = bpf_map_lookup_elem(inner_map1); | |
5526 | * if (timer) | |
5527 | * // mismatch would have been allowed | |
5528 | * bpf_timer_init(timer, inner_map2); | |
5529 | * } | |
5530 | * | |
5531 | * Comparing map_ptr is enough to distinguish normal and outer maps. | |
5532 | */ | |
5533 | if (meta->map_ptr != reg->map_ptr || | |
5534 | meta->map_uid != reg->map_uid) { | |
5535 | verbose(env, | |
5536 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", | |
5537 | meta->map_uid, reg->map_uid); | |
5538 | return -EINVAL; | |
5539 | } | |
b00628b1 | 5540 | } |
33ff9823 | 5541 | meta->map_ptr = reg->map_ptr; |
3e8ce298 | 5542 | meta->map_uid = reg->map_uid; |
17a52670 AS |
5543 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
5544 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
5545 | * check that [key, key + map->key_size) are within | |
5546 | * stack limits and initialized | |
5547 | */ | |
33ff9823 | 5548 | if (!meta->map_ptr) { |
17a52670 AS |
5549 | /* in function declaration map_ptr must come before |
5550 | * map_key, so that it's verified and known before | |
5551 | * we have to check map_key here. Otherwise it means | |
5552 | * that kernel subsystem misconfigured verifier | |
5553 | */ | |
61bd5218 | 5554 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
5555 | return -EACCES; |
5556 | } | |
d71962f3 PC |
5557 | err = check_helper_mem_access(env, regno, |
5558 | meta->map_ptr->key_size, false, | |
5559 | NULL); | |
48946bd6 HL |
5560 | } else if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE || |
5561 | base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) { | |
5562 | if (type_may_be_null(arg_type) && register_is_null(reg)) | |
5563 | return 0; | |
5564 | ||
17a52670 AS |
5565 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
5566 | * check [value, value + map->value_size) validity | |
5567 | */ | |
33ff9823 | 5568 | if (!meta->map_ptr) { |
17a52670 | 5569 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 5570 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
5571 | return -EACCES; |
5572 | } | |
2ea864c5 | 5573 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
5574 | err = check_helper_mem_access(env, regno, |
5575 | meta->map_ptr->value_size, false, | |
2ea864c5 | 5576 | meta); |
eaa6bcb7 HL |
5577 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
5578 | if (!reg->btf_id) { | |
5579 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
5580 | return -EACCES; | |
5581 | } | |
22dc4a0f | 5582 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 5583 | meta->ret_btf_id = reg->btf_id; |
c18f0b6a LB |
5584 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
5585 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
5586 | if (process_spin_lock(env, regno, true)) | |
5587 | return -EACCES; | |
5588 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
5589 | if (process_spin_lock(env, regno, false)) | |
5590 | return -EACCES; | |
5591 | } else { | |
5592 | verbose(env, "verifier internal error\n"); | |
5593 | return -EFAULT; | |
5594 | } | |
b00628b1 AS |
5595 | } else if (arg_type == ARG_PTR_TO_TIMER) { |
5596 | if (process_timer_func(env, regno, meta)) | |
5597 | return -EACCES; | |
69c087ba YS |
5598 | } else if (arg_type == ARG_PTR_TO_FUNC) { |
5599 | meta->subprogno = reg->subprogno; | |
a2bbe7cc LB |
5600 | } else if (arg_type_is_mem_ptr(arg_type)) { |
5601 | /* The access to this pointer is only checked when we hit the | |
5602 | * next is_mem_size argument below. | |
5603 | */ | |
5604 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); | |
90133415 | 5605 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 5606 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 5607 | |
d583691c | 5608 | err = check_mem_size_reg(env, reg, regno, zero_size_allowed, meta); |
457f4436 AN |
5609 | } else if (arg_type_is_alloc_size(arg_type)) { |
5610 | if (!tnum_is_const(reg->var_off)) { | |
28a8add6 | 5611 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
5612 | regno); |
5613 | return -EACCES; | |
5614 | } | |
5615 | meta->mem_size = reg->var_off.value; | |
57c3bb72 AI |
5616 | } else if (arg_type_is_int_ptr(arg_type)) { |
5617 | int size = int_ptr_type_to_size(arg_type); | |
5618 | ||
5619 | err = check_helper_mem_access(env, regno, size, false, meta); | |
5620 | if (err) | |
5621 | return err; | |
5622 | err = check_ptr_alignment(env, reg, 0, size, true); | |
fff13c4b FR |
5623 | } else if (arg_type == ARG_PTR_TO_CONST_STR) { |
5624 | struct bpf_map *map = reg->map_ptr; | |
5625 | int map_off; | |
5626 | u64 map_addr; | |
5627 | char *str_ptr; | |
5628 | ||
a8fad73e | 5629 | if (!bpf_map_is_rdonly(map)) { |
fff13c4b FR |
5630 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
5631 | return -EACCES; | |
5632 | } | |
5633 | ||
5634 | if (!tnum_is_const(reg->var_off)) { | |
5635 | verbose(env, "R%d is not a constant address'\n", regno); | |
5636 | return -EACCES; | |
5637 | } | |
5638 | ||
5639 | if (!map->ops->map_direct_value_addr) { | |
5640 | verbose(env, "no direct value access support for this map type\n"); | |
5641 | return -EACCES; | |
5642 | } | |
5643 | ||
5644 | err = check_map_access(env, regno, reg->off, | |
5645 | map->value_size - reg->off, false); | |
5646 | if (err) | |
5647 | return err; | |
5648 | ||
5649 | map_off = reg->off + reg->var_off.value; | |
5650 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); | |
5651 | if (err) { | |
5652 | verbose(env, "direct value access on string failed\n"); | |
5653 | return err; | |
5654 | } | |
5655 | ||
5656 | str_ptr = (char *)(long)(map_addr); | |
5657 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { | |
5658 | verbose(env, "string is not zero-terminated\n"); | |
5659 | return -EINVAL; | |
5660 | } | |
17a52670 AS |
5661 | } |
5662 | ||
5663 | return err; | |
5664 | } | |
5665 | ||
0126240f LB |
5666 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
5667 | { | |
5668 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 5669 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
5670 | |
5671 | if (func_id != BPF_FUNC_map_update_elem) | |
5672 | return false; | |
5673 | ||
5674 | /* It's not possible to get access to a locked struct sock in these | |
5675 | * contexts, so updating is safe. | |
5676 | */ | |
5677 | switch (type) { | |
5678 | case BPF_PROG_TYPE_TRACING: | |
5679 | if (eatype == BPF_TRACE_ITER) | |
5680 | return true; | |
5681 | break; | |
5682 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
5683 | case BPF_PROG_TYPE_SCHED_CLS: | |
5684 | case BPF_PROG_TYPE_SCHED_ACT: | |
5685 | case BPF_PROG_TYPE_XDP: | |
5686 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
5687 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
5688 | case BPF_PROG_TYPE_SK_LOOKUP: | |
5689 | return true; | |
5690 | default: | |
5691 | break; | |
5692 | } | |
5693 | ||
5694 | verbose(env, "cannot update sockmap in this context\n"); | |
5695 | return false; | |
5696 | } | |
5697 | ||
e411901c MF |
5698 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
5699 | { | |
5700 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); | |
5701 | } | |
5702 | ||
61bd5218 JK |
5703 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
5704 | struct bpf_map *map, int func_id) | |
35578d79 | 5705 | { |
35578d79 KX |
5706 | if (!map) |
5707 | return 0; | |
5708 | ||
6aff67c8 AS |
5709 | /* We need a two way check, first is from map perspective ... */ |
5710 | switch (map->map_type) { | |
5711 | case BPF_MAP_TYPE_PROG_ARRAY: | |
5712 | if (func_id != BPF_FUNC_tail_call) | |
5713 | goto error; | |
5714 | break; | |
5715 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
5716 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 5717 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 5718 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
5719 | func_id != BPF_FUNC_perf_event_read_value && |
5720 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
5721 | goto error; |
5722 | break; | |
457f4436 AN |
5723 | case BPF_MAP_TYPE_RINGBUF: |
5724 | if (func_id != BPF_FUNC_ringbuf_output && | |
5725 | func_id != BPF_FUNC_ringbuf_reserve && | |
457f4436 AN |
5726 | func_id != BPF_FUNC_ringbuf_query) |
5727 | goto error; | |
5728 | break; | |
6aff67c8 AS |
5729 | case BPF_MAP_TYPE_STACK_TRACE: |
5730 | if (func_id != BPF_FUNC_get_stackid) | |
5731 | goto error; | |
5732 | break; | |
4ed8ec52 | 5733 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 5734 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 5735 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
5736 | goto error; |
5737 | break; | |
cd339431 | 5738 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 5739 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
5740 | if (func_id != BPF_FUNC_get_local_storage) |
5741 | goto error; | |
5742 | break; | |
546ac1ff | 5743 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 5744 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
5745 | if (func_id != BPF_FUNC_redirect_map && |
5746 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
5747 | goto error; |
5748 | break; | |
fbfc504a BT |
5749 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
5750 | * appear. | |
5751 | */ | |
6710e112 JDB |
5752 | case BPF_MAP_TYPE_CPUMAP: |
5753 | if (func_id != BPF_FUNC_redirect_map) | |
5754 | goto error; | |
5755 | break; | |
fada7fdc JL |
5756 | case BPF_MAP_TYPE_XSKMAP: |
5757 | if (func_id != BPF_FUNC_redirect_map && | |
5758 | func_id != BPF_FUNC_map_lookup_elem) | |
5759 | goto error; | |
5760 | break; | |
56f668df | 5761 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 5762 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
5763 | if (func_id != BPF_FUNC_map_lookup_elem) |
5764 | goto error; | |
16a43625 | 5765 | break; |
174a79ff JF |
5766 | case BPF_MAP_TYPE_SOCKMAP: |
5767 | if (func_id != BPF_FUNC_sk_redirect_map && | |
5768 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 5769 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 5770 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 5771 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
5772 | func_id != BPF_FUNC_map_lookup_elem && |
5773 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
5774 | goto error; |
5775 | break; | |
81110384 JF |
5776 | case BPF_MAP_TYPE_SOCKHASH: |
5777 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
5778 | func_id != BPF_FUNC_sock_hash_update && | |
5779 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 5780 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 5781 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
5782 | func_id != BPF_FUNC_map_lookup_elem && |
5783 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
5784 | goto error; |
5785 | break; | |
2dbb9b9e MKL |
5786 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
5787 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
5788 | goto error; | |
5789 | break; | |
f1a2e44a MV |
5790 | case BPF_MAP_TYPE_QUEUE: |
5791 | case BPF_MAP_TYPE_STACK: | |
5792 | if (func_id != BPF_FUNC_map_peek_elem && | |
5793 | func_id != BPF_FUNC_map_pop_elem && | |
5794 | func_id != BPF_FUNC_map_push_elem) | |
5795 | goto error; | |
5796 | break; | |
6ac99e8f MKL |
5797 | case BPF_MAP_TYPE_SK_STORAGE: |
5798 | if (func_id != BPF_FUNC_sk_storage_get && | |
5799 | func_id != BPF_FUNC_sk_storage_delete) | |
5800 | goto error; | |
5801 | break; | |
8ea63684 KS |
5802 | case BPF_MAP_TYPE_INODE_STORAGE: |
5803 | if (func_id != BPF_FUNC_inode_storage_get && | |
5804 | func_id != BPF_FUNC_inode_storage_delete) | |
5805 | goto error; | |
5806 | break; | |
4cf1bc1f KS |
5807 | case BPF_MAP_TYPE_TASK_STORAGE: |
5808 | if (func_id != BPF_FUNC_task_storage_get && | |
5809 | func_id != BPF_FUNC_task_storage_delete) | |
5810 | goto error; | |
5811 | break; | |
9330986c JK |
5812 | case BPF_MAP_TYPE_BLOOM_FILTER: |
5813 | if (func_id != BPF_FUNC_map_peek_elem && | |
5814 | func_id != BPF_FUNC_map_push_elem) | |
5815 | goto error; | |
5816 | break; | |
6aff67c8 AS |
5817 | default: |
5818 | break; | |
5819 | } | |
5820 | ||
5821 | /* ... and second from the function itself. */ | |
5822 | switch (func_id) { | |
5823 | case BPF_FUNC_tail_call: | |
5824 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
5825 | goto error; | |
e411901c MF |
5826 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
5827 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
5828 | return -EINVAL; |
5829 | } | |
6aff67c8 AS |
5830 | break; |
5831 | case BPF_FUNC_perf_event_read: | |
5832 | case BPF_FUNC_perf_event_output: | |
908432ca | 5833 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 5834 | case BPF_FUNC_skb_output: |
d831ee84 | 5835 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
5836 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
5837 | goto error; | |
5838 | break; | |
5b029a32 DB |
5839 | case BPF_FUNC_ringbuf_output: |
5840 | case BPF_FUNC_ringbuf_reserve: | |
5841 | case BPF_FUNC_ringbuf_query: | |
5842 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) | |
5843 | goto error; | |
5844 | break; | |
6aff67c8 AS |
5845 | case BPF_FUNC_get_stackid: |
5846 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
5847 | goto error; | |
5848 | break; | |
60d20f91 | 5849 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 5850 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
5851 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
5852 | goto error; | |
5853 | break; | |
97f91a7c | 5854 | case BPF_FUNC_redirect_map: |
9c270af3 | 5855 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 5856 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
5857 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
5858 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
5859 | goto error; |
5860 | break; | |
174a79ff | 5861 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 5862 | case BPF_FUNC_msg_redirect_map: |
81110384 | 5863 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
5864 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
5865 | goto error; | |
5866 | break; | |
81110384 JF |
5867 | case BPF_FUNC_sk_redirect_hash: |
5868 | case BPF_FUNC_msg_redirect_hash: | |
5869 | case BPF_FUNC_sock_hash_update: | |
5870 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
5871 | goto error; |
5872 | break; | |
cd339431 | 5873 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
5874 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
5875 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
5876 | goto error; |
5877 | break; | |
2dbb9b9e | 5878 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
5879 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
5880 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
5881 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
5882 | goto error; |
5883 | break; | |
f1a2e44a | 5884 | case BPF_FUNC_map_pop_elem: |
f1a2e44a MV |
5885 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
5886 | map->map_type != BPF_MAP_TYPE_STACK) | |
5887 | goto error; | |
5888 | break; | |
9330986c JK |
5889 | case BPF_FUNC_map_peek_elem: |
5890 | case BPF_FUNC_map_push_elem: | |
5891 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
5892 | map->map_type != BPF_MAP_TYPE_STACK && | |
5893 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) | |
5894 | goto error; | |
5895 | break; | |
6ac99e8f MKL |
5896 | case BPF_FUNC_sk_storage_get: |
5897 | case BPF_FUNC_sk_storage_delete: | |
5898 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
5899 | goto error; | |
5900 | break; | |
8ea63684 KS |
5901 | case BPF_FUNC_inode_storage_get: |
5902 | case BPF_FUNC_inode_storage_delete: | |
5903 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
5904 | goto error; | |
5905 | break; | |
4cf1bc1f KS |
5906 | case BPF_FUNC_task_storage_get: |
5907 | case BPF_FUNC_task_storage_delete: | |
5908 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
5909 | goto error; | |
5910 | break; | |
6aff67c8 AS |
5911 | default: |
5912 | break; | |
35578d79 KX |
5913 | } |
5914 | ||
5915 | return 0; | |
6aff67c8 | 5916 | error: |
61bd5218 | 5917 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 5918 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 5919 | return -EINVAL; |
35578d79 KX |
5920 | } |
5921 | ||
90133415 | 5922 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
5923 | { |
5924 | int count = 0; | |
5925 | ||
39f19ebb | 5926 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5927 | count++; |
39f19ebb | 5928 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5929 | count++; |
39f19ebb | 5930 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5931 | count++; |
39f19ebb | 5932 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 5933 | count++; |
39f19ebb | 5934 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
5935 | count++; |
5936 | ||
90133415 DB |
5937 | /* We only support one arg being in raw mode at the moment, |
5938 | * which is sufficient for the helper functions we have | |
5939 | * right now. | |
5940 | */ | |
5941 | return count <= 1; | |
5942 | } | |
5943 | ||
5944 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
5945 | enum bpf_arg_type arg_next) | |
5946 | { | |
5947 | return (arg_type_is_mem_ptr(arg_curr) && | |
5948 | !arg_type_is_mem_size(arg_next)) || | |
5949 | (!arg_type_is_mem_ptr(arg_curr) && | |
5950 | arg_type_is_mem_size(arg_next)); | |
5951 | } | |
5952 | ||
5953 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
5954 | { | |
5955 | /* bpf_xxx(..., buf, len) call will access 'len' | |
5956 | * bytes from memory 'buf'. Both arg types need | |
5957 | * to be paired, so make sure there's no buggy | |
5958 | * helper function specification. | |
5959 | */ | |
5960 | if (arg_type_is_mem_size(fn->arg1_type) || | |
5961 | arg_type_is_mem_ptr(fn->arg5_type) || | |
5962 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
5963 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
5964 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
5965 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
5966 | return false; | |
5967 | ||
5968 | return true; | |
5969 | } | |
5970 | ||
1b986589 | 5971 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
5972 | { |
5973 | int count = 0; | |
5974 | ||
1b986589 | 5975 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 5976 | count++; |
1b986589 | 5977 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 5978 | count++; |
1b986589 | 5979 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 5980 | count++; |
1b986589 | 5981 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 5982 | count++; |
1b986589 | 5983 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
5984 | count++; |
5985 | ||
1b986589 MKL |
5986 | /* A reference acquiring function cannot acquire |
5987 | * another refcounted ptr. | |
5988 | */ | |
64d85290 | 5989 | if (may_be_acquire_function(func_id) && count) |
1b986589 MKL |
5990 | return false; |
5991 | ||
fd978bf7 JS |
5992 | /* We only support one arg being unreferenced at the moment, |
5993 | * which is sufficient for the helper functions we have right now. | |
5994 | */ | |
5995 | return count <= 1; | |
5996 | } | |
5997 | ||
9436ef6e LB |
5998 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
5999 | { | |
6000 | int i; | |
6001 | ||
1df8f55a | 6002 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
9436ef6e LB |
6003 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
6004 | return false; | |
6005 | ||
1df8f55a MKL |
6006 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
6007 | return false; | |
6008 | } | |
6009 | ||
9436ef6e LB |
6010 | return true; |
6011 | } | |
6012 | ||
1b986589 | 6013 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
6014 | { |
6015 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 6016 | check_arg_pair_ok(fn) && |
9436ef6e | 6017 | check_btf_id_ok(fn) && |
1b986589 | 6018 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
6019 | } |
6020 | ||
de8f3a83 DB |
6021 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
6022 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 6023 | */ |
f4d7e40a AS |
6024 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
6025 | struct bpf_func_state *state) | |
969bf05e | 6026 | { |
58e2af8b | 6027 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
6028 | int i; |
6029 | ||
6030 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 6031 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 6032 | mark_reg_unknown(env, regs, i); |
969bf05e | 6033 | |
f3709f69 JS |
6034 | bpf_for_each_spilled_reg(i, state, reg) { |
6035 | if (!reg) | |
969bf05e | 6036 | continue; |
de8f3a83 | 6037 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 6038 | __mark_reg_unknown(env, reg); |
969bf05e AS |
6039 | } |
6040 | } | |
6041 | ||
f4d7e40a AS |
6042 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
6043 | { | |
6044 | struct bpf_verifier_state *vstate = env->cur_state; | |
6045 | int i; | |
6046 | ||
6047 | for (i = 0; i <= vstate->curframe; i++) | |
6048 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
6049 | } | |
6050 | ||
6d94e741 AS |
6051 | enum { |
6052 | AT_PKT_END = -1, | |
6053 | BEYOND_PKT_END = -2, | |
6054 | }; | |
6055 | ||
6056 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
6057 | { | |
6058 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
6059 | struct bpf_reg_state *reg = &state->regs[regn]; | |
6060 | ||
6061 | if (reg->type != PTR_TO_PACKET) | |
6062 | /* PTR_TO_PACKET_META is not supported yet */ | |
6063 | return; | |
6064 | ||
6065 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
6066 | * How far beyond pkt_end it goes is unknown. | |
6067 | * if (!range_open) it's the case of pkt >= pkt_end | |
6068 | * if (range_open) it's the case of pkt > pkt_end | |
6069 | * hence this pointer is at least 1 byte bigger than pkt_end | |
6070 | */ | |
6071 | if (range_open) | |
6072 | reg->range = BEYOND_PKT_END; | |
6073 | else | |
6074 | reg->range = AT_PKT_END; | |
6075 | } | |
6076 | ||
fd978bf7 | 6077 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
6078 | struct bpf_func_state *state, |
6079 | int ref_obj_id) | |
fd978bf7 JS |
6080 | { |
6081 | struct bpf_reg_state *regs = state->regs, *reg; | |
6082 | int i; | |
6083 | ||
6084 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 6085 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
6086 | mark_reg_unknown(env, regs, i); |
6087 | ||
6088 | bpf_for_each_spilled_reg(i, state, reg) { | |
6089 | if (!reg) | |
6090 | continue; | |
1b986589 | 6091 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 6092 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
6093 | } |
6094 | } | |
6095 | ||
6096 | /* The pointer with the specified id has released its reference to kernel | |
6097 | * resources. Identify all copies of the same pointer and clear the reference. | |
6098 | */ | |
6099 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 6100 | int ref_obj_id) |
fd978bf7 JS |
6101 | { |
6102 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 6103 | int err; |
fd978bf7 JS |
6104 | int i; |
6105 | ||
1b986589 MKL |
6106 | err = release_reference_state(cur_func(env), ref_obj_id); |
6107 | if (err) | |
6108 | return err; | |
6109 | ||
fd978bf7 | 6110 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 6111 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 6112 | |
1b986589 | 6113 | return 0; |
fd978bf7 JS |
6114 | } |
6115 | ||
51c39bb1 AS |
6116 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
6117 | struct bpf_reg_state *regs) | |
6118 | { | |
6119 | int i; | |
6120 | ||
6121 | /* after the call registers r0 - r5 were scratched */ | |
6122 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
6123 | mark_reg_not_init(env, regs, caller_saved[i]); | |
6124 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
6125 | } | |
6126 | } | |
6127 | ||
14351375 YS |
6128 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
6129 | struct bpf_func_state *caller, | |
6130 | struct bpf_func_state *callee, | |
6131 | int insn_idx); | |
6132 | ||
6133 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
6134 | int *insn_idx, int subprog, | |
6135 | set_callee_state_fn set_callee_state_cb) | |
f4d7e40a AS |
6136 | { |
6137 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 6138 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 6139 | struct bpf_func_state *caller, *callee; |
14351375 | 6140 | int err; |
51c39bb1 | 6141 | bool is_global = false; |
f4d7e40a | 6142 | |
aada9ce6 | 6143 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 6144 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 6145 | state->curframe + 2); |
f4d7e40a AS |
6146 | return -E2BIG; |
6147 | } | |
6148 | ||
f4d7e40a AS |
6149 | caller = state->frame[state->curframe]; |
6150 | if (state->frame[state->curframe + 1]) { | |
6151 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
6152 | state->curframe + 1); | |
6153 | return -EFAULT; | |
6154 | } | |
6155 | ||
51c39bb1 AS |
6156 | func_info_aux = env->prog->aux->func_info_aux; |
6157 | if (func_info_aux) | |
6158 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
34747c41 | 6159 | err = btf_check_subprog_arg_match(env, subprog, caller->regs); |
51c39bb1 AS |
6160 | if (err == -EFAULT) |
6161 | return err; | |
6162 | if (is_global) { | |
6163 | if (err) { | |
6164 | verbose(env, "Caller passes invalid args into func#%d\n", | |
6165 | subprog); | |
6166 | return err; | |
6167 | } else { | |
6168 | if (env->log.level & BPF_LOG_LEVEL) | |
6169 | verbose(env, | |
6170 | "Func#%d is global and valid. Skipping.\n", | |
6171 | subprog); | |
6172 | clear_caller_saved_regs(env, caller->regs); | |
6173 | ||
45159b27 | 6174 | /* All global functions return a 64-bit SCALAR_VALUE */ |
51c39bb1 | 6175 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
45159b27 | 6176 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
51c39bb1 AS |
6177 | |
6178 | /* continue with next insn after call */ | |
6179 | return 0; | |
6180 | } | |
6181 | } | |
6182 | ||
bfc6bb74 | 6183 | if (insn->code == (BPF_JMP | BPF_CALL) && |
a5bebc4f | 6184 | insn->src_reg == 0 && |
bfc6bb74 AS |
6185 | insn->imm == BPF_FUNC_timer_set_callback) { |
6186 | struct bpf_verifier_state *async_cb; | |
6187 | ||
6188 | /* there is no real recursion here. timer callbacks are async */ | |
7ddc80a4 | 6189 | env->subprog_info[subprog].is_async_cb = true; |
bfc6bb74 AS |
6190 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
6191 | *insn_idx, subprog); | |
6192 | if (!async_cb) | |
6193 | return -EFAULT; | |
6194 | callee = async_cb->frame[0]; | |
6195 | callee->async_entry_cnt = caller->async_entry_cnt + 1; | |
6196 | ||
6197 | /* Convert bpf_timer_set_callback() args into timer callback args */ | |
6198 | err = set_callee_state_cb(env, caller, callee, *insn_idx); | |
6199 | if (err) | |
6200 | return err; | |
6201 | ||
6202 | clear_caller_saved_regs(env, caller->regs); | |
6203 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
6204 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
6205 | /* continue with next insn after call */ | |
6206 | return 0; | |
6207 | } | |
6208 | ||
f4d7e40a AS |
6209 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
6210 | if (!callee) | |
6211 | return -ENOMEM; | |
6212 | state->frame[state->curframe + 1] = callee; | |
6213 | ||
6214 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
6215 | * into its own stack before reading from it. | |
6216 | * callee can read/write into caller's stack | |
6217 | */ | |
6218 | init_func_state(env, callee, | |
6219 | /* remember the callsite, it will be used by bpf_exit */ | |
6220 | *insn_idx /* callsite */, | |
6221 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 6222 | subprog /* subprog number within this prog */); |
f4d7e40a | 6223 | |
fd978bf7 | 6224 | /* Transfer references to the callee */ |
c69431aa | 6225 | err = copy_reference_state(callee, caller); |
fd978bf7 JS |
6226 | if (err) |
6227 | return err; | |
6228 | ||
14351375 YS |
6229 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
6230 | if (err) | |
6231 | return err; | |
f4d7e40a | 6232 | |
51c39bb1 | 6233 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
6234 | |
6235 | /* only increment it after check_reg_arg() finished */ | |
6236 | state->curframe++; | |
6237 | ||
6238 | /* and go analyze first insn of the callee */ | |
14351375 | 6239 | *insn_idx = env->subprog_info[subprog].start - 1; |
f4d7e40a | 6240 | |
06ee7115 | 6241 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 6242 | verbose(env, "caller:\n"); |
0f55f9ed | 6243 | print_verifier_state(env, caller, true); |
f4d7e40a | 6244 | verbose(env, "callee:\n"); |
0f55f9ed | 6245 | print_verifier_state(env, callee, true); |
f4d7e40a AS |
6246 | } |
6247 | return 0; | |
6248 | } | |
6249 | ||
314ee05e YS |
6250 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
6251 | struct bpf_func_state *caller, | |
6252 | struct bpf_func_state *callee) | |
6253 | { | |
6254 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, | |
6255 | * void *callback_ctx, u64 flags); | |
6256 | * callback_fn(struct bpf_map *map, void *key, void *value, | |
6257 | * void *callback_ctx); | |
6258 | */ | |
6259 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6260 | ||
6261 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6262 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6263 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6264 | ||
6265 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6266 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6267 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6268 | ||
6269 | /* pointer to stack or null */ | |
6270 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; | |
6271 | ||
6272 | /* unused */ | |
6273 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6274 | return 0; | |
6275 | } | |
6276 | ||
14351375 YS |
6277 | static int set_callee_state(struct bpf_verifier_env *env, |
6278 | struct bpf_func_state *caller, | |
6279 | struct bpf_func_state *callee, int insn_idx) | |
6280 | { | |
6281 | int i; | |
6282 | ||
6283 | /* copy r1 - r5 args that callee can access. The copy includes parent | |
6284 | * pointers, which connects us up to the liveness chain | |
6285 | */ | |
6286 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) | |
6287 | callee->regs[i] = caller->regs[i]; | |
6288 | return 0; | |
6289 | } | |
6290 | ||
6291 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
6292 | int *insn_idx) | |
6293 | { | |
6294 | int subprog, target_insn; | |
6295 | ||
6296 | target_insn = *insn_idx + insn->imm + 1; | |
6297 | subprog = find_subprog(env, target_insn); | |
6298 | if (subprog < 0) { | |
6299 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
6300 | target_insn); | |
6301 | return -EFAULT; | |
6302 | } | |
6303 | ||
6304 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); | |
6305 | } | |
6306 | ||
69c087ba YS |
6307 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
6308 | struct bpf_func_state *caller, | |
6309 | struct bpf_func_state *callee, | |
6310 | int insn_idx) | |
6311 | { | |
6312 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; | |
6313 | struct bpf_map *map; | |
6314 | int err; | |
6315 | ||
6316 | if (bpf_map_ptr_poisoned(insn_aux)) { | |
6317 | verbose(env, "tail_call abusing map_ptr\n"); | |
6318 | return -EINVAL; | |
6319 | } | |
6320 | ||
6321 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); | |
6322 | if (!map->ops->map_set_for_each_callback_args || | |
6323 | !map->ops->map_for_each_callback) { | |
6324 | verbose(env, "callback function not allowed for map\n"); | |
6325 | return -ENOTSUPP; | |
6326 | } | |
6327 | ||
6328 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); | |
6329 | if (err) | |
6330 | return err; | |
6331 | ||
6332 | callee->in_callback_fn = true; | |
6333 | return 0; | |
6334 | } | |
6335 | ||
e6f2dd0f JK |
6336 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
6337 | struct bpf_func_state *caller, | |
6338 | struct bpf_func_state *callee, | |
6339 | int insn_idx) | |
6340 | { | |
6341 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, | |
6342 | * u64 flags); | |
6343 | * callback_fn(u32 index, void *callback_ctx); | |
6344 | */ | |
6345 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; | |
6346 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; | |
6347 | ||
6348 | /* unused */ | |
6349 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); | |
6350 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6351 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6352 | ||
6353 | callee->in_callback_fn = true; | |
6354 | return 0; | |
6355 | } | |
6356 | ||
b00628b1 AS |
6357 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
6358 | struct bpf_func_state *caller, | |
6359 | struct bpf_func_state *callee, | |
6360 | int insn_idx) | |
6361 | { | |
6362 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; | |
6363 | ||
6364 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); | |
6365 | * callback_fn(struct bpf_map *map, void *key, void *value); | |
6366 | */ | |
6367 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; | |
6368 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); | |
6369 | callee->regs[BPF_REG_1].map_ptr = map_ptr; | |
6370 | ||
6371 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; | |
6372 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6373 | callee->regs[BPF_REG_2].map_ptr = map_ptr; | |
6374 | ||
6375 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; | |
6376 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); | |
6377 | callee->regs[BPF_REG_3].map_ptr = map_ptr; | |
6378 | ||
6379 | /* unused */ | |
6380 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6381 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
bfc6bb74 | 6382 | callee->in_async_callback_fn = true; |
b00628b1 AS |
6383 | return 0; |
6384 | } | |
6385 | ||
7c7e3d31 SL |
6386 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
6387 | struct bpf_func_state *caller, | |
6388 | struct bpf_func_state *callee, | |
6389 | int insn_idx) | |
6390 | { | |
6391 | /* bpf_find_vma(struct task_struct *task, u64 addr, | |
6392 | * void *callback_fn, void *callback_ctx, u64 flags) | |
6393 | * (callback_fn)(struct task_struct *task, | |
6394 | * struct vm_area_struct *vma, void *callback_ctx); | |
6395 | */ | |
6396 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; | |
6397 | ||
6398 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; | |
6399 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); | |
6400 | callee->regs[BPF_REG_2].btf = btf_vmlinux; | |
d19ddb47 | 6401 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
7c7e3d31 SL |
6402 | |
6403 | /* pointer to stack or null */ | |
6404 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; | |
6405 | ||
6406 | /* unused */ | |
6407 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); | |
6408 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); | |
6409 | callee->in_callback_fn = true; | |
6410 | return 0; | |
6411 | } | |
6412 | ||
f4d7e40a AS |
6413 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
6414 | { | |
6415 | struct bpf_verifier_state *state = env->cur_state; | |
6416 | struct bpf_func_state *caller, *callee; | |
6417 | struct bpf_reg_state *r0; | |
fd978bf7 | 6418 | int err; |
f4d7e40a AS |
6419 | |
6420 | callee = state->frame[state->curframe]; | |
6421 | r0 = &callee->regs[BPF_REG_0]; | |
6422 | if (r0->type == PTR_TO_STACK) { | |
6423 | /* technically it's ok to return caller's stack pointer | |
6424 | * (or caller's caller's pointer) back to the caller, | |
6425 | * since these pointers are valid. Only current stack | |
6426 | * pointer will be invalid as soon as function exits, | |
6427 | * but let's be conservative | |
6428 | */ | |
6429 | verbose(env, "cannot return stack pointer to the caller\n"); | |
6430 | return -EINVAL; | |
6431 | } | |
6432 | ||
6433 | state->curframe--; | |
6434 | caller = state->frame[state->curframe]; | |
69c087ba YS |
6435 | if (callee->in_callback_fn) { |
6436 | /* enforce R0 return value range [0, 1]. */ | |
6437 | struct tnum range = tnum_range(0, 1); | |
6438 | ||
6439 | if (r0->type != SCALAR_VALUE) { | |
6440 | verbose(env, "R0 not a scalar value\n"); | |
6441 | return -EACCES; | |
6442 | } | |
6443 | if (!tnum_in(range, r0->var_off)) { | |
6444 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); | |
6445 | return -EINVAL; | |
6446 | } | |
6447 | } else { | |
6448 | /* return to the caller whatever r0 had in the callee */ | |
6449 | caller->regs[BPF_REG_0] = *r0; | |
6450 | } | |
f4d7e40a | 6451 | |
fd978bf7 | 6452 | /* Transfer references to the caller */ |
c69431aa | 6453 | err = copy_reference_state(caller, callee); |
fd978bf7 JS |
6454 | if (err) |
6455 | return err; | |
6456 | ||
f4d7e40a | 6457 | *insn_idx = callee->callsite + 1; |
06ee7115 | 6458 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a | 6459 | verbose(env, "returning from callee:\n"); |
0f55f9ed | 6460 | print_verifier_state(env, callee, true); |
f4d7e40a | 6461 | verbose(env, "to caller at %d:\n", *insn_idx); |
0f55f9ed | 6462 | print_verifier_state(env, caller, true); |
f4d7e40a AS |
6463 | } |
6464 | /* clear everything in the callee */ | |
6465 | free_func_state(callee); | |
6466 | state->frame[state->curframe + 1] = NULL; | |
6467 | return 0; | |
6468 | } | |
6469 | ||
849fa506 YS |
6470 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
6471 | int func_id, | |
6472 | struct bpf_call_arg_meta *meta) | |
6473 | { | |
6474 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
6475 | ||
6476 | if (ret_type != RET_INTEGER || | |
6477 | (func_id != BPF_FUNC_get_stack && | |
fd0b88f7 | 6478 | func_id != BPF_FUNC_get_task_stack && |
47cc0ed5 DB |
6479 | func_id != BPF_FUNC_probe_read_str && |
6480 | func_id != BPF_FUNC_probe_read_kernel_str && | |
6481 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
6482 | return; |
6483 | ||
10060503 | 6484 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 6485 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
6486 | ret_reg->smin_value = -MAX_ERRNO; |
6487 | ret_reg->s32_min_value = -MAX_ERRNO; | |
849fa506 YS |
6488 | __reg_deduce_bounds(ret_reg); |
6489 | __reg_bound_offset(ret_reg); | |
10060503 | 6490 | __update_reg_bounds(ret_reg); |
849fa506 YS |
6491 | } |
6492 | ||
c93552c4 DB |
6493 | static int |
6494 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
6495 | int func_id, int insn_idx) | |
6496 | { | |
6497 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 6498 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
6499 | |
6500 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
6501 | func_id != BPF_FUNC_map_lookup_elem && |
6502 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
6503 | func_id != BPF_FUNC_map_delete_elem && |
6504 | func_id != BPF_FUNC_map_push_elem && | |
6505 | func_id != BPF_FUNC_map_pop_elem && | |
69c087ba | 6506 | func_id != BPF_FUNC_map_peek_elem && |
e6a4750f BT |
6507 | func_id != BPF_FUNC_for_each_map_elem && |
6508 | func_id != BPF_FUNC_redirect_map) | |
c93552c4 | 6509 | return 0; |
09772d92 | 6510 | |
591fe988 | 6511 | if (map == NULL) { |
c93552c4 DB |
6512 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
6513 | return -EINVAL; | |
6514 | } | |
6515 | ||
591fe988 DB |
6516 | /* In case of read-only, some additional restrictions |
6517 | * need to be applied in order to prevent altering the | |
6518 | * state of the map from program side. | |
6519 | */ | |
6520 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
6521 | (func_id == BPF_FUNC_map_delete_elem || | |
6522 | func_id == BPF_FUNC_map_update_elem || | |
6523 | func_id == BPF_FUNC_map_push_elem || | |
6524 | func_id == BPF_FUNC_map_pop_elem)) { | |
6525 | verbose(env, "write into map forbidden\n"); | |
6526 | return -EACCES; | |
6527 | } | |
6528 | ||
d2e4c1e6 | 6529 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 6530 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 6531 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 6532 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 6533 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 6534 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
6535 | return 0; |
6536 | } | |
6537 | ||
d2e4c1e6 DB |
6538 | static int |
6539 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
6540 | int func_id, int insn_idx) | |
6541 | { | |
6542 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
6543 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
6544 | struct bpf_map *map = meta->map_ptr; | |
6545 | struct tnum range; | |
6546 | u64 val; | |
cc52d914 | 6547 | int err; |
d2e4c1e6 DB |
6548 | |
6549 | if (func_id != BPF_FUNC_tail_call) | |
6550 | return 0; | |
6551 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
6552 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
6553 | return -EINVAL; | |
6554 | } | |
6555 | ||
6556 | range = tnum_range(0, map->max_entries - 1); | |
6557 | reg = ®s[BPF_REG_3]; | |
6558 | ||
6559 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
6560 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
6561 | return 0; | |
6562 | } | |
6563 | ||
cc52d914 DB |
6564 | err = mark_chain_precision(env, BPF_REG_3); |
6565 | if (err) | |
6566 | return err; | |
6567 | ||
d2e4c1e6 DB |
6568 | val = reg->var_off.value; |
6569 | if (bpf_map_key_unseen(aux)) | |
6570 | bpf_map_key_store(aux, val); | |
6571 | else if (!bpf_map_key_poisoned(aux) && | |
6572 | bpf_map_key_immediate(aux) != val) | |
6573 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
6574 | return 0; | |
6575 | } | |
6576 | ||
fd978bf7 JS |
6577 | static int check_reference_leak(struct bpf_verifier_env *env) |
6578 | { | |
6579 | struct bpf_func_state *state = cur_func(env); | |
6580 | int i; | |
6581 | ||
6582 | for (i = 0; i < state->acquired_refs; i++) { | |
6583 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
6584 | state->refs[i].id, state->refs[i].insn_idx); | |
6585 | } | |
6586 | return state->acquired_refs ? -EINVAL : 0; | |
6587 | } | |
6588 | ||
7b15523a FR |
6589 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
6590 | struct bpf_reg_state *regs) | |
6591 | { | |
6592 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; | |
6593 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; | |
6594 | struct bpf_map *fmt_map = fmt_reg->map_ptr; | |
6595 | int err, fmt_map_off, num_args; | |
6596 | u64 fmt_addr; | |
6597 | char *fmt; | |
6598 | ||
6599 | /* data must be an array of u64 */ | |
6600 | if (data_len_reg->var_off.value % 8) | |
6601 | return -EINVAL; | |
6602 | num_args = data_len_reg->var_off.value / 8; | |
6603 | ||
6604 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const | |
6605 | * and map_direct_value_addr is set. | |
6606 | */ | |
6607 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; | |
6608 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, | |
6609 | fmt_map_off); | |
8e8ee109 FR |
6610 | if (err) { |
6611 | verbose(env, "verifier bug\n"); | |
6612 | return -EFAULT; | |
6613 | } | |
7b15523a FR |
6614 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
6615 | ||
6616 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we | |
6617 | * can focus on validating the format specifiers. | |
6618 | */ | |
48cac3f4 | 6619 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
7b15523a FR |
6620 | if (err < 0) |
6621 | verbose(env, "Invalid format string\n"); | |
6622 | ||
6623 | return err; | |
6624 | } | |
6625 | ||
9b99edca JO |
6626 | static int check_get_func_ip(struct bpf_verifier_env *env) |
6627 | { | |
9b99edca JO |
6628 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
6629 | int func_id = BPF_FUNC_get_func_ip; | |
6630 | ||
6631 | if (type == BPF_PROG_TYPE_TRACING) { | |
f92c1e18 | 6632 | if (!bpf_prog_has_trampoline(env->prog)) { |
9b99edca JO |
6633 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
6634 | func_id_name(func_id), func_id); | |
6635 | return -ENOTSUPP; | |
6636 | } | |
6637 | return 0; | |
9ffd9f3f JO |
6638 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
6639 | return 0; | |
9b99edca JO |
6640 | } |
6641 | ||
6642 | verbose(env, "func %s#%d not supported for program type %d\n", | |
6643 | func_id_name(func_id), func_id, type); | |
6644 | return -ENOTSUPP; | |
6645 | } | |
6646 | ||
69c087ba YS |
6647 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
6648 | int *insn_idx_p) | |
17a52670 | 6649 | { |
17a52670 | 6650 | const struct bpf_func_proto *fn = NULL; |
3c480732 | 6651 | enum bpf_return_type ret_type; |
c25b2ae1 | 6652 | enum bpf_type_flag ret_flag; |
638f5b90 | 6653 | struct bpf_reg_state *regs; |
33ff9823 | 6654 | struct bpf_call_arg_meta meta; |
69c087ba | 6655 | int insn_idx = *insn_idx_p; |
969bf05e | 6656 | bool changes_data; |
69c087ba | 6657 | int i, err, func_id; |
17a52670 AS |
6658 | |
6659 | /* find function prototype */ | |
69c087ba | 6660 | func_id = insn->imm; |
17a52670 | 6661 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
61bd5218 JK |
6662 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
6663 | func_id); | |
17a52670 AS |
6664 | return -EINVAL; |
6665 | } | |
6666 | ||
00176a34 | 6667 | if (env->ops->get_func_proto) |
5e43f899 | 6668 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 6669 | if (!fn) { |
61bd5218 JK |
6670 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
6671 | func_id); | |
17a52670 AS |
6672 | return -EINVAL; |
6673 | } | |
6674 | ||
6675 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 6676 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 6677 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
6678 | return -EINVAL; |
6679 | } | |
6680 | ||
eae2e83e JO |
6681 | if (fn->allowed && !fn->allowed(env->prog)) { |
6682 | verbose(env, "helper call is not allowed in probe\n"); | |
6683 | return -EINVAL; | |
6684 | } | |
6685 | ||
04514d13 | 6686 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 6687 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
6688 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
6689 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
6690 | func_id_name(func_id), func_id); | |
6691 | return -EINVAL; | |
6692 | } | |
969bf05e | 6693 | |
33ff9823 | 6694 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 6695 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 6696 | |
1b986589 | 6697 | err = check_func_proto(fn, func_id); |
435faee1 | 6698 | if (err) { |
61bd5218 | 6699 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 6700 | func_id_name(func_id), func_id); |
435faee1 DB |
6701 | return err; |
6702 | } | |
6703 | ||
d83525ca | 6704 | meta.func_id = func_id; |
17a52670 | 6705 | /* check args */ |
523a4cf4 | 6706 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
af7ec138 | 6707 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
6708 | if (err) |
6709 | return err; | |
6710 | } | |
17a52670 | 6711 | |
c93552c4 DB |
6712 | err = record_func_map(env, &meta, func_id, insn_idx); |
6713 | if (err) | |
6714 | return err; | |
6715 | ||
d2e4c1e6 DB |
6716 | err = record_func_key(env, &meta, func_id, insn_idx); |
6717 | if (err) | |
6718 | return err; | |
6719 | ||
435faee1 DB |
6720 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
6721 | * is inferred from register state. | |
6722 | */ | |
6723 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
6724 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
6725 | BPF_WRITE, -1, false); | |
435faee1 DB |
6726 | if (err) |
6727 | return err; | |
6728 | } | |
6729 | ||
e6f2dd0f | 6730 | if (is_release_function(func_id)) { |
1b986589 | 6731 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
6732 | if (err) { |
6733 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
6734 | func_id_name(func_id), func_id); | |
fd978bf7 | 6735 | return err; |
46f8bc92 | 6736 | } |
fd978bf7 JS |
6737 | } |
6738 | ||
638f5b90 | 6739 | regs = cur_regs(env); |
cd339431 | 6740 | |
e6f2dd0f JK |
6741 | switch (func_id) { |
6742 | case BPF_FUNC_tail_call: | |
6743 | err = check_reference_leak(env); | |
6744 | if (err) { | |
6745 | verbose(env, "tail_call would lead to reference leak\n"); | |
6746 | return err; | |
6747 | } | |
6748 | break; | |
6749 | case BPF_FUNC_get_local_storage: | |
6750 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
6751 | * this is required because get_local_storage() can't return an error. | |
6752 | */ | |
6753 | if (!register_is_null(®s[BPF_REG_2])) { | |
6754 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
6755 | return -EINVAL; | |
6756 | } | |
6757 | break; | |
6758 | case BPF_FUNC_for_each_map_elem: | |
69c087ba YS |
6759 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
6760 | set_map_elem_callback_state); | |
e6f2dd0f JK |
6761 | break; |
6762 | case BPF_FUNC_timer_set_callback: | |
b00628b1 AS |
6763 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
6764 | set_timer_callback_state); | |
e6f2dd0f JK |
6765 | break; |
6766 | case BPF_FUNC_find_vma: | |
7c7e3d31 SL |
6767 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
6768 | set_find_vma_callback_state); | |
e6f2dd0f JK |
6769 | break; |
6770 | case BPF_FUNC_snprintf: | |
7b15523a | 6771 | err = check_bpf_snprintf_call(env, regs); |
e6f2dd0f JK |
6772 | break; |
6773 | case BPF_FUNC_loop: | |
6774 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, | |
6775 | set_loop_callback_state); | |
6776 | break; | |
7b15523a FR |
6777 | } |
6778 | ||
e6f2dd0f JK |
6779 | if (err) |
6780 | return err; | |
6781 | ||
17a52670 | 6782 | /* reset caller saved regs */ |
dc503a8a | 6783 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 6784 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
6785 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
6786 | } | |
17a52670 | 6787 | |
5327ed3d JW |
6788 | /* helper call returns 64-bit value. */ |
6789 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
6790 | ||
dc503a8a | 6791 | /* update return register (already marked as written above) */ |
3c480732 | 6792 | ret_type = fn->ret_type; |
c25b2ae1 | 6793 | ret_flag = type_flag(fn->ret_type); |
3c480732 | 6794 | if (ret_type == RET_INTEGER) { |
f1174f77 | 6795 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 6796 | mark_reg_unknown(env, regs, BPF_REG_0); |
3c480732 | 6797 | } else if (ret_type == RET_VOID) { |
17a52670 | 6798 | regs[BPF_REG_0].type = NOT_INIT; |
3c480732 | 6799 | } else if (base_type(ret_type) == RET_PTR_TO_MAP_VALUE) { |
f1174f77 | 6800 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 6801 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
6802 | /* remember map_ptr, so that check_map_access() |
6803 | * can check 'value_size' boundary of memory access | |
6804 | * to map element returned from bpf_map_lookup_elem() | |
6805 | */ | |
33ff9823 | 6806 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
6807 | verbose(env, |
6808 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
6809 | return -EINVAL; |
6810 | } | |
33ff9823 | 6811 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
3e8ce298 | 6812 | regs[BPF_REG_0].map_uid = meta.map_uid; |
c25b2ae1 HL |
6813 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
6814 | if (!type_may_be_null(ret_type) && | |
6815 | map_value_has_spin_lock(meta.map_ptr)) { | |
6816 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 | 6817 | } |
3c480732 | 6818 | } else if (base_type(ret_type) == RET_PTR_TO_SOCKET) { |
c64b7983 | 6819 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 6820 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
3c480732 | 6821 | } else if (base_type(ret_type) == RET_PTR_TO_SOCK_COMMON) { |
85a51f8c | 6822 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 6823 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
3c480732 | 6824 | } else if (base_type(ret_type) == RET_PTR_TO_TCP_SOCK) { |
655a51e5 | 6825 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 6826 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
3c480732 | 6827 | } else if (base_type(ret_type) == RET_PTR_TO_ALLOC_MEM) { |
457f4436 | 6828 | mark_reg_known_zero(env, regs, BPF_REG_0); |
c25b2ae1 | 6829 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
457f4436 | 6830 | regs[BPF_REG_0].mem_size = meta.mem_size; |
3c480732 | 6831 | } else if (base_type(ret_type) == RET_PTR_TO_MEM_OR_BTF_ID) { |
eaa6bcb7 HL |
6832 | const struct btf_type *t; |
6833 | ||
6834 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 6835 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
6836 | if (!btf_type_is_struct(t)) { |
6837 | u32 tsize; | |
6838 | const struct btf_type *ret; | |
6839 | const char *tname; | |
6840 | ||
6841 | /* resolve the type size of ksym. */ | |
22dc4a0f | 6842 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 6843 | if (IS_ERR(ret)) { |
22dc4a0f | 6844 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
6845 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
6846 | tname, PTR_ERR(ret)); | |
6847 | return -EINVAL; | |
6848 | } | |
c25b2ae1 | 6849 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
eaa6bcb7 HL |
6850 | regs[BPF_REG_0].mem_size = tsize; |
6851 | } else { | |
34d3a78c HL |
6852 | /* MEM_RDONLY may be carried from ret_flag, but it |
6853 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise | |
6854 | * it will confuse the check of PTR_TO_BTF_ID in | |
6855 | * check_mem_access(). | |
6856 | */ | |
6857 | ret_flag &= ~MEM_RDONLY; | |
6858 | ||
c25b2ae1 | 6859 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
22dc4a0f | 6860 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
6861 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
6862 | } | |
3c480732 | 6863 | } else if (base_type(ret_type) == RET_PTR_TO_BTF_ID) { |
af7ec138 YS |
6864 | int ret_btf_id; |
6865 | ||
6866 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
c25b2ae1 | 6867 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
af7ec138 YS |
6868 | ret_btf_id = *fn->ret_btf_id; |
6869 | if (ret_btf_id == 0) { | |
3c480732 HL |
6870 | verbose(env, "invalid return type %u of func %s#%d\n", |
6871 | base_type(ret_type), func_id_name(func_id), | |
6872 | func_id); | |
af7ec138 YS |
6873 | return -EINVAL; |
6874 | } | |
22dc4a0f AN |
6875 | /* current BPF helper definitions are only coming from |
6876 | * built-in code with type IDs from vmlinux BTF | |
6877 | */ | |
6878 | regs[BPF_REG_0].btf = btf_vmlinux; | |
af7ec138 | 6879 | regs[BPF_REG_0].btf_id = ret_btf_id; |
17a52670 | 6880 | } else { |
3c480732 HL |
6881 | verbose(env, "unknown return type %u of func %s#%d\n", |
6882 | base_type(ret_type), func_id_name(func_id), func_id); | |
17a52670 AS |
6883 | return -EINVAL; |
6884 | } | |
04fd61ab | 6885 | |
c25b2ae1 | 6886 | if (type_may_be_null(regs[BPF_REG_0].type)) |
93c230e3 MKL |
6887 | regs[BPF_REG_0].id = ++env->id_gen; |
6888 | ||
0f3adc28 | 6889 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
6890 | /* For release_reference() */ |
6891 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 6892 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
6893 | int id = acquire_reference_state(env, insn_idx); |
6894 | ||
6895 | if (id < 0) | |
6896 | return id; | |
6897 | /* For mark_ptr_or_null_reg() */ | |
6898 | regs[BPF_REG_0].id = id; | |
6899 | /* For release_reference() */ | |
6900 | regs[BPF_REG_0].ref_obj_id = id; | |
6901 | } | |
1b986589 | 6902 | |
849fa506 YS |
6903 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
6904 | ||
61bd5218 | 6905 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
6906 | if (err) |
6907 | return err; | |
04fd61ab | 6908 | |
fa28dcb8 SL |
6909 | if ((func_id == BPF_FUNC_get_stack || |
6910 | func_id == BPF_FUNC_get_task_stack) && | |
6911 | !env->prog->has_callchain_buf) { | |
c195651e YS |
6912 | const char *err_str; |
6913 | ||
6914 | #ifdef CONFIG_PERF_EVENTS | |
6915 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
6916 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
6917 | #else | |
6918 | err = -ENOTSUPP; | |
6919 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
6920 | #endif | |
6921 | if (err) { | |
6922 | verbose(env, err_str, func_id_name(func_id), func_id); | |
6923 | return err; | |
6924 | } | |
6925 | ||
6926 | env->prog->has_callchain_buf = true; | |
6927 | } | |
6928 | ||
5d99cb2c SL |
6929 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
6930 | env->prog->call_get_stack = true; | |
6931 | ||
9b99edca JO |
6932 | if (func_id == BPF_FUNC_get_func_ip) { |
6933 | if (check_get_func_ip(env)) | |
6934 | return -ENOTSUPP; | |
6935 | env->prog->call_get_func_ip = true; | |
6936 | } | |
6937 | ||
969bf05e AS |
6938 | if (changes_data) |
6939 | clear_all_pkt_pointers(env); | |
6940 | return 0; | |
6941 | } | |
6942 | ||
e6ac2450 MKL |
6943 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
6944 | * the BTF func_proto's return value size and argument. | |
6945 | */ | |
6946 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, | |
6947 | size_t reg_size) | |
6948 | { | |
6949 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; | |
6950 | ||
6951 | if (regno == BPF_REG_0) { | |
6952 | /* Function return value */ | |
6953 | reg->live |= REG_LIVE_WRITTEN; | |
6954 | reg->subreg_def = reg_size == sizeof(u64) ? | |
6955 | DEF_NOT_SUBREG : env->insn_idx + 1; | |
6956 | } else { | |
6957 | /* Function argument */ | |
6958 | if (reg_size == sizeof(u64)) { | |
6959 | mark_insn_zext(env, reg); | |
6960 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
6961 | } else { | |
6962 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); | |
6963 | } | |
6964 | } | |
6965 | } | |
6966 | ||
5c073f26 KKD |
6967 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
6968 | int *insn_idx_p) | |
e6ac2450 MKL |
6969 | { |
6970 | const struct btf_type *t, *func, *func_proto, *ptr_type; | |
6971 | struct bpf_reg_state *regs = cur_regs(env); | |
6972 | const char *func_name, *ptr_type_name; | |
6973 | u32 i, nargs, func_id, ptr_type_id; | |
5c073f26 | 6974 | int err, insn_idx = *insn_idx_p; |
e6ac2450 | 6975 | const struct btf_param *args; |
2357672c | 6976 | struct btf *desc_btf; |
5c073f26 | 6977 | bool acq; |
e6ac2450 | 6978 | |
a5d82727 KKD |
6979 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
6980 | if (!insn->imm) | |
6981 | return 0; | |
6982 | ||
b202d844 | 6983 | desc_btf = find_kfunc_desc_btf(env, insn->imm, insn->off); |
2357672c KKD |
6984 | if (IS_ERR(desc_btf)) |
6985 | return PTR_ERR(desc_btf); | |
6986 | ||
e6ac2450 | 6987 | func_id = insn->imm; |
2357672c KKD |
6988 | func = btf_type_by_id(desc_btf, func_id); |
6989 | func_name = btf_name_by_offset(desc_btf, func->name_off); | |
6990 | func_proto = btf_type_by_id(desc_btf, func->type); | |
e6ac2450 | 6991 | |
b202d844 KKD |
6992 | if (!btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), |
6993 | BTF_KFUNC_TYPE_CHECK, func_id)) { | |
e6ac2450 MKL |
6994 | verbose(env, "calling kernel function %s is not allowed\n", |
6995 | func_name); | |
6996 | return -EACCES; | |
6997 | } | |
6998 | ||
5c073f26 KKD |
6999 | acq = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), |
7000 | BTF_KFUNC_TYPE_ACQUIRE, func_id); | |
7001 | ||
e6ac2450 | 7002 | /* Check the arguments */ |
2357672c | 7003 | err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs); |
5c073f26 | 7004 | if (err < 0) |
e6ac2450 | 7005 | return err; |
5c073f26 KKD |
7006 | /* In case of release function, we get register number of refcounted |
7007 | * PTR_TO_BTF_ID back from btf_check_kfunc_arg_match, do the release now | |
7008 | */ | |
7009 | if (err) { | |
7010 | err = release_reference(env, regs[err].ref_obj_id); | |
7011 | if (err) { | |
7012 | verbose(env, "kfunc %s#%d reference has not been acquired before\n", | |
7013 | func_name, func_id); | |
7014 | return err; | |
7015 | } | |
7016 | } | |
e6ac2450 MKL |
7017 | |
7018 | for (i = 0; i < CALLER_SAVED_REGS; i++) | |
7019 | mark_reg_not_init(env, regs, caller_saved[i]); | |
7020 | ||
7021 | /* Check return type */ | |
2357672c | 7022 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
5c073f26 KKD |
7023 | |
7024 | if (acq && !btf_type_is_ptr(t)) { | |
7025 | verbose(env, "acquire kernel function does not return PTR_TO_BTF_ID\n"); | |
7026 | return -EINVAL; | |
7027 | } | |
7028 | ||
e6ac2450 MKL |
7029 | if (btf_type_is_scalar(t)) { |
7030 | mark_reg_unknown(env, regs, BPF_REG_0); | |
7031 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); | |
7032 | } else if (btf_type_is_ptr(t)) { | |
2357672c | 7033 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, |
e6ac2450 MKL |
7034 | &ptr_type_id); |
7035 | if (!btf_type_is_struct(ptr_type)) { | |
2357672c | 7036 | ptr_type_name = btf_name_by_offset(desc_btf, |
e6ac2450 MKL |
7037 | ptr_type->name_off); |
7038 | verbose(env, "kernel function %s returns pointer type %s %s is not supported\n", | |
7039 | func_name, btf_type_str(ptr_type), | |
7040 | ptr_type_name); | |
7041 | return -EINVAL; | |
7042 | } | |
7043 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
2357672c | 7044 | regs[BPF_REG_0].btf = desc_btf; |
e6ac2450 MKL |
7045 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; |
7046 | regs[BPF_REG_0].btf_id = ptr_type_id; | |
5c073f26 KKD |
7047 | if (btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), |
7048 | BTF_KFUNC_TYPE_RET_NULL, func_id)) { | |
7049 | regs[BPF_REG_0].type |= PTR_MAYBE_NULL; | |
7050 | /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ | |
7051 | regs[BPF_REG_0].id = ++env->id_gen; | |
7052 | } | |
e6ac2450 | 7053 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
5c073f26 KKD |
7054 | if (acq) { |
7055 | int id = acquire_reference_state(env, insn_idx); | |
7056 | ||
7057 | if (id < 0) | |
7058 | return id; | |
7059 | regs[BPF_REG_0].id = id; | |
7060 | regs[BPF_REG_0].ref_obj_id = id; | |
7061 | } | |
e6ac2450 MKL |
7062 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ |
7063 | ||
7064 | nargs = btf_type_vlen(func_proto); | |
7065 | args = (const struct btf_param *)(func_proto + 1); | |
7066 | for (i = 0; i < nargs; i++) { | |
7067 | u32 regno = i + 1; | |
7068 | ||
2357672c | 7069 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
e6ac2450 MKL |
7070 | if (btf_type_is_ptr(t)) |
7071 | mark_btf_func_reg_size(env, regno, sizeof(void *)); | |
7072 | else | |
7073 | /* scalar. ensured by btf_check_kfunc_arg_match() */ | |
7074 | mark_btf_func_reg_size(env, regno, t->size); | |
7075 | } | |
7076 | ||
7077 | return 0; | |
7078 | } | |
7079 | ||
b03c9f9f EC |
7080 | static bool signed_add_overflows(s64 a, s64 b) |
7081 | { | |
7082 | /* Do the add in u64, where overflow is well-defined */ | |
7083 | s64 res = (s64)((u64)a + (u64)b); | |
7084 | ||
7085 | if (b < 0) | |
7086 | return res > a; | |
7087 | return res < a; | |
7088 | } | |
7089 | ||
bc895e8b | 7090 | static bool signed_add32_overflows(s32 a, s32 b) |
3f50f132 JF |
7091 | { |
7092 | /* Do the add in u32, where overflow is well-defined */ | |
7093 | s32 res = (s32)((u32)a + (u32)b); | |
7094 | ||
7095 | if (b < 0) | |
7096 | return res > a; | |
7097 | return res < a; | |
7098 | } | |
7099 | ||
bc895e8b | 7100 | static bool signed_sub_overflows(s64 a, s64 b) |
b03c9f9f EC |
7101 | { |
7102 | /* Do the sub in u64, where overflow is well-defined */ | |
7103 | s64 res = (s64)((u64)a - (u64)b); | |
7104 | ||
7105 | if (b < 0) | |
7106 | return res < a; | |
7107 | return res > a; | |
969bf05e AS |
7108 | } |
7109 | ||
3f50f132 JF |
7110 | static bool signed_sub32_overflows(s32 a, s32 b) |
7111 | { | |
bc895e8b | 7112 | /* Do the sub in u32, where overflow is well-defined */ |
3f50f132 JF |
7113 | s32 res = (s32)((u32)a - (u32)b); |
7114 | ||
7115 | if (b < 0) | |
7116 | return res < a; | |
7117 | return res > a; | |
7118 | } | |
7119 | ||
bb7f0f98 AS |
7120 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
7121 | const struct bpf_reg_state *reg, | |
7122 | enum bpf_reg_type type) | |
7123 | { | |
7124 | bool known = tnum_is_const(reg->var_off); | |
7125 | s64 val = reg->var_off.value; | |
7126 | s64 smin = reg->smin_value; | |
7127 | ||
7128 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
7129 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
c25b2ae1 | 7130 | reg_type_str(env, type), val); |
bb7f0f98 AS |
7131 | return false; |
7132 | } | |
7133 | ||
7134 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
7135 | verbose(env, "%s pointer offset %d is not allowed\n", | |
c25b2ae1 | 7136 | reg_type_str(env, type), reg->off); |
bb7f0f98 AS |
7137 | return false; |
7138 | } | |
7139 | ||
7140 | if (smin == S64_MIN) { | |
7141 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
c25b2ae1 | 7142 | reg_type_str(env, type)); |
bb7f0f98 AS |
7143 | return false; |
7144 | } | |
7145 | ||
7146 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
7147 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
c25b2ae1 | 7148 | smin, reg_type_str(env, type)); |
bb7f0f98 AS |
7149 | return false; |
7150 | } | |
7151 | ||
7152 | return true; | |
7153 | } | |
7154 | ||
979d63d5 DB |
7155 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
7156 | { | |
7157 | return &env->insn_aux_data[env->insn_idx]; | |
7158 | } | |
7159 | ||
a6aaece0 DB |
7160 | enum { |
7161 | REASON_BOUNDS = -1, | |
7162 | REASON_TYPE = -2, | |
7163 | REASON_PATHS = -3, | |
7164 | REASON_LIMIT = -4, | |
7165 | REASON_STACK = -5, | |
7166 | }; | |
7167 | ||
979d63d5 | 7168 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
bb01a1bb | 7169 | u32 *alu_limit, bool mask_to_left) |
979d63d5 | 7170 | { |
7fedb63a | 7171 | u32 max = 0, ptr_limit = 0; |
979d63d5 DB |
7172 | |
7173 | switch (ptr_reg->type) { | |
7174 | case PTR_TO_STACK: | |
1b1597e6 | 7175 | /* Offset 0 is out-of-bounds, but acceptable start for the |
7fedb63a DB |
7176 | * left direction, see BPF_REG_FP. Also, unknown scalar |
7177 | * offset where we would need to deal with min/max bounds is | |
7178 | * currently prohibited for unprivileged. | |
1b1597e6 PK |
7179 | */ |
7180 | max = MAX_BPF_STACK + mask_to_left; | |
7fedb63a | 7181 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
b658bbb8 | 7182 | break; |
979d63d5 | 7183 | case PTR_TO_MAP_VALUE: |
1b1597e6 | 7184 | max = ptr_reg->map_ptr->value_size; |
7fedb63a DB |
7185 | ptr_limit = (mask_to_left ? |
7186 | ptr_reg->smin_value : | |
7187 | ptr_reg->umax_value) + ptr_reg->off; | |
b658bbb8 | 7188 | break; |
979d63d5 | 7189 | default: |
a6aaece0 | 7190 | return REASON_TYPE; |
979d63d5 | 7191 | } |
b658bbb8 DB |
7192 | |
7193 | if (ptr_limit >= max) | |
a6aaece0 | 7194 | return REASON_LIMIT; |
b658bbb8 DB |
7195 | *alu_limit = ptr_limit; |
7196 | return 0; | |
979d63d5 DB |
7197 | } |
7198 | ||
d3bd7413 DB |
7199 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
7200 | const struct bpf_insn *insn) | |
7201 | { | |
2c78ee89 | 7202 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
7203 | } |
7204 | ||
7205 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
7206 | u32 alu_state, u32 alu_limit) | |
7207 | { | |
7208 | /* If we arrived here from different branches with different | |
7209 | * state or limits to sanitize, then this won't work. | |
7210 | */ | |
7211 | if (aux->alu_state && | |
7212 | (aux->alu_state != alu_state || | |
7213 | aux->alu_limit != alu_limit)) | |
a6aaece0 | 7214 | return REASON_PATHS; |
d3bd7413 | 7215 | |
e6ac5933 | 7216 | /* Corresponding fixup done in do_misc_fixups(). */ |
d3bd7413 DB |
7217 | aux->alu_state = alu_state; |
7218 | aux->alu_limit = alu_limit; | |
7219 | return 0; | |
7220 | } | |
7221 | ||
7222 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
7223 | struct bpf_insn *insn) | |
7224 | { | |
7225 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
7226 | ||
7227 | if (can_skip_alu_sanitation(env, insn)) | |
7228 | return 0; | |
7229 | ||
7230 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
7231 | } | |
7232 | ||
f5288193 DB |
7233 | static bool sanitize_needed(u8 opcode) |
7234 | { | |
7235 | return opcode == BPF_ADD || opcode == BPF_SUB; | |
7236 | } | |
7237 | ||
3d0220f6 DB |
7238 | struct bpf_sanitize_info { |
7239 | struct bpf_insn_aux_data aux; | |
bb01a1bb | 7240 | bool mask_to_left; |
3d0220f6 DB |
7241 | }; |
7242 | ||
9183671a DB |
7243 | static struct bpf_verifier_state * |
7244 | sanitize_speculative_path(struct bpf_verifier_env *env, | |
7245 | const struct bpf_insn *insn, | |
7246 | u32 next_idx, u32 curr_idx) | |
7247 | { | |
7248 | struct bpf_verifier_state *branch; | |
7249 | struct bpf_reg_state *regs; | |
7250 | ||
7251 | branch = push_stack(env, next_idx, curr_idx, true); | |
7252 | if (branch && insn) { | |
7253 | regs = branch->frame[branch->curframe]->regs; | |
7254 | if (BPF_SRC(insn->code) == BPF_K) { | |
7255 | mark_reg_unknown(env, regs, insn->dst_reg); | |
7256 | } else if (BPF_SRC(insn->code) == BPF_X) { | |
7257 | mark_reg_unknown(env, regs, insn->dst_reg); | |
7258 | mark_reg_unknown(env, regs, insn->src_reg); | |
7259 | } | |
7260 | } | |
7261 | return branch; | |
7262 | } | |
7263 | ||
979d63d5 DB |
7264 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
7265 | struct bpf_insn *insn, | |
7266 | const struct bpf_reg_state *ptr_reg, | |
6f55b2f2 | 7267 | const struct bpf_reg_state *off_reg, |
979d63d5 | 7268 | struct bpf_reg_state *dst_reg, |
3d0220f6 | 7269 | struct bpf_sanitize_info *info, |
7fedb63a | 7270 | const bool commit_window) |
979d63d5 | 7271 | { |
3d0220f6 | 7272 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
979d63d5 | 7273 | struct bpf_verifier_state *vstate = env->cur_state; |
801c6058 | 7274 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
6f55b2f2 | 7275 | bool off_is_neg = off_reg->smin_value < 0; |
979d63d5 DB |
7276 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
7277 | u8 opcode = BPF_OP(insn->code); | |
7278 | u32 alu_state, alu_limit; | |
7279 | struct bpf_reg_state tmp; | |
7280 | bool ret; | |
f232326f | 7281 | int err; |
979d63d5 | 7282 | |
d3bd7413 | 7283 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
7284 | return 0; |
7285 | ||
7286 | /* We already marked aux for masking from non-speculative | |
7287 | * paths, thus we got here in the first place. We only care | |
7288 | * to explore bad access from here. | |
7289 | */ | |
7290 | if (vstate->speculative) | |
7291 | goto do_sim; | |
7292 | ||
bb01a1bb DB |
7293 | if (!commit_window) { |
7294 | if (!tnum_is_const(off_reg->var_off) && | |
7295 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) | |
7296 | return REASON_BOUNDS; | |
7297 | ||
7298 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
7299 | (opcode == BPF_SUB && !off_is_neg); | |
7300 | } | |
7301 | ||
7302 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); | |
f232326f PK |
7303 | if (err < 0) |
7304 | return err; | |
7305 | ||
7fedb63a DB |
7306 | if (commit_window) { |
7307 | /* In commit phase we narrow the masking window based on | |
7308 | * the observed pointer move after the simulated operation. | |
7309 | */ | |
3d0220f6 DB |
7310 | alu_state = info->aux.alu_state; |
7311 | alu_limit = abs(info->aux.alu_limit - alu_limit); | |
7fedb63a DB |
7312 | } else { |
7313 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
801c6058 | 7314 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
7fedb63a DB |
7315 | alu_state |= ptr_is_dst_reg ? |
7316 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
e042aa53 DB |
7317 | |
7318 | /* Limit pruning on unknown scalars to enable deep search for | |
7319 | * potential masking differences from other program paths. | |
7320 | */ | |
7321 | if (!off_is_imm) | |
7322 | env->explore_alu_limits = true; | |
7fedb63a DB |
7323 | } |
7324 | ||
f232326f PK |
7325 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
7326 | if (err < 0) | |
7327 | return err; | |
979d63d5 | 7328 | do_sim: |
7fedb63a DB |
7329 | /* If we're in commit phase, we're done here given we already |
7330 | * pushed the truncated dst_reg into the speculative verification | |
7331 | * stack. | |
a7036191 DB |
7332 | * |
7333 | * Also, when register is a known constant, we rewrite register-based | |
7334 | * operation to immediate-based, and thus do not need masking (and as | |
7335 | * a consequence, do not need to simulate the zero-truncation either). | |
7fedb63a | 7336 | */ |
a7036191 | 7337 | if (commit_window || off_is_imm) |
7fedb63a DB |
7338 | return 0; |
7339 | ||
979d63d5 DB |
7340 | /* Simulate and find potential out-of-bounds access under |
7341 | * speculative execution from truncation as a result of | |
7342 | * masking when off was not within expected range. If off | |
7343 | * sits in dst, then we temporarily need to move ptr there | |
7344 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
7345 | * for cases where we use K-based arithmetic in one direction | |
7346 | * and truncated reg-based in the other in order to explore | |
7347 | * bad access. | |
7348 | */ | |
7349 | if (!ptr_is_dst_reg) { | |
7350 | tmp = *dst_reg; | |
7351 | *dst_reg = *ptr_reg; | |
7352 | } | |
9183671a DB |
7353 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
7354 | env->insn_idx); | |
0803278b | 7355 | if (!ptr_is_dst_reg && ret) |
979d63d5 | 7356 | *dst_reg = tmp; |
a6aaece0 DB |
7357 | return !ret ? REASON_STACK : 0; |
7358 | } | |
7359 | ||
fe9a5ca7 DB |
7360 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
7361 | { | |
7362 | struct bpf_verifier_state *vstate = env->cur_state; | |
7363 | ||
7364 | /* If we simulate paths under speculation, we don't update the | |
7365 | * insn as 'seen' such that when we verify unreachable paths in | |
7366 | * the non-speculative domain, sanitize_dead_code() can still | |
7367 | * rewrite/sanitize them. | |
7368 | */ | |
7369 | if (!vstate->speculative) | |
7370 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; | |
7371 | } | |
7372 | ||
a6aaece0 DB |
7373 | static int sanitize_err(struct bpf_verifier_env *env, |
7374 | const struct bpf_insn *insn, int reason, | |
7375 | const struct bpf_reg_state *off_reg, | |
7376 | const struct bpf_reg_state *dst_reg) | |
7377 | { | |
7378 | static const char *err = "pointer arithmetic with it prohibited for !root"; | |
7379 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; | |
7380 | u32 dst = insn->dst_reg, src = insn->src_reg; | |
7381 | ||
7382 | switch (reason) { | |
7383 | case REASON_BOUNDS: | |
7384 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", | |
7385 | off_reg == dst_reg ? dst : src, err); | |
7386 | break; | |
7387 | case REASON_TYPE: | |
7388 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", | |
7389 | off_reg == dst_reg ? src : dst, err); | |
7390 | break; | |
7391 | case REASON_PATHS: | |
7392 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", | |
7393 | dst, op, err); | |
7394 | break; | |
7395 | case REASON_LIMIT: | |
7396 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", | |
7397 | dst, op, err); | |
7398 | break; | |
7399 | case REASON_STACK: | |
7400 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", | |
7401 | dst, err); | |
7402 | break; | |
7403 | default: | |
7404 | verbose(env, "verifier internal error: unknown reason (%d)\n", | |
7405 | reason); | |
7406 | break; | |
7407 | } | |
7408 | ||
7409 | return -EACCES; | |
979d63d5 DB |
7410 | } |
7411 | ||
01f810ac AM |
7412 | /* check that stack access falls within stack limits and that 'reg' doesn't |
7413 | * have a variable offset. | |
7414 | * | |
7415 | * Variable offset is prohibited for unprivileged mode for simplicity since it | |
7416 | * requires corresponding support in Spectre masking for stack ALU. See also | |
7417 | * retrieve_ptr_limit(). | |
7418 | * | |
7419 | * | |
7420 | * 'off' includes 'reg->off'. | |
7421 | */ | |
7422 | static int check_stack_access_for_ptr_arithmetic( | |
7423 | struct bpf_verifier_env *env, | |
7424 | int regno, | |
7425 | const struct bpf_reg_state *reg, | |
7426 | int off) | |
7427 | { | |
7428 | if (!tnum_is_const(reg->var_off)) { | |
7429 | char tn_buf[48]; | |
7430 | ||
7431 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
7432 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", | |
7433 | regno, tn_buf, off); | |
7434 | return -EACCES; | |
7435 | } | |
7436 | ||
7437 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
7438 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
7439 | "prohibited for !root; off=%d\n", regno, off); | |
7440 | return -EACCES; | |
7441 | } | |
7442 | ||
7443 | return 0; | |
7444 | } | |
7445 | ||
073815b7 DB |
7446 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
7447 | const struct bpf_insn *insn, | |
7448 | const struct bpf_reg_state *dst_reg) | |
7449 | { | |
7450 | u32 dst = insn->dst_reg; | |
7451 | ||
7452 | /* For unprivileged we require that resulting offset must be in bounds | |
7453 | * in order to be able to sanitize access later on. | |
7454 | */ | |
7455 | if (env->bypass_spec_v1) | |
7456 | return 0; | |
7457 | ||
7458 | switch (dst_reg->type) { | |
7459 | case PTR_TO_STACK: | |
7460 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, | |
7461 | dst_reg->off + dst_reg->var_off.value)) | |
7462 | return -EACCES; | |
7463 | break; | |
7464 | case PTR_TO_MAP_VALUE: | |
7465 | if (check_map_access(env, dst, dst_reg->off, 1, false)) { | |
7466 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
7467 | "prohibited for !root\n", dst); | |
7468 | return -EACCES; | |
7469 | } | |
7470 | break; | |
7471 | default: | |
7472 | break; | |
7473 | } | |
7474 | ||
7475 | return 0; | |
7476 | } | |
01f810ac | 7477 | |
f1174f77 | 7478 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
7479 | * Caller should also handle BPF_MOV case separately. |
7480 | * If we return -EACCES, caller may want to try again treating pointer as a | |
7481 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
7482 | */ | |
7483 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
7484 | struct bpf_insn *insn, | |
7485 | const struct bpf_reg_state *ptr_reg, | |
7486 | const struct bpf_reg_state *off_reg) | |
969bf05e | 7487 | { |
f4d7e40a AS |
7488 | struct bpf_verifier_state *vstate = env->cur_state; |
7489 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
7490 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 7491 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
7492 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
7493 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
7494 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
7495 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
3d0220f6 | 7496 | struct bpf_sanitize_info info = {}; |
969bf05e | 7497 | u8 opcode = BPF_OP(insn->code); |
24c109bb | 7498 | u32 dst = insn->dst_reg; |
979d63d5 | 7499 | int ret; |
969bf05e | 7500 | |
f1174f77 | 7501 | dst_reg = ®s[dst]; |
969bf05e | 7502 | |
6f16101e DB |
7503 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
7504 | smin_val > smax_val || umin_val > umax_val) { | |
7505 | /* Taint dst register if offset had invalid bounds derived from | |
7506 | * e.g. dead branches. | |
7507 | */ | |
f54c7898 | 7508 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 7509 | return 0; |
f1174f77 EC |
7510 | } |
7511 | ||
7512 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
7513 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
7514 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
7515 | __mark_reg_unknown(env, dst_reg); | |
7516 | return 0; | |
7517 | } | |
7518 | ||
82abbf8d AS |
7519 | verbose(env, |
7520 | "R%d 32-bit pointer arithmetic prohibited\n", | |
7521 | dst); | |
f1174f77 | 7522 | return -EACCES; |
969bf05e AS |
7523 | } |
7524 | ||
c25b2ae1 | 7525 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
aad2eeaf | 7526 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
c25b2ae1 | 7527 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 7528 | return -EACCES; |
c25b2ae1 HL |
7529 | } |
7530 | ||
7531 | switch (base_type(ptr_reg->type)) { | |
aad2eeaf | 7532 | case CONST_PTR_TO_MAP: |
7c696732 YS |
7533 | /* smin_val represents the known value */ |
7534 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
7535 | break; | |
8731745e | 7536 | fallthrough; |
aad2eeaf | 7537 | case PTR_TO_PACKET_END: |
c64b7983 | 7538 | case PTR_TO_SOCKET: |
46f8bc92 | 7539 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 7540 | case PTR_TO_TCP_SOCK: |
fada7fdc | 7541 | case PTR_TO_XDP_SOCK: |
aad2eeaf | 7542 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
c25b2ae1 | 7543 | dst, reg_type_str(env, ptr_reg->type)); |
f1174f77 | 7544 | return -EACCES; |
aad2eeaf JS |
7545 | default: |
7546 | break; | |
f1174f77 EC |
7547 | } |
7548 | ||
7549 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
7550 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 7551 | */ |
f1174f77 EC |
7552 | dst_reg->type = ptr_reg->type; |
7553 | dst_reg->id = ptr_reg->id; | |
969bf05e | 7554 | |
bb7f0f98 AS |
7555 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
7556 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
7557 | return -EINVAL; | |
7558 | ||
3f50f132 JF |
7559 | /* pointer types do not carry 32-bit bounds at the moment. */ |
7560 | __mark_reg32_unbounded(dst_reg); | |
7561 | ||
7fedb63a DB |
7562 | if (sanitize_needed(opcode)) { |
7563 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, | |
3d0220f6 | 7564 | &info, false); |
a6aaece0 DB |
7565 | if (ret < 0) |
7566 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
7fedb63a | 7567 | } |
a6aaece0 | 7568 | |
f1174f77 EC |
7569 | switch (opcode) { |
7570 | case BPF_ADD: | |
7571 | /* We can take a fixed offset as long as it doesn't overflow | |
7572 | * the s32 'off' field | |
969bf05e | 7573 | */ |
b03c9f9f EC |
7574 | if (known && (ptr_reg->off + smin_val == |
7575 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 7576 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
7577 | dst_reg->smin_value = smin_ptr; |
7578 | dst_reg->smax_value = smax_ptr; | |
7579 | dst_reg->umin_value = umin_ptr; | |
7580 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 7581 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 7582 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 7583 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
7584 | break; |
7585 | } | |
f1174f77 EC |
7586 | /* A new variable offset is created. Note that off_reg->off |
7587 | * == 0, since it's a scalar. | |
7588 | * dst_reg gets the pointer type and since some positive | |
7589 | * integer value was added to the pointer, give it a new 'id' | |
7590 | * if it's a PTR_TO_PACKET. | |
7591 | * this creates a new 'base' pointer, off_reg (variable) gets | |
7592 | * added into the variable offset, and we copy the fixed offset | |
7593 | * from ptr_reg. | |
969bf05e | 7594 | */ |
b03c9f9f EC |
7595 | if (signed_add_overflows(smin_ptr, smin_val) || |
7596 | signed_add_overflows(smax_ptr, smax_val)) { | |
7597 | dst_reg->smin_value = S64_MIN; | |
7598 | dst_reg->smax_value = S64_MAX; | |
7599 | } else { | |
7600 | dst_reg->smin_value = smin_ptr + smin_val; | |
7601 | dst_reg->smax_value = smax_ptr + smax_val; | |
7602 | } | |
7603 | if (umin_ptr + umin_val < umin_ptr || | |
7604 | umax_ptr + umax_val < umax_ptr) { | |
7605 | dst_reg->umin_value = 0; | |
7606 | dst_reg->umax_value = U64_MAX; | |
7607 | } else { | |
7608 | dst_reg->umin_value = umin_ptr + umin_val; | |
7609 | dst_reg->umax_value = umax_ptr + umax_val; | |
7610 | } | |
f1174f77 EC |
7611 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
7612 | dst_reg->off = ptr_reg->off; | |
0962590e | 7613 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 7614 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
7615 | dst_reg->id = ++env->id_gen; |
7616 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 7617 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
7618 | } |
7619 | break; | |
7620 | case BPF_SUB: | |
7621 | if (dst_reg == off_reg) { | |
7622 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
7623 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
7624 | dst); | |
f1174f77 EC |
7625 | return -EACCES; |
7626 | } | |
7627 | /* We don't allow subtraction from FP, because (according to | |
7628 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
7629 | * be able to deal with it. | |
969bf05e | 7630 | */ |
f1174f77 | 7631 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
7632 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
7633 | dst); | |
f1174f77 EC |
7634 | return -EACCES; |
7635 | } | |
b03c9f9f EC |
7636 | if (known && (ptr_reg->off - smin_val == |
7637 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 7638 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
7639 | dst_reg->smin_value = smin_ptr; |
7640 | dst_reg->smax_value = smax_ptr; | |
7641 | dst_reg->umin_value = umin_ptr; | |
7642 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
7643 | dst_reg->var_off = ptr_reg->var_off; |
7644 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 7645 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 7646 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
7647 | break; |
7648 | } | |
f1174f77 EC |
7649 | /* A new variable offset is created. If the subtrahend is known |
7650 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 7651 | */ |
b03c9f9f EC |
7652 | if (signed_sub_overflows(smin_ptr, smax_val) || |
7653 | signed_sub_overflows(smax_ptr, smin_val)) { | |
7654 | /* Overflow possible, we know nothing */ | |
7655 | dst_reg->smin_value = S64_MIN; | |
7656 | dst_reg->smax_value = S64_MAX; | |
7657 | } else { | |
7658 | dst_reg->smin_value = smin_ptr - smax_val; | |
7659 | dst_reg->smax_value = smax_ptr - smin_val; | |
7660 | } | |
7661 | if (umin_ptr < umax_val) { | |
7662 | /* Overflow possible, we know nothing */ | |
7663 | dst_reg->umin_value = 0; | |
7664 | dst_reg->umax_value = U64_MAX; | |
7665 | } else { | |
7666 | /* Cannot overflow (as long as bounds are consistent) */ | |
7667 | dst_reg->umin_value = umin_ptr - umax_val; | |
7668 | dst_reg->umax_value = umax_ptr - umin_val; | |
7669 | } | |
f1174f77 EC |
7670 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
7671 | dst_reg->off = ptr_reg->off; | |
0962590e | 7672 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 7673 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
7674 | dst_reg->id = ++env->id_gen; |
7675 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 7676 | if (smin_val < 0) |
22dc4a0f | 7677 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 7678 | } |
f1174f77 EC |
7679 | break; |
7680 | case BPF_AND: | |
7681 | case BPF_OR: | |
7682 | case BPF_XOR: | |
82abbf8d AS |
7683 | /* bitwise ops on pointers are troublesome, prohibit. */ |
7684 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
7685 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
7686 | return -EACCES; |
7687 | default: | |
7688 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
7689 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
7690 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 7691 | return -EACCES; |
43188702 JF |
7692 | } |
7693 | ||
bb7f0f98 AS |
7694 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
7695 | return -EINVAL; | |
7696 | ||
b03c9f9f EC |
7697 | __update_reg_bounds(dst_reg); |
7698 | __reg_deduce_bounds(dst_reg); | |
7699 | __reg_bound_offset(dst_reg); | |
0d6303db | 7700 | |
073815b7 DB |
7701 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
7702 | return -EACCES; | |
7fedb63a DB |
7703 | if (sanitize_needed(opcode)) { |
7704 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, | |
3d0220f6 | 7705 | &info, true); |
7fedb63a DB |
7706 | if (ret < 0) |
7707 | return sanitize_err(env, insn, ret, off_reg, dst_reg); | |
0d6303db DB |
7708 | } |
7709 | ||
43188702 JF |
7710 | return 0; |
7711 | } | |
7712 | ||
3f50f132 JF |
7713 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
7714 | struct bpf_reg_state *src_reg) | |
7715 | { | |
7716 | s32 smin_val = src_reg->s32_min_value; | |
7717 | s32 smax_val = src_reg->s32_max_value; | |
7718 | u32 umin_val = src_reg->u32_min_value; | |
7719 | u32 umax_val = src_reg->u32_max_value; | |
7720 | ||
7721 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
7722 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
7723 | dst_reg->s32_min_value = S32_MIN; | |
7724 | dst_reg->s32_max_value = S32_MAX; | |
7725 | } else { | |
7726 | dst_reg->s32_min_value += smin_val; | |
7727 | dst_reg->s32_max_value += smax_val; | |
7728 | } | |
7729 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
7730 | dst_reg->u32_max_value + umax_val < umax_val) { | |
7731 | dst_reg->u32_min_value = 0; | |
7732 | dst_reg->u32_max_value = U32_MAX; | |
7733 | } else { | |
7734 | dst_reg->u32_min_value += umin_val; | |
7735 | dst_reg->u32_max_value += umax_val; | |
7736 | } | |
7737 | } | |
7738 | ||
07cd2631 JF |
7739 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
7740 | struct bpf_reg_state *src_reg) | |
7741 | { | |
7742 | s64 smin_val = src_reg->smin_value; | |
7743 | s64 smax_val = src_reg->smax_value; | |
7744 | u64 umin_val = src_reg->umin_value; | |
7745 | u64 umax_val = src_reg->umax_value; | |
7746 | ||
7747 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
7748 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
7749 | dst_reg->smin_value = S64_MIN; | |
7750 | dst_reg->smax_value = S64_MAX; | |
7751 | } else { | |
7752 | dst_reg->smin_value += smin_val; | |
7753 | dst_reg->smax_value += smax_val; | |
7754 | } | |
7755 | if (dst_reg->umin_value + umin_val < umin_val || | |
7756 | dst_reg->umax_value + umax_val < umax_val) { | |
7757 | dst_reg->umin_value = 0; | |
7758 | dst_reg->umax_value = U64_MAX; | |
7759 | } else { | |
7760 | dst_reg->umin_value += umin_val; | |
7761 | dst_reg->umax_value += umax_val; | |
7762 | } | |
3f50f132 JF |
7763 | } |
7764 | ||
7765 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
7766 | struct bpf_reg_state *src_reg) | |
7767 | { | |
7768 | s32 smin_val = src_reg->s32_min_value; | |
7769 | s32 smax_val = src_reg->s32_max_value; | |
7770 | u32 umin_val = src_reg->u32_min_value; | |
7771 | u32 umax_val = src_reg->u32_max_value; | |
7772 | ||
7773 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
7774 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
7775 | /* Overflow possible, we know nothing */ | |
7776 | dst_reg->s32_min_value = S32_MIN; | |
7777 | dst_reg->s32_max_value = S32_MAX; | |
7778 | } else { | |
7779 | dst_reg->s32_min_value -= smax_val; | |
7780 | dst_reg->s32_max_value -= smin_val; | |
7781 | } | |
7782 | if (dst_reg->u32_min_value < umax_val) { | |
7783 | /* Overflow possible, we know nothing */ | |
7784 | dst_reg->u32_min_value = 0; | |
7785 | dst_reg->u32_max_value = U32_MAX; | |
7786 | } else { | |
7787 | /* Cannot overflow (as long as bounds are consistent) */ | |
7788 | dst_reg->u32_min_value -= umax_val; | |
7789 | dst_reg->u32_max_value -= umin_val; | |
7790 | } | |
07cd2631 JF |
7791 | } |
7792 | ||
7793 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
7794 | struct bpf_reg_state *src_reg) | |
7795 | { | |
7796 | s64 smin_val = src_reg->smin_value; | |
7797 | s64 smax_val = src_reg->smax_value; | |
7798 | u64 umin_val = src_reg->umin_value; | |
7799 | u64 umax_val = src_reg->umax_value; | |
7800 | ||
7801 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
7802 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
7803 | /* Overflow possible, we know nothing */ | |
7804 | dst_reg->smin_value = S64_MIN; | |
7805 | dst_reg->smax_value = S64_MAX; | |
7806 | } else { | |
7807 | dst_reg->smin_value -= smax_val; | |
7808 | dst_reg->smax_value -= smin_val; | |
7809 | } | |
7810 | if (dst_reg->umin_value < umax_val) { | |
7811 | /* Overflow possible, we know nothing */ | |
7812 | dst_reg->umin_value = 0; | |
7813 | dst_reg->umax_value = U64_MAX; | |
7814 | } else { | |
7815 | /* Cannot overflow (as long as bounds are consistent) */ | |
7816 | dst_reg->umin_value -= umax_val; | |
7817 | dst_reg->umax_value -= umin_val; | |
7818 | } | |
3f50f132 JF |
7819 | } |
7820 | ||
7821 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
7822 | struct bpf_reg_state *src_reg) | |
7823 | { | |
7824 | s32 smin_val = src_reg->s32_min_value; | |
7825 | u32 umin_val = src_reg->u32_min_value; | |
7826 | u32 umax_val = src_reg->u32_max_value; | |
7827 | ||
7828 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
7829 | /* Ain't nobody got time to multiply that sign */ | |
7830 | __mark_reg32_unbounded(dst_reg); | |
7831 | return; | |
7832 | } | |
7833 | /* Both values are positive, so we can work with unsigned and | |
7834 | * copy the result to signed (unless it exceeds S32_MAX). | |
7835 | */ | |
7836 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
7837 | /* Potential overflow, we know nothing */ | |
7838 | __mark_reg32_unbounded(dst_reg); | |
7839 | return; | |
7840 | } | |
7841 | dst_reg->u32_min_value *= umin_val; | |
7842 | dst_reg->u32_max_value *= umax_val; | |
7843 | if (dst_reg->u32_max_value > S32_MAX) { | |
7844 | /* Overflow possible, we know nothing */ | |
7845 | dst_reg->s32_min_value = S32_MIN; | |
7846 | dst_reg->s32_max_value = S32_MAX; | |
7847 | } else { | |
7848 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7849 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7850 | } | |
07cd2631 JF |
7851 | } |
7852 | ||
7853 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
7854 | struct bpf_reg_state *src_reg) | |
7855 | { | |
7856 | s64 smin_val = src_reg->smin_value; | |
7857 | u64 umin_val = src_reg->umin_value; | |
7858 | u64 umax_val = src_reg->umax_value; | |
7859 | ||
07cd2631 JF |
7860 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
7861 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 7862 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
7863 | return; |
7864 | } | |
7865 | /* Both values are positive, so we can work with unsigned and | |
7866 | * copy the result to signed (unless it exceeds S64_MAX). | |
7867 | */ | |
7868 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
7869 | /* Potential overflow, we know nothing */ | |
3f50f132 | 7870 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
7871 | return; |
7872 | } | |
7873 | dst_reg->umin_value *= umin_val; | |
7874 | dst_reg->umax_value *= umax_val; | |
7875 | if (dst_reg->umax_value > S64_MAX) { | |
7876 | /* Overflow possible, we know nothing */ | |
7877 | dst_reg->smin_value = S64_MIN; | |
7878 | dst_reg->smax_value = S64_MAX; | |
7879 | } else { | |
7880 | dst_reg->smin_value = dst_reg->umin_value; | |
7881 | dst_reg->smax_value = dst_reg->umax_value; | |
7882 | } | |
7883 | } | |
7884 | ||
3f50f132 JF |
7885 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
7886 | struct bpf_reg_state *src_reg) | |
7887 | { | |
7888 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7889 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7890 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
7891 | s32 smin_val = src_reg->s32_min_value; | |
7892 | u32 umax_val = src_reg->u32_max_value; | |
7893 | ||
049c4e13 DB |
7894 | if (src_known && dst_known) { |
7895 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 7896 | return; |
049c4e13 | 7897 | } |
3f50f132 JF |
7898 | |
7899 | /* We get our minimum from the var_off, since that's inherently | |
7900 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
7901 | */ | |
7902 | dst_reg->u32_min_value = var32_off.value; | |
7903 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
7904 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
7905 | /* Lose signed bounds when ANDing negative numbers, | |
7906 | * ain't nobody got time for that. | |
7907 | */ | |
7908 | dst_reg->s32_min_value = S32_MIN; | |
7909 | dst_reg->s32_max_value = S32_MAX; | |
7910 | } else { | |
7911 | /* ANDing two positives gives a positive, so safe to | |
7912 | * cast result into s64. | |
7913 | */ | |
7914 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
7915 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
7916 | } | |
3f50f132 JF |
7917 | } |
7918 | ||
07cd2631 JF |
7919 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
7920 | struct bpf_reg_state *src_reg) | |
7921 | { | |
3f50f132 JF |
7922 | bool src_known = tnum_is_const(src_reg->var_off); |
7923 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
7924 | s64 smin_val = src_reg->smin_value; |
7925 | u64 umax_val = src_reg->umax_value; | |
7926 | ||
3f50f132 | 7927 | if (src_known && dst_known) { |
4fbb38a3 | 7928 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
7929 | return; |
7930 | } | |
7931 | ||
07cd2631 JF |
7932 | /* We get our minimum from the var_off, since that's inherently |
7933 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
7934 | */ | |
07cd2631 JF |
7935 | dst_reg->umin_value = dst_reg->var_off.value; |
7936 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
7937 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
7938 | /* Lose signed bounds when ANDing negative numbers, | |
7939 | * ain't nobody got time for that. | |
7940 | */ | |
7941 | dst_reg->smin_value = S64_MIN; | |
7942 | dst_reg->smax_value = S64_MAX; | |
7943 | } else { | |
7944 | /* ANDing two positives gives a positive, so safe to | |
7945 | * cast result into s64. | |
7946 | */ | |
7947 | dst_reg->smin_value = dst_reg->umin_value; | |
7948 | dst_reg->smax_value = dst_reg->umax_value; | |
7949 | } | |
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_or(struct bpf_reg_state *dst_reg, |
7955 | struct bpf_reg_state *src_reg) | |
7956 | { | |
7957 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
7958 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
7959 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
7960 | s32 smin_val = src_reg->s32_min_value; |
7961 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 | 7962 | |
049c4e13 DB |
7963 | if (src_known && dst_known) { |
7964 | __mark_reg32_known(dst_reg, var32_off.value); | |
3f50f132 | 7965 | return; |
049c4e13 | 7966 | } |
3f50f132 JF |
7967 | |
7968 | /* We get our maximum from the var_off, and our minimum is the | |
7969 | * maximum of the operands' minima | |
7970 | */ | |
7971 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
7972 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
7973 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
7974 | /* Lose signed bounds when ORing negative numbers, | |
7975 | * ain't nobody got time for that. | |
7976 | */ | |
7977 | dst_reg->s32_min_value = S32_MIN; | |
7978 | dst_reg->s32_max_value = S32_MAX; | |
7979 | } else { | |
7980 | /* ORing two positives gives a positive, so safe to | |
7981 | * cast result into s64. | |
7982 | */ | |
5b9fbeb7 DB |
7983 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
7984 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
7985 | } |
7986 | } | |
7987 | ||
07cd2631 JF |
7988 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
7989 | struct bpf_reg_state *src_reg) | |
7990 | { | |
3f50f132 JF |
7991 | bool src_known = tnum_is_const(src_reg->var_off); |
7992 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
7993 | s64 smin_val = src_reg->smin_value; |
7994 | u64 umin_val = src_reg->umin_value; | |
7995 | ||
3f50f132 | 7996 | if (src_known && dst_known) { |
4fbb38a3 | 7997 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
7998 | return; |
7999 | } | |
8000 | ||
07cd2631 JF |
8001 | /* We get our maximum from the var_off, and our minimum is the |
8002 | * maximum of the operands' minima | |
8003 | */ | |
07cd2631 JF |
8004 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
8005 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
8006 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
8007 | /* Lose signed bounds when ORing negative numbers, | |
8008 | * ain't nobody got time for that. | |
8009 | */ | |
8010 | dst_reg->smin_value = S64_MIN; | |
8011 | dst_reg->smax_value = S64_MAX; | |
8012 | } else { | |
8013 | /* ORing two positives gives a positive, so safe to | |
8014 | * cast result into s64. | |
8015 | */ | |
8016 | dst_reg->smin_value = dst_reg->umin_value; | |
8017 | dst_reg->smax_value = dst_reg->umax_value; | |
8018 | } | |
8019 | /* We may learn something more from the var_off */ | |
8020 | __update_reg_bounds(dst_reg); | |
8021 | } | |
8022 | ||
2921c90d YS |
8023 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
8024 | struct bpf_reg_state *src_reg) | |
8025 | { | |
8026 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
8027 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
8028 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
8029 | s32 smin_val = src_reg->s32_min_value; | |
8030 | ||
049c4e13 DB |
8031 | if (src_known && dst_known) { |
8032 | __mark_reg32_known(dst_reg, var32_off.value); | |
2921c90d | 8033 | return; |
049c4e13 | 8034 | } |
2921c90d YS |
8035 | |
8036 | /* We get both minimum and maximum from the var32_off. */ | |
8037 | dst_reg->u32_min_value = var32_off.value; | |
8038 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
8039 | ||
8040 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
8041 | /* XORing two positive sign numbers gives a positive, | |
8042 | * so safe to cast u32 result into s32. | |
8043 | */ | |
8044 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
8045 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
8046 | } else { | |
8047 | dst_reg->s32_min_value = S32_MIN; | |
8048 | dst_reg->s32_max_value = S32_MAX; | |
8049 | } | |
8050 | } | |
8051 | ||
8052 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
8053 | struct bpf_reg_state *src_reg) | |
8054 | { | |
8055 | bool src_known = tnum_is_const(src_reg->var_off); | |
8056 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
8057 | s64 smin_val = src_reg->smin_value; | |
8058 | ||
8059 | if (src_known && dst_known) { | |
8060 | /* dst_reg->var_off.value has been updated earlier */ | |
8061 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
8062 | return; | |
8063 | } | |
8064 | ||
8065 | /* We get both minimum and maximum from the var_off. */ | |
8066 | dst_reg->umin_value = dst_reg->var_off.value; | |
8067 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
8068 | ||
8069 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
8070 | /* XORing two positive sign numbers gives a positive, | |
8071 | * so safe to cast u64 result into s64. | |
8072 | */ | |
8073 | dst_reg->smin_value = dst_reg->umin_value; | |
8074 | dst_reg->smax_value = dst_reg->umax_value; | |
8075 | } else { | |
8076 | dst_reg->smin_value = S64_MIN; | |
8077 | dst_reg->smax_value = S64_MAX; | |
8078 | } | |
8079 | ||
8080 | __update_reg_bounds(dst_reg); | |
8081 | } | |
8082 | ||
3f50f132 JF |
8083 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
8084 | u64 umin_val, u64 umax_val) | |
07cd2631 | 8085 | { |
07cd2631 JF |
8086 | /* We lose all sign bit information (except what we can pick |
8087 | * up from var_off) | |
8088 | */ | |
3f50f132 JF |
8089 | dst_reg->s32_min_value = S32_MIN; |
8090 | dst_reg->s32_max_value = S32_MAX; | |
8091 | /* If we might shift our top bit out, then we know nothing */ | |
8092 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
8093 | dst_reg->u32_min_value = 0; | |
8094 | dst_reg->u32_max_value = U32_MAX; | |
8095 | } else { | |
8096 | dst_reg->u32_min_value <<= umin_val; | |
8097 | dst_reg->u32_max_value <<= umax_val; | |
8098 | } | |
8099 | } | |
8100 | ||
8101 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
8102 | struct bpf_reg_state *src_reg) | |
8103 | { | |
8104 | u32 umax_val = src_reg->u32_max_value; | |
8105 | u32 umin_val = src_reg->u32_min_value; | |
8106 | /* u32 alu operation will zext upper bits */ | |
8107 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
8108 | ||
8109 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
8110 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
8111 | /* Not required but being careful mark reg64 bounds as unknown so | |
8112 | * that we are forced to pick them up from tnum and zext later and | |
8113 | * if some path skips this step we are still safe. | |
8114 | */ | |
8115 | __mark_reg64_unbounded(dst_reg); | |
8116 | __update_reg32_bounds(dst_reg); | |
8117 | } | |
8118 | ||
8119 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
8120 | u64 umin_val, u64 umax_val) | |
8121 | { | |
8122 | /* Special case <<32 because it is a common compiler pattern to sign | |
8123 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
8124 | * positive we know this shift will also be positive so we can track | |
8125 | * bounds correctly. Otherwise we lose all sign bit information except | |
8126 | * what we can pick up from var_off. Perhaps we can generalize this | |
8127 | * later to shifts of any length. | |
8128 | */ | |
8129 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
8130 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
8131 | else | |
8132 | dst_reg->smax_value = S64_MAX; | |
8133 | ||
8134 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
8135 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
8136 | else | |
8137 | dst_reg->smin_value = S64_MIN; | |
8138 | ||
07cd2631 JF |
8139 | /* If we might shift our top bit out, then we know nothing */ |
8140 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
8141 | dst_reg->umin_value = 0; | |
8142 | dst_reg->umax_value = U64_MAX; | |
8143 | } else { | |
8144 | dst_reg->umin_value <<= umin_val; | |
8145 | dst_reg->umax_value <<= umax_val; | |
8146 | } | |
3f50f132 JF |
8147 | } |
8148 | ||
8149 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
8150 | struct bpf_reg_state *src_reg) | |
8151 | { | |
8152 | u64 umax_val = src_reg->umax_value; | |
8153 | u64 umin_val = src_reg->umin_value; | |
8154 | ||
8155 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
8156 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
8157 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
8158 | ||
07cd2631 JF |
8159 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
8160 | /* We may learn something more from the var_off */ | |
8161 | __update_reg_bounds(dst_reg); | |
8162 | } | |
8163 | ||
3f50f132 JF |
8164 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
8165 | struct bpf_reg_state *src_reg) | |
8166 | { | |
8167 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
8168 | u32 umax_val = src_reg->u32_max_value; | |
8169 | u32 umin_val = src_reg->u32_min_value; | |
8170 | ||
8171 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
8172 | * be negative, then either: | |
8173 | * 1) src_reg might be zero, so the sign bit of the result is | |
8174 | * unknown, so we lose our signed bounds | |
8175 | * 2) it's known negative, thus the unsigned bounds capture the | |
8176 | * signed bounds | |
8177 | * 3) the signed bounds cross zero, so they tell us nothing | |
8178 | * about the result | |
8179 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 8180 | * unsigned bounds capture the signed bounds. |
3f50f132 JF |
8181 | * Thus, in all cases it suffices to blow away our signed bounds |
8182 | * and rely on inferring new ones from the unsigned bounds and | |
8183 | * var_off of the result. | |
8184 | */ | |
8185 | dst_reg->s32_min_value = S32_MIN; | |
8186 | dst_reg->s32_max_value = S32_MAX; | |
8187 | ||
8188 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
8189 | dst_reg->u32_min_value >>= umax_val; | |
8190 | dst_reg->u32_max_value >>= umin_val; | |
8191 | ||
8192 | __mark_reg64_unbounded(dst_reg); | |
8193 | __update_reg32_bounds(dst_reg); | |
8194 | } | |
8195 | ||
07cd2631 JF |
8196 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
8197 | struct bpf_reg_state *src_reg) | |
8198 | { | |
8199 | u64 umax_val = src_reg->umax_value; | |
8200 | u64 umin_val = src_reg->umin_value; | |
8201 | ||
8202 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
8203 | * be negative, then either: | |
8204 | * 1) src_reg might be zero, so the sign bit of the result is | |
8205 | * unknown, so we lose our signed bounds | |
8206 | * 2) it's known negative, thus the unsigned bounds capture the | |
8207 | * signed bounds | |
8208 | * 3) the signed bounds cross zero, so they tell us nothing | |
8209 | * about the result | |
8210 | * If the value in dst_reg is known nonnegative, then again the | |
18b24d78 | 8211 | * unsigned bounds capture the signed bounds. |
07cd2631 JF |
8212 | * Thus, in all cases it suffices to blow away our signed bounds |
8213 | * and rely on inferring new ones from the unsigned bounds and | |
8214 | * var_off of the result. | |
8215 | */ | |
8216 | dst_reg->smin_value = S64_MIN; | |
8217 | dst_reg->smax_value = S64_MAX; | |
8218 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
8219 | dst_reg->umin_value >>= umax_val; | |
8220 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
8221 | |
8222 | /* Its not easy to operate on alu32 bounds here because it depends | |
8223 | * on bits being shifted in. Take easy way out and mark unbounded | |
8224 | * so we can recalculate later from tnum. | |
8225 | */ | |
8226 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
8227 | __update_reg_bounds(dst_reg); |
8228 | } | |
8229 | ||
3f50f132 JF |
8230 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
8231 | struct bpf_reg_state *src_reg) | |
07cd2631 | 8232 | { |
3f50f132 | 8233 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
8234 | |
8235 | /* Upon reaching here, src_known is true and | |
8236 | * umax_val is equal to umin_val. | |
8237 | */ | |
3f50f132 JF |
8238 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
8239 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 8240 | |
3f50f132 JF |
8241 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
8242 | ||
8243 | /* blow away the dst_reg umin_value/umax_value and rely on | |
8244 | * dst_reg var_off to refine the result. | |
8245 | */ | |
8246 | dst_reg->u32_min_value = 0; | |
8247 | dst_reg->u32_max_value = U32_MAX; | |
8248 | ||
8249 | __mark_reg64_unbounded(dst_reg); | |
8250 | __update_reg32_bounds(dst_reg); | |
8251 | } | |
8252 | ||
8253 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
8254 | struct bpf_reg_state *src_reg) | |
8255 | { | |
8256 | u64 umin_val = src_reg->umin_value; | |
8257 | ||
8258 | /* Upon reaching here, src_known is true and umax_val is equal | |
8259 | * to umin_val. | |
8260 | */ | |
8261 | dst_reg->smin_value >>= umin_val; | |
8262 | dst_reg->smax_value >>= umin_val; | |
8263 | ||
8264 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
8265 | |
8266 | /* blow away the dst_reg umin_value/umax_value and rely on | |
8267 | * dst_reg var_off to refine the result. | |
8268 | */ | |
8269 | dst_reg->umin_value = 0; | |
8270 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
8271 | |
8272 | /* Its not easy to operate on alu32 bounds here because it depends | |
8273 | * on bits being shifted in from upper 32-bits. Take easy way out | |
8274 | * and mark unbounded so we can recalculate later from tnum. | |
8275 | */ | |
8276 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
8277 | __update_reg_bounds(dst_reg); |
8278 | } | |
8279 | ||
468f6eaf JH |
8280 | /* WARNING: This function does calculations on 64-bit values, but the actual |
8281 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
8282 | * need extra checks in the 32-bit case. | |
8283 | */ | |
f1174f77 EC |
8284 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
8285 | struct bpf_insn *insn, | |
8286 | struct bpf_reg_state *dst_reg, | |
8287 | struct bpf_reg_state src_reg) | |
969bf05e | 8288 | { |
638f5b90 | 8289 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 8290 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 8291 | bool src_known; |
b03c9f9f EC |
8292 | s64 smin_val, smax_val; |
8293 | u64 umin_val, umax_val; | |
3f50f132 JF |
8294 | s32 s32_min_val, s32_max_val; |
8295 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 8296 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
3f50f132 | 8297 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
a6aaece0 | 8298 | int ret; |
b799207e | 8299 | |
b03c9f9f EC |
8300 | smin_val = src_reg.smin_value; |
8301 | smax_val = src_reg.smax_value; | |
8302 | umin_val = src_reg.umin_value; | |
8303 | umax_val = src_reg.umax_value; | |
f23cc643 | 8304 | |
3f50f132 JF |
8305 | s32_min_val = src_reg.s32_min_value; |
8306 | s32_max_val = src_reg.s32_max_value; | |
8307 | u32_min_val = src_reg.u32_min_value; | |
8308 | u32_max_val = src_reg.u32_max_value; | |
8309 | ||
8310 | if (alu32) { | |
8311 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
8312 | if ((src_known && |
8313 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
8314 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
8315 | /* Taint dst register if offset had invalid bounds | |
8316 | * derived from e.g. dead branches. | |
8317 | */ | |
8318 | __mark_reg_unknown(env, dst_reg); | |
8319 | return 0; | |
8320 | } | |
8321 | } else { | |
8322 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
8323 | if ((src_known && |
8324 | (smin_val != smax_val || umin_val != umax_val)) || | |
8325 | smin_val > smax_val || umin_val > umax_val) { | |
8326 | /* Taint dst register if offset had invalid bounds | |
8327 | * derived from e.g. dead branches. | |
8328 | */ | |
8329 | __mark_reg_unknown(env, dst_reg); | |
8330 | return 0; | |
8331 | } | |
6f16101e DB |
8332 | } |
8333 | ||
bb7f0f98 AS |
8334 | if (!src_known && |
8335 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 8336 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
8337 | return 0; |
8338 | } | |
8339 | ||
f5288193 DB |
8340 | if (sanitize_needed(opcode)) { |
8341 | ret = sanitize_val_alu(env, insn); | |
8342 | if (ret < 0) | |
8343 | return sanitize_err(env, insn, ret, NULL, NULL); | |
8344 | } | |
8345 | ||
3f50f132 JF |
8346 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
8347 | * There are two classes of instructions: The first class we track both | |
8348 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
8349 | * greatest amount of precision when alu operations are mixed with jmp32 | |
8350 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
8351 | * and BPF_OR. This is possible because these ops have fairly easy to | |
8352 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
8353 | * See alu32 verifier tests for examples. The second class of | |
8354 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
8355 | * with regards to tracking sign/unsigned bounds because the bits may | |
8356 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
8357 | * the reg unbounded in the subreg bound space and use the resulting | |
8358 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
8359 | */ | |
48461135 JB |
8360 | switch (opcode) { |
8361 | case BPF_ADD: | |
3f50f132 | 8362 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 8363 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 8364 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
8365 | break; |
8366 | case BPF_SUB: | |
3f50f132 | 8367 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 8368 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 8369 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
8370 | break; |
8371 | case BPF_MUL: | |
3f50f132 JF |
8372 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
8373 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 8374 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
8375 | break; |
8376 | case BPF_AND: | |
3f50f132 JF |
8377 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
8378 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 8379 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
8380 | break; |
8381 | case BPF_OR: | |
3f50f132 JF |
8382 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
8383 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 8384 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 8385 | break; |
2921c90d YS |
8386 | case BPF_XOR: |
8387 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
8388 | scalar32_min_max_xor(dst_reg, &src_reg); | |
8389 | scalar_min_max_xor(dst_reg, &src_reg); | |
8390 | break; | |
48461135 | 8391 | case BPF_LSH: |
468f6eaf JH |
8392 | if (umax_val >= insn_bitness) { |
8393 | /* Shifts greater than 31 or 63 are undefined. | |
8394 | * This includes shifts by a negative number. | |
b03c9f9f | 8395 | */ |
61bd5218 | 8396 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
8397 | break; |
8398 | } | |
3f50f132 JF |
8399 | if (alu32) |
8400 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
8401 | else | |
8402 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
8403 | break; |
8404 | case BPF_RSH: | |
468f6eaf JH |
8405 | if (umax_val >= insn_bitness) { |
8406 | /* Shifts greater than 31 or 63 are undefined. | |
8407 | * This includes shifts by a negative number. | |
b03c9f9f | 8408 | */ |
61bd5218 | 8409 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
8410 | break; |
8411 | } | |
3f50f132 JF |
8412 | if (alu32) |
8413 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
8414 | else | |
8415 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 8416 | break; |
9cbe1f5a YS |
8417 | case BPF_ARSH: |
8418 | if (umax_val >= insn_bitness) { | |
8419 | /* Shifts greater than 31 or 63 are undefined. | |
8420 | * This includes shifts by a negative number. | |
8421 | */ | |
8422 | mark_reg_unknown(env, regs, insn->dst_reg); | |
8423 | break; | |
8424 | } | |
3f50f132 JF |
8425 | if (alu32) |
8426 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
8427 | else | |
8428 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 8429 | break; |
48461135 | 8430 | default: |
61bd5218 | 8431 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
8432 | break; |
8433 | } | |
8434 | ||
3f50f132 JF |
8435 | /* ALU32 ops are zero extended into 64bit register */ |
8436 | if (alu32) | |
8437 | zext_32_to_64(dst_reg); | |
468f6eaf | 8438 | |
294f2fc6 | 8439 | __update_reg_bounds(dst_reg); |
b03c9f9f EC |
8440 | __reg_deduce_bounds(dst_reg); |
8441 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
8442 | return 0; |
8443 | } | |
8444 | ||
8445 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
8446 | * and var_off. | |
8447 | */ | |
8448 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
8449 | struct bpf_insn *insn) | |
8450 | { | |
f4d7e40a AS |
8451 | struct bpf_verifier_state *vstate = env->cur_state; |
8452 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
8453 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
8454 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
8455 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 8456 | int err; |
f1174f77 EC |
8457 | |
8458 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
8459 | src_reg = NULL; |
8460 | if (dst_reg->type != SCALAR_VALUE) | |
8461 | ptr_reg = dst_reg; | |
75748837 AS |
8462 | else |
8463 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
8464 | * incorrectly propagated into other registers by find_equal_scalars() | |
8465 | */ | |
8466 | dst_reg->id = 0; | |
f1174f77 EC |
8467 | if (BPF_SRC(insn->code) == BPF_X) { |
8468 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
8469 | if (src_reg->type != SCALAR_VALUE) { |
8470 | if (dst_reg->type != SCALAR_VALUE) { | |
8471 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
8472 | * an arbitrary scalar. Disallow all math except |
8473 | * pointer subtraction | |
f1174f77 | 8474 | */ |
dd066823 | 8475 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
8476 | mark_reg_unknown(env, regs, insn->dst_reg); |
8477 | return 0; | |
f1174f77 | 8478 | } |
82abbf8d AS |
8479 | verbose(env, "R%d pointer %s pointer prohibited\n", |
8480 | insn->dst_reg, | |
8481 | bpf_alu_string[opcode >> 4]); | |
8482 | return -EACCES; | |
f1174f77 EC |
8483 | } else { |
8484 | /* scalar += pointer | |
8485 | * This is legal, but we have to reverse our | |
8486 | * src/dest handling in computing the range | |
8487 | */ | |
b5dc0163 AS |
8488 | err = mark_chain_precision(env, insn->dst_reg); |
8489 | if (err) | |
8490 | return err; | |
82abbf8d AS |
8491 | return adjust_ptr_min_max_vals(env, insn, |
8492 | src_reg, dst_reg); | |
f1174f77 EC |
8493 | } |
8494 | } else if (ptr_reg) { | |
8495 | /* pointer += scalar */ | |
b5dc0163 AS |
8496 | err = mark_chain_precision(env, insn->src_reg); |
8497 | if (err) | |
8498 | return err; | |
82abbf8d AS |
8499 | return adjust_ptr_min_max_vals(env, insn, |
8500 | dst_reg, src_reg); | |
f1174f77 EC |
8501 | } |
8502 | } else { | |
8503 | /* Pretend the src is a reg with a known value, since we only | |
8504 | * need to be able to read from this state. | |
8505 | */ | |
8506 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 8507 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 8508 | src_reg = &off_reg; |
82abbf8d AS |
8509 | if (ptr_reg) /* pointer += K */ |
8510 | return adjust_ptr_min_max_vals(env, insn, | |
8511 | ptr_reg, src_reg); | |
f1174f77 EC |
8512 | } |
8513 | ||
8514 | /* Got here implies adding two SCALAR_VALUEs */ | |
8515 | if (WARN_ON_ONCE(ptr_reg)) { | |
0f55f9ed | 8516 | print_verifier_state(env, state, true); |
61bd5218 | 8517 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
8518 | return -EINVAL; |
8519 | } | |
8520 | if (WARN_ON(!src_reg)) { | |
0f55f9ed | 8521 | print_verifier_state(env, state, true); |
61bd5218 | 8522 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
8523 | return -EINVAL; |
8524 | } | |
8525 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
8526 | } |
8527 | ||
17a52670 | 8528 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 8529 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 8530 | { |
638f5b90 | 8531 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
8532 | u8 opcode = BPF_OP(insn->code); |
8533 | int err; | |
8534 | ||
8535 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
8536 | if (opcode == BPF_NEG) { | |
8537 | if (BPF_SRC(insn->code) != 0 || | |
8538 | insn->src_reg != BPF_REG_0 || | |
8539 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 8540 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
8541 | return -EINVAL; |
8542 | } | |
8543 | } else { | |
8544 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
8545 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
8546 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 8547 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
8548 | return -EINVAL; |
8549 | } | |
8550 | } | |
8551 | ||
8552 | /* check src operand */ | |
dc503a8a | 8553 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8554 | if (err) |
8555 | return err; | |
8556 | ||
1be7f75d | 8557 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 8558 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
8559 | insn->dst_reg); |
8560 | return -EACCES; | |
8561 | } | |
8562 | ||
17a52670 | 8563 | /* check dest operand */ |
dc503a8a | 8564 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
8565 | if (err) |
8566 | return err; | |
8567 | ||
8568 | } else if (opcode == BPF_MOV) { | |
8569 | ||
8570 | if (BPF_SRC(insn->code) == BPF_X) { | |
8571 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 8572 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
8573 | return -EINVAL; |
8574 | } | |
8575 | ||
8576 | /* check src operand */ | |
dc503a8a | 8577 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8578 | if (err) |
8579 | return err; | |
8580 | } else { | |
8581 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 8582 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
8583 | return -EINVAL; |
8584 | } | |
8585 | } | |
8586 | ||
fbeb1603 AF |
8587 | /* check dest operand, mark as required later */ |
8588 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
8589 | if (err) |
8590 | return err; | |
8591 | ||
8592 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
8593 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
8594 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
8595 | ||
17a52670 AS |
8596 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
8597 | /* case: R1 = R2 | |
8598 | * copy register state to dest reg | |
8599 | */ | |
75748837 AS |
8600 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
8601 | /* Assign src and dst registers the same ID | |
8602 | * that will be used by find_equal_scalars() | |
8603 | * to propagate min/max range. | |
8604 | */ | |
8605 | src_reg->id = ++env->id_gen; | |
e434b8cd JW |
8606 | *dst_reg = *src_reg; |
8607 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 8608 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 8609 | } else { |
f1174f77 | 8610 | /* R1 = (u32) R2 */ |
1be7f75d | 8611 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
8612 | verbose(env, |
8613 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
8614 | insn->src_reg); |
8615 | return -EACCES; | |
e434b8cd JW |
8616 | } else if (src_reg->type == SCALAR_VALUE) { |
8617 | *dst_reg = *src_reg; | |
75748837 AS |
8618 | /* Make sure ID is cleared otherwise |
8619 | * dst_reg min/max could be incorrectly | |
8620 | * propagated into src_reg by find_equal_scalars() | |
8621 | */ | |
8622 | dst_reg->id = 0; | |
e434b8cd | 8623 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 8624 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
8625 | } else { |
8626 | mark_reg_unknown(env, regs, | |
8627 | insn->dst_reg); | |
1be7f75d | 8628 | } |
3f50f132 | 8629 | zext_32_to_64(dst_reg); |
3cf2b61e DB |
8630 | |
8631 | __update_reg_bounds(dst_reg); | |
8632 | __reg_deduce_bounds(dst_reg); | |
8633 | __reg_bound_offset(dst_reg); | |
17a52670 AS |
8634 | } |
8635 | } else { | |
8636 | /* case: R = imm | |
8637 | * remember the value we stored into this reg | |
8638 | */ | |
fbeb1603 AF |
8639 | /* clear any state __mark_reg_known doesn't set */ |
8640 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 8641 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
8642 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
8643 | __mark_reg_known(regs + insn->dst_reg, | |
8644 | insn->imm); | |
8645 | } else { | |
8646 | __mark_reg_known(regs + insn->dst_reg, | |
8647 | (u32)insn->imm); | |
8648 | } | |
17a52670 AS |
8649 | } |
8650 | ||
8651 | } else if (opcode > BPF_END) { | |
61bd5218 | 8652 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
8653 | return -EINVAL; |
8654 | ||
8655 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
8656 | ||
17a52670 AS |
8657 | if (BPF_SRC(insn->code) == BPF_X) { |
8658 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 8659 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
8660 | return -EINVAL; |
8661 | } | |
8662 | /* check src1 operand */ | |
dc503a8a | 8663 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
8664 | if (err) |
8665 | return err; | |
8666 | } else { | |
8667 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 8668 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
8669 | return -EINVAL; |
8670 | } | |
8671 | } | |
8672 | ||
8673 | /* check src2 operand */ | |
dc503a8a | 8674 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
8675 | if (err) |
8676 | return err; | |
8677 | ||
8678 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
8679 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 8680 | verbose(env, "div by zero\n"); |
17a52670 AS |
8681 | return -EINVAL; |
8682 | } | |
8683 | ||
229394e8 RV |
8684 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
8685 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
8686 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
8687 | ||
8688 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 8689 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
8690 | return -EINVAL; |
8691 | } | |
8692 | } | |
8693 | ||
1a0dc1ac | 8694 | /* check dest operand */ |
dc503a8a | 8695 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
8696 | if (err) |
8697 | return err; | |
8698 | ||
f1174f77 | 8699 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
8700 | } |
8701 | ||
8702 | return 0; | |
8703 | } | |
8704 | ||
c6a9efa1 PC |
8705 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
8706 | struct bpf_reg_state *dst_reg, | |
6d94e741 | 8707 | enum bpf_reg_type type, int new_range) |
c6a9efa1 PC |
8708 | { |
8709 | struct bpf_reg_state *reg; | |
8710 | int i; | |
8711 | ||
8712 | for (i = 0; i < MAX_BPF_REG; i++) { | |
8713 | reg = &state->regs[i]; | |
8714 | if (reg->type == type && reg->id == dst_reg->id) | |
8715 | /* keep the maximum range already checked */ | |
8716 | reg->range = max(reg->range, new_range); | |
8717 | } | |
8718 | ||
8719 | bpf_for_each_spilled_reg(i, state, reg) { | |
8720 | if (!reg) | |
8721 | continue; | |
8722 | if (reg->type == type && reg->id == dst_reg->id) | |
8723 | reg->range = max(reg->range, new_range); | |
8724 | } | |
8725 | } | |
8726 | ||
f4d7e40a | 8727 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 8728 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 8729 | enum bpf_reg_type type, |
fb2a311a | 8730 | bool range_right_open) |
969bf05e | 8731 | { |
6d94e741 | 8732 | int new_range, i; |
2d2be8ca | 8733 | |
fb2a311a DB |
8734 | if (dst_reg->off < 0 || |
8735 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
8736 | /* This doesn't give us any range */ |
8737 | return; | |
8738 | ||
b03c9f9f EC |
8739 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
8740 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
8741 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
8742 | * than pkt_end, but that's because it's also less than pkt. | |
8743 | */ | |
8744 | return; | |
8745 | ||
fb2a311a DB |
8746 | new_range = dst_reg->off; |
8747 | if (range_right_open) | |
2fa7d94a | 8748 | new_range++; |
fb2a311a DB |
8749 | |
8750 | /* Examples for register markings: | |
2d2be8ca | 8751 | * |
fb2a311a | 8752 | * pkt_data in dst register: |
2d2be8ca DB |
8753 | * |
8754 | * r2 = r3; | |
8755 | * r2 += 8; | |
8756 | * if (r2 > pkt_end) goto <handle exception> | |
8757 | * <access okay> | |
8758 | * | |
b4e432f1 DB |
8759 | * r2 = r3; |
8760 | * r2 += 8; | |
8761 | * if (r2 < pkt_end) goto <access okay> | |
8762 | * <handle exception> | |
8763 | * | |
2d2be8ca DB |
8764 | * Where: |
8765 | * r2 == dst_reg, pkt_end == src_reg | |
8766 | * r2=pkt(id=n,off=8,r=0) | |
8767 | * r3=pkt(id=n,off=0,r=0) | |
8768 | * | |
fb2a311a | 8769 | * pkt_data in src register: |
2d2be8ca DB |
8770 | * |
8771 | * r2 = r3; | |
8772 | * r2 += 8; | |
8773 | * if (pkt_end >= r2) goto <access okay> | |
8774 | * <handle exception> | |
8775 | * | |
b4e432f1 DB |
8776 | * r2 = r3; |
8777 | * r2 += 8; | |
8778 | * if (pkt_end <= r2) goto <handle exception> | |
8779 | * <access okay> | |
8780 | * | |
2d2be8ca DB |
8781 | * Where: |
8782 | * pkt_end == dst_reg, r2 == src_reg | |
8783 | * r2=pkt(id=n,off=8,r=0) | |
8784 | * r3=pkt(id=n,off=0,r=0) | |
8785 | * | |
8786 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
8787 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
8788 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
8789 | * the check. | |
969bf05e | 8790 | */ |
2d2be8ca | 8791 | |
f1174f77 EC |
8792 | /* If our ids match, then we must have the same max_value. And we |
8793 | * don't care about the other reg's fixed offset, since if it's too big | |
8794 | * the range won't allow anything. | |
8795 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
8796 | */ | |
c6a9efa1 PC |
8797 | for (i = 0; i <= vstate->curframe; i++) |
8798 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
8799 | new_range); | |
969bf05e AS |
8800 | } |
8801 | ||
3f50f132 | 8802 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 8803 | { |
3f50f132 JF |
8804 | struct tnum subreg = tnum_subreg(reg->var_off); |
8805 | s32 sval = (s32)val; | |
a72dafaf | 8806 | |
3f50f132 JF |
8807 | switch (opcode) { |
8808 | case BPF_JEQ: | |
8809 | if (tnum_is_const(subreg)) | |
8810 | return !!tnum_equals_const(subreg, val); | |
8811 | break; | |
8812 | case BPF_JNE: | |
8813 | if (tnum_is_const(subreg)) | |
8814 | return !tnum_equals_const(subreg, val); | |
8815 | break; | |
8816 | case BPF_JSET: | |
8817 | if ((~subreg.mask & subreg.value) & val) | |
8818 | return 1; | |
8819 | if (!((subreg.mask | subreg.value) & val)) | |
8820 | return 0; | |
8821 | break; | |
8822 | case BPF_JGT: | |
8823 | if (reg->u32_min_value > val) | |
8824 | return 1; | |
8825 | else if (reg->u32_max_value <= val) | |
8826 | return 0; | |
8827 | break; | |
8828 | case BPF_JSGT: | |
8829 | if (reg->s32_min_value > sval) | |
8830 | return 1; | |
ee114dd6 | 8831 | else if (reg->s32_max_value <= sval) |
3f50f132 JF |
8832 | return 0; |
8833 | break; | |
8834 | case BPF_JLT: | |
8835 | if (reg->u32_max_value < val) | |
8836 | return 1; | |
8837 | else if (reg->u32_min_value >= val) | |
8838 | return 0; | |
8839 | break; | |
8840 | case BPF_JSLT: | |
8841 | if (reg->s32_max_value < sval) | |
8842 | return 1; | |
8843 | else if (reg->s32_min_value >= sval) | |
8844 | return 0; | |
8845 | break; | |
8846 | case BPF_JGE: | |
8847 | if (reg->u32_min_value >= val) | |
8848 | return 1; | |
8849 | else if (reg->u32_max_value < val) | |
8850 | return 0; | |
8851 | break; | |
8852 | case BPF_JSGE: | |
8853 | if (reg->s32_min_value >= sval) | |
8854 | return 1; | |
8855 | else if (reg->s32_max_value < sval) | |
8856 | return 0; | |
8857 | break; | |
8858 | case BPF_JLE: | |
8859 | if (reg->u32_max_value <= val) | |
8860 | return 1; | |
8861 | else if (reg->u32_min_value > val) | |
8862 | return 0; | |
8863 | break; | |
8864 | case BPF_JSLE: | |
8865 | if (reg->s32_max_value <= sval) | |
8866 | return 1; | |
8867 | else if (reg->s32_min_value > sval) | |
8868 | return 0; | |
8869 | break; | |
8870 | } | |
4f7b3e82 | 8871 | |
3f50f132 JF |
8872 | return -1; |
8873 | } | |
092ed096 | 8874 | |
3f50f132 JF |
8875 | |
8876 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
8877 | { | |
8878 | s64 sval = (s64)val; | |
a72dafaf | 8879 | |
4f7b3e82 AS |
8880 | switch (opcode) { |
8881 | case BPF_JEQ: | |
8882 | if (tnum_is_const(reg->var_off)) | |
8883 | return !!tnum_equals_const(reg->var_off, val); | |
8884 | break; | |
8885 | case BPF_JNE: | |
8886 | if (tnum_is_const(reg->var_off)) | |
8887 | return !tnum_equals_const(reg->var_off, val); | |
8888 | break; | |
960ea056 JK |
8889 | case BPF_JSET: |
8890 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
8891 | return 1; | |
8892 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
8893 | return 0; | |
8894 | break; | |
4f7b3e82 AS |
8895 | case BPF_JGT: |
8896 | if (reg->umin_value > val) | |
8897 | return 1; | |
8898 | else if (reg->umax_value <= val) | |
8899 | return 0; | |
8900 | break; | |
8901 | case BPF_JSGT: | |
a72dafaf | 8902 | if (reg->smin_value > sval) |
4f7b3e82 | 8903 | return 1; |
ee114dd6 | 8904 | else if (reg->smax_value <= sval) |
4f7b3e82 AS |
8905 | return 0; |
8906 | break; | |
8907 | case BPF_JLT: | |
8908 | if (reg->umax_value < val) | |
8909 | return 1; | |
8910 | else if (reg->umin_value >= val) | |
8911 | return 0; | |
8912 | break; | |
8913 | case BPF_JSLT: | |
a72dafaf | 8914 | if (reg->smax_value < sval) |
4f7b3e82 | 8915 | return 1; |
a72dafaf | 8916 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
8917 | return 0; |
8918 | break; | |
8919 | case BPF_JGE: | |
8920 | if (reg->umin_value >= val) | |
8921 | return 1; | |
8922 | else if (reg->umax_value < val) | |
8923 | return 0; | |
8924 | break; | |
8925 | case BPF_JSGE: | |
a72dafaf | 8926 | if (reg->smin_value >= sval) |
4f7b3e82 | 8927 | return 1; |
a72dafaf | 8928 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
8929 | return 0; |
8930 | break; | |
8931 | case BPF_JLE: | |
8932 | if (reg->umax_value <= val) | |
8933 | return 1; | |
8934 | else if (reg->umin_value > val) | |
8935 | return 0; | |
8936 | break; | |
8937 | case BPF_JSLE: | |
a72dafaf | 8938 | if (reg->smax_value <= sval) |
4f7b3e82 | 8939 | return 1; |
a72dafaf | 8940 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
8941 | return 0; |
8942 | break; | |
8943 | } | |
8944 | ||
8945 | return -1; | |
8946 | } | |
8947 | ||
3f50f132 JF |
8948 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
8949 | * and return: | |
8950 | * 1 - branch will be taken and "goto target" will be executed | |
8951 | * 0 - branch will not be taken and fall-through to next insn | |
8952 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
8953 | * range [0,10] | |
604dca5e | 8954 | */ |
3f50f132 JF |
8955 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
8956 | bool is_jmp32) | |
604dca5e | 8957 | { |
cac616db JF |
8958 | if (__is_pointer_value(false, reg)) { |
8959 | if (!reg_type_not_null(reg->type)) | |
8960 | return -1; | |
8961 | ||
8962 | /* If pointer is valid tests against zero will fail so we can | |
8963 | * use this to direct branch taken. | |
8964 | */ | |
8965 | if (val != 0) | |
8966 | return -1; | |
8967 | ||
8968 | switch (opcode) { | |
8969 | case BPF_JEQ: | |
8970 | return 0; | |
8971 | case BPF_JNE: | |
8972 | return 1; | |
8973 | default: | |
8974 | return -1; | |
8975 | } | |
8976 | } | |
604dca5e | 8977 | |
3f50f132 JF |
8978 | if (is_jmp32) |
8979 | return is_branch32_taken(reg, val, opcode); | |
8980 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
8981 | } |
8982 | ||
6d94e741 AS |
8983 | static int flip_opcode(u32 opcode) |
8984 | { | |
8985 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
8986 | static const u8 opcode_flip[16] = { | |
8987 | /* these stay the same */ | |
8988 | [BPF_JEQ >> 4] = BPF_JEQ, | |
8989 | [BPF_JNE >> 4] = BPF_JNE, | |
8990 | [BPF_JSET >> 4] = BPF_JSET, | |
8991 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
8992 | [BPF_JGE >> 4] = BPF_JLE, | |
8993 | [BPF_JGT >> 4] = BPF_JLT, | |
8994 | [BPF_JLE >> 4] = BPF_JGE, | |
8995 | [BPF_JLT >> 4] = BPF_JGT, | |
8996 | [BPF_JSGE >> 4] = BPF_JSLE, | |
8997 | [BPF_JSGT >> 4] = BPF_JSLT, | |
8998 | [BPF_JSLE >> 4] = BPF_JSGE, | |
8999 | [BPF_JSLT >> 4] = BPF_JSGT | |
9000 | }; | |
9001 | return opcode_flip[opcode >> 4]; | |
9002 | } | |
9003 | ||
9004 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
9005 | struct bpf_reg_state *src_reg, | |
9006 | u8 opcode) | |
9007 | { | |
9008 | struct bpf_reg_state *pkt; | |
9009 | ||
9010 | if (src_reg->type == PTR_TO_PACKET_END) { | |
9011 | pkt = dst_reg; | |
9012 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
9013 | pkt = src_reg; | |
9014 | opcode = flip_opcode(opcode); | |
9015 | } else { | |
9016 | return -1; | |
9017 | } | |
9018 | ||
9019 | if (pkt->range >= 0) | |
9020 | return -1; | |
9021 | ||
9022 | switch (opcode) { | |
9023 | case BPF_JLE: | |
9024 | /* pkt <= pkt_end */ | |
9025 | fallthrough; | |
9026 | case BPF_JGT: | |
9027 | /* pkt > pkt_end */ | |
9028 | if (pkt->range == BEYOND_PKT_END) | |
9029 | /* pkt has at last one extra byte beyond pkt_end */ | |
9030 | return opcode == BPF_JGT; | |
9031 | break; | |
9032 | case BPF_JLT: | |
9033 | /* pkt < pkt_end */ | |
9034 | fallthrough; | |
9035 | case BPF_JGE: | |
9036 | /* pkt >= pkt_end */ | |
9037 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
9038 | return opcode == BPF_JGE; | |
9039 | break; | |
9040 | } | |
9041 | return -1; | |
9042 | } | |
9043 | ||
48461135 JB |
9044 | /* Adjusts the register min/max values in the case that the dst_reg is the |
9045 | * variable register that we are working on, and src_reg is a constant or we're | |
9046 | * simply doing a BPF_K check. | |
f1174f77 | 9047 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
9048 | */ |
9049 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
9050 | struct bpf_reg_state *false_reg, |
9051 | u64 val, u32 val32, | |
092ed096 | 9052 | u8 opcode, bool is_jmp32) |
48461135 | 9053 | { |
3f50f132 JF |
9054 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
9055 | struct tnum false_64off = false_reg->var_off; | |
9056 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
9057 | struct tnum true_64off = true_reg->var_off; | |
9058 | s64 sval = (s64)val; | |
9059 | s32 sval32 = (s32)val32; | |
a72dafaf | 9060 | |
f1174f77 EC |
9061 | /* If the dst_reg is a pointer, we can't learn anything about its |
9062 | * variable offset from the compare (unless src_reg were a pointer into | |
9063 | * the same object, but we don't bother with that. | |
9064 | * Since false_reg and true_reg have the same type by construction, we | |
9065 | * only need to check one of them for pointerness. | |
9066 | */ | |
9067 | if (__is_pointer_value(false, false_reg)) | |
9068 | return; | |
4cabc5b1 | 9069 | |
48461135 JB |
9070 | switch (opcode) { |
9071 | case BPF_JEQ: | |
48461135 | 9072 | case BPF_JNE: |
a72dafaf JW |
9073 | { |
9074 | struct bpf_reg_state *reg = | |
9075 | opcode == BPF_JEQ ? true_reg : false_reg; | |
9076 | ||
e688c3db AS |
9077 | /* JEQ/JNE comparison doesn't change the register equivalence. |
9078 | * r1 = r2; | |
9079 | * if (r1 == 42) goto label; | |
9080 | * ... | |
9081 | * label: // here both r1 and r2 are known to be 42. | |
9082 | * | |
9083 | * Hence when marking register as known preserve it's ID. | |
48461135 | 9084 | */ |
3f50f132 JF |
9085 | if (is_jmp32) |
9086 | __mark_reg32_known(reg, val32); | |
9087 | else | |
e688c3db | 9088 | ___mark_reg_known(reg, val); |
48461135 | 9089 | break; |
a72dafaf | 9090 | } |
960ea056 | 9091 | case BPF_JSET: |
3f50f132 JF |
9092 | if (is_jmp32) { |
9093 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
9094 | if (is_power_of_2(val32)) | |
9095 | true_32off = tnum_or(true_32off, | |
9096 | tnum_const(val32)); | |
9097 | } else { | |
9098 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
9099 | if (is_power_of_2(val)) | |
9100 | true_64off = tnum_or(true_64off, | |
9101 | tnum_const(val)); | |
9102 | } | |
960ea056 | 9103 | break; |
48461135 | 9104 | case BPF_JGE: |
a72dafaf JW |
9105 | case BPF_JGT: |
9106 | { | |
3f50f132 JF |
9107 | if (is_jmp32) { |
9108 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
9109 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
9110 | ||
9111 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
9112 | false_umax); | |
9113 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
9114 | true_umin); | |
9115 | } else { | |
9116 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
9117 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
9118 | ||
9119 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
9120 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
9121 | } | |
b03c9f9f | 9122 | break; |
a72dafaf | 9123 | } |
48461135 | 9124 | case BPF_JSGE: |
a72dafaf JW |
9125 | case BPF_JSGT: |
9126 | { | |
3f50f132 JF |
9127 | if (is_jmp32) { |
9128 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
9129 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 9130 | |
3f50f132 JF |
9131 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
9132 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
9133 | } else { | |
9134 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
9135 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
9136 | ||
9137 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
9138 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
9139 | } | |
48461135 | 9140 | break; |
a72dafaf | 9141 | } |
b4e432f1 | 9142 | case BPF_JLE: |
a72dafaf JW |
9143 | case BPF_JLT: |
9144 | { | |
3f50f132 JF |
9145 | if (is_jmp32) { |
9146 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
9147 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
9148 | ||
9149 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
9150 | false_umin); | |
9151 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
9152 | true_umax); | |
9153 | } else { | |
9154 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
9155 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
9156 | ||
9157 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
9158 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
9159 | } | |
b4e432f1 | 9160 | break; |
a72dafaf | 9161 | } |
b4e432f1 | 9162 | case BPF_JSLE: |
a72dafaf JW |
9163 | case BPF_JSLT: |
9164 | { | |
3f50f132 JF |
9165 | if (is_jmp32) { |
9166 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
9167 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 9168 | |
3f50f132 JF |
9169 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
9170 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
9171 | } else { | |
9172 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
9173 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
9174 | ||
9175 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
9176 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
9177 | } | |
b4e432f1 | 9178 | break; |
a72dafaf | 9179 | } |
48461135 | 9180 | default: |
0fc31b10 | 9181 | return; |
48461135 JB |
9182 | } |
9183 | ||
3f50f132 JF |
9184 | if (is_jmp32) { |
9185 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
9186 | tnum_subreg(false_32off)); | |
9187 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
9188 | tnum_subreg(true_32off)); | |
9189 | __reg_combine_32_into_64(false_reg); | |
9190 | __reg_combine_32_into_64(true_reg); | |
9191 | } else { | |
9192 | false_reg->var_off = false_64off; | |
9193 | true_reg->var_off = true_64off; | |
9194 | __reg_combine_64_into_32(false_reg); | |
9195 | __reg_combine_64_into_32(true_reg); | |
9196 | } | |
48461135 JB |
9197 | } |
9198 | ||
f1174f77 EC |
9199 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
9200 | * the variable reg. | |
48461135 JB |
9201 | */ |
9202 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
9203 | struct bpf_reg_state *false_reg, |
9204 | u64 val, u32 val32, | |
092ed096 | 9205 | u8 opcode, bool is_jmp32) |
48461135 | 9206 | { |
6d94e741 | 9207 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
9208 | /* This uses zero as "not present in table"; luckily the zero opcode, |
9209 | * BPF_JA, can't get here. | |
b03c9f9f | 9210 | */ |
0fc31b10 | 9211 | if (opcode) |
3f50f132 | 9212 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
9213 | } |
9214 | ||
9215 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
9216 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
9217 | struct bpf_reg_state *dst_reg) | |
9218 | { | |
b03c9f9f EC |
9219 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
9220 | dst_reg->umin_value); | |
9221 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
9222 | dst_reg->umax_value); | |
9223 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
9224 | dst_reg->smin_value); | |
9225 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
9226 | dst_reg->smax_value); | |
f1174f77 EC |
9227 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
9228 | dst_reg->var_off); | |
b03c9f9f EC |
9229 | /* We might have learned new bounds from the var_off. */ |
9230 | __update_reg_bounds(src_reg); | |
9231 | __update_reg_bounds(dst_reg); | |
9232 | /* We might have learned something about the sign bit. */ | |
9233 | __reg_deduce_bounds(src_reg); | |
9234 | __reg_deduce_bounds(dst_reg); | |
9235 | /* We might have learned some bits from the bounds. */ | |
9236 | __reg_bound_offset(src_reg); | |
9237 | __reg_bound_offset(dst_reg); | |
9238 | /* Intersecting with the old var_off might have improved our bounds | |
9239 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
9240 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
9241 | */ | |
9242 | __update_reg_bounds(src_reg); | |
9243 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
9244 | } |
9245 | ||
9246 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
9247 | struct bpf_reg_state *true_dst, | |
9248 | struct bpf_reg_state *false_src, | |
9249 | struct bpf_reg_state *false_dst, | |
9250 | u8 opcode) | |
9251 | { | |
9252 | switch (opcode) { | |
9253 | case BPF_JEQ: | |
9254 | __reg_combine_min_max(true_src, true_dst); | |
9255 | break; | |
9256 | case BPF_JNE: | |
9257 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 9258 | break; |
4cabc5b1 | 9259 | } |
48461135 JB |
9260 | } |
9261 | ||
fd978bf7 JS |
9262 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
9263 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 9264 | bool is_null) |
57a09bf0 | 9265 | { |
c25b2ae1 | 9266 | if (type_may_be_null(reg->type) && reg->id == id && |
93c230e3 | 9267 | !WARN_ON_ONCE(!reg->id)) { |
b03c9f9f EC |
9268 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
9269 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 9270 | reg->off)) { |
e60b0d12 DB |
9271 | /* Old offset (both fixed and variable parts) should |
9272 | * have been known-zero, because we don't allow pointer | |
9273 | * arithmetic on pointers that might be NULL. If we | |
9274 | * see this happening, don't convert the register. | |
9275 | */ | |
9276 | return; | |
f1174f77 EC |
9277 | } |
9278 | if (is_null) { | |
9279 | reg->type = SCALAR_VALUE; | |
1b986589 MKL |
9280 | /* We don't need id and ref_obj_id from this point |
9281 | * onwards anymore, thus we should better reset it, | |
9282 | * so that state pruning has chances to take effect. | |
9283 | */ | |
9284 | reg->id = 0; | |
9285 | reg->ref_obj_id = 0; | |
4ddb7416 DB |
9286 | |
9287 | return; | |
9288 | } | |
9289 | ||
9290 | mark_ptr_not_null_reg(reg); | |
9291 | ||
9292 | if (!reg_may_point_to_spin_lock(reg)) { | |
1b986589 MKL |
9293 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
9294 | * in release_reg_references(). | |
9295 | * | |
9296 | * reg->id is still used by spin_lock ptr. Other | |
9297 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
9298 | */ |
9299 | reg->id = 0; | |
56f668df | 9300 | } |
57a09bf0 TG |
9301 | } |
9302 | } | |
9303 | ||
c6a9efa1 PC |
9304 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
9305 | bool is_null) | |
9306 | { | |
9307 | struct bpf_reg_state *reg; | |
9308 | int i; | |
9309 | ||
9310 | for (i = 0; i < MAX_BPF_REG; i++) | |
9311 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
9312 | ||
9313 | bpf_for_each_spilled_reg(i, state, reg) { | |
9314 | if (!reg) | |
9315 | continue; | |
9316 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
9317 | } | |
9318 | } | |
9319 | ||
57a09bf0 TG |
9320 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
9321 | * be folded together at some point. | |
9322 | */ | |
840b9615 JS |
9323 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
9324 | bool is_null) | |
57a09bf0 | 9325 | { |
f4d7e40a | 9326 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 9327 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 9328 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 9329 | u32 id = regs[regno].id; |
c6a9efa1 | 9330 | int i; |
57a09bf0 | 9331 | |
1b986589 MKL |
9332 | if (ref_obj_id && ref_obj_id == id && is_null) |
9333 | /* regs[regno] is in the " == NULL" branch. | |
9334 | * No one could have freed the reference state before | |
9335 | * doing the NULL check. | |
9336 | */ | |
9337 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 9338 | |
c6a9efa1 PC |
9339 | for (i = 0; i <= vstate->curframe; i++) |
9340 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
9341 | } |
9342 | ||
5beca081 DB |
9343 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
9344 | struct bpf_reg_state *dst_reg, | |
9345 | struct bpf_reg_state *src_reg, | |
9346 | struct bpf_verifier_state *this_branch, | |
9347 | struct bpf_verifier_state *other_branch) | |
9348 | { | |
9349 | if (BPF_SRC(insn->code) != BPF_X) | |
9350 | return false; | |
9351 | ||
092ed096 JW |
9352 | /* Pointers are always 64-bit. */ |
9353 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
9354 | return false; | |
9355 | ||
5beca081 DB |
9356 | switch (BPF_OP(insn->code)) { |
9357 | case BPF_JGT: | |
9358 | if ((dst_reg->type == PTR_TO_PACKET && | |
9359 | src_reg->type == PTR_TO_PACKET_END) || | |
9360 | (dst_reg->type == PTR_TO_PACKET_META && | |
9361 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9362 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
9363 | find_good_pkt_pointers(this_branch, dst_reg, | |
9364 | dst_reg->type, false); | |
6d94e741 | 9365 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
9366 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9367 | src_reg->type == PTR_TO_PACKET) || | |
9368 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9369 | src_reg->type == PTR_TO_PACKET_META)) { | |
9370 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
9371 | find_good_pkt_pointers(other_branch, src_reg, | |
9372 | src_reg->type, true); | |
6d94e741 | 9373 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
9374 | } else { |
9375 | return false; | |
9376 | } | |
9377 | break; | |
9378 | case BPF_JLT: | |
9379 | if ((dst_reg->type == PTR_TO_PACKET && | |
9380 | src_reg->type == PTR_TO_PACKET_END) || | |
9381 | (dst_reg->type == PTR_TO_PACKET_META && | |
9382 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9383 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
9384 | find_good_pkt_pointers(other_branch, dst_reg, | |
9385 | dst_reg->type, true); | |
6d94e741 | 9386 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
9387 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9388 | src_reg->type == PTR_TO_PACKET) || | |
9389 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9390 | src_reg->type == PTR_TO_PACKET_META)) { | |
9391 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
9392 | find_good_pkt_pointers(this_branch, src_reg, | |
9393 | src_reg->type, false); | |
6d94e741 | 9394 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
9395 | } else { |
9396 | return false; | |
9397 | } | |
9398 | break; | |
9399 | case BPF_JGE: | |
9400 | if ((dst_reg->type == PTR_TO_PACKET && | |
9401 | src_reg->type == PTR_TO_PACKET_END) || | |
9402 | (dst_reg->type == PTR_TO_PACKET_META && | |
9403 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9404 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
9405 | find_good_pkt_pointers(this_branch, dst_reg, | |
9406 | dst_reg->type, true); | |
6d94e741 | 9407 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
9408 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9409 | src_reg->type == PTR_TO_PACKET) || | |
9410 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9411 | src_reg->type == PTR_TO_PACKET_META)) { | |
9412 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
9413 | find_good_pkt_pointers(other_branch, src_reg, | |
9414 | src_reg->type, false); | |
6d94e741 | 9415 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
9416 | } else { |
9417 | return false; | |
9418 | } | |
9419 | break; | |
9420 | case BPF_JLE: | |
9421 | if ((dst_reg->type == PTR_TO_PACKET && | |
9422 | src_reg->type == PTR_TO_PACKET_END) || | |
9423 | (dst_reg->type == PTR_TO_PACKET_META && | |
9424 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
9425 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
9426 | find_good_pkt_pointers(other_branch, dst_reg, | |
9427 | dst_reg->type, false); | |
6d94e741 | 9428 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
9429 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
9430 | src_reg->type == PTR_TO_PACKET) || | |
9431 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
9432 | src_reg->type == PTR_TO_PACKET_META)) { | |
9433 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
9434 | find_good_pkt_pointers(this_branch, src_reg, | |
9435 | src_reg->type, true); | |
6d94e741 | 9436 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
9437 | } else { |
9438 | return false; | |
9439 | } | |
9440 | break; | |
9441 | default: | |
9442 | return false; | |
9443 | } | |
9444 | ||
9445 | return true; | |
9446 | } | |
9447 | ||
75748837 AS |
9448 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
9449 | struct bpf_reg_state *known_reg) | |
9450 | { | |
9451 | struct bpf_func_state *state; | |
9452 | struct bpf_reg_state *reg; | |
9453 | int i, j; | |
9454 | ||
9455 | for (i = 0; i <= vstate->curframe; i++) { | |
9456 | state = vstate->frame[i]; | |
9457 | for (j = 0; j < MAX_BPF_REG; j++) { | |
9458 | reg = &state->regs[j]; | |
9459 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
9460 | *reg = *known_reg; | |
9461 | } | |
9462 | ||
9463 | bpf_for_each_spilled_reg(j, state, reg) { | |
9464 | if (!reg) | |
9465 | continue; | |
9466 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
9467 | *reg = *known_reg; | |
9468 | } | |
9469 | } | |
9470 | } | |
9471 | ||
58e2af8b | 9472 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
9473 | struct bpf_insn *insn, int *insn_idx) |
9474 | { | |
f4d7e40a AS |
9475 | struct bpf_verifier_state *this_branch = env->cur_state; |
9476 | struct bpf_verifier_state *other_branch; | |
9477 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 9478 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 9479 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 9480 | bool is_jmp32; |
fb8d251e | 9481 | int pred = -1; |
17a52670 AS |
9482 | int err; |
9483 | ||
092ed096 JW |
9484 | /* Only conditional jumps are expected to reach here. */ |
9485 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
9486 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
9487 | return -EINVAL; |
9488 | } | |
9489 | ||
9490 | if (BPF_SRC(insn->code) == BPF_X) { | |
9491 | if (insn->imm != 0) { | |
092ed096 | 9492 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
9493 | return -EINVAL; |
9494 | } | |
9495 | ||
9496 | /* check src1 operand */ | |
dc503a8a | 9497 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9498 | if (err) |
9499 | return err; | |
1be7f75d AS |
9500 | |
9501 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 9502 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
9503 | insn->src_reg); |
9504 | return -EACCES; | |
9505 | } | |
fb8d251e | 9506 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
9507 | } else { |
9508 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 9509 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
9510 | return -EINVAL; |
9511 | } | |
9512 | } | |
9513 | ||
9514 | /* check src2 operand */ | |
dc503a8a | 9515 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9516 | if (err) |
9517 | return err; | |
9518 | ||
1a0dc1ac | 9519 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 9520 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 9521 | |
3f50f132 JF |
9522 | if (BPF_SRC(insn->code) == BPF_K) { |
9523 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
9524 | } else if (src_reg->type == SCALAR_VALUE && | |
9525 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
9526 | pred = is_branch_taken(dst_reg, | |
9527 | tnum_subreg(src_reg->var_off).value, | |
9528 | opcode, | |
9529 | is_jmp32); | |
9530 | } else if (src_reg->type == SCALAR_VALUE && | |
9531 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
9532 | pred = is_branch_taken(dst_reg, | |
9533 | src_reg->var_off.value, | |
9534 | opcode, | |
9535 | is_jmp32); | |
6d94e741 AS |
9536 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
9537 | reg_is_pkt_pointer_any(src_reg) && | |
9538 | !is_jmp32) { | |
9539 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
9540 | } |
9541 | ||
b5dc0163 | 9542 | if (pred >= 0) { |
cac616db JF |
9543 | /* If we get here with a dst_reg pointer type it is because |
9544 | * above is_branch_taken() special cased the 0 comparison. | |
9545 | */ | |
9546 | if (!__is_pointer_value(false, dst_reg)) | |
9547 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
9548 | if (BPF_SRC(insn->code) == BPF_X && !err && |
9549 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
9550 | err = mark_chain_precision(env, insn->src_reg); |
9551 | if (err) | |
9552 | return err; | |
9553 | } | |
9183671a | 9554 | |
fb8d251e | 9555 | if (pred == 1) { |
9183671a DB |
9556 | /* Only follow the goto, ignore fall-through. If needed, push |
9557 | * the fall-through branch for simulation under speculative | |
9558 | * execution. | |
9559 | */ | |
9560 | if (!env->bypass_spec_v1 && | |
9561 | !sanitize_speculative_path(env, insn, *insn_idx + 1, | |
9562 | *insn_idx)) | |
9563 | return -EFAULT; | |
fb8d251e AS |
9564 | *insn_idx += insn->off; |
9565 | return 0; | |
9566 | } else if (pred == 0) { | |
9183671a DB |
9567 | /* Only follow the fall-through branch, since that's where the |
9568 | * program will go. If needed, push the goto branch for | |
9569 | * simulation under speculative execution. | |
fb8d251e | 9570 | */ |
9183671a DB |
9571 | if (!env->bypass_spec_v1 && |
9572 | !sanitize_speculative_path(env, insn, | |
9573 | *insn_idx + insn->off + 1, | |
9574 | *insn_idx)) | |
9575 | return -EFAULT; | |
fb8d251e | 9576 | return 0; |
17a52670 AS |
9577 | } |
9578 | ||
979d63d5 DB |
9579 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
9580 | false); | |
17a52670 AS |
9581 | if (!other_branch) |
9582 | return -EFAULT; | |
f4d7e40a | 9583 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 9584 | |
48461135 JB |
9585 | /* detect if we are comparing against a constant value so we can adjust |
9586 | * our min/max values for our dst register. | |
f1174f77 EC |
9587 | * this is only legit if both are scalars (or pointers to the same |
9588 | * object, I suppose, but we don't support that right now), because | |
9589 | * otherwise the different base pointers mean the offsets aren't | |
9590 | * comparable. | |
48461135 JB |
9591 | */ |
9592 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 9593 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 9594 | |
f1174f77 | 9595 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
9596 | src_reg->type == SCALAR_VALUE) { |
9597 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
9598 | (is_jmp32 && |
9599 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 9600 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 9601 | dst_reg, |
3f50f132 JF |
9602 | src_reg->var_off.value, |
9603 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
9604 | opcode, is_jmp32); |
9605 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
9606 | (is_jmp32 && |
9607 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 9608 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 9609 | src_reg, |
3f50f132 JF |
9610 | dst_reg->var_off.value, |
9611 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
9612 | opcode, is_jmp32); |
9613 | else if (!is_jmp32 && | |
9614 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 9615 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
9616 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
9617 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 9618 | src_reg, dst_reg, opcode); |
e688c3db AS |
9619 | if (src_reg->id && |
9620 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
9621 | find_equal_scalars(this_branch, src_reg); |
9622 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
9623 | } | |
9624 | ||
f1174f77 EC |
9625 | } |
9626 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 9627 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
9628 | dst_reg, insn->imm, (u32)insn->imm, |
9629 | opcode, is_jmp32); | |
48461135 JB |
9630 | } |
9631 | ||
e688c3db AS |
9632 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
9633 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
9634 | find_equal_scalars(this_branch, dst_reg); |
9635 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
9636 | } | |
9637 | ||
092ed096 JW |
9638 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
9639 | * NOTE: these optimizations below are related with pointer comparison | |
9640 | * which will never be JMP32. | |
9641 | */ | |
9642 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 9643 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
c25b2ae1 | 9644 | type_may_be_null(dst_reg->type)) { |
840b9615 | 9645 | /* Mark all identical registers in each branch as either |
57a09bf0 TG |
9646 | * safe or unknown depending R == 0 or R != 0 conditional. |
9647 | */ | |
840b9615 JS |
9648 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
9649 | opcode == BPF_JNE); | |
9650 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
9651 | opcode == BPF_JEQ); | |
5beca081 DB |
9652 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
9653 | this_branch, other_branch) && | |
9654 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
9655 | verbose(env, "R%d pointer comparison prohibited\n", |
9656 | insn->dst_reg); | |
1be7f75d | 9657 | return -EACCES; |
17a52670 | 9658 | } |
06ee7115 | 9659 | if (env->log.level & BPF_LOG_LEVEL) |
2e576648 | 9660 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
9661 | return 0; |
9662 | } | |
9663 | ||
17a52670 | 9664 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 9665 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 9666 | { |
d8eca5bb | 9667 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 9668 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 9669 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 9670 | struct bpf_map *map; |
17a52670 AS |
9671 | int err; |
9672 | ||
9673 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 9674 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
9675 | return -EINVAL; |
9676 | } | |
9677 | if (insn->off != 0) { | |
61bd5218 | 9678 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
9679 | return -EINVAL; |
9680 | } | |
9681 | ||
dc503a8a | 9682 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
9683 | if (err) |
9684 | return err; | |
9685 | ||
4976b718 | 9686 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 9687 | if (insn->src_reg == 0) { |
6b173873 JK |
9688 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
9689 | ||
4976b718 | 9690 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 9691 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 9692 | return 0; |
6b173873 | 9693 | } |
17a52670 | 9694 | |
d400a6cf DB |
9695 | /* All special src_reg cases are listed below. From this point onwards |
9696 | * we either succeed and assign a corresponding dst_reg->type after | |
9697 | * zeroing the offset, or fail and reject the program. | |
9698 | */ | |
9699 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 9700 | |
d400a6cf | 9701 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
4976b718 | 9702 | dst_reg->type = aux->btf_var.reg_type; |
34d3a78c | 9703 | switch (base_type(dst_reg->type)) { |
4976b718 HL |
9704 | case PTR_TO_MEM: |
9705 | dst_reg->mem_size = aux->btf_var.mem_size; | |
9706 | break; | |
9707 | case PTR_TO_BTF_ID: | |
22dc4a0f | 9708 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
9709 | dst_reg->btf_id = aux->btf_var.btf_id; |
9710 | break; | |
9711 | default: | |
9712 | verbose(env, "bpf verifier is misconfigured\n"); | |
9713 | return -EFAULT; | |
9714 | } | |
9715 | return 0; | |
9716 | } | |
9717 | ||
69c087ba YS |
9718 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
9719 | struct bpf_prog_aux *aux = env->prog->aux; | |
3990ed4c MKL |
9720 | u32 subprogno = find_subprog(env, |
9721 | env->insn_idx + insn->imm + 1); | |
69c087ba YS |
9722 | |
9723 | if (!aux->func_info) { | |
9724 | verbose(env, "missing btf func_info\n"); | |
9725 | return -EINVAL; | |
9726 | } | |
9727 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { | |
9728 | verbose(env, "callback function not static\n"); | |
9729 | return -EINVAL; | |
9730 | } | |
9731 | ||
9732 | dst_reg->type = PTR_TO_FUNC; | |
9733 | dst_reg->subprogno = subprogno; | |
9734 | return 0; | |
9735 | } | |
9736 | ||
d8eca5bb | 9737 | map = env->used_maps[aux->map_index]; |
4976b718 | 9738 | dst_reg->map_ptr = map; |
d8eca5bb | 9739 | |
387544bf AS |
9740 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
9741 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { | |
4976b718 HL |
9742 | dst_reg->type = PTR_TO_MAP_VALUE; |
9743 | dst_reg->off = aux->map_off; | |
d8eca5bb | 9744 | if (map_value_has_spin_lock(map)) |
4976b718 | 9745 | dst_reg->id = ++env->id_gen; |
387544bf AS |
9746 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
9747 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { | |
4976b718 | 9748 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
9749 | } else { |
9750 | verbose(env, "bpf verifier is misconfigured\n"); | |
9751 | return -EINVAL; | |
9752 | } | |
17a52670 | 9753 | |
17a52670 AS |
9754 | return 0; |
9755 | } | |
9756 | ||
96be4325 DB |
9757 | static bool may_access_skb(enum bpf_prog_type type) |
9758 | { | |
9759 | switch (type) { | |
9760 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
9761 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 9762 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
9763 | return true; |
9764 | default: | |
9765 | return false; | |
9766 | } | |
9767 | } | |
9768 | ||
ddd872bc AS |
9769 | /* verify safety of LD_ABS|LD_IND instructions: |
9770 | * - they can only appear in the programs where ctx == skb | |
9771 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
9772 | * preserve R6-R9, and store return value into R0 | |
9773 | * | |
9774 | * Implicit input: | |
9775 | * ctx == skb == R6 == CTX | |
9776 | * | |
9777 | * Explicit input: | |
9778 | * SRC == any register | |
9779 | * IMM == 32-bit immediate | |
9780 | * | |
9781 | * Output: | |
9782 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
9783 | */ | |
58e2af8b | 9784 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 9785 | { |
638f5b90 | 9786 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 9787 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 9788 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
9789 | int i, err; |
9790 | ||
7e40781c | 9791 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 9792 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
9793 | return -EINVAL; |
9794 | } | |
9795 | ||
e0cea7ce DB |
9796 | if (!env->ops->gen_ld_abs) { |
9797 | verbose(env, "bpf verifier is misconfigured\n"); | |
9798 | return -EINVAL; | |
9799 | } | |
9800 | ||
ddd872bc | 9801 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 9802 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 9803 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 9804 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
9805 | return -EINVAL; |
9806 | } | |
9807 | ||
9808 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 9809 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
9810 | if (err) |
9811 | return err; | |
9812 | ||
fd978bf7 JS |
9813 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
9814 | * gen_ld_abs() may terminate the program at runtime, leading to | |
9815 | * reference leak. | |
9816 | */ | |
9817 | err = check_reference_leak(env); | |
9818 | if (err) { | |
9819 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
9820 | return err; | |
9821 | } | |
9822 | ||
d83525ca AS |
9823 | if (env->cur_state->active_spin_lock) { |
9824 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
9825 | return -EINVAL; | |
9826 | } | |
9827 | ||
6d4f151a | 9828 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
9829 | verbose(env, |
9830 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
9831 | return -EINVAL; |
9832 | } | |
9833 | ||
9834 | if (mode == BPF_IND) { | |
9835 | /* check explicit source operand */ | |
dc503a8a | 9836 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
9837 | if (err) |
9838 | return err; | |
9839 | } | |
9840 | ||
be80a1d3 | 9841 | err = check_ptr_off_reg(env, ®s[ctx_reg], ctx_reg); |
6d4f151a DB |
9842 | if (err < 0) |
9843 | return err; | |
9844 | ||
ddd872bc | 9845 | /* reset caller saved regs to unreadable */ |
dc503a8a | 9846 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 9847 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
9848 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
9849 | } | |
ddd872bc AS |
9850 | |
9851 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
9852 | * the value fetched from the packet. |
9853 | * Already marked as written above. | |
ddd872bc | 9854 | */ |
61bd5218 | 9855 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
9856 | /* ld_abs load up to 32-bit skb data. */ |
9857 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
9858 | return 0; |
9859 | } | |
9860 | ||
390ee7e2 AS |
9861 | static int check_return_code(struct bpf_verifier_env *env) |
9862 | { | |
5cf1e914 | 9863 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 9864 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
9865 | struct bpf_reg_state *reg; |
9866 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 9867 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 9868 | int err; |
bfc6bb74 AS |
9869 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
9870 | const bool is_subprog = frame->subprogno; | |
27ae7997 | 9871 | |
9e4e01df | 9872 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
f782e2c3 DB |
9873 | if (!is_subprog && |
9874 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || | |
7e40781c | 9875 | prog_type == BPF_PROG_TYPE_LSM) && |
27ae7997 MKL |
9876 | !prog->aux->attach_func_proto->type) |
9877 | return 0; | |
9878 | ||
8fb33b60 | 9879 | /* eBPF calling convention is such that R0 is used |
27ae7997 MKL |
9880 | * to return the value from eBPF program. |
9881 | * Make sure that it's readable at this time | |
9882 | * of bpf_exit, which means that program wrote | |
9883 | * something into it earlier | |
9884 | */ | |
9885 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
9886 | if (err) | |
9887 | return err; | |
9888 | ||
9889 | if (is_pointer_value(env, BPF_REG_0)) { | |
9890 | verbose(env, "R0 leaks addr as return value\n"); | |
9891 | return -EACCES; | |
9892 | } | |
390ee7e2 | 9893 | |
f782e2c3 | 9894 | reg = cur_regs(env) + BPF_REG_0; |
bfc6bb74 AS |
9895 | |
9896 | if (frame->in_async_callback_fn) { | |
9897 | /* enforce return zero from async callbacks like timer */ | |
9898 | if (reg->type != SCALAR_VALUE) { | |
9899 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", | |
c25b2ae1 | 9900 | reg_type_str(env, reg->type)); |
bfc6bb74 AS |
9901 | return -EINVAL; |
9902 | } | |
9903 | ||
9904 | if (!tnum_in(tnum_const(0), reg->var_off)) { | |
9905 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); | |
9906 | return -EINVAL; | |
9907 | } | |
9908 | return 0; | |
9909 | } | |
9910 | ||
f782e2c3 DB |
9911 | if (is_subprog) { |
9912 | if (reg->type != SCALAR_VALUE) { | |
9913 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
c25b2ae1 | 9914 | reg_type_str(env, reg->type)); |
f782e2c3 DB |
9915 | return -EINVAL; |
9916 | } | |
9917 | return 0; | |
9918 | } | |
9919 | ||
7e40781c | 9920 | switch (prog_type) { |
983695fa DB |
9921 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
9922 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
9923 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
9924 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
9925 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
9926 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
9927 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 9928 | range = tnum_range(1, 1); |
77241217 SF |
9929 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
9930 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) | |
9931 | range = tnum_range(0, 3); | |
ed4ed404 | 9932 | break; |
390ee7e2 | 9933 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 9934 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
9935 | range = tnum_range(0, 3); | |
9936 | enforce_attach_type_range = tnum_range(2, 3); | |
9937 | } | |
ed4ed404 | 9938 | break; |
390ee7e2 AS |
9939 | case BPF_PROG_TYPE_CGROUP_SOCK: |
9940 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 9941 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 9942 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 9943 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 9944 | break; |
15ab09bd AS |
9945 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
9946 | if (!env->prog->aux->attach_btf_id) | |
9947 | return 0; | |
9948 | range = tnum_const(0); | |
9949 | break; | |
15d83c4d | 9950 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
9951 | switch (env->prog->expected_attach_type) { |
9952 | case BPF_TRACE_FENTRY: | |
9953 | case BPF_TRACE_FEXIT: | |
9954 | range = tnum_const(0); | |
9955 | break; | |
9956 | case BPF_TRACE_RAW_TP: | |
9957 | case BPF_MODIFY_RETURN: | |
15d83c4d | 9958 | return 0; |
2ec0616e DB |
9959 | case BPF_TRACE_ITER: |
9960 | break; | |
e92888c7 YS |
9961 | default: |
9962 | return -ENOTSUPP; | |
9963 | } | |
15d83c4d | 9964 | break; |
e9ddbb77 JS |
9965 | case BPF_PROG_TYPE_SK_LOOKUP: |
9966 | range = tnum_range(SK_DROP, SK_PASS); | |
9967 | break; | |
e92888c7 YS |
9968 | case BPF_PROG_TYPE_EXT: |
9969 | /* freplace program can return anything as its return value | |
9970 | * depends on the to-be-replaced kernel func or bpf program. | |
9971 | */ | |
390ee7e2 AS |
9972 | default: |
9973 | return 0; | |
9974 | } | |
9975 | ||
390ee7e2 | 9976 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 9977 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
c25b2ae1 | 9978 | reg_type_str(env, reg->type)); |
390ee7e2 AS |
9979 | return -EINVAL; |
9980 | } | |
9981 | ||
9982 | if (!tnum_in(range, reg->var_off)) { | |
bc2591d6 | 9983 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
390ee7e2 AS |
9984 | return -EINVAL; |
9985 | } | |
5cf1e914 | 9986 | |
9987 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
9988 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
9989 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
9990 | return 0; |
9991 | } | |
9992 | ||
475fb78f AS |
9993 | /* non-recursive DFS pseudo code |
9994 | * 1 procedure DFS-iterative(G,v): | |
9995 | * 2 label v as discovered | |
9996 | * 3 let S be a stack | |
9997 | * 4 S.push(v) | |
9998 | * 5 while S is not empty | |
9999 | * 6 t <- S.pop() | |
10000 | * 7 if t is what we're looking for: | |
10001 | * 8 return t | |
10002 | * 9 for all edges e in G.adjacentEdges(t) do | |
10003 | * 10 if edge e is already labelled | |
10004 | * 11 continue with the next edge | |
10005 | * 12 w <- G.adjacentVertex(t,e) | |
10006 | * 13 if vertex w is not discovered and not explored | |
10007 | * 14 label e as tree-edge | |
10008 | * 15 label w as discovered | |
10009 | * 16 S.push(w) | |
10010 | * 17 continue at 5 | |
10011 | * 18 else if vertex w is discovered | |
10012 | * 19 label e as back-edge | |
10013 | * 20 else | |
10014 | * 21 // vertex w is explored | |
10015 | * 22 label e as forward- or cross-edge | |
10016 | * 23 label t as explored | |
10017 | * 24 S.pop() | |
10018 | * | |
10019 | * convention: | |
10020 | * 0x10 - discovered | |
10021 | * 0x11 - discovered and fall-through edge labelled | |
10022 | * 0x12 - discovered and fall-through and branch edges labelled | |
10023 | * 0x20 - explored | |
10024 | */ | |
10025 | ||
10026 | enum { | |
10027 | DISCOVERED = 0x10, | |
10028 | EXPLORED = 0x20, | |
10029 | FALLTHROUGH = 1, | |
10030 | BRANCH = 2, | |
10031 | }; | |
10032 | ||
dc2a4ebc AS |
10033 | static u32 state_htab_size(struct bpf_verifier_env *env) |
10034 | { | |
10035 | return env->prog->len; | |
10036 | } | |
10037 | ||
5d839021 AS |
10038 | static struct bpf_verifier_state_list **explored_state( |
10039 | struct bpf_verifier_env *env, | |
10040 | int idx) | |
10041 | { | |
dc2a4ebc AS |
10042 | struct bpf_verifier_state *cur = env->cur_state; |
10043 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
10044 | ||
10045 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
10046 | } |
10047 | ||
10048 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
10049 | { | |
a8f500af | 10050 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 10051 | } |
f1bca824 | 10052 | |
59e2e27d WAF |
10053 | enum { |
10054 | DONE_EXPLORING = 0, | |
10055 | KEEP_EXPLORING = 1, | |
10056 | }; | |
10057 | ||
475fb78f AS |
10058 | /* t, w, e - match pseudo-code above: |
10059 | * t - index of current instruction | |
10060 | * w - next instruction | |
10061 | * e - edge | |
10062 | */ | |
2589726d AS |
10063 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
10064 | bool loop_ok) | |
475fb78f | 10065 | { |
7df737e9 AS |
10066 | int *insn_stack = env->cfg.insn_stack; |
10067 | int *insn_state = env->cfg.insn_state; | |
10068 | ||
475fb78f | 10069 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 10070 | return DONE_EXPLORING; |
475fb78f AS |
10071 | |
10072 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 10073 | return DONE_EXPLORING; |
475fb78f AS |
10074 | |
10075 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 10076 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 10077 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
10078 | return -EINVAL; |
10079 | } | |
10080 | ||
f1bca824 AS |
10081 | if (e == BRANCH) |
10082 | /* mark branch target for state pruning */ | |
5d839021 | 10083 | init_explored_state(env, w); |
f1bca824 | 10084 | |
475fb78f AS |
10085 | if (insn_state[w] == 0) { |
10086 | /* tree-edge */ | |
10087 | insn_state[t] = DISCOVERED | e; | |
10088 | insn_state[w] = DISCOVERED; | |
7df737e9 | 10089 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 10090 | return -E2BIG; |
7df737e9 | 10091 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 10092 | return KEEP_EXPLORING; |
475fb78f | 10093 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 10094 | if (loop_ok && env->bpf_capable) |
59e2e27d | 10095 | return DONE_EXPLORING; |
d9762e84 MKL |
10096 | verbose_linfo(env, t, "%d: ", t); |
10097 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 10098 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
10099 | return -EINVAL; |
10100 | } else if (insn_state[w] == EXPLORED) { | |
10101 | /* forward- or cross-edge */ | |
10102 | insn_state[t] = DISCOVERED | e; | |
10103 | } else { | |
61bd5218 | 10104 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
10105 | return -EFAULT; |
10106 | } | |
59e2e27d WAF |
10107 | return DONE_EXPLORING; |
10108 | } | |
10109 | ||
efdb22de YS |
10110 | static int visit_func_call_insn(int t, int insn_cnt, |
10111 | struct bpf_insn *insns, | |
10112 | struct bpf_verifier_env *env, | |
10113 | bool visit_callee) | |
10114 | { | |
10115 | int ret; | |
10116 | ||
10117 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
10118 | if (ret) | |
10119 | return ret; | |
10120 | ||
10121 | if (t + 1 < insn_cnt) | |
10122 | init_explored_state(env, t + 1); | |
10123 | if (visit_callee) { | |
10124 | init_explored_state(env, t); | |
86fc6ee6 AS |
10125 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
10126 | /* It's ok to allow recursion from CFG point of | |
10127 | * view. __check_func_call() will do the actual | |
10128 | * check. | |
10129 | */ | |
10130 | bpf_pseudo_func(insns + t)); | |
efdb22de YS |
10131 | } |
10132 | return ret; | |
10133 | } | |
10134 | ||
59e2e27d WAF |
10135 | /* Visits the instruction at index t and returns one of the following: |
10136 | * < 0 - an error occurred | |
10137 | * DONE_EXPLORING - the instruction was fully explored | |
10138 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
10139 | */ | |
10140 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) | |
10141 | { | |
10142 | struct bpf_insn *insns = env->prog->insnsi; | |
10143 | int ret; | |
10144 | ||
69c087ba YS |
10145 | if (bpf_pseudo_func(insns + t)) |
10146 | return visit_func_call_insn(t, insn_cnt, insns, env, true); | |
10147 | ||
59e2e27d WAF |
10148 | /* All non-branch instructions have a single fall-through edge. */ |
10149 | if (BPF_CLASS(insns[t].code) != BPF_JMP && | |
10150 | BPF_CLASS(insns[t].code) != BPF_JMP32) | |
10151 | return push_insn(t, t + 1, FALLTHROUGH, env, false); | |
10152 | ||
10153 | switch (BPF_OP(insns[t].code)) { | |
10154 | case BPF_EXIT: | |
10155 | return DONE_EXPLORING; | |
10156 | ||
10157 | case BPF_CALL: | |
bfc6bb74 AS |
10158 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
10159 | /* Mark this call insn to trigger is_state_visited() check | |
10160 | * before call itself is processed by __check_func_call(). | |
10161 | * Otherwise new async state will be pushed for further | |
10162 | * exploration. | |
10163 | */ | |
10164 | init_explored_state(env, t); | |
efdb22de YS |
10165 | return visit_func_call_insn(t, insn_cnt, insns, env, |
10166 | insns[t].src_reg == BPF_PSEUDO_CALL); | |
59e2e27d WAF |
10167 | |
10168 | case BPF_JA: | |
10169 | if (BPF_SRC(insns[t].code) != BPF_K) | |
10170 | return -EINVAL; | |
10171 | ||
10172 | /* unconditional jump with single edge */ | |
10173 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, | |
10174 | true); | |
10175 | if (ret) | |
10176 | return ret; | |
10177 | ||
10178 | /* unconditional jmp is not a good pruning point, | |
10179 | * but it's marked, since backtracking needs | |
10180 | * to record jmp history in is_state_visited(). | |
10181 | */ | |
10182 | init_explored_state(env, t + insns[t].off + 1); | |
10183 | /* tell verifier to check for equivalent states | |
10184 | * after every call and jump | |
10185 | */ | |
10186 | if (t + 1 < insn_cnt) | |
10187 | init_explored_state(env, t + 1); | |
10188 | ||
10189 | return ret; | |
10190 | ||
10191 | default: | |
10192 | /* conditional jump with two edges */ | |
10193 | init_explored_state(env, t); | |
10194 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); | |
10195 | if (ret) | |
10196 | return ret; | |
10197 | ||
10198 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); | |
10199 | } | |
475fb78f AS |
10200 | } |
10201 | ||
10202 | /* non-recursive depth-first-search to detect loops in BPF program | |
10203 | * loop == back-edge in directed graph | |
10204 | */ | |
58e2af8b | 10205 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 10206 | { |
475fb78f | 10207 | int insn_cnt = env->prog->len; |
7df737e9 | 10208 | int *insn_stack, *insn_state; |
475fb78f | 10209 | int ret = 0; |
59e2e27d | 10210 | int i; |
475fb78f | 10211 | |
7df737e9 | 10212 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
10213 | if (!insn_state) |
10214 | return -ENOMEM; | |
10215 | ||
7df737e9 | 10216 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 10217 | if (!insn_stack) { |
71dde681 | 10218 | kvfree(insn_state); |
475fb78f AS |
10219 | return -ENOMEM; |
10220 | } | |
10221 | ||
10222 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
10223 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 10224 | env->cfg.cur_stack = 1; |
475fb78f | 10225 | |
59e2e27d WAF |
10226 | while (env->cfg.cur_stack > 0) { |
10227 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 10228 | |
59e2e27d WAF |
10229 | ret = visit_insn(t, insn_cnt, env); |
10230 | switch (ret) { | |
10231 | case DONE_EXPLORING: | |
10232 | insn_state[t] = EXPLORED; | |
10233 | env->cfg.cur_stack--; | |
10234 | break; | |
10235 | case KEEP_EXPLORING: | |
10236 | break; | |
10237 | default: | |
10238 | if (ret > 0) { | |
10239 | verbose(env, "visit_insn internal bug\n"); | |
10240 | ret = -EFAULT; | |
475fb78f | 10241 | } |
475fb78f | 10242 | goto err_free; |
59e2e27d | 10243 | } |
475fb78f AS |
10244 | } |
10245 | ||
59e2e27d | 10246 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 10247 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
10248 | ret = -EFAULT; |
10249 | goto err_free; | |
10250 | } | |
475fb78f | 10251 | |
475fb78f AS |
10252 | for (i = 0; i < insn_cnt; i++) { |
10253 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 10254 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
10255 | ret = -EINVAL; |
10256 | goto err_free; | |
10257 | } | |
10258 | } | |
10259 | ret = 0; /* cfg looks good */ | |
10260 | ||
10261 | err_free: | |
71dde681 AS |
10262 | kvfree(insn_state); |
10263 | kvfree(insn_stack); | |
7df737e9 | 10264 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
10265 | return ret; |
10266 | } | |
10267 | ||
09b28d76 AS |
10268 | static int check_abnormal_return(struct bpf_verifier_env *env) |
10269 | { | |
10270 | int i; | |
10271 | ||
10272 | for (i = 1; i < env->subprog_cnt; i++) { | |
10273 | if (env->subprog_info[i].has_ld_abs) { | |
10274 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
10275 | return -EINVAL; | |
10276 | } | |
10277 | if (env->subprog_info[i].has_tail_call) { | |
10278 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
10279 | return -EINVAL; | |
10280 | } | |
10281 | } | |
10282 | return 0; | |
10283 | } | |
10284 | ||
838e9690 YS |
10285 | /* The minimum supported BTF func info size */ |
10286 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
10287 | #define MAX_FUNCINFO_REC_SIZE 252 | |
10288 | ||
c454a46b MKL |
10289 | static int check_btf_func(struct bpf_verifier_env *env, |
10290 | const union bpf_attr *attr, | |
af2ac3e1 | 10291 | bpfptr_t uattr) |
838e9690 | 10292 | { |
09b28d76 | 10293 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 10294 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 10295 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 10296 | struct bpf_func_info *krecord; |
8c1b6e69 | 10297 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
10298 | struct bpf_prog *prog; |
10299 | const struct btf *btf; | |
af2ac3e1 | 10300 | bpfptr_t urecord; |
d0b2818e | 10301 | u32 prev_offset = 0; |
09b28d76 | 10302 | bool scalar_return; |
e7ed83d6 | 10303 | int ret = -ENOMEM; |
838e9690 YS |
10304 | |
10305 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
10306 | if (!nfuncs) { |
10307 | if (check_abnormal_return(env)) | |
10308 | return -EINVAL; | |
838e9690 | 10309 | return 0; |
09b28d76 | 10310 | } |
838e9690 YS |
10311 | |
10312 | if (nfuncs != env->subprog_cnt) { | |
10313 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
10314 | return -EINVAL; | |
10315 | } | |
10316 | ||
10317 | urec_size = attr->func_info_rec_size; | |
10318 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
10319 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
10320 | urec_size % sizeof(u32)) { | |
10321 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
10322 | return -EINVAL; | |
10323 | } | |
10324 | ||
c454a46b MKL |
10325 | prog = env->prog; |
10326 | btf = prog->aux->btf; | |
838e9690 | 10327 | |
af2ac3e1 | 10328 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
838e9690 YS |
10329 | min_size = min_t(u32, krec_size, urec_size); |
10330 | ||
ba64e7d8 | 10331 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
10332 | if (!krecord) |
10333 | return -ENOMEM; | |
8c1b6e69 AS |
10334 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
10335 | if (!info_aux) | |
10336 | goto err_free; | |
ba64e7d8 | 10337 | |
838e9690 YS |
10338 | for (i = 0; i < nfuncs; i++) { |
10339 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
10340 | if (ret) { | |
10341 | if (ret == -E2BIG) { | |
10342 | verbose(env, "nonzero tailing record in func info"); | |
10343 | /* set the size kernel expects so loader can zero | |
10344 | * out the rest of the record. | |
10345 | */ | |
af2ac3e1 AS |
10346 | if (copy_to_bpfptr_offset(uattr, |
10347 | offsetof(union bpf_attr, func_info_rec_size), | |
10348 | &min_size, sizeof(min_size))) | |
838e9690 YS |
10349 | ret = -EFAULT; |
10350 | } | |
c454a46b | 10351 | goto err_free; |
838e9690 YS |
10352 | } |
10353 | ||
af2ac3e1 | 10354 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
838e9690 | 10355 | ret = -EFAULT; |
c454a46b | 10356 | goto err_free; |
838e9690 YS |
10357 | } |
10358 | ||
d30d42e0 | 10359 | /* check insn_off */ |
09b28d76 | 10360 | ret = -EINVAL; |
838e9690 | 10361 | if (i == 0) { |
d30d42e0 | 10362 | if (krecord[i].insn_off) { |
838e9690 | 10363 | verbose(env, |
d30d42e0 MKL |
10364 | "nonzero insn_off %u for the first func info record", |
10365 | krecord[i].insn_off); | |
c454a46b | 10366 | goto err_free; |
838e9690 | 10367 | } |
d30d42e0 | 10368 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
10369 | verbose(env, |
10370 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 10371 | krecord[i].insn_off, prev_offset); |
c454a46b | 10372 | goto err_free; |
838e9690 YS |
10373 | } |
10374 | ||
d30d42e0 | 10375 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 10376 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 10377 | goto err_free; |
838e9690 YS |
10378 | } |
10379 | ||
10380 | /* check type_id */ | |
ba64e7d8 | 10381 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 10382 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 10383 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 10384 | krecord[i].type_id); |
c454a46b | 10385 | goto err_free; |
838e9690 | 10386 | } |
51c39bb1 | 10387 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
10388 | |
10389 | func_proto = btf_type_by_id(btf, type->type); | |
10390 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
10391 | /* btf_func_check() already verified it during BTF load */ | |
10392 | goto err_free; | |
10393 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
10394 | scalar_return = | |
10395 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); | |
10396 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { | |
10397 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
10398 | goto err_free; | |
10399 | } | |
10400 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
10401 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
10402 | goto err_free; | |
10403 | } | |
10404 | ||
d30d42e0 | 10405 | prev_offset = krecord[i].insn_off; |
af2ac3e1 | 10406 | bpfptr_add(&urecord, urec_size); |
838e9690 YS |
10407 | } |
10408 | ||
ba64e7d8 YS |
10409 | prog->aux->func_info = krecord; |
10410 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 10411 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
10412 | return 0; |
10413 | ||
c454a46b | 10414 | err_free: |
ba64e7d8 | 10415 | kvfree(krecord); |
8c1b6e69 | 10416 | kfree(info_aux); |
838e9690 YS |
10417 | return ret; |
10418 | } | |
10419 | ||
ba64e7d8 YS |
10420 | static void adjust_btf_func(struct bpf_verifier_env *env) |
10421 | { | |
8c1b6e69 | 10422 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
10423 | int i; |
10424 | ||
8c1b6e69 | 10425 | if (!aux->func_info) |
ba64e7d8 YS |
10426 | return; |
10427 | ||
10428 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 10429 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
10430 | } |
10431 | ||
1b773d00 | 10432 | #define MIN_BPF_LINEINFO_SIZE offsetofend(struct bpf_line_info, line_col) |
c454a46b MKL |
10433 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
10434 | ||
10435 | static int check_btf_line(struct bpf_verifier_env *env, | |
10436 | const union bpf_attr *attr, | |
af2ac3e1 | 10437 | bpfptr_t uattr) |
c454a46b MKL |
10438 | { |
10439 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
10440 | struct bpf_subprog_info *sub; | |
10441 | struct bpf_line_info *linfo; | |
10442 | struct bpf_prog *prog; | |
10443 | const struct btf *btf; | |
af2ac3e1 | 10444 | bpfptr_t ulinfo; |
c454a46b MKL |
10445 | int err; |
10446 | ||
10447 | nr_linfo = attr->line_info_cnt; | |
10448 | if (!nr_linfo) | |
10449 | return 0; | |
0e6491b5 BC |
10450 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
10451 | return -EINVAL; | |
c454a46b MKL |
10452 | |
10453 | rec_size = attr->line_info_rec_size; | |
10454 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
10455 | rec_size > MAX_LINEINFO_REC_SIZE || | |
10456 | rec_size & (sizeof(u32) - 1)) | |
10457 | return -EINVAL; | |
10458 | ||
10459 | /* Need to zero it in case the userspace may | |
10460 | * pass in a smaller bpf_line_info object. | |
10461 | */ | |
10462 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
10463 | GFP_KERNEL | __GFP_NOWARN); | |
10464 | if (!linfo) | |
10465 | return -ENOMEM; | |
10466 | ||
10467 | prog = env->prog; | |
10468 | btf = prog->aux->btf; | |
10469 | ||
10470 | s = 0; | |
10471 | sub = env->subprog_info; | |
af2ac3e1 | 10472 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
c454a46b MKL |
10473 | expected_size = sizeof(struct bpf_line_info); |
10474 | ncopy = min_t(u32, expected_size, rec_size); | |
10475 | for (i = 0; i < nr_linfo; i++) { | |
10476 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
10477 | if (err) { | |
10478 | if (err == -E2BIG) { | |
10479 | verbose(env, "nonzero tailing record in line_info"); | |
af2ac3e1 AS |
10480 | if (copy_to_bpfptr_offset(uattr, |
10481 | offsetof(union bpf_attr, line_info_rec_size), | |
10482 | &expected_size, sizeof(expected_size))) | |
c454a46b MKL |
10483 | err = -EFAULT; |
10484 | } | |
10485 | goto err_free; | |
10486 | } | |
10487 | ||
af2ac3e1 | 10488 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
c454a46b MKL |
10489 | err = -EFAULT; |
10490 | goto err_free; | |
10491 | } | |
10492 | ||
10493 | /* | |
10494 | * Check insn_off to ensure | |
10495 | * 1) strictly increasing AND | |
10496 | * 2) bounded by prog->len | |
10497 | * | |
10498 | * The linfo[0].insn_off == 0 check logically falls into | |
10499 | * the later "missing bpf_line_info for func..." case | |
10500 | * because the first linfo[0].insn_off must be the | |
10501 | * first sub also and the first sub must have | |
10502 | * subprog_info[0].start == 0. | |
10503 | */ | |
10504 | if ((i && linfo[i].insn_off <= prev_offset) || | |
10505 | linfo[i].insn_off >= prog->len) { | |
10506 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
10507 | i, linfo[i].insn_off, prev_offset, | |
10508 | prog->len); | |
10509 | err = -EINVAL; | |
10510 | goto err_free; | |
10511 | } | |
10512 | ||
fdbaa0be MKL |
10513 | if (!prog->insnsi[linfo[i].insn_off].code) { |
10514 | verbose(env, | |
10515 | "Invalid insn code at line_info[%u].insn_off\n", | |
10516 | i); | |
10517 | err = -EINVAL; | |
10518 | goto err_free; | |
10519 | } | |
10520 | ||
23127b33 MKL |
10521 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
10522 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
10523 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
10524 | err = -EINVAL; | |
10525 | goto err_free; | |
10526 | } | |
10527 | ||
10528 | if (s != env->subprog_cnt) { | |
10529 | if (linfo[i].insn_off == sub[s].start) { | |
10530 | sub[s].linfo_idx = i; | |
10531 | s++; | |
10532 | } else if (sub[s].start < linfo[i].insn_off) { | |
10533 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
10534 | err = -EINVAL; | |
10535 | goto err_free; | |
10536 | } | |
10537 | } | |
10538 | ||
10539 | prev_offset = linfo[i].insn_off; | |
af2ac3e1 | 10540 | bpfptr_add(&ulinfo, rec_size); |
c454a46b MKL |
10541 | } |
10542 | ||
10543 | if (s != env->subprog_cnt) { | |
10544 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
10545 | env->subprog_cnt - s, s); | |
10546 | err = -EINVAL; | |
10547 | goto err_free; | |
10548 | } | |
10549 | ||
10550 | prog->aux->linfo = linfo; | |
10551 | prog->aux->nr_linfo = nr_linfo; | |
10552 | ||
10553 | return 0; | |
10554 | ||
10555 | err_free: | |
10556 | kvfree(linfo); | |
10557 | return err; | |
10558 | } | |
10559 | ||
fbd94c7a AS |
10560 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
10561 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE | |
10562 | ||
10563 | static int check_core_relo(struct bpf_verifier_env *env, | |
10564 | const union bpf_attr *attr, | |
10565 | bpfptr_t uattr) | |
10566 | { | |
10567 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; | |
10568 | struct bpf_core_relo core_relo = {}; | |
10569 | struct bpf_prog *prog = env->prog; | |
10570 | const struct btf *btf = prog->aux->btf; | |
10571 | struct bpf_core_ctx ctx = { | |
10572 | .log = &env->log, | |
10573 | .btf = btf, | |
10574 | }; | |
10575 | bpfptr_t u_core_relo; | |
10576 | int err; | |
10577 | ||
10578 | nr_core_relo = attr->core_relo_cnt; | |
10579 | if (!nr_core_relo) | |
10580 | return 0; | |
10581 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) | |
10582 | return -EINVAL; | |
10583 | ||
10584 | rec_size = attr->core_relo_rec_size; | |
10585 | if (rec_size < MIN_CORE_RELO_SIZE || | |
10586 | rec_size > MAX_CORE_RELO_SIZE || | |
10587 | rec_size % sizeof(u32)) | |
10588 | return -EINVAL; | |
10589 | ||
10590 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); | |
10591 | expected_size = sizeof(struct bpf_core_relo); | |
10592 | ncopy = min_t(u32, expected_size, rec_size); | |
10593 | ||
10594 | /* Unlike func_info and line_info, copy and apply each CO-RE | |
10595 | * relocation record one at a time. | |
10596 | */ | |
10597 | for (i = 0; i < nr_core_relo; i++) { | |
10598 | /* future proofing when sizeof(bpf_core_relo) changes */ | |
10599 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); | |
10600 | if (err) { | |
10601 | if (err == -E2BIG) { | |
10602 | verbose(env, "nonzero tailing record in core_relo"); | |
10603 | if (copy_to_bpfptr_offset(uattr, | |
10604 | offsetof(union bpf_attr, core_relo_rec_size), | |
10605 | &expected_size, sizeof(expected_size))) | |
10606 | err = -EFAULT; | |
10607 | } | |
10608 | break; | |
10609 | } | |
10610 | ||
10611 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { | |
10612 | err = -EFAULT; | |
10613 | break; | |
10614 | } | |
10615 | ||
10616 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { | |
10617 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", | |
10618 | i, core_relo.insn_off, prog->len); | |
10619 | err = -EINVAL; | |
10620 | break; | |
10621 | } | |
10622 | ||
10623 | err = bpf_core_apply(&ctx, &core_relo, i, | |
10624 | &prog->insnsi[core_relo.insn_off / 8]); | |
10625 | if (err) | |
10626 | break; | |
10627 | bpfptr_add(&u_core_relo, rec_size); | |
10628 | } | |
10629 | return err; | |
10630 | } | |
10631 | ||
c454a46b MKL |
10632 | static int check_btf_info(struct bpf_verifier_env *env, |
10633 | const union bpf_attr *attr, | |
af2ac3e1 | 10634 | bpfptr_t uattr) |
c454a46b MKL |
10635 | { |
10636 | struct btf *btf; | |
10637 | int err; | |
10638 | ||
09b28d76 AS |
10639 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
10640 | if (check_abnormal_return(env)) | |
10641 | return -EINVAL; | |
c454a46b | 10642 | return 0; |
09b28d76 | 10643 | } |
c454a46b MKL |
10644 | |
10645 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
10646 | if (IS_ERR(btf)) | |
10647 | return PTR_ERR(btf); | |
350a5c4d AS |
10648 | if (btf_is_kernel(btf)) { |
10649 | btf_put(btf); | |
10650 | return -EACCES; | |
10651 | } | |
c454a46b MKL |
10652 | env->prog->aux->btf = btf; |
10653 | ||
10654 | err = check_btf_func(env, attr, uattr); | |
10655 | if (err) | |
10656 | return err; | |
10657 | ||
10658 | err = check_btf_line(env, attr, uattr); | |
10659 | if (err) | |
10660 | return err; | |
10661 | ||
fbd94c7a AS |
10662 | err = check_core_relo(env, attr, uattr); |
10663 | if (err) | |
10664 | return err; | |
10665 | ||
c454a46b | 10666 | return 0; |
ba64e7d8 YS |
10667 | } |
10668 | ||
f1174f77 EC |
10669 | /* check %cur's range satisfies %old's */ |
10670 | static bool range_within(struct bpf_reg_state *old, | |
10671 | struct bpf_reg_state *cur) | |
10672 | { | |
b03c9f9f EC |
10673 | return old->umin_value <= cur->umin_value && |
10674 | old->umax_value >= cur->umax_value && | |
10675 | old->smin_value <= cur->smin_value && | |
fd675184 DB |
10676 | old->smax_value >= cur->smax_value && |
10677 | old->u32_min_value <= cur->u32_min_value && | |
10678 | old->u32_max_value >= cur->u32_max_value && | |
10679 | old->s32_min_value <= cur->s32_min_value && | |
10680 | old->s32_max_value >= cur->s32_max_value; | |
f1174f77 EC |
10681 | } |
10682 | ||
f1174f77 EC |
10683 | /* If in the old state two registers had the same id, then they need to have |
10684 | * the same id in the new state as well. But that id could be different from | |
10685 | * the old state, so we need to track the mapping from old to new ids. | |
10686 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
10687 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
10688 | * regs with a different old id could still have new id 9, we don't care about | |
10689 | * that. | |
10690 | * So we look through our idmap to see if this old id has been seen before. If | |
10691 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 10692 | */ |
c9e73e3d | 10693 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
969bf05e | 10694 | { |
f1174f77 | 10695 | unsigned int i; |
969bf05e | 10696 | |
c9e73e3d | 10697 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
f1174f77 EC |
10698 | if (!idmap[i].old) { |
10699 | /* Reached an empty slot; haven't seen this id before */ | |
10700 | idmap[i].old = old_id; | |
10701 | idmap[i].cur = cur_id; | |
10702 | return true; | |
10703 | } | |
10704 | if (idmap[i].old == old_id) | |
10705 | return idmap[i].cur == cur_id; | |
10706 | } | |
10707 | /* We ran out of idmap slots, which should be impossible */ | |
10708 | WARN_ON_ONCE(1); | |
10709 | return false; | |
10710 | } | |
10711 | ||
9242b5f5 AS |
10712 | static void clean_func_state(struct bpf_verifier_env *env, |
10713 | struct bpf_func_state *st) | |
10714 | { | |
10715 | enum bpf_reg_liveness live; | |
10716 | int i, j; | |
10717 | ||
10718 | for (i = 0; i < BPF_REG_FP; i++) { | |
10719 | live = st->regs[i].live; | |
10720 | /* liveness must not touch this register anymore */ | |
10721 | st->regs[i].live |= REG_LIVE_DONE; | |
10722 | if (!(live & REG_LIVE_READ)) | |
10723 | /* since the register is unused, clear its state | |
10724 | * to make further comparison simpler | |
10725 | */ | |
f54c7898 | 10726 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
10727 | } |
10728 | ||
10729 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
10730 | live = st->stack[i].spilled_ptr.live; | |
10731 | /* liveness must not touch this stack slot anymore */ | |
10732 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
10733 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 10734 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
10735 | for (j = 0; j < BPF_REG_SIZE; j++) |
10736 | st->stack[i].slot_type[j] = STACK_INVALID; | |
10737 | } | |
10738 | } | |
10739 | } | |
10740 | ||
10741 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
10742 | struct bpf_verifier_state *st) | |
10743 | { | |
10744 | int i; | |
10745 | ||
10746 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
10747 | /* all regs in this state in all frames were already marked */ | |
10748 | return; | |
10749 | ||
10750 | for (i = 0; i <= st->curframe; i++) | |
10751 | clean_func_state(env, st->frame[i]); | |
10752 | } | |
10753 | ||
10754 | /* the parentage chains form a tree. | |
10755 | * the verifier states are added to state lists at given insn and | |
10756 | * pushed into state stack for future exploration. | |
10757 | * when the verifier reaches bpf_exit insn some of the verifer states | |
10758 | * stored in the state lists have their final liveness state already, | |
10759 | * but a lot of states will get revised from liveness point of view when | |
10760 | * the verifier explores other branches. | |
10761 | * Example: | |
10762 | * 1: r0 = 1 | |
10763 | * 2: if r1 == 100 goto pc+1 | |
10764 | * 3: r0 = 2 | |
10765 | * 4: exit | |
10766 | * when the verifier reaches exit insn the register r0 in the state list of | |
10767 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
10768 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
10769 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
10770 | * | |
10771 | * Since the verifier pushes the branch states as it sees them while exploring | |
10772 | * the program the condition of walking the branch instruction for the second | |
10773 | * time means that all states below this branch were already explored and | |
8fb33b60 | 10774 | * their final liveness marks are already propagated. |
9242b5f5 AS |
10775 | * Hence when the verifier completes the search of state list in is_state_visited() |
10776 | * we can call this clean_live_states() function to mark all liveness states | |
10777 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
10778 | * will not be used. | |
10779 | * This function also clears the registers and stack for states that !READ | |
10780 | * to simplify state merging. | |
10781 | * | |
10782 | * Important note here that walking the same branch instruction in the callee | |
10783 | * doesn't meant that the states are DONE. The verifier has to compare | |
10784 | * the callsites | |
10785 | */ | |
10786 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
10787 | struct bpf_verifier_state *cur) | |
10788 | { | |
10789 | struct bpf_verifier_state_list *sl; | |
10790 | int i; | |
10791 | ||
5d839021 | 10792 | sl = *explored_state(env, insn); |
a8f500af | 10793 | while (sl) { |
2589726d AS |
10794 | if (sl->state.branches) |
10795 | goto next; | |
dc2a4ebc AS |
10796 | if (sl->state.insn_idx != insn || |
10797 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
10798 | goto next; |
10799 | for (i = 0; i <= cur->curframe; i++) | |
10800 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
10801 | goto next; | |
10802 | clean_verifier_state(env, &sl->state); | |
10803 | next: | |
10804 | sl = sl->next; | |
10805 | } | |
10806 | } | |
10807 | ||
f1174f77 | 10808 | /* Returns true if (rold safe implies rcur safe) */ |
e042aa53 DB |
10809 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
10810 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) | |
f1174f77 | 10811 | { |
f4d7e40a AS |
10812 | bool equal; |
10813 | ||
dc503a8a EC |
10814 | if (!(rold->live & REG_LIVE_READ)) |
10815 | /* explored state didn't use this */ | |
10816 | return true; | |
10817 | ||
679c782d | 10818 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
10819 | |
10820 | if (rold->type == PTR_TO_STACK) | |
10821 | /* two stack pointers are equal only if they're pointing to | |
10822 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
10823 | */ | |
10824 | return equal && rold->frameno == rcur->frameno; | |
10825 | ||
10826 | if (equal) | |
969bf05e AS |
10827 | return true; |
10828 | ||
f1174f77 EC |
10829 | if (rold->type == NOT_INIT) |
10830 | /* explored state can't have used this */ | |
969bf05e | 10831 | return true; |
f1174f77 EC |
10832 | if (rcur->type == NOT_INIT) |
10833 | return false; | |
c25b2ae1 | 10834 | switch (base_type(rold->type)) { |
f1174f77 | 10835 | case SCALAR_VALUE: |
e042aa53 DB |
10836 | if (env->explore_alu_limits) |
10837 | return false; | |
f1174f77 | 10838 | if (rcur->type == SCALAR_VALUE) { |
b5dc0163 AS |
10839 | if (!rold->precise && !rcur->precise) |
10840 | return true; | |
f1174f77 EC |
10841 | /* new val must satisfy old val knowledge */ |
10842 | return range_within(rold, rcur) && | |
10843 | tnum_in(rold->var_off, rcur->var_off); | |
10844 | } else { | |
179d1c56 JH |
10845 | /* We're trying to use a pointer in place of a scalar. |
10846 | * Even if the scalar was unbounded, this could lead to | |
10847 | * pointer leaks because scalars are allowed to leak | |
10848 | * while pointers are not. We could make this safe in | |
10849 | * special cases if root is calling us, but it's | |
10850 | * probably not worth the hassle. | |
f1174f77 | 10851 | */ |
179d1c56 | 10852 | return false; |
f1174f77 | 10853 | } |
69c087ba | 10854 | case PTR_TO_MAP_KEY: |
f1174f77 | 10855 | case PTR_TO_MAP_VALUE: |
c25b2ae1 HL |
10856 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
10857 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
10858 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
10859 | * checked, doing so could have affected others with the same | |
10860 | * id, and we can't check for that because we lost the id when | |
10861 | * we converted to a PTR_TO_MAP_VALUE. | |
10862 | */ | |
10863 | if (type_may_be_null(rold->type)) { | |
10864 | if (!type_may_be_null(rcur->type)) | |
10865 | return false; | |
10866 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
10867 | return false; | |
10868 | /* Check our ids match any regs they're supposed to */ | |
10869 | return check_ids(rold->id, rcur->id, idmap); | |
10870 | } | |
10871 | ||
1b688a19 EC |
10872 | /* If the new min/max/var_off satisfy the old ones and |
10873 | * everything else matches, we are OK. | |
d83525ca AS |
10874 | * 'id' is not compared, since it's only used for maps with |
10875 | * bpf_spin_lock inside map element and in such cases if | |
10876 | * the rest of the prog is valid for one map element then | |
10877 | * it's valid for all map elements regardless of the key | |
10878 | * used in bpf_map_lookup() | |
1b688a19 EC |
10879 | */ |
10880 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
10881 | range_within(rold, rcur) && | |
10882 | tnum_in(rold->var_off, rcur->var_off); | |
de8f3a83 | 10883 | case PTR_TO_PACKET_META: |
f1174f77 | 10884 | case PTR_TO_PACKET: |
de8f3a83 | 10885 | if (rcur->type != rold->type) |
f1174f77 EC |
10886 | return false; |
10887 | /* We must have at least as much range as the old ptr | |
10888 | * did, so that any accesses which were safe before are | |
10889 | * still safe. This is true even if old range < old off, | |
10890 | * since someone could have accessed through (ptr - k), or | |
10891 | * even done ptr -= k in a register, to get a safe access. | |
10892 | */ | |
10893 | if (rold->range > rcur->range) | |
10894 | return false; | |
10895 | /* If the offsets don't match, we can't trust our alignment; | |
10896 | * nor can we be sure that we won't fall out of range. | |
10897 | */ | |
10898 | if (rold->off != rcur->off) | |
10899 | return false; | |
10900 | /* id relations must be preserved */ | |
10901 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
10902 | return false; | |
10903 | /* new val must satisfy old val knowledge */ | |
10904 | return range_within(rold, rcur) && | |
10905 | tnum_in(rold->var_off, rcur->var_off); | |
10906 | case PTR_TO_CTX: | |
10907 | case CONST_PTR_TO_MAP: | |
f1174f77 | 10908 | case PTR_TO_PACKET_END: |
d58e468b | 10909 | case PTR_TO_FLOW_KEYS: |
c64b7983 | 10910 | case PTR_TO_SOCKET: |
46f8bc92 | 10911 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 10912 | case PTR_TO_TCP_SOCK: |
fada7fdc | 10913 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
10914 | /* Only valid matches are exact, which memcmp() above |
10915 | * would have accepted | |
10916 | */ | |
10917 | default: | |
10918 | /* Don't know what's going on, just say it's not safe */ | |
10919 | return false; | |
10920 | } | |
969bf05e | 10921 | |
f1174f77 EC |
10922 | /* Shouldn't get here; if we do, say it's not safe */ |
10923 | WARN_ON_ONCE(1); | |
969bf05e AS |
10924 | return false; |
10925 | } | |
10926 | ||
e042aa53 DB |
10927 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
10928 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) | |
638f5b90 AS |
10929 | { |
10930 | int i, spi; | |
10931 | ||
638f5b90 AS |
10932 | /* walk slots of the explored stack and ignore any additional |
10933 | * slots in the current stack, since explored(safe) state | |
10934 | * didn't use them | |
10935 | */ | |
10936 | for (i = 0; i < old->allocated_stack; i++) { | |
10937 | spi = i / BPF_REG_SIZE; | |
10938 | ||
b233920c AS |
10939 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
10940 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 10941 | /* explored state didn't use this */ |
fd05e57b | 10942 | continue; |
b233920c | 10943 | } |
cc2b14d5 | 10944 | |
638f5b90 AS |
10945 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
10946 | continue; | |
19e2dbb7 AS |
10947 | |
10948 | /* explored stack has more populated slots than current stack | |
10949 | * and these slots were used | |
10950 | */ | |
10951 | if (i >= cur->allocated_stack) | |
10952 | return false; | |
10953 | ||
cc2b14d5 AS |
10954 | /* if old state was safe with misc data in the stack |
10955 | * it will be safe with zero-initialized stack. | |
10956 | * The opposite is not true | |
10957 | */ | |
10958 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
10959 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
10960 | continue; | |
638f5b90 AS |
10961 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
10962 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
10963 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 10964 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
10965 | * this verifier states are not equivalent, |
10966 | * return false to continue verification of this path | |
10967 | */ | |
10968 | return false; | |
27113c59 | 10969 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
638f5b90 | 10970 | continue; |
27113c59 | 10971 | if (!is_spilled_reg(&old->stack[spi])) |
638f5b90 | 10972 | continue; |
e042aa53 DB |
10973 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
10974 | &cur->stack[spi].spilled_ptr, idmap)) | |
638f5b90 AS |
10975 | /* when explored and current stack slot are both storing |
10976 | * spilled registers, check that stored pointers types | |
10977 | * are the same as well. | |
10978 | * Ex: explored safe path could have stored | |
10979 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
10980 | * but current path has stored: | |
10981 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
10982 | * such verifier states are not equivalent. | |
10983 | * return false to continue verification of this path | |
10984 | */ | |
10985 | return false; | |
10986 | } | |
10987 | return true; | |
10988 | } | |
10989 | ||
fd978bf7 JS |
10990 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
10991 | { | |
10992 | if (old->acquired_refs != cur->acquired_refs) | |
10993 | return false; | |
10994 | return !memcmp(old->refs, cur->refs, | |
10995 | sizeof(*old->refs) * old->acquired_refs); | |
10996 | } | |
10997 | ||
f1bca824 AS |
10998 | /* compare two verifier states |
10999 | * | |
11000 | * all states stored in state_list are known to be valid, since | |
11001 | * verifier reached 'bpf_exit' instruction through them | |
11002 | * | |
11003 | * this function is called when verifier exploring different branches of | |
11004 | * execution popped from the state stack. If it sees an old state that has | |
11005 | * more strict register state and more strict stack state then this execution | |
11006 | * branch doesn't need to be explored further, since verifier already | |
11007 | * concluded that more strict state leads to valid finish. | |
11008 | * | |
11009 | * Therefore two states are equivalent if register state is more conservative | |
11010 | * and explored stack state is more conservative than the current one. | |
11011 | * Example: | |
11012 | * explored current | |
11013 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
11014 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
11015 | * | |
11016 | * In other words if current stack state (one being explored) has more | |
11017 | * valid slots than old one that already passed validation, it means | |
11018 | * the verifier can stop exploring and conclude that current state is valid too | |
11019 | * | |
11020 | * Similarly with registers. If explored state has register type as invalid | |
11021 | * whereas register type in current state is meaningful, it means that | |
11022 | * the current state will reach 'bpf_exit' instruction safely | |
11023 | */ | |
c9e73e3d | 11024 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
f4d7e40a | 11025 | struct bpf_func_state *cur) |
f1bca824 AS |
11026 | { |
11027 | int i; | |
11028 | ||
c9e73e3d LB |
11029 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
11030 | for (i = 0; i < MAX_BPF_REG; i++) | |
e042aa53 DB |
11031 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
11032 | env->idmap_scratch)) | |
c9e73e3d | 11033 | return false; |
f1bca824 | 11034 | |
e042aa53 | 11035 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
c9e73e3d | 11036 | return false; |
fd978bf7 JS |
11037 | |
11038 | if (!refsafe(old, cur)) | |
c9e73e3d LB |
11039 | return false; |
11040 | ||
11041 | return true; | |
f1bca824 AS |
11042 | } |
11043 | ||
f4d7e40a AS |
11044 | static bool states_equal(struct bpf_verifier_env *env, |
11045 | struct bpf_verifier_state *old, | |
11046 | struct bpf_verifier_state *cur) | |
11047 | { | |
11048 | int i; | |
11049 | ||
11050 | if (old->curframe != cur->curframe) | |
11051 | return false; | |
11052 | ||
979d63d5 DB |
11053 | /* Verification state from speculative execution simulation |
11054 | * must never prune a non-speculative execution one. | |
11055 | */ | |
11056 | if (old->speculative && !cur->speculative) | |
11057 | return false; | |
11058 | ||
d83525ca AS |
11059 | if (old->active_spin_lock != cur->active_spin_lock) |
11060 | return false; | |
11061 | ||
f4d7e40a AS |
11062 | /* for states to be equal callsites have to be the same |
11063 | * and all frame states need to be equivalent | |
11064 | */ | |
11065 | for (i = 0; i <= old->curframe; i++) { | |
11066 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
11067 | return false; | |
c9e73e3d | 11068 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
f4d7e40a AS |
11069 | return false; |
11070 | } | |
11071 | return true; | |
11072 | } | |
11073 | ||
5327ed3d JW |
11074 | /* Return 0 if no propagation happened. Return negative error code if error |
11075 | * happened. Otherwise, return the propagated bit. | |
11076 | */ | |
55e7f3b5 JW |
11077 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
11078 | struct bpf_reg_state *reg, | |
11079 | struct bpf_reg_state *parent_reg) | |
11080 | { | |
5327ed3d JW |
11081 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
11082 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
11083 | int err; |
11084 | ||
5327ed3d JW |
11085 | /* When comes here, read flags of PARENT_REG or REG could be any of |
11086 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
11087 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
11088 | */ | |
11089 | if (parent_flag == REG_LIVE_READ64 || | |
11090 | /* Or if there is no read flag from REG. */ | |
11091 | !flag || | |
11092 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
11093 | parent_flag == flag) | |
55e7f3b5 JW |
11094 | return 0; |
11095 | ||
5327ed3d | 11096 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
11097 | if (err) |
11098 | return err; | |
11099 | ||
5327ed3d | 11100 | return flag; |
55e7f3b5 JW |
11101 | } |
11102 | ||
8e9cd9ce | 11103 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
11104 | * straight-line code between a state and its parent. When we arrive at an |
11105 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
11106 | * code, so read marks in the state must propagate to the parent regardless | |
11107 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 11108 | * in mark_reg_read() is for. |
8e9cd9ce | 11109 | */ |
f4d7e40a AS |
11110 | static int propagate_liveness(struct bpf_verifier_env *env, |
11111 | const struct bpf_verifier_state *vstate, | |
11112 | struct bpf_verifier_state *vparent) | |
dc503a8a | 11113 | { |
3f8cafa4 | 11114 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 11115 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 11116 | int i, frame, err = 0; |
dc503a8a | 11117 | |
f4d7e40a AS |
11118 | if (vparent->curframe != vstate->curframe) { |
11119 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
11120 | vparent->curframe, vstate->curframe); | |
11121 | return -EFAULT; | |
11122 | } | |
dc503a8a EC |
11123 | /* Propagate read liveness of registers... */ |
11124 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 11125 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
11126 | parent = vparent->frame[frame]; |
11127 | state = vstate->frame[frame]; | |
11128 | parent_reg = parent->regs; | |
11129 | state_reg = state->regs; | |
83d16312 JK |
11130 | /* We don't need to worry about FP liveness, it's read-only */ |
11131 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
11132 | err = propagate_liveness_reg(env, &state_reg[i], |
11133 | &parent_reg[i]); | |
5327ed3d | 11134 | if (err < 0) |
3f8cafa4 | 11135 | return err; |
5327ed3d JW |
11136 | if (err == REG_LIVE_READ64) |
11137 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 11138 | } |
f4d7e40a | 11139 | |
1b04aee7 | 11140 | /* Propagate stack slots. */ |
f4d7e40a AS |
11141 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
11142 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
11143 | parent_reg = &parent->stack[i].spilled_ptr; |
11144 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
11145 | err = propagate_liveness_reg(env, state_reg, |
11146 | parent_reg); | |
5327ed3d | 11147 | if (err < 0) |
3f8cafa4 | 11148 | return err; |
dc503a8a EC |
11149 | } |
11150 | } | |
5327ed3d | 11151 | return 0; |
dc503a8a EC |
11152 | } |
11153 | ||
a3ce685d AS |
11154 | /* find precise scalars in the previous equivalent state and |
11155 | * propagate them into the current state | |
11156 | */ | |
11157 | static int propagate_precision(struct bpf_verifier_env *env, | |
11158 | const struct bpf_verifier_state *old) | |
11159 | { | |
11160 | struct bpf_reg_state *state_reg; | |
11161 | struct bpf_func_state *state; | |
11162 | int i, err = 0; | |
11163 | ||
11164 | state = old->frame[old->curframe]; | |
11165 | state_reg = state->regs; | |
11166 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
11167 | if (state_reg->type != SCALAR_VALUE || | |
11168 | !state_reg->precise) | |
11169 | continue; | |
11170 | if (env->log.level & BPF_LOG_LEVEL2) | |
11171 | verbose(env, "propagating r%d\n", i); | |
11172 | err = mark_chain_precision(env, i); | |
11173 | if (err < 0) | |
11174 | return err; | |
11175 | } | |
11176 | ||
11177 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
27113c59 | 11178 | if (!is_spilled_reg(&state->stack[i])) |
a3ce685d AS |
11179 | continue; |
11180 | state_reg = &state->stack[i].spilled_ptr; | |
11181 | if (state_reg->type != SCALAR_VALUE || | |
11182 | !state_reg->precise) | |
11183 | continue; | |
11184 | if (env->log.level & BPF_LOG_LEVEL2) | |
11185 | verbose(env, "propagating fp%d\n", | |
11186 | (-i - 1) * BPF_REG_SIZE); | |
11187 | err = mark_chain_precision_stack(env, i); | |
11188 | if (err < 0) | |
11189 | return err; | |
11190 | } | |
11191 | return 0; | |
11192 | } | |
11193 | ||
2589726d AS |
11194 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
11195 | struct bpf_verifier_state *cur) | |
11196 | { | |
11197 | struct bpf_func_state *fold, *fcur; | |
11198 | int i, fr = cur->curframe; | |
11199 | ||
11200 | if (old->curframe != fr) | |
11201 | return false; | |
11202 | ||
11203 | fold = old->frame[fr]; | |
11204 | fcur = cur->frame[fr]; | |
11205 | for (i = 0; i < MAX_BPF_REG; i++) | |
11206 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
11207 | offsetof(struct bpf_reg_state, parent))) | |
11208 | return false; | |
11209 | return true; | |
11210 | } | |
11211 | ||
11212 | ||
58e2af8b | 11213 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 11214 | { |
58e2af8b | 11215 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 11216 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 11217 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 11218 | int i, j, err, states_cnt = 0; |
10d274e8 | 11219 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 11220 | |
b5dc0163 | 11221 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 11222 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
11223 | /* this 'insn_idx' instruction wasn't marked, so we will not |
11224 | * be doing state search here | |
11225 | */ | |
11226 | return 0; | |
11227 | ||
2589726d AS |
11228 | /* bpf progs typically have pruning point every 4 instructions |
11229 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
11230 | * Do not add new state for future pruning if the verifier hasn't seen | |
11231 | * at least 2 jumps and at least 8 instructions. | |
11232 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
11233 | * In tests that amounts to up to 50% reduction into total verifier | |
11234 | * memory consumption and 20% verifier time speedup. | |
11235 | */ | |
11236 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
11237 | env->insn_processed - env->prev_insn_processed >= 8) | |
11238 | add_new_state = true; | |
11239 | ||
a8f500af AS |
11240 | pprev = explored_state(env, insn_idx); |
11241 | sl = *pprev; | |
11242 | ||
9242b5f5 AS |
11243 | clean_live_states(env, insn_idx, cur); |
11244 | ||
a8f500af | 11245 | while (sl) { |
dc2a4ebc AS |
11246 | states_cnt++; |
11247 | if (sl->state.insn_idx != insn_idx) | |
11248 | goto next; | |
bfc6bb74 | 11249 | |
2589726d | 11250 | if (sl->state.branches) { |
bfc6bb74 AS |
11251 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
11252 | ||
11253 | if (frame->in_async_callback_fn && | |
11254 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { | |
11255 | /* Different async_entry_cnt means that the verifier is | |
11256 | * processing another entry into async callback. | |
11257 | * Seeing the same state is not an indication of infinite | |
11258 | * loop or infinite recursion. | |
11259 | * But finding the same state doesn't mean that it's safe | |
11260 | * to stop processing the current state. The previous state | |
11261 | * hasn't yet reached bpf_exit, since state.branches > 0. | |
11262 | * Checking in_async_callback_fn alone is not enough either. | |
11263 | * Since the verifier still needs to catch infinite loops | |
11264 | * inside async callbacks. | |
11265 | */ | |
11266 | } else if (states_maybe_looping(&sl->state, cur) && | |
11267 | states_equal(env, &sl->state, cur)) { | |
2589726d AS |
11268 | verbose_linfo(env, insn_idx, "; "); |
11269 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
11270 | return -EINVAL; | |
11271 | } | |
11272 | /* if the verifier is processing a loop, avoid adding new state | |
11273 | * too often, since different loop iterations have distinct | |
11274 | * states and may not help future pruning. | |
11275 | * This threshold shouldn't be too low to make sure that | |
11276 | * a loop with large bound will be rejected quickly. | |
11277 | * The most abusive loop will be: | |
11278 | * r1 += 1 | |
11279 | * if r1 < 1000000 goto pc-2 | |
11280 | * 1M insn_procssed limit / 100 == 10k peak states. | |
11281 | * This threshold shouldn't be too high either, since states | |
11282 | * at the end of the loop are likely to be useful in pruning. | |
11283 | */ | |
11284 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
11285 | env->insn_processed - env->prev_insn_processed < 100) | |
11286 | add_new_state = false; | |
11287 | goto miss; | |
11288 | } | |
638f5b90 | 11289 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 11290 | sl->hit_cnt++; |
f1bca824 | 11291 | /* reached equivalent register/stack state, |
dc503a8a EC |
11292 | * prune the search. |
11293 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
11294 | * If we have any write marks in env->cur_state, they |
11295 | * will prevent corresponding reads in the continuation | |
11296 | * from reaching our parent (an explored_state). Our | |
11297 | * own state will get the read marks recorded, but | |
11298 | * they'll be immediately forgotten as we're pruning | |
11299 | * this state and will pop a new one. | |
f1bca824 | 11300 | */ |
f4d7e40a | 11301 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
11302 | |
11303 | /* if previous state reached the exit with precision and | |
11304 | * current state is equivalent to it (except precsion marks) | |
11305 | * the precision needs to be propagated back in | |
11306 | * the current state. | |
11307 | */ | |
11308 | err = err ? : push_jmp_history(env, cur); | |
11309 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
11310 | if (err) |
11311 | return err; | |
f1bca824 | 11312 | return 1; |
dc503a8a | 11313 | } |
2589726d AS |
11314 | miss: |
11315 | /* when new state is not going to be added do not increase miss count. | |
11316 | * Otherwise several loop iterations will remove the state | |
11317 | * recorded earlier. The goal of these heuristics is to have | |
11318 | * states from some iterations of the loop (some in the beginning | |
11319 | * and some at the end) to help pruning. | |
11320 | */ | |
11321 | if (add_new_state) | |
11322 | sl->miss_cnt++; | |
9f4686c4 AS |
11323 | /* heuristic to determine whether this state is beneficial |
11324 | * to keep checking from state equivalence point of view. | |
11325 | * Higher numbers increase max_states_per_insn and verification time, | |
11326 | * but do not meaningfully decrease insn_processed. | |
11327 | */ | |
11328 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
11329 | /* the state is unlikely to be useful. Remove it to | |
11330 | * speed up verification | |
11331 | */ | |
11332 | *pprev = sl->next; | |
11333 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
11334 | u32 br = sl->state.branches; |
11335 | ||
11336 | WARN_ONCE(br, | |
11337 | "BUG live_done but branches_to_explore %d\n", | |
11338 | br); | |
9f4686c4 AS |
11339 | free_verifier_state(&sl->state, false); |
11340 | kfree(sl); | |
11341 | env->peak_states--; | |
11342 | } else { | |
11343 | /* cannot free this state, since parentage chain may | |
11344 | * walk it later. Add it for free_list instead to | |
11345 | * be freed at the end of verification | |
11346 | */ | |
11347 | sl->next = env->free_list; | |
11348 | env->free_list = sl; | |
11349 | } | |
11350 | sl = *pprev; | |
11351 | continue; | |
11352 | } | |
dc2a4ebc | 11353 | next: |
9f4686c4 AS |
11354 | pprev = &sl->next; |
11355 | sl = *pprev; | |
f1bca824 AS |
11356 | } |
11357 | ||
06ee7115 AS |
11358 | if (env->max_states_per_insn < states_cnt) |
11359 | env->max_states_per_insn = states_cnt; | |
11360 | ||
2c78ee89 | 11361 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 11362 | return push_jmp_history(env, cur); |
ceefbc96 | 11363 | |
2589726d | 11364 | if (!add_new_state) |
b5dc0163 | 11365 | return push_jmp_history(env, cur); |
ceefbc96 | 11366 | |
2589726d AS |
11367 | /* There were no equivalent states, remember the current one. |
11368 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 11369 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 11370 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 11371 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
11372 | * again on the way to bpf_exit. |
11373 | * When looping the sl->state.branches will be > 0 and this state | |
11374 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 11375 | */ |
638f5b90 | 11376 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
11377 | if (!new_sl) |
11378 | return -ENOMEM; | |
06ee7115 AS |
11379 | env->total_states++; |
11380 | env->peak_states++; | |
2589726d AS |
11381 | env->prev_jmps_processed = env->jmps_processed; |
11382 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
11383 | |
11384 | /* add new state to the head of linked list */ | |
679c782d EC |
11385 | new = &new_sl->state; |
11386 | err = copy_verifier_state(new, cur); | |
1969db47 | 11387 | if (err) { |
679c782d | 11388 | free_verifier_state(new, false); |
1969db47 AS |
11389 | kfree(new_sl); |
11390 | return err; | |
11391 | } | |
dc2a4ebc | 11392 | new->insn_idx = insn_idx; |
2589726d AS |
11393 | WARN_ONCE(new->branches != 1, |
11394 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 11395 | |
2589726d | 11396 | cur->parent = new; |
b5dc0163 AS |
11397 | cur->first_insn_idx = insn_idx; |
11398 | clear_jmp_history(cur); | |
5d839021 AS |
11399 | new_sl->next = *explored_state(env, insn_idx); |
11400 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
11401 | /* connect new state to parentage chain. Current frame needs all |
11402 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
11403 | * to the stack implicitly by JITs) so in callers' frames connect just | |
11404 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
11405 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
11406 | * from callee with its full parentage chain, anyway. | |
11407 | */ | |
8e9cd9ce EC |
11408 | /* clear write marks in current state: the writes we did are not writes |
11409 | * our child did, so they don't screen off its reads from us. | |
11410 | * (There are no read marks in current state, because reads always mark | |
11411 | * their parent and current state never has children yet. Only | |
11412 | * explored_states can get read marks.) | |
11413 | */ | |
eea1c227 AS |
11414 | for (j = 0; j <= cur->curframe; j++) { |
11415 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
11416 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
11417 | for (i = 0; i < BPF_REG_FP; i++) | |
11418 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
11419 | } | |
f4d7e40a AS |
11420 | |
11421 | /* all stack frames are accessible from callee, clear them all */ | |
11422 | for (j = 0; j <= cur->curframe; j++) { | |
11423 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 11424 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 11425 | |
679c782d | 11426 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 11427 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
11428 | frame->stack[i].spilled_ptr.parent = |
11429 | &newframe->stack[i].spilled_ptr; | |
11430 | } | |
f4d7e40a | 11431 | } |
f1bca824 AS |
11432 | return 0; |
11433 | } | |
11434 | ||
c64b7983 JS |
11435 | /* Return true if it's OK to have the same insn return a different type. */ |
11436 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
11437 | { | |
c25b2ae1 | 11438 | switch (base_type(type)) { |
c64b7983 JS |
11439 | case PTR_TO_CTX: |
11440 | case PTR_TO_SOCKET: | |
46f8bc92 | 11441 | case PTR_TO_SOCK_COMMON: |
655a51e5 | 11442 | case PTR_TO_TCP_SOCK: |
fada7fdc | 11443 | case PTR_TO_XDP_SOCK: |
2a02759e | 11444 | case PTR_TO_BTF_ID: |
c64b7983 JS |
11445 | return false; |
11446 | default: | |
11447 | return true; | |
11448 | } | |
11449 | } | |
11450 | ||
11451 | /* If an instruction was previously used with particular pointer types, then we | |
11452 | * need to be careful to avoid cases such as the below, where it may be ok | |
11453 | * for one branch accessing the pointer, but not ok for the other branch: | |
11454 | * | |
11455 | * R1 = sock_ptr | |
11456 | * goto X; | |
11457 | * ... | |
11458 | * R1 = some_other_valid_ptr; | |
11459 | * goto X; | |
11460 | * ... | |
11461 | * R2 = *(u32 *)(R1 + 0); | |
11462 | */ | |
11463 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
11464 | { | |
11465 | return src != prev && (!reg_type_mismatch_ok(src) || | |
11466 | !reg_type_mismatch_ok(prev)); | |
11467 | } | |
11468 | ||
58e2af8b | 11469 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 11470 | { |
6f8a57cc | 11471 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 11472 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 11473 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 11474 | struct bpf_reg_state *regs; |
06ee7115 | 11475 | int insn_cnt = env->prog->len; |
17a52670 | 11476 | bool do_print_state = false; |
b5dc0163 | 11477 | int prev_insn_idx = -1; |
17a52670 | 11478 | |
17a52670 AS |
11479 | for (;;) { |
11480 | struct bpf_insn *insn; | |
11481 | u8 class; | |
11482 | int err; | |
11483 | ||
b5dc0163 | 11484 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 11485 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 11486 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 11487 | env->insn_idx, insn_cnt); |
17a52670 AS |
11488 | return -EFAULT; |
11489 | } | |
11490 | ||
c08435ec | 11491 | insn = &insns[env->insn_idx]; |
17a52670 AS |
11492 | class = BPF_CLASS(insn->code); |
11493 | ||
06ee7115 | 11494 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
11495 | verbose(env, |
11496 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 11497 | env->insn_processed); |
17a52670 AS |
11498 | return -E2BIG; |
11499 | } | |
11500 | ||
c08435ec | 11501 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
11502 | if (err < 0) |
11503 | return err; | |
11504 | if (err == 1) { | |
11505 | /* found equivalent state, can prune the search */ | |
06ee7115 | 11506 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 11507 | if (do_print_state) |
979d63d5 DB |
11508 | verbose(env, "\nfrom %d to %d%s: safe\n", |
11509 | env->prev_insn_idx, env->insn_idx, | |
11510 | env->cur_state->speculative ? | |
11511 | " (speculative execution)" : ""); | |
f1bca824 | 11512 | else |
c08435ec | 11513 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
11514 | } |
11515 | goto process_bpf_exit; | |
11516 | } | |
11517 | ||
c3494801 AS |
11518 | if (signal_pending(current)) |
11519 | return -EAGAIN; | |
11520 | ||
3c2ce60b DB |
11521 | if (need_resched()) |
11522 | cond_resched(); | |
11523 | ||
2e576648 CL |
11524 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
11525 | verbose(env, "\nfrom %d to %d%s:", | |
11526 | env->prev_insn_idx, env->insn_idx, | |
11527 | env->cur_state->speculative ? | |
11528 | " (speculative execution)" : ""); | |
11529 | print_verifier_state(env, state->frame[state->curframe], true); | |
17a52670 AS |
11530 | do_print_state = false; |
11531 | } | |
11532 | ||
06ee7115 | 11533 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 | 11534 | const struct bpf_insn_cbs cbs = { |
e6ac2450 | 11535 | .cb_call = disasm_kfunc_name, |
7105e828 | 11536 | .cb_print = verbose, |
abe08840 | 11537 | .private_data = env, |
7105e828 DB |
11538 | }; |
11539 | ||
2e576648 CL |
11540 | if (verifier_state_scratched(env)) |
11541 | print_insn_state(env, state->frame[state->curframe]); | |
11542 | ||
c08435ec | 11543 | verbose_linfo(env, env->insn_idx, "; "); |
2e576648 | 11544 | env->prev_log_len = env->log.len_used; |
c08435ec | 11545 | verbose(env, "%d: ", env->insn_idx); |
abe08840 | 11546 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
2e576648 CL |
11547 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
11548 | env->prev_log_len = env->log.len_used; | |
17a52670 AS |
11549 | } |
11550 | ||
cae1927c | 11551 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
11552 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
11553 | env->prev_insn_idx); | |
cae1927c JK |
11554 | if (err) |
11555 | return err; | |
11556 | } | |
13a27dfc | 11557 | |
638f5b90 | 11558 | regs = cur_regs(env); |
fe9a5ca7 | 11559 | sanitize_mark_insn_seen(env); |
b5dc0163 | 11560 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 11561 | |
17a52670 | 11562 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 11563 | err = check_alu_op(env, insn); |
17a52670 AS |
11564 | if (err) |
11565 | return err; | |
11566 | ||
11567 | } else if (class == BPF_LDX) { | |
3df126f3 | 11568 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
11569 | |
11570 | /* check for reserved fields is already done */ | |
11571 | ||
17a52670 | 11572 | /* check src operand */ |
dc503a8a | 11573 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
11574 | if (err) |
11575 | return err; | |
11576 | ||
dc503a8a | 11577 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
11578 | if (err) |
11579 | return err; | |
11580 | ||
725f9dcd AS |
11581 | src_reg_type = regs[insn->src_reg].type; |
11582 | ||
17a52670 AS |
11583 | /* check that memory (src_reg + off) is readable, |
11584 | * the state of dst_reg will be updated by this func | |
11585 | */ | |
c08435ec DB |
11586 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
11587 | insn->off, BPF_SIZE(insn->code), | |
11588 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
11589 | if (err) |
11590 | return err; | |
11591 | ||
c08435ec | 11592 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
11593 | |
11594 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
11595 | /* saw a valid insn |
11596 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 11597 | * save type to validate intersecting paths |
9bac3d6d | 11598 | */ |
3df126f3 | 11599 | *prev_src_type = src_reg_type; |
9bac3d6d | 11600 | |
c64b7983 | 11601 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
11602 | /* ABuser program is trying to use the same insn |
11603 | * dst_reg = *(u32*) (src_reg + off) | |
11604 | * with different pointer types: | |
11605 | * src_reg == ctx in one branch and | |
11606 | * src_reg == stack|map in some other branch. | |
11607 | * Reject it. | |
11608 | */ | |
61bd5218 | 11609 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
11610 | return -EINVAL; |
11611 | } | |
11612 | ||
17a52670 | 11613 | } else if (class == BPF_STX) { |
3df126f3 | 11614 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 11615 | |
91c960b0 BJ |
11616 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
11617 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
11618 | if (err) |
11619 | return err; | |
c08435ec | 11620 | env->insn_idx++; |
17a52670 AS |
11621 | continue; |
11622 | } | |
11623 | ||
5ca419f2 BJ |
11624 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
11625 | verbose(env, "BPF_STX uses reserved fields\n"); | |
11626 | return -EINVAL; | |
11627 | } | |
11628 | ||
17a52670 | 11629 | /* check src1 operand */ |
dc503a8a | 11630 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
11631 | if (err) |
11632 | return err; | |
11633 | /* check src2 operand */ | |
dc503a8a | 11634 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11635 | if (err) |
11636 | return err; | |
11637 | ||
d691f9e8 AS |
11638 | dst_reg_type = regs[insn->dst_reg].type; |
11639 | ||
17a52670 | 11640 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
11641 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
11642 | insn->off, BPF_SIZE(insn->code), | |
11643 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
11644 | if (err) |
11645 | return err; | |
11646 | ||
c08435ec | 11647 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
11648 | |
11649 | if (*prev_dst_type == NOT_INIT) { | |
11650 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 11651 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 11652 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
11653 | return -EINVAL; |
11654 | } | |
11655 | ||
17a52670 AS |
11656 | } else if (class == BPF_ST) { |
11657 | if (BPF_MODE(insn->code) != BPF_MEM || | |
11658 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 11659 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
11660 | return -EINVAL; |
11661 | } | |
11662 | /* check src operand */ | |
dc503a8a | 11663 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
11664 | if (err) |
11665 | return err; | |
11666 | ||
f37a8cb8 | 11667 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 11668 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f | 11669 | insn->dst_reg, |
c25b2ae1 | 11670 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
f37a8cb8 DB |
11671 | return -EACCES; |
11672 | } | |
11673 | ||
17a52670 | 11674 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
11675 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
11676 | insn->off, BPF_SIZE(insn->code), | |
11677 | BPF_WRITE, -1, false); | |
17a52670 AS |
11678 | if (err) |
11679 | return err; | |
11680 | ||
092ed096 | 11681 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
11682 | u8 opcode = BPF_OP(insn->code); |
11683 | ||
2589726d | 11684 | env->jmps_processed++; |
17a52670 AS |
11685 | if (opcode == BPF_CALL) { |
11686 | if (BPF_SRC(insn->code) != BPF_K || | |
2357672c KKD |
11687 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
11688 | && insn->off != 0) || | |
f4d7e40a | 11689 | (insn->src_reg != BPF_REG_0 && |
e6ac2450 MKL |
11690 | insn->src_reg != BPF_PSEUDO_CALL && |
11691 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || | |
092ed096 JW |
11692 | insn->dst_reg != BPF_REG_0 || |
11693 | class == BPF_JMP32) { | |
61bd5218 | 11694 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
11695 | return -EINVAL; |
11696 | } | |
11697 | ||
d83525ca AS |
11698 | if (env->cur_state->active_spin_lock && |
11699 | (insn->src_reg == BPF_PSEUDO_CALL || | |
11700 | insn->imm != BPF_FUNC_spin_unlock)) { | |
11701 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
11702 | return -EINVAL; | |
11703 | } | |
f4d7e40a | 11704 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 11705 | err = check_func_call(env, insn, &env->insn_idx); |
e6ac2450 | 11706 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
5c073f26 | 11707 | err = check_kfunc_call(env, insn, &env->insn_idx); |
f4d7e40a | 11708 | else |
69c087ba | 11709 | err = check_helper_call(env, insn, &env->insn_idx); |
17a52670 AS |
11710 | if (err) |
11711 | return err; | |
17a52670 AS |
11712 | } else if (opcode == BPF_JA) { |
11713 | if (BPF_SRC(insn->code) != BPF_K || | |
11714 | insn->imm != 0 || | |
11715 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
11716 | insn->dst_reg != BPF_REG_0 || |
11717 | class == BPF_JMP32) { | |
61bd5218 | 11718 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
11719 | return -EINVAL; |
11720 | } | |
11721 | ||
c08435ec | 11722 | env->insn_idx += insn->off + 1; |
17a52670 AS |
11723 | continue; |
11724 | ||
11725 | } else if (opcode == BPF_EXIT) { | |
11726 | if (BPF_SRC(insn->code) != BPF_K || | |
11727 | insn->imm != 0 || | |
11728 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
11729 | insn->dst_reg != BPF_REG_0 || |
11730 | class == BPF_JMP32) { | |
61bd5218 | 11731 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
11732 | return -EINVAL; |
11733 | } | |
11734 | ||
d83525ca AS |
11735 | if (env->cur_state->active_spin_lock) { |
11736 | verbose(env, "bpf_spin_unlock is missing\n"); | |
11737 | return -EINVAL; | |
11738 | } | |
11739 | ||
f4d7e40a AS |
11740 | if (state->curframe) { |
11741 | /* exit from nested function */ | |
c08435ec | 11742 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
11743 | if (err) |
11744 | return err; | |
11745 | do_print_state = true; | |
11746 | continue; | |
11747 | } | |
11748 | ||
fd978bf7 JS |
11749 | err = check_reference_leak(env); |
11750 | if (err) | |
11751 | return err; | |
11752 | ||
390ee7e2 AS |
11753 | err = check_return_code(env); |
11754 | if (err) | |
11755 | return err; | |
f1bca824 | 11756 | process_bpf_exit: |
0f55f9ed | 11757 | mark_verifier_state_scratched(env); |
2589726d | 11758 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 11759 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 11760 | &env->insn_idx, pop_log); |
638f5b90 AS |
11761 | if (err < 0) { |
11762 | if (err != -ENOENT) | |
11763 | return err; | |
17a52670 AS |
11764 | break; |
11765 | } else { | |
11766 | do_print_state = true; | |
11767 | continue; | |
11768 | } | |
11769 | } else { | |
c08435ec | 11770 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
11771 | if (err) |
11772 | return err; | |
11773 | } | |
11774 | } else if (class == BPF_LD) { | |
11775 | u8 mode = BPF_MODE(insn->code); | |
11776 | ||
11777 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
11778 | err = check_ld_abs(env, insn); |
11779 | if (err) | |
11780 | return err; | |
11781 | ||
17a52670 AS |
11782 | } else if (mode == BPF_IMM) { |
11783 | err = check_ld_imm(env, insn); | |
11784 | if (err) | |
11785 | return err; | |
11786 | ||
c08435ec | 11787 | env->insn_idx++; |
fe9a5ca7 | 11788 | sanitize_mark_insn_seen(env); |
17a52670 | 11789 | } else { |
61bd5218 | 11790 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
11791 | return -EINVAL; |
11792 | } | |
11793 | } else { | |
61bd5218 | 11794 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
11795 | return -EINVAL; |
11796 | } | |
11797 | ||
c08435ec | 11798 | env->insn_idx++; |
17a52670 AS |
11799 | } |
11800 | ||
11801 | return 0; | |
11802 | } | |
11803 | ||
541c3bad AN |
11804 | static int find_btf_percpu_datasec(struct btf *btf) |
11805 | { | |
11806 | const struct btf_type *t; | |
11807 | const char *tname; | |
11808 | int i, n; | |
11809 | ||
11810 | /* | |
11811 | * Both vmlinux and module each have their own ".data..percpu" | |
11812 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
11813 | * types to look at only module's own BTF types. | |
11814 | */ | |
11815 | n = btf_nr_types(btf); | |
11816 | if (btf_is_module(btf)) | |
11817 | i = btf_nr_types(btf_vmlinux); | |
11818 | else | |
11819 | i = 1; | |
11820 | ||
11821 | for(; i < n; i++) { | |
11822 | t = btf_type_by_id(btf, i); | |
11823 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
11824 | continue; | |
11825 | ||
11826 | tname = btf_name_by_offset(btf, t->name_off); | |
11827 | if (!strcmp(tname, ".data..percpu")) | |
11828 | return i; | |
11829 | } | |
11830 | ||
11831 | return -ENOENT; | |
11832 | } | |
11833 | ||
4976b718 HL |
11834 | /* replace pseudo btf_id with kernel symbol address */ |
11835 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
11836 | struct bpf_insn *insn, | |
11837 | struct bpf_insn_aux_data *aux) | |
11838 | { | |
eaa6bcb7 HL |
11839 | const struct btf_var_secinfo *vsi; |
11840 | const struct btf_type *datasec; | |
541c3bad | 11841 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
11842 | const struct btf_type *t; |
11843 | const char *sym_name; | |
eaa6bcb7 | 11844 | bool percpu = false; |
f16e6313 | 11845 | u32 type, id = insn->imm; |
541c3bad | 11846 | struct btf *btf; |
f16e6313 | 11847 | s32 datasec_id; |
4976b718 | 11848 | u64 addr; |
541c3bad | 11849 | int i, btf_fd, err; |
4976b718 | 11850 | |
541c3bad AN |
11851 | btf_fd = insn[1].imm; |
11852 | if (btf_fd) { | |
11853 | btf = btf_get_by_fd(btf_fd); | |
11854 | if (IS_ERR(btf)) { | |
11855 | verbose(env, "invalid module BTF object FD specified.\n"); | |
11856 | return -EINVAL; | |
11857 | } | |
11858 | } else { | |
11859 | if (!btf_vmlinux) { | |
11860 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
11861 | return -EINVAL; | |
11862 | } | |
11863 | btf = btf_vmlinux; | |
11864 | btf_get(btf); | |
4976b718 HL |
11865 | } |
11866 | ||
541c3bad | 11867 | t = btf_type_by_id(btf, id); |
4976b718 HL |
11868 | if (!t) { |
11869 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
11870 | err = -ENOENT; |
11871 | goto err_put; | |
4976b718 HL |
11872 | } |
11873 | ||
11874 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
11875 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
11876 | err = -EINVAL; | |
11877 | goto err_put; | |
4976b718 HL |
11878 | } |
11879 | ||
541c3bad | 11880 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
11881 | addr = kallsyms_lookup_name(sym_name); |
11882 | if (!addr) { | |
11883 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
11884 | sym_name); | |
541c3bad AN |
11885 | err = -ENOENT; |
11886 | goto err_put; | |
4976b718 HL |
11887 | } |
11888 | ||
541c3bad | 11889 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 11890 | if (datasec_id > 0) { |
541c3bad | 11891 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
11892 | for_each_vsi(i, datasec, vsi) { |
11893 | if (vsi->type == id) { | |
11894 | percpu = true; | |
11895 | break; | |
11896 | } | |
11897 | } | |
11898 | } | |
11899 | ||
4976b718 HL |
11900 | insn[0].imm = (u32)addr; |
11901 | insn[1].imm = addr >> 32; | |
11902 | ||
11903 | type = t->type; | |
541c3bad | 11904 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 | 11905 | if (percpu) { |
5844101a | 11906 | aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; |
541c3bad | 11907 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
11908 | aux->btf_var.btf_id = type; |
11909 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
11910 | const struct btf_type *ret; |
11911 | const char *tname; | |
11912 | u32 tsize; | |
11913 | ||
11914 | /* resolve the type size of ksym. */ | |
541c3bad | 11915 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 11916 | if (IS_ERR(ret)) { |
541c3bad | 11917 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
11918 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
11919 | tname, PTR_ERR(ret)); | |
541c3bad AN |
11920 | err = -EINVAL; |
11921 | goto err_put; | |
4976b718 | 11922 | } |
34d3a78c | 11923 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
4976b718 HL |
11924 | aux->btf_var.mem_size = tsize; |
11925 | } else { | |
11926 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 11927 | aux->btf_var.btf = btf; |
4976b718 HL |
11928 | aux->btf_var.btf_id = type; |
11929 | } | |
541c3bad AN |
11930 | |
11931 | /* check whether we recorded this BTF (and maybe module) already */ | |
11932 | for (i = 0; i < env->used_btf_cnt; i++) { | |
11933 | if (env->used_btfs[i].btf == btf) { | |
11934 | btf_put(btf); | |
11935 | return 0; | |
11936 | } | |
11937 | } | |
11938 | ||
11939 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
11940 | err = -E2BIG; | |
11941 | goto err_put; | |
11942 | } | |
11943 | ||
11944 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
11945 | btf_mod->btf = btf; | |
11946 | btf_mod->module = NULL; | |
11947 | ||
11948 | /* if we reference variables from kernel module, bump its refcount */ | |
11949 | if (btf_is_module(btf)) { | |
11950 | btf_mod->module = btf_try_get_module(btf); | |
11951 | if (!btf_mod->module) { | |
11952 | err = -ENXIO; | |
11953 | goto err_put; | |
11954 | } | |
11955 | } | |
11956 | ||
11957 | env->used_btf_cnt++; | |
11958 | ||
4976b718 | 11959 | return 0; |
541c3bad AN |
11960 | err_put: |
11961 | btf_put(btf); | |
11962 | return err; | |
4976b718 HL |
11963 | } |
11964 | ||
56f668df MKL |
11965 | static int check_map_prealloc(struct bpf_map *map) |
11966 | { | |
11967 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
11968 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
11969 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
11970 | !(map->map_flags & BPF_F_NO_PREALLOC); |
11971 | } | |
11972 | ||
d83525ca AS |
11973 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
11974 | { | |
11975 | switch (type) { | |
11976 | case BPF_PROG_TYPE_KPROBE: | |
11977 | case BPF_PROG_TYPE_TRACEPOINT: | |
11978 | case BPF_PROG_TYPE_PERF_EVENT: | |
11979 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
11980 | return true; | |
11981 | default: | |
11982 | return false; | |
11983 | } | |
11984 | } | |
11985 | ||
94dacdbd TG |
11986 | static bool is_preallocated_map(struct bpf_map *map) |
11987 | { | |
11988 | if (!check_map_prealloc(map)) | |
11989 | return false; | |
11990 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
11991 | return false; | |
11992 | return true; | |
11993 | } | |
11994 | ||
61bd5218 JK |
11995 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
11996 | struct bpf_map *map, | |
fdc15d38 AS |
11997 | struct bpf_prog *prog) |
11998 | ||
11999 | { | |
7e40781c | 12000 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
94dacdbd TG |
12001 | /* |
12002 | * Validate that trace type programs use preallocated hash maps. | |
12003 | * | |
12004 | * For programs attached to PERF events this is mandatory as the | |
12005 | * perf NMI can hit any arbitrary code sequence. | |
12006 | * | |
12007 | * All other trace types using preallocated hash maps are unsafe as | |
12008 | * well because tracepoint or kprobes can be inside locked regions | |
12009 | * of the memory allocator or at a place where a recursion into the | |
12010 | * memory allocator would see inconsistent state. | |
12011 | * | |
2ed905c5 TG |
12012 | * On RT enabled kernels run-time allocation of all trace type |
12013 | * programs is strictly prohibited due to lock type constraints. On | |
12014 | * !RT kernels it is allowed for backwards compatibility reasons for | |
12015 | * now, but warnings are emitted so developers are made aware of | |
12016 | * the unsafety and can fix their programs before this is enforced. | |
56f668df | 12017 | */ |
7e40781c UP |
12018 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
12019 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { | |
61bd5218 | 12020 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
12021 | return -EINVAL; |
12022 | } | |
2ed905c5 TG |
12023 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
12024 | verbose(env, "trace type programs can only use preallocated hash map\n"); | |
12025 | return -EINVAL; | |
12026 | } | |
94dacdbd TG |
12027 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
12028 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
fdc15d38 | 12029 | } |
a3884572 | 12030 | |
9e7a4d98 KS |
12031 | if (map_value_has_spin_lock(map)) { |
12032 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { | |
12033 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
12034 | return -EINVAL; | |
12035 | } | |
12036 | ||
12037 | if (is_tracing_prog_type(prog_type)) { | |
12038 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
12039 | return -EINVAL; | |
12040 | } | |
12041 | ||
12042 | if (prog->aux->sleepable) { | |
12043 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
12044 | return -EINVAL; | |
12045 | } | |
d83525ca AS |
12046 | } |
12047 | ||
5e0bc308 DB |
12048 | if (map_value_has_timer(map)) { |
12049 | if (is_tracing_prog_type(prog_type)) { | |
12050 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); | |
12051 | return -EINVAL; | |
12052 | } | |
12053 | } | |
12054 | ||
a3884572 | 12055 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 12056 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
12057 | verbose(env, "offload device mismatch between prog and map\n"); |
12058 | return -EINVAL; | |
12059 | } | |
12060 | ||
85d33df3 MKL |
12061 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
12062 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
12063 | return -EINVAL; | |
12064 | } | |
12065 | ||
1e6c62a8 AS |
12066 | if (prog->aux->sleepable) |
12067 | switch (map->map_type) { | |
12068 | case BPF_MAP_TYPE_HASH: | |
12069 | case BPF_MAP_TYPE_LRU_HASH: | |
12070 | case BPF_MAP_TYPE_ARRAY: | |
638e4b82 AS |
12071 | case BPF_MAP_TYPE_PERCPU_HASH: |
12072 | case BPF_MAP_TYPE_PERCPU_ARRAY: | |
12073 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: | |
12074 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: | |
12075 | case BPF_MAP_TYPE_HASH_OF_MAPS: | |
1e6c62a8 AS |
12076 | if (!is_preallocated_map(map)) { |
12077 | verbose(env, | |
638e4b82 | 12078 | "Sleepable programs can only use preallocated maps\n"); |
1e6c62a8 AS |
12079 | return -EINVAL; |
12080 | } | |
12081 | break; | |
ba90c2cc | 12082 | case BPF_MAP_TYPE_RINGBUF: |
0fe4b381 KS |
12083 | case BPF_MAP_TYPE_INODE_STORAGE: |
12084 | case BPF_MAP_TYPE_SK_STORAGE: | |
12085 | case BPF_MAP_TYPE_TASK_STORAGE: | |
ba90c2cc | 12086 | break; |
1e6c62a8 AS |
12087 | default: |
12088 | verbose(env, | |
ba90c2cc | 12089 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
1e6c62a8 AS |
12090 | return -EINVAL; |
12091 | } | |
12092 | ||
fdc15d38 AS |
12093 | return 0; |
12094 | } | |
12095 | ||
b741f163 RG |
12096 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
12097 | { | |
12098 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
12099 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
12100 | } | |
12101 | ||
4976b718 HL |
12102 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
12103 | * | |
12104 | * 1. if it accesses map FD, replace it with actual map pointer. | |
12105 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
12106 | * | |
12107 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 12108 | */ |
4976b718 | 12109 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
12110 | { |
12111 | struct bpf_insn *insn = env->prog->insnsi; | |
12112 | int insn_cnt = env->prog->len; | |
fdc15d38 | 12113 | int i, j, err; |
0246e64d | 12114 | |
f1f7714e | 12115 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
12116 | if (err) |
12117 | return err; | |
12118 | ||
0246e64d | 12119 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 12120 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 12121 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 12122 | verbose(env, "BPF_LDX uses reserved fields\n"); |
d691f9e8 AS |
12123 | return -EINVAL; |
12124 | } | |
12125 | ||
0246e64d | 12126 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 12127 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
12128 | struct bpf_map *map; |
12129 | struct fd f; | |
d8eca5bb | 12130 | u64 addr; |
387544bf | 12131 | u32 fd; |
0246e64d AS |
12132 | |
12133 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
12134 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
12135 | insn[1].off != 0) { | |
61bd5218 | 12136 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
12137 | return -EINVAL; |
12138 | } | |
12139 | ||
d8eca5bb | 12140 | if (insn[0].src_reg == 0) |
0246e64d AS |
12141 | /* valid generic load 64-bit imm */ |
12142 | goto next_insn; | |
12143 | ||
4976b718 HL |
12144 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
12145 | aux = &env->insn_aux_data[i]; | |
12146 | err = check_pseudo_btf_id(env, insn, aux); | |
12147 | if (err) | |
12148 | return err; | |
12149 | goto next_insn; | |
12150 | } | |
12151 | ||
69c087ba YS |
12152 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
12153 | aux = &env->insn_aux_data[i]; | |
12154 | aux->ptr_type = PTR_TO_FUNC; | |
12155 | goto next_insn; | |
12156 | } | |
12157 | ||
d8eca5bb DB |
12158 | /* In final convert_pseudo_ld_imm64() step, this is |
12159 | * converted into regular 64-bit imm load insn. | |
12160 | */ | |
387544bf AS |
12161 | switch (insn[0].src_reg) { |
12162 | case BPF_PSEUDO_MAP_VALUE: | |
12163 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
12164 | break; | |
12165 | case BPF_PSEUDO_MAP_FD: | |
12166 | case BPF_PSEUDO_MAP_IDX: | |
12167 | if (insn[1].imm == 0) | |
12168 | break; | |
12169 | fallthrough; | |
12170 | default: | |
12171 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
12172 | return -EINVAL; |
12173 | } | |
12174 | ||
387544bf AS |
12175 | switch (insn[0].src_reg) { |
12176 | case BPF_PSEUDO_MAP_IDX_VALUE: | |
12177 | case BPF_PSEUDO_MAP_IDX: | |
12178 | if (bpfptr_is_null(env->fd_array)) { | |
12179 | verbose(env, "fd_idx without fd_array is invalid\n"); | |
12180 | return -EPROTO; | |
12181 | } | |
12182 | if (copy_from_bpfptr_offset(&fd, env->fd_array, | |
12183 | insn[0].imm * sizeof(fd), | |
12184 | sizeof(fd))) | |
12185 | return -EFAULT; | |
12186 | break; | |
12187 | default: | |
12188 | fd = insn[0].imm; | |
12189 | break; | |
12190 | } | |
12191 | ||
12192 | f = fdget(fd); | |
c2101297 | 12193 | map = __bpf_map_get(f); |
0246e64d | 12194 | if (IS_ERR(map)) { |
61bd5218 | 12195 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 12196 | insn[0].imm); |
0246e64d AS |
12197 | return PTR_ERR(map); |
12198 | } | |
12199 | ||
61bd5218 | 12200 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
12201 | if (err) { |
12202 | fdput(f); | |
12203 | return err; | |
12204 | } | |
12205 | ||
d8eca5bb | 12206 | aux = &env->insn_aux_data[i]; |
387544bf AS |
12207 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
12208 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { | |
d8eca5bb DB |
12209 | addr = (unsigned long)map; |
12210 | } else { | |
12211 | u32 off = insn[1].imm; | |
12212 | ||
12213 | if (off >= BPF_MAX_VAR_OFF) { | |
12214 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
12215 | fdput(f); | |
12216 | return -EINVAL; | |
12217 | } | |
12218 | ||
12219 | if (!map->ops->map_direct_value_addr) { | |
12220 | verbose(env, "no direct value access support for this map type\n"); | |
12221 | fdput(f); | |
12222 | return -EINVAL; | |
12223 | } | |
12224 | ||
12225 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
12226 | if (err) { | |
12227 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
12228 | map->value_size, off); | |
12229 | fdput(f); | |
12230 | return err; | |
12231 | } | |
12232 | ||
12233 | aux->map_off = off; | |
12234 | addr += off; | |
12235 | } | |
12236 | ||
12237 | insn[0].imm = (u32)addr; | |
12238 | insn[1].imm = addr >> 32; | |
0246e64d AS |
12239 | |
12240 | /* check whether we recorded this map already */ | |
d8eca5bb | 12241 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 12242 | if (env->used_maps[j] == map) { |
d8eca5bb | 12243 | aux->map_index = j; |
0246e64d AS |
12244 | fdput(f); |
12245 | goto next_insn; | |
12246 | } | |
d8eca5bb | 12247 | } |
0246e64d AS |
12248 | |
12249 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
12250 | fdput(f); | |
12251 | return -E2BIG; | |
12252 | } | |
12253 | ||
0246e64d AS |
12254 | /* hold the map. If the program is rejected by verifier, |
12255 | * the map will be released by release_maps() or it | |
12256 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 12257 | * and all maps are released in free_used_maps() |
0246e64d | 12258 | */ |
1e0bd5a0 | 12259 | bpf_map_inc(map); |
d8eca5bb DB |
12260 | |
12261 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
12262 | env->used_maps[env->used_map_cnt++] = map; |
12263 | ||
b741f163 | 12264 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 12265 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 12266 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
12267 | fdput(f); |
12268 | return -EBUSY; | |
12269 | } | |
12270 | ||
0246e64d AS |
12271 | fdput(f); |
12272 | next_insn: | |
12273 | insn++; | |
12274 | i++; | |
5e581dad DB |
12275 | continue; |
12276 | } | |
12277 | ||
12278 | /* Basic sanity check before we invest more work here. */ | |
12279 | if (!bpf_opcode_in_insntable(insn->code)) { | |
12280 | verbose(env, "unknown opcode %02x\n", insn->code); | |
12281 | return -EINVAL; | |
0246e64d AS |
12282 | } |
12283 | } | |
12284 | ||
12285 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
12286 | * 'struct bpf_map *' into a register instead of user map_fd. | |
12287 | * These pointers will be used later by verifier to validate map access. | |
12288 | */ | |
12289 | return 0; | |
12290 | } | |
12291 | ||
12292 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 12293 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 12294 | { |
a2ea0746 DB |
12295 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
12296 | env->used_map_cnt); | |
0246e64d AS |
12297 | } |
12298 | ||
541c3bad AN |
12299 | /* drop refcnt of maps used by the rejected program */ |
12300 | static void release_btfs(struct bpf_verifier_env *env) | |
12301 | { | |
12302 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
12303 | env->used_btf_cnt); | |
12304 | } | |
12305 | ||
0246e64d | 12306 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 12307 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
12308 | { |
12309 | struct bpf_insn *insn = env->prog->insnsi; | |
12310 | int insn_cnt = env->prog->len; | |
12311 | int i; | |
12312 | ||
69c087ba YS |
12313 | for (i = 0; i < insn_cnt; i++, insn++) { |
12314 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) | |
12315 | continue; | |
12316 | if (insn->src_reg == BPF_PSEUDO_FUNC) | |
12317 | continue; | |
12318 | insn->src_reg = 0; | |
12319 | } | |
0246e64d AS |
12320 | } |
12321 | ||
8041902d AS |
12322 | /* single env->prog->insni[off] instruction was replaced with the range |
12323 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
12324 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
12325 | */ | |
75f0fc7b HF |
12326 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
12327 | struct bpf_insn_aux_data *new_data, | |
12328 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d | 12329 | { |
75f0fc7b | 12330 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
b325fbca | 12331 | struct bpf_insn *insn = new_prog->insnsi; |
d203b0fd | 12332 | u32 old_seen = old_data[off].seen; |
b325fbca | 12333 | u32 prog_len; |
c131187d | 12334 | int i; |
8041902d | 12335 | |
b325fbca JW |
12336 | /* aux info at OFF always needs adjustment, no matter fast path |
12337 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
12338 | * original insn at old prog. | |
12339 | */ | |
12340 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
12341 | ||
8041902d | 12342 | if (cnt == 1) |
75f0fc7b | 12343 | return; |
b325fbca | 12344 | prog_len = new_prog->len; |
75f0fc7b | 12345 | |
8041902d AS |
12346 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
12347 | memcpy(new_data + off + cnt - 1, old_data + off, | |
12348 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 12349 | for (i = off; i < off + cnt - 1; i++) { |
d203b0fd DB |
12350 | /* Expand insni[off]'s seen count to the patched range. */ |
12351 | new_data[i].seen = old_seen; | |
b325fbca JW |
12352 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
12353 | } | |
8041902d AS |
12354 | env->insn_aux_data = new_data; |
12355 | vfree(old_data); | |
8041902d AS |
12356 | } |
12357 | ||
cc8b0b92 AS |
12358 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
12359 | { | |
12360 | int i; | |
12361 | ||
12362 | if (len == 1) | |
12363 | return; | |
4cb3d99c JW |
12364 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
12365 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 12366 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 12367 | continue; |
9c8105bd | 12368 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
12369 | } |
12370 | } | |
12371 | ||
7506d211 | 12372 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
a748c697 MF |
12373 | { |
12374 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
12375 | int i, sz = prog->aux->size_poke_tab; | |
12376 | struct bpf_jit_poke_descriptor *desc; | |
12377 | ||
12378 | for (i = 0; i < sz; i++) { | |
12379 | desc = &tab[i]; | |
7506d211 JF |
12380 | if (desc->insn_idx <= off) |
12381 | continue; | |
a748c697 MF |
12382 | desc->insn_idx += len - 1; |
12383 | } | |
12384 | } | |
12385 | ||
8041902d AS |
12386 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
12387 | const struct bpf_insn *patch, u32 len) | |
12388 | { | |
12389 | struct bpf_prog *new_prog; | |
75f0fc7b HF |
12390 | struct bpf_insn_aux_data *new_data = NULL; |
12391 | ||
12392 | if (len > 1) { | |
12393 | new_data = vzalloc(array_size(env->prog->len + len - 1, | |
12394 | sizeof(struct bpf_insn_aux_data))); | |
12395 | if (!new_data) | |
12396 | return NULL; | |
12397 | } | |
8041902d AS |
12398 | |
12399 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
12400 | if (IS_ERR(new_prog)) { |
12401 | if (PTR_ERR(new_prog) == -ERANGE) | |
12402 | verbose(env, | |
12403 | "insn %d cannot be patched due to 16-bit range\n", | |
12404 | env->insn_aux_data[off].orig_idx); | |
75f0fc7b | 12405 | vfree(new_data); |
8041902d | 12406 | return NULL; |
4f73379e | 12407 | } |
75f0fc7b | 12408 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
cc8b0b92 | 12409 | adjust_subprog_starts(env, off, len); |
7506d211 | 12410 | adjust_poke_descs(new_prog, off, len); |
8041902d AS |
12411 | return new_prog; |
12412 | } | |
12413 | ||
52875a04 JK |
12414 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
12415 | u32 off, u32 cnt) | |
12416 | { | |
12417 | int i, j; | |
12418 | ||
12419 | /* find first prog starting at or after off (first to remove) */ | |
12420 | for (i = 0; i < env->subprog_cnt; i++) | |
12421 | if (env->subprog_info[i].start >= off) | |
12422 | break; | |
12423 | /* find first prog starting at or after off + cnt (first to stay) */ | |
12424 | for (j = i; j < env->subprog_cnt; j++) | |
12425 | if (env->subprog_info[j].start >= off + cnt) | |
12426 | break; | |
12427 | /* if j doesn't start exactly at off + cnt, we are just removing | |
12428 | * the front of previous prog | |
12429 | */ | |
12430 | if (env->subprog_info[j].start != off + cnt) | |
12431 | j--; | |
12432 | ||
12433 | if (j > i) { | |
12434 | struct bpf_prog_aux *aux = env->prog->aux; | |
12435 | int move; | |
12436 | ||
12437 | /* move fake 'exit' subprog as well */ | |
12438 | move = env->subprog_cnt + 1 - j; | |
12439 | ||
12440 | memmove(env->subprog_info + i, | |
12441 | env->subprog_info + j, | |
12442 | sizeof(*env->subprog_info) * move); | |
12443 | env->subprog_cnt -= j - i; | |
12444 | ||
12445 | /* remove func_info */ | |
12446 | if (aux->func_info) { | |
12447 | move = aux->func_info_cnt - j; | |
12448 | ||
12449 | memmove(aux->func_info + i, | |
12450 | aux->func_info + j, | |
12451 | sizeof(*aux->func_info) * move); | |
12452 | aux->func_info_cnt -= j - i; | |
12453 | /* func_info->insn_off is set after all code rewrites, | |
12454 | * in adjust_btf_func() - no need to adjust | |
12455 | */ | |
12456 | } | |
12457 | } else { | |
12458 | /* convert i from "first prog to remove" to "first to adjust" */ | |
12459 | if (env->subprog_info[i].start == off) | |
12460 | i++; | |
12461 | } | |
12462 | ||
12463 | /* update fake 'exit' subprog as well */ | |
12464 | for (; i <= env->subprog_cnt; i++) | |
12465 | env->subprog_info[i].start -= cnt; | |
12466 | ||
12467 | return 0; | |
12468 | } | |
12469 | ||
12470 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
12471 | u32 cnt) | |
12472 | { | |
12473 | struct bpf_prog *prog = env->prog; | |
12474 | u32 i, l_off, l_cnt, nr_linfo; | |
12475 | struct bpf_line_info *linfo; | |
12476 | ||
12477 | nr_linfo = prog->aux->nr_linfo; | |
12478 | if (!nr_linfo) | |
12479 | return 0; | |
12480 | ||
12481 | linfo = prog->aux->linfo; | |
12482 | ||
12483 | /* find first line info to remove, count lines to be removed */ | |
12484 | for (i = 0; i < nr_linfo; i++) | |
12485 | if (linfo[i].insn_off >= off) | |
12486 | break; | |
12487 | ||
12488 | l_off = i; | |
12489 | l_cnt = 0; | |
12490 | for (; i < nr_linfo; i++) | |
12491 | if (linfo[i].insn_off < off + cnt) | |
12492 | l_cnt++; | |
12493 | else | |
12494 | break; | |
12495 | ||
12496 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
12497 | * last removed linfo. prog is already modified, so prog->len == off | |
12498 | * means no live instructions after (tail of the program was removed). | |
12499 | */ | |
12500 | if (prog->len != off && l_cnt && | |
12501 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
12502 | l_cnt--; | |
12503 | linfo[--i].insn_off = off + cnt; | |
12504 | } | |
12505 | ||
12506 | /* remove the line info which refer to the removed instructions */ | |
12507 | if (l_cnt) { | |
12508 | memmove(linfo + l_off, linfo + i, | |
12509 | sizeof(*linfo) * (nr_linfo - i)); | |
12510 | ||
12511 | prog->aux->nr_linfo -= l_cnt; | |
12512 | nr_linfo = prog->aux->nr_linfo; | |
12513 | } | |
12514 | ||
12515 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
12516 | for (i = l_off; i < nr_linfo; i++) | |
12517 | linfo[i].insn_off -= cnt; | |
12518 | ||
12519 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
12520 | for (i = 0; i <= env->subprog_cnt; i++) | |
12521 | if (env->subprog_info[i].linfo_idx > l_off) { | |
12522 | /* program may have started in the removed region but | |
12523 | * may not be fully removed | |
12524 | */ | |
12525 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
12526 | env->subprog_info[i].linfo_idx -= l_cnt; | |
12527 | else | |
12528 | env->subprog_info[i].linfo_idx = l_off; | |
12529 | } | |
12530 | ||
12531 | return 0; | |
12532 | } | |
12533 | ||
12534 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
12535 | { | |
12536 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12537 | unsigned int orig_prog_len = env->prog->len; | |
12538 | int err; | |
12539 | ||
08ca90af JK |
12540 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
12541 | bpf_prog_offload_remove_insns(env, off, cnt); | |
12542 | ||
52875a04 JK |
12543 | err = bpf_remove_insns(env->prog, off, cnt); |
12544 | if (err) | |
12545 | return err; | |
12546 | ||
12547 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
12548 | if (err) | |
12549 | return err; | |
12550 | ||
12551 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
12552 | if (err) | |
12553 | return err; | |
12554 | ||
12555 | memmove(aux_data + off, aux_data + off + cnt, | |
12556 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
12557 | ||
12558 | return 0; | |
12559 | } | |
12560 | ||
2a5418a1 DB |
12561 | /* The verifier does more data flow analysis than llvm and will not |
12562 | * explore branches that are dead at run time. Malicious programs can | |
12563 | * have dead code too. Therefore replace all dead at-run-time code | |
12564 | * with 'ja -1'. | |
12565 | * | |
12566 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
12567 | * program and through another bug we would manage to jump there, then | |
12568 | * we'd execute beyond program memory otherwise. Returning exception | |
12569 | * code also wouldn't work since we can have subprogs where the dead | |
12570 | * code could be located. | |
c131187d AS |
12571 | */ |
12572 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
12573 | { | |
12574 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 12575 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
12576 | struct bpf_insn *insn = env->prog->insnsi; |
12577 | const int insn_cnt = env->prog->len; | |
12578 | int i; | |
12579 | ||
12580 | for (i = 0; i < insn_cnt; i++) { | |
12581 | if (aux_data[i].seen) | |
12582 | continue; | |
2a5418a1 | 12583 | memcpy(insn + i, &trap, sizeof(trap)); |
45c709f8 | 12584 | aux_data[i].zext_dst = false; |
c131187d AS |
12585 | } |
12586 | } | |
12587 | ||
e2ae4ca2 JK |
12588 | static bool insn_is_cond_jump(u8 code) |
12589 | { | |
12590 | u8 op; | |
12591 | ||
092ed096 JW |
12592 | if (BPF_CLASS(code) == BPF_JMP32) |
12593 | return true; | |
12594 | ||
e2ae4ca2 JK |
12595 | if (BPF_CLASS(code) != BPF_JMP) |
12596 | return false; | |
12597 | ||
12598 | op = BPF_OP(code); | |
12599 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
12600 | } | |
12601 | ||
12602 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
12603 | { | |
12604 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12605 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
12606 | struct bpf_insn *insn = env->prog->insnsi; | |
12607 | const int insn_cnt = env->prog->len; | |
12608 | int i; | |
12609 | ||
12610 | for (i = 0; i < insn_cnt; i++, insn++) { | |
12611 | if (!insn_is_cond_jump(insn->code)) | |
12612 | continue; | |
12613 | ||
12614 | if (!aux_data[i + 1].seen) | |
12615 | ja.off = insn->off; | |
12616 | else if (!aux_data[i + 1 + insn->off].seen) | |
12617 | ja.off = 0; | |
12618 | else | |
12619 | continue; | |
12620 | ||
08ca90af JK |
12621 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
12622 | bpf_prog_offload_replace_insn(env, i, &ja); | |
12623 | ||
e2ae4ca2 JK |
12624 | memcpy(insn, &ja, sizeof(ja)); |
12625 | } | |
12626 | } | |
12627 | ||
52875a04 JK |
12628 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
12629 | { | |
12630 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
12631 | int insn_cnt = env->prog->len; | |
12632 | int i, err; | |
12633 | ||
12634 | for (i = 0; i < insn_cnt; i++) { | |
12635 | int j; | |
12636 | ||
12637 | j = 0; | |
12638 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
12639 | j++; | |
12640 | if (!j) | |
12641 | continue; | |
12642 | ||
12643 | err = verifier_remove_insns(env, i, j); | |
12644 | if (err) | |
12645 | return err; | |
12646 | insn_cnt = env->prog->len; | |
12647 | } | |
12648 | ||
12649 | return 0; | |
12650 | } | |
12651 | ||
a1b14abc JK |
12652 | static int opt_remove_nops(struct bpf_verifier_env *env) |
12653 | { | |
12654 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
12655 | struct bpf_insn *insn = env->prog->insnsi; | |
12656 | int insn_cnt = env->prog->len; | |
12657 | int i, err; | |
12658 | ||
12659 | for (i = 0; i < insn_cnt; i++) { | |
12660 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
12661 | continue; | |
12662 | ||
12663 | err = verifier_remove_insns(env, i, 1); | |
12664 | if (err) | |
12665 | return err; | |
12666 | insn_cnt--; | |
12667 | i--; | |
12668 | } | |
12669 | ||
12670 | return 0; | |
12671 | } | |
12672 | ||
d6c2308c JW |
12673 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
12674 | const union bpf_attr *attr) | |
a4b1d3c1 | 12675 | { |
d6c2308c | 12676 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 12677 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 12678 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 12679 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 12680 | struct bpf_prog *new_prog; |
d6c2308c | 12681 | bool rnd_hi32; |
a4b1d3c1 | 12682 | |
d6c2308c | 12683 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 12684 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
12685 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
12686 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
12687 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
12688 | for (i = 0; i < len; i++) { |
12689 | int adj_idx = i + delta; | |
12690 | struct bpf_insn insn; | |
83a28819 | 12691 | int load_reg; |
a4b1d3c1 | 12692 | |
d6c2308c | 12693 | insn = insns[adj_idx]; |
83a28819 | 12694 | load_reg = insn_def_regno(&insn); |
d6c2308c JW |
12695 | if (!aux[adj_idx].zext_dst) { |
12696 | u8 code, class; | |
12697 | u32 imm_rnd; | |
12698 | ||
12699 | if (!rnd_hi32) | |
12700 | continue; | |
12701 | ||
12702 | code = insn.code; | |
12703 | class = BPF_CLASS(code); | |
83a28819 | 12704 | if (load_reg == -1) |
d6c2308c JW |
12705 | continue; |
12706 | ||
12707 | /* NOTE: arg "reg" (the fourth one) is only used for | |
83a28819 IL |
12708 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
12709 | * here. | |
d6c2308c | 12710 | */ |
83a28819 | 12711 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
d6c2308c JW |
12712 | if (class == BPF_LD && |
12713 | BPF_MODE(code) == BPF_IMM) | |
12714 | i++; | |
12715 | continue; | |
12716 | } | |
12717 | ||
12718 | /* ctx load could be transformed into wider load. */ | |
12719 | if (class == BPF_LDX && | |
12720 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
12721 | continue; | |
12722 | ||
12723 | imm_rnd = get_random_int(); | |
12724 | rnd_hi32_patch[0] = insn; | |
12725 | rnd_hi32_patch[1].imm = imm_rnd; | |
83a28819 | 12726 | rnd_hi32_patch[3].dst_reg = load_reg; |
d6c2308c JW |
12727 | patch = rnd_hi32_patch; |
12728 | patch_len = 4; | |
12729 | goto apply_patch_buffer; | |
12730 | } | |
12731 | ||
39491867 BJ |
12732 | /* Add in an zero-extend instruction if a) the JIT has requested |
12733 | * it or b) it's a CMPXCHG. | |
12734 | * | |
12735 | * The latter is because: BPF_CMPXCHG always loads a value into | |
12736 | * R0, therefore always zero-extends. However some archs' | |
12737 | * equivalent instruction only does this load when the | |
12738 | * comparison is successful. This detail of CMPXCHG is | |
12739 | * orthogonal to the general zero-extension behaviour of the | |
12740 | * CPU, so it's treated independently of bpf_jit_needs_zext. | |
12741 | */ | |
12742 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) | |
a4b1d3c1 JW |
12743 | continue; |
12744 | ||
83a28819 IL |
12745 | if (WARN_ON(load_reg == -1)) { |
12746 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); | |
12747 | return -EFAULT; | |
b2e37a71 IL |
12748 | } |
12749 | ||
a4b1d3c1 | 12750 | zext_patch[0] = insn; |
b2e37a71 IL |
12751 | zext_patch[1].dst_reg = load_reg; |
12752 | zext_patch[1].src_reg = load_reg; | |
d6c2308c JW |
12753 | patch = zext_patch; |
12754 | patch_len = 2; | |
12755 | apply_patch_buffer: | |
12756 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
12757 | if (!new_prog) |
12758 | return -ENOMEM; | |
12759 | env->prog = new_prog; | |
12760 | insns = new_prog->insnsi; | |
12761 | aux = env->insn_aux_data; | |
d6c2308c | 12762 | delta += patch_len - 1; |
a4b1d3c1 JW |
12763 | } |
12764 | ||
12765 | return 0; | |
12766 | } | |
12767 | ||
c64b7983 JS |
12768 | /* convert load instructions that access fields of a context type into a |
12769 | * sequence of instructions that access fields of the underlying structure: | |
12770 | * struct __sk_buff -> struct sk_buff | |
12771 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 12772 | */ |
58e2af8b | 12773 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 12774 | { |
00176a34 | 12775 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 12776 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 12777 | const int insn_cnt = env->prog->len; |
36bbef52 | 12778 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 12779 | u32 target_size, size_default, off; |
9bac3d6d | 12780 | struct bpf_prog *new_prog; |
d691f9e8 | 12781 | enum bpf_access_type type; |
f96da094 | 12782 | bool is_narrower_load; |
9bac3d6d | 12783 | |
b09928b9 DB |
12784 | if (ops->gen_prologue || env->seen_direct_write) { |
12785 | if (!ops->gen_prologue) { | |
12786 | verbose(env, "bpf verifier is misconfigured\n"); | |
12787 | return -EINVAL; | |
12788 | } | |
36bbef52 DB |
12789 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
12790 | env->prog); | |
12791 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 12792 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
12793 | return -EINVAL; |
12794 | } else if (cnt) { | |
8041902d | 12795 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
12796 | if (!new_prog) |
12797 | return -ENOMEM; | |
8041902d | 12798 | |
36bbef52 | 12799 | env->prog = new_prog; |
3df126f3 | 12800 | delta += cnt - 1; |
36bbef52 DB |
12801 | } |
12802 | } | |
12803 | ||
c64b7983 | 12804 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
12805 | return 0; |
12806 | ||
3df126f3 | 12807 | insn = env->prog->insnsi + delta; |
36bbef52 | 12808 | |
9bac3d6d | 12809 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 | 12810 | bpf_convert_ctx_access_t convert_ctx_access; |
2039f26f | 12811 | bool ctx_access; |
c64b7983 | 12812 | |
62c7989b DB |
12813 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
12814 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
12815 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
2039f26f | 12816 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
d691f9e8 | 12817 | type = BPF_READ; |
2039f26f DB |
12818 | ctx_access = true; |
12819 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || | |
12820 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
12821 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
12822 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || | |
12823 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || | |
12824 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || | |
12825 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || | |
12826 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { | |
d691f9e8 | 12827 | type = BPF_WRITE; |
2039f26f DB |
12828 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
12829 | } else { | |
9bac3d6d | 12830 | continue; |
2039f26f | 12831 | } |
9bac3d6d | 12832 | |
af86ca4e | 12833 | if (type == BPF_WRITE && |
2039f26f | 12834 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
af86ca4e | 12835 | struct bpf_insn patch[] = { |
af86ca4e | 12836 | *insn, |
2039f26f | 12837 | BPF_ST_NOSPEC(), |
af86ca4e AS |
12838 | }; |
12839 | ||
12840 | cnt = ARRAY_SIZE(patch); | |
12841 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
12842 | if (!new_prog) | |
12843 | return -ENOMEM; | |
12844 | ||
12845 | delta += cnt - 1; | |
12846 | env->prog = new_prog; | |
12847 | insn = new_prog->insnsi + i + delta; | |
12848 | continue; | |
12849 | } | |
12850 | ||
2039f26f DB |
12851 | if (!ctx_access) |
12852 | continue; | |
12853 | ||
c64b7983 JS |
12854 | switch (env->insn_aux_data[i + delta].ptr_type) { |
12855 | case PTR_TO_CTX: | |
12856 | if (!ops->convert_ctx_access) | |
12857 | continue; | |
12858 | convert_ctx_access = ops->convert_ctx_access; | |
12859 | break; | |
12860 | case PTR_TO_SOCKET: | |
46f8bc92 | 12861 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
12862 | convert_ctx_access = bpf_sock_convert_ctx_access; |
12863 | break; | |
655a51e5 MKL |
12864 | case PTR_TO_TCP_SOCK: |
12865 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
12866 | break; | |
fada7fdc JL |
12867 | case PTR_TO_XDP_SOCK: |
12868 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
12869 | break; | |
2a02759e | 12870 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
12871 | if (type == BPF_READ) { |
12872 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
12873 | BPF_SIZE((insn)->code); | |
12874 | env->prog->aux->num_exentries++; | |
7e40781c | 12875 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
2a02759e AS |
12876 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
12877 | return -EINVAL; | |
12878 | } | |
2a02759e | 12879 | continue; |
c64b7983 | 12880 | default: |
9bac3d6d | 12881 | continue; |
c64b7983 | 12882 | } |
9bac3d6d | 12883 | |
31fd8581 | 12884 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 12885 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
12886 | |
12887 | /* If the read access is a narrower load of the field, | |
12888 | * convert to a 4/8-byte load, to minimum program type specific | |
12889 | * convert_ctx_access changes. If conversion is successful, | |
12890 | * we will apply proper mask to the result. | |
12891 | */ | |
f96da094 | 12892 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
12893 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
12894 | off = insn->off; | |
31fd8581 | 12895 | if (is_narrower_load) { |
f96da094 DB |
12896 | u8 size_code; |
12897 | ||
12898 | if (type == BPF_WRITE) { | |
61bd5218 | 12899 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
12900 | return -EINVAL; |
12901 | } | |
31fd8581 | 12902 | |
f96da094 | 12903 | size_code = BPF_H; |
31fd8581 YS |
12904 | if (ctx_field_size == 4) |
12905 | size_code = BPF_W; | |
12906 | else if (ctx_field_size == 8) | |
12907 | size_code = BPF_DW; | |
f96da094 | 12908 | |
bc23105c | 12909 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
12910 | insn->code = BPF_LDX | BPF_MEM | size_code; |
12911 | } | |
f96da094 DB |
12912 | |
12913 | target_size = 0; | |
c64b7983 JS |
12914 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
12915 | &target_size); | |
f96da094 DB |
12916 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
12917 | (ctx_field_size && !target_size)) { | |
61bd5218 | 12918 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
12919 | return -EINVAL; |
12920 | } | |
f96da094 DB |
12921 | |
12922 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
12923 | u8 shift = bpf_ctx_narrow_access_offset( |
12924 | off, size, size_default) * 8; | |
d7af7e49 AI |
12925 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
12926 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); | |
12927 | return -EINVAL; | |
12928 | } | |
46f53a65 AI |
12929 | if (ctx_field_size <= 4) { |
12930 | if (shift) | |
12931 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
12932 | insn->dst_reg, | |
12933 | shift); | |
31fd8581 | 12934 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 12935 | (1 << size * 8) - 1); |
46f53a65 AI |
12936 | } else { |
12937 | if (shift) | |
12938 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
12939 | insn->dst_reg, | |
12940 | shift); | |
31fd8581 | 12941 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 12942 | (1ULL << size * 8) - 1); |
46f53a65 | 12943 | } |
31fd8581 | 12944 | } |
9bac3d6d | 12945 | |
8041902d | 12946 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
12947 | if (!new_prog) |
12948 | return -ENOMEM; | |
12949 | ||
3df126f3 | 12950 | delta += cnt - 1; |
9bac3d6d AS |
12951 | |
12952 | /* keep walking new program and skip insns we just inserted */ | |
12953 | env->prog = new_prog; | |
3df126f3 | 12954 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
12955 | } |
12956 | ||
12957 | return 0; | |
12958 | } | |
12959 | ||
1c2a088a AS |
12960 | static int jit_subprogs(struct bpf_verifier_env *env) |
12961 | { | |
12962 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
12963 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 12964 | struct bpf_map *map_ptr; |
7105e828 | 12965 | struct bpf_insn *insn; |
1c2a088a | 12966 | void *old_bpf_func; |
c4c0bdc0 | 12967 | int err, num_exentries; |
1c2a088a | 12968 | |
f910cefa | 12969 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
12970 | return 0; |
12971 | ||
7105e828 | 12972 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
3990ed4c | 12973 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
69c087ba | 12974 | continue; |
69c087ba | 12975 | |
c7a89784 DB |
12976 | /* Upon error here we cannot fall back to interpreter but |
12977 | * need a hard reject of the program. Thus -EFAULT is | |
12978 | * propagated in any case. | |
12979 | */ | |
1c2a088a AS |
12980 | subprog = find_subprog(env, i + insn->imm + 1); |
12981 | if (subprog < 0) { | |
12982 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
12983 | i + insn->imm + 1); | |
12984 | return -EFAULT; | |
12985 | } | |
12986 | /* temporarily remember subprog id inside insn instead of | |
12987 | * aux_data, since next loop will split up all insns into funcs | |
12988 | */ | |
f910cefa | 12989 | insn->off = subprog; |
1c2a088a AS |
12990 | /* remember original imm in case JIT fails and fallback |
12991 | * to interpreter will be needed | |
12992 | */ | |
12993 | env->insn_aux_data[i].call_imm = insn->imm; | |
12994 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
12995 | insn->imm = 1; | |
3990ed4c MKL |
12996 | if (bpf_pseudo_func(insn)) |
12997 | /* jit (e.g. x86_64) may emit fewer instructions | |
12998 | * if it learns a u32 imm is the same as a u64 imm. | |
12999 | * Force a non zero here. | |
13000 | */ | |
13001 | insn[1].imm = 1; | |
1c2a088a AS |
13002 | } |
13003 | ||
c454a46b MKL |
13004 | err = bpf_prog_alloc_jited_linfo(prog); |
13005 | if (err) | |
13006 | goto out_undo_insn; | |
13007 | ||
13008 | err = -ENOMEM; | |
6396bb22 | 13009 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 13010 | if (!func) |
c7a89784 | 13011 | goto out_undo_insn; |
1c2a088a | 13012 | |
f910cefa | 13013 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 13014 | subprog_start = subprog_end; |
4cb3d99c | 13015 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
13016 | |
13017 | len = subprog_end - subprog_start; | |
fb7dd8bc | 13018 | /* bpf_prog_run() doesn't call subprogs directly, |
492ecee8 AS |
13019 | * hence main prog stats include the runtime of subprogs. |
13020 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
700d4796 | 13021 | * func[i]->stats will never be accessed and stays NULL |
492ecee8 AS |
13022 | */ |
13023 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
13024 | if (!func[i]) |
13025 | goto out_free; | |
13026 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
13027 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 13028 | func[i]->type = prog->type; |
1c2a088a | 13029 | func[i]->len = len; |
4f74d809 DB |
13030 | if (bpf_prog_calc_tag(func[i])) |
13031 | goto out_free; | |
1c2a088a | 13032 | func[i]->is_func = 1; |
ba64e7d8 | 13033 | func[i]->aux->func_idx = i; |
f263a814 | 13034 | /* Below members will be freed only at prog->aux */ |
ba64e7d8 YS |
13035 | func[i]->aux->btf = prog->aux->btf; |
13036 | func[i]->aux->func_info = prog->aux->func_info; | |
f263a814 JF |
13037 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
13038 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; | |
ba64e7d8 | 13039 | |
a748c697 | 13040 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
f263a814 | 13041 | struct bpf_jit_poke_descriptor *poke; |
a748c697 | 13042 | |
f263a814 JF |
13043 | poke = &prog->aux->poke_tab[j]; |
13044 | if (poke->insn_idx < subprog_end && | |
13045 | poke->insn_idx >= subprog_start) | |
13046 | poke->aux = func[i]->aux; | |
a748c697 MF |
13047 | } |
13048 | ||
1c2a088a AS |
13049 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
13050 | * Long term would need debug info to populate names | |
13051 | */ | |
13052 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 13053 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 13054 | func[i]->jit_requested = 1; |
d2a3b7c5 | 13055 | func[i]->blinding_requested = prog->blinding_requested; |
e6ac2450 | 13056 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
2357672c | 13057 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
c454a46b MKL |
13058 | func[i]->aux->linfo = prog->aux->linfo; |
13059 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
13060 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
13061 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
13062 | num_exentries = 0; |
13063 | insn = func[i]->insnsi; | |
13064 | for (j = 0; j < func[i]->len; j++, insn++) { | |
13065 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
13066 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
13067 | num_exentries++; | |
13068 | } | |
13069 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 13070 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
13071 | func[i] = bpf_int_jit_compile(func[i]); |
13072 | if (!func[i]->jited) { | |
13073 | err = -ENOTSUPP; | |
13074 | goto out_free; | |
13075 | } | |
13076 | cond_resched(); | |
13077 | } | |
a748c697 | 13078 | |
1c2a088a AS |
13079 | /* at this point all bpf functions were successfully JITed |
13080 | * now populate all bpf_calls with correct addresses and | |
13081 | * run last pass of JIT | |
13082 | */ | |
f910cefa | 13083 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
13084 | insn = func[i]->insnsi; |
13085 | for (j = 0; j < func[i]->len; j++, insn++) { | |
69c087ba | 13086 | if (bpf_pseudo_func(insn)) { |
3990ed4c | 13087 | subprog = insn->off; |
69c087ba YS |
13088 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
13089 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; | |
13090 | continue; | |
13091 | } | |
23a2d70c | 13092 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
13093 | continue; |
13094 | subprog = insn->off; | |
3d717fad | 13095 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
1c2a088a | 13096 | } |
2162fed4 SD |
13097 | |
13098 | /* we use the aux data to keep a list of the start addresses | |
13099 | * of the JITed images for each function in the program | |
13100 | * | |
13101 | * for some architectures, such as powerpc64, the imm field | |
13102 | * might not be large enough to hold the offset of the start | |
13103 | * address of the callee's JITed image from __bpf_call_base | |
13104 | * | |
13105 | * in such cases, we can lookup the start address of a callee | |
13106 | * by using its subprog id, available from the off field of | |
13107 | * the call instruction, as an index for this list | |
13108 | */ | |
13109 | func[i]->aux->func = func; | |
13110 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 13111 | } |
f910cefa | 13112 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
13113 | old_bpf_func = func[i]->bpf_func; |
13114 | tmp = bpf_int_jit_compile(func[i]); | |
13115 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
13116 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 13117 | err = -ENOTSUPP; |
1c2a088a AS |
13118 | goto out_free; |
13119 | } | |
13120 | cond_resched(); | |
13121 | } | |
13122 | ||
13123 | /* finally lock prog and jit images for all functions and | |
13124 | * populate kallsysm | |
13125 | */ | |
f910cefa | 13126 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
13127 | bpf_prog_lock_ro(func[i]); |
13128 | bpf_prog_kallsyms_add(func[i]); | |
13129 | } | |
7105e828 DB |
13130 | |
13131 | /* Last step: make now unused interpreter insns from main | |
13132 | * prog consistent for later dump requests, so they can | |
13133 | * later look the same as if they were interpreted only. | |
13134 | */ | |
13135 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
69c087ba YS |
13136 | if (bpf_pseudo_func(insn)) { |
13137 | insn[0].imm = env->insn_aux_data[i].call_imm; | |
3990ed4c MKL |
13138 | insn[1].imm = insn->off; |
13139 | insn->off = 0; | |
69c087ba YS |
13140 | continue; |
13141 | } | |
23a2d70c | 13142 | if (!bpf_pseudo_call(insn)) |
7105e828 DB |
13143 | continue; |
13144 | insn->off = env->insn_aux_data[i].call_imm; | |
13145 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 13146 | insn->imm = subprog; |
7105e828 DB |
13147 | } |
13148 | ||
1c2a088a AS |
13149 | prog->jited = 1; |
13150 | prog->bpf_func = func[0]->bpf_func; | |
d00c6473 | 13151 | prog->jited_len = func[0]->jited_len; |
1c2a088a | 13152 | prog->aux->func = func; |
f910cefa | 13153 | prog->aux->func_cnt = env->subprog_cnt; |
e16301fb | 13154 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
13155 | return 0; |
13156 | out_free: | |
f263a814 JF |
13157 | /* We failed JIT'ing, so at this point we need to unregister poke |
13158 | * descriptors from subprogs, so that kernel is not attempting to | |
13159 | * patch it anymore as we're freeing the subprog JIT memory. | |
13160 | */ | |
13161 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
13162 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
13163 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
13164 | } | |
13165 | /* At this point we're guaranteed that poke descriptors are not | |
13166 | * live anymore. We can just unlink its descriptor table as it's | |
13167 | * released with the main prog. | |
13168 | */ | |
a748c697 MF |
13169 | for (i = 0; i < env->subprog_cnt; i++) { |
13170 | if (!func[i]) | |
13171 | continue; | |
f263a814 | 13172 | func[i]->aux->poke_tab = NULL; |
a748c697 MF |
13173 | bpf_jit_free(func[i]); |
13174 | } | |
1c2a088a | 13175 | kfree(func); |
c7a89784 | 13176 | out_undo_insn: |
1c2a088a AS |
13177 | /* cleanup main prog to be interpreted */ |
13178 | prog->jit_requested = 0; | |
d2a3b7c5 | 13179 | prog->blinding_requested = 0; |
1c2a088a | 13180 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
23a2d70c | 13181 | if (!bpf_pseudo_call(insn)) |
1c2a088a AS |
13182 | continue; |
13183 | insn->off = 0; | |
13184 | insn->imm = env->insn_aux_data[i].call_imm; | |
13185 | } | |
e16301fb | 13186 | bpf_prog_jit_attempt_done(prog); |
1c2a088a AS |
13187 | return err; |
13188 | } | |
13189 | ||
1ea47e01 AS |
13190 | static int fixup_call_args(struct bpf_verifier_env *env) |
13191 | { | |
19d28fbd | 13192 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
13193 | struct bpf_prog *prog = env->prog; |
13194 | struct bpf_insn *insn = prog->insnsi; | |
e6ac2450 | 13195 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
1ea47e01 | 13196 | int i, depth; |
19d28fbd | 13197 | #endif |
e4052d06 | 13198 | int err = 0; |
1ea47e01 | 13199 | |
e4052d06 QM |
13200 | if (env->prog->jit_requested && |
13201 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
13202 | err = jit_subprogs(env); |
13203 | if (err == 0) | |
1c2a088a | 13204 | return 0; |
c7a89784 DB |
13205 | if (err == -EFAULT) |
13206 | return err; | |
19d28fbd DM |
13207 | } |
13208 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e6ac2450 MKL |
13209 | if (has_kfunc_call) { |
13210 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); | |
13211 | return -EINVAL; | |
13212 | } | |
e411901c MF |
13213 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
13214 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
13215 | * have to be rejected, since interpreter doesn't support them yet. | |
13216 | */ | |
13217 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
13218 | return -EINVAL; | |
13219 | } | |
1ea47e01 | 13220 | for (i = 0; i < prog->len; i++, insn++) { |
69c087ba YS |
13221 | if (bpf_pseudo_func(insn)) { |
13222 | /* When JIT fails the progs with callback calls | |
13223 | * have to be rejected, since interpreter doesn't support them yet. | |
13224 | */ | |
13225 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); | |
13226 | return -EINVAL; | |
13227 | } | |
13228 | ||
23a2d70c | 13229 | if (!bpf_pseudo_call(insn)) |
1ea47e01 AS |
13230 | continue; |
13231 | depth = get_callee_stack_depth(env, insn, i); | |
13232 | if (depth < 0) | |
13233 | return depth; | |
13234 | bpf_patch_call_args(insn, depth); | |
13235 | } | |
19d28fbd DM |
13236 | err = 0; |
13237 | #endif | |
13238 | return err; | |
1ea47e01 AS |
13239 | } |
13240 | ||
e6ac2450 MKL |
13241 | static int fixup_kfunc_call(struct bpf_verifier_env *env, |
13242 | struct bpf_insn *insn) | |
13243 | { | |
13244 | const struct bpf_kfunc_desc *desc; | |
13245 | ||
a5d82727 KKD |
13246 | if (!insn->imm) { |
13247 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); | |
13248 | return -EINVAL; | |
13249 | } | |
13250 | ||
e6ac2450 MKL |
13251 | /* insn->imm has the btf func_id. Replace it with |
13252 | * an address (relative to __bpf_base_call). | |
13253 | */ | |
2357672c | 13254 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
e6ac2450 MKL |
13255 | if (!desc) { |
13256 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", | |
13257 | insn->imm); | |
13258 | return -EFAULT; | |
13259 | } | |
13260 | ||
13261 | insn->imm = desc->imm; | |
13262 | ||
13263 | return 0; | |
13264 | } | |
13265 | ||
e6ac5933 BJ |
13266 | /* Do various post-verification rewrites in a single program pass. |
13267 | * These rewrites simplify JIT and interpreter implementations. | |
e245c5c6 | 13268 | */ |
e6ac5933 | 13269 | static int do_misc_fixups(struct bpf_verifier_env *env) |
e245c5c6 | 13270 | { |
79741b3b | 13271 | struct bpf_prog *prog = env->prog; |
f92c1e18 | 13272 | enum bpf_attach_type eatype = prog->expected_attach_type; |
9b99edca | 13273 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
79741b3b | 13274 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 13275 | const struct bpf_func_proto *fn; |
79741b3b | 13276 | const int insn_cnt = prog->len; |
09772d92 | 13277 | const struct bpf_map_ops *ops; |
c93552c4 | 13278 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
13279 | struct bpf_insn insn_buf[16]; |
13280 | struct bpf_prog *new_prog; | |
13281 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 13282 | int i, ret, cnt, delta = 0; |
e245c5c6 | 13283 | |
79741b3b | 13284 | for (i = 0; i < insn_cnt; i++, insn++) { |
e6ac5933 | 13285 | /* Make divide-by-zero exceptions impossible. */ |
f6b1b3bf DB |
13286 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
13287 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
13288 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 13289 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf | 13290 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
e88b2c6e DB |
13291 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
13292 | struct bpf_insn *patchlet; | |
13293 | struct bpf_insn chk_and_div[] = { | |
9b00f1b7 | 13294 | /* [R,W]x div 0 -> 0 */ |
e88b2c6e DB |
13295 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
13296 | BPF_JNE | BPF_K, insn->src_reg, | |
13297 | 0, 2, 0), | |
f6b1b3bf DB |
13298 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
13299 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
13300 | *insn, | |
13301 | }; | |
e88b2c6e | 13302 | struct bpf_insn chk_and_mod[] = { |
9b00f1b7 | 13303 | /* [R,W]x mod 0 -> [R,W]x */ |
e88b2c6e DB |
13304 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
13305 | BPF_JEQ | BPF_K, insn->src_reg, | |
9b00f1b7 | 13306 | 0, 1 + (is64 ? 0 : 1), 0), |
f6b1b3bf | 13307 | *insn, |
9b00f1b7 DB |
13308 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
13309 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), | |
f6b1b3bf | 13310 | }; |
f6b1b3bf | 13311 | |
e88b2c6e DB |
13312 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
13313 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : | |
9b00f1b7 | 13314 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
f6b1b3bf DB |
13315 | |
13316 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
13317 | if (!new_prog) |
13318 | return -ENOMEM; | |
13319 | ||
13320 | delta += cnt - 1; | |
13321 | env->prog = prog = new_prog; | |
13322 | insn = new_prog->insnsi + i + delta; | |
13323 | continue; | |
13324 | } | |
13325 | ||
e6ac5933 | 13326 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
e0cea7ce DB |
13327 | if (BPF_CLASS(insn->code) == BPF_LD && |
13328 | (BPF_MODE(insn->code) == BPF_ABS || | |
13329 | BPF_MODE(insn->code) == BPF_IND)) { | |
13330 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
13331 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
13332 | verbose(env, "bpf verifier is misconfigured\n"); | |
13333 | return -EINVAL; | |
13334 | } | |
13335 | ||
13336 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13337 | if (!new_prog) | |
13338 | return -ENOMEM; | |
13339 | ||
13340 | delta += cnt - 1; | |
13341 | env->prog = prog = new_prog; | |
13342 | insn = new_prog->insnsi + i + delta; | |
13343 | continue; | |
13344 | } | |
13345 | ||
e6ac5933 | 13346 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
979d63d5 DB |
13347 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
13348 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
13349 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
13350 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
979d63d5 | 13351 | struct bpf_insn *patch = &insn_buf[0]; |
801c6058 | 13352 | bool issrc, isneg, isimm; |
979d63d5 DB |
13353 | u32 off_reg; |
13354 | ||
13355 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
13356 | if (!aux->alu_state || |
13357 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
13358 | continue; |
13359 | ||
13360 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
13361 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
13362 | BPF_ALU_SANITIZE_SRC; | |
801c6058 | 13363 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
979d63d5 DB |
13364 | |
13365 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
801c6058 DB |
13366 | if (isimm) { |
13367 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
13368 | } else { | |
13369 | if (isneg) | |
13370 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
13371 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); | |
13372 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
13373 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
13374 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
13375 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
13376 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); | |
13377 | } | |
b9b34ddb DB |
13378 | if (!issrc) |
13379 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); | |
13380 | insn->src_reg = BPF_REG_AX; | |
979d63d5 DB |
13381 | if (isneg) |
13382 | insn->code = insn->code == code_add ? | |
13383 | code_sub : code_add; | |
13384 | *patch++ = *insn; | |
801c6058 | 13385 | if (issrc && isneg && !isimm) |
979d63d5 DB |
13386 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
13387 | cnt = patch - insn_buf; | |
13388 | ||
13389 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13390 | if (!new_prog) | |
13391 | return -ENOMEM; | |
13392 | ||
13393 | delta += cnt - 1; | |
13394 | env->prog = prog = new_prog; | |
13395 | insn = new_prog->insnsi + i + delta; | |
13396 | continue; | |
13397 | } | |
13398 | ||
79741b3b AS |
13399 | if (insn->code != (BPF_JMP | BPF_CALL)) |
13400 | continue; | |
cc8b0b92 AS |
13401 | if (insn->src_reg == BPF_PSEUDO_CALL) |
13402 | continue; | |
e6ac2450 MKL |
13403 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
13404 | ret = fixup_kfunc_call(env, insn); | |
13405 | if (ret) | |
13406 | return ret; | |
13407 | continue; | |
13408 | } | |
e245c5c6 | 13409 | |
79741b3b AS |
13410 | if (insn->imm == BPF_FUNC_get_route_realm) |
13411 | prog->dst_needed = 1; | |
13412 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
13413 | bpf_user_rnd_init_once(); | |
9802d865 JB |
13414 | if (insn->imm == BPF_FUNC_override_return) |
13415 | prog->kprobe_override = 1; | |
79741b3b | 13416 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
13417 | /* If we tail call into other programs, we |
13418 | * cannot make any assumptions since they can | |
13419 | * be replaced dynamically during runtime in | |
13420 | * the program array. | |
13421 | */ | |
13422 | prog->cb_access = 1; | |
e411901c MF |
13423 | if (!allow_tail_call_in_subprogs(env)) |
13424 | prog->aux->stack_depth = MAX_BPF_STACK; | |
13425 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 13426 | |
79741b3b | 13427 | /* mark bpf_tail_call as different opcode to avoid |
8fb33b60 | 13428 | * conditional branch in the interpreter for every normal |
79741b3b AS |
13429 | * call and to prevent accidental JITing by JIT compiler |
13430 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 13431 | */ |
79741b3b | 13432 | insn->imm = 0; |
71189fa9 | 13433 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 13434 | |
c93552c4 | 13435 | aux = &env->insn_aux_data[i + delta]; |
d2a3b7c5 | 13436 | if (env->bpf_capable && !prog->blinding_requested && |
cc52d914 | 13437 | prog->jit_requested && |
d2e4c1e6 DB |
13438 | !bpf_map_key_poisoned(aux) && |
13439 | !bpf_map_ptr_poisoned(aux) && | |
13440 | !bpf_map_ptr_unpriv(aux)) { | |
13441 | struct bpf_jit_poke_descriptor desc = { | |
13442 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
13443 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
13444 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 13445 | .insn_idx = i + delta, |
d2e4c1e6 DB |
13446 | }; |
13447 | ||
13448 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
13449 | if (ret < 0) { | |
13450 | verbose(env, "adding tail call poke descriptor failed\n"); | |
13451 | return ret; | |
13452 | } | |
13453 | ||
13454 | insn->imm = ret + 1; | |
13455 | continue; | |
13456 | } | |
13457 | ||
c93552c4 DB |
13458 | if (!bpf_map_ptr_unpriv(aux)) |
13459 | continue; | |
13460 | ||
b2157399 AS |
13461 | /* instead of changing every JIT dealing with tail_call |
13462 | * emit two extra insns: | |
13463 | * if (index >= max_entries) goto out; | |
13464 | * index &= array->index_mask; | |
13465 | * to avoid out-of-bounds cpu speculation | |
13466 | */ | |
c93552c4 | 13467 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 13468 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
13469 | return -EINVAL; |
13470 | } | |
c93552c4 | 13471 | |
d2e4c1e6 | 13472 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
13473 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
13474 | map_ptr->max_entries, 2); | |
13475 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
13476 | container_of(map_ptr, | |
13477 | struct bpf_array, | |
13478 | map)->index_mask); | |
13479 | insn_buf[2] = *insn; | |
13480 | cnt = 3; | |
13481 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13482 | if (!new_prog) | |
13483 | return -ENOMEM; | |
13484 | ||
13485 | delta += cnt - 1; | |
13486 | env->prog = prog = new_prog; | |
13487 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
13488 | continue; |
13489 | } | |
e245c5c6 | 13490 | |
b00628b1 AS |
13491 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
13492 | /* The verifier will process callback_fn as many times as necessary | |
13493 | * with different maps and the register states prepared by | |
13494 | * set_timer_callback_state will be accurate. | |
13495 | * | |
13496 | * The following use case is valid: | |
13497 | * map1 is shared by prog1, prog2, prog3. | |
13498 | * prog1 calls bpf_timer_init for some map1 elements | |
13499 | * prog2 calls bpf_timer_set_callback for some map1 elements. | |
13500 | * Those that were not bpf_timer_init-ed will return -EINVAL. | |
13501 | * prog3 calls bpf_timer_start for some map1 elements. | |
13502 | * Those that were not both bpf_timer_init-ed and | |
13503 | * bpf_timer_set_callback-ed will return -EINVAL. | |
13504 | */ | |
13505 | struct bpf_insn ld_addrs[2] = { | |
13506 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), | |
13507 | }; | |
13508 | ||
13509 | insn_buf[0] = ld_addrs[0]; | |
13510 | insn_buf[1] = ld_addrs[1]; | |
13511 | insn_buf[2] = *insn; | |
13512 | cnt = 3; | |
13513 | ||
13514 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13515 | if (!new_prog) | |
13516 | return -ENOMEM; | |
13517 | ||
13518 | delta += cnt - 1; | |
13519 | env->prog = prog = new_prog; | |
13520 | insn = new_prog->insnsi + i + delta; | |
13521 | goto patch_call_imm; | |
13522 | } | |
13523 | ||
b00fa38a JK |
13524 | if (insn->imm == BPF_FUNC_task_storage_get || |
13525 | insn->imm == BPF_FUNC_sk_storage_get || | |
13526 | insn->imm == BPF_FUNC_inode_storage_get) { | |
13527 | if (env->prog->aux->sleepable) | |
d56c9fe6 | 13528 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); |
b00fa38a | 13529 | else |
d56c9fe6 | 13530 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); |
b00fa38a JK |
13531 | insn_buf[1] = *insn; |
13532 | cnt = 2; | |
13533 | ||
13534 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13535 | if (!new_prog) | |
13536 | return -ENOMEM; | |
13537 | ||
13538 | delta += cnt - 1; | |
13539 | env->prog = prog = new_prog; | |
13540 | insn = new_prog->insnsi + i + delta; | |
13541 | goto patch_call_imm; | |
13542 | } | |
13543 | ||
89c63074 | 13544 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
13545 | * and other inlining handlers are currently limited to 64 bit |
13546 | * only. | |
89c63074 | 13547 | */ |
60b58afc | 13548 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
13549 | (insn->imm == BPF_FUNC_map_lookup_elem || |
13550 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
13551 | insn->imm == BPF_FUNC_map_delete_elem || |
13552 | insn->imm == BPF_FUNC_map_push_elem || | |
13553 | insn->imm == BPF_FUNC_map_pop_elem || | |
e6a4750f | 13554 | insn->imm == BPF_FUNC_map_peek_elem || |
0640c77c AI |
13555 | insn->imm == BPF_FUNC_redirect_map || |
13556 | insn->imm == BPF_FUNC_for_each_map_elem)) { | |
c93552c4 DB |
13557 | aux = &env->insn_aux_data[i + delta]; |
13558 | if (bpf_map_ptr_poisoned(aux)) | |
13559 | goto patch_call_imm; | |
13560 | ||
d2e4c1e6 | 13561 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
13562 | ops = map_ptr->ops; |
13563 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
13564 | ops->map_gen_lookup) { | |
13565 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
13566 | if (cnt == -EOPNOTSUPP) |
13567 | goto patch_map_ops_generic; | |
13568 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
13569 | verbose(env, "bpf verifier is misconfigured\n"); |
13570 | return -EINVAL; | |
13571 | } | |
81ed18ab | 13572 | |
09772d92 DB |
13573 | new_prog = bpf_patch_insn_data(env, i + delta, |
13574 | insn_buf, cnt); | |
13575 | if (!new_prog) | |
13576 | return -ENOMEM; | |
81ed18ab | 13577 | |
09772d92 DB |
13578 | delta += cnt - 1; |
13579 | env->prog = prog = new_prog; | |
13580 | insn = new_prog->insnsi + i + delta; | |
13581 | continue; | |
13582 | } | |
81ed18ab | 13583 | |
09772d92 DB |
13584 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
13585 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
13586 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
13587 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
13588 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
13589 | (int (*)(struct bpf_map *map, void *key, void *value, | |
13590 | u64 flags))NULL)); | |
84430d42 DB |
13591 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
13592 | (int (*)(struct bpf_map *map, void *value, | |
13593 | u64 flags))NULL)); | |
13594 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
13595 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
13596 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
13597 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
e6a4750f BT |
13598 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
13599 | (int (*)(struct bpf_map *map, u32 ifindex, u64 flags))NULL)); | |
0640c77c AI |
13600 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
13601 | (int (*)(struct bpf_map *map, | |
13602 | bpf_callback_t callback_fn, | |
13603 | void *callback_ctx, | |
13604 | u64 flags))NULL)); | |
e6a4750f | 13605 | |
4a8f87e6 | 13606 | patch_map_ops_generic: |
09772d92 DB |
13607 | switch (insn->imm) { |
13608 | case BPF_FUNC_map_lookup_elem: | |
3d717fad | 13609 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
09772d92 DB |
13610 | continue; |
13611 | case BPF_FUNC_map_update_elem: | |
3d717fad | 13612 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
09772d92 DB |
13613 | continue; |
13614 | case BPF_FUNC_map_delete_elem: | |
3d717fad | 13615 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
09772d92 | 13616 | continue; |
84430d42 | 13617 | case BPF_FUNC_map_push_elem: |
3d717fad | 13618 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
84430d42 DB |
13619 | continue; |
13620 | case BPF_FUNC_map_pop_elem: | |
3d717fad | 13621 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
84430d42 DB |
13622 | continue; |
13623 | case BPF_FUNC_map_peek_elem: | |
3d717fad | 13624 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
84430d42 | 13625 | continue; |
e6a4750f | 13626 | case BPF_FUNC_redirect_map: |
3d717fad | 13627 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
e6a4750f | 13628 | continue; |
0640c77c AI |
13629 | case BPF_FUNC_for_each_map_elem: |
13630 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); | |
e6a4750f | 13631 | continue; |
09772d92 | 13632 | } |
81ed18ab | 13633 | |
09772d92 | 13634 | goto patch_call_imm; |
81ed18ab AS |
13635 | } |
13636 | ||
e6ac5933 | 13637 | /* Implement bpf_jiffies64 inline. */ |
5576b991 MKL |
13638 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
13639 | insn->imm == BPF_FUNC_jiffies64) { | |
13640 | struct bpf_insn ld_jiffies_addr[2] = { | |
13641 | BPF_LD_IMM64(BPF_REG_0, | |
13642 | (unsigned long)&jiffies), | |
13643 | }; | |
13644 | ||
13645 | insn_buf[0] = ld_jiffies_addr[0]; | |
13646 | insn_buf[1] = ld_jiffies_addr[1]; | |
13647 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
13648 | BPF_REG_0, 0); | |
13649 | cnt = 3; | |
13650 | ||
13651 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
13652 | cnt); | |
13653 | if (!new_prog) | |
13654 | return -ENOMEM; | |
13655 | ||
13656 | delta += cnt - 1; | |
13657 | env->prog = prog = new_prog; | |
13658 | insn = new_prog->insnsi + i + delta; | |
13659 | continue; | |
13660 | } | |
13661 | ||
f92c1e18 JO |
13662 | /* Implement bpf_get_func_arg inline. */ |
13663 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13664 | insn->imm == BPF_FUNC_get_func_arg) { | |
13665 | /* Load nr_args from ctx - 8 */ | |
13666 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
13667 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); | |
13668 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); | |
13669 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); | |
13670 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); | |
13671 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
13672 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
13673 | insn_buf[7] = BPF_JMP_A(1); | |
13674 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); | |
13675 | cnt = 9; | |
13676 | ||
13677 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13678 | if (!new_prog) | |
13679 | return -ENOMEM; | |
13680 | ||
13681 | delta += cnt - 1; | |
13682 | env->prog = prog = new_prog; | |
13683 | insn = new_prog->insnsi + i + delta; | |
13684 | continue; | |
13685 | } | |
13686 | ||
13687 | /* Implement bpf_get_func_ret inline. */ | |
13688 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13689 | insn->imm == BPF_FUNC_get_func_ret) { | |
13690 | if (eatype == BPF_TRACE_FEXIT || | |
13691 | eatype == BPF_MODIFY_RETURN) { | |
13692 | /* Load nr_args from ctx - 8 */ | |
13693 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
13694 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); | |
13695 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); | |
13696 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); | |
13697 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); | |
13698 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); | |
13699 | cnt = 6; | |
13700 | } else { | |
13701 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); | |
13702 | cnt = 1; | |
13703 | } | |
13704 | ||
13705 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
13706 | if (!new_prog) | |
13707 | return -ENOMEM; | |
13708 | ||
13709 | delta += cnt - 1; | |
13710 | env->prog = prog = new_prog; | |
13711 | insn = new_prog->insnsi + i + delta; | |
13712 | continue; | |
13713 | } | |
13714 | ||
13715 | /* Implement get_func_arg_cnt inline. */ | |
13716 | if (prog_type == BPF_PROG_TYPE_TRACING && | |
13717 | insn->imm == BPF_FUNC_get_func_arg_cnt) { | |
13718 | /* Load nr_args from ctx - 8 */ | |
13719 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); | |
13720 | ||
13721 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
13722 | if (!new_prog) | |
13723 | return -ENOMEM; | |
13724 | ||
13725 | env->prog = prog = new_prog; | |
13726 | insn = new_prog->insnsi + i + delta; | |
13727 | continue; | |
13728 | } | |
13729 | ||
f705ec76 | 13730 | /* Implement bpf_get_func_ip inline. */ |
9b99edca JO |
13731 | if (prog_type == BPF_PROG_TYPE_TRACING && |
13732 | insn->imm == BPF_FUNC_get_func_ip) { | |
f92c1e18 JO |
13733 | /* Load IP address from ctx - 16 */ |
13734 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); | |
9b99edca JO |
13735 | |
13736 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); | |
13737 | if (!new_prog) | |
13738 | return -ENOMEM; | |
13739 | ||
13740 | env->prog = prog = new_prog; | |
13741 | insn = new_prog->insnsi + i + delta; | |
13742 | continue; | |
13743 | } | |
13744 | ||
81ed18ab | 13745 | patch_call_imm: |
5e43f899 | 13746 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
13747 | /* all functions that have prototype and verifier allowed |
13748 | * programs to call them, must be real in-kernel functions | |
13749 | */ | |
13750 | if (!fn->func) { | |
61bd5218 JK |
13751 | verbose(env, |
13752 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
13753 | func_id_name(insn->imm), insn->imm); |
13754 | return -EFAULT; | |
e245c5c6 | 13755 | } |
79741b3b | 13756 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 13757 | } |
e245c5c6 | 13758 | |
d2e4c1e6 DB |
13759 | /* Since poke tab is now finalized, publish aux to tracker. */ |
13760 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
13761 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
13762 | if (!map_ptr->ops->map_poke_track || | |
13763 | !map_ptr->ops->map_poke_untrack || | |
13764 | !map_ptr->ops->map_poke_run) { | |
13765 | verbose(env, "bpf verifier is misconfigured\n"); | |
13766 | return -EINVAL; | |
13767 | } | |
13768 | ||
13769 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
13770 | if (ret < 0) { | |
13771 | verbose(env, "tracking tail call prog failed\n"); | |
13772 | return ret; | |
13773 | } | |
13774 | } | |
13775 | ||
e6ac2450 MKL |
13776 | sort_kfunc_descs_by_imm(env->prog); |
13777 | ||
79741b3b AS |
13778 | return 0; |
13779 | } | |
e245c5c6 | 13780 | |
58e2af8b | 13781 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 13782 | { |
58e2af8b | 13783 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
13784 | int i; |
13785 | ||
9f4686c4 AS |
13786 | sl = env->free_list; |
13787 | while (sl) { | |
13788 | sln = sl->next; | |
13789 | free_verifier_state(&sl->state, false); | |
13790 | kfree(sl); | |
13791 | sl = sln; | |
13792 | } | |
51c39bb1 | 13793 | env->free_list = NULL; |
9f4686c4 | 13794 | |
f1bca824 AS |
13795 | if (!env->explored_states) |
13796 | return; | |
13797 | ||
dc2a4ebc | 13798 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
13799 | sl = env->explored_states[i]; |
13800 | ||
a8f500af AS |
13801 | while (sl) { |
13802 | sln = sl->next; | |
13803 | free_verifier_state(&sl->state, false); | |
13804 | kfree(sl); | |
13805 | sl = sln; | |
13806 | } | |
51c39bb1 | 13807 | env->explored_states[i] = NULL; |
f1bca824 | 13808 | } |
51c39bb1 | 13809 | } |
f1bca824 | 13810 | |
51c39bb1 AS |
13811 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
13812 | { | |
6f8a57cc | 13813 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
13814 | struct bpf_verifier_state *state; |
13815 | struct bpf_reg_state *regs; | |
13816 | int ret, i; | |
13817 | ||
13818 | env->prev_linfo = NULL; | |
13819 | env->pass_cnt++; | |
13820 | ||
13821 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
13822 | if (!state) | |
13823 | return -ENOMEM; | |
13824 | state->curframe = 0; | |
13825 | state->speculative = false; | |
13826 | state->branches = 1; | |
13827 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
13828 | if (!state->frame[0]) { | |
13829 | kfree(state); | |
13830 | return -ENOMEM; | |
13831 | } | |
13832 | env->cur_state = state; | |
13833 | init_func_state(env, state->frame[0], | |
13834 | BPF_MAIN_FUNC /* callsite */, | |
13835 | 0 /* frameno */, | |
13836 | subprog); | |
13837 | ||
13838 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 13839 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
13840 | ret = btf_prepare_func_args(env, subprog, regs); |
13841 | if (ret) | |
13842 | goto out; | |
13843 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
13844 | if (regs[i].type == PTR_TO_CTX) | |
13845 | mark_reg_known_zero(env, regs, i); | |
13846 | else if (regs[i].type == SCALAR_VALUE) | |
13847 | mark_reg_unknown(env, regs, i); | |
cf9f2f8d | 13848 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
e5069b9c DB |
13849 | const u32 mem_size = regs[i].mem_size; |
13850 | ||
13851 | mark_reg_known_zero(env, regs, i); | |
13852 | regs[i].mem_size = mem_size; | |
13853 | regs[i].id = ++env->id_gen; | |
13854 | } | |
51c39bb1 AS |
13855 | } |
13856 | } else { | |
13857 | /* 1st arg to a function */ | |
13858 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
13859 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
34747c41 | 13860 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
51c39bb1 AS |
13861 | if (ret == -EFAULT) |
13862 | /* unlikely verifier bug. abort. | |
13863 | * ret == 0 and ret < 0 are sadly acceptable for | |
13864 | * main() function due to backward compatibility. | |
13865 | * Like socket filter program may be written as: | |
13866 | * int bpf_prog(struct pt_regs *ctx) | |
13867 | * and never dereference that ctx in the program. | |
13868 | * 'struct pt_regs' is a type mismatch for socket | |
13869 | * filter that should be using 'struct __sk_buff'. | |
13870 | */ | |
13871 | goto out; | |
13872 | } | |
13873 | ||
13874 | ret = do_check(env); | |
13875 | out: | |
f59bbfc2 AS |
13876 | /* check for NULL is necessary, since cur_state can be freed inside |
13877 | * do_check() under memory pressure. | |
13878 | */ | |
13879 | if (env->cur_state) { | |
13880 | free_verifier_state(env->cur_state, true); | |
13881 | env->cur_state = NULL; | |
13882 | } | |
6f8a57cc AN |
13883 | while (!pop_stack(env, NULL, NULL, false)); |
13884 | if (!ret && pop_log) | |
13885 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 | 13886 | free_states(env); |
51c39bb1 AS |
13887 | return ret; |
13888 | } | |
13889 | ||
13890 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
13891 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
13892 | * Consider: | |
13893 | * int bar(int); | |
13894 | * int foo(int f) | |
13895 | * { | |
13896 | * return bar(f); | |
13897 | * } | |
13898 | * int bar(int b) | |
13899 | * { | |
13900 | * ... | |
13901 | * } | |
13902 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
13903 | * will be assumed that bar() already verified successfully and call to bar() | |
13904 | * from foo() will be checked for type match only. Later bar() will be verified | |
13905 | * independently to check that it's safe for R1=any_scalar_value. | |
13906 | */ | |
13907 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
13908 | { | |
13909 | struct bpf_prog_aux *aux = env->prog->aux; | |
13910 | int i, ret; | |
13911 | ||
13912 | if (!aux->func_info) | |
13913 | return 0; | |
13914 | ||
13915 | for (i = 1; i < env->subprog_cnt; i++) { | |
13916 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
13917 | continue; | |
13918 | env->insn_idx = env->subprog_info[i].start; | |
13919 | WARN_ON_ONCE(env->insn_idx == 0); | |
13920 | ret = do_check_common(env, i); | |
13921 | if (ret) { | |
13922 | return ret; | |
13923 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
13924 | verbose(env, | |
13925 | "Func#%d is safe for any args that match its prototype\n", | |
13926 | i); | |
13927 | } | |
13928 | } | |
13929 | return 0; | |
13930 | } | |
13931 | ||
13932 | static int do_check_main(struct bpf_verifier_env *env) | |
13933 | { | |
13934 | int ret; | |
13935 | ||
13936 | env->insn_idx = 0; | |
13937 | ret = do_check_common(env, 0); | |
13938 | if (!ret) | |
13939 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
13940 | return ret; | |
13941 | } | |
13942 | ||
13943 | ||
06ee7115 AS |
13944 | static void print_verification_stats(struct bpf_verifier_env *env) |
13945 | { | |
13946 | int i; | |
13947 | ||
13948 | if (env->log.level & BPF_LOG_STATS) { | |
13949 | verbose(env, "verification time %lld usec\n", | |
13950 | div_u64(env->verification_time, 1000)); | |
13951 | verbose(env, "stack depth "); | |
13952 | for (i = 0; i < env->subprog_cnt; i++) { | |
13953 | u32 depth = env->subprog_info[i].stack_depth; | |
13954 | ||
13955 | verbose(env, "%d", depth); | |
13956 | if (i + 1 < env->subprog_cnt) | |
13957 | verbose(env, "+"); | |
13958 | } | |
13959 | verbose(env, "\n"); | |
13960 | } | |
13961 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
13962 | "total_states %d peak_states %d mark_read %d\n", | |
13963 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
13964 | env->max_states_per_insn, env->total_states, | |
13965 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
13966 | } |
13967 | ||
27ae7997 MKL |
13968 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
13969 | { | |
13970 | const struct btf_type *t, *func_proto; | |
13971 | const struct bpf_struct_ops *st_ops; | |
13972 | const struct btf_member *member; | |
13973 | struct bpf_prog *prog = env->prog; | |
13974 | u32 btf_id, member_idx; | |
13975 | const char *mname; | |
13976 | ||
12aa8a94 THJ |
13977 | if (!prog->gpl_compatible) { |
13978 | verbose(env, "struct ops programs must have a GPL compatible license\n"); | |
13979 | return -EINVAL; | |
13980 | } | |
13981 | ||
27ae7997 MKL |
13982 | btf_id = prog->aux->attach_btf_id; |
13983 | st_ops = bpf_struct_ops_find(btf_id); | |
13984 | if (!st_ops) { | |
13985 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
13986 | btf_id); | |
13987 | return -ENOTSUPP; | |
13988 | } | |
13989 | ||
13990 | t = st_ops->type; | |
13991 | member_idx = prog->expected_attach_type; | |
13992 | if (member_idx >= btf_type_vlen(t)) { | |
13993 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
13994 | member_idx, st_ops->name); | |
13995 | return -EINVAL; | |
13996 | } | |
13997 | ||
13998 | member = &btf_type_member(t)[member_idx]; | |
13999 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
14000 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
14001 | NULL); | |
14002 | if (!func_proto) { | |
14003 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
14004 | mname, member_idx, st_ops->name); | |
14005 | return -EINVAL; | |
14006 | } | |
14007 | ||
14008 | if (st_ops->check_member) { | |
14009 | int err = st_ops->check_member(t, member); | |
14010 | ||
14011 | if (err) { | |
14012 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
14013 | mname, st_ops->name); | |
14014 | return err; | |
14015 | } | |
14016 | } | |
14017 | ||
14018 | prog->aux->attach_func_proto = func_proto; | |
14019 | prog->aux->attach_func_name = mname; | |
14020 | env->ops = st_ops->verifier_ops; | |
14021 | ||
14022 | return 0; | |
14023 | } | |
6ba43b76 KS |
14024 | #define SECURITY_PREFIX "security_" |
14025 | ||
f7b12b6f | 14026 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 14027 | { |
69191754 | 14028 | if (within_error_injection_list(addr) || |
f7b12b6f | 14029 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 14030 | return 0; |
6ba43b76 | 14031 | |
6ba43b76 KS |
14032 | return -EINVAL; |
14033 | } | |
27ae7997 | 14034 | |
1e6c62a8 AS |
14035 | /* list of non-sleepable functions that are otherwise on |
14036 | * ALLOW_ERROR_INJECTION list | |
14037 | */ | |
14038 | BTF_SET_START(btf_non_sleepable_error_inject) | |
14039 | /* Three functions below can be called from sleepable and non-sleepable context. | |
14040 | * Assume non-sleepable from bpf safety point of view. | |
14041 | */ | |
9dd3d069 | 14042 | BTF_ID(func, __filemap_add_folio) |
1e6c62a8 AS |
14043 | BTF_ID(func, should_fail_alloc_page) |
14044 | BTF_ID(func, should_failslab) | |
14045 | BTF_SET_END(btf_non_sleepable_error_inject) | |
14046 | ||
14047 | static int check_non_sleepable_error_inject(u32 btf_id) | |
14048 | { | |
14049 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
14050 | } | |
14051 | ||
f7b12b6f THJ |
14052 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
14053 | const struct bpf_prog *prog, | |
14054 | const struct bpf_prog *tgt_prog, | |
14055 | u32 btf_id, | |
14056 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 14057 | { |
be8704ff | 14058 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 14059 | const char prefix[] = "btf_trace_"; |
5b92a28a | 14060 | int ret = 0, subprog = -1, i; |
38207291 | 14061 | const struct btf_type *t; |
5b92a28a | 14062 | bool conservative = true; |
38207291 | 14063 | const char *tname; |
5b92a28a | 14064 | struct btf *btf; |
f7b12b6f | 14065 | long addr = 0; |
38207291 | 14066 | |
f1b9509c | 14067 | if (!btf_id) { |
efc68158 | 14068 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
14069 | return -EINVAL; |
14070 | } | |
22dc4a0f | 14071 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 14072 | if (!btf) { |
efc68158 | 14073 | bpf_log(log, |
5b92a28a AS |
14074 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
14075 | return -EINVAL; | |
14076 | } | |
14077 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 14078 | if (!t) { |
efc68158 | 14079 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
14080 | return -EINVAL; |
14081 | } | |
5b92a28a | 14082 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 14083 | if (!tname) { |
efc68158 | 14084 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
14085 | return -EINVAL; |
14086 | } | |
5b92a28a AS |
14087 | if (tgt_prog) { |
14088 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
14089 | ||
14090 | for (i = 0; i < aux->func_info_cnt; i++) | |
14091 | if (aux->func_info[i].type_id == btf_id) { | |
14092 | subprog = i; | |
14093 | break; | |
14094 | } | |
14095 | if (subprog == -1) { | |
efc68158 | 14096 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
14097 | return -EINVAL; |
14098 | } | |
14099 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
14100 | if (prog_extension) { |
14101 | if (conservative) { | |
efc68158 | 14102 | bpf_log(log, |
be8704ff AS |
14103 | "Cannot replace static functions\n"); |
14104 | return -EINVAL; | |
14105 | } | |
14106 | if (!prog->jit_requested) { | |
efc68158 | 14107 | bpf_log(log, |
be8704ff AS |
14108 | "Extension programs should be JITed\n"); |
14109 | return -EINVAL; | |
14110 | } | |
be8704ff AS |
14111 | } |
14112 | if (!tgt_prog->jited) { | |
efc68158 | 14113 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
14114 | return -EINVAL; |
14115 | } | |
14116 | if (tgt_prog->type == prog->type) { | |
14117 | /* Cannot fentry/fexit another fentry/fexit program. | |
14118 | * Cannot attach program extension to another extension. | |
14119 | * It's ok to attach fentry/fexit to extension program. | |
14120 | */ | |
efc68158 | 14121 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
14122 | return -EINVAL; |
14123 | } | |
14124 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
14125 | prog_extension && | |
14126 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
14127 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
14128 | /* Program extensions can extend all program types | |
14129 | * except fentry/fexit. The reason is the following. | |
14130 | * The fentry/fexit programs are used for performance | |
14131 | * analysis, stats and can be attached to any program | |
14132 | * type except themselves. When extension program is | |
14133 | * replacing XDP function it is necessary to allow | |
14134 | * performance analysis of all functions. Both original | |
14135 | * XDP program and its program extension. Hence | |
14136 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
14137 | * allowed. If extending of fentry/fexit was allowed it | |
14138 | * would be possible to create long call chain | |
14139 | * fentry->extension->fentry->extension beyond | |
14140 | * reasonable stack size. Hence extending fentry is not | |
14141 | * allowed. | |
14142 | */ | |
efc68158 | 14143 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
14144 | return -EINVAL; |
14145 | } | |
5b92a28a | 14146 | } else { |
be8704ff | 14147 | if (prog_extension) { |
efc68158 | 14148 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
14149 | return -EINVAL; |
14150 | } | |
5b92a28a | 14151 | } |
f1b9509c AS |
14152 | |
14153 | switch (prog->expected_attach_type) { | |
14154 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 14155 | if (tgt_prog) { |
efc68158 | 14156 | bpf_log(log, |
5b92a28a AS |
14157 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
14158 | return -EINVAL; | |
14159 | } | |
38207291 | 14160 | if (!btf_type_is_typedef(t)) { |
efc68158 | 14161 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
14162 | btf_id); |
14163 | return -EINVAL; | |
14164 | } | |
f1b9509c | 14165 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 14166 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
14167 | btf_id, tname); |
14168 | return -EINVAL; | |
14169 | } | |
14170 | tname += sizeof(prefix) - 1; | |
5b92a28a | 14171 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
14172 | if (!btf_type_is_ptr(t)) |
14173 | /* should never happen in valid vmlinux build */ | |
14174 | return -EINVAL; | |
5b92a28a | 14175 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
14176 | if (!btf_type_is_func_proto(t)) |
14177 | /* should never happen in valid vmlinux build */ | |
14178 | return -EINVAL; | |
14179 | ||
f7b12b6f | 14180 | break; |
15d83c4d YS |
14181 | case BPF_TRACE_ITER: |
14182 | if (!btf_type_is_func(t)) { | |
efc68158 | 14183 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
14184 | btf_id); |
14185 | return -EINVAL; | |
14186 | } | |
14187 | t = btf_type_by_id(btf, t->type); | |
14188 | if (!btf_type_is_func_proto(t)) | |
14189 | return -EINVAL; | |
f7b12b6f THJ |
14190 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
14191 | if (ret) | |
14192 | return ret; | |
14193 | break; | |
be8704ff AS |
14194 | default: |
14195 | if (!prog_extension) | |
14196 | return -EINVAL; | |
df561f66 | 14197 | fallthrough; |
ae240823 | 14198 | case BPF_MODIFY_RETURN: |
9e4e01df | 14199 | case BPF_LSM_MAC: |
fec56f58 AS |
14200 | case BPF_TRACE_FENTRY: |
14201 | case BPF_TRACE_FEXIT: | |
14202 | if (!btf_type_is_func(t)) { | |
efc68158 | 14203 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
14204 | btf_id); |
14205 | return -EINVAL; | |
14206 | } | |
be8704ff | 14207 | if (prog_extension && |
efc68158 | 14208 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 14209 | return -EINVAL; |
5b92a28a | 14210 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
14211 | if (!btf_type_is_func_proto(t)) |
14212 | return -EINVAL; | |
f7b12b6f | 14213 | |
4a1e7c0c THJ |
14214 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
14215 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
14216 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
14217 | return -EINVAL; | |
14218 | ||
f7b12b6f | 14219 | if (tgt_prog && conservative) |
5b92a28a | 14220 | t = NULL; |
f7b12b6f THJ |
14221 | |
14222 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 14223 | if (ret < 0) |
f7b12b6f THJ |
14224 | return ret; |
14225 | ||
5b92a28a | 14226 | if (tgt_prog) { |
e9eeec58 YS |
14227 | if (subprog == 0) |
14228 | addr = (long) tgt_prog->bpf_func; | |
14229 | else | |
14230 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
14231 | } else { |
14232 | addr = kallsyms_lookup_name(tname); | |
14233 | if (!addr) { | |
efc68158 | 14234 | bpf_log(log, |
5b92a28a AS |
14235 | "The address of function %s cannot be found\n", |
14236 | tname); | |
f7b12b6f | 14237 | return -ENOENT; |
5b92a28a | 14238 | } |
fec56f58 | 14239 | } |
18644cec | 14240 | |
1e6c62a8 AS |
14241 | if (prog->aux->sleepable) { |
14242 | ret = -EINVAL; | |
14243 | switch (prog->type) { | |
14244 | case BPF_PROG_TYPE_TRACING: | |
14245 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
14246 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
14247 | */ | |
14248 | if (!check_non_sleepable_error_inject(btf_id) && | |
14249 | within_error_injection_list(addr)) | |
14250 | ret = 0; | |
14251 | break; | |
14252 | case BPF_PROG_TYPE_LSM: | |
14253 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
14254 | * Only some of them are sleepable. | |
14255 | */ | |
423f1610 | 14256 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
14257 | ret = 0; |
14258 | break; | |
14259 | default: | |
14260 | break; | |
14261 | } | |
f7b12b6f THJ |
14262 | if (ret) { |
14263 | bpf_log(log, "%s is not sleepable\n", tname); | |
14264 | return ret; | |
14265 | } | |
1e6c62a8 | 14266 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 14267 | if (tgt_prog) { |
efc68158 | 14268 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
14269 | return -EINVAL; |
14270 | } | |
14271 | ret = check_attach_modify_return(addr, tname); | |
14272 | if (ret) { | |
14273 | bpf_log(log, "%s() is not modifiable\n", tname); | |
14274 | return ret; | |
1af9270e | 14275 | } |
18644cec | 14276 | } |
f7b12b6f THJ |
14277 | |
14278 | break; | |
14279 | } | |
14280 | tgt_info->tgt_addr = addr; | |
14281 | tgt_info->tgt_name = tname; | |
14282 | tgt_info->tgt_type = t; | |
14283 | return 0; | |
14284 | } | |
14285 | ||
35e3815f JO |
14286 | BTF_SET_START(btf_id_deny) |
14287 | BTF_ID_UNUSED | |
14288 | #ifdef CONFIG_SMP | |
14289 | BTF_ID(func, migrate_disable) | |
14290 | BTF_ID(func, migrate_enable) | |
14291 | #endif | |
14292 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU | |
14293 | BTF_ID(func, rcu_read_unlock_strict) | |
14294 | #endif | |
14295 | BTF_SET_END(btf_id_deny) | |
14296 | ||
f7b12b6f THJ |
14297 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
14298 | { | |
14299 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 14300 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
14301 | struct bpf_attach_target_info tgt_info = {}; |
14302 | u32 btf_id = prog->aux->attach_btf_id; | |
14303 | struct bpf_trampoline *tr; | |
14304 | int ret; | |
14305 | u64 key; | |
14306 | ||
79a7f8bd AS |
14307 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
14308 | if (prog->aux->sleepable) | |
14309 | /* attach_btf_id checked to be zero already */ | |
14310 | return 0; | |
14311 | verbose(env, "Syscall programs can only be sleepable\n"); | |
14312 | return -EINVAL; | |
14313 | } | |
14314 | ||
f7b12b6f THJ |
14315 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
14316 | prog->type != BPF_PROG_TYPE_LSM) { | |
14317 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); | |
14318 | return -EINVAL; | |
14319 | } | |
14320 | ||
14321 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
14322 | return check_struct_ops_btf_id(env); | |
14323 | ||
14324 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
14325 | prog->type != BPF_PROG_TYPE_LSM && | |
14326 | prog->type != BPF_PROG_TYPE_EXT) | |
14327 | return 0; | |
14328 | ||
14329 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
14330 | if (ret) | |
fec56f58 | 14331 | return ret; |
f7b12b6f THJ |
14332 | |
14333 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
14334 | /* to make freplace equivalent to their targets, they need to |
14335 | * inherit env->ops and expected_attach_type for the rest of the | |
14336 | * verification | |
14337 | */ | |
f7b12b6f THJ |
14338 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
14339 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
14340 | } | |
14341 | ||
14342 | /* store info about the attachment target that will be used later */ | |
14343 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
14344 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
14345 | ||
4a1e7c0c THJ |
14346 | if (tgt_prog) { |
14347 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
14348 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
14349 | } | |
14350 | ||
f7b12b6f THJ |
14351 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
14352 | prog->aux->attach_btf_trace = true; | |
14353 | return 0; | |
14354 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
14355 | if (!bpf_iter_prog_supported(prog)) | |
14356 | return -EINVAL; | |
14357 | return 0; | |
14358 | } | |
14359 | ||
14360 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
14361 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
14362 | if (ret < 0) | |
14363 | return ret; | |
35e3815f JO |
14364 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
14365 | btf_id_set_contains(&btf_id_deny, btf_id)) { | |
14366 | return -EINVAL; | |
38207291 | 14367 | } |
f7b12b6f | 14368 | |
22dc4a0f | 14369 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
14370 | tr = bpf_trampoline_get(key, &tgt_info); |
14371 | if (!tr) | |
14372 | return -ENOMEM; | |
14373 | ||
3aac1ead | 14374 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 14375 | return 0; |
38207291 MKL |
14376 | } |
14377 | ||
76654e67 AM |
14378 | struct btf *bpf_get_btf_vmlinux(void) |
14379 | { | |
14380 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
14381 | mutex_lock(&bpf_verifier_lock); | |
14382 | if (!btf_vmlinux) | |
14383 | btf_vmlinux = btf_parse_vmlinux(); | |
14384 | mutex_unlock(&bpf_verifier_lock); | |
14385 | } | |
14386 | return btf_vmlinux; | |
14387 | } | |
14388 | ||
af2ac3e1 | 14389 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
51580e79 | 14390 | { |
06ee7115 | 14391 | u64 start_time = ktime_get_ns(); |
58e2af8b | 14392 | struct bpf_verifier_env *env; |
b9193c1b | 14393 | struct bpf_verifier_log *log; |
9e4c24e7 | 14394 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 14395 | bool is_priv; |
51580e79 | 14396 | |
eba0c929 AB |
14397 | /* no program is valid */ |
14398 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
14399 | return -EINVAL; | |
14400 | ||
58e2af8b | 14401 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
14402 | * allocate/free it every time bpf_check() is called |
14403 | */ | |
58e2af8b | 14404 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
14405 | if (!env) |
14406 | return -ENOMEM; | |
61bd5218 | 14407 | log = &env->log; |
cbd35700 | 14408 | |
9e4c24e7 | 14409 | len = (*prog)->len; |
fad953ce | 14410 | env->insn_aux_data = |
9e4c24e7 | 14411 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
14412 | ret = -ENOMEM; |
14413 | if (!env->insn_aux_data) | |
14414 | goto err_free_env; | |
9e4c24e7 JK |
14415 | for (i = 0; i < len; i++) |
14416 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 14417 | env->prog = *prog; |
00176a34 | 14418 | env->ops = bpf_verifier_ops[env->prog->type]; |
387544bf | 14419 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
2c78ee89 | 14420 | is_priv = bpf_capable(); |
0246e64d | 14421 | |
76654e67 | 14422 | bpf_get_btf_vmlinux(); |
8580ac94 | 14423 | |
cbd35700 | 14424 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
14425 | if (!is_priv) |
14426 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
14427 | |
14428 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
14429 | /* user requested verbose verifier output | |
14430 | * and supplied buffer to store the verification trace | |
14431 | */ | |
e7bf8249 JK |
14432 | log->level = attr->log_level; |
14433 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
14434 | log->len_total = attr->log_size; | |
cbd35700 | 14435 | |
e7bf8249 | 14436 | /* log attributes have to be sane */ |
866de407 HT |
14437 | if (!bpf_verifier_log_attr_valid(log)) { |
14438 | ret = -EINVAL; | |
3df126f3 | 14439 | goto err_unlock; |
866de407 | 14440 | } |
cbd35700 | 14441 | } |
1ad2f583 | 14442 | |
0f55f9ed CL |
14443 | mark_verifier_state_clean(env); |
14444 | ||
8580ac94 AS |
14445 | if (IS_ERR(btf_vmlinux)) { |
14446 | /* Either gcc or pahole or kernel are broken. */ | |
14447 | verbose(env, "in-kernel BTF is malformed\n"); | |
14448 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 14449 | goto skip_full_check; |
8580ac94 AS |
14450 | } |
14451 | ||
1ad2f583 DB |
14452 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
14453 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 14454 | env->strict_alignment = true; |
e9ee9efc DM |
14455 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
14456 | env->strict_alignment = false; | |
cbd35700 | 14457 | |
2c78ee89 | 14458 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
01f810ac | 14459 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
41c48f3a | 14460 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
14461 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
14462 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
14463 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 14464 | |
10d274e8 AS |
14465 | if (is_priv) |
14466 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
14467 | ||
dc2a4ebc | 14468 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 14469 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
14470 | GFP_USER); |
14471 | ret = -ENOMEM; | |
14472 | if (!env->explored_states) | |
14473 | goto skip_full_check; | |
14474 | ||
e6ac2450 MKL |
14475 | ret = add_subprog_and_kfunc(env); |
14476 | if (ret < 0) | |
14477 | goto skip_full_check; | |
14478 | ||
d9762e84 | 14479 | ret = check_subprogs(env); |
475fb78f AS |
14480 | if (ret < 0) |
14481 | goto skip_full_check; | |
14482 | ||
c454a46b | 14483 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
14484 | if (ret < 0) |
14485 | goto skip_full_check; | |
14486 | ||
be8704ff AS |
14487 | ret = check_attach_btf_id(env); |
14488 | if (ret) | |
14489 | goto skip_full_check; | |
14490 | ||
4976b718 HL |
14491 | ret = resolve_pseudo_ldimm64(env); |
14492 | if (ret < 0) | |
14493 | goto skip_full_check; | |
14494 | ||
ceb11679 YZ |
14495 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
14496 | ret = bpf_prog_offload_verifier_prep(env->prog); | |
14497 | if (ret) | |
14498 | goto skip_full_check; | |
14499 | } | |
14500 | ||
d9762e84 MKL |
14501 | ret = check_cfg(env); |
14502 | if (ret < 0) | |
14503 | goto skip_full_check; | |
14504 | ||
51c39bb1 AS |
14505 | ret = do_check_subprogs(env); |
14506 | ret = ret ?: do_check_main(env); | |
cbd35700 | 14507 | |
c941ce9c QM |
14508 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
14509 | ret = bpf_prog_offload_finalize(env); | |
14510 | ||
0246e64d | 14511 | skip_full_check: |
51c39bb1 | 14512 | kvfree(env->explored_states); |
0246e64d | 14513 | |
c131187d | 14514 | if (ret == 0) |
9b38c405 | 14515 | ret = check_max_stack_depth(env); |
c131187d | 14516 | |
9b38c405 | 14517 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
14518 | if (is_priv) { |
14519 | if (ret == 0) | |
14520 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
14521 | if (ret == 0) |
14522 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
14523 | if (ret == 0) |
14524 | ret = opt_remove_nops(env); | |
52875a04 JK |
14525 | } else { |
14526 | if (ret == 0) | |
14527 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
14528 | } |
14529 | ||
9bac3d6d AS |
14530 | if (ret == 0) |
14531 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
14532 | ret = convert_ctx_accesses(env); | |
14533 | ||
e245c5c6 | 14534 | if (ret == 0) |
e6ac5933 | 14535 | ret = do_misc_fixups(env); |
e245c5c6 | 14536 | |
a4b1d3c1 JW |
14537 | /* do 32-bit optimization after insn patching has done so those patched |
14538 | * insns could be handled correctly. | |
14539 | */ | |
d6c2308c JW |
14540 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
14541 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
14542 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
14543 | : false; | |
a4b1d3c1 JW |
14544 | } |
14545 | ||
1ea47e01 AS |
14546 | if (ret == 0) |
14547 | ret = fixup_call_args(env); | |
14548 | ||
06ee7115 AS |
14549 | env->verification_time = ktime_get_ns() - start_time; |
14550 | print_verification_stats(env); | |
aba64c7d | 14551 | env->prog->aux->verified_insns = env->insn_processed; |
06ee7115 | 14552 | |
a2a7d570 | 14553 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 14554 | ret = -ENOSPC; |
a2a7d570 | 14555 | if (log->level && !log->ubuf) { |
cbd35700 | 14556 | ret = -EFAULT; |
a2a7d570 | 14557 | goto err_release_maps; |
cbd35700 AS |
14558 | } |
14559 | ||
541c3bad AN |
14560 | if (ret) |
14561 | goto err_release_maps; | |
14562 | ||
14563 | if (env->used_map_cnt) { | |
0246e64d | 14564 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
14565 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
14566 | sizeof(env->used_maps[0]), | |
14567 | GFP_KERNEL); | |
0246e64d | 14568 | |
9bac3d6d | 14569 | if (!env->prog->aux->used_maps) { |
0246e64d | 14570 | ret = -ENOMEM; |
a2a7d570 | 14571 | goto err_release_maps; |
0246e64d AS |
14572 | } |
14573 | ||
9bac3d6d | 14574 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 14575 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 14576 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
14577 | } |
14578 | if (env->used_btf_cnt) { | |
14579 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
14580 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
14581 | sizeof(env->used_btfs[0]), | |
14582 | GFP_KERNEL); | |
14583 | if (!env->prog->aux->used_btfs) { | |
14584 | ret = -ENOMEM; | |
14585 | goto err_release_maps; | |
14586 | } | |
0246e64d | 14587 | |
541c3bad AN |
14588 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
14589 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
14590 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
14591 | } | |
14592 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
14593 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
14594 | * bpf_ld_imm64 instructions | |
14595 | */ | |
14596 | convert_pseudo_ld_imm64(env); | |
14597 | } | |
cbd35700 | 14598 | |
541c3bad | 14599 | adjust_btf_func(env); |
ba64e7d8 | 14600 | |
a2a7d570 | 14601 | err_release_maps: |
9bac3d6d | 14602 | if (!env->prog->aux->used_maps) |
0246e64d | 14603 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 14604 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
14605 | */ |
14606 | release_maps(env); | |
541c3bad AN |
14607 | if (!env->prog->aux->used_btfs) |
14608 | release_btfs(env); | |
03f87c0b THJ |
14609 | |
14610 | /* extension progs temporarily inherit the attach_type of their targets | |
14611 | for verification purposes, so set it back to zero before returning | |
14612 | */ | |
14613 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
14614 | env->prog->expected_attach_type = 0; | |
14615 | ||
9bac3d6d | 14616 | *prog = env->prog; |
3df126f3 | 14617 | err_unlock: |
45a73c17 AS |
14618 | if (!is_priv) |
14619 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
14620 | vfree(env->insn_aux_data); |
14621 | err_free_env: | |
14622 | kfree(env); | |
51580e79 AS |
14623 | return ret; |
14624 | } |