]>
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> |
6ba43b76 | 22 | #include <linux/error-injection.h> |
9e4e01df | 23 | #include <linux/bpf_lsm.h> |
1e6c62a8 | 24 | #include <linux/btf_ids.h> |
51580e79 | 25 | |
f4ac7e0b JK |
26 | #include "disasm.h" |
27 | ||
00176a34 | 28 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
91cc1a99 | 29 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
00176a34 JK |
30 | [_id] = & _name ## _verifier_ops, |
31 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 32 | #define BPF_LINK_TYPE(_id, _name) |
00176a34 JK |
33 | #include <linux/bpf_types.h> |
34 | #undef BPF_PROG_TYPE | |
35 | #undef BPF_MAP_TYPE | |
f2e10bff | 36 | #undef BPF_LINK_TYPE |
00176a34 JK |
37 | }; |
38 | ||
51580e79 AS |
39 | /* bpf_check() is a static code analyzer that walks eBPF program |
40 | * instruction by instruction and updates register/stack state. | |
41 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
42 | * | |
43 | * The first pass is depth-first-search to check that the program is a DAG. | |
44 | * It rejects the following programs: | |
45 | * - larger than BPF_MAXINSNS insns | |
46 | * - if loop is present (detected via back-edge) | |
47 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
48 | * - out of bounds or malformed jumps | |
49 | * The second pass is all possible path descent from the 1st insn. | |
50 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 51 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
52 | * insn is less then 4K, but there are too many branches that change stack/regs. |
53 | * Number of 'branches to be analyzed' is limited to 1k | |
54 | * | |
55 | * On entry to each instruction, each register has a type, and the instruction | |
56 | * changes the types of the registers depending on instruction semantics. | |
57 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
58 | * copied to R1. | |
59 | * | |
60 | * All registers are 64-bit. | |
61 | * R0 - return register | |
62 | * R1-R5 argument passing registers | |
63 | * R6-R9 callee saved registers | |
64 | * R10 - frame pointer read-only | |
65 | * | |
66 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
67 | * and has type PTR_TO_CTX. | |
68 | * | |
69 | * Verifier tracks arithmetic operations on pointers in case: | |
70 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
71 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
72 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
73 | * and 2nd arithmetic instruction is pattern matched to recognize | |
74 | * that it wants to construct a pointer to some element within stack. | |
75 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
76 | * (and -20 constant is saved for further stack bounds checking). | |
77 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
78 | * | |
f1174f77 | 79 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 80 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 81 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
82 | * |
83 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
84 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
85 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
86 | * |
87 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
88 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
89 | * | |
90 | * registers used to pass values to function calls are checked against | |
91 | * function argument constraints. | |
92 | * | |
93 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
94 | * It means that the register type passed to this function must be | |
95 | * PTR_TO_STACK and it will be used inside the function as | |
96 | * 'pointer to map element key' | |
97 | * | |
98 | * For example the argument constraints for bpf_map_lookup_elem(): | |
99 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
100 | * .arg1_type = ARG_CONST_MAP_PTR, | |
101 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
102 | * | |
103 | * ret_type says that this function returns 'pointer to map elem value or null' | |
104 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
105 | * 2nd argument should be a pointer to stack, which will be used inside | |
106 | * the helper function as a pointer to map element key. | |
107 | * | |
108 | * On the kernel side the helper function looks like: | |
109 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
110 | * { | |
111 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
112 | * void *key = (void *) (unsigned long) r2; | |
113 | * void *value; | |
114 | * | |
115 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
116 | * [key, key + map->key_size) bytes are valid and were initialized on | |
117 | * the stack of eBPF program. | |
118 | * } | |
119 | * | |
120 | * Corresponding eBPF program may look like: | |
121 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
122 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
123 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
124 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
125 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
126 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
127 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
128 | * | |
129 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
130 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
131 | * and were initialized prior to this call. | |
132 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
133 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
134 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
135 | * returns ether pointer to map value or NULL. | |
136 | * | |
137 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
138 | * insn, the register holding that pointer in the true branch changes state to | |
139 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
140 | * branch. See check_cond_jmp_op(). | |
141 | * | |
142 | * After the call R0 is set to return type of the function and registers R1-R5 | |
143 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
144 | * |
145 | * The following reference types represent a potential reference to a kernel | |
146 | * resource which, after first being allocated, must be checked and freed by | |
147 | * the BPF program: | |
148 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
149 | * | |
150 | * When the verifier sees a helper call return a reference type, it allocates a | |
151 | * pointer id for the reference and stores it in the current function state. | |
152 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
153 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
154 | * passes through a NULL-check conditional. For the branch wherein the state is | |
155 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
156 | * |
157 | * For each helper function that allocates a reference, such as | |
158 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
159 | * bpf_sk_release(). When a reference type passes into the release function, | |
160 | * the verifier also releases the reference. If any unchecked or unreleased | |
161 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
162 | */ |
163 | ||
17a52670 | 164 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 165 | struct bpf_verifier_stack_elem { |
17a52670 AS |
166 | /* verifer state is 'st' |
167 | * before processing instruction 'insn_idx' | |
168 | * and after processing instruction 'prev_insn_idx' | |
169 | */ | |
58e2af8b | 170 | struct bpf_verifier_state st; |
17a52670 AS |
171 | int insn_idx; |
172 | int prev_insn_idx; | |
58e2af8b | 173 | struct bpf_verifier_stack_elem *next; |
6f8a57cc AN |
174 | /* length of verifier log at the time this state was pushed on stack */ |
175 | u32 log_pos; | |
cbd35700 AS |
176 | }; |
177 | ||
b285fcb7 | 178 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 179 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 180 | |
d2e4c1e6 DB |
181 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
182 | #define BPF_MAP_KEY_SEEN (1ULL << 62) | |
183 | ||
c93552c4 DB |
184 | #define BPF_MAP_PTR_UNPRIV 1UL |
185 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
186 | POISON_POINTER_DELTA)) | |
187 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
188 | ||
189 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) | |
190 | { | |
d2e4c1e6 | 191 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
c93552c4 DB |
192 | } |
193 | ||
194 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
195 | { | |
d2e4c1e6 | 196 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
c93552c4 DB |
197 | } |
198 | ||
199 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
200 | const struct bpf_map *map, bool unpriv) | |
201 | { | |
202 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
203 | unpriv |= bpf_map_ptr_unpriv(aux); | |
d2e4c1e6 DB |
204 | aux->map_ptr_state = (unsigned long)map | |
205 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
206 | } | |
207 | ||
208 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) | |
209 | { | |
210 | return aux->map_key_state & BPF_MAP_KEY_POISON; | |
211 | } | |
212 | ||
213 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) | |
214 | { | |
215 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); | |
216 | } | |
217 | ||
218 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) | |
219 | { | |
220 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); | |
221 | } | |
222 | ||
223 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) | |
224 | { | |
225 | bool poisoned = bpf_map_key_poisoned(aux); | |
226 | ||
227 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | | |
228 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); | |
c93552c4 | 229 | } |
fad73a1a | 230 | |
33ff9823 DB |
231 | struct bpf_call_arg_meta { |
232 | struct bpf_map *map_ptr; | |
435faee1 | 233 | bool raw_mode; |
36bbef52 | 234 | bool pkt_access; |
435faee1 DB |
235 | int regno; |
236 | int access_size; | |
457f4436 | 237 | int mem_size; |
10060503 | 238 | u64 msize_max_value; |
1b986589 | 239 | int ref_obj_id; |
d83525ca | 240 | int func_id; |
22dc4a0f | 241 | struct btf *btf; |
eaa6bcb7 | 242 | u32 btf_id; |
22dc4a0f | 243 | struct btf *ret_btf; |
eaa6bcb7 | 244 | u32 ret_btf_id; |
33ff9823 DB |
245 | }; |
246 | ||
8580ac94 AS |
247 | struct btf *btf_vmlinux; |
248 | ||
cbd35700 AS |
249 | static DEFINE_MUTEX(bpf_verifier_lock); |
250 | ||
d9762e84 MKL |
251 | static const struct bpf_line_info * |
252 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
253 | { | |
254 | const struct bpf_line_info *linfo; | |
255 | const struct bpf_prog *prog; | |
256 | u32 i, nr_linfo; | |
257 | ||
258 | prog = env->prog; | |
259 | nr_linfo = prog->aux->nr_linfo; | |
260 | ||
261 | if (!nr_linfo || insn_off >= prog->len) | |
262 | return NULL; | |
263 | ||
264 | linfo = prog->aux->linfo; | |
265 | for (i = 1; i < nr_linfo; i++) | |
266 | if (insn_off < linfo[i].insn_off) | |
267 | break; | |
268 | ||
269 | return &linfo[i - 1]; | |
270 | } | |
271 | ||
77d2e05a MKL |
272 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
273 | va_list args) | |
cbd35700 | 274 | { |
a2a7d570 | 275 | unsigned int n; |
cbd35700 | 276 | |
a2a7d570 | 277 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
278 | |
279 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
280 | "verifier log line truncated - local buffer too short\n"); | |
281 | ||
282 | n = min(log->len_total - log->len_used - 1, n); | |
283 | log->kbuf[n] = '\0'; | |
284 | ||
8580ac94 AS |
285 | if (log->level == BPF_LOG_KERNEL) { |
286 | pr_err("BPF:%s\n", log->kbuf); | |
287 | return; | |
288 | } | |
a2a7d570 JK |
289 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
290 | log->len_used += n; | |
291 | else | |
292 | log->ubuf = NULL; | |
cbd35700 | 293 | } |
abe08840 | 294 | |
6f8a57cc AN |
295 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
296 | { | |
297 | char zero = 0; | |
298 | ||
299 | if (!bpf_verifier_log_needed(log)) | |
300 | return; | |
301 | ||
302 | log->len_used = new_pos; | |
303 | if (put_user(zero, log->ubuf + new_pos)) | |
304 | log->ubuf = NULL; | |
305 | } | |
306 | ||
abe08840 JO |
307 | /* log_level controls verbosity level of eBPF verifier. |
308 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
309 | * so the user can figure out what's wrong with the program | |
430e68d1 | 310 | */ |
abe08840 JO |
311 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
312 | const char *fmt, ...) | |
313 | { | |
314 | va_list args; | |
315 | ||
77d2e05a MKL |
316 | if (!bpf_verifier_log_needed(&env->log)) |
317 | return; | |
318 | ||
abe08840 | 319 | va_start(args, fmt); |
77d2e05a | 320 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
321 | va_end(args); |
322 | } | |
323 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
324 | ||
325 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
326 | { | |
77d2e05a | 327 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
328 | va_list args; |
329 | ||
77d2e05a MKL |
330 | if (!bpf_verifier_log_needed(&env->log)) |
331 | return; | |
332 | ||
abe08840 | 333 | va_start(args, fmt); |
77d2e05a | 334 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
335 | va_end(args); |
336 | } | |
cbd35700 | 337 | |
9e15db66 AS |
338 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
339 | const char *fmt, ...) | |
340 | { | |
341 | va_list args; | |
342 | ||
343 | if (!bpf_verifier_log_needed(log)) | |
344 | return; | |
345 | ||
346 | va_start(args, fmt); | |
347 | bpf_verifier_vlog(log, fmt, args); | |
348 | va_end(args); | |
349 | } | |
350 | ||
d9762e84 MKL |
351 | static const char *ltrim(const char *s) |
352 | { | |
353 | while (isspace(*s)) | |
354 | s++; | |
355 | ||
356 | return s; | |
357 | } | |
358 | ||
359 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
360 | u32 insn_off, | |
361 | const char *prefix_fmt, ...) | |
362 | { | |
363 | const struct bpf_line_info *linfo; | |
364 | ||
365 | if (!bpf_verifier_log_needed(&env->log)) | |
366 | return; | |
367 | ||
368 | linfo = find_linfo(env, insn_off); | |
369 | if (!linfo || linfo == env->prev_linfo) | |
370 | return; | |
371 | ||
372 | if (prefix_fmt) { | |
373 | va_list args; | |
374 | ||
375 | va_start(args, prefix_fmt); | |
376 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
377 | va_end(args); | |
378 | } | |
379 | ||
380 | verbose(env, "%s\n", | |
381 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
382 | linfo->line_off))); | |
383 | ||
384 | env->prev_linfo = linfo; | |
385 | } | |
386 | ||
de8f3a83 DB |
387 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
388 | { | |
389 | return type == PTR_TO_PACKET || | |
390 | type == PTR_TO_PACKET_META; | |
391 | } | |
392 | ||
46f8bc92 MKL |
393 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
394 | { | |
395 | return type == PTR_TO_SOCKET || | |
655a51e5 | 396 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
397 | type == PTR_TO_TCP_SOCK || |
398 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
399 | } |
400 | ||
cac616db JF |
401 | static bool reg_type_not_null(enum bpf_reg_type type) |
402 | { | |
403 | return type == PTR_TO_SOCKET || | |
404 | type == PTR_TO_TCP_SOCK || | |
405 | type == PTR_TO_MAP_VALUE || | |
01c66c48 | 406 | type == PTR_TO_SOCK_COMMON; |
cac616db JF |
407 | } |
408 | ||
840b9615 JS |
409 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
410 | { | |
fd978bf7 | 411 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 412 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 | 413 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
b121b341 | 414 | type == PTR_TO_TCP_SOCK_OR_NULL || |
457f4436 | 415 | type == PTR_TO_BTF_ID_OR_NULL || |
afbf21dc YS |
416 | type == PTR_TO_MEM_OR_NULL || |
417 | type == PTR_TO_RDONLY_BUF_OR_NULL || | |
418 | type == PTR_TO_RDWR_BUF_OR_NULL; | |
fd978bf7 JS |
419 | } |
420 | ||
d83525ca AS |
421 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
422 | { | |
423 | return reg->type == PTR_TO_MAP_VALUE && | |
424 | map_value_has_spin_lock(reg->map_ptr); | |
425 | } | |
426 | ||
cba368c1 MKL |
427 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
428 | { | |
429 | return type == PTR_TO_SOCKET || | |
430 | type == PTR_TO_SOCKET_OR_NULL || | |
431 | type == PTR_TO_TCP_SOCK || | |
457f4436 AN |
432 | type == PTR_TO_TCP_SOCK_OR_NULL || |
433 | type == PTR_TO_MEM || | |
434 | type == PTR_TO_MEM_OR_NULL; | |
cba368c1 MKL |
435 | } |
436 | ||
1b986589 | 437 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 438 | { |
1b986589 | 439 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
440 | } |
441 | ||
fd1b0d60 LB |
442 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
443 | { | |
444 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || | |
445 | type == ARG_PTR_TO_MEM_OR_NULL || | |
446 | type == ARG_PTR_TO_CTX_OR_NULL || | |
447 | type == ARG_PTR_TO_SOCKET_OR_NULL || | |
448 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; | |
449 | } | |
450 | ||
fd978bf7 JS |
451 | /* Determine whether the function releases some resources allocated by another |
452 | * function call. The first reference type argument will be assumed to be | |
453 | * released by release_reference(). | |
454 | */ | |
455 | static bool is_release_function(enum bpf_func_id func_id) | |
456 | { | |
457f4436 AN |
457 | return func_id == BPF_FUNC_sk_release || |
458 | func_id == BPF_FUNC_ringbuf_submit || | |
459 | func_id == BPF_FUNC_ringbuf_discard; | |
840b9615 JS |
460 | } |
461 | ||
64d85290 | 462 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
46f8bc92 MKL |
463 | { |
464 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 | 465 | func_id == BPF_FUNC_sk_lookup_udp || |
64d85290 | 466 | func_id == BPF_FUNC_skc_lookup_tcp || |
457f4436 AN |
467 | func_id == BPF_FUNC_map_lookup_elem || |
468 | func_id == BPF_FUNC_ringbuf_reserve; | |
64d85290 JS |
469 | } |
470 | ||
471 | static bool is_acquire_function(enum bpf_func_id func_id, | |
472 | const struct bpf_map *map) | |
473 | { | |
474 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
475 | ||
476 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
477 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 AN |
478 | func_id == BPF_FUNC_skc_lookup_tcp || |
479 | func_id == BPF_FUNC_ringbuf_reserve) | |
64d85290 JS |
480 | return true; |
481 | ||
482 | if (func_id == BPF_FUNC_map_lookup_elem && | |
483 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
484 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
485 | return true; | |
486 | ||
487 | return false; | |
46f8bc92 MKL |
488 | } |
489 | ||
1b986589 MKL |
490 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
491 | { | |
492 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
493 | func_id == BPF_FUNC_sk_fullsock || |
494 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
495 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
496 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
497 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || | |
498 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
499 | } |
500 | ||
17a52670 AS |
501 | /* string representation of 'enum bpf_reg_type' */ |
502 | static const char * const reg_type_str[] = { | |
503 | [NOT_INIT] = "?", | |
f1174f77 | 504 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
505 | [PTR_TO_CTX] = "ctx", |
506 | [CONST_PTR_TO_MAP] = "map_ptr", | |
507 | [PTR_TO_MAP_VALUE] = "map_value", | |
508 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 509 | [PTR_TO_STACK] = "fp", |
969bf05e | 510 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 511 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 512 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 513 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
514 | [PTR_TO_SOCKET] = "sock", |
515 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
516 | [PTR_TO_SOCK_COMMON] = "sock_common", |
517 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
518 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
519 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 520 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 521 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
9e15db66 | 522 | [PTR_TO_BTF_ID] = "ptr_", |
b121b341 | 523 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
eaa6bcb7 | 524 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
457f4436 AN |
525 | [PTR_TO_MEM] = "mem", |
526 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", | |
afbf21dc YS |
527 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
528 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", | |
529 | [PTR_TO_RDWR_BUF] = "rdwr_buf", | |
530 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", | |
17a52670 AS |
531 | }; |
532 | ||
8efea21d EC |
533 | static char slot_type_char[] = { |
534 | [STACK_INVALID] = '?', | |
535 | [STACK_SPILL] = 'r', | |
536 | [STACK_MISC] = 'm', | |
537 | [STACK_ZERO] = '0', | |
538 | }; | |
539 | ||
4e92024a AS |
540 | static void print_liveness(struct bpf_verifier_env *env, |
541 | enum bpf_reg_liveness live) | |
542 | { | |
9242b5f5 | 543 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
544 | verbose(env, "_"); |
545 | if (live & REG_LIVE_READ) | |
546 | verbose(env, "r"); | |
547 | if (live & REG_LIVE_WRITTEN) | |
548 | verbose(env, "w"); | |
9242b5f5 AS |
549 | if (live & REG_LIVE_DONE) |
550 | verbose(env, "D"); | |
4e92024a AS |
551 | } |
552 | ||
f4d7e40a AS |
553 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
554 | const struct bpf_reg_state *reg) | |
555 | { | |
556 | struct bpf_verifier_state *cur = env->cur_state; | |
557 | ||
558 | return cur->frame[reg->frameno]; | |
559 | } | |
560 | ||
22dc4a0f | 561 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
9e15db66 | 562 | { |
22dc4a0f | 563 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
9e15db66 AS |
564 | } |
565 | ||
61bd5218 | 566 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 567 | const struct bpf_func_state *state) |
17a52670 | 568 | { |
f4d7e40a | 569 | const struct bpf_reg_state *reg; |
17a52670 AS |
570 | enum bpf_reg_type t; |
571 | int i; | |
572 | ||
f4d7e40a AS |
573 | if (state->frameno) |
574 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 575 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
576 | reg = &state->regs[i]; |
577 | t = reg->type; | |
17a52670 AS |
578 | if (t == NOT_INIT) |
579 | continue; | |
4e92024a AS |
580 | verbose(env, " R%d", i); |
581 | print_liveness(env, reg->live); | |
582 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
583 | if (t == SCALAR_VALUE && reg->precise) |
584 | verbose(env, "P"); | |
f1174f77 EC |
585 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
586 | tnum_is_const(reg->var_off)) { | |
587 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 588 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 589 | } else { |
eaa6bcb7 HL |
590 | if (t == PTR_TO_BTF_ID || |
591 | t == PTR_TO_BTF_ID_OR_NULL || | |
592 | t == PTR_TO_PERCPU_BTF_ID) | |
22dc4a0f | 593 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
cba368c1 MKL |
594 | verbose(env, "(id=%d", reg->id); |
595 | if (reg_type_may_be_refcounted_or_null(t)) | |
596 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 597 | if (t != SCALAR_VALUE) |
61bd5218 | 598 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 599 | if (type_is_pkt_pointer(t)) |
61bd5218 | 600 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
601 | else if (t == CONST_PTR_TO_MAP || |
602 | t == PTR_TO_MAP_VALUE || | |
603 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 604 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
605 | reg->map_ptr->key_size, |
606 | reg->map_ptr->value_size); | |
7d1238f2 EC |
607 | if (tnum_is_const(reg->var_off)) { |
608 | /* Typically an immediate SCALAR_VALUE, but | |
609 | * could be a pointer whose offset is too big | |
610 | * for reg->off | |
611 | */ | |
61bd5218 | 612 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
613 | } else { |
614 | if (reg->smin_value != reg->umin_value && | |
615 | reg->smin_value != S64_MIN) | |
61bd5218 | 616 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
617 | (long long)reg->smin_value); |
618 | if (reg->smax_value != reg->umax_value && | |
619 | reg->smax_value != S64_MAX) | |
61bd5218 | 620 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
621 | (long long)reg->smax_value); |
622 | if (reg->umin_value != 0) | |
61bd5218 | 623 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
624 | (unsigned long long)reg->umin_value); |
625 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 626 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
627 | (unsigned long long)reg->umax_value); |
628 | if (!tnum_is_unknown(reg->var_off)) { | |
629 | char tn_buf[48]; | |
f1174f77 | 630 | |
7d1238f2 | 631 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 632 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 633 | } |
3f50f132 JF |
634 | if (reg->s32_min_value != reg->smin_value && |
635 | reg->s32_min_value != S32_MIN) | |
636 | verbose(env, ",s32_min_value=%d", | |
637 | (int)(reg->s32_min_value)); | |
638 | if (reg->s32_max_value != reg->smax_value && | |
639 | reg->s32_max_value != S32_MAX) | |
640 | verbose(env, ",s32_max_value=%d", | |
641 | (int)(reg->s32_max_value)); | |
642 | if (reg->u32_min_value != reg->umin_value && | |
643 | reg->u32_min_value != U32_MIN) | |
644 | verbose(env, ",u32_min_value=%d", | |
645 | (int)(reg->u32_min_value)); | |
646 | if (reg->u32_max_value != reg->umax_value && | |
647 | reg->u32_max_value != U32_MAX) | |
648 | verbose(env, ",u32_max_value=%d", | |
649 | (int)(reg->u32_max_value)); | |
f1174f77 | 650 | } |
61bd5218 | 651 | verbose(env, ")"); |
f1174f77 | 652 | } |
17a52670 | 653 | } |
638f5b90 | 654 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
655 | char types_buf[BPF_REG_SIZE + 1]; |
656 | bool valid = false; | |
657 | int j; | |
658 | ||
659 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
660 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
661 | valid = true; | |
662 | types_buf[j] = slot_type_char[ | |
663 | state->stack[i].slot_type[j]]; | |
664 | } | |
665 | types_buf[BPF_REG_SIZE] = 0; | |
666 | if (!valid) | |
667 | continue; | |
668 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
669 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
b5dc0163 AS |
670 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
671 | reg = &state->stack[i].spilled_ptr; | |
672 | t = reg->type; | |
673 | verbose(env, "=%s", reg_type_str[t]); | |
674 | if (t == SCALAR_VALUE && reg->precise) | |
675 | verbose(env, "P"); | |
676 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
677 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
678 | } else { | |
8efea21d | 679 | verbose(env, "=%s", types_buf); |
b5dc0163 | 680 | } |
17a52670 | 681 | } |
fd978bf7 JS |
682 | if (state->acquired_refs && state->refs[0].id) { |
683 | verbose(env, " refs=%d", state->refs[0].id); | |
684 | for (i = 1; i < state->acquired_refs; i++) | |
685 | if (state->refs[i].id) | |
686 | verbose(env, ",%d", state->refs[i].id); | |
687 | } | |
61bd5218 | 688 | verbose(env, "\n"); |
17a52670 AS |
689 | } |
690 | ||
84dbf350 JS |
691 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
692 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
693 | const struct bpf_func_state *src) \ | |
694 | { \ | |
695 | if (!src->FIELD) \ | |
696 | return 0; \ | |
697 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
698 | /* internal bug, make state invalid to reject the program */ \ | |
699 | memset(dst, 0, sizeof(*dst)); \ | |
700 | return -EFAULT; \ | |
701 | } \ | |
702 | memcpy(dst->FIELD, src->FIELD, \ | |
703 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
704 | return 0; \ | |
638f5b90 | 705 | } |
fd978bf7 JS |
706 | /* copy_reference_state() */ |
707 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
708 | /* copy_stack_state() */ |
709 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
710 | #undef COPY_STATE_FN | |
711 | ||
712 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
713 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
714 | bool copy_old) \ | |
715 | { \ | |
716 | u32 old_size = state->COUNT; \ | |
717 | struct bpf_##NAME##_state *new_##FIELD; \ | |
718 | int slot = size / SIZE; \ | |
719 | \ | |
720 | if (size <= old_size || !size) { \ | |
721 | if (copy_old) \ | |
722 | return 0; \ | |
723 | state->COUNT = slot * SIZE; \ | |
724 | if (!size && old_size) { \ | |
725 | kfree(state->FIELD); \ | |
726 | state->FIELD = NULL; \ | |
727 | } \ | |
728 | return 0; \ | |
729 | } \ | |
730 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
731 | GFP_KERNEL); \ | |
732 | if (!new_##FIELD) \ | |
733 | return -ENOMEM; \ | |
734 | if (copy_old) { \ | |
735 | if (state->FIELD) \ | |
736 | memcpy(new_##FIELD, state->FIELD, \ | |
737 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
738 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
739 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
740 | } \ | |
741 | state->COUNT = slot * SIZE; \ | |
742 | kfree(state->FIELD); \ | |
743 | state->FIELD = new_##FIELD; \ | |
744 | return 0; \ | |
745 | } | |
fd978bf7 JS |
746 | /* realloc_reference_state() */ |
747 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
748 | /* realloc_stack_state() */ |
749 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
750 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
751 | |
752 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
753 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 754 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
755 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
756 | * which realloc_stack_state() copies over. It points to previous | |
757 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 758 | */ |
fd978bf7 JS |
759 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
760 | int refs_size, bool copy_old) | |
638f5b90 | 761 | { |
fd978bf7 JS |
762 | int err = realloc_reference_state(state, refs_size, copy_old); |
763 | if (err) | |
764 | return err; | |
765 | return realloc_stack_state(state, stack_size, copy_old); | |
766 | } | |
767 | ||
768 | /* Acquire a pointer id from the env and update the state->refs to include | |
769 | * this new pointer reference. | |
770 | * On success, returns a valid pointer id to associate with the register | |
771 | * On failure, returns a negative errno. | |
638f5b90 | 772 | */ |
fd978bf7 | 773 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 774 | { |
fd978bf7 JS |
775 | struct bpf_func_state *state = cur_func(env); |
776 | int new_ofs = state->acquired_refs; | |
777 | int id, err; | |
778 | ||
779 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
780 | if (err) | |
781 | return err; | |
782 | id = ++env->id_gen; | |
783 | state->refs[new_ofs].id = id; | |
784 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 785 | |
fd978bf7 JS |
786 | return id; |
787 | } | |
788 | ||
789 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 790 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
791 | { |
792 | int i, last_idx; | |
793 | ||
fd978bf7 JS |
794 | last_idx = state->acquired_refs - 1; |
795 | for (i = 0; i < state->acquired_refs; i++) { | |
796 | if (state->refs[i].id == ptr_id) { | |
797 | if (last_idx && i != last_idx) | |
798 | memcpy(&state->refs[i], &state->refs[last_idx], | |
799 | sizeof(*state->refs)); | |
800 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
801 | state->acquired_refs--; | |
638f5b90 | 802 | return 0; |
638f5b90 | 803 | } |
638f5b90 | 804 | } |
46f8bc92 | 805 | return -EINVAL; |
fd978bf7 JS |
806 | } |
807 | ||
808 | static int transfer_reference_state(struct bpf_func_state *dst, | |
809 | struct bpf_func_state *src) | |
810 | { | |
811 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
812 | if (err) | |
813 | return err; | |
814 | err = copy_reference_state(dst, src); | |
815 | if (err) | |
816 | return err; | |
638f5b90 AS |
817 | return 0; |
818 | } | |
819 | ||
f4d7e40a AS |
820 | static void free_func_state(struct bpf_func_state *state) |
821 | { | |
5896351e AS |
822 | if (!state) |
823 | return; | |
fd978bf7 | 824 | kfree(state->refs); |
f4d7e40a AS |
825 | kfree(state->stack); |
826 | kfree(state); | |
827 | } | |
828 | ||
b5dc0163 AS |
829 | static void clear_jmp_history(struct bpf_verifier_state *state) |
830 | { | |
831 | kfree(state->jmp_history); | |
832 | state->jmp_history = NULL; | |
833 | state->jmp_history_cnt = 0; | |
834 | } | |
835 | ||
1969db47 AS |
836 | static void free_verifier_state(struct bpf_verifier_state *state, |
837 | bool free_self) | |
638f5b90 | 838 | { |
f4d7e40a AS |
839 | int i; |
840 | ||
841 | for (i = 0; i <= state->curframe; i++) { | |
842 | free_func_state(state->frame[i]); | |
843 | state->frame[i] = NULL; | |
844 | } | |
b5dc0163 | 845 | clear_jmp_history(state); |
1969db47 AS |
846 | if (free_self) |
847 | kfree(state); | |
638f5b90 AS |
848 | } |
849 | ||
850 | /* copy verifier state from src to dst growing dst stack space | |
851 | * when necessary to accommodate larger src stack | |
852 | */ | |
f4d7e40a AS |
853 | static int copy_func_state(struct bpf_func_state *dst, |
854 | const struct bpf_func_state *src) | |
638f5b90 AS |
855 | { |
856 | int err; | |
857 | ||
fd978bf7 JS |
858 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
859 | false); | |
860 | if (err) | |
861 | return err; | |
862 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
863 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
864 | if (err) |
865 | return err; | |
638f5b90 AS |
866 | return copy_stack_state(dst, src); |
867 | } | |
868 | ||
f4d7e40a AS |
869 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
870 | const struct bpf_verifier_state *src) | |
871 | { | |
872 | struct bpf_func_state *dst; | |
b5dc0163 | 873 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
f4d7e40a AS |
874 | int i, err; |
875 | ||
b5dc0163 AS |
876 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
877 | kfree(dst_state->jmp_history); | |
878 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); | |
879 | if (!dst_state->jmp_history) | |
880 | return -ENOMEM; | |
881 | } | |
882 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); | |
883 | dst_state->jmp_history_cnt = src->jmp_history_cnt; | |
884 | ||
f4d7e40a AS |
885 | /* if dst has more stack frames then src frame, free them */ |
886 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
887 | free_func_state(dst_state->frame[i]); | |
888 | dst_state->frame[i] = NULL; | |
889 | } | |
979d63d5 | 890 | dst_state->speculative = src->speculative; |
f4d7e40a | 891 | dst_state->curframe = src->curframe; |
d83525ca | 892 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
893 | dst_state->branches = src->branches; |
894 | dst_state->parent = src->parent; | |
b5dc0163 AS |
895 | dst_state->first_insn_idx = src->first_insn_idx; |
896 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
897 | for (i = 0; i <= src->curframe; i++) { |
898 | dst = dst_state->frame[i]; | |
899 | if (!dst) { | |
900 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
901 | if (!dst) | |
902 | return -ENOMEM; | |
903 | dst_state->frame[i] = dst; | |
904 | } | |
905 | err = copy_func_state(dst, src->frame[i]); | |
906 | if (err) | |
907 | return err; | |
908 | } | |
909 | return 0; | |
910 | } | |
911 | ||
2589726d AS |
912 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
913 | { | |
914 | while (st) { | |
915 | u32 br = --st->branches; | |
916 | ||
917 | /* WARN_ON(br > 1) technically makes sense here, | |
918 | * but see comment in push_stack(), hence: | |
919 | */ | |
920 | WARN_ONCE((int)br < 0, | |
921 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
922 | br); | |
923 | if (br) | |
924 | break; | |
925 | st = st->parent; | |
926 | } | |
927 | } | |
928 | ||
638f5b90 | 929 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 930 | int *insn_idx, bool pop_log) |
638f5b90 AS |
931 | { |
932 | struct bpf_verifier_state *cur = env->cur_state; | |
933 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
934 | int err; | |
17a52670 AS |
935 | |
936 | if (env->head == NULL) | |
638f5b90 | 937 | return -ENOENT; |
17a52670 | 938 | |
638f5b90 AS |
939 | if (cur) { |
940 | err = copy_verifier_state(cur, &head->st); | |
941 | if (err) | |
942 | return err; | |
943 | } | |
6f8a57cc AN |
944 | if (pop_log) |
945 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
946 | if (insn_idx) |
947 | *insn_idx = head->insn_idx; | |
17a52670 | 948 | if (prev_insn_idx) |
638f5b90 AS |
949 | *prev_insn_idx = head->prev_insn_idx; |
950 | elem = head->next; | |
1969db47 | 951 | free_verifier_state(&head->st, false); |
638f5b90 | 952 | kfree(head); |
17a52670 AS |
953 | env->head = elem; |
954 | env->stack_size--; | |
638f5b90 | 955 | return 0; |
17a52670 AS |
956 | } |
957 | ||
58e2af8b | 958 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
959 | int insn_idx, int prev_insn_idx, |
960 | bool speculative) | |
17a52670 | 961 | { |
638f5b90 | 962 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 963 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 964 | int err; |
17a52670 | 965 | |
638f5b90 | 966 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
967 | if (!elem) |
968 | goto err; | |
969 | ||
17a52670 AS |
970 | elem->insn_idx = insn_idx; |
971 | elem->prev_insn_idx = prev_insn_idx; | |
972 | elem->next = env->head; | |
6f8a57cc | 973 | elem->log_pos = env->log.len_used; |
17a52670 AS |
974 | env->head = elem; |
975 | env->stack_size++; | |
1969db47 AS |
976 | err = copy_verifier_state(&elem->st, cur); |
977 | if (err) | |
978 | goto err; | |
979d63d5 | 979 | elem->st.speculative |= speculative; |
b285fcb7 AS |
980 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
981 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
982 | env->stack_size); | |
17a52670 AS |
983 | goto err; |
984 | } | |
2589726d AS |
985 | if (elem->st.parent) { |
986 | ++elem->st.parent->branches; | |
987 | /* WARN_ON(branches > 2) technically makes sense here, | |
988 | * but | |
989 | * 1. speculative states will bump 'branches' for non-branch | |
990 | * instructions | |
991 | * 2. is_state_visited() heuristics may decide not to create | |
992 | * a new state for a sequence of branches and all such current | |
993 | * and cloned states will be pointing to a single parent state | |
994 | * which might have large 'branches' count. | |
995 | */ | |
996 | } | |
17a52670 AS |
997 | return &elem->st; |
998 | err: | |
5896351e AS |
999 | free_verifier_state(env->cur_state, true); |
1000 | env->cur_state = NULL; | |
17a52670 | 1001 | /* pop all elements and return */ |
6f8a57cc | 1002 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1003 | return NULL; |
1004 | } | |
1005 | ||
1006 | #define CALLER_SAVED_REGS 6 | |
1007 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1008 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1009 | }; | |
1010 | ||
f54c7898 DB |
1011 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1012 | struct bpf_reg_state *reg); | |
f1174f77 | 1013 | |
e688c3db AS |
1014 | /* This helper doesn't clear reg->id */ |
1015 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
b03c9f9f | 1016 | { |
b03c9f9f EC |
1017 | reg->var_off = tnum_const(imm); |
1018 | reg->smin_value = (s64)imm; | |
1019 | reg->smax_value = (s64)imm; | |
1020 | reg->umin_value = imm; | |
1021 | reg->umax_value = imm; | |
3f50f132 JF |
1022 | |
1023 | reg->s32_min_value = (s32)imm; | |
1024 | reg->s32_max_value = (s32)imm; | |
1025 | reg->u32_min_value = (u32)imm; | |
1026 | reg->u32_max_value = (u32)imm; | |
1027 | } | |
1028 | ||
e688c3db AS |
1029 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1030 | * known to have the value @imm. | |
1031 | */ | |
1032 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1033 | { | |
1034 | /* Clear id, off, and union(map_ptr, range) */ | |
1035 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1036 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
1037 | ___mark_reg_known(reg, imm); | |
1038 | } | |
1039 | ||
3f50f132 JF |
1040 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
1041 | { | |
1042 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1043 | reg->s32_min_value = (s32)imm; | |
1044 | reg->s32_max_value = (s32)imm; | |
1045 | reg->u32_min_value = (u32)imm; | |
1046 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1047 | } |
1048 | ||
f1174f77 EC |
1049 | /* Mark the 'variable offset' part of a register as zero. This should be |
1050 | * used only on registers holding a pointer type. | |
1051 | */ | |
1052 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1053 | { |
b03c9f9f | 1054 | __mark_reg_known(reg, 0); |
f1174f77 | 1055 | } |
a9789ef9 | 1056 | |
cc2b14d5 AS |
1057 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1058 | { | |
1059 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1060 | reg->type = SCALAR_VALUE; |
1061 | } | |
1062 | ||
61bd5218 JK |
1063 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1064 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1065 | { |
1066 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1067 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1068 | /* Something bad happened, let's kill all regs */ |
1069 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1070 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1071 | return; |
1072 | } | |
1073 | __mark_reg_known_zero(regs + regno); | |
1074 | } | |
1075 | ||
de8f3a83 DB |
1076 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1077 | { | |
1078 | return type_is_pkt_pointer(reg->type); | |
1079 | } | |
1080 | ||
1081 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1082 | { | |
1083 | return reg_is_pkt_pointer(reg) || | |
1084 | reg->type == PTR_TO_PACKET_END; | |
1085 | } | |
1086 | ||
1087 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1088 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1089 | enum bpf_reg_type which) | |
1090 | { | |
1091 | /* The register can already have a range from prior markings. | |
1092 | * This is fine as long as it hasn't been advanced from its | |
1093 | * origin. | |
1094 | */ | |
1095 | return reg->type == which && | |
1096 | reg->id == 0 && | |
1097 | reg->off == 0 && | |
1098 | tnum_equals_const(reg->var_off, 0); | |
1099 | } | |
1100 | ||
3f50f132 JF |
1101 | /* Reset the min/max bounds of a register */ |
1102 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1103 | { | |
1104 | reg->smin_value = S64_MIN; | |
1105 | reg->smax_value = S64_MAX; | |
1106 | reg->umin_value = 0; | |
1107 | reg->umax_value = U64_MAX; | |
1108 | ||
1109 | reg->s32_min_value = S32_MIN; | |
1110 | reg->s32_max_value = S32_MAX; | |
1111 | reg->u32_min_value = 0; | |
1112 | reg->u32_max_value = U32_MAX; | |
1113 | } | |
1114 | ||
1115 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1116 | { | |
1117 | reg->smin_value = S64_MIN; | |
1118 | reg->smax_value = S64_MAX; | |
1119 | reg->umin_value = 0; | |
1120 | reg->umax_value = U64_MAX; | |
1121 | } | |
1122 | ||
1123 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1124 | { | |
1125 | reg->s32_min_value = S32_MIN; | |
1126 | reg->s32_max_value = S32_MAX; | |
1127 | reg->u32_min_value = 0; | |
1128 | reg->u32_max_value = U32_MAX; | |
1129 | } | |
1130 | ||
1131 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1132 | { | |
1133 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1134 | ||
1135 | /* min signed is max(sign bit) | min(other bits) */ | |
1136 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1137 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1138 | /* max signed is min(sign bit) | max(other bits) */ | |
1139 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1140 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1141 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1142 | reg->u32_max_value = min(reg->u32_max_value, | |
1143 | (u32)(var32_off.value | var32_off.mask)); | |
1144 | } | |
1145 | ||
1146 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1147 | { |
1148 | /* min signed is max(sign bit) | min(other bits) */ | |
1149 | reg->smin_value = max_t(s64, reg->smin_value, | |
1150 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1151 | /* max signed is min(sign bit) | max(other bits) */ | |
1152 | reg->smax_value = min_t(s64, reg->smax_value, | |
1153 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1154 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1155 | reg->umax_value = min(reg->umax_value, | |
1156 | reg->var_off.value | reg->var_off.mask); | |
1157 | } | |
1158 | ||
3f50f132 JF |
1159 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1160 | { | |
1161 | __update_reg32_bounds(reg); | |
1162 | __update_reg64_bounds(reg); | |
1163 | } | |
1164 | ||
b03c9f9f | 1165 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1166 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1167 | { | |
1168 | /* Learn sign from signed bounds. | |
1169 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1170 | * are the same, so combine. This works even in the negative case, e.g. | |
1171 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1172 | */ | |
1173 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1174 | reg->s32_min_value = reg->u32_min_value = | |
1175 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1176 | reg->s32_max_value = reg->u32_max_value = | |
1177 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1178 | return; | |
1179 | } | |
1180 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1181 | * boundary, so we must be careful. | |
1182 | */ | |
1183 | if ((s32)reg->u32_max_value >= 0) { | |
1184 | /* Positive. We can't learn anything from the smin, but smax | |
1185 | * is positive, hence safe. | |
1186 | */ | |
1187 | reg->s32_min_value = reg->u32_min_value; | |
1188 | reg->s32_max_value = reg->u32_max_value = | |
1189 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1190 | } else if ((s32)reg->u32_min_value < 0) { | |
1191 | /* Negative. We can't learn anything from the smax, but smin | |
1192 | * is negative, hence safe. | |
1193 | */ | |
1194 | reg->s32_min_value = reg->u32_min_value = | |
1195 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1196 | reg->s32_max_value = reg->u32_max_value; | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1201 | { |
1202 | /* Learn sign from signed bounds. | |
1203 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1204 | * are the same, so combine. This works even in the negative case, e.g. | |
1205 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1206 | */ | |
1207 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1208 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1209 | reg->umin_value); | |
1210 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1211 | reg->umax_value); | |
1212 | return; | |
1213 | } | |
1214 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1215 | * boundary, so we must be careful. | |
1216 | */ | |
1217 | if ((s64)reg->umax_value >= 0) { | |
1218 | /* Positive. We can't learn anything from the smin, but smax | |
1219 | * is positive, hence safe. | |
1220 | */ | |
1221 | reg->smin_value = reg->umin_value; | |
1222 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1223 | reg->umax_value); | |
1224 | } else if ((s64)reg->umin_value < 0) { | |
1225 | /* Negative. We can't learn anything from the smax, but smin | |
1226 | * is negative, hence safe. | |
1227 | */ | |
1228 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1229 | reg->umin_value); | |
1230 | reg->smax_value = reg->umax_value; | |
1231 | } | |
1232 | } | |
1233 | ||
3f50f132 JF |
1234 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1235 | { | |
1236 | __reg32_deduce_bounds(reg); | |
1237 | __reg64_deduce_bounds(reg); | |
1238 | } | |
1239 | ||
b03c9f9f EC |
1240 | /* Attempts to improve var_off based on unsigned min/max information */ |
1241 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1242 | { | |
3f50f132 JF |
1243 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1244 | tnum_range(reg->umin_value, | |
1245 | reg->umax_value)); | |
1246 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1247 | tnum_range(reg->u32_min_value, | |
1248 | reg->u32_max_value)); | |
1249 | ||
1250 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1251 | } |
1252 | ||
3f50f132 | 1253 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1254 | { |
3f50f132 JF |
1255 | reg->umin_value = reg->u32_min_value; |
1256 | reg->umax_value = reg->u32_max_value; | |
1257 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds | |
1258 | * but must be positive otherwise set to worse case bounds | |
1259 | * and refine later from tnum. | |
1260 | */ | |
3a71dc36 | 1261 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
3f50f132 JF |
1262 | reg->smax_value = reg->s32_max_value; |
1263 | else | |
1264 | reg->smax_value = U32_MAX; | |
3a71dc36 JF |
1265 | if (reg->s32_min_value >= 0) |
1266 | reg->smin_value = reg->s32_min_value; | |
1267 | else | |
1268 | reg->smin_value = 0; | |
3f50f132 JF |
1269 | } |
1270 | ||
1271 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1272 | { | |
1273 | /* special case when 64-bit register has upper 32-bit register | |
1274 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1275 | * allowing us to use 32-bit bounds directly, | |
1276 | */ | |
1277 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1278 | __reg_assign_32_into_64(reg); | |
1279 | } else { | |
1280 | /* Otherwise the best we can do is push lower 32bit known and | |
1281 | * unknown bits into register (var_off set from jmp logic) | |
1282 | * then learn as much as possible from the 64-bit tnum | |
1283 | * known and unknown bits. The previous smin/smax bounds are | |
1284 | * invalid here because of jmp32 compare so mark them unknown | |
1285 | * so they do not impact tnum bounds calculation. | |
1286 | */ | |
1287 | __mark_reg64_unbounded(reg); | |
1288 | __update_reg_bounds(reg); | |
1289 | } | |
1290 | ||
1291 | /* Intersecting with the old var_off might have improved our bounds | |
1292 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1293 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1294 | */ | |
1295 | __reg_deduce_bounds(reg); | |
1296 | __reg_bound_offset(reg); | |
1297 | __update_reg_bounds(reg); | |
1298 | } | |
1299 | ||
1300 | static bool __reg64_bound_s32(s64 a) | |
1301 | { | |
b0270958 | 1302 | return a > S32_MIN && a < S32_MAX; |
3f50f132 JF |
1303 | } |
1304 | ||
1305 | static bool __reg64_bound_u32(u64 a) | |
1306 | { | |
1307 | if (a > U32_MIN && a < U32_MAX) | |
1308 | return true; | |
1309 | return false; | |
1310 | } | |
1311 | ||
1312 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1313 | { | |
1314 | __mark_reg32_unbounded(reg); | |
1315 | ||
b0270958 | 1316 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
3f50f132 | 1317 | reg->s32_min_value = (s32)reg->smin_value; |
3f50f132 | 1318 | reg->s32_max_value = (s32)reg->smax_value; |
b0270958 | 1319 | } |
3f50f132 JF |
1320 | if (__reg64_bound_u32(reg->umin_value)) |
1321 | reg->u32_min_value = (u32)reg->umin_value; | |
1322 | if (__reg64_bound_u32(reg->umax_value)) | |
1323 | reg->u32_max_value = (u32)reg->umax_value; | |
1324 | ||
1325 | /* Intersecting with the old var_off might have improved our bounds | |
1326 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1327 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1328 | */ | |
1329 | __reg_deduce_bounds(reg); | |
1330 | __reg_bound_offset(reg); | |
1331 | __update_reg_bounds(reg); | |
b03c9f9f EC |
1332 | } |
1333 | ||
f1174f77 | 1334 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1335 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1336 | struct bpf_reg_state *reg) | |
f1174f77 | 1337 | { |
a9c676bc AS |
1338 | /* |
1339 | * Clear type, id, off, and union(map_ptr, range) and | |
1340 | * padding between 'type' and union | |
1341 | */ | |
1342 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1343 | reg->type = SCALAR_VALUE; |
f1174f77 | 1344 | reg->var_off = tnum_unknown; |
f4d7e40a | 1345 | reg->frameno = 0; |
2c78ee89 | 1346 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1347 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1348 | } |
1349 | ||
61bd5218 JK |
1350 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1351 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1352 | { |
1353 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1354 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1355 | /* Something bad happened, let's kill all regs except FP */ |
1356 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1357 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1358 | return; |
1359 | } | |
f54c7898 | 1360 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1361 | } |
1362 | ||
f54c7898 DB |
1363 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1364 | struct bpf_reg_state *reg) | |
f1174f77 | 1365 | { |
f54c7898 | 1366 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1367 | reg->type = NOT_INIT; |
1368 | } | |
1369 | ||
61bd5218 JK |
1370 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1371 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1372 | { |
1373 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1374 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1375 | /* Something bad happened, let's kill all regs except FP */ |
1376 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1377 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1378 | return; |
1379 | } | |
f54c7898 | 1380 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1381 | } |
1382 | ||
41c48f3a AI |
1383 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1384 | struct bpf_reg_state *regs, u32 regno, | |
22dc4a0f AN |
1385 | enum bpf_reg_type reg_type, |
1386 | struct btf *btf, u32 btf_id) | |
41c48f3a AI |
1387 | { |
1388 | if (reg_type == SCALAR_VALUE) { | |
1389 | mark_reg_unknown(env, regs, regno); | |
1390 | return; | |
1391 | } | |
1392 | mark_reg_known_zero(env, regs, regno); | |
1393 | regs[regno].type = PTR_TO_BTF_ID; | |
22dc4a0f | 1394 | regs[regno].btf = btf; |
41c48f3a AI |
1395 | regs[regno].btf_id = btf_id; |
1396 | } | |
1397 | ||
5327ed3d | 1398 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1399 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1400 | struct bpf_func_state *state) |
17a52670 | 1401 | { |
f4d7e40a | 1402 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1403 | int i; |
1404 | ||
dc503a8a | 1405 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1406 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1407 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1408 | regs[i].parent = NULL; |
5327ed3d | 1409 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1410 | } |
17a52670 AS |
1411 | |
1412 | /* frame pointer */ | |
f1174f77 | 1413 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1414 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1415 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1416 | } |
1417 | ||
f4d7e40a AS |
1418 | #define BPF_MAIN_FUNC (-1) |
1419 | static void init_func_state(struct bpf_verifier_env *env, | |
1420 | struct bpf_func_state *state, | |
1421 | int callsite, int frameno, int subprogno) | |
1422 | { | |
1423 | state->callsite = callsite; | |
1424 | state->frameno = frameno; | |
1425 | state->subprogno = subprogno; | |
1426 | init_reg_state(env, state); | |
1427 | } | |
1428 | ||
17a52670 AS |
1429 | enum reg_arg_type { |
1430 | SRC_OP, /* register is used as source operand */ | |
1431 | DST_OP, /* register is used as destination operand */ | |
1432 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1433 | }; | |
1434 | ||
cc8b0b92 AS |
1435 | static int cmp_subprogs(const void *a, const void *b) |
1436 | { | |
9c8105bd JW |
1437 | return ((struct bpf_subprog_info *)a)->start - |
1438 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1439 | } |
1440 | ||
1441 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1442 | { | |
9c8105bd | 1443 | struct bpf_subprog_info *p; |
cc8b0b92 | 1444 | |
9c8105bd JW |
1445 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1446 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1447 | if (!p) |
1448 | return -ENOENT; | |
9c8105bd | 1449 | return p - env->subprog_info; |
cc8b0b92 AS |
1450 | |
1451 | } | |
1452 | ||
1453 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1454 | { | |
1455 | int insn_cnt = env->prog->len; | |
1456 | int ret; | |
1457 | ||
1458 | if (off >= insn_cnt || off < 0) { | |
1459 | verbose(env, "call to invalid destination\n"); | |
1460 | return -EINVAL; | |
1461 | } | |
1462 | ret = find_subprog(env, off); | |
1463 | if (ret >= 0) | |
1464 | return 0; | |
4cb3d99c | 1465 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1466 | verbose(env, "too many subprograms\n"); |
1467 | return -E2BIG; | |
1468 | } | |
9c8105bd JW |
1469 | env->subprog_info[env->subprog_cnt++].start = off; |
1470 | sort(env->subprog_info, env->subprog_cnt, | |
1471 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1472 | return 0; |
1473 | } | |
1474 | ||
1475 | static int check_subprogs(struct bpf_verifier_env *env) | |
1476 | { | |
1477 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1478 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1479 | struct bpf_insn *insn = env->prog->insnsi; |
1480 | int insn_cnt = env->prog->len; | |
1481 | ||
f910cefa JW |
1482 | /* Add entry function. */ |
1483 | ret = add_subprog(env, 0); | |
1484 | if (ret < 0) | |
1485 | return ret; | |
1486 | ||
cc8b0b92 AS |
1487 | /* determine subprog starts. The end is one before the next starts */ |
1488 | for (i = 0; i < insn_cnt; i++) { | |
1489 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1490 | continue; | |
1491 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1492 | continue; | |
2c78ee89 AS |
1493 | if (!env->bpf_capable) { |
1494 | verbose(env, | |
1495 | "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); | |
cc8b0b92 AS |
1496 | return -EPERM; |
1497 | } | |
cc8b0b92 AS |
1498 | ret = add_subprog(env, i + insn[i].imm + 1); |
1499 | if (ret < 0) | |
1500 | return ret; | |
1501 | } | |
1502 | ||
4cb3d99c JW |
1503 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1504 | * logic. 'subprog_cnt' should not be increased. | |
1505 | */ | |
1506 | subprog[env->subprog_cnt].start = insn_cnt; | |
1507 | ||
06ee7115 | 1508 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1509 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1510 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1511 | |
1512 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1513 | subprog_start = subprog[cur_subprog].start; |
1514 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1515 | for (i = 0; i < insn_cnt; i++) { |
1516 | u8 code = insn[i].code; | |
1517 | ||
7f6e4312 MF |
1518 | if (code == (BPF_JMP | BPF_CALL) && |
1519 | insn[i].imm == BPF_FUNC_tail_call && | |
1520 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
1521 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
1522 | if (BPF_CLASS(code) == BPF_LD && |
1523 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
1524 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 1525 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1526 | goto next; |
1527 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1528 | goto next; | |
1529 | off = i + insn[i].off + 1; | |
1530 | if (off < subprog_start || off >= subprog_end) { | |
1531 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1532 | return -EINVAL; | |
1533 | } | |
1534 | next: | |
1535 | if (i == subprog_end - 1) { | |
1536 | /* to avoid fall-through from one subprog into another | |
1537 | * the last insn of the subprog should be either exit | |
1538 | * or unconditional jump back | |
1539 | */ | |
1540 | if (code != (BPF_JMP | BPF_EXIT) && | |
1541 | code != (BPF_JMP | BPF_JA)) { | |
1542 | verbose(env, "last insn is not an exit or jmp\n"); | |
1543 | return -EINVAL; | |
1544 | } | |
1545 | subprog_start = subprog_end; | |
4cb3d99c JW |
1546 | cur_subprog++; |
1547 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1548 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1549 | } |
1550 | } | |
1551 | return 0; | |
1552 | } | |
1553 | ||
679c782d EC |
1554 | /* Parentage chain of this register (or stack slot) should take care of all |
1555 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1556 | */ | |
f4d7e40a | 1557 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 1558 | const struct bpf_reg_state *state, |
5327ed3d | 1559 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
1560 | { |
1561 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1562 | int cnt = 0; |
dc503a8a EC |
1563 | |
1564 | while (parent) { | |
1565 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1566 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1567 | break; |
9242b5f5 AS |
1568 | if (parent->live & REG_LIVE_DONE) { |
1569 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1570 | reg_type_str[parent->type], | |
1571 | parent->var_off.value, parent->off); | |
1572 | return -EFAULT; | |
1573 | } | |
5327ed3d JW |
1574 | /* The first condition is more likely to be true than the |
1575 | * second, checked it first. | |
1576 | */ | |
1577 | if ((parent->live & REG_LIVE_READ) == flag || | |
1578 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
1579 | /* The parentage chain never changes and |
1580 | * this parent was already marked as LIVE_READ. | |
1581 | * There is no need to keep walking the chain again and | |
1582 | * keep re-marking all parents as LIVE_READ. | |
1583 | * This case happens when the same register is read | |
1584 | * multiple times without writes into it in-between. | |
5327ed3d JW |
1585 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
1586 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
1587 | */ |
1588 | break; | |
dc503a8a | 1589 | /* ... then we depend on parent's value */ |
5327ed3d JW |
1590 | parent->live |= flag; |
1591 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
1592 | if (flag == REG_LIVE_READ64) | |
1593 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
1594 | state = parent; |
1595 | parent = state->parent; | |
f4d7e40a | 1596 | writes = true; |
06ee7115 | 1597 | cnt++; |
dc503a8a | 1598 | } |
06ee7115 AS |
1599 | |
1600 | if (env->longest_mark_read_walk < cnt) | |
1601 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1602 | return 0; |
dc503a8a EC |
1603 | } |
1604 | ||
5327ed3d JW |
1605 | /* This function is supposed to be used by the following 32-bit optimization |
1606 | * code only. It returns TRUE if the source or destination register operates | |
1607 | * on 64-bit, otherwise return FALSE. | |
1608 | */ | |
1609 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
1610 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
1611 | { | |
1612 | u8 code, class, op; | |
1613 | ||
1614 | code = insn->code; | |
1615 | class = BPF_CLASS(code); | |
1616 | op = BPF_OP(code); | |
1617 | if (class == BPF_JMP) { | |
1618 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
1619 | * conservatively. | |
1620 | */ | |
1621 | if (op == BPF_EXIT) | |
1622 | return true; | |
1623 | if (op == BPF_CALL) { | |
1624 | /* BPF to BPF call will reach here because of marking | |
1625 | * caller saved clobber with DST_OP_NO_MARK for which we | |
1626 | * don't care the register def because they are anyway | |
1627 | * marked as NOT_INIT already. | |
1628 | */ | |
1629 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1630 | return false; | |
1631 | /* Helper call will reach here because of arg type | |
1632 | * check, conservatively return TRUE. | |
1633 | */ | |
1634 | if (t == SRC_OP) | |
1635 | return true; | |
1636 | ||
1637 | return false; | |
1638 | } | |
1639 | } | |
1640 | ||
1641 | if (class == BPF_ALU64 || class == BPF_JMP || | |
1642 | /* BPF_END always use BPF_ALU class. */ | |
1643 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
1644 | return true; | |
1645 | ||
1646 | if (class == BPF_ALU || class == BPF_JMP32) | |
1647 | return false; | |
1648 | ||
1649 | if (class == BPF_LDX) { | |
1650 | if (t != SRC_OP) | |
1651 | return BPF_SIZE(code) == BPF_DW; | |
1652 | /* LDX source must be ptr. */ | |
1653 | return true; | |
1654 | } | |
1655 | ||
1656 | if (class == BPF_STX) { | |
1657 | if (reg->type != SCALAR_VALUE) | |
1658 | return true; | |
1659 | return BPF_SIZE(code) == BPF_DW; | |
1660 | } | |
1661 | ||
1662 | if (class == BPF_LD) { | |
1663 | u8 mode = BPF_MODE(code); | |
1664 | ||
1665 | /* LD_IMM64 */ | |
1666 | if (mode == BPF_IMM) | |
1667 | return true; | |
1668 | ||
1669 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
1670 | if (t != SRC_OP) | |
1671 | return false; | |
1672 | ||
1673 | /* Implicit ctx ptr. */ | |
1674 | if (regno == BPF_REG_6) | |
1675 | return true; | |
1676 | ||
1677 | /* Explicit source could be any width. */ | |
1678 | return true; | |
1679 | } | |
1680 | ||
1681 | if (class == BPF_ST) | |
1682 | /* The only source register for BPF_ST is a ptr. */ | |
1683 | return true; | |
1684 | ||
1685 | /* Conservatively return true at default. */ | |
1686 | return true; | |
1687 | } | |
1688 | ||
b325fbca JW |
1689 | /* Return TRUE if INSN doesn't have explicit value define. */ |
1690 | static bool insn_no_def(struct bpf_insn *insn) | |
1691 | { | |
1692 | u8 class = BPF_CLASS(insn->code); | |
1693 | ||
1694 | return (class == BPF_JMP || class == BPF_JMP32 || | |
1695 | class == BPF_STX || class == BPF_ST); | |
1696 | } | |
1697 | ||
1698 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
1699 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
1700 | { | |
1701 | if (insn_no_def(insn)) | |
1702 | return false; | |
1703 | ||
1704 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); | |
1705 | } | |
1706 | ||
5327ed3d JW |
1707 | static void mark_insn_zext(struct bpf_verifier_env *env, |
1708 | struct bpf_reg_state *reg) | |
1709 | { | |
1710 | s32 def_idx = reg->subreg_def; | |
1711 | ||
1712 | if (def_idx == DEF_NOT_SUBREG) | |
1713 | return; | |
1714 | ||
1715 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
1716 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
1717 | reg->subreg_def = DEF_NOT_SUBREG; | |
1718 | } | |
1719 | ||
dc503a8a | 1720 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
1721 | enum reg_arg_type t) |
1722 | { | |
f4d7e40a AS |
1723 | struct bpf_verifier_state *vstate = env->cur_state; |
1724 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 1725 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 1726 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 1727 | bool rw64; |
dc503a8a | 1728 | |
17a52670 | 1729 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1730 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1731 | return -EINVAL; |
1732 | } | |
1733 | ||
c342dc10 | 1734 | reg = ®s[regno]; |
5327ed3d | 1735 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
1736 | if (t == SRC_OP) { |
1737 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1738 | if (reg->type == NOT_INIT) { |
61bd5218 | 1739 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1740 | return -EACCES; |
1741 | } | |
679c782d | 1742 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1743 | if (regno == BPF_REG_FP) |
1744 | return 0; | |
1745 | ||
5327ed3d JW |
1746 | if (rw64) |
1747 | mark_insn_zext(env, reg); | |
1748 | ||
1749 | return mark_reg_read(env, reg, reg->parent, | |
1750 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
1751 | } else { |
1752 | /* check whether register used as dest operand can be written to */ | |
1753 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1754 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1755 | return -EACCES; |
1756 | } | |
c342dc10 | 1757 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 1758 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 1759 | if (t == DST_OP) |
61bd5218 | 1760 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1761 | } |
1762 | return 0; | |
1763 | } | |
1764 | ||
b5dc0163 AS |
1765 | /* for any branch, call, exit record the history of jmps in the given state */ |
1766 | static int push_jmp_history(struct bpf_verifier_env *env, | |
1767 | struct bpf_verifier_state *cur) | |
1768 | { | |
1769 | u32 cnt = cur->jmp_history_cnt; | |
1770 | struct bpf_idx_pair *p; | |
1771 | ||
1772 | cnt++; | |
1773 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
1774 | if (!p) | |
1775 | return -ENOMEM; | |
1776 | p[cnt - 1].idx = env->insn_idx; | |
1777 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
1778 | cur->jmp_history = p; | |
1779 | cur->jmp_history_cnt = cnt; | |
1780 | return 0; | |
1781 | } | |
1782 | ||
1783 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
1784 | * history then previous instruction came from straight line execution. | |
1785 | */ | |
1786 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
1787 | u32 *history) | |
1788 | { | |
1789 | u32 cnt = *history; | |
1790 | ||
1791 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
1792 | i = st->jmp_history[cnt - 1].prev_idx; | |
1793 | (*history)--; | |
1794 | } else { | |
1795 | i--; | |
1796 | } | |
1797 | return i; | |
1798 | } | |
1799 | ||
1800 | /* For given verifier state backtrack_insn() is called from the last insn to | |
1801 | * the first insn. Its purpose is to compute a bitmask of registers and | |
1802 | * stack slots that needs precision in the parent verifier state. | |
1803 | */ | |
1804 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
1805 | u32 *reg_mask, u64 *stack_mask) | |
1806 | { | |
1807 | const struct bpf_insn_cbs cbs = { | |
1808 | .cb_print = verbose, | |
1809 | .private_data = env, | |
1810 | }; | |
1811 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
1812 | u8 class = BPF_CLASS(insn->code); | |
1813 | u8 opcode = BPF_OP(insn->code); | |
1814 | u8 mode = BPF_MODE(insn->code); | |
1815 | u32 dreg = 1u << insn->dst_reg; | |
1816 | u32 sreg = 1u << insn->src_reg; | |
1817 | u32 spi; | |
1818 | ||
1819 | if (insn->code == 0) | |
1820 | return 0; | |
1821 | if (env->log.level & BPF_LOG_LEVEL) { | |
1822 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
1823 | verbose(env, "%d: ", idx); | |
1824 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
1825 | } | |
1826 | ||
1827 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1828 | if (!(*reg_mask & dreg)) | |
1829 | return 0; | |
1830 | if (opcode == BPF_MOV) { | |
1831 | if (BPF_SRC(insn->code) == BPF_X) { | |
1832 | /* dreg = sreg | |
1833 | * dreg needs precision after this insn | |
1834 | * sreg needs precision before this insn | |
1835 | */ | |
1836 | *reg_mask &= ~dreg; | |
1837 | *reg_mask |= sreg; | |
1838 | } else { | |
1839 | /* dreg = K | |
1840 | * dreg needs precision after this insn. | |
1841 | * Corresponding register is already marked | |
1842 | * as precise=true in this verifier state. | |
1843 | * No further markings in parent are necessary | |
1844 | */ | |
1845 | *reg_mask &= ~dreg; | |
1846 | } | |
1847 | } else { | |
1848 | if (BPF_SRC(insn->code) == BPF_X) { | |
1849 | /* dreg += sreg | |
1850 | * both dreg and sreg need precision | |
1851 | * before this insn | |
1852 | */ | |
1853 | *reg_mask |= sreg; | |
1854 | } /* else dreg += K | |
1855 | * dreg still needs precision before this insn | |
1856 | */ | |
1857 | } | |
1858 | } else if (class == BPF_LDX) { | |
1859 | if (!(*reg_mask & dreg)) | |
1860 | return 0; | |
1861 | *reg_mask &= ~dreg; | |
1862 | ||
1863 | /* scalars can only be spilled into stack w/o losing precision. | |
1864 | * Load from any other memory can be zero extended. | |
1865 | * The desire to keep that precision is already indicated | |
1866 | * by 'precise' mark in corresponding register of this state. | |
1867 | * No further tracking necessary. | |
1868 | */ | |
1869 | if (insn->src_reg != BPF_REG_FP) | |
1870 | return 0; | |
1871 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1872 | return 0; | |
1873 | ||
1874 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
1875 | * that [fp - off] slot contains scalar that needs to be | |
1876 | * tracked with precision | |
1877 | */ | |
1878 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1879 | if (spi >= 64) { | |
1880 | verbose(env, "BUG spi %d\n", spi); | |
1881 | WARN_ONCE(1, "verifier backtracking bug"); | |
1882 | return -EFAULT; | |
1883 | } | |
1884 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 1885 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 1886 | if (*reg_mask & dreg) |
b3b50f05 | 1887 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
1888 | * to access memory. It means backtracking |
1889 | * encountered a case of pointer subtraction. | |
1890 | */ | |
1891 | return -ENOTSUPP; | |
1892 | /* scalars can only be spilled into stack */ | |
1893 | if (insn->dst_reg != BPF_REG_FP) | |
1894 | return 0; | |
1895 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1896 | return 0; | |
1897 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1898 | if (spi >= 64) { | |
1899 | verbose(env, "BUG spi %d\n", spi); | |
1900 | WARN_ONCE(1, "verifier backtracking bug"); | |
1901 | return -EFAULT; | |
1902 | } | |
1903 | if (!(*stack_mask & (1ull << spi))) | |
1904 | return 0; | |
1905 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
1906 | if (class == BPF_STX) |
1907 | *reg_mask |= sreg; | |
b5dc0163 AS |
1908 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
1909 | if (opcode == BPF_CALL) { | |
1910 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1911 | return -ENOTSUPP; | |
1912 | /* regular helper call sets R0 */ | |
1913 | *reg_mask &= ~1; | |
1914 | if (*reg_mask & 0x3f) { | |
1915 | /* if backtracing was looking for registers R1-R5 | |
1916 | * they should have been found already. | |
1917 | */ | |
1918 | verbose(env, "BUG regs %x\n", *reg_mask); | |
1919 | WARN_ONCE(1, "verifier backtracking bug"); | |
1920 | return -EFAULT; | |
1921 | } | |
1922 | } else if (opcode == BPF_EXIT) { | |
1923 | return -ENOTSUPP; | |
1924 | } | |
1925 | } else if (class == BPF_LD) { | |
1926 | if (!(*reg_mask & dreg)) | |
1927 | return 0; | |
1928 | *reg_mask &= ~dreg; | |
1929 | /* It's ld_imm64 or ld_abs or ld_ind. | |
1930 | * For ld_imm64 no further tracking of precision | |
1931 | * into parent is necessary | |
1932 | */ | |
1933 | if (mode == BPF_IND || mode == BPF_ABS) | |
1934 | /* to be analyzed */ | |
1935 | return -ENOTSUPP; | |
b5dc0163 AS |
1936 | } |
1937 | return 0; | |
1938 | } | |
1939 | ||
1940 | /* the scalar precision tracking algorithm: | |
1941 | * . at the start all registers have precise=false. | |
1942 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
1943 | * . once precise value of the scalar register is used in: | |
1944 | * . ptr + scalar alu | |
1945 | * . if (scalar cond K|scalar) | |
1946 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
1947 | * backtrack through the verifier states and mark all registers and | |
1948 | * stack slots with spilled constants that these scalar regisers | |
1949 | * should be precise. | |
1950 | * . during state pruning two registers (or spilled stack slots) | |
1951 | * are equivalent if both are not precise. | |
1952 | * | |
1953 | * Note the verifier cannot simply walk register parentage chain, | |
1954 | * since many different registers and stack slots could have been | |
1955 | * used to compute single precise scalar. | |
1956 | * | |
1957 | * The approach of starting with precise=true for all registers and then | |
1958 | * backtrack to mark a register as not precise when the verifier detects | |
1959 | * that program doesn't care about specific value (e.g., when helper | |
1960 | * takes register as ARG_ANYTHING parameter) is not safe. | |
1961 | * | |
1962 | * It's ok to walk single parentage chain of the verifier states. | |
1963 | * It's possible that this backtracking will go all the way till 1st insn. | |
1964 | * All other branches will be explored for needing precision later. | |
1965 | * | |
1966 | * The backtracking needs to deal with cases like: | |
1967 | * 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) | |
1968 | * r9 -= r8 | |
1969 | * r5 = r9 | |
1970 | * if r5 > 0x79f goto pc+7 | |
1971 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
1972 | * r5 += 1 | |
1973 | * ... | |
1974 | * call bpf_perf_event_output#25 | |
1975 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
1976 | * | |
1977 | * and this case: | |
1978 | * r6 = 1 | |
1979 | * call foo // uses callee's r6 inside to compute r0 | |
1980 | * r0 += r6 | |
1981 | * if r0 == 0 goto | |
1982 | * | |
1983 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
1984 | * | |
1985 | * Also if parent's curframe > frame where backtracking started, | |
1986 | * the verifier need to mark registers in both frames, otherwise callees | |
1987 | * may incorrectly prune callers. This is similar to | |
1988 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
1989 | * | |
1990 | * For now backtracking falls back into conservative marking. | |
1991 | */ | |
1992 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
1993 | struct bpf_verifier_state *st) | |
1994 | { | |
1995 | struct bpf_func_state *func; | |
1996 | struct bpf_reg_state *reg; | |
1997 | int i, j; | |
1998 | ||
1999 | /* big hammer: mark all scalars precise in this path. | |
2000 | * pop_stack may still get !precise scalars. | |
2001 | */ | |
2002 | for (; st; st = st->parent) | |
2003 | for (i = 0; i <= st->curframe; i++) { | |
2004 | func = st->frame[i]; | |
2005 | for (j = 0; j < BPF_REG_FP; j++) { | |
2006 | reg = &func->regs[j]; | |
2007 | if (reg->type != SCALAR_VALUE) | |
2008 | continue; | |
2009 | reg->precise = true; | |
2010 | } | |
2011 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
2012 | if (func->stack[j].slot_type[0] != STACK_SPILL) | |
2013 | continue; | |
2014 | reg = &func->stack[j].spilled_ptr; | |
2015 | if (reg->type != SCALAR_VALUE) | |
2016 | continue; | |
2017 | reg->precise = true; | |
2018 | } | |
2019 | } | |
2020 | } | |
2021 | ||
a3ce685d AS |
2022 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
2023 | int spi) | |
b5dc0163 AS |
2024 | { |
2025 | struct bpf_verifier_state *st = env->cur_state; | |
2026 | int first_idx = st->first_insn_idx; | |
2027 | int last_idx = env->insn_idx; | |
2028 | struct bpf_func_state *func; | |
2029 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2030 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2031 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2032 | bool skip_first = true; |
a3ce685d | 2033 | bool new_marks = false; |
b5dc0163 AS |
2034 | int i, err; |
2035 | ||
2c78ee89 | 2036 | if (!env->bpf_capable) |
b5dc0163 AS |
2037 | return 0; |
2038 | ||
2039 | func = st->frame[st->curframe]; | |
a3ce685d AS |
2040 | if (regno >= 0) { |
2041 | reg = &func->regs[regno]; | |
2042 | if (reg->type != SCALAR_VALUE) { | |
2043 | WARN_ONCE(1, "backtracing misuse"); | |
2044 | return -EFAULT; | |
2045 | } | |
2046 | if (!reg->precise) | |
2047 | new_marks = true; | |
2048 | else | |
2049 | reg_mask = 0; | |
2050 | reg->precise = true; | |
b5dc0163 | 2051 | } |
b5dc0163 | 2052 | |
a3ce685d AS |
2053 | while (spi >= 0) { |
2054 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { | |
2055 | stack_mask = 0; | |
2056 | break; | |
2057 | } | |
2058 | reg = &func->stack[spi].spilled_ptr; | |
2059 | if (reg->type != SCALAR_VALUE) { | |
2060 | stack_mask = 0; | |
2061 | break; | |
2062 | } | |
2063 | if (!reg->precise) | |
2064 | new_marks = true; | |
2065 | else | |
2066 | stack_mask = 0; | |
2067 | reg->precise = true; | |
2068 | break; | |
2069 | } | |
2070 | ||
2071 | if (!new_marks) | |
2072 | return 0; | |
2073 | if (!reg_mask && !stack_mask) | |
2074 | return 0; | |
b5dc0163 AS |
2075 | for (;;) { |
2076 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2077 | u32 history = st->jmp_history_cnt; |
2078 | ||
2079 | if (env->log.level & BPF_LOG_LEVEL) | |
2080 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
2081 | for (i = last_idx;;) { | |
2082 | if (skip_first) { | |
2083 | err = 0; | |
2084 | skip_first = false; | |
2085 | } else { | |
2086 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2087 | } | |
2088 | if (err == -ENOTSUPP) { | |
2089 | mark_all_scalars_precise(env, st); | |
2090 | return 0; | |
2091 | } else if (err) { | |
2092 | return err; | |
2093 | } | |
2094 | if (!reg_mask && !stack_mask) | |
2095 | /* Found assignment(s) into tracked register in this state. | |
2096 | * Since this state is already marked, just return. | |
2097 | * Nothing to be tracked further in the parent state. | |
2098 | */ | |
2099 | return 0; | |
2100 | if (i == first_idx) | |
2101 | break; | |
2102 | i = get_prev_insn_idx(st, i, &history); | |
2103 | if (i >= env->prog->len) { | |
2104 | /* This can happen if backtracking reached insn 0 | |
2105 | * and there are still reg_mask or stack_mask | |
2106 | * to backtrack. | |
2107 | * It means the backtracking missed the spot where | |
2108 | * particular register was initialized with a constant. | |
2109 | */ | |
2110 | verbose(env, "BUG backtracking idx %d\n", i); | |
2111 | WARN_ONCE(1, "verifier backtracking bug"); | |
2112 | return -EFAULT; | |
2113 | } | |
2114 | } | |
2115 | st = st->parent; | |
2116 | if (!st) | |
2117 | break; | |
2118 | ||
a3ce685d | 2119 | new_marks = false; |
b5dc0163 AS |
2120 | func = st->frame[st->curframe]; |
2121 | bitmap_from_u64(mask, reg_mask); | |
2122 | for_each_set_bit(i, mask, 32) { | |
2123 | reg = &func->regs[i]; | |
a3ce685d AS |
2124 | if (reg->type != SCALAR_VALUE) { |
2125 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2126 | continue; |
a3ce685d | 2127 | } |
b5dc0163 AS |
2128 | if (!reg->precise) |
2129 | new_marks = true; | |
2130 | reg->precise = true; | |
2131 | } | |
2132 | ||
2133 | bitmap_from_u64(mask, stack_mask); | |
2134 | for_each_set_bit(i, mask, 64) { | |
2135 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2136 | /* the sequence of instructions: |
2137 | * 2: (bf) r3 = r10 | |
2138 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2139 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2140 | * doesn't contain jmps. It's backtracked | |
2141 | * as a single block. | |
2142 | * During backtracking insn 3 is not recognized as | |
2143 | * stack access, so at the end of backtracking | |
2144 | * stack slot fp-8 is still marked in stack_mask. | |
2145 | * However the parent state may not have accessed | |
2146 | * fp-8 and it's "unallocated" stack space. | |
2147 | * In such case fallback to conservative. | |
b5dc0163 | 2148 | */ |
2339cd6c AS |
2149 | mark_all_scalars_precise(env, st); |
2150 | return 0; | |
b5dc0163 AS |
2151 | } |
2152 | ||
a3ce685d AS |
2153 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
2154 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2155 | continue; |
a3ce685d | 2156 | } |
b5dc0163 | 2157 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2158 | if (reg->type != SCALAR_VALUE) { |
2159 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2160 | continue; |
a3ce685d | 2161 | } |
b5dc0163 AS |
2162 | if (!reg->precise) |
2163 | new_marks = true; | |
2164 | reg->precise = true; | |
2165 | } | |
2166 | if (env->log.level & BPF_LOG_LEVEL) { | |
2167 | print_verifier_state(env, func); | |
2168 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
2169 | new_marks ? "didn't have" : "already had", | |
2170 | reg_mask, stack_mask); | |
2171 | } | |
2172 | ||
a3ce685d AS |
2173 | if (!reg_mask && !stack_mask) |
2174 | break; | |
b5dc0163 AS |
2175 | if (!new_marks) |
2176 | break; | |
2177 | ||
2178 | last_idx = st->last_insn_idx; | |
2179 | first_idx = st->first_insn_idx; | |
2180 | } | |
2181 | return 0; | |
2182 | } | |
2183 | ||
a3ce685d AS |
2184 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
2185 | { | |
2186 | return __mark_chain_precision(env, regno, -1); | |
2187 | } | |
2188 | ||
2189 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2190 | { | |
2191 | return __mark_chain_precision(env, -1, spi); | |
2192 | } | |
b5dc0163 | 2193 | |
1be7f75d AS |
2194 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2195 | { | |
2196 | switch (type) { | |
2197 | case PTR_TO_MAP_VALUE: | |
2198 | case PTR_TO_MAP_VALUE_OR_NULL: | |
2199 | case PTR_TO_STACK: | |
2200 | case PTR_TO_CTX: | |
969bf05e | 2201 | case PTR_TO_PACKET: |
de8f3a83 | 2202 | case PTR_TO_PACKET_META: |
969bf05e | 2203 | case PTR_TO_PACKET_END: |
d58e468b | 2204 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2205 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
2206 | case PTR_TO_SOCKET: |
2207 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
2208 | case PTR_TO_SOCK_COMMON: |
2209 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
2210 | case PTR_TO_TCP_SOCK: |
2211 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 2212 | case PTR_TO_XDP_SOCK: |
65726b5b | 2213 | case PTR_TO_BTF_ID: |
b121b341 | 2214 | case PTR_TO_BTF_ID_OR_NULL: |
afbf21dc YS |
2215 | case PTR_TO_RDONLY_BUF: |
2216 | case PTR_TO_RDONLY_BUF_OR_NULL: | |
2217 | case PTR_TO_RDWR_BUF: | |
2218 | case PTR_TO_RDWR_BUF_OR_NULL: | |
eaa6bcb7 | 2219 | case PTR_TO_PERCPU_BTF_ID: |
1be7f75d AS |
2220 | return true; |
2221 | default: | |
2222 | return false; | |
2223 | } | |
2224 | } | |
2225 | ||
cc2b14d5 AS |
2226 | /* Does this register contain a constant zero? */ |
2227 | static bool register_is_null(struct bpf_reg_state *reg) | |
2228 | { | |
2229 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2230 | } | |
2231 | ||
f7cf25b2 AS |
2232 | static bool register_is_const(struct bpf_reg_state *reg) |
2233 | { | |
2234 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2235 | } | |
2236 | ||
5689d49b YS |
2237 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
2238 | { | |
2239 | return tnum_is_unknown(reg->var_off) && | |
2240 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && | |
2241 | reg->umin_value == 0 && reg->umax_value == U64_MAX && | |
2242 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && | |
2243 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; | |
2244 | } | |
2245 | ||
2246 | static bool register_is_bounded(struct bpf_reg_state *reg) | |
2247 | { | |
2248 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); | |
2249 | } | |
2250 | ||
6e7e63cb JH |
2251 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2252 | const struct bpf_reg_state *reg) | |
2253 | { | |
2254 | if (allow_ptr_leaks) | |
2255 | return false; | |
2256 | ||
2257 | return reg->type != SCALAR_VALUE; | |
2258 | } | |
2259 | ||
f7cf25b2 AS |
2260 | static void save_register_state(struct bpf_func_state *state, |
2261 | int spi, struct bpf_reg_state *reg) | |
2262 | { | |
2263 | int i; | |
2264 | ||
2265 | state->stack[spi].spilled_ptr = *reg; | |
2266 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2267 | ||
2268 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2269 | state->stack[spi].slot_type[i] = STACK_SPILL; | |
2270 | } | |
2271 | ||
17a52670 AS |
2272 | /* check_stack_read/write functions track spill/fill of registers, |
2273 | * stack boundary and alignment are checked in check_mem_access() | |
2274 | */ | |
61bd5218 | 2275 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 2276 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 2277 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 2278 | { |
f4d7e40a | 2279 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 2280 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 2281 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 2282 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 2283 | |
f4d7e40a | 2284 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 2285 | state->acquired_refs, true); |
638f5b90 AS |
2286 | if (err) |
2287 | return err; | |
9c399760 AS |
2288 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
2289 | * so it's aligned access and [off, off + size) are within stack limits | |
2290 | */ | |
638f5b90 AS |
2291 | if (!env->allow_ptr_leaks && |
2292 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
2293 | size != BPF_REG_SIZE) { | |
2294 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
2295 | return -EACCES; | |
2296 | } | |
17a52670 | 2297 | |
f4d7e40a | 2298 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
2299 | if (value_regno >= 0) |
2300 | reg = &cur->regs[value_regno]; | |
17a52670 | 2301 | |
5689d49b | 2302 | if (reg && size == BPF_REG_SIZE && register_is_bounded(reg) && |
2c78ee89 | 2303 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
2304 | if (dst_reg != BPF_REG_FP) { |
2305 | /* The backtracking logic can only recognize explicit | |
2306 | * stack slot address like [fp - 8]. Other spill of | |
2307 | * scalar via different register has to be conervative. | |
2308 | * Backtrack from here and mark all registers as precise | |
2309 | * that contributed into 'reg' being a constant. | |
2310 | */ | |
2311 | err = mark_chain_precision(env, value_regno); | |
2312 | if (err) | |
2313 | return err; | |
2314 | } | |
f7cf25b2 AS |
2315 | save_register_state(state, spi, reg); |
2316 | } else if (reg && is_spillable_regtype(reg->type)) { | |
17a52670 | 2317 | /* register containing pointer is being spilled into stack */ |
9c399760 | 2318 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 2319 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 2320 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
2321 | return -EACCES; |
2322 | } | |
2323 | ||
f7cf25b2 | 2324 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2325 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2326 | return -EINVAL; | |
2327 | } | |
2328 | ||
2c78ee89 | 2329 | if (!env->bypass_spec_v4) { |
f7cf25b2 | 2330 | bool sanitize = false; |
17a52670 | 2331 | |
f7cf25b2 AS |
2332 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
2333 | register_is_const(&state->stack[spi].spilled_ptr)) | |
2334 | sanitize = true; | |
2335 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2336 | if (state->stack[spi].slot_type[i] == STACK_MISC) { | |
2337 | sanitize = true; | |
2338 | break; | |
2339 | } | |
2340 | if (sanitize) { | |
af86ca4e AS |
2341 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
2342 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
2343 | ||
2344 | /* detected reuse of integer stack slot with a pointer | |
2345 | * which means either llvm is reusing stack slot or | |
2346 | * an attacker is trying to exploit CVE-2018-3639 | |
2347 | * (speculative store bypass) | |
2348 | * Have to sanitize that slot with preemptive | |
2349 | * store of zero. | |
2350 | */ | |
2351 | if (*poff && *poff != soff) { | |
2352 | /* disallow programs where single insn stores | |
2353 | * into two different stack slots, since verifier | |
2354 | * cannot sanitize them | |
2355 | */ | |
2356 | verbose(env, | |
2357 | "insn %d cannot access two stack slots fp%d and fp%d", | |
2358 | insn_idx, *poff, soff); | |
2359 | return -EINVAL; | |
2360 | } | |
2361 | *poff = soff; | |
2362 | } | |
af86ca4e | 2363 | } |
f7cf25b2 | 2364 | save_register_state(state, spi, reg); |
9c399760 | 2365 | } else { |
cc2b14d5 AS |
2366 | u8 type = STACK_MISC; |
2367 | ||
679c782d EC |
2368 | /* regular write of data into stack destroys any spilled ptr */ |
2369 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
2370 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
2371 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
2372 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2373 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 2374 | |
cc2b14d5 AS |
2375 | /* only mark the slot as written if all 8 bytes were written |
2376 | * otherwise read propagation may incorrectly stop too soon | |
2377 | * when stack slots are partially written. | |
2378 | * This heuristic means that read propagation will be | |
2379 | * conservative, since it will add reg_live_read marks | |
2380 | * to stack slots all the way to first state when programs | |
2381 | * writes+reads less than 8 bytes | |
2382 | */ | |
2383 | if (size == BPF_REG_SIZE) | |
2384 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2385 | ||
2386 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2387 | if (reg && register_is_null(reg)) { |
2388 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2389 | err = mark_chain_precision(env, value_regno); | |
2390 | if (err) | |
2391 | return err; | |
cc2b14d5 | 2392 | type = STACK_ZERO; |
b5dc0163 | 2393 | } |
cc2b14d5 | 2394 | |
0bae2d4d | 2395 | /* Mark slots affected by this stack write. */ |
9c399760 | 2396 | for (i = 0; i < size; i++) |
638f5b90 | 2397 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2398 | type; |
17a52670 AS |
2399 | } |
2400 | return 0; | |
2401 | } | |
2402 | ||
61bd5218 | 2403 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
2404 | struct bpf_func_state *reg_state /* func where register points to */, |
2405 | int off, int size, int value_regno) | |
17a52670 | 2406 | { |
f4d7e40a AS |
2407 | struct bpf_verifier_state *vstate = env->cur_state; |
2408 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 2409 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 2410 | struct bpf_reg_state *reg; |
638f5b90 | 2411 | u8 *stype; |
17a52670 | 2412 | |
f4d7e40a | 2413 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
2414 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
2415 | off, size); | |
2416 | return -EACCES; | |
2417 | } | |
f4d7e40a | 2418 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 2419 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 2420 | |
638f5b90 | 2421 | if (stype[0] == STACK_SPILL) { |
9c399760 | 2422 | if (size != BPF_REG_SIZE) { |
f7cf25b2 AS |
2423 | if (reg->type != SCALAR_VALUE) { |
2424 | verbose_linfo(env, env->insn_idx, "; "); | |
2425 | verbose(env, "invalid size of register fill\n"); | |
2426 | return -EACCES; | |
2427 | } | |
2428 | if (value_regno >= 0) { | |
2429 | mark_reg_unknown(env, state->regs, value_regno); | |
2430 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
2431 | } | |
2432 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
2433 | return 0; | |
17a52670 | 2434 | } |
9c399760 | 2435 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 2436 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 2437 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
2438 | return -EACCES; |
2439 | } | |
2440 | } | |
2441 | ||
dc503a8a | 2442 | if (value_regno >= 0) { |
17a52670 | 2443 | /* restore register state from stack */ |
f7cf25b2 | 2444 | state->regs[value_regno] = *reg; |
2f18f62e AS |
2445 | /* mark reg as written since spilled pointer state likely |
2446 | * has its liveness marks cleared by is_state_visited() | |
2447 | * which resets stack/reg liveness for state transitions | |
2448 | */ | |
2449 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
6e7e63cb JH |
2450 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
2451 | /* If value_regno==-1, the caller is asking us whether | |
2452 | * it is acceptable to use this value as a SCALAR_VALUE | |
2453 | * (e.g. for XADD). | |
2454 | * We must not allow unprivileged callers to do that | |
2455 | * with spilled pointers. | |
2456 | */ | |
2457 | verbose(env, "leaking pointer from stack off %d\n", | |
2458 | off); | |
2459 | return -EACCES; | |
dc503a8a | 2460 | } |
f7cf25b2 | 2461 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 | 2462 | } else { |
cc2b14d5 AS |
2463 | int zeros = 0; |
2464 | ||
17a52670 | 2465 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
2466 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
2467 | continue; | |
2468 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
2469 | zeros++; | |
2470 | continue; | |
17a52670 | 2471 | } |
cc2b14d5 AS |
2472 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
2473 | off, i, size); | |
2474 | return -EACCES; | |
2475 | } | |
f7cf25b2 | 2476 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
cc2b14d5 AS |
2477 | if (value_regno >= 0) { |
2478 | if (zeros == size) { | |
2479 | /* any size read into register is zero extended, | |
2480 | * so the whole register == const_zero | |
2481 | */ | |
2482 | __mark_reg_const_zero(&state->regs[value_regno]); | |
b5dc0163 AS |
2483 | /* backtracking doesn't support STACK_ZERO yet, |
2484 | * so mark it precise here, so that later | |
2485 | * backtracking can stop here. | |
2486 | * Backtracking may not need this if this register | |
2487 | * doesn't participate in pointer adjustment. | |
2488 | * Forward propagation of precise flag is not | |
2489 | * necessary either. This mark is only to stop | |
2490 | * backtracking. Any register that contributed | |
2491 | * to const 0 was marked precise before spill. | |
2492 | */ | |
2493 | state->regs[value_regno].precise = true; | |
cc2b14d5 AS |
2494 | } else { |
2495 | /* have read misc data from the stack */ | |
2496 | mark_reg_unknown(env, state->regs, value_regno); | |
2497 | } | |
2498 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 2499 | } |
17a52670 | 2500 | } |
f7cf25b2 | 2501 | return 0; |
17a52670 AS |
2502 | } |
2503 | ||
e4298d25 DB |
2504 | static int check_stack_access(struct bpf_verifier_env *env, |
2505 | const struct bpf_reg_state *reg, | |
2506 | int off, int size) | |
2507 | { | |
2508 | /* Stack accesses must be at a fixed offset, so that we | |
2509 | * can determine what type of data were returned. See | |
2510 | * check_stack_read(). | |
2511 | */ | |
2512 | if (!tnum_is_const(reg->var_off)) { | |
2513 | char tn_buf[48]; | |
2514 | ||
2515 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 2516 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
2517 | tn_buf, off, size); |
2518 | return -EACCES; | |
2519 | } | |
2520 | ||
2521 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
2522 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
2523 | return -EACCES; | |
2524 | } | |
2525 | ||
2526 | return 0; | |
2527 | } | |
2528 | ||
591fe988 DB |
2529 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
2530 | int off, int size, enum bpf_access_type type) | |
2531 | { | |
2532 | struct bpf_reg_state *regs = cur_regs(env); | |
2533 | struct bpf_map *map = regs[regno].map_ptr; | |
2534 | u32 cap = bpf_map_flags_to_cap(map); | |
2535 | ||
2536 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
2537 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
2538 | map->value_size, off, size); | |
2539 | return -EACCES; | |
2540 | } | |
2541 | ||
2542 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
2543 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
2544 | map->value_size, off, size); | |
2545 | return -EACCES; | |
2546 | } | |
2547 | ||
2548 | return 0; | |
2549 | } | |
2550 | ||
457f4436 AN |
2551 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
2552 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
2553 | int off, int size, u32 mem_size, | |
2554 | bool zero_size_allowed) | |
17a52670 | 2555 | { |
457f4436 AN |
2556 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
2557 | struct bpf_reg_state *reg; | |
2558 | ||
2559 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
2560 | return 0; | |
17a52670 | 2561 | |
457f4436 AN |
2562 | reg = &cur_regs(env)[regno]; |
2563 | switch (reg->type) { | |
2564 | case PTR_TO_MAP_VALUE: | |
61bd5218 | 2565 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
2566 | mem_size, off, size); |
2567 | break; | |
2568 | case PTR_TO_PACKET: | |
2569 | case PTR_TO_PACKET_META: | |
2570 | case PTR_TO_PACKET_END: | |
2571 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
2572 | off, size, regno, reg->id, off, mem_size); | |
2573 | break; | |
2574 | case PTR_TO_MEM: | |
2575 | default: | |
2576 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
2577 | mem_size, off, size); | |
17a52670 | 2578 | } |
457f4436 AN |
2579 | |
2580 | return -EACCES; | |
17a52670 AS |
2581 | } |
2582 | ||
457f4436 AN |
2583 | /* check read/write into a memory region with possible variable offset */ |
2584 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
2585 | int off, int size, u32 mem_size, | |
2586 | bool zero_size_allowed) | |
dbcfe5f7 | 2587 | { |
f4d7e40a AS |
2588 | struct bpf_verifier_state *vstate = env->cur_state; |
2589 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
2590 | struct bpf_reg_state *reg = &state->regs[regno]; |
2591 | int err; | |
2592 | ||
457f4436 | 2593 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
2594 | * need to try adding each of min_value and max_value to off |
2595 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 2596 | */ |
06ee7115 | 2597 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 2598 | print_verifier_state(env, state); |
b7137c4e | 2599 | |
dbcfe5f7 GB |
2600 | /* The minimum value is only important with signed |
2601 | * comparisons where we can't assume the floor of a | |
2602 | * value is 0. If we are using signed variables for our | |
2603 | * index'es we need to make sure that whatever we use | |
2604 | * will have a set floor within our range. | |
2605 | */ | |
b7137c4e DB |
2606 | if (reg->smin_value < 0 && |
2607 | (reg->smin_value == S64_MIN || | |
2608 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
2609 | reg->smin_value + off < 0)) { | |
61bd5218 | 2610 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
2611 | regno); |
2612 | return -EACCES; | |
2613 | } | |
457f4436 AN |
2614 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
2615 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 2616 | if (err) { |
457f4436 | 2617 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 2618 | regno); |
dbcfe5f7 GB |
2619 | return err; |
2620 | } | |
2621 | ||
b03c9f9f EC |
2622 | /* If we haven't set a max value then we need to bail since we can't be |
2623 | * sure we won't do bad things. | |
2624 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 2625 | */ |
b03c9f9f | 2626 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 2627 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
2628 | regno); |
2629 | return -EACCES; | |
2630 | } | |
457f4436 AN |
2631 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
2632 | mem_size, zero_size_allowed); | |
2633 | if (err) { | |
2634 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 2635 | regno); |
457f4436 AN |
2636 | return err; |
2637 | } | |
2638 | ||
2639 | return 0; | |
2640 | } | |
d83525ca | 2641 | |
457f4436 AN |
2642 | /* check read/write into a map element with possible variable offset */ |
2643 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
2644 | int off, int size, bool zero_size_allowed) | |
2645 | { | |
2646 | struct bpf_verifier_state *vstate = env->cur_state; | |
2647 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
2648 | struct bpf_reg_state *reg = &state->regs[regno]; | |
2649 | struct bpf_map *map = reg->map_ptr; | |
2650 | int err; | |
2651 | ||
2652 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
2653 | zero_size_allowed); | |
2654 | if (err) | |
2655 | return err; | |
2656 | ||
2657 | if (map_value_has_spin_lock(map)) { | |
2658 | u32 lock = map->spin_lock_off; | |
d83525ca AS |
2659 | |
2660 | /* if any part of struct bpf_spin_lock can be touched by | |
2661 | * load/store reject this program. | |
2662 | * To check that [x1, x2) overlaps with [y1, y2) | |
2663 | * it is sufficient to check x1 < y2 && y1 < x2. | |
2664 | */ | |
2665 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
2666 | lock < reg->umax_value + off + size) { | |
2667 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
2668 | return -EACCES; | |
2669 | } | |
2670 | } | |
f1174f77 | 2671 | return err; |
dbcfe5f7 GB |
2672 | } |
2673 | ||
969bf05e AS |
2674 | #define MAX_PACKET_OFF 0xffff |
2675 | ||
7e40781c UP |
2676 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
2677 | { | |
3aac1ead | 2678 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
7e40781c UP |
2679 | } |
2680 | ||
58e2af8b | 2681 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
2682 | const struct bpf_call_arg_meta *meta, |
2683 | enum bpf_access_type t) | |
4acf6c0b | 2684 | { |
7e40781c UP |
2685 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
2686 | ||
2687 | switch (prog_type) { | |
5d66fa7d | 2688 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
2689 | case BPF_PROG_TYPE_LWT_IN: |
2690 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 2691 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 2692 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 2693 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 2694 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
2695 | if (t == BPF_WRITE) |
2696 | return false; | |
8731745e | 2697 | fallthrough; |
5d66fa7d DB |
2698 | |
2699 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
2700 | case BPF_PROG_TYPE_SCHED_CLS: |
2701 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 2702 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 2703 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 2704 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 2705 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
2706 | if (meta) |
2707 | return meta->pkt_access; | |
2708 | ||
2709 | env->seen_direct_write = true; | |
4acf6c0b | 2710 | return true; |
0d01da6a SF |
2711 | |
2712 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2713 | if (t == BPF_WRITE) | |
2714 | env->seen_direct_write = true; | |
2715 | ||
2716 | return true; | |
2717 | ||
4acf6c0b BB |
2718 | default: |
2719 | return false; | |
2720 | } | |
2721 | } | |
2722 | ||
f1174f77 | 2723 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2724 | int size, bool zero_size_allowed) |
f1174f77 | 2725 | { |
638f5b90 | 2726 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
2727 | struct bpf_reg_state *reg = ®s[regno]; |
2728 | int err; | |
2729 | ||
2730 | /* We may have added a variable offset to the packet pointer; but any | |
2731 | * reg->range we have comes after that. We are only checking the fixed | |
2732 | * offset. | |
2733 | */ | |
2734 | ||
2735 | /* We don't allow negative numbers, because we aren't tracking enough | |
2736 | * detail to prove they're safe. | |
2737 | */ | |
b03c9f9f | 2738 | if (reg->smin_value < 0) { |
61bd5218 | 2739 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
2740 | regno); |
2741 | return -EACCES; | |
2742 | } | |
6d94e741 AS |
2743 | |
2744 | err = reg->range < 0 ? -EINVAL : | |
2745 | __check_mem_access(env, regno, off, size, reg->range, | |
457f4436 | 2746 | zero_size_allowed); |
f1174f77 | 2747 | if (err) { |
61bd5218 | 2748 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
2749 | return err; |
2750 | } | |
e647815a | 2751 | |
457f4436 | 2752 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
2753 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
2754 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 2755 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
2756 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
2757 | */ | |
2758 | env->prog->aux->max_pkt_offset = | |
2759 | max_t(u32, env->prog->aux->max_pkt_offset, | |
2760 | off + reg->umax_value + size - 1); | |
2761 | ||
f1174f77 EC |
2762 | return err; |
2763 | } | |
2764 | ||
2765 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 2766 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 | 2767 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
22dc4a0f | 2768 | struct btf **btf, u32 *btf_id) |
17a52670 | 2769 | { |
f96da094 DB |
2770 | struct bpf_insn_access_aux info = { |
2771 | .reg_type = *reg_type, | |
9e15db66 | 2772 | .log = &env->log, |
f96da094 | 2773 | }; |
31fd8581 | 2774 | |
4f9218aa | 2775 | if (env->ops->is_valid_access && |
5e43f899 | 2776 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
2777 | /* A non zero info.ctx_field_size indicates that this field is a |
2778 | * candidate for later verifier transformation to load the whole | |
2779 | * field and then apply a mask when accessed with a narrower | |
2780 | * access than actual ctx access size. A zero info.ctx_field_size | |
2781 | * will only allow for whole field access and rejects any other | |
2782 | * type of narrower access. | |
31fd8581 | 2783 | */ |
23994631 | 2784 | *reg_type = info.reg_type; |
31fd8581 | 2785 | |
22dc4a0f AN |
2786 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) { |
2787 | *btf = info.btf; | |
9e15db66 | 2788 | *btf_id = info.btf_id; |
22dc4a0f | 2789 | } else { |
9e15db66 | 2790 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
22dc4a0f | 2791 | } |
32bbe007 AS |
2792 | /* remember the offset of last byte accessed in ctx */ |
2793 | if (env->prog->aux->max_ctx_offset < off + size) | |
2794 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 2795 | return 0; |
32bbe007 | 2796 | } |
17a52670 | 2797 | |
61bd5218 | 2798 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
2799 | return -EACCES; |
2800 | } | |
2801 | ||
d58e468b PP |
2802 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
2803 | int size) | |
2804 | { | |
2805 | if (size < 0 || off < 0 || | |
2806 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
2807 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
2808 | off, size); | |
2809 | return -EACCES; | |
2810 | } | |
2811 | return 0; | |
2812 | } | |
2813 | ||
5f456649 MKL |
2814 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
2815 | u32 regno, int off, int size, | |
2816 | enum bpf_access_type t) | |
c64b7983 JS |
2817 | { |
2818 | struct bpf_reg_state *regs = cur_regs(env); | |
2819 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 2820 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 2821 | bool valid; |
c64b7983 JS |
2822 | |
2823 | if (reg->smin_value < 0) { | |
2824 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
2825 | regno); | |
2826 | return -EACCES; | |
2827 | } | |
2828 | ||
46f8bc92 MKL |
2829 | switch (reg->type) { |
2830 | case PTR_TO_SOCK_COMMON: | |
2831 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
2832 | break; | |
2833 | case PTR_TO_SOCKET: | |
2834 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
2835 | break; | |
655a51e5 MKL |
2836 | case PTR_TO_TCP_SOCK: |
2837 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
2838 | break; | |
fada7fdc JL |
2839 | case PTR_TO_XDP_SOCK: |
2840 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
2841 | break; | |
46f8bc92 MKL |
2842 | default: |
2843 | valid = false; | |
c64b7983 JS |
2844 | } |
2845 | ||
5f456649 | 2846 | |
46f8bc92 MKL |
2847 | if (valid) { |
2848 | env->insn_aux_data[insn_idx].ctx_field_size = | |
2849 | info.ctx_field_size; | |
2850 | return 0; | |
2851 | } | |
2852 | ||
2853 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
2854 | regno, reg_type_str[reg->type], off, size); | |
2855 | ||
2856 | return -EACCES; | |
c64b7983 JS |
2857 | } |
2858 | ||
2a159c6f DB |
2859 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
2860 | { | |
2861 | return cur_regs(env) + regno; | |
2862 | } | |
2863 | ||
4cabc5b1 DB |
2864 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
2865 | { | |
2a159c6f | 2866 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
2867 | } |
2868 | ||
f37a8cb8 DB |
2869 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
2870 | { | |
2a159c6f | 2871 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 2872 | |
46f8bc92 MKL |
2873 | return reg->type == PTR_TO_CTX; |
2874 | } | |
2875 | ||
2876 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
2877 | { | |
2878 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2879 | ||
2880 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
2881 | } |
2882 | ||
ca369602 DB |
2883 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
2884 | { | |
2a159c6f | 2885 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
2886 | |
2887 | return type_is_pkt_pointer(reg->type); | |
2888 | } | |
2889 | ||
4b5defde DB |
2890 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
2891 | { | |
2892 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2893 | ||
2894 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
2895 | return reg->type == PTR_TO_FLOW_KEYS; | |
2896 | } | |
2897 | ||
61bd5218 JK |
2898 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
2899 | const struct bpf_reg_state *reg, | |
d1174416 | 2900 | int off, int size, bool strict) |
969bf05e | 2901 | { |
f1174f77 | 2902 | struct tnum reg_off; |
e07b98d9 | 2903 | int ip_align; |
d1174416 DM |
2904 | |
2905 | /* Byte size accesses are always allowed. */ | |
2906 | if (!strict || size == 1) | |
2907 | return 0; | |
2908 | ||
e4eda884 DM |
2909 | /* For platforms that do not have a Kconfig enabling |
2910 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
2911 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
2912 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
2913 | * to this code only in strict mode where we want to emulate | |
2914 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
2915 | * unconditional IP align value of '2'. | |
e07b98d9 | 2916 | */ |
e4eda884 | 2917 | ip_align = 2; |
f1174f77 EC |
2918 | |
2919 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
2920 | if (!tnum_is_aligned(reg_off, size)) { | |
2921 | char tn_buf[48]; | |
2922 | ||
2923 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
2924 | verbose(env, |
2925 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 2926 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
2927 | return -EACCES; |
2928 | } | |
79adffcd | 2929 | |
969bf05e AS |
2930 | return 0; |
2931 | } | |
2932 | ||
61bd5218 JK |
2933 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
2934 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
2935 | const char *pointer_desc, |
2936 | int off, int size, bool strict) | |
79adffcd | 2937 | { |
f1174f77 EC |
2938 | struct tnum reg_off; |
2939 | ||
2940 | /* Byte size accesses are always allowed. */ | |
2941 | if (!strict || size == 1) | |
2942 | return 0; | |
2943 | ||
2944 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
2945 | if (!tnum_is_aligned(reg_off, size)) { | |
2946 | char tn_buf[48]; | |
2947 | ||
2948 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 2949 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 2950 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
2951 | return -EACCES; |
2952 | } | |
2953 | ||
969bf05e AS |
2954 | return 0; |
2955 | } | |
2956 | ||
e07b98d9 | 2957 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
2958 | const struct bpf_reg_state *reg, int off, |
2959 | int size, bool strict_alignment_once) | |
79adffcd | 2960 | { |
ca369602 | 2961 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 2962 | const char *pointer_desc = ""; |
d1174416 | 2963 | |
79adffcd DB |
2964 | switch (reg->type) { |
2965 | case PTR_TO_PACKET: | |
de8f3a83 DB |
2966 | case PTR_TO_PACKET_META: |
2967 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
2968 | * right in front, treat it the very same way. | |
2969 | */ | |
61bd5218 | 2970 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
2971 | case PTR_TO_FLOW_KEYS: |
2972 | pointer_desc = "flow keys "; | |
2973 | break; | |
f1174f77 EC |
2974 | case PTR_TO_MAP_VALUE: |
2975 | pointer_desc = "value "; | |
2976 | break; | |
2977 | case PTR_TO_CTX: | |
2978 | pointer_desc = "context "; | |
2979 | break; | |
2980 | case PTR_TO_STACK: | |
2981 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
2982 | /* The stack spill tracking logic in check_stack_write() |
2983 | * and check_stack_read() relies on stack accesses being | |
2984 | * aligned. | |
2985 | */ | |
2986 | strict = true; | |
f1174f77 | 2987 | break; |
c64b7983 JS |
2988 | case PTR_TO_SOCKET: |
2989 | pointer_desc = "sock "; | |
2990 | break; | |
46f8bc92 MKL |
2991 | case PTR_TO_SOCK_COMMON: |
2992 | pointer_desc = "sock_common "; | |
2993 | break; | |
655a51e5 MKL |
2994 | case PTR_TO_TCP_SOCK: |
2995 | pointer_desc = "tcp_sock "; | |
2996 | break; | |
fada7fdc JL |
2997 | case PTR_TO_XDP_SOCK: |
2998 | pointer_desc = "xdp_sock "; | |
2999 | break; | |
79adffcd | 3000 | default: |
f1174f77 | 3001 | break; |
79adffcd | 3002 | } |
61bd5218 JK |
3003 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
3004 | strict); | |
79adffcd DB |
3005 | } |
3006 | ||
f4d7e40a AS |
3007 | static int update_stack_depth(struct bpf_verifier_env *env, |
3008 | const struct bpf_func_state *func, | |
3009 | int off) | |
3010 | { | |
9c8105bd | 3011 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
3012 | |
3013 | if (stack >= -off) | |
3014 | return 0; | |
3015 | ||
3016 | /* update known max for given subprogram */ | |
9c8105bd | 3017 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
3018 | return 0; |
3019 | } | |
f4d7e40a | 3020 | |
70a87ffe AS |
3021 | /* starting from main bpf function walk all instructions of the function |
3022 | * and recursively walk all callees that given function can call. | |
3023 | * Ignore jump and exit insns. | |
3024 | * Since recursion is prevented by check_cfg() this algorithm | |
3025 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
3026 | */ | |
3027 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
3028 | { | |
9c8105bd JW |
3029 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
3030 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 3031 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 3032 | bool tail_call_reachable = false; |
70a87ffe AS |
3033 | int ret_insn[MAX_CALL_FRAMES]; |
3034 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 3035 | int j; |
f4d7e40a | 3036 | |
70a87ffe | 3037 | process_func: |
7f6e4312 MF |
3038 | /* protect against potential stack overflow that might happen when |
3039 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
3040 | * depth for such case down to 256 so that the worst case scenario | |
3041 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
3042 | * 8k). | |
3043 | * | |
3044 | * To get the idea what might happen, see an example: | |
3045 | * func1 -> sub rsp, 128 | |
3046 | * subfunc1 -> sub rsp, 256 | |
3047 | * tailcall1 -> add rsp, 256 | |
3048 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
3049 | * subfunc2 -> sub rsp, 64 | |
3050 | * subfunc22 -> sub rsp, 128 | |
3051 | * tailcall2 -> add rsp, 128 | |
3052 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
3053 | * | |
3054 | * tailcall will unwind the current stack frame but it will not get rid | |
3055 | * of caller's stack as shown on the example above. | |
3056 | */ | |
3057 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
3058 | verbose(env, | |
3059 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
3060 | depth); | |
3061 | return -EACCES; | |
3062 | } | |
70a87ffe AS |
3063 | /* round up to 32-bytes, since this is granularity |
3064 | * of interpreter stack size | |
3065 | */ | |
9c8105bd | 3066 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 3067 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 3068 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 3069 | frame + 1, depth); |
f4d7e40a AS |
3070 | return -EACCES; |
3071 | } | |
70a87ffe | 3072 | continue_func: |
4cb3d99c | 3073 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
3074 | for (; i < subprog_end; i++) { |
3075 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
3076 | continue; | |
3077 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
3078 | continue; | |
3079 | /* remember insn and function to return to */ | |
3080 | ret_insn[frame] = i + 1; | |
9c8105bd | 3081 | ret_prog[frame] = idx; |
70a87ffe AS |
3082 | |
3083 | /* find the callee */ | |
3084 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
3085 | idx = find_subprog(env, i); |
3086 | if (idx < 0) { | |
70a87ffe AS |
3087 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
3088 | i); | |
3089 | return -EFAULT; | |
3090 | } | |
ebf7d1f5 MF |
3091 | |
3092 | if (subprog[idx].has_tail_call) | |
3093 | tail_call_reachable = true; | |
3094 | ||
70a87ffe AS |
3095 | frame++; |
3096 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
3097 | verbose(env, "the call stack of %d frames is too deep !\n", |
3098 | frame); | |
3099 | return -E2BIG; | |
70a87ffe AS |
3100 | } |
3101 | goto process_func; | |
3102 | } | |
ebf7d1f5 MF |
3103 | /* if tail call got detected across bpf2bpf calls then mark each of the |
3104 | * currently present subprog frames as tail call reachable subprogs; | |
3105 | * this info will be utilized by JIT so that we will be preserving the | |
3106 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
3107 | */ | |
3108 | if (tail_call_reachable) | |
3109 | for (j = 0; j < frame; j++) | |
3110 | subprog[ret_prog[j]].tail_call_reachable = true; | |
3111 | ||
70a87ffe AS |
3112 | /* end of for() loop means the last insn of the 'subprog' |
3113 | * was reached. Doesn't matter whether it was JA or EXIT | |
3114 | */ | |
3115 | if (frame == 0) | |
3116 | return 0; | |
9c8105bd | 3117 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
3118 | frame--; |
3119 | i = ret_insn[frame]; | |
9c8105bd | 3120 | idx = ret_prog[frame]; |
70a87ffe | 3121 | goto continue_func; |
f4d7e40a AS |
3122 | } |
3123 | ||
19d28fbd | 3124 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
3125 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
3126 | const struct bpf_insn *insn, int idx) | |
3127 | { | |
3128 | int start = idx + insn->imm + 1, subprog; | |
3129 | ||
3130 | subprog = find_subprog(env, start); | |
3131 | if (subprog < 0) { | |
3132 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
3133 | start); | |
3134 | return -EFAULT; | |
3135 | } | |
9c8105bd | 3136 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 3137 | } |
19d28fbd | 3138 | #endif |
1ea47e01 | 3139 | |
51c39bb1 AS |
3140 | int check_ctx_reg(struct bpf_verifier_env *env, |
3141 | const struct bpf_reg_state *reg, int regno) | |
58990d1f DB |
3142 | { |
3143 | /* Access to ctx or passing it to a helper is only allowed in | |
3144 | * its original, unmodified form. | |
3145 | */ | |
3146 | ||
3147 | if (reg->off) { | |
3148 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
3149 | regno, reg->off); | |
3150 | return -EACCES; | |
3151 | } | |
3152 | ||
3153 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3154 | char tn_buf[48]; | |
3155 | ||
3156 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3157 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
3158 | return -EACCES; | |
3159 | } | |
3160 | ||
3161 | return 0; | |
3162 | } | |
3163 | ||
afbf21dc YS |
3164 | static int __check_buffer_access(struct bpf_verifier_env *env, |
3165 | const char *buf_info, | |
3166 | const struct bpf_reg_state *reg, | |
3167 | int regno, int off, int size) | |
9df1c28b MM |
3168 | { |
3169 | if (off < 0) { | |
3170 | verbose(env, | |
4fc00b79 | 3171 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 3172 | regno, buf_info, off, size); |
9df1c28b MM |
3173 | return -EACCES; |
3174 | } | |
3175 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3176 | char tn_buf[48]; | |
3177 | ||
3178 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3179 | verbose(env, | |
4fc00b79 | 3180 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
3181 | regno, off, tn_buf); |
3182 | return -EACCES; | |
3183 | } | |
afbf21dc YS |
3184 | |
3185 | return 0; | |
3186 | } | |
3187 | ||
3188 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
3189 | const struct bpf_reg_state *reg, | |
3190 | int regno, int off, int size) | |
3191 | { | |
3192 | int err; | |
3193 | ||
3194 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
3195 | if (err) | |
3196 | return err; | |
3197 | ||
9df1c28b MM |
3198 | if (off + size > env->prog->aux->max_tp_access) |
3199 | env->prog->aux->max_tp_access = off + size; | |
3200 | ||
3201 | return 0; | |
3202 | } | |
3203 | ||
afbf21dc YS |
3204 | static int check_buffer_access(struct bpf_verifier_env *env, |
3205 | const struct bpf_reg_state *reg, | |
3206 | int regno, int off, int size, | |
3207 | bool zero_size_allowed, | |
3208 | const char *buf_info, | |
3209 | u32 *max_access) | |
3210 | { | |
3211 | int err; | |
3212 | ||
3213 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
3214 | if (err) | |
3215 | return err; | |
3216 | ||
3217 | if (off + size > *max_access) | |
3218 | *max_access = off + size; | |
3219 | ||
3220 | return 0; | |
3221 | } | |
3222 | ||
3f50f132 JF |
3223 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
3224 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
3225 | { | |
3226 | reg->var_off = tnum_subreg(reg->var_off); | |
3227 | __reg_assign_32_into_64(reg); | |
3228 | } | |
9df1c28b | 3229 | |
0c17d1d2 JH |
3230 | /* truncate register to smaller size (in bytes) |
3231 | * must be called with size < BPF_REG_SIZE | |
3232 | */ | |
3233 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
3234 | { | |
3235 | u64 mask; | |
3236 | ||
3237 | /* clear high bits in bit representation */ | |
3238 | reg->var_off = tnum_cast(reg->var_off, size); | |
3239 | ||
3240 | /* fix arithmetic bounds */ | |
3241 | mask = ((u64)1 << (size * 8)) - 1; | |
3242 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
3243 | reg->umin_value &= mask; | |
3244 | reg->umax_value &= mask; | |
3245 | } else { | |
3246 | reg->umin_value = 0; | |
3247 | reg->umax_value = mask; | |
3248 | } | |
3249 | reg->smin_value = reg->umin_value; | |
3250 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
3251 | |
3252 | /* If size is smaller than 32bit register the 32bit register | |
3253 | * values are also truncated so we push 64-bit bounds into | |
3254 | * 32-bit bounds. Above were truncated < 32-bits already. | |
3255 | */ | |
3256 | if (size >= 4) | |
3257 | return; | |
3258 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
3259 | } |
3260 | ||
a23740ec AN |
3261 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
3262 | { | |
3263 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; | |
3264 | } | |
3265 | ||
3266 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
3267 | { | |
3268 | void *ptr; | |
3269 | u64 addr; | |
3270 | int err; | |
3271 | ||
3272 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
3273 | if (err) | |
3274 | return err; | |
2dedd7d2 | 3275 | ptr = (void *)(long)addr + off; |
a23740ec AN |
3276 | |
3277 | switch (size) { | |
3278 | case sizeof(u8): | |
3279 | *val = (u64)*(u8 *)ptr; | |
3280 | break; | |
3281 | case sizeof(u16): | |
3282 | *val = (u64)*(u16 *)ptr; | |
3283 | break; | |
3284 | case sizeof(u32): | |
3285 | *val = (u64)*(u32 *)ptr; | |
3286 | break; | |
3287 | case sizeof(u64): | |
3288 | *val = *(u64 *)ptr; | |
3289 | break; | |
3290 | default: | |
3291 | return -EINVAL; | |
3292 | } | |
3293 | return 0; | |
3294 | } | |
3295 | ||
9e15db66 AS |
3296 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
3297 | struct bpf_reg_state *regs, | |
3298 | int regno, int off, int size, | |
3299 | enum bpf_access_type atype, | |
3300 | int value_regno) | |
3301 | { | |
3302 | struct bpf_reg_state *reg = regs + regno; | |
22dc4a0f AN |
3303 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
3304 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); | |
9e15db66 AS |
3305 | u32 btf_id; |
3306 | int ret; | |
3307 | ||
9e15db66 AS |
3308 | if (off < 0) { |
3309 | verbose(env, | |
3310 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
3311 | regno, tname, off); | |
3312 | return -EACCES; | |
3313 | } | |
3314 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3315 | char tn_buf[48]; | |
3316 | ||
3317 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3318 | verbose(env, | |
3319 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
3320 | regno, tname, off, tn_buf); | |
3321 | return -EACCES; | |
3322 | } | |
3323 | ||
27ae7997 | 3324 | if (env->ops->btf_struct_access) { |
22dc4a0f AN |
3325 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
3326 | off, size, atype, &btf_id); | |
27ae7997 MKL |
3327 | } else { |
3328 | if (atype != BPF_READ) { | |
3329 | verbose(env, "only read is supported\n"); | |
3330 | return -EACCES; | |
3331 | } | |
3332 | ||
22dc4a0f AN |
3333 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
3334 | atype, &btf_id); | |
27ae7997 MKL |
3335 | } |
3336 | ||
9e15db66 AS |
3337 | if (ret < 0) |
3338 | return ret; | |
3339 | ||
41c48f3a | 3340 | if (atype == BPF_READ && value_regno >= 0) |
22dc4a0f | 3341 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id); |
41c48f3a AI |
3342 | |
3343 | return 0; | |
3344 | } | |
3345 | ||
3346 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
3347 | struct bpf_reg_state *regs, | |
3348 | int regno, int off, int size, | |
3349 | enum bpf_access_type atype, | |
3350 | int value_regno) | |
3351 | { | |
3352 | struct bpf_reg_state *reg = regs + regno; | |
3353 | struct bpf_map *map = reg->map_ptr; | |
3354 | const struct btf_type *t; | |
3355 | const char *tname; | |
3356 | u32 btf_id; | |
3357 | int ret; | |
3358 | ||
3359 | if (!btf_vmlinux) { | |
3360 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
3361 | return -ENOTSUPP; | |
3362 | } | |
3363 | ||
3364 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
3365 | verbose(env, "map_ptr access not supported for map type %d\n", | |
3366 | map->map_type); | |
3367 | return -ENOTSUPP; | |
3368 | } | |
3369 | ||
3370 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
3371 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
3372 | ||
3373 | if (!env->allow_ptr_to_map_access) { | |
3374 | verbose(env, | |
3375 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
3376 | tname); | |
3377 | return -EPERM; | |
9e15db66 | 3378 | } |
27ae7997 | 3379 | |
41c48f3a AI |
3380 | if (off < 0) { |
3381 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
3382 | regno, tname, off); | |
3383 | return -EACCES; | |
3384 | } | |
3385 | ||
3386 | if (atype != BPF_READ) { | |
3387 | verbose(env, "only read from %s is supported\n", tname); | |
3388 | return -EACCES; | |
3389 | } | |
3390 | ||
22dc4a0f | 3391 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id); |
41c48f3a AI |
3392 | if (ret < 0) |
3393 | return ret; | |
3394 | ||
3395 | if (value_regno >= 0) | |
22dc4a0f | 3396 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id); |
41c48f3a | 3397 | |
9e15db66 AS |
3398 | return 0; |
3399 | } | |
3400 | ||
41c48f3a | 3401 | |
17a52670 AS |
3402 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
3403 | * if t==write, value_regno is a register which value is stored into memory | |
3404 | * if t==read, value_regno is a register which will receive the value from memory | |
3405 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
3406 | * if t==read && value_regno==-1, don't care what we read from memory | |
3407 | */ | |
ca369602 DB |
3408 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
3409 | int off, int bpf_size, enum bpf_access_type t, | |
3410 | int value_regno, bool strict_alignment_once) | |
17a52670 | 3411 | { |
638f5b90 AS |
3412 | struct bpf_reg_state *regs = cur_regs(env); |
3413 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 3414 | struct bpf_func_state *state; |
17a52670 AS |
3415 | int size, err = 0; |
3416 | ||
3417 | size = bpf_size_to_bytes(bpf_size); | |
3418 | if (size < 0) | |
3419 | return size; | |
3420 | ||
f1174f77 | 3421 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 3422 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
3423 | if (err) |
3424 | return err; | |
17a52670 | 3425 | |
f1174f77 EC |
3426 | /* for access checks, reg->off is just part of off */ |
3427 | off += reg->off; | |
3428 | ||
3429 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
3430 | if (t == BPF_WRITE && value_regno >= 0 && |
3431 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3432 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
3433 | return -EACCES; |
3434 | } | |
591fe988 DB |
3435 | err = check_map_access_type(env, regno, off, size, t); |
3436 | if (err) | |
3437 | return err; | |
9fd29c08 | 3438 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
3439 | if (!err && t == BPF_READ && value_regno >= 0) { |
3440 | struct bpf_map *map = reg->map_ptr; | |
3441 | ||
3442 | /* if map is read-only, track its contents as scalars */ | |
3443 | if (tnum_is_const(reg->var_off) && | |
3444 | bpf_map_is_rdonly(map) && | |
3445 | map->ops->map_direct_value_addr) { | |
3446 | int map_off = off + reg->var_off.value; | |
3447 | u64 val = 0; | |
3448 | ||
3449 | err = bpf_map_direct_read(map, map_off, size, | |
3450 | &val); | |
3451 | if (err) | |
3452 | return err; | |
3453 | ||
3454 | regs[value_regno].type = SCALAR_VALUE; | |
3455 | __mark_reg_known(®s[value_regno], val); | |
3456 | } else { | |
3457 | mark_reg_unknown(env, regs, value_regno); | |
3458 | } | |
3459 | } | |
457f4436 AN |
3460 | } else if (reg->type == PTR_TO_MEM) { |
3461 | if (t == BPF_WRITE && value_regno >= 0 && | |
3462 | is_pointer_value(env, value_regno)) { | |
3463 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
3464 | return -EACCES; | |
3465 | } | |
3466 | err = check_mem_region_access(env, regno, off, size, | |
3467 | reg->mem_size, false); | |
3468 | if (!err && t == BPF_READ && value_regno >= 0) | |
3469 | mark_reg_unknown(env, regs, value_regno); | |
1a0dc1ac | 3470 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 3471 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
22dc4a0f | 3472 | struct btf *btf = NULL; |
9e15db66 | 3473 | u32 btf_id = 0; |
19de99f7 | 3474 | |
1be7f75d AS |
3475 | if (t == BPF_WRITE && value_regno >= 0 && |
3476 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3477 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
3478 | return -EACCES; |
3479 | } | |
f1174f77 | 3480 | |
58990d1f DB |
3481 | err = check_ctx_reg(env, reg, regno); |
3482 | if (err < 0) | |
3483 | return err; | |
3484 | ||
22dc4a0f | 3485 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id); |
9e15db66 AS |
3486 | if (err) |
3487 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 3488 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 3489 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
3490 | * PTR_TO_PACKET[_META,_END]. In the latter |
3491 | * case, we know the offset is zero. | |
f1174f77 | 3492 | */ |
46f8bc92 | 3493 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 3494 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 3495 | } else { |
638f5b90 | 3496 | mark_reg_known_zero(env, regs, |
61bd5218 | 3497 | value_regno); |
46f8bc92 MKL |
3498 | if (reg_type_may_be_null(reg_type)) |
3499 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
3500 | /* A load of ctx field could have different |
3501 | * actual load size with the one encoded in the | |
3502 | * insn. When the dst is PTR, it is for sure not | |
3503 | * a sub-register. | |
3504 | */ | |
3505 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
b121b341 | 3506 | if (reg_type == PTR_TO_BTF_ID || |
22dc4a0f AN |
3507 | reg_type == PTR_TO_BTF_ID_OR_NULL) { |
3508 | regs[value_regno].btf = btf; | |
9e15db66 | 3509 | regs[value_regno].btf_id = btf_id; |
22dc4a0f | 3510 | } |
46f8bc92 | 3511 | } |
638f5b90 | 3512 | regs[value_regno].type = reg_type; |
969bf05e | 3513 | } |
17a52670 | 3514 | |
f1174f77 | 3515 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 3516 | off += reg->var_off.value; |
e4298d25 DB |
3517 | err = check_stack_access(env, reg, off, size); |
3518 | if (err) | |
3519 | return err; | |
8726679a | 3520 | |
f4d7e40a AS |
3521 | state = func(env, reg); |
3522 | err = update_stack_depth(env, state, off); | |
3523 | if (err) | |
3524 | return err; | |
8726679a | 3525 | |
638f5b90 | 3526 | if (t == BPF_WRITE) |
61bd5218 | 3527 | err = check_stack_write(env, state, off, size, |
af86ca4e | 3528 | value_regno, insn_idx); |
638f5b90 | 3529 | else |
61bd5218 JK |
3530 | err = check_stack_read(env, state, off, size, |
3531 | value_regno); | |
de8f3a83 | 3532 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 3533 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 3534 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
3535 | return -EACCES; |
3536 | } | |
4acf6c0b BB |
3537 | if (t == BPF_WRITE && value_regno >= 0 && |
3538 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
3539 | verbose(env, "R%d leaks addr into packet\n", |
3540 | value_regno); | |
4acf6c0b BB |
3541 | return -EACCES; |
3542 | } | |
9fd29c08 | 3543 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 3544 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 3545 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
3546 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
3547 | if (t == BPF_WRITE && value_regno >= 0 && | |
3548 | is_pointer_value(env, value_regno)) { | |
3549 | verbose(env, "R%d leaks addr into flow keys\n", | |
3550 | value_regno); | |
3551 | return -EACCES; | |
3552 | } | |
3553 | ||
3554 | err = check_flow_keys_access(env, off, size); | |
3555 | if (!err && t == BPF_READ && value_regno >= 0) | |
3556 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 3557 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 3558 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
3559 | verbose(env, "R%d cannot write into %s\n", |
3560 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
3561 | return -EACCES; |
3562 | } | |
5f456649 | 3563 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
3564 | if (!err && value_regno >= 0) |
3565 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
3566 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
3567 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
3568 | if (!err && t == BPF_READ && value_regno >= 0) | |
3569 | mark_reg_unknown(env, regs, value_regno); | |
9e15db66 AS |
3570 | } else if (reg->type == PTR_TO_BTF_ID) { |
3571 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, | |
3572 | value_regno); | |
41c48f3a AI |
3573 | } else if (reg->type == CONST_PTR_TO_MAP) { |
3574 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
3575 | value_regno); | |
afbf21dc YS |
3576 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
3577 | if (t == BPF_WRITE) { | |
3578 | verbose(env, "R%d cannot write into %s\n", | |
3579 | regno, reg_type_str[reg->type]); | |
3580 | return -EACCES; | |
3581 | } | |
f6dfbe31 CIK |
3582 | err = check_buffer_access(env, reg, regno, off, size, false, |
3583 | "rdonly", | |
afbf21dc YS |
3584 | &env->prog->aux->max_rdonly_access); |
3585 | if (!err && value_regno >= 0) | |
3586 | mark_reg_unknown(env, regs, value_regno); | |
3587 | } else if (reg->type == PTR_TO_RDWR_BUF) { | |
f6dfbe31 CIK |
3588 | err = check_buffer_access(env, reg, regno, off, size, false, |
3589 | "rdwr", | |
afbf21dc YS |
3590 | &env->prog->aux->max_rdwr_access); |
3591 | if (!err && t == BPF_READ && value_regno >= 0) | |
3592 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 3593 | } else { |
61bd5218 JK |
3594 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
3595 | reg_type_str[reg->type]); | |
17a52670 AS |
3596 | return -EACCES; |
3597 | } | |
969bf05e | 3598 | |
f1174f77 | 3599 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 3600 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 3601 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 3602 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 3603 | } |
17a52670 AS |
3604 | return err; |
3605 | } | |
3606 | ||
91c960b0 | 3607 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 3608 | { |
17a52670 AS |
3609 | int err; |
3610 | ||
91c960b0 BJ |
3611 | if (insn->imm != BPF_ADD) { |
3612 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); | |
3613 | return -EINVAL; | |
3614 | } | |
3615 | ||
3616 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { | |
3617 | verbose(env, "invalid atomic operand size\n"); | |
17a52670 AS |
3618 | return -EINVAL; |
3619 | } | |
3620 | ||
3621 | /* check src1 operand */ | |
dc503a8a | 3622 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3623 | if (err) |
3624 | return err; | |
3625 | ||
3626 | /* check src2 operand */ | |
dc503a8a | 3627 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3628 | if (err) |
3629 | return err; | |
3630 | ||
6bdf6abc | 3631 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 3632 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
3633 | return -EACCES; |
3634 | } | |
3635 | ||
ca369602 | 3636 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 3637 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
3638 | is_flow_key_reg(env, insn->dst_reg) || |
3639 | is_sk_reg(env, insn->dst_reg)) { | |
91c960b0 | 3640 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
2a159c6f DB |
3641 | insn->dst_reg, |
3642 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
3643 | return -EACCES; |
3644 | } | |
3645 | ||
91c960b0 | 3646 | /* check whether we can read the memory */ |
31fd8581 | 3647 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3648 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
3649 | if (err) |
3650 | return err; | |
3651 | ||
91c960b0 | 3652 | /* check whether we can write into the same memory */ |
31fd8581 | 3653 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3654 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
3655 | } |
3656 | ||
2011fccf AI |
3657 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
3658 | int off, int access_size, | |
3659 | bool zero_size_allowed) | |
3660 | { | |
3661 | struct bpf_reg_state *reg = reg_state(env, regno); | |
3662 | ||
3663 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
3664 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
3665 | if (tnum_is_const(reg->var_off)) { | |
3666 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
3667 | regno, off, access_size); | |
3668 | } else { | |
3669 | char tn_buf[48]; | |
3670 | ||
3671 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3672 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
3673 | regno, tn_buf, access_size); | |
3674 | } | |
3675 | return -EACCES; | |
3676 | } | |
3677 | return 0; | |
3678 | } | |
3679 | ||
17a52670 AS |
3680 | /* when register 'regno' is passed into function that will read 'access_size' |
3681 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
3682 | * and all elements of stack are initialized. |
3683 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
3684 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 3685 | */ |
58e2af8b | 3686 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
3687 | int access_size, bool zero_size_allowed, |
3688 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3689 | { |
2a159c6f | 3690 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 3691 | struct bpf_func_state *state = func(env, reg); |
f7cf25b2 | 3692 | int err, min_off, max_off, i, j, slot, spi; |
17a52670 | 3693 | |
2011fccf AI |
3694 | if (tnum_is_const(reg->var_off)) { |
3695 | min_off = max_off = reg->var_off.value + reg->off; | |
3696 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
3697 | zero_size_allowed); | |
3698 | if (err) | |
3699 | return err; | |
3700 | } else { | |
088ec26d AI |
3701 | /* Variable offset is prohibited for unprivileged mode for |
3702 | * simplicity since it requires corresponding support in | |
3703 | * Spectre masking for stack ALU. | |
3704 | * See also retrieve_ptr_limit(). | |
3705 | */ | |
2c78ee89 | 3706 | if (!env->bypass_spec_v1) { |
088ec26d | 3707 | char tn_buf[48]; |
f1174f77 | 3708 | |
088ec26d AI |
3709 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3710 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
3711 | regno, tn_buf); | |
3712 | return -EACCES; | |
3713 | } | |
f2bcd05e AI |
3714 | /* Only initialized buffer on stack is allowed to be accessed |
3715 | * with variable offset. With uninitialized buffer it's hard to | |
3716 | * guarantee that whole memory is marked as initialized on | |
3717 | * helper return since specific bounds are unknown what may | |
3718 | * cause uninitialized stack leaking. | |
3719 | */ | |
3720 | if (meta && meta->raw_mode) | |
3721 | meta = NULL; | |
3722 | ||
107c26a7 AI |
3723 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
3724 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
3725 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
3726 | regno); | |
3727 | return -EACCES; | |
3728 | } | |
2011fccf | 3729 | min_off = reg->smin_value + reg->off; |
107c26a7 | 3730 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
3731 | err = __check_stack_boundary(env, regno, min_off, access_size, |
3732 | zero_size_allowed); | |
107c26a7 AI |
3733 | if (err) { |
3734 | verbose(env, "R%d min value is outside of stack bound\n", | |
3735 | regno); | |
2011fccf | 3736 | return err; |
107c26a7 | 3737 | } |
2011fccf AI |
3738 | err = __check_stack_boundary(env, regno, max_off, access_size, |
3739 | zero_size_allowed); | |
107c26a7 AI |
3740 | if (err) { |
3741 | verbose(env, "R%d max value is outside of stack bound\n", | |
3742 | regno); | |
2011fccf | 3743 | return err; |
107c26a7 | 3744 | } |
17a52670 AS |
3745 | } |
3746 | ||
435faee1 DB |
3747 | if (meta && meta->raw_mode) { |
3748 | meta->access_size = access_size; | |
3749 | meta->regno = regno; | |
3750 | return 0; | |
3751 | } | |
3752 | ||
2011fccf | 3753 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
3754 | u8 *stype; |
3755 | ||
2011fccf | 3756 | slot = -i - 1; |
638f5b90 | 3757 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
3758 | if (state->allocated_stack <= slot) |
3759 | goto err; | |
3760 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
3761 | if (*stype == STACK_MISC) | |
3762 | goto mark; | |
3763 | if (*stype == STACK_ZERO) { | |
3764 | /* helper can write anything into the stack */ | |
3765 | *stype = STACK_MISC; | |
3766 | goto mark; | |
17a52670 | 3767 | } |
1d68f22b YS |
3768 | |
3769 | if (state->stack[spi].slot_type[0] == STACK_SPILL && | |
3770 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) | |
3771 | goto mark; | |
3772 | ||
f7cf25b2 | 3773 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
cd17d38f YS |
3774 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
3775 | env->allow_ptr_leaks)) { | |
f54c7898 | 3776 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
f7cf25b2 AS |
3777 | for (j = 0; j < BPF_REG_SIZE; j++) |
3778 | state->stack[spi].slot_type[j] = STACK_MISC; | |
3779 | goto mark; | |
3780 | } | |
3781 | ||
cc2b14d5 | 3782 | err: |
2011fccf AI |
3783 | if (tnum_is_const(reg->var_off)) { |
3784 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
3785 | min_off, i - min_off, access_size); | |
3786 | } else { | |
3787 | char tn_buf[48]; | |
3788 | ||
3789 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3790 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
3791 | tn_buf, i - min_off, access_size); | |
3792 | } | |
cc2b14d5 AS |
3793 | return -EACCES; |
3794 | mark: | |
3795 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
3796 | * the whole slot to be marked as 'read' | |
3797 | */ | |
679c782d | 3798 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
3799 | state->stack[spi].spilled_ptr.parent, |
3800 | REG_LIVE_READ64); | |
17a52670 | 3801 | } |
2011fccf | 3802 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
3803 | } |
3804 | ||
06c1c049 GB |
3805 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
3806 | int access_size, bool zero_size_allowed, | |
3807 | struct bpf_call_arg_meta *meta) | |
3808 | { | |
638f5b90 | 3809 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 3810 | |
f1174f77 | 3811 | switch (reg->type) { |
06c1c049 | 3812 | case PTR_TO_PACKET: |
de8f3a83 | 3813 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
3814 | return check_packet_access(env, regno, reg->off, access_size, |
3815 | zero_size_allowed); | |
06c1c049 | 3816 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
3817 | if (check_map_access_type(env, regno, reg->off, access_size, |
3818 | meta && meta->raw_mode ? BPF_WRITE : | |
3819 | BPF_READ)) | |
3820 | return -EACCES; | |
9fd29c08 YS |
3821 | return check_map_access(env, regno, reg->off, access_size, |
3822 | zero_size_allowed); | |
457f4436 AN |
3823 | case PTR_TO_MEM: |
3824 | return check_mem_region_access(env, regno, reg->off, | |
3825 | access_size, reg->mem_size, | |
3826 | zero_size_allowed); | |
afbf21dc YS |
3827 | case PTR_TO_RDONLY_BUF: |
3828 | if (meta && meta->raw_mode) | |
3829 | return -EACCES; | |
3830 | return check_buffer_access(env, reg, regno, reg->off, | |
3831 | access_size, zero_size_allowed, | |
3832 | "rdonly", | |
3833 | &env->prog->aux->max_rdonly_access); | |
3834 | case PTR_TO_RDWR_BUF: | |
3835 | return check_buffer_access(env, reg, regno, reg->off, | |
3836 | access_size, zero_size_allowed, | |
3837 | "rdwr", | |
3838 | &env->prog->aux->max_rdwr_access); | |
0d004c02 | 3839 | case PTR_TO_STACK: |
06c1c049 GB |
3840 | return check_stack_boundary(env, regno, access_size, |
3841 | zero_size_allowed, meta); | |
0d004c02 LB |
3842 | default: /* scalar_value or invalid ptr */ |
3843 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
3844 | if (zero_size_allowed && access_size == 0 && | |
3845 | register_is_null(reg)) | |
3846 | return 0; | |
3847 | ||
3848 | verbose(env, "R%d type=%s expected=%s\n", regno, | |
3849 | reg_type_str[reg->type], | |
3850 | reg_type_str[PTR_TO_STACK]); | |
3851 | return -EACCES; | |
06c1c049 GB |
3852 | } |
3853 | } | |
3854 | ||
d83525ca AS |
3855 | /* Implementation details: |
3856 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
3857 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
3858 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
3859 | * value_or_null->value transition, since the verifier only cares about | |
3860 | * the range of access to valid map value pointer and doesn't care about actual | |
3861 | * address of the map element. | |
3862 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
3863 | * reg->id > 0 after value_or_null->value transition. By doing so | |
3864 | * two bpf_map_lookups will be considered two different pointers that | |
3865 | * point to different bpf_spin_locks. | |
3866 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
3867 | * dead-locks. | |
3868 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
3869 | * reg_is_refcounted() logic. The verifier needs to remember only | |
3870 | * one spin_lock instead of array of acquired_refs. | |
3871 | * cur_state->active_spin_lock remembers which map value element got locked | |
3872 | * and clears it after bpf_spin_unlock. | |
3873 | */ | |
3874 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
3875 | bool is_lock) | |
3876 | { | |
3877 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
3878 | struct bpf_verifier_state *cur = env->cur_state; | |
3879 | bool is_const = tnum_is_const(reg->var_off); | |
3880 | struct bpf_map *map = reg->map_ptr; | |
3881 | u64 val = reg->var_off.value; | |
3882 | ||
d83525ca AS |
3883 | if (!is_const) { |
3884 | verbose(env, | |
3885 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
3886 | regno); | |
3887 | return -EINVAL; | |
3888 | } | |
3889 | if (!map->btf) { | |
3890 | verbose(env, | |
3891 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
3892 | map->name); | |
3893 | return -EINVAL; | |
3894 | } | |
3895 | if (!map_value_has_spin_lock(map)) { | |
3896 | if (map->spin_lock_off == -E2BIG) | |
3897 | verbose(env, | |
3898 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
3899 | map->name); | |
3900 | else if (map->spin_lock_off == -ENOENT) | |
3901 | verbose(env, | |
3902 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
3903 | map->name); | |
3904 | else | |
3905 | verbose(env, | |
3906 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
3907 | map->name); | |
3908 | return -EINVAL; | |
3909 | } | |
3910 | if (map->spin_lock_off != val + reg->off) { | |
3911 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
3912 | val + reg->off); | |
3913 | return -EINVAL; | |
3914 | } | |
3915 | if (is_lock) { | |
3916 | if (cur->active_spin_lock) { | |
3917 | verbose(env, | |
3918 | "Locking two bpf_spin_locks are not allowed\n"); | |
3919 | return -EINVAL; | |
3920 | } | |
3921 | cur->active_spin_lock = reg->id; | |
3922 | } else { | |
3923 | if (!cur->active_spin_lock) { | |
3924 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
3925 | return -EINVAL; | |
3926 | } | |
3927 | if (cur->active_spin_lock != reg->id) { | |
3928 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
3929 | return -EINVAL; | |
3930 | } | |
3931 | cur->active_spin_lock = 0; | |
3932 | } | |
3933 | return 0; | |
3934 | } | |
3935 | ||
90133415 DB |
3936 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
3937 | { | |
3938 | return type == ARG_PTR_TO_MEM || | |
3939 | type == ARG_PTR_TO_MEM_OR_NULL || | |
3940 | type == ARG_PTR_TO_UNINIT_MEM; | |
3941 | } | |
3942 | ||
3943 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
3944 | { | |
3945 | return type == ARG_CONST_SIZE || | |
3946 | type == ARG_CONST_SIZE_OR_ZERO; | |
3947 | } | |
3948 | ||
457f4436 AN |
3949 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
3950 | { | |
3951 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; | |
3952 | } | |
3953 | ||
57c3bb72 AI |
3954 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
3955 | { | |
3956 | return type == ARG_PTR_TO_INT || | |
3957 | type == ARG_PTR_TO_LONG; | |
3958 | } | |
3959 | ||
3960 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
3961 | { | |
3962 | if (type == ARG_PTR_TO_INT) | |
3963 | return sizeof(u32); | |
3964 | else if (type == ARG_PTR_TO_LONG) | |
3965 | return sizeof(u64); | |
3966 | ||
3967 | return -EINVAL; | |
3968 | } | |
3969 | ||
912f442c LB |
3970 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
3971 | const struct bpf_call_arg_meta *meta, | |
3972 | enum bpf_arg_type *arg_type) | |
3973 | { | |
3974 | if (!meta->map_ptr) { | |
3975 | /* kernel subsystem misconfigured verifier */ | |
3976 | verbose(env, "invalid map_ptr to access map->type\n"); | |
3977 | return -EACCES; | |
3978 | } | |
3979 | ||
3980 | switch (meta->map_ptr->map_type) { | |
3981 | case BPF_MAP_TYPE_SOCKMAP: | |
3982 | case BPF_MAP_TYPE_SOCKHASH: | |
3983 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 3984 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
3985 | } else { |
3986 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
3987 | return -EINVAL; | |
3988 | } | |
3989 | break; | |
3990 | ||
3991 | default: | |
3992 | break; | |
3993 | } | |
3994 | return 0; | |
3995 | } | |
3996 | ||
f79e7ea5 LB |
3997 | struct bpf_reg_types { |
3998 | const enum bpf_reg_type types[10]; | |
1df8f55a | 3999 | u32 *btf_id; |
f79e7ea5 LB |
4000 | }; |
4001 | ||
4002 | static const struct bpf_reg_types map_key_value_types = { | |
4003 | .types = { | |
4004 | PTR_TO_STACK, | |
4005 | PTR_TO_PACKET, | |
4006 | PTR_TO_PACKET_META, | |
4007 | PTR_TO_MAP_VALUE, | |
4008 | }, | |
4009 | }; | |
4010 | ||
4011 | static const struct bpf_reg_types sock_types = { | |
4012 | .types = { | |
4013 | PTR_TO_SOCK_COMMON, | |
4014 | PTR_TO_SOCKET, | |
4015 | PTR_TO_TCP_SOCK, | |
4016 | PTR_TO_XDP_SOCK, | |
4017 | }, | |
4018 | }; | |
4019 | ||
49a2a4d4 | 4020 | #ifdef CONFIG_NET |
1df8f55a MKL |
4021 | static const struct bpf_reg_types btf_id_sock_common_types = { |
4022 | .types = { | |
4023 | PTR_TO_SOCK_COMMON, | |
4024 | PTR_TO_SOCKET, | |
4025 | PTR_TO_TCP_SOCK, | |
4026 | PTR_TO_XDP_SOCK, | |
4027 | PTR_TO_BTF_ID, | |
4028 | }, | |
4029 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
4030 | }; | |
49a2a4d4 | 4031 | #endif |
1df8f55a | 4032 | |
f79e7ea5 LB |
4033 | static const struct bpf_reg_types mem_types = { |
4034 | .types = { | |
4035 | PTR_TO_STACK, | |
4036 | PTR_TO_PACKET, | |
4037 | PTR_TO_PACKET_META, | |
4038 | PTR_TO_MAP_VALUE, | |
4039 | PTR_TO_MEM, | |
4040 | PTR_TO_RDONLY_BUF, | |
4041 | PTR_TO_RDWR_BUF, | |
4042 | }, | |
4043 | }; | |
4044 | ||
4045 | static const struct bpf_reg_types int_ptr_types = { | |
4046 | .types = { | |
4047 | PTR_TO_STACK, | |
4048 | PTR_TO_PACKET, | |
4049 | PTR_TO_PACKET_META, | |
4050 | PTR_TO_MAP_VALUE, | |
4051 | }, | |
4052 | }; | |
4053 | ||
4054 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; | |
4055 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
4056 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
4057 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; | |
4058 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; | |
4059 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
4060 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; | |
eaa6bcb7 | 4061 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
f79e7ea5 | 4062 | |
0789e13b | 4063 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
f79e7ea5 LB |
4064 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
4065 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, | |
4066 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, | |
4067 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, | |
4068 | [ARG_CONST_SIZE] = &scalar_types, | |
4069 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
4070 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
4071 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
4072 | [ARG_PTR_TO_CTX] = &context_types, | |
4073 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, | |
4074 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, | |
49a2a4d4 | 4075 | #ifdef CONFIG_NET |
1df8f55a | 4076 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 4077 | #endif |
f79e7ea5 LB |
4078 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
4079 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, | |
4080 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, | |
4081 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
4082 | [ARG_PTR_TO_MEM] = &mem_types, | |
4083 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, | |
4084 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, | |
4085 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, | |
4086 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, | |
4087 | [ARG_PTR_TO_INT] = &int_ptr_types, | |
4088 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 4089 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
f79e7ea5 LB |
4090 | }; |
4091 | ||
4092 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 MKL |
4093 | enum bpf_arg_type arg_type, |
4094 | const u32 *arg_btf_id) | |
f79e7ea5 LB |
4095 | { |
4096 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
4097 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 4098 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
4099 | int i, j; |
4100 | ||
a968d5e2 MKL |
4101 | compatible = compatible_reg_types[arg_type]; |
4102 | if (!compatible) { | |
4103 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
4104 | return -EFAULT; | |
4105 | } | |
4106 | ||
f79e7ea5 LB |
4107 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
4108 | expected = compatible->types[i]; | |
4109 | if (expected == NOT_INIT) | |
4110 | break; | |
4111 | ||
4112 | if (type == expected) | |
a968d5e2 | 4113 | goto found; |
f79e7ea5 LB |
4114 | } |
4115 | ||
4116 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); | |
4117 | for (j = 0; j + 1 < i; j++) | |
4118 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); | |
4119 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); | |
4120 | return -EACCES; | |
a968d5e2 MKL |
4121 | |
4122 | found: | |
4123 | if (type == PTR_TO_BTF_ID) { | |
1df8f55a MKL |
4124 | if (!arg_btf_id) { |
4125 | if (!compatible->btf_id) { | |
4126 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
4127 | return -EFAULT; | |
4128 | } | |
4129 | arg_btf_id = compatible->btf_id; | |
4130 | } | |
4131 | ||
22dc4a0f AN |
4132 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, |
4133 | btf_vmlinux, *arg_btf_id)) { | |
a968d5e2 | 4134 | verbose(env, "R%d is of type %s but %s is expected\n", |
22dc4a0f AN |
4135 | regno, kernel_type_name(reg->btf, reg->btf_id), |
4136 | kernel_type_name(btf_vmlinux, *arg_btf_id)); | |
a968d5e2 MKL |
4137 | return -EACCES; |
4138 | } | |
4139 | ||
4140 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4141 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", | |
4142 | regno); | |
4143 | return -EACCES; | |
4144 | } | |
4145 | } | |
4146 | ||
4147 | return 0; | |
f79e7ea5 LB |
4148 | } |
4149 | ||
af7ec138 YS |
4150 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
4151 | struct bpf_call_arg_meta *meta, | |
4152 | const struct bpf_func_proto *fn) | |
17a52670 | 4153 | { |
af7ec138 | 4154 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 4155 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 4156 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 4157 | enum bpf_reg_type type = reg->type; |
17a52670 AS |
4158 | int err = 0; |
4159 | ||
80f1d68c | 4160 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
4161 | return 0; |
4162 | ||
dc503a8a EC |
4163 | err = check_reg_arg(env, regno, SRC_OP); |
4164 | if (err) | |
4165 | return err; | |
17a52670 | 4166 | |
1be7f75d AS |
4167 | if (arg_type == ARG_ANYTHING) { |
4168 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
4169 | verbose(env, "R%d leaks addr into helper function\n", |
4170 | regno); | |
1be7f75d AS |
4171 | return -EACCES; |
4172 | } | |
80f1d68c | 4173 | return 0; |
1be7f75d | 4174 | } |
80f1d68c | 4175 | |
de8f3a83 | 4176 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 4177 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 4178 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
4179 | return -EACCES; |
4180 | } | |
4181 | ||
912f442c LB |
4182 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
4183 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || | |
4184 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
4185 | err = resolve_map_arg_type(env, meta, &arg_type); | |
4186 | if (err) | |
4187 | return err; | |
4188 | } | |
4189 | ||
fd1b0d60 LB |
4190 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
4191 | /* A NULL register has a SCALAR_VALUE type, so skip | |
4192 | * type checking. | |
4193 | */ | |
4194 | goto skip_type_check; | |
4195 | ||
a968d5e2 | 4196 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
f79e7ea5 LB |
4197 | if (err) |
4198 | return err; | |
4199 | ||
a968d5e2 | 4200 | if (type == PTR_TO_CTX) { |
feec7040 LB |
4201 | err = check_ctx_reg(env, reg, regno); |
4202 | if (err < 0) | |
4203 | return err; | |
d7b9454a LB |
4204 | } |
4205 | ||
fd1b0d60 | 4206 | skip_type_check: |
02f7c958 | 4207 | if (reg->ref_obj_id) { |
457f4436 AN |
4208 | if (meta->ref_obj_id) { |
4209 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
4210 | regno, reg->ref_obj_id, | |
4211 | meta->ref_obj_id); | |
4212 | return -EFAULT; | |
4213 | } | |
4214 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
4215 | } |
4216 | ||
17a52670 AS |
4217 | if (arg_type == ARG_CONST_MAP_PTR) { |
4218 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 4219 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
4220 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
4221 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
4222 | * check that [key, key + map->key_size) are within | |
4223 | * stack limits and initialized | |
4224 | */ | |
33ff9823 | 4225 | if (!meta->map_ptr) { |
17a52670 AS |
4226 | /* in function declaration map_ptr must come before |
4227 | * map_key, so that it's verified and known before | |
4228 | * we have to check map_key here. Otherwise it means | |
4229 | * that kernel subsystem misconfigured verifier | |
4230 | */ | |
61bd5218 | 4231 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
4232 | return -EACCES; |
4233 | } | |
d71962f3 PC |
4234 | err = check_helper_mem_access(env, regno, |
4235 | meta->map_ptr->key_size, false, | |
4236 | NULL); | |
2ea864c5 | 4237 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
4238 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
4239 | !register_is_null(reg)) || | |
2ea864c5 | 4240 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
4241 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
4242 | * check [value, value + map->value_size) validity | |
4243 | */ | |
33ff9823 | 4244 | if (!meta->map_ptr) { |
17a52670 | 4245 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 4246 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
4247 | return -EACCES; |
4248 | } | |
2ea864c5 | 4249 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
4250 | err = check_helper_mem_access(env, regno, |
4251 | meta->map_ptr->value_size, false, | |
2ea864c5 | 4252 | meta); |
eaa6bcb7 HL |
4253 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
4254 | if (!reg->btf_id) { | |
4255 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
4256 | return -EACCES; | |
4257 | } | |
22dc4a0f | 4258 | meta->ret_btf = reg->btf; |
eaa6bcb7 | 4259 | meta->ret_btf_id = reg->btf_id; |
c18f0b6a LB |
4260 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
4261 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
4262 | if (process_spin_lock(env, regno, true)) | |
4263 | return -EACCES; | |
4264 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
4265 | if (process_spin_lock(env, regno, false)) | |
4266 | return -EACCES; | |
4267 | } else { | |
4268 | verbose(env, "verifier internal error\n"); | |
4269 | return -EFAULT; | |
4270 | } | |
a2bbe7cc LB |
4271 | } else if (arg_type_is_mem_ptr(arg_type)) { |
4272 | /* The access to this pointer is only checked when we hit the | |
4273 | * next is_mem_size argument below. | |
4274 | */ | |
4275 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); | |
90133415 | 4276 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 4277 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 4278 | |
10060503 JF |
4279 | /* This is used to refine r0 return value bounds for helpers |
4280 | * that enforce this value as an upper bound on return values. | |
4281 | * See do_refine_retval_range() for helpers that can refine | |
4282 | * the return value. C type of helper is u32 so we pull register | |
4283 | * bound from umax_value however, if negative verifier errors | |
4284 | * out. Only upper bounds can be learned because retval is an | |
4285 | * int type and negative retvals are allowed. | |
849fa506 | 4286 | */ |
10060503 | 4287 | meta->msize_max_value = reg->umax_value; |
849fa506 | 4288 | |
f1174f77 EC |
4289 | /* The register is SCALAR_VALUE; the access check |
4290 | * happens using its boundaries. | |
06c1c049 | 4291 | */ |
f1174f77 | 4292 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
4293 | /* For unprivileged variable accesses, disable raw |
4294 | * mode so that the program is required to | |
4295 | * initialize all the memory that the helper could | |
4296 | * just partially fill up. | |
4297 | */ | |
4298 | meta = NULL; | |
4299 | ||
b03c9f9f | 4300 | if (reg->smin_value < 0) { |
61bd5218 | 4301 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
4302 | regno); |
4303 | return -EACCES; | |
4304 | } | |
06c1c049 | 4305 | |
b03c9f9f | 4306 | if (reg->umin_value == 0) { |
f1174f77 EC |
4307 | err = check_helper_mem_access(env, regno - 1, 0, |
4308 | zero_size_allowed, | |
4309 | meta); | |
06c1c049 GB |
4310 | if (err) |
4311 | return err; | |
06c1c049 | 4312 | } |
f1174f77 | 4313 | |
b03c9f9f | 4314 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 4315 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
4316 | regno); |
4317 | return -EACCES; | |
4318 | } | |
4319 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 4320 | reg->umax_value, |
f1174f77 | 4321 | zero_size_allowed, meta); |
b5dc0163 AS |
4322 | if (!err) |
4323 | err = mark_chain_precision(env, regno); | |
457f4436 AN |
4324 | } else if (arg_type_is_alloc_size(arg_type)) { |
4325 | if (!tnum_is_const(reg->var_off)) { | |
28a8add6 | 4326 | verbose(env, "R%d is not a known constant'\n", |
457f4436 AN |
4327 | regno); |
4328 | return -EACCES; | |
4329 | } | |
4330 | meta->mem_size = reg->var_off.value; | |
57c3bb72 AI |
4331 | } else if (arg_type_is_int_ptr(arg_type)) { |
4332 | int size = int_ptr_type_to_size(arg_type); | |
4333 | ||
4334 | err = check_helper_mem_access(env, regno, size, false, meta); | |
4335 | if (err) | |
4336 | return err; | |
4337 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
4338 | } |
4339 | ||
4340 | return err; | |
4341 | } | |
4342 | ||
0126240f LB |
4343 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
4344 | { | |
4345 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 4346 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
4347 | |
4348 | if (func_id != BPF_FUNC_map_update_elem) | |
4349 | return false; | |
4350 | ||
4351 | /* It's not possible to get access to a locked struct sock in these | |
4352 | * contexts, so updating is safe. | |
4353 | */ | |
4354 | switch (type) { | |
4355 | case BPF_PROG_TYPE_TRACING: | |
4356 | if (eatype == BPF_TRACE_ITER) | |
4357 | return true; | |
4358 | break; | |
4359 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
4360 | case BPF_PROG_TYPE_SCHED_CLS: | |
4361 | case BPF_PROG_TYPE_SCHED_ACT: | |
4362 | case BPF_PROG_TYPE_XDP: | |
4363 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
4364 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
4365 | case BPF_PROG_TYPE_SK_LOOKUP: | |
4366 | return true; | |
4367 | default: | |
4368 | break; | |
4369 | } | |
4370 | ||
4371 | verbose(env, "cannot update sockmap in this context\n"); | |
4372 | return false; | |
4373 | } | |
4374 | ||
e411901c MF |
4375 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
4376 | { | |
4377 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); | |
4378 | } | |
4379 | ||
61bd5218 JK |
4380 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
4381 | struct bpf_map *map, int func_id) | |
35578d79 | 4382 | { |
35578d79 KX |
4383 | if (!map) |
4384 | return 0; | |
4385 | ||
6aff67c8 AS |
4386 | /* We need a two way check, first is from map perspective ... */ |
4387 | switch (map->map_type) { | |
4388 | case BPF_MAP_TYPE_PROG_ARRAY: | |
4389 | if (func_id != BPF_FUNC_tail_call) | |
4390 | goto error; | |
4391 | break; | |
4392 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
4393 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 4394 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 4395 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
4396 | func_id != BPF_FUNC_perf_event_read_value && |
4397 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
4398 | goto error; |
4399 | break; | |
457f4436 AN |
4400 | case BPF_MAP_TYPE_RINGBUF: |
4401 | if (func_id != BPF_FUNC_ringbuf_output && | |
4402 | func_id != BPF_FUNC_ringbuf_reserve && | |
4403 | func_id != BPF_FUNC_ringbuf_submit && | |
4404 | func_id != BPF_FUNC_ringbuf_discard && | |
4405 | func_id != BPF_FUNC_ringbuf_query) | |
4406 | goto error; | |
4407 | break; | |
6aff67c8 AS |
4408 | case BPF_MAP_TYPE_STACK_TRACE: |
4409 | if (func_id != BPF_FUNC_get_stackid) | |
4410 | goto error; | |
4411 | break; | |
4ed8ec52 | 4412 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 4413 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 4414 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
4415 | goto error; |
4416 | break; | |
cd339431 | 4417 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 4418 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
4419 | if (func_id != BPF_FUNC_get_local_storage) |
4420 | goto error; | |
4421 | break; | |
546ac1ff | 4422 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 4423 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
4424 | if (func_id != BPF_FUNC_redirect_map && |
4425 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
4426 | goto error; |
4427 | break; | |
fbfc504a BT |
4428 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
4429 | * appear. | |
4430 | */ | |
6710e112 JDB |
4431 | case BPF_MAP_TYPE_CPUMAP: |
4432 | if (func_id != BPF_FUNC_redirect_map) | |
4433 | goto error; | |
4434 | break; | |
fada7fdc JL |
4435 | case BPF_MAP_TYPE_XSKMAP: |
4436 | if (func_id != BPF_FUNC_redirect_map && | |
4437 | func_id != BPF_FUNC_map_lookup_elem) | |
4438 | goto error; | |
4439 | break; | |
56f668df | 4440 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 4441 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
4442 | if (func_id != BPF_FUNC_map_lookup_elem) |
4443 | goto error; | |
16a43625 | 4444 | break; |
174a79ff JF |
4445 | case BPF_MAP_TYPE_SOCKMAP: |
4446 | if (func_id != BPF_FUNC_sk_redirect_map && | |
4447 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 4448 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 4449 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 4450 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
4451 | func_id != BPF_FUNC_map_lookup_elem && |
4452 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
4453 | goto error; |
4454 | break; | |
81110384 JF |
4455 | case BPF_MAP_TYPE_SOCKHASH: |
4456 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
4457 | func_id != BPF_FUNC_sock_hash_update && | |
4458 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 4459 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 4460 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
4461 | func_id != BPF_FUNC_map_lookup_elem && |
4462 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
4463 | goto error; |
4464 | break; | |
2dbb9b9e MKL |
4465 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
4466 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
4467 | goto error; | |
4468 | break; | |
f1a2e44a MV |
4469 | case BPF_MAP_TYPE_QUEUE: |
4470 | case BPF_MAP_TYPE_STACK: | |
4471 | if (func_id != BPF_FUNC_map_peek_elem && | |
4472 | func_id != BPF_FUNC_map_pop_elem && | |
4473 | func_id != BPF_FUNC_map_push_elem) | |
4474 | goto error; | |
4475 | break; | |
6ac99e8f MKL |
4476 | case BPF_MAP_TYPE_SK_STORAGE: |
4477 | if (func_id != BPF_FUNC_sk_storage_get && | |
4478 | func_id != BPF_FUNC_sk_storage_delete) | |
4479 | goto error; | |
4480 | break; | |
8ea63684 KS |
4481 | case BPF_MAP_TYPE_INODE_STORAGE: |
4482 | if (func_id != BPF_FUNC_inode_storage_get && | |
4483 | func_id != BPF_FUNC_inode_storage_delete) | |
4484 | goto error; | |
4485 | break; | |
4cf1bc1f KS |
4486 | case BPF_MAP_TYPE_TASK_STORAGE: |
4487 | if (func_id != BPF_FUNC_task_storage_get && | |
4488 | func_id != BPF_FUNC_task_storage_delete) | |
4489 | goto error; | |
4490 | break; | |
6aff67c8 AS |
4491 | default: |
4492 | break; | |
4493 | } | |
4494 | ||
4495 | /* ... and second from the function itself. */ | |
4496 | switch (func_id) { | |
4497 | case BPF_FUNC_tail_call: | |
4498 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
4499 | goto error; | |
e411901c MF |
4500 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
4501 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
4502 | return -EINVAL; |
4503 | } | |
6aff67c8 AS |
4504 | break; |
4505 | case BPF_FUNC_perf_event_read: | |
4506 | case BPF_FUNC_perf_event_output: | |
908432ca | 4507 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 4508 | case BPF_FUNC_skb_output: |
d831ee84 | 4509 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
4510 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
4511 | goto error; | |
4512 | break; | |
4513 | case BPF_FUNC_get_stackid: | |
4514 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
4515 | goto error; | |
4516 | break; | |
60d20f91 | 4517 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 4518 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
4519 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
4520 | goto error; | |
4521 | break; | |
97f91a7c | 4522 | case BPF_FUNC_redirect_map: |
9c270af3 | 4523 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 4524 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
4525 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
4526 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
4527 | goto error; |
4528 | break; | |
174a79ff | 4529 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 4530 | case BPF_FUNC_msg_redirect_map: |
81110384 | 4531 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
4532 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
4533 | goto error; | |
4534 | break; | |
81110384 JF |
4535 | case BPF_FUNC_sk_redirect_hash: |
4536 | case BPF_FUNC_msg_redirect_hash: | |
4537 | case BPF_FUNC_sock_hash_update: | |
4538 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
4539 | goto error; |
4540 | break; | |
cd339431 | 4541 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
4542 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
4543 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
4544 | goto error; |
4545 | break; | |
2dbb9b9e | 4546 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
4547 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
4548 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
4549 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
4550 | goto error; |
4551 | break; | |
f1a2e44a MV |
4552 | case BPF_FUNC_map_peek_elem: |
4553 | case BPF_FUNC_map_pop_elem: | |
4554 | case BPF_FUNC_map_push_elem: | |
4555 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
4556 | map->map_type != BPF_MAP_TYPE_STACK) | |
4557 | goto error; | |
4558 | break; | |
6ac99e8f MKL |
4559 | case BPF_FUNC_sk_storage_get: |
4560 | case BPF_FUNC_sk_storage_delete: | |
4561 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
4562 | goto error; | |
4563 | break; | |
8ea63684 KS |
4564 | case BPF_FUNC_inode_storage_get: |
4565 | case BPF_FUNC_inode_storage_delete: | |
4566 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
4567 | goto error; | |
4568 | break; | |
4cf1bc1f KS |
4569 | case BPF_FUNC_task_storage_get: |
4570 | case BPF_FUNC_task_storage_delete: | |
4571 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
4572 | goto error; | |
4573 | break; | |
6aff67c8 AS |
4574 | default: |
4575 | break; | |
35578d79 KX |
4576 | } |
4577 | ||
4578 | return 0; | |
6aff67c8 | 4579 | error: |
61bd5218 | 4580 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 4581 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 4582 | return -EINVAL; |
35578d79 KX |
4583 | } |
4584 | ||
90133415 | 4585 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
4586 | { |
4587 | int count = 0; | |
4588 | ||
39f19ebb | 4589 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4590 | count++; |
39f19ebb | 4591 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4592 | count++; |
39f19ebb | 4593 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4594 | count++; |
39f19ebb | 4595 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4596 | count++; |
39f19ebb | 4597 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
4598 | count++; |
4599 | ||
90133415 DB |
4600 | /* We only support one arg being in raw mode at the moment, |
4601 | * which is sufficient for the helper functions we have | |
4602 | * right now. | |
4603 | */ | |
4604 | return count <= 1; | |
4605 | } | |
4606 | ||
4607 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
4608 | enum bpf_arg_type arg_next) | |
4609 | { | |
4610 | return (arg_type_is_mem_ptr(arg_curr) && | |
4611 | !arg_type_is_mem_size(arg_next)) || | |
4612 | (!arg_type_is_mem_ptr(arg_curr) && | |
4613 | arg_type_is_mem_size(arg_next)); | |
4614 | } | |
4615 | ||
4616 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
4617 | { | |
4618 | /* bpf_xxx(..., buf, len) call will access 'len' | |
4619 | * bytes from memory 'buf'. Both arg types need | |
4620 | * to be paired, so make sure there's no buggy | |
4621 | * helper function specification. | |
4622 | */ | |
4623 | if (arg_type_is_mem_size(fn->arg1_type) || | |
4624 | arg_type_is_mem_ptr(fn->arg5_type) || | |
4625 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
4626 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
4627 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
4628 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
4629 | return false; | |
4630 | ||
4631 | return true; | |
4632 | } | |
4633 | ||
1b986589 | 4634 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
4635 | { |
4636 | int count = 0; | |
4637 | ||
1b986589 | 4638 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 4639 | count++; |
1b986589 | 4640 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 4641 | count++; |
1b986589 | 4642 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 4643 | count++; |
1b986589 | 4644 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 4645 | count++; |
1b986589 | 4646 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
4647 | count++; |
4648 | ||
1b986589 MKL |
4649 | /* A reference acquiring function cannot acquire |
4650 | * another refcounted ptr. | |
4651 | */ | |
64d85290 | 4652 | if (may_be_acquire_function(func_id) && count) |
1b986589 MKL |
4653 | return false; |
4654 | ||
fd978bf7 JS |
4655 | /* We only support one arg being unreferenced at the moment, |
4656 | * which is sufficient for the helper functions we have right now. | |
4657 | */ | |
4658 | return count <= 1; | |
4659 | } | |
4660 | ||
9436ef6e LB |
4661 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
4662 | { | |
4663 | int i; | |
4664 | ||
1df8f55a | 4665 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
9436ef6e LB |
4666 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
4667 | return false; | |
4668 | ||
1df8f55a MKL |
4669 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
4670 | return false; | |
4671 | } | |
4672 | ||
9436ef6e LB |
4673 | return true; |
4674 | } | |
4675 | ||
1b986589 | 4676 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
4677 | { |
4678 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 4679 | check_arg_pair_ok(fn) && |
9436ef6e | 4680 | check_btf_id_ok(fn) && |
1b986589 | 4681 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
4682 | } |
4683 | ||
de8f3a83 DB |
4684 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
4685 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 4686 | */ |
f4d7e40a AS |
4687 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
4688 | struct bpf_func_state *state) | |
969bf05e | 4689 | { |
58e2af8b | 4690 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
4691 | int i; |
4692 | ||
4693 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 4694 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 4695 | mark_reg_unknown(env, regs, i); |
969bf05e | 4696 | |
f3709f69 JS |
4697 | bpf_for_each_spilled_reg(i, state, reg) { |
4698 | if (!reg) | |
969bf05e | 4699 | continue; |
de8f3a83 | 4700 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 4701 | __mark_reg_unknown(env, reg); |
969bf05e AS |
4702 | } |
4703 | } | |
4704 | ||
f4d7e40a AS |
4705 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
4706 | { | |
4707 | struct bpf_verifier_state *vstate = env->cur_state; | |
4708 | int i; | |
4709 | ||
4710 | for (i = 0; i <= vstate->curframe; i++) | |
4711 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
4712 | } | |
4713 | ||
6d94e741 AS |
4714 | enum { |
4715 | AT_PKT_END = -1, | |
4716 | BEYOND_PKT_END = -2, | |
4717 | }; | |
4718 | ||
4719 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) | |
4720 | { | |
4721 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4722 | struct bpf_reg_state *reg = &state->regs[regn]; | |
4723 | ||
4724 | if (reg->type != PTR_TO_PACKET) | |
4725 | /* PTR_TO_PACKET_META is not supported yet */ | |
4726 | return; | |
4727 | ||
4728 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. | |
4729 | * How far beyond pkt_end it goes is unknown. | |
4730 | * if (!range_open) it's the case of pkt >= pkt_end | |
4731 | * if (range_open) it's the case of pkt > pkt_end | |
4732 | * hence this pointer is at least 1 byte bigger than pkt_end | |
4733 | */ | |
4734 | if (range_open) | |
4735 | reg->range = BEYOND_PKT_END; | |
4736 | else | |
4737 | reg->range = AT_PKT_END; | |
4738 | } | |
4739 | ||
fd978bf7 | 4740 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
4741 | struct bpf_func_state *state, |
4742 | int ref_obj_id) | |
fd978bf7 JS |
4743 | { |
4744 | struct bpf_reg_state *regs = state->regs, *reg; | |
4745 | int i; | |
4746 | ||
4747 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 4748 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
4749 | mark_reg_unknown(env, regs, i); |
4750 | ||
4751 | bpf_for_each_spilled_reg(i, state, reg) { | |
4752 | if (!reg) | |
4753 | continue; | |
1b986589 | 4754 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 4755 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
4756 | } |
4757 | } | |
4758 | ||
4759 | /* The pointer with the specified id has released its reference to kernel | |
4760 | * resources. Identify all copies of the same pointer and clear the reference. | |
4761 | */ | |
4762 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 4763 | int ref_obj_id) |
fd978bf7 JS |
4764 | { |
4765 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 4766 | int err; |
fd978bf7 JS |
4767 | int i; |
4768 | ||
1b986589 MKL |
4769 | err = release_reference_state(cur_func(env), ref_obj_id); |
4770 | if (err) | |
4771 | return err; | |
4772 | ||
fd978bf7 | 4773 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 4774 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 4775 | |
1b986589 | 4776 | return 0; |
fd978bf7 JS |
4777 | } |
4778 | ||
51c39bb1 AS |
4779 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
4780 | struct bpf_reg_state *regs) | |
4781 | { | |
4782 | int i; | |
4783 | ||
4784 | /* after the call registers r0 - r5 were scratched */ | |
4785 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
4786 | mark_reg_not_init(env, regs, caller_saved[i]); | |
4787 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
4788 | } | |
4789 | } | |
4790 | ||
f4d7e40a AS |
4791 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
4792 | int *insn_idx) | |
4793 | { | |
4794 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 4795 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 4796 | struct bpf_func_state *caller, *callee; |
fd978bf7 | 4797 | int i, err, subprog, target_insn; |
51c39bb1 | 4798 | bool is_global = false; |
f4d7e40a | 4799 | |
aada9ce6 | 4800 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 4801 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 4802 | state->curframe + 2); |
f4d7e40a AS |
4803 | return -E2BIG; |
4804 | } | |
4805 | ||
4806 | target_insn = *insn_idx + insn->imm; | |
4807 | subprog = find_subprog(env, target_insn + 1); | |
4808 | if (subprog < 0) { | |
4809 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
4810 | target_insn + 1); | |
4811 | return -EFAULT; | |
4812 | } | |
4813 | ||
4814 | caller = state->frame[state->curframe]; | |
4815 | if (state->frame[state->curframe + 1]) { | |
4816 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
4817 | state->curframe + 1); | |
4818 | return -EFAULT; | |
4819 | } | |
4820 | ||
51c39bb1 AS |
4821 | func_info_aux = env->prog->aux->func_info_aux; |
4822 | if (func_info_aux) | |
4823 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
4824 | err = btf_check_func_arg_match(env, subprog, caller->regs); | |
4825 | if (err == -EFAULT) | |
4826 | return err; | |
4827 | if (is_global) { | |
4828 | if (err) { | |
4829 | verbose(env, "Caller passes invalid args into func#%d\n", | |
4830 | subprog); | |
4831 | return err; | |
4832 | } else { | |
4833 | if (env->log.level & BPF_LOG_LEVEL) | |
4834 | verbose(env, | |
4835 | "Func#%d is global and valid. Skipping.\n", | |
4836 | subprog); | |
4837 | clear_caller_saved_regs(env, caller->regs); | |
4838 | ||
4839 | /* All global functions return SCALAR_VALUE */ | |
4840 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
4841 | ||
4842 | /* continue with next insn after call */ | |
4843 | return 0; | |
4844 | } | |
4845 | } | |
4846 | ||
f4d7e40a AS |
4847 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
4848 | if (!callee) | |
4849 | return -ENOMEM; | |
4850 | state->frame[state->curframe + 1] = callee; | |
4851 | ||
4852 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
4853 | * into its own stack before reading from it. | |
4854 | * callee can read/write into caller's stack | |
4855 | */ | |
4856 | init_func_state(env, callee, | |
4857 | /* remember the callsite, it will be used by bpf_exit */ | |
4858 | *insn_idx /* callsite */, | |
4859 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 4860 | subprog /* subprog number within this prog */); |
f4d7e40a | 4861 | |
fd978bf7 JS |
4862 | /* Transfer references to the callee */ |
4863 | err = transfer_reference_state(callee, caller); | |
4864 | if (err) | |
4865 | return err; | |
4866 | ||
679c782d EC |
4867 | /* copy r1 - r5 args that callee can access. The copy includes parent |
4868 | * pointers, which connects us up to the liveness chain | |
4869 | */ | |
f4d7e40a AS |
4870 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
4871 | callee->regs[i] = caller->regs[i]; | |
4872 | ||
51c39bb1 | 4873 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
4874 | |
4875 | /* only increment it after check_reg_arg() finished */ | |
4876 | state->curframe++; | |
4877 | ||
4878 | /* and go analyze first insn of the callee */ | |
4879 | *insn_idx = target_insn; | |
4880 | ||
06ee7115 | 4881 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4882 | verbose(env, "caller:\n"); |
4883 | print_verifier_state(env, caller); | |
4884 | verbose(env, "callee:\n"); | |
4885 | print_verifier_state(env, callee); | |
4886 | } | |
4887 | return 0; | |
4888 | } | |
4889 | ||
4890 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
4891 | { | |
4892 | struct bpf_verifier_state *state = env->cur_state; | |
4893 | struct bpf_func_state *caller, *callee; | |
4894 | struct bpf_reg_state *r0; | |
fd978bf7 | 4895 | int err; |
f4d7e40a AS |
4896 | |
4897 | callee = state->frame[state->curframe]; | |
4898 | r0 = &callee->regs[BPF_REG_0]; | |
4899 | if (r0->type == PTR_TO_STACK) { | |
4900 | /* technically it's ok to return caller's stack pointer | |
4901 | * (or caller's caller's pointer) back to the caller, | |
4902 | * since these pointers are valid. Only current stack | |
4903 | * pointer will be invalid as soon as function exits, | |
4904 | * but let's be conservative | |
4905 | */ | |
4906 | verbose(env, "cannot return stack pointer to the caller\n"); | |
4907 | return -EINVAL; | |
4908 | } | |
4909 | ||
4910 | state->curframe--; | |
4911 | caller = state->frame[state->curframe]; | |
4912 | /* return to the caller whatever r0 had in the callee */ | |
4913 | caller->regs[BPF_REG_0] = *r0; | |
4914 | ||
fd978bf7 JS |
4915 | /* Transfer references to the caller */ |
4916 | err = transfer_reference_state(caller, callee); | |
4917 | if (err) | |
4918 | return err; | |
4919 | ||
f4d7e40a | 4920 | *insn_idx = callee->callsite + 1; |
06ee7115 | 4921 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4922 | verbose(env, "returning from callee:\n"); |
4923 | print_verifier_state(env, callee); | |
4924 | verbose(env, "to caller at %d:\n", *insn_idx); | |
4925 | print_verifier_state(env, caller); | |
4926 | } | |
4927 | /* clear everything in the callee */ | |
4928 | free_func_state(callee); | |
4929 | state->frame[state->curframe + 1] = NULL; | |
4930 | return 0; | |
4931 | } | |
4932 | ||
849fa506 YS |
4933 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
4934 | int func_id, | |
4935 | struct bpf_call_arg_meta *meta) | |
4936 | { | |
4937 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
4938 | ||
4939 | if (ret_type != RET_INTEGER || | |
4940 | (func_id != BPF_FUNC_get_stack && | |
47cc0ed5 DB |
4941 | func_id != BPF_FUNC_probe_read_str && |
4942 | func_id != BPF_FUNC_probe_read_kernel_str && | |
4943 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
4944 | return; |
4945 | ||
10060503 | 4946 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 4947 | ret_reg->s32_max_value = meta->msize_max_value; |
b0270958 AS |
4948 | ret_reg->smin_value = -MAX_ERRNO; |
4949 | ret_reg->s32_min_value = -MAX_ERRNO; | |
849fa506 YS |
4950 | __reg_deduce_bounds(ret_reg); |
4951 | __reg_bound_offset(ret_reg); | |
10060503 | 4952 | __update_reg_bounds(ret_reg); |
849fa506 YS |
4953 | } |
4954 | ||
c93552c4 DB |
4955 | static int |
4956 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4957 | int func_id, int insn_idx) | |
4958 | { | |
4959 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 4960 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
4961 | |
4962 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
4963 | func_id != BPF_FUNC_map_lookup_elem && |
4964 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
4965 | func_id != BPF_FUNC_map_delete_elem && |
4966 | func_id != BPF_FUNC_map_push_elem && | |
4967 | func_id != BPF_FUNC_map_pop_elem && | |
4968 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 4969 | return 0; |
09772d92 | 4970 | |
591fe988 | 4971 | if (map == NULL) { |
c93552c4 DB |
4972 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
4973 | return -EINVAL; | |
4974 | } | |
4975 | ||
591fe988 DB |
4976 | /* In case of read-only, some additional restrictions |
4977 | * need to be applied in order to prevent altering the | |
4978 | * state of the map from program side. | |
4979 | */ | |
4980 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
4981 | (func_id == BPF_FUNC_map_delete_elem || | |
4982 | func_id == BPF_FUNC_map_update_elem || | |
4983 | func_id == BPF_FUNC_map_push_elem || | |
4984 | func_id == BPF_FUNC_map_pop_elem)) { | |
4985 | verbose(env, "write into map forbidden\n"); | |
4986 | return -EACCES; | |
4987 | } | |
4988 | ||
d2e4c1e6 | 4989 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 4990 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 4991 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 4992 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 4993 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 4994 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
4995 | return 0; |
4996 | } | |
4997 | ||
d2e4c1e6 DB |
4998 | static int |
4999 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
5000 | int func_id, int insn_idx) | |
5001 | { | |
5002 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
5003 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
5004 | struct bpf_map *map = meta->map_ptr; | |
5005 | struct tnum range; | |
5006 | u64 val; | |
cc52d914 | 5007 | int err; |
d2e4c1e6 DB |
5008 | |
5009 | if (func_id != BPF_FUNC_tail_call) | |
5010 | return 0; | |
5011 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
5012 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
5013 | return -EINVAL; | |
5014 | } | |
5015 | ||
5016 | range = tnum_range(0, map->max_entries - 1); | |
5017 | reg = ®s[BPF_REG_3]; | |
5018 | ||
5019 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
5020 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
5021 | return 0; | |
5022 | } | |
5023 | ||
cc52d914 DB |
5024 | err = mark_chain_precision(env, BPF_REG_3); |
5025 | if (err) | |
5026 | return err; | |
5027 | ||
d2e4c1e6 DB |
5028 | val = reg->var_off.value; |
5029 | if (bpf_map_key_unseen(aux)) | |
5030 | bpf_map_key_store(aux, val); | |
5031 | else if (!bpf_map_key_poisoned(aux) && | |
5032 | bpf_map_key_immediate(aux) != val) | |
5033 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
5034 | return 0; | |
5035 | } | |
5036 | ||
fd978bf7 JS |
5037 | static int check_reference_leak(struct bpf_verifier_env *env) |
5038 | { | |
5039 | struct bpf_func_state *state = cur_func(env); | |
5040 | int i; | |
5041 | ||
5042 | for (i = 0; i < state->acquired_refs; i++) { | |
5043 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
5044 | state->refs[i].id, state->refs[i].insn_idx); | |
5045 | } | |
5046 | return state->acquired_refs ? -EINVAL : 0; | |
5047 | } | |
5048 | ||
f4d7e40a | 5049 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 5050 | { |
17a52670 | 5051 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 5052 | struct bpf_reg_state *regs; |
33ff9823 | 5053 | struct bpf_call_arg_meta meta; |
969bf05e | 5054 | bool changes_data; |
17a52670 AS |
5055 | int i, err; |
5056 | ||
5057 | /* find function prototype */ | |
5058 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
5059 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
5060 | func_id); | |
17a52670 AS |
5061 | return -EINVAL; |
5062 | } | |
5063 | ||
00176a34 | 5064 | if (env->ops->get_func_proto) |
5e43f899 | 5065 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 5066 | if (!fn) { |
61bd5218 JK |
5067 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
5068 | func_id); | |
17a52670 AS |
5069 | return -EINVAL; |
5070 | } | |
5071 | ||
5072 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 5073 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 5074 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
5075 | return -EINVAL; |
5076 | } | |
5077 | ||
eae2e83e JO |
5078 | if (fn->allowed && !fn->allowed(env->prog)) { |
5079 | verbose(env, "helper call is not allowed in probe\n"); | |
5080 | return -EINVAL; | |
5081 | } | |
5082 | ||
04514d13 | 5083 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 5084 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
5085 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
5086 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
5087 | func_id_name(func_id), func_id); | |
5088 | return -EINVAL; | |
5089 | } | |
969bf05e | 5090 | |
33ff9823 | 5091 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 5092 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 5093 | |
1b986589 | 5094 | err = check_func_proto(fn, func_id); |
435faee1 | 5095 | if (err) { |
61bd5218 | 5096 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 5097 | func_id_name(func_id), func_id); |
435faee1 DB |
5098 | return err; |
5099 | } | |
5100 | ||
d83525ca | 5101 | meta.func_id = func_id; |
17a52670 | 5102 | /* check args */ |
a7658e1a | 5103 | for (i = 0; i < 5; i++) { |
af7ec138 | 5104 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
5105 | if (err) |
5106 | return err; | |
5107 | } | |
17a52670 | 5108 | |
c93552c4 DB |
5109 | err = record_func_map(env, &meta, func_id, insn_idx); |
5110 | if (err) | |
5111 | return err; | |
5112 | ||
d2e4c1e6 DB |
5113 | err = record_func_key(env, &meta, func_id, insn_idx); |
5114 | if (err) | |
5115 | return err; | |
5116 | ||
435faee1 DB |
5117 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
5118 | * is inferred from register state. | |
5119 | */ | |
5120 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
5121 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
5122 | BPF_WRITE, -1, false); | |
435faee1 DB |
5123 | if (err) |
5124 | return err; | |
5125 | } | |
5126 | ||
fd978bf7 JS |
5127 | if (func_id == BPF_FUNC_tail_call) { |
5128 | err = check_reference_leak(env); | |
5129 | if (err) { | |
5130 | verbose(env, "tail_call would lead to reference leak\n"); | |
5131 | return err; | |
5132 | } | |
5133 | } else if (is_release_function(func_id)) { | |
1b986589 | 5134 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
5135 | if (err) { |
5136 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
5137 | func_id_name(func_id), func_id); | |
fd978bf7 | 5138 | return err; |
46f8bc92 | 5139 | } |
fd978bf7 JS |
5140 | } |
5141 | ||
638f5b90 | 5142 | regs = cur_regs(env); |
cd339431 RG |
5143 | |
5144 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
5145 | * this is required because get_local_storage() can't return an error. | |
5146 | */ | |
5147 | if (func_id == BPF_FUNC_get_local_storage && | |
5148 | !register_is_null(®s[BPF_REG_2])) { | |
5149 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
5150 | return -EINVAL; | |
5151 | } | |
5152 | ||
17a52670 | 5153 | /* reset caller saved regs */ |
dc503a8a | 5154 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 5155 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
5156 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
5157 | } | |
17a52670 | 5158 | |
5327ed3d JW |
5159 | /* helper call returns 64-bit value. */ |
5160 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
5161 | ||
dc503a8a | 5162 | /* update return register (already marked as written above) */ |
17a52670 | 5163 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 5164 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 5165 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
5166 | } else if (fn->ret_type == RET_VOID) { |
5167 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
5168 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
5169 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 5170 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 5171 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
5172 | /* remember map_ptr, so that check_map_access() |
5173 | * can check 'value_size' boundary of memory access | |
5174 | * to map element returned from bpf_map_lookup_elem() | |
5175 | */ | |
33ff9823 | 5176 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
5177 | verbose(env, |
5178 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
5179 | return -EINVAL; |
5180 | } | |
33ff9823 | 5181 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
5182 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
5183 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
5184 | if (map_value_has_spin_lock(meta.map_ptr)) |
5185 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
5186 | } else { |
5187 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
4d31f301 | 5188 | } |
c64b7983 JS |
5189 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
5190 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5191 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
85a51f8c LB |
5192 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
5193 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5194 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
655a51e5 MKL |
5195 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
5196 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5197 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
457f4436 AN |
5198 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
5199 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5200 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; | |
457f4436 | 5201 | regs[BPF_REG_0].mem_size = meta.mem_size; |
63d9b80d HL |
5202 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
5203 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { | |
eaa6bcb7 HL |
5204 | const struct btf_type *t; |
5205 | ||
5206 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
22dc4a0f | 5207 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
eaa6bcb7 HL |
5208 | if (!btf_type_is_struct(t)) { |
5209 | u32 tsize; | |
5210 | const struct btf_type *ret; | |
5211 | const char *tname; | |
5212 | ||
5213 | /* resolve the type size of ksym. */ | |
22dc4a0f | 5214 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
eaa6bcb7 | 5215 | if (IS_ERR(ret)) { |
22dc4a0f | 5216 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
eaa6bcb7 HL |
5217 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
5218 | tname, PTR_ERR(ret)); | |
5219 | return -EINVAL; | |
5220 | } | |
63d9b80d HL |
5221 | regs[BPF_REG_0].type = |
5222 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
5223 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; | |
eaa6bcb7 HL |
5224 | regs[BPF_REG_0].mem_size = tsize; |
5225 | } else { | |
63d9b80d HL |
5226 | regs[BPF_REG_0].type = |
5227 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
5228 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; | |
22dc4a0f | 5229 | regs[BPF_REG_0].btf = meta.ret_btf; |
eaa6bcb7 HL |
5230 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
5231 | } | |
3ca1032a KS |
5232 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL || |
5233 | fn->ret_type == RET_PTR_TO_BTF_ID) { | |
af7ec138 YS |
5234 | int ret_btf_id; |
5235 | ||
5236 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
3ca1032a KS |
5237 | regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ? |
5238 | PTR_TO_BTF_ID : | |
5239 | PTR_TO_BTF_ID_OR_NULL; | |
af7ec138 YS |
5240 | ret_btf_id = *fn->ret_btf_id; |
5241 | if (ret_btf_id == 0) { | |
5242 | verbose(env, "invalid return type %d of func %s#%d\n", | |
5243 | fn->ret_type, func_id_name(func_id), func_id); | |
5244 | return -EINVAL; | |
5245 | } | |
22dc4a0f AN |
5246 | /* current BPF helper definitions are only coming from |
5247 | * built-in code with type IDs from vmlinux BTF | |
5248 | */ | |
5249 | regs[BPF_REG_0].btf = btf_vmlinux; | |
af7ec138 | 5250 | regs[BPF_REG_0].btf_id = ret_btf_id; |
17a52670 | 5251 | } else { |
61bd5218 | 5252 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 5253 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
5254 | return -EINVAL; |
5255 | } | |
04fd61ab | 5256 | |
93c230e3 MKL |
5257 | if (reg_type_may_be_null(regs[BPF_REG_0].type)) |
5258 | regs[BPF_REG_0].id = ++env->id_gen; | |
5259 | ||
0f3adc28 | 5260 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
5261 | /* For release_reference() */ |
5262 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 5263 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
5264 | int id = acquire_reference_state(env, insn_idx); |
5265 | ||
5266 | if (id < 0) | |
5267 | return id; | |
5268 | /* For mark_ptr_or_null_reg() */ | |
5269 | regs[BPF_REG_0].id = id; | |
5270 | /* For release_reference() */ | |
5271 | regs[BPF_REG_0].ref_obj_id = id; | |
5272 | } | |
1b986589 | 5273 | |
849fa506 YS |
5274 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
5275 | ||
61bd5218 | 5276 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
5277 | if (err) |
5278 | return err; | |
04fd61ab | 5279 | |
fa28dcb8 SL |
5280 | if ((func_id == BPF_FUNC_get_stack || |
5281 | func_id == BPF_FUNC_get_task_stack) && | |
5282 | !env->prog->has_callchain_buf) { | |
c195651e YS |
5283 | const char *err_str; |
5284 | ||
5285 | #ifdef CONFIG_PERF_EVENTS | |
5286 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
5287 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
5288 | #else | |
5289 | err = -ENOTSUPP; | |
5290 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
5291 | #endif | |
5292 | if (err) { | |
5293 | verbose(env, err_str, func_id_name(func_id), func_id); | |
5294 | return err; | |
5295 | } | |
5296 | ||
5297 | env->prog->has_callchain_buf = true; | |
5298 | } | |
5299 | ||
5d99cb2c SL |
5300 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
5301 | env->prog->call_get_stack = true; | |
5302 | ||
969bf05e AS |
5303 | if (changes_data) |
5304 | clear_all_pkt_pointers(env); | |
5305 | return 0; | |
5306 | } | |
5307 | ||
b03c9f9f EC |
5308 | static bool signed_add_overflows(s64 a, s64 b) |
5309 | { | |
5310 | /* Do the add in u64, where overflow is well-defined */ | |
5311 | s64 res = (s64)((u64)a + (u64)b); | |
5312 | ||
5313 | if (b < 0) | |
5314 | return res > a; | |
5315 | return res < a; | |
5316 | } | |
5317 | ||
3f50f132 JF |
5318 | static bool signed_add32_overflows(s64 a, s64 b) |
5319 | { | |
5320 | /* Do the add in u32, where overflow is well-defined */ | |
5321 | s32 res = (s32)((u32)a + (u32)b); | |
5322 | ||
5323 | if (b < 0) | |
5324 | return res > a; | |
5325 | return res < a; | |
5326 | } | |
5327 | ||
5328 | static bool signed_sub_overflows(s32 a, s32 b) | |
b03c9f9f EC |
5329 | { |
5330 | /* Do the sub in u64, where overflow is well-defined */ | |
5331 | s64 res = (s64)((u64)a - (u64)b); | |
5332 | ||
5333 | if (b < 0) | |
5334 | return res < a; | |
5335 | return res > a; | |
969bf05e AS |
5336 | } |
5337 | ||
3f50f132 JF |
5338 | static bool signed_sub32_overflows(s32 a, s32 b) |
5339 | { | |
5340 | /* Do the sub in u64, where overflow is well-defined */ | |
5341 | s32 res = (s32)((u32)a - (u32)b); | |
5342 | ||
5343 | if (b < 0) | |
5344 | return res < a; | |
5345 | return res > a; | |
5346 | } | |
5347 | ||
bb7f0f98 AS |
5348 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
5349 | const struct bpf_reg_state *reg, | |
5350 | enum bpf_reg_type type) | |
5351 | { | |
5352 | bool known = tnum_is_const(reg->var_off); | |
5353 | s64 val = reg->var_off.value; | |
5354 | s64 smin = reg->smin_value; | |
5355 | ||
5356 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
5357 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
5358 | reg_type_str[type], val); | |
5359 | return false; | |
5360 | } | |
5361 | ||
5362 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
5363 | verbose(env, "%s pointer offset %d is not allowed\n", | |
5364 | reg_type_str[type], reg->off); | |
5365 | return false; | |
5366 | } | |
5367 | ||
5368 | if (smin == S64_MIN) { | |
5369 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
5370 | reg_type_str[type]); | |
5371 | return false; | |
5372 | } | |
5373 | ||
5374 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
5375 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
5376 | smin, reg_type_str[type]); | |
5377 | return false; | |
5378 | } | |
5379 | ||
5380 | return true; | |
5381 | } | |
5382 | ||
979d63d5 DB |
5383 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
5384 | { | |
5385 | return &env->insn_aux_data[env->insn_idx]; | |
5386 | } | |
5387 | ||
5388 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
5389 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
5390 | { | |
5391 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
5392 | (opcode == BPF_SUB && !off_is_neg); | |
5393 | u32 off; | |
5394 | ||
5395 | switch (ptr_reg->type) { | |
5396 | case PTR_TO_STACK: | |
088ec26d AI |
5397 | /* Indirect variable offset stack access is prohibited in |
5398 | * unprivileged mode so it's not handled here. | |
5399 | */ | |
979d63d5 DB |
5400 | off = ptr_reg->off + ptr_reg->var_off.value; |
5401 | if (mask_to_left) | |
5402 | *ptr_limit = MAX_BPF_STACK + off; | |
5403 | else | |
5404 | *ptr_limit = -off; | |
5405 | return 0; | |
5406 | case PTR_TO_MAP_VALUE: | |
5407 | if (mask_to_left) { | |
5408 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
5409 | } else { | |
5410 | off = ptr_reg->smin_value + ptr_reg->off; | |
5411 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
5412 | } | |
5413 | return 0; | |
5414 | default: | |
5415 | return -EINVAL; | |
5416 | } | |
5417 | } | |
5418 | ||
d3bd7413 DB |
5419 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
5420 | const struct bpf_insn *insn) | |
5421 | { | |
2c78ee89 | 5422 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
5423 | } |
5424 | ||
5425 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
5426 | u32 alu_state, u32 alu_limit) | |
5427 | { | |
5428 | /* If we arrived here from different branches with different | |
5429 | * state or limits to sanitize, then this won't work. | |
5430 | */ | |
5431 | if (aux->alu_state && | |
5432 | (aux->alu_state != alu_state || | |
5433 | aux->alu_limit != alu_limit)) | |
5434 | return -EACCES; | |
5435 | ||
5436 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
5437 | aux->alu_state = alu_state; | |
5438 | aux->alu_limit = alu_limit; | |
5439 | return 0; | |
5440 | } | |
5441 | ||
5442 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
5443 | struct bpf_insn *insn) | |
5444 | { | |
5445 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
5446 | ||
5447 | if (can_skip_alu_sanitation(env, insn)) | |
5448 | return 0; | |
5449 | ||
5450 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
5451 | } | |
5452 | ||
979d63d5 DB |
5453 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
5454 | struct bpf_insn *insn, | |
5455 | const struct bpf_reg_state *ptr_reg, | |
5456 | struct bpf_reg_state *dst_reg, | |
5457 | bool off_is_neg) | |
5458 | { | |
5459 | struct bpf_verifier_state *vstate = env->cur_state; | |
5460 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
5461 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
5462 | u8 opcode = BPF_OP(insn->code); | |
5463 | u32 alu_state, alu_limit; | |
5464 | struct bpf_reg_state tmp; | |
5465 | bool ret; | |
5466 | ||
d3bd7413 | 5467 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
5468 | return 0; |
5469 | ||
5470 | /* We already marked aux for masking from non-speculative | |
5471 | * paths, thus we got here in the first place. We only care | |
5472 | * to explore bad access from here. | |
5473 | */ | |
5474 | if (vstate->speculative) | |
5475 | goto do_sim; | |
5476 | ||
5477 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
5478 | alu_state |= ptr_is_dst_reg ? | |
5479 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
5480 | ||
5481 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
5482 | return 0; | |
d3bd7413 | 5483 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 5484 | return -EACCES; |
979d63d5 DB |
5485 | do_sim: |
5486 | /* Simulate and find potential out-of-bounds access under | |
5487 | * speculative execution from truncation as a result of | |
5488 | * masking when off was not within expected range. If off | |
5489 | * sits in dst, then we temporarily need to move ptr there | |
5490 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
5491 | * for cases where we use K-based arithmetic in one direction | |
5492 | * and truncated reg-based in the other in order to explore | |
5493 | * bad access. | |
5494 | */ | |
5495 | if (!ptr_is_dst_reg) { | |
5496 | tmp = *dst_reg; | |
5497 | *dst_reg = *ptr_reg; | |
5498 | } | |
5499 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 5500 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
5501 | *dst_reg = tmp; |
5502 | return !ret ? -EFAULT : 0; | |
5503 | } | |
5504 | ||
f1174f77 | 5505 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
5506 | * Caller should also handle BPF_MOV case separately. |
5507 | * If we return -EACCES, caller may want to try again treating pointer as a | |
5508 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
5509 | */ | |
5510 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
5511 | struct bpf_insn *insn, | |
5512 | const struct bpf_reg_state *ptr_reg, | |
5513 | const struct bpf_reg_state *off_reg) | |
969bf05e | 5514 | { |
f4d7e40a AS |
5515 | struct bpf_verifier_state *vstate = env->cur_state; |
5516 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5517 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 5518 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
5519 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
5520 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
5521 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
5522 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 5523 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 5524 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 5525 | int ret; |
969bf05e | 5526 | |
f1174f77 | 5527 | dst_reg = ®s[dst]; |
969bf05e | 5528 | |
6f16101e DB |
5529 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
5530 | smin_val > smax_val || umin_val > umax_val) { | |
5531 | /* Taint dst register if offset had invalid bounds derived from | |
5532 | * e.g. dead branches. | |
5533 | */ | |
f54c7898 | 5534 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 5535 | return 0; |
f1174f77 EC |
5536 | } |
5537 | ||
5538 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
5539 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
5540 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
5541 | __mark_reg_unknown(env, dst_reg); | |
5542 | return 0; | |
5543 | } | |
5544 | ||
82abbf8d AS |
5545 | verbose(env, |
5546 | "R%d 32-bit pointer arithmetic prohibited\n", | |
5547 | dst); | |
f1174f77 | 5548 | return -EACCES; |
969bf05e AS |
5549 | } |
5550 | ||
aad2eeaf JS |
5551 | switch (ptr_reg->type) { |
5552 | case PTR_TO_MAP_VALUE_OR_NULL: | |
5553 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
5554 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5555 | return -EACCES; |
aad2eeaf | 5556 | case CONST_PTR_TO_MAP: |
7c696732 YS |
5557 | /* smin_val represents the known value */ |
5558 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
5559 | break; | |
8731745e | 5560 | fallthrough; |
aad2eeaf | 5561 | case PTR_TO_PACKET_END: |
c64b7983 JS |
5562 | case PTR_TO_SOCKET: |
5563 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
5564 | case PTR_TO_SOCK_COMMON: |
5565 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
5566 | case PTR_TO_TCP_SOCK: |
5567 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 5568 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
5569 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
5570 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5571 | return -EACCES; |
9d7eceed DB |
5572 | case PTR_TO_MAP_VALUE: |
5573 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
5574 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
5575 | off_reg == dst_reg ? dst : src); | |
5576 | return -EACCES; | |
5577 | } | |
df561f66 | 5578 | fallthrough; |
aad2eeaf JS |
5579 | default: |
5580 | break; | |
f1174f77 EC |
5581 | } |
5582 | ||
5583 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
5584 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 5585 | */ |
f1174f77 EC |
5586 | dst_reg->type = ptr_reg->type; |
5587 | dst_reg->id = ptr_reg->id; | |
969bf05e | 5588 | |
bb7f0f98 AS |
5589 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
5590 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
5591 | return -EINVAL; | |
5592 | ||
3f50f132 JF |
5593 | /* pointer types do not carry 32-bit bounds at the moment. */ |
5594 | __mark_reg32_unbounded(dst_reg); | |
5595 | ||
f1174f77 EC |
5596 | switch (opcode) { |
5597 | case BPF_ADD: | |
979d63d5 DB |
5598 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5599 | if (ret < 0) { | |
5600 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
5601 | return ret; | |
5602 | } | |
f1174f77 EC |
5603 | /* We can take a fixed offset as long as it doesn't overflow |
5604 | * the s32 'off' field | |
969bf05e | 5605 | */ |
b03c9f9f EC |
5606 | if (known && (ptr_reg->off + smin_val == |
5607 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 5608 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
5609 | dst_reg->smin_value = smin_ptr; |
5610 | dst_reg->smax_value = smax_ptr; | |
5611 | dst_reg->umin_value = umin_ptr; | |
5612 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 5613 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 5614 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 5615 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5616 | break; |
5617 | } | |
f1174f77 EC |
5618 | /* A new variable offset is created. Note that off_reg->off |
5619 | * == 0, since it's a scalar. | |
5620 | * dst_reg gets the pointer type and since some positive | |
5621 | * integer value was added to the pointer, give it a new 'id' | |
5622 | * if it's a PTR_TO_PACKET. | |
5623 | * this creates a new 'base' pointer, off_reg (variable) gets | |
5624 | * added into the variable offset, and we copy the fixed offset | |
5625 | * from ptr_reg. | |
969bf05e | 5626 | */ |
b03c9f9f EC |
5627 | if (signed_add_overflows(smin_ptr, smin_val) || |
5628 | signed_add_overflows(smax_ptr, smax_val)) { | |
5629 | dst_reg->smin_value = S64_MIN; | |
5630 | dst_reg->smax_value = S64_MAX; | |
5631 | } else { | |
5632 | dst_reg->smin_value = smin_ptr + smin_val; | |
5633 | dst_reg->smax_value = smax_ptr + smax_val; | |
5634 | } | |
5635 | if (umin_ptr + umin_val < umin_ptr || | |
5636 | umax_ptr + umax_val < umax_ptr) { | |
5637 | dst_reg->umin_value = 0; | |
5638 | dst_reg->umax_value = U64_MAX; | |
5639 | } else { | |
5640 | dst_reg->umin_value = umin_ptr + umin_val; | |
5641 | dst_reg->umax_value = umax_ptr + umax_val; | |
5642 | } | |
f1174f77 EC |
5643 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
5644 | dst_reg->off = ptr_reg->off; | |
0962590e | 5645 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5646 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5647 | dst_reg->id = ++env->id_gen; |
5648 | /* something was added to pkt_ptr, set range to zero */ | |
22dc4a0f | 5649 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
f1174f77 EC |
5650 | } |
5651 | break; | |
5652 | case BPF_SUB: | |
979d63d5 DB |
5653 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5654 | if (ret < 0) { | |
5655 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
5656 | return ret; | |
5657 | } | |
f1174f77 EC |
5658 | if (dst_reg == off_reg) { |
5659 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
5660 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
5661 | dst); | |
f1174f77 EC |
5662 | return -EACCES; |
5663 | } | |
5664 | /* We don't allow subtraction from FP, because (according to | |
5665 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
5666 | * be able to deal with it. | |
969bf05e | 5667 | */ |
f1174f77 | 5668 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
5669 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
5670 | dst); | |
f1174f77 EC |
5671 | return -EACCES; |
5672 | } | |
b03c9f9f EC |
5673 | if (known && (ptr_reg->off - smin_val == |
5674 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 5675 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
5676 | dst_reg->smin_value = smin_ptr; |
5677 | dst_reg->smax_value = smax_ptr; | |
5678 | dst_reg->umin_value = umin_ptr; | |
5679 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
5680 | dst_reg->var_off = ptr_reg->var_off; |
5681 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 5682 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 5683 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5684 | break; |
5685 | } | |
f1174f77 EC |
5686 | /* A new variable offset is created. If the subtrahend is known |
5687 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 5688 | */ |
b03c9f9f EC |
5689 | if (signed_sub_overflows(smin_ptr, smax_val) || |
5690 | signed_sub_overflows(smax_ptr, smin_val)) { | |
5691 | /* Overflow possible, we know nothing */ | |
5692 | dst_reg->smin_value = S64_MIN; | |
5693 | dst_reg->smax_value = S64_MAX; | |
5694 | } else { | |
5695 | dst_reg->smin_value = smin_ptr - smax_val; | |
5696 | dst_reg->smax_value = smax_ptr - smin_val; | |
5697 | } | |
5698 | if (umin_ptr < umax_val) { | |
5699 | /* Overflow possible, we know nothing */ | |
5700 | dst_reg->umin_value = 0; | |
5701 | dst_reg->umax_value = U64_MAX; | |
5702 | } else { | |
5703 | /* Cannot overflow (as long as bounds are consistent) */ | |
5704 | dst_reg->umin_value = umin_ptr - umax_val; | |
5705 | dst_reg->umax_value = umax_ptr - umin_val; | |
5706 | } | |
f1174f77 EC |
5707 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
5708 | dst_reg->off = ptr_reg->off; | |
0962590e | 5709 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5710 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5711 | dst_reg->id = ++env->id_gen; |
5712 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 5713 | if (smin_val < 0) |
22dc4a0f | 5714 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
43188702 | 5715 | } |
f1174f77 EC |
5716 | break; |
5717 | case BPF_AND: | |
5718 | case BPF_OR: | |
5719 | case BPF_XOR: | |
82abbf8d AS |
5720 | /* bitwise ops on pointers are troublesome, prohibit. */ |
5721 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
5722 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
5723 | return -EACCES; |
5724 | default: | |
5725 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
5726 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
5727 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 5728 | return -EACCES; |
43188702 JF |
5729 | } |
5730 | ||
bb7f0f98 AS |
5731 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
5732 | return -EINVAL; | |
5733 | ||
b03c9f9f EC |
5734 | __update_reg_bounds(dst_reg); |
5735 | __reg_deduce_bounds(dst_reg); | |
5736 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
5737 | |
5738 | /* For unprivileged we require that resulting offset must be in bounds | |
5739 | * in order to be able to sanitize access later on. | |
5740 | */ | |
2c78ee89 | 5741 | if (!env->bypass_spec_v1) { |
e4298d25 DB |
5742 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
5743 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
5744 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
5745 | "prohibited for !root\n", dst); | |
5746 | return -EACCES; | |
5747 | } else if (dst_reg->type == PTR_TO_STACK && | |
5748 | check_stack_access(env, dst_reg, dst_reg->off + | |
5749 | dst_reg->var_off.value, 1)) { | |
5750 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
5751 | "prohibited for !root\n", dst); | |
5752 | return -EACCES; | |
5753 | } | |
0d6303db DB |
5754 | } |
5755 | ||
43188702 JF |
5756 | return 0; |
5757 | } | |
5758 | ||
3f50f132 JF |
5759 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
5760 | struct bpf_reg_state *src_reg) | |
5761 | { | |
5762 | s32 smin_val = src_reg->s32_min_value; | |
5763 | s32 smax_val = src_reg->s32_max_value; | |
5764 | u32 umin_val = src_reg->u32_min_value; | |
5765 | u32 umax_val = src_reg->u32_max_value; | |
5766 | ||
5767 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
5768 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
5769 | dst_reg->s32_min_value = S32_MIN; | |
5770 | dst_reg->s32_max_value = S32_MAX; | |
5771 | } else { | |
5772 | dst_reg->s32_min_value += smin_val; | |
5773 | dst_reg->s32_max_value += smax_val; | |
5774 | } | |
5775 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
5776 | dst_reg->u32_max_value + umax_val < umax_val) { | |
5777 | dst_reg->u32_min_value = 0; | |
5778 | dst_reg->u32_max_value = U32_MAX; | |
5779 | } else { | |
5780 | dst_reg->u32_min_value += umin_val; | |
5781 | dst_reg->u32_max_value += umax_val; | |
5782 | } | |
5783 | } | |
5784 | ||
07cd2631 JF |
5785 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
5786 | struct bpf_reg_state *src_reg) | |
5787 | { | |
5788 | s64 smin_val = src_reg->smin_value; | |
5789 | s64 smax_val = src_reg->smax_value; | |
5790 | u64 umin_val = src_reg->umin_value; | |
5791 | u64 umax_val = src_reg->umax_value; | |
5792 | ||
5793 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
5794 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
5795 | dst_reg->smin_value = S64_MIN; | |
5796 | dst_reg->smax_value = S64_MAX; | |
5797 | } else { | |
5798 | dst_reg->smin_value += smin_val; | |
5799 | dst_reg->smax_value += smax_val; | |
5800 | } | |
5801 | if (dst_reg->umin_value + umin_val < umin_val || | |
5802 | dst_reg->umax_value + umax_val < umax_val) { | |
5803 | dst_reg->umin_value = 0; | |
5804 | dst_reg->umax_value = U64_MAX; | |
5805 | } else { | |
5806 | dst_reg->umin_value += umin_val; | |
5807 | dst_reg->umax_value += umax_val; | |
5808 | } | |
3f50f132 JF |
5809 | } |
5810 | ||
5811 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
5812 | struct bpf_reg_state *src_reg) | |
5813 | { | |
5814 | s32 smin_val = src_reg->s32_min_value; | |
5815 | s32 smax_val = src_reg->s32_max_value; | |
5816 | u32 umin_val = src_reg->u32_min_value; | |
5817 | u32 umax_val = src_reg->u32_max_value; | |
5818 | ||
5819 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
5820 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
5821 | /* Overflow possible, we know nothing */ | |
5822 | dst_reg->s32_min_value = S32_MIN; | |
5823 | dst_reg->s32_max_value = S32_MAX; | |
5824 | } else { | |
5825 | dst_reg->s32_min_value -= smax_val; | |
5826 | dst_reg->s32_max_value -= smin_val; | |
5827 | } | |
5828 | if (dst_reg->u32_min_value < umax_val) { | |
5829 | /* Overflow possible, we know nothing */ | |
5830 | dst_reg->u32_min_value = 0; | |
5831 | dst_reg->u32_max_value = U32_MAX; | |
5832 | } else { | |
5833 | /* Cannot overflow (as long as bounds are consistent) */ | |
5834 | dst_reg->u32_min_value -= umax_val; | |
5835 | dst_reg->u32_max_value -= umin_val; | |
5836 | } | |
07cd2631 JF |
5837 | } |
5838 | ||
5839 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
5840 | struct bpf_reg_state *src_reg) | |
5841 | { | |
5842 | s64 smin_val = src_reg->smin_value; | |
5843 | s64 smax_val = src_reg->smax_value; | |
5844 | u64 umin_val = src_reg->umin_value; | |
5845 | u64 umax_val = src_reg->umax_value; | |
5846 | ||
5847 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
5848 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
5849 | /* Overflow possible, we know nothing */ | |
5850 | dst_reg->smin_value = S64_MIN; | |
5851 | dst_reg->smax_value = S64_MAX; | |
5852 | } else { | |
5853 | dst_reg->smin_value -= smax_val; | |
5854 | dst_reg->smax_value -= smin_val; | |
5855 | } | |
5856 | if (dst_reg->umin_value < umax_val) { | |
5857 | /* Overflow possible, we know nothing */ | |
5858 | dst_reg->umin_value = 0; | |
5859 | dst_reg->umax_value = U64_MAX; | |
5860 | } else { | |
5861 | /* Cannot overflow (as long as bounds are consistent) */ | |
5862 | dst_reg->umin_value -= umax_val; | |
5863 | dst_reg->umax_value -= umin_val; | |
5864 | } | |
3f50f132 JF |
5865 | } |
5866 | ||
5867 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
5868 | struct bpf_reg_state *src_reg) | |
5869 | { | |
5870 | s32 smin_val = src_reg->s32_min_value; | |
5871 | u32 umin_val = src_reg->u32_min_value; | |
5872 | u32 umax_val = src_reg->u32_max_value; | |
5873 | ||
5874 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
5875 | /* Ain't nobody got time to multiply that sign */ | |
5876 | __mark_reg32_unbounded(dst_reg); | |
5877 | return; | |
5878 | } | |
5879 | /* Both values are positive, so we can work with unsigned and | |
5880 | * copy the result to signed (unless it exceeds S32_MAX). | |
5881 | */ | |
5882 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
5883 | /* Potential overflow, we know nothing */ | |
5884 | __mark_reg32_unbounded(dst_reg); | |
5885 | return; | |
5886 | } | |
5887 | dst_reg->u32_min_value *= umin_val; | |
5888 | dst_reg->u32_max_value *= umax_val; | |
5889 | if (dst_reg->u32_max_value > S32_MAX) { | |
5890 | /* Overflow possible, we know nothing */ | |
5891 | dst_reg->s32_min_value = S32_MIN; | |
5892 | dst_reg->s32_max_value = S32_MAX; | |
5893 | } else { | |
5894 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5895 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5896 | } | |
07cd2631 JF |
5897 | } |
5898 | ||
5899 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
5900 | struct bpf_reg_state *src_reg) | |
5901 | { | |
5902 | s64 smin_val = src_reg->smin_value; | |
5903 | u64 umin_val = src_reg->umin_value; | |
5904 | u64 umax_val = src_reg->umax_value; | |
5905 | ||
07cd2631 JF |
5906 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
5907 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 5908 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5909 | return; |
5910 | } | |
5911 | /* Both values are positive, so we can work with unsigned and | |
5912 | * copy the result to signed (unless it exceeds S64_MAX). | |
5913 | */ | |
5914 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
5915 | /* Potential overflow, we know nothing */ | |
3f50f132 | 5916 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5917 | return; |
5918 | } | |
5919 | dst_reg->umin_value *= umin_val; | |
5920 | dst_reg->umax_value *= umax_val; | |
5921 | if (dst_reg->umax_value > S64_MAX) { | |
5922 | /* Overflow possible, we know nothing */ | |
5923 | dst_reg->smin_value = S64_MIN; | |
5924 | dst_reg->smax_value = S64_MAX; | |
5925 | } else { | |
5926 | dst_reg->smin_value = dst_reg->umin_value; | |
5927 | dst_reg->smax_value = dst_reg->umax_value; | |
5928 | } | |
5929 | } | |
5930 | ||
3f50f132 JF |
5931 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
5932 | struct bpf_reg_state *src_reg) | |
5933 | { | |
5934 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5935 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5936 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5937 | s32 smin_val = src_reg->s32_min_value; | |
5938 | u32 umax_val = src_reg->u32_max_value; | |
5939 | ||
5940 | /* Assuming scalar64_min_max_and will be called so its safe | |
5941 | * to skip updating register for known 32-bit case. | |
5942 | */ | |
5943 | if (src_known && dst_known) | |
5944 | return; | |
5945 | ||
5946 | /* We get our minimum from the var_off, since that's inherently | |
5947 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5948 | */ | |
5949 | dst_reg->u32_min_value = var32_off.value; | |
5950 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
5951 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
5952 | /* Lose signed bounds when ANDing negative numbers, | |
5953 | * ain't nobody got time for that. | |
5954 | */ | |
5955 | dst_reg->s32_min_value = S32_MIN; | |
5956 | dst_reg->s32_max_value = S32_MAX; | |
5957 | } else { | |
5958 | /* ANDing two positives gives a positive, so safe to | |
5959 | * cast result into s64. | |
5960 | */ | |
5961 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5962 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5963 | } | |
5964 | ||
5965 | } | |
5966 | ||
07cd2631 JF |
5967 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
5968 | struct bpf_reg_state *src_reg) | |
5969 | { | |
3f50f132 JF |
5970 | bool src_known = tnum_is_const(src_reg->var_off); |
5971 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
5972 | s64 smin_val = src_reg->smin_value; |
5973 | u64 umax_val = src_reg->umax_value; | |
5974 | ||
3f50f132 | 5975 | if (src_known && dst_known) { |
4fbb38a3 | 5976 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
5977 | return; |
5978 | } | |
5979 | ||
07cd2631 JF |
5980 | /* We get our minimum from the var_off, since that's inherently |
5981 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5982 | */ | |
07cd2631 JF |
5983 | dst_reg->umin_value = dst_reg->var_off.value; |
5984 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
5985 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
5986 | /* Lose signed bounds when ANDing negative numbers, | |
5987 | * ain't nobody got time for that. | |
5988 | */ | |
5989 | dst_reg->smin_value = S64_MIN; | |
5990 | dst_reg->smax_value = S64_MAX; | |
5991 | } else { | |
5992 | /* ANDing two positives gives a positive, so safe to | |
5993 | * cast result into s64. | |
5994 | */ | |
5995 | dst_reg->smin_value = dst_reg->umin_value; | |
5996 | dst_reg->smax_value = dst_reg->umax_value; | |
5997 | } | |
5998 | /* We may learn something more from the var_off */ | |
5999 | __update_reg_bounds(dst_reg); | |
6000 | } | |
6001 | ||
3f50f132 JF |
6002 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
6003 | struct bpf_reg_state *src_reg) | |
6004 | { | |
6005 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
6006 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
6007 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5b9fbeb7 DB |
6008 | s32 smin_val = src_reg->s32_min_value; |
6009 | u32 umin_val = src_reg->u32_min_value; | |
3f50f132 JF |
6010 | |
6011 | /* Assuming scalar64_min_max_or will be called so it is safe | |
6012 | * to skip updating register for known case. | |
6013 | */ | |
6014 | if (src_known && dst_known) | |
6015 | return; | |
6016 | ||
6017 | /* We get our maximum from the var_off, and our minimum is the | |
6018 | * maximum of the operands' minima | |
6019 | */ | |
6020 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
6021 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
6022 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
6023 | /* Lose signed bounds when ORing negative numbers, | |
6024 | * ain't nobody got time for that. | |
6025 | */ | |
6026 | dst_reg->s32_min_value = S32_MIN; | |
6027 | dst_reg->s32_max_value = S32_MAX; | |
6028 | } else { | |
6029 | /* ORing two positives gives a positive, so safe to | |
6030 | * cast result into s64. | |
6031 | */ | |
5b9fbeb7 DB |
6032 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
6033 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
3f50f132 JF |
6034 | } |
6035 | } | |
6036 | ||
07cd2631 JF |
6037 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
6038 | struct bpf_reg_state *src_reg) | |
6039 | { | |
3f50f132 JF |
6040 | bool src_known = tnum_is_const(src_reg->var_off); |
6041 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
6042 | s64 smin_val = src_reg->smin_value; |
6043 | u64 umin_val = src_reg->umin_value; | |
6044 | ||
3f50f132 | 6045 | if (src_known && dst_known) { |
4fbb38a3 | 6046 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
6047 | return; |
6048 | } | |
6049 | ||
07cd2631 JF |
6050 | /* We get our maximum from the var_off, and our minimum is the |
6051 | * maximum of the operands' minima | |
6052 | */ | |
07cd2631 JF |
6053 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
6054 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
6055 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
6056 | /* Lose signed bounds when ORing negative numbers, | |
6057 | * ain't nobody got time for that. | |
6058 | */ | |
6059 | dst_reg->smin_value = S64_MIN; | |
6060 | dst_reg->smax_value = S64_MAX; | |
6061 | } else { | |
6062 | /* ORing two positives gives a positive, so safe to | |
6063 | * cast result into s64. | |
6064 | */ | |
6065 | dst_reg->smin_value = dst_reg->umin_value; | |
6066 | dst_reg->smax_value = dst_reg->umax_value; | |
6067 | } | |
6068 | /* We may learn something more from the var_off */ | |
6069 | __update_reg_bounds(dst_reg); | |
6070 | } | |
6071 | ||
2921c90d YS |
6072 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
6073 | struct bpf_reg_state *src_reg) | |
6074 | { | |
6075 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
6076 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
6077 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
6078 | s32 smin_val = src_reg->s32_min_value; | |
6079 | ||
6080 | /* Assuming scalar64_min_max_xor will be called so it is safe | |
6081 | * to skip updating register for known case. | |
6082 | */ | |
6083 | if (src_known && dst_known) | |
6084 | return; | |
6085 | ||
6086 | /* We get both minimum and maximum from the var32_off. */ | |
6087 | dst_reg->u32_min_value = var32_off.value; | |
6088 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
6089 | ||
6090 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
6091 | /* XORing two positive sign numbers gives a positive, | |
6092 | * so safe to cast u32 result into s32. | |
6093 | */ | |
6094 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
6095 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
6096 | } else { | |
6097 | dst_reg->s32_min_value = S32_MIN; | |
6098 | dst_reg->s32_max_value = S32_MAX; | |
6099 | } | |
6100 | } | |
6101 | ||
6102 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
6103 | struct bpf_reg_state *src_reg) | |
6104 | { | |
6105 | bool src_known = tnum_is_const(src_reg->var_off); | |
6106 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
6107 | s64 smin_val = src_reg->smin_value; | |
6108 | ||
6109 | if (src_known && dst_known) { | |
6110 | /* dst_reg->var_off.value has been updated earlier */ | |
6111 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
6112 | return; | |
6113 | } | |
6114 | ||
6115 | /* We get both minimum and maximum from the var_off. */ | |
6116 | dst_reg->umin_value = dst_reg->var_off.value; | |
6117 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
6118 | ||
6119 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
6120 | /* XORing two positive sign numbers gives a positive, | |
6121 | * so safe to cast u64 result into s64. | |
6122 | */ | |
6123 | dst_reg->smin_value = dst_reg->umin_value; | |
6124 | dst_reg->smax_value = dst_reg->umax_value; | |
6125 | } else { | |
6126 | dst_reg->smin_value = S64_MIN; | |
6127 | dst_reg->smax_value = S64_MAX; | |
6128 | } | |
6129 | ||
6130 | __update_reg_bounds(dst_reg); | |
6131 | } | |
6132 | ||
3f50f132 JF |
6133 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
6134 | u64 umin_val, u64 umax_val) | |
07cd2631 | 6135 | { |
07cd2631 JF |
6136 | /* We lose all sign bit information (except what we can pick |
6137 | * up from var_off) | |
6138 | */ | |
3f50f132 JF |
6139 | dst_reg->s32_min_value = S32_MIN; |
6140 | dst_reg->s32_max_value = S32_MAX; | |
6141 | /* If we might shift our top bit out, then we know nothing */ | |
6142 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
6143 | dst_reg->u32_min_value = 0; | |
6144 | dst_reg->u32_max_value = U32_MAX; | |
6145 | } else { | |
6146 | dst_reg->u32_min_value <<= umin_val; | |
6147 | dst_reg->u32_max_value <<= umax_val; | |
6148 | } | |
6149 | } | |
6150 | ||
6151 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6152 | struct bpf_reg_state *src_reg) | |
6153 | { | |
6154 | u32 umax_val = src_reg->u32_max_value; | |
6155 | u32 umin_val = src_reg->u32_min_value; | |
6156 | /* u32 alu operation will zext upper bits */ | |
6157 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
6158 | ||
6159 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
6160 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
6161 | /* Not required but being careful mark reg64 bounds as unknown so | |
6162 | * that we are forced to pick them up from tnum and zext later and | |
6163 | * if some path skips this step we are still safe. | |
6164 | */ | |
6165 | __mark_reg64_unbounded(dst_reg); | |
6166 | __update_reg32_bounds(dst_reg); | |
6167 | } | |
6168 | ||
6169 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6170 | u64 umin_val, u64 umax_val) | |
6171 | { | |
6172 | /* Special case <<32 because it is a common compiler pattern to sign | |
6173 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
6174 | * positive we know this shift will also be positive so we can track | |
6175 | * bounds correctly. Otherwise we lose all sign bit information except | |
6176 | * what we can pick up from var_off. Perhaps we can generalize this | |
6177 | * later to shifts of any length. | |
6178 | */ | |
6179 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
6180 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
6181 | else | |
6182 | dst_reg->smax_value = S64_MAX; | |
6183 | ||
6184 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
6185 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
6186 | else | |
6187 | dst_reg->smin_value = S64_MIN; | |
6188 | ||
07cd2631 JF |
6189 | /* If we might shift our top bit out, then we know nothing */ |
6190 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
6191 | dst_reg->umin_value = 0; | |
6192 | dst_reg->umax_value = U64_MAX; | |
6193 | } else { | |
6194 | dst_reg->umin_value <<= umin_val; | |
6195 | dst_reg->umax_value <<= umax_val; | |
6196 | } | |
3f50f132 JF |
6197 | } |
6198 | ||
6199 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6200 | struct bpf_reg_state *src_reg) | |
6201 | { | |
6202 | u64 umax_val = src_reg->umax_value; | |
6203 | u64 umin_val = src_reg->umin_value; | |
6204 | ||
6205 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
6206 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
6207 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
6208 | ||
07cd2631 JF |
6209 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
6210 | /* We may learn something more from the var_off */ | |
6211 | __update_reg_bounds(dst_reg); | |
6212 | } | |
6213 | ||
3f50f132 JF |
6214 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
6215 | struct bpf_reg_state *src_reg) | |
6216 | { | |
6217 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
6218 | u32 umax_val = src_reg->u32_max_value; | |
6219 | u32 umin_val = src_reg->u32_min_value; | |
6220 | ||
6221 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
6222 | * be negative, then either: | |
6223 | * 1) src_reg might be zero, so the sign bit of the result is | |
6224 | * unknown, so we lose our signed bounds | |
6225 | * 2) it's known negative, thus the unsigned bounds capture the | |
6226 | * signed bounds | |
6227 | * 3) the signed bounds cross zero, so they tell us nothing | |
6228 | * about the result | |
6229 | * If the value in dst_reg is known nonnegative, then again the | |
6230 | * unsigned bounts capture the signed bounds. | |
6231 | * Thus, in all cases it suffices to blow away our signed bounds | |
6232 | * and rely on inferring new ones from the unsigned bounds and | |
6233 | * var_off of the result. | |
6234 | */ | |
6235 | dst_reg->s32_min_value = S32_MIN; | |
6236 | dst_reg->s32_max_value = S32_MAX; | |
6237 | ||
6238 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
6239 | dst_reg->u32_min_value >>= umax_val; | |
6240 | dst_reg->u32_max_value >>= umin_val; | |
6241 | ||
6242 | __mark_reg64_unbounded(dst_reg); | |
6243 | __update_reg32_bounds(dst_reg); | |
6244 | } | |
6245 | ||
07cd2631 JF |
6246 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
6247 | struct bpf_reg_state *src_reg) | |
6248 | { | |
6249 | u64 umax_val = src_reg->umax_value; | |
6250 | u64 umin_val = src_reg->umin_value; | |
6251 | ||
6252 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
6253 | * be negative, then either: | |
6254 | * 1) src_reg might be zero, so the sign bit of the result is | |
6255 | * unknown, so we lose our signed bounds | |
6256 | * 2) it's known negative, thus the unsigned bounds capture the | |
6257 | * signed bounds | |
6258 | * 3) the signed bounds cross zero, so they tell us nothing | |
6259 | * about the result | |
6260 | * If the value in dst_reg is known nonnegative, then again the | |
6261 | * unsigned bounts capture the signed bounds. | |
6262 | * Thus, in all cases it suffices to blow away our signed bounds | |
6263 | * and rely on inferring new ones from the unsigned bounds and | |
6264 | * var_off of the result. | |
6265 | */ | |
6266 | dst_reg->smin_value = S64_MIN; | |
6267 | dst_reg->smax_value = S64_MAX; | |
6268 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
6269 | dst_reg->umin_value >>= umax_val; | |
6270 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
6271 | |
6272 | /* Its not easy to operate on alu32 bounds here because it depends | |
6273 | * on bits being shifted in. Take easy way out and mark unbounded | |
6274 | * so we can recalculate later from tnum. | |
6275 | */ | |
6276 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
6277 | __update_reg_bounds(dst_reg); |
6278 | } | |
6279 | ||
3f50f132 JF |
6280 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
6281 | struct bpf_reg_state *src_reg) | |
07cd2631 | 6282 | { |
3f50f132 | 6283 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
6284 | |
6285 | /* Upon reaching here, src_known is true and | |
6286 | * umax_val is equal to umin_val. | |
6287 | */ | |
3f50f132 JF |
6288 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
6289 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 6290 | |
3f50f132 JF |
6291 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
6292 | ||
6293 | /* blow away the dst_reg umin_value/umax_value and rely on | |
6294 | * dst_reg var_off to refine the result. | |
6295 | */ | |
6296 | dst_reg->u32_min_value = 0; | |
6297 | dst_reg->u32_max_value = U32_MAX; | |
6298 | ||
6299 | __mark_reg64_unbounded(dst_reg); | |
6300 | __update_reg32_bounds(dst_reg); | |
6301 | } | |
6302 | ||
6303 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
6304 | struct bpf_reg_state *src_reg) | |
6305 | { | |
6306 | u64 umin_val = src_reg->umin_value; | |
6307 | ||
6308 | /* Upon reaching here, src_known is true and umax_val is equal | |
6309 | * to umin_val. | |
6310 | */ | |
6311 | dst_reg->smin_value >>= umin_val; | |
6312 | dst_reg->smax_value >>= umin_val; | |
6313 | ||
6314 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
6315 | |
6316 | /* blow away the dst_reg umin_value/umax_value and rely on | |
6317 | * dst_reg var_off to refine the result. | |
6318 | */ | |
6319 | dst_reg->umin_value = 0; | |
6320 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
6321 | |
6322 | /* Its not easy to operate on alu32 bounds here because it depends | |
6323 | * on bits being shifted in from upper 32-bits. Take easy way out | |
6324 | * and mark unbounded so we can recalculate later from tnum. | |
6325 | */ | |
6326 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
6327 | __update_reg_bounds(dst_reg); |
6328 | } | |
6329 | ||
468f6eaf JH |
6330 | /* WARNING: This function does calculations on 64-bit values, but the actual |
6331 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
6332 | * need extra checks in the 32-bit case. | |
6333 | */ | |
f1174f77 EC |
6334 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
6335 | struct bpf_insn *insn, | |
6336 | struct bpf_reg_state *dst_reg, | |
6337 | struct bpf_reg_state src_reg) | |
969bf05e | 6338 | { |
638f5b90 | 6339 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 6340 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 6341 | bool src_known; |
b03c9f9f EC |
6342 | s64 smin_val, smax_val; |
6343 | u64 umin_val, umax_val; | |
3f50f132 JF |
6344 | s32 s32_min_val, s32_max_val; |
6345 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 6346 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
6347 | u32 dst = insn->dst_reg; |
6348 | int ret; | |
3f50f132 | 6349 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
b799207e | 6350 | |
b03c9f9f EC |
6351 | smin_val = src_reg.smin_value; |
6352 | smax_val = src_reg.smax_value; | |
6353 | umin_val = src_reg.umin_value; | |
6354 | umax_val = src_reg.umax_value; | |
f23cc643 | 6355 | |
3f50f132 JF |
6356 | s32_min_val = src_reg.s32_min_value; |
6357 | s32_max_val = src_reg.s32_max_value; | |
6358 | u32_min_val = src_reg.u32_min_value; | |
6359 | u32_max_val = src_reg.u32_max_value; | |
6360 | ||
6361 | if (alu32) { | |
6362 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
6363 | if ((src_known && |
6364 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
6365 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
6366 | /* Taint dst register if offset had invalid bounds | |
6367 | * derived from e.g. dead branches. | |
6368 | */ | |
6369 | __mark_reg_unknown(env, dst_reg); | |
6370 | return 0; | |
6371 | } | |
6372 | } else { | |
6373 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
6374 | if ((src_known && |
6375 | (smin_val != smax_val || umin_val != umax_val)) || | |
6376 | smin_val > smax_val || umin_val > umax_val) { | |
6377 | /* Taint dst register if offset had invalid bounds | |
6378 | * derived from e.g. dead branches. | |
6379 | */ | |
6380 | __mark_reg_unknown(env, dst_reg); | |
6381 | return 0; | |
6382 | } | |
6f16101e DB |
6383 | } |
6384 | ||
bb7f0f98 AS |
6385 | if (!src_known && |
6386 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 6387 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
6388 | return 0; |
6389 | } | |
6390 | ||
3f50f132 JF |
6391 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
6392 | * There are two classes of instructions: The first class we track both | |
6393 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
6394 | * greatest amount of precision when alu operations are mixed with jmp32 | |
6395 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
6396 | * and BPF_OR. This is possible because these ops have fairly easy to | |
6397 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
6398 | * See alu32 verifier tests for examples. The second class of | |
6399 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
6400 | * with regards to tracking sign/unsigned bounds because the bits may | |
6401 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
6402 | * the reg unbounded in the subreg bound space and use the resulting | |
6403 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
6404 | */ | |
48461135 JB |
6405 | switch (opcode) { |
6406 | case BPF_ADD: | |
d3bd7413 DB |
6407 | ret = sanitize_val_alu(env, insn); |
6408 | if (ret < 0) { | |
6409 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
6410 | return ret; | |
6411 | } | |
3f50f132 | 6412 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 6413 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 6414 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
6415 | break; |
6416 | case BPF_SUB: | |
d3bd7413 DB |
6417 | ret = sanitize_val_alu(env, insn); |
6418 | if (ret < 0) { | |
6419 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
6420 | return ret; | |
6421 | } | |
3f50f132 | 6422 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 6423 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 6424 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
6425 | break; |
6426 | case BPF_MUL: | |
3f50f132 JF |
6427 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
6428 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 6429 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
6430 | break; |
6431 | case BPF_AND: | |
3f50f132 JF |
6432 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
6433 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 6434 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
6435 | break; |
6436 | case BPF_OR: | |
3f50f132 JF |
6437 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
6438 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 6439 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 6440 | break; |
2921c90d YS |
6441 | case BPF_XOR: |
6442 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
6443 | scalar32_min_max_xor(dst_reg, &src_reg); | |
6444 | scalar_min_max_xor(dst_reg, &src_reg); | |
6445 | break; | |
48461135 | 6446 | case BPF_LSH: |
468f6eaf JH |
6447 | if (umax_val >= insn_bitness) { |
6448 | /* Shifts greater than 31 or 63 are undefined. | |
6449 | * This includes shifts by a negative number. | |
b03c9f9f | 6450 | */ |
61bd5218 | 6451 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
6452 | break; |
6453 | } | |
3f50f132 JF |
6454 | if (alu32) |
6455 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
6456 | else | |
6457 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
6458 | break; |
6459 | case BPF_RSH: | |
468f6eaf JH |
6460 | if (umax_val >= insn_bitness) { |
6461 | /* Shifts greater than 31 or 63 are undefined. | |
6462 | * This includes shifts by a negative number. | |
b03c9f9f | 6463 | */ |
61bd5218 | 6464 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
6465 | break; |
6466 | } | |
3f50f132 JF |
6467 | if (alu32) |
6468 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
6469 | else | |
6470 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 6471 | break; |
9cbe1f5a YS |
6472 | case BPF_ARSH: |
6473 | if (umax_val >= insn_bitness) { | |
6474 | /* Shifts greater than 31 or 63 are undefined. | |
6475 | * This includes shifts by a negative number. | |
6476 | */ | |
6477 | mark_reg_unknown(env, regs, insn->dst_reg); | |
6478 | break; | |
6479 | } | |
3f50f132 JF |
6480 | if (alu32) |
6481 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
6482 | else | |
6483 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 6484 | break; |
48461135 | 6485 | default: |
61bd5218 | 6486 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
6487 | break; |
6488 | } | |
6489 | ||
3f50f132 JF |
6490 | /* ALU32 ops are zero extended into 64bit register */ |
6491 | if (alu32) | |
6492 | zext_32_to_64(dst_reg); | |
468f6eaf | 6493 | |
294f2fc6 | 6494 | __update_reg_bounds(dst_reg); |
b03c9f9f EC |
6495 | __reg_deduce_bounds(dst_reg); |
6496 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
6497 | return 0; |
6498 | } | |
6499 | ||
6500 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
6501 | * and var_off. | |
6502 | */ | |
6503 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
6504 | struct bpf_insn *insn) | |
6505 | { | |
f4d7e40a AS |
6506 | struct bpf_verifier_state *vstate = env->cur_state; |
6507 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
6508 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
6509 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
6510 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 6511 | int err; |
f1174f77 EC |
6512 | |
6513 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
6514 | src_reg = NULL; |
6515 | if (dst_reg->type != SCALAR_VALUE) | |
6516 | ptr_reg = dst_reg; | |
75748837 AS |
6517 | else |
6518 | /* Make sure ID is cleared otherwise dst_reg min/max could be | |
6519 | * incorrectly propagated into other registers by find_equal_scalars() | |
6520 | */ | |
6521 | dst_reg->id = 0; | |
f1174f77 EC |
6522 | if (BPF_SRC(insn->code) == BPF_X) { |
6523 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
6524 | if (src_reg->type != SCALAR_VALUE) { |
6525 | if (dst_reg->type != SCALAR_VALUE) { | |
6526 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
6527 | * an arbitrary scalar. Disallow all math except |
6528 | * pointer subtraction | |
f1174f77 | 6529 | */ |
dd066823 | 6530 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
6531 | mark_reg_unknown(env, regs, insn->dst_reg); |
6532 | return 0; | |
f1174f77 | 6533 | } |
82abbf8d AS |
6534 | verbose(env, "R%d pointer %s pointer prohibited\n", |
6535 | insn->dst_reg, | |
6536 | bpf_alu_string[opcode >> 4]); | |
6537 | return -EACCES; | |
f1174f77 EC |
6538 | } else { |
6539 | /* scalar += pointer | |
6540 | * This is legal, but we have to reverse our | |
6541 | * src/dest handling in computing the range | |
6542 | */ | |
b5dc0163 AS |
6543 | err = mark_chain_precision(env, insn->dst_reg); |
6544 | if (err) | |
6545 | return err; | |
82abbf8d AS |
6546 | return adjust_ptr_min_max_vals(env, insn, |
6547 | src_reg, dst_reg); | |
f1174f77 EC |
6548 | } |
6549 | } else if (ptr_reg) { | |
6550 | /* pointer += scalar */ | |
b5dc0163 AS |
6551 | err = mark_chain_precision(env, insn->src_reg); |
6552 | if (err) | |
6553 | return err; | |
82abbf8d AS |
6554 | return adjust_ptr_min_max_vals(env, insn, |
6555 | dst_reg, src_reg); | |
f1174f77 EC |
6556 | } |
6557 | } else { | |
6558 | /* Pretend the src is a reg with a known value, since we only | |
6559 | * need to be able to read from this state. | |
6560 | */ | |
6561 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 6562 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 6563 | src_reg = &off_reg; |
82abbf8d AS |
6564 | if (ptr_reg) /* pointer += K */ |
6565 | return adjust_ptr_min_max_vals(env, insn, | |
6566 | ptr_reg, src_reg); | |
f1174f77 EC |
6567 | } |
6568 | ||
6569 | /* Got here implies adding two SCALAR_VALUEs */ | |
6570 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 6571 | print_verifier_state(env, state); |
61bd5218 | 6572 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
6573 | return -EINVAL; |
6574 | } | |
6575 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 6576 | print_verifier_state(env, state); |
61bd5218 | 6577 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
6578 | return -EINVAL; |
6579 | } | |
6580 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
6581 | } |
6582 | ||
17a52670 | 6583 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 6584 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 6585 | { |
638f5b90 | 6586 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
6587 | u8 opcode = BPF_OP(insn->code); |
6588 | int err; | |
6589 | ||
6590 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
6591 | if (opcode == BPF_NEG) { | |
6592 | if (BPF_SRC(insn->code) != 0 || | |
6593 | insn->src_reg != BPF_REG_0 || | |
6594 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 6595 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
6596 | return -EINVAL; |
6597 | } | |
6598 | } else { | |
6599 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
6600 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
6601 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 6602 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
6603 | return -EINVAL; |
6604 | } | |
6605 | } | |
6606 | ||
6607 | /* check src operand */ | |
dc503a8a | 6608 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6609 | if (err) |
6610 | return err; | |
6611 | ||
1be7f75d | 6612 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 6613 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
6614 | insn->dst_reg); |
6615 | return -EACCES; | |
6616 | } | |
6617 | ||
17a52670 | 6618 | /* check dest operand */ |
dc503a8a | 6619 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
6620 | if (err) |
6621 | return err; | |
6622 | ||
6623 | } else if (opcode == BPF_MOV) { | |
6624 | ||
6625 | if (BPF_SRC(insn->code) == BPF_X) { | |
6626 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6627 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6628 | return -EINVAL; |
6629 | } | |
6630 | ||
6631 | /* check src operand */ | |
dc503a8a | 6632 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6633 | if (err) |
6634 | return err; | |
6635 | } else { | |
6636 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6637 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6638 | return -EINVAL; |
6639 | } | |
6640 | } | |
6641 | ||
fbeb1603 AF |
6642 | /* check dest operand, mark as required later */ |
6643 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
6644 | if (err) |
6645 | return err; | |
6646 | ||
6647 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
6648 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
6649 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
6650 | ||
17a52670 AS |
6651 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6652 | /* case: R1 = R2 | |
6653 | * copy register state to dest reg | |
6654 | */ | |
75748837 AS |
6655 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
6656 | /* Assign src and dst registers the same ID | |
6657 | * that will be used by find_equal_scalars() | |
6658 | * to propagate min/max range. | |
6659 | */ | |
6660 | src_reg->id = ++env->id_gen; | |
e434b8cd JW |
6661 | *dst_reg = *src_reg; |
6662 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 6663 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 6664 | } else { |
f1174f77 | 6665 | /* R1 = (u32) R2 */ |
1be7f75d | 6666 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
6667 | verbose(env, |
6668 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
6669 | insn->src_reg); |
6670 | return -EACCES; | |
e434b8cd JW |
6671 | } else if (src_reg->type == SCALAR_VALUE) { |
6672 | *dst_reg = *src_reg; | |
75748837 AS |
6673 | /* Make sure ID is cleared otherwise |
6674 | * dst_reg min/max could be incorrectly | |
6675 | * propagated into src_reg by find_equal_scalars() | |
6676 | */ | |
6677 | dst_reg->id = 0; | |
e434b8cd | 6678 | dst_reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 6679 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
6680 | } else { |
6681 | mark_reg_unknown(env, regs, | |
6682 | insn->dst_reg); | |
1be7f75d | 6683 | } |
3f50f132 | 6684 | zext_32_to_64(dst_reg); |
17a52670 AS |
6685 | } |
6686 | } else { | |
6687 | /* case: R = imm | |
6688 | * remember the value we stored into this reg | |
6689 | */ | |
fbeb1603 AF |
6690 | /* clear any state __mark_reg_known doesn't set */ |
6691 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 6692 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
6693 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6694 | __mark_reg_known(regs + insn->dst_reg, | |
6695 | insn->imm); | |
6696 | } else { | |
6697 | __mark_reg_known(regs + insn->dst_reg, | |
6698 | (u32)insn->imm); | |
6699 | } | |
17a52670 AS |
6700 | } |
6701 | ||
6702 | } else if (opcode > BPF_END) { | |
61bd5218 | 6703 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
6704 | return -EINVAL; |
6705 | ||
6706 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
6707 | ||
17a52670 AS |
6708 | if (BPF_SRC(insn->code) == BPF_X) { |
6709 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6710 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6711 | return -EINVAL; |
6712 | } | |
6713 | /* check src1 operand */ | |
dc503a8a | 6714 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6715 | if (err) |
6716 | return err; | |
6717 | } else { | |
6718 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6719 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6720 | return -EINVAL; |
6721 | } | |
6722 | } | |
6723 | ||
6724 | /* check src2 operand */ | |
dc503a8a | 6725 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6726 | if (err) |
6727 | return err; | |
6728 | ||
6729 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
6730 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 6731 | verbose(env, "div by zero\n"); |
17a52670 AS |
6732 | return -EINVAL; |
6733 | } | |
6734 | ||
229394e8 RV |
6735 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
6736 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
6737 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
6738 | ||
6739 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 6740 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
6741 | return -EINVAL; |
6742 | } | |
6743 | } | |
6744 | ||
1a0dc1ac | 6745 | /* check dest operand */ |
dc503a8a | 6746 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
6747 | if (err) |
6748 | return err; | |
6749 | ||
f1174f77 | 6750 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
6751 | } |
6752 | ||
6753 | return 0; | |
6754 | } | |
6755 | ||
c6a9efa1 PC |
6756 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
6757 | struct bpf_reg_state *dst_reg, | |
6d94e741 | 6758 | enum bpf_reg_type type, int new_range) |
c6a9efa1 PC |
6759 | { |
6760 | struct bpf_reg_state *reg; | |
6761 | int i; | |
6762 | ||
6763 | for (i = 0; i < MAX_BPF_REG; i++) { | |
6764 | reg = &state->regs[i]; | |
6765 | if (reg->type == type && reg->id == dst_reg->id) | |
6766 | /* keep the maximum range already checked */ | |
6767 | reg->range = max(reg->range, new_range); | |
6768 | } | |
6769 | ||
6770 | bpf_for_each_spilled_reg(i, state, reg) { | |
6771 | if (!reg) | |
6772 | continue; | |
6773 | if (reg->type == type && reg->id == dst_reg->id) | |
6774 | reg->range = max(reg->range, new_range); | |
6775 | } | |
6776 | } | |
6777 | ||
f4d7e40a | 6778 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 6779 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 6780 | enum bpf_reg_type type, |
fb2a311a | 6781 | bool range_right_open) |
969bf05e | 6782 | { |
6d94e741 | 6783 | int new_range, i; |
2d2be8ca | 6784 | |
fb2a311a DB |
6785 | if (dst_reg->off < 0 || |
6786 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
6787 | /* This doesn't give us any range */ |
6788 | return; | |
6789 | ||
b03c9f9f EC |
6790 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
6791 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
6792 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
6793 | * than pkt_end, but that's because it's also less than pkt. | |
6794 | */ | |
6795 | return; | |
6796 | ||
fb2a311a DB |
6797 | new_range = dst_reg->off; |
6798 | if (range_right_open) | |
6799 | new_range--; | |
6800 | ||
6801 | /* Examples for register markings: | |
2d2be8ca | 6802 | * |
fb2a311a | 6803 | * pkt_data in dst register: |
2d2be8ca DB |
6804 | * |
6805 | * r2 = r3; | |
6806 | * r2 += 8; | |
6807 | * if (r2 > pkt_end) goto <handle exception> | |
6808 | * <access okay> | |
6809 | * | |
b4e432f1 DB |
6810 | * r2 = r3; |
6811 | * r2 += 8; | |
6812 | * if (r2 < pkt_end) goto <access okay> | |
6813 | * <handle exception> | |
6814 | * | |
2d2be8ca DB |
6815 | * Where: |
6816 | * r2 == dst_reg, pkt_end == src_reg | |
6817 | * r2=pkt(id=n,off=8,r=0) | |
6818 | * r3=pkt(id=n,off=0,r=0) | |
6819 | * | |
fb2a311a | 6820 | * pkt_data in src register: |
2d2be8ca DB |
6821 | * |
6822 | * r2 = r3; | |
6823 | * r2 += 8; | |
6824 | * if (pkt_end >= r2) goto <access okay> | |
6825 | * <handle exception> | |
6826 | * | |
b4e432f1 DB |
6827 | * r2 = r3; |
6828 | * r2 += 8; | |
6829 | * if (pkt_end <= r2) goto <handle exception> | |
6830 | * <access okay> | |
6831 | * | |
2d2be8ca DB |
6832 | * Where: |
6833 | * pkt_end == dst_reg, r2 == src_reg | |
6834 | * r2=pkt(id=n,off=8,r=0) | |
6835 | * r3=pkt(id=n,off=0,r=0) | |
6836 | * | |
6837 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
6838 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
6839 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
6840 | * the check. | |
969bf05e | 6841 | */ |
2d2be8ca | 6842 | |
f1174f77 EC |
6843 | /* If our ids match, then we must have the same max_value. And we |
6844 | * don't care about the other reg's fixed offset, since if it's too big | |
6845 | * the range won't allow anything. | |
6846 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
6847 | */ | |
c6a9efa1 PC |
6848 | for (i = 0; i <= vstate->curframe; i++) |
6849 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
6850 | new_range); | |
969bf05e AS |
6851 | } |
6852 | ||
3f50f132 | 6853 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 6854 | { |
3f50f132 JF |
6855 | struct tnum subreg = tnum_subreg(reg->var_off); |
6856 | s32 sval = (s32)val; | |
a72dafaf | 6857 | |
3f50f132 JF |
6858 | switch (opcode) { |
6859 | case BPF_JEQ: | |
6860 | if (tnum_is_const(subreg)) | |
6861 | return !!tnum_equals_const(subreg, val); | |
6862 | break; | |
6863 | case BPF_JNE: | |
6864 | if (tnum_is_const(subreg)) | |
6865 | return !tnum_equals_const(subreg, val); | |
6866 | break; | |
6867 | case BPF_JSET: | |
6868 | if ((~subreg.mask & subreg.value) & val) | |
6869 | return 1; | |
6870 | if (!((subreg.mask | subreg.value) & val)) | |
6871 | return 0; | |
6872 | break; | |
6873 | case BPF_JGT: | |
6874 | if (reg->u32_min_value > val) | |
6875 | return 1; | |
6876 | else if (reg->u32_max_value <= val) | |
6877 | return 0; | |
6878 | break; | |
6879 | case BPF_JSGT: | |
6880 | if (reg->s32_min_value > sval) | |
6881 | return 1; | |
6882 | else if (reg->s32_max_value < sval) | |
6883 | return 0; | |
6884 | break; | |
6885 | case BPF_JLT: | |
6886 | if (reg->u32_max_value < val) | |
6887 | return 1; | |
6888 | else if (reg->u32_min_value >= val) | |
6889 | return 0; | |
6890 | break; | |
6891 | case BPF_JSLT: | |
6892 | if (reg->s32_max_value < sval) | |
6893 | return 1; | |
6894 | else if (reg->s32_min_value >= sval) | |
6895 | return 0; | |
6896 | break; | |
6897 | case BPF_JGE: | |
6898 | if (reg->u32_min_value >= val) | |
6899 | return 1; | |
6900 | else if (reg->u32_max_value < val) | |
6901 | return 0; | |
6902 | break; | |
6903 | case BPF_JSGE: | |
6904 | if (reg->s32_min_value >= sval) | |
6905 | return 1; | |
6906 | else if (reg->s32_max_value < sval) | |
6907 | return 0; | |
6908 | break; | |
6909 | case BPF_JLE: | |
6910 | if (reg->u32_max_value <= val) | |
6911 | return 1; | |
6912 | else if (reg->u32_min_value > val) | |
6913 | return 0; | |
6914 | break; | |
6915 | case BPF_JSLE: | |
6916 | if (reg->s32_max_value <= sval) | |
6917 | return 1; | |
6918 | else if (reg->s32_min_value > sval) | |
6919 | return 0; | |
6920 | break; | |
6921 | } | |
4f7b3e82 | 6922 | |
3f50f132 JF |
6923 | return -1; |
6924 | } | |
092ed096 | 6925 | |
3f50f132 JF |
6926 | |
6927 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
6928 | { | |
6929 | s64 sval = (s64)val; | |
a72dafaf | 6930 | |
4f7b3e82 AS |
6931 | switch (opcode) { |
6932 | case BPF_JEQ: | |
6933 | if (tnum_is_const(reg->var_off)) | |
6934 | return !!tnum_equals_const(reg->var_off, val); | |
6935 | break; | |
6936 | case BPF_JNE: | |
6937 | if (tnum_is_const(reg->var_off)) | |
6938 | return !tnum_equals_const(reg->var_off, val); | |
6939 | break; | |
960ea056 JK |
6940 | case BPF_JSET: |
6941 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
6942 | return 1; | |
6943 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
6944 | return 0; | |
6945 | break; | |
4f7b3e82 AS |
6946 | case BPF_JGT: |
6947 | if (reg->umin_value > val) | |
6948 | return 1; | |
6949 | else if (reg->umax_value <= val) | |
6950 | return 0; | |
6951 | break; | |
6952 | case BPF_JSGT: | |
a72dafaf | 6953 | if (reg->smin_value > sval) |
4f7b3e82 | 6954 | return 1; |
a72dafaf | 6955 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6956 | return 0; |
6957 | break; | |
6958 | case BPF_JLT: | |
6959 | if (reg->umax_value < val) | |
6960 | return 1; | |
6961 | else if (reg->umin_value >= val) | |
6962 | return 0; | |
6963 | break; | |
6964 | case BPF_JSLT: | |
a72dafaf | 6965 | if (reg->smax_value < sval) |
4f7b3e82 | 6966 | return 1; |
a72dafaf | 6967 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
6968 | return 0; |
6969 | break; | |
6970 | case BPF_JGE: | |
6971 | if (reg->umin_value >= val) | |
6972 | return 1; | |
6973 | else if (reg->umax_value < val) | |
6974 | return 0; | |
6975 | break; | |
6976 | case BPF_JSGE: | |
a72dafaf | 6977 | if (reg->smin_value >= sval) |
4f7b3e82 | 6978 | return 1; |
a72dafaf | 6979 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6980 | return 0; |
6981 | break; | |
6982 | case BPF_JLE: | |
6983 | if (reg->umax_value <= val) | |
6984 | return 1; | |
6985 | else if (reg->umin_value > val) | |
6986 | return 0; | |
6987 | break; | |
6988 | case BPF_JSLE: | |
a72dafaf | 6989 | if (reg->smax_value <= sval) |
4f7b3e82 | 6990 | return 1; |
a72dafaf | 6991 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
6992 | return 0; |
6993 | break; | |
6994 | } | |
6995 | ||
6996 | return -1; | |
6997 | } | |
6998 | ||
3f50f132 JF |
6999 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
7000 | * and return: | |
7001 | * 1 - branch will be taken and "goto target" will be executed | |
7002 | * 0 - branch will not be taken and fall-through to next insn | |
7003 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
7004 | * range [0,10] | |
604dca5e | 7005 | */ |
3f50f132 JF |
7006 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
7007 | bool is_jmp32) | |
604dca5e | 7008 | { |
cac616db JF |
7009 | if (__is_pointer_value(false, reg)) { |
7010 | if (!reg_type_not_null(reg->type)) | |
7011 | return -1; | |
7012 | ||
7013 | /* If pointer is valid tests against zero will fail so we can | |
7014 | * use this to direct branch taken. | |
7015 | */ | |
7016 | if (val != 0) | |
7017 | return -1; | |
7018 | ||
7019 | switch (opcode) { | |
7020 | case BPF_JEQ: | |
7021 | return 0; | |
7022 | case BPF_JNE: | |
7023 | return 1; | |
7024 | default: | |
7025 | return -1; | |
7026 | } | |
7027 | } | |
604dca5e | 7028 | |
3f50f132 JF |
7029 | if (is_jmp32) |
7030 | return is_branch32_taken(reg, val, opcode); | |
7031 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
7032 | } |
7033 | ||
6d94e741 AS |
7034 | static int flip_opcode(u32 opcode) |
7035 | { | |
7036 | /* How can we transform "a <op> b" into "b <op> a"? */ | |
7037 | static const u8 opcode_flip[16] = { | |
7038 | /* these stay the same */ | |
7039 | [BPF_JEQ >> 4] = BPF_JEQ, | |
7040 | [BPF_JNE >> 4] = BPF_JNE, | |
7041 | [BPF_JSET >> 4] = BPF_JSET, | |
7042 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
7043 | [BPF_JGE >> 4] = BPF_JLE, | |
7044 | [BPF_JGT >> 4] = BPF_JLT, | |
7045 | [BPF_JLE >> 4] = BPF_JGE, | |
7046 | [BPF_JLT >> 4] = BPF_JGT, | |
7047 | [BPF_JSGE >> 4] = BPF_JSLE, | |
7048 | [BPF_JSGT >> 4] = BPF_JSLT, | |
7049 | [BPF_JSLE >> 4] = BPF_JSGE, | |
7050 | [BPF_JSLT >> 4] = BPF_JSGT | |
7051 | }; | |
7052 | return opcode_flip[opcode >> 4]; | |
7053 | } | |
7054 | ||
7055 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, | |
7056 | struct bpf_reg_state *src_reg, | |
7057 | u8 opcode) | |
7058 | { | |
7059 | struct bpf_reg_state *pkt; | |
7060 | ||
7061 | if (src_reg->type == PTR_TO_PACKET_END) { | |
7062 | pkt = dst_reg; | |
7063 | } else if (dst_reg->type == PTR_TO_PACKET_END) { | |
7064 | pkt = src_reg; | |
7065 | opcode = flip_opcode(opcode); | |
7066 | } else { | |
7067 | return -1; | |
7068 | } | |
7069 | ||
7070 | if (pkt->range >= 0) | |
7071 | return -1; | |
7072 | ||
7073 | switch (opcode) { | |
7074 | case BPF_JLE: | |
7075 | /* pkt <= pkt_end */ | |
7076 | fallthrough; | |
7077 | case BPF_JGT: | |
7078 | /* pkt > pkt_end */ | |
7079 | if (pkt->range == BEYOND_PKT_END) | |
7080 | /* pkt has at last one extra byte beyond pkt_end */ | |
7081 | return opcode == BPF_JGT; | |
7082 | break; | |
7083 | case BPF_JLT: | |
7084 | /* pkt < pkt_end */ | |
7085 | fallthrough; | |
7086 | case BPF_JGE: | |
7087 | /* pkt >= pkt_end */ | |
7088 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) | |
7089 | return opcode == BPF_JGE; | |
7090 | break; | |
7091 | } | |
7092 | return -1; | |
7093 | } | |
7094 | ||
48461135 JB |
7095 | /* Adjusts the register min/max values in the case that the dst_reg is the |
7096 | * variable register that we are working on, and src_reg is a constant or we're | |
7097 | * simply doing a BPF_K check. | |
f1174f77 | 7098 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
7099 | */ |
7100 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
7101 | struct bpf_reg_state *false_reg, |
7102 | u64 val, u32 val32, | |
092ed096 | 7103 | u8 opcode, bool is_jmp32) |
48461135 | 7104 | { |
3f50f132 JF |
7105 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
7106 | struct tnum false_64off = false_reg->var_off; | |
7107 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
7108 | struct tnum true_64off = true_reg->var_off; | |
7109 | s64 sval = (s64)val; | |
7110 | s32 sval32 = (s32)val32; | |
a72dafaf | 7111 | |
f1174f77 EC |
7112 | /* If the dst_reg is a pointer, we can't learn anything about its |
7113 | * variable offset from the compare (unless src_reg were a pointer into | |
7114 | * the same object, but we don't bother with that. | |
7115 | * Since false_reg and true_reg have the same type by construction, we | |
7116 | * only need to check one of them for pointerness. | |
7117 | */ | |
7118 | if (__is_pointer_value(false, false_reg)) | |
7119 | return; | |
4cabc5b1 | 7120 | |
48461135 JB |
7121 | switch (opcode) { |
7122 | case BPF_JEQ: | |
48461135 | 7123 | case BPF_JNE: |
a72dafaf JW |
7124 | { |
7125 | struct bpf_reg_state *reg = | |
7126 | opcode == BPF_JEQ ? true_reg : false_reg; | |
7127 | ||
e688c3db AS |
7128 | /* JEQ/JNE comparison doesn't change the register equivalence. |
7129 | * r1 = r2; | |
7130 | * if (r1 == 42) goto label; | |
7131 | * ... | |
7132 | * label: // here both r1 and r2 are known to be 42. | |
7133 | * | |
7134 | * Hence when marking register as known preserve it's ID. | |
48461135 | 7135 | */ |
3f50f132 JF |
7136 | if (is_jmp32) |
7137 | __mark_reg32_known(reg, val32); | |
7138 | else | |
e688c3db | 7139 | ___mark_reg_known(reg, val); |
48461135 | 7140 | break; |
a72dafaf | 7141 | } |
960ea056 | 7142 | case BPF_JSET: |
3f50f132 JF |
7143 | if (is_jmp32) { |
7144 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
7145 | if (is_power_of_2(val32)) | |
7146 | true_32off = tnum_or(true_32off, | |
7147 | tnum_const(val32)); | |
7148 | } else { | |
7149 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
7150 | if (is_power_of_2(val)) | |
7151 | true_64off = tnum_or(true_64off, | |
7152 | tnum_const(val)); | |
7153 | } | |
960ea056 | 7154 | break; |
48461135 | 7155 | case BPF_JGE: |
a72dafaf JW |
7156 | case BPF_JGT: |
7157 | { | |
3f50f132 JF |
7158 | if (is_jmp32) { |
7159 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
7160 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
7161 | ||
7162 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
7163 | false_umax); | |
7164 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
7165 | true_umin); | |
7166 | } else { | |
7167 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
7168 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
7169 | ||
7170 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
7171 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
7172 | } | |
b03c9f9f | 7173 | break; |
a72dafaf | 7174 | } |
48461135 | 7175 | case BPF_JSGE: |
a72dafaf JW |
7176 | case BPF_JSGT: |
7177 | { | |
3f50f132 JF |
7178 | if (is_jmp32) { |
7179 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
7180 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 7181 | |
3f50f132 JF |
7182 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
7183 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
7184 | } else { | |
7185 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
7186 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
7187 | ||
7188 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
7189 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
7190 | } | |
48461135 | 7191 | break; |
a72dafaf | 7192 | } |
b4e432f1 | 7193 | case BPF_JLE: |
a72dafaf JW |
7194 | case BPF_JLT: |
7195 | { | |
3f50f132 JF |
7196 | if (is_jmp32) { |
7197 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
7198 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
7199 | ||
7200 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
7201 | false_umin); | |
7202 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
7203 | true_umax); | |
7204 | } else { | |
7205 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
7206 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
7207 | ||
7208 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
7209 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
7210 | } | |
b4e432f1 | 7211 | break; |
a72dafaf | 7212 | } |
b4e432f1 | 7213 | case BPF_JSLE: |
a72dafaf JW |
7214 | case BPF_JSLT: |
7215 | { | |
3f50f132 JF |
7216 | if (is_jmp32) { |
7217 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
7218 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 7219 | |
3f50f132 JF |
7220 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
7221 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
7222 | } else { | |
7223 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
7224 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
7225 | ||
7226 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
7227 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
7228 | } | |
b4e432f1 | 7229 | break; |
a72dafaf | 7230 | } |
48461135 | 7231 | default: |
0fc31b10 | 7232 | return; |
48461135 JB |
7233 | } |
7234 | ||
3f50f132 JF |
7235 | if (is_jmp32) { |
7236 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
7237 | tnum_subreg(false_32off)); | |
7238 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
7239 | tnum_subreg(true_32off)); | |
7240 | __reg_combine_32_into_64(false_reg); | |
7241 | __reg_combine_32_into_64(true_reg); | |
7242 | } else { | |
7243 | false_reg->var_off = false_64off; | |
7244 | true_reg->var_off = true_64off; | |
7245 | __reg_combine_64_into_32(false_reg); | |
7246 | __reg_combine_64_into_32(true_reg); | |
7247 | } | |
48461135 JB |
7248 | } |
7249 | ||
f1174f77 EC |
7250 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
7251 | * the variable reg. | |
48461135 JB |
7252 | */ |
7253 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
7254 | struct bpf_reg_state *false_reg, |
7255 | u64 val, u32 val32, | |
092ed096 | 7256 | u8 opcode, bool is_jmp32) |
48461135 | 7257 | { |
6d94e741 | 7258 | opcode = flip_opcode(opcode); |
0fc31b10 JH |
7259 | /* This uses zero as "not present in table"; luckily the zero opcode, |
7260 | * BPF_JA, can't get here. | |
b03c9f9f | 7261 | */ |
0fc31b10 | 7262 | if (opcode) |
3f50f132 | 7263 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
7264 | } |
7265 | ||
7266 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
7267 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
7268 | struct bpf_reg_state *dst_reg) | |
7269 | { | |
b03c9f9f EC |
7270 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
7271 | dst_reg->umin_value); | |
7272 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
7273 | dst_reg->umax_value); | |
7274 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
7275 | dst_reg->smin_value); | |
7276 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
7277 | dst_reg->smax_value); | |
f1174f77 EC |
7278 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
7279 | dst_reg->var_off); | |
b03c9f9f EC |
7280 | /* We might have learned new bounds from the var_off. */ |
7281 | __update_reg_bounds(src_reg); | |
7282 | __update_reg_bounds(dst_reg); | |
7283 | /* We might have learned something about the sign bit. */ | |
7284 | __reg_deduce_bounds(src_reg); | |
7285 | __reg_deduce_bounds(dst_reg); | |
7286 | /* We might have learned some bits from the bounds. */ | |
7287 | __reg_bound_offset(src_reg); | |
7288 | __reg_bound_offset(dst_reg); | |
7289 | /* Intersecting with the old var_off might have improved our bounds | |
7290 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
7291 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
7292 | */ | |
7293 | __update_reg_bounds(src_reg); | |
7294 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
7295 | } |
7296 | ||
7297 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
7298 | struct bpf_reg_state *true_dst, | |
7299 | struct bpf_reg_state *false_src, | |
7300 | struct bpf_reg_state *false_dst, | |
7301 | u8 opcode) | |
7302 | { | |
7303 | switch (opcode) { | |
7304 | case BPF_JEQ: | |
7305 | __reg_combine_min_max(true_src, true_dst); | |
7306 | break; | |
7307 | case BPF_JNE: | |
7308 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 7309 | break; |
4cabc5b1 | 7310 | } |
48461135 JB |
7311 | } |
7312 | ||
fd978bf7 JS |
7313 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
7314 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 7315 | bool is_null) |
57a09bf0 | 7316 | { |
93c230e3 MKL |
7317 | if (reg_type_may_be_null(reg->type) && reg->id == id && |
7318 | !WARN_ON_ONCE(!reg->id)) { | |
f1174f77 EC |
7319 | /* Old offset (both fixed and variable parts) should |
7320 | * have been known-zero, because we don't allow pointer | |
7321 | * arithmetic on pointers that might be NULL. | |
7322 | */ | |
b03c9f9f EC |
7323 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
7324 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 7325 | reg->off)) { |
b03c9f9f EC |
7326 | __mark_reg_known_zero(reg); |
7327 | reg->off = 0; | |
f1174f77 EC |
7328 | } |
7329 | if (is_null) { | |
7330 | reg->type = SCALAR_VALUE; | |
840b9615 | 7331 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
64d85290 JS |
7332 | const struct bpf_map *map = reg->map_ptr; |
7333 | ||
7334 | if (map->inner_map_meta) { | |
840b9615 | 7335 | reg->type = CONST_PTR_TO_MAP; |
64d85290 JS |
7336 | reg->map_ptr = map->inner_map_meta; |
7337 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { | |
fada7fdc | 7338 | reg->type = PTR_TO_XDP_SOCK; |
64d85290 JS |
7339 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
7340 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
7341 | reg->type = PTR_TO_SOCKET; | |
840b9615 JS |
7342 | } else { |
7343 | reg->type = PTR_TO_MAP_VALUE; | |
7344 | } | |
c64b7983 JS |
7345 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
7346 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
7347 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
7348 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
7349 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
7350 | reg->type = PTR_TO_TCP_SOCK; | |
b121b341 YS |
7351 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
7352 | reg->type = PTR_TO_BTF_ID; | |
457f4436 AN |
7353 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
7354 | reg->type = PTR_TO_MEM; | |
afbf21dc YS |
7355 | } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
7356 | reg->type = PTR_TO_RDONLY_BUF; | |
7357 | } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { | |
7358 | reg->type = PTR_TO_RDWR_BUF; | |
56f668df | 7359 | } |
1b986589 MKL |
7360 | if (is_null) { |
7361 | /* We don't need id and ref_obj_id from this point | |
7362 | * onwards anymore, thus we should better reset it, | |
7363 | * so that state pruning has chances to take effect. | |
7364 | */ | |
7365 | reg->id = 0; | |
7366 | reg->ref_obj_id = 0; | |
7367 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
7368 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
7369 | * in release_reg_references(). | |
7370 | * | |
7371 | * reg->id is still used by spin_lock ptr. Other | |
7372 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
7373 | */ |
7374 | reg->id = 0; | |
56f668df | 7375 | } |
57a09bf0 TG |
7376 | } |
7377 | } | |
7378 | ||
c6a9efa1 PC |
7379 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
7380 | bool is_null) | |
7381 | { | |
7382 | struct bpf_reg_state *reg; | |
7383 | int i; | |
7384 | ||
7385 | for (i = 0; i < MAX_BPF_REG; i++) | |
7386 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
7387 | ||
7388 | bpf_for_each_spilled_reg(i, state, reg) { | |
7389 | if (!reg) | |
7390 | continue; | |
7391 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
7392 | } | |
7393 | } | |
7394 | ||
57a09bf0 TG |
7395 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
7396 | * be folded together at some point. | |
7397 | */ | |
840b9615 JS |
7398 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
7399 | bool is_null) | |
57a09bf0 | 7400 | { |
f4d7e40a | 7401 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 7402 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 7403 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 7404 | u32 id = regs[regno].id; |
c6a9efa1 | 7405 | int i; |
57a09bf0 | 7406 | |
1b986589 MKL |
7407 | if (ref_obj_id && ref_obj_id == id && is_null) |
7408 | /* regs[regno] is in the " == NULL" branch. | |
7409 | * No one could have freed the reference state before | |
7410 | * doing the NULL check. | |
7411 | */ | |
7412 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 7413 | |
c6a9efa1 PC |
7414 | for (i = 0; i <= vstate->curframe; i++) |
7415 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
7416 | } |
7417 | ||
5beca081 DB |
7418 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
7419 | struct bpf_reg_state *dst_reg, | |
7420 | struct bpf_reg_state *src_reg, | |
7421 | struct bpf_verifier_state *this_branch, | |
7422 | struct bpf_verifier_state *other_branch) | |
7423 | { | |
7424 | if (BPF_SRC(insn->code) != BPF_X) | |
7425 | return false; | |
7426 | ||
092ed096 JW |
7427 | /* Pointers are always 64-bit. */ |
7428 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
7429 | return false; | |
7430 | ||
5beca081 DB |
7431 | switch (BPF_OP(insn->code)) { |
7432 | case BPF_JGT: | |
7433 | if ((dst_reg->type == PTR_TO_PACKET && | |
7434 | src_reg->type == PTR_TO_PACKET_END) || | |
7435 | (dst_reg->type == PTR_TO_PACKET_META && | |
7436 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7437 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
7438 | find_good_pkt_pointers(this_branch, dst_reg, | |
7439 | dst_reg->type, false); | |
6d94e741 | 7440 | mark_pkt_end(other_branch, insn->dst_reg, true); |
5beca081 DB |
7441 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
7442 | src_reg->type == PTR_TO_PACKET) || | |
7443 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7444 | src_reg->type == PTR_TO_PACKET_META)) { | |
7445 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
7446 | find_good_pkt_pointers(other_branch, src_reg, | |
7447 | src_reg->type, true); | |
6d94e741 | 7448 | mark_pkt_end(this_branch, insn->src_reg, false); |
5beca081 DB |
7449 | } else { |
7450 | return false; | |
7451 | } | |
7452 | break; | |
7453 | case BPF_JLT: | |
7454 | if ((dst_reg->type == PTR_TO_PACKET && | |
7455 | src_reg->type == PTR_TO_PACKET_END) || | |
7456 | (dst_reg->type == PTR_TO_PACKET_META && | |
7457 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7458 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
7459 | find_good_pkt_pointers(other_branch, dst_reg, | |
7460 | dst_reg->type, true); | |
6d94e741 | 7461 | mark_pkt_end(this_branch, insn->dst_reg, false); |
5beca081 DB |
7462 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
7463 | src_reg->type == PTR_TO_PACKET) || | |
7464 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7465 | src_reg->type == PTR_TO_PACKET_META)) { | |
7466 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
7467 | find_good_pkt_pointers(this_branch, src_reg, | |
7468 | src_reg->type, false); | |
6d94e741 | 7469 | mark_pkt_end(other_branch, insn->src_reg, true); |
5beca081 DB |
7470 | } else { |
7471 | return false; | |
7472 | } | |
7473 | break; | |
7474 | case BPF_JGE: | |
7475 | if ((dst_reg->type == PTR_TO_PACKET && | |
7476 | src_reg->type == PTR_TO_PACKET_END) || | |
7477 | (dst_reg->type == PTR_TO_PACKET_META && | |
7478 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7479 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
7480 | find_good_pkt_pointers(this_branch, dst_reg, | |
7481 | dst_reg->type, true); | |
6d94e741 | 7482 | mark_pkt_end(other_branch, insn->dst_reg, false); |
5beca081 DB |
7483 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
7484 | src_reg->type == PTR_TO_PACKET) || | |
7485 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7486 | src_reg->type == PTR_TO_PACKET_META)) { | |
7487 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
7488 | find_good_pkt_pointers(other_branch, src_reg, | |
7489 | src_reg->type, false); | |
6d94e741 | 7490 | mark_pkt_end(this_branch, insn->src_reg, true); |
5beca081 DB |
7491 | } else { |
7492 | return false; | |
7493 | } | |
7494 | break; | |
7495 | case BPF_JLE: | |
7496 | if ((dst_reg->type == PTR_TO_PACKET && | |
7497 | src_reg->type == PTR_TO_PACKET_END) || | |
7498 | (dst_reg->type == PTR_TO_PACKET_META && | |
7499 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7500 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
7501 | find_good_pkt_pointers(other_branch, dst_reg, | |
7502 | dst_reg->type, false); | |
6d94e741 | 7503 | mark_pkt_end(this_branch, insn->dst_reg, true); |
5beca081 DB |
7504 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
7505 | src_reg->type == PTR_TO_PACKET) || | |
7506 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7507 | src_reg->type == PTR_TO_PACKET_META)) { | |
7508 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
7509 | find_good_pkt_pointers(this_branch, src_reg, | |
7510 | src_reg->type, true); | |
6d94e741 | 7511 | mark_pkt_end(other_branch, insn->src_reg, false); |
5beca081 DB |
7512 | } else { |
7513 | return false; | |
7514 | } | |
7515 | break; | |
7516 | default: | |
7517 | return false; | |
7518 | } | |
7519 | ||
7520 | return true; | |
7521 | } | |
7522 | ||
75748837 AS |
7523 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
7524 | struct bpf_reg_state *known_reg) | |
7525 | { | |
7526 | struct bpf_func_state *state; | |
7527 | struct bpf_reg_state *reg; | |
7528 | int i, j; | |
7529 | ||
7530 | for (i = 0; i <= vstate->curframe; i++) { | |
7531 | state = vstate->frame[i]; | |
7532 | for (j = 0; j < MAX_BPF_REG; j++) { | |
7533 | reg = &state->regs[j]; | |
7534 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
7535 | *reg = *known_reg; | |
7536 | } | |
7537 | ||
7538 | bpf_for_each_spilled_reg(j, state, reg) { | |
7539 | if (!reg) | |
7540 | continue; | |
7541 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) | |
7542 | *reg = *known_reg; | |
7543 | } | |
7544 | } | |
7545 | } | |
7546 | ||
58e2af8b | 7547 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
7548 | struct bpf_insn *insn, int *insn_idx) |
7549 | { | |
f4d7e40a AS |
7550 | struct bpf_verifier_state *this_branch = env->cur_state; |
7551 | struct bpf_verifier_state *other_branch; | |
7552 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 7553 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 7554 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 7555 | bool is_jmp32; |
fb8d251e | 7556 | int pred = -1; |
17a52670 AS |
7557 | int err; |
7558 | ||
092ed096 JW |
7559 | /* Only conditional jumps are expected to reach here. */ |
7560 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
7561 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
7562 | return -EINVAL; |
7563 | } | |
7564 | ||
7565 | if (BPF_SRC(insn->code) == BPF_X) { | |
7566 | if (insn->imm != 0) { | |
092ed096 | 7567 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
7568 | return -EINVAL; |
7569 | } | |
7570 | ||
7571 | /* check src1 operand */ | |
dc503a8a | 7572 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7573 | if (err) |
7574 | return err; | |
1be7f75d AS |
7575 | |
7576 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 7577 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
7578 | insn->src_reg); |
7579 | return -EACCES; | |
7580 | } | |
fb8d251e | 7581 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
7582 | } else { |
7583 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 7584 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
7585 | return -EINVAL; |
7586 | } | |
7587 | } | |
7588 | ||
7589 | /* check src2 operand */ | |
dc503a8a | 7590 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7591 | if (err) |
7592 | return err; | |
7593 | ||
1a0dc1ac | 7594 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 7595 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 7596 | |
3f50f132 JF |
7597 | if (BPF_SRC(insn->code) == BPF_K) { |
7598 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
7599 | } else if (src_reg->type == SCALAR_VALUE && | |
7600 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
7601 | pred = is_branch_taken(dst_reg, | |
7602 | tnum_subreg(src_reg->var_off).value, | |
7603 | opcode, | |
7604 | is_jmp32); | |
7605 | } else if (src_reg->type == SCALAR_VALUE && | |
7606 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
7607 | pred = is_branch_taken(dst_reg, | |
7608 | src_reg->var_off.value, | |
7609 | opcode, | |
7610 | is_jmp32); | |
6d94e741 AS |
7611 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
7612 | reg_is_pkt_pointer_any(src_reg) && | |
7613 | !is_jmp32) { | |
7614 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); | |
3f50f132 JF |
7615 | } |
7616 | ||
b5dc0163 | 7617 | if (pred >= 0) { |
cac616db JF |
7618 | /* If we get here with a dst_reg pointer type it is because |
7619 | * above is_branch_taken() special cased the 0 comparison. | |
7620 | */ | |
7621 | if (!__is_pointer_value(false, dst_reg)) | |
7622 | err = mark_chain_precision(env, insn->dst_reg); | |
6d94e741 AS |
7623 | if (BPF_SRC(insn->code) == BPF_X && !err && |
7624 | !__is_pointer_value(false, src_reg)) | |
b5dc0163 AS |
7625 | err = mark_chain_precision(env, insn->src_reg); |
7626 | if (err) | |
7627 | return err; | |
7628 | } | |
fb8d251e AS |
7629 | if (pred == 1) { |
7630 | /* only follow the goto, ignore fall-through */ | |
7631 | *insn_idx += insn->off; | |
7632 | return 0; | |
7633 | } else if (pred == 0) { | |
7634 | /* only follow fall-through branch, since | |
7635 | * that's where the program will go | |
7636 | */ | |
7637 | return 0; | |
17a52670 AS |
7638 | } |
7639 | ||
979d63d5 DB |
7640 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
7641 | false); | |
17a52670 AS |
7642 | if (!other_branch) |
7643 | return -EFAULT; | |
f4d7e40a | 7644 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 7645 | |
48461135 JB |
7646 | /* detect if we are comparing against a constant value so we can adjust |
7647 | * our min/max values for our dst register. | |
f1174f77 EC |
7648 | * this is only legit if both are scalars (or pointers to the same |
7649 | * object, I suppose, but we don't support that right now), because | |
7650 | * otherwise the different base pointers mean the offsets aren't | |
7651 | * comparable. | |
48461135 JB |
7652 | */ |
7653 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 7654 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 7655 | |
f1174f77 | 7656 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
7657 | src_reg->type == SCALAR_VALUE) { |
7658 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
7659 | (is_jmp32 && |
7660 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 7661 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 7662 | dst_reg, |
3f50f132 JF |
7663 | src_reg->var_off.value, |
7664 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
7665 | opcode, is_jmp32); |
7666 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
7667 | (is_jmp32 && |
7668 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 7669 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 7670 | src_reg, |
3f50f132 JF |
7671 | dst_reg->var_off.value, |
7672 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
7673 | opcode, is_jmp32); |
7674 | else if (!is_jmp32 && | |
7675 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 7676 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
7677 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
7678 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 7679 | src_reg, dst_reg, opcode); |
e688c3db AS |
7680 | if (src_reg->id && |
7681 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { | |
75748837 AS |
7682 | find_equal_scalars(this_branch, src_reg); |
7683 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); | |
7684 | } | |
7685 | ||
f1174f77 EC |
7686 | } |
7687 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 7688 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
7689 | dst_reg, insn->imm, (u32)insn->imm, |
7690 | opcode, is_jmp32); | |
48461135 JB |
7691 | } |
7692 | ||
e688c3db AS |
7693 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
7694 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { | |
75748837 AS |
7695 | find_equal_scalars(this_branch, dst_reg); |
7696 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); | |
7697 | } | |
7698 | ||
092ed096 JW |
7699 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
7700 | * NOTE: these optimizations below are related with pointer comparison | |
7701 | * which will never be JMP32. | |
7702 | */ | |
7703 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 7704 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
7705 | reg_type_may_be_null(dst_reg->type)) { |
7706 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
7707 | * safe or unknown depending R == 0 or R != 0 conditional. |
7708 | */ | |
840b9615 JS |
7709 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
7710 | opcode == BPF_JNE); | |
7711 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
7712 | opcode == BPF_JEQ); | |
5beca081 DB |
7713 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
7714 | this_branch, other_branch) && | |
7715 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
7716 | verbose(env, "R%d pointer comparison prohibited\n", |
7717 | insn->dst_reg); | |
1be7f75d | 7718 | return -EACCES; |
17a52670 | 7719 | } |
06ee7115 | 7720 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 7721 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
7722 | return 0; |
7723 | } | |
7724 | ||
17a52670 | 7725 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 7726 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 7727 | { |
d8eca5bb | 7728 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 7729 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 7730 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 7731 | struct bpf_map *map; |
17a52670 AS |
7732 | int err; |
7733 | ||
7734 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 7735 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
7736 | return -EINVAL; |
7737 | } | |
7738 | if (insn->off != 0) { | |
61bd5218 | 7739 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
7740 | return -EINVAL; |
7741 | } | |
7742 | ||
dc503a8a | 7743 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
7744 | if (err) |
7745 | return err; | |
7746 | ||
4976b718 | 7747 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 7748 | if (insn->src_reg == 0) { |
6b173873 JK |
7749 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
7750 | ||
4976b718 | 7751 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 7752 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 7753 | return 0; |
6b173873 | 7754 | } |
17a52670 | 7755 | |
4976b718 HL |
7756 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
7757 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
7758 | ||
7759 | dst_reg->type = aux->btf_var.reg_type; | |
7760 | switch (dst_reg->type) { | |
7761 | case PTR_TO_MEM: | |
7762 | dst_reg->mem_size = aux->btf_var.mem_size; | |
7763 | break; | |
7764 | case PTR_TO_BTF_ID: | |
eaa6bcb7 | 7765 | case PTR_TO_PERCPU_BTF_ID: |
22dc4a0f | 7766 | dst_reg->btf = aux->btf_var.btf; |
4976b718 HL |
7767 | dst_reg->btf_id = aux->btf_var.btf_id; |
7768 | break; | |
7769 | default: | |
7770 | verbose(env, "bpf verifier is misconfigured\n"); | |
7771 | return -EFAULT; | |
7772 | } | |
7773 | return 0; | |
7774 | } | |
7775 | ||
d8eca5bb DB |
7776 | map = env->used_maps[aux->map_index]; |
7777 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 7778 | dst_reg->map_ptr = map; |
d8eca5bb DB |
7779 | |
7780 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
4976b718 HL |
7781 | dst_reg->type = PTR_TO_MAP_VALUE; |
7782 | dst_reg->off = aux->map_off; | |
d8eca5bb | 7783 | if (map_value_has_spin_lock(map)) |
4976b718 | 7784 | dst_reg->id = ++env->id_gen; |
d8eca5bb | 7785 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
4976b718 | 7786 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
7787 | } else { |
7788 | verbose(env, "bpf verifier is misconfigured\n"); | |
7789 | return -EINVAL; | |
7790 | } | |
17a52670 | 7791 | |
17a52670 AS |
7792 | return 0; |
7793 | } | |
7794 | ||
96be4325 DB |
7795 | static bool may_access_skb(enum bpf_prog_type type) |
7796 | { | |
7797 | switch (type) { | |
7798 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
7799 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 7800 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
7801 | return true; |
7802 | default: | |
7803 | return false; | |
7804 | } | |
7805 | } | |
7806 | ||
ddd872bc AS |
7807 | /* verify safety of LD_ABS|LD_IND instructions: |
7808 | * - they can only appear in the programs where ctx == skb | |
7809 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
7810 | * preserve R6-R9, and store return value into R0 | |
7811 | * | |
7812 | * Implicit input: | |
7813 | * ctx == skb == R6 == CTX | |
7814 | * | |
7815 | * Explicit input: | |
7816 | * SRC == any register | |
7817 | * IMM == 32-bit immediate | |
7818 | * | |
7819 | * Output: | |
7820 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
7821 | */ | |
58e2af8b | 7822 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 7823 | { |
638f5b90 | 7824 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 7825 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 7826 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
7827 | int i, err; |
7828 | ||
7e40781c | 7829 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 7830 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
7831 | return -EINVAL; |
7832 | } | |
7833 | ||
e0cea7ce DB |
7834 | if (!env->ops->gen_ld_abs) { |
7835 | verbose(env, "bpf verifier is misconfigured\n"); | |
7836 | return -EINVAL; | |
7837 | } | |
7838 | ||
ddd872bc | 7839 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 7840 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 7841 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 7842 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
7843 | return -EINVAL; |
7844 | } | |
7845 | ||
7846 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 7847 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
7848 | if (err) |
7849 | return err; | |
7850 | ||
fd978bf7 JS |
7851 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
7852 | * gen_ld_abs() may terminate the program at runtime, leading to | |
7853 | * reference leak. | |
7854 | */ | |
7855 | err = check_reference_leak(env); | |
7856 | if (err) { | |
7857 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
7858 | return err; | |
7859 | } | |
7860 | ||
d83525ca AS |
7861 | if (env->cur_state->active_spin_lock) { |
7862 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
7863 | return -EINVAL; | |
7864 | } | |
7865 | ||
6d4f151a | 7866 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
7867 | verbose(env, |
7868 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
7869 | return -EINVAL; |
7870 | } | |
7871 | ||
7872 | if (mode == BPF_IND) { | |
7873 | /* check explicit source operand */ | |
dc503a8a | 7874 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
7875 | if (err) |
7876 | return err; | |
7877 | } | |
7878 | ||
6d4f151a DB |
7879 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
7880 | if (err < 0) | |
7881 | return err; | |
7882 | ||
ddd872bc | 7883 | /* reset caller saved regs to unreadable */ |
dc503a8a | 7884 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 7885 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
7886 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
7887 | } | |
ddd872bc AS |
7888 | |
7889 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
7890 | * the value fetched from the packet. |
7891 | * Already marked as written above. | |
ddd872bc | 7892 | */ |
61bd5218 | 7893 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
7894 | /* ld_abs load up to 32-bit skb data. */ |
7895 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
7896 | return 0; |
7897 | } | |
7898 | ||
390ee7e2 AS |
7899 | static int check_return_code(struct bpf_verifier_env *env) |
7900 | { | |
5cf1e914 | 7901 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 7902 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
7903 | struct bpf_reg_state *reg; |
7904 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 7905 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 | 7906 | int err; |
f782e2c3 | 7907 | const bool is_subprog = env->cur_state->frame[0]->subprogno; |
27ae7997 | 7908 | |
9e4e01df | 7909 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
f782e2c3 DB |
7910 | if (!is_subprog && |
7911 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || | |
7e40781c | 7912 | prog_type == BPF_PROG_TYPE_LSM) && |
27ae7997 MKL |
7913 | !prog->aux->attach_func_proto->type) |
7914 | return 0; | |
7915 | ||
7916 | /* eBPF calling convetion is such that R0 is used | |
7917 | * to return the value from eBPF program. | |
7918 | * Make sure that it's readable at this time | |
7919 | * of bpf_exit, which means that program wrote | |
7920 | * something into it earlier | |
7921 | */ | |
7922 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
7923 | if (err) | |
7924 | return err; | |
7925 | ||
7926 | if (is_pointer_value(env, BPF_REG_0)) { | |
7927 | verbose(env, "R0 leaks addr as return value\n"); | |
7928 | return -EACCES; | |
7929 | } | |
390ee7e2 | 7930 | |
f782e2c3 DB |
7931 | reg = cur_regs(env) + BPF_REG_0; |
7932 | if (is_subprog) { | |
7933 | if (reg->type != SCALAR_VALUE) { | |
7934 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", | |
7935 | reg_type_str[reg->type]); | |
7936 | return -EINVAL; | |
7937 | } | |
7938 | return 0; | |
7939 | } | |
7940 | ||
7e40781c | 7941 | switch (prog_type) { |
983695fa DB |
7942 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
7943 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
7944 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
7945 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
7946 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
7947 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
7948 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 7949 | range = tnum_range(1, 1); |
ed4ed404 | 7950 | break; |
390ee7e2 | 7951 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 7952 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
7953 | range = tnum_range(0, 3); | |
7954 | enforce_attach_type_range = tnum_range(2, 3); | |
7955 | } | |
ed4ed404 | 7956 | break; |
390ee7e2 AS |
7957 | case BPF_PROG_TYPE_CGROUP_SOCK: |
7958 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 7959 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 7960 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 7961 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 7962 | break; |
15ab09bd AS |
7963 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
7964 | if (!env->prog->aux->attach_btf_id) | |
7965 | return 0; | |
7966 | range = tnum_const(0); | |
7967 | break; | |
15d83c4d | 7968 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
7969 | switch (env->prog->expected_attach_type) { |
7970 | case BPF_TRACE_FENTRY: | |
7971 | case BPF_TRACE_FEXIT: | |
7972 | range = tnum_const(0); | |
7973 | break; | |
7974 | case BPF_TRACE_RAW_TP: | |
7975 | case BPF_MODIFY_RETURN: | |
15d83c4d | 7976 | return 0; |
2ec0616e DB |
7977 | case BPF_TRACE_ITER: |
7978 | break; | |
e92888c7 YS |
7979 | default: |
7980 | return -ENOTSUPP; | |
7981 | } | |
15d83c4d | 7982 | break; |
e9ddbb77 JS |
7983 | case BPF_PROG_TYPE_SK_LOOKUP: |
7984 | range = tnum_range(SK_DROP, SK_PASS); | |
7985 | break; | |
e92888c7 YS |
7986 | case BPF_PROG_TYPE_EXT: |
7987 | /* freplace program can return anything as its return value | |
7988 | * depends on the to-be-replaced kernel func or bpf program. | |
7989 | */ | |
390ee7e2 AS |
7990 | default: |
7991 | return 0; | |
7992 | } | |
7993 | ||
390ee7e2 | 7994 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 7995 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
7996 | reg_type_str[reg->type]); |
7997 | return -EINVAL; | |
7998 | } | |
7999 | ||
8000 | if (!tnum_in(range, reg->var_off)) { | |
5cf1e914 | 8001 | char tn_buf[48]; |
8002 | ||
61bd5218 | 8003 | verbose(env, "At program exit the register R0 "); |
390ee7e2 | 8004 | if (!tnum_is_unknown(reg->var_off)) { |
390ee7e2 | 8005 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 8006 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 8007 | } else { |
61bd5218 | 8008 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 8009 | } |
5cf1e914 | 8010 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
983695fa | 8011 | verbose(env, " should have been in %s\n", tn_buf); |
390ee7e2 AS |
8012 | return -EINVAL; |
8013 | } | |
5cf1e914 | 8014 | |
8015 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
8016 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
8017 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
8018 | return 0; |
8019 | } | |
8020 | ||
475fb78f AS |
8021 | /* non-recursive DFS pseudo code |
8022 | * 1 procedure DFS-iterative(G,v): | |
8023 | * 2 label v as discovered | |
8024 | * 3 let S be a stack | |
8025 | * 4 S.push(v) | |
8026 | * 5 while S is not empty | |
8027 | * 6 t <- S.pop() | |
8028 | * 7 if t is what we're looking for: | |
8029 | * 8 return t | |
8030 | * 9 for all edges e in G.adjacentEdges(t) do | |
8031 | * 10 if edge e is already labelled | |
8032 | * 11 continue with the next edge | |
8033 | * 12 w <- G.adjacentVertex(t,e) | |
8034 | * 13 if vertex w is not discovered and not explored | |
8035 | * 14 label e as tree-edge | |
8036 | * 15 label w as discovered | |
8037 | * 16 S.push(w) | |
8038 | * 17 continue at 5 | |
8039 | * 18 else if vertex w is discovered | |
8040 | * 19 label e as back-edge | |
8041 | * 20 else | |
8042 | * 21 // vertex w is explored | |
8043 | * 22 label e as forward- or cross-edge | |
8044 | * 23 label t as explored | |
8045 | * 24 S.pop() | |
8046 | * | |
8047 | * convention: | |
8048 | * 0x10 - discovered | |
8049 | * 0x11 - discovered and fall-through edge labelled | |
8050 | * 0x12 - discovered and fall-through and branch edges labelled | |
8051 | * 0x20 - explored | |
8052 | */ | |
8053 | ||
8054 | enum { | |
8055 | DISCOVERED = 0x10, | |
8056 | EXPLORED = 0x20, | |
8057 | FALLTHROUGH = 1, | |
8058 | BRANCH = 2, | |
8059 | }; | |
8060 | ||
dc2a4ebc AS |
8061 | static u32 state_htab_size(struct bpf_verifier_env *env) |
8062 | { | |
8063 | return env->prog->len; | |
8064 | } | |
8065 | ||
5d839021 AS |
8066 | static struct bpf_verifier_state_list **explored_state( |
8067 | struct bpf_verifier_env *env, | |
8068 | int idx) | |
8069 | { | |
dc2a4ebc AS |
8070 | struct bpf_verifier_state *cur = env->cur_state; |
8071 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
8072 | ||
8073 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
8074 | } |
8075 | ||
8076 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
8077 | { | |
a8f500af | 8078 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 8079 | } |
f1bca824 | 8080 | |
59e2e27d WAF |
8081 | enum { |
8082 | DONE_EXPLORING = 0, | |
8083 | KEEP_EXPLORING = 1, | |
8084 | }; | |
8085 | ||
475fb78f AS |
8086 | /* t, w, e - match pseudo-code above: |
8087 | * t - index of current instruction | |
8088 | * w - next instruction | |
8089 | * e - edge | |
8090 | */ | |
2589726d AS |
8091 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
8092 | bool loop_ok) | |
475fb78f | 8093 | { |
7df737e9 AS |
8094 | int *insn_stack = env->cfg.insn_stack; |
8095 | int *insn_state = env->cfg.insn_state; | |
8096 | ||
475fb78f | 8097 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
59e2e27d | 8098 | return DONE_EXPLORING; |
475fb78f AS |
8099 | |
8100 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
59e2e27d | 8101 | return DONE_EXPLORING; |
475fb78f AS |
8102 | |
8103 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 8104 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 8105 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
8106 | return -EINVAL; |
8107 | } | |
8108 | ||
f1bca824 AS |
8109 | if (e == BRANCH) |
8110 | /* mark branch target for state pruning */ | |
5d839021 | 8111 | init_explored_state(env, w); |
f1bca824 | 8112 | |
475fb78f AS |
8113 | if (insn_state[w] == 0) { |
8114 | /* tree-edge */ | |
8115 | insn_state[t] = DISCOVERED | e; | |
8116 | insn_state[w] = DISCOVERED; | |
7df737e9 | 8117 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 8118 | return -E2BIG; |
7df737e9 | 8119 | insn_stack[env->cfg.cur_stack++] = w; |
59e2e27d | 8120 | return KEEP_EXPLORING; |
475fb78f | 8121 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
2c78ee89 | 8122 | if (loop_ok && env->bpf_capable) |
59e2e27d | 8123 | return DONE_EXPLORING; |
d9762e84 MKL |
8124 | verbose_linfo(env, t, "%d: ", t); |
8125 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 8126 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
8127 | return -EINVAL; |
8128 | } else if (insn_state[w] == EXPLORED) { | |
8129 | /* forward- or cross-edge */ | |
8130 | insn_state[t] = DISCOVERED | e; | |
8131 | } else { | |
61bd5218 | 8132 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
8133 | return -EFAULT; |
8134 | } | |
59e2e27d WAF |
8135 | return DONE_EXPLORING; |
8136 | } | |
8137 | ||
8138 | /* Visits the instruction at index t and returns one of the following: | |
8139 | * < 0 - an error occurred | |
8140 | * DONE_EXPLORING - the instruction was fully explored | |
8141 | * KEEP_EXPLORING - there is still work to be done before it is fully explored | |
8142 | */ | |
8143 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) | |
8144 | { | |
8145 | struct bpf_insn *insns = env->prog->insnsi; | |
8146 | int ret; | |
8147 | ||
8148 | /* All non-branch instructions have a single fall-through edge. */ | |
8149 | if (BPF_CLASS(insns[t].code) != BPF_JMP && | |
8150 | BPF_CLASS(insns[t].code) != BPF_JMP32) | |
8151 | return push_insn(t, t + 1, FALLTHROUGH, env, false); | |
8152 | ||
8153 | switch (BPF_OP(insns[t].code)) { | |
8154 | case BPF_EXIT: | |
8155 | return DONE_EXPLORING; | |
8156 | ||
8157 | case BPF_CALL: | |
8158 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); | |
8159 | if (ret) | |
8160 | return ret; | |
8161 | ||
8162 | if (t + 1 < insn_cnt) | |
8163 | init_explored_state(env, t + 1); | |
8164 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { | |
8165 | init_explored_state(env, t); | |
8166 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, | |
8167 | env, false); | |
8168 | } | |
8169 | return ret; | |
8170 | ||
8171 | case BPF_JA: | |
8172 | if (BPF_SRC(insns[t].code) != BPF_K) | |
8173 | return -EINVAL; | |
8174 | ||
8175 | /* unconditional jump with single edge */ | |
8176 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, | |
8177 | true); | |
8178 | if (ret) | |
8179 | return ret; | |
8180 | ||
8181 | /* unconditional jmp is not a good pruning point, | |
8182 | * but it's marked, since backtracking needs | |
8183 | * to record jmp history in is_state_visited(). | |
8184 | */ | |
8185 | init_explored_state(env, t + insns[t].off + 1); | |
8186 | /* tell verifier to check for equivalent states | |
8187 | * after every call and jump | |
8188 | */ | |
8189 | if (t + 1 < insn_cnt) | |
8190 | init_explored_state(env, t + 1); | |
8191 | ||
8192 | return ret; | |
8193 | ||
8194 | default: | |
8195 | /* conditional jump with two edges */ | |
8196 | init_explored_state(env, t); | |
8197 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); | |
8198 | if (ret) | |
8199 | return ret; | |
8200 | ||
8201 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); | |
8202 | } | |
475fb78f AS |
8203 | } |
8204 | ||
8205 | /* non-recursive depth-first-search to detect loops in BPF program | |
8206 | * loop == back-edge in directed graph | |
8207 | */ | |
58e2af8b | 8208 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f | 8209 | { |
475fb78f | 8210 | int insn_cnt = env->prog->len; |
7df737e9 | 8211 | int *insn_stack, *insn_state; |
475fb78f | 8212 | int ret = 0; |
59e2e27d | 8213 | int i; |
475fb78f | 8214 | |
7df737e9 | 8215 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
8216 | if (!insn_state) |
8217 | return -ENOMEM; | |
8218 | ||
7df737e9 | 8219 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 8220 | if (!insn_stack) { |
71dde681 | 8221 | kvfree(insn_state); |
475fb78f AS |
8222 | return -ENOMEM; |
8223 | } | |
8224 | ||
8225 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
8226 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 8227 | env->cfg.cur_stack = 1; |
475fb78f | 8228 | |
59e2e27d WAF |
8229 | while (env->cfg.cur_stack > 0) { |
8230 | int t = insn_stack[env->cfg.cur_stack - 1]; | |
475fb78f | 8231 | |
59e2e27d WAF |
8232 | ret = visit_insn(t, insn_cnt, env); |
8233 | switch (ret) { | |
8234 | case DONE_EXPLORING: | |
8235 | insn_state[t] = EXPLORED; | |
8236 | env->cfg.cur_stack--; | |
8237 | break; | |
8238 | case KEEP_EXPLORING: | |
8239 | break; | |
8240 | default: | |
8241 | if (ret > 0) { | |
8242 | verbose(env, "visit_insn internal bug\n"); | |
8243 | ret = -EFAULT; | |
475fb78f | 8244 | } |
475fb78f | 8245 | goto err_free; |
59e2e27d | 8246 | } |
475fb78f AS |
8247 | } |
8248 | ||
59e2e27d | 8249 | if (env->cfg.cur_stack < 0) { |
61bd5218 | 8250 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
8251 | ret = -EFAULT; |
8252 | goto err_free; | |
8253 | } | |
475fb78f | 8254 | |
475fb78f AS |
8255 | for (i = 0; i < insn_cnt; i++) { |
8256 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 8257 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
8258 | ret = -EINVAL; |
8259 | goto err_free; | |
8260 | } | |
8261 | } | |
8262 | ret = 0; /* cfg looks good */ | |
8263 | ||
8264 | err_free: | |
71dde681 AS |
8265 | kvfree(insn_state); |
8266 | kvfree(insn_stack); | |
7df737e9 | 8267 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
8268 | return ret; |
8269 | } | |
8270 | ||
09b28d76 AS |
8271 | static int check_abnormal_return(struct bpf_verifier_env *env) |
8272 | { | |
8273 | int i; | |
8274 | ||
8275 | for (i = 1; i < env->subprog_cnt; i++) { | |
8276 | if (env->subprog_info[i].has_ld_abs) { | |
8277 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
8278 | return -EINVAL; | |
8279 | } | |
8280 | if (env->subprog_info[i].has_tail_call) { | |
8281 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
8282 | return -EINVAL; | |
8283 | } | |
8284 | } | |
8285 | return 0; | |
8286 | } | |
8287 | ||
838e9690 YS |
8288 | /* The minimum supported BTF func info size */ |
8289 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
8290 | #define MAX_FUNCINFO_REC_SIZE 252 | |
8291 | ||
c454a46b MKL |
8292 | static int check_btf_func(struct bpf_verifier_env *env, |
8293 | const union bpf_attr *attr, | |
8294 | union bpf_attr __user *uattr) | |
838e9690 | 8295 | { |
09b28d76 | 8296 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 8297 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 8298 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 8299 | struct bpf_func_info *krecord; |
8c1b6e69 | 8300 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
8301 | struct bpf_prog *prog; |
8302 | const struct btf *btf; | |
838e9690 | 8303 | void __user *urecord; |
d0b2818e | 8304 | u32 prev_offset = 0; |
09b28d76 | 8305 | bool scalar_return; |
e7ed83d6 | 8306 | int ret = -ENOMEM; |
838e9690 YS |
8307 | |
8308 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
8309 | if (!nfuncs) { |
8310 | if (check_abnormal_return(env)) | |
8311 | return -EINVAL; | |
838e9690 | 8312 | return 0; |
09b28d76 | 8313 | } |
838e9690 YS |
8314 | |
8315 | if (nfuncs != env->subprog_cnt) { | |
8316 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
8317 | return -EINVAL; | |
8318 | } | |
8319 | ||
8320 | urec_size = attr->func_info_rec_size; | |
8321 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
8322 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
8323 | urec_size % sizeof(u32)) { | |
8324 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
8325 | return -EINVAL; | |
8326 | } | |
8327 | ||
c454a46b MKL |
8328 | prog = env->prog; |
8329 | btf = prog->aux->btf; | |
838e9690 YS |
8330 | |
8331 | urecord = u64_to_user_ptr(attr->func_info); | |
8332 | min_size = min_t(u32, krec_size, urec_size); | |
8333 | ||
ba64e7d8 | 8334 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
8335 | if (!krecord) |
8336 | return -ENOMEM; | |
8c1b6e69 AS |
8337 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
8338 | if (!info_aux) | |
8339 | goto err_free; | |
ba64e7d8 | 8340 | |
838e9690 YS |
8341 | for (i = 0; i < nfuncs; i++) { |
8342 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
8343 | if (ret) { | |
8344 | if (ret == -E2BIG) { | |
8345 | verbose(env, "nonzero tailing record in func info"); | |
8346 | /* set the size kernel expects so loader can zero | |
8347 | * out the rest of the record. | |
8348 | */ | |
8349 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
8350 | ret = -EFAULT; | |
8351 | } | |
c454a46b | 8352 | goto err_free; |
838e9690 YS |
8353 | } |
8354 | ||
ba64e7d8 | 8355 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 8356 | ret = -EFAULT; |
c454a46b | 8357 | goto err_free; |
838e9690 YS |
8358 | } |
8359 | ||
d30d42e0 | 8360 | /* check insn_off */ |
09b28d76 | 8361 | ret = -EINVAL; |
838e9690 | 8362 | if (i == 0) { |
d30d42e0 | 8363 | if (krecord[i].insn_off) { |
838e9690 | 8364 | verbose(env, |
d30d42e0 MKL |
8365 | "nonzero insn_off %u for the first func info record", |
8366 | krecord[i].insn_off); | |
c454a46b | 8367 | goto err_free; |
838e9690 | 8368 | } |
d30d42e0 | 8369 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
8370 | verbose(env, |
8371 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 8372 | krecord[i].insn_off, prev_offset); |
c454a46b | 8373 | goto err_free; |
838e9690 YS |
8374 | } |
8375 | ||
d30d42e0 | 8376 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 8377 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 8378 | goto err_free; |
838e9690 YS |
8379 | } |
8380 | ||
8381 | /* check type_id */ | |
ba64e7d8 | 8382 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 8383 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 8384 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 8385 | krecord[i].type_id); |
c454a46b | 8386 | goto err_free; |
838e9690 | 8387 | } |
51c39bb1 | 8388 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
8389 | |
8390 | func_proto = btf_type_by_id(btf, type->type); | |
8391 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
8392 | /* btf_func_check() already verified it during BTF load */ | |
8393 | goto err_free; | |
8394 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
8395 | scalar_return = | |
8396 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); | |
8397 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { | |
8398 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
8399 | goto err_free; | |
8400 | } | |
8401 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
8402 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
8403 | goto err_free; | |
8404 | } | |
8405 | ||
d30d42e0 | 8406 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
8407 | urecord += urec_size; |
8408 | } | |
8409 | ||
ba64e7d8 YS |
8410 | prog->aux->func_info = krecord; |
8411 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 8412 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
8413 | return 0; |
8414 | ||
c454a46b | 8415 | err_free: |
ba64e7d8 | 8416 | kvfree(krecord); |
8c1b6e69 | 8417 | kfree(info_aux); |
838e9690 YS |
8418 | return ret; |
8419 | } | |
8420 | ||
ba64e7d8 YS |
8421 | static void adjust_btf_func(struct bpf_verifier_env *env) |
8422 | { | |
8c1b6e69 | 8423 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
8424 | int i; |
8425 | ||
8c1b6e69 | 8426 | if (!aux->func_info) |
ba64e7d8 YS |
8427 | return; |
8428 | ||
8429 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 8430 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
8431 | } |
8432 | ||
c454a46b MKL |
8433 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
8434 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
8435 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
8436 | ||
8437 | static int check_btf_line(struct bpf_verifier_env *env, | |
8438 | const union bpf_attr *attr, | |
8439 | union bpf_attr __user *uattr) | |
8440 | { | |
8441 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
8442 | struct bpf_subprog_info *sub; | |
8443 | struct bpf_line_info *linfo; | |
8444 | struct bpf_prog *prog; | |
8445 | const struct btf *btf; | |
8446 | void __user *ulinfo; | |
8447 | int err; | |
8448 | ||
8449 | nr_linfo = attr->line_info_cnt; | |
8450 | if (!nr_linfo) | |
8451 | return 0; | |
8452 | ||
8453 | rec_size = attr->line_info_rec_size; | |
8454 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
8455 | rec_size > MAX_LINEINFO_REC_SIZE || | |
8456 | rec_size & (sizeof(u32) - 1)) | |
8457 | return -EINVAL; | |
8458 | ||
8459 | /* Need to zero it in case the userspace may | |
8460 | * pass in a smaller bpf_line_info object. | |
8461 | */ | |
8462 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
8463 | GFP_KERNEL | __GFP_NOWARN); | |
8464 | if (!linfo) | |
8465 | return -ENOMEM; | |
8466 | ||
8467 | prog = env->prog; | |
8468 | btf = prog->aux->btf; | |
8469 | ||
8470 | s = 0; | |
8471 | sub = env->subprog_info; | |
8472 | ulinfo = u64_to_user_ptr(attr->line_info); | |
8473 | expected_size = sizeof(struct bpf_line_info); | |
8474 | ncopy = min_t(u32, expected_size, rec_size); | |
8475 | for (i = 0; i < nr_linfo; i++) { | |
8476 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
8477 | if (err) { | |
8478 | if (err == -E2BIG) { | |
8479 | verbose(env, "nonzero tailing record in line_info"); | |
8480 | if (put_user(expected_size, | |
8481 | &uattr->line_info_rec_size)) | |
8482 | err = -EFAULT; | |
8483 | } | |
8484 | goto err_free; | |
8485 | } | |
8486 | ||
8487 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
8488 | err = -EFAULT; | |
8489 | goto err_free; | |
8490 | } | |
8491 | ||
8492 | /* | |
8493 | * Check insn_off to ensure | |
8494 | * 1) strictly increasing AND | |
8495 | * 2) bounded by prog->len | |
8496 | * | |
8497 | * The linfo[0].insn_off == 0 check logically falls into | |
8498 | * the later "missing bpf_line_info for func..." case | |
8499 | * because the first linfo[0].insn_off must be the | |
8500 | * first sub also and the first sub must have | |
8501 | * subprog_info[0].start == 0. | |
8502 | */ | |
8503 | if ((i && linfo[i].insn_off <= prev_offset) || | |
8504 | linfo[i].insn_off >= prog->len) { | |
8505 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
8506 | i, linfo[i].insn_off, prev_offset, | |
8507 | prog->len); | |
8508 | err = -EINVAL; | |
8509 | goto err_free; | |
8510 | } | |
8511 | ||
fdbaa0be MKL |
8512 | if (!prog->insnsi[linfo[i].insn_off].code) { |
8513 | verbose(env, | |
8514 | "Invalid insn code at line_info[%u].insn_off\n", | |
8515 | i); | |
8516 | err = -EINVAL; | |
8517 | goto err_free; | |
8518 | } | |
8519 | ||
23127b33 MKL |
8520 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
8521 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
8522 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
8523 | err = -EINVAL; | |
8524 | goto err_free; | |
8525 | } | |
8526 | ||
8527 | if (s != env->subprog_cnt) { | |
8528 | if (linfo[i].insn_off == sub[s].start) { | |
8529 | sub[s].linfo_idx = i; | |
8530 | s++; | |
8531 | } else if (sub[s].start < linfo[i].insn_off) { | |
8532 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
8533 | err = -EINVAL; | |
8534 | goto err_free; | |
8535 | } | |
8536 | } | |
8537 | ||
8538 | prev_offset = linfo[i].insn_off; | |
8539 | ulinfo += rec_size; | |
8540 | } | |
8541 | ||
8542 | if (s != env->subprog_cnt) { | |
8543 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
8544 | env->subprog_cnt - s, s); | |
8545 | err = -EINVAL; | |
8546 | goto err_free; | |
8547 | } | |
8548 | ||
8549 | prog->aux->linfo = linfo; | |
8550 | prog->aux->nr_linfo = nr_linfo; | |
8551 | ||
8552 | return 0; | |
8553 | ||
8554 | err_free: | |
8555 | kvfree(linfo); | |
8556 | return err; | |
8557 | } | |
8558 | ||
8559 | static int check_btf_info(struct bpf_verifier_env *env, | |
8560 | const union bpf_attr *attr, | |
8561 | union bpf_attr __user *uattr) | |
8562 | { | |
8563 | struct btf *btf; | |
8564 | int err; | |
8565 | ||
09b28d76 AS |
8566 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
8567 | if (check_abnormal_return(env)) | |
8568 | return -EINVAL; | |
c454a46b | 8569 | return 0; |
09b28d76 | 8570 | } |
c454a46b MKL |
8571 | |
8572 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
8573 | if (IS_ERR(btf)) | |
8574 | return PTR_ERR(btf); | |
8575 | env->prog->aux->btf = btf; | |
8576 | ||
8577 | err = check_btf_func(env, attr, uattr); | |
8578 | if (err) | |
8579 | return err; | |
8580 | ||
8581 | err = check_btf_line(env, attr, uattr); | |
8582 | if (err) | |
8583 | return err; | |
8584 | ||
8585 | return 0; | |
ba64e7d8 YS |
8586 | } |
8587 | ||
f1174f77 EC |
8588 | /* check %cur's range satisfies %old's */ |
8589 | static bool range_within(struct bpf_reg_state *old, | |
8590 | struct bpf_reg_state *cur) | |
8591 | { | |
b03c9f9f EC |
8592 | return old->umin_value <= cur->umin_value && |
8593 | old->umax_value >= cur->umax_value && | |
8594 | old->smin_value <= cur->smin_value && | |
8595 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
8596 | } |
8597 | ||
8598 | /* Maximum number of register states that can exist at once */ | |
8599 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
8600 | struct idpair { | |
8601 | u32 old; | |
8602 | u32 cur; | |
8603 | }; | |
8604 | ||
8605 | /* If in the old state two registers had the same id, then they need to have | |
8606 | * the same id in the new state as well. But that id could be different from | |
8607 | * the old state, so we need to track the mapping from old to new ids. | |
8608 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
8609 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
8610 | * regs with a different old id could still have new id 9, we don't care about | |
8611 | * that. | |
8612 | * So we look through our idmap to see if this old id has been seen before. If | |
8613 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 8614 | */ |
f1174f77 | 8615 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 8616 | { |
f1174f77 | 8617 | unsigned int i; |
969bf05e | 8618 | |
f1174f77 EC |
8619 | for (i = 0; i < ID_MAP_SIZE; i++) { |
8620 | if (!idmap[i].old) { | |
8621 | /* Reached an empty slot; haven't seen this id before */ | |
8622 | idmap[i].old = old_id; | |
8623 | idmap[i].cur = cur_id; | |
8624 | return true; | |
8625 | } | |
8626 | if (idmap[i].old == old_id) | |
8627 | return idmap[i].cur == cur_id; | |
8628 | } | |
8629 | /* We ran out of idmap slots, which should be impossible */ | |
8630 | WARN_ON_ONCE(1); | |
8631 | return false; | |
8632 | } | |
8633 | ||
9242b5f5 AS |
8634 | static void clean_func_state(struct bpf_verifier_env *env, |
8635 | struct bpf_func_state *st) | |
8636 | { | |
8637 | enum bpf_reg_liveness live; | |
8638 | int i, j; | |
8639 | ||
8640 | for (i = 0; i < BPF_REG_FP; i++) { | |
8641 | live = st->regs[i].live; | |
8642 | /* liveness must not touch this register anymore */ | |
8643 | st->regs[i].live |= REG_LIVE_DONE; | |
8644 | if (!(live & REG_LIVE_READ)) | |
8645 | /* since the register is unused, clear its state | |
8646 | * to make further comparison simpler | |
8647 | */ | |
f54c7898 | 8648 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
8649 | } |
8650 | ||
8651 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
8652 | live = st->stack[i].spilled_ptr.live; | |
8653 | /* liveness must not touch this stack slot anymore */ | |
8654 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
8655 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 8656 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
8657 | for (j = 0; j < BPF_REG_SIZE; j++) |
8658 | st->stack[i].slot_type[j] = STACK_INVALID; | |
8659 | } | |
8660 | } | |
8661 | } | |
8662 | ||
8663 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
8664 | struct bpf_verifier_state *st) | |
8665 | { | |
8666 | int i; | |
8667 | ||
8668 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
8669 | /* all regs in this state in all frames were already marked */ | |
8670 | return; | |
8671 | ||
8672 | for (i = 0; i <= st->curframe; i++) | |
8673 | clean_func_state(env, st->frame[i]); | |
8674 | } | |
8675 | ||
8676 | /* the parentage chains form a tree. | |
8677 | * the verifier states are added to state lists at given insn and | |
8678 | * pushed into state stack for future exploration. | |
8679 | * when the verifier reaches bpf_exit insn some of the verifer states | |
8680 | * stored in the state lists have their final liveness state already, | |
8681 | * but a lot of states will get revised from liveness point of view when | |
8682 | * the verifier explores other branches. | |
8683 | * Example: | |
8684 | * 1: r0 = 1 | |
8685 | * 2: if r1 == 100 goto pc+1 | |
8686 | * 3: r0 = 2 | |
8687 | * 4: exit | |
8688 | * when the verifier reaches exit insn the register r0 in the state list of | |
8689 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
8690 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
8691 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
8692 | * | |
8693 | * Since the verifier pushes the branch states as it sees them while exploring | |
8694 | * the program the condition of walking the branch instruction for the second | |
8695 | * time means that all states below this branch were already explored and | |
8696 | * their final liveness markes are already propagated. | |
8697 | * Hence when the verifier completes the search of state list in is_state_visited() | |
8698 | * we can call this clean_live_states() function to mark all liveness states | |
8699 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
8700 | * will not be used. | |
8701 | * This function also clears the registers and stack for states that !READ | |
8702 | * to simplify state merging. | |
8703 | * | |
8704 | * Important note here that walking the same branch instruction in the callee | |
8705 | * doesn't meant that the states are DONE. The verifier has to compare | |
8706 | * the callsites | |
8707 | */ | |
8708 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
8709 | struct bpf_verifier_state *cur) | |
8710 | { | |
8711 | struct bpf_verifier_state_list *sl; | |
8712 | int i; | |
8713 | ||
5d839021 | 8714 | sl = *explored_state(env, insn); |
a8f500af | 8715 | while (sl) { |
2589726d AS |
8716 | if (sl->state.branches) |
8717 | goto next; | |
dc2a4ebc AS |
8718 | if (sl->state.insn_idx != insn || |
8719 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
8720 | goto next; |
8721 | for (i = 0; i <= cur->curframe; i++) | |
8722 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
8723 | goto next; | |
8724 | clean_verifier_state(env, &sl->state); | |
8725 | next: | |
8726 | sl = sl->next; | |
8727 | } | |
8728 | } | |
8729 | ||
f1174f77 | 8730 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
8731 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
8732 | struct idpair *idmap) | |
f1174f77 | 8733 | { |
f4d7e40a AS |
8734 | bool equal; |
8735 | ||
dc503a8a EC |
8736 | if (!(rold->live & REG_LIVE_READ)) |
8737 | /* explored state didn't use this */ | |
8738 | return true; | |
8739 | ||
679c782d | 8740 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
8741 | |
8742 | if (rold->type == PTR_TO_STACK) | |
8743 | /* two stack pointers are equal only if they're pointing to | |
8744 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
8745 | */ | |
8746 | return equal && rold->frameno == rcur->frameno; | |
8747 | ||
8748 | if (equal) | |
969bf05e AS |
8749 | return true; |
8750 | ||
f1174f77 EC |
8751 | if (rold->type == NOT_INIT) |
8752 | /* explored state can't have used this */ | |
969bf05e | 8753 | return true; |
f1174f77 EC |
8754 | if (rcur->type == NOT_INIT) |
8755 | return false; | |
8756 | switch (rold->type) { | |
8757 | case SCALAR_VALUE: | |
8758 | if (rcur->type == SCALAR_VALUE) { | |
b5dc0163 AS |
8759 | if (!rold->precise && !rcur->precise) |
8760 | return true; | |
f1174f77 EC |
8761 | /* new val must satisfy old val knowledge */ |
8762 | return range_within(rold, rcur) && | |
8763 | tnum_in(rold->var_off, rcur->var_off); | |
8764 | } else { | |
179d1c56 JH |
8765 | /* We're trying to use a pointer in place of a scalar. |
8766 | * Even if the scalar was unbounded, this could lead to | |
8767 | * pointer leaks because scalars are allowed to leak | |
8768 | * while pointers are not. We could make this safe in | |
8769 | * special cases if root is calling us, but it's | |
8770 | * probably not worth the hassle. | |
f1174f77 | 8771 | */ |
179d1c56 | 8772 | return false; |
f1174f77 EC |
8773 | } |
8774 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
8775 | /* If the new min/max/var_off satisfy the old ones and |
8776 | * everything else matches, we are OK. | |
d83525ca AS |
8777 | * 'id' is not compared, since it's only used for maps with |
8778 | * bpf_spin_lock inside map element and in such cases if | |
8779 | * the rest of the prog is valid for one map element then | |
8780 | * it's valid for all map elements regardless of the key | |
8781 | * used in bpf_map_lookup() | |
1b688a19 EC |
8782 | */ |
8783 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
8784 | range_within(rold, rcur) && | |
8785 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
8786 | case PTR_TO_MAP_VALUE_OR_NULL: |
8787 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
8788 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
8789 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
8790 | * checked, doing so could have affected others with the same | |
8791 | * id, and we can't check for that because we lost the id when | |
8792 | * we converted to a PTR_TO_MAP_VALUE. | |
8793 | */ | |
8794 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
8795 | return false; | |
8796 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
8797 | return false; | |
8798 | /* Check our ids match any regs they're supposed to */ | |
8799 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 8800 | case PTR_TO_PACKET_META: |
f1174f77 | 8801 | case PTR_TO_PACKET: |
de8f3a83 | 8802 | if (rcur->type != rold->type) |
f1174f77 EC |
8803 | return false; |
8804 | /* We must have at least as much range as the old ptr | |
8805 | * did, so that any accesses which were safe before are | |
8806 | * still safe. This is true even if old range < old off, | |
8807 | * since someone could have accessed through (ptr - k), or | |
8808 | * even done ptr -= k in a register, to get a safe access. | |
8809 | */ | |
8810 | if (rold->range > rcur->range) | |
8811 | return false; | |
8812 | /* If the offsets don't match, we can't trust our alignment; | |
8813 | * nor can we be sure that we won't fall out of range. | |
8814 | */ | |
8815 | if (rold->off != rcur->off) | |
8816 | return false; | |
8817 | /* id relations must be preserved */ | |
8818 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
8819 | return false; | |
8820 | /* new val must satisfy old val knowledge */ | |
8821 | return range_within(rold, rcur) && | |
8822 | tnum_in(rold->var_off, rcur->var_off); | |
8823 | case PTR_TO_CTX: | |
8824 | case CONST_PTR_TO_MAP: | |
f1174f77 | 8825 | case PTR_TO_PACKET_END: |
d58e468b | 8826 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
8827 | case PTR_TO_SOCKET: |
8828 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
8829 | case PTR_TO_SOCK_COMMON: |
8830 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
8831 | case PTR_TO_TCP_SOCK: |
8832 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 8833 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
8834 | /* Only valid matches are exact, which memcmp() above |
8835 | * would have accepted | |
8836 | */ | |
8837 | default: | |
8838 | /* Don't know what's going on, just say it's not safe */ | |
8839 | return false; | |
8840 | } | |
969bf05e | 8841 | |
f1174f77 EC |
8842 | /* Shouldn't get here; if we do, say it's not safe */ |
8843 | WARN_ON_ONCE(1); | |
969bf05e AS |
8844 | return false; |
8845 | } | |
8846 | ||
f4d7e40a AS |
8847 | static bool stacksafe(struct bpf_func_state *old, |
8848 | struct bpf_func_state *cur, | |
638f5b90 AS |
8849 | struct idpair *idmap) |
8850 | { | |
8851 | int i, spi; | |
8852 | ||
638f5b90 AS |
8853 | /* walk slots of the explored stack and ignore any additional |
8854 | * slots in the current stack, since explored(safe) state | |
8855 | * didn't use them | |
8856 | */ | |
8857 | for (i = 0; i < old->allocated_stack; i++) { | |
8858 | spi = i / BPF_REG_SIZE; | |
8859 | ||
b233920c AS |
8860 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
8861 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 8862 | /* explored state didn't use this */ |
fd05e57b | 8863 | continue; |
b233920c | 8864 | } |
cc2b14d5 | 8865 | |
638f5b90 AS |
8866 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
8867 | continue; | |
19e2dbb7 AS |
8868 | |
8869 | /* explored stack has more populated slots than current stack | |
8870 | * and these slots were used | |
8871 | */ | |
8872 | if (i >= cur->allocated_stack) | |
8873 | return false; | |
8874 | ||
cc2b14d5 AS |
8875 | /* if old state was safe with misc data in the stack |
8876 | * it will be safe with zero-initialized stack. | |
8877 | * The opposite is not true | |
8878 | */ | |
8879 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
8880 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
8881 | continue; | |
638f5b90 AS |
8882 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
8883 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
8884 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 8885 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
8886 | * this verifier states are not equivalent, |
8887 | * return false to continue verification of this path | |
8888 | */ | |
8889 | return false; | |
8890 | if (i % BPF_REG_SIZE) | |
8891 | continue; | |
8892 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
8893 | continue; | |
8894 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
8895 | &cur->stack[spi].spilled_ptr, | |
8896 | idmap)) | |
8897 | /* when explored and current stack slot are both storing | |
8898 | * spilled registers, check that stored pointers types | |
8899 | * are the same as well. | |
8900 | * Ex: explored safe path could have stored | |
8901 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
8902 | * but current path has stored: | |
8903 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
8904 | * such verifier states are not equivalent. | |
8905 | * return false to continue verification of this path | |
8906 | */ | |
8907 | return false; | |
8908 | } | |
8909 | return true; | |
8910 | } | |
8911 | ||
fd978bf7 JS |
8912 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
8913 | { | |
8914 | if (old->acquired_refs != cur->acquired_refs) | |
8915 | return false; | |
8916 | return !memcmp(old->refs, cur->refs, | |
8917 | sizeof(*old->refs) * old->acquired_refs); | |
8918 | } | |
8919 | ||
f1bca824 AS |
8920 | /* compare two verifier states |
8921 | * | |
8922 | * all states stored in state_list are known to be valid, since | |
8923 | * verifier reached 'bpf_exit' instruction through them | |
8924 | * | |
8925 | * this function is called when verifier exploring different branches of | |
8926 | * execution popped from the state stack. If it sees an old state that has | |
8927 | * more strict register state and more strict stack state then this execution | |
8928 | * branch doesn't need to be explored further, since verifier already | |
8929 | * concluded that more strict state leads to valid finish. | |
8930 | * | |
8931 | * Therefore two states are equivalent if register state is more conservative | |
8932 | * and explored stack state is more conservative than the current one. | |
8933 | * Example: | |
8934 | * explored current | |
8935 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
8936 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
8937 | * | |
8938 | * In other words if current stack state (one being explored) has more | |
8939 | * valid slots than old one that already passed validation, it means | |
8940 | * the verifier can stop exploring and conclude that current state is valid too | |
8941 | * | |
8942 | * Similarly with registers. If explored state has register type as invalid | |
8943 | * whereas register type in current state is meaningful, it means that | |
8944 | * the current state will reach 'bpf_exit' instruction safely | |
8945 | */ | |
f4d7e40a AS |
8946 | static bool func_states_equal(struct bpf_func_state *old, |
8947 | struct bpf_func_state *cur) | |
f1bca824 | 8948 | { |
f1174f77 EC |
8949 | struct idpair *idmap; |
8950 | bool ret = false; | |
f1bca824 AS |
8951 | int i; |
8952 | ||
f1174f77 EC |
8953 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
8954 | /* If we failed to allocate the idmap, just say it's not safe */ | |
8955 | if (!idmap) | |
1a0dc1ac | 8956 | return false; |
f1174f77 EC |
8957 | |
8958 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 8959 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 8960 | goto out_free; |
f1bca824 AS |
8961 | } |
8962 | ||
638f5b90 AS |
8963 | if (!stacksafe(old, cur, idmap)) |
8964 | goto out_free; | |
fd978bf7 JS |
8965 | |
8966 | if (!refsafe(old, cur)) | |
8967 | goto out_free; | |
f1174f77 EC |
8968 | ret = true; |
8969 | out_free: | |
8970 | kfree(idmap); | |
8971 | return ret; | |
f1bca824 AS |
8972 | } |
8973 | ||
f4d7e40a AS |
8974 | static bool states_equal(struct bpf_verifier_env *env, |
8975 | struct bpf_verifier_state *old, | |
8976 | struct bpf_verifier_state *cur) | |
8977 | { | |
8978 | int i; | |
8979 | ||
8980 | if (old->curframe != cur->curframe) | |
8981 | return false; | |
8982 | ||
979d63d5 DB |
8983 | /* Verification state from speculative execution simulation |
8984 | * must never prune a non-speculative execution one. | |
8985 | */ | |
8986 | if (old->speculative && !cur->speculative) | |
8987 | return false; | |
8988 | ||
d83525ca AS |
8989 | if (old->active_spin_lock != cur->active_spin_lock) |
8990 | return false; | |
8991 | ||
f4d7e40a AS |
8992 | /* for states to be equal callsites have to be the same |
8993 | * and all frame states need to be equivalent | |
8994 | */ | |
8995 | for (i = 0; i <= old->curframe; i++) { | |
8996 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
8997 | return false; | |
8998 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
8999 | return false; | |
9000 | } | |
9001 | return true; | |
9002 | } | |
9003 | ||
5327ed3d JW |
9004 | /* Return 0 if no propagation happened. Return negative error code if error |
9005 | * happened. Otherwise, return the propagated bit. | |
9006 | */ | |
55e7f3b5 JW |
9007 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
9008 | struct bpf_reg_state *reg, | |
9009 | struct bpf_reg_state *parent_reg) | |
9010 | { | |
5327ed3d JW |
9011 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
9012 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
9013 | int err; |
9014 | ||
5327ed3d JW |
9015 | /* When comes here, read flags of PARENT_REG or REG could be any of |
9016 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
9017 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
9018 | */ | |
9019 | if (parent_flag == REG_LIVE_READ64 || | |
9020 | /* Or if there is no read flag from REG. */ | |
9021 | !flag || | |
9022 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
9023 | parent_flag == flag) | |
55e7f3b5 JW |
9024 | return 0; |
9025 | ||
5327ed3d | 9026 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
9027 | if (err) |
9028 | return err; | |
9029 | ||
5327ed3d | 9030 | return flag; |
55e7f3b5 JW |
9031 | } |
9032 | ||
8e9cd9ce | 9033 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
9034 | * straight-line code between a state and its parent. When we arrive at an |
9035 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
9036 | * code, so read marks in the state must propagate to the parent regardless | |
9037 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 9038 | * in mark_reg_read() is for. |
8e9cd9ce | 9039 | */ |
f4d7e40a AS |
9040 | static int propagate_liveness(struct bpf_verifier_env *env, |
9041 | const struct bpf_verifier_state *vstate, | |
9042 | struct bpf_verifier_state *vparent) | |
dc503a8a | 9043 | { |
3f8cafa4 | 9044 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 9045 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 9046 | int i, frame, err = 0; |
dc503a8a | 9047 | |
f4d7e40a AS |
9048 | if (vparent->curframe != vstate->curframe) { |
9049 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
9050 | vparent->curframe, vstate->curframe); | |
9051 | return -EFAULT; | |
9052 | } | |
dc503a8a EC |
9053 | /* Propagate read liveness of registers... */ |
9054 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 9055 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
9056 | parent = vparent->frame[frame]; |
9057 | state = vstate->frame[frame]; | |
9058 | parent_reg = parent->regs; | |
9059 | state_reg = state->regs; | |
83d16312 JK |
9060 | /* We don't need to worry about FP liveness, it's read-only */ |
9061 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
9062 | err = propagate_liveness_reg(env, &state_reg[i], |
9063 | &parent_reg[i]); | |
5327ed3d | 9064 | if (err < 0) |
3f8cafa4 | 9065 | return err; |
5327ed3d JW |
9066 | if (err == REG_LIVE_READ64) |
9067 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 9068 | } |
f4d7e40a | 9069 | |
1b04aee7 | 9070 | /* Propagate stack slots. */ |
f4d7e40a AS |
9071 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
9072 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
9073 | parent_reg = &parent->stack[i].spilled_ptr; |
9074 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
9075 | err = propagate_liveness_reg(env, state_reg, |
9076 | parent_reg); | |
5327ed3d | 9077 | if (err < 0) |
3f8cafa4 | 9078 | return err; |
dc503a8a EC |
9079 | } |
9080 | } | |
5327ed3d | 9081 | return 0; |
dc503a8a EC |
9082 | } |
9083 | ||
a3ce685d AS |
9084 | /* find precise scalars in the previous equivalent state and |
9085 | * propagate them into the current state | |
9086 | */ | |
9087 | static int propagate_precision(struct bpf_verifier_env *env, | |
9088 | const struct bpf_verifier_state *old) | |
9089 | { | |
9090 | struct bpf_reg_state *state_reg; | |
9091 | struct bpf_func_state *state; | |
9092 | int i, err = 0; | |
9093 | ||
9094 | state = old->frame[old->curframe]; | |
9095 | state_reg = state->regs; | |
9096 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
9097 | if (state_reg->type != SCALAR_VALUE || | |
9098 | !state_reg->precise) | |
9099 | continue; | |
9100 | if (env->log.level & BPF_LOG_LEVEL2) | |
9101 | verbose(env, "propagating r%d\n", i); | |
9102 | err = mark_chain_precision(env, i); | |
9103 | if (err < 0) | |
9104 | return err; | |
9105 | } | |
9106 | ||
9107 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
9108 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
9109 | continue; | |
9110 | state_reg = &state->stack[i].spilled_ptr; | |
9111 | if (state_reg->type != SCALAR_VALUE || | |
9112 | !state_reg->precise) | |
9113 | continue; | |
9114 | if (env->log.level & BPF_LOG_LEVEL2) | |
9115 | verbose(env, "propagating fp%d\n", | |
9116 | (-i - 1) * BPF_REG_SIZE); | |
9117 | err = mark_chain_precision_stack(env, i); | |
9118 | if (err < 0) | |
9119 | return err; | |
9120 | } | |
9121 | return 0; | |
9122 | } | |
9123 | ||
2589726d AS |
9124 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
9125 | struct bpf_verifier_state *cur) | |
9126 | { | |
9127 | struct bpf_func_state *fold, *fcur; | |
9128 | int i, fr = cur->curframe; | |
9129 | ||
9130 | if (old->curframe != fr) | |
9131 | return false; | |
9132 | ||
9133 | fold = old->frame[fr]; | |
9134 | fcur = cur->frame[fr]; | |
9135 | for (i = 0; i < MAX_BPF_REG; i++) | |
9136 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
9137 | offsetof(struct bpf_reg_state, parent))) | |
9138 | return false; | |
9139 | return true; | |
9140 | } | |
9141 | ||
9142 | ||
58e2af8b | 9143 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 9144 | { |
58e2af8b | 9145 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 9146 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 9147 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 9148 | int i, j, err, states_cnt = 0; |
10d274e8 | 9149 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 9150 | |
b5dc0163 | 9151 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 9152 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
9153 | /* this 'insn_idx' instruction wasn't marked, so we will not |
9154 | * be doing state search here | |
9155 | */ | |
9156 | return 0; | |
9157 | ||
2589726d AS |
9158 | /* bpf progs typically have pruning point every 4 instructions |
9159 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
9160 | * Do not add new state for future pruning if the verifier hasn't seen | |
9161 | * at least 2 jumps and at least 8 instructions. | |
9162 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
9163 | * In tests that amounts to up to 50% reduction into total verifier | |
9164 | * memory consumption and 20% verifier time speedup. | |
9165 | */ | |
9166 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
9167 | env->insn_processed - env->prev_insn_processed >= 8) | |
9168 | add_new_state = true; | |
9169 | ||
a8f500af AS |
9170 | pprev = explored_state(env, insn_idx); |
9171 | sl = *pprev; | |
9172 | ||
9242b5f5 AS |
9173 | clean_live_states(env, insn_idx, cur); |
9174 | ||
a8f500af | 9175 | while (sl) { |
dc2a4ebc AS |
9176 | states_cnt++; |
9177 | if (sl->state.insn_idx != insn_idx) | |
9178 | goto next; | |
2589726d AS |
9179 | if (sl->state.branches) { |
9180 | if (states_maybe_looping(&sl->state, cur) && | |
9181 | states_equal(env, &sl->state, cur)) { | |
9182 | verbose_linfo(env, insn_idx, "; "); | |
9183 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
9184 | return -EINVAL; | |
9185 | } | |
9186 | /* if the verifier is processing a loop, avoid adding new state | |
9187 | * too often, since different loop iterations have distinct | |
9188 | * states and may not help future pruning. | |
9189 | * This threshold shouldn't be too low to make sure that | |
9190 | * a loop with large bound will be rejected quickly. | |
9191 | * The most abusive loop will be: | |
9192 | * r1 += 1 | |
9193 | * if r1 < 1000000 goto pc-2 | |
9194 | * 1M insn_procssed limit / 100 == 10k peak states. | |
9195 | * This threshold shouldn't be too high either, since states | |
9196 | * at the end of the loop are likely to be useful in pruning. | |
9197 | */ | |
9198 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
9199 | env->insn_processed - env->prev_insn_processed < 100) | |
9200 | add_new_state = false; | |
9201 | goto miss; | |
9202 | } | |
638f5b90 | 9203 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 9204 | sl->hit_cnt++; |
f1bca824 | 9205 | /* reached equivalent register/stack state, |
dc503a8a EC |
9206 | * prune the search. |
9207 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
9208 | * If we have any write marks in env->cur_state, they |
9209 | * will prevent corresponding reads in the continuation | |
9210 | * from reaching our parent (an explored_state). Our | |
9211 | * own state will get the read marks recorded, but | |
9212 | * they'll be immediately forgotten as we're pruning | |
9213 | * this state and will pop a new one. | |
f1bca824 | 9214 | */ |
f4d7e40a | 9215 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
9216 | |
9217 | /* if previous state reached the exit with precision and | |
9218 | * current state is equivalent to it (except precsion marks) | |
9219 | * the precision needs to be propagated back in | |
9220 | * the current state. | |
9221 | */ | |
9222 | err = err ? : push_jmp_history(env, cur); | |
9223 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
9224 | if (err) |
9225 | return err; | |
f1bca824 | 9226 | return 1; |
dc503a8a | 9227 | } |
2589726d AS |
9228 | miss: |
9229 | /* when new state is not going to be added do not increase miss count. | |
9230 | * Otherwise several loop iterations will remove the state | |
9231 | * recorded earlier. The goal of these heuristics is to have | |
9232 | * states from some iterations of the loop (some in the beginning | |
9233 | * and some at the end) to help pruning. | |
9234 | */ | |
9235 | if (add_new_state) | |
9236 | sl->miss_cnt++; | |
9f4686c4 AS |
9237 | /* heuristic to determine whether this state is beneficial |
9238 | * to keep checking from state equivalence point of view. | |
9239 | * Higher numbers increase max_states_per_insn and verification time, | |
9240 | * but do not meaningfully decrease insn_processed. | |
9241 | */ | |
9242 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
9243 | /* the state is unlikely to be useful. Remove it to | |
9244 | * speed up verification | |
9245 | */ | |
9246 | *pprev = sl->next; | |
9247 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
9248 | u32 br = sl->state.branches; |
9249 | ||
9250 | WARN_ONCE(br, | |
9251 | "BUG live_done but branches_to_explore %d\n", | |
9252 | br); | |
9f4686c4 AS |
9253 | free_verifier_state(&sl->state, false); |
9254 | kfree(sl); | |
9255 | env->peak_states--; | |
9256 | } else { | |
9257 | /* cannot free this state, since parentage chain may | |
9258 | * walk it later. Add it for free_list instead to | |
9259 | * be freed at the end of verification | |
9260 | */ | |
9261 | sl->next = env->free_list; | |
9262 | env->free_list = sl; | |
9263 | } | |
9264 | sl = *pprev; | |
9265 | continue; | |
9266 | } | |
dc2a4ebc | 9267 | next: |
9f4686c4 AS |
9268 | pprev = &sl->next; |
9269 | sl = *pprev; | |
f1bca824 AS |
9270 | } |
9271 | ||
06ee7115 AS |
9272 | if (env->max_states_per_insn < states_cnt) |
9273 | env->max_states_per_insn = states_cnt; | |
9274 | ||
2c78ee89 | 9275 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 9276 | return push_jmp_history(env, cur); |
ceefbc96 | 9277 | |
2589726d | 9278 | if (!add_new_state) |
b5dc0163 | 9279 | return push_jmp_history(env, cur); |
ceefbc96 | 9280 | |
2589726d AS |
9281 | /* There were no equivalent states, remember the current one. |
9282 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 9283 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 9284 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 9285 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
9286 | * again on the way to bpf_exit. |
9287 | * When looping the sl->state.branches will be > 0 and this state | |
9288 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 9289 | */ |
638f5b90 | 9290 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
9291 | if (!new_sl) |
9292 | return -ENOMEM; | |
06ee7115 AS |
9293 | env->total_states++; |
9294 | env->peak_states++; | |
2589726d AS |
9295 | env->prev_jmps_processed = env->jmps_processed; |
9296 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
9297 | |
9298 | /* add new state to the head of linked list */ | |
679c782d EC |
9299 | new = &new_sl->state; |
9300 | err = copy_verifier_state(new, cur); | |
1969db47 | 9301 | if (err) { |
679c782d | 9302 | free_verifier_state(new, false); |
1969db47 AS |
9303 | kfree(new_sl); |
9304 | return err; | |
9305 | } | |
dc2a4ebc | 9306 | new->insn_idx = insn_idx; |
2589726d AS |
9307 | WARN_ONCE(new->branches != 1, |
9308 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 9309 | |
2589726d | 9310 | cur->parent = new; |
b5dc0163 AS |
9311 | cur->first_insn_idx = insn_idx; |
9312 | clear_jmp_history(cur); | |
5d839021 AS |
9313 | new_sl->next = *explored_state(env, insn_idx); |
9314 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
9315 | /* connect new state to parentage chain. Current frame needs all |
9316 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
9317 | * to the stack implicitly by JITs) so in callers' frames connect just | |
9318 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
9319 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
9320 | * from callee with its full parentage chain, anyway. | |
9321 | */ | |
8e9cd9ce EC |
9322 | /* clear write marks in current state: the writes we did are not writes |
9323 | * our child did, so they don't screen off its reads from us. | |
9324 | * (There are no read marks in current state, because reads always mark | |
9325 | * their parent and current state never has children yet. Only | |
9326 | * explored_states can get read marks.) | |
9327 | */ | |
eea1c227 AS |
9328 | for (j = 0; j <= cur->curframe; j++) { |
9329 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
9330 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
9331 | for (i = 0; i < BPF_REG_FP; i++) | |
9332 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
9333 | } | |
f4d7e40a AS |
9334 | |
9335 | /* all stack frames are accessible from callee, clear them all */ | |
9336 | for (j = 0; j <= cur->curframe; j++) { | |
9337 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 9338 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 9339 | |
679c782d | 9340 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 9341 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
9342 | frame->stack[i].spilled_ptr.parent = |
9343 | &newframe->stack[i].spilled_ptr; | |
9344 | } | |
f4d7e40a | 9345 | } |
f1bca824 AS |
9346 | return 0; |
9347 | } | |
9348 | ||
c64b7983 JS |
9349 | /* Return true if it's OK to have the same insn return a different type. */ |
9350 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
9351 | { | |
9352 | switch (type) { | |
9353 | case PTR_TO_CTX: | |
9354 | case PTR_TO_SOCKET: | |
9355 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
9356 | case PTR_TO_SOCK_COMMON: |
9357 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
9358 | case PTR_TO_TCP_SOCK: |
9359 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 9360 | case PTR_TO_XDP_SOCK: |
2a02759e | 9361 | case PTR_TO_BTF_ID: |
b121b341 | 9362 | case PTR_TO_BTF_ID_OR_NULL: |
c64b7983 JS |
9363 | return false; |
9364 | default: | |
9365 | return true; | |
9366 | } | |
9367 | } | |
9368 | ||
9369 | /* If an instruction was previously used with particular pointer types, then we | |
9370 | * need to be careful to avoid cases such as the below, where it may be ok | |
9371 | * for one branch accessing the pointer, but not ok for the other branch: | |
9372 | * | |
9373 | * R1 = sock_ptr | |
9374 | * goto X; | |
9375 | * ... | |
9376 | * R1 = some_other_valid_ptr; | |
9377 | * goto X; | |
9378 | * ... | |
9379 | * R2 = *(u32 *)(R1 + 0); | |
9380 | */ | |
9381 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
9382 | { | |
9383 | return src != prev && (!reg_type_mismatch_ok(src) || | |
9384 | !reg_type_mismatch_ok(prev)); | |
9385 | } | |
9386 | ||
58e2af8b | 9387 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 9388 | { |
6f8a57cc | 9389 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 9390 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 9391 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 9392 | struct bpf_reg_state *regs; |
06ee7115 | 9393 | int insn_cnt = env->prog->len; |
17a52670 | 9394 | bool do_print_state = false; |
b5dc0163 | 9395 | int prev_insn_idx = -1; |
17a52670 | 9396 | |
17a52670 AS |
9397 | for (;;) { |
9398 | struct bpf_insn *insn; | |
9399 | u8 class; | |
9400 | int err; | |
9401 | ||
b5dc0163 | 9402 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 9403 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 9404 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 9405 | env->insn_idx, insn_cnt); |
17a52670 AS |
9406 | return -EFAULT; |
9407 | } | |
9408 | ||
c08435ec | 9409 | insn = &insns[env->insn_idx]; |
17a52670 AS |
9410 | class = BPF_CLASS(insn->code); |
9411 | ||
06ee7115 | 9412 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
9413 | verbose(env, |
9414 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 9415 | env->insn_processed); |
17a52670 AS |
9416 | return -E2BIG; |
9417 | } | |
9418 | ||
c08435ec | 9419 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
9420 | if (err < 0) |
9421 | return err; | |
9422 | if (err == 1) { | |
9423 | /* found equivalent state, can prune the search */ | |
06ee7115 | 9424 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 9425 | if (do_print_state) |
979d63d5 DB |
9426 | verbose(env, "\nfrom %d to %d%s: safe\n", |
9427 | env->prev_insn_idx, env->insn_idx, | |
9428 | env->cur_state->speculative ? | |
9429 | " (speculative execution)" : ""); | |
f1bca824 | 9430 | else |
c08435ec | 9431 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
9432 | } |
9433 | goto process_bpf_exit; | |
9434 | } | |
9435 | ||
c3494801 AS |
9436 | if (signal_pending(current)) |
9437 | return -EAGAIN; | |
9438 | ||
3c2ce60b DB |
9439 | if (need_resched()) |
9440 | cond_resched(); | |
9441 | ||
06ee7115 AS |
9442 | if (env->log.level & BPF_LOG_LEVEL2 || |
9443 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
9444 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 9445 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 9446 | else |
979d63d5 DB |
9447 | verbose(env, "\nfrom %d to %d%s:", |
9448 | env->prev_insn_idx, env->insn_idx, | |
9449 | env->cur_state->speculative ? | |
9450 | " (speculative execution)" : ""); | |
f4d7e40a | 9451 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
9452 | do_print_state = false; |
9453 | } | |
9454 | ||
06ee7115 | 9455 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
9456 | const struct bpf_insn_cbs cbs = { |
9457 | .cb_print = verbose, | |
abe08840 | 9458 | .private_data = env, |
7105e828 DB |
9459 | }; |
9460 | ||
c08435ec DB |
9461 | verbose_linfo(env, env->insn_idx, "; "); |
9462 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 9463 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
9464 | } |
9465 | ||
cae1927c | 9466 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
9467 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
9468 | env->prev_insn_idx); | |
cae1927c JK |
9469 | if (err) |
9470 | return err; | |
9471 | } | |
13a27dfc | 9472 | |
638f5b90 | 9473 | regs = cur_regs(env); |
51c39bb1 | 9474 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
b5dc0163 | 9475 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 9476 | |
17a52670 | 9477 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 9478 | err = check_alu_op(env, insn); |
17a52670 AS |
9479 | if (err) |
9480 | return err; | |
9481 | ||
9482 | } else if (class == BPF_LDX) { | |
3df126f3 | 9483 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
9484 | |
9485 | /* check for reserved fields is already done */ | |
9486 | ||
17a52670 | 9487 | /* check src operand */ |
dc503a8a | 9488 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9489 | if (err) |
9490 | return err; | |
9491 | ||
dc503a8a | 9492 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
9493 | if (err) |
9494 | return err; | |
9495 | ||
725f9dcd AS |
9496 | src_reg_type = regs[insn->src_reg].type; |
9497 | ||
17a52670 AS |
9498 | /* check that memory (src_reg + off) is readable, |
9499 | * the state of dst_reg will be updated by this func | |
9500 | */ | |
c08435ec DB |
9501 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
9502 | insn->off, BPF_SIZE(insn->code), | |
9503 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
9504 | if (err) |
9505 | return err; | |
9506 | ||
c08435ec | 9507 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
9508 | |
9509 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
9510 | /* saw a valid insn |
9511 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 9512 | * save type to validate intersecting paths |
9bac3d6d | 9513 | */ |
3df126f3 | 9514 | *prev_src_type = src_reg_type; |
9bac3d6d | 9515 | |
c64b7983 | 9516 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
9517 | /* ABuser program is trying to use the same insn |
9518 | * dst_reg = *(u32*) (src_reg + off) | |
9519 | * with different pointer types: | |
9520 | * src_reg == ctx in one branch and | |
9521 | * src_reg == stack|map in some other branch. | |
9522 | * Reject it. | |
9523 | */ | |
61bd5218 | 9524 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
9525 | return -EINVAL; |
9526 | } | |
9527 | ||
17a52670 | 9528 | } else if (class == BPF_STX) { |
3df126f3 | 9529 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 9530 | |
91c960b0 BJ |
9531 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
9532 | err = check_atomic(env, env->insn_idx, insn); | |
17a52670 AS |
9533 | if (err) |
9534 | return err; | |
c08435ec | 9535 | env->insn_idx++; |
17a52670 AS |
9536 | continue; |
9537 | } | |
9538 | ||
17a52670 | 9539 | /* check src1 operand */ |
dc503a8a | 9540 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9541 | if (err) |
9542 | return err; | |
9543 | /* check src2 operand */ | |
dc503a8a | 9544 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9545 | if (err) |
9546 | return err; | |
9547 | ||
d691f9e8 AS |
9548 | dst_reg_type = regs[insn->dst_reg].type; |
9549 | ||
17a52670 | 9550 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
9551 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
9552 | insn->off, BPF_SIZE(insn->code), | |
9553 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
9554 | if (err) |
9555 | return err; | |
9556 | ||
c08435ec | 9557 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
9558 | |
9559 | if (*prev_dst_type == NOT_INIT) { | |
9560 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 9561 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 9562 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
9563 | return -EINVAL; |
9564 | } | |
9565 | ||
17a52670 AS |
9566 | } else if (class == BPF_ST) { |
9567 | if (BPF_MODE(insn->code) != BPF_MEM || | |
9568 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 9569 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
9570 | return -EINVAL; |
9571 | } | |
9572 | /* check src operand */ | |
dc503a8a | 9573 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9574 | if (err) |
9575 | return err; | |
9576 | ||
f37a8cb8 | 9577 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 9578 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
9579 | insn->dst_reg, |
9580 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
9581 | return -EACCES; |
9582 | } | |
9583 | ||
17a52670 | 9584 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
9585 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
9586 | insn->off, BPF_SIZE(insn->code), | |
9587 | BPF_WRITE, -1, false); | |
17a52670 AS |
9588 | if (err) |
9589 | return err; | |
9590 | ||
092ed096 | 9591 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
9592 | u8 opcode = BPF_OP(insn->code); |
9593 | ||
2589726d | 9594 | env->jmps_processed++; |
17a52670 AS |
9595 | if (opcode == BPF_CALL) { |
9596 | if (BPF_SRC(insn->code) != BPF_K || | |
9597 | insn->off != 0 || | |
f4d7e40a AS |
9598 | (insn->src_reg != BPF_REG_0 && |
9599 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
9600 | insn->dst_reg != BPF_REG_0 || |
9601 | class == BPF_JMP32) { | |
61bd5218 | 9602 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
9603 | return -EINVAL; |
9604 | } | |
9605 | ||
d83525ca AS |
9606 | if (env->cur_state->active_spin_lock && |
9607 | (insn->src_reg == BPF_PSEUDO_CALL || | |
9608 | insn->imm != BPF_FUNC_spin_unlock)) { | |
9609 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
9610 | return -EINVAL; | |
9611 | } | |
f4d7e40a | 9612 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 9613 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 9614 | else |
c08435ec | 9615 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
9616 | if (err) |
9617 | return err; | |
9618 | ||
9619 | } else if (opcode == BPF_JA) { | |
9620 | if (BPF_SRC(insn->code) != BPF_K || | |
9621 | insn->imm != 0 || | |
9622 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
9623 | insn->dst_reg != BPF_REG_0 || |
9624 | class == BPF_JMP32) { | |
61bd5218 | 9625 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
9626 | return -EINVAL; |
9627 | } | |
9628 | ||
c08435ec | 9629 | env->insn_idx += insn->off + 1; |
17a52670 AS |
9630 | continue; |
9631 | ||
9632 | } else if (opcode == BPF_EXIT) { | |
9633 | if (BPF_SRC(insn->code) != BPF_K || | |
9634 | insn->imm != 0 || | |
9635 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
9636 | insn->dst_reg != BPF_REG_0 || |
9637 | class == BPF_JMP32) { | |
61bd5218 | 9638 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
9639 | return -EINVAL; |
9640 | } | |
9641 | ||
d83525ca AS |
9642 | if (env->cur_state->active_spin_lock) { |
9643 | verbose(env, "bpf_spin_unlock is missing\n"); | |
9644 | return -EINVAL; | |
9645 | } | |
9646 | ||
f4d7e40a AS |
9647 | if (state->curframe) { |
9648 | /* exit from nested function */ | |
c08435ec | 9649 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
9650 | if (err) |
9651 | return err; | |
9652 | do_print_state = true; | |
9653 | continue; | |
9654 | } | |
9655 | ||
fd978bf7 JS |
9656 | err = check_reference_leak(env); |
9657 | if (err) | |
9658 | return err; | |
9659 | ||
390ee7e2 AS |
9660 | err = check_return_code(env); |
9661 | if (err) | |
9662 | return err; | |
f1bca824 | 9663 | process_bpf_exit: |
2589726d | 9664 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 9665 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 9666 | &env->insn_idx, pop_log); |
638f5b90 AS |
9667 | if (err < 0) { |
9668 | if (err != -ENOENT) | |
9669 | return err; | |
17a52670 AS |
9670 | break; |
9671 | } else { | |
9672 | do_print_state = true; | |
9673 | continue; | |
9674 | } | |
9675 | } else { | |
c08435ec | 9676 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
9677 | if (err) |
9678 | return err; | |
9679 | } | |
9680 | } else if (class == BPF_LD) { | |
9681 | u8 mode = BPF_MODE(insn->code); | |
9682 | ||
9683 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
9684 | err = check_ld_abs(env, insn); |
9685 | if (err) | |
9686 | return err; | |
9687 | ||
17a52670 AS |
9688 | } else if (mode == BPF_IMM) { |
9689 | err = check_ld_imm(env, insn); | |
9690 | if (err) | |
9691 | return err; | |
9692 | ||
c08435ec | 9693 | env->insn_idx++; |
51c39bb1 | 9694 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
17a52670 | 9695 | } else { |
61bd5218 | 9696 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
9697 | return -EINVAL; |
9698 | } | |
9699 | } else { | |
61bd5218 | 9700 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
9701 | return -EINVAL; |
9702 | } | |
9703 | ||
c08435ec | 9704 | env->insn_idx++; |
17a52670 AS |
9705 | } |
9706 | ||
9707 | return 0; | |
9708 | } | |
9709 | ||
541c3bad AN |
9710 | static int find_btf_percpu_datasec(struct btf *btf) |
9711 | { | |
9712 | const struct btf_type *t; | |
9713 | const char *tname; | |
9714 | int i, n; | |
9715 | ||
9716 | /* | |
9717 | * Both vmlinux and module each have their own ".data..percpu" | |
9718 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF | |
9719 | * types to look at only module's own BTF types. | |
9720 | */ | |
9721 | n = btf_nr_types(btf); | |
9722 | if (btf_is_module(btf)) | |
9723 | i = btf_nr_types(btf_vmlinux); | |
9724 | else | |
9725 | i = 1; | |
9726 | ||
9727 | for(; i < n; i++) { | |
9728 | t = btf_type_by_id(btf, i); | |
9729 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) | |
9730 | continue; | |
9731 | ||
9732 | tname = btf_name_by_offset(btf, t->name_off); | |
9733 | if (!strcmp(tname, ".data..percpu")) | |
9734 | return i; | |
9735 | } | |
9736 | ||
9737 | return -ENOENT; | |
9738 | } | |
9739 | ||
4976b718 HL |
9740 | /* replace pseudo btf_id with kernel symbol address */ |
9741 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
9742 | struct bpf_insn *insn, | |
9743 | struct bpf_insn_aux_data *aux) | |
9744 | { | |
eaa6bcb7 HL |
9745 | const struct btf_var_secinfo *vsi; |
9746 | const struct btf_type *datasec; | |
541c3bad | 9747 | struct btf_mod_pair *btf_mod; |
4976b718 HL |
9748 | const struct btf_type *t; |
9749 | const char *sym_name; | |
eaa6bcb7 | 9750 | bool percpu = false; |
f16e6313 | 9751 | u32 type, id = insn->imm; |
541c3bad | 9752 | struct btf *btf; |
f16e6313 | 9753 | s32 datasec_id; |
4976b718 | 9754 | u64 addr; |
541c3bad | 9755 | int i, btf_fd, err; |
4976b718 | 9756 | |
541c3bad AN |
9757 | btf_fd = insn[1].imm; |
9758 | if (btf_fd) { | |
9759 | btf = btf_get_by_fd(btf_fd); | |
9760 | if (IS_ERR(btf)) { | |
9761 | verbose(env, "invalid module BTF object FD specified.\n"); | |
9762 | return -EINVAL; | |
9763 | } | |
9764 | } else { | |
9765 | if (!btf_vmlinux) { | |
9766 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
9767 | return -EINVAL; | |
9768 | } | |
9769 | btf = btf_vmlinux; | |
9770 | btf_get(btf); | |
4976b718 HL |
9771 | } |
9772 | ||
541c3bad | 9773 | t = btf_type_by_id(btf, id); |
4976b718 HL |
9774 | if (!t) { |
9775 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
541c3bad AN |
9776 | err = -ENOENT; |
9777 | goto err_put; | |
4976b718 HL |
9778 | } |
9779 | ||
9780 | if (!btf_type_is_var(t)) { | |
541c3bad AN |
9781 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
9782 | err = -EINVAL; | |
9783 | goto err_put; | |
4976b718 HL |
9784 | } |
9785 | ||
541c3bad | 9786 | sym_name = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
9787 | addr = kallsyms_lookup_name(sym_name); |
9788 | if (!addr) { | |
9789 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
9790 | sym_name); | |
541c3bad AN |
9791 | err = -ENOENT; |
9792 | goto err_put; | |
4976b718 HL |
9793 | } |
9794 | ||
541c3bad | 9795 | datasec_id = find_btf_percpu_datasec(btf); |
eaa6bcb7 | 9796 | if (datasec_id > 0) { |
541c3bad | 9797 | datasec = btf_type_by_id(btf, datasec_id); |
eaa6bcb7 HL |
9798 | for_each_vsi(i, datasec, vsi) { |
9799 | if (vsi->type == id) { | |
9800 | percpu = true; | |
9801 | break; | |
9802 | } | |
9803 | } | |
9804 | } | |
9805 | ||
4976b718 HL |
9806 | insn[0].imm = (u32)addr; |
9807 | insn[1].imm = addr >> 32; | |
9808 | ||
9809 | type = t->type; | |
541c3bad | 9810 | t = btf_type_skip_modifiers(btf, type, NULL); |
eaa6bcb7 HL |
9811 | if (percpu) { |
9812 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; | |
541c3bad | 9813 | aux->btf_var.btf = btf; |
eaa6bcb7 HL |
9814 | aux->btf_var.btf_id = type; |
9815 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
9816 | const struct btf_type *ret; |
9817 | const char *tname; | |
9818 | u32 tsize; | |
9819 | ||
9820 | /* resolve the type size of ksym. */ | |
541c3bad | 9821 | ret = btf_resolve_size(btf, t, &tsize); |
4976b718 | 9822 | if (IS_ERR(ret)) { |
541c3bad | 9823 | tname = btf_name_by_offset(btf, t->name_off); |
4976b718 HL |
9824 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
9825 | tname, PTR_ERR(ret)); | |
541c3bad AN |
9826 | err = -EINVAL; |
9827 | goto err_put; | |
4976b718 HL |
9828 | } |
9829 | aux->btf_var.reg_type = PTR_TO_MEM; | |
9830 | aux->btf_var.mem_size = tsize; | |
9831 | } else { | |
9832 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
541c3bad | 9833 | aux->btf_var.btf = btf; |
4976b718 HL |
9834 | aux->btf_var.btf_id = type; |
9835 | } | |
541c3bad AN |
9836 | |
9837 | /* check whether we recorded this BTF (and maybe module) already */ | |
9838 | for (i = 0; i < env->used_btf_cnt; i++) { | |
9839 | if (env->used_btfs[i].btf == btf) { | |
9840 | btf_put(btf); | |
9841 | return 0; | |
9842 | } | |
9843 | } | |
9844 | ||
9845 | if (env->used_btf_cnt >= MAX_USED_BTFS) { | |
9846 | err = -E2BIG; | |
9847 | goto err_put; | |
9848 | } | |
9849 | ||
9850 | btf_mod = &env->used_btfs[env->used_btf_cnt]; | |
9851 | btf_mod->btf = btf; | |
9852 | btf_mod->module = NULL; | |
9853 | ||
9854 | /* if we reference variables from kernel module, bump its refcount */ | |
9855 | if (btf_is_module(btf)) { | |
9856 | btf_mod->module = btf_try_get_module(btf); | |
9857 | if (!btf_mod->module) { | |
9858 | err = -ENXIO; | |
9859 | goto err_put; | |
9860 | } | |
9861 | } | |
9862 | ||
9863 | env->used_btf_cnt++; | |
9864 | ||
4976b718 | 9865 | return 0; |
541c3bad AN |
9866 | err_put: |
9867 | btf_put(btf); | |
9868 | return err; | |
4976b718 HL |
9869 | } |
9870 | ||
56f668df MKL |
9871 | static int check_map_prealloc(struct bpf_map *map) |
9872 | { | |
9873 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
9874 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
9875 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
9876 | !(map->map_flags & BPF_F_NO_PREALLOC); |
9877 | } | |
9878 | ||
d83525ca AS |
9879 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
9880 | { | |
9881 | switch (type) { | |
9882 | case BPF_PROG_TYPE_KPROBE: | |
9883 | case BPF_PROG_TYPE_TRACEPOINT: | |
9884 | case BPF_PROG_TYPE_PERF_EVENT: | |
9885 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
9886 | return true; | |
9887 | default: | |
9888 | return false; | |
9889 | } | |
9890 | } | |
9891 | ||
94dacdbd TG |
9892 | static bool is_preallocated_map(struct bpf_map *map) |
9893 | { | |
9894 | if (!check_map_prealloc(map)) | |
9895 | return false; | |
9896 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
9897 | return false; | |
9898 | return true; | |
9899 | } | |
9900 | ||
61bd5218 JK |
9901 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
9902 | struct bpf_map *map, | |
fdc15d38 AS |
9903 | struct bpf_prog *prog) |
9904 | ||
9905 | { | |
7e40781c | 9906 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
94dacdbd TG |
9907 | /* |
9908 | * Validate that trace type programs use preallocated hash maps. | |
9909 | * | |
9910 | * For programs attached to PERF events this is mandatory as the | |
9911 | * perf NMI can hit any arbitrary code sequence. | |
9912 | * | |
9913 | * All other trace types using preallocated hash maps are unsafe as | |
9914 | * well because tracepoint or kprobes can be inside locked regions | |
9915 | * of the memory allocator or at a place where a recursion into the | |
9916 | * memory allocator would see inconsistent state. | |
9917 | * | |
2ed905c5 TG |
9918 | * On RT enabled kernels run-time allocation of all trace type |
9919 | * programs is strictly prohibited due to lock type constraints. On | |
9920 | * !RT kernels it is allowed for backwards compatibility reasons for | |
9921 | * now, but warnings are emitted so developers are made aware of | |
9922 | * the unsafety and can fix their programs before this is enforced. | |
56f668df | 9923 | */ |
7e40781c UP |
9924 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
9925 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { | |
61bd5218 | 9926 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
9927 | return -EINVAL; |
9928 | } | |
2ed905c5 TG |
9929 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
9930 | verbose(env, "trace type programs can only use preallocated hash map\n"); | |
9931 | return -EINVAL; | |
9932 | } | |
94dacdbd TG |
9933 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
9934 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
fdc15d38 | 9935 | } |
a3884572 | 9936 | |
9e7a4d98 KS |
9937 | if (map_value_has_spin_lock(map)) { |
9938 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { | |
9939 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); | |
9940 | return -EINVAL; | |
9941 | } | |
9942 | ||
9943 | if (is_tracing_prog_type(prog_type)) { | |
9944 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
9945 | return -EINVAL; | |
9946 | } | |
9947 | ||
9948 | if (prog->aux->sleepable) { | |
9949 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); | |
9950 | return -EINVAL; | |
9951 | } | |
d83525ca AS |
9952 | } |
9953 | ||
a3884572 | 9954 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 9955 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
9956 | verbose(env, "offload device mismatch between prog and map\n"); |
9957 | return -EINVAL; | |
9958 | } | |
9959 | ||
85d33df3 MKL |
9960 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
9961 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
9962 | return -EINVAL; | |
9963 | } | |
9964 | ||
1e6c62a8 AS |
9965 | if (prog->aux->sleepable) |
9966 | switch (map->map_type) { | |
9967 | case BPF_MAP_TYPE_HASH: | |
9968 | case BPF_MAP_TYPE_LRU_HASH: | |
9969 | case BPF_MAP_TYPE_ARRAY: | |
9970 | if (!is_preallocated_map(map)) { | |
9971 | verbose(env, | |
9972 | "Sleepable programs can only use preallocated hash maps\n"); | |
9973 | return -EINVAL; | |
9974 | } | |
9975 | break; | |
9976 | default: | |
9977 | verbose(env, | |
9978 | "Sleepable programs can only use array and hash maps\n"); | |
9979 | return -EINVAL; | |
9980 | } | |
9981 | ||
fdc15d38 AS |
9982 | return 0; |
9983 | } | |
9984 | ||
b741f163 RG |
9985 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
9986 | { | |
9987 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
9988 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
9989 | } | |
9990 | ||
4976b718 HL |
9991 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
9992 | * | |
9993 | * 1. if it accesses map FD, replace it with actual map pointer. | |
9994 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
9995 | * | |
9996 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 9997 | */ |
4976b718 | 9998 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
9999 | { |
10000 | struct bpf_insn *insn = env->prog->insnsi; | |
10001 | int insn_cnt = env->prog->len; | |
fdc15d38 | 10002 | int i, j, err; |
0246e64d | 10003 | |
f1f7714e | 10004 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
10005 | if (err) |
10006 | return err; | |
10007 | ||
0246e64d | 10008 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 10009 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 10010 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 10011 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
10012 | return -EINVAL; |
10013 | } | |
10014 | ||
d691f9e8 AS |
10015 | if (BPF_CLASS(insn->code) == BPF_STX && |
10016 | ((BPF_MODE(insn->code) != BPF_MEM && | |
91c960b0 | 10017 | BPF_MODE(insn->code) != BPF_ATOMIC) || insn->imm != 0)) { |
61bd5218 | 10018 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
10019 | return -EINVAL; |
10020 | } | |
10021 | ||
0246e64d | 10022 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 10023 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
10024 | struct bpf_map *map; |
10025 | struct fd f; | |
d8eca5bb | 10026 | u64 addr; |
0246e64d AS |
10027 | |
10028 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
10029 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
10030 | insn[1].off != 0) { | |
61bd5218 | 10031 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
10032 | return -EINVAL; |
10033 | } | |
10034 | ||
d8eca5bb | 10035 | if (insn[0].src_reg == 0) |
0246e64d AS |
10036 | /* valid generic load 64-bit imm */ |
10037 | goto next_insn; | |
10038 | ||
4976b718 HL |
10039 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
10040 | aux = &env->insn_aux_data[i]; | |
10041 | err = check_pseudo_btf_id(env, insn, aux); | |
10042 | if (err) | |
10043 | return err; | |
10044 | goto next_insn; | |
10045 | } | |
10046 | ||
d8eca5bb DB |
10047 | /* In final convert_pseudo_ld_imm64() step, this is |
10048 | * converted into regular 64-bit imm load insn. | |
10049 | */ | |
10050 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
10051 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
10052 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
10053 | insn[1].imm != 0)) { | |
10054 | verbose(env, | |
10055 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
10056 | return -EINVAL; |
10057 | } | |
10058 | ||
20182390 | 10059 | f = fdget(insn[0].imm); |
c2101297 | 10060 | map = __bpf_map_get(f); |
0246e64d | 10061 | if (IS_ERR(map)) { |
61bd5218 | 10062 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 10063 | insn[0].imm); |
0246e64d AS |
10064 | return PTR_ERR(map); |
10065 | } | |
10066 | ||
61bd5218 | 10067 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
10068 | if (err) { |
10069 | fdput(f); | |
10070 | return err; | |
10071 | } | |
10072 | ||
d8eca5bb DB |
10073 | aux = &env->insn_aux_data[i]; |
10074 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
10075 | addr = (unsigned long)map; | |
10076 | } else { | |
10077 | u32 off = insn[1].imm; | |
10078 | ||
10079 | if (off >= BPF_MAX_VAR_OFF) { | |
10080 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
10081 | fdput(f); | |
10082 | return -EINVAL; | |
10083 | } | |
10084 | ||
10085 | if (!map->ops->map_direct_value_addr) { | |
10086 | verbose(env, "no direct value access support for this map type\n"); | |
10087 | fdput(f); | |
10088 | return -EINVAL; | |
10089 | } | |
10090 | ||
10091 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
10092 | if (err) { | |
10093 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
10094 | map->value_size, off); | |
10095 | fdput(f); | |
10096 | return err; | |
10097 | } | |
10098 | ||
10099 | aux->map_off = off; | |
10100 | addr += off; | |
10101 | } | |
10102 | ||
10103 | insn[0].imm = (u32)addr; | |
10104 | insn[1].imm = addr >> 32; | |
0246e64d AS |
10105 | |
10106 | /* check whether we recorded this map already */ | |
d8eca5bb | 10107 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 10108 | if (env->used_maps[j] == map) { |
d8eca5bb | 10109 | aux->map_index = j; |
0246e64d AS |
10110 | fdput(f); |
10111 | goto next_insn; | |
10112 | } | |
d8eca5bb | 10113 | } |
0246e64d AS |
10114 | |
10115 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
10116 | fdput(f); | |
10117 | return -E2BIG; | |
10118 | } | |
10119 | ||
0246e64d AS |
10120 | /* hold the map. If the program is rejected by verifier, |
10121 | * the map will be released by release_maps() or it | |
10122 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 10123 | * and all maps are released in free_used_maps() |
0246e64d | 10124 | */ |
1e0bd5a0 | 10125 | bpf_map_inc(map); |
d8eca5bb DB |
10126 | |
10127 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
10128 | env->used_maps[env->used_map_cnt++] = map; |
10129 | ||
b741f163 | 10130 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 10131 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 10132 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
10133 | fdput(f); |
10134 | return -EBUSY; | |
10135 | } | |
10136 | ||
0246e64d AS |
10137 | fdput(f); |
10138 | next_insn: | |
10139 | insn++; | |
10140 | i++; | |
5e581dad DB |
10141 | continue; |
10142 | } | |
10143 | ||
10144 | /* Basic sanity check before we invest more work here. */ | |
10145 | if (!bpf_opcode_in_insntable(insn->code)) { | |
10146 | verbose(env, "unknown opcode %02x\n", insn->code); | |
10147 | return -EINVAL; | |
0246e64d AS |
10148 | } |
10149 | } | |
10150 | ||
10151 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
10152 | * 'struct bpf_map *' into a register instead of user map_fd. | |
10153 | * These pointers will be used later by verifier to validate map access. | |
10154 | */ | |
10155 | return 0; | |
10156 | } | |
10157 | ||
10158 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 10159 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 10160 | { |
a2ea0746 DB |
10161 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
10162 | env->used_map_cnt); | |
0246e64d AS |
10163 | } |
10164 | ||
541c3bad AN |
10165 | /* drop refcnt of maps used by the rejected program */ |
10166 | static void release_btfs(struct bpf_verifier_env *env) | |
10167 | { | |
10168 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, | |
10169 | env->used_btf_cnt); | |
10170 | } | |
10171 | ||
0246e64d | 10172 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
58e2af8b | 10173 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
10174 | { |
10175 | struct bpf_insn *insn = env->prog->insnsi; | |
10176 | int insn_cnt = env->prog->len; | |
10177 | int i; | |
10178 | ||
10179 | for (i = 0; i < insn_cnt; i++, insn++) | |
10180 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
10181 | insn->src_reg = 0; | |
10182 | } | |
10183 | ||
8041902d AS |
10184 | /* single env->prog->insni[off] instruction was replaced with the range |
10185 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
10186 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
10187 | */ | |
b325fbca JW |
10188 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
10189 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d AS |
10190 | { |
10191 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
b325fbca JW |
10192 | struct bpf_insn *insn = new_prog->insnsi; |
10193 | u32 prog_len; | |
c131187d | 10194 | int i; |
8041902d | 10195 | |
b325fbca JW |
10196 | /* aux info at OFF always needs adjustment, no matter fast path |
10197 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
10198 | * original insn at old prog. | |
10199 | */ | |
10200 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
10201 | ||
8041902d AS |
10202 | if (cnt == 1) |
10203 | return 0; | |
b325fbca | 10204 | prog_len = new_prog->len; |
fad953ce KC |
10205 | new_data = vzalloc(array_size(prog_len, |
10206 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
10207 | if (!new_data) |
10208 | return -ENOMEM; | |
10209 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
10210 | memcpy(new_data + off + cnt - 1, old_data + off, | |
10211 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 10212 | for (i = off; i < off + cnt - 1; i++) { |
51c39bb1 | 10213 | new_data[i].seen = env->pass_cnt; |
b325fbca JW |
10214 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
10215 | } | |
8041902d AS |
10216 | env->insn_aux_data = new_data; |
10217 | vfree(old_data); | |
10218 | return 0; | |
10219 | } | |
10220 | ||
cc8b0b92 AS |
10221 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
10222 | { | |
10223 | int i; | |
10224 | ||
10225 | if (len == 1) | |
10226 | return; | |
4cb3d99c JW |
10227 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
10228 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 10229 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 10230 | continue; |
9c8105bd | 10231 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
10232 | } |
10233 | } | |
10234 | ||
a748c697 MF |
10235 | static void adjust_poke_descs(struct bpf_prog *prog, u32 len) |
10236 | { | |
10237 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
10238 | int i, sz = prog->aux->size_poke_tab; | |
10239 | struct bpf_jit_poke_descriptor *desc; | |
10240 | ||
10241 | for (i = 0; i < sz; i++) { | |
10242 | desc = &tab[i]; | |
10243 | desc->insn_idx += len - 1; | |
10244 | } | |
10245 | } | |
10246 | ||
8041902d AS |
10247 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
10248 | const struct bpf_insn *patch, u32 len) | |
10249 | { | |
10250 | struct bpf_prog *new_prog; | |
10251 | ||
10252 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
10253 | if (IS_ERR(new_prog)) { |
10254 | if (PTR_ERR(new_prog) == -ERANGE) | |
10255 | verbose(env, | |
10256 | "insn %d cannot be patched due to 16-bit range\n", | |
10257 | env->insn_aux_data[off].orig_idx); | |
8041902d | 10258 | return NULL; |
4f73379e | 10259 | } |
b325fbca | 10260 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
8041902d | 10261 | return NULL; |
cc8b0b92 | 10262 | adjust_subprog_starts(env, off, len); |
a748c697 | 10263 | adjust_poke_descs(new_prog, len); |
8041902d AS |
10264 | return new_prog; |
10265 | } | |
10266 | ||
52875a04 JK |
10267 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
10268 | u32 off, u32 cnt) | |
10269 | { | |
10270 | int i, j; | |
10271 | ||
10272 | /* find first prog starting at or after off (first to remove) */ | |
10273 | for (i = 0; i < env->subprog_cnt; i++) | |
10274 | if (env->subprog_info[i].start >= off) | |
10275 | break; | |
10276 | /* find first prog starting at or after off + cnt (first to stay) */ | |
10277 | for (j = i; j < env->subprog_cnt; j++) | |
10278 | if (env->subprog_info[j].start >= off + cnt) | |
10279 | break; | |
10280 | /* if j doesn't start exactly at off + cnt, we are just removing | |
10281 | * the front of previous prog | |
10282 | */ | |
10283 | if (env->subprog_info[j].start != off + cnt) | |
10284 | j--; | |
10285 | ||
10286 | if (j > i) { | |
10287 | struct bpf_prog_aux *aux = env->prog->aux; | |
10288 | int move; | |
10289 | ||
10290 | /* move fake 'exit' subprog as well */ | |
10291 | move = env->subprog_cnt + 1 - j; | |
10292 | ||
10293 | memmove(env->subprog_info + i, | |
10294 | env->subprog_info + j, | |
10295 | sizeof(*env->subprog_info) * move); | |
10296 | env->subprog_cnt -= j - i; | |
10297 | ||
10298 | /* remove func_info */ | |
10299 | if (aux->func_info) { | |
10300 | move = aux->func_info_cnt - j; | |
10301 | ||
10302 | memmove(aux->func_info + i, | |
10303 | aux->func_info + j, | |
10304 | sizeof(*aux->func_info) * move); | |
10305 | aux->func_info_cnt -= j - i; | |
10306 | /* func_info->insn_off is set after all code rewrites, | |
10307 | * in adjust_btf_func() - no need to adjust | |
10308 | */ | |
10309 | } | |
10310 | } else { | |
10311 | /* convert i from "first prog to remove" to "first to adjust" */ | |
10312 | if (env->subprog_info[i].start == off) | |
10313 | i++; | |
10314 | } | |
10315 | ||
10316 | /* update fake 'exit' subprog as well */ | |
10317 | for (; i <= env->subprog_cnt; i++) | |
10318 | env->subprog_info[i].start -= cnt; | |
10319 | ||
10320 | return 0; | |
10321 | } | |
10322 | ||
10323 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
10324 | u32 cnt) | |
10325 | { | |
10326 | struct bpf_prog *prog = env->prog; | |
10327 | u32 i, l_off, l_cnt, nr_linfo; | |
10328 | struct bpf_line_info *linfo; | |
10329 | ||
10330 | nr_linfo = prog->aux->nr_linfo; | |
10331 | if (!nr_linfo) | |
10332 | return 0; | |
10333 | ||
10334 | linfo = prog->aux->linfo; | |
10335 | ||
10336 | /* find first line info to remove, count lines to be removed */ | |
10337 | for (i = 0; i < nr_linfo; i++) | |
10338 | if (linfo[i].insn_off >= off) | |
10339 | break; | |
10340 | ||
10341 | l_off = i; | |
10342 | l_cnt = 0; | |
10343 | for (; i < nr_linfo; i++) | |
10344 | if (linfo[i].insn_off < off + cnt) | |
10345 | l_cnt++; | |
10346 | else | |
10347 | break; | |
10348 | ||
10349 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
10350 | * last removed linfo. prog is already modified, so prog->len == off | |
10351 | * means no live instructions after (tail of the program was removed). | |
10352 | */ | |
10353 | if (prog->len != off && l_cnt && | |
10354 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
10355 | l_cnt--; | |
10356 | linfo[--i].insn_off = off + cnt; | |
10357 | } | |
10358 | ||
10359 | /* remove the line info which refer to the removed instructions */ | |
10360 | if (l_cnt) { | |
10361 | memmove(linfo + l_off, linfo + i, | |
10362 | sizeof(*linfo) * (nr_linfo - i)); | |
10363 | ||
10364 | prog->aux->nr_linfo -= l_cnt; | |
10365 | nr_linfo = prog->aux->nr_linfo; | |
10366 | } | |
10367 | ||
10368 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
10369 | for (i = l_off; i < nr_linfo; i++) | |
10370 | linfo[i].insn_off -= cnt; | |
10371 | ||
10372 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
10373 | for (i = 0; i <= env->subprog_cnt; i++) | |
10374 | if (env->subprog_info[i].linfo_idx > l_off) { | |
10375 | /* program may have started in the removed region but | |
10376 | * may not be fully removed | |
10377 | */ | |
10378 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
10379 | env->subprog_info[i].linfo_idx -= l_cnt; | |
10380 | else | |
10381 | env->subprog_info[i].linfo_idx = l_off; | |
10382 | } | |
10383 | ||
10384 | return 0; | |
10385 | } | |
10386 | ||
10387 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
10388 | { | |
10389 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
10390 | unsigned int orig_prog_len = env->prog->len; | |
10391 | int err; | |
10392 | ||
08ca90af JK |
10393 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
10394 | bpf_prog_offload_remove_insns(env, off, cnt); | |
10395 | ||
52875a04 JK |
10396 | err = bpf_remove_insns(env->prog, off, cnt); |
10397 | if (err) | |
10398 | return err; | |
10399 | ||
10400 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
10401 | if (err) | |
10402 | return err; | |
10403 | ||
10404 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
10405 | if (err) | |
10406 | return err; | |
10407 | ||
10408 | memmove(aux_data + off, aux_data + off + cnt, | |
10409 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
10410 | ||
10411 | return 0; | |
10412 | } | |
10413 | ||
2a5418a1 DB |
10414 | /* The verifier does more data flow analysis than llvm and will not |
10415 | * explore branches that are dead at run time. Malicious programs can | |
10416 | * have dead code too. Therefore replace all dead at-run-time code | |
10417 | * with 'ja -1'. | |
10418 | * | |
10419 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
10420 | * program and through another bug we would manage to jump there, then | |
10421 | * we'd execute beyond program memory otherwise. Returning exception | |
10422 | * code also wouldn't work since we can have subprogs where the dead | |
10423 | * code could be located. | |
c131187d AS |
10424 | */ |
10425 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
10426 | { | |
10427 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 10428 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
10429 | struct bpf_insn *insn = env->prog->insnsi; |
10430 | const int insn_cnt = env->prog->len; | |
10431 | int i; | |
10432 | ||
10433 | for (i = 0; i < insn_cnt; i++) { | |
10434 | if (aux_data[i].seen) | |
10435 | continue; | |
2a5418a1 | 10436 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
10437 | } |
10438 | } | |
10439 | ||
e2ae4ca2 JK |
10440 | static bool insn_is_cond_jump(u8 code) |
10441 | { | |
10442 | u8 op; | |
10443 | ||
092ed096 JW |
10444 | if (BPF_CLASS(code) == BPF_JMP32) |
10445 | return true; | |
10446 | ||
e2ae4ca2 JK |
10447 | if (BPF_CLASS(code) != BPF_JMP) |
10448 | return false; | |
10449 | ||
10450 | op = BPF_OP(code); | |
10451 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
10452 | } | |
10453 | ||
10454 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
10455 | { | |
10456 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
10457 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
10458 | struct bpf_insn *insn = env->prog->insnsi; | |
10459 | const int insn_cnt = env->prog->len; | |
10460 | int i; | |
10461 | ||
10462 | for (i = 0; i < insn_cnt; i++, insn++) { | |
10463 | if (!insn_is_cond_jump(insn->code)) | |
10464 | continue; | |
10465 | ||
10466 | if (!aux_data[i + 1].seen) | |
10467 | ja.off = insn->off; | |
10468 | else if (!aux_data[i + 1 + insn->off].seen) | |
10469 | ja.off = 0; | |
10470 | else | |
10471 | continue; | |
10472 | ||
08ca90af JK |
10473 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
10474 | bpf_prog_offload_replace_insn(env, i, &ja); | |
10475 | ||
e2ae4ca2 JK |
10476 | memcpy(insn, &ja, sizeof(ja)); |
10477 | } | |
10478 | } | |
10479 | ||
52875a04 JK |
10480 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
10481 | { | |
10482 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
10483 | int insn_cnt = env->prog->len; | |
10484 | int i, err; | |
10485 | ||
10486 | for (i = 0; i < insn_cnt; i++) { | |
10487 | int j; | |
10488 | ||
10489 | j = 0; | |
10490 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
10491 | j++; | |
10492 | if (!j) | |
10493 | continue; | |
10494 | ||
10495 | err = verifier_remove_insns(env, i, j); | |
10496 | if (err) | |
10497 | return err; | |
10498 | insn_cnt = env->prog->len; | |
10499 | } | |
10500 | ||
10501 | return 0; | |
10502 | } | |
10503 | ||
a1b14abc JK |
10504 | static int opt_remove_nops(struct bpf_verifier_env *env) |
10505 | { | |
10506 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
10507 | struct bpf_insn *insn = env->prog->insnsi; | |
10508 | int insn_cnt = env->prog->len; | |
10509 | int i, err; | |
10510 | ||
10511 | for (i = 0; i < insn_cnt; i++) { | |
10512 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
10513 | continue; | |
10514 | ||
10515 | err = verifier_remove_insns(env, i, 1); | |
10516 | if (err) | |
10517 | return err; | |
10518 | insn_cnt--; | |
10519 | i--; | |
10520 | } | |
10521 | ||
10522 | return 0; | |
10523 | } | |
10524 | ||
d6c2308c JW |
10525 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
10526 | const union bpf_attr *attr) | |
a4b1d3c1 | 10527 | { |
d6c2308c | 10528 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 10529 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 10530 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 10531 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 10532 | struct bpf_prog *new_prog; |
d6c2308c | 10533 | bool rnd_hi32; |
a4b1d3c1 | 10534 | |
d6c2308c | 10535 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 10536 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
10537 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
10538 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
10539 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
10540 | for (i = 0; i < len; i++) { |
10541 | int adj_idx = i + delta; | |
10542 | struct bpf_insn insn; | |
10543 | ||
d6c2308c JW |
10544 | insn = insns[adj_idx]; |
10545 | if (!aux[adj_idx].zext_dst) { | |
10546 | u8 code, class; | |
10547 | u32 imm_rnd; | |
10548 | ||
10549 | if (!rnd_hi32) | |
10550 | continue; | |
10551 | ||
10552 | code = insn.code; | |
10553 | class = BPF_CLASS(code); | |
10554 | if (insn_no_def(&insn)) | |
10555 | continue; | |
10556 | ||
10557 | /* NOTE: arg "reg" (the fourth one) is only used for | |
10558 | * BPF_STX which has been ruled out in above | |
10559 | * check, it is safe to pass NULL here. | |
10560 | */ | |
10561 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { | |
10562 | if (class == BPF_LD && | |
10563 | BPF_MODE(code) == BPF_IMM) | |
10564 | i++; | |
10565 | continue; | |
10566 | } | |
10567 | ||
10568 | /* ctx load could be transformed into wider load. */ | |
10569 | if (class == BPF_LDX && | |
10570 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
10571 | continue; | |
10572 | ||
10573 | imm_rnd = get_random_int(); | |
10574 | rnd_hi32_patch[0] = insn; | |
10575 | rnd_hi32_patch[1].imm = imm_rnd; | |
10576 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; | |
10577 | patch = rnd_hi32_patch; | |
10578 | patch_len = 4; | |
10579 | goto apply_patch_buffer; | |
10580 | } | |
10581 | ||
10582 | if (!bpf_jit_needs_zext()) | |
a4b1d3c1 JW |
10583 | continue; |
10584 | ||
a4b1d3c1 JW |
10585 | zext_patch[0] = insn; |
10586 | zext_patch[1].dst_reg = insn.dst_reg; | |
10587 | zext_patch[1].src_reg = insn.dst_reg; | |
d6c2308c JW |
10588 | patch = zext_patch; |
10589 | patch_len = 2; | |
10590 | apply_patch_buffer: | |
10591 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
10592 | if (!new_prog) |
10593 | return -ENOMEM; | |
10594 | env->prog = new_prog; | |
10595 | insns = new_prog->insnsi; | |
10596 | aux = env->insn_aux_data; | |
d6c2308c | 10597 | delta += patch_len - 1; |
a4b1d3c1 JW |
10598 | } |
10599 | ||
10600 | return 0; | |
10601 | } | |
10602 | ||
c64b7983 JS |
10603 | /* convert load instructions that access fields of a context type into a |
10604 | * sequence of instructions that access fields of the underlying structure: | |
10605 | * struct __sk_buff -> struct sk_buff | |
10606 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 10607 | */ |
58e2af8b | 10608 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 10609 | { |
00176a34 | 10610 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 10611 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 10612 | const int insn_cnt = env->prog->len; |
36bbef52 | 10613 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 10614 | u32 target_size, size_default, off; |
9bac3d6d | 10615 | struct bpf_prog *new_prog; |
d691f9e8 | 10616 | enum bpf_access_type type; |
f96da094 | 10617 | bool is_narrower_load; |
9bac3d6d | 10618 | |
b09928b9 DB |
10619 | if (ops->gen_prologue || env->seen_direct_write) { |
10620 | if (!ops->gen_prologue) { | |
10621 | verbose(env, "bpf verifier is misconfigured\n"); | |
10622 | return -EINVAL; | |
10623 | } | |
36bbef52 DB |
10624 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
10625 | env->prog); | |
10626 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 10627 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
10628 | return -EINVAL; |
10629 | } else if (cnt) { | |
8041902d | 10630 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
10631 | if (!new_prog) |
10632 | return -ENOMEM; | |
8041902d | 10633 | |
36bbef52 | 10634 | env->prog = new_prog; |
3df126f3 | 10635 | delta += cnt - 1; |
36bbef52 DB |
10636 | } |
10637 | } | |
10638 | ||
c64b7983 | 10639 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
10640 | return 0; |
10641 | ||
3df126f3 | 10642 | insn = env->prog->insnsi + delta; |
36bbef52 | 10643 | |
9bac3d6d | 10644 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
10645 | bpf_convert_ctx_access_t convert_ctx_access; |
10646 | ||
62c7989b DB |
10647 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
10648 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
10649 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 10650 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 10651 | type = BPF_READ; |
62c7989b DB |
10652 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
10653 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
10654 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 10655 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
10656 | type = BPF_WRITE; |
10657 | else | |
9bac3d6d AS |
10658 | continue; |
10659 | ||
af86ca4e AS |
10660 | if (type == BPF_WRITE && |
10661 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
10662 | struct bpf_insn patch[] = { | |
10663 | /* Sanitize suspicious stack slot with zero. | |
10664 | * There are no memory dependencies for this store, | |
10665 | * since it's only using frame pointer and immediate | |
10666 | * constant of zero | |
10667 | */ | |
10668 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
10669 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
10670 | 0), | |
10671 | /* the original STX instruction will immediately | |
10672 | * overwrite the same stack slot with appropriate value | |
10673 | */ | |
10674 | *insn, | |
10675 | }; | |
10676 | ||
10677 | cnt = ARRAY_SIZE(patch); | |
10678 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
10679 | if (!new_prog) | |
10680 | return -ENOMEM; | |
10681 | ||
10682 | delta += cnt - 1; | |
10683 | env->prog = new_prog; | |
10684 | insn = new_prog->insnsi + i + delta; | |
10685 | continue; | |
10686 | } | |
10687 | ||
c64b7983 JS |
10688 | switch (env->insn_aux_data[i + delta].ptr_type) { |
10689 | case PTR_TO_CTX: | |
10690 | if (!ops->convert_ctx_access) | |
10691 | continue; | |
10692 | convert_ctx_access = ops->convert_ctx_access; | |
10693 | break; | |
10694 | case PTR_TO_SOCKET: | |
46f8bc92 | 10695 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
10696 | convert_ctx_access = bpf_sock_convert_ctx_access; |
10697 | break; | |
655a51e5 MKL |
10698 | case PTR_TO_TCP_SOCK: |
10699 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
10700 | break; | |
fada7fdc JL |
10701 | case PTR_TO_XDP_SOCK: |
10702 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
10703 | break; | |
2a02759e | 10704 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
10705 | if (type == BPF_READ) { |
10706 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
10707 | BPF_SIZE((insn)->code); | |
10708 | env->prog->aux->num_exentries++; | |
7e40781c | 10709 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
2a02759e AS |
10710 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
10711 | return -EINVAL; | |
10712 | } | |
2a02759e | 10713 | continue; |
c64b7983 | 10714 | default: |
9bac3d6d | 10715 | continue; |
c64b7983 | 10716 | } |
9bac3d6d | 10717 | |
31fd8581 | 10718 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 10719 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
10720 | |
10721 | /* If the read access is a narrower load of the field, | |
10722 | * convert to a 4/8-byte load, to minimum program type specific | |
10723 | * convert_ctx_access changes. If conversion is successful, | |
10724 | * we will apply proper mask to the result. | |
10725 | */ | |
f96da094 | 10726 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
10727 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
10728 | off = insn->off; | |
31fd8581 | 10729 | if (is_narrower_load) { |
f96da094 DB |
10730 | u8 size_code; |
10731 | ||
10732 | if (type == BPF_WRITE) { | |
61bd5218 | 10733 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
10734 | return -EINVAL; |
10735 | } | |
31fd8581 | 10736 | |
f96da094 | 10737 | size_code = BPF_H; |
31fd8581 YS |
10738 | if (ctx_field_size == 4) |
10739 | size_code = BPF_W; | |
10740 | else if (ctx_field_size == 8) | |
10741 | size_code = BPF_DW; | |
f96da094 | 10742 | |
bc23105c | 10743 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
10744 | insn->code = BPF_LDX | BPF_MEM | size_code; |
10745 | } | |
f96da094 DB |
10746 | |
10747 | target_size = 0; | |
c64b7983 JS |
10748 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
10749 | &target_size); | |
f96da094 DB |
10750 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
10751 | (ctx_field_size && !target_size)) { | |
61bd5218 | 10752 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
10753 | return -EINVAL; |
10754 | } | |
f96da094 DB |
10755 | |
10756 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
10757 | u8 shift = bpf_ctx_narrow_access_offset( |
10758 | off, size, size_default) * 8; | |
46f53a65 AI |
10759 | if (ctx_field_size <= 4) { |
10760 | if (shift) | |
10761 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
10762 | insn->dst_reg, | |
10763 | shift); | |
31fd8581 | 10764 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 10765 | (1 << size * 8) - 1); |
46f53a65 AI |
10766 | } else { |
10767 | if (shift) | |
10768 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
10769 | insn->dst_reg, | |
10770 | shift); | |
31fd8581 | 10771 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 10772 | (1ULL << size * 8) - 1); |
46f53a65 | 10773 | } |
31fd8581 | 10774 | } |
9bac3d6d | 10775 | |
8041902d | 10776 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
10777 | if (!new_prog) |
10778 | return -ENOMEM; | |
10779 | ||
3df126f3 | 10780 | delta += cnt - 1; |
9bac3d6d AS |
10781 | |
10782 | /* keep walking new program and skip insns we just inserted */ | |
10783 | env->prog = new_prog; | |
3df126f3 | 10784 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
10785 | } |
10786 | ||
10787 | return 0; | |
10788 | } | |
10789 | ||
1c2a088a AS |
10790 | static int jit_subprogs(struct bpf_verifier_env *env) |
10791 | { | |
10792 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
10793 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 10794 | struct bpf_map *map_ptr; |
7105e828 | 10795 | struct bpf_insn *insn; |
1c2a088a | 10796 | void *old_bpf_func; |
c4c0bdc0 | 10797 | int err, num_exentries; |
1c2a088a | 10798 | |
f910cefa | 10799 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
10800 | return 0; |
10801 | ||
7105e828 | 10802 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
10803 | if (insn->code != (BPF_JMP | BPF_CALL) || |
10804 | insn->src_reg != BPF_PSEUDO_CALL) | |
10805 | continue; | |
c7a89784 DB |
10806 | /* Upon error here we cannot fall back to interpreter but |
10807 | * need a hard reject of the program. Thus -EFAULT is | |
10808 | * propagated in any case. | |
10809 | */ | |
1c2a088a AS |
10810 | subprog = find_subprog(env, i + insn->imm + 1); |
10811 | if (subprog < 0) { | |
10812 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
10813 | i + insn->imm + 1); | |
10814 | return -EFAULT; | |
10815 | } | |
10816 | /* temporarily remember subprog id inside insn instead of | |
10817 | * aux_data, since next loop will split up all insns into funcs | |
10818 | */ | |
f910cefa | 10819 | insn->off = subprog; |
1c2a088a AS |
10820 | /* remember original imm in case JIT fails and fallback |
10821 | * to interpreter will be needed | |
10822 | */ | |
10823 | env->insn_aux_data[i].call_imm = insn->imm; | |
10824 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
10825 | insn->imm = 1; | |
10826 | } | |
10827 | ||
c454a46b MKL |
10828 | err = bpf_prog_alloc_jited_linfo(prog); |
10829 | if (err) | |
10830 | goto out_undo_insn; | |
10831 | ||
10832 | err = -ENOMEM; | |
6396bb22 | 10833 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 10834 | if (!func) |
c7a89784 | 10835 | goto out_undo_insn; |
1c2a088a | 10836 | |
f910cefa | 10837 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 10838 | subprog_start = subprog_end; |
4cb3d99c | 10839 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
10840 | |
10841 | len = subprog_end - subprog_start; | |
492ecee8 AS |
10842 | /* BPF_PROG_RUN doesn't call subprogs directly, |
10843 | * hence main prog stats include the runtime of subprogs. | |
10844 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
10845 | * func[i]->aux->stats will never be accessed and stays NULL | |
10846 | */ | |
10847 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
10848 | if (!func[i]) |
10849 | goto out_free; | |
10850 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
10851 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 10852 | func[i]->type = prog->type; |
1c2a088a | 10853 | func[i]->len = len; |
4f74d809 DB |
10854 | if (bpf_prog_calc_tag(func[i])) |
10855 | goto out_free; | |
1c2a088a | 10856 | func[i]->is_func = 1; |
ba64e7d8 YS |
10857 | func[i]->aux->func_idx = i; |
10858 | /* the btf and func_info will be freed only at prog->aux */ | |
10859 | func[i]->aux->btf = prog->aux->btf; | |
10860 | func[i]->aux->func_info = prog->aux->func_info; | |
10861 | ||
a748c697 MF |
10862 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
10863 | u32 insn_idx = prog->aux->poke_tab[j].insn_idx; | |
10864 | int ret; | |
10865 | ||
10866 | if (!(insn_idx >= subprog_start && | |
10867 | insn_idx <= subprog_end)) | |
10868 | continue; | |
10869 | ||
10870 | ret = bpf_jit_add_poke_descriptor(func[i], | |
10871 | &prog->aux->poke_tab[j]); | |
10872 | if (ret < 0) { | |
10873 | verbose(env, "adding tail call poke descriptor failed\n"); | |
10874 | goto out_free; | |
10875 | } | |
10876 | ||
10877 | func[i]->insnsi[insn_idx - subprog_start].imm = ret + 1; | |
10878 | ||
10879 | map_ptr = func[i]->aux->poke_tab[ret].tail_call.map; | |
10880 | ret = map_ptr->ops->map_poke_track(map_ptr, func[i]->aux); | |
10881 | if (ret < 0) { | |
10882 | verbose(env, "tracking tail call prog failed\n"); | |
10883 | goto out_free; | |
10884 | } | |
10885 | } | |
10886 | ||
1c2a088a AS |
10887 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
10888 | * Long term would need debug info to populate names | |
10889 | */ | |
10890 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 10891 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 10892 | func[i]->jit_requested = 1; |
c454a46b MKL |
10893 | func[i]->aux->linfo = prog->aux->linfo; |
10894 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
10895 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
10896 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
10897 | num_exentries = 0; |
10898 | insn = func[i]->insnsi; | |
10899 | for (j = 0; j < func[i]->len; j++, insn++) { | |
10900 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
10901 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
10902 | num_exentries++; | |
10903 | } | |
10904 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 10905 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
10906 | func[i] = bpf_int_jit_compile(func[i]); |
10907 | if (!func[i]->jited) { | |
10908 | err = -ENOTSUPP; | |
10909 | goto out_free; | |
10910 | } | |
10911 | cond_resched(); | |
10912 | } | |
a748c697 MF |
10913 | |
10914 | /* Untrack main program's aux structs so that during map_poke_run() | |
10915 | * we will not stumble upon the unfilled poke descriptors; each | |
10916 | * of the main program's poke descs got distributed across subprogs | |
10917 | * and got tracked onto map, so we are sure that none of them will | |
10918 | * be missed after the operation below | |
10919 | */ | |
10920 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
10921 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
10922 | ||
10923 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
10924 | } | |
10925 | ||
1c2a088a AS |
10926 | /* at this point all bpf functions were successfully JITed |
10927 | * now populate all bpf_calls with correct addresses and | |
10928 | * run last pass of JIT | |
10929 | */ | |
f910cefa | 10930 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10931 | insn = func[i]->insnsi; |
10932 | for (j = 0; j < func[i]->len; j++, insn++) { | |
10933 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
10934 | insn->src_reg != BPF_PSEUDO_CALL) | |
10935 | continue; | |
10936 | subprog = insn->off; | |
0d306c31 PB |
10937 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
10938 | __bpf_call_base; | |
1c2a088a | 10939 | } |
2162fed4 SD |
10940 | |
10941 | /* we use the aux data to keep a list of the start addresses | |
10942 | * of the JITed images for each function in the program | |
10943 | * | |
10944 | * for some architectures, such as powerpc64, the imm field | |
10945 | * might not be large enough to hold the offset of the start | |
10946 | * address of the callee's JITed image from __bpf_call_base | |
10947 | * | |
10948 | * in such cases, we can lookup the start address of a callee | |
10949 | * by using its subprog id, available from the off field of | |
10950 | * the call instruction, as an index for this list | |
10951 | */ | |
10952 | func[i]->aux->func = func; | |
10953 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 10954 | } |
f910cefa | 10955 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10956 | old_bpf_func = func[i]->bpf_func; |
10957 | tmp = bpf_int_jit_compile(func[i]); | |
10958 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
10959 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 10960 | err = -ENOTSUPP; |
1c2a088a AS |
10961 | goto out_free; |
10962 | } | |
10963 | cond_resched(); | |
10964 | } | |
10965 | ||
10966 | /* finally lock prog and jit images for all functions and | |
10967 | * populate kallsysm | |
10968 | */ | |
f910cefa | 10969 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10970 | bpf_prog_lock_ro(func[i]); |
10971 | bpf_prog_kallsyms_add(func[i]); | |
10972 | } | |
7105e828 DB |
10973 | |
10974 | /* Last step: make now unused interpreter insns from main | |
10975 | * prog consistent for later dump requests, so they can | |
10976 | * later look the same as if they were interpreted only. | |
10977 | */ | |
10978 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
10979 | if (insn->code != (BPF_JMP | BPF_CALL) || |
10980 | insn->src_reg != BPF_PSEUDO_CALL) | |
10981 | continue; | |
10982 | insn->off = env->insn_aux_data[i].call_imm; | |
10983 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 10984 | insn->imm = subprog; |
7105e828 DB |
10985 | } |
10986 | ||
1c2a088a AS |
10987 | prog->jited = 1; |
10988 | prog->bpf_func = func[0]->bpf_func; | |
10989 | prog->aux->func = func; | |
f910cefa | 10990 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 10991 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
10992 | return 0; |
10993 | out_free: | |
a748c697 MF |
10994 | for (i = 0; i < env->subprog_cnt; i++) { |
10995 | if (!func[i]) | |
10996 | continue; | |
10997 | ||
10998 | for (j = 0; j < func[i]->aux->size_poke_tab; j++) { | |
10999 | map_ptr = func[i]->aux->poke_tab[j].tail_call.map; | |
11000 | map_ptr->ops->map_poke_untrack(map_ptr, func[i]->aux); | |
11001 | } | |
11002 | bpf_jit_free(func[i]); | |
11003 | } | |
1c2a088a | 11004 | kfree(func); |
c7a89784 | 11005 | out_undo_insn: |
1c2a088a AS |
11006 | /* cleanup main prog to be interpreted */ |
11007 | prog->jit_requested = 0; | |
11008 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
11009 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
11010 | insn->src_reg != BPF_PSEUDO_CALL) | |
11011 | continue; | |
11012 | insn->off = 0; | |
11013 | insn->imm = env->insn_aux_data[i].call_imm; | |
11014 | } | |
c454a46b | 11015 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
11016 | return err; |
11017 | } | |
11018 | ||
1ea47e01 AS |
11019 | static int fixup_call_args(struct bpf_verifier_env *env) |
11020 | { | |
19d28fbd | 11021 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
11022 | struct bpf_prog *prog = env->prog; |
11023 | struct bpf_insn *insn = prog->insnsi; | |
11024 | int i, depth; | |
19d28fbd | 11025 | #endif |
e4052d06 | 11026 | int err = 0; |
1ea47e01 | 11027 | |
e4052d06 QM |
11028 | if (env->prog->jit_requested && |
11029 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
11030 | err = jit_subprogs(env); |
11031 | if (err == 0) | |
1c2a088a | 11032 | return 0; |
c7a89784 DB |
11033 | if (err == -EFAULT) |
11034 | return err; | |
19d28fbd DM |
11035 | } |
11036 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e411901c MF |
11037 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
11038 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
11039 | * have to be rejected, since interpreter doesn't support them yet. | |
11040 | */ | |
11041 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
11042 | return -EINVAL; | |
11043 | } | |
1ea47e01 AS |
11044 | for (i = 0; i < prog->len; i++, insn++) { |
11045 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
11046 | insn->src_reg != BPF_PSEUDO_CALL) | |
11047 | continue; | |
11048 | depth = get_callee_stack_depth(env, insn, i); | |
11049 | if (depth < 0) | |
11050 | return depth; | |
11051 | bpf_patch_call_args(insn, depth); | |
11052 | } | |
19d28fbd DM |
11053 | err = 0; |
11054 | #endif | |
11055 | return err; | |
1ea47e01 AS |
11056 | } |
11057 | ||
79741b3b | 11058 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 11059 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
11060 | * |
11061 | * this function is called after eBPF program passed verification | |
11062 | */ | |
79741b3b | 11063 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 11064 | { |
79741b3b | 11065 | struct bpf_prog *prog = env->prog; |
d2e4c1e6 | 11066 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
79741b3b | 11067 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 11068 | const struct bpf_func_proto *fn; |
79741b3b | 11069 | const int insn_cnt = prog->len; |
09772d92 | 11070 | const struct bpf_map_ops *ops; |
c93552c4 | 11071 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
11072 | struct bpf_insn insn_buf[16]; |
11073 | struct bpf_prog *new_prog; | |
11074 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 11075 | int i, ret, cnt, delta = 0; |
e245c5c6 | 11076 | |
79741b3b | 11077 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
11078 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
11079 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
11080 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 11081 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
11082 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
11083 | struct bpf_insn mask_and_div[] = { | |
11084 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
11085 | /* Rx div 0 -> 0 */ | |
11086 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
11087 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
11088 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
11089 | *insn, | |
11090 | }; | |
11091 | struct bpf_insn mask_and_mod[] = { | |
11092 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
11093 | /* Rx mod 0 -> Rx */ | |
11094 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
11095 | *insn, | |
11096 | }; | |
11097 | struct bpf_insn *patchlet; | |
11098 | ||
11099 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
11100 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
11101 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
11102 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
11103 | } else { | |
11104 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
11105 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
11106 | } | |
11107 | ||
11108 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
11109 | if (!new_prog) |
11110 | return -ENOMEM; | |
11111 | ||
11112 | delta += cnt - 1; | |
11113 | env->prog = prog = new_prog; | |
11114 | insn = new_prog->insnsi + i + delta; | |
11115 | continue; | |
11116 | } | |
11117 | ||
e0cea7ce DB |
11118 | if (BPF_CLASS(insn->code) == BPF_LD && |
11119 | (BPF_MODE(insn->code) == BPF_ABS || | |
11120 | BPF_MODE(insn->code) == BPF_IND)) { | |
11121 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
11122 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
11123 | verbose(env, "bpf verifier is misconfigured\n"); | |
11124 | return -EINVAL; | |
11125 | } | |
11126 | ||
11127 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
11128 | if (!new_prog) | |
11129 | return -ENOMEM; | |
11130 | ||
11131 | delta += cnt - 1; | |
11132 | env->prog = prog = new_prog; | |
11133 | insn = new_prog->insnsi + i + delta; | |
11134 | continue; | |
11135 | } | |
11136 | ||
979d63d5 DB |
11137 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
11138 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
11139 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
11140 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
11141 | struct bpf_insn insn_buf[16]; | |
11142 | struct bpf_insn *patch = &insn_buf[0]; | |
11143 | bool issrc, isneg; | |
11144 | u32 off_reg; | |
11145 | ||
11146 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
11147 | if (!aux->alu_state || |
11148 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
11149 | continue; |
11150 | ||
11151 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
11152 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
11153 | BPF_ALU_SANITIZE_SRC; | |
11154 | ||
11155 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
11156 | if (isneg) | |
11157 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
11158 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
11159 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
11160 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
11161 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
11162 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
11163 | if (issrc) { | |
11164 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
11165 | off_reg); | |
11166 | insn->src_reg = BPF_REG_AX; | |
11167 | } else { | |
11168 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
11169 | BPF_REG_AX); | |
11170 | } | |
11171 | if (isneg) | |
11172 | insn->code = insn->code == code_add ? | |
11173 | code_sub : code_add; | |
11174 | *patch++ = *insn; | |
11175 | if (issrc && isneg) | |
11176 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
11177 | cnt = patch - insn_buf; | |
11178 | ||
11179 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
11180 | if (!new_prog) | |
11181 | return -ENOMEM; | |
11182 | ||
11183 | delta += cnt - 1; | |
11184 | env->prog = prog = new_prog; | |
11185 | insn = new_prog->insnsi + i + delta; | |
11186 | continue; | |
11187 | } | |
11188 | ||
79741b3b AS |
11189 | if (insn->code != (BPF_JMP | BPF_CALL)) |
11190 | continue; | |
cc8b0b92 AS |
11191 | if (insn->src_reg == BPF_PSEUDO_CALL) |
11192 | continue; | |
e245c5c6 | 11193 | |
79741b3b AS |
11194 | if (insn->imm == BPF_FUNC_get_route_realm) |
11195 | prog->dst_needed = 1; | |
11196 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
11197 | bpf_user_rnd_init_once(); | |
9802d865 JB |
11198 | if (insn->imm == BPF_FUNC_override_return) |
11199 | prog->kprobe_override = 1; | |
79741b3b | 11200 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
11201 | /* If we tail call into other programs, we |
11202 | * cannot make any assumptions since they can | |
11203 | * be replaced dynamically during runtime in | |
11204 | * the program array. | |
11205 | */ | |
11206 | prog->cb_access = 1; | |
e411901c MF |
11207 | if (!allow_tail_call_in_subprogs(env)) |
11208 | prog->aux->stack_depth = MAX_BPF_STACK; | |
11209 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 11210 | |
79741b3b AS |
11211 | /* mark bpf_tail_call as different opcode to avoid |
11212 | * conditional branch in the interpeter for every normal | |
11213 | * call and to prevent accidental JITing by JIT compiler | |
11214 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 11215 | */ |
79741b3b | 11216 | insn->imm = 0; |
71189fa9 | 11217 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 11218 | |
c93552c4 | 11219 | aux = &env->insn_aux_data[i + delta]; |
2c78ee89 | 11220 | if (env->bpf_capable && !expect_blinding && |
cc52d914 | 11221 | prog->jit_requested && |
d2e4c1e6 DB |
11222 | !bpf_map_key_poisoned(aux) && |
11223 | !bpf_map_ptr_poisoned(aux) && | |
11224 | !bpf_map_ptr_unpriv(aux)) { | |
11225 | struct bpf_jit_poke_descriptor desc = { | |
11226 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
11227 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
11228 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 11229 | .insn_idx = i + delta, |
d2e4c1e6 DB |
11230 | }; |
11231 | ||
11232 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
11233 | if (ret < 0) { | |
11234 | verbose(env, "adding tail call poke descriptor failed\n"); | |
11235 | return ret; | |
11236 | } | |
11237 | ||
11238 | insn->imm = ret + 1; | |
11239 | continue; | |
11240 | } | |
11241 | ||
c93552c4 DB |
11242 | if (!bpf_map_ptr_unpriv(aux)) |
11243 | continue; | |
11244 | ||
b2157399 AS |
11245 | /* instead of changing every JIT dealing with tail_call |
11246 | * emit two extra insns: | |
11247 | * if (index >= max_entries) goto out; | |
11248 | * index &= array->index_mask; | |
11249 | * to avoid out-of-bounds cpu speculation | |
11250 | */ | |
c93552c4 | 11251 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 11252 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
11253 | return -EINVAL; |
11254 | } | |
c93552c4 | 11255 | |
d2e4c1e6 | 11256 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
11257 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
11258 | map_ptr->max_entries, 2); | |
11259 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
11260 | container_of(map_ptr, | |
11261 | struct bpf_array, | |
11262 | map)->index_mask); | |
11263 | insn_buf[2] = *insn; | |
11264 | cnt = 3; | |
11265 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
11266 | if (!new_prog) | |
11267 | return -ENOMEM; | |
11268 | ||
11269 | delta += cnt - 1; | |
11270 | env->prog = prog = new_prog; | |
11271 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
11272 | continue; |
11273 | } | |
e245c5c6 | 11274 | |
89c63074 | 11275 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
11276 | * and other inlining handlers are currently limited to 64 bit |
11277 | * only. | |
89c63074 | 11278 | */ |
60b58afc | 11279 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
11280 | (insn->imm == BPF_FUNC_map_lookup_elem || |
11281 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
11282 | insn->imm == BPF_FUNC_map_delete_elem || |
11283 | insn->imm == BPF_FUNC_map_push_elem || | |
11284 | insn->imm == BPF_FUNC_map_pop_elem || | |
11285 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
11286 | aux = &env->insn_aux_data[i + delta]; |
11287 | if (bpf_map_ptr_poisoned(aux)) | |
11288 | goto patch_call_imm; | |
11289 | ||
d2e4c1e6 | 11290 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
11291 | ops = map_ptr->ops; |
11292 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
11293 | ops->map_gen_lookup) { | |
11294 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
4a8f87e6 DB |
11295 | if (cnt == -EOPNOTSUPP) |
11296 | goto patch_map_ops_generic; | |
11297 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
09772d92 DB |
11298 | verbose(env, "bpf verifier is misconfigured\n"); |
11299 | return -EINVAL; | |
11300 | } | |
81ed18ab | 11301 | |
09772d92 DB |
11302 | new_prog = bpf_patch_insn_data(env, i + delta, |
11303 | insn_buf, cnt); | |
11304 | if (!new_prog) | |
11305 | return -ENOMEM; | |
81ed18ab | 11306 | |
09772d92 DB |
11307 | delta += cnt - 1; |
11308 | env->prog = prog = new_prog; | |
11309 | insn = new_prog->insnsi + i + delta; | |
11310 | continue; | |
11311 | } | |
81ed18ab | 11312 | |
09772d92 DB |
11313 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
11314 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
11315 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
11316 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
11317 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
11318 | (int (*)(struct bpf_map *map, void *key, void *value, | |
11319 | u64 flags))NULL)); | |
84430d42 DB |
11320 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
11321 | (int (*)(struct bpf_map *map, void *value, | |
11322 | u64 flags))NULL)); | |
11323 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
11324 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
11325 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
11326 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
4a8f87e6 | 11327 | patch_map_ops_generic: |
09772d92 DB |
11328 | switch (insn->imm) { |
11329 | case BPF_FUNC_map_lookup_elem: | |
11330 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
11331 | __bpf_call_base; | |
11332 | continue; | |
11333 | case BPF_FUNC_map_update_elem: | |
11334 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
11335 | __bpf_call_base; | |
11336 | continue; | |
11337 | case BPF_FUNC_map_delete_elem: | |
11338 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
11339 | __bpf_call_base; | |
11340 | continue; | |
84430d42 DB |
11341 | case BPF_FUNC_map_push_elem: |
11342 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
11343 | __bpf_call_base; | |
11344 | continue; | |
11345 | case BPF_FUNC_map_pop_elem: | |
11346 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
11347 | __bpf_call_base; | |
11348 | continue; | |
11349 | case BPF_FUNC_map_peek_elem: | |
11350 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
11351 | __bpf_call_base; | |
11352 | continue; | |
09772d92 | 11353 | } |
81ed18ab | 11354 | |
09772d92 | 11355 | goto patch_call_imm; |
81ed18ab AS |
11356 | } |
11357 | ||
5576b991 MKL |
11358 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
11359 | insn->imm == BPF_FUNC_jiffies64) { | |
11360 | struct bpf_insn ld_jiffies_addr[2] = { | |
11361 | BPF_LD_IMM64(BPF_REG_0, | |
11362 | (unsigned long)&jiffies), | |
11363 | }; | |
11364 | ||
11365 | insn_buf[0] = ld_jiffies_addr[0]; | |
11366 | insn_buf[1] = ld_jiffies_addr[1]; | |
11367 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
11368 | BPF_REG_0, 0); | |
11369 | cnt = 3; | |
11370 | ||
11371 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
11372 | cnt); | |
11373 | if (!new_prog) | |
11374 | return -ENOMEM; | |
11375 | ||
11376 | delta += cnt - 1; | |
11377 | env->prog = prog = new_prog; | |
11378 | insn = new_prog->insnsi + i + delta; | |
11379 | continue; | |
11380 | } | |
11381 | ||
81ed18ab | 11382 | patch_call_imm: |
5e43f899 | 11383 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
11384 | /* all functions that have prototype and verifier allowed |
11385 | * programs to call them, must be real in-kernel functions | |
11386 | */ | |
11387 | if (!fn->func) { | |
61bd5218 JK |
11388 | verbose(env, |
11389 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
11390 | func_id_name(insn->imm), insn->imm); |
11391 | return -EFAULT; | |
e245c5c6 | 11392 | } |
79741b3b | 11393 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 11394 | } |
e245c5c6 | 11395 | |
d2e4c1e6 DB |
11396 | /* Since poke tab is now finalized, publish aux to tracker. */ |
11397 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
11398 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
11399 | if (!map_ptr->ops->map_poke_track || | |
11400 | !map_ptr->ops->map_poke_untrack || | |
11401 | !map_ptr->ops->map_poke_run) { | |
11402 | verbose(env, "bpf verifier is misconfigured\n"); | |
11403 | return -EINVAL; | |
11404 | } | |
11405 | ||
11406 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
11407 | if (ret < 0) { | |
11408 | verbose(env, "tracking tail call prog failed\n"); | |
11409 | return ret; | |
11410 | } | |
11411 | } | |
11412 | ||
79741b3b AS |
11413 | return 0; |
11414 | } | |
e245c5c6 | 11415 | |
58e2af8b | 11416 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 11417 | { |
58e2af8b | 11418 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
11419 | int i; |
11420 | ||
9f4686c4 AS |
11421 | sl = env->free_list; |
11422 | while (sl) { | |
11423 | sln = sl->next; | |
11424 | free_verifier_state(&sl->state, false); | |
11425 | kfree(sl); | |
11426 | sl = sln; | |
11427 | } | |
51c39bb1 | 11428 | env->free_list = NULL; |
9f4686c4 | 11429 | |
f1bca824 AS |
11430 | if (!env->explored_states) |
11431 | return; | |
11432 | ||
dc2a4ebc | 11433 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
11434 | sl = env->explored_states[i]; |
11435 | ||
a8f500af AS |
11436 | while (sl) { |
11437 | sln = sl->next; | |
11438 | free_verifier_state(&sl->state, false); | |
11439 | kfree(sl); | |
11440 | sl = sln; | |
11441 | } | |
51c39bb1 | 11442 | env->explored_states[i] = NULL; |
f1bca824 | 11443 | } |
51c39bb1 | 11444 | } |
f1bca824 | 11445 | |
51c39bb1 AS |
11446 | /* The verifier is using insn_aux_data[] to store temporary data during |
11447 | * verification and to store information for passes that run after the | |
11448 | * verification like dead code sanitization. do_check_common() for subprogram N | |
11449 | * may analyze many other subprograms. sanitize_insn_aux_data() clears all | |
11450 | * temporary data after do_check_common() finds that subprogram N cannot be | |
11451 | * verified independently. pass_cnt counts the number of times | |
11452 | * do_check_common() was run and insn->aux->seen tells the pass number | |
11453 | * insn_aux_data was touched. These variables are compared to clear temporary | |
11454 | * data from failed pass. For testing and experiments do_check_common() can be | |
11455 | * run multiple times even when prior attempt to verify is unsuccessful. | |
11456 | */ | |
11457 | static void sanitize_insn_aux_data(struct bpf_verifier_env *env) | |
11458 | { | |
11459 | struct bpf_insn *insn = env->prog->insnsi; | |
11460 | struct bpf_insn_aux_data *aux; | |
11461 | int i, class; | |
11462 | ||
11463 | for (i = 0; i < env->prog->len; i++) { | |
11464 | class = BPF_CLASS(insn[i].code); | |
11465 | if (class != BPF_LDX && class != BPF_STX) | |
11466 | continue; | |
11467 | aux = &env->insn_aux_data[i]; | |
11468 | if (aux->seen != env->pass_cnt) | |
11469 | continue; | |
11470 | memset(aux, 0, offsetof(typeof(*aux), orig_idx)); | |
11471 | } | |
f1bca824 AS |
11472 | } |
11473 | ||
51c39bb1 AS |
11474 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
11475 | { | |
6f8a57cc | 11476 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
11477 | struct bpf_verifier_state *state; |
11478 | struct bpf_reg_state *regs; | |
11479 | int ret, i; | |
11480 | ||
11481 | env->prev_linfo = NULL; | |
11482 | env->pass_cnt++; | |
11483 | ||
11484 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
11485 | if (!state) | |
11486 | return -ENOMEM; | |
11487 | state->curframe = 0; | |
11488 | state->speculative = false; | |
11489 | state->branches = 1; | |
11490 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
11491 | if (!state->frame[0]) { | |
11492 | kfree(state); | |
11493 | return -ENOMEM; | |
11494 | } | |
11495 | env->cur_state = state; | |
11496 | init_func_state(env, state->frame[0], | |
11497 | BPF_MAIN_FUNC /* callsite */, | |
11498 | 0 /* frameno */, | |
11499 | subprog); | |
11500 | ||
11501 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 11502 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
11503 | ret = btf_prepare_func_args(env, subprog, regs); |
11504 | if (ret) | |
11505 | goto out; | |
11506 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
11507 | if (regs[i].type == PTR_TO_CTX) | |
11508 | mark_reg_known_zero(env, regs, i); | |
11509 | else if (regs[i].type == SCALAR_VALUE) | |
11510 | mark_reg_unknown(env, regs, i); | |
11511 | } | |
11512 | } else { | |
11513 | /* 1st arg to a function */ | |
11514 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
11515 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
11516 | ret = btf_check_func_arg_match(env, subprog, regs); | |
11517 | if (ret == -EFAULT) | |
11518 | /* unlikely verifier bug. abort. | |
11519 | * ret == 0 and ret < 0 are sadly acceptable for | |
11520 | * main() function due to backward compatibility. | |
11521 | * Like socket filter program may be written as: | |
11522 | * int bpf_prog(struct pt_regs *ctx) | |
11523 | * and never dereference that ctx in the program. | |
11524 | * 'struct pt_regs' is a type mismatch for socket | |
11525 | * filter that should be using 'struct __sk_buff'. | |
11526 | */ | |
11527 | goto out; | |
11528 | } | |
11529 | ||
11530 | ret = do_check(env); | |
11531 | out: | |
f59bbfc2 AS |
11532 | /* check for NULL is necessary, since cur_state can be freed inside |
11533 | * do_check() under memory pressure. | |
11534 | */ | |
11535 | if (env->cur_state) { | |
11536 | free_verifier_state(env->cur_state, true); | |
11537 | env->cur_state = NULL; | |
11538 | } | |
6f8a57cc AN |
11539 | while (!pop_stack(env, NULL, NULL, false)); |
11540 | if (!ret && pop_log) | |
11541 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 AS |
11542 | free_states(env); |
11543 | if (ret) | |
11544 | /* clean aux data in case subprog was rejected */ | |
11545 | sanitize_insn_aux_data(env); | |
11546 | return ret; | |
11547 | } | |
11548 | ||
11549 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
11550 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
11551 | * Consider: | |
11552 | * int bar(int); | |
11553 | * int foo(int f) | |
11554 | * { | |
11555 | * return bar(f); | |
11556 | * } | |
11557 | * int bar(int b) | |
11558 | * { | |
11559 | * ... | |
11560 | * } | |
11561 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
11562 | * will be assumed that bar() already verified successfully and call to bar() | |
11563 | * from foo() will be checked for type match only. Later bar() will be verified | |
11564 | * independently to check that it's safe for R1=any_scalar_value. | |
11565 | */ | |
11566 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
11567 | { | |
11568 | struct bpf_prog_aux *aux = env->prog->aux; | |
11569 | int i, ret; | |
11570 | ||
11571 | if (!aux->func_info) | |
11572 | return 0; | |
11573 | ||
11574 | for (i = 1; i < env->subprog_cnt; i++) { | |
11575 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
11576 | continue; | |
11577 | env->insn_idx = env->subprog_info[i].start; | |
11578 | WARN_ON_ONCE(env->insn_idx == 0); | |
11579 | ret = do_check_common(env, i); | |
11580 | if (ret) { | |
11581 | return ret; | |
11582 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
11583 | verbose(env, | |
11584 | "Func#%d is safe for any args that match its prototype\n", | |
11585 | i); | |
11586 | } | |
11587 | } | |
11588 | return 0; | |
11589 | } | |
11590 | ||
11591 | static int do_check_main(struct bpf_verifier_env *env) | |
11592 | { | |
11593 | int ret; | |
11594 | ||
11595 | env->insn_idx = 0; | |
11596 | ret = do_check_common(env, 0); | |
11597 | if (!ret) | |
11598 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
11599 | return ret; | |
11600 | } | |
11601 | ||
11602 | ||
06ee7115 AS |
11603 | static void print_verification_stats(struct bpf_verifier_env *env) |
11604 | { | |
11605 | int i; | |
11606 | ||
11607 | if (env->log.level & BPF_LOG_STATS) { | |
11608 | verbose(env, "verification time %lld usec\n", | |
11609 | div_u64(env->verification_time, 1000)); | |
11610 | verbose(env, "stack depth "); | |
11611 | for (i = 0; i < env->subprog_cnt; i++) { | |
11612 | u32 depth = env->subprog_info[i].stack_depth; | |
11613 | ||
11614 | verbose(env, "%d", depth); | |
11615 | if (i + 1 < env->subprog_cnt) | |
11616 | verbose(env, "+"); | |
11617 | } | |
11618 | verbose(env, "\n"); | |
11619 | } | |
11620 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
11621 | "total_states %d peak_states %d mark_read %d\n", | |
11622 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
11623 | env->max_states_per_insn, env->total_states, | |
11624 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
11625 | } |
11626 | ||
27ae7997 MKL |
11627 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
11628 | { | |
11629 | const struct btf_type *t, *func_proto; | |
11630 | const struct bpf_struct_ops *st_ops; | |
11631 | const struct btf_member *member; | |
11632 | struct bpf_prog *prog = env->prog; | |
11633 | u32 btf_id, member_idx; | |
11634 | const char *mname; | |
11635 | ||
11636 | btf_id = prog->aux->attach_btf_id; | |
11637 | st_ops = bpf_struct_ops_find(btf_id); | |
11638 | if (!st_ops) { | |
11639 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
11640 | btf_id); | |
11641 | return -ENOTSUPP; | |
11642 | } | |
11643 | ||
11644 | t = st_ops->type; | |
11645 | member_idx = prog->expected_attach_type; | |
11646 | if (member_idx >= btf_type_vlen(t)) { | |
11647 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
11648 | member_idx, st_ops->name); | |
11649 | return -EINVAL; | |
11650 | } | |
11651 | ||
11652 | member = &btf_type_member(t)[member_idx]; | |
11653 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
11654 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
11655 | NULL); | |
11656 | if (!func_proto) { | |
11657 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
11658 | mname, member_idx, st_ops->name); | |
11659 | return -EINVAL; | |
11660 | } | |
11661 | ||
11662 | if (st_ops->check_member) { | |
11663 | int err = st_ops->check_member(t, member); | |
11664 | ||
11665 | if (err) { | |
11666 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
11667 | mname, st_ops->name); | |
11668 | return err; | |
11669 | } | |
11670 | } | |
11671 | ||
11672 | prog->aux->attach_func_proto = func_proto; | |
11673 | prog->aux->attach_func_name = mname; | |
11674 | env->ops = st_ops->verifier_ops; | |
11675 | ||
11676 | return 0; | |
11677 | } | |
6ba43b76 KS |
11678 | #define SECURITY_PREFIX "security_" |
11679 | ||
f7b12b6f | 11680 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 11681 | { |
69191754 | 11682 | if (within_error_injection_list(addr) || |
f7b12b6f | 11683 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 11684 | return 0; |
6ba43b76 | 11685 | |
6ba43b76 KS |
11686 | return -EINVAL; |
11687 | } | |
27ae7997 | 11688 | |
1e6c62a8 AS |
11689 | /* list of non-sleepable functions that are otherwise on |
11690 | * ALLOW_ERROR_INJECTION list | |
11691 | */ | |
11692 | BTF_SET_START(btf_non_sleepable_error_inject) | |
11693 | /* Three functions below can be called from sleepable and non-sleepable context. | |
11694 | * Assume non-sleepable from bpf safety point of view. | |
11695 | */ | |
11696 | BTF_ID(func, __add_to_page_cache_locked) | |
11697 | BTF_ID(func, should_fail_alloc_page) | |
11698 | BTF_ID(func, should_failslab) | |
11699 | BTF_SET_END(btf_non_sleepable_error_inject) | |
11700 | ||
11701 | static int check_non_sleepable_error_inject(u32 btf_id) | |
11702 | { | |
11703 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
11704 | } | |
11705 | ||
f7b12b6f THJ |
11706 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
11707 | const struct bpf_prog *prog, | |
11708 | const struct bpf_prog *tgt_prog, | |
11709 | u32 btf_id, | |
11710 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 11711 | { |
be8704ff | 11712 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 11713 | const char prefix[] = "btf_trace_"; |
5b92a28a | 11714 | int ret = 0, subprog = -1, i; |
38207291 | 11715 | const struct btf_type *t; |
5b92a28a | 11716 | bool conservative = true; |
38207291 | 11717 | const char *tname; |
5b92a28a | 11718 | struct btf *btf; |
f7b12b6f | 11719 | long addr = 0; |
38207291 | 11720 | |
f1b9509c | 11721 | if (!btf_id) { |
efc68158 | 11722 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
11723 | return -EINVAL; |
11724 | } | |
22dc4a0f | 11725 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
5b92a28a | 11726 | if (!btf) { |
efc68158 | 11727 | bpf_log(log, |
5b92a28a AS |
11728 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
11729 | return -EINVAL; | |
11730 | } | |
11731 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 11732 | if (!t) { |
efc68158 | 11733 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
11734 | return -EINVAL; |
11735 | } | |
5b92a28a | 11736 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 11737 | if (!tname) { |
efc68158 | 11738 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
11739 | return -EINVAL; |
11740 | } | |
5b92a28a AS |
11741 | if (tgt_prog) { |
11742 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
11743 | ||
11744 | for (i = 0; i < aux->func_info_cnt; i++) | |
11745 | if (aux->func_info[i].type_id == btf_id) { | |
11746 | subprog = i; | |
11747 | break; | |
11748 | } | |
11749 | if (subprog == -1) { | |
efc68158 | 11750 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
11751 | return -EINVAL; |
11752 | } | |
11753 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
11754 | if (prog_extension) { |
11755 | if (conservative) { | |
efc68158 | 11756 | bpf_log(log, |
be8704ff AS |
11757 | "Cannot replace static functions\n"); |
11758 | return -EINVAL; | |
11759 | } | |
11760 | if (!prog->jit_requested) { | |
efc68158 | 11761 | bpf_log(log, |
be8704ff AS |
11762 | "Extension programs should be JITed\n"); |
11763 | return -EINVAL; | |
11764 | } | |
be8704ff AS |
11765 | } |
11766 | if (!tgt_prog->jited) { | |
efc68158 | 11767 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
11768 | return -EINVAL; |
11769 | } | |
11770 | if (tgt_prog->type == prog->type) { | |
11771 | /* Cannot fentry/fexit another fentry/fexit program. | |
11772 | * Cannot attach program extension to another extension. | |
11773 | * It's ok to attach fentry/fexit to extension program. | |
11774 | */ | |
efc68158 | 11775 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
11776 | return -EINVAL; |
11777 | } | |
11778 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
11779 | prog_extension && | |
11780 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
11781 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
11782 | /* Program extensions can extend all program types | |
11783 | * except fentry/fexit. The reason is the following. | |
11784 | * The fentry/fexit programs are used for performance | |
11785 | * analysis, stats and can be attached to any program | |
11786 | * type except themselves. When extension program is | |
11787 | * replacing XDP function it is necessary to allow | |
11788 | * performance analysis of all functions. Both original | |
11789 | * XDP program and its program extension. Hence | |
11790 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
11791 | * allowed. If extending of fentry/fexit was allowed it | |
11792 | * would be possible to create long call chain | |
11793 | * fentry->extension->fentry->extension beyond | |
11794 | * reasonable stack size. Hence extending fentry is not | |
11795 | * allowed. | |
11796 | */ | |
efc68158 | 11797 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
11798 | return -EINVAL; |
11799 | } | |
5b92a28a | 11800 | } else { |
be8704ff | 11801 | if (prog_extension) { |
efc68158 | 11802 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
11803 | return -EINVAL; |
11804 | } | |
5b92a28a | 11805 | } |
f1b9509c AS |
11806 | |
11807 | switch (prog->expected_attach_type) { | |
11808 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 11809 | if (tgt_prog) { |
efc68158 | 11810 | bpf_log(log, |
5b92a28a AS |
11811 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
11812 | return -EINVAL; | |
11813 | } | |
38207291 | 11814 | if (!btf_type_is_typedef(t)) { |
efc68158 | 11815 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
11816 | btf_id); |
11817 | return -EINVAL; | |
11818 | } | |
f1b9509c | 11819 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 11820 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
11821 | btf_id, tname); |
11822 | return -EINVAL; | |
11823 | } | |
11824 | tname += sizeof(prefix) - 1; | |
5b92a28a | 11825 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
11826 | if (!btf_type_is_ptr(t)) |
11827 | /* should never happen in valid vmlinux build */ | |
11828 | return -EINVAL; | |
5b92a28a | 11829 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
11830 | if (!btf_type_is_func_proto(t)) |
11831 | /* should never happen in valid vmlinux build */ | |
11832 | return -EINVAL; | |
11833 | ||
f7b12b6f | 11834 | break; |
15d83c4d YS |
11835 | case BPF_TRACE_ITER: |
11836 | if (!btf_type_is_func(t)) { | |
efc68158 | 11837 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
11838 | btf_id); |
11839 | return -EINVAL; | |
11840 | } | |
11841 | t = btf_type_by_id(btf, t->type); | |
11842 | if (!btf_type_is_func_proto(t)) | |
11843 | return -EINVAL; | |
f7b12b6f THJ |
11844 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
11845 | if (ret) | |
11846 | return ret; | |
11847 | break; | |
be8704ff AS |
11848 | default: |
11849 | if (!prog_extension) | |
11850 | return -EINVAL; | |
df561f66 | 11851 | fallthrough; |
ae240823 | 11852 | case BPF_MODIFY_RETURN: |
9e4e01df | 11853 | case BPF_LSM_MAC: |
fec56f58 AS |
11854 | case BPF_TRACE_FENTRY: |
11855 | case BPF_TRACE_FEXIT: | |
11856 | if (!btf_type_is_func(t)) { | |
efc68158 | 11857 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
11858 | btf_id); |
11859 | return -EINVAL; | |
11860 | } | |
be8704ff | 11861 | if (prog_extension && |
efc68158 | 11862 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 11863 | return -EINVAL; |
5b92a28a | 11864 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
11865 | if (!btf_type_is_func_proto(t)) |
11866 | return -EINVAL; | |
f7b12b6f | 11867 | |
4a1e7c0c THJ |
11868 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
11869 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
11870 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
11871 | return -EINVAL; | |
11872 | ||
f7b12b6f | 11873 | if (tgt_prog && conservative) |
5b92a28a | 11874 | t = NULL; |
f7b12b6f THJ |
11875 | |
11876 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 11877 | if (ret < 0) |
f7b12b6f THJ |
11878 | return ret; |
11879 | ||
5b92a28a | 11880 | if (tgt_prog) { |
e9eeec58 YS |
11881 | if (subprog == 0) |
11882 | addr = (long) tgt_prog->bpf_func; | |
11883 | else | |
11884 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
11885 | } else { |
11886 | addr = kallsyms_lookup_name(tname); | |
11887 | if (!addr) { | |
efc68158 | 11888 | bpf_log(log, |
5b92a28a AS |
11889 | "The address of function %s cannot be found\n", |
11890 | tname); | |
f7b12b6f | 11891 | return -ENOENT; |
5b92a28a | 11892 | } |
fec56f58 | 11893 | } |
18644cec | 11894 | |
1e6c62a8 AS |
11895 | if (prog->aux->sleepable) { |
11896 | ret = -EINVAL; | |
11897 | switch (prog->type) { | |
11898 | case BPF_PROG_TYPE_TRACING: | |
11899 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
11900 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
11901 | */ | |
11902 | if (!check_non_sleepable_error_inject(btf_id) && | |
11903 | within_error_injection_list(addr)) | |
11904 | ret = 0; | |
11905 | break; | |
11906 | case BPF_PROG_TYPE_LSM: | |
11907 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
11908 | * Only some of them are sleepable. | |
11909 | */ | |
423f1610 | 11910 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
1e6c62a8 AS |
11911 | ret = 0; |
11912 | break; | |
11913 | default: | |
11914 | break; | |
11915 | } | |
f7b12b6f THJ |
11916 | if (ret) { |
11917 | bpf_log(log, "%s is not sleepable\n", tname); | |
11918 | return ret; | |
11919 | } | |
1e6c62a8 | 11920 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 11921 | if (tgt_prog) { |
efc68158 | 11922 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
11923 | return -EINVAL; |
11924 | } | |
11925 | ret = check_attach_modify_return(addr, tname); | |
11926 | if (ret) { | |
11927 | bpf_log(log, "%s() is not modifiable\n", tname); | |
11928 | return ret; | |
1af9270e | 11929 | } |
18644cec | 11930 | } |
f7b12b6f THJ |
11931 | |
11932 | break; | |
11933 | } | |
11934 | tgt_info->tgt_addr = addr; | |
11935 | tgt_info->tgt_name = tname; | |
11936 | tgt_info->tgt_type = t; | |
11937 | return 0; | |
11938 | } | |
11939 | ||
11940 | static int check_attach_btf_id(struct bpf_verifier_env *env) | |
11941 | { | |
11942 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 11943 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
11944 | struct bpf_attach_target_info tgt_info = {}; |
11945 | u32 btf_id = prog->aux->attach_btf_id; | |
11946 | struct bpf_trampoline *tr; | |
11947 | int ret; | |
11948 | u64 key; | |
11949 | ||
11950 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && | |
11951 | prog->type != BPF_PROG_TYPE_LSM) { | |
11952 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); | |
11953 | return -EINVAL; | |
11954 | } | |
11955 | ||
11956 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
11957 | return check_struct_ops_btf_id(env); | |
11958 | ||
11959 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
11960 | prog->type != BPF_PROG_TYPE_LSM && | |
11961 | prog->type != BPF_PROG_TYPE_EXT) | |
11962 | return 0; | |
11963 | ||
11964 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
11965 | if (ret) | |
fec56f58 | 11966 | return ret; |
f7b12b6f THJ |
11967 | |
11968 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
11969 | /* to make freplace equivalent to their targets, they need to |
11970 | * inherit env->ops and expected_attach_type for the rest of the | |
11971 | * verification | |
11972 | */ | |
f7b12b6f THJ |
11973 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
11974 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
11975 | } | |
11976 | ||
11977 | /* store info about the attachment target that will be used later */ | |
11978 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
11979 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
11980 | ||
4a1e7c0c THJ |
11981 | if (tgt_prog) { |
11982 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
11983 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
11984 | } | |
11985 | ||
f7b12b6f THJ |
11986 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
11987 | prog->aux->attach_btf_trace = true; | |
11988 | return 0; | |
11989 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
11990 | if (!bpf_iter_prog_supported(prog)) | |
11991 | return -EINVAL; | |
11992 | return 0; | |
11993 | } | |
11994 | ||
11995 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
11996 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
11997 | if (ret < 0) | |
11998 | return ret; | |
38207291 | 11999 | } |
f7b12b6f | 12000 | |
22dc4a0f | 12001 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
f7b12b6f THJ |
12002 | tr = bpf_trampoline_get(key, &tgt_info); |
12003 | if (!tr) | |
12004 | return -ENOMEM; | |
12005 | ||
3aac1ead | 12006 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 12007 | return 0; |
38207291 MKL |
12008 | } |
12009 | ||
76654e67 AM |
12010 | struct btf *bpf_get_btf_vmlinux(void) |
12011 | { | |
12012 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
12013 | mutex_lock(&bpf_verifier_lock); | |
12014 | if (!btf_vmlinux) | |
12015 | btf_vmlinux = btf_parse_vmlinux(); | |
12016 | mutex_unlock(&bpf_verifier_lock); | |
12017 | } | |
12018 | return btf_vmlinux; | |
12019 | } | |
12020 | ||
838e9690 YS |
12021 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
12022 | union bpf_attr __user *uattr) | |
51580e79 | 12023 | { |
06ee7115 | 12024 | u64 start_time = ktime_get_ns(); |
58e2af8b | 12025 | struct bpf_verifier_env *env; |
b9193c1b | 12026 | struct bpf_verifier_log *log; |
9e4c24e7 | 12027 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 12028 | bool is_priv; |
51580e79 | 12029 | |
eba0c929 AB |
12030 | /* no program is valid */ |
12031 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
12032 | return -EINVAL; | |
12033 | ||
58e2af8b | 12034 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
12035 | * allocate/free it every time bpf_check() is called |
12036 | */ | |
58e2af8b | 12037 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
12038 | if (!env) |
12039 | return -ENOMEM; | |
61bd5218 | 12040 | log = &env->log; |
cbd35700 | 12041 | |
9e4c24e7 | 12042 | len = (*prog)->len; |
fad953ce | 12043 | env->insn_aux_data = |
9e4c24e7 | 12044 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
12045 | ret = -ENOMEM; |
12046 | if (!env->insn_aux_data) | |
12047 | goto err_free_env; | |
9e4c24e7 JK |
12048 | for (i = 0; i < len; i++) |
12049 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 12050 | env->prog = *prog; |
00176a34 | 12051 | env->ops = bpf_verifier_ops[env->prog->type]; |
2c78ee89 | 12052 | is_priv = bpf_capable(); |
0246e64d | 12053 | |
76654e67 | 12054 | bpf_get_btf_vmlinux(); |
8580ac94 | 12055 | |
cbd35700 | 12056 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
12057 | if (!is_priv) |
12058 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
12059 | |
12060 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
12061 | /* user requested verbose verifier output | |
12062 | * and supplied buffer to store the verification trace | |
12063 | */ | |
e7bf8249 JK |
12064 | log->level = attr->log_level; |
12065 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
12066 | log->len_total = attr->log_size; | |
cbd35700 AS |
12067 | |
12068 | ret = -EINVAL; | |
e7bf8249 | 12069 | /* log attributes have to be sane */ |
7a9f5c65 | 12070 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 12071 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 12072 | goto err_unlock; |
cbd35700 | 12073 | } |
1ad2f583 | 12074 | |
8580ac94 AS |
12075 | if (IS_ERR(btf_vmlinux)) { |
12076 | /* Either gcc or pahole or kernel are broken. */ | |
12077 | verbose(env, "in-kernel BTF is malformed\n"); | |
12078 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 12079 | goto skip_full_check; |
8580ac94 AS |
12080 | } |
12081 | ||
1ad2f583 DB |
12082 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
12083 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 12084 | env->strict_alignment = true; |
e9ee9efc DM |
12085 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
12086 | env->strict_alignment = false; | |
cbd35700 | 12087 | |
2c78ee89 | 12088 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
41c48f3a | 12089 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
12090 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
12091 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
12092 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 12093 | |
10d274e8 AS |
12094 | if (is_priv) |
12095 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
12096 | ||
cae1927c | 12097 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 12098 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 12099 | if (ret) |
f4e3ec0d | 12100 | goto skip_full_check; |
ab3f0063 JK |
12101 | } |
12102 | ||
dc2a4ebc | 12103 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 12104 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
12105 | GFP_USER); |
12106 | ret = -ENOMEM; | |
12107 | if (!env->explored_states) | |
12108 | goto skip_full_check; | |
12109 | ||
d9762e84 | 12110 | ret = check_subprogs(env); |
475fb78f AS |
12111 | if (ret < 0) |
12112 | goto skip_full_check; | |
12113 | ||
c454a46b | 12114 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
12115 | if (ret < 0) |
12116 | goto skip_full_check; | |
12117 | ||
be8704ff AS |
12118 | ret = check_attach_btf_id(env); |
12119 | if (ret) | |
12120 | goto skip_full_check; | |
12121 | ||
4976b718 HL |
12122 | ret = resolve_pseudo_ldimm64(env); |
12123 | if (ret < 0) | |
12124 | goto skip_full_check; | |
12125 | ||
d9762e84 MKL |
12126 | ret = check_cfg(env); |
12127 | if (ret < 0) | |
12128 | goto skip_full_check; | |
12129 | ||
51c39bb1 AS |
12130 | ret = do_check_subprogs(env); |
12131 | ret = ret ?: do_check_main(env); | |
cbd35700 | 12132 | |
c941ce9c QM |
12133 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
12134 | ret = bpf_prog_offload_finalize(env); | |
12135 | ||
0246e64d | 12136 | skip_full_check: |
51c39bb1 | 12137 | kvfree(env->explored_states); |
0246e64d | 12138 | |
c131187d | 12139 | if (ret == 0) |
9b38c405 | 12140 | ret = check_max_stack_depth(env); |
c131187d | 12141 | |
9b38c405 | 12142 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
12143 | if (is_priv) { |
12144 | if (ret == 0) | |
12145 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
12146 | if (ret == 0) |
12147 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
12148 | if (ret == 0) |
12149 | ret = opt_remove_nops(env); | |
52875a04 JK |
12150 | } else { |
12151 | if (ret == 0) | |
12152 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
12153 | } |
12154 | ||
9bac3d6d AS |
12155 | if (ret == 0) |
12156 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
12157 | ret = convert_ctx_accesses(env); | |
12158 | ||
e245c5c6 | 12159 | if (ret == 0) |
79741b3b | 12160 | ret = fixup_bpf_calls(env); |
e245c5c6 | 12161 | |
a4b1d3c1 JW |
12162 | /* do 32-bit optimization after insn patching has done so those patched |
12163 | * insns could be handled correctly. | |
12164 | */ | |
d6c2308c JW |
12165 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
12166 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
12167 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
12168 | : false; | |
a4b1d3c1 JW |
12169 | } |
12170 | ||
1ea47e01 AS |
12171 | if (ret == 0) |
12172 | ret = fixup_call_args(env); | |
12173 | ||
06ee7115 AS |
12174 | env->verification_time = ktime_get_ns() - start_time; |
12175 | print_verification_stats(env); | |
12176 | ||
a2a7d570 | 12177 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 12178 | ret = -ENOSPC; |
a2a7d570 | 12179 | if (log->level && !log->ubuf) { |
cbd35700 | 12180 | ret = -EFAULT; |
a2a7d570 | 12181 | goto err_release_maps; |
cbd35700 AS |
12182 | } |
12183 | ||
541c3bad AN |
12184 | if (ret) |
12185 | goto err_release_maps; | |
12186 | ||
12187 | if (env->used_map_cnt) { | |
0246e64d | 12188 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
9bac3d6d AS |
12189 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
12190 | sizeof(env->used_maps[0]), | |
12191 | GFP_KERNEL); | |
0246e64d | 12192 | |
9bac3d6d | 12193 | if (!env->prog->aux->used_maps) { |
0246e64d | 12194 | ret = -ENOMEM; |
a2a7d570 | 12195 | goto err_release_maps; |
0246e64d AS |
12196 | } |
12197 | ||
9bac3d6d | 12198 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 12199 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 12200 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
541c3bad AN |
12201 | } |
12202 | if (env->used_btf_cnt) { | |
12203 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ | |
12204 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, | |
12205 | sizeof(env->used_btfs[0]), | |
12206 | GFP_KERNEL); | |
12207 | if (!env->prog->aux->used_btfs) { | |
12208 | ret = -ENOMEM; | |
12209 | goto err_release_maps; | |
12210 | } | |
0246e64d | 12211 | |
541c3bad AN |
12212 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
12213 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); | |
12214 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; | |
12215 | } | |
12216 | if (env->used_map_cnt || env->used_btf_cnt) { | |
0246e64d AS |
12217 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
12218 | * bpf_ld_imm64 instructions | |
12219 | */ | |
12220 | convert_pseudo_ld_imm64(env); | |
12221 | } | |
cbd35700 | 12222 | |
541c3bad | 12223 | adjust_btf_func(env); |
ba64e7d8 | 12224 | |
a2a7d570 | 12225 | err_release_maps: |
9bac3d6d | 12226 | if (!env->prog->aux->used_maps) |
0246e64d | 12227 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 12228 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
12229 | */ |
12230 | release_maps(env); | |
541c3bad AN |
12231 | if (!env->prog->aux->used_btfs) |
12232 | release_btfs(env); | |
03f87c0b THJ |
12233 | |
12234 | /* extension progs temporarily inherit the attach_type of their targets | |
12235 | for verification purposes, so set it back to zero before returning | |
12236 | */ | |
12237 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
12238 | env->prog->expected_attach_type = 0; | |
12239 | ||
9bac3d6d | 12240 | *prog = env->prog; |
3df126f3 | 12241 | err_unlock: |
45a73c17 AS |
12242 | if (!is_priv) |
12243 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
12244 | vfree(env->insn_aux_data); |
12245 | err_free_env: | |
12246 | kfree(env); | |
51580e79 AS |
12247 | return ret; |
12248 | } |