]>
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> |
51580e79 AS |
7 | #include <linux/kernel.h> |
8 | #include <linux/types.h> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/bpf.h> | |
838e9690 | 11 | #include <linux/btf.h> |
58e2af8b | 12 | #include <linux/bpf_verifier.h> |
51580e79 AS |
13 | #include <linux/filter.h> |
14 | #include <net/netlink.h> | |
15 | #include <linux/file.h> | |
16 | #include <linux/vmalloc.h> | |
ebb676da | 17 | #include <linux/stringify.h> |
cc8b0b92 AS |
18 | #include <linux/bsearch.h> |
19 | #include <linux/sort.h> | |
c195651e | 20 | #include <linux/perf_event.h> |
d9762e84 | 21 | #include <linux/ctype.h> |
51580e79 | 22 | |
f4ac7e0b JK |
23 | #include "disasm.h" |
24 | ||
00176a34 | 25 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 26 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
27 | [_id] = & _name ## _verifier_ops, |
28 | #define BPF_MAP_TYPE(_id, _ops) | |
29 | #include <linux/bpf_types.h> | |
30 | #undef BPF_PROG_TYPE | |
31 | #undef BPF_MAP_TYPE | |
32 | }; | |
33 | ||
51580e79 AS |
34 | /* bpf_check() is a static code analyzer that walks eBPF program |
35 | * instruction by instruction and updates register/stack state. | |
36 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
37 | * | |
38 | * The first pass is depth-first-search to check that the program is a DAG. | |
39 | * It rejects the following programs: | |
40 | * - larger than BPF_MAXINSNS insns | |
41 | * - if loop is present (detected via back-edge) | |
42 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
43 | * - out of bounds or malformed jumps | |
44 | * The second pass is all possible path descent from the 1st insn. | |
45 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 46 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
47 | * insn is less then 4K, but there are too many branches that change stack/regs. |
48 | * Number of 'branches to be analyzed' is limited to 1k | |
49 | * | |
50 | * On entry to each instruction, each register has a type, and the instruction | |
51 | * changes the types of the registers depending on instruction semantics. | |
52 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
53 | * copied to R1. | |
54 | * | |
55 | * All registers are 64-bit. | |
56 | * R0 - return register | |
57 | * R1-R5 argument passing registers | |
58 | * R6-R9 callee saved registers | |
59 | * R10 - frame pointer read-only | |
60 | * | |
61 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
62 | * and has type PTR_TO_CTX. | |
63 | * | |
64 | * Verifier tracks arithmetic operations on pointers in case: | |
65 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
66 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
67 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
68 | * and 2nd arithmetic instruction is pattern matched to recognize | |
69 | * that it wants to construct a pointer to some element within stack. | |
70 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
71 | * (and -20 constant is saved for further stack bounds checking). | |
72 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
73 | * | |
f1174f77 | 74 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 75 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 76 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
77 | * |
78 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
79 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
80 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
81 | * |
82 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
83 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
84 | * | |
85 | * registers used to pass values to function calls are checked against | |
86 | * function argument constraints. | |
87 | * | |
88 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
89 | * It means that the register type passed to this function must be | |
90 | * PTR_TO_STACK and it will be used inside the function as | |
91 | * 'pointer to map element key' | |
92 | * | |
93 | * For example the argument constraints for bpf_map_lookup_elem(): | |
94 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
95 | * .arg1_type = ARG_CONST_MAP_PTR, | |
96 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
97 | * | |
98 | * ret_type says that this function returns 'pointer to map elem value or null' | |
99 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
100 | * 2nd argument should be a pointer to stack, which will be used inside | |
101 | * the helper function as a pointer to map element key. | |
102 | * | |
103 | * On the kernel side the helper function looks like: | |
104 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
105 | * { | |
106 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
107 | * void *key = (void *) (unsigned long) r2; | |
108 | * void *value; | |
109 | * | |
110 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
111 | * [key, key + map->key_size) bytes are valid and were initialized on | |
112 | * the stack of eBPF program. | |
113 | * } | |
114 | * | |
115 | * Corresponding eBPF program may look like: | |
116 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
117 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
118 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
119 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
120 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
121 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
122 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
123 | * | |
124 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
125 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
126 | * and were initialized prior to this call. | |
127 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
128 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
129 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
130 | * returns ether pointer to map value or NULL. | |
131 | * | |
132 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
133 | * insn, the register holding that pointer in the true branch changes state to | |
134 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
135 | * branch. See check_cond_jmp_op(). | |
136 | * | |
137 | * After the call R0 is set to return type of the function and registers R1-R5 | |
138 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
139 | * |
140 | * The following reference types represent a potential reference to a kernel | |
141 | * resource which, after first being allocated, must be checked and freed by | |
142 | * the BPF program: | |
143 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
144 | * | |
145 | * When the verifier sees a helper call return a reference type, it allocates a | |
146 | * pointer id for the reference and stores it in the current function state. | |
147 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
148 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
149 | * passes through a NULL-check conditional. For the branch wherein the state is | |
150 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
151 | * |
152 | * For each helper function that allocates a reference, such as | |
153 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
154 | * bpf_sk_release(). When a reference type passes into the release function, | |
155 | * the verifier also releases the reference. If any unchecked or unreleased | |
156 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
157 | */ |
158 | ||
17a52670 | 159 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 160 | struct bpf_verifier_stack_elem { |
17a52670 AS |
161 | /* verifer state is 'st' |
162 | * before processing instruction 'insn_idx' | |
163 | * and after processing instruction 'prev_insn_idx' | |
164 | */ | |
58e2af8b | 165 | struct bpf_verifier_state st; |
17a52670 AS |
166 | int insn_idx; |
167 | int prev_insn_idx; | |
58e2af8b | 168 | struct bpf_verifier_stack_elem *next; |
cbd35700 AS |
169 | }; |
170 | ||
b285fcb7 | 171 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 172 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 173 | |
d2e4c1e6 DB |
174 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
175 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
176 | ||
c93552c4 DB |
177 | #define BPF_MAP_PTR_UNPRIV 1UL |
178 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
179 | POISON_POINTER_DELTA)) | |
180 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
181 | ||
182 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) | |
183 | { | |
d2e4c1e6 | 184 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
185 | } |
186 | ||
187 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
188 | { | |
d2e4c1e6 | 189 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
190 | } |
191 | ||
192 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
193 | const struct bpf_map *map, bool unpriv) | |
194 | { | |
195 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
196 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
197 | aux->map_ptr_state = (unsigned long)map | |
198 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
199 | } | |
200 | ||
201 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
202 | { | |
203 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
204 | } | |
205 | ||
206 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
207 | { | |
208 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
209 | } | |
210 | ||
211 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
212 | { | |
213 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
214 | } | |
215 | ||
216 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
217 | { | |
218 | bool poisoned = bpf_map_key_poisoned(aux); | |
219 | ||
220 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
221 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 222 | } |
fad73a1a | 223 | |
33ff9823 DB |
224 | struct bpf_call_arg_meta { |
225 | struct bpf_map *map_ptr; | |
435faee1 | 226 | bool raw_mode; |
36bbef52 | 227 | bool pkt_access; |
435faee1 DB |
228 | int regno; |
229 | int access_size; | |
849fa506 YS |
230 | s64 msize_smax_value; |
231 | u64 msize_umax_value; | |
1b986589 | 232 | int ref_obj_id; |
d83525ca | 233 | int func_id; |
a7658e1a | 234 | u32 btf_id; |
33ff9823 DB |
235 | }; |
236 | ||
8580ac94 AS |
237 | struct btf *btf_vmlinux; |
238 | ||
cbd35700 AS |
239 | static DEFINE_MUTEX(bpf_verifier_lock); |
240 | ||
d9762e84 MKL |
241 | static const struct bpf_line_info * |
242 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
243 | { | |
244 | const struct bpf_line_info *linfo; | |
245 | const struct bpf_prog *prog; | |
246 | u32 i, nr_linfo; | |
247 | ||
248 | prog = env->prog; | |
249 | nr_linfo = prog->aux->nr_linfo; | |
250 | ||
251 | if (!nr_linfo || insn_off >= prog->len) | |
252 | return NULL; | |
253 | ||
254 | linfo = prog->aux->linfo; | |
255 | for (i = 1; i < nr_linfo; i++) | |
256 | if (insn_off < linfo[i].insn_off) | |
257 | break; | |
258 | ||
259 | return &linfo[i - 1]; | |
260 | } | |
261 | ||
77d2e05a MKL |
262 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
263 | va_list args) | |
cbd35700 | 264 | { |
a2a7d570 | 265 | unsigned int n; |
cbd35700 | 266 | |
a2a7d570 | 267 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
268 | |
269 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
270 | "verifier log line truncated - local buffer too short\n"); | |
271 | ||
272 | n = min(log->len_total - log->len_used - 1, n); | |
273 | log->kbuf[n] = '\0'; | |
274 | ||
8580ac94 AS |
275 | if (log->level == BPF_LOG_KERNEL) { |
276 | pr_err("BPF:%s\n", log->kbuf); | |
277 | return; | |
278 | } | |
a2a7d570 JK |
279 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
280 | log->len_used += n; | |
281 | else | |
282 | log->ubuf = NULL; | |
cbd35700 | 283 | } |
abe08840 JO |
284 | |
285 | /* log_level controls verbosity level of eBPF verifier. | |
286 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
287 | * so the user can figure out what's wrong with the program | |
430e68d1 | 288 | */ |
abe08840 JO |
289 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
290 | const char *fmt, ...) | |
291 | { | |
292 | va_list args; | |
293 | ||
77d2e05a MKL |
294 | if (!bpf_verifier_log_needed(&env->log)) |
295 | return; | |
296 | ||
abe08840 | 297 | va_start(args, fmt); |
77d2e05a | 298 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
299 | va_end(args); |
300 | } | |
301 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
302 | ||
303 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
304 | { | |
77d2e05a | 305 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
306 | va_list args; |
307 | ||
77d2e05a MKL |
308 | if (!bpf_verifier_log_needed(&env->log)) |
309 | return; | |
310 | ||
abe08840 | 311 | va_start(args, fmt); |
77d2e05a | 312 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
313 | va_end(args); |
314 | } | |
cbd35700 | 315 | |
9e15db66 AS |
316 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
317 | const char *fmt, ...) | |
318 | { | |
319 | va_list args; | |
320 | ||
321 | if (!bpf_verifier_log_needed(log)) | |
322 | return; | |
323 | ||
324 | va_start(args, fmt); | |
325 | bpf_verifier_vlog(log, fmt, args); | |
326 | va_end(args); | |
327 | } | |
328 | ||
d9762e84 MKL |
329 | static const char *ltrim(const char *s) |
330 | { | |
331 | while (isspace(*s)) | |
332 | s++; | |
333 | ||
334 | return s; | |
335 | } | |
336 | ||
337 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
338 | u32 insn_off, | |
339 | const char *prefix_fmt, ...) | |
340 | { | |
341 | const struct bpf_line_info *linfo; | |
342 | ||
343 | if (!bpf_verifier_log_needed(&env->log)) | |
344 | return; | |
345 | ||
346 | linfo = find_linfo(env, insn_off); | |
347 | if (!linfo || linfo == env->prev_linfo) | |
348 | return; | |
349 | ||
350 | if (prefix_fmt) { | |
351 | va_list args; | |
352 | ||
353 | va_start(args, prefix_fmt); | |
354 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
355 | va_end(args); | |
356 | } | |
357 | ||
358 | verbose(env, "%s\n", | |
359 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
360 | linfo->line_off))); | |
361 | ||
362 | env->prev_linfo = linfo; | |
363 | } | |
364 | ||
de8f3a83 DB |
365 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
366 | { | |
367 | return type == PTR_TO_PACKET || | |
368 | type == PTR_TO_PACKET_META; | |
369 | } | |
370 | ||
46f8bc92 MKL |
371 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
372 | { | |
373 | return type == PTR_TO_SOCKET || | |
655a51e5 | 374 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
375 | type == PTR_TO_TCP_SOCK || |
376 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
377 | } |
378 | ||
840b9615 JS |
379 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
380 | { | |
fd978bf7 | 381 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 382 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 MKL |
383 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
384 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
fd978bf7 JS |
385 | } |
386 | ||
d83525ca AS |
387 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
388 | { | |
389 | return reg->type == PTR_TO_MAP_VALUE && | |
390 | map_value_has_spin_lock(reg->map_ptr); | |
391 | } | |
392 | ||
cba368c1 MKL |
393 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
394 | { | |
395 | return type == PTR_TO_SOCKET || | |
396 | type == PTR_TO_SOCKET_OR_NULL || | |
397 | type == PTR_TO_TCP_SOCK || | |
398 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
399 | } | |
400 | ||
1b986589 | 401 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 402 | { |
1b986589 | 403 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
404 | } |
405 | ||
406 | /* Determine whether the function releases some resources allocated by another | |
407 | * function call. The first reference type argument will be assumed to be | |
408 | * released by release_reference(). | |
409 | */ | |
410 | static bool is_release_function(enum bpf_func_id func_id) | |
411 | { | |
6acc9b43 | 412 | return func_id == BPF_FUNC_sk_release; |
840b9615 JS |
413 | } |
414 | ||
46f8bc92 MKL |
415 | static bool is_acquire_function(enum bpf_func_id func_id) |
416 | { | |
417 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 LB |
418 | func_id == BPF_FUNC_sk_lookup_udp || |
419 | func_id == BPF_FUNC_skc_lookup_tcp; | |
46f8bc92 MKL |
420 | } |
421 | ||
1b986589 MKL |
422 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
423 | { | |
424 | return func_id == BPF_FUNC_tcp_sock || | |
425 | func_id == BPF_FUNC_sk_fullsock; | |
426 | } | |
427 | ||
17a52670 AS |
428 | /* string representation of 'enum bpf_reg_type' */ |
429 | static const char * const reg_type_str[] = { | |
430 | [NOT_INIT] = "?", | |
f1174f77 | 431 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
432 | [PTR_TO_CTX] = "ctx", |
433 | [CONST_PTR_TO_MAP] = "map_ptr", | |
434 | [PTR_TO_MAP_VALUE] = "map_value", | |
435 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 436 | [PTR_TO_STACK] = "fp", |
969bf05e | 437 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 438 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 439 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 440 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
441 | [PTR_TO_SOCKET] = "sock", |
442 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
443 | [PTR_TO_SOCK_COMMON] = "sock_common", |
444 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
445 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
446 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 447 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 448 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
9e15db66 | 449 | [PTR_TO_BTF_ID] = "ptr_", |
17a52670 AS |
450 | }; |
451 | ||
8efea21d EC |
452 | static char slot_type_char[] = { |
453 | [STACK_INVALID] = '?', | |
454 | [STACK_SPILL] = 'r', | |
455 | [STACK_MISC] = 'm', | |
456 | [STACK_ZERO] = '0', | |
457 | }; | |
458 | ||
4e92024a AS |
459 | static void print_liveness(struct bpf_verifier_env *env, |
460 | enum bpf_reg_liveness live) | |
461 | { | |
9242b5f5 | 462 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
463 | verbose(env, "_"); |
464 | if (live & REG_LIVE_READ) | |
465 | verbose(env, "r"); | |
466 | if (live & REG_LIVE_WRITTEN) | |
467 | verbose(env, "w"); | |
9242b5f5 AS |
468 | if (live & REG_LIVE_DONE) |
469 | verbose(env, "D"); | |
4e92024a AS |
470 | } |
471 | ||
f4d7e40a AS |
472 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
473 | const struct bpf_reg_state *reg) | |
474 | { | |
475 | struct bpf_verifier_state *cur = env->cur_state; | |
476 | ||
477 | return cur->frame[reg->frameno]; | |
478 | } | |
479 | ||
9e15db66 AS |
480 | const char *kernel_type_name(u32 id) |
481 | { | |
482 | return btf_name_by_offset(btf_vmlinux, | |
483 | btf_type_by_id(btf_vmlinux, id)->name_off); | |
484 | } | |
485 | ||
61bd5218 | 486 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 487 | const struct bpf_func_state *state) |
17a52670 | 488 | { |
f4d7e40a | 489 | const struct bpf_reg_state *reg; |
17a52670 AS |
490 | enum bpf_reg_type t; |
491 | int i; | |
492 | ||
f4d7e40a AS |
493 | if (state->frameno) |
494 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 495 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
496 | reg = &state->regs[i]; |
497 | t = reg->type; | |
17a52670 AS |
498 | if (t == NOT_INIT) |
499 | continue; | |
4e92024a AS |
500 | verbose(env, " R%d", i); |
501 | print_liveness(env, reg->live); | |
502 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
503 | if (t == SCALAR_VALUE && reg->precise) |
504 | verbose(env, "P"); | |
f1174f77 EC |
505 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
506 | tnum_is_const(reg->var_off)) { | |
507 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 508 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 509 | } else { |
9e15db66 AS |
510 | if (t == PTR_TO_BTF_ID) |
511 | verbose(env, "%s", kernel_type_name(reg->btf_id)); | |
cba368c1 MKL |
512 | verbose(env, "(id=%d", reg->id); |
513 | if (reg_type_may_be_refcounted_or_null(t)) | |
514 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 515 | if (t != SCALAR_VALUE) |
61bd5218 | 516 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 517 | if (type_is_pkt_pointer(t)) |
61bd5218 | 518 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
519 | else if (t == CONST_PTR_TO_MAP || |
520 | t == PTR_TO_MAP_VALUE || | |
521 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 522 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
523 | reg->map_ptr->key_size, |
524 | reg->map_ptr->value_size); | |
7d1238f2 EC |
525 | if (tnum_is_const(reg->var_off)) { |
526 | /* Typically an immediate SCALAR_VALUE, but | |
527 | * could be a pointer whose offset is too big | |
528 | * for reg->off | |
529 | */ | |
61bd5218 | 530 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
531 | } else { |
532 | if (reg->smin_value != reg->umin_value && | |
533 | reg->smin_value != S64_MIN) | |
61bd5218 | 534 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
535 | (long long)reg->smin_value); |
536 | if (reg->smax_value != reg->umax_value && | |
537 | reg->smax_value != S64_MAX) | |
61bd5218 | 538 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
539 | (long long)reg->smax_value); |
540 | if (reg->umin_value != 0) | |
61bd5218 | 541 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
542 | (unsigned long long)reg->umin_value); |
543 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 544 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
545 | (unsigned long long)reg->umax_value); |
546 | if (!tnum_is_unknown(reg->var_off)) { | |
547 | char tn_buf[48]; | |
f1174f77 | 548 | |
7d1238f2 | 549 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 550 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 551 | } |
f1174f77 | 552 | } |
61bd5218 | 553 | verbose(env, ")"); |
f1174f77 | 554 | } |
17a52670 | 555 | } |
638f5b90 | 556 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
557 | char types_buf[BPF_REG_SIZE + 1]; |
558 | bool valid = false; | |
559 | int j; | |
560 | ||
561 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
562 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
563 | valid = true; | |
564 | types_buf[j] = slot_type_char[ | |
565 | state->stack[i].slot_type[j]]; | |
566 | } | |
567 | types_buf[BPF_REG_SIZE] = 0; | |
568 | if (!valid) | |
569 | continue; | |
570 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
571 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
b5dc0163 AS |
572 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
573 | reg = &state->stack[i].spilled_ptr; | |
574 | t = reg->type; | |
575 | verbose(env, "=%s", reg_type_str[t]); | |
576 | if (t == SCALAR_VALUE && reg->precise) | |
577 | verbose(env, "P"); | |
578 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
579 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
580 | } else { | |
8efea21d | 581 | verbose(env, "=%s", types_buf); |
b5dc0163 | 582 | } |
17a52670 | 583 | } |
fd978bf7 JS |
584 | if (state->acquired_refs && state->refs[0].id) { |
585 | verbose(env, " refs=%d", state->refs[0].id); | |
586 | for (i = 1; i < state->acquired_refs; i++) | |
587 | if (state->refs[i].id) | |
588 | verbose(env, ",%d", state->refs[i].id); | |
589 | } | |
61bd5218 | 590 | verbose(env, "\n"); |
17a52670 AS |
591 | } |
592 | ||
84dbf350 JS |
593 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
594 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
595 | const struct bpf_func_state *src) \ | |
596 | { \ | |
597 | if (!src->FIELD) \ | |
598 | return 0; \ | |
599 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
600 | /* internal bug, make state invalid to reject the program */ \ | |
601 | memset(dst, 0, sizeof(*dst)); \ | |
602 | return -EFAULT; \ | |
603 | } \ | |
604 | memcpy(dst->FIELD, src->FIELD, \ | |
605 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
606 | return 0; \ | |
638f5b90 | 607 | } |
fd978bf7 JS |
608 | /* copy_reference_state() */ |
609 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
610 | /* copy_stack_state() */ |
611 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
612 | #undef COPY_STATE_FN | |
613 | ||
614 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
615 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
616 | bool copy_old) \ | |
617 | { \ | |
618 | u32 old_size = state->COUNT; \ | |
619 | struct bpf_##NAME##_state *new_##FIELD; \ | |
620 | int slot = size / SIZE; \ | |
621 | \ | |
622 | if (size <= old_size || !size) { \ | |
623 | if (copy_old) \ | |
624 | return 0; \ | |
625 | state->COUNT = slot * SIZE; \ | |
626 | if (!size && old_size) { \ | |
627 | kfree(state->FIELD); \ | |
628 | state->FIELD = NULL; \ | |
629 | } \ | |
630 | return 0; \ | |
631 | } \ | |
632 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
633 | GFP_KERNEL); \ | |
634 | if (!new_##FIELD) \ | |
635 | return -ENOMEM; \ | |
636 | if (copy_old) { \ | |
637 | if (state->FIELD) \ | |
638 | memcpy(new_##FIELD, state->FIELD, \ | |
639 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
640 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
641 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
642 | } \ | |
643 | state->COUNT = slot * SIZE; \ | |
644 | kfree(state->FIELD); \ | |
645 | state->FIELD = new_##FIELD; \ | |
646 | return 0; \ | |
647 | } | |
fd978bf7 JS |
648 | /* realloc_reference_state() */ |
649 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
650 | /* realloc_stack_state() */ |
651 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
652 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
653 | |
654 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
655 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 656 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
657 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
658 | * which realloc_stack_state() copies over. It points to previous | |
659 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 660 | */ |
fd978bf7 JS |
661 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
662 | int refs_size, bool copy_old) | |
638f5b90 | 663 | { |
fd978bf7 JS |
664 | int err = realloc_reference_state(state, refs_size, copy_old); |
665 | if (err) | |
666 | return err; | |
667 | return realloc_stack_state(state, stack_size, copy_old); | |
668 | } | |
669 | ||
670 | /* Acquire a pointer id from the env and update the state->refs to include | |
671 | * this new pointer reference. | |
672 | * On success, returns a valid pointer id to associate with the register | |
673 | * On failure, returns a negative errno. | |
638f5b90 | 674 | */ |
fd978bf7 | 675 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 676 | { |
fd978bf7 JS |
677 | struct bpf_func_state *state = cur_func(env); |
678 | int new_ofs = state->acquired_refs; | |
679 | int id, err; | |
680 | ||
681 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
682 | if (err) | |
683 | return err; | |
684 | id = ++env->id_gen; | |
685 | state->refs[new_ofs].id = id; | |
686 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 687 | |
fd978bf7 JS |
688 | return id; |
689 | } | |
690 | ||
691 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 692 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
693 | { |
694 | int i, last_idx; | |
695 | ||
fd978bf7 JS |
696 | last_idx = state->acquired_refs - 1; |
697 | for (i = 0; i < state->acquired_refs; i++) { | |
698 | if (state->refs[i].id == ptr_id) { | |
699 | if (last_idx && i != last_idx) | |
700 | memcpy(&state->refs[i], &state->refs[last_idx], | |
701 | sizeof(*state->refs)); | |
702 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
703 | state->acquired_refs--; | |
638f5b90 | 704 | return 0; |
638f5b90 | 705 | } |
638f5b90 | 706 | } |
46f8bc92 | 707 | return -EINVAL; |
fd978bf7 JS |
708 | } |
709 | ||
710 | static int transfer_reference_state(struct bpf_func_state *dst, | |
711 | struct bpf_func_state *src) | |
712 | { | |
713 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
714 | if (err) | |
715 | return err; | |
716 | err = copy_reference_state(dst, src); | |
717 | if (err) | |
718 | return err; | |
638f5b90 AS |
719 | return 0; |
720 | } | |
721 | ||
f4d7e40a AS |
722 | static void free_func_state(struct bpf_func_state *state) |
723 | { | |
5896351e AS |
724 | if (!state) |
725 | return; | |
fd978bf7 | 726 | kfree(state->refs); |
f4d7e40a AS |
727 | kfree(state->stack); |
728 | kfree(state); | |
729 | } | |
730 | ||
b5dc0163 AS |
731 | static void clear_jmp_history(struct bpf_verifier_state *state) |
732 | { | |
733 | kfree(state->jmp_history); | |
734 | state->jmp_history = NULL; | |
735 | state->jmp_history_cnt = 0; | |
736 | } | |
737 | ||
1969db47 AS |
738 | static void free_verifier_state(struct bpf_verifier_state *state, |
739 | bool free_self) | |
638f5b90 | 740 | { |
f4d7e40a AS |
741 | int i; |
742 | ||
743 | for (i = 0; i <= state->curframe; i++) { | |
744 | free_func_state(state->frame[i]); | |
745 | state->frame[i] = NULL; | |
746 | } | |
b5dc0163 | 747 | clear_jmp_history(state); |
1969db47 AS |
748 | if (free_self) |
749 | kfree(state); | |
638f5b90 AS |
750 | } |
751 | ||
752 | /* copy verifier state from src to dst growing dst stack space | |
753 | * when necessary to accommodate larger src stack | |
754 | */ | |
f4d7e40a AS |
755 | static int copy_func_state(struct bpf_func_state *dst, |
756 | const struct bpf_func_state *src) | |
638f5b90 AS |
757 | { |
758 | int err; | |
759 | ||
fd978bf7 JS |
760 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
761 | false); | |
762 | if (err) | |
763 | return err; | |
764 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
765 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
766 | if (err) |
767 | return err; | |
638f5b90 AS |
768 | return copy_stack_state(dst, src); |
769 | } | |
770 | ||
f4d7e40a AS |
771 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
772 | const struct bpf_verifier_state *src) | |
773 | { | |
774 | struct bpf_func_state *dst; | |
b5dc0163 | 775 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
f4d7e40a AS |
776 | int i, err; |
777 | ||
b5dc0163 AS |
778 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
779 | kfree(dst_state->jmp_history); | |
780 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); | |
781 | if (!dst_state->jmp_history) | |
782 | return -ENOMEM; | |
783 | } | |
784 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); | |
785 | dst_state->jmp_history_cnt = src->jmp_history_cnt; | |
786 | ||
f4d7e40a AS |
787 | /* if dst has more stack frames then src frame, free them */ |
788 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
789 | free_func_state(dst_state->frame[i]); | |
790 | dst_state->frame[i] = NULL; | |
791 | } | |
979d63d5 | 792 | dst_state->speculative = src->speculative; |
f4d7e40a | 793 | dst_state->curframe = src->curframe; |
d83525ca | 794 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
795 | dst_state->branches = src->branches; |
796 | dst_state->parent = src->parent; | |
b5dc0163 AS |
797 | dst_state->first_insn_idx = src->first_insn_idx; |
798 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
799 | for (i = 0; i <= src->curframe; i++) { |
800 | dst = dst_state->frame[i]; | |
801 | if (!dst) { | |
802 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
803 | if (!dst) | |
804 | return -ENOMEM; | |
805 | dst_state->frame[i] = dst; | |
806 | } | |
807 | err = copy_func_state(dst, src->frame[i]); | |
808 | if (err) | |
809 | return err; | |
810 | } | |
811 | return 0; | |
812 | } | |
813 | ||
2589726d AS |
814 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
815 | { | |
816 | while (st) { | |
817 | u32 br = --st->branches; | |
818 | ||
819 | /* WARN_ON(br > 1) technically makes sense here, | |
820 | * but see comment in push_stack(), hence: | |
821 | */ | |
822 | WARN_ONCE((int)br < 0, | |
823 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
824 | br); | |
825 | if (br) | |
826 | break; | |
827 | st = st->parent; | |
828 | } | |
829 | } | |
830 | ||
638f5b90 AS |
831 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
832 | int *insn_idx) | |
833 | { | |
834 | struct bpf_verifier_state *cur = env->cur_state; | |
835 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
836 | int err; | |
17a52670 AS |
837 | |
838 | if (env->head == NULL) | |
638f5b90 | 839 | return -ENOENT; |
17a52670 | 840 | |
638f5b90 AS |
841 | if (cur) { |
842 | err = copy_verifier_state(cur, &head->st); | |
843 | if (err) | |
844 | return err; | |
845 | } | |
846 | if (insn_idx) | |
847 | *insn_idx = head->insn_idx; | |
17a52670 | 848 | if (prev_insn_idx) |
638f5b90 AS |
849 | *prev_insn_idx = head->prev_insn_idx; |
850 | elem = head->next; | |
1969db47 | 851 | free_verifier_state(&head->st, false); |
638f5b90 | 852 | kfree(head); |
17a52670 AS |
853 | env->head = elem; |
854 | env->stack_size--; | |
638f5b90 | 855 | return 0; |
17a52670 AS |
856 | } |
857 | ||
58e2af8b | 858 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
859 | int insn_idx, int prev_insn_idx, |
860 | bool speculative) | |
17a52670 | 861 | { |
638f5b90 | 862 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 863 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 864 | int err; |
17a52670 | 865 | |
638f5b90 | 866 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
867 | if (!elem) |
868 | goto err; | |
869 | ||
17a52670 AS |
870 | elem->insn_idx = insn_idx; |
871 | elem->prev_insn_idx = prev_insn_idx; | |
872 | elem->next = env->head; | |
873 | env->head = elem; | |
874 | env->stack_size++; | |
1969db47 AS |
875 | err = copy_verifier_state(&elem->st, cur); |
876 | if (err) | |
877 | goto err; | |
979d63d5 | 878 | elem->st.speculative |= speculative; |
b285fcb7 AS |
879 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
880 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
881 | env->stack_size); | |
17a52670 AS |
882 | goto err; |
883 | } | |
2589726d AS |
884 | if (elem->st.parent) { |
885 | ++elem->st.parent->branches; | |
886 | /* WARN_ON(branches > 2) technically makes sense here, | |
887 | * but | |
888 | * 1. speculative states will bump 'branches' for non-branch | |
889 | * instructions | |
890 | * 2. is_state_visited() heuristics may decide not to create | |
891 | * a new state for a sequence of branches and all such current | |
892 | * and cloned states will be pointing to a single parent state | |
893 | * which might have large 'branches' count. | |
894 | */ | |
895 | } | |
17a52670 AS |
896 | return &elem->st; |
897 | err: | |
5896351e AS |
898 | free_verifier_state(env->cur_state, true); |
899 | env->cur_state = NULL; | |
17a52670 | 900 | /* pop all elements and return */ |
638f5b90 | 901 | while (!pop_stack(env, NULL, NULL)); |
17a52670 AS |
902 | return NULL; |
903 | } | |
904 | ||
905 | #define CALLER_SAVED_REGS 6 | |
906 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
907 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
908 | }; | |
909 | ||
f54c7898 DB |
910 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
911 | struct bpf_reg_state *reg); | |
f1174f77 | 912 | |
b03c9f9f EC |
913 | /* Mark the unknown part of a register (variable offset or scalar value) as |
914 | * known to have the value @imm. | |
915 | */ | |
916 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
917 | { | |
a9c676bc AS |
918 | /* Clear id, off, and union(map_ptr, range) */ |
919 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
920 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
921 | reg->var_off = tnum_const(imm); |
922 | reg->smin_value = (s64)imm; | |
923 | reg->smax_value = (s64)imm; | |
924 | reg->umin_value = imm; | |
925 | reg->umax_value = imm; | |
926 | } | |
927 | ||
f1174f77 EC |
928 | /* Mark the 'variable offset' part of a register as zero. This should be |
929 | * used only on registers holding a pointer type. | |
930 | */ | |
931 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 932 | { |
b03c9f9f | 933 | __mark_reg_known(reg, 0); |
f1174f77 | 934 | } |
a9789ef9 | 935 | |
cc2b14d5 AS |
936 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
937 | { | |
938 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
939 | reg->type = SCALAR_VALUE; |
940 | } | |
941 | ||
61bd5218 JK |
942 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
943 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
944 | { |
945 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 946 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
947 | /* Something bad happened, let's kill all regs */ |
948 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 949 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
950 | return; |
951 | } | |
952 | __mark_reg_known_zero(regs + regno); | |
953 | } | |
954 | ||
de8f3a83 DB |
955 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
956 | { | |
957 | return type_is_pkt_pointer(reg->type); | |
958 | } | |
959 | ||
960 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
961 | { | |
962 | return reg_is_pkt_pointer(reg) || | |
963 | reg->type == PTR_TO_PACKET_END; | |
964 | } | |
965 | ||
966 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
967 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
968 | enum bpf_reg_type which) | |
969 | { | |
970 | /* The register can already have a range from prior markings. | |
971 | * This is fine as long as it hasn't been advanced from its | |
972 | * origin. | |
973 | */ | |
974 | return reg->type == which && | |
975 | reg->id == 0 && | |
976 | reg->off == 0 && | |
977 | tnum_equals_const(reg->var_off, 0); | |
978 | } | |
979 | ||
b03c9f9f EC |
980 | /* Attempts to improve min/max values based on var_off information */ |
981 | static void __update_reg_bounds(struct bpf_reg_state *reg) | |
982 | { | |
983 | /* min signed is max(sign bit) | min(other bits) */ | |
984 | reg->smin_value = max_t(s64, reg->smin_value, | |
985 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
986 | /* max signed is min(sign bit) | max(other bits) */ | |
987 | reg->smax_value = min_t(s64, reg->smax_value, | |
988 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
989 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
990 | reg->umax_value = min(reg->umax_value, | |
991 | reg->var_off.value | reg->var_off.mask); | |
992 | } | |
993 | ||
994 | /* Uses signed min/max values to inform unsigned, and vice-versa */ | |
995 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) | |
996 | { | |
997 | /* Learn sign from signed bounds. | |
998 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
999 | * are the same, so combine. This works even in the negative case, e.g. | |
1000 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1001 | */ | |
1002 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1003 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1004 | reg->umin_value); | |
1005 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1006 | reg->umax_value); | |
1007 | return; | |
1008 | } | |
1009 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1010 | * boundary, so we must be careful. | |
1011 | */ | |
1012 | if ((s64)reg->umax_value >= 0) { | |
1013 | /* Positive. We can't learn anything from the smin, but smax | |
1014 | * is positive, hence safe. | |
1015 | */ | |
1016 | reg->smin_value = reg->umin_value; | |
1017 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1018 | reg->umax_value); | |
1019 | } else if ((s64)reg->umin_value < 0) { | |
1020 | /* Negative. We can't learn anything from the smax, but smin | |
1021 | * is negative, hence safe. | |
1022 | */ | |
1023 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1024 | reg->umin_value); | |
1025 | reg->smax_value = reg->umax_value; | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | /* Attempts to improve var_off based on unsigned min/max information */ | |
1030 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1031 | { | |
1032 | reg->var_off = tnum_intersect(reg->var_off, | |
1033 | tnum_range(reg->umin_value, | |
1034 | reg->umax_value)); | |
1035 | } | |
1036 | ||
581738a6 YS |
1037 | static void __reg_bound_offset32(struct bpf_reg_state *reg) |
1038 | { | |
1039 | u64 mask = 0xffffFFFF; | |
1040 | struct tnum range = tnum_range(reg->umin_value & mask, | |
1041 | reg->umax_value & mask); | |
1042 | struct tnum lo32 = tnum_cast(reg->var_off, 4); | |
1043 | struct tnum hi32 = tnum_lshift(tnum_rshift(reg->var_off, 32), 32); | |
1044 | ||
1045 | reg->var_off = tnum_or(hi32, tnum_intersect(lo32, range)); | |
1046 | } | |
1047 | ||
b03c9f9f EC |
1048 | /* Reset the min/max bounds of a register */ |
1049 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1050 | { | |
1051 | reg->smin_value = S64_MIN; | |
1052 | reg->smax_value = S64_MAX; | |
1053 | reg->umin_value = 0; | |
1054 | reg->umax_value = U64_MAX; | |
1055 | } | |
1056 | ||
f1174f77 | 1057 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1058 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1059 | struct bpf_reg_state *reg) | |
f1174f77 | 1060 | { |
a9c676bc AS |
1061 | /* |
1062 | * Clear type, id, off, and union(map_ptr, range) and | |
1063 | * padding between 'type' and union | |
1064 | */ | |
1065 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1066 | reg->type = SCALAR_VALUE; |
f1174f77 | 1067 | reg->var_off = tnum_unknown; |
f4d7e40a | 1068 | reg->frameno = 0; |
f54c7898 DB |
1069 | reg->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks ? |
1070 | true : false; | |
b03c9f9f | 1071 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1072 | } |
1073 | ||
61bd5218 JK |
1074 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1075 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1076 | { |
1077 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1078 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1079 | /* Something bad happened, let's kill all regs except FP */ |
1080 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1081 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1082 | return; |
1083 | } | |
f54c7898 | 1084 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1085 | } |
1086 | ||
f54c7898 DB |
1087 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1088 | struct bpf_reg_state *reg) | |
f1174f77 | 1089 | { |
f54c7898 | 1090 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1091 | reg->type = NOT_INIT; |
1092 | } | |
1093 | ||
61bd5218 JK |
1094 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1095 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1096 | { |
1097 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1098 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1099 | /* Something bad happened, let's kill all regs except FP */ |
1100 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1101 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1102 | return; |
1103 | } | |
f54c7898 | 1104 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1105 | } |
1106 | ||
5327ed3d | 1107 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1108 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1109 | struct bpf_func_state *state) |
17a52670 | 1110 | { |
f4d7e40a | 1111 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1112 | int i; |
1113 | ||
dc503a8a | 1114 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1115 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1116 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1117 | regs[i].parent = NULL; |
5327ed3d | 1118 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1119 | } |
17a52670 AS |
1120 | |
1121 | /* frame pointer */ | |
f1174f77 | 1122 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1123 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1124 | regs[BPF_REG_FP].frameno = state->frameno; |
17a52670 AS |
1125 | |
1126 | /* 1st arg to a function */ | |
1127 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
61bd5218 | 1128 | mark_reg_known_zero(env, regs, BPF_REG_1); |
6760bf2d DB |
1129 | } |
1130 | ||
f4d7e40a AS |
1131 | #define BPF_MAIN_FUNC (-1) |
1132 | static void init_func_state(struct bpf_verifier_env *env, | |
1133 | struct bpf_func_state *state, | |
1134 | int callsite, int frameno, int subprogno) | |
1135 | { | |
1136 | state->callsite = callsite; | |
1137 | state->frameno = frameno; | |
1138 | state->subprogno = subprogno; | |
1139 | init_reg_state(env, state); | |
1140 | } | |
1141 | ||
17a52670 AS |
1142 | enum reg_arg_type { |
1143 | SRC_OP, /* register is used as source operand */ | |
1144 | DST_OP, /* register is used as destination operand */ | |
1145 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1146 | }; | |
1147 | ||
cc8b0b92 AS |
1148 | static int cmp_subprogs(const void *a, const void *b) |
1149 | { | |
9c8105bd JW |
1150 | return ((struct bpf_subprog_info *)a)->start - |
1151 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1152 | } |
1153 | ||
1154 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1155 | { | |
9c8105bd | 1156 | struct bpf_subprog_info *p; |
cc8b0b92 | 1157 | |
9c8105bd JW |
1158 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1159 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1160 | if (!p) |
1161 | return -ENOENT; | |
9c8105bd | 1162 | return p - env->subprog_info; |
cc8b0b92 AS |
1163 | |
1164 | } | |
1165 | ||
1166 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1167 | { | |
1168 | int insn_cnt = env->prog->len; | |
1169 | int ret; | |
1170 | ||
1171 | if (off >= insn_cnt || off < 0) { | |
1172 | verbose(env, "call to invalid destination\n"); | |
1173 | return -EINVAL; | |
1174 | } | |
1175 | ret = find_subprog(env, off); | |
1176 | if (ret >= 0) | |
1177 | return 0; | |
4cb3d99c | 1178 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1179 | verbose(env, "too many subprograms\n"); |
1180 | return -E2BIG; | |
1181 | } | |
9c8105bd JW |
1182 | env->subprog_info[env->subprog_cnt++].start = off; |
1183 | sort(env->subprog_info, env->subprog_cnt, | |
1184 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1185 | return 0; |
1186 | } | |
1187 | ||
1188 | static int check_subprogs(struct bpf_verifier_env *env) | |
1189 | { | |
1190 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1191 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1192 | struct bpf_insn *insn = env->prog->insnsi; |
1193 | int insn_cnt = env->prog->len; | |
1194 | ||
f910cefa JW |
1195 | /* Add entry function. */ |
1196 | ret = add_subprog(env, 0); | |
1197 | if (ret < 0) | |
1198 | return ret; | |
1199 | ||
cc8b0b92 AS |
1200 | /* determine subprog starts. The end is one before the next starts */ |
1201 | for (i = 0; i < insn_cnt; i++) { | |
1202 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1203 | continue; | |
1204 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1205 | continue; | |
1206 | if (!env->allow_ptr_leaks) { | |
1207 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); | |
1208 | return -EPERM; | |
1209 | } | |
cc8b0b92 AS |
1210 | ret = add_subprog(env, i + insn[i].imm + 1); |
1211 | if (ret < 0) | |
1212 | return ret; | |
1213 | } | |
1214 | ||
4cb3d99c JW |
1215 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1216 | * logic. 'subprog_cnt' should not be increased. | |
1217 | */ | |
1218 | subprog[env->subprog_cnt].start = insn_cnt; | |
1219 | ||
06ee7115 | 1220 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1221 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1222 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1223 | |
1224 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1225 | subprog_start = subprog[cur_subprog].start; |
1226 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1227 | for (i = 0; i < insn_cnt; i++) { |
1228 | u8 code = insn[i].code; | |
1229 | ||
092ed096 | 1230 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1231 | goto next; |
1232 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1233 | goto next; | |
1234 | off = i + insn[i].off + 1; | |
1235 | if (off < subprog_start || off >= subprog_end) { | |
1236 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1237 | return -EINVAL; | |
1238 | } | |
1239 | next: | |
1240 | if (i == subprog_end - 1) { | |
1241 | /* to avoid fall-through from one subprog into another | |
1242 | * the last insn of the subprog should be either exit | |
1243 | * or unconditional jump back | |
1244 | */ | |
1245 | if (code != (BPF_JMP | BPF_EXIT) && | |
1246 | code != (BPF_JMP | BPF_JA)) { | |
1247 | verbose(env, "last insn is not an exit or jmp\n"); | |
1248 | return -EINVAL; | |
1249 | } | |
1250 | subprog_start = subprog_end; | |
4cb3d99c JW |
1251 | cur_subprog++; |
1252 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1253 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1254 | } |
1255 | } | |
1256 | return 0; | |
1257 | } | |
1258 | ||
679c782d EC |
1259 | /* Parentage chain of this register (or stack slot) should take care of all |
1260 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1261 | */ | |
f4d7e40a | 1262 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 1263 | const struct bpf_reg_state *state, |
5327ed3d | 1264 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
1265 | { |
1266 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1267 | int cnt = 0; |
dc503a8a EC |
1268 | |
1269 | while (parent) { | |
1270 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1271 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1272 | break; |
9242b5f5 AS |
1273 | if (parent->live & REG_LIVE_DONE) { |
1274 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1275 | reg_type_str[parent->type], | |
1276 | parent->var_off.value, parent->off); | |
1277 | return -EFAULT; | |
1278 | } | |
5327ed3d JW |
1279 | /* The first condition is more likely to be true than the |
1280 | * second, checked it first. | |
1281 | */ | |
1282 | if ((parent->live & REG_LIVE_READ) == flag || | |
1283 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
1284 | /* The parentage chain never changes and |
1285 | * this parent was already marked as LIVE_READ. | |
1286 | * There is no need to keep walking the chain again and | |
1287 | * keep re-marking all parents as LIVE_READ. | |
1288 | * This case happens when the same register is read | |
1289 | * multiple times without writes into it in-between. | |
5327ed3d JW |
1290 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
1291 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
1292 | */ |
1293 | break; | |
dc503a8a | 1294 | /* ... then we depend on parent's value */ |
5327ed3d JW |
1295 | parent->live |= flag; |
1296 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
1297 | if (flag == REG_LIVE_READ64) | |
1298 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
1299 | state = parent; |
1300 | parent = state->parent; | |
f4d7e40a | 1301 | writes = true; |
06ee7115 | 1302 | cnt++; |
dc503a8a | 1303 | } |
06ee7115 AS |
1304 | |
1305 | if (env->longest_mark_read_walk < cnt) | |
1306 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1307 | return 0; |
dc503a8a EC |
1308 | } |
1309 | ||
5327ed3d JW |
1310 | /* This function is supposed to be used by the following 32-bit optimization |
1311 | * code only. It returns TRUE if the source or destination register operates | |
1312 | * on 64-bit, otherwise return FALSE. | |
1313 | */ | |
1314 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
1315 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
1316 | { | |
1317 | u8 code, class, op; | |
1318 | ||
1319 | code = insn->code; | |
1320 | class = BPF_CLASS(code); | |
1321 | op = BPF_OP(code); | |
1322 | if (class == BPF_JMP) { | |
1323 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
1324 | * conservatively. | |
1325 | */ | |
1326 | if (op == BPF_EXIT) | |
1327 | return true; | |
1328 | if (op == BPF_CALL) { | |
1329 | /* BPF to BPF call will reach here because of marking | |
1330 | * caller saved clobber with DST_OP_NO_MARK for which we | |
1331 | * don't care the register def because they are anyway | |
1332 | * marked as NOT_INIT already. | |
1333 | */ | |
1334 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1335 | return false; | |
1336 | /* Helper call will reach here because of arg type | |
1337 | * check, conservatively return TRUE. | |
1338 | */ | |
1339 | if (t == SRC_OP) | |
1340 | return true; | |
1341 | ||
1342 | return false; | |
1343 | } | |
1344 | } | |
1345 | ||
1346 | if (class == BPF_ALU64 || class == BPF_JMP || | |
1347 | /* BPF_END always use BPF_ALU class. */ | |
1348 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
1349 | return true; | |
1350 | ||
1351 | if (class == BPF_ALU || class == BPF_JMP32) | |
1352 | return false; | |
1353 | ||
1354 | if (class == BPF_LDX) { | |
1355 | if (t != SRC_OP) | |
1356 | return BPF_SIZE(code) == BPF_DW; | |
1357 | /* LDX source must be ptr. */ | |
1358 | return true; | |
1359 | } | |
1360 | ||
1361 | if (class == BPF_STX) { | |
1362 | if (reg->type != SCALAR_VALUE) | |
1363 | return true; | |
1364 | return BPF_SIZE(code) == BPF_DW; | |
1365 | } | |
1366 | ||
1367 | if (class == BPF_LD) { | |
1368 | u8 mode = BPF_MODE(code); | |
1369 | ||
1370 | /* LD_IMM64 */ | |
1371 | if (mode == BPF_IMM) | |
1372 | return true; | |
1373 | ||
1374 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
1375 | if (t != SRC_OP) | |
1376 | return false; | |
1377 | ||
1378 | /* Implicit ctx ptr. */ | |
1379 | if (regno == BPF_REG_6) | |
1380 | return true; | |
1381 | ||
1382 | /* Explicit source could be any width. */ | |
1383 | return true; | |
1384 | } | |
1385 | ||
1386 | if (class == BPF_ST) | |
1387 | /* The only source register for BPF_ST is a ptr. */ | |
1388 | return true; | |
1389 | ||
1390 | /* Conservatively return true at default. */ | |
1391 | return true; | |
1392 | } | |
1393 | ||
b325fbca JW |
1394 | /* Return TRUE if INSN doesn't have explicit value define. */ |
1395 | static bool insn_no_def(struct bpf_insn *insn) | |
1396 | { | |
1397 | u8 class = BPF_CLASS(insn->code); | |
1398 | ||
1399 | return (class == BPF_JMP || class == BPF_JMP32 || | |
1400 | class == BPF_STX || class == BPF_ST); | |
1401 | } | |
1402 | ||
1403 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
1404 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
1405 | { | |
1406 | if (insn_no_def(insn)) | |
1407 | return false; | |
1408 | ||
1409 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); | |
1410 | } | |
1411 | ||
5327ed3d JW |
1412 | static void mark_insn_zext(struct bpf_verifier_env *env, |
1413 | struct bpf_reg_state *reg) | |
1414 | { | |
1415 | s32 def_idx = reg->subreg_def; | |
1416 | ||
1417 | if (def_idx == DEF_NOT_SUBREG) | |
1418 | return; | |
1419 | ||
1420 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
1421 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
1422 | reg->subreg_def = DEF_NOT_SUBREG; | |
1423 | } | |
1424 | ||
dc503a8a | 1425 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
1426 | enum reg_arg_type t) |
1427 | { | |
f4d7e40a AS |
1428 | struct bpf_verifier_state *vstate = env->cur_state; |
1429 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 1430 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 1431 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 1432 | bool rw64; |
dc503a8a | 1433 | |
17a52670 | 1434 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1435 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1436 | return -EINVAL; |
1437 | } | |
1438 | ||
c342dc10 | 1439 | reg = ®s[regno]; |
5327ed3d | 1440 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
1441 | if (t == SRC_OP) { |
1442 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1443 | if (reg->type == NOT_INIT) { |
61bd5218 | 1444 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1445 | return -EACCES; |
1446 | } | |
679c782d | 1447 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1448 | if (regno == BPF_REG_FP) |
1449 | return 0; | |
1450 | ||
5327ed3d JW |
1451 | if (rw64) |
1452 | mark_insn_zext(env, reg); | |
1453 | ||
1454 | return mark_reg_read(env, reg, reg->parent, | |
1455 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
1456 | } else { |
1457 | /* check whether register used as dest operand can be written to */ | |
1458 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1459 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1460 | return -EACCES; |
1461 | } | |
c342dc10 | 1462 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 1463 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 1464 | if (t == DST_OP) |
61bd5218 | 1465 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1466 | } |
1467 | return 0; | |
1468 | } | |
1469 | ||
b5dc0163 AS |
1470 | /* for any branch, call, exit record the history of jmps in the given state */ |
1471 | static int push_jmp_history(struct bpf_verifier_env *env, | |
1472 | struct bpf_verifier_state *cur) | |
1473 | { | |
1474 | u32 cnt = cur->jmp_history_cnt; | |
1475 | struct bpf_idx_pair *p; | |
1476 | ||
1477 | cnt++; | |
1478 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
1479 | if (!p) | |
1480 | return -ENOMEM; | |
1481 | p[cnt - 1].idx = env->insn_idx; | |
1482 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
1483 | cur->jmp_history = p; | |
1484 | cur->jmp_history_cnt = cnt; | |
1485 | return 0; | |
1486 | } | |
1487 | ||
1488 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
1489 | * history then previous instruction came from straight line execution. | |
1490 | */ | |
1491 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
1492 | u32 *history) | |
1493 | { | |
1494 | u32 cnt = *history; | |
1495 | ||
1496 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
1497 | i = st->jmp_history[cnt - 1].prev_idx; | |
1498 | (*history)--; | |
1499 | } else { | |
1500 | i--; | |
1501 | } | |
1502 | return i; | |
1503 | } | |
1504 | ||
1505 | /* For given verifier state backtrack_insn() is called from the last insn to | |
1506 | * the first insn. Its purpose is to compute a bitmask of registers and | |
1507 | * stack slots that needs precision in the parent verifier state. | |
1508 | */ | |
1509 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
1510 | u32 *reg_mask, u64 *stack_mask) | |
1511 | { | |
1512 | const struct bpf_insn_cbs cbs = { | |
1513 | .cb_print = verbose, | |
1514 | .private_data = env, | |
1515 | }; | |
1516 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
1517 | u8 class = BPF_CLASS(insn->code); | |
1518 | u8 opcode = BPF_OP(insn->code); | |
1519 | u8 mode = BPF_MODE(insn->code); | |
1520 | u32 dreg = 1u << insn->dst_reg; | |
1521 | u32 sreg = 1u << insn->src_reg; | |
1522 | u32 spi; | |
1523 | ||
1524 | if (insn->code == 0) | |
1525 | return 0; | |
1526 | if (env->log.level & BPF_LOG_LEVEL) { | |
1527 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
1528 | verbose(env, "%d: ", idx); | |
1529 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
1530 | } | |
1531 | ||
1532 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1533 | if (!(*reg_mask & dreg)) | |
1534 | return 0; | |
1535 | if (opcode == BPF_MOV) { | |
1536 | if (BPF_SRC(insn->code) == BPF_X) { | |
1537 | /* dreg = sreg | |
1538 | * dreg needs precision after this insn | |
1539 | * sreg needs precision before this insn | |
1540 | */ | |
1541 | *reg_mask &= ~dreg; | |
1542 | *reg_mask |= sreg; | |
1543 | } else { | |
1544 | /* dreg = K | |
1545 | * dreg needs precision after this insn. | |
1546 | * Corresponding register is already marked | |
1547 | * as precise=true in this verifier state. | |
1548 | * No further markings in parent are necessary | |
1549 | */ | |
1550 | *reg_mask &= ~dreg; | |
1551 | } | |
1552 | } else { | |
1553 | if (BPF_SRC(insn->code) == BPF_X) { | |
1554 | /* dreg += sreg | |
1555 | * both dreg and sreg need precision | |
1556 | * before this insn | |
1557 | */ | |
1558 | *reg_mask |= sreg; | |
1559 | } /* else dreg += K | |
1560 | * dreg still needs precision before this insn | |
1561 | */ | |
1562 | } | |
1563 | } else if (class == BPF_LDX) { | |
1564 | if (!(*reg_mask & dreg)) | |
1565 | return 0; | |
1566 | *reg_mask &= ~dreg; | |
1567 | ||
1568 | /* scalars can only be spilled into stack w/o losing precision. | |
1569 | * Load from any other memory can be zero extended. | |
1570 | * The desire to keep that precision is already indicated | |
1571 | * by 'precise' mark in corresponding register of this state. | |
1572 | * No further tracking necessary. | |
1573 | */ | |
1574 | if (insn->src_reg != BPF_REG_FP) | |
1575 | return 0; | |
1576 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1577 | return 0; | |
1578 | ||
1579 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
1580 | * that [fp - off] slot contains scalar that needs to be | |
1581 | * tracked with precision | |
1582 | */ | |
1583 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1584 | if (spi >= 64) { | |
1585 | verbose(env, "BUG spi %d\n", spi); | |
1586 | WARN_ONCE(1, "verifier backtracking bug"); | |
1587 | return -EFAULT; | |
1588 | } | |
1589 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 1590 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 1591 | if (*reg_mask & dreg) |
b3b50f05 | 1592 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
1593 | * to access memory. It means backtracking |
1594 | * encountered a case of pointer subtraction. | |
1595 | */ | |
1596 | return -ENOTSUPP; | |
1597 | /* scalars can only be spilled into stack */ | |
1598 | if (insn->dst_reg != BPF_REG_FP) | |
1599 | return 0; | |
1600 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1601 | return 0; | |
1602 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1603 | if (spi >= 64) { | |
1604 | verbose(env, "BUG spi %d\n", spi); | |
1605 | WARN_ONCE(1, "verifier backtracking bug"); | |
1606 | return -EFAULT; | |
1607 | } | |
1608 | if (!(*stack_mask & (1ull << spi))) | |
1609 | return 0; | |
1610 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
1611 | if (class == BPF_STX) |
1612 | *reg_mask |= sreg; | |
b5dc0163 AS |
1613 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
1614 | if (opcode == BPF_CALL) { | |
1615 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1616 | return -ENOTSUPP; | |
1617 | /* regular helper call sets R0 */ | |
1618 | *reg_mask &= ~1; | |
1619 | if (*reg_mask & 0x3f) { | |
1620 | /* if backtracing was looking for registers R1-R5 | |
1621 | * they should have been found already. | |
1622 | */ | |
1623 | verbose(env, "BUG regs %x\n", *reg_mask); | |
1624 | WARN_ONCE(1, "verifier backtracking bug"); | |
1625 | return -EFAULT; | |
1626 | } | |
1627 | } else if (opcode == BPF_EXIT) { | |
1628 | return -ENOTSUPP; | |
1629 | } | |
1630 | } else if (class == BPF_LD) { | |
1631 | if (!(*reg_mask & dreg)) | |
1632 | return 0; | |
1633 | *reg_mask &= ~dreg; | |
1634 | /* It's ld_imm64 or ld_abs or ld_ind. | |
1635 | * For ld_imm64 no further tracking of precision | |
1636 | * into parent is necessary | |
1637 | */ | |
1638 | if (mode == BPF_IND || mode == BPF_ABS) | |
1639 | /* to be analyzed */ | |
1640 | return -ENOTSUPP; | |
b5dc0163 AS |
1641 | } |
1642 | return 0; | |
1643 | } | |
1644 | ||
1645 | /* the scalar precision tracking algorithm: | |
1646 | * . at the start all registers have precise=false. | |
1647 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
1648 | * . once precise value of the scalar register is used in: | |
1649 | * . ptr + scalar alu | |
1650 | * . if (scalar cond K|scalar) | |
1651 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
1652 | * backtrack through the verifier states and mark all registers and | |
1653 | * stack slots with spilled constants that these scalar regisers | |
1654 | * should be precise. | |
1655 | * . during state pruning two registers (or spilled stack slots) | |
1656 | * are equivalent if both are not precise. | |
1657 | * | |
1658 | * Note the verifier cannot simply walk register parentage chain, | |
1659 | * since many different registers and stack slots could have been | |
1660 | * used to compute single precise scalar. | |
1661 | * | |
1662 | * The approach of starting with precise=true for all registers and then | |
1663 | * backtrack to mark a register as not precise when the verifier detects | |
1664 | * that program doesn't care about specific value (e.g., when helper | |
1665 | * takes register as ARG_ANYTHING parameter) is not safe. | |
1666 | * | |
1667 | * It's ok to walk single parentage chain of the verifier states. | |
1668 | * It's possible that this backtracking will go all the way till 1st insn. | |
1669 | * All other branches will be explored for needing precision later. | |
1670 | * | |
1671 | * The backtracking needs to deal with cases like: | |
1672 | * 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) | |
1673 | * r9 -= r8 | |
1674 | * r5 = r9 | |
1675 | * if r5 > 0x79f goto pc+7 | |
1676 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
1677 | * r5 += 1 | |
1678 | * ... | |
1679 | * call bpf_perf_event_output#25 | |
1680 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
1681 | * | |
1682 | * and this case: | |
1683 | * r6 = 1 | |
1684 | * call foo // uses callee's r6 inside to compute r0 | |
1685 | * r0 += r6 | |
1686 | * if r0 == 0 goto | |
1687 | * | |
1688 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
1689 | * | |
1690 | * Also if parent's curframe > frame where backtracking started, | |
1691 | * the verifier need to mark registers in both frames, otherwise callees | |
1692 | * may incorrectly prune callers. This is similar to | |
1693 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
1694 | * | |
1695 | * For now backtracking falls back into conservative marking. | |
1696 | */ | |
1697 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
1698 | struct bpf_verifier_state *st) | |
1699 | { | |
1700 | struct bpf_func_state *func; | |
1701 | struct bpf_reg_state *reg; | |
1702 | int i, j; | |
1703 | ||
1704 | /* big hammer: mark all scalars precise in this path. | |
1705 | * pop_stack may still get !precise scalars. | |
1706 | */ | |
1707 | for (; st; st = st->parent) | |
1708 | for (i = 0; i <= st->curframe; i++) { | |
1709 | func = st->frame[i]; | |
1710 | for (j = 0; j < BPF_REG_FP; j++) { | |
1711 | reg = &func->regs[j]; | |
1712 | if (reg->type != SCALAR_VALUE) | |
1713 | continue; | |
1714 | reg->precise = true; | |
1715 | } | |
1716 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
1717 | if (func->stack[j].slot_type[0] != STACK_SPILL) | |
1718 | continue; | |
1719 | reg = &func->stack[j].spilled_ptr; | |
1720 | if (reg->type != SCALAR_VALUE) | |
1721 | continue; | |
1722 | reg->precise = true; | |
1723 | } | |
1724 | } | |
1725 | } | |
1726 | ||
a3ce685d AS |
1727 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
1728 | int spi) | |
b5dc0163 AS |
1729 | { |
1730 | struct bpf_verifier_state *st = env->cur_state; | |
1731 | int first_idx = st->first_insn_idx; | |
1732 | int last_idx = env->insn_idx; | |
1733 | struct bpf_func_state *func; | |
1734 | struct bpf_reg_state *reg; | |
a3ce685d AS |
1735 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
1736 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 1737 | bool skip_first = true; |
a3ce685d | 1738 | bool new_marks = false; |
b5dc0163 AS |
1739 | int i, err; |
1740 | ||
1741 | if (!env->allow_ptr_leaks) | |
1742 | /* backtracking is root only for now */ | |
1743 | return 0; | |
1744 | ||
1745 | func = st->frame[st->curframe]; | |
a3ce685d AS |
1746 | if (regno >= 0) { |
1747 | reg = &func->regs[regno]; | |
1748 | if (reg->type != SCALAR_VALUE) { | |
1749 | WARN_ONCE(1, "backtracing misuse"); | |
1750 | return -EFAULT; | |
1751 | } | |
1752 | if (!reg->precise) | |
1753 | new_marks = true; | |
1754 | else | |
1755 | reg_mask = 0; | |
1756 | reg->precise = true; | |
b5dc0163 | 1757 | } |
b5dc0163 | 1758 | |
a3ce685d AS |
1759 | while (spi >= 0) { |
1760 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { | |
1761 | stack_mask = 0; | |
1762 | break; | |
1763 | } | |
1764 | reg = &func->stack[spi].spilled_ptr; | |
1765 | if (reg->type != SCALAR_VALUE) { | |
1766 | stack_mask = 0; | |
1767 | break; | |
1768 | } | |
1769 | if (!reg->precise) | |
1770 | new_marks = true; | |
1771 | else | |
1772 | stack_mask = 0; | |
1773 | reg->precise = true; | |
1774 | break; | |
1775 | } | |
1776 | ||
1777 | if (!new_marks) | |
1778 | return 0; | |
1779 | if (!reg_mask && !stack_mask) | |
1780 | return 0; | |
b5dc0163 AS |
1781 | for (;;) { |
1782 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
1783 | u32 history = st->jmp_history_cnt; |
1784 | ||
1785 | if (env->log.level & BPF_LOG_LEVEL) | |
1786 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
1787 | for (i = last_idx;;) { | |
1788 | if (skip_first) { | |
1789 | err = 0; | |
1790 | skip_first = false; | |
1791 | } else { | |
1792 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
1793 | } | |
1794 | if (err == -ENOTSUPP) { | |
1795 | mark_all_scalars_precise(env, st); | |
1796 | return 0; | |
1797 | } else if (err) { | |
1798 | return err; | |
1799 | } | |
1800 | if (!reg_mask && !stack_mask) | |
1801 | /* Found assignment(s) into tracked register in this state. | |
1802 | * Since this state is already marked, just return. | |
1803 | * Nothing to be tracked further in the parent state. | |
1804 | */ | |
1805 | return 0; | |
1806 | if (i == first_idx) | |
1807 | break; | |
1808 | i = get_prev_insn_idx(st, i, &history); | |
1809 | if (i >= env->prog->len) { | |
1810 | /* This can happen if backtracking reached insn 0 | |
1811 | * and there are still reg_mask or stack_mask | |
1812 | * to backtrack. | |
1813 | * It means the backtracking missed the spot where | |
1814 | * particular register was initialized with a constant. | |
1815 | */ | |
1816 | verbose(env, "BUG backtracking idx %d\n", i); | |
1817 | WARN_ONCE(1, "verifier backtracking bug"); | |
1818 | return -EFAULT; | |
1819 | } | |
1820 | } | |
1821 | st = st->parent; | |
1822 | if (!st) | |
1823 | break; | |
1824 | ||
a3ce685d | 1825 | new_marks = false; |
b5dc0163 AS |
1826 | func = st->frame[st->curframe]; |
1827 | bitmap_from_u64(mask, reg_mask); | |
1828 | for_each_set_bit(i, mask, 32) { | |
1829 | reg = &func->regs[i]; | |
a3ce685d AS |
1830 | if (reg->type != SCALAR_VALUE) { |
1831 | reg_mask &= ~(1u << i); | |
b5dc0163 | 1832 | continue; |
a3ce685d | 1833 | } |
b5dc0163 AS |
1834 | if (!reg->precise) |
1835 | new_marks = true; | |
1836 | reg->precise = true; | |
1837 | } | |
1838 | ||
1839 | bitmap_from_u64(mask, stack_mask); | |
1840 | for_each_set_bit(i, mask, 64) { | |
1841 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
1842 | /* the sequence of instructions: |
1843 | * 2: (bf) r3 = r10 | |
1844 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
1845 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
1846 | * doesn't contain jmps. It's backtracked | |
1847 | * as a single block. | |
1848 | * During backtracking insn 3 is not recognized as | |
1849 | * stack access, so at the end of backtracking | |
1850 | * stack slot fp-8 is still marked in stack_mask. | |
1851 | * However the parent state may not have accessed | |
1852 | * fp-8 and it's "unallocated" stack space. | |
1853 | * In such case fallback to conservative. | |
b5dc0163 | 1854 | */ |
2339cd6c AS |
1855 | mark_all_scalars_precise(env, st); |
1856 | return 0; | |
b5dc0163 AS |
1857 | } |
1858 | ||
a3ce685d AS |
1859 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
1860 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 1861 | continue; |
a3ce685d | 1862 | } |
b5dc0163 | 1863 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
1864 | if (reg->type != SCALAR_VALUE) { |
1865 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 1866 | continue; |
a3ce685d | 1867 | } |
b5dc0163 AS |
1868 | if (!reg->precise) |
1869 | new_marks = true; | |
1870 | reg->precise = true; | |
1871 | } | |
1872 | if (env->log.level & BPF_LOG_LEVEL) { | |
1873 | print_verifier_state(env, func); | |
1874 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
1875 | new_marks ? "didn't have" : "already had", | |
1876 | reg_mask, stack_mask); | |
1877 | } | |
1878 | ||
a3ce685d AS |
1879 | if (!reg_mask && !stack_mask) |
1880 | break; | |
b5dc0163 AS |
1881 | if (!new_marks) |
1882 | break; | |
1883 | ||
1884 | last_idx = st->last_insn_idx; | |
1885 | first_idx = st->first_insn_idx; | |
1886 | } | |
1887 | return 0; | |
1888 | } | |
1889 | ||
a3ce685d AS |
1890 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
1891 | { | |
1892 | return __mark_chain_precision(env, regno, -1); | |
1893 | } | |
1894 | ||
1895 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
1896 | { | |
1897 | return __mark_chain_precision(env, -1, spi); | |
1898 | } | |
b5dc0163 | 1899 | |
1be7f75d AS |
1900 | static bool is_spillable_regtype(enum bpf_reg_type type) |
1901 | { | |
1902 | switch (type) { | |
1903 | case PTR_TO_MAP_VALUE: | |
1904 | case PTR_TO_MAP_VALUE_OR_NULL: | |
1905 | case PTR_TO_STACK: | |
1906 | case PTR_TO_CTX: | |
969bf05e | 1907 | case PTR_TO_PACKET: |
de8f3a83 | 1908 | case PTR_TO_PACKET_META: |
969bf05e | 1909 | case PTR_TO_PACKET_END: |
d58e468b | 1910 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 1911 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
1912 | case PTR_TO_SOCKET: |
1913 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
1914 | case PTR_TO_SOCK_COMMON: |
1915 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
1916 | case PTR_TO_TCP_SOCK: |
1917 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 1918 | case PTR_TO_XDP_SOCK: |
65726b5b | 1919 | case PTR_TO_BTF_ID: |
1be7f75d AS |
1920 | return true; |
1921 | default: | |
1922 | return false; | |
1923 | } | |
1924 | } | |
1925 | ||
cc2b14d5 AS |
1926 | /* Does this register contain a constant zero? */ |
1927 | static bool register_is_null(struct bpf_reg_state *reg) | |
1928 | { | |
1929 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
1930 | } | |
1931 | ||
f7cf25b2 AS |
1932 | static bool register_is_const(struct bpf_reg_state *reg) |
1933 | { | |
1934 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
1935 | } | |
1936 | ||
1937 | static void save_register_state(struct bpf_func_state *state, | |
1938 | int spi, struct bpf_reg_state *reg) | |
1939 | { | |
1940 | int i; | |
1941 | ||
1942 | state->stack[spi].spilled_ptr = *reg; | |
1943 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1944 | ||
1945 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1946 | state->stack[spi].slot_type[i] = STACK_SPILL; | |
1947 | } | |
1948 | ||
17a52670 AS |
1949 | /* check_stack_read/write functions track spill/fill of registers, |
1950 | * stack boundary and alignment are checked in check_mem_access() | |
1951 | */ | |
61bd5218 | 1952 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 1953 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 1954 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 1955 | { |
f4d7e40a | 1956 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 1957 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 1958 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 1959 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 1960 | |
f4d7e40a | 1961 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 1962 | state->acquired_refs, true); |
638f5b90 AS |
1963 | if (err) |
1964 | return err; | |
9c399760 AS |
1965 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
1966 | * so it's aligned access and [off, off + size) are within stack limits | |
1967 | */ | |
638f5b90 AS |
1968 | if (!env->allow_ptr_leaks && |
1969 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
1970 | size != BPF_REG_SIZE) { | |
1971 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
1972 | return -EACCES; | |
1973 | } | |
17a52670 | 1974 | |
f4d7e40a | 1975 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
1976 | if (value_regno >= 0) |
1977 | reg = &cur->regs[value_regno]; | |
17a52670 | 1978 | |
f7cf25b2 AS |
1979 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
1980 | !register_is_null(reg) && env->allow_ptr_leaks) { | |
b5dc0163 AS |
1981 | if (dst_reg != BPF_REG_FP) { |
1982 | /* The backtracking logic can only recognize explicit | |
1983 | * stack slot address like [fp - 8]. Other spill of | |
1984 | * scalar via different register has to be conervative. | |
1985 | * Backtrack from here and mark all registers as precise | |
1986 | * that contributed into 'reg' being a constant. | |
1987 | */ | |
1988 | err = mark_chain_precision(env, value_regno); | |
1989 | if (err) | |
1990 | return err; | |
1991 | } | |
f7cf25b2 AS |
1992 | save_register_state(state, spi, reg); |
1993 | } else if (reg && is_spillable_regtype(reg->type)) { | |
17a52670 | 1994 | /* register containing pointer is being spilled into stack */ |
9c399760 | 1995 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 1996 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 1997 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1998 | return -EACCES; |
1999 | } | |
2000 | ||
f7cf25b2 | 2001 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2002 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2003 | return -EINVAL; | |
2004 | } | |
2005 | ||
f7cf25b2 AS |
2006 | if (!env->allow_ptr_leaks) { |
2007 | bool sanitize = false; | |
17a52670 | 2008 | |
f7cf25b2 AS |
2009 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
2010 | register_is_const(&state->stack[spi].spilled_ptr)) | |
2011 | sanitize = true; | |
2012 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2013 | if (state->stack[spi].slot_type[i] == STACK_MISC) { | |
2014 | sanitize = true; | |
2015 | break; | |
2016 | } | |
2017 | if (sanitize) { | |
af86ca4e AS |
2018 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
2019 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
2020 | ||
2021 | /* detected reuse of integer stack slot with a pointer | |
2022 | * which means either llvm is reusing stack slot or | |
2023 | * an attacker is trying to exploit CVE-2018-3639 | |
2024 | * (speculative store bypass) | |
2025 | * Have to sanitize that slot with preemptive | |
2026 | * store of zero. | |
2027 | */ | |
2028 | if (*poff && *poff != soff) { | |
2029 | /* disallow programs where single insn stores | |
2030 | * into two different stack slots, since verifier | |
2031 | * cannot sanitize them | |
2032 | */ | |
2033 | verbose(env, | |
2034 | "insn %d cannot access two stack slots fp%d and fp%d", | |
2035 | insn_idx, *poff, soff); | |
2036 | return -EINVAL; | |
2037 | } | |
2038 | *poff = soff; | |
2039 | } | |
af86ca4e | 2040 | } |
f7cf25b2 | 2041 | save_register_state(state, spi, reg); |
9c399760 | 2042 | } else { |
cc2b14d5 AS |
2043 | u8 type = STACK_MISC; |
2044 | ||
679c782d EC |
2045 | /* regular write of data into stack destroys any spilled ptr */ |
2046 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
2047 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
2048 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
2049 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2050 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 2051 | |
cc2b14d5 AS |
2052 | /* only mark the slot as written if all 8 bytes were written |
2053 | * otherwise read propagation may incorrectly stop too soon | |
2054 | * when stack slots are partially written. | |
2055 | * This heuristic means that read propagation will be | |
2056 | * conservative, since it will add reg_live_read marks | |
2057 | * to stack slots all the way to first state when programs | |
2058 | * writes+reads less than 8 bytes | |
2059 | */ | |
2060 | if (size == BPF_REG_SIZE) | |
2061 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2062 | ||
2063 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2064 | if (reg && register_is_null(reg)) { |
2065 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2066 | err = mark_chain_precision(env, value_regno); | |
2067 | if (err) | |
2068 | return err; | |
cc2b14d5 | 2069 | type = STACK_ZERO; |
b5dc0163 | 2070 | } |
cc2b14d5 | 2071 | |
0bae2d4d | 2072 | /* Mark slots affected by this stack write. */ |
9c399760 | 2073 | for (i = 0; i < size; i++) |
638f5b90 | 2074 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2075 | type; |
17a52670 AS |
2076 | } |
2077 | return 0; | |
2078 | } | |
2079 | ||
61bd5218 | 2080 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
2081 | struct bpf_func_state *reg_state /* func where register points to */, |
2082 | int off, int size, int value_regno) | |
17a52670 | 2083 | { |
f4d7e40a AS |
2084 | struct bpf_verifier_state *vstate = env->cur_state; |
2085 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 2086 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 2087 | struct bpf_reg_state *reg; |
638f5b90 | 2088 | u8 *stype; |
17a52670 | 2089 | |
f4d7e40a | 2090 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
2091 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
2092 | off, size); | |
2093 | return -EACCES; | |
2094 | } | |
f4d7e40a | 2095 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 2096 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 2097 | |
638f5b90 | 2098 | if (stype[0] == STACK_SPILL) { |
9c399760 | 2099 | if (size != BPF_REG_SIZE) { |
f7cf25b2 AS |
2100 | if (reg->type != SCALAR_VALUE) { |
2101 | verbose_linfo(env, env->insn_idx, "; "); | |
2102 | verbose(env, "invalid size of register fill\n"); | |
2103 | return -EACCES; | |
2104 | } | |
2105 | if (value_regno >= 0) { | |
2106 | mark_reg_unknown(env, state->regs, value_regno); | |
2107 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
2108 | } | |
2109 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
2110 | return 0; | |
17a52670 | 2111 | } |
9c399760 | 2112 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 2113 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 2114 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
2115 | return -EACCES; |
2116 | } | |
2117 | } | |
2118 | ||
dc503a8a | 2119 | if (value_regno >= 0) { |
17a52670 | 2120 | /* restore register state from stack */ |
f7cf25b2 | 2121 | state->regs[value_regno] = *reg; |
2f18f62e AS |
2122 | /* mark reg as written since spilled pointer state likely |
2123 | * has its liveness marks cleared by is_state_visited() | |
2124 | * which resets stack/reg liveness for state transitions | |
2125 | */ | |
2126 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
dc503a8a | 2127 | } |
f7cf25b2 | 2128 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 | 2129 | } else { |
cc2b14d5 AS |
2130 | int zeros = 0; |
2131 | ||
17a52670 | 2132 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
2133 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
2134 | continue; | |
2135 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
2136 | zeros++; | |
2137 | continue; | |
17a52670 | 2138 | } |
cc2b14d5 AS |
2139 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
2140 | off, i, size); | |
2141 | return -EACCES; | |
2142 | } | |
f7cf25b2 | 2143 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
cc2b14d5 AS |
2144 | if (value_regno >= 0) { |
2145 | if (zeros == size) { | |
2146 | /* any size read into register is zero extended, | |
2147 | * so the whole register == const_zero | |
2148 | */ | |
2149 | __mark_reg_const_zero(&state->regs[value_regno]); | |
b5dc0163 AS |
2150 | /* backtracking doesn't support STACK_ZERO yet, |
2151 | * so mark it precise here, so that later | |
2152 | * backtracking can stop here. | |
2153 | * Backtracking may not need this if this register | |
2154 | * doesn't participate in pointer adjustment. | |
2155 | * Forward propagation of precise flag is not | |
2156 | * necessary either. This mark is only to stop | |
2157 | * backtracking. Any register that contributed | |
2158 | * to const 0 was marked precise before spill. | |
2159 | */ | |
2160 | state->regs[value_regno].precise = true; | |
cc2b14d5 AS |
2161 | } else { |
2162 | /* have read misc data from the stack */ | |
2163 | mark_reg_unknown(env, state->regs, value_regno); | |
2164 | } | |
2165 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 2166 | } |
17a52670 | 2167 | } |
f7cf25b2 | 2168 | return 0; |
17a52670 AS |
2169 | } |
2170 | ||
e4298d25 DB |
2171 | static int check_stack_access(struct bpf_verifier_env *env, |
2172 | const struct bpf_reg_state *reg, | |
2173 | int off, int size) | |
2174 | { | |
2175 | /* Stack accesses must be at a fixed offset, so that we | |
2176 | * can determine what type of data were returned. See | |
2177 | * check_stack_read(). | |
2178 | */ | |
2179 | if (!tnum_is_const(reg->var_off)) { | |
2180 | char tn_buf[48]; | |
2181 | ||
2182 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 2183 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
2184 | tn_buf, off, size); |
2185 | return -EACCES; | |
2186 | } | |
2187 | ||
2188 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
2189 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
2190 | return -EACCES; | |
2191 | } | |
2192 | ||
2193 | return 0; | |
2194 | } | |
2195 | ||
591fe988 DB |
2196 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
2197 | int off, int size, enum bpf_access_type type) | |
2198 | { | |
2199 | struct bpf_reg_state *regs = cur_regs(env); | |
2200 | struct bpf_map *map = regs[regno].map_ptr; | |
2201 | u32 cap = bpf_map_flags_to_cap(map); | |
2202 | ||
2203 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
2204 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
2205 | map->value_size, off, size); | |
2206 | return -EACCES; | |
2207 | } | |
2208 | ||
2209 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
2210 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
2211 | map->value_size, off, size); | |
2212 | return -EACCES; | |
2213 | } | |
2214 | ||
2215 | return 0; | |
2216 | } | |
2217 | ||
17a52670 | 2218 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
f1174f77 | 2219 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2220 | int size, bool zero_size_allowed) |
17a52670 | 2221 | { |
638f5b90 AS |
2222 | struct bpf_reg_state *regs = cur_regs(env); |
2223 | struct bpf_map *map = regs[regno].map_ptr; | |
17a52670 | 2224 | |
9fd29c08 YS |
2225 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
2226 | off + size > map->value_size) { | |
61bd5218 | 2227 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
17a52670 AS |
2228 | map->value_size, off, size); |
2229 | return -EACCES; | |
2230 | } | |
2231 | return 0; | |
2232 | } | |
2233 | ||
f1174f77 EC |
2234 | /* check read/write into a map element with possible variable offset */ |
2235 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
9fd29c08 | 2236 | int off, int size, bool zero_size_allowed) |
dbcfe5f7 | 2237 | { |
f4d7e40a AS |
2238 | struct bpf_verifier_state *vstate = env->cur_state; |
2239 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
2240 | struct bpf_reg_state *reg = &state->regs[regno]; |
2241 | int err; | |
2242 | ||
f1174f77 EC |
2243 | /* We may have adjusted the register to this map value, so we |
2244 | * need to try adding each of min_value and max_value to off | |
2245 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 2246 | */ |
06ee7115 | 2247 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 2248 | print_verifier_state(env, state); |
b7137c4e | 2249 | |
dbcfe5f7 GB |
2250 | /* The minimum value is only important with signed |
2251 | * comparisons where we can't assume the floor of a | |
2252 | * value is 0. If we are using signed variables for our | |
2253 | * index'es we need to make sure that whatever we use | |
2254 | * will have a set floor within our range. | |
2255 | */ | |
b7137c4e DB |
2256 | if (reg->smin_value < 0 && |
2257 | (reg->smin_value == S64_MIN || | |
2258 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
2259 | reg->smin_value + off < 0)) { | |
61bd5218 | 2260 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
2261 | regno); |
2262 | return -EACCES; | |
2263 | } | |
9fd29c08 YS |
2264 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
2265 | zero_size_allowed); | |
dbcfe5f7 | 2266 | if (err) { |
61bd5218 JK |
2267 | verbose(env, "R%d min value is outside of the array range\n", |
2268 | regno); | |
dbcfe5f7 GB |
2269 | return err; |
2270 | } | |
2271 | ||
b03c9f9f EC |
2272 | /* If we haven't set a max value then we need to bail since we can't be |
2273 | * sure we won't do bad things. | |
2274 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 2275 | */ |
b03c9f9f | 2276 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
61bd5218 | 2277 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
dbcfe5f7 GB |
2278 | regno); |
2279 | return -EACCES; | |
2280 | } | |
9fd29c08 YS |
2281 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
2282 | zero_size_allowed); | |
f1174f77 | 2283 | if (err) |
61bd5218 JK |
2284 | verbose(env, "R%d max value is outside of the array range\n", |
2285 | regno); | |
d83525ca AS |
2286 | |
2287 | if (map_value_has_spin_lock(reg->map_ptr)) { | |
2288 | u32 lock = reg->map_ptr->spin_lock_off; | |
2289 | ||
2290 | /* if any part of struct bpf_spin_lock can be touched by | |
2291 | * load/store reject this program. | |
2292 | * To check that [x1, x2) overlaps with [y1, y2) | |
2293 | * it is sufficient to check x1 < y2 && y1 < x2. | |
2294 | */ | |
2295 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
2296 | lock < reg->umax_value + off + size) { | |
2297 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
2298 | return -EACCES; | |
2299 | } | |
2300 | } | |
f1174f77 | 2301 | return err; |
dbcfe5f7 GB |
2302 | } |
2303 | ||
969bf05e AS |
2304 | #define MAX_PACKET_OFF 0xffff |
2305 | ||
58e2af8b | 2306 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
2307 | const struct bpf_call_arg_meta *meta, |
2308 | enum bpf_access_type t) | |
4acf6c0b | 2309 | { |
36bbef52 | 2310 | switch (env->prog->type) { |
5d66fa7d | 2311 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
2312 | case BPF_PROG_TYPE_LWT_IN: |
2313 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 2314 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 2315 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 2316 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 2317 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
2318 | if (t == BPF_WRITE) |
2319 | return false; | |
7e57fbb2 | 2320 | /* fallthrough */ |
5d66fa7d DB |
2321 | |
2322 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
2323 | case BPF_PROG_TYPE_SCHED_CLS: |
2324 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 2325 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 2326 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 2327 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 2328 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
2329 | if (meta) |
2330 | return meta->pkt_access; | |
2331 | ||
2332 | env->seen_direct_write = true; | |
4acf6c0b | 2333 | return true; |
0d01da6a SF |
2334 | |
2335 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2336 | if (t == BPF_WRITE) | |
2337 | env->seen_direct_write = true; | |
2338 | ||
2339 | return true; | |
2340 | ||
4acf6c0b BB |
2341 | default: |
2342 | return false; | |
2343 | } | |
2344 | } | |
2345 | ||
f1174f77 | 2346 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
9fd29c08 | 2347 | int off, int size, bool zero_size_allowed) |
969bf05e | 2348 | { |
638f5b90 | 2349 | struct bpf_reg_state *regs = cur_regs(env); |
58e2af8b | 2350 | struct bpf_reg_state *reg = ®s[regno]; |
969bf05e | 2351 | |
9fd29c08 YS |
2352 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
2353 | (u64)off + size > reg->range) { | |
61bd5218 | 2354 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
d91b28ed | 2355 | off, size, regno, reg->id, reg->off, reg->range); |
969bf05e AS |
2356 | return -EACCES; |
2357 | } | |
2358 | return 0; | |
2359 | } | |
2360 | ||
f1174f77 | 2361 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2362 | int size, bool zero_size_allowed) |
f1174f77 | 2363 | { |
638f5b90 | 2364 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
2365 | struct bpf_reg_state *reg = ®s[regno]; |
2366 | int err; | |
2367 | ||
2368 | /* We may have added a variable offset to the packet pointer; but any | |
2369 | * reg->range we have comes after that. We are only checking the fixed | |
2370 | * offset. | |
2371 | */ | |
2372 | ||
2373 | /* We don't allow negative numbers, because we aren't tracking enough | |
2374 | * detail to prove they're safe. | |
2375 | */ | |
b03c9f9f | 2376 | if (reg->smin_value < 0) { |
61bd5218 | 2377 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
2378 | regno); |
2379 | return -EACCES; | |
2380 | } | |
9fd29c08 | 2381 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
f1174f77 | 2382 | if (err) { |
61bd5218 | 2383 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
2384 | return err; |
2385 | } | |
e647815a JW |
2386 | |
2387 | /* __check_packet_access has made sure "off + size - 1" is within u16. | |
2388 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, | |
2389 | * otherwise find_good_pkt_pointers would have refused to set range info | |
2390 | * that __check_packet_access would have rejected this pkt access. | |
2391 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. | |
2392 | */ | |
2393 | env->prog->aux->max_pkt_offset = | |
2394 | max_t(u32, env->prog->aux->max_pkt_offset, | |
2395 | off + reg->umax_value + size - 1); | |
2396 | ||
f1174f77 EC |
2397 | return err; |
2398 | } | |
2399 | ||
2400 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 2401 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 AS |
2402 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
2403 | u32 *btf_id) | |
17a52670 | 2404 | { |
f96da094 DB |
2405 | struct bpf_insn_access_aux info = { |
2406 | .reg_type = *reg_type, | |
9e15db66 | 2407 | .log = &env->log, |
f96da094 | 2408 | }; |
31fd8581 | 2409 | |
4f9218aa | 2410 | if (env->ops->is_valid_access && |
5e43f899 | 2411 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
2412 | /* A non zero info.ctx_field_size indicates that this field is a |
2413 | * candidate for later verifier transformation to load the whole | |
2414 | * field and then apply a mask when accessed with a narrower | |
2415 | * access than actual ctx access size. A zero info.ctx_field_size | |
2416 | * will only allow for whole field access and rejects any other | |
2417 | * type of narrower access. | |
31fd8581 | 2418 | */ |
23994631 | 2419 | *reg_type = info.reg_type; |
31fd8581 | 2420 | |
9e15db66 AS |
2421 | if (*reg_type == PTR_TO_BTF_ID) |
2422 | *btf_id = info.btf_id; | |
2423 | else | |
2424 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; | |
32bbe007 AS |
2425 | /* remember the offset of last byte accessed in ctx */ |
2426 | if (env->prog->aux->max_ctx_offset < off + size) | |
2427 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 2428 | return 0; |
32bbe007 | 2429 | } |
17a52670 | 2430 | |
61bd5218 | 2431 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
2432 | return -EACCES; |
2433 | } | |
2434 | ||
d58e468b PP |
2435 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
2436 | int size) | |
2437 | { | |
2438 | if (size < 0 || off < 0 || | |
2439 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
2440 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
2441 | off, size); | |
2442 | return -EACCES; | |
2443 | } | |
2444 | return 0; | |
2445 | } | |
2446 | ||
5f456649 MKL |
2447 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
2448 | u32 regno, int off, int size, | |
2449 | enum bpf_access_type t) | |
c64b7983 JS |
2450 | { |
2451 | struct bpf_reg_state *regs = cur_regs(env); | |
2452 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 2453 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 2454 | bool valid; |
c64b7983 JS |
2455 | |
2456 | if (reg->smin_value < 0) { | |
2457 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
2458 | regno); | |
2459 | return -EACCES; | |
2460 | } | |
2461 | ||
46f8bc92 MKL |
2462 | switch (reg->type) { |
2463 | case PTR_TO_SOCK_COMMON: | |
2464 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
2465 | break; | |
2466 | case PTR_TO_SOCKET: | |
2467 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
2468 | break; | |
655a51e5 MKL |
2469 | case PTR_TO_TCP_SOCK: |
2470 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
2471 | break; | |
fada7fdc JL |
2472 | case PTR_TO_XDP_SOCK: |
2473 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
2474 | break; | |
46f8bc92 MKL |
2475 | default: |
2476 | valid = false; | |
c64b7983 JS |
2477 | } |
2478 | ||
5f456649 | 2479 | |
46f8bc92 MKL |
2480 | if (valid) { |
2481 | env->insn_aux_data[insn_idx].ctx_field_size = | |
2482 | info.ctx_field_size; | |
2483 | return 0; | |
2484 | } | |
2485 | ||
2486 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
2487 | regno, reg_type_str[reg->type], off, size); | |
2488 | ||
2489 | return -EACCES; | |
c64b7983 JS |
2490 | } |
2491 | ||
4cabc5b1 DB |
2492 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2493 | const struct bpf_reg_state *reg) | |
1be7f75d | 2494 | { |
4cabc5b1 | 2495 | if (allow_ptr_leaks) |
1be7f75d AS |
2496 | return false; |
2497 | ||
f1174f77 | 2498 | return reg->type != SCALAR_VALUE; |
1be7f75d AS |
2499 | } |
2500 | ||
2a159c6f DB |
2501 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
2502 | { | |
2503 | return cur_regs(env) + regno; | |
2504 | } | |
2505 | ||
4cabc5b1 DB |
2506 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
2507 | { | |
2a159c6f | 2508 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
2509 | } |
2510 | ||
f37a8cb8 DB |
2511 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
2512 | { | |
2a159c6f | 2513 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 2514 | |
46f8bc92 MKL |
2515 | return reg->type == PTR_TO_CTX; |
2516 | } | |
2517 | ||
2518 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
2519 | { | |
2520 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2521 | ||
2522 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
2523 | } |
2524 | ||
ca369602 DB |
2525 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
2526 | { | |
2a159c6f | 2527 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
2528 | |
2529 | return type_is_pkt_pointer(reg->type); | |
2530 | } | |
2531 | ||
4b5defde DB |
2532 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
2533 | { | |
2534 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2535 | ||
2536 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
2537 | return reg->type == PTR_TO_FLOW_KEYS; | |
2538 | } | |
2539 | ||
61bd5218 JK |
2540 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
2541 | const struct bpf_reg_state *reg, | |
d1174416 | 2542 | int off, int size, bool strict) |
969bf05e | 2543 | { |
f1174f77 | 2544 | struct tnum reg_off; |
e07b98d9 | 2545 | int ip_align; |
d1174416 DM |
2546 | |
2547 | /* Byte size accesses are always allowed. */ | |
2548 | if (!strict || size == 1) | |
2549 | return 0; | |
2550 | ||
e4eda884 DM |
2551 | /* For platforms that do not have a Kconfig enabling |
2552 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
2553 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
2554 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
2555 | * to this code only in strict mode where we want to emulate | |
2556 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
2557 | * unconditional IP align value of '2'. | |
e07b98d9 | 2558 | */ |
e4eda884 | 2559 | ip_align = 2; |
f1174f77 EC |
2560 | |
2561 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
2562 | if (!tnum_is_aligned(reg_off, size)) { | |
2563 | char tn_buf[48]; | |
2564 | ||
2565 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
2566 | verbose(env, |
2567 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 2568 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
2569 | return -EACCES; |
2570 | } | |
79adffcd | 2571 | |
969bf05e AS |
2572 | return 0; |
2573 | } | |
2574 | ||
61bd5218 JK |
2575 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
2576 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
2577 | const char *pointer_desc, |
2578 | int off, int size, bool strict) | |
79adffcd | 2579 | { |
f1174f77 EC |
2580 | struct tnum reg_off; |
2581 | ||
2582 | /* Byte size accesses are always allowed. */ | |
2583 | if (!strict || size == 1) | |
2584 | return 0; | |
2585 | ||
2586 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
2587 | if (!tnum_is_aligned(reg_off, size)) { | |
2588 | char tn_buf[48]; | |
2589 | ||
2590 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 2591 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 2592 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
2593 | return -EACCES; |
2594 | } | |
2595 | ||
969bf05e AS |
2596 | return 0; |
2597 | } | |
2598 | ||
e07b98d9 | 2599 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
2600 | const struct bpf_reg_state *reg, int off, |
2601 | int size, bool strict_alignment_once) | |
79adffcd | 2602 | { |
ca369602 | 2603 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 2604 | const char *pointer_desc = ""; |
d1174416 | 2605 | |
79adffcd DB |
2606 | switch (reg->type) { |
2607 | case PTR_TO_PACKET: | |
de8f3a83 DB |
2608 | case PTR_TO_PACKET_META: |
2609 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
2610 | * right in front, treat it the very same way. | |
2611 | */ | |
61bd5218 | 2612 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
2613 | case PTR_TO_FLOW_KEYS: |
2614 | pointer_desc = "flow keys "; | |
2615 | break; | |
f1174f77 EC |
2616 | case PTR_TO_MAP_VALUE: |
2617 | pointer_desc = "value "; | |
2618 | break; | |
2619 | case PTR_TO_CTX: | |
2620 | pointer_desc = "context "; | |
2621 | break; | |
2622 | case PTR_TO_STACK: | |
2623 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
2624 | /* The stack spill tracking logic in check_stack_write() |
2625 | * and check_stack_read() relies on stack accesses being | |
2626 | * aligned. | |
2627 | */ | |
2628 | strict = true; | |
f1174f77 | 2629 | break; |
c64b7983 JS |
2630 | case PTR_TO_SOCKET: |
2631 | pointer_desc = "sock "; | |
2632 | break; | |
46f8bc92 MKL |
2633 | case PTR_TO_SOCK_COMMON: |
2634 | pointer_desc = "sock_common "; | |
2635 | break; | |
655a51e5 MKL |
2636 | case PTR_TO_TCP_SOCK: |
2637 | pointer_desc = "tcp_sock "; | |
2638 | break; | |
fada7fdc JL |
2639 | case PTR_TO_XDP_SOCK: |
2640 | pointer_desc = "xdp_sock "; | |
2641 | break; | |
79adffcd | 2642 | default: |
f1174f77 | 2643 | break; |
79adffcd | 2644 | } |
61bd5218 JK |
2645 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
2646 | strict); | |
79adffcd DB |
2647 | } |
2648 | ||
f4d7e40a AS |
2649 | static int update_stack_depth(struct bpf_verifier_env *env, |
2650 | const struct bpf_func_state *func, | |
2651 | int off) | |
2652 | { | |
9c8105bd | 2653 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
2654 | |
2655 | if (stack >= -off) | |
2656 | return 0; | |
2657 | ||
2658 | /* update known max for given subprogram */ | |
9c8105bd | 2659 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
2660 | return 0; |
2661 | } | |
f4d7e40a | 2662 | |
70a87ffe AS |
2663 | /* starting from main bpf function walk all instructions of the function |
2664 | * and recursively walk all callees that given function can call. | |
2665 | * Ignore jump and exit insns. | |
2666 | * Since recursion is prevented by check_cfg() this algorithm | |
2667 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
2668 | */ | |
2669 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
2670 | { | |
9c8105bd JW |
2671 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
2672 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 2673 | struct bpf_insn *insn = env->prog->insnsi; |
70a87ffe AS |
2674 | int ret_insn[MAX_CALL_FRAMES]; |
2675 | int ret_prog[MAX_CALL_FRAMES]; | |
f4d7e40a | 2676 | |
70a87ffe AS |
2677 | process_func: |
2678 | /* round up to 32-bytes, since this is granularity | |
2679 | * of interpreter stack size | |
2680 | */ | |
9c8105bd | 2681 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 2682 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 2683 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 2684 | frame + 1, depth); |
f4d7e40a AS |
2685 | return -EACCES; |
2686 | } | |
70a87ffe | 2687 | continue_func: |
4cb3d99c | 2688 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
2689 | for (; i < subprog_end; i++) { |
2690 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
2691 | continue; | |
2692 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
2693 | continue; | |
2694 | /* remember insn and function to return to */ | |
2695 | ret_insn[frame] = i + 1; | |
9c8105bd | 2696 | ret_prog[frame] = idx; |
70a87ffe AS |
2697 | |
2698 | /* find the callee */ | |
2699 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
2700 | idx = find_subprog(env, i); |
2701 | if (idx < 0) { | |
70a87ffe AS |
2702 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
2703 | i); | |
2704 | return -EFAULT; | |
2705 | } | |
70a87ffe AS |
2706 | frame++; |
2707 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
2708 | verbose(env, "the call stack of %d frames is too deep !\n", |
2709 | frame); | |
2710 | return -E2BIG; | |
70a87ffe AS |
2711 | } |
2712 | goto process_func; | |
2713 | } | |
2714 | /* end of for() loop means the last insn of the 'subprog' | |
2715 | * was reached. Doesn't matter whether it was JA or EXIT | |
2716 | */ | |
2717 | if (frame == 0) | |
2718 | return 0; | |
9c8105bd | 2719 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
2720 | frame--; |
2721 | i = ret_insn[frame]; | |
9c8105bd | 2722 | idx = ret_prog[frame]; |
70a87ffe | 2723 | goto continue_func; |
f4d7e40a AS |
2724 | } |
2725 | ||
19d28fbd | 2726 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
2727 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
2728 | const struct bpf_insn *insn, int idx) | |
2729 | { | |
2730 | int start = idx + insn->imm + 1, subprog; | |
2731 | ||
2732 | subprog = find_subprog(env, start); | |
2733 | if (subprog < 0) { | |
2734 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
2735 | start); | |
2736 | return -EFAULT; | |
2737 | } | |
9c8105bd | 2738 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 2739 | } |
19d28fbd | 2740 | #endif |
1ea47e01 | 2741 | |
58990d1f DB |
2742 | static int check_ctx_reg(struct bpf_verifier_env *env, |
2743 | const struct bpf_reg_state *reg, int regno) | |
2744 | { | |
2745 | /* Access to ctx or passing it to a helper is only allowed in | |
2746 | * its original, unmodified form. | |
2747 | */ | |
2748 | ||
2749 | if (reg->off) { | |
2750 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
2751 | regno, reg->off); | |
2752 | return -EACCES; | |
2753 | } | |
2754 | ||
2755 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2756 | char tn_buf[48]; | |
2757 | ||
2758 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2759 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
2760 | return -EACCES; | |
2761 | } | |
2762 | ||
2763 | return 0; | |
2764 | } | |
2765 | ||
9df1c28b MM |
2766 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
2767 | const struct bpf_reg_state *reg, | |
2768 | int regno, int off, int size) | |
2769 | { | |
2770 | if (off < 0) { | |
2771 | verbose(env, | |
2772 | "R%d invalid tracepoint buffer access: off=%d, size=%d", | |
2773 | regno, off, size); | |
2774 | return -EACCES; | |
2775 | } | |
2776 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2777 | char tn_buf[48]; | |
2778 | ||
2779 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2780 | verbose(env, | |
2781 | "R%d invalid variable buffer offset: off=%d, var_off=%s", | |
2782 | regno, off, tn_buf); | |
2783 | return -EACCES; | |
2784 | } | |
2785 | if (off + size > env->prog->aux->max_tp_access) | |
2786 | env->prog->aux->max_tp_access = off + size; | |
2787 | ||
2788 | return 0; | |
2789 | } | |
2790 | ||
2791 | ||
0c17d1d2 JH |
2792 | /* truncate register to smaller size (in bytes) |
2793 | * must be called with size < BPF_REG_SIZE | |
2794 | */ | |
2795 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
2796 | { | |
2797 | u64 mask; | |
2798 | ||
2799 | /* clear high bits in bit representation */ | |
2800 | reg->var_off = tnum_cast(reg->var_off, size); | |
2801 | ||
2802 | /* fix arithmetic bounds */ | |
2803 | mask = ((u64)1 << (size * 8)) - 1; | |
2804 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
2805 | reg->umin_value &= mask; | |
2806 | reg->umax_value &= mask; | |
2807 | } else { | |
2808 | reg->umin_value = 0; | |
2809 | reg->umax_value = mask; | |
2810 | } | |
2811 | reg->smin_value = reg->umin_value; | |
2812 | reg->smax_value = reg->umax_value; | |
2813 | } | |
2814 | ||
a23740ec AN |
2815 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
2816 | { | |
2817 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; | |
2818 | } | |
2819 | ||
2820 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
2821 | { | |
2822 | void *ptr; | |
2823 | u64 addr; | |
2824 | int err; | |
2825 | ||
2826 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
2827 | if (err) | |
2828 | return err; | |
2dedd7d2 | 2829 | ptr = (void *)(long)addr + off; |
a23740ec AN |
2830 | |
2831 | switch (size) { | |
2832 | case sizeof(u8): | |
2833 | *val = (u64)*(u8 *)ptr; | |
2834 | break; | |
2835 | case sizeof(u16): | |
2836 | *val = (u64)*(u16 *)ptr; | |
2837 | break; | |
2838 | case sizeof(u32): | |
2839 | *val = (u64)*(u32 *)ptr; | |
2840 | break; | |
2841 | case sizeof(u64): | |
2842 | *val = *(u64 *)ptr; | |
2843 | break; | |
2844 | default: | |
2845 | return -EINVAL; | |
2846 | } | |
2847 | return 0; | |
2848 | } | |
2849 | ||
9e15db66 AS |
2850 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
2851 | struct bpf_reg_state *regs, | |
2852 | int regno, int off, int size, | |
2853 | enum bpf_access_type atype, | |
2854 | int value_regno) | |
2855 | { | |
2856 | struct bpf_reg_state *reg = regs + regno; | |
2857 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); | |
2858 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
2859 | u32 btf_id; | |
2860 | int ret; | |
2861 | ||
9e15db66 AS |
2862 | if (off < 0) { |
2863 | verbose(env, | |
2864 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
2865 | regno, tname, off); | |
2866 | return -EACCES; | |
2867 | } | |
2868 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2869 | char tn_buf[48]; | |
2870 | ||
2871 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2872 | verbose(env, | |
2873 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
2874 | regno, tname, off, tn_buf); | |
2875 | return -EACCES; | |
2876 | } | |
2877 | ||
27ae7997 MKL |
2878 | if (env->ops->btf_struct_access) { |
2879 | ret = env->ops->btf_struct_access(&env->log, t, off, size, | |
2880 | atype, &btf_id); | |
2881 | } else { | |
2882 | if (atype != BPF_READ) { | |
2883 | verbose(env, "only read is supported\n"); | |
2884 | return -EACCES; | |
2885 | } | |
2886 | ||
2887 | ret = btf_struct_access(&env->log, t, off, size, atype, | |
2888 | &btf_id); | |
2889 | } | |
2890 | ||
9e15db66 AS |
2891 | if (ret < 0) |
2892 | return ret; | |
2893 | ||
27ae7997 MKL |
2894 | if (atype == BPF_READ) { |
2895 | if (ret == SCALAR_VALUE) { | |
2896 | mark_reg_unknown(env, regs, value_regno); | |
2897 | return 0; | |
2898 | } | |
2899 | mark_reg_known_zero(env, regs, value_regno); | |
2900 | regs[value_regno].type = PTR_TO_BTF_ID; | |
2901 | regs[value_regno].btf_id = btf_id; | |
9e15db66 | 2902 | } |
27ae7997 | 2903 | |
9e15db66 AS |
2904 | return 0; |
2905 | } | |
2906 | ||
17a52670 AS |
2907 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
2908 | * if t==write, value_regno is a register which value is stored into memory | |
2909 | * if t==read, value_regno is a register which will receive the value from memory | |
2910 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
2911 | * if t==read && value_regno==-1, don't care what we read from memory | |
2912 | */ | |
ca369602 DB |
2913 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
2914 | int off, int bpf_size, enum bpf_access_type t, | |
2915 | int value_regno, bool strict_alignment_once) | |
17a52670 | 2916 | { |
638f5b90 AS |
2917 | struct bpf_reg_state *regs = cur_regs(env); |
2918 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 2919 | struct bpf_func_state *state; |
17a52670 AS |
2920 | int size, err = 0; |
2921 | ||
2922 | size = bpf_size_to_bytes(bpf_size); | |
2923 | if (size < 0) | |
2924 | return size; | |
2925 | ||
f1174f77 | 2926 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 2927 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
2928 | if (err) |
2929 | return err; | |
17a52670 | 2930 | |
f1174f77 EC |
2931 | /* for access checks, reg->off is just part of off */ |
2932 | off += reg->off; | |
2933 | ||
2934 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
2935 | if (t == BPF_WRITE && value_regno >= 0 && |
2936 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2937 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
2938 | return -EACCES; |
2939 | } | |
591fe988 DB |
2940 | err = check_map_access_type(env, regno, off, size, t); |
2941 | if (err) | |
2942 | return err; | |
9fd29c08 | 2943 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
2944 | if (!err && t == BPF_READ && value_regno >= 0) { |
2945 | struct bpf_map *map = reg->map_ptr; | |
2946 | ||
2947 | /* if map is read-only, track its contents as scalars */ | |
2948 | if (tnum_is_const(reg->var_off) && | |
2949 | bpf_map_is_rdonly(map) && | |
2950 | map->ops->map_direct_value_addr) { | |
2951 | int map_off = off + reg->var_off.value; | |
2952 | u64 val = 0; | |
2953 | ||
2954 | err = bpf_map_direct_read(map, map_off, size, | |
2955 | &val); | |
2956 | if (err) | |
2957 | return err; | |
2958 | ||
2959 | regs[value_regno].type = SCALAR_VALUE; | |
2960 | __mark_reg_known(®s[value_regno], val); | |
2961 | } else { | |
2962 | mark_reg_unknown(env, regs, value_regno); | |
2963 | } | |
2964 | } | |
1a0dc1ac | 2965 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 2966 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
9e15db66 | 2967 | u32 btf_id = 0; |
19de99f7 | 2968 | |
1be7f75d AS |
2969 | if (t == BPF_WRITE && value_regno >= 0 && |
2970 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2971 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
2972 | return -EACCES; |
2973 | } | |
f1174f77 | 2974 | |
58990d1f DB |
2975 | err = check_ctx_reg(env, reg, regno); |
2976 | if (err < 0) | |
2977 | return err; | |
2978 | ||
9e15db66 AS |
2979 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
2980 | if (err) | |
2981 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 2982 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 2983 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
2984 | * PTR_TO_PACKET[_META,_END]. In the latter |
2985 | * case, we know the offset is zero. | |
f1174f77 | 2986 | */ |
46f8bc92 | 2987 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 2988 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 2989 | } else { |
638f5b90 | 2990 | mark_reg_known_zero(env, regs, |
61bd5218 | 2991 | value_regno); |
46f8bc92 MKL |
2992 | if (reg_type_may_be_null(reg_type)) |
2993 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
2994 | /* A load of ctx field could have different |
2995 | * actual load size with the one encoded in the | |
2996 | * insn. When the dst is PTR, it is for sure not | |
2997 | * a sub-register. | |
2998 | */ | |
2999 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
9e15db66 AS |
3000 | if (reg_type == PTR_TO_BTF_ID) |
3001 | regs[value_regno].btf_id = btf_id; | |
46f8bc92 | 3002 | } |
638f5b90 | 3003 | regs[value_regno].type = reg_type; |
969bf05e | 3004 | } |
17a52670 | 3005 | |
f1174f77 | 3006 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 3007 | off += reg->var_off.value; |
e4298d25 DB |
3008 | err = check_stack_access(env, reg, off, size); |
3009 | if (err) | |
3010 | return err; | |
8726679a | 3011 | |
f4d7e40a AS |
3012 | state = func(env, reg); |
3013 | err = update_stack_depth(env, state, off); | |
3014 | if (err) | |
3015 | return err; | |
8726679a | 3016 | |
638f5b90 | 3017 | if (t == BPF_WRITE) |
61bd5218 | 3018 | err = check_stack_write(env, state, off, size, |
af86ca4e | 3019 | value_regno, insn_idx); |
638f5b90 | 3020 | else |
61bd5218 JK |
3021 | err = check_stack_read(env, state, off, size, |
3022 | value_regno); | |
de8f3a83 | 3023 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 3024 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 3025 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
3026 | return -EACCES; |
3027 | } | |
4acf6c0b BB |
3028 | if (t == BPF_WRITE && value_regno >= 0 && |
3029 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
3030 | verbose(env, "R%d leaks addr into packet\n", |
3031 | value_regno); | |
4acf6c0b BB |
3032 | return -EACCES; |
3033 | } | |
9fd29c08 | 3034 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 3035 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 3036 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
3037 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
3038 | if (t == BPF_WRITE && value_regno >= 0 && | |
3039 | is_pointer_value(env, value_regno)) { | |
3040 | verbose(env, "R%d leaks addr into flow keys\n", | |
3041 | value_regno); | |
3042 | return -EACCES; | |
3043 | } | |
3044 | ||
3045 | err = check_flow_keys_access(env, off, size); | |
3046 | if (!err && t == BPF_READ && value_regno >= 0) | |
3047 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 3048 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 3049 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
3050 | verbose(env, "R%d cannot write into %s\n", |
3051 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
3052 | return -EACCES; |
3053 | } | |
5f456649 | 3054 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
3055 | if (!err && value_regno >= 0) |
3056 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
3057 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
3058 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
3059 | if (!err && t == BPF_READ && value_regno >= 0) | |
3060 | mark_reg_unknown(env, regs, value_regno); | |
9e15db66 AS |
3061 | } else if (reg->type == PTR_TO_BTF_ID) { |
3062 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, | |
3063 | value_regno); | |
17a52670 | 3064 | } else { |
61bd5218 JK |
3065 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
3066 | reg_type_str[reg->type]); | |
17a52670 AS |
3067 | return -EACCES; |
3068 | } | |
969bf05e | 3069 | |
f1174f77 | 3070 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 3071 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 3072 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 3073 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 3074 | } |
17a52670 AS |
3075 | return err; |
3076 | } | |
3077 | ||
31fd8581 | 3078 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 3079 | { |
17a52670 AS |
3080 | int err; |
3081 | ||
3082 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
3083 | insn->imm != 0) { | |
61bd5218 | 3084 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
3085 | return -EINVAL; |
3086 | } | |
3087 | ||
3088 | /* check src1 operand */ | |
dc503a8a | 3089 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3090 | if (err) |
3091 | return err; | |
3092 | ||
3093 | /* check src2 operand */ | |
dc503a8a | 3094 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3095 | if (err) |
3096 | return err; | |
3097 | ||
6bdf6abc | 3098 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 3099 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
3100 | return -EACCES; |
3101 | } | |
3102 | ||
ca369602 | 3103 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 3104 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
3105 | is_flow_key_reg(env, insn->dst_reg) || |
3106 | is_sk_reg(env, insn->dst_reg)) { | |
ca369602 | 3107 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
2a159c6f DB |
3108 | insn->dst_reg, |
3109 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
3110 | return -EACCES; |
3111 | } | |
3112 | ||
17a52670 | 3113 | /* check whether atomic_add can read the memory */ |
31fd8581 | 3114 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3115 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
3116 | if (err) |
3117 | return err; | |
3118 | ||
3119 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 3120 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3121 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
3122 | } |
3123 | ||
2011fccf AI |
3124 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
3125 | int off, int access_size, | |
3126 | bool zero_size_allowed) | |
3127 | { | |
3128 | struct bpf_reg_state *reg = reg_state(env, regno); | |
3129 | ||
3130 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
3131 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
3132 | if (tnum_is_const(reg->var_off)) { | |
3133 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
3134 | regno, off, access_size); | |
3135 | } else { | |
3136 | char tn_buf[48]; | |
3137 | ||
3138 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3139 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
3140 | regno, tn_buf, access_size); | |
3141 | } | |
3142 | return -EACCES; | |
3143 | } | |
3144 | return 0; | |
3145 | } | |
3146 | ||
17a52670 AS |
3147 | /* when register 'regno' is passed into function that will read 'access_size' |
3148 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
3149 | * and all elements of stack are initialized. |
3150 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
3151 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 3152 | */ |
58e2af8b | 3153 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
3154 | int access_size, bool zero_size_allowed, |
3155 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3156 | { |
2a159c6f | 3157 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 3158 | struct bpf_func_state *state = func(env, reg); |
f7cf25b2 | 3159 | int err, min_off, max_off, i, j, slot, spi; |
17a52670 | 3160 | |
914cb781 | 3161 | if (reg->type != PTR_TO_STACK) { |
f1174f77 | 3162 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 3163 | if (zero_size_allowed && access_size == 0 && |
914cb781 | 3164 | register_is_null(reg)) |
8e2fe1d9 DB |
3165 | return 0; |
3166 | ||
61bd5218 | 3167 | verbose(env, "R%d type=%s expected=%s\n", regno, |
914cb781 | 3168 | reg_type_str[reg->type], |
8e2fe1d9 | 3169 | reg_type_str[PTR_TO_STACK]); |
17a52670 | 3170 | return -EACCES; |
8e2fe1d9 | 3171 | } |
17a52670 | 3172 | |
2011fccf AI |
3173 | if (tnum_is_const(reg->var_off)) { |
3174 | min_off = max_off = reg->var_off.value + reg->off; | |
3175 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
3176 | zero_size_allowed); | |
3177 | if (err) | |
3178 | return err; | |
3179 | } else { | |
088ec26d AI |
3180 | /* Variable offset is prohibited for unprivileged mode for |
3181 | * simplicity since it requires corresponding support in | |
3182 | * Spectre masking for stack ALU. | |
3183 | * See also retrieve_ptr_limit(). | |
3184 | */ | |
3185 | if (!env->allow_ptr_leaks) { | |
3186 | char tn_buf[48]; | |
f1174f77 | 3187 | |
088ec26d AI |
3188 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3189 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
3190 | regno, tn_buf); | |
3191 | return -EACCES; | |
3192 | } | |
f2bcd05e AI |
3193 | /* Only initialized buffer on stack is allowed to be accessed |
3194 | * with variable offset. With uninitialized buffer it's hard to | |
3195 | * guarantee that whole memory is marked as initialized on | |
3196 | * helper return since specific bounds are unknown what may | |
3197 | * cause uninitialized stack leaking. | |
3198 | */ | |
3199 | if (meta && meta->raw_mode) | |
3200 | meta = NULL; | |
3201 | ||
107c26a7 AI |
3202 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
3203 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
3204 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
3205 | regno); | |
3206 | return -EACCES; | |
3207 | } | |
2011fccf | 3208 | min_off = reg->smin_value + reg->off; |
107c26a7 | 3209 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
3210 | err = __check_stack_boundary(env, regno, min_off, access_size, |
3211 | zero_size_allowed); | |
107c26a7 AI |
3212 | if (err) { |
3213 | verbose(env, "R%d min value is outside of stack bound\n", | |
3214 | regno); | |
2011fccf | 3215 | return err; |
107c26a7 | 3216 | } |
2011fccf AI |
3217 | err = __check_stack_boundary(env, regno, max_off, access_size, |
3218 | zero_size_allowed); | |
107c26a7 AI |
3219 | if (err) { |
3220 | verbose(env, "R%d max value is outside of stack bound\n", | |
3221 | regno); | |
2011fccf | 3222 | return err; |
107c26a7 | 3223 | } |
17a52670 AS |
3224 | } |
3225 | ||
435faee1 DB |
3226 | if (meta && meta->raw_mode) { |
3227 | meta->access_size = access_size; | |
3228 | meta->regno = regno; | |
3229 | return 0; | |
3230 | } | |
3231 | ||
2011fccf | 3232 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
3233 | u8 *stype; |
3234 | ||
2011fccf | 3235 | slot = -i - 1; |
638f5b90 | 3236 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
3237 | if (state->allocated_stack <= slot) |
3238 | goto err; | |
3239 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
3240 | if (*stype == STACK_MISC) | |
3241 | goto mark; | |
3242 | if (*stype == STACK_ZERO) { | |
3243 | /* helper can write anything into the stack */ | |
3244 | *stype = STACK_MISC; | |
3245 | goto mark; | |
17a52670 | 3246 | } |
f7cf25b2 AS |
3247 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
3248 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { | |
f54c7898 | 3249 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
f7cf25b2 AS |
3250 | for (j = 0; j < BPF_REG_SIZE; j++) |
3251 | state->stack[spi].slot_type[j] = STACK_MISC; | |
3252 | goto mark; | |
3253 | } | |
3254 | ||
cc2b14d5 | 3255 | err: |
2011fccf AI |
3256 | if (tnum_is_const(reg->var_off)) { |
3257 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
3258 | min_off, i - min_off, access_size); | |
3259 | } else { | |
3260 | char tn_buf[48]; | |
3261 | ||
3262 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3263 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
3264 | tn_buf, i - min_off, access_size); | |
3265 | } | |
cc2b14d5 AS |
3266 | return -EACCES; |
3267 | mark: | |
3268 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
3269 | * the whole slot to be marked as 'read' | |
3270 | */ | |
679c782d | 3271 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
3272 | state->stack[spi].spilled_ptr.parent, |
3273 | REG_LIVE_READ64); | |
17a52670 | 3274 | } |
2011fccf | 3275 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
3276 | } |
3277 | ||
06c1c049 GB |
3278 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
3279 | int access_size, bool zero_size_allowed, | |
3280 | struct bpf_call_arg_meta *meta) | |
3281 | { | |
638f5b90 | 3282 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 3283 | |
f1174f77 | 3284 | switch (reg->type) { |
06c1c049 | 3285 | case PTR_TO_PACKET: |
de8f3a83 | 3286 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
3287 | return check_packet_access(env, regno, reg->off, access_size, |
3288 | zero_size_allowed); | |
06c1c049 | 3289 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
3290 | if (check_map_access_type(env, regno, reg->off, access_size, |
3291 | meta && meta->raw_mode ? BPF_WRITE : | |
3292 | BPF_READ)) | |
3293 | return -EACCES; | |
9fd29c08 YS |
3294 | return check_map_access(env, regno, reg->off, access_size, |
3295 | zero_size_allowed); | |
f1174f77 | 3296 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
3297 | return check_stack_boundary(env, regno, access_size, |
3298 | zero_size_allowed, meta); | |
3299 | } | |
3300 | } | |
3301 | ||
d83525ca AS |
3302 | /* Implementation details: |
3303 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
3304 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
3305 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
3306 | * value_or_null->value transition, since the verifier only cares about | |
3307 | * the range of access to valid map value pointer and doesn't care about actual | |
3308 | * address of the map element. | |
3309 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
3310 | * reg->id > 0 after value_or_null->value transition. By doing so | |
3311 | * two bpf_map_lookups will be considered two different pointers that | |
3312 | * point to different bpf_spin_locks. | |
3313 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
3314 | * dead-locks. | |
3315 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
3316 | * reg_is_refcounted() logic. The verifier needs to remember only | |
3317 | * one spin_lock instead of array of acquired_refs. | |
3318 | * cur_state->active_spin_lock remembers which map value element got locked | |
3319 | * and clears it after bpf_spin_unlock. | |
3320 | */ | |
3321 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
3322 | bool is_lock) | |
3323 | { | |
3324 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
3325 | struct bpf_verifier_state *cur = env->cur_state; | |
3326 | bool is_const = tnum_is_const(reg->var_off); | |
3327 | struct bpf_map *map = reg->map_ptr; | |
3328 | u64 val = reg->var_off.value; | |
3329 | ||
3330 | if (reg->type != PTR_TO_MAP_VALUE) { | |
3331 | verbose(env, "R%d is not a pointer to map_value\n", regno); | |
3332 | return -EINVAL; | |
3333 | } | |
3334 | if (!is_const) { | |
3335 | verbose(env, | |
3336 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
3337 | regno); | |
3338 | return -EINVAL; | |
3339 | } | |
3340 | if (!map->btf) { | |
3341 | verbose(env, | |
3342 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
3343 | map->name); | |
3344 | return -EINVAL; | |
3345 | } | |
3346 | if (!map_value_has_spin_lock(map)) { | |
3347 | if (map->spin_lock_off == -E2BIG) | |
3348 | verbose(env, | |
3349 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
3350 | map->name); | |
3351 | else if (map->spin_lock_off == -ENOENT) | |
3352 | verbose(env, | |
3353 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
3354 | map->name); | |
3355 | else | |
3356 | verbose(env, | |
3357 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
3358 | map->name); | |
3359 | return -EINVAL; | |
3360 | } | |
3361 | if (map->spin_lock_off != val + reg->off) { | |
3362 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
3363 | val + reg->off); | |
3364 | return -EINVAL; | |
3365 | } | |
3366 | if (is_lock) { | |
3367 | if (cur->active_spin_lock) { | |
3368 | verbose(env, | |
3369 | "Locking two bpf_spin_locks are not allowed\n"); | |
3370 | return -EINVAL; | |
3371 | } | |
3372 | cur->active_spin_lock = reg->id; | |
3373 | } else { | |
3374 | if (!cur->active_spin_lock) { | |
3375 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
3376 | return -EINVAL; | |
3377 | } | |
3378 | if (cur->active_spin_lock != reg->id) { | |
3379 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
3380 | return -EINVAL; | |
3381 | } | |
3382 | cur->active_spin_lock = 0; | |
3383 | } | |
3384 | return 0; | |
3385 | } | |
3386 | ||
90133415 DB |
3387 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
3388 | { | |
3389 | return type == ARG_PTR_TO_MEM || | |
3390 | type == ARG_PTR_TO_MEM_OR_NULL || | |
3391 | type == ARG_PTR_TO_UNINIT_MEM; | |
3392 | } | |
3393 | ||
3394 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
3395 | { | |
3396 | return type == ARG_CONST_SIZE || | |
3397 | type == ARG_CONST_SIZE_OR_ZERO; | |
3398 | } | |
3399 | ||
57c3bb72 AI |
3400 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
3401 | { | |
3402 | return type == ARG_PTR_TO_INT || | |
3403 | type == ARG_PTR_TO_LONG; | |
3404 | } | |
3405 | ||
3406 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
3407 | { | |
3408 | if (type == ARG_PTR_TO_INT) | |
3409 | return sizeof(u32); | |
3410 | else if (type == ARG_PTR_TO_LONG) | |
3411 | return sizeof(u64); | |
3412 | ||
3413 | return -EINVAL; | |
3414 | } | |
3415 | ||
58e2af8b | 3416 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
3417 | enum bpf_arg_type arg_type, |
3418 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3419 | { |
638f5b90 | 3420 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 3421 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
3422 | int err = 0; |
3423 | ||
80f1d68c | 3424 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
3425 | return 0; |
3426 | ||
dc503a8a EC |
3427 | err = check_reg_arg(env, regno, SRC_OP); |
3428 | if (err) | |
3429 | return err; | |
17a52670 | 3430 | |
1be7f75d AS |
3431 | if (arg_type == ARG_ANYTHING) { |
3432 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
3433 | verbose(env, "R%d leaks addr into helper function\n", |
3434 | regno); | |
1be7f75d AS |
3435 | return -EACCES; |
3436 | } | |
80f1d68c | 3437 | return 0; |
1be7f75d | 3438 | } |
80f1d68c | 3439 | |
de8f3a83 | 3440 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 3441 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 3442 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
3443 | return -EACCES; |
3444 | } | |
3445 | ||
8e2fe1d9 | 3446 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
2ea864c5 | 3447 | arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3448 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
3449 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
17a52670 | 3450 | expected_type = PTR_TO_STACK; |
6ac99e8f MKL |
3451 | if (register_is_null(reg) && |
3452 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) | |
3453 | /* final test in check_stack_boundary() */; | |
3454 | else if (!type_is_pkt_pointer(type) && | |
3455 | type != PTR_TO_MAP_VALUE && | |
3456 | type != expected_type) | |
6841de8b | 3457 | goto err_type; |
39f19ebb AS |
3458 | } else if (arg_type == ARG_CONST_SIZE || |
3459 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
f1174f77 EC |
3460 | expected_type = SCALAR_VALUE; |
3461 | if (type != expected_type) | |
6841de8b | 3462 | goto err_type; |
17a52670 AS |
3463 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
3464 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
3465 | if (type != expected_type) |
3466 | goto err_type; | |
608cd71a AS |
3467 | } else if (arg_type == ARG_PTR_TO_CTX) { |
3468 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
3469 | if (type != expected_type) |
3470 | goto err_type; | |
58990d1f DB |
3471 | err = check_ctx_reg(env, reg, regno); |
3472 | if (err < 0) | |
3473 | return err; | |
46f8bc92 MKL |
3474 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
3475 | expected_type = PTR_TO_SOCK_COMMON; | |
3476 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ | |
3477 | if (!type_is_sk_pointer(type)) | |
3478 | goto err_type; | |
1b986589 MKL |
3479 | if (reg->ref_obj_id) { |
3480 | if (meta->ref_obj_id) { | |
3481 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
3482 | regno, reg->ref_obj_id, | |
3483 | meta->ref_obj_id); | |
3484 | return -EFAULT; | |
3485 | } | |
3486 | meta->ref_obj_id = reg->ref_obj_id; | |
fd978bf7 | 3487 | } |
6ac99e8f MKL |
3488 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
3489 | expected_type = PTR_TO_SOCKET; | |
3490 | if (type != expected_type) | |
3491 | goto err_type; | |
a7658e1a AS |
3492 | } else if (arg_type == ARG_PTR_TO_BTF_ID) { |
3493 | expected_type = PTR_TO_BTF_ID; | |
3494 | if (type != expected_type) | |
3495 | goto err_type; | |
3496 | if (reg->btf_id != meta->btf_id) { | |
3497 | verbose(env, "Helper has type %s got %s in R%d\n", | |
3498 | kernel_type_name(meta->btf_id), | |
3499 | kernel_type_name(reg->btf_id), regno); | |
3500 | ||
3501 | return -EACCES; | |
3502 | } | |
3503 | if (!tnum_is_const(reg->var_off) || reg->var_off.value || reg->off) { | |
3504 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", | |
3505 | regno); | |
3506 | return -EACCES; | |
3507 | } | |
d83525ca AS |
3508 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
3509 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
3510 | if (process_spin_lock(env, regno, true)) | |
3511 | return -EACCES; | |
3512 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
3513 | if (process_spin_lock(env, regno, false)) | |
3514 | return -EACCES; | |
3515 | } else { | |
3516 | verbose(env, "verifier internal error\n"); | |
3517 | return -EFAULT; | |
3518 | } | |
90133415 | 3519 | } else if (arg_type_is_mem_ptr(arg_type)) { |
8e2fe1d9 DB |
3520 | expected_type = PTR_TO_STACK; |
3521 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 3522 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
3523 | * happens during stack boundary checking. |
3524 | */ | |
914cb781 | 3525 | if (register_is_null(reg) && |
db1ac496 | 3526 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
6841de8b | 3527 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
3528 | else if (!type_is_pkt_pointer(type) && |
3529 | type != PTR_TO_MAP_VALUE && | |
f1174f77 | 3530 | type != expected_type) |
6841de8b | 3531 | goto err_type; |
39f19ebb | 3532 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
57c3bb72 AI |
3533 | } else if (arg_type_is_int_ptr(arg_type)) { |
3534 | expected_type = PTR_TO_STACK; | |
3535 | if (!type_is_pkt_pointer(type) && | |
3536 | type != PTR_TO_MAP_VALUE && | |
3537 | type != expected_type) | |
3538 | goto err_type; | |
17a52670 | 3539 | } else { |
61bd5218 | 3540 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
3541 | return -EFAULT; |
3542 | } | |
3543 | ||
17a52670 AS |
3544 | if (arg_type == ARG_CONST_MAP_PTR) { |
3545 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 3546 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
3547 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
3548 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
3549 | * check that [key, key + map->key_size) are within | |
3550 | * stack limits and initialized | |
3551 | */ | |
33ff9823 | 3552 | if (!meta->map_ptr) { |
17a52670 AS |
3553 | /* in function declaration map_ptr must come before |
3554 | * map_key, so that it's verified and known before | |
3555 | * we have to check map_key here. Otherwise it means | |
3556 | * that kernel subsystem misconfigured verifier | |
3557 | */ | |
61bd5218 | 3558 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
3559 | return -EACCES; |
3560 | } | |
d71962f3 PC |
3561 | err = check_helper_mem_access(env, regno, |
3562 | meta->map_ptr->key_size, false, | |
3563 | NULL); | |
2ea864c5 | 3564 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3565 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
3566 | !register_is_null(reg)) || | |
2ea864c5 | 3567 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
3568 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
3569 | * check [value, value + map->value_size) validity | |
3570 | */ | |
33ff9823 | 3571 | if (!meta->map_ptr) { |
17a52670 | 3572 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 3573 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
3574 | return -EACCES; |
3575 | } | |
2ea864c5 | 3576 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
3577 | err = check_helper_mem_access(env, regno, |
3578 | meta->map_ptr->value_size, false, | |
2ea864c5 | 3579 | meta); |
90133415 | 3580 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 3581 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 3582 | |
849fa506 YS |
3583 | /* remember the mem_size which may be used later |
3584 | * to refine return values. | |
3585 | */ | |
3586 | meta->msize_smax_value = reg->smax_value; | |
3587 | meta->msize_umax_value = reg->umax_value; | |
3588 | ||
f1174f77 EC |
3589 | /* The register is SCALAR_VALUE; the access check |
3590 | * happens using its boundaries. | |
06c1c049 | 3591 | */ |
f1174f77 | 3592 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
3593 | /* For unprivileged variable accesses, disable raw |
3594 | * mode so that the program is required to | |
3595 | * initialize all the memory that the helper could | |
3596 | * just partially fill up. | |
3597 | */ | |
3598 | meta = NULL; | |
3599 | ||
b03c9f9f | 3600 | if (reg->smin_value < 0) { |
61bd5218 | 3601 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
3602 | regno); |
3603 | return -EACCES; | |
3604 | } | |
06c1c049 | 3605 | |
b03c9f9f | 3606 | if (reg->umin_value == 0) { |
f1174f77 EC |
3607 | err = check_helper_mem_access(env, regno - 1, 0, |
3608 | zero_size_allowed, | |
3609 | meta); | |
06c1c049 GB |
3610 | if (err) |
3611 | return err; | |
06c1c049 | 3612 | } |
f1174f77 | 3613 | |
b03c9f9f | 3614 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 3615 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
3616 | regno); |
3617 | return -EACCES; | |
3618 | } | |
3619 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 3620 | reg->umax_value, |
f1174f77 | 3621 | zero_size_allowed, meta); |
b5dc0163 AS |
3622 | if (!err) |
3623 | err = mark_chain_precision(env, regno); | |
57c3bb72 AI |
3624 | } else if (arg_type_is_int_ptr(arg_type)) { |
3625 | int size = int_ptr_type_to_size(arg_type); | |
3626 | ||
3627 | err = check_helper_mem_access(env, regno, size, false, meta); | |
3628 | if (err) | |
3629 | return err; | |
3630 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
3631 | } |
3632 | ||
3633 | return err; | |
6841de8b | 3634 | err_type: |
61bd5218 | 3635 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
3636 | reg_type_str[type], reg_type_str[expected_type]); |
3637 | return -EACCES; | |
17a52670 AS |
3638 | } |
3639 | ||
61bd5218 JK |
3640 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
3641 | struct bpf_map *map, int func_id) | |
35578d79 | 3642 | { |
35578d79 KX |
3643 | if (!map) |
3644 | return 0; | |
3645 | ||
6aff67c8 AS |
3646 | /* We need a two way check, first is from map perspective ... */ |
3647 | switch (map->map_type) { | |
3648 | case BPF_MAP_TYPE_PROG_ARRAY: | |
3649 | if (func_id != BPF_FUNC_tail_call) | |
3650 | goto error; | |
3651 | break; | |
3652 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
3653 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 3654 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 3655 | func_id != BPF_FUNC_skb_output && |
908432ca | 3656 | func_id != BPF_FUNC_perf_event_read_value) |
6aff67c8 AS |
3657 | goto error; |
3658 | break; | |
3659 | case BPF_MAP_TYPE_STACK_TRACE: | |
3660 | if (func_id != BPF_FUNC_get_stackid) | |
3661 | goto error; | |
3662 | break; | |
4ed8ec52 | 3663 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 3664 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 3665 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
3666 | goto error; |
3667 | break; | |
cd339431 | 3668 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 3669 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
3670 | if (func_id != BPF_FUNC_get_local_storage) |
3671 | goto error; | |
3672 | break; | |
546ac1ff | 3673 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 3674 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
3675 | if (func_id != BPF_FUNC_redirect_map && |
3676 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
3677 | goto error; |
3678 | break; | |
fbfc504a BT |
3679 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
3680 | * appear. | |
3681 | */ | |
6710e112 JDB |
3682 | case BPF_MAP_TYPE_CPUMAP: |
3683 | if (func_id != BPF_FUNC_redirect_map) | |
3684 | goto error; | |
3685 | break; | |
fada7fdc JL |
3686 | case BPF_MAP_TYPE_XSKMAP: |
3687 | if (func_id != BPF_FUNC_redirect_map && | |
3688 | func_id != BPF_FUNC_map_lookup_elem) | |
3689 | goto error; | |
3690 | break; | |
56f668df | 3691 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 3692 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
3693 | if (func_id != BPF_FUNC_map_lookup_elem) |
3694 | goto error; | |
16a43625 | 3695 | break; |
174a79ff JF |
3696 | case BPF_MAP_TYPE_SOCKMAP: |
3697 | if (func_id != BPF_FUNC_sk_redirect_map && | |
3698 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb JF |
3699 | func_id != BPF_FUNC_map_delete_elem && |
3700 | func_id != BPF_FUNC_msg_redirect_map) | |
174a79ff JF |
3701 | goto error; |
3702 | break; | |
81110384 JF |
3703 | case BPF_MAP_TYPE_SOCKHASH: |
3704 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
3705 | func_id != BPF_FUNC_sock_hash_update && | |
3706 | func_id != BPF_FUNC_map_delete_elem && | |
3707 | func_id != BPF_FUNC_msg_redirect_hash) | |
3708 | goto error; | |
3709 | break; | |
2dbb9b9e MKL |
3710 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
3711 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
3712 | goto error; | |
3713 | break; | |
f1a2e44a MV |
3714 | case BPF_MAP_TYPE_QUEUE: |
3715 | case BPF_MAP_TYPE_STACK: | |
3716 | if (func_id != BPF_FUNC_map_peek_elem && | |
3717 | func_id != BPF_FUNC_map_pop_elem && | |
3718 | func_id != BPF_FUNC_map_push_elem) | |
3719 | goto error; | |
3720 | break; | |
6ac99e8f MKL |
3721 | case BPF_MAP_TYPE_SK_STORAGE: |
3722 | if (func_id != BPF_FUNC_sk_storage_get && | |
3723 | func_id != BPF_FUNC_sk_storage_delete) | |
3724 | goto error; | |
3725 | break; | |
6aff67c8 AS |
3726 | default: |
3727 | break; | |
3728 | } | |
3729 | ||
3730 | /* ... and second from the function itself. */ | |
3731 | switch (func_id) { | |
3732 | case BPF_FUNC_tail_call: | |
3733 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
3734 | goto error; | |
f910cefa | 3735 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
3736 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
3737 | return -EINVAL; | |
3738 | } | |
6aff67c8 AS |
3739 | break; |
3740 | case BPF_FUNC_perf_event_read: | |
3741 | case BPF_FUNC_perf_event_output: | |
908432ca | 3742 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 3743 | case BPF_FUNC_skb_output: |
6aff67c8 AS |
3744 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
3745 | goto error; | |
3746 | break; | |
3747 | case BPF_FUNC_get_stackid: | |
3748 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
3749 | goto error; | |
3750 | break; | |
60d20f91 | 3751 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 3752 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
3753 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
3754 | goto error; | |
3755 | break; | |
97f91a7c | 3756 | case BPF_FUNC_redirect_map: |
9c270af3 | 3757 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 3758 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
3759 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
3760 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
3761 | goto error; |
3762 | break; | |
174a79ff | 3763 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 3764 | case BPF_FUNC_msg_redirect_map: |
81110384 | 3765 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
3766 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
3767 | goto error; | |
3768 | break; | |
81110384 JF |
3769 | case BPF_FUNC_sk_redirect_hash: |
3770 | case BPF_FUNC_msg_redirect_hash: | |
3771 | case BPF_FUNC_sock_hash_update: | |
3772 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
3773 | goto error; |
3774 | break; | |
cd339431 | 3775 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
3776 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
3777 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
3778 | goto error; |
3779 | break; | |
2dbb9b9e MKL |
3780 | case BPF_FUNC_sk_select_reuseport: |
3781 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) | |
3782 | goto error; | |
3783 | break; | |
f1a2e44a MV |
3784 | case BPF_FUNC_map_peek_elem: |
3785 | case BPF_FUNC_map_pop_elem: | |
3786 | case BPF_FUNC_map_push_elem: | |
3787 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
3788 | map->map_type != BPF_MAP_TYPE_STACK) | |
3789 | goto error; | |
3790 | break; | |
6ac99e8f MKL |
3791 | case BPF_FUNC_sk_storage_get: |
3792 | case BPF_FUNC_sk_storage_delete: | |
3793 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
3794 | goto error; | |
3795 | break; | |
6aff67c8 AS |
3796 | default: |
3797 | break; | |
35578d79 KX |
3798 | } |
3799 | ||
3800 | return 0; | |
6aff67c8 | 3801 | error: |
61bd5218 | 3802 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 3803 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 3804 | return -EINVAL; |
35578d79 KX |
3805 | } |
3806 | ||
90133415 | 3807 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
3808 | { |
3809 | int count = 0; | |
3810 | ||
39f19ebb | 3811 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3812 | count++; |
39f19ebb | 3813 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3814 | count++; |
39f19ebb | 3815 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3816 | count++; |
39f19ebb | 3817 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3818 | count++; |
39f19ebb | 3819 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
3820 | count++; |
3821 | ||
90133415 DB |
3822 | /* We only support one arg being in raw mode at the moment, |
3823 | * which is sufficient for the helper functions we have | |
3824 | * right now. | |
3825 | */ | |
3826 | return count <= 1; | |
3827 | } | |
3828 | ||
3829 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
3830 | enum bpf_arg_type arg_next) | |
3831 | { | |
3832 | return (arg_type_is_mem_ptr(arg_curr) && | |
3833 | !arg_type_is_mem_size(arg_next)) || | |
3834 | (!arg_type_is_mem_ptr(arg_curr) && | |
3835 | arg_type_is_mem_size(arg_next)); | |
3836 | } | |
3837 | ||
3838 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
3839 | { | |
3840 | /* bpf_xxx(..., buf, len) call will access 'len' | |
3841 | * bytes from memory 'buf'. Both arg types need | |
3842 | * to be paired, so make sure there's no buggy | |
3843 | * helper function specification. | |
3844 | */ | |
3845 | if (arg_type_is_mem_size(fn->arg1_type) || | |
3846 | arg_type_is_mem_ptr(fn->arg5_type) || | |
3847 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
3848 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
3849 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
3850 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
3851 | return false; | |
3852 | ||
3853 | return true; | |
3854 | } | |
3855 | ||
1b986589 | 3856 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
3857 | { |
3858 | int count = 0; | |
3859 | ||
1b986589 | 3860 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 3861 | count++; |
1b986589 | 3862 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 3863 | count++; |
1b986589 | 3864 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 3865 | count++; |
1b986589 | 3866 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 3867 | count++; |
1b986589 | 3868 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
3869 | count++; |
3870 | ||
1b986589 MKL |
3871 | /* A reference acquiring function cannot acquire |
3872 | * another refcounted ptr. | |
3873 | */ | |
3874 | if (is_acquire_function(func_id) && count) | |
3875 | return false; | |
3876 | ||
fd978bf7 JS |
3877 | /* We only support one arg being unreferenced at the moment, |
3878 | * which is sufficient for the helper functions we have right now. | |
3879 | */ | |
3880 | return count <= 1; | |
3881 | } | |
3882 | ||
1b986589 | 3883 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
3884 | { |
3885 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 3886 | check_arg_pair_ok(fn) && |
1b986589 | 3887 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
3888 | } |
3889 | ||
de8f3a83 DB |
3890 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
3891 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 3892 | */ |
f4d7e40a AS |
3893 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
3894 | struct bpf_func_state *state) | |
969bf05e | 3895 | { |
58e2af8b | 3896 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
3897 | int i; |
3898 | ||
3899 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 3900 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 3901 | mark_reg_unknown(env, regs, i); |
969bf05e | 3902 | |
f3709f69 JS |
3903 | bpf_for_each_spilled_reg(i, state, reg) { |
3904 | if (!reg) | |
969bf05e | 3905 | continue; |
de8f3a83 | 3906 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 3907 | __mark_reg_unknown(env, reg); |
969bf05e AS |
3908 | } |
3909 | } | |
3910 | ||
f4d7e40a AS |
3911 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
3912 | { | |
3913 | struct bpf_verifier_state *vstate = env->cur_state; | |
3914 | int i; | |
3915 | ||
3916 | for (i = 0; i <= vstate->curframe; i++) | |
3917 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
3918 | } | |
3919 | ||
fd978bf7 | 3920 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
3921 | struct bpf_func_state *state, |
3922 | int ref_obj_id) | |
fd978bf7 JS |
3923 | { |
3924 | struct bpf_reg_state *regs = state->regs, *reg; | |
3925 | int i; | |
3926 | ||
3927 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 3928 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
3929 | mark_reg_unknown(env, regs, i); |
3930 | ||
3931 | bpf_for_each_spilled_reg(i, state, reg) { | |
3932 | if (!reg) | |
3933 | continue; | |
1b986589 | 3934 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 3935 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
3936 | } |
3937 | } | |
3938 | ||
3939 | /* The pointer with the specified id has released its reference to kernel | |
3940 | * resources. Identify all copies of the same pointer and clear the reference. | |
3941 | */ | |
3942 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 3943 | int ref_obj_id) |
fd978bf7 JS |
3944 | { |
3945 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 3946 | int err; |
fd978bf7 JS |
3947 | int i; |
3948 | ||
1b986589 MKL |
3949 | err = release_reference_state(cur_func(env), ref_obj_id); |
3950 | if (err) | |
3951 | return err; | |
3952 | ||
fd978bf7 | 3953 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 3954 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 3955 | |
1b986589 | 3956 | return 0; |
fd978bf7 JS |
3957 | } |
3958 | ||
f4d7e40a AS |
3959 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
3960 | int *insn_idx) | |
3961 | { | |
3962 | struct bpf_verifier_state *state = env->cur_state; | |
3963 | struct bpf_func_state *caller, *callee; | |
fd978bf7 | 3964 | int i, err, subprog, target_insn; |
f4d7e40a | 3965 | |
aada9ce6 | 3966 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 3967 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 3968 | state->curframe + 2); |
f4d7e40a AS |
3969 | return -E2BIG; |
3970 | } | |
3971 | ||
3972 | target_insn = *insn_idx + insn->imm; | |
3973 | subprog = find_subprog(env, target_insn + 1); | |
3974 | if (subprog < 0) { | |
3975 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
3976 | target_insn + 1); | |
3977 | return -EFAULT; | |
3978 | } | |
3979 | ||
3980 | caller = state->frame[state->curframe]; | |
3981 | if (state->frame[state->curframe + 1]) { | |
3982 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
3983 | state->curframe + 1); | |
3984 | return -EFAULT; | |
3985 | } | |
3986 | ||
3987 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); | |
3988 | if (!callee) | |
3989 | return -ENOMEM; | |
3990 | state->frame[state->curframe + 1] = callee; | |
3991 | ||
3992 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
3993 | * into its own stack before reading from it. | |
3994 | * callee can read/write into caller's stack | |
3995 | */ | |
3996 | init_func_state(env, callee, | |
3997 | /* remember the callsite, it will be used by bpf_exit */ | |
3998 | *insn_idx /* callsite */, | |
3999 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 4000 | subprog /* subprog number within this prog */); |
f4d7e40a | 4001 | |
fd978bf7 JS |
4002 | /* Transfer references to the callee */ |
4003 | err = transfer_reference_state(callee, caller); | |
4004 | if (err) | |
4005 | return err; | |
4006 | ||
679c782d EC |
4007 | /* copy r1 - r5 args that callee can access. The copy includes parent |
4008 | * pointers, which connects us up to the liveness chain | |
4009 | */ | |
f4d7e40a AS |
4010 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
4011 | callee->regs[i] = caller->regs[i]; | |
4012 | ||
679c782d | 4013 | /* after the call registers r0 - r5 were scratched */ |
f4d7e40a AS |
4014 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
4015 | mark_reg_not_init(env, caller->regs, caller_saved[i]); | |
4016 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
4017 | } | |
4018 | ||
4019 | /* only increment it after check_reg_arg() finished */ | |
4020 | state->curframe++; | |
4021 | ||
8c1b6e69 AS |
4022 | if (btf_check_func_arg_match(env, subprog)) |
4023 | return -EINVAL; | |
4024 | ||
f4d7e40a AS |
4025 | /* and go analyze first insn of the callee */ |
4026 | *insn_idx = target_insn; | |
4027 | ||
06ee7115 | 4028 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4029 | verbose(env, "caller:\n"); |
4030 | print_verifier_state(env, caller); | |
4031 | verbose(env, "callee:\n"); | |
4032 | print_verifier_state(env, callee); | |
4033 | } | |
4034 | return 0; | |
4035 | } | |
4036 | ||
4037 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
4038 | { | |
4039 | struct bpf_verifier_state *state = env->cur_state; | |
4040 | struct bpf_func_state *caller, *callee; | |
4041 | struct bpf_reg_state *r0; | |
fd978bf7 | 4042 | int err; |
f4d7e40a AS |
4043 | |
4044 | callee = state->frame[state->curframe]; | |
4045 | r0 = &callee->regs[BPF_REG_0]; | |
4046 | if (r0->type == PTR_TO_STACK) { | |
4047 | /* technically it's ok to return caller's stack pointer | |
4048 | * (or caller's caller's pointer) back to the caller, | |
4049 | * since these pointers are valid. Only current stack | |
4050 | * pointer will be invalid as soon as function exits, | |
4051 | * but let's be conservative | |
4052 | */ | |
4053 | verbose(env, "cannot return stack pointer to the caller\n"); | |
4054 | return -EINVAL; | |
4055 | } | |
4056 | ||
4057 | state->curframe--; | |
4058 | caller = state->frame[state->curframe]; | |
4059 | /* return to the caller whatever r0 had in the callee */ | |
4060 | caller->regs[BPF_REG_0] = *r0; | |
4061 | ||
fd978bf7 JS |
4062 | /* Transfer references to the caller */ |
4063 | err = transfer_reference_state(caller, callee); | |
4064 | if (err) | |
4065 | return err; | |
4066 | ||
f4d7e40a | 4067 | *insn_idx = callee->callsite + 1; |
06ee7115 | 4068 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4069 | verbose(env, "returning from callee:\n"); |
4070 | print_verifier_state(env, callee); | |
4071 | verbose(env, "to caller at %d:\n", *insn_idx); | |
4072 | print_verifier_state(env, caller); | |
4073 | } | |
4074 | /* clear everything in the callee */ | |
4075 | free_func_state(callee); | |
4076 | state->frame[state->curframe + 1] = NULL; | |
4077 | return 0; | |
4078 | } | |
4079 | ||
849fa506 YS |
4080 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
4081 | int func_id, | |
4082 | struct bpf_call_arg_meta *meta) | |
4083 | { | |
4084 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
4085 | ||
4086 | if (ret_type != RET_INTEGER || | |
4087 | (func_id != BPF_FUNC_get_stack && | |
4088 | func_id != BPF_FUNC_probe_read_str)) | |
4089 | return; | |
4090 | ||
4091 | ret_reg->smax_value = meta->msize_smax_value; | |
4092 | ret_reg->umax_value = meta->msize_umax_value; | |
4093 | __reg_deduce_bounds(ret_reg); | |
4094 | __reg_bound_offset(ret_reg); | |
4095 | } | |
4096 | ||
c93552c4 DB |
4097 | static int |
4098 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4099 | int func_id, int insn_idx) | |
4100 | { | |
4101 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 4102 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
4103 | |
4104 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
4105 | func_id != BPF_FUNC_map_lookup_elem && |
4106 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
4107 | func_id != BPF_FUNC_map_delete_elem && |
4108 | func_id != BPF_FUNC_map_push_elem && | |
4109 | func_id != BPF_FUNC_map_pop_elem && | |
4110 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 4111 | return 0; |
09772d92 | 4112 | |
591fe988 | 4113 | if (map == NULL) { |
c93552c4 DB |
4114 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
4115 | return -EINVAL; | |
4116 | } | |
4117 | ||
591fe988 DB |
4118 | /* In case of read-only, some additional restrictions |
4119 | * need to be applied in order to prevent altering the | |
4120 | * state of the map from program side. | |
4121 | */ | |
4122 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
4123 | (func_id == BPF_FUNC_map_delete_elem || | |
4124 | func_id == BPF_FUNC_map_update_elem || | |
4125 | func_id == BPF_FUNC_map_push_elem || | |
4126 | func_id == BPF_FUNC_map_pop_elem)) { | |
4127 | verbose(env, "write into map forbidden\n"); | |
4128 | return -EACCES; | |
4129 | } | |
4130 | ||
d2e4c1e6 | 4131 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 DB |
4132 | bpf_map_ptr_store(aux, meta->map_ptr, |
4133 | meta->map_ptr->unpriv_array); | |
d2e4c1e6 | 4134 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 DB |
4135 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
4136 | meta->map_ptr->unpriv_array); | |
4137 | return 0; | |
4138 | } | |
4139 | ||
d2e4c1e6 DB |
4140 | static int |
4141 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4142 | int func_id, int insn_idx) | |
4143 | { | |
4144 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
4145 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
4146 | struct bpf_map *map = meta->map_ptr; | |
4147 | struct tnum range; | |
4148 | u64 val; | |
cc52d914 | 4149 | int err; |
d2e4c1e6 DB |
4150 | |
4151 | if (func_id != BPF_FUNC_tail_call) | |
4152 | return 0; | |
4153 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
4154 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
4155 | return -EINVAL; | |
4156 | } | |
4157 | ||
4158 | range = tnum_range(0, map->max_entries - 1); | |
4159 | reg = ®s[BPF_REG_3]; | |
4160 | ||
4161 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
4162 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4163 | return 0; | |
4164 | } | |
4165 | ||
cc52d914 DB |
4166 | err = mark_chain_precision(env, BPF_REG_3); |
4167 | if (err) | |
4168 | return err; | |
4169 | ||
d2e4c1e6 DB |
4170 | val = reg->var_off.value; |
4171 | if (bpf_map_key_unseen(aux)) | |
4172 | bpf_map_key_store(aux, val); | |
4173 | else if (!bpf_map_key_poisoned(aux) && | |
4174 | bpf_map_key_immediate(aux) != val) | |
4175 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4176 | return 0; | |
4177 | } | |
4178 | ||
fd978bf7 JS |
4179 | static int check_reference_leak(struct bpf_verifier_env *env) |
4180 | { | |
4181 | struct bpf_func_state *state = cur_func(env); | |
4182 | int i; | |
4183 | ||
4184 | for (i = 0; i < state->acquired_refs; i++) { | |
4185 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
4186 | state->refs[i].id, state->refs[i].insn_idx); | |
4187 | } | |
4188 | return state->acquired_refs ? -EINVAL : 0; | |
4189 | } | |
4190 | ||
f4d7e40a | 4191 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 4192 | { |
17a52670 | 4193 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 4194 | struct bpf_reg_state *regs; |
33ff9823 | 4195 | struct bpf_call_arg_meta meta; |
969bf05e | 4196 | bool changes_data; |
17a52670 AS |
4197 | int i, err; |
4198 | ||
4199 | /* find function prototype */ | |
4200 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
4201 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
4202 | func_id); | |
17a52670 AS |
4203 | return -EINVAL; |
4204 | } | |
4205 | ||
00176a34 | 4206 | if (env->ops->get_func_proto) |
5e43f899 | 4207 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 4208 | if (!fn) { |
61bd5218 JK |
4209 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
4210 | func_id); | |
17a52670 AS |
4211 | return -EINVAL; |
4212 | } | |
4213 | ||
4214 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 4215 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 4216 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
4217 | return -EINVAL; |
4218 | } | |
4219 | ||
04514d13 | 4220 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 4221 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
4222 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
4223 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
4224 | func_id_name(func_id), func_id); | |
4225 | return -EINVAL; | |
4226 | } | |
969bf05e | 4227 | |
33ff9823 | 4228 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 4229 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 4230 | |
1b986589 | 4231 | err = check_func_proto(fn, func_id); |
435faee1 | 4232 | if (err) { |
61bd5218 | 4233 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 4234 | func_id_name(func_id), func_id); |
435faee1 DB |
4235 | return err; |
4236 | } | |
4237 | ||
d83525ca | 4238 | meta.func_id = func_id; |
17a52670 | 4239 | /* check args */ |
a7658e1a | 4240 | for (i = 0; i < 5; i++) { |
9cc31b3a AS |
4241 | err = btf_resolve_helper_id(&env->log, fn, i); |
4242 | if (err > 0) | |
4243 | meta.btf_id = err; | |
a7658e1a AS |
4244 | err = check_func_arg(env, BPF_REG_1 + i, fn->arg_type[i], &meta); |
4245 | if (err) | |
4246 | return err; | |
4247 | } | |
17a52670 | 4248 | |
c93552c4 DB |
4249 | err = record_func_map(env, &meta, func_id, insn_idx); |
4250 | if (err) | |
4251 | return err; | |
4252 | ||
d2e4c1e6 DB |
4253 | err = record_func_key(env, &meta, func_id, insn_idx); |
4254 | if (err) | |
4255 | return err; | |
4256 | ||
435faee1 DB |
4257 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
4258 | * is inferred from register state. | |
4259 | */ | |
4260 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
4261 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
4262 | BPF_WRITE, -1, false); | |
435faee1 DB |
4263 | if (err) |
4264 | return err; | |
4265 | } | |
4266 | ||
fd978bf7 JS |
4267 | if (func_id == BPF_FUNC_tail_call) { |
4268 | err = check_reference_leak(env); | |
4269 | if (err) { | |
4270 | verbose(env, "tail_call would lead to reference leak\n"); | |
4271 | return err; | |
4272 | } | |
4273 | } else if (is_release_function(func_id)) { | |
1b986589 | 4274 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
4275 | if (err) { |
4276 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
4277 | func_id_name(func_id), func_id); | |
fd978bf7 | 4278 | return err; |
46f8bc92 | 4279 | } |
fd978bf7 JS |
4280 | } |
4281 | ||
638f5b90 | 4282 | regs = cur_regs(env); |
cd339431 RG |
4283 | |
4284 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
4285 | * this is required because get_local_storage() can't return an error. | |
4286 | */ | |
4287 | if (func_id == BPF_FUNC_get_local_storage && | |
4288 | !register_is_null(®s[BPF_REG_2])) { | |
4289 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
4290 | return -EINVAL; | |
4291 | } | |
4292 | ||
17a52670 | 4293 | /* reset caller saved regs */ |
dc503a8a | 4294 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 4295 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
4296 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
4297 | } | |
17a52670 | 4298 | |
5327ed3d JW |
4299 | /* helper call returns 64-bit value. */ |
4300 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
4301 | ||
dc503a8a | 4302 | /* update return register (already marked as written above) */ |
17a52670 | 4303 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 4304 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 4305 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
4306 | } else if (fn->ret_type == RET_VOID) { |
4307 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
4308 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
4309 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 4310 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 4311 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
4312 | /* remember map_ptr, so that check_map_access() |
4313 | * can check 'value_size' boundary of memory access | |
4314 | * to map element returned from bpf_map_lookup_elem() | |
4315 | */ | |
33ff9823 | 4316 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
4317 | verbose(env, |
4318 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
4319 | return -EINVAL; |
4320 | } | |
33ff9823 | 4321 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
4322 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
4323 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
4324 | if (map_value_has_spin_lock(meta.map_ptr)) |
4325 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
4326 | } else { |
4327 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
4328 | regs[BPF_REG_0].id = ++env->id_gen; | |
4329 | } | |
c64b7983 JS |
4330 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
4331 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4332 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
0f3adc28 | 4333 | regs[BPF_REG_0].id = ++env->id_gen; |
85a51f8c LB |
4334 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
4335 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4336 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
4337 | regs[BPF_REG_0].id = ++env->id_gen; | |
655a51e5 MKL |
4338 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
4339 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4340 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
4341 | regs[BPF_REG_0].id = ++env->id_gen; | |
17a52670 | 4342 | } else { |
61bd5218 | 4343 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 4344 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
4345 | return -EINVAL; |
4346 | } | |
04fd61ab | 4347 | |
0f3adc28 | 4348 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
4349 | /* For release_reference() */ |
4350 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
0f3adc28 LB |
4351 | } else if (is_acquire_function(func_id)) { |
4352 | int id = acquire_reference_state(env, insn_idx); | |
4353 | ||
4354 | if (id < 0) | |
4355 | return id; | |
4356 | /* For mark_ptr_or_null_reg() */ | |
4357 | regs[BPF_REG_0].id = id; | |
4358 | /* For release_reference() */ | |
4359 | regs[BPF_REG_0].ref_obj_id = id; | |
4360 | } | |
1b986589 | 4361 | |
849fa506 YS |
4362 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
4363 | ||
61bd5218 | 4364 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
4365 | if (err) |
4366 | return err; | |
04fd61ab | 4367 | |
c195651e YS |
4368 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
4369 | const char *err_str; | |
4370 | ||
4371 | #ifdef CONFIG_PERF_EVENTS | |
4372 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
4373 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
4374 | #else | |
4375 | err = -ENOTSUPP; | |
4376 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
4377 | #endif | |
4378 | if (err) { | |
4379 | verbose(env, err_str, func_id_name(func_id), func_id); | |
4380 | return err; | |
4381 | } | |
4382 | ||
4383 | env->prog->has_callchain_buf = true; | |
4384 | } | |
4385 | ||
969bf05e AS |
4386 | if (changes_data) |
4387 | clear_all_pkt_pointers(env); | |
4388 | return 0; | |
4389 | } | |
4390 | ||
b03c9f9f EC |
4391 | static bool signed_add_overflows(s64 a, s64 b) |
4392 | { | |
4393 | /* Do the add in u64, where overflow is well-defined */ | |
4394 | s64 res = (s64)((u64)a + (u64)b); | |
4395 | ||
4396 | if (b < 0) | |
4397 | return res > a; | |
4398 | return res < a; | |
4399 | } | |
4400 | ||
4401 | static bool signed_sub_overflows(s64 a, s64 b) | |
4402 | { | |
4403 | /* Do the sub in u64, where overflow is well-defined */ | |
4404 | s64 res = (s64)((u64)a - (u64)b); | |
4405 | ||
4406 | if (b < 0) | |
4407 | return res < a; | |
4408 | return res > a; | |
969bf05e AS |
4409 | } |
4410 | ||
bb7f0f98 AS |
4411 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
4412 | const struct bpf_reg_state *reg, | |
4413 | enum bpf_reg_type type) | |
4414 | { | |
4415 | bool known = tnum_is_const(reg->var_off); | |
4416 | s64 val = reg->var_off.value; | |
4417 | s64 smin = reg->smin_value; | |
4418 | ||
4419 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
4420 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
4421 | reg_type_str[type], val); | |
4422 | return false; | |
4423 | } | |
4424 | ||
4425 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
4426 | verbose(env, "%s pointer offset %d is not allowed\n", | |
4427 | reg_type_str[type], reg->off); | |
4428 | return false; | |
4429 | } | |
4430 | ||
4431 | if (smin == S64_MIN) { | |
4432 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
4433 | reg_type_str[type]); | |
4434 | return false; | |
4435 | } | |
4436 | ||
4437 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
4438 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
4439 | smin, reg_type_str[type]); | |
4440 | return false; | |
4441 | } | |
4442 | ||
4443 | return true; | |
4444 | } | |
4445 | ||
979d63d5 DB |
4446 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
4447 | { | |
4448 | return &env->insn_aux_data[env->insn_idx]; | |
4449 | } | |
4450 | ||
4451 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
4452 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
4453 | { | |
4454 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
4455 | (opcode == BPF_SUB && !off_is_neg); | |
4456 | u32 off; | |
4457 | ||
4458 | switch (ptr_reg->type) { | |
4459 | case PTR_TO_STACK: | |
088ec26d AI |
4460 | /* Indirect variable offset stack access is prohibited in |
4461 | * unprivileged mode so it's not handled here. | |
4462 | */ | |
979d63d5 DB |
4463 | off = ptr_reg->off + ptr_reg->var_off.value; |
4464 | if (mask_to_left) | |
4465 | *ptr_limit = MAX_BPF_STACK + off; | |
4466 | else | |
4467 | *ptr_limit = -off; | |
4468 | return 0; | |
4469 | case PTR_TO_MAP_VALUE: | |
4470 | if (mask_to_left) { | |
4471 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
4472 | } else { | |
4473 | off = ptr_reg->smin_value + ptr_reg->off; | |
4474 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
4475 | } | |
4476 | return 0; | |
4477 | default: | |
4478 | return -EINVAL; | |
4479 | } | |
4480 | } | |
4481 | ||
d3bd7413 DB |
4482 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
4483 | const struct bpf_insn *insn) | |
4484 | { | |
4485 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; | |
4486 | } | |
4487 | ||
4488 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
4489 | u32 alu_state, u32 alu_limit) | |
4490 | { | |
4491 | /* If we arrived here from different branches with different | |
4492 | * state or limits to sanitize, then this won't work. | |
4493 | */ | |
4494 | if (aux->alu_state && | |
4495 | (aux->alu_state != alu_state || | |
4496 | aux->alu_limit != alu_limit)) | |
4497 | return -EACCES; | |
4498 | ||
4499 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
4500 | aux->alu_state = alu_state; | |
4501 | aux->alu_limit = alu_limit; | |
4502 | return 0; | |
4503 | } | |
4504 | ||
4505 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
4506 | struct bpf_insn *insn) | |
4507 | { | |
4508 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4509 | ||
4510 | if (can_skip_alu_sanitation(env, insn)) | |
4511 | return 0; | |
4512 | ||
4513 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
4514 | } | |
4515 | ||
979d63d5 DB |
4516 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
4517 | struct bpf_insn *insn, | |
4518 | const struct bpf_reg_state *ptr_reg, | |
4519 | struct bpf_reg_state *dst_reg, | |
4520 | bool off_is_neg) | |
4521 | { | |
4522 | struct bpf_verifier_state *vstate = env->cur_state; | |
4523 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4524 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
4525 | u8 opcode = BPF_OP(insn->code); | |
4526 | u32 alu_state, alu_limit; | |
4527 | struct bpf_reg_state tmp; | |
4528 | bool ret; | |
4529 | ||
d3bd7413 | 4530 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
4531 | return 0; |
4532 | ||
4533 | /* We already marked aux for masking from non-speculative | |
4534 | * paths, thus we got here in the first place. We only care | |
4535 | * to explore bad access from here. | |
4536 | */ | |
4537 | if (vstate->speculative) | |
4538 | goto do_sim; | |
4539 | ||
4540 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
4541 | alu_state |= ptr_is_dst_reg ? | |
4542 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
4543 | ||
4544 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
4545 | return 0; | |
d3bd7413 | 4546 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 4547 | return -EACCES; |
979d63d5 DB |
4548 | do_sim: |
4549 | /* Simulate and find potential out-of-bounds access under | |
4550 | * speculative execution from truncation as a result of | |
4551 | * masking when off was not within expected range. If off | |
4552 | * sits in dst, then we temporarily need to move ptr there | |
4553 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
4554 | * for cases where we use K-based arithmetic in one direction | |
4555 | * and truncated reg-based in the other in order to explore | |
4556 | * bad access. | |
4557 | */ | |
4558 | if (!ptr_is_dst_reg) { | |
4559 | tmp = *dst_reg; | |
4560 | *dst_reg = *ptr_reg; | |
4561 | } | |
4562 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 4563 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
4564 | *dst_reg = tmp; |
4565 | return !ret ? -EFAULT : 0; | |
4566 | } | |
4567 | ||
f1174f77 | 4568 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
4569 | * Caller should also handle BPF_MOV case separately. |
4570 | * If we return -EACCES, caller may want to try again treating pointer as a | |
4571 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
4572 | */ | |
4573 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
4574 | struct bpf_insn *insn, | |
4575 | const struct bpf_reg_state *ptr_reg, | |
4576 | const struct bpf_reg_state *off_reg) | |
969bf05e | 4577 | { |
f4d7e40a AS |
4578 | struct bpf_verifier_state *vstate = env->cur_state; |
4579 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4580 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 4581 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
4582 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
4583 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
4584 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
4585 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 4586 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 4587 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 4588 | int ret; |
969bf05e | 4589 | |
f1174f77 | 4590 | dst_reg = ®s[dst]; |
969bf05e | 4591 | |
6f16101e DB |
4592 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
4593 | smin_val > smax_val || umin_val > umax_val) { | |
4594 | /* Taint dst register if offset had invalid bounds derived from | |
4595 | * e.g. dead branches. | |
4596 | */ | |
f54c7898 | 4597 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 4598 | return 0; |
f1174f77 EC |
4599 | } |
4600 | ||
4601 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
4602 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
82abbf8d AS |
4603 | verbose(env, |
4604 | "R%d 32-bit pointer arithmetic prohibited\n", | |
4605 | dst); | |
f1174f77 | 4606 | return -EACCES; |
969bf05e AS |
4607 | } |
4608 | ||
aad2eeaf JS |
4609 | switch (ptr_reg->type) { |
4610 | case PTR_TO_MAP_VALUE_OR_NULL: | |
4611 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
4612 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 4613 | return -EACCES; |
aad2eeaf JS |
4614 | case CONST_PTR_TO_MAP: |
4615 | case PTR_TO_PACKET_END: | |
c64b7983 JS |
4616 | case PTR_TO_SOCKET: |
4617 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
4618 | case PTR_TO_SOCK_COMMON: |
4619 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
4620 | case PTR_TO_TCP_SOCK: |
4621 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 4622 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
4623 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
4624 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 4625 | return -EACCES; |
9d7eceed DB |
4626 | case PTR_TO_MAP_VALUE: |
4627 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
4628 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
4629 | off_reg == dst_reg ? dst : src); | |
4630 | return -EACCES; | |
4631 | } | |
4632 | /* fall-through */ | |
aad2eeaf JS |
4633 | default: |
4634 | break; | |
f1174f77 EC |
4635 | } |
4636 | ||
4637 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
4638 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 4639 | */ |
f1174f77 EC |
4640 | dst_reg->type = ptr_reg->type; |
4641 | dst_reg->id = ptr_reg->id; | |
969bf05e | 4642 | |
bb7f0f98 AS |
4643 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
4644 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
4645 | return -EINVAL; | |
4646 | ||
f1174f77 EC |
4647 | switch (opcode) { |
4648 | case BPF_ADD: | |
979d63d5 DB |
4649 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
4650 | if (ret < 0) { | |
4651 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
4652 | return ret; | |
4653 | } | |
f1174f77 EC |
4654 | /* We can take a fixed offset as long as it doesn't overflow |
4655 | * the s32 'off' field | |
969bf05e | 4656 | */ |
b03c9f9f EC |
4657 | if (known && (ptr_reg->off + smin_val == |
4658 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 4659 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
4660 | dst_reg->smin_value = smin_ptr; |
4661 | dst_reg->smax_value = smax_ptr; | |
4662 | dst_reg->umin_value = umin_ptr; | |
4663 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 4664 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 4665 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 4666 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
4667 | break; |
4668 | } | |
f1174f77 EC |
4669 | /* A new variable offset is created. Note that off_reg->off |
4670 | * == 0, since it's a scalar. | |
4671 | * dst_reg gets the pointer type and since some positive | |
4672 | * integer value was added to the pointer, give it a new 'id' | |
4673 | * if it's a PTR_TO_PACKET. | |
4674 | * this creates a new 'base' pointer, off_reg (variable) gets | |
4675 | * added into the variable offset, and we copy the fixed offset | |
4676 | * from ptr_reg. | |
969bf05e | 4677 | */ |
b03c9f9f EC |
4678 | if (signed_add_overflows(smin_ptr, smin_val) || |
4679 | signed_add_overflows(smax_ptr, smax_val)) { | |
4680 | dst_reg->smin_value = S64_MIN; | |
4681 | dst_reg->smax_value = S64_MAX; | |
4682 | } else { | |
4683 | dst_reg->smin_value = smin_ptr + smin_val; | |
4684 | dst_reg->smax_value = smax_ptr + smax_val; | |
4685 | } | |
4686 | if (umin_ptr + umin_val < umin_ptr || | |
4687 | umax_ptr + umax_val < umax_ptr) { | |
4688 | dst_reg->umin_value = 0; | |
4689 | dst_reg->umax_value = U64_MAX; | |
4690 | } else { | |
4691 | dst_reg->umin_value = umin_ptr + umin_val; | |
4692 | dst_reg->umax_value = umax_ptr + umax_val; | |
4693 | } | |
f1174f77 EC |
4694 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
4695 | dst_reg->off = ptr_reg->off; | |
0962590e | 4696 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 4697 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
4698 | dst_reg->id = ++env->id_gen; |
4699 | /* something was added to pkt_ptr, set range to zero */ | |
0962590e | 4700 | dst_reg->raw = 0; |
f1174f77 EC |
4701 | } |
4702 | break; | |
4703 | case BPF_SUB: | |
979d63d5 DB |
4704 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
4705 | if (ret < 0) { | |
4706 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
4707 | return ret; | |
4708 | } | |
f1174f77 EC |
4709 | if (dst_reg == off_reg) { |
4710 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
4711 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
4712 | dst); | |
f1174f77 EC |
4713 | return -EACCES; |
4714 | } | |
4715 | /* We don't allow subtraction from FP, because (according to | |
4716 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
4717 | * be able to deal with it. | |
969bf05e | 4718 | */ |
f1174f77 | 4719 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
4720 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
4721 | dst); | |
f1174f77 EC |
4722 | return -EACCES; |
4723 | } | |
b03c9f9f EC |
4724 | if (known && (ptr_reg->off - smin_val == |
4725 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 4726 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
4727 | dst_reg->smin_value = smin_ptr; |
4728 | dst_reg->smax_value = smax_ptr; | |
4729 | dst_reg->umin_value = umin_ptr; | |
4730 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
4731 | dst_reg->var_off = ptr_reg->var_off; |
4732 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 4733 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 4734 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
4735 | break; |
4736 | } | |
f1174f77 EC |
4737 | /* A new variable offset is created. If the subtrahend is known |
4738 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 4739 | */ |
b03c9f9f EC |
4740 | if (signed_sub_overflows(smin_ptr, smax_val) || |
4741 | signed_sub_overflows(smax_ptr, smin_val)) { | |
4742 | /* Overflow possible, we know nothing */ | |
4743 | dst_reg->smin_value = S64_MIN; | |
4744 | dst_reg->smax_value = S64_MAX; | |
4745 | } else { | |
4746 | dst_reg->smin_value = smin_ptr - smax_val; | |
4747 | dst_reg->smax_value = smax_ptr - smin_val; | |
4748 | } | |
4749 | if (umin_ptr < umax_val) { | |
4750 | /* Overflow possible, we know nothing */ | |
4751 | dst_reg->umin_value = 0; | |
4752 | dst_reg->umax_value = U64_MAX; | |
4753 | } else { | |
4754 | /* Cannot overflow (as long as bounds are consistent) */ | |
4755 | dst_reg->umin_value = umin_ptr - umax_val; | |
4756 | dst_reg->umax_value = umax_ptr - umin_val; | |
4757 | } | |
f1174f77 EC |
4758 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
4759 | dst_reg->off = ptr_reg->off; | |
0962590e | 4760 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 4761 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
4762 | dst_reg->id = ++env->id_gen; |
4763 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 4764 | if (smin_val < 0) |
0962590e | 4765 | dst_reg->raw = 0; |
43188702 | 4766 | } |
f1174f77 EC |
4767 | break; |
4768 | case BPF_AND: | |
4769 | case BPF_OR: | |
4770 | case BPF_XOR: | |
82abbf8d AS |
4771 | /* bitwise ops on pointers are troublesome, prohibit. */ |
4772 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
4773 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
4774 | return -EACCES; |
4775 | default: | |
4776 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
4777 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
4778 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 4779 | return -EACCES; |
43188702 JF |
4780 | } |
4781 | ||
bb7f0f98 AS |
4782 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
4783 | return -EINVAL; | |
4784 | ||
b03c9f9f EC |
4785 | __update_reg_bounds(dst_reg); |
4786 | __reg_deduce_bounds(dst_reg); | |
4787 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
4788 | |
4789 | /* For unprivileged we require that resulting offset must be in bounds | |
4790 | * in order to be able to sanitize access later on. | |
4791 | */ | |
e4298d25 DB |
4792 | if (!env->allow_ptr_leaks) { |
4793 | if (dst_reg->type == PTR_TO_MAP_VALUE && | |
4794 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
4795 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
4796 | "prohibited for !root\n", dst); | |
4797 | return -EACCES; | |
4798 | } else if (dst_reg->type == PTR_TO_STACK && | |
4799 | check_stack_access(env, dst_reg, dst_reg->off + | |
4800 | dst_reg->var_off.value, 1)) { | |
4801 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
4802 | "prohibited for !root\n", dst); | |
4803 | return -EACCES; | |
4804 | } | |
0d6303db DB |
4805 | } |
4806 | ||
43188702 JF |
4807 | return 0; |
4808 | } | |
4809 | ||
468f6eaf JH |
4810 | /* WARNING: This function does calculations on 64-bit values, but the actual |
4811 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
4812 | * need extra checks in the 32-bit case. | |
4813 | */ | |
f1174f77 EC |
4814 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
4815 | struct bpf_insn *insn, | |
4816 | struct bpf_reg_state *dst_reg, | |
4817 | struct bpf_reg_state src_reg) | |
969bf05e | 4818 | { |
638f5b90 | 4819 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 4820 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 4821 | bool src_known, dst_known; |
b03c9f9f EC |
4822 | s64 smin_val, smax_val; |
4823 | u64 umin_val, umax_val; | |
468f6eaf | 4824 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
4825 | u32 dst = insn->dst_reg; |
4826 | int ret; | |
48461135 | 4827 | |
b799207e JH |
4828 | if (insn_bitness == 32) { |
4829 | /* Relevant for 32-bit RSH: Information can propagate towards | |
4830 | * LSB, so it isn't sufficient to only truncate the output to | |
4831 | * 32 bits. | |
4832 | */ | |
4833 | coerce_reg_to_size(dst_reg, 4); | |
4834 | coerce_reg_to_size(&src_reg, 4); | |
4835 | } | |
4836 | ||
b03c9f9f EC |
4837 | smin_val = src_reg.smin_value; |
4838 | smax_val = src_reg.smax_value; | |
4839 | umin_val = src_reg.umin_value; | |
4840 | umax_val = src_reg.umax_value; | |
f1174f77 EC |
4841 | src_known = tnum_is_const(src_reg.var_off); |
4842 | dst_known = tnum_is_const(dst_reg->var_off); | |
f23cc643 | 4843 | |
6f16101e DB |
4844 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
4845 | smin_val > smax_val || umin_val > umax_val) { | |
4846 | /* Taint dst register if offset had invalid bounds derived from | |
4847 | * e.g. dead branches. | |
4848 | */ | |
f54c7898 | 4849 | __mark_reg_unknown(env, dst_reg); |
6f16101e DB |
4850 | return 0; |
4851 | } | |
4852 | ||
bb7f0f98 AS |
4853 | if (!src_known && |
4854 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 4855 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
4856 | return 0; |
4857 | } | |
4858 | ||
48461135 JB |
4859 | switch (opcode) { |
4860 | case BPF_ADD: | |
d3bd7413 DB |
4861 | ret = sanitize_val_alu(env, insn); |
4862 | if (ret < 0) { | |
4863 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
4864 | return ret; | |
4865 | } | |
b03c9f9f EC |
4866 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
4867 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
4868 | dst_reg->smin_value = S64_MIN; | |
4869 | dst_reg->smax_value = S64_MAX; | |
4870 | } else { | |
4871 | dst_reg->smin_value += smin_val; | |
4872 | dst_reg->smax_value += smax_val; | |
4873 | } | |
4874 | if (dst_reg->umin_value + umin_val < umin_val || | |
4875 | dst_reg->umax_value + umax_val < umax_val) { | |
4876 | dst_reg->umin_value = 0; | |
4877 | dst_reg->umax_value = U64_MAX; | |
4878 | } else { | |
4879 | dst_reg->umin_value += umin_val; | |
4880 | dst_reg->umax_value += umax_val; | |
4881 | } | |
f1174f77 | 4882 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
4883 | break; |
4884 | case BPF_SUB: | |
d3bd7413 DB |
4885 | ret = sanitize_val_alu(env, insn); |
4886 | if (ret < 0) { | |
4887 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
4888 | return ret; | |
4889 | } | |
b03c9f9f EC |
4890 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
4891 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
4892 | /* Overflow possible, we know nothing */ | |
4893 | dst_reg->smin_value = S64_MIN; | |
4894 | dst_reg->smax_value = S64_MAX; | |
4895 | } else { | |
4896 | dst_reg->smin_value -= smax_val; | |
4897 | dst_reg->smax_value -= smin_val; | |
4898 | } | |
4899 | if (dst_reg->umin_value < umax_val) { | |
4900 | /* Overflow possible, we know nothing */ | |
4901 | dst_reg->umin_value = 0; | |
4902 | dst_reg->umax_value = U64_MAX; | |
4903 | } else { | |
4904 | /* Cannot overflow (as long as bounds are consistent) */ | |
4905 | dst_reg->umin_value -= umax_val; | |
4906 | dst_reg->umax_value -= umin_val; | |
4907 | } | |
f1174f77 | 4908 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
4909 | break; |
4910 | case BPF_MUL: | |
b03c9f9f EC |
4911 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
4912 | if (smin_val < 0 || dst_reg->smin_value < 0) { | |
f1174f77 | 4913 | /* Ain't nobody got time to multiply that sign */ |
b03c9f9f EC |
4914 | __mark_reg_unbounded(dst_reg); |
4915 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
4916 | break; |
4917 | } | |
b03c9f9f EC |
4918 | /* Both values are positive, so we can work with unsigned and |
4919 | * copy the result to signed (unless it exceeds S64_MAX). | |
f1174f77 | 4920 | */ |
b03c9f9f EC |
4921 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
4922 | /* Potential overflow, we know nothing */ | |
4923 | __mark_reg_unbounded(dst_reg); | |
4924 | /* (except what we can learn from the var_off) */ | |
4925 | __update_reg_bounds(dst_reg); | |
4926 | break; | |
4927 | } | |
4928 | dst_reg->umin_value *= umin_val; | |
4929 | dst_reg->umax_value *= umax_val; | |
4930 | if (dst_reg->umax_value > S64_MAX) { | |
4931 | /* Overflow possible, we know nothing */ | |
4932 | dst_reg->smin_value = S64_MIN; | |
4933 | dst_reg->smax_value = S64_MAX; | |
4934 | } else { | |
4935 | dst_reg->smin_value = dst_reg->umin_value; | |
4936 | dst_reg->smax_value = dst_reg->umax_value; | |
4937 | } | |
48461135 JB |
4938 | break; |
4939 | case BPF_AND: | |
f1174f77 | 4940 | if (src_known && dst_known) { |
b03c9f9f EC |
4941 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
4942 | src_reg.var_off.value); | |
f1174f77 EC |
4943 | break; |
4944 | } | |
b03c9f9f EC |
4945 | /* We get our minimum from the var_off, since that's inherently |
4946 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
f23cc643 | 4947 | */ |
f1174f77 | 4948 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
b03c9f9f EC |
4949 | dst_reg->umin_value = dst_reg->var_off.value; |
4950 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
4951 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
4952 | /* Lose signed bounds when ANDing negative numbers, | |
4953 | * ain't nobody got time for that. | |
4954 | */ | |
4955 | dst_reg->smin_value = S64_MIN; | |
4956 | dst_reg->smax_value = S64_MAX; | |
4957 | } else { | |
4958 | /* ANDing two positives gives a positive, so safe to | |
4959 | * cast result into s64. | |
4960 | */ | |
4961 | dst_reg->smin_value = dst_reg->umin_value; | |
4962 | dst_reg->smax_value = dst_reg->umax_value; | |
4963 | } | |
4964 | /* We may learn something more from the var_off */ | |
4965 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
4966 | break; |
4967 | case BPF_OR: | |
4968 | if (src_known && dst_known) { | |
b03c9f9f EC |
4969 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
4970 | src_reg.var_off.value); | |
f1174f77 EC |
4971 | break; |
4972 | } | |
b03c9f9f EC |
4973 | /* We get our maximum from the var_off, and our minimum is the |
4974 | * maximum of the operands' minima | |
f1174f77 EC |
4975 | */ |
4976 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); | |
b03c9f9f EC |
4977 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
4978 | dst_reg->umax_value = dst_reg->var_off.value | | |
4979 | dst_reg->var_off.mask; | |
4980 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
4981 | /* Lose signed bounds when ORing negative numbers, | |
4982 | * ain't nobody got time for that. | |
4983 | */ | |
4984 | dst_reg->smin_value = S64_MIN; | |
4985 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 4986 | } else { |
b03c9f9f EC |
4987 | /* ORing two positives gives a positive, so safe to |
4988 | * cast result into s64. | |
4989 | */ | |
4990 | dst_reg->smin_value = dst_reg->umin_value; | |
4991 | dst_reg->smax_value = dst_reg->umax_value; | |
f1174f77 | 4992 | } |
b03c9f9f EC |
4993 | /* We may learn something more from the var_off */ |
4994 | __update_reg_bounds(dst_reg); | |
48461135 JB |
4995 | break; |
4996 | case BPF_LSH: | |
468f6eaf JH |
4997 | if (umax_val >= insn_bitness) { |
4998 | /* Shifts greater than 31 or 63 are undefined. | |
4999 | * This includes shifts by a negative number. | |
b03c9f9f | 5000 | */ |
61bd5218 | 5001 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
5002 | break; |
5003 | } | |
b03c9f9f EC |
5004 | /* We lose all sign bit information (except what we can pick |
5005 | * up from var_off) | |
48461135 | 5006 | */ |
b03c9f9f EC |
5007 | dst_reg->smin_value = S64_MIN; |
5008 | dst_reg->smax_value = S64_MAX; | |
5009 | /* If we might shift our top bit out, then we know nothing */ | |
5010 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
5011 | dst_reg->umin_value = 0; | |
5012 | dst_reg->umax_value = U64_MAX; | |
d1174416 | 5013 | } else { |
b03c9f9f EC |
5014 | dst_reg->umin_value <<= umin_val; |
5015 | dst_reg->umax_value <<= umax_val; | |
d1174416 | 5016 | } |
afbe1a5b | 5017 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
5018 | /* We may learn something more from the var_off */ |
5019 | __update_reg_bounds(dst_reg); | |
48461135 JB |
5020 | break; |
5021 | case BPF_RSH: | |
468f6eaf JH |
5022 | if (umax_val >= insn_bitness) { |
5023 | /* Shifts greater than 31 or 63 are undefined. | |
5024 | * This includes shifts by a negative number. | |
b03c9f9f | 5025 | */ |
61bd5218 | 5026 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
5027 | break; |
5028 | } | |
4374f256 EC |
5029 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
5030 | * be negative, then either: | |
5031 | * 1) src_reg might be zero, so the sign bit of the result is | |
5032 | * unknown, so we lose our signed bounds | |
5033 | * 2) it's known negative, thus the unsigned bounds capture the | |
5034 | * signed bounds | |
5035 | * 3) the signed bounds cross zero, so they tell us nothing | |
5036 | * about the result | |
5037 | * If the value in dst_reg is known nonnegative, then again the | |
5038 | * unsigned bounts capture the signed bounds. | |
5039 | * Thus, in all cases it suffices to blow away our signed bounds | |
5040 | * and rely on inferring new ones from the unsigned bounds and | |
5041 | * var_off of the result. | |
5042 | */ | |
5043 | dst_reg->smin_value = S64_MIN; | |
5044 | dst_reg->smax_value = S64_MAX; | |
afbe1a5b | 5045 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
5046 | dst_reg->umin_value >>= umax_val; |
5047 | dst_reg->umax_value >>= umin_val; | |
5048 | /* We may learn something more from the var_off */ | |
5049 | __update_reg_bounds(dst_reg); | |
48461135 | 5050 | break; |
9cbe1f5a YS |
5051 | case BPF_ARSH: |
5052 | if (umax_val >= insn_bitness) { | |
5053 | /* Shifts greater than 31 or 63 are undefined. | |
5054 | * This includes shifts by a negative number. | |
5055 | */ | |
5056 | mark_reg_unknown(env, regs, insn->dst_reg); | |
5057 | break; | |
5058 | } | |
5059 | ||
5060 | /* Upon reaching here, src_known is true and | |
5061 | * umax_val is equal to umin_val. | |
5062 | */ | |
5063 | dst_reg->smin_value >>= umin_val; | |
5064 | dst_reg->smax_value >>= umin_val; | |
5065 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); | |
5066 | ||
5067 | /* blow away the dst_reg umin_value/umax_value and rely on | |
5068 | * dst_reg var_off to refine the result. | |
5069 | */ | |
5070 | dst_reg->umin_value = 0; | |
5071 | dst_reg->umax_value = U64_MAX; | |
5072 | __update_reg_bounds(dst_reg); | |
5073 | break; | |
48461135 | 5074 | default: |
61bd5218 | 5075 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
5076 | break; |
5077 | } | |
5078 | ||
468f6eaf JH |
5079 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
5080 | /* 32-bit ALU ops are (32,32)->32 */ | |
5081 | coerce_reg_to_size(dst_reg, 4); | |
468f6eaf JH |
5082 | } |
5083 | ||
b03c9f9f EC |
5084 | __reg_deduce_bounds(dst_reg); |
5085 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
5086 | return 0; |
5087 | } | |
5088 | ||
5089 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
5090 | * and var_off. | |
5091 | */ | |
5092 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
5093 | struct bpf_insn *insn) | |
5094 | { | |
f4d7e40a AS |
5095 | struct bpf_verifier_state *vstate = env->cur_state; |
5096 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5097 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
5098 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
5099 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 5100 | int err; |
f1174f77 EC |
5101 | |
5102 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
5103 | src_reg = NULL; |
5104 | if (dst_reg->type != SCALAR_VALUE) | |
5105 | ptr_reg = dst_reg; | |
5106 | if (BPF_SRC(insn->code) == BPF_X) { | |
5107 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
5108 | if (src_reg->type != SCALAR_VALUE) { |
5109 | if (dst_reg->type != SCALAR_VALUE) { | |
5110 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
5111 | * an arbitrary scalar. Disallow all math except |
5112 | * pointer subtraction | |
f1174f77 | 5113 | */ |
dd066823 | 5114 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
5115 | mark_reg_unknown(env, regs, insn->dst_reg); |
5116 | return 0; | |
f1174f77 | 5117 | } |
82abbf8d AS |
5118 | verbose(env, "R%d pointer %s pointer prohibited\n", |
5119 | insn->dst_reg, | |
5120 | bpf_alu_string[opcode >> 4]); | |
5121 | return -EACCES; | |
f1174f77 EC |
5122 | } else { |
5123 | /* scalar += pointer | |
5124 | * This is legal, but we have to reverse our | |
5125 | * src/dest handling in computing the range | |
5126 | */ | |
b5dc0163 AS |
5127 | err = mark_chain_precision(env, insn->dst_reg); |
5128 | if (err) | |
5129 | return err; | |
82abbf8d AS |
5130 | return adjust_ptr_min_max_vals(env, insn, |
5131 | src_reg, dst_reg); | |
f1174f77 EC |
5132 | } |
5133 | } else if (ptr_reg) { | |
5134 | /* pointer += scalar */ | |
b5dc0163 AS |
5135 | err = mark_chain_precision(env, insn->src_reg); |
5136 | if (err) | |
5137 | return err; | |
82abbf8d AS |
5138 | return adjust_ptr_min_max_vals(env, insn, |
5139 | dst_reg, src_reg); | |
f1174f77 EC |
5140 | } |
5141 | } else { | |
5142 | /* Pretend the src is a reg with a known value, since we only | |
5143 | * need to be able to read from this state. | |
5144 | */ | |
5145 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 5146 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 5147 | src_reg = &off_reg; |
82abbf8d AS |
5148 | if (ptr_reg) /* pointer += K */ |
5149 | return adjust_ptr_min_max_vals(env, insn, | |
5150 | ptr_reg, src_reg); | |
f1174f77 EC |
5151 | } |
5152 | ||
5153 | /* Got here implies adding two SCALAR_VALUEs */ | |
5154 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 5155 | print_verifier_state(env, state); |
61bd5218 | 5156 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
5157 | return -EINVAL; |
5158 | } | |
5159 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 5160 | print_verifier_state(env, state); |
61bd5218 | 5161 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
5162 | return -EINVAL; |
5163 | } | |
5164 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
5165 | } |
5166 | ||
17a52670 | 5167 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 5168 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 5169 | { |
638f5b90 | 5170 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
5171 | u8 opcode = BPF_OP(insn->code); |
5172 | int err; | |
5173 | ||
5174 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
5175 | if (opcode == BPF_NEG) { | |
5176 | if (BPF_SRC(insn->code) != 0 || | |
5177 | insn->src_reg != BPF_REG_0 || | |
5178 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 5179 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
5180 | return -EINVAL; |
5181 | } | |
5182 | } else { | |
5183 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
5184 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
5185 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 5186 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
5187 | return -EINVAL; |
5188 | } | |
5189 | } | |
5190 | ||
5191 | /* check src operand */ | |
dc503a8a | 5192 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5193 | if (err) |
5194 | return err; | |
5195 | ||
1be7f75d | 5196 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 5197 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
5198 | insn->dst_reg); |
5199 | return -EACCES; | |
5200 | } | |
5201 | ||
17a52670 | 5202 | /* check dest operand */ |
dc503a8a | 5203 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
5204 | if (err) |
5205 | return err; | |
5206 | ||
5207 | } else if (opcode == BPF_MOV) { | |
5208 | ||
5209 | if (BPF_SRC(insn->code) == BPF_X) { | |
5210 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 5211 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
5212 | return -EINVAL; |
5213 | } | |
5214 | ||
5215 | /* check src operand */ | |
dc503a8a | 5216 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5217 | if (err) |
5218 | return err; | |
5219 | } else { | |
5220 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 5221 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
5222 | return -EINVAL; |
5223 | } | |
5224 | } | |
5225 | ||
fbeb1603 AF |
5226 | /* check dest operand, mark as required later */ |
5227 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
5228 | if (err) |
5229 | return err; | |
5230 | ||
5231 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
5232 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
5233 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
5234 | ||
17a52670 AS |
5235 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
5236 | /* case: R1 = R2 | |
5237 | * copy register state to dest reg | |
5238 | */ | |
e434b8cd JW |
5239 | *dst_reg = *src_reg; |
5240 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 5241 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 5242 | } else { |
f1174f77 | 5243 | /* R1 = (u32) R2 */ |
1be7f75d | 5244 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
5245 | verbose(env, |
5246 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
5247 | insn->src_reg); |
5248 | return -EACCES; | |
e434b8cd JW |
5249 | } else if (src_reg->type == SCALAR_VALUE) { |
5250 | *dst_reg = *src_reg; | |
5251 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 5252 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
5253 | } else { |
5254 | mark_reg_unknown(env, regs, | |
5255 | insn->dst_reg); | |
1be7f75d | 5256 | } |
e434b8cd | 5257 | coerce_reg_to_size(dst_reg, 4); |
17a52670 AS |
5258 | } |
5259 | } else { | |
5260 | /* case: R = imm | |
5261 | * remember the value we stored into this reg | |
5262 | */ | |
fbeb1603 AF |
5263 | /* clear any state __mark_reg_known doesn't set */ |
5264 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 5265 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
5266 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
5267 | __mark_reg_known(regs + insn->dst_reg, | |
5268 | insn->imm); | |
5269 | } else { | |
5270 | __mark_reg_known(regs + insn->dst_reg, | |
5271 | (u32)insn->imm); | |
5272 | } | |
17a52670 AS |
5273 | } |
5274 | ||
5275 | } else if (opcode > BPF_END) { | |
61bd5218 | 5276 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
5277 | return -EINVAL; |
5278 | ||
5279 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
5280 | ||
17a52670 AS |
5281 | if (BPF_SRC(insn->code) == BPF_X) { |
5282 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 5283 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
5284 | return -EINVAL; |
5285 | } | |
5286 | /* check src1 operand */ | |
dc503a8a | 5287 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5288 | if (err) |
5289 | return err; | |
5290 | } else { | |
5291 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 5292 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
5293 | return -EINVAL; |
5294 | } | |
5295 | } | |
5296 | ||
5297 | /* check src2 operand */ | |
dc503a8a | 5298 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5299 | if (err) |
5300 | return err; | |
5301 | ||
5302 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
5303 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 5304 | verbose(env, "div by zero\n"); |
17a52670 AS |
5305 | return -EINVAL; |
5306 | } | |
5307 | ||
229394e8 RV |
5308 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
5309 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
5310 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
5311 | ||
5312 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 5313 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
5314 | return -EINVAL; |
5315 | } | |
5316 | } | |
5317 | ||
1a0dc1ac | 5318 | /* check dest operand */ |
dc503a8a | 5319 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
5320 | if (err) |
5321 | return err; | |
5322 | ||
f1174f77 | 5323 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
5324 | } |
5325 | ||
5326 | return 0; | |
5327 | } | |
5328 | ||
c6a9efa1 PC |
5329 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
5330 | struct bpf_reg_state *dst_reg, | |
5331 | enum bpf_reg_type type, u16 new_range) | |
5332 | { | |
5333 | struct bpf_reg_state *reg; | |
5334 | int i; | |
5335 | ||
5336 | for (i = 0; i < MAX_BPF_REG; i++) { | |
5337 | reg = &state->regs[i]; | |
5338 | if (reg->type == type && reg->id == dst_reg->id) | |
5339 | /* keep the maximum range already checked */ | |
5340 | reg->range = max(reg->range, new_range); | |
5341 | } | |
5342 | ||
5343 | bpf_for_each_spilled_reg(i, state, reg) { | |
5344 | if (!reg) | |
5345 | continue; | |
5346 | if (reg->type == type && reg->id == dst_reg->id) | |
5347 | reg->range = max(reg->range, new_range); | |
5348 | } | |
5349 | } | |
5350 | ||
f4d7e40a | 5351 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 5352 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 5353 | enum bpf_reg_type type, |
fb2a311a | 5354 | bool range_right_open) |
969bf05e | 5355 | { |
fb2a311a | 5356 | u16 new_range; |
c6a9efa1 | 5357 | int i; |
2d2be8ca | 5358 | |
fb2a311a DB |
5359 | if (dst_reg->off < 0 || |
5360 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
5361 | /* This doesn't give us any range */ |
5362 | return; | |
5363 | ||
b03c9f9f EC |
5364 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
5365 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
5366 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
5367 | * than pkt_end, but that's because it's also less than pkt. | |
5368 | */ | |
5369 | return; | |
5370 | ||
fb2a311a DB |
5371 | new_range = dst_reg->off; |
5372 | if (range_right_open) | |
5373 | new_range--; | |
5374 | ||
5375 | /* Examples for register markings: | |
2d2be8ca | 5376 | * |
fb2a311a | 5377 | * pkt_data in dst register: |
2d2be8ca DB |
5378 | * |
5379 | * r2 = r3; | |
5380 | * r2 += 8; | |
5381 | * if (r2 > pkt_end) goto <handle exception> | |
5382 | * <access okay> | |
5383 | * | |
b4e432f1 DB |
5384 | * r2 = r3; |
5385 | * r2 += 8; | |
5386 | * if (r2 < pkt_end) goto <access okay> | |
5387 | * <handle exception> | |
5388 | * | |
2d2be8ca DB |
5389 | * Where: |
5390 | * r2 == dst_reg, pkt_end == src_reg | |
5391 | * r2=pkt(id=n,off=8,r=0) | |
5392 | * r3=pkt(id=n,off=0,r=0) | |
5393 | * | |
fb2a311a | 5394 | * pkt_data in src register: |
2d2be8ca DB |
5395 | * |
5396 | * r2 = r3; | |
5397 | * r2 += 8; | |
5398 | * if (pkt_end >= r2) goto <access okay> | |
5399 | * <handle exception> | |
5400 | * | |
b4e432f1 DB |
5401 | * r2 = r3; |
5402 | * r2 += 8; | |
5403 | * if (pkt_end <= r2) goto <handle exception> | |
5404 | * <access okay> | |
5405 | * | |
2d2be8ca DB |
5406 | * Where: |
5407 | * pkt_end == dst_reg, r2 == src_reg | |
5408 | * r2=pkt(id=n,off=8,r=0) | |
5409 | * r3=pkt(id=n,off=0,r=0) | |
5410 | * | |
5411 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
5412 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
5413 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
5414 | * the check. | |
969bf05e | 5415 | */ |
2d2be8ca | 5416 | |
f1174f77 EC |
5417 | /* If our ids match, then we must have the same max_value. And we |
5418 | * don't care about the other reg's fixed offset, since if it's too big | |
5419 | * the range won't allow anything. | |
5420 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
5421 | */ | |
c6a9efa1 PC |
5422 | for (i = 0; i <= vstate->curframe; i++) |
5423 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
5424 | new_range); | |
969bf05e AS |
5425 | } |
5426 | ||
4f7b3e82 AS |
5427 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
5428 | * and return: | |
5429 | * 1 - branch will be taken and "goto target" will be executed | |
5430 | * 0 - branch will not be taken and fall-through to next insn | |
5431 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] | |
5432 | */ | |
092ed096 JW |
5433 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
5434 | bool is_jmp32) | |
4f7b3e82 | 5435 | { |
092ed096 | 5436 | struct bpf_reg_state reg_lo; |
a72dafaf JW |
5437 | s64 sval; |
5438 | ||
4f7b3e82 AS |
5439 | if (__is_pointer_value(false, reg)) |
5440 | return -1; | |
5441 | ||
092ed096 JW |
5442 | if (is_jmp32) { |
5443 | reg_lo = *reg; | |
5444 | reg = ®_lo; | |
5445 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size | |
5446 | * could truncate high bits and update umin/umax according to | |
5447 | * information of low bits. | |
5448 | */ | |
5449 | coerce_reg_to_size(reg, 4); | |
5450 | /* smin/smax need special handling. For example, after coerce, | |
5451 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when | |
5452 | * used as operand to JMP32. It is a negative number from s32's | |
5453 | * point of view, while it is a positive number when seen as | |
5454 | * s64. The smin/smax are kept as s64, therefore, when used with | |
5455 | * JMP32, they need to be transformed into s32, then sign | |
5456 | * extended back to s64. | |
5457 | * | |
5458 | * Also, smin/smax were copied from umin/umax. If umin/umax has | |
5459 | * different sign bit, then min/max relationship doesn't | |
5460 | * maintain after casting into s32, for this case, set smin/smax | |
5461 | * to safest range. | |
5462 | */ | |
5463 | if ((reg->umax_value ^ reg->umin_value) & | |
5464 | (1ULL << 31)) { | |
5465 | reg->smin_value = S32_MIN; | |
5466 | reg->smax_value = S32_MAX; | |
5467 | } | |
5468 | reg->smin_value = (s64)(s32)reg->smin_value; | |
5469 | reg->smax_value = (s64)(s32)reg->smax_value; | |
5470 | ||
5471 | val = (u32)val; | |
5472 | sval = (s64)(s32)val; | |
5473 | } else { | |
5474 | sval = (s64)val; | |
5475 | } | |
a72dafaf | 5476 | |
4f7b3e82 AS |
5477 | switch (opcode) { |
5478 | case BPF_JEQ: | |
5479 | if (tnum_is_const(reg->var_off)) | |
5480 | return !!tnum_equals_const(reg->var_off, val); | |
5481 | break; | |
5482 | case BPF_JNE: | |
5483 | if (tnum_is_const(reg->var_off)) | |
5484 | return !tnum_equals_const(reg->var_off, val); | |
5485 | break; | |
960ea056 JK |
5486 | case BPF_JSET: |
5487 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
5488 | return 1; | |
5489 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
5490 | return 0; | |
5491 | break; | |
4f7b3e82 AS |
5492 | case BPF_JGT: |
5493 | if (reg->umin_value > val) | |
5494 | return 1; | |
5495 | else if (reg->umax_value <= val) | |
5496 | return 0; | |
5497 | break; | |
5498 | case BPF_JSGT: | |
a72dafaf | 5499 | if (reg->smin_value > sval) |
4f7b3e82 | 5500 | return 1; |
a72dafaf | 5501 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
5502 | return 0; |
5503 | break; | |
5504 | case BPF_JLT: | |
5505 | if (reg->umax_value < val) | |
5506 | return 1; | |
5507 | else if (reg->umin_value >= val) | |
5508 | return 0; | |
5509 | break; | |
5510 | case BPF_JSLT: | |
a72dafaf | 5511 | if (reg->smax_value < sval) |
4f7b3e82 | 5512 | return 1; |
a72dafaf | 5513 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
5514 | return 0; |
5515 | break; | |
5516 | case BPF_JGE: | |
5517 | if (reg->umin_value >= val) | |
5518 | return 1; | |
5519 | else if (reg->umax_value < val) | |
5520 | return 0; | |
5521 | break; | |
5522 | case BPF_JSGE: | |
a72dafaf | 5523 | if (reg->smin_value >= sval) |
4f7b3e82 | 5524 | return 1; |
a72dafaf | 5525 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
5526 | return 0; |
5527 | break; | |
5528 | case BPF_JLE: | |
5529 | if (reg->umax_value <= val) | |
5530 | return 1; | |
5531 | else if (reg->umin_value > val) | |
5532 | return 0; | |
5533 | break; | |
5534 | case BPF_JSLE: | |
a72dafaf | 5535 | if (reg->smax_value <= sval) |
4f7b3e82 | 5536 | return 1; |
a72dafaf | 5537 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
5538 | return 0; |
5539 | break; | |
5540 | } | |
5541 | ||
5542 | return -1; | |
5543 | } | |
5544 | ||
092ed096 JW |
5545 | /* Generate min value of the high 32-bit from TNUM info. */ |
5546 | static u64 gen_hi_min(struct tnum var) | |
5547 | { | |
5548 | return var.value & ~0xffffffffULL; | |
5549 | } | |
5550 | ||
5551 | /* Generate max value of the high 32-bit from TNUM info. */ | |
5552 | static u64 gen_hi_max(struct tnum var) | |
5553 | { | |
5554 | return (var.value | var.mask) & ~0xffffffffULL; | |
5555 | } | |
5556 | ||
5557 | /* Return true if VAL is compared with a s64 sign extended from s32, and they | |
5558 | * are with the same signedness. | |
5559 | */ | |
5560 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) | |
5561 | { | |
5562 | return ((s32)sval >= 0 && | |
5563 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || | |
5564 | ((s32)sval < 0 && | |
5565 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); | |
5566 | } | |
5567 | ||
48461135 JB |
5568 | /* Adjusts the register min/max values in the case that the dst_reg is the |
5569 | * variable register that we are working on, and src_reg is a constant or we're | |
5570 | * simply doing a BPF_K check. | |
f1174f77 | 5571 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
5572 | */ |
5573 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
5574 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 5575 | u8 opcode, bool is_jmp32) |
48461135 | 5576 | { |
a72dafaf JW |
5577 | s64 sval; |
5578 | ||
f1174f77 EC |
5579 | /* If the dst_reg is a pointer, we can't learn anything about its |
5580 | * variable offset from the compare (unless src_reg were a pointer into | |
5581 | * the same object, but we don't bother with that. | |
5582 | * Since false_reg and true_reg have the same type by construction, we | |
5583 | * only need to check one of them for pointerness. | |
5584 | */ | |
5585 | if (__is_pointer_value(false, false_reg)) | |
5586 | return; | |
4cabc5b1 | 5587 | |
092ed096 JW |
5588 | val = is_jmp32 ? (u32)val : val; |
5589 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 5590 | |
48461135 JB |
5591 | switch (opcode) { |
5592 | case BPF_JEQ: | |
48461135 | 5593 | case BPF_JNE: |
a72dafaf JW |
5594 | { |
5595 | struct bpf_reg_state *reg = | |
5596 | opcode == BPF_JEQ ? true_reg : false_reg; | |
5597 | ||
5598 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but | |
5599 | * if it is true we know the value for sure. Likewise for | |
5600 | * BPF_JNE. | |
48461135 | 5601 | */ |
092ed096 JW |
5602 | if (is_jmp32) { |
5603 | u64 old_v = reg->var_off.value; | |
5604 | u64 hi_mask = ~0xffffffffULL; | |
5605 | ||
5606 | reg->var_off.value = (old_v & hi_mask) | val; | |
5607 | reg->var_off.mask &= hi_mask; | |
5608 | } else { | |
5609 | __mark_reg_known(reg, val); | |
5610 | } | |
48461135 | 5611 | break; |
a72dafaf | 5612 | } |
960ea056 JK |
5613 | case BPF_JSET: |
5614 | false_reg->var_off = tnum_and(false_reg->var_off, | |
5615 | tnum_const(~val)); | |
5616 | if (is_power_of_2(val)) | |
5617 | true_reg->var_off = tnum_or(true_reg->var_off, | |
5618 | tnum_const(val)); | |
5619 | break; | |
48461135 | 5620 | case BPF_JGE: |
a72dafaf JW |
5621 | case BPF_JGT: |
5622 | { | |
5623 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
5624 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
5625 | ||
092ed096 JW |
5626 | if (is_jmp32) { |
5627 | false_umax += gen_hi_max(false_reg->var_off); | |
5628 | true_umin += gen_hi_min(true_reg->var_off); | |
5629 | } | |
a72dafaf JW |
5630 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
5631 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b03c9f9f | 5632 | break; |
a72dafaf | 5633 | } |
48461135 | 5634 | case BPF_JSGE: |
a72dafaf JW |
5635 | case BPF_JSGT: |
5636 | { | |
5637 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
5638 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
5639 | ||
092ed096 JW |
5640 | /* If the full s64 was not sign-extended from s32 then don't |
5641 | * deduct further info. | |
5642 | */ | |
5643 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) | |
5644 | break; | |
a72dafaf JW |
5645 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
5646 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
48461135 | 5647 | break; |
a72dafaf | 5648 | } |
b4e432f1 | 5649 | case BPF_JLE: |
a72dafaf JW |
5650 | case BPF_JLT: |
5651 | { | |
5652 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
5653 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
5654 | ||
092ed096 JW |
5655 | if (is_jmp32) { |
5656 | false_umin += gen_hi_min(false_reg->var_off); | |
5657 | true_umax += gen_hi_max(true_reg->var_off); | |
5658 | } | |
a72dafaf JW |
5659 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
5660 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b4e432f1 | 5661 | break; |
a72dafaf | 5662 | } |
b4e432f1 | 5663 | case BPF_JSLE: |
a72dafaf JW |
5664 | case BPF_JSLT: |
5665 | { | |
5666 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
5667 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
5668 | ||
092ed096 JW |
5669 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5670 | break; | |
a72dafaf JW |
5671 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
5672 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
b4e432f1 | 5673 | break; |
a72dafaf | 5674 | } |
48461135 JB |
5675 | default: |
5676 | break; | |
5677 | } | |
5678 | ||
b03c9f9f EC |
5679 | __reg_deduce_bounds(false_reg); |
5680 | __reg_deduce_bounds(true_reg); | |
5681 | /* We might have learned some bits from the bounds. */ | |
5682 | __reg_bound_offset(false_reg); | |
5683 | __reg_bound_offset(true_reg); | |
581738a6 YS |
5684 | if (is_jmp32) { |
5685 | __reg_bound_offset32(false_reg); | |
5686 | __reg_bound_offset32(true_reg); | |
5687 | } | |
b03c9f9f EC |
5688 | /* Intersecting with the old var_off might have improved our bounds |
5689 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5690 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5691 | */ | |
5692 | __update_reg_bounds(false_reg); | |
5693 | __update_reg_bounds(true_reg); | |
48461135 JB |
5694 | } |
5695 | ||
f1174f77 EC |
5696 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
5697 | * the variable reg. | |
48461135 JB |
5698 | */ |
5699 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
5700 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 5701 | u8 opcode, bool is_jmp32) |
48461135 | 5702 | { |
a72dafaf JW |
5703 | s64 sval; |
5704 | ||
f1174f77 EC |
5705 | if (__is_pointer_value(false, false_reg)) |
5706 | return; | |
4cabc5b1 | 5707 | |
092ed096 JW |
5708 | val = is_jmp32 ? (u32)val : val; |
5709 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 5710 | |
48461135 JB |
5711 | switch (opcode) { |
5712 | case BPF_JEQ: | |
48461135 | 5713 | case BPF_JNE: |
a72dafaf JW |
5714 | { |
5715 | struct bpf_reg_state *reg = | |
5716 | opcode == BPF_JEQ ? true_reg : false_reg; | |
5717 | ||
092ed096 JW |
5718 | if (is_jmp32) { |
5719 | u64 old_v = reg->var_off.value; | |
5720 | u64 hi_mask = ~0xffffffffULL; | |
5721 | ||
5722 | reg->var_off.value = (old_v & hi_mask) | val; | |
5723 | reg->var_off.mask &= hi_mask; | |
5724 | } else { | |
5725 | __mark_reg_known(reg, val); | |
5726 | } | |
48461135 | 5727 | break; |
a72dafaf | 5728 | } |
960ea056 JK |
5729 | case BPF_JSET: |
5730 | false_reg->var_off = tnum_and(false_reg->var_off, | |
5731 | tnum_const(~val)); | |
5732 | if (is_power_of_2(val)) | |
5733 | true_reg->var_off = tnum_or(true_reg->var_off, | |
5734 | tnum_const(val)); | |
5735 | break; | |
48461135 | 5736 | case BPF_JGE: |
a72dafaf JW |
5737 | case BPF_JGT: |
5738 | { | |
5739 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; | |
5740 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; | |
5741 | ||
092ed096 JW |
5742 | if (is_jmp32) { |
5743 | false_umin += gen_hi_min(false_reg->var_off); | |
5744 | true_umax += gen_hi_max(true_reg->var_off); | |
5745 | } | |
a72dafaf JW |
5746 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
5747 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b03c9f9f | 5748 | break; |
a72dafaf | 5749 | } |
48461135 | 5750 | case BPF_JSGE: |
a72dafaf JW |
5751 | case BPF_JSGT: |
5752 | { | |
5753 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; | |
5754 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; | |
5755 | ||
092ed096 JW |
5756 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5757 | break; | |
a72dafaf JW |
5758 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
5759 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
48461135 | 5760 | break; |
a72dafaf | 5761 | } |
b4e432f1 | 5762 | case BPF_JLE: |
a72dafaf JW |
5763 | case BPF_JLT: |
5764 | { | |
5765 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; | |
5766 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; | |
5767 | ||
092ed096 JW |
5768 | if (is_jmp32) { |
5769 | false_umax += gen_hi_max(false_reg->var_off); | |
5770 | true_umin += gen_hi_min(true_reg->var_off); | |
5771 | } | |
a72dafaf JW |
5772 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
5773 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b4e432f1 | 5774 | break; |
a72dafaf | 5775 | } |
b4e432f1 | 5776 | case BPF_JSLE: |
a72dafaf JW |
5777 | case BPF_JSLT: |
5778 | { | |
5779 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; | |
5780 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; | |
5781 | ||
092ed096 JW |
5782 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5783 | break; | |
a72dafaf JW |
5784 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
5785 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
b4e432f1 | 5786 | break; |
a72dafaf | 5787 | } |
48461135 JB |
5788 | default: |
5789 | break; | |
5790 | } | |
5791 | ||
b03c9f9f EC |
5792 | __reg_deduce_bounds(false_reg); |
5793 | __reg_deduce_bounds(true_reg); | |
5794 | /* We might have learned some bits from the bounds. */ | |
5795 | __reg_bound_offset(false_reg); | |
5796 | __reg_bound_offset(true_reg); | |
581738a6 YS |
5797 | if (is_jmp32) { |
5798 | __reg_bound_offset32(false_reg); | |
5799 | __reg_bound_offset32(true_reg); | |
5800 | } | |
b03c9f9f EC |
5801 | /* Intersecting with the old var_off might have improved our bounds |
5802 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5803 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5804 | */ | |
5805 | __update_reg_bounds(false_reg); | |
5806 | __update_reg_bounds(true_reg); | |
f1174f77 EC |
5807 | } |
5808 | ||
5809 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
5810 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
5811 | struct bpf_reg_state *dst_reg) | |
5812 | { | |
b03c9f9f EC |
5813 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
5814 | dst_reg->umin_value); | |
5815 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
5816 | dst_reg->umax_value); | |
5817 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
5818 | dst_reg->smin_value); | |
5819 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
5820 | dst_reg->smax_value); | |
f1174f77 EC |
5821 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
5822 | dst_reg->var_off); | |
b03c9f9f EC |
5823 | /* We might have learned new bounds from the var_off. */ |
5824 | __update_reg_bounds(src_reg); | |
5825 | __update_reg_bounds(dst_reg); | |
5826 | /* We might have learned something about the sign bit. */ | |
5827 | __reg_deduce_bounds(src_reg); | |
5828 | __reg_deduce_bounds(dst_reg); | |
5829 | /* We might have learned some bits from the bounds. */ | |
5830 | __reg_bound_offset(src_reg); | |
5831 | __reg_bound_offset(dst_reg); | |
5832 | /* Intersecting with the old var_off might have improved our bounds | |
5833 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5834 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5835 | */ | |
5836 | __update_reg_bounds(src_reg); | |
5837 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
5838 | } |
5839 | ||
5840 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
5841 | struct bpf_reg_state *true_dst, | |
5842 | struct bpf_reg_state *false_src, | |
5843 | struct bpf_reg_state *false_dst, | |
5844 | u8 opcode) | |
5845 | { | |
5846 | switch (opcode) { | |
5847 | case BPF_JEQ: | |
5848 | __reg_combine_min_max(true_src, true_dst); | |
5849 | break; | |
5850 | case BPF_JNE: | |
5851 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 5852 | break; |
4cabc5b1 | 5853 | } |
48461135 JB |
5854 | } |
5855 | ||
fd978bf7 JS |
5856 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
5857 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 5858 | bool is_null) |
57a09bf0 | 5859 | { |
840b9615 | 5860 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
5861 | /* Old offset (both fixed and variable parts) should |
5862 | * have been known-zero, because we don't allow pointer | |
5863 | * arithmetic on pointers that might be NULL. | |
5864 | */ | |
b03c9f9f EC |
5865 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
5866 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 5867 | reg->off)) { |
b03c9f9f EC |
5868 | __mark_reg_known_zero(reg); |
5869 | reg->off = 0; | |
f1174f77 EC |
5870 | } |
5871 | if (is_null) { | |
5872 | reg->type = SCALAR_VALUE; | |
840b9615 JS |
5873 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
5874 | if (reg->map_ptr->inner_map_meta) { | |
5875 | reg->type = CONST_PTR_TO_MAP; | |
5876 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
fada7fdc JL |
5877 | } else if (reg->map_ptr->map_type == |
5878 | BPF_MAP_TYPE_XSKMAP) { | |
5879 | reg->type = PTR_TO_XDP_SOCK; | |
840b9615 JS |
5880 | } else { |
5881 | reg->type = PTR_TO_MAP_VALUE; | |
5882 | } | |
c64b7983 JS |
5883 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
5884 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
5885 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
5886 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
5887 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
5888 | reg->type = PTR_TO_TCP_SOCK; | |
56f668df | 5889 | } |
1b986589 MKL |
5890 | if (is_null) { |
5891 | /* We don't need id and ref_obj_id from this point | |
5892 | * onwards anymore, thus we should better reset it, | |
5893 | * so that state pruning has chances to take effect. | |
5894 | */ | |
5895 | reg->id = 0; | |
5896 | reg->ref_obj_id = 0; | |
5897 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
5898 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
5899 | * in release_reg_references(). | |
5900 | * | |
5901 | * reg->id is still used by spin_lock ptr. Other | |
5902 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
5903 | */ |
5904 | reg->id = 0; | |
56f668df | 5905 | } |
57a09bf0 TG |
5906 | } |
5907 | } | |
5908 | ||
c6a9efa1 PC |
5909 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
5910 | bool is_null) | |
5911 | { | |
5912 | struct bpf_reg_state *reg; | |
5913 | int i; | |
5914 | ||
5915 | for (i = 0; i < MAX_BPF_REG; i++) | |
5916 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
5917 | ||
5918 | bpf_for_each_spilled_reg(i, state, reg) { | |
5919 | if (!reg) | |
5920 | continue; | |
5921 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
5922 | } | |
5923 | } | |
5924 | ||
57a09bf0 TG |
5925 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
5926 | * be folded together at some point. | |
5927 | */ | |
840b9615 JS |
5928 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
5929 | bool is_null) | |
57a09bf0 | 5930 | { |
f4d7e40a | 5931 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 5932 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 5933 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 5934 | u32 id = regs[regno].id; |
c6a9efa1 | 5935 | int i; |
57a09bf0 | 5936 | |
1b986589 MKL |
5937 | if (ref_obj_id && ref_obj_id == id && is_null) |
5938 | /* regs[regno] is in the " == NULL" branch. | |
5939 | * No one could have freed the reference state before | |
5940 | * doing the NULL check. | |
5941 | */ | |
5942 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 5943 | |
c6a9efa1 PC |
5944 | for (i = 0; i <= vstate->curframe; i++) |
5945 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
5946 | } |
5947 | ||
5beca081 DB |
5948 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
5949 | struct bpf_reg_state *dst_reg, | |
5950 | struct bpf_reg_state *src_reg, | |
5951 | struct bpf_verifier_state *this_branch, | |
5952 | struct bpf_verifier_state *other_branch) | |
5953 | { | |
5954 | if (BPF_SRC(insn->code) != BPF_X) | |
5955 | return false; | |
5956 | ||
092ed096 JW |
5957 | /* Pointers are always 64-bit. */ |
5958 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
5959 | return false; | |
5960 | ||
5beca081 DB |
5961 | switch (BPF_OP(insn->code)) { |
5962 | case BPF_JGT: | |
5963 | if ((dst_reg->type == PTR_TO_PACKET && | |
5964 | src_reg->type == PTR_TO_PACKET_END) || | |
5965 | (dst_reg->type == PTR_TO_PACKET_META && | |
5966 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5967 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
5968 | find_good_pkt_pointers(this_branch, dst_reg, | |
5969 | dst_reg->type, false); | |
5970 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5971 | src_reg->type == PTR_TO_PACKET) || | |
5972 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5973 | src_reg->type == PTR_TO_PACKET_META)) { | |
5974 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
5975 | find_good_pkt_pointers(other_branch, src_reg, | |
5976 | src_reg->type, true); | |
5977 | } else { | |
5978 | return false; | |
5979 | } | |
5980 | break; | |
5981 | case BPF_JLT: | |
5982 | if ((dst_reg->type == PTR_TO_PACKET && | |
5983 | src_reg->type == PTR_TO_PACKET_END) || | |
5984 | (dst_reg->type == PTR_TO_PACKET_META && | |
5985 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5986 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
5987 | find_good_pkt_pointers(other_branch, dst_reg, | |
5988 | dst_reg->type, true); | |
5989 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5990 | src_reg->type == PTR_TO_PACKET) || | |
5991 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5992 | src_reg->type == PTR_TO_PACKET_META)) { | |
5993 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
5994 | find_good_pkt_pointers(this_branch, src_reg, | |
5995 | src_reg->type, false); | |
5996 | } else { | |
5997 | return false; | |
5998 | } | |
5999 | break; | |
6000 | case BPF_JGE: | |
6001 | if ((dst_reg->type == PTR_TO_PACKET && | |
6002 | src_reg->type == PTR_TO_PACKET_END) || | |
6003 | (dst_reg->type == PTR_TO_PACKET_META && | |
6004 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
6005 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
6006 | find_good_pkt_pointers(this_branch, dst_reg, | |
6007 | dst_reg->type, true); | |
6008 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
6009 | src_reg->type == PTR_TO_PACKET) || | |
6010 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
6011 | src_reg->type == PTR_TO_PACKET_META)) { | |
6012 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
6013 | find_good_pkt_pointers(other_branch, src_reg, | |
6014 | src_reg->type, false); | |
6015 | } else { | |
6016 | return false; | |
6017 | } | |
6018 | break; | |
6019 | case BPF_JLE: | |
6020 | if ((dst_reg->type == PTR_TO_PACKET && | |
6021 | src_reg->type == PTR_TO_PACKET_END) || | |
6022 | (dst_reg->type == PTR_TO_PACKET_META && | |
6023 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
6024 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
6025 | find_good_pkt_pointers(other_branch, dst_reg, | |
6026 | dst_reg->type, false); | |
6027 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
6028 | src_reg->type == PTR_TO_PACKET) || | |
6029 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
6030 | src_reg->type == PTR_TO_PACKET_META)) { | |
6031 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
6032 | find_good_pkt_pointers(this_branch, src_reg, | |
6033 | src_reg->type, true); | |
6034 | } else { | |
6035 | return false; | |
6036 | } | |
6037 | break; | |
6038 | default: | |
6039 | return false; | |
6040 | } | |
6041 | ||
6042 | return true; | |
6043 | } | |
6044 | ||
58e2af8b | 6045 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
6046 | struct bpf_insn *insn, int *insn_idx) |
6047 | { | |
f4d7e40a AS |
6048 | struct bpf_verifier_state *this_branch = env->cur_state; |
6049 | struct bpf_verifier_state *other_branch; | |
6050 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 6051 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 6052 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 6053 | bool is_jmp32; |
fb8d251e | 6054 | int pred = -1; |
17a52670 AS |
6055 | int err; |
6056 | ||
092ed096 JW |
6057 | /* Only conditional jumps are expected to reach here. */ |
6058 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
6059 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
6060 | return -EINVAL; |
6061 | } | |
6062 | ||
6063 | if (BPF_SRC(insn->code) == BPF_X) { | |
6064 | if (insn->imm != 0) { | |
092ed096 | 6065 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
6066 | return -EINVAL; |
6067 | } | |
6068 | ||
6069 | /* check src1 operand */ | |
dc503a8a | 6070 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6071 | if (err) |
6072 | return err; | |
1be7f75d AS |
6073 | |
6074 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 6075 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
6076 | insn->src_reg); |
6077 | return -EACCES; | |
6078 | } | |
fb8d251e | 6079 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
6080 | } else { |
6081 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 6082 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
6083 | return -EINVAL; |
6084 | } | |
6085 | } | |
6086 | ||
6087 | /* check src2 operand */ | |
dc503a8a | 6088 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6089 | if (err) |
6090 | return err; | |
6091 | ||
1a0dc1ac | 6092 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 6093 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 6094 | |
fb8d251e AS |
6095 | if (BPF_SRC(insn->code) == BPF_K) |
6096 | pred = is_branch_taken(dst_reg, insn->imm, | |
6097 | opcode, is_jmp32); | |
6098 | else if (src_reg->type == SCALAR_VALUE && | |
6099 | tnum_is_const(src_reg->var_off)) | |
6100 | pred = is_branch_taken(dst_reg, src_reg->var_off.value, | |
6101 | opcode, is_jmp32); | |
b5dc0163 AS |
6102 | if (pred >= 0) { |
6103 | err = mark_chain_precision(env, insn->dst_reg); | |
6104 | if (BPF_SRC(insn->code) == BPF_X && !err) | |
6105 | err = mark_chain_precision(env, insn->src_reg); | |
6106 | if (err) | |
6107 | return err; | |
6108 | } | |
fb8d251e AS |
6109 | if (pred == 1) { |
6110 | /* only follow the goto, ignore fall-through */ | |
6111 | *insn_idx += insn->off; | |
6112 | return 0; | |
6113 | } else if (pred == 0) { | |
6114 | /* only follow fall-through branch, since | |
6115 | * that's where the program will go | |
6116 | */ | |
6117 | return 0; | |
17a52670 AS |
6118 | } |
6119 | ||
979d63d5 DB |
6120 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
6121 | false); | |
17a52670 AS |
6122 | if (!other_branch) |
6123 | return -EFAULT; | |
f4d7e40a | 6124 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 6125 | |
48461135 JB |
6126 | /* detect if we are comparing against a constant value so we can adjust |
6127 | * our min/max values for our dst register. | |
f1174f77 EC |
6128 | * this is only legit if both are scalars (or pointers to the same |
6129 | * object, I suppose, but we don't support that right now), because | |
6130 | * otherwise the different base pointers mean the offsets aren't | |
6131 | * comparable. | |
48461135 JB |
6132 | */ |
6133 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 JW |
6134 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
6135 | struct bpf_reg_state lo_reg0 = *dst_reg; | |
6136 | struct bpf_reg_state lo_reg1 = *src_reg; | |
6137 | struct bpf_reg_state *src_lo, *dst_lo; | |
6138 | ||
6139 | dst_lo = &lo_reg0; | |
6140 | src_lo = &lo_reg1; | |
6141 | coerce_reg_to_size(dst_lo, 4); | |
6142 | coerce_reg_to_size(src_lo, 4); | |
6143 | ||
f1174f77 | 6144 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
6145 | src_reg->type == SCALAR_VALUE) { |
6146 | if (tnum_is_const(src_reg->var_off) || | |
6147 | (is_jmp32 && tnum_is_const(src_lo->var_off))) | |
f4d7e40a | 6148 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 JW |
6149 | dst_reg, |
6150 | is_jmp32 | |
6151 | ? src_lo->var_off.value | |
6152 | : src_reg->var_off.value, | |
6153 | opcode, is_jmp32); | |
6154 | else if (tnum_is_const(dst_reg->var_off) || | |
6155 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) | |
f4d7e40a | 6156 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 JW |
6157 | src_reg, |
6158 | is_jmp32 | |
6159 | ? dst_lo->var_off.value | |
6160 | : dst_reg->var_off.value, | |
6161 | opcode, is_jmp32); | |
6162 | else if (!is_jmp32 && | |
6163 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 6164 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
6165 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
6166 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 6167 | src_reg, dst_reg, opcode); |
f1174f77 EC |
6168 | } |
6169 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 6170 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 6171 | dst_reg, insn->imm, opcode, is_jmp32); |
48461135 JB |
6172 | } |
6173 | ||
092ed096 JW |
6174 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
6175 | * NOTE: these optimizations below are related with pointer comparison | |
6176 | * which will never be JMP32. | |
6177 | */ | |
6178 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 6179 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
6180 | reg_type_may_be_null(dst_reg->type)) { |
6181 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
6182 | * safe or unknown depending R == 0 or R != 0 conditional. |
6183 | */ | |
840b9615 JS |
6184 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
6185 | opcode == BPF_JNE); | |
6186 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
6187 | opcode == BPF_JEQ); | |
5beca081 DB |
6188 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
6189 | this_branch, other_branch) && | |
6190 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
6191 | verbose(env, "R%d pointer comparison prohibited\n", |
6192 | insn->dst_reg); | |
1be7f75d | 6193 | return -EACCES; |
17a52670 | 6194 | } |
06ee7115 | 6195 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 6196 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
6197 | return 0; |
6198 | } | |
6199 | ||
17a52670 | 6200 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 6201 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 6202 | { |
d8eca5bb | 6203 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 6204 | struct bpf_reg_state *regs = cur_regs(env); |
d8eca5bb | 6205 | struct bpf_map *map; |
17a52670 AS |
6206 | int err; |
6207 | ||
6208 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 6209 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
6210 | return -EINVAL; |
6211 | } | |
6212 | if (insn->off != 0) { | |
61bd5218 | 6213 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
6214 | return -EINVAL; |
6215 | } | |
6216 | ||
dc503a8a | 6217 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
6218 | if (err) |
6219 | return err; | |
6220 | ||
6b173873 | 6221 | if (insn->src_reg == 0) { |
6b173873 JK |
6222 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
6223 | ||
f1174f77 | 6224 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 6225 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 6226 | return 0; |
6b173873 | 6227 | } |
17a52670 | 6228 | |
d8eca5bb DB |
6229 | map = env->used_maps[aux->map_index]; |
6230 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
6231 | regs[insn->dst_reg].map_ptr = map; | |
6232 | ||
6233 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
6234 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
6235 | regs[insn->dst_reg].off = aux->map_off; | |
6236 | if (map_value_has_spin_lock(map)) | |
6237 | regs[insn->dst_reg].id = ++env->id_gen; | |
6238 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
6239 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
6240 | } else { | |
6241 | verbose(env, "bpf verifier is misconfigured\n"); | |
6242 | return -EINVAL; | |
6243 | } | |
17a52670 | 6244 | |
17a52670 AS |
6245 | return 0; |
6246 | } | |
6247 | ||
96be4325 DB |
6248 | static bool may_access_skb(enum bpf_prog_type type) |
6249 | { | |
6250 | switch (type) { | |
6251 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
6252 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 6253 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
6254 | return true; |
6255 | default: | |
6256 | return false; | |
6257 | } | |
6258 | } | |
6259 | ||
ddd872bc AS |
6260 | /* verify safety of LD_ABS|LD_IND instructions: |
6261 | * - they can only appear in the programs where ctx == skb | |
6262 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
6263 | * preserve R6-R9, and store return value into R0 | |
6264 | * | |
6265 | * Implicit input: | |
6266 | * ctx == skb == R6 == CTX | |
6267 | * | |
6268 | * Explicit input: | |
6269 | * SRC == any register | |
6270 | * IMM == 32-bit immediate | |
6271 | * | |
6272 | * Output: | |
6273 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
6274 | */ | |
58e2af8b | 6275 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 6276 | { |
638f5b90 | 6277 | struct bpf_reg_state *regs = cur_regs(env); |
ddd872bc | 6278 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
6279 | int i, err; |
6280 | ||
24701ece | 6281 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 6282 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
6283 | return -EINVAL; |
6284 | } | |
6285 | ||
e0cea7ce DB |
6286 | if (!env->ops->gen_ld_abs) { |
6287 | verbose(env, "bpf verifier is misconfigured\n"); | |
6288 | return -EINVAL; | |
6289 | } | |
6290 | ||
f910cefa | 6291 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
6292 | /* when program has LD_ABS insn JITs and interpreter assume |
6293 | * that r1 == ctx == skb which is not the case for callees | |
6294 | * that can have arbitrary arguments. It's problematic | |
6295 | * for main prog as well since JITs would need to analyze | |
6296 | * all functions in order to make proper register save/restore | |
6297 | * decisions in the main prog. Hence disallow LD_ABS with calls | |
6298 | */ | |
6299 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); | |
6300 | return -EINVAL; | |
6301 | } | |
6302 | ||
ddd872bc | 6303 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 6304 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 6305 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 6306 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
6307 | return -EINVAL; |
6308 | } | |
6309 | ||
6310 | /* check whether implicit source operand (register R6) is readable */ | |
dc503a8a | 6311 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
ddd872bc AS |
6312 | if (err) |
6313 | return err; | |
6314 | ||
fd978bf7 JS |
6315 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
6316 | * gen_ld_abs() may terminate the program at runtime, leading to | |
6317 | * reference leak. | |
6318 | */ | |
6319 | err = check_reference_leak(env); | |
6320 | if (err) { | |
6321 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
6322 | return err; | |
6323 | } | |
6324 | ||
d83525ca AS |
6325 | if (env->cur_state->active_spin_lock) { |
6326 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
6327 | return -EINVAL; | |
6328 | } | |
6329 | ||
ddd872bc | 6330 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
61bd5218 JK |
6331 | verbose(env, |
6332 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
6333 | return -EINVAL; |
6334 | } | |
6335 | ||
6336 | if (mode == BPF_IND) { | |
6337 | /* check explicit source operand */ | |
dc503a8a | 6338 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
6339 | if (err) |
6340 | return err; | |
6341 | } | |
6342 | ||
6343 | /* reset caller saved regs to unreadable */ | |
dc503a8a | 6344 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 6345 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
6346 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
6347 | } | |
ddd872bc AS |
6348 | |
6349 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
6350 | * the value fetched from the packet. |
6351 | * Already marked as written above. | |
ddd872bc | 6352 | */ |
61bd5218 | 6353 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
6354 | /* ld_abs load up to 32-bit skb data. */ |
6355 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
6356 | return 0; |
6357 | } | |
6358 | ||
390ee7e2 AS |
6359 | static int check_return_code(struct bpf_verifier_env *env) |
6360 | { | |
5cf1e914 | 6361 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 6362 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
6363 | struct bpf_reg_state *reg; |
6364 | struct tnum range = tnum_range(0, 1); | |
27ae7997 MKL |
6365 | int err; |
6366 | ||
6367 | /* The struct_ops func-ptr's return type could be "void" */ | |
6368 | if (env->prog->type == BPF_PROG_TYPE_STRUCT_OPS && | |
6369 | !prog->aux->attach_func_proto->type) | |
6370 | return 0; | |
6371 | ||
6372 | /* eBPF calling convetion is such that R0 is used | |
6373 | * to return the value from eBPF program. | |
6374 | * Make sure that it's readable at this time | |
6375 | * of bpf_exit, which means that program wrote | |
6376 | * something into it earlier | |
6377 | */ | |
6378 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
6379 | if (err) | |
6380 | return err; | |
6381 | ||
6382 | if (is_pointer_value(env, BPF_REG_0)) { | |
6383 | verbose(env, "R0 leaks addr as return value\n"); | |
6384 | return -EACCES; | |
6385 | } | |
390ee7e2 AS |
6386 | |
6387 | switch (env->prog->type) { | |
983695fa DB |
6388 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
6389 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
6390 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG) | |
6391 | range = tnum_range(1, 1); | |
ed4ed404 | 6392 | break; |
390ee7e2 | 6393 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 6394 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
6395 | range = tnum_range(0, 3); | |
6396 | enforce_attach_type_range = tnum_range(2, 3); | |
6397 | } | |
ed4ed404 | 6398 | break; |
390ee7e2 AS |
6399 | case BPF_PROG_TYPE_CGROUP_SOCK: |
6400 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 6401 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 6402 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 6403 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 6404 | break; |
15ab09bd AS |
6405 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
6406 | if (!env->prog->aux->attach_btf_id) | |
6407 | return 0; | |
6408 | range = tnum_const(0); | |
6409 | break; | |
390ee7e2 AS |
6410 | default: |
6411 | return 0; | |
6412 | } | |
6413 | ||
638f5b90 | 6414 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 6415 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 6416 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
6417 | reg_type_str[reg->type]); |
6418 | return -EINVAL; | |
6419 | } | |
6420 | ||
6421 | if (!tnum_in(range, reg->var_off)) { | |
5cf1e914 | 6422 | char tn_buf[48]; |
6423 | ||
61bd5218 | 6424 | verbose(env, "At program exit the register R0 "); |
390ee7e2 | 6425 | if (!tnum_is_unknown(reg->var_off)) { |
390ee7e2 | 6426 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 6427 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 6428 | } else { |
61bd5218 | 6429 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 6430 | } |
5cf1e914 | 6431 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
983695fa | 6432 | verbose(env, " should have been in %s\n", tn_buf); |
390ee7e2 AS |
6433 | return -EINVAL; |
6434 | } | |
5cf1e914 | 6435 | |
6436 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
6437 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
6438 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
6439 | return 0; |
6440 | } | |
6441 | ||
475fb78f AS |
6442 | /* non-recursive DFS pseudo code |
6443 | * 1 procedure DFS-iterative(G,v): | |
6444 | * 2 label v as discovered | |
6445 | * 3 let S be a stack | |
6446 | * 4 S.push(v) | |
6447 | * 5 while S is not empty | |
6448 | * 6 t <- S.pop() | |
6449 | * 7 if t is what we're looking for: | |
6450 | * 8 return t | |
6451 | * 9 for all edges e in G.adjacentEdges(t) do | |
6452 | * 10 if edge e is already labelled | |
6453 | * 11 continue with the next edge | |
6454 | * 12 w <- G.adjacentVertex(t,e) | |
6455 | * 13 if vertex w is not discovered and not explored | |
6456 | * 14 label e as tree-edge | |
6457 | * 15 label w as discovered | |
6458 | * 16 S.push(w) | |
6459 | * 17 continue at 5 | |
6460 | * 18 else if vertex w is discovered | |
6461 | * 19 label e as back-edge | |
6462 | * 20 else | |
6463 | * 21 // vertex w is explored | |
6464 | * 22 label e as forward- or cross-edge | |
6465 | * 23 label t as explored | |
6466 | * 24 S.pop() | |
6467 | * | |
6468 | * convention: | |
6469 | * 0x10 - discovered | |
6470 | * 0x11 - discovered and fall-through edge labelled | |
6471 | * 0x12 - discovered and fall-through and branch edges labelled | |
6472 | * 0x20 - explored | |
6473 | */ | |
6474 | ||
6475 | enum { | |
6476 | DISCOVERED = 0x10, | |
6477 | EXPLORED = 0x20, | |
6478 | FALLTHROUGH = 1, | |
6479 | BRANCH = 2, | |
6480 | }; | |
6481 | ||
dc2a4ebc AS |
6482 | static u32 state_htab_size(struct bpf_verifier_env *env) |
6483 | { | |
6484 | return env->prog->len; | |
6485 | } | |
6486 | ||
5d839021 AS |
6487 | static struct bpf_verifier_state_list **explored_state( |
6488 | struct bpf_verifier_env *env, | |
6489 | int idx) | |
6490 | { | |
dc2a4ebc AS |
6491 | struct bpf_verifier_state *cur = env->cur_state; |
6492 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
6493 | ||
6494 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
6495 | } |
6496 | ||
6497 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
6498 | { | |
a8f500af | 6499 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 6500 | } |
f1bca824 | 6501 | |
475fb78f AS |
6502 | /* t, w, e - match pseudo-code above: |
6503 | * t - index of current instruction | |
6504 | * w - next instruction | |
6505 | * e - edge | |
6506 | */ | |
2589726d AS |
6507 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
6508 | bool loop_ok) | |
475fb78f | 6509 | { |
7df737e9 AS |
6510 | int *insn_stack = env->cfg.insn_stack; |
6511 | int *insn_state = env->cfg.insn_state; | |
6512 | ||
475fb78f AS |
6513 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
6514 | return 0; | |
6515 | ||
6516 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
6517 | return 0; | |
6518 | ||
6519 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 6520 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 6521 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
6522 | return -EINVAL; |
6523 | } | |
6524 | ||
f1bca824 AS |
6525 | if (e == BRANCH) |
6526 | /* mark branch target for state pruning */ | |
5d839021 | 6527 | init_explored_state(env, w); |
f1bca824 | 6528 | |
475fb78f AS |
6529 | if (insn_state[w] == 0) { |
6530 | /* tree-edge */ | |
6531 | insn_state[t] = DISCOVERED | e; | |
6532 | insn_state[w] = DISCOVERED; | |
7df737e9 | 6533 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 6534 | return -E2BIG; |
7df737e9 | 6535 | insn_stack[env->cfg.cur_stack++] = w; |
475fb78f AS |
6536 | return 1; |
6537 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2589726d AS |
6538 | if (loop_ok && env->allow_ptr_leaks) |
6539 | return 0; | |
d9762e84 MKL |
6540 | verbose_linfo(env, t, "%d: ", t); |
6541 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 6542 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
6543 | return -EINVAL; |
6544 | } else if (insn_state[w] == EXPLORED) { | |
6545 | /* forward- or cross-edge */ | |
6546 | insn_state[t] = DISCOVERED | e; | |
6547 | } else { | |
61bd5218 | 6548 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
6549 | return -EFAULT; |
6550 | } | |
6551 | return 0; | |
6552 | } | |
6553 | ||
6554 | /* non-recursive depth-first-search to detect loops in BPF program | |
6555 | * loop == back-edge in directed graph | |
6556 | */ | |
58e2af8b | 6557 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
6558 | { |
6559 | struct bpf_insn *insns = env->prog->insnsi; | |
6560 | int insn_cnt = env->prog->len; | |
7df737e9 | 6561 | int *insn_stack, *insn_state; |
475fb78f AS |
6562 | int ret = 0; |
6563 | int i, t; | |
6564 | ||
7df737e9 | 6565 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
6566 | if (!insn_state) |
6567 | return -ENOMEM; | |
6568 | ||
7df737e9 | 6569 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 6570 | if (!insn_stack) { |
71dde681 | 6571 | kvfree(insn_state); |
475fb78f AS |
6572 | return -ENOMEM; |
6573 | } | |
6574 | ||
6575 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
6576 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 6577 | env->cfg.cur_stack = 1; |
475fb78f AS |
6578 | |
6579 | peek_stack: | |
7df737e9 | 6580 | if (env->cfg.cur_stack == 0) |
475fb78f | 6581 | goto check_state; |
7df737e9 | 6582 | t = insn_stack[env->cfg.cur_stack - 1]; |
475fb78f | 6583 | |
092ed096 JW |
6584 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
6585 | BPF_CLASS(insns[t].code) == BPF_JMP32) { | |
475fb78f AS |
6586 | u8 opcode = BPF_OP(insns[t].code); |
6587 | ||
6588 | if (opcode == BPF_EXIT) { | |
6589 | goto mark_explored; | |
6590 | } else if (opcode == BPF_CALL) { | |
2589726d | 6591 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
6592 | if (ret == 1) |
6593 | goto peek_stack; | |
6594 | else if (ret < 0) | |
6595 | goto err_free; | |
07016151 | 6596 | if (t + 1 < insn_cnt) |
5d839021 | 6597 | init_explored_state(env, t + 1); |
cc8b0b92 | 6598 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
5d839021 | 6599 | init_explored_state(env, t); |
2589726d AS |
6600 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
6601 | env, false); | |
cc8b0b92 AS |
6602 | if (ret == 1) |
6603 | goto peek_stack; | |
6604 | else if (ret < 0) | |
6605 | goto err_free; | |
6606 | } | |
475fb78f AS |
6607 | } else if (opcode == BPF_JA) { |
6608 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
6609 | ret = -EINVAL; | |
6610 | goto err_free; | |
6611 | } | |
6612 | /* unconditional jump with single edge */ | |
6613 | ret = push_insn(t, t + insns[t].off + 1, | |
2589726d | 6614 | FALLTHROUGH, env, true); |
475fb78f AS |
6615 | if (ret == 1) |
6616 | goto peek_stack; | |
6617 | else if (ret < 0) | |
6618 | goto err_free; | |
b5dc0163 AS |
6619 | /* unconditional jmp is not a good pruning point, |
6620 | * but it's marked, since backtracking needs | |
6621 | * to record jmp history in is_state_visited(). | |
6622 | */ | |
6623 | init_explored_state(env, t + insns[t].off + 1); | |
f1bca824 AS |
6624 | /* tell verifier to check for equivalent states |
6625 | * after every call and jump | |
6626 | */ | |
c3de6317 | 6627 | if (t + 1 < insn_cnt) |
5d839021 | 6628 | init_explored_state(env, t + 1); |
475fb78f AS |
6629 | } else { |
6630 | /* conditional jump with two edges */ | |
5d839021 | 6631 | init_explored_state(env, t); |
2589726d | 6632 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
475fb78f AS |
6633 | if (ret == 1) |
6634 | goto peek_stack; | |
6635 | else if (ret < 0) | |
6636 | goto err_free; | |
6637 | ||
2589726d | 6638 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
475fb78f AS |
6639 | if (ret == 1) |
6640 | goto peek_stack; | |
6641 | else if (ret < 0) | |
6642 | goto err_free; | |
6643 | } | |
6644 | } else { | |
6645 | /* all other non-branch instructions with single | |
6646 | * fall-through edge | |
6647 | */ | |
2589726d | 6648 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
6649 | if (ret == 1) |
6650 | goto peek_stack; | |
6651 | else if (ret < 0) | |
6652 | goto err_free; | |
6653 | } | |
6654 | ||
6655 | mark_explored: | |
6656 | insn_state[t] = EXPLORED; | |
7df737e9 | 6657 | if (env->cfg.cur_stack-- <= 0) { |
61bd5218 | 6658 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
6659 | ret = -EFAULT; |
6660 | goto err_free; | |
6661 | } | |
6662 | goto peek_stack; | |
6663 | ||
6664 | check_state: | |
6665 | for (i = 0; i < insn_cnt; i++) { | |
6666 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 6667 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
6668 | ret = -EINVAL; |
6669 | goto err_free; | |
6670 | } | |
6671 | } | |
6672 | ret = 0; /* cfg looks good */ | |
6673 | ||
6674 | err_free: | |
71dde681 AS |
6675 | kvfree(insn_state); |
6676 | kvfree(insn_stack); | |
7df737e9 | 6677 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
6678 | return ret; |
6679 | } | |
6680 | ||
838e9690 YS |
6681 | /* The minimum supported BTF func info size */ |
6682 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
6683 | #define MAX_FUNCINFO_REC_SIZE 252 | |
6684 | ||
c454a46b MKL |
6685 | static int check_btf_func(struct bpf_verifier_env *env, |
6686 | const union bpf_attr *attr, | |
6687 | union bpf_attr __user *uattr) | |
838e9690 | 6688 | { |
d0b2818e | 6689 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 6690 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 6691 | struct bpf_func_info *krecord; |
8c1b6e69 | 6692 | struct bpf_func_info_aux *info_aux = NULL; |
838e9690 | 6693 | const struct btf_type *type; |
c454a46b MKL |
6694 | struct bpf_prog *prog; |
6695 | const struct btf *btf; | |
838e9690 | 6696 | void __user *urecord; |
d0b2818e | 6697 | u32 prev_offset = 0; |
838e9690 YS |
6698 | int ret = 0; |
6699 | ||
6700 | nfuncs = attr->func_info_cnt; | |
6701 | if (!nfuncs) | |
6702 | return 0; | |
6703 | ||
6704 | if (nfuncs != env->subprog_cnt) { | |
6705 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
6706 | return -EINVAL; | |
6707 | } | |
6708 | ||
6709 | urec_size = attr->func_info_rec_size; | |
6710 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
6711 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
6712 | urec_size % sizeof(u32)) { | |
6713 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
6714 | return -EINVAL; | |
6715 | } | |
6716 | ||
c454a46b MKL |
6717 | prog = env->prog; |
6718 | btf = prog->aux->btf; | |
838e9690 YS |
6719 | |
6720 | urecord = u64_to_user_ptr(attr->func_info); | |
6721 | min_size = min_t(u32, krec_size, urec_size); | |
6722 | ||
ba64e7d8 | 6723 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
6724 | if (!krecord) |
6725 | return -ENOMEM; | |
8c1b6e69 AS |
6726 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
6727 | if (!info_aux) | |
6728 | goto err_free; | |
ba64e7d8 | 6729 | |
838e9690 YS |
6730 | for (i = 0; i < nfuncs; i++) { |
6731 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
6732 | if (ret) { | |
6733 | if (ret == -E2BIG) { | |
6734 | verbose(env, "nonzero tailing record in func info"); | |
6735 | /* set the size kernel expects so loader can zero | |
6736 | * out the rest of the record. | |
6737 | */ | |
6738 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
6739 | ret = -EFAULT; | |
6740 | } | |
c454a46b | 6741 | goto err_free; |
838e9690 YS |
6742 | } |
6743 | ||
ba64e7d8 | 6744 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 6745 | ret = -EFAULT; |
c454a46b | 6746 | goto err_free; |
838e9690 YS |
6747 | } |
6748 | ||
d30d42e0 | 6749 | /* check insn_off */ |
838e9690 | 6750 | if (i == 0) { |
d30d42e0 | 6751 | if (krecord[i].insn_off) { |
838e9690 | 6752 | verbose(env, |
d30d42e0 MKL |
6753 | "nonzero insn_off %u for the first func info record", |
6754 | krecord[i].insn_off); | |
838e9690 | 6755 | ret = -EINVAL; |
c454a46b | 6756 | goto err_free; |
838e9690 | 6757 | } |
d30d42e0 | 6758 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
6759 | verbose(env, |
6760 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 6761 | krecord[i].insn_off, prev_offset); |
838e9690 | 6762 | ret = -EINVAL; |
c454a46b | 6763 | goto err_free; |
838e9690 YS |
6764 | } |
6765 | ||
d30d42e0 | 6766 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 YS |
6767 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
6768 | ret = -EINVAL; | |
c454a46b | 6769 | goto err_free; |
838e9690 YS |
6770 | } |
6771 | ||
6772 | /* check type_id */ | |
ba64e7d8 | 6773 | type = btf_type_by_id(btf, krecord[i].type_id); |
838e9690 YS |
6774 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
6775 | verbose(env, "invalid type id %d in func info", | |
ba64e7d8 | 6776 | krecord[i].type_id); |
838e9690 | 6777 | ret = -EINVAL; |
c454a46b | 6778 | goto err_free; |
838e9690 | 6779 | } |
d30d42e0 | 6780 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
6781 | urecord += urec_size; |
6782 | } | |
6783 | ||
ba64e7d8 YS |
6784 | prog->aux->func_info = krecord; |
6785 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 6786 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
6787 | return 0; |
6788 | ||
c454a46b | 6789 | err_free: |
ba64e7d8 | 6790 | kvfree(krecord); |
8c1b6e69 | 6791 | kfree(info_aux); |
838e9690 YS |
6792 | return ret; |
6793 | } | |
6794 | ||
ba64e7d8 YS |
6795 | static void adjust_btf_func(struct bpf_verifier_env *env) |
6796 | { | |
8c1b6e69 | 6797 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
6798 | int i; |
6799 | ||
8c1b6e69 | 6800 | if (!aux->func_info) |
ba64e7d8 YS |
6801 | return; |
6802 | ||
6803 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 6804 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
6805 | } |
6806 | ||
c454a46b MKL |
6807 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
6808 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
6809 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
6810 | ||
6811 | static int check_btf_line(struct bpf_verifier_env *env, | |
6812 | const union bpf_attr *attr, | |
6813 | union bpf_attr __user *uattr) | |
6814 | { | |
6815 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
6816 | struct bpf_subprog_info *sub; | |
6817 | struct bpf_line_info *linfo; | |
6818 | struct bpf_prog *prog; | |
6819 | const struct btf *btf; | |
6820 | void __user *ulinfo; | |
6821 | int err; | |
6822 | ||
6823 | nr_linfo = attr->line_info_cnt; | |
6824 | if (!nr_linfo) | |
6825 | return 0; | |
6826 | ||
6827 | rec_size = attr->line_info_rec_size; | |
6828 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
6829 | rec_size > MAX_LINEINFO_REC_SIZE || | |
6830 | rec_size & (sizeof(u32) - 1)) | |
6831 | return -EINVAL; | |
6832 | ||
6833 | /* Need to zero it in case the userspace may | |
6834 | * pass in a smaller bpf_line_info object. | |
6835 | */ | |
6836 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
6837 | GFP_KERNEL | __GFP_NOWARN); | |
6838 | if (!linfo) | |
6839 | return -ENOMEM; | |
6840 | ||
6841 | prog = env->prog; | |
6842 | btf = prog->aux->btf; | |
6843 | ||
6844 | s = 0; | |
6845 | sub = env->subprog_info; | |
6846 | ulinfo = u64_to_user_ptr(attr->line_info); | |
6847 | expected_size = sizeof(struct bpf_line_info); | |
6848 | ncopy = min_t(u32, expected_size, rec_size); | |
6849 | for (i = 0; i < nr_linfo; i++) { | |
6850 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
6851 | if (err) { | |
6852 | if (err == -E2BIG) { | |
6853 | verbose(env, "nonzero tailing record in line_info"); | |
6854 | if (put_user(expected_size, | |
6855 | &uattr->line_info_rec_size)) | |
6856 | err = -EFAULT; | |
6857 | } | |
6858 | goto err_free; | |
6859 | } | |
6860 | ||
6861 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
6862 | err = -EFAULT; | |
6863 | goto err_free; | |
6864 | } | |
6865 | ||
6866 | /* | |
6867 | * Check insn_off to ensure | |
6868 | * 1) strictly increasing AND | |
6869 | * 2) bounded by prog->len | |
6870 | * | |
6871 | * The linfo[0].insn_off == 0 check logically falls into | |
6872 | * the later "missing bpf_line_info for func..." case | |
6873 | * because the first linfo[0].insn_off must be the | |
6874 | * first sub also and the first sub must have | |
6875 | * subprog_info[0].start == 0. | |
6876 | */ | |
6877 | if ((i && linfo[i].insn_off <= prev_offset) || | |
6878 | linfo[i].insn_off >= prog->len) { | |
6879 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
6880 | i, linfo[i].insn_off, prev_offset, | |
6881 | prog->len); | |
6882 | err = -EINVAL; | |
6883 | goto err_free; | |
6884 | } | |
6885 | ||
fdbaa0be MKL |
6886 | if (!prog->insnsi[linfo[i].insn_off].code) { |
6887 | verbose(env, | |
6888 | "Invalid insn code at line_info[%u].insn_off\n", | |
6889 | i); | |
6890 | err = -EINVAL; | |
6891 | goto err_free; | |
6892 | } | |
6893 | ||
23127b33 MKL |
6894 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
6895 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
6896 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
6897 | err = -EINVAL; | |
6898 | goto err_free; | |
6899 | } | |
6900 | ||
6901 | if (s != env->subprog_cnt) { | |
6902 | if (linfo[i].insn_off == sub[s].start) { | |
6903 | sub[s].linfo_idx = i; | |
6904 | s++; | |
6905 | } else if (sub[s].start < linfo[i].insn_off) { | |
6906 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
6907 | err = -EINVAL; | |
6908 | goto err_free; | |
6909 | } | |
6910 | } | |
6911 | ||
6912 | prev_offset = linfo[i].insn_off; | |
6913 | ulinfo += rec_size; | |
6914 | } | |
6915 | ||
6916 | if (s != env->subprog_cnt) { | |
6917 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
6918 | env->subprog_cnt - s, s); | |
6919 | err = -EINVAL; | |
6920 | goto err_free; | |
6921 | } | |
6922 | ||
6923 | prog->aux->linfo = linfo; | |
6924 | prog->aux->nr_linfo = nr_linfo; | |
6925 | ||
6926 | return 0; | |
6927 | ||
6928 | err_free: | |
6929 | kvfree(linfo); | |
6930 | return err; | |
6931 | } | |
6932 | ||
6933 | static int check_btf_info(struct bpf_verifier_env *env, | |
6934 | const union bpf_attr *attr, | |
6935 | union bpf_attr __user *uattr) | |
6936 | { | |
6937 | struct btf *btf; | |
6938 | int err; | |
6939 | ||
6940 | if (!attr->func_info_cnt && !attr->line_info_cnt) | |
6941 | return 0; | |
6942 | ||
6943 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
6944 | if (IS_ERR(btf)) | |
6945 | return PTR_ERR(btf); | |
6946 | env->prog->aux->btf = btf; | |
6947 | ||
6948 | err = check_btf_func(env, attr, uattr); | |
6949 | if (err) | |
6950 | return err; | |
6951 | ||
6952 | err = check_btf_line(env, attr, uattr); | |
6953 | if (err) | |
6954 | return err; | |
6955 | ||
6956 | return 0; | |
ba64e7d8 YS |
6957 | } |
6958 | ||
f1174f77 EC |
6959 | /* check %cur's range satisfies %old's */ |
6960 | static bool range_within(struct bpf_reg_state *old, | |
6961 | struct bpf_reg_state *cur) | |
6962 | { | |
b03c9f9f EC |
6963 | return old->umin_value <= cur->umin_value && |
6964 | old->umax_value >= cur->umax_value && | |
6965 | old->smin_value <= cur->smin_value && | |
6966 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
6967 | } |
6968 | ||
6969 | /* Maximum number of register states that can exist at once */ | |
6970 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
6971 | struct idpair { | |
6972 | u32 old; | |
6973 | u32 cur; | |
6974 | }; | |
6975 | ||
6976 | /* If in the old state two registers had the same id, then they need to have | |
6977 | * the same id in the new state as well. But that id could be different from | |
6978 | * the old state, so we need to track the mapping from old to new ids. | |
6979 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
6980 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
6981 | * regs with a different old id could still have new id 9, we don't care about | |
6982 | * that. | |
6983 | * So we look through our idmap to see if this old id has been seen before. If | |
6984 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 6985 | */ |
f1174f77 | 6986 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 6987 | { |
f1174f77 | 6988 | unsigned int i; |
969bf05e | 6989 | |
f1174f77 EC |
6990 | for (i = 0; i < ID_MAP_SIZE; i++) { |
6991 | if (!idmap[i].old) { | |
6992 | /* Reached an empty slot; haven't seen this id before */ | |
6993 | idmap[i].old = old_id; | |
6994 | idmap[i].cur = cur_id; | |
6995 | return true; | |
6996 | } | |
6997 | if (idmap[i].old == old_id) | |
6998 | return idmap[i].cur == cur_id; | |
6999 | } | |
7000 | /* We ran out of idmap slots, which should be impossible */ | |
7001 | WARN_ON_ONCE(1); | |
7002 | return false; | |
7003 | } | |
7004 | ||
9242b5f5 AS |
7005 | static void clean_func_state(struct bpf_verifier_env *env, |
7006 | struct bpf_func_state *st) | |
7007 | { | |
7008 | enum bpf_reg_liveness live; | |
7009 | int i, j; | |
7010 | ||
7011 | for (i = 0; i < BPF_REG_FP; i++) { | |
7012 | live = st->regs[i].live; | |
7013 | /* liveness must not touch this register anymore */ | |
7014 | st->regs[i].live |= REG_LIVE_DONE; | |
7015 | if (!(live & REG_LIVE_READ)) | |
7016 | /* since the register is unused, clear its state | |
7017 | * to make further comparison simpler | |
7018 | */ | |
f54c7898 | 7019 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
7020 | } |
7021 | ||
7022 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
7023 | live = st->stack[i].spilled_ptr.live; | |
7024 | /* liveness must not touch this stack slot anymore */ | |
7025 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
7026 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 7027 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
7028 | for (j = 0; j < BPF_REG_SIZE; j++) |
7029 | st->stack[i].slot_type[j] = STACK_INVALID; | |
7030 | } | |
7031 | } | |
7032 | } | |
7033 | ||
7034 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
7035 | struct bpf_verifier_state *st) | |
7036 | { | |
7037 | int i; | |
7038 | ||
7039 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
7040 | /* all regs in this state in all frames were already marked */ | |
7041 | return; | |
7042 | ||
7043 | for (i = 0; i <= st->curframe; i++) | |
7044 | clean_func_state(env, st->frame[i]); | |
7045 | } | |
7046 | ||
7047 | /* the parentage chains form a tree. | |
7048 | * the verifier states are added to state lists at given insn and | |
7049 | * pushed into state stack for future exploration. | |
7050 | * when the verifier reaches bpf_exit insn some of the verifer states | |
7051 | * stored in the state lists have their final liveness state already, | |
7052 | * but a lot of states will get revised from liveness point of view when | |
7053 | * the verifier explores other branches. | |
7054 | * Example: | |
7055 | * 1: r0 = 1 | |
7056 | * 2: if r1 == 100 goto pc+1 | |
7057 | * 3: r0 = 2 | |
7058 | * 4: exit | |
7059 | * when the verifier reaches exit insn the register r0 in the state list of | |
7060 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
7061 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
7062 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
7063 | * | |
7064 | * Since the verifier pushes the branch states as it sees them while exploring | |
7065 | * the program the condition of walking the branch instruction for the second | |
7066 | * time means that all states below this branch were already explored and | |
7067 | * their final liveness markes are already propagated. | |
7068 | * Hence when the verifier completes the search of state list in is_state_visited() | |
7069 | * we can call this clean_live_states() function to mark all liveness states | |
7070 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
7071 | * will not be used. | |
7072 | * This function also clears the registers and stack for states that !READ | |
7073 | * to simplify state merging. | |
7074 | * | |
7075 | * Important note here that walking the same branch instruction in the callee | |
7076 | * doesn't meant that the states are DONE. The verifier has to compare | |
7077 | * the callsites | |
7078 | */ | |
7079 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
7080 | struct bpf_verifier_state *cur) | |
7081 | { | |
7082 | struct bpf_verifier_state_list *sl; | |
7083 | int i; | |
7084 | ||
5d839021 | 7085 | sl = *explored_state(env, insn); |
a8f500af | 7086 | while (sl) { |
2589726d AS |
7087 | if (sl->state.branches) |
7088 | goto next; | |
dc2a4ebc AS |
7089 | if (sl->state.insn_idx != insn || |
7090 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
7091 | goto next; |
7092 | for (i = 0; i <= cur->curframe; i++) | |
7093 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
7094 | goto next; | |
7095 | clean_verifier_state(env, &sl->state); | |
7096 | next: | |
7097 | sl = sl->next; | |
7098 | } | |
7099 | } | |
7100 | ||
f1174f77 | 7101 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
7102 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
7103 | struct idpair *idmap) | |
f1174f77 | 7104 | { |
f4d7e40a AS |
7105 | bool equal; |
7106 | ||
dc503a8a EC |
7107 | if (!(rold->live & REG_LIVE_READ)) |
7108 | /* explored state didn't use this */ | |
7109 | return true; | |
7110 | ||
679c782d | 7111 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
7112 | |
7113 | if (rold->type == PTR_TO_STACK) | |
7114 | /* two stack pointers are equal only if they're pointing to | |
7115 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
7116 | */ | |
7117 | return equal && rold->frameno == rcur->frameno; | |
7118 | ||
7119 | if (equal) | |
969bf05e AS |
7120 | return true; |
7121 | ||
f1174f77 EC |
7122 | if (rold->type == NOT_INIT) |
7123 | /* explored state can't have used this */ | |
969bf05e | 7124 | return true; |
f1174f77 EC |
7125 | if (rcur->type == NOT_INIT) |
7126 | return false; | |
7127 | switch (rold->type) { | |
7128 | case SCALAR_VALUE: | |
7129 | if (rcur->type == SCALAR_VALUE) { | |
b5dc0163 AS |
7130 | if (!rold->precise && !rcur->precise) |
7131 | return true; | |
f1174f77 EC |
7132 | /* new val must satisfy old val knowledge */ |
7133 | return range_within(rold, rcur) && | |
7134 | tnum_in(rold->var_off, rcur->var_off); | |
7135 | } else { | |
179d1c56 JH |
7136 | /* We're trying to use a pointer in place of a scalar. |
7137 | * Even if the scalar was unbounded, this could lead to | |
7138 | * pointer leaks because scalars are allowed to leak | |
7139 | * while pointers are not. We could make this safe in | |
7140 | * special cases if root is calling us, but it's | |
7141 | * probably not worth the hassle. | |
f1174f77 | 7142 | */ |
179d1c56 | 7143 | return false; |
f1174f77 EC |
7144 | } |
7145 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
7146 | /* If the new min/max/var_off satisfy the old ones and |
7147 | * everything else matches, we are OK. | |
d83525ca AS |
7148 | * 'id' is not compared, since it's only used for maps with |
7149 | * bpf_spin_lock inside map element and in such cases if | |
7150 | * the rest of the prog is valid for one map element then | |
7151 | * it's valid for all map elements regardless of the key | |
7152 | * used in bpf_map_lookup() | |
1b688a19 EC |
7153 | */ |
7154 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
7155 | range_within(rold, rcur) && | |
7156 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
7157 | case PTR_TO_MAP_VALUE_OR_NULL: |
7158 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
7159 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
7160 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
7161 | * checked, doing so could have affected others with the same | |
7162 | * id, and we can't check for that because we lost the id when | |
7163 | * we converted to a PTR_TO_MAP_VALUE. | |
7164 | */ | |
7165 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
7166 | return false; | |
7167 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
7168 | return false; | |
7169 | /* Check our ids match any regs they're supposed to */ | |
7170 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 7171 | case PTR_TO_PACKET_META: |
f1174f77 | 7172 | case PTR_TO_PACKET: |
de8f3a83 | 7173 | if (rcur->type != rold->type) |
f1174f77 EC |
7174 | return false; |
7175 | /* We must have at least as much range as the old ptr | |
7176 | * did, so that any accesses which were safe before are | |
7177 | * still safe. This is true even if old range < old off, | |
7178 | * since someone could have accessed through (ptr - k), or | |
7179 | * even done ptr -= k in a register, to get a safe access. | |
7180 | */ | |
7181 | if (rold->range > rcur->range) | |
7182 | return false; | |
7183 | /* If the offsets don't match, we can't trust our alignment; | |
7184 | * nor can we be sure that we won't fall out of range. | |
7185 | */ | |
7186 | if (rold->off != rcur->off) | |
7187 | return false; | |
7188 | /* id relations must be preserved */ | |
7189 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
7190 | return false; | |
7191 | /* new val must satisfy old val knowledge */ | |
7192 | return range_within(rold, rcur) && | |
7193 | tnum_in(rold->var_off, rcur->var_off); | |
7194 | case PTR_TO_CTX: | |
7195 | case CONST_PTR_TO_MAP: | |
f1174f77 | 7196 | case PTR_TO_PACKET_END: |
d58e468b | 7197 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
7198 | case PTR_TO_SOCKET: |
7199 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
7200 | case PTR_TO_SOCK_COMMON: |
7201 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
7202 | case PTR_TO_TCP_SOCK: |
7203 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 7204 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
7205 | /* Only valid matches are exact, which memcmp() above |
7206 | * would have accepted | |
7207 | */ | |
7208 | default: | |
7209 | /* Don't know what's going on, just say it's not safe */ | |
7210 | return false; | |
7211 | } | |
969bf05e | 7212 | |
f1174f77 EC |
7213 | /* Shouldn't get here; if we do, say it's not safe */ |
7214 | WARN_ON_ONCE(1); | |
969bf05e AS |
7215 | return false; |
7216 | } | |
7217 | ||
f4d7e40a AS |
7218 | static bool stacksafe(struct bpf_func_state *old, |
7219 | struct bpf_func_state *cur, | |
638f5b90 AS |
7220 | struct idpair *idmap) |
7221 | { | |
7222 | int i, spi; | |
7223 | ||
638f5b90 AS |
7224 | /* walk slots of the explored stack and ignore any additional |
7225 | * slots in the current stack, since explored(safe) state | |
7226 | * didn't use them | |
7227 | */ | |
7228 | for (i = 0; i < old->allocated_stack; i++) { | |
7229 | spi = i / BPF_REG_SIZE; | |
7230 | ||
b233920c AS |
7231 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
7232 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 7233 | /* explored state didn't use this */ |
fd05e57b | 7234 | continue; |
b233920c | 7235 | } |
cc2b14d5 | 7236 | |
638f5b90 AS |
7237 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
7238 | continue; | |
19e2dbb7 AS |
7239 | |
7240 | /* explored stack has more populated slots than current stack | |
7241 | * and these slots were used | |
7242 | */ | |
7243 | if (i >= cur->allocated_stack) | |
7244 | return false; | |
7245 | ||
cc2b14d5 AS |
7246 | /* if old state was safe with misc data in the stack |
7247 | * it will be safe with zero-initialized stack. | |
7248 | * The opposite is not true | |
7249 | */ | |
7250 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
7251 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
7252 | continue; | |
638f5b90 AS |
7253 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
7254 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
7255 | /* Ex: old explored (safe) state has STACK_SPILL in | |
7256 | * this stack slot, but current has has STACK_MISC -> | |
7257 | * this verifier states are not equivalent, | |
7258 | * return false to continue verification of this path | |
7259 | */ | |
7260 | return false; | |
7261 | if (i % BPF_REG_SIZE) | |
7262 | continue; | |
7263 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
7264 | continue; | |
7265 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
7266 | &cur->stack[spi].spilled_ptr, | |
7267 | idmap)) | |
7268 | /* when explored and current stack slot are both storing | |
7269 | * spilled registers, check that stored pointers types | |
7270 | * are the same as well. | |
7271 | * Ex: explored safe path could have stored | |
7272 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
7273 | * but current path has stored: | |
7274 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
7275 | * such verifier states are not equivalent. | |
7276 | * return false to continue verification of this path | |
7277 | */ | |
7278 | return false; | |
7279 | } | |
7280 | return true; | |
7281 | } | |
7282 | ||
fd978bf7 JS |
7283 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
7284 | { | |
7285 | if (old->acquired_refs != cur->acquired_refs) | |
7286 | return false; | |
7287 | return !memcmp(old->refs, cur->refs, | |
7288 | sizeof(*old->refs) * old->acquired_refs); | |
7289 | } | |
7290 | ||
f1bca824 AS |
7291 | /* compare two verifier states |
7292 | * | |
7293 | * all states stored in state_list are known to be valid, since | |
7294 | * verifier reached 'bpf_exit' instruction through them | |
7295 | * | |
7296 | * this function is called when verifier exploring different branches of | |
7297 | * execution popped from the state stack. If it sees an old state that has | |
7298 | * more strict register state and more strict stack state then this execution | |
7299 | * branch doesn't need to be explored further, since verifier already | |
7300 | * concluded that more strict state leads to valid finish. | |
7301 | * | |
7302 | * Therefore two states are equivalent if register state is more conservative | |
7303 | * and explored stack state is more conservative than the current one. | |
7304 | * Example: | |
7305 | * explored current | |
7306 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
7307 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
7308 | * | |
7309 | * In other words if current stack state (one being explored) has more | |
7310 | * valid slots than old one that already passed validation, it means | |
7311 | * the verifier can stop exploring and conclude that current state is valid too | |
7312 | * | |
7313 | * Similarly with registers. If explored state has register type as invalid | |
7314 | * whereas register type in current state is meaningful, it means that | |
7315 | * the current state will reach 'bpf_exit' instruction safely | |
7316 | */ | |
f4d7e40a AS |
7317 | static bool func_states_equal(struct bpf_func_state *old, |
7318 | struct bpf_func_state *cur) | |
f1bca824 | 7319 | { |
f1174f77 EC |
7320 | struct idpair *idmap; |
7321 | bool ret = false; | |
f1bca824 AS |
7322 | int i; |
7323 | ||
f1174f77 EC |
7324 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
7325 | /* If we failed to allocate the idmap, just say it's not safe */ | |
7326 | if (!idmap) | |
1a0dc1ac | 7327 | return false; |
f1174f77 EC |
7328 | |
7329 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 7330 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 7331 | goto out_free; |
f1bca824 AS |
7332 | } |
7333 | ||
638f5b90 AS |
7334 | if (!stacksafe(old, cur, idmap)) |
7335 | goto out_free; | |
fd978bf7 JS |
7336 | |
7337 | if (!refsafe(old, cur)) | |
7338 | goto out_free; | |
f1174f77 EC |
7339 | ret = true; |
7340 | out_free: | |
7341 | kfree(idmap); | |
7342 | return ret; | |
f1bca824 AS |
7343 | } |
7344 | ||
f4d7e40a AS |
7345 | static bool states_equal(struct bpf_verifier_env *env, |
7346 | struct bpf_verifier_state *old, | |
7347 | struct bpf_verifier_state *cur) | |
7348 | { | |
7349 | int i; | |
7350 | ||
7351 | if (old->curframe != cur->curframe) | |
7352 | return false; | |
7353 | ||
979d63d5 DB |
7354 | /* Verification state from speculative execution simulation |
7355 | * must never prune a non-speculative execution one. | |
7356 | */ | |
7357 | if (old->speculative && !cur->speculative) | |
7358 | return false; | |
7359 | ||
d83525ca AS |
7360 | if (old->active_spin_lock != cur->active_spin_lock) |
7361 | return false; | |
7362 | ||
f4d7e40a AS |
7363 | /* for states to be equal callsites have to be the same |
7364 | * and all frame states need to be equivalent | |
7365 | */ | |
7366 | for (i = 0; i <= old->curframe; i++) { | |
7367 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
7368 | return false; | |
7369 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
7370 | return false; | |
7371 | } | |
7372 | return true; | |
7373 | } | |
7374 | ||
5327ed3d JW |
7375 | /* Return 0 if no propagation happened. Return negative error code if error |
7376 | * happened. Otherwise, return the propagated bit. | |
7377 | */ | |
55e7f3b5 JW |
7378 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
7379 | struct bpf_reg_state *reg, | |
7380 | struct bpf_reg_state *parent_reg) | |
7381 | { | |
5327ed3d JW |
7382 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
7383 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
7384 | int err; |
7385 | ||
5327ed3d JW |
7386 | /* When comes here, read flags of PARENT_REG or REG could be any of |
7387 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
7388 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
7389 | */ | |
7390 | if (parent_flag == REG_LIVE_READ64 || | |
7391 | /* Or if there is no read flag from REG. */ | |
7392 | !flag || | |
7393 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
7394 | parent_flag == flag) | |
55e7f3b5 JW |
7395 | return 0; |
7396 | ||
5327ed3d | 7397 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
7398 | if (err) |
7399 | return err; | |
7400 | ||
5327ed3d | 7401 | return flag; |
55e7f3b5 JW |
7402 | } |
7403 | ||
8e9cd9ce | 7404 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
7405 | * straight-line code between a state and its parent. When we arrive at an |
7406 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
7407 | * code, so read marks in the state must propagate to the parent regardless | |
7408 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 7409 | * in mark_reg_read() is for. |
8e9cd9ce | 7410 | */ |
f4d7e40a AS |
7411 | static int propagate_liveness(struct bpf_verifier_env *env, |
7412 | const struct bpf_verifier_state *vstate, | |
7413 | struct bpf_verifier_state *vparent) | |
dc503a8a | 7414 | { |
3f8cafa4 | 7415 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 7416 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 7417 | int i, frame, err = 0; |
dc503a8a | 7418 | |
f4d7e40a AS |
7419 | if (vparent->curframe != vstate->curframe) { |
7420 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
7421 | vparent->curframe, vstate->curframe); | |
7422 | return -EFAULT; | |
7423 | } | |
dc503a8a EC |
7424 | /* Propagate read liveness of registers... */ |
7425 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 7426 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
7427 | parent = vparent->frame[frame]; |
7428 | state = vstate->frame[frame]; | |
7429 | parent_reg = parent->regs; | |
7430 | state_reg = state->regs; | |
83d16312 JK |
7431 | /* We don't need to worry about FP liveness, it's read-only */ |
7432 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
7433 | err = propagate_liveness_reg(env, &state_reg[i], |
7434 | &parent_reg[i]); | |
5327ed3d | 7435 | if (err < 0) |
3f8cafa4 | 7436 | return err; |
5327ed3d JW |
7437 | if (err == REG_LIVE_READ64) |
7438 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 7439 | } |
f4d7e40a | 7440 | |
1b04aee7 | 7441 | /* Propagate stack slots. */ |
f4d7e40a AS |
7442 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
7443 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
7444 | parent_reg = &parent->stack[i].spilled_ptr; |
7445 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
7446 | err = propagate_liveness_reg(env, state_reg, |
7447 | parent_reg); | |
5327ed3d | 7448 | if (err < 0) |
3f8cafa4 | 7449 | return err; |
dc503a8a EC |
7450 | } |
7451 | } | |
5327ed3d | 7452 | return 0; |
dc503a8a EC |
7453 | } |
7454 | ||
a3ce685d AS |
7455 | /* find precise scalars in the previous equivalent state and |
7456 | * propagate them into the current state | |
7457 | */ | |
7458 | static int propagate_precision(struct bpf_verifier_env *env, | |
7459 | const struct bpf_verifier_state *old) | |
7460 | { | |
7461 | struct bpf_reg_state *state_reg; | |
7462 | struct bpf_func_state *state; | |
7463 | int i, err = 0; | |
7464 | ||
7465 | state = old->frame[old->curframe]; | |
7466 | state_reg = state->regs; | |
7467 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
7468 | if (state_reg->type != SCALAR_VALUE || | |
7469 | !state_reg->precise) | |
7470 | continue; | |
7471 | if (env->log.level & BPF_LOG_LEVEL2) | |
7472 | verbose(env, "propagating r%d\n", i); | |
7473 | err = mark_chain_precision(env, i); | |
7474 | if (err < 0) | |
7475 | return err; | |
7476 | } | |
7477 | ||
7478 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
7479 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
7480 | continue; | |
7481 | state_reg = &state->stack[i].spilled_ptr; | |
7482 | if (state_reg->type != SCALAR_VALUE || | |
7483 | !state_reg->precise) | |
7484 | continue; | |
7485 | if (env->log.level & BPF_LOG_LEVEL2) | |
7486 | verbose(env, "propagating fp%d\n", | |
7487 | (-i - 1) * BPF_REG_SIZE); | |
7488 | err = mark_chain_precision_stack(env, i); | |
7489 | if (err < 0) | |
7490 | return err; | |
7491 | } | |
7492 | return 0; | |
7493 | } | |
7494 | ||
2589726d AS |
7495 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
7496 | struct bpf_verifier_state *cur) | |
7497 | { | |
7498 | struct bpf_func_state *fold, *fcur; | |
7499 | int i, fr = cur->curframe; | |
7500 | ||
7501 | if (old->curframe != fr) | |
7502 | return false; | |
7503 | ||
7504 | fold = old->frame[fr]; | |
7505 | fcur = cur->frame[fr]; | |
7506 | for (i = 0; i < MAX_BPF_REG; i++) | |
7507 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
7508 | offsetof(struct bpf_reg_state, parent))) | |
7509 | return false; | |
7510 | return true; | |
7511 | } | |
7512 | ||
7513 | ||
58e2af8b | 7514 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 7515 | { |
58e2af8b | 7516 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 7517 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 7518 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 7519 | int i, j, err, states_cnt = 0; |
10d274e8 | 7520 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 7521 | |
b5dc0163 | 7522 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 7523 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
7524 | /* this 'insn_idx' instruction wasn't marked, so we will not |
7525 | * be doing state search here | |
7526 | */ | |
7527 | return 0; | |
7528 | ||
2589726d AS |
7529 | /* bpf progs typically have pruning point every 4 instructions |
7530 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
7531 | * Do not add new state for future pruning if the verifier hasn't seen | |
7532 | * at least 2 jumps and at least 8 instructions. | |
7533 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
7534 | * In tests that amounts to up to 50% reduction into total verifier | |
7535 | * memory consumption and 20% verifier time speedup. | |
7536 | */ | |
7537 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
7538 | env->insn_processed - env->prev_insn_processed >= 8) | |
7539 | add_new_state = true; | |
7540 | ||
a8f500af AS |
7541 | pprev = explored_state(env, insn_idx); |
7542 | sl = *pprev; | |
7543 | ||
9242b5f5 AS |
7544 | clean_live_states(env, insn_idx, cur); |
7545 | ||
a8f500af | 7546 | while (sl) { |
dc2a4ebc AS |
7547 | states_cnt++; |
7548 | if (sl->state.insn_idx != insn_idx) | |
7549 | goto next; | |
2589726d AS |
7550 | if (sl->state.branches) { |
7551 | if (states_maybe_looping(&sl->state, cur) && | |
7552 | states_equal(env, &sl->state, cur)) { | |
7553 | verbose_linfo(env, insn_idx, "; "); | |
7554 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
7555 | return -EINVAL; | |
7556 | } | |
7557 | /* if the verifier is processing a loop, avoid adding new state | |
7558 | * too often, since different loop iterations have distinct | |
7559 | * states and may not help future pruning. | |
7560 | * This threshold shouldn't be too low to make sure that | |
7561 | * a loop with large bound will be rejected quickly. | |
7562 | * The most abusive loop will be: | |
7563 | * r1 += 1 | |
7564 | * if r1 < 1000000 goto pc-2 | |
7565 | * 1M insn_procssed limit / 100 == 10k peak states. | |
7566 | * This threshold shouldn't be too high either, since states | |
7567 | * at the end of the loop are likely to be useful in pruning. | |
7568 | */ | |
7569 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
7570 | env->insn_processed - env->prev_insn_processed < 100) | |
7571 | add_new_state = false; | |
7572 | goto miss; | |
7573 | } | |
638f5b90 | 7574 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 7575 | sl->hit_cnt++; |
f1bca824 | 7576 | /* reached equivalent register/stack state, |
dc503a8a EC |
7577 | * prune the search. |
7578 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
7579 | * If we have any write marks in env->cur_state, they |
7580 | * will prevent corresponding reads in the continuation | |
7581 | * from reaching our parent (an explored_state). Our | |
7582 | * own state will get the read marks recorded, but | |
7583 | * they'll be immediately forgotten as we're pruning | |
7584 | * this state and will pop a new one. | |
f1bca824 | 7585 | */ |
f4d7e40a | 7586 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
7587 | |
7588 | /* if previous state reached the exit with precision and | |
7589 | * current state is equivalent to it (except precsion marks) | |
7590 | * the precision needs to be propagated back in | |
7591 | * the current state. | |
7592 | */ | |
7593 | err = err ? : push_jmp_history(env, cur); | |
7594 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
7595 | if (err) |
7596 | return err; | |
f1bca824 | 7597 | return 1; |
dc503a8a | 7598 | } |
2589726d AS |
7599 | miss: |
7600 | /* when new state is not going to be added do not increase miss count. | |
7601 | * Otherwise several loop iterations will remove the state | |
7602 | * recorded earlier. The goal of these heuristics is to have | |
7603 | * states from some iterations of the loop (some in the beginning | |
7604 | * and some at the end) to help pruning. | |
7605 | */ | |
7606 | if (add_new_state) | |
7607 | sl->miss_cnt++; | |
9f4686c4 AS |
7608 | /* heuristic to determine whether this state is beneficial |
7609 | * to keep checking from state equivalence point of view. | |
7610 | * Higher numbers increase max_states_per_insn and verification time, | |
7611 | * but do not meaningfully decrease insn_processed. | |
7612 | */ | |
7613 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
7614 | /* the state is unlikely to be useful. Remove it to | |
7615 | * speed up verification | |
7616 | */ | |
7617 | *pprev = sl->next; | |
7618 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
7619 | u32 br = sl->state.branches; |
7620 | ||
7621 | WARN_ONCE(br, | |
7622 | "BUG live_done but branches_to_explore %d\n", | |
7623 | br); | |
9f4686c4 AS |
7624 | free_verifier_state(&sl->state, false); |
7625 | kfree(sl); | |
7626 | env->peak_states--; | |
7627 | } else { | |
7628 | /* cannot free this state, since parentage chain may | |
7629 | * walk it later. Add it for free_list instead to | |
7630 | * be freed at the end of verification | |
7631 | */ | |
7632 | sl->next = env->free_list; | |
7633 | env->free_list = sl; | |
7634 | } | |
7635 | sl = *pprev; | |
7636 | continue; | |
7637 | } | |
dc2a4ebc | 7638 | next: |
9f4686c4 AS |
7639 | pprev = &sl->next; |
7640 | sl = *pprev; | |
f1bca824 AS |
7641 | } |
7642 | ||
06ee7115 AS |
7643 | if (env->max_states_per_insn < states_cnt) |
7644 | env->max_states_per_insn = states_cnt; | |
7645 | ||
ceefbc96 | 7646 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 7647 | return push_jmp_history(env, cur); |
ceefbc96 | 7648 | |
2589726d | 7649 | if (!add_new_state) |
b5dc0163 | 7650 | return push_jmp_history(env, cur); |
ceefbc96 | 7651 | |
2589726d AS |
7652 | /* There were no equivalent states, remember the current one. |
7653 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 7654 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 7655 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 7656 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
7657 | * again on the way to bpf_exit. |
7658 | * When looping the sl->state.branches will be > 0 and this state | |
7659 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 7660 | */ |
638f5b90 | 7661 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
7662 | if (!new_sl) |
7663 | return -ENOMEM; | |
06ee7115 AS |
7664 | env->total_states++; |
7665 | env->peak_states++; | |
2589726d AS |
7666 | env->prev_jmps_processed = env->jmps_processed; |
7667 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
7668 | |
7669 | /* add new state to the head of linked list */ | |
679c782d EC |
7670 | new = &new_sl->state; |
7671 | err = copy_verifier_state(new, cur); | |
1969db47 | 7672 | if (err) { |
679c782d | 7673 | free_verifier_state(new, false); |
1969db47 AS |
7674 | kfree(new_sl); |
7675 | return err; | |
7676 | } | |
dc2a4ebc | 7677 | new->insn_idx = insn_idx; |
2589726d AS |
7678 | WARN_ONCE(new->branches != 1, |
7679 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 7680 | |
2589726d | 7681 | cur->parent = new; |
b5dc0163 AS |
7682 | cur->first_insn_idx = insn_idx; |
7683 | clear_jmp_history(cur); | |
5d839021 AS |
7684 | new_sl->next = *explored_state(env, insn_idx); |
7685 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
7686 | /* connect new state to parentage chain. Current frame needs all |
7687 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
7688 | * to the stack implicitly by JITs) so in callers' frames connect just | |
7689 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
7690 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
7691 | * from callee with its full parentage chain, anyway. | |
7692 | */ | |
8e9cd9ce EC |
7693 | /* clear write marks in current state: the writes we did are not writes |
7694 | * our child did, so they don't screen off its reads from us. | |
7695 | * (There are no read marks in current state, because reads always mark | |
7696 | * their parent and current state never has children yet. Only | |
7697 | * explored_states can get read marks.) | |
7698 | */ | |
eea1c227 AS |
7699 | for (j = 0; j <= cur->curframe; j++) { |
7700 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
7701 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
7702 | for (i = 0; i < BPF_REG_FP; i++) | |
7703 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
7704 | } | |
f4d7e40a AS |
7705 | |
7706 | /* all stack frames are accessible from callee, clear them all */ | |
7707 | for (j = 0; j <= cur->curframe; j++) { | |
7708 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 7709 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 7710 | |
679c782d | 7711 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 7712 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
7713 | frame->stack[i].spilled_ptr.parent = |
7714 | &newframe->stack[i].spilled_ptr; | |
7715 | } | |
f4d7e40a | 7716 | } |
f1bca824 AS |
7717 | return 0; |
7718 | } | |
7719 | ||
c64b7983 JS |
7720 | /* Return true if it's OK to have the same insn return a different type. */ |
7721 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
7722 | { | |
7723 | switch (type) { | |
7724 | case PTR_TO_CTX: | |
7725 | case PTR_TO_SOCKET: | |
7726 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
7727 | case PTR_TO_SOCK_COMMON: |
7728 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
7729 | case PTR_TO_TCP_SOCK: |
7730 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 7731 | case PTR_TO_XDP_SOCK: |
2a02759e | 7732 | case PTR_TO_BTF_ID: |
c64b7983 JS |
7733 | return false; |
7734 | default: | |
7735 | return true; | |
7736 | } | |
7737 | } | |
7738 | ||
7739 | /* If an instruction was previously used with particular pointer types, then we | |
7740 | * need to be careful to avoid cases such as the below, where it may be ok | |
7741 | * for one branch accessing the pointer, but not ok for the other branch: | |
7742 | * | |
7743 | * R1 = sock_ptr | |
7744 | * goto X; | |
7745 | * ... | |
7746 | * R1 = some_other_valid_ptr; | |
7747 | * goto X; | |
7748 | * ... | |
7749 | * R2 = *(u32 *)(R1 + 0); | |
7750 | */ | |
7751 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
7752 | { | |
7753 | return src != prev && (!reg_type_mismatch_ok(src) || | |
7754 | !reg_type_mismatch_ok(prev)); | |
7755 | } | |
7756 | ||
58e2af8b | 7757 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 7758 | { |
638f5b90 | 7759 | struct bpf_verifier_state *state; |
17a52670 | 7760 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 7761 | struct bpf_reg_state *regs; |
06ee7115 | 7762 | int insn_cnt = env->prog->len; |
17a52670 | 7763 | bool do_print_state = false; |
b5dc0163 | 7764 | int prev_insn_idx = -1; |
17a52670 | 7765 | |
d9762e84 MKL |
7766 | env->prev_linfo = NULL; |
7767 | ||
638f5b90 AS |
7768 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
7769 | if (!state) | |
7770 | return -ENOMEM; | |
f4d7e40a | 7771 | state->curframe = 0; |
979d63d5 | 7772 | state->speculative = false; |
2589726d | 7773 | state->branches = 1; |
f4d7e40a AS |
7774 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
7775 | if (!state->frame[0]) { | |
7776 | kfree(state); | |
7777 | return -ENOMEM; | |
7778 | } | |
7779 | env->cur_state = state; | |
7780 | init_func_state(env, state->frame[0], | |
7781 | BPF_MAIN_FUNC /* callsite */, | |
7782 | 0 /* frameno */, | |
7783 | 0 /* subprogno, zero == main subprog */); | |
c08435ec | 7784 | |
8c1b6e69 AS |
7785 | if (btf_check_func_arg_match(env, 0)) |
7786 | return -EINVAL; | |
7787 | ||
17a52670 AS |
7788 | for (;;) { |
7789 | struct bpf_insn *insn; | |
7790 | u8 class; | |
7791 | int err; | |
7792 | ||
b5dc0163 | 7793 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 7794 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 7795 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 7796 | env->insn_idx, insn_cnt); |
17a52670 AS |
7797 | return -EFAULT; |
7798 | } | |
7799 | ||
c08435ec | 7800 | insn = &insns[env->insn_idx]; |
17a52670 AS |
7801 | class = BPF_CLASS(insn->code); |
7802 | ||
06ee7115 | 7803 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
7804 | verbose(env, |
7805 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 7806 | env->insn_processed); |
17a52670 AS |
7807 | return -E2BIG; |
7808 | } | |
7809 | ||
c08435ec | 7810 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
7811 | if (err < 0) |
7812 | return err; | |
7813 | if (err == 1) { | |
7814 | /* found equivalent state, can prune the search */ | |
06ee7115 | 7815 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 7816 | if (do_print_state) |
979d63d5 DB |
7817 | verbose(env, "\nfrom %d to %d%s: safe\n", |
7818 | env->prev_insn_idx, env->insn_idx, | |
7819 | env->cur_state->speculative ? | |
7820 | " (speculative execution)" : ""); | |
f1bca824 | 7821 | else |
c08435ec | 7822 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
7823 | } |
7824 | goto process_bpf_exit; | |
7825 | } | |
7826 | ||
c3494801 AS |
7827 | if (signal_pending(current)) |
7828 | return -EAGAIN; | |
7829 | ||
3c2ce60b DB |
7830 | if (need_resched()) |
7831 | cond_resched(); | |
7832 | ||
06ee7115 AS |
7833 | if (env->log.level & BPF_LOG_LEVEL2 || |
7834 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
7835 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 7836 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 7837 | else |
979d63d5 DB |
7838 | verbose(env, "\nfrom %d to %d%s:", |
7839 | env->prev_insn_idx, env->insn_idx, | |
7840 | env->cur_state->speculative ? | |
7841 | " (speculative execution)" : ""); | |
f4d7e40a | 7842 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
7843 | do_print_state = false; |
7844 | } | |
7845 | ||
06ee7115 | 7846 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
7847 | const struct bpf_insn_cbs cbs = { |
7848 | .cb_print = verbose, | |
abe08840 | 7849 | .private_data = env, |
7105e828 DB |
7850 | }; |
7851 | ||
c08435ec DB |
7852 | verbose_linfo(env, env->insn_idx, "; "); |
7853 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 7854 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
7855 | } |
7856 | ||
cae1927c | 7857 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
7858 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
7859 | env->prev_insn_idx); | |
cae1927c JK |
7860 | if (err) |
7861 | return err; | |
7862 | } | |
13a27dfc | 7863 | |
638f5b90 | 7864 | regs = cur_regs(env); |
c08435ec | 7865 | env->insn_aux_data[env->insn_idx].seen = true; |
b5dc0163 | 7866 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 7867 | |
17a52670 | 7868 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 7869 | err = check_alu_op(env, insn); |
17a52670 AS |
7870 | if (err) |
7871 | return err; | |
7872 | ||
7873 | } else if (class == BPF_LDX) { | |
3df126f3 | 7874 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
7875 | |
7876 | /* check for reserved fields is already done */ | |
7877 | ||
17a52670 | 7878 | /* check src operand */ |
dc503a8a | 7879 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7880 | if (err) |
7881 | return err; | |
7882 | ||
dc503a8a | 7883 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
7884 | if (err) |
7885 | return err; | |
7886 | ||
725f9dcd AS |
7887 | src_reg_type = regs[insn->src_reg].type; |
7888 | ||
17a52670 AS |
7889 | /* check that memory (src_reg + off) is readable, |
7890 | * the state of dst_reg will be updated by this func | |
7891 | */ | |
c08435ec DB |
7892 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
7893 | insn->off, BPF_SIZE(insn->code), | |
7894 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
7895 | if (err) |
7896 | return err; | |
7897 | ||
c08435ec | 7898 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
7899 | |
7900 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
7901 | /* saw a valid insn |
7902 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 7903 | * save type to validate intersecting paths |
9bac3d6d | 7904 | */ |
3df126f3 | 7905 | *prev_src_type = src_reg_type; |
9bac3d6d | 7906 | |
c64b7983 | 7907 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
7908 | /* ABuser program is trying to use the same insn |
7909 | * dst_reg = *(u32*) (src_reg + off) | |
7910 | * with different pointer types: | |
7911 | * src_reg == ctx in one branch and | |
7912 | * src_reg == stack|map in some other branch. | |
7913 | * Reject it. | |
7914 | */ | |
61bd5218 | 7915 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
7916 | return -EINVAL; |
7917 | } | |
7918 | ||
17a52670 | 7919 | } else if (class == BPF_STX) { |
3df126f3 | 7920 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 7921 | |
17a52670 | 7922 | if (BPF_MODE(insn->code) == BPF_XADD) { |
c08435ec | 7923 | err = check_xadd(env, env->insn_idx, insn); |
17a52670 AS |
7924 | if (err) |
7925 | return err; | |
c08435ec | 7926 | env->insn_idx++; |
17a52670 AS |
7927 | continue; |
7928 | } | |
7929 | ||
17a52670 | 7930 | /* check src1 operand */ |
dc503a8a | 7931 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7932 | if (err) |
7933 | return err; | |
7934 | /* check src2 operand */ | |
dc503a8a | 7935 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7936 | if (err) |
7937 | return err; | |
7938 | ||
d691f9e8 AS |
7939 | dst_reg_type = regs[insn->dst_reg].type; |
7940 | ||
17a52670 | 7941 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
7942 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
7943 | insn->off, BPF_SIZE(insn->code), | |
7944 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
7945 | if (err) |
7946 | return err; | |
7947 | ||
c08435ec | 7948 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
7949 | |
7950 | if (*prev_dst_type == NOT_INIT) { | |
7951 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 7952 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 7953 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
7954 | return -EINVAL; |
7955 | } | |
7956 | ||
17a52670 AS |
7957 | } else if (class == BPF_ST) { |
7958 | if (BPF_MODE(insn->code) != BPF_MEM || | |
7959 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 7960 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
7961 | return -EINVAL; |
7962 | } | |
7963 | /* check src operand */ | |
dc503a8a | 7964 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7965 | if (err) |
7966 | return err; | |
7967 | ||
f37a8cb8 | 7968 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 7969 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
7970 | insn->dst_reg, |
7971 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
7972 | return -EACCES; |
7973 | } | |
7974 | ||
17a52670 | 7975 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
7976 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
7977 | insn->off, BPF_SIZE(insn->code), | |
7978 | BPF_WRITE, -1, false); | |
17a52670 AS |
7979 | if (err) |
7980 | return err; | |
7981 | ||
092ed096 | 7982 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
7983 | u8 opcode = BPF_OP(insn->code); |
7984 | ||
2589726d | 7985 | env->jmps_processed++; |
17a52670 AS |
7986 | if (opcode == BPF_CALL) { |
7987 | if (BPF_SRC(insn->code) != BPF_K || | |
7988 | insn->off != 0 || | |
f4d7e40a AS |
7989 | (insn->src_reg != BPF_REG_0 && |
7990 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
7991 | insn->dst_reg != BPF_REG_0 || |
7992 | class == BPF_JMP32) { | |
61bd5218 | 7993 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
7994 | return -EINVAL; |
7995 | } | |
7996 | ||
d83525ca AS |
7997 | if (env->cur_state->active_spin_lock && |
7998 | (insn->src_reg == BPF_PSEUDO_CALL || | |
7999 | insn->imm != BPF_FUNC_spin_unlock)) { | |
8000 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
8001 | return -EINVAL; | |
8002 | } | |
f4d7e40a | 8003 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 8004 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 8005 | else |
c08435ec | 8006 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
8007 | if (err) |
8008 | return err; | |
8009 | ||
8010 | } else if (opcode == BPF_JA) { | |
8011 | if (BPF_SRC(insn->code) != BPF_K || | |
8012 | insn->imm != 0 || | |
8013 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
8014 | insn->dst_reg != BPF_REG_0 || |
8015 | class == BPF_JMP32) { | |
61bd5218 | 8016 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
8017 | return -EINVAL; |
8018 | } | |
8019 | ||
c08435ec | 8020 | env->insn_idx += insn->off + 1; |
17a52670 AS |
8021 | continue; |
8022 | ||
8023 | } else if (opcode == BPF_EXIT) { | |
8024 | if (BPF_SRC(insn->code) != BPF_K || | |
8025 | insn->imm != 0 || | |
8026 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
8027 | insn->dst_reg != BPF_REG_0 || |
8028 | class == BPF_JMP32) { | |
61bd5218 | 8029 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
8030 | return -EINVAL; |
8031 | } | |
8032 | ||
d83525ca AS |
8033 | if (env->cur_state->active_spin_lock) { |
8034 | verbose(env, "bpf_spin_unlock is missing\n"); | |
8035 | return -EINVAL; | |
8036 | } | |
8037 | ||
f4d7e40a AS |
8038 | if (state->curframe) { |
8039 | /* exit from nested function */ | |
c08435ec | 8040 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
8041 | if (err) |
8042 | return err; | |
8043 | do_print_state = true; | |
8044 | continue; | |
8045 | } | |
8046 | ||
fd978bf7 JS |
8047 | err = check_reference_leak(env); |
8048 | if (err) | |
8049 | return err; | |
8050 | ||
390ee7e2 AS |
8051 | err = check_return_code(env); |
8052 | if (err) | |
8053 | return err; | |
f1bca824 | 8054 | process_bpf_exit: |
2589726d | 8055 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 8056 | err = pop_stack(env, &prev_insn_idx, |
c08435ec | 8057 | &env->insn_idx); |
638f5b90 AS |
8058 | if (err < 0) { |
8059 | if (err != -ENOENT) | |
8060 | return err; | |
17a52670 AS |
8061 | break; |
8062 | } else { | |
8063 | do_print_state = true; | |
8064 | continue; | |
8065 | } | |
8066 | } else { | |
c08435ec | 8067 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
8068 | if (err) |
8069 | return err; | |
8070 | } | |
8071 | } else if (class == BPF_LD) { | |
8072 | u8 mode = BPF_MODE(insn->code); | |
8073 | ||
8074 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
8075 | err = check_ld_abs(env, insn); |
8076 | if (err) | |
8077 | return err; | |
8078 | ||
17a52670 AS |
8079 | } else if (mode == BPF_IMM) { |
8080 | err = check_ld_imm(env, insn); | |
8081 | if (err) | |
8082 | return err; | |
8083 | ||
c08435ec DB |
8084 | env->insn_idx++; |
8085 | env->insn_aux_data[env->insn_idx].seen = true; | |
17a52670 | 8086 | } else { |
61bd5218 | 8087 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
8088 | return -EINVAL; |
8089 | } | |
8090 | } else { | |
61bd5218 | 8091 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
8092 | return -EINVAL; |
8093 | } | |
8094 | ||
c08435ec | 8095 | env->insn_idx++; |
17a52670 AS |
8096 | } |
8097 | ||
9c8105bd | 8098 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
17a52670 AS |
8099 | return 0; |
8100 | } | |
8101 | ||
56f668df MKL |
8102 | static int check_map_prealloc(struct bpf_map *map) |
8103 | { | |
8104 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
8105 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
8106 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
8107 | !(map->map_flags & BPF_F_NO_PREALLOC); |
8108 | } | |
8109 | ||
d83525ca AS |
8110 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
8111 | { | |
8112 | switch (type) { | |
8113 | case BPF_PROG_TYPE_KPROBE: | |
8114 | case BPF_PROG_TYPE_TRACEPOINT: | |
8115 | case BPF_PROG_TYPE_PERF_EVENT: | |
8116 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
8117 | return true; | |
8118 | default: | |
8119 | return false; | |
8120 | } | |
8121 | } | |
8122 | ||
61bd5218 JK |
8123 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
8124 | struct bpf_map *map, | |
fdc15d38 AS |
8125 | struct bpf_prog *prog) |
8126 | ||
8127 | { | |
56f668df MKL |
8128 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
8129 | * preallocated hash maps, since doing memory allocation | |
8130 | * in overflow_handler can crash depending on where nmi got | |
8131 | * triggered. | |
8132 | */ | |
8133 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
8134 | if (!check_map_prealloc(map)) { | |
61bd5218 | 8135 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
8136 | return -EINVAL; |
8137 | } | |
8138 | if (map->inner_map_meta && | |
8139 | !check_map_prealloc(map->inner_map_meta)) { | |
61bd5218 | 8140 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
56f668df MKL |
8141 | return -EINVAL; |
8142 | } | |
fdc15d38 | 8143 | } |
a3884572 | 8144 | |
d83525ca AS |
8145 | if ((is_tracing_prog_type(prog->type) || |
8146 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && | |
8147 | map_value_has_spin_lock(map)) { | |
8148 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
8149 | return -EINVAL; | |
8150 | } | |
8151 | ||
a3884572 | 8152 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 8153 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
8154 | verbose(env, "offload device mismatch between prog and map\n"); |
8155 | return -EINVAL; | |
8156 | } | |
8157 | ||
fdc15d38 AS |
8158 | return 0; |
8159 | } | |
8160 | ||
b741f163 RG |
8161 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
8162 | { | |
8163 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
8164 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
8165 | } | |
8166 | ||
0246e64d AS |
8167 | /* look for pseudo eBPF instructions that access map FDs and |
8168 | * replace them with actual map pointers | |
8169 | */ | |
58e2af8b | 8170 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
8171 | { |
8172 | struct bpf_insn *insn = env->prog->insnsi; | |
8173 | int insn_cnt = env->prog->len; | |
fdc15d38 | 8174 | int i, j, err; |
0246e64d | 8175 | |
f1f7714e | 8176 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
8177 | if (err) |
8178 | return err; | |
8179 | ||
0246e64d | 8180 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 8181 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 8182 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 8183 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
8184 | return -EINVAL; |
8185 | } | |
8186 | ||
d691f9e8 AS |
8187 | if (BPF_CLASS(insn->code) == BPF_STX && |
8188 | ((BPF_MODE(insn->code) != BPF_MEM && | |
8189 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 8190 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
8191 | return -EINVAL; |
8192 | } | |
8193 | ||
0246e64d | 8194 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 8195 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
8196 | struct bpf_map *map; |
8197 | struct fd f; | |
d8eca5bb | 8198 | u64 addr; |
0246e64d AS |
8199 | |
8200 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
8201 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
8202 | insn[1].off != 0) { | |
61bd5218 | 8203 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
8204 | return -EINVAL; |
8205 | } | |
8206 | ||
d8eca5bb | 8207 | if (insn[0].src_reg == 0) |
0246e64d AS |
8208 | /* valid generic load 64-bit imm */ |
8209 | goto next_insn; | |
8210 | ||
d8eca5bb DB |
8211 | /* In final convert_pseudo_ld_imm64() step, this is |
8212 | * converted into regular 64-bit imm load insn. | |
8213 | */ | |
8214 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
8215 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
8216 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
8217 | insn[1].imm != 0)) { | |
8218 | verbose(env, | |
8219 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
8220 | return -EINVAL; |
8221 | } | |
8222 | ||
20182390 | 8223 | f = fdget(insn[0].imm); |
c2101297 | 8224 | map = __bpf_map_get(f); |
0246e64d | 8225 | if (IS_ERR(map)) { |
61bd5218 | 8226 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 8227 | insn[0].imm); |
0246e64d AS |
8228 | return PTR_ERR(map); |
8229 | } | |
8230 | ||
61bd5218 | 8231 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
8232 | if (err) { |
8233 | fdput(f); | |
8234 | return err; | |
8235 | } | |
8236 | ||
d8eca5bb DB |
8237 | aux = &env->insn_aux_data[i]; |
8238 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
8239 | addr = (unsigned long)map; | |
8240 | } else { | |
8241 | u32 off = insn[1].imm; | |
8242 | ||
8243 | if (off >= BPF_MAX_VAR_OFF) { | |
8244 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
8245 | fdput(f); | |
8246 | return -EINVAL; | |
8247 | } | |
8248 | ||
8249 | if (!map->ops->map_direct_value_addr) { | |
8250 | verbose(env, "no direct value access support for this map type\n"); | |
8251 | fdput(f); | |
8252 | return -EINVAL; | |
8253 | } | |
8254 | ||
8255 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
8256 | if (err) { | |
8257 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
8258 | map->value_size, off); | |
8259 | fdput(f); | |
8260 | return err; | |
8261 | } | |
8262 | ||
8263 | aux->map_off = off; | |
8264 | addr += off; | |
8265 | } | |
8266 | ||
8267 | insn[0].imm = (u32)addr; | |
8268 | insn[1].imm = addr >> 32; | |
0246e64d AS |
8269 | |
8270 | /* check whether we recorded this map already */ | |
d8eca5bb | 8271 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 8272 | if (env->used_maps[j] == map) { |
d8eca5bb | 8273 | aux->map_index = j; |
0246e64d AS |
8274 | fdput(f); |
8275 | goto next_insn; | |
8276 | } | |
d8eca5bb | 8277 | } |
0246e64d AS |
8278 | |
8279 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
8280 | fdput(f); | |
8281 | return -E2BIG; | |
8282 | } | |
8283 | ||
0246e64d AS |
8284 | /* hold the map. If the program is rejected by verifier, |
8285 | * the map will be released by release_maps() or it | |
8286 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 8287 | * and all maps are released in free_used_maps() |
0246e64d | 8288 | */ |
1e0bd5a0 | 8289 | bpf_map_inc(map); |
d8eca5bb DB |
8290 | |
8291 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
8292 | env->used_maps[env->used_map_cnt++] = map; |
8293 | ||
b741f163 | 8294 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 8295 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 8296 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
8297 | fdput(f); |
8298 | return -EBUSY; | |
8299 | } | |
8300 | ||
0246e64d AS |
8301 | fdput(f); |
8302 | next_insn: | |
8303 | insn++; | |
8304 | i++; | |
5e581dad DB |
8305 | continue; |
8306 | } | |
8307 | ||
8308 | /* Basic sanity check before we invest more work here. */ | |
8309 | if (!bpf_opcode_in_insntable(insn->code)) { | |
8310 | verbose(env, "unknown opcode %02x\n", insn->code); | |
8311 | return -EINVAL; | |
0246e64d AS |
8312 | } |
8313 | } | |
8314 | ||
8315 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
8316 | * 'struct bpf_map *' into a register instead of user map_fd. | |
8317 | * These pointers will be used later by verifier to validate map access. | |
8318 | */ | |
8319 | return 0; | |
8320 | } | |
8321 | ||
8322 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 8323 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 8324 | { |
a2ea0746 DB |
8325 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
8326 | env->used_map_cnt); | |
0246e64d AS |
8327 | } |
8328 | ||
8329 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 8330 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
8331 | { |
8332 | struct bpf_insn *insn = env->prog->insnsi; | |
8333 | int insn_cnt = env->prog->len; | |
8334 | int i; | |
8335 | ||
8336 | for (i = 0; i < insn_cnt; i++, insn++) | |
8337 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
8338 | insn->src_reg = 0; | |
8339 | } | |
8340 | ||
8041902d AS |
8341 | /* single env->prog->insni[off] instruction was replaced with the range |
8342 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
8343 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
8344 | */ | |
b325fbca JW |
8345 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
8346 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d AS |
8347 | { |
8348 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
b325fbca JW |
8349 | struct bpf_insn *insn = new_prog->insnsi; |
8350 | u32 prog_len; | |
c131187d | 8351 | int i; |
8041902d | 8352 | |
b325fbca JW |
8353 | /* aux info at OFF always needs adjustment, no matter fast path |
8354 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
8355 | * original insn at old prog. | |
8356 | */ | |
8357 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
8358 | ||
8041902d AS |
8359 | if (cnt == 1) |
8360 | return 0; | |
b325fbca | 8361 | prog_len = new_prog->len; |
fad953ce KC |
8362 | new_data = vzalloc(array_size(prog_len, |
8363 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
8364 | if (!new_data) |
8365 | return -ENOMEM; | |
8366 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
8367 | memcpy(new_data + off + cnt - 1, old_data + off, | |
8368 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 8369 | for (i = off; i < off + cnt - 1; i++) { |
c131187d | 8370 | new_data[i].seen = true; |
b325fbca JW |
8371 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
8372 | } | |
8041902d AS |
8373 | env->insn_aux_data = new_data; |
8374 | vfree(old_data); | |
8375 | return 0; | |
8376 | } | |
8377 | ||
cc8b0b92 AS |
8378 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
8379 | { | |
8380 | int i; | |
8381 | ||
8382 | if (len == 1) | |
8383 | return; | |
4cb3d99c JW |
8384 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
8385 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 8386 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 8387 | continue; |
9c8105bd | 8388 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
8389 | } |
8390 | } | |
8391 | ||
8041902d AS |
8392 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
8393 | const struct bpf_insn *patch, u32 len) | |
8394 | { | |
8395 | struct bpf_prog *new_prog; | |
8396 | ||
8397 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
8398 | if (IS_ERR(new_prog)) { |
8399 | if (PTR_ERR(new_prog) == -ERANGE) | |
8400 | verbose(env, | |
8401 | "insn %d cannot be patched due to 16-bit range\n", | |
8402 | env->insn_aux_data[off].orig_idx); | |
8041902d | 8403 | return NULL; |
4f73379e | 8404 | } |
b325fbca | 8405 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
8041902d | 8406 | return NULL; |
cc8b0b92 | 8407 | adjust_subprog_starts(env, off, len); |
8041902d AS |
8408 | return new_prog; |
8409 | } | |
8410 | ||
52875a04 JK |
8411 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
8412 | u32 off, u32 cnt) | |
8413 | { | |
8414 | int i, j; | |
8415 | ||
8416 | /* find first prog starting at or after off (first to remove) */ | |
8417 | for (i = 0; i < env->subprog_cnt; i++) | |
8418 | if (env->subprog_info[i].start >= off) | |
8419 | break; | |
8420 | /* find first prog starting at or after off + cnt (first to stay) */ | |
8421 | for (j = i; j < env->subprog_cnt; j++) | |
8422 | if (env->subprog_info[j].start >= off + cnt) | |
8423 | break; | |
8424 | /* if j doesn't start exactly at off + cnt, we are just removing | |
8425 | * the front of previous prog | |
8426 | */ | |
8427 | if (env->subprog_info[j].start != off + cnt) | |
8428 | j--; | |
8429 | ||
8430 | if (j > i) { | |
8431 | struct bpf_prog_aux *aux = env->prog->aux; | |
8432 | int move; | |
8433 | ||
8434 | /* move fake 'exit' subprog as well */ | |
8435 | move = env->subprog_cnt + 1 - j; | |
8436 | ||
8437 | memmove(env->subprog_info + i, | |
8438 | env->subprog_info + j, | |
8439 | sizeof(*env->subprog_info) * move); | |
8440 | env->subprog_cnt -= j - i; | |
8441 | ||
8442 | /* remove func_info */ | |
8443 | if (aux->func_info) { | |
8444 | move = aux->func_info_cnt - j; | |
8445 | ||
8446 | memmove(aux->func_info + i, | |
8447 | aux->func_info + j, | |
8448 | sizeof(*aux->func_info) * move); | |
8449 | aux->func_info_cnt -= j - i; | |
8450 | /* func_info->insn_off is set after all code rewrites, | |
8451 | * in adjust_btf_func() - no need to adjust | |
8452 | */ | |
8453 | } | |
8454 | } else { | |
8455 | /* convert i from "first prog to remove" to "first to adjust" */ | |
8456 | if (env->subprog_info[i].start == off) | |
8457 | i++; | |
8458 | } | |
8459 | ||
8460 | /* update fake 'exit' subprog as well */ | |
8461 | for (; i <= env->subprog_cnt; i++) | |
8462 | env->subprog_info[i].start -= cnt; | |
8463 | ||
8464 | return 0; | |
8465 | } | |
8466 | ||
8467 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
8468 | u32 cnt) | |
8469 | { | |
8470 | struct bpf_prog *prog = env->prog; | |
8471 | u32 i, l_off, l_cnt, nr_linfo; | |
8472 | struct bpf_line_info *linfo; | |
8473 | ||
8474 | nr_linfo = prog->aux->nr_linfo; | |
8475 | if (!nr_linfo) | |
8476 | return 0; | |
8477 | ||
8478 | linfo = prog->aux->linfo; | |
8479 | ||
8480 | /* find first line info to remove, count lines to be removed */ | |
8481 | for (i = 0; i < nr_linfo; i++) | |
8482 | if (linfo[i].insn_off >= off) | |
8483 | break; | |
8484 | ||
8485 | l_off = i; | |
8486 | l_cnt = 0; | |
8487 | for (; i < nr_linfo; i++) | |
8488 | if (linfo[i].insn_off < off + cnt) | |
8489 | l_cnt++; | |
8490 | else | |
8491 | break; | |
8492 | ||
8493 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
8494 | * last removed linfo. prog is already modified, so prog->len == off | |
8495 | * means no live instructions after (tail of the program was removed). | |
8496 | */ | |
8497 | if (prog->len != off && l_cnt && | |
8498 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
8499 | l_cnt--; | |
8500 | linfo[--i].insn_off = off + cnt; | |
8501 | } | |
8502 | ||
8503 | /* remove the line info which refer to the removed instructions */ | |
8504 | if (l_cnt) { | |
8505 | memmove(linfo + l_off, linfo + i, | |
8506 | sizeof(*linfo) * (nr_linfo - i)); | |
8507 | ||
8508 | prog->aux->nr_linfo -= l_cnt; | |
8509 | nr_linfo = prog->aux->nr_linfo; | |
8510 | } | |
8511 | ||
8512 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
8513 | for (i = l_off; i < nr_linfo; i++) | |
8514 | linfo[i].insn_off -= cnt; | |
8515 | ||
8516 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
8517 | for (i = 0; i <= env->subprog_cnt; i++) | |
8518 | if (env->subprog_info[i].linfo_idx > l_off) { | |
8519 | /* program may have started in the removed region but | |
8520 | * may not be fully removed | |
8521 | */ | |
8522 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
8523 | env->subprog_info[i].linfo_idx -= l_cnt; | |
8524 | else | |
8525 | env->subprog_info[i].linfo_idx = l_off; | |
8526 | } | |
8527 | ||
8528 | return 0; | |
8529 | } | |
8530 | ||
8531 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
8532 | { | |
8533 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8534 | unsigned int orig_prog_len = env->prog->len; | |
8535 | int err; | |
8536 | ||
08ca90af JK |
8537 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
8538 | bpf_prog_offload_remove_insns(env, off, cnt); | |
8539 | ||
52875a04 JK |
8540 | err = bpf_remove_insns(env->prog, off, cnt); |
8541 | if (err) | |
8542 | return err; | |
8543 | ||
8544 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
8545 | if (err) | |
8546 | return err; | |
8547 | ||
8548 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
8549 | if (err) | |
8550 | return err; | |
8551 | ||
8552 | memmove(aux_data + off, aux_data + off + cnt, | |
8553 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
8554 | ||
8555 | return 0; | |
8556 | } | |
8557 | ||
2a5418a1 DB |
8558 | /* The verifier does more data flow analysis than llvm and will not |
8559 | * explore branches that are dead at run time. Malicious programs can | |
8560 | * have dead code too. Therefore replace all dead at-run-time code | |
8561 | * with 'ja -1'. | |
8562 | * | |
8563 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
8564 | * program and through another bug we would manage to jump there, then | |
8565 | * we'd execute beyond program memory otherwise. Returning exception | |
8566 | * code also wouldn't work since we can have subprogs where the dead | |
8567 | * code could be located. | |
c131187d AS |
8568 | */ |
8569 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
8570 | { | |
8571 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 8572 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
8573 | struct bpf_insn *insn = env->prog->insnsi; |
8574 | const int insn_cnt = env->prog->len; | |
8575 | int i; | |
8576 | ||
8577 | for (i = 0; i < insn_cnt; i++) { | |
8578 | if (aux_data[i].seen) | |
8579 | continue; | |
2a5418a1 | 8580 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
8581 | } |
8582 | } | |
8583 | ||
e2ae4ca2 JK |
8584 | static bool insn_is_cond_jump(u8 code) |
8585 | { | |
8586 | u8 op; | |
8587 | ||
092ed096 JW |
8588 | if (BPF_CLASS(code) == BPF_JMP32) |
8589 | return true; | |
8590 | ||
e2ae4ca2 JK |
8591 | if (BPF_CLASS(code) != BPF_JMP) |
8592 | return false; | |
8593 | ||
8594 | op = BPF_OP(code); | |
8595 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
8596 | } | |
8597 | ||
8598 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
8599 | { | |
8600 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8601 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
8602 | struct bpf_insn *insn = env->prog->insnsi; | |
8603 | const int insn_cnt = env->prog->len; | |
8604 | int i; | |
8605 | ||
8606 | for (i = 0; i < insn_cnt; i++, insn++) { | |
8607 | if (!insn_is_cond_jump(insn->code)) | |
8608 | continue; | |
8609 | ||
8610 | if (!aux_data[i + 1].seen) | |
8611 | ja.off = insn->off; | |
8612 | else if (!aux_data[i + 1 + insn->off].seen) | |
8613 | ja.off = 0; | |
8614 | else | |
8615 | continue; | |
8616 | ||
08ca90af JK |
8617 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
8618 | bpf_prog_offload_replace_insn(env, i, &ja); | |
8619 | ||
e2ae4ca2 JK |
8620 | memcpy(insn, &ja, sizeof(ja)); |
8621 | } | |
8622 | } | |
8623 | ||
52875a04 JK |
8624 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
8625 | { | |
8626 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8627 | int insn_cnt = env->prog->len; | |
8628 | int i, err; | |
8629 | ||
8630 | for (i = 0; i < insn_cnt; i++) { | |
8631 | int j; | |
8632 | ||
8633 | j = 0; | |
8634 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
8635 | j++; | |
8636 | if (!j) | |
8637 | continue; | |
8638 | ||
8639 | err = verifier_remove_insns(env, i, j); | |
8640 | if (err) | |
8641 | return err; | |
8642 | insn_cnt = env->prog->len; | |
8643 | } | |
8644 | ||
8645 | return 0; | |
8646 | } | |
8647 | ||
a1b14abc JK |
8648 | static int opt_remove_nops(struct bpf_verifier_env *env) |
8649 | { | |
8650 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
8651 | struct bpf_insn *insn = env->prog->insnsi; | |
8652 | int insn_cnt = env->prog->len; | |
8653 | int i, err; | |
8654 | ||
8655 | for (i = 0; i < insn_cnt; i++) { | |
8656 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
8657 | continue; | |
8658 | ||
8659 | err = verifier_remove_insns(env, i, 1); | |
8660 | if (err) | |
8661 | return err; | |
8662 | insn_cnt--; | |
8663 | i--; | |
8664 | } | |
8665 | ||
8666 | return 0; | |
8667 | } | |
8668 | ||
d6c2308c JW |
8669 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
8670 | const union bpf_attr *attr) | |
a4b1d3c1 | 8671 | { |
d6c2308c | 8672 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 8673 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 8674 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 8675 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 8676 | struct bpf_prog *new_prog; |
d6c2308c | 8677 | bool rnd_hi32; |
a4b1d3c1 | 8678 | |
d6c2308c | 8679 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 8680 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
8681 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
8682 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
8683 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
8684 | for (i = 0; i < len; i++) { |
8685 | int adj_idx = i + delta; | |
8686 | struct bpf_insn insn; | |
8687 | ||
d6c2308c JW |
8688 | insn = insns[adj_idx]; |
8689 | if (!aux[adj_idx].zext_dst) { | |
8690 | u8 code, class; | |
8691 | u32 imm_rnd; | |
8692 | ||
8693 | if (!rnd_hi32) | |
8694 | continue; | |
8695 | ||
8696 | code = insn.code; | |
8697 | class = BPF_CLASS(code); | |
8698 | if (insn_no_def(&insn)) | |
8699 | continue; | |
8700 | ||
8701 | /* NOTE: arg "reg" (the fourth one) is only used for | |
8702 | * BPF_STX which has been ruled out in above | |
8703 | * check, it is safe to pass NULL here. | |
8704 | */ | |
8705 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { | |
8706 | if (class == BPF_LD && | |
8707 | BPF_MODE(code) == BPF_IMM) | |
8708 | i++; | |
8709 | continue; | |
8710 | } | |
8711 | ||
8712 | /* ctx load could be transformed into wider load. */ | |
8713 | if (class == BPF_LDX && | |
8714 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
8715 | continue; | |
8716 | ||
8717 | imm_rnd = get_random_int(); | |
8718 | rnd_hi32_patch[0] = insn; | |
8719 | rnd_hi32_patch[1].imm = imm_rnd; | |
8720 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; | |
8721 | patch = rnd_hi32_patch; | |
8722 | patch_len = 4; | |
8723 | goto apply_patch_buffer; | |
8724 | } | |
8725 | ||
8726 | if (!bpf_jit_needs_zext()) | |
a4b1d3c1 JW |
8727 | continue; |
8728 | ||
a4b1d3c1 JW |
8729 | zext_patch[0] = insn; |
8730 | zext_patch[1].dst_reg = insn.dst_reg; | |
8731 | zext_patch[1].src_reg = insn.dst_reg; | |
d6c2308c JW |
8732 | patch = zext_patch; |
8733 | patch_len = 2; | |
8734 | apply_patch_buffer: | |
8735 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
8736 | if (!new_prog) |
8737 | return -ENOMEM; | |
8738 | env->prog = new_prog; | |
8739 | insns = new_prog->insnsi; | |
8740 | aux = env->insn_aux_data; | |
d6c2308c | 8741 | delta += patch_len - 1; |
a4b1d3c1 JW |
8742 | } |
8743 | ||
8744 | return 0; | |
8745 | } | |
8746 | ||
c64b7983 JS |
8747 | /* convert load instructions that access fields of a context type into a |
8748 | * sequence of instructions that access fields of the underlying structure: | |
8749 | * struct __sk_buff -> struct sk_buff | |
8750 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 8751 | */ |
58e2af8b | 8752 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 8753 | { |
00176a34 | 8754 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 8755 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 8756 | const int insn_cnt = env->prog->len; |
36bbef52 | 8757 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 8758 | u32 target_size, size_default, off; |
9bac3d6d | 8759 | struct bpf_prog *new_prog; |
d691f9e8 | 8760 | enum bpf_access_type type; |
f96da094 | 8761 | bool is_narrower_load; |
9bac3d6d | 8762 | |
b09928b9 DB |
8763 | if (ops->gen_prologue || env->seen_direct_write) { |
8764 | if (!ops->gen_prologue) { | |
8765 | verbose(env, "bpf verifier is misconfigured\n"); | |
8766 | return -EINVAL; | |
8767 | } | |
36bbef52 DB |
8768 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
8769 | env->prog); | |
8770 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 8771 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
8772 | return -EINVAL; |
8773 | } else if (cnt) { | |
8041902d | 8774 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
8775 | if (!new_prog) |
8776 | return -ENOMEM; | |
8041902d | 8777 | |
36bbef52 | 8778 | env->prog = new_prog; |
3df126f3 | 8779 | delta += cnt - 1; |
36bbef52 DB |
8780 | } |
8781 | } | |
8782 | ||
c64b7983 | 8783 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
8784 | return 0; |
8785 | ||
3df126f3 | 8786 | insn = env->prog->insnsi + delta; |
36bbef52 | 8787 | |
9bac3d6d | 8788 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
8789 | bpf_convert_ctx_access_t convert_ctx_access; |
8790 | ||
62c7989b DB |
8791 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
8792 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
8793 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 8794 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 8795 | type = BPF_READ; |
62c7989b DB |
8796 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
8797 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
8798 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 8799 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
8800 | type = BPF_WRITE; |
8801 | else | |
9bac3d6d AS |
8802 | continue; |
8803 | ||
af86ca4e AS |
8804 | if (type == BPF_WRITE && |
8805 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
8806 | struct bpf_insn patch[] = { | |
8807 | /* Sanitize suspicious stack slot with zero. | |
8808 | * There are no memory dependencies for this store, | |
8809 | * since it's only using frame pointer and immediate | |
8810 | * constant of zero | |
8811 | */ | |
8812 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
8813 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
8814 | 0), | |
8815 | /* the original STX instruction will immediately | |
8816 | * overwrite the same stack slot with appropriate value | |
8817 | */ | |
8818 | *insn, | |
8819 | }; | |
8820 | ||
8821 | cnt = ARRAY_SIZE(patch); | |
8822 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
8823 | if (!new_prog) | |
8824 | return -ENOMEM; | |
8825 | ||
8826 | delta += cnt - 1; | |
8827 | env->prog = new_prog; | |
8828 | insn = new_prog->insnsi + i + delta; | |
8829 | continue; | |
8830 | } | |
8831 | ||
c64b7983 JS |
8832 | switch (env->insn_aux_data[i + delta].ptr_type) { |
8833 | case PTR_TO_CTX: | |
8834 | if (!ops->convert_ctx_access) | |
8835 | continue; | |
8836 | convert_ctx_access = ops->convert_ctx_access; | |
8837 | break; | |
8838 | case PTR_TO_SOCKET: | |
46f8bc92 | 8839 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
8840 | convert_ctx_access = bpf_sock_convert_ctx_access; |
8841 | break; | |
655a51e5 MKL |
8842 | case PTR_TO_TCP_SOCK: |
8843 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
8844 | break; | |
fada7fdc JL |
8845 | case PTR_TO_XDP_SOCK: |
8846 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
8847 | break; | |
2a02759e | 8848 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
8849 | if (type == BPF_READ) { |
8850 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
8851 | BPF_SIZE((insn)->code); | |
8852 | env->prog->aux->num_exentries++; | |
8853 | } else if (env->prog->type != BPF_PROG_TYPE_STRUCT_OPS) { | |
2a02759e AS |
8854 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
8855 | return -EINVAL; | |
8856 | } | |
2a02759e | 8857 | continue; |
c64b7983 | 8858 | default: |
9bac3d6d | 8859 | continue; |
c64b7983 | 8860 | } |
9bac3d6d | 8861 | |
31fd8581 | 8862 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 8863 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
8864 | |
8865 | /* If the read access is a narrower load of the field, | |
8866 | * convert to a 4/8-byte load, to minimum program type specific | |
8867 | * convert_ctx_access changes. If conversion is successful, | |
8868 | * we will apply proper mask to the result. | |
8869 | */ | |
f96da094 | 8870 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
8871 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
8872 | off = insn->off; | |
31fd8581 | 8873 | if (is_narrower_load) { |
f96da094 DB |
8874 | u8 size_code; |
8875 | ||
8876 | if (type == BPF_WRITE) { | |
61bd5218 | 8877 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
8878 | return -EINVAL; |
8879 | } | |
31fd8581 | 8880 | |
f96da094 | 8881 | size_code = BPF_H; |
31fd8581 YS |
8882 | if (ctx_field_size == 4) |
8883 | size_code = BPF_W; | |
8884 | else if (ctx_field_size == 8) | |
8885 | size_code = BPF_DW; | |
f96da094 | 8886 | |
bc23105c | 8887 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
8888 | insn->code = BPF_LDX | BPF_MEM | size_code; |
8889 | } | |
f96da094 DB |
8890 | |
8891 | target_size = 0; | |
c64b7983 JS |
8892 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
8893 | &target_size); | |
f96da094 DB |
8894 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
8895 | (ctx_field_size && !target_size)) { | |
61bd5218 | 8896 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
8897 | return -EINVAL; |
8898 | } | |
f96da094 DB |
8899 | |
8900 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
8901 | u8 shift = bpf_ctx_narrow_access_offset( |
8902 | off, size, size_default) * 8; | |
46f53a65 AI |
8903 | if (ctx_field_size <= 4) { |
8904 | if (shift) | |
8905 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
8906 | insn->dst_reg, | |
8907 | shift); | |
31fd8581 | 8908 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 8909 | (1 << size * 8) - 1); |
46f53a65 AI |
8910 | } else { |
8911 | if (shift) | |
8912 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
8913 | insn->dst_reg, | |
8914 | shift); | |
31fd8581 | 8915 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 8916 | (1ULL << size * 8) - 1); |
46f53a65 | 8917 | } |
31fd8581 | 8918 | } |
9bac3d6d | 8919 | |
8041902d | 8920 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
8921 | if (!new_prog) |
8922 | return -ENOMEM; | |
8923 | ||
3df126f3 | 8924 | delta += cnt - 1; |
9bac3d6d AS |
8925 | |
8926 | /* keep walking new program and skip insns we just inserted */ | |
8927 | env->prog = new_prog; | |
3df126f3 | 8928 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
8929 | } |
8930 | ||
8931 | return 0; | |
8932 | } | |
8933 | ||
1c2a088a AS |
8934 | static int jit_subprogs(struct bpf_verifier_env *env) |
8935 | { | |
8936 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
8937 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
7105e828 | 8938 | struct bpf_insn *insn; |
1c2a088a | 8939 | void *old_bpf_func; |
c454a46b | 8940 | int err; |
1c2a088a | 8941 | |
f910cefa | 8942 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
8943 | return 0; |
8944 | ||
7105e828 | 8945 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
8946 | if (insn->code != (BPF_JMP | BPF_CALL) || |
8947 | insn->src_reg != BPF_PSEUDO_CALL) | |
8948 | continue; | |
c7a89784 DB |
8949 | /* Upon error here we cannot fall back to interpreter but |
8950 | * need a hard reject of the program. Thus -EFAULT is | |
8951 | * propagated in any case. | |
8952 | */ | |
1c2a088a AS |
8953 | subprog = find_subprog(env, i + insn->imm + 1); |
8954 | if (subprog < 0) { | |
8955 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
8956 | i + insn->imm + 1); | |
8957 | return -EFAULT; | |
8958 | } | |
8959 | /* temporarily remember subprog id inside insn instead of | |
8960 | * aux_data, since next loop will split up all insns into funcs | |
8961 | */ | |
f910cefa | 8962 | insn->off = subprog; |
1c2a088a AS |
8963 | /* remember original imm in case JIT fails and fallback |
8964 | * to interpreter will be needed | |
8965 | */ | |
8966 | env->insn_aux_data[i].call_imm = insn->imm; | |
8967 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
8968 | insn->imm = 1; | |
8969 | } | |
8970 | ||
c454a46b MKL |
8971 | err = bpf_prog_alloc_jited_linfo(prog); |
8972 | if (err) | |
8973 | goto out_undo_insn; | |
8974 | ||
8975 | err = -ENOMEM; | |
6396bb22 | 8976 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 8977 | if (!func) |
c7a89784 | 8978 | goto out_undo_insn; |
1c2a088a | 8979 | |
f910cefa | 8980 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 8981 | subprog_start = subprog_end; |
4cb3d99c | 8982 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
8983 | |
8984 | len = subprog_end - subprog_start; | |
492ecee8 AS |
8985 | /* BPF_PROG_RUN doesn't call subprogs directly, |
8986 | * hence main prog stats include the runtime of subprogs. | |
8987 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
8988 | * func[i]->aux->stats will never be accessed and stays NULL | |
8989 | */ | |
8990 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
8991 | if (!func[i]) |
8992 | goto out_free; | |
8993 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
8994 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 8995 | func[i]->type = prog->type; |
1c2a088a | 8996 | func[i]->len = len; |
4f74d809 DB |
8997 | if (bpf_prog_calc_tag(func[i])) |
8998 | goto out_free; | |
1c2a088a | 8999 | func[i]->is_func = 1; |
ba64e7d8 YS |
9000 | func[i]->aux->func_idx = i; |
9001 | /* the btf and func_info will be freed only at prog->aux */ | |
9002 | func[i]->aux->btf = prog->aux->btf; | |
9003 | func[i]->aux->func_info = prog->aux->func_info; | |
9004 | ||
1c2a088a AS |
9005 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
9006 | * Long term would need debug info to populate names | |
9007 | */ | |
9008 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 9009 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 9010 | func[i]->jit_requested = 1; |
c454a46b MKL |
9011 | func[i]->aux->linfo = prog->aux->linfo; |
9012 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
9013 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
9014 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
1c2a088a AS |
9015 | func[i] = bpf_int_jit_compile(func[i]); |
9016 | if (!func[i]->jited) { | |
9017 | err = -ENOTSUPP; | |
9018 | goto out_free; | |
9019 | } | |
9020 | cond_resched(); | |
9021 | } | |
9022 | /* at this point all bpf functions were successfully JITed | |
9023 | * now populate all bpf_calls with correct addresses and | |
9024 | * run last pass of JIT | |
9025 | */ | |
f910cefa | 9026 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
9027 | insn = func[i]->insnsi; |
9028 | for (j = 0; j < func[i]->len; j++, insn++) { | |
9029 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
9030 | insn->src_reg != BPF_PSEUDO_CALL) | |
9031 | continue; | |
9032 | subprog = insn->off; | |
0d306c31 PB |
9033 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
9034 | __bpf_call_base; | |
1c2a088a | 9035 | } |
2162fed4 SD |
9036 | |
9037 | /* we use the aux data to keep a list of the start addresses | |
9038 | * of the JITed images for each function in the program | |
9039 | * | |
9040 | * for some architectures, such as powerpc64, the imm field | |
9041 | * might not be large enough to hold the offset of the start | |
9042 | * address of the callee's JITed image from __bpf_call_base | |
9043 | * | |
9044 | * in such cases, we can lookup the start address of a callee | |
9045 | * by using its subprog id, available from the off field of | |
9046 | * the call instruction, as an index for this list | |
9047 | */ | |
9048 | func[i]->aux->func = func; | |
9049 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 9050 | } |
f910cefa | 9051 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
9052 | old_bpf_func = func[i]->bpf_func; |
9053 | tmp = bpf_int_jit_compile(func[i]); | |
9054 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
9055 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 9056 | err = -ENOTSUPP; |
1c2a088a AS |
9057 | goto out_free; |
9058 | } | |
9059 | cond_resched(); | |
9060 | } | |
9061 | ||
9062 | /* finally lock prog and jit images for all functions and | |
9063 | * populate kallsysm | |
9064 | */ | |
f910cefa | 9065 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
9066 | bpf_prog_lock_ro(func[i]); |
9067 | bpf_prog_kallsyms_add(func[i]); | |
9068 | } | |
7105e828 DB |
9069 | |
9070 | /* Last step: make now unused interpreter insns from main | |
9071 | * prog consistent for later dump requests, so they can | |
9072 | * later look the same as if they were interpreted only. | |
9073 | */ | |
9074 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
9075 | if (insn->code != (BPF_JMP | BPF_CALL) || |
9076 | insn->src_reg != BPF_PSEUDO_CALL) | |
9077 | continue; | |
9078 | insn->off = env->insn_aux_data[i].call_imm; | |
9079 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 9080 | insn->imm = subprog; |
7105e828 DB |
9081 | } |
9082 | ||
1c2a088a AS |
9083 | prog->jited = 1; |
9084 | prog->bpf_func = func[0]->bpf_func; | |
9085 | prog->aux->func = func; | |
f910cefa | 9086 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 9087 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
9088 | return 0; |
9089 | out_free: | |
f910cefa | 9090 | for (i = 0; i < env->subprog_cnt; i++) |
1c2a088a AS |
9091 | if (func[i]) |
9092 | bpf_jit_free(func[i]); | |
9093 | kfree(func); | |
c7a89784 | 9094 | out_undo_insn: |
1c2a088a AS |
9095 | /* cleanup main prog to be interpreted */ |
9096 | prog->jit_requested = 0; | |
9097 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
9098 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
9099 | insn->src_reg != BPF_PSEUDO_CALL) | |
9100 | continue; | |
9101 | insn->off = 0; | |
9102 | insn->imm = env->insn_aux_data[i].call_imm; | |
9103 | } | |
c454a46b | 9104 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
9105 | return err; |
9106 | } | |
9107 | ||
1ea47e01 AS |
9108 | static int fixup_call_args(struct bpf_verifier_env *env) |
9109 | { | |
19d28fbd | 9110 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
9111 | struct bpf_prog *prog = env->prog; |
9112 | struct bpf_insn *insn = prog->insnsi; | |
9113 | int i, depth; | |
19d28fbd | 9114 | #endif |
e4052d06 | 9115 | int err = 0; |
1ea47e01 | 9116 | |
e4052d06 QM |
9117 | if (env->prog->jit_requested && |
9118 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
9119 | err = jit_subprogs(env); |
9120 | if (err == 0) | |
1c2a088a | 9121 | return 0; |
c7a89784 DB |
9122 | if (err == -EFAULT) |
9123 | return err; | |
19d28fbd DM |
9124 | } |
9125 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 AS |
9126 | for (i = 0; i < prog->len; i++, insn++) { |
9127 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
9128 | insn->src_reg != BPF_PSEUDO_CALL) | |
9129 | continue; | |
9130 | depth = get_callee_stack_depth(env, insn, i); | |
9131 | if (depth < 0) | |
9132 | return depth; | |
9133 | bpf_patch_call_args(insn, depth); | |
9134 | } | |
19d28fbd DM |
9135 | err = 0; |
9136 | #endif | |
9137 | return err; | |
1ea47e01 AS |
9138 | } |
9139 | ||
79741b3b | 9140 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 9141 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
9142 | * |
9143 | * this function is called after eBPF program passed verification | |
9144 | */ | |
79741b3b | 9145 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 9146 | { |
79741b3b | 9147 | struct bpf_prog *prog = env->prog; |
d2e4c1e6 | 9148 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
79741b3b | 9149 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 9150 | const struct bpf_func_proto *fn; |
79741b3b | 9151 | const int insn_cnt = prog->len; |
09772d92 | 9152 | const struct bpf_map_ops *ops; |
c93552c4 | 9153 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
9154 | struct bpf_insn insn_buf[16]; |
9155 | struct bpf_prog *new_prog; | |
9156 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 9157 | int i, ret, cnt, delta = 0; |
e245c5c6 | 9158 | |
79741b3b | 9159 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
9160 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
9161 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
9162 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 9163 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
9164 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
9165 | struct bpf_insn mask_and_div[] = { | |
9166 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
9167 | /* Rx div 0 -> 0 */ | |
9168 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
9169 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
9170 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
9171 | *insn, | |
9172 | }; | |
9173 | struct bpf_insn mask_and_mod[] = { | |
9174 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
9175 | /* Rx mod 0 -> Rx */ | |
9176 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
9177 | *insn, | |
9178 | }; | |
9179 | struct bpf_insn *patchlet; | |
9180 | ||
9181 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
9182 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
9183 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
9184 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
9185 | } else { | |
9186 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
9187 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
9188 | } | |
9189 | ||
9190 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
9191 | if (!new_prog) |
9192 | return -ENOMEM; | |
9193 | ||
9194 | delta += cnt - 1; | |
9195 | env->prog = prog = new_prog; | |
9196 | insn = new_prog->insnsi + i + delta; | |
9197 | continue; | |
9198 | } | |
9199 | ||
e0cea7ce DB |
9200 | if (BPF_CLASS(insn->code) == BPF_LD && |
9201 | (BPF_MODE(insn->code) == BPF_ABS || | |
9202 | BPF_MODE(insn->code) == BPF_IND)) { | |
9203 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
9204 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
9205 | verbose(env, "bpf verifier is misconfigured\n"); | |
9206 | return -EINVAL; | |
9207 | } | |
9208 | ||
9209 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
9210 | if (!new_prog) | |
9211 | return -ENOMEM; | |
9212 | ||
9213 | delta += cnt - 1; | |
9214 | env->prog = prog = new_prog; | |
9215 | insn = new_prog->insnsi + i + delta; | |
9216 | continue; | |
9217 | } | |
9218 | ||
979d63d5 DB |
9219 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
9220 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
9221 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
9222 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
9223 | struct bpf_insn insn_buf[16]; | |
9224 | struct bpf_insn *patch = &insn_buf[0]; | |
9225 | bool issrc, isneg; | |
9226 | u32 off_reg; | |
9227 | ||
9228 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
9229 | if (!aux->alu_state || |
9230 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
9231 | continue; |
9232 | ||
9233 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
9234 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
9235 | BPF_ALU_SANITIZE_SRC; | |
9236 | ||
9237 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
9238 | if (isneg) | |
9239 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
9240 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
9241 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
9242 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
9243 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
9244 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
9245 | if (issrc) { | |
9246 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
9247 | off_reg); | |
9248 | insn->src_reg = BPF_REG_AX; | |
9249 | } else { | |
9250 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
9251 | BPF_REG_AX); | |
9252 | } | |
9253 | if (isneg) | |
9254 | insn->code = insn->code == code_add ? | |
9255 | code_sub : code_add; | |
9256 | *patch++ = *insn; | |
9257 | if (issrc && isneg) | |
9258 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
9259 | cnt = patch - insn_buf; | |
9260 | ||
9261 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
9262 | if (!new_prog) | |
9263 | return -ENOMEM; | |
9264 | ||
9265 | delta += cnt - 1; | |
9266 | env->prog = prog = new_prog; | |
9267 | insn = new_prog->insnsi + i + delta; | |
9268 | continue; | |
9269 | } | |
9270 | ||
79741b3b AS |
9271 | if (insn->code != (BPF_JMP | BPF_CALL)) |
9272 | continue; | |
cc8b0b92 AS |
9273 | if (insn->src_reg == BPF_PSEUDO_CALL) |
9274 | continue; | |
e245c5c6 | 9275 | |
79741b3b AS |
9276 | if (insn->imm == BPF_FUNC_get_route_realm) |
9277 | prog->dst_needed = 1; | |
9278 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
9279 | bpf_user_rnd_init_once(); | |
9802d865 JB |
9280 | if (insn->imm == BPF_FUNC_override_return) |
9281 | prog->kprobe_override = 1; | |
79741b3b | 9282 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
9283 | /* If we tail call into other programs, we |
9284 | * cannot make any assumptions since they can | |
9285 | * be replaced dynamically during runtime in | |
9286 | * the program array. | |
9287 | */ | |
9288 | prog->cb_access = 1; | |
80a58d02 | 9289 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
e647815a | 9290 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
7b9f6da1 | 9291 | |
79741b3b AS |
9292 | /* mark bpf_tail_call as different opcode to avoid |
9293 | * conditional branch in the interpeter for every normal | |
9294 | * call and to prevent accidental JITing by JIT compiler | |
9295 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 9296 | */ |
79741b3b | 9297 | insn->imm = 0; |
71189fa9 | 9298 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 9299 | |
c93552c4 | 9300 | aux = &env->insn_aux_data[i + delta]; |
cc52d914 DB |
9301 | if (env->allow_ptr_leaks && !expect_blinding && |
9302 | prog->jit_requested && | |
d2e4c1e6 DB |
9303 | !bpf_map_key_poisoned(aux) && |
9304 | !bpf_map_ptr_poisoned(aux) && | |
9305 | !bpf_map_ptr_unpriv(aux)) { | |
9306 | struct bpf_jit_poke_descriptor desc = { | |
9307 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
9308 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
9309 | .tail_call.key = bpf_map_key_immediate(aux), | |
9310 | }; | |
9311 | ||
9312 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
9313 | if (ret < 0) { | |
9314 | verbose(env, "adding tail call poke descriptor failed\n"); | |
9315 | return ret; | |
9316 | } | |
9317 | ||
9318 | insn->imm = ret + 1; | |
9319 | continue; | |
9320 | } | |
9321 | ||
c93552c4 DB |
9322 | if (!bpf_map_ptr_unpriv(aux)) |
9323 | continue; | |
9324 | ||
b2157399 AS |
9325 | /* instead of changing every JIT dealing with tail_call |
9326 | * emit two extra insns: | |
9327 | * if (index >= max_entries) goto out; | |
9328 | * index &= array->index_mask; | |
9329 | * to avoid out-of-bounds cpu speculation | |
9330 | */ | |
c93552c4 | 9331 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 9332 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
9333 | return -EINVAL; |
9334 | } | |
c93552c4 | 9335 | |
d2e4c1e6 | 9336 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
9337 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
9338 | map_ptr->max_entries, 2); | |
9339 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
9340 | container_of(map_ptr, | |
9341 | struct bpf_array, | |
9342 | map)->index_mask); | |
9343 | insn_buf[2] = *insn; | |
9344 | cnt = 3; | |
9345 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
9346 | if (!new_prog) | |
9347 | return -ENOMEM; | |
9348 | ||
9349 | delta += cnt - 1; | |
9350 | env->prog = prog = new_prog; | |
9351 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
9352 | continue; |
9353 | } | |
e245c5c6 | 9354 | |
89c63074 | 9355 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
9356 | * and other inlining handlers are currently limited to 64 bit |
9357 | * only. | |
89c63074 | 9358 | */ |
60b58afc | 9359 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
9360 | (insn->imm == BPF_FUNC_map_lookup_elem || |
9361 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
9362 | insn->imm == BPF_FUNC_map_delete_elem || |
9363 | insn->imm == BPF_FUNC_map_push_elem || | |
9364 | insn->imm == BPF_FUNC_map_pop_elem || | |
9365 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
9366 | aux = &env->insn_aux_data[i + delta]; |
9367 | if (bpf_map_ptr_poisoned(aux)) | |
9368 | goto patch_call_imm; | |
9369 | ||
d2e4c1e6 | 9370 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
9371 | ops = map_ptr->ops; |
9372 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
9373 | ops->map_gen_lookup) { | |
9374 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
9375 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
9376 | verbose(env, "bpf verifier is misconfigured\n"); | |
9377 | return -EINVAL; | |
9378 | } | |
81ed18ab | 9379 | |
09772d92 DB |
9380 | new_prog = bpf_patch_insn_data(env, i + delta, |
9381 | insn_buf, cnt); | |
9382 | if (!new_prog) | |
9383 | return -ENOMEM; | |
81ed18ab | 9384 | |
09772d92 DB |
9385 | delta += cnt - 1; |
9386 | env->prog = prog = new_prog; | |
9387 | insn = new_prog->insnsi + i + delta; | |
9388 | continue; | |
9389 | } | |
81ed18ab | 9390 | |
09772d92 DB |
9391 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
9392 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
9393 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
9394 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
9395 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
9396 | (int (*)(struct bpf_map *map, void *key, void *value, | |
9397 | u64 flags))NULL)); | |
84430d42 DB |
9398 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
9399 | (int (*)(struct bpf_map *map, void *value, | |
9400 | u64 flags))NULL)); | |
9401 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
9402 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
9403 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
9404 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
9405 | ||
09772d92 DB |
9406 | switch (insn->imm) { |
9407 | case BPF_FUNC_map_lookup_elem: | |
9408 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
9409 | __bpf_call_base; | |
9410 | continue; | |
9411 | case BPF_FUNC_map_update_elem: | |
9412 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
9413 | __bpf_call_base; | |
9414 | continue; | |
9415 | case BPF_FUNC_map_delete_elem: | |
9416 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
9417 | __bpf_call_base; | |
9418 | continue; | |
84430d42 DB |
9419 | case BPF_FUNC_map_push_elem: |
9420 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
9421 | __bpf_call_base; | |
9422 | continue; | |
9423 | case BPF_FUNC_map_pop_elem: | |
9424 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
9425 | __bpf_call_base; | |
9426 | continue; | |
9427 | case BPF_FUNC_map_peek_elem: | |
9428 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
9429 | __bpf_call_base; | |
9430 | continue; | |
09772d92 | 9431 | } |
81ed18ab | 9432 | |
09772d92 | 9433 | goto patch_call_imm; |
81ed18ab AS |
9434 | } |
9435 | ||
9436 | patch_call_imm: | |
5e43f899 | 9437 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
9438 | /* all functions that have prototype and verifier allowed |
9439 | * programs to call them, must be real in-kernel functions | |
9440 | */ | |
9441 | if (!fn->func) { | |
61bd5218 JK |
9442 | verbose(env, |
9443 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
9444 | func_id_name(insn->imm), insn->imm); |
9445 | return -EFAULT; | |
e245c5c6 | 9446 | } |
79741b3b | 9447 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 9448 | } |
e245c5c6 | 9449 | |
d2e4c1e6 DB |
9450 | /* Since poke tab is now finalized, publish aux to tracker. */ |
9451 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
9452 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
9453 | if (!map_ptr->ops->map_poke_track || | |
9454 | !map_ptr->ops->map_poke_untrack || | |
9455 | !map_ptr->ops->map_poke_run) { | |
9456 | verbose(env, "bpf verifier is misconfigured\n"); | |
9457 | return -EINVAL; | |
9458 | } | |
9459 | ||
9460 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
9461 | if (ret < 0) { | |
9462 | verbose(env, "tracking tail call prog failed\n"); | |
9463 | return ret; | |
9464 | } | |
9465 | } | |
9466 | ||
79741b3b AS |
9467 | return 0; |
9468 | } | |
e245c5c6 | 9469 | |
58e2af8b | 9470 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 9471 | { |
58e2af8b | 9472 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
9473 | int i; |
9474 | ||
9f4686c4 AS |
9475 | sl = env->free_list; |
9476 | while (sl) { | |
9477 | sln = sl->next; | |
9478 | free_verifier_state(&sl->state, false); | |
9479 | kfree(sl); | |
9480 | sl = sln; | |
9481 | } | |
9482 | ||
f1bca824 AS |
9483 | if (!env->explored_states) |
9484 | return; | |
9485 | ||
dc2a4ebc | 9486 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
9487 | sl = env->explored_states[i]; |
9488 | ||
a8f500af AS |
9489 | while (sl) { |
9490 | sln = sl->next; | |
9491 | free_verifier_state(&sl->state, false); | |
9492 | kfree(sl); | |
9493 | sl = sln; | |
9494 | } | |
f1bca824 AS |
9495 | } |
9496 | ||
71dde681 | 9497 | kvfree(env->explored_states); |
f1bca824 AS |
9498 | } |
9499 | ||
06ee7115 AS |
9500 | static void print_verification_stats(struct bpf_verifier_env *env) |
9501 | { | |
9502 | int i; | |
9503 | ||
9504 | if (env->log.level & BPF_LOG_STATS) { | |
9505 | verbose(env, "verification time %lld usec\n", | |
9506 | div_u64(env->verification_time, 1000)); | |
9507 | verbose(env, "stack depth "); | |
9508 | for (i = 0; i < env->subprog_cnt; i++) { | |
9509 | u32 depth = env->subprog_info[i].stack_depth; | |
9510 | ||
9511 | verbose(env, "%d", depth); | |
9512 | if (i + 1 < env->subprog_cnt) | |
9513 | verbose(env, "+"); | |
9514 | } | |
9515 | verbose(env, "\n"); | |
9516 | } | |
9517 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
9518 | "total_states %d peak_states %d mark_read %d\n", | |
9519 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
9520 | env->max_states_per_insn, env->total_states, | |
9521 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
9522 | } |
9523 | ||
27ae7997 MKL |
9524 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
9525 | { | |
9526 | const struct btf_type *t, *func_proto; | |
9527 | const struct bpf_struct_ops *st_ops; | |
9528 | const struct btf_member *member; | |
9529 | struct bpf_prog *prog = env->prog; | |
9530 | u32 btf_id, member_idx; | |
9531 | const char *mname; | |
9532 | ||
9533 | btf_id = prog->aux->attach_btf_id; | |
9534 | st_ops = bpf_struct_ops_find(btf_id); | |
9535 | if (!st_ops) { | |
9536 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
9537 | btf_id); | |
9538 | return -ENOTSUPP; | |
9539 | } | |
9540 | ||
9541 | t = st_ops->type; | |
9542 | member_idx = prog->expected_attach_type; | |
9543 | if (member_idx >= btf_type_vlen(t)) { | |
9544 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
9545 | member_idx, st_ops->name); | |
9546 | return -EINVAL; | |
9547 | } | |
9548 | ||
9549 | member = &btf_type_member(t)[member_idx]; | |
9550 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
9551 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
9552 | NULL); | |
9553 | if (!func_proto) { | |
9554 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
9555 | mname, member_idx, st_ops->name); | |
9556 | return -EINVAL; | |
9557 | } | |
9558 | ||
9559 | if (st_ops->check_member) { | |
9560 | int err = st_ops->check_member(t, member); | |
9561 | ||
9562 | if (err) { | |
9563 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
9564 | mname, st_ops->name); | |
9565 | return err; | |
9566 | } | |
9567 | } | |
9568 | ||
9569 | prog->aux->attach_func_proto = func_proto; | |
9570 | prog->aux->attach_func_name = mname; | |
9571 | env->ops = st_ops->verifier_ops; | |
9572 | ||
9573 | return 0; | |
9574 | } | |
9575 | ||
38207291 MKL |
9576 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
9577 | { | |
9578 | struct bpf_prog *prog = env->prog; | |
5b92a28a | 9579 | struct bpf_prog *tgt_prog = prog->aux->linked_prog; |
38207291 | 9580 | u32 btf_id = prog->aux->attach_btf_id; |
f1b9509c | 9581 | const char prefix[] = "btf_trace_"; |
5b92a28a | 9582 | int ret = 0, subprog = -1, i; |
fec56f58 | 9583 | struct bpf_trampoline *tr; |
38207291 | 9584 | const struct btf_type *t; |
5b92a28a | 9585 | bool conservative = true; |
38207291 | 9586 | const char *tname; |
5b92a28a | 9587 | struct btf *btf; |
fec56f58 | 9588 | long addr; |
5b92a28a | 9589 | u64 key; |
38207291 | 9590 | |
27ae7997 MKL |
9591 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
9592 | return check_struct_ops_btf_id(env); | |
9593 | ||
f1b9509c AS |
9594 | if (prog->type != BPF_PROG_TYPE_TRACING) |
9595 | return 0; | |
38207291 | 9596 | |
f1b9509c AS |
9597 | if (!btf_id) { |
9598 | verbose(env, "Tracing programs must provide btf_id\n"); | |
9599 | return -EINVAL; | |
9600 | } | |
5b92a28a AS |
9601 | btf = bpf_prog_get_target_btf(prog); |
9602 | if (!btf) { | |
9603 | verbose(env, | |
9604 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); | |
9605 | return -EINVAL; | |
9606 | } | |
9607 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c AS |
9608 | if (!t) { |
9609 | verbose(env, "attach_btf_id %u is invalid\n", btf_id); | |
9610 | return -EINVAL; | |
9611 | } | |
5b92a28a | 9612 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c AS |
9613 | if (!tname) { |
9614 | verbose(env, "attach_btf_id %u doesn't have a name\n", btf_id); | |
9615 | return -EINVAL; | |
9616 | } | |
5b92a28a AS |
9617 | if (tgt_prog) { |
9618 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
9619 | ||
9620 | for (i = 0; i < aux->func_info_cnt; i++) | |
9621 | if (aux->func_info[i].type_id == btf_id) { | |
9622 | subprog = i; | |
9623 | break; | |
9624 | } | |
9625 | if (subprog == -1) { | |
9626 | verbose(env, "Subprog %s doesn't exist\n", tname); | |
9627 | return -EINVAL; | |
9628 | } | |
9629 | conservative = aux->func_info_aux[subprog].unreliable; | |
9630 | key = ((u64)aux->id) << 32 | btf_id; | |
9631 | } else { | |
9632 | key = btf_id; | |
9633 | } | |
f1b9509c AS |
9634 | |
9635 | switch (prog->expected_attach_type) { | |
9636 | case BPF_TRACE_RAW_TP: | |
5b92a28a AS |
9637 | if (tgt_prog) { |
9638 | verbose(env, | |
9639 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); | |
9640 | return -EINVAL; | |
9641 | } | |
38207291 MKL |
9642 | if (!btf_type_is_typedef(t)) { |
9643 | verbose(env, "attach_btf_id %u is not a typedef\n", | |
9644 | btf_id); | |
9645 | return -EINVAL; | |
9646 | } | |
f1b9509c | 9647 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
38207291 MKL |
9648 | verbose(env, "attach_btf_id %u points to wrong type name %s\n", |
9649 | btf_id, tname); | |
9650 | return -EINVAL; | |
9651 | } | |
9652 | tname += sizeof(prefix) - 1; | |
5b92a28a | 9653 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
9654 | if (!btf_type_is_ptr(t)) |
9655 | /* should never happen in valid vmlinux build */ | |
9656 | return -EINVAL; | |
5b92a28a | 9657 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
9658 | if (!btf_type_is_func_proto(t)) |
9659 | /* should never happen in valid vmlinux build */ | |
9660 | return -EINVAL; | |
9661 | ||
9662 | /* remember two read only pointers that are valid for | |
9663 | * the life time of the kernel | |
9664 | */ | |
9665 | prog->aux->attach_func_name = tname; | |
9666 | prog->aux->attach_func_proto = t; | |
9667 | prog->aux->attach_btf_trace = true; | |
f1b9509c | 9668 | return 0; |
fec56f58 AS |
9669 | case BPF_TRACE_FENTRY: |
9670 | case BPF_TRACE_FEXIT: | |
9671 | if (!btf_type_is_func(t)) { | |
9672 | verbose(env, "attach_btf_id %u is not a function\n", | |
9673 | btf_id); | |
9674 | return -EINVAL; | |
9675 | } | |
5b92a28a | 9676 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
9677 | if (!btf_type_is_func_proto(t)) |
9678 | return -EINVAL; | |
5b92a28a | 9679 | tr = bpf_trampoline_lookup(key); |
fec56f58 AS |
9680 | if (!tr) |
9681 | return -ENOMEM; | |
9682 | prog->aux->attach_func_name = tname; | |
5b92a28a | 9683 | /* t is either vmlinux type or another program's type */ |
fec56f58 AS |
9684 | prog->aux->attach_func_proto = t; |
9685 | mutex_lock(&tr->mutex); | |
9686 | if (tr->func.addr) { | |
9687 | prog->aux->trampoline = tr; | |
9688 | goto out; | |
9689 | } | |
5b92a28a AS |
9690 | if (tgt_prog && conservative) { |
9691 | prog->aux->attach_func_proto = NULL; | |
9692 | t = NULL; | |
9693 | } | |
9694 | ret = btf_distill_func_proto(&env->log, btf, t, | |
fec56f58 AS |
9695 | tname, &tr->func.model); |
9696 | if (ret < 0) | |
9697 | goto out; | |
5b92a28a AS |
9698 | if (tgt_prog) { |
9699 | if (!tgt_prog->jited) { | |
9700 | /* for now */ | |
9701 | verbose(env, "Can trace only JITed BPF progs\n"); | |
9702 | ret = -EINVAL; | |
9703 | goto out; | |
9704 | } | |
9705 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING) { | |
9706 | /* prevent cycles */ | |
9707 | verbose(env, "Cannot recursively attach\n"); | |
9708 | ret = -EINVAL; | |
9709 | goto out; | |
9710 | } | |
e9eeec58 YS |
9711 | if (subprog == 0) |
9712 | addr = (long) tgt_prog->bpf_func; | |
9713 | else | |
9714 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
9715 | } else { |
9716 | addr = kallsyms_lookup_name(tname); | |
9717 | if (!addr) { | |
9718 | verbose(env, | |
9719 | "The address of function %s cannot be found\n", | |
9720 | tname); | |
9721 | ret = -ENOENT; | |
9722 | goto out; | |
9723 | } | |
fec56f58 AS |
9724 | } |
9725 | tr->func.addr = (void *)addr; | |
9726 | prog->aux->trampoline = tr; | |
9727 | out: | |
9728 | mutex_unlock(&tr->mutex); | |
9729 | if (ret) | |
9730 | bpf_trampoline_put(tr); | |
9731 | return ret; | |
f1b9509c AS |
9732 | default: |
9733 | return -EINVAL; | |
38207291 | 9734 | } |
38207291 MKL |
9735 | } |
9736 | ||
838e9690 YS |
9737 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
9738 | union bpf_attr __user *uattr) | |
51580e79 | 9739 | { |
06ee7115 | 9740 | u64 start_time = ktime_get_ns(); |
58e2af8b | 9741 | struct bpf_verifier_env *env; |
b9193c1b | 9742 | struct bpf_verifier_log *log; |
9e4c24e7 | 9743 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 9744 | bool is_priv; |
51580e79 | 9745 | |
eba0c929 AB |
9746 | /* no program is valid */ |
9747 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
9748 | return -EINVAL; | |
9749 | ||
58e2af8b | 9750 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
9751 | * allocate/free it every time bpf_check() is called |
9752 | */ | |
58e2af8b | 9753 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
9754 | if (!env) |
9755 | return -ENOMEM; | |
61bd5218 | 9756 | log = &env->log; |
cbd35700 | 9757 | |
9e4c24e7 | 9758 | len = (*prog)->len; |
fad953ce | 9759 | env->insn_aux_data = |
9e4c24e7 | 9760 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
9761 | ret = -ENOMEM; |
9762 | if (!env->insn_aux_data) | |
9763 | goto err_free_env; | |
9e4c24e7 JK |
9764 | for (i = 0; i < len; i++) |
9765 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 9766 | env->prog = *prog; |
00176a34 | 9767 | env->ops = bpf_verifier_ops[env->prog->type]; |
45a73c17 | 9768 | is_priv = capable(CAP_SYS_ADMIN); |
0246e64d | 9769 | |
8580ac94 AS |
9770 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
9771 | mutex_lock(&bpf_verifier_lock); | |
9772 | if (!btf_vmlinux) | |
9773 | btf_vmlinux = btf_parse_vmlinux(); | |
9774 | mutex_unlock(&bpf_verifier_lock); | |
9775 | } | |
9776 | ||
cbd35700 | 9777 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
9778 | if (!is_priv) |
9779 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
9780 | |
9781 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
9782 | /* user requested verbose verifier output | |
9783 | * and supplied buffer to store the verification trace | |
9784 | */ | |
e7bf8249 JK |
9785 | log->level = attr->log_level; |
9786 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
9787 | log->len_total = attr->log_size; | |
cbd35700 AS |
9788 | |
9789 | ret = -EINVAL; | |
e7bf8249 | 9790 | /* log attributes have to be sane */ |
7a9f5c65 | 9791 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 9792 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 9793 | goto err_unlock; |
cbd35700 | 9794 | } |
1ad2f583 | 9795 | |
8580ac94 AS |
9796 | if (IS_ERR(btf_vmlinux)) { |
9797 | /* Either gcc or pahole or kernel are broken. */ | |
9798 | verbose(env, "in-kernel BTF is malformed\n"); | |
9799 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 9800 | goto skip_full_check; |
8580ac94 AS |
9801 | } |
9802 | ||
38207291 MKL |
9803 | ret = check_attach_btf_id(env); |
9804 | if (ret) | |
9805 | goto skip_full_check; | |
9806 | ||
1ad2f583 DB |
9807 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
9808 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 9809 | env->strict_alignment = true; |
e9ee9efc DM |
9810 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
9811 | env->strict_alignment = false; | |
cbd35700 | 9812 | |
e2ae4ca2 JK |
9813 | env->allow_ptr_leaks = is_priv; |
9814 | ||
10d274e8 AS |
9815 | if (is_priv) |
9816 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
9817 | ||
f4e3ec0d JK |
9818 | ret = replace_map_fd_with_map_ptr(env); |
9819 | if (ret < 0) | |
9820 | goto skip_full_check; | |
9821 | ||
cae1927c | 9822 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 9823 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 9824 | if (ret) |
f4e3ec0d | 9825 | goto skip_full_check; |
ab3f0063 JK |
9826 | } |
9827 | ||
dc2a4ebc | 9828 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 9829 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
9830 | GFP_USER); |
9831 | ret = -ENOMEM; | |
9832 | if (!env->explored_states) | |
9833 | goto skip_full_check; | |
9834 | ||
d9762e84 | 9835 | ret = check_subprogs(env); |
475fb78f AS |
9836 | if (ret < 0) |
9837 | goto skip_full_check; | |
9838 | ||
c454a46b | 9839 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
9840 | if (ret < 0) |
9841 | goto skip_full_check; | |
9842 | ||
d9762e84 MKL |
9843 | ret = check_cfg(env); |
9844 | if (ret < 0) | |
9845 | goto skip_full_check; | |
9846 | ||
17a52670 | 9847 | ret = do_check(env); |
8c01c4f8 CG |
9848 | if (env->cur_state) { |
9849 | free_verifier_state(env->cur_state, true); | |
9850 | env->cur_state = NULL; | |
9851 | } | |
cbd35700 | 9852 | |
c941ce9c QM |
9853 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
9854 | ret = bpf_prog_offload_finalize(env); | |
9855 | ||
0246e64d | 9856 | skip_full_check: |
638f5b90 | 9857 | while (!pop_stack(env, NULL, NULL)); |
f1bca824 | 9858 | free_states(env); |
0246e64d | 9859 | |
c131187d | 9860 | if (ret == 0) |
9b38c405 | 9861 | ret = check_max_stack_depth(env); |
c131187d | 9862 | |
9b38c405 | 9863 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
9864 | if (is_priv) { |
9865 | if (ret == 0) | |
9866 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
9867 | if (ret == 0) |
9868 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
9869 | if (ret == 0) |
9870 | ret = opt_remove_nops(env); | |
52875a04 JK |
9871 | } else { |
9872 | if (ret == 0) | |
9873 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
9874 | } |
9875 | ||
9bac3d6d AS |
9876 | if (ret == 0) |
9877 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
9878 | ret = convert_ctx_accesses(env); | |
9879 | ||
e245c5c6 | 9880 | if (ret == 0) |
79741b3b | 9881 | ret = fixup_bpf_calls(env); |
e245c5c6 | 9882 | |
a4b1d3c1 JW |
9883 | /* do 32-bit optimization after insn patching has done so those patched |
9884 | * insns could be handled correctly. | |
9885 | */ | |
d6c2308c JW |
9886 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
9887 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
9888 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
9889 | : false; | |
a4b1d3c1 JW |
9890 | } |
9891 | ||
1ea47e01 AS |
9892 | if (ret == 0) |
9893 | ret = fixup_call_args(env); | |
9894 | ||
06ee7115 AS |
9895 | env->verification_time = ktime_get_ns() - start_time; |
9896 | print_verification_stats(env); | |
9897 | ||
a2a7d570 | 9898 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 9899 | ret = -ENOSPC; |
a2a7d570 | 9900 | if (log->level && !log->ubuf) { |
cbd35700 | 9901 | ret = -EFAULT; |
a2a7d570 | 9902 | goto err_release_maps; |
cbd35700 AS |
9903 | } |
9904 | ||
0246e64d AS |
9905 | if (ret == 0 && env->used_map_cnt) { |
9906 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
9907 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
9908 | sizeof(env->used_maps[0]), | |
9909 | GFP_KERNEL); | |
0246e64d | 9910 | |
9bac3d6d | 9911 | if (!env->prog->aux->used_maps) { |
0246e64d | 9912 | ret = -ENOMEM; |
a2a7d570 | 9913 | goto err_release_maps; |
0246e64d AS |
9914 | } |
9915 | ||
9bac3d6d | 9916 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 9917 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 9918 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
9919 | |
9920 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
9921 | * bpf_ld_imm64 instructions | |
9922 | */ | |
9923 | convert_pseudo_ld_imm64(env); | |
9924 | } | |
cbd35700 | 9925 | |
ba64e7d8 YS |
9926 | if (ret == 0) |
9927 | adjust_btf_func(env); | |
9928 | ||
a2a7d570 | 9929 | err_release_maps: |
9bac3d6d | 9930 | if (!env->prog->aux->used_maps) |
0246e64d | 9931 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 9932 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
9933 | */ |
9934 | release_maps(env); | |
9bac3d6d | 9935 | *prog = env->prog; |
3df126f3 | 9936 | err_unlock: |
45a73c17 AS |
9937 | if (!is_priv) |
9938 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
9939 | vfree(env->insn_aux_data); |
9940 | err_free_env: | |
9941 | kfree(env); | |
51580e79 AS |
9942 | return ret; |
9943 | } |